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