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