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