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