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