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