nd6_nbr.c revision 260504
1212793Sdim/*-
2212793Sdim * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3212793Sdim * All rights reserved.
4212793Sdim *
5212793Sdim * Redistribution and use in source and binary forms, with or without
6212793Sdim * modification, are permitted provided that the following conditions
7212793Sdim * are met:
8212793Sdim * 1. Redistributions of source code must retain the above copyright
9212793Sdim *    notice, this list of conditions and the following disclaimer.
10212793Sdim * 2. Redistributions in binary form must reproduce the above copyright
11212793Sdim *    notice, this list of conditions and the following disclaimer in the
12212793Sdim *    documentation and/or other materials provided with the distribution.
13212793Sdim * 3. Neither the name of the project nor the names of its contributors
14212793Sdim *    may be used to endorse or promote products derived from this software
15212793Sdim *    without specific prior written permission.
16212793Sdim *
17249423Sdim * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18249423Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20212793Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21212793Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22212793Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23226633Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24226633Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25226633Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26226633Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27226633Sdim * SUCH DAMAGE.
28239462Sdim *
29226633Sdim *	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
30226633Sdim */
31226633Sdim
32226633Sdim#include <sys/cdefs.h>
33239462Sdim__FBSDID("$FreeBSD: stable/10/sys/netinet6/nd6_nbr.c 260504 2014-01-10 09:45:28Z ae $");
34226633Sdim
35226633Sdim#include "opt_inet.h"
36226633Sdim#include "opt_inet6.h"
37226633Sdim#include "opt_ipsec.h"
38212793Sdim#include "opt_mpath.h"
39226633Sdim
40226633Sdim#include <sys/param.h>
41226633Sdim#include <sys/systm.h>
42226633Sdim#include <sys/malloc.h>
43212793Sdim#include <sys/lock.h>
44226633Sdim#include <sys/rwlock.h>
45226633Sdim#include <sys/mbuf.h>
46212793Sdim#include <sys/socket.h>
47226633Sdim#include <sys/sockio.h>
48226633Sdim#include <sys/time.h>
49226633Sdim#include <sys/kernel.h>
50226633Sdim#include <sys/errno.h>
51212793Sdim#include <sys/syslog.h>
52226633Sdim#include <sys/queue.h>
53226633Sdim#include <sys/callout.h>
54212793Sdim
55226633Sdim#include <net/if.h>
56226633Sdim#include <net/if_types.h>
57212793Sdim#include <net/if_dl.h>
58226633Sdim#include <net/if_var.h>
59226633Sdim#include <net/route.h>
60226633Sdim#ifdef RADIX_MPATH
61226633Sdim#include <net/radix_mpath.h>
62226633Sdim#endif
63226633Sdim
64226633Sdim#include <netinet/in.h>
65226633Sdim#include <netinet/in_var.h>
66226633Sdim#include <net/if_llatbl.h>
67226633Sdim#define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
68226633Sdim#include <netinet6/in6_var.h>
69226633Sdim#include <netinet6/in6_ifattach.h>
70226633Sdim#include <netinet/ip6.h>
71226633Sdim#include <netinet6/ip6_var.h>
72226633Sdim#include <netinet6/scope6_var.h>
73226633Sdim#include <netinet6/nd6.h>
74226633Sdim#include <netinet/icmp6.h>
75226633Sdim#include <netinet/ip_carp.h>
76226633Sdim#include <netinet6/send.h>
77226633Sdim
78226633Sdim#define SDL(s) ((struct sockaddr_dl *)s)
79226633Sdim
80226633Sdimstruct dadq;
81226633Sdimstatic struct dadq *nd6_dad_find(struct ifaddr *);
82226633Sdimstatic void nd6_dad_starttimer(struct dadq *, int);
83226633Sdimstatic void nd6_dad_stoptimer(struct dadq *);
84226633Sdimstatic void nd6_dad_timer(struct dadq *);
85226633Sdimstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
86212793Sdimstatic void nd6_dad_ns_input(struct ifaddr *);
87226633Sdimstatic void nd6_dad_na_input(struct ifaddr *);
88226633Sdimstatic void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
89226633Sdim    const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
90226633Sdim
91226633SdimVNET_DEFINE(int, dad_ignore_ns) = 0;	/* ignore NS in DAD - specwise incorrect*/
92212793SdimVNET_DEFINE(int, dad_maxtry) = 15;	/* max # of *tries* to transmit DAD packet */
93226633Sdim#define	V_dad_ignore_ns			VNET(dad_ignore_ns)
94226633Sdim#define	V_dad_maxtry			VNET(dad_maxtry)
95226633Sdim
96226633Sdim/*
97212793Sdim * Input a Neighbor Solicitation Message.
98226633Sdim *
99226633Sdim * Based on RFC 2461
100226633Sdim * Based on RFC 2462 (duplicate address detection)
101226633Sdim */
102212793Sdimvoid
103226633Sdimnd6_ns_input(struct mbuf *m, int off, int icmp6len)
104226633Sdim{
105212793Sdim	struct ifnet *ifp = m->m_pkthdr.rcvif;
106212793Sdim	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
107212793Sdim	struct nd_neighbor_solicit *nd_ns;
108218893Sdim	struct in6_addr saddr6 = ip6->ip6_src;
109218893Sdim	struct in6_addr daddr6 = ip6->ip6_dst;
110218893Sdim	struct in6_addr taddr6;
111218893Sdim	struct in6_addr myaddr6;
112218893Sdim	char *lladdr = NULL;
113212793Sdim	struct ifaddr *ifa = NULL;
114218893Sdim	int lladdrlen = 0;
115218893Sdim	int anycast = 0, proxy = 0, tentative = 0;
116226633Sdim	int tlladdr;
117226633Sdim	int rflag;
118226633Sdim	union nd_opts ndopts;
119226633Sdim	struct sockaddr_dl proxydl;
120226633Sdim	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
121226633Sdim
122226633Sdim	rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
123226633Sdim	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
124226633Sdim		rflag = 0;
125226633Sdim#ifndef PULLDOWN_TEST
126226633Sdim	IP6_EXTHDR_CHECK(m, off, icmp6len,);
127226633Sdim	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
128226633Sdim#else
129226633Sdim	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
130226633Sdim	if (nd_ns == NULL) {
131226633Sdim		ICMP6STAT_INC(icp6s_tooshort);
132218893Sdim		return;
133218893Sdim	}
134218893Sdim#endif
135212793Sdim	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
136212793Sdim	taddr6 = nd_ns->nd_ns_target;
137212793Sdim	if (in6_setscope(&taddr6, ifp, NULL) != 0)
138212793Sdim		goto bad;
139212793Sdim
140218893Sdim	if (ip6->ip6_hlim != 255) {
141212793Sdim		nd6log((LOG_ERR,
142212793Sdim		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
143		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
144		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
145		goto bad;
146	}
147
148	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
149		/* dst has to be a solicited node multicast address. */
150		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
151		    /* don't check ifindex portion */
152		    daddr6.s6_addr32[1] == 0 &&
153		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
154		    daddr6.s6_addr8[12] == 0xff) {
155			; /* good */
156		} else {
157			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
158			    "(wrong ip6 dst)\n"));
159			goto bad;
160		}
161	} else if (!V_nd6_onlink_ns_rfc4861) {
162		struct sockaddr_in6 src_sa6;
163
164		/*
165		 * According to recent IETF discussions, it is not a good idea
166		 * to accept a NS from an address which would not be deemed
167		 * to be a neighbor otherwise.  This point is expected to be
168		 * clarified in future revisions of the specification.
169		 */
170		bzero(&src_sa6, sizeof(src_sa6));
171		src_sa6.sin6_family = AF_INET6;
172		src_sa6.sin6_len = sizeof(src_sa6);
173		src_sa6.sin6_addr = saddr6;
174		if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
175			nd6log((LOG_INFO, "nd6_ns_input: "
176				"NS packet from non-neighbor\n"));
177			goto bad;
178		}
179	}
180
181	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
182		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
183		goto bad;
184	}
185
186	icmp6len -= sizeof(*nd_ns);
187	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
188	if (nd6_options(&ndopts) < 0) {
189		nd6log((LOG_INFO,
190		    "nd6_ns_input: invalid ND option, ignored\n"));
191		/* nd6_options have incremented stats */
192		goto freeit;
193	}
194
195	if (ndopts.nd_opts_src_lladdr) {
196		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
197		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
198	}
199
200	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
201		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
202		    "(link-layer address option)\n"));
203		goto bad;
204	}
205
206	/*
207	 * Attaching target link-layer address to the NA?
208	 * (RFC 2461 7.2.4)
209	 *
210	 * NS IP dst is unicast/anycast			MUST NOT add
211	 * NS IP dst is solicited-node multicast	MUST add
212	 *
213	 * In implementation, we add target link-layer address by default.
214	 * We do not add one in MUST NOT cases.
215	 */
216	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
217		tlladdr = 0;
218	else
219		tlladdr = 1;
220
221	/*
222	 * Target address (taddr6) must be either:
223	 * (1) Valid unicast/anycast address for my receiving interface,
224	 * (2) Unicast address for which I'm offering proxy service, or
225	 * (3) "tentative" address on which DAD is being performed.
226	 */
227	/* (1) and (3) check. */
228	if (ifp->if_carp)
229		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
230	else
231		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
232
233	/* (2) check. */
234	if (ifa == NULL) {
235		struct rtentry *rt;
236		struct sockaddr_in6 tsin6;
237		int need_proxy;
238#ifdef RADIX_MPATH
239		struct route_in6 ro;
240#endif
241
242		bzero(&tsin6, sizeof tsin6);
243		tsin6.sin6_len = sizeof(struct sockaddr_in6);
244		tsin6.sin6_family = AF_INET6;
245		tsin6.sin6_addr = taddr6;
246
247		/* Always use the default FIB. */
248#ifdef RADIX_MPATH
249		bzero(&ro, sizeof(ro));
250		ro.ro_dst = tsin6;
251		rtalloc_mpath_fib((struct route *)&ro, RTF_ANNOUNCE,
252		    RT_DEFAULT_FIB);
253		rt = ro.ro_rt;
254#else
255		rt = in6_rtalloc1((struct sockaddr *)&tsin6, 0, 0,
256		    RT_DEFAULT_FIB);
257#endif
258		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
259		    rt->rt_gateway->sa_family == AF_LINK);
260		if (rt != NULL) {
261			/*
262			 * Make a copy while we can be sure that rt_gateway
263			 * is still stable before unlocking to avoid lock
264			 * order problems.  proxydl will only be used if
265			 * proxy will be set in the next block.
266			 */
267			if (need_proxy)
268				proxydl = *SDL(rt->rt_gateway);
269			RTFREE_LOCKED(rt);
270		}
271		if (need_proxy) {
272			/*
273			 * proxy NDP for single entry
274			 */
275			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
276				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
277			if (ifa)
278				proxy = 1;
279		}
280	}
281	if (ifa == NULL) {
282		/*
283		 * We've got an NS packet, and we don't have that adddress
284		 * assigned for us.  We MUST silently ignore it.
285		 * See RFC2461 7.2.3.
286		 */
287		goto freeit;
288	}
289	myaddr6 = *IFA_IN6(ifa);
290	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
291	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
292	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
293		goto freeit;
294
295	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
296		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
297		    "(if %d, NS packet %d)\n",
298		    ip6_sprintf(ip6bufs, &taddr6),
299		    ifp->if_addrlen, lladdrlen - 2));
300		goto bad;
301	}
302
303	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
304		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
305		    ip6_sprintf(ip6bufs, &saddr6)));
306		goto freeit;
307	}
308
309	/*
310	 * We have neighbor solicitation packet, with target address equals to
311	 * one of my tentative address.
312	 *
313	 * src addr	how to process?
314	 * ---		---
315	 * multicast	of course, invalid (rejected in ip6_input)
316	 * unicast	somebody is doing address resolution -> ignore
317	 * unspec	dup address detection
318	 *
319	 * The processing is defined in RFC 2462.
320	 */
321	if (tentative) {
322		/*
323		 * If source address is unspecified address, it is for
324		 * duplicate address detection.
325		 *
326		 * If not, the packet is for addess resolution;
327		 * silently ignore it.
328		 */
329		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
330			nd6_dad_ns_input(ifa);
331
332		goto freeit;
333	}
334
335	/*
336	 * If the source address is unspecified address, entries must not
337	 * be created or updated.
338	 * It looks that sender is performing DAD.  Output NA toward
339	 * all-node multicast address, to tell the sender that I'm using
340	 * the address.
341	 * S bit ("solicited") must be zero.
342	 */
343	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
344		struct in6_addr in6_all;
345
346		in6_all = in6addr_linklocal_allnodes;
347		if (in6_setscope(&in6_all, ifp, NULL) != 0)
348			goto bad;
349		nd6_na_output_fib(ifp, &in6_all, &taddr6,
350		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
351		    rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL,
352		    M_GETFIB(m));
353		goto freeit;
354	}
355
356	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
357	    ND_NEIGHBOR_SOLICIT, 0);
358
359	nd6_na_output_fib(ifp, &saddr6, &taddr6,
360	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
361	    rflag | ND_NA_FLAG_SOLICITED, tlladdr,
362	    proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m));
363 freeit:
364	if (ifa != NULL)
365		ifa_free(ifa);
366	m_freem(m);
367	return;
368
369 bad:
370	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
371		ip6_sprintf(ip6bufs, &saddr6)));
372	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
373		ip6_sprintf(ip6bufs, &daddr6)));
374	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
375		ip6_sprintf(ip6bufs, &taddr6)));
376	ICMP6STAT_INC(icp6s_badns);
377	if (ifa != NULL)
378		ifa_free(ifa);
379	m_freem(m);
380}
381
382/*
383 * Output a Neighbor Solicitation Message. Caller specifies:
384 *	- ICMP6 header source IP6 address
385 *	- ND6 header target IP6 address
386 *	- ND6 header source datalink address
387 *
388 * Based on RFC 2461
389 * Based on RFC 2462 (duplicate address detection)
390 *
391 *   ln - for source address determination
392 *  dad - duplicate address detection
393 */
394void
395nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
396    const struct in6_addr *taddr6, struct llentry *ln, int dad)
397{
398	struct mbuf *m;
399	struct m_tag *mtag;
400	struct ip6_hdr *ip6;
401	struct nd_neighbor_solicit *nd_ns;
402	struct ip6_moptions im6o;
403	int icmp6len;
404	int maxlen;
405	caddr_t mac;
406	struct route_in6 ro;
407
408	if (IN6_IS_ADDR_MULTICAST(taddr6))
409		return;
410
411	/* estimate the size of message */
412	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
413	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
414	if (max_linkhdr + maxlen >= MCLBYTES) {
415#ifdef DIAGNOSTIC
416		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
417		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
418#endif
419		return;
420	}
421
422	if (max_linkhdr + maxlen > MHLEN)
423		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
424	else
425		m = m_gethdr(M_NOWAIT, MT_DATA);
426	if (m == NULL)
427		return;
428
429	bzero(&ro, sizeof(ro));
430
431	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
432		m->m_flags |= M_MCAST;
433		im6o.im6o_multicast_ifp = ifp;
434		im6o.im6o_multicast_hlim = 255;
435		im6o.im6o_multicast_loop = 0;
436	}
437
438	icmp6len = sizeof(*nd_ns);
439	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
440	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
441
442	/* fill neighbor solicitation packet */
443	ip6 = mtod(m, struct ip6_hdr *);
444	ip6->ip6_flow = 0;
445	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
446	ip6->ip6_vfc |= IPV6_VERSION;
447	/* ip6->ip6_plen will be set later */
448	ip6->ip6_nxt = IPPROTO_ICMPV6;
449	ip6->ip6_hlim = 255;
450	if (daddr6)
451		ip6->ip6_dst = *daddr6;
452	else {
453		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
454		ip6->ip6_dst.s6_addr16[1] = 0;
455		ip6->ip6_dst.s6_addr32[1] = 0;
456		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
457		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
458		ip6->ip6_dst.s6_addr8[12] = 0xff;
459		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
460			goto bad;
461	}
462	if (!dad) {
463		struct ifaddr *ifa;
464
465		/*
466		 * RFC2461 7.2.2:
467		 * "If the source address of the packet prompting the
468		 * solicitation is the same as one of the addresses assigned
469		 * to the outgoing interface, that address SHOULD be placed
470		 * in the IP Source Address of the outgoing solicitation.
471		 * Otherwise, any one of the addresses assigned to the
472		 * interface should be used."
473		 *
474		 * We use the source address for the prompting packet
475		 * (saddr6), if:
476		 * - saddr6 is given from the caller (by giving "ln"), and
477		 * - saddr6 belongs to the outgoing interface.
478		 * Otherwise, we perform the source address selection as usual.
479		 */
480		struct in6_addr *hsrc;
481
482		hsrc = NULL;
483		if (ln != NULL) {
484			LLE_RLOCK(ln);
485			if (ln->la_hold != NULL) {
486				struct ip6_hdr *hip6;		/* hold ip6 */
487
488				/*
489				 * assuming every packet in la_hold has the same IP
490				 * header
491				 */
492				hip6 = mtod(ln->la_hold, struct ip6_hdr *);
493				/* XXX pullup? */
494				if (sizeof(*hip6) < ln->la_hold->m_len) {
495					ip6->ip6_src = hip6->ip6_src;
496					hsrc = &hip6->ip6_src;
497				}
498			}
499			LLE_RUNLOCK(ln);
500		}
501		if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
502		    hsrc)) != NULL) {
503			/* ip6_src set already. */
504			ifa_free(ifa);
505		} else {
506			int error;
507			struct sockaddr_in6 dst_sa;
508			struct in6_addr src_in;
509			struct ifnet *oifp;
510
511			bzero(&dst_sa, sizeof(dst_sa));
512			dst_sa.sin6_family = AF_INET6;
513			dst_sa.sin6_len = sizeof(dst_sa);
514			dst_sa.sin6_addr = ip6->ip6_dst;
515
516			oifp = ifp;
517			error = in6_selectsrc(&dst_sa, NULL,
518			    NULL, &ro, NULL, &oifp, &src_in);
519			if (error) {
520				char ip6buf[INET6_ADDRSTRLEN];
521				nd6log((LOG_DEBUG,
522				    "nd6_ns_output: source can't be "
523				    "determined: dst=%s, error=%d\n",
524				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
525				    error));
526				goto bad;
527			}
528			ip6->ip6_src = src_in;
529		}
530	} else {
531		/*
532		 * Source address for DAD packet must always be IPv6
533		 * unspecified address. (0::0)
534		 * We actually don't have to 0-clear the address (we did it
535		 * above), but we do so here explicitly to make the intention
536		 * clearer.
537		 */
538		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
539	}
540	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
541	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
542	nd_ns->nd_ns_code = 0;
543	nd_ns->nd_ns_reserved = 0;
544	nd_ns->nd_ns_target = *taddr6;
545	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
546
547	/*
548	 * Add source link-layer address option.
549	 *
550	 *				spec		implementation
551	 *				---		---
552	 * DAD packet			MUST NOT	do not add the option
553	 * there's no link layer address:
554	 *				impossible	do not add the option
555	 * there's link layer address:
556	 *	Multicast NS		MUST add one	add the option
557	 *	Unicast NS		SHOULD add one	add the option
558	 */
559	if (!dad && (mac = nd6_ifptomac(ifp))) {
560		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
561		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
562		/* 8 byte alignments... */
563		optlen = (optlen + 7) & ~7;
564
565		m->m_pkthdr.len += optlen;
566		m->m_len += optlen;
567		icmp6len += optlen;
568		bzero((caddr_t)nd_opt, optlen);
569		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
570		nd_opt->nd_opt_len = optlen >> 3;
571		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
572	}
573
574	ip6->ip6_plen = htons((u_short)icmp6len);
575	nd_ns->nd_ns_cksum = 0;
576	nd_ns->nd_ns_cksum =
577	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
578
579	if (send_sendso_input_hook != NULL) {
580		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
581			sizeof(unsigned short), M_NOWAIT);
582		if (mtag == NULL)
583			goto bad;
584		*(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
585		m_tag_prepend(m, mtag);
586	}
587
588	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
589	icmp6_ifstat_inc(ifp, ifs6_out_msg);
590	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
591	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
592
593	/* We don't cache this route. */
594	RO_RTFREE(&ro);
595
596	return;
597
598  bad:
599	if (ro.ro_rt) {
600		RTFREE(ro.ro_rt);
601	}
602	m_freem(m);
603	return;
604}
605
606/*
607 * Neighbor advertisement input handling.
608 *
609 * Based on RFC 2461
610 * Based on RFC 2462 (duplicate address detection)
611 *
612 * the following items are not implemented yet:
613 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
614 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
615 */
616void
617nd6_na_input(struct mbuf *m, int off, int icmp6len)
618{
619	struct ifnet *ifp = m->m_pkthdr.rcvif;
620	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
621	struct nd_neighbor_advert *nd_na;
622	struct in6_addr daddr6 = ip6->ip6_dst;
623	struct in6_addr taddr6;
624	int flags;
625	int is_router;
626	int is_solicited;
627	int is_override;
628	char *lladdr = NULL;
629	int lladdrlen = 0;
630	int checklink = 0;
631	struct ifaddr *ifa;
632	struct llentry *ln = NULL;
633	union nd_opts ndopts;
634	struct mbuf *chain = NULL;
635	struct m_tag *mtag;
636	struct sockaddr_in6 sin6;
637	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
638
639	if (ip6->ip6_hlim != 255) {
640		nd6log((LOG_ERR,
641		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
642		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
643		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
644		goto bad;
645	}
646
647#ifndef PULLDOWN_TEST
648	IP6_EXTHDR_CHECK(m, off, icmp6len,);
649	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
650#else
651	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
652	if (nd_na == NULL) {
653		ICMP6STAT_INC(icp6s_tooshort);
654		return;
655	}
656#endif
657
658	flags = nd_na->nd_na_flags_reserved;
659	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
660	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
661	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
662
663	taddr6 = nd_na->nd_na_target;
664	if (in6_setscope(&taddr6, ifp, NULL))
665		goto bad;	/* XXX: impossible */
666
667	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
668		nd6log((LOG_ERR,
669		    "nd6_na_input: invalid target address %s\n",
670		    ip6_sprintf(ip6bufs, &taddr6)));
671		goto bad;
672	}
673	if (IN6_IS_ADDR_MULTICAST(&daddr6))
674		if (is_solicited) {
675			nd6log((LOG_ERR,
676			    "nd6_na_input: a solicited adv is multicasted\n"));
677			goto bad;
678		}
679
680	icmp6len -= sizeof(*nd_na);
681	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
682	if (nd6_options(&ndopts) < 0) {
683		nd6log((LOG_INFO,
684		    "nd6_na_input: invalid ND option, ignored\n"));
685		/* nd6_options have incremented stats */
686		goto freeit;
687	}
688
689	if (ndopts.nd_opts_tgt_lladdr) {
690		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
691		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
692	}
693
694	/*
695	 * This effectively disables the DAD check on a non-master CARP
696	 * address.
697	 */
698	if (ifp->if_carp)
699		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
700	else
701		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
702
703	/*
704	 * Target address matches one of my interface address.
705	 *
706	 * If my address is tentative, this means that there's somebody
707	 * already using the same address as mine.  This indicates DAD failure.
708	 * This is defined in RFC 2462.
709	 *
710	 * Otherwise, process as defined in RFC 2461.
711	 */
712	if (ifa
713	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
714		ifa_free(ifa);
715		nd6_dad_na_input(ifa);
716		goto freeit;
717	}
718
719	/* Just for safety, maybe unnecessary. */
720	if (ifa) {
721		ifa_free(ifa);
722		log(LOG_ERR,
723		    "nd6_na_input: duplicate IP6 address %s\n",
724		    ip6_sprintf(ip6bufs, &taddr6));
725		goto freeit;
726	}
727
728	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
729		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
730		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
731		    ifp->if_addrlen, lladdrlen - 2));
732		goto bad;
733	}
734
735	/*
736	 * If no neighbor cache entry is found, NA SHOULD silently be
737	 * discarded.
738	 */
739	IF_AFDATA_RLOCK(ifp);
740	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
741	IF_AFDATA_RUNLOCK(ifp);
742	if (ln == NULL) {
743		goto freeit;
744	}
745
746	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
747		/*
748		 * If the link-layer has address, and no lladdr option came,
749		 * discard the packet.
750		 */
751		if (ifp->if_addrlen && lladdr == NULL) {
752			goto freeit;
753		}
754
755		/*
756		 * Record link-layer address, and update the state.
757		 */
758		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
759		ln->la_flags |= LLE_VALID;
760		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
761		if (is_solicited) {
762			ln->ln_state = ND6_LLINFO_REACHABLE;
763			ln->ln_byhint = 0;
764			if (!ND6_LLINFO_PERMANENT(ln)) {
765				nd6_llinfo_settimer_locked(ln,
766				    (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
767			}
768		} else {
769			ln->ln_state = ND6_LLINFO_STALE;
770			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
771		}
772		if ((ln->ln_router = is_router) != 0) {
773			/*
774			 * This means a router's state has changed from
775			 * non-reachable to probably reachable, and might
776			 * affect the status of associated prefixes..
777			 */
778			checklink = 1;
779		}
780	} else {
781		int llchange;
782
783		/*
784		 * Check if the link-layer address has changed or not.
785		 */
786		if (lladdr == NULL)
787			llchange = 0;
788		else {
789			if (ln->la_flags & LLE_VALID) {
790				if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
791					llchange = 1;
792				else
793					llchange = 0;
794			} else
795				llchange = 1;
796		}
797
798		/*
799		 * This is VERY complex.  Look at it with care.
800		 *
801		 * override solicit lladdr llchange	action
802		 *					(L: record lladdr)
803		 *
804		 *	0	0	n	--	(2c)
805		 *	0	0	y	n	(2b) L
806		 *	0	0	y	y	(1)    REACHABLE->STALE
807		 *	0	1	n	--	(2c)   *->REACHABLE
808		 *	0	1	y	n	(2b) L *->REACHABLE
809		 *	0	1	y	y	(1)    REACHABLE->STALE
810		 *	1	0	n	--	(2a)
811		 *	1	0	y	n	(2a) L
812		 *	1	0	y	y	(2a) L *->STALE
813		 *	1	1	n	--	(2a)   *->REACHABLE
814		 *	1	1	y	n	(2a) L *->REACHABLE
815		 *	1	1	y	y	(2a) L *->REACHABLE
816		 */
817		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
818			/*
819			 * If state is REACHABLE, make it STALE.
820			 * no other updates should be done.
821			 */
822			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
823				ln->ln_state = ND6_LLINFO_STALE;
824				nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
825			}
826			goto freeit;
827		} else if (is_override				   /* (2a) */
828			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
829			|| lladdr == NULL) {			   /* (2c) */
830			/*
831			 * Update link-local address, if any.
832			 */
833			if (lladdr != NULL) {
834				bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
835				ln->la_flags |= LLE_VALID;
836				EVENTHANDLER_INVOKE(lle_event, ln,
837				    LLENTRY_RESOLVED);
838			}
839
840			/*
841			 * If solicited, make the state REACHABLE.
842			 * If not solicited and the link-layer address was
843			 * changed, make it STALE.
844			 */
845			if (is_solicited) {
846				ln->ln_state = ND6_LLINFO_REACHABLE;
847				ln->ln_byhint = 0;
848				if (!ND6_LLINFO_PERMANENT(ln)) {
849					nd6_llinfo_settimer_locked(ln,
850					    (long)ND_IFINFO(ifp)->reachable * hz);
851				}
852			} else {
853				if (lladdr != NULL && llchange) {
854					ln->ln_state = ND6_LLINFO_STALE;
855					nd6_llinfo_settimer_locked(ln,
856					    (long)V_nd6_gctimer * hz);
857				}
858			}
859		}
860
861		if (ln->ln_router && !is_router) {
862			/*
863			 * The peer dropped the router flag.
864			 * Remove the sender from the Default Router List and
865			 * update the Destination Cache entries.
866			 */
867			struct nd_defrouter *dr;
868			struct in6_addr *in6;
869
870			in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
871
872			/*
873			 * Lock to protect the default router list.
874			 * XXX: this might be unnecessary, since this function
875			 * is only called under the network software interrupt
876			 * context.  However, we keep it just for safety.
877			 */
878			dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
879			if (dr)
880				defrtrlist_del(dr);
881			else if (ND_IFINFO(ln->lle_tbl->llt_ifp)->flags &
882			    ND6_IFF_ACCEPT_RTADV) {
883				/*
884				 * Even if the neighbor is not in the default
885				 * router list, the neighbor may be used
886				 * as a next hop for some destinations
887				 * (e.g. redirect case). So we must
888				 * call rt6_flush explicitly.
889				 */
890				rt6_flush(&ip6->ip6_src, ifp);
891			}
892		}
893		ln->ln_router = is_router;
894	}
895        /* XXX - QL
896	 *  Does this matter?
897	 *  rt->rt_flags &= ~RTF_REJECT;
898	 */
899	ln->la_asked = 0;
900	if (ln->la_hold) {
901		struct mbuf *m_hold, *m_hold_next;
902
903		/*
904		 * reset the la_hold in advance, to explicitly
905		 * prevent a la_hold lookup in nd6_output()
906		 * (wouldn't happen, though...)
907		 */
908		for (m_hold = ln->la_hold, ln->la_hold = NULL;
909		    m_hold; m_hold = m_hold_next) {
910			m_hold_next = m_hold->m_nextpkt;
911			m_hold->m_nextpkt = NULL;
912			/*
913			 * we assume ifp is not a loopback here, so just set
914			 * the 2nd argument as the 1st one.
915			 */
916
917			if (send_sendso_input_hook != NULL) {
918				mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
919				    sizeof(unsigned short), M_NOWAIT);
920				if (mtag == NULL)
921					goto bad;
922				m_tag_prepend(m, mtag);
923			}
924
925			nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
926		}
927	}
928 freeit:
929	if (ln != NULL) {
930		if (chain)
931			memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
932		LLE_WUNLOCK(ln);
933
934		if (chain)
935			nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
936	}
937	if (checklink)
938		pfxlist_onlink_check();
939
940	m_freem(m);
941	return;
942
943 bad:
944	if (ln != NULL)
945		LLE_WUNLOCK(ln);
946
947	ICMP6STAT_INC(icp6s_badna);
948	m_freem(m);
949}
950
951/*
952 * Neighbor advertisement output handling.
953 *
954 * Based on RFC 2461
955 *
956 * the following items are not implemented yet:
957 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
958 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
959 *
960 * tlladdr - 1 if include target link-layer address
961 * sdl0 - sockaddr_dl (= proxy NA) or NULL
962 */
963static void
964nd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
965    const struct in6_addr *taddr6, u_long flags, int tlladdr,
966    struct sockaddr *sdl0, u_int fibnum)
967{
968	struct mbuf *m;
969	struct m_tag *mtag;
970	struct ifnet *oifp;
971	struct ip6_hdr *ip6;
972	struct nd_neighbor_advert *nd_na;
973	struct ip6_moptions im6o;
974	struct in6_addr src, daddr6;
975	struct sockaddr_in6 dst_sa;
976	int icmp6len, maxlen, error;
977	caddr_t mac = NULL;
978	struct route_in6 ro;
979
980	bzero(&ro, sizeof(ro));
981
982	daddr6 = *daddr6_0;	/* make a local copy for modification */
983
984	/* estimate the size of message */
985	maxlen = sizeof(*ip6) + sizeof(*nd_na);
986	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
987	if (max_linkhdr + maxlen >= MCLBYTES) {
988#ifdef DIAGNOSTIC
989		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
990		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
991#endif
992		return;
993	}
994
995	if (max_linkhdr + maxlen > MHLEN)
996		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
997	else
998		m = m_gethdr(M_NOWAIT, MT_DATA);
999	if (m == NULL)
1000		return;
1001	M_SETFIB(m, fibnum);
1002
1003	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1004		m->m_flags |= M_MCAST;
1005		im6o.im6o_multicast_ifp = ifp;
1006		im6o.im6o_multicast_hlim = 255;
1007		im6o.im6o_multicast_loop = 0;
1008	}
1009
1010	icmp6len = sizeof(*nd_na);
1011	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
1012	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
1013
1014	/* fill neighbor advertisement packet */
1015	ip6 = mtod(m, struct ip6_hdr *);
1016	ip6->ip6_flow = 0;
1017	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1018	ip6->ip6_vfc |= IPV6_VERSION;
1019	ip6->ip6_nxt = IPPROTO_ICMPV6;
1020	ip6->ip6_hlim = 255;
1021	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
1022		/* reply to DAD */
1023		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1024		daddr6.s6_addr16[1] = 0;
1025		daddr6.s6_addr32[1] = 0;
1026		daddr6.s6_addr32[2] = 0;
1027		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1028		if (in6_setscope(&daddr6, ifp, NULL))
1029			goto bad;
1030
1031		flags &= ~ND_NA_FLAG_SOLICITED;
1032	}
1033	ip6->ip6_dst = daddr6;
1034	bzero(&dst_sa, sizeof(struct sockaddr_in6));
1035	dst_sa.sin6_family = AF_INET6;
1036	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1037	dst_sa.sin6_addr = daddr6;
1038
1039	/*
1040	 * Select a source whose scope is the same as that of the dest.
1041	 */
1042	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1043	oifp = ifp;
1044	error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &oifp, &src);
1045	if (error) {
1046		char ip6buf[INET6_ADDRSTRLEN];
1047		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1048		    "determined: dst=%s, error=%d\n",
1049		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1050		goto bad;
1051	}
1052	ip6->ip6_src = src;
1053	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1054	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1055	nd_na->nd_na_code = 0;
1056	nd_na->nd_na_target = *taddr6;
1057	in6_clearscope(&nd_na->nd_na_target); /* XXX */
1058
1059	/*
1060	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1061	 * see nd6_ns_input() for details.
1062	 * Basically, if NS packet is sent to unicast/anycast addr,
1063	 * target lladdr option SHOULD NOT be included.
1064	 */
1065	if (tlladdr) {
1066		/*
1067		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
1068		 * lladdr in sdl0.  If we are not proxying (sending NA for
1069		 * my address) use lladdr configured for the interface.
1070		 */
1071		if (sdl0 == NULL) {
1072			if (ifp->if_carp)
1073				mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1074			if (mac == NULL)
1075				mac = nd6_ifptomac(ifp);
1076		} else if (sdl0->sa_family == AF_LINK) {
1077			struct sockaddr_dl *sdl;
1078			sdl = (struct sockaddr_dl *)sdl0;
1079			if (sdl->sdl_alen == ifp->if_addrlen)
1080				mac = LLADDR(sdl);
1081		}
1082	}
1083	if (tlladdr && mac) {
1084		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1085		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1086
1087		/* roundup to 8 bytes alignment! */
1088		optlen = (optlen + 7) & ~7;
1089
1090		m->m_pkthdr.len += optlen;
1091		m->m_len += optlen;
1092		icmp6len += optlen;
1093		bzero((caddr_t)nd_opt, optlen);
1094		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1095		nd_opt->nd_opt_len = optlen >> 3;
1096		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1097	} else
1098		flags &= ~ND_NA_FLAG_OVERRIDE;
1099
1100	ip6->ip6_plen = htons((u_short)icmp6len);
1101	nd_na->nd_na_flags_reserved = flags;
1102	nd_na->nd_na_cksum = 0;
1103	nd_na->nd_na_cksum =
1104	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1105
1106	if (send_sendso_input_hook != NULL) {
1107		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1108		    sizeof(unsigned short), M_NOWAIT);
1109		if (mtag == NULL)
1110			goto bad;
1111		*(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1112		m_tag_prepend(m, mtag);
1113	}
1114
1115	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1116	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1117	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1118	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1119
1120	/* We don't cache this route. */
1121	RO_RTFREE(&ro);
1122
1123	return;
1124
1125  bad:
1126	if (ro.ro_rt) {
1127		RTFREE(ro.ro_rt);
1128	}
1129	m_freem(m);
1130	return;
1131}
1132
1133#ifndef BURN_BRIDGES
1134void
1135nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
1136    const struct in6_addr *taddr6, u_long flags, int tlladdr,
1137    struct sockaddr *sdl0)
1138{
1139
1140	nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
1141	    RT_DEFAULT_FIB);
1142}
1143#endif
1144
1145caddr_t
1146nd6_ifptomac(struct ifnet *ifp)
1147{
1148	switch (ifp->if_type) {
1149	case IFT_ARCNET:
1150	case IFT_ETHER:
1151	case IFT_FDDI:
1152	case IFT_IEEE1394:
1153#ifdef IFT_L2VLAN
1154	case IFT_L2VLAN:
1155#endif
1156#ifdef IFT_IEEE80211
1157	case IFT_IEEE80211:
1158#endif
1159	case IFT_INFINIBAND:
1160	case IFT_BRIDGE:
1161	case IFT_ISO88025:
1162		return IF_LLADDR(ifp);
1163	default:
1164		return NULL;
1165	}
1166}
1167
1168struct dadq {
1169	TAILQ_ENTRY(dadq) dad_list;
1170	struct ifaddr *dad_ifa;
1171	int dad_count;		/* max NS to send */
1172	int dad_ns_tcount;	/* # of trials to send NS */
1173	int dad_ns_ocount;	/* NS sent so far */
1174	int dad_ns_icount;
1175	int dad_na_icount;
1176	struct callout dad_timer_ch;
1177	struct vnet *dad_vnet;
1178};
1179
1180static VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1181VNET_DEFINE(int, dad_init) = 0;
1182#define	V_dadq				VNET(dadq)
1183#define	V_dad_init			VNET(dad_init)
1184
1185static struct dadq *
1186nd6_dad_find(struct ifaddr *ifa)
1187{
1188	struct dadq *dp;
1189
1190	TAILQ_FOREACH(dp, &V_dadq, dad_list)
1191		if (dp->dad_ifa == ifa)
1192			return (dp);
1193
1194	return (NULL);
1195}
1196
1197static void
1198nd6_dad_starttimer(struct dadq *dp, int ticks)
1199{
1200
1201	callout_reset(&dp->dad_timer_ch, ticks,
1202	    (void (*)(void *))nd6_dad_timer, (void *)dp);
1203}
1204
1205static void
1206nd6_dad_stoptimer(struct dadq *dp)
1207{
1208
1209	callout_stop(&dp->dad_timer_ch);
1210}
1211
1212/*
1213 * Start Duplicate Address Detection (DAD) for specified interface address.
1214 */
1215void
1216nd6_dad_start(struct ifaddr *ifa, int delay)
1217{
1218	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1219	struct dadq *dp;
1220	char ip6buf[INET6_ADDRSTRLEN];
1221
1222	if (!V_dad_init) {
1223		TAILQ_INIT(&V_dadq);
1224		V_dad_init++;
1225	}
1226
1227	/*
1228	 * If we don't need DAD, don't do it.
1229	 * There are several cases:
1230	 * - DAD is disabled (ip6_dad_count == 0)
1231	 * - the interface address is anycast
1232	 */
1233	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1234		log(LOG_DEBUG,
1235			"nd6_dad_start: called with non-tentative address "
1236			"%s(%s)\n",
1237			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1238			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1239		return;
1240	}
1241	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1242		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1243		return;
1244	}
1245	if (!V_ip6_dad_count) {
1246		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1247		return;
1248	}
1249	if (ifa->ifa_ifp == NULL)
1250		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1251	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1252		return;
1253	}
1254	if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED)
1255		return;
1256	if (nd6_dad_find(ifa) != NULL) {
1257		/* DAD already in progress */
1258		return;
1259	}
1260
1261	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1262	if (dp == NULL) {
1263		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1264			"%s(%s)\n",
1265			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1266			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1267		return;
1268	}
1269	bzero(dp, sizeof(*dp));
1270	callout_init(&dp->dad_timer_ch, 0);
1271#ifdef VIMAGE
1272	dp->dad_vnet = curvnet;
1273#endif
1274	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
1275
1276	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1277	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1278
1279	/*
1280	 * Send NS packet for DAD, ip6_dad_count times.
1281	 * Note that we must delay the first transmission, if this is the
1282	 * first packet to be sent from the interface after interface
1283	 * (re)initialization.
1284	 */
1285	dp->dad_ifa = ifa;
1286	ifa_ref(ifa);	/* just for safety */
1287	dp->dad_count = V_ip6_dad_count;
1288	dp->dad_ns_icount = dp->dad_na_icount = 0;
1289	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1290	if (delay == 0) {
1291		nd6_dad_ns_output(dp, ifa);
1292		nd6_dad_starttimer(dp,
1293		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1294	} else {
1295		nd6_dad_starttimer(dp, delay);
1296	}
1297}
1298
1299/*
1300 * terminate DAD unconditionally.  used for address removals.
1301 */
1302void
1303nd6_dad_stop(struct ifaddr *ifa)
1304{
1305	struct dadq *dp;
1306
1307	if (!V_dad_init)
1308		return;
1309	dp = nd6_dad_find(ifa);
1310	if (!dp) {
1311		/* DAD wasn't started yet */
1312		return;
1313	}
1314
1315	nd6_dad_stoptimer(dp);
1316
1317	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1318	free(dp, M_IP6NDP);
1319	dp = NULL;
1320	ifa_free(ifa);
1321}
1322
1323static void
1324nd6_dad_timer(struct dadq *dp)
1325{
1326	CURVNET_SET(dp->dad_vnet);
1327	struct ifaddr *ifa = dp->dad_ifa;
1328	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1329	char ip6buf[INET6_ADDRSTRLEN];
1330
1331	/* Sanity check */
1332	if (ia == NULL) {
1333		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1334		goto done;
1335	}
1336	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1337		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1338			"%s(%s)\n",
1339			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1340			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1341		goto done;
1342	}
1343	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1344		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1345			"%s(%s)\n",
1346			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1347			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1348		goto done;
1349	}
1350
1351	/* timeouted with IFF_{RUNNING,UP} check */
1352	if (dp->dad_ns_tcount > V_dad_maxtry) {
1353		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1354		    if_name(ifa->ifa_ifp)));
1355
1356		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1357		free(dp, M_IP6NDP);
1358		dp = NULL;
1359		ifa_free(ifa);
1360		goto done;
1361	}
1362
1363	/* Need more checks? */
1364	if (dp->dad_ns_ocount < dp->dad_count) {
1365		/*
1366		 * We have more NS to go.  Send NS packet for DAD.
1367		 */
1368		nd6_dad_ns_output(dp, ifa);
1369		nd6_dad_starttimer(dp,
1370		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1371	} else {
1372		/*
1373		 * We have transmitted sufficient number of DAD packets.
1374		 * See what we've got.
1375		 */
1376		int duplicate;
1377
1378		duplicate = 0;
1379
1380		if (dp->dad_na_icount) {
1381			/*
1382			 * the check is in nd6_dad_na_input(),
1383			 * but just in case
1384			 */
1385			duplicate++;
1386		}
1387
1388		if (dp->dad_ns_icount) {
1389			/* We've seen NS, means DAD has failed. */
1390			duplicate++;
1391		}
1392
1393		if (duplicate) {
1394			/* (*dp) will be freed in nd6_dad_duplicated() */
1395			dp = NULL;
1396			nd6_dad_duplicated(ifa);
1397		} else {
1398			/*
1399			 * We are done with DAD.  No NA came, no NS came.
1400			 * No duplicate address found.
1401			 */
1402			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1403
1404			nd6log((LOG_DEBUG,
1405			    "%s: DAD complete for %s - no duplicates found\n",
1406			    if_name(ifa->ifa_ifp),
1407			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1408
1409			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1410			free(dp, M_IP6NDP);
1411			dp = NULL;
1412			ifa_free(ifa);
1413		}
1414	}
1415
1416done:
1417	CURVNET_RESTORE();
1418}
1419
1420void
1421nd6_dad_duplicated(struct ifaddr *ifa)
1422{
1423	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1424	struct ifnet *ifp;
1425	struct dadq *dp;
1426	char ip6buf[INET6_ADDRSTRLEN];
1427
1428	dp = nd6_dad_find(ifa);
1429	if (dp == NULL) {
1430		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1431		return;
1432	}
1433
1434	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1435	    "NS in/out=%d/%d, NA in=%d\n",
1436	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1437	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1438
1439	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1440	ia->ia6_flags |= IN6_IFF_DUPLICATED;
1441
1442	/* We are done with DAD, with duplicate address found. (failure) */
1443	nd6_dad_stoptimer(dp);
1444
1445	ifp = ifa->ifa_ifp;
1446	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1447	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1448	log(LOG_ERR, "%s: manual intervention required\n",
1449	    if_name(ifp));
1450
1451	/*
1452	 * If the address is a link-local address formed from an interface
1453	 * identifier based on the hardware address which is supposed to be
1454	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1455	 * operation on the interface SHOULD be disabled.
1456	 * [RFC 4862, Section 5.4.5]
1457	 */
1458	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1459		struct in6_addr in6;
1460
1461		/*
1462		 * To avoid over-reaction, we only apply this logic when we are
1463		 * very sure that hardware addresses are supposed to be unique.
1464		 */
1465		switch (ifp->if_type) {
1466		case IFT_ETHER:
1467		case IFT_FDDI:
1468		case IFT_ATM:
1469		case IFT_IEEE1394:
1470#ifdef IFT_IEEE80211
1471		case IFT_IEEE80211:
1472#endif
1473		case IFT_INFINIBAND:
1474			in6 = ia->ia_addr.sin6_addr;
1475			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1476			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1477				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1478				log(LOG_ERR, "%s: possible hardware address "
1479				    "duplication detected, disable IPv6\n",
1480				    if_name(ifp));
1481			}
1482			break;
1483		}
1484	}
1485
1486	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1487	free(dp, M_IP6NDP);
1488	dp = NULL;
1489	ifa_free(ifa);
1490}
1491
1492static void
1493nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1494{
1495	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1496	struct ifnet *ifp = ifa->ifa_ifp;
1497
1498	dp->dad_ns_tcount++;
1499	if ((ifp->if_flags & IFF_UP) == 0) {
1500		return;
1501	}
1502	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1503		return;
1504	}
1505
1506	dp->dad_ns_ocount++;
1507	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1508}
1509
1510static void
1511nd6_dad_ns_input(struct ifaddr *ifa)
1512{
1513	struct in6_ifaddr *ia;
1514	struct ifnet *ifp;
1515	const struct in6_addr *taddr6;
1516	struct dadq *dp;
1517	int duplicate;
1518
1519	if (ifa == NULL)
1520		panic("ifa == NULL in nd6_dad_ns_input");
1521
1522	ia = (struct in6_ifaddr *)ifa;
1523	ifp = ifa->ifa_ifp;
1524	taddr6 = &ia->ia_addr.sin6_addr;
1525	duplicate = 0;
1526	dp = nd6_dad_find(ifa);
1527
1528	/* Quickhack - completely ignore DAD NS packets */
1529	if (V_dad_ignore_ns) {
1530		char ip6buf[INET6_ADDRSTRLEN];
1531		nd6log((LOG_INFO,
1532		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1533		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1534		    if_name(ifa->ifa_ifp)));
1535		return;
1536	}
1537
1538	/*
1539	 * if I'm yet to start DAD, someone else started using this address
1540	 * first.  I have a duplicate and you win.
1541	 */
1542	if (dp == NULL || dp->dad_ns_ocount == 0)
1543		duplicate++;
1544
1545	/* XXX more checks for loopback situation - see nd6_dad_timer too */
1546
1547	if (duplicate) {
1548		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
1549		nd6_dad_duplicated(ifa);
1550	} else {
1551		/*
1552		 * not sure if I got a duplicate.
1553		 * increment ns count and see what happens.
1554		 */
1555		if (dp)
1556			dp->dad_ns_icount++;
1557	}
1558}
1559
1560static void
1561nd6_dad_na_input(struct ifaddr *ifa)
1562{
1563	struct dadq *dp;
1564
1565	if (ifa == NULL)
1566		panic("ifa == NULL in nd6_dad_na_input");
1567
1568	dp = nd6_dad_find(ifa);
1569	if (dp)
1570		dp->dad_na_icount++;
1571
1572	/* remove the address. */
1573	nd6_dad_duplicated(ifa);
1574}
1575