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