1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <arpa/inet.h>
36 #include <net/route.h>
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
42 #ifdef HAVE_SYS_BITOPS_H
43 #include <sys/bitops.h>
45 #include "compat/bitops.h"
49 /* Purely for the ND6_IFF_AUTO_LINKLOCAL #define which is solely used
50 * to generate our CAN_ADD_LLADDR #define. */
51 # include <netinet6/in6_var.h>
52 # include <netinet6/nd6.h>
84 #ifndef SHA256_DIGEST_LENGTH
85 # define SHA256_DIGEST_LENGTH 32
88 #ifdef IPV6_POLLADDRFLAG
89 # warning kernel does not report IPv6 address flag changes
90 # warning polling tentative address flags periodically
93 /* Hackery at it's finest. */
96 # define s6_addr32 _S6_un._S6_u32
98 # define s6_addr32 __u6_addr.__u6_addr32
102 #if defined(HAVE_IN6_ADDR_GEN_MODE_NONE) || defined(ND6_IFF_AUTO_LINKLOCAL) || \
103 defined(IFF_NOLINKLOCAL)
104 /* Only add the LL address if we have a carrier, so DaD works. */
105 #define CAN_ADD_LLADDR(ifp) \
106 (!((ifp)->options->options & DHCPCD_LINK) || (ifp)->carrier != LINK_DOWN)
108 /* Although we can add our own LL address, we cannot drop it
109 * without unplumbing the if which is a lot of code.
110 * So just keep it for the time being. */
111 #define CAN_DROP_LLADDR(ifp) (0)
113 #define CAN_DROP_LLADDR(ifp) (1)
116 /* We have no control over the OS adding the LLADDR, so just let it do it
117 * as we cannot force our own view on it. */
118 #define CAN_ADD_LLADDR(ifp) (0)
119 #define CAN_DROP_LLADDR(ifp) (0)
122 #ifdef IPV6_MANAGETEMPADDR
123 static void ipv6_regentempifid(void *);
124 static void ipv6_regentempaddr(void *);
126 #define ipv6_regentempifid(a) {}
130 ipv6_init(struct dhcpcd_ctx *ctx)
133 if (ctx->ra_routers != NULL)
136 ctx->ra_routers = malloc(sizeof(*ctx->ra_routers));
137 if (ctx->ra_routers == NULL)
139 TAILQ_INIT(ctx->ra_routers);
149 ipv6_readsecret(struct dhcpcd_ctx *ctx)
158 if ((ctx->secret_len = read_hwaddr_aton(&ctx->secret, SECRET)) != 0)
159 return (ssize_t)ctx->secret_len;
162 logerr("%s: %s", __func__, SECRET);
164 /* Chaining arc4random should be good enough.
165 * RFC7217 section 5.1 states the key SHOULD be at least 128 bits.
166 * To attempt and future proof ourselves, we'll generate a key of
167 * 512 bits (64 bytes). */
168 if (ctx->secret_len < 64) {
169 if ((ctx->secret = malloc(64)) == NULL) {
173 ctx->secret_len = 64;
176 for (len = 0; len < 512 / NBBY; len += sizeof(r)) {
178 memcpy(p, &r, sizeof(r));
182 /* Ensure that only the dhcpcd user can read the secret.
183 * Write permission is also denied as chaning it would remove
185 if ((fp = fopen(SECRET, "w")) == NULL ||
186 chmod(SECRET, S_IRUSR) == -1)
188 x = fprintf(fp, "%s\n",
189 hwaddr_ntoa(ctx->secret, ctx->secret_len, line, sizeof(line)));
190 if (fclose(fp) == EOF)
194 return (ssize_t)ctx->secret_len;
197 logerr("%s: %s", __func__, SECRET);
205 /* http://www.iana.org/assignments/ipv6-interface-ids/ipv6-interface-ids.xhtml
207 static const struct reslowhigh {
208 const uint8_t high[8];
209 const uint8_t low[8];
211 /* RFC4291 + RFC6543 */
212 { { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0x00, 0x00, 0x00 },
213 { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0xff, 0xff, 0xff } },
215 { { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 },
216 { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }
220 ipv6_reserved(const struct in6_addr *addr)
222 uint64_t id, low, high;
224 const struct reslowhigh *r;
226 id = be64dec(addr->s6_addr + sizeof(id));
227 if (id == 0) /* RFC4291 */
229 for (i = 0; i < sizeof(reslowhigh) / sizeof(reslowhigh[0]); i++) {
231 low = be64dec(r->low);
232 high = be64dec(r->high);
233 if (id >= low && id <= high)
241 ipv6_makestableprivate1(struct in6_addr *addr,
242 const struct in6_addr *prefix, int prefix_len,
243 const unsigned char *netiface, size_t netiface_len,
244 const unsigned char *netid, size_t netid_len,
245 unsigned short vlanid,
246 uint32_t *dad_counter,
247 const unsigned char *secret, size_t secret_len)
249 unsigned char buf[2048], *p, digest[SHA256_DIGEST_LENGTH];
253 if (prefix_len < 0 || prefix_len > 120) {
258 l = (size_t)(ROUNDUP8(prefix_len) / NBBY);
259 len = l + netiface_len + netid_len + sizeof(*dad_counter) + secret_len;
261 len += sizeof(vlanid);
262 if (len > sizeof(buf)) {
267 for (;; (*dad_counter)++) {
268 /* Combine all parameters into one buffer */
270 memcpy(p, prefix, l);
272 memcpy(p, netiface, netiface_len);
274 memcpy(p, netid, netid_len);
276 /* Don't use a vlanid if not set.
277 * This ensures prior versions have the same unique address. */
279 memcpy(p, &vlanid, sizeof(vlanid));
282 memcpy(p, dad_counter, sizeof(*dad_counter));
283 p += sizeof(*dad_counter);
284 memcpy(p, secret, secret_len);
286 /* Make an address using the digest of the above.
287 * RFC7217 Section 5.1 states that we shouldn't use MD5.
288 * Pity as we use that for HMAC-MD5 which is still deemed OK.
289 * SHA-256 is recommended */
291 SHA256_Update(&ctx, buf, len);
292 SHA256_Final(digest, &ctx);
295 memcpy(p, prefix, l);
296 /* RFC7217 section 5.2 says we need to start taking the id from
297 * the least significant bit */
298 len = sizeof(addr->s6_addr) - l;
299 memcpy(p + l, digest + (sizeof(digest) - len), len);
301 /* Ensure that the Interface ID does not match a reserved one,
302 * if it does then treat it as a DAD failure.
303 * RFC7217 section 5.2 */
304 if (prefix_len != 64)
306 if (!ipv6_reserved(addr))
314 ipv6_makestableprivate(struct in6_addr *addr,
315 const struct in6_addr *prefix, int prefix_len,
316 const struct interface *ifp,
322 if (ifp->ctx->secret_len == 0) {
323 if (ipv6_readsecret(ifp->ctx) == -1)
327 dad = (uint32_t)*dad_counter;
329 /* For our implementation, we shall set the hardware address
330 * as the interface identifier */
331 r = ipv6_makestableprivate1(addr, prefix, prefix_len,
332 ifp->hwaddr, ifp->hwlen,
333 ifp->ssid, ifp->ssid_len,
335 ifp->ctx->secret, ifp->ctx->secret_len);
338 *dad_counter = (int)dad;
343 ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp,
344 const struct in6_addr *prefix, int prefix_len)
346 const struct ipv6_addr *ap;
349 if (prefix_len < 0 || prefix_len > 120) {
354 if (ifp->options->options & DHCPCD_SLAACPRIVATE) {
356 if (ipv6_makestableprivate(addr,
357 prefix, prefix_len, ifp, &dad) == -1)
362 if (prefix_len > 64) {
366 if ((ap = ipv6_linklocal(ifp)) == NULL) {
367 /* We delay a few functions until we get a local-link address
368 * so this should never be hit. */
373 /* Make the address from the first local-link address */
374 memcpy(addr, prefix, sizeof(*prefix));
375 addr->s6_addr32[2] = ap->addr.s6_addr32[2];
376 addr->s6_addr32[3] = ap->addr.s6_addr32[3];
381 ipv6_makeprefix(struct in6_addr *prefix, const struct in6_addr *addr, int len)
385 if (len < 0 || len > 128) {
392 memcpy(&prefix->s6_addr, &addr->s6_addr, (size_t)bytes);
394 /* Coverify false positive.
395 * bytelen cannot be 16 if bitlen is non zero */
396 /* coverity[overrun-local] */
397 prefix->s6_addr[bytes] =
398 (uint8_t)(prefix->s6_addr[bytes] >> (NBBY - bits));
400 memset((char *)prefix->s6_addr + bytes, 0,
401 sizeof(prefix->s6_addr) - (size_t)bytes);
406 ipv6_mask(struct in6_addr *mask, int len)
408 static const unsigned char masks[NBBY] =
409 { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
412 if (len < 0 || len > 128) {
417 memset(mask, 0, sizeof(*mask));
420 for (i = 0; i < bytes; i++)
421 mask->s6_addr[i] = 0xff;
423 /* Coverify false positive.
424 * bytelen cannot be 16 if bitlen is non zero */
425 /* coverity[overrun-local] */
426 mask->s6_addr[bytes] = masks[bits - 1];
432 ipv6_prefixlen(const struct in6_addr *mask)
435 const unsigned char *lim, *p;
437 lim = (const unsigned char *)mask + sizeof(*mask);
438 for (p = (const unsigned char *)mask; p < lim; x++, p++) {
444 for (y = 0; y < NBBY; y++) {
445 if ((*p & (0x80 >> y)) == 0)
451 * when the limit pointer is given, do a stricter check on the
455 if (y != 0 && (*p & (0x00ff >> y)) != 0)
457 for (p = p + 1; p < lim; p++)
462 return (uint8_t)(x * NBBY + y);
466 in6_to_h64(uint64_t *vhigh, uint64_t *vlow, const struct in6_addr *addr)
469 *vhigh = be64dec(addr->s6_addr);
470 *vlow = be64dec(addr->s6_addr + 8);
474 h64_to_in6(struct in6_addr *addr, uint64_t vhigh, uint64_t vlow)
477 be64enc(addr->s6_addr, vhigh);
478 be64enc(addr->s6_addr + 8, vlow);
483 const struct in6_addr *prefix, // prefix from router
484 short prefix_len, // length of prefix received
485 uint64_t user_number, // "random" number from user
486 struct in6_addr *result, // resultant prefix
487 short result_len) // desired prefix length
489 uint64_t vh, vl, user_low, user_high;
491 if (prefix_len < 1 || prefix_len > 128 ||
492 result_len < 1 || result_len > 128)
498 /* Check that the user_number fits inside result_len less prefix_len */
499 if (result_len < prefix_len ||
500 fls64(user_number) > result_len - prefix_len)
506 /* If user_number is zero, just copy the prefix into the result. */
507 if (user_number == 0) {
512 /* Shift user_number so it fit's just inside result_len.
513 * Shifting by 0 or sizeof(user_number) is undefined,
514 * so we cater for that. */
515 if (result_len == 128) {
517 user_low = user_number;
518 } else if (result_len > 64) {
519 if (prefix_len >= 64)
522 user_high = user_number >> (result_len - prefix_len);
523 user_low = user_number << (128 - result_len);
524 } else if (result_len == 64) {
525 user_high = user_number;
528 user_high = user_number << (64 - result_len);
532 /* convert to two 64bit host order values */
533 in6_to_h64(&vh, &vl, prefix);
538 /* copy back result */
539 h64_to_in6(result, vh, vl);
544 #ifdef IPV6_POLLADDRFLAG
546 ipv6_checkaddrflags(void *arg)
548 struct ipv6_addr *ia;
558 if ((flags = if_addrflags6(ia->iface, &ia->addr, alias)) == -1) {
559 if (errno != EEXIST && errno != EADDRNOTAVAIL)
560 logerr("%s: if_addrflags6", __func__);
564 if (!(flags & IN6_IFF_TENTATIVE)) {
565 /* Simulate the kernel announcing the new address. */
566 ipv6_handleifa(ia->iface->ctx, RTM_NEWADDR,
567 ia->iface->ctx->ifaces, ia->iface->name,
568 &ia->addr, ia->prefix_len, flags, 0);
570 /* Still tentative? Check again in a bit. */
573 ms_to_ts(&tv, RETRANS_TIMER / 2);
574 eloop_timeout_add_tv(ia->iface->ctx->eloop, &tv,
575 ipv6_checkaddrflags, ia);
581 ipv6_deletedaddr(struct ipv6_addr *ia)
587 /* NOREJECT is set if we delegated exactly the prefix to another
589 * This can only be one address, so just clear the flag.
590 * This should ensure the reject route will be restored. */
591 if (ia->delegating_prefix != NULL)
592 ia->delegating_prefix->flags &= ~IPV6_AF_NOREJECT;
597 ipv6_deleteaddr(struct ipv6_addr *ia)
599 struct ipv6_state *state;
600 struct ipv6_addr *ap;
602 loginfox("%s: deleting address %s", ia->iface->name, ia->saddr);
603 if (if_address6(RTM_DELADDR, ia) == -1 &&
604 errno != EADDRNOTAVAIL && errno != ESRCH &&
605 errno != ENXIO && errno != ENODEV)
608 ipv6_deletedaddr(ia);
610 state = IPV6_STATE(ia->iface);
611 TAILQ_FOREACH(ap, &state->addrs, next) {
612 if (IN6_ARE_ADDR_EQUAL(&ap->addr, &ia->addr)) {
613 TAILQ_REMOVE(&state->addrs, ap, next);
620 /* Advertise the address if it exists on another interface. */
621 ipv6nd_advertise(ia);
626 ipv6_addaddr1(struct ipv6_addr *ia, const struct timespec *now)
628 struct interface *ifp;
629 uint32_t pltime, vltime;
630 __printflike(1, 2) void (*logfunc)(const char *, ...);
632 bool vltime_was_zero = ia->prefix_vltime == 0;
635 struct ipv6_state *state;
636 struct ipv6_addr *ia2;
638 /* If we re-add then address on Solaris then the prefix
639 * route will be scrubbed and re-added. Something might
640 * be using it, so let's avoid it. */
641 if (ia->flags & IPV6_AF_DADCOMPLETED) {
642 logdebugx("%s: IP address %s already exists",
643 ia->iface->name, ia->saddr);
652 /* Remember the interface of the address. */
655 if (!(ia->flags & IPV6_AF_DADCOMPLETED) &&
656 ipv6_iffindaddr(ifp, &ia->addr, IN6_IFF_NOTUSEABLE))
657 ia->flags |= IPV6_AF_DADCOMPLETED;
659 /* Adjust plftime and vltime based on acquired time */
660 pltime = ia->prefix_pltime;
661 vltime = ia->prefix_vltime;
662 if (timespecisset(&ia->acquired) &&
663 (ia->prefix_pltime != ND6_INFINITE_LIFETIME ||
664 ia->prefix_vltime != ND6_INFINITE_LIFETIME))
669 clock_gettime(CLOCK_MONOTONIC, &n);
672 timespecsub(now, &ia->acquired, &n);
673 if (ia->prefix_pltime != ND6_INFINITE_LIFETIME) {
674 ia->prefix_pltime -= (uint32_t)n.tv_sec;
675 /* This can happen when confirming a
676 * deprecated but still valid lease. */
677 if (ia->prefix_pltime > pltime)
678 ia->prefix_pltime = 0;
680 if (ia->prefix_vltime != ND6_INFINITE_LIFETIME) {
681 ia->prefix_vltime -= (uint32_t)n.tv_sec;
682 /* This should never happen. */
683 if (ia->prefix_vltime > vltime) {
684 logerrx("%s: %s: lifetime overflow",
685 ifp->name, ia->saddr);
686 ia->prefix_vltime = ia->prefix_pltime = 0;
691 logfunc = ia->flags & IPV6_AF_NEW ? loginfox : logdebugx;
692 logfunc("%s: adding %saddress %s", ifp->name,
693 #ifdef IPV6_AF_TEMPORARY
694 ia->flags & IPV6_AF_TEMPORARY ? "temporary " : "",
699 if (ia->prefix_pltime == ND6_INFINITE_LIFETIME &&
700 ia->prefix_vltime == ND6_INFINITE_LIFETIME)
701 logdebugx("%s: pltime infinity, vltime infinity",
703 else if (ia->prefix_pltime == ND6_INFINITE_LIFETIME)
704 logdebugx("%s: pltime infinity, vltime %"PRIu32" seconds",
705 ifp->name, ia->prefix_vltime);
706 else if (ia->prefix_vltime == ND6_INFINITE_LIFETIME)
707 logdebugx("%s: pltime %"PRIu32"seconds, vltime infinity",
708 ifp->name, ia->prefix_pltime);
710 logdebugx("%s: pltime %"PRIu32" seconds, vltime %"PRIu32
712 ifp->name, ia->prefix_pltime, ia->prefix_vltime);
714 if (if_address6(RTM_NEWADDR, ia) == -1) {
716 /* Restore real pltime and vltime */
717 ia->prefix_pltime = pltime;
718 ia->prefix_vltime = vltime;
722 #ifdef IPV6_MANAGETEMPADDR
723 /* RFC4941 Section 3.4 */
724 if (ia->flags & IPV6_AF_TEMPORARY &&
727 ip6_use_tempaddr(ifp->name))
728 eloop_timeout_add_sec(ifp->ctx->eloop,
729 (time_t)ia->prefix_pltime - REGEN_ADVANCE,
730 ipv6_regentempaddr, ia);
733 /* Restore real pltime and vltime */
734 ia->prefix_pltime = pltime;
735 ia->prefix_vltime = vltime;
737 ia->flags &= ~IPV6_AF_NEW;
738 ia->flags |= IPV6_AF_ADDED;
740 if (ia->delegating_prefix != NULL)
741 ia->flags |= IPV6_AF_DELEGATED;
744 #ifdef IPV6_POLLADDRFLAG
745 eloop_timeout_delete(ifp->ctx->eloop,
746 ipv6_checkaddrflags, ia);
747 if (!(ia->flags & IPV6_AF_DADCOMPLETED)) {
750 ms_to_ts(&tv, RETRANS_TIMER / 2);
751 eloop_timeout_add_tv(ifp->ctx->eloop,
752 &tv, ipv6_checkaddrflags, ia);
757 /* Solaris does not announce new addresses which need DaD
758 * so we need to take a copy and add it to our list.
759 * Otherwise aliasing gets confused if we add another
760 * address during DaD. */
762 state = IPV6_STATE(ifp);
763 TAILQ_FOREACH(ia2, &state->addrs, next) {
764 if (IN6_ARE_ADDR_EQUAL(&ia2->addr, &ia->addr))
768 if ((ia2 = malloc(sizeof(*ia2))) == NULL) {
770 return 0; /* Well, we did add the address */
772 memcpy(ia2, ia, sizeof(*ia2));
773 TAILQ_INSERT_TAIL(&state->addrs, ia2, next);
781 /* Re-advertise the preferred address to be safe. */
782 if (!vltime_was_zero)
783 ipv6nd_advertise(ia);
790 /* Find the next logical alias address we can use. */
792 ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl)
794 struct ipv6_state *state;
795 struct ipv6_addr *iap;
797 char alias[IF_NAMESIZE];
799 if (ia->alias[0] != '\0')
801 state = IPV6_STATE(ia->iface);
803 /* First find an existng address.
804 * This can happen when dhcpcd restarts as ND and DHCPv6
805 * maintain their own lists of addresses. */
806 TAILQ_FOREACH(iap, &state->addrs, next) {
807 if (iap->alias[0] != '\0' &&
808 IN6_ARE_ADDR_EQUAL(&iap->addr, &ia->addr))
810 strlcpy(ia->alias, iap->alias, sizeof(ia->alias));
817 if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >=
823 TAILQ_FOREACH(iap, &state->addrs, next) {
824 if (iap->alias[0] == '\0')
826 if (IN6_IS_ADDR_UNSPECIFIED(&iap->addr)) {
827 /* No address assigned? Lets use it. */
828 strlcpy(ia->alias, iap->alias, sizeof(ia->alias));
833 if (strcmp(iap->alias, alias) == 0)
838 if (lun == UINT_MAX) {
846 strlcpy(ia->alias, alias, sizeof(ia->alias));
852 ipv6_addaddr(struct ipv6_addr *ia, const struct timespec *now)
857 struct ipv6_addr *replaced_ia;
859 blank = (ia->alias[0] == '\0');
860 if ((replaced = ipv6_aliasaddr(ia, &replaced_ia)) == -1)
863 logdebugx("%s: aliased %s", ia->alias, ia->saddr);
866 if ((r = ipv6_addaddr1(ia, now)) == 0) {
869 struct ipv6_state *state;
871 state = IPV6_STATE(ia->iface);
872 TAILQ_REMOVE(&state->addrs, replaced_ia, next);
873 ipv6_freeaddr(replaced_ia);
881 ipv6_findaddrmatch(const struct ipv6_addr *addr, const struct in6_addr *match,
887 (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)) ==
888 (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED))
890 } else if (addr->prefix_vltime &&
891 IN6_ARE_ADDR_EQUAL(&addr->addr, match) &&
892 (!flags || addr->flags & flags))
899 ipv6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, unsigned int flags)
901 struct ipv6_addr *nap;
903 struct ipv6_addr *dap;
906 nap = ipv6nd_findaddr(ctx, addr, flags);
908 dap = dhcp6_findaddr(ctx, addr, flags);
915 if (nap->iface->metric < dap->iface->metric)
924 ipv6_addaddrs(struct ipv6_addrhead *addrs)
926 struct ipv6_addr *ap, *apn;
932 TAILQ_FOREACH_SAFE(ap, addrs, next, apn) {
933 /* A delegated prefix is not an address. */
934 if (ap->flags & IPV6_AF_DELEGATEDPFX)
936 if (ap->prefix_vltime == 0) {
937 if (ap->flags & IPV6_AF_ADDED) {
941 eloop_q_timeout_delete(ap->iface->ctx->eloop,
943 if (ap->flags & IPV6_AF_REQUEST) {
944 ap->flags &= ~IPV6_AF_ADDED;
946 TAILQ_REMOVE(addrs, ap, next);
949 } else if (!(ap->flags & IPV6_AF_STALE) &&
950 !IN6_IS_ADDR_UNSPECIFIED(&ap->addr))
952 if (ap->flags & IPV6_AF_NEW)
954 if (!timespecisset(&now))
955 clock_gettime(CLOCK_MONOTONIC, &now);
956 ipv6_addaddr(ap, &now);
964 ipv6_freeaddr(struct ipv6_addr *ia)
967 struct ipv6_addr *iad;
969 /* Forget the reference */
970 if (ia->flags & IPV6_AF_DELEGATEDPFX) {
971 TAILQ_FOREACH(iad, &ia->pd_pfxs, pd_next) {
972 iad->delegating_prefix = NULL;
974 } else if (ia->delegating_prefix != NULL) {
975 TAILQ_REMOVE(&ia->delegating_prefix->pd_pfxs, ia, pd_next);
979 if (ia->dhcp6_fd != -1) {
981 eloop_event_delete(ia->iface->ctx->eloop, ia->dhcp6_fd);
984 eloop_q_timeout_delete(ia->iface->ctx->eloop, 0, NULL, ia);
990 ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop,
991 const struct interface *ifd)
993 struct ipv6_addr *ap, *apn, *apf;
1000 TAILQ_FOREACH_SAFE(ap, addrs, next, apn) {
1003 (ap->delegating_prefix == NULL ||
1004 ap->delegating_prefix->iface != ifd))
1008 TAILQ_REMOVE(addrs, ap, next);
1009 if (drop && ap->flags & IPV6_AF_ADDED &&
1010 (ap->iface->options->options &
1011 (DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
1012 (DHCPCD_EXITING | DHCPCD_PERSISTENT))
1014 /* Don't drop link-local addresses. */
1015 if (!IN6_IS_ADDR_LINKLOCAL(&ap->addr) ||
1016 CAN_DROP_LLADDR(ap->iface))
1019 TAILQ_REMOVE(addrs, ap, next);
1020 /* Find the same address somewhere else */
1021 apf = ipv6_findaddr(ap->iface->ctx, &ap->addr,
1024 (apf->iface != ap->iface)))
1025 ipv6_deleteaddr(ap);
1026 if (!(ap->iface->options->options &
1027 DHCPCD_EXITING) && apf)
1029 if (!timespecisset(&now))
1030 clock_gettime(CLOCK_MONOTONIC,
1032 ipv6_addaddr(apf, &now);
1043 static struct ipv6_state *
1044 ipv6_getstate(struct interface *ifp)
1046 struct ipv6_state *state;
1048 state = IPV6_STATE(ifp);
1049 if (state == NULL) {
1050 ifp->if_data[IF_DATA_IPV6] = calloc(1, sizeof(*state));
1051 state = IPV6_STATE(ifp);
1052 if (state == NULL) {
1056 TAILQ_INIT(&state->addrs);
1057 TAILQ_INIT(&state->ll_callbacks);
1059 /* Regenerate new ids */
1061 ip6_use_tempaddr(ifp->name))
1062 ipv6_regentempifid(ifp);
1068 ipv6_anyglobal(struct interface *sifp)
1070 struct interface *ifp;
1071 struct ipv6_state *state;
1072 struct ipv6_addr *ia;
1074 TAILQ_FOREACH(ifp, sifp->ctx->ifaces, next) {
1075 if (ifp != sifp && ip6_forwarding(ifp->name) != 1)
1078 state = IPV6_STATE(ifp);
1082 TAILQ_FOREACH(ia, &state->addrs, next) {
1083 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr))
1085 /* Let's be optimistic.
1086 * Any decent OS won't forward or accept traffic
1087 * from/to tentative or detached addresses. */
1088 if (!(ia->addr_flags & IN6_IFF_DUPLICATED))
1096 ipv6_handleifa(struct dhcpcd_ctx *ctx,
1097 int cmd, struct if_head *ifs, const char *ifname,
1098 const struct in6_addr *addr, uint8_t prefix_len, int addrflags, pid_t pid)
1100 struct interface *ifp;
1101 struct ipv6_state *state;
1102 struct ipv6_addr *ia;
1103 struct ll_callback *cb;
1107 struct sockaddr_in6 subnet;
1109 /* Solaris on-link route is an unspecified address! */
1110 if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
1111 if (if_getsubnet(ctx, ifname, AF_INET6,
1112 &subnet, sizeof(subnet)) == -1)
1117 addr = &subnet.sin6_addr;
1122 char dbuf[INET6_ADDRSTRLEN];
1125 dbp = inet_ntop(AF_INET6, &addr->s6_addr,
1126 dbuf, INET6_ADDRSTRLEN);
1127 loginfox("%s: cmd %d addr %s addrflags %d",
1128 ifname, cmd, dbp, addrflags);
1135 if ((ifp = if_find(ifs, ifname)) == NULL)
1137 if ((state = ipv6_getstate(ifp)) == NULL)
1139 anyglobal = ipv6_anyglobal(ifp) != NULL;
1141 TAILQ_FOREACH(ia, &state->addrs, next) {
1142 if (IN6_ARE_ADDR_EQUAL(&ia->addr, addr))
1149 TAILQ_REMOVE(&state->addrs, ia, next);
1150 #ifdef ND6_ADVERTISE
1151 /* Advertise the address if it exists on
1152 * another interface. */
1153 ipv6nd_advertise(ia);
1155 /* We'll free it at the end of the function. */
1160 ia = ipv6_newaddr(ifp, addr, prefix_len, 0);
1162 strlcpy(ia->alias, ifname, sizeof(ia->alias));
1164 if (if_getlifetime6(ia) == -1) {
1165 /* No support or address vanished.
1166 * Either way, just set a deprecated
1167 * infinite time lifetime and continue.
1168 * This is fine because we only want
1169 * to know this when trying to extend
1170 * temporary addresses.
1171 * As we can't extend infinite, we'll
1172 * create a new temporary address. */
1173 ia->prefix_pltime = 0;
1175 ND6_INFINITE_LIFETIME;
1177 /* This is a minor regression against RFC 4941
1178 * because the kernel only knows when the
1179 * lifetimes were last updated, not when the
1180 * address was initially created.
1181 * Provided dhcpcd is not restarted, this
1182 * won't be a problem.
1183 * If we don't like it, we can always
1184 * pretend lifetimes are infinite and always
1185 * generate a new temporary address on
1187 ia->acquired = ia->created;
1188 TAILQ_INSERT_TAIL(&state->addrs, ia, next);
1190 ia->addr_flags = addrflags;
1191 ia->flags &= ~IPV6_AF_STALE;
1192 #ifdef IPV6_MANAGETEMPADDR
1193 if (ia->addr_flags & IN6_IFF_TEMPORARY)
1194 ia->flags |= IPV6_AF_TEMPORARY;
1196 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr) || ia->dadcallback) {
1197 #ifdef IPV6_POLLADDRFLAG
1198 if (ia->addr_flags & IN6_IFF_TENTATIVE) {
1201 ms_to_ts(&tv, RETRANS_TIMER / 2);
1202 eloop_timeout_add_tv(
1203 ia->iface->ctx->eloop,
1204 &tv, ipv6_checkaddrflags, ia);
1209 if (ia->dadcallback) {
1210 ia->dadcallback(ia);
1211 if (ctx->options & DHCPCD_FORKED)
1215 if (IN6_IS_ADDR_LINKLOCAL(&ia->addr) &&
1216 !(ia->addr_flags & IN6_IFF_NOTUSEABLE))
1218 /* Now run any callbacks.
1219 * Typically IPv6RS or DHCPv6 */
1221 TAILQ_FIRST(&state->ll_callbacks)))
1224 &state->ll_callbacks,
1226 cb->callback(cb->arg);
1228 if (ctx->options & DHCPCD_FORKED)
1239 ctx->options &= ~DHCPCD_RTBUILD;
1240 ipv6nd_handleifa(cmd, ia, pid);
1242 dhcp6_handleifa(cmd, ia, pid);
1246 /* Done with the ia now, so free it. */
1247 if (cmd == RTM_DELADDR)
1249 else if (!(ia->addr_flags & IN6_IFF_NOTUSEABLE))
1250 ia->flags |= IPV6_AF_DADCOMPLETED;
1252 /* If we've not already called rt_build via the IPv6ND
1253 * or DHCP6 handlers and the existance of any useable
1254 * global address on the interface has changed,
1255 * call rt_build to add/remove the default route. */
1256 if (ifp->active && ifp->options->options & DHCPCD_IPV6 &&
1257 !(ctx->options & DHCPCD_RTBUILD) &&
1258 (ipv6_anyglobal(ifp) != NULL) != anyglobal)
1259 rt_build(ctx, AF_INET6);
1263 ipv6_hasaddr(const struct interface *ifp)
1266 if (ipv6nd_iffindaddr(ifp, NULL, 0) != NULL)
1269 if (dhcp6_iffindaddr(ifp, NULL, 0) != NULL)
1276 ipv6_iffindaddr(struct interface *ifp, const struct in6_addr *addr,
1279 struct ipv6_state *state;
1280 struct ipv6_addr *ap;
1282 state = IPV6_STATE(ifp);
1284 TAILQ_FOREACH(ap, &state->addrs, next) {
1286 if (IN6_IS_ADDR_LINKLOCAL(&ap->addr) &&
1287 (!revflags || !(ap->addr_flags & revflags)))
1290 if (IN6_ARE_ADDR_EQUAL(&ap->addr, addr) &&
1291 (!revflags || !(ap->addr_flags & revflags)))
1299 static struct ipv6_addr *
1300 ipv6_iffindmaskaddr(const struct interface *ifp, const struct in6_addr *addr)
1302 struct ipv6_state *state;
1303 struct ipv6_addr *ap;
1304 struct in6_addr mask;
1306 state = IPV6_STATE(ifp);
1308 TAILQ_FOREACH(ap, &state->addrs, next) {
1309 if (ipv6_mask(&mask, ap->prefix_len) == -1)
1311 if (IN6_ARE_MASKED_ADDR_EQUAL(&ap->addr, addr, &mask))
1319 ipv6_findmaskaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr)
1321 struct interface *ifp;
1322 struct ipv6_addr *ap;
1324 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1325 ap = ipv6_iffindmaskaddr(ifp, addr);
1333 ipv6_addlinklocalcallback(struct interface *ifp,
1334 void (*callback)(void *), void *arg)
1336 struct ipv6_state *state;
1337 struct ll_callback *cb;
1339 state = ipv6_getstate(ifp);
1340 TAILQ_FOREACH(cb, &state->ll_callbacks, next) {
1341 if (cb->callback == callback && cb->arg == arg)
1345 cb = malloc(sizeof(*cb));
1350 cb->callback = callback;
1352 TAILQ_INSERT_TAIL(&state->ll_callbacks, cb, next);
1357 static struct ipv6_addr *
1358 ipv6_newlinklocal(struct interface *ifp)
1360 struct ipv6_addr *ia;
1361 struct in6_addr in6;
1363 memset(&in6, 0, sizeof(in6));
1364 in6.s6_addr32[0] = htonl(0xfe800000);
1365 ia = ipv6_newaddr(ifp, &in6, 64, 0);
1367 ia->prefix_pltime = ND6_INFINITE_LIFETIME;
1368 ia->prefix_vltime = ND6_INFINITE_LIFETIME;
1373 static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1374 static const uint8_t allone[8] =
1375 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1378 ipv6_addlinklocal(struct interface *ifp)
1380 struct ipv6_state *state;
1381 struct ipv6_addr *ap, *ap2;
1384 /* Check sanity before malloc */
1385 if (!(ifp->options->options & DHCPCD_SLAACPRIVATE)) {
1386 switch (ifp->family) {
1388 /* Check for a valid hardware address */
1389 if (ifp->hwlen != 6 && ifp->hwlen != 8) {
1393 if (memcmp(ifp->hwaddr, allzero, ifp->hwlen) == 0 ||
1394 memcmp(ifp->hwaddr, allone, ifp->hwlen) == 0)
1406 state = ipv6_getstate(ifp);
1410 ap = ipv6_newlinklocal(ifp);
1415 if (ifp->options->options & DHCPCD_SLAACPRIVATE) {
1417 if (ipv6_makestableprivate(&ap->addr,
1418 &ap->prefix, ap->prefix_len, ifp, &dadcounter) == -1)
1423 ap->dadcounter = dadcounter;
1425 memcpy(ap->addr.s6_addr, ap->prefix.s6_addr, 8);
1426 switch (ifp->family) {
1428 if (ifp->hwlen == 6) {
1429 ap->addr.s6_addr[ 8] = ifp->hwaddr[0];
1430 ap->addr.s6_addr[ 9] = ifp->hwaddr[1];
1431 ap->addr.s6_addr[10] = ifp->hwaddr[2];
1432 ap->addr.s6_addr[11] = 0xff;
1433 ap->addr.s6_addr[12] = 0xfe;
1434 ap->addr.s6_addr[13] = ifp->hwaddr[3];
1435 ap->addr.s6_addr[14] = ifp->hwaddr[4];
1436 ap->addr.s6_addr[15] = ifp->hwaddr[5];
1437 } else if (ifp->hwlen == 8)
1438 memcpy(&ap->addr.s6_addr[8], ifp->hwaddr, 8);
1447 /* Sanity check: g bit must not indciate "group" */
1448 if (EUI64_GROUP(&ap->addr)) {
1453 EUI64_TO_IFID(&ap->addr);
1456 /* Do we already have this address? */
1457 TAILQ_FOREACH(ap2, &state->addrs, next) {
1458 if (IN6_ARE_ADDR_EQUAL(&ap->addr, &ap2->addr)) {
1459 if (ap2->addr_flags & IN6_IFF_DUPLICATED) {
1460 if (ifp->options->options &
1461 DHCPCD_SLAACPRIVATE)
1464 goto nextslaacprivate;
1467 errno = EADDRNOTAVAIL;
1471 logwarnx("%s: waiting for %s to complete",
1472 ap2->iface->name, ap2->saddr);
1479 inet_ntop(AF_INET6, &ap->addr, ap->saddr, sizeof(ap->saddr));
1480 TAILQ_INSERT_TAIL(&state->addrs, ap, next);
1481 ipv6_addaddr(ap, NULL);
1486 ipv6_tryaddlinklocal(struct interface *ifp)
1488 struct ipv6_addr *ia;
1490 /* We can't assign a link-locak address to this,
1491 * the ppp process has to. */
1492 if (ifp->flags & IFF_POINTOPOINT)
1495 ia = ipv6_iffindaddr(ifp, NULL, IN6_IFF_DUPLICATED);
1497 #ifdef IPV6_POLLADDRFLAG
1498 if (ia->addr_flags & IN6_IFF_TENTATIVE) {
1501 ms_to_ts(&tv, RETRANS_TIMER / 2);
1502 eloop_timeout_add_tv(
1503 ia->iface->ctx->eloop,
1504 &tv, ipv6_checkaddrflags, ia);
1509 if (!CAN_ADD_LLADDR(ifp))
1512 return ipv6_addlinklocal(ifp);
1516 ipv6_newaddr(struct interface *ifp, const struct in6_addr *addr,
1517 uint8_t prefix_len, unsigned int flags)
1519 struct ipv6_addr *ia;
1520 char buf[INET6_ADDRSTRLEN];
1525 /* If adding a new DHCP / RA derived address, check current flags
1526 * from an existing address. */
1527 ia = ipv6_iffindaddr(ifp, addr, 0);
1529 addr_flags = ia->addr_flags;
1531 addr_flags = IN6_IFF_TENTATIVE;
1533 ia = calloc(1, sizeof(*ia));
1538 ia->addr_flags = addr_flags;
1539 ia->flags = IPV6_AF_NEW | flags;
1540 if (!(ia->addr_flags & IN6_IFF_NOTUSEABLE))
1541 ia->flags |= IPV6_AF_DADCOMPLETED;
1542 ia->prefix_len = prefix_len;
1545 #ifdef IPV6_AF_TEMPORARY
1546 tempaddr = ia->flags & IPV6_AF_TEMPORARY;
1551 if (ia->flags & IPV6_AF_AUTOCONF && !tempaddr) {
1553 ia->dadcounter = ipv6_makeaddr(&ia->addr, ifp,
1556 if (ia->dadcounter == -1)
1558 } else if (ia->flags & IPV6_AF_RAPFX) {
1562 cbp = inet_ntop(AF_INET6, &ia->addr, buf, sizeof(buf));
1567 } else if (ia->flags & (IPV6_AF_REQUEST | IPV6_AF_DELEGATEDPFX) &&
1571 cbp = inet_ntop(AF_INET6, &ia->prefix, buf, sizeof(buf));
1575 if (ipv6_makeprefix(&ia->prefix,
1576 &ia->addr, ia->prefix_len) == -1)
1580 cbp = inet_ntop(AF_INET6, &ia->addr, buf, sizeof(buf));
1584 snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", cbp, ia->prefix_len);
1595 ipv6_staticdadcallback(void *arg)
1597 struct ipv6_addr *ia = arg;
1600 wascompleted = (ia->flags & IPV6_AF_DADCOMPLETED);
1601 ia->flags |= IPV6_AF_DADCOMPLETED;
1602 if (ia->flags & IPV6_AF_DUPLICATED)
1603 logwarnx("%s: DAD detected %s", ia->iface->name,
1605 else if (!wascompleted) {
1606 logdebugx("%s: IPv6 static DAD completed",
1610 #define FINISHED (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)
1611 if (!wascompleted) {
1612 struct interface *ifp;
1613 struct ipv6_state *state;
1616 state = IPV6_STATE(ifp);
1617 TAILQ_FOREACH(ia, &state->addrs, next) {
1618 if (ia->flags & IPV6_AF_STATIC &&
1619 (ia->flags & FINISHED) != FINISHED)
1626 script_runreason(ifp, "STATIC6");
1632 ipv6_env(FILE *fp, const char *prefix, const struct interface *ifp)
1634 struct ipv6_addr *ia;
1636 ia = ipv6_iffindaddr(UNCONST(ifp), &ifp->options->req_addr6,
1637 IN6_IFF_NOTUSEABLE);
1640 if (efprintf(fp, "%s_ip6_address=%s", prefix, ia->saddr) == -1)
1646 ipv6_staticdadcompleted(const struct interface *ifp)
1648 const struct ipv6_state *state;
1649 const struct ipv6_addr *ia;
1652 if ((state = IPV6_CSTATE(ifp)) == NULL)
1655 #define COMPLETED (IPV6_AF_STATIC | IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)
1656 TAILQ_FOREACH(ia, &state->addrs, next) {
1657 if ((ia->flags & COMPLETED) == COMPLETED &&
1658 !(ia->addr_flags & IN6_IFF_NOTUSEABLE))
1665 ipv6_startstatic(struct interface *ifp)
1667 struct ipv6_addr *ia;
1670 if (IN6_IS_ADDR_UNSPECIFIED(&ifp->options->req_addr6))
1673 ia = ipv6_iffindaddr(ifp, &ifp->options->req_addr6, 0);
1675 (ia->prefix_len != ifp->options->req_prefix_len ||
1676 ia->addr_flags & IN6_IFF_NOTUSEABLE))
1678 ipv6_deleteaddr(ia);
1682 struct ipv6_state *state;
1684 ia = ipv6_newaddr(ifp, &ifp->options->req_addr6,
1685 ifp->options->req_prefix_len, 0);
1688 state = IPV6_STATE(ifp);
1689 TAILQ_INSERT_TAIL(&state->addrs, ia, next);
1693 ia->flags |= IPV6_AF_STATIC | IPV6_AF_ONLINK;
1694 ia->prefix_vltime = ND6_INFINITE_LIFETIME;
1695 ia->prefix_pltime = ND6_INFINITE_LIFETIME;
1696 ia->dadcallback = ipv6_staticdadcallback;
1697 ipv6_addaddr(ia, NULL);
1698 rt_build(ifp->ctx, AF_INET6);
1700 script_runreason(ifp, "STATIC6");
1704 /* Ensure the interface has a link-local address */
1706 ipv6_start(struct interface *ifp)
1708 #ifdef IPV6_POLLADDRFLAG
1709 struct ipv6_state *state;
1711 /* We need to update the address flags. */
1712 if ((state = IPV6_STATE(ifp)) != NULL) {
1713 struct ipv6_addr *ia;
1717 TAILQ_FOREACH(ia, &state->addrs, next) {
1723 flags = if_addrflags6(ia->iface, &ia->addr, alias);
1725 ia->addr_flags = flags;
1730 if (ipv6_tryaddlinklocal(ifp) == -1)
1733 if (IPV6_CSTATE(ifp)) {
1734 /* Regenerate new ids */
1735 if (ip6_use_tempaddr(ifp->name))
1736 ipv6_regentempifid(ifp);
1743 ipv6_freedrop(struct interface *ifp, int drop)
1745 struct ipv6_state *state;
1746 struct ll_callback *cb;
1751 if ((state = IPV6_STATE(ifp)) == NULL)
1754 /* If we got here, we can get rid of any LL callbacks. */
1755 while ((cb = TAILQ_FIRST(&state->ll_callbacks))) {
1756 TAILQ_REMOVE(&state->ll_callbacks, cb, next);
1760 ipv6_freedrop_addrs(&state->addrs, drop ? 2 : 0, NULL);
1762 if (ifp->ctx->ra_routers != NULL)
1763 rt_build(ifp->ctx, AF_INET6);
1765 /* Because we need to cache the addresses we don't control,
1766 * we only free the state on when NOT dropping addresses. */
1768 ifp->if_data[IF_DATA_IPV6] = NULL;
1769 eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp);
1774 ipv6_ctxfree(struct dhcpcd_ctx *ctx)
1777 free(ctx->ra_routers);
1782 ipv6_handleifa_addrs(int cmd,
1783 struct ipv6_addrhead *addrs, const struct ipv6_addr *addr, pid_t pid)
1785 struct ipv6_addr *ia, *ian;
1786 uint8_t found, alldadcompleted;
1788 alldadcompleted = 1;
1790 TAILQ_FOREACH_SAFE(ia, addrs, next, ian) {
1791 if (!IN6_ARE_ADDR_EQUAL(&addr->addr, &ia->addr)) {
1792 if (ia->flags & IPV6_AF_ADDED &&
1793 !(ia->flags & IPV6_AF_DADCOMPLETED))
1794 alldadcompleted = 0;
1799 if (ia->flags & IPV6_AF_ADDED) {
1800 logwarnx("%s: pid %d deleted address %s",
1801 ia->iface->name, pid, ia->saddr);
1802 ia->flags &= ~IPV6_AF_ADDED;
1804 if (ia->flags & IPV6_AF_DELEGATED) {
1805 TAILQ_REMOVE(addrs, ia, next);
1806 ipv6_deletedaddr(ia);
1811 /* Safety - ignore tentative announcements */
1812 if (addr->addr_flags &
1813 (IN6_IFF_DETACHED | IN6_IFF_TENTATIVE))
1815 if ((ia->flags & IPV6_AF_DADCOMPLETED) == 0) {
1817 if (addr->addr_flags & IN6_IFF_DUPLICATED)
1818 ia->flags |= IPV6_AF_DUPLICATED;
1820 ia->flags &= ~IPV6_AF_DUPLICATED;
1821 if (ia->dadcallback)
1822 ia->dadcallback(ia);
1823 /* We need to set this here in-case the
1824 * dadcallback function checks it */
1825 ia->flags |= IPV6_AF_DADCOMPLETED;
1831 return alldadcompleted ? found : 0;
1834 #ifdef IPV6_MANAGETEMPADDR
1835 static const struct ipv6_addr *
1836 ipv6_findaddrid(struct dhcpcd_ctx *ctx, uint8_t *addr)
1838 const struct interface *ifp;
1839 const struct ipv6_state *state;
1840 const struct ipv6_addr *ia;
1842 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1843 if ((state = IPV6_CSTATE(ifp))) {
1844 TAILQ_FOREACH(ia, &state->addrs, next) {
1845 if (memcmp(&ia->addr.s6_addr[8], addr, 8) == 0)
1853 static const uint8_t nullid[8];
1854 static const uint8_t anycastid[8] = {
1855 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
1856 static const uint8_t isatapid[4] = { 0x00, 0x00, 0x5e, 0xfe };
1859 ipv6_regen_desync(struct interface *ifp, int force)
1861 struct ipv6_state *state;
1864 state = IPV6_STATE(ifp);
1866 /* RFC4941 Section 5 states that DESYNC_FACTOR must never be
1867 * greater than TEMP_VALID_LIFETIME - REGEN_ADVANCE.
1868 * I believe this is an error and it should be never be greateter than
1869 * TEMP_PREFERRED_LIFETIME - REGEN_ADVANCE. */
1870 max = ip6_temp_preferred_lifetime(ifp->name) - REGEN_ADVANCE;
1871 if (state->desync_factor && !force && state->desync_factor < max)
1873 if (state->desync_factor == 0)
1874 state->desync_factor =
1875 (time_t)arc4random_uniform(MIN(MAX_DESYNC_FACTOR,
1877 max = ip6_temp_preferred_lifetime(ifp->name) -
1878 state->desync_factor - REGEN_ADVANCE;
1879 eloop_timeout_add_sec(ifp->ctx->eloop, max, ipv6_regentempifid, ifp);
1883 ipv6_gentempifid(struct interface *ifp)
1885 struct ipv6_state *state;
1887 uint8_t seed[16], digest[16];
1890 if ((state = IPV6_STATE(ifp)) == NULL)
1894 if (memcmp(nullid, state->randomseed0, sizeof(nullid)) == 0) {
1898 memcpy(seed, &r, sizeof(r));
1900 memcpy(seed + sizeof(r), &r, sizeof(r));
1902 memcpy(seed, state->randomseed0, sizeof(state->randomseed0));
1904 memcpy(seed + sizeof(state->randomseed0),
1905 state->randomseed1, sizeof(state->randomseed1));
1909 MD5Update(&md5, seed, sizeof(seed));
1910 MD5Final(digest, &md5);
1912 /* RFC4941 Section 3.2.1.1
1913 * Take the left-most 64bits and set bit 6 to zero */
1914 memcpy(state->randomid, digest, sizeof(state->randomid));
1915 state->randomid[0] = (uint8_t)(state->randomid[0] & ~EUI64_UBIT);
1917 /* RFC4941 Section 3.2.1.4
1918 * Reject reserved or existing id's */
1919 if (memcmp(nullid, state->randomid, sizeof(nullid)) == 0 ||
1920 (memcmp(anycastid, state->randomid, 7) == 0 &&
1921 (anycastid[7] & state->randomid[7]) == anycastid[7]) ||
1922 memcmp(isatapid, state->randomid, sizeof(isatapid)) == 0 ||
1923 ipv6_findaddrid(ifp->ctx, state->randomid))
1925 if (++retry < GEN_TEMPID_RETRY_MAX) {
1926 memcpy(seed, digest + 8, 8);
1929 memset(state->randomid, 0, sizeof(state->randomid));
1932 /* RFC4941 Section 3.2.1.6
1933 * Save the right-most 64bits of the digest */
1934 memcpy(state->randomseed0, digest + 8,
1935 sizeof(state->randomseed0));
1938 /* RFC4941 Section 3.3.7 */
1940 ipv6_tempdadcallback(void *arg)
1942 struct ipv6_addr *ia = arg;
1944 if (ia->flags & IPV6_AF_DUPLICATED) {
1945 struct ipv6_addr *ia1;
1948 if (++ia->dadcounter == TEMP_IDGEN_RETRIES) {
1949 logerrx("%s: too many duplicate temporary addresses",
1953 clock_gettime(CLOCK_MONOTONIC, &tv);
1954 if ((ia1 = ipv6_createtempaddr(ia, &tv)) == NULL)
1957 ia1->dadcounter = ia->dadcounter;
1958 ipv6_deleteaddr(ia);
1960 ipv6_addaddr(ia1, &ia1->acquired);
1965 ipv6_createtempaddr(struct ipv6_addr *ia0, const struct timespec *now)
1967 struct ipv6_state *state;
1968 const struct ipv6_state *cstate;
1970 struct in6_addr addr, mask;
1972 const struct interface *ifp;
1973 const struct ipv6_addr *ap;
1974 struct ipv6_addr *ia;
1975 uint32_t i, trylimit;
1977 trylimit = TEMP_IDGEN_RETRIES;
1978 state = IPV6_STATE(ia0->iface);
1982 ipv6_mask(&mask, ia0->prefix_len);
1983 /* clear the old ifid */
1984 for (i = 0; i < 4; i++)
1985 addr.s6_addr32[i] &= mask.s6_addr32[i];
1988 if (memcmp(state->randomid, nullid, sizeof(nullid)) == 0)
1991 memcpy(state->randomseed1, &ia0->addr.s6_addr[8],
1992 sizeof(state->randomseed1));
1993 ipv6_gentempifid(ia0->iface);
1994 if (memcmp(state->randomid, nullid, sizeof(nullid)) == 0) {
1999 memcpy(&randid[0], state->randomid, sizeof(randid[0]));
2000 memcpy(&randid[1], state->randomid + sizeof(randid[1]),
2002 addr.s6_addr32[2] |= randid[0] & ~mask.s6_addr32[2];
2003 addr.s6_addr32[3] |= randid[1] & ~mask.s6_addr32[3];
2005 /* Ensure we don't already have it */
2006 TAILQ_FOREACH(ifp, ia0->iface->ctx->ifaces, next) {
2007 cstate = IPV6_CSTATE(ifp);
2009 TAILQ_FOREACH(ap, &cstate->addrs, next) {
2010 if (IN6_ARE_ADDR_EQUAL(&ap->addr, &addr)) {
2011 if (--trylimit == 0) {
2022 ia = ipv6_newaddr(ia0->iface, &addr, ia0->prefix_len,
2023 IPV6_AF_AUTOCONF | IPV6_AF_TEMPORARY);
2024 /* Must be made tentative, for our DaD to work */
2025 ia->addr_flags = IN6_IFF_TENTATIVE;
2026 ia->dadcallback = ipv6_tempdadcallback;
2027 ia->created = ia->acquired = now ? *now : ia0->acquired;
2029 /* Ensure desync is still valid */
2030 ipv6_regen_desync(ia->iface, 0);
2032 /* RFC4941 Section 3.3.4 */
2033 i = (uint32_t)(ip6_temp_preferred_lifetime(ia0->iface->name) -
2034 state->desync_factor);
2035 ia->prefix_pltime = MIN(ia0->prefix_pltime, i);
2036 i = (uint32_t)ip6_temp_valid_lifetime(ia0->iface->name);
2037 ia->prefix_vltime = MIN(ia0->prefix_vltime, i);
2038 if (ia->prefix_pltime <= REGEN_ADVANCE ||
2039 ia->prefix_pltime > ia0->prefix_vltime)
2046 TAILQ_INSERT_TAIL(&state->addrs, ia, next);
2051 ipv6_settemptime(struct ipv6_addr *ia, int flags)
2053 struct ipv6_state *state;
2054 struct ipv6_addr *ap, *first;
2056 state = IPV6_STATE(ia->iface);
2058 TAILQ_FOREACH_REVERSE(ap, &state->addrs, ipv6_addrhead, next) {
2059 if (ap->flags & IPV6_AF_TEMPORARY &&
2060 ap->prefix_pltime &&
2061 IN6_ARE_ADDR_EQUAL(&ia->prefix, &ap->prefix))
2066 if (ap->prefix_pltime -
2067 (uint32_t)(ia->acquired.tv_sec -
2068 ap->acquired.tv_sec)
2075 if (!(ap->flags & IPV6_AF_ADDED))
2076 ap->flags |= IPV6_AF_NEW | IPV6_AF_AUTOCONF;
2077 ap->flags &= ~IPV6_AF_STALE;
2079 /* RFC4941 Section 3.4
2080 * Deprecated prefix, deprecate the temporary address */
2081 if (ia->prefix_pltime == 0) {
2082 ap->prefix_pltime = 0;
2086 /* Ensure desync is still valid */
2087 ipv6_regen_desync(ap->iface, 0);
2089 /* RFC4941 Section 3.3.2
2090 * Extend temporary times, but ensure that they
2091 * never last beyond the system limit. */
2092 ext = ia->acquired.tv_sec + (time_t)ia->prefix_pltime;
2093 max = ap->created.tv_sec +
2094 ip6_temp_preferred_lifetime(ap->iface->name) -
2095 state->desync_factor;
2097 ap->prefix_pltime = ia->prefix_pltime;
2100 (uint32_t)(max - ia->acquired.tv_sec);
2103 ext = ia->acquired.tv_sec + (time_t)ia->prefix_vltime;
2104 max = ap->created.tv_sec +
2105 ip6_temp_valid_lifetime(ap->iface->name);
2107 ap->prefix_vltime = ia->prefix_vltime;
2110 (uint32_t)(max - ia->acquired.tv_sec);
2112 /* Just extend the latest matching prefix */
2113 ap->acquired = ia->acquired;
2115 /* If extending return the last match as
2116 * it's the most current.
2117 * If deprecating, deprecate any other addresses we
2118 * may have, although this should not be needed */
2119 if (ia->prefix_pltime)
2129 ipv6_addtempaddrs(struct interface *ifp, const struct timespec *now)
2131 struct ipv6_state *state;
2132 struct ipv6_addr *ia;
2134 state = IPV6_STATE(ifp);
2135 TAILQ_FOREACH(ia, &state->addrs, next) {
2136 if (ia->flags & IPV6_AF_TEMPORARY &&
2137 !(ia->flags & IPV6_AF_STALE))
2138 ipv6_addaddr(ia, now);
2143 ipv6_regentempaddr(void *arg)
2145 struct ipv6_addr *ia = arg, *ia1;
2148 logdebugx("%s: regen temp addr %s", ia->iface->name, ia->saddr);
2149 clock_gettime(CLOCK_MONOTONIC, &tv);
2150 ia1 = ipv6_createtempaddr(ia, &tv);
2152 ipv6_addaddr(ia1, &tv);
2158 ipv6_regentempifid(void *arg)
2160 struct interface *ifp = arg;
2161 struct ipv6_state *state;
2163 state = IPV6_STATE(ifp);
2164 if (memcmp(state->randomid, nullid, sizeof(state->randomid)))
2165 ipv6_gentempifid(ifp);
2167 ipv6_regen_desync(ifp, 1);
2169 #endif /* IPV6_MANAGETEMPADDR */
2172 ipv6_markaddrsstale(struct interface *ifp, unsigned int flags)
2174 struct ipv6_state *state;
2175 struct ipv6_addr *ia;
2177 state = IPV6_STATE(ifp);
2181 TAILQ_FOREACH(ia, &state->addrs, next) {
2182 if (flags == 0 || ia->flags & flags)
2183 ia->flags |= IPV6_AF_STALE;
2188 ipv6_deletestaleaddrs(struct interface *ifp)
2190 struct ipv6_state *state;
2191 struct ipv6_addr *ia, *ia1;
2193 state = IPV6_STATE(ifp);
2197 TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) {
2198 if (ia->flags & IPV6_AF_STALE)
2199 ipv6_handleifa(ifp->ctx, RTM_DELADDR,
2200 ifp->ctx->ifaces, ifp->name,
2201 &ia->addr, ia->prefix_len, 0, getpid());
2207 inet6_makeroute(struct interface *ifp, const struct ra *rap)
2211 if ((rt = rt_new(ifp)) == NULL)
2214 #ifdef HAVE_ROUTE_METRIC
2215 rt->rt_metric = ifp->metric;
2218 rt->rt_mtu = rap->mtu;
2223 inet6_makeprefix(struct interface *ifp, const struct ra *rap,
2224 const struct ipv6_addr *addr)
2227 struct in6_addr netmask;
2229 if (addr == NULL || addr->prefix_len > 128) {
2234 /* There is no point in trying to manage a /128 prefix,
2235 * ones without a lifetime. */
2236 if (addr->prefix_len == 128 || addr->prefix_vltime == 0)
2239 /* Don't install a reject route when not creating bigger prefixes. */
2240 if (addr->flags & IPV6_AF_NOREJECT)
2243 /* This address is the delegated prefix, so add a reject route for
2244 * it via the loopback interface. */
2245 if (addr->flags & IPV6_AF_DELEGATEDPFX) {
2246 struct interface *lo0;
2248 TAILQ_FOREACH(lo0, ifp->ctx->ifaces, next) {
2249 if (lo0->flags & IFF_LOOPBACK)
2253 logwarnx("cannot find a loopback interface "
2259 if ((rt = inet6_makeroute(ifp, rap)) == NULL)
2262 sa_in6_init(&rt->rt_dest, &addr->prefix);
2263 ipv6_mask(&netmask, addr->prefix_len);
2264 sa_in6_init(&rt->rt_netmask, &netmask);
2265 if (addr->flags & IPV6_AF_DELEGATEDPFX) {
2266 rt->rt_flags |= RTF_REJECT;
2267 /* Linux does not like a gateway for a reject route. */
2269 sa_in6_init(&rt->rt_gateway, &in6addr_loopback);
2271 } else if (!(addr->flags & IPV6_AF_ONLINK))
2272 sa_in6_init(&rt->rt_gateway, &rap->from);
2274 rt->rt_gateway.sa_family = AF_UNSPEC;
2275 sa_in6_init(&rt->rt_ifa, &addr->addr);
2280 inet6_makerouter(struct ra *rap)
2284 if ((rt = inet6_makeroute(rap->iface, rap)) == NULL)
2286 sa_in6_init(&rt->rt_dest, &in6addr_any);
2287 sa_in6_init(&rt->rt_netmask, &in6addr_any);
2288 sa_in6_init(&rt->rt_gateway, &rap->from);
2292 #define RT_IS_DEFAULT(rtp) \
2293 (IN6_ARE_ADDR_EQUAL(&((rtp)->dest), &in6addr_any) && \
2294 IN6_ARE_ADDR_EQUAL(&((rtp)->mask), &in6addr_any))
2297 inet6_staticroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
2299 struct interface *ifp;
2300 struct ipv6_state *state;
2301 struct ipv6_addr *ia;
2304 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
2305 if ((state = IPV6_STATE(ifp)) == NULL)
2307 TAILQ_FOREACH(ia, &state->addrs, next) {
2308 if ((ia->flags & (IPV6_AF_ADDED | IPV6_AF_STATIC)) ==
2309 (IPV6_AF_ADDED | IPV6_AF_STATIC))
2311 rt = inet6_makeprefix(ifp, NULL, ia);
2313 rt_proto_add(routes, rt);
2321 inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
2325 const struct ipv6_addr *addr;
2327 TAILQ_FOREACH(rap, ctx->ra_routers, next) {
2330 TAILQ_FOREACH(addr, &rap->addrs, next) {
2331 if (addr->prefix_vltime == 0)
2333 rt = inet6_makeprefix(rap->iface, rap, addr);
2335 rt->rt_dflags |= RTDF_RA;
2336 rt_proto_add(routes, rt);
2339 if (rap->lifetime == 0)
2341 if (ipv6_anyglobal(rap->iface) == NULL)
2343 rt = inet6_makerouter(rap);
2346 rt->rt_dflags |= RTDF_RA;
2347 rt_proto_add(routes, rt);
2354 inet6_dhcproutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx,
2357 struct interface *ifp;
2358 const struct dhcp6_state *d6_state;
2359 const struct ipv6_addr *addr;
2362 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
2363 d6_state = D6_CSTATE(ifp);
2364 if (d6_state && d6_state->state == dstate) {
2365 TAILQ_FOREACH(addr, &d6_state->addrs, next) {
2366 rt = inet6_makeprefix(ifp, NULL, addr);
2369 rt->rt_dflags |= RTDF_DHCP;
2370 rt_proto_add(routes, rt);
2379 inet6_getroutes(struct dhcpcd_ctx *ctx, rb_tree_t *routes)
2382 /* Should static take priority? */
2383 if (inet6_staticroutes(routes, ctx) == -1)
2386 /* First add reachable routers and their prefixes */
2387 if (inet6_raroutes(routes, ctx) == -1)
2391 /* We have no way of knowing if prefixes added by DHCP are reachable
2392 * or not, so we have to assume they are.
2393 * Add bound before delegated so we can prefer interfaces better. */
2394 if (inet6_dhcproutes(routes, ctx, DH6S_BOUND) == -1)
2396 if (inet6_dhcproutes(routes, ctx, DH6S_DELEGATED) == -1)