nd6.c revision 1.20 1 /* $NetBSD: nd6.c,v 1.20 2000/03/23 07:03:30 thorpej Exp $ */
2 /* $KAME: nd6.c,v 1.41 2000/02/24 16:34:50 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 /*
34 * XXX
35 * KAME 970409 note:
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/protosw.h>
51 #include <sys/errno.h>
52 #include <sys/ioctl.h>
53 #include <sys/syslog.h>
54 #include <sys/queue.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_atm.h>
60 #include <net/route.h>
61
62 #include <netinet/in.h>
63 #include <net/if_ether.h>
64 #include <netinet/if_inarp.h>
65 #include <net/if_fddi.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet6/in6_prefix.h>
71 #include <netinet/icmp6.h>
72
73 #include "loop.h"
74 extern struct ifnet loif[NLOOP];
75
76 #include <net/net_osdep.h>
77
78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
80
81 #define SIN6(s) ((struct sockaddr_in6 *)s)
82 #define SDL(s) ((struct sockaddr_dl *)s)
83
84 /* timer values */
85 int nd6_prune = 1; /* walk list every 1 seconds */
86 int nd6_delay = 5; /* delay first probe time 5 second */
87 int nd6_umaxtries = 3; /* maximum unicast query */
88 int nd6_mmaxtries = 3; /* maximum multicast query */
89 int nd6_useloopback = 1; /* use loopback interface for local traffic */
90
91 /* preventing too many loops in ND option parsing */
92 int nd6_maxndopt = 10; /* max # of ND options allowed */
93
94 /* for debugging? */
95 static int nd6_inuse, nd6_allocated;
96
97 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
98 struct nd_ifinfo *nd_ifinfo = NULL;
99 struct nd_drhead nd_defrouter;
100 struct nd_prhead nd_prefix = { 0 };
101
102 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
103 static struct sockaddr_in6 all1_sa;
104
105 static void nd6_slowtimo __P((void *));
106
107 struct callout nd6_slowtimo_ch;
108 struct callout nd6_timer_ch;
109
110 void
111 nd6_init()
112 {
113 static int nd6_init_done = 0;
114 int i;
115
116 if (nd6_init_done) {
117 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
118 return;
119 }
120
121 all1_sa.sin6_family = AF_INET6;
122 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
123 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
124 all1_sa.sin6_addr.s6_addr[i] = 0xff;
125
126 /* initialization of the default router list */
127 TAILQ_INIT(&nd_defrouter);
128
129 nd6_init_done = 1;
130
131 /* start timer */
132 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
133 nd6_slowtimo, NULL);
134 }
135
136 void
137 nd6_ifattach(ifp)
138 struct ifnet *ifp;
139 {
140 static size_t if_indexlim = 8;
141
142 /*
143 * We have some arrays that should be indexed by if_index.
144 * since if_index will grow dynamically, they should grow too.
145 */
146 if (nd_ifinfo == NULL || if_index >= if_indexlim) {
147 size_t n;
148 caddr_t q;
149
150 while (if_index >= if_indexlim)
151 if_indexlim <<= 1;
152
153 /* grow nd_ifinfo */
154 n = if_indexlim * sizeof(struct nd_ifinfo);
155 q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
156 bzero(q, n);
157 if (nd_ifinfo) {
158 bcopy((caddr_t)nd_ifinfo, q, n/2);
159 free((caddr_t)nd_ifinfo, M_IP6NDP);
160 }
161 nd_ifinfo = (struct nd_ifinfo *)q;
162 }
163
164 #define ND nd_ifinfo[ifp->if_index]
165 ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
166 ND.chlim = IPV6_DEFHLIM;
167 ND.basereachable = REACHABLE_TIME;
168 ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
169 ND.retrans = RETRANS_TIMER;
170 ND.receivedra = 0;
171 nd6_setmtu(ifp);
172 #undef ND
173 }
174
175 /*
176 * Reset ND level link MTU. This function is called when the physical MTU
177 * changes, which means we might have to adjust the ND level MTU.
178 */
179 void
180 nd6_setmtu(ifp)
181 struct ifnet *ifp;
182 {
183 #define MIN(a,b) ((a) < (b) ? (a) : (b))
184 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
185 u_long oldmaxmtu = ndi->maxmtu;
186 u_long oldlinkmtu = ndi->linkmtu;
187
188 switch(ifp->if_type) {
189 case IFT_ARCNET: /* XXX MTU handling needs more work */
190 ndi->maxmtu = MIN(60480, ifp->if_mtu);
191 break;
192 case IFT_ETHER:
193 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
194 break;
195 case IFT_ATM:
196 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
197 break;
198 default:
199 ndi->maxmtu = ifp->if_mtu;
200 break;
201 }
202
203 if (oldmaxmtu != ndi->maxmtu) {
204 /*
205 * If the ND level MTU is not set yet, or if the maxmtu
206 * is reset to a smaller value than the ND level MTU,
207 * also reset the ND level MTU.
208 */
209 if (ndi->linkmtu == 0 ||
210 ndi->maxmtu < ndi->linkmtu) {
211 ndi->linkmtu = ndi->maxmtu;
212 /* also adjust in6_maxmtu if necessary. */
213 if (oldlinkmtu == 0) {
214 /*
215 * XXX: the case analysis is grotty, but
216 * it is not efficient to call in6_setmaxmtu()
217 * here when we are during the initialization
218 * procedure.
219 */
220 if (in6_maxmtu < ndi->linkmtu)
221 in6_maxmtu = ndi->linkmtu;
222 }
223 else
224 in6_setmaxmtu();
225 }
226 }
227 #undef MIN
228 }
229
230 void
231 nd6_option_init(opt, icmp6len, ndopts)
232 void *opt;
233 int icmp6len;
234 union nd_opts *ndopts;
235 {
236 bzero(ndopts, sizeof(*ndopts));
237 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
238 ndopts->nd_opts_last
239 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
240
241 if (icmp6len == 0) {
242 ndopts->nd_opts_done = 1;
243 ndopts->nd_opts_search = NULL;
244 }
245 }
246
247 /*
248 * Take one ND option.
249 */
250 struct nd_opt_hdr *
251 nd6_option(ndopts)
252 union nd_opts *ndopts;
253 {
254 struct nd_opt_hdr *nd_opt;
255 int olen;
256
257 if (!ndopts)
258 panic("ndopts == NULL in nd6_option\n");
259 if (!ndopts->nd_opts_last)
260 panic("uninitialized ndopts in nd6_option\n");
261 if (!ndopts->nd_opts_search)
262 return NULL;
263 if (ndopts->nd_opts_done)
264 return NULL;
265
266 nd_opt = ndopts->nd_opts_search;
267
268 olen = nd_opt->nd_opt_len << 3;
269 if (olen == 0) {
270 /*
271 * Message validation requires that all included
272 * options have a length that is greater than zero.
273 */
274 bzero(ndopts, sizeof(*ndopts));
275 return NULL;
276 }
277
278 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
279 if (!(ndopts->nd_opts_search < ndopts->nd_opts_last)) {
280 ndopts->nd_opts_done = 1;
281 ndopts->nd_opts_search = NULL;
282 }
283 return nd_opt;
284 }
285
286 /*
287 * Parse multiple ND options.
288 * This function is much easier to use, for ND routines that do not need
289 * multiple options of the same type.
290 */
291 int
292 nd6_options(ndopts)
293 union nd_opts *ndopts;
294 {
295 struct nd_opt_hdr *nd_opt;
296 int i = 0;
297
298 if (!ndopts)
299 panic("ndopts == NULL in nd6_options\n");
300 if (!ndopts->nd_opts_last)
301 panic("uninitialized ndopts in nd6_options\n");
302 if (!ndopts->nd_opts_search)
303 return 0;
304
305 while (1) {
306 nd_opt = nd6_option(ndopts);
307 if (!nd_opt && !ndopts->nd_opts_last) {
308 /*
309 * Message validation requires that all included
310 * options have a length that is greater than zero.
311 */
312 bzero(ndopts, sizeof(*ndopts));
313 return -1;
314 }
315
316 if (!nd_opt)
317 goto skip1;
318
319 switch (nd_opt->nd_opt_type) {
320 case ND_OPT_SOURCE_LINKADDR:
321 case ND_OPT_TARGET_LINKADDR:
322 case ND_OPT_MTU:
323 case ND_OPT_REDIRECTED_HEADER:
324 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
325 printf("duplicated ND6 option found "
326 "(type=%d)\n", nd_opt->nd_opt_type);
327 /* XXX bark? */
328 } else {
329 ndopts->nd_opt_array[nd_opt->nd_opt_type]
330 = nd_opt;
331 }
332 break;
333 case ND_OPT_PREFIX_INFORMATION:
334 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
335 ndopts->nd_opt_array[nd_opt->nd_opt_type]
336 = nd_opt;
337 }
338 ndopts->nd_opts_pi_end =
339 (struct nd_opt_prefix_info *)nd_opt;
340 break;
341 default:
342 /*
343 * Unknown options must be silently ignored,
344 * to accomodate future extension to the protocol.
345 */
346 log(LOG_INFO,
347 "nd6_options: unsupported option %d - "
348 "option ignored\n", nd_opt->nd_opt_type);
349 }
350
351 skip1:
352 i++;
353 if (i > nd6_maxndopt) {
354 icmp6stat.icp6s_nd_toomanyopt++;
355 printf("too many loop in nd opt\n");
356 break;
357 }
358
359 if (ndopts->nd_opts_done)
360 break;
361 }
362
363 return 0;
364 }
365
366 /*
367 * ND6 timer routine to expire default route list and prefix list
368 */
369 void
370 nd6_timer(ignored_arg)
371 void *ignored_arg;
372 {
373 int s;
374 register struct llinfo_nd6 *ln;
375 register struct nd_defrouter *dr;
376 register struct nd_prefix *pr;
377 long time_second = time.tv_sec;
378
379 s = splsoftnet();
380 callout_reset(&nd6_timer_ch, nd6_prune * hz,
381 nd6_timer, NULL);
382
383 ln = llinfo_nd6.ln_next;
384 /* XXX BSD/OS separates this code -- itojun */
385 while (ln && ln != &llinfo_nd6) {
386 struct rtentry *rt;
387 struct ifnet *ifp;
388 struct sockaddr_in6 *dst;
389 struct llinfo_nd6 *next = ln->ln_next;
390
391 if ((rt = ln->ln_rt) == NULL) {
392 ln = next;
393 continue;
394 }
395 if ((ifp = rt->rt_ifp) == NULL) {
396 ln = next;
397 continue;
398 }
399 dst = (struct sockaddr_in6 *)rt_key(rt);
400
401 if (ln->ln_expire > time_second) {
402 ln = next;
403 continue;
404 }
405
406 /* sanity check */
407 if (!rt)
408 panic("rt=0 in nd6_timer(ln=%p)\n", ln);
409 if (!dst)
410 panic("dst=0 in nd6_timer(ln=%p)\n", ln);
411
412 switch (ln->ln_state) {
413 case ND6_LLINFO_INCOMPLETE:
414 if (ln->ln_asked < nd6_mmaxtries) {
415 ln->ln_asked++;
416 ln->ln_expire = time_second +
417 nd_ifinfo[ifp->if_index].retrans / 1000;
418 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
419 ln, 0);
420 } else {
421 struct mbuf *m = ln->ln_hold;
422 if (m) {
423 if (rt->rt_ifp) {
424 /*
425 * Fake rcvif to make ICMP error
426 * more helpful in diagnosing
427 * for the receiver.
428 * XXX: should we consider
429 * older rcvif?
430 */
431 m->m_pkthdr.rcvif = rt->rt_ifp;
432 }
433 icmp6_error(m, ICMP6_DST_UNREACH,
434 ICMP6_DST_UNREACH_ADDR, 0);
435 ln->ln_hold = NULL;
436 }
437 nd6_free(rt);
438 }
439 break;
440 case ND6_LLINFO_REACHABLE:
441 if (ln->ln_expire)
442 ln->ln_state = ND6_LLINFO_STALE;
443 break;
444 /*
445 * ND6_LLINFO_STALE state requires nothing for timer
446 * routine.
447 */
448 case ND6_LLINFO_DELAY:
449 ln->ln_asked = 1;
450 ln->ln_state = ND6_LLINFO_PROBE;
451 ln->ln_expire = time_second +
452 nd_ifinfo[ifp->if_index].retrans / 1000;
453 nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr,
454 ln, 0);
455 break;
456
457 case ND6_LLINFO_PROBE:
458 if (ln->ln_asked < nd6_umaxtries) {
459 ln->ln_asked++;
460 ln->ln_expire = time_second +
461 nd_ifinfo[ifp->if_index].retrans / 1000;
462 nd6_ns_output(ifp, &dst->sin6_addr,
463 &dst->sin6_addr, ln, 0);
464 } else {
465 nd6_free(rt);
466 }
467 break;
468 case ND6_LLINFO_WAITDELETE:
469 nd6_free(rt);
470 break;
471 }
472 ln = next;
473 }
474
475 /* expire */
476 dr = TAILQ_FIRST(&nd_defrouter);
477 while (dr) {
478 if (dr->expire && dr->expire < time_second) {
479 struct nd_defrouter *t;
480 t = TAILQ_NEXT(dr, dr_entry);
481 defrtrlist_del(dr);
482 dr = t;
483 } else
484 dr = TAILQ_NEXT(dr, dr_entry);
485 }
486 pr = nd_prefix.lh_first;
487 while (pr) {
488 struct in6_ifaddr *ia6;
489 struct in6_addrlifetime *lt6;
490
491 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
492 ia6 = NULL;
493 else
494 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
495
496 if (ia6) {
497 /* check address lifetime */
498 lt6 = &ia6->ia6_lifetime;
499 if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
500 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
501 if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
502 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
503 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
504 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
505 }
506 }
507
508 /*
509 * check prefix lifetime.
510 * since pltime is just for autoconf, pltime processing for
511 * prefix is not necessary.
512 *
513 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
514 * can use the old prefix information to validate the
515 * next prefix information to come. See prelist_update()
516 * for actual validation.
517 */
518 if (pr->ndpr_expire
519 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
520 struct nd_prefix *t;
521 t = pr->ndpr_next;
522
523 /*
524 * address expiration and prefix expiration are
525 * separate. NEVER perform in6_ifdel here.
526 */
527
528 prelist_remove(pr);
529 pr = t;
530 } else
531 pr = pr->ndpr_next;
532 }
533 splx(s);
534 }
535
536 /*
537 * Nuke neighbor cache/prefix/default router management table, right before
538 * ifp goes away.
539 */
540 void
541 nd6_purge(ifp)
542 struct ifnet *ifp;
543 {
544 struct llinfo_nd6 *ln, *nln;
545 struct nd_defrouter *dr, *ndr, drany;
546 struct nd_prefix *pr, *npr;
547
548 /* Nuke default router list entries toward ifp */
549 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
550 /*
551 * The first entry of the list may be stored in
552 * the routing table, so we'll delete it later.
553 */
554 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
555 ndr = TAILQ_NEXT(dr, dr_entry);
556 if (dr->ifp == ifp)
557 defrtrlist_del(dr);
558 }
559 dr = TAILQ_FIRST(&nd_defrouter);
560 if (dr->ifp == ifp)
561 defrtrlist_del(dr);
562 }
563
564 /* Nuke prefix list entries toward ifp */
565 for (pr = nd_prefix.lh_first; pr; pr = npr) {
566 npr = pr->ndpr_next;
567 if (pr->ndpr_ifp == ifp) {
568 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
569 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
570 prelist_remove(pr);
571 }
572 }
573
574 /* cancel default outgoing interface setting */
575 if (nd6_defifindex == ifp->if_index)
576 nd6_setdefaultiface(0);
577
578 /* refresh default router list */
579 bzero(&drany, sizeof(drany));
580 defrouter_delreq(&drany, 0);
581 defrouter_select();
582
583 /*
584 * Nuke neighbor cache entries for the ifp.
585 * Note that rt->rt_ifp may not be the same as ifp,
586 * due to KAME goto ours hack. See RTM_RESOLVE case in
587 * nd6_rtrequest(), and ip6_input().
588 */
589 ln = llinfo_nd6.ln_next;
590 while (ln && ln != &llinfo_nd6) {
591 struct rtentry *rt;
592 struct sockaddr_dl *sdl;
593
594 nln = ln->ln_next;
595 rt = ln->ln_rt;
596 if (rt && rt->rt_gateway &&
597 rt->rt_gateway->sa_family == AF_LINK) {
598 sdl = (struct sockaddr_dl *)rt->rt_gateway;
599 if (sdl->sdl_index == ifp->if_index)
600 nd6_free(rt);
601 }
602 ln = nln;
603 }
604
605 /*
606 * Neighbor cache entry for interface route will be retained
607 * with ND6_LLINFO_WAITDELETE state, by nd6_free(). Nuke it.
608 */
609 ln = llinfo_nd6.ln_next;
610 while (ln && ln != &llinfo_nd6) {
611 struct rtentry *rt;
612 struct sockaddr_dl *sdl;
613
614 nln = ln->ln_next;
615 rt = ln->ln_rt;
616 if (rt && rt->rt_gateway &&
617 rt->rt_gateway->sa_family == AF_LINK) {
618 sdl = (struct sockaddr_dl *)rt->rt_gateway;
619 if (sdl->sdl_index == ifp->if_index) {
620 rtrequest(RTM_DELETE, rt_key(rt),
621 (struct sockaddr *)0, rt_mask(rt), 0,
622 (struct rtentry **)0);
623 }
624 }
625 ln = nln;
626 }
627 }
628
629 struct rtentry *
630 nd6_lookup(addr6, create, ifp)
631 struct in6_addr *addr6;
632 int create;
633 struct ifnet *ifp;
634 {
635 struct rtentry *rt;
636 struct sockaddr_in6 sin6;
637
638 bzero(&sin6, sizeof(sin6));
639 sin6.sin6_len = sizeof(struct sockaddr_in6);
640 sin6.sin6_family = AF_INET6;
641 sin6.sin6_addr = *addr6;
642 rt = rtalloc1((struct sockaddr *)&sin6, create);
643 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
644 /*
645 * This is the case for the default route.
646 * If we want to create a neighbor cache for the address, we
647 * should free the route for the destination and allocate an
648 * interface route.
649 */
650 if (create) {
651 RTFREE(rt);
652 rt = 0;
653 }
654 }
655 if (!rt) {
656 if (create && ifp) {
657 /*
658 * If no route is available and create is set,
659 * we allocate a host route for the destination
660 * and treat it like an interface route.
661 * This hack is necessary for a neighbor which can't
662 * be covered by our own prefix.
663 */
664 struct ifaddr *ifa =
665 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
666 if (ifa == NULL)
667 return(NULL);
668
669 /*
670 * Create a new route. RTF_LLINFO is necessary
671 * to create a Neighbor Cache entry for the
672 * destination in nd6_rtrequest which will be
673 * called in rtequest via ifa->ifa_rtrequest.
674 */
675 if (rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
676 ifa->ifa_addr,
677 (struct sockaddr *)&all1_sa,
678 (ifa->ifa_flags |
679 RTF_HOST | RTF_LLINFO) & ~RTF_CLONING,
680 &rt))
681 log(LOG_ERR,
682 "nd6_lookup: failed to add route for a "
683 "neighbor(%s)\n", ip6_sprintf(addr6));
684 if (rt == NULL)
685 return(NULL);
686 if (rt->rt_llinfo) {
687 struct llinfo_nd6 *ln =
688 (struct llinfo_nd6 *)rt->rt_llinfo;
689 ln->ln_state = ND6_LLINFO_NOSTATE;
690 }
691 }
692 else
693 return(NULL);
694 }
695 rt->rt_refcnt--;
696 /*
697 * Validation for the entry.
698 * XXX: we can't use rt->rt_ifp to check for the interface, since
699 * it might be the loopback interface if the entry is for our
700 * own address on a non-loopback interface. Instead, we should
701 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
702 */
703 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
704 rt->rt_gateway->sa_family != AF_LINK ||
705 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
706 if (create) {
707 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
708 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
709 /* xxx more logs... kazu */
710 }
711 return(0);
712 }
713 return(rt);
714 }
715
716 /*
717 * Detect if a given IPv6 address identifies a neighbor on a given link.
718 * XXX: should take care of the destination of a p2p link?
719 */
720 int
721 nd6_is_addr_neighbor(addr, ifp)
722 struct in6_addr *addr;
723 struct ifnet *ifp;
724 {
725 register struct ifaddr *ifa;
726 int i;
727
728 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
729 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
730
731 /* A link-local address is always a neighbor. */
732 if (IN6_IS_ADDR_LINKLOCAL(addr))
733 return(1);
734
735 /*
736 * If the address matches one of our addresses,
737 * it should be a neighbor.
738 */
739 for (ifa = ifp->if_addrlist.tqh_first;
740 ifa;
741 ifa = ifa->ifa_list.tqe_next)
742 {
743 if (ifa->ifa_addr->sa_family != AF_INET6)
744 next: continue;
745
746 for (i = 0; i < 4; i++) {
747 if ((IFADDR6(ifa).s6_addr32[i] ^ addr->s6_addr32[i]) &
748 IFMASK6(ifa).s6_addr32[i])
749 goto next;
750 }
751 return(1);
752 }
753
754 /*
755 * Even if the address matches none of our addresses, it might be
756 * in the neighbor cache.
757 */
758 if (nd6_lookup(addr, 0, ifp))
759 return(1);
760
761 return(0);
762 #undef IFADDR6
763 #undef IFMASK6
764 }
765
766 /*
767 * Free an nd6 llinfo entry.
768 */
769 void
770 nd6_free(rt)
771 struct rtentry *rt;
772 {
773 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
774 struct sockaddr_dl *sdl;
775 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
776 struct nd_defrouter *dr;
777
778 /*
779 * Clear all destination cache entries for the neighbor.
780 * XXX: is it better to restrict this to hosts?
781 */
782 pfctlinput(PRC_HOSTDEAD, rt_key(rt));
783
784 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
785 int s;
786 s = splsoftnet();
787 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
788 rt->rt_ifp);
789 if (ln->ln_router || dr) {
790 /*
791 * rt6_flush must be called whether or not the neighbor
792 * is in the Default Router List.
793 * See a corresponding comment in nd6_na_input().
794 */
795 rt6_flush(&in6, rt->rt_ifp);
796 }
797
798 if (dr) {
799 /*
800 * Unreachablity of a router might affect the default
801 * router selection and on-link detection of advertised
802 * prefixes.
803 */
804
805 /*
806 * Temporarily fake the state to choose a new default
807 * router and to perform on-link determination of
808 * prefixes coreectly.
809 * Below the state will be set correctly,
810 * or the entry itself will be deleted.
811 */
812 ln->ln_state = ND6_LLINFO_INCOMPLETE;
813
814 if (dr == TAILQ_FIRST(&nd_defrouter)) {
815 /*
816 * It is used as the current default router,
817 * so we have to move it to the end of the
818 * list and choose a new one.
819 * XXX: it is not very efficient if this is
820 * the only router.
821 */
822 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
823 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
824
825 defrouter_select();
826 }
827 pfxlist_onlink_check();
828 }
829 splx(s);
830 }
831
832 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
833 sdl->sdl_family == AF_LINK) {
834 sdl->sdl_alen = 0;
835 ln->ln_state = ND6_LLINFO_WAITDELETE;
836 ln->ln_asked = 0;
837 rt->rt_flags &= ~RTF_REJECT;
838 return;
839 }
840
841 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
842 rt_mask(rt), 0, (struct rtentry **)0);
843 }
844
845 /*
846 * Upper-layer reachability hint for Neighbor Unreachability Detection.
847 *
848 * XXX cost-effective metods?
849 */
850 void
851 nd6_nud_hint(rt, dst6)
852 struct rtentry *rt;
853 struct in6_addr *dst6;
854 {
855 struct llinfo_nd6 *ln;
856 long time_second = time.tv_sec;
857
858 /*
859 * If the caller specified "rt", use that. Otherwise, resolve the
860 * routing table by supplied "dst6".
861 */
862 if (!rt) {
863 if (!dst6)
864 return;
865 if (!(rt = nd6_lookup(dst6, 0, NULL)))
866 return;
867 }
868
869 if ((rt->rt_flags & RTF_GATEWAY)
870 || (rt->rt_flags & RTF_LLINFO) == 0
871 || !rt->rt_llinfo
872 || !rt->rt_gateway
873 || rt->rt_gateway->sa_family != AF_LINK) {
874 /* This is not a host route. */
875 return;
876 }
877
878 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
879 if (ln->ln_state == ND6_LLINFO_INCOMPLETE)
880 return;
881
882 ln->ln_state = ND6_LLINFO_REACHABLE;
883 if (ln->ln_expire)
884 ln->ln_expire = time_second +
885 nd_ifinfo[rt->rt_ifp->if_index].reachable;
886 }
887
888 #ifdef OLDIP6OUTPUT
889 /*
890 * Resolve an IP6 address into an ethernet address. If success,
891 * desten is filled in. If there is no entry in ndptab,
892 * set one up and multicast a solicitation for the IP6 address.
893 * Hold onto this mbuf and resend it once the address
894 * is finally resolved. A return value of 1 indicates
895 * that desten has been filled in and the packet should be sent
896 * normally; a 0 return indicates that the packet has been
897 * taken over here, either now or for later transmission.
898 */
899 int
900 nd6_resolve(ifp, rt, m, dst, desten)
901 struct ifnet *ifp;
902 struct rtentry *rt;
903 struct mbuf *m;
904 struct sockaddr *dst;
905 u_char *desten;
906 {
907 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
908 struct sockaddr_dl *sdl;
909 long time_second = time.tv_sec;
910
911 if (m->m_flags & M_MCAST) {
912 switch (ifp->if_type) {
913 case IFT_ETHER:
914 case IFT_FDDI:
915 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
916 desten);
917 return(1);
918 break;
919 case IFT_ARCNET:
920 *desten = 0;
921 return(1);
922 break;
923 default:
924 return(0);
925 }
926 }
927 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
928 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
929 else {
930 if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
931 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
932 }
933 if (!ln || !rt) {
934 log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
935 ip6_sprintf(&(SIN6(dst)->sin6_addr)));
936 m_freem(m);
937 return(0);
938 }
939 sdl = SDL(rt->rt_gateway);
940 /*
941 * Ckeck the address family and length is valid, the address
942 * is resolved; otherwise, try to resolve.
943 */
944 if (ln->ln_state >= ND6_LLINFO_REACHABLE
945 && sdl->sdl_family == AF_LINK
946 && sdl->sdl_alen != 0) {
947 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
948 if (ln->ln_state == ND6_LLINFO_STALE) {
949 ln->ln_asked = 0;
950 ln->ln_state = ND6_LLINFO_DELAY;
951 ln->ln_expire = time_second + nd6_delay;
952 }
953 return(1);
954 }
955 /*
956 * There is an ndp entry, but no ethernet address
957 * response yet. Replace the held mbuf with this
958 * latest one.
959 *
960 * XXX Does the code conform to rate-limiting rule?
961 * (RFC 2461 7.2.2)
962 */
963 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
964 ln->ln_state == ND6_LLINFO_NOSTATE)
965 ln->ln_state = ND6_LLINFO_INCOMPLETE;
966 if (ln->ln_hold)
967 m_freem(ln->ln_hold);
968 ln->ln_hold = m;
969 if (ln->ln_expire) {
970 rt->rt_flags &= ~RTF_REJECT;
971 if (ln->ln_asked < nd6_mmaxtries &&
972 ln->ln_expire < time_second) {
973 ln->ln_asked++;
974 ln->ln_expire = time_second +
975 nd_ifinfo[ifp->if_index].retrans / 1000;
976 nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
977 ln, 0);
978 }
979 }
980 return(0);
981 }
982 #endif /* OLDIP6OUTPUT */
983
984 void
985 nd6_rtrequest(req, rt, sa)
986 int req;
987 struct rtentry *rt;
988 struct sockaddr *sa; /* xxx unused */
989 {
990 struct sockaddr *gate = rt->rt_gateway;
991 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
992 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
993 struct ifnet *ifp = rt->rt_ifp;
994 struct ifaddr *ifa;
995 long time_second = time.tv_sec;
996
997 if (rt->rt_flags & RTF_GATEWAY)
998 return;
999
1000 switch (req) {
1001 case RTM_ADD:
1002 /*
1003 * There is no backward compatibility :)
1004 *
1005 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1006 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1007 * rt->rt_flags |= RTF_CLONING;
1008 */
1009 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1010 /*
1011 * Case 1: This route should come from
1012 * a route to interface. RTF_LLINFO flag is set
1013 * for a host route whose destination should be
1014 * treated as on-link.
1015 */
1016 rt_setgate(rt, rt_key(rt),
1017 (struct sockaddr *)&null_sdl);
1018 gate = rt->rt_gateway;
1019 SDL(gate)->sdl_type = ifp->if_type;
1020 SDL(gate)->sdl_index = ifp->if_index;
1021 if (ln)
1022 ln->ln_expire = time_second;
1023 #if 1
1024 if (ln && ln->ln_expire == 0) {
1025 /* cludge for desktops */
1026 #if 0
1027 printf("nd6_request: time.tv_sec is zero; "
1028 "treat it as 1\n");
1029 #endif
1030 ln->ln_expire = 1;
1031 }
1032 #endif
1033 if (rt->rt_flags & RTF_CLONING)
1034 break;
1035 }
1036 /*
1037 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1038 * We don't do that here since llinfo is not ready yet.
1039 *
1040 * There are also couple of other things to be discussed:
1041 * - unsolicited NA code needs improvement beforehand
1042 * - RFC2461 says we MAY send multicast unsolicited NA
1043 * (7.2.6 paragraph 4), however, it also says that we
1044 * SHOULD provide a mechanism to prevent multicast NA storm.
1045 * we don't have anything like it right now.
1046 * note that the mechanism need a mutual agreement
1047 * between proxies, which means that we need to implement
1048 * a new protocol, or new kludge.
1049 * - from RFC2461 6.2.4, host MUST NOT send unsolicited NA.
1050 * we need to check ip6forwarding before sending it.
1051 * (or should we allow proxy ND configuration only for
1052 * routers? there's no mention about proxy ND from hosts)
1053 */
1054 #if 0
1055 /* XXX it does not work */
1056 if (rt->rt_flags & RTF_ANNOUNCE)
1057 nd6_na_output(ifp,
1058 &SIN6(rt_key(rt))->sin6_addr,
1059 &SIN6(rt_key(rt))->sin6_addr,
1060 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1061 1, NULL);
1062 #endif
1063 /* FALLTHROUGH */
1064 case RTM_RESOLVE:
1065 if (gate->sa_family != AF_LINK ||
1066 gate->sa_len < sizeof(null_sdl)) {
1067 log(LOG_DEBUG, "nd6_rtrequest: bad gateway value\n");
1068 break;
1069 }
1070 SDL(gate)->sdl_type = ifp->if_type;
1071 SDL(gate)->sdl_index = ifp->if_index;
1072 if (ln != 0)
1073 break; /* This happens on a route change */
1074 /*
1075 * Case 2: This route may come from cloning, or a manual route
1076 * add with a LL address.
1077 */
1078 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1079 rt->rt_llinfo = (caddr_t)ln;
1080 if (!ln) {
1081 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1082 break;
1083 }
1084 nd6_inuse++;
1085 nd6_allocated++;
1086 Bzero(ln, sizeof(*ln));
1087 ln->ln_rt = rt;
1088 /* this is required for "ndp" command. - shin */
1089 if (req == RTM_ADD) {
1090 /*
1091 * gate should have some valid AF_LINK entry,
1092 * and ln->ln_expire should have some lifetime
1093 * which is specified by ndp command.
1094 */
1095 ln->ln_state = ND6_LLINFO_REACHABLE;
1096 } else {
1097 /*
1098 * When req == RTM_RESOLVE, rt is created and
1099 * initialized in rtrequest(), so rt_expire is 0.
1100 */
1101 ln->ln_state = ND6_LLINFO_NOSTATE;
1102 ln->ln_expire = time_second;
1103 }
1104 rt->rt_flags |= RTF_LLINFO;
1105 ln->ln_next = llinfo_nd6.ln_next;
1106 llinfo_nd6.ln_next = ln;
1107 ln->ln_prev = &llinfo_nd6;
1108 ln->ln_next->ln_prev = ln;
1109
1110 /*
1111 * check if rt_key(rt) is one of my address assigned
1112 * to the interface.
1113 */
1114 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1115 &SIN6(rt_key(rt))->sin6_addr);
1116 if (ifa) {
1117 caddr_t macp = nd6_ifptomac(ifp);
1118 ln->ln_expire = 0;
1119 ln->ln_state = ND6_LLINFO_REACHABLE;
1120 if (macp) {
1121 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1122 SDL(gate)->sdl_alen = ifp->if_addrlen;
1123 }
1124 if (nd6_useloopback) {
1125 rt->rt_ifp = &loif[0]; /*XXX*/
1126 /*
1127 * Make sure rt_ifa be equal to the ifaddr
1128 * corresponding to the address.
1129 * We need this because when we refer
1130 * rt_ifa->ia6_flags in ip6_input, we assume
1131 * that the rt_ifa points to the address instead
1132 * of the loopback address.
1133 */
1134 if (ifa != rt->rt_ifa) {
1135 IFAFREE(rt->rt_ifa);
1136 IFAREF(ifa);
1137 rt->rt_ifa = ifa;
1138 }
1139 }
1140 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1141 ln->ln_expire = 0;
1142 ln->ln_state = ND6_LLINFO_REACHABLE;
1143
1144 /* join solicited node multicast for proxy ND */
1145 if (ifp->if_flags & IFF_MULTICAST) {
1146 struct in6_addr llsol;
1147 int error;
1148
1149 llsol = SIN6(rt_key(rt))->sin6_addr;
1150 llsol.s6_addr16[0] = htons(0xff02);
1151 llsol.s6_addr16[1] = htons(ifp->if_index);
1152 llsol.s6_addr32[1] = 0;
1153 llsol.s6_addr32[2] = htonl(1);
1154 llsol.s6_addr8[12] = 0xff;
1155
1156 (void)in6_addmulti(&llsol, ifp, &error);
1157 if (error)
1158 printf(
1159 "nd6_rtrequest: could not join solicited node multicast (errno=%d)\n", error);
1160 }
1161 }
1162 break;
1163
1164 case RTM_DELETE:
1165 if (!ln)
1166 break;
1167 /* leave from solicited node multicast for proxy ND */
1168 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1169 (ifp->if_flags & IFF_MULTICAST) != 0) {
1170 struct in6_addr llsol;
1171 struct in6_multi *in6m;
1172
1173 llsol = SIN6(rt_key(rt))->sin6_addr;
1174 llsol.s6_addr16[0] = htons(0xff02);
1175 llsol.s6_addr16[1] = htons(ifp->if_index);
1176 llsol.s6_addr32[1] = 0;
1177 llsol.s6_addr32[2] = htonl(1);
1178 llsol.s6_addr8[12] = 0xff;
1179
1180 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1181 if (in6m)
1182 in6_delmulti(in6m);
1183 }
1184 nd6_inuse--;
1185 ln->ln_next->ln_prev = ln->ln_prev;
1186 ln->ln_prev->ln_next = ln->ln_next;
1187 ln->ln_prev = NULL;
1188 rt->rt_llinfo = 0;
1189 rt->rt_flags &= ~RTF_LLINFO;
1190 if (ln->ln_hold)
1191 m_freem(ln->ln_hold);
1192 Free((caddr_t)ln);
1193 }
1194 }
1195
1196 void
1197 nd6_p2p_rtrequest(req, rt, sa)
1198 int req;
1199 struct rtentry *rt;
1200 struct sockaddr *sa; /* xxx unused */
1201 {
1202 struct sockaddr *gate = rt->rt_gateway;
1203 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1204 struct ifnet *ifp = rt->rt_ifp;
1205 struct ifaddr *ifa;
1206
1207 if (rt->rt_flags & RTF_GATEWAY)
1208 return;
1209
1210 switch (req) {
1211 case RTM_ADD:
1212 /*
1213 * There is no backward compatibility :)
1214 *
1215 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1216 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1217 * rt->rt_flags |= RTF_CLONING;
1218 */
1219 if (rt->rt_flags & RTF_CLONING) {
1220 /*
1221 * Case 1: This route should come from
1222 * a route to interface.
1223 */
1224 rt_setgate(rt, rt_key(rt),
1225 (struct sockaddr *)&null_sdl);
1226 gate = rt->rt_gateway;
1227 SDL(gate)->sdl_type = ifp->if_type;
1228 SDL(gate)->sdl_index = ifp->if_index;
1229 break;
1230 }
1231 /* Announce a new entry if requested. */
1232 if (rt->rt_flags & RTF_ANNOUNCE)
1233 nd6_na_output(ifp,
1234 &SIN6(rt_key(rt))->sin6_addr,
1235 &SIN6(rt_key(rt))->sin6_addr,
1236 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1237 1, NULL);
1238 /* FALLTHROUGH */
1239 case RTM_RESOLVE:
1240 /*
1241 * check if rt_key(rt) is one of my address assigned
1242 * to the interface.
1243 */
1244 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1245 &SIN6(rt_key(rt))->sin6_addr);
1246 if (ifa) {
1247 if (nd6_useloopback) {
1248 rt->rt_ifp = &loif[0]; /*XXX*/
1249 }
1250 }
1251 break;
1252 }
1253 }
1254
1255 int
1256 nd6_ioctl(cmd, data, ifp)
1257 u_long cmd;
1258 caddr_t data;
1259 struct ifnet *ifp;
1260 {
1261 struct in6_drlist *drl = (struct in6_drlist *)data;
1262 struct in6_prlist *prl = (struct in6_prlist *)data;
1263 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1264 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1265 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1266 struct nd_defrouter *dr, any;
1267 struct nd_prefix *pr;
1268 struct rtentry *rt;
1269 int i = 0, error = 0;
1270 int s;
1271
1272 switch (cmd) {
1273 case SIOCGDRLST_IN6:
1274 bzero(drl, sizeof(*drl));
1275 s = splsoftnet();
1276 dr = TAILQ_FIRST(&nd_defrouter);
1277 while (dr && i < DRLSTSIZ) {
1278 drl->defrouter[i].rtaddr = dr->rtaddr;
1279 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1280 /* XXX: need to this hack for KAME stack */
1281 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1282 }
1283 else
1284 log(LOG_ERR,
1285 "default router list contains a "
1286 "non-linklocal address(%s)\n",
1287 ip6_sprintf(&drl->defrouter[i].rtaddr));
1288
1289 drl->defrouter[i].flags = dr->flags;
1290 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1291 drl->defrouter[i].expire = dr->expire;
1292 drl->defrouter[i].if_index = dr->ifp->if_index;
1293 i++;
1294 dr = TAILQ_NEXT(dr, dr_entry);
1295 }
1296 splx(s);
1297 break;
1298 case SIOCGPRLST_IN6:
1299 /*
1300 * XXX meaning of fields, especialy "raflags", is very
1301 * differnet between RA prefix list and RR/static prefix list.
1302 * how about separating ioctls into two?
1303 */
1304 bzero(prl, sizeof(*prl));
1305 s = splsoftnet();
1306 pr = nd_prefix.lh_first;
1307 while (pr && i < PRLSTSIZ) {
1308 struct nd_pfxrouter *pfr;
1309 int j;
1310
1311 prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1312 prl->prefix[i].raflags = pr->ndpr_raf;
1313 prl->prefix[i].prefixlen = pr->ndpr_plen;
1314 prl->prefix[i].vltime = pr->ndpr_vltime;
1315 prl->prefix[i].pltime = pr->ndpr_pltime;
1316 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1317 prl->prefix[i].expire = pr->ndpr_expire;
1318
1319 pfr = pr->ndpr_advrtrs.lh_first;
1320 j = 0;
1321 while(pfr) {
1322 if (j < DRLSTSIZ) {
1323 #define RTRADDR prl->prefix[i].advrtr[j]
1324 RTRADDR = pfr->router->rtaddr;
1325 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1326 /* XXX: hack for KAME */
1327 RTRADDR.s6_addr16[1] = 0;
1328 }
1329 else
1330 log(LOG_ERR,
1331 "a router(%s) advertises "
1332 "a prefix with "
1333 "non-link local address\n",
1334 ip6_sprintf(&RTRADDR));
1335 #undef RTRADDR
1336 }
1337 j++;
1338 pfr = pfr->pfr_next;
1339 }
1340 prl->prefix[i].advrtrs = j;
1341 prl->prefix[i].origin = PR_ORIG_RA;
1342
1343 i++;
1344 pr = pr->ndpr_next;
1345 }
1346 {
1347 struct rr_prefix *rpp;
1348
1349 for (rpp = LIST_FIRST(&rr_prefix); rpp;
1350 rpp = LIST_NEXT(rpp, rp_entry)) {
1351 if (i >= PRLSTSIZ)
1352 break;
1353 prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
1354 prl->prefix[i].raflags = rpp->rp_raf;
1355 prl->prefix[i].prefixlen = rpp->rp_plen;
1356 prl->prefix[i].vltime = rpp->rp_vltime;
1357 prl->prefix[i].pltime = rpp->rp_pltime;
1358 prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1359 prl->prefix[i].expire = rpp->rp_expire;
1360 prl->prefix[i].advrtrs = 0;
1361 prl->prefix[i].origin = rpp->rp_origin;
1362 i++;
1363 }
1364 }
1365 splx(s);
1366
1367 break;
1368 case SIOCGIFINFO_IN6:
1369 ndi->ndi = nd_ifinfo[ifp->if_index];
1370 break;
1371 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1372 /* flush default router list */
1373 /*
1374 * xxx sumikawa: should not delete route if default
1375 * route equals to the top of default router list
1376 */
1377 bzero(&any, sizeof(any));
1378 defrouter_delreq(&any, 0);
1379 defrouter_select();
1380 /* xxx sumikawa: flush prefix list */
1381 break;
1382 case SIOCSPFXFLUSH_IN6:
1383 {
1384 /* flush all the prefix advertised by routers */
1385 struct nd_prefix *pr, *next;
1386
1387 s = splsoftnet();
1388 for (pr = nd_prefix.lh_first; pr; pr = next) {
1389 next = pr->ndpr_next;
1390 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1391 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1392 prelist_remove(pr);
1393 }
1394 splx(s);
1395 break;
1396 }
1397 case SIOCSRTRFLUSH_IN6:
1398 {
1399 /* flush all the default routers */
1400 struct nd_defrouter *dr, *next;
1401
1402 s = splsoftnet();
1403 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1404 /*
1405 * The first entry of the list may be stored in
1406 * the routing table, so we'll delete it later.
1407 */
1408 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1409 next = TAILQ_NEXT(dr, dr_entry);
1410 defrtrlist_del(dr);
1411 }
1412 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1413 }
1414 splx(s);
1415 break;
1416 }
1417 case SIOCGNBRINFO_IN6:
1418 {
1419 struct llinfo_nd6 *ln;
1420 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1421
1422 /*
1423 * XXX: KAME specific hack for scoped addresses
1424 * XXXX: for other scopes than link-local?
1425 */
1426 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1427 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1428 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1429
1430 if (*idp == 0)
1431 *idp = htons(ifp->if_index);
1432 }
1433
1434 s = splsoftnet();
1435 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1436 error = EINVAL;
1437 splx(s);
1438 break;
1439 }
1440 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1441 nbi->state = ln->ln_state;
1442 nbi->asked = ln->ln_asked;
1443 nbi->isrouter = ln->ln_router;
1444 nbi->expire = ln->ln_expire;
1445 splx(s);
1446
1447 break;
1448 }
1449 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1450 ndif->ifindex = nd6_defifindex;
1451 break;
1452 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1453 return(nd6_setdefaultiface(ndif->ifindex));
1454 break;
1455 }
1456 return(error);
1457 }
1458
1459 /*
1460 * Create neighbor cache entry and cache link-layer address,
1461 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1462 */
1463 struct rtentry *
1464 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1465 struct ifnet *ifp;
1466 struct in6_addr *from;
1467 char *lladdr;
1468 int lladdrlen;
1469 int type; /* ICMP6 type */
1470 int code; /* type dependent information */
1471 {
1472 struct rtentry *rt = NULL;
1473 struct llinfo_nd6 *ln = NULL;
1474 int is_newentry;
1475 struct sockaddr_dl *sdl = NULL;
1476 int do_update;
1477 int olladdr;
1478 int llchange;
1479 int newstate = 0;
1480 long time_second = time.tv_sec;
1481
1482 if (!ifp)
1483 panic("ifp == NULL in nd6_cache_lladdr");
1484 if (!from)
1485 panic("from == NULL in nd6_cache_lladdr");
1486
1487 /* nothing must be updated for unspecified address */
1488 if (IN6_IS_ADDR_UNSPECIFIED(from))
1489 return NULL;
1490
1491 /*
1492 * Validation about ifp->if_addrlen and lladdrlen must be done in
1493 * the caller.
1494 *
1495 * XXX If the link does not have link-layer adderss, what should
1496 * we do? (ifp->if_addrlen == 0)
1497 * Spec says nothing in sections for RA, RS and NA. There's small
1498 * description on it in NS section (RFC 2461 7.2.3).
1499 */
1500
1501 rt = nd6_lookup(from, 0, ifp);
1502 if (!rt) {
1503 #if 0
1504 /* nothing must be done if there's no lladdr */
1505 if (!lladdr || !lladdrlen)
1506 return NULL;
1507 #endif
1508
1509 rt = nd6_lookup(from, 1, ifp);
1510 is_newentry = 1;
1511 } else
1512 is_newentry = 0;
1513
1514 if (!rt)
1515 return NULL;
1516 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1517 fail:
1518 nd6_free(rt);
1519 return NULL;
1520 }
1521 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1522 if (!ln)
1523 goto fail;
1524 if (!rt->rt_gateway)
1525 goto fail;
1526 if (rt->rt_gateway->sa_family != AF_LINK)
1527 goto fail;
1528 sdl = SDL(rt->rt_gateway);
1529
1530 olladdr = (sdl->sdl_alen) ? 1 : 0;
1531 if (olladdr && lladdr) {
1532 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1533 llchange = 1;
1534 else
1535 llchange = 0;
1536 } else
1537 llchange = 0;
1538
1539 /*
1540 * newentry olladdr lladdr llchange (*=record)
1541 * 0 n n -- (1)
1542 * 0 y n -- (2)
1543 * 0 n y -- (3) * STALE
1544 * 0 y y n (4) *
1545 * 0 y y y (5) * STALE
1546 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1547 * 1 -- y -- (7) * STALE
1548 */
1549
1550 if (lladdr) { /*(3-5) and (7)*/
1551 /*
1552 * Record source link-layer address
1553 * XXX is it dependent to ifp->if_type?
1554 */
1555 sdl->sdl_alen = ifp->if_addrlen;
1556 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1557 }
1558
1559 if (!is_newentry) {
1560 if ((!olladdr && lladdr) /*(3)*/
1561 || (olladdr && lladdr && llchange)) { /*(5)*/
1562 do_update = 1;
1563 newstate = ND6_LLINFO_STALE;
1564 } else /*(1-2,4)*/
1565 do_update = 0;
1566 } else {
1567 do_update = 1;
1568 if (!lladdr) /*(6)*/
1569 newstate = ND6_LLINFO_NOSTATE;
1570 else /*(7)*/
1571 newstate = ND6_LLINFO_STALE;
1572 }
1573
1574 if (do_update) {
1575 /*
1576 * Update the state of the neighbor cache.
1577 */
1578 ln->ln_state = newstate;
1579
1580 if (ln->ln_state == ND6_LLINFO_STALE) {
1581 rt->rt_flags &= ~RTF_REJECT;
1582 if (ln->ln_hold) {
1583 #ifdef OLDIP6OUTPUT
1584 (*ifp->if_output)(ifp, ln->ln_hold,
1585 rt_key(rt), rt);
1586 #else
1587 nd6_output(ifp, ln->ln_hold,
1588 (struct sockaddr_in6 *)rt_key(rt),
1589 rt);
1590 #endif
1591 ln->ln_hold = 0;
1592 }
1593 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1594 /* probe right away */
1595 ln->ln_expire = time_second;
1596 }
1597 }
1598
1599 /*
1600 * ICMP6 type dependent behavior.
1601 *
1602 * NS: clear IsRouter if new entry
1603 * RS: clear IsRouter
1604 * RA: set IsRouter if there's lladdr
1605 * redir: clear IsRouter if new entry
1606 *
1607 * RA case, (1):
1608 * The spec says that we must set IsRouter in the following cases:
1609 * - If lladdr exist, set IsRouter. This means (1-5).
1610 * - If it is old entry (!newentry), set IsRouter. This means (7).
1611 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1612 * A quetion arises for (1) case. (1) case has no lladdr in the
1613 * neighbor cache, this is similar to (6).
1614 * This case is rare but we figured that we MUST NOT set IsRouter.
1615 *
1616 * newentry olladdr lladdr llchange NS RS RA redir
1617 * D R
1618 * 0 n n -- (1) c ? s
1619 * 0 y n -- (2) c s s
1620 * 0 n y -- (3) c s s
1621 * 0 y y n (4) c s s
1622 * 0 y y y (5) c s s
1623 * 1 -- n -- (6) c c c s
1624 * 1 -- y -- (7) c c s c s
1625 *
1626 * (c=clear s=set)
1627 */
1628 switch (type & 0xff) {
1629 case ND_NEIGHBOR_SOLICIT:
1630 /*
1631 * New entry must have is_router flag cleared.
1632 */
1633 if (is_newentry) /*(6-7)*/
1634 ln->ln_router = 0;
1635 break;
1636 case ND_REDIRECT:
1637 /*
1638 * If the icmp is a redirect to a better router, always set the
1639 * is_router flag. Otherwise, if the entry is newly created,
1640 * clear the flag. [RFC 2461, sec 8.3]
1641 *
1642 */
1643 if (code == ND_REDIRECT_ROUTER)
1644 ln->ln_router = 1;
1645 else if (is_newentry) /*(6-7)*/
1646 ln->ln_router = 0;
1647 break;
1648 case ND_ROUTER_SOLICIT:
1649 /*
1650 * is_router flag must always be cleared.
1651 */
1652 ln->ln_router = 0;
1653 break;
1654 case ND_ROUTER_ADVERT:
1655 /*
1656 * Mark an entry with lladdr as a router.
1657 */
1658 if ((!is_newentry && (olladdr || lladdr)) /*(2-5)*/
1659 || (is_newentry && lladdr)) { /*(7)*/
1660 ln->ln_router = 1;
1661 }
1662 break;
1663 }
1664
1665 return rt;
1666 }
1667
1668 static void
1669 nd6_slowtimo(ignored_arg)
1670 void *ignored_arg;
1671 {
1672 int s = splsoftnet();
1673 register int i;
1674 register struct nd_ifinfo *nd6if;
1675
1676 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1677 nd6_slowtimo, NULL);
1678 for (i = 1; i < if_index + 1; i++) {
1679 nd6if = &nd_ifinfo[i];
1680 if (nd6if->basereachable && /* already initialized */
1681 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1682 /*
1683 * Since reachable time rarely changes by router
1684 * advertisements, we SHOULD insure that a new random
1685 * value gets recomputed at least once every few hours.
1686 * (RFC 2461, 6.3.4)
1687 */
1688 nd6if->recalctm = nd6_recalc_reachtm_interval;
1689 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1690 }
1691 }
1692 splx(s);
1693 }
1694
1695 #define senderr(e) { error = (e); goto bad;}
1696 int
1697 nd6_output(ifp, m0, dst, rt0)
1698 register struct ifnet *ifp;
1699 struct mbuf *m0;
1700 struct sockaddr_in6 *dst;
1701 struct rtentry *rt0;
1702 {
1703 register struct mbuf *m = m0;
1704 register struct rtentry *rt = rt0;
1705 struct llinfo_nd6 *ln = NULL;
1706 int error = 0;
1707 long time_second = time.tv_sec;
1708
1709 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1710 goto sendpkt;
1711
1712 /*
1713 * XXX: we currently do not make neighbor cache on any interface
1714 * other than ARCnet, Ethernet and FDDI.
1715 */
1716 switch (ifp->if_type) {
1717 case IFT_ARCNET:
1718 case IFT_ETHER:
1719 case IFT_FDDI:
1720 break;
1721 default:
1722 goto sendpkt;
1723 }
1724
1725 /*
1726 * next hop determination. This routine is derived from ether_outpout.
1727 */
1728 if (rt) {
1729 if ((rt->rt_flags & RTF_UP) == 0) {
1730 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
1731 NULL)
1732 {
1733 rt->rt_refcnt--;
1734 if (rt->rt_ifp != ifp)
1735 return nd6_output(ifp, m0, dst, rt); /* XXX: loop care? */
1736 } else
1737 senderr(EHOSTUNREACH);
1738 }
1739 if (rt->rt_flags & RTF_GATEWAY) {
1740 if (rt->rt_gwroute == 0)
1741 goto lookup;
1742 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1743 rtfree(rt); rt = rt0;
1744 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1745 if ((rt = rt->rt_gwroute) == 0)
1746 senderr(EHOSTUNREACH);
1747 }
1748 }
1749 if (rt->rt_flags & RTF_REJECT)
1750 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1751 }
1752
1753 /*
1754 * Address resolution or Neighbor Unreachability Detection
1755 * for the next hop.
1756 * At this point, the destination of the packet must be a unicast
1757 * or an anycast address(i.e. not a multicast).
1758 */
1759
1760 /* Look up the neighbor cache for the nexthop */
1761 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1762 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1763 else {
1764 if ((rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1765 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1766 }
1767 if (!ln || !rt) {
1768 log(LOG_DEBUG, "nd6_output: can't allocate llinfo for %s "
1769 "(ln=%p, rt=%p)\n",
1770 ip6_sprintf(&dst->sin6_addr), ln, rt);
1771 senderr(EIO); /* XXX: good error? */
1772 }
1773
1774
1775 /*
1776 * The first time we send a packet to a neighbor whose entry is
1777 * STALE, we have to change the state to DELAY and a sets a timer to
1778 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1779 * neighbor unreachability detection on expiration.
1780 * (RFC 2461 7.3.3)
1781 */
1782 if (ln->ln_state == ND6_LLINFO_STALE) {
1783 ln->ln_asked = 0;
1784 ln->ln_state = ND6_LLINFO_DELAY;
1785 ln->ln_expire = time_second + nd6_delay;
1786 }
1787
1788 /*
1789 * If the neighbor cache entry has a state other than INCOMPLETE
1790 * (i.e. its link-layer address is already reloved), just
1791 * send the packet.
1792 */
1793 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1794 goto sendpkt;
1795
1796 /*
1797 * There is a neighbor cache entry, but no ethernet address
1798 * response yet. Replace the held mbuf (if any) with this
1799 * latest one.
1800 *
1801 * XXX Does the code conform to rate-limiting rule?
1802 * (RFC 2461 7.2.2)
1803 */
1804 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
1805 ln->ln_state == ND6_LLINFO_NOSTATE)
1806 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1807 if (ln->ln_hold)
1808 m_freem(ln->ln_hold);
1809 ln->ln_hold = m;
1810 if (ln->ln_expire) {
1811 rt->rt_flags &= ~RTF_REJECT;
1812 if (ln->ln_asked < nd6_mmaxtries &&
1813 ln->ln_expire < time_second) {
1814 ln->ln_asked++;
1815 ln->ln_expire = time_second +
1816 nd_ifinfo[ifp->if_index].retrans / 1000;
1817 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1818 }
1819 }
1820 return(0);
1821
1822 sendpkt:
1823 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1824
1825 bad:
1826 if (m)
1827 m_freem(m);
1828 return (error);
1829 }
1830 #undef senderr
1831
1832 int
1833 nd6_storelladdr(ifp, rt, m, dst, desten)
1834 struct ifnet *ifp;
1835 struct rtentry *rt;
1836 struct mbuf *m;
1837 struct sockaddr *dst;
1838 u_char *desten;
1839 {
1840 struct sockaddr_dl *sdl;
1841
1842 if (m->m_flags & M_MCAST) {
1843 switch (ifp->if_type) {
1844 case IFT_ETHER:
1845 case IFT_FDDI:
1846 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1847 desten);
1848 return(1);
1849 break;
1850 case IFT_ARCNET:
1851 *desten = 0;
1852 return(1);
1853 default:
1854 return(0);
1855 }
1856 }
1857
1858 if (rt == NULL ||
1859 rt->rt_gateway->sa_family != AF_LINK) {
1860 printf("nd6_storelladdr: something odd happens\n");
1861 return(0);
1862 }
1863 sdl = SDL(rt->rt_gateway);
1864 if (sdl->sdl_alen != 0)
1865 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1866
1867 return(1);
1868 }
1869