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