blob: 6e8ec3e64877a343f8fd3e08853bc5465b6dc484 [file] [log] [blame]
Mike Frysinger7031f622006-05-08 03:20:50 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +01002/*
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
Mike Frysinger7031f622006-05-08 03:20:50 +00004 *
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +01005 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00006 */
Mike Frysinger7031f622006-05-08 03:20:50 +00007#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +00008
Denys Vlasenkoac906fa2009-06-17 11:54:52 +02009#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020010unsigned dhcp_verbose;
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020011#endif
12
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000013const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = {
14 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
15};
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +010016
17/* Supported options are easily added here.
18 * See RFC2132 for more options.
19 * OPTION_REQ: these options are requested by udhcpc (unless -o).
20 */
21const struct dhcp_option dhcp_options[] = {
22 /* flags code */
23 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
24 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
25 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
26// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
27// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
28 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
29// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
30// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
31 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
32 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
33 { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */
34 { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */
35 { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */
36 { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */
37 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
38 { OPTION_U16 , 0x1a }, /* DHCP_MTU */
39 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
40 { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */
41 { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */
42 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
43 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
44 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
45 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
46 { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */
47//TODO: must be combined with 'sname' and 'file' handling:
48 { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */
49 { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */
50//TODO: not a string, but a set of LASCII strings:
51// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */
52#if ENABLE_FEATURE_UDHCP_RFC3397
53 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */
54#endif
55 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
56 { OPTION_STRING , 0xfc }, /* DHCP_WPAD */
57
58 /* Options below have no match in dhcp_option_strings[],
59 * are not passed to dhcpc scripts, and cannot be specified
60 * with "option XXX YYY" syntax in dhcpd config file.
61 * These entries are only used internally by udhcp[cd]
62 * to correctly encode options into packets.
63 */
64
65 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
66 { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */
67 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
68 { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */
69//FIXME: handling of this option is not exactly correct:
70 { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */
71 { 0, 0 } /* zeroed terminating entry */
72};
73
74/* Used for converting options from incoming packets to env variables
75 * for udhcpc stript, and for setting options for udhcpd via
76 * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file.
77 */
78/* Must match dhcp_options[] order */
79const char dhcp_option_strings[] ALIGN1 =
80 "subnet" "\0" /* DHCP_SUBNET */
81 "timezone" "\0" /* DHCP_TIME_OFFSET */
82 "router" "\0" /* DHCP_ROUTER */
83// "timesrv" "\0" /* DHCP_TIME_SERVER */
84// "namesrv" "\0" /* DHCP_NAME_SERVER */
85 "dns" "\0" /* DHCP_DNS_SERVER */
86// "logsrv" "\0" /* DHCP_LOG_SERVER */
87// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
88 "lprsrv" "\0" /* DHCP_LPR_SERVER */
89 "hostname" "\0" /* DHCP_HOST_NAME */
90 "bootsize" "\0" /* DHCP_BOOT_SIZE */
91 "domain" "\0" /* DHCP_DOMAIN_NAME */
92 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
93 "rootpath" "\0" /* DHCP_ROOT_PATH */
94 "ipttl" "\0" /* DHCP_IP_TTL */
95 "mtu" "\0" /* DHCP_MTU */
96 "broadcast" "\0" /* DHCP_BROADCAST */
97 "nisdomain" "\0" /* DHCP_NIS_DOMAIN */
98 "nissrv" "\0" /* DHCP_NIS_SERVER */
99 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
100 "wins" "\0" /* DHCP_WINS_SERVER */
101 "lease" "\0" /* DHCP_LEASE_TIME */
102 "serverid" "\0" /* DHCP_SERVER_ID */
103 "message" "\0" /* DHCP_ERR_MESSAGE */
104 "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */
105 "bootfile" "\0" /* DHCP_BOOT_FILE */
106// "userclass" "\0" /* DHCP_USER_CLASS */
107#if ENABLE_FEATURE_UDHCP_RFC3397
108 "search" "\0" /* DHCP_DOMAIN_SEARCH */
109#endif
110// "staticroutes" is only used to set udhcpc environment, it doesn't work
111// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet
112// by "string->option" conversion code:
113 "staticroutes" "\0"/* DHCP_STATIC_ROUTES */
114 "wpad" "\0" /* DHCP_WPAD */
115 ;
116
117/* Lengths of the different option types */
118const uint8_t dhcp_option_lengths[] ALIGN1 = {
119 [OPTION_IP] = 4,
120 [OPTION_IP_PAIR] = 8,
121// [OPTION_BOOLEAN] = 1,
122 [OPTION_STRING] = 1,
123#if ENABLE_FEATURE_UDHCP_RFC3397
124 [OPTION_STR1035] = 1,
125#endif
126 [OPTION_U8] = 1,
127 [OPTION_U16] = 2,
128// [OPTION_S16] = 2,
129 [OPTION_U32] = 4,
130 [OPTION_S32] = 4,
131 /* Just like OPTION_STRING, we use minimum length here */
132 [OPTION_STATIC_ROUTES] = 5,
133};
134
135
136#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
137static void log_option(const char *pfx, const uint8_t *opt)
138{
139 if (dhcp_verbose >= 2) {
140 char buf[256 * 2 + 2];
141 *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0';
142 bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf);
143 }
144}
145#else
146# define log_option(pfx, opt) ((void)0)
147#endif
148
Denys Vlasenko9107b632010-03-27 23:23:41 +0100149unsigned FAST_FUNC udhcp_option_idx(const char *name)
150{
151 int n = index_in_strings(dhcp_option_strings, name);
152 if (n >= 0)
153 return n;
154
155 {
156 char buf[sizeof(dhcp_option_strings)];
157 char *d = buf;
158 const char *s = dhcp_option_strings;
159 while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) {
160 *d++ = (*s == '\0' ? ' ' : *s);
161 s++;
162 }
163 *d = '\0';
164 bb_error_msg_and_die("unknown option '%s', known options: %s", name, buf);
165 }
166}
167
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100168/* get an option with bounds checking (warning, result is not aligned). */
169uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code)
170{
171 uint8_t *optionptr;
172 int len;
173 int rem;
174 int overload = 0;
175 enum {
176 FILE_FIELD101 = FILE_FIELD * 0x101,
177 SNAME_FIELD101 = SNAME_FIELD * 0x101,
178 };
179
180 /* option bytes: [code][len][data1][data2]..[dataLEN] */
181 optionptr = packet->options;
182 rem = sizeof(packet->options);
183 while (1) {
184 if (rem <= 0) {
185 bb_error_msg("bad packet, malformed option field");
186 return NULL;
187 }
188 if (optionptr[OPT_CODE] == DHCP_PADDING) {
189 rem--;
190 optionptr++;
191 continue;
192 }
193 if (optionptr[OPT_CODE] == DHCP_END) {
194 if ((overload & FILE_FIELD101) == FILE_FIELD) {
195 /* can use packet->file, and didn't look at it yet */
196 overload |= FILE_FIELD101; /* "we looked at it" */
197 optionptr = packet->file;
198 rem = sizeof(packet->file);
199 continue;
200 }
201 if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
202 /* can use packet->sname, and didn't look at it yet */
203 overload |= SNAME_FIELD101; /* "we looked at it" */
204 optionptr = packet->sname;
205 rem = sizeof(packet->sname);
206 continue;
207 }
208 break;
209 }
210 len = 2 + optionptr[OPT_LEN];
211 rem -= len;
212 if (rem < 0)
213 continue; /* complain and return NULL */
214
215 if (optionptr[OPT_CODE] == code) {
216 log_option("Option found", optionptr);
217 return optionptr + OPT_DATA;
218 }
219
220 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
221 overload |= optionptr[OPT_DATA];
222 /* fall through */
223 }
224 optionptr += len;
225 }
226
227 /* log3 because udhcpc uses it a lot - very noisy */
228 log3("Option 0x%02x not found", code);
229 return NULL;
230}
231
232/* return the position of the 'end' option (no bounds checking) */
233int FAST_FUNC udhcp_end_option(uint8_t *optionptr)
234{
235 int i = 0;
236
237 while (optionptr[i] != DHCP_END) {
238 if (optionptr[i] != DHCP_PADDING)
239 i += optionptr[i + OPT_LEN] + OPT_DATA-1;
240 i++;
241 }
242 return i;
243}
244
245/* Add an option (supplied in binary form) to the options.
246 * Option format: [code][len][data1][data2]..[dataLEN]
247 */
Denys Vlasenko7724c762010-03-26 09:32:09 +0100248void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100249{
250 unsigned len;
Denys Vlasenko7724c762010-03-26 09:32:09 +0100251 uint8_t *optionptr = packet->options;
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100252 unsigned end = udhcp_end_option(optionptr);
253
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100254 len = OPT_DATA + addopt[OPT_LEN];
Denys Vlasenko7724c762010-03-26 09:32:09 +0100255 /* end position + (option code/length + addopt length) + end option */
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100256 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100257//TODO: learn how to use overflow option if we exhaust packet->options[]
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100258 bb_error_msg("option 0x%02x did not fit into the packet",
259 addopt[OPT_CODE]);
260 return;
261 }
262 log_option("Adding option", addopt);
263 memcpy(optionptr + end, addopt, len);
264 optionptr[end + len] = DHCP_END;
265}
266
Denys Vlasenko7724c762010-03-26 09:32:09 +0100267/* Add an one to four byte option to a packet */
268void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data)
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100269{
270 const struct dhcp_option *dh;
271
272 for (dh = dhcp_options; dh->code; dh++) {
273 if (dh->code == code) {
274 uint8_t option[6], len;
275
276 option[OPT_CODE] = code;
277 len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK];
278 option[OPT_LEN] = len;
279 if (BB_BIG_ENDIAN)
280 data <<= 8 * (4 - len);
281 /* Assignment is unaligned! */
282 move_to_unaligned32(&option[OPT_DATA], data);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100283 udhcp_add_binary_option(packet, option);
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100284 return;
285 }
286 }
287
288 bb_error_msg("can't add option 0x%02x", code);
289}
290
291/* Find option 'code' in opt_list */
Denys Vlasenko7724c762010-03-26 09:32:09 +0100292struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code)
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100293{
294 while (opt_list && opt_list->data[OPT_CODE] < code)
295 opt_list = opt_list->next;
296
297 if (opt_list && opt_list->data[OPT_CODE] == code)
298 return opt_list;
299 return NULL;
300}
301
302/* Parse string to IP in network order */
303int FAST_FUNC udhcp_str2nip(const char *str, void *arg)
304{
305 len_and_sockaddr *lsa;
306
307 lsa = host_and_af2sockaddr(str, 0, AF_INET);
308 if (!lsa)
309 return 0;
310 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
311 free(lsa);
312 return 1;
313}
314
315/* udhcp_str2optset:
316 * Parse string option representation to binary form
317 * and add it to opt_list
318 */
319/* helper: add an option to the opt_list */
320static NOINLINE void attach_option(
321 struct option_set **opt_list,
322 const struct dhcp_option *option,
323 char *buffer,
324 int length)
325{
326 struct option_set *existing, *new, **curr;
327#if ENABLE_FEATURE_UDHCP_RFC3397
328 char *allocated = NULL;
329#endif
330
Denys Vlasenko7724c762010-03-26 09:32:09 +0100331 existing = udhcp_find_option(*opt_list, option->code);
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100332 if (!existing) {
333 log2("Attaching option %02x to list", option->code);
334#if ENABLE_FEATURE_UDHCP_RFC3397
335 if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
336 /* reuse buffer and length for RFC1035-formatted string */
337 allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
338 }
339#endif
340 /* make a new option */
341 new = xmalloc(sizeof(*new));
342 new->data = xmalloc(length + OPT_DATA);
343 new->data[OPT_CODE] = option->code;
344 new->data[OPT_LEN] = length;
345 memcpy(new->data + OPT_DATA, buffer, length);
346
347 curr = opt_list;
348 while (*curr && (*curr)->data[OPT_CODE] < option->code)
349 curr = &(*curr)->next;
350
351 new->next = *curr;
352 *curr = new;
353 goto ret;
354 }
355
356 if (option->flags & OPTION_LIST) {
357 unsigned old_len;
358
359 /* add it to an existing option */
360 log1("Attaching option %02x to existing member of list", option->code);
361 old_len = existing->data[OPT_LEN];
362#if ENABLE_FEATURE_UDHCP_RFC3397
363 if ((option->flags & OPTION_TYPE_MASK) == OPTION_STR1035) {
364 /* reuse buffer and length for RFC1035-formatted string */
365 allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length);
366 }
367#endif
368 if (old_len + length < 255) {
369 /* actually 255 is ok too, but adding a space can overlow it */
370
371 existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
372 if ((option->flags & OPTION_TYPE_MASK) == OPTION_STRING) {
373 /* add space separator between STRING options in a list */
374 existing->data[OPT_DATA + old_len] = ' ';
375 old_len++;
376 }
377 memcpy(existing->data + OPT_DATA + old_len, buffer, length);
378 existing->data[OPT_LEN] = old_len + length;
379 } /* else, ignore the data, we could put this in a second option in the future */
380 } /* else, ignore the new data */
381
382 ret: ;
383#if ENABLE_FEATURE_UDHCP_RFC3397
384 free(allocated);
385#endif
386}
387
388int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg)
389{
390 struct option_set **opt_list = arg;
391 char *opt, *val, *endptr;
392 char *str;
393 const struct dhcp_option *option;
Denys Vlasenko9107b632010-03-27 23:23:41 +0100394 int retval, length;
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100395 char buffer[8] ALIGNED(4);
396 uint16_t *result_u16 = (uint16_t *) buffer;
397 uint32_t *result_u32 = (uint32_t *) buffer;
398
399 /* Cheat, the only *const* str possible is "" */
400 str = (char *) const_str;
401 opt = strtok(str, " \t=");
402 if (!opt)
403 return 0;
404
Denys Vlasenko9107b632010-03-27 23:23:41 +0100405 option = &dhcp_options[udhcp_option_idx(opt)];
Denys Vlasenkoa8f6b992010-03-26 08:35:24 +0100406
407 retval = 0;
408 do {
409 val = strtok(NULL, ", \t");
410 if (!val)
411 break;
412 length = dhcp_option_lengths[option->flags & OPTION_TYPE_MASK];
413 retval = 0;
414 opt = buffer; /* new meaning for variable opt */
415 switch (option->flags & OPTION_TYPE_MASK) {
416 case OPTION_IP:
417 retval = udhcp_str2nip(val, buffer);
418 break;
419 case OPTION_IP_PAIR:
420 retval = udhcp_str2nip(val, buffer);
421 val = strtok(NULL, ", \t/-");
422 if (!val)
423 retval = 0;
424 if (retval)
425 retval = udhcp_str2nip(val, buffer + 4);
426 break;
427 case OPTION_STRING:
428#if ENABLE_FEATURE_UDHCP_RFC3397
429 case OPTION_STR1035:
430#endif
431 length = strnlen(val, 254);
432 if (length > 0) {
433 opt = val;
434 retval = 1;
435 }
436 break;
437// case OPTION_BOOLEAN: {
438// static const char noyes[] ALIGN1 = "no\0yes\0";
439// buffer[0] = retval = index_in_strings(noyes, val);
440// retval++; /* 0 - bad; 1: "no" 2: "yes" */
441// break;
442// }
443 case OPTION_U8:
444 buffer[0] = strtoul(val, &endptr, 0);
445 retval = (endptr[0] == '\0');
446 break;
447 /* htonX are macros in older libc's, using temp var
448 * in code below for safety */
449 /* TODO: use bb_strtoX? */
450 case OPTION_U16: {
451 unsigned long tmp = strtoul(val, &endptr, 0);
452 *result_u16 = htons(tmp);
453 retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
454 break;
455 }
456// case OPTION_S16: {
457// long tmp = strtol(val, &endptr, 0);
458// *result_u16 = htons(tmp);
459// retval = (endptr[0] == '\0');
460// break;
461// }
462 case OPTION_U32: {
463 unsigned long tmp = strtoul(val, &endptr, 0);
464 *result_u32 = htonl(tmp);
465 retval = (endptr[0] == '\0');
466 break;
467 }
468 case OPTION_S32: {
469 long tmp = strtol(val, &endptr, 0);
470 *result_u32 = htonl(tmp);
471 retval = (endptr[0] == '\0');
472 break;
473 }
474 default:
475 break;
476 }
477 if (retval)
478 attach_option(opt_list, option, opt, length);
479 } while (retval && option->flags & OPTION_LIST);
480
481 return retval;
482}