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