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