nd6.c revision 1.48 1 /* $NetBSD: nd6.c,v 1.48 2001/06/27 17:36:14 itojun Exp $ */
2 /* $KAME: nd6.c,v 1.151 2001/06/19 14:24:41 sumikawa Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * XXX
35 * KAME 970409 note:
36 * BSD/OS version heavily modifies this code, related to llinfo.
37 * Since we don't have BSD/OS version of net/route.c in our hand,
38 * I left the code mostly as it was in 970310. -- itojun
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/protosw.h>
51 #include <sys/errno.h>
52 #include <sys/ioctl.h>
53 #include <sys/syslog.h>
54 #include <sys/queue.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_atm.h>
60 #include <net/if_ieee1394.h>
61 #include <net/route.h>
62
63 #include <netinet/in.h>
64 #include <net/if_ether.h>
65 #include <netinet/if_inarp.h>
66 #include <net/if_fddi.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/nd6.h>
71 #include <netinet6/in6_prefix.h>
72 #include <netinet/icmp6.h>
73
74 #include "loop.h"
75 extern struct ifnet loif[NLOOP];
76
77 #include <net/net_osdep.h>
78
79 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
80 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
81
82 #define SIN6(s) ((struct sockaddr_in6 *)s)
83 #define SDL(s) ((struct sockaddr_dl *)s)
84
85 /* timer values */
86 int nd6_prune = 1; /* walk list every 1 seconds */
87 int nd6_delay = 5; /* delay first probe time 5 second */
88 int nd6_umaxtries = 3; /* maximum unicast query */
89 int nd6_mmaxtries = 3; /* maximum multicast query */
90 int nd6_useloopback = 1; /* use loopback interface for local traffic */
91 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
92
93 /* preventing too many loops in ND option parsing */
94 int nd6_maxndopt = 10; /* max # of ND options allowed */
95
96 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
97
98 #ifdef ND6_DEBUG
99 int nd6_debug = 1;
100 #else
101 int nd6_debug = 0;
102 #endif
103
104 /* for debugging? */
105 static int nd6_inuse, nd6_allocated;
106
107 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
108 static size_t nd_ifinfo_indexlim = 8;
109 struct nd_ifinfo *nd_ifinfo = NULL;
110 struct nd_drhead nd_defrouter;
111 struct nd_prhead nd_prefix = { 0 };
112
113 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
114 static struct sockaddr_in6 all1_sa;
115
116 static void nd6_slowtimo __P((void *));
117
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 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
629 /* refresh default router list */
630 bzero(&drany, sizeof(drany));
631 defrouter_delreq(&drany, 0);
632 defrouter_select();
633 }
634
635 /*
636 * Nuke neighbor cache entries for the ifp.
637 * Note that rt->rt_ifp may not be the same as ifp,
638 * due to KAME goto ours hack. See RTM_RESOLVE case in
639 * nd6_rtrequest(), and ip6_input().
640 */
641 ln = llinfo_nd6.ln_next;
642 while (ln && ln != &llinfo_nd6) {
643 struct rtentry *rt;
644 struct sockaddr_dl *sdl;
645
646 nln = ln->ln_next;
647 rt = ln->ln_rt;
648 if (rt && rt->rt_gateway &&
649 rt->rt_gateway->sa_family == AF_LINK) {
650 sdl = (struct sockaddr_dl *)rt->rt_gateway;
651 if (sdl->sdl_index == ifp->if_index)
652 nln = nd6_free(rt);
653 }
654 ln = nln;
655 }
656 }
657
658 struct rtentry *
659 nd6_lookup(addr6, create, ifp)
660 struct in6_addr *addr6;
661 int create;
662 struct ifnet *ifp;
663 {
664 struct rtentry *rt;
665 struct sockaddr_in6 sin6;
666
667 bzero(&sin6, sizeof(sin6));
668 sin6.sin6_len = sizeof(struct sockaddr_in6);
669 sin6.sin6_family = AF_INET6;
670 sin6.sin6_addr = *addr6;
671 rt = rtalloc1((struct sockaddr *)&sin6, create);
672 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
673 /*
674 * This is the case for the default route.
675 * If we want to create a neighbor cache for the address, we
676 * should free the route for the destination and allocate an
677 * interface route.
678 */
679 if (create) {
680 RTFREE(rt);
681 rt = 0;
682 }
683 }
684 if (!rt) {
685 if (create && ifp) {
686 int e;
687
688 /*
689 * If no route is available and create is set,
690 * we allocate a host route for the destination
691 * and treat it like an interface route.
692 * This hack is necessary for a neighbor which can't
693 * be covered by our own prefix.
694 */
695 struct ifaddr *ifa =
696 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
697 if (ifa == NULL)
698 return(NULL);
699
700 /*
701 * Create a new route. RTF_LLINFO is necessary
702 * to create a Neighbor Cache entry for the
703 * destination in nd6_rtrequest which will be
704 * called in rtequest via ifa->ifa_rtrequest.
705 */
706 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
707 ifa->ifa_addr,
708 (struct sockaddr *)&all1_sa,
709 (ifa->ifa_flags |
710 RTF_HOST | RTF_LLINFO) &
711 ~RTF_CLONING,
712 &rt)) != 0)
713 log(LOG_ERR,
714 "nd6_lookup: failed to add route for a "
715 "neighbor(%s), errno=%d\n",
716 ip6_sprintf(addr6), e);
717 if (rt == NULL)
718 return(NULL);
719 if (rt->rt_llinfo) {
720 struct llinfo_nd6 *ln =
721 (struct llinfo_nd6 *)rt->rt_llinfo;
722 ln->ln_state = ND6_LLINFO_NOSTATE;
723 }
724 } else
725 return(NULL);
726 }
727 rt->rt_refcnt--;
728 /*
729 * Validation for the entry.
730 * XXX: we can't use rt->rt_ifp to check for the interface, since
731 * it might be the loopback interface if the entry is for our
732 * own address on a non-loopback interface. Instead, we should
733 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
734 */
735 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
736 rt->rt_gateway->sa_family != AF_LINK ||
737 (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
738 if (create) {
739 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
740 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
741 /* xxx more logs... kazu */
742 }
743 return(0);
744 }
745 return(rt);
746 }
747
748 /*
749 * Detect if a given IPv6 address identifies a neighbor on a given link.
750 * XXX: should take care of the destination of a p2p link?
751 */
752 int
753 nd6_is_addr_neighbor(addr, ifp)
754 struct sockaddr_in6 *addr;
755 struct ifnet *ifp;
756 {
757 struct ifaddr *ifa;
758 int i;
759
760 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
761 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
762
763 /*
764 * A link-local address is always a neighbor.
765 * XXX: we should use the sin6_scope_id field rather than the embedded
766 * interface index.
767 */
768 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
769 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
770 return(1);
771
772 /*
773 * If the address matches one of our addresses,
774 * it should be a neighbor.
775 */
776 for (ifa = ifp->if_addrlist.tqh_first;
777 ifa;
778 ifa = ifa->ifa_list.tqe_next)
779 {
780 if (ifa->ifa_addr->sa_family != AF_INET6)
781 next: continue;
782
783 for (i = 0; i < 4; i++) {
784 if ((IFADDR6(ifa).s6_addr32[i] ^
785 addr->sin6_addr.s6_addr32[i]) &
786 IFMASK6(ifa).s6_addr32[i])
787 goto next;
788 }
789 return(1);
790 }
791
792 /*
793 * Even if the address matches none of our addresses, it might be
794 * in the neighbor cache.
795 */
796 if (nd6_lookup(&addr->sin6_addr, 0, ifp))
797 return(1);
798
799 return(0);
800 #undef IFADDR6
801 #undef IFMASK6
802 }
803
804 /*
805 * Free an nd6 llinfo entry.
806 */
807 struct llinfo_nd6 *
808 nd6_free(rt)
809 struct rtentry *rt;
810 {
811 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
812 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
813 struct nd_defrouter *dr;
814
815 /*
816 * Clear all destination cache entries for the neighbor.
817 * XXX: is it better to restrict this to hosts?
818 */
819 pfctlinput(PRC_HOSTDEAD, rt_key(rt));
820
821 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
822 int s;
823 s = splsoftnet();
824 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
825 rt->rt_ifp);
826 if (ln->ln_router || dr) {
827 /*
828 * rt6_flush must be called whether or not the neighbor
829 * is in the Default Router List.
830 * See a corresponding comment in nd6_na_input().
831 */
832 rt6_flush(&in6, rt->rt_ifp);
833 }
834
835 if (dr) {
836 /*
837 * Unreachablity of a router might affect the default
838 * router selection and on-link detection of advertised
839 * prefixes.
840 */
841
842 /*
843 * Temporarily fake the state to choose a new default
844 * router and to perform on-link determination of
845 * prefixes coreectly.
846 * Below the state will be set correctly,
847 * or the entry itself will be deleted.
848 */
849 ln->ln_state = ND6_LLINFO_INCOMPLETE;
850
851 if (dr == TAILQ_FIRST(&nd_defrouter)) {
852 /*
853 * It is used as the current default router,
854 * so we have to move it to the end of the
855 * list and choose a new one.
856 * XXX: it is not very efficient if this is
857 * the only router.
858 */
859 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
860 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
861
862 defrouter_select();
863 }
864 pfxlist_onlink_check();
865 }
866 splx(s);
867 }
868
869 /*
870 * Before deleting the entry, remember the next entry as the
871 * return value. We need this because pfxlist_onlink_check() above
872 * might have freed other entries (particularly the old next entry) as
873 * a side effect (XXX).
874 */
875 next = ln->ln_next;
876
877 /*
878 * Detach the route from the routing tree and the list of neighbor
879 * caches, and disable the route entry not to be used in already
880 * cached routes.
881 */
882 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
883 rt_mask(rt), 0, (struct rtentry **)0);
884
885 return(next);
886 }
887
888 /*
889 * Upper-layer reachability hint for Neighbor Unreachability Detection.
890 *
891 * XXX cost-effective metods?
892 */
893 void
894 nd6_nud_hint(rt, dst6, force)
895 struct rtentry *rt;
896 struct in6_addr *dst6;
897 int force;
898 {
899 struct llinfo_nd6 *ln;
900 long time_second = time.tv_sec;
901
902 /*
903 * If the caller specified "rt", use that. Otherwise, resolve the
904 * routing table by supplied "dst6".
905 */
906 if (!rt) {
907 if (!dst6)
908 return;
909 if (!(rt = nd6_lookup(dst6, 0, NULL)))
910 return;
911 }
912
913 if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
914 (rt->rt_flags & RTF_LLINFO) == 0 ||
915 !rt->rt_llinfo || !rt->rt_gateway ||
916 rt->rt_gateway->sa_family != AF_LINK) {
917 /* This is not a host route. */
918 return;
919 }
920
921 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
922 if (ln->ln_state < ND6_LLINFO_REACHABLE)
923 return;
924
925 /*
926 * if we get upper-layer reachability confirmation many times,
927 * it is possible we have false information.
928 */
929 if (!force) {
930 ln->ln_byhint++;
931 if (ln->ln_byhint > nd6_maxnudhint)
932 return;
933 }
934
935 ln->ln_state = ND6_LLINFO_REACHABLE;
936 if (ln->ln_expire)
937 ln->ln_expire = time_second +
938 nd_ifinfo[rt->rt_ifp->if_index].reachable;
939 }
940
941 #ifdef OLDIP6OUTPUT
942 /*
943 * Resolve an IP6 address into an ethernet address. If success,
944 * desten is filled in. If there is no entry in ndptab,
945 * set one up and multicast a solicitation for the IP6 address.
946 * Hold onto this mbuf and resend it once the address
947 * is finally resolved. A return value of 1 indicates
948 * that desten has been filled in and the packet should be sent
949 * normally; a 0 return indicates that the packet has been
950 * taken over here, either now or for later transmission.
951 */
952 int
953 nd6_resolve(ifp, rt, m, dst, desten)
954 struct ifnet *ifp;
955 struct rtentry *rt;
956 struct mbuf *m;
957 struct sockaddr *dst;
958 u_char *desten;
959 {
960 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
961 struct sockaddr_dl *sdl;
962 long time_second = time.tv_sec;
963
964 if (m->m_flags & M_MCAST) {
965 switch (ifp->if_type) {
966 case IFT_ETHER:
967 case IFT_FDDI:
968 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
969 desten);
970 return(1);
971 case IFT_IEEE1394:
972 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
973 return(1);
974 case IFT_ARCNET:
975 *desten = 0;
976 return(1);
977 default:
978 m_freem(m);
979 return(0);
980 }
981 }
982 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
983 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
984 else {
985 if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
986 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
987 }
988 if (!ln || !rt) {
989 log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
990 ip6_sprintf(&(SIN6(dst)->sin6_addr)));
991 m_freem(m);
992 return(0);
993 }
994 sdl = SDL(rt->rt_gateway);
995 /*
996 * Ckeck the address family and length is valid, the address
997 * is resolved; otherwise, try to resolve.
998 */
999 if (ln->ln_state >= ND6_LLINFO_REACHABLE
1000 && sdl->sdl_family == AF_LINK
1001 && sdl->sdl_alen != 0) {
1002 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1003 if (ln->ln_state == ND6_LLINFO_STALE) {
1004 ln->ln_asked = 0;
1005 ln->ln_state = ND6_LLINFO_DELAY;
1006 ln->ln_expire = time_second + nd6_delay;
1007 }
1008 return(1);
1009 }
1010 /*
1011 * There is an ndp entry, but no ethernet address
1012 * response yet. Replace the held mbuf with this
1013 * latest one.
1014 *
1015 * XXX Does the code conform to rate-limiting rule?
1016 * (RFC 2461 7.2.2)
1017 */
1018 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1019 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1020 if (ln->ln_hold)
1021 m_freem(ln->ln_hold);
1022 ln->ln_hold = m;
1023 if (ln->ln_expire) {
1024 if (ln->ln_asked < nd6_mmaxtries &&
1025 ln->ln_expire < time_second) {
1026 ln->ln_asked++;
1027 ln->ln_expire = time_second +
1028 nd_ifinfo[ifp->if_index].retrans / 1000;
1029 nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
1030 ln, 0);
1031 }
1032 }
1033 /* do not free mbuf here, it is queued into llinfo_nd6 */
1034 return(0);
1035 }
1036 #endif /* OLDIP6OUTPUT */
1037
1038 void
1039 nd6_rtrequest(req, rt, info)
1040 int req;
1041 struct rtentry *rt;
1042 struct rt_addrinfo *info; /* xxx unused */
1043 {
1044 struct sockaddr *gate = rt->rt_gateway;
1045 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1046 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1047 struct ifnet *ifp = rt->rt_ifp;
1048 struct ifaddr *ifa;
1049 long time_second = time.tv_sec;
1050
1051 if (rt->rt_flags & RTF_GATEWAY)
1052 return;
1053
1054 switch (req) {
1055 case RTM_ADD:
1056 /*
1057 * There is no backward compatibility :)
1058 *
1059 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1060 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1061 * rt->rt_flags |= RTF_CLONING;
1062 */
1063 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1064 /*
1065 * Case 1: This route should come from
1066 * a route to interface. RTF_LLINFO flag is set
1067 * for a host route whose destination should be
1068 * treated as on-link.
1069 */
1070 rt_setgate(rt, rt_key(rt),
1071 (struct sockaddr *)&null_sdl);
1072 gate = rt->rt_gateway;
1073 SDL(gate)->sdl_type = ifp->if_type;
1074 SDL(gate)->sdl_index = ifp->if_index;
1075 if (ln)
1076 ln->ln_expire = time_second;
1077 #if 1
1078 if (ln && ln->ln_expire == 0) {
1079 /* kludge for desktops */
1080 #if 0
1081 printf("nd6_request: time.tv_sec is zero; "
1082 "treat it as 1\n");
1083 #endif
1084 ln->ln_expire = 1;
1085 }
1086 #endif
1087 if (rt->rt_flags & RTF_CLONING)
1088 break;
1089 }
1090 /*
1091 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1092 * We don't do that here since llinfo is not ready yet.
1093 *
1094 * There are also couple of other things to be discussed:
1095 * - unsolicited NA code needs improvement beforehand
1096 * - RFC2461 says we MAY send multicast unsolicited NA
1097 * (7.2.6 paragraph 4), however, it also says that we
1098 * SHOULD provide a mechanism to prevent multicast NA storm.
1099 * we don't have anything like it right now.
1100 * note that the mechanism needs a mutual agreement
1101 * between proxies, which means that we need to implement
1102 * a new protocol, or a new kludge.
1103 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1104 * we need to check ip6forwarding before sending it.
1105 * (or should we allow proxy ND configuration only for
1106 * routers? there's no mention about proxy ND from hosts)
1107 */
1108 #if 0
1109 /* XXX it does not work */
1110 if (rt->rt_flags & RTF_ANNOUNCE)
1111 nd6_na_output(ifp,
1112 &SIN6(rt_key(rt))->sin6_addr,
1113 &SIN6(rt_key(rt))->sin6_addr,
1114 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1115 1, NULL);
1116 #endif
1117 /* FALLTHROUGH */
1118 case RTM_RESOLVE:
1119 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
1120 /*
1121 * Address resolution isn't necessary for a point to
1122 * point link, so we can skip this test for a p2p link.
1123 */
1124 if (gate->sa_family != AF_LINK ||
1125 gate->sa_len < sizeof(null_sdl)) {
1126 log(LOG_DEBUG,
1127 "nd6_rtrequest: bad gateway value: %s\n",
1128 if_name(ifp));
1129 break;
1130 }
1131 SDL(gate)->sdl_type = ifp->if_type;
1132 SDL(gate)->sdl_index = ifp->if_index;
1133 }
1134 if (ln != NULL)
1135 break; /* This happens on a route change */
1136 /*
1137 * Case 2: This route may come from cloning, or a manual route
1138 * add with a LL address.
1139 */
1140 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1141 rt->rt_llinfo = (caddr_t)ln;
1142 if (!ln) {
1143 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1144 break;
1145 }
1146 nd6_inuse++;
1147 nd6_allocated++;
1148 Bzero(ln, sizeof(*ln));
1149 ln->ln_rt = rt;
1150 /* this is required for "ndp" command. - shin */
1151 if (req == RTM_ADD) {
1152 /*
1153 * gate should have some valid AF_LINK entry,
1154 * and ln->ln_expire should have some lifetime
1155 * which is specified by ndp command.
1156 */
1157 ln->ln_state = ND6_LLINFO_REACHABLE;
1158 ln->ln_byhint = 0;
1159 } else {
1160 /*
1161 * When req == RTM_RESOLVE, rt is created and
1162 * initialized in rtrequest(), so rt_expire is 0.
1163 */
1164 ln->ln_state = ND6_LLINFO_NOSTATE;
1165 ln->ln_expire = time_second;
1166 }
1167 rt->rt_flags |= RTF_LLINFO;
1168 ln->ln_next = llinfo_nd6.ln_next;
1169 llinfo_nd6.ln_next = ln;
1170 ln->ln_prev = &llinfo_nd6;
1171 ln->ln_next->ln_prev = ln;
1172
1173 /*
1174 * check if rt_key(rt) is one of my address assigned
1175 * to the interface.
1176 */
1177 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1178 &SIN6(rt_key(rt))->sin6_addr);
1179 if (ifa) {
1180 caddr_t macp = nd6_ifptomac(ifp);
1181 ln->ln_expire = 0;
1182 ln->ln_state = ND6_LLINFO_REACHABLE;
1183 ln->ln_byhint = 0;
1184 if (macp) {
1185 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1186 SDL(gate)->sdl_alen = ifp->if_addrlen;
1187 }
1188 if (nd6_useloopback) {
1189 rt->rt_ifp = &loif[0]; /*XXX*/
1190 /*
1191 * Make sure rt_ifa be equal to the ifaddr
1192 * corresponding to the address.
1193 * We need this because when we refer
1194 * rt_ifa->ia6_flags in ip6_input, we assume
1195 * that the rt_ifa points to the address instead
1196 * of the loopback address.
1197 */
1198 if (ifa != rt->rt_ifa) {
1199 IFAFREE(rt->rt_ifa);
1200 IFAREF(ifa);
1201 rt->rt_ifa = ifa;
1202 }
1203 }
1204 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1205 ln->ln_expire = 0;
1206 ln->ln_state = ND6_LLINFO_REACHABLE;
1207 ln->ln_byhint = 0;
1208
1209 /* join solicited node multicast for proxy ND */
1210 if (ifp->if_flags & IFF_MULTICAST) {
1211 struct in6_addr llsol;
1212 int error;
1213
1214 llsol = SIN6(rt_key(rt))->sin6_addr;
1215 llsol.s6_addr16[0] = htons(0xff02);
1216 llsol.s6_addr16[1] = htons(ifp->if_index);
1217 llsol.s6_addr32[1] = 0;
1218 llsol.s6_addr32[2] = htonl(1);
1219 llsol.s6_addr8[12] = 0xff;
1220
1221 if (!in6_addmulti(&llsol, ifp, &error)) {
1222 nd6log((LOG_ERR, "%s: "
1223 "failed to join %s (errno=%d)\n",
1224 if_name(ifp), ip6_sprintf(&llsol),
1225 error));
1226 }
1227 }
1228 }
1229 break;
1230
1231 case RTM_DELETE:
1232 if (!ln)
1233 break;
1234 /* leave from solicited node multicast for proxy ND */
1235 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1236 (ifp->if_flags & IFF_MULTICAST) != 0) {
1237 struct in6_addr llsol;
1238 struct in6_multi *in6m;
1239
1240 llsol = SIN6(rt_key(rt))->sin6_addr;
1241 llsol.s6_addr16[0] = htons(0xff02);
1242 llsol.s6_addr16[1] = htons(ifp->if_index);
1243 llsol.s6_addr32[1] = 0;
1244 llsol.s6_addr32[2] = htonl(1);
1245 llsol.s6_addr8[12] = 0xff;
1246
1247 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1248 if (in6m)
1249 in6_delmulti(in6m);
1250 }
1251 nd6_inuse--;
1252 ln->ln_next->ln_prev = ln->ln_prev;
1253 ln->ln_prev->ln_next = ln->ln_next;
1254 ln->ln_prev = NULL;
1255 rt->rt_llinfo = 0;
1256 rt->rt_flags &= ~RTF_LLINFO;
1257 if (ln->ln_hold)
1258 m_freem(ln->ln_hold);
1259 Free((caddr_t)ln);
1260 }
1261 }
1262
1263 void
1264 nd6_p2p_rtrequest(req, rt, info)
1265 int req;
1266 struct rtentry *rt;
1267 struct rt_addrinfo *info; /* xxx unused */
1268 {
1269 struct sockaddr *gate = rt->rt_gateway;
1270 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1271 struct ifnet *ifp = rt->rt_ifp;
1272 struct ifaddr *ifa;
1273
1274 if (rt->rt_flags & RTF_GATEWAY)
1275 return;
1276
1277 switch (req) {
1278 case RTM_ADD:
1279 /*
1280 * There is no backward compatibility :)
1281 *
1282 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1283 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1284 * rt->rt_flags |= RTF_CLONING;
1285 */
1286 if (rt->rt_flags & RTF_CLONING) {
1287 /*
1288 * Case 1: This route should come from
1289 * a route to interface.
1290 */
1291 rt_setgate(rt, rt_key(rt),
1292 (struct sockaddr *)&null_sdl);
1293 gate = rt->rt_gateway;
1294 SDL(gate)->sdl_type = ifp->if_type;
1295 SDL(gate)->sdl_index = ifp->if_index;
1296 break;
1297 }
1298 /* Announce a new entry if requested. */
1299 if (rt->rt_flags & RTF_ANNOUNCE)
1300 nd6_na_output(ifp,
1301 &SIN6(rt_key(rt))->sin6_addr,
1302 &SIN6(rt_key(rt))->sin6_addr,
1303 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1304 1, NULL);
1305 /* FALLTHROUGH */
1306 case RTM_RESOLVE:
1307 /*
1308 * check if rt_key(rt) is one of my address assigned
1309 * to the interface.
1310 */
1311 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1312 &SIN6(rt_key(rt))->sin6_addr);
1313 if (ifa) {
1314 if (nd6_useloopback) {
1315 rt->rt_ifp = &loif[0]; /*XXX*/
1316 }
1317 }
1318 break;
1319 }
1320 }
1321
1322 int
1323 nd6_ioctl(cmd, data, ifp)
1324 u_long cmd;
1325 caddr_t data;
1326 struct ifnet *ifp;
1327 {
1328 struct in6_drlist *drl = (struct in6_drlist *)data;
1329 struct in6_prlist *prl = (struct in6_prlist *)data;
1330 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1331 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1332 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1333 struct nd_defrouter *dr, any;
1334 struct nd_prefix *pr;
1335 struct rtentry *rt;
1336 int i = 0, error = 0;
1337 int s;
1338
1339 switch (cmd) {
1340 case SIOCGDRLST_IN6:
1341 bzero(drl, sizeof(*drl));
1342 s = splsoftnet();
1343 dr = TAILQ_FIRST(&nd_defrouter);
1344 while (dr && i < DRLSTSIZ) {
1345 drl->defrouter[i].rtaddr = dr->rtaddr;
1346 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1347 /* XXX: need to this hack for KAME stack */
1348 drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1349 } else
1350 log(LOG_ERR,
1351 "default router list contains a "
1352 "non-linklocal address(%s)\n",
1353 ip6_sprintf(&drl->defrouter[i].rtaddr));
1354
1355 drl->defrouter[i].flags = dr->flags;
1356 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1357 drl->defrouter[i].expire = dr->expire;
1358 drl->defrouter[i].if_index = dr->ifp->if_index;
1359 i++;
1360 dr = TAILQ_NEXT(dr, dr_entry);
1361 }
1362 splx(s);
1363 break;
1364 case SIOCGPRLST_IN6:
1365 /*
1366 * XXX meaning of fields, especialy "raflags", is very
1367 * differnet between RA prefix list and RR/static prefix list.
1368 * how about separating ioctls into two?
1369 */
1370 bzero(prl, sizeof(*prl));
1371 s = splsoftnet();
1372 pr = nd_prefix.lh_first;
1373 while (pr && i < PRLSTSIZ) {
1374 struct nd_pfxrouter *pfr;
1375 int j;
1376
1377 prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1378 prl->prefix[i].raflags = pr->ndpr_raf;
1379 prl->prefix[i].prefixlen = pr->ndpr_plen;
1380 prl->prefix[i].vltime = pr->ndpr_vltime;
1381 prl->prefix[i].pltime = pr->ndpr_pltime;
1382 prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1383 prl->prefix[i].expire = pr->ndpr_expire;
1384
1385 pfr = pr->ndpr_advrtrs.lh_first;
1386 j = 0;
1387 while (pfr) {
1388 if (j < DRLSTSIZ) {
1389 #define RTRADDR prl->prefix[i].advrtr[j]
1390 RTRADDR = pfr->router->rtaddr;
1391 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1392 /* XXX: hack for KAME */
1393 RTRADDR.s6_addr16[1] = 0;
1394 } else
1395 log(LOG_ERR,
1396 "a router(%s) advertises "
1397 "a prefix with "
1398 "non-link local address\n",
1399 ip6_sprintf(&RTRADDR));
1400 #undef RTRADDR
1401 }
1402 j++;
1403 pfr = pfr->pfr_next;
1404 }
1405 prl->prefix[i].advrtrs = j;
1406 prl->prefix[i].origin = PR_ORIG_RA;
1407
1408 i++;
1409 pr = pr->ndpr_next;
1410 }
1411 {
1412 struct rr_prefix *rpp;
1413
1414 for (rpp = LIST_FIRST(&rr_prefix); rpp;
1415 rpp = LIST_NEXT(rpp, rp_entry)) {
1416 if (i >= PRLSTSIZ)
1417 break;
1418 prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
1419 prl->prefix[i].raflags = rpp->rp_raf;
1420 prl->prefix[i].prefixlen = rpp->rp_plen;
1421 prl->prefix[i].vltime = rpp->rp_vltime;
1422 prl->prefix[i].pltime = rpp->rp_pltime;
1423 prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1424 prl->prefix[i].expire = rpp->rp_expire;
1425 prl->prefix[i].advrtrs = 0;
1426 prl->prefix[i].origin = rpp->rp_origin;
1427 i++;
1428 }
1429 }
1430 splx(s);
1431
1432 break;
1433 case SIOCGIFINFO_IN6:
1434 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1435 error = EINVAL;
1436 break;
1437 }
1438 ndi->ndi = nd_ifinfo[ifp->if_index];
1439 break;
1440 case SIOCSIFINFO_FLAGS:
1441 /* XXX: almost all other fields of ndi->ndi is unused */
1442 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1443 error = EINVAL;
1444 break;
1445 }
1446 nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
1447 break;
1448 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1449 /* flush default router list */
1450 /*
1451 * xxx sumikawa: should not delete route if default
1452 * route equals to the top of default router list
1453 */
1454 bzero(&any, sizeof(any));
1455 defrouter_delreq(&any, 0);
1456 defrouter_select();
1457 /* xxx sumikawa: flush prefix list */
1458 break;
1459 case SIOCSPFXFLUSH_IN6:
1460 {
1461 /* flush all the prefix advertised by routers */
1462 struct nd_prefix *pr, *next;
1463
1464 s = splsoftnet();
1465 for (pr = nd_prefix.lh_first; pr; pr = next) {
1466 next = pr->ndpr_next;
1467 if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1468 in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1469 prelist_remove(pr);
1470 }
1471 splx(s);
1472 break;
1473 }
1474 case SIOCSRTRFLUSH_IN6:
1475 {
1476 /* flush all the default routers */
1477 struct nd_defrouter *dr, *next;
1478
1479 s = splsoftnet();
1480 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1481 /*
1482 * The first entry of the list may be stored in
1483 * the routing table, so we'll delete it later.
1484 */
1485 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1486 next = TAILQ_NEXT(dr, dr_entry);
1487 defrtrlist_del(dr);
1488 }
1489 defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1490 }
1491 splx(s);
1492 break;
1493 }
1494 case SIOCGNBRINFO_IN6:
1495 {
1496 struct llinfo_nd6 *ln;
1497 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1498
1499 /*
1500 * XXX: KAME specific hack for scoped addresses
1501 * XXXX: for other scopes than link-local?
1502 */
1503 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1504 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1505 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1506
1507 if (*idp == 0)
1508 *idp = htons(ifp->if_index);
1509 }
1510
1511 s = splsoftnet();
1512 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1513 error = EINVAL;
1514 splx(s);
1515 break;
1516 }
1517 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1518 nbi->state = ln->ln_state;
1519 nbi->asked = ln->ln_asked;
1520 nbi->isrouter = ln->ln_router;
1521 nbi->expire = ln->ln_expire;
1522 splx(s);
1523
1524 break;
1525 }
1526 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1527 ndif->ifindex = nd6_defifindex;
1528 break;
1529 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1530 return(nd6_setdefaultiface(ndif->ifindex));
1531 break;
1532 }
1533 return(error);
1534 }
1535
1536 /*
1537 * Create neighbor cache entry and cache link-layer address,
1538 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1539 */
1540 struct rtentry *
1541 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1542 struct ifnet *ifp;
1543 struct in6_addr *from;
1544 char *lladdr;
1545 int lladdrlen;
1546 int type; /* ICMP6 type */
1547 int code; /* type dependent information */
1548 {
1549 struct rtentry *rt = NULL;
1550 struct llinfo_nd6 *ln = NULL;
1551 int is_newentry;
1552 struct sockaddr_dl *sdl = NULL;
1553 int do_update;
1554 int olladdr;
1555 int llchange;
1556 int newstate = 0;
1557 long time_second = time.tv_sec;
1558
1559 if (!ifp)
1560 panic("ifp == NULL in nd6_cache_lladdr");
1561 if (!from)
1562 panic("from == NULL in nd6_cache_lladdr");
1563
1564 /* nothing must be updated for unspecified address */
1565 if (IN6_IS_ADDR_UNSPECIFIED(from))
1566 return NULL;
1567
1568 /*
1569 * Validation about ifp->if_addrlen and lladdrlen must be done in
1570 * the caller.
1571 *
1572 * XXX If the link does not have link-layer adderss, what should
1573 * we do? (ifp->if_addrlen == 0)
1574 * Spec says nothing in sections for RA, RS and NA. There's small
1575 * description on it in NS section (RFC 2461 7.2.3).
1576 */
1577
1578 rt = nd6_lookup(from, 0, ifp);
1579 if (!rt) {
1580 #if 0
1581 /* nothing must be done if there's no lladdr */
1582 if (!lladdr || !lladdrlen)
1583 return NULL;
1584 #endif
1585
1586 rt = nd6_lookup(from, 1, ifp);
1587 is_newentry = 1;
1588 } else {
1589 /* do nothing if static ndp is set */
1590 if (rt->rt_flags & RTF_STATIC)
1591 return NULL;
1592 is_newentry = 0;
1593 }
1594
1595 if (!rt)
1596 return NULL;
1597 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1598 fail:
1599 (void)nd6_free(rt);
1600 return NULL;
1601 }
1602 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1603 if (!ln)
1604 goto fail;
1605 if (!rt->rt_gateway)
1606 goto fail;
1607 if (rt->rt_gateway->sa_family != AF_LINK)
1608 goto fail;
1609 sdl = SDL(rt->rt_gateway);
1610
1611 olladdr = (sdl->sdl_alen) ? 1 : 0;
1612 if (olladdr && lladdr) {
1613 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1614 llchange = 1;
1615 else
1616 llchange = 0;
1617 } else
1618 llchange = 0;
1619
1620 /*
1621 * newentry olladdr lladdr llchange (*=record)
1622 * 0 n n -- (1)
1623 * 0 y n -- (2)
1624 * 0 n y -- (3) * STALE
1625 * 0 y y n (4) *
1626 * 0 y y y (5) * STALE
1627 * 1 -- n -- (6) NOSTATE(= PASSIVE)
1628 * 1 -- y -- (7) * STALE
1629 */
1630
1631 if (lladdr) { /*(3-5) and (7)*/
1632 /*
1633 * Record source link-layer address
1634 * XXX is it dependent to ifp->if_type?
1635 */
1636 sdl->sdl_alen = ifp->if_addrlen;
1637 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1638 }
1639
1640 if (!is_newentry) {
1641 if ((!olladdr && lladdr) /*(3)*/
1642 || (olladdr && lladdr && llchange)) { /*(5)*/
1643 do_update = 1;
1644 newstate = ND6_LLINFO_STALE;
1645 } else /*(1-2,4)*/
1646 do_update = 0;
1647 } else {
1648 do_update = 1;
1649 if (!lladdr) /*(6)*/
1650 newstate = ND6_LLINFO_NOSTATE;
1651 else /*(7)*/
1652 newstate = ND6_LLINFO_STALE;
1653 }
1654
1655 if (do_update) {
1656 /*
1657 * Update the state of the neighbor cache.
1658 */
1659 ln->ln_state = newstate;
1660
1661 if (ln->ln_state == ND6_LLINFO_STALE) {
1662 /*
1663 * XXX: since nd6_output() below will cause
1664 * state tansition to DELAY and reset the timer,
1665 * we must set the timer now, although it is actually
1666 * meaningless.
1667 */
1668 ln->ln_expire = time_second + nd6_gctimer;
1669
1670 if (ln->ln_hold) {
1671 #ifdef OLDIP6OUTPUT
1672 ln->ln_asked = 0;
1673 ln->ln_state = ND6_LLINFO_DELAY;
1674 ln->ln_expire = time_second + nd6_delay;
1675 (*ifp->if_output)(ifp, ln->ln_hold,
1676 rt_key(rt), rt);
1677 #else
1678 /*
1679 * we assume ifp is not a p2p here, so just
1680 * set the 2nd argument as the 1st one.
1681 */
1682 nd6_output(ifp, ifp, ln->ln_hold,
1683 (struct sockaddr_in6 *)rt_key(rt),
1684 rt);
1685 #endif
1686 ln->ln_hold = NULL;
1687 }
1688 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1689 /* probe right away */
1690 ln->ln_expire = time_second;
1691 }
1692 }
1693
1694 /*
1695 * ICMP6 type dependent behavior.
1696 *
1697 * NS: clear IsRouter if new entry
1698 * RS: clear IsRouter
1699 * RA: set IsRouter if there's lladdr
1700 * redir: clear IsRouter if new entry
1701 *
1702 * RA case, (1):
1703 * The spec says that we must set IsRouter in the following cases:
1704 * - If lladdr exist, set IsRouter. This means (1-5).
1705 * - If it is old entry (!newentry), set IsRouter. This means (7).
1706 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1707 * A quetion arises for (1) case. (1) case has no lladdr in the
1708 * neighbor cache, this is similar to (6).
1709 * This case is rare but we figured that we MUST NOT set IsRouter.
1710 *
1711 * newentry olladdr lladdr llchange NS RS RA redir
1712 * D R
1713 * 0 n n -- (1) c ? s
1714 * 0 y n -- (2) c s s
1715 * 0 n y -- (3) c s s
1716 * 0 y y n (4) c s s
1717 * 0 y y y (5) c s s
1718 * 1 -- n -- (6) c c c s
1719 * 1 -- y -- (7) c c s c s
1720 *
1721 * (c=clear s=set)
1722 */
1723 switch (type & 0xff) {
1724 case ND_NEIGHBOR_SOLICIT:
1725 /*
1726 * New entry must have is_router flag cleared.
1727 */
1728 if (is_newentry) /*(6-7)*/
1729 ln->ln_router = 0;
1730 break;
1731 case ND_REDIRECT:
1732 /*
1733 * If the icmp is a redirect to a better router, always set the
1734 * is_router flag. Otherwise, if the entry is newly created,
1735 * clear the flag. [RFC 2461, sec 8.3]
1736 */
1737 if (code == ND_REDIRECT_ROUTER)
1738 ln->ln_router = 1;
1739 else if (is_newentry) /*(6-7)*/
1740 ln->ln_router = 0;
1741 break;
1742 case ND_ROUTER_SOLICIT:
1743 /*
1744 * is_router flag must always be cleared.
1745 */
1746 ln->ln_router = 0;
1747 break;
1748 case ND_ROUTER_ADVERT:
1749 /*
1750 * Mark an entry with lladdr as a router.
1751 */
1752 if ((!is_newentry && (olladdr || lladdr)) /*(2-5)*/
1753 || (is_newentry && lladdr)) { /*(7)*/
1754 ln->ln_router = 1;
1755 }
1756 break;
1757 }
1758
1759 /*
1760 * When the link-layer address of a router changes, select the
1761 * best router again. In particular, when the neighbor entry is newly
1762 * created, it might affect the selection policy.
1763 * Question: can we restrict the first condition to the "is_newentry"
1764 * case?
1765 * XXX: when we hear an RA from a new router with the link-layer
1766 * address option, defrouter_select() is called twice, since
1767 * defrtrlist_update called the function as well. However, I believe
1768 * we can compromise the overhead, since it only happens the first
1769 * time.
1770 */
1771 if (do_update && ln->ln_router)
1772 defrouter_select();
1773
1774 return rt;
1775 }
1776
1777 static void
1778 nd6_slowtimo(ignored_arg)
1779 void *ignored_arg;
1780 {
1781 int s = splsoftnet();
1782 int i;
1783 struct nd_ifinfo *nd6if;
1784
1785 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1786 nd6_slowtimo, NULL);
1787 for (i = 1; i < if_index + 1; i++) {
1788 if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
1789 continue;
1790 nd6if = &nd_ifinfo[i];
1791 if (nd6if->basereachable && /* already initialized */
1792 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1793 /*
1794 * Since reachable time rarely changes by router
1795 * advertisements, we SHOULD insure that a new random
1796 * value gets recomputed at least once every few hours.
1797 * (RFC 2461, 6.3.4)
1798 */
1799 nd6if->recalctm = nd6_recalc_reachtm_interval;
1800 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1801 }
1802 }
1803 splx(s);
1804 }
1805
1806 #define senderr(e) { error = (e); goto bad;}
1807 int
1808 nd6_output(ifp, origifp, m0, dst, rt0)
1809 struct ifnet *ifp;
1810 struct ifnet *origifp;
1811 struct mbuf *m0;
1812 struct sockaddr_in6 *dst;
1813 struct rtentry *rt0;
1814 {
1815 struct mbuf *m = m0;
1816 struct rtentry *rt = rt0;
1817 struct sockaddr_in6 *gw6 = NULL;
1818 struct llinfo_nd6 *ln = NULL;
1819 int error = 0;
1820 long time_second = time.tv_sec;
1821
1822 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1823 goto sendpkt;
1824
1825 /*
1826 * XXX: we currently do not make neighbor cache on any interface
1827 * other than ARCnet, Ethernet, FDDI and GIF.
1828 *
1829 * RFC2893 says:
1830 * - unidirectional tunnels needs no ND
1831 */
1832 switch (ifp->if_type) {
1833 case IFT_ARCNET:
1834 case IFT_ETHER:
1835 case IFT_FDDI:
1836 case IFT_IEEE1394:
1837 case IFT_GIF: /* XXX need more cases? */
1838 break;
1839 default:
1840 goto sendpkt;
1841 }
1842
1843 /*
1844 * next hop determination. This routine is derived from ether_outpout.
1845 */
1846 if (rt) {
1847 if ((rt->rt_flags & RTF_UP) == 0) {
1848 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
1849 NULL)
1850 {
1851 rt->rt_refcnt--;
1852 if (rt->rt_ifp != ifp) {
1853 /* XXX: loop care? */
1854 return nd6_output(ifp, origifp, m0,
1855 dst, rt);
1856 }
1857 } else
1858 senderr(EHOSTUNREACH);
1859 }
1860
1861 if (rt->rt_flags & RTF_GATEWAY) {
1862 gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1863
1864 /*
1865 * We skip link-layer address resolution and NUD
1866 * if the gateway is not a neighbor from ND point
1867 * of view, regardless the value of the
1868 * nd_ifinfo.flags.
1869 * The second condition is a bit tricky: we skip
1870 * if the gateway is our own address, which is
1871 * sometimes used to install a route to a p2p link.
1872 */
1873 if (!nd6_is_addr_neighbor(gw6, ifp) ||
1874 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1875 /*
1876 * We allow this kind of tricky route only
1877 * when the outgoing interface is p2p.
1878 * XXX: we may need a more generic rule here.
1879 */
1880 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1881 senderr(EHOSTUNREACH);
1882
1883 goto sendpkt;
1884 }
1885
1886 if (rt->rt_gwroute == 0)
1887 goto lookup;
1888 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1889 rtfree(rt); rt = rt0;
1890 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1891 if ((rt = rt->rt_gwroute) == 0)
1892 senderr(EHOSTUNREACH);
1893 }
1894 }
1895 }
1896
1897 /*
1898 * Address resolution or Neighbor Unreachability Detection
1899 * for the next hop.
1900 * At this point, the destination of the packet must be a unicast
1901 * or an anycast address(i.e. not a multicast).
1902 */
1903
1904 /* Look up the neighbor cache for the nexthop */
1905 if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1906 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1907 else {
1908 /*
1909 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1910 * the condition below is not very efficient. But we believe
1911 * it is tolerable, because this should be a rare case.
1912 */
1913 if (nd6_is_addr_neighbor(dst, ifp) &&
1914 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1915 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1916 }
1917 if (!ln || !rt) {
1918 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1919 !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
1920 log(LOG_DEBUG,
1921 "nd6_output: can't allocate llinfo for %s "
1922 "(ln=%p, rt=%p)\n",
1923 ip6_sprintf(&dst->sin6_addr), ln, rt);
1924 senderr(EIO); /* XXX: good error? */
1925 }
1926
1927 goto sendpkt; /* send anyway */
1928 }
1929
1930 /* We don't have to do link-layer address resolution on a p2p link. */
1931 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1932 ln->ln_state < ND6_LLINFO_REACHABLE) {
1933 ln->ln_state = ND6_LLINFO_STALE;
1934 ln->ln_expire = time_second + nd6_gctimer;
1935 }
1936
1937 /*
1938 * The first time we send a packet to a neighbor whose entry is
1939 * STALE, we have to change the state to DELAY and a sets a timer to
1940 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1941 * neighbor unreachability detection on expiration.
1942 * (RFC 2461 7.3.3)
1943 */
1944 if (ln->ln_state == ND6_LLINFO_STALE) {
1945 ln->ln_asked = 0;
1946 ln->ln_state = ND6_LLINFO_DELAY;
1947 ln->ln_expire = time_second + nd6_delay;
1948 }
1949
1950 /*
1951 * If the neighbor cache entry has a state other than INCOMPLETE
1952 * (i.e. its link-layer address is already reloved), just
1953 * send the packet.
1954 */
1955 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1956 goto sendpkt;
1957
1958 /*
1959 * There is a neighbor cache entry, but no ethernet address
1960 * response yet. Replace the held mbuf (if any) with this
1961 * latest one.
1962 *
1963 * XXX Does the code conform to rate-limiting rule?
1964 * (RFC 2461 7.2.2)
1965 */
1966 if (ln->ln_state == ND6_LLINFO_NOSTATE)
1967 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1968 if (ln->ln_hold)
1969 m_freem(ln->ln_hold);
1970 ln->ln_hold = m;
1971 if (ln->ln_expire) {
1972 if (ln->ln_asked < nd6_mmaxtries &&
1973 ln->ln_expire < time_second) {
1974 ln->ln_asked++;
1975 ln->ln_expire = time_second +
1976 nd_ifinfo[ifp->if_index].retrans / 1000;
1977 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1978 }
1979 }
1980 return(0);
1981
1982 sendpkt:
1983
1984 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1985 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1986 rt));
1987 }
1988 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1989
1990 bad:
1991 if (m)
1992 m_freem(m);
1993 return (error);
1994 }
1995 #undef senderr
1996
1997 int
1998 nd6_storelladdr(ifp, rt, m, dst, desten)
1999 struct ifnet *ifp;
2000 struct rtentry *rt;
2001 struct mbuf *m;
2002 struct sockaddr *dst;
2003 u_char *desten;
2004 {
2005 struct sockaddr_dl *sdl;
2006
2007 if (m->m_flags & M_MCAST) {
2008 switch (ifp->if_type) {
2009 case IFT_ETHER:
2010 case IFT_FDDI:
2011 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2012 desten);
2013 return(1);
2014 case IFT_IEEE1394:
2015 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
2016 return(1);
2017 case IFT_ARCNET:
2018 *desten = 0;
2019 return(1);
2020 default:
2021 m_freem(m);
2022 return(0);
2023 }
2024 }
2025
2026 if (rt == NULL) {
2027 /* this could happen, if we could not allocate memory */
2028 m_freem(m);
2029 return(0);
2030 }
2031 if (rt->rt_gateway->sa_family != AF_LINK) {
2032 printf("nd6_storelladdr: something odd happens\n");
2033 m_freem(m);
2034 return(0);
2035 }
2036 sdl = SDL(rt->rt_gateway);
2037 if (sdl->sdl_alen == 0) {
2038 /* this should be impossible, but we bark here for debugging */
2039 printf("nd6_storelladdr: sdl_alen == 0\n");
2040 m_freem(m);
2041 return(0);
2042 }
2043
2044 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2045 return(1);
2046 }
2047