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