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