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