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