1 /* $FreeBSD: src/sys/netinet6/nd6_rtr.c,v 1.2.2.5 2003/04/05 10:28:53 ume Exp $ */
2 /* $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
45 #include <sys/syslog.h>
46 #include <sys/queue.h>
47 #include <sys/globaldata.h>
48 #include <sys/mutex.h>
50 #include <sys/thread2.h>
51 #include <sys/mutex2.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/radix.h>
59 #include <netinet/in.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/in6_ifattach.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #include <netinet6/nd6.h>
65 #include <netinet/icmp6.h>
66 #include <netinet6/scope6_var.h>
68 #include <net/net_osdep.h>
70 #define SDL(s) ((struct sockaddr_dl *)s)
72 static struct nd_defrouter *defrtrlist_update (struct nd_defrouter *);
73 static struct in6_ifaddr *in6_ifadd (struct nd_prefix *,
75 static struct nd_pfxrouter *pfxrtr_lookup (struct nd_prefix *,
76 struct nd_defrouter *);
77 static void pfxrtr_add (struct nd_prefix *, struct nd_defrouter *);
78 static void pfxrtr_del (struct nd_pfxrouter *);
79 static struct nd_pfxrouter *find_pfxlist_reachable_router
81 static void defrouter_addifreq (struct ifnet *);
83 static void in6_init_address_ltimes(struct nd_prefix *ndpr,
84 struct in6_addrlifetime *lt6);
86 static int rt6_deleteroute (struct radix_node *, void *);
88 extern int nd6_recalc_reachtm_interval;
90 static struct ifnet *nd6_defifp;
93 int ip6_use_tempaddr = 0;
95 int ip6_desync_factor;
96 u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
97 u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
99 * shorter lifetimes for debugging purposes.
100 int ip6_temp_preferred_lifetime = 800;
101 static int ip6_temp_valid_lifetime = 1800;
103 int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
106 * Receive Router Solicitation Message - just for routers.
107 * Router solicitation/advertisement is mostly managed by userland program
108 * (rtadvd) so here we have no function like nd6_ra_output().
113 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
115 struct ifnet *ifp = m->m_pkthdr.rcvif;
116 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
117 struct nd_router_solicit *nd_rs;
118 struct in6_addr saddr6 = ip6->ip6_src;
122 struct sockaddr_dl *sdl = NULL;
123 struct llinfo_nd6 *ln = NULL;
124 struct rtentry *rt = NULL;
127 union nd_opts ndopts;
129 /* If I'm not a router, ignore it. */
130 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
134 if (ip6->ip6_hlim != 255) {
136 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
137 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
138 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
143 * Don't update the neighbor cache, if src = ::.
144 * This indicates that the src has no IP address assigned yet.
146 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
149 #ifndef PULLDOWN_TEST
150 IP6_EXTHDR_CHECK(m, off, icmp6len,);
151 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
153 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
155 icmp6stat.icp6s_tooshort++;
160 icmp6len -= sizeof(*nd_rs);
161 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
162 if (nd6_options(&ndopts) < 0) {
164 "nd6_rs_input: invalid ND option, ignored\n"));
165 /* nd6_options have incremented stats */
169 if (ndopts.nd_opts_src_lladdr) {
170 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
171 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
174 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
176 "nd6_rs_input: lladdrlen mismatch for %s "
177 "(if %d, RS packet %d)\n",
178 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
182 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
189 icmp6stat.icp6s_badrs++;
194 * Receive Router Advertisement Message.
197 * TODO: on-link bit on prefix information
198 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
201 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
203 struct ifnet *ifp = m->m_pkthdr.rcvif;
204 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
205 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
206 struct nd_router_advert *nd_ra;
207 struct in6_addr saddr6 = ip6->ip6_src;
209 struct in6_addr daddr6 = ip6->ip6_dst;
210 int flags; /* = nd_ra->nd_ra_flags_reserved; */
211 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
212 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
214 union nd_opts ndopts;
215 struct nd_defrouter *dr;
218 * We only accept RAs only when
219 * the system-wide variable allows the acceptance, and
220 * per-interface variable allows RAs on the receiving interface.
222 if (ip6_accept_rtadv == 0)
224 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
227 if (ip6->ip6_hlim != 255) {
229 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
230 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
231 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
235 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
237 "nd6_ra_input: src %s is not link-local\n",
238 ip6_sprintf(&saddr6)));
242 #ifndef PULLDOWN_TEST
243 IP6_EXTHDR_CHECK(m, off, icmp6len,);
244 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
246 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
248 icmp6stat.icp6s_tooshort++;
253 icmp6len -= sizeof(*nd_ra);
254 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
255 if (nd6_options(&ndopts) < 0) {
257 "nd6_ra_input: invalid ND option, ignored\n"));
258 /* nd6_options have incremented stats */
263 struct nd_defrouter dr0;
264 u_int32_t advreachable = nd_ra->nd_ra_reachable;
267 dr0.flags = nd_ra->nd_ra_flags_reserved;
268 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
269 dr0.expire = time_uptime + dr0.rtlifetime;
271 dr0.advint = 0; /* Mobile IPv6 */
272 dr0.advint_expire = 0; /* Mobile IPv6 */
273 dr0.advints_lost = 0; /* Mobile IPv6 */
274 /* unspecified or not? (RFC 2461 6.3.4) */
276 advreachable = ntohl(advreachable);
277 if (advreachable <= MAX_REACHABLE_TIME &&
278 ndi->basereachable != advreachable) {
279 ndi->basereachable = advreachable;
280 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
281 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
284 if (nd_ra->nd_ra_retransmit)
285 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
286 if (nd_ra->nd_ra_curhoplimit)
287 ndi->chlim = nd_ra->nd_ra_curhoplimit;
288 dr = defrtrlist_update(&dr0);
294 if (ndopts.nd_opts_pi) {
295 struct nd_opt_hdr *pt;
296 struct nd_opt_prefix_info *pi = NULL;
299 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
300 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
301 pt = (struct nd_opt_hdr *)((caddr_t)pt +
302 (pt->nd_opt_len << 3))) {
303 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
305 pi = (struct nd_opt_prefix_info *)pt;
307 if (pi->nd_opt_pi_len != 4) {
309 "nd6_ra_input: invalid option "
310 "len %d for prefix information option, "
311 "ignored\n", pi->nd_opt_pi_len));
315 if (128 < pi->nd_opt_pi_prefix_len) {
317 "nd6_ra_input: invalid prefix "
318 "len %d for prefix information option, "
319 "ignored\n", pi->nd_opt_pi_prefix_len));
323 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
324 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
326 "nd6_ra_input: invalid prefix "
328 ip6_sprintf(&pi->nd_opt_pi_prefix)));
332 bzero(&pr, sizeof(pr));
333 pr.ndpr_prefix.sin6_family = AF_INET6;
334 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
335 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
336 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
338 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
339 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
340 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
341 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
342 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
343 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
344 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
345 if (in6_init_prefix_ltimes(&pr))
346 continue; /* prefix lifetime init failed */
347 prelist_update(&pr, dr, m);
354 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
355 u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
358 if (mtu < IPV6_MMTU) {
359 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
360 "mtu=%d sent from %s, ignoring\n",
361 mtu, ip6_sprintf(&ip6->ip6_src)));
367 if (mtu <= ndi->maxmtu) {
368 int change = (ndi->linkmtu != mtu);
371 if (change) /* in6_maxmtu may change */
374 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
375 "mtu=%d sent from %s; "
376 "exceeds maxmtu %d, ignoring\n",
377 mtu, ip6_sprintf(&ip6->ip6_src),
381 nd6log((LOG_INFO, "nd6_ra_input: mtu option "
382 "mtu=%d sent from %s; maxmtu unknown, "
384 mtu, ip6_sprintf(&ip6->ip6_src)));
391 * Source link layer address
397 if (ndopts.nd_opts_src_lladdr) {
398 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
399 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
402 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
404 "nd6_ra_input: lladdrlen mismatch for %s "
405 "(if %d, RA packet %d)\n",
406 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
410 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
413 * Installing a link-layer address might change the state of the
414 * router's neighbor cache, which might also affect our on-link
415 * detection of adveritsed prefixes.
417 pfxlist_onlink_check();
425 icmp6stat.icp6s_badra++;
430 * default router list proccessing sub routines
434 /* tell the change to user processes watching the routing socket. */
436 nd6_rtmsg(int cmd, struct rtentry *rt)
438 struct rt_addrinfo info;
440 bzero((caddr_t)&info, sizeof(info));
441 info.rti_info[RTAX_DST] = rt_key(rt);
442 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
443 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
444 if (TAILQ_EMPTY(&rt->rt_ifp->if_addrheads[mycpuid])) {
445 info.rti_info[RTAX_IFP] = NULL;
447 info.rti_info[RTAX_IFP] =
449 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa;
451 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
453 rt_missmsg(cmd, &info, rt->rt_flags, 0);
458 defrouter_addreq(struct nd_defrouter *new)
460 struct sockaddr_in6 def, mask, gate;
462 bzero(&def, sizeof(def));
463 bzero(&mask, sizeof(mask));
464 bzero(&gate, sizeof(gate));
466 def.sin6_len = mask.sin6_len = gate.sin6_len =
467 sizeof(struct sockaddr_in6);
468 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
469 gate.sin6_addr = new->rtaddr;
471 rtrequest_global(RTM_ADD, (struct sockaddr *)&def,
472 (struct sockaddr *)&gate, (struct sockaddr *)&mask, RTF_GATEWAY);
476 /* Add a route to a given interface as default */
478 defrouter_addifreq(struct ifnet *ifp)
480 struct sockaddr_in6 def, mask;
484 bzero(&def, sizeof(def));
485 bzero(&mask, sizeof(mask));
487 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
488 def.sin6_family = mask.sin6_family = AF_INET6;
491 * Search for an ifaddr beloging to the specified interface.
492 * XXX: An IPv6 address are required to be assigned on the interface.
494 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
495 nd6log((LOG_ERR, /* better error? */
496 "defrouter_addifreq: failed to find an ifaddr "
497 "to install a route to interface %s\n",
502 flags = ifa->ifa_flags;
503 error = rtrequest_global(RTM_ADD, (struct sockaddr *)&def,
504 ifa->ifa_addr, (struct sockaddr *)&mask, flags);
507 "defrouter_addifreq: failed to install a route to "
508 "interface %s (errno = %d)\n",
509 if_name(ifp), error));
513 struct nd_defrouter *
514 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
516 struct nd_defrouter *dr;
518 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
519 dr = TAILQ_NEXT(dr, dr_entry)) {
520 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
524 return (NULL); /* search failed */
528 defrouter_delreq(struct nd_defrouter *dr, int dofree)
530 struct sockaddr_in6 def, mask, gate;
532 bzero(&def, sizeof(def));
533 bzero(&mask, sizeof(mask));
534 bzero(&gate, sizeof(gate));
536 def.sin6_len = mask.sin6_len = gate.sin6_len =
537 sizeof(struct sockaddr_in6);
538 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
539 gate.sin6_addr = dr->rtaddr;
541 rtrequest_global(RTM_DELETE, (struct sockaddr *)&def,
542 (struct sockaddr *)&gate, (struct sockaddr *)&mask, RTF_GATEWAY);
543 if (dofree) /* XXX: necessary? */
548 defrtrlist_del(struct nd_defrouter *dr)
550 struct nd_defrouter *deldr = NULL;
551 struct nd_prefix *pr;
554 * Flush all the routing table entries that use the router
557 if (!ip6_forwarding && ip6_accept_rtadv) /* XXX: better condition? */
558 rt6_flush(&dr->rtaddr, dr->ifp);
560 if (dr == TAILQ_FIRST(&nd_defrouter))
561 deldr = dr; /* The router is primary. */
563 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
566 * Also delete all the pointers to the router in each prefix lists.
568 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
569 struct nd_pfxrouter *pfxrtr;
570 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
573 pfxlist_onlink_check();
576 * If the router is the primary one, choose a new one.
577 * Note that defrouter_select() will remove the current gateway
578 * from the routing table.
587 * Default Router Selection according to Section 6.3.6 of RFC 2461:
588 * 1) Routers that are reachable or probably reachable should be
590 * 2) When no routers on the list are known to be reachable or
591 * probably reachable, routers SHOULD be selected in a round-robin
593 * 3) If the Default Router List is empty, assume that all
594 * destinations are on-link.
597 defrouter_select(void)
599 struct nd_defrouter *dr, anydr;
600 struct rtentry *rt = NULL;
601 struct llinfo_nd6 *ln = NULL;
606 * Search for a (probably) reachable router from the list.
608 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
609 dr = TAILQ_NEXT(dr, dr_entry)) {
610 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
611 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
612 ND6_IS_LLINFO_PROBREACH(ln)) {
613 /* Got it, and move it to the head */
614 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
615 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
620 if ((dr = TAILQ_FIRST(&nd_defrouter))) {
622 * De-install the previous default gateway and install
624 * Note that if there is no reachable router in the list,
625 * the head entry will be used anyway.
626 * XXX: do we have to check the current routing table entry?
628 bzero(&anydr, sizeof(anydr));
629 defrouter_delreq(&anydr, 0);
630 defrouter_addreq(dr);
634 * The Default Router List is empty, so install the default
635 * route to an inteface.
636 * XXX: The specification does not say this mechanism should
637 * be restricted to hosts, but this would be not useful
638 * (even harmful) for routers.
640 if (!ip6_forwarding) {
642 * De-install the current default route
645 bzero(&anydr, sizeof(anydr));
646 defrouter_delreq(&anydr, 0);
649 * Install a route to the default interface
651 * XXX: we enable this for host only, because
652 * this may override a default route installed
653 * a user process (e.g. routing daemon) in a
656 defrouter_addifreq(nd6_defifp);
658 nd6log((LOG_INFO, "defrouter_select: "
659 "there's no default router and no default"
664 mtx_unlock(&nd6_mtx);
668 static struct nd_defrouter *
669 defrtrlist_update(struct nd_defrouter *new)
671 struct nd_defrouter *dr, *n;
675 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
677 if (new->rtlifetime == 0) {
682 dr->flags = new->flags; /* xxx flag check */
683 dr->rtlifetime = new->rtlifetime;
684 dr->expire = new->expire;
686 mtx_unlock(&nd6_mtx);
690 /* entry does not exist */
691 if (new->rtlifetime == 0) {
692 mtx_unlock(&nd6_mtx);
696 n = (struct nd_defrouter *)kmalloc(sizeof(*n), M_IP6NDP,
699 mtx_unlock(&nd6_mtx);
705 * Insert the new router at the end of the Default Router List.
706 * If there is no other router, install it anyway. Otherwise,
707 * just continue to use the current default router.
709 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
710 if (TAILQ_FIRST(&nd_defrouter) == n)
712 mtx_unlock(&nd6_mtx);
716 static struct nd_pfxrouter *
717 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
719 struct nd_pfxrouter *search;
721 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
722 if (search->router == dr)
730 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
732 struct nd_pfxrouter *new;
734 new = kmalloc(sizeof(*new), M_IP6NDP, M_INTWAIT | M_ZERO);
737 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
739 pfxlist_onlink_check();
743 pfxrtr_del(struct nd_pfxrouter *pfr)
745 LIST_REMOVE(pfr, pfr_entry);
746 kfree(pfr, M_IP6NDP);
750 nd6_prefix_lookup(struct nd_prefix *pr)
752 struct nd_prefix *search;
754 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
755 if (pr->ndpr_ifp == search->ndpr_ifp &&
756 pr->ndpr_plen == search->ndpr_plen &&
757 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
758 &search->ndpr_prefix.sin6_addr, pr->ndpr_plen))
766 nd6_prelist_add(struct nd_prefix *pr, struct nd_defrouter *dr,
767 struct nd_prefix **newp)
769 struct nd_prefix *new = NULL;
772 new = kmalloc(sizeof(*new), M_IP6NDP, M_INTWAIT);
778 LIST_INIT(&new->ndpr_advrtrs);
779 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
780 /* make prefix in the canonical form */
781 for (i = 0; i < 4; i++) {
782 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
783 new->ndpr_mask.s6_addr32[i];
787 /* link ndpr_entry to nd_prefix list */
788 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
789 mtx_unlock(&nd6_mtx);
791 /* ND_OPT_PI_FLAG_ONLINK processing */
792 if (new->ndpr_raf_onlink) {
795 if ((e = nd6_prefix_onlink(new)) != 0) {
796 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
797 "the prefix %s/%d on-link on %s (errno=%d)\n",
798 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
799 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
800 /* proceed anyway. XXX: is it correct? */
811 prelist_remove(struct nd_prefix *pr)
813 struct nd_pfxrouter *pfr, *next;
816 /* make sure to invalidate the prefix until it is really freed. */
821 * Though these flags are now meaningless, we'd rather keep the value
822 * not to confuse users when executing "ndp -p".
824 pr->ndpr_raf_onlink = 0;
825 pr->ndpr_raf_auto = 0;
827 if ((pr->ndpr_stateflags & NDPRF_ONLINK) &&
828 (e = nd6_prefix_offlink(pr)) != 0) {
829 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
831 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
832 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
833 /* what should we do? */
836 if (pr->ndpr_refcnt > 0)
837 return; /* notice here? */
841 /* unlink ndpr_entry from nd_prefix list */
842 LIST_REMOVE(pr, ndpr_entry);
844 /* free list of routers that adversed the prefix */
845 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
846 next = pfr->pfr_next;
848 kfree(pfr, M_IP6NDP);
850 mtx_unlock(&nd6_mtx);
854 pfxlist_onlink_check();
862 prelist_update(struct nd_prefix *new, struct nd_defrouter *dr, struct mbuf *m)
864 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
865 struct ifaddr_container *ifac;
866 struct ifnet *ifp = new->ndpr_ifp;
867 struct nd_prefix *pr;
870 struct in6_addrlifetime lt6_tmp;
876 * Authenticity for NA consists authentication for
877 * both IP header and IP datagrams, doesn't it ?
879 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
880 auth = ((m->m_flags & M_AUTHIPHDR) &&
881 (m->m_flags & M_AUTHIPDGM));
885 if ((pr = nd6_prefix_lookup(new)) != NULL) {
887 * nd6_prefix_lookup() ensures that pr and new have the same
888 * prefix on a same interface.
892 * Update prefix information. Note that the on-link (L) bit
893 * and the autonomous (A) bit should NOT be changed from 1
896 if (new->ndpr_raf_onlink == 1)
897 pr->ndpr_raf_onlink = 1;
898 if (new->ndpr_raf_auto == 1)
899 pr->ndpr_raf_auto = 1;
900 if (new->ndpr_raf_onlink) {
901 pr->ndpr_vltime = new->ndpr_vltime;
902 pr->ndpr_pltime = new->ndpr_pltime;
903 pr->ndpr_preferred = new->ndpr_preferred;
904 pr->ndpr_expire = new->ndpr_expire;
907 if (new->ndpr_raf_onlink &&
908 !(pr->ndpr_stateflags & NDPRF_ONLINK)) {
911 if ((e = nd6_prefix_onlink(pr)) != 0) {
913 "prelist_update: failed to make "
914 "the prefix %s/%d on-link on %s "
916 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
917 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
918 /* proceed anyway. XXX: is it correct? */
922 if (dr && pfxrtr_lookup(pr, dr) == NULL)
925 struct nd_prefix *newpr = NULL;
927 if (new->ndpr_vltime == 0)
929 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
932 bzero(&new->ndpr_addr, sizeof(struct in6_addr));
934 error = nd6_prelist_add(new, dr, &newpr);
935 if (error != 0 || newpr == NULL) {
936 nd6log((LOG_NOTICE, "prelist_update: "
937 "nd6_prelist_add failed for %s/%d on %s "
938 "errno=%d, returnpr=%p\n",
939 ip6_sprintf(&new->ndpr_prefix.sin6_addr),
940 new->ndpr_plen, if_name(new->ndpr_ifp),
942 goto end; /* we should just give up in this case. */
946 * XXX: from the ND point of view, we can ignore a prefix
947 * with the on-link bit being zero. However, we need a
948 * prefix structure for references from autoconfigured
949 * addresses. Thus, we explicitly make sure that the prefix
950 * itself expires now.
952 if (newpr->ndpr_raf_onlink == 0) {
953 newpr->ndpr_vltime = 0;
954 newpr->ndpr_pltime = 0;
955 in6_init_prefix_ltimes(newpr);
962 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
963 * Note that pr must be non NULL at this point.
966 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
967 if (!new->ndpr_raf_auto)
971 * 5.5.3 (b). the link-local prefix should have been ignored in
976 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime.
977 * This should have been done in nd6_ra_input.
981 * 5.5.3 (d). If the prefix advertised does not match the prefix of an
982 * address already in the list, and the Valid Lifetime is not 0,
983 * form an address. Note that even a manually configured address
984 * should reject autoconfiguration of a new address.
986 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
987 struct ifaddr *ifa = ifac->ifa;
988 struct in6_ifaddr *ifa6;
990 u_int32_t storedlifetime;
992 if (ifa->ifa_addr->sa_family != AF_INET6)
995 ifa6 = (struct in6_ifaddr *)ifa;
998 * Spec is not clear here, but I believe we should concentrate
999 * on unicast (i.e. not anycast) addresses.
1000 * XXX: other ia6_flags? detached or duplicated?
1002 if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
1005 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL);
1006 if (ifa_plen != new->ndpr_plen ||
1007 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr,
1008 &new->ndpr_prefix.sin6_addr, ifa_plen))
1011 if (ia6_match == NULL) /* remember the first one */
1014 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1018 * An already autoconfigured address matched. Now that we
1019 * are sure there is at least one matched address, we can
1020 * proceed to 5.5.3. (e): update the lifetimes according to the
1021 * "two hours" rule and the privacy extension.
1023 #define TWOHOUR (120*60)
1024 lt6_tmp = ifa6->ia6_lifetime;
1026 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1027 storedlifetime = ND6_INFINITE_LIFETIME;
1028 else if (IFA6_IS_INVALID(ifa6))
1031 storedlifetime = lt6_tmp.ia6t_expire - time_uptime;
1033 /* when not updating, keep the current stored lifetime. */
1034 lt6_tmp.ia6t_vltime = storedlifetime;
1036 if (TWOHOUR < new->ndpr_vltime ||
1037 storedlifetime < new->ndpr_vltime) {
1038 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1039 } else if (storedlifetime <= TWOHOUR
1042 * This condition is logically redundant, so we just
1044 * See IPng 6712, 6717, and 6721.
1046 && new->ndpr_vltime <= storedlifetime
1050 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1054 * new->ndpr_vltime <= TWOHOUR &&
1055 * TWOHOUR < storedlifetime
1057 lt6_tmp.ia6t_vltime = TWOHOUR;
1060 /* The 2 hour rule is not imposed for preferred lifetime. */
1061 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1063 in6_init_address_ltimes(pr, <6_tmp);
1066 * When adjusting the lifetimes of an existing temporary
1067 * address, only lower the lifetimes.
1068 * RFC 3041 3.3. (1).
1069 * XXX: how should we modify ia6t_[pv]ltime?
1071 if (ifa6->ia6_flags & IN6_IFF_TEMPORARY) {
1072 if (lt6_tmp.ia6t_expire == 0 || /* no expire */
1073 lt6_tmp.ia6t_expire >
1074 ifa6->ia6_lifetime.ia6t_expire) {
1075 lt6_tmp.ia6t_expire =
1076 ifa6->ia6_lifetime.ia6t_expire;
1078 if (lt6_tmp.ia6t_preferred == 0 || /* no expire */
1079 lt6_tmp.ia6t_preferred >
1080 ifa6->ia6_lifetime.ia6t_preferred) {
1081 lt6_tmp.ia6t_preferred =
1082 ifa6->ia6_lifetime.ia6t_preferred;
1086 ifa6->ia6_lifetime = lt6_tmp;
1088 if (ia6_match == NULL && new->ndpr_vltime) {
1090 * No address matched and the valid lifetime is non-zero.
1091 * Create a new address.
1093 if ((ia6 = in6_ifadd(new, NULL)) != NULL) {
1095 * note that we should use pr (not new) for reference.
1102 * When a new public address is created as described
1103 * in RFC2462, also create a new temporary address.
1106 * When an interface connects to a new link, a new
1107 * randomized interface identifier should be generated
1108 * immediately together with a new set of temporary
1109 * addresses. Thus, we specifiy 1 as the 2nd arg of
1112 if (ip6_use_tempaddr) {
1114 if ((e = in6_tmpifadd(ia6, 1)) != 0) {
1115 nd6log((LOG_NOTICE, "prelist_update: "
1116 "failed to create a temporary "
1117 "address, errno=%d\n",
1123 * A newly added address might affect the status
1124 * of other addresses, so we check and update it.
1125 * XXX: what if address duplication happens?
1127 pfxlist_onlink_check();
1129 /* just set an error. do not bark here. */
1130 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1137 mtx_unlock(&nd6_mtx);
1142 * A supplement function used in the on-link detection below;
1143 * detect if a given prefix has a (probably) reachable advertising router.
1144 * XXX: lengthy function name...
1146 static struct nd_pfxrouter *
1147 find_pfxlist_reachable_router(struct nd_prefix *pr)
1149 struct nd_pfxrouter *pfxrtr;
1151 struct llinfo_nd6 *ln;
1153 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1154 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1155 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1156 pfxrtr->router->ifp)) &&
1157 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1158 ND6_IS_LLINFO_PROBREACH(ln))
1166 * Check if each prefix in the prefix list has at least one available router
1167 * that advertised the prefix (a router is "available" if its neighbor cache
1168 * entry is reachable or probably reachable).
1169 * If the check fails, the prefix may be off-link, because, for example,
1170 * we have moved from the network but the lifetime of the prefix has not
1171 * expired yet. So we should not use the prefix if there is another prefix
1172 * that has an available router.
1173 * But, if there is no prefix that has an available router, we still regards
1174 * all the prefixes as on-link. This is because we can't tell if all the
1175 * routers are simply dead or if we really moved from the network and there
1176 * is no router around us.
1179 pfxlist_onlink_check(void)
1181 struct nd_prefix *pr;
1182 struct in6_ifaddr *ifa;
1185 * Check if there is a prefix that has a reachable advertising
1188 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1189 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1195 * There is at least one prefix that has a reachable router.
1196 * Detach prefixes which have no reachable advertising
1197 * router, and attach other prefixes.
1199 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1200 /* XXX: a link-local prefix should never be detached */
1201 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1205 * we aren't interested in prefixes without the L bit
1208 if (pr->ndpr_raf_onlink == 0)
1211 if (!(pr->ndpr_stateflags & NDPRF_DETACHED) &&
1212 find_pfxlist_reachable_router(pr) == NULL)
1213 pr->ndpr_stateflags |= NDPRF_DETACHED;
1214 if ((pr->ndpr_stateflags & NDPRF_DETACHED) &&
1215 find_pfxlist_reachable_router(pr) != NULL)
1216 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1219 /* there is no prefix that has a reachable router */
1220 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1221 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1224 if (pr->ndpr_raf_onlink == 0)
1227 if (pr->ndpr_stateflags & NDPRF_DETACHED)
1228 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1233 * Remove each interface route associated with a (just) detached
1234 * prefix, and reinstall the interface route for a (just) attached
1235 * prefix. Note that all attempt of reinstallation does not
1236 * necessarily success, when a same prefix is shared among multiple
1237 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1238 * so we don't have to care about them.
1240 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1243 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1246 if (pr->ndpr_raf_onlink == 0)
1249 if ((pr->ndpr_stateflags & NDPRF_DETACHED) &&
1250 (pr->ndpr_stateflags & NDPRF_ONLINK)) {
1251 if ((e = nd6_prefix_offlink(pr)) != 0) {
1253 "pfxlist_onlink_check: failed to "
1254 "make %s/%d offlink, errno=%d\n",
1255 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1259 if (!(pr->ndpr_stateflags & NDPRF_DETACHED) &&
1260 !(pr->ndpr_stateflags & NDPRF_ONLINK) &&
1261 pr->ndpr_raf_onlink) {
1262 if ((e = nd6_prefix_onlink(pr)) != 0) {
1264 "pfxlist_onlink_check: failed to "
1265 "make %s/%d offlink, errno=%d\n",
1266 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1273 * Changes on the prefix status might affect address status as well.
1274 * Make sure that all addresses derived from an attached prefix are
1275 * attached, and that all addresses derived from a detached prefix are
1276 * detached. Note, however, that a manually configured address should
1277 * always be attached.
1278 * The precise detection logic is same as the one for prefixes.
1280 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1281 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1284 if (ifa->ia6_ndpr == NULL) {
1286 * This can happen when we first configure the address
1287 * (i.e. the address exists, but the prefix does not).
1288 * XXX: complicated relationships...
1293 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1297 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1298 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1301 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1304 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1305 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1307 ifa->ia6_flags |= IN6_IFF_DETACHED;
1311 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1312 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1315 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1321 nd6_prefix_onlink(struct nd_prefix *pr)
1324 struct ifnet *ifp = pr->ndpr_ifp;
1325 struct sockaddr_in6 mask6;
1326 struct nd_prefix *opr;
1331 if (pr->ndpr_stateflags & NDPRF_ONLINK) {
1333 "nd6_prefix_onlink: %s/%d is already on-link\n",
1334 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
1339 * Add the interface route associated with the prefix. Before
1340 * installing the route, check if there's the same prefix on another
1341 * interface, and the prefix has already installed the interface route.
1342 * Although such a configuration is expected to be rare, we explicitly
1345 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1349 if (!(opr->ndpr_stateflags & NDPRF_ONLINK))
1352 if (opr->ndpr_plen == pr->ndpr_plen &&
1353 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1354 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1359 * We prefer link-local addresses as the associated interface address.
1361 /* search for a link-local addr */
1362 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1363 IN6_IFF_NOTREADY| IN6_IFF_ANYCAST);
1365 struct ifaddr_container *ifac;
1367 /* XXX: freebsd does not have ifa_ifwithaf */
1368 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1369 if (ifac->ifa->ifa_addr->sa_family == AF_INET6) {
1374 /* should we care about ia6_flags? */
1378 * This can still happen, when, for example, we receive an RA
1379 * containing a prefix with the L bit set and the A bit clear,
1380 * after removing all IPv6 addresses on the receiving
1381 * interface. This should, of course, be rare though.
1384 "nd6_prefix_onlink: failed to find any ifaddr"
1385 " to add route for a prefix(%s/%d) on %s\n",
1386 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1387 pr->ndpr_plen, if_name(ifp)));
1392 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1393 * ifa->ifa_rtrequest = nd6_rtrequest;
1395 bzero(&mask6, sizeof(mask6));
1396 mask6.sin6_len = sizeof(mask6);
1397 mask6.sin6_addr = pr->ndpr_mask;
1398 rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP;
1399 if (nd6_need_cache(ifp)) {
1400 /* explicitly set in case ifa_flags does not set the flag. */
1401 rtflags |= RTF_CLONING;
1404 * explicitly clear the cloning bit in case ifa_flags sets it.
1406 rtflags &= ~RTF_CLONING;
1408 error = rtrequest_global(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1409 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags);
1411 pr->ndpr_stateflags |= NDPRF_ONLINK;
1413 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1414 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1416 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1417 pr->ndpr_plen, if_name(ifp),
1418 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1419 ip6_sprintf(&mask6.sin6_addr), rtflags, error));
1425 nd6_prefix_offlink(struct nd_prefix *pr)
1428 struct ifnet *ifp = pr->ndpr_ifp;
1429 struct nd_prefix *opr;
1430 struct sockaddr_in6 sa6, mask6;
1433 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1435 "nd6_prefix_offlink: %s/%d is already off-link\n",
1436 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1440 bzero(&sa6, sizeof(sa6));
1441 sa6.sin6_family = AF_INET6;
1442 sa6.sin6_len = sizeof(sa6);
1443 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1444 sizeof(struct in6_addr));
1445 bzero(&mask6, sizeof(mask6));
1446 mask6.sin6_family = AF_INET6;
1447 mask6.sin6_len = sizeof(sa6);
1448 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1449 error = rtrequest_global(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1450 (struct sockaddr *)&mask6, 0);
1452 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1455 * There might be the same prefix on another interface,
1456 * the prefix which could not be on-link just because we have
1457 * the interface route (see comments in nd6_prefix_onlink).
1458 * If there's one, try to make the prefix on-link on the
1461 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1465 if (opr->ndpr_stateflags & NDPRF_ONLINK)
1469 * KAME specific: detached prefixes should not be
1472 if (opr->ndpr_stateflags & NDPRF_DETACHED)
1475 if (opr->ndpr_plen == pr->ndpr_plen &&
1476 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1477 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1480 if ((e = nd6_prefix_onlink(opr)) != 0) {
1482 "nd6_prefix_offlink: failed to "
1483 "recover a prefix %s/%d from %s "
1484 "to %s (errno = %d)\n",
1485 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1486 opr->ndpr_plen, if_name(ifp),
1487 if_name(opr->ndpr_ifp), e));
1492 /* XXX: can we still set the NDPRF_ONLINK flag? */
1494 "nd6_prefix_offlink: failed to delete route: "
1495 "%s/%d on %s (errno = %d)\n",
1496 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
1505 * ifid: Mobile IPv6 addition
1507 static struct in6_ifaddr *
1508 in6_ifadd(struct nd_prefix *pr, struct in6_addr *ifid)
1510 struct ifnet *ifp = pr->ndpr_ifp;
1512 struct in6_aliasreq ifra;
1513 struct in6_ifaddr *ia, *ib;
1515 struct in6_addr mask;
1516 int prefixlen = pr->ndpr_plen;
1518 in6_prefixlen2mask(&mask, prefixlen);
1521 * find a link-local address (will be interface ID).
1522 * Is it really mandatory? Theoretically, a global or a site-local
1523 * address can be configured without a link-local address, if we
1524 * have a unique interface identifier...
1526 * it is not mandatory to have a link-local address, we can generate
1527 * interface identifier on the fly. we do this because:
1528 * (1) it should be the easiest way to find interface identifier.
1529 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1530 * for multiple addresses on a single interface, and possible shortcut
1531 * of DAD. we omitted DAD for this reason in the past.
1532 * (3) a user can prevent autoconfiguration of global address
1533 * by removing link-local address by hand (this is partly because we
1534 * don't have other way to control the use of IPv6 on a interface.
1535 * this has been our design choice - cf. NRL's "ifconfig auto").
1536 * (4) it is easier to manage when an interface has addresses
1537 * with the same interface identifier, than to have multiple addresses
1538 * with different interface identifiers.
1540 * Mobile IPv6 addition: allow for caller to specify a wished interface
1541 * ID. This is to not break connections when moving addresses between
1544 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1546 ib = (struct in6_ifaddr *)ifa;
1550 #if 0 /* don't care link local addr state, and always do DAD */
1551 /* if link-local address is not eligible, do not autoconfigure. */
1552 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1553 kprintf("in6_ifadd: link-local address not ready\n");
1558 /* prefixlen + ifidlen must be equal to 128 */
1559 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1560 if (prefixlen != plen0) {
1561 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1562 "(prefix=%d ifid=%d)\n",
1563 if_name(ifp), prefixlen, 128 - plen0));
1569 bzero(&ifra, sizeof(ifra));
1571 * in6_update_ifa() does not use ifra_name, but we accurately set it
1574 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1575 ifra.ifra_addr.sin6_family = AF_INET6;
1576 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1578 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
1579 sizeof(ifra.ifra_addr.sin6_addr));
1580 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1581 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1582 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1583 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1586 if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid))
1587 ifid = &ib->ia_addr.sin6_addr;
1588 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1589 (ifid->s6_addr32[0] & ~mask.s6_addr32[0]);
1590 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1591 (ifid->s6_addr32[1] & ~mask.s6_addr32[1]);
1592 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1593 (ifid->s6_addr32[2] & ~mask.s6_addr32[2]);
1594 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1595 (ifid->s6_addr32[3] & ~mask.s6_addr32[3]);
1597 /* new prefix mask. */
1598 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1599 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1600 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1601 sizeof(ifra.ifra_prefixmask.sin6_addr));
1605 * XXX: in6_init_address_ltimes would override these values later.
1606 * We should reconsider this logic.
1608 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1609 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1611 /* XXX: scope zone ID? */
1613 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1615 * temporarily set the nopfx flag to avoid conflict.
1616 * XXX: we should reconsider the entire mechanism about prefix
1619 ifra.ifra_flags |= IN6_IFF_NOPFX;
1622 * keep the new address, regardless of the result of in6_update_ifa.
1623 * XXX: this address is now meaningless.
1624 * We should reconsider its role.
1626 pr->ndpr_addr = ifra.ifra_addr.sin6_addr;
1628 /* allocate ifaddr structure, link into chain, etc. */
1629 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
1631 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1632 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
1634 return (NULL); /* ifaddr must not have been allocated. */
1637 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1639 return (ia); /* this is always non-NULL */
1644 * ia0: corresponding public address
1647 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen)
1649 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1650 struct in6_ifaddr *newia;
1651 struct in6_aliasreq ifra;
1653 int trylimit = 3; /* XXX: adhoc value */
1654 u_int32_t randid[2];
1655 time_t vltime0, pltime0;
1657 bzero(&ifra, sizeof(ifra));
1658 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1659 ifra.ifra_addr = ia0->ia_addr;
1660 /* copy prefix mask */
1661 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1662 /* clear the old IFID */
1663 for (i = 0; i < 4; i++) {
1664 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1665 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1669 in6_get_tmpifid(ifp, (u_int8_t *)randid,
1670 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen);
1671 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1672 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1673 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1674 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1677 * If by chance the new temporary address is the same as an address
1678 * already assigned to the interface, generate a new randomized
1679 * interface identifier and repeat this step.
1682 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
1683 if (trylimit-- == 0) {
1684 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find "
1685 "a unique random IFID\n"));
1693 * The Valid Lifetime is the lower of the Valid Lifetime of the
1694 * public address or TEMP_VALID_LIFETIME.
1695 * The Preferred Lifetime is the lower of the Preferred Lifetime
1696 * of the public address or TEMP_PREFERRED_LIFETIME -
1699 if (ia0->ia6_lifetime.ia6t_expire != 0) {
1700 vltime0 = IFA6_IS_INVALID(ia0) ?
1701 0 : (ia0->ia6_lifetime.ia6t_expire - time_uptime);
1702 if (vltime0 > ip6_temp_valid_lifetime)
1703 vltime0 = ip6_temp_valid_lifetime;
1705 vltime0 = ip6_temp_valid_lifetime;
1706 if (ia0->ia6_lifetime.ia6t_preferred != 0) {
1707 pltime0 = IFA6_IS_DEPRECATED(ia0) ?
1708 0 : (ia0->ia6_lifetime.ia6t_preferred - time_uptime);
1709 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor) {
1710 pltime0 = ip6_temp_preferred_lifetime -
1714 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
1715 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1716 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1719 * A temporary address is created only if this calculated Preferred
1720 * Lifetime is greater than REGEN_ADVANCE time units.
1722 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
1725 /* XXX: scope zone ID? */
1727 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
1729 /* allocate ifaddr structure, link into chain, etc. */
1730 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0)
1733 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1734 if (newia == NULL) { /* XXX: can it happen? */
1736 "in6_tmpifadd: ifa update succeeded, but we got "
1738 return (EINVAL); /* XXX */
1740 newia->ia6_ndpr = ia0->ia6_ndpr;
1741 newia->ia6_ndpr->ndpr_refcnt++;
1747 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1749 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
1750 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1751 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1752 "(%d) is greater than valid lifetime(%d)\n",
1753 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
1756 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1757 ndpr->ndpr_preferred = 0;
1759 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1760 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1761 ndpr->ndpr_expire = 0;
1763 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
1769 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1771 /* init ia6t_expire */
1772 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1773 lt6->ia6t_expire = 0;
1775 lt6->ia6t_expire = time_uptime;
1776 lt6->ia6t_expire += lt6->ia6t_vltime;
1779 /* init ia6t_preferred */
1780 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1781 lt6->ia6t_preferred = 0;
1783 lt6->ia6t_preferred = time_uptime;
1784 lt6->ia6t_preferred += lt6->ia6t_pltime;
1789 * Delete all the routing table entries that use the specified gateway.
1791 * XXX: this function causes search through all entries of routing table, so
1792 * it shouldn't be called when acting as a router.
1795 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
1797 struct radix_node_head *rnh = rt_tables[mycpuid][AF_INET6];
1799 /* We'll care only link-local addresses */
1800 if (!IN6_IS_ADDR_LINKLOCAL(gateway))
1802 /* XXX: hack for KAME's link-local address kludge */
1803 gateway->s6_addr16[1] = htons(ifp->if_index);
1805 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1809 rt6_deleteroute(struct radix_node *rn, void *arg)
1811 #define SIN6(s) ((struct sockaddr_in6 *)s)
1812 struct rtentry *rt = (struct rtentry *)rn;
1813 struct in6_addr *gate = (struct in6_addr *)arg;
1815 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1818 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1822 * Do not delete a static route.
1823 * XXX: this seems to be a bit ad-hoc. Should we consider the
1824 * 'cloned' bit instead?
1826 if (rt->rt_flags & RTF_STATIC)
1830 * We delete only host route. This means, in particular, we don't
1831 * delete default route.
1833 if (!(rt->rt_flags & RTF_HOST))
1836 return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt),
1842 nd6_setdefaultiface(int ifindex)
1846 if (ifindex < 0 || if_index < ifindex)
1849 if (nd6_defifindex != ifindex) {
1850 nd6_defifindex = ifindex;
1851 if (nd6_defifindex > 0)
1852 nd6_defifp = ifindex2ifnet[nd6_defifindex];
1857 * If the Default Router List is empty, install a route
1858 * to the specified interface as default or remove the default
1859 * route when the default interface becomes canceled.
1860 * The check for the queue is actually redundant, but
1861 * we do this here to avoid re-install the default route
1862 * if the list is NOT empty.
1864 if (TAILQ_FIRST(&nd_defrouter) == NULL)
1868 * Our current implementation assumes one-to-one maping between
1869 * interfaces and links, so it would be natural to use the
1870 * default interface as the default link.
1872 scope6_setdefault(nd6_defifp);