1 /* $NetBSD: nd6_rtr.c,v 1.62 2006/11/20 04:34:16 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.62 2006/11/20 04:34:16 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 #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_second + 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 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
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 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
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 LIST_FOREACH(search, &nd_prefix, ndpr_entry) {
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_second;
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 = LIST_FIRST(&pr->ndpr_advrtrs); pfr != NULL; pfr = next) {
988 next = LIST_NEXT(pfr, pfr_entry);
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_second;
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 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1135 struct in6_ifaddr *ifa6;
1136 u_int32_t remaininglifetime;
1137
1138 if (ifa->ifa_addr->sa_family != AF_INET6)
1139 continue;
1140
1141 ifa6 = (struct in6_ifaddr *)ifa;
1142
1143 /*
1144 * We only consider autoconfigured addresses as per rfc2462bis.
1145 */
1146 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1147 continue;
1148
1149 /*
1150 * Spec is not clear here, but I believe we should concentrate
1151 * on unicast (i.e. not anycast) addresses.
1152 * XXX: other ia6_flags? detached or duplicated?
1153 */
1154 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
1155 continue;
1156
1157 /*
1158 * Ignore the address if it is not associated with a prefix
1159 * or is associated with a prefix that is different from this
1160 * one. (pr is never NULL here)
1161 */
1162 if (ifa6->ia6_ndpr != pr)
1163 continue;
1164
1165 if (ia6_match == NULL) /* remember the first one */
1166 ia6_match = ifa6;
1167
1168 /*
1169 * An already autoconfigured address matched. Now that we
1170 * are sure there is at least one matched address, we can
1171 * proceed to 5.5.3. (e): update the lifetimes according to the
1172 * "two hours" rule and the privacy extension.
1173 * We apply some clarifications in rfc2462bis:
1174 * - use remaininglifetime instead of storedlifetime as a
1175 * variable name
1176 * - remove the dead code in the "two-hour" rule
1177 */
1178 #define TWOHOUR (120*60)
1179 lt6_tmp = ifa6->ia6_lifetime;
1180 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1181 remaininglifetime = ND6_INFINITE_LIFETIME;
1182 else if (time_second - ifa6->ia6_updatetime >
1183 lt6_tmp.ia6t_vltime) {
1184 /*
1185 * The case of "invalid" address. We should usually
1186 * not see this case.
1187 */
1188 remaininglifetime = 0;
1189 } else
1190 remaininglifetime = lt6_tmp.ia6t_vltime -
1191 (time_second - ifa6->ia6_updatetime);
1192
1193 /* when not updating, keep the current stored lifetime. */
1194 lt6_tmp.ia6t_vltime = remaininglifetime;
1195
1196 if (TWOHOUR < new->ndpr_vltime ||
1197 remaininglifetime < new->ndpr_vltime) {
1198 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1199 } else if (remaininglifetime <= TWOHOUR) {
1200 if (auth)
1201 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1202 } else {
1203 /*
1204 * new->ndpr_vltime <= TWOHOUR &&
1205 * TWOHOUR < remaininglifetime
1206 */
1207 lt6_tmp.ia6t_vltime = TWOHOUR;
1208 }
1209
1210 /* The 2 hour rule is not imposed for preferred lifetime. */
1211 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1212
1213 in6_init_address_ltimes(pr, <6_tmp);
1214
1215 /*
1216 * We need to treat lifetimes for temporary addresses
1217 * differently, according to
1218 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1219 * we only update the lifetimes when they are in the maximum
1220 * intervals.
1221 */
1222 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1223 u_int32_t maxvltime, maxpltime;
1224
1225 if (ip6_temp_valid_lifetime >
1226 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1227 ip6_desync_factor)) {
1228 maxvltime = ip6_temp_valid_lifetime -
1229 (time_second - ifa6->ia6_createtime) -
1230 ip6_desync_factor;
1231 } else
1232 maxvltime = 0;
1233 if (ip6_temp_preferred_lifetime >
1234 (u_int32_t)((time_second - ifa6->ia6_createtime) +
1235 ip6_desync_factor)) {
1236 maxpltime = ip6_temp_preferred_lifetime -
1237 (time_second - ifa6->ia6_createtime) -
1238 ip6_desync_factor;
1239 } else
1240 maxpltime = 0;
1241
1242 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1243 lt6_tmp.ia6t_vltime > maxvltime) {
1244 lt6_tmp.ia6t_vltime = maxvltime;
1245 }
1246 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1247 lt6_tmp.ia6t_pltime > maxpltime) {
1248 lt6_tmp.ia6t_pltime = maxpltime;
1249 }
1250 }
1251
1252 ifa6->ia6_lifetime = lt6_tmp;
1253 ifa6->ia6_updatetime = time_second;
1254 }
1255 if (ia6_match == NULL && new->ndpr_vltime) {
1256 int ifidlen;
1257
1258 /*
1259 * 5.5.3 (d) (continued)
1260 * No address matched and the valid lifetime is non-zero.
1261 * Create a new address.
1262 */
1263
1264 /*
1265 * Prefix Length check:
1266 * If the sum of the prefix length and interface identifier
1267 * length does not equal 128 bits, the Prefix Information
1268 * option MUST be ignored. The length of the interface
1269 * identifier is defined in a separate link-type specific
1270 * document.
1271 */
1272 ifidlen = in6_if2idlen(ifp);
1273 if (ifidlen < 0) {
1274 /* this should not happen, so we always log it. */
1275 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1276 if_name(ifp));
1277 goto end;
1278 }
1279 if (ifidlen + pr->ndpr_plen != 128) {
1280 nd6log((LOG_INFO,
1281 "prelist_update: invalid prefixlen "
1282 "%d for %s, ignored\n",
1283 pr->ndpr_plen, if_name(ifp)));
1284 goto end;
1285 }
1286
1287 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
1288 /*
1289 * note that we should use pr (not new) for reference.
1290 */
1291 pr->ndpr_refcnt++;
1292 ia6->ia6_ndpr = pr;
1293
1294 /*
1295 * draft-ietf-ipngwg-temp-addresses-v2-00 3.3 (2).
1296 * When a new public address is created as described
1297 * in RFC2462, also create a new temporary address.
1298 *
1299 * draft-ietf-ipngwg-temp-addresses-v2-00 3.5.
1300 * When an interface connects to a new link, a new
1301 * randomized interface identifier should be generated
1302 * immediately together with a new set of temporary
1303 * addresses. Thus, we specifiy 1 as the 2nd arg of
1304 * in6_tmpifadd().
1305 */
1306 if (ip6_use_tempaddr) {
1307 int e;
1308 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1309 nd6log((LOG_NOTICE, "prelist_update: "
1310 "failed to create a temporary "
1311 "address, errno=%d\n",
1312 e));
1313 }
1314 }
1315
1316 /*
1317 * A newly added address might affect the status
1318 * of other addresses, so we check and update it.
1319 * XXX: what if address duplication happens?
1320 */
1321 pfxlist_onlink_check();
1322 } else {
1323 /* just set an error. do not bark here. */
1324 error = EADDRNOTAVAIL; /* XXX: might be unused. */
1325 }
1326 }
1327
1328 end:
1329 splx(s);
1330 return error;
1331 }
1332
1333 /*
1334 * A supplement function used in the on-link detection below;
1335 * detect if a given prefix has a (probably) reachable advertising router.
1336 * XXX: lengthy function name...
1337 */
1338 static struct nd_pfxrouter *
1339 find_pfxlist_reachable_router(pr)
1340 struct nd_prefix *pr;
1341 {
1342 struct nd_pfxrouter *pfxrtr;
1343 struct rtentry *rt;
1344 struct llinfo_nd6 *ln;
1345
1346 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1347 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1348 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1349 pfxrtr->router->ifp)) &&
1350 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1351 ND6_IS_LLINFO_PROBREACH(ln))
1352 break; /* found */
1353 }
1354
1355 return (pfxrtr);
1356 }
1357
1358 /*
1359 * Check if each prefix in the prefix list has at least one available router
1360 * that advertised the prefix (a router is "available" if its neighbor cache
1361 * entry is reachable or probably reachable).
1362 * If the check fails, the prefix may be off-link, because, for example,
1363 * we have moved from the network but the lifetime of the prefix has not
1364 * expired yet. So we should not use the prefix if there is another prefix
1365 * that has an available router.
1366 * But, if there is no prefix that has an available router, we still regards
1367 * all the prefixes as on-link. This is because we can't tell if all the
1368 * routers are simply dead or if we really moved from the network and there
1369 * is no router around us.
1370 */
1371 void
1372 pfxlist_onlink_check()
1373 {
1374 struct nd_prefix *pr;
1375 struct in6_ifaddr *ifa;
1376 struct nd_defrouter *dr;
1377 struct nd_pfxrouter *pfxrtr = NULL;
1378
1379 /*
1380 * Check if there is a prefix that has a reachable advertising
1381 * router.
1382 */
1383 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1384 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
1385 break;
1386 }
1387 /*
1388 * If we have no such prefix, check whether we still have a router
1389 * that does not advertise any prefixes.
1390 */
1391 if (pr == NULL) {
1392 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1393 struct nd_prefix *pr0;
1394
1395 LIST_FOREACH(pr0, &nd_prefix, ndpr_entry) {
1396 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1397 break;
1398 }
1399 if (pfxrtr)
1400 break;
1401 }
1402 }
1403 if (pr != NULL || (TAILQ_FIRST(&nd_defrouter) && !pfxrtr)) {
1404 /*
1405 * There is at least one prefix that has a reachable router,
1406 * or at least a router which probably does not advertise
1407 * any prefixes. The latter would be the case when we move
1408 * to a new link where we have a router that does not provide
1409 * prefixes and we configure an address by hand.
1410 * Detach prefixes which have no reachable advertising
1411 * router, and attach other prefixes.
1412 */
1413 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1414 /* XXX: a link-local prefix should never be detached */
1415 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1416 continue;
1417
1418 /*
1419 * we aren't interested in prefixes without the L bit
1420 * set.
1421 */
1422 if (pr->ndpr_raf_onlink == 0)
1423 continue;
1424
1425 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1426 find_pfxlist_reachable_router(pr) == NULL)
1427 pr->ndpr_stateflags |= NDPRF_DETACHED;
1428 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1429 find_pfxlist_reachable_router(pr) != 0)
1430 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1431 }
1432 } else {
1433 /* there is no prefix that has a reachable router */
1434 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1435 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1436 continue;
1437
1438 if (pr->ndpr_raf_onlink == 0)
1439 continue;
1440
1441 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1442 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
1443 }
1444 }
1445
1446 /*
1447 * Remove each interface route associated with a (just) detached
1448 * prefix, and reinstall the interface route for a (just) attached
1449 * prefix. Note that all attempt of reinstallation does not
1450 * necessarily success, when a same prefix is shared among multiple
1451 * interfaces. Such cases will be handled in nd6_prefix_onlink,
1452 * so we don't have to care about them.
1453 */
1454 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1455 int e;
1456
1457 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1458 continue;
1459
1460 if (pr->ndpr_raf_onlink == 0)
1461 continue;
1462
1463 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1464 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1465 if ((e = nd6_prefix_offlink(pr)) != 0) {
1466 nd6log((LOG_ERR,
1467 "pfxlist_onlink_check: failed to "
1468 "make %s/%d offlink, errno=%d\n",
1469 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1470 pr->ndpr_plen, e));
1471 }
1472 }
1473 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
1474 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
1475 pr->ndpr_raf_onlink) {
1476 if ((e = nd6_prefix_onlink(pr)) != 0) {
1477 nd6log((LOG_ERR,
1478 "pfxlist_onlink_check: failed to "
1479 "make %s/%d onlink, errno=%d\n",
1480 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1481 pr->ndpr_plen, e));
1482 }
1483 }
1484 }
1485
1486 /*
1487 * Changes on the prefix status might affect address status as well.
1488 * Make sure that all addresses derived from an attached prefix are
1489 * attached, and that all addresses derived from a detached prefix are
1490 * detached. Note, however, that a manually configured address should
1491 * always be attached.
1492 * The precise detection logic is same as the one for prefixes.
1493 */
1494 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1495 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
1496 continue;
1497
1498 if (ifa->ia6_ndpr == NULL) {
1499 /*
1500 * This can happen when we first configure the address
1501 * (i.e. the address exists, but the prefix does not).
1502 * XXX: complicated relationships...
1503 */
1504 continue;
1505 }
1506
1507 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
1508 break;
1509 }
1510 if (ifa) {
1511 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1512 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1513 continue;
1514
1515 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
1516 continue;
1517
1518 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1519 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1520 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1521 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1522 nd6_dad_start((struct ifaddr *)ifa,
1523 0);
1524 }
1525 } else {
1526 if ((ifa->ia6_flags & IN6_IFF_DETACHED) == 0) {
1527 ifa->ia6_flags |= IN6_IFF_DETACHED;
1528 }
1529 }
1530 }
1531 }
1532 else {
1533 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) {
1534 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1535 continue;
1536
1537 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
1538 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1539 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1540 /* Do we need a delay in this case? */
1541 nd6_dad_start((struct ifaddr *)ifa, 0);
1542 }
1543 }
1544 }
1545 }
1546
1547 int
1548 nd6_prefix_onlink(pr)
1549 struct nd_prefix *pr;
1550 {
1551 struct ifaddr *ifa;
1552 struct ifnet *ifp = pr->ndpr_ifp;
1553 struct sockaddr_in6 mask6;
1554 struct nd_prefix *opr;
1555 u_long rtflags;
1556 int error = 0;
1557 struct rtentry *rt = NULL;
1558
1559 /* sanity check */
1560 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1561 nd6log((LOG_ERR,
1562 "nd6_prefix_onlink: %s/%d is already on-link\n",
1563 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1564 return (EEXIST);
1565 }
1566
1567 /*
1568 * Add the interface route associated with the prefix. Before
1569 * installing the route, check if there's the same prefix on another
1570 * interface, and the prefix has already installed the interface route.
1571 * Although such a configuration is expected to be rare, we explicitly
1572 * allow it.
1573 */
1574 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
1575 if (opr == pr)
1576 continue;
1577
1578 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1579 continue;
1580
1581 if (opr->ndpr_plen == pr->ndpr_plen &&
1582 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1583 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
1584 return (0);
1585 }
1586
1587 /*
1588 * We prefer link-local addresses as the associated interface address.
1589 */
1590 /* search for a link-local addr */
1591 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
1592 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
1593 if (ifa == NULL) {
1594 /* XXX: freebsd does not have ifa_ifwithaf */
1595 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1596 if (ifa->ifa_addr->sa_family == AF_INET6)
1597 break;
1598 }
1599 /* should we care about ia6_flags? */
1600 }
1601 if (ifa == NULL) {
1602 /*
1603 * This can still happen, when, for example, we receive an RA
1604 * containing a prefix with the L bit set and the A bit clear,
1605 * after removing all IPv6 addresses on the receiving
1606 * interface. This should, of course, be rare though.
1607 */
1608 nd6log((LOG_NOTICE,
1609 "nd6_prefix_onlink: failed to find any ifaddr"
1610 " to add route for a prefix(%s/%d) on %s\n",
1611 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1612 pr->ndpr_plen, if_name(ifp)));
1613 return (0);
1614 }
1615
1616 /*
1617 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
1618 * ifa->ifa_rtrequest = nd6_rtrequest;
1619 */
1620 bzero(&mask6, sizeof(mask6));
1621 mask6.sin6_len = sizeof(mask6);
1622 mask6.sin6_addr = pr->ndpr_mask;
1623 /* rtrequest() will probably set RTF_UP, but we're not sure. */
1624 rtflags = ifa->ifa_flags | RTF_UP;
1625 if (nd6_need_cache(ifp)) {
1626 /* explicitly set in case ifa_flags does not set the flag. */
1627 rtflags |= RTF_CLONING;
1628 } else {
1629 /*
1630 * explicitly clear the cloning bit in case ifa_flags sets it.
1631 */
1632 rtflags &= ~RTF_CLONING;
1633 }
1634 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1635 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt);
1636 if (error == 0) {
1637 if (rt != NULL) /* this should be non NULL, though */
1638 nd6_rtmsg(RTM_ADD, rt);
1639 pr->ndpr_stateflags |= NDPRF_ONLINK;
1640 } else {
1641 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a"
1642 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
1643 "errno = %d\n",
1644 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
1645 pr->ndpr_plen, if_name(ifp),
1646 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
1647 ip6_sprintf(&mask6.sin6_addr), rtflags, error));
1648 }
1649
1650 if (rt != NULL)
1651 rt->rt_refcnt--;
1652
1653 return (error);
1654 }
1655
1656 int
1657 nd6_prefix_offlink(pr)
1658 struct nd_prefix *pr;
1659 {
1660 int error = 0;
1661 struct ifnet *ifp = pr->ndpr_ifp;
1662 struct nd_prefix *opr;
1663 struct sockaddr_in6 sa6, mask6;
1664 struct rtentry *rt = NULL;
1665
1666 /* sanity check */
1667 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1668 nd6log((LOG_ERR,
1669 "nd6_prefix_offlink: %s/%d is already off-link\n",
1670 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen));
1671 return (EEXIST);
1672 }
1673
1674 bzero(&sa6, sizeof(sa6));
1675 sa6.sin6_family = AF_INET6;
1676 sa6.sin6_len = sizeof(sa6);
1677 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1678 sizeof(struct in6_addr));
1679 bzero(&mask6, sizeof(mask6));
1680 mask6.sin6_family = AF_INET6;
1681 mask6.sin6_len = sizeof(sa6);
1682 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1683 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1684 (struct sockaddr *)&mask6, 0, &rt);
1685 if (error == 0) {
1686 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
1687
1688 /* report the route deletion to the routing socket. */
1689 if (rt != NULL)
1690 nd6_rtmsg(RTM_DELETE, rt);
1691
1692 /*
1693 * There might be the same prefix on another interface,
1694 * the prefix which could not be on-link just because we have
1695 * the interface route (see comments in nd6_prefix_onlink).
1696 * If there's one, try to make the prefix on-link on the
1697 * interface.
1698 */
1699 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
1700 if (opr == pr)
1701 continue;
1702
1703 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
1704 continue;
1705
1706 /*
1707 * KAME specific: detached prefixes should not be
1708 * on-link.
1709 */
1710 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
1711 continue;
1712
1713 if (opr->ndpr_plen == pr->ndpr_plen &&
1714 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
1715 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
1716 int e;
1717
1718 if ((e = nd6_prefix_onlink(opr)) != 0) {
1719 nd6log((LOG_ERR,
1720 "nd6_prefix_offlink: failed to "
1721 "recover a prefix %s/%d from %s "
1722 "to %s (errno = %d)\n",
1723 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
1724 opr->ndpr_plen, if_name(ifp),
1725 if_name(opr->ndpr_ifp), e));
1726 }
1727 }
1728 }
1729 } else {
1730 /* XXX: can we still set the NDPRF_ONLINK flag? */
1731 nd6log((LOG_ERR,
1732 "nd6_prefix_offlink: failed to delete route: "
1733 "%s/%d on %s (errno = %d)\n",
1734 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
1735 error));
1736 }
1737
1738 if (rt != NULL) {
1739 if (rt->rt_refcnt <= 0) {
1740 /* XXX: we should free the entry ourselves. */
1741 rt->rt_refcnt++;
1742 rtfree(rt);
1743 }
1744 }
1745
1746 return (error);
1747 }
1748
1749 static struct in6_ifaddr *
1750 in6_ifadd(pr, mcast)
1751 struct nd_prefixctl *pr;
1752 int mcast;
1753 {
1754 struct ifnet *ifp = pr->ndpr_ifp;
1755 struct ifaddr *ifa;
1756 struct in6_aliasreq ifra;
1757 struct in6_ifaddr *ia, *ib;
1758 int error, plen0;
1759 struct in6_addr mask;
1760 int prefixlen = pr->ndpr_plen;
1761 int updateflags;
1762
1763 in6_prefixlen2mask(&mask, prefixlen);
1764
1765 /*
1766 * find a link-local address (will be interface ID).
1767 * Is it really mandatory? Theoretically, a global or a site-local
1768 * address can be configured without a link-local address, if we
1769 * have a unique interface identifier...
1770 *
1771 * it is not mandatory to have a link-local address, we can generate
1772 * interface identifier on the fly. we do this because:
1773 * (1) it should be the easiest way to find interface identifier.
1774 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1775 * for multiple addresses on a single interface, and possible shortcut
1776 * of DAD. we omitted DAD for this reason in the past.
1777 * (3) a user can prevent autoconfiguration of global address
1778 * by removing link-local address by hand (this is partly because we
1779 * don't have other way to control the use of IPv6 on an interface.
1780 * this has been our design choice - cf. NRL's "ifconfig auto").
1781 * (4) it is easier to manage when an interface has addresses
1782 * with the same interface identifier, than to have multiple addresses
1783 * with different interface identifiers.
1784 */
1785 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1786 if (ifa)
1787 ib = (struct in6_ifaddr *)ifa;
1788 else
1789 return NULL;
1790
1791 #if 0 /* don't care link local addr state, and always do DAD */
1792 /* if link-local address is not eligible, do not autoconfigure. */
1793 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1794 printf("in6_ifadd: link-local address not ready\n");
1795 return NULL;
1796 }
1797 #endif
1798
1799 /* prefixlen + ifidlen must be equal to 128 */
1800 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1801 if (prefixlen != plen0) {
1802 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1803 "(prefix=%d ifid=%d)\n",
1804 if_name(ifp), prefixlen, 128 - plen0));
1805 return NULL;
1806 }
1807
1808 /* make ifaddr */
1809
1810 memset(&ifra, 0, sizeof(ifra));
1811 /*
1812 * in6_update_ifa() does not use ifra_name, but we accurately set it
1813 * for safety.
1814 */
1815 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1816 ifra.ifra_addr.sin6_family = AF_INET6;
1817 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
1818 /* prefix */
1819 ifra.ifra_addr.sin6_addr = pr->ndpr_prefix.sin6_addr;
1820 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1821 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1822 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1823 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1824
1825 /* interface ID */
1826 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1827 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1828 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1829 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1830 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1831 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1832 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1833 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1834
1835 /* new prefix mask. */
1836 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1837 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1838 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
1839 sizeof(ifra.ifra_prefixmask.sin6_addr));
1840
1841 /* lifetimes */
1842 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1843 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1844
1845 /* XXX: scope zone ID? */
1846
1847 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1848
1849 /*
1850 * Make sure that we do not have this address already. This should
1851 * usually not happen, but we can still see this case, e.g., if we
1852 * have manually configured the exact address to be configured.
1853 */
1854 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
1855 /* this should be rare enough to make an explicit log */
1856 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1857 ip6_sprintf(&ifra.ifra_addr.sin6_addr));
1858 return (NULL);
1859 }
1860
1861 /*
1862 * Allocate ifaddr structure, link into chain, etc.
1863 * If we are going to create a new address upon receiving a multicasted
1864 * RA, we need to impose a random delay before starting DAD.
1865 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1866 */
1867 updateflags = 0;
1868 if (mcast)
1869 updateflags |= IN6_IFAUPDATE_DADDELAY;
1870 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1871 nd6log((LOG_ERR,
1872 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1873 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
1874 error));
1875 return (NULL); /* ifaddr must not have been allocated. */
1876 }
1877
1878 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1879
1880 return (ia); /* this is always non-NULL */
1881 }
1882
1883 int
1884 in6_tmpifadd(ia0, forcegen, dad_delay)
1885 const struct in6_ifaddr *ia0; /* corresponding public address */
1886 int forcegen, dad_delay;
1887 {
1888 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
1889 struct in6_ifaddr *newia, *ia;
1890 struct in6_aliasreq ifra;
1891 int i, error;
1892 int trylimit = 3; /* XXX: adhoc value */
1893 int updateflags;
1894 u_int32_t randid[2];
1895 u_int32_t vltime0, pltime0;
1896
1897 memset(&ifra, 0, sizeof(ifra));
1898 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
1899 ifra.ifra_addr = ia0->ia_addr;
1900 /* copy prefix mask */
1901 ifra.ifra_prefixmask = ia0->ia_prefixmask;
1902 /* clear the old IFID */
1903 for (i = 0; i < 4; i++) {
1904 ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
1905 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1906 }
1907
1908 again:
1909 if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
1910 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
1911 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
1912 "random IFID\n"));
1913 return (EINVAL);
1914 }
1915 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1916 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
1917 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1918 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1919
1920 /*
1921 * in6_get_tmpifid() quite likely provided a unique interface ID.
1922 * However, we may still have a chance to see collision, because
1923 * there may be a time lag between generation of the ID and generation
1924 * of the address. So, we'll do one more sanity check.
1925 */
1926 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1927 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1928 &ifra.ifra_addr.sin6_addr)) {
1929 if (trylimit-- == 0) {
1930 /*
1931 * Give up. Something strange should have
1932 * happened.
1933 */
1934 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
1935 "find a unique random IFID\n"));
1936 return (EEXIST);
1937 }
1938 forcegen = 1;
1939 goto again;
1940 }
1941 }
1942
1943 /*
1944 * The Valid Lifetime is the lower of the Valid Lifetime of the
1945 * public address or TEMP_VALID_LIFETIME.
1946 * The Preferred Lifetime is the lower of the Preferred Lifetime
1947 * of the public address or TEMP_PREFERRED_LIFETIME -
1948 * DESYNC_FACTOR.
1949 */
1950 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1951 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
1952 (ia0->ia6_lifetime.ia6t_vltime -
1953 (time_second - ia0->ia6_updatetime));
1954 if (vltime0 > ip6_temp_valid_lifetime)
1955 vltime0 = ip6_temp_valid_lifetime;
1956 } else
1957 vltime0 = ip6_temp_valid_lifetime;
1958 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1959 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
1960 (ia0->ia6_lifetime.ia6t_pltime -
1961 (time_second - ia0->ia6_updatetime));
1962 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){
1963 pltime0 = ip6_temp_preferred_lifetime -
1964 ip6_desync_factor;
1965 }
1966 } else
1967 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
1968 ifra.ifra_lifetime.ia6t_vltime = vltime0;
1969 ifra.ifra_lifetime.ia6t_pltime = pltime0;
1970
1971 /*
1972 * A temporary address is created only if this calculated Preferred
1973 * Lifetime is greater than REGEN_ADVANCE time units.
1974 */
1975 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
1976 return (0);
1977
1978 /* XXX: scope zone ID? */
1979
1980 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
1981
1982 /* allocate ifaddr structure, link into chain, etc. */
1983 updateflags = 0;
1984 if (dad_delay)
1985 updateflags |= IN6_IFAUPDATE_DADDELAY;
1986 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
1987 return (error);
1988
1989 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1990 if (newia == NULL) { /* XXX: can it happen? */
1991 nd6log((LOG_ERR,
1992 "in6_tmpifadd: ifa update succeeded, but we got "
1993 "no ifaddr\n"));
1994 return (EINVAL); /* XXX */
1995 }
1996 newia->ia6_ndpr = ia0->ia6_ndpr;
1997 newia->ia6_ndpr->ndpr_refcnt++;
1998
1999 /*
2000 * A newly added address might affect the status of other addresses.
2001 * XXX: when the temporary address is generated with a new public
2002 * address, the onlink check is redundant. However, it would be safe
2003 * to do the check explicitly everywhere a new address is generated,
2004 * and, in fact, we surely need the check when we create a new
2005 * temporary address due to deprecation of an old temporary address.
2006 */
2007 pfxlist_onlink_check();
2008
2009 return (0);
2010 }
2011
2012 static int
2013 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
2014 {
2015
2016 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */
2017 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
2018 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
2019 "(%d) is greater than valid lifetime(%d)\n",
2020 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
2021 return (EINVAL);
2022 }
2023 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
2024 ndpr->ndpr_preferred = 0;
2025 else
2026 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
2027 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2028 ndpr->ndpr_expire = 0;
2029 else
2030 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
2031
2032 return 0;
2033 }
2034
2035 static void
2036 in6_init_address_ltimes(struct nd_prefix *new,
2037 struct in6_addrlifetime *lt6)
2038 {
2039
2040 /* Valid lifetime must not be updated unless explicitly specified. */
2041 /* init ia6t_expire */
2042 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
2043 lt6->ia6t_expire = 0;
2044 else {
2045 lt6->ia6t_expire = time_second;
2046 lt6->ia6t_expire += lt6->ia6t_vltime;
2047 }
2048
2049 /* init ia6t_preferred */
2050 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
2051 lt6->ia6t_preferred = 0;
2052 else {
2053 lt6->ia6t_preferred = time_second;
2054 lt6->ia6t_preferred += lt6->ia6t_pltime;
2055 }
2056 }
2057
2058 /*
2059 * Delete all the routing table entries that use the specified gateway.
2060 * XXX: this function causes search through all entries of routing table, so
2061 * it shouldn't be called when acting as a router.
2062 */
2063 void
2064 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2065 {
2066 struct radix_node_head *rnh = rt_tables[AF_INET6];
2067 int s = splsoftnet();
2068
2069 /* We'll care only link-local addresses */
2070 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
2071 splx(s);
2072 return;
2073 }
2074
2075 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
2076 splx(s);
2077 }
2078
2079 static int
2080 rt6_deleteroute(rn, arg)
2081 struct radix_node *rn;
2082 void *arg;
2083 {
2084 #define SIN6(s) ((struct sockaddr_in6 *)s)
2085 struct rtentry *rt = (struct rtentry *)rn;
2086 struct in6_addr *gate = (struct in6_addr *)arg;
2087
2088 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
2089 return (0);
2090
2091 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
2092 return (0);
2093
2094 /*
2095 * Do not delete a static route.
2096 * XXX: this seems to be a bit ad-hoc. Should we consider the
2097 * 'cloned' bit instead?
2098 */
2099 if ((rt->rt_flags & RTF_STATIC) != 0)
2100 return (0);
2101
2102 /*
2103 * We delete only host route. This means, in particular, we don't
2104 * delete default route.
2105 */
2106 if ((rt->rt_flags & RTF_HOST) == 0)
2107 return (0);
2108
2109 return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
2110 rt_mask(rt), rt->rt_flags, 0));
2111 #undef SIN6
2112 }
2113
2114 int
2115 nd6_setdefaultiface(ifindex)
2116 int ifindex;
2117 {
2118 int error = 0;
2119
2120 if (ifindex < 0 || if_indexlim <= ifindex)
2121 return (EINVAL);
2122 if (ifindex != 0 && !ifindex2ifnet[ifindex])
2123 return (EINVAL);
2124
2125 if (nd6_defifindex != ifindex) {
2126 nd6_defifindex = ifindex;
2127 if (nd6_defifindex > 0) {
2128 nd6_defifp = ifindex2ifnet[nd6_defifindex];
2129 } else
2130 nd6_defifp = NULL;
2131
2132 /*
2133 * Our current implementation assumes one-to-one maping between
2134 * interfaces and links, so it would be natural to use the
2135 * default interface as the default link.
2136 */
2137 scope6_setdefault(nd6_defifp);
2138 }
2139
2140 return (error);
2141 }
Cache object: 9cf1d912c7e5a041cf36264d9b6a0492
|