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