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/syslog.h>
54 #include <sys/queue.h>
57 #include <net/if_var.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <net/route_var.h>
62 #include <net/radix.h>
65 #include <netinet/in.h>
66 #include <net/if_llatbl.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_ifattach.h>
69 #include <netinet/ip6.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/nd6.h>
72 #include <netinet/icmp6.h>
73 #include <netinet6/scope6_var.h>
75 static int rtpref(struct nd_defrouter *);
76 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
77 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
79 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
80 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
81 struct nd_defrouter *);
82 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
83 static void pfxrtr_del(struct nd_pfxrouter *);
84 static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
85 static void defrouter_delreq(struct nd_defrouter *);
86 static void nd6_rtmsg(int, struct rtentry *);
88 static int in6_init_prefix_ltimes(struct nd_prefix *);
89 static void in6_init_address_ltimes(struct nd_prefix *,
90 struct in6_addrlifetime *);
92 static int rt6_deleteroute(const struct rtentry *, void *);
94 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
95 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
97 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
98 VNET_DEFINE(int, nd6_defifindex);
99 #define V_nd6_defifp VNET(nd6_defifp)
101 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
103 VNET_DEFINE(int, ip6_desync_factor);
104 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
105 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
107 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
110 VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
113 /* RTPREF_MEDIUM has to be 0! */
114 #define RTPREF_HIGH 1
115 #define RTPREF_MEDIUM 0
116 #define RTPREF_LOW (-1)
117 #define RTPREF_RESERVED (-2)
118 #define RTPREF_INVALID (-3) /* internal */
121 * Receive Router Solicitation Message - just for routers.
122 * Router solicitation/advertisement is mostly managed by userland program
123 * (rtadvd) so here we have no function like nd6_ra_output().
128 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
130 struct ifnet *ifp = m->m_pkthdr.rcvif;
131 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
132 struct nd_router_solicit *nd_rs;
133 struct in6_addr saddr6 = ip6->ip6_src;
136 union nd_opts ndopts;
137 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
140 * Accept RS only when V_ip6_forwarding=1 and the interface has
141 * no ND6_IFF_ACCEPT_RTADV.
143 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
146 /* RFC 6980: Nodes MUST silently ignore fragments */
147 if(m->m_flags & M_FRAGMENTED)
151 if (ip6->ip6_hlim != 255) {
153 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
154 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
155 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
160 * Don't update the neighbor cache, if src = ::.
161 * This indicates that the src has no IP address assigned yet.
163 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
166 #ifndef PULLDOWN_TEST
167 IP6_EXTHDR_CHECK(m, off, icmp6len,);
168 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
170 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
172 ICMP6STAT_INC(icp6s_tooshort);
177 icmp6len -= sizeof(*nd_rs);
178 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
179 if (nd6_options(&ndopts) < 0) {
181 "nd6_rs_input: invalid ND option, ignored\n"));
182 /* nd6_options have incremented stats */
186 if (ndopts.nd_opts_src_lladdr) {
187 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
188 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
191 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
193 "nd6_rs_input: lladdrlen mismatch for %s "
194 "(if %d, RS packet %d)\n",
195 ip6_sprintf(ip6bufs, &saddr6),
196 ifp->if_addrlen, lladdrlen - 2));
200 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
207 ICMP6STAT_INC(icp6s_badrs);
213 * An initial update routine for draft-ietf-6man-ipv6only-flag.
214 * We need to iterate over all default routers for the given
215 * interface to see whether they are all advertising the "S"
216 * (IPv6-Only) flag. If they do set, otherwise unset, the
217 * interface flag we later use to filter on.
220 defrtr_ipv6_only_ifp(struct ifnet *ifp)
222 struct nd_defrouter *dr;
223 bool ipv6_only, ipv6_only_old;
225 struct epoch_tracker et;
230 if (V_nd6_ignore_ipv6_only_ra != 0)
235 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
236 if (dr->ifp == ifp &&
237 (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
241 IF_AFDATA_WLOCK(ifp);
242 ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
243 IF_AFDATA_WUNLOCK(ifp);
245 /* If nothing changed, we have an early exit. */
246 if (ipv6_only == ipv6_only_old)
251 * Should we want to set the IPV6-ONLY flag, check if the
252 * interface has a non-0/0 and non-link-local IPv4 address
253 * configured on it. If it has we will assume working
254 * IPv4 operations and will clear the interface flag.
256 has_ipv4_addr = false;
259 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
260 if (ifa->ifa_addr->sa_family != AF_INET)
263 satosin(ifa->ifa_addr)->sin_addr)) {
264 has_ipv4_addr = true;
270 if (ipv6_only && has_ipv4_addr) {
271 log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
272 "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
277 IF_AFDATA_WLOCK(ifp);
279 ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
281 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
282 IF_AFDATA_WUNLOCK(ifp);
285 /* Send notification of flag change. */
290 defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
293 IF_AFDATA_WLOCK(ifp);
294 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
295 IF_AFDATA_WUNLOCK(ifp);
297 #endif /* EXPERIMENTAL */
300 nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
304 * XXX-BZ we might want to trigger re-evaluation of our default router
305 * availability. E.g., on link down the default router might be
306 * unreachable but a different interface might still have connectivity.
310 if (linkstate == LINK_STATE_DOWN)
311 defrtr_ipv6_only_ipf_down(ifp);
316 * Receive Router Advertisement Message.
319 * TODO: on-link bit on prefix information
320 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
323 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
325 struct ifnet *ifp = m->m_pkthdr.rcvif;
326 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
327 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
328 struct nd_router_advert *nd_ra;
329 struct in6_addr saddr6 = ip6->ip6_src;
331 union nd_opts ndopts;
332 struct nd_defrouter *dr;
333 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
338 * We only accept RAs only when the per-interface flag
339 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
341 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
344 /* RFC 6980: Nodes MUST silently ignore fragments */
345 if(m->m_flags & M_FRAGMENTED)
348 if (ip6->ip6_hlim != 255) {
350 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
351 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
352 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
356 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
358 "nd6_ra_input: src %s is not link-local\n",
359 ip6_sprintf(ip6bufs, &saddr6)));
363 #ifndef PULLDOWN_TEST
364 IP6_EXTHDR_CHECK(m, off, icmp6len,);
365 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
367 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
369 ICMP6STAT_INC(icp6s_tooshort);
374 icmp6len -= sizeof(*nd_ra);
375 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
376 if (nd6_options(&ndopts) < 0) {
378 "nd6_ra_input: invalid ND option, ignored\n"));
379 /* nd6_options have incremented stats */
384 struct nd_defrouter dr0;
385 u_int32_t advreachable = nd_ra->nd_ra_reachable;
387 /* remember if this is a multicasted advertisement */
388 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
391 bzero(&dr0, sizeof(dr0));
393 dr0.raflags = nd_ra->nd_ra_flags_reserved;
395 * Effectively-disable routes from RA messages when
396 * ND6_IFF_NO_RADR enabled on the receiving interface or
397 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
399 if (ndi->flags & ND6_IFF_NO_RADR)
401 else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
404 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
405 dr0.expire = time_uptime + dr0.rtlifetime;
407 /* unspecified or not? (RFC 2461 6.3.4) */
409 advreachable = ntohl(advreachable);
410 if (advreachable <= MAX_REACHABLE_TIME &&
411 ndi->basereachable != advreachable) {
412 ndi->basereachable = advreachable;
413 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
414 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
417 if (nd_ra->nd_ra_retransmit)
418 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
419 if (nd_ra->nd_ra_curhoplimit) {
420 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
421 ndi->chlim = nd_ra->nd_ra_curhoplimit;
422 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
423 log(LOG_ERR, "RA with a lower CurHopLimit sent from "
424 "%s on %s (current = %d, received = %d). "
425 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
426 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
429 dr = defrtrlist_update(&dr0);
431 defrtr_ipv6_only_ifp(ifp);
438 if (ndopts.nd_opts_pi) {
439 struct nd_opt_hdr *pt;
440 struct nd_opt_prefix_info *pi = NULL;
441 struct nd_prefixctl pr;
443 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
444 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
445 pt = (struct nd_opt_hdr *)((caddr_t)pt +
446 (pt->nd_opt_len << 3))) {
447 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
449 pi = (struct nd_opt_prefix_info *)pt;
451 if (pi->nd_opt_pi_len != 4) {
453 "nd6_ra_input: invalid option "
454 "len %d for prefix information option, "
455 "ignored\n", pi->nd_opt_pi_len));
459 if (128 < pi->nd_opt_pi_prefix_len) {
461 "nd6_ra_input: invalid prefix "
462 "len %d for prefix information option, "
463 "ignored\n", pi->nd_opt_pi_prefix_len));
467 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
468 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
470 "nd6_ra_input: invalid prefix "
473 &pi->nd_opt_pi_prefix)));
477 bzero(&pr, sizeof(pr));
478 pr.ndpr_prefix.sin6_family = AF_INET6;
479 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
480 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
481 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
483 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
484 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
485 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
486 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
487 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
488 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
489 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
490 (void)prelist_update(&pr, dr, m, mcast);
501 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
505 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
508 if (mtu < IPV6_MMTU) {
509 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
510 "mtu=%lu sent from %s, ignoring\n",
511 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
516 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
517 ? ndi->maxmtu : ifp->if_mtu;
519 int change = (ndi->linkmtu != mtu);
523 /* in6_maxmtu may change */
528 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
529 "mtu=%lu sent from %s; "
530 "exceeds maxmtu %lu, ignoring\n",
531 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
538 * Source link layer address
544 if (ndopts.nd_opts_src_lladdr) {
545 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
546 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
549 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
551 "nd6_ra_input: lladdrlen mismatch for %s "
552 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
553 ifp->if_addrlen, lladdrlen - 2));
557 nd6_cache_lladdr(ifp, &saddr6, lladdr,
558 lladdrlen, ND_ROUTER_ADVERT, 0);
561 * Installing a link-layer address might change the state of the
562 * router's neighbor cache, which might also affect our on-link
563 * detection of adveritsed prefixes.
565 pfxlist_onlink_check();
573 ICMP6STAT_INC(icp6s_badra);
577 /* tell the change to user processes watching the routing socket. */
579 nd6_rtmsg(int cmd, struct rtentry *rt)
581 struct rt_addrinfo info;
585 bzero((caddr_t)&info, sizeof(info));
586 info.rti_info[RTAX_DST] = rt_key(rt);
587 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
588 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
591 struct epoch_tracker et;
594 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
595 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
598 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
602 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
608 * default router list processing sub routines
612 defrouter_addreq(struct nd_defrouter *new)
614 struct sockaddr_in6 def, mask, gate;
615 struct rtentry *newrt = NULL;
618 bzero(&def, sizeof(def));
619 bzero(&mask, sizeof(mask));
620 bzero(&gate, sizeof(gate));
622 def.sin6_len = mask.sin6_len = gate.sin6_len =
623 sizeof(struct sockaddr_in6);
624 def.sin6_family = gate.sin6_family = AF_INET6;
625 gate.sin6_addr = new->rtaddr;
627 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
628 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
629 RTF_GATEWAY, &newrt, new->ifp->if_fib);
631 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
638 struct nd_defrouter *
639 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
641 struct nd_defrouter *dr;
644 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
645 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
652 struct nd_defrouter *
653 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
655 struct nd_defrouter *dr;
658 dr = defrouter_lookup_locked(addr, ifp);
664 defrouter_ref(struct nd_defrouter *dr)
667 refcount_acquire(&dr->refcnt);
671 defrouter_rele(struct nd_defrouter *dr)
674 if (refcount_release(&dr->refcnt))
679 * Remove the default route for a given router.
680 * This is just a subroutine function for defrouter_select_fib(), and
681 * should not be called from anywhere else.
684 defrouter_delreq(struct nd_defrouter *dr)
686 struct sockaddr_in6 def, mask, gate;
687 struct rtentry *oldrt = NULL;
689 bzero(&def, sizeof(def));
690 bzero(&mask, sizeof(mask));
691 bzero(&gate, sizeof(gate));
693 def.sin6_len = mask.sin6_len = gate.sin6_len =
694 sizeof(struct sockaddr_in6);
695 def.sin6_family = gate.sin6_family = AF_INET6;
696 gate.sin6_addr = dr->rtaddr;
698 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
699 (struct sockaddr *)&gate,
700 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
702 nd6_rtmsg(RTM_DELETE, oldrt);
710 * Remove all default routes from default router list.
713 defrouter_reset(void)
715 struct nd_defrouter *dr, **dra;
721 * We can't delete routes with the ND lock held, so make a copy of the
722 * current default router list and use that when deleting routes.
725 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
729 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
732 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
740 for (i = 0; i < count && dra[i] != NULL; i++) {
741 defrouter_delreq(dra[i]);
742 defrouter_rele(dra[i]);
747 * XXX should we also nuke any default routers in the kernel, by
748 * going through them by rtalloc1()?
753 * Look up a matching default router list entry and remove it. Returns true if a
754 * matching entry was found, false otherwise.
757 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
759 struct nd_defrouter *dr;
762 dr = defrouter_lookup_locked(addr, ifp);
768 defrouter_unlink(dr, NULL);
776 * Remove a router from the global list and optionally stash it in a
777 * caller-supplied queue.
779 * The ND lock must be held.
782 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
786 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
789 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
793 defrouter_del(struct nd_defrouter *dr)
795 struct nd_defrouter *deldr = NULL;
796 struct nd_prefix *pr;
797 struct nd_pfxrouter *pfxrtr;
802 * Flush all the routing table entries that use the router
805 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
806 rt6_flush(&dr->rtaddr, dr->ifp);
809 defrtr_ipv6_only_ifp(dr->ifp);
814 defrouter_delreq(dr);
818 * Also delete all the pointers to the router in each prefix lists.
821 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
822 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
827 pfxlist_onlink_check();
830 * If the router is the primary one, choose a new one.
831 * Note that defrouter_select_fib() will remove the current
832 * gateway from the routing table.
835 defrouter_select_fib(deldr->ifp->if_fib);
838 * Release the list reference.
844 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
845 * draft-ietf-ipngwg-router-selection:
846 * 1) Routers that are reachable or probably reachable should be preferred.
847 * If we have more than one (probably) reachable router, prefer ones
848 * with the highest router preference.
849 * 2) When no routers on the list are known to be reachable or
850 * probably reachable, routers SHOULD be selected in a round-robin
851 * fashion, regardless of router preference values.
852 * 3) If the Default Router List is empty, assume that all
853 * destinations are on-link.
855 * We assume nd_defrouter is sorted by router preference value.
856 * Since the code below covers both with and without router preference cases,
857 * we do not need to classify the cases by ifdef.
859 * At this moment, we do not try to install more than one default router,
860 * even when the multipath routing is available, because we're not sure about
861 * the benefits for stub hosts comparing to the risk of making the code
862 * complicated and the possibility of introducing bugs.
864 * We maintain a single list of routers for multiple FIBs, only considering one
865 * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
866 * we do the whole thing multiple times.
869 defrouter_select_fib(int fibnum)
871 struct epoch_tracker et;
872 struct nd_defrouter *dr, *selected_dr, *installed_dr;
873 struct llentry *ln = NULL;
875 if (fibnum == RT_ALL_FIBS) {
876 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
877 defrouter_select_fib(fibnum);
883 * Let's handle easy case (3) first:
884 * If default router list is empty, there's nothing to be done.
886 if (TAILQ_EMPTY(&V_nd_defrouter)) {
892 * Search for a (probably) reachable router from the list.
893 * We just pick up the first reachable one (if any), assuming that
894 * the ordering rule of the list described in defrtrlist_update().
896 selected_dr = installed_dr = NULL;
897 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
899 if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
900 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
901 ND6_IS_LLINFO_PROBREACH(ln)) {
903 defrouter_ref(selected_dr);
911 if (dr->installed && dr->ifp->if_fib == fibnum) {
912 if (installed_dr == NULL) {
914 defrouter_ref(installed_dr);
917 * this should not happen.
918 * warn for diagnosis.
920 log(LOG_ERR, "defrouter_select_fib: more than "
921 "one router is installed\n");
926 * If none of the default routers was found to be reachable,
927 * round-robin the list regardless of preference.
928 * Otherwise, if we have an installed router, check if the selected
929 * (reachable) router should really be preferred to the installed one.
930 * We only prefer the new router when the old one is not reachable
931 * or when the new one has a really higher preference value.
933 if (selected_dr == NULL) {
934 if (installed_dr == NULL ||
935 TAILQ_NEXT(installed_dr, dr_entry) == NULL)
936 dr = TAILQ_FIRST(&V_nd_defrouter);
938 dr = TAILQ_NEXT(installed_dr, dr_entry);
940 /* Ensure we select a router for this FIB. */
941 TAILQ_FOREACH_FROM(dr, &V_nd_defrouter, dr_entry) {
942 if (dr->ifp->if_fib == fibnum) {
944 defrouter_ref(selected_dr);
948 } else if (installed_dr != NULL) {
950 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
951 installed_dr->ifp)) &&
952 ND6_IS_LLINFO_PROBREACH(ln) &&
953 installed_dr->ifp->if_fib == fibnum &&
954 rtpref(selected_dr) <= rtpref(installed_dr)) {
955 defrouter_rele(selected_dr);
956 selected_dr = installed_dr;
965 * If we selected a router for this FIB and it's different
966 * than the installed one, remove the installed router and
967 * install the selected one in its place.
969 if (installed_dr != selected_dr) {
970 if (installed_dr != NULL) {
971 defrouter_delreq(installed_dr);
972 defrouter_rele(installed_dr);
974 if (selected_dr != NULL)
975 defrouter_addreq(selected_dr);
977 if (selected_dr != NULL)
978 defrouter_rele(selected_dr);
982 * Maintain old KPI for default router selection.
983 * If unspecified, we can re-select routers for all FIBs.
986 defrouter_select(void)
988 defrouter_select_fib(RT_ALL_FIBS);
992 * for default router selection
993 * regards router-preference field as a 2-bit signed integer
996 rtpref(struct nd_defrouter *dr)
998 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
999 case ND_RA_FLAG_RTPREF_HIGH:
1000 return (RTPREF_HIGH);
1001 case ND_RA_FLAG_RTPREF_MEDIUM:
1002 case ND_RA_FLAG_RTPREF_RSV:
1003 return (RTPREF_MEDIUM);
1004 case ND_RA_FLAG_RTPREF_LOW:
1005 return (RTPREF_LOW);
1008 * This case should never happen. If it did, it would mean a
1009 * serious bug of kernel internal. We thus always bark here.
1010 * Or, can we even panic?
1012 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
1013 return (RTPREF_INVALID);
1018 static struct nd_defrouter *
1019 defrtrlist_update(struct nd_defrouter *new)
1021 struct nd_defrouter *dr, *n;
1026 if (new->rtlifetime == 0) {
1027 defrouter_remove(&new->rtaddr, new->ifp);
1032 writelocked = false;
1034 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1036 oldpref = rtpref(dr);
1039 dr->raflags = new->raflags; /* XXX flag check */
1040 dr->rtlifetime = new->rtlifetime;
1041 dr->expire = new->expire;
1044 * If the preference does not change, there's no need
1045 * to sort the entries. Also make sure the selected
1046 * router is still installed in the kernel.
1048 if (dr->installed && rtpref(new) == oldpref) {
1058 * The router needs to be reinserted into the default router
1059 * list, so upgrade to a write lock. If that fails and the list
1060 * has potentially changed while the lock was dropped, we'll
1061 * redo the lookup with the write lock held.
1065 if (!ND6_TRY_UPGRADE()) {
1066 genid = V_nd6_list_genid;
1069 if (genid != V_nd6_list_genid)
1076 * The preferred router may have changed, so relocate this
1079 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
1082 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
1087 memcpy(n, new, sizeof(*n));
1088 /* Initialize with an extra reference for the caller. */
1089 refcount_init(&n->refcnt, 2);
1093 * Insert the new router in the Default Router List;
1094 * The Default Router List should be in the descending order
1095 * of router-preferece. Routers with the same preference are
1096 * sorted in the arriving time order.
1099 /* insert at the end of the group */
1100 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1101 if (rtpref(n) > rtpref(dr))
1105 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1107 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
1111 defrouter_select_fib(new->ifp->if_fib);
1116 static struct nd_pfxrouter *
1117 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
1119 struct nd_pfxrouter *search;
1123 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
1124 if (search->router == dr)
1131 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
1133 struct nd_pfxrouter *new;
1136 ND6_UNLOCK_ASSERT();
1139 if (pfxrtr_lookup(pr, dr) != NULL) {
1145 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1152 if (pfxrtr_lookup(pr, dr) == NULL) {
1153 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
1156 /* We lost a race to add the reference. */
1158 free(new, M_IP6NDP);
1164 pfxlist_onlink_check();
1168 pfxrtr_del(struct nd_pfxrouter *pfr)
1173 LIST_REMOVE(pfr, pfr_entry);
1174 defrouter_rele(pfr->router);
1175 free(pfr, M_IP6NDP);
1178 static struct nd_prefix *
1179 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1181 struct nd_prefix *search;
1185 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1186 if (key->ndpr_ifp == search->ndpr_ifp &&
1187 key->ndpr_plen == search->ndpr_plen &&
1188 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1189 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1190 nd6_prefix_ref(search);
1198 nd6_prefix_lookup(struct nd_prefixctl *key)
1200 struct nd_prefix *search;
1203 search = nd6_prefix_lookup_locked(key);
1209 nd6_prefix_ref(struct nd_prefix *pr)
1212 refcount_acquire(&pr->ndpr_refcnt);
1216 nd6_prefix_rele(struct nd_prefix *pr)
1219 if (refcount_release(&pr->ndpr_refcnt)) {
1220 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1221 ("prefix %p has advertising routers", pr));
1227 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
1228 struct nd_prefix **newp)
1230 struct nd_prefix *new;
1231 char ip6buf[INET6_ADDRSTRLEN];
1234 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
1237 refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1238 new->ndpr_ifp = pr->ndpr_ifp;
1239 new->ndpr_prefix = pr->ndpr_prefix;
1240 new->ndpr_plen = pr->ndpr_plen;
1241 new->ndpr_vltime = pr->ndpr_vltime;
1242 new->ndpr_pltime = pr->ndpr_pltime;
1243 new->ndpr_flags = pr->ndpr_flags;
1244 if ((error = in6_init_prefix_ltimes(new)) != 0) {
1245 free(new, M_IP6NDP);
1248 new->ndpr_lastupdate = time_uptime;
1250 /* initialization */
1251 LIST_INIT(&new->ndpr_advrtrs);
1252 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1253 /* make prefix in the canonical form */
1254 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
1257 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1261 /* ND_OPT_PI_FLAG_ONLINK processing */
1262 if (new->ndpr_raf_onlink) {
1264 if ((error = nd6_prefix_onlink(new)) != 0) {
1265 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
1266 "the prefix %s/%d on-link on %s (errno=%d)\n",
1267 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1268 pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
1269 /* proceed anyway. XXX: is it correct? */
1271 ND6_ONLINK_UNLOCK();
1275 pfxrtr_add(new, dr);
1282 * Remove a prefix from the prefix list and optionally stash it in a
1283 * caller-provided list.
1285 * The ND6 lock must be held.
1288 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1293 LIST_REMOVE(pr, ndpr_entry);
1296 LIST_INSERT_HEAD(list, pr, ndpr_entry);
1300 * Free an unlinked prefix, first marking it off-link if necessary.
1303 nd6_prefix_del(struct nd_prefix *pr)
1305 struct nd_pfxrouter *pfr, *next;
1307 char ip6buf[INET6_ADDRSTRLEN];
1309 KASSERT(pr->ndpr_addrcnt == 0,
1310 ("prefix %p has referencing addresses", pr));
1311 ND6_UNLOCK_ASSERT();
1314 * Though these flags are now meaningless, we'd rather keep the value
1315 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
1316 * when executing "ndp -p".
1318 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1320 if ((e = nd6_prefix_offlink(pr)) != 0) {
1322 "nd6_prefix_del: failed to make %s/%d offlink "
1323 "on %s, errno=%d\n",
1324 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1325 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1326 /* what should we do? */
1328 ND6_ONLINK_UNLOCK();
1331 /* Release references to routers that have advertised this prefix. */
1333 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1337 nd6_prefix_rele(pr);
1339 pfxlist_onlink_check();
1343 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
1344 struct mbuf *m, int mcast)
1346 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1348 struct ifnet *ifp = new->ndpr_ifp;
1349 struct nd_prefix *pr;
1352 struct in6_addrlifetime lt6_tmp;
1353 char ip6buf[INET6_ADDRSTRLEN];
1354 struct epoch_tracker et;
1359 * Authenticity for NA consists authentication for
1360 * both IP header and IP datagrams, doesn't it ?
1362 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1363 auth = ((m->m_flags & M_AUTHIPHDR) &&
1364 (m->m_flags & M_AUTHIPDGM));
1368 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1370 * nd6_prefix_lookup() ensures that pr and new have the same
1371 * prefix on a same interface.
1375 * Update prefix information. Note that the on-link (L) bit
1376 * and the autonomous (A) bit should NOT be changed from 1
1379 if (new->ndpr_raf_onlink == 1)
1380 pr->ndpr_raf_onlink = 1;
1381 if (new->ndpr_raf_auto == 1)
1382 pr->ndpr_raf_auto = 1;
1383 if (new->ndpr_raf_onlink) {
1384 pr->ndpr_vltime = new->ndpr_vltime;
1385 pr->ndpr_pltime = new->ndpr_pltime;
1386 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1387 pr->ndpr_lastupdate = time_uptime;
1390 if (new->ndpr_raf_onlink &&
1391 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1393 if ((error = nd6_prefix_onlink(pr)) != 0) {
1395 "prelist_update: failed to make "
1396 "the prefix %s/%d on-link on %s "
1399 &pr->ndpr_prefix.sin6_addr),
1400 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1402 /* proceed anyway. XXX: is it correct? */
1404 ND6_ONLINK_UNLOCK();
1410 if (new->ndpr_vltime == 0)
1412 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1415 error = nd6_prelist_add(new, dr, &pr);
1417 nd6log((LOG_NOTICE, "prelist_update: "
1418 "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
1419 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
1420 new->ndpr_plen, if_name(new->ndpr_ifp), error));
1421 goto end; /* we should just give up in this case. */
1425 * XXX: from the ND point of view, we can ignore a prefix
1426 * with the on-link bit being zero. However, we need a
1427 * prefix structure for references from autoconfigured
1428 * addresses. Thus, we explicitly make sure that the prefix
1429 * itself expires now.
1431 if (pr->ndpr_raf_onlink == 0) {
1432 pr->ndpr_vltime = 0;
1433 pr->ndpr_pltime = 0;
1434 in6_init_prefix_ltimes(pr);
1439 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1440 * Note that pr must be non NULL at this point.
1443 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1444 if (!new->ndpr_raf_auto)
1448 * 5.5.3 (b). the link-local prefix should have been ignored in
1452 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1453 if (new->ndpr_pltime > new->ndpr_vltime) {
1454 error = EINVAL; /* XXX: won't be used */
1459 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1460 * an address configured by stateless autoconfiguration already in the
1461 * list of addresses associated with the interface, and the Valid
1462 * Lifetime is not 0, form an address. We first check if we have
1463 * a matching prefix.
1464 * Note: we apply a clarification in rfc2462bis-02 here. We only
1465 * consider autoconfigured addresses while RFC2462 simply said
1468 NET_EPOCH_ENTER(et);
1469 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1470 struct in6_ifaddr *ifa6;
1471 u_int32_t remaininglifetime;
1473 if (ifa->ifa_addr->sa_family != AF_INET6)
1476 ifa6 = (struct in6_ifaddr *)ifa;
1479 * We only consider autoconfigured addresses as per rfc2462bis.
1481 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1485 * Spec is not clear here, but I believe we should concentrate
1486 * on unicast (i.e. not anycast) addresses.
1487 * XXX: other ia6_flags? detached or duplicated?
1489 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1493 * Ignore the address if it is not associated with a prefix
1494 * or is associated with a prefix that is different from this
1495 * one. (pr is never NULL here)
1497 if (ifa6->ia6_ndpr != pr)
1500 if (ia6_match == NULL) /* remember the first one */
1504 * An already autoconfigured address matched. Now that we
1505 * are sure there is at least one matched address, we can
1506 * proceed to 5.5.3. (e): update the lifetimes according to the
1507 * "two hours" rule and the privacy extension.
1508 * We apply some clarifications in rfc2462bis:
1509 * - use remaininglifetime instead of storedlifetime as a
1511 * - remove the dead code in the "two-hour" rule
1513 #define TWOHOUR (120*60)
1514 lt6_tmp = ifa6->ia6_lifetime;
1516 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1517 remaininglifetime = ND6_INFINITE_LIFETIME;
1518 else if (time_uptime - ifa6->ia6_updatetime >
1519 lt6_tmp.ia6t_vltime) {
1521 * The case of "invalid" address. We should usually
1522 * not see this case.
1524 remaininglifetime = 0;
1526 remaininglifetime = lt6_tmp.ia6t_vltime -
1527 (time_uptime - ifa6->ia6_updatetime);
1529 /* when not updating, keep the current stored lifetime. */
1530 lt6_tmp.ia6t_vltime = remaininglifetime;
1532 if (TWOHOUR < new->ndpr_vltime ||
1533 remaininglifetime < new->ndpr_vltime) {
1534 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1535 } else if (remaininglifetime <= TWOHOUR) {
1537 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1541 * new->ndpr_vltime <= TWOHOUR &&
1542 * TWOHOUR < remaininglifetime
1544 lt6_tmp.ia6t_vltime = TWOHOUR;
1547 /* The 2 hour rule is not imposed for preferred lifetime. */
1548 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1550 in6_init_address_ltimes(pr, <6_tmp);
1553 * We need to treat lifetimes for temporary addresses
1554 * differently, according to
1555 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1556 * we only update the lifetimes when they are in the maximum
1559 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1560 u_int32_t maxvltime, maxpltime;
1562 if (V_ip6_temp_valid_lifetime >
1563 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1564 V_ip6_desync_factor)) {
1565 maxvltime = V_ip6_temp_valid_lifetime -
1566 (time_uptime - ifa6->ia6_createtime) -
1567 V_ip6_desync_factor;
1570 if (V_ip6_temp_preferred_lifetime >
1571 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1572 V_ip6_desync_factor)) {
1573 maxpltime = V_ip6_temp_preferred_lifetime -
1574 (time_uptime - ifa6->ia6_createtime) -
1575 V_ip6_desync_factor;
1579 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1580 lt6_tmp.ia6t_vltime > maxvltime) {
1581 lt6_tmp.ia6t_vltime = maxvltime;
1583 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1584 lt6_tmp.ia6t_pltime > maxpltime) {
1585 lt6_tmp.ia6t_pltime = maxpltime;
1588 ifa6->ia6_lifetime = lt6_tmp;
1589 ifa6->ia6_updatetime = time_uptime;
1592 if (ia6_match == NULL && new->ndpr_vltime) {
1596 * 5.5.3 (d) (continued)
1597 * No address matched and the valid lifetime is non-zero.
1598 * Create a new address.
1602 * Prefix Length check:
1603 * If the sum of the prefix length and interface identifier
1604 * length does not equal 128 bits, the Prefix Information
1605 * option MUST be ignored. The length of the interface
1606 * identifier is defined in a separate link-type specific
1609 ifidlen = in6_if2idlen(ifp);
1611 /* this should not happen, so we always log it. */
1612 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1616 if (ifidlen + pr->ndpr_plen != 128) {
1618 "prelist_update: invalid prefixlen "
1619 "%d for %s, ignored\n",
1620 pr->ndpr_plen, if_name(ifp)));
1624 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1626 * note that we should use pr (not new) for reference.
1633 * When a new public address is created as described
1634 * in RFC2462, also create a new temporary address.
1637 * When an interface connects to a new link, a new
1638 * randomized interface identifier should be generated
1639 * immediately together with a new set of temporary
1640 * addresses. Thus, we specifiy 1 as the 2nd arg of
1643 if (V_ip6_use_tempaddr) {
1645 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1646 nd6log((LOG_NOTICE, "prelist_update: "
1647 "failed to create a temporary "
1648 "address, errno=%d\n",
1652 ifa_free(&ia6->ia_ifa);
1655 * A newly added address might affect the status
1656 * of other addresses, so we check and update it.
1657 * XXX: what if address duplication happens?
1659 pfxlist_onlink_check();
1661 /* just set an error. do not bark here. */
1662 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1668 nd6_prefix_rele(pr);
1673 * A supplement function used in the on-link detection below;
1674 * detect if a given prefix has a (probably) reachable advertising router.
1675 * XXX: lengthy function name...
1677 static struct nd_pfxrouter *
1678 find_pfxlist_reachable_router(struct nd_prefix *pr)
1680 struct epoch_tracker et;
1681 struct nd_pfxrouter *pfxrtr;
1687 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1688 NET_EPOCH_ENTER(et);
1689 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1693 canreach = ND6_IS_LLINFO_PROBREACH(ln);
1702 * Check if each prefix in the prefix list has at least one available router
1703 * that advertised the prefix (a router is "available" if its neighbor cache
1704 * entry is reachable or probably reachable).
1705 * If the check fails, the prefix may be off-link, because, for example,
1706 * we have moved from the network but the lifetime of the prefix has not
1707 * expired yet. So we should not use the prefix if there is another prefix
1708 * that has an available router.
1709 * But, if there is no prefix that has an available router, we still regard
1710 * all the prefixes as on-link. This is because we can't tell if all the
1711 * routers are simply dead or if we really moved from the network and there
1712 * is no router around us.
1715 pfxlist_onlink_check(void)
1717 struct nd_prefix *pr;
1718 struct in6_ifaddr *ifa;
1719 struct nd_defrouter *dr;
1720 struct nd_pfxrouter *pfxrtr = NULL;
1721 struct rm_priotracker in6_ifa_tracker;
1729 * Check if there is a prefix that has a reachable advertising
1732 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1733 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1738 * If we have no such prefix, check whether we still have a router
1739 * that does not advertise any prefixes.
1742 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1743 struct nd_prefix *pr0;
1745 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1746 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1753 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1755 * There is at least one prefix that has a reachable router,
1756 * or at least a router which probably does not advertise
1757 * any prefixes. The latter would be the case when we move
1758 * to a new link where we have a router that does not provide
1759 * prefixes and we configure an address by hand.
1760 * Detach prefixes which have no reachable advertising
1761 * router, and attach other prefixes.
1763 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1764 /* XXX: a link-local prefix should never be detached */
1765 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1766 pr->ndpr_raf_onlink == 0 ||
1767 pr->ndpr_raf_auto == 0)
1770 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1771 find_pfxlist_reachable_router(pr) == NULL)
1772 pr->ndpr_stateflags |= NDPRF_DETACHED;
1773 else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1774 find_pfxlist_reachable_router(pr) != NULL)
1775 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1778 /* there is no prefix that has a reachable router */
1779 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1780 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1781 pr->ndpr_raf_onlink == 0 ||
1782 pr->ndpr_raf_auto == 0)
1784 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1789 * Remove each interface route associated with a (just) detached
1790 * prefix, and reinstall the interface route for a (just) attached
1791 * prefix. Note that all attempt of reinstallation does not
1792 * necessarily success, when a same prefix is shared among multiple
1793 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1794 * so we don't have to care about them.
1797 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1798 char ip6buf[INET6_ADDRSTRLEN];
1801 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1802 pr->ndpr_raf_onlink == 0 ||
1803 pr->ndpr_raf_auto == 0)
1806 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1807 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1808 genid = V_nd6_list_genid;
1810 if ((flags & NDPRF_ONLINK) != 0 &&
1811 (e = nd6_prefix_offlink(pr)) != 0) {
1813 "pfxlist_onlink_check: failed to "
1814 "make %s/%d offlink, errno=%d\n",
1816 &pr->ndpr_prefix.sin6_addr),
1818 } else if ((flags & NDPRF_ONLINK) == 0 &&
1819 (e = nd6_prefix_onlink(pr)) != 0) {
1821 "pfxlist_onlink_check: failed to "
1822 "make %s/%d onlink, errno=%d\n",
1824 &pr->ndpr_prefix.sin6_addr),
1828 if (genid != V_nd6_list_genid)
1834 * Changes on the prefix status might affect address status as well.
1835 * Make sure that all addresses derived from an attached prefix are
1836 * attached, and that all addresses derived from a detached prefix are
1837 * detached. Note, however, that a manually configured address should
1838 * always be attached.
1839 * The precise detection logic is same as the one for prefixes.
1841 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1842 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1843 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1846 if (ifa->ia6_ndpr == NULL) {
1848 * This can happen when we first configure the address
1849 * (i.e. the address exists, but the prefix does not).
1850 * XXX: complicated relationships...
1855 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1859 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1860 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1863 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1866 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1867 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1868 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1869 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1870 nd6_dad_start((struct ifaddr *)ifa, 0);
1873 ifa->ia6_flags |= IN6_IFF_DETACHED;
1877 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
1878 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1881 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1882 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1883 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1884 /* Do we need a delay in this case? */
1885 nd6_dad_start((struct ifaddr *)ifa, 0);
1889 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1891 ND6_ONLINK_UNLOCK();
1895 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
1897 struct sockaddr_dl sdl;
1899 struct sockaddr_in6 mask6;
1901 int error, a_failure, fibnum, maxfib;
1904 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1905 * ifa->ifa_rtrequest = nd6_rtrequest;
1907 bzero(&mask6, sizeof(mask6));
1908 mask6.sin6_len = sizeof(mask6);
1909 mask6.sin6_addr = pr->ndpr_mask;
1910 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
1912 bzero(&sdl, sizeof(struct sockaddr_dl));
1913 sdl.sdl_len = sizeof(struct sockaddr_dl);
1914 sdl.sdl_family = AF_LINK;
1915 sdl.sdl_type = ifa->ifa_ifp->if_type;
1916 sdl.sdl_index = ifa->ifa_ifp->if_index;
1918 if(V_rt_add_addr_allfibs) {
1920 maxfib = rt_numfibs;
1922 fibnum = ifa->ifa_ifp->if_fib;
1923 maxfib = fibnum + 1;
1926 for (; fibnum < maxfib; fibnum++) {
1929 error = in6_rtrequest(RTM_ADD,
1930 (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
1931 (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
1933 KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
1934 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
1937 nd6_rtmsg(RTM_ADD, rt);
1939 pr->ndpr_stateflags |= NDPRF_ONLINK;
1941 char ip6buf[INET6_ADDRSTRLEN];
1942 char ip6bufg[INET6_ADDRSTRLEN];
1943 char ip6bufm[INET6_ADDRSTRLEN];
1944 struct sockaddr_in6 *sin6;
1946 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1947 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
1948 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
1949 "flags=%lx errno = %d\n",
1950 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1951 pr->ndpr_plen, if_name(pr->ndpr_ifp),
1952 ip6_sprintf(ip6bufg, &sin6->sin6_addr),
1953 ip6_sprintf(ip6bufm, &mask6.sin6_addr),
1956 /* Save last error to return, see rtinit(). */
1967 /* Return the last error we got. */
1972 nd6_prefix_onlink(struct nd_prefix *pr)
1975 struct ifnet *ifp = pr->ndpr_ifp;
1976 struct nd_prefix *opr;
1977 char ip6buf[INET6_ADDRSTRLEN];
1980 ND6_ONLINK_LOCK_ASSERT();
1981 ND6_UNLOCK_ASSERT();
1983 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1987 * Add the interface route associated with the prefix. Before
1988 * installing the route, check if there's the same prefix on another
1989 * interface, and the prefix has already installed the interface route.
1990 * Although such a configuration is expected to be rare, we explicitly
1994 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
1998 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2001 if (!V_rt_add_addr_allfibs &&
2002 opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2005 if (opr->ndpr_plen == pr->ndpr_plen &&
2006 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2007 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2015 * We prefer link-local addresses as the associated interface address.
2017 /* search for a link-local addr */
2018 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
2019 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
2021 struct epoch_tracker et;
2023 /* XXX: freebsd does not have ifa_ifwithaf */
2024 NET_EPOCH_ENTER(et);
2025 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2026 if (ifa->ifa_addr->sa_family == AF_INET6) {
2032 /* should we care about ia6_flags? */
2036 * This can still happen, when, for example, we receive an RA
2037 * containing a prefix with the L bit set and the A bit clear,
2038 * after removing all IPv6 addresses on the receiving
2039 * interface. This should, of course, be rare though.
2042 "nd6_prefix_onlink: failed to find any ifaddr"
2043 " to add route for a prefix(%s/%d) on %s\n",
2044 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2045 pr->ndpr_plen, if_name(ifp)));
2049 error = nd6_prefix_onlink_rtrequest(pr, ifa);
2058 nd6_prefix_offlink(struct nd_prefix *pr)
2061 struct ifnet *ifp = pr->ndpr_ifp;
2062 struct nd_prefix *opr;
2063 struct sockaddr_in6 sa6, mask6;
2065 char ip6buf[INET6_ADDRSTRLEN];
2067 int fibnum, maxfib, a_failure;
2069 ND6_ONLINK_LOCK_ASSERT();
2070 ND6_UNLOCK_ASSERT();
2072 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
2075 bzero(&sa6, sizeof(sa6));
2076 sa6.sin6_family = AF_INET6;
2077 sa6.sin6_len = sizeof(sa6);
2078 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
2079 sizeof(struct in6_addr));
2080 bzero(&mask6, sizeof(mask6));
2081 mask6.sin6_family = AF_INET6;
2082 mask6.sin6_len = sizeof(sa6);
2083 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
2085 if (V_rt_add_addr_allfibs) {
2087 maxfib = rt_numfibs;
2089 fibnum = ifp->if_fib;
2090 maxfib = fibnum + 1;
2094 for (; fibnum < maxfib; fibnum++) {
2096 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
2097 (struct sockaddr *)&mask6, 0, &rt, fibnum);
2099 /* report the route deletion to the routing socket. */
2101 nd6_rtmsg(RTM_DELETE, rt);
2103 /* Save last error to return, see rtinit(). */
2113 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
2116 * There might be the same prefix on another interface,
2117 * the prefix which could not be on-link just because we have
2118 * the interface route (see comments in nd6_prefix_onlink).
2119 * If there's one, try to make the prefix on-link on the
2124 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
2126 * KAME specific: detached prefixes should not be
2129 if (opr == pr || (opr->ndpr_stateflags &
2130 (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
2133 if (opr->ndpr_plen == pr->ndpr_plen &&
2134 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2135 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2138 genid = V_nd6_list_genid;
2140 if ((e = nd6_prefix_onlink(opr)) != 0) {
2142 "nd6_prefix_offlink: failed to "
2143 "recover a prefix %s/%d from %s "
2144 "to %s (errno = %d)\n",
2146 &opr->ndpr_prefix.sin6_addr),
2147 opr->ndpr_plen, if_name(ifp),
2148 if_name(opr->ndpr_ifp), e));
2152 if (genid != V_nd6_list_genid)
2158 /* XXX: can we still set the NDPRF_ONLINK flag? */
2160 "nd6_prefix_offlink: failed to delete route: "
2161 "%s/%d on %s (errno = %d)\n",
2162 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
2163 if_name(ifp), error));
2167 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2168 (struct sockaddr *)&mask6, LLE_STATIC);
2173 static struct in6_ifaddr *
2174 in6_ifadd(struct nd_prefixctl *pr, int mcast)
2176 struct ifnet *ifp = pr->ndpr_ifp;
2178 struct in6_aliasreq ifra;
2179 struct in6_ifaddr *ia, *ib;
2181 struct in6_addr mask;
2182 int prefixlen = pr->ndpr_plen;
2184 char ip6buf[INET6_ADDRSTRLEN];
2186 in6_prefixlen2mask(&mask, prefixlen);
2189 * find a link-local address (will be interface ID).
2190 * Is it really mandatory? Theoretically, a global or a site-local
2191 * address can be configured without a link-local address, if we
2192 * have a unique interface identifier...
2194 * it is not mandatory to have a link-local address, we can generate
2195 * interface identifier on the fly. we do this because:
2196 * (1) it should be the easiest way to find interface identifier.
2197 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
2198 * for multiple addresses on a single interface, and possible shortcut
2199 * of DAD. we omitted DAD for this reason in the past.
2200 * (3) a user can prevent autoconfiguration of global address
2201 * by removing link-local address by hand (this is partly because we
2202 * don't have other way to control the use of IPv6 on an interface.
2203 * this has been our design choice - cf. NRL's "ifconfig auto").
2204 * (4) it is easier to manage when an interface has addresses
2205 * with the same interface identifier, than to have multiple addresses
2206 * with different interface identifiers.
2208 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
2210 ib = (struct in6_ifaddr *)ifa;
2214 /* prefixlen + ifidlen must be equal to 128 */
2215 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
2216 if (prefixlen != plen0) {
2218 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
2219 "(prefix=%d ifid=%d)\n",
2220 if_name(ifp), prefixlen, 128 - plen0));
2225 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
2227 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
2229 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
2230 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
2231 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
2232 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
2233 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2234 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
2235 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2236 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
2240 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
2241 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
2243 /* XXX: scope zone ID? */
2245 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
2248 * Make sure that we do not have this address already. This should
2249 * usually not happen, but we can still see this case, e.g., if we
2250 * have manually configured the exact address to be configured.
2252 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
2253 &ifra.ifra_addr.sin6_addr);
2256 /* this should be rare enough to make an explicit log */
2257 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
2258 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
2263 * Allocate ifaddr structure, link into chain, etc.
2264 * If we are going to create a new address upon receiving a multicasted
2265 * RA, we need to impose a random delay before starting DAD.
2266 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
2270 updateflags |= IN6_IFAUPDATE_DADDELAY;
2271 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
2273 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
2274 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
2275 if_name(ifp), error));
2276 return (NULL); /* ifaddr must not have been allocated. */
2279 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2281 * XXXRW: Assumption of non-NULLness here might not be true with
2282 * fine-grained locking -- should we validate it? Or just return
2283 * earlier ifa rather than looking it up again?
2285 return (ia); /* this is always non-NULL and referenced. */
2289 * ia0 - corresponding public address
2292 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
2294 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
2295 struct in6_ifaddr *newia;
2296 struct in6_aliasreq ifra;
2298 int trylimit = 3; /* XXX: adhoc value */
2300 u_int32_t randid[2];
2301 time_t vltime0, pltime0;
2303 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
2304 &ia0->ia_prefixmask.sin6_addr);
2306 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
2307 /* clear the old IFID */
2308 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
2309 &ifra.ifra_prefixmask.sin6_addr);
2312 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2313 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2314 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2318 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2319 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
2320 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2321 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
2324 * in6_get_tmpifid() quite likely provided a unique interface ID.
2325 * However, we may still have a chance to see collision, because
2326 * there may be a time lag between generation of the ID and generation
2327 * of the address. So, we'll do one more sanity check.
2330 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
2331 if (trylimit-- > 0) {
2336 /* Give up. Something strange should have happened. */
2337 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2338 "find a unique random IFID\n"));
2343 * The Valid Lifetime is the lower of the Valid Lifetime of the
2344 * public address or TEMP_VALID_LIFETIME.
2345 * The Preferred Lifetime is the lower of the Preferred Lifetime
2346 * of the public address or TEMP_PREFERRED_LIFETIME -
2349 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2350 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2351 (ia0->ia6_lifetime.ia6t_vltime -
2352 (time_uptime - ia0->ia6_updatetime));
2353 if (vltime0 > V_ip6_temp_valid_lifetime)
2354 vltime0 = V_ip6_temp_valid_lifetime;
2356 vltime0 = V_ip6_temp_valid_lifetime;
2357 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2358 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2359 (ia0->ia6_lifetime.ia6t_pltime -
2360 (time_uptime - ia0->ia6_updatetime));
2361 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2362 pltime0 = V_ip6_temp_preferred_lifetime -
2363 V_ip6_desync_factor;
2366 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
2367 ifra.ifra_lifetime.ia6t_vltime = vltime0;
2368 ifra.ifra_lifetime.ia6t_pltime = pltime0;
2371 * A temporary address is created only if this calculated Preferred
2372 * Lifetime is greater than REGEN_ADVANCE time units.
2374 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
2377 /* XXX: scope zone ID? */
2379 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
2381 /* allocate ifaddr structure, link into chain, etc. */
2384 updateflags |= IN6_IFAUPDATE_DADDELAY;
2385 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2388 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2389 if (newia == NULL) { /* XXX: can it happen? */
2391 "in6_tmpifadd: ifa update succeeded, but we got "
2393 return (EINVAL); /* XXX */
2395 newia->ia6_ndpr = ia0->ia6_ndpr;
2396 newia->ia6_ndpr->ndpr_addrcnt++;
2397 ifa_free(&newia->ia_ifa);
2400 * A newly added address might affect the status of other addresses.
2401 * XXX: when the temporary address is generated with a new public
2402 * address, the onlink check is redundant. However, it would be safe
2403 * to do the check explicitly everywhere a new address is generated,
2404 * and, in fact, we surely need the check when we create a new
2405 * temporary address due to deprecation of an old temporary address.
2407 pfxlist_onlink_check();
2413 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2415 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2416 ndpr->ndpr_preferred = 0;
2418 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2419 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2420 ndpr->ndpr_expire = 0;
2422 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2428 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
2430 /* init ia6t_expire */
2431 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2432 lt6->ia6t_expire = 0;
2434 lt6->ia6t_expire = time_uptime;
2435 lt6->ia6t_expire += lt6->ia6t_vltime;
2438 /* init ia6t_preferred */
2439 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2440 lt6->ia6t_preferred = 0;
2442 lt6->ia6t_preferred = time_uptime;
2443 lt6->ia6t_preferred += lt6->ia6t_pltime;
2448 * Delete all the routing table entries that use the specified gateway.
2449 * XXX: this function causes search through all entries of routing table, so
2450 * it shouldn't be called when acting as a router.
2453 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2456 /* We'll care only link-local addresses */
2457 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2460 /* XXX Do we really need to walk any but the default FIB? */
2461 rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2465 rt6_deleteroute(const struct rtentry *rt, void *arg)
2467 #define SIN6(s) ((struct sockaddr_in6 *)s)
2468 struct in6_addr *gate = (struct in6_addr *)arg;
2470 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2473 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
2478 * Do not delete a static route.
2479 * XXX: this seems to be a bit ad-hoc. Should we consider the
2480 * 'cloned' bit instead?
2482 if ((rt->rt_flags & RTF_STATIC) != 0)
2486 * We delete only host route. This means, in particular, we don't
2487 * delete default route.
2489 if ((rt->rt_flags & RTF_HOST) == 0)
2497 nd6_setdefaultiface(int ifindex)
2501 if (ifindex < 0 || V_if_index < ifindex)
2503 if (ifindex != 0 && !ifnet_byindex(ifindex))
2506 if (V_nd6_defifindex != ifindex) {
2507 V_nd6_defifindex = ifindex;
2508 if (V_nd6_defifindex > 0)
2509 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2511 V_nd6_defifp = NULL;
2514 * Our current implementation assumes one-to-one maping between
2515 * interfaces and links, so it would be natural to use the
2516 * default interface as the default link.
2518 scope6_setdefault(V_nd6_defifp);