nd6.c revision 1.46 1 /* $NetBSD: nd6.c,v 1.46 2001/05/24 08:17:22 itojun Exp $ */
2 /* $KAME: nd6.c,v 1.137 2001/03/21 21:52:06 jinmei 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 default router list */
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 m_freem(m);
977 return(0);
978 }
979 }
980 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
981 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
982 else {
983 if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
984 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
985 }
986 if (!ln || !rt) {
987 log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
988 ip6_sprintf(&(SIN6(dst)->sin6_addr)));
989 m_freem(m);
990 return(0);
991 }
992 sdl = SDL(rt->rt_gateway);
993 /*
994 * Ckeck the address family and length is valid, the address
995 * is resolved; otherwise, try to resolve.
996 */
997 if (ln->ln_state >= ND6_LLINFO_REACHABLE
998 && sdl->sdl_family == AF_LINK
999 && sdl->sdl_alen != 0) {
1000 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1001 if (ln->ln_state == ND6_LLINFO_STALE) {
1002 ln->ln_asked = 0;
1003 ln->ln_state = ND6_LLINFO_DELAY;
1004 ln->ln_expire = time_second + nd6_delay;
1005 }
1006 return(1);
1007 }
1008 /*
1009 * There is an ndp entry, but no ethernet address
1010 * response yet. Replace the held mbuf with this
1011 * latest one.
1012 *
1013 * XXX Does the code conform to rate-limiting rule?
1014 * (RFC 2461 7.2.2)
1015 */
1016 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1017 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1018 if (ln->ln_hold)
1019 m_freem(ln->ln_hold);
1020 ln->ln_hold = m;
1021 if (ln->ln_expire) {
1022 if (ln->ln_asked < nd6_mmaxtries &&
1023 ln->ln_expire < time_second) {
1024 ln->ln_asked++;
1025 ln->ln_expire = time_second +
1026 nd_ifinfo[ifp->if_index].retrans / 1000;
1027 nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
1028 ln, 0);
1029 }
1030 }
1031 /* do not free mbuf here, it is queued into llinfo_nd6 */
1032 return(0);
1033 }
1034 #endif /* OLDIP6OUTPUT */
1035
1036 void
1037 nd6_rtrequest(req, rt, info)
1038 int req;
1039 struct rtentry *rt;
1040 struct rt_addrinfo *info; /* xxx unused */
1041 {
1042 struct sockaddr *gate = rt->rt_gateway;
1043 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1044 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1045 struct ifnet *ifp = rt->rt_ifp;
1046 struct ifaddr *ifa;
1047 long time_second = time.tv_sec;
1048
1049 if (rt->rt_flags & RTF_GATEWAY)
1050 return;
1051
1052 switch (req) {
1053 case RTM_ADD:
1054 /*
1055 * There is no backward compatibility :)
1056 *
1057 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1058 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1059 * rt->rt_flags |= RTF_CLONING;
1060 */
1061 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1062 /*
1063 * Case 1: This route should come from
1064 * a route to interface. RTF_LLINFO flag is set
1065 * for a host route whose destination should be
1066 * treated as on-link.
1067 */
1068 rt_setgate(rt, rt_key(rt),
1069 (struct sockaddr *)&null_sdl);
1070 gate = rt->rt_gateway;
1071 SDL(gate)->sdl_type = ifp->if_type;
1072 SDL(gate)->sdl_index = ifp->if_index;
1073 if (ln)
1074 ln->ln_expire = time_second;
1075 #if 1
1076 if (ln && ln->ln_expire == 0) {
1077 /* kludge for desktops */
1078 #if 0
1079 printf("nd6_request: time.tv_sec is zero; "
1080 "treat it as 1\n");
1081 #endif
1082 ln->ln_expire = 1;
1083 }
1084 #endif
1085 if (rt->rt_flags & RTF_CLONING)
1086 break;
1087 }
1088 /*
1089 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1090 * We don't do that here since llinfo is not ready yet.
1091 *
1092 * There are also couple of other things to be discussed:
1093 * - unsolicited NA code needs improvement beforehand
1094 * - RFC2461 says we MAY send multicast unsolicited NA
1095 * (7.2.6 paragraph 4), however, it also says that we
1096 * SHOULD provide a mechanism to prevent multicast NA storm.
1097 * we don't have anything like it right now.
1098 * note that the mechanism needs a mutual agreement
1099 * between proxies, which means that we need to implement
1100 * a new protocol, or a new kludge.
1101 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1102 * we need to check ip6forwarding before sending it.
1103 * (or should we allow proxy ND configuration only for
1104 * routers? there's no mention about proxy ND from hosts)
1105 */
1106 #if 0
1107 /* XXX it does not work */
1108 if (rt->rt_flags & RTF_ANNOUNCE)
1109 nd6_na_output(ifp,
1110 &SIN6(rt_key(rt))->sin6_addr,
1111 &SIN6(rt_key(rt))->sin6_addr,
1112 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1113 1, NULL);
1114 #endif
1115 /* FALLTHROUGH */
1116 case RTM_RESOLVE:
1117 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
1118 /*
1119 * Address resolution isn't necessary for a point to
1120 * point link, so we can skip this test for a p2p link.
1121 */
1122 if (gate->sa_family != AF_LINK ||
1123 gate->sa_len < sizeof(null_sdl)) {
1124 log(LOG_DEBUG,
1125 "nd6_rtrequest: bad gateway value: %s\n",
1126 if_name(ifp));
1127 break;
1128 }
1129 SDL(gate)->sdl_type = ifp->if_type;
1130 SDL(gate)->sdl_index = ifp->if_index;
1131 }
1132 if (ln != NULL)
1133 break; /* This happens on a route change */
1134 /*
1135 * Case 2: This route may come from cloning, or a manual route
1136 * add with a LL address.
1137 */
1138 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1139 rt->rt_llinfo = (caddr_t)ln;
1140 if (!ln) {
1141 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1142 break;
1143 }
1144 nd6_inuse++;
1145 nd6_allocated++;
1146 Bzero(ln, sizeof(*ln));
1147 ln->ln_rt = rt;
1148 /* this is required for "ndp" command. - shin */
1149 if (req == RTM_ADD) {
1150 /*
1151 * gate should have some valid AF_LINK entry,
1152 * and ln->ln_expire should have some lifetime
1153 * which is specified by ndp command.
1154 */
1155 ln->ln_state = ND6_LLINFO_REACHABLE;
1156 ln->ln_byhint = 0;
1157 } else {
1158 /*
1159 * When req == RTM_RESOLVE, rt is created and
1160 * initialized in rtrequest(), so rt_expire is 0.
1161 */
1162 ln->ln_state = ND6_LLINFO_NOSTATE;
1163 ln->ln_expire = time_second;
1164 }
1165 rt->rt_flags |= RTF_LLINFO;
1166 ln->ln_next = llinfo_nd6.ln_next;
1167 llinfo_nd6.ln_next = ln;
1168 ln->ln_prev = &llinfo_nd6;
1169 ln->ln_next->ln_prev = ln;
1170
1171 /*
1172 * check if rt_key(rt) is one of my address assigned
1173 * to the interface.
1174 */
1175 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1176 &SIN6(rt_key(rt))->sin6_addr);
1177 if (ifa) {
1178 caddr_t macp = nd6_ifptomac(ifp);
1179 ln->ln_expire = 0;
1180 ln->ln_state = ND6_LLINFO_REACHABLE;
1181 ln->ln_byhint = 0;
1182 if (macp) {
1183 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1184 SDL(gate)->sdl_alen = ifp->if_addrlen;
1185 }
1186 if (nd6_useloopback) {
1187 rt->rt_ifp = &loif[0]; /*XXX*/
1188 /*
1189 * Make sure rt_ifa be equal to the ifaddr
1190 * corresponding to the address.
1191 * We need this because when we refer
1192 * rt_ifa->ia6_flags in ip6_input, we assume
1193 * that the rt_ifa points to the address instead
1194 * of the loopback address.
1195 */
1196 if (ifa != rt->rt_ifa) {
1197 IFAFREE(rt->rt_ifa);
1198 IFAREF(ifa);
1199 rt->rt_ifa = ifa;
1200 }
1201 }
1202 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1203 ln->ln_expire = 0;
1204 ln->ln_state = ND6_LLINFO_REACHABLE;
1205 ln->ln_byhint = 0;
1206
1207 /* join solicited node multicast for proxy ND */
1208 if (ifp->if_flags & IFF_MULTICAST) {
1209 struct in6_addr llsol;
1210 int error;
1211
1212 llsol = SIN6(rt_key(rt))->sin6_addr;
1213 llsol.s6_addr16[0] = htons(0xff02);
1214 llsol.s6_addr16[1] = htons(ifp->if_index);
1215 llsol.s6_addr32[1] = 0;
1216 llsol.s6_addr32[2] = htonl(1);
1217 llsol.s6_addr8[12] = 0xff;
1218
1219 if (!in6_addmulti(&llsol, ifp, &error)) {
1220 nd6log((LOG_ERR, "%s: "
1221 "failed to join %s (errno=%d)\n",
1222 if_name(ifp), ip6_sprintf(&llsol),
1223 error));
1224 }
1225 }
1226 }
1227 break;
1228
1229 case RTM_DELETE:
1230 if (!ln)
1231 break;
1232 /* leave from solicited node multicast for proxy ND */
1233 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1234 (ifp->if_flags & IFF_MULTICAST) != 0) {
1235 struct in6_addr llsol;
1236 struct in6_multi *in6m;
1237
1238 llsol = SIN6(rt_key(rt))->sin6_addr;
1239 llsol.s6_addr16[0] = htons(0xff02);
1240 llsol.s6_addr16[1] = htons(ifp->if_index);
1241 llsol.s6_addr32[1] = 0;
1242 llsol.s6_addr32[2] = htonl(1);
1243 llsol.s6_addr8[12] = 0xff;
1244
1245 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1246 if (in6m)
1247 in6_delmulti(in6m);
1248 }
1249 nd6_inuse--;
1250 ln->ln_next->ln_prev = ln->ln_prev;
1251 ln->ln_prev->ln_next = ln->ln_next;
1252 ln->ln_prev = NULL;
1253 rt->rt_llinfo = 0;
1254 rt->rt_flags &= ~RTF_LLINFO;
1255 if (ln->ln_hold)
1256 m_freem(ln->ln_hold);
1257 Free((caddr_t)ln);
1258 }
1259 }
1260
1261 void
1262 nd6_p2p_rtrequest(req, rt, info)
1263 int req;
1264 struct rtentry *rt;
1265 struct rt_addrinfo *info; /* xxx unused */
1266 {
1267 struct sockaddr *gate = rt->rt_gateway;
1268 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1269 struct ifnet *ifp = rt->rt_ifp;
1270 struct ifaddr *ifa;
1271
1272 if (rt->rt_flags & RTF_GATEWAY)
1273 return;
1274
1275 switch (req) {
1276 case RTM_ADD:
1277 /*
1278 * There is no backward compatibility :)
1279 *
1280 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1281 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1282 * rt->rt_flags |= RTF_CLONING;
1283 */
1284 if (rt->rt_flags & RTF_CLONING) {
1285 /*
1286 * Case 1: This route should come from
1287 * a route to interface.
1288 */
1289 rt_setgate(rt, rt_key(rt),
1290 (struct sockaddr *)&null_sdl);
1291 gate = rt->rt_gateway;
1292 SDL(gate)->sdl_type = ifp->if_type;
1293 SDL(gate)->sdl_index = ifp->if_index;
1294 break;
1295 }
1296 /* Announce a new entry if requested. */
1297 if (rt->rt_flags & RTF_ANNOUNCE)
1298 nd6_na_output(ifp,
1299 &SIN6(rt_key(rt))->sin6_addr,
1300 &SIN6(rt_key(rt))->sin6_addr,
1301 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1302 1, NULL);
1303 /* FALLTHROUGH */
1304 case RTM_RESOLVE:
1305 /*
1306 * check if rt_key(rt) is one of my address assigned
1307 * to the interface.
1308 */
1309 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1310 &SIN6(rt_key(rt))->sin6_addr);
1311 if (ifa) {
1312 if (nd6_useloopback) {
1313 rt->rt_ifp = &loif[0]; /*XXX*/
1314 }
1315 }
1316 break;
1317 }
1318 }
1319
1320 int
1321 nd6_ioctl(cmd, data, ifp)
1322 u_long cmd;
1323 caddr_t data;
1324 struct ifnet *ifp;
1325 {
1326 struct in6_drlist *drl = (struct in6_drlist *)data;
1327 struct in6_prlist *prl = (struct in6_prlist *)data;
1328 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1329 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1330 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1331 struct nd_defrouter *dr, any;
1332 struct nd_prefix *pr;
1333 struct rtentry *rt;
1334 int i = 0, error = 0;
1335 int s;
1336
1337 switch (cmd) {
1338 case SIOCGDRLST_IN6:
1339 bzero(drl, sizeof(*drl));
1340 s = splsoftnet();
1341 dr = TAILQ_FIRST(&nd_defrouter);
1342 while (dr && i < DRLSTSIZ) {
1343 drl->defrouter[i].rtaddr = dr->rtaddr;
1344 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1345 /* XXX: need to this hack for KAME stack */
1346 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1347 } else
1348 log(LOG_ERR,
1349 "default router list contains a "
1350 "non-linklocal address(%s)\n",
1351 ip6_sprintf(&drl->defrouter[i].rtaddr));
1352
1353 drl->defrouter[i].flags = dr->flags;
1354 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1355 drl->defrouter[i].expire = dr->expire;
1356 drl->defrouter[i].if_index = dr->ifp->if_index;
1357 i++;
1358 dr = TAILQ_NEXT(dr, dr_entry);
1359 }
1360 splx(s);
1361 break;
1362 case SIOCGPRLST_IN6:
1363 /*
1364 * XXX meaning of fields, especialy "raflags", is very
1365 * differnet between RA prefix list and RR/static prefix list.
1366 * how about separating ioctls into two?
1367 */
1368 bzero(prl, sizeof(*prl));
1369 s = splsoftnet();
1370 pr = nd_prefix.lh_first;
1371 while (pr && i < PRLSTSIZ) {
1372 struct nd_pfxrouter *pfr;
1373 int j;
1374
1375 prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1376 prl->prefix[i].raflags = pr->ndpr_raf;
1377 prl->prefix[i].prefixlen = pr->ndpr_plen;
1378 prl->prefix[i].vltime = pr->ndpr_vltime;
1379 prl->prefix[i].pltime = pr->ndpr_pltime;
1380 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1381 prl->prefix[i].expire = pr->ndpr_expire;
1382
1383 pfr = pr->ndpr_advrtrs.lh_first;
1384 j = 0;
1385 while (pfr) {
1386 if (j < DRLSTSIZ) {
1387 #define RTRADDR prl->prefix[i].advrtr[j]
1388 RTRADDR = pfr->router->rtaddr;
1389 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1390 /* XXX: hack for KAME */
1391 RTRADDR.s6_addr16[1] = 0;
1392 } else
1393 log(LOG_ERR,
1394 "a router(%s) advertises "
1395 "a prefix with "
1396 "non-link local address\n",
1397 ip6_sprintf(&RTRADDR));
1398 #undef RTRADDR
1399 }
1400 j++;
1401 pfr = pfr->pfr_next;
1402 }
1403 prl->prefix[i].advrtrs = j;
1404 prl->prefix[i].origin = PR_ORIG_RA;
1405
1406 i++;
1407 pr = pr->ndpr_next;
1408 }
1409 {
1410 struct rr_prefix *rpp;
1411
1412 for (rpp = LIST_FIRST(&rr_prefix); rpp;
1413 rpp = LIST_NEXT(rpp, rp_entry)) {
1414 if (i >= PRLSTSIZ)
1415 break;
1416 prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
1417 prl->prefix[i].raflags = rpp->rp_raf;
1418 prl->prefix[i].prefixlen = rpp->rp_plen;
1419 prl->prefix[i].vltime = rpp->rp_vltime;
1420 prl->prefix[i].pltime = rpp->rp_pltime;
1421 prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1422 prl->prefix[i].expire = rpp->rp_expire;
1423 prl->prefix[i].advrtrs = 0;
1424 prl->prefix[i].origin = rpp->rp_origin;
1425 i++;
1426 }
1427 }
1428 splx(s);
1429
1430 break;
1431 case SIOCGIFINFO_IN6:
1432 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1433 error = EINVAL;
1434 break;
1435 }
1436 ndi->ndi = nd_ifinfo[ifp->if_index];
1437 break;
1438 case SIOCSIFINFO_FLAGS:
1439 /* XXX: almost all other fields of ndi->ndi is unused */
1440 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1441 error = EINVAL;
1442 break;
1443 }
1444 nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
1445 break;
1446 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1447 /* flush default router list */
1448 /*
1449 * xxx sumikawa: should not delete route if default
1450 * route equals to the top of default router list
1451 */
1452 bzero(&any, sizeof(any));
1453 defrouter_delreq(&any, 0);
1454 defrouter_select();
1455 /* xxx sumikawa: flush prefix list */
1456 break;
1457 case SIOCSPFXFLUSH_IN6:
1458 {
1459 /* flush all the prefix advertised by routers */
1460 struct nd_prefix *pr, *next;
1461
1462 s = splsoftnet();
1463 for (pr = nd_prefix.lh_first; pr; pr = next) {
1464 next = pr->ndpr_next;
1465 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1466 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1467 prelist_remove(pr);
1468 }
1469 splx(s);
1470 break;
1471 }
1472 case SIOCSRTRFLUSH_IN6:
1473 {
1474 /* flush all the default routers */
1475 struct nd_defrouter *dr, *next;
1476
1477 s = splsoftnet();
1478 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1479 /*
1480 * The first entry of the list may be stored in
1481 * the routing table, so we'll delete it later.
1482 */
1483 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1484 next = TAILQ_NEXT(dr, dr_entry);
1485 defrtrlist_del(dr);
1486 }
1487 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1488 }
1489 splx(s);
1490 break;
1491 }
1492 case SIOCGNBRINFO_IN6:
1493 {
1494 struct llinfo_nd6 *ln;
1495 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1496
1497 /*
1498 * XXX: KAME specific hack for scoped addresses
1499 * XXXX: for other scopes than link-local?
1500 */
1501 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1502 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1503 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1504
1505 if (*idp == 0)
1506 *idp = htons(ifp->if_index);
1507 }
1508
1509 s = splsoftnet();
1510 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1511 error = EINVAL;
1512 splx(s);
1513 break;
1514 }
1515 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1516 nbi->state = ln->ln_state;
1517 nbi->asked = ln->ln_asked;
1518 nbi->isrouter = ln->ln_router;
1519 nbi->expire = ln->ln_expire;
1520 splx(s);
1521
1522 break;
1523 }
1524 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1525 ndif->ifindex = nd6_defifindex;
1526 break;
1527 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1528 return(nd6_setdefaultiface(ndif->ifindex));
1529 break;
1530 }
1531 return(error);
1532 }
1533
1534 /*
1535 * Create neighbor cache entry and cache link-layer address,
1536 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1537 */
1538 struct rtentry *
1539 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1540 struct ifnet *ifp;
1541 struct in6_addr *from;
1542 char *lladdr;
1543 int lladdrlen;
1544 int type; /* ICMP6 type */
1545 int code; /* type dependent information */
1546 {
1547 struct rtentry *rt = NULL;
1548 struct llinfo_nd6 *ln = NULL;
1549 int is_newentry;
1550 struct sockaddr_dl *sdl = NULL;
1551 int do_update;
1552 int olladdr;
1553 int llchange;
1554 int newstate = 0;
1555 long time_second = time.tv_sec;
1556
1557 if (!ifp)
1558 panic("ifp == NULL in nd6_cache_lladdr");
1559 if (!from)
1560 panic("from == NULL in nd6_cache_lladdr");
1561
1562 /* nothing must be updated for unspecified address */
1563 if (IN6_IS_ADDR_UNSPECIFIED(from))
1564 return NULL;
1565
1566 /*
1567 * Validation about ifp->if_addrlen and lladdrlen must be done in
1568 * the caller.
1569 *
1570 * XXX If the link does not have link-layer adderss, what should
1571 * we do? (ifp->if_addrlen == 0)
1572 * Spec says nothing in sections for RA, RS and NA. There's small
1573 * description on it in NS section (RFC 2461 7.2.3).
1574 */
1575
1576 rt = nd6_lookup(from, 0, ifp);
1577 if (!rt) {
1578 #if 0
1579 /* nothing must be done if there's no lladdr */
1580 if (!lladdr || !lladdrlen)
1581 return NULL;
1582 #endif
1583
1584 rt = nd6_lookup(from, 1, ifp);
1585 is_newentry = 1;
1586 } else {
1587 /* do nothing if static ndp is set */
1588 if (rt->rt_flags & RTF_STATIC)
1589 return NULL;
1590 is_newentry = 0;
1591 }
1592
1593 if (!rt)
1594 return NULL;
1595 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1596 fail:
1597 (void)nd6_free(rt);
1598 return NULL;
1599 }
1600 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1601 if (!ln)
1602 goto fail;
1603 if (!rt->rt_gateway)
1604 goto fail;
1605 if (rt->rt_gateway->sa_family != AF_LINK)
1606 goto fail;
1607 sdl = SDL(rt->rt_gateway);
1608
1609 olladdr = (sdl->sdl_alen) ? 1 : 0;
1610 if (olladdr && lladdr) {
1611 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1612 llchange = 1;
1613 else
1614 llchange = 0;
1615 } else
1616 llchange = 0;
1617
1618 /*
1619 * newentry olladdr lladdr llchange (*=record)
1620 * 0 n n -- (1)
1621 * 0 y n -- (2)
1622 * 0 n y -- (3) * STALE
1623 * 0 y y n (4) *
1624 * 0 y y y (5) * STALE
1625 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1626 * 1 -- y -- (7) * STALE
1627 */
1628
1629 if (lladdr) { /*(3-5) and (7)*/
1630 /*
1631 * Record source link-layer address
1632 * XXX is it dependent to ifp->if_type?
1633 */
1634 sdl->sdl_alen = ifp->if_addrlen;
1635 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1636 }
1637
1638 if (!is_newentry) {
1639 if ((!olladdr && lladdr) /*(3)*/
1640 || (olladdr && lladdr && llchange)) { /*(5)*/
1641 do_update = 1;
1642 newstate = ND6_LLINFO_STALE;
1643 } else /*(1-2,4)*/
1644 do_update = 0;
1645 } else {
1646 do_update = 1;
1647 if (!lladdr) /*(6)*/
1648 newstate = ND6_LLINFO_NOSTATE;
1649 else /*(7)*/
1650 newstate = ND6_LLINFO_STALE;
1651 }
1652
1653 if (do_update) {
1654 /*
1655 * Update the state of the neighbor cache.
1656 */
1657 ln->ln_state = newstate;
1658
1659 if (ln->ln_state == ND6_LLINFO_STALE) {
1660 /*
1661 * XXX: since nd6_output() below will cause
1662 * state tansition to DELAY and reset the timer,
1663 * we must set the timer now, although it is actually
1664 * meaningless.
1665 */
1666 ln->ln_expire = time_second + nd6_gctimer;
1667
1668 if (ln->ln_hold) {
1669 #ifdef OLDIP6OUTPUT
1670 ln->ln_asked = 0;
1671 ln->ln_state = ND6_LLINFO_DELAY;
1672 ln->ln_expire = time_second + nd6_delay;
1673 (*ifp->if_output)(ifp, ln->ln_hold,
1674 rt_key(rt), rt);
1675 #else
1676 /*
1677 * we assume ifp is not a p2p here, so just
1678 * set the 2nd argument as the 1st one.
1679 */
1680 nd6_output(ifp, ifp, ln->ln_hold,
1681 (struct sockaddr_in6 *)rt_key(rt),
1682 rt);
1683 #endif
1684 ln->ln_hold = NULL;
1685 }
1686 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1687 /* probe right away */
1688 ln->ln_expire = time_second;
1689 }
1690 }
1691
1692 /*
1693 * ICMP6 type dependent behavior.
1694 *
1695 * NS: clear IsRouter if new entry
1696 * RS: clear IsRouter
1697 * RA: set IsRouter if there's lladdr
1698 * redir: clear IsRouter if new entry
1699 *
1700 * RA case, (1):
1701 * The spec says that we must set IsRouter in the following cases:
1702 * - If lladdr exist, set IsRouter. This means (1-5).
1703 * - If it is old entry (!newentry), set IsRouter. This means (7).
1704 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1705 * A quetion arises for (1) case. (1) case has no lladdr in the
1706 * neighbor cache, this is similar to (6).
1707 * This case is rare but we figured that we MUST NOT set IsRouter.
1708 *
1709 * newentry olladdr lladdr llchange NS RS RA redir
1710 * D R
1711 * 0 n n -- (1) c ? s
1712 * 0 y n -- (2) c s s
1713 * 0 n y -- (3) c s s
1714 * 0 y y n (4) c s s
1715 * 0 y y y (5) c s s
1716 * 1 -- n -- (6) c c c s
1717 * 1 -- y -- (7) c c s c s
1718 *
1719 * (c=clear s=set)
1720 */
1721 switch (type & 0xff) {
1722 case ND_NEIGHBOR_SOLICIT:
1723 /*
1724 * New entry must have is_router flag cleared.
1725 */
1726 if (is_newentry) /*(6-7)*/
1727 ln->ln_router = 0;
1728 break;
1729 case ND_REDIRECT:
1730 /*
1731 * If the icmp is a redirect to a better router, always set the
1732 * is_router flag. Otherwise, if the entry is newly created,
1733 * clear the flag. [RFC 2461, sec 8.3]
1734 */
1735 if (code == ND_REDIRECT_ROUTER)
1736 ln->ln_router = 1;
1737 else if (is_newentry) /*(6-7)*/
1738 ln->ln_router = 0;
1739 break;
1740 case ND_ROUTER_SOLICIT:
1741 /*
1742 * is_router flag must always be cleared.
1743 */
1744 ln->ln_router = 0;
1745 break;
1746 case ND_ROUTER_ADVERT:
1747 /*
1748 * Mark an entry with lladdr as a router.
1749 */
1750 if ((!is_newentry && (olladdr || lladdr)) /*(2-5)*/
1751 || (is_newentry && lladdr)) { /*(7)*/
1752 ln->ln_router = 1;
1753 }
1754 break;
1755 }
1756
1757 return rt;
1758 }
1759
1760 static void
1761 nd6_slowtimo(ignored_arg)
1762 void *ignored_arg;
1763 {
1764 int s = splsoftnet();
1765 int i;
1766 struct nd_ifinfo *nd6if;
1767
1768 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1769 nd6_slowtimo, NULL);
1770 for (i = 1; i < if_index + 1; i++) {
1771 if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
1772 continue;
1773 nd6if = &nd_ifinfo[i];
1774 if (nd6if->basereachable && /* already initialized */
1775 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1776 /*
1777 * Since reachable time rarely changes by router
1778 * advertisements, we SHOULD insure that a new random
1779 * value gets recomputed at least once every few hours.
1780 * (RFC 2461, 6.3.4)
1781 */
1782 nd6if->recalctm = nd6_recalc_reachtm_interval;
1783 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1784 }
1785 }
1786 splx(s);
1787 }
1788
1789 #define senderr(e) { error = (e); goto bad;}
1790 int
1791 nd6_output(ifp, origifp, m0, dst, rt0)
1792 struct ifnet *ifp;
1793 struct ifnet *origifp;
1794 struct mbuf *m0;
1795 struct sockaddr_in6 *dst;
1796 struct rtentry *rt0;
1797 {
1798 struct mbuf *m = m0;
1799 struct rtentry *rt = rt0;
1800 struct sockaddr_in6 *gw6 = NULL;
1801 struct llinfo_nd6 *ln = NULL;
1802 int error = 0;
1803 long time_second = time.tv_sec;
1804
1805 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1806 goto sendpkt;
1807
1808 /*
1809 * XXX: we currently do not make neighbor cache on any interface
1810 * other than ARCnet, Ethernet, FDDI and GIF.
1811 *
1812 * RFC2893 says:
1813 * - unidirectional tunnels needs no ND
1814 */
1815 switch (ifp->if_type) {
1816 case IFT_ARCNET:
1817 case IFT_ETHER:
1818 case IFT_FDDI:
1819 case IFT_IEEE1394:
1820 case IFT_GIF: /* XXX need more cases? */
1821 break;
1822 default:
1823 goto sendpkt;
1824 }
1825
1826 /*
1827 * next hop determination. This routine is derived from ether_outpout.
1828 */
1829 if (rt) {
1830 if ((rt->rt_flags & RTF_UP) == 0) {
1831 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
1832 NULL)
1833 {
1834 rt->rt_refcnt--;
1835 if (rt->rt_ifp != ifp) {
1836 /* XXX: loop care? */
1837 return nd6_output(ifp, origifp, m0,
1838 dst, rt);
1839 }
1840 } else
1841 senderr(EHOSTUNREACH);
1842 }
1843
1844 if (rt->rt_flags & RTF_GATEWAY) {
1845 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1846
1847 /*
1848 * We skip link-layer address resolution and NUD
1849 * if the gateway is not a neighbor from ND point
1850 * of view, regardless the value of the
1851 * nd_ifinfo.flags.
1852 * The second condition is a bit tricky: we skip
1853 * if the gateway is our own address, which is
1854 * sometimes used to install a route to a p2p link.
1855 */
1856 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1857 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1858 /*
1859 * We allow this kind of tricky route only
1860 * when the outgoing interface is p2p.
1861 * XXX: we may need a more generic rule here.
1862 */
1863 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1864 senderr(EHOSTUNREACH);
1865
1866 goto sendpkt;
1867 }
1868
1869 if (rt->rt_gwroute == 0)
1870 goto lookup;
1871 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1872 rtfree(rt); rt = rt0;
1873 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1874 if ((rt = rt->rt_gwroute) == 0)
1875 senderr(EHOSTUNREACH);
1876 }
1877 }
1878 }
1879
1880 /*
1881 * Address resolution or Neighbor Unreachability Detection
1882 * for the next hop.
1883 * At this point, the destination of the packet must be a unicast
1884 * or an anycast address(i.e. not a multicast).
1885 */
1886
1887 /* Look up the neighbor cache for the nexthop */
1888 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1889 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1890 else {
1891 /*
1892 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1893 * the condition below is not very efficient. But we believe
1894 * it is tolerable, because this should be a rare case.
1895 */
1896 if (nd6_is_addr_neighbor(dst, ifp) &&
1897 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1898 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1899 }
1900 if (!ln || !rt) {
1901 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1902 !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
1903 log(LOG_DEBUG,
1904 "nd6_output: can't allocate llinfo for %s "
1905 "(ln=%p, rt=%p)\n",
1906 ip6_sprintf(&dst->sin6_addr), ln, rt);
1907 senderr(EIO); /* XXX: good error? */
1908 }
1909
1910 goto sendpkt; /* send anyway */
1911 }
1912
1913 /* We don't have to do link-layer address resolution on a p2p link. */
1914 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1915 ln->ln_state < ND6_LLINFO_REACHABLE) {
1916 ln->ln_state = ND6_LLINFO_STALE;
1917 ln->ln_expire = time_second + nd6_gctimer;
1918 }
1919
1920 /*
1921 * The first time we send a packet to a neighbor whose entry is
1922 * STALE, we have to change the state to DELAY and a sets a timer to
1923 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1924 * neighbor unreachability detection on expiration.
1925 * (RFC 2461 7.3.3)
1926 */
1927 if (ln->ln_state == ND6_LLINFO_STALE) {
1928 ln->ln_asked = 0;
1929 ln->ln_state = ND6_LLINFO_DELAY;
1930 ln->ln_expire = time_second + nd6_delay;
1931 }
1932
1933 /*
1934 * If the neighbor cache entry has a state other than INCOMPLETE
1935 * (i.e. its link-layer address is already reloved), just
1936 * send the packet.
1937 */
1938 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1939 goto sendpkt;
1940
1941 /*
1942 * There is a neighbor cache entry, but no ethernet address
1943 * response yet. Replace the held mbuf (if any) with this
1944 * latest one.
1945 *
1946 * XXX Does the code conform to rate-limiting rule?
1947 * (RFC 2461 7.2.2)
1948 */
1949 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1950 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1951 if (ln->ln_hold)
1952 m_freem(ln->ln_hold);
1953 ln->ln_hold = m;
1954 if (ln->ln_expire) {
1955 if (ln->ln_asked < nd6_mmaxtries &&
1956 ln->ln_expire < time_second) {
1957 ln->ln_asked++;
1958 ln->ln_expire = time_second +
1959 nd_ifinfo[ifp->if_index].retrans / 1000;
1960 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1961 }
1962 }
1963 return(0);
1964
1965 sendpkt:
1966
1967 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1968 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1969 rt));
1970 }
1971 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1972
1973 bad:
1974 if (m)
1975 m_freem(m);
1976 return (error);
1977 }
1978 #undef senderr
1979
1980 int
1981 nd6_storelladdr(ifp, rt, m, dst, desten)
1982 struct ifnet *ifp;
1983 struct rtentry *rt;
1984 struct mbuf *m;
1985 struct sockaddr *dst;
1986 u_char *desten;
1987 {
1988 struct sockaddr_dl *sdl;
1989
1990 if (m->m_flags & M_MCAST) {
1991 switch (ifp->if_type) {
1992 case IFT_ETHER:
1993 case IFT_FDDI:
1994 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1995 desten);
1996 return(1);
1997 case IFT_IEEE1394:
1998 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
1999 return(1);
2000 case IFT_ARCNET:
2001 *desten = 0;
2002 return(1);
2003 default:
2004 m_freem(m);
2005 return(0);
2006 }
2007 }
2008
2009 if (rt == NULL) {
2010 /* this could happen, if we could not allocate memory */
2011 m_freem(m);
2012 return(0);
2013 }
2014 if (rt->rt_gateway->sa_family != AF_LINK) {
2015 printf("nd6_storelladdr: something odd happens\n");
2016 m_freem(m);
2017 return(0);
2018 }
2019 sdl = SDL(rt->rt_gateway);
2020 if (sdl->sdl_alen == 0) {
2021 /* this should be impossible, but we bark here for debugging */
2022 printf("nd6_storelladdr: sdl_alen == 0\n");
2023 m_freem(m);
2024 return(0);
2025 }
2026
2027 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2028 return(1);
2029 }
2030