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