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