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