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