nd6_rtr.c revision 1.113 1 /* $NetBSD: nd6_rtr.c,v 1.113 2016/07/04 06:48:14 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.113 2016/07/04 06:48:14 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 IFADDR_FOREACH_SAFE(ifa, ifp, ifa_next) {
917 if (ifa->ifa_addr->sa_family != AF_INET6)
918 continue;
919 ia = (struct in6_ifaddr *)ifa;
920 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) ==
921 IN6_IFF_AUTOCONF && ia->ia6_ndpr == pr) {
922 in6_purgeaddr(ifa);
923 }
924 }
925 if (pr->ndpr_refcnt == 0)
926 prelist_remove(pr);
927 }
928 }
929 int
930 nd6_prelist_add(struct nd_prefixctl *prc, struct nd_defrouter *dr,
931 struct nd_prefix **newp)
932 {
933 struct nd_prefix *newpr = NULL;
934 int i, s;
935 int error;
936 struct in6_ifextra *ext = prc->ndprc_ifp->if_afdata[AF_INET6];
937
938 if (ip6_maxifprefixes >= 0) {
939 if (ext->nprefixes >= ip6_maxifprefixes / 2)
940 purge_detached(prc->ndprc_ifp);
941 if (ext->nprefixes >= ip6_maxifprefixes)
942 return ENOMEM;
943 }
944
945 error = 0;
946 newpr = malloc(sizeof(*newpr), M_IP6NDP, M_NOWAIT|M_ZERO);
947 if (newpr == NULL)
948 return ENOMEM;
949 newpr->ndpr_ifp = prc->ndprc_ifp;
950 newpr->ndpr_prefix = prc->ndprc_prefix;
951 newpr->ndpr_plen = prc->ndprc_plen;
952 newpr->ndpr_vltime = prc->ndprc_vltime;
953 newpr->ndpr_pltime = prc->ndprc_pltime;
954 newpr->ndpr_flags = prc->ndprc_flags;
955 if ((error = in6_init_prefix_ltimes(newpr)) != 0) {
956 free(newpr, M_IP6NDP);
957 return(error);
958 }
959 newpr->ndpr_lastupdate = time_uptime;
960 if (newp != NULL)
961 *newp = newpr;
962
963 /* initialization */
964 LIST_INIT(&newpr->ndpr_advrtrs);
965 in6_prefixlen2mask(&newpr->ndpr_mask, newpr->ndpr_plen);
966 /* make prefix in the canonical form */
967 for (i = 0; i < 4; i++) {
968 newpr->ndpr_prefix.sin6_addr.s6_addr32[i] &=
969 newpr->ndpr_mask.s6_addr32[i];
970 }
971
972 s = splsoftnet();
973 /* link ndpr_entry to nd_prefix list */
974 LIST_INSERT_HEAD(&nd_prefix, newpr, ndpr_entry);
975 splx(s);
976
977 /* ND_OPT_PI_FLAG_ONLINK processing */
978 if (newpr->ndpr_raf_onlink) {
979 int e;
980
981 if ((e = nd6_prefix_onlink(newpr)) != 0) {
982 nd6log(LOG_ERR, "failed to make "
983 "the prefix %s/%d on-link on %s (errno=%d)\n",
984 ip6_sprintf(&prc->ndprc_prefix.sin6_addr),
985 prc->ndprc_plen, if_name(prc->ndprc_ifp), e);
986 /* proceed anyway. XXX: is it correct? */
987 }
988 }
989
990 if (dr)
991 pfxrtr_add(newpr, dr);
992
993 ext->nprefixes++;
994
995 return 0;
996 }
997
998 void
999 prelist_remove(struct nd_prefix *pr)
1000 {
1001 struct nd_pfxrouter *pfr, *next;
1002 int e, s;
1003 struct in6_ifextra *ext = pr->ndpr_ifp->if_afdata[AF_INET6];
1004
1005 /* make sure to invalidate the prefix until it is really freed. */
1006 pr->ndpr_vltime = 0;
1007 pr->ndpr_pltime = 0;
1008 #if 0
1009 /*
1010 * Though these flags are now meaningless, we'd rather keep the value
1011 * not to confuse users when executing "ndp -p".
1012 */
1013 pr->ndpr_raf_onlink = 0;
1014 pr->ndpr_raf_auto = 0;
1015 #endif
1016 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
1017 (e = nd6_prefix_offlink(pr)) != 0) {
1018 nd6log(LOG_ERR,
1019 "failed to make %s/%d offlink on %s, errno=%d\n",
1020 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1021 pr->ndpr_plen, if_name(pr->ndpr_ifp), e);
1022 /* what should we do? */
1023 }
1024
1025 if (pr->ndpr_refcnt > 0)
1026 return; /* notice here? */
1027
1028 s = splsoftnet();
1029 /* unlink ndpr_entry from nd_prefix list */
1030 LIST_REMOVE(pr, ndpr_entry);
1031
1032 /* free list of routers that adversed the prefix */
1033 for (pfr = LIST_FIRST(&pr->ndpr_advrtrs); pfr != NULL; pfr = next) {
1034 next = LIST_NEXT(pfr, pfr_entry);
1035
1036 free(pfr, M_IP6NDP);
1037 }
1038
1039 if (ext) {
1040 ext->nprefixes--;
1041 if (ext->nprefixes < 0) {
1042 log(LOG_WARNING, "prelist_remove: negative count on "
1043 "%s\n", pr->ndpr_ifp->if_xname);
1044 }
1045 }
1046 splx(s);
1047
1048 free(pr, M_IP6NDP);
1049
1050 pfxlist_onlink_check();
1051 }
1052
1053 static int
1054 prelist_update(struct nd_prefixctl *newprc,
1055 struct nd_defrouter *dr, /* may be NULL */
1056 struct mbuf *m,
1057 int mcast)
1058 {
1059 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
1060 struct ifaddr *ifa;
1061 struct ifnet *ifp = newprc->ndprc_ifp;
1062 struct nd_prefix *pr;
1063 int s = splsoftnet();
1064 int error = 0;
1065 int auth;
1066 struct in6_addrlifetime lt6_tmp;
1067
1068 auth = 0;
1069 if (m) {
1070 /*
1071 * Authenticity for NA consists authentication for
1072 * both IP header and IP datagrams, doesn't it ?
1073 */
1074 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
1075 auth = (m->m_flags & M_AUTHIPHDR
1076 && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
1077 #endif
1078 }
1079
1080 if ((pr = nd6_prefix_lookup(newprc)) != NULL) {
1081 /*
1082 * nd6_prefix_lookup() ensures that pr and newprc have the same
1083 * prefix on a same interface.
1084 */
1085
1086 /*
1087 * Update prefix information. Note that the on-link (L) bit
1088 * and the autonomous (A) bit should NOT be changed from 1
1089 * to 0.
1090 */
1091 if (newprc->ndprc_raf_onlink == 1)
1092 pr->ndpr_raf_onlink = 1;
1093 if (newprc->ndprc_raf_auto == 1)
1094 pr->ndpr_raf_auto = 1;
1095 if (newprc->ndprc_raf_onlink) {
1096 pr->ndpr_vltime = newprc->ndprc_vltime;
1097 pr->ndpr_pltime = newprc->ndprc_pltime;
1098 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
1099 pr->ndpr_lastupdate = time_uptime;
1100 }
1101
1102 if (newprc->ndprc_raf_onlink &&
1103 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1104 int e;
1105
1106 if ((e = nd6_prefix_onlink(pr)) != 0) {
1107 nd6log(LOG_ERR,
1108 "failed to make "
1109 "the prefix %s/%d on-link on %s "
1110 "(errno=%d)\n",
1111 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1112 pr->ndpr_plen, if_name(pr->ndpr_ifp), e);
1113 /* proceed anyway. XXX: is it correct? */
1114 }
1115 }
1116
1117 if (dr && pfxrtr_lookup(pr, dr) == NULL)
1118 pfxrtr_add(pr, dr);
1119 } else {
1120 struct nd_prefix *newpr = NULL;
1121
1122 if (newprc->ndprc_vltime == 0)
1123 goto end;
1124 if (newprc->ndprc_raf_onlink == 0 && newprc->ndprc_raf_auto == 0)
1125 goto end;
1126
1127 if (ip6_rtadv_maxroutes <= nd6_numroutes) {
1128 ICMP6_STATINC(ICMP6_STAT_DROPPED_RAROUTE);
1129 goto end;
1130 }
1131
1132 error = nd6_prelist_add(newprc, dr, &newpr);
1133 if (error != 0 || newpr == NULL) {
1134 nd6log(LOG_NOTICE,
1135 "nd6_prelist_add failed for %s/%d on %s "
1136 "errno=%d, returnpr=%p\n",
1137 ip6_sprintf(&newprc->ndprc_prefix.sin6_addr),
1138 newprc->ndprc_plen, if_name(newprc->ndprc_ifp),
1139 error, newpr);
1140 goto end; /* we should just give up in this case. */
1141 }
1142
1143 /*
1144 * XXX: from the ND point of view, we can ignore a prefix
1145 * with the on-link bit being zero. However, we need a
1146 * prefix structure for references from autoconfigured
1147 * addresses. Thus, we explicitly make sure that the prefix
1148 * itself expires now.
1149 */
1150 if (newpr->ndpr_raf_onlink == 0) {
1151 newpr->ndpr_vltime = 0;
1152 newpr->ndpr_pltime = 0;
1153 in6_init_prefix_ltimes(newpr);
1154 }
1155
1156 pr = newpr;
1157 }
1158
1159 /*
1160 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
1161 * Note that pr must be non NULL at this point.
1162 */
1163
1164 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
1165 if (!newprc->ndprc_raf_auto)
1166 goto end;
1167
1168 /*
1169 * 5.5.3 (b). the link-local prefix should have been ignored in
1170 * nd6_ra_input.
1171 */
1172
1173 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1174 if (newprc->ndprc_pltime > newprc->ndprc_vltime) {
1175 error = EINVAL; /* XXX: won't be used */
1176 goto end;
1177 }
1178
1179 /*
1180 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
1181 * an address configured by stateless autoconfiguration already in the
1182 * list of addresses associated with the interface, and the Valid
1183 * Lifetime is not 0, form an address. We first check if we have
1184 * a matching prefix.
1185 * Note: we apply a clarification in rfc2462bis-02 here. We only
1186 * consider autoconfigured addresses while RFC2462 simply said
1187 * "address".
1188 */
1189 IFADDR_FOREACH(ifa, ifp) {
1190 struct in6_ifaddr *ifa6;
1191 u_int32_t remaininglifetime;
1192
1193 if (ifa->ifa_addr->sa_family != AF_INET6)
1194 continue;
1195
1196 ifa6 = (struct in6_ifaddr *)ifa;
1197
1198 /*
1199 * We only consider autoconfigured addresses as per rfc2462bis.
1200 */
1201 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1202 continue;
1203
1204 /*
1205 * Spec is not clear here, but I believe we should concentrate
1206 * on unicast (i.e. not anycast) addresses.
1207 * XXX: other ia6_flags? detached or duplicated?
1208 */
1209 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1210 continue;
1211
1212 /*
1213 * Ignore the address if it is not associated with a prefix
1214 * or is associated with a prefix that is different from this
1215 * one. (pr is never NULL here)
1216 */
1217 if (ifa6->ia6_ndpr != pr)
1218 continue;
1219
1220 if (ia6_match == NULL) /* remember the first one */
1221 ia6_match = ifa6;
1222
1223 /*
1224 * An already autoconfigured address matched. Now that we
1225 * are sure there is at least one matched address, we can
1226 * proceed to 5.5.3. (e): update the lifetimes according to the
1227 * "two hours" rule and the privacy extension.
1228 * We apply some clarifications in rfc2462bis:
1229 * - use remaininglifetime instead of storedlifetime as a
1230 * variable name
1231 * - remove the dead code in the "two-hour" rule
1232 */
1233 #define TWOHOUR (120*60)
1234 lt6_tmp = ifa6->ia6_lifetime;
1235 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1236 remaininglifetime = ND6_INFINITE_LIFETIME;
1237 else if (time_uptime - ifa6->ia6_updatetime >
1238 lt6_tmp.ia6t_vltime) {
1239 /*
1240 * The case of "invalid" address. We should usually
1241 * not see this case.
1242 */
1243 remaininglifetime = 0;
1244 } else
1245 remaininglifetime = lt6_tmp.ia6t_vltime -
1246 (time_uptime - ifa6->ia6_updatetime);
1247
1248 /* when not updating, keep the current stored lifetime. */
1249 lt6_tmp.ia6t_vltime = remaininglifetime;
1250
1251 if (TWOHOUR < newprc->ndprc_vltime ||
1252 remaininglifetime < newprc->ndprc_vltime) {
1253 lt6_tmp.ia6t_vltime = newprc->ndprc_vltime;
1254 } else if (remaininglifetime <= TWOHOUR) {
1255 if (auth)
1256 lt6_tmp.ia6t_vltime = newprc->ndprc_vltime;
1257 } else {
1258 /*
1259 * newprc->ndprc_vltime <= TWOHOUR &&
1260 * TWOHOUR < remaininglifetime
1261 */
1262 lt6_tmp.ia6t_vltime = TWOHOUR;
1263 }
1264
1265 /* The 2 hour rule is not imposed for preferred lifetime. */
1266 lt6_tmp.ia6t_pltime = newprc->ndprc_pltime;
1267
1268 in6_init_address_ltimes(pr, <6_tmp);
1269
1270 /*
1271 * We need to treat lifetimes for temporary addresses
1272 * differently, according to
1273 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1274 * we only update the lifetimes when they are in the maximum
1275 * intervals.
1276 */
1277 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1278 u_int32_t maxvltime, maxpltime;
1279
1280 if (ip6_temp_valid_lifetime >
1281 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1282 ip6_desync_factor)) {
1283 maxvltime = ip6_temp_valid_lifetime -
1284 (time_uptime - ifa6->ia6_createtime) -
1285 ip6_desync_factor;
1286 } else
1287 maxvltime = 0;
1288 if (ip6_temp_preferred_lifetime >
1289 (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1290 ip6_desync_factor)) {
1291 maxpltime = ip6_temp_preferred_lifetime -
1292 (time_uptime - ifa6->ia6_createtime) -
1293 ip6_desync_factor;
1294 } else
1295 maxpltime = 0;
1296
1297 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1298 lt6_tmp.ia6t_vltime > maxvltime) {
1299 lt6_tmp.ia6t_vltime = maxvltime;
1300 }
1301 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1302 lt6_tmp.ia6t_pltime > maxpltime) {
1303 lt6_tmp.ia6t_pltime = maxpltime;
1304 }
1305 }
1306
1307 ifa6->ia6_lifetime = lt6_tmp;
1308 ifa6->ia6_updatetime = time_uptime;
1309 }
1310 if (ia6_match == NULL && newprc->ndprc_vltime) {
1311 int ifidlen;
1312
1313 /*
1314 * 5.5.3 (d) (continued)
1315 * No address matched and the valid lifetime is non-zero.
1316 * Create a new address.
1317 */
1318
1319 /*
1320 * Prefix Length check:
1321 * If the sum of the prefix length and interface identifier
1322 * length does not equal 128 bits, the Prefix Information
1323 * option MUST be ignored. The length of the interface
1324 * identifier is defined in a separate link-type specific
1325 * document.
1326 */
1327 ifidlen = in6_if2idlen(ifp);
1328 if (ifidlen < 0) {
1329 /* this should not happen, so we always log it. */
1330 log(LOG_ERR, "%s: IFID undefined (%s)\n",
1331 __func__, if_name(ifp));
1332 goto end;
1333 }
1334 if (ifidlen + pr->ndpr_plen != 128) {
1335 nd6log(LOG_INFO,
1336 "invalid prefixlen %d for %s, ignored\n",
1337 pr->ndpr_plen, if_name(ifp));
1338 goto end;
1339 }
1340
1341 if ((ia6 = in6_ifadd(newprc, mcast)) != NULL) {
1342 /*
1343 * note that we should use pr (not newprc) for reference.
1344 */
1345 pr->ndpr_refcnt++;
1346 ia6->ia6_ndpr = pr;
1347
1348 /*
1349 * draft-ietf-ipngwg-temp-addresses-v2-00 3.3 (2).
1350 * When a new public address is created as described
1351 * in RFC2462, also create a new temporary address.
1352 *
1353 * draft-ietf-ipngwg-temp-addresses-v2-00 3.5.
1354 * When an interface connects to a new link, a new
1355 * randomized interface identifier should be generated
1356 * immediately together with a new set of temporary
1357 * addresses. Thus, we specifiy 1 as the 2nd arg of
1358 * in6_tmpifadd().
1359 */
1360 if (ip6_use_tempaddr) {
1361 int e;
1362 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1363 nd6log(LOG_NOTICE,
1364 "failed to create a temporary "
1365 "address, errno=%d\n", e);
1366 }
1367 }
1368
1369 /*
1370 * A newly added address might affect the status
1371 * of other addresses, so we check and update it.
1372 * XXX: what if address duplication happens?
1373 */
1374 pfxlist_onlink_check();
1375 } else {
1376 /* just set an error. do not bark here. */
1377 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1378 }
1379 }
1380
1381 end:
1382 splx(s);
1383 return error;
1384 }
1385
1386 /*
1387 * A supplement function used in the on-link detection below;
1388 * detect if a given prefix has a (probably) reachable advertising router.
1389 * XXX: lengthy function name...
1390 */
1391 static struct nd_pfxrouter *
1392 find_pfxlist_reachable_router(struct nd_prefix *pr)
1393 {
1394 struct nd_pfxrouter *pfxrtr;
1395
1396 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1397 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1398 if (pfxrtr->router->ifp->if_flags & IFF_UP &&
1399 pfxrtr->router->ifp->if_link_state != LINK_STATE_DOWN &&
1400 nd6_is_llinfo_probreach(pfxrtr->router))
1401 break; /* found */
1402 }
1403
1404 return (pfxrtr);
1405 }
1406
1407 /*
1408 * Check if each prefix in the prefix list has at least one available router
1409 * that advertised the prefix (a router is "available" if its neighbor cache
1410 * entry is reachable or probably reachable).
1411 * If the check fails, the prefix may be off-link, because, for example,
1412 * we have moved from the network but the lifetime of the prefix has not
1413 * expired yet. So we should not use the prefix if there is another prefix
1414 * that has an available router.
1415 * But, if there is no prefix that has an available router, we still regards
1416 * all the prefixes as on-link. This is because we can't tell if all the
1417 * routers are simply dead or if we really moved from the network and there
1418 * is no router around us.
1419 */
1420 void
1421 pfxlist_onlink_check(void)
1422 {
1423 struct nd_prefix *pr;
1424 struct in6_ifaddr *ifa;
1425 struct nd_defrouter *dr;
1426 struct nd_pfxrouter *pfxrtr = NULL;
1427
1428 /*
1429 * Check if there is a prefix that has a reachable advertising
1430 * router.
1431 */
1432 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1433 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1434 break;
1435 }
1436 /*
1437 * If we have no such prefix, check whether we still have a router
1438 * that does not advertise any prefixes.
1439 */
1440 if (pr == NULL) {
1441 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1442 struct nd_prefix *pr0;
1443
1444 LIST_FOREACH(pr0, &nd_prefix, ndpr_entry) {
1445 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1446 break;
1447 }
1448 if (pfxrtr)
1449 break;
1450 }
1451 }
1452 if (pr != NULL || (TAILQ_FIRST(&nd_defrouter) && !pfxrtr)) {
1453 /*
1454 * There is at least one prefix that has a reachable router,
1455 * or at least a router which probably does not advertise
1456 * any prefixes. The latter would be the case when we move
1457 * to a new link where we have a router that does not provide
1458 * prefixes and we configure an address by hand.
1459 * Detach prefixes which have no reachable advertising
1460 * router, and attach other prefixes.
1461 */
1462 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1463 /* XXX: a link-local prefix should never be detached */
1464 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1465 continue;
1466
1467 /*
1468 * we aren't interested in prefixes without the L bit
1469 * set.
1470 */
1471 if (pr->ndpr_raf_onlink == 0)
1472 continue;
1473
1474 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1475 find_pfxlist_reachable_router(pr) == NULL)
1476 pr->ndpr_stateflags |= NDPRF_DETACHED;
1477 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1478 find_pfxlist_reachable_router(pr) != 0)
1479 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1480 }
1481 } else {
1482 /* there is no prefix that has a reachable router */
1483 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1484 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1485 continue;
1486
1487 if (pr->ndpr_raf_onlink == 0)
1488 continue;
1489
1490 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1491 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1492 }
1493 }
1494
1495 /*
1496 * Remove each interface route associated with a (just) detached
1497 * prefix, and reinstall the interface route for a (just) attached
1498 * prefix. Note that all attempt of reinstallation does not
1499 * necessarily success, when a same prefix is shared among multiple
1500 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1501 * so we don't have to care about them.
1502 */
1503 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1504 int e;
1505
1506 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1507 continue;
1508
1509 if (pr->ndpr_raf_onlink == 0)
1510 continue;
1511
1512 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1513 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1514 if ((e = nd6_prefix_offlink(pr)) != 0) {
1515 nd6log(LOG_ERR,
1516 "failed to make %s/%d offlink, errno=%d\n",
1517 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1518 pr->ndpr_plen, e);
1519 }
1520 }
1521 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1522 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1523 pr->ndpr_raf_onlink) {
1524 if ((e = nd6_prefix_onlink(pr)) != 0) {
1525 nd6log(LOG_ERR,
1526 "failed to make %s/%d onlink, errno=%d\n",
1527 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1528 pr->ndpr_plen, e);
1529 }
1530 }
1531 }
1532
1533 /*
1534 * Changes on the prefix status might affect address status as well.
1535 * Make sure that all addresses derived from an attached prefix are
1536 * attached, and that all addresses derived from a detached prefix are
1537 * detached. Note, however, that a manually configured address should
1538 * always be attached.
1539 * The precise detection logic is same as the one for prefixes.
1540 */
1541 IN6_ADDRLIST_READER_FOREACH(ifa) {
1542 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1543 continue;
1544
1545 if (ifa->ia6_ndpr == NULL) {
1546 /*
1547 * This can happen when we first configure the address
1548 * (i.e. the address exists, but the prefix does not).
1549 * XXX: complicated relationships...
1550 */
1551 continue;
1552 }
1553
1554 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1555 break;
1556 }
1557 if (ifa) {
1558 IN6_ADDRLIST_READER_FOREACH(ifa) {
1559 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1560 continue;
1561
1562 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1563 continue;
1564
1565 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1566 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1567 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1568 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1569 nd6_dad_start((struct ifaddr *)ifa,
1570 0);
1571 /* We will notify the routing socket
1572 * of the DAD result, so no need to
1573 * here */
1574 }
1575 } else {
1576 if ((ifa->ia6_flags & IN6_IFF_DETACHED) == 0) {
1577 ifa->ia6_flags |= IN6_IFF_DETACHED;
1578 rt_newaddrmsg(RTM_NEWADDR,
1579 (struct ifaddr *)ifa, 0, NULL);
1580 }
1581 }
1582 }
1583 }
1584 else {
1585 IN6_ADDRLIST_READER_FOREACH(ifa) {
1586 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1587 continue;
1588
1589 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1590 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1591 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1592 /* Do we need a delay in this case? */
1593 nd6_dad_start((struct ifaddr *)ifa, 0);
1594 }
1595 }
1596 }
1597 }
1598
1599 int
1600 nd6_prefix_onlink(struct nd_prefix *pr)
1601 {
1602 struct ifaddr *ifa;
1603 struct ifnet *ifp = pr->ndpr_ifp;
1604 struct sockaddr_in6 mask6;
1605 struct nd_prefix *opr;
1606 u_long rtflags;
1607 int error = 0;
1608
1609 /* sanity check */
1610 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1611 nd6log(LOG_ERR, "%s/%d is already on-link\n",
1612 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
1613 return (EEXIST);
1614 }
1615
1616 /*
1617 * Add the interface route associated with the prefix. Before
1618 * installing the route, check if there's the same prefix on another
1619 * interface, and the prefix has already installed the interface route.
1620 * Although such a configuration is expected to be rare, we explicitly
1621 * allow it.
1622 */
1623 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
1624 if (opr == pr)
1625 continue;
1626
1627 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1628 continue;
1629
1630 if (opr->ndpr_plen == pr->ndpr_plen &&
1631 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1632 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1633 return (0);
1634 }
1635
1636 /*
1637 * We prefer link-local addresses as the associated interface address.
1638 */
1639 /* search for a link-local addr */
1640 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1641 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1642 if (ifa == NULL) {
1643 /* XXX: freebsd does not have ifa_ifwithaf */
1644 IFADDR_FOREACH(ifa, ifp) {
1645 if (ifa->ifa_addr->sa_family == AF_INET6)
1646 break;
1647 }
1648 /* should we care about ia6_flags? */
1649 }
1650 if (ifa == NULL) {
1651 /*
1652 * This can still happen, when, for example, we receive an RA
1653 * containing a prefix with the L bit set and the A bit clear,
1654 * after removing all IPv6 addresses on the receiving
1655 * interface. This should, of course, be rare though.
1656 */
1657 nd6log(LOG_NOTICE, "failed to find any ifaddr"
1658 " to add route for a prefix(%s/%d) on %s\n",
1659 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1660 pr->ndpr_plen, if_name(ifp));
1661 return (0);
1662 }
1663
1664 /*
1665 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1666 * ifa->ifa_rtrequest = nd6_rtrequest;
1667 */
1668 memset(&mask6, 0, sizeof(mask6));
1669 mask6.sin6_family = AF_INET6;
1670 mask6.sin6_len = sizeof(mask6);
1671 mask6.sin6_addr = pr->ndpr_mask;
1672 /* rtrequest() will probably set RTF_UP, but we're not sure. */
1673 rtflags = ifa->ifa_flags | RTF_UP;
1674 if (nd6_need_cache(ifp)) {
1675 /* explicitly set in case ifa_flags does not set the flag. */
1676 rtflags |= RTF_CONNECTED;
1677 } else {
1678 /*
1679 * explicitly clear the cloning bit in case ifa_flags sets it.
1680 */
1681 rtflags &= ~RTF_CONNECTED;
1682 }
1683 error = rtrequest_newmsg(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1684 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags);
1685 if (error == 0) {
1686 nd6_numroutes++;
1687 pr->ndpr_stateflags |= NDPRF_ONLINK;
1688 } else {
1689 nd6log(LOG_ERR, "failed to add route for a"
1690 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1691 "errno = %d\n",
1692 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1693 pr->ndpr_plen, if_name(ifp),
1694 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1695 ip6_sprintf(&mask6.sin6_addr), rtflags, error);
1696 }
1697
1698 return (error);
1699 }
1700
1701 int
1702 nd6_prefix_offlink(struct nd_prefix *pr)
1703 {
1704 int error = 0;
1705 struct ifnet *ifp = pr->ndpr_ifp;
1706 struct nd_prefix *opr;
1707 struct sockaddr_in6 sa6, mask6;
1708
1709 /* sanity check */
1710 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1711 nd6log(LOG_ERR, "%s/%d is already off-link\n",
1712 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
1713 return (EEXIST);
1714 }
1715
1716 sockaddr_in6_init(&sa6, &pr->ndpr_prefix.sin6_addr, 0, 0, 0);
1717 sockaddr_in6_init(&mask6, &pr->ndpr_mask, 0, 0, 0);
1718 error = rtrequest_newmsg(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1719 (struct sockaddr *)&mask6, 0);
1720 if (error == 0) {
1721 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1722 nd6_numroutes--;
1723
1724 /*
1725 * There might be the same prefix on another interface,
1726 * the prefix which could not be on-link just because we have
1727 * the interface route (see comments in nd6_prefix_onlink).
1728 * If there's one, try to make the prefix on-link on the
1729 * interface.
1730 */
1731 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
1732 if (opr == pr)
1733 continue;
1734
1735 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1736 continue;
1737
1738 /*
1739 * KAME specific: detached prefixes should not be
1740 * on-link.
1741 */
1742 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1743 continue;
1744
1745 if (opr->ndpr_plen == pr->ndpr_plen &&
1746 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1747 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1748 int e;
1749
1750 if ((e = nd6_prefix_onlink(opr)) != 0) {
1751 nd6log(LOG_ERR, "failed to "
1752 "recover a prefix %s/%d from %s "
1753 "to %s (errno = %d)\n",
1754 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1755 opr->ndpr_plen, if_name(ifp),
1756 if_name(opr->ndpr_ifp), e);
1757 }
1758 }
1759 }
1760 } else {
1761 /* XXX: can we still set the NDPRF_ONLINK flag? */
1762 nd6log(LOG_ERR, "failed to delete route: "
1763 "%s/%d on %s (errno = %d)\n",
1764 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
1765 error);
1766 }
1767
1768 return error;
1769 }
1770
1771 static struct in6_ifaddr *
1772 in6_ifadd(struct nd_prefixctl *prc, int mcast)
1773 {
1774 struct ifnet *ifp = prc->ndprc_ifp;
1775 struct ifaddr *ifa;
1776 struct in6_aliasreq ifra;
1777 struct in6_ifaddr *ia, *ib;
1778 int error, plen0;
1779 struct in6_addr mask;
1780 int prefixlen = prc->ndprc_plen;
1781 int updateflags;
1782
1783 in6_prefixlen2mask(&mask, prefixlen);
1784
1785 /*
1786 * find a link-local address (will be interface ID).
1787 * Is it really mandatory? Theoretically, a global or a site-local
1788 * address can be configured without a link-local address, if we
1789 * have a unique interface identifier...
1790 *
1791 * it is not mandatory to have a link-local address, we can generate
1792 * interface identifier on the fly. we do this because:
1793 * (1) it should be the easiest way to find interface identifier.
1794 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1795 * for multiple addresses on a single interface, and possible shortcut
1796 * of DAD. we omitted DAD for this reason in the past.
1797 * (3) a user can prevent autoconfiguration of global address
1798 * by removing link-local address by hand (this is partly because we
1799 * don't have other way to control the use of IPv6 on an interface.
1800 * this has been our design choice - cf. NRL's "ifconfig auto").
1801 * (4) it is easier to manage when an interface has addresses
1802 * with the same interface identifier, than to have multiple addresses
1803 * with different interface identifiers.
1804 */
1805 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1806 if (ifa)
1807 ib = (struct in6_ifaddr *)ifa;
1808 else
1809 return NULL;
1810
1811 #if 0 /* don't care link local addr state, and always do DAD */
1812 /* if link-local address is not eligible, do not autoconfigure. */
1813 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1814 printf("in6_ifadd: link-local address not ready\n");
1815 return NULL;
1816 }
1817 #endif
1818
1819 /* prefixlen + ifidlen must be equal to 128 */
1820 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1821 if (prefixlen != plen0) {
1822 nd6log(LOG_INFO, "wrong prefixlen for %s "
1823 "(prefix=%d ifid=%d)\n",
1824 if_name(ifp), prefixlen, 128 - plen0);
1825 return NULL;
1826 }
1827
1828 /* make ifaddr */
1829
1830 memset(&ifra, 0, sizeof(ifra));
1831 /*
1832 * in6_update_ifa() does not use ifra_name, but we accurately set it
1833 * for safety.
1834 */
1835 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1836 sockaddr_in6_init(&ifra.ifra_addr, &prc->ndprc_prefix.sin6_addr, 0, 0, 0);
1837 /* prefix */
1838 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1839 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1840 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1841 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1842
1843 /* interface ID */
1844 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1845 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1846 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1847 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1848 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1849 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1850 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1851 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1852
1853 /* new prefix mask. */
1854 sockaddr_in6_init(&ifra.ifra_prefixmask, &mask, 0, 0, 0);
1855
1856 /* lifetimes */
1857 ifra.ifra_lifetime.ia6t_vltime = prc->ndprc_vltime;
1858 ifra.ifra_lifetime.ia6t_pltime = prc->ndprc_pltime;
1859
1860 /* XXX: scope zone ID? */
1861
1862 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1863
1864 /*
1865 * Make sure that we do not have this address already. This should
1866 * usually not happen, but we can still see this case, e.g., if we
1867 * have manually configured the exact address to be configured.
1868 */
1869 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
1870 /* this should be rare enough to make an explicit log */
1871 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1872 ip6_sprintf(&ifra.ifra_addr.sin6_addr));
1873 return (NULL);
1874 }
1875
1876 /*
1877 * Allocate ifaddr structure, link into chain, etc.
1878 * If we are going to create a new address upon receiving a multicasted
1879 * RA, we need to impose a random delay before starting DAD.
1880 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1881 */
1882 updateflags = 0;
1883 if (mcast)
1884 updateflags |= IN6_IFAUPDATE_DADDELAY;
1885 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1886 nd6log(LOG_ERR, "failed to make ifaddr %s on %s (errno=%d)\n",
1887 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
1888 error);
1889 return (NULL); /* ifaddr must not have been allocated. */
1890 }
1891
1892 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1893
1894 return (ia); /* this is always non-NULL */
1895 }
1896
1897 int
1898 in6_tmpifadd(
1899 const struct in6_ifaddr *ia0, /* corresponding public address */
1900 int forcegen,
1901 int dad_delay)
1902 {
1903 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1904 struct in6_ifaddr *newia, *ia;
1905 struct in6_aliasreq ifra;
1906 int i, error;
1907 int trylimit = 3; /* XXX: adhoc value */
1908 int updateflags;
1909 u_int32_t randid[2];
1910 u_int32_t vltime0, pltime0;
1911
1912 memset(&ifra, 0, sizeof(ifra));
1913 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1914 ifra.ifra_addr = ia0->ia_addr;
1915 /* copy prefix mask */
1916 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1917 /* clear the old IFID */
1918 for (i = 0; i < 4; i++) {
1919 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1920 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1921 }
1922
1923 again:
1924 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1925 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1926 nd6log(LOG_NOTICE, "failed to find a good random IFID\n");
1927 return (EINVAL);
1928 }
1929 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1930 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1931 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1932 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1933
1934 /*
1935 * in6_get_tmpifid() quite likely provided a unique interface ID.
1936 * However, we may still have a chance to see collision, because
1937 * there may be a time lag between generation of the ID and generation
1938 * of the address. So, we'll do one more sanity check.
1939 */
1940 IN6_ADDRLIST_READER_FOREACH(ia) {
1941 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1942 &ifra.ifra_addr.sin6_addr)) {
1943 if (trylimit-- == 0) {
1944 /*
1945 * Give up. Something strange should have
1946 * happened.
1947 */
1948 nd6log(LOG_NOTICE,
1949 "failed to find a unique random IFID\n");
1950 return (EEXIST);
1951 }
1952 forcegen = 1;
1953 goto again;
1954 }
1955 }
1956
1957 /*
1958 * The Valid Lifetime is the lower of the Valid Lifetime of the
1959 * public address or TEMP_VALID_LIFETIME.
1960 * The Preferred Lifetime is the lower of the Preferred Lifetime
1961 * of the public address or TEMP_PREFERRED_LIFETIME -
1962 * DESYNC_FACTOR.
1963 */
1964 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1965 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1966 (ia0->ia6_lifetime.ia6t_vltime -
1967 (time_uptime - ia0->ia6_updatetime));
1968 if (vltime0 > ip6_temp_valid_lifetime)
1969 vltime0 = ip6_temp_valid_lifetime;
1970 } else
1971 vltime0 = ip6_temp_valid_lifetime;
1972 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1973 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
1974 (ia0->ia6_lifetime.ia6t_pltime -
1975 (time_uptime - ia0->ia6_updatetime));
1976 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){
1977 pltime0 = ip6_temp_preferred_lifetime -
1978 ip6_desync_factor;
1979 }
1980 } else
1981 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
1982 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1983 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1984
1985 /*
1986 * A temporary address is created only if this calculated Preferred
1987 * Lifetime is greater than REGEN_ADVANCE time units.
1988 */
1989 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
1990 return (0);
1991
1992 /* XXX: scope zone ID? */
1993
1994 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
1995
1996 /* allocate ifaddr structure, link into chain, etc. */
1997 updateflags = 0;
1998 if (dad_delay)
1999 updateflags |= IN6_IFAUPDATE_DADDELAY;
2000 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
2001 return (error);
2002
2003 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
2004 if (newia == NULL) { /* XXX: can it happen? */
2005 nd6log(LOG_ERR,
2006 "ifa update succeeded, but we got no ifaddr\n");
2007 return (EINVAL); /* XXX */
2008 }
2009 newia->ia6_ndpr = ia0->ia6_ndpr;
2010 newia->ia6_ndpr->ndpr_refcnt++;
2011
2012 /*
2013 * A newly added address might affect the status of other addresses.
2014 * XXX: when the temporary address is generated with a new public
2015 * address, the onlink check is redundant. However, it would be safe
2016 * to do the check explicitly everywhere a new address is generated,
2017 * and, in fact, we surely need the check when we create a new
2018 * temporary address due to deprecation of an old temporary address.
2019 */
2020 pfxlist_onlink_check();
2021
2022 return (0);
2023 }
2024
2025 static int
2026 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2027 {
2028
2029 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
2030 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
2031 nd6log(LOG_INFO, "preferred lifetime"
2032 "(%d) is greater than valid lifetime(%d)\n",
2033 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime);
2034 return (EINVAL);
2035 }
2036 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2037 ndpr->ndpr_preferred = 0;
2038 else
2039 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
2040 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2041 ndpr->ndpr_expire = 0;
2042 else
2043 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
2044
2045 return 0;
2046 }
2047
2048 static void
2049 in6_init_address_ltimes(struct nd_prefix *newpr,
2050 struct in6_addrlifetime *lt6)
2051 {
2052
2053 /* Valid lifetime must not be updated unless explicitly specified. */
2054 /* init ia6t_expire */
2055 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2056 lt6->ia6t_expire = 0;
2057 else {
2058 lt6->ia6t_expire = time_uptime;
2059 lt6->ia6t_expire += lt6->ia6t_vltime;
2060 }
2061
2062 /* init ia6t_preferred */
2063 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2064 lt6->ia6t_preferred = 0;
2065 else {
2066 lt6->ia6t_preferred = time_uptime;
2067 lt6->ia6t_preferred += lt6->ia6t_pltime;
2068 }
2069 }
2070
2071 /*
2072 * Delete all the routing table entries that use the specified gateway.
2073 * XXX: this function causes search through all entries of routing table, so
2074 * it shouldn't be called when acting as a router.
2075 */
2076 void
2077 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2078 {
2079 int s = splsoftnet();
2080
2081 /* We'll care only link-local addresses */
2082 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
2083 splx(s);
2084 return;
2085 }
2086
2087 rt_walktree(AF_INET6, rt6_deleteroute, (void *)gateway);
2088 splx(s);
2089 }
2090
2091 static int
2092 rt6_deleteroute(struct rtentry *rt, void *arg)
2093 {
2094 struct in6_addr *gate = (struct in6_addr *)arg;
2095
2096 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2097 return (0);
2098
2099 if (!IN6_ARE_ADDR_EQUAL(gate, &satosin6(rt->rt_gateway)->sin6_addr))
2100 return (0);
2101
2102 /*
2103 * Do not delete a static route.
2104 * XXX: this seems to be a bit ad-hoc. Should we consider the
2105 * 'cloned' bit instead?
2106 */
2107 if ((rt->rt_flags & RTF_STATIC) != 0)
2108 return (0);
2109
2110 /*
2111 * We delete only host route. This means, in particular, we don't
2112 * delete default route.
2113 */
2114 if ((rt->rt_flags & RTF_HOST) == 0)
2115 return (0);
2116
2117 return (rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
2118 rt_mask(rt), rt->rt_flags, NULL));
2119 }
2120
2121 int
2122 nd6_setdefaultiface(int ifindex)
2123 {
2124 ifnet_t *ifp;
2125 int error = 0;
2126 int s;
2127
2128 s = pserialize_read_enter();
2129 ifp = if_byindex(ifindex);
2130 if (ifp == NULL) {
2131 pserialize_read_exit(s);
2132 return EINVAL;
2133 }
2134 if (nd6_defifindex != ifindex) {
2135 nd6_defifindex = ifindex;
2136 nd6_defifp = nd6_defifindex > 0 ? ifp : NULL;
2137
2138 /*
2139 * Our current implementation assumes one-to-one maping between
2140 * interfaces and links, so it would be natural to use the
2141 * default interface as the default link.
2142 */
2143 scope6_setdefault(nd6_defifp);
2144 }
2145 pserialize_read_exit(s);
2146
2147 return (error);
2148 }
2149