2 __RCSID("$NetBSD: dhcp6.c,v 1.15 2015/08/21 10:39:00 roy Exp $");
5 * dhcpcd - DHCP client daemon
6 * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* TODO: We should decline dupliate addresses detected */
34 #include <sys/utsname.h>
36 #include <netinet/in.h>
56 #include "if-options.h"
61 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
64 /* DHCPCD Project has been assigned an IANA PEN of 40712 */
65 #define DHCPCD_IANA_PEN 40712
67 /* Unsure if I want this */
68 //#define VENDOR_SPLIT
70 /* Support older systems with different defines */
71 #if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO)
72 #define IPV6_RECVPKTINFO IPV6_PKTINFO
80 static const struct dhcp6_op dhcp6_ops
[] = {
81 { DHCP6_SOLICIT
, "SOLICIT6" },
82 { DHCP6_ADVERTISE
, "ADVERTISE6" },
83 { DHCP6_REQUEST
, "REQUEST6" },
84 { DHCP6_REPLY
, "REPLY6" },
85 { DHCP6_RENEW
, "RENEW6" },
86 { DHCP6_REBIND
, "REBIND6" },
87 { DHCP6_CONFIRM
, "CONFIRM6" },
88 { DHCP6_INFORMATION_REQ
, "INFORM6" },
89 { DHCP6_RELEASE
, "RELEASE6" },
90 { DHCP6_RECONFIGURE
, "RECONFIURE6" },
99 const struct dhcp_compat dhcp_compats
[] = {
100 { DHO_DNSSERVER
, D6_OPTION_DNS_SERVERS
},
101 { DHO_HOSTNAME
, D6_OPTION_FQDN
},
102 { DHO_DNSDOMAIN
, D6_OPTION_FQDN
},
103 { DHO_NISSERVER
, D6_OPTION_NIS_SERVERS
},
104 { DHO_NTPSERVER
, D6_OPTION_SNTP_SERVERS
},
105 { DHO_RAPIDCOMMIT
, D6_OPTION_RAPID_COMMIT
},
106 { DHO_FQDN
, D6_OPTION_FQDN
},
107 { DHO_VIVCO
, D6_OPTION_VENDOR_CLASS
},
108 { DHO_VIVSO
, D6_OPTION_VENDOR_OPTS
},
109 { DHO_DNSSEARCH
, D6_OPTION_DOMAIN_LIST
},
113 static const char * const dhcp6_statuses
[] = {
115 "Unspecified Failure",
116 "No Addresses Available",
122 struct dhcp6_ia_addr
{
123 struct in6_addr addr
;
128 struct dhcp6_pd_addr
{
132 struct in6_addr prefix
;
136 dhcp6_printoptions(const struct dhcpcd_ctx
*ctx
,
137 const struct dhcp_opt
*opts
, size_t opts_len
)
140 const struct dhcp_opt
*opt
, *opt2
;
143 for (i
= 0, opt
= ctx
->dhcp6_opts
;
144 i
< ctx
->dhcp6_opts_len
; i
++, opt
++)
146 for (j
= 0, opt2
= opts
; j
< opts_len
; j
++, opt2
++)
147 if (opt2
->option
== opt
->option
)
150 cols
= printf("%05d %s", opt
->option
, opt
->var
);
151 dhcp_print_option_encoding(opt
, cols
);
154 for (i
= 0, opt
= opts
; i
< opts_len
; i
++, opt
++) {
155 cols
= printf("%05d %s", opt
->option
, opt
->var
);
156 dhcp_print_option_encoding(opt
, cols
);
161 dhcp6_makevendor(struct dhcp6_option
*o
, const struct interface
*ifp
)
163 const struct if_options
*ifo
;
169 const struct vivco
*vivco
;
170 char vendor
[VENDORCLASSID_MAX_LEN
];
173 len
= sizeof(uint32_t); /* IANA PEN */
175 for (i
= 0, vivco
= ifo
->vivco
;
178 len
+= sizeof(uint16_t) + vivco
->len
;
179 vlen
= 0; /* silence bogus gcc warning */
181 vlen
= dhcp_vendor(vendor
, sizeof(vendor
));
185 len
+= sizeof(uint16_t) + (size_t)vlen
;
188 if (len
> UINT16_MAX
) {
189 logger(ifp
->ctx
, LOG_ERR
,
190 "%s: DHCPv6 Vendor Class too big", ifp
->name
);
195 o
->code
= htons(D6_OPTION_VENDOR_CLASS
);
196 o
->len
= htons((uint16_t)len
);
197 p
= D6_OPTION_DATA(o
);
198 u32
= htonl(ifo
->vivco_en
? ifo
->vivco_en
: DHCPCD_IANA_PEN
);
199 memcpy(p
, &u32
, sizeof(u32
));
202 for (i
= 0, vivco
= ifo
->vivco
;
206 u16
= htons((uint16_t)vivco
->len
);
207 memcpy(p
, &u16
, sizeof(u16
));
209 memcpy(p
, vivco
->data
, vivco
->len
);
213 u16
= htons((uint16_t)vlen
);
214 memcpy(p
, &u16
, sizeof(u16
));
216 memcpy(p
, vendor
, (size_t)vlen
);
223 static const struct dhcp6_option
*
224 dhcp6_findoption(uint16_t code
, const uint8_t *d
, size_t len
)
226 const struct dhcp6_option
*o
;
230 for (o
= (const struct dhcp6_option
*)d
;
232 o
= D6_CNEXT_OPTION(o
))
234 ol
= sizeof(*o
) + ntohs(o
->len
);
248 static const uint8_t *
249 dhcp6_getoption(struct dhcpcd_ctx
*ctx
,
250 size_t *os
, unsigned int *code
, size_t *len
,
251 const uint8_t *od
, size_t ol
, struct dhcp_opt
**oopt
)
253 const struct dhcp6_option
*o
;
255 struct dhcp_opt
*opt
;
263 o
= (const struct dhcp6_option
*)od
;
264 *len
= ntohs(o
->len
);
269 *code
= ntohs(o
->code
);
273 for (i
= 0, opt
= ctx
->dhcp6_opts
;
274 i
< ctx
->dhcp6_opts_len
; i
++, opt
++)
276 if (opt
->option
== *code
) {
283 return D6_COPTION_DATA(o
);
287 static const struct dhcp6_option
*
288 dhcp6_getmoption(uint16_t code
, const struct dhcp6_message
*m
, size_t len
)
291 if (len
< sizeof(*m
)) {
296 return dhcp6_findoption(code
,
297 (const uint8_t *)D6_CFIRST_OPTION(m
), len
);
301 dhcp6_updateelapsed(struct interface
*ifp
, struct dhcp6_message
*m
, size_t len
)
303 struct dhcp6_state
*state
;
304 const struct dhcp6_option
*co
;
305 struct dhcp6_option
*o
;
310 co
= dhcp6_getmoption(D6_OPTION_ELAPSED
, m
, len
);
315 state
= D6_STATE(ifp
);
316 clock_gettime(CLOCK_MONOTONIC
, &tv
);
317 if (state
->RTC
== 0) {
318 /* An RTC of zero means we're the first message
319 * out of the door, so the elapsed time is zero. */
323 timespecsub(&tv
, &state
->started
, &tv
);
324 /* Elapsed time is measured in centiseconds.
325 * We need to be sure it will not potentially overflow. */
326 if (tv
.tv_sec
>= (UINT16_MAX
/ CSEC_PER_SEC
) + 1)
329 hsec
= (tv
.tv_sec
* CSEC_PER_SEC
) +
330 (tv
.tv_nsec
/ NSEC_PER_CSEC
);
331 if (hsec
> UINT16_MAX
)
335 u16
= htons((uint16_t)hsec
);
336 memcpy(D6_OPTION_DATA(o
), &u16
, sizeof(u16
));
341 dhcp6_newxid(const struct interface
*ifp
, struct dhcp6_message
*m
)
345 if (ifp
->options
->options
& DHCPCD_XID_HWADDR
&&
346 ifp
->hwlen
>= sizeof(xid
))
347 /* The lower bits are probably more unique on the network */
348 memcpy(&xid
, (ifp
->hwaddr
+ ifp
->hwlen
) - sizeof(xid
),
353 m
->xid
[0] = (xid
>> 16) & 0xff;
354 m
->xid
[1] = (xid
>> 8) & 0xff;
355 m
->xid
[2] = xid
& 0xff;
358 static const struct if_sla
*
359 dhcp6_findselfsla(struct interface
*ifp
, const uint8_t *iaid
)
363 for (i
= 0; i
< ifp
->options
->ia_len
; i
++) {
365 memcmp(&ifp
->options
->ia
[i
].iaid
, iaid
,
366 sizeof(ifp
->options
->ia
[i
].iaid
)) == 0)
368 for (j
= 0; j
< ifp
->options
->ia
[i
].sla_len
; j
++) {
369 if (strcmp(ifp
->options
->ia
[i
].sla
[j
].ifname
,
371 return &ifp
->options
->ia
[i
].sla
[j
];
389 if ((n
& 0x0000FFFFU
) == 0) {
393 if ((n
& 0x000000FFU
) == 0) {
397 if ((n
& 0x0000000FU
) == 0) {
401 if ((n
& 0x00000003U
) == 0) {
405 if ((n
& 0x00000001U
) == 0)
413 dhcp6_delegateaddr(struct in6_addr
*addr
, struct interface
*ifp
,
414 const struct ipv6_addr
*prefix
, const struct if_sla
*sla
, struct if_ia
*ia
)
416 struct dhcp6_state
*state
;
418 char sabuf
[INET6_ADDRSTRLEN
];
421 state
= D6_STATE(ifp
);
423 ifp
->if_data
[IF_DATA_DHCP6
] = calloc(1, sizeof(*state
));
424 state
= D6_STATE(ifp
);
426 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
430 TAILQ_INIT(&state
->addrs
);
431 state
->state
= DH6S_DELEGATED
;
432 state
->reason
= "DELEGATED6";
435 if (sla
== NULL
|| sla
->sla_set
== 0) {
436 asla
.sla
= ifp
->index
;
439 } else if (sla
->prefix_len
== 0) {
442 asla
.prefix_len
= prefix
->prefix_len
;
447 if (sla
->prefix_len
== 0) {
451 if (ia
->sla_max
== 0) {
452 const struct interface
*ifi
;
455 TAILQ_FOREACH(ifi
, ifp
->ctx
->ifaces
, next
) {
456 if (ifi
!= ifp
&& ifi
->index
> sla_max
)
457 sla_max
= ifi
->index
;
460 sla_max
= ia
->sla_max
;
462 bits
= ffs32(sla_max
);
464 if (prefix
->prefix_len
+ bits
> UINT8_MAX
)
465 asla
.prefix_len
= UINT8_MAX
;
467 asla
.prefix_len
= (uint8_t)(prefix
->prefix_len
+ bits
);
469 /* Make a 64 prefix by default, as this maks SLAAC
470 * possible. Otherwise round up to the nearest octet. */
471 if (asla
.prefix_len
<= 64)
472 asla
.prefix_len
= 64;
474 asla
.prefix_len
= (uint8_t)ROUNDUP8(asla
.prefix_len
);
478 #define BIT(n) (1l << (n))
479 #define BIT_MASK(len) (BIT(len) - 1)
480 if (ia
->sla_max
== 0)
481 /* Work out the real sla_max from our bits used */
482 ia
->sla_max
= (uint32_t)BIT_MASK(asla
.prefix_len
-
486 if (ipv6_userprefix(&prefix
->prefix
, prefix
->prefix_len
,
487 sla
->sla
, addr
, sla
->prefix_len
) == -1)
489 sa
= inet_ntop(AF_INET6
, &prefix
->prefix
,
490 sabuf
, sizeof(sabuf
));
491 logger(ifp
->ctx
, LOG_ERR
,
492 "%s: invalid prefix %s/%d + %d/%d: %m",
493 ifp
->name
, sa
, prefix
->prefix_len
,
494 sla
->sla
, sla
->prefix_len
);
498 if (prefix
->prefix_exclude_len
&&
499 IN6_ARE_ADDR_EQUAL(addr
, &prefix
->prefix_exclude
))
501 sa
= inet_ntop(AF_INET6
, &prefix
->prefix_exclude
,
502 sabuf
, sizeof(sabuf
));
503 logger(ifp
->ctx
, LOG_ERR
,
504 "%s: cannot delegate excluded prefix %s/%d",
505 ifp
->name
, sa
, prefix
->prefix_exclude_len
);
509 return sla
->prefix_len
;
513 dhcp6_has_public_addr(const struct interface
*ifp
)
515 const struct dhcp6_state
*state
= D6_CSTATE(ifp
);
516 const struct ipv6_addr
*ia
;
520 TAILQ_FOREACH(ia
, &state
->addrs
, next
) {
521 if (ipv6_publicaddr(ia
))
528 dhcp6_makemessage(struct interface
*ifp
)
530 struct dhcp6_state
*state
;
531 struct dhcp6_message
*m
;
532 struct dhcp6_option
*o
, *so
, *eo
;
533 const struct dhcp6_option
*si
, *unicast
;
534 size_t l
, n
, len
, ml
;
536 uint16_t u16
, n_options
, auth_len
;
537 struct if_options
*ifo
;
538 const struct dhcp_opt
*opt
, *opt2
;
542 const struct ipv6_addr
*ap
;
543 char hbuf
[HOSTNAME_MAX_LEN
+ 1];
544 const char *hostname
;
546 struct dhcp6_ia_addr
*iap
;
547 struct dhcp6_pd_addr
*pdp
;
549 state
= D6_STATE(ifp
);
558 if (fqdn
== FQDN_DISABLE
&& ifo
->options
& DHCPCD_HOSTNAME
) {
559 /* We're sending the DHCPv4 hostname option, so send FQDN as
560 * DHCPv6 has no FQDN option and DHCPv4 must not send
561 * hostname and FQDN according to RFC4702 */
564 if (fqdn
!= FQDN_DISABLE
) {
565 if (ifo
->hostname
[0] == '\0')
566 hostname
= get_hostname(hbuf
, sizeof(hbuf
),
567 ifo
->options
& DHCPCD_HOSTNAME_SHORT
? 1 : 0);
569 hostname
= ifo
->hostname
;
571 hostname
= NULL
; /* appearse gcc */
573 /* Work out option size first */
577 if (state
->state
!= DH6S_RELEASE
) {
578 for (l
= 0, opt
= ifp
->ctx
->dhcp6_opts
;
579 l
< ifp
->ctx
->dhcp6_opts_len
;
582 for (n
= 0, opt2
= ifo
->dhcp6_override
;
583 n
< ifo
->dhcp6_override_len
;
586 if (opt
->option
== opt2
->option
)
589 if (n
< ifo
->dhcp6_override_len
)
591 if (!(opt
->type
& NOREQ
) &&
592 (opt
->type
& REQUEST
||
593 has_option_mask(ifo
->requestmask6
, opt
->option
)))
599 for (l
= 0, opt
= ifo
->dhcp6_override
;
600 l
< ifo
->dhcp6_override_len
;
603 if (!(opt
->type
& NOREQ
) &&
604 (opt
->type
& REQUEST
||
605 has_option_mask(ifo
->requestmask6
, opt
->option
)))
611 if (dhcp6_findselfsla(ifp
, NULL
)) {
618 if (fqdn
!= FQDN_DISABLE
)
619 len
+= sizeof(*o
) + 1 + encode_rfc1035(hostname
, NULL
);
621 if ((ifo
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) !=
622 DHCPCD_AUTH_SENDREQUIRE
)
623 len
+= sizeof(*o
); /* Reconfigure Accept */
626 len
+= sizeof(*state
->send
);
627 len
+= sizeof(*o
) + ifp
->ctx
->duid_len
;
628 len
+= sizeof(*o
) + sizeof(uint16_t); /* elapsed */
629 len
+= sizeof(*o
) + dhcp6_makevendor(NULL
, ifp
);
634 switch(state
->state
) {
637 ml
= state
->recv_len
;
646 si
= dhcp6_getmoption(D6_OPTION_SERVERID
, m
, ml
);
651 len
+= sizeof(*si
) + ntohs(si
->len
);
662 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
663 if (ap
->prefix_vltime
== 0 &&
664 !(ap
->flags
& IPV6_AF_REQUEST
))
666 if (ap
->ia_type
== D6_OPTION_IA_PD
) {
667 len
+= sizeof(*o
) + sizeof(u8
) +
668 sizeof(u32
) + sizeof(u32
) +
670 if (ap
->prefix_exclude_len
)
671 len
+= sizeof(*o
) + 1 +
672 (uint8_t)((ap
->prefix_exclude_len
-
673 ap
->prefix_len
- 1) / NBBY
) + 1;
675 len
+= sizeof(*o
) + sizeof(ap
->addr
) +
676 sizeof(u32
) + sizeof(u32
);
680 len
+= ifo
->ia_len
* (sizeof(*o
) + (sizeof(u32
) * 3));
687 if (state
->state
== DH6S_DISCOVER
&&
688 !(ifp
->ctx
->options
& DHCPCD_TEST
) &&
689 has_option_mask(ifo
->requestmask6
, D6_OPTION_RAPID_COMMIT
))
697 /* Depending on state, get the unicast address */
698 switch(state
->state
) {
700 case DH6S_INIT
: /* FALLTHROUGH */
702 type
= DHCP6_SOLICIT
;
705 type
= DHCP6_REQUEST
;
706 unicast
= dhcp6_getmoption(D6_OPTION_UNICAST
, m
, ml
);
709 type
= DHCP6_CONFIRM
;
716 unicast
= dhcp6_getmoption(D6_OPTION_UNICAST
, m
, ml
);
719 type
= DHCP6_INFORMATION_REQ
;
722 type
= DHCP6_RELEASE
;
723 unicast
= dhcp6_getmoption(D6_OPTION_UNICAST
, m
, ml
);
731 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
732 ssize_t alen
= dhcp_auth_encode(&ifo
->auth
,
733 state
->auth
.token
, NULL
, 0, 6, type
, NULL
, 0);
734 if (alen
!= -1 && alen
> UINT16_MAX
) {
739 logger(ifp
->ctx
, LOG_ERR
,
740 "%s: dhcp_auth_encode: %m", ifp
->name
);
741 else if (alen
!= 0) {
742 auth_len
= (uint16_t)alen
;
743 len
+= sizeof(*o
) + auth_len
;
747 state
->send
= malloc(len
);
748 if (state
->send
== NULL
)
751 state
->send_len
= len
;
752 state
->send
->type
= type
;
754 /* If we found a unicast option, copy it to our state for sending */
755 if (unicast
&& ntohs(unicast
->len
) == sizeof(state
->unicast
))
756 memcpy(&state
->unicast
, D6_COPTION_DATA(unicast
),
757 sizeof(state
->unicast
));
759 state
->unicast
= in6addr_any
;
761 dhcp6_newxid(ifp
, state
->send
);
763 o
= D6_FIRST_OPTION(state
->send
);
764 o
->code
= htons(D6_OPTION_CLIENTID
);
765 o
->len
= htons((uint16_t)ifp
->ctx
->duid_len
);
766 memcpy(D6_OPTION_DATA(o
), ifp
->ctx
->duid
, ifp
->ctx
->duid_len
);
769 o
= D6_NEXT_OPTION(o
);
770 memcpy(o
, si
, sizeof(*si
) + ntohs(si
->len
));
773 o
= D6_NEXT_OPTION(o
);
774 o
->code
= htons(D6_OPTION_ELAPSED
);
775 o
->len
= htons(sizeof(uint16_t));
776 p
= D6_OPTION_DATA(o
);
777 memset(p
, 0, sizeof(uint16_t));
779 o
= D6_NEXT_OPTION(o
);
780 dhcp6_makevendor(o
, ifp
);
782 if (state
->state
== DH6S_DISCOVER
&&
783 !(ifp
->ctx
->options
& DHCPCD_TEST
) &&
784 has_option_mask(ifo
->requestmask6
, D6_OPTION_RAPID_COMMIT
))
786 o
= D6_NEXT_OPTION(o
);
787 o
->code
= htons(D6_OPTION_RAPID_COMMIT
);
791 for (l
= 0; IA
&& l
< ifo
->ia_len
; l
++) {
792 o
= D6_NEXT_OPTION(o
);
793 o
->code
= htons(ifo
->ia
[l
].ia_type
);
794 o
->len
= htons(sizeof(u32
) + sizeof(u32
) + sizeof(u32
));
795 p
= D6_OPTION_DATA(o
);
796 memcpy(p
, ifo
->ia
[l
].iaid
, sizeof(u32
));
798 memset(p
, 0, sizeof(u32
) + sizeof(u32
));
799 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
800 if (ap
->prefix_vltime
== 0 &&
801 !(ap
->flags
& IPV6_AF_REQUEST
))
803 if (memcmp(ifo
->ia
[l
].iaid
, ap
->iaid
, sizeof(u32
)))
805 so
= D6_NEXT_OPTION(o
);
806 if (ap
->ia_type
== D6_OPTION_IA_PD
) {
807 so
->code
= htons(D6_OPTION_IAPREFIX
);
808 so
->len
= htons(sizeof(ap
->prefix
) +
809 sizeof(u32
) + sizeof(u32
) + sizeof(u8
));
810 pdp
= (struct dhcp6_pd_addr
*)
812 pdp
->pltime
= htonl(ap
->prefix_pltime
);
813 pdp
->vltime
= htonl(ap
->prefix_vltime
);
814 pdp
->prefix_len
= ap
->prefix_len
;
815 pdp
->prefix
= ap
->prefix
;
817 /* RFC6603 Section 4.2 */
818 if (ap
->prefix_exclude_len
) {
819 n
= (size_t)((ap
->prefix_exclude_len
-
820 ap
->prefix_len
- 1) / NBBY
) + 1;
821 eo
= D6_NEXT_OPTION(so
);
822 eo
->code
= htons(D6_OPTION_PD_EXCLUDE
);
823 eo
->len
= (uint16_t)(n
+ 1);
824 p
= D6_OPTION_DATA(eo
);
825 *p
++ = (uint8_t)ap
->prefix_exclude_len
;
826 pp
= ap
->prefix_exclude
.s6_addr
;
827 pp
+= (size_t)((ap
->prefix_len
- 1) / NBBY
)
829 u8
= ap
->prefix_len
% NBBY
;
835 *p
= (uint8_t)(*pp
<< u8
);
836 u16
= (uint16_t)(ntohs(so
->len
) +
837 sizeof(*eo
) + eo
->len
);
838 so
->len
= htons(u16
);
839 eo
->len
= htons(eo
->len
);
842 u16
= (uint16_t)(ntohs(o
->len
) + sizeof(*so
)
846 so
->code
= htons(D6_OPTION_IA_ADDR
);
847 so
->len
= sizeof(ap
->addr
) +
848 sizeof(u32
) + sizeof(u32
);
849 iap
= (struct dhcp6_ia_addr
*)
851 iap
->addr
= ap
->addr
;
852 iap
->pltime
= htonl(ap
->prefix_pltime
);
853 iap
->vltime
= htonl(ap
->prefix_vltime
);
854 u16
= (uint16_t)(ntohs(o
->len
) + sizeof(*so
)
856 so
->len
= htons(so
->len
);
862 if (state
->send
->type
!= DHCP6_RELEASE
) {
863 if (fqdn
!= FQDN_DISABLE
) {
864 o
= D6_NEXT_OPTION(o
);
865 o
->code
= htons(D6_OPTION_FQDN
);
866 p
= D6_OPTION_DATA(o
);
878 l
= encode_rfc1035(hostname
, p
+ 1);
881 o
->len
= htons((uint16_t)(l
+ 1));
884 if ((ifo
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) !=
885 DHCPCD_AUTH_SENDREQUIRE
)
887 o
= D6_NEXT_OPTION(o
);
888 o
->code
= htons(D6_OPTION_RECONF_ACCEPT
);
893 o
= D6_NEXT_OPTION(o
);
894 o
->code
= htons(D6_OPTION_ORO
);
896 p
= D6_OPTION_DATA(o
);
897 for (l
= 0, opt
= ifp
->ctx
->dhcp6_opts
;
898 l
< ifp
->ctx
->dhcp6_opts_len
;
901 for (n
= 0, opt2
= ifo
->dhcp6_override
;
902 n
< ifo
->dhcp6_override_len
;
905 if (opt
->option
== opt2
->option
)
908 if (n
< ifo
->dhcp6_override_len
)
910 if (!(opt
->type
& NOREQ
) &&
911 (opt
->type
& REQUEST
||
912 has_option_mask(ifo
->requestmask6
,
915 u16
= htons((uint16_t)opt
->option
);
916 memcpy(p
, &u16
, sizeof(u16
));
918 o
->len
= (uint16_t)(o
->len
+ sizeof(u16
));
921 for (l
= 0, opt
= ifo
->dhcp6_override
;
922 l
< ifo
->dhcp6_override_len
;
925 if (!(opt
->type
& NOREQ
) &&
926 (opt
->type
& REQUEST
||
927 has_option_mask(ifo
->requestmask6
,
930 u16
= htons((uint16_t)opt
->option
);
931 memcpy(p
, &u16
, sizeof(u16
));
933 o
->len
= (uint16_t)(o
->len
+ sizeof(u16
));
936 if (dhcp6_findselfsla(ifp
, NULL
)) {
937 u16
= htons(D6_OPTION_PD_EXCLUDE
);
938 memcpy(p
, &u16
, sizeof(u16
));
939 o
->len
= (uint16_t)(o
->len
+ sizeof(u16
));
941 o
->len
= htons(o
->len
);
945 /* This has to be the last option */
946 if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
&& auth_len
!= 0) {
947 o
= D6_NEXT_OPTION(o
);
948 o
->code
= htons(D6_OPTION_AUTH
);
949 o
->len
= htons((uint16_t)auth_len
);
950 /* data will be filled at send message time */
957 dhcp6_get_op(uint16_t type
)
959 const struct dhcp6_op
*d
;
961 for (d
= dhcp6_ops
; d
->name
; d
++)
968 dhcp6_freedrop_addrs(struct interface
*ifp
, int drop
,
969 const struct interface
*ifd
)
971 struct dhcp6_state
*state
;
973 state
= D6_STATE(ifp
);
975 ipv6_freedrop_addrs(&state
->addrs
, drop
, ifd
);
977 ipv6_buildroutes(ifp
->ctx
);
981 static void dhcp6_delete_delegates(struct interface
*ifp
)
983 struct interface
*ifp0
;
985 if (ifp
->ctx
->ifaces
) {
986 TAILQ_FOREACH(ifp0
, ifp
->ctx
->ifaces
, next
) {
988 dhcp6_freedrop_addrs(ifp0
, 1, ifp
);
994 dhcp6_update_auth(struct interface
*ifp
, struct dhcp6_message
*m
, size_t len
)
996 struct dhcp6_state
*state
;
997 const struct dhcp6_option
*co
;
998 struct dhcp6_option
*o
;
1000 co
= dhcp6_getmoption(D6_OPTION_AUTH
, m
, len
);
1005 state
= D6_STATE(ifp
);
1007 return dhcp_auth_encode(&ifp
->options
->auth
, state
->auth
.token
,
1008 (uint8_t *)state
->send
, state
->send_len
,
1009 6, state
->send
->type
,
1010 D6_OPTION_DATA(o
), ntohs(o
->len
));
1014 dhcp6_sendmessage(struct interface
*ifp
, void (*callback
)(void *))
1016 struct dhcp6_state
*state
;
1017 struct ipv6_ctx
*ctx
;
1018 struct sockaddr_in6 dst
;
1020 struct in6_pktinfo pi
;
1021 struct timespec RTprev
;
1025 const char *broad_uni
;
1026 const struct in6_addr alldhcp
= IN6ADDR_LINKLOCAL_ALLDHCP_INIT
;
1028 if (!callback
&& ifp
->carrier
== LINK_DOWN
)
1031 memset(&dst
, 0, sizeof(dst
));
1032 dst
.sin6_family
= AF_INET6
;
1033 dst
.sin6_port
= htons(DHCP6_SERVER_PORT
);
1035 dst
.sin6_len
= sizeof(dst
);
1038 state
= D6_STATE(ifp
);
1039 /* We need to ensure we have sufficient scope to unicast the address */
1040 /* XXX FIXME: We should check any added addresses we have like from
1041 * a Router Advertisement */
1042 if (IN6_IS_ADDR_UNSPECIFIED(&state
->unicast
) ||
1043 (state
->state
== DH6S_REQUEST
&&
1044 (!IN6_IS_ADDR_LINKLOCAL(&state
->unicast
) || !ipv6_linklocal(ifp
))))
1046 dst
.sin6_addr
= alldhcp
;
1047 broad_uni
= "broadcasting";
1049 dst
.sin6_addr
= state
->unicast
;
1050 broad_uni
= "unicasting";
1054 logger(ifp
->ctx
, LOG_DEBUG
,
1055 "%s: %s %s with xid 0x%02x%02x%02x",
1058 dhcp6_get_op(state
->send
->type
),
1059 state
->send
->xid
[0],
1060 state
->send
->xid
[1],
1061 state
->send
->xid
[2]);
1064 !(ifp
->options
->options
& DHCPCD_INITIAL_DELAY
))
1067 /* Some buggy PPP servers close the link too early
1068 * after sending an invalid status in their reply
1069 * which means this host won't see it.
1070 * 1 second grace seems to be the sweet spot. */
1071 if (ifp
->flags
& IFF_POINTOPOINT
)
1072 state
->RT
.tv_sec
= 1;
1074 state
->RT
.tv_sec
= 0;
1075 state
->RT
.tv_nsec
= (suseconds_t
)arc4random_uniform(
1076 (uint32_t)(state
->IMD
* NSEC_PER_SEC
));
1077 timespecnorm(&state
->RT
);
1078 broad_uni
= "delaying";
1081 if (state
->RTC
== 0) {
1082 RTprev
.tv_sec
= state
->IRT
;
1084 state
->RT
.tv_sec
= RTprev
.tv_sec
;
1085 state
->RT
.tv_nsec
= 0;
1088 timespecadd(&state
->RT
, &state
->RT
, &state
->RT
);
1091 rnd
= DHCP6_RAND_MIN
;
1092 rnd
+= (suseconds_t
)arc4random_uniform(
1093 DHCP6_RAND_MAX
- DHCP6_RAND_MIN
);
1094 rnd
/= MSEC_PER_SEC
;
1098 ts_to_ms(ms
, &RTprev
);
1099 ms
= (time_t)((double)ms
* rnd
);
1100 ms_to_ts(&RTprev
, ms
);
1102 timespecsub(&state
->RT
, &RTprev
, &state
->RT
);
1104 timespecadd(&state
->RT
, &RTprev
, &state
->RT
);
1106 if (state
->MRT
!= 0 && state
->RT
.tv_sec
> state
->MRT
) {
1107 RTprev
.tv_sec
= state
->MRT
;
1109 state
->RT
.tv_sec
= state
->MRT
;
1110 state
->RT
.tv_nsec
= 0;
1111 ts_to_ms(ms
, &RTprev
);
1112 ms
= (time_t)((double)ms
* rnd
);
1113 ms_to_ts(&RTprev
, ms
);
1115 timespecsub(&state
->RT
, &RTprev
, &state
->RT
);
1117 timespecadd(&state
->RT
, &RTprev
, &state
->RT
);
1121 if (ifp
->carrier
!= LINK_DOWN
)
1122 logger(ifp
->ctx
, LOG_DEBUG
,
1123 "%s: %s %s (xid 0x%02x%02x%02x),"
1124 " next in %0.1f seconds",
1127 dhcp6_get_op(state
->send
->type
),
1128 state
->send
->xid
[0],
1129 state
->send
->xid
[1],
1130 state
->send
->xid
[2],
1131 timespec_to_double(&state
->RT
));
1133 /* This sometimes happens when we delegate to this interface
1134 * AND run DHCPv6 on it normally. */
1135 assert(timespec_to_double(&state
->RT
) != 0);
1137 /* Wait the initial delay */
1138 if (state
->IMD
!= 0) {
1140 eloop_timeout_add_tv(ifp
->ctx
->eloop
,
1141 &state
->RT
, callback
, ifp
);
1146 if (ifp
->carrier
== LINK_DOWN
)
1149 /* Update the elapsed time */
1150 dhcp6_updateelapsed(ifp
, state
->send
, state
->send_len
);
1151 if (ifp
->options
->auth
.options
& DHCPCD_AUTH_SEND
&&
1152 dhcp6_update_auth(ifp
, state
->send
, state
->send_len
) == -1)
1154 logger(ifp
->ctx
, LOG_ERR
,
1155 "%s: dhcp6_updateauth: %m", ifp
->name
);
1160 ctx
= ifp
->ctx
->ipv6
;
1161 dst
.sin6_scope_id
= ifp
->index
;
1162 ctx
->sndhdr
.msg_name
= (void *)&dst
;
1163 ctx
->sndhdr
.msg_iov
[0].iov_base
= state
->send
;
1164 ctx
->sndhdr
.msg_iov
[0].iov_len
= state
->send_len
;
1166 /* Set the outbound interface */
1167 cm
= CMSG_FIRSTHDR(&ctx
->sndhdr
);
1168 if (cm
== NULL
) /* unlikely */
1170 cm
->cmsg_level
= IPPROTO_IPV6
;
1171 cm
->cmsg_type
= IPV6_PKTINFO
;
1172 cm
->cmsg_len
= CMSG_LEN(sizeof(pi
));
1173 memset(&pi
, 0, sizeof(pi
));
1174 pi
.ipi6_ifindex
= ifp
->index
;
1175 memcpy(CMSG_DATA(cm
), &pi
, sizeof(pi
));
1177 if (sendmsg(ctx
->dhcp_fd
, &ctx
->sndhdr
, 0) == -1) {
1178 logger(ifp
->ctx
, LOG_ERR
,
1179 "%s: %s: sendmsg: %m", ifp
->name
, __func__
);
1180 ifp
->options
->options
&= ~DHCPCD_IPV6
;
1181 dhcp6_drop(ifp
, "EXPIRE6");
1187 if (state
->MRC
== 0 || state
->RTC
< state
->MRC
)
1188 eloop_timeout_add_tv(ifp
->ctx
->eloop
,
1189 &state
->RT
, callback
, ifp
);
1190 else if (state
->MRC
!= 0 && state
->MRCcallback
)
1191 eloop_timeout_add_tv(ifp
->ctx
->eloop
,
1192 &state
->RT
, state
->MRCcallback
, ifp
);
1194 logger(ifp
->ctx
, LOG_WARNING
,
1195 "%s: sent %d times with no reply",
1196 ifp
->name
, state
->RTC
);
1202 dhcp6_sendinform(void *arg
)
1205 dhcp6_sendmessage(arg
, dhcp6_sendinform
);
1209 dhcp6_senddiscover(void *arg
)
1212 dhcp6_sendmessage(arg
, dhcp6_senddiscover
);
1216 dhcp6_sendrequest(void *arg
)
1219 dhcp6_sendmessage(arg
, dhcp6_sendrequest
);
1223 dhcp6_sendrebind(void *arg
)
1226 dhcp6_sendmessage(arg
, dhcp6_sendrebind
);
1230 dhcp6_sendrenew(void *arg
)
1233 dhcp6_sendmessage(arg
, dhcp6_sendrenew
);
1237 dhcp6_sendconfirm(void *arg
)
1240 dhcp6_sendmessage(arg
, dhcp6_sendconfirm
);
1244 dhcp6_sendrelease(void *arg
)
1247 dhcp6_sendmessage(arg
, dhcp6_sendrelease
);
1251 dhcp6_startrenew(void *arg
)
1253 struct interface
*ifp
;
1254 struct dhcp6_state
*state
;
1257 state
= D6_STATE(ifp
);
1258 state
->state
= DH6S_RENEW
;
1260 state
->IRT
= REN_TIMEOUT
;
1261 state
->MRT
= REN_MAX_RT
;
1264 if (dhcp6_makemessage(ifp
) == -1)
1265 logger(ifp
->ctx
, LOG_ERR
,
1266 "%s: dhcp6_makemessage: %m", ifp
->name
);
1268 dhcp6_sendrenew(ifp
);
1272 dhcp6_dadcompleted(const struct interface
*ifp
)
1274 const struct dhcp6_state
*state
;
1275 const struct ipv6_addr
*ap
;
1277 state
= D6_CSTATE(ifp
);
1278 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
1279 if (ap
->flags
& IPV6_AF_ADDED
&&
1280 !(ap
->flags
& IPV6_AF_DADCOMPLETED
))
1287 dhcp6_dadcallback(void *arg
)
1289 struct ipv6_addr
*ap
= arg
;
1290 struct interface
*ifp
;
1291 struct dhcp6_state
*state
;
1292 int wascompleted
, valid
;
1294 wascompleted
= (ap
->flags
& IPV6_AF_DADCOMPLETED
);
1295 ap
->flags
|= IPV6_AF_DADCOMPLETED
;
1296 if (ap
->flags
& IPV6_AF_DUPLICATED
)
1298 * We should decline the address */
1299 logger(ap
->iface
->ctx
, LOG_WARNING
, "%s: DAD detected %s",
1300 ap
->iface
->name
, ap
->saddr
);
1302 if (!wascompleted
) {
1304 state
= D6_STATE(ifp
);
1305 if (state
->state
== DH6S_BOUND
||
1306 state
->state
== DH6S_DELEGATED
)
1308 struct ipv6_addr
*ap2
;
1310 valid
= (ap
->delegating_iface
== NULL
);
1311 TAILQ_FOREACH(ap2
, &state
->addrs
, next
) {
1312 if (ap2
->flags
& IPV6_AF_ADDED
&&
1313 !(ap2
->flags
& IPV6_AF_DADCOMPLETED
))
1319 if (!wascompleted
) {
1320 logger(ap
->iface
->ctx
, LOG_DEBUG
,
1321 "%s: DHCPv6 DAD completed", ifp
->name
);
1322 script_runreason(ifp
,
1323 ap
->delegating_iface
?
1324 "DELEGATED6" : state
->reason
);
1326 dhcpcd_daemonise(ifp
->ctx
);
1333 dhcp6_addrequestedaddrs(struct interface
*ifp
)
1335 struct dhcp6_state
*state
;
1338 struct ipv6_addr
*a
;
1339 char iabuf
[INET6_ADDRSTRLEN
];
1342 state
= D6_STATE(ifp
);
1343 /* Add any requested prefixes / addresses */
1344 for (i
= 0; i
< ifp
->options
->ia_len
; i
++) {
1345 ia
= &ifp
->options
->ia
[i
];
1346 if (!((ia
->ia_type
== D6_OPTION_IA_PD
&& ia
->prefix_len
) ||
1347 !IN6_IS_ADDR_UNSPECIFIED(&ia
->addr
)))
1349 a
= calloc(1, sizeof(*a
));
1351 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
1354 a
->flags
= IPV6_AF_REQUEST
;
1356 a
->dadcallback
= dhcp6_dadcallback
;
1357 memcpy(&a
->iaid
, &ia
->iaid
, sizeof(a
->iaid
));
1358 a
->ia_type
= ia
->ia_type
;
1359 //a->prefix_pltime = 0;
1360 //a->prefix_vltime = 0;
1362 if (ia
->ia_type
== D6_OPTION_IA_PD
) {
1363 memcpy(&a
->prefix
, &ia
->addr
, sizeof(a
->addr
));
1364 a
->prefix_len
= ia
->prefix_len
;
1365 iap
= inet_ntop(AF_INET6
, &a
->prefix
,
1366 iabuf
, sizeof(iabuf
));
1368 memcpy(&a
->addr
, &ia
->addr
, sizeof(a
->addr
));
1370 * RFC 5942 Section 5
1371 * We cannot assume any prefix length, nor tie the
1372 * address to an existing one as it could expire
1373 * before the address.
1374 * As such we just give it a 128 prefix.
1376 a
->prefix_len
= 128;
1377 ipv6_makeprefix(&a
->prefix
, &a
->addr
, a
->prefix_len
);
1378 iap
= inet_ntop(AF_INET6
, &a
->addr
,
1379 iabuf
, sizeof(iabuf
));
1381 snprintf(a
->saddr
, sizeof(a
->saddr
),
1382 "%s/%d", iap
, a
->prefix_len
);
1383 TAILQ_INSERT_TAIL(&state
->addrs
, a
, next
);
1388 dhcp6_startdiscover(void *arg
)
1390 struct interface
*ifp
;
1391 struct dhcp6_state
*state
;
1394 dhcp6_delete_delegates(ifp
);
1395 logger(ifp
->ctx
, LOG_INFO
, "%s: soliciting a DHCPv6 lease", ifp
->name
);
1396 state
= D6_STATE(ifp
);
1397 state
->state
= DH6S_DISCOVER
;
1399 state
->IMD
= SOL_MAX_DELAY
;
1400 state
->IRT
= SOL_TIMEOUT
;
1401 state
->MRT
= state
->sol_max_rt
;
1404 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
1409 dhcp6_freedrop_addrs(ifp
, 0, NULL
);
1410 unlink(state
->leasefile
);
1412 dhcp6_addrequestedaddrs(ifp
);
1414 if (dhcp6_makemessage(ifp
) == -1)
1415 logger(ifp
->ctx
, LOG_ERR
,
1416 "%s: dhcp6_makemessage: %m", ifp
->name
);
1418 dhcp6_senddiscover(ifp
);
1422 dhcp6_failconfirm(void *arg
)
1424 struct interface
*ifp
;
1427 logger(ifp
->ctx
, LOG_ERR
,
1428 "%s: failed to confirm prior address", ifp
->name
);
1429 /* Section 18.1.2 says that we SHOULD use the last known
1430 * IP address(s) and lifetimes if we didn't get a reply.
1431 * I disagree with this. */
1432 dhcp6_startdiscover(ifp
);
1436 dhcp6_failrequest(void *arg
)
1438 struct interface
*ifp
;
1441 logger(ifp
->ctx
, LOG_ERR
, "%s: failed to request address", ifp
->name
);
1442 /* Section 18.1.1 says that client local policy dictates
1443 * what happens if a REQUEST fails.
1444 * Of the possible scenarios listed, moving back to the
1445 * DISCOVER phase makes more sense for us. */
1446 dhcp6_startdiscover(ifp
);
1450 dhcp6_failrebind(void *arg
)
1452 struct interface
*ifp
;
1455 logger(ifp
->ctx
, LOG_ERR
,
1456 "%s: failed to rebind prior delegation", ifp
->name
);
1457 dhcp6_delete_delegates(ifp
);
1458 /* Section 18.1.2 says that we SHOULD use the last known
1459 * IP address(s) and lifetimes if we didn't get a reply.
1460 * I disagree with this. */
1461 dhcp6_startdiscover(ifp
);
1466 dhcp6_hasprefixdelegation(struct interface
*ifp
)
1472 for (i
= 0; i
< ifp
->options
->ia_len
; i
++) {
1473 if (t
&& t
!= ifp
->options
->ia
[i
].ia_type
) {
1474 if (t
== D6_OPTION_IA_PD
||
1475 ifp
->options
->ia
[i
].ia_type
== D6_OPTION_IA_PD
)
1478 t
= ifp
->options
->ia
[i
].ia_type
;
1480 return t
== D6_OPTION_IA_PD
? 1 : 0;
1484 dhcp6_startrebind(void *arg
)
1486 struct interface
*ifp
;
1487 struct dhcp6_state
*state
;
1491 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp6_sendrenew
, ifp
);
1492 state
= D6_STATE(ifp
);
1493 if (state
->state
== DH6S_RENEW
)
1494 logger(ifp
->ctx
, LOG_WARNING
,
1495 "%s: failed to renew DHCPv6, rebinding", ifp
->name
);
1497 logger(ifp
->ctx
, LOG_INFO
,
1498 "%s: rebinding prior DHCPv6 lease", ifp
->name
);
1499 state
->state
= DH6S_REBIND
;
1503 /* RFC 3633 section 12.1 */
1504 pd
= dhcp6_hasprefixdelegation(ifp
);
1506 state
->IMD
= CNF_MAX_DELAY
;
1507 state
->IRT
= CNF_TIMEOUT
;
1508 state
->MRT
= CNF_MAX_RT
;
1510 state
->IRT
= REB_TIMEOUT
;
1511 state
->MRT
= REB_MAX_RT
;
1514 if (dhcp6_makemessage(ifp
) == -1)
1515 logger(ifp
->ctx
, LOG_ERR
,
1516 "%s: dhcp6_makemessage: %m", ifp
->name
);
1518 dhcp6_sendrebind(ifp
);
1520 /* RFC 3633 section 12.1 */
1522 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1523 CNF_MAX_RD
, dhcp6_failrebind
, ifp
);
1528 dhcp6_startrequest(struct interface
*ifp
)
1530 struct dhcp6_state
*state
;
1532 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp6_senddiscover
, ifp
);
1533 state
= D6_STATE(ifp
);
1534 state
->state
= DH6S_REQUEST
;
1536 state
->IRT
= REQ_TIMEOUT
;
1537 state
->MRT
= REQ_MAX_RT
;
1538 state
->MRC
= REQ_MAX_RC
;
1539 state
->MRCcallback
= dhcp6_failrequest
;
1541 if (dhcp6_makemessage(ifp
) == -1) {
1542 logger(ifp
->ctx
, LOG_ERR
,
1543 "%s: dhcp6_makemessage: %m", ifp
->name
);
1547 dhcp6_sendrequest(ifp
);
1551 dhcp6_startconfirm(struct interface
*ifp
)
1553 struct dhcp6_state
*state
;
1555 state
= D6_STATE(ifp
);
1556 state
->state
= DH6S_CONFIRM
;
1558 state
->IMD
= CNF_MAX_DELAY
;
1559 state
->IRT
= CNF_TIMEOUT
;
1560 state
->MRT
= CNF_MAX_RT
;
1563 logger(ifp
->ctx
, LOG_INFO
,
1564 "%s: confirming prior DHCPv6 lease", ifp
->name
);
1565 if (dhcp6_makemessage(ifp
) == -1) {
1566 logger(ifp
->ctx
, LOG_ERR
,
1567 "%s: dhcp6_makemessage: %m", ifp
->name
);
1570 dhcp6_sendconfirm(ifp
);
1571 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
1572 CNF_MAX_RD
, dhcp6_failconfirm
, ifp
);
1576 dhcp6_startinform(void *arg
)
1578 struct interface
*ifp
;
1579 struct dhcp6_state
*state
;
1582 state
= D6_STATE(ifp
);
1583 if (state
->new == NULL
|| ifp
->options
->options
& DHCPCD_DEBUG
)
1584 logger(ifp
->ctx
, LOG_INFO
,
1585 "%s: requesting DHCPv6 information", ifp
->name
);
1586 state
->state
= DH6S_INFORM
;
1588 state
->IMD
= INF_MAX_DELAY
;
1589 state
->IRT
= INF_TIMEOUT
;
1590 state
->MRT
= state
->inf_max_rt
;
1593 if (dhcp6_makemessage(ifp
) == -1)
1594 logger(ifp
->ctx
, LOG_ERR
,
1595 "%s: dhcp6_makemessage: %m", ifp
->name
);
1597 dhcp6_sendinform(ifp
);
1601 dhcp6_startexpire(void *arg
)
1603 struct interface
*ifp
;
1606 eloop_timeout_delete(ifp
->ctx
->eloop
, dhcp6_sendrebind
, ifp
);
1608 logger(ifp
->ctx
, LOG_ERR
, "%s: DHCPv6 lease expired", ifp
->name
);
1609 dhcp6_freedrop_addrs(ifp
, 1, NULL
);
1610 dhcp6_delete_delegates(ifp
);
1611 script_runreason(ifp
, "EXPIRE6");
1612 if (ipv6nd_hasradhcp(ifp
) || dhcp6_hasprefixdelegation(ifp
))
1613 dhcp6_startdiscover(ifp
);
1615 logger(ifp
->ctx
, LOG_WARNING
,
1616 "%s: no advertising IPv6 router wants DHCP", ifp
->name
);
1620 dhcp6_finishrelease(void *arg
)
1622 struct interface
*ifp
;
1623 struct dhcp6_state
*state
;
1625 ifp
= (struct interface
*)arg
;
1626 state
= D6_STATE(ifp
);
1627 state
->state
= DH6S_RELEASED
;
1628 dhcp6_drop(ifp
, "RELEASE6");
1632 dhcp6_startrelease(struct interface
*ifp
)
1634 struct dhcp6_state
*state
;
1636 state
= D6_STATE(ifp
);
1637 if (state
->state
!= DH6S_BOUND
)
1640 state
->state
= DH6S_RELEASE
;
1642 state
->IRT
= REL_TIMEOUT
;
1644 /* MRC of REL_MAX_RC is optional in RFC 3315 18.1.6 */
1646 state
->MRC
= REL_MAX_RC
;
1647 state
->MRCcallback
= dhcp6_finishrelease
;
1650 state
->MRCcallback
= NULL
;
1653 if (dhcp6_makemessage(ifp
) == -1)
1654 logger(ifp
->ctx
, LOG_ERR
,
1655 "%s: dhcp6_makemessage: %m", ifp
->name
);
1657 dhcp6_sendrelease(ifp
);
1658 dhcp6_finishrelease(ifp
);
1663 dhcp6_checkstatusok(const struct interface
*ifp
,
1664 const struct dhcp6_message
*m
, const uint8_t *p
, size_t len
)
1666 const struct dhcp6_option
*o
;
1671 o
= dhcp6_findoption(D6_OPTION_STATUS_CODE
, p
, len
);
1673 o
= dhcp6_getmoption(D6_OPTION_STATUS_CODE
, m
, len
);
1675 //logger(ifp->ctx, LOG_DEBUG, "%s: no status", ifp->name);
1679 len
= ntohs(o
->len
);
1680 if (len
< sizeof(code
)) {
1681 logger(ifp
->ctx
, LOG_ERR
, "%s: status truncated", ifp
->name
);
1685 p
= D6_COPTION_DATA(o
);
1686 memcpy(&code
, p
, sizeof(code
));
1688 if (code
== D6_STATUS_OK
)
1691 len
-= sizeof(code
);
1694 if (code
< sizeof(dhcp6_statuses
) / sizeof(char *)) {
1695 p
= (const uint8_t *)dhcp6_statuses
[code
];
1696 len
= strlen((const char *)p
);
1702 status
= malloc(len
+ 1);
1703 if (status
== NULL
) {
1704 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
1708 memcpy(status
, p
, len
);
1710 logger(ifp
->ctx
, LOG_ERR
, "%s: DHCPv6 REPLY: %s", ifp
->name
, status
);
1715 const struct ipv6_addr
*
1716 dhcp6_iffindaddr(const struct interface
*ifp
, const struct in6_addr
*addr
,
1719 const struct dhcp6_state
*state
;
1720 const struct ipv6_addr
*ap
;
1722 if ((state
= D6_STATE(ifp
)) != NULL
) {
1723 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
1724 if (ipv6_findaddrmatch(ap
, addr
, flags
))
1732 dhcp6_findaddr(struct dhcpcd_ctx
*ctx
, const struct in6_addr
*addr
,
1735 struct interface
*ifp
;
1736 struct ipv6_addr
*ap
;
1737 struct dhcp6_state
*state
;
1739 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1740 if ((state
= D6_STATE(ifp
)) != NULL
) {
1741 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
1742 if (ipv6_findaddrmatch(ap
, addr
, flags
))
1751 dhcp6_findna(struct interface
*ifp
, uint16_t ot
, const uint8_t *iaid
,
1752 const uint8_t *d
, size_t l
, const struct timespec
*acquired
)
1754 struct dhcp6_state
*state
;
1755 const struct dhcp6_option
*o
;
1756 struct ipv6_addr
*a
;
1757 char iabuf
[INET6_ADDRSTRLEN
];
1762 const struct dhcp6_ia_addr
*iap
;
1765 state
= D6_STATE(ifp
);
1766 while ((o
= dhcp6_findoption(D6_OPTION_IA_ADDR
, d
, l
))) {
1767 off
= (size_t)((const uint8_t *)o
- d
);
1770 u32
= ntohs(o
->len
);
1771 l
-= sizeof(*o
) + u32
;
1772 d
+= sizeof(*o
) + u32
;
1775 logger(ifp
->ctx
, LOG_ERR
,
1776 "%s: IA Address option truncated", ifp
->name
);
1779 iap
= (const struct dhcp6_ia_addr
*)D6_COPTION_DATA(o
);
1780 TAILQ_FOREACH(a
, &state
->addrs
, next
) {
1781 if (ipv6_findaddrmatch(a
, &iap
->addr
, 0))
1785 a
= calloc(1, sizeof(*a
));
1787 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
1791 a
->flags
= IPV6_AF_NEW
| IPV6_AF_ONLINK
;
1792 a
->dadcallback
= dhcp6_dadcallback
;
1794 memcpy(a
->iaid
, iaid
, sizeof(a
->iaid
));
1795 a
->addr
= iap
->addr
;
1796 a
->created
= *acquired
;
1799 * RFC 5942 Section 5
1800 * We cannot assume any prefix length, nor tie the
1801 * address to an existing one as it could expire
1802 * before the address.
1803 * As such we just give it a 128 prefix.
1805 a
->prefix_len
= 128;
1806 ipv6_makeprefix(&a
->prefix
, &a
->addr
, a
->prefix_len
);
1807 ia
= inet_ntop(AF_INET6
, &a
->addr
,
1808 iabuf
, sizeof(iabuf
));
1809 snprintf(a
->saddr
, sizeof(a
->saddr
),
1810 "%s/%d", ia
, a
->prefix_len
);
1812 TAILQ_INSERT_TAIL(&state
->addrs
, a
, next
);
1814 if (!(a
->flags
& IPV6_AF_ONLINK
))
1815 a
->flags
|= IPV6_AF_ONLINK
| IPV6_AF_NEW
;
1816 a
->flags
&= ~IPV6_AF_STALE
;
1818 a
->acquired
= *acquired
;
1819 a
->prefix_pltime
= ntohl(iap
->pltime
);
1820 u32
= ntohl(iap
->vltime
);
1821 if (a
->prefix_vltime
!= u32
) {
1822 a
->flags
|= IPV6_AF_NEW
;
1823 a
->prefix_vltime
= u32
;
1825 if (a
->prefix_pltime
&& a
->prefix_pltime
< state
->lowpl
)
1826 state
->lowpl
= a
->prefix_pltime
;
1827 if (a
->prefix_vltime
&& a
->prefix_vltime
> state
->expire
)
1828 state
->expire
= a
->prefix_vltime
;
1835 dhcp6_findpd(struct interface
*ifp
, const uint8_t *iaid
,
1836 const uint8_t *d
, size_t l
, const struct timespec
*acquired
)
1838 struct dhcp6_state
*state
;
1839 const struct dhcp6_option
*o
, *ex
;
1840 const uint8_t *p
, *op
;
1841 struct ipv6_addr
*a
;
1842 char iabuf
[INET6_ADDRSTRLEN
];
1848 const struct dhcp6_pd_addr
*pdp
;
1851 state
= D6_STATE(ifp
);
1852 while ((o
= dhcp6_findoption(D6_OPTION_IAPREFIX
, d
, l
))) {
1853 off
= (size_t)((const uint8_t *)o
- d
);
1857 l
-= sizeof(*o
) + ol
;
1858 d
+= sizeof(*o
) + ol
;
1859 if (ol
< sizeof(*pdp
)) {
1861 logger(ifp
->ctx
, LOG_ERR
,
1862 "%s: IA Prefix option truncated", ifp
->name
);
1866 pdp
= (const struct dhcp6_pd_addr
*)D6_COPTION_DATA(o
);
1867 TAILQ_FOREACH(a
, &state
->addrs
, next
) {
1868 if (IN6_ARE_ADDR_EQUAL(&a
->prefix
, &pdp
->prefix
))
1872 a
= calloc(1, sizeof(*a
));
1874 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
1878 a
->flags
= IPV6_AF_NEW
| IPV6_AF_DELEGATEDPFX
;
1879 a
->created
= *acquired
;
1880 a
->dadcallback
= dhcp6_dadcallback
;
1881 a
->ia_type
= D6_OPTION_IA_PD
;
1882 memcpy(a
->iaid
, iaid
, sizeof(a
->iaid
));
1883 a
->prefix
= pdp
->prefix
;
1884 a
->prefix_len
= pdp
->prefix_len
;
1885 ia
= inet_ntop(AF_INET6
, &a
->prefix
,
1886 iabuf
, sizeof(iabuf
));
1887 snprintf(a
->saddr
, sizeof(a
->saddr
),
1888 "%s/%d", ia
, a
->prefix_len
);
1889 TAILQ_INSERT_TAIL(&state
->addrs
, a
, next
);
1891 if (!(a
->flags
& IPV6_AF_DELEGATEDPFX
))
1892 a
->flags
|= IPV6_AF_NEW
| IPV6_AF_DELEGATEDPFX
;
1893 a
->flags
&= ~(IPV6_AF_STALE
| IPV6_AF_REQUEST
);
1894 if (a
->prefix_vltime
!= ntohl(pdp
->vltime
))
1895 a
->flags
|= IPV6_AF_NEW
;
1898 a
->acquired
= *acquired
;
1899 a
->prefix_pltime
= ntohl(pdp
->pltime
);
1900 a
->prefix_vltime
= ntohl(pdp
->vltime
);
1902 if (a
->prefix_pltime
&& a
->prefix_pltime
< state
->lowpl
)
1903 state
->lowpl
= a
->prefix_pltime
;
1904 if (a
->prefix_vltime
&& a
->prefix_vltime
> state
->expire
)
1905 state
->expire
= a
->prefix_vltime
;
1908 p
= D6_COPTION_DATA(o
) + sizeof(pdp
);
1909 ol
= (uint16_t)(ol
- sizeof(pdp
));
1910 ex
= dhcp6_findoption(D6_OPTION_PD_EXCLUDE
, p
, ol
);
1911 a
->prefix_exclude_len
= 0;
1912 memset(&a
->prefix_exclude
, 0, sizeof(a
->prefix_exclude
));
1915 struct dhcp6_option
*w
;
1920 wp
= D6_OPTION_DATA(w
);
1928 ol
= ntohs(ex
->len
);
1930 logger(ifp
->ctx
, LOG_ERR
,
1931 "%s: truncated PD Exclude", ifp
->name
);
1934 op
= D6_COPTION_DATA(ex
);
1935 a
->prefix_exclude_len
= *op
++;
1937 if (((a
->prefix_exclude_len
- a
->prefix_len
- 1) / NBBY
) + 1
1940 logger(ifp
->ctx
, LOG_ERR
,
1941 "%s: PD Exclude length mismatch", ifp
->name
);
1942 a
->prefix_exclude_len
= 0;
1945 u8
= a
->prefix_len
% NBBY
;
1946 memcpy(&a
->prefix_exclude
, &a
->prefix
,
1947 sizeof(a
->prefix_exclude
));
1950 pw
= a
->prefix_exclude
.s6_addr
+
1951 (a
->prefix_exclude_len
/ NBBY
) - 1;
1955 *pw
= (uint8_t)(*pw
| (*op
>> u8
));
1961 dhcp6_findia(struct interface
*ifp
, const struct dhcp6_message
*m
, size_t l
,
1962 const char *sfrom
, const struct timespec
*acquired
)
1964 struct dhcp6_state
*state
;
1965 const struct if_options
*ifo
;
1966 const struct dhcp6_option
*o
;
1970 uint32_t u32
, renew
, rebind
;
1973 char buf
[sizeof(iaid
) * 3];
1974 struct ipv6_addr
*ap
, *nap
;
1976 if (l
< sizeof(*m
)) {
1977 /* Should be impossible with guards at packet in
1978 * and reading leases */
1985 state
= D6_STATE(ifp
);
1986 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
1987 ap
->flags
|= IPV6_AF_STALE
;
1990 for (o
= D6_CFIRST_OPTION(m
); l
> sizeof(*o
); o
= D6_CNEXT_OPTION(o
)) {
1992 if (sizeof(*o
) + ol
> l
) {
1994 logger(ifp
->ctx
, LOG_ERR
,
1995 "%s: option overflow", ifp
->name
);
1998 l
-= sizeof(*o
) + ol
;
2000 code
= ntohs(o
->code
);
2002 case D6_OPTION_IA_TA
:
2005 case D6_OPTION_IA_NA
:
2006 case D6_OPTION_IA_PD
:
2014 logger(ifp
->ctx
, LOG_ERR
,
2015 "%s: IA option truncated", ifp
->name
);
2019 p
= D6_COPTION_DATA(o
);
2020 memcpy(iaid
, p
, sizeof(iaid
));
2022 ol
= (uint16_t)(ol
- sizeof(iaid
));
2024 for (j
= 0; j
< ifo
->ia_len
; j
++) {
2025 if (memcmp(&ifo
->ia
[j
].iaid
, iaid
, sizeof(iaid
)) == 0)
2028 if (j
== ifo
->ia_len
&&
2029 !(ifo
->ia_len
== 0 && ifp
->ctx
->options
& DHCPCD_DUMPLEASE
))
2031 logger(ifp
->ctx
, LOG_DEBUG
,
2032 "%s: ignoring unrequested IAID %s",
2034 hwaddr_ntoa(iaid
, sizeof(iaid
), buf
, sizeof(buf
)));
2037 if ( j
< ifo
->ia_len
&& ifo
->ia
[j
].ia_type
!= code
) {
2038 logger(ifp
->ctx
, LOG_ERR
,
2039 "%s: IAID %s: option type mismatch",
2041 hwaddr_ntoa(iaid
, sizeof(iaid
), buf
, sizeof(buf
)));
2045 if (code
!= D6_OPTION_IA_TA
) {
2046 memcpy(&u32
, p
, sizeof(u32
));
2049 ol
= (uint16_t)(ol
- sizeof(u32
));
2050 memcpy(&u32
, p
, sizeof(u32
));
2051 rebind
= ntohl(u32
);
2053 ol
= (uint16_t)(ol
- sizeof(u32
));
2055 renew
= rebind
= 0; /* appease gcc */
2056 if (dhcp6_checkstatusok(ifp
, NULL
, p
, ol
) == -1) {
2060 if (code
== D6_OPTION_IA_PD
) {
2061 if (dhcp6_findpd(ifp
, iaid
, p
, ol
, acquired
) == 0) {
2062 logger(ifp
->ctx
, LOG_WARNING
,
2063 "%s: %s: DHCPv6 REPLY missing Prefix",
2068 if (dhcp6_findna(ifp
, code
, iaid
, p
, ol
, acquired
) == 0)
2070 logger(ifp
->ctx
, LOG_WARNING
,
2071 "%s: %s: DHCPv6 REPLY missing IA Address",
2076 if (code
!= D6_OPTION_IA_TA
) {
2077 if (renew
> rebind
&& rebind
> 0) {
2079 logger(ifp
->ctx
, LOG_WARNING
,
2080 "%s: T1 (%d) > T2 (%d) from %s",
2081 ifp
->name
, renew
, rebind
, sfrom
);
2086 (renew
< state
->renew
|| state
->renew
== 0))
2087 state
->renew
= renew
;
2089 (rebind
< state
->rebind
|| state
->rebind
== 0))
2090 state
->rebind
= rebind
;
2094 TAILQ_FOREACH_SAFE(ap
, &state
->addrs
, next
, nap
) {
2095 if (ap
->flags
& IPV6_AF_STALE
) {
2096 eloop_q_timeout_delete(ifp
->ctx
->eloop
, 0, NULL
, ap
);
2097 if (ap
->flags
& IPV6_AF_REQUEST
) {
2098 ap
->prefix_vltime
= ap
->prefix_pltime
= 0;
2100 TAILQ_REMOVE(&state
->addrs
, ap
, next
);
2111 dhcp6_validatelease(struct interface
*ifp
,
2112 const struct dhcp6_message
*m
, size_t len
,
2113 const char *sfrom
, const struct timespec
*acquired
)
2115 struct dhcp6_state
*state
;
2119 if (len
<= sizeof(*m
)) {
2120 logger(ifp
->ctx
, LOG_ERR
,
2121 "%s: DHCPv6 lease truncated", ifp
->name
);
2125 state
= D6_STATE(ifp
);
2126 if (dhcp6_checkstatusok(ifp
, m
, NULL
, len
) == -1)
2129 state
->renew
= state
->rebind
= state
->expire
= 0;
2130 state
->lowpl
= ND6_INFINITE_LIFETIME
;
2132 clock_gettime(CLOCK_MONOTONIC
, &aq
);
2135 nia
= dhcp6_findia(ifp
, m
, len
, sfrom
, acquired
);
2137 logger(ifp
->ctx
, LOG_ERR
,
2138 "%s: no useable IA found in lease", ifp
->name
);
2145 dhcp6_writelease(const struct interface
*ifp
)
2147 const struct dhcp6_state
*state
;
2151 state
= D6_CSTATE(ifp
);
2152 logger(ifp
->ctx
, LOG_DEBUG
,
2153 "%s: writing lease `%s'", ifp
->name
, state
->leasefile
);
2155 fd
= open(state
->leasefile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
2157 logger(ifp
->ctx
, LOG_ERR
, "%s: dhcp6_writelease: %m", ifp
->name
);
2160 bytes
= write(fd
, state
->new, state
->new_len
);
2166 dhcp6_readlease(struct interface
*ifp
, int validate
)
2168 struct dhcp6_state
*state
;
2172 const struct dhcp6_option
*o
;
2173 struct timespec acquired
;
2177 state
= D6_STATE(ifp
);
2178 if (stat(state
->leasefile
, &st
) == -1)
2180 logger(ifp
->ctx
, LOG_DEBUG
, "%s: reading lease `%s'",
2181 ifp
->name
, state
->leasefile
);
2182 if (st
.st_size
> UINT32_MAX
) {
2186 if ((fd
= open(state
->leasefile
, O_RDONLY
)) == -1)
2188 if ((state
->new = malloc((size_t)st
.st_size
)) == NULL
)
2191 state
->new_len
= (size_t)st
.st_size
;
2192 bytes
= read(fd
, state
->new, state
->new_len
);
2194 if (bytes
!= (ssize_t
)state
->new_len
)
2197 /* If not validating IA's and if they have expired,
2198 * skip to the auth check. */
2204 if ((now
= time(NULL
)) == -1)
2207 clock_gettime(CLOCK_MONOTONIC
, &acquired
);
2208 acquired
.tv_sec
-= now
- st
.st_mtime
;
2210 /* Check to see if the lease is still valid */
2211 fd
= dhcp6_validatelease(ifp
, state
->new, state
->new_len
, NULL
,
2216 if (!(ifp
->ctx
->options
& DHCPCD_DUMPLEASE
) &&
2217 state
->expire
!= ND6_INFINITE_LIFETIME
)
2219 if ((time_t)state
->expire
< now
- st
.st_mtime
) {
2221 LOG_DEBUG
,"%s: discarding expired lease",
2231 /* Authenticate the message */
2232 o
= dhcp6_getmoption(D6_OPTION_AUTH
, state
->new, state
->new_len
);
2234 if (dhcp_auth_validate(&state
->auth
, &ifp
->options
->auth
,
2235 (uint8_t *)state
->new, state
->new_len
, 6, state
->new->type
,
2236 D6_COPTION_DATA(o
), ntohs(o
->len
)) == NULL
)
2238 logger(ifp
->ctx
, LOG_DEBUG
,
2239 "%s: dhcp_auth_validate: %m", ifp
->name
);
2240 logger(ifp
->ctx
, LOG_ERR
,
2241 "%s: authentication failed", ifp
->name
);
2244 if (state
->auth
.token
)
2245 logger(ifp
->ctx
, LOG_DEBUG
,
2246 "%s: validated using 0x%08" PRIu32
,
2247 ifp
->name
, state
->auth
.token
->secretid
);
2249 logger(ifp
->ctx
, LOG_DEBUG
,
2250 "%s: accepted reconfigure key", ifp
->name
);
2251 } else if ((ifp
->options
->auth
.options
& DHCPCD_AUTH_SENDREQUIRE
) ==
2252 DHCPCD_AUTH_SENDREQUIRE
)
2254 logger(ifp
->ctx
, LOG_ERR
,
2255 "%s: authentication now required", ifp
->name
);
2262 dhcp6_freedrop_addrs(ifp
, 0, NULL
);
2266 if (!(ifp
->ctx
->options
& DHCPCD_DUMPLEASE
))
2267 unlink(state
->leasefile
);
2272 dhcp6_startinit(struct interface
*ifp
)
2274 struct dhcp6_state
*state
;
2276 uint8_t has_ta
, has_non_ta
;
2279 state
= D6_STATE(ifp
);
2280 state
->state
= DH6S_INIT
;
2281 state
->expire
= ND6_INFINITE_LIFETIME
;
2282 state
->lowpl
= ND6_INFINITE_LIFETIME
;
2284 dhcp6_addrequestedaddrs(ifp
);
2285 has_ta
= has_non_ta
= 0;
2286 for (i
= 0; i
< ifp
->options
->ia_len
; i
++) {
2287 switch (ifp
->options
->ia
[i
].ia_type
) {
2288 case D6_OPTION_IA_TA
:
2296 if (!(ifp
->ctx
->options
& DHCPCD_TEST
) &&
2297 !(has_ta
&& !has_non_ta
) &&
2298 ifp
->options
->reboot
!= 0)
2300 r
= dhcp6_readlease(ifp
, 1);
2302 if (errno
!= ENOENT
)
2303 logger(ifp
->ctx
, LOG_ERR
,
2304 "%s: dhcp6_readlease: %s: %m",
2305 ifp
->name
, state
->leasefile
);
2306 } else if (r
!= 0) {
2307 /* RFC 3633 section 12.1 */
2308 if (dhcp6_hasprefixdelegation(ifp
))
2309 dhcp6_startrebind(ifp
);
2311 dhcp6_startconfirm(ifp
);
2315 dhcp6_startdiscover(ifp
);
2318 static struct ipv6_addr
*
2319 dhcp6_ifdelegateaddr(struct interface
*ifp
, struct ipv6_addr
*prefix
,
2320 const struct if_sla
*sla
, struct if_ia
*ia
, struct interface
*ifs
)
2322 struct dhcp6_state
*state
;
2323 struct in6_addr addr
;
2324 struct ipv6_addr
*a
, *ap
, *apn
;
2325 char sabuf
[INET6_ADDRSTRLEN
];
2329 /* RFC6603 Section 4.2 */
2330 if (strcmp(ifp
->name
, ifs
->name
) == 0) {
2331 if (prefix
->prefix_exclude_len
== 0) {
2332 /* Don't spam the log automatically */
2334 logger(ifp
->ctx
, LOG_WARNING
,
2335 "%s: DHCPv6 server does not support "
2336 "OPTION_PD_EXCLUDE",
2340 pfxlen
= prefix
->prefix_exclude_len
;
2341 memcpy(&addr
, &prefix
->prefix_exclude
, sizeof(addr
));
2342 } else if ((pfxlen
= dhcp6_delegateaddr(&addr
, ifp
, prefix
,
2347 a
= calloc(1, sizeof(*a
));
2349 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
2353 a
->flags
= IPV6_AF_NEW
| IPV6_AF_ONLINK
;
2354 a
->dadcallback
= dhcp6_dadcallback
;
2355 a
->delegating_iface
= ifs
;
2356 memcpy(&a
->iaid
, &prefix
->iaid
, sizeof(a
->iaid
));
2357 a
->created
= a
->acquired
= prefix
->acquired
;
2358 a
->prefix_pltime
= prefix
->prefix_pltime
;
2359 a
->prefix_vltime
= prefix
->prefix_vltime
;
2361 a
->prefix_len
= (uint8_t)pfxlen
;
2363 /* Wang a 1 at the end as the prefix could be >64
2364 * making SLAAC impossible. */
2365 a
->addr
= a
->prefix
;
2366 a
->addr
.s6_addr
[sizeof(a
->addr
.s6_addr
) - 1] =
2367 (uint8_t)(a
->addr
.s6_addr
[sizeof(a
->addr
.s6_addr
) - 1] + 1);
2369 state
= D6_STATE(ifp
);
2370 /* Remove any exiting address */
2371 TAILQ_FOREACH_SAFE(ap
, &state
->addrs
, next
, apn
) {
2372 if (IN6_ARE_ADDR_EQUAL(&ap
->addr
, &a
->addr
)) {
2373 TAILQ_REMOVE(&state
->addrs
, ap
, next
);
2374 /* Keep our flags */
2375 a
->flags
|= ap
->flags
;
2376 a
->flags
&= ~IPV6_AF_NEW
;
2377 a
->created
= ap
->created
;
2382 sa
= inet_ntop(AF_INET6
, &a
->addr
, sabuf
, sizeof(sabuf
));
2383 snprintf(a
->saddr
, sizeof(a
->saddr
), "%s/%d", sa
, a
->prefix_len
);
2384 TAILQ_INSERT_TAIL(&state
->addrs
, a
, next
);
2389 dhcp6_script_try_run(struct interface
*ifp
, int delegated
)
2391 struct dhcp6_state
*state
;
2392 struct ipv6_addr
*ap
;
2395 state
= D6_STATE(ifp
);
2397 /* If all addresses have completed DAD run the script */
2398 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
2399 if (!(ap
->flags
& IPV6_AF_ADDED
))
2401 if (ap
->flags
& IPV6_AF_ONLINK
) {
2402 if (!(ap
->flags
& IPV6_AF_DADCOMPLETED
) &&
2403 ipv6_iffindaddr(ap
->iface
, &ap
->addr
))
2404 ap
->flags
|= IPV6_AF_DADCOMPLETED
;
2405 if ((ap
->flags
& IPV6_AF_DADCOMPLETED
) == 0 &&
2406 ((delegated
&& ap
->delegating_iface
) ||
2407 (!delegated
&& !ap
->delegating_iface
)))
2415 script_runreason(ifp
, delegated
? "DELEGATED6" : state
->reason
);
2417 dhcpcd_daemonise(ifp
->ctx
);
2419 logger(ifp
->ctx
, LOG_DEBUG
,
2420 "%s: waiting for DHCPv6 DAD to complete", ifp
->name
);
2424 dhcp6_delegate_prefix(struct interface
*ifp
)
2426 struct if_options
*ifo
;
2427 struct dhcp6_state
*state
, *ifd_state
;
2428 struct ipv6_addr
*ap
;
2432 struct interface
*ifd
;
2433 uint8_t carrier_warned
, abrt
;
2436 state
= D6_STATE(ifp
);
2438 /* Try to load configured interfaces for delegation that do not exist */
2439 for (i
= 0; i
< ifo
->ia_len
; i
++) {
2441 for (j
= 0; j
< ia
->sla_len
; j
++) {
2443 for (k
= 0; k
< i
; j
++)
2444 if (strcmp(sla
->ifname
, ia
->sla
[j
].ifname
) == 0)
2447 if_find(ifp
->ctx
->ifaces
, sla
->ifname
) == NULL
)
2449 logger(ifp
->ctx
, LOG_INFO
,
2450 "%s: loading for delegation", sla
->ifname
);
2451 if (dhcpcd_handleinterface(ifp
->ctx
, 2,
2453 logger(ifp
->ctx
, LOG_ERR
,
2454 "%s: interface does not exist"
2461 TAILQ_FOREACH(ifd
, ifp
->ctx
->ifaces
, next
) {
2463 carrier_warned
= abrt
= 0;
2464 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
2465 if (!(ap
->flags
& IPV6_AF_DELEGATEDPFX
))
2467 if (ap
->flags
& IPV6_AF_NEW
) {
2468 ap
->flags
&= ~IPV6_AF_NEW
;
2469 logger(ifp
->ctx
, LOG_DEBUG
,
2470 "%s: delegated prefix %s",
2471 ifp
->name
, ap
->saddr
);
2473 for (i
= 0; i
< ifo
->ia_len
; i
++) {
2475 if (memcmp(ia
->iaid
, ap
->iaid
,
2478 if (ia
->sla_len
== 0) {
2479 /* no SLA configured, so lets
2481 if (ifd
->carrier
!= LINK_UP
) {
2482 logger(ifp
->ctx
, LOG_DEBUG
,
2483 "%s: has no carrier, cannot"
2484 " delegate addresses",
2489 if (dhcp6_ifdelegateaddr(ifd
, ap
,
2493 for (j
= 0; j
< ia
->sla_len
; j
++) {
2495 if (sla
->sla_set
&& sla
->sla
== 0)
2497 IPV6_AF_DELEGATEDZERO
;
2498 if (strcmp(ifd
->name
, sla
->ifname
))
2500 if (ifd
->carrier
!= LINK_UP
) {
2501 logger(ifp
->ctx
, LOG_DEBUG
,
2502 "%s: has no carrier, cannot"
2503 " delegate addresses",
2508 if (dhcp6_ifdelegateaddr(ifd
, ap
,
2512 if (carrier_warned
||abrt
)
2515 if (carrier_warned
|| abrt
)
2518 if (k
&& !carrier_warned
) {
2519 ifd_state
= D6_STATE(ifd
);
2520 ipv6_addaddrs(&ifd_state
->addrs
);
2522 dhcp6_script_try_run(ifd
, 1);
2528 dhcp6_find_delegates1(void *arg
)
2531 dhcp6_find_delegates(arg
);
2535 dhcp6_find_delegates(struct interface
*ifp
)
2537 struct if_options
*ifo
;
2538 struct dhcp6_state
*state
;
2539 struct ipv6_addr
*ap
;
2543 struct interface
*ifd
;
2546 TAILQ_FOREACH(ifd
, ifp
->ctx
->ifaces
, next
) {
2548 state
= D6_STATE(ifd
);
2549 if (state
== NULL
|| state
->state
!= DH6S_BOUND
)
2551 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
2552 if (!(ap
->flags
& IPV6_AF_DELEGATEDPFX
))
2554 for (i
= 0; i
< ifo
->ia_len
; i
++) {
2556 if (memcmp(ia
->iaid
, ap
->iaid
,
2559 for (j
= 0; j
< ia
->sla_len
; j
++) {
2561 if (strcmp(ifp
->name
, sla
->ifname
))
2563 if (ipv6_linklocal(ifp
) == NULL
) {
2564 logger(ifp
->ctx
, LOG_DEBUG
,
2565 "%s: delaying adding"
2566 " delegated addresses for"
2569 ipv6_addlinklocalcallback(ifp
,
2570 dhcp6_find_delegates1
, ifp
);
2573 if (dhcp6_ifdelegateaddr(ifp
, ap
,
2582 logger(ifp
->ctx
, LOG_INFO
,
2583 "%s: adding delegated prefixes", ifp
->name
);
2584 state
= D6_STATE(ifp
);
2585 state
->state
= DH6S_DELEGATED
;
2586 ipv6_addaddrs(&state
->addrs
);
2588 ipv6_buildroutes(ifp
->ctx
);
2589 dhcp6_script_try_run(ifp
, 1);
2596 dhcp6_handledata(void *arg
)
2598 struct dhcpcd_ctx
*dctx
;
2599 struct ipv6_ctx
*ctx
;
2603 struct in6_pktinfo pkt
;
2604 struct interface
*ifp
;
2606 struct dhcp6_message
*r
;
2607 struct dhcp6_state
*state
;
2608 const struct dhcp6_option
*o
, *auth
;
2609 const struct dhcp_opt
*opt
;
2610 const struct if_options
*ifo
;
2611 struct ipv6_addr
*ap
;
2618 ctx
->rcvhdr
.msg_controllen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
2619 bytes
= recvmsg(ctx
->dhcp_fd
, &ctx
->rcvhdr
, 0);
2621 logger(dctx
, LOG_ERR
, "%s: recvmsg: %m", __func__
);
2622 close(ctx
->dhcp_fd
);
2623 eloop_event_delete(dctx
->eloop
, ctx
->dhcp_fd
);
2627 len
= (size_t)bytes
;
2628 ctx
->sfrom
= inet_ntop(AF_INET6
, &ctx
->from
.sin6_addr
,
2629 ctx
->ntopbuf
, sizeof(ctx
->ntopbuf
));
2630 if (len
< sizeof(struct dhcp6_message
)) {
2631 logger(dctx
, LOG_ERR
,
2632 "DHCPv6 packet too short from %s", ctx
->sfrom
);
2636 pkt
.ipi6_ifindex
= 0;
2637 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&ctx
->rcvhdr
);
2639 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&ctx
->rcvhdr
, cm
))
2641 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2643 switch(cm
->cmsg_type
) {
2645 if (cm
->cmsg_len
== CMSG_LEN(sizeof(pkt
)))
2646 memcpy(&pkt
, CMSG_DATA(cm
), sizeof(pkt
));
2650 if (pkt
.ipi6_ifindex
== 0) {
2651 logger(dctx
, LOG_ERR
,
2652 "DHCPv6 reply did not contain index from %s", ctx
->sfrom
);
2656 TAILQ_FOREACH(ifp
, dctx
->ifaces
, next
) {
2657 if (ifp
->index
== (unsigned int)pkt
.ipi6_ifindex
)
2661 logger(dctx
, LOG_DEBUG
,
2662 "DHCPv6 reply for unexpected interface from %s",
2667 state
= D6_STATE(ifp
);
2668 if (state
== NULL
|| state
->send
== NULL
) {
2669 logger(ifp
->ctx
, LOG_DEBUG
,
2670 "%s: DHCPv6 reply received but not running", ifp
->name
);
2674 r
= (struct dhcp6_message
*)ctx
->rcvhdr
.msg_iov
[0].iov_base
;
2675 /* We're already bound and this message is for another machine */
2676 /* XXX DELEGATED? */
2677 if (r
->type
!= DHCP6_RECONFIGURE
&&
2678 (state
->state
== DH6S_BOUND
|| state
->state
== DH6S_INFORMED
))
2680 logger(ifp
->ctx
, LOG_DEBUG
,
2681 "%s: DHCPv6 reply received but already bound", ifp
->name
);
2685 if (r
->type
!= DHCP6_RECONFIGURE
&&
2686 (r
->xid
[0] != state
->send
->xid
[0] ||
2687 r
->xid
[1] != state
->send
->xid
[1] ||
2688 r
->xid
[2] != state
->send
->xid
[2]))
2690 logger(dctx
, LOG_DEBUG
,
2691 "%s: wrong xid 0x%02x%02x%02x"
2692 " (expecting 0x%02x%02x%02x) from %s",
2694 r
->xid
[0], r
->xid
[1], r
->xid
[2],
2695 state
->send
->xid
[0], state
->send
->xid
[1],
2696 state
->send
->xid
[2],
2701 if (dhcp6_getmoption(D6_OPTION_SERVERID
, r
, len
) == NULL
) {
2702 logger(ifp
->ctx
, LOG_DEBUG
, "%s: no DHCPv6 server ID from %s",
2703 ifp
->name
, ctx
->sfrom
);
2707 o
= dhcp6_getmoption(D6_OPTION_CLIENTID
, r
, len
);
2708 if (o
== NULL
|| ntohs(o
->len
) != dctx
->duid_len
||
2709 memcmp(D6_COPTION_DATA(o
), dctx
->duid
, dctx
->duid_len
) != 0)
2711 logger(ifp
->ctx
, LOG_DEBUG
, "%s: incorrect client ID from %s",
2712 ifp
->name
, ctx
->sfrom
);
2717 for (i
= 0, opt
= dctx
->dhcp6_opts
;
2718 i
< dctx
->dhcp6_opts_len
;
2721 if (has_option_mask(ifo
->requiremask6
, opt
->option
) &&
2722 dhcp6_getmoption((uint16_t)opt
->option
, r
, len
) == NULL
)
2724 logger(ifp
->ctx
, LOG_WARNING
,
2725 "%s: reject DHCPv6 (no option %s) from %s",
2726 ifp
->name
, opt
->var
, ctx
->sfrom
);
2729 if (has_option_mask(ifo
->rejectmask6
, opt
->option
) &&
2730 dhcp6_getmoption((uint16_t)opt
->option
, r
, len
))
2732 logger(ifp
->ctx
, LOG_WARNING
,
2733 "%s: reject DHCPv6 (option %s) from %s",
2734 ifp
->name
, opt
->var
, ctx
->sfrom
);
2739 /* Authenticate the message */
2740 auth
= dhcp6_getmoption(D6_OPTION_AUTH
, r
, len
);
2742 if (dhcp_auth_validate(&state
->auth
, &ifo
->auth
,
2743 (uint8_t *)r
, len
, 6, r
->type
,
2744 D6_COPTION_DATA(auth
), ntohs(auth
->len
)) == NULL
)
2746 logger(ifp
->ctx
, LOG_DEBUG
, "dhcp_auth_validate: %m");
2747 logger(ifp
->ctx
, LOG_ERR
,
2748 "%s: authentication failed from %s",
2749 ifp
->name
, ctx
->sfrom
);
2752 if (state
->auth
.token
)
2753 logger(ifp
->ctx
, LOG_DEBUG
,
2754 "%s: validated using 0x%08" PRIu32
,
2755 ifp
->name
, state
->auth
.token
->secretid
);
2757 logger(ifp
->ctx
, LOG_DEBUG
,
2758 "%s: accepted reconfigure key", ifp
->name
);
2759 } else if (ifo
->auth
.options
& DHCPCD_AUTH_SEND
) {
2760 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
) {
2761 logger(ifp
->ctx
, LOG_ERR
,
2762 "%s: no authentication from %s",
2763 ifp
->name
, ctx
->sfrom
);
2766 logger(ifp
->ctx
, LOG_WARNING
,
2767 "%s: no authentication from %s", ifp
->name
, ctx
->sfrom
);
2770 op
= dhcp6_get_op(r
->type
);
2773 switch(state
->state
) {
2775 if (dhcp6_checkstatusok(ifp
, r
, NULL
, len
) == -1)
2778 o
= dhcp6_getmoption(D6_OPTION_INFO_REFRESH_TIME
,
2780 if (o
== NULL
|| ntohs(o
->len
) != sizeof(u32
))
2781 state
->renew
= IRT_DEFAULT
;
2783 memcpy(&u32
, D6_COPTION_DATA(o
), sizeof(u32
));
2784 state
->renew
= ntohl(u32
);
2785 if (state
->renew
< IRT_MINIMUM
)
2786 state
->renew
= IRT_MINIMUM
;
2790 error
= dhcp6_checkstatusok(ifp
, r
, NULL
, len
);
2791 /* If we got an OK status the chances are that we
2792 * didn't get the IA's returned, so preserve them
2793 * from our saved response */
2797 dhcp6_validatelease(ifp
, r
, len
,
2798 ctx
->sfrom
, NULL
) == -1)
2800 dhcp6_startdiscover(ifp
);
2805 if (has_option_mask(ifo
->requestmask6
,
2806 D6_OPTION_RAPID_COMMIT
) &&
2807 dhcp6_getmoption(D6_OPTION_RAPID_COMMIT
, r
, len
))
2808 state
->state
= DH6S_REQUEST
;
2811 case DH6S_REQUEST
: /* FALLTHROUGH */
2812 case DH6S_RENEW
: /* FALLTHROUGH */
2814 if (dhcp6_validatelease(ifp
, r
, len
,
2815 ctx
->sfrom
, NULL
) == -1)
2817 /* PD doesn't use CONFIRM, so REBIND could
2818 * throw up an invalid prefix if we
2820 if (dhcp6_hasprefixdelegation(ifp
))
2821 dhcp6_startdiscover(ifp
);
2829 case DHCP6_ADVERTISE
:
2830 if (state
->state
!= DH6S_DISCOVER
) {
2835 o
= dhcp6_getmoption(D6_OPTION_SOL_MAX_RT
, r
, len
);
2836 if (o
&& ntohs(o
->len
) >= sizeof(u32
)) {
2837 memcpy(&u32
, D6_COPTION_DATA(o
), sizeof(u32
));
2839 if (u32
>= 60 && u32
<= 86400) {
2840 logger(ifp
->ctx
, LOG_DEBUG
,
2841 "%s: SOL_MAX_RT %llu -> %d", ifp
->name
,
2842 (unsigned long long)state
->sol_max_rt
, u32
);
2843 state
->sol_max_rt
= (time_t)u32
;
2845 logger(ifp
->ctx
, LOG_ERR
,
2846 "%s: invalid SOL_MAX_RT %d",
2849 o
= dhcp6_getmoption(D6_OPTION_INF_MAX_RT
, r
, len
);
2850 if (o
&& ntohs(o
->len
) >= sizeof(u32
)) {
2851 memcpy(&u32
, D6_COPTION_DATA(o
), sizeof(u32
));
2853 if (u32
>= 60 && u32
<= 86400) {
2854 logger(ifp
->ctx
, LOG_DEBUG
,
2855 "%s: INF_MAX_RT %llu -> %d",
2857 (unsigned long long)state
->inf_max_rt
, u32
);
2858 state
->inf_max_rt
= (time_t)u32
;
2860 logger(ifp
->ctx
, LOG_ERR
,
2861 "%s: invalid INF_MAX_RT %d",
2864 if (dhcp6_validatelease(ifp
, r
, len
, ctx
->sfrom
, NULL
) == -1)
2867 case DHCP6_RECONFIGURE
:
2869 logger(ifp
->ctx
, LOG_ERR
,
2870 "%s: unauthenticated %s from %s",
2871 ifp
->name
, op
, ctx
->sfrom
);
2872 if (ifo
->auth
.options
& DHCPCD_AUTH_REQUIRE
)
2875 logger(ifp
->ctx
, LOG_INFO
, "%s: %s from %s",
2876 ifp
->name
, op
, ctx
->sfrom
);
2877 o
= dhcp6_getmoption(D6_OPTION_RECONF_MSG
, r
, len
);
2879 logger(ifp
->ctx
, LOG_ERR
,
2880 "%s: missing Reconfigure Message option",
2884 if (ntohs(o
->len
) != 1) {
2885 logger(ifp
->ctx
, LOG_ERR
,
2886 "%s: missing Reconfigure Message type", ifp
->name
);
2889 switch(*D6_COPTION_DATA(o
)) {
2891 if (state
->state
!= DH6S_BOUND
) {
2892 logger(ifp
->ctx
, LOG_ERR
,
2893 "%s: not bound, ignoring %s",
2897 eloop_timeout_delete(ifp
->ctx
->eloop
,
2898 dhcp6_startrenew
, ifp
);
2899 dhcp6_startrenew(ifp
);
2901 case DHCP6_INFORMATION_REQ
:
2902 if (state
->state
!= DH6S_INFORMED
) {
2903 logger(ifp
->ctx
, LOG_ERR
,
2904 "%s: not informed, ignoring %s",
2908 eloop_timeout_delete(ifp
->ctx
->eloop
,
2909 dhcp6_sendinform
, ifp
);
2910 dhcp6_startinform(ifp
);
2913 logger(ifp
->ctx
, LOG_ERR
,
2914 "%s: unsupported %s type %d",
2915 ifp
->name
, op
, *D6_COPTION_DATA(o
));
2920 logger(ifp
->ctx
, LOG_ERR
, "%s: invalid DHCP6 type %s (%d)",
2921 ifp
->name
, op
, r
->type
);
2925 logger(ifp
->ctx
, LOG_WARNING
,
2926 "%s: invalid state for DHCP6 type %s (%d)",
2927 ifp
->name
, op
, r
->type
);
2931 if (state
->recv_len
< (size_t)len
) {
2933 state
->recv
= malloc(len
);
2934 if (state
->recv
== NULL
) {
2935 logger(ifp
->ctx
, LOG_ERR
,
2936 "%s: malloc recv: %m", ifp
->name
);
2940 memcpy(state
->recv
, r
, len
);
2941 state
->recv_len
= len
;
2944 case DHCP6_ADVERTISE
:
2945 if (state
->state
== DH6S_REQUEST
) /* rapid commit */
2947 ap
= TAILQ_FIRST(&state
->addrs
);
2948 logger(ifp
->ctx
, LOG_INFO
, "%s: ADV %s from %s",
2949 ifp
->name
, ap
->saddr
, ctx
->sfrom
);
2950 if (ifp
->ctx
->options
& DHCPCD_TEST
)
2952 dhcp6_startrequest(ifp
);
2958 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
2959 if (ap
->flags
& IPV6_AF_NEW
) {
2964 logger(ifp
->ctx
, has_new
? LOG_INFO
: LOG_DEBUG
,
2965 "%s: %s received from %s", ifp
->name
, op
, ctx
->sfrom
);
2967 state
->reason
= NULL
;
2968 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
2969 switch(state
->state
) {
2972 state
->expire
= ND6_INFINITE_LIFETIME
;
2973 state
->lowpl
= ND6_INFINITE_LIFETIME
;
2974 state
->reason
= "INFORM6";
2977 if (state
->reason
== NULL
)
2978 state
->reason
= "BOUND6";
2981 if (state
->reason
== NULL
)
2982 state
->reason
= "RENEW6";
2985 if (state
->reason
== NULL
)
2986 state
->reason
= "REBIND6";
2989 if (state
->reason
== NULL
)
2990 state
->reason
= "REBOOT6";
2991 if (state
->renew
== 0) {
2992 if (state
->expire
== ND6_INFINITE_LIFETIME
)
2993 state
->renew
= ND6_INFINITE_LIFETIME
;
2994 else if (state
->lowpl
!= ND6_INFINITE_LIFETIME
)
2995 state
->renew
= (uint32_t)(state
->lowpl
* 0.5);
2997 if (state
->rebind
== 0) {
2998 if (state
->expire
== ND6_INFINITE_LIFETIME
)
2999 state
->rebind
= ND6_INFINITE_LIFETIME
;
3000 else if (state
->lowpl
!= ND6_INFINITE_LIFETIME
)
3001 state
->rebind
= (uint32_t)(state
->lowpl
* 0.8);
3005 state
->reason
= "UNKNOWN6";
3009 if (state
->state
!= DH6S_CONFIRM
) {
3011 state
->old
= state
->new;
3012 state
->old_len
= state
->new_len
;
3013 state
->new = state
->recv
;
3014 state
->new_len
= state
->recv_len
;
3016 state
->recv_len
= 0;
3019 if (ifp
->ctx
->options
& DHCPCD_TEST
)
3020 script_runreason(ifp
, "TEST");
3022 if (state
->state
== DH6S_INFORM
)
3023 state
->state
= DH6S_INFORMED
;
3025 state
->state
= DH6S_BOUND
;
3026 if (state
->renew
&& state
->renew
!= ND6_INFINITE_LIFETIME
)
3027 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3028 (time_t)state
->renew
,
3029 state
->state
== DH6S_INFORMED
?
3030 dhcp6_startinform
: dhcp6_startrenew
, ifp
);
3031 if (state
->rebind
&& state
->rebind
!= ND6_INFINITE_LIFETIME
)
3032 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3033 (time_t)state
->rebind
, dhcp6_startrebind
, ifp
);
3034 if (state
->expire
!= ND6_INFINITE_LIFETIME
)
3035 eloop_timeout_add_sec(ifp
->ctx
->eloop
,
3036 (time_t)state
->expire
, dhcp6_startexpire
, ifp
);
3038 ipv6nd_runignoredra(ifp
);
3039 ipv6_addaddrs(&state
->addrs
);
3040 dhcp6_delegate_prefix(ifp
);
3042 if (state
->state
== DH6S_INFORMED
)
3043 logger(ifp
->ctx
, has_new
? LOG_INFO
: LOG_DEBUG
,
3044 "%s: refresh in %"PRIu32
" seconds",
3045 ifp
->name
, state
->renew
);
3046 else if (state
->renew
|| state
->rebind
)
3047 logger(ifp
->ctx
, has_new
? LOG_INFO
: LOG_DEBUG
,
3048 "%s: renew in %"PRIu32
" seconds,"
3049 " rebind in %"PRIu32
" seconds",
3050 ifp
->name
, state
->renew
, state
->rebind
);
3051 else if (state
->expire
== 0)
3052 logger(ifp
->ctx
, has_new
? LOG_INFO
: LOG_DEBUG
,
3053 "%s: will expire", ifp
->name
);
3055 ipv6_buildroutes(ifp
->ctx
);
3056 dhcp6_writelease(ifp
);
3057 dhcp6_script_try_run(ifp
, 0);
3060 if (ifp
->ctx
->options
& DHCPCD_TEST
||
3061 (ifp
->options
->options
& DHCPCD_INFORM
&&
3062 !(ifp
->ctx
->options
& DHCPCD_MASTER
)))
3064 eloop_exit(ifp
->ctx
->eloop
, EXIT_SUCCESS
);
3069 dhcp6_open(struct dhcpcd_ctx
*dctx
)
3071 struct ipv6_ctx
*ctx
;
3072 struct sockaddr_in6 sa
;
3075 memset(&sa
, 0, sizeof(sa
));
3076 sa
.sin6_family
= AF_INET6
;
3077 sa
.sin6_port
= htons(DHCP6_CLIENT_PORT
);
3079 sa
.sin6_len
= sizeof(sa
);
3083 ctx
->dhcp_fd
= xsocket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
,
3084 O_NONBLOCK
|O_CLOEXEC
);
3085 if (ctx
->dhcp_fd
== -1)
3089 if (setsockopt(ctx
->dhcp_fd
, SOL_SOCKET
, SO_REUSEADDR
,
3090 &n
, sizeof(n
)) == -1)
3094 if (setsockopt(ctx
->dhcp_fd
, SOL_SOCKET
, SO_BROADCAST
,
3095 &n
, sizeof(n
)) == -1)
3100 if (setsockopt(ctx
->dhcp_fd
, SOL_SOCKET
, SO_REUSEPORT
,
3101 &n
, sizeof(n
)) == -1)
3102 logger(dctx
, LOG_WARNING
, "setsockopt: SO_REUSEPORT: %m");
3105 if (bind(ctx
->dhcp_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
3109 if (setsockopt(ctx
->dhcp_fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
3110 &n
, sizeof(n
)) == -1)
3113 eloop_event_add(dctx
->eloop
, ctx
->dhcp_fd
,
3114 dhcp6_handledata
, dctx
, NULL
, NULL
);
3118 close(ctx
->dhcp_fd
);
3124 dhcp6_start1(void *arg
)
3126 struct interface
*ifp
= arg
;
3127 struct if_options
*ifo
= ifp
->options
;
3128 struct dhcp6_state
*state
;
3130 const struct dhcp_compat
*dhc
;
3132 state
= D6_STATE(ifp
);
3133 /* If no DHCPv6 options are configured,
3134 match configured DHCPv4 options to DHCPv6 equivalents. */
3135 for (i
= 0; i
< sizeof(ifo
->requestmask6
); i
++) {
3136 if (ifo
->requestmask6
[i
] != '\0')
3139 if (i
== sizeof(ifo
->requestmask6
)) {
3140 for (dhc
= dhcp_compats
; dhc
->dhcp_opt
; dhc
++) {
3141 if (has_option_mask(ifo
->requestmask
, dhc
->dhcp_opt
))
3142 add_option_mask(ifo
->requestmask6
,
3145 if (ifo
->fqdn
!= FQDN_DISABLE
||
3146 ifo
->options
& DHCPCD_HOSTNAME
)
3147 add_option_mask(ifo
->requestmask6
, D6_OPTION_FQDN
);
3150 /* Rapid commit won't work with Prefix Delegation Exclusion */
3151 if (dhcp6_findselfsla(ifp
, NULL
))
3152 del_option_mask(ifo
->requestmask6
, D6_OPTION_RAPID_COMMIT
);
3154 if (state
->state
== DH6S_INFORM
) {
3155 add_option_mask(ifo
->requestmask6
, D6_OPTION_INFO_REFRESH_TIME
);
3156 dhcp6_startinform(ifp
);
3158 del_option_mask(ifo
->requestmask6
, D6_OPTION_INFO_REFRESH_TIME
);
3159 dhcp6_startinit(ifp
);
3164 dhcp6_start(struct interface
*ifp
, enum DH6S init_state
)
3166 struct dhcp6_state
*state
;
3168 state
= D6_STATE(ifp
);
3170 if (state
->state
== DH6S_INFORMED
&&
3171 init_state
== DH6S_INFORM
)
3173 dhcp6_startinform(ifp
);
3176 if (init_state
== DH6S_INIT
&&
3177 ifp
->options
->options
& DHCPCD_DHCP6
&&
3178 (state
->state
== DH6S_INFORM
||
3179 state
->state
== DH6S_INFORMED
||
3180 state
->state
== DH6S_DELEGATED
))
3182 /* Change from stateless to stateful */
3185 /* We're already running DHCP6 */
3186 /* XXX: What if the managed flag vanishes from all RA? */
3190 if (!(ifp
->options
->options
& DHCPCD_DHCP6
))
3193 if (ifp
->ctx
->ipv6
->dhcp_fd
== -1 && dhcp6_open(ifp
->ctx
) == -1)
3196 ifp
->if_data
[IF_DATA_DHCP6
] = calloc(1, sizeof(*state
));
3197 state
= D6_STATE(ifp
);
3201 state
->sol_max_rt
= SOL_MAX_RT
;
3202 state
->inf_max_rt
= INF_MAX_RT
;
3203 TAILQ_INIT(&state
->addrs
);
3206 state
->state
= init_state
;
3207 dhcp_set_leasefile(state
->leasefile
, sizeof(state
->leasefile
),
3209 if (ipv6_linklocal(ifp
) == NULL
) {
3210 logger(ifp
->ctx
, LOG_DEBUG
,
3211 "%s: delaying DHCPv6 soliciation for LL address",
3213 ipv6_addlinklocalcallback(ifp
, dhcp6_start1
, ifp
);
3222 dhcp6_reboot(struct interface
*ifp
)
3224 struct dhcp6_state
*state
;
3226 state
= D6_STATE(ifp
);
3228 switch (state
->state
) {
3230 dhcp6_startrebind(ifp
);
3233 dhcp6_startinform(ifp
);
3236 dhcp6_startdiscover(ifp
);
3243 dhcp6_freedrop(struct interface
*ifp
, int drop
, const char *reason
)
3245 struct dhcp6_state
*state
;
3246 struct dhcpcd_ctx
*ctx
;
3247 unsigned long long options
;
3251 * As the interface is going away from dhcpcd we need to
3252 * remove the delegated addresses, otherwise we lose track
3253 * of which interface is delegating as we remeber it by pointer.
3254 * So if we need to change this behaviour, we need to change
3255 * how we remember which interface delegated.
3257 * XXX The below is no longer true due to the change of the
3258 * default IAID, but do PPP links have stable ethernet
3261 * To make it more interesting, on some OS's with PPP links
3262 * there is no guarantee the delegating interface will have
3263 * the same name or index so think very hard before changing
3267 options
= ifp
->options
->options
;
3270 dropdele
= (options
& (DHCPCD_STOPPING
| DHCPCD_RELEASE
) &&
3271 (options
& DHCPCD_NODROP
) != DHCPCD_NODROP
);
3273 if (ifp
->ctx
->eloop
)
3274 eloop_timeout_delete(ifp
->ctx
->eloop
, NULL
, ifp
);
3277 dhcp6_delete_delegates(ifp
);
3279 state
= D6_STATE(ifp
);
3281 /* Failure to send the release may cause this function to
3283 if (state
->state
== DH6S_RELEASE
) {
3284 dhcp6_finishrelease(ifp
);
3288 if (drop
&& options
& DHCPCD_RELEASE
) {
3289 if (ifp
->carrier
== LINK_UP
&&
3290 state
->state
!= DH6S_RELEASED
)
3292 dhcp6_startrelease(ifp
);
3295 unlink(state
->leasefile
);
3297 dhcp6_freedrop_addrs(ifp
, drop
, NULL
);
3299 state
->old
= state
->new;
3300 state
->old_len
= state
->new_len
;
3303 if (drop
&& state
->old
&&
3304 (options
& DHCPCD_NODROP
) != DHCPCD_NODROP
)
3308 script_runreason(ifp
, reason
);
3314 ifp
->if_data
[IF_DATA_DHCP6
] = NULL
;
3317 /* If we don't have any more DHCP6 enabled interfaces,
3318 * close the global socket and release resources */
3321 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
3326 if (ifp
== NULL
&& ctx
->ipv6
) {
3327 if (ctx
->ipv6
->dhcp_fd
!= -1) {
3328 eloop_event_delete(ctx
->eloop
, ctx
->ipv6
->dhcp_fd
);
3329 close(ctx
->ipv6
->dhcp_fd
);
3330 ctx
->ipv6
->dhcp_fd
= -1;
3336 dhcp6_drop(struct interface
*ifp
, const char *reason
)
3339 dhcp6_freedrop(ifp
, 1, reason
);
3343 dhcp6_free(struct interface
*ifp
)
3346 dhcp6_freedrop(ifp
, 0, NULL
);
3350 dhcp6_handleifa(struct dhcpcd_ctx
*ctx
, int cmd
, const char *ifname
,
3351 const struct in6_addr
*addr
, int flags
)
3353 struct interface
*ifp
;
3354 struct dhcp6_state
*state
;
3356 if (ctx
->ifaces
== NULL
)
3359 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
3360 state
= D6_STATE(ifp
);
3361 if (state
== NULL
|| strcmp(ifp
->name
, ifname
))
3363 ipv6_handleifa_addrs(cmd
, &state
->addrs
, addr
, flags
);
3369 dhcp6_env(char **env
, const char *prefix
, const struct interface
*ifp
,
3370 const struct dhcp6_message
*m
, size_t len
)
3372 const struct if_options
*ifo
;
3373 struct dhcp_opt
*opt
, *vo
;
3374 const struct dhcp6_option
*o
;
3379 const struct dhcpcd_ctx
*ctx
;
3380 const struct dhcp6_state
*state
;
3381 const struct ipv6_addr
*ap
;
3388 if (len
< sizeof(*m
)) {
3389 /* Should be impossible with guards at packet in
3390 * and reading leases */
3398 /* Zero our indexes */
3400 for (i
= 0, opt
= ctx
->dhcp6_opts
;
3401 i
< ctx
->dhcp6_opts_len
;
3403 dhcp_zero_index(opt
);
3404 for (i
= 0, opt
= ifp
->options
->dhcp6_override
;
3405 i
< ifp
->options
->dhcp6_override_len
;
3407 dhcp_zero_index(opt
);
3408 for (i
= 0, opt
= ctx
->vivso
;
3411 dhcp_zero_index(opt
);
3412 i
= strlen(prefix
) + strlen("_dhcp6") + 1;
3415 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
3418 snprintf(pfx
, i
, "%s_dhcp6", prefix
);
3422 /* Unlike DHCP, DHCPv6 options *may* occur more than once.
3423 * There is also no provision for option concatenation unlike DHCP. */
3424 for (o
= D6_CFIRST_OPTION(m
);
3425 len
> (ssize_t
)sizeof(*o
);
3426 o
= D6_CNEXT_OPTION(o
))
3429 if (sizeof(*o
) + ol
> len
) {
3433 len
-= sizeof(*o
) + ol
;
3434 oc
= ntohs(o
->code
);
3435 if (has_option_mask(ifo
->nomask6
, oc
))
3437 for (i
= 0, opt
= ifo
->dhcp6_override
;
3438 i
< ifo
->dhcp6_override_len
;
3440 if (opt
->option
== oc
)
3442 if (i
== ifo
->dhcp6_override_len
&&
3443 oc
== D6_OPTION_VENDOR_OPTS
&&
3446 memcpy(&en
, D6_COPTION_DATA(o
), sizeof(en
));
3448 vo
= vivso_find(en
, ifp
);
3451 if (i
== ifo
->dhcp6_override_len
) {
3452 for (i
= 0, opt
= ctx
->dhcp6_opts
;
3453 i
< ctx
->dhcp6_opts_len
;
3455 if (opt
->option
== oc
)
3457 if (i
== ctx
->dhcp6_opts_len
)
3461 n
+= dhcp_envoption(ifp
->ctx
,
3462 env
== NULL
? NULL
: &env
[n
],
3464 opt
, dhcp6_getoption
, D6_COPTION_DATA(o
), ol
);
3467 n
+= dhcp_envoption(ifp
->ctx
,
3468 env
== NULL
? NULL
: &env
[n
],
3470 vo
, dhcp6_getoption
,
3471 D6_COPTION_DATA(o
) + sizeof(en
),
3478 /* Needed for Delegated Prefixes */
3479 state
= D6_CSTATE(ifp
);
3481 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
3482 if (ap
->delegating_iface
) {
3483 i
+= strlen(ap
->saddr
) + 1;
3487 i
+= strlen(prefix
) + strlen("_delegated_dhcp6_prefix=");
3488 v
= val
= env
[n
] = malloc(i
);
3490 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
3493 v
+= snprintf(val
, i
, "%s_delegated_dhcp6_prefix=", prefix
);
3494 TAILQ_FOREACH(ap
, &state
->addrs
, next
) {
3495 if (ap
->delegating_iface
) {
3496 /* Can't use stpcpy(3) due to "security" */
3497 const char *sap
= ap
->saddr
;
3501 while (*++sap
!= '\0');
3514 dhcp6_dump(struct interface
*ifp
)
3516 struct dhcp6_state
*state
;
3518 ifp
->if_data
[IF_DATA_DHCP6
] = state
= calloc(1, sizeof(*state
));
3519 if (state
== NULL
) {
3520 logger(ifp
->ctx
, LOG_ERR
, "%s: %m", __func__
);
3523 TAILQ_INIT(&state
->addrs
);
3524 dhcp_set_leasefile(state
->leasefile
, sizeof(state
->leasefile
),
3526 if (dhcp6_readlease(ifp
, 0) == -1) {
3527 logger(ifp
->ctx
, LOG_ERR
, "%s: %s: %m",
3528 *ifp
->name
? ifp
->name
: state
->leasefile
, __func__
);
3531 state
->reason
= "DUMP6";
3532 return script_runreason(ifp
, state
->reason
);