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