1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <net/route.h>
35 #include <netinet/if_ether.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
40 #include <netinet/udp.h>
44 # include <net/if_dl.h>
60 #define ELOOP_QUEUE ELOOP_DHCP
67 #include "dhcp-common.h"
78 #define DAD "Duplicate address detected"
79 #define DHCP_MIN_LEASE 20
81 #define IPV4A ADDRIPV4 | ARRAY
82 #define IPV4R ADDRIPV4 | REQUEST
84 /* We should define a maximum for the NAK exponential backoff */
88 #define IPDEFTTL 64 /* RFC1340 */
91 /* Support older systems with different defines */
92 #if !defined(IP_RECVPKTINFO) && defined(IP_PKTINFO)
93 #define IP_RECVPKTINFO IP_PKTINFO
96 /* Assert the correct structure size for on wire */
97 __CTASSERT(sizeof(struct ip
) == 20);
98 __CTASSERT(sizeof(struct udphdr
) == 8);
99 __CTASSERT(sizeof(struct bootp
) == 300);
106 static const struct dhcp_op dhcp_ops
[] = {
107 { DHCP_DISCOVER
, "DISCOVER" },
108 { DHCP_OFFER
, "OFFER" },
109 { DHCP_REQUEST
, "REQUEST" },
110 { DHCP_DECLINE
, "DECLINE" },
113 { DHCP_RELEASE
, "RELEASE" },
114 { DHCP_INFORM
, "INFORM" },
115 { DHCP_FORCERENEW
, "FORCERENEW"},
119 static const char * const dhcp_params
[] = {
128 static int dhcp_openbpf(struct interface
*);
129 static void dhcp_start1(void *);
130 #if defined(ARP) && (!defined(KERNEL_RFC5227) || defined(ARPING))
131 static void dhcp_arp_found(struct arp_state
*, const struct arp_msg
*);
133 static void dhcp_handledhcp(struct interface
*, struct bootp
*, size_t,
134 const struct in_addr
*);
135 static void dhcp_handleifudp(void *, unsigned short);
136 static int dhcp_initstate(struct interface
*);
139 dhcp_printoptions(const struct dhcpcd_ctx
*ctx
,
140 const struct dhcp_opt
*opts
, size_t opts_len
)
142 const char * const *p
;
144 const struct dhcp_opt
*opt
, *opt2
;
147 for (p
= dhcp_params
; *p
; p
++)
150 for (i
= 0, opt
= ctx
->dhcp_opts
; i
< ctx
->dhcp_opts_len
; i
++, opt
++) {
151 for (j
= 0, opt2
= opts
; j
< opts_len
; j
++, opt2
++)
152 if (opt
->option
== opt2
->option
)
155 cols
= printf("%03d %s", opt
->option
, opt
->var
);
156 dhcp_print_option_encoding(opt
, cols
);
159 for (i
= 0, opt
= opts
; i
< opts_len
; i
++, opt
++) {
160 cols
= printf("%03d %s", opt
->option
, opt
->var
);
161 dhcp_print_option_encoding(opt
, cols
);
165 static const uint8_t *
166 get_option(struct dhcpcd_ctx
*ctx
,
167 const struct bootp
*bootp
, size_t bootp_len
,
168 unsigned int opt
, size_t *opt_len
)
170 const uint8_t *p
, *e
;
171 uint8_t l
, o
, ol
, overl
, *bp
;
175 if (bootp
== NULL
|| bootp_len
< DHCP_MIN_LEN
) {
180 /* Check we have the magic cookie */
181 if (!IS_DHCP(bootp
)) {
186 p
= bootp
->vend
+ 4; /* options after the 4 byte cookie */
187 e
= (const uint8_t *)bootp
+ bootp_len
;
196 /* No length to read */
200 /* bit 1 set means parse boot file */
201 overl
= (uint8_t)(overl
& ~1);
203 e
= p
+ sizeof(bootp
->file
);
204 } else if (overl
& 2) {
205 /* bit 2 set means parse server name */
206 overl
= (uint8_t)(overl
& ~2);
208 e
= p
+ sizeof(bootp
->sname
);
211 /* No length to read */
215 /* Check we can read the length */
222 /* Check we can read the option data, if present */
228 if (o
== DHO_OPTSOVERLOADED
) {
229 /* Ensure we only get this option once by setting
230 * the last bit as well as the value.
231 * This is valid because only the first two bits
232 * actually mean anything in RFC2132 Section 9.3 */
233 if (l
== 1 && !overl
)
239 /* We must concatonate the options. */
240 if (bl
+ l
> ctx
->opt_buffer_len
) {
246 (bp
- ctx
->opt_buffer
);
249 nb
= realloc(ctx
->opt_buffer
, bl
+ l
);
252 ctx
->opt_buffer
= nb
;
253 ctx
->opt_buffer_len
= bl
+ l
;
254 bp
= ctx
->opt_buffer
+ pos
;
257 bp
= ctx
->opt_buffer
;
273 return (const uint8_t *)ctx
->opt_buffer
;
282 get_option_addr(struct dhcpcd_ctx
*ctx
,
283 struct in_addr
*a
, const struct bootp
*bootp
, size_t bootp_len
,
289 p
= get_option(ctx
, bootp
, bootp_len
, option
, &len
);
290 if (!p
|| len
< (ssize_t
)sizeof(a
->s_addr
))
292 memcpy(&a
->s_addr
, p
, sizeof(a
->s_addr
));
297 get_option_uint32(struct dhcpcd_ctx
*ctx
,
298 uint32_t *i
, const struct bootp
*bootp
, size_t bootp_len
, uint8_t option
)
304 p
= get_option(ctx
, bootp
, bootp_len
, option
, &len
);
305 if (!p
|| len
< (ssize_t
)sizeof(d
))
307 memcpy(&d
, p
, sizeof(d
));
314 get_option_uint16(struct dhcpcd_ctx
*ctx
,
315 uint16_t *i
, const struct bootp
*bootp
, size_t bootp_len
, uint8_t option
)
321 p
= get_option(ctx
, bootp
, bootp_len
, option
, &len
);
322 if (!p
|| len
< (ssize_t
)sizeof(d
))
324 memcpy(&d
, p
, sizeof(d
));
331 get_option_uint8(struct dhcpcd_ctx
*ctx
,
332 uint8_t *i
, const struct bootp
*bootp
, size_t bootp_len
, uint8_t option
)
337 p
= get_option(ctx
, bootp
, bootp_len
, option
, &len
);
338 if (!p
|| len
< (ssize_t
)sizeof(*p
))
346 print_rfc3442(FILE *fp
, const uint8_t *data
, size_t data_len
)
348 const uint8_t *p
= data
, *e
;
353 /* Minimum is 5 -first is CIDR and a router length of 4 */
362 if (fputc(' ', fp
) == EOF
)
370 ocets
= (size_t)(cidr
+ 7) / NBBY
;
371 if (p
+ 4 + ocets
> e
) {
375 /* If we have ocets then we have a destination and netmask */
378 memcpy(&addr
.s_addr
, p
, ocets
);
381 if (fprintf(fp
, "%s/%d", inet_ntoa(addr
), cidr
) == -1)
384 /* Finally, snag the router */
385 memcpy(&addr
.s_addr
, p
, 4);
387 if (fprintf(fp
, " %s", inet_ntoa(addr
)) == -1)
391 if (fputc('\0', fp
) == EOF
)
397 decode_rfc3442_rt(rb_tree_t
*routes
, struct interface
*ifp
,
398 const uint8_t *data
, size_t dl
)
400 const uint8_t *p
= data
;
404 struct rt
*rt
= NULL
;
405 struct in_addr dest
, netmask
, gateway
;
408 /* Minimum is 5 -first is CIDR and a router length of 4 */
423 ocets
= (size_t)(cidr
+ 7) / NBBY
;
424 if (p
+ 4 + ocets
> e
) {
429 if ((rt
= rt_new(ifp
)) == NULL
)
432 /* If we have ocets then we have a destination and netmask */
435 memcpy(&dest
.s_addr
, p
, ocets
);
437 netmask
.s_addr
= htonl(~0U << (32 - cidr
));
441 /* Finally, snag the router */
442 memcpy(&gateway
.s_addr
, p
, 4);
445 if (netmask
.s_addr
== INADDR_BROADCAST
)
446 rt
->rt_flags
= RTF_HOST
;
448 sa_in_init(&rt
->rt_dest
, &dest
);
449 sa_in_init(&rt
->rt_netmask
, &netmask
);
450 sa_in_init(&rt
->rt_gateway
, &gateway
);
451 if (rt_proto_add(routes
, rt
))
458 print_rfc3361(FILE *fp
, const uint8_t *data
, size_t dl
)
461 char sip
[NS_MAXDNAME
];
473 if (decode_rfc1035(sip
, sizeof(sip
), data
, dl
) == -1)
475 if (efprintf(fp
, "%s", sip
) == -1)
483 addr
.s_addr
= INADDR_BROADCAST
;
486 data
+= sizeof(addr
.s_addr
), dl
-= sizeof(addr
.s_addr
))
488 memcpy(&addr
.s_addr
, data
, sizeof(addr
.s_addr
));
489 if (fprintf(fp
, "%s", inet_ntoa(addr
)) == -1)
491 if (dl
!= sizeof(addr
.s_addr
)) {
492 if (fputc(' ', fp
) == EOF
)
496 if (fputc('\0', fp
) == EOF
)
508 get_option_string(struct dhcpcd_ctx
*ctx
,
509 const struct bootp
*bootp
, size_t bootp_len
, uint8_t option
)
515 p
= get_option(ctx
, bootp
, bootp_len
, option
, &len
);
516 if (!p
|| len
== 0 || *p
== '\0')
519 s
= malloc(sizeof(char) * (len
+ 1));
527 /* This calculates the netmask that we should use for static routes.
528 * This IS different from the calculation used to calculate the netmask
529 * for an interface address. */
531 route_netmask(uint32_t ip_in
)
533 /* used to be unsigned long - check if error */
534 uint32_t p
= ntohl(ip_in
);
556 /* We need to obey routing options.
557 * If we have a CSR then we only use that.
558 * Otherwise we add static routes and then routers. */
560 get_option_routes(rb_tree_t
*routes
, struct interface
*ifp
,
561 const struct bootp
*bootp
, size_t bootp_len
)
563 struct if_options
*ifo
= ifp
->options
;
566 struct rt
*rt
= NULL
;
567 struct in_addr dest
, netmask
, gateway
;
569 const char *csr
= "";
572 /* If we have CSR's then we MUST use these only */
573 if (!has_option_mask(ifo
->nomask
, DHO_CSR
))
574 p
= get_option(ifp
->ctx
, bootp
, bootp_len
, DHO_CSR
, &len
);
577 /* Check for crappy MS option */
578 if (!p
&& !has_option_mask(ifo
->nomask
, DHO_MSCSR
)) {
579 p
= get_option(ifp
->ctx
, bootp
, bootp_len
, DHO_MSCSR
, &len
);
583 if (p
&& (n
= decode_rfc3442_rt(routes
, ifp
, p
, len
)) != -1) {
584 const struct dhcp_state
*state
;
586 state
= D_CSTATE(ifp
);
587 if (!(ifo
->options
& DHCPCD_CSR_WARNED
) &&
588 !(state
->added
& STATE_FAKE
))
590 logdebugx("%s: using %sClassless Static Routes",
592 ifo
->options
|= DHCPCD_CSR_WARNED
;
598 /* OK, get our static routes first. */
599 if (!has_option_mask(ifo
->nomask
, DHO_STATICROUTE
))
600 p
= get_option(ifp
->ctx
, bootp
, bootp_len
,
601 DHO_STATICROUTE
, &len
);
604 /* RFC 2131 Section 5.8 states length MUST be in multiples of 8 */
605 if (p
&& len
% 8 == 0) {
608 memcpy(&dest
.s_addr
, p
, sizeof(dest
.s_addr
));
610 memcpy(&gateway
.s_addr
, p
, sizeof(gateway
.s_addr
));
612 /* RFC 2131 Section 5.8 states default route is
614 if (gateway
.s_addr
== INADDR_ANY
)
616 if ((rt
= rt_new(ifp
)) == NULL
)
619 /* A on-link host route is normally set by having the
620 * gateway match the destination or assigned address */
621 if (gateway
.s_addr
== dest
.s_addr
||
622 (gateway
.s_addr
== bootp
->yiaddr
||
623 gateway
.s_addr
== bootp
->ciaddr
))
625 gateway
.s_addr
= INADDR_ANY
;
626 netmask
.s_addr
= INADDR_BROADCAST
;
628 netmask
.s_addr
= route_netmask(dest
.s_addr
);
629 if (netmask
.s_addr
== INADDR_BROADCAST
)
630 rt
->rt_flags
= RTF_HOST
;
632 sa_in_init(&rt
->rt_dest
, &dest
);
633 sa_in_init(&rt
->rt_netmask
, &netmask
);
634 sa_in_init(&rt
->rt_gateway
, &gateway
);
635 if (rt_proto_add(routes
, rt
))
640 /* Now grab our routers */
641 if (!has_option_mask(ifo
->nomask
, DHO_ROUTER
))
642 p
= get_option(ifp
->ctx
, bootp
, bootp_len
, DHO_ROUTER
, &len
);
645 if (p
&& len
% 4 == 0) {
647 dest
.s_addr
= INADDR_ANY
;
648 netmask
.s_addr
= INADDR_ANY
;
650 if ((rt
= rt_new(ifp
)) == NULL
)
652 memcpy(&gateway
.s_addr
, p
, sizeof(gateway
.s_addr
));
654 sa_in_init(&rt
->rt_dest
, &dest
);
655 sa_in_init(&rt
->rt_netmask
, &netmask
);
656 sa_in_init(&rt
->rt_gateway
, &gateway
);
657 if (rt_proto_add(routes
, rt
))
666 dhcp_get_mtu(const struct interface
*ifp
)
668 const struct dhcp_state
*state
;
671 if (ifp
->options
->mtu
)
672 return (uint16_t)ifp
->options
->mtu
;
673 mtu
= 0; /* bogus gcc warning */
674 if ((state
= D_CSTATE(ifp
)) == NULL
||
675 has_option_mask(ifp
->options
->nomask
, DHO_MTU
) ||
676 get_option_uint16(ifp
->ctx
, &mtu
,
677 state
->new, state
->new_len
, DHO_MTU
) == -1)
682 /* Grab our routers from the DHCP message and apply any MTU value
683 * the message contains */
685 dhcp_get_routes(rb_tree_t
*routes
, struct interface
*ifp
)
687 const struct dhcp_state
*state
;
689 if ((state
= D_CSTATE(ifp
)) == NULL
|| !(state
->added
& STATE_ADDED
))
691 return get_option_routes(routes
, ifp
, state
->new, state
->new_len
);
694 /* Assumes DHCP options */
696 dhcp_message_add_addr(struct bootp
*bootp
,
697 uint8_t type
, struct in_addr addr
)
703 while (*p
!= DHO_END
) {
708 len
= (size_t)(p
- bootp
->vend
);
709 if (len
+ 6 > sizeof(bootp
->vend
)) {
716 memcpy(p
, &addr
.s_addr
, 4);
723 make_message(struct bootp
**bootpm
, const struct interface
*ifp
, uint8_t type
)
727 uint8_t *n_params
= NULL
;
731 const struct dhcp_opt
*opt
;
732 struct if_options
*ifo
= ifp
->options
;
733 const struct dhcp_state
*state
= D_CSTATE(ifp
);
734 const struct dhcp_lease
*lease
= &state
->lease
;
735 char hbuf
[HOSTNAME_MAX_LEN
+ 1];
736 const char *hostname
;
737 const struct vivco
*vivco
;
740 uint8_t *auth
, auth_len
;
743 if ((mtu
= if_getmtu(ifp
)) == -1)
744 logerr("%s: if_getmtu", ifp
->name
);
745 else if (mtu
< MTU_MIN
) {
746 if (if_setmtu(ifp
, MTU_MIN
) == -1)
747 logerr("%s: if_setmtu", ifp
->name
);
751 if (ifo
->options
& DHCPCD_BOOTP
)
752 bootp
= calloc(1, sizeof (*bootp
));
754 /* Make the maximal message we could send */
755 bootp
= calloc(1, (size_t)(mtu
- IP_UDP_SIZE
));
761 if (state
->addr
!= NULL
&&
762 (type
== DHCP_INFORM
|| type
== DHCP_RELEASE
||
763 (type
== DHCP_REQUEST
&&
764 state
->addr
->mask
.s_addr
== lease
->mask
.s_addr
&&
765 (state
->new == NULL
|| IS_DHCP(state
->new)) &&
766 !(state
->added
& (STATE_FAKE
| STATE_EXPIRED
)))))
767 bootp
->ciaddr
= state
->addr
->addr
.s_addr
;
769 bootp
->op
= BOOTREQUEST
;
770 bootp
->htype
= (uint8_t)ifp
->hwtype
;
771 if (ifp
->hwlen
!= 0 && ifp
->hwlen
<= sizeof(bootp
->chaddr
)) {
772 bootp
->hlen
= (uint8_t)ifp
->hwlen
;
773 memcpy(&bootp
->chaddr
, &ifp
->hwaddr
, ifp
->hwlen
);
776 if (ifo
->options
& DHCPCD_BROADCAST
&&
777 bootp
->ciaddr
== 0 &&
778 type
!= DHCP_DECLINE
&&
779 type
!= DHCP_RELEASE
)
780 bootp
->flags
= htons(BROADCAST_FLAG
);
782 if (type
!= DHCP_DECLINE
&& type
!= DHCP_RELEASE
) {
784 unsigned long long secs
;
786 clock_gettime(CLOCK_MONOTONIC
, &tv
);
787 secs
= eloop_timespec_diff(&tv
, &state
->started
, NULL
);
788 if (secs
> UINT16_MAX
)
789 bootp
->secs
= htons((uint16_t)UINT16_MAX
);
791 bootp
->secs
= htons((uint16_t)secs
);
794 bootp
->xid
= htonl(state
->xid
);
796 if (ifo
->options
& DHCPCD_BOOTP
)
797 return sizeof(*bootp
);
800 e
= (uint8_t *)bootp
+ (mtu
- IP_UDP_SIZE
) - 1; /* -1 for DHO_END */
802 ul
= htonl(MAGIC_COOKIE
);
803 memcpy(p
, &ul
, sizeof(ul
));
806 #define AREA_LEFT (size_t)(e - p)
807 #define AREA_FIT(s) if ((s) > AREA_LEFT) goto toobig
808 #define AREA_CHECK(s) if ((s) + 2UL > AREA_LEFT) goto toobig
809 #define PUT_ADDR(o, a) do { \
813 memcpy(p, &(a)->s_addr, 4); \
815 } while (0 /* CONSTCOND */)
817 /* Options are listed in numerical order as per RFC 7844 Section 3.1
818 * XXX: They should be randomised. */
821 if (lease
->addr
.s_addr
&& lease
->cookie
== htonl(MAGIC_COOKIE
)) {
822 if (type
== DHCP_DECLINE
||
823 (type
== DHCP_REQUEST
&&
824 (state
->addr
== NULL
||
825 state
->added
& (STATE_FAKE
| STATE_EXPIRED
) ||
826 lease
->addr
.s_addr
!= state
->addr
->addr
.s_addr
)))
829 PUT_ADDR(DHO_IPADDRESS
, &lease
->addr
);
834 *p
++ = DHO_MESSAGETYPE
;
838 if (lease
->addr
.s_addr
&& lease
->cookie
== htonl(MAGIC_COOKIE
)) {
839 if (type
== DHCP_RELEASE
|| putip
) {
840 if (lease
->server
.s_addr
)
841 PUT_ADDR(DHO_SERVERID
, &lease
->server
);
845 if (type
== DHCP_DECLINE
) {
847 if (len
> AREA_LEFT
) {
855 #define DHCP_DIR(type) ((type) == DHCP_DISCOVER || (type) == DHCP_INFORM || \
856 (type) == DHCP_REQUEST)
858 if (DHCP_DIR(type
)) {
859 /* vendor is already encoded correctly, so just add it */
860 if (ifo
->vendor
[0]) {
861 AREA_CHECK(ifo
->vendor
[0]);
863 memcpy(p
, ifo
->vendor
, (size_t)ifo
->vendor
[0] + 1);
864 p
+= ifo
->vendor
[0] + 1;
868 if (type
== DHCP_DISCOVER
&& ifo
->options
& DHCPCD_REQUEST
)
869 PUT_ADDR(DHO_IPADDRESS
, &ifo
->req_addr
);
871 if (DHCP_DIR(type
)) {
872 if (type
!= DHCP_INFORM
) {
873 if (ifo
->leasetime
!= 0) {
875 *p
++ = DHO_LEASETIME
;
877 ul
= htonl(ifo
->leasetime
);
884 *p
++ = DHO_PARAMETERREQUESTLIST
;
887 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
888 i
< ifp
->ctx
->dhcp_opts_len
;
891 if (!DHC_REQOPT(opt
, ifo
->requestmask
, ifo
->nomask
))
893 if (type
== DHCP_INFORM
&&
894 (opt
->option
== DHO_RENEWALTIME
||
895 opt
->option
== DHO_REBINDTIME
))
898 *p
++ = (uint8_t)opt
->option
;
900 for (i
= 0, opt
= ifo
->dhcp_override
;
901 i
< ifo
->dhcp_override_len
;
904 /* Check if added above */
905 for (lp
= n_params
+ 1; lp
< p
; lp
++)
906 if (*lp
== (uint8_t)opt
->option
)
910 if (!DHC_REQOPT(opt
, ifo
->requestmask
, ifo
->nomask
))
912 if (type
== DHCP_INFORM
&&
913 (opt
->option
== DHO_RENEWALTIME
||
914 opt
->option
== DHO_REBINDTIME
))
917 *p
++ = (uint8_t)opt
->option
;
919 *n_params
= (uint8_t)(p
- n_params
- 1);
922 !(has_option_mask(ifo
->nomask
, DHO_MAXMESSAGESIZE
)))
925 *p
++ = DHO_MAXMESSAGESIZE
;
927 sz
= htons((uint16_t)(mtu
- IP_UDP_SIZE
));
932 if (ifo
->userclass
[0] &&
933 !has_option_mask(ifo
->nomask
, DHO_USERCLASS
))
935 AREA_CHECK(ifo
->userclass
[0]);
936 *p
++ = DHO_USERCLASS
;
937 memcpy(p
, ifo
->userclass
,
938 (size_t)ifo
->userclass
[0] + 1);
939 p
+= ifo
->userclass
[0] + 1;
943 if (state
->clientid
) {
944 AREA_CHECK(state
->clientid
[0]);
946 memcpy(p
, state
->clientid
, (size_t)state
->clientid
[0] + 1);
947 p
+= state
->clientid
[0] + 1;
950 if (DHCP_DIR(type
) &&
951 !has_option_mask(ifo
->nomask
, DHO_VENDORCLASSID
) &&
952 ifo
->vendorclassid
[0])
954 AREA_CHECK(ifo
->vendorclassid
[0]);
955 *p
++ = DHO_VENDORCLASSID
;
956 memcpy(p
, ifo
->vendorclassid
, (size_t)ifo
->vendorclassid
[0]+1);
957 p
+= ifo
->vendorclassid
[0] + 1;
960 if (type
== DHCP_DISCOVER
&&
961 !(ifp
->ctx
->options
& DHCPCD_TEST
) &&
962 DHC_REQ(ifo
->requestmask
, ifo
->nomask
, DHO_RAPIDCOMMIT
))
964 /* RFC 4039 Section 3 */
966 *p
++ = DHO_RAPIDCOMMIT
;
970 if (DHCP_DIR(type
)) {
971 hostname
= dhcp_get_hostname(hbuf
, sizeof(hbuf
), ifo
);
974 * RFC4702 3.1 States that if we send the Client FQDN option
975 * then we MUST NOT also send the Host Name option.
976 * Technically we could, but that is not RFC conformant and
977 * also seems to break some DHCP server implemetations such as
978 * Windows. On the other hand, ISC dhcpd is just as non RFC
979 * conformant by not accepting a partially qualified FQDN.
981 if (ifo
->fqdn
!= FQDN_DISABLE
) {
982 /* IETF DHC-FQDN option (81), RFC4702 */
985 i
+= encode_rfc1035(hostname
, NULL
);
991 * S: 1 => Client requests Server to update
992 * a RR in DNS as well as PTR
993 * O: 1 => Server indicates to client that
994 * DNS has been updated
995 * E: 1 => Name data is DNS format
996 * N: 1 => Client requests Server to not
1000 *p
++ = (uint8_t)((ifo
->fqdn
& 0x09) | 0x04);
1002 *p
++ = (FQDN_NONE
& 0x09) | 0x04;
1003 *p
++ = 0; /* from server for PTR RR */
1004 *p
++ = 0; /* from server for A RR if S=1 */
1006 i
= encode_rfc1035(hostname
, p
);
1009 } else if (ifo
->options
& DHCPCD_HOSTNAME
&& hostname
) {
1010 len
= strlen(hostname
);
1012 *p
++ = DHO_HOSTNAME
;
1013 *p
++ = (uint8_t)len
;
1014 memcpy(p
, hostname
, len
);
1020 auth
= NULL
; /* appease GCC */
1022 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
1023 ssize_t alen
= dhcp_auth_encode(ifp
->ctx
, &ifo
->auth
,
1025 NULL
, 0, 4, type
, NULL
, 0);
1026 if (alen
!= -1 && alen
> UINT8_MAX
) {
1031 logerr("%s: dhcp_auth_encode", ifp
->name
);
1032 else if (alen
!= 0) {
1033 auth_len
= (uint8_t)alen
;
1034 AREA_CHECK(auth_len
);
1035 *p
++ = DHO_AUTHENTICATION
;
1043 /* RFC 2563 Auto Configure */
1044 if (type
== DHCP_DISCOVER
&& ifo
->options
& DHCPCD_IPV4LL
&&
1045 !(has_option_mask(ifo
->nomask
, DHO_AUTOCONFIGURE
)))
1048 *p
++ = DHO_AUTOCONFIGURE
;
1053 if (DHCP_DIR(type
)) {
1054 if (ifo
->mudurl
[0]) {
1055 AREA_CHECK(ifo
->mudurl
[0]);
1057 memcpy(p
, ifo
->mudurl
, (size_t)ifo
->mudurl
[0] + 1);
1058 p
+= ifo
->mudurl
[0] + 1;
1061 if (ifo
->vivco_len
&&
1062 !has_option_mask(ifo
->nomask
, DHO_VIVCO
))
1064 AREA_CHECK(sizeof(ul
));
1068 ul
= htonl(ifo
->vivco_en
);
1069 memcpy(p
, &ul
, sizeof(ul
));
1071 for (i
= 0, vivco
= ifo
->vivco
;
1075 AREA_FIT(vivco
->len
);
1076 if (vivco
->len
+ 2 + *lp
> 255) {
1077 logerrx("%s: VIVCO option too big",
1082 *p
++ = (uint8_t)vivco
->len
;
1083 memcpy(p
, vivco
->data
, vivco
->len
);
1085 *lp
= (uint8_t)(*lp
+ vivco
->len
+ 1);
1090 if ((ifo
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) !=
1091 DHCPCD_AUTH_SENDREQUIRE
&&
1092 !has_option_mask(ifo
->nomask
, DHO_FORCERENEW_NONCE
))
1094 /* We support HMAC-MD5 */
1096 *p
++ = DHO_FORCERENEW_NONCE
;
1098 *p
++ = AUTH_ALG_HMAC_MD5
;
1104 len
= (size_t)(p
- (uint8_t *)bootp
);
1106 /* Pad out to the BOOTP message length.
1107 * Even if we send a DHCP packet with a variable length vendor area,
1108 * some servers / relay agents don't like packets smaller than
1109 * a BOOTP message which is fine because that's stipulated
1110 * in RFC1542 section 2.1. */
1111 while (len
< sizeof(*bootp
)) {
1117 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
&& auth_len
!= 0)
1118 dhcp_auth_encode(ifp
->ctx
, &ifo
->auth
, state
->auth
.token
,
1119 (uint8_t *)bootp
, len
, 4, type
, auth
, auth_len
);
1122 return (ssize_t
)len
;
1125 logerrx("%s: DHCP message too big", ifp
->name
);
1131 read_lease(struct interface
*ifp
, struct bootp
**bootp
)
1135 uint8_t buf
[FRAMELEN_MAX
];
1137 struct dhcp_state
*state
= D_STATE(ifp
);
1142 const uint8_t *auth
;
1149 if (state
->leasefile
[0] == '\0') {
1150 logdebugx("reading standard input");
1151 sbytes
= read(fileno(stdin
), buf
.buf
, sizeof(buf
.buf
));
1153 logdebugx("%s: reading lease: %s",
1154 ifp
->name
, state
->leasefile
);
1155 sbytes
= dhcp_readfile(ifp
->ctx
, state
->leasefile
,
1156 buf
.buf
, sizeof(buf
.buf
));
1159 if (errno
!= ENOENT
)
1160 logerr("%s: %s", ifp
->name
, state
->leasefile
);
1163 bytes
= (size_t)sbytes
;
1165 /* Ensure the packet is at lease BOOTP sized
1166 * with a vendor area of 4 octets
1167 * (it should be more, and our read packet enforces this so this
1168 * code should not be needed, but of course people could
1169 * scribble whatever in the stored lease file. */
1170 if (bytes
< DHCP_MIN_LEN
) {
1171 logerrx("%s: %s: truncated lease", ifp
->name
, __func__
);
1175 if (ifp
->ctx
->options
& DHCPCD_DUMPLEASE
)
1178 /* We may have found a BOOTP server */
1179 if (get_option_uint8(ifp
->ctx
, &type
, &buf
.bootp
, bytes
,
1180 DHO_MESSAGETYPE
) == -1)
1184 /* Authenticate the message */
1185 auth
= get_option(ifp
->ctx
, &buf
.bootp
, bytes
,
1186 DHO_AUTHENTICATION
, &auth_len
);
1188 if (dhcp_auth_validate(&state
->auth
, &ifp
->options
->auth
,
1189 &buf
.bootp
, bytes
, 4, type
, auth
, auth_len
) == NULL
)
1191 logerr("%s: authentication failed", ifp
->name
);
1194 if (state
->auth
.token
)
1195 logdebugx("%s: validated using 0x%08" PRIu32
,
1196 ifp
->name
, state
->auth
.token
->secretid
);
1198 logdebugx("%s: accepted reconfigure key", ifp
->name
);
1199 } else if ((ifp
->options
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) ==
1200 DHCPCD_AUTH_SENDREQUIRE
)
1202 logerrx("%s: authentication now required", ifp
->name
);
1208 *bootp
= malloc(bytes
);
1209 if (*bootp
== NULL
) {
1213 memcpy(*bootp
, buf
.buf
, bytes
);
1217 static const struct dhcp_opt
*
1218 dhcp_getoverride(const struct if_options
*ifo
, unsigned int o
)
1221 const struct dhcp_opt
*opt
;
1223 for (i
= 0, opt
= ifo
->dhcp_override
;
1224 i
< ifo
->dhcp_override_len
;
1227 if (opt
->option
== o
)
1233 static const uint8_t *
1234 dhcp_getoption(struct dhcpcd_ctx
*ctx
,
1235 size_t *os
, unsigned int *code
, size_t *len
,
1236 const uint8_t *od
, size_t ol
, struct dhcp_opt
**oopt
)
1239 struct dhcp_opt
*opt
;
1246 *os
= 2; /* code + len */
1247 *code
= (unsigned int)*od
++;
1248 *len
= (size_t)*od
++;
1249 if (*len
> ol
- *os
) {
1256 for (i
= 0, opt
= ctx
->dhcp_opts
; i
< ctx
->dhcp_opts_len
; i
++, opt
++) {
1257 if (opt
->option
== *code
) {
1267 dhcp_env(FILE *fenv
, const char *prefix
, const struct interface
*ifp
,
1268 const struct bootp
*bootp
, size_t bootp_len
)
1270 const struct if_options
*ifo
;
1272 struct in_addr addr
;
1275 struct dhcp_opt
*opt
, *vo
;
1277 char safe
[(BOOTP_FILE_LEN
* 4) + 1];
1282 if (get_option_uint8(ifp
->ctx
, &overl
, bootp
, bootp_len
,
1283 DHO_OPTSOVERLOADED
) == -1)
1286 if (bootp
->yiaddr
|| bootp
->ciaddr
) {
1287 /* Set some useful variables that we derive from the DHCP
1288 * message but are not necessarily in the options */
1289 addr
.s_addr
= bootp
->yiaddr
? bootp
->yiaddr
: bootp
->ciaddr
;
1290 if (efprintf(fenv
, "%s_ip_address=%s",
1291 prefix
, inet_ntoa(addr
)) == -1)
1293 if (get_option_addr(ifp
->ctx
, &net
,
1294 bootp
, bootp_len
, DHO_SUBNETMASK
) == -1) {
1295 net
.s_addr
= ipv4_getnetmask(addr
.s_addr
);
1296 if (efprintf(fenv
, "%s_subnet_mask=%s",
1297 prefix
, inet_ntoa(net
)) == -1)
1300 if (efprintf(fenv
, "%s_subnet_cidr=%d",
1301 prefix
, inet_ntocidr(net
))== -1)
1303 if (get_option_addr(ifp
->ctx
, &brd
,
1304 bootp
, bootp_len
, DHO_BROADCAST
) == -1)
1306 brd
.s_addr
= addr
.s_addr
| ~net
.s_addr
;
1307 if (efprintf(fenv
, "%s_broadcast_address=%s",
1308 prefix
, inet_ntoa(brd
)) == -1)
1311 addr
.s_addr
= bootp
->yiaddr
& net
.s_addr
;
1312 if (efprintf(fenv
, "%s_network_number=%s",
1313 prefix
, inet_ntoa(addr
)) == -1)
1317 if (*bootp
->file
&& !(overl
& 1)) {
1318 print_string(safe
, sizeof(safe
), OT_STRING
,
1319 bootp
->file
, sizeof(bootp
->file
));
1320 if (efprintf(fenv
, "%s_filename=%s", prefix
, safe
) == -1)
1323 if (*bootp
->sname
&& !(overl
& 2)) {
1324 print_string(safe
, sizeof(safe
), OT_STRING
| OT_DOMAIN
,
1325 bootp
->sname
, sizeof(bootp
->sname
));
1326 if (efprintf(fenv
, "%s_server_name=%s", prefix
, safe
) == -1)
1330 /* Zero our indexes */
1331 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
1332 i
< ifp
->ctx
->dhcp_opts_len
;
1334 dhcp_zero_index(opt
);
1335 for (i
= 0, opt
= ifp
->options
->dhcp_override
;
1336 i
< ifp
->options
->dhcp_override_len
;
1338 dhcp_zero_index(opt
);
1339 for (i
= 0, opt
= ifp
->ctx
->vivso
;
1340 i
< ifp
->ctx
->vivso_len
;
1342 dhcp_zero_index(opt
);
1344 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
1345 i
< ifp
->ctx
->dhcp_opts_len
;
1348 if (has_option_mask(ifo
->nomask
, opt
->option
))
1350 if (dhcp_getoverride(ifo
, opt
->option
))
1352 p
= get_option(ifp
->ctx
, bootp
, bootp_len
, opt
->option
, &pl
);
1355 dhcp_envoption(ifp
->ctx
, fenv
, prefix
, ifp
->name
,
1356 opt
, dhcp_getoption
, p
, pl
);
1358 if (opt
->option
!= DHO_VIVSO
|| pl
<= (int)sizeof(uint32_t))
1360 memcpy(&en
, p
, sizeof(en
));
1362 vo
= vivso_find(en
, ifp
);
1365 /* Skip over en + total size */
1366 p
+= sizeof(en
) + 1;
1367 pl
-= sizeof(en
) + 1;
1368 dhcp_envoption(ifp
->ctx
, fenv
, prefix
, ifp
->name
,
1369 vo
, dhcp_getoption
, p
, pl
);
1372 for (i
= 0, opt
= ifo
->dhcp_override
;
1373 i
< ifo
->dhcp_override_len
;
1376 if (has_option_mask(ifo
->nomask
, opt
->option
))
1378 p
= get_option(ifp
->ctx
, bootp
, bootp_len
, opt
->option
, &pl
);
1381 dhcp_envoption(ifp
->ctx
, fenv
, prefix
, ifp
->name
,
1382 opt
, dhcp_getoption
, p
, pl
);
1389 get_lease(struct interface
*ifp
,
1390 struct dhcp_lease
*lease
, const struct bootp
*bootp
, size_t len
)
1392 struct dhcpcd_ctx
*ctx
;
1394 assert(bootp
!= NULL
);
1396 memcpy(&lease
->cookie
, bootp
->vend
, sizeof(lease
->cookie
));
1397 /* BOOTP does not set yiaddr for replies when ciaddr is set. */
1398 lease
->addr
.s_addr
= bootp
->yiaddr
? bootp
->yiaddr
: bootp
->ciaddr
;
1400 if (ifp
->options
->options
& (DHCPCD_STATIC
| DHCPCD_INFORM
)) {
1401 if (ifp
->options
->req_addr
.s_addr
!= INADDR_ANY
) {
1402 lease
->mask
= ifp
->options
->req_mask
;
1403 if (ifp
->options
->req_brd
.s_addr
!= INADDR_ANY
)
1404 lease
->brd
= ifp
->options
->req_brd
;
1407 lease
->addr
.s_addr
| ~lease
->mask
.s_addr
;
1409 const struct ipv4_addr
*ia
;
1411 ia
= ipv4_iffindaddr(ifp
, &lease
->addr
, NULL
);
1413 lease
->mask
= ia
->mask
;
1414 lease
->brd
= ia
->brd
;
1417 if (get_option_addr(ctx
, &lease
->mask
, bootp
, len
,
1418 DHO_SUBNETMASK
) == -1)
1419 lease
->mask
.s_addr
=
1420 ipv4_getnetmask(lease
->addr
.s_addr
);
1421 if (get_option_addr(ctx
, &lease
->brd
, bootp
, len
,
1422 DHO_BROADCAST
) == -1)
1424 lease
->addr
.s_addr
| ~lease
->mask
.s_addr
;
1426 if (get_option_uint32(ctx
, &lease
->leasetime
,
1427 bootp
, len
, DHO_LEASETIME
) != 0)
1428 lease
->leasetime
= DHCP_INFINITE_LIFETIME
;
1429 if (get_option_uint32(ctx
, &lease
->renewaltime
,
1430 bootp
, len
, DHO_RENEWALTIME
) != 0)
1431 lease
->renewaltime
= 0;
1432 if (get_option_uint32(ctx
, &lease
->rebindtime
,
1433 bootp
, len
, DHO_REBINDTIME
) != 0)
1434 lease
->rebindtime
= 0;
1435 if (get_option_addr(ctx
, &lease
->server
, bootp
, len
, DHO_SERVERID
) != 0)
1436 lease
->server
.s_addr
= INADDR_ANY
;
1440 get_dhcp_op(uint8_t type
)
1442 const struct dhcp_op
*d
;
1444 for (d
= dhcp_ops
; d
->name
; d
++)
1445 if (d
->value
== type
)
1451 dhcp_fallback(void *arg
)
1453 struct interface
*iface
;
1455 iface
= (struct interface
*)arg
;
1456 dhcpcd_selectprofile(iface
, iface
->options
->fallback
);
1457 dhcpcd_startinterface(iface
);
1461 dhcp_new_xid(struct interface
*ifp
)
1463 struct dhcp_state
*state
;
1464 const struct interface
*ifp1
;
1465 const struct dhcp_state
*state1
;
1467 state
= D_STATE(ifp
);
1468 if (ifp
->options
->options
& DHCPCD_XID_HWADDR
&&
1469 ifp
->hwlen
>= sizeof(state
->xid
))
1470 /* The lower bits are probably more unique on the network */
1472 (ifp
->hwaddr
+ ifp
->hwlen
) - sizeof(state
->xid
),
1473 sizeof(state
->xid
));
1476 state
->xid
= arc4random();
1479 /* Ensure it's unique */
1480 TAILQ_FOREACH(ifp1
, ifp
->ctx
->ifaces
, next
) {
1483 if ((state1
= D_CSTATE(ifp1
)) == NULL
)
1485 if (state1
->xid
== state
->xid
)
1489 if (ifp
->options
->options
& DHCPCD_XID_HWADDR
&&
1490 ifp
->hwlen
>= sizeof(state
->xid
))
1492 logerrx("%s: duplicate xid on %s",
1493 ifp
->name
, ifp1
->name
);
1499 /* We can't do this when sharing leases across interfaes */
1501 /* As the XID changes, re-apply the filter. */
1502 if (state
->bpf_fd
!= -1) {
1503 if (bpf_bootp(ifp
, state
->bpf_fd
) == -1)
1504 logerr(__func__
); /* try to continue */
1510 dhcp_closebpf(struct interface
*ifp
)
1512 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
1513 struct dhcp_state
*state
= D_STATE(ifp
);
1516 if (IN_PRIVSEP_SE(ctx
))
1517 ps_bpf_closebootp(ifp
);
1520 if (state
->bpf
!= NULL
) {
1521 eloop_event_delete(ctx
->eloop
, state
->bpf
->bpf_fd
);
1522 bpf_close(state
->bpf
);
1528 dhcp_closeinet(struct interface
*ifp
)
1530 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
1531 struct dhcp_state
*state
= D_STATE(ifp
);
1534 if (IN_PRIVSEP_SE(ctx
)) {
1535 if (state
->addr
!= NULL
)
1536 ps_inet_closebootp(state
->addr
);
1540 if (state
->udp_rfd
!= -1) {
1541 eloop_event_delete(ctx
->eloop
, state
->udp_rfd
);
1542 close(state
->udp_rfd
);
1543 state
->udp_rfd
= -1;
1548 dhcp_close(struct interface
*ifp
)
1550 struct dhcp_state
*state
= D_STATE(ifp
);
1556 dhcp_closeinet(ifp
);
1558 state
->interval
= 0;
1562 dhcp_openudp(struct in_addr
*ia
)
1565 struct sockaddr_in sin
;
1568 if ((s
= xsocket(PF_INET
, SOCK_DGRAM
| SOCK_CXNB
, IPPROTO_UDP
)) == -1)
1572 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1)
1575 if (setsockopt(s
, IPPROTO_IP
, IP_RECVIF
, &n
, sizeof(n
)) == -1)
1578 if (setsockopt(s
, IPPROTO_IP
, IP_RECVPKTINFO
, &n
, sizeof(n
)) == -1)
1582 if (setsockopt(s
, SOL_SOCKET
, SO_RERROR
, &n
, sizeof(n
)) == -1)
1586 memset(&sin
, 0, sizeof(sin
));
1587 sin
.sin_family
= AF_INET
;
1588 sin
.sin_port
= htons(BOOTPC
);
1591 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1)
1602 in_cksum(const void *data
, size_t len
, uint32_t *isum
)
1604 const uint16_t *word
= data
;
1605 uint32_t sum
= isum
!= NULL
? *isum
: 0;
1607 for (; len
> 1; len
-= sizeof(*word
))
1611 sum
+= htons((uint16_t)(*(const uint8_t *)word
<< 8));
1616 sum
= (sum
>> 16) + (sum
& 0xffff);
1619 return (uint16_t)~sum
;
1622 static struct bootp_pkt
*
1623 dhcp_makeudppacket(size_t *sz
, const uint8_t *data
, size_t length
,
1624 struct in_addr source
, struct in_addr dest
)
1626 struct bootp_pkt
*udpp
;
1630 if ((udpp
= calloc(1, sizeof(*ip
) + sizeof(*udp
) + length
)) == NULL
)
1635 /* OK, this is important :)
1636 * We copy the data to our packet and then create a small part of the
1637 * ip structure and an invalid ip_len (basically udp length).
1638 * We then fill the udp structure and put the checksum
1639 * of the whole packet into the udp checksum.
1640 * Finally we complete the ip structure and ip checksum.
1641 * If we don't do the ordering like so then the udp checksum will be
1642 * broken, so find another way of doing it! */
1644 memcpy(&udpp
->bootp
, data
, length
);
1646 ip
->ip_p
= IPPROTO_UDP
;
1647 ip
->ip_src
.s_addr
= source
.s_addr
;
1648 if (dest
.s_addr
== 0)
1649 ip
->ip_dst
.s_addr
= INADDR_BROADCAST
;
1651 ip
->ip_dst
.s_addr
= dest
.s_addr
;
1653 udp
->uh_sport
= htons(BOOTPC
);
1654 udp
->uh_dport
= htons(BOOTPS
);
1655 udp
->uh_ulen
= htons((uint16_t)(sizeof(*udp
) + length
));
1656 ip
->ip_len
= udp
->uh_ulen
;
1657 udp
->uh_sum
= in_cksum(udpp
, sizeof(*ip
) + sizeof(*udp
) + length
, NULL
);
1659 ip
->ip_v
= IPVERSION
;
1660 ip
->ip_hl
= sizeof(*ip
) >> 2;
1661 ip
->ip_id
= (uint16_t)arc4random_uniform(UINT16_MAX
);
1662 ip
->ip_ttl
= IPDEFTTL
;
1663 ip
->ip_len
= htons((uint16_t)(sizeof(*ip
) + sizeof(*udp
) + length
));
1664 ip
->ip_sum
= in_cksum(ip
, sizeof(*ip
), NULL
);
1665 if (ip
->ip_sum
== 0)
1666 ip
->ip_sum
= 0xffff; /* RFC 768 */
1668 *sz
= sizeof(*ip
) + sizeof(*udp
) + length
;
1673 dhcp_sendudp(struct interface
*ifp
, struct in_addr
*to
, void *data
, size_t len
)
1675 struct sockaddr_in sin
= {
1676 .sin_family
= AF_INET
,
1678 .sin_port
= htons(BOOTPS
),
1680 .sin_len
= sizeof(sin
),
1683 struct udphdr udp
= {
1684 .uh_sport
= htons(BOOTPC
),
1685 .uh_dport
= htons(BOOTPS
),
1686 .uh_ulen
= htons((uint16_t)(sizeof(udp
) + len
)),
1688 struct iovec iov
[] = {
1689 { .iov_base
= &udp
, .iov_len
= sizeof(udp
), },
1690 { .iov_base
= data
, .iov_len
= len
, },
1692 struct msghdr msg
= {
1693 .msg_name
= (void *)&sin
,
1694 .msg_namelen
= sizeof(sin
),
1696 .msg_iovlen
= __arraycount(iov
),
1698 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
1701 if (ctx
->options
& DHCPCD_PRIVSEP
)
1702 return ps_inet_sendbootp(ifp
, &msg
);
1704 return sendmsg(ctx
->udp_wfd
, &msg
, 0);
1708 send_message(struct interface
*ifp
, uint8_t type
,
1709 void (*callback
)(void *))
1711 struct dhcp_state
*state
= D_STATE(ifp
);
1712 struct if_options
*ifo
= ifp
->options
;
1713 struct bootp
*bootp
;
1714 struct bootp_pkt
*udp
;
1717 struct in_addr from
, to
;
1720 if (callback
== NULL
) {
1721 /* No carrier? Don't bother sending the packet. */
1722 if (!if_is_link_up(ifp
))
1724 logdebugx("%s: sending %s with xid 0x%x",
1726 ifo
->options
& DHCPCD_BOOTP
? "BOOTP" : get_dhcp_op(type
),
1728 RT
= 0; /* bogus gcc warning */
1730 if (state
->interval
== 0)
1731 state
->interval
= 4;
1733 state
->interval
*= 2;
1734 if (state
->interval
> 64)
1735 state
->interval
= 64;
1737 RT
= (state
->interval
* MSEC_PER_SEC
) +
1738 (arc4random_uniform(MSEC_PER_SEC
* 2) - MSEC_PER_SEC
);
1739 /* No carrier? Don't bother sending the packet.
1740 * However, we do need to advance the timeout. */
1741 if (!if_is_link_up(ifp
))
1743 logdebugx("%s: sending %s (xid 0x%x), next in %0.1f seconds",
1745 ifo
->options
& DHCPCD_BOOTP
? "BOOTP" : get_dhcp_op(type
),
1747 (float)RT
/ MSEC_PER_SEC
);
1750 r
= make_message(&bootp
, ifp
, type
);
1755 if (!(state
->added
& (STATE_FAKE
| STATE_EXPIRED
)) &&
1756 state
->addr
!= NULL
&&
1757 ipv4_iffindaddr(ifp
, &state
->lease
.addr
, NULL
) != NULL
)
1758 from
.s_addr
= state
->lease
.addr
.s_addr
;
1760 from
.s_addr
= INADDR_ANY
;
1761 if (from
.s_addr
!= INADDR_ANY
&&
1762 state
->lease
.server
.s_addr
!= INADDR_ANY
)
1763 to
.s_addr
= state
->lease
.server
.s_addr
;
1765 to
.s_addr
= INADDR_BROADCAST
;
1768 * If not listening on the unspecified address we can
1769 * only receive broadcast messages via BPF.
1770 * Sockets bound to an address cannot receive broadcast messages
1771 * even if they are setup to send them.
1772 * Broadcasting from UDP is only an optimisation for rebinding
1773 * and on BSD, at least, is reliant on the subnet route being
1774 * correctly configured to receive the unicast reply.
1775 * As such, we always broadcast and receive the reply to it via BPF.
1776 * This also guarantees we have a DHCP server attached to the
1777 * interface we want to configure because we can't dictate the
1778 * interface via IP_PKTINFO unlike for IPv6.
1780 if (to
.s_addr
!= INADDR_BROADCAST
) {
1781 if (dhcp_sendudp(ifp
, &to
, bootp
, len
) != -1)
1783 logerr("%s: dhcp_sendudp", ifp
->name
);
1786 if (dhcp_openbpf(ifp
) == -1)
1789 udp
= dhcp_makeudppacket(&ulen
, (uint8_t *)bootp
, len
, from
, to
);
1791 logerr("%s: dhcp_makeudppacket", ifp
->name
);
1794 } else if (ifp
->ctx
->options
& DHCPCD_PRIVSEP
) {
1795 r
= ps_bpf_sendbootp(ifp
, udp
, ulen
);
1799 r
= bpf_send(state
->bpf
, ETHERTYPE_IP
, udp
, ulen
);
1802 /* If we failed to send a raw packet this normally means
1803 * we don't have the ability to work beneath the IP layer
1804 * for this interface.
1805 * As such we remove it from consideration without actually
1806 * stopping the interface. */
1808 logerr("%s: bpf_send", ifp
->name
);
1816 if (!(ifp
->ctx
->options
& DHCPCD_TEST
))
1817 dhcp_drop(ifp
, "FAIL");
1818 eloop_timeout_delete(ifp
->ctx
->eloop
,
1828 /* Even if we fail to send a packet we should continue as we are
1829 * as our failure timeouts will change out codepath when needed. */
1830 if (callback
!= NULL
)
1831 eloop_timeout_add_msec(ifp
->ctx
->eloop
, RT
, callback
, ifp
);
1835 send_inform(void *arg
)
1838 send_message((struct interface
*)arg
, DHCP_INFORM
, send_inform
);
1842 send_discover(void *arg
)
1845 send_message((struct interface
*)arg
, DHCP_DISCOVER
, send_discover
);
1849 send_request(void *arg
)
1852 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_request
);
1856 send_renew(void *arg
)
1859 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_renew
);
1863 send_rebind(void *arg
)
1866 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_rebind
);
1870 dhcp_discover(void *arg
)
1872 struct interface
*ifp
= arg
;
1873 struct dhcp_state
*state
= D_STATE(ifp
);
1874 struct if_options
*ifo
= ifp
->options
;
1876 state
->state
= DHS_DISCOVER
;
1878 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
1879 if (!(state
->added
& STATE_EXPIRED
)) {
1880 if (ifo
->fallback
&& ifo
->fallback_time
)
1881 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1882 ifo
->fallback_time
, dhcp_fallback
, ifp
);
1884 else if (ifo
->options
& DHCPCD_IPV4LL
)
1885 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1886 ifo
->ipv4ll_time
, ipv4ll_start
, ifp
);
1889 if (ifo
->options
& DHCPCD_REQUEST
)
1890 loginfox("%s: soliciting a DHCP lease (requesting %s)",
1891 ifp
->name
, inet_ntoa(ifo
->req_addr
));
1893 loginfox("%s: soliciting a %s lease",
1894 ifp
->name
, ifo
->options
& DHCPCD_BOOTP
? "BOOTP" : "DHCP");
1899 dhcp_requestfailed(void *arg
)
1901 struct interface
*ifp
= arg
;
1902 struct dhcp_state
*state
= D_STATE(ifp
);
1904 logwarnx("%s: failed to request the lease", ifp
->name
);
1906 state
->offer
= NULL
;
1907 state
->offer_len
= 0;
1908 state
->interval
= 0;
1913 dhcp_request(void *arg
)
1915 struct interface
*ifp
= arg
;
1916 struct dhcp_state
*state
= D_STATE(ifp
);
1917 struct if_options
*ifo
= ifp
->options
;
1919 state
->state
= DHS_REQUEST
;
1920 // Handle the server being silent to our request.
1921 if (ifo
->request_time
!= 0)
1922 eloop_timeout_add_sec(ifp
->ctx
->eloop
, ifo
->request_time
,
1923 dhcp_requestfailed
, ifp
);
1928 dhcp_expire(void *arg
)
1930 struct interface
*ifp
= arg
;
1931 struct dhcp_state
*state
= D_STATE(ifp
);
1933 if (ifp
->options
->options
& DHCPCD_LASTLEASE_EXTEND
) {
1934 logwarnx("%s: DHCP lease expired, extending lease", ifp
->name
);
1935 state
->added
|= STATE_EXPIRED
;
1937 logerrx("%s: DHCP lease expired", ifp
->name
);
1938 dhcp_drop(ifp
, "EXPIRE");
1939 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
1941 state
->interval
= 0;
1945 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
1947 dhcp_decline(struct interface
*ifp
)
1949 struct dhcp_state
*state
= D_STATE(ifp
);
1951 // Set the expired state so we send over BPF as this could be
1952 // an address defence failure.
1953 state
->added
|= STATE_EXPIRED
;
1954 send_message(ifp
, DHCP_DECLINE
, NULL
);
1959 dhcp_startrenew(void *arg
)
1961 struct interface
*ifp
= arg
;
1962 struct dhcp_state
*state
;
1963 struct dhcp_lease
*lease
;
1965 if ((state
= D_STATE(ifp
)) == NULL
)
1968 /* Only renew in the bound or renew states */
1969 if (state
->state
!= DHS_BOUND
&&
1970 state
->state
!= DHS_RENEW
)
1973 /* Remove the timeout as the renew may have been forced. */
1974 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp_startrenew
, ifp
);
1976 lease
= &state
->lease
;
1977 logdebugx("%s: renewing lease of %s", ifp
->name
,
1978 inet_ntoa(lease
->addr
));
1979 state
->state
= DHS_RENEW
;
1981 state
->interval
= 0;
1986 dhcp_renew(struct interface
*ifp
)
1989 dhcp_startrenew(ifp
);
1993 dhcp_rebind(void *arg
)
1995 struct interface
*ifp
= arg
;
1996 struct dhcp_state
*state
= D_STATE(ifp
);
1997 struct dhcp_lease
*lease
= &state
->lease
;
1999 logwarnx("%s: failed to renew DHCP, rebinding", ifp
->name
);
2000 logdebugx("%s: expire in %"PRIu32
" seconds",
2001 ifp
->name
, lease
->leasetime
- lease
->rebindtime
);
2002 state
->state
= DHS_REBIND
;
2003 eloop_timeout_delete(ifp
->ctx
->eloop
, send_renew
, ifp
);
2004 state
->lease
.server
.s_addr
= INADDR_ANY
;
2005 state
->interval
= 0;
2006 ifp
->options
->options
&= ~(DHCPCD_CSR_WARNED
|
2007 DHCPCD_ROUTER_HOST_ROUTE_WARNED
);
2011 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
2013 dhcp_finish_dad(struct interface
*ifp
, struct in_addr
*ia
)
2015 struct dhcp_state
*state
= D_STATE(ifp
);
2017 if (state
->state
== DHS_BOUND
)
2019 if (state
->offer
== NULL
|| state
->offer
->yiaddr
!= ia
->s_addr
)
2022 logdebugx("%s: DAD completed for %s", ifp
->name
, inet_ntoa(*ia
));
2023 if (!(ifp
->options
->options
& DHCPCD_INFORM
))
2025 #ifndef IN_IFF_DUPLICATED
2027 struct bootp
*bootp
;
2031 len
= state
->new_len
;
2032 state
->new = state
->offer
;
2033 state
->new_len
= state
->offer_len
;
2034 get_lease(ifp
, &state
->lease
, state
->new, state
->new_len
);
2035 ipv4_applyaddr(ifp
);
2037 state
->new_len
= len
;
2042 /* Stop IPv4LL now we have a working DHCP address */
2043 if (!IN_LINKLOCAL(ntohl(ia
->s_addr
)))
2047 if (ifp
->options
->options
& DHCPCD_INFORM
)
2052 dhcp_addr_duplicated(struct interface
*ifp
, struct in_addr
*ia
)
2054 struct dhcp_state
*state
= D_STATE(ifp
);
2055 unsigned long long opts
= ifp
->options
->options
;
2056 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
2057 bool deleted
= false;
2058 #ifdef IN_IFF_DUPLICATED
2059 struct ipv4_addr
*iap
;
2062 if ((state
->offer
== NULL
|| state
->offer
->yiaddr
!= ia
->s_addr
) &&
2063 !IN_ARE_ADDR_EQUAL(ia
, &state
->lease
.addr
))
2066 /* RFC 2131 3.1.5, Client-server interaction */
2067 logerrx("%s: DAD detected %s", ifp
->name
, inet_ntoa(*ia
));
2068 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2069 if (!(opts
& DHCPCD_STATIC
) && !state
->lease
.frominfo
)
2071 #ifdef IN_IFF_DUPLICATED
2072 if ((iap
= ipv4_iffindaddr(ifp
, ia
, NULL
)) != NULL
) {
2073 ipv4_deladdr(iap
, 0);
2077 eloop_timeout_delete(ctx
->eloop
, NULL
, ifp
);
2078 if (opts
& (DHCPCD_STATIC
| DHCPCD_INFORM
)) {
2079 state
->reason
= "EXPIRE";
2080 script_runreason(ifp
, state
->reason
);
2081 #define NOT_ONLY_SELF (DHCPCD_MANAGER | DHCPCD_IPV6RS | DHCPCD_DHCP6)
2082 if (!(ctx
->options
& NOT_ONLY_SELF
))
2083 eloop_exit(ifp
->ctx
->eloop
, EXIT_FAILURE
);
2086 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2087 DHCP_RAND_MAX
, dhcp_discover
, ifp
);
2093 #ifdef KERNEL_RFC5227
2096 dhcp_arp_announced(struct arp_state
*state
)
2104 dhcp_arp_defend_failed(struct arp_state
*astate
)
2106 struct interface
*ifp
= astate
->iface
;
2107 struct dhcp_state
*state
= D_STATE(ifp
);
2109 if (!(ifp
->options
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
)))
2111 dhcp_drop(ifp
, "EXPIRED");
2112 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2117 #if !defined(KERNEL_RFC5227) || defined(ARPING)
2118 static void dhcp_arp_not_found(struct arp_state
*);
2120 static struct arp_state
*
2121 dhcp_arp_new(struct interface
*ifp
, struct in_addr
*addr
)
2123 struct arp_state
*astate
;
2125 astate
= arp_new(ifp
, addr
);
2129 astate
->found_cb
= dhcp_arp_found
;
2130 astate
->not_found_cb
= dhcp_arp_not_found
;
2131 #ifdef KERNEL_RFC5227
2132 astate
->announced_cb
= dhcp_arp_announced
;
2134 astate
->announced_cb
= NULL
;
2135 astate
->defend_failed_cb
= dhcp_arp_defend_failed
;
2143 dhcp_arping(struct interface
*ifp
)
2145 struct dhcp_state
*state
;
2146 struct if_options
*ifo
;
2147 struct arp_state
*astate
;
2148 struct in_addr addr
;
2150 state
= D_STATE(ifp
);
2153 if (ifo
->arping_len
== 0 || state
->arping_index
> ifo
->arping_len
)
2156 if (state
->arping_index
+ 1 == ifo
->arping_len
) {
2157 state
->arping_index
++;
2158 dhcpcd_startinterface(ifp
);
2162 addr
.s_addr
= ifo
->arping
[++state
->arping_index
];
2163 astate
= dhcp_arp_new(ifp
, &addr
);
2164 if (astate
== NULL
) {
2173 #if !defined(KERNEL_RFC5227) || defined(ARPING)
2175 dhcp_arp_not_found(struct arp_state
*astate
)
2177 struct interface
*ifp
;
2179 ifp
= astate
->iface
;
2181 if (dhcp_arping(ifp
) == 1) {
2187 dhcp_finish_dad(ifp
, &astate
->addr
);
2191 dhcp_arp_found(struct arp_state
*astate
, const struct arp_msg
*amsg
)
2193 struct in_addr addr
;
2194 struct interface
*ifp
= astate
->iface
;
2196 struct dhcp_state
*state
;
2197 struct if_options
*ifo
;
2199 state
= D_STATE(ifp
);
2202 if (state
->arping_index
!= -1 &&
2203 state
->arping_index
< ifo
->arping_len
&&
2205 amsg
->sip
.s_addr
== ifo
->arping
[state
->arping_index
])
2207 char buf
[HWADDR_LEN
* 3];
2209 hwaddr_ntoa(amsg
->sha
, ifp
->hwlen
, buf
, sizeof(buf
));
2210 if (dhcpcd_selectprofile(ifp
, buf
) == -1 &&
2211 dhcpcd_selectprofile(ifp
, inet_ntoa(amsg
->sip
)) == -1)
2213 /* We didn't find a profile for this
2214 * address or hwaddr, so move to the next
2216 dhcp_arp_not_found(astate
);
2220 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2221 dhcpcd_startinterface(ifp
);
2228 addr
= astate
->addr
;
2230 dhcp_addr_duplicated(ifp
, &addr
);
2237 dhcp_bind(struct interface
*ifp
)
2239 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
2240 struct dhcp_state
*state
= D_STATE(ifp
);
2241 struct if_options
*ifo
= ifp
->options
;
2242 struct dhcp_lease
*lease
= &state
->lease
;
2245 state
->reason
= NULL
;
2246 /* If we don't have an offer, we are re-binding a lease on preference,
2247 * normally when two interfaces have a lease matching IP addresses. */
2250 state
->old
= state
->new;
2251 state
->old_len
= state
->new_len
;
2252 state
->new = state
->offer
;
2253 state
->new_len
= state
->offer_len
;
2254 state
->offer
= NULL
;
2255 state
->offer_len
= 0;
2257 get_lease(ifp
, lease
, state
->new, state
->new_len
);
2258 if (ifo
->options
& DHCPCD_STATIC
) {
2259 loginfox("%s: using static address %s/%d",
2260 ifp
->name
, inet_ntoa(lease
->addr
),
2261 inet_ntocidr(lease
->mask
));
2262 lease
->leasetime
= DHCP_INFINITE_LIFETIME
;
2263 state
->reason
= "STATIC";
2264 } else if (ifo
->options
& DHCPCD_INFORM
) {
2265 loginfox("%s: received approval for %s",
2266 ifp
->name
, inet_ntoa(lease
->addr
));
2267 lease
->leasetime
= DHCP_INFINITE_LIFETIME
;
2268 state
->reason
= "INFORM";
2270 if (lease
->frominfo
)
2271 state
->reason
= "TIMEOUT";
2272 if (lease
->leasetime
== DHCP_INFINITE_LIFETIME
) {
2273 lease
->renewaltime
=
2276 loginfox("%s: leased %s for infinity",
2277 ifp
->name
, inet_ntoa(lease
->addr
));
2279 if (lease
->leasetime
< DHCP_MIN_LEASE
) {
2280 logwarnx("%s: minimum lease is %d seconds",
2281 ifp
->name
, DHCP_MIN_LEASE
);
2282 lease
->leasetime
= DHCP_MIN_LEASE
;
2284 if (lease
->rebindtime
== 0)
2286 (uint32_t)(lease
->leasetime
* T2
);
2287 else if (lease
->rebindtime
>= lease
->leasetime
) {
2289 (uint32_t)(lease
->leasetime
* T2
);
2290 logwarnx("%s: rebind time greater than lease "
2291 "time, forcing to %"PRIu32
" seconds",
2292 ifp
->name
, lease
->rebindtime
);
2294 if (lease
->renewaltime
== 0)
2295 lease
->renewaltime
=
2296 (uint32_t)(lease
->leasetime
* T1
);
2297 else if (lease
->renewaltime
> lease
->rebindtime
) {
2298 lease
->renewaltime
=
2299 (uint32_t)(lease
->leasetime
* T1
);
2300 logwarnx("%s: renewal time greater than "
2301 "rebind time, forcing to %"PRIu32
" seconds",
2302 ifp
->name
, lease
->renewaltime
);
2304 if (state
->state
== DHS_RENEW
&& state
->addr
&&
2305 lease
->addr
.s_addr
== state
->addr
->addr
.s_addr
&&
2306 !(state
->added
& STATE_FAKE
))
2307 logdebugx("%s: leased %s for %"PRIu32
" seconds",
2308 ifp
->name
, inet_ntoa(lease
->addr
),
2311 loginfox("%s: leased %s for %"PRIu32
" seconds",
2312 ifp
->name
, inet_ntoa(lease
->addr
),
2316 if (ctx
->options
& DHCPCD_TEST
) {
2317 state
->reason
= "TEST";
2318 script_runreason(ifp
, state
->reason
);
2319 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
2322 if (state
->reason
== NULL
) {
2324 !(state
->added
& (STATE_FAKE
| STATE_EXPIRED
)))
2326 if (state
->old
->yiaddr
== state
->new->yiaddr
&&
2327 lease
->server
.s_addr
&&
2328 state
->state
!= DHS_REBIND
)
2329 state
->reason
= "RENEW";
2331 state
->reason
= "REBIND";
2332 } else if (state
->state
== DHS_REBOOT
)
2333 state
->reason
= "REBOOT";
2335 state
->reason
= "BOUND";
2337 if (lease
->leasetime
== DHCP_INFINITE_LIFETIME
)
2338 lease
->renewaltime
= lease
->rebindtime
= lease
->leasetime
;
2340 eloop_timeout_add_sec(ctx
->eloop
,
2341 lease
->renewaltime
, dhcp_startrenew
, ifp
);
2342 eloop_timeout_add_sec(ctx
->eloop
,
2343 lease
->rebindtime
, dhcp_rebind
, ifp
);
2344 eloop_timeout_add_sec(ctx
->eloop
,
2345 lease
->leasetime
, dhcp_expire
, ifp
);
2346 logdebugx("%s: renew in %"PRIu32
" seconds, rebind in %"PRIu32
2348 ifp
->name
, lease
->renewaltime
, lease
->rebindtime
);
2350 state
->state
= DHS_BOUND
;
2351 if (!state
->lease
.frominfo
&&
2352 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))) {
2353 logdebugx("%s: writing lease: %s",
2354 ifp
->name
, state
->leasefile
);
2355 if (dhcp_writefile(ifp
->ctx
, state
->leasefile
, 0640,
2356 state
->new, state
->new_len
) == -1)
2357 logerr("dhcp_writefile: %s", state
->leasefile
);
2360 old_state
= state
->added
;
2362 if (!(ifo
->options
& DHCPCD_CONFIGURE
)) {
2363 struct ipv4_addr
*ia
;
2365 script_runreason(ifp
, state
->reason
);
2366 dhcpcd_daemonise(ifp
->ctx
);
2368 /* We we are not configuring the address, we need to keep
2369 * the BPF socket open if the address does not exist. */
2370 ia
= ipv4_iffindaddr(ifp
, &state
->lease
.addr
, NULL
);
2373 state
->added
= STATE_ADDED
;
2380 /* Add the address */
2381 if (ipv4_applyaddr(ifp
) == NULL
) {
2382 /* There was an error adding the address.
2383 * If we are in oneshot, exit with a failure. */
2384 if (ctx
->options
& DHCPCD_ONESHOT
) {
2385 loginfox("exiting due to oneshot");
2386 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
2391 /* Close the BPF filter as we can now receive DHCP messages
2392 * on a UDP socket. */
2396 /* If not in manager mode, open an address specific socket. */
2397 if (ctx
->options
& DHCPCD_MANAGER
||
2398 ifo
->options
& DHCPCD_STATIC
||
2399 (state
->old
!= NULL
&&
2400 state
->old
->yiaddr
== state
->new->yiaddr
&&
2401 old_state
& STATE_ADDED
&& !(old_state
& STATE_FAKE
)))
2404 dhcp_closeinet(ifp
);
2406 if (IN_PRIVSEP_SE(ctx
)) {
2407 if (ps_inet_openbootp(state
->addr
) == -1)
2413 state
->udp_rfd
= dhcp_openudp(&state
->addr
->addr
);
2414 if (state
->udp_rfd
== -1) {
2416 /* Address sharing without manager mode is not supported.
2417 * It's also possible another DHCP client could be running,
2418 * which is even worse.
2419 * We still need to work, so re-open BPF. */
2423 if (eloop_event_add(ctx
->eloop
, state
->udp_rfd
, ELE_READ
,
2424 dhcp_handleifudp
, ifp
) == -1)
2425 logerr("%s: eloop_event_add", __func__
);
2429 dhcp_message_new(struct bootp
**bootp
,
2430 const struct in_addr
*addr
, const struct in_addr
*mask
)
2435 if ((*bootp
= calloc(1, sizeof(**bootp
))) == NULL
)
2438 (*bootp
)->yiaddr
= addr
->s_addr
;
2441 cookie
= htonl(MAGIC_COOKIE
);
2442 memcpy(p
, &cookie
, sizeof(cookie
));
2443 p
+= sizeof(cookie
);
2445 if (mask
->s_addr
!= INADDR_ANY
) {
2446 *p
++ = DHO_SUBNETMASK
;
2447 *p
++ = sizeof(mask
->s_addr
);
2448 memcpy(p
, &mask
->s_addr
, sizeof(mask
->s_addr
));
2449 p
+= sizeof(mask
->s_addr
);
2453 return sizeof(**bootp
);
2456 #if defined(ARP) || defined(KERNEL_RFC5227)
2458 dhcp_arp_address(struct interface
*ifp
)
2460 struct dhcp_state
*state
;
2461 struct in_addr addr
;
2462 struct ipv4_addr
*ia
;
2464 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2466 state
= D_STATE(ifp
);
2467 addr
.s_addr
= state
->offer
->yiaddr
== INADDR_ANY
?
2468 state
->offer
->ciaddr
: state
->offer
->yiaddr
;
2469 /* If the interface already has the address configured
2470 * then we can't ARP for duplicate detection. */
2471 ia
= ipv4_iffindaddr(ifp
, &addr
, NULL
);
2472 #ifdef IN_IFF_NOTUSEABLE
2473 if (ia
== NULL
|| ia
->addr_flags
& IN_IFF_NOTUSEABLE
) {
2474 state
->state
= DHS_PROBE
;
2476 struct dhcp_lease l
;
2478 get_lease(ifp
, &l
, state
->offer
, state
->offer_len
);
2479 /* Add the address now, let the kernel handle DAD. */
2480 ipv4_addaddr(ifp
, &l
.addr
, &l
.mask
, &l
.brd
,
2481 l
.leasetime
, l
.rebindtime
);
2482 } else if (ia
->addr_flags
& IN_IFF_DUPLICATED
)
2483 dhcp_addr_duplicated(ifp
, &ia
->addr
);
2485 loginfox("%s: waiting for DAD on %s",
2486 ifp
->name
, inet_ntoa(addr
));
2490 if (!(ifp
->flags
& IFF_NOARP
) &&
2491 ifp
->options
->options
& DHCPCD_ARP
)
2493 struct arp_state
*astate
;
2494 struct dhcp_lease l
;
2496 /* Even if the address exists, we need to defend it. */
2497 astate
= dhcp_arp_new(ifp
, &addr
);
2502 state
->state
= DHS_PROBE
;
2503 get_lease(ifp
, &l
, state
->offer
, state
->offer_len
);
2504 loginfox("%s: probing address %s/%d",
2505 ifp
->name
, inet_ntoa(l
.addr
), inet_ntocidr(l
.mask
));
2506 /* We need to handle DAD. */
2517 dhcp_arp_bind(struct interface
*ifp
)
2520 if (ifp
->ctx
->options
& DHCPCD_TEST
||
2521 dhcp_arp_address(ifp
) == 1)
2527 dhcp_lastlease(void *arg
)
2529 struct interface
*ifp
= arg
;
2530 struct dhcp_state
*state
= D_STATE(ifp
);
2532 loginfox("%s: timed out contacting a DHCP server, using last lease",
2534 #if defined(ARP) || defined(KERNEL_RFC5227)
2539 /* Set expired here because dhcp_bind() -> ipv4_addaddr() will reset
2541 state
->added
|= STATE_EXPIRED
;
2542 state
->interval
= 0;
2547 dhcp_static(struct interface
*ifp
)
2549 struct if_options
*ifo
;
2550 struct dhcp_state
*state
;
2551 struct ipv4_addr
*ia
;
2553 state
= D_STATE(ifp
);
2557 if (ifo
->req_addr
.s_addr
== INADDR_ANY
&&
2558 (ia
= ipv4_iffindaddr(ifp
, NULL
, NULL
)) == NULL
)
2560 loginfox("%s: waiting for 3rd party to "
2561 "configure IP address", ifp
->name
);
2562 state
->reason
= "3RDPARTY";
2563 script_runreason(ifp
, state
->reason
);
2567 state
->offer_len
= dhcp_message_new(&state
->offer
,
2568 ia
? &ia
->addr
: &ifo
->req_addr
,
2569 ia
? &ia
->mask
: &ifo
->req_mask
);
2570 if (state
->offer_len
)
2571 #if defined(ARP) || defined(KERNEL_RFC5227)
2579 dhcp_inform(struct interface
*ifp
)
2581 struct dhcp_state
*state
;
2582 struct if_options
*ifo
;
2583 struct ipv4_addr
*ia
;
2585 state
= D_STATE(ifp
);
2589 state
->offer
= NULL
;
2590 state
->offer_len
= 0;
2592 if (ifo
->req_addr
.s_addr
== INADDR_ANY
) {
2593 ia
= ipv4_iffindaddr(ifp
, NULL
, NULL
);
2595 loginfox("%s: waiting for 3rd party to "
2596 "configure IP address",
2598 if (!(ifp
->ctx
->options
& DHCPCD_TEST
)) {
2599 state
->reason
= "3RDPARTY";
2600 script_runreason(ifp
, state
->reason
);
2605 ia
= ipv4_iffindaddr(ifp
, &ifo
->req_addr
, &ifo
->req_mask
);
2607 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
2608 logerrx("%s: cannot add IP address in test mode",
2612 ia
= ipv4_iffindaddr(ifp
, &ifo
->req_addr
, NULL
);
2614 /* Netmask must be different, delete it. */
2615 ipv4_deladdr(ia
, 1);
2616 state
->offer_len
= dhcp_message_new(&state
->offer
,
2617 &ifo
->req_addr
, &ifo
->req_mask
);
2619 if (dhcp_arp_address(ifp
) != 1)
2622 ia
= ipv4_iffindaddr(ifp
,
2623 &ifo
->req_addr
, &ifo
->req_mask
);
2628 state
->state
= DHS_INFORM
;
2630 state
->offer_len
= dhcp_message_new(&state
->offer
,
2631 &ia
->addr
, &ia
->mask
);
2632 if (state
->offer_len
) {
2634 get_lease(ifp
, &state
->lease
, state
->offer
, state
->offer_len
);
2640 dhcp_reboot_newopts(struct interface
*ifp
, unsigned long long oldopts
)
2642 struct if_options
*ifo
;
2643 struct dhcp_state
*state
= D_STATE(ifp
);
2645 if (state
== NULL
|| state
->state
== DHS_NONE
)
2648 if ((ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2649 (state
->addr
== NULL
||
2650 state
->addr
->addr
.s_addr
!= ifo
->req_addr
.s_addr
)) ||
2651 (oldopts
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2652 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))))
2654 dhcp_drop(ifp
, "EXPIRE");
2660 dhcp_activeaddr(const struct interface
*ifp
, const struct in_addr
*addr
)
2662 const struct interface
*ifp1
;
2663 const struct dhcp_state
*state
;
2665 TAILQ_FOREACH(ifp1
, ifp
->ctx
->ifaces
, next
) {
2668 if ((state
= D_CSTATE(ifp1
)) == NULL
)
2670 switch(state
->state
) {
2680 if (state
->lease
.addr
.s_addr
== addr
->s_addr
)
2688 dhcp_reboot(struct interface
*ifp
)
2690 struct if_options
*ifo
;
2691 struct dhcp_state
*state
= D_STATE(ifp
);
2693 struct ipv4_addr
*ia
;
2696 if (state
== NULL
|| state
->state
== DHS_NONE
)
2699 state
->state
= DHS_REBOOT
;
2700 state
->interval
= 0;
2702 if (ifo
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
)) {
2703 loginfox("%s: waiting for carrier", ifp
->name
);
2706 if (ifo
->options
& DHCPCD_STATIC
) {
2710 if (ifo
->options
& DHCPCD_INFORM
) {
2711 loginfox("%s: informing address of %s",
2712 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2716 if (ifo
->reboot
== 0 || state
->offer
== NULL
) {
2720 if (!IS_DHCP(state
->offer
))
2723 loginfox("%s: rebinding lease of %s",
2724 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2727 #ifndef KERNEL_RFC5227
2728 /* Create the DHCP ARP state so we can defend it. */
2729 (void)dhcp_arp_new(ifp
, &state
->lease
.addr
);
2732 /* If the address exists on the interface and no other interface
2733 * is currently using it then announce it to ensure this
2734 * interface gets the reply. */
2735 ia
= ipv4_iffindaddr(ifp
, &state
->lease
.addr
, NULL
);
2737 !(ifp
->ctx
->options
& DHCPCD_TEST
) &&
2738 #ifdef IN_IFF_NOTUSEABLE
2739 !(ia
->addr_flags
& IN_IFF_NOTUSEABLE
) &&
2741 dhcp_activeaddr(ifp
, &state
->lease
.addr
) == 0)
2742 arp_ifannounceaddr(ifp
, &state
->lease
.addr
);
2746 state
->lease
.server
.s_addr
= INADDR_ANY
;
2747 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2750 /* Need to add this before dhcp_expire and friends. */
2751 if (!ifo
->fallback
&& ifo
->options
& DHCPCD_IPV4LL
)
2752 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2753 ifo
->ipv4ll_time
, ipv4ll_start
, ifp
);
2756 if (ifo
->options
& DHCPCD_LASTLEASE
&& state
->lease
.frominfo
)
2757 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2758 ifo
->reboot
, dhcp_lastlease
, ifp
);
2759 else if (!(ifo
->options
& DHCPCD_INFORM
))
2760 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2761 ifo
->reboot
, dhcp_expire
, ifp
);
2763 /* Don't bother ARP checking as the server could NAK us first.
2764 * Don't call dhcp_request as that would change the state */
2769 dhcp_drop(struct interface
*ifp
, const char *reason
)
2771 struct dhcp_state
*state
= D_STATE(ifp
);
2773 /* dhcp_start may just have been called and we don't yet have a state
2774 * but we do have a timeout, so punt it. */
2775 if (state
== NULL
|| state
->state
== DHS_NONE
) {
2776 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2781 if (state
->addr
!= NULL
)
2782 arp_freeaddr(ifp
, &state
->addr
->addr
);
2785 state
->arping_index
= -1;
2788 if (ifp
->options
->options
& DHCPCD_RELEASE
&&
2789 !(ifp
->options
->options
& DHCPCD_INFORM
))
2791 /* Failure to send the release may cause this function to
2792 * re-enter so guard by setting the state. */
2793 if (state
->state
== DHS_RELEASE
)
2795 state
->state
= DHS_RELEASE
;
2797 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2798 if (if_is_link_up(ifp
) &&
2799 state
->new != NULL
&&
2800 state
->lease
.server
.s_addr
!= INADDR_ANY
)
2802 loginfox("%s: releasing lease of %s",
2803 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2805 send_message(ifp
, DHCP_RELEASE
, NULL
);
2809 else if (state
->auth
.reconf
!= NULL
) {
2811 * Drop the lease as the token may only be present
2812 * in the initial reply message and not subsequent
2814 * If dhcpcd is restarted, the token is lost.
2815 * XXX persist this in another file?
2817 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2821 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2823 dhcp_auth_reset(&state
->auth
);
2826 state
->state
= DHS_NONE
;
2828 state
->offer
= NULL
;
2829 state
->offer_len
= 0;
2831 state
->old
= state
->new;
2832 state
->old_len
= state
->new_len
;
2835 state
->reason
= reason
;
2836 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
2837 ipv4_applyaddr(ifp
);
2841 script_runreason(ifp
, state
->reason
);
2846 state
->lease
.addr
.s_addr
= 0;
2847 ifp
->options
->options
&= ~(DHCPCD_CSR_WARNED
|
2848 DHCPCD_ROUTER_HOST_ROUTE_WARNED
);
2850 /* Close DHCP ports so a changed interface family is picked
2851 * up by a new BPF state. */
2856 blacklisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2860 for (i
= 0; i
< ifo
->blacklist_len
; i
+= 2)
2861 if (ifo
->blacklist
[i
] == (addr
& ifo
->blacklist
[i
+ 1]))
2866 #define WHTLST_NONE 0
2867 #define WHTLST_MATCH 1
2868 #define WHTLST_NOMATCH 2
2870 whitelisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2874 if (ifo
->whitelist_len
== 0)
2876 for (i
= 0; i
< ifo
->whitelist_len
; i
+= 2)
2877 if (ifo
->whitelist
[i
] == (addr
& ifo
->whitelist
[i
+ 1]))
2878 return WHTLST_MATCH
;
2879 return WHTLST_NOMATCH
;
2883 log_dhcp(int loglevel
, const char *msg
,
2884 const struct interface
*ifp
, const struct bootp
*bootp
, size_t bootp_len
,
2885 const struct in_addr
*from
, int ad
)
2888 char *a
, sname
[sizeof(bootp
->sname
) * 4];
2889 struct in_addr addr
;
2893 if (strcmp(msg
, "NAK:") == 0) {
2894 a
= get_option_string(ifp
->ctx
, bootp
, bootp_len
, DHO_MESSAGE
);
2900 tmpl
= (al
* 4) + 1;
2907 print_string(tmp
, tmpl
, OT_STRING
, (uint8_t *)a
, al
);
2911 } else if (ad
&& bootp
->yiaddr
!= 0) {
2912 addr
.s_addr
= bootp
->yiaddr
;
2913 a
= strdup(inet_ntoa(addr
));
2922 r
= get_option_addr(ifp
->ctx
, &addr
, bootp
, bootp_len
, DHO_SERVERID
);
2923 if (get_option_uint8(ifp
->ctx
, &overl
, bootp
, bootp_len
,
2924 DHO_OPTSOVERLOADED
) == -1)
2926 if (bootp
->sname
[0] && r
== 0 && !(overl
& 2)) {
2927 print_string(sname
, sizeof(sname
), OT_STRING
| OT_DOMAIN
,
2928 bootp
->sname
, sizeof(bootp
->sname
));
2930 logmessage(loglevel
, "%s: %s %s %s %s",
2931 ifp
->name
, msg
, tfrom
, inet_ntoa(addr
), sname
);
2933 logmessage(loglevel
, "%s: %s %s %s %s %s",
2934 ifp
->name
, msg
, a
, tfrom
, inet_ntoa(addr
), sname
);
2941 logmessage(loglevel
, "%s: %s %s %s",
2942 ifp
->name
, msg
, tfrom
, inet_ntoa(addr
));
2944 logmessage(loglevel
, "%s: %s %s %s %s",
2945 ifp
->name
, msg
, a
, tfrom
, inet_ntoa(addr
));
2950 /* If we're sharing the same IP address with another interface on the
2951 * same network, we may receive the DHCP reply on the wrong interface.
2952 * Try and re-direct it here. */
2954 dhcp_redirect_dhcp(struct interface
*ifp
, struct bootp
*bootp
, size_t bootp_len
,
2955 const struct in_addr
*from
)
2957 struct interface
*ifn
;
2958 const struct dhcp_state
*state
;
2961 xid
= ntohl(bootp
->xid
);
2962 TAILQ_FOREACH(ifn
, ifp
->ctx
->ifaces
, next
) {
2965 state
= D_CSTATE(ifn
);
2966 if (state
== NULL
|| state
->state
== DHS_NONE
)
2968 if (state
->xid
!= xid
)
2970 if (ifn
->hwlen
<= sizeof(bootp
->chaddr
) &&
2971 memcmp(bootp
->chaddr
, ifn
->hwaddr
, ifn
->hwlen
))
2973 logdebugx("%s: redirecting DHCP message to %s",
2974 ifp
->name
, ifn
->name
);
2975 dhcp_handledhcp(ifn
, bootp
, bootp_len
, from
);
2980 dhcp_handledhcp(struct interface
*ifp
, struct bootp
*bootp
, size_t bootp_len
,
2981 const struct in_addr
*from
)
2983 struct dhcp_state
*state
= D_STATE(ifp
);
2984 struct if_options
*ifo
= ifp
->options
;
2985 struct dhcp_lease
*lease
= &state
->lease
;
2987 struct in_addr addr
;
2991 uint32_t v6only_time
= 0;
2992 bool use_v6only
= false;
2994 const uint8_t *auth
;
2997 #ifdef IN_IFF_DUPLICATED
2998 struct ipv4_addr
*ia
;
3001 #define LOGDHCP0(l, m) \
3002 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0)
3003 #define LOGDHCP(l, m) \
3004 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1)
3006 #define IS_STATE_ACTIVE(s) ((s)-state != DHS_NONE && \
3007 (s)->state != DHS_INIT && (s)->state != DHS_BOUND)
3009 if (bootp
->op
!= BOOTREPLY
) {
3010 if (IS_STATE_ACTIVE(state
))
3011 logdebugx("%s: op (%d) is not BOOTREPLY",
3012 ifp
->name
, bootp
->op
);
3016 if (state
->xid
!= ntohl(bootp
->xid
)) {
3017 if (IS_STATE_ACTIVE(state
))
3018 logdebugx("%s: wrong xid 0x%x (expecting 0x%x) from %s",
3019 ifp
->name
, ntohl(bootp
->xid
), state
->xid
,
3021 dhcp_redirect_dhcp(ifp
, bootp
, bootp_len
, from
);
3025 if (ifp
->hwlen
<= sizeof(bootp
->chaddr
) &&
3026 memcmp(bootp
->chaddr
, ifp
->hwaddr
, ifp
->hwlen
))
3028 if (IS_STATE_ACTIVE(state
)) {
3029 char buf
[sizeof(bootp
->chaddr
) * 3];
3031 logdebugx("%s: xid 0x%x is for hwaddr %s",
3032 ifp
->name
, ntohl(bootp
->xid
),
3033 hwaddr_ntoa(bootp
->chaddr
, sizeof(bootp
->chaddr
),
3036 dhcp_redirect_dhcp(ifp
, bootp
, bootp_len
, from
);
3043 i
= whitelisted_ip(ifp
->options
, from
->s_addr
);
3045 case WHTLST_NOMATCH
:
3046 logwarnx("%s: non whitelisted DHCP packet from %s",
3047 ifp
->name
, inet_ntoa(*from
));
3052 if (blacklisted_ip(ifp
->options
, from
->s_addr
) == 1) {
3053 logwarnx("%s: blacklisted DHCP packet from %s",
3054 ifp
->name
, inet_ntoa(*from
));
3059 /* We may have found a BOOTP server */
3060 if (get_option_uint8(ifp
->ctx
, &type
,
3061 bootp
, bootp_len
, DHO_MESSAGETYPE
) == -1)
3063 else if (ifo
->options
& DHCPCD_BOOTP
) {
3064 logdebugx("%s: ignoring DHCP reply (expecting BOOTP)",
3070 /* Authenticate the message */
3071 auth
= get_option(ifp
->ctx
, bootp
, bootp_len
,
3072 DHO_AUTHENTICATION
, &auth_len
);
3074 if (dhcp_auth_validate(&state
->auth
, &ifo
->auth
,
3075 (uint8_t *)bootp
, bootp_len
, 4, type
,
3076 auth
, auth_len
) == NULL
)
3078 LOGDHCP0(LOG_ERR
, "authentication failed");
3081 if (state
->auth
.token
)
3082 logdebugx("%s: validated using 0x%08" PRIu32
,
3083 ifp
->name
, state
->auth
.token
->secretid
);
3085 loginfox("%s: accepted reconfigure key", ifp
->name
);
3086 } else if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
3087 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
) {
3088 LOGDHCP0(LOG_ERR
, "no authentication");
3091 LOGDHCP0(LOG_WARNING
, "no authentication");
3096 if (type
== DHCP_FORCERENEW
) {
3097 if (from
->s_addr
== INADDR_ANY
||
3098 from
->s_addr
== INADDR_BROADCAST
)
3100 LOGDHCP(LOG_ERR
, "discarding Force Renew");
3105 LOGDHCP(LOG_ERR
, "unauthenticated Force Renew");
3106 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
)
3109 if (state
->state
!= DHS_BOUND
&& state
->state
!= DHS_INFORM
) {
3110 LOGDHCP(LOG_DEBUG
, "not bound, ignoring Force Renew");
3113 LOGDHCP(LOG_INFO
, "Force Renew from");
3114 /* The rebind and expire timings are still the same, we just
3115 * enter the renew state early */
3116 if (state
->state
== DHS_BOUND
)
3119 eloop_timeout_delete(ifp
->ctx
->eloop
,
3124 LOGDHCP(LOG_ERR
, "unauthenticated Force Renew");
3129 if (state
->state
== DHS_BOUND
) {
3130 LOGDHCP(LOG_DEBUG
, "bound, ignoring");
3134 if (state
->state
== DHS_PROBE
) {
3135 /* Ignore any DHCP messages whilst probing a lease to bind. */
3136 LOGDHCP(LOG_DEBUG
, "probing, ignoring");
3140 /* reset the message counter */
3141 state
->interval
= 0;
3143 /* Ensure that no reject options are present */
3144 for (i
= 1; i
< 255; i
++) {
3145 if (has_option_mask(ifo
->rejectmask
, i
) &&
3146 get_option_uint8(ifp
->ctx
, &tmp
,
3147 bootp
, bootp_len
, (uint8_t)i
) == 0)
3149 LOGDHCP(LOG_WARNING
, "reject DHCP");
3154 if (type
== DHCP_NAK
) {
3155 /* For NAK, only check if we require the ServerID */
3156 if (has_option_mask(ifo
->requiremask
, DHO_SERVERID
) &&
3157 get_option_addr(ifp
->ctx
, &addr
,
3158 bootp
, bootp_len
, DHO_SERVERID
) == -1)
3160 LOGDHCP(LOG_WARNING
, "reject NAK");
3164 /* We should restart on a NAK */
3165 LOGDHCP(LOG_WARNING
, "NAK:");
3166 if ((msg
= get_option_string(ifp
->ctx
,
3167 bootp
, bootp_len
, DHO_MESSAGE
)))
3169 logwarnx("%s: message: %s", ifp
->name
, msg
);
3172 if (state
->state
== DHS_INFORM
) /* INFORM should not be NAKed */
3174 if (!(ifp
->ctx
->options
& DHCPCD_TEST
)) {
3175 dhcp_drop(ifp
, "NAK");
3176 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3179 /* If we constantly get NAKS then we should slowly back off */
3180 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3181 state
->nakoff
, dhcp_discover
, ifp
);
3182 if (state
->nakoff
== 0)
3186 if (state
->nakoff
> NAKOFF_MAX
)
3187 state
->nakoff
= NAKOFF_MAX
;
3192 /* Ensure that all required options are present */
3193 for (i
= 1; i
< 255; i
++) {
3194 if (has_option_mask(ifo
->requiremask
, i
) &&
3195 get_option_uint8(ifp
->ctx
, &tmp
,
3196 bootp
, bootp_len
, (uint8_t)i
) != 0)
3198 /* If we are BOOTP, then ignore the need for serverid.
3199 * To ignore BOOTP, require dhcp_message_type.
3200 * However, nothing really stops BOOTP from providing
3201 * DHCP style options as well so the above isn't
3203 if (type
== 0 && i
== DHO_SERVERID
)
3205 LOGDHCP(LOG_WARNING
, "reject DHCP");
3210 if (has_option_mask(ifo
->requestmask
, DHO_IPV6_PREFERRED_ONLY
)) {
3211 if (get_option_uint32(ifp
->ctx
, &v6only_time
, bootp
, bootp_len
,
3212 DHO_IPV6_PREFERRED_ONLY
) == 0 && (state
->state
== DHS_DISCOVER
||
3213 state
->state
== DHS_REBOOT
|| state
->state
== DHS_NONE
))
3218 if (v6only_time
< MIN_V6ONLY_WAIT
)
3219 v6only_time
= MIN_V6ONLY_WAIT
;
3220 snprintf(v6msg
, sizeof(v6msg
),
3221 "IPv6-Only Preferred received (%u seconds)",
3223 LOGDHCP(LOG_INFO
, v6msg
);
3227 /* DHCP Auto-Configure, RFC 2563 */
3228 if (type
== DHCP_OFFER
&& bootp
->yiaddr
== 0) {
3229 LOGDHCP(LOG_WARNING
, "no address given");
3230 if ((msg
= get_option_string(ifp
->ctx
,
3231 bootp
, bootp_len
, DHO_MESSAGE
)))
3233 logwarnx("%s: message: %s", ifp
->name
, msg
);
3237 if (state
->state
== DHS_DISCOVER
&&
3238 get_option_uint8(ifp
->ctx
, &tmp
, bootp
, bootp_len
,
3239 DHO_AUTOCONFIGURE
) == 0)
3243 LOGDHCP(LOG_WARNING
, "IPv4LL disabled from");
3250 LOGDHCP(LOG_WARNING
, "IPv4LL enabled from");
3254 logerrx("%s: unknown auto configuration "
3259 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3260 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3261 use_v6only
? v6only_time
: DHCP_MAX
,
3262 dhcp_discover
, ifp
);
3269 dhcp_drop(ifp
, "EXPIRE");
3270 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3271 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3272 eloop_timeout_add_sec(ifp
->ctx
->eloop
, v6only_time
,
3273 dhcp_discover
, ifp
);
3277 /* Ensure that the address offered is valid */
3278 if ((type
== 0 || type
== DHCP_OFFER
|| type
== DHCP_ACK
) &&
3279 (bootp
->ciaddr
== INADDR_ANY
|| bootp
->ciaddr
== INADDR_BROADCAST
)
3281 (bootp
->yiaddr
== INADDR_ANY
|| bootp
->yiaddr
== INADDR_BROADCAST
))
3283 LOGDHCP(LOG_WARNING
, "reject invalid address");
3287 #ifdef IN_IFF_DUPLICATED
3288 ia
= ipv4_iffindaddr(ifp
, &lease
->addr
, NULL
);
3289 if (ia
&& ia
->addr_flags
& IN_IFF_DUPLICATED
) {
3290 LOGDHCP(LOG_WARNING
, "declined duplicate address");
3293 ipv4_deladdr(ia
, 0);
3294 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3295 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3296 DHCP_RAND_MAX
, dhcp_discover
, ifp
);
3301 bootp_copied
= false;
3302 if ((type
== 0 || type
== DHCP_OFFER
) && state
->state
== DHS_DISCOVER
) {
3303 lease
->frominfo
= 0;
3304 lease
->addr
.s_addr
= bootp
->yiaddr
;
3305 memcpy(&lease
->cookie
, bootp
->vend
, sizeof(lease
->cookie
));
3307 get_option_addr(ifp
->ctx
,
3308 &lease
->server
, bootp
, bootp_len
, DHO_SERVERID
) != 0)
3309 lease
->server
.s_addr
= INADDR_ANY
;
3311 /* Test for rapid commit in the OFFER */
3312 if (!(ifp
->ctx
->options
& DHCPCD_TEST
) &&
3313 has_option_mask(ifo
->requestmask
, DHO_RAPIDCOMMIT
) &&
3314 get_option(ifp
->ctx
, bootp
, bootp_len
,
3315 DHO_RAPIDCOMMIT
, NULL
))
3317 state
->state
= DHS_REQUEST
;
3321 LOGDHCP(LOG_INFO
, "offered");
3322 if (state
->offer_len
< bootp_len
) {
3324 if ((state
->offer
= malloc(bootp_len
)) == NULL
) {
3326 state
->offer_len
= 0;
3330 state
->offer_len
= bootp_len
;
3331 memcpy(state
->offer
, bootp
, bootp_len
);
3332 bootp_copied
= true;
3333 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
3335 state
->old
= state
->new;
3336 state
->old_len
= state
->new_len
;
3337 state
->new = state
->offer
;
3338 state
->new_len
= state
->offer_len
;
3339 state
->offer
= NULL
;
3340 state
->offer_len
= 0;
3341 state
->reason
= "TEST";
3342 script_runreason(ifp
, state
->reason
);
3343 eloop_exit(ifp
->ctx
->eloop
, EXIT_SUCCESS
);
3345 state
->bpf
->bpf_flags
|= BPF_EOF
;
3348 eloop_timeout_delete(ifp
->ctx
->eloop
, send_discover
, ifp
);
3349 /* We don't request BOOTP addresses */
3351 /* We used to ARP check here, but that seems to be in
3352 * violation of RFC2131 where it only describes
3353 * DECLINE after REQUEST.
3354 * It also seems that some MS DHCP servers actually
3355 * ignore DECLINE if no REQUEST, ie we decline a
3363 if (type
== DHCP_OFFER
) {
3364 LOGDHCP(LOG_WARNING
, "ignoring offer of");
3368 /* We should only be dealing with acks */
3369 if (type
!= DHCP_ACK
) {
3370 LOGDHCP(LOG_ERR
, "not ACK or OFFER");
3374 if (state
->state
== DHS_DISCOVER
) {
3375 /* We only allow ACK of rapid commit DISCOVER. */
3376 if (has_option_mask(ifo
->requestmask
,
3378 get_option(ifp
->ctx
, bootp
, bootp_len
,
3379 DHO_RAPIDCOMMIT
, NULL
))
3380 state
->state
= DHS_REQUEST
;
3382 LOGDHCP(LOG_DEBUG
, "ignoring ack of");
3388 if (!(ifo
->options
& DHCPCD_INFORM
))
3389 LOGDHCP(LOG_DEBUG
, "acknowledged");
3391 ifo
->options
&= ~DHCPCD_STATIC
;
3394 /* No NAK, so reset the backoff
3395 * We don't reset on an OFFER message because the server could
3396 * potentially NAK the REQUEST. */
3399 /* BOOTP could have already assigned this above. */
3400 if (!bootp_copied
) {
3401 if (state
->offer_len
< bootp_len
) {
3403 if ((state
->offer
= malloc(bootp_len
)) == NULL
) {
3405 state
->offer_len
= 0;
3409 state
->offer_len
= bootp_len
;
3410 memcpy(state
->offer
, bootp
, bootp_len
);
3413 lease
->frominfo
= 0;
3414 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3416 #if defined(ARP) || defined(KERNEL_RFC5227)
3424 get_udp_data(void *packet
, size_t *len
)
3426 const struct ip
*ip
= packet
;
3427 size_t ip_hl
= (size_t)ip
->ip_hl
* 4;
3430 p
+= ip_hl
+ sizeof(struct udphdr
);
3431 *len
= (size_t)ntohs(ip
->ip_len
) - sizeof(struct udphdr
) - ip_hl
;
3436 is_packet_udp_bootp(void *packet
, size_t plen
)
3438 struct ip
*ip
= packet
;
3442 if (plen
< sizeof(*ip
))
3445 if (ip
->ip_v
!= IPVERSION
|| ip
->ip_p
!= IPPROTO_UDP
)
3449 if (ntohs(ip
->ip_len
) > plen
)
3452 ip_hlen
= (size_t)ip
->ip_hl
* 4;
3453 if (ip_hlen
< sizeof(*ip
))
3456 /* Check we have a UDP header and BOOTP. */
3457 if (ip_hlen
+ sizeof(udp
) + offsetof(struct bootp
, vend
) > plen
)
3461 memcpy(&udp
, (char *)ip
+ ip_hlen
, sizeof(udp
));
3462 if (ntohs(udp
.uh_ulen
) < sizeof(udp
))
3464 if (ip_hlen
+ ntohs(udp
.uh_ulen
) > plen
)
3467 /* Check it's to the right port. */
3468 if (udp
.uh_dport
!= htons(BOOTPC
))
3474 /* Lengths have already been checked. */
3476 checksums_valid(void *packet
,
3477 struct in_addr
*from
, unsigned int flags
)
3479 struct ip
*ip
= packet
;
3482 uint16_t w
[sizeof(struct ip
) / 2];
3485 .ip_p
= IPPROTO_UDP
,
3486 .ip_src
= ip
->ip_src
,
3487 .ip_dst
= ip
->ip_dst
,
3492 char *udpp
, *uh_sump
;
3496 from
->s_addr
= ip
->ip_src
.s_addr
;
3498 ip_hlen
= (size_t)ip
->ip_hl
* 4;
3499 if (in_cksum(ip
, ip_hlen
, NULL
) != 0)
3502 if (flags
& BPF_PARTIALCSUM
)
3505 udpp
= (char *)ip
+ ip_hlen
;
3506 memcpy(&udp
, udpp
, sizeof(udp
));
3507 if (udp
.uh_sum
== 0)
3510 /* UDP checksum is based on a pseudo IP header alongside
3511 * the UDP header and payload. */
3512 pip
.ip
.ip_len
= udp
.uh_ulen
;
3515 /* Need to zero the UDP sum in the packet for the checksum to work. */
3516 uh_sump
= udpp
+ offsetof(struct udphdr
, uh_sum
);
3517 memset(uh_sump
, 0, sizeof(udp
.uh_sum
));
3519 /* Checksum pseudo header and then UDP + payload. */
3520 in_cksum(pip
.w
, sizeof(pip
.w
), &csum
);
3521 csum
= in_cksum(udpp
, ntohs(udp
.uh_ulen
), &csum
);
3523 #if 0 /* Not needed, just here for completeness. */
3524 /* Put the checksum back. */
3525 memcpy(uh_sump
, &udp
.uh_sum
, sizeof(udp
.uh_sum
));
3528 return csum
== udp
.uh_sum
;
3532 dhcp_handlebootp(struct interface
*ifp
, struct bootp
*bootp
, size_t len
,
3533 struct in_addr
*from
)
3537 /* Unlikely, but appeases sanitizers. */
3538 if (len
> FRAMELEN_MAX
) {
3539 logerrx("%s: packet exceeded frame length (%zu) from %s",
3540 ifp
->name
, len
, inet_ntoa(*from
));
3544 /* To make our IS_DHCP macro easy, ensure the vendor
3545 * area has at least 4 octets. */
3546 v
= len
- offsetof(struct bootp
, vend
);
3548 bootp
->vend
[v
++] = '\0';
3552 dhcp_handledhcp(ifp
, bootp
, len
, from
);
3556 dhcp_packet(struct interface
*ifp
, uint8_t *data
, size_t len
,
3557 unsigned int bpf_flags
)
3559 struct bootp
*bootp
;
3560 struct in_addr from
;
3562 size_t fl
= bpf_frame_header_len(ifp
);
3564 const struct dhcp_state
*state
= D_CSTATE(ifp
);
3566 /* It's possible that an interface departs and arrives in short
3567 * order to receive a BPF frame out of order.
3568 * There is a similar check in ARP, but much lower down the stack.
3569 * It's not needed for other inet protocols because we send the
3570 * message as a whole and select the interface off that and then
3571 * check state. BPF on the other hand is very interface
3572 * specific and we do need this check. */
3576 /* Ignore double reads */
3577 if (IN_PRIVSEP(ifp
->ctx
)) {
3578 switch (state
->state
) {
3579 case DHS_BOUND
: /* FALLTHROUGH */
3588 /* Trim frame header */
3591 logerrx("%s: %s: short frame header %zu",
3592 __func__
, ifp
->name
, len
);
3596 /* Move the data to avoid alignment errors. */
3597 memmove(data
, data
+ fl
, len
);
3600 /* Validate filter. */
3601 if (!is_packet_udp_bootp(data
, len
)) {
3603 logerrx("%s: DHCP BPF validation failure", ifp
->name
);
3608 if (!checksums_valid(data
, &from
, bpf_flags
)) {
3609 logerrx("%s: checksum failure from %s",
3610 ifp
->name
, inet_ntoa(from
));
3615 * DHCP has a variable option area rather than a fixed vendor area.
3616 * Because DHCP uses the BOOTP protocol it should still send BOOTP
3617 * sized packets to be RFC compliant.
3618 * However some servers send a truncated vendor area.
3619 * dhcpcd can work fine without the vendor area being sent.
3621 bootp
= get_udp_data(data
, &udp_len
);
3622 dhcp_handlebootp(ifp
, bootp
, udp_len
, &from
);
3626 dhcp_readbpf(void *arg
, unsigned short events
)
3628 struct interface
*ifp
= arg
;
3629 uint8_t buf
[FRAMELEN_MAX
];
3631 struct dhcp_state
*state
= D_STATE(ifp
);
3632 struct bpf
*bpf
= state
->bpf
;
3634 if (events
!= ELE_READ
)
3635 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
3637 bpf
->bpf_flags
&= ~BPF_EOF
;
3638 while (!(bpf
->bpf_flags
& BPF_EOF
)) {
3639 bytes
= bpf_read(bpf
, buf
, sizeof(buf
));
3641 if (state
->state
!= DHS_NONE
) {
3642 logerr("%s: %s", __func__
, ifp
->name
);
3647 dhcp_packet(ifp
, buf
, (size_t)bytes
, bpf
->bpf_flags
);
3648 /* Check we still have a state after processing. */
3649 if ((state
= D_STATE(ifp
)) == NULL
)
3651 if ((bpf
= state
->bpf
) == NULL
)
3657 dhcp_recvmsg(struct dhcpcd_ctx
*ctx
, struct msghdr
*msg
)
3659 struct sockaddr_in
*from
= (struct sockaddr_in
*)msg
->msg_name
;
3660 struct iovec
*iov
= &msg
->msg_iov
[0];
3661 struct interface
*ifp
;
3662 const struct dhcp_state
*state
;
3664 ifp
= if_findifpfromcmsg(ctx
, msg
, NULL
);
3670 if (iov
->iov_len
< offsetof(struct bootp
, vend
)) {
3671 logerrx("%s: truncated packet (%zu) from %s",
3672 ifp
->name
, iov
->iov_len
, inet_ntoa(from
->sin_addr
));
3676 state
= D_CSTATE(ifp
);
3677 if (state
== NULL
) {
3678 /* Try re-directing it to another interface. */
3679 dhcp_redirect_dhcp(ifp
, (struct bootp
*)iov
->iov_base
,
3680 iov
->iov_len
, &from
->sin_addr
);
3684 if (state
->bpf
!= NULL
) {
3685 /* Avoid a duplicate read if BPF is open for the interface. */
3689 if (IN_PRIVSEP(ctx
)) {
3690 switch (state
->state
) {
3691 case DHS_BOUND
: /* FALLTHROUGH */
3695 /* Any other state we ignore it or will receive
3702 dhcp_handlebootp(ifp
, iov
->iov_base
, iov
->iov_len
,
3707 dhcp_readudp(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
,
3708 unsigned short events
)
3710 const struct dhcp_state
*state
;
3711 struct sockaddr_in from
;
3714 uint8_t buf
[10 * 1024]; /* Maximum MTU */
3716 struct iovec iov
= {
3717 .iov_base
= iovbuf
.buf
,
3718 .iov_len
= sizeof(iovbuf
.buf
),
3723 uint8_t buf
[CMSG_SPACE(sizeof(struct sockaddr_dl
))];
3725 uint8_t buf
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
3727 } cmsgbuf
= { .buf
= { 0 } };
3728 struct msghdr msg
= {
3729 .msg_name
= &from
, .msg_namelen
= sizeof(from
),
3730 .msg_iov
= &iov
, .msg_iovlen
= 1,
3731 .msg_control
= cmsgbuf
.buf
, .msg_controllen
= sizeof(cmsgbuf
.buf
),
3736 if (events
!= ELE_READ
)
3737 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
3740 state
= D_CSTATE(ifp
);
3745 bytes
= recvmsg(s
, &msg
, 0);
3751 iov
.iov_len
= (size_t)bytes
;
3752 dhcp_recvmsg(ctx
, &msg
);
3756 dhcp_handleudp(void *arg
, unsigned short events
)
3758 struct dhcpcd_ctx
*ctx
= arg
;
3760 dhcp_readudp(ctx
, NULL
, events
);
3764 dhcp_handleifudp(void *arg
, unsigned short events
)
3766 struct interface
*ifp
= arg
;
3768 dhcp_readudp(ifp
->ctx
, ifp
, events
);
3772 dhcp_openbpf(struct interface
*ifp
)
3774 struct dhcp_state
*state
;
3776 state
= D_STATE(ifp
);
3779 if (IN_PRIVSEP_SE(ifp
->ctx
)) {
3780 if (ps_bpf_openbootp(ifp
) == -1) {
3788 if (state
->bpf
!= NULL
)
3791 state
->bpf
= bpf_open(ifp
, bpf_bootp
, NULL
);
3792 if (state
->bpf
== NULL
) {
3793 if (errno
== ENOENT
) {
3794 logerrx("%s not found", bpf_name
);
3795 /* May as well disable IPv4 entirely at
3796 * this point as we really need it. */
3797 ifp
->options
->options
&= ~DHCPCD_IPV4
;
3799 logerr("%s: %s", __func__
, ifp
->name
);
3803 if (eloop_event_add(ifp
->ctx
->eloop
, state
->bpf
->bpf_fd
, ELE_READ
,
3804 dhcp_readbpf
, ifp
) == -1)
3805 logerr("%s: eloop_event_add", __func__
);
3810 dhcp_free(struct interface
*ifp
)
3812 struct dhcp_state
*state
= D_STATE(ifp
);
3813 struct dhcpcd_ctx
*ctx
;
3820 state
->state
= DHS_NONE
;
3824 free(state
->clientid
);
3829 /* If we don't have any more DHCP enabled interfaces,
3830 * close the global socket and release resources */
3832 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
3833 state
= D_STATE(ifp
);
3834 if (state
!= NULL
&& state
->state
!= DHS_NONE
)
3839 if (ctx
->udp_rfd
!= -1) {
3840 eloop_event_delete(ctx
->eloop
, ctx
->udp_rfd
);
3841 close(ctx
->udp_rfd
);
3844 if (ctx
->udp_wfd
!= -1) {
3845 close(ctx
->udp_wfd
);
3849 free(ctx
->opt_buffer
);
3850 ctx
->opt_buffer
= NULL
;
3851 ctx
->opt_buffer_len
= 0;
3856 dhcp_initstate(struct interface
*ifp
)
3858 struct dhcp_state
*state
;
3860 state
= D_STATE(ifp
);
3864 ifp
->if_data
[IF_DATA_DHCP
] = calloc(1, sizeof(*state
));
3865 state
= D_STATE(ifp
);
3869 state
->state
= DHS_NONE
;
3870 /* 0 is a valid fd, so init to -1 */
3871 state
->udp_rfd
= -1;
3873 state
->arping_index
= -1;
3879 dhcp_init(struct interface
*ifp
)
3881 struct dhcp_state
*state
;
3882 struct if_options
*ifo
;
3884 char buf
[(sizeof(ifo
->clientid
) - 1) * 3];
3886 if (dhcp_initstate(ifp
) == -1)
3889 state
= D_STATE(ifp
);
3890 state
->state
= DHS_INIT
;
3891 state
->reason
= "PREINIT";
3893 dhcp_set_leasefile(state
->leasefile
, sizeof(state
->leasefile
),
3897 /* We need to drop the leasefile so that dhcp_start
3898 * doesn't load it. */
3899 if (ifo
->options
& DHCPCD_REQUEST
)
3900 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3902 free(state
->clientid
);
3903 state
->clientid
= NULL
;
3905 if (ifo
->options
& DHCPCD_ANONYMOUS
) {
3906 /* Removing the option could show that we want anonymous.
3907 * As such keep it as it's already in the hwaddr field. */
3909 } else if (*ifo
->clientid
) {
3910 state
->clientid
= malloc((size_t)(ifo
->clientid
[0] + 1));
3911 if (state
->clientid
== NULL
)
3913 memcpy(state
->clientid
, ifo
->clientid
,
3914 (size_t)(ifo
->clientid
[0]) + 1);
3915 } else if (ifo
->options
& DHCPCD_CLIENTID
) {
3916 if (ifo
->options
& DHCPCD_DUID
) {
3917 state
->clientid
= malloc(ifp
->ctx
->duid_len
+ 6);
3918 if (state
->clientid
== NULL
)
3920 state
->clientid
[0] =(uint8_t)(ifp
->ctx
->duid_len
+ 5);
3921 state
->clientid
[1] = 255; /* RFC 4361 */
3922 memcpy(state
->clientid
+ 2, ifo
->iaid
, 4);
3923 memcpy(state
->clientid
+ 6, ifp
->ctx
->duid
,
3924 ifp
->ctx
->duid_len
);
3927 len
= (uint8_t)(ifp
->hwlen
+ 1);
3928 state
->clientid
= malloc((size_t)len
+ 1);
3929 if (state
->clientid
== NULL
)
3931 state
->clientid
[0] = len
;
3932 state
->clientid
[1] = (uint8_t)ifp
->hwtype
;
3933 memcpy(state
->clientid
+ 2, ifp
->hwaddr
,
3938 if (ifo
->options
& DHCPCD_DUID
)
3939 /* Don't bother logging as DUID and IAID are reported
3940 * at device start. */
3943 if (ifo
->options
& DHCPCD_CLIENTID
&& state
->clientid
!= NULL
)
3944 logdebugx("%s: using ClientID %s", ifp
->name
,
3945 hwaddr_ntoa(state
->clientid
+ 1, state
->clientid
[0],
3947 else if (ifp
->hwlen
)
3948 logdebugx("%s: using hwaddr %s", ifp
->name
,
3949 hwaddr_ntoa(ifp
->hwaddr
, ifp
->hwlen
, buf
, sizeof(buf
)));
3958 dhcp_start1(void *arg
)
3960 struct interface
*ifp
= arg
;
3961 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
3962 struct if_options
*ifo
= ifp
->options
;
3963 struct dhcp_state
*state
;
3967 if (!(ifo
->options
& DHCPCD_IPV4
))
3970 /* Listen on *.*.*.*:bootpc so that the kernel never sends an
3971 * ICMP port unreachable message back to the DHCP server.
3972 * Only do this in manager mode so we don't swallow messages
3973 * for dhcpcd running on another interface. */
3974 if ((ctx
->options
& (DHCPCD_MANAGER
|DHCPCD_PRIVSEP
)) == DHCPCD_MANAGER
3975 && ctx
->udp_rfd
== -1)
3977 ctx
->udp_rfd
= dhcp_openudp(NULL
);
3978 if (ctx
->udp_rfd
== -1) {
3982 if (eloop_event_add(ctx
->eloop
, ctx
->udp_rfd
, ELE_READ
,
3983 dhcp_handleudp
, ctx
) == -1)
3984 logerr("%s: eloop_event_add", __func__
);
3986 if (!IN_PRIVSEP(ctx
) && ctx
->udp_wfd
== -1) {
3987 ctx
->udp_wfd
= xsocket(PF_INET
, SOCK_RAW
|SOCK_CXNB
,IPPROTO_UDP
);
3988 if (ctx
->udp_wfd
== -1) {
3994 if (dhcp_init(ifp
) == -1) {
3995 logerr("%s: dhcp_init", ifp
->name
);
3999 state
= D_STATE(ifp
);
4000 clock_gettime(CLOCK_MONOTONIC
, &state
->started
);
4001 state
->interval
= 0;
4003 state
->offer
= NULL
;
4004 state
->offer_len
= 0;
4007 if (ifo
->arping_len
&& state
->arping_index
< ifo
->arping_len
) {
4013 if (ifo
->options
& DHCPCD_STATIC
) {
4018 if (ifo
->options
& DHCPCD_INFORM
) {
4023 /* We don't want to read the old lease if we NAK an old test */
4024 nolease
= state
->offer
&& ifp
->ctx
->options
& DHCPCD_TEST
;
4025 if (!nolease
&& ifo
->options
& DHCPCD_DHCP
) {
4026 state
->offer_len
= read_lease(ifp
, &state
->offer
);
4027 /* Check the saved lease matches the type we want */
4029 #ifdef IN_IFF_DUPLICATED
4030 struct in_addr addr
;
4031 struct ipv4_addr
*ia
;
4033 addr
.s_addr
= state
->offer
->yiaddr
;
4034 ia
= ipv4_iffindaddr(ifp
, &addr
, NULL
);
4037 if ((!IS_DHCP(state
->offer
) &&
4038 !(ifo
->options
& DHCPCD_BOOTP
)) ||
4039 #ifdef IN_IFF_DUPLICATED
4040 (ia
&& ia
->addr_flags
& IN_IFF_DUPLICATED
) ||
4042 (IS_DHCP(state
->offer
) &&
4043 ifo
->options
& DHCPCD_BOOTP
))
4046 state
->offer
= NULL
;
4047 state
->offer_len
= 0;
4052 struct ipv4_addr
*ia
;
4055 get_lease(ifp
, &state
->lease
, state
->offer
, state
->offer_len
);
4056 state
->lease
.frominfo
= 1;
4057 if (state
->new == NULL
&&
4058 (ia
= ipv4_iffindaddr(ifp
,
4059 &state
->lease
.addr
, &state
->lease
.mask
)) != NULL
)
4061 /* We still have the IP address from the last lease.
4062 * Fake add the address and routes from it so the lease
4063 * can be cleaned up. */
4064 state
->new = malloc(state
->offer_len
);
4067 state
->offer
, state
->offer_len
);
4068 state
->new_len
= state
->offer_len
;
4070 state
->added
|= STATE_ADDED
| STATE_FAKE
;
4071 rt_build(ifp
->ctx
, AF_INET
);
4075 if (!IS_DHCP(state
->offer
)) {
4077 state
->offer
= NULL
;
4078 state
->offer_len
= 0;
4079 } else if (!(ifo
->options
& DHCPCD_LASTLEASE_EXTEND
) &&
4080 state
->lease
.leasetime
!= DHCP_INFINITE_LIFETIME
&&
4081 dhcp_filemtime(ifp
->ctx
, state
->leasefile
, &mtime
) == 0)
4085 /* Offset lease times and check expiry */
4088 (time_t)state
->lease
.leasetime
< now
- mtime
)
4090 logdebugx("%s: discarding expired lease",
4093 state
->offer
= NULL
;
4094 state
->offer_len
= 0;
4095 state
->lease
.addr
.s_addr
= 0;
4096 /* Technically we should discard the lease
4097 * as it's expired, just as DHCPv6 addresses
4098 * would be by the kernel.
4099 * However, this may violate POLA so
4100 * we currently leave it be.
4101 * If we get a totally different lease from
4102 * the DHCP server we'll drop it anyway, as
4103 * we will on any other event which would
4104 * trigger a lease drop.
4105 * This should only happen if dhcpcd stops
4106 * running and the lease expires before
4107 * dhcpcd starts again. */
4110 dhcp_drop(ifp
, "EXPIRE");
4113 l
= (uint32_t)(now
- mtime
);
4114 state
->lease
.leasetime
-= l
;
4115 state
->lease
.renewaltime
-= l
;
4116 state
->lease
.rebindtime
-= l
;
4122 if (!(ifo
->options
& DHCPCD_DHCP
)) {
4123 if (ifo
->options
& DHCPCD_IPV4LL
)
4129 if (state
->offer
== NULL
||
4130 !IS_DHCP(state
->offer
) ||
4131 ifo
->options
& DHCPCD_ANONYMOUS
)
4138 dhcp_start(struct interface
*ifp
)
4142 const struct dhcp_state
*state
;
4145 if (!(ifp
->options
->options
& DHCPCD_IPV4
))
4148 /* If we haven't been given a netmask for our requested address,
4150 if (ifp
->options
->req_addr
.s_addr
!= INADDR_ANY
&&
4151 ifp
->options
->req_mask
.s_addr
== INADDR_ANY
)
4152 ifp
->options
->req_mask
.s_addr
=
4153 ipv4_getnetmask(ifp
->options
->req_addr
.s_addr
);
4155 /* If we haven't specified a ClientID and our hardware address
4156 * length is greater than BOOTP CHADDR then we enforce a ClientID
4157 * of the hardware address type and the hardware address.
4158 * If there is no hardware address and no ClientID set,
4159 * force a DUID based ClientID. */
4160 if (ifp
->hwlen
> 16)
4161 ifp
->options
->options
|= DHCPCD_CLIENTID
;
4162 else if (ifp
->hwlen
== 0 && !(ifp
->options
->options
& DHCPCD_CLIENTID
))
4163 ifp
->options
->options
|= DHCPCD_CLIENTID
| DHCPCD_DUID
;
4165 /* Firewire and InfiniBand interfaces require ClientID and
4166 * the broadcast option being set. */
4167 switch (ifp
->hwtype
) {
4168 case ARPHRD_IEEE1394
: /* FALLTHROUGH */
4169 case ARPHRD_INFINIBAND
:
4170 ifp
->options
->options
|= DHCPCD_CLIENTID
| DHCPCD_BROADCAST
;
4174 /* If we violate RFC2131 section 3.7 then require ARP
4175 * to detect if any other client wants our address. */
4176 if (ifp
->options
->options
& DHCPCD_LASTLEASE_EXTEND
)
4177 ifp
->options
->options
|= DHCPCD_ARP
;
4179 /* No point in delaying a static configuration */
4180 if (ifp
->options
->options
& DHCPCD_STATIC
||
4181 !(ifp
->options
->options
& DHCPCD_INITIAL_DELAY
))
4188 /* If we have arpinged then we have already delayed. */
4189 state
= D_CSTATE(ifp
);
4190 if (state
!= NULL
&& state
->arping_index
!= -1) {
4195 delay
= MSEC_PER_SEC
+
4196 (arc4random_uniform(MSEC_PER_SEC
* 2) - MSEC_PER_SEC
);
4197 logdebugx("%s: delaying IPv4 for %0.1f seconds",
4198 ifp
->name
, (float)delay
/ MSEC_PER_SEC
);
4200 eloop_timeout_add_msec(ifp
->ctx
->eloop
, delay
, dhcp_start1
, ifp
);
4204 dhcp_abort(struct interface
*ifp
)
4206 struct dhcp_state
*state
;
4208 state
= D_STATE(ifp
);
4211 state
->arping_index
= -1;
4214 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp_start1
, ifp
);
4216 if (state
!= NULL
&& state
->added
) {
4217 rt_build(ifp
->ctx
, AF_INET
);
4219 if (ifp
->options
->options
& DHCPCD_ARP
)
4220 arp_announceaddr(ifp
->ctx
, &state
->addr
->addr
);
4226 dhcp_handleifa(int cmd
, struct ipv4_addr
*ia
, pid_t pid
)
4228 struct interface
*ifp
;
4229 struct dhcp_state
*state
;
4230 struct if_options
*ifo
;
4234 state
= D_STATE(ifp
);
4235 if (state
== NULL
|| state
->state
== DHS_NONE
)
4238 if (cmd
== RTM_DELADDR
) {
4239 if (state
->addr
== ia
) {
4240 loginfox("%s: pid %d deleted IP address %s",
4241 ifp
->name
, pid
, ia
->saddr
);
4244 /* Don't clear the added state as we need
4245 * to drop the lease. */
4246 dhcp_drop(ifp
, "EXPIRE");
4252 if (cmd
!= RTM_NEWADDR
)
4255 #ifdef IN_IFF_NOTUSEABLE
4256 if (!(ia
->addr_flags
& IN_IFF_NOTUSEABLE
))
4257 dhcp_finish_dad(ifp
, &ia
->addr
);
4258 else if (ia
->addr_flags
& IN_IFF_DUPLICATED
)
4259 return dhcp_addr_duplicated(ifp
, &ia
->addr
) ? NULL
: ia
;
4265 if (IN_PRIVSEP_SE(ifp
->ctx
) &&
4266 !(ifp
->ctx
->options
& (DHCPCD_MANAGER
| DHCPCD_CONFIGURE
)) &&
4267 IN_ARE_ADDR_EQUAL(&state
->lease
.addr
, &ia
->addr
))
4270 state
->added
= STATE_ADDED
;
4272 if (ps_inet_openbootp(ia
) == -1)
4277 /* If we have requested a specific address, return now.
4278 * The below code is only for when inform or static has been
4279 * requested without a specific address. */
4280 if (ifo
->req_addr
.s_addr
!= INADDR_ANY
)
4283 /* Only inform if we are NOT in the inform state or bound. */
4284 if (ifo
->options
& DHCPCD_INFORM
) {
4285 if (state
->state
!= DHS_INFORM
&& state
->state
!= DHS_BOUND
)
4290 /* Static and inform are mutually exclusive. If not static, return. */
4291 if (!(ifo
->options
& DHCPCD_STATIC
))
4295 state
->old
= state
->new;
4296 state
->new_len
= dhcp_message_new(&state
->new, &ia
->addr
, &ia
->mask
);
4297 if (state
->new == NULL
)
4300 if (ifp
->flags
& IFF_POINTOPOINT
) {
4301 for (i
= 1; i
< 255; i
++)
4302 if (i
!= DHO_ROUTER
&& has_option_mask(ifo
->dstmask
,i
))
4303 dhcp_message_add_addr(state
->new, i
, ia
->brd
);
4306 state
->reason
= "STATIC";
4307 rt_build(ifp
->ctx
, AF_INET
);
4308 script_runreason(ifp
, state
->reason
);
4315 dhcp_dump(struct interface
*ifp
)
4317 struct dhcp_state
*state
;
4319 ifp
->if_data
[IF_DATA_DHCP
] = state
= calloc(1, sizeof(*state
));
4320 if (state
== NULL
) {
4324 state
->new_len
= read_lease(ifp
, &state
->new);
4325 if (state
->new == NULL
) {
4326 logerr("read_lease");
4329 state
->reason
= "DUMP";
4330 return script_runreason(ifp
, state
->reason
);