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