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