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