Home | History | Annotate | Line # | Download | only in netinet6
nd6_nbr.c revision 1.31
      1 /*	$NetBSD: nd6_nbr.c,v 1.31 2001/10/17 10:55:09 itojun Exp $	*/
      2 /*	$KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include "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 		nd6log((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 #ifdef DIAGNOSTIC
    355 	if (max_linkhdr + maxlen >= MCLBYTES) {
    356 		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
    357 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
    358 		panic("nd6_ns_output: insufficient MCLBYTES");
    359 		/* NOTREACHED */
    360 	}
    361 #endif
    362 
    363 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    364 	if (m && max_linkhdr + maxlen >= MHLEN) {
    365 		MCLGET(m, M_DONTWAIT);
    366 		if ((m->m_flags & M_EXT) == 0) {
    367 			m_free(m);
    368 			m = NULL;
    369 		}
    370 	}
    371 	if (m == NULL)
    372 		return;
    373 	m->m_pkthdr.rcvif = NULL;
    374 
    375 	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
    376 		m->m_flags |= M_MCAST;
    377 		im6o.im6o_multicast_ifp = ifp;
    378 		im6o.im6o_multicast_hlim = 255;
    379 		im6o.im6o_multicast_loop = 0;
    380 	}
    381 
    382 	icmp6len = sizeof(*nd_ns);
    383 	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
    384 	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
    385 
    386 	/* fill neighbor solicitation packet */
    387 	ip6 = mtod(m, struct ip6_hdr *);
    388 	ip6->ip6_flow = 0;
    389 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    390 	ip6->ip6_vfc |= IPV6_VERSION;
    391 	/* ip6->ip6_plen will be set later */
    392 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    393 	ip6->ip6_hlim = 255;
    394 	if (daddr6)
    395 		ip6->ip6_dst = *daddr6;
    396 	else {
    397 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    398 		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
    399 		ip6->ip6_dst.s6_addr32[1] = 0;
    400 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
    401 		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
    402 		ip6->ip6_dst.s6_addr8[12] = 0xff;
    403 	}
    404 	if (!dad) {
    405 		/*
    406 		 * RFC2461 7.2.2:
    407 		 * "If the source address of the packet prompting the
    408 		 * solicitation is the same as one of the addresses assigned
    409 		 * to the outgoing interface, that address SHOULD be placed
    410 		 * in the IP Source Address of the outgoing solicitation.
    411 		 * Otherwise, any one of the addresses assigned to the
    412 		 * interface should be used."
    413 		 *
    414 		 * We use the source address for the prompting packet
    415 		 * (saddr6), if:
    416 		 * - saddr6 is given from the caller (by giving "ln"), and
    417 		 * - saddr6 belongs to the outgoing interface.
    418 		 * Otherwise, we perform a scope-wise match.
    419 		 */
    420 		struct ip6_hdr *hip6;		/* hold ip6 */
    421 		struct in6_addr *saddr6;
    422 
    423 		if (ln && ln->ln_hold) {
    424 			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
    425 			/* XXX pullup? */
    426 			if (sizeof(*hip6) < ln->ln_hold->m_len)
    427 				saddr6 = &hip6->ip6_src;
    428 			else
    429 				saddr6 = NULL;
    430 		} else
    431 			saddr6 = NULL;
    432 		if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
    433 			bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
    434 		else {
    435 			ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
    436 			if (ia == NULL) {
    437 				m_freem(m);
    438 				return;
    439 			}
    440 			ip6->ip6_src = ia->ia_addr.sin6_addr;
    441 		}
    442 	} else {
    443 		/*
    444 		 * Source address for DAD packet must always be IPv6
    445 		 * unspecified address. (0::0)
    446 		 */
    447 		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
    448 	}
    449 	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
    450 	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
    451 	nd_ns->nd_ns_code = 0;
    452 	nd_ns->nd_ns_reserved = 0;
    453 	nd_ns->nd_ns_target = *taddr6;
    454 
    455 	if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
    456 		nd_ns->nd_ns_target.s6_addr16[1] = 0;
    457 
    458 	/*
    459 	 * Add source link-layer address option.
    460 	 *
    461 	 *				spec		implementation
    462 	 *				---		---
    463 	 * DAD packet			MUST NOT	do not add the option
    464 	 * there's no link layer address:
    465 	 *				impossible	do not add the option
    466 	 * there's link layer address:
    467 	 *	Multicast NS		MUST add one	add the option
    468 	 *	Unicast NS		SHOULD add one	add the option
    469 	 */
    470 	if (!dad && (mac = nd6_ifptomac(ifp))) {
    471 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    472 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
    473 		/* 8 byte alignments... */
    474 		optlen = (optlen + 7) & ~7;
    475 
    476 		m->m_pkthdr.len += optlen;
    477 		m->m_len += optlen;
    478 		icmp6len += optlen;
    479 		bzero((caddr_t)nd_opt, optlen);
    480 		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
    481 		nd_opt->nd_opt_len = optlen >> 3;
    482 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
    483 	}
    484 
    485 	ip6->ip6_plen = htons((u_short)icmp6len);
    486 	nd_ns->nd_ns_cksum = 0;
    487 	nd_ns->nd_ns_cksum
    488 		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
    489 
    490 #ifdef IPSEC
    491 	/* Don't lookup socket */
    492 	(void)ipsec_setsocket(m, NULL);
    493 #endif
    494 	ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
    495 	if (outif) {
    496 		icmp6_ifstat_inc(outif, ifs6_out_msg);
    497 		icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
    498 	}
    499 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
    500 }
    501 
    502 /*
    503  * Neighbor advertisement input handling.
    504  *
    505  * Based on RFC 2461
    506  * Based on RFC 2462 (duplicated address detection)
    507  *
    508  * the following items are not implemented yet:
    509  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
    510  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
    511  */
    512 void
    513 nd6_na_input(m, off, icmp6len)
    514 	struct mbuf *m;
    515 	int off, icmp6len;
    516 {
    517 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    518 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    519 	struct nd_neighbor_advert *nd_na;
    520 #if 0
    521 	struct in6_addr saddr6 = ip6->ip6_src;
    522 #endif
    523 	struct in6_addr daddr6 = ip6->ip6_dst;
    524 	struct in6_addr taddr6;
    525 	int flags;
    526 	int is_router;
    527 	int is_solicited;
    528 	int is_override;
    529 	char *lladdr = NULL;
    530 	int lladdrlen = 0;
    531 	struct ifaddr *ifa;
    532 	struct llinfo_nd6 *ln;
    533 	struct rtentry *rt;
    534 	struct sockaddr_dl *sdl;
    535 	union nd_opts ndopts;
    536 	long time_second = time.tv_sec;
    537 
    538 	if (ip6->ip6_hlim != 255) {
    539 		nd6log((LOG_ERR,
    540 		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
    541 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
    542 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
    543 		goto bad;
    544 	}
    545 
    546 #ifndef PULLDOWN_TEST
    547 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
    548 	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
    549 #else
    550 	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
    551 	if (nd_na == NULL) {
    552 		icmp6stat.icp6s_tooshort++;
    553 		return;
    554 	}
    555 #endif
    556 	taddr6 = nd_na->nd_na_target;
    557 	flags = nd_na->nd_na_flags_reserved;
    558 	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
    559 	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
    560 	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
    561 
    562 	if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
    563 		taddr6.s6_addr16[1] = htons(ifp->if_index);
    564 
    565 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
    566 		nd6log((LOG_ERR,
    567 		    "nd6_na_input: invalid target address %s\n",
    568 		    ip6_sprintf(&taddr6)));
    569 		goto bad;
    570 	}
    571 	if (IN6_IS_ADDR_MULTICAST(&daddr6))
    572 		if (is_solicited) {
    573 			nd6log((LOG_ERR,
    574 			    "nd6_na_input: a solicited adv is multicasted\n"));
    575 			goto bad;
    576 		}
    577 
    578 	icmp6len -= sizeof(*nd_na);
    579 	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
    580 	if (nd6_options(&ndopts) < 0) {
    581 		nd6log((LOG_INFO,
    582 		    "nd6_na_input: invalid ND option, ignored\n"));
    583 		/* nd6_options have incremented stats */
    584 		goto freeit;
    585 	}
    586 
    587 	if (ndopts.nd_opts_tgt_lladdr) {
    588 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
    589 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
    590 	}
    591 
    592 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
    593 
    594 	/*
    595 	 * Target address matches one of my interface address.
    596 	 *
    597 	 * If my address is tentative, this means that there's somebody
    598 	 * already using the same address as mine.  This indicates DAD failure.
    599 	 * This is defined in RFC 2462.
    600 	 *
    601 	 * Otherwise, process as defined in RFC 2461.
    602 	 */
    603 	if (ifa
    604 	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
    605 		nd6_dad_na_input(ifa);
    606 		goto freeit;
    607 	}
    608 
    609 	/* Just for safety, maybe unnecessary. */
    610 	if (ifa) {
    611 		log(LOG_ERR,
    612 		    "nd6_na_input: duplicate IP6 address %s\n",
    613 		    ip6_sprintf(&taddr6));
    614 		goto freeit;
    615 	}
    616 
    617 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    618 		nd6log((LOG_INFO,
    619 		    "nd6_na_input: lladdrlen mismatch for %s "
    620 		    "(if %d, NA packet %d)\n",
    621 			ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
    622 		goto bad;
    623 	}
    624 
    625 	/*
    626 	 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
    627 	 */
    628 	rt = nd6_lookup(&taddr6, 0, ifp);
    629 	if ((rt == NULL) ||
    630 	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
    631 	   ((sdl = SDL(rt->rt_gateway)) == NULL))
    632 		goto freeit;
    633 
    634 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
    635 		/*
    636 		 * If the link-layer has address, and no lladdr option came,
    637 		 * discard the packet.
    638 		 */
    639 		if (ifp->if_addrlen && !lladdr)
    640 			goto freeit;
    641 
    642 		/*
    643 		 * Record link-layer address, and update the state.
    644 		 */
    645 		sdl->sdl_alen = ifp->if_addrlen;
    646 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
    647 		if (is_solicited) {
    648 			ln->ln_state = ND6_LLINFO_REACHABLE;
    649 			ln->ln_byhint = 0;
    650 			if (ln->ln_expire)
    651 				ln->ln_expire = time_second +
    652 				    nd_ifinfo[rt->rt_ifp->if_index].reachable;
    653 		} else {
    654 			ln->ln_state = ND6_LLINFO_STALE;
    655 			ln->ln_expire = time_second + nd6_gctimer;
    656 		}
    657 		if ((ln->ln_router = is_router) != 0) {
    658 			/*
    659 			 * This means a router's state has changed from
    660 			 * non-reachable to probably reachable, and might
    661 			 * affect the status of associated prefixes..
    662 			 */
    663 			pfxlist_onlink_check();
    664 		}
    665 	} else {
    666 		int llchange;
    667 
    668 		/*
    669 		 * Check if the link-layer address has changed or not.
    670 		 */
    671 		if (!lladdr)
    672 			llchange = 0;
    673 		else {
    674 			if (sdl->sdl_alen) {
    675 				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
    676 					llchange = 1;
    677 				else
    678 					llchange = 0;
    679 			} else
    680 				llchange = 1;
    681 		}
    682 
    683 		/*
    684 		 * This is VERY complex.  Look at it with care.
    685 		 *
    686 		 * override solicit lladdr llchange	action
    687 		 *					(L: record lladdr)
    688 		 *
    689 		 *	0	0	n	--	(2c)
    690 		 *	0	0	y	n	(2b) L
    691 		 *	0	0	y	y	(1)    REACHABLE->STALE
    692 		 *	0	1	n	--	(2c)   *->REACHABLE
    693 		 *	0	1	y	n	(2b) L *->REACHABLE
    694 		 *	0	1	y	y	(1)    REACHABLE->STALE
    695 		 *	1	0	n	--	(2a)
    696 		 *	1	0	y	n	(2a) L
    697 		 *	1	0	y	y	(2a) L *->STALE
    698 		 *	1	1	n	--	(2a)   *->REACHABLE
    699 		 *	1	1	y	n	(2a) L *->REACHABLE
    700 		 *	1	1	y	y	(2a) L *->REACHABLE
    701 		 */
    702 		if (!is_override && (lladdr && llchange)) {	   /* (1) */
    703 			/*
    704 			 * If state is REACHABLE, make it STALE.
    705 			 * no other updates should be done.
    706 			 */
    707 			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
    708 				ln->ln_state = ND6_LLINFO_STALE;
    709 				ln->ln_expire = time_second + nd6_gctimer;
    710 			}
    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 				ln->ln_byhint = 0;
    731 				if (ln->ln_expire) {
    732 					ln->ln_expire = time_second +
    733 					    nd_ifinfo[ifp->if_index].reachable;
    734 				}
    735 			} else {
    736 				if (lladdr && llchange) {
    737 					ln->ln_state = ND6_LLINFO_STALE;
    738 					ln->ln_expire = time_second + nd6_gctimer;
    739 				}
    740 			}
    741 		}
    742 
    743 		if (ln->ln_router && !is_router) {
    744 			/*
    745 			 * The peer dropped the router flag.
    746 			 * Remove the sender from the Default Router List and
    747 			 * update the Destination Cache entries.
    748 			 */
    749 			struct nd_defrouter *dr;
    750 			struct in6_addr *in6;
    751 			int s;
    752 
    753 			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
    754 
    755 			/*
    756 			 * Lock to protect the default router list.
    757 			 * XXX: this might be unnecessary, since this function
    758 			 * is only called under the network software interrupt
    759 			 * context.  However, we keep it just for safety.
    760 			 */
    761 			s = splsoftnet();
    762 			dr = defrouter_lookup(in6, rt->rt_ifp);
    763 			if (dr)
    764 				defrtrlist_del(dr);
    765 			else if (!ip6_forwarding && ip6_accept_rtadv) {
    766 				/*
    767 				 * Even if the neighbor is not in the default
    768 				 * router list, the neighbor may be used
    769 				 * as a next hop for some destinations
    770 				 * (e.g. redirect case). So we must
    771 				 * call rt6_flush explicitly.
    772 				 */
    773 				rt6_flush(&ip6->ip6_src, rt->rt_ifp);
    774 			}
    775 			splx(s);
    776 		}
    777 		ln->ln_router = is_router;
    778 	}
    779 	rt->rt_flags &= ~RTF_REJECT;
    780 	ln->ln_asked = 0;
    781 	if (ln->ln_hold) {
    782 		/*
    783 		 * we assume ifp is not a p2p here, so just set the 2nd
    784 		 * argument as the 1st one.
    785 		 */
    786 		nd6_output(ifp, ifp, ln->ln_hold,
    787 			   (struct sockaddr_in6 *)rt_key(rt), rt);
    788 		ln->ln_hold = 0;
    789 	}
    790 
    791  freeit:
    792 	m_freem(m);
    793 	return;
    794 
    795  bad:
    796 	icmp6stat.icp6s_badna++;
    797 	m_freem(m);
    798 }
    799 
    800 /*
    801  * Neighbor advertisement output handling.
    802  *
    803  * Based on RFC 2461
    804  *
    805  * the following items are not implemented yet:
    806  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
    807  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
    808  */
    809 void
    810 nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
    811 	struct ifnet *ifp;
    812 	struct in6_addr *daddr6, *taddr6;
    813 	u_long flags;
    814 	int tlladdr;		/* 1 if include target link-layer address */
    815 	struct sockaddr *sdl0;	/* sockaddr_dl (= proxy NA) or NULL */
    816 {
    817 	struct mbuf *m;
    818 	struct ip6_hdr *ip6;
    819 	struct nd_neighbor_advert *nd_na;
    820 	struct in6_ifaddr *ia = NULL;
    821 	struct ip6_moptions im6o;
    822 	int icmp6len;
    823 	int maxlen;
    824 	caddr_t mac;
    825 	struct ifnet *outif = NULL;
    826 
    827 	/* estimate the size of message */
    828 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
    829 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    830 #ifdef DIAGNOSTIC
    831 	if (max_linkhdr + maxlen >= MCLBYTES) {
    832 		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
    833 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
    834 		panic("nd6_na_output: insufficient MCLBYTES");
    835 		/* NOTREACHED */
    836 	}
    837 #endif
    838 
    839 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    840 	if (m && max_linkhdr + maxlen >= MHLEN) {
    841 		MCLGET(m, M_DONTWAIT);
    842 		if ((m->m_flags & M_EXT) == 0) {
    843 			m_free(m);
    844 			m = NULL;
    845 		}
    846 	}
    847 	if (m == NULL)
    848 		return;
    849 	m->m_pkthdr.rcvif = NULL;
    850 
    851 	if (IN6_IS_ADDR_MULTICAST(daddr6)) {
    852 		m->m_flags |= M_MCAST;
    853 		im6o.im6o_multicast_ifp = ifp;
    854 		im6o.im6o_multicast_hlim = 255;
    855 		im6o.im6o_multicast_loop = 0;
    856 	}
    857 
    858 	icmp6len = sizeof(*nd_na);
    859 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
    860 	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
    861 
    862 	/* fill neighbor advertisement packet */
    863 	ip6 = mtod(m, struct ip6_hdr *);
    864 	ip6->ip6_flow = 0;
    865 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    866 	ip6->ip6_vfc |= IPV6_VERSION;
    867 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    868 	ip6->ip6_hlim = 255;
    869 	if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
    870 		/* reply to DAD */
    871 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    872 		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
    873 		ip6->ip6_dst.s6_addr32[1] = 0;
    874 		ip6->ip6_dst.s6_addr32[2] = 0;
    875 		ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
    876 		flags &= ~ND_NA_FLAG_SOLICITED;
    877 	} else
    878 		ip6->ip6_dst = *daddr6;
    879 
    880 	/*
    881 	 * Select a source whose scope is the same as that of the dest.
    882 	 */
    883 	ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
    884 	if (ia == NULL) {
    885 		m_freem(m);
    886 		return;
    887 	}
    888 	ip6->ip6_src = ia->ia_addr.sin6_addr;
    889 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
    890 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
    891 	nd_na->nd_na_code = 0;
    892 	nd_na->nd_na_target = *taddr6;
    893 	if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
    894 		nd_na->nd_na_target.s6_addr16[1] = 0;
    895 
    896 	/*
    897 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
    898 	 * see nd6_ns_input() for details.
    899 	 * Basically, if NS packet is sent to unicast/anycast addr,
    900 	 * target lladdr option SHOULD NOT be included.
    901 	 */
    902 	if (tlladdr) {
    903 		mac = NULL;
    904 		/*
    905 		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
    906 		 * lladdr in sdl0.  If we are not proxying (sending NA for
    907 		 * my address) use lladdr configured for the interface.
    908 		 */
    909 		if (sdl0 == NULL)
    910 			mac = nd6_ifptomac(ifp);
    911 		else if (sdl0->sa_family == AF_LINK) {
    912 			struct sockaddr_dl *sdl;
    913 			sdl = (struct sockaddr_dl *)sdl0;
    914 			if (sdl->sdl_alen == ifp->if_addrlen)
    915 				mac = LLADDR(sdl);
    916 		}
    917 	}
    918 	if (tlladdr && mac) {
    919 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    920 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
    921 
    922 		/* roundup to 8 bytes alignment! */
    923 		optlen = (optlen + 7) & ~7;
    924 
    925 		m->m_pkthdr.len += optlen;
    926 		m->m_len += optlen;
    927 		icmp6len += optlen;
    928 		bzero((caddr_t)nd_opt, optlen);
    929 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
    930 		nd_opt->nd_opt_len = optlen >> 3;
    931 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
    932 	} else
    933 		flags &= ~ND_NA_FLAG_OVERRIDE;
    934 
    935 	ip6->ip6_plen = htons((u_short)icmp6len);
    936 	nd_na->nd_na_flags_reserved = flags;
    937 	nd_na->nd_na_cksum = 0;
    938 	nd_na->nd_na_cksum =
    939 		in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
    940 
    941 #ifdef IPSEC
    942 	/* Don't lookup socket */
    943 	(void)ipsec_setsocket(m, NULL);
    944 #endif
    945 	ip6_output(m, NULL, NULL, 0, &im6o, &outif);
    946 	if (outif) {
    947 		icmp6_ifstat_inc(outif, ifs6_out_msg);
    948 		icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
    949 	}
    950 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
    951 }
    952 
    953 caddr_t
    954 nd6_ifptomac(ifp)
    955 	struct ifnet *ifp;
    956 {
    957 	switch (ifp->if_type) {
    958 	case IFT_ARCNET:
    959 	case IFT_ETHER:
    960 	case IFT_FDDI:
    961 	case IFT_IEEE1394:
    962 		return LLADDR(ifp->if_sadl);
    963 		break;
    964 	default:
    965 		return NULL;
    966 	}
    967 }
    968 
    969 TAILQ_HEAD(dadq_head, dadq);
    970 struct dadq {
    971 	TAILQ_ENTRY(dadq) dad_list;
    972 	struct ifaddr *dad_ifa;
    973 	int dad_count;		/* max NS to send */
    974 	int dad_ns_tcount;	/* # of trials to send NS */
    975 	int dad_ns_ocount;	/* NS sent so far */
    976 	int dad_ns_icount;
    977 	int dad_na_icount;
    978 	struct callout dad_timer_ch;
    979 };
    980 
    981 static struct dadq_head dadq;
    982 static int dad_init = 0;
    983 
    984 static struct dadq *
    985 nd6_dad_find(ifa)
    986 	struct ifaddr *ifa;
    987 {
    988 	struct dadq *dp;
    989 
    990 	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
    991 		if (dp->dad_ifa == ifa)
    992 			return dp;
    993 	}
    994 	return NULL;
    995 }
    996 
    997 static void
    998 nd6_dad_starttimer(dp, ticks)
    999 	struct dadq *dp;
   1000 	int ticks;
   1001 {
   1002 
   1003 	callout_reset(&dp->dad_timer_ch, ticks,
   1004 	    (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
   1005 }
   1006 
   1007 static void
   1008 nd6_dad_stoptimer(dp)
   1009 	struct dadq *dp;
   1010 {
   1011 
   1012 	callout_stop(&dp->dad_timer_ch);
   1013 }
   1014 
   1015 /*
   1016  * Start Duplicated Address Detection (DAD) for specified interface address.
   1017  */
   1018 void
   1019 nd6_dad_start(ifa, tick)
   1020 	struct ifaddr *ifa;
   1021 	int *tick;	/* minimum delay ticks for IFF_UP event */
   1022 {
   1023 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1024 	struct dadq *dp;
   1025 
   1026 	if (!dad_init) {
   1027 		TAILQ_INIT(&dadq);
   1028 		dad_init++;
   1029 	}
   1030 
   1031 	/*
   1032 	 * If we don't need DAD, don't do it.
   1033 	 * There are several cases:
   1034 	 * - DAD is disabled (ip6_dad_count == 0)
   1035 	 * - the interface address is anycast
   1036 	 */
   1037 	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
   1038 		log(LOG_DEBUG,
   1039 			"nd6_dad_start: called with non-tentative address "
   1040 			"%s(%s)\n",
   1041 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1042 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1043 		return;
   1044 	}
   1045 	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
   1046 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1047 		return;
   1048 	}
   1049 	if (!ip6_dad_count) {
   1050 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1051 		return;
   1052 	}
   1053 	if (!ifa->ifa_ifp)
   1054 		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
   1055 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
   1056 		return;
   1057 	if (nd6_dad_find(ifa) != NULL) {
   1058 		/* DAD already in progress */
   1059 		return;
   1060 	}
   1061 
   1062 	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
   1063 	if (dp == NULL) {
   1064 		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
   1065 			"%s(%s)\n",
   1066 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1067 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1068 		return;
   1069 	}
   1070 	bzero(dp, sizeof(*dp));
   1071 	callout_init(&dp->dad_timer_ch);
   1072 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
   1073 
   1074 	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
   1075 	    ip6_sprintf(&ia->ia_addr.sin6_addr)));
   1076 
   1077 	/*
   1078 	 * Send NS packet for DAD, ip6_dad_count times.
   1079 	 * Note that we must delay the first transmission, if this is the
   1080 	 * first packet to be sent from the interface after interface
   1081 	 * (re)initialization.
   1082 	 */
   1083 	dp->dad_ifa = ifa;
   1084 	IFAREF(ifa);	/* just for safety */
   1085 	dp->dad_count = ip6_dad_count;
   1086 	dp->dad_ns_icount = dp->dad_na_icount = 0;
   1087 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
   1088 	if (tick == NULL) {
   1089 		nd6_dad_ns_output(dp, ifa);
   1090 		nd6_dad_starttimer(dp,
   1091 		    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
   1092 	} else {
   1093 		int ntick;
   1094 
   1095 		if (*tick == 0)
   1096 			ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
   1097 		else
   1098 			ntick = *tick + random() % (hz / 2);
   1099 		*tick = ntick;
   1100 		nd6_dad_starttimer(dp, ntick);
   1101 	}
   1102 }
   1103 
   1104 /*
   1105  * terminate DAD unconditionally.  used for address removals.
   1106  */
   1107 void
   1108 nd6_dad_stop(ifa)
   1109 	struct ifaddr *ifa;
   1110 {
   1111 	struct dadq *dp;
   1112 
   1113 	if (!dad_init)
   1114 		return;
   1115 	dp = nd6_dad_find(ifa);
   1116 	if (!dp) {
   1117 		/* DAD wasn't started yet */
   1118 		return;
   1119 	}
   1120 
   1121 	nd6_dad_stoptimer(dp);
   1122 
   1123 	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1124 	free(dp, M_IP6NDP);
   1125 	dp = NULL;
   1126 	IFAFREE(ifa);
   1127 }
   1128 
   1129 static void
   1130 nd6_dad_timer(ifa)
   1131 	struct ifaddr *ifa;
   1132 {
   1133 	int s;
   1134 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1135 	struct dadq *dp;
   1136 
   1137 	s = splsoftnet();	/* XXX */
   1138 
   1139 	/* Sanity check */
   1140 	if (ia == NULL) {
   1141 		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
   1142 		goto done;
   1143 	}
   1144 	dp = nd6_dad_find(ifa);
   1145 	if (dp == NULL) {
   1146 		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
   1147 		goto done;
   1148 	}
   1149 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
   1150 		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
   1151 			"%s(%s)\n",
   1152 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1153 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1154 		goto done;
   1155 	}
   1156 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
   1157 		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
   1158 			"%s(%s)\n",
   1159 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1160 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1161 		goto done;
   1162 	}
   1163 
   1164 	/* timeouted with IFF_{RUNNING,UP} check */
   1165 	if (dp->dad_ns_tcount > dad_maxtry) {
   1166 		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
   1167 			if_name(ifa->ifa_ifp)));
   1168 
   1169 		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1170 		free(dp, M_IP6NDP);
   1171 		dp = NULL;
   1172 		IFAFREE(ifa);
   1173 		goto done;
   1174 	}
   1175 
   1176 	/* Need more checks? */
   1177 	if (dp->dad_ns_ocount < dp->dad_count) {
   1178 		/*
   1179 		 * We have more NS to go.  Send NS packet for DAD.
   1180 		 */
   1181 		nd6_dad_ns_output(dp, ifa);
   1182 		nd6_dad_starttimer(dp,
   1183 		    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
   1184 	} else {
   1185 		/*
   1186 		 * We have transmitted sufficient number of DAD packets.
   1187 		 * See what we've got.
   1188 		 */
   1189 		int duplicate;
   1190 
   1191 		duplicate = 0;
   1192 
   1193 		if (dp->dad_na_icount) {
   1194 			/*
   1195 			 * the check is in nd6_dad_na_input(),
   1196 			 * but just in case
   1197 			 */
   1198 			duplicate++;
   1199 		}
   1200 
   1201 		if (dp->dad_ns_icount) {
   1202 #if 0 /* heuristics */
   1203 			/*
   1204 			 * if
   1205 			 * - we have sent many(?) DAD NS, and
   1206 			 * - the number of NS we sent equals to the
   1207 			 *   number of NS we've got, and
   1208 			 * - we've got no NA
   1209 			 * we may have a faulty network card/driver which
   1210 			 * loops back multicasts to myself.
   1211 			 */
   1212 			if (3 < dp->dad_count
   1213 			 && dp->dad_ns_icount == dp->dad_count
   1214 			 && dp->dad_na_icount == 0) {
   1215 				log(LOG_INFO, "DAD questionable for %s(%s): "
   1216 					"network card loops back multicast?\n",
   1217 					ip6_sprintf(&ia->ia_addr.sin6_addr),
   1218 					if_name(ifa->ifa_ifp));
   1219 				/* XXX consider it a duplicate or not? */
   1220 				/* duplicate++; */
   1221 			} else {
   1222 				/* We've seen NS, means DAD has failed. */
   1223 				duplicate++;
   1224 			}
   1225 #else
   1226 			/* We've seen NS, means DAD has failed. */
   1227 			duplicate++;
   1228 #endif
   1229 		}
   1230 
   1231 		if (duplicate) {
   1232 			/* (*dp) will be freed in nd6_dad_duplicated() */
   1233 			dp = NULL;
   1234 			nd6_dad_duplicated(ifa);
   1235 		} else {
   1236 			/*
   1237 			 * We are done with DAD.  No NA came, no NS came.
   1238 			 * duplicated address found.
   1239 			 */
   1240 			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1241 
   1242 			nd6log((LOG_DEBUG,
   1243 			    "%s: DAD complete for %s - no duplicates found\n",
   1244 			    if_name(ifa->ifa_ifp),
   1245 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
   1246 
   1247 			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1248 			free(dp, M_IP6NDP);
   1249 			dp = NULL;
   1250 			IFAFREE(ifa);
   1251 		}
   1252 	}
   1253 
   1254 done:
   1255 	splx(s);
   1256 }
   1257 
   1258 void
   1259 nd6_dad_duplicated(ifa)
   1260 	struct ifaddr *ifa;
   1261 {
   1262 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1263 	struct dadq *dp;
   1264 
   1265 	dp = nd6_dad_find(ifa);
   1266 	if (dp == NULL) {
   1267 		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
   1268 		return;
   1269 	}
   1270 
   1271 	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
   1272 	    "NS in/out=%d/%d, NA in=%d\n",
   1273 	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
   1274 	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
   1275 
   1276 	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1277 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
   1278 
   1279 	/* We are done with DAD, with duplicated address found. (failure) */
   1280 	nd6_dad_stoptimer(dp);
   1281 
   1282 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
   1283 	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
   1284 	log(LOG_ERR, "%s: manual intervention required\n",
   1285 	    if_name(ifa->ifa_ifp));
   1286 
   1287 	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1288 	free(dp, M_IP6NDP);
   1289 	dp = NULL;
   1290 	IFAFREE(ifa);
   1291 }
   1292 
   1293 static void
   1294 nd6_dad_ns_output(dp, ifa)
   1295 	struct dadq *dp;
   1296 	struct ifaddr *ifa;
   1297 {
   1298 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1299 	struct ifnet *ifp = ifa->ifa_ifp;
   1300 
   1301 	dp->dad_ns_tcount++;
   1302 	if ((ifp->if_flags & IFF_UP) == 0) {
   1303 #if 0
   1304 		printf("%s: interface down?\n", if_name(ifp));
   1305 #endif
   1306 		return;
   1307 	}
   1308 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1309 #if 0
   1310 		printf("%s: interface not running?\n", if_name(ifp));
   1311 #endif
   1312 		return;
   1313 	}
   1314 
   1315 	dp->dad_ns_ocount++;
   1316 	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
   1317 }
   1318 
   1319 static void
   1320 nd6_dad_ns_input(ifa)
   1321 	struct ifaddr *ifa;
   1322 {
   1323 	struct in6_ifaddr *ia;
   1324 	struct ifnet *ifp;
   1325 	struct in6_addr *taddr6;
   1326 	struct dadq *dp;
   1327 	int duplicate;
   1328 
   1329 	if (!ifa)
   1330 		panic("ifa == NULL in nd6_dad_ns_input");
   1331 
   1332 	ia = (struct in6_ifaddr *)ifa;
   1333 	ifp = ifa->ifa_ifp;
   1334 	taddr6 = &ia->ia_addr.sin6_addr;
   1335 	duplicate = 0;
   1336 	dp = nd6_dad_find(ifa);
   1337 
   1338 	/* Quickhack - completely ignore DAD NS packets */
   1339 	if (dad_ignore_ns) {
   1340 		nd6log((LOG_INFO,
   1341 		    "nd6_dad_ns_input: ignoring DAD NS packet for "
   1342 		    "address %s(%s)\n", ip6_sprintf(taddr6),
   1343 		    if_name(ifa->ifa_ifp)));
   1344 		return;
   1345 	}
   1346 
   1347 	/*
   1348 	 * if I'm yet to start DAD, someone else started using this address
   1349 	 * first.  I have a duplicate and you win.
   1350 	 */
   1351 	if (!dp || dp->dad_ns_ocount == 0)
   1352 		duplicate++;
   1353 
   1354 	/* XXX more checks for loopback situation - see nd6_dad_timer too */
   1355 
   1356 	if (duplicate) {
   1357 		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
   1358 		nd6_dad_duplicated(ifa);
   1359 	} else {
   1360 		/*
   1361 		 * not sure if I got a duplicate.
   1362 		 * increment ns count and see what happens.
   1363 		 */
   1364 		if (dp)
   1365 			dp->dad_ns_icount++;
   1366 	}
   1367 }
   1368 
   1369 static void
   1370 nd6_dad_na_input(ifa)
   1371 	struct ifaddr *ifa;
   1372 {
   1373 	struct dadq *dp;
   1374 
   1375 	if (!ifa)
   1376 		panic("ifa == NULL in nd6_dad_na_input");
   1377 
   1378 	dp = nd6_dad_find(ifa);
   1379 	if (dp)
   1380 		dp->dad_na_icount++;
   1381 
   1382 	/* remove the address. */
   1383 	nd6_dad_duplicated(ifa);
   1384 }
   1385