nd6.c revision 54263
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/netinet6/nd6.c 54263 1999-12-07 17:39:16Z shin $
30 */
31
32/*
33 * XXX
34 * KAME 970409 note:
35 * BSD/OS version heavily modifies this code, related to llinfo.
36 * Since we don't have BSD/OS version of net/route.c in our hand,
37 * I left the code mostly as it was in 970310.  -- itojun
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46#include <sys/time.h>
47#include <sys/kernel.h>
48#include <sys/errno.h>
49#include <sys/syslog.h>
50#include <sys/queue.h>
51
52#include <net/if.h>
53#include <net/if_dl.h>
54#include <net/if_types.h>
55#include <net/if_atm.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59#include <netinet/if_ether.h>
60#include <netinet/if_fddi.h>
61#include <netinet6/in6_var.h>
62#include <netinet6/ip6.h>
63#include <netinet6/ip6_var.h>
64#include <netinet6/nd6.h>
65#include <netinet6/in6_prefix.h>
66#include <netinet6/icmp6.h>
67
68#include "loop.h"
69
70#include <net/net_osdep.h>
71
72#define	ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
73#define	ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
74
75#define	SIN6(s) ((struct sockaddr_in6 *)s)
76#define	SDL(s) ((struct sockaddr_dl *)s)
77
78/* timer values */
79int	nd6_prune	= 1;	/* walk list every 1 seconds */
80int	nd6_delay	= 5;	/* delay first probe time 5 second */
81int	nd6_umaxtries	= 3;	/* maximum unicast query */
82int	nd6_mmaxtries	= 3;	/* maximum multicast query */
83int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
84int	nd6_proxyall	= 0;	/* enable Proxy Neighbor Advertisement */
85
86/* for debugging? */
87static int	nd6_inuse, nd6_allocated;
88
89struct	llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
90struct	nd_ifinfo *nd_ifinfo = NULL;
91struct	nd_drhead nd_defrouter = { 0 };
92struct	nd_prhead nd_prefix = { 0 };
93
94int	nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
95static struct	sockaddr_in6 all1_sa;
96
97static void	nd6_slowtimo __P((void *));
98
99void
100nd6_init()
101{
102	static int nd6_init_done = 0;
103	int i;
104
105	if (nd6_init_done) {
106		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
107		return;
108	}
109
110	all1_sa.sin6_family = AF_INET6;
111	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
112	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
113		all1_sa.sin6_addr.s6_addr[i] = 0xff;
114
115	nd6_init_done = 1;
116
117	/* start timer */
118	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
119}
120
121void
122nd6_ifattach(ifp)
123	struct ifnet *ifp;
124{
125	static size_t if_indexlim = 8;
126
127	/*
128	 * We have some arrays that should be indexed by if_index.
129	 * since if_index will grow dynamically, they should grow too.
130	 */
131	if (nd_ifinfo == NULL || if_index >= if_indexlim) {
132		size_t n;
133		caddr_t q;
134
135		while (if_index >= if_indexlim)
136			if_indexlim <<= 1;
137
138		/* grow nd_ifinfo */
139		n = if_indexlim * sizeof(struct nd_ifinfo);
140		q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
141		bzero(q, n);
142		if (nd_ifinfo) {
143			bcopy((caddr_t)nd_ifinfo, q, n/2);
144			free((caddr_t)nd_ifinfo, M_IP6NDP);
145		}
146		nd_ifinfo = (struct nd_ifinfo *)q;
147	}
148
149#define ND nd_ifinfo[ifp->if_index]
150	ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
151	ND.chlim = IPV6_DEFHLIM;
152	ND.basereachable = REACHABLE_TIME;
153	ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
154	ND.retrans = RETRANS_TIMER;
155	ND.receivedra = 0;
156	nd6_setmtu(ifp);
157#undef ND
158}
159
160/*
161 * Reset ND level link MTU. This function is called when the physical MTU
162 * changes, which means we might have to adjust the ND level MTU.
163 */
164void
165nd6_setmtu(ifp)
166	struct ifnet *ifp;
167{
168#define MIN(a,b) ((a) < (b) ? (a) : (b))
169	struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
170	u_long oldmaxmtu = ndi->maxmtu;
171	u_long oldlinkmtu = ndi->linkmtu;
172
173	switch(ifp->if_type) {
174	 case IFT_ARCNET:	/* XXX MTU handling needs more work */
175		 ndi->maxmtu = MIN(60480, ifp->if_mtu);
176		 break;
177	 case IFT_ETHER:
178		 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
179		 break;
180	 case IFT_FDDI:
181		 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
182		 break;
183	 case IFT_ATM:
184		 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
185		 break;
186	 default:
187		 ndi->maxmtu = ifp->if_mtu;
188		 break;
189	}
190
191	if (oldmaxmtu != ndi->maxmtu) {
192		/*
193		 * If the ND level MTU is not set yet, or if the maxmtu
194		 * is reset to a smaller value than the ND level MTU,
195		 * also reset the ND level MTU.
196		 */
197		if (ndi->linkmtu == 0 ||
198		    ndi->maxmtu < ndi->linkmtu) {
199			ndi->linkmtu = ndi->maxmtu;
200			/* also adjust in6_maxmtu if necessary. */
201			if (oldlinkmtu == 0) {
202				/*
203				 * XXX: the case analysis is grotty, but
204				 * it is not efficient to call in6_setmaxmtu()
205				 * here when we are during the initialization
206				 * procedure.
207				 */
208				if (in6_maxmtu < ndi->linkmtu)
209					in6_maxmtu = ndi->linkmtu;
210			} else
211				in6_setmaxmtu();
212		}
213	}
214#undef MIN
215}
216
217void
218nd6_option_init(opt, icmp6len, ndopts)
219	void *opt;
220	int icmp6len;
221	union nd_opts *ndopts;
222{
223	bzero(ndopts, sizeof(*ndopts));
224	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
225	ndopts->nd_opts_last
226		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
227
228	if (icmp6len == 0) {
229		ndopts->nd_opts_done = 1;
230		ndopts->nd_opts_search = NULL;
231	}
232}
233
234/*
235 * Take one ND option.
236 */
237struct nd_opt_hdr *
238nd6_option(ndopts)
239	union nd_opts *ndopts;
240{
241	struct nd_opt_hdr *nd_opt;
242	int olen;
243
244	if (!ndopts)
245		panic("ndopts == NULL in nd6_option\n");
246	if (!ndopts->nd_opts_last)
247		panic("uninitialized ndopts in nd6_option\n");
248	if (!ndopts->nd_opts_search)
249		return NULL;
250	if (ndopts->nd_opts_done)
251		return NULL;
252
253	nd_opt = ndopts->nd_opts_search;
254
255	olen = nd_opt->nd_opt_len << 3;
256	if (olen == 0) {
257		/*
258		 * Message validation requires that all included
259		 * options have a length that is greater than zero.
260		 */
261		bzero(ndopts, sizeof(*ndopts));
262		return NULL;
263	}
264
265	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
266	if (!(ndopts->nd_opts_search < ndopts->nd_opts_last)) {
267		ndopts->nd_opts_done = 1;
268		ndopts->nd_opts_search = NULL;
269	}
270	return nd_opt;
271}
272
273/*
274 * Parse multiple ND options.
275 * This function is much easier to use, for ND routines that do not need
276 * multiple options of the same type.
277 */
278int
279nd6_options(ndopts)
280	union nd_opts *ndopts;
281{
282	struct nd_opt_hdr *nd_opt;
283	int i = 0;
284
285	if (!ndopts)
286		panic("ndopts == NULL in nd6_options\n");
287	if (!ndopts->nd_opts_last)
288		panic("uninitialized ndopts in nd6_options\n");
289	if (!ndopts->nd_opts_search)
290		return 0;
291
292	while (1) {
293		nd_opt = nd6_option(ndopts);
294		if (!nd_opt && !ndopts->nd_opts_last) {
295			/*
296			 * Message validation requires that all included
297			 * options have a length that is greater than zero.
298			 */
299			bzero(ndopts, sizeof(*ndopts));
300			return -1;
301		}
302
303		if (!nd_opt)
304			goto skip1;
305
306		switch (nd_opt->nd_opt_type) {
307		case ND_OPT_SOURCE_LINKADDR:
308		case ND_OPT_TARGET_LINKADDR:
309		case ND_OPT_MTU:
310		case ND_OPT_REDIRECTED_HEADER:
311			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
312				printf("duplicated ND6 option found "
313					"(type=%d)\n", nd_opt->nd_opt_type);
314				/* XXX bark? */
315			} else {
316				ndopts->nd_opt_array[nd_opt->nd_opt_type]
317					= nd_opt;
318			}
319			break;
320		case ND_OPT_PREFIX_INFORMATION:
321			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
322				ndopts->nd_opt_array[nd_opt->nd_opt_type]
323					= nd_opt;
324			}
325			ndopts->nd_opts_pi_end =
326				(struct nd_opt_prefix_info *)nd_opt;
327			break;
328		default:
329			/*
330			 * Unknown options must be silently ignored,
331			 * to accomodate future extension to the protocol.
332			 */
333			log(LOG_INFO,
334			    "nd6_options: unsupported option %d - "
335			    "option ignored\n", nd_opt->nd_opt_type);
336		}
337
338skip1:
339		i++;
340		if (i > 10) {
341			printf("too many loop in nd opt\n");
342			break;
343		}
344
345		if (ndopts->nd_opts_done)
346			break;
347	}
348
349	return 0;
350}
351
352/*
353 * ND6 timer routine to expire default route list and prefix list
354 */
355void
356nd6_timer(ignored_arg)
357	void	*ignored_arg;
358{
359	int s;
360	register struct llinfo_nd6 *ln;
361	register struct nd_defrouter *dr;
362	register struct nd_prefix *pr;
363
364	s = splnet();
365	timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
366
367	ln = llinfo_nd6.ln_next;
368	/* XXX BSD/OS separates this code -- itojun */
369	while (ln && ln != &llinfo_nd6) {
370		struct rtentry *rt;
371		struct ifnet *ifp;
372		struct sockaddr_in6 *dst;
373		struct llinfo_nd6 *next = ln->ln_next;
374
375		if ((rt = ln->ln_rt) == NULL) {
376			ln = next;
377			continue;
378		}
379		if ((ifp = rt->rt_ifp) == NULL) {
380			ln = next;
381			continue;
382		}
383		dst = (struct sockaddr_in6 *)rt_key(rt);
384
385		if (ln->ln_expire > time_second) {
386			ln = next;
387			continue;
388		}
389
390		/* sanity check */
391		if (!rt)
392			panic("rt=0 in nd6_timer(ln=%p)\n", ln);
393		if (!dst)
394			panic("dst=0 in nd6_timer(ln=%p)\n", ln);
395
396		switch (ln->ln_state) {
397		case ND6_LLINFO_INCOMPLETE:
398			if (ln->ln_asked < nd6_mmaxtries) {
399				ln->ln_asked++;
400				ln->ln_expire = time_second +
401					nd_ifinfo[ifp->if_index].retrans / 1000;
402				nd6_ns_output(ifp, NULL, &dst->sin6_addr,
403					ln, 0);
404			} else {
405				struct mbuf *m = ln->ln_hold;
406				if (m) {
407					if (rt->rt_ifp) {
408						/*
409						 * Fake rcvif to make ICMP error
410						 * more helpful in diagnosing
411						 * for the receiver.
412						 * XXX: should we consider
413						 * older rcvif?
414						 */
415						m->m_pkthdr.rcvif = rt->rt_ifp;
416					}
417					icmp6_error(m, ICMP6_DST_UNREACH,
418						    ICMP6_DST_UNREACH_ADDR, 0);
419					ln->ln_hold = NULL;
420				}
421				nd6_free(rt);
422			}
423			break;
424		case ND6_LLINFO_REACHABLE:
425			if (ln->ln_expire) {
426				ln->ln_state = ND6_LLINFO_STALE;
427			}
428			break;
429		/*
430		 * ND6_LLINFO_STALE state requires nothing for timer
431		 * routine.
432		 */
433		case ND6_LLINFO_DELAY:
434			ln->ln_asked = 1;
435			ln->ln_state = ND6_LLINFO_PROBE;
436			ln->ln_expire = time_second +
437				nd_ifinfo[ifp->if_index].retrans / 1000;
438			nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr,
439				ln, 0);
440			break;
441
442		case ND6_LLINFO_PROBE:
443			if (ln->ln_asked < nd6_umaxtries) {
444				ln->ln_asked++;
445				ln->ln_expire = time_second +
446					nd_ifinfo[ifp->if_index].retrans / 1000;
447				nd6_ns_output(ifp, &dst->sin6_addr,
448					       &dst->sin6_addr, ln, 0);
449			} else {
450				nd6_free(rt);
451			}
452			break;
453		case ND6_LLINFO_WAITDELETE:
454			nd6_free(rt);
455			break;
456		}
457		ln = next;
458	}
459
460	/* expire */
461	dr = LIST_FIRST(&nd_defrouter);
462	while (dr) {
463		if (dr->expire && dr->expire < time_second) {
464			struct nd_defrouter *t;
465			t = LIST_NEXT(dr, dr_entry);
466			defrtrlist_del(dr);
467			dr = t;
468		} else
469			dr = LIST_NEXT(dr, dr_entry);
470	}
471	pr = LIST_FIRST(&nd_prefix);
472	while (pr) {
473		struct in6_ifaddr *ia6;
474		struct in6_addrlifetime *lt6;
475
476		if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
477			ia6 = NULL;
478		else
479			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
480
481		if (ia6) {
482			/* check address lifetime */
483			lt6 = &ia6->ia6_lifetime;
484			if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
485				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
486			if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
487				if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
488					in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
489				/* xxx ND_OPT_PI_FLAG_ONLINK processing */
490			}
491		}
492
493		/*
494		 * check prefix lifetime.
495		 * since pltime is just for autoconf, pltime processing for
496		 * prefix is not necessary.
497		 *
498		 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
499		 * can use the old prefix information to validate the
500		 * next prefix information to come.  See prelist_update()
501		 * for actual validation.
502		 */
503		if (pr->ndpr_expire
504		 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
505			struct nd_prefix *t;
506			t = LIST_NEXT(pr, ndpr_entry);
507
508			/*
509			 * address expiration and prefix expiration are
510			 * separate.  NEVER perform in6_ifdel here.
511			 */
512
513			prelist_remove(pr);
514			pr = t;
515		} else
516			pr = LIST_NEXT(pr, ndpr_entry);
517	}
518	splx(s);
519}
520
521struct rtentry *
522nd6_lookup(addr6, create, ifp)
523	struct in6_addr *addr6;
524	int create;
525	struct ifnet *ifp;
526{
527	struct rtentry *rt;
528	struct sockaddr_in6 sin6;
529
530	bzero(&sin6, sizeof(sin6));
531	sin6.sin6_len = sizeof(struct sockaddr_in6);
532	sin6.sin6_family = AF_INET6;
533	sin6.sin6_addr = *addr6;
534	rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
535	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
536		/*
537		 * This is the case for the default route.
538		 * If we want to create a neighbor cache for the address, we
539		 * should free the route for the destination and allocate an
540		 * interface route.
541		 */
542		if (create) {
543			RTFREE(rt);
544			rt = 0;
545		}
546	}
547	if (!rt) {
548		if (create && ifp) {
549			/*
550			 * If no route is available and create is set,
551			 * we allocate a host route for the destination
552			 * and treat it like an interface route.
553			 * This hack is necessary for a neighbor which can't
554			 * be covered by our own prefix.
555			 */
556			struct ifaddr *ifa =
557				ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
558			if (ifa == NULL)
559				return(NULL);
560
561			/*
562			 * Create a new route. RTF_LLINFO is necessary
563			 * to create a Neighbor Cache entry for the
564			 * destination in nd6_rtrequest which will be
565			 * called in rtequest via ifa->ifa_rtrequest.
566			 */
567			if (rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
568				      ifa->ifa_addr,
569				      (struct sockaddr *)&all1_sa,
570				      (ifa->ifa_flags |
571				       RTF_HOST | RTF_LLINFO) & ~RTF_CLONING,
572				      &rt))
573				log(LOG_ERR,
574				    "nd6_lookup: failed to add route for a "
575				    "neighbor(%s)\n", ip6_sprintf(addr6));
576			if (rt == NULL)
577				return(NULL);
578			if (rt->rt_llinfo) {
579				struct llinfo_nd6 *ln =
580					(struct llinfo_nd6 *)rt->rt_llinfo;
581				ln->ln_state = ND6_LLINFO_NOSTATE;
582			}
583		} else
584			return(NULL);
585	}
586	rt->rt_refcnt--;
587	/*
588	 * Validation for the entry.
589	 * XXX: we can't use rt->rt_ifp to check for the interface, since
590	 *      it might be the loopback interface if the entry is for our
591	 *      own address on a non-loopback interface. Instead, we should
592	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
593	 */
594	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
595	    rt->rt_gateway->sa_family != AF_LINK ||
596	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
597		if (create) {
598			log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
599			    ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
600			/* xxx more logs... kazu */
601		}
602		return(0);
603	}
604	return(rt);
605}
606
607/*
608 * Detect if a given IPv6 address identifies a neighbor on a given link.
609 * XXX: should take care of the destination of a p2p link?
610 */
611int
612nd6_is_addr_neighbor(addr, ifp)
613	struct in6_addr *addr;
614	struct ifnet *ifp;
615{
616	register struct ifaddr *ifa;
617	int i;
618
619#define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
620#define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
621
622	/* A link-local address is always a neighbor. */
623	if (IN6_IS_ADDR_LINKLOCAL(addr))
624		return(1);
625
626	/*
627	 * If the address matches one of our addresses,
628	 * it should be a neighbor.
629	 */
630	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
631	{
632		if (ifa->ifa_addr->sa_family != AF_INET6)
633			next: continue;
634
635		for (i = 0; i < 4; i++) {
636			if ((IFADDR6(ifa).s6_addr32[i] ^ addr->s6_addr32[i]) &
637			    IFMASK6(ifa).s6_addr32[i])
638				goto next;
639		}
640		return(1);
641	}
642
643	/*
644	 * Even if the address matches none of our addresses, it might be
645	 * in the neighbor cache.
646	 */
647	if (nd6_lookup(addr, 0, ifp))
648		return(1);
649
650	return(0);
651#undef IFADDR6
652#undef IFMASK6
653}
654
655/*
656 * Free an nd6 llinfo entry.
657 */
658void
659nd6_free(rt)
660	struct rtentry *rt;
661{
662	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
663	struct sockaddr_dl *sdl;
664
665	if (ln->ln_router) {
666		/* remove from default router list */
667		struct nd_defrouter *dr;
668		struct in6_addr *in6;
669		int s;
670		in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
671
672		s = splnet();
673		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->
674				      sin6_addr,
675				      rt->rt_ifp);
676		if (dr)
677			defrtrlist_del(dr);
678		else if (!ip6_forwarding && ip6_accept_rtadv) {
679			/*
680			 * rt6_flush must be called in any case.
681			 * see the comment in nd6_na_input().
682			 */
683			rt6_flush(in6, rt->rt_ifp);
684		}
685		splx(s);
686	}
687
688	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
689	   sdl->sdl_family == AF_LINK) {
690		sdl->sdl_alen = 0;
691		ln->ln_state = ND6_LLINFO_WAITDELETE;
692		ln->ln_asked = 0;
693		rt->rt_flags &= ~RTF_REJECT;
694		return;
695	}
696	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
697		  0, (struct rtentry **)0);
698}
699
700/*
701 * Upper-layer reachability hint for Neighbor Unreachability Detection.
702 *
703 * XXX cost-effective metods?
704 */
705void
706nd6_nud_hint(rt, dst6)
707	struct rtentry *rt;
708	struct in6_addr *dst6;
709{
710	struct llinfo_nd6 *ln;
711
712	/*
713	 * If the caller specified "rt", use that.  Otherwise, resolve the
714	 * routing table by supplied "dst6".
715	 */
716	if (!rt) {
717		if (!dst6)
718			return;
719		if (!(rt = nd6_lookup(dst6, 0, NULL)))
720			return;
721	}
722
723	if ((rt->rt_flags & RTF_GATEWAY)
724	 || (rt->rt_flags & RTF_LLINFO) == 0
725	 || !rt->rt_llinfo
726	 || !rt->rt_gateway
727	 || rt->rt_gateway->sa_family != AF_LINK) {
728		/* This is not a host route. */
729		return;
730	}
731
732	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
733	if (ln->ln_state == ND6_LLINFO_INCOMPLETE)
734		return;
735
736	ln->ln_state = ND6_LLINFO_REACHABLE;
737	if (ln->ln_expire)
738		ln->ln_expire = time_second +
739			nd_ifinfo[rt->rt_ifp->if_index].reachable;
740}
741
742void
743nd6_rtrequest(req, rt, sa)
744	int	req;
745	struct rtentry *rt;
746	struct sockaddr *sa; /* xxx unused */
747{
748	struct sockaddr *gate = rt->rt_gateway;
749	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
750	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
751	struct ifnet *ifp = rt->rt_ifp;
752	struct ifaddr *ifa;
753
754	if (rt->rt_flags & RTF_GATEWAY)
755		return;
756
757	switch (req) {
758	case RTM_ADD:
759		/*
760		 * There is no backward compatibility :)
761		 *
762		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
763		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
764		 *	   rt->rt_flags |= RTF_CLONING;
765		 */
766		if (rt->rt_flags & RTF_CLONING || rt->rt_flags & RTF_LLINFO) {
767			/*
768			 * Case 1: This route should come from
769			 * a route to interface. RTF_LLINFO flag is set
770			 * for a host route whose destination should be
771			 * treated as on-link.
772			 */
773			rt_setgate(rt, rt_key(rt),
774				   (struct sockaddr *)&null_sdl);
775			gate = rt->rt_gateway;
776			SDL(gate)->sdl_type = ifp->if_type;
777			SDL(gate)->sdl_index = ifp->if_index;
778			if (ln)
779				ln->ln_expire = time_second;
780			if (ln && ln->ln_expire == 0) {
781				/* cludge for desktops */
782				ln->ln_expire = 1;
783			}
784			if (rt->rt_flags & RTF_CLONING)
785				break;
786		}
787		/* Announce a new entry if requested. */
788		if (rt->rt_flags & RTF_ANNOUNCE)
789			nd6_na_output(ifp,
790				      &SIN6(rt_key(rt))->sin6_addr,
791				      &SIN6(rt_key(rt))->sin6_addr,
792				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
793				      1);
794		/* FALLTHROUGH */
795	case RTM_RESOLVE:
796		if (gate->sa_family != AF_LINK ||
797		   gate->sa_len < sizeof(null_sdl)) {
798			log(LOG_DEBUG, "nd6_rtrequest: bad gateway value\n");
799			break;
800		}
801		SDL(gate)->sdl_type = ifp->if_type;
802		SDL(gate)->sdl_index = ifp->if_index;
803		if (ln != 0)
804			break;	/* This happens on a route change */
805		/*
806		 * Case 2: This route may come from cloning, or a manual route
807		 * add with a LL address.
808		 */
809		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
810		rt->rt_llinfo = (caddr_t)ln;
811		if (!ln) {
812			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
813			break;
814		}
815		nd6_inuse++;
816		nd6_allocated++;
817		Bzero(ln, sizeof(*ln));
818		ln->ln_rt = rt;
819		/* this is required for "ndp" command. - shin */
820		if (req == RTM_ADD) {
821		        /*
822			 * gate should have some valid AF_LINK entry,
823			 * and ln->ln_expire should have some lifetime
824			 * which is specified by ndp command.
825			 */
826			ln->ln_state = ND6_LLINFO_REACHABLE;
827		} else {
828		        /*
829			 * When req == RTM_RESOLVE, rt is created and
830			 * initialized in rtrequest(), so rt_expire is 0.
831			 */
832			ln->ln_state = ND6_LLINFO_INCOMPLETE;
833			ln->ln_expire = time_second;
834		}
835		rt->rt_flags |= RTF_LLINFO;
836		ln->ln_next = llinfo_nd6.ln_next;
837		llinfo_nd6.ln_next = ln;
838		ln->ln_prev = &llinfo_nd6;
839		ln->ln_next->ln_prev = ln;
840
841		/*
842		 * check if rt_key(rt) is one of my address assigned
843		 * to the interface.
844		 */
845		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
846					  &SIN6(rt_key(rt))->sin6_addr);
847		if (ifa) {
848			caddr_t macp = nd6_ifptomac(ifp);
849			ln->ln_expire = 0;
850			ln->ln_state = ND6_LLINFO_REACHABLE;
851			if (macp) {
852				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
853				SDL(gate)->sdl_alen = ifp->if_addrlen;
854			}
855			if (nd6_useloopback) {
856				rt->rt_ifp = &loif[0];	/*XXX*/
857				/*
858				 * Make sure rt_ifa be equal to the ifaddr
859				 * corresponding to the address.
860				 * We need this because when we refer
861				 * rt_ifa->ia6_flags in ip6_input, we assume
862				 * that the rt_ifa points to the address instead
863				 * of the loopback address.
864				 */
865				if (ifa != rt->rt_ifa) {
866					rt->rt_ifa->ifa_refcnt--;
867					ifa->ifa_refcnt++;
868					rt->rt_ifa = ifa;
869				}
870			}
871		}
872		break;
873
874	case RTM_DELETE:
875		if (!ln)
876			break;
877		nd6_inuse--;
878		ln->ln_next->ln_prev = ln->ln_prev;
879		ln->ln_prev->ln_next = ln->ln_next;
880		ln->ln_prev = NULL;
881		rt->rt_llinfo = 0;
882		rt->rt_flags &= ~RTF_LLINFO;
883		if (ln->ln_hold)
884			m_freem(ln->ln_hold);
885		Free((caddr_t)ln);
886	}
887}
888
889void
890nd6_p2p_rtrequest(req, rt, sa)
891	int	req;
892	struct rtentry *rt;
893	struct sockaddr *sa; /* xxx unused */
894{
895	struct sockaddr *gate = rt->rt_gateway;
896	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
897	struct ifnet *ifp = rt->rt_ifp;
898	struct ifaddr *ifa;
899
900	if (rt->rt_flags & RTF_GATEWAY)
901		return;
902
903	switch (req) {
904	case RTM_ADD:
905		/*
906		 * There is no backward compatibility :)
907		 *
908		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
909		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
910		 *	   rt->rt_flags |= RTF_CLONING;
911		 */
912		if (rt->rt_flags & RTF_CLONING) {
913			/*
914			 * Case 1: This route should come from
915			 * a route to interface.
916			 */
917			rt_setgate(rt, rt_key(rt),
918				   (struct sockaddr *)&null_sdl);
919			gate = rt->rt_gateway;
920			SDL(gate)->sdl_type = ifp->if_type;
921			SDL(gate)->sdl_index = ifp->if_index;
922			break;
923		}
924		/* Announce a new entry if requested. */
925		if (rt->rt_flags & RTF_ANNOUNCE)
926			nd6_na_output(ifp,
927				      &SIN6(rt_key(rt))->sin6_addr,
928				      &SIN6(rt_key(rt))->sin6_addr,
929				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
930				      1);
931		/* FALLTHROUGH */
932	case RTM_RESOLVE:
933		/*
934		 * check if rt_key(rt) is one of my address assigned
935		 * to the interface.
936		 */
937 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
938					  &SIN6(rt_key(rt))->sin6_addr);
939		if (ifa) {
940			if (nd6_useloopback) {
941				rt->rt_ifp = &loif[0];	/*XXX*/
942			}
943		}
944		break;
945	}
946}
947
948int
949nd6_ioctl(cmd, data, ifp)
950	u_long cmd;
951	caddr_t	data;
952	struct ifnet *ifp;
953{
954	struct in6_drlist *drl = (struct in6_drlist *)data;
955	struct in6_prlist *prl = (struct in6_prlist *)data;
956	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
957	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
958	struct nd_defrouter *dr, any;
959	struct nd_prefix *pr;
960	struct rtentry *rt;
961	int i = 0, error = 0;
962	int s;
963
964	switch (cmd) {
965	case SIOCGDRLST_IN6:
966		bzero(drl, sizeof(*drl));
967		s = splnet();
968		dr = LIST_FIRST(&nd_defrouter);
969		while (dr && i < DRLSTSIZ) {
970			drl->defrouter[i].rtaddr = dr->rtaddr;
971			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
972				/* XXX: need to this hack for KAME stack */
973				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
974			} else
975				log(LOG_ERR,
976				    "default router list contains a "
977				    "non-linklocal address(%s)\n",
978				    ip6_sprintf(&drl->defrouter[i].rtaddr));
979
980			drl->defrouter[i].flags = dr->flags;
981			drl->defrouter[i].rtlifetime = dr->rtlifetime;
982			drl->defrouter[i].expire = dr->expire;
983			drl->defrouter[i].if_index = dr->ifp->if_index;
984			i++;
985			dr = LIST_NEXT(dr, dr_entry);
986		}
987		splx(s);
988		break;
989	case SIOCGPRLST_IN6:
990		bzero(prl, sizeof(*prl));
991		s = splnet();
992		pr = LIST_FIRST(&nd_prefix);
993		while (pr && i < PRLSTSIZ) {
994			struct nd_pfxrouter *pfr;
995			int j;
996
997			prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
998			prl->prefix[i].raflags = pr->ndpr_raf;
999			prl->prefix[i].prefixlen = pr->ndpr_plen;
1000			prl->prefix[i].vltime = pr->ndpr_vltime;
1001			prl->prefix[i].pltime = pr->ndpr_pltime;
1002			prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1003			prl->prefix[i].expire = pr->ndpr_expire;
1004
1005			pfr = LIST_FIRST(&pr->ndpr_advrtrs);
1006			j = 0;
1007			while(pfr) {
1008				if (j < DRLSTSIZ) {
1009#define RTRADDR prl->prefix[i].advrtr[j]
1010					RTRADDR = pfr->router->rtaddr;
1011					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1012						/* XXX: hack for KAME */
1013						RTRADDR.s6_addr16[1] = 0;
1014					} else
1015						log(LOG_ERR,
1016						    "a router(%s) advertises "
1017						    "a prefix with "
1018						    "non-link local address\n",
1019						    ip6_sprintf(&RTRADDR));
1020#undef RTRADDR
1021				}
1022				j++;
1023				pfr = LIST_NEXT(pfr, pfr_entry);
1024			}
1025			prl->prefix[i].advrtrs = j;
1026
1027			i++;
1028			pr = LIST_NEXT(pr, ndpr_entry);
1029		}
1030		splx(s);
1031	      {
1032		struct rr_prefix *rpp;
1033
1034		for (rpp = LIST_FIRST(&rr_prefix); rpp;
1035		     rpp = LIST_NEXT(rpp, rp_entry)) {
1036			if (i >= PRLSTSIZ)
1037				break;
1038			prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
1039			prl->prefix[i].raflags = rpp->rp_raf;
1040			prl->prefix[i].prefixlen = rpp->rp_plen;
1041			prl->prefix[i].vltime = rpp->rp_vltime;
1042			prl->prefix[i].pltime = rpp->rp_pltime;
1043			prl->prefix[i].if_index = rpp->rp_ifp->if_index;
1044			prl->prefix[i].expire = rpp->rp_expire;
1045			prl->prefix[i].advrtrs = 0;
1046			i++;
1047		}
1048	      }
1049
1050		break;
1051	case SIOCGIFINFO_IN6:
1052		ndi->ndi = nd_ifinfo[ifp->if_index];
1053		break;
1054	case SIOCSNDFLUSH_IN6:
1055		/* flush default router list */
1056		/*
1057		 * xxx sumikawa: should not delete route if default
1058		 * route equals to the top of default router list
1059		 */
1060		bzero(&any, sizeof(any));
1061		defrouter_delreq(&any, 0);
1062		/* xxx sumikawa: flush prefix list */
1063		break;
1064	case SIOCSPFXFLUSH_IN6:
1065	    {
1066		/* flush all the prefix advertised by routers */
1067		struct nd_prefix *pr, *next;
1068
1069		s = splnet();
1070		for (pr = LIST_FIRST(&nd_prefix); pr; pr = next) {
1071			next = LIST_NEXT(pr, ndpr_entry);
1072			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
1073				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
1074			prelist_remove(pr);
1075		}
1076		splx(s);
1077		break;
1078	    }
1079	case SIOCSRTRFLUSH_IN6:
1080	    {
1081		/* flush all the default routers */
1082		struct nd_defrouter *dr, *next;
1083
1084		s = splnet();
1085		if ((dr = LIST_FIRST(&nd_defrouter)) != NULL) {
1086			/*
1087			 * The first entry of the list may be stored in
1088			 * the routing table, so we'll delete it later.
1089			 */
1090			for (dr = LIST_NEXT(dr, dr_entry); dr; dr = next) {
1091				next = LIST_NEXT(dr, dr_entry);
1092				defrtrlist_del(dr);
1093			}
1094			defrtrlist_del(LIST_FIRST(&nd_defrouter));
1095		}
1096		splx(s);
1097		break;
1098	    }
1099	case SIOCGNBRINFO_IN6:
1100	    {
1101		struct llinfo_nd6 *ln;
1102		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1103
1104		/*
1105		 * XXX: KAME specific hack for scoped addresses
1106		 *      XXXX: for other scopes than link-local?
1107		 */
1108		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1109		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1110			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1111
1112			if (*idp == 0)
1113				*idp = htons(ifp->if_index);
1114		}
1115
1116		s = splnet();
1117		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1118			error = EINVAL;
1119			break;
1120		}
1121		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1122		nbi->state = ln->ln_state;
1123		nbi->asked = ln->ln_asked;
1124		nbi->isrouter = ln->ln_router;
1125		nbi->expire = ln->ln_expire;
1126		splx(s);
1127
1128		break;
1129	    }
1130	}
1131	return(error);
1132}
1133
1134/*
1135 * Create neighbor cache entry and cache link-layer address,
1136 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1137 */
1138struct rtentry *
1139nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1140	struct ifnet *ifp;
1141	struct in6_addr *from;
1142	char *lladdr;
1143	int lladdrlen;
1144	int type;	/* ICMP6 type */
1145	int code;	/* type dependent information */
1146{
1147	struct rtentry *rt = NULL;
1148	struct llinfo_nd6 *ln = NULL;
1149	int is_newentry;
1150	struct sockaddr_dl *sdl = NULL;
1151	int do_update;
1152	int olladdr;
1153	int llchange;
1154	int newstate = 0;
1155
1156	if (!ifp)
1157		panic("ifp == NULL in nd6_cache_lladdr");
1158	if (!from)
1159		panic("from == NULL in nd6_cache_lladdr");
1160
1161	/* nothing must be updated for unspecified address */
1162	if (IN6_IS_ADDR_UNSPECIFIED(from))
1163		return NULL;
1164
1165	/*
1166	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1167	 * the caller.
1168	 *
1169	 * XXX If the link does not have link-layer adderss, what should
1170	 * we do? (ifp->if_addrlen == 0)
1171	 * Spec says nothing in sections for RA, RS and NA.  There's small
1172	 * description on it in NS section (RFC 2461 7.2.3).
1173	 */
1174
1175	rt = nd6_lookup(from, 0, ifp);
1176	if (!rt) {
1177		rt = nd6_lookup(from, 1, ifp);
1178		is_newentry = 1;
1179	} else
1180		is_newentry = 0;
1181
1182	if (!rt)
1183		return NULL;
1184	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1185fail:
1186		nd6_free(rt);
1187		return NULL;
1188	}
1189	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1190	if (!ln)
1191		goto fail;
1192	if (!rt->rt_gateway)
1193		goto fail;
1194	if (rt->rt_gateway->sa_family != AF_LINK)
1195		goto fail;
1196	sdl = SDL(rt->rt_gateway);
1197
1198	olladdr = (sdl->sdl_alen) ? 1 : 0;
1199	if (olladdr && lladdr) {
1200		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1201			llchange = 1;
1202		else
1203			llchange = 0;
1204	} else
1205		llchange = 0;
1206
1207	/*
1208	 * newentry olladdr  lladdr  llchange	(*=record)
1209	 *	0	n	n	--	(1)
1210	 *	0	y	n	--	(2)
1211	 *	0	n	y	--	(3) * STALE
1212	 *	0	y	y	n	(4) *
1213	 *	0	y	y	y	(5) * STALE
1214	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1215	 *	1	--	y	--	(7) * STALE
1216	 */
1217
1218	if (lladdr) {		/*(3-5) and (7)*/
1219		/*
1220		 * Record source link-layer address
1221		 * XXX is it dependent to ifp->if_type?
1222		 */
1223		sdl->sdl_alen = ifp->if_addrlen;
1224		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1225	}
1226
1227	if (!is_newentry) {
1228		if ((!olladdr && lladdr)		/*(3)*/
1229		 || (olladdr && lladdr && llchange)) {	/*(5)*/
1230			do_update = 1;
1231			newstate = ND6_LLINFO_STALE;
1232		} else					/*(1-2,4)*/
1233			do_update = 0;
1234	} else {
1235		do_update = 1;
1236		if (!lladdr)				/*(6)*/
1237			newstate = ND6_LLINFO_NOSTATE;
1238		else					/*(7)*/
1239			newstate = ND6_LLINFO_STALE;
1240	}
1241
1242	if (do_update) {
1243		/*
1244		 * Update the state of the neighbor cache.
1245		 */
1246		ln->ln_state = newstate;
1247
1248		if (ln->ln_state == ND6_LLINFO_STALE) {
1249			rt->rt_flags &= ~RTF_REJECT;
1250			if (ln->ln_hold) {
1251				nd6_output(ifp, ln->ln_hold,
1252					   (struct sockaddr_in6 *)rt_key(rt),
1253					   rt);
1254				ln->ln_hold = 0;
1255			}
1256		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1257			/* probe right away */
1258			ln->ln_expire = time_second;
1259		}
1260	}
1261
1262	/*
1263	 * ICMP6 type dependent behavior.
1264	 *
1265	 * NS: clear IsRouter if new entry
1266	 * RS: clear IsRouter
1267	 * RA: set IsRouter if there's lladdr
1268	 * redir: clear IsRouter if new entry
1269	 *
1270	 * RA case, (1):
1271	 * The spec says that we must set IsRouter in the following cases:
1272	 * - If lladdr exist, set IsRouter.  This means (1-5).
1273	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1274	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1275	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1276	 * neighbor cache, this is similar to (6).
1277	 * This case is rare but we figured that we MUST NOT set IsRouter.
1278	 *
1279	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1280	 *							D R
1281	 *	0	n	n	--	(1)	c   ?     s
1282	 *	0	y	n	--	(2)	c   s     s
1283	 *	0	n	y	--	(3)	c   s     s
1284	 *	0	y	y	n	(4)	c   s     s
1285	 *	0	y	y	y	(5)	c   s     s
1286	 *	1	--	n	--	(6) c	c 	c s
1287	 *	1	--	y	--	(7) c	c   s	c s
1288	 *
1289	 *					(c=clear s=set)
1290	 */
1291	switch (type & 0xff) {
1292	case ND_NEIGHBOR_SOLICIT:
1293		/*
1294		 * New entry must have is_router flag cleared.
1295		 */
1296		if (is_newentry)	/*(6-7)*/
1297			ln->ln_router = 0;
1298		break;
1299	case ND_REDIRECT:
1300		/*
1301		 * If the icmp is a redirect to a better router, always set the
1302		 * is_router flag. Otherwise, if the entry is newly created,
1303		 * clear the flag. [RFC 2461, sec 8.3]
1304		 *
1305		 */
1306		if (code == ND_REDIRECT_ROUTER)
1307			ln->ln_router = 1;
1308		else if (is_newentry) /*(6-7)*/
1309			ln->ln_router = 0;
1310		break;
1311	case ND_ROUTER_SOLICIT:
1312		/*
1313		 * is_router flag must always be cleared.
1314		 */
1315		ln->ln_router = 0;
1316		break;
1317	case ND_ROUTER_ADVERT:
1318		/*
1319		 * Mark an entry with lladdr as a router.
1320		 */
1321		if ((!is_newentry && (olladdr || lladdr))	/*(2-5)*/
1322		 || (is_newentry && lladdr)) {			/*(7)*/
1323			ln->ln_router = 1;
1324		}
1325		break;
1326	}
1327
1328	return rt;
1329}
1330
1331static void
1332nd6_slowtimo(ignored_arg)
1333    void *ignored_arg;
1334{
1335	int s = splnet();
1336	register int i;
1337	register struct nd_ifinfo *nd6if;
1338
1339	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
1340	for (i = 1; i < if_index + 1; i++) {
1341		nd6if = &nd_ifinfo[i];
1342		if (nd6if->basereachable && /* already initialized */
1343		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1344			/*
1345			 * Since reachable time rarely changes by router
1346			 * advertisements, we SHOULD insure that a new random
1347			 * value gets recomputed at least once every few hours.
1348			 * (RFC 2461, 6.3.4)
1349			 */
1350			nd6if->recalctm = nd6_recalc_reachtm_interval;
1351			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1352		}
1353	}
1354	splx(s);
1355}
1356
1357#define senderr(e) { error = (e); goto bad;}
1358int
1359nd6_output(ifp, m0, dst, rt0)
1360	register struct ifnet *ifp;
1361	struct mbuf *m0;
1362	struct sockaddr_in6 *dst;
1363	struct rtentry *rt0;
1364{
1365	register struct mbuf *m = m0;
1366	register struct rtentry *rt = rt0;
1367	struct llinfo_nd6 *ln = NULL;
1368	int error = 0;
1369
1370	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1371		goto sendpkt;
1372
1373	/*
1374	 * XXX: we currently do not make neighbor cache on any interface
1375	 * other than ARCnet, Ethernet and FDDI.
1376	 */
1377	switch (ifp->if_type) {
1378	case IFT_ARCNET:
1379	case IFT_ETHER:
1380	case IFT_FDDI:
1381		break;
1382	default:
1383		goto sendpkt;
1384	}
1385
1386	/*
1387	 * next hop determination. This routine is derived from ether_outpout.
1388	 */
1389	if (rt) {
1390		if ((rt->rt_flags & RTF_UP) == 0) {
1391			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
1392				NULL)
1393			{
1394				rt->rt_refcnt--;
1395				if (rt->rt_ifp != ifp)
1396					return nd6_output(ifp, m0, dst, rt); /* XXX: loop care? */
1397			} else
1398				senderr(EHOSTUNREACH);
1399		}
1400		if (rt->rt_flags & RTF_GATEWAY) {
1401			if (rt->rt_gwroute == 0)
1402				goto lookup;
1403			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1404				rtfree(rt); rt = rt0;
1405			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1406				if ((rt = rt->rt_gwroute) == 0)
1407					senderr(EHOSTUNREACH);
1408			}
1409		}
1410		if (rt->rt_flags & RTF_REJECT)
1411			senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1412	}
1413
1414	/*
1415	 * Address resolution or Neighbor Unreachability Detection
1416	 * for the next hop.
1417	 * At this point, the destination of the packet must be a unicast
1418	 * or an anycast address(i.e. not a multicast).
1419	 */
1420
1421	/* Look up the neighbor cache for the nexthop */
1422	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1423		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1424	else {
1425		if ((rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1426			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1427	}
1428	if (!ln || !rt) {
1429		log(LOG_DEBUG, "nd6_output: can't allocate llinfo for %s "
1430		    "(ln=%p, rt=%p)\n",
1431		    ip6_sprintf(&dst->sin6_addr), ln, rt);
1432		senderr(EIO);	/* XXX: good error? */
1433	}
1434
1435
1436	/*
1437	 * The first time we send a packet to a neighbor whose entry is
1438	 * STALE, we have to change the state to DELAY and a sets a timer to
1439	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1440	 * neighbor unreachability detection on expiration.
1441	 * (RFC 2461 7.3.3)
1442	 */
1443	if (ln->ln_state == ND6_LLINFO_STALE) {
1444		ln->ln_asked = 0;
1445		ln->ln_state = ND6_LLINFO_DELAY;
1446		ln->ln_expire = time_second + nd6_delay;
1447	}
1448
1449	/*
1450	 * If the neighbor cache entry has a state other than INCOMPLETE
1451	 * (i.e. its link-layer address is already reloved), just
1452	 * send the packet.
1453	 */
1454	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1455		goto sendpkt;
1456
1457	/*
1458	 * There is a neighbor cache entry, but no ethernet address
1459	 * response yet. Replace the held mbuf (if any) with this
1460	 * latest one.
1461	 *
1462	 * XXX Does the code conform to rate-limiting rule?
1463	 * (RFC 2461 7.2.2)
1464	 */
1465	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
1466	    ln->ln_state == ND6_LLINFO_NOSTATE)
1467		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1468	if (ln->ln_hold)
1469		m_freem(ln->ln_hold);
1470	ln->ln_hold = m;
1471	if (ln->ln_expire) {
1472		rt->rt_flags &= ~RTF_REJECT;
1473		if (ln->ln_asked < nd6_mmaxtries &&
1474		    ln->ln_expire < time_second) {
1475			ln->ln_asked++;
1476			ln->ln_expire = time_second +
1477				nd_ifinfo[ifp->if_index].retrans / 1000;
1478			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1479		}
1480	}
1481	return(0);
1482
1483  sendpkt:
1484	return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1485
1486  bad:
1487	if (m)
1488		m_freem(m);
1489	return (error);
1490}
1491#undef senderr
1492
1493int
1494nd6_storelladdr(ifp, rt, m, dst, desten)
1495	struct ifnet *ifp;
1496	struct rtentry *rt;
1497	struct mbuf *m;
1498	struct sockaddr *dst;
1499	u_char *desten;
1500{
1501	struct sockaddr_dl *sdl;
1502
1503	if (m->m_flags & M_MCAST) {
1504		switch (ifp->if_type) {
1505		case IFT_ETHER:
1506		case IFT_FDDI:
1507			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1508						 desten);
1509			return(1);
1510			break;
1511		case IFT_ARCNET:
1512			*desten = 0;
1513			return(1);
1514		default:
1515			return(0);
1516		}
1517	}
1518
1519	if (rt == NULL ||
1520	    rt->rt_gateway->sa_family != AF_LINK) {
1521		printf("nd6_storelladdr: something odd happens\n");
1522		return(0);
1523	}
1524	sdl = SDL(rt->rt_gateway);
1525	if (sdl->sdl_alen != 0)
1526		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1527
1528	return(1);
1529}
1530