]> git.ipfire.org Git - thirdparty/dhcpcd.git/blob - dhcp.c
d4ba47c9b6acf638c2f554129e2256d530290733
[thirdparty/dhcpcd.git] / dhcp.c
1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2014 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 <sys/param.h>
29 #include <sys/socket.h>
30 #include <sys/stat.h>
31
32 #ifdef __linux__
33 # include <asm/types.h> /* for systems with broken headers */
34 # include <linux/rtnetlink.h>
35 #endif
36
37 #include <arpa/inet.h>
38 #include <net/if.h>
39 #include <net/route.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
45 #include <netinet/udp.h>
46 #undef __FAVOR_BSD
47
48 #include <ctype.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <inttypes.h>
52 #include <stddef.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <syslog.h>
56 #include <unistd.h>
57
58 #include "config.h"
59 #include "arp.h"
60 #include "common.h"
61 #include "dhcp.h"
62 #include "dhcpcd.h"
63 #include "dhcp-common.h"
64 #include "duid.h"
65 #include "eloop.h"
66 #include "if.h"
67 #include "ipv4.h"
68 #include "ipv4ll.h"
69 #include "script.h"
70
71 #define DAD "Duplicate address detected"
72 #define DHCP_MIN_LEASE 20
73
74 #define IPV4A ADDRIPV4 | ARRAY
75 #define IPV4R ADDRIPV4 | REQUEST
76
77 /* We should define a maximum for the NAK exponential backoff */
78 #define NAKOFF_MAX 60
79
80 /* Wait N nanoseconds between sending a RELEASE and dropping the address.
81 * This gives the kernel enough time to actually send it. */
82 #define RELEASE_DELAY_S 0
83 #define RELEASE_DELAY_NS 10000000
84
85 #ifndef IPDEFTTL
86 #define IPDEFTTL 64 /* RFC1340 */
87 #endif
88
89 struct dhcp_op {
90 uint8_t value;
91 const char *name;
92 };
93
94 static const struct dhcp_op dhcp_ops[] = {
95 { DHCP_DISCOVER, "DISCOVER" },
96 { DHCP_OFFER, "OFFER" },
97 { DHCP_REQUEST, "REQUEST" },
98 { DHCP_DECLINE, "DECLINE" },
99 { DHCP_ACK, "ACK" },
100 { DHCP_NAK, "NAK" },
101 { DHCP_RELEASE, "RELEASE" },
102 { DHCP_INFORM, "INFORM" },
103 { DHCP_FORCERENEW, "DHCP_FORCERENEW"},
104 { 0, NULL }
105 };
106
107 static const char * const dhcp_params[] = {
108 "ip_address",
109 "subnet_cidr",
110 "network_number",
111 "filename",
112 "server_name",
113 NULL
114 };
115
116 struct udp_dhcp_packet
117 {
118 struct ip ip;
119 struct udphdr udp;
120 struct dhcp_message dhcp;
121 };
122
123 static const size_t udp_dhcp_len = sizeof(struct udp_dhcp_packet);
124
125 static int dhcp_open(struct interface *);
126
127 void
128 dhcp_printoptions(const struct dhcpcd_ctx *ctx)
129 {
130 const char * const *p;
131 size_t i;
132 const struct dhcp_opt *opt;
133
134 for (p = dhcp_params; *p; p++)
135 printf(" %s\n", *p);
136
137 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++)
138 printf("%03d %s\n", opt->option, opt->var);
139 }
140
141 #define get_option_raw(ctx, dhcp, opt) get_option(ctx, dhcp, opt, NULL)
142 static const uint8_t *
143 get_option(struct dhcpcd_ctx *ctx,
144 const struct dhcp_message *dhcp, unsigned int opt, size_t *len)
145 {
146 const uint8_t *p = dhcp->options;
147 const uint8_t *e = p + sizeof(dhcp->options);
148 uint8_t l, ol = 0;
149 uint8_t o = 0;
150 uint8_t overl = 0;
151 uint8_t *bp = NULL;
152 const uint8_t *op = NULL;
153 size_t bl = 0;
154
155 while (p < e) {
156 o = *p++;
157 if (o == opt) {
158 if (op) {
159 if (!ctx->opt_buffer) {
160 ctx->opt_buffer =
161 malloc(DHCP_OPTION_LEN +
162 BOOTFILE_LEN + SERVERNAME_LEN);
163 if (ctx->opt_buffer == NULL)
164 return NULL;
165 }
166 if (!bp)
167 bp = ctx->opt_buffer;
168 memcpy(bp, op, ol);
169 bp += ol;
170 }
171 ol = *p;
172 if (p + ol > e) {
173 errno = EINVAL;
174 return NULL;
175 }
176 op = p + 1;
177 bl += ol;
178 }
179 switch (o) {
180 case DHO_PAD:
181 continue;
182 case DHO_END:
183 if (overl & 1) {
184 /* bit 1 set means parse boot file */
185 overl &= ~1;
186 p = dhcp->bootfile;
187 e = p + sizeof(dhcp->bootfile);
188 } else if (overl & 2) {
189 /* bit 2 set means parse server name */
190 overl &= ~2;
191 p = dhcp->servername;
192 e = p + sizeof(dhcp->servername);
193 } else
194 goto exit;
195 break;
196 case DHO_OPTIONSOVERLOADED:
197 /* Ensure we only get this option once */
198 if (!overl)
199 overl = p[1];
200 break;
201 }
202 l = *p++;
203 p += l;
204 }
205
206 exit:
207 if (len)
208 *len = bl;
209 if (bp) {
210 memcpy(bp, op, ol);
211 return (const uint8_t *)ctx->opt_buffer;
212 }
213 if (op)
214 return op;
215 errno = ENOENT;
216 return NULL;
217 }
218
219 int
220 get_option_addr(struct dhcpcd_ctx *ctx,
221 struct in_addr *a, const struct dhcp_message *dhcp,
222 uint8_t option)
223 {
224 const uint8_t *p;
225 size_t len;
226
227 p = get_option(ctx, dhcp, option, &len);
228 if (!p || len < (ssize_t)sizeof(a->s_addr))
229 return -1;
230 memcpy(&a->s_addr, p, sizeof(a->s_addr));
231 return 0;
232 }
233
234 static int
235 get_option_uint32(struct dhcpcd_ctx *ctx,
236 uint32_t *i, const struct dhcp_message *dhcp, uint8_t option)
237 {
238 const uint8_t *p;
239 size_t len;
240 uint32_t d;
241
242 p = get_option(ctx, dhcp, option, &len);
243 if (!p || len < (ssize_t)sizeof(d))
244 return -1;
245 memcpy(&d, p, sizeof(d));
246 if (i)
247 *i = ntohl(d);
248 return 0;
249 }
250
251 static int
252 get_option_uint8(struct dhcpcd_ctx *ctx,
253 uint8_t *i, const struct dhcp_message *dhcp, uint8_t option)
254 {
255 const uint8_t *p;
256 size_t len;
257
258 p = get_option(ctx, dhcp, option, &len);
259 if (!p || len < (ssize_t)sizeof(*p))
260 return -1;
261 if (i)
262 *i = *(p);
263 return 0;
264 }
265
266 ssize_t
267 decode_rfc3442(char *out, size_t len, const uint8_t *p, size_t pl)
268 {
269 const uint8_t *e;
270 size_t bytes = 0, ocets;
271 int b;
272 uint8_t cidr;
273 struct in_addr addr;
274 char *o = out;
275
276 /* Minimum is 5 -first is CIDR and a router length of 4 */
277 if (pl < 5) {
278 errno = EINVAL;
279 return -1;
280 }
281
282 e = p + pl;
283 while (p < e) {
284 cidr = *p++;
285 if (cidr > 32) {
286 errno = EINVAL;
287 return -1;
288 }
289 ocets = (cidr + 7) / 8;
290 if (!out) {
291 p += 4 + ocets;
292 bytes += ((4 * 4) * 2) + 4;
293 continue;
294 }
295 if ((((4 * 4) * 2) + 4) > len) {
296 errno = ENOBUFS;
297 return -1;
298 }
299 if (o != out) {
300 *o++ = ' ';
301 len--;
302 }
303 /* If we have ocets then we have a destination and netmask */
304 if (ocets > 0) {
305 addr.s_addr = 0;
306 memcpy(&addr.s_addr, p, ocets);
307 b = snprintf(o, len, "%s/%d", inet_ntoa(addr), cidr);
308 p += ocets;
309 } else
310 b = snprintf(o, len, "0.0.0.0/0");
311 o += b;
312 len -= (size_t)b;
313
314 /* Finally, snag the router */
315 memcpy(&addr.s_addr, p, 4);
316 p += 4;
317 b = snprintf(o, len, " %s", inet_ntoa(addr));
318 o += b;
319 len -= (size_t)b;
320 }
321
322 if (out)
323 return o - out;
324 return (ssize_t)bytes;
325 }
326
327 static struct rt_head *
328 decode_rfc3442_rt(const uint8_t *data, size_t dl)
329 {
330 const uint8_t *p = data;
331 const uint8_t *e;
332 uint8_t cidr;
333 size_t ocets;
334 struct rt_head *routes;
335 struct rt *rt = NULL;
336
337 /* Minimum is 5 -first is CIDR and a router length of 4 */
338 if (dl < 5)
339 return NULL;
340
341 routes = malloc(sizeof(*routes));
342 TAILQ_INIT(routes);
343 e = p + dl;
344 while (p < e) {
345 cidr = *p++;
346 if (cidr > 32) {
347 ipv4_freeroutes(routes);
348 errno = EINVAL;
349 return NULL;
350 }
351
352 rt = calloc(1, sizeof(*rt));
353 if (rt == NULL) {
354 syslog(LOG_ERR, "%s: %m", __func__);
355 ipv4_freeroutes(routes);
356 return NULL;
357 }
358 TAILQ_INSERT_TAIL(routes, rt, next);
359
360 ocets = (cidr + 7) / 8;
361 /* If we have ocets then we have a destination and netmask */
362 if (ocets > 0) {
363 memcpy(&rt->dest.s_addr, p, ocets);
364 p += ocets;
365 rt->net.s_addr = htonl(~0U << (32 - cidr));
366 }
367
368 /* Finally, snag the router */
369 memcpy(&rt->gate.s_addr, p, 4);
370 p += 4;
371 }
372 return routes;
373 }
374
375 char *
376 decode_rfc3361(const uint8_t *data, size_t dl)
377 {
378 uint8_t enc;
379 size_t l;
380 ssize_t r;
381 char *sip = NULL;
382 struct in_addr addr;
383 char *p;
384
385 if (dl < 2) {
386 errno = EINVAL;
387 return 0;
388 }
389
390 enc = *data++;
391 dl--;
392 switch (enc) {
393 case 0:
394 if ((r = decode_rfc3397(NULL, 0, data, dl)) > 0) {
395 l = (size_t)r;
396 sip = malloc(l);
397 if (sip == NULL)
398 return 0;
399 decode_rfc3397(sip, l, data, dl);
400 }
401 break;
402 case 1:
403 if (dl == 0 || dl % 4 != 0) {
404 errno = EINVAL;
405 break;
406 }
407 addr.s_addr = INADDR_BROADCAST;
408 l = ((dl / sizeof(addr.s_addr)) * ((4 * 4) + 1)) + 1;
409 sip = p = malloc(l);
410 if (sip == NULL)
411 return 0;
412 while (dl != 0) {
413 memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
414 data += sizeof(addr.s_addr);
415 p += snprintf(p, l - (size_t)(p - sip),
416 "%s ", inet_ntoa(addr));
417 dl -= sizeof(addr.s_addr);
418 }
419 *--p = '\0';
420 break;
421 default:
422 errno = EINVAL;
423 return 0;
424 }
425
426 return sip;
427 }
428
429 /* Decode an RFC5969 6rd order option into a space
430 * separated string. Returns length of string (including
431 * terminating zero) or zero on error. */
432 ssize_t
433 decode_rfc5969(char *out, size_t len, const uint8_t *p, size_t pl)
434 {
435 uint8_t ipv4masklen, ipv6prefixlen;
436 uint8_t ipv6prefix[16];
437 uint8_t br[4];
438 int i;
439 ssize_t b, bytes = 0;
440
441 if (pl < 22) {
442 errno = EINVAL;
443 return 0;
444 }
445
446 ipv4masklen = *p++;
447 pl--;
448 ipv6prefixlen = *p++;
449 pl--;
450
451 for (i = 0; i < 16; i++) {
452 ipv6prefix[i] = *p++;
453 pl--;
454 }
455 if (out) {
456 b= snprintf(out, len,
457 "%d %d "
458 "%02x%02x:%02x%02x:"
459 "%02x%02x:%02x%02x:"
460 "%02x%02x:%02x%02x:"
461 "%02x%02x:%02x%02x",
462 ipv4masklen, ipv6prefixlen,
463 ipv6prefix[0], ipv6prefix[1], ipv6prefix[2], ipv6prefix[3],
464 ipv6prefix[4], ipv6prefix[5], ipv6prefix[6], ipv6prefix[7],
465 ipv6prefix[8], ipv6prefix[9], ipv6prefix[10],ipv6prefix[11],
466 ipv6prefix[12],ipv6prefix[13],ipv6prefix[14], ipv6prefix[15]
467 );
468
469 len -= (size_t)b;
470 out += b;
471 bytes += b;
472 } else {
473 bytes += 16 * 2 + 8 + 2 + 1 + 2;
474 }
475
476 while (pl >= 4) {
477 br[0] = *p++;
478 br[1] = *p++;
479 br[2] = *p++;
480 br[3] = *p++;
481 pl -= 4;
482
483 if (out) {
484 b= snprintf(out, len, " %d.%d.%d.%d",
485 br[0], br[1], br[2], br[3]);
486 len -= (size_t)b;
487 out += b;
488 bytes += b;
489 } else {
490 bytes += (4 * 4);
491 }
492 }
493
494 return bytes;
495 }
496
497 static char *
498 get_option_string(struct dhcpcd_ctx *ctx,
499 const struct dhcp_message *dhcp, uint8_t option)
500 {
501 size_t len;
502 const uint8_t *p;
503 char *s;
504
505 p = get_option(ctx, dhcp, option, &len);
506 if (!p || len == 0 || *p == '\0')
507 return NULL;
508
509 s = malloc(sizeof(char) * (len + 1));
510 if (s) {
511 memcpy(s, p, len);
512 s[len] = '\0';
513 }
514 return s;
515 }
516
517 /* This calculates the netmask that we should use for static routes.
518 * This IS different from the calculation used to calculate the netmask
519 * for an interface address. */
520 static uint32_t
521 route_netmask(uint32_t ip_in)
522 {
523 /* used to be unsigned long - check if error */
524 uint32_t p = ntohl(ip_in);
525 uint32_t t;
526
527 if (IN_CLASSA(p))
528 t = ~IN_CLASSA_NET;
529 else {
530 if (IN_CLASSB(p))
531 t = ~IN_CLASSB_NET;
532 else {
533 if (IN_CLASSC(p))
534 t = ~IN_CLASSC_NET;
535 else
536 t = 0;
537 }
538 }
539
540 while (t & p)
541 t >>= 1;
542
543 return (htonl(~t));
544 }
545
546 /* We need to obey routing options.
547 * If we have a CSR then we only use that.
548 * Otherwise we add static routes and then routers. */
549 struct rt_head *
550 get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp)
551 {
552 struct if_options *ifo = ifp->options;
553 const uint8_t *p;
554 const uint8_t *e;
555 struct rt_head *routes = NULL;
556 struct rt *route = NULL;
557 size_t len;
558 const char *csr = "";
559
560 /* If we have CSR's then we MUST use these only */
561 if (!has_option_mask(ifo->nomask, DHO_CSR))
562 p = get_option(ifp->ctx, dhcp, DHO_CSR, &len);
563 else
564 p = NULL;
565 /* Check for crappy MS option */
566 if (!p && !has_option_mask(ifo->nomask, DHO_MSCSR)) {
567 p = get_option(ifp->ctx, dhcp, DHO_MSCSR, &len);
568 if (p)
569 csr = "MS ";
570 }
571 if (p) {
572 routes = decode_rfc3442_rt(p, len);
573 if (routes) {
574 if (!(ifo->options & DHCPCD_CSR_WARNED)) {
575 syslog(LOG_DEBUG,
576 "%s: using %sClassless Static Routes",
577 ifp->name, csr);
578 ifo->options |= DHCPCD_CSR_WARNED;
579 }
580 return routes;
581 }
582 }
583
584 /* OK, get our static routes first. */
585 routes = malloc(sizeof(*routes));
586 if (routes == NULL) {
587 syslog(LOG_ERR, "%s: %m", __func__);
588 return NULL;
589 }
590 TAILQ_INIT(routes);
591 if (!has_option_mask(ifo->nomask, DHO_STATICROUTE))
592 p = get_option(ifp->ctx, dhcp, DHO_STATICROUTE, &len);
593 else
594 p = NULL;
595 if (p) {
596 e = p + len;
597 while (p < e) {
598 route = calloc(1, sizeof(*route));
599 if (route == NULL) {
600 syslog(LOG_ERR, "%s: %m", __func__);
601 ipv4_freeroutes(routes);
602 return NULL;
603 }
604 memcpy(&route->dest.s_addr, p, 4);
605 p += 4;
606 memcpy(&route->gate.s_addr, p, 4);
607 p += 4;
608 route->net.s_addr = route_netmask(route->dest.s_addr);
609 TAILQ_INSERT_TAIL(routes, route, next);
610 }
611 }
612
613 /* Now grab our routers */
614 if (!has_option_mask(ifo->nomask, DHO_ROUTER))
615 p = get_option(ifp->ctx, dhcp, DHO_ROUTER, &len);
616 else
617 p = NULL;
618 if (p) {
619 e = p + len;
620 while (p < e) {
621 route = calloc(1, sizeof(*route));
622 if (route == NULL) {
623 syslog(LOG_ERR, "%s: %m", __func__);
624 ipv4_freeroutes(routes);
625 return NULL;
626 }
627 memcpy(&route->gate.s_addr, p, 4);
628 p += 4;
629 TAILQ_INSERT_TAIL(routes, route, next);
630 }
631 }
632
633 return routes;
634 }
635
636 #define PUTADDR(_type, _val) \
637 { \
638 *p++ = _type; \
639 *p++ = 4; \
640 memcpy(p, &_val.s_addr, 4); \
641 p += 4; \
642 }
643
644 int
645 dhcp_message_add_addr(struct dhcp_message *dhcp,
646 uint8_t type, struct in_addr addr)
647 {
648 uint8_t *p;
649 size_t len;
650
651 p = dhcp->options;
652 while (*p != DHO_END) {
653 p++;
654 p += *p + 1;
655 }
656
657 len = (size_t)(p - (uint8_t *)dhcp);
658 if (len + 6 > sizeof(*dhcp)) {
659 errno = ENOMEM;
660 return -1;
661 }
662
663 PUTADDR(type, addr);
664 *p = DHO_END;
665 return 0;
666 }
667
668 ssize_t
669 make_message(struct dhcp_message **message,
670 const struct interface *iface,
671 uint8_t type)
672 {
673 struct dhcp_message *dhcp;
674 uint8_t *m, *lp, *p, *auth;
675 uint8_t *n_params = NULL;
676 uint32_t ul;
677 uint16_t sz;
678 size_t len, i, auth_len;
679 const struct dhcp_opt *opt;
680 struct if_options *ifo = iface->options;
681 const struct dhcp_state *state = D_CSTATE(iface);
682 const struct dhcp_lease *lease = &state->lease;
683 time_t up = uptime() - state->start_uptime;
684 char hbuf[HOSTNAME_MAX_LEN + 1];
685 const char *hostname;
686 const struct vivco *vivco;
687
688 dhcp = calloc(1, sizeof (*dhcp));
689 if (dhcp == NULL)
690 return -1;
691 m = (uint8_t *)dhcp;
692 p = dhcp->options;
693
694 if ((type == DHCP_INFORM || type == DHCP_RELEASE ||
695 (type == DHCP_REQUEST &&
696 state->net.s_addr == lease->net.s_addr &&
697 (state->new == NULL ||
698 state->new->cookie == htonl(MAGIC_COOKIE)))))
699 {
700 dhcp->ciaddr = state->addr.s_addr;
701 /* In-case we haven't actually configured the address yet */
702 if (type == DHCP_INFORM && state->addr.s_addr == 0)
703 dhcp->ciaddr = lease->addr.s_addr;
704 }
705
706 dhcp->op = DHCP_BOOTREQUEST;
707 dhcp->hwtype = (uint8_t)iface->family;
708 switch (iface->family) {
709 case ARPHRD_ETHER:
710 case ARPHRD_IEEE802:
711 dhcp->hwlen = (uint8_t)iface->hwlen;
712 memcpy(&dhcp->chaddr, &iface->hwaddr, iface->hwlen);
713 break;
714 }
715
716 if (ifo->options & DHCPCD_BROADCAST &&
717 dhcp->ciaddr == 0 &&
718 type != DHCP_DECLINE &&
719 type != DHCP_RELEASE)
720 dhcp->flags = htons(BROADCAST_FLAG);
721
722 if (type != DHCP_DECLINE && type != DHCP_RELEASE) {
723 if (up < 0 || up > (time_t)UINT16_MAX)
724 dhcp->secs = htons((uint16_t)UINT16_MAX);
725 else
726 dhcp->secs = htons(up);
727 }
728 dhcp->xid = htonl(state->xid);
729 dhcp->cookie = htonl(MAGIC_COOKIE);
730
731 *p++ = DHO_MESSAGETYPE;
732 *p++ = 1;
733 *p++ = type;
734
735 if (state->clientid) {
736 *p++ = DHO_CLIENTID;
737 memcpy(p, state->clientid, state->clientid[0] + 1);
738 p += state->clientid[0] + 1;
739 }
740
741 if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) {
742 if (type == DHCP_DECLINE ||
743 (type == DHCP_REQUEST &&
744 lease->addr.s_addr != state->addr.s_addr))
745 {
746 PUTADDR(DHO_IPADDRESS, lease->addr);
747 if (lease->server.s_addr)
748 PUTADDR(DHO_SERVERID, lease->server);
749 }
750
751 if (type == DHCP_RELEASE) {
752 if (lease->server.s_addr)
753 PUTADDR(DHO_SERVERID, lease->server);
754 }
755 }
756
757 if (type == DHCP_DECLINE) {
758 *p++ = DHO_MESSAGE;
759 len = strlen(DAD);
760 *p++ = (uint8_t)len;
761 memcpy(p, DAD, len);
762 p += len;
763 }
764
765 if (type == DHCP_DISCOVER &&
766 !(iface->ctx->options & DHCPCD_TEST) &&
767 has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT))
768 {
769 /* RFC 4039 Section 3 */
770 *p++ = DHO_RAPIDCOMMIT;
771 *p++ = 0;
772 }
773
774 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_REQUEST)
775 PUTADDR(DHO_IPADDRESS, ifo->req_addr);
776
777 /* RFC 2563 Auto Configure */
778 if (type == DHCP_DISCOVER && ifo->options & DHCPCD_IPV4LL) {
779 *p++ = DHO_AUTOCONFIGURE;
780 *p++ = 1;
781 *p++ = 1;
782 }
783
784 if (type == DHCP_DISCOVER ||
785 type == DHCP_INFORM ||
786 type == DHCP_REQUEST)
787 {
788 int mtu;
789
790 *p++ = DHO_MAXMESSAGESIZE;
791 *p++ = 2;
792 mtu = if_getmtu(iface->name);
793 if (mtu < MTU_MIN) {
794 if (if_setmtu(iface->name, MTU_MIN) == 0)
795 sz = MTU_MIN;
796 } else if (mtu > MTU_MAX) {
797 /* Even though our MTU could be greater than
798 * MTU_MAX (1500) dhcpcd does not presently
799 * handle DHCP packets any bigger. */
800 mtu = MTU_MAX;
801 }
802 sz = htons(mtu);
803 memcpy(p, &sz, 2);
804 p += 2;
805
806 if (ifo->userclass[0]) {
807 *p++ = DHO_USERCLASS;
808 memcpy(p, ifo->userclass, ifo->userclass[0] + 1);
809 p += ifo->userclass[0] + 1;
810 }
811
812 if (ifo->vendorclassid[0]) {
813 *p++ = DHO_VENDORCLASSID;
814 memcpy(p, ifo->vendorclassid,
815 ifo->vendorclassid[0] + 1);
816 p += ifo->vendorclassid[0] + 1;
817 }
818
819
820 if (type != DHCP_INFORM) {
821 if (ifo->leasetime != 0) {
822 *p++ = DHO_LEASETIME;
823 *p++ = 4;
824 ul = htonl(ifo->leasetime);
825 memcpy(p, &ul, 4);
826 p += 4;
827 }
828 }
829
830 if (ifo->hostname[0] == '\0')
831 hostname = get_hostname(hbuf, sizeof(hbuf),
832 ifo->options & DHCPCD_HOSTNAME_SHORT ? 1 : 0);
833 else
834 hostname = ifo->hostname;
835
836 /*
837 * RFC4702 3.1 States that if we send the Client FQDN option
838 * then we MUST NOT also send the Host Name option.
839 * Technically we could, but that is not RFC conformant and
840 * also seems to break some DHCP server implemetations such as
841 * Windows. On the other hand, ISC dhcpd is just as non RFC
842 * conformant by not accepting a partially qualified FQDN.
843 */
844 if (ifo->fqdn != FQDN_DISABLE) {
845 /* IETF DHC-FQDN option (81), RFC4702 */
846 *p++ = DHO_FQDN;
847 lp = p;
848 *p++ = 3;
849 /*
850 * Flags: 0000NEOS
851 * S: 1 => Client requests Server to update
852 * a RR in DNS as well as PTR
853 * O: 1 => Server indicates to client that
854 * DNS has been updated
855 * E: 1 => Name data is DNS format
856 * N: 1 => Client requests Server to not
857 * update DNS
858 */
859 if (hostname)
860 *p++ = (ifo->fqdn & 0x09) | 0x04;
861 else
862 *p++ = (FQDN_NONE & 0x09) | 0x04;
863 *p++ = 0; /* from server for PTR RR */
864 *p++ = 0; /* from server for A RR if S=1 */
865 if (hostname) {
866 i = encode_rfc1035(hostname, p);
867 *lp += i;
868 p += i;
869 }
870 } else if (ifo->options & DHCPCD_HOSTNAME && hostname) {
871 *p++ = DHO_HOSTNAME;
872 len = strlen(hostname);
873 *p++ = (uint8_t)len;
874 memcpy(p, hostname, len);
875 p += len;
876 }
877
878 /* vendor is already encoded correctly, so just add it */
879 if (ifo->vendor[0]) {
880 *p++ = DHO_VENDOR;
881 memcpy(p, ifo->vendor, ifo->vendor[0] + 1);
882 p += ifo->vendor[0] + 1;
883 }
884
885 if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) !=
886 DHCPCD_AUTH_SENDREQUIRE)
887 {
888 /* We support HMAC-MD5 */
889 *p++ = DHO_FORCERENEW_NONCE;
890 *p++ = 1;
891 *p++ = AUTH_ALG_HMAC_MD5;
892 }
893
894 if (ifo->vivco_len) {
895 *p++ = DHO_VIVCO;
896 lp = p++;
897 *lp = sizeof(ul);
898 ul = htonl(ifo->vivco_en);
899 memcpy(p, &ul, sizeof(ul));
900 p += sizeof(ul);
901 for (i = 0, vivco = ifo->vivco;
902 i < ifo->vivco_len;
903 i++, vivco++)
904 {
905 len = (size_t)(p - m) + vivco->len + 1;
906 if (len > sizeof(*dhcp))
907 goto toobig;
908 if (vivco->len + 2 + *lp > 255) {
909 syslog(LOG_ERR,
910 "%s: VIVCO option too big",
911 iface->name);
912 free(dhcp);
913 return -1;
914 }
915 *p++ = (uint8_t)vivco->len;
916 memcpy(p, vivco->data, vivco->len);
917 p += vivco->len;
918 *lp += (uint8_t)vivco->len + 1;
919 }
920 }
921
922 len = (size_t)((p - m) + 3);
923 if (len > sizeof(*dhcp))
924 goto toobig;
925 *p++ = DHO_PARAMETERREQUESTLIST;
926 n_params = p;
927 *p++ = 0;
928 for (i = 0, opt = iface->ctx->dhcp_opts;
929 i < iface->ctx->dhcp_opts_len;
930 i++, opt++)
931 {
932 if (!(opt->type & REQUEST ||
933 has_option_mask(ifo->requestmask, opt->option)))
934 continue;
935 if (opt->type & NOREQ)
936 continue;
937 if (type == DHCP_INFORM &&
938 (opt->option == DHO_RENEWALTIME ||
939 opt->option == DHO_REBINDTIME))
940 continue;
941 len = (size_t)((p - m) + 2);
942 if (len > sizeof(*dhcp))
943 goto toobig;
944 *p++ = (uint8_t)opt->option;
945 }
946 *n_params = (uint8_t)(p - n_params - 1);
947 }
948
949 /* silence GCC */
950 auth_len = 0;
951 auth = NULL;
952
953 if (ifo->auth.options & DHCPCD_AUTH_SEND) {
954 auth_len = (size_t)dhcp_auth_encode(&ifo->auth,
955 state->auth.token,
956 NULL, 0, 4, type, NULL, 0);
957 if (auth_len > 0) {
958 len = (size_t)((p + auth_len) - m);
959 if (auth_len > 255 || len > sizeof(*dhcp))
960 goto toobig;
961 *p++ = DHO_AUTHENTICATION;
962 *p++ = (uint8_t)auth_len;
963 auth = p;
964 p += auth_len;
965 } else if ((ssize_t)auth_len == -1)
966 syslog(LOG_ERR, "%s: dhcp_auth_encode: %m",
967 iface->name);
968 }
969
970 *p++ = DHO_END;
971
972 #ifdef BOOTP_MESSAGE_LENTH_MIN
973 /* Some crappy DHCP servers think they have to obey the BOOTP minimum
974 * message length.
975 * They are wrong, but we should still cater for them. */
976 while (p - m < BOOTP_MESSAGE_LENTH_MIN)
977 *p++ = DHO_PAD;
978 #endif
979
980 len = (size_t)(p - m);
981 if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len > 0)
982 dhcp_auth_encode(&ifo->auth, state->auth.token,
983 m, len, 4, type, auth, auth_len);
984
985 *message = dhcp;
986 return (ssize_t)len;
987
988 toobig:
989 syslog(LOG_ERR, "%s: DHCP messge too big", iface->name);
990 free(dhcp);
991 return -1;
992 }
993
994 ssize_t
995 write_lease(const struct interface *ifp, const struct dhcp_message *dhcp)
996 {
997 int fd;
998 size_t len;
999 ssize_t bytes;
1000 const uint8_t *e, *p;
1001 uint8_t l;
1002 uint8_t o = 0;
1003 const struct dhcp_state *state = D_CSTATE(ifp);
1004
1005 /* We don't write BOOTP leases */
1006 if (is_bootp(ifp, dhcp)) {
1007 unlink(state->leasefile);
1008 return 0;
1009 }
1010
1011 syslog(LOG_DEBUG, "%s: writing lease `%s'",
1012 ifp->name, state->leasefile);
1013
1014 fd = open(state->leasefile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1015 if (fd == -1)
1016 return -1;
1017
1018 /* Only write as much as we need */
1019 p = dhcp->options;
1020 e = p + sizeof(dhcp->options);
1021 len = sizeof(*dhcp);
1022 while (p < e) {
1023 o = *p;
1024 if (o == DHO_END) {
1025 len = (size_t)(p - (const uint8_t *)dhcp);
1026 break;
1027 }
1028 p++;
1029 if (o != DHO_PAD) {
1030 l = *p++;
1031 p += l;
1032 }
1033 }
1034 bytes = write(fd, dhcp, len);
1035 close(fd);
1036 return bytes;
1037 }
1038
1039 struct dhcp_message *
1040 read_lease(struct interface *ifp)
1041 {
1042 int fd;
1043 struct dhcp_message *dhcp;
1044 struct dhcp_state *state = D_STATE(ifp);
1045 ssize_t bytes;
1046 const uint8_t *auth;
1047 uint8_t type;
1048 size_t auth_len;
1049
1050 fd = open(state->leasefile, O_RDONLY);
1051 if (fd == -1) {
1052 if (errno != ENOENT)
1053 syslog(LOG_ERR, "%s: open `%s': %m",
1054 ifp->name, state->leasefile);
1055 return NULL;
1056 }
1057 syslog(LOG_DEBUG, "%s: reading lease `%s'",
1058 ifp->name, state->leasefile);
1059 dhcp = calloc(1, sizeof(*dhcp));
1060 if (dhcp == NULL) {
1061 close(fd);
1062 return NULL;
1063 }
1064 bytes = read(fd, dhcp, sizeof(*dhcp));
1065 close(fd);
1066 if (bytes < 0) {
1067 free(dhcp);
1068 return NULL;
1069 }
1070
1071 /* We may have found a BOOTP server */
1072 if (get_option_uint8(ifp->ctx, &type, dhcp, DHO_MESSAGETYPE) == -1)
1073 type = 0;
1074 /* Authenticate the message */
1075 auth = get_option(ifp->ctx, dhcp, DHO_AUTHENTICATION, &auth_len);
1076 if (auth) {
1077 if (dhcp_auth_validate(&state->auth, &ifp->options->auth,
1078 (uint8_t *)dhcp, sizeof(*dhcp), 4, type,
1079 auth, auth_len) == NULL)
1080 {
1081 syslog(LOG_DEBUG, "%s: dhcp_auth_validate: %m",
1082 ifp->name);
1083 free(dhcp);
1084 return NULL;
1085 }
1086 if (state->auth.token)
1087 syslog(LOG_DEBUG, "%s: validated using 0x%08" PRIu32,
1088 ifp->name, state->auth.token->secretid);
1089 else
1090 syslog(LOG_DEBUG, "%s: accepted reconfigure key",
1091 ifp->name);
1092 }
1093
1094 return dhcp;
1095 }
1096
1097 static const struct dhcp_opt *
1098 dhcp_getoverride(const struct if_options *ifo, unsigned int o)
1099 {
1100 size_t i;
1101 const struct dhcp_opt *opt;
1102
1103 for (i = 0, opt = ifo->dhcp_override;
1104 i < ifo->dhcp_override_len;
1105 i++, opt++)
1106 {
1107 if (opt->option == o)
1108 return opt;
1109 }
1110 return NULL;
1111 }
1112
1113 static const uint8_t *
1114 dhcp_getoption(struct dhcpcd_ctx *ctx,
1115 size_t *os, unsigned int *code, size_t *len,
1116 const uint8_t *od, size_t ol, struct dhcp_opt **oopt)
1117 {
1118 size_t i;
1119 struct dhcp_opt *opt;
1120
1121 if (od) {
1122 if (ol < 2) {
1123 errno = EINVAL;
1124 return NULL;
1125 }
1126 *os = 2; /* code + len */
1127 *code = (unsigned int)*od++;
1128 *len = (size_t)*od++;
1129 if (*len > ol) {
1130 errno = EINVAL;
1131 return NULL;
1132 }
1133 }
1134
1135 for (i = 0, opt = ctx->dhcp_opts; i < ctx->dhcp_opts_len; i++, opt++) {
1136 if (opt->option == *code) {
1137 *oopt = opt;
1138 break;
1139 }
1140 }
1141
1142 return od;
1143 }
1144
1145 ssize_t
1146 dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp,
1147 const struct interface *ifp)
1148 {
1149 const struct if_options *ifo;
1150 const uint8_t *p;
1151 struct in_addr addr;
1152 struct in_addr net;
1153 struct in_addr brd;
1154 struct dhcp_opt *opt, *vo;
1155 size_t e, i, pl;
1156 char **ep;
1157 char cidr[4];
1158 uint8_t overl = 0;
1159 uint32_t en;
1160
1161 e = 0;
1162 ifo = ifp->options;
1163 get_option_uint8(ifp->ctx, &overl, dhcp, DHO_OPTIONSOVERLOADED);
1164
1165 if (env == NULL) {
1166 if (dhcp->yiaddr || dhcp->ciaddr)
1167 e += 5;
1168 if (*dhcp->bootfile && !(overl & 1))
1169 e++;
1170 if (*dhcp->servername && !(overl & 2))
1171 e++;
1172 for (i = 0, opt = ifp->ctx->dhcp_opts;
1173 i < ifp->ctx->dhcp_opts_len;
1174 i++, opt++)
1175 {
1176 if (has_option_mask(ifo->nomask, opt->option))
1177 continue;
1178 if (dhcp_getoverride(ifo, opt->option))
1179 continue;
1180 p = get_option(ifp->ctx, dhcp, opt->option, &pl);
1181 if (!p)
1182 continue;
1183 e += dhcp_envoption(ifp->ctx, NULL, NULL, ifp->name,
1184 opt, dhcp_getoption, p, pl);
1185 }
1186 for (i = 0, opt = ifo->dhcp_override;
1187 i < ifo->dhcp_override_len;
1188 i++, opt++)
1189 {
1190 if (has_option_mask(ifo->nomask, opt->option))
1191 continue;
1192 p = get_option(ifp->ctx, dhcp, opt->option, &pl);
1193 if (!p)
1194 continue;
1195 e += dhcp_envoption(ifp->ctx, NULL, NULL, ifp->name,
1196 opt, dhcp_getoption, p, pl);
1197 }
1198 return (ssize_t)e;
1199 }
1200
1201 ep = env;
1202 if (dhcp->yiaddr || dhcp->ciaddr) {
1203 /* Set some useful variables that we derive from the DHCP
1204 * message but are not necessarily in the options */
1205 addr.s_addr = dhcp->yiaddr ? dhcp->yiaddr : dhcp->ciaddr;
1206 setvar(&ep, prefix, "ip_address", inet_ntoa(addr));
1207 if (get_option_addr(ifp->ctx, &net,
1208 dhcp, DHO_SUBNETMASK) == -1) {
1209 net.s_addr = ipv4_getnetmask(addr.s_addr);
1210 setvar(&ep, prefix, "subnet_mask", inet_ntoa(net));
1211 }
1212 snprintf(cidr, sizeof(cidr), "%d", inet_ntocidr(net));
1213 setvar(&ep, prefix, "subnet_cidr", cidr);
1214 if (get_option_addr(ifp->ctx, &brd,
1215 dhcp, DHO_BROADCAST) == -1) {
1216 brd.s_addr = addr.s_addr | ~net.s_addr;
1217 setvar(&ep, prefix, "broadcast_address",
1218 inet_ntoa(brd));
1219 }
1220 addr.s_addr = dhcp->yiaddr & net.s_addr;
1221 setvar(&ep, prefix, "network_number", inet_ntoa(addr));
1222 }
1223
1224 if (*dhcp->bootfile && !(overl & 1))
1225 setvar(&ep, prefix, "filename", (const char *)dhcp->bootfile);
1226 if (*dhcp->servername && !(overl & 2))
1227 setvar(&ep, prefix, "server_name",
1228 (const char *)dhcp->servername);
1229
1230 /* Zero our indexes */
1231 if (env) {
1232 for (i = 0, opt = ifp->ctx->dhcp_opts;
1233 i < ifp->ctx->dhcp_opts_len;
1234 i++, opt++)
1235 dhcp_zero_index(opt);
1236 for (i = 0, opt = ifp->options->dhcp_override;
1237 i < ifp->options->dhcp_override_len;
1238 i++, opt++)
1239 dhcp_zero_index(opt);
1240 for (i = 0, opt = ifp->ctx->vivso;
1241 i < ifp->ctx->vivso_len;
1242 i++, opt++)
1243 dhcp_zero_index(opt);
1244 }
1245
1246 for (i = 0, opt = ifp->ctx->dhcp_opts;
1247 i < ifp->ctx->dhcp_opts_len;
1248 i++, opt++)
1249 {
1250 if (has_option_mask(ifo->nomask, opt->option))
1251 continue;
1252 if (dhcp_getoverride(ifo, opt->option))
1253 continue;
1254 if ((p = get_option(ifp->ctx, dhcp, opt->option, &pl))) {
1255 ep += dhcp_envoption(ifp->ctx, ep, prefix, ifp->name,
1256 opt, dhcp_getoption, p, pl);
1257 if (opt->option == DHO_VIVSO &&
1258 pl > (int)sizeof(uint32_t))
1259 {
1260 memcpy(&en, p, sizeof(en));
1261 en = ntohl(en);
1262 vo = vivso_find(en, ifp);
1263 if (vo) {
1264 /* Skip over en + total size */
1265 p += sizeof(en) + 1;
1266 pl -= sizeof(en) + 1;
1267 ep += dhcp_envoption(ifp->ctx,
1268 ep, prefix, ifp->name,
1269 vo, dhcp_getoption, p, pl);
1270 }
1271 }
1272 }
1273 }
1274
1275 for (i = 0, opt = ifo->dhcp_override;
1276 i < ifo->dhcp_override_len;
1277 i++, opt++)
1278 {
1279 if (has_option_mask(ifo->nomask, opt->option))
1280 continue;
1281 if ((p = get_option(ifp->ctx, dhcp, opt->option, &pl)))
1282 ep += dhcp_envoption(ifp->ctx, ep, prefix, ifp->name,
1283 opt, dhcp_getoption, p, pl);
1284 }
1285
1286 return ep - env;
1287 }
1288
1289 static void
1290 get_lease(struct dhcpcd_ctx *ctx,
1291 struct dhcp_lease *lease, const struct dhcp_message *dhcp)
1292 {
1293
1294 lease->cookie = dhcp->cookie;
1295 /* BOOTP does not set yiaddr for replies when ciaddr is set. */
1296 if (dhcp->yiaddr)
1297 lease->addr.s_addr = dhcp->yiaddr;
1298 else
1299 lease->addr.s_addr = dhcp->ciaddr;
1300 if (get_option_addr(ctx, &lease->net, dhcp, DHO_SUBNETMASK) == -1)
1301 lease->net.s_addr = ipv4_getnetmask(lease->addr.s_addr);
1302 if (get_option_addr(ctx, &lease->brd, dhcp, DHO_BROADCAST) == -1)
1303 lease->brd.s_addr = lease->addr.s_addr | ~lease->net.s_addr;
1304 if (get_option_uint32(ctx, &lease->leasetime, dhcp, DHO_LEASETIME) != 0)
1305 lease->leasetime = ~0U; /* Default to infinite lease */
1306 if (get_option_uint32(ctx, &lease->renewaltime,
1307 dhcp, DHO_RENEWALTIME) != 0)
1308 lease->renewaltime = 0;
1309 if (get_option_uint32(ctx, &lease->rebindtime,
1310 dhcp, DHO_REBINDTIME) != 0)
1311 lease->rebindtime = 0;
1312 if (get_option_addr(ctx, &lease->server, dhcp, DHO_SERVERID) != 0)
1313 lease->server.s_addr = INADDR_ANY;
1314 }
1315
1316 static const char *
1317 get_dhcp_op(uint8_t type)
1318 {
1319 const struct dhcp_op *d;
1320
1321 for (d = dhcp_ops; d->name; d++)
1322 if (d->value == type)
1323 return d->name;
1324 return NULL;
1325 }
1326
1327 static void
1328 dhcp_fallback(void *arg)
1329 {
1330 struct interface *iface;
1331
1332 iface = (struct interface *)arg;
1333 dhcpcd_selectprofile(iface, iface->options->fallback);
1334 dhcpcd_startinterface(iface);
1335 }
1336
1337 uint32_t
1338 dhcp_xid(const struct interface *ifp)
1339 {
1340 uint32_t xid;
1341
1342 if (ifp->options->options & DHCPCD_XID_HWADDR &&
1343 ifp->hwlen >= sizeof(xid))
1344 /* The lower bits are probably more unique on the network */
1345 memcpy(&xid, (ifp->hwaddr + ifp->hwlen) - sizeof(xid),
1346 sizeof(xid));
1347 else
1348 xid = arc4random();
1349
1350 return xid;
1351 }
1352
1353 void
1354 dhcp_close(struct interface *ifp)
1355 {
1356 struct dhcp_state *state = D_STATE(ifp);
1357
1358 if (state == NULL)
1359 return;
1360
1361 if (state->arp_fd != -1) {
1362 eloop_event_delete(ifp->ctx->eloop, state->arp_fd);
1363 close(state->arp_fd);
1364 state->arp_fd = -1;
1365 }
1366 if (state->raw_fd != -1) {
1367 eloop_event_delete(ifp->ctx->eloop, state->raw_fd);
1368 close(state->raw_fd);
1369 state->raw_fd = -1;
1370 }
1371 if (state->udp_fd != -1) {
1372 eloop_event_delete(ifp->ctx->eloop, state->udp_fd);
1373 close(state->udp_fd);
1374 state->udp_fd = -1;
1375 }
1376
1377 state->interval = 0;
1378 }
1379
1380 static int
1381 dhcp_openudp(struct dhcpcd_ctx *ctx, struct interface *ifp)
1382 {
1383 int s;
1384 struct sockaddr_in sin;
1385 int n;
1386 struct dhcp_state *state;
1387 #ifdef SO_BINDTODEVICE
1388 struct ifreq ifr;
1389 char *p;
1390 #endif
1391
1392 #ifdef SOCK_CLOEXEC
1393 if ((s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
1394 return -1;
1395 #else
1396 if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
1397 return -1;
1398 if ((n = fcntl(s, F_GETFD, 0)) == -1 ||
1399 fcntl(s, F_SETFD, n | FD_CLOEXEC) == -1)
1400 {
1401 close(s);
1402 return -1;
1403 }
1404 #endif
1405
1406 n = 1;
1407 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
1408 goto eexit;
1409 #ifdef SO_BINDTODEVICE
1410 if (ifp) {
1411 memset(&ifr, 0, sizeof(ifr));
1412 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1413 /* We can only bind to the real device */
1414 p = strchr(ifr.ifr_name, ':');
1415 if (p)
1416 *p = '\0';
1417 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, &ifr,
1418 sizeof(ifr)) == -1)
1419 goto eexit;
1420 }
1421 #endif
1422 memset(&sin, 0, sizeof(sin));
1423 sin.sin_family = AF_INET;
1424 sin.sin_port = htons(DHCP_CLIENT_PORT);
1425 if (ifp) {
1426 state = D_STATE(ifp);
1427 sin.sin_addr.s_addr = state->addr.s_addr;
1428 } else
1429 state = NULL; /* appease gcc */
1430 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
1431 goto eexit;
1432
1433 if (ifp)
1434 state->udp_fd = s;
1435 else
1436 ctx->udp_fd = s;
1437 return 0;
1438
1439 eexit:
1440 close(s);
1441 return -1;
1442 }
1443
1444 static ssize_t
1445 dhcp_sendpacket(const struct interface *iface, struct in_addr to,
1446 const uint8_t *data, size_t len)
1447 {
1448 struct sockaddr_in sin;
1449
1450 memset(&sin, 0, sizeof(sin));
1451 sin.sin_family = AF_INET;
1452 sin.sin_addr.s_addr = to.s_addr;
1453 sin.sin_port = htons(DHCP_SERVER_PORT);
1454 return sendto(D_CSTATE(iface)->udp_fd, data, len, 0,
1455 (struct sockaddr *)&sin, sizeof(sin));
1456 }
1457
1458 static uint16_t
1459 checksum(const void *data, uint16_t len)
1460 {
1461 const uint8_t *addr = data;
1462 uint32_t sum = 0;
1463 uint16_t res;
1464
1465 while (len > 1) {
1466 sum += addr[0] * 256 + addr[1];
1467 addr += 2;
1468 len -= 2;
1469 }
1470
1471 if (len == 1)
1472 sum += *addr * 256;
1473
1474 sum = (sum >> 16) + (sum & 0xffff);
1475 sum += (sum >> 16);
1476
1477 res = htons(sum);
1478
1479 return ~res;
1480 }
1481
1482 static struct udp_dhcp_packet *
1483 dhcp_makeudppacket(size_t *sz, const uint8_t *data, size_t length,
1484 struct in_addr source, struct in_addr dest)
1485 {
1486 struct udp_dhcp_packet *udpp;
1487 struct ip *ip;
1488 struct udphdr *udp;
1489
1490 udpp = calloc(1, sizeof(*udpp));
1491 if (udpp == NULL)
1492 return NULL;
1493 ip = &udpp->ip;
1494 udp = &udpp->udp;
1495
1496 /* OK, this is important :)
1497 * We copy the data to our packet and then create a small part of the
1498 * ip structure and an invalid ip_len (basically udp length).
1499 * We then fill the udp structure and put the checksum
1500 * of the whole packet into the udp checksum.
1501 * Finally we complete the ip structure and ip checksum.
1502 * If we don't do the ordering like so then the udp checksum will be
1503 * broken, so find another way of doing it! */
1504
1505 memcpy(&udpp->dhcp, data, length);
1506
1507 ip->ip_p = IPPROTO_UDP;
1508 ip->ip_src.s_addr = source.s_addr;
1509 if (dest.s_addr == 0)
1510 ip->ip_dst.s_addr = INADDR_BROADCAST;
1511 else
1512 ip->ip_dst.s_addr = dest.s_addr;
1513
1514 udp->uh_sport = htons(DHCP_CLIENT_PORT);
1515 udp->uh_dport = htons(DHCP_SERVER_PORT);
1516 udp->uh_ulen = htons(sizeof(*udp) + length);
1517 ip->ip_len = udp->uh_ulen;
1518 udp->uh_sum = checksum(udpp, sizeof(*udpp));
1519
1520 ip->ip_v = IPVERSION;
1521 ip->ip_hl = sizeof(*ip) >> 2;
1522 ip->ip_id = (uint16_t)arc4random_uniform(UINT16_MAX);
1523 ip->ip_ttl = IPDEFTTL;
1524 ip->ip_len = htons(sizeof(*ip) + sizeof(*udp) + length);
1525 ip->ip_sum = checksum(ip, sizeof(*ip));
1526
1527 *sz = sizeof(*ip) + sizeof(*udp) + length;
1528 return udpp;
1529 }
1530
1531 static void
1532 send_message(struct interface *iface, uint8_t type,
1533 void (*callback)(void *))
1534 {
1535 struct dhcp_state *state = D_STATE(iface);
1536 struct if_options *ifo = iface->options;
1537 struct dhcp_message *dhcp;
1538 struct udp_dhcp_packet *udp;
1539 size_t len;
1540 ssize_t r;
1541 struct in_addr from, to;
1542 in_addr_t a = 0;
1543 struct timeval tv;
1544
1545 if (!callback)
1546 syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x",
1547 iface->name, get_dhcp_op(type), state->xid);
1548 else {
1549 if (state->interval == 0)
1550 state->interval = 4;
1551 else {
1552 state->interval *= 2;
1553 if (state->interval > 64)
1554 state->interval = 64;
1555 }
1556 tv.tv_sec = state->interval + DHCP_RAND_MIN;
1557 tv.tv_usec = (suseconds_t)arc4random_uniform(
1558 (DHCP_RAND_MAX - DHCP_RAND_MIN) * 1000000);
1559 timernorm(&tv);
1560 syslog(LOG_DEBUG,
1561 "%s: sending %s (xid 0x%x), next in %0.1f seconds",
1562 iface->name, get_dhcp_op(type), state->xid,
1563 timeval_to_double(&tv));
1564 }
1565
1566 /* Ensure sockets are open. */
1567 if (dhcp_open(iface) == -1) {
1568 if (!(iface->ctx->options & DHCPCD_TEST))
1569 dhcp_drop(iface, "FAIL");
1570 return;
1571 }
1572
1573 /* If we couldn't open a UDP port for our IP address
1574 * then we cannot renew.
1575 * This could happen if our IP was pulled out from underneath us.
1576 * Also, we should not unicast from a BOOTP lease. */
1577 if (state->udp_fd == -1 ||
1578 (!(ifo->options & DHCPCD_INFORM) &&
1579 is_bootp(iface, state->new)))
1580 {
1581 a = state->addr.s_addr;
1582 state->addr.s_addr = 0;
1583 }
1584 r = make_message(&dhcp, iface, type);
1585 if (r == -1)
1586 goto fail;
1587 len = (size_t)r;
1588 if (a)
1589 state->addr.s_addr = a;
1590 from.s_addr = dhcp->ciaddr;
1591 if (from.s_addr)
1592 to.s_addr = state->lease.server.s_addr;
1593 else
1594 to.s_addr = 0;
1595 if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
1596 r = dhcp_sendpacket(iface, to, (uint8_t *)dhcp, len);
1597 if (r == -1) {
1598 syslog(LOG_ERR, "%s: dhcp_sendpacket: %m", iface->name);
1599 dhcp_close(iface);
1600 }
1601 } else {
1602 size_t ulen;
1603
1604 r = 0;
1605 udp = dhcp_makeudppacket(&ulen, (uint8_t *)dhcp, len, from, to);
1606 if (udp == NULL) {
1607 syslog(LOG_ERR, "dhcp_makeudppacket: %m");
1608 } else {
1609 r = if_sendrawpacket(iface, ETHERTYPE_IP,
1610 (uint8_t *)udp, ulen);
1611 free(udp);
1612 }
1613 /* If we failed to send a raw packet this normally means
1614 * we don't have the ability to work beneath the IP layer
1615 * for this interface.
1616 * As such we remove it from consideration without actually
1617 * stopping the interface. */
1618 if (r == -1) {
1619 syslog(LOG_ERR, "%s: if_sendrawpacket: %m",
1620 iface->name);
1621 switch(errno) {
1622 case ENETDOWN:
1623 case ENETRESET:
1624 case ENETUNREACH:
1625 break;
1626 default:
1627 if (!(iface->ctx->options & DHCPCD_TEST))
1628 dhcp_drop(iface, "FAIL");
1629 dhcp_close(iface);
1630 eloop_timeout_delete(iface->ctx->eloop,
1631 NULL, iface);
1632 callback = NULL;
1633 }
1634 }
1635 }
1636 free(dhcp);
1637
1638 fail:
1639 /* Even if we fail to send a packet we should continue as we are
1640 * as our failure timeouts will change out codepath when needed. */
1641 if (callback)
1642 eloop_timeout_add_tv(iface->ctx->eloop, &tv, callback, iface);
1643 }
1644
1645 static void
1646 send_inform(void *arg)
1647 {
1648
1649 send_message((struct interface *)arg, DHCP_INFORM, send_inform);
1650 }
1651
1652 static void
1653 send_discover(void *arg)
1654 {
1655
1656 send_message((struct interface *)arg, DHCP_DISCOVER, send_discover);
1657 }
1658
1659 static void
1660 send_request(void *arg)
1661 {
1662
1663 send_message((struct interface *)arg, DHCP_REQUEST, send_request);
1664 }
1665
1666 static void
1667 send_renew(void *arg)
1668 {
1669
1670 send_message((struct interface *)arg, DHCP_REQUEST, send_renew);
1671 }
1672
1673 static void
1674 send_rebind(void *arg)
1675 {
1676
1677 send_message((struct interface *)arg, DHCP_REQUEST, send_rebind);
1678 }
1679
1680 void
1681 dhcp_discover(void *arg)
1682 {
1683 struct interface *ifp = arg;
1684 struct dhcp_state *state = D_STATE(ifp);
1685 struct if_options *ifo = ifp->options;
1686 time_t timeout = ifo->timeout;
1687
1688 /* If we're rebooting and we're not daemonised then we need
1689 * to shorten the normal timeout to ensure we try correctly
1690 * for a fallback or IPv4LL address. */
1691 if (state->state == DHS_REBOOT &&
1692 !(ifp->ctx->options & DHCPCD_DAEMONISED))
1693 {
1694 if (ifo->reboot >= timeout)
1695 timeout = 2;
1696 else
1697 timeout -= ifo->reboot;
1698 }
1699
1700 state->state = DHS_DISCOVER;
1701 state->xid = dhcp_xid(ifp);
1702 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
1703 if (ifo->fallback)
1704 eloop_timeout_add_sec(ifp->ctx->eloop,
1705 timeout, dhcp_fallback, ifp);
1706 else if (ifo->options & DHCPCD_IPV4LL &&
1707 !IN_LINKLOCAL(htonl(state->addr.s_addr)))
1708 {
1709 if (IN_LINKLOCAL(htonl(state->fail.s_addr)))
1710 timeout = RATE_LIMIT_INTERVAL;
1711 eloop_timeout_add_sec(ifp->ctx->eloop,
1712 timeout, ipv4ll_start, ifp);
1713 }
1714 if (ifo->options & DHCPCD_REQUEST)
1715 syslog(LOG_INFO, "%s: soliciting a DHCP lease (requesting %s)",
1716 ifp->name, inet_ntoa(ifo->req_addr));
1717 else
1718 syslog(LOG_INFO, "%s: soliciting a DHCP lease", ifp->name);
1719 send_discover(ifp);
1720 }
1721
1722 static void
1723 dhcp_request(void *arg)
1724 {
1725 struct interface *ifp = arg;
1726 struct dhcp_state *state = D_STATE(ifp);
1727
1728 state->state = DHS_REQUEST;
1729 send_request(ifp);
1730 }
1731
1732 static void
1733 dhcp_expire(void *arg)
1734 {
1735 struct interface *ifp = arg;
1736 struct dhcp_state *state = D_STATE(ifp);
1737
1738 syslog(LOG_ERR, "%s: DHCP lease expired", ifp->name);
1739 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
1740 dhcp_drop(ifp, "EXPIRE");
1741 unlink(state->leasefile);
1742
1743 state->interval = 0;
1744 dhcp_discover(ifp);
1745 }
1746
1747 void
1748 dhcp_decline(struct interface *ifp)
1749 {
1750
1751 send_message(ifp, DHCP_DECLINE, NULL);
1752 }
1753
1754 static void
1755 dhcp_renew(void *arg)
1756 {
1757 struct interface *ifp = arg;
1758 struct dhcp_state *state = D_STATE(ifp);
1759 struct dhcp_lease *lease = &state->lease;
1760
1761 syslog(LOG_DEBUG, "%s: renewing lease of %s",
1762 ifp->name, inet_ntoa(lease->addr));
1763 syslog(LOG_DEBUG, "%s: rebind in %"PRIu32" seconds,"
1764 " expire in %"PRIu32" seconds",
1765 ifp->name, lease->rebindtime - lease->renewaltime,
1766 lease->leasetime - lease->renewaltime);
1767 state->state = DHS_RENEW;
1768 state->xid = dhcp_xid(ifp);
1769 send_renew(ifp);
1770 }
1771
1772 static void
1773 dhcp_rebind(void *arg)
1774 {
1775 struct interface *ifp = arg;
1776 struct dhcp_state *state = D_STATE(ifp);
1777 struct dhcp_lease *lease = &state->lease;
1778
1779 syslog(LOG_WARNING, "%s: failed to renew DHCP, rebinding",
1780 ifp->name);
1781 syslog(LOG_DEBUG, "%s: expire in %"PRIu32" seconds",
1782 ifp->name, lease->leasetime - lease->rebindtime);
1783 state->state = DHS_REBIND;
1784 eloop_timeout_delete(ifp->ctx->eloop, send_renew, ifp);
1785 state->lease.server.s_addr = 0;
1786 ifp->options->options &= ~ DHCPCD_CSR_WARNED;
1787 send_rebind(ifp);
1788 }
1789
1790 void
1791 dhcp_bind(void *arg)
1792 {
1793 struct interface *iface = arg;
1794 struct dhcp_state *state = D_STATE(iface);
1795 struct if_options *ifo = iface->options;
1796 struct dhcp_lease *lease = &state->lease;
1797 struct timeval tv;
1798 uint8_t ipv4ll = 0;
1799
1800 state->reason = NULL;
1801 state->xid = 0;
1802 free(state->old);
1803 state->old = state->new;
1804 state->new = state->offer;
1805 state->offer = NULL;
1806 get_lease(iface->ctx, lease, state->new);
1807 if (ifo->options & DHCPCD_STATIC) {
1808 syslog(LOG_INFO, "%s: using static address %s/%d",
1809 iface->name, inet_ntoa(lease->addr),
1810 inet_ntocidr(lease->net));
1811 lease->leasetime = ~0U;
1812 state->reason = "STATIC";
1813 } else if (state->new->cookie != htonl(MAGIC_COOKIE)) {
1814 syslog(LOG_INFO, "%s: using IPv4LL address %s",
1815 iface->name, inet_ntoa(lease->addr));
1816 lease->leasetime = ~0U;
1817 state->reason = "IPV4LL";
1818 ipv4ll = 1;
1819 } else if (ifo->options & DHCPCD_INFORM) {
1820 if (ifo->req_addr.s_addr != 0)
1821 lease->addr.s_addr = ifo->req_addr.s_addr;
1822 else
1823 lease->addr.s_addr = state->addr.s_addr;
1824 syslog(LOG_INFO, "%s: received approval for %s", iface->name,
1825 inet_ntoa(lease->addr));
1826 lease->leasetime = ~0U;
1827 state->reason = "INFORM";
1828 } else {
1829 if (gettimeofday(&tv, NULL) == 0)
1830 lease->leasedfrom = tv.tv_sec;
1831 else if (lease->frominfo)
1832 state->reason = "TIMEOUT";
1833 if (lease->leasetime == ~0U) {
1834 lease->renewaltime =
1835 lease->rebindtime =
1836 lease->leasetime;
1837 syslog(LOG_INFO, "%s: leased %s for infinity",
1838 iface->name, inet_ntoa(lease->addr));
1839 } else {
1840 if (lease->leasetime < DHCP_MIN_LEASE) {
1841 syslog(LOG_WARNING,
1842 "%s: minimum lease is %d seconds",
1843 iface->name, DHCP_MIN_LEASE);
1844 lease->leasetime = DHCP_MIN_LEASE;
1845 }
1846 if (lease->rebindtime == 0)
1847 lease->rebindtime =
1848 (uint32_t)(lease->leasetime * T2);
1849 else if (lease->rebindtime >= lease->leasetime) {
1850 lease->rebindtime =
1851 (uint32_t)(lease->leasetime * T2);
1852 syslog(LOG_WARNING,
1853 "%s: rebind time greater than lease "
1854 "time, forcing to %"PRIu32" seconds",
1855 iface->name, lease->rebindtime);
1856 }
1857 if (lease->renewaltime == 0)
1858 lease->renewaltime =
1859 (uint32_t)(lease->leasetime * T1);
1860 else if (lease->renewaltime > lease->rebindtime) {
1861 lease->renewaltime =
1862 (uint32_t)(lease->leasetime * T1);
1863 syslog(LOG_WARNING,
1864 "%s: renewal time greater than rebind "
1865 "time, forcing to %"PRIu32" seconds",
1866 iface->name, lease->renewaltime);
1867 }
1868 syslog(lease->addr.s_addr == state->addr.s_addr ?
1869 LOG_DEBUG : LOG_INFO,
1870 "%s: leased %s for %"PRIu32" seconds", iface->name,
1871 inet_ntoa(lease->addr), lease->leasetime);
1872 }
1873 }
1874 if (iface->ctx->options & DHCPCD_TEST) {
1875 state->reason = "TEST";
1876 script_runreason(iface, state->reason);
1877 eloop_exit(iface->ctx->eloop, EXIT_SUCCESS);
1878 return;
1879 }
1880 if (state->reason == NULL) {
1881 if (state->old) {
1882 if (state->old->yiaddr == state->new->yiaddr &&
1883 lease->server.s_addr)
1884 state->reason = "RENEW";
1885 else
1886 state->reason = "REBIND";
1887 } else if (state->state == DHS_REBOOT)
1888 state->reason = "REBOOT";
1889 else
1890 state->reason = "BOUND";
1891 }
1892 if (lease->leasetime == ~0U)
1893 lease->renewaltime = lease->rebindtime = lease->leasetime;
1894 else {
1895 eloop_timeout_add_sec(iface->ctx->eloop,
1896 (time_t)lease->renewaltime, dhcp_renew, iface);
1897 eloop_timeout_add_sec(iface->ctx->eloop,
1898 (time_t)lease->rebindtime, dhcp_rebind, iface);
1899 eloop_timeout_add_sec(iface->ctx->eloop,
1900 (time_t)lease->leasetime, dhcp_expire, iface);
1901 syslog(LOG_DEBUG,
1902 "%s: renew in %"PRIu32" seconds, rebind in %"PRIu32
1903 " seconds",
1904 iface->name, lease->renewaltime, lease->rebindtime);
1905 }
1906 ipv4_applyaddr(iface);
1907 if (dhcpcd_daemonise(iface->ctx) == 0) {
1908 if (!ipv4ll)
1909 arp_close(iface);
1910 state->state = DHS_BOUND;
1911 if (ifo->options & DHCPCD_ARP) {
1912 state->claims = 0;
1913 arp_announce(iface);
1914 }
1915 }
1916 }
1917
1918 static void
1919 dhcp_timeout(void *arg)
1920 {
1921 struct interface *ifp = arg;
1922 struct dhcp_state *state = D_STATE(ifp);
1923
1924 dhcp_bind(ifp);
1925 state->interval = 0;
1926 dhcp_discover(ifp);
1927 }
1928
1929 struct dhcp_message *
1930 dhcp_message_new(const struct in_addr *addr, const struct in_addr *mask)
1931 {
1932 struct dhcp_message *dhcp;
1933 uint8_t *p;
1934
1935 dhcp = calloc(1, sizeof(*dhcp));
1936 if (dhcp == NULL)
1937 return NULL;
1938 dhcp->yiaddr = addr->s_addr;
1939 p = dhcp->options;
1940 if (mask && mask->s_addr != INADDR_ANY) {
1941 *p++ = DHO_SUBNETMASK;
1942 *p++ = sizeof(mask->s_addr);
1943 memcpy(p, &mask->s_addr, sizeof(mask->s_addr));
1944 p+= sizeof(mask->s_addr);
1945 }
1946 *p++ = DHO_END;
1947 return dhcp;
1948 }
1949
1950 static void
1951 dhcp_static(struct interface *ifp)
1952 {
1953 struct if_options *ifo;
1954 struct dhcp_state *state;
1955
1956 state = D_STATE(ifp);
1957 ifo = ifp->options;
1958 if (ifo->req_addr.s_addr == INADDR_ANY) {
1959 syslog(LOG_INFO,
1960 "%s: waiting for 3rd party to "
1961 "configure IP address",
1962 ifp->name);
1963 state->reason = "3RDPARTY";
1964 script_runreason(ifp, state->reason);
1965 return;
1966 }
1967 state->offer = dhcp_message_new(&ifo->req_addr, &ifo->req_mask);
1968 if (state->offer) {
1969 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
1970 dhcp_bind(ifp);
1971 }
1972 }
1973
1974 void
1975 dhcp_inform(struct interface *ifp)
1976 {
1977 struct dhcp_state *state;
1978 struct if_options *ifo;
1979 struct ipv4_addr *ap;
1980
1981 state = D_STATE(ifp);
1982 ifo = ifp->options;
1983 if (ifp->ctx->options & DHCPCD_TEST) {
1984 state->addr.s_addr = ifo->req_addr.s_addr;
1985 state->net.s_addr = ifo->req_mask.s_addr;
1986 } else {
1987 if (ifo->req_addr.s_addr == INADDR_ANY) {
1988 state = D_STATE(ifp);
1989 ap = ipv4_findaddr(ifp, NULL, NULL);
1990 if (ap == NULL) {
1991 syslog(LOG_INFO,
1992 "%s: waiting for 3rd party to "
1993 "configure IP address",
1994 ifp->name);
1995 state->reason = "3RDPARTY";
1996 script_runreason(ifp, state->reason);
1997 return;
1998 }
1999 state->offer =
2000 dhcp_message_new(&ap->addr, &ap->net);
2001 } else
2002 state->offer =
2003 dhcp_message_new(&ifo->req_addr, &ifo->req_mask);
2004 if (state->offer) {
2005 ifo->options |= DHCPCD_STATIC;
2006 dhcp_bind(ifp);
2007 ifo->options &= ~DHCPCD_STATIC;
2008 }
2009 }
2010
2011 state->state = DHS_INFORM;
2012 state->xid = dhcp_xid(ifp);
2013 send_inform(ifp);
2014 }
2015
2016 void
2017 dhcp_reboot_newopts(struct interface *ifp, unsigned long long oldopts)
2018 {
2019 struct if_options *ifo;
2020 struct dhcp_state *state = D_STATE(ifp);
2021
2022 if (state == NULL)
2023 return;
2024 ifo = ifp->options;
2025 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) &&
2026 state->addr.s_addr != ifo->req_addr.s_addr) ||
2027 (oldopts & (DHCPCD_INFORM | DHCPCD_STATIC) &&
2028 !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))))
2029 {
2030 dhcp_drop(ifp, "EXPIRE");
2031 }
2032 }
2033
2034 static void
2035 dhcp_reboot(struct interface *ifp)
2036 {
2037 struct if_options *ifo;
2038 struct dhcp_state *state = D_STATE(ifp);
2039
2040 if (state == NULL)
2041 return;
2042 ifo = ifp->options;
2043 state->interval = 0;
2044
2045 if (ifo->options & DHCPCD_LINK && ifp->carrier == LINK_DOWN) {
2046 syslog(LOG_INFO, "%s: waiting for carrier", ifp->name);
2047 return;
2048 }
2049 if (ifo->options & DHCPCD_STATIC) {
2050 dhcp_static(ifp);
2051 return;
2052 }
2053 if (ifo->reboot == 0 || state->offer == NULL) {
2054 dhcp_discover(ifp);
2055 return;
2056 }
2057 if (ifo->options & DHCPCD_INFORM) {
2058 syslog(LOG_INFO, "%s: informing address of %s",
2059 ifp->name, inet_ntoa(state->lease.addr));
2060 } else if (state->offer->cookie == 0) {
2061 if (ifo->options & DHCPCD_IPV4LL) {
2062 state->claims = 0;
2063 arp_announce(ifp);
2064 } else
2065 dhcp_discover(ifp);
2066 return;
2067 } else {
2068 syslog(LOG_INFO, "%s: rebinding lease of %s",
2069 ifp->name, inet_ntoa(state->lease.addr));
2070 }
2071 state->state = DHS_REBOOT;
2072 state->xid = dhcp_xid(ifp);
2073 state->lease.server.s_addr = 0;
2074 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
2075 if (ifo->fallback)
2076 eloop_timeout_add_sec(ifp->ctx->eloop,
2077 ifo->reboot, dhcp_fallback, ifp);
2078 else if (ifo->options & DHCPCD_LASTLEASE && state->lease.frominfo)
2079 eloop_timeout_add_sec(ifp->ctx->eloop,
2080 ifo->reboot, dhcp_timeout, ifp);
2081 else if (!(ifo->options & DHCPCD_INFORM &&
2082 ifp->ctx->options & (DHCPCD_MASTER | DHCPCD_DAEMONISED)))
2083 eloop_timeout_add_sec(ifp->ctx->eloop,
2084 ifo->reboot, dhcp_expire, ifp);
2085 /* Don't bother ARP checking as the server could NAK us first. */
2086 if (ifo->options & DHCPCD_INFORM)
2087 dhcp_inform(ifp);
2088 else {
2089 /* Don't call dhcp_request as that would change the state */
2090 send_request(ifp);
2091 }
2092 }
2093
2094 void
2095 dhcp_drop(struct interface *ifp, const char *reason)
2096 {
2097 struct dhcp_state *state;
2098 #ifdef RELEASE_SLOW
2099 struct timespec ts;
2100 #endif
2101
2102 state = D_STATE(ifp);
2103 if (state == NULL)
2104 return;
2105 dhcp_auth_reset(&state->auth);
2106 dhcp_close(ifp);
2107 arp_close(ifp);
2108 eloop_timeouts_delete(ifp->ctx->eloop, ifp, dhcp_expire, NULL);
2109 if (ifp->options->options & DHCPCD_RELEASE) {
2110 unlink(state->leasefile);
2111 if (ifp->carrier != LINK_DOWN &&
2112 state->new != NULL &&
2113 state->new->cookie == htonl(MAGIC_COOKIE))
2114 {
2115 syslog(LOG_INFO, "%s: releasing lease of %s",
2116 ifp->name, inet_ntoa(state->lease.addr));
2117 state->xid = dhcp_xid(ifp);
2118 send_message(ifp, DHCP_RELEASE, NULL);
2119 #ifdef RELEASE_SLOW
2120 /* Give the packet a chance to go */
2121 ts.tv_sec = RELEASE_DELAY_S;
2122 ts.tv_nsec = RELEASE_DELAY_NS;
2123 nanosleep(&ts, NULL);
2124 #endif
2125 }
2126 }
2127 free(state->old);
2128 state->old = state->new;
2129 state->new = NULL;
2130 state->reason = reason;
2131 ipv4_applyaddr(ifp);
2132 free(state->old);
2133 state->old = NULL;
2134 state->lease.addr.s_addr = 0;
2135 ifp->options->options &= ~ DHCPCD_CSR_WARNED;
2136 }
2137
2138 static void
2139 log_dhcp1(int lvl, const char *msg,
2140 const struct interface *iface, const struct dhcp_message *dhcp,
2141 const struct in_addr *from, int ad)
2142 {
2143 const char *tfrom;
2144 char *a;
2145 struct in_addr addr;
2146 int r;
2147
2148 if (strcmp(msg, "NAK:") == 0)
2149 a = get_option_string(iface->ctx, dhcp, DHO_MESSAGE);
2150 else if (ad && dhcp->yiaddr != 0) {
2151 addr.s_addr = dhcp->yiaddr;
2152 a = strdup(inet_ntoa(addr));
2153 if (a == NULL) {
2154 syslog(LOG_ERR, "%s: %m", __func__);
2155 return;
2156 }
2157 } else
2158 a = NULL;
2159
2160 tfrom = "from";
2161 r = get_option_addr(iface->ctx, &addr, dhcp, DHO_SERVERID);
2162 if (dhcp->servername[0] && r == 0) {
2163 if (a == NULL)
2164 syslog(lvl, "%s: %s %s %s `%s'", iface->name, msg,
2165 tfrom, inet_ntoa(addr), dhcp->servername);
2166 else
2167 syslog(lvl, "%s: %s %s %s %s `%s'", iface->name, msg, a,
2168 tfrom, inet_ntoa(addr), dhcp->servername);
2169 } else {
2170 if (r != 0) {
2171 tfrom = "via";
2172 addr = *from;
2173 }
2174 if (a == NULL)
2175 syslog(lvl, "%s: %s %s %s",
2176 iface->name, msg, tfrom, inet_ntoa(addr));
2177 else
2178 syslog(lvl, "%s: %s %s %s %s",
2179 iface->name, msg, a, tfrom, inet_ntoa(addr));
2180 }
2181 free(a);
2182 }
2183
2184 static void
2185 log_dhcp(int lvl, const char *msg,
2186 const struct interface *iface, const struct dhcp_message *dhcp,
2187 const struct in_addr *from)
2188 {
2189
2190 log_dhcp1(lvl, msg, iface, dhcp, from, 1);
2191 }
2192
2193 static int
2194 blacklisted_ip(const struct if_options *ifo, in_addr_t addr)
2195 {
2196 size_t i;
2197
2198 for (i = 0; i < ifo->blacklist_len; i += 2)
2199 if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1]))
2200 return 1;
2201 return 0;
2202 }
2203
2204 static int
2205 whitelisted_ip(const struct if_options *ifo, in_addr_t addr)
2206 {
2207 size_t i;
2208
2209 if (ifo->whitelist_len == 0)
2210 return -1;
2211 for (i = 0; i < ifo->whitelist_len; i += 2)
2212 if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1]))
2213 return 1;
2214 return 0;
2215 }
2216
2217 static void
2218 dhcp_handledhcp(struct interface *iface, struct dhcp_message **dhcpp,
2219 const struct in_addr *from)
2220 {
2221 struct dhcp_state *state = D_STATE(iface);
2222 struct if_options *ifo = iface->options;
2223 struct dhcp_message *dhcp = *dhcpp;
2224 struct dhcp_lease *lease = &state->lease;
2225 uint8_t type, tmp;
2226 const uint8_t *auth;
2227 struct in_addr addr;
2228 unsigned int i;
2229 size_t auth_len;
2230 char *msg;
2231
2232 /* We may have found a BOOTP server */
2233 if (get_option_uint8(iface->ctx, &type, dhcp, DHO_MESSAGETYPE) == -1)
2234 type = 0;
2235
2236 /* Authenticate the message */
2237 auth = get_option(iface->ctx, dhcp, DHO_AUTHENTICATION, &auth_len);
2238 if (auth) {
2239 if (dhcp_auth_validate(&state->auth, &ifo->auth,
2240 (uint8_t *)*dhcpp, sizeof(**dhcpp), 4, type,
2241 auth, auth_len) == NULL)
2242 {
2243 syslog(LOG_DEBUG, "%s: dhcp_auth_validate: %m",
2244 iface->name);
2245 log_dhcp1(LOG_ERR, "authentication failed",
2246 iface, dhcp, from, 0);
2247 return;
2248 }
2249 if (state->auth.token)
2250 syslog(LOG_DEBUG, "%s: validated using 0x%08" PRIu32,
2251 iface->name, state->auth.token->secretid);
2252 else
2253 syslog(LOG_DEBUG, "%s: accepted reconfigure key",
2254 iface->name);
2255 } else if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) {
2256 log_dhcp1(LOG_ERR, "no authentication", iface, dhcp, from, 0);
2257 return;
2258 } else if (ifo->auth.options & DHCPCD_AUTH_SEND)
2259 log_dhcp1(LOG_WARNING, "no authentication",
2260 iface, dhcp, from, 0);
2261
2262 /* RFC 3203 */
2263 if (type == DHCP_FORCERENEW) {
2264 if (from->s_addr == INADDR_ANY ||
2265 from->s_addr == INADDR_BROADCAST)
2266 {
2267 log_dhcp(LOG_ERR, "discarding Force Renew",
2268 iface, dhcp, from);
2269 return;
2270 }
2271 if (auth == NULL) {
2272 log_dhcp(LOG_ERR, "unauthenticated Force Renew",
2273 iface, dhcp, from);
2274 return;
2275 }
2276 if (state->state != DHS_BOUND && state->state != DHS_INFORM) {
2277 log_dhcp(LOG_DEBUG, "not bound, ignoring Force Renew",
2278 iface, dhcp, from);
2279 return;
2280 }
2281 log_dhcp(LOG_ERR, "Force Renew from", iface, dhcp, from);
2282 /* The rebind and expire timings are still the same, we just
2283 * enter the renew state early */
2284 if (state->state == DHS_BOUND) {
2285 eloop_timeout_delete(iface->ctx->eloop,
2286 dhcp_renew, iface);
2287 dhcp_renew(iface);
2288 } else {
2289 eloop_timeout_delete(iface->ctx->eloop,
2290 send_inform, iface);
2291 dhcp_inform(iface);
2292 }
2293 return;
2294 }
2295
2296 if (state->state == DHS_BOUND) {
2297 /* Before we supported FORCERENEW we closed off the raw
2298 * port so we effectively ignored all messages.
2299 * As such we'll not log by default here. */
2300 //log_dhcp(LOG_DEBUG, "bound, ignoring", iface, dhcp, from);
2301 return;
2302 }
2303
2304 /* Ensure it's the right transaction */
2305 if (state->xid != ntohl(dhcp->xid)) {
2306 syslog(LOG_DEBUG,
2307 "%s: wrong xid 0x%x (expecting 0x%x) from %s",
2308 iface->name, ntohl(dhcp->xid), state->xid,
2309 inet_ntoa(*from));
2310 return;
2311 }
2312 /* reset the message counter */
2313 state->interval = 0;
2314
2315 if (type == DHCP_NAK) {
2316 /* For NAK, only check if we require the ServerID */
2317 if (has_option_mask(ifo->requiremask, DHO_SERVERID) &&
2318 get_option_addr(iface->ctx, &addr, dhcp, DHO_SERVERID) == -1)
2319 {
2320 log_dhcp(LOG_WARNING, "reject NAK", iface, dhcp, from);
2321 return;
2322 }
2323
2324 /* We should restart on a NAK */
2325 log_dhcp(LOG_WARNING, "NAK:", iface, dhcp, from);
2326 if ((msg = get_option_string(iface->ctx, dhcp, DHO_MESSAGE))) {
2327 syslog(LOG_WARNING, "%s: message: %s",
2328 iface->name, msg);
2329 free(msg);
2330 }
2331 if (!(iface->ctx->options & DHCPCD_TEST)) {
2332 dhcp_drop(iface, "NAK");
2333 unlink(state->leasefile);
2334 }
2335
2336 /* If we constantly get NAKS then we should slowly back off */
2337 eloop_timeout_add_sec(iface->ctx->eloop,
2338 state->nakoff, dhcp_discover, iface);
2339 if (state->nakoff == 0)
2340 state->nakoff = 1;
2341 else {
2342 state->nakoff *= 2;
2343 if (state->nakoff > NAKOFF_MAX)
2344 state->nakoff = NAKOFF_MAX;
2345 }
2346 return;
2347 }
2348
2349 /* DHCP Auto-Configure, RFC 2563 */
2350 if (type == DHCP_OFFER && dhcp->yiaddr == 0) {
2351 log_dhcp(LOG_WARNING, "no address given", iface, dhcp, from);
2352 if ((msg = get_option_string(iface->ctx, dhcp, DHO_MESSAGE))) {
2353 syslog(LOG_WARNING, "%s: message: %s",
2354 iface->name, msg);
2355 free(msg);
2356 }
2357 if (state->state == DHS_DISCOVER &&
2358 get_option_uint8(iface->ctx, &tmp, dhcp,
2359 DHO_AUTOCONFIGURE) == 0)
2360 {
2361 switch (tmp) {
2362 case 0:
2363 log_dhcp(LOG_WARNING, "IPv4LL disabled from",
2364 iface, dhcp, from);
2365 dhcp_close(iface);
2366 eloop_timeout_delete(iface->ctx->eloop,
2367 NULL, iface);
2368 eloop_timeout_add_sec(iface->ctx->eloop,
2369 DHCP_MAX, dhcp_discover,
2370 iface);
2371 break;
2372 case 1:
2373 log_dhcp(LOG_WARNING, "IPv4LL enabled from",
2374 iface, dhcp, from);
2375 eloop_timeout_delete(iface->ctx->eloop,
2376 NULL, iface);
2377 if (IN_LINKLOCAL(htonl(state->addr.s_addr)))
2378 eloop_timeout_add_sec(iface->ctx->eloop,
2379 DHCP_MAX, dhcp_discover, iface);
2380 else
2381 ipv4ll_start(iface);
2382 break;
2383 default:
2384 syslog(LOG_ERR,
2385 "%s: unknown auto configuration option %d",
2386 iface->name, tmp);
2387 break;
2388 }
2389 }
2390 return;
2391 }
2392
2393 /* Ensure that all required options are present */
2394 for (i = 1; i < 255; i++) {
2395 if (has_option_mask(ifo->requiremask, i) &&
2396 get_option_uint8(iface->ctx, &tmp, dhcp, (uint8_t)i) != 0)
2397 {
2398 /* If we are bootp, then ignore the need for serverid.
2399 * To ignore bootp, require dhcp_message_type. */
2400 if (type == 0 && i == DHO_SERVERID)
2401 continue;
2402 log_dhcp(LOG_WARNING, "reject DHCP", iface, dhcp, from);
2403 return;
2404 }
2405 }
2406
2407 /* Ensure that the address offered is valid */
2408 if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) &&
2409 (dhcp->ciaddr == INADDR_ANY || dhcp->ciaddr == INADDR_BROADCAST) &&
2410 (dhcp->yiaddr == INADDR_ANY || dhcp->yiaddr == INADDR_BROADCAST))
2411 {
2412 log_dhcp(LOG_WARNING, "reject invalid address",
2413 iface, dhcp, from);
2414 return;
2415 }
2416
2417 if ((type == 0 || type == DHCP_OFFER) &&
2418 state->state == DHS_DISCOVER)
2419 {
2420 lease->frominfo = 0;
2421 lease->addr.s_addr = dhcp->yiaddr;
2422 lease->cookie = dhcp->cookie;
2423 if (type == 0 ||
2424 get_option_addr(iface->ctx,
2425 &lease->server, dhcp, DHO_SERVERID) != 0)
2426 lease->server.s_addr = INADDR_ANY;
2427 log_dhcp(LOG_INFO, "offered", iface, dhcp, from);
2428 free(state->offer);
2429 state->offer = dhcp;
2430 *dhcpp = NULL;
2431 if (iface->ctx->options & DHCPCD_TEST) {
2432 free(state->old);
2433 state->old = state->new;
2434 state->new = state->offer;
2435 state->offer = NULL;
2436 state->reason = "TEST";
2437 script_runreason(iface, state->reason);
2438 eloop_exit(iface->ctx->eloop, EXIT_SUCCESS);
2439 return;
2440 }
2441 eloop_timeout_delete(iface->ctx->eloop, send_discover, iface);
2442 /* We don't request BOOTP addresses */
2443 if (type) {
2444 /* We used to ARP check here, but that seems to be in
2445 * violation of RFC2131 where it only describes
2446 * DECLINE after REQUEST.
2447 * It also seems that some MS DHCP servers actually
2448 * ignore DECLINE if no REQUEST, ie we decline a
2449 * DISCOVER. */
2450 dhcp_request(iface);
2451 return;
2452 }
2453 }
2454
2455 if (type) {
2456 if (type == DHCP_OFFER) {
2457 log_dhcp(LOG_WARNING, "ignoring offer of",
2458 iface, dhcp, from);
2459 return;
2460 }
2461
2462 /* We should only be dealing with acks */
2463 if (type != DHCP_ACK) {
2464 log_dhcp(LOG_ERR, "not ACK or OFFER",
2465 iface, dhcp, from);
2466 return;
2467 }
2468
2469 if (!(ifo->options & DHCPCD_INFORM))
2470 log_dhcp(LOG_DEBUG, "acknowledged", iface, dhcp, from);
2471 else
2472 ifo->options &= ~DHCPCD_STATIC;
2473 }
2474
2475
2476 /* No NAK, so reset the backoff
2477 * We don't reset on an OFFER message because the server could
2478 * potentially NAK the REQUEST. */
2479 state->nakoff = 0;
2480
2481 /* BOOTP could have already assigned this above, so check we still
2482 * have a pointer. */
2483 if (*dhcpp) {
2484 free(state->offer);
2485 state->offer = dhcp;
2486 *dhcpp = NULL;
2487 }
2488
2489 lease->frominfo = 0;
2490 eloop_timeout_delete(iface->ctx->eloop, NULL, iface);
2491
2492 if (ifo->options & DHCPCD_ARP &&
2493 state->addr.s_addr != state->offer->yiaddr)
2494 {
2495 /* If the interface already has the address configured
2496 * then we can't ARP for duplicate detection. */
2497 addr.s_addr = state->offer->yiaddr;
2498 if (!ipv4_findaddr(iface, &addr, NULL)) {
2499 state->claims = 0;
2500 state->probes = 0;
2501 state->conflicts = 0;
2502 arp_probe(iface);
2503 return;
2504 }
2505 }
2506
2507 dhcp_bind(iface);
2508 }
2509
2510 static size_t
2511 get_udp_data(const uint8_t **data, const uint8_t *udp)
2512 {
2513 struct udp_dhcp_packet p;
2514
2515 memcpy(&p, udp, sizeof(p));
2516 *data = udp + offsetof(struct udp_dhcp_packet, dhcp);
2517 return ntohs(p.ip.ip_len) - sizeof(p.ip) - sizeof(p.udp);
2518 }
2519
2520 static int
2521 valid_udp_packet(const uint8_t *data, size_t data_len, struct in_addr *from,
2522 int noudpcsum)
2523 {
2524 struct udp_dhcp_packet p;
2525 uint16_t bytes, udpsum;
2526
2527 if (data_len < sizeof(p.ip)) {
2528 if (from)
2529 from->s_addr = INADDR_ANY;
2530 errno = EINVAL;
2531 return -1;
2532 }
2533 memcpy(&p, data, MIN(data_len, sizeof(p)));
2534 if (from)
2535 from->s_addr = p.ip.ip_src.s_addr;
2536 if (data_len > sizeof(p)) {
2537 errno = EINVAL;
2538 return -1;
2539 }
2540 if (checksum(&p.ip, sizeof(p.ip)) != 0) {
2541 errno = EINVAL;
2542 return -1;
2543 }
2544
2545 bytes = ntohs(p.ip.ip_len);
2546 if (data_len < bytes) {
2547 errno = EINVAL;
2548 return -1;
2549 }
2550
2551 if (noudpcsum == 0) {
2552 udpsum = p.udp.uh_sum;
2553 p.udp.uh_sum = 0;
2554 p.ip.ip_hl = 0;
2555 p.ip.ip_v = 0;
2556 p.ip.ip_tos = 0;
2557 p.ip.ip_len = p.udp.uh_ulen;
2558 p.ip.ip_id = 0;
2559 p.ip.ip_off = 0;
2560 p.ip.ip_ttl = 0;
2561 p.ip.ip_sum = 0;
2562 if (udpsum && checksum(&p, bytes) != udpsum) {
2563 errno = EINVAL;
2564 return -1;
2565 }
2566 }
2567
2568 return 0;
2569 }
2570
2571 static void
2572 dhcp_handlepacket(void *arg)
2573 {
2574 struct interface *ifp = arg;
2575 struct dhcp_message *dhcp = NULL;
2576 const uint8_t *pp;
2577 size_t bytes;
2578 struct in_addr from;
2579 int i, flags;
2580 const struct dhcp_state *state = D_CSTATE(ifp);
2581
2582 /* Need this API due to BPF */
2583 flags = 0;
2584 while (!(flags & RAW_EOF)) {
2585 bytes = (size_t)if_readrawpacket(ifp, ETHERTYPE_IP,
2586 ifp->ctx->packet, udp_dhcp_len, &flags);
2587 if (bytes == 0 || (ssize_t)bytes == -1) {
2588 syslog(LOG_ERR, "%s: dhcp if_readrawpacket: %m",
2589 ifp->name);
2590 dhcp_close(ifp);
2591 break;
2592 }
2593 if (valid_udp_packet(ifp->ctx->packet, bytes,
2594 &from, flags & RAW_PARTIALCSUM) == -1)
2595 {
2596 syslog(LOG_ERR, "%s: invalid UDP packet from %s",
2597 ifp->name, inet_ntoa(from));
2598 continue;
2599 }
2600 i = whitelisted_ip(ifp->options, from.s_addr);
2601 if (i == 0) {
2602 syslog(LOG_WARNING,
2603 "%s: non whitelisted DHCP packet from %s",
2604 ifp->name, inet_ntoa(from));
2605 continue;
2606 } else if (i != 1 &&
2607 blacklisted_ip(ifp->options, from.s_addr) == 1)
2608 {
2609 syslog(LOG_WARNING,
2610 "%s: blacklisted DHCP packet from %s",
2611 ifp->name, inet_ntoa(from));
2612 continue;
2613 }
2614 if (ifp->flags & IFF_POINTOPOINT &&
2615 state->dst.s_addr != from.s_addr)
2616 {
2617 syslog(LOG_WARNING,
2618 "%s: server %s is not destination",
2619 ifp->name, inet_ntoa(from));
2620 }
2621 bytes = get_udp_data(&pp, ifp->ctx->packet);
2622 if (bytes > sizeof(*dhcp)) {
2623 syslog(LOG_ERR,
2624 "%s: packet greater than DHCP size from %s",
2625 ifp->name, inet_ntoa(from));
2626 continue;
2627 }
2628 if (dhcp == NULL) {
2629 dhcp = calloc(1, sizeof(*dhcp));
2630 if (dhcp == NULL) {
2631 syslog(LOG_ERR, "%s: calloc: %m", __func__);
2632 break;
2633 }
2634 }
2635 memcpy(dhcp, pp, bytes);
2636 if (dhcp->cookie != htonl(MAGIC_COOKIE)) {
2637 syslog(LOG_DEBUG, "%s: bogus cookie from %s",
2638 ifp->name, inet_ntoa(from));
2639 continue;
2640 }
2641 /* Ensure packet is for us */
2642 if (ifp->hwlen <= sizeof(dhcp->chaddr) &&
2643 memcmp(dhcp->chaddr, ifp->hwaddr, ifp->hwlen))
2644 {
2645 char buf[sizeof(dhcp->chaddr) * 3];
2646
2647 syslog(LOG_DEBUG, "%s: xid 0x%x is not for hwaddr %s",
2648 ifp->name, ntohl(dhcp->xid),
2649 hwaddr_ntoa(dhcp->chaddr, sizeof(dhcp->chaddr),
2650 buf, sizeof(buf)));
2651 continue;
2652 }
2653 dhcp_handledhcp(ifp, &dhcp, &from);
2654 if (state->raw_fd == -1)
2655 break;
2656 }
2657 free(dhcp);
2658 }
2659
2660 static void
2661 dhcp_handleudp1(struct dhcpcd_ctx *ctx, int *fd, const char *ifname)
2662 {
2663 uint8_t buffer[sizeof(struct dhcp_message)];
2664
2665 /* Just read what's in the UDP fd and discard it as we always read
2666 * from the raw fd */
2667 if (read(*fd, buffer, sizeof(buffer)) == -1) {
2668 syslog(LOG_ERR, "%s: %s: %m", ifname, __func__);
2669 eloop_event_delete(ctx->eloop, *fd);
2670 close(*fd);
2671 *fd = -1;
2672 }
2673 }
2674
2675 static void
2676 dhcp_handleudp(void *arg)
2677 {
2678 struct dhcpcd_ctx *ctx;
2679
2680 ctx = arg;
2681 dhcp_handleudp1(arg, &ctx->udp_fd, NULL);
2682 }
2683
2684 static void
2685 dhcp_handleifudp(void *arg)
2686 {
2687 const struct interface *ifp;
2688 struct dhcp_state *state;
2689
2690 ifp = arg;
2691 state = D_STATE(ifp);
2692 dhcp_handleudp1(ifp->ctx, &state->udp_fd, ifp->name);
2693 }
2694
2695 static int
2696 dhcp_open(struct interface *ifp)
2697 {
2698 struct dhcp_state *state;
2699
2700 if (ifp->ctx->packet == NULL) {
2701 ifp->ctx->packet = malloc(udp_dhcp_len);
2702 if (ifp->ctx->packet == NULL) {
2703 syslog(LOG_ERR, "%s: %m", __func__);
2704 return -1;
2705 }
2706 }
2707
2708 state = D_STATE(ifp);
2709 if (state->raw_fd == -1) {
2710 state->raw_fd = if_openrawsocket(ifp, ETHERTYPE_IP);
2711 if (state->raw_fd == -1) {
2712 syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
2713 return -1;
2714 }
2715 eloop_event_add(ifp->ctx->eloop,
2716 state->raw_fd, dhcp_handlepacket, ifp);
2717 }
2718 if (state->udp_fd == -1 &&
2719 state->addr.s_addr != 0 &&
2720 state->new != NULL &&
2721 (state->new->cookie == htonl(MAGIC_COOKIE) ||
2722 ifp->options->options & DHCPCD_INFORM))
2723 {
2724 if (dhcp_openudp(ifp->ctx, ifp) == -1 && errno != EADDRINUSE) {
2725 syslog(LOG_ERR, "%s: dhcp_openudp: %m", ifp->name);
2726 return -1;
2727 }
2728 if (state->udp_fd != -1)
2729 eloop_event_add(ifp->ctx->eloop,
2730 state->udp_fd, dhcp_handleifudp, ifp);
2731 }
2732 return 0;
2733 }
2734
2735 int
2736 dhcp_dump(struct interface *ifp)
2737 {
2738 struct dhcp_state *state;
2739
2740 ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state));
2741 if (state == NULL)
2742 goto eexit;
2743 snprintf(state->leasefile, sizeof(state->leasefile),
2744 LEASEFILE, ifp->name);
2745 state->new = read_lease(ifp);
2746 if (state->new == NULL && errno == ENOENT) {
2747 strlcpy(state->leasefile, ifp->name, sizeof(state->leasefile));
2748 state->new = read_lease(ifp);
2749 }
2750 if (state->new == NULL) {
2751 if (errno == ENOENT)
2752 syslog(LOG_ERR, "%s: no lease to dump", ifp->name);
2753 return -1;
2754 }
2755 state->reason = "DUMP";
2756 return script_runreason(ifp, state->reason);
2757
2758 eexit:
2759 syslog(LOG_ERR, "%s: %m", __func__);
2760 return -1;
2761 }
2762
2763 void
2764 dhcp_free(struct interface *ifp)
2765 {
2766 struct dhcp_state *state = D_STATE(ifp);
2767 struct dhcpcd_ctx *ctx;
2768
2769 if (state) {
2770 free(state->old);
2771 free(state->new);
2772 free(state->offer);
2773 free(state->buffer);
2774 free(state->clientid);
2775 free(state);
2776 ifp->if_data[IF_DATA_DHCP] = NULL;
2777 }
2778
2779 ctx = ifp->ctx;
2780 /* If we don't have any more DHCP enabled interfaces,
2781 * close the global socket and release resources */
2782 if (ctx->ifaces) {
2783 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
2784 if (D_STATE(ifp))
2785 break;
2786 }
2787 }
2788 if (ifp == NULL) {
2789 if (ctx->udp_fd != -1) {
2790 eloop_event_delete(ctx->eloop, ctx->udp_fd);
2791 close(ctx->udp_fd);
2792 ctx->udp_fd = -1;
2793 }
2794
2795 free(ctx->packet);
2796 free(ctx->opt_buffer);
2797 ctx->packet = NULL;
2798 ctx->opt_buffer = NULL;
2799 }
2800 }
2801
2802 static int
2803 dhcp_init(struct interface *ifp)
2804 {
2805 struct dhcp_state *state;
2806 const struct if_options *ifo;
2807 uint8_t len;
2808 char buf[(sizeof(ifo->clientid) - 1) * 3];
2809
2810 state = D_STATE(ifp);
2811 if (state == NULL) {
2812 ifp->if_data[IF_DATA_DHCP] = calloc(1, sizeof(*state));
2813 state = D_STATE(ifp);
2814 if (state == NULL)
2815 return -1;
2816 /* 0 is a valid fd, so init to -1 */
2817 state->raw_fd = state->udp_fd = state->arp_fd = -1;
2818 }
2819
2820 state->state = DHS_INIT;
2821 state->reason = "PREINIT";
2822 state->nakoff = 0;
2823 snprintf(state->leasefile, sizeof(state->leasefile),
2824 LEASEFILE, ifp->name);
2825
2826 ifo = ifp->options;
2827 /* We need to drop the leasefile so that dhcp_start
2828 * doesn't load it. */
2829 if (ifo->options & DHCPCD_REQUEST)
2830 unlink(state->leasefile);
2831
2832 free(state->clientid);
2833 state->clientid = NULL;
2834
2835 if (*ifo->clientid) {
2836 state->clientid = malloc((size_t)(ifo->clientid[0] + 1));
2837 if (state->clientid == NULL)
2838 goto eexit;
2839 memcpy(state->clientid, ifo->clientid,
2840 (size_t)(ifo->clientid[0]) + 1);
2841 } else if (ifo->options & DHCPCD_CLIENTID) {
2842 if (ifo->options & DHCPCD_DUID) {
2843 state->clientid = malloc(ifp->ctx->duid_len + 6);
2844 if (state->clientid == NULL)
2845 goto eexit;
2846 state->clientid[0] =(uint8_t)(ifp->ctx->duid_len + 5);
2847 state->clientid[1] = 255; /* RFC 4361 */
2848 memcpy(state->clientid + 2, ifo->iaid, 4);
2849 memcpy(state->clientid + 6, ifp->ctx->duid,
2850 ifp->ctx->duid_len);
2851 } else {
2852 len = (uint8_t)(ifp->hwlen + 1);
2853 state->clientid = malloc((size_t)len + 1);
2854 if (state->clientid == NULL)
2855 goto eexit;
2856 state->clientid[0] = len;
2857 state->clientid[1] = (uint8_t)ifp->family;
2858 memcpy(state->clientid + 2, ifp->hwaddr,
2859 ifp->hwlen);
2860 }
2861 }
2862
2863 if (ifo->options & DHCPCD_DUID)
2864 /* Don't bother logging as DUID and IAID are reported
2865 * at device start. */
2866 return 0;
2867
2868 if (ifo->options & DHCPCD_CLIENTID)
2869 syslog(LOG_DEBUG, "%s: using ClientID %s", ifp->name,
2870 hwaddr_ntoa(state->clientid + 1, state->clientid[0],
2871 buf, sizeof(buf)));
2872 else if (ifp->hwlen)
2873 syslog(LOG_DEBUG, "%s: using hwaddr %s", ifp->name,
2874 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf)));
2875 return 0;
2876
2877 eexit:
2878 syslog(LOG_ERR, "%s: Error making ClientID: %m", __func__);
2879 return -1;
2880 }
2881
2882 static void
2883 dhcp_start1(void *arg)
2884 {
2885 struct interface *ifp = arg;
2886 struct if_options *ifo = ifp->options;
2887 struct dhcp_state *state;
2888 struct stat st;
2889 struct timeval now;
2890 uint32_t l;
2891 int nolease;
2892
2893 if (!(ifo->options & DHCPCD_IPV4))
2894 return;
2895
2896 /* Listen on *.*.*.*:bootpc so that the kernel never sends an
2897 * ICMP port unreachable message back to the DHCP server */
2898 if (ifp->ctx->udp_fd == -1 && dhcp_openudp(ifp->ctx, NULL) != -1)
2899 eloop_event_add(ifp->ctx->eloop,
2900 ifp->ctx->udp_fd, dhcp_handleudp, ifp->ctx);
2901
2902 if (dhcp_init(ifp) == -1) {
2903 syslog(LOG_ERR, "%s: dhcp_init: %m", ifp->name);
2904 return;
2905 }
2906
2907 /* Close any pre-existing sockets as we're starting over */
2908 dhcp_close(ifp);
2909
2910 state = D_STATE(ifp);
2911 state->start_uptime = uptime();
2912 free(state->offer);
2913 state->offer = NULL;
2914
2915 if (state->arping_index < ifo->arping_len) {
2916 arp_start(ifp);
2917 return;
2918 }
2919
2920 if (ifo->options & DHCPCD_STATIC) {
2921 dhcp_static(ifp);
2922 return;
2923 }
2924
2925 if (ifo->options & DHCPCD_DHCP && dhcp_open(ifp) == -1)
2926 return;
2927
2928 if (ifo->options & DHCPCD_INFORM) {
2929 dhcp_inform(ifp);
2930 return;
2931 }
2932 if (ifp->hwlen == 0 && ifo->clientid[0] == '\0') {
2933 syslog(LOG_WARNING, "%s: needs a clientid to configure",
2934 ifp->name);
2935 dhcp_drop(ifp, "FAIL");
2936 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
2937 return;
2938 }
2939 /* We don't want to read the old lease if we NAK an old test */
2940 nolease = state->offer && ifp->ctx->options & DHCPCD_TEST;
2941 if (!nolease)
2942 state->offer = read_lease(ifp);
2943 if (state->offer) {
2944 get_lease(ifp->ctx, &state->lease, state->offer);
2945 state->lease.frominfo = 1;
2946 if (state->offer->cookie == 0) {
2947 if (state->offer->yiaddr == state->addr.s_addr) {
2948 free(state->offer);
2949 state->offer = NULL;
2950 }
2951 } else if (state->lease.leasetime != ~0U &&
2952 stat(state->leasefile, &st) == 0)
2953 {
2954 /* Offset lease times and check expiry */
2955 gettimeofday(&now, NULL);
2956 if ((time_t)state->lease.leasetime <
2957 now.tv_sec - st.st_mtime)
2958 {
2959 syslog(LOG_DEBUG,
2960 "%s: discarding expired lease",
2961 ifp->name);
2962 free(state->offer);
2963 state->offer = NULL;
2964 state->lease.addr.s_addr = 0;
2965 } else {
2966 l = (uint32_t)(now.tv_sec - st.st_mtime);
2967 state->lease.leasetime -= l;
2968 state->lease.renewaltime -= l;
2969 state->lease.rebindtime -= l;
2970 }
2971 }
2972 }
2973
2974 if (!(ifo->options & DHCPCD_DHCP)) {
2975 if (ifo->options & DHCPCD_IPV4LL) {
2976 if (state->offer && state->offer->cookie != 0) {
2977 free(state->offer);
2978 state->offer = NULL;
2979 }
2980 ipv4ll_start(ifp);
2981 }
2982 return;
2983 }
2984
2985 if (state->offer == NULL)
2986 dhcp_discover(ifp);
2987 else if (state->offer->cookie == 0 && ifo->options & DHCPCD_IPV4LL)
2988 ipv4ll_start(ifp);
2989 else
2990 dhcp_reboot(ifp);
2991 }
2992
2993 void
2994 dhcp_start(struct interface *ifp)
2995 {
2996 struct timeval tv;
2997
2998 if (!(ifp->options->options & DHCPCD_IPV4))
2999 return;
3000
3001 /* No point in delaying a static configuration */
3002 if (ifp->options->options & DHCPCD_STATIC &&
3003 !(ifp->options->options & DHCPCD_INFORM))
3004 {
3005 tv.tv_sec = 0;
3006 tv.tv_usec = 0;
3007 } else {
3008 tv.tv_sec = DHCP_MIN_DELAY;
3009 tv.tv_usec = (suseconds_t)arc4random_uniform(
3010 (DHCP_MAX_DELAY - DHCP_MIN_DELAY) * 1000000);
3011 timernorm(&tv);
3012 syslog(LOG_DEBUG,
3013 "%s: delaying DHCP for %0.1f seconds",
3014 ifp->name, timeval_to_double(&tv));
3015 }
3016
3017 eloop_timeout_add_tv(ifp->ctx->eloop, &tv, dhcp_start1, ifp);
3018 }
3019
3020 void
3021 dhcp_handleifa(int type, struct interface *ifp,
3022 const struct in_addr *addr,
3023 const struct in_addr *net,
3024 const struct in_addr *dst)
3025 {
3026 struct dhcp_state *state;
3027 struct if_options *ifo;
3028 uint8_t i;
3029
3030 state = D_STATE(ifp);
3031 if (state == NULL)
3032 return;
3033
3034 if (type == RTM_DELADDR) {
3035 if (state->new &&
3036 (state->new->yiaddr == addr->s_addr ||
3037 (state->new->yiaddr == INADDR_ANY &&
3038 state->new->ciaddr == addr->s_addr)))
3039 {
3040 syslog(LOG_INFO, "%s: removing IP address %s/%d",
3041 ifp->name, inet_ntoa(state->lease.addr),
3042 inet_ntocidr(state->lease.net));
3043 dhcp_drop(ifp, "EXPIRE");
3044 }
3045 return;
3046 }
3047
3048 if (type != RTM_NEWADDR)
3049 return;
3050
3051 ifo = ifp->options;
3052 if (ifo->options & DHCPCD_INFORM) {
3053 if (state->state != DHS_INFORM)
3054 dhcp_inform(ifp);
3055 return;
3056 }
3057
3058 if (!(ifo->options & DHCPCD_STATIC))
3059 return;
3060 if (ifo->req_addr.s_addr != INADDR_ANY)
3061 return;
3062
3063 free(state->old);
3064 state->old = state->new;
3065 state->new = dhcp_message_new(addr, net);
3066 if (state->new == NULL)
3067 return;
3068 state->dst.s_addr = dst ? dst->s_addr : INADDR_ANY;
3069 if (dst) {
3070 for (i = 1; i < 255; i++)
3071 if (i != DHO_ROUTER && has_option_mask(ifo->dstmask,i))
3072 dhcp_message_add_addr(state->new, i, *dst);
3073 }
3074 state->reason = "STATIC";
3075 ipv4_buildroutes(ifp->ctx);
3076 script_runreason(ifp, state->reason);
3077 if (ifo->options & DHCPCD_INFORM) {
3078 state->state = DHS_INFORM;
3079 state->xid = dhcp_xid(ifp);
3080 state->lease.server.s_addr = dst ? dst->s_addr : INADDR_ANY;
3081 state->addr = *addr;
3082 state->net = *net;
3083 dhcp_inform(ifp);
3084 }
3085 }