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