nd6_rtr.c revision 1.39 1 /* $NetBSD: nd6_rtr.c,v 1.39 2003/05/15 14:57:58 itojun Exp $ */
2 /* $KAME: nd6_rtr.c,v 1.95 2001/02/07 08:09:47 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6_rtr.c,v 1.39 2003/05/15 14:57:58 itojun Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
47
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/if_dl.h>
51 #include <net/route.h>
52 #include <net/radix.h>
53
54 #include <netinet/in.h>
55 #include <netinet6/in6_var.h>
56 #include <netinet/ip6.h>
57 #include <netinet6/ip6_var.h>
58 #include <netinet6/nd6.h>
59 #include <netinet/icmp6.h>
60
61 #include <net/net_osdep.h>
62
63 #define SDL(s) ((struct sockaddr_dl *)s)
64
65 static int rtpref __P((struct nd_defrouter *));
66 static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *));
67 static struct in6_ifaddr *in6_ifadd __P((struct nd_prefix *));
68 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
69 struct nd_defrouter *));
70 static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *));
71 static void pfxrtr_del __P((struct nd_pfxrouter *));
72 static struct nd_pfxrouter *find_pfxlist_reachable_router
73 __P((struct nd_prefix *));
74 static void defrouter_delreq __P((struct nd_defrouter *));
75 static void defrouter_addifreq __P((struct ifnet *));
76 static void defrouter_delifreq __P((void));
77 static void nd6_rtmsg __P((int, struct rtentry *));
78
79 static void in6_init_address_ltimes __P((struct nd_prefix *ndpr,
80 struct in6_addrlifetime *lt6));
81
82 static int rt6_deleteroute __P((struct radix_node *, void *));
83
84 extern int nd6_recalc_reachtm_interval;
85
86 static struct ifnet *nd6_defifp;
87 int nd6_defifindex;
88 static struct ifaddr *nd6_defif_installed = NULL;
89
90 /*
91 * Receive Router Solicitation Message - just for routers.
92 * Router solicitation/advertisement is mostly managed by userland program
93 * (rtadvd) so here we have no function like nd6_ra_output().
94 *
95 * Based on RFC 2461
96 */
97 void
98 nd6_rs_input(m, off, icmp6len)
99 struct mbuf *m;
100 int off, icmp6len;
101 {
102 struct ifnet *ifp = m->m_pkthdr.rcvif;
103 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
104 struct nd_router_solicit *nd_rs;
105 struct in6_addr saddr6 = ip6->ip6_src;
106 #if 0
107 struct in6_addr daddr6 = ip6->ip6_dst;
108 #endif
109 char *lladdr = NULL;
110 int lladdrlen = 0;
111 #if 0
112 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
113 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
114 struct rtentry *rt = NULL;
115 int is_newentry;
116 #endif
117 union nd_opts ndopts;
118
119 /* If I'm not a router, ignore it. */
120 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
121 goto freeit;
122
123 /* Sanity checks */
124 if (ip6->ip6_hlim != 255) {
125 nd6log((LOG_ERR,
126 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
127 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
128 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
129 goto bad;
130 }
131
132 /*
133 * Don't update the neighbor cache, if src = ::.
134 * This indicates that the src has no IP address assigned yet.
135 */
136 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
137 goto freeit;
138
139 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
140 if (nd_rs == NULL) {
141 icmp6stat.icp6s_tooshort++;
142 return;
143 }
144
145 icmp6len -= sizeof(*nd_rs);
146 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
147 if (nd6_options(&ndopts) < 0) {
148 nd6log((LOG_INFO,
149 "nd6_rs_input: invalid ND option, ignored\n"));
150 /* nd6_options have incremented stats */
151 goto freeit;
152 }
153
154 if (ndopts.nd_opts_src_lladdr) {
155 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
156 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
157 }
158
159 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
160 nd6log((LOG_INFO,
161 "nd6_rs_input: lladdrlen mismatch for %s "
162 "(if %d, RS packet %d)\n",
163 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
164 goto bad;
165 }
166
167 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
168
169 freeit:
170 m_freem(m);
171 return;
172
173 bad:
174 icmp6stat.icp6s_badrs++;
175 m_freem(m);
176 }
177
178 /*
179 * Receive Router Advertisement Message.
180 *
181 * Based on RFC 2461
182 * TODO: on-link bit on prefix information
183 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
184 */
185 void
186 nd6_ra_input(m, off, icmp6len)
187 struct mbuf *m;
188 int off, icmp6len;
189 {
190 struct ifnet *ifp = m->m_pkthdr.rcvif;
191 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
192 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
193 struct nd_router_advert *nd_ra;
194 struct in6_addr saddr6 = ip6->ip6_src;
195 #if 0
196 struct in6_addr daddr6 = ip6->ip6_dst;
197 int flags; /* = nd_ra->nd_ra_flags_reserved; */
198 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
199 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
200 #endif
201 union nd_opts ndopts;
202 struct nd_defrouter *dr;
203
204 /*
205 * We only accept RAs only when
206 * the system-wide variable allows the acceptance, and
207 * per-interface variable allows RAs on the receiving interface.
208 */
209 if (ip6_accept_rtadv == 0)
210 goto freeit;
211 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
212 goto freeit;
213
214 if (ip6->ip6_hlim != 255) {
215 nd6log((LOG_ERR,
216 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
217 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
218 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
219 goto bad;
220 }
221
222 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
223 nd6log((LOG_ERR,
224 "nd6_ra_input: src %s is not link-local\n",
225 ip6_sprintf(&saddr6)));
226 goto bad;
227 }
228
229 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
230 if (nd_ra == NULL) {
231 icmp6stat.icp6s_tooshort++;
232 return;
233 }
234
235 icmp6len -= sizeof(*nd_ra);
236 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
237 if (nd6_options(&ndopts) < 0) {
238 nd6log((LOG_INFO,
239 "nd6_ra_input: invalid ND option, ignored\n"));
240 /* nd6_options have incremented stats */
241 goto freeit;
242 }
243
244 {
245 struct nd_defrouter dr0;
246 u_int32_t advreachable = nd_ra->nd_ra_reachable;
247 long time_second = time.tv_sec;
248
249 Bzero(&dr0, sizeof(dr0));
250 dr0.rtaddr = saddr6;
251 dr0.flags = nd_ra->nd_ra_flags_reserved;
252 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
253 dr0.expire = time_second + dr0.rtlifetime;
254 dr0.ifp = ifp;
255 /* unspecified or not? (RFC 2461 6.3.4) */
256 if (advreachable) {
257 NTOHL(advreachable);
258 if (advreachable <= MAX_REACHABLE_TIME &&
259 ndi->basereachable != advreachable) {
260 ndi->basereachable = advreachable;
261 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
262 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
263 }
264 }
265 if (nd_ra->nd_ra_retransmit)
266 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
267 if (nd_ra->nd_ra_curhoplimit)
268 ndi->chlim = nd_ra->nd_ra_curhoplimit;
269 dr = defrtrlist_update(&dr0);
270 }
271
272 /*
273 * prefix
274 */
275 if (ndopts.nd_opts_pi) {
276 struct nd_opt_hdr *pt;
277 struct nd_opt_prefix_info *pi = NULL;
278 struct nd_prefix pr;
279 long time_second = time.tv_sec;
280
281 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
282 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
283 pt = (struct nd_opt_hdr *)((caddr_t)pt +
284 (pt->nd_opt_len << 3))) {
285 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
286 continue;
287 pi = (struct nd_opt_prefix_info *)pt;
288
289 if (pi->nd_opt_pi_len != 4) {
290 nd6log((LOG_INFO,
291 "nd6_ra_input: invalid option "
292 "len %d for prefix information option, "
293 "ignored\n", pi->nd_opt_pi_len));
294 continue;
295 }
296
297 if (128 < pi->nd_opt_pi_prefix_len) {
298 nd6log((LOG_INFO,
299 "nd6_ra_input: invalid prefix "
300 "len %d for prefix information option, "
301 "ignored\n", pi->nd_opt_pi_prefix_len));
302 continue;
303 }
304
305 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
306 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
307 nd6log((LOG_INFO,
308 "nd6_ra_input: invalid prefix "
309 "%s, ignored\n",
310 ip6_sprintf(&pi->nd_opt_pi_prefix)));
311 continue;
312 }
313
314 /* aggregatable unicast address, rfc2374 */
315 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
316 && pi->nd_opt_pi_prefix_len != 64) {
317 nd6log((LOG_INFO,
318 "nd6_ra_input: invalid prefixlen "
319 "%d for rfc2374 prefix %s, ignored\n",
320 pi->nd_opt_pi_prefix_len,
321 ip6_sprintf(&pi->nd_opt_pi_prefix)));
322 continue;
323 }
324
325 bzero(&pr, sizeof(pr));
326 pr.ndpr_prefix.sin6_family = AF_INET6;
327 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
328 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
329 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
330
331 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
332 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
333 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
334 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
335 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
336 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
337 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
338 pr.ndpr_lastupdate = time_second;
339
340 if (in6_init_prefix_ltimes(&pr))
341 continue; /* prefix lifetime init failed */
342
343 (void)prelist_update(&pr, dr, m);
344 }
345 }
346
347 /*
348 * MTU
349 */
350 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
351 u_long mtu;
352 u_long maxmtu;
353
354 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
355
356 /* lower bound */
357 if (mtu < IPV6_MMTU) {
358 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
359 "mtu=%lu sent from %s, ignoring\n",
360 mtu, ip6_sprintf(&ip6->ip6_src)));
361 goto skip;
362 }
363
364 /* upper bound */
365 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
366 ? ndi->maxmtu : ifp->if_mtu;
367 if (mtu <= maxmtu) {
368 int change = (ndi->linkmtu != mtu);
369
370 ndi->linkmtu = mtu;
371 if (change) /* in6_maxmtu may change */
372 in6_setmaxmtu();
373 } else {
374 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
375 "mtu=%lu sent from %s; "
376 "exceeds maxmtu %lu, ignoring\n",
377 mtu, ip6_sprintf(&ip6->ip6_src), maxmtu));
378 }
379 }
380
381 skip:
382
383 /*
384 * Source link layer address
385 */
386 {
387 char *lladdr = NULL;
388 int lladdrlen = 0;
389
390 if (ndopts.nd_opts_src_lladdr) {
391 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
392 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
393 }
394
395 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
396 nd6log((LOG_INFO,
397 "nd6_ra_input: lladdrlen mismatch for %s "
398 "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6),
399 ifp->if_addrlen, lladdrlen - 2));
400 goto bad;
401 }
402
403 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
404
405 /*
406 * Installing a link-layer address might change the state of the
407 * router's neighbor cache, which might also affect our on-link
408 * detection of adveritsed prefixes.
409 */
410 pfxlist_onlink_check();
411 }
412
413 freeit:
414 m_freem(m);
415 return;
416
417 bad:
418 icmp6stat.icp6s_badra++;
419 m_freem(m);
420 }
421
422 /*
423 * default router list proccessing sub routines
424 */
425
426 /* tell the change to user processes watching the routing socket. */
427 static void
428 nd6_rtmsg(cmd, rt)
429 int cmd;
430 struct rtentry *rt;
431 {
432 struct rt_addrinfo info;
433
434 bzero((caddr_t)&info, sizeof(info));
435 info.rti_info[RTAX_DST] = rt_key(rt);
436 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
437 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
438 if (rt->rt_ifp) {
439 info.rti_info[RTAX_IFP] =
440 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
441 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
442 }
443
444 rt_missmsg(cmd, &info, rt->rt_flags, 0);
445 }
446
447 void
448 defrouter_addreq(new)
449 struct nd_defrouter *new;
450 {
451 struct sockaddr_in6 def, mask, gate;
452 struct rtentry *newrt = NULL;
453 int s;
454 int error;
455
456 Bzero(&def, sizeof(def));
457 Bzero(&mask, sizeof(mask));
458 Bzero(&gate, sizeof(gate)); /* for safety */
459
460 def.sin6_len = mask.sin6_len = gate.sin6_len =
461 sizeof(struct sockaddr_in6);
462 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
463 gate.sin6_addr = new->rtaddr;
464 #ifndef SCOPEDROUTING
465 gate.sin6_scope_id = 0; /* XXX */
466 #endif
467
468 s = splsoftnet();
469 error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
470 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
471 RTF_GATEWAY, &newrt);
472 if (newrt) {
473 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
474 newrt->rt_refcnt--;
475 }
476 if (error == 0)
477 new->installed = 1;
478 splx(s);
479 return;
480 }
481
482 /* Add a route to a given interface as default */
483 static void
484 defrouter_addifreq(ifp)
485 struct ifnet *ifp;
486 {
487 struct sockaddr_in6 def, mask;
488 struct ifaddr *ifa;
489 struct rtentry *newrt = NULL;
490 int error, flags;
491 struct rt_addrinfo info;
492
493 /* remove one if we have already installed one */
494 if (nd6_defif_installed)
495 defrouter_delifreq();
496
497 bzero(&def, sizeof(def));
498 bzero(&mask, sizeof(mask));
499
500 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
501 def.sin6_family = mask.sin6_family = AF_INET6;
502
503 /*
504 * Search for an ifaddr beloging to the specified interface.
505 * XXX: An IPv6 address are required to be assigned on the interface.
506 */
507 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
508 nd6log((LOG_ERR, /* better error? */
509 "defrouter_addifreq: failed to find an ifaddr "
510 "to install a route to interface %s\n",
511 if_name(ifp)));
512 return;
513 }
514
515 /* RTF_CLONING is necessary to make sure to perform ND */
516 flags = ifa->ifa_flags | RTF_CLONING;
517 bzero(&info, sizeof(info));
518 info.rti_info[RTAX_DST] = (struct sockaddr *)&def;
519 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)ifa->ifa_addr;
520 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask;
521 info.rti_info[RTAX_IFA] = (struct sockaddr *)ifa->ifa_addr;
522 info.rti_flags = flags;
523 error = rtrequest1(RTM_ADD, &info, &newrt);
524 if (error != 0) {
525 nd6log((LOG_ERR,
526 "defrouter_addifreq: failed to install a route to "
527 "interface %s (errno = %d)\n",
528 if_name(ifp), error));
529
530 if (newrt) /* maybe unnecessary, but do it for safety */
531 newrt->rt_refcnt--;
532 } else {
533 if (newrt) {
534 nd6_rtmsg(RTM_ADD, newrt);
535 newrt->rt_refcnt--;
536 }
537 }
538
539 nd6_defif_installed = ifa;
540 IFAREF(ifa);
541 }
542
543 /* Remove a default route points to interface */
544 static void
545 defrouter_delifreq()
546 {
547 struct sockaddr_in6 def, mask;
548 struct rtentry *oldrt = NULL;
549
550 if (!nd6_defif_installed)
551 return;
552
553 Bzero(&def, sizeof(def));
554 Bzero(&mask, sizeof(mask));
555
556 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
557 def.sin6_family = mask.sin6_family = AF_INET6;
558
559 rtrequest(RTM_DELETE, (struct sockaddr *)&def,
560 (struct sockaddr *)nd6_defif_installed->ifa_addr,
561 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt);
562 if (oldrt) {
563 nd6_rtmsg(RTM_DELETE, oldrt);
564 if (oldrt->rt_refcnt <= 0) {
565 /*
566 * XXX: borrowed from the RTM_DELETE case of
567 * rtrequest().
568 */
569 oldrt->rt_refcnt++;
570 rtfree(oldrt);
571 }
572 }
573
574 IFAFREE(nd6_defif_installed);
575 nd6_defif_installed = NULL;
576 }
577
578 struct nd_defrouter *
579 defrouter_lookup(addr, ifp)
580 struct in6_addr *addr;
581 struct ifnet *ifp;
582 {
583 struct nd_defrouter *dr;
584
585 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
586 dr = TAILQ_NEXT(dr, dr_entry)) {
587 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
588 return (dr);
589 }
590 }
591
592 return (NULL); /* search failed */
593 }
594
595 void
596 defrtrlist_del(dr)
597 struct nd_defrouter *dr;
598 {
599 struct nd_defrouter *deldr = NULL;
600 struct nd_prefix *pr;
601
602 /*
603 * Flush all the routing table entries that use the router
604 * as a next hop.
605 */
606 if (!ip6_forwarding && ip6_accept_rtadv) /* XXX: better condition? */
607 rt6_flush(&dr->rtaddr, dr->ifp);
608
609 if (dr->installed) {
610 deldr = dr;
611 defrouter_delreq(dr);
612 }
613 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
614
615 /*
616 * Also delete all the pointers to the router in each prefix lists.
617 */
618 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
619 struct nd_pfxrouter *pfxrtr;
620 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
621 pfxrtr_del(pfxrtr);
622 }
623 pfxlist_onlink_check();
624
625 /*
626 * If the router is the primary one, choose a new one.
627 * Note that defrouter_select() will remove the current gateway
628 * from the routing table.
629 */
630 if (deldr)
631 defrouter_select();
632
633 free(dr, M_IP6NDP);
634 }
635
636 /*
637 * Remove the default route for a given router.
638 * This is just a subroutine function for defrouter_select(), and should
639 * not be called from anywhere else.
640 */
641 static void
642 defrouter_delreq(dr)
643 struct nd_defrouter *dr;
644 {
645 struct sockaddr_in6 def, mask, gw;
646 struct rtentry *oldrt = NULL;
647
648 #ifdef DIAGNOSTIC
649 if (!dr)
650 panic("dr == NULL in defrouter_delreq");
651 #endif
652
653 Bzero(&def, sizeof(def));
654 Bzero(&mask, sizeof(mask));
655 Bzero(&gw, sizeof(gw)); /* for safety */
656
657 def.sin6_len = mask.sin6_len = gw.sin6_len =
658 sizeof(struct sockaddr_in6);
659 def.sin6_family = mask.sin6_family = gw.sin6_family = AF_INET6;
660 gw.sin6_addr = dr->rtaddr;
661 #ifndef SCOPEDROUTING
662 gw.sin6_scope_id = 0; /* XXX */
663 #endif
664
665 rtrequest(RTM_DELETE, (struct sockaddr *)&def,
666 (struct sockaddr *)&gw,
667 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt);
668 if (oldrt) {
669 nd6_rtmsg(RTM_DELETE, oldrt);
670 if (oldrt->rt_refcnt <= 0) {
671 /*
672 * XXX: borrowed from the RTM_DELETE case of
673 * rtrequest().
674 */
675 oldrt->rt_refcnt++;
676 rtfree(oldrt);
677 }
678 }
679
680 dr->installed = 0;
681 }
682
683 /*
684 * remove all default routes from default router list
685 */
686 void
687 defrouter_reset()
688 {
689 struct nd_defrouter *dr;
690
691 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
692 dr = TAILQ_NEXT(dr, dr_entry))
693 defrouter_delreq(dr);
694 defrouter_delifreq();
695
696 /*
697 * XXX should we also nuke any default routers in the kernel, by
698 * going through them by rtalloc1()?
699 */
700 }
701
702 /*
703 * Default Router Selection according to Section 6.3.6 of RFC 2461 and
704 * draft-ietf-ipngwg-router-selection:
705 * 1) Routers that are reachable or probably reachable should be preferred.
706 * If we have more than one (probably) reachable router, prefer ones
707 * with the highest router preference.
708 * 2) When no routers on the list are known to be reachable or
709 * probably reachable, routers SHOULD be selected in a round-robin
710 * fashion, regardless of router preference values.
711 * 3) If the Default Router List is empty, assume that all
712 * destinations are on-link.
713 *
714 * We assume nd_defrouter is sorted by router preference value.
715 * Since the code below covers both with and without router preference cases,
716 * we do not need to classify the cases by ifdef.
717 *
718 * At this moment, we do not try to install more than one default router,
719 * even when the multipath routing is available, because we're not sure about
720 * the benefits for stub hosts comparing to the risk of making the code
721 * complicated and the possibility of introducing bugs.
722 */
723 void
724 defrouter_select()
725 {
726 int s = splsoftnet();
727 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
728 struct rtentry *rt = NULL;
729 struct llinfo_nd6 *ln = NULL;
730
731 /*
732 * This function should be called only when acting as an autoconfigured
733 * host. Although the remaining part of this function is not effective
734 * if the node is not an autoconfigured host, we explicitly exclude
735 * such cases here for safety.
736 */
737 if (ip6_forwarding || !ip6_accept_rtadv) {
738 nd6log((LOG_WARNING,
739 "defrouter_select: called unexpectedly (forwarding=%d, "
740 "accept_rtadv=%d)\n", ip6_forwarding, ip6_accept_rtadv));
741 splx(s);
742 return;
743 }
744
745 /*
746 * Let's handle easy case (3) first:
747 * If default router list is empty, we should probably install
748 * an interface route and assume that all destinations are on-link.
749 */
750 if (!TAILQ_FIRST(&nd_defrouter)) {
751 /*
752 * XXX: The specification does not say this mechanism should
753 * be restricted to hosts, but this would be not useful
754 * (even harmful) for routers.
755 * This test is meaningless due to a test at the beginning of
756 * the function, but we intentionally keep it to make the note
757 * clear.
758 */
759 if (!ip6_forwarding) {
760 if (nd6_defifp) {
761 /*
762 * Install a route to the default interface
763 * as default route.
764 */
765 defrouter_addifreq(nd6_defifp);
766 } else {
767 /*
768 * purge the existing route.
769 * XXX: is this really correct?
770 */
771 defrouter_delifreq();
772 nd6log((LOG_INFO, "defrouter_select: "
773 "there's no default router and no default"
774 " interface\n"));
775 }
776 }
777 splx(s);
778 return;
779 }
780
781 /*
782 * If we have a default route for the default interface, delete it.
783 * Note that the existence of the route is checked in the delete
784 * function.
785 */
786 defrouter_delifreq();
787
788 /*
789 * Search for a (probably) reachable router from the list.
790 * We just pick up the first reachable one (if any), assuming that
791 * the ordering rule of the list described in defrtrlist_update().
792 */
793 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
794 dr = TAILQ_NEXT(dr, dr_entry)) {
795 if (!selected_dr &&
796 (rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
797 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
798 ND6_IS_LLINFO_PROBREACH(ln)) {
799 selected_dr = dr;
800 }
801
802 if (dr->installed && !installed_dr)
803 installed_dr = dr;
804 else if (dr->installed && installed_dr) {
805 /* this should not happen. warn for diagnosis. */
806 log(LOG_ERR, "defrouter_select: more than one router"
807 " is installed\n");
808 }
809 }
810 /*
811 * If none of the default routers was found to be reachable,
812 * round-robin the list regardless of preference.
813 * Otherwise, if we have an installed router, check if the selected
814 * (reachable) router should really be preferred to the installed one.
815 * We only prefer the new router when the old one is not reachable
816 * or when the new one has a really higher preference value.
817 */
818 if (!selected_dr) {
819 if (!installed_dr || !TAILQ_NEXT(installed_dr, dr_entry))
820 selected_dr = TAILQ_FIRST(&nd_defrouter);
821 else
822 selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
823 } else if (installed_dr &&
824 (rt = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) &&
825 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
826 ND6_IS_LLINFO_PROBREACH(ln) &&
827 rtpref(selected_dr) <= rtpref(installed_dr)) {
828 selected_dr = installed_dr;
829 }
830
831 /*
832 * If the selected router is different than the installed one,
833 * remove the installed router and install the selected one.
834 * Note that the selected router is never NULL here.
835 */
836 if (installed_dr != selected_dr) {
837 if (installed_dr)
838 defrouter_delreq(installed_dr);
839 defrouter_addreq(selected_dr);
840 }
841
842 splx(s);
843 return;
844 }
845
846 /*
847 * for default router selection
848 * regards router-preference field as a 2-bit signed integer
849 */
850 static int
851 rtpref(struct nd_defrouter *dr)
852 {
853 #ifdef RTPREF
854 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
855 case ND_RA_FLAG_RTPREF_HIGH:
856 return RTPREF_HIGH;
857 case ND_RA_FLAG_RTPREF_MEDIUM:
858 case ND_RA_FLAG_RTPREF_RSV:
859 return RTPREF_MEDIUM;
860 case ND_RA_FLAG_RTPREF_LOW:
861 return RTPREF_LOW;
862 default:
863 /*
864 * This case should never happen. If it did, it would mean a
865 * serious bug of kernel internal. We thus always bark here.
866 * Or, can we even panic?
867 */
868 log(LOG_ERR, "rtpref: impossible RA flag %x", dr->flags);
869 return RTPREF_INVALID;
870 }
871 /* NOTREACHED */
872 #else
873 return 0;
874 #endif
875 }
876
877 static struct nd_defrouter *
878 defrtrlist_update(new)
879 struct nd_defrouter *new;
880 {
881 struct nd_defrouter *dr, *n;
882 int s = splsoftnet();
883
884 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
885 /* entry exists */
886 if (new->rtlifetime == 0) {
887 defrtrlist_del(dr);
888 dr = NULL;
889 } else {
890 int oldpref = rtpref(dr);
891
892 /* override */
893 dr->flags = new->flags; /* xxx flag check */
894 dr->rtlifetime = new->rtlifetime;
895 dr->expire = new->expire;
896
897 /*
898 * If the preference does not change, there's no need
899 * to sort the entries.
900 */
901 if (rtpref(new) == oldpref) {
902 splx(s);
903 return (dr);
904 }
905
906 /*
907 * preferred router may be changed, so relocate
908 * this router.
909 * XXX: calling TAILQ_REMOVE directly is a bad manner.
910 * However, since defrtrlist_del() has many side
911 * effects, we intentionally do so here.
912 * defrouter_select() below will handle routing
913 * changes later.
914 */
915 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
916 n = dr;
917 goto insert;
918 }
919 splx(s);
920 return (dr);
921 }
922
923 /* entry does not exist */
924 if (new->rtlifetime == 0) {
925 splx(s);
926 return (NULL);
927 }
928
929 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
930 if (n == NULL) {
931 splx(s);
932 return (NULL);
933 }
934 bzero(n, sizeof(*n));
935 *n = *new;
936
937 insert:
938 /*
939 * Insert the new router in the Default Router List;
940 * The Default Router List should be in the descending order
941 * of router-preferece. Routers with the same preference are
942 * sorted in the arriving time order.
943 */
944
945 /* insert at the end of the group */
946 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
947 dr = TAILQ_NEXT(dr, dr_entry)) {
948 if (rtpref(n) > rtpref(dr))
949 break;
950 }
951 if (dr)
952 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
953 else
954 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
955
956 defrouter_select();
957
958 splx(s);
959
960 return (n);
961 }
962
963 static struct nd_pfxrouter *
964 pfxrtr_lookup(pr, dr)
965 struct nd_prefix *pr;
966 struct nd_defrouter *dr;
967 {
968 struct nd_pfxrouter *search;
969
970 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
971 if (search->router == dr)
972 break;
973 }
974
975 return (search);
976 }
977
978 static void
979 pfxrtr_add(pr, dr)
980 struct nd_prefix *pr;
981 struct nd_defrouter *dr;
982 {
983 struct nd_pfxrouter *new;
984
985 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
986 if (new == NULL)
987 return;
988 bzero(new, sizeof(*new));
989 new->router = dr;
990
991 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
992
993 pfxlist_onlink_check();
994 }
995
996 static void
997 pfxrtr_del(pfr)
998 struct nd_pfxrouter *pfr;
999 {
1000 LIST_REMOVE(pfr, pfr_entry);
1001 free(pfr, M_IP6NDP);
1002 }
1003
1004 struct nd_prefix *
1005 nd6_prefix_lookup(pr)
1006 struct nd_prefix *pr;
1007 {
1008 struct nd_prefix *search;
1009
1010 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
1011 if (pr->ndpr_ifp == search->ndpr_ifp &&
1012 pr->ndpr_plen == search->ndpr_plen &&
1013 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1014 &search->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1015 break;
1016 }
1017 }
1018
1019 return (search);
1020 }
1021
1022 int
1023 nd6_prelist_add(pr, dr, newp)
1024 struct nd_prefix *pr, **newp;
1025 struct nd_defrouter *dr;
1026 {
1027 struct nd_prefix *new = NULL;
1028 int i, s;
1029
1030 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
1031 if (new == NULL)
1032 return ENOMEM;
1033 bzero(new, sizeof(*new));
1034 *new = *pr;
1035 if (newp != NULL)
1036 *newp = new;
1037
1038 /* initilization */
1039 LIST_INIT(&new->ndpr_advrtrs);
1040 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
1041 /* make prefix in the canonical form */
1042 for (i = 0; i < 4; i++)
1043 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
1044 new->ndpr_mask.s6_addr32[i];
1045
1046 s = splsoftnet();
1047 /* link ndpr_entry to nd_prefix list */
1048 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
1049 splx(s);
1050
1051 /* ND_OPT_PI_FLAG_ONLINK processing */
1052 if (new->ndpr_raf_onlink) {
1053 int e;
1054
1055 if ((e = nd6_prefix_onlink(new)) != 0) {
1056 nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
1057 "the prefix %s/%d on-link on %s (errno=%d)\n",
1058 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1059 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1060 /* proceed anyway. XXX: is it correct? */
1061 }
1062 }
1063
1064 if (dr)
1065 pfxrtr_add(new, dr);
1066
1067 return 0;
1068 }
1069
1070 void
1071 prelist_remove(pr)
1072 struct nd_prefix *pr;
1073 {
1074 struct nd_pfxrouter *pfr, *next;
1075 int e, s;
1076
1077 /* make sure to invalidate the prefix until it is really freed. */
1078 pr->ndpr_vltime = 0;
1079 pr->ndpr_pltime = 0;
1080 #if 0
1081 /*
1082 * Though these flags are now meaningless, we'd rather keep the value
1083 * not to confuse users when executing "ndp -p".
1084 */
1085 pr->ndpr_raf_onlink = 0;
1086 pr->ndpr_raf_auto = 0;
1087 #endif
1088 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
1089 (e = nd6_prefix_offlink(pr)) != 0) {
1090 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink "
1091 "on %s, errno=%d\n",
1092 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1093 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1094 /* what should we do? */
1095 }
1096
1097 if (pr->ndpr_refcnt > 0)
1098 return; /* notice here? */
1099
1100 s = splsoftnet();
1101 /* unlink ndpr_entry from nd_prefix list */
1102 LIST_REMOVE(pr, ndpr_entry);
1103
1104 /* free list of routers that adversed the prefix */
1105 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
1106 next = pfr->pfr_next;
1107
1108 free(pfr, M_IP6NDP);
1109 }
1110 splx(s);
1111
1112 free(pr, M_IP6NDP);
1113
1114 pfxlist_onlink_check();
1115 }
1116
1117 int
1118 prelist_update(new, dr, m)
1119 struct nd_prefix *new;
1120 struct nd_defrouter *dr; /* may be NULL */
1121 struct mbuf *m;
1122 {
1123 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1124 struct ifaddr *ifa;
1125 struct ifnet *ifp = new->ndpr_ifp;
1126 struct nd_prefix *pr;
1127 int s = splsoftnet();
1128 int error = 0;
1129 int newprefix = 0;
1130 int auth;
1131 struct in6_addrlifetime lt6_tmp;
1132
1133 auth = 0;
1134 if (m) {
1135 /*
1136 * Authenticity for NA consists authentication for
1137 * both IP header and IP datagrams, doesn't it ?
1138 */
1139 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1140 auth = (m->m_flags & M_AUTHIPHDR
1141 && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
1142 #endif
1143 }
1144
1145 if ((pr = nd6_prefix_lookup(new)) != NULL) {
1146 /*
1147 * nd6_prefix_lookup() ensures that pr and new have the same
1148 * prefix on a same interface.
1149 */
1150
1151 /*
1152 * Update prefix information. Note that the on-link (L) bit
1153 * and the autonomous (A) bit should NOT be changed from 1
1154 * to 0.
1155 */
1156 if (new->ndpr_raf_onlink == 1)
1157 pr->ndpr_raf_onlink = 1;
1158 if (new->ndpr_raf_auto == 1)
1159 pr->ndpr_raf_auto = 1;
1160 if (new->ndpr_raf_onlink) {
1161 pr->ndpr_vltime = new->ndpr_vltime;
1162 pr->ndpr_pltime = new->ndpr_pltime;
1163 pr->ndpr_preferred = new->ndpr_preferred;
1164 pr->ndpr_expire = new->ndpr_expire;
1165 pr->ndpr_lastupdate = new->ndpr_lastupdate;
1166 }
1167
1168 if (new->ndpr_raf_onlink &&
1169 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1170 int e;
1171
1172 if ((e = nd6_prefix_onlink(pr)) != 0) {
1173 nd6log((LOG_ERR,
1174 "prelist_update: failed to make "
1175 "the prefix %s/%d on-link on %s "
1176 "(errno=%d)\n",
1177 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1178 pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
1179 /* proceed anyway. XXX: is it correct? */
1180 }
1181 }
1182
1183 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1184 pfxrtr_add(pr, dr);
1185 } else {
1186 struct nd_prefix *newpr = NULL;
1187
1188 newprefix = 1;
1189
1190 if (new->ndpr_vltime == 0)
1191 goto end;
1192 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
1193 goto end;
1194
1195 error = nd6_prelist_add(new, dr, &newpr);
1196 if (error != 0 || newpr == NULL) {
1197 nd6log((LOG_NOTICE, "prelist_update: "
1198 "nd6_prelist_add failed for %s/%d on %s "
1199 "errno=%d, returnpr=%p\n",
1200 ip6_sprintf(&new->ndpr_prefix.sin6_addr),
1201 new->ndpr_plen, if_name(new->ndpr_ifp),
1202 error, newpr));
1203 goto end; /* we should just give up in this case. */
1204 }
1205
1206 /*
1207 * XXX: from the ND point of view, we can ignore a prefix
1208 * with the on-link bit being zero. However, we need a
1209 * prefix structure for references from autoconfigured
1210 * addresses. Thus, we explicitly make sure that the prefix
1211 * itself expires now.
1212 */
1213 if (newpr->ndpr_raf_onlink == 0) {
1214 newpr->ndpr_vltime = 0;
1215 newpr->ndpr_pltime = 0;
1216 in6_init_prefix_ltimes(newpr);
1217 }
1218
1219 pr = newpr;
1220 }
1221
1222 /*
1223 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1224 * Note that pr must be non NULL at this point.
1225 */
1226
1227 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1228 if (!new->ndpr_raf_auto)
1229 goto end;
1230
1231 /*
1232 * 5.5.3 (b). the link-local prefix should have been ignored in
1233 * nd6_ra_input.
1234 */
1235
1236 /*
1237 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime.
1238 * This should have been done in nd6_ra_input.
1239 */
1240
1241 /*
1242 * 5.5.3 (d). If the prefix advertised does not match the prefix of an
1243 * address already in the list, and the Valid Lifetime is not 0,
1244 * form an address. Note that even a manually configured address
1245 * should reject autoconfiguration of a new address.
1246 */
1247 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1248 {
1249 struct in6_ifaddr *ifa6;
1250 int ifa_plen;
1251 u_int32_t storedlifetime;
1252 long time_second = time.tv_sec;
1253
1254 if (ifa->ifa_addr->sa_family != AF_INET6)
1255 continue;
1256
1257 ifa6 = (struct in6_ifaddr *)ifa;
1258
1259 /*
1260 * Spec is not clear here, but I believe we should concentrate
1261 * on unicast (i.e. not anycast) addresses.
1262 * XXX: other ia6_flags? detached or duplicated?
1263 */
1264 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1265 continue;
1266
1267 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL);
1268 if (ifa_plen != new->ndpr_plen ||
1269 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr,
1270 &new->ndpr_prefix.sin6_addr, ifa_plen))
1271 continue;
1272
1273 if (ia6_match == NULL) /* remember the first one */
1274 ia6_match = ifa6;
1275
1276 if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1277 continue;
1278
1279 /*
1280 * An already autoconfigured address matched. Now that we
1281 * are sure there is at least one matched address, we can
1282 * proceed to 5.5.3. (e): update the lifetimes according to the
1283 * "two hours" rule and the privacy extension.
1284 */
1285 #define TWOHOUR (120*60)
1286 /*
1287 * RFC2462 introduces the notion of StoredLifetime to the
1288 * "two hours" rule as follows:
1289 * the Lifetime associated with the previously autoconfigured
1290 * address.
1291 * Our interpretation of this definition is "the remaining
1292 * lifetime to expiration at the evaluation time". One might
1293 * be wondering if this interpretation is really conform to the
1294 * RFC, because the text can read that "Lifetimes" are never
1295 * decreased, and our definition of the "storedlifetime" below
1296 * essentially reduces the "Valid Lifetime" advertised in the
1297 * previous RA. But, this is due to the wording of the text,
1298 * and our interpretation is the same as an author's intention.
1299 * See the discussion in the IETF ipngwg ML in August 2001,
1300 * with the Subject "StoredLifetime in RFC 2462".
1301 */
1302 lt6_tmp = ifa6->ia6_lifetime;
1303 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1304 storedlifetime = ND6_INFINITE_LIFETIME;
1305 else if (time_second - ifa6->ia6_updatetime >
1306 lt6_tmp.ia6t_vltime) {
1307 /*
1308 * The case of "invalid" address. We should usually
1309 * not see this case.
1310 */
1311 storedlifetime = 0;
1312 } else
1313 storedlifetime = lt6_tmp.ia6t_vltime -
1314 (time_second - ifa6->ia6_updatetime);
1315 if (TWOHOUR < new->ndpr_vltime ||
1316 storedlifetime < new->ndpr_vltime) {
1317 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1318 } else if (storedlifetime <= TWOHOUR
1319 #if 0
1320 /*
1321 * This condition is logically redundant, so we just
1322 * omit it.
1323 * See IPng 6712, 6717, and 6721.
1324 */
1325 && new->ndpr_vltime <= storedlifetime
1326 #endif
1327 ) {
1328 if (auth) {
1329 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1330 }
1331 } else {
1332 /*
1333 * new->ndpr_vltime <= TWOHOUR &&
1334 * TWOHOUR < storedlifetime
1335 */
1336 lt6_tmp.ia6t_vltime = TWOHOUR;
1337 }
1338
1339 /* The 2 hour rule is not imposed for preferred lifetime. */
1340 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1341
1342 in6_init_address_ltimes(pr, <6_tmp);
1343
1344 ifa6->ia6_lifetime = lt6_tmp;
1345 ifa6->ia6_updatetime = time_second;
1346 }
1347 if (ia6_match == NULL && new->ndpr_vltime) {
1348 /*
1349 * No address matched and the valid lifetime is non-zero.
1350 * Create a new address.
1351 */
1352 if ((ia6 = in6_ifadd(new)) != NULL) {
1353 /*
1354 * note that we should use pr (not new) for reference.
1355 */
1356 pr->ndpr_refcnt++;
1357 ia6->ia6_ndpr = pr;
1358
1359 /*
1360 * A newly added address might affect the status
1361 * of other addresses, so we check and update it.
1362 * XXX: what if address duplication happens?
1363 */
1364 pfxlist_onlink_check();
1365 } else {
1366 /* just set an error. do not bark here. */
1367 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1368 }
1369 }
1370
1371 end:
1372 splx(s);
1373 return error;
1374 }
1375
1376 /*
1377 * A supplement function used in the on-link detection below;
1378 * detect if a given prefix has a (probably) reachable advertising router.
1379 * XXX: lengthy function name...
1380 */
1381 static struct nd_pfxrouter *
1382 find_pfxlist_reachable_router(pr)
1383 struct nd_prefix *pr;
1384 {
1385 struct nd_pfxrouter *pfxrtr;
1386 struct rtentry *rt;
1387 struct llinfo_nd6 *ln;
1388
1389 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1390 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1391 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1392 pfxrtr->router->ifp)) &&
1393 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1394 ND6_IS_LLINFO_PROBREACH(ln))
1395 break; /* found */
1396 }
1397
1398 return (pfxrtr);
1399 }
1400
1401 /*
1402 * Check if each prefix in the prefix list has at least one available router
1403 * that advertised the prefix (a router is "available" if its neighbor cache
1404 * entry is reachable or probably reachable).
1405 * If the check fails, the prefix may be off-link, because, for example,
1406 * we have moved from the network but the lifetime of the prefix has not
1407 * expired yet. So we should not use the prefix if there is another prefix
1408 * that has an available router.
1409 * But, if there is no prefix that has an available router, we still regards
1410 * all the prefixes as on-link. This is because we can't tell if all the
1411 * routers are simply dead or if we really moved from the network and there
1412 * is no router around us.
1413 */
1414 void
1415 pfxlist_onlink_check()
1416 {
1417 struct nd_prefix *pr;
1418 struct in6_ifaddr *ifa;
1419
1420 /*
1421 * Check if there is a prefix that has a reachable advertising
1422 * router.
1423 */
1424 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1425 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1426 break;
1427 }
1428 if (pr != NULL || TAILQ_FIRST(&nd_defrouter) != NULL) {
1429 /*
1430 * There is at least one prefix that has a reachable router,
1431 * or at least a router which probably does not advertise
1432 * any prefixes. The latter would be the case when we move
1433 * to a new link where we have a router that does not provide
1434 * prefixes and we configure an address by hand.
1435 * Detach prefixes which have no reachable advertising
1436 * router, and attach other prefixes.
1437 */
1438 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1439 /* XXX: a link-local prefix should never be detached */
1440 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1441 continue;
1442
1443 /*
1444 * we aren't interested in prefixes without the L bit
1445 * set.
1446 */
1447 if (pr->ndpr_raf_onlink == 0)
1448 continue;
1449
1450 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1451 find_pfxlist_reachable_router(pr) == NULL)
1452 pr->ndpr_stateflags |= NDPRF_DETACHED;
1453 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1454 find_pfxlist_reachable_router(pr) != 0)
1455 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1456 }
1457 } else {
1458 /* there is no prefix that has a reachable router */
1459 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1460 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1461 continue;
1462
1463 if (pr->ndpr_raf_onlink == 0)
1464 continue;
1465
1466 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1467 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1468 }
1469 }
1470
1471 /*
1472 * Remove each interface route associated with a (just) detached
1473 * prefix, and reinstall the interface route for a (just) attached
1474 * prefix. Note that all attempt of reinstallation does not
1475 * necessarily success, when a same prefix is shared among multiple
1476 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1477 * so we don't have to care about them.
1478 */
1479 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1480 int e;
1481
1482 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1483 continue;
1484
1485 if (pr->ndpr_raf_onlink == 0)
1486 continue;
1487
1488 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1489 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1490 if ((e = nd6_prefix_offlink(pr)) != 0) {
1491 nd6log((LOG_ERR,
1492 "pfxlist_onlink_check: failed to "
1493 "make %s/%d offlink, errno=%d\n",
1494 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1495 pr->ndpr_plen, e));
1496 }
1497 }
1498 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1499 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1500 pr->ndpr_raf_onlink) {
1501 if ((e = nd6_prefix_onlink(pr)) != 0) {
1502 nd6log((LOG_ERR,
1503 "pfxlist_onlink_check: failed to "
1504 "make %s/%d offlink, errno=%d\n",
1505 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1506 pr->ndpr_plen, e));
1507 }
1508 }
1509 }
1510
1511 /*
1512 * Changes on the prefix status might affect address status as well.
1513 * Make sure that all addresses derived from an attached prefix are
1514 * attached, and that all addresses derived from a detached prefix are
1515 * detached. Note, however, that a manually configured address should
1516 * always be attached.
1517 * The precise detection logic is same as the one for prefixes.
1518 */
1519 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1520 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1521 continue;
1522
1523 if (ifa->ia6_ndpr == NULL) {
1524 /*
1525 * This can happen when we first configure the address
1526 * (i.e. the address exists, but the prefix does not).
1527 * XXX: complicated relationships...
1528 */
1529 continue;
1530 }
1531
1532 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1533 break;
1534 }
1535 if (ifa) {
1536 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1537 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1538 continue;
1539
1540 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1541 continue;
1542
1543 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1544 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1545 else
1546 ifa->ia6_flags |= IN6_IFF_DETACHED;
1547 }
1548 }
1549 else {
1550 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1551 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1552 continue;
1553
1554 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1555 }
1556 }
1557 }
1558
1559 int
1560 nd6_prefix_onlink(pr)
1561 struct nd_prefix *pr;
1562 {
1563 struct ifaddr *ifa;
1564 struct ifnet *ifp = pr->ndpr_ifp;
1565 struct sockaddr_in6 mask6;
1566 struct nd_prefix *opr;
1567 u_long rtflags;
1568 int error = 0;
1569 struct rtentry *rt = NULL;
1570
1571 /* sanity check */
1572 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1573 nd6log((LOG_ERR,
1574 "nd6_prefix_onlink: %s/%d is already on-link\n",
1575 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
1576 return (EEXIST));
1577 }
1578
1579 /*
1580 * Add the interface route associated with the prefix. Before
1581 * installing the route, check if there's the same prefix on another
1582 * interface, and the prefix has already installed the interface route.
1583 * Although such a configuration is expected to be rare, we explicitly
1584 * allow it.
1585 */
1586 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1587 if (opr == pr)
1588 continue;
1589
1590 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1591 continue;
1592
1593 if (opr->ndpr_plen == pr->ndpr_plen &&
1594 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1595 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1596 return (0);
1597 }
1598
1599 /*
1600 * We prefer link-local addresses as the associated interface address.
1601 */
1602 /* search for a link-local addr */
1603 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1604 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1605 if (ifa == NULL) {
1606 /* XXX: freebsd does not have ifa_ifwithaf */
1607 for (ifa = ifp->if_addrlist.tqh_first;
1608 ifa;
1609 ifa = ifa->ifa_list.tqe_next)
1610 {
1611 if (ifa->ifa_addr->sa_family == AF_INET6)
1612 break;
1613 }
1614 /* should we care about ia6_flags? */
1615 }
1616 if (ifa == NULL) {
1617 /*
1618 * This can still happen, when, for example, we receive an RA
1619 * containing a prefix with the L bit set and the A bit clear,
1620 * after removing all IPv6 addresses on the receiving
1621 * interface. This should, of course, be rare though.
1622 */
1623 nd6log((LOG_NOTICE,
1624 "nd6_prefix_onlink: failed to find any ifaddr"
1625 " to add route for a prefix(%s/%d) on %s\n",
1626 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1627 pr->ndpr_plen, if_name(ifp)));
1628 return (0);
1629 }
1630
1631 /*
1632 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1633 * ifa->ifa_rtrequest = nd6_rtrequest;
1634 */
1635 bzero(&mask6, sizeof(mask6));
1636 mask6.sin6_len = sizeof(mask6);
1637 mask6.sin6_addr = pr->ndpr_mask;
1638 /* rtrequest() will probably set RTF_UP, but we're not sure. */
1639 rtflags = ifa->ifa_flags | RTF_UP;
1640 if (nd6_need_cache(ifp)) {
1641 /* explicitly set in case ifa_flags does not set the flag. */
1642 rtflags |= RTF_CLONING;
1643 } else {
1644 /*
1645 * explicitly clear the cloning bit in case ifa_flags sets it.
1646 */
1647 rtflags &= ~RTF_CLONING;
1648 }
1649 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1650 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt);
1651 if (error == 0) {
1652 if (rt != NULL) /* this should be non NULL, though */
1653 nd6_rtmsg(RTM_ADD, rt);
1654 pr->ndpr_stateflags |= NDPRF_ONLINK;
1655 } else {
1656 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1657 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1658 "errno = %d\n",
1659 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1660 pr->ndpr_plen, if_name(ifp),
1661 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1662 ip6_sprintf(&mask6.sin6_addr), rtflags, error));
1663 }
1664
1665 if (rt != NULL)
1666 rt->rt_refcnt--;
1667
1668 return (error);
1669 }
1670
1671 int
1672 nd6_prefix_offlink(pr)
1673 struct nd_prefix *pr;
1674 {
1675 int error = 0;
1676 struct ifnet *ifp = pr->ndpr_ifp;
1677 struct nd_prefix *opr;
1678 struct sockaddr_in6 sa6, mask6;
1679 struct rtentry *rt = NULL;
1680
1681 /* sanity check */
1682 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1683 nd6log((LOG_ERR,
1684 "nd6_prefix_offlink: %s/%d is already off-link\n",
1685 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1686 return (EEXIST);
1687 }
1688
1689 bzero(&sa6, sizeof(sa6));
1690 sa6.sin6_family = AF_INET6;
1691 sa6.sin6_len = sizeof(sa6);
1692 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1693 sizeof(struct in6_addr));
1694 bzero(&mask6, sizeof(mask6));
1695 mask6.sin6_family = AF_INET6;
1696 mask6.sin6_len = sizeof(sa6);
1697 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1698 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1699 (struct sockaddr *)&mask6, 0, &rt);
1700 if (error == 0) {
1701 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1702
1703 /* report the route deletion to the routing socket. */
1704 if (rt != NULL)
1705 nd6_rtmsg(RTM_DELETE, rt);
1706
1707 /*
1708 * There might be the same prefix on another interface,
1709 * the prefix which could not be on-link just because we have
1710 * the interface route (see comments in nd6_prefix_onlink).
1711 * If there's one, try to make the prefix on-link on the
1712 * interface.
1713 */
1714 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
1715 if (opr == pr)
1716 continue;
1717
1718 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1719 continue;
1720
1721 /*
1722 * KAME specific: detached prefixes should not be
1723 * on-link.
1724 */
1725 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1726 continue;
1727
1728 if (opr->ndpr_plen == pr->ndpr_plen &&
1729 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1730 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1731 int e;
1732
1733 if ((e = nd6_prefix_onlink(opr)) != 0) {
1734 nd6log((LOG_ERR,
1735 "nd6_prefix_offlink: failed to "
1736 "recover a prefix %s/%d from %s "
1737 "to %s (errno = %d)\n",
1738 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1739 opr->ndpr_plen, if_name(ifp),
1740 if_name(opr->ndpr_ifp), e));
1741 }
1742 }
1743 }
1744 } else {
1745 /* XXX: can we still set the NDPRF_ONLINK flag? */
1746 nd6log((LOG_ERR,
1747 "nd6_prefix_offlink: failed to delete route: "
1748 "%s/%d on %s (errno = %d)\n",
1749 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
1750 error));
1751 }
1752
1753 if (rt != NULL) {
1754 if (rt->rt_refcnt <= 0) {
1755 /* XXX: we should free the entry ourselves. */
1756 rt->rt_refcnt++;
1757 rtfree(rt);
1758 }
1759 }
1760
1761 return (error);
1762 }
1763
1764 static struct in6_ifaddr *
1765 in6_ifadd(pr)
1766 struct nd_prefix *pr;
1767 {
1768 struct ifnet *ifp = pr->ndpr_ifp;
1769 struct ifaddr *ifa;
1770 struct in6_aliasreq ifra;
1771 struct in6_ifaddr *ia, *ib;
1772 int error, plen0;
1773 struct in6_addr mask;
1774 int prefixlen = pr->ndpr_plen;
1775
1776 in6_prefixlen2mask(&mask, prefixlen);
1777
1778 /*
1779 * find a link-local address (will be interface ID).
1780 * Is it really mandatory? Theoretically, a global or a site-local
1781 * address can be configured without a link-local address, if we
1782 * have a unique interface identifier...
1783 *
1784 * it is not mandatory to have a link-local address, we can generate
1785 * interface identifier on the fly. we do this because:
1786 * (1) it should be the easiest way to find interface identifier.
1787 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1788 * for multiple addresses on a single interface, and possible shortcut
1789 * of DAD. we omitted DAD for this reason in the past.
1790 * (3) a user can prevent autoconfiguration of global address
1791 * by removing link-local address by hand (this is partly because we
1792 * don't have other way to control the use of IPv6 on a interface.
1793 * this has been our design choice - cf. NRL's "ifconfig auto").
1794 * (4) it is easier to manage when an interface has addresses
1795 * with the same interface identifier, than to have multiple addresses
1796 * with different interface identifiers.
1797 */
1798 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1799 if (ifa)
1800 ib = (struct in6_ifaddr *)ifa;
1801 else
1802 return NULL;
1803
1804 #if 0 /* don't care link local addr state, and always do DAD */
1805 /* if link-local address is not eligible, do not autoconfigure. */
1806 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1807 printf("in6_ifadd: link-local address not ready\n");
1808 return NULL;
1809 }
1810 #endif
1811
1812 /* prefixlen + ifidlen must be equal to 128 */
1813 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1814 if (prefixlen != plen0) {
1815 nd6log((LOG_ERR, "in6_ifadd: wrong prefixlen for %s"
1816 "(prefix=%d ifid=%d)\n",
1817 if_name(ifp), prefixlen, 128 - plen0));
1818 return NULL;
1819 }
1820
1821 /* make ifaddr */
1822
1823 bzero(&ifra, sizeof(ifra));
1824 /*
1825 * in6_update_ifa() does not use ifra_name, but we accurately set it
1826 * for safety.
1827 */
1828 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1829 ifra.ifra_addr.sin6_family = AF_INET6;
1830 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1831 /* prefix */
1832 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
1833 sizeof(ifra.ifra_addr.sin6_addr));
1834 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1835 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1836 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1837 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1838
1839 /* interface ID */
1840 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1841 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1842 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1843 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1844 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1845 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1846 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1847 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1848
1849 /* new prefix mask. */
1850 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1851 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1852 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1853 sizeof(ifra.ifra_prefixmask.sin6_addr));
1854
1855 /*
1856 * lifetime.
1857 * XXX: in6_init_address_ltimes would override these values later.
1858 * We should reconsider this logic.
1859 */
1860 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1861 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1862
1863 /* XXX: scope zone ID? */
1864
1865 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1866
1867 /* allocate ifaddr structure, link into chain, etc. */
1868 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
1869 nd6log((LOG_ERR,
1870 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1871 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
1872 error));
1873 return (NULL); /* ifaddr must not have been allocated. */
1874 }
1875
1876 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1877
1878 return (ia); /* this is always non-NULL */
1879 }
1880
1881 int
1882 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1883 {
1884 long time_second = time.tv_sec;
1885
1886 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
1887 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1888 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1889 "(%d) is greater than valid lifetime(%d)\n",
1890 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
1891 return (EINVAL);
1892 }
1893 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1894 ndpr->ndpr_preferred = 0;
1895 else
1896 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
1897 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1898 ndpr->ndpr_expire = 0;
1899 else
1900 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
1901
1902 return 0;
1903 }
1904
1905 static void
1906 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1907 {
1908 long time_second = time.tv_sec;
1909
1910 /* Valid lifetime must not be updated unless explicitly specified. */
1911 /* init ia6t_expire */
1912 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1913 lt6->ia6t_expire = 0;
1914 else {
1915 lt6->ia6t_expire = time_second;
1916 lt6->ia6t_expire += lt6->ia6t_vltime;
1917 }
1918
1919 /* init ia6t_preferred */
1920 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1921 lt6->ia6t_preferred = 0;
1922 else {
1923 lt6->ia6t_preferred = time_second;
1924 lt6->ia6t_preferred += lt6->ia6t_pltime;
1925 }
1926 }
1927
1928 /*
1929 * Delete all the routing table entries that use the specified gateway.
1930 * XXX: this function causes search through all entries of routing table, so
1931 * it shouldn't be called when acting as a router.
1932 */
1933 void
1934 rt6_flush(gateway, ifp)
1935 struct in6_addr *gateway;
1936 struct ifnet *ifp;
1937 {
1938 struct radix_node_head *rnh = rt_tables[AF_INET6];
1939 int s = splsoftnet();
1940
1941 /* We'll care only link-local addresses */
1942 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1943 splx(s);
1944 return;
1945 }
1946 /* XXX: hack for KAME's link-local address kludge */
1947 gateway->s6_addr16[1] = htons(ifp->if_index);
1948
1949 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1950 splx(s);
1951 }
1952
1953 static int
1954 rt6_deleteroute(rn, arg)
1955 struct radix_node *rn;
1956 void *arg;
1957 {
1958 #define SIN6(s) ((struct sockaddr_in6 *)s)
1959 struct rtentry *rt = (struct rtentry *)rn;
1960 struct in6_addr *gate = (struct in6_addr *)arg;
1961
1962 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1963 return (0);
1964
1965 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1966 return (0);
1967
1968 /*
1969 * Do not delete a static route.
1970 * XXX: this seems to be a bit ad-hoc. Should we consider the
1971 * 'cloned' bit instead?
1972 */
1973 if ((rt->rt_flags & RTF_STATIC) != 0)
1974 return (0);
1975
1976 /*
1977 * Do not delete a static route.
1978 * XXX: this seems to be a bit ad-hoc. Should we consider the
1979 * 'cloned' bit instead?
1980 */
1981 if ((rt->rt_flags & RTF_STATIC) != 0)
1982 return (0);
1983
1984 /*
1985 * We delete only host route. This means, in particular, we don't
1986 * delete default route.
1987 */
1988 if ((rt->rt_flags & RTF_HOST) == 0)
1989 return (0);
1990
1991 return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1992 rt_mask(rt), rt->rt_flags, 0));
1993 #undef SIN6
1994 }
1995
1996 int
1997 nd6_setdefaultiface(ifindex)
1998 int ifindex;
1999 {
2000 int error = 0;
2001
2002 if (ifindex < 0 || if_index < ifindex)
2003 return (EINVAL);
2004
2005 if (nd6_defifindex != ifindex) {
2006 nd6_defifindex = ifindex;
2007 if (nd6_defifindex > 0) {
2008 nd6_defifp = ifindex2ifnet[nd6_defifindex];
2009 } else
2010 nd6_defifp = NULL;
2011
2012 /*
2013 * Rescan default router list, refresh default route(s).
2014 */
2015 defrouter_select();
2016 }
2017
2018 return (error);
2019 }
2020