nd6_nbr.c revision 191816
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $ 3053541Sshin */ 3153541Sshin 32174510Sobrien#include <sys/cdefs.h> 33174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/nd6_nbr.c 191816 2009-05-05 10:56:12Z zec $"); 34174510Sobrien 3562587Sitojun#include "opt_inet.h" 3662587Sitojun#include "opt_inet6.h" 37142215Sglebius#include "opt_ipsec.h" 38142215Sglebius#include "opt_carp.h" 39178167Sqingli#include "opt_mpath.h" 4055009Sshin 4153541Sshin#include <sys/param.h> 4253541Sshin#include <sys/systm.h> 4353541Sshin#include <sys/malloc.h> 44186119Sqingli#include <sys/lock.h> 45186119Sqingli#include <sys/rwlock.h> 4653541Sshin#include <sys/mbuf.h> 4753541Sshin#include <sys/socket.h> 4853541Sshin#include <sys/sockio.h> 4953541Sshin#include <sys/time.h> 5053541Sshin#include <sys/kernel.h> 5153541Sshin#include <sys/errno.h> 5253541Sshin#include <sys/syslog.h> 5353541Sshin#include <sys/queue.h> 5478064Sume#include <sys/callout.h> 55181803Sbz#include <sys/vimage.h> 5653541Sshin 5753541Sshin#include <net/if.h> 5853541Sshin#include <net/if_types.h> 5953541Sshin#include <net/if_dl.h> 60147306Sbrooks#include <net/if_var.h> 6153541Sshin#include <net/route.h> 62178167Sqingli#ifdef RADIX_MPATH 63178167Sqingli#include <net/radix_mpath.h> 64178167Sqingli#endif 6553541Sshin 6653541Sshin#include <netinet/in.h> 6753541Sshin#include <netinet/in_var.h> 68186119Sqingli#include <net/if_llatbl.h> 69186119Sqingli#define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le)) 7053541Sshin#include <netinet6/in6_var.h> 71151477Ssuz#include <netinet6/in6_ifattach.h> 7262587Sitojun#include <netinet/ip6.h> 7353541Sshin#include <netinet6/ip6_var.h> 74148385Sume#include <netinet6/scope6_var.h> 7553541Sshin#include <netinet6/nd6.h> 7662587Sitojun#include <netinet/icmp6.h> 77185571Sbz#include <netinet6/vinet6.h> 7853541Sshin 79142215Sglebius#ifdef DEV_CARP 80142215Sglebius#include <netinet/ip_carp.h> 81142215Sglebius#endif 82142215Sglebius 8362587Sitojun#define SDL(s) ((struct sockaddr_dl *)s) 8453541Sshin 8562587Sitojunstruct dadq; 86175162Sobrienstatic struct dadq *nd6_dad_find(struct ifaddr *); 87175162Sobrienstatic void nd6_dad_starttimer(struct dadq *, int); 88175162Sobrienstatic void nd6_dad_stoptimer(struct dadq *); 89191816Szecstatic void nd6_dad_timer(struct dadq *); 90175162Sobrienstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *); 91175162Sobrienstatic void nd6_dad_ns_input(struct ifaddr *); 92175162Sobrienstatic void nd6_dad_na_input(struct ifaddr *); 9353541Sshin 94185088Szec#ifdef VIMAGE_GLOBALS 95185088Szecint dad_ignore_ns; 96185088Szecint dad_maxtry; 97185088Szec#endif 9853541Sshin 9953541Sshin/* 100108470Sschweikh * Input a Neighbor Solicitation Message. 10153541Sshin * 10253541Sshin * Based on RFC 2461 103148987Sume * Based on RFC 2462 (duplicate address detection) 10453541Sshin */ 10553541Sshinvoid 106171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len) 10753541Sshin{ 108183550Szec INIT_VNET_INET6(curvnet); 10953541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 11053541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 11162587Sitojun struct nd_neighbor_solicit *nd_ns; 11253541Sshin struct in6_addr saddr6 = ip6->ip6_src; 11353541Sshin struct in6_addr daddr6 = ip6->ip6_dst; 11462587Sitojun struct in6_addr taddr6; 11553541Sshin struct in6_addr myaddr6; 11653541Sshin char *lladdr = NULL; 117142215Sglebius struct ifaddr *ifa = NULL; 11853541Sshin int lladdrlen = 0; 11953541Sshin int anycast = 0, proxy = 0, tentative = 0; 12053541Sshin int tlladdr; 12153541Sshin union nd_opts ndopts; 12262587Sitojun struct sockaddr_dl *proxydl = NULL; 123165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 12453541Sshin 12578064Sume#ifndef PULLDOWN_TEST 12678064Sume IP6_EXTHDR_CHECK(m, off, icmp6len,); 12778064Sume nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off); 12878064Sume#else 12978064Sume IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len); 13078064Sume if (nd_ns == NULL) { 131190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 13278064Sume return; 13378064Sume } 13478064Sume#endif 13578064Sume ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */ 13678064Sume taddr6 = nd_ns->nd_ns_target; 137148385Sume if (in6_setscope(&taddr6, ifp, NULL) != 0) 138148385Sume goto bad; 13978064Sume 14053541Sshin if (ip6->ip6_hlim != 255) { 14178064Sume nd6log((LOG_ERR, 14278064Sume "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n", 143165118Sbz ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 144165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 14578064Sume goto bad; 14653541Sshin } 14753541Sshin 14853541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 149148987Sume /* dst has to be a solicited node multicast address. */ 150120941Sume if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL && 15195023Ssuz /* don't check ifindex portion */ 152120941Sume daddr6.s6_addr32[1] == 0 && 153120941Sume daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE && 154120941Sume daddr6.s6_addr8[12] == 0xff) { 15595023Ssuz ; /* good */ 15653541Sshin } else { 15778064Sume nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " 158120941Sume "(wrong ip6 dst)\n")); 15953541Sshin goto bad; 16053541Sshin } 161185348Szec } else if (!V_nd6_onlink_ns_rfc4861) { 162183529Scperciva struct sockaddr_in6 src_sa6; 163183529Scperciva 164183529Scperciva /* 165183529Scperciva * According to recent IETF discussions, it is not a good idea 166183529Scperciva * to accept a NS from an address which would not be deemed 167183529Scperciva * to be a neighbor otherwise. This point is expected to be 168183529Scperciva * clarified in future revisions of the specification. 169183529Scperciva */ 170183529Scperciva bzero(&src_sa6, sizeof(src_sa6)); 171183529Scperciva src_sa6.sin6_family = AF_INET6; 172183529Scperciva src_sa6.sin6_len = sizeof(src_sa6); 173183529Scperciva src_sa6.sin6_addr = saddr6; 174186119Sqingli if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) { 175183529Scperciva nd6log((LOG_INFO, "nd6_ns_input: " 176183529Scperciva "NS packet from non-neighbor\n")); 177183529Scperciva goto bad; 178183529Scperciva } 17953541Sshin } 18053541Sshin 18153541Sshin if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 18278064Sume nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n")); 18353541Sshin goto bad; 18453541Sshin } 18553541Sshin 18653541Sshin icmp6len -= sizeof(*nd_ns); 18753541Sshin nd6_option_init(nd_ns + 1, icmp6len, &ndopts); 18853541Sshin if (nd6_options(&ndopts) < 0) { 18978064Sume nd6log((LOG_INFO, 19078064Sume "nd6_ns_input: invalid ND option, ignored\n")); 19178064Sume /* nd6_options have incremented stats */ 19278064Sume goto freeit; 19353541Sshin } 19453541Sshin 19553541Sshin if (ndopts.nd_opts_src_lladdr) { 19695023Ssuz lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 19753541Sshin lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 19853541Sshin } 199120941Sume 20053541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) { 20178064Sume nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " 20278064Sume "(link-layer address option)\n")); 20353541Sshin goto bad; 20453541Sshin } 20553541Sshin 20653541Sshin /* 20753541Sshin * Attaching target link-layer address to the NA? 20853541Sshin * (RFC 2461 7.2.4) 20953541Sshin * 21053541Sshin * NS IP dst is unicast/anycast MUST NOT add 21153541Sshin * NS IP dst is solicited-node multicast MUST add 21253541Sshin * 21353541Sshin * In implementation, we add target link-layer address by default. 21453541Sshin * We do not add one in MUST NOT cases. 21553541Sshin */ 21653541Sshin if (!IN6_IS_ADDR_MULTICAST(&daddr6)) 21753541Sshin tlladdr = 0; 21853541Sshin else 21953541Sshin tlladdr = 1; 22053541Sshin 22153541Sshin /* 22253541Sshin * Target address (taddr6) must be either: 22353541Sshin * (1) Valid unicast/anycast address for my receiving interface, 22453541Sshin * (2) Unicast address for which I'm offering proxy service, or 22553541Sshin * (3) "tentative" address on which DAD is being performed. 22653541Sshin */ 22753541Sshin /* (1) and (3) check. */ 228142215Sglebius#ifdef DEV_CARP 229142215Sglebius if (ifp->if_carp) 230142215Sglebius ifa = carp_iamatch6(ifp->if_carp, &taddr6); 231151465Ssuz if (ifa == NULL) 232142215Sglebius ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 233142215Sglebius#else 23453541Sshin ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 235142215Sglebius#endif 23653541Sshin 23753541Sshin /* (2) check. */ 238151465Ssuz if (ifa == NULL) { 23953541Sshin struct rtentry *rt; 24053541Sshin struct sockaddr_in6 tsin6; 241121765Ssam int need_proxy; 242178167Sqingli#ifdef RADIX_MPATH 243178167Sqingli struct route_in6 ro; 244178167Sqingli#endif 24553541Sshin 246171260Sdelphij bzero(&tsin6, sizeof tsin6); 24753541Sshin tsin6.sin6_len = sizeof(struct sockaddr_in6); 24853541Sshin tsin6.sin6_family = AF_INET6; 24953541Sshin tsin6.sin6_addr = taddr6; 25053541Sshin 251178167Sqingli#ifdef RADIX_MPATH 252178167Sqingli bzero(&ro, sizeof(ro)); 253178167Sqingli ro.ro_dst = tsin6; 254178167Sqingli rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE); 255178167Sqingli rt = ro.ro_rt; 256178167Sqingli#else 25753541Sshin rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0); 258178167Sqingli#endif 259121765Ssam need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 && 260121765Ssam rt->rt_gateway->sa_family == AF_LINK); 261121765Ssam if (rt) 262187946Sbz RTFREE_LOCKED(rt); 263121765Ssam if (need_proxy) { 26453541Sshin /* 26562587Sitojun * proxy NDP for single entry 26653541Sshin */ 26762587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 26862587Sitojun IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 26962587Sitojun if (ifa) { 27053541Sshin proxy = 1; 27162587Sitojun proxydl = SDL(rt->rt_gateway); 27262587Sitojun } 27353541Sshin } 27453541Sshin } 275151479Ssuz if (ifa == NULL) { 27653541Sshin /* 27778064Sume * We've got an NS packet, and we don't have that adddress 27853541Sshin * assigned for us. We MUST silently ignore it. 27953541Sshin * See RFC2461 7.2.3. 28053541Sshin */ 28162587Sitojun goto freeit; 28253541Sshin } 28353541Sshin myaddr6 = *IFA_IN6(ifa); 28453541Sshin anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST; 28553541Sshin tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE; 28653541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) 28762587Sitojun goto freeit; 28853541Sshin 28953541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 290120941Sume nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s " 29153541Sshin "(if %d, NS packet %d)\n", 292165118Sbz ip6_sprintf(ip6bufs, &taddr6), 293120941Sume ifp->if_addrlen, lladdrlen - 2)); 29478064Sume goto bad; 29553541Sshin } 29653541Sshin 29753541Sshin if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { 298120941Sume nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n", 299165118Sbz ip6_sprintf(ip6bufs, &saddr6))); 30062587Sitojun goto freeit; 30153541Sshin } 30253541Sshin 30353541Sshin /* 30453541Sshin * We have neighbor solicitation packet, with target address equals to 30553541Sshin * one of my tentative address. 30653541Sshin * 30753541Sshin * src addr how to process? 30853541Sshin * --- --- 30953541Sshin * multicast of course, invalid (rejected in ip6_input) 31053541Sshin * unicast somebody is doing address resolution -> ignore 31153541Sshin * unspec dup address detection 31253541Sshin * 31353541Sshin * The processing is defined in RFC 2462. 31453541Sshin */ 31553541Sshin if (tentative) { 31653541Sshin /* 31753541Sshin * If source address is unspecified address, it is for 318148987Sume * duplicate address detection. 31953541Sshin * 32053541Sshin * If not, the packet is for addess resolution; 32153541Sshin * silently ignore it. 32253541Sshin */ 32353541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 32453541Sshin nd6_dad_ns_input(ifa); 32553541Sshin 32662587Sitojun goto freeit; 32753541Sshin } 32853541Sshin 32953541Sshin /* 33053541Sshin * If the source address is unspecified address, entries must not 33153541Sshin * be created or updated. 33253541Sshin * It looks that sender is performing DAD. Output NA toward 33353541Sshin * all-node multicast address, to tell the sender that I'm using 33453541Sshin * the address. 33553541Sshin * S bit ("solicited") must be zero. 33653541Sshin */ 33753541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 338148385Sume struct in6_addr in6_all; 339148385Sume 340148385Sume in6_all = in6addr_linklocal_allnodes; 341148385Sume if (in6_setscope(&in6_all, ifp, NULL) != 0) 342148385Sume goto bad; 343148385Sume nd6_na_output(ifp, &in6_all, &taddr6, 344120941Sume ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 345181803Sbz (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0), 346120941Sume tlladdr, (struct sockaddr *)proxydl); 34762587Sitojun goto freeit; 34853541Sshin } 34953541Sshin 350120941Sume nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, 351120941Sume ND_NEIGHBOR_SOLICIT, 0); 35253541Sshin 35353541Sshin nd6_na_output(ifp, &saddr6, &taddr6, 354120941Sume ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 355181803Sbz (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED, 356120941Sume tlladdr, (struct sockaddr *)proxydl); 35762587Sitojun freeit: 35862587Sitojun m_freem(m); 35953541Sshin return; 36053541Sshin 36153541Sshin bad: 362165118Sbz nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", 363165118Sbz ip6_sprintf(ip6bufs, &saddr6))); 364165118Sbz nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", 365165118Sbz ip6_sprintf(ip6bufs, &daddr6))); 366165118Sbz nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", 367165118Sbz ip6_sprintf(ip6bufs, &taddr6))); 368190964Srwatson ICMP6STAT_INC(icp6s_badns); 36962587Sitojun m_freem(m); 37053541Sshin} 37153541Sshin 37253541Sshin/* 373108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies: 37453541Sshin * - ICMP6 header source IP6 address 37553541Sshin * - ND6 header target IP6 address 37653541Sshin * - ND6 header source datalink address 37753541Sshin * 37853541Sshin * Based on RFC 2461 379148987Sume * Based on RFC 2462 (duplicate address detection) 380171259Sdelphij * 381171259Sdelphij * ln - for source address determination 382171259Sdelphij * dad - duplicate address detection 38353541Sshin */ 38453541Sshinvoid 385186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6, 386186119Sqingli const struct in6_addr *taddr6, struct llentry *ln, int dad) 38753541Sshin{ 388183550Szec INIT_VNET_INET6(ifp->if_vnet); 38953541Sshin struct mbuf *m; 39053541Sshin struct ip6_hdr *ip6; 39153541Sshin struct nd_neighbor_solicit *nd_ns; 392148385Sume struct in6_addr *src, src_in; 39353541Sshin struct ip6_moptions im6o; 39453541Sshin int icmp6len; 39562587Sitojun int maxlen; 39653541Sshin caddr_t mac; 397148385Sume struct route_in6 ro; 398120941Sume 399148385Sume bzero(&ro, sizeof(ro)); 400148385Sume 40153541Sshin if (IN6_IS_ADDR_MULTICAST(taddr6)) 40253541Sshin return; 40353541Sshin 40462587Sitojun /* estimate the size of message */ 40562587Sitojun maxlen = sizeof(*ip6) + sizeof(*nd_ns); 40662587Sitojun maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 40762587Sitojun if (max_linkhdr + maxlen >= MCLBYTES) { 40862587Sitojun#ifdef DIAGNOSTIC 40962587Sitojun printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES " 41062587Sitojun "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 41162587Sitojun#endif 41253541Sshin return; 41362587Sitojun } 41453541Sshin 415111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 41662587Sitojun if (m && max_linkhdr + maxlen >= MHLEN) { 417111119Simp MCLGET(m, M_DONTWAIT); 41862587Sitojun if ((m->m_flags & M_EXT) == 0) { 41962587Sitojun m_free(m); 42062587Sitojun m = NULL; 42162587Sitojun } 42262587Sitojun } 42362587Sitojun if (m == NULL) 42462587Sitojun return; 42578064Sume m->m_pkthdr.rcvif = NULL; 42662587Sitojun 42753541Sshin if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) { 42853541Sshin m->m_flags |= M_MCAST; 42953541Sshin im6o.im6o_multicast_ifp = ifp; 43053541Sshin im6o.im6o_multicast_hlim = 255; 43153541Sshin im6o.im6o_multicast_loop = 0; 43253541Sshin } 43353541Sshin 43453541Sshin icmp6len = sizeof(*nd_ns); 43553541Sshin m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; 43695023Ssuz m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 43753541Sshin 43853541Sshin /* fill neighbor solicitation packet */ 43953541Sshin ip6 = mtod(m, struct ip6_hdr *); 44053541Sshin ip6->ip6_flow = 0; 44162587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 44262587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 44353541Sshin /* ip6->ip6_plen will be set later */ 44453541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 44553541Sshin ip6->ip6_hlim = 255; 44653541Sshin if (daddr6) 44753541Sshin ip6->ip6_dst = *daddr6; 44853541Sshin else { 44953541Sshin ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 450148385Sume ip6->ip6_dst.s6_addr16[1] = 0; 45153541Sshin ip6->ip6_dst.s6_addr32[1] = 0; 45253541Sshin ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; 45353541Sshin ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; 45453541Sshin ip6->ip6_dst.s6_addr8[12] = 0xff; 455148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 456148385Sume goto bad; 45753541Sshin } 45853541Sshin if (!dad) { 45953541Sshin /* 46053541Sshin * RFC2461 7.2.2: 46153541Sshin * "If the source address of the packet prompting the 46253541Sshin * solicitation is the same as one of the addresses assigned 46353541Sshin * to the outgoing interface, that address SHOULD be placed 46453541Sshin * in the IP Source Address of the outgoing solicitation. 46553541Sshin * Otherwise, any one of the addresses assigned to the 46653541Sshin * interface should be used." 46753541Sshin * 46853541Sshin * We use the source address for the prompting packet 46953541Sshin * (saddr6), if: 47053541Sshin * - saddr6 is given from the caller (by giving "ln"), and 47153541Sshin * - saddr6 belongs to the outgoing interface. 472148385Sume * Otherwise, we perform the source address selection as usual. 47353541Sshin */ 47495023Ssuz struct ip6_hdr *hip6; /* hold ip6 */ 475148385Sume struct in6_addr *hsrc = NULL; 47653541Sshin 477186160Skmacy if ((ln != NULL) && ln->la_hold) { 478148987Sume /* 479186119Sqingli * assuming every packet in la_hold has the same IP 480148987Sume * header 481148987Sume */ 482186119Sqingli hip6 = mtod(ln->la_hold, struct ip6_hdr *); 48353541Sshin /* XXX pullup? */ 484186119Sqingli if (sizeof(*hip6) < ln->la_hold->m_len) 485148385Sume hsrc = &hip6->ip6_src; 48653541Sshin else 487148385Sume hsrc = NULL; 488148385Sume } 489148385Sume if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc)) 490148385Sume src = hsrc; 49153541Sshin else { 492148385Sume int error; 493148385Sume struct sockaddr_in6 dst_sa; 494148385Sume 495148385Sume bzero(&dst_sa, sizeof(dst_sa)); 496148385Sume dst_sa.sin6_family = AF_INET6; 497148385Sume dst_sa.sin6_len = sizeof(dst_sa); 498148385Sume dst_sa.sin6_addr = ip6->ip6_dst; 499148385Sume 500148385Sume src = in6_selectsrc(&dst_sa, NULL, 501148385Sume NULL, &ro, NULL, NULL, &error); 502148385Sume if (src == NULL) { 503165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 504148385Sume nd6log((LOG_DEBUG, 505148385Sume "nd6_ns_output: source can't be " 506148385Sume "determined: dst=%s, error=%d\n", 507165118Sbz ip6_sprintf(ip6buf, &dst_sa.sin6_addr), 508165118Sbz error)); 509148385Sume goto bad; 51053541Sshin } 51153541Sshin } 51253541Sshin } else { 51353541Sshin /* 51453541Sshin * Source address for DAD packet must always be IPv6 51553541Sshin * unspecified address. (0::0) 516148385Sume * We actually don't have to 0-clear the address (we did it 517148385Sume * above), but we do so here explicitly to make the intention 518148385Sume * clearer. 51953541Sshin */ 520148385Sume bzero(&src_in, sizeof(src_in)); 521148385Sume src = &src_in; 52253541Sshin } 523148385Sume ip6->ip6_src = *src; 52453541Sshin nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1); 52553541Sshin nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; 52653541Sshin nd_ns->nd_ns_code = 0; 52753541Sshin nd_ns->nd_ns_reserved = 0; 52853541Sshin nd_ns->nd_ns_target = *taddr6; 529121315Sume in6_clearscope(&nd_ns->nd_ns_target); /* XXX */ 53053541Sshin 53153541Sshin /* 53253541Sshin * Add source link-layer address option. 53353541Sshin * 53453541Sshin * spec implementation 53553541Sshin * --- --- 53653541Sshin * DAD packet MUST NOT do not add the option 53753541Sshin * there's no link layer address: 53853541Sshin * impossible do not add the option 53953541Sshin * there's link layer address: 54053541Sshin * Multicast NS MUST add one add the option 54153541Sshin * Unicast NS SHOULD add one add the option 54253541Sshin */ 54353541Sshin if (!dad && (mac = nd6_ifptomac(ifp))) { 54453541Sshin int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 54553541Sshin struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); 54653541Sshin /* 8 byte alignments... */ 54753541Sshin optlen = (optlen + 7) & ~7; 548120941Sume 54953541Sshin m->m_pkthdr.len += optlen; 55053541Sshin m->m_len += optlen; 55153541Sshin icmp6len += optlen; 55253541Sshin bzero((caddr_t)nd_opt, optlen); 55353541Sshin nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; 55453541Sshin nd_opt->nd_opt_len = optlen >> 3; 55553541Sshin bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 55653541Sshin } 55753541Sshin 55853541Sshin ip6->ip6_plen = htons((u_short)icmp6len); 55953541Sshin nd_ns->nd_ns_cksum = 0; 560120941Sume nd_ns->nd_ns_cksum = 561120941Sume in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); 56253541Sshin 563151536Ssuz ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL); 564148385Sume icmp6_ifstat_inc(ifp, ifs6_out_msg); 565148385Sume icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit); 566190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]); 567148385Sume 568148385Sume if (ro.ro_rt) { /* we don't cache this route. */ 569148385Sume RTFREE(ro.ro_rt); 57053541Sshin } 571148385Sume return; 572148385Sume 573148385Sume bad: 574148385Sume if (ro.ro_rt) { 575148385Sume RTFREE(ro.ro_rt); 576148385Sume } 577148385Sume m_freem(m); 578148385Sume return; 57953541Sshin} 58053541Sshin 58153541Sshin/* 58253541Sshin * Neighbor advertisement input handling. 58353541Sshin * 58453541Sshin * Based on RFC 2461 585148987Sume * Based on RFC 2462 (duplicate address detection) 58662587Sitojun * 58762587Sitojun * the following items are not implemented yet: 58862587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 58962587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 59053541Sshin */ 59153541Sshinvoid 592171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len) 59353541Sshin{ 594183550Szec INIT_VNET_INET6(curvnet); 59553541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 59653541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 59762587Sitojun struct nd_neighbor_advert *nd_na; 59853541Sshin struct in6_addr daddr6 = ip6->ip6_dst; 59962587Sitojun struct in6_addr taddr6; 60062587Sitojun int flags; 60162587Sitojun int is_router; 60262587Sitojun int is_solicited; 60362587Sitojun int is_override; 60453541Sshin char *lladdr = NULL; 60553541Sshin int lladdrlen = 0; 606186468Skmacy int checklink = 0; 60753541Sshin struct ifaddr *ifa; 608186119Sqingli struct llentry *ln = NULL; 60953541Sshin union nd_opts ndopts; 610186119Sqingli struct mbuf *chain = NULL; 611186119Sqingli struct sockaddr_in6 sin6; 612165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 61353541Sshin 61453541Sshin if (ip6->ip6_hlim != 255) { 61578064Sume nd6log((LOG_ERR, 61678064Sume "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", 617165118Sbz ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 618165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 61978064Sume goto bad; 62062587Sitojun } 62162587Sitojun 62262587Sitojun#ifndef PULLDOWN_TEST 62362587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 62462587Sitojun nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); 62562587Sitojun#else 62662587Sitojun IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len); 62762587Sitojun if (nd_na == NULL) { 628190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 62953541Sshin return; 63053541Sshin } 63162587Sitojun#endif 632148385Sume 63362587Sitojun flags = nd_na->nd_na_flags_reserved; 63462587Sitojun is_router = ((flags & ND_NA_FLAG_ROUTER) != 0); 63562587Sitojun is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0); 63662587Sitojun is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); 63753541Sshin 638148385Sume taddr6 = nd_na->nd_na_target; 639148385Sume if (in6_setscope(&taddr6, ifp, NULL)) 640150202Ssuz goto bad; /* XXX: impossible */ 64153541Sshin 64253541Sshin if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 64378064Sume nd6log((LOG_ERR, 64453541Sshin "nd6_na_input: invalid target address %s\n", 645165118Sbz ip6_sprintf(ip6bufs, &taddr6))); 64678064Sume goto bad; 64753541Sshin } 64853541Sshin if (IN6_IS_ADDR_MULTICAST(&daddr6)) 64953541Sshin if (is_solicited) { 65078064Sume nd6log((LOG_ERR, 65178064Sume "nd6_na_input: a solicited adv is multicasted\n")); 65278064Sume goto bad; 65353541Sshin } 65453541Sshin 65553541Sshin icmp6len -= sizeof(*nd_na); 65653541Sshin nd6_option_init(nd_na + 1, icmp6len, &ndopts); 65753541Sshin if (nd6_options(&ndopts) < 0) { 65878064Sume nd6log((LOG_INFO, 65978064Sume "nd6_na_input: invalid ND option, ignored\n")); 66078064Sume /* nd6_options have incremented stats */ 66162587Sitojun goto freeit; 66253541Sshin } 66353541Sshin 66453541Sshin if (ndopts.nd_opts_tgt_lladdr) { 66553541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 66653541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 66753541Sshin } 66853541Sshin 66953541Sshin ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 67053541Sshin 67153541Sshin /* 67253541Sshin * Target address matches one of my interface address. 67353541Sshin * 67453541Sshin * If my address is tentative, this means that there's somebody 67553541Sshin * already using the same address as mine. This indicates DAD failure. 67653541Sshin * This is defined in RFC 2462. 67753541Sshin * 67853541Sshin * Otherwise, process as defined in RFC 2461. 67953541Sshin */ 68053541Sshin if (ifa 68153541Sshin && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) { 68253541Sshin nd6_dad_na_input(ifa); 68362587Sitojun goto freeit; 68453541Sshin } 68553541Sshin 68695023Ssuz /* Just for safety, maybe unnecessary. */ 68753541Sshin if (ifa) { 68853541Sshin log(LOG_ERR, 68953541Sshin "nd6_na_input: duplicate IP6 address %s\n", 690165118Sbz ip6_sprintf(ip6bufs, &taddr6)); 69162587Sitojun goto freeit; 69253541Sshin } 69353541Sshin 69453541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 695120941Sume nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s " 696165118Sbz "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6), 697120941Sume ifp->if_addrlen, lladdrlen - 2)); 69878064Sume goto bad; 69953541Sshin } 70053541Sshin 70153541Sshin /* 702120941Sume * If no neighbor cache entry is found, NA SHOULD silently be 703120941Sume * discarded. 70453541Sshin */ 705186119Sqingli IF_AFDATA_LOCK(ifp); 706186119Sqingli ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp); 707186119Sqingli IF_AFDATA_UNLOCK(ifp); 708186119Sqingli if (ln == NULL) { 70962587Sitojun goto freeit; 710186119Sqingli } 71153541Sshin 71253541Sshin if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 71353541Sshin /* 71453541Sshin * If the link-layer has address, and no lladdr option came, 71553541Sshin * discard the packet. 71653541Sshin */ 717186119Sqingli if (ifp->if_addrlen && lladdr == NULL) { 71862587Sitojun goto freeit; 719186119Sqingli } 72053541Sshin 72153541Sshin /* 72253541Sshin * Record link-layer address, and update the state. 72353541Sshin */ 724186119Sqingli bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen); 725186119Sqingli ln->la_flags |= LLE_VALID; 72653541Sshin if (is_solicited) { 72753541Sshin ln->ln_state = ND6_LLINFO_REACHABLE; 72862587Sitojun ln->ln_byhint = 0; 729151539Ssuz if (!ND6_LLINFO_PERMANENT(ln)) { 730186119Sqingli nd6_llinfo_settimer_locked(ln, 731186119Sqingli (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz); 732120941Sume } 73378064Sume } else { 73453541Sshin ln->ln_state = ND6_LLINFO_STALE; 735186119Sqingli nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 73678064Sume } 73778064Sume if ((ln->ln_router = is_router) != 0) { 73878064Sume /* 73978064Sume * This means a router's state has changed from 74078064Sume * non-reachable to probably reachable, and might 74178064Sume * affect the status of associated prefixes.. 74278064Sume */ 743186468Skmacy checklink = 1; 74478064Sume } 74553541Sshin } else { 74653541Sshin int llchange; 74753541Sshin 74853541Sshin /* 74953541Sshin * Check if the link-layer address has changed or not. 75053541Sshin */ 751151465Ssuz if (lladdr == NULL) 75253541Sshin llchange = 0; 75353541Sshin else { 754186119Sqingli if (ln->la_flags & LLE_VALID) { 755186119Sqingli if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen)) 75653541Sshin llchange = 1; 75753541Sshin else 75853541Sshin llchange = 0; 75953541Sshin } else 76053541Sshin llchange = 1; 76153541Sshin } 76253541Sshin 76353541Sshin /* 76453541Sshin * This is VERY complex. Look at it with care. 76553541Sshin * 76653541Sshin * override solicit lladdr llchange action 76753541Sshin * (L: record lladdr) 76853541Sshin * 76953541Sshin * 0 0 n -- (2c) 77053541Sshin * 0 0 y n (2b) L 77153541Sshin * 0 0 y y (1) REACHABLE->STALE 77253541Sshin * 0 1 n -- (2c) *->REACHABLE 77353541Sshin * 0 1 y n (2b) L *->REACHABLE 77453541Sshin * 0 1 y y (1) REACHABLE->STALE 77553541Sshin * 1 0 n -- (2a) 77653541Sshin * 1 0 y n (2a) L 77753541Sshin * 1 0 y y (2a) L *->STALE 77853541Sshin * 1 1 n -- (2a) *->REACHABLE 77953541Sshin * 1 1 y n (2a) L *->REACHABLE 78053541Sshin * 1 1 y y (2a) L *->REACHABLE 78153541Sshin */ 782151539Ssuz if (!is_override && (lladdr != NULL && llchange)) { /* (1) */ 78353541Sshin /* 78453541Sshin * If state is REACHABLE, make it STALE. 78553541Sshin * no other updates should be done. 78653541Sshin */ 78778064Sume if (ln->ln_state == ND6_LLINFO_REACHABLE) { 78853541Sshin ln->ln_state = ND6_LLINFO_STALE; 789186119Sqingli nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 79078064Sume } 79162587Sitojun goto freeit; 79253541Sshin } else if (is_override /* (2a) */ 793151465Ssuz || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */ 794151465Ssuz || lladdr == NULL) { /* (2c) */ 79553541Sshin /* 79653541Sshin * Update link-local address, if any. 79753541Sshin */ 798151465Ssuz if (lladdr != NULL) { 799186119Sqingli bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen); 800186119Sqingli ln->la_flags |= LLE_VALID; 80153541Sshin } 80253541Sshin 80353541Sshin /* 80453541Sshin * If solicited, make the state REACHABLE. 80553541Sshin * If not solicited and the link-layer address was 80653541Sshin * changed, make it STALE. 80753541Sshin */ 80853541Sshin if (is_solicited) { 80953541Sshin ln->ln_state = ND6_LLINFO_REACHABLE; 81062587Sitojun ln->ln_byhint = 0; 811151539Ssuz if (!ND6_LLINFO_PERMANENT(ln)) { 812186119Sqingli nd6_llinfo_settimer_locked(ln, 813151539Ssuz (long)ND_IFINFO(ifp)->reachable * hz); 81453541Sshin } 81553541Sshin } else { 816151465Ssuz if (lladdr != NULL && llchange) { 81753541Sshin ln->ln_state = ND6_LLINFO_STALE; 818186119Sqingli nd6_llinfo_settimer_locked(ln, 819181803Sbz (long)V_nd6_gctimer * hz); 82078064Sume } 82153541Sshin } 82253541Sshin } 82353541Sshin 82453541Sshin if (ln->ln_router && !is_router) { 82553541Sshin /* 82653541Sshin * The peer dropped the router flag. 82753541Sshin * Remove the sender from the Default Router List and 82853541Sshin * update the Destination Cache entries. 82953541Sshin */ 83053541Sshin struct nd_defrouter *dr; 83153541Sshin struct in6_addr *in6; 83253541Sshin 833186119Sqingli in6 = &L3_ADDR_SIN6(ln)->sin6_addr; 83495023Ssuz 83595023Ssuz /* 83695023Ssuz * Lock to protect the default router list. 83795023Ssuz * XXX: this might be unnecessary, since this function 83895023Ssuz * is only called under the network software interrupt 839120941Sume * context. However, we keep it just for safety. 84095023Ssuz */ 841186119Sqingli dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp); 84253541Sshin if (dr) 84353541Sshin defrtrlist_del(dr); 844181803Sbz else if (!V_ip6_forwarding) { 84553541Sshin /* 84653541Sshin * Even if the neighbor is not in the default 84753541Sshin * router list, the neighbor may be used 84853541Sshin * as a next hop for some destinations 84953541Sshin * (e.g. redirect case). So we must 85053541Sshin * call rt6_flush explicitly. 85153541Sshin */ 852128422Sluigi rt6_flush(&ip6->ip6_src, ifp); 85353541Sshin } 85453541Sshin } 85553541Sshin ln->ln_router = is_router; 85653541Sshin } 857186119Sqingli /* XXX - QL 858186119Sqingli * Does this matter? 859186119Sqingli * rt->rt_flags &= ~RTF_REJECT; 860186119Sqingli */ 861186119Sqingli ln->la_asked = 0; 862186119Sqingli if (ln->la_hold) { 863151539Ssuz struct mbuf *m_hold, *m_hold_next; 864151539Ssuz 865169273Ssuz /* 866186119Sqingli * reset the la_hold in advance, to explicitly 867186119Sqingli * prevent a la_hold lookup in nd6_output() 868169273Ssuz * (wouldn't happen, though...) 869169273Ssuz */ 870186119Sqingli for (m_hold = ln->la_hold, ln->la_hold = NULL; 871169241Ssuz m_hold; m_hold = m_hold_next) { 872151539Ssuz m_hold_next = m_hold->m_nextpkt; 873169241Ssuz m_hold->m_nextpkt = NULL; 874151539Ssuz /* 875151539Ssuz * we assume ifp is not a loopback here, so just set 876151539Ssuz * the 2nd argument as the 1st one. 877151539Ssuz */ 878186119Sqingli nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain); 879151539Ssuz } 88053541Sshin } 881186119Sqingli freeit: 882186160Skmacy if (ln != NULL) { 883186119Sqingli if (chain) 884186119Sqingli memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6)); 885186119Sqingli LLE_WUNLOCK(ln); 88662587Sitojun 887186119Sqingli if (chain) 888186119Sqingli nd6_output_flush(ifp, ifp, chain, &sin6, NULL); 889186119Sqingli } 890186468Skmacy if (checklink) 891186468Skmacy pfxlist_onlink_check(); 892186468Skmacy 89362587Sitojun m_freem(m); 89478064Sume return; 89578064Sume 89678064Sume bad: 897186160Skmacy if (ln != NULL) 898186119Sqingli LLE_WUNLOCK(ln); 899186119Sqingli 900190964Srwatson ICMP6STAT_INC(icp6s_badna); 90178064Sume m_freem(m); 90253541Sshin} 90353541Sshin 90453541Sshin/* 90553541Sshin * Neighbor advertisement output handling. 90653541Sshin * 90753541Sshin * Based on RFC 2461 90853541Sshin * 90962587Sitojun * the following items are not implemented yet: 91062587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 91162587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 912171259Sdelphij * 913171259Sdelphij * tlladdr - 1 if include target link-layer address 914171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL 91553541Sshin */ 91653541Sshinvoid 917171259Sdelphijnd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0, 918171259Sdelphij const struct in6_addr *taddr6, u_long flags, int tlladdr, 919171259Sdelphij struct sockaddr *sdl0) 92053541Sshin{ 921183550Szec INIT_VNET_INET6(ifp->if_vnet); 92253541Sshin struct mbuf *m; 92353541Sshin struct ip6_hdr *ip6; 92453541Sshin struct nd_neighbor_advert *nd_na; 92553541Sshin struct ip6_moptions im6o; 926148385Sume struct in6_addr *src, daddr6; 927148385Sume struct sockaddr_in6 dst_sa; 928148385Sume int icmp6len, maxlen, error; 92992733Speter caddr_t mac = NULL; 930148385Sume struct route_in6 ro; 93162587Sitojun 932148385Sume bzero(&ro, sizeof(ro)); 933148385Sume 934148385Sume daddr6 = *daddr6_0; /* make a local copy for modification */ 935148385Sume 93662587Sitojun /* estimate the size of message */ 93762587Sitojun maxlen = sizeof(*ip6) + sizeof(*nd_na); 93862587Sitojun maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 93962587Sitojun if (max_linkhdr + maxlen >= MCLBYTES) { 94062587Sitojun#ifdef DIAGNOSTIC 94162587Sitojun printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES " 94262587Sitojun "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 94362587Sitojun#endif 94453541Sshin return; 94562587Sitojun } 94653541Sshin 947111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 94862587Sitojun if (m && max_linkhdr + maxlen >= MHLEN) { 949111119Simp MCLGET(m, M_DONTWAIT); 95062587Sitojun if ((m->m_flags & M_EXT) == 0) { 95162587Sitojun m_free(m); 95262587Sitojun m = NULL; 95362587Sitojun } 95462587Sitojun } 95562587Sitojun if (m == NULL) 95662587Sitojun return; 95778064Sume m->m_pkthdr.rcvif = NULL; 95862587Sitojun 959148385Sume if (IN6_IS_ADDR_MULTICAST(&daddr6)) { 96053541Sshin m->m_flags |= M_MCAST; 96153541Sshin im6o.im6o_multicast_ifp = ifp; 96253541Sshin im6o.im6o_multicast_hlim = 255; 96353541Sshin im6o.im6o_multicast_loop = 0; 96453541Sshin } 96553541Sshin 96653541Sshin icmp6len = sizeof(*nd_na); 96753541Sshin m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len; 96895023Ssuz m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 96953541Sshin 97053541Sshin /* fill neighbor advertisement packet */ 97153541Sshin ip6 = mtod(m, struct ip6_hdr *); 97253541Sshin ip6->ip6_flow = 0; 97362587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 97462587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 97553541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 97653541Sshin ip6->ip6_hlim = 255; 977148385Sume if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) { 97853541Sshin /* reply to DAD */ 979153227Sume daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 980153227Sume daddr6.s6_addr16[1] = 0; 981153227Sume daddr6.s6_addr32[1] = 0; 982153227Sume daddr6.s6_addr32[2] = 0; 983153227Sume daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE; 984148385Sume if (in6_setscope(&daddr6, ifp, NULL)) 985148385Sume goto bad; 986148385Sume 98753541Sshin flags &= ~ND_NA_FLAG_SOLICITED; 988148385Sume } 989148385Sume ip6->ip6_dst = daddr6; 990148385Sume bzero(&dst_sa, sizeof(struct sockaddr_in6)); 991148385Sume dst_sa.sin6_family = AF_INET6; 992148385Sume dst_sa.sin6_len = sizeof(struct sockaddr_in6); 993148385Sume dst_sa.sin6_addr = daddr6; 99453541Sshin 99553541Sshin /* 99653541Sshin * Select a source whose scope is the same as that of the dest. 99753541Sshin */ 998148385Sume bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa)); 999148385Sume src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error); 1000148385Sume if (src == NULL) { 1001165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 1002148385Sume nd6log((LOG_DEBUG, "nd6_na_output: source can't be " 1003148385Sume "determined: dst=%s, error=%d\n", 1004165118Sbz ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error)); 1005148385Sume goto bad; 100653541Sshin } 1007148385Sume ip6->ip6_src = *src; 100853541Sshin nd_na = (struct nd_neighbor_advert *)(ip6 + 1); 100953541Sshin nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; 101053541Sshin nd_na->nd_na_code = 0; 101153541Sshin nd_na->nd_na_target = *taddr6; 1012121315Sume in6_clearscope(&nd_na->nd_na_target); /* XXX */ 101353541Sshin 101453541Sshin /* 101553541Sshin * "tlladdr" indicates NS's condition for adding tlladdr or not. 101653541Sshin * see nd6_ns_input() for details. 101753541Sshin * Basically, if NS packet is sent to unicast/anycast addr, 101853541Sshin * target lladdr option SHOULD NOT be included. 101953541Sshin */ 102062587Sitojun if (tlladdr) { 102162587Sitojun /* 102262587Sitojun * sdl0 != NULL indicates proxy NA. If we do proxy, use 102362587Sitojun * lladdr in sdl0. If we are not proxying (sending NA for 102462587Sitojun * my address) use lladdr configured for the interface. 102562587Sitojun */ 1026142215Sglebius if (sdl0 == NULL) { 1027142215Sglebius#ifdef DEV_CARP 1028142215Sglebius if (ifp->if_carp) 1029142215Sglebius mac = carp_macmatch6(ifp->if_carp, m, taddr6); 1030142215Sglebius if (mac == NULL) 1031142215Sglebius mac = nd6_ifptomac(ifp); 1032142215Sglebius#else 103362587Sitojun mac = nd6_ifptomac(ifp); 1034142215Sglebius#endif 1035142215Sglebius } else if (sdl0->sa_family == AF_LINK) { 103662587Sitojun struct sockaddr_dl *sdl; 103762587Sitojun sdl = (struct sockaddr_dl *)sdl0; 103862587Sitojun if (sdl->sdl_alen == ifp->if_addrlen) 103962587Sitojun mac = LLADDR(sdl); 104062587Sitojun } 104162587Sitojun } 104262587Sitojun if (tlladdr && mac) { 104353541Sshin int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 104453541Sshin struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1); 1045120941Sume 104653541Sshin /* roundup to 8 bytes alignment! */ 104753541Sshin optlen = (optlen + 7) & ~7; 104853541Sshin 104953541Sshin m->m_pkthdr.len += optlen; 105053541Sshin m->m_len += optlen; 105153541Sshin icmp6len += optlen; 105253541Sshin bzero((caddr_t)nd_opt, optlen); 105353541Sshin nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 105453541Sshin nd_opt->nd_opt_len = optlen >> 3; 105553541Sshin bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 105653541Sshin } else 105753541Sshin flags &= ~ND_NA_FLAG_OVERRIDE; 105853541Sshin 105953541Sshin ip6->ip6_plen = htons((u_short)icmp6len); 106053541Sshin nd_na->nd_na_flags_reserved = flags; 106153541Sshin nd_na->nd_na_cksum = 0; 106253541Sshin nd_na->nd_na_cksum = 1063120941Sume in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); 106453541Sshin 1065148385Sume ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL); 1066148385Sume icmp6_ifstat_inc(ifp, ifs6_out_msg); 1067148385Sume icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert); 1068190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]); 1069148385Sume 1070148385Sume if (ro.ro_rt) { /* we don't cache this route. */ 1071148385Sume RTFREE(ro.ro_rt); 107253541Sshin } 1073148385Sume return; 1074148385Sume 1075148385Sume bad: 1076148385Sume if (ro.ro_rt) { 1077148385Sume RTFREE(ro.ro_rt); 1078148385Sume } 1079148385Sume m_freem(m); 1080148385Sume return; 108153541Sshin} 108253541Sshin 108353541Sshincaddr_t 1084171259Sdelphijnd6_ifptomac(struct ifnet *ifp) 108553541Sshin{ 108653541Sshin switch (ifp->if_type) { 108753541Sshin case IFT_ARCNET: 108853541Sshin case IFT_ETHER: 108953541Sshin case IFT_FDDI: 109078064Sume case IFT_IEEE1394: 109178468Ssumikawa#ifdef IFT_L2VLAN 109278468Ssumikawa case IFT_L2VLAN: 109378468Ssumikawa#endif 109478064Sume#ifdef IFT_IEEE80211 109578064Sume case IFT_IEEE80211: 109678064Sume#endif 1097142215Sglebius#ifdef IFT_CARP 1098142215Sglebius case IFT_CARP: 1099142215Sglebius#endif 1100149829Sthompsa case IFT_BRIDGE: 1101120049Smdodd case IFT_ISO88025: 1102147306Sbrooks return IF_LLADDR(ifp); 110353541Sshin default: 110453541Sshin return NULL; 110553541Sshin } 110653541Sshin} 110753541Sshin 110853541Sshinstruct dadq { 110960938Sjake TAILQ_ENTRY(dadq) dad_list; 111053541Sshin struct ifaddr *dad_ifa; 111153541Sshin int dad_count; /* max NS to send */ 111262587Sitojun int dad_ns_tcount; /* # of trials to send NS */ 111353541Sshin int dad_ns_ocount; /* NS sent so far */ 111453541Sshin int dad_ns_icount; 111553541Sshin int dad_na_icount; 111678064Sume struct callout dad_timer_ch; 1117191816Szec struct vnet *dad_vnet; 111853541Sshin}; 111953541Sshin 1120185088Szec#ifdef VIMAGE_GLOBALS 1121191816Szecstatic TAILQ_HEAD(, dadq) dadq; 1122185088Szecint dad_init; 1123185088Szec#endif 112453541Sshin 112553541Sshinstatic struct dadq * 1126171259Sdelphijnd6_dad_find(struct ifaddr *ifa) 112753541Sshin{ 1128183550Szec INIT_VNET_INET6(curvnet); 112953541Sshin struct dadq *dp; 113053541Sshin 1131181803Sbz for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) { 113253541Sshin if (dp->dad_ifa == ifa) 113353541Sshin return dp; 113453541Sshin } 113553541Sshin return NULL; 113653541Sshin} 113753541Sshin 113878064Sumestatic void 1139171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks) 114078064Sume{ 114178064Sume 114278064Sume callout_reset(&dp->dad_timer_ch, ticks, 1143191816Szec (void (*)(void *))nd6_dad_timer, (void *)dp); 114478064Sume} 114578064Sume 114678064Sumestatic void 1147171259Sdelphijnd6_dad_stoptimer(struct dadq *dp) 114878064Sume{ 114978064Sume 115078064Sume callout_stop(&dp->dad_timer_ch); 115178064Sume} 115278064Sume 115353541Sshin/* 1154148987Sume * Start Duplicate Address Detection (DAD) for specified interface address. 115553541Sshin */ 115653541Sshinvoid 1157171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay) 115853541Sshin{ 1159183550Szec INIT_VNET_INET6(curvnet); 116053541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 116153541Sshin struct dadq *dp; 1162165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 116353541Sshin 1164181803Sbz if (!V_dad_init) { 1165181803Sbz TAILQ_INIT(&V_dadq); 1166181803Sbz V_dad_init++; 116753541Sshin } 116853541Sshin 116953541Sshin /* 117053541Sshin * If we don't need DAD, don't do it. 117153541Sshin * There are several cases: 117253541Sshin * - DAD is disabled (ip6_dad_count == 0) 117353541Sshin * - the interface address is anycast 117453541Sshin */ 117553541Sshin if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) { 117662587Sitojun log(LOG_DEBUG, 117762587Sitojun "nd6_dad_start: called with non-tentative address " 117853541Sshin "%s(%s)\n", 1179165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 118053541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 118153541Sshin return; 118253541Sshin } 118353541Sshin if (ia->ia6_flags & IN6_IFF_ANYCAST) { 118453541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 118553541Sshin return; 118653541Sshin } 1187181803Sbz if (!V_ip6_dad_count) { 118853541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 118953541Sshin return; 119053541Sshin } 1191151465Ssuz if (ifa->ifa_ifp == NULL) 119253541Sshin panic("nd6_dad_start: ifa->ifa_ifp == NULL"); 1193120941Sume if (!(ifa->ifa_ifp->if_flags & IFF_UP)) { 119453541Sshin return; 1195120941Sume } 119653541Sshin if (nd6_dad_find(ifa) != NULL) { 119753541Sshin /* DAD already in progress */ 119853541Sshin return; 119953541Sshin } 120053541Sshin 120153541Sshin dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT); 120253541Sshin if (dp == NULL) { 120362587Sitojun log(LOG_ERR, "nd6_dad_start: memory allocation failed for " 120453541Sshin "%s(%s)\n", 1205165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 120653541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 120753541Sshin return; 120853541Sshin } 120953541Sshin bzero(dp, sizeof(*dp)); 121078064Sume callout_init(&dp->dad_timer_ch, 0); 1211191816Szec#ifdef VIMAGE 1212191816Szec dp->dad_vnet = curvnet; 1213191816Szec#endif 1214182150Sjulian TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list); 121553541Sshin 121678064Sume nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1217165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 121853541Sshin 121953541Sshin /* 122053541Sshin * Send NS packet for DAD, ip6_dad_count times. 122153541Sshin * Note that we must delay the first transmission, if this is the 122253541Sshin * first packet to be sent from the interface after interface 122353541Sshin * (re)initialization. 122453541Sshin */ 122553541Sshin dp->dad_ifa = ifa; 122695023Ssuz IFAREF(ifa); /* just for safety */ 1227181803Sbz dp->dad_count = V_ip6_dad_count; 122853541Sshin dp->dad_ns_icount = dp->dad_na_icount = 0; 122962587Sitojun dp->dad_ns_ocount = dp->dad_ns_tcount = 0; 1230151539Ssuz if (delay == 0) { 123162587Sitojun nd6_dad_ns_output(dp, ifa); 1232121161Sume nd6_dad_starttimer(dp, 1233151539Ssuz (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 123453541Sshin } else { 1235151539Ssuz nd6_dad_starttimer(dp, delay); 123653541Sshin } 123753541Sshin} 123853541Sshin 123978064Sume/* 124078064Sume * terminate DAD unconditionally. used for address removals. 124178064Sume */ 124278064Sumevoid 1243171259Sdelphijnd6_dad_stop(struct ifaddr *ifa) 124478064Sume{ 1245183550Szec INIT_VNET_INET6(curvnet); 124678064Sume struct dadq *dp; 124778064Sume 1248181803Sbz if (!V_dad_init) 124978064Sume return; 125078064Sume dp = nd6_dad_find(ifa); 125178064Sume if (!dp) { 125278064Sume /* DAD wasn't started yet */ 125378064Sume return; 125478064Sume } 125578064Sume 125678064Sume nd6_dad_stoptimer(dp); 125778064Sume 1258181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 125978064Sume free(dp, M_IP6NDP); 126078064Sume dp = NULL; 126178064Sume IFAFREE(ifa); 126278064Sume} 126378064Sume 126453541Sshinstatic void 1265191816Szecnd6_dad_timer(struct dadq *dp) 126653541Sshin{ 1267183550Szec CURVNET_SET(dp->dad_vnet); 1268183550Szec INIT_VNET_INET6(curvnet); 126953541Sshin int s; 1270191816Szec struct ifaddr *ifa = dp->dad_ifa; 127153541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1272165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 127353541Sshin 127495023Ssuz s = splnet(); /* XXX */ 127553541Sshin 127653541Sshin /* Sanity check */ 127753541Sshin if (ia == NULL) { 127862587Sitojun log(LOG_ERR, "nd6_dad_timer: called with null parameter\n"); 127953541Sshin goto done; 128053541Sshin } 128153541Sshin if (ia->ia6_flags & IN6_IFF_DUPLICATED) { 128262587Sitojun log(LOG_ERR, "nd6_dad_timer: called with duplicated address " 128353541Sshin "%s(%s)\n", 1284165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 128553541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 128653541Sshin goto done; 128753541Sshin } 128853541Sshin if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) { 128962587Sitojun log(LOG_ERR, "nd6_dad_timer: called with non-tentative address " 129053541Sshin "%s(%s)\n", 1291165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 129253541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 129353541Sshin goto done; 129453541Sshin } 129553541Sshin 129662587Sitojun /* timeouted with IFF_{RUNNING,UP} check */ 1297181803Sbz if (dp->dad_ns_tcount > V_dad_maxtry) { 129878064Sume nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n", 1299120941Sume if_name(ifa->ifa_ifp))); 130062587Sitojun 1301181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 130262587Sitojun free(dp, M_IP6NDP); 130362587Sitojun dp = NULL; 130462587Sitojun IFAFREE(ifa); 130562587Sitojun goto done; 130662587Sitojun } 130762587Sitojun 130853541Sshin /* Need more checks? */ 130953541Sshin if (dp->dad_ns_ocount < dp->dad_count) { 131053541Sshin /* 131153541Sshin * We have more NS to go. Send NS packet for DAD. 131253541Sshin */ 131362587Sitojun nd6_dad_ns_output(dp, ifa); 1314120941Sume nd6_dad_starttimer(dp, 1315151539Ssuz (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 131653541Sshin } else { 131753541Sshin /* 131853541Sshin * We have transmitted sufficient number of DAD packets. 131953541Sshin * See what we've got. 132053541Sshin */ 132153541Sshin int duplicate; 132253541Sshin 132353541Sshin duplicate = 0; 132453541Sshin 132553541Sshin if (dp->dad_na_icount) { 132653541Sshin /* 132753541Sshin * the check is in nd6_dad_na_input(), 132853541Sshin * but just in case 132953541Sshin */ 133053541Sshin duplicate++; 133153541Sshin } 133253541Sshin 133353541Sshin if (dp->dad_ns_icount) { 133453541Sshin /* We've seen NS, means DAD has failed. */ 133553541Sshin duplicate++; 133653541Sshin } 133753541Sshin 133853541Sshin if (duplicate) { 133953541Sshin /* (*dp) will be freed in nd6_dad_duplicated() */ 134053541Sshin dp = NULL; 134153541Sshin nd6_dad_duplicated(ifa); 134253541Sshin } else { 134353541Sshin /* 134453541Sshin * We are done with DAD. No NA came, no NS came. 1345148987Sume * No duplicate address found. 134653541Sshin */ 134753541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 134853541Sshin 134978064Sume nd6log((LOG_DEBUG, 135062587Sitojun "%s: DAD complete for %s - no duplicates found\n", 135162587Sitojun if_name(ifa->ifa_ifp), 1352165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 135353541Sshin 1354181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 135553541Sshin free(dp, M_IP6NDP); 135653541Sshin dp = NULL; 135762587Sitojun IFAFREE(ifa); 135853541Sshin } 135953541Sshin } 136053541Sshin 136153541Sshindone: 136253541Sshin splx(s); 1363183550Szec CURVNET_RESTORE(); 136453541Sshin} 136553541Sshin 136653541Sshinvoid 1367171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa) 136853541Sshin{ 1369183550Szec INIT_VNET_INET6(curvnet); 137053541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1371151477Ssuz struct ifnet *ifp; 137253541Sshin struct dadq *dp; 1373165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 137453541Sshin 137553541Sshin dp = nd6_dad_find(ifa); 137653541Sshin if (dp == NULL) { 137762587Sitojun log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n"); 137853541Sshin return; 137953541Sshin } 138053541Sshin 138178064Sume log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: " 138278064Sume "NS in/out=%d/%d, NA in=%d\n", 1383165118Sbz if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 138478064Sume dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount); 138553541Sshin 138653541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 138753541Sshin ia->ia6_flags |= IN6_IFF_DUPLICATED; 138853541Sshin 1389148987Sume /* We are done with DAD, with duplicate address found. (failure) */ 139078064Sume nd6_dad_stoptimer(dp); 139153541Sshin 1392151477Ssuz ifp = ifa->ifa_ifp; 139362587Sitojun log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n", 1394165118Sbz if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)); 139562587Sitojun log(LOG_ERR, "%s: manual intervention required\n", 1396151477Ssuz if_name(ifp)); 139753541Sshin 1398151477Ssuz /* 1399151477Ssuz * If the address is a link-local address formed from an interface 1400151477Ssuz * identifier based on the hardware address which is supposed to be 1401151477Ssuz * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP 1402151477Ssuz * operation on the interface SHOULD be disabled. 1403151477Ssuz * [rfc2462bis-03 Section 5.4.5] 1404151477Ssuz */ 1405151477Ssuz if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 1406151477Ssuz struct in6_addr in6; 1407151477Ssuz 1408151477Ssuz /* 1409151477Ssuz * To avoid over-reaction, we only apply this logic when we are 1410151477Ssuz * very sure that hardware addresses are supposed to be unique. 1411151477Ssuz */ 1412151477Ssuz switch (ifp->if_type) { 1413151477Ssuz case IFT_ETHER: 1414151477Ssuz case IFT_FDDI: 1415151477Ssuz case IFT_ATM: 1416151477Ssuz case IFT_IEEE1394: 1417151477Ssuz#ifdef IFT_IEEE80211 1418151477Ssuz case IFT_IEEE80211: 1419151477Ssuz#endif 1420151477Ssuz in6 = ia->ia_addr.sin6_addr; 1421151477Ssuz if (in6_get_hw_ifid(ifp, &in6) == 0 && 1422151477Ssuz IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { 1423151477Ssuz ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1424151477Ssuz log(LOG_ERR, "%s: possible hardware address " 1425151477Ssuz "duplication detected, disable IPv6\n", 1426151477Ssuz if_name(ifp)); 1427151477Ssuz } 1428151477Ssuz break; 1429151477Ssuz } 1430151477Ssuz } 1431151477Ssuz 1432181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 143353541Sshin free(dp, M_IP6NDP); 143453541Sshin dp = NULL; 143562587Sitojun IFAFREE(ifa); 143653541Sshin} 143753541Sshin 143862587Sitojunstatic void 1439171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa) 144062587Sitojun{ 144162587Sitojun struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 144262587Sitojun struct ifnet *ifp = ifa->ifa_ifp; 144362587Sitojun 144462587Sitojun dp->dad_ns_tcount++; 144562587Sitojun if ((ifp->if_flags & IFF_UP) == 0) { 144662587Sitojun return; 144762587Sitojun } 1448148887Srwatson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 144962587Sitojun return; 145062587Sitojun } 145162587Sitojun 145262587Sitojun dp->dad_ns_ocount++; 145362587Sitojun nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1); 145462587Sitojun} 145562587Sitojun 145662587Sitojunstatic void 1457171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa) 145853541Sshin{ 1459183550Szec INIT_VNET_INET6(curvnet); 146053541Sshin struct in6_ifaddr *ia; 146153541Sshin struct ifnet *ifp; 146278064Sume const struct in6_addr *taddr6; 146353541Sshin struct dadq *dp; 146453541Sshin int duplicate; 146553541Sshin 1466151465Ssuz if (ifa == NULL) 146753541Sshin panic("ifa == NULL in nd6_dad_ns_input"); 146853541Sshin 146953541Sshin ia = (struct in6_ifaddr *)ifa; 147053541Sshin ifp = ifa->ifa_ifp; 147153541Sshin taddr6 = &ia->ia_addr.sin6_addr; 147253541Sshin duplicate = 0; 147353541Sshin dp = nd6_dad_find(ifa); 147453541Sshin 147553541Sshin /* Quickhack - completely ignore DAD NS packets */ 1476181803Sbz if (V_dad_ignore_ns) { 1477165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 147878064Sume nd6log((LOG_INFO, 147978064Sume "nd6_dad_ns_input: ignoring DAD NS packet for " 1480165118Sbz "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6), 148178064Sume if_name(ifa->ifa_ifp))); 148253541Sshin return; 148353541Sshin } 148453541Sshin 148553541Sshin /* 148653541Sshin * if I'm yet to start DAD, someone else started using this address 148753541Sshin * first. I have a duplicate and you win. 148853541Sshin */ 1489151465Ssuz if (dp == NULL || dp->dad_ns_ocount == 0) 149053541Sshin duplicate++; 149153541Sshin 149253541Sshin /* XXX more checks for loopback situation - see nd6_dad_timer too */ 149353541Sshin 149453541Sshin if (duplicate) { 149553541Sshin dp = NULL; /* will be freed in nd6_dad_duplicated() */ 149653541Sshin nd6_dad_duplicated(ifa); 149753541Sshin } else { 149853541Sshin /* 149953541Sshin * not sure if I got a duplicate. 150053541Sshin * increment ns count and see what happens. 150153541Sshin */ 150253541Sshin if (dp) 150353541Sshin dp->dad_ns_icount++; 150453541Sshin } 150553541Sshin} 150653541Sshin 150762587Sitojunstatic void 1508171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa) 150953541Sshin{ 151053541Sshin struct dadq *dp; 151153541Sshin 1512151465Ssuz if (ifa == NULL) 151353541Sshin panic("ifa == NULL in nd6_dad_na_input"); 151453541Sshin 151553541Sshin dp = nd6_dad_find(ifa); 151653541Sshin if (dp) 151753541Sshin dp->dad_na_icount++; 151853541Sshin 151953541Sshin /* remove the address. */ 152053541Sshin nd6_dad_duplicated(ifa); 152153541Sshin} 1522