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