nd6_rtr.c revision 1.13 1 /* $NetBSD: nd6_rtr.c,v 1.13 2000/03/02 07:14:52 itojun Exp $ */
2 /* $KAME: nd6_rtr.c,v 1.27 2000/02/26 06:53:11 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/time.h>
40 #include <sys/kernel.h>
41 #include <sys/errno.h>
42 #include <sys/ioctl.h>
43 #include <sys/syslog.h>
44
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <net/radix.h>
50
51 #include <netinet/in.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet6/nd6.h>
56 #include <netinet/icmp6.h>
57
58 #include <net/net_osdep.h>
59
60 #define SDL(s) ((struct sockaddr_dl *)s)
61
62 static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *));
63 static int prelist_add __P((struct nd_prefix *, struct nd_defrouter *));
64 static struct nd_prefix *prefix_lookup __P((struct nd_prefix *));
65 static struct in6_ifaddr *in6_ifadd __P((struct ifnet *, struct in6_addr *,
66 struct in6_addr *, int));
67 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
68 struct nd_defrouter *));
69 static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *));
70 static void pfxrtr_del __P((struct nd_pfxrouter *));
71 static struct nd_pfxrouter *find_pfxlist_reachable_router __P((struct nd_prefix *));
72 static void nd6_detach_prefix __P((struct nd_prefix *));
73 static void nd6_attach_prefix __P((struct nd_prefix *));
74 static void defrouter_addifreq __P((struct ifnet *));
75
76 static void in6_init_address_ltimes __P((struct nd_prefix *ndpr,
77 struct in6_addrlifetime *lt6,
78 int update_vltime));
79
80 static int rt6_deleteroute __P((struct radix_node *, void *));
81
82 extern int nd6_recalc_reachtm_interval;
83
84 struct ifnet *nd6_defifp;
85 int nd6_defifindex;
86
87 /*
88 * Receive Router Solicitation Message - just for routers.
89 * Router solicitation/advertisement is mostly managed by userland program
90 * (rtadvd) so here we have no function like nd6_ra_output().
91 *
92 * Based on RFC 2461
93 */
94 void
95 nd6_rs_input(m, off, icmp6len)
96 struct mbuf *m;
97 int off, icmp6len;
98 {
99 struct ifnet *ifp = m->m_pkthdr.rcvif;
100 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
101 struct nd_router_solicit *nd_rs;
102 struct in6_addr saddr6 = ip6->ip6_src;
103 #if 0
104 struct in6_addr daddr6 = ip6->ip6_dst;
105 #endif
106 char *lladdr = NULL;
107 int lladdrlen = 0;
108 #if 0
109 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
110 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
111 struct rtentry *rt = NULL;
112 int is_newentry;
113 #endif
114 union nd_opts ndopts;
115
116 /* If I'm not a router, ignore it. */
117 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
118 goto freeit;
119
120 /* Sanity checks */
121 if (ip6->ip6_hlim != 255) {
122 log(LOG_ERR,
123 "nd6_rs_input: invalid hlim %d\n", ip6->ip6_hlim);
124 goto freeit;
125 }
126
127 /*
128 * Don't update the neighbor cache, if src = ::.
129 * This indicates that the src has no IP address assigned yet.
130 */
131 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
132 goto freeit;
133
134 #ifndef PULLDOWN_TEST
135 IP6_EXTHDR_CHECK(m, off, icmp6len,);
136 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
137 #else
138 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
139 if (nd_rs == NULL) {
140 icmp6stat.icp6s_tooshort++;
141 return;
142 }
143 #endif
144
145 icmp6len -= sizeof(*nd_rs);
146 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
147 if (nd6_options(&ndopts) < 0) {
148 log(LOG_INFO, "nd6_rs_input: invalid ND option, ignored\n");
149 goto freeit;
150 }
151
152 if (ndopts.nd_opts_src_lladdr) {
153 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
154 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
155 }
156
157 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
158 log(LOG_INFO,
159 "nd6_rs_input: lladdrlen mismatch for %s "
160 "(if %d, RS packet %d)\n",
161 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
162 }
163
164 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
165
166 freeit:
167 m_freem(m);
168 }
169
170 /*
171 * Receive Router Advertisement Message.
172 *
173 * Based on RFC 2461
174 * TODO: on-link bit on prefix information
175 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
176 */
177 void
178 nd6_ra_input(m, off, icmp6len)
179 struct mbuf *m;
180 int off, icmp6len;
181 {
182 struct ifnet *ifp = m->m_pkthdr.rcvif;
183 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
184 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
185 struct nd_router_advert *nd_ra;
186 struct in6_addr saddr6 = ip6->ip6_src;
187 #if 0
188 struct in6_addr daddr6 = ip6->ip6_dst;
189 int flags; /* = nd_ra->nd_ra_flags_reserved; */
190 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
191 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
192 #endif
193 union nd_opts ndopts;
194 struct nd_defrouter *dr;
195
196 if (ip6_accept_rtadv == 0)
197 goto freeit;
198
199 if (ip6->ip6_hlim != 255) {
200 log(LOG_ERR,
201 "nd6_ra_input: invalid hlim %d\n", ip6->ip6_hlim);
202 goto freeit;
203 }
204
205 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
206 log(LOG_ERR,
207 "nd6_ra_input: src %s is not link-local\n",
208 ip6_sprintf(&saddr6));
209 goto freeit;
210 }
211
212 #ifndef PULLDOWN_TEST
213 IP6_EXTHDR_CHECK(m, off, icmp6len,);
214 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
215 #else
216 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
217 if (nd_ra == NULL) {
218 icmp6stat.icp6s_tooshort++;
219 return;
220 }
221 #endif
222
223 icmp6len -= sizeof(*nd_ra);
224 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
225 if (nd6_options(&ndopts) < 0) {
226 log(LOG_INFO, "nd6_ra_input: invalid ND option, ignored\n");
227 goto freeit;
228 }
229
230 {
231 struct nd_defrouter dr0;
232 u_int32_t advreachable = nd_ra->nd_ra_reachable;
233 long time_second = time.tv_sec;
234
235 dr0.rtaddr = saddr6;
236 dr0.flags = nd_ra->nd_ra_flags_reserved;
237 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
238 dr0.expire = time_second + dr0.rtlifetime;
239 dr0.ifp = ifp;
240 /* unspecified or not? (RFC 2461 6.3.4) */
241 if (advreachable) {
242 NTOHL(advreachable);
243 if (advreachable <= MAX_REACHABLE_TIME &&
244 ndi->basereachable != advreachable) {
245 ndi->basereachable = advreachable;
246 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
247 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
248 }
249 }
250 if (nd_ra->nd_ra_retransmit)
251 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
252 if (nd_ra->nd_ra_curhoplimit)
253 ndi->chlim = nd_ra->nd_ra_curhoplimit;
254 dr = defrtrlist_update(&dr0);
255 }
256
257 /*
258 * prefix
259 */
260 if (ndopts.nd_opts_pi) {
261 struct nd_opt_hdr *pt;
262 struct nd_opt_prefix_info *pi;
263 struct nd_prefix pr;
264
265 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
266 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
267 pt = (struct nd_opt_hdr *)((caddr_t)pt +
268 (pt->nd_opt_len << 3))) {
269 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
270 continue;
271 pi = (struct nd_opt_prefix_info *)pt;
272
273 if (pi->nd_opt_pi_len != 4) {
274 log(LOG_INFO, "nd6_ra_input: invalid option "
275 "len %d for prefix information option, "
276 "ignored\n", pi->nd_opt_pi_len);
277 continue;
278 }
279
280 if (128 < pi->nd_opt_pi_prefix_len) {
281 log(LOG_INFO, "nd6_ra_input: invalid prefix "
282 "len %d for prefix information option, "
283 "ignored\n", pi->nd_opt_pi_prefix_len);
284 continue;
285 }
286
287 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
288 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
289 log(LOG_INFO, "nd6_ra_input: invalid prefix "
290 "%s, ignored\n",
291 ip6_sprintf(&pi->nd_opt_pi_prefix));
292 continue;
293 }
294
295 /* aggregatable unicast address, rfc2374 */
296 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
297 && pi->nd_opt_pi_prefix_len != 64) {
298 log(LOG_INFO, "nd6_ra_input: invalid prefixlen "
299 "%d for rfc2374 prefix %s, ignored\n",
300 pi->nd_opt_pi_prefix_len,
301 ip6_sprintf(&pi->nd_opt_pi_prefix));
302 continue;
303 }
304
305 bzero(&pr, sizeof(pr));
306 pr.ndpr_prefix.sin6_family = AF_INET6;
307 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
308 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
309 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
310
311 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
312 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
313 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
314 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
315 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
316 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
317 pr.ndpr_pltime =
318 ntohl(pi->nd_opt_pi_preferred_time);
319
320 if (in6_init_prefix_ltimes(&pr))
321 continue; /* prefix lifetime init failed */
322
323 (void)prelist_update(&pr, dr, m);
324 }
325 }
326
327 /*
328 * MTU
329 */
330 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
331 u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
332
333 /* lower bound */
334 if (mtu < IPV6_MMTU) {
335 log(LOG_INFO, "nd6_ra_input: bogus mtu option "
336 "mtu=%d sent from %s, ignoring\n",
337 mtu, ip6_sprintf(&ip6->ip6_src));
338 goto skip;
339 }
340
341 /* upper bound */
342 if (ndi->maxmtu) {
343 if (mtu <= ndi->maxmtu) {
344 int change = (ndi->linkmtu != mtu);
345
346 ndi->linkmtu = mtu;
347 if (change) /* in6_maxmtu may change */
348 in6_setmaxmtu();
349 } else {
350 log(LOG_INFO, "nd6_ra_input: bogus mtu "
351 "mtu=%d sent from %s; "
352 "exceeds maxmtu %d, ignoring\n",
353 mtu, ip6_sprintf(&ip6->ip6_src),
354 ndi->maxmtu);
355 }
356 } else {
357 log(LOG_INFO, "nd6_ra_input: mtu option "
358 "mtu=%d sent from %s; maxmtu unknown, "
359 "ignoring\n",
360 mtu, ip6_sprintf(&ip6->ip6_src));
361 }
362 }
363
364 skip:
365
366 /*
367 * Src linkaddress
368 */
369 {
370 char *lladdr = NULL;
371 int lladdrlen = 0;
372
373 if (ndopts.nd_opts_src_lladdr) {
374 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
375 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
376 }
377
378 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
379 log(LOG_INFO,
380 "nd6_ra_input: lladdrlen mismatch for %s "
381 "(if %d, RA packet %d)\n",
382 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
383 }
384
385 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
386
387 /*
388 * Installing a link-layer address might change the state of the
389 * router's neighbor cache, which might also affect our on-link
390 * detection of adveritsed prefixes.
391 */
392 pfxlist_onlink_check();
393 }
394
395 freeit:
396 m_freem(m);
397 }
398
399 /*
400 * default router list proccessing sub routines
401 */
402 void
403 defrouter_addreq(new)
404 struct nd_defrouter *new;
405 {
406 struct sockaddr_in6 def, mask, gate;
407 int s;
408
409 Bzero(&def, sizeof(def));
410 Bzero(&mask, sizeof(mask));
411 Bzero(&gate, sizeof(gate));
412
413 def.sin6_len = mask.sin6_len = gate.sin6_len
414 = sizeof(struct sockaddr_in6);
415 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
416 gate.sin6_addr = new->rtaddr;
417
418 s = splsoftnet();
419 (void)rtrequest(RTM_ADD, (struct sockaddr *)&def,
420 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
421 RTF_GATEWAY, NULL);
422 splx(s);
423 return;
424 }
425
426 /* Add a route to a given interface as default */
427 static void
428 defrouter_addifreq(ifp)
429 struct ifnet *ifp;
430 {
431 struct sockaddr_in6 def, mask;
432 struct ifaddr *ifa;
433 int error, flags;
434
435 bzero(&def, sizeof(def));
436 bzero(&mask, sizeof(mask));
437
438 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
439 def.sin6_family = mask.sin6_family = AF_INET6;
440
441 /*
442 * Search for an ifaddr beloging to the specified interface.
443 * XXX: An IPv6 address are required to be assigned on the interface.
444 */
445 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
446 log(LOG_ERR, /* better error? */
447 "defrouter_addifreq: failed to find an ifaddr "
448 "to install a route to interface %s\n",
449 if_name(ifp));
450 return;
451 }
452
453 flags = ifa->ifa_flags;
454 if ((ifp->if_flags & IFF_POINTOPOINT) != 0)
455 flags &= ~RTF_CLONING;
456 if ((error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
457 ifa->ifa_addr, (struct sockaddr *)&mask,
458 flags, NULL)) != 0) {
459 log(LOG_ERR,
460 "defrouter_addifreq: failed to install a route to "
461 "interface %s (errno = %d)\n",
462 if_name(ifp), error);
463 }
464 }
465
466 struct nd_defrouter *
467 defrouter_lookup(addr, ifp)
468 struct in6_addr *addr;
469 struct ifnet *ifp;
470 {
471 struct nd_defrouter *dr;
472
473 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
474 dr = TAILQ_NEXT(dr, dr_entry)) {
475 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
476 return(dr);
477 }
478
479 return(NULL); /* search failed */
480 }
481
482 void
483 defrouter_delreq(dr, dofree)
484 struct nd_defrouter *dr;
485 int dofree;
486 {
487 struct sockaddr_in6 def, mask, gate;
488
489 Bzero(&def, sizeof(def));
490 Bzero(&mask, sizeof(mask));
491 Bzero(&gate, sizeof(gate));
492
493 def.sin6_len = mask.sin6_len = gate.sin6_len
494 = sizeof(struct sockaddr_in6);
495 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
496 gate.sin6_addr = dr->rtaddr;
497
498 rtrequest(RTM_DELETE, (struct sockaddr *)&def,
499 (struct sockaddr *)&gate,
500 (struct sockaddr *)&mask,
501 RTF_GATEWAY, (struct rtentry **)0);
502
503 if (dofree) /* XXX: necessary? */
504 free(dr, M_IP6NDP);
505 }
506
507 void
508 defrtrlist_del(dr)
509 struct nd_defrouter *dr;
510 {
511 struct nd_defrouter *deldr = NULL;
512 struct nd_prefix *pr;
513
514 /*
515 * Flush all the routing table entries that use the router
516 * as a next hop.
517 */
518 if (!ip6_forwarding && ip6_accept_rtadv) {
519 /* above is a good condition? */
520 rt6_flush(&dr->rtaddr, dr->ifp);
521 }
522
523 if (dr == TAILQ_FIRST(&nd_defrouter))
524 deldr = dr; /* The router is primary. */
525
526 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
527
528 /*
529 * Also delete all the pointers to the router in each prefix lists.
530 */
531 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
532 struct nd_pfxrouter *pfxrtr;
533 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
534 pfxrtr_del(pfxrtr);
535 }
536 pfxlist_onlink_check();
537
538 /*
539 * If the router is the primary one, choose a new one.
540 * Note that defrouter_select() will remove the current gateway
541 * from the routing table.
542 */
543 if (deldr)
544 defrouter_select();
545
546 free(dr, M_IP6NDP);
547 }
548
549 /*
550 * Default Router Selection according to Section 6.3.6 of RFC 2461:
551 * 1) Routers that are reachable or probably reachable should be
552 * preferred.
553 * 2) When no routers on the list are known to be reachable or
554 * probably reachable, routers SHOULD be selected in a round-robin
555 * fashion.
556 * 3) If the Default Router List is empty, assume that all
557 * destinations are on-link.
558 */
559 void
560 defrouter_select()
561 {
562 int s = splsoftnet();
563 struct nd_defrouter *dr, anydr;
564 struct rtentry *rt = NULL;
565 struct llinfo_nd6 *ln = NULL;
566
567 /*
568 * Search for a (probably) reachable router from the list.
569 */
570 for (dr = TAILQ_FIRST(&nd_defrouter); dr;
571 dr = TAILQ_NEXT(dr, dr_entry)) {
572 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
573 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
574 ND6_IS_LLINFO_PROBREACH(ln)) {
575 /* Got it, and move it to the head */
576 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
577 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
578 break;
579 }
580 }
581
582 if ((dr = TAILQ_FIRST(&nd_defrouter))) {
583 /*
584 * De-install the previous default gateway and install
585 * a new one.
586 * Note that if there is no reachable router in the list,
587 * the head entry will be used anyway.
588 * XXX: do we have to check the current routing table entry?
589 */
590 bzero(&anydr, sizeof(anydr));
591 defrouter_delreq(&anydr, 0);
592 defrouter_addreq(dr);
593 }
594 else {
595 /*
596 * The Default Router List is empty, so install the default
597 * route to an inteface.
598 * XXX: The specification does not say this mechanism should
599 * be restricted to hosts, but this would be not useful
600 * (even harmful) for routers.
601 */
602 if (!ip6_forwarding) {
603 /*
604 * De-install the current default route
605 * in advance.
606 */
607 bzero(&anydr, sizeof(anydr));
608 defrouter_delreq(&anydr, 0);
609 if (nd6_defifp) {
610 /*
611 * Install a route to the default interface
612 * as default route.
613 */
614 defrouter_addifreq(nd6_defifp);
615 }
616 else /* noisy log? */
617 log(LOG_INFO, "defrouter_select: "
618 "there's no default router and no default"
619 " interface\n");
620 }
621 }
622
623 splx(s);
624 return;
625 }
626
627 static struct nd_defrouter *
628 defrtrlist_update(new)
629 struct nd_defrouter *new;
630 {
631 struct nd_defrouter *dr, *n;
632 int s = splsoftnet();
633
634 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
635 /* entry exists */
636 if (new->rtlifetime == 0) {
637 defrtrlist_del(dr);
638 dr = NULL;
639 } else {
640 /* override */
641 dr->flags = new->flags; /* xxx flag check */
642 dr->rtlifetime = new->rtlifetime;
643 dr->expire = new->expire;
644 }
645 splx(s);
646 return(dr);
647 }
648
649 /* entry does not exist */
650 if (new->rtlifetime == 0) {
651 splx(s);
652 return(NULL);
653 }
654
655 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
656 if (n == NULL) {
657 splx(s);
658 return(NULL);
659 }
660 bzero(n, sizeof(*n));
661 *n = *new;
662
663 /*
664 * Insert the new router at the end of the Default Router List.
665 * If there is no other router, install it anyway. Otherwise,
666 * just continue to use the current default router.
667 */
668 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
669 if (TAILQ_FIRST(&nd_defrouter) == n)
670 defrouter_select();
671 splx(s);
672
673 return(n);
674 }
675
676 static struct nd_pfxrouter *
677 pfxrtr_lookup(pr, dr)
678 struct nd_prefix *pr;
679 struct nd_defrouter *dr;
680 {
681 struct nd_pfxrouter *search;
682
683 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
684 if (search->router == dr)
685 break;
686 }
687
688 return(search);
689 }
690
691 static void
692 pfxrtr_add(pr, dr)
693 struct nd_prefix *pr;
694 struct nd_defrouter *dr;
695 {
696 struct nd_pfxrouter *new;
697
698 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
699 if (new == NULL)
700 return;
701 bzero(new, sizeof(*new));
702 new->router = dr;
703
704 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
705
706 pfxlist_onlink_check();
707 }
708
709 static void
710 pfxrtr_del(pfr)
711 struct nd_pfxrouter *pfr;
712 {
713 LIST_REMOVE(pfr, pfr_entry);
714 free(pfr, M_IP6NDP);
715 }
716
717 static struct nd_prefix *
718 prefix_lookup(pr)
719 struct nd_prefix *pr;
720 {
721 struct nd_prefix *search;
722
723 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
724 if (pr->ndpr_ifp == search->ndpr_ifp &&
725 pr->ndpr_plen == search->ndpr_plen &&
726 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
727 &search->ndpr_prefix.sin6_addr,
728 pr->ndpr_plen)
729 ) {
730 break;
731 }
732 }
733
734 return(search);
735 }
736
737 static int
738 prelist_add(pr, dr)
739 struct nd_prefix *pr;
740 struct nd_defrouter *dr;
741 {
742 struct nd_prefix *new;
743 int i, s;
744
745 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
746 if (new == NULL)
747 return ENOMEM;
748 bzero(new, sizeof(*new));
749 *new = *pr;
750
751 /* initilization */
752 new->ndpr_statef_onlink = pr->ndpr_statef_onlink;
753 LIST_INIT(&new->ndpr_advrtrs);
754 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
755 /* make prefix in the canonical form */
756 for (i = 0; i < 4; i++)
757 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
758 new->ndpr_mask.s6_addr32[i];
759
760 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
761
762 s = splsoftnet();
763 /* link ndpr_entry to nd_prefix list */
764 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
765 splx(s);
766
767 if (dr)
768 pfxrtr_add(new, dr);
769
770 return 0;
771 }
772
773 void
774 prelist_remove(pr)
775 struct nd_prefix *pr;
776 {
777 struct nd_pfxrouter *pfr, *next;
778 int s;
779
780 s = splsoftnet();
781 /* unlink ndpr_entry from nd_prefix list */
782 LIST_REMOVE(pr, ndpr_entry);
783 splx(s);
784
785 /* free list of routers that adversed the prefix */
786 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
787 next = pfr->pfr_next;
788
789 free(pfr, M_IP6NDP);
790 }
791 free(pr, M_IP6NDP);
792
793 pfxlist_onlink_check();
794 }
795
796 /*
797 * NOTE: We set address lifetime to keep
798 * address lifetime <= prefix lifetime
799 * invariant. This is to simplify on-link determination code.
800 * If onlink determination is udated, this routine may have to be updated too.
801 */
802 int
803 prelist_update(new, dr, m)
804 struct nd_prefix *new;
805 struct nd_defrouter *dr; /* may be NULL */
806 struct mbuf *m;
807 {
808 struct in6_ifaddr *ia6 = NULL;
809 struct nd_prefix *pr;
810 int s = splsoftnet();
811 int error = 0;
812 int auth;
813 struct in6_addrlifetime *lt6;
814
815 auth = 0;
816 if (m) {
817 /*
818 * Authenticity for NA consists authentication for
819 * both IP header and IP datagrams, doesn't it ?
820 */
821 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
822 auth = (m->m_flags & M_AUTHIPHDR
823 && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
824 #endif
825 }
826
827 if ((pr = prefix_lookup(new)) != NULL) {
828 if (pr->ndpr_ifp != new->ndpr_ifp) {
829 error = EADDRNOTAVAIL;
830 goto end;
831 }
832 /* update prefix information */
833 pr->ndpr_flags = new->ndpr_flags;
834 pr->ndpr_vltime = new->ndpr_vltime;
835 pr->ndpr_pltime = new->ndpr_pltime;
836 pr->ndpr_preferred = new->ndpr_preferred;
837 pr->ndpr_expire = new->ndpr_expire;
838
839 /*
840 * RFC 2462 5.5.3 (d) or (e)
841 * We got a prefix which we have seen in the past.
842 */
843 if (!new->ndpr_raf_auto)
844 goto noautoconf1;
845
846 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
847 ia6 = NULL;
848 else
849 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
850
851 if (ia6 == NULL) {
852 /*
853 * Special case:
854 * (1) We have seen the prefix advertised before, but
855 * we have never performed autoconfig for this prefix.
856 * This is because Autonomous bit was 0 previously, or
857 * autoconfig failed due to some other reasons.
858 * (2) We have seen the prefix advertised before and
859 * we have performed autoconfig in the past, but
860 * we seem to have no interface address right now.
861 * This is because the interface address have expired.
862 *
863 * This prefix is fresh, with respect to autoconfig
864 * process.
865 *
866 * Add an address based on RFC 2462 5.5.3 (d).
867 */
868 ia6 = in6_ifadd(pr->ndpr_ifp,
869 &pr->ndpr_prefix.sin6_addr, &pr->ndpr_addr,
870 new->ndpr_plen);
871 if (!ia6) {
872 error = EADDRNOTAVAIL;
873 log(LOG_ERR, "prelist_update: failed to add a "
874 "new address\n");
875 goto noautoconf1;
876 }
877
878 lt6 = &ia6->ia6_lifetime;
879
880 /* address lifetime <= prefix lifetime */
881 lt6->ia6t_vltime = new->ndpr_vltime;
882 lt6->ia6t_pltime = new->ndpr_pltime;
883 in6_init_address_ltimes(new, lt6, 1);
884 } else {
885 #define TWOHOUR (120*60)
886 /*
887 * We have seen the prefix before, and we have added
888 * interface address in the past. We still have
889 * the interface address assigned.
890 *
891 * update address lifetime based on RFC 2462
892 * 5.5.3 (e).
893 */
894 int update = 0;
895
896 lt6 = &ia6->ia6_lifetime;
897
898 #if 0 /* RFC 2462 5.5.3 (e) */
899 lt6->ia6t_pltime = new->ndpr_pltime;
900 if (TWOHOUR < new->ndpr_vltime
901 || lt6pr->nd < new->ndpr_vltime) {
902 lt6->ia6t_vltime = new->ndpr_vltime;
903 update++;
904 } else if (auth
905 && lt6->ia6t_vltime <= TWOHOUR0
906 && new->ndpr_vltime <= lt6->ia6t_vltime) {
907 lt6->ia6t_vltime = new->ndpr_vltime;
908 update++;
909 } else {
910 lt6->ia6t_vltime = TWOHOUR;
911 update++;
912 }
913
914 /* 2 hour rule is not imposed for pref lifetime */
915 new->ndpr_apltime = new->ndpr_pltime;
916 lt6->ia6t_pltime = new->ndpr_pltime;
917 #else /* update from Jim Bound, (ipng 6712) */
918 if (TWOHOUR < new->ndpr_vltime
919 || lt6->ia6t_vltime < new->ndpr_vltime) {
920 lt6->ia6t_vltime = new->ndpr_vltime;
921 update++;
922 } else if (auth) {
923 lt6->ia6t_vltime = new->ndpr_vltime;
924 update++;
925 }
926
927 /* jim bound rule is not imposed for pref lifetime */
928 lt6->ia6t_pltime = new->ndpr_pltime;
929 #endif
930 in6_init_address_ltimes(new, lt6, update);
931 }
932
933 noautoconf1:
934
935 #if 0
936 /* address lifetime expire processing, RFC 2462 5.5.4. */
937 if (pr->ndpr_preferred && pr->ndpr_preferred < time_second) {
938 struct in6_ifaddr *ia6;
939
940 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
941 if (ia6)
942 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
943 }
944 #endif
945
946 if (dr && pfxrtr_lookup(pr, dr) == NULL)
947 pfxrtr_add(pr, dr);
948 } else {
949 int error_tmp;
950
951 if (new->ndpr_vltime == 0) goto end;
952
953 bzero(&new->ndpr_addr, sizeof(struct in6_addr));
954
955 /*
956 * RFC 2462 5.5.3 (d)
957 * We got a fresh prefix. Perform some sanity checks
958 * and add an interface address by appending interface ID
959 * to the advertised prefix.
960 */
961 if (!new->ndpr_raf_auto)
962 goto noautoconf2;
963
964 ia6 = in6_ifadd(new->ndpr_ifp, &new->ndpr_prefix.sin6_addr,
965 &new->ndpr_addr, new->ndpr_plen);
966 if (!ia6) {
967 error = EADDRNOTAVAIL;
968 log(LOG_ERR, "prelist_update: "
969 "failed to add a new address\n");
970 goto noautoconf2;
971 }
972 /* set onlink bit if an interface route is configured */
973 new->ndpr_statef_onlink = (ia6->ia_flags & IFA_ROUTE) ? 1 : 0;
974
975 lt6 = &ia6->ia6_lifetime;
976
977 /* address lifetime <= prefix lifetime */
978 lt6->ia6t_vltime = new->ndpr_vltime;
979 lt6->ia6t_pltime = new->ndpr_pltime;
980 in6_init_address_ltimes(new, lt6, 1);
981
982 noautoconf2:
983 error_tmp = prelist_add(new, dr);
984 error = error_tmp ? error_tmp : error;
985 }
986
987 end:
988 splx(s);
989 return error;
990 }
991
992 /*
993 * A supplement function used in the on-link detection below;
994 * detect if a given prefix has a (probably) reachable advertising router.
995 * XXX: lengthy function name...
996 */
997 static struct nd_pfxrouter *
998 find_pfxlist_reachable_router(pr)
999 struct nd_prefix *pr;
1000 {
1001 struct nd_pfxrouter *pfxrtr;
1002 struct rtentry *rt;
1003 struct llinfo_nd6 *ln;
1004
1005 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
1006 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
1007 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
1008 pfxrtr->router->ifp)) &&
1009 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
1010 ND6_IS_LLINFO_PROBREACH(ln))
1011 break; /* found */
1012 }
1013
1014 return(pfxrtr);
1015
1016 }
1017
1018 /*
1019 * Check if each prefix in the prefix list has at least one available router
1020 * that advertised the prefix (A router is "available" if its neighbor cache
1021 * entry has reachable or probably reachable).
1022 * If the check fails, the prefix may be off-link, because, for example,
1023 * we have moved from the network but the lifetime of the prefix has not
1024 * been expired yet. So we should not use the prefix if there is another
1025 * prefix that has an available router.
1026 * But if there is no prefix that has an available router, we still regards
1027 * all the prefixes as on-link. This is because we can't tell if all the
1028 * routers are simply dead or if we really moved from the network and there
1029 * is no router around us.
1030 */
1031 void
1032 pfxlist_onlink_check()
1033 {
1034 struct nd_prefix *pr;
1035
1036 /*
1037 * Check if there is a prefix that has a reachable advertising
1038 * router.
1039 */
1040 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1041 if (find_pfxlist_reachable_router(pr))
1042 break;
1043 }
1044
1045 if (pr) {
1046 /*
1047 * There is at least one prefix that has a reachable router.
1048 * First, detach prefixes which has no reachable advertising
1049 * router and then attach other prefixes.
1050 * The order is important since an attached prefix and a
1051 * detached prefix may have a same interface route.
1052 */
1053 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1054 if (find_pfxlist_reachable_router(pr) == NULL &&
1055 pr->ndpr_statef_onlink) {
1056 pr->ndpr_statef_onlink = 0;
1057 nd6_detach_prefix(pr);
1058 }
1059 }
1060 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1061 if (find_pfxlist_reachable_router(pr) &&
1062 pr->ndpr_statef_onlink == 0)
1063 nd6_attach_prefix(pr);
1064 }
1065 }
1066 else {
1067 /* there is no prefix that has a reachable router */
1068 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next)
1069 if (pr->ndpr_statef_onlink == 0)
1070 nd6_attach_prefix(pr);
1071 }
1072 }
1073
1074 static void
1075 nd6_detach_prefix(pr)
1076 struct nd_prefix *pr;
1077 {
1078 struct in6_ifaddr *ia6;
1079 struct sockaddr_in6 sa6, mask6;
1080
1081 /*
1082 * Delete the interface route associated with the prefix.
1083 */
1084 bzero(&sa6, sizeof(sa6));
1085 sa6.sin6_family = AF_INET6;
1086 sa6.sin6_len = sizeof(sa6);
1087 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
1088 sizeof(struct in6_addr));
1089 bzero(&mask6, sizeof(mask6));
1090 mask6.sin6_family = AF_INET6;
1091 mask6.sin6_len = sizeof(sa6);
1092 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
1093 {
1094 int e;
1095
1096 e = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
1097 (struct sockaddr *)&mask6, 0, NULL);
1098 if (e) {
1099 log(LOG_ERR,
1100 "nd6_detach_prefix: failed to delete route: "
1101 "%s/%d (errno = %d)\n",
1102 ip6_sprintf(&sa6.sin6_addr),
1103 pr->ndpr_plen,
1104 e);
1105 }
1106 }
1107
1108 /*
1109 * Mark the address derived from the prefix detached so that
1110 * it won't be used as a source address for a new connection.
1111 */
1112 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1113 ia6 = NULL;
1114 else
1115 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1116 if (ia6)
1117 ia6->ia6_flags |= IN6_IFF_DETACHED;
1118 }
1119
1120 static void
1121 nd6_attach_prefix(pr)
1122 struct nd_prefix *pr;
1123 {
1124 struct ifaddr *ifa;
1125 struct in6_ifaddr *ia6;
1126
1127 /*
1128 * Add the interface route associated with the prefix(if necessary)
1129 * Should we consider if the L bit is set in pr->ndpr_flags?
1130 */
1131 ifa = ifaof_ifpforaddr((struct sockaddr *)&pr->ndpr_prefix,
1132 pr->ndpr_ifp);
1133 if (ifa == NULL) {
1134 log(LOG_ERR,
1135 "nd6_attach_prefix: failed to find any ifaddr"
1136 " to add route for a prefix(%s/%d)\n",
1137 ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen);
1138 }
1139 else {
1140 int e;
1141 struct sockaddr_in6 mask6;
1142
1143 bzero(&mask6, sizeof(mask6));
1144 mask6.sin6_family = AF_INET6;
1145 mask6.sin6_len = sizeof(mask6);
1146 mask6.sin6_addr = pr->ndpr_mask;
1147 e = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
1148 ifa->ifa_addr, (struct sockaddr *)&mask6,
1149 ifa->ifa_flags, NULL);
1150 if (e == 0)
1151 pr->ndpr_statef_onlink = 1;
1152 else {
1153 log(LOG_ERR,
1154 "nd6_attach_prefix: failed to add route for"
1155 " a prefix(%s/%d), errno = %d\n",
1156 ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen, e);
1157 }
1158 }
1159
1160 /*
1161 * Now the address derived from the prefix can be used as a source
1162 * for a new connection, so clear the detached flag.
1163 */
1164 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1165 ia6 = NULL;
1166 else
1167 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
1168 if (ia6) {
1169 ia6->ia6_flags &= ~IN6_IFF_DETACHED;
1170 if (pr->ndpr_statef_onlink)
1171 ia6->ia_flags |= IFA_ROUTE;
1172 }
1173 }
1174
1175 static struct in6_ifaddr *
1176 in6_ifadd(ifp, in6, addr, prefixlen)
1177 struct ifnet *ifp;
1178 struct in6_addr *in6;
1179 struct in6_addr *addr;
1180 int prefixlen; /* prefix len of the new prefix in "in6" */
1181 {
1182 struct ifaddr *ifa;
1183 struct in6_ifaddr *ia, *ib, *oia;
1184 int s, error;
1185 struct in6_addr mask;
1186
1187 in6_len2mask(&mask, prefixlen);
1188
1189 /* find link-local address (will be interface ID) */
1190 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */
1191 if (ifa)
1192 ib = (struct in6_ifaddr *)ifa;
1193 else
1194 return NULL;
1195
1196 #if 0 /* don't care link local addr state, and always do DAD */
1197 /* if link-local address is not eligible, do not autoconfigure. */
1198 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
1199 printf("in6_ifadd: link-local address not ready\n");
1200 return NULL;
1201 }
1202 #endif
1203
1204 /* prefixlen + ifidlen must be equal to 128 */
1205 if (prefixlen != in6_mask2len(&ib->ia_prefixmask.sin6_addr)) {
1206 log(LOG_ERR, "in6_ifadd: wrong prefixlen for %s"
1207 "(prefix=%d ifid=%d)\n", if_name(ifp),
1208 prefixlen,
1209 128 - in6_mask2len(&ib->ia_prefixmask.sin6_addr));
1210 return NULL;
1211 }
1212
1213 /* make ifaddr */
1214 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_DONTWAIT);
1215 if (ia == NULL) {
1216 printf("ENOBUFS in in6_ifadd %d\n", __LINE__);
1217 return NULL;
1218 }
1219
1220 bzero((caddr_t)ia, sizeof(*ia));
1221 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1222 if (ifp->if_flags & IFF_POINTOPOINT)
1223 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1224 else
1225 ia->ia_ifa.ifa_dstaddr = NULL;
1226 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1227 ia->ia_ifp = ifp;
1228
1229 /* link to in6_ifaddr */
1230 if ((oia = in6_ifaddr) != NULL) {
1231 for( ; oia->ia_next; oia = oia->ia_next)
1232 continue;
1233 oia->ia_next = ia;
1234 } else
1235 in6_ifaddr = ia;
1236 IFAREF((struct ifaddr *)ia);
1237
1238 /* link to if_addrlist */
1239 if (ifp->if_addrlist.tqh_first != NULL) {
1240 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
1241 ifa_list);
1242 IFAREF((struct ifaddr *)ia);
1243 }
1244 #if 0
1245 else {
1246 /*
1247 * this should not be the case because there is at least one
1248 * link-local address(see the beginning of the function).
1249 */
1250 TAILQ_INIT(&ifp->if_addrlist);
1251 }
1252 #endif
1253
1254 /* new address */
1255 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
1256 ia->ia_addr.sin6_family = AF_INET6;
1257 /* prefix */
1258 bcopy(in6, &ia->ia_addr.sin6_addr, sizeof(ia->ia_addr.sin6_addr));
1259 ia->ia_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
1260 ia->ia_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
1261 ia->ia_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
1262 ia->ia_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1263 /* interface ID */
1264 ia->ia_addr.sin6_addr.s6_addr32[0]
1265 |= (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1266 ia->ia_addr.sin6_addr.s6_addr32[1]
1267 |= (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1268 ia->ia_addr.sin6_addr.s6_addr32[2]
1269 |= (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1270 ia->ia_addr.sin6_addr.s6_addr32[3]
1271 |= (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1272
1273 /* new prefix */
1274 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1275 ia->ia_prefixmask.sin6_family = AF_INET6;
1276 bcopy(&mask, &ia->ia_prefixmask.sin6_addr,
1277 sizeof(ia->ia_prefixmask.sin6_addr));
1278
1279 /* same routine */
1280 ia->ia_ifa.ifa_rtrequest =
1281 (ifp->if_type == IFT_PPP) ? nd6_p2p_rtrequest : nd6_rtrequest;
1282 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1283 ia->ia_ifa.ifa_metric = ifp->if_metric;
1284
1285 /* add interface route */
1286 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP|RTF_CLONING))) {
1287 log(LOG_NOTICE, "in6_ifadd: failed to add an interface route "
1288 "for %s/%d on %s, errno = %d\n",
1289 ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen,
1290 if_name(ifp), error);
1291 }
1292 else
1293 ia->ia_flags |= IFA_ROUTE;
1294
1295 *addr = ia->ia_addr.sin6_addr;
1296
1297 if (ifp->if_flags & IFF_MULTICAST) {
1298 int error; /* not used */
1299 struct in6_addr sol6;
1300
1301 /* Restore saved multicast addresses(if any). */
1302 in6_restoremkludge(ia, ifp);
1303
1304 /* join solicited node multicast address */
1305 bzero(&sol6, sizeof(sol6));
1306 sol6.s6_addr16[0] = htons(0xff02);
1307 sol6.s6_addr16[1] = htons(ifp->if_index);
1308 sol6.s6_addr32[1] = 0;
1309 sol6.s6_addr32[2] = htonl(1);
1310 sol6.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
1311 sol6.s6_addr8[12] = 0xff;
1312 (void)in6_addmulti(&sol6, ifp, &error);
1313 }
1314
1315 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1316
1317 /*
1318 * To make the interface up. Only AF_INET6 in ia is used...
1319 */
1320 s = splimp();
1321 if (ifp->if_ioctl && (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) {
1322 splx(s);
1323 return NULL;
1324 }
1325 splx(s);
1326
1327 /* Perform DAD, if needed. */
1328 nd6_dad_start((struct ifaddr *)ia, NULL);
1329
1330 return ia;
1331 }
1332
1333 int
1334 in6_ifdel(ifp, in6)
1335 struct ifnet *ifp;
1336 struct in6_addr *in6;
1337 {
1338 struct in6_ifaddr *ia = (struct in6_ifaddr *)NULL;
1339 struct in6_ifaddr *oia = (struct in6_ifaddr *)NULL;
1340
1341 if (!ifp)
1342 return -1;
1343
1344 ia = in6ifa_ifpwithaddr(ifp, in6);
1345 if (!ia)
1346 return -1;
1347
1348 if (ifp->if_flags & IFF_MULTICAST) {
1349 /*
1350 * delete solicited multicast addr for deleting host id
1351 */
1352 struct in6_multi *in6m;
1353 struct in6_addr llsol;
1354 bzero(&llsol, sizeof(struct in6_addr));
1355 llsol.s6_addr16[0] = htons(0xff02);
1356 llsol.s6_addr16[1] = htons(ifp->if_index);
1357 llsol.s6_addr32[1] = 0;
1358 llsol.s6_addr32[2] = htonl(1);
1359 llsol.s6_addr32[3] =
1360 ia->ia_addr.sin6_addr.s6_addr32[3];
1361 llsol.s6_addr8[12] = 0xff;
1362
1363 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1364 if (in6m)
1365 in6_delmulti(in6m);
1366 }
1367
1368 if (ia->ia_flags & IFA_ROUTE) {
1369 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1370 ia->ia_flags &= ~IFA_ROUTE;
1371 }
1372
1373 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
1374 IFAFREE(&ia->ia_ifa);
1375
1376 /* lladdr is never deleted */
1377 oia = ia;
1378 if (oia == (ia = in6_ifaddr))
1379 in6_ifaddr = ia->ia_next;
1380 else {
1381 while (ia->ia_next && (ia->ia_next != oia))
1382 ia = ia->ia_next;
1383 if (ia->ia_next)
1384 ia->ia_next = oia->ia_next;
1385 else
1386 return -1;
1387 }
1388
1389 in6_savemkludge(oia);
1390 IFAFREE((&oia->ia_ifa));
1391 /* xxx
1392 rtrequest(RTM_DELETE,
1393 (struct sockaddr *)&ia->ia_addr,
1394 (struct sockaddr *)0
1395 (struct sockaddr *)&ia->ia_prefixmask,
1396 RTF_UP|RTF_CLONING,
1397 (struct rtentry **)0);
1398 */
1399 return 0;
1400 }
1401
1402 int
1403 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
1404 {
1405 long time_second = time.tv_sec;
1406
1407 /* check if preferred lifetime > valid lifetime */
1408 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
1409 log(LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
1410 "(%d) is greater than valid lifetime(%d)\n",
1411 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime);
1412 return (EINVAL);
1413 }
1414 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1415 ndpr->ndpr_preferred = 0;
1416 else
1417 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
1418 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1419 ndpr->ndpr_expire = 0;
1420 else
1421 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
1422
1423 return 0;
1424 }
1425
1426 static void
1427 in6_init_address_ltimes(struct nd_prefix *new,
1428 struct in6_addrlifetime *lt6,
1429 int update_vltime)
1430 {
1431 long time_second = time.tv_sec;
1432
1433 /* Valid lifetime must not be updated unless explicitly specified. */
1434 if (update_vltime) {
1435 /* init ia6t_expire */
1436 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1437 lt6->ia6t_expire = 0;
1438 else {
1439 lt6->ia6t_expire = time_second;
1440 lt6->ia6t_expire += lt6->ia6t_vltime;
1441 }
1442 /* Ensure addr lifetime <= prefix lifetime. */
1443 if (new->ndpr_expire && lt6->ia6t_expire &&
1444 new->ndpr_expire < lt6->ia6t_expire)
1445 lt6->ia6t_expire = new->ndpr_expire;
1446 }
1447
1448 /* init ia6t_preferred */
1449 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1450 lt6->ia6t_preferred = 0;
1451 else {
1452 lt6->ia6t_preferred = time_second;
1453 lt6->ia6t_preferred += lt6->ia6t_pltime;
1454 }
1455 /* Ensure addr lifetime <= prefix lifetime. */
1456 if (new->ndpr_preferred && lt6->ia6t_preferred
1457 && new->ndpr_preferred < lt6->ia6t_preferred)
1458 lt6->ia6t_preferred = new->ndpr_preferred;
1459 }
1460
1461 /*
1462 * Delete all the routing table entries that use the specified gateway.
1463 * XXX: this function causes search through all entries of routing table, so
1464 * it shouldn't be called when acting as a router.
1465 */
1466 void
1467 rt6_flush(gateway, ifp)
1468 struct in6_addr *gateway;
1469 struct ifnet *ifp;
1470 {
1471 struct radix_node_head *rnh = rt_tables[AF_INET6];
1472 int s = splsoftnet();
1473
1474 /* We'll care only link-local addresses */
1475 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1476 splx(s);
1477 return;
1478 }
1479 /* XXX: hack for KAME's link-local address kludge */
1480 gateway->s6_addr16[1] = htons(ifp->if_index);
1481
1482 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
1483 splx(s);
1484 }
1485
1486 static int
1487 rt6_deleteroute(rn, arg)
1488 struct radix_node *rn;
1489 void *arg;
1490 {
1491 #define SIN6(s) ((struct sockaddr_in6 *)s)
1492 struct rtentry *rt = (struct rtentry *)rn;
1493 struct in6_addr *gate = (struct in6_addr *)arg;
1494
1495 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
1496 return(0);
1497
1498 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
1499 return(0);
1500
1501 /*
1502 * We delete only host route. This means, in particular, we don't
1503 * delete default route.
1504 */
1505 if ((rt->rt_flags & RTF_HOST) == 0)
1506 return(0);
1507
1508 return(rtrequest(RTM_DELETE, rt_key(rt),
1509 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
1510 #undef SIN6
1511 }
1512
1513 int
1514 nd6_setdefaultiface(ifindex)
1515 int ifindex;
1516 {
1517 int error = 0;
1518
1519 if (ifindex < 0 || if_index < ifindex)
1520 return(EINVAL);
1521
1522 if (nd6_defifindex != ifindex) {
1523 nd6_defifindex = ifindex;
1524 if (nd6_defifindex > 0)
1525 nd6_defifp = ifindex2ifnet[nd6_defifindex];
1526 else
1527 nd6_defifp = NULL;
1528
1529 /*
1530 * If the Default Router List is empty, install a route
1531 * to the specified interface as default or remove the default
1532 * route when the default interface becomes canceled.
1533 * The check for the queue is actually redundant, but
1534 * we do this here to avoid re-install the default route
1535 * if the list is NOT empty.
1536 */
1537 if (TAILQ_FIRST(&nd_defrouter) == NULL)
1538 defrouter_select();
1539 }
1540
1541 return(error);
1542 }
1543