1
2
3
4
5
6
7#include "common.h"
8
9#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
10unsigned dhcp_verbose;
11#endif
12
13const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = {
14 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
15};
16
17
18
19
20
21const struct dhcp_optflag dhcp_optflags[] = {
22
23 { OPTION_IP | OPTION_REQ, 0x01 },
24 { OPTION_S32 , 0x02 },
25 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 },
26
27
28 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 },
29
30
31 { OPTION_IP | OPTION_LIST , 0x09 },
32 { OPTION_STRING | OPTION_REQ, 0x0c },
33 { OPTION_U16 , 0x0d },
34 { OPTION_STRING | OPTION_REQ, 0x0f },
35 { OPTION_IP , 0x10 },
36 { OPTION_STRING , 0x11 },
37 { OPTION_U8 , 0x17 },
38 { OPTION_U16 , 0x1a },
39 { OPTION_IP | OPTION_REQ, 0x1c },
40 { OPTION_STRING , 0x28 },
41 { OPTION_IP | OPTION_LIST , 0x29 },
42 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a },
43 { OPTION_IP | OPTION_LIST , 0x2c },
44 { OPTION_U32 , 0x33 },
45 { OPTION_IP , 0x36 },
46 { OPTION_STRING , 0x38 },
47
48 { OPTION_STRING , 0x42 },
49 { OPTION_STRING , 0x43 },
50
51
52#if ENABLE_FEATURE_UDHCP_RFC3397
53 { OPTION_DNS_STRING | OPTION_LIST , 0x77 },
54 { OPTION_SIP_SERVERS , 0x78 },
55#endif
56 { OPTION_STATIC_ROUTES , 0x79 },
57 { OPTION_STRING , 0xfc },
58
59
60
61
62
63
64
65
66 { OPTION_IP , 0x32 },
67 { OPTION_U8 , 0x35 },
68 { OPTION_U16 , 0x39 },
69 { OPTION_STRING , 0x3c },
70
71 { OPTION_STRING , 0x3d },
72 { 0, 0 }
73};
74
75
76
77
78
79
80const char dhcp_option_strings[] ALIGN1 =
81 "subnet" "\0"
82 "timezone" "\0"
83 "router" "\0"
84
85
86 "dns" "\0"
87
88
89 "lprsrv" "\0"
90 "hostname" "\0"
91 "bootsize" "\0"
92 "domain" "\0"
93 "swapsrv" "\0"
94 "rootpath" "\0"
95 "ipttl" "\0"
96 "mtu" "\0"
97 "broadcast" "\0"
98 "nisdomain" "\0"
99 "nissrv" "\0"
100 "ntpsrv" "\0"
101 "wins" "\0"
102 "lease" "\0"
103 "serverid" "\0"
104 "message" "\0"
105 "tftp" "\0"
106 "bootfile" "\0"
107
108#if ENABLE_FEATURE_UDHCP_RFC3397
109 "search" "\0"
110
111
112 "sipsrv" "\0"
113#endif
114
115
116 "staticroutes" "\0"
117 "wpad" "\0"
118 ;
119
120
121
122
123
124
125
126
127
128const uint8_t dhcp_option_lengths[] ALIGN1 = {
129 [OPTION_IP] = 4,
130 [OPTION_IP_PAIR] = 8,
131
132 [OPTION_STRING] = 1,
133#if ENABLE_FEATURE_UDHCP_RFC3397
134 [OPTION_DNS_STRING] = 1,
135 [OPTION_SIP_SERVERS] = 1,
136#endif
137 [OPTION_U8] = 1,
138 [OPTION_U16] = 2,
139
140 [OPTION_U32] = 4,
141 [OPTION_S32] = 4,
142
143 [OPTION_STATIC_ROUTES] = 5,
144};
145
146
147#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
148static void log_option(const char *pfx, const uint8_t *opt)
149{
150 if (dhcp_verbose >= 2) {
151 char buf[256 * 2 + 2];
152 *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0';
153 bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf);
154 }
155}
156#else
157# define log_option(pfx, opt) ((void)0)
158#endif
159
160unsigned FAST_FUNC udhcp_option_idx(const char *name)
161{
162 int n = index_in_strings(dhcp_option_strings, name);
163 if (n >= 0)
164 return n;
165
166 {
167 char buf[sizeof(dhcp_option_strings)];
168 char *d = buf;
169 const char *s = dhcp_option_strings;
170 while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) {
171 *d++ = (*s == '\0' ? ' ' : *s);
172 s++;
173 }
174 *d = '\0';
175 bb_error_msg_and_die("unknown option '%s', known options: %s", name, buf);
176 }
177}
178
179
180uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code)
181{
182 uint8_t *optionptr;
183 int len;
184 int rem;
185 int overload = 0;
186 enum {
187 FILE_FIELD101 = FILE_FIELD * 0x101,
188 SNAME_FIELD101 = SNAME_FIELD * 0x101,
189 };
190
191
192 optionptr = packet->options;
193 rem = sizeof(packet->options);
194 while (1) {
195 if (rem <= 0) {
196 bb_error_msg("bad packet, malformed option field");
197 return NULL;
198 }
199 if (optionptr[OPT_CODE] == DHCP_PADDING) {
200 rem--;
201 optionptr++;
202 continue;
203 }
204 if (optionptr[OPT_CODE] == DHCP_END) {
205 if ((overload & FILE_FIELD101) == FILE_FIELD) {
206
207 overload |= FILE_FIELD101;
208 optionptr = packet->file;
209 rem = sizeof(packet->file);
210 continue;
211 }
212 if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
213
214 overload |= SNAME_FIELD101;
215 optionptr = packet->sname;
216 rem = sizeof(packet->sname);
217 continue;
218 }
219 break;
220 }
221 len = 2 + optionptr[OPT_LEN];
222 rem -= len;
223 if (rem < 0)
224 continue;
225
226 if (optionptr[OPT_CODE] == code) {
227 log_option("Option found", optionptr);
228 return optionptr + OPT_DATA;
229 }
230
231 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
232 overload |= optionptr[OPT_DATA];
233
234 }
235 optionptr += len;
236 }
237
238
239 log3("Option 0x%02x not found", code);
240 return NULL;
241}
242
243
244int FAST_FUNC udhcp_end_option(uint8_t *optionptr)
245{
246 int i = 0;
247
248 while (optionptr[i] != DHCP_END) {
249 if (optionptr[i] != DHCP_PADDING)
250 i += optionptr[i + OPT_LEN] + OPT_DATA-1;
251 i++;
252 }
253 return i;
254}
255
256
257
258
259void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
260{
261 unsigned len;
262 uint8_t *optionptr = packet->options;
263 unsigned end = udhcp_end_option(optionptr);
264
265 len = OPT_DATA + addopt[OPT_LEN];
266
267 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) {
268
269 bb_error_msg("option 0x%02x did not fit into the packet",
270 addopt[OPT_CODE]);
271 return;
272 }
273 log_option("Adding option", addopt);
274 memcpy(optionptr + end, addopt, len);
275 optionptr[end + len] = DHCP_END;
276}
277
278
279void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data)
280{
281 const struct dhcp_optflag *dh;
282
283 for (dh = dhcp_optflags; dh->code; dh++) {
284 if (dh->code == code) {
285 uint8_t option[6], len;
286
287 option[OPT_CODE] = code;
288 len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK];
289 option[OPT_LEN] = len;
290 if (BB_BIG_ENDIAN)
291 data <<= 8 * (4 - len);
292
293 move_to_unaligned32(&option[OPT_DATA], data);
294 udhcp_add_binary_option(packet, option);
295 return;
296 }
297 }
298
299 bb_error_msg("can't add option 0x%02x", code);
300}
301
302
303struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code)
304{
305 while (opt_list && opt_list->data[OPT_CODE] < code)
306 opt_list = opt_list->next;
307
308 if (opt_list && opt_list->data[OPT_CODE] == code)
309 return opt_list;
310 return NULL;
311}
312
313
314int FAST_FUNC udhcp_str2nip(const char *str, void *arg)
315{
316 len_and_sockaddr *lsa;
317
318 lsa = host_and_af2sockaddr(str, 0, AF_INET);
319 if (!lsa)
320 return 0;
321 *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
322 free(lsa);
323 return 1;
324}
325
326
327
328
329
330
331
332static char *allocate_tempopt_if_needed(
333 const struct dhcp_optflag *optflag,
334 char *buffer,
335 int *length_p)
336{
337 char *allocated = NULL;
338 if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_BIN) {
339 const char *end;
340 allocated = xstrdup(buffer);
341 end = hex2bin(allocated, buffer, 255);
342 if (errno)
343 bb_error_msg_and_die("malformed hex string '%s'", buffer);
344 *length_p = end - allocated;
345 }
346 return allocated;
347}
348
349static NOINLINE void attach_option(
350 struct option_set **opt_list,
351 const struct dhcp_optflag *optflag,
352 char *buffer,
353 int length)
354{
355 struct option_set *existing, *new, **curr;
356 char *allocated = NULL;
357
358 existing = udhcp_find_option(*opt_list, optflag->code);
359 if (!existing) {
360 log2("Attaching option %02x to list", optflag->code);
361 allocated = allocate_tempopt_if_needed(optflag, buffer, &length);
362#if ENABLE_FEATURE_UDHCP_RFC3397
363 if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) {
364
365 allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
366 }
367#endif
368
369 new = xmalloc(sizeof(*new));
370 new->data = xmalloc(length + OPT_DATA);
371 new->data[OPT_CODE] = optflag->code;
372 new->data[OPT_LEN] = length;
373 memcpy(new->data + OPT_DATA, buffer, length);
374
375 curr = opt_list;
376 while (*curr && (*curr)->data[OPT_CODE] < optflag->code)
377 curr = &(*curr)->next;
378
379 new->next = *curr;
380 *curr = new;
381 goto ret;
382 }
383
384 if (optflag->flags & OPTION_LIST) {
385 unsigned old_len;
386
387
388 log2("Attaching option %02x to existing member of list", optflag->code);
389 allocated = allocate_tempopt_if_needed(optflag, buffer, &length);
390 old_len = existing->data[OPT_LEN];
391#if ENABLE_FEATURE_UDHCP_RFC3397
392 if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) {
393
394 allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length);
395 }
396#endif
397 if (old_len + length < 255) {
398
399
400 existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
401 if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING) {
402
403 existing->data[OPT_DATA + old_len] = ' ';
404 old_len++;
405 }
406 memcpy(existing->data + OPT_DATA + old_len, buffer, length);
407 existing->data[OPT_LEN] = old_len + length;
408 }
409 }
410
411 ret:
412 free(allocated);
413}
414
415int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg)
416{
417 struct option_set **opt_list = arg;
418 char *opt, *val, *endptr;
419 char *str;
420 const struct dhcp_optflag *optflag;
421 struct dhcp_optflag bin_optflag;
422 unsigned optcode;
423 int retval, length;
424 char buffer[8] ALIGNED(4);
425 uint16_t *result_u16 = (uint16_t *) buffer;
426 uint32_t *result_u32 = (uint32_t *) buffer;
427
428
429 str = (char *) const_str;
430 opt = strtok(str, " \t=");
431 if (!opt)
432 return 0;
433
434 optcode = bb_strtou(opt, NULL, 0);
435 if (!errno && optcode < 255) {
436
437 bin_optflag.flags = OPTION_BIN;
438 bin_optflag.code = optcode;
439 optflag = &bin_optflag;
440 } else {
441 optflag = &dhcp_optflags[udhcp_option_idx(opt)];
442 }
443
444 retval = 0;
445 do {
446 val = strtok(NULL, ", \t");
447 if (!val)
448 break;
449 length = dhcp_option_lengths[optflag->flags & OPTION_TYPE_MASK];
450 retval = 0;
451 opt = buffer;
452 switch (optflag->flags & OPTION_TYPE_MASK) {
453 case OPTION_IP:
454 retval = udhcp_str2nip(val, buffer);
455 break;
456 case OPTION_IP_PAIR:
457 retval = udhcp_str2nip(val, buffer);
458 val = strtok(NULL, ", \t/-");
459 if (!val)
460 retval = 0;
461 if (retval)
462 retval = udhcp_str2nip(val, buffer + 4);
463 break;
464 case OPTION_STRING:
465#if ENABLE_FEATURE_UDHCP_RFC3397
466 case OPTION_DNS_STRING:
467#endif
468 length = strnlen(val, 254);
469 if (length > 0) {
470 opt = val;
471 retval = 1;
472 }
473 break;
474
475
476
477
478
479
480 case OPTION_U8:
481 buffer[0] = strtoul(val, &endptr, 0);
482 retval = (endptr[0] == '\0');
483 break;
484
485
486
487 case OPTION_U16: {
488 unsigned long tmp = strtoul(val, &endptr, 0);
489 *result_u16 = htons(tmp);
490 retval = (endptr[0] == '\0' );
491 break;
492 }
493
494
495
496
497
498
499 case OPTION_U32: {
500 unsigned long tmp = strtoul(val, &endptr, 0);
501 *result_u32 = htonl(tmp);
502 retval = (endptr[0] == '\0');
503 break;
504 }
505 case OPTION_S32: {
506 long tmp = strtol(val, &endptr, 0);
507 *result_u32 = htonl(tmp);
508 retval = (endptr[0] == '\0');
509 break;
510 }
511 case OPTION_BIN:
512 opt = val;
513 retval = 1;
514 default:
515 break;
516 }
517 if (retval)
518 attach_option(opt_list, optflag, opt, length);
519 } while (retval && optflag->flags & OPTION_LIST);
520
521 return retval;
522}
523