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