nd6.c revision 1.266 1 /* $NetBSD: nd6.c,v 1.266 2020/01/20 18:38:22 thorpej Exp $ */
2 /* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.266 2020/01/20 18:38:22 thorpej Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_net_mpsafe.h"
38 #endif
39
40 #include "bridge.h"
41 #include "carp.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/kmem.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sockio.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/errno.h>
54 #include <sys/ioctl.h>
55 #include <sys/syslog.h>
56 #include <sys/queue.h>
57 #include <sys/cprng.h>
58 #include <sys/workqueue.h>
59
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_llatbl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65 #include <net/if_ether.h>
66 #include <net/if_arc.h>
67
68 #include <netinet/in.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet6/in6_ifattach.h>
75 #include <netinet/icmp6.h>
76 #include <netinet6/icmp6_private.h>
77
78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
80
81 /* timer values */
82 int nd6_prune = 1; /* walk list every 1 seconds */
83 int nd6_delay = 5; /* delay first probe time 5 second */
84 int nd6_umaxtries = 3; /* maximum unicast query */
85 int nd6_mmaxtries = 3; /* maximum multicast query */
86 int nd6_useloopback = 1; /* use loopback interface for local traffic */
87 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
88
89 /* preventing too many loops in ND option parsing */
90 int nd6_maxndopt = 10; /* max # of ND options allowed */
91
92 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
93
94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
95
96 #ifdef ND6_DEBUG
97 int nd6_debug = 1;
98 #else
99 int nd6_debug = 0;
100 #endif
101
102 krwlock_t nd6_lock __cacheline_aligned;
103
104 struct nd_drhead nd_defrouter;
105 struct nd_prhead nd_prefix = { 0 };
106
107 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
108
109 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
110 static void nd6_slowtimo(void *);
111 static int regen_tmpaddr(const struct in6_ifaddr *);
112 static void nd6_free(struct llentry *, int);
113 static void nd6_llinfo_timer(void *);
114 static void nd6_timer(void *);
115 static void nd6_timer_work(struct work *, void *);
116 static void clear_llinfo_pqueue(struct llentry *);
117 static struct nd_opt_hdr *nd6_option(union nd_opts *);
118
119 static callout_t nd6_slowtimo_ch;
120 static callout_t nd6_timer_ch;
121 static struct workqueue *nd6_timer_wq;
122 static struct work nd6_timer_wk;
123
124 static int fill_drlist(void *, size_t *);
125 static int fill_prlist(void *, size_t *);
126
127 static struct ifnet *nd6_defifp;
128 static int nd6_defifindex;
129
130 static int nd6_setdefaultiface(int);
131
132 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
133
134 void
135 nd6_init(void)
136 {
137 int error;
138
139 nd6_nbr_init();
140
141 rw_init(&nd6_lock);
142
143 /* initialization of the default router list */
144 ND_DEFROUTER_LIST_INIT();
145
146 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
147 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
148
149 error = workqueue_create(&nd6_timer_wq, "nd6_timer",
150 nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
151 if (error)
152 panic("%s: workqueue_create failed (%d)\n", __func__, error);
153
154 /* start timer */
155 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
156 nd6_slowtimo, NULL);
157 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
158 }
159
160 struct nd_ifinfo *
161 nd6_ifattach(struct ifnet *ifp)
162 {
163 struct nd_ifinfo *nd;
164
165 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
166
167 nd->initialized = 1;
168
169 nd->chlim = IPV6_DEFHLIM;
170 nd->basereachable = REACHABLE_TIME;
171 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
172 nd->retrans = RETRANS_TIMER;
173
174 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
175
176 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
177 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
178 * because one of its members should. */
179 if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
180 (ifp->if_flags & IFF_LOOPBACK))
181 nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
182
183 /* A loopback interface does not need to accept RTADV.
184 * A bridge interface should not accept RTADV
185 * because one of its members should. */
186 if (ip6_accept_rtadv &&
187 !(ifp->if_flags & IFF_LOOPBACK) &&
188 !(ifp->if_type != IFT_BRIDGE))
189 nd->flags |= ND6_IFF_ACCEPT_RTADV;
190
191 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
192 nd6_setmtu0(ifp, nd);
193
194 return nd;
195 }
196
197 void
198 nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
199 {
200
201 /* Ensure all IPv6 addresses are purged before calling nd6_purge */
202 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
203 nd6_purge(ifp, ext);
204 kmem_free(ext->nd_ifinfo, sizeof(struct nd_ifinfo));
205 }
206
207 void
208 nd6_setmtu(struct ifnet *ifp)
209 {
210 nd6_setmtu0(ifp, ND_IFINFO(ifp));
211 }
212
213 void
214 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
215 {
216 u_int32_t omaxmtu;
217
218 omaxmtu = ndi->maxmtu;
219
220 switch (ifp->if_type) {
221 case IFT_ARCNET:
222 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
223 break;
224 default:
225 ndi->maxmtu = ifp->if_mtu;
226 break;
227 }
228
229 /*
230 * Decreasing the interface MTU under IPV6 minimum MTU may cause
231 * undesirable situation. We thus notify the operator of the change
232 * explicitly. The check for omaxmtu is necessary to restrict the
233 * log to the case of changing the MTU, not initializing it.
234 */
235 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
236 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
237 " small for IPv6 which needs %lu\n",
238 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
239 IPV6_MMTU);
240 }
241
242 if (ndi->maxmtu > in6_maxmtu)
243 in6_setmaxmtu(); /* check all interfaces just in case */
244 }
245
246 void
247 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
248 {
249
250 memset(ndopts, 0, sizeof(*ndopts));
251 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
252 ndopts->nd_opts_last
253 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
254
255 if (icmp6len == 0) {
256 ndopts->nd_opts_done = 1;
257 ndopts->nd_opts_search = NULL;
258 }
259 }
260
261 /*
262 * Take one ND option.
263 */
264 static struct nd_opt_hdr *
265 nd6_option(union nd_opts *ndopts)
266 {
267 struct nd_opt_hdr *nd_opt;
268 int olen;
269
270 KASSERT(ndopts != NULL);
271 KASSERT(ndopts->nd_opts_last != NULL);
272
273 if (ndopts->nd_opts_search == NULL)
274 return NULL;
275 if (ndopts->nd_opts_done)
276 return NULL;
277
278 nd_opt = ndopts->nd_opts_search;
279
280 /* make sure nd_opt_len is inside the buffer */
281 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
282 memset(ndopts, 0, sizeof(*ndopts));
283 return NULL;
284 }
285
286 olen = nd_opt->nd_opt_len << 3;
287 if (olen == 0) {
288 /*
289 * Message validation requires that all included
290 * options have a length that is greater than zero.
291 */
292 memset(ndopts, 0, sizeof(*ndopts));
293 return NULL;
294 }
295
296 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
297 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
298 /* option overruns the end of buffer, invalid */
299 memset(ndopts, 0, sizeof(*ndopts));
300 return NULL;
301 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
302 /* reached the end of options chain */
303 ndopts->nd_opts_done = 1;
304 ndopts->nd_opts_search = NULL;
305 }
306 return nd_opt;
307 }
308
309 /*
310 * Parse multiple ND options.
311 * This function is much easier to use, for ND routines that do not need
312 * multiple options of the same type.
313 */
314 int
315 nd6_options(union nd_opts *ndopts)
316 {
317 struct nd_opt_hdr *nd_opt;
318 int i = 0;
319
320 KASSERT(ndopts != NULL);
321 KASSERT(ndopts->nd_opts_last != NULL);
322
323 if (ndopts->nd_opts_search == NULL)
324 return 0;
325
326 while (1) {
327 nd_opt = nd6_option(ndopts);
328 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
329 /*
330 * Message validation requires that all included
331 * options have a length that is greater than zero.
332 */
333 ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
334 memset(ndopts, 0, sizeof(*ndopts));
335 return -1;
336 }
337
338 if (nd_opt == NULL)
339 goto skip1;
340
341 switch (nd_opt->nd_opt_type) {
342 case ND_OPT_SOURCE_LINKADDR:
343 case ND_OPT_TARGET_LINKADDR:
344 case ND_OPT_MTU:
345 case ND_OPT_REDIRECTED_HEADER:
346 case ND_OPT_NONCE:
347 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
348 nd6log(LOG_INFO,
349 "duplicated ND6 option found (type=%d)\n",
350 nd_opt->nd_opt_type);
351 /* XXX bark? */
352 } else {
353 ndopts->nd_opt_array[nd_opt->nd_opt_type]
354 = nd_opt;
355 }
356 break;
357 case ND_OPT_PREFIX_INFORMATION:
358 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
359 ndopts->nd_opt_array[nd_opt->nd_opt_type]
360 = nd_opt;
361 }
362 ndopts->nd_opts_pi_end =
363 (struct nd_opt_prefix_info *)nd_opt;
364 break;
365 default:
366 /*
367 * Unknown options must be silently ignored,
368 * to accommodate future extension to the protocol.
369 */
370 nd6log(LOG_DEBUG,
371 "nd6_options: unsupported option %d - "
372 "option ignored\n", nd_opt->nd_opt_type);
373 }
374
375 skip1:
376 i++;
377 if (i > nd6_maxndopt) {
378 ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
379 nd6log(LOG_INFO, "too many loop in nd opt\n");
380 break;
381 }
382
383 if (ndopts->nd_opts_done)
384 break;
385 }
386
387 return 0;
388 }
389
390 /*
391 * ND6 timer routine to handle ND6 entries
392 */
393 void
394 nd6_llinfo_settimer(struct llentry *ln, time_t xtick)
395 {
396
397 CTASSERT(sizeof(time_t) > sizeof(int));
398 LLE_WLOCK_ASSERT(ln);
399
400 KASSERT(xtick >= 0);
401
402 /*
403 * We have to take care of a reference leak which occurs if
404 * callout_reset overwrites a pending callout schedule. Unfortunately
405 * we don't have a mean to know the overwrite, so we need to know it
406 * using callout_stop. We need to call callout_pending first to exclude
407 * the case that the callout has never been scheduled.
408 */
409 if (callout_pending(&ln->la_timer)) {
410 bool expired = callout_stop(&ln->la_timer);
411 if (!expired)
412 LLE_REMREF(ln);
413 }
414
415 ln->ln_expire = time_uptime + xtick / hz;
416 LLE_ADDREF(ln);
417 if (xtick > INT_MAX) {
418 ln->ln_ntick = xtick - INT_MAX;
419 callout_reset(&ln->ln_timer_ch, INT_MAX,
420 nd6_llinfo_timer, ln);
421 } else {
422 ln->ln_ntick = 0;
423 callout_reset(&ln->ln_timer_ch, xtick,
424 nd6_llinfo_timer, ln);
425 }
426 }
427
428 /*
429 * Gets source address of the first packet in hold queue
430 * and stores it in @src.
431 * Returns pointer to @src (if hold queue is not empty) or NULL.
432 */
433 static struct in6_addr *
434 nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
435 {
436 struct ip6_hdr *hip6;
437
438 if (ln == NULL || ln->ln_hold == NULL)
439 return NULL;
440
441 /*
442 * assuming every packet in ln_hold has the same IP header
443 */
444 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
445 /* XXX pullup? */
446 if (sizeof(*hip6) < ln->ln_hold->m_len)
447 *src = hip6->ip6_src;
448 else
449 src = NULL;
450
451 return src;
452 }
453
454 static void
455 nd6_llinfo_timer(void *arg)
456 {
457 struct llentry *ln = arg;
458 struct ifnet *ifp;
459 struct nd_ifinfo *ndi = NULL;
460 bool send_ns = false;
461 const struct in6_addr *daddr6 = NULL;
462 const struct in6_addr *taddr6 = &ln->r_l3addr.addr6;
463 struct sockaddr_in6 sin6;
464
465 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
466
467 LLE_WLOCK(ln);
468 if ((ln->la_flags & LLE_LINKED) == 0)
469 goto out;
470 if (ln->ln_ntick > 0) {
471 nd6_llinfo_settimer(ln, ln->ln_ntick);
472 goto out;
473 }
474
475 ifp = ln->lle_tbl->llt_ifp;
476 KASSERT(ifp != NULL);
477
478 ndi = ND_IFINFO(ifp);
479
480 switch (ln->ln_state) {
481 case ND6_LLINFO_WAITDELETE:
482 LLE_REMREF(ln);
483 nd6_free(ln, 0);
484 ln = NULL;
485 break;
486
487 case ND6_LLINFO_INCOMPLETE:
488 if (ln->ln_asked++ < nd6_mmaxtries) {
489 send_ns = true;
490 break;
491 }
492
493 if (ln->ln_hold) {
494 struct mbuf *m = ln->ln_hold, *m0;
495
496 /*
497 * assuming every packet in ln_hold has
498 * the same IP header
499 */
500 m0 = m->m_nextpkt;
501 m->m_nextpkt = NULL;
502 ln->ln_hold = m0;
503 clear_llinfo_pqueue(ln);
504
505 icmp6_error2(m, ICMP6_DST_UNREACH,
506 ICMP6_DST_UNREACH_ADDR, 0, ifp);
507 }
508
509 sockaddr_in6_init(&sin6, taddr6, 0, 0, 0);
510 rt_clonedmsg(RTM_MISS, sin6tosa(&sin6), NULL, ifp);
511
512 /*
513 * Move to the ND6_LLINFO_WAITDELETE state for another
514 * interval at which point the llentry will be freed
515 * unless it's attempted to be used again and we'll
516 * resend NS again, rinse and repeat.
517 */
518 ln->ln_state = ND6_LLINFO_WAITDELETE;
519 if (ln->ln_asked == nd6_mmaxtries)
520 nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
521 else
522 send_ns = true;
523 break;
524
525 case ND6_LLINFO_REACHABLE:
526 if (!ND6_LLINFO_PERMANENT(ln)) {
527 ln->ln_state = ND6_LLINFO_STALE;
528 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
529 }
530 break;
531
532 case ND6_LLINFO_PURGE:
533 case ND6_LLINFO_STALE:
534 /* Garbage Collection(RFC 2461 5.3) */
535 if (!ND6_LLINFO_PERMANENT(ln)) {
536 LLE_REMREF(ln);
537 nd6_free(ln, 1);
538 ln = NULL;
539 }
540 break;
541
542 case ND6_LLINFO_DELAY:
543 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
544 /* We need NUD */
545 ln->ln_asked = 1;
546 ln->ln_state = ND6_LLINFO_PROBE;
547 daddr6 = &ln->r_l3addr.addr6;
548 send_ns = true;
549 } else {
550 ln->ln_state = ND6_LLINFO_STALE; /* XXX */
551 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
552 }
553 break;
554 case ND6_LLINFO_PROBE:
555 if (ln->ln_asked < nd6_umaxtries) {
556 ln->ln_asked++;
557 daddr6 = &ln->r_l3addr.addr6;
558 send_ns = true;
559 } else {
560 LLE_REMREF(ln);
561 nd6_free(ln, 0);
562 ln = NULL;
563 }
564 break;
565 }
566
567 if (send_ns) {
568 struct in6_addr src, *psrc;
569
570 nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
571 psrc = nd6_llinfo_get_holdsrc(ln, &src);
572 LLE_FREE_LOCKED(ln);
573 ln = NULL;
574 nd6_ns_output(ifp, daddr6, taddr6, psrc, NULL);
575 }
576
577 out:
578 if (ln != NULL)
579 LLE_FREE_LOCKED(ln);
580 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
581 }
582
583 /*
584 * ND6 timer routine to expire default route list and prefix list
585 */
586 static void
587 nd6_timer_work(struct work *wk, void *arg)
588 {
589 struct nd_defrouter *next_dr, *dr;
590 struct nd_prefix *next_pr, *pr;
591 struct in6_ifaddr *ia6, *nia6;
592 int s, bound;
593 struct psref psref;
594
595 callout_reset(&nd6_timer_ch, nd6_prune * hz,
596 nd6_timer, NULL);
597
598 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
599
600 /* expire default router list */
601
602 ND6_WLOCK();
603 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, next_dr) {
604 if (dr->expire && dr->expire < time_uptime) {
605 nd6_defrtrlist_del(dr, NULL);
606 }
607 }
608 ND6_UNLOCK();
609
610 /*
611 * expire interface addresses.
612 * in the past the loop was inside prefix expiry processing.
613 * However, from a stricter speci-confrmance standpoint, we should
614 * rather separate address lifetimes and prefix lifetimes.
615 */
616 bound = curlwp_bind();
617 addrloop:
618 s = pserialize_read_enter();
619 for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
620 nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
621
622 ia6_acquire(ia6, &psref);
623 pserialize_read_exit(s);
624
625 /* check address lifetime */
626 if (IFA6_IS_INVALID(ia6)) {
627 int regen = 0;
628 struct ifnet *ifp;
629
630 /*
631 * If the expiring address is temporary, try
632 * regenerating a new one. This would be useful when
633 * we suspended a laptop PC, then turned it on after a
634 * period that could invalidate all temporary
635 * addresses. Although we may have to restart the
636 * loop (see below), it must be after purging the
637 * address. Otherwise, we'd see an infinite loop of
638 * regeneration.
639 */
640 if (ip6_use_tempaddr &&
641 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
642 IFNET_LOCK(ia6->ia_ifa.ifa_ifp);
643 if (regen_tmpaddr(ia6) == 0)
644 regen = 1;
645 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp);
646 }
647
648 ifp = ia6->ia_ifa.ifa_ifp;
649 IFNET_LOCK(ifp);
650 /*
651 * Need to take the lock first to prevent if_detach
652 * from running in6_purgeaddr concurrently.
653 */
654 if (!if_is_deactivated(ifp)) {
655 ia6_release(ia6, &psref);
656 in6_purgeaddr(&ia6->ia_ifa);
657 } else {
658 /*
659 * ifp is being destroyed, ia6 will be destroyed
660 * by if_detach.
661 */
662 ia6_release(ia6, &psref);
663 }
664 ia6 = NULL;
665 IFNET_UNLOCK(ifp);
666
667 if (regen)
668 goto addrloop; /* XXX: see below */
669 } else if (IFA6_IS_DEPRECATED(ia6)) {
670 int oldflags = ia6->ia6_flags;
671
672 if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
673 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
674 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
675 }
676
677 /*
678 * If a temporary address has just become deprecated,
679 * regenerate a new one if possible.
680 */
681 if (ip6_use_tempaddr &&
682 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
683 (oldflags & IN6_IFF_DEPRECATED) == 0) {
684 int ret;
685
686 IFNET_LOCK(ia6->ia_ifa.ifa_ifp);
687 ret = regen_tmpaddr(ia6);
688 IFNET_UNLOCK(ia6->ia_ifa.ifa_ifp);
689 if (ret == 0) {
690 /*
691 * A new temporary address is
692 * generated.
693 * XXX: this means the address chain
694 * has changed while we are still in
695 * the loop. Although the change
696 * would not cause disaster (because
697 * it's not a deletion, but an
698 * addition,) we'd rather restart the
699 * loop just for safety. Or does this
700 * significantly reduce performance??
701 */
702 ia6_release(ia6, &psref);
703 goto addrloop;
704 }
705 }
706 } else {
707 /*
708 * A new RA might have made a deprecated address
709 * preferred.
710 */
711 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
712 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
713 rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
714 }
715 }
716 s = pserialize_read_enter();
717 ia6_release(ia6, &psref);
718 }
719 pserialize_read_exit(s);
720 curlwp_bindx(bound);
721
722 /* expire prefix list */
723 ND6_WLOCK();
724 ND_PREFIX_LIST_FOREACH_SAFE(pr, next_pr) {
725 /*
726 * check prefix lifetime.
727 * since pltime is just for autoconf, pltime processing for
728 * prefix is not necessary.
729 */
730 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
731 time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
732 /*
733 * Just invalidate the prefix here. Removing it
734 * will be done when purging an associated address.
735 */
736 KASSERTMSG(pr->ndpr_refcnt > 0, "ndpr_refcnt=%d",
737 pr->ndpr_refcnt);
738 nd6_invalidate_prefix(pr);
739 }
740 }
741 ND6_UNLOCK();
742
743 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
744 }
745
746 static void
747 nd6_timer(void *ignored_arg)
748 {
749
750 workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
751 }
752
753 /* ia6: deprecated/invalidated temporary address */
754 static int
755 regen_tmpaddr(const struct in6_ifaddr *ia6)
756 {
757 struct ifaddr *ifa;
758 struct ifnet *ifp;
759 struct in6_ifaddr *public_ifa6 = NULL;
760 int s;
761
762 ifp = ia6->ia_ifa.ifa_ifp;
763 s = pserialize_read_enter();
764 IFADDR_READER_FOREACH(ifa, ifp) {
765 struct in6_ifaddr *it6;
766
767 if (ifa->ifa_addr->sa_family != AF_INET6)
768 continue;
769
770 it6 = (struct in6_ifaddr *)ifa;
771
772 /* ignore no autoconf addresses. */
773 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
774 continue;
775
776 /* ignore autoconf addresses with different prefixes. */
777 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
778 continue;
779
780 /*
781 * Now we are looking at an autoconf address with the same
782 * prefix as ours. If the address is temporary and is still
783 * preferred, do not create another one. It would be rare, but
784 * could happen, for example, when we resume a laptop PC after
785 * a long period.
786 */
787 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
788 !IFA6_IS_DEPRECATED(it6)) {
789 public_ifa6 = NULL;
790 break;
791 }
792
793 /*
794 * This is a public autoconf address that has the same prefix
795 * as ours. If it is preferred, keep it. We can't break the
796 * loop here, because there may be a still-preferred temporary
797 * address with the prefix.
798 */
799 if (!IFA6_IS_DEPRECATED(it6))
800 public_ifa6 = it6;
801 }
802
803 if (public_ifa6 != NULL) {
804 int e;
805 struct psref psref;
806
807 ia6_acquire(public_ifa6, &psref);
808 pserialize_read_exit(s);
809 /*
810 * Random factor is introduced in the preferred lifetime, so
811 * we do not need additional delay (3rd arg to in6_tmpifadd).
812 */
813 ND6_WLOCK();
814 e = in6_tmpifadd(public_ifa6, 0, 0);
815 ND6_UNLOCK();
816 if (e != 0) {
817 ia6_release(public_ifa6, &psref);
818 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
819 " tmp addr, errno=%d\n", e);
820 return -1;
821 }
822 ia6_release(public_ifa6, &psref);
823 return 0;
824 }
825 pserialize_read_exit(s);
826
827 return -1;
828 }
829
830 bool
831 nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
832 {
833 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
834 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
835 return true;
836 case ND6_IFF_ACCEPT_RTADV:
837 return ip6_accept_rtadv != 0;
838 case ND6_IFF_OVERRIDE_RTADV:
839 case 0:
840 default:
841 return false;
842 }
843 }
844
845 /*
846 * Nuke neighbor cache/prefix/default router management table, right before
847 * ifp goes away.
848 */
849 void
850 nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
851 {
852 struct nd_defrouter *dr, *ndr;
853 struct nd_prefix *pr, *npr;
854
855 /*
856 * During detach, the ND info might be already removed, but
857 * then is explitly passed as argument.
858 * Otherwise get it from ifp->if_afdata.
859 */
860 if (ext == NULL)
861 ext = ifp->if_afdata[AF_INET6];
862 if (ext == NULL)
863 return;
864
865 ND6_WLOCK();
866 /*
867 * Nuke default router list entries toward ifp.
868 * We defer removal of default router list entries that is installed
869 * in the routing table, in order to keep additional side effects as
870 * small as possible.
871 */
872 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) {
873 if (dr->installed)
874 continue;
875
876 if (dr->ifp == ifp) {
877 KASSERT(ext != NULL);
878 nd6_defrtrlist_del(dr, ext);
879 }
880 }
881
882 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) {
883 if (!dr->installed)
884 continue;
885
886 if (dr->ifp == ifp) {
887 KASSERT(ext != NULL);
888 nd6_defrtrlist_del(dr, ext);
889 }
890 }
891
892 /* Nuke prefix list entries toward ifp */
893 ND_PREFIX_LIST_FOREACH_SAFE(pr, npr) {
894 if (pr->ndpr_ifp == ifp) {
895 /*
896 * All addresses referencing pr should be already freed.
897 */
898 KASSERTMSG(pr->ndpr_refcnt == 0, "ndpr_refcnt=%d",
899 pr->ndpr_refcnt);
900 nd6_prelist_remove(pr);
901 }
902 }
903
904 /* cancel default outgoing interface setting */
905 if (nd6_defifindex == ifp->if_index)
906 nd6_setdefaultiface(0);
907
908 /* XXX: too restrictive? */
909 if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
910 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
911 if (ndi && nd6_accepts_rtadv(ndi)) {
912 /* refresh default router list */
913 nd6_defrouter_select();
914 }
915 }
916 ND6_UNLOCK();
917
918 /*
919 * We may not need to nuke the neighbor cache entries here
920 * because the neighbor cache is kept in if_afdata[AF_INET6].
921 * nd6_purge() is invoked by in6_ifdetach() which is called
922 * from if_detach() where everything gets purged. However
923 * in6_ifdetach is directly called from vlan(4), so we still
924 * need to purge entries here.
925 */
926 if (ext->lltable != NULL)
927 lltable_purge_entries(ext->lltable);
928 }
929
930 void
931 nd6_assert_purged(struct ifnet *ifp)
932 {
933 struct nd_defrouter *dr;
934 struct nd_prefix *pr;
935 char ip6buf[INET6_ADDRSTRLEN] __diagused;
936
937 ND6_RLOCK();
938 ND_DEFROUTER_LIST_FOREACH(dr) {
939 KASSERTMSG(dr->ifp != ifp,
940 "defrouter %s remains on %s",
941 IN6_PRINT(ip6buf, &dr->rtaddr), ifp->if_xname);
942 }
943
944 ND_PREFIX_LIST_FOREACH(pr) {
945 KASSERTMSG(pr->ndpr_ifp != ifp,
946 "prefix %s/%d remains on %s",
947 IN6_PRINT(ip6buf, &pr->ndpr_prefix.sin6_addr),
948 pr->ndpr_plen, ifp->if_xname);
949 }
950 ND6_UNLOCK();
951 }
952
953 struct llentry *
954 nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
955 {
956 struct sockaddr_in6 sin6;
957 struct llentry *ln;
958
959 sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
960
961 IF_AFDATA_RLOCK(ifp);
962 ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
963 sin6tosa(&sin6));
964 IF_AFDATA_RUNLOCK(ifp);
965
966 return ln;
967 }
968
969 struct llentry *
970 nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
971 {
972 struct sockaddr_in6 sin6;
973 struct llentry *ln;
974 struct rtentry *rt;
975
976 sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
977 rt = rtalloc1(sin6tosa(&sin6), 0);
978
979 IF_AFDATA_WLOCK(ifp);
980 ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt);
981 IF_AFDATA_WUNLOCK(ifp);
982
983 if (rt != NULL)
984 rt_unref(rt);
985 if (ln != NULL)
986 ln->ln_state = ND6_LLINFO_NOSTATE;
987
988 return ln;
989 }
990
991 /*
992 * Test whether a given IPv6 address is a neighbor or not, ignoring
993 * the actual neighbor cache. The neighbor cache is ignored in order
994 * to not reenter the routing code from within itself.
995 */
996 static int
997 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
998 {
999 struct nd_prefix *pr;
1000 struct ifaddr *dstaddr;
1001 int s;
1002
1003 /*
1004 * A link-local address is always a neighbor.
1005 * XXX: a link does not necessarily specify a single interface.
1006 */
1007 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1008 struct sockaddr_in6 sin6_copy;
1009 u_int32_t zone;
1010
1011 /*
1012 * We need sin6_copy since sa6_recoverscope() may modify the
1013 * content (XXX).
1014 */
1015 sin6_copy = *addr;
1016 if (sa6_recoverscope(&sin6_copy))
1017 return 0; /* XXX: should be impossible */
1018 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1019 return 0;
1020 if (sin6_copy.sin6_scope_id == zone)
1021 return 1;
1022 else
1023 return 0;
1024 }
1025
1026 /*
1027 * If the address matches one of our addresses,
1028 * it should be a neighbor.
1029 * If the address matches one of our on-link prefixes, it should be a
1030 * neighbor.
1031 */
1032 ND6_RLOCK();
1033 ND_PREFIX_LIST_FOREACH(pr) {
1034 if (pr->ndpr_ifp != ifp)
1035 continue;
1036
1037 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1038 struct rtentry *rt;
1039
1040 rt = rtalloc1(sin6tosa(&pr->ndpr_prefix), 0);
1041 if (rt == NULL)
1042 continue;
1043 /*
1044 * This is the case where multiple interfaces
1045 * have the same prefix, but only one is installed
1046 * into the routing table and that prefix entry
1047 * is not the one being examined here. In the case
1048 * where RADIX_MPATH is enabled, multiple route
1049 * entries (of the same rt_key value) will be
1050 * installed because the interface addresses all
1051 * differ.
1052 */
1053 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1054 &satocsin6(rt_getkey(rt))->sin6_addr)) {
1055 rt_unref(rt);
1056 continue;
1057 }
1058 rt_unref(rt);
1059 }
1060
1061 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1062 &addr->sin6_addr, &pr->ndpr_mask)) {
1063 ND6_UNLOCK();
1064 return 1;
1065 }
1066 }
1067 ND6_UNLOCK();
1068
1069 /*
1070 * If the address is assigned on the node of the other side of
1071 * a p2p interface, the address should be a neighbor.
1072 */
1073 s = pserialize_read_enter();
1074 dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
1075 if (dstaddr != NULL) {
1076 if (dstaddr->ifa_ifp == ifp) {
1077 pserialize_read_exit(s);
1078 return 1;
1079 }
1080 }
1081 pserialize_read_exit(s);
1082
1083 /*
1084 * If the default router list is empty, all addresses are regarded
1085 * as on-link, and thus, as a neighbor.
1086 */
1087 ND6_RLOCK();
1088 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1089 ND_DEFROUTER_LIST_EMPTY() && nd6_defifindex == ifp->if_index) {
1090 ND6_UNLOCK();
1091 return 1;
1092 }
1093 ND6_UNLOCK();
1094
1095 return 0;
1096 }
1097
1098 /*
1099 * Detect if a given IPv6 address identifies a neighbor on a given link.
1100 * XXX: should take care of the destination of a p2p link?
1101 */
1102 int
1103 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1104 {
1105 struct nd_prefix *pr;
1106 struct llentry *ln;
1107 struct rtentry *rt;
1108
1109 /*
1110 * A link-local address is always a neighbor.
1111 * XXX: a link does not necessarily specify a single interface.
1112 */
1113 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1114 struct sockaddr_in6 sin6_copy;
1115 u_int32_t zone;
1116
1117 /*
1118 * We need sin6_copy since sa6_recoverscope() may modify the
1119 * content (XXX).
1120 */
1121 sin6_copy = *addr;
1122 if (sa6_recoverscope(&sin6_copy))
1123 return 0; /* XXX: should be impossible */
1124 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1125 return 0;
1126 if (sin6_copy.sin6_scope_id == zone)
1127 return 1;
1128 else
1129 return 0;
1130 }
1131
1132 /*
1133 * If the address matches one of our on-link prefixes, it should be a
1134 * neighbor.
1135 */
1136 ND6_RLOCK();
1137 ND_PREFIX_LIST_FOREACH(pr) {
1138 if (pr->ndpr_ifp != ifp)
1139 continue;
1140
1141 if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
1142 continue;
1143
1144 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1145 &addr->sin6_addr, &pr->ndpr_mask)) {
1146 ND6_UNLOCK();
1147 return 1;
1148 }
1149 }
1150
1151 /*
1152 * If the default router list is empty, all addresses are regarded
1153 * as on-link, and thus, as a neighbor.
1154 * XXX: we restrict the condition to hosts, because routers usually do
1155 * not have the "default router list".
1156 */
1157 if (!ip6_forwarding && ND_DEFROUTER_LIST_EMPTY() &&
1158 nd6_defifindex == ifp->if_index) {
1159 ND6_UNLOCK();
1160 return 1;
1161 }
1162 ND6_UNLOCK();
1163
1164 if (nd6_is_new_addr_neighbor(addr, ifp))
1165 return 1;
1166
1167 /*
1168 * Even if the address matches none of our addresses, it might be
1169 * in the neighbor cache or a connected route.
1170 */
1171 ln = nd6_lookup(&addr->sin6_addr, ifp, false);
1172 if (ln != NULL) {
1173 LLE_RUNLOCK(ln);
1174 return 1;
1175 }
1176
1177 rt = rtalloc1(sin6tocsa(addr), 0);
1178 if (rt == NULL)
1179 return 0;
1180
1181 if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
1182 #if NBRIDGE > 0
1183 || rt->rt_ifp->if_bridge == ifp->if_bridge
1184 #endif
1185 #if NCARP > 0
1186 || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
1187 (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
1188 (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
1189 rt->rt_ifp->if_carpdev == ifp->if_carpdev)
1190 #endif
1191 )) {
1192 rt_unref(rt);
1193 return 1;
1194 }
1195 rt_unref(rt);
1196
1197 return 0;
1198 }
1199
1200 /*
1201 * Free an nd6 llinfo entry.
1202 * Since the function would cause significant changes in the kernel, DO NOT
1203 * make it global, unless you have a strong reason for the change, and are sure
1204 * that the change is safe.
1205 */
1206 static void
1207 nd6_free(struct llentry *ln, int gc)
1208 {
1209 struct ifnet *ifp;
1210 struct in6_addr *in6;
1211
1212 KASSERT(ln != NULL);
1213 LLE_WLOCK_ASSERT(ln);
1214
1215 ifp = ln->lle_tbl->llt_ifp;
1216 in6 = &ln->r_l3addr.addr6;
1217 /*
1218 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1219 * even though it is not harmful, it was not really necessary.
1220 */
1221
1222 if (!ip6_forwarding && ln->ln_router) {
1223 if (ln->ln_state == ND6_LLINFO_STALE && gc) {
1224 /*
1225 * If the reason for the deletion is just garbage
1226 * collection, and the neighbor is an active
1227 * router, do not delete it. Instead, reset the GC
1228 * timer using the router's lifetime.
1229 * Simply deleting the entry may affect default
1230 * router selection, which is not necessarily a good
1231 * thing, especially when we're using router preference
1232 * values.
1233 * XXX: the check for ln_state would be redundant,
1234 * but we intentionally keep it just in case.
1235 */
1236 if (ln->ln_expire > time_uptime)
1237 nd6_llinfo_settimer(ln,
1238 (ln->ln_expire - time_uptime) * hz);
1239 else
1240 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
1241 LLE_WUNLOCK(ln);
1242 return;
1243 }
1244
1245 ND6_WLOCK();
1246
1247 /*
1248 * We need to unlock to avoid a LOR with nd6_rt_flush()
1249 * with the rnh and for the calls to
1250 * nd6_pfxlist_onlink_check() and nd6_defrouter_select() in the
1251 * block further down for calls into nd6_lookup().
1252 * We still hold a ref.
1253 *
1254 * Temporarily fake the state to choose a new default
1255 * router and to perform on-link determination of
1256 * prefixes correctly.
1257 * Below the state will be set correctly,
1258 * or the entry itself will be deleted.
1259 */
1260 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1261 LLE_WUNLOCK(ln);
1262
1263 /*
1264 * nd6_rt_flush must be called whether or not the neighbor
1265 * is in the Default Router List.
1266 * See a corresponding comment in nd6_na_input().
1267 */
1268 nd6_rt_flush(in6, ifp);
1269
1270 /*
1271 * Unreachablity of a router might affect the default
1272 * router selection and on-link detection of advertised
1273 * prefixes.
1274 *
1275 * Since nd6_defrouter_select() does not affect the
1276 * on-link determination and MIP6 needs the check
1277 * before the default router selection, we perform
1278 * the check now.
1279 */
1280 nd6_pfxlist_onlink_check();
1281
1282 /*
1283 * refresh default router list
1284 */
1285 nd6_defrouter_select();
1286
1287 #ifdef __FreeBSD__
1288 /*
1289 * If this entry was added by an on-link redirect, remove the
1290 * corresponding host route.
1291 */
1292 if (ln->la_flags & LLE_REDIRECT)
1293 nd6_free_redirect(ln);
1294 #endif
1295
1296 ND6_UNLOCK();
1297 LLE_WLOCK(ln);
1298 }
1299
1300 if (ln->la_flags & LLE_VALID || gc) {
1301 struct sockaddr_in6 sin6;
1302 const char *lladdr;
1303
1304 sockaddr_in6_init(&sin6, in6, 0, 0, 0);
1305 lladdr = ln->la_flags & LLE_VALID ?
1306 (const char *)&ln->ll_addr : NULL;
1307 rt_clonedmsg(RTM_DELETE, sin6tosa(&sin6), lladdr, ifp);
1308 }
1309
1310 /*
1311 * Save to unlock. We still hold an extra reference and will not
1312 * free(9) in llentry_free() if someone else holds one as well.
1313 */
1314 LLE_WUNLOCK(ln);
1315 IF_AFDATA_LOCK(ifp);
1316 LLE_WLOCK(ln);
1317
1318 lltable_free_entry(LLTABLE6(ifp), ln);
1319
1320 IF_AFDATA_UNLOCK(ifp);
1321 }
1322
1323 /*
1324 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1325 *
1326 * XXX cost-effective methods?
1327 */
1328 void
1329 nd6_nud_hint(struct rtentry *rt)
1330 {
1331 struct llentry *ln;
1332 struct ifnet *ifp;
1333
1334 if (rt == NULL)
1335 return;
1336
1337 ifp = rt->rt_ifp;
1338 ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
1339 if (ln == NULL)
1340 return;
1341
1342 if (ln->ln_state < ND6_LLINFO_REACHABLE)
1343 goto done;
1344
1345 /*
1346 * if we get upper-layer reachability confirmation many times,
1347 * it is possible we have false information.
1348 */
1349 ln->ln_byhint++;
1350 if (ln->ln_byhint > nd6_maxnudhint)
1351 goto done;
1352
1353 ln->ln_state = ND6_LLINFO_REACHABLE;
1354 if (!ND6_LLINFO_PERMANENT(ln))
1355 nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz);
1356
1357 done:
1358 LLE_WUNLOCK(ln);
1359
1360 return;
1361 }
1362
1363 struct gc_args {
1364 int gc_entries;
1365 const struct in6_addr *skip_in6;
1366 };
1367
1368 static int
1369 nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
1370 {
1371 struct gc_args *args = farg;
1372 int *n = &args->gc_entries;
1373 const struct in6_addr *skip_in6 = args->skip_in6;
1374
1375 if (*n <= 0)
1376 return 0;
1377
1378 if (ND6_LLINFO_PERMANENT(ln))
1379 return 0;
1380
1381 if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6))
1382 return 0;
1383
1384 LLE_WLOCK(ln);
1385 if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1386 ln->ln_state = ND6_LLINFO_STALE;
1387 else
1388 ln->ln_state = ND6_LLINFO_PURGE;
1389 nd6_llinfo_settimer(ln, 0);
1390 LLE_WUNLOCK(ln);
1391
1392 (*n)--;
1393 return 0;
1394 }
1395
1396 static void
1397 nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6)
1398 {
1399
1400 if (ip6_neighborgcthresh >= 0 &&
1401 lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
1402 struct gc_args gc_args = {10, in6};
1403 /*
1404 * XXX entries that are "less recently used" should be
1405 * freed first.
1406 */
1407 lltable_foreach_lle(llt, nd6_purge_entry, &gc_args);
1408 }
1409 }
1410
1411 void
1412 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
1413 {
1414 struct sockaddr *gate = rt->rt_gateway;
1415 struct ifnet *ifp = rt->rt_ifp;
1416 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
1417 struct ifaddr *ifa;
1418
1419 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1420
1421 if (req == RTM_LLINFO_UPD) {
1422 int rc;
1423 struct in6_addr *in6;
1424 struct in6_addr in6_all;
1425 int anycast;
1426
1427 if ((ifa = info->rti_ifa) == NULL)
1428 return;
1429
1430 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1431 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
1432
1433 in6_all = in6addr_linklocal_allnodes;
1434 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
1435 log(LOG_ERR, "%s: failed to set scope %s "
1436 "(errno=%d)\n", __func__, if_name(ifp), rc);
1437 return;
1438 }
1439
1440 /* XXX don't set Override for proxy addresses */
1441 nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
1442 (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
1443 #if 0
1444 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
1445 #endif
1446 , 1, NULL);
1447 return;
1448 }
1449
1450 if ((rt->rt_flags & RTF_GATEWAY) != 0)
1451 return;
1452
1453 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1454 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1455 /*
1456 * This is probably an interface direct route for a link
1457 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1458 * We do not need special treatment below for such a route.
1459 * Moreover, the RTF_LLINFO flag which would be set below
1460 * would annoy the ndp(8) command.
1461 */
1462 return;
1463 }
1464
1465 switch (req) {
1466 case RTM_ADD: {
1467 struct psref psref;
1468
1469 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1470 /*
1471 * There is no backward compatibility :)
1472 *
1473 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1474 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1475 * rt->rt_flags |= RTF_CLONING;
1476 */
1477 /* XXX should move to route.c? */
1478 if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
1479 union {
1480 struct sockaddr sa;
1481 struct sockaddr_dl sdl;
1482 struct sockaddr_storage ss;
1483 } u;
1484 /*
1485 * Case 1: This route should come from a route to
1486 * interface (RTF_CLONING case) or the route should be
1487 * treated as on-link but is currently not
1488 * (RTF_LLINFO && ln == NULL case).
1489 */
1490 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
1491 ifp->if_index, ifp->if_type,
1492 NULL, namelen, NULL, addrlen) == NULL) {
1493 printf("%s.%d: sockaddr_dl_init(, %zu, ) "
1494 "failed on %s\n", __func__, __LINE__,
1495 sizeof(u.ss), if_name(ifp));
1496 }
1497 rt_setgate(rt, &u.sa);
1498 gate = rt->rt_gateway;
1499 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1500 if (gate == NULL) {
1501 log(LOG_ERR,
1502 "%s: rt_setgate failed on %s\n", __func__,
1503 if_name(ifp));
1504 break;
1505 }
1506
1507 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1508 if ((rt->rt_flags & RTF_CONNECTED) != 0)
1509 break;
1510 }
1511 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1512 /*
1513 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1514 * We don't do that here since llinfo is not ready yet.
1515 *
1516 * There are also couple of other things to be discussed:
1517 * - unsolicited NA code needs improvement beforehand
1518 * - RFC2461 says we MAY send multicast unsolicited NA
1519 * (7.2.6 paragraph 4), however, it also says that we
1520 * SHOULD provide a mechanism to prevent multicast NA storm.
1521 * we don't have anything like it right now.
1522 * note that the mechanism needs a mutual agreement
1523 * between proxies, which means that we need to implement
1524 * a new protocol, or a new kludge.
1525 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1526 * we need to check ip6forwarding before sending it.
1527 * (or should we allow proxy ND configuration only for
1528 * routers? there's no mention about proxy ND from hosts)
1529 */
1530 #if 0
1531 /* XXX it does not work */
1532 if (rt->rt_flags & RTF_ANNOUNCE)
1533 nd6_na_output(ifp,
1534 &satocsin6(rt_getkey(rt))->sin6_addr,
1535 &satocsin6(rt_getkey(rt))->sin6_addr,
1536 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1537 1, NULL);
1538 #endif
1539
1540 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1541 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1542 /*
1543 * Address resolution isn't necessary for a point to
1544 * point link, so we can skip this test for a p2p link.
1545 */
1546 if (gate->sa_family != AF_LINK ||
1547 gate->sa_len <
1548 sockaddr_dl_measure(namelen, addrlen)) {
1549 log(LOG_DEBUG,
1550 "nd6_rtrequest: bad gateway value: %s\n",
1551 if_name(ifp));
1552 break;
1553 }
1554 satosdl(gate)->sdl_type = ifp->if_type;
1555 satosdl(gate)->sdl_index = ifp->if_index;
1556 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1557 }
1558 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
1559
1560 /*
1561 * When called from rt_ifa_addlocal, we cannot depend on that
1562 * the address (rt_getkey(rt)) exits in the address list of the
1563 * interface. So check RTF_LOCAL instead.
1564 */
1565 if (rt->rt_flags & RTF_LOCAL) {
1566 if (nd6_useloopback)
1567 rt->rt_ifp = lo0ifp; /* XXX */
1568 break;
1569 }
1570
1571 /*
1572 * check if rt_getkey(rt) is an address assigned
1573 * to the interface.
1574 */
1575 ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp,
1576 &satocsin6(rt_getkey(rt))->sin6_addr, &psref);
1577 if (ifa != NULL) {
1578 if (nd6_useloopback) {
1579 rt->rt_ifp = lo0ifp; /* XXX */
1580 /*
1581 * Make sure rt_ifa be equal to the ifaddr
1582 * corresponding to the address.
1583 * We need this because when we refer
1584 * rt_ifa->ia6_flags in ip6_input, we assume
1585 * that the rt_ifa points to the address instead
1586 * of the loopback address.
1587 */
1588 if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA)
1589 && ifa != rt->rt_ifa)
1590 rt_replace_ifa(rt, ifa);
1591 }
1592 } else if (rt->rt_flags & RTF_ANNOUNCE) {
1593 /* join solicited node multicast for proxy ND */
1594 if (ifp->if_flags & IFF_MULTICAST) {
1595 struct in6_addr llsol;
1596 int error;
1597
1598 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1599 llsol.s6_addr32[0] = htonl(0xff020000);
1600 llsol.s6_addr32[1] = 0;
1601 llsol.s6_addr32[2] = htonl(1);
1602 llsol.s6_addr8[12] = 0xff;
1603 if (in6_setscope(&llsol, ifp, NULL))
1604 goto out;
1605 if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1606 char ip6buf[INET6_ADDRSTRLEN];
1607 nd6log(LOG_ERR, "%s: failed to join "
1608 "%s (errno=%d)\n", if_name(ifp),
1609 IN6_PRINT(ip6buf, &llsol), error);
1610 }
1611 }
1612 }
1613 out:
1614 ifa_release(ifa, &psref);
1615 /*
1616 * If we have too many cache entries, initiate immediate
1617 * purging for some entries.
1618 */
1619 if (rt->rt_ifp != NULL)
1620 nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL);
1621 break;
1622 }
1623
1624 case RTM_DELETE:
1625 /* leave from solicited node multicast for proxy ND */
1626 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1627 (ifp->if_flags & IFF_MULTICAST) != 0) {
1628 struct in6_addr llsol;
1629
1630 llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1631 llsol.s6_addr32[0] = htonl(0xff020000);
1632 llsol.s6_addr32[1] = 0;
1633 llsol.s6_addr32[2] = htonl(1);
1634 llsol.s6_addr8[12] = 0xff;
1635 if (in6_setscope(&llsol, ifp, NULL) == 0)
1636 in6_lookup_and_delete_multi(&llsol, ifp);
1637 }
1638 break;
1639 }
1640 }
1641
1642 int
1643 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1644 {
1645 struct in6_drlist *drl = (struct in6_drlist *)data;
1646 struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1647 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1648 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1649 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1650 struct nd_defrouter *dr;
1651 struct nd_prefix *pr;
1652 int i = 0, error = 0;
1653
1654 switch (cmd) {
1655 case SIOCGDRLST_IN6:
1656 /*
1657 * obsolete API, use sysctl under net.inet6.icmp6
1658 */
1659 memset(drl, 0, sizeof(*drl));
1660 ND6_RLOCK();
1661 ND_DEFROUTER_LIST_FOREACH(dr) {
1662 if (i >= DRLSTSIZ)
1663 break;
1664 drl->defrouter[i].rtaddr = dr->rtaddr;
1665 in6_clearscope(&drl->defrouter[i].rtaddr);
1666
1667 drl->defrouter[i].flags = dr->flags;
1668 drl->defrouter[i].rtlifetime = dr->rtlifetime;
1669 drl->defrouter[i].expire = dr->expire ?
1670 time_mono_to_wall(dr->expire) : 0;
1671 drl->defrouter[i].if_index = dr->ifp->if_index;
1672 i++;
1673 }
1674 ND6_UNLOCK();
1675 break;
1676 case SIOCGPRLST_IN6:
1677 /*
1678 * obsolete API, use sysctl under net.inet6.icmp6
1679 *
1680 * XXX the structure in6_prlist was changed in backward-
1681 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6,
1682 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1683 */
1684 /*
1685 * XXX meaning of fields, especialy "raflags", is very
1686 * differnet between RA prefix list and RR/static prefix list.
1687 * how about separating ioctls into two?
1688 */
1689 memset(oprl, 0, sizeof(*oprl));
1690 ND6_RLOCK();
1691 ND_PREFIX_LIST_FOREACH(pr) {
1692 struct nd_pfxrouter *pfr;
1693 int j;
1694
1695 if (i >= PRLSTSIZ)
1696 break;
1697 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1698 oprl->prefix[i].raflags = pr->ndpr_raf;
1699 oprl->prefix[i].prefixlen = pr->ndpr_plen;
1700 oprl->prefix[i].vltime = pr->ndpr_vltime;
1701 oprl->prefix[i].pltime = pr->ndpr_pltime;
1702 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1703 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1704 oprl->prefix[i].expire = 0;
1705 else {
1706 time_t maxexpire;
1707
1708 /* XXX: we assume time_t is signed. */
1709 maxexpire = (-1) &
1710 ~((time_t)1 <<
1711 ((sizeof(maxexpire) * 8) - 1));
1712 if (pr->ndpr_vltime <
1713 maxexpire - pr->ndpr_lastupdate) {
1714 time_t expire;
1715 expire = pr->ndpr_lastupdate +
1716 pr->ndpr_vltime;
1717 oprl->prefix[i].expire = expire ?
1718 time_mono_to_wall(expire) : 0;
1719 } else
1720 oprl->prefix[i].expire = maxexpire;
1721 }
1722
1723 j = 0;
1724 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1725 if (j < DRLSTSIZ) {
1726 #define RTRADDR oprl->prefix[i].advrtr[j]
1727 RTRADDR = pfr->router->rtaddr;
1728 in6_clearscope(&RTRADDR);
1729 #undef RTRADDR
1730 }
1731 j++;
1732 }
1733 oprl->prefix[i].advrtrs = j;
1734 oprl->prefix[i].origin = PR_ORIG_RA;
1735
1736 i++;
1737 }
1738 ND6_UNLOCK();
1739
1740 break;
1741 case OSIOCGIFINFO_IN6:
1742 #define ND ndi->ndi
1743 /* XXX: old ndp(8) assumes a positive value for linkmtu. */
1744 memset(&ND, 0, sizeof(ND));
1745 ND.linkmtu = IN6_LINKMTU(ifp);
1746 ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1747 ND.basereachable = ND_IFINFO(ifp)->basereachable;
1748 ND.reachable = ND_IFINFO(ifp)->reachable;
1749 ND.retrans = ND_IFINFO(ifp)->retrans;
1750 ND.flags = ND_IFINFO(ifp)->flags;
1751 ND.recalctm = ND_IFINFO(ifp)->recalctm;
1752 ND.chlim = ND_IFINFO(ifp)->chlim;
1753 break;
1754 case SIOCGIFINFO_IN6:
1755 ND = *ND_IFINFO(ifp);
1756 break;
1757 case SIOCSIFINFO_IN6:
1758 /*
1759 * used to change host variables from userland.
1760 * intented for a use on router to reflect RA configurations.
1761 */
1762 /* 0 means 'unspecified' */
1763 if (ND.linkmtu != 0) {
1764 if (ND.linkmtu < IPV6_MMTU ||
1765 ND.linkmtu > IN6_LINKMTU(ifp)) {
1766 error = EINVAL;
1767 break;
1768 }
1769 ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1770 }
1771
1772 if (ND.basereachable != 0) {
1773 int obasereachable = ND_IFINFO(ifp)->basereachable;
1774
1775 ND_IFINFO(ifp)->basereachable = ND.basereachable;
1776 if (ND.basereachable != obasereachable)
1777 ND_IFINFO(ifp)->reachable =
1778 ND_COMPUTE_RTIME(ND.basereachable);
1779 }
1780 if (ND.retrans != 0)
1781 ND_IFINFO(ifp)->retrans = ND.retrans;
1782 if (ND.chlim != 0)
1783 ND_IFINFO(ifp)->chlim = ND.chlim;
1784 /* FALLTHROUGH */
1785 case SIOCSIFINFO_FLAGS:
1786 {
1787 struct ifaddr *ifa;
1788 struct in6_ifaddr *ia;
1789 int s;
1790
1791 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1792 !(ND.flags & ND6_IFF_IFDISABLED))
1793 {
1794 /*
1795 * If the interface is marked as ND6_IFF_IFDISABLED and
1796 * has a link-local address with IN6_IFF_DUPLICATED,
1797 * do not clear ND6_IFF_IFDISABLED.
1798 * See RFC 4862, section 5.4.5.
1799 */
1800 int duplicated_linklocal = 0;
1801
1802 s = pserialize_read_enter();
1803 IFADDR_READER_FOREACH(ifa, ifp) {
1804 if (ifa->ifa_addr->sa_family != AF_INET6)
1805 continue;
1806 ia = (struct in6_ifaddr *)ifa;
1807 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1808 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1809 {
1810 duplicated_linklocal = 1;
1811 break;
1812 }
1813 }
1814 pserialize_read_exit(s);
1815
1816 if (duplicated_linklocal) {
1817 ND.flags |= ND6_IFF_IFDISABLED;
1818 log(LOG_ERR, "%s: Cannot enable an interface"
1819 " with a link-local address marked"
1820 " duplicate.\n", if_name(ifp));
1821 } else {
1822 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1823 if (ifp->if_flags & IFF_UP)
1824 in6_if_up(ifp);
1825 }
1826 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1827 (ND.flags & ND6_IFF_IFDISABLED)) {
1828 int bound = curlwp_bind();
1829 /* Mark all IPv6 addresses as tentative. */
1830
1831 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1832 s = pserialize_read_enter();
1833 IFADDR_READER_FOREACH(ifa, ifp) {
1834 struct psref psref;
1835 if (ifa->ifa_addr->sa_family != AF_INET6)
1836 continue;
1837 ifa_acquire(ifa, &psref);
1838 pserialize_read_exit(s);
1839
1840 nd6_dad_stop(ifa);
1841
1842 ia = (struct in6_ifaddr *)ifa;
1843 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1844
1845 s = pserialize_read_enter();
1846 ifa_release(ifa, &psref);
1847 }
1848 pserialize_read_exit(s);
1849 curlwp_bindx(bound);
1850 }
1851
1852 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1853 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1854 /* auto_linklocal 0->1 transition */
1855
1856 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1857 in6_ifattach(ifp, NULL);
1858 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1859 ifp->if_flags & IFF_UP)
1860 {
1861 /*
1862 * When the IF already has
1863 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1864 * address is assigned, and IFF_UP, try to
1865 * assign one.
1866 */
1867 int haslinklocal = 0;
1868
1869 s = pserialize_read_enter();
1870 IFADDR_READER_FOREACH(ifa, ifp) {
1871 if (ifa->ifa_addr->sa_family !=AF_INET6)
1872 continue;
1873 ia = (struct in6_ifaddr *)ifa;
1874 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
1875 haslinklocal = 1;
1876 break;
1877 }
1878 }
1879 pserialize_read_exit(s);
1880 if (!haslinklocal)
1881 in6_ifattach(ifp, NULL);
1882 }
1883 }
1884 }
1885 ND_IFINFO(ifp)->flags = ND.flags;
1886 break;
1887 #undef ND
1888 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1889 /* sync kernel routing table with the default router list */
1890 ND6_WLOCK();
1891 nd6_defrouter_reset();
1892 nd6_defrouter_select();
1893 ND6_UNLOCK();
1894 break;
1895 case SIOCSPFXFLUSH_IN6:
1896 {
1897 /* flush all the prefix advertised by routers */
1898 struct nd_prefix *pfx, *next;
1899
1900 restart:
1901 ND6_WLOCK();
1902 ND_PREFIX_LIST_FOREACH_SAFE(pfx, next) {
1903 struct in6_ifaddr *ia, *ia_next;
1904 int _s;
1905
1906 /* Only flush prefixes for the given interface. */
1907 if (ifp != lo0ifp && ifp != pfx->ndpr_ifp)
1908 continue;
1909
1910 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
1911 continue; /* XXX */
1912
1913 /* do we really have to remove addresses as well? */
1914 _s = pserialize_read_enter();
1915 for (ia = IN6_ADDRLIST_READER_FIRST(); ia;
1916 ia = ia_next) {
1917 struct ifnet *ifa_ifp;
1918 int bound;
1919 struct psref psref;
1920
1921 /* ia might be removed. keep the next ptr. */
1922 ia_next = IN6_ADDRLIST_READER_NEXT(ia);
1923
1924 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1925 continue;
1926
1927 if (ia->ia6_ndpr != pfx)
1928 continue;
1929
1930 bound = curlwp_bind();
1931 ia6_acquire(ia, &psref);
1932 pserialize_read_exit(_s);
1933 ND6_UNLOCK();
1934
1935 ifa_ifp = ia->ia_ifa.ifa_ifp;
1936 if (ifa_ifp == ifp) {
1937 /* Already have IFNET_LOCK(ifp) */
1938 KASSERT(!if_is_deactivated(ifp));
1939 ia6_release(ia, &psref);
1940 in6_purgeaddr(&ia->ia_ifa);
1941 curlwp_bindx(bound);
1942 goto restart;
1943 }
1944 IFNET_LOCK(ifa_ifp);
1945 /*
1946 * Need to take the lock first to prevent
1947 * if_detach from running in6_purgeaddr
1948 * concurrently.
1949 */
1950 if (!if_is_deactivated(ifa_ifp)) {
1951 ia6_release(ia, &psref);
1952 in6_purgeaddr(&ia->ia_ifa);
1953 } else {
1954 /*
1955 * ifp is being destroyed, ia will be
1956 * destroyed by if_detach.
1957 */
1958 ia6_release(ia, &psref);
1959 /* XXX may cause busy loop */
1960 }
1961 IFNET_UNLOCK(ifa_ifp);
1962 curlwp_bindx(bound);
1963 goto restart;
1964 }
1965 pserialize_read_exit(_s);
1966
1967 KASSERTMSG(pfx->ndpr_refcnt == 0, "ndpr_refcnt=%d",
1968 pfx->ndpr_refcnt);
1969 nd6_prelist_remove(pfx);
1970 }
1971 ND6_UNLOCK();
1972 break;
1973 }
1974 case SIOCSRTRFLUSH_IN6:
1975 {
1976 /* flush all the default routers */
1977 struct nd_defrouter *drtr, *next;
1978
1979 ND6_WLOCK();
1980 #if 0
1981 /* XXX Is this really needed? */
1982 nd6_defrouter_reset();
1983 #endif
1984 ND_DEFROUTER_LIST_FOREACH_SAFE(drtr, next) {
1985 /* Only flush routers for the given interface. */
1986 if (ifp != lo0ifp && ifp != drtr->ifp)
1987 continue;
1988
1989 nd6_defrtrlist_del(drtr, NULL);
1990 }
1991 nd6_defrouter_select();
1992 ND6_UNLOCK();
1993 break;
1994 }
1995 case SIOCGNBRINFO_IN6:
1996 {
1997 struct llentry *ln;
1998 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1999
2000 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
2001 return error;
2002
2003 ln = nd6_lookup(&nb_addr, ifp, false);
2004 if (ln == NULL) {
2005 error = EINVAL;
2006 break;
2007 }
2008 nbi->state = ln->ln_state;
2009 nbi->asked = ln->ln_asked;
2010 nbi->isrouter = ln->ln_router;
2011 nbi->expire = ln->ln_expire ?
2012 time_mono_to_wall(ln->ln_expire) : 0;
2013 LLE_RUNLOCK(ln);
2014
2015 break;
2016 }
2017 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
2018 ndif->ifindex = nd6_defifindex;
2019 break;
2020 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
2021 return nd6_setdefaultiface(ndif->ifindex);
2022 }
2023 return error;
2024 }
2025
2026 void
2027 nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
2028 {
2029 struct mbuf *m_hold, *m_hold_next;
2030 struct sockaddr_in6 sin6;
2031
2032 LLE_WLOCK_ASSERT(ln);
2033
2034 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
2035
2036 m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
2037
2038 LLE_WUNLOCK(ln);
2039 for (; m_hold != NULL; m_hold = m_hold_next) {
2040 m_hold_next = m_hold->m_nextpkt;
2041 m_hold->m_nextpkt = NULL;
2042
2043 /*
2044 * we assume ifp is not a p2p here, so
2045 * just set the 2nd argument as the
2046 * 1st one.
2047 */
2048 ip6_if_output(ifp, ifp, m_hold, &sin6, NULL);
2049 }
2050 LLE_WLOCK(ln);
2051 }
2052
2053 /*
2054 * Create neighbor cache entry and cache link-layer address,
2055 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
2056 */
2057 void
2058 nd6_cache_lladdr(
2059 struct ifnet *ifp,
2060 struct in6_addr *from,
2061 char *lladdr,
2062 int lladdrlen,
2063 int type, /* ICMP6 type */
2064 int code /* type dependent information */
2065 )
2066 {
2067 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
2068 struct llentry *ln = NULL;
2069 int is_newentry;
2070 int do_update;
2071 int olladdr;
2072 int llchange;
2073 int newstate = 0;
2074 uint16_t router = 0;
2075
2076 KASSERT(ifp != NULL);
2077 KASSERT(from != NULL);
2078
2079 /* nothing must be updated for unspecified address */
2080 if (IN6_IS_ADDR_UNSPECIFIED(from))
2081 return;
2082
2083 /*
2084 * Validation about ifp->if_addrlen and lladdrlen must be done in
2085 * the caller.
2086 *
2087 * XXX If the link does not have link-layer adderss, what should
2088 * we do? (ifp->if_addrlen == 0)
2089 * Spec says nothing in sections for RA, RS and NA. There's small
2090 * description on it in NS section (RFC 2461 7.2.3).
2091 */
2092
2093 ln = nd6_lookup(from, ifp, true);
2094 if (ln == NULL) {
2095 #if 0
2096 /* nothing must be done if there's no lladdr */
2097 if (!lladdr || !lladdrlen)
2098 return NULL;
2099 #endif
2100
2101 ln = nd6_create(from, ifp);
2102 is_newentry = 1;
2103 } else {
2104 /* do nothing if static ndp is set */
2105 if (ln->la_flags & LLE_STATIC) {
2106 LLE_WUNLOCK(ln);
2107 return;
2108 }
2109 is_newentry = 0;
2110 }
2111
2112 if (ln == NULL)
2113 return;
2114
2115 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
2116 if (olladdr && lladdr) {
2117 llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
2118 } else
2119 llchange = 0;
2120
2121 /*
2122 * newentry olladdr lladdr llchange (*=record)
2123 * 0 n n -- (1)
2124 * 0 y n -- (2)
2125 * 0 n y -- (3) * STALE
2126 * 0 y y n (4) *
2127 * 0 y y y (5) * STALE
2128 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2129 * 1 -- y -- (7) * STALE
2130 */
2131
2132 if (lladdr) { /* (3-5) and (7) */
2133 /*
2134 * Record source link-layer address
2135 * XXX is it dependent to ifp->if_type?
2136 */
2137 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2138 ln->la_flags |= LLE_VALID;
2139 }
2140
2141 if (!is_newentry) {
2142 if ((!olladdr && lladdr) || /* (3) */
2143 (olladdr && lladdr && llchange)) { /* (5) */
2144 do_update = 1;
2145 newstate = ND6_LLINFO_STALE;
2146 } else /* (1-2,4) */
2147 do_update = 0;
2148 } else {
2149 do_update = 1;
2150 if (lladdr == NULL) /* (6) */
2151 newstate = ND6_LLINFO_NOSTATE;
2152 else /* (7) */
2153 newstate = ND6_LLINFO_STALE;
2154 }
2155
2156 if (do_update) {
2157 /*
2158 * Update the state of the neighbor cache.
2159 */
2160 ln->ln_state = newstate;
2161
2162 if (ln->ln_state == ND6_LLINFO_STALE) {
2163 /*
2164 * XXX: since nd6_output() below will cause
2165 * state tansition to DELAY and reset the timer,
2166 * we must set the timer now, although it is actually
2167 * meaningless.
2168 */
2169 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
2170
2171 nd6_llinfo_release_pkts(ln, ifp);
2172 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
2173 /* probe right away */
2174 nd6_llinfo_settimer((void *)ln, 0);
2175 }
2176 }
2177
2178 /*
2179 * ICMP6 type dependent behavior.
2180 *
2181 * NS: clear IsRouter if new entry
2182 * RS: clear IsRouter
2183 * RA: set IsRouter if there's lladdr
2184 * redir: clear IsRouter if new entry
2185 *
2186 * RA case, (1):
2187 * The spec says that we must set IsRouter in the following cases:
2188 * - If lladdr exist, set IsRouter. This means (1-5).
2189 * - If it is old entry (!newentry), set IsRouter. This means (7).
2190 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
2191 * A quetion arises for (1) case. (1) case has no lladdr in the
2192 * neighbor cache, this is similar to (6).
2193 * This case is rare but we figured that we MUST NOT set IsRouter.
2194 *
2195 * newentry olladdr lladdr llchange NS RS RA redir
2196 * D R
2197 * 0 n n -- (1) c ? s
2198 * 0 y n -- (2) c s s
2199 * 0 n y -- (3) c s s
2200 * 0 y y n (4) c s s
2201 * 0 y y y (5) c s s
2202 * 1 -- n -- (6) c c c s
2203 * 1 -- y -- (7) c c s c s
2204 *
2205 * (c=clear s=set)
2206 */
2207 switch (type & 0xff) {
2208 case ND_NEIGHBOR_SOLICIT:
2209 /*
2210 * New entry must have is_router flag cleared.
2211 */
2212 if (is_newentry) /* (6-7) */
2213 ln->ln_router = 0;
2214 break;
2215 case ND_REDIRECT:
2216 /*
2217 * If the icmp is a redirect to a better router, always set the
2218 * is_router flag. Otherwise, if the entry is newly created,
2219 * clear the flag. [RFC 2461, sec 8.3]
2220 */
2221 if (code == ND_REDIRECT_ROUTER)
2222 ln->ln_router = 1;
2223 else if (is_newentry) /* (6-7) */
2224 ln->ln_router = 0;
2225 break;
2226 case ND_ROUTER_SOLICIT:
2227 /*
2228 * is_router flag must always be cleared.
2229 */
2230 ln->ln_router = 0;
2231 break;
2232 case ND_ROUTER_ADVERT:
2233 /*
2234 * Mark an entry with lladdr as a router.
2235 */
2236 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
2237 (is_newentry && lladdr)) { /* (7) */
2238 ln->ln_router = 1;
2239 }
2240 break;
2241 }
2242
2243 if (do_update && lladdr != NULL) {
2244 struct sockaddr_in6 sin6;
2245
2246 sockaddr_in6_init(&sin6, from, 0, 0, 0);
2247 rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE,
2248 sin6tosa(&sin6), lladdr, ifp);
2249 }
2250
2251 if (ln != NULL) {
2252 router = ln->ln_router;
2253 LLE_WUNLOCK(ln);
2254 }
2255
2256 /*
2257 * If we have too many cache entries, initiate immediate
2258 * purging for some entries.
2259 */
2260 if (is_newentry)
2261 nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6);
2262
2263 /*
2264 * When the link-layer address of a router changes, select the
2265 * best router again. In particular, when the neighbor entry is newly
2266 * created, it might affect the selection policy.
2267 * Question: can we restrict the first condition to the "is_newentry"
2268 * case?
2269 * XXX: when we hear an RA from a new router with the link-layer
2270 * address option, nd6_defrouter_select() is called twice, since
2271 * defrtrlist_update called the function as well. However, I believe
2272 * we can compromise the overhead, since it only happens the first
2273 * time.
2274 * XXX: although nd6_defrouter_select() should not have a bad effect
2275 * for those are not autoconfigured hosts, we explicitly avoid such
2276 * cases for safety.
2277 */
2278 if (do_update && router && !ip6_forwarding &&
2279 nd6_accepts_rtadv(ndi)) {
2280 ND6_WLOCK();
2281 nd6_defrouter_select();
2282 ND6_UNLOCK();
2283 }
2284 }
2285
2286 static void
2287 nd6_slowtimo(void *ignored_arg)
2288 {
2289 struct nd_ifinfo *nd6if;
2290 struct ifnet *ifp;
2291 int s;
2292
2293 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
2294 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2295 nd6_slowtimo, NULL);
2296
2297 s = pserialize_read_enter();
2298 IFNET_READER_FOREACH(ifp) {
2299 nd6if = ND_IFINFO(ifp);
2300 if (nd6if->basereachable && /* already initialized */
2301 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2302 /*
2303 * Since reachable time rarely changes by router
2304 * advertisements, we SHOULD insure that a new random
2305 * value gets recomputed at least once every few hours.
2306 * (RFC 2461, 6.3.4)
2307 */
2308 nd6if->recalctm = nd6_recalc_reachtm_interval;
2309 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2310 }
2311 }
2312 pserialize_read_exit(s);
2313
2314 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2315 }
2316
2317 /*
2318 * Return 0 if a neighbor cache is found. Return EWOULDBLOCK if a cache is not
2319 * found and trying to resolve a neighbor; in this case the mbuf is queued in
2320 * the list. Otherwise return errno after freeing the mbuf.
2321 */
2322 int
2323 nd6_resolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
2324 const struct sockaddr *_dst, uint8_t *lldst, size_t dstsize)
2325 {
2326 struct llentry *ln = NULL;
2327 bool created = false;
2328 const struct sockaddr_in6 *dst = satocsin6(_dst);
2329 int error;
2330
2331 /* discard the packet if IPv6 operation is disabled on the interface */
2332 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2333 m_freem(m);
2334 return ENETDOWN; /* better error? */
2335 }
2336
2337 /*
2338 * Address resolution or Neighbor Unreachability Detection
2339 * for the next hop.
2340 * At this point, the destination of the packet must be a unicast
2341 * or an anycast address(i.e. not a multicast).
2342 */
2343
2344 /* Look up the neighbor cache for the nexthop */
2345 ln = nd6_lookup(&dst->sin6_addr, ifp, false);
2346
2347 if (ln != NULL && (ln->la_flags & LLE_VALID) != 0 &&
2348 ln->ln_state == ND6_LLINFO_REACHABLE) {
2349 /* Fast path */
2350 memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
2351 LLE_RUNLOCK(ln);
2352 return 0;
2353 }
2354 if (ln != NULL)
2355 LLE_RUNLOCK(ln);
2356
2357 /* Slow path */
2358 ln = nd6_lookup(&dst->sin6_addr, ifp, true);
2359 if (ln == NULL && nd6_is_addr_neighbor(dst, ifp)) {
2360 /*
2361 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2362 * the condition below is not very efficient. But we believe
2363 * it is tolerable, because this should be a rare case.
2364 */
2365 ln = nd6_create(&dst->sin6_addr, ifp);
2366 if (ln == NULL) {
2367 char ip6buf[INET6_ADDRSTRLEN];
2368 log(LOG_DEBUG,
2369 "%s: can't allocate llinfo for %s "
2370 "(ln=%p, rt=%p)\n", __func__,
2371 IN6_PRINT(ip6buf, &dst->sin6_addr), ln, rt);
2372 m_freem(m);
2373 return ENOBUFS;
2374 }
2375 created = true;
2376 }
2377
2378 if (ln == NULL) {
2379 m_freem(m);
2380 return ENETDOWN; /* better error? */
2381 }
2382
2383 LLE_WLOCK_ASSERT(ln);
2384
2385 /* We don't have to do link-layer address resolution on a p2p link. */
2386 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2387 ln->ln_state < ND6_LLINFO_REACHABLE) {
2388 ln->ln_state = ND6_LLINFO_STALE;
2389 nd6_llinfo_settimer(ln, nd6_gctimer * hz);
2390 }
2391
2392 /*
2393 * The first time we send a packet to a neighbor whose entry is
2394 * STALE, we have to change the state to DELAY and a sets a timer to
2395 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2396 * neighbor unreachability detection on expiration.
2397 * (RFC 2461 7.3.3)
2398 */
2399 if (ln->ln_state == ND6_LLINFO_STALE) {
2400 ln->ln_asked = 0;
2401 ln->ln_state = ND6_LLINFO_DELAY;
2402 nd6_llinfo_settimer(ln, nd6_delay * hz);
2403 }
2404
2405 /*
2406 * If the neighbor cache entry has a state other than INCOMPLETE
2407 * (i.e. its link-layer address is already resolved), just
2408 * send the packet.
2409 */
2410 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
2411 KASSERT((ln->la_flags & LLE_VALID) != 0);
2412 memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
2413 LLE_WUNLOCK(ln);
2414 return 0;
2415 }
2416
2417 /*
2418 * There is a neighbor cache entry, but no ethernet address
2419 * response yet. Append this latest packet to the end of the
2420 * packet queue in the mbuf, unless the number of the packet
2421 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen,
2422 * the oldest packet in the queue will be removed.
2423 */
2424 if (ln->ln_state == ND6_LLINFO_NOSTATE ||
2425 ln->ln_state == ND6_LLINFO_WAITDELETE)
2426 ln->ln_state = ND6_LLINFO_INCOMPLETE;
2427 if (ln->ln_hold) {
2428 struct mbuf *m_hold;
2429 int i;
2430
2431 i = 0;
2432 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2433 i++;
2434 if (m_hold->m_nextpkt == NULL) {
2435 m_hold->m_nextpkt = m;
2436 break;
2437 }
2438 }
2439 while (i >= nd6_maxqueuelen) {
2440 m_hold = ln->ln_hold;
2441 ln->ln_hold = ln->ln_hold->m_nextpkt;
2442 m_freem(m_hold);
2443 i--;
2444 }
2445 } else {
2446 ln->ln_hold = m;
2447 }
2448
2449 if (ln->ln_asked >= nd6_mmaxtries)
2450 error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
2451 EHOSTUNREACH : EHOSTDOWN;
2452 else
2453 error = EWOULDBLOCK;
2454
2455 /*
2456 * If there has been no NS for the neighbor after entering the
2457 * INCOMPLETE state, send the first solicitation.
2458 */
2459 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2460 struct in6_addr src, *psrc;
2461
2462 ln->ln_asked++;
2463 nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->retrans * hz / 1000);
2464 psrc = nd6_llinfo_get_holdsrc(ln, &src);
2465 LLE_WUNLOCK(ln);
2466 nd6_ns_output(ifp, NULL, &dst->sin6_addr, psrc, NULL);
2467 } else
2468 LLE_WUNLOCK(ln);
2469
2470 if (created)
2471 nd6_gc_neighbors(LLTABLE6(ifp), &dst->sin6_addr);
2472
2473 return error;
2474 }
2475
2476 int
2477 nd6_need_cache(struct ifnet *ifp)
2478 {
2479 /*
2480 * XXX: we currently do not make neighbor cache on any interface
2481 * other than ARCnet, Ethernet, and GIF.
2482 *
2483 * RFC2893 says:
2484 * - unidirectional tunnels needs no ND
2485 */
2486 switch (ifp->if_type) {
2487 case IFT_ARCNET:
2488 case IFT_ETHER:
2489 case IFT_IEEE1394:
2490 case IFT_CARP:
2491 case IFT_GIF: /* XXX need more cases? */
2492 case IFT_PPP:
2493 case IFT_TUNNEL:
2494 return 1;
2495 default:
2496 return 0;
2497 }
2498 }
2499
2500 static void
2501 clear_llinfo_pqueue(struct llentry *ln)
2502 {
2503 struct mbuf *m_hold, *m_hold_next;
2504
2505 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2506 m_hold_next = m_hold->m_nextpkt;
2507 m_hold->m_nextpkt = NULL;
2508 m_freem(m_hold);
2509 }
2510
2511 ln->ln_hold = NULL;
2512 return;
2513 }
2514
2515 int
2516 nd6_sysctl(
2517 int name,
2518 void *oldp, /* syscall arg, need copyout */
2519 size_t *oldlenp,
2520 void *newp, /* syscall arg, need copyin */
2521 size_t newlen
2522 )
2523 {
2524 int (*fill_func)(void *, size_t *);
2525
2526 if (newp)
2527 return EPERM;
2528
2529 switch (name) {
2530 case ICMPV6CTL_ND6_DRLIST:
2531 fill_func = fill_drlist;
2532 break;
2533
2534 case ICMPV6CTL_ND6_PRLIST:
2535 fill_func = fill_prlist;
2536 break;
2537
2538 case ICMPV6CTL_ND6_MAXQLEN:
2539 return 0;
2540
2541 default:
2542 return ENOPROTOOPT;
2543 }
2544
2545 if (oldlenp == NULL)
2546 return EINVAL;
2547
2548 size_t ol;
2549 int error = (*fill_func)(NULL, &ol); /* calc len needed */
2550 if (error)
2551 return error;
2552
2553 if (oldp == NULL) {
2554 *oldlenp = ol;
2555 return 0;
2556 }
2557
2558 ol = *oldlenp = uimin(ol, *oldlenp);
2559 if (ol == 0)
2560 return 0;
2561
2562 void *p = kmem_alloc(ol, KM_SLEEP);
2563 error = (*fill_func)(p, oldlenp);
2564 if (!error)
2565 error = copyout(p, oldp, *oldlenp);
2566 kmem_free(p, ol);
2567
2568 return error;
2569 }
2570
2571 static int
2572 fill_drlist(void *oldp, size_t *oldlenp)
2573 {
2574 int error = 0;
2575 struct in6_defrouter *d = NULL, *de = NULL;
2576 struct nd_defrouter *dr;
2577 size_t l;
2578
2579 if (oldp) {
2580 d = (struct in6_defrouter *)oldp;
2581 de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
2582 }
2583 l = 0;
2584
2585 ND6_RLOCK();
2586 ND_DEFROUTER_LIST_FOREACH(dr) {
2587
2588 if (oldp && d + 1 <= de) {
2589 memset(d, 0, sizeof(*d));
2590 sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0);
2591 if (sa6_recoverscope(&d->rtaddr)) {
2592 char ip6buf[INET6_ADDRSTRLEN];
2593 log(LOG_ERR,
2594 "scope error in router list (%s)\n",
2595 IN6_PRINT(ip6buf, &d->rtaddr.sin6_addr));
2596 /* XXX: press on... */
2597 }
2598 d->flags = dr->flags;
2599 d->rtlifetime = dr->rtlifetime;
2600 d->expire = dr->expire ?
2601 time_mono_to_wall(dr->expire) : 0;
2602 d->if_index = dr->ifp->if_index;
2603 }
2604
2605 l += sizeof(*d);
2606 if (d)
2607 d++;
2608 }
2609 ND6_UNLOCK();
2610
2611 *oldlenp = l; /* (void *)d - (void *)oldp */
2612
2613 return error;
2614 }
2615
2616 static int
2617 fill_prlist(void *oldp, size_t *oldlenp)
2618 {
2619 int error = 0;
2620 struct nd_prefix *pr;
2621 uint8_t *p = NULL, *ps = NULL;
2622 uint8_t *pe = NULL;
2623 size_t l;
2624 char ip6buf[INET6_ADDRSTRLEN];
2625
2626 if (oldp) {
2627 ps = p = (uint8_t*)oldp;
2628 pe = (uint8_t*)oldp + *oldlenp;
2629 }
2630 l = 0;
2631
2632 ND6_RLOCK();
2633 ND_PREFIX_LIST_FOREACH(pr) {
2634 u_short advrtrs;
2635 struct sockaddr_in6 sin6;
2636 struct nd_pfxrouter *pfr;
2637 struct in6_prefix pfx;
2638
2639 if (oldp && p + sizeof(struct in6_prefix) <= pe)
2640 {
2641 memset(&pfx, 0, sizeof(pfx));
2642 ps = p;
2643 pfx.prefix = pr->ndpr_prefix;
2644
2645 if (sa6_recoverscope(&pfx.prefix)) {
2646 log(LOG_ERR,
2647 "scope error in prefix list (%s)\n",
2648 IN6_PRINT(ip6buf, &pfx.prefix.sin6_addr));
2649 /* XXX: press on... */
2650 }
2651 pfx.raflags = pr->ndpr_raf;
2652 pfx.prefixlen = pr->ndpr_plen;
2653 pfx.vltime = pr->ndpr_vltime;
2654 pfx.pltime = pr->ndpr_pltime;
2655 pfx.if_index = pr->ndpr_ifp->if_index;
2656 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2657 pfx.expire = 0;
2658 else {
2659 time_t maxexpire;
2660
2661 /* XXX: we assume time_t is signed. */
2662 maxexpire = (-1) &
2663 ~((time_t)1 <<
2664 ((sizeof(maxexpire) * 8) - 1));
2665 if (pr->ndpr_vltime <
2666 maxexpire - pr->ndpr_lastupdate) {
2667 pfx.expire = pr->ndpr_lastupdate +
2668 pr->ndpr_vltime;
2669 } else
2670 pfx.expire = maxexpire;
2671 }
2672 pfx.refcnt = pr->ndpr_refcnt;
2673 pfx.flags = pr->ndpr_stateflags;
2674 pfx.origin = PR_ORIG_RA;
2675
2676 p += sizeof(pfx); l += sizeof(pfx);
2677
2678 advrtrs = 0;
2679 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2680 if (p + sizeof(sin6) > pe) {
2681 advrtrs++;
2682 continue;
2683 }
2684
2685 sockaddr_in6_init(&sin6, &pfr->router->rtaddr,
2686 0, 0, 0);
2687 if (sa6_recoverscope(&sin6)) {
2688 log(LOG_ERR,
2689 "scope error in "
2690 "prefix list (%s)\n",
2691 IN6_PRINT(ip6buf,
2692 &pfr->router->rtaddr));
2693 }
2694 advrtrs++;
2695 memcpy(p, &sin6, sizeof(sin6));
2696 p += sizeof(sin6);
2697 l += sizeof(sin6);
2698 }
2699 pfx.advrtrs = advrtrs;
2700 memcpy(ps, &pfx, sizeof(pfx));
2701 }
2702 else {
2703 l += sizeof(pfx);
2704 advrtrs = 0;
2705 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2706 advrtrs++;
2707 l += sizeof(sin6);
2708 }
2709 }
2710 }
2711 ND6_UNLOCK();
2712
2713 *oldlenp = l;
2714
2715 return error;
2716 }
2717
2718 static int
2719 nd6_setdefaultiface(int ifindex)
2720 {
2721 ifnet_t *ifp;
2722 int error = 0;
2723 int s;
2724
2725 s = pserialize_read_enter();
2726 ifp = if_byindex(ifindex);
2727 if (ifp == NULL) {
2728 pserialize_read_exit(s);
2729 return EINVAL;
2730 }
2731 if (nd6_defifindex != ifindex) {
2732 nd6_defifindex = ifindex;
2733 nd6_defifp = nd6_defifindex > 0 ? ifp : NULL;
2734
2735 /*
2736 * Our current implementation assumes one-to-one maping between
2737 * interfaces and links, so it would be natural to use the
2738 * default interface as the default link.
2739 */
2740 scope6_setdefault(nd6_defifp);
2741 }
2742 pserialize_read_exit(s);
2743
2744 return (error);
2745 }
2746