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