nd6.c revision 62587
1254219Scy/*	$FreeBSD: head/sys/netinet6/nd6.c 62587 2000-07-04 16:35:15Z itojun $	*/
2254219Scy/*	$KAME: nd6.c,v 1.68 2000/07/02 14:48:02 itojun Exp $	*/
3254219Scy
4254219Scy/*
5254219Scy * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6254219Scy * All rights reserved.
7254219Scy *
8254219Scy * Redistribution and use in source and binary forms, with or without
9254219Scy * modification, are permitted provided that the following conditions
10254219Scy * are met:
11254219Scy * 1. Redistributions of source code must retain the above copyright
12254219Scy *    notice, this list of conditions and the following disclaimer.
13254219Scy * 2. Redistributions in binary form must reproduce the above copyright
14254219Scy *    notice, this list of conditions and the following disclaimer in the
15254219Scy *    documentation and/or other materials provided with the distribution.
16254219Scy * 3. Neither the name of the project nor the names of its contributors
17254219Scy *    may be used to endorse or promote products derived from this software
18254219Scy *    without specific prior written permission.
19254219Scy *
20254219Scy * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21254219Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22254219Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23254219Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24254219Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25254219Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26254219Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27254219Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28254219Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29254219Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30254219Scy * SUCH DAMAGE.
31254219Scy */
32254219Scy
33254219Scy/*
34254219Scy * XXX
35254219Scy * KAME 970409 note:
36254219Scy * BSD/OS version heavily modifies this code, related to llinfo.
37254219Scy * Since we don't have BSD/OS version of net/route.c in our hand,
38254219Scy * I left the code mostly as it was in 970310.  -- itojun
39254219Scy */
40254219Scy
41254219Scy#include "opt_inet.h"
42254219Scy#include "opt_inet6.h"
43254219Scy
44254219Scy#include <sys/param.h>
45254219Scy#include <sys/systm.h>
46254219Scy#include <sys/malloc.h>
47254219Scy#include <sys/mbuf.h>
48254219Scy#include <sys/socket.h>
49254219Scy#include <sys/sockio.h>
50254219Scy#include <sys/time.h>
51254219Scy#include <sys/kernel.h>
52254219Scy#include <sys/protosw.h>
53254219Scy#include <sys/errno.h>
54254219Scy#include <sys/syslog.h>
55254219Scy#include <sys/queue.h>
56254219Scy
57254219Scy#include <net/if.h>
58254219Scy#include <net/if_dl.h>
59254219Scy#include <net/if_types.h>
60254219Scy#include <net/if_atm.h>
61254219Scy#include <net/route.h>
62254219Scy
63254219Scy#include <netinet/in.h>
64254219Scy#include <netinet/if_ether.h>
65254219Scy#include <netinet/if_fddi.h>
66254219Scy#include <netinet6/in6_var.h>
67254219Scy#include <netinet/ip6.h>
68254219Scy#include <netinet6/ip6_var.h>
69254219Scy#include <netinet6/nd6.h>
70254219Scy#include <netinet6/in6_prefix.h>
71254219Scy#include <netinet/icmp6.h>
72254219Scy
73254219Scy#include "loop.h"
74254219Scy
75254219Scy#include <net/net_osdep.h>
76254219Scy
77254219Scy#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
78254219Scy#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
79254219Scy
80254219Scy#define SIN6(s) ((struct sockaddr_in6 *)s)
81254219Scy#define SDL(s) ((struct sockaddr_dl *)s)
82254219Scy
83254219Scy/* timer values */
84254219Scyint	nd6_prune	= 1;	/* walk list every 1 seconds */
85254219Scyint	nd6_delay	= 5;	/* delay first probe time 5 second */
86254219Scyint	nd6_umaxtries	= 3;	/* maximum unicast query */
87254219Scyint	nd6_mmaxtries	= 3;	/* maximum multicast query */
88254219Scyint	nd6_useloopback = 1;	/* use loopback interface for local traffic */
89254219Scy
90254219Scy/* preventing too many loops in ND option parsing */
91254219Scyint nd6_maxndopt = 10;	/* max # of ND options allowed */
92254219Scy
93254219Scyint nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
94254219Scy
95254219Scy/* for debugging? */
96254219Scystatic int nd6_inuse, nd6_allocated;
97254219Scy
98254219Scystruct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
99254219Scystatic size_t nd_ifinfo_indexlim = 8;
100254219Scystruct nd_ifinfo *nd_ifinfo = NULL;
101254219Scystruct nd_drhead nd_defrouter;
102254219Scystruct nd_prhead nd_prefix = { 0 };
103254219Scy
104254219Scyint nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
105254219Scystatic struct sockaddr_in6 all1_sa;
106254219Scy
107254219Scystatic void nd6_slowtimo __P((void *));
108254219Scy
109254219Scyvoid
110254219Scynd6_init()
111254219Scy{
112254219Scy	static int nd6_init_done = 0;
113254219Scy	int i;
114254219Scy
115254219Scy	if (nd6_init_done) {
116254219Scy		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
117254219Scy		return;
118254219Scy	}
119254219Scy
120254219Scy	all1_sa.sin6_family = AF_INET6;
121254219Scy	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
122254219Scy	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
123254219Scy		all1_sa.sin6_addr.s6_addr[i] = 0xff;
124254219Scy
125254219Scy	/* initialization of the default router list */
126254219Scy	TAILQ_INIT(&nd_defrouter);
127254219Scy
128254219Scy	nd6_init_done = 1;
129254219Scy
130254219Scy	/* start timer */
131254219Scy	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
132254219Scy}
133254219Scy
134254219Scyvoid
135254219Scynd6_ifattach(ifp)
136254219Scy	struct ifnet *ifp;
137254219Scy{
138254219Scy
139254219Scy	/*
140254219Scy	 * We have some arrays that should be indexed by if_index.
141254219Scy	 * since if_index will grow dynamically, they should grow too.
142254219Scy	 */
143254219Scy	if (nd_ifinfo == NULL || if_index >= nd_ifinfo_indexlim) {
144254219Scy		size_t n;
145254219Scy		caddr_t q;
146254219Scy
147254219Scy		while (if_index >= nd_ifinfo_indexlim)
148254219Scy			nd_ifinfo_indexlim <<= 1;
149254219Scy
150254219Scy		/* grow nd_ifinfo */
151254219Scy		n = nd_ifinfo_indexlim * sizeof(struct nd_ifinfo);
152254219Scy		q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
153254219Scy		bzero(q, n);
154254219Scy		if (nd_ifinfo) {
155254219Scy			bcopy((caddr_t)nd_ifinfo, q, n/2);
156254219Scy			free((caddr_t)nd_ifinfo, M_IP6NDP);
157254219Scy		}
158254219Scy		nd_ifinfo = (struct nd_ifinfo *)q;
159254219Scy	}
160254219Scy
161254219Scy#define ND nd_ifinfo[ifp->if_index]
162254219Scy
163254219Scy	/* don't initialize if called twice */
164254219Scy	if (ND.linkmtu)
165254219Scy		return;
166254219Scy
167254219Scy	ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
168254219Scy	ND.chlim = IPV6_DEFHLIM;
169254219Scy	ND.basereachable = REACHABLE_TIME;
170254219Scy	ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
171254219Scy	ND.retrans = RETRANS_TIMER;
172254219Scy	ND.receivedra = 0;
173254219Scy	ND.flags = ND6_IFF_PERFORMNUD;
174254219Scy	nd6_setmtu(ifp);
175254219Scy#undef ND
176254219Scy}
177254219Scy
178254219Scy/*
179254219Scy * Reset ND level link MTU. This function is called when the physical MTU
180254219Scy * changes, which means we might have to adjust the ND level MTU.
181254219Scy */
182254219Scyvoid
183254219Scynd6_setmtu(ifp)
184254219Scy	struct ifnet *ifp;
185254219Scy{
186254219Scy#define MIN(a,b) ((a) < (b) ? (a) : (b))
187254219Scy	struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
188254219Scy	u_long oldmaxmtu = ndi->maxmtu;
189254219Scy	u_long oldlinkmtu = ndi->linkmtu;
190254219Scy
191254219Scy	switch(ifp->if_type) {
192254219Scy	 case IFT_ARCNET:	/* XXX MTU handling needs more work */
193254219Scy		 ndi->maxmtu = MIN(60480, ifp->if_mtu);
194254219Scy		 break;
195254219Scy	 case IFT_ETHER:
196254219Scy		 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
197254219Scy		 break;
198254219Scy	 case IFT_FDDI:
199254219Scy		 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
200254219Scy		 break;
201254219Scy	 case IFT_ATM:
202254219Scy		 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
203254219Scy		 break;
204254219Scy	 default:
205254219Scy		 ndi->maxmtu = ifp->if_mtu;
206254219Scy		 break;
207254219Scy	}
208254219Scy
209254219Scy	if (oldmaxmtu != ndi->maxmtu) {
210254219Scy		/*
211254219Scy		 * If the ND level MTU is not set yet, or if the maxmtu
212254219Scy		 * is reset to a smaller value than the ND level MTU,
213254219Scy		 * also reset the ND level MTU.
214254219Scy		 */
215254219Scy		if (ndi->linkmtu == 0 ||
216254219Scy		    ndi->maxmtu < ndi->linkmtu) {
217254219Scy			ndi->linkmtu = ndi->maxmtu;
218254219Scy			/* also adjust in6_maxmtu if necessary. */
219254219Scy			if (oldlinkmtu == 0) {
220254219Scy				/*
221254219Scy				 * XXX: the case analysis is grotty, but
222254219Scy				 * it is not efficient to call in6_setmaxmtu()
223254219Scy				 * here when we are during the initialization
224254219Scy				 * procedure.
225254219Scy				 */
226254219Scy				if (in6_maxmtu < ndi->linkmtu)
227254219Scy					in6_maxmtu = ndi->linkmtu;
228254219Scy			} else
229254219Scy				in6_setmaxmtu();
230254219Scy		}
231254219Scy	}
232254219Scy#undef MIN
233254219Scy}
234254219Scy
235254219Scyvoid
236254219Scynd6_option_init(opt, icmp6len, ndopts)
237254219Scy	void *opt;
238254219Scy	int icmp6len;
239254219Scy	union nd_opts *ndopts;
240254219Scy{
241254219Scy	bzero(ndopts, sizeof(*ndopts));
242254219Scy	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
243254219Scy	ndopts->nd_opts_last
244254219Scy		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
245254219Scy
246254219Scy	if (icmp6len == 0) {
247254219Scy		ndopts->nd_opts_done = 1;
248254219Scy		ndopts->nd_opts_search = NULL;
249254219Scy	}
250254219Scy}
251254219Scy
252254219Scy/*
253254219Scy * Take one ND option.
254254219Scy */
255254219Scystruct nd_opt_hdr *
256254219Scynd6_option(ndopts)
257254219Scy	union nd_opts *ndopts;
258254219Scy{
259254219Scy	struct nd_opt_hdr *nd_opt;
260254219Scy	int olen;
261254219Scy
262254219Scy	if (!ndopts)
263254219Scy		panic("ndopts == NULL in nd6_option\n");
264254219Scy	if (!ndopts->nd_opts_last)
265254219Scy		panic("uninitialized ndopts in nd6_option\n");
266254219Scy	if (!ndopts->nd_opts_search)
267254219Scy		return NULL;
268254219Scy	if (ndopts->nd_opts_done)
269254219Scy		return NULL;
270254219Scy
271254219Scy	nd_opt = ndopts->nd_opts_search;
272254219Scy
273254219Scy	olen = nd_opt->nd_opt_len << 3;
274254219Scy	if (olen == 0) {
275254219Scy		/*
276254219Scy		 * Message validation requires that all included
277254219Scy		 * options have a length that is greater than zero.
278254219Scy		 */
279254219Scy		bzero(ndopts, sizeof(*ndopts));
280254219Scy		return NULL;
281254219Scy	}
282254219Scy
283254219Scy	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
284254219Scy	if (!(ndopts->nd_opts_search < ndopts->nd_opts_last)) {
285254219Scy		ndopts->nd_opts_done = 1;
286254219Scy		ndopts->nd_opts_search = NULL;
287254219Scy	}
288254219Scy	return nd_opt;
289254219Scy}
290254219Scy
291254219Scy/*
292254219Scy * Parse multiple ND options.
293254219Scy * This function is much easier to use, for ND routines that do not need
294254219Scy * multiple options of the same type.
295254219Scy */
296254219Scyint
297254219Scynd6_options(ndopts)
298254219Scy	union nd_opts *ndopts;
299254219Scy{
300254219Scy	struct nd_opt_hdr *nd_opt;
301254219Scy	int i = 0;
302254219Scy
303254219Scy	if (!ndopts)
304254219Scy		panic("ndopts == NULL in nd6_options\n");
305254219Scy	if (!ndopts->nd_opts_last)
306254219Scy		panic("uninitialized ndopts in nd6_options\n");
307254219Scy	if (!ndopts->nd_opts_search)
308254219Scy		return 0;
309254219Scy
310254219Scy	while (1) {
311254219Scy		nd_opt = nd6_option(ndopts);
312254219Scy		if (!nd_opt && !ndopts->nd_opts_last) {
313254219Scy			/*
314254219Scy			 * Message validation requires that all included
315254219Scy			 * options have a length that is greater than zero.
316254219Scy			 */
317254219Scy			bzero(ndopts, sizeof(*ndopts));
318254219Scy			return -1;
319254219Scy		}
320254219Scy
321254219Scy		if (!nd_opt)
322254219Scy			goto skip1;
323254219Scy
324254219Scy		switch (nd_opt->nd_opt_type) {
325254219Scy		case ND_OPT_SOURCE_LINKADDR:
326254219Scy		case ND_OPT_TARGET_LINKADDR:
327254219Scy		case ND_OPT_MTU:
328254219Scy		case ND_OPT_REDIRECTED_HEADER:
329254219Scy			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
330254219Scy				printf("duplicated ND6 option found "
331254219Scy					"(type=%d)\n", nd_opt->nd_opt_type);
332254219Scy				/* XXX bark? */
333254219Scy			} else {
334254219Scy				ndopts->nd_opt_array[nd_opt->nd_opt_type]
335254219Scy					= nd_opt;
336254219Scy			}
337254219Scy			break;
338254219Scy		case ND_OPT_PREFIX_INFORMATION:
339254219Scy			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
340254219Scy				ndopts->nd_opt_array[nd_opt->nd_opt_type]
341254219Scy					= nd_opt;
342254219Scy			}
343254219Scy			ndopts->nd_opts_pi_end =
344254219Scy				(struct nd_opt_prefix_info *)nd_opt;
345254219Scy			break;
346254219Scy		default:
347254219Scy			/*
348254219Scy			 * Unknown options must be silently ignored,
349254219Scy			 * to accomodate future extension to the protocol.
350254219Scy			 */
351254219Scy			log(LOG_DEBUG,
352254219Scy			    "nd6_options: unsupported option %d - "
353254219Scy			    "option ignored\n", nd_opt->nd_opt_type);
354254219Scy		}
355254219Scy
356254219Scyskip1:
357254219Scy		i++;
358254219Scy		if (i > nd6_maxndopt) {
359254219Scy			icmp6stat.icp6s_nd_toomanyopt++;
360254219Scy			printf("too many loop in nd opt\n");
361254219Scy			break;
362254219Scy		}
363254219Scy
364254219Scy		if (ndopts->nd_opts_done)
365254219Scy			break;
366254219Scy	}
367254219Scy
368254219Scy	return 0;
369254219Scy}
370254219Scy
371254219Scy/*
372254219Scy * ND6 timer routine to expire default route list and prefix list
373254219Scy */
374254219Scyvoid
375254219Scynd6_timer(ignored_arg)
376254219Scy	void	*ignored_arg;
377254219Scy{
378254219Scy	int s;
379254219Scy	register struct llinfo_nd6 *ln;
380254219Scy	register struct nd_defrouter *dr;
381254219Scy	register struct nd_prefix *pr;
382254219Scy
383254219Scy	s = splnet();
384254219Scy	timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
385254219Scy
386254219Scy	ln = llinfo_nd6.ln_next;
387254219Scy	/* XXX BSD/OS separates this code -- itojun */
388254219Scy	while (ln && ln != &llinfo_nd6) {
389254219Scy		struct rtentry *rt;
390254219Scy		struct ifnet *ifp;
391254219Scy		struct sockaddr_in6 *dst;
392254219Scy		struct llinfo_nd6 *next = ln->ln_next;
393254219Scy		/* XXX: used for the DELAY case only: */
394254219Scy		struct nd_ifinfo *ndi = NULL;
395254219Scy
396254219Scy		if ((rt = ln->ln_rt) == NULL) {
397254219Scy			ln = next;
398254219Scy			continue;
399254219Scy		}
400254219Scy		if ((ifp = rt->rt_ifp) == NULL) {
401254219Scy			ln = next;
402254219Scy			continue;
403254219Scy		}
404254219Scy		ndi = &nd_ifinfo[ifp->if_index];
405254219Scy		dst = (struct sockaddr_in6 *)rt_key(rt);
406254219Scy
407254219Scy		if (ln->ln_expire > time_second) {
408254219Scy			ln = next;
409254219Scy			continue;
410254219Scy		}
411254219Scy
412254219Scy		/* sanity check */
413254219Scy		if (!rt)
414254219Scy			panic("rt=0 in nd6_timer(ln=%p)\n", ln);
415254219Scy		if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
416254219Scy			panic("rt_llinfo(%p) is not equal to ln(%p)\n",
417254219Scy			      rt->rt_llinfo, ln);
418254219Scy		if (!dst)
419254219Scy			panic("dst=0 in nd6_timer(ln=%p)\n", ln);
420254219Scy
421254219Scy		switch (ln->ln_state) {
422254219Scy		case ND6_LLINFO_INCOMPLETE:
423254219Scy			if (ln->ln_asked < nd6_mmaxtries) {
424254219Scy				ln->ln_asked++;
425254219Scy				ln->ln_expire = time_second +
426254219Scy					nd_ifinfo[ifp->if_index].retrans / 1000;
427254219Scy				nd6_ns_output(ifp, NULL, &dst->sin6_addr,
428254219Scy					ln, 0);
429254219Scy			} else {
430254219Scy				struct mbuf *m = ln->ln_hold;
431254219Scy				if (m) {
432254219Scy					if (rt->rt_ifp) {
433254219Scy						/*
434254219Scy						 * Fake rcvif to make ICMP error
435254219Scy						 * more helpful in diagnosing
436254219Scy						 * for the receiver.
437254219Scy						 * XXX: should we consider
438254219Scy						 * older rcvif?
439254219Scy						 */
440254219Scy						m->m_pkthdr.rcvif = rt->rt_ifp;
441254219Scy					}
442254219Scy					icmp6_error(m, ICMP6_DST_UNREACH,
443254219Scy						    ICMP6_DST_UNREACH_ADDR, 0);
444254219Scy					ln->ln_hold = NULL;
445254219Scy				}
446254219Scy				nd6_free(rt);
447254219Scy			}
448254219Scy			break;
449254219Scy		case ND6_LLINFO_REACHABLE:
450254219Scy			if (ln->ln_expire)
451254219Scy				ln->ln_state = ND6_LLINFO_STALE;
452254219Scy			break;
453254219Scy		/*
454254219Scy		 * ND6_LLINFO_STALE state requires nothing for timer
455254219Scy		 * routine.
456254219Scy		 */
457254219Scy		case ND6_LLINFO_DELAY:
458254219Scy			if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
459254219Scy				/* We need NUD */
460254219Scy				ln->ln_asked = 1;
461254219Scy				ln->ln_state = ND6_LLINFO_PROBE;
462254219Scy				ln->ln_expire = time_second +
463254219Scy					ndi->retrans / 1000;
464254219Scy				nd6_ns_output(ifp, &dst->sin6_addr,
465254219Scy					      &dst->sin6_addr,
466254219Scy					      ln, 0);
467254219Scy			} else
468254219Scy				ln->ln_state = ND6_LLINFO_STALE; /* XXX */
469254219Scy			break;
470254219Scy		case ND6_LLINFO_PROBE:
471254219Scy			if (ln->ln_asked < nd6_umaxtries) {
472254219Scy				ln->ln_asked++;
473254219Scy				ln->ln_expire = time_second +
474254219Scy					nd_ifinfo[ifp->if_index].retrans / 1000;
475254219Scy				nd6_ns_output(ifp, &dst->sin6_addr,
476254219Scy					       &dst->sin6_addr, ln, 0);
477254219Scy			} else {
478254219Scy				nd6_free(rt);
479254219Scy			}
480254219Scy			break;
481254219Scy		case ND6_LLINFO_WAITDELETE:
482254219Scy			nd6_free(rt);
483254219Scy			break;
484254219Scy		}
485254219Scy		ln = next;
486254219Scy	}
487254219Scy
488254219Scy	/* expire */
489254219Scy	dr = TAILQ_FIRST(&nd_defrouter);
490254219Scy	while (dr) {
491254219Scy		if (dr->expire && dr->expire < time_second) {
492254219Scy			struct nd_defrouter *t;
493254219Scy			t = TAILQ_NEXT(dr, dr_entry);
494254219Scy			defrtrlist_del(dr);
495254219Scy			dr = t;
496254219Scy		} else {
497254219Scy			dr = TAILQ_NEXT(dr, dr_entry);
498254219Scy		}
499254219Scy	}
500254219Scy	pr = nd_prefix.lh_first;
501254219Scy	while (pr) {
502254219Scy		struct in6_ifaddr *ia6;
503254219Scy		struct in6_addrlifetime *lt6;
504254219Scy
505254219Scy		if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
506254219Scy			ia6 = NULL;
507254219Scy		else
508254219Scy			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
509254219Scy
510254219Scy		if (ia6) {
511254219Scy			/* check address lifetime */
512254219Scy			lt6 = &ia6->ia6_lifetime;
513254219Scy			if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
514254219Scy				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
515254219Scy			if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
516254219Scy				if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
517254219Scy					in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
518254219Scy				/* xxx ND_OPT_PI_FLAG_ONLINK processing */
519254219Scy			}
520254219Scy		}
521254219Scy
522254219Scy		/*
523254219Scy		 * check prefix lifetime.
524254219Scy		 * since pltime is just for autoconf, pltime processing for
525254219Scy		 * prefix is not necessary.
526254219Scy		 *
527254219Scy		 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
528254219Scy		 * can use the old prefix information to validate the
529254219Scy		 * next prefix information to come.  See prelist_update()
530254219Scy		 * for actual validation.
531254219Scy		 */
532254219Scy		if (pr->ndpr_expire
533254219Scy		 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
534254219Scy			struct nd_prefix *t;
535254219Scy			t = pr->ndpr_next;
536254219Scy
537254219Scy			/*
538254219Scy			 * address expiration and prefix expiration are
539254219Scy			 * separate.  NEVER perform in6_ifdel here.
540254219Scy			 */
541254219Scy
542254219Scy			prelist_remove(pr);
543254219Scy			pr = t;
544254219Scy		} else
545254219Scy			pr = pr->ndpr_next;
546254219Scy	}
547254219Scy	splx(s);
548254219Scy}
549254219Scy
550254219Scy/*
551254219Scy * Nuke neighbor cache/prefix/default router management table, right before
552254219Scy * ifp goes away.
553254219Scy */
554254219Scyvoid
555254219Scynd6_purge(ifp)
556254219Scy	struct ifnet *ifp;
557254219Scy{
558254219Scy	struct llinfo_nd6 *ln, *nln;
559254219Scy	struct nd_defrouter *dr, *ndr, drany;
560254219Scy	struct nd_prefix *pr, *npr;
561254219Scy
562254219Scy	/* Nuke default router list entries toward ifp */
563254219Scy	if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
564254219Scy		/*
565254219Scy		 * The first entry of the list may be stored in
566254219Scy		 * the routing table, so we'll delete it later.
567254219Scy		 */
568254219Scy		for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
569254219Scy			ndr = TAILQ_NEXT(dr, dr_entry);
570254219Scy			if (dr->ifp == ifp)
571254219Scy				defrtrlist_del(dr);
572254219Scy		}
573254219Scy		dr = TAILQ_FIRST(&nd_defrouter);
574254219Scy		if (dr->ifp == ifp)
575254219Scy			defrtrlist_del(dr);
576254219Scy	}
577254219Scy
578254219Scy	/* Nuke prefix list entries toward ifp */
579254219Scy	for (pr = nd_prefix.lh_first; pr; pr = npr) {
580254219Scy		npr = pr->ndpr_next;
581254219Scy		if (pr->ndpr_ifp == ifp) {
582254219Scy			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
583				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
584			prelist_remove(pr);
585		}
586	}
587
588	/* cancel default outgoing interface setting */
589	if (nd6_defifindex == ifp->if_index)
590		nd6_setdefaultiface(0);
591
592	/* refresh default router list */
593	bzero(&drany, sizeof(drany));
594	defrouter_delreq(&drany, 0);
595	defrouter_select();
596
597	/*
598	 * Nuke neighbor cache entries for the ifp.
599	 * Note that rt->rt_ifp may not be the same as ifp,
600	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
601	 * nd6_rtrequest(), and ip6_input().
602	 */
603	ln = llinfo_nd6.ln_next;
604	while (ln && ln != &llinfo_nd6) {
605		struct rtentry *rt;
606		struct sockaddr_dl *sdl;
607
608		nln = ln->ln_next;
609		rt = ln->ln_rt;
610		if (rt && rt->rt_gateway &&
611		    rt->rt_gateway->sa_family == AF_LINK) {
612			sdl = (struct sockaddr_dl *)rt->rt_gateway;
613			if (sdl->sdl_index == ifp->if_index)
614				nd6_free(rt);
615		}
616		ln = nln;
617	}
618
619	/*
620	 * Neighbor cache entry for interface route will be retained
621	 * with ND6_LLINFO_WAITDELETE state, by nd6_free().  Nuke it.
622	 */
623	ln = llinfo_nd6.ln_next;
624	while (ln && ln != &llinfo_nd6) {
625		struct rtentry *rt;
626		struct sockaddr_dl *sdl;
627
628		nln = ln->ln_next;
629		rt = ln->ln_rt;
630		if (rt && rt->rt_gateway &&
631		    rt->rt_gateway->sa_family == AF_LINK) {
632			sdl = (struct sockaddr_dl *)rt->rt_gateway;
633			if (sdl->sdl_index == ifp->if_index) {
634				rtrequest(RTM_DELETE, rt_key(rt),
635				    (struct sockaddr *)0, rt_mask(rt), 0,
636				    (struct rtentry **)0);
637			}
638		}
639		ln = nln;
640	}
641}
642
643struct rtentry *
644nd6_lookup(addr6, create, ifp)
645	struct in6_addr *addr6;
646	int create;
647	struct ifnet *ifp;
648{
649	struct rtentry *rt;
650	struct sockaddr_in6 sin6;
651
652	bzero(&sin6, sizeof(sin6));
653	sin6.sin6_len = sizeof(struct sockaddr_in6);
654	sin6.sin6_family = AF_INET6;
655	sin6.sin6_addr = *addr6;
656#ifdef SCOPEDROUTING
657	sin6.sin6_scope_id = in6_addr2scopeid(ifp, addr6);
658#endif
659	rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
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		} else
713			return(NULL);
714	}
715	rt->rt_refcnt--;
716	/*
717	 * Validation for the entry.
718	 * XXX: we can't use rt->rt_ifp to check for the interface, since
719	 *      it might be the loopback interface if the entry is for our
720	 *      own address on a non-loopback interface. Instead, we should
721	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
722	 */
723	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
724	    rt->rt_gateway->sa_family != AF_LINK ||
725	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
726		if (create) {
727			log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
728			    ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
729			/* xxx more logs... kazu */
730		}
731		return(0);
732	}
733	return(rt);
734}
735
736/*
737 * Detect if a given IPv6 address identifies a neighbor on a given link.
738 * XXX: should take care of the destination of a p2p link?
739 */
740int
741nd6_is_addr_neighbor(addr, ifp)
742	struct sockaddr_in6 *addr;
743	struct ifnet *ifp;
744{
745	register struct ifaddr *ifa;
746	int i;
747
748#define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
749#define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
750
751	/*
752	 * A link-local address is always a neighbor.
753	 * XXX: we should use the sin6_scope_id field rather than the embedded
754	 * interface index.
755	 */
756	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
757	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
758		return(1);
759
760	/*
761	 * If the address matches one of our addresses,
762	 * it should be a neighbor.
763	 */
764	for (ifa = ifp->if_addrlist.tqh_first;
765	     ifa;
766	     ifa = ifa->ifa_list.tqe_next)
767	{
768		if (ifa->ifa_addr->sa_family != AF_INET6)
769			next: continue;
770
771		for (i = 0; i < 4; i++) {
772			if ((IFADDR6(ifa).s6_addr32[i] ^
773			     addr->sin6_addr.s6_addr32[i]) &
774			    IFMASK6(ifa).s6_addr32[i])
775				goto next;
776		}
777		return(1);
778	}
779
780	/*
781	 * Even if the address matches none of our addresses, it might be
782	 * in the neighbor cache.
783	 */
784	if (nd6_lookup(&addr->sin6_addr, 0, ifp))
785		return(1);
786
787	return(0);
788#undef IFADDR6
789#undef IFMASK6
790}
791
792/*
793 * Free an nd6 llinfo entry.
794 */
795void
796nd6_free(rt)
797	struct rtentry *rt;
798{
799	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
800	struct sockaddr_dl *sdl;
801	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
802	struct nd_defrouter *dr;
803
804	/*
805	 * Clear all destination cache entries for the neighbor.
806	 * XXX: is it better to restrict this to hosts?
807	 */
808	pfctlinput(PRC_HOSTDEAD, rt_key(rt));
809
810	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
811		int s;
812		s = splnet();
813		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
814				      rt->rt_ifp);
815		if (ln->ln_router || dr) {
816			/*
817			 * rt6_flush must be called whether or not the neighbor
818			 * is in the Default Router List.
819			 * See a corresponding comment in nd6_na_input().
820			 */
821			rt6_flush(&in6, rt->rt_ifp);
822		}
823
824		if (dr) {
825			/*
826			 * Unreachablity of a router might affect the default
827			 * router selection and on-link detection of advertised
828			 * prefixes.
829			 */
830
831			/*
832			 * Temporarily fake the state to choose a new default
833			 * router and to perform on-link determination of
834			 * prefixes coreectly.
835			 * Below the state will be set correctly,
836			 * or the entry itself will be deleted.
837			 */
838			ln->ln_state = ND6_LLINFO_INCOMPLETE;
839
840			if (dr == TAILQ_FIRST(&nd_defrouter)) {
841				/*
842				 * It is used as the current default router,
843				 * so we have to move it to the end of the
844				 * list and choose a new one.
845				 * XXX: it is not very efficient if this is
846				 *      the only router.
847				 */
848				TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
849				TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
850
851				defrouter_select();
852			}
853			pfxlist_onlink_check();
854		}
855		splx(s);
856	}
857
858	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
859	    sdl->sdl_family == AF_LINK) {
860		sdl->sdl_alen = 0;
861		ln->ln_state = ND6_LLINFO_WAITDELETE;
862		ln->ln_asked = 0;
863		rt->rt_flags &= ~RTF_REJECT;
864		return;
865	}
866
867	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
868		  rt_mask(rt), 0, (struct rtentry **)0);
869}
870
871/*
872 * Upper-layer reachability hint for Neighbor Unreachability Detection.
873 *
874 * XXX cost-effective metods?
875 */
876void
877nd6_nud_hint(rt, dst6, force)
878	struct rtentry *rt;
879	struct in6_addr *dst6;
880	int force;
881{
882	struct llinfo_nd6 *ln;
883
884	/*
885	 * If the caller specified "rt", use that.  Otherwise, resolve the
886	 * routing table by supplied "dst6".
887	 */
888	if (!rt) {
889		if (!dst6)
890			return;
891		if (!(rt = nd6_lookup(dst6, 0, NULL)))
892			return;
893	}
894
895	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
896	    (rt->rt_flags & RTF_LLINFO) == 0 ||
897	    !rt->rt_llinfo || !rt->rt_gateway ||
898	    rt->rt_gateway->sa_family != AF_LINK) {
899		/* This is not a host route. */
900		return;
901	}
902
903	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
904	if (ln->ln_state < ND6_LLINFO_REACHABLE)
905		return;
906
907	/*
908	 * if we get upper-layer reachability confirmation many times,
909	 * it is possible we have false information.
910	 */
911	if (!force) {
912		ln->ln_byhint++;
913		if (ln->ln_byhint > nd6_maxnudhint)
914			return;
915	}
916
917	ln->ln_state = ND6_LLINFO_REACHABLE;
918	if (ln->ln_expire)
919		ln->ln_expire = time_second +
920			nd_ifinfo[rt->rt_ifp->if_index].reachable;
921}
922
923#ifdef OLDIP6OUTPUT
924/*
925 * Resolve an IP6 address into an ethernet address. If success,
926 * desten is filled in. If there is no entry in ndptab,
927 * set one up and multicast a solicitation for the IP6 address.
928 * Hold onto this mbuf and resend it once the address
929 * is finally resolved. A return value of 1 indicates
930 * that desten has been filled in and the packet should be sent
931 * normally; a 0 return indicates that the packet has been
932 * taken over here, either now or for later transmission.
933 */
934int
935nd6_resolve(ifp, rt, m, dst, desten)
936	struct ifnet *ifp;
937	struct rtentry *rt;
938	struct mbuf *m;
939	struct sockaddr *dst;
940	u_char *desten;
941{
942	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
943	struct sockaddr_dl *sdl;
944
945	if (m->m_flags & M_MCAST) {
946		switch (ifp->if_type) {
947		case IFT_ETHER:
948		case IFT_FDDI:
949			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
950						 desten);
951			return(1);
952			break;
953		case IFT_ARCNET:
954			*desten = 0;
955			return(1);
956			break;
957		default:
958			return(0);
959		}
960	}
961	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
962		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
963	else {
964		if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
965			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
966	}
967	if (!ln || !rt) {
968		log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
969			ip6_sprintf(&(SIN6(dst)->sin6_addr)));
970		m_freem(m);
971		return(0);
972	}
973	sdl = SDL(rt->rt_gateway);
974	/*
975	 * Ckeck the address family and length is valid, the address
976	 * is resolved; otherwise, try to resolve.
977	 */
978	if (ln->ln_state >= ND6_LLINFO_REACHABLE
979	   && sdl->sdl_family == AF_LINK
980	   && sdl->sdl_alen != 0) {
981		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
982		if (ln->ln_state == ND6_LLINFO_STALE) {
983			ln->ln_asked = 0;
984			ln->ln_state = ND6_LLINFO_DELAY;
985			ln->ln_expire = time_second + nd6_delay;
986		}
987		return(1);
988	}
989	/*
990	 * There is an ndp entry, but no ethernet address
991	 * response yet. Replace the held mbuf with this
992	 * latest one.
993	 *
994	 * XXX Does the code conform to rate-limiting rule?
995	 * (RFC 2461 7.2.2)
996	 */
997	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
998	    ln->ln_state == ND6_LLINFO_NOSTATE)
999		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1000	if (ln->ln_hold)
1001		m_freem(ln->ln_hold);
1002	ln->ln_hold = m;
1003	if (ln->ln_expire) {
1004		rt->rt_flags &= ~RTF_REJECT;
1005		if (ln->ln_asked < nd6_mmaxtries &&
1006		    ln->ln_expire < time_second) {
1007			ln->ln_asked++;
1008			ln->ln_expire = time_second +
1009				nd_ifinfo[ifp->if_index].retrans / 1000;
1010			nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
1011				ln, 0);
1012		}
1013	}
1014	return(0);
1015}
1016#endif /* OLDIP6OUTPUT */
1017
1018void
1019nd6_rtrequest(req, rt, sa)
1020	int	req;
1021	struct rtentry *rt;
1022	struct sockaddr *sa; /* xxx unused */
1023{
1024	struct sockaddr *gate = rt->rt_gateway;
1025	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1026	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1027	struct ifnet *ifp = rt->rt_ifp;
1028	struct ifaddr *ifa;
1029
1030	if (rt->rt_flags & RTF_GATEWAY)
1031		return;
1032
1033	switch (req) {
1034	case RTM_ADD:
1035		/*
1036		 * There is no backward compatibility :)
1037		 *
1038		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1039		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1040		 *	   rt->rt_flags |= RTF_CLONING;
1041		 */
1042		if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1043			/*
1044			 * Case 1: This route should come from
1045			 * a route to interface. RTF_LLINFO flag is set
1046			 * for a host route whose destination should be
1047			 * treated as on-link.
1048			 */
1049			rt_setgate(rt, rt_key(rt),
1050				   (struct sockaddr *)&null_sdl);
1051			gate = rt->rt_gateway;
1052			SDL(gate)->sdl_type = ifp->if_type;
1053			SDL(gate)->sdl_index = ifp->if_index;
1054			if (ln)
1055				ln->ln_expire = time_second;
1056#if 1
1057			if (ln && ln->ln_expire == 0) {
1058				/* cludge for desktops */
1059#if 0
1060				printf("nd6_request: time.tv_sec is zero; "
1061				       "treat it as 1\n");
1062#endif
1063				ln->ln_expire = 1;
1064			}
1065#endif
1066			if (rt->rt_flags & RTF_CLONING)
1067				break;
1068		}
1069		/*
1070		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1071		 * We don't do that here since llinfo is not ready yet.
1072		 *
1073		 * There are also couple of other things to be discussed:
1074		 * - unsolicited NA code needs improvement beforehand
1075		 * - RFC2461 says we MAY send multicast unsolicited NA
1076		 *   (7.2.6 paragraph 4), however, it also says that we
1077		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1078		 *   we don't have anything like it right now.
1079		 *   note that the mechanism need a mutual agreement
1080		 *   between proxies, which means that we need to implement
1081		 *   a new protocol, or new kludge.
1082		 * - from RFC2461 6.2.4, host MUST NOT send unsolicited NA.
1083		 *   we need to check ip6forwarding before sending it.
1084		 *   (or should we allow proxy ND configuration only for
1085		 *   routers?  there's no mention about proxy ND from hosts)
1086		 */
1087#if 0
1088		/* XXX it does not work */
1089		if (rt->rt_flags & RTF_ANNOUNCE)
1090			nd6_na_output(ifp,
1091			      &SIN6(rt_key(rt))->sin6_addr,
1092			      &SIN6(rt_key(rt))->sin6_addr,
1093			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1094			      1, NULL);
1095#endif
1096		/* FALLTHROUGH */
1097	case RTM_RESOLVE:
1098		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
1099			/*
1100			 * Address resolution isn't necessary for a point to
1101			 * point link, so we can skip this test for a p2p link.
1102			 */
1103			if (gate->sa_family != AF_LINK ||
1104			    gate->sa_len < sizeof(null_sdl)) {
1105				log(LOG_DEBUG,
1106				    "nd6_rtrequest: bad gateway value\n");
1107				break;
1108			}
1109			SDL(gate)->sdl_type = ifp->if_type;
1110			SDL(gate)->sdl_index = ifp->if_index;
1111		}
1112		if (ln != NULL)
1113			break;	/* This happens on a route change */
1114		/*
1115		 * Case 2: This route may come from cloning, or a manual route
1116		 * add with a LL address.
1117		 */
1118		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1119		rt->rt_llinfo = (caddr_t)ln;
1120		if (!ln) {
1121			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1122			break;
1123		}
1124		nd6_inuse++;
1125		nd6_allocated++;
1126		Bzero(ln, sizeof(*ln));
1127		ln->ln_rt = rt;
1128		/* this is required for "ndp" command. - shin */
1129		if (req == RTM_ADD) {
1130		        /*
1131			 * gate should have some valid AF_LINK entry,
1132			 * and ln->ln_expire should have some lifetime
1133			 * which is specified by ndp command.
1134			 */
1135			ln->ln_state = ND6_LLINFO_REACHABLE;
1136			ln->ln_byhint = 0;
1137		} else {
1138		        /*
1139			 * When req == RTM_RESOLVE, rt is created and
1140			 * initialized in rtrequest(), so rt_expire is 0.
1141			 */
1142			ln->ln_state = ND6_LLINFO_NOSTATE;
1143			ln->ln_expire = time_second;
1144		}
1145		rt->rt_flags |= RTF_LLINFO;
1146		ln->ln_next = llinfo_nd6.ln_next;
1147		llinfo_nd6.ln_next = ln;
1148		ln->ln_prev = &llinfo_nd6;
1149		ln->ln_next->ln_prev = ln;
1150
1151		/*
1152		 * check if rt_key(rt) is one of my address assigned
1153		 * to the interface.
1154		 */
1155		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1156					  &SIN6(rt_key(rt))->sin6_addr);
1157		if (ifa) {
1158			caddr_t macp = nd6_ifptomac(ifp);
1159			ln->ln_expire = 0;
1160			ln->ln_state = ND6_LLINFO_REACHABLE;
1161			ln->ln_byhint = 0;
1162			if (macp) {
1163				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1164				SDL(gate)->sdl_alen = ifp->if_addrlen;
1165			}
1166			if (nd6_useloopback) {
1167				rt->rt_ifp = &loif[0];	/*XXX*/
1168				/*
1169				 * Make sure rt_ifa be equal to the ifaddr
1170				 * corresponding to the address.
1171				 * We need this because when we refer
1172				 * rt_ifa->ia6_flags in ip6_input, we assume
1173				 * that the rt_ifa points to the address instead
1174				 * of the loopback address.
1175				 */
1176				if (ifa != rt->rt_ifa) {
1177					IFAFREE(rt->rt_ifa);
1178					ifa->ifa_refcnt++;
1179					rt->rt_ifa = ifa;
1180				}
1181			}
1182		} else if (rt->rt_flags & RTF_ANNOUNCE) {
1183			ln->ln_expire = 0;
1184			ln->ln_state = ND6_LLINFO_REACHABLE;
1185			ln->ln_byhint = 0;
1186
1187			/* join solicited node multicast for proxy ND */
1188			if (ifp->if_flags & IFF_MULTICAST) {
1189				struct in6_addr llsol;
1190				int error;
1191
1192				llsol = SIN6(rt_key(rt))->sin6_addr;
1193				llsol.s6_addr16[0] = htons(0xff02);
1194				llsol.s6_addr16[1] = htons(ifp->if_index);
1195				llsol.s6_addr32[1] = 0;
1196				llsol.s6_addr32[2] = htonl(1);
1197				llsol.s6_addr8[12] = 0xff;
1198
1199				(void)in6_addmulti(&llsol, ifp, &error);
1200				if (error)
1201					printf(
1202"nd6_rtrequest: could not join solicited node multicast (errno=%d)\n", error);
1203			}
1204		}
1205		break;
1206
1207	case RTM_DELETE:
1208		if (!ln)
1209			break;
1210		/* leave from solicited node multicast for proxy ND */
1211		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1212		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1213			struct in6_addr llsol;
1214			struct in6_multi *in6m;
1215
1216			llsol = SIN6(rt_key(rt))->sin6_addr;
1217			llsol.s6_addr16[0] = htons(0xff02);
1218			llsol.s6_addr16[1] = htons(ifp->if_index);
1219			llsol.s6_addr32[1] = 0;
1220			llsol.s6_addr32[2] = htonl(1);
1221			llsol.s6_addr8[12] = 0xff;
1222
1223			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1224			if (in6m)
1225				in6_delmulti(in6m);
1226		}
1227		nd6_inuse--;
1228		ln->ln_next->ln_prev = ln->ln_prev;
1229		ln->ln_prev->ln_next = ln->ln_next;
1230		ln->ln_prev = NULL;
1231		rt->rt_llinfo = 0;
1232		rt->rt_flags &= ~RTF_LLINFO;
1233		if (ln->ln_hold)
1234			m_freem(ln->ln_hold);
1235		Free((caddr_t)ln);
1236	}
1237}
1238
1239void
1240nd6_p2p_rtrequest(req, rt, sa)
1241	int	req;
1242	struct rtentry *rt;
1243	struct sockaddr *sa; /* xxx unused */
1244{
1245	struct sockaddr *gate = rt->rt_gateway;
1246	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1247	struct ifnet *ifp = rt->rt_ifp;
1248	struct ifaddr *ifa;
1249
1250	if (rt->rt_flags & RTF_GATEWAY)
1251		return;
1252
1253	switch (req) {
1254	case RTM_ADD:
1255		/*
1256		 * There is no backward compatibility :)
1257		 *
1258		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1259		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1260		 *	   rt->rt_flags |= RTF_CLONING;
1261		 */
1262		if (rt->rt_flags & RTF_CLONING) {
1263			/*
1264			 * Case 1: This route should come from
1265			 * a route to interface.
1266			 */
1267			rt_setgate(rt, rt_key(rt),
1268				   (struct sockaddr *)&null_sdl);
1269			gate = rt->rt_gateway;
1270			SDL(gate)->sdl_type = ifp->if_type;
1271			SDL(gate)->sdl_index = ifp->if_index;
1272			break;
1273		}
1274		/* Announce a new entry if requested. */
1275		if (rt->rt_flags & RTF_ANNOUNCE)
1276			nd6_na_output(ifp,
1277				      &SIN6(rt_key(rt))->sin6_addr,
1278				      &SIN6(rt_key(rt))->sin6_addr,
1279				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1280				      1, NULL);
1281		/* FALLTHROUGH */
1282	case RTM_RESOLVE:
1283		/*
1284		 * check if rt_key(rt) is one of my address assigned
1285		 * to the interface.
1286		 */
1287 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1288					  &SIN6(rt_key(rt))->sin6_addr);
1289		if (ifa) {
1290			if (nd6_useloopback) {
1291				rt->rt_ifp = &loif[0];	/*XXX*/
1292			}
1293		}
1294		break;
1295	}
1296}
1297
1298int
1299nd6_ioctl(cmd, data, ifp)
1300	u_long cmd;
1301	caddr_t	data;
1302	struct ifnet *ifp;
1303{
1304	struct in6_drlist *drl = (struct in6_drlist *)data;
1305	struct in6_prlist *prl = (struct in6_prlist *)data;
1306	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1307	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1308	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1309	struct nd_defrouter *dr, any;
1310	struct nd_prefix *pr;
1311	struct rtentry *rt;
1312	int i = 0, error = 0;
1313	int s;
1314
1315	switch (cmd) {
1316	case SIOCGDRLST_IN6:
1317		bzero(drl, sizeof(*drl));
1318		s = splnet();
1319		dr = TAILQ_FIRST(&nd_defrouter);
1320		while (dr && i < DRLSTSIZ) {
1321			drl->defrouter[i].rtaddr = dr->rtaddr;
1322			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1323				/* XXX: need to this hack for KAME stack */
1324				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1325			} else
1326				log(LOG_ERR,
1327				    "default router list contains a "
1328				    "non-linklocal address(%s)\n",
1329				    ip6_sprintf(&drl->defrouter[i].rtaddr));
1330
1331			drl->defrouter[i].flags = dr->flags;
1332			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1333			drl->defrouter[i].expire = dr->expire;
1334			drl->defrouter[i].if_index = dr->ifp->if_index;
1335			i++;
1336			dr = TAILQ_NEXT(dr, dr_entry);
1337		}
1338		splx(s);
1339		break;
1340	case SIOCGPRLST_IN6:
1341		/*
1342		 * XXX meaning of fields, especialy "raflags", is very
1343		 * differnet between RA prefix list and RR/static prefix list.
1344		 * how about separating ioctls into two?
1345		 */
1346		bzero(prl, sizeof(*prl));
1347		s = splnet();
1348		pr = nd_prefix.lh_first;
1349		while (pr && i < PRLSTSIZ) {
1350			struct nd_pfxrouter *pfr;
1351			int j;
1352
1353			prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1354			prl->prefix[i].raflags = pr->ndpr_raf;
1355			prl->prefix[i].prefixlen = pr->ndpr_plen;
1356			prl->prefix[i].vltime = pr->ndpr_vltime;
1357			prl->prefix[i].pltime = pr->ndpr_pltime;
1358			prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1359			prl->prefix[i].expire = pr->ndpr_expire;
1360
1361			pfr = pr->ndpr_advrtrs.lh_first;
1362			j = 0;
1363			while(pfr) {
1364				if (j < DRLSTSIZ) {
1365#define RTRADDR prl->prefix[i].advrtr[j]
1366					RTRADDR = pfr->router->rtaddr;
1367					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1368						/* XXX: hack for KAME */
1369						RTRADDR.s6_addr16[1] = 0;
1370					} else
1371						log(LOG_ERR,
1372						    "a router(%s) advertises "
1373						    "a prefix with "
1374						    "non-link local address\n",
1375						    ip6_sprintf(&RTRADDR));
1376#undef RTRADDR
1377				}
1378				j++;
1379				pfr = pfr->pfr_next;
1380			}
1381			prl->prefix[i].advrtrs = j;
1382			prl->prefix[i].origin = PR_ORIG_RA;
1383
1384			i++;
1385			pr = pr->ndpr_next;
1386		}
1387	      {
1388		struct rr_prefix *rpp;
1389
1390		for (rpp = LIST_FIRST(&rr_prefix); rpp;
1391		     rpp = LIST_NEXT(rpp, rp_entry)) {
1392			if (i >= PRLSTSIZ)
1393				break;
1394			prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
1395			prl->prefix[i].raflags = rpp->rp_raf;
1396			prl->prefix[i].prefixlen = rpp->rp_plen;
1397			prl->prefix[i].vltime = rpp->rp_vltime;
1398			prl->prefix[i].pltime = rpp->rp_pltime;
1399			prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1400			prl->prefix[i].expire = rpp->rp_expire;
1401			prl->prefix[i].advrtrs = 0;
1402			prl->prefix[i].origin = rpp->rp_origin;
1403			i++;
1404		}
1405	      }
1406		splx(s);
1407
1408		break;
1409	case SIOCGIFINFO_IN6:
1410		if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1411			error = EINVAL;
1412			break;
1413		}
1414		ndi->ndi = nd_ifinfo[ifp->if_index];
1415		break;
1416	case SIOCSIFINFO_FLAGS:
1417		/* XXX: almost all other fields of ndi->ndi is unused */
1418		if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1419			error = EINVAL;
1420			break;
1421		}
1422		nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
1423		break;
1424	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1425		/* flush default router list */
1426		/*
1427		 * xxx sumikawa: should not delete route if default
1428		 * route equals to the top of default router list
1429		 */
1430		bzero(&any, sizeof(any));
1431		defrouter_delreq(&any, 0);
1432		defrouter_select();
1433		/* xxx sumikawa: flush prefix list */
1434		break;
1435	case SIOCSPFXFLUSH_IN6:
1436	    {
1437		/* flush all the prefix advertised by routers */
1438		struct nd_prefix *pr, *next;
1439
1440		s = splnet();
1441		for (pr = nd_prefix.lh_first; pr; pr = next) {
1442			next = pr->ndpr_next;
1443			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1444				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1445			prelist_remove(pr);
1446		}
1447		splx(s);
1448		break;
1449	    }
1450	case SIOCSRTRFLUSH_IN6:
1451	    {
1452		/* flush all the default routers */
1453		struct nd_defrouter *dr, *next;
1454
1455		s = splnet();
1456		if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1457			/*
1458			 * The first entry of the list may be stored in
1459			 * the routing table, so we'll delete it later.
1460			 */
1461			for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1462				next = TAILQ_NEXT(dr, dr_entry);
1463				defrtrlist_del(dr);
1464			}
1465			defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1466		}
1467		splx(s);
1468		break;
1469	    }
1470	case SIOCGNBRINFO_IN6:
1471	    {
1472		struct llinfo_nd6 *ln;
1473		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1474
1475		/*
1476		 * XXX: KAME specific hack for scoped addresses
1477		 *      XXXX: for other scopes than link-local?
1478		 */
1479		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1480		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1481			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1482
1483			if (*idp == 0)
1484				*idp = htons(ifp->if_index);
1485		}
1486
1487		s = splnet();
1488		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1489			error = EINVAL;
1490			splx(s);
1491			break;
1492		}
1493		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1494		nbi->state = ln->ln_state;
1495		nbi->asked = ln->ln_asked;
1496		nbi->isrouter = ln->ln_router;
1497		nbi->expire = ln->ln_expire;
1498		splx(s);
1499
1500		break;
1501	    }
1502	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1503		ndif->ifindex = nd6_defifindex;
1504		break;
1505	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1506		return(nd6_setdefaultiface(ndif->ifindex));
1507		break;
1508	}
1509	return(error);
1510}
1511
1512/*
1513 * Create neighbor cache entry and cache link-layer address,
1514 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1515 */
1516struct rtentry *
1517nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1518	struct ifnet *ifp;
1519	struct in6_addr *from;
1520	char *lladdr;
1521	int lladdrlen;
1522	int type;	/* ICMP6 type */
1523	int code;	/* type dependent information */
1524{
1525	struct rtentry *rt = NULL;
1526	struct llinfo_nd6 *ln = NULL;
1527	int is_newentry;
1528	struct sockaddr_dl *sdl = NULL;
1529	int do_update;
1530	int olladdr;
1531	int llchange;
1532	int newstate = 0;
1533
1534	if (!ifp)
1535		panic("ifp == NULL in nd6_cache_lladdr");
1536	if (!from)
1537		panic("from == NULL in nd6_cache_lladdr");
1538
1539	/* nothing must be updated for unspecified address */
1540	if (IN6_IS_ADDR_UNSPECIFIED(from))
1541		return NULL;
1542
1543	/*
1544	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1545	 * the caller.
1546	 *
1547	 * XXX If the link does not have link-layer adderss, what should
1548	 * we do? (ifp->if_addrlen == 0)
1549	 * Spec says nothing in sections for RA, RS and NA.  There's small
1550	 * description on it in NS section (RFC 2461 7.2.3).
1551	 */
1552
1553	rt = nd6_lookup(from, 0, ifp);
1554	if (!rt) {
1555#if 0
1556		/* nothing must be done if there's no lladdr */
1557		if (!lladdr || !lladdrlen)
1558			return NULL;
1559#endif
1560
1561		rt = nd6_lookup(from, 1, ifp);
1562		is_newentry = 1;
1563	} else
1564		is_newentry = 0;
1565
1566	if (!rt)
1567		return NULL;
1568	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1569fail:
1570		nd6_free(rt);
1571		return NULL;
1572	}
1573	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1574	if (!ln)
1575		goto fail;
1576	if (!rt->rt_gateway)
1577		goto fail;
1578	if (rt->rt_gateway->sa_family != AF_LINK)
1579		goto fail;
1580	sdl = SDL(rt->rt_gateway);
1581
1582	olladdr = (sdl->sdl_alen) ? 1 : 0;
1583	if (olladdr && lladdr) {
1584		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1585			llchange = 1;
1586		else
1587			llchange = 0;
1588	} else
1589		llchange = 0;
1590
1591	/*
1592	 * newentry olladdr  lladdr  llchange	(*=record)
1593	 *	0	n	n	--	(1)
1594	 *	0	y	n	--	(2)
1595	 *	0	n	y	--	(3) * STALE
1596	 *	0	y	y	n	(4) *
1597	 *	0	y	y	y	(5) * STALE
1598	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1599	 *	1	--	y	--	(7) * STALE
1600	 */
1601
1602	if (lladdr) {		/*(3-5) and (7)*/
1603		/*
1604		 * Record source link-layer address
1605		 * XXX is it dependent to ifp->if_type?
1606		 */
1607		sdl->sdl_alen = ifp->if_addrlen;
1608		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1609	}
1610
1611	if (!is_newentry) {
1612		if ((!olladdr && lladdr)		/*(3)*/
1613		 || (olladdr && lladdr && llchange)) {	/*(5)*/
1614			do_update = 1;
1615			newstate = ND6_LLINFO_STALE;
1616		} else					/*(1-2,4)*/
1617			do_update = 0;
1618	} else {
1619		do_update = 1;
1620		if (!lladdr)				/*(6)*/
1621			newstate = ND6_LLINFO_NOSTATE;
1622		else					/*(7)*/
1623			newstate = ND6_LLINFO_STALE;
1624	}
1625
1626	if (do_update) {
1627		/*
1628		 * Update the state of the neighbor cache.
1629		 */
1630		ln->ln_state = newstate;
1631
1632		if (ln->ln_state == ND6_LLINFO_STALE) {
1633			rt->rt_flags &= ~RTF_REJECT;
1634			if (ln->ln_hold) {
1635#ifdef OLDIP6OUTPUT
1636				(*ifp->if_output)(ifp, ln->ln_hold,
1637						  rt_key(rt), rt);
1638#else
1639				/*
1640				 * we assume ifp is not a p2p here, so just
1641				 * set the 2nd argument as the 1st one.
1642				 */
1643				nd6_output(ifp, ifp, ln->ln_hold,
1644					   (struct sockaddr_in6 *)rt_key(rt),
1645					   rt);
1646#endif
1647				ln->ln_hold = 0;
1648			}
1649		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1650			/* probe right away */
1651			ln->ln_expire = time_second;
1652		}
1653	}
1654
1655	/*
1656	 * ICMP6 type dependent behavior.
1657	 *
1658	 * NS: clear IsRouter if new entry
1659	 * RS: clear IsRouter
1660	 * RA: set IsRouter if there's lladdr
1661	 * redir: clear IsRouter if new entry
1662	 *
1663	 * RA case, (1):
1664	 * The spec says that we must set IsRouter in the following cases:
1665	 * - If lladdr exist, set IsRouter.  This means (1-5).
1666	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1667	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1668	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1669	 * neighbor cache, this is similar to (6).
1670	 * This case is rare but we figured that we MUST NOT set IsRouter.
1671	 *
1672	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1673	 *							D R
1674	 *	0	n	n	--	(1)	c   ?     s
1675	 *	0	y	n	--	(2)	c   s     s
1676	 *	0	n	y	--	(3)	c   s     s
1677	 *	0	y	y	n	(4)	c   s     s
1678	 *	0	y	y	y	(5)	c   s     s
1679	 *	1	--	n	--	(6) c	c 	c s
1680	 *	1	--	y	--	(7) c	c   s	c s
1681	 *
1682	 *					(c=clear s=set)
1683	 */
1684	switch (type & 0xff) {
1685	case ND_NEIGHBOR_SOLICIT:
1686		/*
1687		 * New entry must have is_router flag cleared.
1688		 */
1689		if (is_newentry)	/*(6-7)*/
1690			ln->ln_router = 0;
1691		break;
1692	case ND_REDIRECT:
1693		/*
1694		 * If the icmp is a redirect to a better router, always set the
1695		 * is_router flag. Otherwise, if the entry is newly created,
1696		 * clear the flag. [RFC 2461, sec 8.3]
1697		 */
1698		if (code == ND_REDIRECT_ROUTER)
1699			ln->ln_router = 1;
1700		else if (is_newentry) /*(6-7)*/
1701			ln->ln_router = 0;
1702		break;
1703	case ND_ROUTER_SOLICIT:
1704		/*
1705		 * is_router flag must always be cleared.
1706		 */
1707		ln->ln_router = 0;
1708		break;
1709	case ND_ROUTER_ADVERT:
1710		/*
1711		 * Mark an entry with lladdr as a router.
1712		 */
1713		if ((!is_newentry && (olladdr || lladdr))	/*(2-5)*/
1714		 || (is_newentry && lladdr)) {			/*(7)*/
1715			ln->ln_router = 1;
1716		}
1717		break;
1718	}
1719
1720	return rt;
1721}
1722
1723static void
1724nd6_slowtimo(ignored_arg)
1725    void *ignored_arg;
1726{
1727	int s = splnet();
1728	register int i;
1729	register struct nd_ifinfo *nd6if;
1730
1731	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
1732	for (i = 1; i < if_index + 1; i++) {
1733		if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
1734			continue;
1735		nd6if = &nd_ifinfo[i];
1736		if (nd6if->basereachable && /* already initialized */
1737		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1738			/*
1739			 * Since reachable time rarely changes by router
1740			 * advertisements, we SHOULD insure that a new random
1741			 * value gets recomputed at least once every few hours.
1742			 * (RFC 2461, 6.3.4)
1743			 */
1744			nd6if->recalctm = nd6_recalc_reachtm_interval;
1745			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1746		}
1747	}
1748	splx(s);
1749}
1750
1751#define senderr(e) { error = (e); goto bad;}
1752int
1753nd6_output(ifp, origifp, m0, dst, rt0)
1754	register struct ifnet *ifp;
1755	struct ifnet *origifp;
1756	struct mbuf *m0;
1757	struct sockaddr_in6 *dst;
1758	struct rtentry *rt0;
1759{
1760	register struct mbuf *m = m0;
1761	register struct rtentry *rt = rt0;
1762	struct sockaddr_in6 *gw6 = NULL;
1763	struct llinfo_nd6 *ln = NULL;
1764	int error = 0;
1765
1766	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1767		goto sendpkt;
1768
1769	/*
1770	 * XXX: we currently do not make neighbor cache on any interface
1771	 * other than ARCnet, Ethernet, FDDI and GIF.
1772	 *
1773	 * draft-ietf-ngtrans-mech-06.txt says:
1774	 * - unidirectional tunnels needs no ND
1775	 */
1776	switch (ifp->if_type) {
1777	case IFT_ARCNET:
1778	case IFT_ETHER:
1779	case IFT_FDDI:
1780	case IFT_GIF:		/* XXX need more cases? */
1781		break;
1782	default:
1783		goto sendpkt;
1784	}
1785
1786	/*
1787	 * next hop determination. This routine is derived from ether_outpout.
1788	 */
1789	if (rt) {
1790		if ((rt->rt_flags & RTF_UP) == 0) {
1791			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
1792				NULL)
1793			{
1794				rt->rt_refcnt--;
1795				if (rt->rt_ifp != ifp) {
1796					/* XXX: loop care? */
1797					return nd6_output(ifp, origifp, m0,
1798							  dst, rt);
1799				}
1800			} else
1801				senderr(EHOSTUNREACH);
1802		}
1803
1804		if (rt->rt_flags & RTF_GATEWAY) {
1805			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1806
1807			/*
1808			 * We skip link-layer address resolution and NUD
1809			 * if the gateway is not a neighbor from ND point
1810			 * of view, regardless the value of the value of
1811			 * nd_ifinfo.flags.
1812			 * The second condition is a bit tricky: we skip
1813			 * if the gateway is our own address, which is
1814			 * sometimes used to install a route to a p2p link.
1815			 */
1816			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1817			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1818				if (rt->rt_flags & RTF_REJECT)
1819					senderr(EHOSTDOWN);
1820
1821				/*
1822				 * We allow this kind of tricky route only
1823				 * when the outgoing interface is p2p.
1824				 * XXX: we may need a more generic rule here.
1825				 */
1826				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1827					senderr(EHOSTUNREACH);
1828
1829				goto sendpkt;
1830			}
1831
1832			if (rt->rt_gwroute == 0)
1833				goto lookup;
1834			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1835				rtfree(rt); rt = rt0;
1836			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1837				if ((rt = rt->rt_gwroute) == 0)
1838					senderr(EHOSTUNREACH);
1839			}
1840		}
1841		if (rt->rt_flags & RTF_REJECT)
1842			senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1843	}
1844
1845	/*
1846	 * Address resolution or Neighbor Unreachability Detection
1847	 * for the next hop.
1848	 * At this point, the destination of the packet must be a unicast
1849	 * or an anycast address(i.e. not a multicast).
1850	 */
1851
1852	/* Look up the neighbor cache for the nexthop */
1853	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1854		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1855	else {
1856		/*
1857		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1858		 * the condition below is not very efficient. But we believe
1859		 * it is tolerable, because this should be a rare case.
1860		 */
1861		if (nd6_is_addr_neighbor(dst, ifp) &&
1862		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1863			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1864	}
1865	if (!ln || !rt) {
1866		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1867		    !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
1868			log(LOG_DEBUG,
1869			    "nd6_output: can't allocate llinfo for %s "
1870			    "(ln=%p, rt=%p)\n",
1871			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1872			senderr(EIO);	/* XXX: good error? */
1873		}
1874
1875		goto sendpkt;	/* send anyway */
1876	}
1877
1878	/* We don't have to do link-layer address resolution on a p2p link. */
1879	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1880	    ln->ln_state < ND6_LLINFO_REACHABLE)
1881		ln->ln_state = ND6_LLINFO_STALE;
1882
1883	/*
1884	 * The first time we send a packet to a neighbor whose entry is
1885	 * STALE, we have to change the state to DELAY and a sets a timer to
1886	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1887	 * neighbor unreachability detection on expiration.
1888	 * (RFC 2461 7.3.3)
1889	 */
1890	if (ln->ln_state == ND6_LLINFO_STALE) {
1891		ln->ln_asked = 0;
1892		ln->ln_state = ND6_LLINFO_DELAY;
1893		ln->ln_expire = time_second + nd6_delay;
1894	}
1895
1896	/*
1897	 * If the neighbor cache entry has a state other than INCOMPLETE
1898	 * (i.e. its link-layer address is already reloved), just
1899	 * send the packet.
1900	 */
1901	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1902		goto sendpkt;
1903
1904	/*
1905	 * There is a neighbor cache entry, but no ethernet address
1906	 * response yet. Replace the held mbuf (if any) with this
1907	 * latest one.
1908	 *
1909	 * XXX Does the code conform to rate-limiting rule?
1910	 * (RFC 2461 7.2.2)
1911	 */
1912	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
1913	    ln->ln_state == ND6_LLINFO_NOSTATE)
1914		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1915	if (ln->ln_hold)
1916		m_freem(ln->ln_hold);
1917	ln->ln_hold = m;
1918	if (ln->ln_expire) {
1919		rt->rt_flags &= ~RTF_REJECT;
1920		if (ln->ln_asked < nd6_mmaxtries &&
1921		    ln->ln_expire < time_second) {
1922			ln->ln_asked++;
1923			ln->ln_expire = time_second +
1924				nd_ifinfo[ifp->if_index].retrans / 1000;
1925			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1926		}
1927	}
1928	return(0);
1929
1930  sendpkt:
1931
1932#ifdef FAKE_LOOPBACK_IF
1933	if (ifp->if_flags & IFF_LOOPBACK) {
1934		return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1935					 rt));
1936	}
1937#endif
1938	return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1939
1940  bad:
1941	if (m)
1942		m_freem(m);
1943	return (error);
1944}
1945#undef senderr
1946
1947int
1948nd6_storelladdr(ifp, rt, m, dst, desten)
1949	struct ifnet *ifp;
1950	struct rtentry *rt;
1951	struct mbuf *m;
1952	struct sockaddr *dst;
1953	u_char *desten;
1954{
1955	struct sockaddr_dl *sdl;
1956
1957	if (m->m_flags & M_MCAST) {
1958		switch (ifp->if_type) {
1959		case IFT_ETHER:
1960		case IFT_FDDI:
1961			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1962						 desten);
1963			return(1);
1964			break;
1965		case IFT_ARCNET:
1966			*desten = 0;
1967			return(1);
1968		default:
1969			return(0);
1970		}
1971	}
1972
1973	if (rt == NULL ||
1974	    rt->rt_gateway->sa_family != AF_LINK) {
1975		printf("nd6_storelladdr: something odd happens\n");
1976		return(0);
1977	}
1978	sdl = SDL(rt->rt_gateway);
1979	if (sdl->sdl_alen == 0) {
1980		/* this should be impossible, but we bark here for debugging */
1981		printf("nd6_storelladdr: sdl_alen == 0\n");
1982		return(0);
1983	}
1984
1985	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1986	return(1);
1987}
1988