2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/refcount.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
48 #include <sys/kernel.h>
50 #include <sys/errno.h>
51 #include <sys/rmlock.h>
52 #include <sys/rwlock.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/queue.h>
58 #include <net/if_var.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62 #include <net/route/nhop.h>
63 #include <net/route/route_ctl.h>
64 #include <net/route/route_var.h>
65 #include <net/radix.h>
68 #include <netinet/in.h>
69 #include <net/if_llatbl.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet6/in6_ifattach.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/nd6.h>
75 #include <netinet/icmp6.h>
76 #include <netinet6/scope6_var.h>
78 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
79 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
81 static int nd6_prefix_onlink(struct nd_prefix *);
83 TAILQ_HEAD(nd6_drhead, nd_defrouter);
84 VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
85 #define V_nd6_defrouter VNET(nd6_defrouter)
87 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
88 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
90 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
91 VNET_DEFINE(int, nd6_defifindex);
92 #define V_nd6_defifp VNET(nd6_defifp)
94 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
96 VNET_DEFINE(int, ip6_desync_factor);
97 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
98 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
100 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
103 VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
106 SYSCTL_DECL(_net_inet6_icmp6);
108 /* RTPREF_MEDIUM has to be 0! */
109 #define RTPREF_HIGH 1
110 #define RTPREF_MEDIUM 0
111 #define RTPREF_LOW (-1)
112 #define RTPREF_RESERVED (-2)
113 #define RTPREF_INVALID (-3) /* internal */
116 defrouter_ref(struct nd_defrouter *dr)
119 refcount_acquire(&dr->refcnt);
123 defrouter_rele(struct nd_defrouter *dr)
126 if (refcount_release(&dr->refcnt))
131 * Remove a router from the global list and optionally stash it in a
132 * caller-supplied queue.
135 defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
140 TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
143 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
147 * Receive Router Solicitation Message - just for routers.
148 * Router solicitation/advertisement is mostly managed by userland program
149 * (rtadvd) so here we have no function like nd6_ra_output().
154 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
158 struct nd_router_solicit *nd_rs;
159 struct in6_addr saddr6;
160 union nd_opts ndopts;
161 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
165 ifp = m->m_pkthdr.rcvif;
168 * Accept RS only when V_ip6_forwarding=1 and the interface has
169 * no ND6_IFF_ACCEPT_RTADV.
171 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
174 /* RFC 6980: Nodes MUST silently ignore fragments */
175 if(m->m_flags & M_FRAGMENTED)
179 ip6 = mtod(m, struct ip6_hdr *);
180 if (ip6->ip6_hlim != 255) {
182 "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
183 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
184 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
189 * Don't update the neighbor cache, if src = ::.
190 * This indicates that the src has no IP address assigned yet.
192 saddr6 = ip6->ip6_src;
193 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
196 if (m->m_len < off + icmp6len) {
197 m = m_pullup(m, off + icmp6len);
199 IP6STAT_INC(ip6s_exthdrtoolong);
203 ip6 = mtod(m, struct ip6_hdr *);
204 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
206 icmp6len -= sizeof(*nd_rs);
207 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
208 if (nd6_options(&ndopts) < 0) {
210 "%s: invalid ND option, ignored\n", __func__));
211 /* nd6_options have incremented stats */
217 if (ndopts.nd_opts_src_lladdr) {
218 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
219 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
222 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
224 "%s: lladdrlen mismatch for %s (if %d, RS packet %d)\n",
225 __func__, ip6_sprintf(ip6bufs, &saddr6),
226 ifp->if_addrlen, lladdrlen - 2));
230 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
237 ICMP6STAT_INC(icp6s_badrs);
243 * An initial update routine for draft-ietf-6man-ipv6only-flag.
244 * We need to iterate over all default routers for the given
245 * interface to see whether they are all advertising the "S"
246 * (IPv6-Only) flag. If they do set, otherwise unset, the
247 * interface flag we later use to filter on.
250 defrtr_ipv6_only_ifp(struct ifnet *ifp)
252 struct nd_defrouter *dr;
253 bool ipv6_only, ipv6_only_old;
255 struct epoch_tracker et;
260 if (V_nd6_ignore_ipv6_only_ra != 0)
265 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
266 if (dr->ifp == ifp &&
267 (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
271 IF_AFDATA_WLOCK(ifp);
272 ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
273 IF_AFDATA_WUNLOCK(ifp);
275 /* If nothing changed, we have an early exit. */
276 if (ipv6_only == ipv6_only_old)
281 * Should we want to set the IPV6-ONLY flag, check if the
282 * interface has a non-0/0 and non-link-local IPv4 address
283 * configured on it. If it has we will assume working
284 * IPv4 operations and will clear the interface flag.
286 has_ipv4_addr = false;
289 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
290 if (ifa->ifa_addr->sa_family != AF_INET)
293 satosin(ifa->ifa_addr)->sin_addr)) {
294 has_ipv4_addr = true;
300 if (ipv6_only && has_ipv4_addr) {
301 log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
302 "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
307 IF_AFDATA_WLOCK(ifp);
309 ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
311 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
312 IF_AFDATA_WUNLOCK(ifp);
315 /* Send notification of flag change. */
320 defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
323 IF_AFDATA_WLOCK(ifp);
324 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
325 IF_AFDATA_WUNLOCK(ifp);
327 #endif /* EXPERIMENTAL */
330 nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
334 * XXX-BZ we might want to trigger re-evaluation of our default router
335 * availability. E.g., on link down the default router might be
336 * unreachable but a different interface might still have connectivity.
340 if (linkstate == LINK_STATE_DOWN)
341 defrtr_ipv6_only_ipf_down(ifp);
346 * Receive Router Advertisement Message.
349 * TODO: on-link bit on prefix information
350 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
353 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
356 struct nd_ifinfo *ndi;
358 struct nd_router_advert *nd_ra;
359 struct in6_addr saddr6;
360 struct nd_defrouter *dr;
361 union nd_opts ndopts;
362 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
366 * We only accept RAs only when the per-interface flag
367 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
369 ifp = m->m_pkthdr.rcvif;
370 ndi = ND_IFINFO(ifp);
371 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
374 /* RFC 6980: Nodes MUST silently ignore fragments */
375 if(m->m_flags & M_FRAGMENTED)
378 ip6 = mtod(m, struct ip6_hdr *);
379 if (ip6->ip6_hlim != 255) {
381 "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
382 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
383 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
387 saddr6 = ip6->ip6_src;
388 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
390 "%s: src %s is not link-local\n", __func__,
391 ip6_sprintf(ip6bufs, &saddr6)));
395 if (m->m_len < off + icmp6len) {
396 m = m_pullup(m, off + icmp6len);
398 IP6STAT_INC(ip6s_exthdrtoolong);
402 ip6 = mtod(m, struct ip6_hdr *);
403 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
405 icmp6len -= sizeof(*nd_ra);
406 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
407 if (nd6_options(&ndopts) < 0) {
409 "%s: invalid ND option, ignored\n", __func__));
410 /* nd6_options have incremented stats */
417 struct nd_defrouter dr0;
418 u_int32_t advreachable = nd_ra->nd_ra_reachable;
420 /* remember if this is a multicasted advertisement */
421 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
424 bzero(&dr0, sizeof(dr0));
426 dr0.raflags = nd_ra->nd_ra_flags_reserved;
428 * Effectively-disable routes from RA messages when
429 * ND6_IFF_NO_RADR enabled on the receiving interface or
430 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
432 if (ndi->flags & ND6_IFF_NO_RADR)
434 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
437 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
438 dr0.expire = time_uptime + dr0.rtlifetime;
440 /* unspecified or not? (RFC 2461 6.3.4) */
442 advreachable = ntohl(advreachable);
443 if (advreachable <= MAX_REACHABLE_TIME &&
444 ndi->basereachable != advreachable) {
445 ndi->basereachable = advreachable;
446 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
447 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
450 if (nd_ra->nd_ra_retransmit)
451 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
452 if (nd_ra->nd_ra_curhoplimit) {
453 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
454 ndi->chlim = nd_ra->nd_ra_curhoplimit;
455 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
456 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
457 "%s on %s (current = %d, received = %d). "
458 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
459 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
462 dr = defrtrlist_update(&dr0);
464 defrtr_ipv6_only_ifp(ifp);
471 if (ndopts.nd_opts_pi) {
472 struct nd_opt_hdr *pt;
473 struct nd_opt_prefix_info *pi = NULL;
474 struct nd_prefixctl pr;
476 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
477 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
478 pt = (struct nd_opt_hdr *)((caddr_t)pt +
479 (pt->nd_opt_len << 3))) {
480 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
482 pi = (struct nd_opt_prefix_info *)pt;
484 if (pi->nd_opt_pi_len != 4) {
486 "%s: invalid option len %d for prefix "
487 "information option, ignored\n", __func__,
492 if (128 < pi->nd_opt_pi_prefix_len) {
494 "%s: invalid prefix len %d for prefix "
495 "information option, ignored\n", __func__,
496 pi->nd_opt_pi_prefix_len));
500 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
501 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
503 "%s: invalid prefix %s, ignored\n",
504 __func__, ip6_sprintf(ip6bufs,
505 &pi->nd_opt_pi_prefix)));
509 bzero(&pr, sizeof(pr));
510 pr.ndpr_prefix.sin6_family = AF_INET6;
511 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
512 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
513 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
515 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
516 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
517 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
518 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
519 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
520 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
521 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
522 (void)prelist_update(&pr, dr, m, mcast);
533 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
537 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
540 if (mtu < IPV6_MMTU) {
541 nd6log((LOG_INFO, "%s: bogus mtu option mtu=%lu sent "
542 "from %s, ignoring\n", __func__,
543 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
548 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
549 ? ndi->maxmtu : ifp->if_mtu;
551 int change = (ndi->linkmtu != mtu);
555 /* in6_maxmtu may change */
560 nd6log((LOG_INFO, "%s: bogus mtu=%lu sent from %s; "
561 "exceeds maxmtu %lu, ignoring\n", __func__,
562 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
569 * Source link layer address
575 if (ndopts.nd_opts_src_lladdr) {
576 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
577 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
580 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
582 "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
583 __func__, ip6_sprintf(ip6bufs, &saddr6),
584 ifp->if_addrlen, lladdrlen - 2));
588 nd6_cache_lladdr(ifp, &saddr6, lladdr,
589 lladdrlen, ND_ROUTER_ADVERT, 0);
592 * Installing a link-layer address might change the state of the
593 * router's neighbor cache, which might also affect our on-link
594 * detection of adveritsed prefixes.
596 pfxlist_onlink_check();
604 ICMP6STAT_INC(icp6s_badra);
609 static struct nd_pfxrouter *
610 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
612 struct nd_pfxrouter *search;
616 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
617 if (search->router == dr)
624 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
626 struct nd_pfxrouter *new;
632 if (pfxrtr_lookup(pr, dr) != NULL) {
638 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
645 if (pfxrtr_lookup(pr, dr) == NULL) {
646 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
649 /* We lost a race to add the reference. */
657 pfxlist_onlink_check();
661 pfxrtr_del(struct nd_pfxrouter *pfr)
666 LIST_REMOVE(pfr, pfr_entry);
667 defrouter_rele(pfr->router);
672 /* Default router list processing sub routines. */
674 defrouter_addreq(struct nd_defrouter *new)
676 struct sockaddr_in6 def, mask, gate;
677 struct rtentry *newrt = NULL;
681 bzero(&def, sizeof(def));
682 bzero(&mask, sizeof(mask));
683 bzero(&gate, sizeof(gate));
685 def.sin6_len = mask.sin6_len = gate.sin6_len =
686 sizeof(struct sockaddr_in6);
687 def.sin6_family = gate.sin6_family = AF_INET6;
688 gate.sin6_addr = new->rtaddr;
689 fibnum = new->ifp->if_fib;
691 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
692 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
693 RTF_GATEWAY, &newrt, fibnum);
695 rt_routemsg(RTM_ADD, newrt, new->ifp, 0, fibnum);
701 * Remove the default route for a given router.
702 * This is just a subroutine function for defrouter_select_fib(), and
703 * should not be called from anywhere else.
706 defrouter_delreq(struct nd_defrouter *dr)
708 struct sockaddr_in6 def, mask, gate;
709 struct rtentry *oldrt = NULL;
710 struct epoch_tracker et;
713 bzero(&def, sizeof(def));
714 bzero(&mask, sizeof(mask));
715 bzero(&gate, sizeof(gate));
717 def.sin6_len = mask.sin6_len = gate.sin6_len =
718 sizeof(struct sockaddr_in6);
719 def.sin6_family = gate.sin6_family = AF_INET6;
720 gate.sin6_addr = dr->rtaddr;
721 fibnum = dr->ifp->if_fib;
724 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
725 (struct sockaddr *)&gate,
726 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, fibnum);
728 rt_routemsg(RTM_DELETE, oldrt, dr->ifp, 0, fibnum);
735 defrouter_del(struct nd_defrouter *dr)
737 struct nd_defrouter *deldr = NULL;
738 struct nd_prefix *pr;
739 struct nd_pfxrouter *pfxrtr;
744 * Flush all the routing table entries that use the router
747 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
748 rt6_flush(&dr->rtaddr, dr->ifp);
751 defrtr_ipv6_only_ifp(dr->ifp);
756 defrouter_delreq(dr);
760 * Also delete all the pointers to the router in each prefix lists.
763 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
764 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
769 pfxlist_onlink_check();
772 * If the router is the primary one, choose a new one.
773 * Note that defrouter_select_fib() will remove the current
774 * gateway from the routing table.
777 defrouter_select_fib(deldr->ifp->if_fib);
780 * Release the list reference.
786 struct nd_defrouter *
787 defrouter_lookup_locked(const struct in6_addr *addr, struct ifnet *ifp)
789 struct nd_defrouter *dr;
792 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
793 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
800 struct nd_defrouter *
801 defrouter_lookup(const struct in6_addr *addr, struct ifnet *ifp)
803 struct nd_defrouter *dr;
806 dr = defrouter_lookup_locked(addr, ifp);
812 * Remove all default routes from default router list.
815 defrouter_reset(void)
817 struct nd_defrouter *dr, **dra;
823 * We can't delete routes with the ND lock held, so make a copy of the
824 * current default router list and use that when deleting routes.
827 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
831 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
834 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
842 for (i = 0; i < count && dra[i] != NULL; i++) {
843 defrouter_delreq(dra[i]);
844 defrouter_rele(dra[i]);
849 * XXX should we also nuke any default routers in the kernel, by
850 * going through them by rtalloc1()?
855 * Look up a matching default router list entry and remove it. Returns true if a
856 * matching entry was found, false otherwise.
859 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
861 struct nd_defrouter *dr;
864 dr = defrouter_lookup_locked(addr, ifp);
870 defrouter_unlink(dr, NULL);
878 * for default router selection
879 * regards router-preference field as a 2-bit signed integer
882 rtpref(struct nd_defrouter *dr)
884 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
885 case ND_RA_FLAG_RTPREF_HIGH:
886 return (RTPREF_HIGH);
887 case ND_RA_FLAG_RTPREF_MEDIUM:
888 case ND_RA_FLAG_RTPREF_RSV:
889 return (RTPREF_MEDIUM);
890 case ND_RA_FLAG_RTPREF_LOW:
894 * This case should never happen. If it did, it would mean a
895 * serious bug of kernel internal. We thus always bark here.
896 * Or, can we even panic?
898 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
899 return (RTPREF_INVALID);
905 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
906 * draft-ietf-ipngwg-router-selection:
907 * 1) Routers that are reachable or probably reachable should be preferred.
908 * If we have more than one (probably) reachable router, prefer ones
909 * with the highest router preference.
910 * 2) When no routers on the list are known to be reachable or
911 * probably reachable, routers SHOULD be selected in a round-robin
912 * fashion, regardless of router preference values.
913 * 3) If the Default Router List is empty, assume that all
914 * destinations are on-link.
916 * We assume nd_defrouter is sorted by router preference value.
917 * Since the code below covers both with and without router preference cases,
918 * we do not need to classify the cases by ifdef.
920 * At this moment, we do not try to install more than one default router,
921 * even when the multipath routing is available, because we're not sure about
922 * the benefits for stub hosts comparing to the risk of making the code
923 * complicated and the possibility of introducing bugs.
925 * We maintain a single list of routers for multiple FIBs, only considering one
926 * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
927 * we do the whole thing multiple times.
930 defrouter_select_fib(int fibnum)
932 struct epoch_tracker et;
933 struct nd_defrouter *dr, *selected_dr, *installed_dr;
934 struct llentry *ln = NULL;
936 if (fibnum == RT_ALL_FIBS) {
937 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
938 defrouter_select_fib(fibnum);
944 * Let's handle easy case (3) first:
945 * If default router list is empty, there's nothing to be done.
947 if (TAILQ_EMPTY(&V_nd6_defrouter)) {
953 * Search for a (probably) reachable router from the list.
954 * We just pick up the first reachable one (if any), assuming that
955 * the ordering rule of the list described in defrtrlist_update().
957 selected_dr = installed_dr = NULL;
958 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
960 if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
961 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
962 ND6_IS_LLINFO_PROBREACH(ln)) {
964 defrouter_ref(selected_dr);
972 if (dr->installed && dr->ifp->if_fib == fibnum) {
973 if (installed_dr == NULL) {
975 defrouter_ref(installed_dr);
978 * this should not happen.
979 * warn for diagnosis.
981 log(LOG_ERR, "defrouter_select_fib: more than "
982 "one router is installed\n");
987 * If none of the default routers was found to be reachable,
988 * round-robin the list regardless of preference.
989 * Otherwise, if we have an installed router, check if the selected
990 * (reachable) router should really be preferred to the installed one.
991 * We only prefer the new router when the old one is not reachable
992 * or when the new one has a really higher preference value.
994 if (selected_dr == NULL) {
995 if (installed_dr == NULL ||
996 TAILQ_NEXT(installed_dr, dr_entry) == NULL)
997 dr = TAILQ_FIRST(&V_nd6_defrouter);
999 dr = TAILQ_NEXT(installed_dr, dr_entry);
1001 /* Ensure we select a router for this FIB. */
1002 TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1003 if (dr->ifp->if_fib == fibnum) {
1005 defrouter_ref(selected_dr);
1009 } else if (installed_dr != NULL) {
1010 NET_EPOCH_ENTER(et);
1011 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
1012 installed_dr->ifp)) &&
1013 ND6_IS_LLINFO_PROBREACH(ln) &&
1014 installed_dr->ifp->if_fib == fibnum &&
1015 rtpref(selected_dr) <= rtpref(installed_dr)) {
1016 defrouter_rele(selected_dr);
1017 selected_dr = installed_dr;
1025 NET_EPOCH_ENTER(et);
1027 * If we selected a router for this FIB and it's different
1028 * than the installed one, remove the installed router and
1029 * install the selected one in its place.
1031 if (installed_dr != selected_dr) {
1032 if (installed_dr != NULL) {
1033 defrouter_delreq(installed_dr);
1034 defrouter_rele(installed_dr);
1036 if (selected_dr != NULL)
1037 defrouter_addreq(selected_dr);
1039 if (selected_dr != NULL)
1040 defrouter_rele(selected_dr);
1044 static struct nd_defrouter *
1045 defrtrlist_update(struct nd_defrouter *new)
1047 struct nd_defrouter *dr, *n;
1052 if (new->rtlifetime == 0) {
1053 defrouter_remove(&new->rtaddr, new->ifp);
1058 writelocked = false;
1060 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1062 oldpref = rtpref(dr);
1065 dr->raflags = new->raflags; /* XXX flag check */
1066 dr->rtlifetime = new->rtlifetime;
1067 dr->expire = new->expire;
1070 * If the preference does not change, there's no need
1071 * to sort the entries. Also make sure the selected
1072 * router is still installed in the kernel.
1074 if (dr->installed && rtpref(new) == oldpref) {
1084 * The router needs to be reinserted into the default router
1085 * list, so upgrade to a write lock. If that fails and the list
1086 * has potentially changed while the lock was dropped, we'll
1087 * redo the lookup with the write lock held.
1091 if (!ND6_TRY_UPGRADE()) {
1092 genid = V_nd6_list_genid;
1095 if (genid != V_nd6_list_genid)
1102 * The preferred router may have changed, so relocate this
1105 TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1108 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
1113 memcpy(n, new, sizeof(*n));
1114 /* Initialize with an extra reference for the caller. */
1115 refcount_init(&n->refcnt, 2);
1119 * Insert the new router in the Default Router List;
1120 * The Default Router List should be in the descending order
1121 * of router-preferece. Routers with the same preference are
1122 * sorted in the arriving time order.
1125 /* insert at the end of the group */
1126 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1127 if (rtpref(n) > rtpref(dr))
1131 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1133 TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1137 defrouter_select_fib(new->ifp->if_fib);
1143 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1145 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1146 ndpr->ndpr_preferred = 0;
1148 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1149 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1150 ndpr->ndpr_expire = 0;
1152 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
1158 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1160 /* init ia6t_expire */
1161 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1162 lt6->ia6t_expire = 0;
1164 lt6->ia6t_expire = time_uptime;
1165 lt6->ia6t_expire += lt6->ia6t_vltime;
1168 /* init ia6t_preferred */
1169 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1170 lt6->ia6t_preferred = 0;
1172 lt6->ia6t_preferred = time_uptime;
1173 lt6->ia6t_preferred += lt6->ia6t_pltime;
1177 static struct in6_ifaddr *
1178 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1180 struct ifnet *ifp = pr->ndpr_ifp;
1182 struct in6_aliasreq ifra;
1183 struct in6_ifaddr *ia, *ib;
1185 struct in6_addr mask;
1186 int prefixlen = pr->ndpr_plen;
1188 char ip6buf[INET6_ADDRSTRLEN];
1190 in6_prefixlen2mask(&mask, prefixlen);
1193 * find a link-local address (will be interface ID).
1194 * Is it really mandatory? Theoretically, a global or a site-local
1195 * address can be configured without a link-local address, if we
1196 * have a unique interface identifier...
1198 * it is not mandatory to have a link-local address, we can generate
1199 * interface identifier on the fly. we do this because:
1200 * (1) it should be the easiest way to find interface identifier.
1201 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1202 * for multiple addresses on a single interface, and possible shortcut
1203 * of DAD. we omitted DAD for this reason in the past.
1204 * (3) a user can prevent autoconfiguration of global address
1205 * by removing link-local address by hand (this is partly because we
1206 * don't have other way to control the use of IPv6 on an interface.
1207 * this has been our design choice - cf. NRL's "ifconfig auto").
1208 * (4) it is easier to manage when an interface has addresses
1209 * with the same interface identifier, than to have multiple addresses
1210 * with different interface identifiers.
1212 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1214 ib = (struct in6_ifaddr *)ifa;
1218 /* prefixlen + ifidlen must be equal to 128 */
1219 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1220 if (prefixlen != plen0) {
1223 "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
1224 __func__, if_name(ifp), prefixlen, 128 - plen0));
1229 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1231 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1233 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1234 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1235 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1236 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1237 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1238 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1239 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1240 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1244 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1245 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1247 /* XXX: scope zone ID? */
1249 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1252 * Make sure that we do not have this address already. This should
1253 * usually not happen, but we can still see this case, e.g., if we
1254 * have manually configured the exact address to be configured.
1256 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1257 &ifra.ifra_addr.sin6_addr);
1260 /* this should be rare enough to make an explicit log */
1261 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1262 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1267 * Allocate ifaddr structure, link into chain, etc.
1268 * If we are going to create a new address upon receiving a multicasted
1269 * RA, we need to impose a random delay before starting DAD.
1270 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1274 updateflags |= IN6_IFAUPDATE_DADDELAY;
1275 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1277 "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__,
1278 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1279 if_name(ifp), error));
1280 return (NULL); /* ifaddr must not have been allocated. */
1283 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1285 * XXXRW: Assumption of non-NULLness here might not be true with
1286 * fine-grained locking -- should we validate it? Or just return
1287 * earlier ifa rather than looking it up again?
1289 return (ia); /* this is always non-NULL and referenced. */
1292 static struct nd_prefix *
1293 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1295 struct nd_prefix *search;
1299 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1300 if (key->ndpr_ifp == search->ndpr_ifp &&
1301 key->ndpr_plen == search->ndpr_plen &&
1302 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1303 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1304 nd6_prefix_ref(search);
1312 nd6_prefix_lookup(struct nd_prefixctl *key)
1314 struct nd_prefix *search;
1317 search = nd6_prefix_lookup_locked(key);
1323 nd6_prefix_ref(struct nd_prefix *pr)
1326 refcount_acquire(&pr->ndpr_refcnt);
1330 nd6_prefix_rele(struct nd_prefix *pr)
1333 if (refcount_release(&pr->ndpr_refcnt)) {
1334 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1335 ("prefix %p has advertising routers", pr));
1341 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
1342 struct nd_prefix **newp)
1344 struct nd_prefix *new;
1345 char ip6buf[INET6_ADDRSTRLEN];
1348 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1351 refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1352 new->ndpr_ifp = pr->ndpr_ifp;
1353 new->ndpr_prefix = pr->ndpr_prefix;
1354 new->ndpr_plen = pr->ndpr_plen;
1355 new->ndpr_vltime = pr->ndpr_vltime;
1356 new->ndpr_pltime = pr->ndpr_pltime;
1357 new->ndpr_flags = pr->ndpr_flags;
1358 if ((error = in6_init_prefix_ltimes(new)) != 0) {
1359 free(new, M_IP6NDP);
1362 new->ndpr_lastupdate = time_uptime;
1364 /* initialization */
1365 LIST_INIT(&new->ndpr_advrtrs);
1366 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1367 /* make prefix in the canonical form */
1368 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
1371 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1375 /* ND_OPT_PI_FLAG_ONLINK processing */
1376 if (new->ndpr_raf_onlink) {
1377 struct epoch_tracker et;
1380 NET_EPOCH_ENTER(et);
1381 if ((error = nd6_prefix_onlink(new)) != 0) {
1382 nd6log((LOG_ERR, "%s: failed to make the prefix %s/%d "
1383 "on-link on %s (errno=%d)\n", __func__,
1384 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1385 pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
1386 /* proceed anyway. XXX: is it correct? */
1389 ND6_ONLINK_UNLOCK();
1393 pfxrtr_add(new, dr);
1400 * Remove a prefix from the prefix list and optionally stash it in a
1401 * caller-provided list.
1403 * The ND6 lock must be held.
1406 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1411 LIST_REMOVE(pr, ndpr_entry);
1414 LIST_INSERT_HEAD(list, pr, ndpr_entry);
1418 * Free an unlinked prefix, first marking it off-link if necessary.
1421 nd6_prefix_del(struct nd_prefix *pr)
1423 struct nd_pfxrouter *pfr, *next;
1425 char ip6buf[INET6_ADDRSTRLEN];
1427 KASSERT(pr->ndpr_addrcnt == 0,
1428 ("prefix %p has referencing addresses", pr));
1429 ND6_UNLOCK_ASSERT();
1432 * Though these flags are now meaningless, we'd rather keep the value
1433 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1434 * when executing "ndp -p".
1436 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1438 if ((e = nd6_prefix_offlink(pr)) != 0) {
1440 "%s: failed to make the prefix %s/%d offlink on %s "
1441 "(errno=%d)\n", __func__,
1442 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1443 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1444 /* what should we do? */
1446 ND6_ONLINK_UNLOCK();
1449 /* Release references to routers that have advertised this prefix. */
1451 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1455 nd6_prefix_rele(pr);
1457 pfxlist_onlink_check();
1461 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1462 struct mbuf *m, int mcast)
1464 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1466 struct ifnet *ifp = new->ndpr_ifp;
1467 struct nd_prefix *pr;
1470 struct in6_addrlifetime lt6_tmp;
1471 char ip6buf[INET6_ADDRSTRLEN];
1478 * Authenticity for NA consists authentication for
1479 * both IP header and IP datagrams, doesn't it ?
1481 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1482 auth = ((m->m_flags & M_AUTHIPHDR) &&
1483 (m->m_flags & M_AUTHIPDGM));
1487 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1489 * nd6_prefix_lookup() ensures that pr and new have the same
1490 * prefix on a same interface.
1494 * Update prefix information. Note that the on-link (L) bit
1495 * and the autonomous (A) bit should NOT be changed from 1
1498 if (new->ndpr_raf_onlink == 1)
1499 pr->ndpr_raf_onlink = 1;
1500 if (new->ndpr_raf_auto == 1)
1501 pr->ndpr_raf_auto = 1;
1502 if (new->ndpr_raf_onlink) {
1503 pr->ndpr_vltime = new->ndpr_vltime;
1504 pr->ndpr_pltime = new->ndpr_pltime;
1505 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1506 pr->ndpr_lastupdate = time_uptime;
1509 if (new->ndpr_raf_onlink &&
1510 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1512 if ((error = nd6_prefix_onlink(pr)) != 0) {
1514 "%s: failed to make the prefix %s/%d "
1515 "on-link on %s (errno=%d)\n", __func__,
1517 &pr->ndpr_prefix.sin6_addr),
1518 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1520 /* proceed anyway. XXX: is it correct? */
1522 ND6_ONLINK_UNLOCK();
1528 if (new->ndpr_vltime == 0)
1530 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1533 error = nd6_prelist_add(new, dr, &pr);
1535 nd6log((LOG_NOTICE, "%s: nd6_prelist_add() failed for "
1536 "the prefix %s/%d on %s (errno=%d)\n", __func__,
1537 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1538 new->ndpr_plen, if_name(new->ndpr_ifp), error));
1539 goto end; /* we should just give up in this case. */
1543 * XXX: from the ND point of view, we can ignore a prefix
1544 * with the on-link bit being zero. However, we need a
1545 * prefix structure for references from autoconfigured
1546 * addresses. Thus, we explicitly make sure that the prefix
1547 * itself expires now.
1549 if (pr->ndpr_raf_onlink == 0) {
1550 pr->ndpr_vltime = 0;
1551 pr->ndpr_pltime = 0;
1552 in6_init_prefix_ltimes(pr);
1557 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1558 * Note that pr must be non NULL at this point.
1561 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1562 if (!new->ndpr_raf_auto)
1566 * 5.5.3 (b). the link-local prefix should have been ignored in
1570 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1571 if (new->ndpr_pltime > new->ndpr_vltime) {
1572 error = EINVAL; /* XXX: won't be used */
1577 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1578 * an address configured by stateless autoconfiguration already in the
1579 * list of addresses associated with the interface, and the Valid
1580 * Lifetime is not 0, form an address. We first check if we have
1581 * a matching prefix.
1582 * Note: we apply a clarification in rfc2462bis-02 here. We only
1583 * consider autoconfigured addresses while RFC2462 simply said
1586 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1587 struct in6_ifaddr *ifa6;
1588 u_int32_t remaininglifetime;
1590 if (ifa->ifa_addr->sa_family != AF_INET6)
1593 ifa6 = (struct in6_ifaddr *)ifa;
1596 * We only consider autoconfigured addresses as per rfc2462bis.
1598 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1602 * Spec is not clear here, but I believe we should concentrate
1603 * on unicast (i.e. not anycast) addresses.
1604 * XXX: other ia6_flags? detached or duplicated?
1606 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1610 * Ignore the address if it is not associated with a prefix
1611 * or is associated with a prefix that is different from this
1612 * one. (pr is never NULL here)
1614 if (ifa6->ia6_ndpr != pr)
1617 if (ia6_match == NULL) /* remember the first one */
1621 * An already autoconfigured address matched. Now that we
1622 * are sure there is at least one matched address, we can
1623 * proceed to 5.5.3. (e): update the lifetimes according to the
1624 * "two hours" rule and the privacy extension.
1625 * We apply some clarifications in rfc2462bis:
1626 * - use remaininglifetime instead of storedlifetime as a
1628 * - remove the dead code in the "two-hour" rule
1630 #define TWOHOUR (120*60)
1631 lt6_tmp = ifa6->ia6_lifetime;
1633 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1634 remaininglifetime = ND6_INFINITE_LIFETIME;
1635 else if (time_uptime - ifa6->ia6_updatetime >
1636 lt6_tmp.ia6t_vltime) {
1638 * The case of "invalid" address. We should usually
1639 * not see this case.
1641 remaininglifetime = 0;
1643 remaininglifetime = lt6_tmp.ia6t_vltime -
1644 (time_uptime - ifa6->ia6_updatetime);
1646 /* when not updating, keep the current stored lifetime. */
1647 lt6_tmp.ia6t_vltime = remaininglifetime;
1649 if (TWOHOUR < new->ndpr_vltime ||
1650 remaininglifetime < new->ndpr_vltime) {
1651 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1652 } else if (remaininglifetime <= TWOHOUR) {
1654 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1658 * new->ndpr_vltime <= TWOHOUR &&
1659 * TWOHOUR < remaininglifetime
1661 lt6_tmp.ia6t_vltime = TWOHOUR;
1664 /* The 2 hour rule is not imposed for preferred lifetime. */
1665 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1667 in6_init_address_ltimes(pr, <6_tmp);
1670 * We need to treat lifetimes for temporary addresses
1671 * differently, according to
1672 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1673 * we only update the lifetimes when they are in the maximum
1676 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1677 u_int32_t maxvltime, maxpltime;
1679 if (V_ip6_temp_valid_lifetime >
1680 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1681 V_ip6_desync_factor)) {
1682 maxvltime = V_ip6_temp_valid_lifetime -
1683 (time_uptime - ifa6->ia6_createtime) -
1684 V_ip6_desync_factor;
1687 if (V_ip6_temp_preferred_lifetime >
1688 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1689 V_ip6_desync_factor)) {
1690 maxpltime = V_ip6_temp_preferred_lifetime -
1691 (time_uptime - ifa6->ia6_createtime) -
1692 V_ip6_desync_factor;
1696 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1697 lt6_tmp.ia6t_vltime > maxvltime) {
1698 lt6_tmp.ia6t_vltime = maxvltime;
1700 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1701 lt6_tmp.ia6t_pltime > maxpltime) {
1702 lt6_tmp.ia6t_pltime = maxpltime;
1705 ifa6->ia6_lifetime = lt6_tmp;
1706 ifa6->ia6_updatetime = time_uptime;
1708 if (ia6_match == NULL && new->ndpr_vltime) {
1712 * 5.5.3 (d) (continued)
1713 * No address matched and the valid lifetime is non-zero.
1714 * Create a new address.
1718 * Prefix Length check:
1719 * If the sum of the prefix length and interface identifier
1720 * length does not equal 128 bits, the Prefix Information
1721 * option MUST be ignored. The length of the interface
1722 * identifier is defined in a separate link-type specific
1725 ifidlen = in6_if2idlen(ifp);
1727 /* this should not happen, so we always log it. */
1728 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1732 if (ifidlen + pr->ndpr_plen != 128) {
1734 "%s: invalid prefixlen %d for %s, ignored\n",
1735 __func__, pr->ndpr_plen, if_name(ifp)));
1739 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1741 * note that we should use pr (not new) for reference.
1748 * When a new public address is created as described
1749 * in RFC2462, also create a new temporary address.
1752 * When an interface connects to a new link, a new
1753 * randomized interface identifier should be generated
1754 * immediately together with a new set of temporary
1755 * addresses. Thus, we specifiy 1 as the 2nd arg of
1758 if (V_ip6_use_tempaddr) {
1760 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1761 nd6log((LOG_NOTICE, "%s: failed to "
1762 "create a temporary address "
1763 "(errno=%d)\n", __func__, e));
1766 ifa_free(&ia6->ia_ifa);
1769 * A newly added address might affect the status
1770 * of other addresses, so we check and update it.
1771 * XXX: what if address duplication happens?
1773 pfxlist_onlink_check();
1775 /* just set an error. do not bark here. */
1776 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1782 nd6_prefix_rele(pr);
1787 * A supplement function used in the on-link detection below;
1788 * detect if a given prefix has a (probably) reachable advertising router.
1789 * XXX: lengthy function name...
1791 static struct nd_pfxrouter *
1792 find_pfxlist_reachable_router(struct nd_prefix *pr)
1794 struct epoch_tracker et;
1795 struct nd_pfxrouter *pfxrtr;
1801 NET_EPOCH_ENTER(et);
1802 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1803 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1806 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1816 * Check if each prefix in the prefix list has at least one available router
1817 * that advertised the prefix (a router is "available" if its neighbor cache
1818 * entry is reachable or probably reachable).
1819 * If the check fails, the prefix may be off-link, because, for example,
1820 * we have moved from the network but the lifetime of the prefix has not
1821 * expired yet. So we should not use the prefix if there is another prefix
1822 * that has an available router.
1823 * But, if there is no prefix that has an available router, we still regard
1824 * all the prefixes as on-link. This is because we can't tell if all the
1825 * routers are simply dead or if we really moved from the network and there
1826 * is no router around us.
1829 pfxlist_onlink_check(void)
1831 struct nd_prefix *pr;
1832 struct in6_ifaddr *ifa;
1833 struct nd_defrouter *dr;
1834 struct nd_pfxrouter *pfxrtr = NULL;
1835 struct rm_priotracker in6_ifa_tracker;
1843 * Check if there is a prefix that has a reachable advertising
1846 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1847 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1852 * If we have no such prefix, check whether we still have a router
1853 * that does not advertise any prefixes.
1856 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1857 struct nd_prefix *pr0;
1859 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1860 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1867 if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1869 * There is at least one prefix that has a reachable router,
1870 * or at least a router which probably does not advertise
1871 * any prefixes. The latter would be the case when we move
1872 * to a new link where we have a router that does not provide
1873 * prefixes and we configure an address by hand.
1874 * Detach prefixes which have no reachable advertising
1875 * router, and attach other prefixes.
1877 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1878 /* XXX: a link-local prefix should never be detached */
1879 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1880 pr->ndpr_raf_onlink == 0 ||
1881 pr->ndpr_raf_auto == 0)
1884 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1885 find_pfxlist_reachable_router(pr) == NULL)
1886 pr->ndpr_stateflags |= NDPRF_DETACHED;
1887 else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1888 find_pfxlist_reachable_router(pr) != NULL)
1889 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1892 /* there is no prefix that has a reachable router */
1893 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1894 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1895 pr->ndpr_raf_onlink == 0 ||
1896 pr->ndpr_raf_auto == 0)
1898 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1903 * Remove each interface route associated with a (just) detached
1904 * prefix, and reinstall the interface route for a (just) attached
1905 * prefix. Note that all attempt of reinstallation does not
1906 * necessarily success, when a same prefix is shared among multiple
1907 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1908 * so we don't have to care about them.
1911 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1912 char ip6buf[INET6_ADDRSTRLEN];
1915 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1916 pr->ndpr_raf_onlink == 0 ||
1917 pr->ndpr_raf_auto == 0)
1920 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1921 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1922 genid = V_nd6_list_genid;
1924 if ((flags & NDPRF_ONLINK) != 0 &&
1925 (e = nd6_prefix_offlink(pr)) != 0) {
1927 "%s: failed to make %s/%d offlink "
1928 "(errno=%d)\n", __func__,
1930 &pr->ndpr_prefix.sin6_addr),
1932 } else if ((flags & NDPRF_ONLINK) == 0 &&
1933 (e = nd6_prefix_onlink(pr)) != 0) {
1935 "%s: failed to make %s/%d onlink "
1936 "(errno=%d)\n", __func__,
1938 &pr->ndpr_prefix.sin6_addr),
1942 if (genid != V_nd6_list_genid)
1948 * Changes on the prefix status might affect address status as well.
1949 * Make sure that all addresses derived from an attached prefix are
1950 * attached, and that all addresses derived from a detached prefix are
1951 * detached. Note, however, that a manually configured address should
1952 * always be attached.
1953 * The precise detection logic is same as the one for prefixes.
1955 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1956 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1957 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1960 if (ifa->ia6_ndpr == NULL) {
1962 * This can happen when we first configure the address
1963 * (i.e. the address exists, but the prefix does not).
1964 * XXX: complicated relationships...
1969 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1973 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1974 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1977 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1980 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1981 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1982 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1983 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1984 nd6_dad_start((struct ifaddr *)ifa, 0);
1987 ifa->ia6_flags |= IN6_IFF_DETACHED;
1991 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1992 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1995 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1996 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1997 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1998 /* Do we need a delay in this case? */
1999 nd6_dad_start((struct ifaddr *)ifa, 0);
2003 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2005 ND6_ONLINK_UNLOCK();
2009 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
2011 struct sockaddr_dl_short sdl;
2013 struct sockaddr_in6 mask6;
2015 int error, a_failure, fibnum, maxfib;
2017 bzero(&mask6, sizeof(mask6));
2018 mask6.sin6_len = sizeof(mask6);
2019 mask6.sin6_addr = pr->ndpr_mask;
2020 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
2022 bzero(&sdl, sizeof(struct sockaddr_dl_short));
2023 sdl.sdl_len = sizeof(struct sockaddr_dl_short);
2024 sdl.sdl_family = AF_LINK;
2025 sdl.sdl_type = ifa->ifa_ifp->if_type;
2026 sdl.sdl_index = ifa->ifa_ifp->if_index;
2028 if(V_rt_add_addr_allfibs) {
2030 maxfib = rt_numfibs;
2032 fibnum = ifa->ifa_ifp->if_fib;
2033 maxfib = fibnum + 1;
2036 for (; fibnum < maxfib; fibnum++) {
2039 error = in6_rtrequest(RTM_ADD,
2040 (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
2041 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
2043 char ip6buf[INET6_ADDRSTRLEN];
2044 char ip6bufg[INET6_ADDRSTRLEN];
2045 char ip6bufm[INET6_ADDRSTRLEN];
2046 struct sockaddr_in6 *sin6;
2048 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2049 nd6log((LOG_ERR, "%s: failed to add "
2050 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
2051 "flags=%lx errno = %d\n", __func__,
2052 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2053 pr->ndpr_plen, if_name(pr->ndpr_ifp),
2054 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
2055 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
2058 /* Save last error to return, see rtinit(). */
2063 pr->ndpr_stateflags |= NDPRF_ONLINK;
2064 rt_routemsg(RTM_ADD, rt, pr->ndpr_ifp, 0, fibnum);
2067 /* Return the last error we got. */
2072 nd6_prefix_onlink(struct nd_prefix *pr)
2074 struct epoch_tracker et;
2076 struct ifnet *ifp = pr->ndpr_ifp;
2077 struct nd_prefix *opr;
2078 char ip6buf[INET6_ADDRSTRLEN];
2081 ND6_ONLINK_LOCK_ASSERT();
2082 ND6_UNLOCK_ASSERT();
2084 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
2088 * Add the interface route associated with the prefix. Before
2089 * installing the route, check if there's the same prefix on another
2090 * interface, and the prefix has already installed the interface route.
2091 * Although such a configuration is expected to be rare, we explicitly
2095 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2099 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2102 if (!V_rt_add_addr_allfibs &&
2103 opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2106 if (opr->ndpr_plen == pr->ndpr_plen &&
2107 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2108 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2116 * We prefer link-local addresses as the associated interface address.
2118 /* search for a link-local addr */
2119 NET_EPOCH_ENTER(et);
2120 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
2121 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
2123 /* XXX: freebsd does not have ifa_ifwithaf */
2124 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2125 if (ifa->ifa_addr->sa_family == AF_INET6) {
2130 /* should we care about ia6_flags? */
2134 * This can still happen, when, for example, we receive an RA
2135 * containing a prefix with the L bit set and the A bit clear,
2136 * after removing all IPv6 addresses on the receiving
2137 * interface. This should, of course, be rare though.
2140 "%s: failed to find any ifaddr to add route for a "
2141 "prefix(%s/%d) on %s\n", __func__,
2142 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2143 pr->ndpr_plen, if_name(ifp)));
2146 error = nd6_prefix_onlink_rtrequest(pr, ifa);
2155 nd6_prefix_offlink(struct nd_prefix *pr)
2158 struct ifnet *ifp = pr->ndpr_ifp;
2159 struct nd_prefix *opr;
2160 struct sockaddr_in6 sa6, mask6;
2162 char ip6buf[INET6_ADDRSTRLEN];
2164 int fibnum, maxfib, a_failure;
2165 struct epoch_tracker et;
2167 ND6_ONLINK_LOCK_ASSERT();
2168 ND6_UNLOCK_ASSERT();
2170 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2173 bzero(&sa6, sizeof(sa6));
2174 sa6.sin6_family = AF_INET6;
2175 sa6.sin6_len = sizeof(sa6);
2176 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
2177 sizeof(struct in6_addr));
2178 bzero(&mask6, sizeof(mask6));
2179 mask6.sin6_family = AF_INET6;
2180 mask6.sin6_len = sizeof(sa6);
2181 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
2183 if (V_rt_add_addr_allfibs) {
2185 maxfib = rt_numfibs;
2187 fibnum = ifp->if_fib;
2188 maxfib = fibnum + 1;
2192 NET_EPOCH_ENTER(et);
2193 for (; fibnum < maxfib; fibnum++) {
2195 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
2196 (struct sockaddr *)&mask6, 0, &rt, fibnum);
2198 /* Save last error to return, see rtinit(). */
2203 /* report route deletion to the routing socket. */
2204 rt_routemsg(RTM_DELETE, rt, ifp, 0, fibnum);
2210 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
2213 * There might be the same prefix on another interface,
2214 * the prefix which could not be on-link just because we have
2215 * the interface route (see comments in nd6_prefix_onlink).
2216 * If there's one, try to make the prefix on-link on the
2221 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2223 * KAME specific: detached prefixes should not be
2226 if (opr == pr || (opr->ndpr_stateflags &
2227 (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
2230 if (opr->ndpr_plen == pr->ndpr_plen &&
2231 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2232 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2235 genid = V_nd6_list_genid;
2237 if ((e = nd6_prefix_onlink(opr)) != 0) {
2239 "%s: failed to recover a prefix "
2240 "%s/%d from %s to %s (errno=%d)\n",
2241 __func__, ip6_sprintf(ip6buf,
2242 &opr->ndpr_prefix.sin6_addr),
2243 opr->ndpr_plen, if_name(ifp),
2244 if_name(opr->ndpr_ifp), e));
2248 if (genid != V_nd6_list_genid)
2254 /* XXX: can we still set the NDPRF_ONLINK flag? */
2256 "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
2257 __func__, ip6_sprintf(ip6buf, &sa6.sin6_addr),
2258 pr->ndpr_plen, if_name(ifp), error));
2262 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2263 (struct sockaddr *)&mask6, LLE_STATIC);
2269 * ia0 - corresponding public address
2272 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2274 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2275 struct in6_ifaddr *newia;
2276 struct in6_aliasreq ifra;
2278 int trylimit = 3; /* XXX: adhoc value */
2280 u_int32_t randid[2];
2281 time_t vltime0, pltime0;
2283 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
2284 &ia0->ia_prefixmask.sin6_addr);
2286 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
2287 /* clear the old IFID */
2288 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
2289 &ifra.ifra_prefixmask.sin6_addr);
2292 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2293 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2294 nd6log((LOG_NOTICE, "%s: failed to find a good random IFID\n",
2298 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2299 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2300 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2301 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2304 * in6_get_tmpifid() quite likely provided a unique interface ID.
2305 * However, we may still have a chance to see collision, because
2306 * there may be a time lag between generation of the ID and generation
2307 * of the address. So, we'll do one more sanity check.
2310 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
2311 if (trylimit-- > 0) {
2316 /* Give up. Something strange should have happened. */
2317 nd6log((LOG_NOTICE, "%s: failed to find a unique random IFID\n",
2323 * The Valid Lifetime is the lower of the Valid Lifetime of the
2324 * public address or TEMP_VALID_LIFETIME.
2325 * The Preferred Lifetime is the lower of the Preferred Lifetime
2326 * of the public address or TEMP_PREFERRED_LIFETIME -
2329 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2330 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2331 (ia0->ia6_lifetime.ia6t_vltime -
2332 (time_uptime - ia0->ia6_updatetime));
2333 if (vltime0 > V_ip6_temp_valid_lifetime)
2334 vltime0 = V_ip6_temp_valid_lifetime;
2336 vltime0 = V_ip6_temp_valid_lifetime;
2337 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2338 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2339 (ia0->ia6_lifetime.ia6t_pltime -
2340 (time_uptime - ia0->ia6_updatetime));
2341 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2342 pltime0 = V_ip6_temp_preferred_lifetime -
2343 V_ip6_desync_factor;
2346 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2347 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2348 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2351 * A temporary address is created only if this calculated Preferred
2352 * Lifetime is greater than REGEN_ADVANCE time units.
2354 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2357 /* XXX: scope zone ID? */
2359 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2361 /* allocate ifaddr structure, link into chain, etc. */
2364 updateflags |= IN6_IFAUPDATE_DADDELAY;
2365 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2368 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2369 if (newia == NULL) { /* XXX: can it happen? */
2371 "%s: ifa update succeeded, but we got no ifaddr\n",
2373 return (EINVAL); /* XXX */
2375 newia->ia6_ndpr = ia0->ia6_ndpr;
2376 newia->ia6_ndpr->ndpr_addrcnt++;
2377 ifa_free(&newia->ia_ifa);
2380 * A newly added address might affect the status of other addresses.
2381 * XXX: when the temporary address is generated with a new public
2382 * address, the onlink check is redundant. However, it would be safe
2383 * to do the check explicitly everywhere a new address is generated,
2384 * and, in fact, we surely need the check when we create a new
2385 * temporary address due to deprecation of an old temporary address.
2387 pfxlist_onlink_check();
2393 rt6_deleteroute(const struct rtentry *rt, const struct nhop_object *nh,
2396 struct in6_addr *gate = (struct in6_addr *)arg;
2399 if (nh->gw_sa.sa_family != AF_INET6)
2402 if (!IN6_ARE_ADDR_EQUAL(gate, &nh->gw6_sa.sin6_addr)) {
2407 * Do not delete a static route.
2408 * XXX: this seems to be a bit ad-hoc. Should we consider the
2409 * 'cloned' bit instead?
2411 nh_rt_flags = nhop_get_rtflags(nh);
2412 if ((nh_rt_flags & RTF_STATIC) != 0)
2416 * We delete only host route. This means, in particular, we don't
2417 * delete default route.
2419 if ((nh_rt_flags & RTF_HOST) == 0)
2427 * Delete all the routing table entries that use the specified gateway.
2428 * XXX: this function causes search through all entries of routing table, so
2429 * it shouldn't be called when acting as a router.
2432 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2435 /* We'll care only link-local addresses */
2436 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2439 /* XXX Do we really need to walk any but the default FIB? */
2440 rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2444 nd6_setdefaultiface(int ifindex)
2448 if (ifindex < 0 || V_if_index < ifindex)
2450 if (ifindex != 0 && !ifnet_byindex(ifindex))
2453 if (V_nd6_defifindex != ifindex) {
2454 V_nd6_defifindex = ifindex;
2455 if (V_nd6_defifindex > 0)
2456 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2458 V_nd6_defifp = NULL;
2461 * Our current implementation assumes one-to-one maping between
2462 * interfaces and links, so it would be natural to use the
2463 * default interface as the default link.
2465 scope6_setdefault(V_nd6_defifp);
2472 nd6_defrouter_list_empty(void)
2475 return (TAILQ_EMPTY(&V_nd6_defrouter));
2479 nd6_defrouter_timer(void)
2481 struct nd_defrouter *dr, *ndr;
2482 struct nd6_drhead drq;
2487 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2488 if (dr->expire && dr->expire < time_uptime)
2489 defrouter_unlink(dr, &drq);
2492 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2493 TAILQ_REMOVE(&drq, dr, dr_entry);
2499 * Nuke default router list entries toward ifp.
2500 * We defer removal of default router list entries that is installed in the
2501 * routing table, in order to keep additional side effects as small as possible.
2504 nd6_defrouter_purge(struct ifnet *ifp)
2506 struct nd_defrouter *dr, *ndr;
2507 struct nd6_drhead drq;
2512 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2516 defrouter_unlink(dr, &drq);
2518 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2522 defrouter_unlink(dr, &drq);
2526 /* Delete the unlinked router objects. */
2527 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2528 TAILQ_REMOVE(&drq, dr, dr_entry);
2534 nd6_defrouter_flush_all(void)
2536 struct nd_defrouter *dr;
2537 struct nd6_drhead drq;
2542 while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2543 defrouter_unlink(dr, &drq);
2546 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2547 TAILQ_REMOVE(&drq, dr, dr_entry);
2553 nd6_defrouter_init(void)
2556 TAILQ_INIT(&V_nd6_defrouter);
2560 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2562 struct in6_defrouter d;
2563 struct nd_defrouter *dr;
2566 if (req->newptr != NULL)
2569 error = sysctl_wire_old_buffer(req, 0);
2573 bzero(&d, sizeof(d));
2574 d.rtaddr.sin6_family = AF_INET6;
2575 d.rtaddr.sin6_len = sizeof(d.rtaddr);
2578 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2579 d.rtaddr.sin6_addr = dr->rtaddr;
2580 error = sa6_recoverscope(&d.rtaddr);
2583 d.flags = dr->raflags;
2584 d.rtlifetime = dr->rtlifetime;
2585 d.expire = dr->expire + (time_second - time_uptime);
2586 d.if_index = dr->ifp->if_index;
2587 error = SYSCTL_OUT(req, &d, sizeof(d));
2594 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2595 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2596 NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2597 "NDP default router list");