]>
git.ipfire.org Git - thirdparty/dhcpcd.git/blob - dhcp.c
d4ba47c9b6acf638c2f554129e2256d530290733
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/socket.h>
33 # include <asm/types.h> /* for systems with broken headers */
34 # include <linux/rtnetlink.h>
37 #include <arpa/inet.h>
39 #include <net/route.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
45 #include <netinet/udp.h>
63 #include "dhcp-common.h"
71 #define DAD "Duplicate address detected"
72 #define DHCP_MIN_LEASE 20
74 #define IPV4A ADDRIPV4 | ARRAY
75 #define IPV4R ADDRIPV4 | REQUEST
77 /* We should define a maximum for the NAK exponential backoff */
80 /* Wait N nanoseconds between sending a RELEASE and dropping the address.
81 * This gives the kernel enough time to actually send it. */
82 #define RELEASE_DELAY_S 0
83 #define RELEASE_DELAY_NS 10000000
86 #define IPDEFTTL 64 /* RFC1340 */
94 static const struct dhcp_op dhcp_ops
[] = {
95 { DHCP_DISCOVER
, "DISCOVER" },
96 { DHCP_OFFER
, "OFFER" },
97 { DHCP_REQUEST
, "REQUEST" },
98 { DHCP_DECLINE
, "DECLINE" },
101 { DHCP_RELEASE
, "RELEASE" },
102 { DHCP_INFORM
, "INFORM" },
103 { DHCP_FORCERENEW
, "DHCP_FORCERENEW"},
107 static const char * const dhcp_params
[] = {
116 struct udp_dhcp_packet
120 struct dhcp_message dhcp
;
123 static const size_t udp_dhcp_len
= sizeof(struct udp_dhcp_packet
);
125 static int dhcp_open(struct interface
*);
128 dhcp_printoptions(const struct dhcpcd_ctx
*ctx
)
130 const char * const *p
;
132 const struct dhcp_opt
*opt
;
134 for (p
= dhcp_params
; *p
; p
++)
137 for (i
= 0, opt
= ctx
->dhcp_opts
; i
< ctx
->dhcp_opts_len
; i
++, opt
++)
138 printf("%03d %s\n", opt
->option
, opt
->var
);
141 #define get_option_raw(ctx, dhcp, opt) get_option(ctx, dhcp, opt, NULL)
142 static const uint8_t *
143 get_option(struct dhcpcd_ctx
*ctx
,
144 const struct dhcp_message
*dhcp
, unsigned int opt
, size_t *len
)
146 const uint8_t *p
= dhcp
->options
;
147 const uint8_t *e
= p
+ sizeof(dhcp
->options
);
152 const uint8_t *op
= NULL
;
159 if (!ctx
->opt_buffer
) {
161 malloc(DHCP_OPTION_LEN
+
162 BOOTFILE_LEN
+ SERVERNAME_LEN
);
163 if (ctx
->opt_buffer
== NULL
)
167 bp
= ctx
->opt_buffer
;
184 /* bit 1 set means parse boot file */
187 e
= p
+ sizeof(dhcp
->bootfile
);
188 } else if (overl
& 2) {
189 /* bit 2 set means parse server name */
191 p
= dhcp
->servername
;
192 e
= p
+ sizeof(dhcp
->servername
);
196 case DHO_OPTIONSOVERLOADED
:
197 /* Ensure we only get this option once */
211 return (const uint8_t *)ctx
->opt_buffer
;
220 get_option_addr(struct dhcpcd_ctx
*ctx
,
221 struct in_addr
*a
, const struct dhcp_message
*dhcp
,
227 p
= get_option(ctx
, dhcp
, option
, &len
);
228 if (!p
|| len
< (ssize_t
)sizeof(a
->s_addr
))
230 memcpy(&a
->s_addr
, p
, sizeof(a
->s_addr
));
235 get_option_uint32(struct dhcpcd_ctx
*ctx
,
236 uint32_t *i
, const struct dhcp_message
*dhcp
, uint8_t option
)
242 p
= get_option(ctx
, dhcp
, option
, &len
);
243 if (!p
|| len
< (ssize_t
)sizeof(d
))
245 memcpy(&d
, p
, sizeof(d
));
252 get_option_uint8(struct dhcpcd_ctx
*ctx
,
253 uint8_t *i
, const struct dhcp_message
*dhcp
, uint8_t option
)
258 p
= get_option(ctx
, dhcp
, option
, &len
);
259 if (!p
|| len
< (ssize_t
)sizeof(*p
))
267 decode_rfc3442(char *out
, size_t len
, const uint8_t *p
, size_t pl
)
270 size_t bytes
= 0, ocets
;
276 /* Minimum is 5 -first is CIDR and a router length of 4 */
289 ocets
= (cidr
+ 7) / 8;
292 bytes
+= ((4 * 4) * 2) + 4;
295 if ((((4 * 4) * 2) + 4) > len
) {
303 /* If we have ocets then we have a destination and netmask */
306 memcpy(&addr
.s_addr
, p
, ocets
);
307 b
= snprintf(o
, len
, "%s/%d", inet_ntoa(addr
), cidr
);
310 b
= snprintf(o
, len
, "0.0.0.0/0");
314 /* Finally, snag the router */
315 memcpy(&addr
.s_addr
, p
, 4);
317 b
= snprintf(o
, len
, " %s", inet_ntoa(addr
));
324 return (ssize_t
)bytes
;
327 static struct rt_head
*
328 decode_rfc3442_rt(const uint8_t *data
, size_t dl
)
330 const uint8_t *p
= data
;
334 struct rt_head
*routes
;
335 struct rt
*rt
= NULL
;
337 /* Minimum is 5 -first is CIDR and a router length of 4 */
341 routes
= malloc(sizeof(*routes
));
347 ipv4_freeroutes(routes
);
352 rt
= calloc(1, sizeof(*rt
));
354 syslog(LOG_ERR
, "%s: %m", __func__
);
355 ipv4_freeroutes(routes
);
358 TAILQ_INSERT_TAIL(routes
, rt
, next
);
360 ocets
= (cidr
+ 7) / 8;
361 /* If we have ocets then we have a destination and netmask */
363 memcpy(&rt
->dest
.s_addr
, p
, ocets
);
365 rt
->net
.s_addr
= htonl(~0U << (32 - cidr
));
368 /* Finally, snag the router */
369 memcpy(&rt
->gate
.s_addr
, p
, 4);
376 decode_rfc3361(const uint8_t *data
, size_t dl
)
394 if ((r
= decode_rfc3397(NULL
, 0, data
, dl
)) > 0) {
399 decode_rfc3397(sip
, l
, data
, dl
);
403 if (dl
== 0 || dl
% 4 != 0) {
407 addr
.s_addr
= INADDR_BROADCAST
;
408 l
= ((dl
/ sizeof(addr
.s_addr
)) * ((4 * 4) + 1)) + 1;
413 memcpy(&addr
.s_addr
, data
, sizeof(addr
.s_addr
));
414 data
+= sizeof(addr
.s_addr
);
415 p
+= snprintf(p
, l
- (size_t)(p
- sip
),
416 "%s ", inet_ntoa(addr
));
417 dl
-= sizeof(addr
.s_addr
);
429 /* Decode an RFC5969 6rd order option into a space
430 * separated string. Returns length of string (including
431 * terminating zero) or zero on error. */
433 decode_rfc5969(char *out
, size_t len
, const uint8_t *p
, size_t pl
)
435 uint8_t ipv4masklen
, ipv6prefixlen
;
436 uint8_t ipv6prefix
[16];
439 ssize_t b
, bytes
= 0;
448 ipv6prefixlen
= *p
++;
451 for (i
= 0; i
< 16; i
++) {
452 ipv6prefix
[i
] = *p
++;
456 b
= snprintf(out
, len
,
462 ipv4masklen
, ipv6prefixlen
,
463 ipv6prefix
[0], ipv6prefix
[1], ipv6prefix
[2], ipv6prefix
[3],
464 ipv6prefix
[4], ipv6prefix
[5], ipv6prefix
[6], ipv6prefix
[7],
465 ipv6prefix
[8], ipv6prefix
[9], ipv6prefix
[10],ipv6prefix
[11],
466 ipv6prefix
[12],ipv6prefix
[13],ipv6prefix
[14], ipv6prefix
[15]
473 bytes
+= 16 * 2 + 8 + 2 + 1 + 2;
484 b
= snprintf(out
, len
, " %d.%d.%d.%d",
485 br
[0], br
[1], br
[2], br
[3]);
498 get_option_string(struct dhcpcd_ctx
*ctx
,
499 const struct dhcp_message
*dhcp
, uint8_t option
)
505 p
= get_option(ctx
, dhcp
, option
, &len
);
506 if (!p
|| len
== 0 || *p
== '\0')
509 s
= malloc(sizeof(char) * (len
+ 1));
517 /* This calculates the netmask that we should use for static routes.
518 * This IS different from the calculation used to calculate the netmask
519 * for an interface address. */
521 route_netmask(uint32_t ip_in
)
523 /* used to be unsigned long - check if error */
524 uint32_t p
= ntohl(ip_in
);
546 /* We need to obey routing options.
547 * If we have a CSR then we only use that.
548 * Otherwise we add static routes and then routers. */
550 get_option_routes(struct interface
*ifp
, const struct dhcp_message
*dhcp
)
552 struct if_options
*ifo
= ifp
->options
;
555 struct rt_head
*routes
= NULL
;
556 struct rt
*route
= NULL
;
558 const char *csr
= "";
560 /* If we have CSR's then we MUST use these only */
561 if (!has_option_mask(ifo
->nomask
, DHO_CSR
))
562 p
= get_option(ifp
->ctx
, dhcp
, DHO_CSR
, &len
);
565 /* Check for crappy MS option */
566 if (!p
&& !has_option_mask(ifo
->nomask
, DHO_MSCSR
)) {
567 p
= get_option(ifp
->ctx
, dhcp
, DHO_MSCSR
, &len
);
572 routes
= decode_rfc3442_rt(p
, len
);
574 if (!(ifo
->options
& DHCPCD_CSR_WARNED
)) {
576 "%s: using %sClassless Static Routes",
578 ifo
->options
|= DHCPCD_CSR_WARNED
;
584 /* OK, get our static routes first. */
585 routes
= malloc(sizeof(*routes
));
586 if (routes
== NULL
) {
587 syslog(LOG_ERR
, "%s: %m", __func__
);
591 if (!has_option_mask(ifo
->nomask
, DHO_STATICROUTE
))
592 p
= get_option(ifp
->ctx
, dhcp
, DHO_STATICROUTE
, &len
);
598 route
= calloc(1, sizeof(*route
));
600 syslog(LOG_ERR
, "%s: %m", __func__
);
601 ipv4_freeroutes(routes
);
604 memcpy(&route
->dest
.s_addr
, p
, 4);
606 memcpy(&route
->gate
.s_addr
, p
, 4);
608 route
->net
.s_addr
= route_netmask(route
->dest
.s_addr
);
609 TAILQ_INSERT_TAIL(routes
, route
, next
);
613 /* Now grab our routers */
614 if (!has_option_mask(ifo
->nomask
, DHO_ROUTER
))
615 p
= get_option(ifp
->ctx
, dhcp
, DHO_ROUTER
, &len
);
621 route
= calloc(1, sizeof(*route
));
623 syslog(LOG_ERR
, "%s: %m", __func__
);
624 ipv4_freeroutes(routes
);
627 memcpy(&route
->gate
.s_addr
, p
, 4);
629 TAILQ_INSERT_TAIL(routes
, route
, next
);
636 #define PUTADDR(_type, _val) \
640 memcpy(p, &_val.s_addr, 4); \
645 dhcp_message_add_addr(struct dhcp_message
*dhcp
,
646 uint8_t type
, struct in_addr addr
)
652 while (*p
!= DHO_END
) {
657 len
= (size_t)(p
- (uint8_t *)dhcp
);
658 if (len
+ 6 > sizeof(*dhcp
)) {
669 make_message(struct dhcp_message
**message
,
670 const struct interface
*iface
,
673 struct dhcp_message
*dhcp
;
674 uint8_t *m
, *lp
, *p
, *auth
;
675 uint8_t *n_params
= NULL
;
678 size_t len
, i
, auth_len
;
679 const struct dhcp_opt
*opt
;
680 struct if_options
*ifo
= iface
->options
;
681 const struct dhcp_state
*state
= D_CSTATE(iface
);
682 const struct dhcp_lease
*lease
= &state
->lease
;
683 time_t up
= uptime() - state
->start_uptime
;
684 char hbuf
[HOSTNAME_MAX_LEN
+ 1];
685 const char *hostname
;
686 const struct vivco
*vivco
;
688 dhcp
= calloc(1, sizeof (*dhcp
));
694 if ((type
== DHCP_INFORM
|| type
== DHCP_RELEASE
||
695 (type
== DHCP_REQUEST
&&
696 state
->net
.s_addr
== lease
->net
.s_addr
&&
697 (state
->new == NULL
||
698 state
->new->cookie
== htonl(MAGIC_COOKIE
)))))
700 dhcp
->ciaddr
= state
->addr
.s_addr
;
701 /* In-case we haven't actually configured the address yet */
702 if (type
== DHCP_INFORM
&& state
->addr
.s_addr
== 0)
703 dhcp
->ciaddr
= lease
->addr
.s_addr
;
706 dhcp
->op
= DHCP_BOOTREQUEST
;
707 dhcp
->hwtype
= (uint8_t)iface
->family
;
708 switch (iface
->family
) {
711 dhcp
->hwlen
= (uint8_t)iface
->hwlen
;
712 memcpy(&dhcp
->chaddr
, &iface
->hwaddr
, iface
->hwlen
);
716 if (ifo
->options
& DHCPCD_BROADCAST
&&
718 type
!= DHCP_DECLINE
&&
719 type
!= DHCP_RELEASE
)
720 dhcp
->flags
= htons(BROADCAST_FLAG
);
722 if (type
!= DHCP_DECLINE
&& type
!= DHCP_RELEASE
) {
723 if (up
< 0 || up
> (time_t)UINT16_MAX
)
724 dhcp
->secs
= htons((uint16_t)UINT16_MAX
);
726 dhcp
->secs
= htons(up
);
728 dhcp
->xid
= htonl(state
->xid
);
729 dhcp
->cookie
= htonl(MAGIC_COOKIE
);
731 *p
++ = DHO_MESSAGETYPE
;
735 if (state
->clientid
) {
737 memcpy(p
, state
->clientid
, state
->clientid
[0] + 1);
738 p
+= state
->clientid
[0] + 1;
741 if (lease
->addr
.s_addr
&& lease
->cookie
== htonl(MAGIC_COOKIE
)) {
742 if (type
== DHCP_DECLINE
||
743 (type
== DHCP_REQUEST
&&
744 lease
->addr
.s_addr
!= state
->addr
.s_addr
))
746 PUTADDR(DHO_IPADDRESS
, lease
->addr
);
747 if (lease
->server
.s_addr
)
748 PUTADDR(DHO_SERVERID
, lease
->server
);
751 if (type
== DHCP_RELEASE
) {
752 if (lease
->server
.s_addr
)
753 PUTADDR(DHO_SERVERID
, lease
->server
);
757 if (type
== DHCP_DECLINE
) {
765 if (type
== DHCP_DISCOVER
&&
766 !(iface
->ctx
->options
& DHCPCD_TEST
) &&
767 has_option_mask(ifo
->requestmask
, DHO_RAPIDCOMMIT
))
769 /* RFC 4039 Section 3 */
770 *p
++ = DHO_RAPIDCOMMIT
;
774 if (type
== DHCP_DISCOVER
&& ifo
->options
& DHCPCD_REQUEST
)
775 PUTADDR(DHO_IPADDRESS
, ifo
->req_addr
);
777 /* RFC 2563 Auto Configure */
778 if (type
== DHCP_DISCOVER
&& ifo
->options
& DHCPCD_IPV4LL
) {
779 *p
++ = DHO_AUTOCONFIGURE
;
784 if (type
== DHCP_DISCOVER
||
785 type
== DHCP_INFORM
||
786 type
== DHCP_REQUEST
)
790 *p
++ = DHO_MAXMESSAGESIZE
;
792 mtu
= if_getmtu(iface
->name
);
794 if (if_setmtu(iface
->name
, MTU_MIN
) == 0)
796 } else if (mtu
> MTU_MAX
) {
797 /* Even though our MTU could be greater than
798 * MTU_MAX (1500) dhcpcd does not presently
799 * handle DHCP packets any bigger. */
806 if (ifo
->userclass
[0]) {
807 *p
++ = DHO_USERCLASS
;
808 memcpy(p
, ifo
->userclass
, ifo
->userclass
[0] + 1);
809 p
+= ifo
->userclass
[0] + 1;
812 if (ifo
->vendorclassid
[0]) {
813 *p
++ = DHO_VENDORCLASSID
;
814 memcpy(p
, ifo
->vendorclassid
,
815 ifo
->vendorclassid
[0] + 1);
816 p
+= ifo
->vendorclassid
[0] + 1;
820 if (type
!= DHCP_INFORM
) {
821 if (ifo
->leasetime
!= 0) {
822 *p
++ = DHO_LEASETIME
;
824 ul
= htonl(ifo
->leasetime
);
830 if (ifo
->hostname
[0] == '\0')
831 hostname
= get_hostname(hbuf
, sizeof(hbuf
),
832 ifo
->options
& DHCPCD_HOSTNAME_SHORT
? 1 : 0);
834 hostname
= ifo
->hostname
;
837 * RFC4702 3.1 States that if we send the Client FQDN option
838 * then we MUST NOT also send the Host Name option.
839 * Technically we could, but that is not RFC conformant and
840 * also seems to break some DHCP server implemetations such as
841 * Windows. On the other hand, ISC dhcpd is just as non RFC
842 * conformant by not accepting a partially qualified FQDN.
844 if (ifo
->fqdn
!= FQDN_DISABLE
) {
845 /* IETF DHC-FQDN option (81), RFC4702 */
851 * S: 1 => Client requests Server to update
852 * a RR in DNS as well as PTR
853 * O: 1 => Server indicates to client that
854 * DNS has been updated
855 * E: 1 => Name data is DNS format
856 * N: 1 => Client requests Server to not
860 *p
++ = (ifo
->fqdn
& 0x09) | 0x04;
862 *p
++ = (FQDN_NONE
& 0x09) | 0x04;
863 *p
++ = 0; /* from server for PTR RR */
864 *p
++ = 0; /* from server for A RR if S=1 */
866 i
= encode_rfc1035(hostname
, p
);
870 } else if (ifo
->options
& DHCPCD_HOSTNAME
&& hostname
) {
872 len
= strlen(hostname
);
874 memcpy(p
, hostname
, len
);
878 /* vendor is already encoded correctly, so just add it */
879 if (ifo
->vendor
[0]) {
881 memcpy(p
, ifo
->vendor
, ifo
->vendor
[0] + 1);
882 p
+= ifo
->vendor
[0] + 1;
885 if ((ifo
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) !=
886 DHCPCD_AUTH_SENDREQUIRE
)
888 /* We support HMAC-MD5 */
889 *p
++ = DHO_FORCERENEW_NONCE
;
891 *p
++ = AUTH_ALG_HMAC_MD5
;
894 if (ifo
->vivco_len
) {
898 ul
= htonl(ifo
->vivco_en
);
899 memcpy(p
, &ul
, sizeof(ul
));
901 for (i
= 0, vivco
= ifo
->vivco
;
905 len
= (size_t)(p
- m
) + vivco
->len
+ 1;
906 if (len
> sizeof(*dhcp
))
908 if (vivco
->len
+ 2 + *lp
> 255) {
910 "%s: VIVCO option too big",
915 *p
++ = (uint8_t)vivco
->len
;
916 memcpy(p
, vivco
->data
, vivco
->len
);
918 *lp
+= (uint8_t)vivco
->len
+ 1;
922 len
= (size_t)((p
- m
) + 3);
923 if (len
> sizeof(*dhcp
))
925 *p
++ = DHO_PARAMETERREQUESTLIST
;
928 for (i
= 0, opt
= iface
->ctx
->dhcp_opts
;
929 i
< iface
->ctx
->dhcp_opts_len
;
932 if (!(opt
->type
& REQUEST
||
933 has_option_mask(ifo
->requestmask
, opt
->option
)))
935 if (opt
->type
& NOREQ
)
937 if (type
== DHCP_INFORM
&&
938 (opt
->option
== DHO_RENEWALTIME
||
939 opt
->option
== DHO_REBINDTIME
))
941 len
= (size_t)((p
- m
) + 2);
942 if (len
> sizeof(*dhcp
))
944 *p
++ = (uint8_t)opt
->option
;
946 *n_params
= (uint8_t)(p
- n_params
- 1);
953 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
954 auth_len
= (size_t)dhcp_auth_encode(&ifo
->auth
,
956 NULL
, 0, 4, type
, NULL
, 0);
958 len
= (size_t)((p
+ auth_len
) - m
);
959 if (auth_len
> 255 || len
> sizeof(*dhcp
))
961 *p
++ = DHO_AUTHENTICATION
;
962 *p
++ = (uint8_t)auth_len
;
965 } else if ((ssize_t
)auth_len
== -1)
966 syslog(LOG_ERR
, "%s: dhcp_auth_encode: %m",
972 #ifdef BOOTP_MESSAGE_LENTH_MIN
973 /* Some crappy DHCP servers think they have to obey the BOOTP minimum
975 * They are wrong, but we should still cater for them. */
976 while (p
- m
< BOOTP_MESSAGE_LENTH_MIN
)
980 len
= (size_t)(p
- m
);
981 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
&& auth_len
> 0)
982 dhcp_auth_encode(&ifo
->auth
, state
->auth
.token
,
983 m
, len
, 4, type
, auth
, auth_len
);
989 syslog(LOG_ERR
, "%s: DHCP messge too big", iface
->name
);
995 write_lease(const struct interface
*ifp
, const struct dhcp_message
*dhcp
)
1000 const uint8_t *e
, *p
;
1003 const struct dhcp_state
*state
= D_CSTATE(ifp
);
1005 /* We don't write BOOTP leases */
1006 if (is_bootp(ifp
, dhcp
)) {
1007 unlink(state
->leasefile
);
1011 syslog(LOG_DEBUG
, "%s: writing lease `%s'",
1012 ifp
->name
, state
->leasefile
);
1014 fd
= open(state
->leasefile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1018 /* Only write as much as we need */
1020 e
= p
+ sizeof(dhcp
->options
);
1021 len
= sizeof(*dhcp
);
1025 len
= (size_t)(p
- (const uint8_t *)dhcp
);
1034 bytes
= write(fd
, dhcp
, len
);
1039 struct dhcp_message
*
1040 read_lease(struct interface
*ifp
)
1043 struct dhcp_message
*dhcp
;
1044 struct dhcp_state
*state
= D_STATE(ifp
);
1046 const uint8_t *auth
;
1050 fd
= open(state
->leasefile
, O_RDONLY
);
1052 if (errno
!= ENOENT
)
1053 syslog(LOG_ERR
, "%s: open `%s': %m",
1054 ifp
->name
, state
->leasefile
);
1057 syslog(LOG_DEBUG
, "%s: reading lease `%s'",
1058 ifp
->name
, state
->leasefile
);
1059 dhcp
= calloc(1, sizeof(*dhcp
));
1064 bytes
= read(fd
, dhcp
, sizeof(*dhcp
));
1071 /* We may have found a BOOTP server */
1072 if (get_option_uint8(ifp
->ctx
, &type
, dhcp
, DHO_MESSAGETYPE
) == -1)
1074 /* Authenticate the message */
1075 auth
= get_option(ifp
->ctx
, dhcp
, DHO_AUTHENTICATION
, &auth_len
);
1077 if (dhcp_auth_validate(&state
->auth
, &ifp
->options
->auth
,
1078 (uint8_t *)dhcp
, sizeof(*dhcp
), 4, type
,
1079 auth
, auth_len
) == NULL
)
1081 syslog(LOG_DEBUG
, "%s: dhcp_auth_validate: %m",
1086 if (state
->auth
.token
)
1087 syslog(LOG_DEBUG
, "%s: validated using 0x%08" PRIu32
,
1088 ifp
->name
, state
->auth
.token
->secretid
);
1090 syslog(LOG_DEBUG
, "%s: accepted reconfigure key",
1097 static const struct dhcp_opt
*
1098 dhcp_getoverride(const struct if_options
*ifo
, unsigned int o
)
1101 const struct dhcp_opt
*opt
;
1103 for (i
= 0, opt
= ifo
->dhcp_override
;
1104 i
< ifo
->dhcp_override_len
;
1107 if (opt
->option
== o
)
1113 static const uint8_t *
1114 dhcp_getoption(struct dhcpcd_ctx
*ctx
,
1115 size_t *os
, unsigned int *code
, size_t *len
,
1116 const uint8_t *od
, size_t ol
, struct dhcp_opt
**oopt
)
1119 struct dhcp_opt
*opt
;
1126 *os
= 2; /* code + len */
1127 *code
= (unsigned int)*od
++;
1128 *len
= (size_t)*od
++;
1135 for (i
= 0, opt
= ctx
->dhcp_opts
; i
< ctx
->dhcp_opts_len
; i
++, opt
++) {
1136 if (opt
->option
== *code
) {
1146 dhcp_env(char **env
, const char *prefix
, const struct dhcp_message
*dhcp
,
1147 const struct interface
*ifp
)
1149 const struct if_options
*ifo
;
1151 struct in_addr addr
;
1154 struct dhcp_opt
*opt
, *vo
;
1163 get_option_uint8(ifp
->ctx
, &overl
, dhcp
, DHO_OPTIONSOVERLOADED
);
1166 if (dhcp
->yiaddr
|| dhcp
->ciaddr
)
1168 if (*dhcp
->bootfile
&& !(overl
& 1))
1170 if (*dhcp
->servername
&& !(overl
& 2))
1172 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
1173 i
< ifp
->ctx
->dhcp_opts_len
;
1176 if (has_option_mask(ifo
->nomask
, opt
->option
))
1178 if (dhcp_getoverride(ifo
, opt
->option
))
1180 p
= get_option(ifp
->ctx
, dhcp
, opt
->option
, &pl
);
1183 e
+= dhcp_envoption(ifp
->ctx
, NULL
, NULL
, ifp
->name
,
1184 opt
, dhcp_getoption
, p
, pl
);
1186 for (i
= 0, opt
= ifo
->dhcp_override
;
1187 i
< ifo
->dhcp_override_len
;
1190 if (has_option_mask(ifo
->nomask
, opt
->option
))
1192 p
= get_option(ifp
->ctx
, dhcp
, opt
->option
, &pl
);
1195 e
+= dhcp_envoption(ifp
->ctx
, NULL
, NULL
, ifp
->name
,
1196 opt
, dhcp_getoption
, p
, pl
);
1202 if (dhcp
->yiaddr
|| dhcp
->ciaddr
) {
1203 /* Set some useful variables that we derive from the DHCP
1204 * message but are not necessarily in the options */
1205 addr
.s_addr
= dhcp
->yiaddr
? dhcp
->yiaddr
: dhcp
->ciaddr
;
1206 setvar(&ep
, prefix
, "ip_address", inet_ntoa(addr
));
1207 if (get_option_addr(ifp
->ctx
, &net
,
1208 dhcp
, DHO_SUBNETMASK
) == -1) {
1209 net
.s_addr
= ipv4_getnetmask(addr
.s_addr
);
1210 setvar(&ep
, prefix
, "subnet_mask", inet_ntoa(net
));
1212 snprintf(cidr
, sizeof(cidr
), "%d", inet_ntocidr(net
));
1213 setvar(&ep
, prefix
, "subnet_cidr", cidr
);
1214 if (get_option_addr(ifp
->ctx
, &brd
,
1215 dhcp
, DHO_BROADCAST
) == -1) {
1216 brd
.s_addr
= addr
.s_addr
| ~net
.s_addr
;
1217 setvar(&ep
, prefix
, "broadcast_address",
1220 addr
.s_addr
= dhcp
->yiaddr
& net
.s_addr
;
1221 setvar(&ep
, prefix
, "network_number", inet_ntoa(addr
));
1224 if (*dhcp
->bootfile
&& !(overl
& 1))
1225 setvar(&ep
, prefix
, "filename", (const char *)dhcp
->bootfile
);
1226 if (*dhcp
->servername
&& !(overl
& 2))
1227 setvar(&ep
, prefix
, "server_name",
1228 (const char *)dhcp
->servername
);
1230 /* Zero our indexes */
1232 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
1233 i
< ifp
->ctx
->dhcp_opts_len
;
1235 dhcp_zero_index(opt
);
1236 for (i
= 0, opt
= ifp
->options
->dhcp_override
;
1237 i
< ifp
->options
->dhcp_override_len
;
1239 dhcp_zero_index(opt
);
1240 for (i
= 0, opt
= ifp
->ctx
->vivso
;
1241 i
< ifp
->ctx
->vivso_len
;
1243 dhcp_zero_index(opt
);
1246 for (i
= 0, opt
= ifp
->ctx
->dhcp_opts
;
1247 i
< ifp
->ctx
->dhcp_opts_len
;
1250 if (has_option_mask(ifo
->nomask
, opt
->option
))
1252 if (dhcp_getoverride(ifo
, opt
->option
))
1254 if ((p
= get_option(ifp
->ctx
, dhcp
, opt
->option
, &pl
))) {
1255 ep
+= dhcp_envoption(ifp
->ctx
, ep
, prefix
, ifp
->name
,
1256 opt
, dhcp_getoption
, p
, pl
);
1257 if (opt
->option
== DHO_VIVSO
&&
1258 pl
> (int)sizeof(uint32_t))
1260 memcpy(&en
, p
, sizeof(en
));
1262 vo
= vivso_find(en
, ifp
);
1264 /* Skip over en + total size */
1265 p
+= sizeof(en
) + 1;
1266 pl
-= sizeof(en
) + 1;
1267 ep
+= dhcp_envoption(ifp
->ctx
,
1268 ep
, prefix
, ifp
->name
,
1269 vo
, dhcp_getoption
, p
, pl
);
1275 for (i
= 0, opt
= ifo
->dhcp_override
;
1276 i
< ifo
->dhcp_override_len
;
1279 if (has_option_mask(ifo
->nomask
, opt
->option
))
1281 if ((p
= get_option(ifp
->ctx
, dhcp
, opt
->option
, &pl
)))
1282 ep
+= dhcp_envoption(ifp
->ctx
, ep
, prefix
, ifp
->name
,
1283 opt
, dhcp_getoption
, p
, pl
);
1290 get_lease(struct dhcpcd_ctx
*ctx
,
1291 struct dhcp_lease
*lease
, const struct dhcp_message
*dhcp
)
1294 lease
->cookie
= dhcp
->cookie
;
1295 /* BOOTP does not set yiaddr for replies when ciaddr is set. */
1297 lease
->addr
.s_addr
= dhcp
->yiaddr
;
1299 lease
->addr
.s_addr
= dhcp
->ciaddr
;
1300 if (get_option_addr(ctx
, &lease
->net
, dhcp
, DHO_SUBNETMASK
) == -1)
1301 lease
->net
.s_addr
= ipv4_getnetmask(lease
->addr
.s_addr
);
1302 if (get_option_addr(ctx
, &lease
->brd
, dhcp
, DHO_BROADCAST
) == -1)
1303 lease
->brd
.s_addr
= lease
->addr
.s_addr
| ~lease
->net
.s_addr
;
1304 if (get_option_uint32(ctx
, &lease
->leasetime
, dhcp
, DHO_LEASETIME
) != 0)
1305 lease
->leasetime
= ~0U; /* Default to infinite lease */
1306 if (get_option_uint32(ctx
, &lease
->renewaltime
,
1307 dhcp
, DHO_RENEWALTIME
) != 0)
1308 lease
->renewaltime
= 0;
1309 if (get_option_uint32(ctx
, &lease
->rebindtime
,
1310 dhcp
, DHO_REBINDTIME
) != 0)
1311 lease
->rebindtime
= 0;
1312 if (get_option_addr(ctx
, &lease
->server
, dhcp
, DHO_SERVERID
) != 0)
1313 lease
->server
.s_addr
= INADDR_ANY
;
1317 get_dhcp_op(uint8_t type
)
1319 const struct dhcp_op
*d
;
1321 for (d
= dhcp_ops
; d
->name
; d
++)
1322 if (d
->value
== type
)
1328 dhcp_fallback(void *arg
)
1330 struct interface
*iface
;
1332 iface
= (struct interface
*)arg
;
1333 dhcpcd_selectprofile(iface
, iface
->options
->fallback
);
1334 dhcpcd_startinterface(iface
);
1338 dhcp_xid(const struct interface
*ifp
)
1342 if (ifp
->options
->options
& DHCPCD_XID_HWADDR
&&
1343 ifp
->hwlen
>= sizeof(xid
))
1344 /* The lower bits are probably more unique on the network */
1345 memcpy(&xid
, (ifp
->hwaddr
+ ifp
->hwlen
) - sizeof(xid
),
1354 dhcp_close(struct interface
*ifp
)
1356 struct dhcp_state
*state
= D_STATE(ifp
);
1361 if (state
->arp_fd
!= -1) {
1362 eloop_event_delete(ifp
->ctx
->eloop
, state
->arp_fd
);
1363 close(state
->arp_fd
);
1366 if (state
->raw_fd
!= -1) {
1367 eloop_event_delete(ifp
->ctx
->eloop
, state
->raw_fd
);
1368 close(state
->raw_fd
);
1371 if (state
->udp_fd
!= -1) {
1372 eloop_event_delete(ifp
->ctx
->eloop
, state
->udp_fd
);
1373 close(state
->udp_fd
);
1377 state
->interval
= 0;
1381 dhcp_openudp(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
)
1384 struct sockaddr_in sin
;
1386 struct dhcp_state
*state
;
1387 #ifdef SO_BINDTODEVICE
1393 if ((s
= socket(PF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
)) == -1)
1396 if ((s
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) == -1)
1398 if ((n
= fcntl(s
, F_GETFD
, 0)) == -1 ||
1399 fcntl(s
, F_SETFD
, n
| FD_CLOEXEC
) == -1)
1407 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1)
1409 #ifdef SO_BINDTODEVICE
1411 memset(&ifr
, 0, sizeof(ifr
));
1412 strlcpy(ifr
.ifr_name
, ifp
->name
, sizeof(ifr
.ifr_name
));
1413 /* We can only bind to the real device */
1414 p
= strchr(ifr
.ifr_name
, ':');
1417 if (setsockopt(s
, SOL_SOCKET
, SO_BINDTODEVICE
, &ifr
,
1422 memset(&sin
, 0, sizeof(sin
));
1423 sin
.sin_family
= AF_INET
;
1424 sin
.sin_port
= htons(DHCP_CLIENT_PORT
);
1426 state
= D_STATE(ifp
);
1427 sin
.sin_addr
.s_addr
= state
->addr
.s_addr
;
1429 state
= NULL
; /* appease gcc */
1430 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1)
1445 dhcp_sendpacket(const struct interface
*iface
, struct in_addr to
,
1446 const uint8_t *data
, size_t len
)
1448 struct sockaddr_in sin
;
1450 memset(&sin
, 0, sizeof(sin
));
1451 sin
.sin_family
= AF_INET
;
1452 sin
.sin_addr
.s_addr
= to
.s_addr
;
1453 sin
.sin_port
= htons(DHCP_SERVER_PORT
);
1454 return sendto(D_CSTATE(iface
)->udp_fd
, data
, len
, 0,
1455 (struct sockaddr
*)&sin
, sizeof(sin
));
1459 checksum(const void *data
, uint16_t len
)
1461 const uint8_t *addr
= data
;
1466 sum
+= addr
[0] * 256 + addr
[1];
1474 sum
= (sum
>> 16) + (sum
& 0xffff);
1482 static struct udp_dhcp_packet
*
1483 dhcp_makeudppacket(size_t *sz
, const uint8_t *data
, size_t length
,
1484 struct in_addr source
, struct in_addr dest
)
1486 struct udp_dhcp_packet
*udpp
;
1490 udpp
= calloc(1, sizeof(*udpp
));
1496 /* OK, this is important :)
1497 * We copy the data to our packet and then create a small part of the
1498 * ip structure and an invalid ip_len (basically udp length).
1499 * We then fill the udp structure and put the checksum
1500 * of the whole packet into the udp checksum.
1501 * Finally we complete the ip structure and ip checksum.
1502 * If we don't do the ordering like so then the udp checksum will be
1503 * broken, so find another way of doing it! */
1505 memcpy(&udpp
->dhcp
, data
, length
);
1507 ip
->ip_p
= IPPROTO_UDP
;
1508 ip
->ip_src
.s_addr
= source
.s_addr
;
1509 if (dest
.s_addr
== 0)
1510 ip
->ip_dst
.s_addr
= INADDR_BROADCAST
;
1512 ip
->ip_dst
.s_addr
= dest
.s_addr
;
1514 udp
->uh_sport
= htons(DHCP_CLIENT_PORT
);
1515 udp
->uh_dport
= htons(DHCP_SERVER_PORT
);
1516 udp
->uh_ulen
= htons(sizeof(*udp
) + length
);
1517 ip
->ip_len
= udp
->uh_ulen
;
1518 udp
->uh_sum
= checksum(udpp
, sizeof(*udpp
));
1520 ip
->ip_v
= IPVERSION
;
1521 ip
->ip_hl
= sizeof(*ip
) >> 2;
1522 ip
->ip_id
= (uint16_t)arc4random_uniform(UINT16_MAX
);
1523 ip
->ip_ttl
= IPDEFTTL
;
1524 ip
->ip_len
= htons(sizeof(*ip
) + sizeof(*udp
) + length
);
1525 ip
->ip_sum
= checksum(ip
, sizeof(*ip
));
1527 *sz
= sizeof(*ip
) + sizeof(*udp
) + length
;
1532 send_message(struct interface
*iface
, uint8_t type
,
1533 void (*callback
)(void *))
1535 struct dhcp_state
*state
= D_STATE(iface
);
1536 struct if_options
*ifo
= iface
->options
;
1537 struct dhcp_message
*dhcp
;
1538 struct udp_dhcp_packet
*udp
;
1541 struct in_addr from
, to
;
1546 syslog(LOG_DEBUG
, "%s: sending %s with xid 0x%x",
1547 iface
->name
, get_dhcp_op(type
), state
->xid
);
1549 if (state
->interval
== 0)
1550 state
->interval
= 4;
1552 state
->interval
*= 2;
1553 if (state
->interval
> 64)
1554 state
->interval
= 64;
1556 tv
.tv_sec
= state
->interval
+ DHCP_RAND_MIN
;
1557 tv
.tv_usec
= (suseconds_t
)arc4random_uniform(
1558 (DHCP_RAND_MAX
- DHCP_RAND_MIN
) * 1000000);
1561 "%s: sending %s (xid 0x%x), next in %0.1f seconds",
1562 iface
->name
, get_dhcp_op(type
), state
->xid
,
1563 timeval_to_double(&tv
));
1566 /* Ensure sockets are open. */
1567 if (dhcp_open(iface
) == -1) {
1568 if (!(iface
->ctx
->options
& DHCPCD_TEST
))
1569 dhcp_drop(iface
, "FAIL");
1573 /* If we couldn't open a UDP port for our IP address
1574 * then we cannot renew.
1575 * This could happen if our IP was pulled out from underneath us.
1576 * Also, we should not unicast from a BOOTP lease. */
1577 if (state
->udp_fd
== -1 ||
1578 (!(ifo
->options
& DHCPCD_INFORM
) &&
1579 is_bootp(iface
, state
->new)))
1581 a
= state
->addr
.s_addr
;
1582 state
->addr
.s_addr
= 0;
1584 r
= make_message(&dhcp
, iface
, type
);
1589 state
->addr
.s_addr
= a
;
1590 from
.s_addr
= dhcp
->ciaddr
;
1592 to
.s_addr
= state
->lease
.server
.s_addr
;
1595 if (to
.s_addr
&& to
.s_addr
!= INADDR_BROADCAST
) {
1596 r
= dhcp_sendpacket(iface
, to
, (uint8_t *)dhcp
, len
);
1598 syslog(LOG_ERR
, "%s: dhcp_sendpacket: %m", iface
->name
);
1605 udp
= dhcp_makeudppacket(&ulen
, (uint8_t *)dhcp
, len
, from
, to
);
1607 syslog(LOG_ERR
, "dhcp_makeudppacket: %m");
1609 r
= if_sendrawpacket(iface
, ETHERTYPE_IP
,
1610 (uint8_t *)udp
, ulen
);
1613 /* If we failed to send a raw packet this normally means
1614 * we don't have the ability to work beneath the IP layer
1615 * for this interface.
1616 * As such we remove it from consideration without actually
1617 * stopping the interface. */
1619 syslog(LOG_ERR
, "%s: if_sendrawpacket: %m",
1627 if (!(iface
->ctx
->options
& DHCPCD_TEST
))
1628 dhcp_drop(iface
, "FAIL");
1630 eloop_timeout_delete(iface
->ctx
->eloop
,
1639 /* Even if we fail to send a packet we should continue as we are
1640 * as our failure timeouts will change out codepath when needed. */
1642 eloop_timeout_add_tv(iface
->ctx
->eloop
, &tv
, callback
, iface
);
1646 send_inform(void *arg
)
1649 send_message((struct interface
*)arg
, DHCP_INFORM
, send_inform
);
1653 send_discover(void *arg
)
1656 send_message((struct interface
*)arg
, DHCP_DISCOVER
, send_discover
);
1660 send_request(void *arg
)
1663 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_request
);
1667 send_renew(void *arg
)
1670 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_renew
);
1674 send_rebind(void *arg
)
1677 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_rebind
);
1681 dhcp_discover(void *arg
)
1683 struct interface
*ifp
= arg
;
1684 struct dhcp_state
*state
= D_STATE(ifp
);
1685 struct if_options
*ifo
= ifp
->options
;
1686 time_t timeout
= ifo
->timeout
;
1688 /* If we're rebooting and we're not daemonised then we need
1689 * to shorten the normal timeout to ensure we try correctly
1690 * for a fallback or IPv4LL address. */
1691 if (state
->state
== DHS_REBOOT
&&
1692 !(ifp
->ctx
->options
& DHCPCD_DAEMONISED
))
1694 if (ifo
->reboot
>= timeout
)
1697 timeout
-= ifo
->reboot
;
1700 state
->state
= DHS_DISCOVER
;
1701 state
->xid
= dhcp_xid(ifp
);
1702 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
1704 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1705 timeout
, dhcp_fallback
, ifp
);
1706 else if (ifo
->options
& DHCPCD_IPV4LL
&&
1707 !IN_LINKLOCAL(htonl(state
->addr
.s_addr
)))
1709 if (IN_LINKLOCAL(htonl(state
->fail
.s_addr
)))
1710 timeout
= RATE_LIMIT_INTERVAL
;
1711 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1712 timeout
, ipv4ll_start
, ifp
);
1714 if (ifo
->options
& DHCPCD_REQUEST
)
1715 syslog(LOG_INFO
, "%s: soliciting a DHCP lease (requesting %s)",
1716 ifp
->name
, inet_ntoa(ifo
->req_addr
));
1718 syslog(LOG_INFO
, "%s: soliciting a DHCP lease", ifp
->name
);
1723 dhcp_request(void *arg
)
1725 struct interface
*ifp
= arg
;
1726 struct dhcp_state
*state
= D_STATE(ifp
);
1728 state
->state
= DHS_REQUEST
;
1733 dhcp_expire(void *arg
)
1735 struct interface
*ifp
= arg
;
1736 struct dhcp_state
*state
= D_STATE(ifp
);
1738 syslog(LOG_ERR
, "%s: DHCP lease expired", ifp
->name
);
1739 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
1740 dhcp_drop(ifp
, "EXPIRE");
1741 unlink(state
->leasefile
);
1743 state
->interval
= 0;
1748 dhcp_decline(struct interface
*ifp
)
1751 send_message(ifp
, DHCP_DECLINE
, NULL
);
1755 dhcp_renew(void *arg
)
1757 struct interface
*ifp
= arg
;
1758 struct dhcp_state
*state
= D_STATE(ifp
);
1759 struct dhcp_lease
*lease
= &state
->lease
;
1761 syslog(LOG_DEBUG
, "%s: renewing lease of %s",
1762 ifp
->name
, inet_ntoa(lease
->addr
));
1763 syslog(LOG_DEBUG
, "%s: rebind in %"PRIu32
" seconds,"
1764 " expire in %"PRIu32
" seconds",
1765 ifp
->name
, lease
->rebindtime
- lease
->renewaltime
,
1766 lease
->leasetime
- lease
->renewaltime
);
1767 state
->state
= DHS_RENEW
;
1768 state
->xid
= dhcp_xid(ifp
);
1773 dhcp_rebind(void *arg
)
1775 struct interface
*ifp
= arg
;
1776 struct dhcp_state
*state
= D_STATE(ifp
);
1777 struct dhcp_lease
*lease
= &state
->lease
;
1779 syslog(LOG_WARNING
, "%s: failed to renew DHCP, rebinding",
1781 syslog(LOG_DEBUG
, "%s: expire in %"PRIu32
" seconds",
1782 ifp
->name
, lease
->leasetime
- lease
->rebindtime
);
1783 state
->state
= DHS_REBIND
;
1784 eloop_timeout_delete(ifp
->ctx
->eloop
, send_renew
, ifp
);
1785 state
->lease
.server
.s_addr
= 0;
1786 ifp
->options
->options
&= ~ DHCPCD_CSR_WARNED
;
1791 dhcp_bind(void *arg
)
1793 struct interface
*iface
= arg
;
1794 struct dhcp_state
*state
= D_STATE(iface
);
1795 struct if_options
*ifo
= iface
->options
;
1796 struct dhcp_lease
*lease
= &state
->lease
;
1800 state
->reason
= NULL
;
1803 state
->old
= state
->new;
1804 state
->new = state
->offer
;
1805 state
->offer
= NULL
;
1806 get_lease(iface
->ctx
, lease
, state
->new);
1807 if (ifo
->options
& DHCPCD_STATIC
) {
1808 syslog(LOG_INFO
, "%s: using static address %s/%d",
1809 iface
->name
, inet_ntoa(lease
->addr
),
1810 inet_ntocidr(lease
->net
));
1811 lease
->leasetime
= ~0U;
1812 state
->reason
= "STATIC";
1813 } else if (state
->new->cookie
!= htonl(MAGIC_COOKIE
)) {
1814 syslog(LOG_INFO
, "%s: using IPv4LL address %s",
1815 iface
->name
, inet_ntoa(lease
->addr
));
1816 lease
->leasetime
= ~0U;
1817 state
->reason
= "IPV4LL";
1819 } else if (ifo
->options
& DHCPCD_INFORM
) {
1820 if (ifo
->req_addr
.s_addr
!= 0)
1821 lease
->addr
.s_addr
= ifo
->req_addr
.s_addr
;
1823 lease
->addr
.s_addr
= state
->addr
.s_addr
;
1824 syslog(LOG_INFO
, "%s: received approval for %s", iface
->name
,
1825 inet_ntoa(lease
->addr
));
1826 lease
->leasetime
= ~0U;
1827 state
->reason
= "INFORM";
1829 if (gettimeofday(&tv
, NULL
) == 0)
1830 lease
->leasedfrom
= tv
.tv_sec
;
1831 else if (lease
->frominfo
)
1832 state
->reason
= "TIMEOUT";
1833 if (lease
->leasetime
== ~0U) {
1834 lease
->renewaltime
=
1837 syslog(LOG_INFO
, "%s: leased %s for infinity",
1838 iface
->name
, inet_ntoa(lease
->addr
));
1840 if (lease
->leasetime
< DHCP_MIN_LEASE
) {
1842 "%s: minimum lease is %d seconds",
1843 iface
->name
, DHCP_MIN_LEASE
);
1844 lease
->leasetime
= DHCP_MIN_LEASE
;
1846 if (lease
->rebindtime
== 0)
1848 (uint32_t)(lease
->leasetime
* T2
);
1849 else if (lease
->rebindtime
>= lease
->leasetime
) {
1851 (uint32_t)(lease
->leasetime
* T2
);
1853 "%s: rebind time greater than lease "
1854 "time, forcing to %"PRIu32
" seconds",
1855 iface
->name
, lease
->rebindtime
);
1857 if (lease
->renewaltime
== 0)
1858 lease
->renewaltime
=
1859 (uint32_t)(lease
->leasetime
* T1
);
1860 else if (lease
->renewaltime
> lease
->rebindtime
) {
1861 lease
->renewaltime
=
1862 (uint32_t)(lease
->leasetime
* T1
);
1864 "%s: renewal time greater than rebind "
1865 "time, forcing to %"PRIu32
" seconds",
1866 iface
->name
, lease
->renewaltime
);
1868 syslog(lease
->addr
.s_addr
== state
->addr
.s_addr
?
1869 LOG_DEBUG
: LOG_INFO
,
1870 "%s: leased %s for %"PRIu32
" seconds", iface
->name
,
1871 inet_ntoa(lease
->addr
), lease
->leasetime
);
1874 if (iface
->ctx
->options
& DHCPCD_TEST
) {
1875 state
->reason
= "TEST";
1876 script_runreason(iface
, state
->reason
);
1877 eloop_exit(iface
->ctx
->eloop
, EXIT_SUCCESS
);
1880 if (state
->reason
== NULL
) {
1882 if (state
->old
->yiaddr
== state
->new->yiaddr
&&
1883 lease
->server
.s_addr
)
1884 state
->reason
= "RENEW";
1886 state
->reason
= "REBIND";
1887 } else if (state
->state
== DHS_REBOOT
)
1888 state
->reason
= "REBOOT";
1890 state
->reason
= "BOUND";
1892 if (lease
->leasetime
== ~0U)
1893 lease
->renewaltime
= lease
->rebindtime
= lease
->leasetime
;
1895 eloop_timeout_add_sec(iface
->ctx
->eloop
,
1896 (time_t)lease
->renewaltime
, dhcp_renew
, iface
);
1897 eloop_timeout_add_sec(iface
->ctx
->eloop
,
1898 (time_t)lease
->rebindtime
, dhcp_rebind
, iface
);
1899 eloop_timeout_add_sec(iface
->ctx
->eloop
,
1900 (time_t)lease
->leasetime
, dhcp_expire
, iface
);
1902 "%s: renew in %"PRIu32
" seconds, rebind in %"PRIu32
1904 iface
->name
, lease
->renewaltime
, lease
->rebindtime
);
1906 ipv4_applyaddr(iface
);
1907 if (dhcpcd_daemonise(iface
->ctx
) == 0) {
1910 state
->state
= DHS_BOUND
;
1911 if (ifo
->options
& DHCPCD_ARP
) {
1913 arp_announce(iface
);
1919 dhcp_timeout(void *arg
)
1921 struct interface
*ifp
= arg
;
1922 struct dhcp_state
*state
= D_STATE(ifp
);
1925 state
->interval
= 0;
1929 struct dhcp_message
*
1930 dhcp_message_new(const struct in_addr
*addr
, const struct in_addr
*mask
)
1932 struct dhcp_message
*dhcp
;
1935 dhcp
= calloc(1, sizeof(*dhcp
));
1938 dhcp
->yiaddr
= addr
->s_addr
;
1940 if (mask
&& mask
->s_addr
!= INADDR_ANY
) {
1941 *p
++ = DHO_SUBNETMASK
;
1942 *p
++ = sizeof(mask
->s_addr
);
1943 memcpy(p
, &mask
->s_addr
, sizeof(mask
->s_addr
));
1944 p
+= sizeof(mask
->s_addr
);
1951 dhcp_static(struct interface
*ifp
)
1953 struct if_options
*ifo
;
1954 struct dhcp_state
*state
;
1956 state
= D_STATE(ifp
);
1958 if (ifo
->req_addr
.s_addr
== INADDR_ANY
) {
1960 "%s: waiting for 3rd party to "
1961 "configure IP address",
1963 state
->reason
= "3RDPARTY";
1964 script_runreason(ifp
, state
->reason
);
1967 state
->offer
= dhcp_message_new(&ifo
->req_addr
, &ifo
->req_mask
);
1969 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
1975 dhcp_inform(struct interface
*ifp
)
1977 struct dhcp_state
*state
;
1978 struct if_options
*ifo
;
1979 struct ipv4_addr
*ap
;
1981 state
= D_STATE(ifp
);
1983 if (ifp
->ctx
->options
& DHCPCD_TEST
) {
1984 state
->addr
.s_addr
= ifo
->req_addr
.s_addr
;
1985 state
->net
.s_addr
= ifo
->req_mask
.s_addr
;
1987 if (ifo
->req_addr
.s_addr
== INADDR_ANY
) {
1988 state
= D_STATE(ifp
);
1989 ap
= ipv4_findaddr(ifp
, NULL
, NULL
);
1992 "%s: waiting for 3rd party to "
1993 "configure IP address",
1995 state
->reason
= "3RDPARTY";
1996 script_runreason(ifp
, state
->reason
);
2000 dhcp_message_new(&ap
->addr
, &ap
->net
);
2003 dhcp_message_new(&ifo
->req_addr
, &ifo
->req_mask
);
2005 ifo
->options
|= DHCPCD_STATIC
;
2007 ifo
->options
&= ~DHCPCD_STATIC
;
2011 state
->state
= DHS_INFORM
;
2012 state
->xid
= dhcp_xid(ifp
);
2017 dhcp_reboot_newopts(struct interface
*ifp
, unsigned long long oldopts
)
2019 struct if_options
*ifo
;
2020 struct dhcp_state
*state
= D_STATE(ifp
);
2025 if ((ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2026 state
->addr
.s_addr
!= ifo
->req_addr
.s_addr
) ||
2027 (oldopts
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
2028 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))))
2030 dhcp_drop(ifp
, "EXPIRE");
2035 dhcp_reboot(struct interface
*ifp
)
2037 struct if_options
*ifo
;
2038 struct dhcp_state
*state
= D_STATE(ifp
);
2043 state
->interval
= 0;
2045 if (ifo
->options
& DHCPCD_LINK
&& ifp
->carrier
== LINK_DOWN
) {
2046 syslog(LOG_INFO
, "%s: waiting for carrier", ifp
->name
);
2049 if (ifo
->options
& DHCPCD_STATIC
) {
2053 if (ifo
->reboot
== 0 || state
->offer
== NULL
) {
2057 if (ifo
->options
& DHCPCD_INFORM
) {
2058 syslog(LOG_INFO
, "%s: informing address of %s",
2059 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2060 } else if (state
->offer
->cookie
== 0) {
2061 if (ifo
->options
& DHCPCD_IPV4LL
) {
2068 syslog(LOG_INFO
, "%s: rebinding lease of %s",
2069 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2071 state
->state
= DHS_REBOOT
;
2072 state
->xid
= dhcp_xid(ifp
);
2073 state
->lease
.server
.s_addr
= 0;
2074 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2076 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2077 ifo
->reboot
, dhcp_fallback
, ifp
);
2078 else if (ifo
->options
& DHCPCD_LASTLEASE
&& state
->lease
.frominfo
)
2079 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2080 ifo
->reboot
, dhcp_timeout
, ifp
);
2081 else if (!(ifo
->options
& DHCPCD_INFORM
&&
2082 ifp
->ctx
->options
& (DHCPCD_MASTER
| DHCPCD_DAEMONISED
)))
2083 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
2084 ifo
->reboot
, dhcp_expire
, ifp
);
2085 /* Don't bother ARP checking as the server could NAK us first. */
2086 if (ifo
->options
& DHCPCD_INFORM
)
2089 /* Don't call dhcp_request as that would change the state */
2095 dhcp_drop(struct interface
*ifp
, const char *reason
)
2097 struct dhcp_state
*state
;
2102 state
= D_STATE(ifp
);
2105 dhcp_auth_reset(&state
->auth
);
2108 eloop_timeouts_delete(ifp
->ctx
->eloop
, ifp
, dhcp_expire
, NULL
);
2109 if (ifp
->options
->options
& DHCPCD_RELEASE
) {
2110 unlink(state
->leasefile
);
2111 if (ifp
->carrier
!= LINK_DOWN
&&
2112 state
->new != NULL
&&
2113 state
->new->cookie
== htonl(MAGIC_COOKIE
))
2115 syslog(LOG_INFO
, "%s: releasing lease of %s",
2116 ifp
->name
, inet_ntoa(state
->lease
.addr
));
2117 state
->xid
= dhcp_xid(ifp
);
2118 send_message(ifp
, DHCP_RELEASE
, NULL
);
2120 /* Give the packet a chance to go */
2121 ts
.tv_sec
= RELEASE_DELAY_S
;
2122 ts
.tv_nsec
= RELEASE_DELAY_NS
;
2123 nanosleep(&ts
, NULL
);
2128 state
->old
= state
->new;
2130 state
->reason
= reason
;
2131 ipv4_applyaddr(ifp
);
2134 state
->lease
.addr
.s_addr
= 0;
2135 ifp
->options
->options
&= ~ DHCPCD_CSR_WARNED
;
2139 log_dhcp1(int lvl
, const char *msg
,
2140 const struct interface
*iface
, const struct dhcp_message
*dhcp
,
2141 const struct in_addr
*from
, int ad
)
2145 struct in_addr addr
;
2148 if (strcmp(msg
, "NAK:") == 0)
2149 a
= get_option_string(iface
->ctx
, dhcp
, DHO_MESSAGE
);
2150 else if (ad
&& dhcp
->yiaddr
!= 0) {
2151 addr
.s_addr
= dhcp
->yiaddr
;
2152 a
= strdup(inet_ntoa(addr
));
2154 syslog(LOG_ERR
, "%s: %m", __func__
);
2161 r
= get_option_addr(iface
->ctx
, &addr
, dhcp
, DHO_SERVERID
);
2162 if (dhcp
->servername
[0] && r
== 0) {
2164 syslog(lvl
, "%s: %s %s %s `%s'", iface
->name
, msg
,
2165 tfrom
, inet_ntoa(addr
), dhcp
->servername
);
2167 syslog(lvl
, "%s: %s %s %s %s `%s'", iface
->name
, msg
, a
,
2168 tfrom
, inet_ntoa(addr
), dhcp
->servername
);
2175 syslog(lvl
, "%s: %s %s %s",
2176 iface
->name
, msg
, tfrom
, inet_ntoa(addr
));
2178 syslog(lvl
, "%s: %s %s %s %s",
2179 iface
->name
, msg
, a
, tfrom
, inet_ntoa(addr
));
2185 log_dhcp(int lvl
, const char *msg
,
2186 const struct interface
*iface
, const struct dhcp_message
*dhcp
,
2187 const struct in_addr
*from
)
2190 log_dhcp1(lvl
, msg
, iface
, dhcp
, from
, 1);
2194 blacklisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2198 for (i
= 0; i
< ifo
->blacklist_len
; i
+= 2)
2199 if (ifo
->blacklist
[i
] == (addr
& ifo
->blacklist
[i
+ 1]))
2205 whitelisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
2209 if (ifo
->whitelist_len
== 0)
2211 for (i
= 0; i
< ifo
->whitelist_len
; i
+= 2)
2212 if (ifo
->whitelist
[i
] == (addr
& ifo
->whitelist
[i
+ 1]))
2218 dhcp_handledhcp(struct interface
*iface
, struct dhcp_message
**dhcpp
,
2219 const struct in_addr
*from
)
2221 struct dhcp_state
*state
= D_STATE(iface
);
2222 struct if_options
*ifo
= iface
->options
;
2223 struct dhcp_message
*dhcp
= *dhcpp
;
2224 struct dhcp_lease
*lease
= &state
->lease
;
2226 const uint8_t *auth
;
2227 struct in_addr addr
;
2232 /* We may have found a BOOTP server */
2233 if (get_option_uint8(iface
->ctx
, &type
, dhcp
, DHO_MESSAGETYPE
) == -1)
2236 /* Authenticate the message */
2237 auth
= get_option(iface
->ctx
, dhcp
, DHO_AUTHENTICATION
, &auth_len
);
2239 if (dhcp_auth_validate(&state
->auth
, &ifo
->auth
,
2240 (uint8_t *)*dhcpp
, sizeof(**dhcpp
), 4, type
,
2241 auth
, auth_len
) == NULL
)
2243 syslog(LOG_DEBUG
, "%s: dhcp_auth_validate: %m",
2245 log_dhcp1(LOG_ERR
, "authentication failed",
2246 iface
, dhcp
, from
, 0);
2249 if (state
->auth
.token
)
2250 syslog(LOG_DEBUG
, "%s: validated using 0x%08" PRIu32
,
2251 iface
->name
, state
->auth
.token
->secretid
);
2253 syslog(LOG_DEBUG
, "%s: accepted reconfigure key",
2255 } else if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
) {
2256 log_dhcp1(LOG_ERR
, "no authentication", iface
, dhcp
, from
, 0);
2258 } else if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
)
2259 log_dhcp1(LOG_WARNING
, "no authentication",
2260 iface
, dhcp
, from
, 0);
2263 if (type
== DHCP_FORCERENEW
) {
2264 if (from
->s_addr
== INADDR_ANY
||
2265 from
->s_addr
== INADDR_BROADCAST
)
2267 log_dhcp(LOG_ERR
, "discarding Force Renew",
2272 log_dhcp(LOG_ERR
, "unauthenticated Force Renew",
2276 if (state
->state
!= DHS_BOUND
&& state
->state
!= DHS_INFORM
) {
2277 log_dhcp(LOG_DEBUG
, "not bound, ignoring Force Renew",
2281 log_dhcp(LOG_ERR
, "Force Renew from", iface
, dhcp
, from
);
2282 /* The rebind and expire timings are still the same, we just
2283 * enter the renew state early */
2284 if (state
->state
== DHS_BOUND
) {
2285 eloop_timeout_delete(iface
->ctx
->eloop
,
2289 eloop_timeout_delete(iface
->ctx
->eloop
,
2290 send_inform
, iface
);
2296 if (state
->state
== DHS_BOUND
) {
2297 /* Before we supported FORCERENEW we closed off the raw
2298 * port so we effectively ignored all messages.
2299 * As such we'll not log by default here. */
2300 //log_dhcp(LOG_DEBUG, "bound, ignoring", iface, dhcp, from);
2304 /* Ensure it's the right transaction */
2305 if (state
->xid
!= ntohl(dhcp
->xid
)) {
2307 "%s: wrong xid 0x%x (expecting 0x%x) from %s",
2308 iface
->name
, ntohl(dhcp
->xid
), state
->xid
,
2312 /* reset the message counter */
2313 state
->interval
= 0;
2315 if (type
== DHCP_NAK
) {
2316 /* For NAK, only check if we require the ServerID */
2317 if (has_option_mask(ifo
->requiremask
, DHO_SERVERID
) &&
2318 get_option_addr(iface
->ctx
, &addr
, dhcp
, DHO_SERVERID
) == -1)
2320 log_dhcp(LOG_WARNING
, "reject NAK", iface
, dhcp
, from
);
2324 /* We should restart on a NAK */
2325 log_dhcp(LOG_WARNING
, "NAK:", iface
, dhcp
, from
);
2326 if ((msg
= get_option_string(iface
->ctx
, dhcp
, DHO_MESSAGE
))) {
2327 syslog(LOG_WARNING
, "%s: message: %s",
2331 if (!(iface
->ctx
->options
& DHCPCD_TEST
)) {
2332 dhcp_drop(iface
, "NAK");
2333 unlink(state
->leasefile
);
2336 /* If we constantly get NAKS then we should slowly back off */
2337 eloop_timeout_add_sec(iface
->ctx
->eloop
,
2338 state
->nakoff
, dhcp_discover
, iface
);
2339 if (state
->nakoff
== 0)
2343 if (state
->nakoff
> NAKOFF_MAX
)
2344 state
->nakoff
= NAKOFF_MAX
;
2349 /* DHCP Auto-Configure, RFC 2563 */
2350 if (type
== DHCP_OFFER
&& dhcp
->yiaddr
== 0) {
2351 log_dhcp(LOG_WARNING
, "no address given", iface
, dhcp
, from
);
2352 if ((msg
= get_option_string(iface
->ctx
, dhcp
, DHO_MESSAGE
))) {
2353 syslog(LOG_WARNING
, "%s: message: %s",
2357 if (state
->state
== DHS_DISCOVER
&&
2358 get_option_uint8(iface
->ctx
, &tmp
, dhcp
,
2359 DHO_AUTOCONFIGURE
) == 0)
2363 log_dhcp(LOG_WARNING
, "IPv4LL disabled from",
2366 eloop_timeout_delete(iface
->ctx
->eloop
,
2368 eloop_timeout_add_sec(iface
->ctx
->eloop
,
2369 DHCP_MAX
, dhcp_discover
,
2373 log_dhcp(LOG_WARNING
, "IPv4LL enabled from",
2375 eloop_timeout_delete(iface
->ctx
->eloop
,
2377 if (IN_LINKLOCAL(htonl(state
->addr
.s_addr
)))
2378 eloop_timeout_add_sec(iface
->ctx
->eloop
,
2379 DHCP_MAX
, dhcp_discover
, iface
);
2381 ipv4ll_start(iface
);
2385 "%s: unknown auto configuration option %d",
2393 /* Ensure that all required options are present */
2394 for (i
= 1; i
< 255; i
++) {
2395 if (has_option_mask(ifo
->requiremask
, i
) &&
2396 get_option_uint8(iface
->ctx
, &tmp
, dhcp
, (uint8_t)i
) != 0)
2398 /* If we are bootp, then ignore the need for serverid.
2399 * To ignore bootp, require dhcp_message_type. */
2400 if (type
== 0 && i
== DHO_SERVERID
)
2402 log_dhcp(LOG_WARNING
, "reject DHCP", iface
, dhcp
, from
);
2407 /* Ensure that the address offered is valid */
2408 if ((type
== 0 || type
== DHCP_OFFER
|| type
== DHCP_ACK
) &&
2409 (dhcp
->ciaddr
== INADDR_ANY
|| dhcp
->ciaddr
== INADDR_BROADCAST
) &&
2410 (dhcp
->yiaddr
== INADDR_ANY
|| dhcp
->yiaddr
== INADDR_BROADCAST
))
2412 log_dhcp(LOG_WARNING
, "reject invalid address",
2417 if ((type
== 0 || type
== DHCP_OFFER
) &&
2418 state
->state
== DHS_DISCOVER
)
2420 lease
->frominfo
= 0;
2421 lease
->addr
.s_addr
= dhcp
->yiaddr
;
2422 lease
->cookie
= dhcp
->cookie
;
2424 get_option_addr(iface
->ctx
,
2425 &lease
->server
, dhcp
, DHO_SERVERID
) != 0)
2426 lease
->server
.s_addr
= INADDR_ANY
;
2427 log_dhcp(LOG_INFO
, "offered", iface
, dhcp
, from
);
2429 state
->offer
= dhcp
;
2431 if (iface
->ctx
->options
& DHCPCD_TEST
) {
2433 state
->old
= state
->new;
2434 state
->new = state
->offer
;
2435 state
->offer
= NULL
;
2436 state
->reason
= "TEST";
2437 script_runreason(iface
, state
->reason
);
2438 eloop_exit(iface
->ctx
->eloop
, EXIT_SUCCESS
);
2441 eloop_timeout_delete(iface
->ctx
->eloop
, send_discover
, iface
);
2442 /* We don't request BOOTP addresses */
2444 /* We used to ARP check here, but that seems to be in
2445 * violation of RFC2131 where it only describes
2446 * DECLINE after REQUEST.
2447 * It also seems that some MS DHCP servers actually
2448 * ignore DECLINE if no REQUEST, ie we decline a
2450 dhcp_request(iface
);
2456 if (type
== DHCP_OFFER
) {
2457 log_dhcp(LOG_WARNING
, "ignoring offer of",
2462 /* We should only be dealing with acks */
2463 if (type
!= DHCP_ACK
) {
2464 log_dhcp(LOG_ERR
, "not ACK or OFFER",
2469 if (!(ifo
->options
& DHCPCD_INFORM
))
2470 log_dhcp(LOG_DEBUG
, "acknowledged", iface
, dhcp
, from
);
2472 ifo
->options
&= ~DHCPCD_STATIC
;
2476 /* No NAK, so reset the backoff
2477 * We don't reset on an OFFER message because the server could
2478 * potentially NAK the REQUEST. */
2481 /* BOOTP could have already assigned this above, so check we still
2482 * have a pointer. */
2485 state
->offer
= dhcp
;
2489 lease
->frominfo
= 0;
2490 eloop_timeout_delete(iface
->ctx
->eloop
, NULL
, iface
);
2492 if (ifo
->options
& DHCPCD_ARP
&&
2493 state
->addr
.s_addr
!= state
->offer
->yiaddr
)
2495 /* If the interface already has the address configured
2496 * then we can't ARP for duplicate detection. */
2497 addr
.s_addr
= state
->offer
->yiaddr
;
2498 if (!ipv4_findaddr(iface
, &addr
, NULL
)) {
2501 state
->conflicts
= 0;
2511 get_udp_data(const uint8_t **data
, const uint8_t *udp
)
2513 struct udp_dhcp_packet p
;
2515 memcpy(&p
, udp
, sizeof(p
));
2516 *data
= udp
+ offsetof(struct udp_dhcp_packet
, dhcp
);
2517 return ntohs(p
.ip
.ip_len
) - sizeof(p
.ip
) - sizeof(p
.udp
);
2521 valid_udp_packet(const uint8_t *data
, size_t data_len
, struct in_addr
*from
,
2524 struct udp_dhcp_packet p
;
2525 uint16_t bytes
, udpsum
;
2527 if (data_len
< sizeof(p
.ip
)) {
2529 from
->s_addr
= INADDR_ANY
;
2533 memcpy(&p
, data
, MIN(data_len
, sizeof(p
)));
2535 from
->s_addr
= p
.ip
.ip_src
.s_addr
;
2536 if (data_len
> sizeof(p
)) {
2540 if (checksum(&p
.ip
, sizeof(p
.ip
)) != 0) {
2545 bytes
= ntohs(p
.ip
.ip_len
);
2546 if (data_len
< bytes
) {
2551 if (noudpcsum
== 0) {
2552 udpsum
= p
.udp
.uh_sum
;
2557 p
.ip
.ip_len
= p
.udp
.uh_ulen
;
2562 if (udpsum
&& checksum(&p
, bytes
) != udpsum
) {
2572 dhcp_handlepacket(void *arg
)
2574 struct interface
*ifp
= arg
;
2575 struct dhcp_message
*dhcp
= NULL
;
2578 struct in_addr from
;
2580 const struct dhcp_state
*state
= D_CSTATE(ifp
);
2582 /* Need this API due to BPF */
2584 while (!(flags
& RAW_EOF
)) {
2585 bytes
= (size_t)if_readrawpacket(ifp
, ETHERTYPE_IP
,
2586 ifp
->ctx
->packet
, udp_dhcp_len
, &flags
);
2587 if (bytes
== 0 || (ssize_t
)bytes
== -1) {
2588 syslog(LOG_ERR
, "%s: dhcp if_readrawpacket: %m",
2593 if (valid_udp_packet(ifp
->ctx
->packet
, bytes
,
2594 &from
, flags
& RAW_PARTIALCSUM
) == -1)
2596 syslog(LOG_ERR
, "%s: invalid UDP packet from %s",
2597 ifp
->name
, inet_ntoa(from
));
2600 i
= whitelisted_ip(ifp
->options
, from
.s_addr
);
2603 "%s: non whitelisted DHCP packet from %s",
2604 ifp
->name
, inet_ntoa(from
));
2606 } else if (i
!= 1 &&
2607 blacklisted_ip(ifp
->options
, from
.s_addr
) == 1)
2610 "%s: blacklisted DHCP packet from %s",
2611 ifp
->name
, inet_ntoa(from
));
2614 if (ifp
->flags
& IFF_POINTOPOINT
&&
2615 state
->dst
.s_addr
!= from
.s_addr
)
2618 "%s: server %s is not destination",
2619 ifp
->name
, inet_ntoa(from
));
2621 bytes
= get_udp_data(&pp
, ifp
->ctx
->packet
);
2622 if (bytes
> sizeof(*dhcp
)) {
2624 "%s: packet greater than DHCP size from %s",
2625 ifp
->name
, inet_ntoa(from
));
2629 dhcp
= calloc(1, sizeof(*dhcp
));
2631 syslog(LOG_ERR
, "%s: calloc: %m", __func__
);
2635 memcpy(dhcp
, pp
, bytes
);
2636 if (dhcp
->cookie
!= htonl(MAGIC_COOKIE
)) {
2637 syslog(LOG_DEBUG
, "%s: bogus cookie from %s",
2638 ifp
->name
, inet_ntoa(from
));
2641 /* Ensure packet is for us */
2642 if (ifp
->hwlen
<= sizeof(dhcp
->chaddr
) &&
2643 memcmp(dhcp
->chaddr
, ifp
->hwaddr
, ifp
->hwlen
))
2645 char buf
[sizeof(dhcp
->chaddr
) * 3];
2647 syslog(LOG_DEBUG
, "%s: xid 0x%x is not for hwaddr %s",
2648 ifp
->name
, ntohl(dhcp
->xid
),
2649 hwaddr_ntoa(dhcp
->chaddr
, sizeof(dhcp
->chaddr
),
2653 dhcp_handledhcp(ifp
, &dhcp
, &from
);
2654 if (state
->raw_fd
== -1)
2661 dhcp_handleudp1(struct dhcpcd_ctx
*ctx
, int *fd
, const char *ifname
)
2663 uint8_t buffer
[sizeof(struct dhcp_message
)];
2665 /* Just read what's in the UDP fd and discard it as we always read
2666 * from the raw fd */
2667 if (read(*fd
, buffer
, sizeof(buffer
)) == -1) {
2668 syslog(LOG_ERR
, "%s: %s: %m", ifname
, __func__
);
2669 eloop_event_delete(ctx
->eloop
, *fd
);
2676 dhcp_handleudp(void *arg
)
2678 struct dhcpcd_ctx
*ctx
;
2681 dhcp_handleudp1(arg
, &ctx
->udp_fd
, NULL
);
2685 dhcp_handleifudp(void *arg
)
2687 const struct interface
*ifp
;
2688 struct dhcp_state
*state
;
2691 state
= D_STATE(ifp
);
2692 dhcp_handleudp1(ifp
->ctx
, &state
->udp_fd
, ifp
->name
);
2696 dhcp_open(struct interface
*ifp
)
2698 struct dhcp_state
*state
;
2700 if (ifp
->ctx
->packet
== NULL
) {
2701 ifp
->ctx
->packet
= malloc(udp_dhcp_len
);
2702 if (ifp
->ctx
->packet
== NULL
) {
2703 syslog(LOG_ERR
, "%s: %m", __func__
);
2708 state
= D_STATE(ifp
);
2709 if (state
->raw_fd
== -1) {
2710 state
->raw_fd
= if_openrawsocket(ifp
, ETHERTYPE_IP
);
2711 if (state
->raw_fd
== -1) {
2712 syslog(LOG_ERR
, "%s: %s: %m", __func__
, ifp
->name
);
2715 eloop_event_add(ifp
->ctx
->eloop
,
2716 state
->raw_fd
, dhcp_handlepacket
, ifp
);
2718 if (state
->udp_fd
== -1 &&
2719 state
->addr
.s_addr
!= 0 &&
2720 state
->new != NULL
&&
2721 (state
->new->cookie
== htonl(MAGIC_COOKIE
) ||
2722 ifp
->options
->options
& DHCPCD_INFORM
))
2724 if (dhcp_openudp(ifp
->ctx
, ifp
) == -1 && errno
!= EADDRINUSE
) {
2725 syslog(LOG_ERR
, "%s: dhcp_openudp: %m", ifp
->name
);
2728 if (state
->udp_fd
!= -1)
2729 eloop_event_add(ifp
->ctx
->eloop
,
2730 state
->udp_fd
, dhcp_handleifudp
, ifp
);
2736 dhcp_dump(struct interface
*ifp
)
2738 struct dhcp_state
*state
;
2740 ifp
->if_data
[IF_DATA_DHCP
] = state
= calloc(1, sizeof(*state
));
2743 snprintf(state
->leasefile
, sizeof(state
->leasefile
),
2744 LEASEFILE
, ifp
->name
);
2745 state
->new = read_lease(ifp
);
2746 if (state
->new == NULL
&& errno
== ENOENT
) {
2747 strlcpy(state
->leasefile
, ifp
->name
, sizeof(state
->leasefile
));
2748 state
->new = read_lease(ifp
);
2750 if (state
->new == NULL
) {
2751 if (errno
== ENOENT
)
2752 syslog(LOG_ERR
, "%s: no lease to dump", ifp
->name
);
2755 state
->reason
= "DUMP";
2756 return script_runreason(ifp
, state
->reason
);
2759 syslog(LOG_ERR
, "%s: %m", __func__
);
2764 dhcp_free(struct interface
*ifp
)
2766 struct dhcp_state
*state
= D_STATE(ifp
);
2767 struct dhcpcd_ctx
*ctx
;
2773 free(state
->buffer
);
2774 free(state
->clientid
);
2776 ifp
->if_data
[IF_DATA_DHCP
] = NULL
;
2780 /* If we don't have any more DHCP enabled interfaces,
2781 * close the global socket and release resources */
2783 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
2789 if (ctx
->udp_fd
!= -1) {
2790 eloop_event_delete(ctx
->eloop
, ctx
->udp_fd
);
2796 free(ctx
->opt_buffer
);
2798 ctx
->opt_buffer
= NULL
;
2803 dhcp_init(struct interface
*ifp
)
2805 struct dhcp_state
*state
;
2806 const struct if_options
*ifo
;
2808 char buf
[(sizeof(ifo
->clientid
) - 1) * 3];
2810 state
= D_STATE(ifp
);
2811 if (state
== NULL
) {
2812 ifp
->if_data
[IF_DATA_DHCP
] = calloc(1, sizeof(*state
));
2813 state
= D_STATE(ifp
);
2816 /* 0 is a valid fd, so init to -1 */
2817 state
->raw_fd
= state
->udp_fd
= state
->arp_fd
= -1;
2820 state
->state
= DHS_INIT
;
2821 state
->reason
= "PREINIT";
2823 snprintf(state
->leasefile
, sizeof(state
->leasefile
),
2824 LEASEFILE
, ifp
->name
);
2827 /* We need to drop the leasefile so that dhcp_start
2828 * doesn't load it. */
2829 if (ifo
->options
& DHCPCD_REQUEST
)
2830 unlink(state
->leasefile
);
2832 free(state
->clientid
);
2833 state
->clientid
= NULL
;
2835 if (*ifo
->clientid
) {
2836 state
->clientid
= malloc((size_t)(ifo
->clientid
[0] + 1));
2837 if (state
->clientid
== NULL
)
2839 memcpy(state
->clientid
, ifo
->clientid
,
2840 (size_t)(ifo
->clientid
[0]) + 1);
2841 } else if (ifo
->options
& DHCPCD_CLIENTID
) {
2842 if (ifo
->options
& DHCPCD_DUID
) {
2843 state
->clientid
= malloc(ifp
->ctx
->duid_len
+ 6);
2844 if (state
->clientid
== NULL
)
2846 state
->clientid
[0] =(uint8_t)(ifp
->ctx
->duid_len
+ 5);
2847 state
->clientid
[1] = 255; /* RFC 4361 */
2848 memcpy(state
->clientid
+ 2, ifo
->iaid
, 4);
2849 memcpy(state
->clientid
+ 6, ifp
->ctx
->duid
,
2850 ifp
->ctx
->duid_len
);
2852 len
= (uint8_t)(ifp
->hwlen
+ 1);
2853 state
->clientid
= malloc((size_t)len
+ 1);
2854 if (state
->clientid
== NULL
)
2856 state
->clientid
[0] = len
;
2857 state
->clientid
[1] = (uint8_t)ifp
->family
;
2858 memcpy(state
->clientid
+ 2, ifp
->hwaddr
,
2863 if (ifo
->options
& DHCPCD_DUID
)
2864 /* Don't bother logging as DUID and IAID are reported
2865 * at device start. */
2868 if (ifo
->options
& DHCPCD_CLIENTID
)
2869 syslog(LOG_DEBUG
, "%s: using ClientID %s", ifp
->name
,
2870 hwaddr_ntoa(state
->clientid
+ 1, state
->clientid
[0],
2872 else if (ifp
->hwlen
)
2873 syslog(LOG_DEBUG
, "%s: using hwaddr %s", ifp
->name
,
2874 hwaddr_ntoa(ifp
->hwaddr
, ifp
->hwlen
, buf
, sizeof(buf
)));
2878 syslog(LOG_ERR
, "%s: Error making ClientID: %m", __func__
);
2883 dhcp_start1(void *arg
)
2885 struct interface
*ifp
= arg
;
2886 struct if_options
*ifo
= ifp
->options
;
2887 struct dhcp_state
*state
;
2893 if (!(ifo
->options
& DHCPCD_IPV4
))
2896 /* Listen on *.*.*.*:bootpc so that the kernel never sends an
2897 * ICMP port unreachable message back to the DHCP server */
2898 if (ifp
->ctx
->udp_fd
== -1 && dhcp_openudp(ifp
->ctx
, NULL
) != -1)
2899 eloop_event_add(ifp
->ctx
->eloop
,
2900 ifp
->ctx
->udp_fd
, dhcp_handleudp
, ifp
->ctx
);
2902 if (dhcp_init(ifp
) == -1) {
2903 syslog(LOG_ERR
, "%s: dhcp_init: %m", ifp
->name
);
2907 /* Close any pre-existing sockets as we're starting over */
2910 state
= D_STATE(ifp
);
2911 state
->start_uptime
= uptime();
2913 state
->offer
= NULL
;
2915 if (state
->arping_index
< ifo
->arping_len
) {
2920 if (ifo
->options
& DHCPCD_STATIC
) {
2925 if (ifo
->options
& DHCPCD_DHCP
&& dhcp_open(ifp
) == -1)
2928 if (ifo
->options
& DHCPCD_INFORM
) {
2932 if (ifp
->hwlen
== 0 && ifo
->clientid
[0] == '\0') {
2933 syslog(LOG_WARNING
, "%s: needs a clientid to configure",
2935 dhcp_drop(ifp
, "FAIL");
2936 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2939 /* We don't want to read the old lease if we NAK an old test */
2940 nolease
= state
->offer
&& ifp
->ctx
->options
& DHCPCD_TEST
;
2942 state
->offer
= read_lease(ifp
);
2944 get_lease(ifp
->ctx
, &state
->lease
, state
->offer
);
2945 state
->lease
.frominfo
= 1;
2946 if (state
->offer
->cookie
== 0) {
2947 if (state
->offer
->yiaddr
== state
->addr
.s_addr
) {
2949 state
->offer
= NULL
;
2951 } else if (state
->lease
.leasetime
!= ~0U &&
2952 stat(state
->leasefile
, &st
) == 0)
2954 /* Offset lease times and check expiry */
2955 gettimeofday(&now
, NULL
);
2956 if ((time_t)state
->lease
.leasetime
<
2957 now
.tv_sec
- st
.st_mtime
)
2960 "%s: discarding expired lease",
2963 state
->offer
= NULL
;
2964 state
->lease
.addr
.s_addr
= 0;
2966 l
= (uint32_t)(now
.tv_sec
- st
.st_mtime
);
2967 state
->lease
.leasetime
-= l
;
2968 state
->lease
.renewaltime
-= l
;
2969 state
->lease
.rebindtime
-= l
;
2974 if (!(ifo
->options
& DHCPCD_DHCP
)) {
2975 if (ifo
->options
& DHCPCD_IPV4LL
) {
2976 if (state
->offer
&& state
->offer
->cookie
!= 0) {
2978 state
->offer
= NULL
;
2985 if (state
->offer
== NULL
)
2987 else if (state
->offer
->cookie
== 0 && ifo
->options
& DHCPCD_IPV4LL
)
2994 dhcp_start(struct interface
*ifp
)
2998 if (!(ifp
->options
->options
& DHCPCD_IPV4
))
3001 /* No point in delaying a static configuration */
3002 if (ifp
->options
->options
& DHCPCD_STATIC
&&
3003 !(ifp
->options
->options
& DHCPCD_INFORM
))
3008 tv
.tv_sec
= DHCP_MIN_DELAY
;
3009 tv
.tv_usec
= (suseconds_t
)arc4random_uniform(
3010 (DHCP_MAX_DELAY
- DHCP_MIN_DELAY
) * 1000000);
3013 "%s: delaying DHCP for %0.1f seconds",
3014 ifp
->name
, timeval_to_double(&tv
));
3017 eloop_timeout_add_tv(ifp
->ctx
->eloop
, &tv
, dhcp_start1
, ifp
);
3021 dhcp_handleifa(int type
, struct interface
*ifp
,
3022 const struct in_addr
*addr
,
3023 const struct in_addr
*net
,
3024 const struct in_addr
*dst
)
3026 struct dhcp_state
*state
;
3027 struct if_options
*ifo
;
3030 state
= D_STATE(ifp
);
3034 if (type
== RTM_DELADDR
) {
3036 (state
->new->yiaddr
== addr
->s_addr
||
3037 (state
->new->yiaddr
== INADDR_ANY
&&
3038 state
->new->ciaddr
== addr
->s_addr
)))
3040 syslog(LOG_INFO
, "%s: removing IP address %s/%d",
3041 ifp
->name
, inet_ntoa(state
->lease
.addr
),
3042 inet_ntocidr(state
->lease
.net
));
3043 dhcp_drop(ifp
, "EXPIRE");
3048 if (type
!= RTM_NEWADDR
)
3052 if (ifo
->options
& DHCPCD_INFORM
) {
3053 if (state
->state
!= DHS_INFORM
)
3058 if (!(ifo
->options
& DHCPCD_STATIC
))
3060 if (ifo
->req_addr
.s_addr
!= INADDR_ANY
)
3064 state
->old
= state
->new;
3065 state
->new = dhcp_message_new(addr
, net
);
3066 if (state
->new == NULL
)
3068 state
->dst
.s_addr
= dst
? dst
->s_addr
: INADDR_ANY
;
3070 for (i
= 1; i
< 255; i
++)
3071 if (i
!= DHO_ROUTER
&& has_option_mask(ifo
->dstmask
,i
))
3072 dhcp_message_add_addr(state
->new, i
, *dst
);
3074 state
->reason
= "STATIC";
3075 ipv4_buildroutes(ifp
->ctx
);
3076 script_runreason(ifp
, state
->reason
);
3077 if (ifo
->options
& DHCPCD_INFORM
) {
3078 state
->state
= DHS_INFORM
;
3079 state
->xid
= dhcp_xid(ifp
);
3080 state
->lease
.server
.s_addr
= dst
? dst
->s_addr
: INADDR_ANY
;
3081 state
->addr
= *addr
;