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