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