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