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