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