HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / netinet6 / nd6_rtr.c
blob78e4465fe5ca7b7f00e139af79cd8ab6f475d80e
1 /* $FreeBSD: src/sys/netinet6/nd6_rtr.c,v 1.2.2.5 2003/04/05 10:28:53 ume Exp $ */
2 /* $DragonFly: src/sys/netinet6/nd6_rtr.c,v 1.17 2008/03/07 11:34:21 sephe Exp $ */
3 /* $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #include <sys/syslog.h>
47 #include <sys/queue.h>
48 #include <sys/globaldata.h>
49 #include <sys/thread2.h>
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/if_dl.h>
54 #include <net/route.h>
55 #include <net/radix.h>
57 #include <netinet/in.h>
58 #include <netinet6/in6_var.h>
59 #include <netinet6/in6_ifattach.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/nd6.h>
63 #include <netinet/icmp6.h>
64 #include <netinet6/scope6_var.h>
66 #include <net/net_osdep.h>
68 #define SDL(s) ((struct sockaddr_dl *)s)
70 static struct nd_defrouter *defrtrlist_update (struct nd_defrouter *);
71 static struct in6_ifaddr *in6_ifadd (struct nd_prefix *,
72 struct in6_addr *);
73 static struct nd_pfxrouter *pfxrtr_lookup (struct nd_prefix *,
74 struct nd_defrouter *);
75 static void pfxrtr_add (struct nd_prefix *, struct nd_defrouter *);
76 static void pfxrtr_del (struct nd_pfxrouter *);
77 static struct nd_pfxrouter *find_pfxlist_reachable_router
78 (struct nd_prefix *);
79 static void defrouter_addifreq (struct ifnet *);
80 static void nd6_rtmsg (int, struct rtentry *);
82 static void in6_init_address_ltimes (struct nd_prefix *ndpr,
83 struct in6_addrlifetime *lt6);
85 static int rt6_deleteroute (struct radix_node *, void *);
87 extern int nd6_recalc_reachtm_interval;
89 static struct ifnet *nd6_defifp;
90 int nd6_defifindex;
92 int ip6_use_tempaddr = 0;
94 int ip6_desync_factor;
95 u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
96 u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
98 * shorter lifetimes for debugging purposes.
99 int ip6_temp_preferred_lifetime = 800;
100 static int ip6_temp_valid_lifetime = 1800;
102 int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
105 * Receive Router Solicitation Message - just for routers.
106 * Router solicitation/advertisement is mostly managed by userland program
107 * (rtadvd) so here we have no function like nd6_ra_output().
109 * Based on RFC 2461
111 void
112 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
114 struct ifnet *ifp = m->m_pkthdr.rcvif;
115 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
116 struct nd_router_solicit *nd_rs;
117 struct in6_addr saddr6 = ip6->ip6_src;
118 #if 0
119 struct in6_addr daddr6 = ip6->ip6_dst;
120 #endif
121 char *lladdr = NULL;
122 int lladdrlen = 0;
123 #if 0
124 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
125 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
126 struct rtentry *rt = NULL;
127 int is_newentry;
128 #endif
129 union nd_opts ndopts;
131 /* If I'm not a router, ignore it. */
132 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
133 goto freeit;
135 /* Sanity checks */
136 if (ip6->ip6_hlim != 255) {
137 nd6log((LOG_ERR,
138 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
139 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
140 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
141 goto bad;
145 * Don't update the neighbor cache, if src = ::.
146 * This indicates that the src has no IP address assigned yet.
148 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
149 goto freeit;
151 #ifndef PULLDOWN_TEST
152 IP6_EXTHDR_CHECK(m, off, icmp6len,);
153 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
154 #else
155 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
156 if (nd_rs == NULL) {
157 icmp6stat.icp6s_tooshort++;
158 return;
160 #endif
162 icmp6len -= sizeof(*nd_rs);
163 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
164 if (nd6_options(&ndopts) < 0) {
165 nd6log((LOG_INFO,
166 "nd6_rs_input: invalid ND option, ignored\n"));
167 /* nd6_options have incremented stats */
168 goto freeit;
171 if (ndopts.nd_opts_src_lladdr) {
172 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
173 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
176 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
177 nd6log((LOG_INFO,
178 "nd6_rs_input: lladdrlen mismatch for %s "
179 "(if %d, RS packet %d)\n",
180 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
181 goto bad;
184 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
186 freeit:
187 m_freem(m);
188 return;
190 bad:
191 icmp6stat.icp6s_badrs++;
192 m_freem(m);
196 * Receive Router Advertisement Message.
198 * Based on RFC 2461
199 * TODO: on-link bit on prefix information
200 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
202 void
203 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
205 struct ifnet *ifp = m->m_pkthdr.rcvif;
206 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
207 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
208 struct nd_router_advert *nd_ra;
209 struct in6_addr saddr6 = ip6->ip6_src;
210 #if 0
211 struct in6_addr daddr6 = ip6->ip6_dst;
212 int flags; /* = nd_ra->nd_ra_flags_reserved; */
213 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
214 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
215 #endif
216 union nd_opts ndopts;
217 struct nd_defrouter *dr;
220 * We only accept RAs only when
221 * the system-wide variable allows the acceptance, and
222 * per-interface variable allows RAs on the receiving interface.
224 if (ip6_accept_rtadv == 0)
225 goto freeit;
226 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
227 goto freeit;
229 if (ip6->ip6_hlim != 255) {
230 nd6log((LOG_ERR,
231 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
232 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
233 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
234 goto bad;
237 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
238 nd6log((LOG_ERR,
239 "nd6_ra_input: src %s is not link-local\n",
240 ip6_sprintf(&saddr6)));
241 goto bad;
244 #ifndef PULLDOWN_TEST
245 IP6_EXTHDR_CHECK(m, off, icmp6len,);
246 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
247 #else
248 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
249 if (nd_ra == NULL) {
250 icmp6stat.icp6s_tooshort++;
251 return;
253 #endif
255 icmp6len -= sizeof(*nd_ra);
256 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
257 if (nd6_options(&ndopts) < 0) {
258 nd6log((LOG_INFO,
259 "nd6_ra_input: invalid ND option, ignored\n"));
260 /* nd6_options have incremented stats */
261 goto freeit;
265 struct nd_defrouter dr0;
266 u_int32_t advreachable = nd_ra->nd_ra_reachable;
268 dr0.rtaddr = saddr6;
269 dr0.flags = nd_ra->nd_ra_flags_reserved;
270 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
271 dr0.expire = time_second + dr0.rtlifetime;
272 dr0.ifp = ifp;
273 dr0.advint = 0; /* Mobile IPv6 */
274 dr0.advint_expire = 0; /* Mobile IPv6 */
275 dr0.advints_lost = 0; /* Mobile IPv6 */
276 /* unspecified or not? (RFC 2461 6.3.4) */
277 if (advreachable) {
278 advreachable = ntohl(advreachable);
279 if (advreachable <= MAX_REACHABLE_TIME &&
280 ndi->basereachable != advreachable) {
281 ndi->basereachable = advreachable;
282 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
283 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
286 if (nd_ra->nd_ra_retransmit)
287 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
288 if (nd_ra->nd_ra_curhoplimit)
289 ndi->chlim = nd_ra->nd_ra_curhoplimit;
290 dr = defrtrlist_update(&dr0);
294 * prefix
296 if (ndopts.nd_opts_pi) {
297 struct nd_opt_hdr *pt;
298 struct nd_opt_prefix_info *pi = NULL;
299 struct nd_prefix pr;
301 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
302 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
303 pt = (struct nd_opt_hdr *)((caddr_t)pt +
304 (pt->nd_opt_len << 3))) {
305 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
306 continue;
307 pi = (struct nd_opt_prefix_info *)pt;
309 if (pi->nd_opt_pi_len != 4) {
310 nd6log((LOG_INFO,
311 "nd6_ra_input: invalid option "
312 "len %d for prefix information option, "
313 "ignored\n", pi->nd_opt_pi_len));
314 continue;
317 if (128 < pi->nd_opt_pi_prefix_len) {
318 nd6log((LOG_INFO,
319 "nd6_ra_input: invalid prefix "
320 "len %d for prefix information option, "
321 "ignored\n", pi->nd_opt_pi_prefix_len));
322 continue;
325 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
326 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
327 nd6log((LOG_INFO,
328 "nd6_ra_input: invalid prefix "
329 "%s, ignored\n",
330 ip6_sprintf(&pi->nd_opt_pi_prefix)));
331 continue;
334 /* aggregatable unicast address, rfc2374 */
335 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
336 && pi->nd_opt_pi_prefix_len != 64) {
337 nd6log((LOG_INFO,
338 "nd6_ra_input: invalid prefixlen "
339 "%d for rfc2374 prefix %s, ignored\n",
340 pi->nd_opt_pi_prefix_len,
341 ip6_sprintf(&pi->nd_opt_pi_prefix)));
342 continue;
345 bzero(&pr, sizeof(pr));
346 pr.ndpr_prefix.sin6_family = AF_INET6;
347 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
348 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
349 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
351 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
352 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
353 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
354 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
355 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
356 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
357 pr.ndpr_pltime =
358 ntohl(pi->nd_opt_pi_preferred_time);
360 if (in6_init_prefix_ltimes(&pr))
361 continue; /* prefix lifetime init failed */
363 prelist_update(&pr, dr, m);
368 * MTU
370 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
371 u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
373 /* lower bound */
374 if (mtu < IPV6_MMTU) {
375 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
376 "mtu=%d sent from %s, ignoring\n",
377 mtu, ip6_sprintf(&ip6->ip6_src)));
378 goto skip;
381 /* upper bound */
382 if (ndi->maxmtu) {
383 if (mtu <= ndi->maxmtu) {
384 int change = (ndi->linkmtu != mtu);
386 ndi->linkmtu = mtu;
387 if (change) /* in6_maxmtu may change */
388 in6_setmaxmtu();
389 } else {
390 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
391 "mtu=%d sent from %s; "
392 "exceeds maxmtu %d, ignoring\n",
393 mtu, ip6_sprintf(&ip6->ip6_src),
394 ndi->maxmtu));
396 } else {
397 nd6log((LOG_INFO, "nd6_ra_input: mtu option "
398 "mtu=%d sent from %s; maxmtu unknown, "
399 "ignoring\n",
400 mtu, ip6_sprintf(&ip6->ip6_src)));
404 skip:
407 * Source link layer address
410 char *lladdr = NULL;
411 int lladdrlen = 0;
413 if (ndopts.nd_opts_src_lladdr) {
414 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
415 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
418 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
419 nd6log((LOG_INFO,
420 "nd6_ra_input: lladdrlen mismatch for %s "
421 "(if %d, RA packet %d)\n",
422 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
423 goto bad;
426 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
429 * Installing a link-layer address might change the state of the
430 * router's neighbor cache, which might also affect our on-link
431 * detection of adveritsed prefixes.
433 pfxlist_onlink_check();
436 freeit:
437 m_freem(m);
438 return;
440 bad:
441 icmp6stat.icp6s_badra++;
442 m_freem(m);
446 * default router list proccessing sub routines
449 /* tell the change to user processes watching the routing socket. */
450 static void
451 nd6_rtmsg(int cmd, struct rtentry *rt)
453 struct rt_addrinfo info;
455 bzero((caddr_t)&info, sizeof(info));
456 info.rti_info[RTAX_DST] = rt_key(rt);
457 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
458 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
459 if (TAILQ_EMPTY(&rt->rt_ifp->if_addrheads[mycpuid])) {
460 info.rti_info[RTAX_IFP] = NULL;
461 } else {
462 info.rti_info[RTAX_IFP] =
463 (struct sockaddr *)
464 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa;
466 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
468 rt_missmsg(cmd, &info, rt->rt_flags, 0);
471 void
472 defrouter_addreq(struct nd_defrouter *new)
474 struct sockaddr_in6 def, mask, gate;
475 struct rtentry *newrt = NULL;
477 bzero(&def, sizeof(def));
478 bzero(&mask, sizeof(mask));
479 bzero(&gate, sizeof(gate));
481 def.sin6_len = mask.sin6_len = gate.sin6_len
482 = sizeof(struct sockaddr_in6);
483 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
484 gate.sin6_addr = new->rtaddr;
486 crit_enter();
487 rtrequest(RTM_ADD, (struct sockaddr *)&def, (struct sockaddr *)&gate,
488 (struct sockaddr *)&mask, RTF_GATEWAY, &newrt);
489 if (newrt) {
490 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
491 newrt->rt_refcnt--;
493 crit_exit();
494 return;
497 /* Add a route to a given interface as default */
498 void
499 defrouter_addifreq(struct ifnet *ifp)
501 struct sockaddr_in6 def, mask;
502 struct ifaddr *ifa;
503 struct rtentry *newrt = NULL;
504 int error, flags;
506 bzero(&def, sizeof(def));
507 bzero(&mask, sizeof(mask));
509 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
510 def.sin6_family = mask.sin6_family = AF_INET6;
513 * Search for an ifaddr beloging to the specified interface.
514 * XXX: An IPv6 address are required to be assigned on the interface.
516 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
517 nd6log((LOG_ERR, /* better error? */
518 "defrouter_addifreq: failed to find an ifaddr "
519 "to install a route to interface %s\n",
520 if_name(ifp)));
521 return;
524 flags = ifa->ifa_flags;
525 error = rtrequest(RTM_ADD, (struct sockaddr *)&def, ifa->ifa_addr,
526 (struct sockaddr *)&mask, flags, &newrt);
527 if (error != 0) {
528 nd6log((LOG_ERR,
529 "defrouter_addifreq: failed to install a route to "
530 "interface %s (errno = %d)\n",
531 if_name(ifp), error));
533 if (newrt) /* maybe unnecessary, but do it for safety */
534 newrt->rt_refcnt--;
535 } else {
536 if (newrt) {
537 nd6_rtmsg(RTM_ADD, newrt);
538 newrt->rt_refcnt--;
543 struct nd_defrouter *
544 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
546 struct nd_defrouter *dr;
548 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
549 dr = TAILQ_NEXT(dr, dr_entry)) {
550 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
551 return (dr);
554 return (NULL); /* search failed */
557 void
558 defrouter_delreq(struct nd_defrouter *dr, int dofree)
560 struct sockaddr_in6 def, mask, gate;
561 struct rtentry *oldrt = NULL;
563 bzero(&def, sizeof(def));
564 bzero(&mask, sizeof(mask));
565 bzero(&gate, sizeof(gate));
567 def.sin6_len = mask.sin6_len = gate.sin6_len
568 = sizeof(struct sockaddr_in6);
569 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
570 gate.sin6_addr = dr->rtaddr;
572 rtrequest(RTM_DELETE, (struct sockaddr *)&def,
573 (struct sockaddr *)&gate,
574 (struct sockaddr *)&mask,
575 RTF_GATEWAY, &oldrt);
576 if (oldrt) {
577 nd6_rtmsg(RTM_DELETE, oldrt);
578 if (oldrt->rt_refcnt <= 0) {
580 * XXX: borrowed from the RTM_DELETE case of
581 * rtrequest().
583 oldrt->rt_refcnt++;
584 rtfree(oldrt);
588 if (dofree) /* XXX: necessary? */
589 kfree(dr, M_IP6NDP);
592 void
593 defrtrlist_del(struct nd_defrouter *dr)
595 struct nd_defrouter *deldr = NULL;
596 struct nd_prefix *pr;
599 * Flush all the routing table entries that use the router
600 * as a next hop.
602 if (!ip6_forwarding && ip6_accept_rtadv) {
603 /* above is a good condition? */
604 rt6_flush(&dr->rtaddr, dr->ifp);
607 if (dr == TAILQ_FIRST(&nd_defrouter))
608 deldr = dr; /* The router is primary. */
610 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
613 * Also delete all the pointers to the router in each prefix lists.
615 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
616 struct nd_pfxrouter *pfxrtr;
617 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
618 pfxrtr_del(pfxrtr);
620 pfxlist_onlink_check();
623 * If the router is the primary one, choose a new one.
624 * Note that defrouter_select() will remove the current gateway
625 * from the routing table.
627 if (deldr)
628 defrouter_select();
630 kfree(dr, M_IP6NDP);
634 * Default Router Selection according to Section 6.3.6 of RFC 2461:
635 * 1) Routers that are reachable or probably reachable should be
636 * preferred.
637 * 2) When no routers on the list are known to be reachable or
638 * probably reachable, routers SHOULD be selected in a round-robin
639 * fashion.
640 * 3) If the Default Router List is empty, assume that all
641 * destinations are on-link.
643 void
644 defrouter_select(void)
646 struct nd_defrouter *dr, anydr;
647 struct rtentry *rt = NULL;
648 struct llinfo_nd6 *ln = NULL;
650 crit_enter();
653 * Search for a (probably) reachable router from the list.
655 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
656 dr = TAILQ_NEXT(dr, dr_entry)) {
657 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
658 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
659 ND6_IS_LLINFO_PROBREACH(ln)) {
660 /* Got it, and move it to the head */
661 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
662 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
663 break;
667 if ((dr = TAILQ_FIRST(&nd_defrouter))) {
669 * De-install the previous default gateway and install
670 * a new one.
671 * Note that if there is no reachable router in the list,
672 * the head entry will be used anyway.
673 * XXX: do we have to check the current routing table entry?
675 bzero(&anydr, sizeof(anydr));
676 defrouter_delreq(&anydr, 0);
677 defrouter_addreq(dr);
679 else {
681 * The Default Router List is empty, so install the default
682 * route to an inteface.
683 * XXX: The specification does not say this mechanism should
684 * be restricted to hosts, but this would be not useful
685 * (even harmful) for routers.
687 if (!ip6_forwarding) {
689 * De-install the current default route
690 * in advance.
692 bzero(&anydr, sizeof(anydr));
693 defrouter_delreq(&anydr, 0);
694 if (nd6_defifp) {
696 * Install a route to the default interface
697 * as default route.
698 * XXX: we enable this for host only, because
699 * this may override a default route installed
700 * a user process (e.g. routing daemon) in a
701 * router case.
703 defrouter_addifreq(nd6_defifp);
704 } else {
705 nd6log((LOG_INFO, "defrouter_select: "
706 "there's no default router and no default"
707 " interface\n"));
712 crit_exit();
713 return;
716 static struct nd_defrouter *
717 defrtrlist_update(struct nd_defrouter *new)
719 struct nd_defrouter *dr, *n;
721 crit_enter();
723 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
724 /* entry exists */
725 if (new->rtlifetime == 0) {
726 defrtrlist_del(dr);
727 dr = NULL;
728 } else {
729 /* override */
730 dr->flags = new->flags; /* xxx flag check */
731 dr->rtlifetime = new->rtlifetime;
732 dr->expire = new->expire;
734 crit_exit();
735 return (dr);
738 /* entry does not exist */
739 if (new->rtlifetime == 0) {
740 crit_exit();
741 return (NULL);
744 n = (struct nd_defrouter *)kmalloc(sizeof(*n), M_IP6NDP,
745 M_NOWAIT | M_ZERO);
746 if (n == NULL) {
747 crit_exit();
748 return (NULL);
750 *n = *new;
753 * Insert the new router at the end of the Default Router List.
754 * If there is no other router, install it anyway. Otherwise,
755 * just continue to use the current default router.
757 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
758 if (TAILQ_FIRST(&nd_defrouter) == n)
759 defrouter_select();
760 crit_exit();
761 return (n);
764 static struct nd_pfxrouter *
765 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
767 struct nd_pfxrouter *search;
769 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
770 if (search->router == dr)
771 break;
774 return (search);
777 static void
778 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
780 struct nd_pfxrouter *new;
782 new = (struct nd_pfxrouter *)kmalloc(sizeof(*new), M_IP6NDP,
783 M_NOWAIT | M_ZERO);
784 if (new == NULL)
785 return;
786 new->router = dr;
788 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
790 pfxlist_onlink_check();
793 static void
794 pfxrtr_del(struct nd_pfxrouter *pfr)
796 LIST_REMOVE(pfr, pfr_entry);
797 kfree(pfr, M_IP6NDP);
800 struct nd_prefix *
801 nd6_prefix_lookup(struct nd_prefix *pr)
803 struct nd_prefix *search;
805 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
806 if (pr->ndpr_ifp == search->ndpr_ifp &&
807 pr->ndpr_plen == search->ndpr_plen &&
808 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
809 &search->ndpr_prefix.sin6_addr,
810 pr->ndpr_plen)
812 break;
816 return (search);
820 nd6_prelist_add(struct nd_prefix *pr, struct nd_defrouter *dr,
821 struct nd_prefix **newp)
823 struct nd_prefix *new = NULL;
824 int i;
826 new = (struct nd_prefix *)kmalloc(sizeof(*new), M_IP6NDP,
827 M_NOWAIT | M_ZERO);
828 if (new == NULL)
829 return ENOMEM;
830 *new = *pr;
831 if (newp != NULL)
832 *newp = new;
834 /* initilization */
835 LIST_INIT(&new->ndpr_advrtrs);
836 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
837 /* make prefix in the canonical form */
838 for (i = 0; i < 4; i++)
839 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
840 new->ndpr_mask.s6_addr32[i];
842 crit_enter();
843 /* link ndpr_entry to nd_prefix list */
844 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
845 crit_exit();
847 /* ND_OPT_PI_FLAG_ONLINK processing */
848 if (new->ndpr_raf_onlink) {
849 int e;
851 if ((e = nd6_prefix_onlink(new)) != 0) {
852 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
853 "the prefix %s/%d on-link on %s (errno=%d)\n",
854 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
855 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
856 /* proceed anyway. XXX: is it correct? */
860 if (dr) {
861 pfxrtr_add(new, dr);
864 return 0;
867 void
868 prelist_remove(struct nd_prefix *pr)
870 struct nd_pfxrouter *pfr, *next;
871 int e;
873 /* make sure to invalidate the prefix until it is really freed. */
874 pr->ndpr_vltime = 0;
875 pr->ndpr_pltime = 0;
876 #if 0
878 * Though these flags are now meaningless, we'd rather keep the value
879 * not to confuse users when executing "ndp -p".
881 pr->ndpr_raf_onlink = 0;
882 pr->ndpr_raf_auto = 0;
883 #endif
884 if ((pr->ndpr_stateflags & NDPRF_ONLINK) &&
885 (e = nd6_prefix_offlink(pr)) != 0) {
886 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
887 "on %s, errno=%d\n",
888 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
889 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
890 /* what should we do? */
893 if (pr->ndpr_refcnt > 0)
894 return; /* notice here? */
896 crit_enter();
898 /* unlink ndpr_entry from nd_prefix list */
899 LIST_REMOVE(pr, ndpr_entry);
901 /* free list of routers that adversed the prefix */
902 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
903 next = pfr->pfr_next;
905 kfree(pfr, M_IP6NDP);
907 crit_exit();
909 kfree(pr, M_IP6NDP);
911 pfxlist_onlink_check();
915 * Parameters:
916 * dr: may be NULL
919 prelist_update(struct nd_prefix *new, struct nd_defrouter *dr, struct mbuf *m)
921 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
922 struct ifaddr_container *ifac;
923 struct ifnet *ifp = new->ndpr_ifp;
924 struct nd_prefix *pr;
925 int error = 0;
926 int newprefix = 0;
927 int auth;
928 struct in6_addrlifetime lt6_tmp;
930 auth = 0;
931 crit_enter();
932 if (m) {
934 * Authenticity for NA consists authentication for
935 * both IP header and IP datagrams, doesn't it ?
937 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
938 auth = (m->m_flags & M_AUTHIPHDR
939 && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
940 #endif
944 if ((pr = nd6_prefix_lookup(new)) != NULL) {
946 * nd6_prefix_lookup() ensures that pr and new have the same
947 * prefix on a same interface.
951 * Update prefix information. Note that the on-link (L) bit
952 * and the autonomous (A) bit should NOT be changed from 1
953 * to 0.
955 if (new->ndpr_raf_onlink == 1)
956 pr->ndpr_raf_onlink = 1;
957 if (new->ndpr_raf_auto == 1)
958 pr->ndpr_raf_auto = 1;
959 if (new->ndpr_raf_onlink) {
960 pr->ndpr_vltime = new->ndpr_vltime;
961 pr->ndpr_pltime = new->ndpr_pltime;
962 pr->ndpr_preferred = new->ndpr_preferred;
963 pr->ndpr_expire = new->ndpr_expire;
966 if (new->ndpr_raf_onlink &&
967 !(pr->ndpr_stateflags & NDPRF_ONLINK)) {
968 int e;
970 if ((e = nd6_prefix_onlink(pr)) != 0) {
971 nd6log((LOG_ERR,
972 "prelist_update: failed to make "
973 "the prefix %s/%d on-link on %s "
974 "(errno=%d)\n",
975 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
976 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
977 /* proceed anyway. XXX: is it correct? */
981 if (dr && pfxrtr_lookup(pr, dr) == NULL)
982 pfxrtr_add(pr, dr);
983 } else {
984 struct nd_prefix *newpr = NULL;
986 newprefix = 1;
988 if (new->ndpr_vltime == 0)
989 goto end;
990 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
991 goto end;
993 bzero(&new->ndpr_addr, sizeof(struct in6_addr));
995 error = nd6_prelist_add(new, dr, &newpr);
996 if (error != 0 || newpr == NULL) {
997 nd6log((LOG_NOTICE, "prelist_update: "
998 "nd6_prelist_add failed for %s/%d on %s "
999 "errno=%d, returnpr=%p\n",
1000 ip6_sprintf(&new->ndpr_prefix.sin6_addr),
1001 new->ndpr_plen, if_name(new->ndpr_ifp),
1002 error, newpr));
1003 goto end; /* we should just give up in this case. */
1007 * XXX: from the ND point of view, we can ignore a prefix
1008 * with the on-link bit being zero. However, we need a
1009 * prefix structure for references from autoconfigured
1010 * addresses. Thus, we explicitly make suret that the prefix
1011 * itself expires now.
1013 if (newpr->ndpr_raf_onlink == 0) {
1014 newpr->ndpr_vltime = 0;
1015 newpr->ndpr_pltime = 0;
1016 in6_init_prefix_ltimes(newpr);
1019 pr = newpr;
1023 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1024 * Note that pr must be non NULL at this point.
1027 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1028 if (!new->ndpr_raf_auto)
1029 goto afteraddrconf;
1032 * 5.5.3 (b). the link-local prefix should have been ignored in
1033 * nd6_ra_input.
1037 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime.
1038 * This should have been done in nd6_ra_input.
1042 * 5.5.3 (d). If the prefix advertised does not match the prefix of an
1043 * address already in the list, and the Valid Lifetime is not 0,
1044 * form an address. Note that even a manually configured address
1045 * should reject autoconfiguration of a new address.
1047 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1048 struct ifaddr *ifa = ifac->ifa;
1049 struct in6_ifaddr *ifa6;
1050 int ifa_plen;
1051 u_int32_t storedlifetime;
1053 if (ifa->ifa_addr->sa_family != AF_INET6)
1054 continue;
1056 ifa6 = (struct in6_ifaddr *)ifa;
1059 * Spec is not clear here, but I believe we should concentrate
1060 * on unicast (i.e. not anycast) addresses.
1061 * XXX: other ia6_flags? detached or duplicated?
1063 if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
1064 continue;
1066 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL);
1067 if (ifa_plen != new->ndpr_plen ||
1068 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr,
1069 &new->ndpr_prefix.sin6_addr,
1070 ifa_plen))
1071 continue;
1073 if (ia6_match == NULL) /* remember the first one */
1074 ia6_match = ifa6;
1076 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1077 continue;
1080 * An already autoconfigured address matched. Now that we
1081 * are sure there is at least one matched address, we can
1082 * proceed to 5.5.3. (e): update the lifetimes according to the
1083 * "two hours" rule and the privacy extension.
1085 #define TWOHOUR (120*60)
1086 lt6_tmp = ifa6->ia6_lifetime;
1088 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1089 storedlifetime = ND6_INFINITE_LIFETIME;
1090 else if (IFA6_IS_INVALID(ifa6))
1091 storedlifetime = 0;
1092 else
1093 storedlifetime = lt6_tmp.ia6t_expire - time_second;
1095 /* when not updating, keep the current stored lifetime. */
1096 lt6_tmp.ia6t_vltime = storedlifetime;
1098 if (TWOHOUR < new->ndpr_vltime ||
1099 storedlifetime < new->ndpr_vltime) {
1100 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1101 } else if (storedlifetime <= TWOHOUR
1102 #if 0
1104 * This condition is logically redundant, so we just
1105 * omit it.
1106 * See IPng 6712, 6717, and 6721.
1108 && new->ndpr_vltime <= storedlifetime
1109 #endif
1111 if (auth) {
1112 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1114 } else {
1116 * new->ndpr_vltime <= TWOHOUR &&
1117 * TWOHOUR < storedlifetime
1119 lt6_tmp.ia6t_vltime = TWOHOUR;
1122 /* The 2 hour rule is not imposed for preferred lifetime. */
1123 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1125 in6_init_address_ltimes(pr, &lt6_tmp);
1128 * When adjusting the lifetimes of an existing temporary
1129 * address, only lower the lifetimes.
1130 * RFC 3041 3.3. (1).
1131 * XXX: how should we modify ia6t_[pv]ltime?
1133 if (ifa6->ia6_flags & IN6_IFF_TEMPORARY) {
1134 if (lt6_tmp.ia6t_expire == 0 || /* no expire */
1135 lt6_tmp.ia6t_expire >
1136 ifa6->ia6_lifetime.ia6t_expire) {
1137 lt6_tmp.ia6t_expire =
1138 ifa6->ia6_lifetime.ia6t_expire;
1140 if (lt6_tmp.ia6t_preferred == 0 || /* no expire */
1141 lt6_tmp.ia6t_preferred >
1142 ifa6->ia6_lifetime.ia6t_preferred) {
1143 lt6_tmp.ia6t_preferred =
1144 ifa6->ia6_lifetime.ia6t_preferred;
1148 ifa6->ia6_lifetime = lt6_tmp;
1150 if (ia6_match == NULL && new->ndpr_vltime) {
1152 * No address matched and the valid lifetime is non-zero.
1153 * Create a new address.
1155 if ((ia6 = in6_ifadd(new, NULL)) != NULL) {
1157 * note that we should use pr (not new) for reference.
1159 pr->ndpr_refcnt++;
1160 ia6->ia6_ndpr = pr;
1162 #if 0
1163 /* XXXYYY Don't do this, according to Jinmei. */
1164 pr->ndpr_addr = new->ndpr_addr;
1165 #endif
1168 * RFC 3041 3.3 (2).
1169 * When a new public address is created as described
1170 * in RFC2462, also create a new temporary address.
1172 * RFC 3041 3.5.
1173 * When an interface connects to a new link, a new
1174 * randomized interface identifier should be generated
1175 * immediately together with a new set of temporary
1176 * addresses. Thus, we specifiy 1 as the 2nd arg of
1177 * in6_tmpifadd().
1179 if (ip6_use_tempaddr) {
1180 int e;
1181 if ((e = in6_tmpifadd(ia6, 1)) != 0) {
1182 nd6log((LOG_NOTICE, "prelist_update: "
1183 "failed to create a temporary "
1184 "address, errno=%d\n",
1185 e));
1190 * A newly added address might affect the status
1191 * of other addresses, so we check and update it.
1192 * XXX: what if address duplication happens?
1194 pfxlist_onlink_check();
1195 } else {
1196 /* just set an error. do not bark here. */
1197 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1201 afteraddrconf:
1203 end:
1204 crit_exit();
1205 return error;
1209 * A supplement function used in the on-link detection below;
1210 * detect if a given prefix has a (probably) reachable advertising router.
1211 * XXX: lengthy function name...
1213 static struct nd_pfxrouter *
1214 find_pfxlist_reachable_router(struct nd_prefix *pr)
1216 struct nd_pfxrouter *pfxrtr;
1217 struct rtentry *rt;
1218 struct llinfo_nd6 *ln;
1220 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1221 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1222 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1223 pfxrtr->router->ifp)) &&
1224 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1225 ND6_IS_LLINFO_PROBREACH(ln))
1226 break; /* found */
1229 return (pfxrtr);
1234 * Check if each prefix in the prefix list has at least one available router
1235 * that advertised the prefix (a router is "available" if its neighbor cache
1236 * entry is reachable or probably reachable).
1237 * If the check fails, the prefix may be off-link, because, for example,
1238 * we have moved from the network but the lifetime of the prefix has not
1239 * expired yet. So we should not use the prefix if there is another prefix
1240 * that has an available router.
1241 * But, if there is no prefix that has an available router, we still regards
1242 * all the prefixes as on-link. This is because we can't tell if all the
1243 * routers are simply dead or if we really moved from the network and there
1244 * is no router around us.
1246 void
1247 pfxlist_onlink_check(void)
1249 struct nd_prefix *pr;
1250 struct in6_ifaddr *ifa;
1253 * Check if there is a prefix that has a reachable advertising
1254 * router.
1256 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1257 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1258 break;
1261 if (pr) {
1263 * There is at least one prefix that has a reachable router.
1264 * Detach prefixes which have no reachable advertising
1265 * router, and attach other prefixes.
1267 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1268 /* XXX: a link-local prefix should never be detached */
1269 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1270 continue;
1273 * we aren't interested in prefixes without the L bit
1274 * set.
1276 if (pr->ndpr_raf_onlink == 0)
1277 continue;
1279 if (!(pr->ndpr_stateflags & NDPRF_DETACHED) &&
1280 find_pfxlist_reachable_router(pr) == NULL)
1281 pr->ndpr_stateflags |= NDPRF_DETACHED;
1282 if ((pr->ndpr_stateflags & NDPRF_DETACHED) &&
1283 find_pfxlist_reachable_router(pr) != 0)
1284 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1286 } else {
1287 /* there is no prefix that has a reachable router */
1288 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1289 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1290 continue;
1292 if (pr->ndpr_raf_onlink == 0)
1293 continue;
1295 if (pr->ndpr_stateflags & NDPRF_DETACHED)
1296 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1301 * Remove each interface route associated with a (just) detached
1302 * prefix, and reinstall the interface route for a (just) attached
1303 * prefix. Note that all attempt of reinstallation does not
1304 * necessarily success, when a same prefix is shared among multiple
1305 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1306 * so we don't have to care about them.
1308 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1309 int e;
1311 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1312 continue;
1314 if (pr->ndpr_raf_onlink == 0)
1315 continue;
1317 if ((pr->ndpr_stateflags & NDPRF_DETACHED) &&
1318 (pr->ndpr_stateflags & NDPRF_ONLINK)) {
1319 if ((e = nd6_prefix_offlink(pr)) != 0) {
1320 nd6log((LOG_ERR,
1321 "pfxlist_onlink_check: failed to "
1322 "make %s/%d offlink, errno=%d\n",
1323 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1324 pr->ndpr_plen, e));
1327 if (!(pr->ndpr_stateflags & NDPRF_DETACHED) &&
1328 !(pr->ndpr_stateflags & NDPRF_ONLINK) &&
1329 pr->ndpr_raf_onlink) {
1330 if ((e = nd6_prefix_onlink(pr)) != 0) {
1331 nd6log((LOG_ERR,
1332 "pfxlist_onlink_check: failed to "
1333 "make %s/%d offlink, errno=%d\n",
1334 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1335 pr->ndpr_plen, e));
1341 * Changes on the prefix status might affect address status as well.
1342 * Make sure that all addresses derived from an attached prefix are
1343 * attached, and that all addresses derived from a detached prefix are
1344 * detached. Note, however, that a manually configured address should
1345 * always be attached.
1346 * The precise detection logic is same as the one for prefixes.
1348 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1349 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1350 continue;
1352 if (ifa->ia6_ndpr == NULL) {
1354 * This can happen when we first configure the address
1355 * (i.e. the address exists, but the prefix does not).
1356 * XXX: complicated relationships...
1358 continue;
1361 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1362 break;
1364 if (ifa) {
1365 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1366 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1367 continue;
1369 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1370 continue;
1372 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1373 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1374 else
1375 ifa->ia6_flags |= IN6_IFF_DETACHED;
1378 else {
1379 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1380 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1381 continue;
1383 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1389 nd6_prefix_onlink(struct nd_prefix *pr)
1391 struct ifaddr *ifa;
1392 struct ifnet *ifp = pr->ndpr_ifp;
1393 struct sockaddr_in6 mask6;
1394 struct nd_prefix *opr;
1395 u_long rtflags;
1396 int error = 0;
1397 struct rtentry *rt = NULL;
1399 /* sanity check */
1400 if (pr->ndpr_stateflags & NDPRF_ONLINK) {
1401 nd6log((LOG_ERR,
1402 "nd6_prefix_onlink: %s/%d is already on-link\n",
1403 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
1404 return (EEXIST));
1408 * Add the interface route associated with the prefix. Before
1409 * installing the route, check if there's the same prefix on another
1410 * interface, and the prefix has already installed the interface route.
1411 * Although such a configuration is expected to be rare, we explicitly
1412 * allow it.
1414 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1415 if (opr == pr)
1416 continue;
1418 if (!(opr->ndpr_stateflags & NDPRF_ONLINK))
1419 continue;
1421 if (opr->ndpr_plen == pr->ndpr_plen &&
1422 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1423 &opr->ndpr_prefix.sin6_addr,
1424 pr->ndpr_plen))
1425 return (0);
1429 * We prefer link-local addresses as the associated interface address.
1431 /* search for a link-local addr */
1432 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1433 IN6_IFF_NOTREADY|
1434 IN6_IFF_ANYCAST);
1435 if (ifa == NULL) {
1436 struct ifaddr_container *ifac;
1438 /* XXX: freebsd does not have ifa_ifwithaf */
1439 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1440 if (ifac->ifa->ifa_addr->sa_family == AF_INET6) {
1441 ifa = ifac->ifa;
1442 break;
1445 /* should we care about ia6_flags? */
1447 if (ifa == NULL) {
1449 * This can still happen, when, for example, we receive an RA
1450 * containing a prefix with the L bit set and the A bit clear,
1451 * after removing all IPv6 addresses on the receiving
1452 * interface. This should, of course, be rare though.
1454 nd6log((LOG_NOTICE,
1455 "nd6_prefix_onlink: failed to find any ifaddr"
1456 " to add route for a prefix(%s/%d) on %s\n",
1457 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1458 pr->ndpr_plen, if_name(ifp)));
1459 return (0);
1463 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1464 * ifa->ifa_rtrequest = nd6_rtrequest;
1466 bzero(&mask6, sizeof(mask6));
1467 mask6.sin6_len = sizeof(mask6);
1468 mask6.sin6_addr = pr->ndpr_mask;
1469 rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP;
1470 if (nd6_need_cache(ifp)) {
1471 /* explicitly set in case ifa_flags does not set the flag. */
1472 rtflags |= RTF_CLONING;
1473 } else {
1475 * explicitly clear the cloning bit in case ifa_flags sets it.
1477 rtflags &= ~RTF_CLONING;
1479 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1480 ifa->ifa_addr, (struct sockaddr *)&mask6,
1481 rtflags, &rt);
1482 if (error == 0) {
1483 if (rt != NULL) /* this should be non NULL, though */
1484 nd6_rtmsg(RTM_ADD, rt);
1485 pr->ndpr_stateflags |= NDPRF_ONLINK;
1487 else {
1488 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1489 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1490 "errno = %d\n",
1491 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1492 pr->ndpr_plen, if_name(ifp),
1493 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1494 ip6_sprintf(&mask6.sin6_addr), rtflags, error));
1497 if (rt != NULL)
1498 rt->rt_refcnt--;
1500 return (error);
1504 nd6_prefix_offlink(struct nd_prefix *pr)
1506 int error = 0;
1507 struct ifnet *ifp = pr->ndpr_ifp;
1508 struct nd_prefix *opr;
1509 struct sockaddr_in6 sa6, mask6;
1510 struct rtentry *rt = NULL;
1512 /* sanity check */
1513 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1514 nd6log((LOG_ERR,
1515 "nd6_prefix_offlink: %s/%d is already off-link\n",
1516 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1517 return (EEXIST);
1520 bzero(&sa6, sizeof(sa6));
1521 sa6.sin6_family = AF_INET6;
1522 sa6.sin6_len = sizeof(sa6);
1523 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1524 sizeof(struct in6_addr));
1525 bzero(&mask6, sizeof(mask6));
1526 mask6.sin6_family = AF_INET6;
1527 mask6.sin6_len = sizeof(sa6);
1528 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1529 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1530 (struct sockaddr *)&mask6, 0, &rt);
1531 if (error == 0) {
1532 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1534 /* report the route deletion to the routing socket. */
1535 if (rt != NULL)
1536 nd6_rtmsg(RTM_DELETE, rt);
1539 * There might be the same prefix on another interface,
1540 * the prefix which could not be on-link just because we have
1541 * the interface route (see comments in nd6_prefix_onlink).
1542 * If there's one, try to make the prefix on-link on the
1543 * interface.
1545 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1546 if (opr == pr)
1547 continue;
1549 if (opr->ndpr_stateflags & NDPRF_ONLINK)
1550 continue;
1553 * KAME specific: detached prefixes should not be
1554 * on-link.
1556 if (opr->ndpr_stateflags & NDPRF_DETACHED)
1557 continue;
1559 if (opr->ndpr_plen == pr->ndpr_plen &&
1560 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1561 &opr->ndpr_prefix.sin6_addr,
1562 pr->ndpr_plen)) {
1563 int e;
1565 if ((e = nd6_prefix_onlink(opr)) != 0) {
1566 nd6log((LOG_ERR,
1567 "nd6_prefix_offlink: failed to "
1568 "recover a prefix %s/%d from %s "
1569 "to %s (errno = %d)\n",
1570 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1571 opr->ndpr_plen, if_name(ifp),
1572 if_name(opr->ndpr_ifp), e));
1577 else {
1578 /* XXX: can we still set the NDPRF_ONLINK flag? */
1579 nd6log((LOG_ERR,
1580 "nd6_prefix_offlink: failed to delete route: "
1581 "%s/%d on %s (errno = %d)\n",
1582 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
1583 error));
1586 if (rt != NULL) {
1587 if (rt->rt_refcnt <= 0) {
1588 /* XXX: we should free the entry ourselves. */
1589 rt->rt_refcnt++;
1590 rtfree(rt);
1594 return (error);
1598 * Parameters:
1599 * ifid: Mobile IPv6 addition
1601 static struct in6_ifaddr *
1602 in6_ifadd(struct nd_prefix *pr, struct in6_addr *ifid)
1604 struct ifnet *ifp = pr->ndpr_ifp;
1605 struct ifaddr *ifa;
1606 struct in6_aliasreq ifra;
1607 struct in6_ifaddr *ia, *ib;
1608 int error, plen0;
1609 struct in6_addr mask;
1610 int prefixlen = pr->ndpr_plen;
1612 in6_len2mask(&mask, prefixlen);
1615 * find a link-local address (will be interface ID).
1616 * Is it really mandatory? Theoretically, a global or a site-local
1617 * address can be configured without a link-local address, if we
1618 * have a unique interface identifier...
1620 * it is not mandatory to have a link-local address, we can generate
1621 * interface identifier on the fly. we do this because:
1622 * (1) it should be the easiest way to find interface identifier.
1623 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1624 * for multiple addresses on a single interface, and possible shortcut
1625 * of DAD. we omitted DAD for this reason in the past.
1626 * (3) a user can prevent autoconfiguration of global address
1627 * by removing link-local address by hand (this is partly because we
1628 * don't have other way to control the use of IPv6 on a interface.
1629 * this has been our design choice - cf. NRL's "ifconfig auto").
1630 * (4) it is easier to manage when an interface has addresses
1631 * with the same interface identifier, than to have multiple addresses
1632 * with different interface identifiers.
1634 * Mobile IPv6 addition: allow for caller to specify a wished interface
1635 * ID. This is to not break connections when moving addresses between
1636 * interfaces.
1638 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */
1639 if (ifa)
1640 ib = (struct in6_ifaddr *)ifa;
1641 else
1642 return NULL;
1644 #if 0 /* don't care link local addr state, and always do DAD */
1645 /* if link-local address is not eligible, do not autoconfigure. */
1646 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1647 kprintf("in6_ifadd: link-local address not ready\n");
1648 return NULL;
1650 #endif
1652 /* prefixlen + ifidlen must be equal to 128 */
1653 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1654 if (prefixlen != plen0) {
1655 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1656 "(prefix=%d ifid=%d)\n",
1657 if_name(ifp), prefixlen, 128 - plen0));
1658 return NULL;
1661 /* make ifaddr */
1663 bzero(&ifra, sizeof(ifra));
1665 * in6_update_ifa() does not use ifra_name, but we accurately set it
1666 * for safety.
1668 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1669 ifra.ifra_addr.sin6_family = AF_INET6;
1670 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1671 /* prefix */
1672 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
1673 sizeof(ifra.ifra_addr.sin6_addr));
1674 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1675 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1676 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1677 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1679 /* interface ID */
1680 if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid))
1681 ifid = &ib->ia_addr.sin6_addr;
1682 ifra.ifra_addr.sin6_addr.s6_addr32[0]
1683 |= (ifid->s6_addr32[0] & ~mask.s6_addr32[0]);
1684 ifra.ifra_addr.sin6_addr.s6_addr32[1]
1685 |= (ifid->s6_addr32[1] & ~mask.s6_addr32[1]);
1686 ifra.ifra_addr.sin6_addr.s6_addr32[2]
1687 |= (ifid->s6_addr32[2] & ~mask.s6_addr32[2]);
1688 ifra.ifra_addr.sin6_addr.s6_addr32[3]
1689 |= (ifid->s6_addr32[3] & ~mask.s6_addr32[3]);
1691 /* new prefix mask. */
1692 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1693 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1694 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1695 sizeof(ifra.ifra_prefixmask.sin6_addr));
1698 * lifetime.
1699 * XXX: in6_init_address_ltimes would override these values later.
1700 * We should reconsider this logic.
1702 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1703 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1705 /* XXX: scope zone ID? */
1707 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1709 * temporarily set the nopfx flag to avoid conflict.
1710 * XXX: we should reconsider the entire mechanism about prefix
1711 * manipulation.
1713 ifra.ifra_flags |= IN6_IFF_NOPFX;
1716 * keep the new address, regardless of the result of in6_update_ifa.
1717 * XXX: this address is now meaningless.
1718 * We should reconsider its role.
1720 pr->ndpr_addr = ifra.ifra_addr.sin6_addr;
1722 /* allocate ifaddr structure, link into chain, etc. */
1723 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
1724 nd6log((LOG_ERR,
1725 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1726 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
1727 error));
1728 return (NULL); /* ifaddr must not have been allocated. */
1731 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1733 return (ia); /* this must NOT be NULL. */
1737 * Parameters:
1738 * ia0: corresponding public address
1741 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen)
1743 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1744 struct in6_ifaddr *newia;
1745 struct in6_aliasreq ifra;
1746 int i, error;
1747 int trylimit = 3; /* XXX: adhoc value */
1748 u_int32_t randid[2];
1749 time_t vltime0, pltime0;
1751 bzero(&ifra, sizeof(ifra));
1752 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1753 ifra.ifra_addr = ia0->ia_addr;
1754 /* copy prefix mask */
1755 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1756 /* clear the old IFID */
1757 for (i = 0; i < 4; i++) {
1758 ifra.ifra_addr.sin6_addr.s6_addr32[i]
1759 &= ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1762 again:
1763 in6_get_tmpifid(ifp, (u_int8_t *)randid,
1764 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8],
1765 forcegen);
1766 ifra.ifra_addr.sin6_addr.s6_addr32[2]
1767 |= (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1768 ifra.ifra_addr.sin6_addr.s6_addr32[3]
1769 |= (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1772 * If by chance the new temporary address is the same as an address
1773 * already assigned to the interface, generate a new randomized
1774 * interface identifier and repeat this step.
1775 * RFC 3041 3.3 (4).
1777 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
1778 if (trylimit-- == 0) {
1779 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find "
1780 "a unique random IFID\n"));
1781 return (EEXIST);
1783 forcegen = 1;
1784 goto again;
1788 * The Valid Lifetime is the lower of the Valid Lifetime of the
1789 * public address or TEMP_VALID_LIFETIME.
1790 * The Preferred Lifetime is the lower of the Preferred Lifetime
1791 * of the public address or TEMP_PREFERRED_LIFETIME -
1792 * DESYNC_FACTOR.
1794 if (ia0->ia6_lifetime.ia6t_expire != 0) {
1795 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1796 (ia0->ia6_lifetime.ia6t_expire - time_second);
1797 if (vltime0 > ip6_temp_valid_lifetime)
1798 vltime0 = ip6_temp_valid_lifetime;
1799 } else
1800 vltime0 = ip6_temp_valid_lifetime;
1801 if (ia0->ia6_lifetime.ia6t_preferred != 0) {
1802 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
1803 (ia0->ia6_lifetime.ia6t_preferred - time_second);
1804 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor) {
1805 pltime0 = ip6_temp_preferred_lifetime -
1806 ip6_desync_factor;
1808 } else
1809 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
1810 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1811 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1814 * A temporary address is created only if this calculated Preferred
1815 * Lifetime is greater than REGEN_ADVANCE time units.
1817 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
1818 return (0);
1820 /* XXX: scope zone ID? */
1822 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
1824 /* allocate ifaddr structure, link into chain, etc. */
1825 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0)
1826 return (error);
1828 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1829 if (newia == NULL) { /* XXX: can it happen? */
1830 nd6log((LOG_ERR,
1831 "in6_tmpifadd: ifa update succeeded, but we got "
1832 "no ifaddr\n"));
1833 return (EINVAL); /* XXX */
1835 newia->ia6_ndpr = ia0->ia6_ndpr;
1836 newia->ia6_ndpr->ndpr_refcnt++;
1838 return (0);
1842 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1844 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
1845 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1846 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1847 "(%d) is greater than valid lifetime(%d)\n",
1848 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
1849 return (EINVAL);
1851 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1852 ndpr->ndpr_preferred = 0;
1853 else
1854 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
1855 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1856 ndpr->ndpr_expire = 0;
1857 else
1858 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
1860 return 0;
1863 static void
1864 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1866 /* init ia6t_expire */
1867 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1868 lt6->ia6t_expire = 0;
1869 else {
1870 lt6->ia6t_expire = time_second;
1871 lt6->ia6t_expire += lt6->ia6t_vltime;
1874 /* init ia6t_preferred */
1875 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1876 lt6->ia6t_preferred = 0;
1877 else {
1878 lt6->ia6t_preferred = time_second;
1879 lt6->ia6t_preferred += lt6->ia6t_pltime;
1884 * Delete all the routing table entries that use the specified gateway.
1885 * XXX: this function causes search through all entries of routing table, so
1886 * it shouldn't be called when acting as a router.
1888 void
1889 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
1891 struct radix_node_head *rnh = rt_tables[mycpuid][AF_INET6];
1893 crit_enter();
1895 /* We'll care only link-local addresses */
1896 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1897 crit_exit();
1898 return;
1900 /* XXX: hack for KAME's link-local address kludge */
1901 gateway->s6_addr16[1] = htons(ifp->if_index);
1903 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1904 crit_exit();
1907 static int
1908 rt6_deleteroute(struct radix_node *rn, void *arg)
1910 #define SIN6(s) ((struct sockaddr_in6 *)s)
1911 struct rtentry *rt = (struct rtentry *)rn;
1912 struct in6_addr *gate = (struct in6_addr *)arg;
1914 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1915 return (0);
1917 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1918 return (0);
1921 * Do not delete a static route.
1922 * XXX: this seems to be a bit ad-hoc. Should we consider the
1923 * 'cloned' bit instead?
1925 if (rt->rt_flags & RTF_STATIC)
1926 return (0);
1929 * We delete only host route. This means, in particular, we don't
1930 * delete default route.
1932 if (!(rt->rt_flags & RTF_HOST))
1933 return (0);
1935 return (rtrequest(RTM_DELETE, rt_key(rt),
1936 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
1937 #undef SIN6
1941 nd6_setdefaultiface(int ifindex)
1943 int error = 0;
1945 if (ifindex < 0 || if_index < ifindex)
1946 return (EINVAL);
1948 if (nd6_defifindex != ifindex) {
1949 nd6_defifindex = ifindex;
1950 if (nd6_defifindex > 0)
1951 nd6_defifp = ifindex2ifnet[nd6_defifindex];
1952 else
1953 nd6_defifp = NULL;
1956 * If the Default Router List is empty, install a route
1957 * to the specified interface as default or remove the default
1958 * route when the default interface becomes canceled.
1959 * The check for the queue is actually redundant, but
1960 * we do this here to avoid re-install the default route
1961 * if the list is NOT empty.
1963 if (TAILQ_FIRST(&nd_defrouter) == NULL)
1964 defrouter_select();
1967 * Our current implementation assumes one-to-one maping between
1968 * interfaces and links, so it would be natural to use the
1969 * default interface as the default link.
1971 scope6_setdefault(nd6_defifp);
1974 return (error);