blob: 5cc08cca082866b0183621a1b4ad4c485c15f60a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * options.c -- DHCP server option packet tools
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5 */
6
7#include <stdlib.h>
8#include <string.h>
9
10#include "common.h"
11#include "dhcpd.h"
12#include "options.h"
13#include "files.h"
14
15
16/* supported options are easily added here */
17struct dhcp_option dhcp_options[] = {
18 /* name[10] flags code */
19 {"subnet", OPTION_IP | OPTION_REQ, 0x01},
20 {"timezone", OPTION_S32, 0x02},
21 {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
22 {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
23 {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
24 {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
25 {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
26 {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
27 {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
28 {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
29 {"bootsize", OPTION_U16, 0x0d},
30 {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
31 {"swapsvr", OPTION_IP, 0x10},
32 {"rootpath", OPTION_STRING, 0x11},
33 {"ipttl", OPTION_U8, 0x17},
34 {"mtu", OPTION_U16, 0x1a},
35 {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
36 {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28},
37 {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29},
38 {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a},
39 {"wins", OPTION_IP | OPTION_LIST, 0x2c},
40 {"requestip", OPTION_IP, 0x32},
41 {"lease", OPTION_U32, 0x33},
42 {"dhcptype", OPTION_U8, 0x35},
43 {"serverid", OPTION_IP, 0x36},
44 {"message", OPTION_STRING, 0x38},
Denis Vlasenkoe175ff22006-09-26 17:41:00 +000045 {"vendorclass", OPTION_STRING, 0x3C},
46 {"clientid", OPTION_STRING, 0x3D},
Mike Frysinger7031f622006-05-08 03:20:50 +000047 {"tftp", OPTION_STRING, 0x42},
48 {"bootfile", OPTION_STRING, 0x43},
Denis Vlasenkoe175ff22006-09-26 17:41:00 +000049 {"userclass", OPTION_STRING, 0x4D},
Mike Frysinger7031f622006-05-08 03:20:50 +000050 {"", 0x00, 0x00}
51};
52
53/* Lengths of the different option types */
54int option_lengths[] = {
55 [OPTION_IP] = 4,
56 [OPTION_IP_PAIR] = 8,
57 [OPTION_BOOLEAN] = 1,
58 [OPTION_STRING] = 1,
59 [OPTION_U8] = 1,
60 [OPTION_U16] = 2,
61 [OPTION_S16] = 2,
62 [OPTION_U32] = 4,
63 [OPTION_S32] = 4
64};
65
66
67/* get an option with bounds checking (warning, not aligned). */
68uint8_t *get_option(struct dhcpMessage *packet, int code)
69{
70 int i, length;
71 uint8_t *optionptr;
72 int over = 0, done = 0, curr = OPTION_FIELD;
73
74 optionptr = packet->options;
75 i = 0;
76 length = 308;
77 while (!done) {
78 if (i >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000079 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000080 return NULL;
81 }
82 if (optionptr[i + OPT_CODE] == code) {
83 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000084 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000085 return NULL;
86 }
87 return optionptr + i + 2;
88 }
89 switch (optionptr[i + OPT_CODE]) {
90 case DHCP_PADDING:
91 i++;
92 break;
93 case DHCP_OPTION_OVER:
94 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000095 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000096 return NULL;
97 }
98 over = optionptr[i + 3];
99 i += optionptr[OPT_LEN] + 2;
100 break;
101 case DHCP_END:
102 if (curr == OPTION_FIELD && over & FILE_FIELD) {
103 optionptr = packet->file;
104 i = 0;
105 length = 128;
106 curr = FILE_FIELD;
107 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
108 optionptr = packet->sname;
109 i = 0;
110 length = 64;
111 curr = SNAME_FIELD;
112 } else done = 1;
113 break;
114 default:
115 i += optionptr[OPT_LEN + i] + 2;
116 }
117 }
118 return NULL;
119}
120
121
122/* return the position of the 'end' option (no bounds checking) */
123int end_option(uint8_t *optionptr)
124{
125 int i = 0;
126
127 while (optionptr[i] != DHCP_END) {
128 if (optionptr[i] == DHCP_PADDING) i++;
129 else i += optionptr[i + OPT_LEN] + 2;
130 }
131 return i;
132}
133
134
135/* add an option string to the options (an option string contains an option code,
136 * length, then data) */
137int add_option_string(uint8_t *optionptr, uint8_t *string)
138{
139 int end = end_option(optionptr);
140
141 /* end position + string length + option code/length + end option */
142 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000143 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000144 string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000145 return 0;
146 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000147 DEBUG("adding option 0x%02x", string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000148 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
149 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
150 return string[OPT_LEN] + 2;
151}
152
153
154/* add a one to four byte option to a packet */
155int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
156{
157 struct dhcp_option *dh;
158
159 for (dh=dhcp_options; dh->code; dh++) {
160 if (dh->code == code) {
161 uint8_t option[6], len;
162
163 option[OPT_CODE] = code;
164 len = option_lengths[dh->flags & TYPE_MASK];
165 option[OPT_LEN] = len;
Rob Landleyc8b360e2006-05-31 22:24:33 +0000166 if (BB_BIG_ENDIAN) data <<= 8 * (4 - len);
Mike Frysinger7031f622006-05-08 03:20:50 +0000167 /* This memcpy is for broken processors which can't
168 * handle a simple unaligned 32-bit assignment */
169 memcpy(&option[OPT_DATA], &data, 4);
170 return add_option_string(optionptr, option);
171 }
172 }
173
Denis Vlasenkoea620772006-10-14 02:23:43 +0000174 bb_error_msg("cannot add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000175 return 0;
176}