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