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