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