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
)) {
1881 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1882 ifo
->reboot
, dhcp_fallback
, ifp
);
1884 else if (ifo
->options
& DHCPCD_IPV4LL
)
1885 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1886 ifo
->reboot
, 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_request(void *arg
)
1901 struct interface
*ifp
= arg
;
1902 struct dhcp_state
*state
= D_STATE(ifp
);
1904 state
->state
= DHS_REQUEST
;
1909 dhcp_expire(void *arg
)
1911 struct interface
*ifp
= arg
;
1912 struct dhcp_state
*state
= D_STATE(ifp
);
1914 if (ifp
->options
->options
& DHCPCD_LASTLEASE_EXTEND
) {
1915 logwarnx("%s: DHCP lease expired, extending lease", ifp
->name
);
1916 state
->added
|= STATE_EXPIRED
;
1918 logerrx("%s: DHCP lease expired", ifp
->name
);
1919 dhcp_drop(ifp
, "EXPIRE");
1920 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
1922 state
->interval
= 0;
1926 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
1928 dhcp_decline(struct interface
*ifp
)
1931 send_message(ifp
, DHCP_DECLINE
, NULL
);
1936 dhcp_startrenew(void *arg
)
1938 struct interface
*ifp
= arg
;
1939 struct dhcp_state
*state
;
1940 struct dhcp_lease
*lease
;
1942 if ((state
= D_STATE(ifp
)) == NULL
)
1945 /* Only renew in the bound or renew states */
1946 if (state
->state
!= DHS_BOUND
&&
1947 state
->state
!= DHS_RENEW
)
1950 /* Remove the timeout as the renew may have been forced. */
1951 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp_startrenew
, ifp
);
1953 lease
= &state
->lease
;
1954 logdebugx("%s: renewing lease of %s", ifp
->name
,
1955 inet_ntoa(lease
->addr
));
1956 state
->state
= DHS_RENEW
;
1958 state
->interval
= 0;
1963 dhcp_renew(struct interface
*ifp
)
1966 dhcp_startrenew(ifp
);
1970 dhcp_rebind(void *arg
)
1972 struct interface
*ifp
= arg
;
1973 struct dhcp_state
*state
= D_STATE(ifp
);
1974 struct dhcp_lease
*lease
= &state
->lease
;
1976 logwarnx("%s: failed to renew DHCP, rebinding", ifp
->name
);
1977 logdebugx("%s: expire in %"PRIu32
" seconds",
1978 ifp
->name
, lease
->leasetime
- lease
->rebindtime
);
1979 state
->state
= DHS_REBIND
;
1980 eloop_timeout_delete(ifp
->ctx
->eloop
, send_renew
, ifp
);
1981 state
->lease
.server
.s_addr
= INADDR_ANY
;
1982 state
->interval
= 0;
1983 ifp
->options
->options
&= ~(DHCPCD_CSR_WARNED
|
1984 DHCPCD_ROUTER_HOST_ROUTE_WARNED
);
1988 #if defined(ARP) || defined(IN_IFF_DUPLICATED)
1990 dhcp_finish_dad(struct interface
*ifp
, struct in_addr
*ia
)
1992 struct dhcp_state
*state
= D_STATE(ifp
);
1994 if (state
->state
== DHS_BOUND
)
1996 if (state
->offer
== NULL
|| state
->offer
->yiaddr
!= ia
->s_addr
)
1999 logdebugx("%s: DAD completed for %s", ifp
->name
, inet_ntoa(*ia
));
2000 if (!(ifp
->options
->options
& DHCPCD_INFORM
))
2002 #ifndef IN_IFF_DUPLICATED
2004 struct bootp
*bootp
;
2008 len
= state
->new_len
;
2009 state
->new = state
->offer
;
2010 state
->new_len
= state
->offer_len
;
2011 get_lease(ifp
, &state
->lease
, state
->new, state
->new_len
);
2012 ipv4_applyaddr(ifp
);
2014 state
->new_len
= len
;
2019 /* Stop IPv4LL now we have a working DHCP address */
2020 if (!IN_LINKLOCAL(ntohl(ia
->s_addr
)))
2024 if (ifp
->options
->options
& DHCPCD_INFORM
)
2029 dhcp_addr_duplicated(struct interface
*ifp
, struct in_addr
*ia
)
2031 struct dhcp_state
*state
= D_STATE(ifp
);
2032 unsigned long long opts
= ifp
->options
->options
;
2033 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
2034 bool deleted
= false;
2035 #ifdef IN_IFF_DUPLICATED
2036 struct ipv4_addr
*iap
;
2039 if ((state
->offer
== NULL
|| state
->offer
->yiaddr
!= ia
->s_addr
) &&
2040 !IN_ARE_ADDR_EQUAL(ia
, &state
->lease
.addr
))
2043 /* RFC 2131 3.1.5, Client-server interaction */
2044 logerrx("%s: DAD detected %s", ifp
->name
, inet_ntoa(*ia
));
2045 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2046 if (!(opts
& DHCPCD_STATIC
) && !state
->lease
.frominfo
)
2048 #ifdef IN_IFF_DUPLICATED
2049 if ((iap
= ipv4_iffindaddr(ifp
, ia
, NULL
)) != NULL
) {
2050 ipv4_deladdr(iap
, 0);
2054 eloop_timeout_delete(ctx
->eloop
, NULL
, ifp
);
2055 if (opts
& (DHCPCD_STATIC
| DHCPCD_INFORM
)) {
2056 state
->reason
= "EXPIRE";
2057 script_runreason(ifp
, state
->reason
);
2058 #define NOT_ONLY_SELF (DHCPCD_MANAGER | DHCPCD_IPV6RS | DHCPCD_DHCP6)
2059 if (!(ctx
->options
& NOT_ONLY_SELF
))
2060 eloop_exit(ifp
->ctx
->eloop
, EXIT_FAILURE
);
2063 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2064 DHCP_RAND_MAX
, dhcp_discover
, ifp
);
2070 #ifdef KERNEL_RFC5227
2073 dhcp_arp_announced(struct arp_state
*state
)
2081 dhcp_arp_defend_failed(struct arp_state
*astate
)
2083 struct interface
*ifp
= astate
->iface
;
2085 dhcp_drop(ifp
, "EXPIRED");
2090 #if !defined(KERNEL_RFC5227) || defined(ARPING)
2091 static void dhcp_arp_not_found(struct arp_state
*);
2093 static struct arp_state
*
2094 dhcp_arp_new(struct interface
*ifp
, struct in_addr
*addr
)
2096 struct arp_state
*astate
;
2098 astate
= arp_new(ifp
, addr
);
2102 astate
->found_cb
= dhcp_arp_found
;
2103 astate
->not_found_cb
= dhcp_arp_not_found
;
2104 #ifdef KERNEL_RFC5227
2105 astate
->announced_cb
= dhcp_arp_announced
;
2107 astate
->announced_cb
= NULL
;
2108 astate
->defend_failed_cb
= dhcp_arp_defend_failed
;
2116 dhcp_arping(struct interface
*ifp
)
2118 struct dhcp_state
*state
;
2119 struct if_options
*ifo
;
2120 struct arp_state
*astate
;
2121 struct in_addr addr
;
2123 state
= D_STATE(ifp
);
2126 if (ifo
->arping_len
== 0 || state
->arping_index
> ifo
->arping_len
)
2129 if (state
->arping_index
+ 1 == ifo
->arping_len
) {
2130 state
->arping_index
++;
2131 dhcpcd_startinterface(ifp
);
2135 addr
.s_addr
= ifo
->arping
[++state
->arping_index
];
2136 astate
= dhcp_arp_new(ifp
, &addr
);
2137 if (astate
== NULL
) {
2146 #if !defined(KERNEL_RFC5227) || defined(ARPING)
2148 dhcp_arp_not_found(struct arp_state
*astate
)
2150 struct interface
*ifp
;
2152 ifp
= astate
->iface
;
2154 if (dhcp_arping(ifp
) == 1) {
2160 dhcp_finish_dad(ifp
, &astate
->addr
);
2164 dhcp_arp_found(struct arp_state
*astate
, const struct arp_msg
*amsg
)
2166 struct in_addr addr
;
2167 struct interface
*ifp
= astate
->iface
;
2169 struct dhcp_state
*state
;
2170 struct if_options
*ifo
;
2172 state
= D_STATE(ifp
);
2175 if (state
->arping_index
!= -1 &&
2176 state
->arping_index
< ifo
->arping_len
&&
2178 amsg
->sip
.s_addr
== ifo
->arping
[state
->arping_index
])
2180 char buf
[HWADDR_LEN
* 3];
2182 hwaddr_ntoa(amsg
->sha
, ifp
->hwlen
, buf
, sizeof(buf
));
2183 if (dhcpcd_selectprofile(ifp
, buf
) == -1 &&
2184 dhcpcd_selectprofile(ifp
, inet_ntoa(amsg
->sip
)) == -1)
2186 /* We didn't find a profile for this
2187 * address or hwaddr, so move to the next
2189 dhcp_arp_not_found(astate
);
2193 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2194 dhcpcd_startinterface(ifp
);
2201 addr
= astate
->addr
;
2203 dhcp_addr_duplicated(ifp
, &addr
);
2210 dhcp_bind(struct interface
*ifp
)
2212 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
2213 struct dhcp_state
*state
= D_STATE(ifp
);
2214 struct if_options
*ifo
= ifp
->options
;
2215 struct dhcp_lease
*lease
= &state
->lease
;
2218 state
->reason
= NULL
;
2219 /* If we don't have an offer, we are re-binding a lease on preference,
2220 * normally when two interfaces have a lease matching IP addresses. */
2223 state
->old
= state
->new;
2224 state
->old_len
= state
->new_len
;
2225 state
->new = state
->offer
;
2226 state
->new_len
= state
->offer_len
;
2227 state
->offer
= NULL
;
2228 state
->offer_len
= 0;
2230 get_lease(ifp
, lease
, state
->new, state
->new_len
);
2231 if (ifo
->options
& DHCPCD_STATIC
) {
2232 loginfox("%s: using static address %s/%d",
2233 ifp
->name
, inet_ntoa(lease
->addr
),
2234 inet_ntocidr(lease
->mask
));
2235 lease
->leasetime
= DHCP_INFINITE_LIFETIME
;
2236 state
->reason
= "STATIC";
2237 } else if (ifo
->options
& DHCPCD_INFORM
) {
2238 loginfox("%s: received approval for %s",
2239 ifp
->name
, inet_ntoa(lease
->addr
));
2240 lease
->leasetime
= DHCP_INFINITE_LIFETIME
;
2241 state
->reason
= "INFORM";
2243 if (lease
->frominfo
)
2244 state
->reason
= "TIMEOUT";
2245 if (lease
->leasetime
== DHCP_INFINITE_LIFETIME
) {
2246 lease
->renewaltime
=
2249 loginfox("%s: leased %s for infinity",
2250 ifp
->name
, inet_ntoa(lease
->addr
));
2252 if (lease
->leasetime
< DHCP_MIN_LEASE
) {
2253 logwarnx("%s: minimum lease is %d seconds",
2254 ifp
->name
, DHCP_MIN_LEASE
);
2255 lease
->leasetime
= DHCP_MIN_LEASE
;
2257 if (lease
->rebindtime
== 0)
2259 (uint32_t)(lease
->leasetime
* T2
);
2260 else if (lease
->rebindtime
>= lease
->leasetime
) {
2262 (uint32_t)(lease
->leasetime
* T2
);
2263 logwarnx("%s: rebind time greater than lease "
2264 "time, forcing to %"PRIu32
" seconds",
2265 ifp
->name
, lease
->rebindtime
);
2267 if (lease
->renewaltime
== 0)
2268 lease
->renewaltime
=
2269 (uint32_t)(lease
->leasetime
* T1
);
2270 else if (lease
->renewaltime
> lease
->rebindtime
) {
2271 lease
->renewaltime
=
2272 (uint32_t)(lease
->leasetime
* T1
);
2273 logwarnx("%s: renewal time greater than "
2274 "rebind time, forcing to %"PRIu32
" seconds",
2275 ifp
->name
, lease
->renewaltime
);
2277 if (state
->state
== DHS_RENEW
&& state
->addr
&&
2278 lease
->addr
.s_addr
== state
->addr
->addr
.s_addr
&&
2279 !(state
->added
& STATE_FAKE
))
2280 logdebugx("%s: leased %s for %"PRIu32
" seconds",
2281 ifp
->name
, inet_ntoa(lease
->addr
),
2284 loginfox("%s: leased %s for %"PRIu32
" seconds",
2285 ifp
->name
, inet_ntoa(lease
->addr
),
2289 if (ctx
->options
& DHCPCD_TEST
) {
2290 state
->reason
= "TEST";
2291 script_runreason(ifp
, state
->reason
);
2292 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
2295 if (state
->reason
== NULL
) {
2297 !(state
->added
& (STATE_FAKE
| STATE_EXPIRED
)))
2299 if (state
->old
->yiaddr
== state
->new->yiaddr
&&
2300 lease
->server
.s_addr
&&
2301 state
->state
!= DHS_REBIND
)
2302 state
->reason
= "RENEW";
2304 state
->reason
= "REBIND";
2305 } else if (state
->state
== DHS_REBOOT
)
2306 state
->reason
= "REBOOT";
2308 state
->reason
= "BOUND";
2310 if (lease
->leasetime
== DHCP_INFINITE_LIFETIME
)
2311 lease
->renewaltime
= lease
->rebindtime
= lease
->leasetime
;
2313 eloop_timeout_add_sec(ctx
->eloop
,
2314 lease
->renewaltime
, dhcp_startrenew
, ifp
);
2315 eloop_timeout_add_sec(ctx
->eloop
,
2316 lease
->rebindtime
, dhcp_rebind
, ifp
);
2317 eloop_timeout_add_sec(ctx
->eloop
,
2318 lease
->leasetime
, dhcp_expire
, ifp
);
2319 logdebugx("%s: renew in %"PRIu32
" seconds, rebind in %"PRIu32
2321 ifp
->name
, lease
->renewaltime
, lease
->rebindtime
);
2323 state
->state
= DHS_BOUND
;
2324 if (!state
->lease
.frominfo
&&
2325 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))) {
2326 logdebugx("%s: writing lease: %s",
2327 ifp
->name
, state
->leasefile
);
2328 if (dhcp_writefile(ifp
->ctx
, state
->leasefile
, 0640,
2329 state
->new, state
->new_len
) == -1)
2330 logerr("dhcp_writefile: %s", state
->leasefile
);
2333 old_state
= state
->added
;
2335 if (!(ifo
->options
& DHCPCD_CONFIGURE
)) {
2336 struct ipv4_addr
*ia
;
2338 script_runreason(ifp
, state
->reason
);
2339 dhcpcd_daemonise(ifp
->ctx
);
2341 /* We we are not configuring the address, we need to keep
2342 * the BPF socket open if the address does not exist. */
2343 ia
= ipv4_iffindaddr(ifp
, &state
->lease
.addr
, NULL
);
2346 state
->added
= STATE_ADDED
;
2353 /* Add the address */
2354 if (ipv4_applyaddr(ifp
) == NULL
) {
2355 /* There was an error adding the address.
2356 * If we are in oneshot, exit with a failure. */
2357 if (ctx
->options
& DHCPCD_ONESHOT
) {
2358 loginfox("exiting due to oneshot");
2359 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
2364 /* Close the BPF filter as we can now receive DHCP messages
2365 * on a UDP socket. */
2369 /* If not in manager mode, open an address specific socket. */
2370 if (ctx
->options
& DHCPCD_MANAGER
||
2371 ifo
->options
& DHCPCD_STATIC
||
2372 (state
->old
!= NULL
&&
2373 state
->old
->yiaddr
== state
->new->yiaddr
&&
2374 old_state
& STATE_ADDED
&& !(old_state
& STATE_FAKE
)))
2377 dhcp_closeinet(ifp
);
2379 if (IN_PRIVSEP_SE(ctx
)) {
2380 if (ps_inet_openbootp(state
->addr
) == -1)
2386 state
->udp_rfd
= dhcp_openudp(&state
->addr
->addr
);
2387 if (state
->udp_rfd
== -1) {
2389 /* Address sharing without manager mode is not supported.
2390 * It's also possible another DHCP client could be running,
2391 * which is even worse.
2392 * We still need to work, so re-open BPF. */
2396 if (eloop_event_add(ctx
->eloop
, state
->udp_rfd
, ELE_READ
,
2397 dhcp_handleifudp
, ifp
) == -1)
2398 logerr("%s: eloop_event_add", __func__
);
2402 dhcp_message_new(struct bootp
**bootp
,
2403 const struct in_addr
*addr
, const struct in_addr
*mask
)
2408 if ((*bootp
= calloc(1, sizeof(**bootp
))) == NULL
)
2411 (*bootp
)->yiaddr
= addr
->s_addr
;
2414 cookie
= htonl(MAGIC_COOKIE
);
2415 memcpy(p
, &cookie
, sizeof(cookie
));
2416 p
+= sizeof(cookie
);
2418 if (mask
->s_addr
!= INADDR_ANY
) {
2419 *p
++ = DHO_SUBNETMASK
;
2420 *p
++ = sizeof(mask
->s_addr
);
2421 memcpy(p
, &mask
->s_addr
, sizeof(mask
->s_addr
));
2422 p
+= sizeof(mask
->s_addr
);
2426 return sizeof(**bootp
);
2429 #if defined(ARP) || defined(KERNEL_RFC5227)
2431 dhcp_arp_address(struct interface
*ifp
)
2433 struct dhcp_state
*state
;
2434 struct in_addr addr
;
2435 struct ipv4_addr
*ia
;
2437 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2439 state
= D_STATE(ifp
);
2440 addr
.s_addr
= state
->offer
->yiaddr
== INADDR_ANY
?
2441 state
->offer
->ciaddr
: state
->offer
->yiaddr
;
2442 /* If the interface already has the address configured
2443 * then we can't ARP for duplicate detection. */
2444 ia
= ipv4_iffindaddr(ifp
, &addr
, NULL
);
2445 #ifdef IN_IFF_NOTUSEABLE
2446 if (ia
== NULL
|| ia
->addr_flags
& IN_IFF_NOTUSEABLE
) {
2447 state
->state
= DHS_PROBE
;
2449 struct dhcp_lease l
;
2451 get_lease(ifp
, &l
, state
->offer
, state
->offer_len
);
2452 /* Add the address now, let the kernel handle DAD. */
2453 ipv4_addaddr(ifp
, &l
.addr
, &l
.mask
, &l
.brd
,
2454 l
.leasetime
, l
.rebindtime
);
2455 } else if (ia
->addr_flags
& IN_IFF_DUPLICATED
)
2456 dhcp_addr_duplicated(ifp
, &ia
->addr
);
2458 loginfox("%s: waiting for DAD on %s",
2459 ifp
->name
, inet_ntoa(addr
));
2463 if (!(ifp
->flags
& IFF_NOARP
) &&
2464 ifp
->options
->options
& DHCPCD_ARP
)
2466 struct arp_state
*astate
;
2467 struct dhcp_lease l
;
2469 /* Even if the address exists, we need to defend it. */
2470 astate
= dhcp_arp_new(ifp
, &addr
);
2475 state
->state
= DHS_PROBE
;
2476 get_lease(ifp
, &l
, state
->offer
, state
->offer_len
);
2477 loginfox("%s: probing address %s/%d",
2478 ifp
->name
, inet_ntoa(l
.addr
), inet_ntocidr(l
.mask
));
2479 /* We need to handle DAD. */
2490 dhcp_arp_bind(struct interface
*ifp
)
2493 if (ifp
->ctx
->options
& DHCPCD_TEST
||
2494 dhcp_arp_address(ifp
) == 1)
2500 dhcp_lastlease(void *arg
)
2502 struct interface
*ifp
= arg
;
2503 struct dhcp_state
*state
= D_STATE(ifp
);
2505 loginfox("%s: timed out contacting a DHCP server, using last lease",
2507 #if defined(ARP) || defined(KERNEL_RFC5227)
2512 /* Set expired here because dhcp_bind() -> ipv4_addaddr() will reset
2514 state
->added
|= STATE_EXPIRED
;
2515 state
->interval
= 0;
2520 dhcp_static(struct interface
*ifp
)
2522 struct if_options
*ifo
;
2523 struct dhcp_state
*state
;
2524 struct ipv4_addr
*ia
;
2526 state
= D_STATE(ifp
);
2530 if (ifo
->req_addr
.s_addr
== INADDR_ANY
&&
2531 (ia
= ipv4_iffindaddr(ifp
, NULL
, NULL
)) == NULL
)
2533 loginfox("%s: waiting for 3rd party to "
2534 "configure IP address", ifp
->name
);
2535 state
->reason
= "3RDPARTY";
2536 script_runreason(ifp
, state
->reason
);
2540 state
->offer_len
= dhcp_message_new(&state
->offer
,
2541 ia
? &ia
->addr
: &ifo
->req_addr
,
2542 ia
? &ia
->mask
: &ifo
->req_mask
);
2543 if (state
->offer_len
)
2544 #if defined(ARP) || defined(KERNEL_RFC5227)
2552 dhcp_inform(struct interface
*ifp
)
2554 struct dhcp_state
*state
;
2555 struct if_options
*ifo
;
2556 struct ipv4_addr
*ia
;
2558 state
= D_STATE(ifp
);
2562 state
->offer
= NULL
;
2563 state
->offer_len
= 0;
2565 if (ifo
->req_addr
.s_addr
== INADDR_ANY
) {
2566 ia
= ipv4_iffindaddr(ifp
, NULL
, NULL
);
2568 loginfox("%s: waiting for 3rd party to "
2569 "configure IP address",
2571 if (!(ifp
->ctx
->options
& DHCPCD_TEST
)) {
2572 state
->reason
= "3RDPARTY";
2573 script_runreason(ifp
, state
->reason
);
2578 ia
= ipv4_iffindaddr(ifp
, &ifo
->req_addr
, &ifo
->req_mask
);
2580 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
2581 logerrx("%s: cannot add IP address in test mode",
2585 ia
= ipv4_iffindaddr(ifp
, &ifo
->req_addr
, NULL
);
2587 /* Netmask must be different, delete it. */
2588 ipv4_deladdr(ia
, 1);
2589 state
->offer_len
= dhcp_message_new(&state
->offer
,
2590 &ifo
->req_addr
, &ifo
->req_mask
);
2592 if (dhcp_arp_address(ifp
) != 1)
2595 ia
= ipv4_iffindaddr(ifp
,
2596 &ifo
->req_addr
, &ifo
->req_mask
);
2601 state
->state
= DHS_INFORM
;
2603 state
->offer_len
= dhcp_message_new(&state
->offer
,
2604 &ia
->addr
, &ia
->mask
);
2605 if (state
->offer_len
) {
2607 get_lease(ifp
, &state
->lease
, state
->offer
, state
->offer_len
);
2613 dhcp_reboot_newopts(struct interface
*ifp
, unsigned long long oldopts
)
2615 struct if_options
*ifo
;
2616 struct dhcp_state
*state
= D_STATE(ifp
);
2618 if (state
== NULL
|| state
->state
== DHS_NONE
)
2621 if ((ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2622 (state
->addr
== NULL
||
2623 state
->addr
->addr
.s_addr
!= ifo
->req_addr
.s_addr
)) ||
2624 (oldopts
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2625 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))))
2627 dhcp_drop(ifp
, "EXPIRE");
2633 dhcp_activeaddr(const struct interface
*ifp
, const struct in_addr
*addr
)
2635 const struct interface
*ifp1
;
2636 const struct dhcp_state
*state
;
2638 TAILQ_FOREACH(ifp1
, ifp
->ctx
->ifaces
, next
) {
2641 if ((state
= D_CSTATE(ifp1
)) == NULL
)
2643 switch(state
->state
) {
2653 if (state
->lease
.addr
.s_addr
== addr
->s_addr
)
2661 dhcp_reboot(struct interface
*ifp
)
2663 struct if_options
*ifo
;
2664 struct dhcp_state
*state
= D_STATE(ifp
);
2666 struct ipv4_addr
*ia
;
2669 if (state
== NULL
|| state
->state
== DHS_NONE
)
2672 state
->state
= DHS_REBOOT
;
2673 state
->interval
= 0;
2675 if (ifo
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
)) {
2676 loginfox("%s: waiting for carrier", ifp
->name
);
2679 if (ifo
->options
& DHCPCD_STATIC
) {
2683 if (ifo
->options
& DHCPCD_INFORM
) {
2684 loginfox("%s: informing address of %s",
2685 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2689 if (ifo
->reboot
== 0 || state
->offer
== NULL
) {
2693 if (!IS_DHCP(state
->offer
))
2696 loginfox("%s: rebinding lease of %s",
2697 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2700 #ifndef KERNEL_RFC5227
2701 /* Create the DHCP ARP state so we can defend it. */
2702 (void)dhcp_arp_new(ifp
, &state
->lease
.addr
);
2705 /* If the address exists on the interface and no other interface
2706 * is currently using it then announce it to ensure this
2707 * interface gets the reply. */
2708 ia
= ipv4_iffindaddr(ifp
, &state
->lease
.addr
, NULL
);
2710 !(ifp
->ctx
->options
& DHCPCD_TEST
) &&
2711 #ifdef IN_IFF_NOTUSEABLE
2712 !(ia
->addr_flags
& IN_IFF_NOTUSEABLE
) &&
2714 dhcp_activeaddr(ifp
, &state
->lease
.addr
) == 0)
2715 arp_ifannounceaddr(ifp
, &state
->lease
.addr
);
2719 state
->lease
.server
.s_addr
= INADDR_ANY
;
2720 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2723 /* Need to add this before dhcp_expire and friends. */
2724 if (!ifo
->fallback
&& ifo
->options
& DHCPCD_IPV4LL
)
2725 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2726 ifo
->reboot
, ipv4ll_start
, ifp
);
2729 if (ifo
->options
& DHCPCD_LASTLEASE
&& state
->lease
.frominfo
)
2730 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2731 ifo
->reboot
, dhcp_lastlease
, ifp
);
2732 else if (!(ifo
->options
& DHCPCD_INFORM
))
2733 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2734 ifo
->reboot
, dhcp_expire
, ifp
);
2736 /* Don't bother ARP checking as the server could NAK us first.
2737 * Don't call dhcp_request as that would change the state */
2742 dhcp_drop(struct interface
*ifp
, const char *reason
)
2744 struct dhcp_state
*state
= D_STATE(ifp
);
2746 /* dhcp_start may just have been called and we don't yet have a state
2747 * but we do have a timeout, so punt it. */
2748 if (state
== NULL
|| state
->state
== DHS_NONE
) {
2749 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2754 if (state
->addr
!= NULL
)
2755 arp_freeaddr(ifp
, &state
->addr
->addr
);
2758 state
->arping_index
= -1;
2761 if (ifp
->options
->options
& DHCPCD_RELEASE
&&
2762 !(ifp
->options
->options
& DHCPCD_INFORM
))
2764 /* Failure to send the release may cause this function to
2765 * re-enter so guard by setting the state. */
2766 if (state
->state
== DHS_RELEASE
)
2768 state
->state
= DHS_RELEASE
;
2770 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2771 if (if_is_link_up(ifp
) &&
2772 state
->new != NULL
&&
2773 state
->lease
.server
.s_addr
!= INADDR_ANY
)
2775 loginfox("%s: releasing lease of %s",
2776 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2778 send_message(ifp
, DHCP_RELEASE
, NULL
);
2782 else if (state
->auth
.reconf
!= NULL
) {
2784 * Drop the lease as the token may only be present
2785 * in the initial reply message and not subsequent
2787 * If dhcpcd is restarted, the token is lost.
2788 * XXX persist this in another file?
2790 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
2794 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2796 dhcp_auth_reset(&state
->auth
);
2799 state
->state
= DHS_NONE
;
2801 state
->offer
= NULL
;
2802 state
->offer_len
= 0;
2804 state
->old
= state
->new;
2805 state
->old_len
= state
->new_len
;
2808 state
->reason
= reason
;
2809 if (ifp
->options
->options
& DHCPCD_CONFIGURE
)
2810 ipv4_applyaddr(ifp
);
2814 script_runreason(ifp
, state
->reason
);
2819 state
->lease
.addr
.s_addr
= 0;
2820 ifp
->options
->options
&= ~(DHCPCD_CSR_WARNED
|
2821 DHCPCD_ROUTER_HOST_ROUTE_WARNED
);
2823 /* Close DHCP ports so a changed interface family is picked
2824 * up by a new BPF state. */
2829 blacklisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2833 for (i
= 0; i
< ifo
->blacklist_len
; i
+= 2)
2834 if (ifo
->blacklist
[i
] == (addr
& ifo
->blacklist
[i
+ 1]))
2839 #define WHTLST_NONE 0
2840 #define WHTLST_MATCH 1
2841 #define WHTLST_NOMATCH 2
2843 whitelisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2847 if (ifo
->whitelist_len
== 0)
2849 for (i
= 0; i
< ifo
->whitelist_len
; i
+= 2)
2850 if (ifo
->whitelist
[i
] == (addr
& ifo
->whitelist
[i
+ 1]))
2851 return WHTLST_MATCH
;
2852 return WHTLST_NOMATCH
;
2856 log_dhcp(int loglevel
, const char *msg
,
2857 const struct interface
*ifp
, const struct bootp
*bootp
, size_t bootp_len
,
2858 const struct in_addr
*from
, int ad
)
2861 char *a
, sname
[sizeof(bootp
->sname
) * 4];
2862 struct in_addr addr
;
2866 if (strcmp(msg
, "NAK:") == 0) {
2867 a
= get_option_string(ifp
->ctx
, bootp
, bootp_len
, DHO_MESSAGE
);
2873 tmpl
= (al
* 4) + 1;
2880 print_string(tmp
, tmpl
, OT_STRING
, (uint8_t *)a
, al
);
2884 } else if (ad
&& bootp
->yiaddr
!= 0) {
2885 addr
.s_addr
= bootp
->yiaddr
;
2886 a
= strdup(inet_ntoa(addr
));
2895 r
= get_option_addr(ifp
->ctx
, &addr
, bootp
, bootp_len
, DHO_SERVERID
);
2896 if (get_option_uint8(ifp
->ctx
, &overl
, bootp
, bootp_len
,
2897 DHO_OPTSOVERLOADED
) == -1)
2899 if (bootp
->sname
[0] && r
== 0 && !(overl
& 2)) {
2900 print_string(sname
, sizeof(sname
), OT_STRING
| OT_DOMAIN
,
2901 bootp
->sname
, sizeof(bootp
->sname
));
2903 logmessage(loglevel
, "%s: %s %s %s %s",
2904 ifp
->name
, msg
, tfrom
, inet_ntoa(addr
), sname
);
2906 logmessage(loglevel
, "%s: %s %s %s %s %s",
2907 ifp
->name
, msg
, a
, tfrom
, inet_ntoa(addr
), sname
);
2914 logmessage(loglevel
, "%s: %s %s %s",
2915 ifp
->name
, msg
, tfrom
, inet_ntoa(addr
));
2917 logmessage(loglevel
, "%s: %s %s %s %s",
2918 ifp
->name
, msg
, a
, tfrom
, inet_ntoa(addr
));
2923 /* If we're sharing the same IP address with another interface on the
2924 * same network, we may receive the DHCP reply on the wrong interface.
2925 * Try and re-direct it here. */
2927 dhcp_redirect_dhcp(struct interface
*ifp
, struct bootp
*bootp
, size_t bootp_len
,
2928 const struct in_addr
*from
)
2930 struct interface
*ifn
;
2931 const struct dhcp_state
*state
;
2934 xid
= ntohl(bootp
->xid
);
2935 TAILQ_FOREACH(ifn
, ifp
->ctx
->ifaces
, next
) {
2938 state
= D_CSTATE(ifn
);
2939 if (state
== NULL
|| state
->state
== DHS_NONE
)
2941 if (state
->xid
!= xid
)
2943 if (ifn
->hwlen
<= sizeof(bootp
->chaddr
) &&
2944 memcmp(bootp
->chaddr
, ifn
->hwaddr
, ifn
->hwlen
))
2946 logdebugx("%s: redirecting DHCP message to %s",
2947 ifp
->name
, ifn
->name
);
2948 dhcp_handledhcp(ifn
, bootp
, bootp_len
, from
);
2953 dhcp_handledhcp(struct interface
*ifp
, struct bootp
*bootp
, size_t bootp_len
,
2954 const struct in_addr
*from
)
2956 struct dhcp_state
*state
= D_STATE(ifp
);
2957 struct if_options
*ifo
= ifp
->options
;
2958 struct dhcp_lease
*lease
= &state
->lease
;
2960 struct in_addr addr
;
2964 uint32_t v6only_time
= 0;
2965 bool use_v6only
= false;
2967 const uint8_t *auth
;
2970 #ifdef IN_IFF_DUPLICATED
2971 struct ipv4_addr
*ia
;
2974 #define LOGDHCP0(l, m) \
2975 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0)
2976 #define LOGDHCP(l, m) \
2977 log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1)
2979 #define IS_STATE_ACTIVE(s) ((s)-state != DHS_NONE && \
2980 (s)->state != DHS_INIT && (s)->state != DHS_BOUND)
2982 if (bootp
->op
!= BOOTREPLY
) {
2983 if (IS_STATE_ACTIVE(state
))
2984 logdebugx("%s: op (%d) is not BOOTREPLY",
2985 ifp
->name
, bootp
->op
);
2989 if (state
->xid
!= ntohl(bootp
->xid
)) {
2990 if (IS_STATE_ACTIVE(state
))
2991 logdebugx("%s: wrong xid 0x%x (expecting 0x%x) from %s",
2992 ifp
->name
, ntohl(bootp
->xid
), state
->xid
,
2994 dhcp_redirect_dhcp(ifp
, bootp
, bootp_len
, from
);
2998 if (ifp
->hwlen
<= sizeof(bootp
->chaddr
) &&
2999 memcmp(bootp
->chaddr
, ifp
->hwaddr
, ifp
->hwlen
))
3001 if (IS_STATE_ACTIVE(state
)) {
3002 char buf
[sizeof(bootp
->chaddr
) * 3];
3004 logdebugx("%s: xid 0x%x is for hwaddr %s",
3005 ifp
->name
, ntohl(bootp
->xid
),
3006 hwaddr_ntoa(bootp
->chaddr
, sizeof(bootp
->chaddr
),
3009 dhcp_redirect_dhcp(ifp
, bootp
, bootp_len
, from
);
3016 i
= whitelisted_ip(ifp
->options
, from
->s_addr
);
3018 case WHTLST_NOMATCH
:
3019 logwarnx("%s: non whitelisted DHCP packet from %s",
3020 ifp
->name
, inet_ntoa(*from
));
3025 if (blacklisted_ip(ifp
->options
, from
->s_addr
) == 1) {
3026 logwarnx("%s: blacklisted DHCP packet from %s",
3027 ifp
->name
, inet_ntoa(*from
));
3032 /* We may have found a BOOTP server */
3033 if (get_option_uint8(ifp
->ctx
, &type
,
3034 bootp
, bootp_len
, DHO_MESSAGETYPE
) == -1)
3036 else if (ifo
->options
& DHCPCD_BOOTP
) {
3037 logdebugx("%s: ignoring DHCP reply (expecting BOOTP)",
3043 /* Authenticate the message */
3044 auth
= get_option(ifp
->ctx
, bootp
, bootp_len
,
3045 DHO_AUTHENTICATION
, &auth_len
);
3047 if (dhcp_auth_validate(&state
->auth
, &ifo
->auth
,
3048 (uint8_t *)bootp
, bootp_len
, 4, type
,
3049 auth
, auth_len
) == NULL
)
3051 LOGDHCP0(LOG_ERR
, "authentication failed");
3054 if (state
->auth
.token
)
3055 logdebugx("%s: validated using 0x%08" PRIu32
,
3056 ifp
->name
, state
->auth
.token
->secretid
);
3058 loginfox("%s: accepted reconfigure key", ifp
->name
);
3059 } else if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
3060 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
) {
3061 LOGDHCP0(LOG_ERR
, "no authentication");
3064 LOGDHCP0(LOG_WARNING
, "no authentication");
3069 if (type
== DHCP_FORCERENEW
) {
3070 if (from
->s_addr
== INADDR_ANY
||
3071 from
->s_addr
== INADDR_BROADCAST
)
3073 LOGDHCP(LOG_ERR
, "discarding Force Renew");
3078 LOGDHCP(LOG_ERR
, "unauthenticated Force Renew");
3079 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
)
3082 if (state
->state
!= DHS_BOUND
&& state
->state
!= DHS_INFORM
) {
3083 LOGDHCP(LOG_DEBUG
, "not bound, ignoring Force Renew");
3086 LOGDHCP(LOG_INFO
, "Force Renew from");
3087 /* The rebind and expire timings are still the same, we just
3088 * enter the renew state early */
3089 if (state
->state
== DHS_BOUND
)
3092 eloop_timeout_delete(ifp
->ctx
->eloop
,
3097 LOGDHCP(LOG_ERR
, "unauthenticated Force Renew");
3102 if (state
->state
== DHS_BOUND
) {
3103 LOGDHCP(LOG_DEBUG
, "bound, ignoring");
3107 if (state
->state
== DHS_PROBE
) {
3108 /* Ignore any DHCP messages whilst probing a lease to bind. */
3109 LOGDHCP(LOG_DEBUG
, "probing, ignoring");
3113 /* reset the message counter */
3114 state
->interval
= 0;
3116 /* Ensure that no reject options are present */
3117 for (i
= 1; i
< 255; i
++) {
3118 if (has_option_mask(ifo
->rejectmask
, i
) &&
3119 get_option_uint8(ifp
->ctx
, &tmp
,
3120 bootp
, bootp_len
, (uint8_t)i
) == 0)
3122 LOGDHCP(LOG_WARNING
, "reject DHCP");
3127 if (type
== DHCP_NAK
) {
3128 /* For NAK, only check if we require the ServerID */
3129 if (has_option_mask(ifo
->requiremask
, DHO_SERVERID
) &&
3130 get_option_addr(ifp
->ctx
, &addr
,
3131 bootp
, bootp_len
, DHO_SERVERID
) == -1)
3133 LOGDHCP(LOG_WARNING
, "reject NAK");
3137 /* We should restart on a NAK */
3138 LOGDHCP(LOG_WARNING
, "NAK:");
3139 if ((msg
= get_option_string(ifp
->ctx
,
3140 bootp
, bootp_len
, DHO_MESSAGE
)))
3142 logwarnx("%s: message: %s", ifp
->name
, msg
);
3145 if (state
->state
== DHS_INFORM
) /* INFORM should not be NAKed */
3147 if (!(ifp
->ctx
->options
& DHCPCD_TEST
)) {
3148 dhcp_drop(ifp
, "NAK");
3149 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3152 /* If we constantly get NAKS then we should slowly back off */
3153 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3154 state
->nakoff
, dhcp_discover
, ifp
);
3155 if (state
->nakoff
== 0)
3159 if (state
->nakoff
> NAKOFF_MAX
)
3160 state
->nakoff
= NAKOFF_MAX
;
3165 /* Ensure that all required options are present */
3166 for (i
= 1; i
< 255; i
++) {
3167 if (has_option_mask(ifo
->requiremask
, i
) &&
3168 get_option_uint8(ifp
->ctx
, &tmp
,
3169 bootp
, bootp_len
, (uint8_t)i
) != 0)
3171 /* If we are BOOTP, then ignore the need for serverid.
3172 * To ignore BOOTP, require dhcp_message_type.
3173 * However, nothing really stops BOOTP from providing
3174 * DHCP style options as well so the above isn't
3176 if (type
== 0 && i
== DHO_SERVERID
)
3178 LOGDHCP(LOG_WARNING
, "reject DHCP");
3183 if (has_option_mask(ifo
->requestmask
, DHO_IPV6_PREFERRED_ONLY
)) {
3184 if (get_option_uint32(ifp
->ctx
, &v6only_time
, bootp
, bootp_len
,
3185 DHO_IPV6_PREFERRED_ONLY
) == 0 &&
3186 (state
->state
== DHS_DISCOVER
|| state
->state
== DHS_REBOOT
))
3191 if (v6only_time
< MIN_V6ONLY_WAIT
)
3192 v6only_time
= MIN_V6ONLY_WAIT
;
3193 snprintf(v6msg
, sizeof(v6msg
),
3194 "IPv6-Only Preferred received (%u seconds)",
3196 LOGDHCP(LOG_INFO
, v6msg
);
3200 /* DHCP Auto-Configure, RFC 2563 */
3201 if (type
== DHCP_OFFER
&& bootp
->yiaddr
== 0) {
3202 LOGDHCP(LOG_WARNING
, "no address given");
3203 if ((msg
= get_option_string(ifp
->ctx
,
3204 bootp
, bootp_len
, DHO_MESSAGE
)))
3206 logwarnx("%s: message: %s", ifp
->name
, msg
);
3210 if (state
->state
== DHS_DISCOVER
&&
3211 get_option_uint8(ifp
->ctx
, &tmp
, bootp
, bootp_len
,
3212 DHO_AUTOCONFIGURE
) == 0)
3216 LOGDHCP(LOG_WARNING
, "IPv4LL disabled from");
3223 LOGDHCP(LOG_WARNING
, "IPv4LL enabled from");
3227 logerrx("%s: unknown auto configuration "
3232 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3233 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3234 use_v6only
? v6only_time
: DHCP_MAX
,
3235 dhcp_discover
, ifp
);
3242 dhcp_drop(ifp
, "EXPIRE");
3243 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3244 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3245 eloop_timeout_add_sec(ifp
->ctx
->eloop
, v6only_time
,
3246 dhcp_discover
, ifp
);
3250 /* Ensure that the address offered is valid */
3251 if ((type
== 0 || type
== DHCP_OFFER
|| type
== DHCP_ACK
) &&
3252 (bootp
->ciaddr
== INADDR_ANY
|| bootp
->ciaddr
== INADDR_BROADCAST
)
3254 (bootp
->yiaddr
== INADDR_ANY
|| bootp
->yiaddr
== INADDR_BROADCAST
))
3256 LOGDHCP(LOG_WARNING
, "reject invalid address");
3260 #ifdef IN_IFF_DUPLICATED
3261 ia
= ipv4_iffindaddr(ifp
, &lease
->addr
, NULL
);
3262 if (ia
&& ia
->addr_flags
& IN_IFF_DUPLICATED
) {
3263 LOGDHCP(LOG_WARNING
, "declined duplicate address");
3266 ipv4_deladdr(ia
, 0);
3267 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3268 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3269 DHCP_RAND_MAX
, dhcp_discover
, ifp
);
3274 bootp_copied
= false;
3275 if ((type
== 0 || type
== DHCP_OFFER
) && state
->state
== DHS_DISCOVER
) {
3276 lease
->frominfo
= 0;
3277 lease
->addr
.s_addr
= bootp
->yiaddr
;
3278 memcpy(&lease
->cookie
, bootp
->vend
, sizeof(lease
->cookie
));
3280 get_option_addr(ifp
->ctx
,
3281 &lease
->server
, bootp
, bootp_len
, DHO_SERVERID
) != 0)
3282 lease
->server
.s_addr
= INADDR_ANY
;
3284 /* Test for rapid commit in the OFFER */
3285 if (!(ifp
->ctx
->options
& DHCPCD_TEST
) &&
3286 has_option_mask(ifo
->requestmask
, DHO_RAPIDCOMMIT
) &&
3287 get_option(ifp
->ctx
, bootp
, bootp_len
,
3288 DHO_RAPIDCOMMIT
, NULL
))
3290 state
->state
= DHS_REQUEST
;
3294 LOGDHCP(LOG_INFO
, "offered");
3295 if (state
->offer_len
< bootp_len
) {
3297 if ((state
->offer
= malloc(bootp_len
)) == NULL
) {
3299 state
->offer_len
= 0;
3303 state
->offer_len
= bootp_len
;
3304 memcpy(state
->offer
, bootp
, bootp_len
);
3305 bootp_copied
= true;
3306 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
3308 state
->old
= state
->new;
3309 state
->old_len
= state
->new_len
;
3310 state
->new = state
->offer
;
3311 state
->new_len
= state
->offer_len
;
3312 state
->offer
= NULL
;
3313 state
->offer_len
= 0;
3314 state
->reason
= "TEST";
3315 script_runreason(ifp
, state
->reason
);
3316 eloop_exit(ifp
->ctx
->eloop
, EXIT_SUCCESS
);
3317 state
->bpf
->bpf_flags
|= BPF_EOF
;
3320 eloop_timeout_delete(ifp
->ctx
->eloop
, send_discover
, ifp
);
3321 /* We don't request BOOTP addresses */
3323 /* We used to ARP check here, but that seems to be in
3324 * violation of RFC2131 where it only describes
3325 * DECLINE after REQUEST.
3326 * It also seems that some MS DHCP servers actually
3327 * ignore DECLINE if no REQUEST, ie we decline a
3335 if (type
== DHCP_OFFER
) {
3336 LOGDHCP(LOG_WARNING
, "ignoring offer of");
3340 /* We should only be dealing with acks */
3341 if (type
!= DHCP_ACK
) {
3342 LOGDHCP(LOG_ERR
, "not ACK or OFFER");
3346 if (state
->state
== DHS_DISCOVER
) {
3347 /* We only allow ACK of rapid commit DISCOVER. */
3348 if (has_option_mask(ifo
->requestmask
,
3350 get_option(ifp
->ctx
, bootp
, bootp_len
,
3351 DHO_RAPIDCOMMIT
, NULL
))
3352 state
->state
= DHS_REQUEST
;
3354 LOGDHCP(LOG_DEBUG
, "ignoring ack of");
3360 if (!(ifo
->options
& DHCPCD_INFORM
))
3361 LOGDHCP(LOG_DEBUG
, "acknowledged");
3363 ifo
->options
&= ~DHCPCD_STATIC
;
3366 /* No NAK, so reset the backoff
3367 * We don't reset on an OFFER message because the server could
3368 * potentially NAK the REQUEST. */
3371 /* BOOTP could have already assigned this above. */
3372 if (!bootp_copied
) {
3373 if (state
->offer_len
< bootp_len
) {
3375 if ((state
->offer
= malloc(bootp_len
)) == NULL
) {
3377 state
->offer_len
= 0;
3381 state
->offer_len
= bootp_len
;
3382 memcpy(state
->offer
, bootp
, bootp_len
);
3385 lease
->frominfo
= 0;
3386 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3388 #if defined(ARP) || defined(KERNEL_RFC5227)
3396 get_udp_data(void *packet
, size_t *len
)
3398 const struct ip
*ip
= packet
;
3399 size_t ip_hl
= (size_t)ip
->ip_hl
* 4;
3402 p
+= ip_hl
+ sizeof(struct udphdr
);
3403 *len
= (size_t)ntohs(ip
->ip_len
) - sizeof(struct udphdr
) - ip_hl
;
3408 is_packet_udp_bootp(void *packet
, size_t plen
)
3410 struct ip
*ip
= packet
;
3414 if (plen
< sizeof(*ip
))
3417 if (ip
->ip_v
!= IPVERSION
|| ip
->ip_p
!= IPPROTO_UDP
)
3421 if (ntohs(ip
->ip_len
) > plen
)
3424 ip_hlen
= (size_t)ip
->ip_hl
* 4;
3425 if (ip_hlen
< sizeof(*ip
))
3428 /* Check we have a UDP header and BOOTP. */
3429 if (ip_hlen
+ sizeof(udp
) + offsetof(struct bootp
, vend
) > plen
)
3433 memcpy(&udp
, (char *)ip
+ ip_hlen
, sizeof(udp
));
3434 if (ntohs(udp
.uh_ulen
) < sizeof(udp
))
3436 if (ip_hlen
+ ntohs(udp
.uh_ulen
) > plen
)
3439 /* Check it's to the right port. */
3440 if (udp
.uh_dport
!= htons(BOOTPC
))
3446 /* Lengths have already been checked. */
3448 checksums_valid(void *packet
,
3449 struct in_addr
*from
, unsigned int flags
)
3451 struct ip
*ip
= packet
;
3454 uint16_t w
[sizeof(struct ip
) / 2];
3457 .ip_p
= IPPROTO_UDP
,
3458 .ip_src
= ip
->ip_src
,
3459 .ip_dst
= ip
->ip_dst
,
3464 char *udpp
, *uh_sump
;
3468 from
->s_addr
= ip
->ip_src
.s_addr
;
3470 ip_hlen
= (size_t)ip
->ip_hl
* 4;
3471 if (in_cksum(ip
, ip_hlen
, NULL
) != 0)
3474 if (flags
& BPF_PARTIALCSUM
)
3477 udpp
= (char *)ip
+ ip_hlen
;
3478 memcpy(&udp
, udpp
, sizeof(udp
));
3479 if (udp
.uh_sum
== 0)
3482 /* UDP checksum is based on a pseudo IP header alongside
3483 * the UDP header and payload. */
3484 pip
.ip
.ip_len
= udp
.uh_ulen
;
3487 /* Need to zero the UDP sum in the packet for the checksum to work. */
3488 uh_sump
= udpp
+ offsetof(struct udphdr
, uh_sum
);
3489 memset(uh_sump
, 0, sizeof(udp
.uh_sum
));
3491 /* Checksum pseudo header and then UDP + payload. */
3492 in_cksum(pip
.w
, sizeof(pip
.w
), &csum
);
3493 csum
= in_cksum(udpp
, ntohs(udp
.uh_ulen
), &csum
);
3495 #if 0 /* Not needed, just here for completeness. */
3496 /* Put the checksum back. */
3497 memcpy(uh_sump
, &udp
.uh_sum
, sizeof(udp
.uh_sum
));
3500 return csum
== udp
.uh_sum
;
3504 dhcp_handlebootp(struct interface
*ifp
, struct bootp
*bootp
, size_t len
,
3505 struct in_addr
*from
)
3509 if (len
< offsetof(struct bootp
, vend
)) {
3510 logerrx("%s: truncated packet (%zu) from %s",
3511 ifp
->name
, len
, inet_ntoa(*from
));
3515 /* Unlikely, but appeases sanitizers. */
3516 if (len
> FRAMELEN_MAX
) {
3517 logerrx("%s: packet exceeded frame length (%zu) from %s",
3518 ifp
->name
, len
, inet_ntoa(*from
));
3522 /* To make our IS_DHCP macro easy, ensure the vendor
3523 * area has at least 4 octets. */
3524 v
= len
- offsetof(struct bootp
, vend
);
3526 bootp
->vend
[v
++] = '\0';
3530 dhcp_handledhcp(ifp
, bootp
, len
, from
);
3534 dhcp_packet(struct interface
*ifp
, uint8_t *data
, size_t len
,
3535 unsigned int bpf_flags
)
3537 struct bootp
*bootp
;
3538 struct in_addr from
;
3540 size_t fl
= bpf_frame_header_len(ifp
);
3542 const struct dhcp_state
*state
= D_CSTATE(ifp
);
3544 /* It's possible that an interface departs and arrives in short
3545 * order to receive a BPF frame out of order.
3546 * There is a similar check in ARP, but much lower down the stack.
3547 * It's not needed for other inet protocols because we send the
3548 * message as a whole and select the interface off that and then
3549 * check state. BPF on the other hand is very interface
3550 * specific and we do need this check. */
3554 /* Ignore double reads */
3555 if (IN_PRIVSEP(ifp
->ctx
)) {
3556 switch (state
->state
) {
3557 case DHS_BOUND
: /* FALLTHROUGH */
3566 /* Trim frame header */
3569 logerrx("%s: %s: short frame header %zu",
3570 __func__
, ifp
->name
, len
);
3574 /* Move the data to avoid alignment errors. */
3575 memmove(data
, data
+ fl
, len
);
3578 /* Validate filter. */
3579 if (!is_packet_udp_bootp(data
, len
)) {
3581 logerrx("%s: DHCP BPF validation failure", ifp
->name
);
3586 if (!checksums_valid(data
, &from
, bpf_flags
)) {
3587 logerrx("%s: checksum failure from %s",
3588 ifp
->name
, inet_ntoa(from
));
3593 * DHCP has a variable option area rather than a fixed vendor area.
3594 * Because DHCP uses the BOOTP protocol it should still send BOOTP
3595 * sized packets to be RFC compliant.
3596 * However some servers send a truncated vendor area.
3597 * dhcpcd can work fine without the vendor area being sent.
3599 bootp
= get_udp_data(data
, &udp_len
);
3600 dhcp_handlebootp(ifp
, bootp
, udp_len
, &from
);
3604 dhcp_readbpf(void *arg
, unsigned short events
)
3606 struct interface
*ifp
= arg
;
3607 uint8_t buf
[FRAMELEN_MAX
];
3609 struct dhcp_state
*state
= D_STATE(ifp
);
3610 struct bpf
*bpf
= state
->bpf
;
3612 if (events
!= ELE_READ
)
3613 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
3615 bpf
->bpf_flags
&= ~BPF_EOF
;
3616 while (!(bpf
->bpf_flags
& BPF_EOF
)) {
3617 bytes
= bpf_read(bpf
, buf
, sizeof(buf
));
3619 if (state
->state
!= DHS_NONE
) {
3620 logerr("%s: %s", __func__
, ifp
->name
);
3625 dhcp_packet(ifp
, buf
, (size_t)bytes
, bpf
->bpf_flags
);
3626 /* Check we still have a state after processing. */
3627 if ((state
= D_STATE(ifp
)) == NULL
)
3629 if ((bpf
= state
->bpf
) == NULL
)
3635 dhcp_recvmsg(struct dhcpcd_ctx
*ctx
, struct msghdr
*msg
)
3637 struct sockaddr_in
*from
= (struct sockaddr_in
*)msg
->msg_name
;
3638 struct iovec
*iov
= &msg
->msg_iov
[0];
3639 struct interface
*ifp
;
3640 const struct dhcp_state
*state
;
3642 ifp
= if_findifpfromcmsg(ctx
, msg
, NULL
);
3647 state
= D_CSTATE(ifp
);
3648 if (state
== NULL
) {
3649 /* Try re-directing it to another interface. */
3650 dhcp_redirect_dhcp(ifp
, (struct bootp
*)iov
->iov_base
,
3651 iov
->iov_len
, &from
->sin_addr
);
3655 if (state
->bpf
!= NULL
) {
3656 /* Avoid a duplicate read if BPF is open for the interface. */
3660 if (IN_PRIVSEP(ctx
)) {
3661 switch (state
->state
) {
3662 case DHS_BOUND
: /* FALLTHROUGH */
3666 /* Any other state we ignore it or will receive
3673 dhcp_handlebootp(ifp
, iov
->iov_base
, iov
->iov_len
,
3678 dhcp_readudp(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
,
3679 unsigned short events
)
3681 const struct dhcp_state
*state
;
3682 struct sockaddr_in from
;
3685 uint8_t buf
[10 * 1024]; /* Maximum MTU */
3687 struct iovec iov
= {
3688 .iov_base
= iovbuf
.buf
,
3689 .iov_len
= sizeof(iovbuf
.buf
),
3694 uint8_t buf
[CMSG_SPACE(sizeof(struct sockaddr_dl
))];
3696 uint8_t buf
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
3698 } cmsgbuf
= { .buf
= { 0 } };
3699 struct msghdr msg
= {
3700 .msg_name
= &from
, .msg_namelen
= sizeof(from
),
3701 .msg_iov
= &iov
, .msg_iovlen
= 1,
3702 .msg_control
= cmsgbuf
.buf
, .msg_controllen
= sizeof(cmsgbuf
.buf
),
3707 if (events
!= ELE_READ
)
3708 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
3711 state
= D_CSTATE(ifp
);
3716 bytes
= recvmsg(s
, &msg
, 0);
3722 iov
.iov_len
= (size_t)bytes
;
3723 dhcp_recvmsg(ctx
, &msg
);
3727 dhcp_handleudp(void *arg
, unsigned short events
)
3729 struct dhcpcd_ctx
*ctx
= arg
;
3731 dhcp_readudp(ctx
, NULL
, events
);
3735 dhcp_handleifudp(void *arg
, unsigned short events
)
3737 struct interface
*ifp
= arg
;
3739 dhcp_readudp(ifp
->ctx
, ifp
, events
);
3743 dhcp_openbpf(struct interface
*ifp
)
3745 struct dhcp_state
*state
;
3747 state
= D_STATE(ifp
);
3750 if (IN_PRIVSEP_SE(ifp
->ctx
)) {
3751 if (ps_bpf_openbootp(ifp
) == -1) {
3759 if (state
->bpf
!= NULL
)
3762 state
->bpf
= bpf_open(ifp
, bpf_bootp
, NULL
);
3763 if (state
->bpf
== NULL
) {
3764 if (errno
== ENOENT
) {
3765 logerrx("%s not found", bpf_name
);
3766 /* May as well disable IPv4 entirely at
3767 * this point as we really need it. */
3768 ifp
->options
->options
&= ~DHCPCD_IPV4
;
3770 logerr("%s: %s", __func__
, ifp
->name
);
3774 if (eloop_event_add(ifp
->ctx
->eloop
, state
->bpf
->bpf_fd
, ELE_READ
,
3775 dhcp_readbpf
, ifp
) == -1)
3776 logerr("%s: eloop_event_add", __func__
);
3781 dhcp_free(struct interface
*ifp
)
3783 struct dhcp_state
*state
= D_STATE(ifp
);
3784 struct dhcpcd_ctx
*ctx
;
3791 state
->state
= DHS_NONE
;
3795 free(state
->clientid
);
3800 /* If we don't have any more DHCP enabled interfaces,
3801 * close the global socket and release resources */
3803 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
3804 state
= D_STATE(ifp
);
3805 if (state
!= NULL
&& state
->state
!= DHS_NONE
)
3810 if (ctx
->udp_rfd
!= -1) {
3811 eloop_event_delete(ctx
->eloop
, ctx
->udp_rfd
);
3812 close(ctx
->udp_rfd
);
3815 if (ctx
->udp_wfd
!= -1) {
3816 close(ctx
->udp_wfd
);
3820 free(ctx
->opt_buffer
);
3821 ctx
->opt_buffer
= NULL
;
3822 ctx
->opt_buffer_len
= 0;
3827 dhcp_initstate(struct interface
*ifp
)
3829 struct dhcp_state
*state
;
3831 state
= D_STATE(ifp
);
3835 ifp
->if_data
[IF_DATA_DHCP
] = calloc(1, sizeof(*state
));
3836 state
= D_STATE(ifp
);
3840 state
->state
= DHS_NONE
;
3841 /* 0 is a valid fd, so init to -1 */
3842 state
->udp_rfd
= -1;
3844 state
->arping_index
= -1;
3850 dhcp_init(struct interface
*ifp
)
3852 struct dhcp_state
*state
;
3853 struct if_options
*ifo
;
3855 char buf
[(sizeof(ifo
->clientid
) - 1) * 3];
3857 if (dhcp_initstate(ifp
) == -1)
3860 state
= D_STATE(ifp
);
3861 state
->state
= DHS_INIT
;
3862 state
->reason
= "PREINIT";
3864 dhcp_set_leasefile(state
->leasefile
, sizeof(state
->leasefile
),
3868 /* We need to drop the leasefile so that dhcp_start
3869 * doesn't load it. */
3870 if (ifo
->options
& DHCPCD_REQUEST
)
3871 dhcp_unlink(ifp
->ctx
, state
->leasefile
);
3873 free(state
->clientid
);
3874 state
->clientid
= NULL
;
3876 if (ifo
->options
& DHCPCD_ANONYMOUS
) {
3877 /* Removing the option could show that we want anonymous.
3878 * As such keep it as it's already in the hwaddr field. */
3880 } else if (*ifo
->clientid
) {
3881 state
->clientid
= malloc((size_t)(ifo
->clientid
[0] + 1));
3882 if (state
->clientid
== NULL
)
3884 memcpy(state
->clientid
, ifo
->clientid
,
3885 (size_t)(ifo
->clientid
[0]) + 1);
3886 } else if (ifo
->options
& DHCPCD_CLIENTID
) {
3887 if (ifo
->options
& DHCPCD_DUID
) {
3888 state
->clientid
= malloc(ifp
->ctx
->duid_len
+ 6);
3889 if (state
->clientid
== NULL
)
3891 state
->clientid
[0] =(uint8_t)(ifp
->ctx
->duid_len
+ 5);
3892 state
->clientid
[1] = 255; /* RFC 4361 */
3893 memcpy(state
->clientid
+ 2, ifo
->iaid
, 4);
3894 memcpy(state
->clientid
+ 6, ifp
->ctx
->duid
,
3895 ifp
->ctx
->duid_len
);
3898 len
= (uint8_t)(ifp
->hwlen
+ 1);
3899 state
->clientid
= malloc((size_t)len
+ 1);
3900 if (state
->clientid
== NULL
)
3902 state
->clientid
[0] = len
;
3903 state
->clientid
[1] = (uint8_t)ifp
->hwtype
;
3904 memcpy(state
->clientid
+ 2, ifp
->hwaddr
,
3909 if (ifo
->options
& DHCPCD_DUID
)
3910 /* Don't bother logging as DUID and IAID are reported
3911 * at device start. */
3914 if (ifo
->options
& DHCPCD_CLIENTID
&& state
->clientid
!= NULL
)
3915 logdebugx("%s: using ClientID %s", ifp
->name
,
3916 hwaddr_ntoa(state
->clientid
+ 1, state
->clientid
[0],
3918 else if (ifp
->hwlen
)
3919 logdebugx("%s: using hwaddr %s", ifp
->name
,
3920 hwaddr_ntoa(ifp
->hwaddr
, ifp
->hwlen
, buf
, sizeof(buf
)));
3929 dhcp_start1(void *arg
)
3931 struct interface
*ifp
= arg
;
3932 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
3933 struct if_options
*ifo
= ifp
->options
;
3934 struct dhcp_state
*state
;
3938 if (!(ifo
->options
& DHCPCD_IPV4
))
3941 /* Listen on *.*.*.*:bootpc so that the kernel never sends an
3942 * ICMP port unreachable message back to the DHCP server.
3943 * Only do this in manager mode so we don't swallow messages
3944 * for dhcpcd running on another interface. */
3945 if ((ctx
->options
& (DHCPCD_MANAGER
|DHCPCD_PRIVSEP
)) == DHCPCD_MANAGER
3946 && ctx
->udp_rfd
== -1)
3948 ctx
->udp_rfd
= dhcp_openudp(NULL
);
3949 if (ctx
->udp_rfd
== -1) {
3953 if (eloop_event_add(ctx
->eloop
, ctx
->udp_rfd
, ELE_READ
,
3954 dhcp_handleudp
, ctx
) == -1)
3955 logerr("%s: eloop_event_add", __func__
);
3957 if (!IN_PRIVSEP(ctx
) && ctx
->udp_wfd
== -1) {
3958 ctx
->udp_wfd
= xsocket(PF_INET
, SOCK_RAW
|SOCK_CXNB
,IPPROTO_UDP
);
3959 if (ctx
->udp_wfd
== -1) {
3965 if (dhcp_init(ifp
) == -1) {
3966 logerr("%s: dhcp_init", ifp
->name
);
3970 state
= D_STATE(ifp
);
3971 clock_gettime(CLOCK_MONOTONIC
, &state
->started
);
3972 state
->interval
= 0;
3974 state
->offer
= NULL
;
3975 state
->offer_len
= 0;
3978 if (ifo
->arping_len
&& state
->arping_index
< ifo
->arping_len
) {
3984 if (ifo
->options
& DHCPCD_STATIC
) {
3989 if (ifo
->options
& DHCPCD_INFORM
) {
3994 /* We don't want to read the old lease if we NAK an old test */
3995 nolease
= state
->offer
&& ifp
->ctx
->options
& DHCPCD_TEST
;
3996 if (!nolease
&& ifo
->options
& DHCPCD_DHCP
) {
3997 state
->offer_len
= read_lease(ifp
, &state
->offer
);
3998 /* Check the saved lease matches the type we want */
4000 #ifdef IN_IFF_DUPLICATED
4001 struct in_addr addr
;
4002 struct ipv4_addr
*ia
;
4004 addr
.s_addr
= state
->offer
->yiaddr
;
4005 ia
= ipv4_iffindaddr(ifp
, &addr
, NULL
);
4008 if ((!IS_DHCP(state
->offer
) &&
4009 !(ifo
->options
& DHCPCD_BOOTP
)) ||
4010 #ifdef IN_IFF_DUPLICATED
4011 (ia
&& ia
->addr_flags
& IN_IFF_DUPLICATED
) ||
4013 (IS_DHCP(state
->offer
) &&
4014 ifo
->options
& DHCPCD_BOOTP
))
4017 state
->offer
= NULL
;
4018 state
->offer_len
= 0;
4023 struct ipv4_addr
*ia
;
4026 get_lease(ifp
, &state
->lease
, state
->offer
, state
->offer_len
);
4027 state
->lease
.frominfo
= 1;
4028 if (state
->new == NULL
&&
4029 (ia
= ipv4_iffindaddr(ifp
,
4030 &state
->lease
.addr
, &state
->lease
.mask
)) != NULL
)
4032 /* We still have the IP address from the last lease.
4033 * Fake add the address and routes from it so the lease
4034 * can be cleaned up. */
4035 state
->new = malloc(state
->offer_len
);
4038 state
->offer
, state
->offer_len
);
4039 state
->new_len
= state
->offer_len
;
4041 state
->added
|= STATE_ADDED
| STATE_FAKE
;
4042 rt_build(ifp
->ctx
, AF_INET
);
4046 if (!IS_DHCP(state
->offer
)) {
4048 state
->offer
= NULL
;
4049 state
->offer_len
= 0;
4050 } else if (!(ifo
->options
& DHCPCD_LASTLEASE_EXTEND
) &&
4051 state
->lease
.leasetime
!= DHCP_INFINITE_LIFETIME
&&
4052 dhcp_filemtime(ifp
->ctx
, state
->leasefile
, &mtime
) == 0)
4056 /* Offset lease times and check expiry */
4059 (time_t)state
->lease
.leasetime
< now
- mtime
)
4061 logdebugx("%s: discarding expired lease",
4064 state
->offer
= NULL
;
4065 state
->offer_len
= 0;
4066 state
->lease
.addr
.s_addr
= 0;
4067 /* Technically we should discard the lease
4068 * as it's expired, just as DHCPv6 addresses
4069 * would be by the kernel.
4070 * However, this may violate POLA so
4071 * we currently leave it be.
4072 * If we get a totally different lease from
4073 * the DHCP server we'll drop it anyway, as
4074 * we will on any other event which would
4075 * trigger a lease drop.
4076 * This should only happen if dhcpcd stops
4077 * running and the lease expires before
4078 * dhcpcd starts again. */
4081 dhcp_drop(ifp
, "EXPIRE");
4084 l
= (uint32_t)(now
- mtime
);
4085 state
->lease
.leasetime
-= l
;
4086 state
->lease
.renewaltime
-= l
;
4087 state
->lease
.rebindtime
-= l
;
4093 if (!(ifo
->options
& DHCPCD_DHCP
)) {
4094 if (ifo
->options
& DHCPCD_IPV4LL
)
4100 if (state
->offer
== NULL
||
4101 !IS_DHCP(state
->offer
) ||
4102 ifo
->options
& DHCPCD_ANONYMOUS
)
4109 dhcp_start(struct interface
*ifp
)
4113 const struct dhcp_state
*state
;
4116 if (!(ifp
->options
->options
& DHCPCD_IPV4
))
4119 /* If we haven't been given a netmask for our requested address,
4121 if (ifp
->options
->req_addr
.s_addr
!= INADDR_ANY
&&
4122 ifp
->options
->req_mask
.s_addr
== INADDR_ANY
)
4123 ifp
->options
->req_mask
.s_addr
=
4124 ipv4_getnetmask(ifp
->options
->req_addr
.s_addr
);
4126 /* If we haven't specified a ClientID and our hardware address
4127 * length is greater than BOOTP CHADDR then we enforce a ClientID
4128 * of the hardware address type and the hardware address.
4129 * If there is no hardware address and no ClientID set,
4130 * force a DUID based ClientID. */
4131 if (ifp
->hwlen
> 16)
4132 ifp
->options
->options
|= DHCPCD_CLIENTID
;
4133 else if (ifp
->hwlen
== 0 && !(ifp
->options
->options
& DHCPCD_CLIENTID
))
4134 ifp
->options
->options
|= DHCPCD_CLIENTID
| DHCPCD_DUID
;
4136 /* Firewire and InfiniBand interfaces require ClientID and
4137 * the broadcast option being set. */
4138 switch (ifp
->hwtype
) {
4139 case ARPHRD_IEEE1394
: /* FALLTHROUGH */
4140 case ARPHRD_INFINIBAND
:
4141 ifp
->options
->options
|= DHCPCD_CLIENTID
| DHCPCD_BROADCAST
;
4145 /* If we violate RFC2131 section 3.7 then require ARP
4146 * to detect if any other client wants our address. */
4147 if (ifp
->options
->options
& DHCPCD_LASTLEASE_EXTEND
)
4148 ifp
->options
->options
|= DHCPCD_ARP
;
4150 /* No point in delaying a static configuration */
4151 if (ifp
->options
->options
& DHCPCD_STATIC
||
4152 !(ifp
->options
->options
& DHCPCD_INITIAL_DELAY
))
4159 /* If we have arpinged then we have already delayed. */
4160 state
= D_CSTATE(ifp
);
4161 if (state
!= NULL
&& state
->arping_index
!= -1) {
4166 delay
= MSEC_PER_SEC
+
4167 (arc4random_uniform(MSEC_PER_SEC
* 2) - MSEC_PER_SEC
);
4168 logdebugx("%s: delaying IPv4 for %0.1f seconds",
4169 ifp
->name
, (float)delay
/ MSEC_PER_SEC
);
4171 eloop_timeout_add_msec(ifp
->ctx
->eloop
, delay
, dhcp_start1
, ifp
);
4175 dhcp_abort(struct interface
*ifp
)
4177 struct dhcp_state
*state
;
4179 state
= D_STATE(ifp
);
4182 state
->arping_index
= -1;
4185 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp_start1
, ifp
);
4187 if (state
!= NULL
&& state
->added
) {
4188 rt_build(ifp
->ctx
, AF_INET
);
4190 if (ifp
->options
->options
& DHCPCD_ARP
)
4191 arp_announceaddr(ifp
->ctx
, &state
->addr
->addr
);
4197 dhcp_handleifa(int cmd
, struct ipv4_addr
*ia
, pid_t pid
)
4199 struct interface
*ifp
;
4200 struct dhcp_state
*state
;
4201 struct if_options
*ifo
;
4205 state
= D_STATE(ifp
);
4206 if (state
== NULL
|| state
->state
== DHS_NONE
)
4209 if (cmd
== RTM_DELADDR
) {
4210 if (state
->addr
== ia
) {
4211 loginfox("%s: pid %d deleted IP address %s",
4212 ifp
->name
, pid
, ia
->saddr
);
4215 /* Don't clear the added state as we need
4216 * to drop the lease. */
4217 dhcp_drop(ifp
, "EXPIRE");
4223 if (cmd
!= RTM_NEWADDR
)
4226 #ifdef IN_IFF_NOTUSEABLE
4227 if (!(ia
->addr_flags
& IN_IFF_NOTUSEABLE
))
4228 dhcp_finish_dad(ifp
, &ia
->addr
);
4229 else if (ia
->addr_flags
& IN_IFF_DUPLICATED
)
4230 return dhcp_addr_duplicated(ifp
, &ia
->addr
) ? NULL
: ia
;
4236 if (IN_PRIVSEP_SE(ifp
->ctx
) &&
4237 !(ifp
->ctx
->options
& (DHCPCD_MANAGER
| DHCPCD_CONFIGURE
)) &&
4238 IN_ARE_ADDR_EQUAL(&state
->lease
.addr
, &ia
->addr
))
4241 state
->added
= STATE_ADDED
;
4243 if (ps_inet_openbootp(ia
) == -1)
4248 /* If we have requested a specific address, return now.
4249 * The below code is only for when inform or static has been
4250 * requested without a specific address. */
4251 if (ifo
->req_addr
.s_addr
!= INADDR_ANY
)
4254 /* Only inform if we are NOT in the inform state or bound. */
4255 if (ifo
->options
& DHCPCD_INFORM
) {
4256 if (state
->state
!= DHS_INFORM
&& state
->state
!= DHS_BOUND
)
4261 /* Static and inform are mutually exclusive. If not static, return. */
4262 if (!(ifo
->options
& DHCPCD_STATIC
))
4266 state
->old
= state
->new;
4267 state
->new_len
= dhcp_message_new(&state
->new, &ia
->addr
, &ia
->mask
);
4268 if (state
->new == NULL
)
4271 if (ifp
->flags
& IFF_POINTOPOINT
) {
4272 for (i
= 1; i
< 255; i
++)
4273 if (i
!= DHO_ROUTER
&& has_option_mask(ifo
->dstmask
,i
))
4274 dhcp_message_add_addr(state
->new, i
, ia
->brd
);
4277 state
->reason
= "STATIC";
4278 rt_build(ifp
->ctx
, AF_INET
);
4279 script_runreason(ifp
, state
->reason
);
4286 dhcp_dump(struct interface
*ifp
)
4288 struct dhcp_state
*state
;
4290 ifp
->if_data
[IF_DATA_DHCP
] = state
= calloc(1, sizeof(*state
));
4291 if (state
== NULL
) {
4295 state
->new_len
= read_lease(ifp
, &state
->new);
4296 if (state
->new == NULL
) {
4297 logerr("read_lease");
4300 state
->reason
= "DUMP";
4301 return script_runreason(ifp
, state
->reason
);