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