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