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/radix.h>
67 #include <netinet/in.h>
68 #include <net/if_llatbl.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/in6_ifattach.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet/icmp6.h>
75 #include <netinet6/scope6_var.h>
77 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
78 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
80 static int nd6_prefix_onlink(struct nd_prefix *);
82 TAILQ_HEAD(nd6_drhead, nd_defrouter);
83 VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
84 #define V_nd6_defrouter VNET(nd6_defrouter)
86 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
87 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
89 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
90 VNET_DEFINE(int, nd6_defifindex);
91 #define V_nd6_defifp VNET(nd6_defifp)
93 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
95 VNET_DEFINE(int, ip6_desync_factor);
96 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
97 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
99 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
102 VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
105 SYSCTL_DECL(_net_inet6_icmp6);
107 /* RTPREF_MEDIUM has to be 0! */
108 #define RTPREF_HIGH 1
109 #define RTPREF_MEDIUM 0
110 #define RTPREF_LOW (-1)
111 #define RTPREF_RESERVED (-2)
112 #define RTPREF_INVALID (-3) /* internal */
115 defrouter_ref(struct nd_defrouter *dr)
118 refcount_acquire(&dr->refcnt);
122 defrouter_rele(struct nd_defrouter *dr)
125 if (refcount_release(&dr->refcnt))
130 * Remove a router from the global list and optionally stash it in a
131 * caller-supplied queue.
134 defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
139 TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
142 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
146 * Receive Router Solicitation Message - just for routers.
147 * Router solicitation/advertisement is mostly managed by userland program
148 * (rtadvd) so here we have no function like nd6_ra_output().
153 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
157 struct nd_router_solicit *nd_rs;
158 struct in6_addr saddr6;
159 union nd_opts ndopts;
160 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
164 ifp = m->m_pkthdr.rcvif;
167 * Accept RS only when V_ip6_forwarding=1 and the interface has
168 * no ND6_IFF_ACCEPT_RTADV.
170 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
173 /* RFC 6980: Nodes MUST silently ignore fragments */
174 if(m->m_flags & M_FRAGMENTED)
178 ip6 = mtod(m, struct ip6_hdr *);
179 if (__predict_false(ip6->ip6_hlim != 255)) {
180 ICMP6STAT_INC(icp6s_invlhlim);
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 (__predict_false(ip6->ip6_hlim != 255)) {
380 ICMP6STAT_INC(icp6s_invlhlim);
382 "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
383 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
384 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
388 saddr6 = ip6->ip6_src;
389 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
391 "%s: src %s is not link-local\n", __func__,
392 ip6_sprintf(ip6bufs, &saddr6)));
396 if (m->m_len < off + icmp6len) {
397 m = m_pullup(m, off + icmp6len);
399 IP6STAT_INC(ip6s_exthdrtoolong);
403 ip6 = mtod(m, struct ip6_hdr *);
404 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
406 icmp6len -= sizeof(*nd_ra);
407 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
408 if (nd6_options(&ndopts) < 0) {
410 "%s: invalid ND option, ignored\n", __func__));
411 /* nd6_options have incremented stats */
418 struct nd_defrouter dr0;
419 u_int32_t advreachable = nd_ra->nd_ra_reachable;
421 /* remember if this is a multicasted advertisement */
422 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
425 bzero(&dr0, sizeof(dr0));
427 dr0.raflags = nd_ra->nd_ra_flags_reserved;
429 * Effectively-disable routes from RA messages when
430 * ND6_IFF_NO_RADR enabled on the receiving interface or
431 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
433 if (ndi->flags & ND6_IFF_NO_RADR)
435 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
438 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
439 dr0.expire = time_uptime + dr0.rtlifetime;
441 /* unspecified or not? (RFC 2461 6.3.4) */
443 advreachable = ntohl(advreachable);
444 if (advreachable <= MAX_REACHABLE_TIME &&
445 ndi->basereachable != advreachable) {
446 ndi->basereachable = advreachable;
447 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
448 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
451 if (nd_ra->nd_ra_retransmit)
452 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
453 if (nd_ra->nd_ra_curhoplimit) {
454 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
455 ndi->chlim = nd_ra->nd_ra_curhoplimit;
456 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
457 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
458 "%s on %s (current = %d, received = %d). "
459 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
460 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
463 dr = defrtrlist_update(&dr0);
465 defrtr_ipv6_only_ifp(ifp);
472 if (ndopts.nd_opts_pi) {
473 struct nd_opt_hdr *pt;
474 struct nd_opt_prefix_info *pi = NULL;
475 struct nd_prefixctl pr;
477 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
478 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
479 pt = (struct nd_opt_hdr *)((caddr_t)pt +
480 (pt->nd_opt_len << 3))) {
481 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
483 pi = (struct nd_opt_prefix_info *)pt;
485 if (pi->nd_opt_pi_len != 4) {
487 "%s: invalid option len %d for prefix "
488 "information option, ignored\n", __func__,
493 if (128 < pi->nd_opt_pi_prefix_len) {
495 "%s: invalid prefix len %d for prefix "
496 "information option, ignored\n", __func__,
497 pi->nd_opt_pi_prefix_len));
501 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
502 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
504 "%s: invalid prefix %s, ignored\n",
505 __func__, ip6_sprintf(ip6bufs,
506 &pi->nd_opt_pi_prefix)));
510 bzero(&pr, sizeof(pr));
511 pr.ndpr_prefix.sin6_family = AF_INET6;
512 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
513 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
514 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
516 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
517 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
518 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
519 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
520 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
521 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
522 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
523 (void)prelist_update(&pr, dr, m, mcast);
534 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
538 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
541 if (mtu < IPV6_MMTU) {
542 nd6log((LOG_INFO, "%s: bogus mtu option mtu=%lu sent "
543 "from %s, ignoring\n", __func__,
544 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
549 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
550 ? ndi->maxmtu : ifp->if_mtu;
552 int change = (ndi->linkmtu != mtu);
556 /* in6_maxmtu may change */
561 nd6log((LOG_INFO, "%s: bogus mtu=%lu sent from %s; "
562 "exceeds maxmtu %lu, ignoring\n", __func__,
563 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
570 * Source link layer address
576 if (ndopts.nd_opts_src_lladdr) {
577 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
578 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
581 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
583 "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
584 __func__, ip6_sprintf(ip6bufs, &saddr6),
585 ifp->if_addrlen, lladdrlen - 2));
589 nd6_cache_lladdr(ifp, &saddr6, lladdr,
590 lladdrlen, ND_ROUTER_ADVERT, 0);
593 * Installing a link-layer address might change the state of the
594 * router's neighbor cache, which might also affect our on-link
595 * detection of adveritsed prefixes.
597 pfxlist_onlink_check();
605 ICMP6STAT_INC(icp6s_badra);
610 static struct nd_pfxrouter *
611 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
613 struct nd_pfxrouter *search;
617 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
618 if (search->router == dr)
625 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
627 struct nd_pfxrouter *new;
633 if (pfxrtr_lookup(pr, dr) != NULL) {
639 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
646 if (pfxrtr_lookup(pr, dr) == NULL) {
647 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
650 /* We lost a race to add the reference. */
658 pfxlist_onlink_check();
662 pfxrtr_del(struct nd_pfxrouter *pfr)
667 LIST_REMOVE(pfr, pfr_entry);
668 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 rt_addrinfo info;
678 struct rib_cmd_info rc;
682 bzero(&def, sizeof(def));
683 bzero(&mask, sizeof(mask));
684 bzero(&gate, sizeof(gate));
686 def.sin6_len = mask.sin6_len = gate.sin6_len =
687 sizeof(struct sockaddr_in6);
688 def.sin6_family = gate.sin6_family = AF_INET6;
689 gate.sin6_addr = new->rtaddr;
690 fibnum = new->ifp->if_fib;
692 bzero((caddr_t)&info, sizeof(info));
693 info.rti_flags = RTF_GATEWAY;
694 info.rti_info[RTAX_DST] = (struct sockaddr *)&def;
695 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gate;
696 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask;
699 error = rib_action(fibnum, RTM_ADD, &info, &rc);
701 struct nhop_object *nh = nhop_select_func(rc.rc_nh_new, 0);
702 rt_routemsg(RTM_ADD, rc.rc_rt, nh, fibnum);
708 * Remove the default route for a given router.
709 * This is just a subroutine function for defrouter_select_fib(), and
710 * should not be called from anywhere else.
713 defrouter_delreq(struct nd_defrouter *dr)
715 struct sockaddr_in6 def, mask, gate;
716 struct rt_addrinfo info;
717 struct rib_cmd_info rc;
718 struct epoch_tracker et;
722 bzero(&def, sizeof(def));
723 bzero(&mask, sizeof(mask));
724 bzero(&gate, sizeof(gate));
726 def.sin6_len = mask.sin6_len = gate.sin6_len =
727 sizeof(struct sockaddr_in6);
728 def.sin6_family = gate.sin6_family = AF_INET6;
729 gate.sin6_addr = dr->rtaddr;
730 fibnum = dr->ifp->if_fib;
732 bzero((caddr_t)&info, sizeof(info));
733 info.rti_flags = RTF_GATEWAY;
734 info.rti_info[RTAX_DST] = (struct sockaddr *)&def;
735 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gate;
736 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask;
739 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
741 struct nhop_object *nh = nhop_select_func(rc.rc_nh_old, 0);
742 rt_routemsg(RTM_DELETE, rc.rc_rt, nh, fibnum);
750 defrouter_del(struct nd_defrouter *dr)
752 struct nd_defrouter *deldr = NULL;
753 struct nd_prefix *pr;
754 struct nd_pfxrouter *pfxrtr;
759 * Flush all the routing table entries that use the router
762 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
763 rt6_flush(&dr->rtaddr, dr->ifp);
766 defrtr_ipv6_only_ifp(dr->ifp);
771 defrouter_delreq(dr);
775 * Also delete all the pointers to the router in each prefix lists.
778 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
779 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
784 pfxlist_onlink_check();
787 * If the router is the primary one, choose a new one.
788 * Note that defrouter_select_fib() will remove the current
789 * gateway from the routing table.
792 defrouter_select_fib(deldr->ifp->if_fib);
795 * Release the list reference.
800 struct nd_defrouter *
801 defrouter_lookup_locked(const struct in6_addr *addr, struct ifnet *ifp)
803 struct nd_defrouter *dr;
806 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
807 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
814 struct nd_defrouter *
815 defrouter_lookup(const struct in6_addr *addr, struct ifnet *ifp)
817 struct nd_defrouter *dr;
820 dr = defrouter_lookup_locked(addr, ifp);
826 * Remove all default routes from default router list.
829 defrouter_reset(void)
831 struct nd_defrouter *dr, **dra;
837 * We can't delete routes with the ND lock held, so make a copy of the
838 * current default router list and use that when deleting routes.
841 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
845 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
848 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
856 for (i = 0; i < count && dra[i] != NULL; i++) {
857 defrouter_delreq(dra[i]);
858 defrouter_rele(dra[i]);
863 * XXX should we also nuke any default routers in the kernel, by
864 * going through them by rtalloc1()?
869 * Look up a matching default router list entry and remove it. Returns true if a
870 * matching entry was found, false otherwise.
873 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
875 struct nd_defrouter *dr;
878 dr = defrouter_lookup_locked(addr, ifp);
884 defrouter_unlink(dr, NULL);
892 * for default router selection
893 * regards router-preference field as a 2-bit signed integer
896 rtpref(struct nd_defrouter *dr)
898 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
899 case ND_RA_FLAG_RTPREF_HIGH:
900 return (RTPREF_HIGH);
901 case ND_RA_FLAG_RTPREF_MEDIUM:
902 case ND_RA_FLAG_RTPREF_RSV:
903 return (RTPREF_MEDIUM);
904 case ND_RA_FLAG_RTPREF_LOW:
908 * This case should never happen. If it did, it would mean a
909 * serious bug of kernel internal. We thus always bark here.
910 * Or, can we even panic?
912 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
913 return (RTPREF_INVALID);
919 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
920 * draft-ietf-ipngwg-router-selection:
921 * 1) Routers that are reachable or probably reachable should be preferred.
922 * If we have more than one (probably) reachable router, prefer ones
923 * with the highest router preference.
924 * 2) When no routers on the list are known to be reachable or
925 * probably reachable, routers SHOULD be selected in a round-robin
926 * fashion, regardless of router preference values.
927 * 3) If the Default Router List is empty, assume that all
928 * destinations are on-link.
930 * We assume nd_defrouter is sorted by router preference value.
931 * Since the code below covers both with and without router preference cases,
932 * we do not need to classify the cases by ifdef.
934 * At this moment, we do not try to install more than one default router,
935 * even when the multipath routing is available, because we're not sure about
936 * the benefits for stub hosts comparing to the risk of making the code
937 * complicated and the possibility of introducing bugs.
939 * We maintain a single list of routers for multiple FIBs, only considering one
940 * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
941 * we do the whole thing multiple times.
944 defrouter_select_fib(int fibnum)
946 struct epoch_tracker et;
947 struct nd_defrouter *dr, *selected_dr, *installed_dr;
948 struct llentry *ln = NULL;
950 if (fibnum == RT_ALL_FIBS) {
951 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
952 defrouter_select_fib(fibnum);
958 * Let's handle easy case (3) first:
959 * If default router list is empty, there's nothing to be done.
961 if (TAILQ_EMPTY(&V_nd6_defrouter)) {
967 * Search for a (probably) reachable router from the list.
968 * We just pick up the first reachable one (if any), assuming that
969 * the ordering rule of the list described in defrtrlist_update().
971 selected_dr = installed_dr = NULL;
972 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
974 if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
975 (ln = nd6_lookup(&dr->rtaddr, LLE_SF(AF_INET6, 0), dr->ifp)) &&
976 ND6_IS_LLINFO_PROBREACH(ln)) {
978 defrouter_ref(selected_dr);
986 if (dr->installed && dr->ifp->if_fib == fibnum) {
987 if (installed_dr == NULL) {
989 defrouter_ref(installed_dr);
992 * this should not happen.
993 * warn for diagnosis.
995 log(LOG_ERR, "defrouter_select_fib: more than "
996 "one router is installed\n");
1001 * If none of the default routers was found to be reachable,
1002 * round-robin the list regardless of preference.
1003 * Otherwise, if we have an installed router, check if the selected
1004 * (reachable) router should really be preferred to the installed one.
1005 * We only prefer the new router when the old one is not reachable
1006 * or when the new one has a really higher preference value.
1008 if (selected_dr == NULL) {
1009 if (installed_dr == NULL ||
1010 TAILQ_NEXT(installed_dr, dr_entry) == NULL)
1011 dr = TAILQ_FIRST(&V_nd6_defrouter);
1013 dr = TAILQ_NEXT(installed_dr, dr_entry);
1015 /* Ensure we select a router for this FIB. */
1016 TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1017 if (dr->ifp->if_fib == fibnum) {
1019 defrouter_ref(selected_dr);
1023 } else if (installed_dr != NULL) {
1024 NET_EPOCH_ENTER(et);
1025 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
1026 installed_dr->ifp)) &&
1027 ND6_IS_LLINFO_PROBREACH(ln) &&
1028 installed_dr->ifp->if_fib == fibnum &&
1029 rtpref(selected_dr) <= rtpref(installed_dr)) {
1030 defrouter_rele(selected_dr);
1031 selected_dr = installed_dr;
1039 NET_EPOCH_ENTER(et);
1041 * If we selected a router for this FIB and it's different
1042 * than the installed one, remove the installed router and
1043 * install the selected one in its place.
1045 if (installed_dr != selected_dr) {
1046 if (installed_dr != NULL) {
1047 defrouter_delreq(installed_dr);
1048 defrouter_rele(installed_dr);
1050 if (selected_dr != NULL)
1051 defrouter_addreq(selected_dr);
1053 if (selected_dr != NULL)
1054 defrouter_rele(selected_dr);
1058 static struct nd_defrouter *
1059 defrtrlist_update(struct nd_defrouter *new)
1061 struct nd_defrouter *dr, *n;
1066 if (new->rtlifetime == 0) {
1067 defrouter_remove(&new->rtaddr, new->ifp);
1072 writelocked = false;
1074 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1076 oldpref = rtpref(dr);
1079 dr->raflags = new->raflags; /* XXX flag check */
1080 dr->rtlifetime = new->rtlifetime;
1081 dr->expire = new->expire;
1084 * If the preference does not change, there's no need
1085 * to sort the entries. Also make sure the selected
1086 * router is still installed in the kernel.
1088 if (dr->installed && rtpref(new) == oldpref) {
1098 * The router needs to be reinserted into the default router
1099 * list, so upgrade to a write lock. If that fails and the list
1100 * has potentially changed while the lock was dropped, we'll
1101 * redo the lookup with the write lock held.
1105 if (!ND6_TRY_UPGRADE()) {
1106 genid = V_nd6_list_genid;
1109 if (genid != V_nd6_list_genid)
1116 * The preferred router may have changed, so relocate this
1119 TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1122 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
1127 memcpy(n, new, sizeof(*n));
1128 /* Initialize with an extra reference for the caller. */
1129 refcount_init(&n->refcnt, 2);
1133 * Insert the new router in the Default Router List;
1134 * The Default Router List should be in the descending order
1135 * of router-preferece. Routers with the same preference are
1136 * sorted in the arriving time order.
1139 /* insert at the end of the group */
1140 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1141 if (rtpref(n) > rtpref(dr))
1145 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1147 TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1151 defrouter_select_fib(new->ifp->if_fib);
1157 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1159 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1160 ndpr->ndpr_preferred = 0;
1162 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1163 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1164 ndpr->ndpr_expire = 0;
1166 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
1172 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1174 /* init ia6t_expire */
1175 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1176 lt6->ia6t_expire = 0;
1178 lt6->ia6t_expire = time_uptime;
1179 lt6->ia6t_expire += lt6->ia6t_vltime;
1182 /* init ia6t_preferred */
1183 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1184 lt6->ia6t_preferred = 0;
1186 lt6->ia6t_preferred = time_uptime;
1187 lt6->ia6t_preferred += lt6->ia6t_pltime;
1191 static struct in6_ifaddr *
1192 in6_ifadd(struct nd_prefixctl *pr, int mcast)
1194 struct ifnet *ifp = pr->ndpr_ifp;
1196 struct in6_aliasreq ifra;
1197 struct in6_ifaddr *ia, *ib;
1199 struct in6_addr mask;
1200 int prefixlen = pr->ndpr_plen;
1202 char ip6buf[INET6_ADDRSTRLEN];
1204 in6_prefixlen2mask(&mask, prefixlen);
1207 * find a link-local address (will be interface ID).
1208 * Is it really mandatory? Theoretically, a global or a site-local
1209 * address can be configured without a link-local address, if we
1210 * have a unique interface identifier...
1212 * it is not mandatory to have a link-local address, we can generate
1213 * interface identifier on the fly. we do this because:
1214 * (1) it should be the easiest way to find interface identifier.
1215 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1216 * for multiple addresses on a single interface, and possible shortcut
1217 * of DAD. we omitted DAD for this reason in the past.
1218 * (3) a user can prevent autoconfiguration of global address
1219 * by removing link-local address by hand (this is partly because we
1220 * don't have other way to control the use of IPv6 on an interface.
1221 * this has been our design choice - cf. NRL's "ifconfig auto").
1222 * (4) it is easier to manage when an interface has addresses
1223 * with the same interface identifier, than to have multiple addresses
1224 * with different interface identifiers.
1226 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1228 ib = (struct in6_ifaddr *)ifa;
1232 /* prefixlen + ifidlen must be equal to 128 */
1233 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1234 if (prefixlen != plen0) {
1237 "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
1238 __func__, if_name(ifp), prefixlen, 128 - plen0));
1243 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1245 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1247 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1248 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1249 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1250 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1251 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1252 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1253 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1254 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1258 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1259 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1261 /* XXX: scope zone ID? */
1263 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1266 * Make sure that we do not have this address already. This should
1267 * usually not happen, but we can still see this case, e.g., if we
1268 * have manually configured the exact address to be configured.
1270 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1271 &ifra.ifra_addr.sin6_addr);
1274 /* this should be rare enough to make an explicit log */
1275 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1276 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1281 * Allocate ifaddr structure, link into chain, etc.
1282 * If we are going to create a new address upon receiving a multicasted
1283 * RA, we need to impose a random delay before starting DAD.
1284 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1288 updateflags |= IN6_IFAUPDATE_DADDELAY;
1289 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1291 "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__,
1292 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1293 if_name(ifp), error));
1294 return (NULL); /* ifaddr must not have been allocated. */
1297 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1299 * XXXRW: Assumption of non-NULLness here might not be true with
1300 * fine-grained locking -- should we validate it? Or just return
1301 * earlier ifa rather than looking it up again?
1303 return (ia); /* this is always non-NULL and referenced. */
1306 static struct nd_prefix *
1307 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1309 struct nd_prefix *search;
1313 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1314 if (key->ndpr_ifp == search->ndpr_ifp &&
1315 key->ndpr_plen == search->ndpr_plen &&
1316 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1317 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1318 nd6_prefix_ref(search);
1326 nd6_prefix_lookup(struct nd_prefixctl *key)
1328 struct nd_prefix *search;
1331 search = nd6_prefix_lookup_locked(key);
1337 nd6_prefix_ref(struct nd_prefix *pr)
1340 refcount_acquire(&pr->ndpr_refcnt);
1344 nd6_prefix_rele(struct nd_prefix *pr)
1347 if (refcount_release(&pr->ndpr_refcnt)) {
1348 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1349 ("prefix %p has advertising routers", pr));
1355 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
1356 struct nd_prefix **newp)
1358 struct nd_prefix *new;
1359 char ip6buf[INET6_ADDRSTRLEN];
1362 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1365 refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1366 new->ndpr_ifp = pr->ndpr_ifp;
1367 new->ndpr_prefix = pr->ndpr_prefix;
1368 new->ndpr_plen = pr->ndpr_plen;
1369 new->ndpr_vltime = pr->ndpr_vltime;
1370 new->ndpr_pltime = pr->ndpr_pltime;
1371 new->ndpr_flags = pr->ndpr_flags;
1372 if ((error = in6_init_prefix_ltimes(new)) != 0) {
1373 free(new, M_IP6NDP);
1376 new->ndpr_lastupdate = time_uptime;
1378 /* initialization */
1379 LIST_INIT(&new->ndpr_advrtrs);
1380 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1381 /* make prefix in the canonical form */
1382 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
1385 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1389 /* ND_OPT_PI_FLAG_ONLINK processing */
1390 if (new->ndpr_raf_onlink) {
1391 struct epoch_tracker et;
1394 NET_EPOCH_ENTER(et);
1395 if ((error = nd6_prefix_onlink(new)) != 0) {
1396 nd6log((LOG_ERR, "%s: failed to make the prefix %s/%d "
1397 "on-link on %s (errno=%d)\n", __func__,
1398 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1399 pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
1400 /* proceed anyway. XXX: is it correct? */
1403 ND6_ONLINK_UNLOCK();
1407 pfxrtr_add(new, dr);
1414 * Remove a prefix from the prefix list and optionally stash it in a
1415 * caller-provided list.
1417 * The ND6 lock must be held.
1420 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1425 LIST_REMOVE(pr, ndpr_entry);
1428 LIST_INSERT_HEAD(list, pr, ndpr_entry);
1432 * Free an unlinked prefix, first marking it off-link if necessary.
1435 nd6_prefix_del(struct nd_prefix *pr)
1437 struct nd_pfxrouter *pfr, *next;
1439 char ip6buf[INET6_ADDRSTRLEN];
1441 KASSERT(pr->ndpr_addrcnt == 0,
1442 ("prefix %p has referencing addresses", pr));
1443 ND6_UNLOCK_ASSERT();
1446 * Though these flags are now meaningless, we'd rather keep the value
1447 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1448 * when executing "ndp -p".
1450 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1452 if ((e = nd6_prefix_offlink(pr)) != 0) {
1454 "%s: failed to make the prefix %s/%d offlink on %s "
1455 "(errno=%d)\n", __func__,
1456 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1457 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1458 /* what should we do? */
1460 ND6_ONLINK_UNLOCK();
1463 /* Release references to routers that have advertised this prefix. */
1465 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1469 nd6_prefix_rele(pr);
1471 pfxlist_onlink_check();
1475 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1476 struct mbuf *m, int mcast)
1478 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1480 struct ifnet *ifp = new->ndpr_ifp;
1481 struct nd_prefix *pr;
1484 struct in6_addrlifetime lt6_tmp;
1485 char ip6buf[INET6_ADDRSTRLEN];
1492 * Authenticity for NA consists authentication for
1493 * both IP header and IP datagrams, doesn't it ?
1495 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1496 auth = ((m->m_flags & M_AUTHIPHDR) &&
1497 (m->m_flags & M_AUTHIPDGM));
1501 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1503 * nd6_prefix_lookup() ensures that pr and new have the same
1504 * prefix on a same interface.
1508 * Update prefix information. Note that the on-link (L) bit
1509 * and the autonomous (A) bit should NOT be changed from 1
1512 if (new->ndpr_raf_onlink == 1)
1513 pr->ndpr_raf_onlink = 1;
1514 if (new->ndpr_raf_auto == 1)
1515 pr->ndpr_raf_auto = 1;
1516 if (new->ndpr_raf_onlink) {
1517 pr->ndpr_vltime = new->ndpr_vltime;
1518 pr->ndpr_pltime = new->ndpr_pltime;
1519 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1520 pr->ndpr_lastupdate = time_uptime;
1523 if (new->ndpr_raf_onlink &&
1524 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1526 if ((error = nd6_prefix_onlink(pr)) != 0) {
1528 "%s: failed to make the prefix %s/%d "
1529 "on-link on %s (errno=%d)\n", __func__,
1531 &pr->ndpr_prefix.sin6_addr),
1532 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1534 /* proceed anyway. XXX: is it correct? */
1536 ND6_ONLINK_UNLOCK();
1542 if (new->ndpr_vltime == 0)
1544 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1547 error = nd6_prelist_add(new, dr, &pr);
1549 nd6log((LOG_NOTICE, "%s: nd6_prelist_add() failed for "
1550 "the prefix %s/%d on %s (errno=%d)\n", __func__,
1551 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1552 new->ndpr_plen, if_name(new->ndpr_ifp), error));
1553 goto end; /* we should just give up in this case. */
1557 * XXX: from the ND point of view, we can ignore a prefix
1558 * with the on-link bit being zero. However, we need a
1559 * prefix structure for references from autoconfigured
1560 * addresses. Thus, we explicitly make sure that the prefix
1561 * itself expires now.
1563 if (pr->ndpr_raf_onlink == 0) {
1564 pr->ndpr_vltime = 0;
1565 pr->ndpr_pltime = 0;
1566 in6_init_prefix_ltimes(pr);
1571 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1572 * Note that pr must be non NULL at this point.
1575 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1576 if (!new->ndpr_raf_auto)
1580 * 5.5.3 (b). the link-local prefix should have been ignored in
1584 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1585 if (new->ndpr_pltime > new->ndpr_vltime) {
1586 error = EINVAL; /* XXX: won't be used */
1591 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1592 * an address configured by stateless autoconfiguration already in the
1593 * list of addresses associated with the interface, and the Valid
1594 * Lifetime is not 0, form an address. We first check if we have
1595 * a matching prefix.
1596 * Note: we apply a clarification in rfc2462bis-02 here. We only
1597 * consider autoconfigured addresses while RFC2462 simply said
1600 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1601 struct in6_ifaddr *ifa6;
1602 u_int32_t remaininglifetime;
1604 if (ifa->ifa_addr->sa_family != AF_INET6)
1607 ifa6 = (struct in6_ifaddr *)ifa;
1610 * We only consider autoconfigured addresses as per rfc2462bis.
1612 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1616 * Spec is not clear here, but I believe we should concentrate
1617 * on unicast (i.e. not anycast) addresses.
1618 * XXX: other ia6_flags? detached or duplicated?
1620 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1624 * Ignore the address if it is not associated with a prefix
1625 * or is associated with a prefix that is different from this
1626 * one. (pr is never NULL here)
1628 if (ifa6->ia6_ndpr != pr)
1631 if (ia6_match == NULL) /* remember the first one */
1635 * An already autoconfigured address matched. Now that we
1636 * are sure there is at least one matched address, we can
1637 * proceed to 5.5.3. (e): update the lifetimes according to the
1638 * "two hours" rule and the privacy extension.
1639 * We apply some clarifications in rfc2462bis:
1640 * - use remaininglifetime instead of storedlifetime as a
1642 * - remove the dead code in the "two-hour" rule
1644 #define TWOHOUR (120*60)
1645 lt6_tmp = ifa6->ia6_lifetime;
1647 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1648 remaininglifetime = ND6_INFINITE_LIFETIME;
1649 else if (time_uptime - ifa6->ia6_updatetime >
1650 lt6_tmp.ia6t_vltime) {
1652 * The case of "invalid" address. We should usually
1653 * not see this case.
1655 remaininglifetime = 0;
1657 remaininglifetime = lt6_tmp.ia6t_vltime -
1658 (time_uptime - ifa6->ia6_updatetime);
1660 /* when not updating, keep the current stored lifetime. */
1661 lt6_tmp.ia6t_vltime = remaininglifetime;
1663 if (TWOHOUR < new->ndpr_vltime ||
1664 remaininglifetime < new->ndpr_vltime) {
1665 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1666 } else if (remaininglifetime <= TWOHOUR) {
1668 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1672 * new->ndpr_vltime <= TWOHOUR &&
1673 * TWOHOUR < remaininglifetime
1675 lt6_tmp.ia6t_vltime = TWOHOUR;
1678 /* The 2 hour rule is not imposed for preferred lifetime. */
1679 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1681 in6_init_address_ltimes(pr, <6_tmp);
1684 * We need to treat lifetimes for temporary addresses
1685 * differently, according to
1686 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1687 * we only update the lifetimes when they are in the maximum
1690 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1691 u_int32_t maxvltime, maxpltime;
1693 if (V_ip6_temp_valid_lifetime >
1694 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1695 V_ip6_desync_factor)) {
1696 maxvltime = V_ip6_temp_valid_lifetime -
1697 (time_uptime - ifa6->ia6_createtime) -
1698 V_ip6_desync_factor;
1701 if (V_ip6_temp_preferred_lifetime >
1702 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1703 V_ip6_desync_factor)) {
1704 maxpltime = V_ip6_temp_preferred_lifetime -
1705 (time_uptime - ifa6->ia6_createtime) -
1706 V_ip6_desync_factor;
1710 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1711 lt6_tmp.ia6t_vltime > maxvltime) {
1712 lt6_tmp.ia6t_vltime = maxvltime;
1714 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1715 lt6_tmp.ia6t_pltime > maxpltime) {
1716 lt6_tmp.ia6t_pltime = maxpltime;
1719 ifa6->ia6_lifetime = lt6_tmp;
1720 ifa6->ia6_updatetime = time_uptime;
1722 if (ia6_match == NULL && new->ndpr_vltime) {
1726 * 5.5.3 (d) (continued)
1727 * No address matched and the valid lifetime is non-zero.
1728 * Create a new address.
1732 * Prefix Length check:
1733 * If the sum of the prefix length and interface identifier
1734 * length does not equal 128 bits, the Prefix Information
1735 * option MUST be ignored. The length of the interface
1736 * identifier is defined in a separate link-type specific
1739 ifidlen = in6_if2idlen(ifp);
1741 /* this should not happen, so we always log it. */
1742 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1746 if (ifidlen + pr->ndpr_plen != 128) {
1748 "%s: invalid prefixlen %d for %s, ignored\n",
1749 __func__, pr->ndpr_plen, if_name(ifp)));
1753 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1755 * note that we should use pr (not new) for reference.
1762 * When a new public address is created as described
1763 * in RFC2462, also create a new temporary address.
1766 * When an interface connects to a new link, a new
1767 * randomized interface identifier should be generated
1768 * immediately together with a new set of temporary
1769 * addresses. Thus, we specifiy 1 as the 2nd arg of
1772 if (V_ip6_use_tempaddr) {
1774 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1775 nd6log((LOG_NOTICE, "%s: failed to "
1776 "create a temporary address "
1777 "(errno=%d)\n", __func__, e));
1780 ifa_free(&ia6->ia_ifa);
1783 * A newly added address might affect the status
1784 * of other addresses, so we check and update it.
1785 * XXX: what if address duplication happens?
1787 pfxlist_onlink_check();
1789 /* just set an error. do not bark here. */
1790 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1796 nd6_prefix_rele(pr);
1801 * A supplement function used in the on-link detection below;
1802 * detect if a given prefix has a (probably) reachable advertising router.
1803 * XXX: lengthy function name...
1805 static struct nd_pfxrouter *
1806 find_pfxlist_reachable_router(struct nd_prefix *pr)
1808 struct epoch_tracker et;
1809 struct nd_pfxrouter *pfxrtr;
1815 NET_EPOCH_ENTER(et);
1816 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1817 ln = nd6_lookup(&pfxrtr->router->rtaddr, LLE_SF(AF_INET6, 0),
1818 pfxrtr->router->ifp);
1821 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1831 * Check if each prefix in the prefix list has at least one available router
1832 * that advertised the prefix (a router is "available" if its neighbor cache
1833 * entry is reachable or probably reachable).
1834 * If the check fails, the prefix may be off-link, because, for example,
1835 * we have moved from the network but the lifetime of the prefix has not
1836 * expired yet. So we should not use the prefix if there is another prefix
1837 * that has an available router.
1838 * But, if there is no prefix that has an available router, we still regard
1839 * all the prefixes as on-link. This is because we can't tell if all the
1840 * routers are simply dead or if we really moved from the network and there
1841 * is no router around us.
1844 pfxlist_onlink_check(void)
1846 struct nd_prefix *pr;
1847 struct in6_ifaddr *ifa;
1848 struct nd_defrouter *dr;
1849 struct nd_pfxrouter *pfxrtr = NULL;
1850 struct rm_priotracker in6_ifa_tracker;
1858 * Check if there is a prefix that has a reachable advertising
1861 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1862 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1867 * If we have no such prefix, check whether we still have a router
1868 * that does not advertise any prefixes.
1871 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1872 struct nd_prefix *pr0;
1874 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1875 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1882 if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1884 * There is at least one prefix that has a reachable router,
1885 * or at least a router which probably does not advertise
1886 * any prefixes. The latter would be the case when we move
1887 * to a new link where we have a router that does not provide
1888 * prefixes and we configure an address by hand.
1889 * Detach prefixes which have no reachable advertising
1890 * router, and attach other prefixes.
1892 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1893 /* XXX: a link-local prefix should never be detached */
1894 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1895 pr->ndpr_raf_onlink == 0 ||
1896 pr->ndpr_raf_auto == 0)
1899 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1900 find_pfxlist_reachable_router(pr) == NULL)
1901 pr->ndpr_stateflags |= NDPRF_DETACHED;
1902 else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1903 find_pfxlist_reachable_router(pr) != NULL)
1904 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1907 /* there is no prefix that has a reachable router */
1908 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1909 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1910 pr->ndpr_raf_onlink == 0 ||
1911 pr->ndpr_raf_auto == 0)
1913 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1918 * Remove each interface route associated with a (just) detached
1919 * prefix, and reinstall the interface route for a (just) attached
1920 * prefix. Note that all attempt of reinstallation does not
1921 * necessarily success, when a same prefix is shared among multiple
1922 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1923 * so we don't have to care about them.
1926 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1927 char ip6buf[INET6_ADDRSTRLEN];
1930 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1931 pr->ndpr_raf_onlink == 0 ||
1932 pr->ndpr_raf_auto == 0)
1935 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1936 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1937 genid = V_nd6_list_genid;
1939 if ((flags & NDPRF_ONLINK) != 0 &&
1940 (e = nd6_prefix_offlink(pr)) != 0) {
1942 "%s: failed to make %s/%d offlink "
1943 "(errno=%d)\n", __func__,
1945 &pr->ndpr_prefix.sin6_addr),
1947 } else if ((flags & NDPRF_ONLINK) == 0 &&
1948 (e = nd6_prefix_onlink(pr)) != 0) {
1950 "%s: failed to make %s/%d onlink "
1951 "(errno=%d)\n", __func__,
1953 &pr->ndpr_prefix.sin6_addr),
1957 if (genid != V_nd6_list_genid)
1963 * Changes on the prefix status might affect address status as well.
1964 * Make sure that all addresses derived from an attached prefix are
1965 * attached, and that all addresses derived from a detached prefix are
1966 * detached. Note, however, that a manually configured address should
1967 * always be attached.
1968 * The precise detection logic is same as the one for prefixes.
1970 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1971 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1972 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1975 if (ifa->ia6_ndpr == NULL) {
1977 * This can happen when we first configure the address
1978 * (i.e. the address exists, but the prefix does not).
1979 * XXX: complicated relationships...
1984 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1988 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1989 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1992 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1995 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1996 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1997 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1998 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1999 nd6_dad_start((struct ifaddr *)ifa, 0);
2002 ifa->ia6_flags |= IN6_IFF_DETACHED;
2006 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
2007 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
2010 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
2011 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2012 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2013 /* Do we need a delay in this case? */
2014 nd6_dad_start((struct ifaddr *)ifa, 0);
2018 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2020 ND6_ONLINK_UNLOCK();
2024 * Add or remove interface route specified by @dst, @netmask and @ifp.
2026 * Returns 0 on success
2029 nd6_prefix_rtrequest(uint32_t fibnum, int cmd, struct sockaddr_in6 *dst,
2030 struct sockaddr_in6 *netmask, struct ifnet *ifp, struct ifaddr *ifa)
2032 struct epoch_tracker et;
2035 /* Prepare gateway */
2036 struct sockaddr_dl_short sdl = {
2037 .sdl_family = AF_LINK,
2038 .sdl_len = sizeof(struct sockaddr_dl_short),
2039 .sdl_type = ifp->if_type,
2040 .sdl_index = ifp->if_index,
2043 struct rt_addrinfo info = {
2045 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
2047 [RTAX_DST] = (struct sockaddr *)dst,
2048 [RTAX_NETMASK] = (struct sockaddr *)netmask,
2049 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
2052 /* Don't set additional per-gw filters on removal */
2054 NET_EPOCH_ENTER(et);
2055 error = rib_handle_ifaddr_info(fibnum, cmd, &info);
2061 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
2065 struct sockaddr_in6 mask6 = {
2066 .sin6_family = AF_INET6,
2067 .sin6_len = sizeof(struct sockaddr_in6),
2068 .sin6_addr = pr->ndpr_mask,
2070 struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
2072 error = nd6_prefix_rtrequest(pr->ndpr_ifp->if_fib, RTM_ADD,
2073 &pr->ndpr_prefix, pmask6, pr->ndpr_ifp, ifa);
2075 pr->ndpr_stateflags |= NDPRF_ONLINK;
2081 nd6_prefix_onlink(struct nd_prefix *pr)
2083 struct epoch_tracker et;
2085 struct ifnet *ifp = pr->ndpr_ifp;
2086 struct nd_prefix *opr;
2087 char ip6buf[INET6_ADDRSTRLEN];
2090 ND6_ONLINK_LOCK_ASSERT();
2091 ND6_UNLOCK_ASSERT();
2093 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
2097 * Add the interface route associated with the prefix. Before
2098 * installing the route, check if there's the same prefix on another
2099 * interface, and the prefix has already installed the interface route.
2100 * Although such a configuration is expected to be rare, we explicitly
2104 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2108 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2111 if (!V_rt_add_addr_allfibs &&
2112 opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2115 if (opr->ndpr_plen == pr->ndpr_plen &&
2116 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2117 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2125 * We prefer link-local addresses as the associated interface address.
2127 /* search for a link-local addr */
2128 NET_EPOCH_ENTER(et);
2129 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
2130 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
2132 /* XXX: freebsd does not have ifa_ifwithaf */
2133 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2134 if (ifa->ifa_addr->sa_family == AF_INET6) {
2139 /* should we care about ia6_flags? */
2143 * This can still happen, when, for example, we receive an RA
2144 * containing a prefix with the L bit set and the A bit clear,
2145 * after removing all IPv6 addresses on the receiving
2146 * interface. This should, of course, be rare though.
2149 "%s: failed to find any ifaddr to add route for a "
2150 "prefix(%s/%d) on %s\n", __func__,
2151 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2152 pr->ndpr_plen, if_name(ifp)));
2155 error = nd6_prefix_onlink_rtrequest(pr, ifa);
2164 nd6_prefix_offlink(struct nd_prefix *pr)
2167 struct ifnet *ifp = pr->ndpr_ifp;
2168 struct nd_prefix *opr;
2169 char ip6buf[INET6_ADDRSTRLEN];
2173 ND6_ONLINK_LOCK_ASSERT();
2174 ND6_UNLOCK_ASSERT();
2176 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2179 struct sockaddr_in6 mask6 = {
2180 .sin6_family = AF_INET6,
2181 .sin6_len = sizeof(struct sockaddr_in6),
2182 .sin6_addr = pr->ndpr_mask,
2184 struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
2186 error = nd6_prefix_rtrequest(ifp->if_fib, RTM_DELETE,
2187 &pr->ndpr_prefix, pmask6, ifp, NULL);
2191 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
2194 * There might be the same prefix on another interface,
2195 * the prefix which could not be on-link just because we have
2196 * the interface route (see comments in nd6_prefix_onlink).
2197 * If there's one, try to make the prefix on-link on the
2202 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2204 * KAME specific: detached prefixes should not be
2207 if (opr == pr || (opr->ndpr_stateflags &
2208 (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
2211 if (opr->ndpr_plen == pr->ndpr_plen &&
2212 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2213 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2216 genid = V_nd6_list_genid;
2218 if ((e = nd6_prefix_onlink(opr)) != 0) {
2220 "%s: failed to recover a prefix "
2221 "%s/%d from %s to %s (errno=%d)\n",
2222 __func__, ip6_sprintf(ip6buf,
2223 &opr->ndpr_prefix.sin6_addr),
2224 opr->ndpr_plen, if_name(ifp),
2225 if_name(opr->ndpr_ifp), e));
2229 if (genid != V_nd6_list_genid)
2235 /* XXX: can we still set the NDPRF_ONLINK flag? */
2237 "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
2238 __func__, ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2239 pr->ndpr_plen, if_name(ifp), error));
2243 lltable_prefix_free(AF_INET6,
2244 (struct sockaddr *)&pr->ndpr_prefix,
2245 (struct sockaddr *)&mask6, LLE_STATIC);
2251 * ia0 - corresponding public address
2254 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2256 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2257 struct in6_ifaddr *newia;
2258 struct in6_aliasreq ifra;
2260 int trylimit = 3; /* XXX: adhoc value */
2262 u_int32_t randid[2];
2263 time_t vltime0, pltime0;
2265 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
2266 &ia0->ia_prefixmask.sin6_addr);
2268 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
2269 /* clear the old IFID */
2270 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
2271 &ifra.ifra_prefixmask.sin6_addr);
2274 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2275 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2276 nd6log((LOG_NOTICE, "%s: failed to find a good random IFID\n",
2280 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2281 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2282 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2283 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2286 * in6_get_tmpifid() quite likely provided a unique interface ID.
2287 * However, we may still have a chance to see collision, because
2288 * there may be a time lag between generation of the ID and generation
2289 * of the address. So, we'll do one more sanity check.
2292 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
2293 if (trylimit-- > 0) {
2298 /* Give up. Something strange should have happened. */
2299 nd6log((LOG_NOTICE, "%s: failed to find a unique random IFID\n",
2305 * The Valid Lifetime is the lower of the Valid Lifetime of the
2306 * public address or TEMP_VALID_LIFETIME.
2307 * The Preferred Lifetime is the lower of the Preferred Lifetime
2308 * of the public address or TEMP_PREFERRED_LIFETIME -
2311 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2312 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2313 (ia0->ia6_lifetime.ia6t_vltime -
2314 (time_uptime - ia0->ia6_updatetime));
2315 if (vltime0 > V_ip6_temp_valid_lifetime)
2316 vltime0 = V_ip6_temp_valid_lifetime;
2318 vltime0 = V_ip6_temp_valid_lifetime;
2319 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2320 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2321 (ia0->ia6_lifetime.ia6t_pltime -
2322 (time_uptime - ia0->ia6_updatetime));
2323 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2324 pltime0 = V_ip6_temp_preferred_lifetime -
2325 V_ip6_desync_factor;
2328 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2329 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2330 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2333 * A temporary address is created only if this calculated Preferred
2334 * Lifetime is greater than REGEN_ADVANCE time units.
2336 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2339 /* XXX: scope zone ID? */
2341 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2343 /* allocate ifaddr structure, link into chain, etc. */
2346 updateflags |= IN6_IFAUPDATE_DADDELAY;
2347 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2350 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2351 if (newia == NULL) { /* XXX: can it happen? */
2353 "%s: ifa update succeeded, but we got no ifaddr\n",
2355 return (EINVAL); /* XXX */
2357 newia->ia6_ndpr = ia0->ia6_ndpr;
2358 newia->ia6_ndpr->ndpr_addrcnt++;
2359 ifa_free(&newia->ia_ifa);
2362 * A newly added address might affect the status of other addresses.
2363 * XXX: when the temporary address is generated with a new public
2364 * address, the onlink check is redundant. However, it would be safe
2365 * to do the check explicitly everywhere a new address is generated,
2366 * and, in fact, we surely need the check when we create a new
2367 * temporary address due to deprecation of an old temporary address.
2369 pfxlist_onlink_check();
2375 rt6_deleteroute(const struct rtentry *rt, const struct nhop_object *nh,
2378 struct in6_addr *gate = (struct in6_addr *)arg;
2381 if (nh->gw_sa.sa_family != AF_INET6)
2384 if (!IN6_ARE_ADDR_EQUAL(gate, &nh->gw6_sa.sin6_addr)) {
2389 * Do not delete a static route.
2390 * XXX: this seems to be a bit ad-hoc. Should we consider the
2391 * 'cloned' bit instead?
2393 nh_rt_flags = nhop_get_rtflags(nh);
2394 if ((nh_rt_flags & RTF_STATIC) != 0)
2398 * We delete only host route. This means, in particular, we don't
2399 * delete default route.
2401 if ((nh_rt_flags & RTF_HOST) == 0)
2409 * Delete all the routing table entries that use the specified gateway.
2410 * XXX: this function causes search through all entries of routing table, so
2411 * it shouldn't be called when acting as a router.
2414 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2417 /* We'll care only link-local addresses */
2418 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2421 /* XXX Do we really need to walk any but the default FIB? */
2422 rib_foreach_table_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2426 nd6_setdefaultiface(int ifindex)
2430 if (ifindex < 0 || V_if_index < ifindex)
2432 if (ifindex != 0 && !ifnet_byindex(ifindex))
2435 if (V_nd6_defifindex != ifindex) {
2436 V_nd6_defifindex = ifindex;
2437 if (V_nd6_defifindex > 0)
2438 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2440 V_nd6_defifp = NULL;
2443 * Our current implementation assumes one-to-one mapping between
2444 * interfaces and links, so it would be natural to use the
2445 * default interface as the default link.
2447 scope6_setdefault(V_nd6_defifp);
2454 nd6_defrouter_list_empty(void)
2457 return (TAILQ_EMPTY(&V_nd6_defrouter));
2461 nd6_defrouter_timer(void)
2463 struct nd_defrouter *dr, *ndr;
2464 struct nd6_drhead drq;
2469 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2470 if (dr->expire && dr->expire < time_uptime)
2471 defrouter_unlink(dr, &drq);
2474 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2475 TAILQ_REMOVE(&drq, dr, dr_entry);
2481 * Nuke default router list entries toward ifp.
2482 * We defer removal of default router list entries that is installed in the
2483 * routing table, in order to keep additional side effects as small as possible.
2486 nd6_defrouter_purge(struct ifnet *ifp)
2488 struct nd_defrouter *dr, *ndr;
2489 struct nd6_drhead drq;
2494 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2498 defrouter_unlink(dr, &drq);
2500 TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2504 defrouter_unlink(dr, &drq);
2508 /* Delete the unlinked router objects. */
2509 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2510 TAILQ_REMOVE(&drq, dr, dr_entry);
2516 nd6_defrouter_flush_all(void)
2518 struct nd_defrouter *dr;
2519 struct nd6_drhead drq;
2524 while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2525 defrouter_unlink(dr, &drq);
2528 while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2529 TAILQ_REMOVE(&drq, dr, dr_entry);
2535 nd6_defrouter_init(void)
2538 TAILQ_INIT(&V_nd6_defrouter);
2542 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2544 struct in6_defrouter d;
2545 struct nd_defrouter *dr;
2548 if (req->newptr != NULL)
2551 error = sysctl_wire_old_buffer(req, 0);
2555 bzero(&d, sizeof(d));
2556 d.rtaddr.sin6_family = AF_INET6;
2557 d.rtaddr.sin6_len = sizeof(d.rtaddr);
2560 TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2561 d.rtaddr.sin6_addr = dr->rtaddr;
2562 error = sa6_recoverscope(&d.rtaddr);
2565 d.flags = dr->raflags;
2566 d.rtlifetime = dr->rtlifetime;
2567 d.expire = dr->expire + (time_second - time_uptime);
2568 d.if_index = dr->ifp->if_index;
2569 error = SYSCTL_OUT(req, &d, sizeof(d));
2576 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2577 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2578 NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2579 "NDP default router list");