1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <unistd.h>
35
36 #include "config.h"
37 #include "common.h"
38 #include "dhcp.h"
39
40 #define REQUEST (1 << 0)
41 #define UINT8 (1 << 1)
42 #define UINT16 (1 << 2)
43 #define SINT16 (1 << 3)
44 #define UINT32 (1 << 4)
45 #define SINT32 (1 << 5)
46 #define IPV4 (1 << 6)
47 #define STRING (1 << 7)
48 #define PAIR (1 << 8)
49 #define ARRAY (1 << 9)
50 #define RFC3361 (1 << 10)
51 #define RFC3397 (1 << 11)
52 #define RFC3442 (1 << 12)
53 #define RFC5969 (1 << 13)
54
55 #define IPV4R IPV4 | REQUEST
56
57 #define DAD "Duplicate address detected"
58
59 /* Our aggregate option buffer.
60 * We ONLY use this when options are split, which for most purposes is
61 * practically never. See RFC3396 for details. */
62 static uint8_t *opt_buffer;
63
64 struct dhcp_opt {
65 uint8_t option;
66 int type;
67 const char *var;
68 };
69
70 static const struct dhcp_opt const dhcp_opts[] = {
71 { 1, IPV4 | REQUEST, "subnet_mask" },
72 /* RFC 3442 states that the CSR has to come before all other
73 * routes. For completeness, we also specify static routes,
74 * then routers. */
75 { 121, RFC3442, "classless_static_routes" },
76 { 249, RFC3442, "ms_classless_static_routes" },
77 { 33, IPV4 | ARRAY | REQUEST, "static_routes" },
78 { 3, IPV4 | ARRAY | REQUEST, "routers" },
79 { 2, UINT32, "time_offset" },
80 { 4, IPV4 | ARRAY, "time_servers" },
81 { 5, IPV4 | ARRAY, "ien116_name_servers" },
82 /* Explicitly include DNS in the list of parameters requested in the DNS request.
83 * Without this some DHCP servers may skip the DNS entries in the DHCP replies.*/
84 { 6, IPV4 | ARRAY | REQUEST, "domain_name_servers" },
85 { 7, IPV4 | ARRAY, "log_servers" },
86 { 8, IPV4 | ARRAY, "cookie_servers" },
87 { 9, IPV4 | ARRAY, "lpr_servers" },
88 { 10, IPV4 | ARRAY, "impress_servers" },
89 { 11, IPV4 | ARRAY, "resource_location_servers" },
90 { 12, STRING, "host_name" },
91 { 13, UINT16, "boot_size" },
92 { 14, STRING, "merit_dump" },
93 /* Explicitly include DNS in the list of parameters requested in the DNS request.
94 * Without this some DHCP servers may skip the DNS entries in the DHCP replies.*/
95 { 15, STRING | REQUEST, "domain_name" },
96 { 16, IPV4, "swap_server" },
97 { 17, STRING, "root_path" },
98 { 18, STRING, "extensions_path" },
99 { 19, UINT8, "ip_forwarding" },
100 { 20, UINT8, "non_local_source_routing" },
101 { 21, IPV4 | ARRAY, "policy_filter" },
102 { 22, SINT16, "max_dgram_reassembly" },
103 { 23, UINT16, "default_ip_ttl" },
104 { 24, UINT32, "path_mtu_aging_timeout" },
105 { 25, UINT16 | ARRAY, "path_mtu_plateau_table" },
106 { 26, UINT16, "interface_mtu" },
107 { 27, UINT8, "all_subnets_local" },
108 { 28, IPV4 | REQUEST, "broadcast_address" },
109 { 29, UINT8, "perform_mask_discovery" },
110 { 30, UINT8, "mask_supplier" },
111 { 31, UINT8, "router_discovery" },
112 { 32, IPV4, "router_solicitation_address" },
113 { 34, UINT8, "trailer_encapsulation" },
114 { 35, UINT32, "arp_cache_timeout" },
115 { 36, UINT16, "ieee802_3_encapsulation" },
116 { 37, UINT8, "default_tcp_ttl" },
117 { 38, UINT32, "tcp_keepalive_interval" },
118 { 39, UINT8, "tcp_keepalive_garbage" },
119 { 40, STRING, "nis_domain" },
120 { 41, IPV4 | ARRAY, "nis_servers" },
121 { 42, IPV4 | ARRAY, "ntp_servers" },
122 { 43, STRING, "vendor_encapsulated_options" },
123 { 44, IPV4 | ARRAY, "netbios_name_servers" },
124 { 45, IPV4, "netbios_dd_server" },
125 { 46, UINT8, "netbios_node_type" },
126 { 47, STRING, "netbios_scope" },
127 { 48, IPV4 | ARRAY, "font_servers" },
128 { 49, IPV4 | ARRAY, "x_display_manager" },
129 { 50, IPV4, "dhcp_requested_address" },
130 { 51, UINT32 | REQUEST, "dhcp_lease_time" },
131 { 52, UINT8, "dhcp_option_overload" },
132 { 53, UINT8, "dhcp_message_type" },
133 { 54, IPV4, "dhcp_server_identifier" },
134 { 55, UINT8 | ARRAY, "dhcp_parameter_request_list" },
135 { 56, STRING, "dhcp_message" },
136 { 57, UINT16, "dhcp_max_message_size" },
137 { 58, UINT32 | REQUEST, "dhcp_renewal_time" },
138 { 59, UINT32 | REQUEST, "dhcp_rebinding_time" },
139 { 64, STRING, "nisplus_domain" },
140 { 65, IPV4 | ARRAY, "nisplus_servers" },
141 { 66, STRING, "tftp_server_name" },
142 { 67, STRING, "bootfile_name" },
143 { 68, IPV4 | ARRAY, "mobile_ip_home_agent" },
144 { 69, IPV4 | ARRAY, "smtp_server" },
145 { 70, IPV4 | ARRAY, "pop_server" },
146 { 71, IPV4 | ARRAY, "nntp_server" },
147 { 72, IPV4 | ARRAY, "www_server" },
148 { 73, IPV4 | ARRAY, "finger_server" },
149 { 74, IPV4 | ARRAY, "irc_server" },
150 { 75, IPV4 | ARRAY, "streettalk_server" },
151 { 76, IPV4 | ARRAY, "streettalk_directory_assistance_server" },
152 { 77, STRING, "user_class" },
153 { 81, STRING | RFC3397, "fqdn_name" },
154 { 85, IPV4 | ARRAY, "nds_servers" },
155 { 86, STRING, "nds_tree_name" },
156 { 87, STRING, "nds_context" },
157 { 88, STRING | RFC3397, "bcms_controller_names" },
158 { 89, IPV4 | ARRAY, "bcms_controller_address" },
159 { 91, UINT32, "client_last_transaction_time" },
160 { 92, IPV4 | ARRAY, "associated_ip" },
161 { 98, STRING, "uap_servers" },
162 { 112, IPV4 | ARRAY, "netinfo_server_address" },
163 { 113, STRING, "netinfo_server_tag" },
164 { 114, STRING, "default_url" },
165 { 118, IPV4, "subnet_selection" },
166 { 119, STRING | RFC3397, "domain_search" },
167 { 120, STRING | RFC3361, "sip_server" },
168 { 212, RFC5969, "sixrd" },
169 { 0, 0, NULL }
170 };
171
172 static const char *if_params[] = {
173 "interface",
174 "reason",
175 "pid",
176 "ifmetric",
177 "ifwireless",
178 "ifflags",
179 "profile",
180 "interface_order",
181 NULL
182 };
183
184 static const char *dhcp_params[] = {
185 "ip_address",
186 "subnet_cidr",
187 "network_number",
188 "ssid",
189 "filename",
190 "server_name",
191 NULL
192 };
193
194 void
print_options(void)195 print_options(void)
196 {
197 const struct dhcp_opt *opt;
198 const char **p;
199
200 for (p = if_params; *p; p++)
201 printf(" - %s\n", *p);
202
203 for (p = dhcp_params; *p; p++)
204 printf(" %s\n", *p);
205
206 for (opt = dhcp_opts; opt->option; opt++)
207 if (opt->var)
208 printf("%03d %s\n", opt->option, opt->var);
209 }
210
make_option_mask(uint8_t * mask,const char * opts,int add)211 int make_option_mask(uint8_t *mask, const char *opts, int add)
212 {
213 char *token, *o, *p, *t;
214 const struct dhcp_opt *opt;
215 int match, n;
216
217 o = p = xstrdup(opts);
218 while ((token = strsep(&p, ", "))) {
219 if (*token == '\0')
220 continue;
221 for (opt = dhcp_opts; opt->option; opt++) {
222 if (!opt->var)
223 continue;
224 match = 0;
225 if (strcmp(opt->var, token) == 0)
226 match = 1;
227 else {
228 errno = 0;
229 n = strtol(token, &t, 0);
230 if (errno == 0 && !*t)
231 if (opt->option == n)
232 match = 1;
233 }
234 if (match) {
235 if (add == 2 && !(opt->type & IPV4)) {
236 free(o);
237 errno = EINVAL;
238 return -1;
239 }
240 if (add == 1 || add == 2)
241 add_option_mask(mask,
242 opt->option);
243 else
244 del_option_mask(mask,
245 opt->option);
246 break;
247 }
248 }
249 if (!opt->option) {
250 free(o);
251 errno = ENOENT;
252 return -1;
253 }
254 }
255 free(o);
256 return 0;
257 }
258
259 static int
valid_length(uint8_t option,int dl,int * type)260 valid_length(uint8_t option, int dl, int *type)
261 {
262 const struct dhcp_opt *opt;
263 ssize_t sz;
264
265 if (dl == 0)
266 return -1;
267
268 for (opt = dhcp_opts; opt->option; opt++) {
269 if (opt->option != option)
270 continue;
271
272 if (type)
273 *type = opt->type;
274
275 if (opt->type == 0 ||
276 opt->type & (STRING | RFC3442 | RFC5969))
277 return 0;
278
279 sz = 0;
280 if (opt->type & (UINT32 | IPV4))
281 sz = sizeof(uint32_t);
282 if (opt->type & UINT16)
283 sz = sizeof(uint16_t);
284 if (opt->type & UINT8)
285 sz = sizeof(uint8_t);
286 if (opt->type & (IPV4 | ARRAY))
287 return dl % sz;
288 return (dl == sz ? 0 : -1);
289 }
290
291 /* unknown option, so let it pass */
292 return 0;
293 }
294
295 #ifdef DEBUG_MEMORY
296 static void
free_option_buffer(void)297 free_option_buffer(void)
298 {
299 free(opt_buffer);
300 }
301 #endif
302
303 #define get_option_raw(dhcp, opt) get_option(dhcp, opt, NULL, NULL)
304 static const uint8_t *
get_option(const struct dhcp_message * dhcp,uint8_t opt,int * len,int * type)305 get_option(const struct dhcp_message *dhcp, uint8_t opt, int *len, int *type)
306 {
307 const uint8_t *p = dhcp->options;
308 const uint8_t *e = p + sizeof(dhcp->options);
309 uint8_t l, ol = 0;
310 uint8_t o = 0;
311 uint8_t overl = 0;
312 uint8_t *bp = NULL;
313 const uint8_t *op = NULL;
314 int bl = 0;
315
316 while (p < e) {
317 o = *p++;
318 if (o == opt) {
319 if (op) {
320 if (!opt_buffer) {
321 opt_buffer = xmalloc(sizeof(*dhcp));
322 #ifdef DEBUG_MEMORY
323 atexit(free_option_buffer);
324 #endif
325 }
326 if (!bp)
327 bp = opt_buffer;
328 memcpy(bp, op, ol);
329 bp += ol;
330 }
331 ol = *p;
332 op = p + 1;
333 bl += ol;
334 }
335 switch (o) {
336 case DHO_PAD:
337 continue;
338 case DHO_END:
339 if (overl & 1) {
340 /* bit 1 set means parse boot file */
341 overl &= ~1;
342 p = dhcp->bootfile;
343 e = p + sizeof(dhcp->bootfile);
344 } else if (overl & 2) {
345 /* bit 2 set means parse server name */
346 overl &= ~2;
347 p = dhcp->servername;
348 e = p + sizeof(dhcp->servername);
349 } else
350 goto exit;
351 break;
352 case DHO_OPTIONSOVERLOADED:
353 /* Ensure we only get this option once */
354 if (!overl)
355 overl = p[1];
356 break;
357 }
358 l = *p++;
359 p += l;
360 }
361
362 exit:
363 if (valid_length(opt, bl, type) == -1) {
364 errno = EINVAL;
365 return NULL;
366 }
367 if (len)
368 *len = bl;
369 if (bp) {
370 memcpy(bp, op, ol);
371 return (const uint8_t *)opt_buffer;
372 }
373 if (op)
374 return op;
375 errno = ENOENT;
376 return NULL;
377 }
378
379 int
get_option_addr(struct in_addr * a,const struct dhcp_message * dhcp,uint8_t option)380 get_option_addr(struct in_addr *a, const struct dhcp_message *dhcp,
381 uint8_t option)
382 {
383 const uint8_t *p = get_option_raw(dhcp, option);
384
385 if (!p)
386 return -1;
387 memcpy(&a->s_addr, p, sizeof(a->s_addr));
388 return 0;
389 }
390
391 int
get_option_uint32(uint32_t * i,const struct dhcp_message * dhcp,uint8_t option)392 get_option_uint32(uint32_t *i, const struct dhcp_message *dhcp, uint8_t option)
393 {
394 const uint8_t *p = get_option_raw(dhcp, option);
395 uint32_t d;
396
397 if (!p)
398 return -1;
399 memcpy(&d, p, sizeof(d));
400 *i = ntohl(d);
401 return 0;
402 }
403
404 int
get_option_uint16(uint16_t * i,const struct dhcp_message * dhcp,uint8_t option)405 get_option_uint16(uint16_t *i, const struct dhcp_message *dhcp, uint8_t option)
406 {
407 const uint8_t *p = get_option_raw(dhcp, option);
408 uint16_t d;
409
410 if (!p)
411 return -1;
412 memcpy(&d, p, sizeof(d));
413 *i = ntohs(d);
414 return 0;
415 }
416
417 int
get_option_uint8(uint8_t * i,const struct dhcp_message * dhcp,uint8_t option)418 get_option_uint8(uint8_t *i, const struct dhcp_message *dhcp, uint8_t option)
419 {
420 const uint8_t *p = get_option_raw(dhcp, option);
421
422 if (!p)
423 return -1;
424 if (i)
425 *i = *(p);
426 return 0;
427 }
428
429 /* Decode an RFC3397 DNS search order option into a space
430 * separated string. Returns length of string (including
431 * terminating zero) or zero on error. out may be NULL
432 * to just determine output length. */
433 ssize_t
decode_rfc3397(char * out,ssize_t len,int pl,const uint8_t * p)434 decode_rfc3397(char *out, ssize_t len, int pl, const uint8_t *p)
435 {
436 const uint8_t *r, *q = p;
437 int count = 0, l, hops;
438 uint8_t ltype;
439
440 while (q - p < pl) {
441 r = NULL;
442 hops = 0;
443 /* We check we are inside our length again incase
444 * the data is NOT terminated correctly. */
445 while ((l = *q++) && q - p < pl) {
446 ltype = l & 0xc0;
447 if (ltype == 0x80 || ltype == 0x40)
448 return 0;
449 else if (ltype == 0xc0) { /* pointer */
450 l = (l & 0x3f) << 8;
451 l |= *q++;
452 /* save source of first jump. */
453 if (!r)
454 r = q;
455 hops++;
456 if (hops > 255)
457 return 0;
458 q = p + l;
459 if (q - p >= pl)
460 return 0;
461 } else {
462 /* straightforward name segment, add with '.' */
463 count += l + 1;
464 if (out) {
465 if ((ssize_t)l + 1 > len) {
466 errno = ENOBUFS;
467 return -1;
468 }
469 memcpy(out, q, l);
470 out += l;
471 *out++ = '.';
472 len -= l;
473 len--;
474 }
475 q += l;
476 }
477 }
478 /* change last dot to space */
479 if (out)
480 *(out - 1) = ' ';
481 if (r)
482 q = r;
483 }
484
485 /* change last space to zero terminator */
486 if (out)
487 *(out - 1) = 0;
488
489 return count;
490 }
491
492 static ssize_t
decode_rfc3442(char * out,ssize_t len,int pl,const uint8_t * p)493 decode_rfc3442(char *out, ssize_t len, int pl, const uint8_t *p)
494 {
495 const uint8_t *e;
496 ssize_t b, bytes = 0, ocets;
497 uint8_t cidr;
498 struct in_addr addr;
499 char *o = out;
500
501 /* Minimum is 5 -first is CIDR and a router length of 4 */
502 if (pl < 5) {
503 errno = EINVAL;
504 return -1;
505 }
506
507 e = p + pl;
508 while (p < e) {
509 cidr = *p++;
510 if (cidr > 32) {
511 errno = EINVAL;
512 return -1;
513 }
514 ocets = (cidr + 7) / 8;
515 if (!out) {
516 p += 4 + ocets;
517 bytes += ((4 * 4) * 2) + 4;
518 continue;
519 }
520 if ((((4 * 4) * 2) + 4) > len) {
521 errno = ENOBUFS;
522 return -1;
523 }
524 if (o != out) {
525 *o++ = ' ';
526 len--;
527 }
528 /* If we have ocets then we have a destination and netmask */
529 if (ocets > 0) {
530 addr.s_addr = 0;
531 memcpy(&addr.s_addr, p, ocets);
532 b = snprintf(o, len, "%s/%d", inet_ntoa(addr), cidr);
533 p += ocets;
534 } else
535 b = snprintf(o, len, "0.0.0.0/0");
536 o += b;
537 len -= b;
538
539 /* Finally, snag the router */
540 memcpy(&addr.s_addr, p, 4);
541 p += 4;
542 b = snprintf(o, len, " %s", inet_ntoa(addr));
543 o += b;
544 len -= b;
545 }
546
547 if (out)
548 return o - out;
549 return bytes;
550 }
551
552 static struct rt *
decode_rfc3442_rt(int dl,const uint8_t * data)553 decode_rfc3442_rt(int dl, const uint8_t *data)
554 {
555 const uint8_t *p = data;
556 const uint8_t *e;
557 uint8_t cidr;
558 size_t ocets;
559 struct rt *routes = NULL;
560 struct rt *rt = NULL;
561
562 /* Minimum is 5 -first is CIDR and a router length of 4 */
563 if (dl < 5)
564 return NULL;
565
566 e = p + dl;
567 while (p < e) {
568 cidr = *p++;
569 if (cidr > 32) {
570 free_routes(routes);
571 errno = EINVAL;
572 return NULL;
573 }
574
575 if (rt) {
576 rt->next = xzalloc(sizeof(*rt));
577 rt = rt->next;
578 } else {
579 routes = rt = xzalloc(sizeof(*routes));
580 }
581 rt->next = NULL;
582
583 ocets = (cidr + 7) / 8;
584 /* If we have ocets then we have a destination and netmask */
585 if (ocets > 0) {
586 memcpy(&rt->dest.s_addr, p, ocets);
587 p += ocets;
588 rt->net.s_addr = htonl(~0U << (32 - cidr));
589 }
590
591 /* Finally, snag the router */
592 memcpy(&rt->gate.s_addr, p, 4);
593 p += 4;
594 }
595 return routes;
596 }
597
598 static char *
decode_rfc3361(int dl,const uint8_t * data)599 decode_rfc3361(int dl, const uint8_t *data)
600 {
601 uint8_t enc;
602 unsigned int l;
603 char *sip = NULL;
604 struct in_addr addr;
605 char *p;
606
607 if (dl < 2) {
608 errno = EINVAL;
609 return 0;
610 }
611
612 enc = *data++;
613 dl--;
614 switch (enc) {
615 case 0:
616 if ((l = decode_rfc3397(NULL, 0, dl, data)) > 0) {
617 sip = xmalloc(l);
618 decode_rfc3397(sip, l, dl, data);
619 }
620 break;
621 case 1:
622 if (dl == 0 || dl % 4 != 0) {
623 errno = EINVAL;
624 break;
625 }
626 addr.s_addr = INADDR_BROADCAST;
627 l = ((dl / sizeof(addr.s_addr)) * ((4 * 4) + 1)) + 1;
628 sip = p = xmalloc(l);
629 while (dl != 0) {
630 memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
631 data += sizeof(addr.s_addr);
632 p += snprintf(p, l - (p - sip), "%s ", inet_ntoa(addr));
633 dl -= sizeof(addr.s_addr);
634 }
635 *--p = '\0';
636 break;
637 default:
638 errno = EINVAL;
639 return 0;
640 }
641
642 return sip;
643 }
644
645 /* Decode an RFC5969 6rd order option into a space
646 * separated string. Returns length of string (including
647 * terminating zero) or zero on error. */
648 static ssize_t
decode_rfc5969(char * out,ssize_t len,int pl,const uint8_t * p)649 decode_rfc5969(char *out, ssize_t len, int pl, const uint8_t *p)
650 {
651 uint8_t ipv4masklen, ipv6prefixlen;
652 uint8_t ipv6prefix[16];
653 uint8_t br[4];
654 int i;
655 ssize_t b, bytes = 0;
656
657 if (pl < 22) {
658 errno = EINVAL;
659 return 0;
660 }
661
662 ipv4masklen = *p++;
663 pl--;
664 ipv6prefixlen = *p++;
665 pl--;
666
667 for (i = 0; i < 16; i++) {
668 ipv6prefix[i] = *p++;
669 pl--;
670 }
671 if (out) {
672 b= snprintf(out, len,
673 "%d %d "
674 "%02x%02x:%02x%02x:"
675 "%02x%02x:%02x%02x:"
676 "%02x%02x:%02x%02x:"
677 "%02x%02x:%02x%02x",
678 ipv4masklen, ipv6prefixlen,
679 ipv6prefix[0], ipv6prefix[1], ipv6prefix[2], ipv6prefix[3],
680 ipv6prefix[4], ipv6prefix[5], ipv6prefix[6], ipv6prefix[7],
681 ipv6prefix[8], ipv6prefix[9], ipv6prefix[10],ipv6prefix[11],
682 ipv6prefix[12],ipv6prefix[13],ipv6prefix[14], ipv6prefix[15]
683 );
684
685 len -= b;
686 out += b;
687 bytes += b;
688 } else {
689 bytes += 16 * 2 + 8 + 2 + 1 + 2;
690 }
691
692 while (pl >= 4) {
693 br[0] = *p++;
694 br[1] = *p++;
695 br[2] = *p++;
696 br[3] = *p++;
697 pl -= 4;
698
699 if (out) {
700 b= snprintf(out, len, " %d.%d.%d.%d",
701 br[0], br[1], br[2], br[3]);
702 len -= b;
703 out += b;
704 bytes += b;
705 } else {
706 bytes += (4 * 4);
707 }
708 }
709
710 return bytes;
711 }
712
713 char *
get_option_string(const struct dhcp_message * dhcp,uint8_t option)714 get_option_string(const struct dhcp_message *dhcp, uint8_t option)
715 {
716 int type = 0;
717 int len;
718 const uint8_t *p;
719 char *s;
720
721 p = get_option(dhcp, option, &len, &type);
722 if (!p || *p == '\0')
723 return NULL;
724
725 if (type & RFC3397) {
726 type = decode_rfc3397(NULL, 0, len, p);
727 if (!type) {
728 errno = EINVAL;
729 return NULL;
730 }
731 s = xmalloc(sizeof(char) * type);
732 decode_rfc3397(s, type, len, p);
733 return s;
734 }
735
736 if (type & RFC3361)
737 return decode_rfc3361(len, p);
738
739 s = xmalloc(sizeof(char) * (len + 1));
740 memcpy(s, p, len);
741 s[len] = '\0';
742 return s;
743 }
744
745 /* This calculates the netmask that we should use for static routes.
746 * This IS different from the calculation used to calculate the netmask
747 * for an interface address. */
748 static uint32_t
route_netmask(uint32_t ip_in)749 route_netmask(uint32_t ip_in)
750 {
751 /* used to be unsigned long - check if error */
752 uint32_t p = ntohl(ip_in);
753 uint32_t t;
754
755 if (IN_CLASSA(p))
756 t = ~IN_CLASSA_NET;
757 else {
758 if (IN_CLASSB(p))
759 t = ~IN_CLASSB_NET;
760 else {
761 if (IN_CLASSC(p))
762 t = ~IN_CLASSC_NET;
763 else
764 t = 0;
765 }
766 }
767
768 while (t & p)
769 t >>= 1;
770
771 return (htonl(~t));
772 }
773
774 /* We need to obey routing options.
775 * If we have a CSR then we only use that.
776 * Otherwise we add static routes and then routers. */
777 struct rt *
get_option_routes(const struct dhcp_message * dhcp,const char * ifname,unsigned long long * opts)778 get_option_routes(const struct dhcp_message *dhcp,
779 const char *ifname, unsigned long long *opts)
780 {
781 const uint8_t *p;
782 const uint8_t *e;
783 struct rt *routes = NULL;
784 struct rt *route = NULL;
785 int len;
786
787 /* If we have CSR's then we MUST use these only */
788 p = get_option(dhcp, DHO_CSR, &len, NULL);
789 /* Check for crappy MS option */
790 if (!p)
791 p = get_option(dhcp, DHO_MSCSR, &len, NULL);
792 if (p) {
793 routes = decode_rfc3442_rt(len, p);
794 if (routes) {
795 if (!(*opts & DHCPCD_CSR_WARNED)) {
796 syslog(LOG_DEBUG,
797 "%s: using Classless Static Routes",
798 ifname);
799 *opts |= DHCPCD_CSR_WARNED;
800 }
801 return routes;
802 }
803 }
804
805 /* OK, get our static routes first. */
806 p = get_option(dhcp, DHO_STATICROUTE, &len, NULL);
807 if (p) {
808 e = p + len;
809 while (p < e) {
810 if (route) {
811 route->next = xmalloc(sizeof(*route));
812 route = route->next;
813 } else
814 routes = route = xmalloc(sizeof(*routes));
815 route->next = NULL;
816 memcpy(&route->dest.s_addr, p, 4);
817 p += 4;
818 memcpy(&route->gate.s_addr, p, 4);
819 p += 4;
820 route->net.s_addr = route_netmask(route->dest.s_addr);
821 }
822 }
823
824 /* Now grab our routers */
825 p = get_option(dhcp, DHO_ROUTER, &len, NULL);
826 if (p) {
827 e = p + len;
828 while (p < e) {
829 if (route) {
830 route->next = xzalloc(sizeof(*route));
831 route = route->next;
832 } else
833 routes = route = xzalloc(sizeof(*route));
834 memcpy(&route->gate.s_addr, p, 4);
835 p += 4;
836 }
837 }
838
839 return routes;
840 }
841
842 static size_t
encode_rfc1035(const char * src,uint8_t * dst)843 encode_rfc1035(const char *src, uint8_t *dst)
844 {
845 uint8_t *p = dst;
846 uint8_t *lp = p++;
847
848 if (*src == '\0')
849 return 0;
850 for (; *src; src++) {
851 if (*src == '\0')
852 break;
853 if (*src == '.') {
854 /* Skip the trailing . */
855 if (src[1] == '\0')
856 break;
857 *lp = p - lp - 1;
858 if (*lp == '\0')
859 return p - dst;
860 lp = p++;
861 } else
862 *p++ = (uint8_t)*src;
863 }
864 *lp = p - lp - 1;
865 *p++ = '\0';
866 return p - dst;
867 }
868
869 #define PUTADDR(_type, _val) \
870 { \
871 *p++ = _type; \
872 *p++ = 4; \
873 memcpy(p, &_val.s_addr, 4); \
874 p += 4; \
875 }
876
877 int
dhcp_message_add_addr(struct dhcp_message * dhcp,uint8_t type,struct in_addr addr)878 dhcp_message_add_addr(struct dhcp_message *dhcp,
879 uint8_t type, struct in_addr addr)
880 {
881 uint8_t *p;
882 size_t len;
883
884 p = dhcp->options;
885 while (*p != DHO_END) {
886 p++;
887 p += *p + 1;
888 }
889
890 len = p - (uint8_t *)dhcp;
891 if (len + 6 > sizeof(*dhcp)) {
892 errno = ENOMEM;
893 return -1;
894 }
895
896 PUTADDR(type, addr);
897 *p = DHO_END;
898 return 0;
899 }
900
901 ssize_t
make_message(struct dhcp_message ** message,const struct interface * iface,uint8_t type)902 make_message(struct dhcp_message **message,
903 const struct interface *iface,
904 uint8_t type)
905 {
906 struct dhcp_message *dhcp;
907 uint8_t *m, *lp, *p;
908 uint8_t *n_params = NULL;
909 time_t up = uptime() - iface->start_uptime;
910 uint32_t ul;
911 uint16_t sz;
912 size_t len;
913 const char *hp;
914 const struct dhcp_opt *opt;
915 const struct if_options *ifo = iface->state->options;
916 const struct dhcp_lease *lease = &iface->state->lease;
917
918 dhcp = xzalloc(sizeof (*dhcp));
919 m = (uint8_t *)dhcp;
920 p = dhcp->options;
921
922 if ((type == DHCP_INFORM || type == DHCP_RELEASE ||
923 (type == DHCP_REQUEST &&
924 iface->net.s_addr == lease->net.s_addr &&
925 (iface->state->new == NULL ||
926 iface->state->new->cookie == htonl(MAGIC_COOKIE)))))
927 {
928 dhcp->ciaddr = iface->addr.s_addr;
929 /* In-case we haven't actually configured the address yet */
930 if (type == DHCP_INFORM && iface->addr.s_addr == 0)
931 dhcp->ciaddr = lease->addr.s_addr;
932 }
933
934 dhcp->op = DHCP_BOOTREQUEST;
935 dhcp->hwtype = iface->family;
936 switch (iface->family) {
937 case ARPHRD_ETHER:
938 case ARPHRD_IEEE802:
939 dhcp->hwlen = iface->hwlen;
940 memcpy(&dhcp->chaddr, &iface->hwaddr, iface->hwlen);
941 break;
942 }
943
944 if (ifo->options & DHCPCD_BROADCAST &&
945 dhcp->ciaddr == 0 &&
946 type != DHCP_DECLINE &&
947 type != DHCP_RELEASE)
948 dhcp->flags = htons(BROADCAST_FLAG);
949
950 if (type != DHCP_DECLINE && type != DHCP_RELEASE) {
951 if (up < 0 || up > (time_t)UINT16_MAX)
952 dhcp->secs = htons((uint16_t)UINT16_MAX);
953 else
954 dhcp->secs = htons(up);
955 }
956 dhcp->xid = iface->state->xid;
957 dhcp->cookie = htonl(MAGIC_COOKIE);
958
959 *p++ = DHO_MESSAGETYPE;
960 *p++ = 1;
961 *p++ = type;
962
963 if (iface->clientid) {
964 *p++ = DHO_CLIENTID;
965 memcpy(p, iface->clientid, iface->clientid[0] + 1);
966 p += iface->clientid[0] + 1;
967 }
968
969 if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) {
970 if (type == DHCP_DECLINE ||
971 (type == DHCP_REQUEST &&
972 lease->addr.s_addr != iface->addr.s_addr))
973 {
974 PUTADDR(DHO_IPADDRESS, lease->addr);
975 if (lease->server.s_addr)
976 PUTADDR(DHO_SERVERID, lease->server);
977 }
978
979 if (type == DHCP_RELEASE) {
980 if (lease->server.s_addr)
981 PUTADDR(DHO_SERVERID, lease->server);
982 }
983 }
984
985 if (type == DHCP_DECLINE) {
986 *p++ = DHO_MESSAGE;
987 len = strlen(DAD);
988 *p++ = len;
989 memcpy(p, DAD, len);
990 p += len;
991 }
992
993 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_REQUEST)
994 PUTADDR(DHO_IPADDRESS, ifo->req_addr);
995
996 if (type == DHCP_DISCOVER ||
997 type == DHCP_INFORM ||
998 type == DHCP_REQUEST)
999 {
1000 *p++ = DHO_MAXMESSAGESIZE;
1001 *p++ = 2;
1002 sz = get_mtu(iface->name);
1003 if (sz < MTU_MIN) {
1004 if (set_mtu(iface->name, MTU_MIN) == 0)
1005 sz = MTU_MIN;
1006 } else if (sz > MTU_MAX) {
1007 /* Even though our MTU could be greater than
1008 * MTU_MAX (1500) dhcpcd does not presently
1009 * handle DHCP packets any bigger. */
1010 sz = MTU_MAX;
1011 }
1012 sz = htons(sz);
1013 memcpy(p, &sz, 2);
1014 p += 2;
1015
1016 if (ifo->userclass[0]) {
1017 *p++ = DHO_USERCLASS;
1018 memcpy(p, ifo->userclass, ifo->userclass[0] + 1);
1019 p += ifo->userclass[0] + 1;
1020 }
1021
1022 if (ifo->vendorclassid[0]) {
1023 *p++ = DHO_VENDORCLASSID;
1024 memcpy(p, ifo->vendorclassid,
1025 ifo->vendorclassid[0] + 1);
1026 p += ifo->vendorclassid[0] + 1;
1027 }
1028
1029
1030 if (type != DHCP_INFORM) {
1031 if (ifo->leasetime != 0) {
1032 *p++ = DHO_LEASETIME;
1033 *p++ = 4;
1034 ul = htonl(ifo->leasetime);
1035 memcpy(p, &ul, 4);
1036 p += 4;
1037 }
1038 }
1039
1040 /* Regardless of RFC2132, we should always send a hostname
1041 * upto the first dot (the short hostname) as otherwise
1042 * confuses some DHCP servers when updating DNS.
1043 * The FQDN option should be used if a FQDN is required. */
1044 if (ifo->options & DHCPCD_HOSTNAME && ifo->hostname[0]) {
1045 *p++ = DHO_HOSTNAME;
1046 hp = strchr(ifo->hostname, '.');
1047 if (hp)
1048 len = hp - ifo->hostname;
1049 else
1050 len = strlen(ifo->hostname);
1051 *p++ = len;
1052 memcpy(p, ifo->hostname, len);
1053 p += len;
1054 }
1055 if (ifo->fqdn != FQDN_DISABLE && ifo->hostname[0]) {
1056 /* IETF DHC-FQDN option (81), RFC4702 */
1057 *p++ = DHO_FQDN;
1058 lp = p;
1059 *p++ = 3;
1060 /*
1061 * Flags: 0000NEOS
1062 * S: 1 => Client requests Server to update
1063 * a RR in DNS as well as PTR
1064 * O: 1 => Server indicates to client that
1065 * DNS has been updated
1066 * E: 1 => Name data is DNS format
1067 * N: 1 => Client requests Server to not
1068 * update DNS
1069 */
1070 *p++ = (ifo->fqdn & 0x09) | 0x04;
1071 *p++ = 0; /* from server for PTR RR */
1072 *p++ = 0; /* from server for A RR if S=1 */
1073 ul = encode_rfc1035(ifo->hostname, p);
1074 *lp += ul;
1075 p += ul;
1076 }
1077
1078 /* vendor is already encoded correctly, so just add it */
1079 if (ifo->vendor[0]) {
1080 *p++ = DHO_VENDOR;
1081 memcpy(p, ifo->vendor, ifo->vendor[0] + 1);
1082 p += ifo->vendor[0] + 1;
1083 }
1084
1085 *p++ = DHO_PARAMETERREQUESTLIST;
1086 n_params = p;
1087 *p++ = 0;
1088 for (opt = dhcp_opts; opt->option; opt++) {
1089 if (!(opt->type & REQUEST ||
1090 has_option_mask(ifo->requestmask, opt->option)))
1091 continue;
1092 if (type == DHCP_INFORM &&
1093 (opt->option == DHO_RENEWALTIME ||
1094 opt->option == DHO_REBINDTIME))
1095 continue;
1096 *p++ = opt->option;
1097 }
1098 *n_params = p - n_params - 1;
1099 }
1100 *p++ = DHO_END;
1101
1102 #ifdef BOOTP_MESSAGE_LENTH_MIN
1103 /* Some crappy DHCP servers think they have to obey the BOOTP minimum
1104 * message length.
1105 * They are wrong, but we should still cater for them. */
1106 while (p - m < BOOTP_MESSAGE_LENTH_MIN)
1107 *p++ = DHO_PAD;
1108 #endif
1109
1110 *message = dhcp;
1111 return p - m;
1112 }
1113
1114 ssize_t
write_lease(const struct interface * iface,const struct dhcp_message * dhcp)1115 write_lease(const struct interface *iface, const struct dhcp_message *dhcp)
1116 {
1117 int fd;
1118 ssize_t bytes = sizeof(*dhcp);
1119 const uint8_t *p = dhcp->options;
1120 const uint8_t *e = p + sizeof(dhcp->options);
1121 uint8_t l;
1122 uint8_t o = 0;
1123
1124 /* We don't write BOOTP leases */
1125 if (is_bootp(dhcp)) {
1126 unlink(iface->leasefile);
1127 return 0;
1128 }
1129
1130 syslog(LOG_DEBUG, "%s: writing lease `%s'",
1131 iface->name, iface->leasefile);
1132
1133 fd = open(iface->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0444);
1134 #ifdef ANDROID
1135 if (fd == -1 && errno == EACCES) {
1136 /* the lease file might have been created when dhcpcd was running as root */
1137 unlink(iface->leasefile);
1138 fd = open(iface->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0444);
1139 }
1140 #endif
1141 if (fd == -1) {
1142 syslog(LOG_ERR, "%s: open: %m", iface->name);
1143 return -1;
1144 }
1145
1146 /* Only write as much as we need */
1147 while (p < e) {
1148 o = *p;
1149 if (o == DHO_END) {
1150 bytes = p - (const uint8_t *)dhcp;
1151 break;
1152 }
1153 p++;
1154 if (o != DHO_PAD) {
1155 l = *p++;
1156 p += l;
1157 }
1158 }
1159 bytes = write(fd, dhcp, bytes);
1160 close(fd);
1161 return bytes;
1162 }
1163
1164 struct dhcp_message *
read_lease(const struct interface * iface)1165 read_lease(const struct interface *iface)
1166 {
1167 int fd;
1168 struct dhcp_message *dhcp;
1169 ssize_t bytes;
1170
1171 fd = open(iface->leasefile, O_RDONLY);
1172 if (fd == -1) {
1173 if (errno != ENOENT)
1174 syslog(LOG_ERR, "%s: open `%s': %m",
1175 iface->name, iface->leasefile);
1176 return NULL;
1177 }
1178 syslog(LOG_DEBUG, "%s: reading lease `%s'",
1179 iface->name, iface->leasefile);
1180 dhcp = xmalloc(sizeof(*dhcp));
1181 memset(dhcp, 0, sizeof(*dhcp));
1182 bytes = read(fd, dhcp, sizeof(*dhcp));
1183 close(fd);
1184 if (bytes < 0) {
1185 free(dhcp);
1186 dhcp = NULL;
1187 }
1188 return dhcp;
1189 }
1190
1191 static ssize_t
print_string(char * s,ssize_t len,int dl,const uint8_t * data)1192 print_string(char *s, ssize_t len, int dl, const uint8_t *data)
1193 {
1194 uint8_t c;
1195 const uint8_t *e, *p;
1196 ssize_t bytes = 0;
1197 ssize_t r;
1198
1199 e = data + dl;
1200 while (data < e) {
1201 c = *data++;
1202 if (c == '\0') {
1203 /* If rest is all NULL, skip it. */
1204 for (p = data; p < e; p++)
1205 if (*p != '\0')
1206 break;
1207 if (p == e)
1208 break;
1209 }
1210 if (!isascii(c) || !isprint(c)) {
1211 if (s) {
1212 if (len < 5) {
1213 errno = ENOBUFS;
1214 return -1;
1215 }
1216 r = snprintf(s, len, "\\%03o", c);
1217 len -= r;
1218 bytes += r;
1219 s += r;
1220 } else
1221 bytes += 4;
1222 continue;
1223 }
1224 switch (c) {
1225 case '"': /* FALLTHROUGH */
1226 case '\'': /* FALLTHROUGH */
1227 case '$': /* FALLTHROUGH */
1228 case '`': /* FALLTHROUGH */
1229 case '\\': /* FALLTHROUGH */
1230 case '|': /* FALLTHROUGH */
1231 case '&':
1232 if (s) {
1233 if (len < 3) {
1234 errno = ENOBUFS;
1235 return -1;
1236 }
1237 *s++ = '\\';
1238 len--;
1239 }
1240 bytes++;
1241 break;
1242 }
1243 if (s) {
1244 *s++ = c;
1245 len--;
1246 }
1247 bytes++;
1248 }
1249
1250 /* NULL */
1251 if (s)
1252 *s = '\0';
1253 bytes++;
1254 return bytes;
1255 }
1256
1257 static ssize_t
print_option(char * s,ssize_t len,int type,int dl,const uint8_t * data)1258 print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data)
1259 {
1260 const uint8_t *e, *t;
1261 uint16_t u16;
1262 int16_t s16;
1263 uint32_t u32;
1264 int32_t s32;
1265 struct in_addr addr;
1266 ssize_t bytes = 0;
1267 ssize_t l;
1268 char *tmp;
1269
1270 if (type & RFC3397) {
1271 l = decode_rfc3397(NULL, 0, dl, data);
1272 if (l < 1)
1273 return l;
1274 tmp = xmalloc(l);
1275 decode_rfc3397(tmp, l, dl, data);
1276 l = print_string(s, len, l - 1, (uint8_t *)tmp);
1277 free(tmp);
1278 return l;
1279 }
1280
1281 if (type & RFC3361) {
1282 if ((tmp = decode_rfc3361(dl, data)) == NULL)
1283 return -1;
1284 l = strlen(tmp);
1285 l = print_string(s, len, l - 1, (uint8_t *)tmp);
1286 free(tmp);
1287 return l;
1288 }
1289
1290 if (type & RFC3442)
1291 return decode_rfc3442(s, len, dl, data);
1292
1293 if (type & RFC5969)
1294 return decode_rfc5969(s, len, dl, data);
1295
1296 if (type & STRING) {
1297 /* Some DHCP servers return NULL strings */
1298 if (*data == '\0')
1299 return 0;
1300 return print_string(s, len, dl, data);
1301 }
1302
1303 if (!s) {
1304 if (type & UINT8)
1305 l = 3;
1306 else if (type & UINT16) {
1307 l = 5;
1308 dl /= 2;
1309 } else if (type & SINT16) {
1310 l = 6;
1311 dl /= 2;
1312 } else if (type & UINT32) {
1313 l = 10;
1314 dl /= 4;
1315 } else if (type & SINT32) {
1316 l = 11;
1317 dl /= 4;
1318 } else if (type & IPV4) {
1319 l = 16;
1320 dl /= 4;
1321 } else {
1322 errno = EINVAL;
1323 return -1;
1324 }
1325 return (l + 1) * dl;
1326 }
1327
1328 t = data;
1329 e = data + dl;
1330 while (data < e) {
1331 if (data != t) {
1332 *s++ = ' ';
1333 bytes++;
1334 len--;
1335 }
1336 if (type & UINT8) {
1337 l = snprintf(s, len, "%d", *data);
1338 data++;
1339 } else if (type & UINT16) {
1340 memcpy(&u16, data, sizeof(u16));
1341 u16 = ntohs(u16);
1342 l = snprintf(s, len, "%d", u16);
1343 data += sizeof(u16);
1344 } else if (type & SINT16) {
1345 memcpy(&s16, data, sizeof(s16));
1346 s16 = ntohs(s16);
1347 l = snprintf(s, len, "%d", s16);
1348 data += sizeof(s16);
1349 } else if (type & UINT32) {
1350 memcpy(&u32, data, sizeof(u32));
1351 u32 = ntohl(u32);
1352 l = snprintf(s, len, "%d", u32);
1353 data += sizeof(u32);
1354 } else if (type & SINT32) {
1355 memcpy(&s32, data, sizeof(s32));
1356 s32 = ntohl(s32);
1357 l = snprintf(s, len, "%d", s32);
1358 data += sizeof(s32);
1359 } else if (type & IPV4) {
1360 memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
1361 l = snprintf(s, len, "%s", inet_ntoa(addr));
1362 data += sizeof(addr.s_addr);
1363 } else
1364 l = 0;
1365 len -= l;
1366 bytes += l;
1367 s += l;
1368 }
1369
1370 return bytes;
1371 }
1372
1373 ssize_t
configure_env(char ** env,const char * prefix,const struct dhcp_message * dhcp,const struct if_options * ifo)1374 configure_env(char **env, const char *prefix, const struct dhcp_message *dhcp,
1375 const struct if_options *ifo)
1376 {
1377 unsigned int i;
1378 const uint8_t *p;
1379 int pl;
1380 struct in_addr addr;
1381 struct in_addr net;
1382 struct in_addr brd;
1383 char *val, *v;
1384 const struct dhcp_opt *opt;
1385 ssize_t len, e = 0;
1386 char **ep;
1387 char cidr[4];
1388 uint8_t overl = 0;
1389
1390 get_option_uint8(&overl, dhcp, DHO_OPTIONSOVERLOADED);
1391
1392 if (!env) {
1393 for (opt = dhcp_opts; opt->option; opt++) {
1394 if (!opt->var)
1395 continue;
1396 if (has_option_mask(ifo->nomask, opt->option))
1397 continue;
1398 if (get_option_raw(dhcp, opt->option))
1399 e++;
1400 }
1401 if (dhcp->yiaddr || dhcp->ciaddr)
1402 e += 5;
1403 if (*dhcp->bootfile && !(overl & 1))
1404 e++;
1405 if (*dhcp->servername && !(overl & 2))
1406 e++;
1407 return e;
1408 }
1409
1410 ep = env;
1411 if (dhcp->yiaddr || dhcp->ciaddr) {
1412 /* Set some useful variables that we derive from the DHCP
1413 * message but are not necessarily in the options */
1414 addr.s_addr = dhcp->yiaddr ? dhcp->yiaddr : dhcp->ciaddr;
1415 setvar(&ep, prefix, "ip_address", inet_ntoa(addr));
1416 if (get_option_addr(&net, dhcp, DHO_SUBNETMASK) == -1) {
1417 net.s_addr = get_netmask(addr.s_addr);
1418 setvar(&ep, prefix, "subnet_mask", inet_ntoa(net));
1419 }
1420 i = inet_ntocidr(net);
1421 snprintf(cidr, sizeof(cidr), "%d", inet_ntocidr(net));
1422 setvar(&ep, prefix, "subnet_cidr", cidr);
1423 if (get_option_addr(&brd, dhcp, DHO_BROADCAST) == -1) {
1424 brd.s_addr = addr.s_addr | ~net.s_addr;
1425 setvar(&ep, prefix, "broadcast_address", inet_ntoa(brd));
1426 }
1427 addr.s_addr = dhcp->yiaddr & net.s_addr;
1428 setvar(&ep, prefix, "network_number", inet_ntoa(addr));
1429 }
1430
1431 if (*dhcp->bootfile && !(overl & 1))
1432 setvar(&ep, prefix, "filename", (const char *)dhcp->bootfile);
1433 if (*dhcp->servername && !(overl & 2))
1434 setvar(&ep, prefix, "server_name", (const char *)dhcp->servername);
1435
1436 for (opt = dhcp_opts; opt->option; opt++) {
1437 if (!opt->var)
1438 continue;
1439 if (has_option_mask(ifo->nomask, opt->option))
1440 continue;
1441 val = NULL;
1442 p = get_option(dhcp, opt->option, &pl, NULL);
1443 if (!p)
1444 continue;
1445 /* We only want the FQDN name */
1446 if (opt->option == DHO_FQDN) {
1447 p += 3;
1448 pl -= 3;
1449 }
1450 len = print_option(NULL, 0, opt->type, pl, p);
1451 if (len < 0)
1452 return -1;
1453 e = strlen(prefix) + strlen(opt->var) + len + 4;
1454 v = val = *ep++ = xmalloc(e);
1455 v += snprintf(val, e, "%s_%s=", prefix, opt->var);
1456 if (len != 0)
1457 print_option(v, len, opt->type, pl, p);
1458 }
1459
1460 return ep - env;
1461 }
1462
1463 void
get_lease(struct dhcp_lease * lease,const struct dhcp_message * dhcp)1464 get_lease(struct dhcp_lease *lease, const struct dhcp_message *dhcp)
1465 {
1466 struct timeval now;
1467
1468 lease->cookie = dhcp->cookie;
1469 /* BOOTP does not set yiaddr for replies when ciaddr is set. */
1470 if (dhcp->yiaddr)
1471 lease->addr.s_addr = dhcp->yiaddr;
1472 else
1473 lease->addr.s_addr = dhcp->ciaddr;
1474 if (get_option_addr(&lease->net, dhcp, DHO_SUBNETMASK) == -1)
1475 lease->net.s_addr = get_netmask(lease->addr.s_addr);
1476 if (get_option_addr(&lease->brd, dhcp, DHO_BROADCAST) == -1)
1477 lease->brd.s_addr = lease->addr.s_addr | ~lease->net.s_addr;
1478 if (get_option_uint32(&lease->leasetime, dhcp, DHO_LEASETIME) == 0) {
1479 /* Ensure that we can use the lease */
1480 get_monotonic(&now);
1481 if (now.tv_sec + (time_t)lease->leasetime < now.tv_sec)
1482 lease->leasetime = ~0U; /* Infinite lease */
1483 } else
1484 lease->leasetime = ~0U; /* Default to infinite lease */
1485 if (get_option_uint32(&lease->renewaltime, dhcp, DHO_RENEWALTIME) != 0)
1486 lease->renewaltime = 0;
1487 if (get_option_uint32(&lease->rebindtime, dhcp, DHO_REBINDTIME) != 0)
1488 lease->rebindtime = 0;
1489 if (get_option_addr(&lease->server, dhcp, DHO_SERVERID) != 0)
1490 lease->server.s_addr = INADDR_ANY;
1491 }
1492