2 * options.c -- DHCP server option packet tools
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
15 /* supported options are easily added here */
16 struct dhcp_option dhcp_options[] = {
17 /* name[10] flags code */
18 {"subnet", OPTION_IP | OPTION_REQ, 0x01},
19 {"timezone", OPTION_S32, 0x02},
20 {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
21 {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
22 {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
23 {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
24 {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
25 {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
26 {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
27 {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
28 {"bootsize", OPTION_U16, 0x0d},
29 {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
30 {"swapsvr", OPTION_IP, 0x10},
31 {"rootpath", OPTION_STRING, 0x11},
32 {"ipttl", OPTION_U8, 0x17},
33 {"mtu", OPTION_U16, 0x1a},
34 {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
35 {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28},
36 {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29},
37 {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a},
38 {"wins", OPTION_IP | OPTION_LIST, 0x2c},
39 {"requestip", OPTION_IP, 0x32},
40 {"lease", OPTION_U32, 0x33},
41 {"dhcptype", OPTION_U8, 0x35},
42 {"serverid", OPTION_IP, 0x36},
43 {"message", OPTION_STRING, 0x38},
44 {"tftp", OPTION_STRING, 0x42},
45 {"bootfile", OPTION_STRING, 0x43},
49 /* Lengths of the different option types */
50 int option_lengths[] = {
63 /* get an option with bounds checking (warning, not aligned). */
64 uint8_t *get_option(struct dhcpMessage *packet, int code)
68 int over = 0, done = 0, curr = OPTION_FIELD;
70 optionptr = packet->options;
75 LOG(LOG_WARNING, "bogus packet, option fields too long.");
78 if (optionptr[i + OPT_CODE] == code) {
79 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
80 LOG(LOG_WARNING, "bogus packet, option fields too long.");
83 return optionptr + i + 2;
85 switch (optionptr[i + OPT_CODE]) {
89 case DHCP_OPTION_OVER:
90 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
91 LOG(LOG_WARNING, "bogus packet, option fields too long.");
94 over = optionptr[i + 3];
95 i += optionptr[OPT_LEN] + 2;
98 if (curr == OPTION_FIELD && over & FILE_FIELD) {
99 optionptr = packet->file;
103 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
104 optionptr = packet->sname;
111 i += optionptr[OPT_LEN + i] + 2;
118 /* return the position of the 'end' option (no bounds checking) */
119 int end_option(uint8_t *optionptr)
123 while (optionptr[i] != DHCP_END) {
124 if (optionptr[i] == DHCP_PADDING) i++;
125 else i += optionptr[i + OPT_LEN] + 2;
131 /* add an option string to the options (an option string contains an option code,
132 * length, then data) */
133 int add_option_string(uint8_t *optionptr, uint8_t *string)
135 int end = end_option(optionptr);
137 /* end position + string length + option code/length + end option */
138 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
139 LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
142 DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
143 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
144 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
145 return string[OPT_LEN] + 2;
149 /* add a one to four byte option to a packet */
150 int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
152 struct dhcp_option *dh;
154 for (dh=dhcp_options; dh->code; dh++) {
155 if (dh->code == code) {
156 uint8_t option[6], len;
158 option[OPT_CODE] = code;
159 len = option_lengths[dh->flags & TYPE_MASK];
160 option[OPT_LEN] = len;
161 if (__BYTE_ORDER == __BIG_ENDIAN)
162 data <<= 8 * (4 - len);
163 /* This memcpy is for broken processors which can't
164 * handle a simple unaligned 32-bit assignment */
165 memcpy(&option[OPT_DATA], &data, 4);
166 return add_option_string(optionptr, option);
170 DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
175 /* find option 'code' in opt_list */
176 struct option_set *find_option(struct option_set *opt_list, char code)
178 while (opt_list && opt_list->data[OPT_CODE] < code)
179 opt_list = opt_list->next;
181 if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
186 /* add an option to the opt_list */
187 void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
189 struct option_set *existing, *new, **curr;
191 /* add it to an existing option */
192 if ((existing = find_option(*opt_list, option->code))) {
193 DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
194 if (option->flags & OPTION_LIST) {
195 if (existing->data[OPT_LEN] + length <= 255) {
196 existing->data = realloc(existing->data,
197 existing->data[OPT_LEN] + length + 2);
198 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
199 existing->data[OPT_LEN] += length;
200 } /* else, ignore the data, we could put this in a second option in the future */
201 } /* else, ignore the new data */
203 DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
205 /* make a new option */
206 new = xmalloc(sizeof(struct option_set));
207 new->data = xmalloc(length + 2);
208 new->data[OPT_CODE] = option->code;
209 new->data[OPT_LEN] = length;
210 memcpy(new->data + 2, buffer, length);
213 while (*curr && (*curr)->data[OPT_CODE] < option->code)
214 curr = &(*curr)->next;