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