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