1 /**	$MirOS: src/sys/netinet6/nd6.c,v 1.6 2005/07/03 21:19:02 tg Exp $ */
2 /*	$OpenBSD: nd6.c,v 1.69 2005/05/09 05:37:36 brad Exp $	*/
3 /*	$KAME: nd6.c,v 1.280 2002/06/08 19:52:07 itojun Exp $	*/
4 
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/timeout.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/protosw.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
47 #include <sys/queue.h>
48 #include <dev/rndvar.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/if_fddi.h>
54 #include <net/route.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/ip_ipsp.h>
59 
60 #include <netinet6/in6_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet6/nd6.h>
64 #include <netinet/icmp6.h>
65 
66 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
67 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
68 
69 #define SIN6(s) ((struct sockaddr_in6 *)s)
70 #define SDL(s) ((struct sockaddr_dl *)s)
71 
72 /* timer values */
73 int	nd6_prune	= 1;	/* walk list every 1 seconds */
74 int	nd6_delay	= 5;	/* delay first probe time 5 second */
75 int	nd6_umaxtries	= 3;	/* maximum unicast query */
76 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
77 int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
78 int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
79 
80 /* preventing too many loops in ND option parsing */
81 int nd6_maxndopt = 10;	/* max # of ND options allowed */
82 
83 int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
84 
85 #ifdef ND6_DEBUG
86 int nd6_debug = 1;
87 #else
88 int nd6_debug = 0;
89 #endif
90 
91 /* for debugging? */
92 static int nd6_inuse, nd6_allocated;
93 
94 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
95 struct nd_drhead nd_defrouter;
96 struct nd_prhead nd_prefix = { 0 };
97 
98 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
99 static struct sockaddr_in6 all1_sa;
100 
101 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
102 static void nd6_slowtimo(void *);
103 static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
104 static void nd6_llinfo_timer(void *);
105 
106 struct timeout nd6_slowtimo_ch;
107 struct timeout nd6_timer_ch;
108 extern struct timeout in6_tmpaddrtimer_ch;
109 
110 static int fill_drlist(void *, size_t *, size_t);
111 static int fill_prlist(void *, size_t *, size_t);
112 
113 void
nd6_init()114 nd6_init()
115 {
116 	static int nd6_init_done = 0;
117 	int i;
118 
119 	if (nd6_init_done) {
120 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
121 		return;
122 	}
123 
124 	all1_sa.sin6_family = AF_INET6;
125 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
126 	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
127 		all1_sa.sin6_addr.s6_addr[i] = 0xff;
128 
129 	/* initialization of the default router list */
130 	TAILQ_INIT(&nd_defrouter);
131 
132 	nd6_init_done = 1;
133 
134 	/* start timer */
135 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
136 	timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
137 }
138 
139 struct nd_ifinfo *
nd6_ifattach(ifp)140 nd6_ifattach(ifp)
141 	struct ifnet *ifp;
142 {
143 	struct nd_ifinfo *nd;
144 
145 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
146 	bzero(nd, sizeof(*nd));
147 
148 	nd->initialized = 1;
149 
150 	nd->chlim = IPV6_DEFHLIM;
151 	nd->basereachable = REACHABLE_TIME;
152 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
153 	nd->retrans = RETRANS_TIMER;
154 	/*
155 	 * Note that the default value of ip6_accept_rtadv is 0, which means
156 	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
157 	 * here.
158 	 */
159 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
160 
161 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
162 	nd6_setmtu0(ifp, nd);
163 
164 	return nd;
165 }
166 
167 void
nd6_ifdetach(nd)168 nd6_ifdetach(nd)
169 	struct nd_ifinfo *nd;
170 {
171 
172 	free(nd, M_IP6NDP);
173 }
174 
175 void
nd6_setmtu(ifp)176 nd6_setmtu(ifp)
177 	struct ifnet *ifp;
178 {
179 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
180 }
181 
182 void
nd6_setmtu0(ifp,ndi)183 nd6_setmtu0(ifp, ndi)
184 	struct ifnet *ifp;
185 	struct nd_ifinfo *ndi;
186 {
187 	u_int32_t omaxmtu;
188 
189 	omaxmtu = ndi->maxmtu;
190 
191 	switch (ifp->if_type) {
192 	case IFT_ARCNET:
193 		ndi->maxmtu = MIN(60480, ifp->if_mtu); /* RFC2497 */
194 		break;
195 	case IFT_FDDI:
196 		ndi->maxmtu = MIN(FDDIMTU, ifp->if_mtu);
197 		break;
198 	default:
199 		ndi->maxmtu = ifp->if_mtu;
200 		break;
201 	}
202 
203 	/*
204 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
205 	 * undesirable situation.  We thus notify the operator of the change
206 	 * explicitly.  The check for omaxmtu is necessary to restrict the
207 	 * log to the case of changing the MTU, not initializing it.
208 	 */
209 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
210 		log(LOG_NOTICE, "nd6_setmtu0: "
211 		    "new link MTU on %s (%lu) is too small for IPv6\n",
212 		    ifp->if_xname, (unsigned long)ndi->maxmtu);
213 	}
214 
215 	if (ndi->maxmtu > in6_maxmtu)
216 		in6_setmaxmtu(); /* check all interfaces just in case */
217 }
218 
219 void
nd6_option_init(opt,icmp6len,ndopts)220 nd6_option_init(opt, icmp6len, ndopts)
221 	void *opt;
222 	int icmp6len;
223 	union nd_opts *ndopts;
224 {
225 
226 	bzero(ndopts, sizeof(*ndopts));
227 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
228 	ndopts->nd_opts_last
229 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
230 
231 	if (icmp6len == 0) {
232 		ndopts->nd_opts_done = 1;
233 		ndopts->nd_opts_search = NULL;
234 	}
235 }
236 
237 /*
238  * Take one ND option.
239  */
240 struct nd_opt_hdr *
nd6_option(ndopts)241 nd6_option(ndopts)
242 	union nd_opts *ndopts;
243 {
244 	struct nd_opt_hdr *nd_opt;
245 	int olen;
246 
247 	if (!ndopts)
248 		panic("ndopts == NULL in nd6_option");
249 	if (!ndopts->nd_opts_last)
250 		panic("uninitialized ndopts in nd6_option");
251 	if (!ndopts->nd_opts_search)
252 		return NULL;
253 	if (ndopts->nd_opts_done)
254 		return NULL;
255 
256 	nd_opt = ndopts->nd_opts_search;
257 
258 	/* make sure nd_opt_len is inside the buffer */
259 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
260 		bzero(ndopts, sizeof(*ndopts));
261 		return NULL;
262 	}
263 
264 	olen = nd_opt->nd_opt_len << 3;
265 	if (olen == 0) {
266 		/*
267 		 * Message validation requires that all included
268 		 * options have a length that is greater than zero.
269 		 */
270 		bzero(ndopts, sizeof(*ndopts));
271 		return NULL;
272 	}
273 
274 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
275 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
276 		/* option overruns the end of buffer, invalid */
277 		bzero(ndopts, sizeof(*ndopts));
278 		return NULL;
279 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
280 		/* reached the end of options chain */
281 		ndopts->nd_opts_done = 1;
282 		ndopts->nd_opts_search = NULL;
283 	}
284 	return nd_opt;
285 }
286 
287 /*
288  * Parse multiple ND options.
289  * This function is much easier to use, for ND routines that do not need
290  * multiple options of the same type.
291  */
292 int
nd6_options(ndopts)293 nd6_options(ndopts)
294 	union nd_opts *ndopts;
295 {
296 	struct nd_opt_hdr *nd_opt;
297 	int i = 0;
298 
299 	if (!ndopts)
300 		panic("ndopts == NULL in nd6_options");
301 	if (!ndopts->nd_opts_last)
302 		panic("uninitialized ndopts in nd6_options");
303 	if (!ndopts->nd_opts_search)
304 		return 0;
305 
306 	while (1) {
307 		nd_opt = nd6_option(ndopts);
308 		if (!nd_opt && !ndopts->nd_opts_last) {
309 			/*
310 			 * Message validation requires that all included
311 			 * options have a length that is greater than zero.
312 			 */
313 			icmp6stat.icp6s_nd_badopt++;
314 			bzero(ndopts, sizeof(*ndopts));
315 			return -1;
316 		}
317 
318 		if (!nd_opt)
319 			goto skip1;
320 
321 		switch (nd_opt->nd_opt_type) {
322 		case ND_OPT_SOURCE_LINKADDR:
323 		case ND_OPT_TARGET_LINKADDR:
324 		case ND_OPT_MTU:
325 		case ND_OPT_REDIRECTED_HEADER:
326 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
327 				nd6log((LOG_INFO,
328 				    "duplicated ND6 option found (type=%d)\n",
329 				    nd_opt->nd_opt_type));
330 				/* XXX bark? */
331 			} else {
332 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
333 					= nd_opt;
334 			}
335 			break;
336 		case ND_OPT_PREFIX_INFORMATION:
337 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
338 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
339 					= nd_opt;
340 			}
341 			ndopts->nd_opts_pi_end =
342 				(struct nd_opt_prefix_info *)nd_opt;
343 			break;
344 		default:
345 			/*
346 			 * Unknown options must be silently ignored,
347 			 * to accommodate future extension to the protocol.
348 			 */
349 			nd6log((LOG_DEBUG,
350 			    "nd6_options: unsupported option %d - "
351 			    "option ignored\n", nd_opt->nd_opt_type));
352 		}
353 
354 skip1:
355 		i++;
356 		if (i > nd6_maxndopt) {
357 			icmp6stat.icp6s_nd_toomanyopt++;
358 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
359 			break;
360 		}
361 
362 		if (ndopts->nd_opts_done)
363 			break;
364 	}
365 
366 	return 0;
367 }
368 
369 /*
370  * ND6 timer routine to handle ND6 entries
371  */
372 void
nd6_llinfo_settimer(struct llinfo_nd6 * ln,long tick)373 nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
374 {
375 	int s;
376 
377 	s = splsoftnet();
378 
379 	if (tick < 0) {
380 		ln->ln_expire = 0;
381 		ln->ln_ntick = 0;
382 		timeout_del(&ln->ln_timer_ch);
383 	} else {
384 		ln->ln_expire = time.tv_sec + tick / hz;
385 		if (tick > INT_MAX) {
386 			ln->ln_ntick = tick - INT_MAX;
387 			timeout_add(&ln->ln_timer_ch, INT_MAX);
388 		} else {
389 			ln->ln_ntick = 0;
390 			timeout_add(&ln->ln_timer_ch, tick);
391 		}
392 	}
393 
394 	splx(s);
395 }
396 
397 static void
nd6_llinfo_timer(void * arg)398 nd6_llinfo_timer(void *arg)
399 {
400 	int s;
401 	struct llinfo_nd6 *ln;
402 	struct rtentry *rt;
403 	struct sockaddr_in6 *dst;
404 	struct ifnet *ifp;
405 	struct nd_ifinfo *ndi = NULL;
406 
407 	s = splsoftnet();
408 
409 	ln = (struct llinfo_nd6 *)arg;
410 
411 	if (ln->ln_ntick > 0) {
412 		if (ln->ln_ntick > INT_MAX) {
413 			ln->ln_ntick -= INT_MAX;
414 			nd6_llinfo_settimer(ln, INT_MAX);
415 		} else {
416 			ln->ln_ntick = 0;
417 			nd6_llinfo_settimer(ln, ln->ln_ntick);
418 		}
419 		splx(s);
420 		return;
421 	}
422 
423 	if ((rt = ln->ln_rt) == NULL)
424 		panic("ln->ln_rt == NULL");
425 	if ((ifp = rt->rt_ifp) == NULL)
426 		panic("ln->ln_rt->rt_ifp == NULL");
427 	ndi = ND_IFINFO(ifp);
428 	dst = (struct sockaddr_in6 *)rt_key(rt);
429 
430 	/* sanity check */
431 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
432 		panic("rt_llinfo(%p) is not equal to ln(%p)",
433 		      rt->rt_llinfo, ln);
434 	if (!dst)
435 		panic("dst=0 in nd6_timer(ln=%p)", ln);
436 
437 	switch (ln->ln_state) {
438 	case ND6_LLINFO_INCOMPLETE:
439 		if (ln->ln_asked < nd6_mmaxtries) {
440 			ln->ln_asked++;
441 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
442 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
443 		} else {
444 			struct mbuf *m = ln->ln_hold;
445 			if (m) {
446 				ln->ln_hold = NULL;
447 				/*
448 				 * Fake rcvif to make the ICMP error
449 				 * more helpful in diagnosing for the
450 				 * receiver.
451 				 * XXX: should we consider
452 				 * older rcvif?
453 				 */
454 				m->m_pkthdr.rcvif = rt->rt_ifp;
455 
456 				icmp6_error(m, ICMP6_DST_UNREACH,
457 				    ICMP6_DST_UNREACH_ADDR, 0);
458 			}
459 			(void)nd6_free(rt, 0);
460 			ln = NULL;
461 		}
462 		break;
463 	case ND6_LLINFO_REACHABLE:
464 		if (!ND6_LLINFO_PERMANENT(ln)) {
465 			ln->ln_state = ND6_LLINFO_STALE;
466 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
467 		}
468 		break;
469 
470 	case ND6_LLINFO_STALE:
471 		/* Garbage Collection(RFC 2461 5.3) */
472 		if (!ND6_LLINFO_PERMANENT(ln)) {
473 			(void)nd6_free(rt, 1);
474 			ln = NULL;
475 		}
476 		break;
477 
478 	case ND6_LLINFO_DELAY:
479 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
480 			/* We need NUD */
481 			ln->ln_asked = 1;
482 			ln->ln_state = ND6_LLINFO_PROBE;
483 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
484 			nd6_ns_output(ifp, &dst->sin6_addr,
485 			    &dst->sin6_addr, ln, 0);
486 		} else {
487 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
488 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
489 		}
490 		break;
491 	case ND6_LLINFO_PROBE:
492 		if (ln->ln_asked < nd6_umaxtries) {
493 			ln->ln_asked++;
494 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
495 			nd6_ns_output(ifp, &dst->sin6_addr,
496 			    &dst->sin6_addr, ln, 0);
497 		} else {
498 			(void)nd6_free(rt, 0);
499 			ln = NULL;
500 		}
501 		break;
502 	}
503 
504 	splx(s);
505 }
506 
507 /*
508  * ND6 timer routine to expire default route list and prefix list
509  */
510 void
nd6_timer(ignored_arg)511 nd6_timer(ignored_arg)
512 	void	*ignored_arg;
513 {
514 	int s;
515 	struct nd_defrouter *dr;
516 	struct nd_prefix *pr;
517 	struct in6_ifaddr *ia6, *nia6;
518 	struct in6_addrlifetime *lt6;
519 
520 	s = splsoftnet();
521 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
522 	timeout_add(&nd6_timer_ch, nd6_prune * hz);
523 
524 	/* expire default router list */
525 	dr = TAILQ_FIRST(&nd_defrouter);
526 	while (dr) {
527 		if (dr->expire && dr->expire < time.tv_sec) {
528 			struct nd_defrouter *t;
529 			t = TAILQ_NEXT(dr, dr_entry);
530 			defrtrlist_del(dr);
531 			dr = t;
532 		} else {
533 			dr = TAILQ_NEXT(dr, dr_entry);
534 		}
535 	}
536 
537 	/*
538 	 * expire interface addresses.
539 	 * in the past the loop was inside prefix expiry processing.
540 	 * However, from a stricter speci-confrmance standpoint, we should
541 	 * rather separate address lifetimes and prefix lifetimes.
542 	 */
543 	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
544 		nia6 = ia6->ia_next;
545 		/* check address lifetime */
546 		lt6 = &ia6->ia6_lifetime;
547 		if (IFA6_IS_INVALID(ia6)) {
548 			in6_purgeaddr(&ia6->ia_ifa);
549 		}
550 		if (IFA6_IS_DEPRECATED(ia6)) {
551 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
552 		} else {
553 			/*
554 			 * A new RA might have made a deprecated address
555 			 * preferred.
556 			 */
557 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
558 		}
559 	}
560 
561 	/* expire prefix list */
562 	pr = nd_prefix.lh_first;
563 	while (pr) {
564 		/*
565 		 * check prefix lifetime.
566 		 * since pltime is just for autoconf, pltime processing for
567 		 * prefix is not necessary.
568 		 */
569 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
570 		    time.tv_sec - pr->ndpr_lastupdate > pr->ndpr_vltime) {
571 			struct nd_prefix *t;
572 			t = pr->ndpr_next;
573 
574 			/*
575 			 * address expiration and prefix expiration are
576 			 * separate.  NEVER perform in6_purgeaddr here.
577 			 */
578 
579 			prelist_remove(pr);
580 			pr = t;
581 		} else
582 			pr = pr->ndpr_next;
583 	}
584 	splx(s);
585 }
586 
587 /*
588  * Nuke neighbor cache/prefix/default router management table, right before
589  * ifp goes away.
590  */
591 void
nd6_purge(ifp)592 nd6_purge(ifp)
593 	struct ifnet *ifp;
594 {
595 	struct llinfo_nd6 *ln, *nln;
596 	struct nd_defrouter *dr, *ndr;
597 	struct nd_prefix *pr, *npr;
598 
599 	/*
600 	 * Nuke default router list entries toward ifp.
601 	 * We defer removal of default router list entries that is installed
602 	 * in the routing table, in order to keep additional side effects as
603 	 * small as possible.
604 	 */
605 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
606 		ndr = TAILQ_NEXT(dr, dr_entry);
607 		if (dr->installed)
608 			continue;
609 
610 		if (dr->ifp == ifp)
611 			defrtrlist_del(dr);
612 	}
613 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
614 		ndr = TAILQ_NEXT(dr, dr_entry);
615 		if (!dr->installed)
616 			continue;
617 
618 		if (dr->ifp == ifp)
619 			defrtrlist_del(dr);
620 	}
621 
622 	/* Nuke prefix list entries toward ifp */
623 	for (pr = nd_prefix.lh_first; pr; pr = npr) {
624 		npr = pr->ndpr_next;
625 		if (pr->ndpr_ifp == ifp) {
626 			/*
627 			 * Because if_detach() does *not* release prefixes
628 			 * while purging addresses the reference count will
629 			 * still be above zero. We therefore reset it to
630 			 * make sure that the prefix really gets purged.
631 			 */
632 			pr->ndpr_refcnt = 0;
633 			/*
634 			 * Previously, pr->ndpr_addr is removed as well,
635 			 * but I strongly believe we don't have to do it.
636 			 * nd6_purge() is only called from in6_ifdetach(),
637 			 * which removes all the associated interface addresses
638 			 * by itself.
639 			 * (jinmei@kame.net 20010129)
640 			 */
641 			prelist_remove(pr);
642 		}
643 	}
644 
645 	/* cancel default outgoing interface setting */
646 	if (nd6_defifindex == ifp->if_index)
647 		nd6_setdefaultiface(0);
648 
649 	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
650 		/* refresh default router list */
651 		defrouter_select();
652 	}
653 
654 	/*
655 	 * Nuke neighbor cache entries for the ifp.
656 	 * Note that rt->rt_ifp may not be the same as ifp,
657 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
658 	 * nd6_rtrequest(), and ip6_input().
659 	 */
660 	ln = llinfo_nd6.ln_next;
661 	while (ln && ln != &llinfo_nd6) {
662 		struct rtentry *rt;
663 		struct sockaddr_dl *sdl;
664 
665 		nln = ln->ln_next;
666 		rt = ln->ln_rt;
667 		if (rt && rt->rt_gateway &&
668 		    rt->rt_gateway->sa_family == AF_LINK) {
669 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
670 			if (sdl->sdl_index == ifp->if_index)
671 				nln = nd6_free(rt, 0);
672 		}
673 		ln = nln;
674 	}
675 }
676 
677 struct rtentry *
nd6_lookup(addr6,create,ifp)678 nd6_lookup(addr6, create, ifp)
679 	struct in6_addr *addr6;
680 	int create;
681 	struct ifnet *ifp;
682 {
683 	struct rtentry *rt;
684 	struct sockaddr_in6 sin6;
685 
686 	bzero(&sin6, sizeof(sin6));
687 	sin6.sin6_len = sizeof(struct sockaddr_in6);
688 	sin6.sin6_family = AF_INET6;
689 	sin6.sin6_addr = *addr6;
690 
691 	rt = rtalloc1((struct sockaddr *)&sin6, create);
692 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
693 		/*
694 		 * This is the case for the default route.
695 		 * If we want to create a neighbor cache for the address, we
696 		 * should free the route for the destination and allocate an
697 		 * interface route.
698 		 */
699 		if (create) {
700 			RTFREE(rt);
701 			rt = 0;
702 		}
703 	}
704 	if (!rt) {
705 		if (create && ifp) {
706 			int e;
707 
708 			/*
709 			 * If no route is available and create is set,
710 			 * we allocate a host route for the destination
711 			 * and treat it like an interface route.
712 			 * This hack is necessary for a neighbor which can't
713 			 * be covered by our own prefix.
714 			 */
715 			struct ifaddr *ifa =
716 			    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
717 			if (ifa == NULL)
718 				return (NULL);
719 
720 			/*
721 			 * Create a new route.  RTF_LLINFO is necessary
722 			 * to create a Neighbor Cache entry for the
723 			 * destination in nd6_rtrequest which will be
724 			 * called in rtrequest via ifa->ifa_rtrequest.
725 			 */
726 			if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
727 			    ifa->ifa_addr, (struct sockaddr *)&all1_sa,
728 			    (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
729 			    ~RTF_CLONING, &rt)) != 0) {
730 #if 0
731 				log(LOG_ERR,
732 				    "nd6_lookup: failed to add route for a "
733 				    "neighbor(%s), errno=%d\n",
734 				    ip6_sprintf(addr6), e);
735 #endif
736 				return (NULL);
737 			}
738 			if (rt == NULL)
739 				return (NULL);
740 			if (rt->rt_llinfo) {
741 				struct llinfo_nd6 *ln =
742 				    (struct llinfo_nd6 *)rt->rt_llinfo;
743 				ln->ln_state = ND6_LLINFO_NOSTATE;
744 			}
745 		} else
746 			return (NULL);
747 	}
748 	rt->rt_refcnt--;
749 	/*
750 	 * Validation for the entry.
751 	 * Note that the check for rt_llinfo is necessary because a cloned
752 	 * route from a parent route that has the L flag (e.g. the default
753 	 * route to a p2p interface) may have the flag, too, while the
754 	 * destination is not actually a neighbor.
755 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
756 	 *      it might be the loopback interface if the entry is for our
757 	 *      own address on a non-loopback interface. Instead, we should
758 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
759 	 *	interface.
760 	 */
761 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
762 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
763 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
764 		if (create) {
765 			nd6log((LOG_DEBUG,
766 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
767 			    ip6_sprintf(addr6),
768 			    ifp ? ifp->if_xname : "unspec"));
769 		}
770 		return (NULL);
771 	}
772 	return (rt);
773 }
774 
775 /*
776  * Detect if a given IPv6 address identifies a neighbor on a given link.
777  * XXX: should take care of the destination of a p2p link?
778  */
779 int
nd6_is_addr_neighbor(addr,ifp)780 nd6_is_addr_neighbor(addr, ifp)
781 	struct sockaddr_in6 *addr;
782 	struct ifnet *ifp;
783 {
784 	struct nd_prefix *pr;
785 	struct rtentry *rt;
786 
787 	/*
788 	 * A link-local address is always a neighbor.
789 	 * XXX: we should use the sin6_scope_id field rather than the embedded
790 	 * interface index.
791 	 * XXX: a link does not necessarily specify a single interface.
792 	 */
793 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
794 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
795 		return (1);
796 
797 	/*
798 	 * If the address matches one of our on-link prefixes, it should be a
799 	 * neighbor.
800 	 */
801 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
802 		if (pr->ndpr_ifp != ifp)
803 			continue;
804 
805 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
806 			continue;
807 
808 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
809 		    &addr->sin6_addr, &pr->ndpr_mask))
810 			return (1);
811 	}
812 
813 	/*
814 	 * If the default router list is empty, all addresses are regarded
815 	 * as on-link, and thus, as a neighbor.
816 	 * XXX: we restrict the condition to hosts, because routers usually do
817 	 * not have the "default router list".
818 	 */
819 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
820 	    nd6_defifindex == ifp->if_index) {
821 		return (1);
822 	}
823 
824 	/*
825 	 * Even if the address matches none of our addresses, it might be
826 	 * in the neighbor cache.
827 	 */
828 	if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL)
829 		return (1);
830 
831 	return (0);
832 }
833 
834 /*
835  * Free an nd6 llinfo entry.
836  * Since the function would cause significant changes in the kernel, DO NOT
837  * make it global, unless you have a strong reason for the change, and are sure
838  * that the change is safe.
839  */
840 static struct llinfo_nd6 *
nd6_free(rt,gc)841 nd6_free(rt, gc)
842 	struct rtentry *rt;
843 	int gc;
844 {
845 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
846 	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
847 	struct nd_defrouter *dr;
848 
849 	/*
850 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
851 	 * even though it is not harmful, it was not really necessary.
852 	 */
853 
854 	if (!ip6_forwarding) {
855 		int s;
856 		s = splsoftnet();
857 		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
858 		    rt->rt_ifp);
859 
860 		if (dr != NULL && dr->expire &&
861 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
862 			/*
863 			 * If the reason for the deletion is just garbage
864 			 * collection, and the neighbor is an active default
865 			 * router, do not delete it.  Instead, reset the GC
866 			 * timer using the router's lifetime.
867 			 * Simply deleting the entry would affect default
868 			 * router selection, which is not necessarily a good
869 			 * thing, especially when we're using router preference
870 			 * values.
871 			 * XXX: the check for ln_state would be redundant,
872 			 *      but we intentionally keep it just in case.
873 			 */
874 			if (dr->expire > time.tv_sec * hz) {
875 				nd6_llinfo_settimer(ln,
876 				    dr->expire - time.tv_sec * hz);
877 			} else
878 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
879 			splx(s);
880 			return (ln->ln_next);
881 		}
882 
883 		if (ln->ln_router || dr) {
884 			/*
885 			 * rt6_flush must be called whether or not the neighbor
886 			 * is in the Default Router List.
887 			 * See a corresponding comment in nd6_na_input().
888 			 */
889 			rt6_flush(&in6, rt->rt_ifp);
890 		}
891 
892 		if (dr) {
893 			/*
894 			 * Unreachablity of a router might affect the default
895 			 * router selection and on-link detection of advertised
896 			 * prefixes.
897 			 */
898 
899 			/*
900 			 * Temporarily fake the state to choose a new default
901 			 * router and to perform on-link determination of
902 			 * prefixes correctly.
903 			 * Below the state will be set correctly,
904 			 * or the entry itself will be deleted.
905 			 */
906 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
907 
908 			/*
909 			 * Since defrouter_select() does not affect the
910 			 * on-link determination and MIP6 needs the check
911 			 * before the default router selection, we perform
912 			 * the check now.
913 			 */
914 			pfxlist_onlink_check();
915 
916 			/*
917 			 * refresh default router list
918 			 */
919 			defrouter_select();
920 		}
921 		splx(s);
922 	}
923 
924 	/*
925 	 * Before deleting the entry, remember the next entry as the
926 	 * return value.  We need this because pfxlist_onlink_check() above
927 	 * might have freed other entries (particularly the old next entry) as
928 	 * a side effect (XXX).
929 	 */
930 	next = ln->ln_next;
931 
932 	/*
933 	 * Detach the route from the routing tree and the list of neighbor
934 	 * caches, and disable the route entry not to be used in already
935 	 * cached routes.
936 	 */
937 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
938 	    rt_mask(rt), 0, (struct rtentry **)0);
939 
940 	return (next);
941 }
942 
943 /*
944  * Upper-layer reachability hint for Neighbor Unreachability Detection.
945  *
946  * XXX cost-effective metods?
947  */
948 void
nd6_nud_hint(rt,dst6,force)949 nd6_nud_hint(rt, dst6, force)
950 	struct rtentry *rt;
951 	struct in6_addr *dst6;
952 	int force;
953 {
954 	struct llinfo_nd6 *ln;
955 
956 	/*
957 	 * If the caller specified "rt", use that.  Otherwise, resolve the
958 	 * routing table by supplied "dst6".
959 	 */
960 	if (!rt) {
961 		if (!dst6)
962 			return;
963 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
964 			return;
965 	}
966 
967 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
968 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
969 	    !rt->rt_llinfo || !rt->rt_gateway ||
970 	    rt->rt_gateway->sa_family != AF_LINK) {
971 		/* This is not a host route. */
972 		return;
973 	}
974 
975 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
976 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
977 		return;
978 
979 	/*
980 	 * if we get upper-layer reachability confirmation many times,
981 	 * it is possible we have false information.
982 	 */
983 	if (!force) {
984 		ln->ln_byhint++;
985 		if (ln->ln_byhint > nd6_maxnudhint)
986 			return;
987 	}
988 
989 	ln->ln_state = ND6_LLINFO_REACHABLE;
990 	if (!ND6_LLINFO_PERMANENT(ln)) {
991 		nd6_llinfo_settimer(ln,
992 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
993 	}
994 }
995 
996 void
nd6_rtrequest(req,rt,info)997 nd6_rtrequest(req, rt, info)
998 	int	req;
999 	struct rtentry *rt;
1000 	struct rt_addrinfo *info; /* xxx unused */
1001 {
1002 	struct sockaddr *gate = rt->rt_gateway;
1003 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1004 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1005 	struct ifnet *ifp = rt->rt_ifp;
1006 	struct ifaddr *ifa;
1007 	int mine = 0;
1008 
1009 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
1010 		return;
1011 
1012 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1013 		/*
1014 		 * This is probably an interface direct route for a link
1015 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1016 		 * We do not need special treatment below for such a route.
1017 		 * Moreover, the RTF_LLINFO flag which would be set below
1018 		 * would annoy the ndp(8) command.
1019 		 */
1020 		return;
1021 	}
1022 
1023 	if (req == RTM_RESOLVE &&
1024 	    (nd6_need_cache(ifp) == 0 || /* stf case */
1025 	     !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1026 		/*
1027 		 * FreeBSD and BSD/OS often make a cloned host route based
1028 		 * on a less-specific route (e.g. the default route).
1029 		 * If the less specific route does not have a "gateway"
1030 		 * (this is the case when the route just goes to a p2p or an
1031 		 * stf interface), we'll mistakenly make a neighbor cache for
1032 		 * the host route, and will see strange neighbor solicitation
1033 		 * for the corresponding destination.  In order to avoid the
1034 		 * confusion, we check if the destination of the route is
1035 		 * a neighbor in terms of neighbor discovery, and stop the
1036 		 * process if not.  Additionally, we remove the LLINFO flag
1037 		 * so that ndp(8) will not try to get the neighbor information
1038 		 * of the destination.
1039 		 */
1040 		rt->rt_flags &= ~RTF_LLINFO;
1041 		return;
1042 	}
1043 
1044 	switch (req) {
1045 	case RTM_ADD:
1046 		/*
1047 		 * There is no backward compatibility :)
1048 		 *
1049 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1050 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1051 		 *	   rt->rt_flags |= RTF_CLONING;
1052 		 */
1053 		if ((rt->rt_flags & RTF_CLONING) ||
1054 		    ((rt->rt_flags & RTF_LLINFO) && !ln)) {
1055 			/*
1056 			 * Case 1: This route should come from a route to
1057 			 * interface (RTF_CLONING case) or the route should be
1058 			 * treated as on-link but is currently not
1059 			 * (RTF_LLINFO && !ln case).
1060 			 */
1061 			rt_setgate(rt, rt_key(rt),
1062 				   (struct sockaddr *)&null_sdl);
1063 			gate = rt->rt_gateway;
1064 			SDL(gate)->sdl_type = ifp->if_type;
1065 			SDL(gate)->sdl_index = ifp->if_index;
1066 			if (ln)
1067 				nd6_llinfo_settimer(ln, 0);
1068 			if ((rt->rt_flags & RTF_CLONING) != 0)
1069 				break;
1070 		}
1071 		/*
1072 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1073 		 * We don't do that here since llinfo is not ready yet.
1074 		 *
1075 		 * There are also couple of other things to be discussed:
1076 		 * - unsolicited NA code needs improvement beforehand
1077 		 * - RFC2461 says we MAY send multicast unsolicited NA
1078 		 *   (7.2.6 paragraph 4), however, it also says that we
1079 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1080 		 *   we don't have anything like it right now.
1081 		 *   note that the mechanism needs a mutual agreement
1082 		 *   between proxies, which means that we need to implement
1083 		 *   a new protocol, or a new kludge.
1084 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1085 		 *   we need to check ip6forwarding before sending it.
1086 		 *   (or should we allow proxy ND configuration only for
1087 		 *   routers?  there's no mention about proxy ND from hosts)
1088 		 */
1089 #if 0
1090 		/* XXX it does not work */
1091 		if (rt->rt_flags & RTF_ANNOUNCE)
1092 			nd6_na_output(ifp,
1093 			      &SIN6(rt_key(rt))->sin6_addr,
1094 			      &SIN6(rt_key(rt))->sin6_addr,
1095 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1096 			      1, NULL);
1097 #endif
1098 		/* FALLTHROUGH */
1099 	case RTM_RESOLVE:
1100 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1101 			/*
1102 			 * Address resolution isn't necessary for a point to
1103 			 * point link, so we can skip this test for a p2p link.
1104 			 */
1105 			if (gate->sa_family != AF_LINK ||
1106 			    gate->sa_len < sizeof(null_sdl)) {
1107 				log(LOG_DEBUG,
1108 				    "nd6_rtrequest: bad gateway value: %s\n",
1109 				    ifp->if_xname);
1110 				break;
1111 			}
1112 			SDL(gate)->sdl_type = ifp->if_type;
1113 			SDL(gate)->sdl_index = ifp->if_index;
1114 		}
1115 		if (ln != NULL)
1116 			break;	/* This happens on a route change */
1117 		/*
1118 		 * Case 2: This route may come from cloning, or a manual route
1119 		 * add with a LL address.
1120 		 */
1121 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1122 		rt->rt_llinfo = (caddr_t)ln;
1123 		if (!ln) {
1124 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1125 			break;
1126 		}
1127 		nd6_inuse++;
1128 		nd6_allocated++;
1129 		Bzero(ln, sizeof(*ln));
1130 		ln->ln_rt = rt;
1131 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
1132 		/* this is required for "ndp" command. - shin */
1133 		if (req == RTM_ADD) {
1134 		        /*
1135 			 * gate should have some valid AF_LINK entry,
1136 			 * and ln->ln_expire should have some lifetime
1137 			 * which is specified by ndp command.
1138 			 */
1139 			ln->ln_state = ND6_LLINFO_REACHABLE;
1140 			ln->ln_byhint = 0;
1141 		} else {
1142 		        /*
1143 			 * When req == RTM_RESOLVE, rt is created and
1144 			 * initialized in rtrequest(), so rt_expire is 0.
1145 			 */
1146 			ln->ln_state = ND6_LLINFO_NOSTATE;
1147 			nd6_llinfo_settimer(ln, 0);
1148 		}
1149 		rt->rt_flags |= RTF_LLINFO;
1150 		ln->ln_next = llinfo_nd6.ln_next;
1151 		llinfo_nd6.ln_next = ln;
1152 		ln->ln_prev = &llinfo_nd6;
1153 		ln->ln_next->ln_prev = ln;
1154 
1155 		/*
1156 		 * check if rt_key(rt) is one of my address assigned
1157 		 * to the interface.
1158 		 */
1159 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1160 		    &SIN6(rt_key(rt))->sin6_addr);
1161 		if (ifa) {
1162 			caddr_t macp = nd6_ifptomac(ifp);
1163 			nd6_llinfo_settimer(ln, -1);
1164 			ln->ln_state = ND6_LLINFO_REACHABLE;
1165 			ln->ln_byhint = 0;
1166 			mine = 1;
1167 			if (macp) {
1168 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1169 				SDL(gate)->sdl_alen = ifp->if_addrlen;
1170 			}
1171 			if (nd6_useloopback) {
1172 				rt->rt_ifp = lo0ifp;	/*XXX*/
1173 				/*
1174 				 * Make sure rt_ifa be equal to the ifaddr
1175 				 * corresponding to the address.
1176 				 * We need this because when we refer
1177 				 * rt_ifa->ia6_flags in ip6_input, we assume
1178 				 * that the rt_ifa points to the address instead
1179 				 * of the loopback address.
1180 				 */
1181 				if (ifa != rt->rt_ifa) {
1182 					IFAFREE(rt->rt_ifa);
1183 					ifa->ifa_refcnt++;
1184 					rt->rt_ifa = ifa;
1185 				}
1186 			}
1187 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
1188 			nd6_llinfo_settimer(ln, -1);
1189 			ln->ln_state = ND6_LLINFO_REACHABLE;
1190 			ln->ln_byhint = 0;
1191 
1192 			/* join solicited node multicast for proxy ND */
1193 			if (ifp->if_flags & IFF_MULTICAST) {
1194 				struct in6_addr llsol;
1195 				int error;
1196 
1197 				llsol = SIN6(rt_key(rt))->sin6_addr;
1198 				llsol.s6_addr16[0] = htons(0xff02);
1199 				llsol.s6_addr16[1] = htons(ifp->if_index);
1200 				llsol.s6_addr32[1] = 0;
1201 				llsol.s6_addr32[2] = htonl(1);
1202 				llsol.s6_addr8[12] = 0xff;
1203 
1204 				if (in6_addmulti(&llsol, ifp, &error)) {
1205 					nd6log((LOG_ERR, "%s: failed to join "
1206 					    "%s (errno=%d)\n", ifp->if_xname,
1207 					    ip6_sprintf(&llsol), error));
1208 				}
1209 			}
1210 		}
1211 		break;
1212 
1213 	case RTM_DELETE:
1214 		if (!ln)
1215 			break;
1216 		/* leave from solicited node multicast for proxy ND */
1217 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1218 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1219 			struct in6_addr llsol;
1220 			struct in6_multi *in6m;
1221 
1222 			llsol = SIN6(rt_key(rt))->sin6_addr;
1223 			llsol.s6_addr16[0] = htons(0xff02);
1224 			llsol.s6_addr16[1] = htons(ifp->if_index);
1225 			llsol.s6_addr32[1] = 0;
1226 			llsol.s6_addr32[2] = htonl(1);
1227 			llsol.s6_addr8[12] = 0xff;
1228 
1229 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1230 			if (in6m)
1231 				in6_delmulti(in6m);
1232 		}
1233 		nd6_inuse--;
1234 		ln->ln_next->ln_prev = ln->ln_prev;
1235 		ln->ln_prev->ln_next = ln->ln_next;
1236 		ln->ln_prev = NULL;
1237 		nd6_llinfo_settimer(ln, -1);
1238 		rt->rt_llinfo = 0;
1239 		rt->rt_flags &= ~RTF_LLINFO;
1240 		if (ln->ln_hold)
1241 			m_freem(ln->ln_hold);
1242 		Free((caddr_t)ln);
1243 	}
1244 }
1245 
1246 int
nd6_ioctl(cmd,data,ifp)1247 nd6_ioctl(cmd, data, ifp)
1248 	u_long cmd;
1249 	caddr_t	data;
1250 	struct ifnet *ifp;
1251 {
1252 	struct in6_drlist *drl = (struct in6_drlist *)data;
1253 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1254 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1255 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1256 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1257 	struct nd_defrouter *dr;
1258 	struct nd_prefix *pr;
1259 	struct rtentry *rt;
1260 	int i = 0, error = 0;
1261 	int s;
1262 
1263 	switch (cmd) {
1264 	case SIOCGDRLST_IN6:
1265 		/*
1266 		 * obsolete API, use sysctl under net.inet6.icmp6
1267 		 */
1268 		bzero(drl, sizeof(*drl));
1269 		s = splsoftnet();
1270 		dr = TAILQ_FIRST(&nd_defrouter);
1271 		while (dr && i < DRLSTSIZ) {
1272 			drl->defrouter[i].rtaddr = dr->rtaddr;
1273 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1274 				/* XXX: need to this hack for KAME stack */
1275 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1276 			} else
1277 				log(LOG_ERR,
1278 				    "default router list contains a "
1279 				    "non-linklocal address(%s)\n",
1280 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
1281 
1282 			drl->defrouter[i].flags = dr->flags;
1283 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1284 			drl->defrouter[i].expire = dr->expire;
1285 			drl->defrouter[i].if_index = dr->ifp->if_index;
1286 			i++;
1287 			dr = TAILQ_NEXT(dr, dr_entry);
1288 		}
1289 		splx(s);
1290 		break;
1291 	case SIOCGPRLST_IN6:
1292 		/*
1293 		 * obsolete API, use sysctl under net.inet6.icmp6
1294 		 *
1295 		 * XXX the structure in6_prlist was changed in backward-
1296 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1297 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1298 		 */
1299 		/*
1300 		 * XXX meaning of fields, especialy "raflags", is very
1301 		 * differnet between RA prefix list and RR/static prefix list.
1302 		 * how about separating ioctls into two?
1303 		 */
1304 		bzero(oprl, sizeof(*oprl));
1305 		s = splsoftnet();
1306 		pr = nd_prefix.lh_first;
1307 		while (pr && i < PRLSTSIZ) {
1308 			struct nd_pfxrouter *pfr;
1309 			int j;
1310 
1311 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1312 			oprl->prefix[i].raflags = pr->ndpr_raf;
1313 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1314 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1315 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1316 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1317 			oprl->prefix[i].expire = pr->ndpr_expire;
1318 
1319 			pfr = pr->ndpr_advrtrs.lh_first;
1320 			j = 0;
1321 			while(pfr) {
1322 				if (j < DRLSTSIZ) {
1323 #define RTRADDR oprl->prefix[i].advrtr[j]
1324 					RTRADDR = pfr->router->rtaddr;
1325 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1326 						/* XXX: hack for KAME */
1327 						RTRADDR.s6_addr16[1] = 0;
1328 					} else
1329 						log(LOG_ERR,
1330 						    "a router(%s) advertises "
1331 						    "a prefix with "
1332 						    "non-link local address\n",
1333 						    ip6_sprintf(&RTRADDR));
1334 #undef RTRADDR
1335 				}
1336 				j++;
1337 				pfr = pfr->pfr_next;
1338 			}
1339 			oprl->prefix[i].advrtrs = j;
1340 			oprl->prefix[i].origin = PR_ORIG_RA;
1341 
1342 			i++;
1343 			pr = pr->ndpr_next;
1344 		}
1345 		splx(s);
1346 
1347 		break;
1348 	case OSIOCGIFINFO_IN6:
1349 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1350 		bzero(&ndi->ndi, sizeof(ndi->ndi));
1351 		ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1352 		ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1353 		ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1354 		ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1355 		ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1356 		ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1357 		ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1358 		ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1359 		break;
1360 	case SIOCGIFINFO_IN6:
1361 		ndi->ndi = *ND_IFINFO(ifp);
1362 		break;
1363 	case SIOCSIFINFO_FLAGS:
1364 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1365 		break;
1366 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1367 		/* sync kernel routing table with the default router list */
1368 		defrouter_reset();
1369 		defrouter_select();
1370 		break;
1371 	case SIOCSPFXFLUSH_IN6:
1372 	{
1373 		/* flush all the prefix advertised by routers */
1374 		struct nd_prefix *pr, *next;
1375 
1376 		s = splsoftnet();
1377 		for (pr = nd_prefix.lh_first; pr; pr = next) {
1378 			struct in6_ifaddr *ia, *ia_next;
1379 
1380 			next = pr->ndpr_next;
1381 
1382 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1383 				continue; /* XXX */
1384 
1385 			/* do we really have to remove addresses as well? */
1386 			for (ia = in6_ifaddr; ia; ia = ia_next) {
1387 				/* ia might be removed.  keep the next ptr. */
1388 				ia_next = ia->ia_next;
1389 
1390 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1391 					continue;
1392 
1393 				if (ia->ia6_ndpr == pr)
1394 					in6_purgeaddr(&ia->ia_ifa);
1395 			}
1396 			prelist_remove(pr);
1397 		}
1398 		splx(s);
1399 		break;
1400 	}
1401 	case SIOCSRTRFLUSH_IN6:
1402 	{
1403 		/* flush all the default routers */
1404 		struct nd_defrouter *dr, *next;
1405 
1406 		s = splsoftnet();
1407 		defrouter_reset();
1408 		for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1409 			next = TAILQ_NEXT(dr, dr_entry);
1410 			defrtrlist_del(dr);
1411 		}
1412 		defrouter_select();
1413 		splx(s);
1414 		break;
1415 	}
1416 	case SIOCGNBRINFO_IN6:
1417 	{
1418 		struct llinfo_nd6 *ln;
1419 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1420 
1421 		/*
1422 		 * XXX: KAME specific hack for scoped addresses
1423 		 *      XXXX: for other scopes than link-local?
1424 		 */
1425 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1426 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1427 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1428 
1429 			if (*idp == 0)
1430 				*idp = htons(ifp->if_index);
1431 		}
1432 
1433 		s = splsoftnet();
1434 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1435 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1436 			error = EINVAL;
1437 			splx(s);
1438 			break;
1439 		}
1440 		nbi->state = ln->ln_state;
1441 		nbi->asked = ln->ln_asked;
1442 		nbi->isrouter = ln->ln_router;
1443 		nbi->expire = ln->ln_expire;
1444 		splx(s);
1445 
1446 		break;
1447 	}
1448 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1449 		ndif->ifindex = nd6_defifindex;
1450 		break;
1451 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1452 		return (nd6_setdefaultiface(ndif->ifindex));
1453 		break;
1454 	}
1455 	return (error);
1456 }
1457 
1458 /*
1459  * Create neighbor cache entry and cache link-layer address,
1460  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1461  */
1462 struct rtentry *
nd6_cache_lladdr(ifp,from,lladdr,lladdrlen,type,code)1463 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1464 	struct ifnet *ifp;
1465 	struct in6_addr *from;
1466 	char *lladdr;
1467 	int lladdrlen;
1468 	int type;	/* ICMP6 type */
1469 	int code;	/* type dependent information */
1470 {
1471 	struct rtentry *rt = NULL;
1472 	struct llinfo_nd6 *ln = NULL;
1473 	int is_newentry;
1474 	struct sockaddr_dl *sdl = NULL;
1475 	int do_update;
1476 	int olladdr;
1477 	int llchange;
1478 	int newstate = 0;
1479 
1480 	if (!ifp)
1481 		panic("ifp == NULL in nd6_cache_lladdr");
1482 	if (!from)
1483 		panic("from == NULL in nd6_cache_lladdr");
1484 
1485 	/* nothing must be updated for unspecified address */
1486 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1487 		return NULL;
1488 
1489 	/*
1490 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1491 	 * the caller.
1492 	 *
1493 	 * XXX If the link does not have link-layer adderss, what should
1494 	 * we do? (ifp->if_addrlen == 0)
1495 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1496 	 * description on it in NS section (RFC 2461 7.2.3).
1497 	 */
1498 
1499 	rt = nd6_lookup(from, 0, ifp);
1500 	if (!rt) {
1501 #if 0
1502 		/* nothing must be done if there's no lladdr */
1503 		if (!lladdr || !lladdrlen)
1504 			return NULL;
1505 #endif
1506 
1507 		rt = nd6_lookup(from, 1, ifp);
1508 		is_newentry = 1;
1509 	} else {
1510 		/* do nothing if static ndp is set */
1511 		if (rt->rt_flags & RTF_STATIC)
1512 			return NULL;
1513 		is_newentry = 0;
1514 	}
1515 
1516 	if (!rt)
1517 		return NULL;
1518 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1519 fail:
1520 		(void)nd6_free(rt, 0);
1521 		return NULL;
1522 	}
1523 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1524 	if (!ln)
1525 		goto fail;
1526 	if (!rt->rt_gateway)
1527 		goto fail;
1528 	if (rt->rt_gateway->sa_family != AF_LINK)
1529 		goto fail;
1530 	sdl = SDL(rt->rt_gateway);
1531 
1532 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1533 	if (olladdr && lladdr) {
1534 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1535 			llchange = 1;
1536 		else
1537 			llchange = 0;
1538 	} else
1539 		llchange = 0;
1540 
1541 	/*
1542 	 * newentry olladdr  lladdr  llchange	(*=record)
1543 	 *	0	n	n	--	(1)
1544 	 *	0	y	n	--	(2)
1545 	 *	0	n	y	--	(3) * STALE
1546 	 *	0	y	y	n	(4) *
1547 	 *	0	y	y	y	(5) * STALE
1548 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1549 	 *	1	--	y	--	(7) * STALE
1550 	 */
1551 
1552 	if (lladdr) {		/* (3-5) and (7) */
1553 		/*
1554 		 * Record source link-layer address
1555 		 * XXX is it dependent to ifp->if_type?
1556 		 */
1557 		sdl->sdl_alen = ifp->if_addrlen;
1558 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1559 	}
1560 
1561 	if (!is_newentry) {
1562 		if ((!olladdr && lladdr) ||		/* (3) */
1563 		    (olladdr && lladdr && llchange)) {	/* (5) */
1564 			do_update = 1;
1565 			newstate = ND6_LLINFO_STALE;
1566 		} else					/* (1-2,4) */
1567 			do_update = 0;
1568 	} else {
1569 		do_update = 1;
1570 		if (!lladdr)				/* (6) */
1571 			newstate = ND6_LLINFO_NOSTATE;
1572 		else					/* (7) */
1573 			newstate = ND6_LLINFO_STALE;
1574 	}
1575 
1576 	if (do_update) {
1577 		/*
1578 		 * Update the state of the neighbor cache.
1579 		 */
1580 		ln->ln_state = newstate;
1581 
1582 		if (ln->ln_state == ND6_LLINFO_STALE) {
1583 			/*
1584 			 * XXX: since nd6_output() below will cause
1585 			 * state tansition to DELAY and reset the timer,
1586 			 * we must set the timer now, although it is actually
1587 			 * meaningless.
1588 			 */
1589 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1590 
1591 			if (ln->ln_hold) {
1592 				/*
1593 				 * we assume ifp is not a p2p here, so just
1594 				 * set the 2nd argument as the 1st one.
1595 				 */
1596 				nd6_output(ifp, ifp, ln->ln_hold,
1597 				    (struct sockaddr_in6 *)rt_key(rt), rt);
1598 				ln->ln_hold = NULL;
1599 			}
1600 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1601 			/* probe right away */
1602 			nd6_llinfo_settimer((void *)ln, 0);
1603 		}
1604 	}
1605 
1606 	/*
1607 	 * ICMP6 type dependent behavior.
1608 	 *
1609 	 * NS: clear IsRouter if new entry
1610 	 * RS: clear IsRouter
1611 	 * RA: set IsRouter if there's lladdr
1612 	 * redir: clear IsRouter if new entry
1613 	 *
1614 	 * RA case, (1):
1615 	 * The spec says that we must set IsRouter in the following cases:
1616 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1617 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1618 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1619 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1620 	 * neighbor cache, this is similar to (6).
1621 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1622 	 *
1623 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1624 	 *							D R
1625 	 *	0	n	n	--	(1)	c   ?     s
1626 	 *	0	y	n	--	(2)	c   s     s
1627 	 *	0	n	y	--	(3)	c   s     s
1628 	 *	0	y	y	n	(4)	c   s     s
1629 	 *	0	y	y	y	(5)	c   s     s
1630 	 *	1	--	n	--	(6) c	c 	c s
1631 	 *	1	--	y	--	(7) c	c   s	c s
1632 	 *
1633 	 *					(c=clear s=set)
1634 	 */
1635 	switch (type & 0xff) {
1636 	case ND_NEIGHBOR_SOLICIT:
1637 		/*
1638 		 * New entry must have is_router flag cleared.
1639 		 */
1640 		if (is_newentry)	/* (6-7) */
1641 			ln->ln_router = 0;
1642 		break;
1643 	case ND_REDIRECT:
1644 		/*
1645 		 * If the icmp is a redirect to a better router, always set the
1646 		 * is_router flag.  Otherwise, if the entry is newly created,
1647 		 * clear the flag.  [RFC 2461, sec 8.3]
1648 		 */
1649 		if (code == ND_REDIRECT_ROUTER)
1650 			ln->ln_router = 1;
1651 		else if (is_newentry) /* (6-7) */
1652 			ln->ln_router = 0;
1653 		break;
1654 	case ND_ROUTER_SOLICIT:
1655 		/*
1656 		 * is_router flag must always be cleared.
1657 		 */
1658 		ln->ln_router = 0;
1659 		break;
1660 	case ND_ROUTER_ADVERT:
1661 		/*
1662 		 * Mark an entry with lladdr as a router.
1663 		 */
1664 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1665 		    (is_newentry && lladdr)) {			/* (7) */
1666 			ln->ln_router = 1;
1667 		}
1668 		break;
1669 	}
1670 
1671 	/*
1672 	 * When the link-layer address of a router changes, select the
1673 	 * best router again.  In particular, when the neighbor entry is newly
1674 	 * created, it might affect the selection policy.
1675 	 * Question: can we restrict the first condition to the "is_newentry"
1676 	 * case?
1677 	 * XXX: when we hear an RA from a new router with the link-layer
1678 	 * address option, defrouter_select() is called twice, since
1679 	 * defrtrlist_update called the function as well.  However, I believe
1680 	 * we can compromise the overhead, since it only happens the first
1681 	 * time.
1682 	 * XXX: although defrouter_select() should not have a bad effect
1683 	 * for those are not autoconfigured hosts, we explicitly avoid such
1684 	 * cases for safety.
1685 	 */
1686 	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1687 		defrouter_select();
1688 
1689 	return rt;
1690 }
1691 
1692 static void
nd6_slowtimo(ignored_arg)1693 nd6_slowtimo(ignored_arg)
1694     void *ignored_arg;
1695 {
1696 	int s = splsoftnet();
1697 	struct nd_ifinfo *nd6if;
1698 	struct ifnet *ifp;
1699 
1700 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
1701 	timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
1702 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1703 	{
1704 		nd6if = ND_IFINFO(ifp);
1705 		if (nd6if->basereachable && /* already initialized */
1706 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1707 			/*
1708 			 * Since reachable time rarely changes by router
1709 			 * advertisements, we SHOULD insure that a new random
1710 			 * value gets recomputed at least once every few hours.
1711 			 * (RFC 2461, 6.3.4)
1712 			 */
1713 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1714 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1715 		}
1716 	}
1717 	splx(s);
1718 }
1719 
1720 #define senderr(e) { error = (e); goto bad;}
1721 int
nd6_output(ifp,origifp,m0,dst,rt0)1722 nd6_output(ifp, origifp, m0, dst, rt0)
1723 	struct ifnet *ifp;
1724 	struct ifnet *origifp;
1725 	struct mbuf *m0;
1726 	struct sockaddr_in6 *dst;
1727 	struct rtentry *rt0;
1728 {
1729 	struct mbuf *m = m0;
1730 	struct rtentry *rt = rt0;
1731 	struct sockaddr_in6 *gw6 = NULL;
1732 	struct llinfo_nd6 *ln = NULL;
1733 	int error = 0;
1734 #ifdef IPSEC
1735 	struct m_tag *mtag;
1736 #endif /* IPSEC */
1737 
1738 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1739 		goto sendpkt;
1740 
1741 	if (nd6_need_cache(ifp) == 0)
1742 		goto sendpkt;
1743 
1744 	/*
1745 	 * next hop determination.  This routine is derived from ether_outpout.
1746 	 */
1747 	if (rt) {
1748 		if ((rt->rt_flags & RTF_UP) == 0) {
1749 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst,
1750 			    1)) != NULL)
1751 			{
1752 				rt->rt_refcnt--;
1753 				if (rt->rt_ifp != ifp)
1754 					senderr(EHOSTUNREACH);
1755 			} else
1756 				senderr(EHOSTUNREACH);
1757 		}
1758 
1759 		if (rt->rt_flags & RTF_GATEWAY) {
1760 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1761 
1762 			/*
1763 			 * We skip link-layer address resolution and NUD
1764 			 * if the gateway is not a neighbor from ND point
1765 			 * of view, regardless of the value of nd_ifinfo.flags.
1766 			 * The second condition is a bit tricky; we skip
1767 			 * if the gateway is our own address, which is
1768 			 * sometimes used to install a route to a p2p link.
1769 			 */
1770 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1771 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1772 				/*
1773 				 * We allow this kind of tricky route only
1774 				 * when the outgoing interface is p2p.
1775 				 * XXX: we may need a more generic rule here.
1776 				 */
1777 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1778 					senderr(EHOSTUNREACH);
1779 
1780 				goto sendpkt;
1781 			}
1782 
1783 			if (rt->rt_gwroute == 0)
1784 				goto lookup;
1785 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1786 				rtfree(rt); rt = rt0;
1787 			lookup:
1788 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1789 				if ((rt = rt->rt_gwroute) == 0)
1790 					senderr(EHOSTUNREACH);
1791 			}
1792 		}
1793 	}
1794 
1795 	/*
1796 	 * Address resolution or Neighbor Unreachability Detection
1797 	 * for the next hop.
1798 	 * At this point, the destination of the packet must be a unicast
1799 	 * or an anycast address(i.e. not a multicast).
1800 	 */
1801 
1802 	/* Look up the neighbor cache for the nexthop */
1803 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1804 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1805 	else {
1806 		/*
1807 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1808 		 * the condition below is not very efficient.  But we believe
1809 		 * it is tolerable, because this should be a rare case.
1810 		 */
1811 		if (nd6_is_addr_neighbor(dst, ifp) &&
1812 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1813 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1814 	}
1815 	if (!ln || !rt) {
1816 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1817 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1818 			log(LOG_DEBUG,
1819 			    "nd6_output: can't allocate llinfo for %s "
1820 			    "(ln=%p, rt=%p)\n",
1821 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1822 			senderr(EIO);	/* XXX: good error? */
1823 		}
1824 
1825 		goto sendpkt;	/* send anyway */
1826 	}
1827 
1828 	/* We don't have to do link-layer address resolution on a p2p link. */
1829 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1830 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1831 		ln->ln_state = ND6_LLINFO_STALE;
1832 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1833 	}
1834 
1835 	/*
1836 	 * The first time we send a packet to a neighbor whose entry is
1837 	 * STALE, we have to change the state to DELAY and a sets a timer to
1838 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1839 	 * neighbor unreachability detection on expiration.
1840 	 * (RFC 2461 7.3.3)
1841 	 */
1842 	if (ln->ln_state == ND6_LLINFO_STALE) {
1843 		ln->ln_asked = 0;
1844 		ln->ln_state = ND6_LLINFO_DELAY;
1845 		nd6_llinfo_settimer(ln, nd6_delay * hz);
1846 	}
1847 
1848 	/*
1849 	 * If the neighbor cache entry has a state other than INCOMPLETE
1850 	 * (i.e. its link-layer address is already resolved), just
1851 	 * send the packet.
1852 	 */
1853 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1854 		goto sendpkt;
1855 
1856 	/*
1857 	 * There is a neighbor cache entry, but no ethernet address
1858 	 * response yet.  Replace the held mbuf (if any) with this
1859 	 * latest one.
1860 	 */
1861 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1862 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1863 	if (ln->ln_hold)
1864 		m_freem(ln->ln_hold);
1865 	ln->ln_hold = m;
1866 	/*
1867 	 * If there has been no NS for the neighbor after entering the
1868 	 * INCOMPLETE state, send the first solicitation.
1869 	 */
1870 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1871 		ln->ln_asked++;
1872 		nd6_llinfo_settimer(ln,
1873 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1874 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1875 	}
1876 	return (0);
1877 
1878   sendpkt:
1879 #ifdef IPSEC
1880 	/*
1881 	 * If the packet needs outgoing IPsec crypto processing and the
1882 	 * interface doesn't support it, drop it.
1883 	 */
1884 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
1885 #endif /* IPSEC */
1886 
1887 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1888 #ifdef IPSEC
1889 		if (mtag != NULL &&
1890 		    (origifp->if_capabilities & IFCAP_IPSEC) == 0) {
1891 			/* Tell IPsec to do its own crypto. */
1892 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1893 			error = EACCES;
1894 			goto bad;
1895 		}
1896 #endif /* IPSEC */
1897 		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1898 		    rt));
1899 	}
1900 #ifdef IPSEC
1901 	if (mtag != NULL &&
1902 	    (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
1903 		/* Tell IPsec to do its own crypto. */
1904 		ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1905 		error = EACCES;
1906 		goto bad;
1907 	}
1908 #endif /* IPSEC */
1909 	return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1910 
1911   bad:
1912 	if (m)
1913 		m_freem(m);
1914 	return (error);
1915 }
1916 #undef senderr
1917 
1918 int
nd6_need_cache(ifp)1919 nd6_need_cache(ifp)
1920 	struct ifnet *ifp;
1921 {
1922 	/*
1923 	 * XXX: we currently do not make neighbor cache on any interface
1924 	 * other than ARCnet, Ethernet, FDDI and GIF.
1925 	 *
1926 	 * RFC2893 says:
1927 	 * - unidirectional tunnels needs no ND
1928 	 */
1929 	switch (ifp->if_type) {
1930 	case IFT_ARCNET:
1931 	case IFT_ETHER:
1932 	case IFT_FDDI:
1933 	case IFT_IEEE1394:
1934 	case IFT_PROPVIRTUAL:
1935 	case IFT_L2VLAN:
1936 	case IFT_IEEE80211:
1937 #ifdef IFT_CARP
1938 	case IFT_CARP:
1939 #endif
1940 	case IFT_GIF:		/* XXX need more cases? */
1941 		return (1);
1942 	default:
1943 		return (0);
1944 	}
1945 }
1946 
1947 int
nd6_storelladdr(ifp,rt,m,dst,desten)1948 nd6_storelladdr(ifp, rt, m, dst, desten)
1949 	struct ifnet *ifp;
1950 	struct rtentry *rt;
1951 	struct mbuf *m;
1952 	struct sockaddr *dst;
1953 	u_char *desten;
1954 {
1955 	struct sockaddr_dl *sdl;
1956 
1957 	if (m->m_flags & M_MCAST) {
1958 		switch (ifp->if_type) {
1959 		case IFT_ETHER:
1960 		case IFT_FDDI:
1961 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1962 						 desten);
1963 			return (1);
1964 			break;
1965 		case IFT_ARCNET:
1966 			*desten = 0;
1967 			return (1);
1968 		default:
1969 			m_freem(m);
1970 			return (0);
1971 		}
1972 	}
1973 
1974 	if (rt == NULL) {
1975 		/* this could happen, if we could not allocate memory */
1976 		m_freem(m);
1977 		return (0);
1978 	}
1979 	if (rt->rt_gateway->sa_family != AF_LINK) {
1980 		printf("nd6_storelladdr: something odd happens\n");
1981 		m_freem(m);
1982 		return (0);
1983 	}
1984 	sdl = SDL(rt->rt_gateway);
1985 	if (sdl->sdl_alen == 0) {
1986 		/* this should be impossible, but we bark here for debugging */
1987 		printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
1988 		    ip6_sprintf(&SIN6(dst)->sin6_addr), ifp->if_xname);
1989 		m_freem(m);
1990 		return (0);
1991 	}
1992 
1993 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1994 	return (1);
1995 }
1996 
1997 int
nd6_sysctl(name,oldp,oldlenp,newp,newlen)1998 nd6_sysctl(name, oldp, oldlenp, newp, newlen)
1999 	int name;
2000 	void *oldp;	/* syscall arg, need copyout */
2001 	size_t *oldlenp;
2002 	void *newp;	/* syscall arg, need copyin */
2003 	size_t newlen;
2004 {
2005 	void *p;
2006 	size_t ol, l;
2007 	int error;
2008 
2009 	error = 0;
2010 	l = 0;
2011 
2012 	if (newp)
2013 		return EPERM;
2014 	if (oldp && !oldlenp)
2015 		return EINVAL;
2016 	ol = oldlenp ? *oldlenp : 0;
2017 
2018 	if (oldp) {
2019 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2020 		if (!p)
2021 			return ENOMEM;
2022 	} else
2023 		p = NULL;
2024 	switch (name) {
2025 	case ICMPV6CTL_ND6_DRLIST:
2026 		error = fill_drlist(p, oldlenp, ol);
2027 		if (!error && p && oldp)
2028 			error = copyout(p, oldp, *oldlenp);
2029 		break;
2030 
2031 	case ICMPV6CTL_ND6_PRLIST:
2032 		error = fill_prlist(p, oldlenp, ol);
2033 		if (!error && p && oldp)
2034 			error = copyout(p, oldp, *oldlenp);
2035 		break;
2036 
2037 	default:
2038 		error = ENOPROTOOPT;
2039 		break;
2040 	}
2041 	if (p)
2042 		free(p, M_TEMP);
2043 
2044 	return (error);
2045 }
2046 
2047 static int
fill_drlist(oldp,oldlenp,ol)2048 fill_drlist(oldp, oldlenp, ol)
2049 	void *oldp;
2050 	size_t *oldlenp, ol;
2051 {
2052 	int error = 0, s;
2053 	struct in6_defrouter *d = NULL, *de = NULL;
2054 	struct nd_defrouter *dr;
2055 	size_t l;
2056 
2057 	s = splsoftnet();
2058 
2059 	if (oldp) {
2060 		d = (struct in6_defrouter *)oldp;
2061 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
2062 	}
2063 	l = 0;
2064 
2065 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2066 	     dr = TAILQ_NEXT(dr, dr_entry)) {
2067 
2068 		if (oldp && d + 1 <= de) {
2069 			bzero(d, sizeof(*d));
2070 			d->rtaddr.sin6_family = AF_INET6;
2071 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2072 			d->rtaddr.sin6_addr = dr->rtaddr;
2073 			in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
2074 			    dr->ifp);
2075 			d->flags = dr->flags;
2076 			d->rtlifetime = dr->rtlifetime;
2077 			d->expire = dr->expire;
2078 			d->if_index = dr->ifp->if_index;
2079 		}
2080 
2081 		l += sizeof(*d);
2082 		if (d)
2083 			d++;
2084 	}
2085 
2086 	if (oldp) {
2087 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2088 		if (l > ol)
2089 			error = ENOMEM;
2090 	} else
2091 		*oldlenp = l;
2092 
2093 	splx(s);
2094 
2095 	return (error);
2096 }
2097 
2098 static int
fill_prlist(oldp,oldlenp,ol)2099 fill_prlist(oldp, oldlenp, ol)
2100 	void *oldp;
2101 	size_t *oldlenp, ol;
2102 {
2103 	int error = 0, s;
2104 	struct nd_prefix *pr;
2105 	struct in6_prefix *p = NULL;
2106 	struct in6_prefix *pe = NULL;
2107 	size_t l;
2108 
2109 	s = splsoftnet();
2110 
2111 	if (oldp) {
2112 		p = (struct in6_prefix *)oldp;
2113 		pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
2114 	}
2115 	l = 0;
2116 
2117 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2118 		u_short advrtrs;
2119 		size_t advance;
2120 		struct sockaddr_in6 *sin6;
2121 		struct sockaddr_in6 *s6;
2122 		struct nd_pfxrouter *pfr;
2123 
2124 		if (oldp && p + 1 <= pe)
2125 		{
2126 			bzero(p, sizeof(*p));
2127 			sin6 = (struct sockaddr_in6 *)(p + 1);
2128 
2129 			p->prefix = pr->ndpr_prefix;
2130 			if (in6_recoverscope(&p->prefix,
2131 			    &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2132 				log(LOG_ERR,
2133 				    "scope error in prefix list (%s)\n",
2134 				    ip6_sprintf(&p->prefix.sin6_addr));
2135 			p->raflags = pr->ndpr_raf;
2136 			p->prefixlen = pr->ndpr_plen;
2137 			p->vltime = pr->ndpr_vltime;
2138 			p->pltime = pr->ndpr_pltime;
2139 			p->if_index = pr->ndpr_ifp->if_index;
2140 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2141 				p->expire = 0;
2142 			else {
2143 				time_t maxexpire;
2144 
2145 				/* XXX: we assume time_t is signed. */
2146 				maxexpire = (sizeof(maxexpire) == 8)
2147 				    ? 0x7FFFFFFFFFFFFFFFLL
2148 				    : 0x7FFFFFFFLL;
2149 				if (pr->ndpr_vltime <
2150 				    maxexpire - pr->ndpr_lastupdate) {
2151 					p->expire = pr->ndpr_lastupdate +
2152 						pr->ndpr_vltime;
2153 				} else
2154 					p->expire = maxexpire;
2155 			}
2156 			p->refcnt = pr->ndpr_refcnt;
2157 			p->flags = pr->ndpr_stateflags;
2158 			p->origin = PR_ORIG_RA;
2159 			advrtrs = 0;
2160 			for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2161 			     pfr = pfr->pfr_next) {
2162 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2163 					advrtrs++;
2164 					continue;
2165 				}
2166 				s6 = &sin6[advrtrs];
2167 				s6->sin6_family = AF_INET6;
2168 				s6->sin6_len = sizeof(struct sockaddr_in6);
2169 				s6->sin6_addr = pfr->router->rtaddr;
2170 				in6_recoverscope(s6, &pfr->router->rtaddr,
2171 				    pfr->router->ifp);
2172 				advrtrs++;
2173 			}
2174 			p->advrtrs = advrtrs;
2175 		}
2176 		else {
2177 			advrtrs = 0;
2178 			for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2179 			     pfr = pfr->pfr_next)
2180 				advrtrs++;
2181 		}
2182 
2183 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2184 		l += advance;
2185 		if (p)
2186 			p = (struct in6_prefix *)((caddr_t)p + advance);
2187 	}
2188 
2189 	if (oldp) {
2190 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2191 		if (l > ol)
2192 			error = ENOMEM;
2193 	} else
2194 		*oldlenp = l;
2195 
2196 	splx(s);
2197 
2198 	return (error);
2199 }
2200