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