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