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