nd6.c revision 1.8 1 /* $NetBSD: nd6.c,v 1.8 1999/07/31 18:41:17 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_var.h>
76 #include <netinet6/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/nd6.h>
79 #include <netinet6/icmp6.h>
80
81 #include "loop.h"
82 #ifdef __NetBSD__
83 extern struct ifnet loif[NLOOP];
84 #endif
85
86 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
87 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
88
89 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
90 #define time_second time.tv_sec
91 #endif
92
93 #define SIN6(s) ((struct sockaddr_in6 *)s)
94 #define SDL(s) ((struct sockaddr_dl *)s)
95
96 /* timer values */
97 int nd6_prune = 1; /* walk list every 1 seconds */
98 int nd6_delay = 5; /* delay first probe time 5 second */
99 int nd6_umaxtries = 3; /* maximum unicast query */
100 int nd6_mmaxtries = 3; /* maximum multicast query */
101 int nd6_useloopback = 1; /* use loopback interface for local traffic */
102 int nd6_proxyall = 0; /* enable Proxy Neighbor Advertisement */
103
104 /* for debugging? */
105 static int nd6_inuse, nd6_allocated;
106
107 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
108 struct nd_ifinfo *nd_ifinfo;
109 struct nd_drhead nd_defrouter = { 0 };
110 struct nd_prhead nd_prefix = { 0 };
111
112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
113 #if 0
114 extern int ip6_forwarding;
115 #endif
116
117 static void nd6_slowtimo __P((void *));
118
119 void
120 nd6_init()
121 {
122 static int nd6_init_done = 0;
123
124 if (nd6_init_done) {
125 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
126 return;
127 }
128 nd6_init_done = 1;
129
130 /* start timer */
131 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
132 }
133
134 void
135 nd6_ifattach(ifp)
136 struct ifnet *ifp;
137 {
138 static size_t if_indexlim = 8;
139
140 /*
141 * We have some arrays that should be indexed by if_index.
142 * since if_index will grow dynamically, they should grow too.
143 */
144 if (nd_ifinfo == NULL || if_index >= if_indexlim) {
145 size_t n;
146 caddr_t q;
147
148 while (if_index >= if_indexlim)
149 if_indexlim <<= 1;
150
151 /* grow nd_ifinfo */
152 n = if_indexlim * sizeof(struct nd_ifinfo);
153 q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
154 bzero(q, n);
155 if (nd_ifinfo) {
156 bcopy((caddr_t)nd_ifinfo, q, n/2);
157 free((caddr_t)nd_ifinfo, M_IP6NDP);
158 }
159 nd_ifinfo = (struct nd_ifinfo *)q;
160 }
161
162 #define ND nd_ifinfo[ifp->if_index]
163 ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
164 ND.chlim = IPV6_DEFHLIM;
165 ND.basereachable = REACHABLE_TIME;
166 ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
167 ND.retrans = RETRANS_TIMER;
168 ND.receivedra = 0;
169 nd6_setmtu(ifp);
170 #undef ND
171 }
172
173 /*
174 * Reset ND level link MTU. This function is called when the physical MTU
175 * changes, which means we might have to adjust the ND level MTU.
176 */
177 void
178 nd6_setmtu(ifp)
179 struct ifnet *ifp;
180 {
181 #define MIN(a,b) ((a) < (b) ? (a) : (b))
182 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
183 u_long oldmaxmtu = ndi->maxmtu;
184 u_long oldlinkmtu = ndi->linkmtu;
185
186 switch(ifp->if_type) {
187 case IFT_ETHER:
188 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
189 break;
190 #if defined(__FreeBSD__) || defined(__bsdi__)
191 case IFT_FDDI:
192 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
193 break;
194 #endif
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 > 10) {
354 printf("too many loop in nd opt\n");
355 break;
356 }
357
358 if (ndopts->nd_opts_done)
359 break;
360 }
361
362 return 0;
363 }
364
365 /*
366 * ND6 timer routine to expire default route list and prefix list
367 */
368 void
369 nd6_timer(ignored_arg)
370 void *ignored_arg;
371 {
372 int s;
373 register struct llinfo_nd6 *ln;
374 register struct nd_defrouter *dr;
375 register struct nd_prefix *pr;
376
377 s = splsoftnet();
378 timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
379
380 ln = llinfo_nd6.ln_next;
381 /* XXX BSD/OS separates this code -- itojun */
382 while (ln && ln != &llinfo_nd6) {
383 struct rtentry *rt;
384 struct ifnet *ifp;
385 struct sockaddr_in6 *dst;
386 struct llinfo_nd6 *next = ln->ln_next;
387
388 if ((rt = ln->ln_rt) == NULL) {
389 ln = next;
390 continue;
391 }
392 if ((ifp = rt->rt_ifp) == NULL) {
393 ln = next;
394 continue;
395 }
396 dst = (struct sockaddr_in6 *)rt_key(rt);
397
398 if (ln->ln_expire > time_second) {
399 ln = next;
400 continue;
401 }
402
403 /* sanity check */
404 if (!rt)
405 panic("rt=0 in nd6_timer(ln=%p)\n", ln);
406 if (!dst)
407 panic("dst=0 in nd6_timer(ln=%p)\n", ln);
408
409 switch (ln->ln_state) {
410 case ND6_LLINFO_INCOMPLETE:
411 if (ln->ln_asked < nd6_mmaxtries) {
412 ln->ln_asked++;
413 ln->ln_expire = time_second +
414 nd_ifinfo[ifp->if_index].retrans / 1000;
415 nd6_ns_output(ifp, NULL, &dst->sin6_addr,
416 ln, 0);
417 } else {
418 struct mbuf *m = ln->ln_hold;
419 if (m) {
420 if (rt->rt_ifp) {
421 /*
422 * Fake rcvif to make ICMP error
423 * more helpful in diagnosing
424 * for the receiver.
425 * XXX: should we consider
426 * older rcvif?
427 */
428 m->m_pkthdr.rcvif = rt->rt_ifp;
429 }
430 icmp6_error(m, ICMP6_DST_UNREACH,
431 ICMP6_DST_UNREACH_ADDR, 0);
432 ln->ln_hold = NULL;
433 }
434 nd6_free(rt);
435 }
436 break;
437 case ND6_LLINFO_REACHABLE:
438 if (ln->ln_expire) {
439 ln->ln_state = ND6_LLINFO_STALE;
440 }
441 break;
442 /*
443 * ND6_LLINFO_STALE state requires nothing for timer
444 * routine.
445 */
446 case ND6_LLINFO_DELAY:
447 ln->ln_asked = 1;
448 ln->ln_state = ND6_LLINFO_PROBE;
449 ln->ln_expire = time_second +
450 nd_ifinfo[ifp->if_index].retrans / 1000;
451 nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr,
452 ln, 0);
453 break;
454
455 case ND6_LLINFO_PROBE:
456 if (ln->ln_asked < nd6_umaxtries) {
457 ln->ln_asked++;
458 ln->ln_expire = time_second +
459 nd_ifinfo[ifp->if_index].retrans / 1000;
460 nd6_ns_output(ifp, &dst->sin6_addr,
461 &dst->sin6_addr, ln, 0);
462 } else {
463 nd6_free(rt);
464 }
465 break;
466 case ND6_LLINFO_WAITDELETE:
467 nd6_free(rt);
468 break;
469 }
470 ln = next;
471 }
472
473 /* expire */
474 dr = nd_defrouter.lh_first;
475 while (dr) {
476 if (dr->expire && dr->expire < time_second) {
477 struct nd_defrouter *t;
478 t = dr->dr_next;
479 defrtrlist_del(dr);
480 dr = t;
481 } else
482 dr = dr->dr_next;
483 }
484 pr = nd_prefix.lh_first;
485 while (pr) {
486 struct in6_ifaddr *ia6;
487 struct in6_addrlifetime *lt6;
488
489 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
490 ia6 = NULL;
491 else
492 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
493
494 if (ia6) {
495 /* check address lifetime */
496 lt6 = &ia6->ia6_lifetime;
497 if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
498 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
499 if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
500 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
501 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
502 /* xxx ND_OPT_PI_FLAG_ONLINK processing */
503 }
504 }
505
506 /*
507 * check prefix lifetime.
508 * since pltime is just for autoconf, pltime processing for
509 * prefix is not necessary.
510 *
511 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
512 * can use the old prefix information to validate the
513 * next prefix information to come. See prelist_update()
514 * for actual validation.
515 */
516 if (pr->ndpr_expire
517 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
518 struct nd_prefix *t;
519 t = pr->ndpr_next;
520
521 /*
522 * address expiration and prefix expiration are
523 * separate. NEVER perform in6_ifdel here.
524 */
525
526 prelist_remove(pr);
527 pr = t;
528 } else
529 pr = pr->ndpr_next;
530 }
531 splx(s);
532 }
533
534 static struct sockaddr_in6 all1_sa = {
535 sizeof(struct sockaddr_in6), AF_INET6, 0, 0,
536 {{{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}}, 0};
537
538 struct rtentry *
539 nd6_lookup(addr6, create, ifp)
540 struct in6_addr *addr6;
541 int create;
542 struct ifnet *ifp;
543 {
544 struct rtentry *rt;
545 struct sockaddr_in6 sin6;
546
547 bzero(&sin6, sizeof(sin6));
548 sin6.sin6_len = sizeof(struct sockaddr_in6);
549 sin6.sin6_family = AF_INET6;
550 sin6.sin6_addr = *addr6;
551 rt = rtalloc1((struct sockaddr *)&sin6, create
552 #ifdef __FreeBSD__
553 , 0UL
554 #endif /*__FreeBSD__*/
555 );
556 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
557 /*
558 * This is the case for the default route.
559 * If we want to create a neighbor cache for the address, we
560 * should free the route for the destination and allocate an
561 * interface route.
562 */
563 if (create) {
564 RTFREE(rt);
565 rt = 0;
566 }
567 }
568 if (!rt) {
569 if (create && ifp) {
570 /*
571 * If no route is available and create is set,
572 * we allocate a host route for the destination
573 * and treat it like an interface route.
574 * This hack is necessary for a neighbor which can't
575 * be covered by our own prefix.
576 */
577 struct ifaddr *ifa =
578 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
579 if (ifa == NULL)
580 return(NULL);
581
582 /*
583 * Create a new route. RTF_LLINFO is necessary
584 * to create a Neighbor Cache entry for the
585 * destination in nd6_rtrequest which will be
586 * called in rtequest via ifa->ifa_rtrequest.
587 */
588 if (rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
589 ifa->ifa_addr,
590 (struct sockaddr *)&all1_sa,
591 (ifa->ifa_flags |
592 RTF_HOST | RTF_LLINFO) & ~RTF_CLONING,
593 &rt))
594 log(LOG_ERR,
595 "nd6_lookup: failed to add route for a "
596 "neighbor(%s)\n", ip6_sprintf(addr6));
597 if (rt == NULL)
598 return(NULL);
599 if (rt->rt_llinfo) {
600 struct llinfo_nd6 *ln =
601 (struct llinfo_nd6 *)rt->rt_llinfo;
602 ln->ln_state = ND6_LLINFO_NOSTATE;
603 }
604 }
605 else
606 return(NULL);
607 }
608 rt->rt_refcnt--;
609 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
610 rt->rt_gateway->sa_family != AF_LINK) {
611 if (create) {
612 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s\n",
613 ip6_sprintf(addr6));
614 /* xxx more logs... kazu */
615 }
616 return(0);
617 }
618 return(rt);
619 }
620
621 /*
622 * Detect if a given IPv6 address identifies a neighbor on a given link.
623 * XXX: should take care of the destination of a p2p link?
624 */
625 int
626 nd6_is_addr_neighbor(addr, ifp)
627 struct in6_addr *addr;
628 struct ifnet *ifp;
629 {
630 register struct ifaddr *ifa;
631 int i;
632
633 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
634 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
635
636 /* A link-local address is always a neighbor. */
637 if (IN6_IS_ADDR_LINKLOCAL(addr))
638 return(1);
639
640 /*
641 * If the address matches one of our addresses,
642 * it should be a neighbor.
643 */
644 #ifdef __bsdi__
645 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
646 #else
647 for (ifa = ifp->if_addrlist.tqh_first;
648 ifa;
649 ifa = ifa->ifa_list.tqe_next)
650 #endif
651 {
652 if (ifa->ifa_addr->sa_family != AF_INET6)
653 next: continue;
654
655 for (i = 0; i < 4; i++) {
656 if ((IFADDR6(ifa).s6_addr32[i] ^ addr->s6_addr32[i]) &
657 IFMASK6(ifa).s6_addr32[i])
658 goto next;
659 }
660 return(1);
661 }
662
663 /*
664 * Even if the address matches none of our addresses, it might be
665 * in the neighbor cache.
666 */
667 if (nd6_lookup(addr, 0, ifp))
668 return(1);
669
670 return(0);
671 #undef IFADDR6
672 #undef IFMASK6
673 }
674
675 /*
676 * Free an nd6 llinfo entry.
677 */
678 void
679 nd6_free(rt)
680 struct rtentry *rt;
681 {
682 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
683 struct sockaddr_dl *sdl;
684
685 if (ln->ln_router) {
686 /* remove from default router list */
687 struct nd_defrouter *dr;
688 struct in6_addr *in6;
689 int s;
690 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
691
692 s = splsoftnet();
693 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->
694 sin6_addr,
695 rt->rt_ifp);
696 if (dr)
697 defrtrlist_del(dr);
698 else if (!ip6_forwarding && ip6_accept_rtadv) {
699 /*
700 * rt6_flush must be called in any case.
701 * see the comment in nd6_na_input().
702 */
703 rt6_flush(in6, rt->rt_ifp);
704 }
705 splx(s);
706 }
707
708 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
709 sdl->sdl_family == AF_LINK) {
710 sdl->sdl_alen = 0;
711 ln->ln_state = ND6_LLINFO_WAITDELETE;
712 ln->ln_asked = 0;
713 rt->rt_flags &= ~RTF_REJECT;
714 return;
715 }
716 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
717 0, (struct rtentry **)0);
718 }
719
720 /*
721 * Upper-layer reachability hint for Neighbor Unreachability Detection.
722 *
723 * XXX cost-effective metods?
724 */
725 void
726 nd6_nud_hint(rt, dst6)
727 struct rtentry *rt;
728 struct in6_addr *dst6;
729 {
730 struct llinfo_nd6 *ln;
731
732 /*
733 * If the caller specified "rt", use that. Otherwise, resolve the
734 * routing table by supplied "dst6".
735 */
736 if (!rt) {
737 if (!dst6)
738 return;
739 if (!(rt = nd6_lookup(dst6, 0, NULL)))
740 return;
741 }
742
743 if ((rt->rt_flags & RTF_GATEWAY)
744 || (rt->rt_flags & RTF_LLINFO) == 0
745 || !rt->rt_llinfo
746 || !rt->rt_gateway
747 || rt->rt_gateway->sa_family != AF_LINK) {
748 /* This is not a host route. */
749 return;
750 }
751
752 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
753 if (ln->ln_state == ND6_LLINFO_INCOMPLETE)
754 return;
755
756 ln->ln_state = ND6_LLINFO_REACHABLE;
757 if (ln->ln_expire)
758 ln->ln_expire = time_second +
759 nd_ifinfo[rt->rt_ifp->if_index].reachable;
760 }
761
762 /*
763 * Resolve an IP6 address into an ethernet address. If success,
764 * desten is filled in. If there is no entry in ndptab,
765 * set one up and multicast a solicitation for the IP6 address.
766 * Hold onto this mbuf and resend it once the address
767 * is finally resolved. A return value of 1 indicates
768 * that desten has been filled in and the packet should be sent
769 * normally; a 0 return indicates that the packet has been
770 * taken over here, either now or for later transmission.
771 */
772 int
773 nd6_resolve(ifp, rt, m, dst, desten)
774 struct ifnet *ifp;
775 struct rtentry *rt;
776 struct mbuf *m;
777 struct sockaddr *dst;
778 u_char *desten;
779 {
780 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
781 struct sockaddr_dl *sdl;
782
783 if (m->m_flags & M_MCAST) {
784 switch (ifp->if_type) {
785 case IFT_ETHER:
786 case IFT_FDDI:
787 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
788 desten);
789 return(1);
790 break;
791 default:
792 return(0);
793 }
794 }
795 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
796 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
797 else {
798 if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
799 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
800 }
801 if (!ln || !rt) {
802 log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
803 ip6_sprintf(&(SIN6(dst)->sin6_addr)));
804 m_freem(m);
805 return(0);
806 }
807 sdl = SDL(rt->rt_gateway);
808 /*
809 * Ckeck the address family and length is valid, the address
810 * is resolved; otherwise, try to resolve.
811 */
812 if (ln->ln_state >= ND6_LLINFO_REACHABLE
813 && sdl->sdl_family == AF_LINK
814 && sdl->sdl_alen != 0) {
815 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
816 if (ln->ln_state == ND6_LLINFO_STALE) {
817 ln->ln_asked = 0;
818 ln->ln_state = ND6_LLINFO_DELAY;
819 ln->ln_expire = time_second + nd6_delay;
820 }
821 return(1);
822 }
823 /*
824 * There is an ndp entry, but no ethernet address
825 * response yet. Replace the held mbuf with this
826 * latest one.
827 *
828 * XXX Does the code conform to rate-limiting rule?
829 * (RFC 2461 7.2.2)
830 */
831 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
832 ln->ln_state == ND6_LLINFO_NOSTATE)
833 ln->ln_state = ND6_LLINFO_INCOMPLETE;
834 if (ln->ln_hold)
835 m_freem(ln->ln_hold);
836 ln->ln_hold = m;
837 if (ln->ln_expire) {
838 rt->rt_flags &= ~RTF_REJECT;
839 if (ln->ln_asked < nd6_mmaxtries &&
840 ln->ln_expire < time_second) {
841 ln->ln_asked++;
842 ln->ln_expire = time_second +
843 nd_ifinfo[ifp->if_index].retrans / 1000;
844 nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
845 ln, 0);
846 }
847 }
848 return(0);
849 }
850
851 void
852 nd6_rtrequest(req, rt, sa)
853 int req;
854 struct rtentry *rt;
855 struct sockaddr *sa; /* xxx unused */
856 {
857 struct sockaddr *gate = rt->rt_gateway;
858 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
859 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
860 struct ifnet *ifp = rt->rt_ifp;
861 struct ifaddr *ifa;
862
863 if (rt->rt_flags & RTF_GATEWAY)
864 return;
865
866 switch (req) {
867 case RTM_ADD:
868 /*
869 * There is no backward compatibility :)
870 *
871 * if ((rt->rt_flags & RTF_HOST) == 0 &&
872 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
873 * rt->rt_flags |= RTF_CLONING;
874 */
875 if (rt->rt_flags & RTF_CLONING || rt->rt_flags & RTF_LLINFO) {
876 /*
877 * Case 1: This route should come from
878 * a route to interface. RTF_LLINFO flag is set
879 * for a host route whose destination should be
880 * treated as on-link.
881 */
882 rt_setgate(rt, rt_key(rt),
883 (struct sockaddr *)&null_sdl);
884 gate = rt->rt_gateway;
885 SDL(gate)->sdl_type = ifp->if_type;
886 SDL(gate)->sdl_index = ifp->if_index;
887 if (ln)
888 ln->ln_expire = time_second;
889 #if 1
890 if (ln && ln->ln_expire == 0) {
891 /* cludge for desktops */
892 #if 0
893 printf("nd6_request: time.tv_sec is zero; "
894 "treat it as 1\n");
895 #endif
896 ln->ln_expire = 1;
897 }
898 #endif
899 if (rt->rt_flags & RTF_CLONING)
900 break;
901 }
902 /* Announce a new entry if requested. */
903 if (rt->rt_flags & RTF_ANNOUNCE)
904 nd6_na_output(ifp,
905 &SIN6(rt_key(rt))->sin6_addr,
906 &SIN6(rt_key(rt))->sin6_addr,
907 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
908 1);
909 /* FALLTHROUGH */
910 case RTM_RESOLVE:
911 if (gate->sa_family != AF_LINK ||
912 gate->sa_len < sizeof(null_sdl)) {
913 log(LOG_DEBUG, "nd6_rtrequest: bad gateway value\n");
914 break;
915 }
916 SDL(gate)->sdl_type = ifp->if_type;
917 SDL(gate)->sdl_index = ifp->if_index;
918 if (ln != 0)
919 break; /* This happens on a route change */
920 /*
921 * Case 2: This route may come from cloning, or a manual route
922 * add with a LL address.
923 */
924 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
925 rt->rt_llinfo = (caddr_t)ln;
926 if (!ln) {
927 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
928 break;
929 }
930 nd6_inuse++;
931 nd6_allocated++;
932 Bzero(ln, sizeof(*ln));
933 ln->ln_rt = rt;
934 /* this is required for "ndp" command. - shin */
935 if (req == RTM_ADD) {
936 /*
937 * gate should have some valid AF_LINK entry,
938 * and ln->ln_expire should have some lifetime
939 * which is specified by ndp command.
940 */
941 ln->ln_state = ND6_LLINFO_REACHABLE;
942 } else {
943 /*
944 * When req == RTM_RESOLVE, rt is created and
945 * initialized in rtrequest(), so rt_expire is 0.
946 */
947 ln->ln_state = ND6_LLINFO_INCOMPLETE;
948 ln->ln_expire = time_second;
949 }
950 rt->rt_flags |= RTF_LLINFO;
951 #if 0
952 insque(ln, &llinfo_nd6);
953 #else
954 ln->ln_next = llinfo_nd6.ln_next;
955 llinfo_nd6.ln_next = ln;
956 ln->ln_prev = &llinfo_nd6;
957 ln->ln_next->ln_prev = ln;
958 #endif
959
960 /*
961 * check if rt_key(rt) is one of my address assigned
962 * to the interface.
963 */
964 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
965 &SIN6(rt_key(rt))->sin6_addr);
966 if (ifa) {
967 caddr_t macp = nd6_ifptomac(ifp);
968 ln->ln_expire = 0;
969 ln->ln_state = ND6_LLINFO_REACHABLE;
970 if (macp) {
971 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
972 SDL(gate)->sdl_alen = ifp->if_addrlen;
973 }
974 if (nd6_useloopback) {
975 #ifdef __bsdi__
976 extern struct ifnet loif;
977 rt->rt_ifp = &loif; /*XXX*/
978 #endif /*__bsdi__*/
979 #if defined(__FreeBSD__) || defined(__NetBSD__)
980 rt->rt_ifp = &loif[0]; /*XXX*/
981 #endif
982 /*
983 * Make sure rt_ifa be equal to the ifaddr
984 * corresponding to the address.
985 * We need this because when we refer
986 * rt_ifa->ia6_flags in ip6_input, we assume
987 * that the rt_ifa points to the address instead
988 * of the loopback address.
989 */
990 if (ifa != rt->rt_ifa) {
991 rt->rt_ifa->ifa_refcnt--;
992 ifa->ifa_refcnt++;
993 rt->rt_ifa = ifa;
994 }
995 }
996 }
997 break;
998
999 case RTM_DELETE:
1000 if (!ln)
1001 break;
1002 nd6_inuse--;
1003 #if 0
1004 remque(ln);
1005 #else
1006 ln->ln_next->ln_prev = ln->ln_prev;
1007 ln->ln_prev->ln_next = ln->ln_next;
1008 ln->ln_prev = NULL;
1009 #endif
1010 rt->rt_llinfo = 0;
1011 rt->rt_flags &= ~RTF_LLINFO;
1012 if (ln->ln_hold)
1013 m_freem(ln->ln_hold);
1014 Free((caddr_t)ln);
1015 }
1016 }
1017
1018 void
1019 nd6_p2p_rtrequest(req, rt, sa)
1020 int req;
1021 struct rtentry *rt;
1022 struct sockaddr *sa; /* xxx unused */
1023 {
1024 struct sockaddr *gate = rt->rt_gateway;
1025 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1026 struct ifnet *ifp = rt->rt_ifp;
1027 struct ifaddr *ifa;
1028
1029 if (rt->rt_flags & RTF_GATEWAY)
1030 return;
1031
1032 switch (req) {
1033 case RTM_ADD:
1034 /*
1035 * There is no backward compatibility :)
1036 *
1037 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1038 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1039 * rt->rt_flags |= RTF_CLONING;
1040 */
1041 if (rt->rt_flags & RTF_CLONING) {
1042 /*
1043 * Case 1: This route should come from
1044 * a route to interface.
1045 */
1046 rt_setgate(rt, rt_key(rt),
1047 (struct sockaddr *)&null_sdl);
1048 gate = rt->rt_gateway;
1049 SDL(gate)->sdl_type = ifp->if_type;
1050 SDL(gate)->sdl_index = ifp->if_index;
1051 break;
1052 }
1053 /* Announce a new entry if rqquested. */
1054 if (rt->rt_flags & RTF_ANNOUNCE)
1055 nd6_na_output(ifp,
1056 &SIN6(rt_key(rt))->sin6_addr,
1057 &SIN6(rt_key(rt))->sin6_addr,
1058 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1059 1);
1060 /* FALLTHROUGH */
1061 case RTM_RESOLVE:
1062 /*
1063 * check if rt_key(rt) is one of my address assigned
1064 * to the interface.
1065 */
1066 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1067 &SIN6(rt_key(rt))->sin6_addr);
1068 if (ifa) {
1069 if (nd6_useloopback) {
1070 #ifdef __bsdi__
1071 extern struct ifnet loif;
1072 rt->rt_ifp = &loif; /*XXX*/
1073 #endif /*__bsdi__*/
1074 #if defined(__FreeBSD__) || defined(__NetBSD__)
1075 rt->rt_ifp = &loif[0]; /*XXX*/
1076 #endif
1077 }
1078 }
1079 break;
1080 }
1081 }
1082
1083 int
1084 nd6_ioctl(cmd, data, ifp)
1085 u_long cmd;
1086 caddr_t data;
1087 struct ifnet *ifp;
1088 {
1089 struct in6_drlist *drl = (struct in6_drlist *)data;
1090 struct in6_prlist *prl = (struct in6_prlist *)data;
1091 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1092 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1093 struct nd_defrouter *dr, any;
1094 struct nd_prefix *pr;
1095 struct rtentry *rt;
1096 int i = 0, error = 0;
1097 int s;
1098
1099 switch (cmd) {
1100 case SIOCGDRLST_IN6:
1101 bzero(drl, sizeof(*drl));
1102 s = splsoftnet();
1103 dr = nd_defrouter.lh_first;
1104 while (dr && i < DRLSTSIZ) {
1105 drl->defrouter[i].rtaddr = dr->rtaddr;
1106 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1107 /* XXX: need to this hack for KAME stack */
1108 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1109 }
1110 else
1111 log(LOG_ERR,
1112 "default router list contains a "
1113 "non-linklocal address(%s)\n",
1114 ip6_sprintf(&drl->defrouter[i].rtaddr));
1115
1116 drl->defrouter[i].flags = dr->flags;
1117 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1118 drl->defrouter[i].expire = dr->expire;
1119 drl->defrouter[i].if_index = dr->ifp->if_index;
1120 i++;
1121 dr = dr->dr_next;
1122 }
1123 splx(s);
1124 break;
1125 case SIOCGPRLST_IN6:
1126 bzero(prl, sizeof(*prl));
1127 s = splsoftnet();
1128 pr = nd_prefix.lh_first;
1129 while (pr && i < PRLSTSIZ) {
1130 struct nd_pfxrouter *pfr;
1131 int j;
1132
1133 prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1134 prl->prefix[i].raflags = pr->ndpr_raf;
1135 prl->prefix[i].prefixlen = pr->ndpr_plen;
1136 prl->prefix[i].vltime = pr->ndpr_vltime;
1137 prl->prefix[i].pltime = pr->ndpr_pltime;
1138 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1139 prl->prefix[i].expire = pr->ndpr_expire;
1140
1141 pfr = pr->ndpr_advrtrs.lh_first;
1142 j = 0;
1143 while(pfr) {
1144 if (j < DRLSTSIZ) {
1145 #define RTRADDR prl->prefix[i].advrtr[j]
1146 RTRADDR = pfr->router->rtaddr;
1147 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1148 /* XXX: hack for KAME */
1149 RTRADDR.s6_addr16[1] = 0;
1150 }
1151 else
1152 log(LOG_ERR,
1153 "a router(%s) advertises "
1154 "a prefix with "
1155 "non-link local address\n",
1156 ip6_sprintf(&RTRADDR));
1157 #undef RTRADDR
1158 }
1159 j++;
1160 pfr = pfr->pfr_next;
1161 }
1162 prl->prefix[i].advrtrs = j;
1163
1164 i++;
1165 pr = pr->ndpr_next;
1166 }
1167 splx(s);
1168 break;
1169 case SIOCGIFINFO_IN6:
1170 ndi->ndi = nd_ifinfo[ifp->if_index];
1171 break;
1172 case SIOCSNDFLUSH_IN6:
1173 /* flush default router list */
1174 /*
1175 * xxx sumikawa: should not delete route if default
1176 * route equals to the top of default router list
1177 */
1178 bzero(&any, sizeof(any));
1179 defrouter_delreq(&any, 0);
1180 /* xxx sumikawa: flush prefix list */
1181 break;
1182 case SIOCSPFXFLUSH_IN6:
1183 {
1184 /* flush all the prefix advertised by routers */
1185 struct nd_prefix *pr, *next;
1186
1187 s = splsoftnet();
1188 for (pr = nd_prefix.lh_first; pr; pr = next) {
1189 next = pr->ndpr_next;
1190 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1191 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1192 prelist_remove(pr);
1193 }
1194 splx(s);
1195 break;
1196 }
1197 case SIOCSRTRFLUSH_IN6:
1198 {
1199 /* flush all the default routers */
1200 struct nd_defrouter *dr, *next;
1201
1202 s = splsoftnet();
1203 if ((dr = nd_defrouter.lh_first) != NULL) {
1204 /*
1205 * The first entry of the list may be stored in
1206 * the routing table, so we'll delete it later.
1207 */
1208 for (dr = dr->dr_next; dr; dr = next) {
1209 next = dr->dr_next;
1210 defrtrlist_del(dr);
1211 }
1212 defrtrlist_del(nd_defrouter.lh_first);
1213 }
1214 splx(s);
1215 break;
1216 }
1217 case SIOCGNBRINFO_IN6:
1218 {
1219 struct llinfo_nd6 *ln;
1220
1221 s = splsoftnet();
1222 if ((rt = nd6_lookup(&nbi->addr, 0, ifp)) == NULL) {
1223 error = EINVAL;
1224 break;
1225 }
1226 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1227 nbi->state = ln->ln_state;
1228 nbi->asked = ln->ln_asked;
1229 nbi->isrouter = ln->ln_router;
1230 nbi->expire = ln->ln_expire;
1231 splx(s);
1232
1233 break;
1234 }
1235 }
1236 return(error);
1237 }
1238
1239 /*
1240 * Create neighbor cache entry and cache link-layer address,
1241 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1242 */
1243 struct rtentry *
1244 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1245 struct ifnet *ifp;
1246 struct in6_addr *from;
1247 char *lladdr;
1248 int lladdrlen;
1249 int type; /* ICMP6 type */
1250 int code; /* type dependent information */
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 * D R
1391 * 0 n n -- (1) c ? s
1392 * 0 y n -- (2) c s s
1393 * 0 n y -- (3) c s s
1394 * 0 y y n (4) c s s
1395 * 0 y y y (5) c s s
1396 * 1 -- n -- (6) c c c s
1397 * 1 -- y -- (7) c c s c s
1398 *
1399 * (c=clear s=set)
1400 */
1401 switch (type & 0xff) {
1402 case ND_NEIGHBOR_SOLICIT:
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_REDIRECT:
1410 /*
1411 * If the icmp is a redirect to a better router, always set the
1412 * is_router flag. Otherwise, if the entry is newly created,
1413 * clear the flag. [RFC 2461, sec 8.3]
1414 *
1415 */
1416 if (code == ND_REDIRECT_ROUTER)
1417 ln->ln_router = 1;
1418 else if (is_newentry) /*(6-7)*/
1419 ln->ln_router = 0;
1420 break;
1421 case ND_ROUTER_SOLICIT:
1422 /*
1423 * is_router flag must always be cleared.
1424 */
1425 ln->ln_router = 0;
1426 break;
1427 case ND_ROUTER_ADVERT:
1428 /*
1429 * Mark an entry with lladdr as a router.
1430 */
1431 if ((!is_newentry && (olladdr || lladdr)) /*(2-5)*/
1432 || (is_newentry && lladdr)) { /*(7)*/
1433 ln->ln_router = 1;
1434 }
1435 break;
1436 }
1437
1438 return rt;
1439 }
1440
1441 static void
1442 nd6_slowtimo(ignored_arg)
1443 void *ignored_arg;
1444 {
1445 int s = splsoftnet();
1446 register int i;
1447 register struct nd_ifinfo *nd6if;
1448
1449 timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
1450 for (i = 1; i < if_index + 1; i++) {
1451 nd6if = &nd_ifinfo[i];
1452 if (nd6if->basereachable && /* already initialized */
1453 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1454 /*
1455 * Since reachable time rarely changes by router
1456 * advertisements, we SHOULD insure that a new random
1457 * value gets recomputed at least once every few hours.
1458 * (RFC 2461, 6.3.4)
1459 */
1460 nd6if->recalctm = nd6_recalc_reachtm_interval;
1461 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1462 }
1463 }
1464 splx(s);
1465 }
1466
1467 #ifdef NEWIP6OUTPUT
1468 /* for experimental */
1469 #define senderr(e) { error = (e); goto bad;}
1470 int
1471 nd6_output(ifp, m0, dst, rt0)
1472 register struct ifnet *ifp;
1473 struct mbuf *m0;
1474 struct sockaddr_in6 *dst;
1475 struct rtentry *rt0;
1476 {
1477 register struct mbuf *m = m0;
1478 register struct rtentry *rt = rt0;
1479 struct llinfo_nd6 *ln = NULL;
1480 int error = 0;
1481
1482 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1483 goto sendpkt;
1484
1485 /*
1486 * XXX: we currently do not make neighbor cache on any interface
1487 * other than Ethernet and FDDI.
1488 */
1489 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_FDDI)
1490 goto sendpkt;
1491
1492 /*
1493 * next hop determination. This routine is derived from ether_outpout.
1494 */
1495 if (rt) {
1496 if ((rt->rt_flags & RTF_UP) == 0) {
1497 #ifdef __FreeBSD__
1498 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
1499 NULL) {
1500 #else
1501 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
1502 NULL) {
1503 #endif
1504 rt->rt_refcnt--;
1505 if (rt->rt_ifp != ifp)
1506 return nd6_output(ifp, m0, dst, rt); /* XXX: loop care? */
1507 } else
1508 senderr(EHOSTUNREACH);
1509 }
1510 if (rt->rt_flags & RTF_GATEWAY) {
1511 if (rt->rt_gwroute == 0)
1512 goto lookup;
1513 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1514 rtfree(rt); rt = rt0;
1515 #ifdef __FreeBSD__
1516 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1517 #else
1518 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1519 #endif
1520 if ((rt = rt->rt_gwroute) == 0)
1521 senderr(EHOSTUNREACH);
1522 #ifdef __bsdi__
1523 /* the "G" test below also prevents rt == rt0 */
1524 if ((rt->rt_flags & RTF_GATEWAY) ||
1525 (rt->rt_ifp != ifp)) {
1526 rt->rt_refcnt--;
1527 rt0->rt_gwroute = 0;
1528 senderr(EHOSTUNREACH);
1529 }
1530 #endif
1531 }
1532 }
1533 if (rt->rt_flags & RTF_REJECT)
1534 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1535 }
1536
1537 /*
1538 * Address resolution or Neighbor Unreachability Detection
1539 * for the next hop.
1540 * At this point, the destination of the packet must be a unicast
1541 * or an anycast address(i.e. not a multicast).
1542 */
1543
1544 /* Look up the neighbor cache for the nexthop */
1545 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1546 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1547 else {
1548 if ((rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1549 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1550 }
1551 if (!ln || !rt) {
1552 log(LOG_DEBUG, "nd6_output: can't allocate llinfo for %s "
1553 "(ln=%p, rt=%p)\n",
1554 ip6_sprintf(&dst->sin6_addr), ln, rt);
1555 senderr(EIO); /* XXX: good error? */
1556 }
1557
1558
1559 /*
1560 * The first time we send a packet to a neighbor whose entry is
1561 * STALE, we have to change the state to DELAY and a sets a timer to
1562 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1563 * neighbor unreachability detection on expiration.
1564 * (RFC 2461 7.3.3)
1565 */
1566 if (ln->ln_state == ND6_LLINFO_STALE) {
1567 ln->ln_asked = 0;
1568 ln->ln_state = ND6_LLINFO_DELAY;
1569 ln->ln_expire = time_second + nd6_delay;
1570 }
1571
1572 /*
1573 * If the neighbor cache entry has a state other than INCOMPLETE
1574 * (i.e. its link-layer address is already reloved), just
1575 * send the packet.
1576 */
1577 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1578 goto sendpkt;
1579
1580 /*
1581 * There is a neighbor cache entry, but no ethernet address
1582 * response yet. Replace the held mbuf (if any) with this
1583 * latest one.
1584 *
1585 * XXX Does the code conform to rate-limiting rule?
1586 * (RFC 2461 7.2.2)
1587 */
1588 if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
1589 ln->ln_state == ND6_LLINFO_NOSTATE)
1590 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1591 if (ln->ln_hold)
1592 m_freem(ln->ln_hold);
1593 ln->ln_hold = m;
1594 if (ln->ln_expire) {
1595 rt->rt_flags &= ~RTF_REJECT;
1596 if (ln->ln_asked < nd6_mmaxtries &&
1597 ln->ln_expire < time_second) {
1598 ln->ln_asked++;
1599 ln->ln_expire = time_second +
1600 nd_ifinfo[ifp->if_index].retrans / 1000;
1601 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1602 }
1603 }
1604 return(0);
1605
1606 sendpkt:
1607 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1608
1609 bad:
1610 if (m)
1611 m_freem(m);
1612 return (error);
1613 }
1614 #undef senderr
1615
1616 int
1617 nd6_storelladdr(ifp, rt, m, dst, desten)
1618 struct ifnet *ifp;
1619 struct rtentry *rt;
1620 struct mbuf *m;
1621 struct sockaddr *dst;
1622 u_char *desten;
1623 {
1624 struct sockaddr_dl *sdl;
1625
1626 if (m->m_flags & M_MCAST) {
1627 switch (ifp->if_type) {
1628 case IFT_ETHER:
1629 case IFT_FDDI:
1630 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1631 desten);
1632 return(1);
1633 break;
1634 default:
1635 return(0);
1636 }
1637 }
1638
1639 if (rt == NULL ||
1640 rt->rt_gateway->sa_family != AF_LINK) {
1641 printf("nd6_storelladdr: something odd happens\n");
1642 return(0);
1643 }
1644 sdl = SDL(rt->rt_gateway);
1645 if (sdl->sdl_alen != 0)
1646 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1647
1648 return(1);
1649 }
1650 #endif /* NEWIP6OUTPUT */
1651