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