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