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