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