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