nd6_nbr.c revision 248328
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 248328 2013-03-15 13:48:53Z glebius $"); 34174510Sobrien 3562587Sitojun#include "opt_inet.h" 3662587Sitojun#include "opt_inet6.h" 37142215Sglebius#include "opt_ipsec.h" 38178167Sqingli#include "opt_mpath.h" 3955009Sshin 4053541Sshin#include <sys/param.h> 4153541Sshin#include <sys/systm.h> 4253541Sshin#include <sys/malloc.h> 43186119Sqingli#include <sys/lock.h> 44186119Sqingli#include <sys/rwlock.h> 4553541Sshin#include <sys/mbuf.h> 4653541Sshin#include <sys/socket.h> 4753541Sshin#include <sys/sockio.h> 4853541Sshin#include <sys/time.h> 4953541Sshin#include <sys/kernel.h> 5053541Sshin#include <sys/errno.h> 5153541Sshin#include <sys/syslog.h> 5253541Sshin#include <sys/queue.h> 5378064Sume#include <sys/callout.h> 5453541Sshin 5553541Sshin#include <net/if.h> 5653541Sshin#include <net/if_types.h> 5753541Sshin#include <net/if_dl.h> 58147306Sbrooks#include <net/if_var.h> 5953541Sshin#include <net/route.h> 60178167Sqingli#ifdef RADIX_MPATH 61178167Sqingli#include <net/radix_mpath.h> 62178167Sqingli#endif 6353541Sshin 6453541Sshin#include <netinet/in.h> 6553541Sshin#include <netinet/in_var.h> 66186119Sqingli#include <net/if_llatbl.h> 67186119Sqingli#define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le)) 6853541Sshin#include <netinet6/in6_var.h> 69151477Ssuz#include <netinet6/in6_ifattach.h> 7062587Sitojun#include <netinet/ip6.h> 7153541Sshin#include <netinet6/ip6_var.h> 72148385Sume#include <netinet6/scope6_var.h> 7353541Sshin#include <netinet6/nd6.h> 7462587Sitojun#include <netinet/icmp6.h> 75211193Swill#include <netinet/ip_carp.h> 76211501Sanchie#include <netinet6/send.h> 7753541Sshin 7862587Sitojun#define SDL(s) ((struct sockaddr_dl *)s) 7953541Sshin 8062587Sitojunstruct dadq; 81175162Sobrienstatic struct dadq *nd6_dad_find(struct ifaddr *); 82175162Sobrienstatic void nd6_dad_starttimer(struct dadq *, int); 83175162Sobrienstatic void nd6_dad_stoptimer(struct dadq *); 84191816Szecstatic void nd6_dad_timer(struct dadq *); 85175162Sobrienstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *); 86175162Sobrienstatic void nd6_dad_ns_input(struct ifaddr *); 87175162Sobrienstatic void nd6_dad_na_input(struct ifaddr *); 88231852Sbzstatic void nd6_na_output_fib(struct ifnet *, const struct in6_addr *, 89231852Sbz const struct in6_addr *, u_long, int, struct sockaddr *, u_int); 9053541Sshin 91207369SbzVNET_DEFINE(int, dad_ignore_ns) = 0; /* ignore NS in DAD - specwise incorrect*/ 92207369SbzVNET_DEFINE(int, dad_maxtry) = 15; /* max # of *tries* to transmit DAD packet */ 93195727Srwatson#define V_dad_ignore_ns VNET(dad_ignore_ns) 94195727Srwatson#define V_dad_maxtry VNET(dad_maxtry) 95195699Srwatson 9653541Sshin/* 97108470Sschweikh * Input a Neighbor Solicitation Message. 9853541Sshin * 9953541Sshin * Based on RFC 2461 100148987Sume * Based on RFC 2462 (duplicate address detection) 10153541Sshin */ 10253541Sshinvoid 103171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len) 10453541Sshin{ 10553541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 10653541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 10762587Sitojun struct nd_neighbor_solicit *nd_ns; 10853541Sshin struct in6_addr saddr6 = ip6->ip6_src; 10953541Sshin struct in6_addr daddr6 = ip6->ip6_dst; 11062587Sitojun struct in6_addr taddr6; 11153541Sshin struct in6_addr myaddr6; 11253541Sshin char *lladdr = NULL; 113142215Sglebius struct ifaddr *ifa = NULL; 11453541Sshin int lladdrlen = 0; 11553541Sshin int anycast = 0, proxy = 0, tentative = 0; 11653541Sshin int tlladdr; 117222728Shrs int rflag; 11853541Sshin union nd_opts ndopts; 119219562Sbz struct sockaddr_dl proxydl; 120165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 12153541Sshin 122222728Shrs rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0; 123222728Shrs if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif) 124222728Shrs rflag = 0; 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 if (ifp->if_carp) 229211157Swill ifa = (*carp_iamatch6_p)(ifp, &taddr6); 230228571Sglebius else 231142215Sglebius ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 23253541Sshin 23353541Sshin /* (2) check. */ 234151465Ssuz if (ifa == NULL) { 23553541Sshin struct rtentry *rt; 23653541Sshin struct sockaddr_in6 tsin6; 237121765Ssam int need_proxy; 238178167Sqingli#ifdef RADIX_MPATH 239178167Sqingli struct route_in6 ro; 240178167Sqingli#endif 24153541Sshin 242171260Sdelphij bzero(&tsin6, sizeof tsin6); 24353541Sshin tsin6.sin6_len = sizeof(struct sockaddr_in6); 24453541Sshin tsin6.sin6_family = AF_INET6; 24553541Sshin tsin6.sin6_addr = taddr6; 24653541Sshin 247231852Sbz /* Always use the default FIB. */ 248178167Sqingli#ifdef RADIX_MPATH 249178167Sqingli bzero(&ro, sizeof(ro)); 250178167Sqingli ro.ro_dst = tsin6; 251231852Sbz rtalloc_mpath_fib((struct route *)&ro, RTF_ANNOUNCE, 252231852Sbz RT_DEFAULT_FIB); 253178167Sqingli rt = ro.ro_rt; 254178167Sqingli#else 255231852Sbz rt = in6_rtalloc1((struct sockaddr *)&tsin6, 0, 0, 256231852Sbz RT_DEFAULT_FIB); 257178167Sqingli#endif 258121765Ssam need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 && 259121765Ssam rt->rt_gateway->sa_family == AF_LINK); 260219562Sbz if (rt != NULL) { 261219562Sbz /* 262219562Sbz * Make a copy while we can be sure that rt_gateway 263219562Sbz * is still stable before unlocking to avoid lock 264219562Sbz * order problems. proxydl will only be used if 265219562Sbz * proxy will be set in the next block. 266219562Sbz */ 267219562Sbz if (need_proxy) 268219562Sbz proxydl = *SDL(rt->rt_gateway); 269187946Sbz RTFREE_LOCKED(rt); 270219562Sbz } 271121765Ssam if (need_proxy) { 27253541Sshin /* 27362587Sitojun * proxy NDP for single entry 27453541Sshin */ 27562587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 27662587Sitojun IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); 277219562Sbz if (ifa) 27853541Sshin proxy = 1; 27953541Sshin } 28053541Sshin } 281151479Ssuz if (ifa == NULL) { 28253541Sshin /* 28378064Sume * We've got an NS packet, and we don't have that adddress 28453541Sshin * assigned for us. We MUST silently ignore it. 28553541Sshin * See RFC2461 7.2.3. 28653541Sshin */ 28762587Sitojun goto freeit; 28853541Sshin } 28953541Sshin myaddr6 = *IFA_IN6(ifa); 29053541Sshin anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST; 29153541Sshin tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE; 29253541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) 29362587Sitojun goto freeit; 29453541Sshin 29553541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 296120941Sume nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s " 29753541Sshin "(if %d, NS packet %d)\n", 298165118Sbz ip6_sprintf(ip6bufs, &taddr6), 299120941Sume ifp->if_addrlen, lladdrlen - 2)); 30078064Sume goto bad; 30153541Sshin } 30253541Sshin 30353541Sshin if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { 304120941Sume nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n", 305165118Sbz ip6_sprintf(ip6bufs, &saddr6))); 30662587Sitojun goto freeit; 30753541Sshin } 30853541Sshin 30953541Sshin /* 31053541Sshin * We have neighbor solicitation packet, with target address equals to 31153541Sshin * one of my tentative address. 31253541Sshin * 31353541Sshin * src addr how to process? 31453541Sshin * --- --- 31553541Sshin * multicast of course, invalid (rejected in ip6_input) 31653541Sshin * unicast somebody is doing address resolution -> ignore 31753541Sshin * unspec dup address detection 31853541Sshin * 31953541Sshin * The processing is defined in RFC 2462. 32053541Sshin */ 32153541Sshin if (tentative) { 32253541Sshin /* 32353541Sshin * If source address is unspecified address, it is for 324148987Sume * duplicate address detection. 32553541Sshin * 32653541Sshin * If not, the packet is for addess resolution; 32753541Sshin * silently ignore it. 32853541Sshin */ 32953541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 33053541Sshin nd6_dad_ns_input(ifa); 33153541Sshin 33262587Sitojun goto freeit; 33353541Sshin } 33453541Sshin 33553541Sshin /* 33653541Sshin * If the source address is unspecified address, entries must not 33753541Sshin * be created or updated. 33853541Sshin * It looks that sender is performing DAD. Output NA toward 33953541Sshin * all-node multicast address, to tell the sender that I'm using 34053541Sshin * the address. 34153541Sshin * S bit ("solicited") must be zero. 34253541Sshin */ 34353541Sshin if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { 344148385Sume struct in6_addr in6_all; 345148385Sume 346148385Sume in6_all = in6addr_linklocal_allnodes; 347148385Sume if (in6_setscope(&in6_all, ifp, NULL) != 0) 348148385Sume goto bad; 349231852Sbz nd6_na_output_fib(ifp, &in6_all, &taddr6, 350120941Sume ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 351231852Sbz rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL, 352231852Sbz M_GETFIB(m)); 35362587Sitojun goto freeit; 35453541Sshin } 35553541Sshin 356120941Sume nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, 357120941Sume ND_NEIGHBOR_SOLICIT, 0); 35853541Sshin 359231852Sbz nd6_na_output_fib(ifp, &saddr6, &taddr6, 360120941Sume ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | 361222728Shrs rflag | ND_NA_FLAG_SOLICITED, tlladdr, 362231852Sbz proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m)); 36362587Sitojun freeit: 364194760Srwatson if (ifa != NULL) 365194760Srwatson ifa_free(ifa); 36662587Sitojun m_freem(m); 36753541Sshin return; 36853541Sshin 36953541Sshin bad: 370165118Sbz nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", 371165118Sbz ip6_sprintf(ip6bufs, &saddr6))); 372165118Sbz nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", 373165118Sbz ip6_sprintf(ip6bufs, &daddr6))); 374165118Sbz nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", 375165118Sbz ip6_sprintf(ip6bufs, &taddr6))); 376190964Srwatson ICMP6STAT_INC(icp6s_badns); 377194760Srwatson if (ifa != NULL) 378194760Srwatson ifa_free(ifa); 37962587Sitojun m_freem(m); 38053541Sshin} 38153541Sshin 38253541Sshin/* 383108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies: 38453541Sshin * - ICMP6 header source IP6 address 38553541Sshin * - ND6 header target IP6 address 38653541Sshin * - ND6 header source datalink address 38753541Sshin * 38853541Sshin * Based on RFC 2461 389148987Sume * Based on RFC 2462 (duplicate address detection) 390171259Sdelphij * 391171259Sdelphij * ln - for source address determination 392171259Sdelphij * dad - duplicate address detection 39353541Sshin */ 39453541Sshinvoid 395186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6, 396186119Sqingli const struct in6_addr *taddr6, struct llentry *ln, int dad) 39753541Sshin{ 39853541Sshin struct mbuf *m; 399211501Sanchie struct m_tag *mtag; 40053541Sshin struct ip6_hdr *ip6; 40153541Sshin struct nd_neighbor_solicit *nd_ns; 40253541Sshin struct ip6_moptions im6o; 40353541Sshin int icmp6len; 40462587Sitojun int maxlen; 40553541Sshin caddr_t mac; 406148385Sume struct route_in6 ro; 407120941Sume 40853541Sshin if (IN6_IS_ADDR_MULTICAST(taddr6)) 40953541Sshin return; 41053541Sshin 41162587Sitojun /* estimate the size of message */ 41262587Sitojun maxlen = sizeof(*ip6) + sizeof(*nd_ns); 41362587Sitojun maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 41462587Sitojun if (max_linkhdr + maxlen >= MCLBYTES) { 41562587Sitojun#ifdef DIAGNOSTIC 41662587Sitojun printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES " 41762587Sitojun "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 41862587Sitojun#endif 41953541Sshin return; 42062587Sitojun } 42153541Sshin 422248328Sglebius if (max_linkhdr + maxlen > MHLEN) 423248328Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 424248328Sglebius else 425248328Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 42662587Sitojun if (m == NULL) 42762587Sitojun return; 42862587Sitojun 429215559Sbz bzero(&ro, sizeof(ro)); 430215559Sbz 43153541Sshin if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) { 43253541Sshin m->m_flags |= M_MCAST; 43353541Sshin im6o.im6o_multicast_ifp = ifp; 43453541Sshin im6o.im6o_multicast_hlim = 255; 43553541Sshin im6o.im6o_multicast_loop = 0; 43653541Sshin } 43753541Sshin 43853541Sshin icmp6len = sizeof(*nd_ns); 43953541Sshin m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; 44095023Ssuz m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 44153541Sshin 44253541Sshin /* fill neighbor solicitation packet */ 44353541Sshin ip6 = mtod(m, struct ip6_hdr *); 44453541Sshin ip6->ip6_flow = 0; 44562587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 44662587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 44753541Sshin /* ip6->ip6_plen will be set later */ 44853541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 44953541Sshin ip6->ip6_hlim = 255; 45053541Sshin if (daddr6) 45153541Sshin ip6->ip6_dst = *daddr6; 45253541Sshin else { 45353541Sshin ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 454148385Sume ip6->ip6_dst.s6_addr16[1] = 0; 45553541Sshin ip6->ip6_dst.s6_addr32[1] = 0; 45653541Sshin ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; 45753541Sshin ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; 45853541Sshin ip6->ip6_dst.s6_addr8[12] = 0xff; 459148385Sume if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) 460148385Sume goto bad; 46153541Sshin } 46253541Sshin if (!dad) { 463194760Srwatson struct ifaddr *ifa; 464194760Srwatson 46553541Sshin /* 46653541Sshin * RFC2461 7.2.2: 46753541Sshin * "If the source address of the packet prompting the 46853541Sshin * solicitation is the same as one of the addresses assigned 46953541Sshin * to the outgoing interface, that address SHOULD be placed 47053541Sshin * in the IP Source Address of the outgoing solicitation. 47153541Sshin * Otherwise, any one of the addresses assigned to the 47253541Sshin * interface should be used." 47353541Sshin * 47453541Sshin * We use the source address for the prompting packet 47553541Sshin * (saddr6), if: 47653541Sshin * - saddr6 is given from the caller (by giving "ln"), and 47753541Sshin * - saddr6 belongs to the outgoing interface. 478148385Sume * Otherwise, we perform the source address selection as usual. 47953541Sshin */ 480216022Sbz struct in6_addr *hsrc; 48153541Sshin 482216022Sbz hsrc = NULL; 483216022Sbz if (ln != NULL) { 484216022Sbz LLE_RLOCK(ln); 485216022Sbz if (ln->la_hold != NULL) { 486216022Sbz struct ip6_hdr *hip6; /* hold ip6 */ 487216022Sbz 488216022Sbz /* 489216022Sbz * assuming every packet in la_hold has the same IP 490216022Sbz * header 491216022Sbz */ 492216022Sbz hip6 = mtod(ln->la_hold, struct ip6_hdr *); 493216022Sbz /* XXX pullup? */ 494216022Sbz if (sizeof(*hip6) < ln->la_hold->m_len) { 495216022Sbz ip6->ip6_src = hip6->ip6_src; 496216022Sbz hsrc = &hip6->ip6_src; 497216022Sbz } 498216022Sbz } 499216022Sbz LLE_RUNLOCK(ln); 500148385Sume } 501194760Srwatson if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 502194760Srwatson hsrc)) != NULL) { 503216022Sbz /* ip6_src set already. */ 504194760Srwatson ifa_free(ifa); 505194760Srwatson } else { 506148385Sume int error; 507148385Sume struct sockaddr_in6 dst_sa; 508216022Sbz struct in6_addr src_in; 509231852Sbz struct ifnet *oifp; 510148385Sume 511148385Sume bzero(&dst_sa, sizeof(dst_sa)); 512148385Sume dst_sa.sin6_family = AF_INET6; 513148385Sume dst_sa.sin6_len = sizeof(dst_sa); 514148385Sume dst_sa.sin6_addr = ip6->ip6_dst; 515148385Sume 516231852Sbz oifp = ifp; 517194777Sbz error = in6_selectsrc(&dst_sa, NULL, 518231852Sbz NULL, &ro, NULL, &oifp, &src_in); 519194777Sbz if (error) { 520165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 521148385Sume nd6log((LOG_DEBUG, 522148385Sume "nd6_ns_output: source can't be " 523148385Sume "determined: dst=%s, error=%d\n", 524165118Sbz ip6_sprintf(ip6buf, &dst_sa.sin6_addr), 525165118Sbz error)); 526148385Sume goto bad; 52753541Sshin } 528216022Sbz ip6->ip6_src = src_in; 52953541Sshin } 53053541Sshin } else { 53153541Sshin /* 53253541Sshin * Source address for DAD packet must always be IPv6 53353541Sshin * unspecified address. (0::0) 534148385Sume * We actually don't have to 0-clear the address (we did it 535148385Sume * above), but we do so here explicitly to make the intention 536148385Sume * clearer. 53753541Sshin */ 538216022Sbz bzero(&ip6->ip6_src, sizeof(ip6->ip6_src)); 53953541Sshin } 54053541Sshin nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1); 54153541Sshin nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; 54253541Sshin nd_ns->nd_ns_code = 0; 54353541Sshin nd_ns->nd_ns_reserved = 0; 54453541Sshin nd_ns->nd_ns_target = *taddr6; 545121315Sume in6_clearscope(&nd_ns->nd_ns_target); /* XXX */ 54653541Sshin 54753541Sshin /* 54853541Sshin * Add source link-layer address option. 54953541Sshin * 55053541Sshin * spec implementation 55153541Sshin * --- --- 55253541Sshin * DAD packet MUST NOT do not add the option 55353541Sshin * there's no link layer address: 55453541Sshin * impossible do not add the option 55553541Sshin * there's link layer address: 55653541Sshin * Multicast NS MUST add one add the option 55753541Sshin * Unicast NS SHOULD add one add the option 55853541Sshin */ 55953541Sshin if (!dad && (mac = nd6_ifptomac(ifp))) { 56053541Sshin int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 56153541Sshin struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); 56253541Sshin /* 8 byte alignments... */ 56353541Sshin optlen = (optlen + 7) & ~7; 564120941Sume 56553541Sshin m->m_pkthdr.len += optlen; 56653541Sshin m->m_len += optlen; 56753541Sshin icmp6len += optlen; 56853541Sshin bzero((caddr_t)nd_opt, optlen); 56953541Sshin nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; 57053541Sshin nd_opt->nd_opt_len = optlen >> 3; 57153541Sshin bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 57253541Sshin } 57353541Sshin 57453541Sshin ip6->ip6_plen = htons((u_short)icmp6len); 57553541Sshin nd_ns->nd_ns_cksum = 0; 576120941Sume nd_ns->nd_ns_cksum = 577120941Sume in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); 57853541Sshin 579211501Sanchie if (send_sendso_input_hook != NULL) { 580211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 581211501Sanchie sizeof(unsigned short), M_NOWAIT); 582211501Sanchie if (mtag == NULL) 583211501Sanchie goto bad; 584211501Sanchie *(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type; 585211501Sanchie m_tag_prepend(m, mtag); 586211501Sanchie } 587211501Sanchie 588151536Ssuz ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL); 589148385Sume icmp6_ifstat_inc(ifp, ifs6_out_msg); 590148385Sume icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit); 591190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]); 592148385Sume 593238092Sglebius /* We don't cache this route. */ 594238092Sglebius RO_RTFREE(&ro); 595238092Sglebius 596148385Sume return; 597148385Sume 598148385Sume bad: 599148385Sume if (ro.ro_rt) { 600148385Sume RTFREE(ro.ro_rt); 601148385Sume } 602148385Sume m_freem(m); 603148385Sume return; 60453541Sshin} 60553541Sshin 60653541Sshin/* 60753541Sshin * Neighbor advertisement input handling. 60853541Sshin * 60953541Sshin * Based on RFC 2461 610148987Sume * Based on RFC 2462 (duplicate address detection) 61162587Sitojun * 61262587Sitojun * the following items are not implemented yet: 61362587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 61462587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 61553541Sshin */ 61653541Sshinvoid 617171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len) 61853541Sshin{ 61953541Sshin struct ifnet *ifp = m->m_pkthdr.rcvif; 62053541Sshin struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 62162587Sitojun struct nd_neighbor_advert *nd_na; 62253541Sshin struct in6_addr daddr6 = ip6->ip6_dst; 62362587Sitojun struct in6_addr taddr6; 62462587Sitojun int flags; 62562587Sitojun int is_router; 62662587Sitojun int is_solicited; 62762587Sitojun int is_override; 62853541Sshin char *lladdr = NULL; 62953541Sshin int lladdrlen = 0; 630186468Skmacy int checklink = 0; 63153541Sshin struct ifaddr *ifa; 632186119Sqingli struct llentry *ln = NULL; 63353541Sshin union nd_opts ndopts; 634186119Sqingli struct mbuf *chain = NULL; 635211501Sanchie struct m_tag *mtag; 636186119Sqingli struct sockaddr_in6 sin6; 637165118Sbz char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 63853541Sshin 63953541Sshin if (ip6->ip6_hlim != 255) { 64078064Sume nd6log((LOG_ERR, 64178064Sume "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", 642165118Sbz ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 643165118Sbz ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 64478064Sume goto bad; 64562587Sitojun } 64662587Sitojun 64762587Sitojun#ifndef PULLDOWN_TEST 64862587Sitojun IP6_EXTHDR_CHECK(m, off, icmp6len,); 64962587Sitojun nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); 65062587Sitojun#else 65162587Sitojun IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len); 65262587Sitojun if (nd_na == NULL) { 653190964Srwatson ICMP6STAT_INC(icp6s_tooshort); 65453541Sshin return; 65553541Sshin } 65662587Sitojun#endif 657148385Sume 65862587Sitojun flags = nd_na->nd_na_flags_reserved; 65962587Sitojun is_router = ((flags & ND_NA_FLAG_ROUTER) != 0); 66062587Sitojun is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0); 66162587Sitojun is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); 66253541Sshin 663148385Sume taddr6 = nd_na->nd_na_target; 664148385Sume if (in6_setscope(&taddr6, ifp, NULL)) 665150202Ssuz goto bad; /* XXX: impossible */ 66653541Sshin 66753541Sshin if (IN6_IS_ADDR_MULTICAST(&taddr6)) { 66878064Sume nd6log((LOG_ERR, 66953541Sshin "nd6_na_input: invalid target address %s\n", 670165118Sbz ip6_sprintf(ip6bufs, &taddr6))); 67178064Sume goto bad; 67253541Sshin } 67353541Sshin if (IN6_IS_ADDR_MULTICAST(&daddr6)) 67453541Sshin if (is_solicited) { 67578064Sume nd6log((LOG_ERR, 67678064Sume "nd6_na_input: a solicited adv is multicasted\n")); 67778064Sume goto bad; 67853541Sshin } 67953541Sshin 68053541Sshin icmp6len -= sizeof(*nd_na); 68153541Sshin nd6_option_init(nd_na + 1, icmp6len, &ndopts); 68253541Sshin if (nd6_options(&ndopts) < 0) { 68378064Sume nd6log((LOG_INFO, 68478064Sume "nd6_na_input: invalid ND option, ignored\n")); 68578064Sume /* nd6_options have incremented stats */ 68662587Sitojun goto freeit; 68753541Sshin } 68853541Sshin 68953541Sshin if (ndopts.nd_opts_tgt_lladdr) { 69053541Sshin lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); 69153541Sshin lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; 69253541Sshin } 69353541Sshin 694228571Sglebius /* 695228571Sglebius * This effectively disables the DAD check on a non-master CARP 696228571Sglebius * address. 697228571Sglebius */ 698228571Sglebius if (ifp->if_carp) 699228571Sglebius ifa = (*carp_iamatch6_p)(ifp, &taddr6); 700228571Sglebius else 701228571Sglebius ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); 70253541Sshin 70353541Sshin /* 70453541Sshin * Target address matches one of my interface address. 70553541Sshin * 70653541Sshin * If my address is tentative, this means that there's somebody 70753541Sshin * already using the same address as mine. This indicates DAD failure. 70853541Sshin * This is defined in RFC 2462. 70953541Sshin * 71053541Sshin * Otherwise, process as defined in RFC 2461. 71153541Sshin */ 71253541Sshin if (ifa 71353541Sshin && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) { 714194760Srwatson ifa_free(ifa); 71553541Sshin nd6_dad_na_input(ifa); 71662587Sitojun goto freeit; 71753541Sshin } 71853541Sshin 71995023Ssuz /* Just for safety, maybe unnecessary. */ 72053541Sshin if (ifa) { 721194760Srwatson ifa_free(ifa); 72253541Sshin log(LOG_ERR, 72353541Sshin "nd6_na_input: duplicate IP6 address %s\n", 724165118Sbz ip6_sprintf(ip6bufs, &taddr6)); 72562587Sitojun goto freeit; 72653541Sshin } 72753541Sshin 72853541Sshin if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 729120941Sume nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s " 730165118Sbz "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6), 731120941Sume ifp->if_addrlen, lladdrlen - 2)); 73278064Sume goto bad; 73353541Sshin } 73453541Sshin 73553541Sshin /* 736120941Sume * If no neighbor cache entry is found, NA SHOULD silently be 737120941Sume * discarded. 73853541Sshin */ 739186119Sqingli IF_AFDATA_LOCK(ifp); 740186119Sqingli ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp); 741186119Sqingli IF_AFDATA_UNLOCK(ifp); 742186119Sqingli if (ln == NULL) { 74362587Sitojun goto freeit; 744186119Sqingli } 74553541Sshin 74653541Sshin if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 74753541Sshin /* 74853541Sshin * If the link-layer has address, and no lladdr option came, 74953541Sshin * discard the packet. 75053541Sshin */ 751186119Sqingli if (ifp->if_addrlen && lladdr == NULL) { 75262587Sitojun goto freeit; 753186119Sqingli } 75453541Sshin 75553541Sshin /* 75653541Sshin * Record link-layer address, and update the state. 75753541Sshin */ 758186119Sqingli bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen); 759186119Sqingli ln->la_flags |= LLE_VALID; 760245925Snp EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 76153541Sshin if (is_solicited) { 76253541Sshin ln->ln_state = ND6_LLINFO_REACHABLE; 76362587Sitojun ln->ln_byhint = 0; 764151539Ssuz if (!ND6_LLINFO_PERMANENT(ln)) { 765186119Sqingli nd6_llinfo_settimer_locked(ln, 766186119Sqingli (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz); 767120941Sume } 76878064Sume } else { 76953541Sshin ln->ln_state = ND6_LLINFO_STALE; 770186119Sqingli nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 77178064Sume } 77278064Sume if ((ln->ln_router = is_router) != 0) { 77378064Sume /* 77478064Sume * This means a router's state has changed from 77578064Sume * non-reachable to probably reachable, and might 77678064Sume * affect the status of associated prefixes.. 77778064Sume */ 778186468Skmacy checklink = 1; 77978064Sume } 78053541Sshin } else { 78153541Sshin int llchange; 78253541Sshin 78353541Sshin /* 78453541Sshin * Check if the link-layer address has changed or not. 78553541Sshin */ 786151465Ssuz if (lladdr == NULL) 78753541Sshin llchange = 0; 78853541Sshin else { 789186119Sqingli if (ln->la_flags & LLE_VALID) { 790186119Sqingli if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen)) 79153541Sshin llchange = 1; 79253541Sshin else 79353541Sshin llchange = 0; 79453541Sshin } else 79553541Sshin llchange = 1; 79653541Sshin } 79753541Sshin 79853541Sshin /* 79953541Sshin * This is VERY complex. Look at it with care. 80053541Sshin * 80153541Sshin * override solicit lladdr llchange action 80253541Sshin * (L: record lladdr) 80353541Sshin * 80453541Sshin * 0 0 n -- (2c) 80553541Sshin * 0 0 y n (2b) L 80653541Sshin * 0 0 y y (1) REACHABLE->STALE 80753541Sshin * 0 1 n -- (2c) *->REACHABLE 80853541Sshin * 0 1 y n (2b) L *->REACHABLE 80953541Sshin * 0 1 y y (1) REACHABLE->STALE 81053541Sshin * 1 0 n -- (2a) 81153541Sshin * 1 0 y n (2a) L 81253541Sshin * 1 0 y y (2a) L *->STALE 81353541Sshin * 1 1 n -- (2a) *->REACHABLE 81453541Sshin * 1 1 y n (2a) L *->REACHABLE 81553541Sshin * 1 1 y y (2a) L *->REACHABLE 81653541Sshin */ 817151539Ssuz if (!is_override && (lladdr != NULL && llchange)) { /* (1) */ 81853541Sshin /* 81953541Sshin * If state is REACHABLE, make it STALE. 82053541Sshin * no other updates should be done. 82153541Sshin */ 82278064Sume if (ln->ln_state == ND6_LLINFO_REACHABLE) { 82353541Sshin ln->ln_state = ND6_LLINFO_STALE; 824186119Sqingli nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 82578064Sume } 82662587Sitojun goto freeit; 82753541Sshin } else if (is_override /* (2a) */ 828151465Ssuz || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */ 829151465Ssuz || lladdr == NULL) { /* (2c) */ 83053541Sshin /* 83153541Sshin * Update link-local address, if any. 83253541Sshin */ 833151465Ssuz if (lladdr != NULL) { 834186119Sqingli bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen); 835186119Sqingli ln->la_flags |= LLE_VALID; 836245925Snp EVENTHANDLER_INVOKE(lle_event, ln, 837245925Snp LLENTRY_RESOLVED); 83853541Sshin } 83953541Sshin 84053541Sshin /* 84153541Sshin * If solicited, make the state REACHABLE. 84253541Sshin * If not solicited and the link-layer address was 84353541Sshin * changed, make it STALE. 84453541Sshin */ 84553541Sshin if (is_solicited) { 84653541Sshin ln->ln_state = ND6_LLINFO_REACHABLE; 84762587Sitojun ln->ln_byhint = 0; 848151539Ssuz if (!ND6_LLINFO_PERMANENT(ln)) { 849186119Sqingli nd6_llinfo_settimer_locked(ln, 850151539Ssuz (long)ND_IFINFO(ifp)->reachable * hz); 85153541Sshin } 85253541Sshin } else { 853151465Ssuz if (lladdr != NULL && llchange) { 85453541Sshin ln->ln_state = ND6_LLINFO_STALE; 855186119Sqingli nd6_llinfo_settimer_locked(ln, 856181803Sbz (long)V_nd6_gctimer * hz); 85778064Sume } 85853541Sshin } 85953541Sshin } 86053541Sshin 86153541Sshin if (ln->ln_router && !is_router) { 86253541Sshin /* 86353541Sshin * The peer dropped the router flag. 86453541Sshin * Remove the sender from the Default Router List and 86553541Sshin * update the Destination Cache entries. 86653541Sshin */ 86753541Sshin struct nd_defrouter *dr; 86853541Sshin struct in6_addr *in6; 86953541Sshin 870186119Sqingli in6 = &L3_ADDR_SIN6(ln)->sin6_addr; 87195023Ssuz 87295023Ssuz /* 87395023Ssuz * Lock to protect the default router list. 87495023Ssuz * XXX: this might be unnecessary, since this function 87595023Ssuz * is only called under the network software interrupt 876120941Sume * context. However, we keep it just for safety. 87795023Ssuz */ 878186119Sqingli dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp); 87953541Sshin if (dr) 88053541Sshin defrtrlist_del(dr); 881222728Shrs else if (ND_IFINFO(ln->lle_tbl->llt_ifp)->flags & 882222728Shrs ND6_IFF_ACCEPT_RTADV) { 88353541Sshin /* 88453541Sshin * Even if the neighbor is not in the default 88553541Sshin * router list, the neighbor may be used 88653541Sshin * as a next hop for some destinations 88753541Sshin * (e.g. redirect case). So we must 88853541Sshin * call rt6_flush explicitly. 88953541Sshin */ 890128422Sluigi rt6_flush(&ip6->ip6_src, ifp); 89153541Sshin } 89253541Sshin } 89353541Sshin ln->ln_router = is_router; 89453541Sshin } 895186119Sqingli /* XXX - QL 896186119Sqingli * Does this matter? 897186119Sqingli * rt->rt_flags &= ~RTF_REJECT; 898186119Sqingli */ 899186119Sqingli ln->la_asked = 0; 900186119Sqingli if (ln->la_hold) { 901151539Ssuz struct mbuf *m_hold, *m_hold_next; 902151539Ssuz 903169273Ssuz /* 904186119Sqingli * reset the la_hold in advance, to explicitly 905186119Sqingli * prevent a la_hold lookup in nd6_output() 906169273Ssuz * (wouldn't happen, though...) 907169273Ssuz */ 908186119Sqingli for (m_hold = ln->la_hold, ln->la_hold = NULL; 909169241Ssuz m_hold; m_hold = m_hold_next) { 910151539Ssuz m_hold_next = m_hold->m_nextpkt; 911169241Ssuz m_hold->m_nextpkt = NULL; 912151539Ssuz /* 913151539Ssuz * we assume ifp is not a loopback here, so just set 914151539Ssuz * the 2nd argument as the 1st one. 915151539Ssuz */ 916211501Sanchie 917211501Sanchie if (send_sendso_input_hook != NULL) { 918211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 919211501Sanchie sizeof(unsigned short), M_NOWAIT); 920211501Sanchie if (mtag == NULL) 921211501Sanchie goto bad; 922211501Sanchie m_tag_prepend(m, mtag); 923211501Sanchie } 924211501Sanchie 925186119Sqingli nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain); 926151539Ssuz } 92753541Sshin } 928186119Sqingli freeit: 929186160Skmacy if (ln != NULL) { 930186119Sqingli if (chain) 931186119Sqingli memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6)); 932186119Sqingli LLE_WUNLOCK(ln); 93362587Sitojun 934186119Sqingli if (chain) 935186119Sqingli nd6_output_flush(ifp, ifp, chain, &sin6, NULL); 936186119Sqingli } 937186468Skmacy if (checklink) 938186468Skmacy pfxlist_onlink_check(); 939186468Skmacy 94062587Sitojun m_freem(m); 94178064Sume return; 94278064Sume 94378064Sume bad: 944186160Skmacy if (ln != NULL) 945186119Sqingli LLE_WUNLOCK(ln); 946186119Sqingli 947190964Srwatson ICMP6STAT_INC(icp6s_badna); 94878064Sume m_freem(m); 94953541Sshin} 95053541Sshin 95153541Sshin/* 95253541Sshin * Neighbor advertisement output handling. 95353541Sshin * 95453541Sshin * Based on RFC 2461 95553541Sshin * 95662587Sitojun * the following items are not implemented yet: 95762587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) 95862587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) 959171259Sdelphij * 960171259Sdelphij * tlladdr - 1 if include target link-layer address 961171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL 96253541Sshin */ 963231852Sbzstatic void 964231852Sbznd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0, 965171259Sdelphij const struct in6_addr *taddr6, u_long flags, int tlladdr, 966231852Sbz struct sockaddr *sdl0, u_int fibnum) 96753541Sshin{ 96853541Sshin struct mbuf *m; 969211501Sanchie struct m_tag *mtag; 970231852Sbz struct ifnet *oifp; 97153541Sshin struct ip6_hdr *ip6; 97253541Sshin struct nd_neighbor_advert *nd_na; 97353541Sshin struct ip6_moptions im6o; 974194777Sbz struct in6_addr src, daddr6; 975148385Sume struct sockaddr_in6 dst_sa; 976148385Sume int icmp6len, maxlen, error; 97792733Speter caddr_t mac = NULL; 978148385Sume struct route_in6 ro; 97962587Sitojun 980148385Sume bzero(&ro, sizeof(ro)); 981148385Sume 982148385Sume daddr6 = *daddr6_0; /* make a local copy for modification */ 983148385Sume 98462587Sitojun /* estimate the size of message */ 98562587Sitojun maxlen = sizeof(*ip6) + sizeof(*nd_na); 98662587Sitojun maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; 98762587Sitojun if (max_linkhdr + maxlen >= MCLBYTES) { 98862587Sitojun#ifdef DIAGNOSTIC 98962587Sitojun printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES " 99062587Sitojun "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); 99162587Sitojun#endif 99253541Sshin return; 99362587Sitojun } 99453541Sshin 995248328Sglebius if (max_linkhdr + maxlen > MHLEN) 996248328Sglebius m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 997248328Sglebius else 998248328Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 99962587Sitojun if (m == NULL) 100062587Sitojun return; 1001231852Sbz M_SETFIB(m, fibnum); 100262587Sitojun 1003148385Sume if (IN6_IS_ADDR_MULTICAST(&daddr6)) { 100453541Sshin m->m_flags |= M_MCAST; 100553541Sshin im6o.im6o_multicast_ifp = ifp; 100653541Sshin im6o.im6o_multicast_hlim = 255; 100753541Sshin im6o.im6o_multicast_loop = 0; 100853541Sshin } 100953541Sshin 101053541Sshin icmp6len = sizeof(*nd_na); 101153541Sshin m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len; 101295023Ssuz m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ 101353541Sshin 101453541Sshin /* fill neighbor advertisement packet */ 101553541Sshin ip6 = mtod(m, struct ip6_hdr *); 101653541Sshin ip6->ip6_flow = 0; 101762587Sitojun ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 101862587Sitojun ip6->ip6_vfc |= IPV6_VERSION; 101953541Sshin ip6->ip6_nxt = IPPROTO_ICMPV6; 102053541Sshin ip6->ip6_hlim = 255; 1021148385Sume if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) { 102253541Sshin /* reply to DAD */ 1023153227Sume daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 1024153227Sume daddr6.s6_addr16[1] = 0; 1025153227Sume daddr6.s6_addr32[1] = 0; 1026153227Sume daddr6.s6_addr32[2] = 0; 1027153227Sume daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE; 1028148385Sume if (in6_setscope(&daddr6, ifp, NULL)) 1029148385Sume goto bad; 1030148385Sume 103153541Sshin flags &= ~ND_NA_FLAG_SOLICITED; 1032148385Sume } 1033148385Sume ip6->ip6_dst = daddr6; 1034148385Sume bzero(&dst_sa, sizeof(struct sockaddr_in6)); 1035148385Sume dst_sa.sin6_family = AF_INET6; 1036148385Sume dst_sa.sin6_len = sizeof(struct sockaddr_in6); 1037148385Sume dst_sa.sin6_addr = daddr6; 103853541Sshin 103953541Sshin /* 104053541Sshin * Select a source whose scope is the same as that of the dest. 104153541Sshin */ 1042148385Sume bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa)); 1043231852Sbz oifp = ifp; 1044231852Sbz error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &oifp, &src); 1045194777Sbz if (error) { 1046165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 1047148385Sume nd6log((LOG_DEBUG, "nd6_na_output: source can't be " 1048148385Sume "determined: dst=%s, error=%d\n", 1049165118Sbz ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error)); 1050148385Sume goto bad; 105153541Sshin } 1052194777Sbz ip6->ip6_src = src; 105353541Sshin nd_na = (struct nd_neighbor_advert *)(ip6 + 1); 105453541Sshin nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; 105553541Sshin nd_na->nd_na_code = 0; 105653541Sshin nd_na->nd_na_target = *taddr6; 1057121315Sume in6_clearscope(&nd_na->nd_na_target); /* XXX */ 105853541Sshin 105953541Sshin /* 106053541Sshin * "tlladdr" indicates NS's condition for adding tlladdr or not. 106153541Sshin * see nd6_ns_input() for details. 106253541Sshin * Basically, if NS packet is sent to unicast/anycast addr, 106353541Sshin * target lladdr option SHOULD NOT be included. 106453541Sshin */ 106562587Sitojun if (tlladdr) { 106662587Sitojun /* 106762587Sitojun * sdl0 != NULL indicates proxy NA. If we do proxy, use 106862587Sitojun * lladdr in sdl0. If we are not proxying (sending NA for 106962587Sitojun * my address) use lladdr configured for the interface. 107062587Sitojun */ 1071142215Sglebius if (sdl0 == NULL) { 1072142215Sglebius if (ifp->if_carp) 1073211157Swill mac = (*carp_macmatch6_p)(ifp, m, taddr6); 1074142215Sglebius if (mac == NULL) 1075142215Sglebius mac = nd6_ifptomac(ifp); 1076142215Sglebius } else if (sdl0->sa_family == AF_LINK) { 107762587Sitojun struct sockaddr_dl *sdl; 107862587Sitojun sdl = (struct sockaddr_dl *)sdl0; 107962587Sitojun if (sdl->sdl_alen == ifp->if_addrlen) 108062587Sitojun mac = LLADDR(sdl); 108162587Sitojun } 108262587Sitojun } 108362587Sitojun if (tlladdr && mac) { 108453541Sshin int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; 108553541Sshin struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1); 1086120941Sume 108753541Sshin /* roundup to 8 bytes alignment! */ 108853541Sshin optlen = (optlen + 7) & ~7; 108953541Sshin 109053541Sshin m->m_pkthdr.len += optlen; 109153541Sshin m->m_len += optlen; 109253541Sshin icmp6len += optlen; 109353541Sshin bzero((caddr_t)nd_opt, optlen); 109453541Sshin nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 109553541Sshin nd_opt->nd_opt_len = optlen >> 3; 109653541Sshin bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); 109753541Sshin } else 109853541Sshin flags &= ~ND_NA_FLAG_OVERRIDE; 109953541Sshin 110053541Sshin ip6->ip6_plen = htons((u_short)icmp6len); 110153541Sshin nd_na->nd_na_flags_reserved = flags; 110253541Sshin nd_na->nd_na_cksum = 0; 110353541Sshin nd_na->nd_na_cksum = 1104120941Sume in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); 110553541Sshin 1106211501Sanchie if (send_sendso_input_hook != NULL) { 1107211501Sanchie mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 1108211501Sanchie sizeof(unsigned short), M_NOWAIT); 1109211501Sanchie if (mtag == NULL) 1110211501Sanchie goto bad; 1111211501Sanchie *(unsigned short *)(mtag + 1) = nd_na->nd_na_type; 1112211501Sanchie m_tag_prepend(m, mtag); 1113211501Sanchie } 1114211501Sanchie 1115148385Sume ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL); 1116148385Sume icmp6_ifstat_inc(ifp, ifs6_out_msg); 1117148385Sume icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert); 1118190964Srwatson ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]); 1119148385Sume 1120238092Sglebius /* We don't cache this route. */ 1121238092Sglebius RO_RTFREE(&ro); 1122238092Sglebius 1123148385Sume return; 1124148385Sume 1125148385Sume bad: 1126148385Sume if (ro.ro_rt) { 1127148385Sume RTFREE(ro.ro_rt); 1128148385Sume } 1129148385Sume m_freem(m); 1130148385Sume return; 113153541Sshin} 113253541Sshin 1133231852Sbz#ifndef BURN_BRIDGES 1134231852Sbzvoid 1135231852Sbznd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0, 1136231852Sbz const struct in6_addr *taddr6, u_long flags, int tlladdr, 1137231852Sbz struct sockaddr *sdl0) 1138231852Sbz{ 1139231852Sbz 1140231852Sbz nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0, 1141231852Sbz RT_DEFAULT_FIB); 1142231852Sbz} 1143231852Sbz#endif 1144231852Sbz 114553541Sshincaddr_t 1146171259Sdelphijnd6_ifptomac(struct ifnet *ifp) 114753541Sshin{ 114853541Sshin switch (ifp->if_type) { 114953541Sshin case IFT_ARCNET: 115053541Sshin case IFT_ETHER: 115153541Sshin case IFT_FDDI: 115278064Sume case IFT_IEEE1394: 115378468Ssumikawa#ifdef IFT_L2VLAN 115478468Ssumikawa case IFT_L2VLAN: 115578468Ssumikawa#endif 115678064Sume#ifdef IFT_IEEE80211 115778064Sume case IFT_IEEE80211: 115878064Sume#endif 1159219819Sjeff case IFT_INFINIBAND: 1160149829Sthompsa case IFT_BRIDGE: 1161120049Smdodd case IFT_ISO88025: 1162147306Sbrooks return IF_LLADDR(ifp); 116353541Sshin default: 116453541Sshin return NULL; 116553541Sshin } 116653541Sshin} 116753541Sshin 116853541Sshinstruct dadq { 116960938Sjake TAILQ_ENTRY(dadq) dad_list; 117053541Sshin struct ifaddr *dad_ifa; 117153541Sshin int dad_count; /* max NS to send */ 117262587Sitojun int dad_ns_tcount; /* # of trials to send NS */ 117353541Sshin int dad_ns_ocount; /* NS sent so far */ 117453541Sshin int dad_ns_icount; 117553541Sshin int dad_na_icount; 117678064Sume struct callout dad_timer_ch; 1177191816Szec struct vnet *dad_vnet; 117853541Sshin}; 117953541Sshin 1180215701Sdimstatic VNET_DEFINE(TAILQ_HEAD(, dadq), dadq); 1181207369SbzVNET_DEFINE(int, dad_init) = 0; 1182195727Srwatson#define V_dadq VNET(dadq) 1183195727Srwatson#define V_dad_init VNET(dad_init) 1184195699Srwatson 118553541Sshinstatic struct dadq * 1186171259Sdelphijnd6_dad_find(struct ifaddr *ifa) 118753541Sshin{ 118853541Sshin struct dadq *dp; 118953541Sshin 1190226340Sglebius TAILQ_FOREACH(dp, &V_dadq, dad_list) 119153541Sshin if (dp->dad_ifa == ifa) 1192226340Sglebius return (dp); 1193226340Sglebius 1194226340Sglebius return (NULL); 119553541Sshin} 119653541Sshin 119778064Sumestatic void 1198171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks) 119978064Sume{ 120078064Sume 120178064Sume callout_reset(&dp->dad_timer_ch, ticks, 1202191816Szec (void (*)(void *))nd6_dad_timer, (void *)dp); 120378064Sume} 120478064Sume 120578064Sumestatic void 1206171259Sdelphijnd6_dad_stoptimer(struct dadq *dp) 120778064Sume{ 120878064Sume 120978064Sume callout_stop(&dp->dad_timer_ch); 121078064Sume} 121178064Sume 121253541Sshin/* 1213148987Sume * Start Duplicate Address Detection (DAD) for specified interface address. 121453541Sshin */ 121553541Sshinvoid 1216171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay) 121753541Sshin{ 121853541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 121953541Sshin struct dadq *dp; 1220165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 122153541Sshin 1222181803Sbz if (!V_dad_init) { 1223181803Sbz TAILQ_INIT(&V_dadq); 1224181803Sbz V_dad_init++; 122553541Sshin } 122653541Sshin 122753541Sshin /* 122853541Sshin * If we don't need DAD, don't do it. 122953541Sshin * There are several cases: 123053541Sshin * - DAD is disabled (ip6_dad_count == 0) 123153541Sshin * - the interface address is anycast 123253541Sshin */ 123353541Sshin if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) { 123462587Sitojun log(LOG_DEBUG, 123562587Sitojun "nd6_dad_start: called with non-tentative address " 123653541Sshin "%s(%s)\n", 1237165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 123853541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 123953541Sshin return; 124053541Sshin } 124153541Sshin if (ia->ia6_flags & IN6_IFF_ANYCAST) { 124253541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 124353541Sshin return; 124453541Sshin } 1245181803Sbz if (!V_ip6_dad_count) { 124653541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 124753541Sshin return; 124853541Sshin } 1249151465Ssuz if (ifa->ifa_ifp == NULL) 125053541Sshin panic("nd6_dad_start: ifa->ifa_ifp == NULL"); 1251120941Sume if (!(ifa->ifa_ifp->if_flags & IFF_UP)) { 125253541Sshin return; 1253120941Sume } 1254197138Shrs if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED) 1255197138Shrs return; 125653541Sshin if (nd6_dad_find(ifa) != NULL) { 125753541Sshin /* DAD already in progress */ 125853541Sshin return; 125953541Sshin } 126053541Sshin 126153541Sshin dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT); 126253541Sshin if (dp == NULL) { 126362587Sitojun log(LOG_ERR, "nd6_dad_start: memory allocation failed for " 126453541Sshin "%s(%s)\n", 1265165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 126653541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 126753541Sshin return; 126853541Sshin } 126953541Sshin bzero(dp, sizeof(*dp)); 127078064Sume callout_init(&dp->dad_timer_ch, 0); 1271191816Szec#ifdef VIMAGE 1272191816Szec dp->dad_vnet = curvnet; 1273191816Szec#endif 1274182150Sjulian TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list); 127553541Sshin 127678064Sume nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), 1277165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 127853541Sshin 127953541Sshin /* 128053541Sshin * Send NS packet for DAD, ip6_dad_count times. 128153541Sshin * Note that we must delay the first transmission, if this is the 128253541Sshin * first packet to be sent from the interface after interface 128353541Sshin * (re)initialization. 128453541Sshin */ 128553541Sshin dp->dad_ifa = ifa; 1286194602Srwatson ifa_ref(ifa); /* just for safety */ 1287181803Sbz dp->dad_count = V_ip6_dad_count; 128853541Sshin dp->dad_ns_icount = dp->dad_na_icount = 0; 128962587Sitojun dp->dad_ns_ocount = dp->dad_ns_tcount = 0; 1290151539Ssuz if (delay == 0) { 129162587Sitojun nd6_dad_ns_output(dp, ifa); 1292121161Sume nd6_dad_starttimer(dp, 1293151539Ssuz (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 129453541Sshin } else { 1295151539Ssuz nd6_dad_starttimer(dp, delay); 129653541Sshin } 129753541Sshin} 129853541Sshin 129978064Sume/* 130078064Sume * terminate DAD unconditionally. used for address removals. 130178064Sume */ 130278064Sumevoid 1303171259Sdelphijnd6_dad_stop(struct ifaddr *ifa) 130478064Sume{ 130578064Sume struct dadq *dp; 130678064Sume 1307181803Sbz if (!V_dad_init) 130878064Sume return; 130978064Sume dp = nd6_dad_find(ifa); 131078064Sume if (!dp) { 131178064Sume /* DAD wasn't started yet */ 131278064Sume return; 131378064Sume } 131478064Sume 131578064Sume nd6_dad_stoptimer(dp); 131678064Sume 1317181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 131878064Sume free(dp, M_IP6NDP); 131978064Sume dp = NULL; 1320194602Srwatson ifa_free(ifa); 132178064Sume} 132278064Sume 132353541Sshinstatic void 1324191816Szecnd6_dad_timer(struct dadq *dp) 132553541Sshin{ 1326183550Szec CURVNET_SET(dp->dad_vnet); 1327191816Szec struct ifaddr *ifa = dp->dad_ifa; 132853541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1329165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 133053541Sshin 133153541Sshin /* Sanity check */ 133253541Sshin if (ia == NULL) { 133362587Sitojun log(LOG_ERR, "nd6_dad_timer: called with null parameter\n"); 133453541Sshin goto done; 133553541Sshin } 133653541Sshin if (ia->ia6_flags & IN6_IFF_DUPLICATED) { 133762587Sitojun log(LOG_ERR, "nd6_dad_timer: called with duplicated address " 133853541Sshin "%s(%s)\n", 1339165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 134053541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 134153541Sshin goto done; 134253541Sshin } 134353541Sshin if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) { 134462587Sitojun log(LOG_ERR, "nd6_dad_timer: called with non-tentative address " 134553541Sshin "%s(%s)\n", 1346165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 134753541Sshin ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); 134853541Sshin goto done; 134953541Sshin } 135053541Sshin 135162587Sitojun /* timeouted with IFF_{RUNNING,UP} check */ 1352181803Sbz if (dp->dad_ns_tcount > V_dad_maxtry) { 135378064Sume nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n", 1354120941Sume if_name(ifa->ifa_ifp))); 135562587Sitojun 1356181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 135762587Sitojun free(dp, M_IP6NDP); 135862587Sitojun dp = NULL; 1359194602Srwatson ifa_free(ifa); 136062587Sitojun goto done; 136162587Sitojun } 136262587Sitojun 136353541Sshin /* Need more checks? */ 136453541Sshin if (dp->dad_ns_ocount < dp->dad_count) { 136553541Sshin /* 136653541Sshin * We have more NS to go. Send NS packet for DAD. 136753541Sshin */ 136862587Sitojun nd6_dad_ns_output(dp, ifa); 1369120941Sume nd6_dad_starttimer(dp, 1370151539Ssuz (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000); 137153541Sshin } else { 137253541Sshin /* 137353541Sshin * We have transmitted sufficient number of DAD packets. 137453541Sshin * See what we've got. 137553541Sshin */ 137653541Sshin int duplicate; 137753541Sshin 137853541Sshin duplicate = 0; 137953541Sshin 138053541Sshin if (dp->dad_na_icount) { 138153541Sshin /* 138253541Sshin * the check is in nd6_dad_na_input(), 138353541Sshin * but just in case 138453541Sshin */ 138553541Sshin duplicate++; 138653541Sshin } 138753541Sshin 138853541Sshin if (dp->dad_ns_icount) { 138953541Sshin /* We've seen NS, means DAD has failed. */ 139053541Sshin duplicate++; 139153541Sshin } 139253541Sshin 139353541Sshin if (duplicate) { 139453541Sshin /* (*dp) will be freed in nd6_dad_duplicated() */ 139553541Sshin dp = NULL; 139653541Sshin nd6_dad_duplicated(ifa); 139753541Sshin } else { 139853541Sshin /* 139953541Sshin * We are done with DAD. No NA came, no NS came. 1400148987Sume * No duplicate address found. 140153541Sshin */ 140253541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 140353541Sshin 140478064Sume nd6log((LOG_DEBUG, 140562587Sitojun "%s: DAD complete for %s - no duplicates found\n", 140662587Sitojun if_name(ifa->ifa_ifp), 1407165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 140853541Sshin 1409181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 141053541Sshin free(dp, M_IP6NDP); 141153541Sshin dp = NULL; 1412194602Srwatson ifa_free(ifa); 141353541Sshin } 141453541Sshin } 141553541Sshin 141653541Sshindone: 1417183550Szec CURVNET_RESTORE(); 141853541Sshin} 141953541Sshin 142053541Sshinvoid 1421171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa) 142253541Sshin{ 142353541Sshin struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 1424151477Ssuz struct ifnet *ifp; 142553541Sshin struct dadq *dp; 1426165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 142753541Sshin 142853541Sshin dp = nd6_dad_find(ifa); 142953541Sshin if (dp == NULL) { 143062587Sitojun log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n"); 143153541Sshin return; 143253541Sshin } 143353541Sshin 143478064Sume log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: " 143578064Sume "NS in/out=%d/%d, NA in=%d\n", 1436165118Sbz if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr), 143778064Sume dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount); 143853541Sshin 143953541Sshin ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 144053541Sshin ia->ia6_flags |= IN6_IFF_DUPLICATED; 144153541Sshin 1442148987Sume /* We are done with DAD, with duplicate address found. (failure) */ 144378064Sume nd6_dad_stoptimer(dp); 144453541Sshin 1445151477Ssuz ifp = ifa->ifa_ifp; 144662587Sitojun log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n", 1447165118Sbz if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)); 144862587Sitojun log(LOG_ERR, "%s: manual intervention required\n", 1449151477Ssuz if_name(ifp)); 145053541Sshin 1451151477Ssuz /* 1452151477Ssuz * If the address is a link-local address formed from an interface 1453151477Ssuz * identifier based on the hardware address which is supposed to be 1454151477Ssuz * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP 1455151477Ssuz * operation on the interface SHOULD be disabled. 1456197138Shrs * [RFC 4862, Section 5.4.5] 1457151477Ssuz */ 1458151477Ssuz if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 1459151477Ssuz struct in6_addr in6; 1460151477Ssuz 1461151477Ssuz /* 1462151477Ssuz * To avoid over-reaction, we only apply this logic when we are 1463151477Ssuz * very sure that hardware addresses are supposed to be unique. 1464151477Ssuz */ 1465151477Ssuz switch (ifp->if_type) { 1466151477Ssuz case IFT_ETHER: 1467151477Ssuz case IFT_FDDI: 1468151477Ssuz case IFT_ATM: 1469151477Ssuz case IFT_IEEE1394: 1470151477Ssuz#ifdef IFT_IEEE80211 1471151477Ssuz case IFT_IEEE80211: 1472151477Ssuz#endif 1473219819Sjeff case IFT_INFINIBAND: 1474151477Ssuz in6 = ia->ia_addr.sin6_addr; 1475151477Ssuz if (in6_get_hw_ifid(ifp, &in6) == 0 && 1476151477Ssuz IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { 1477151477Ssuz ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1478151477Ssuz log(LOG_ERR, "%s: possible hardware address " 1479151477Ssuz "duplication detected, disable IPv6\n", 1480151477Ssuz if_name(ifp)); 1481151477Ssuz } 1482151477Ssuz break; 1483151477Ssuz } 1484151477Ssuz } 1485151477Ssuz 1486181803Sbz TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list); 148753541Sshin free(dp, M_IP6NDP); 148853541Sshin dp = NULL; 1489194602Srwatson ifa_free(ifa); 149053541Sshin} 149153541Sshin 149262587Sitojunstatic void 1493171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa) 149462587Sitojun{ 149562587Sitojun struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 149662587Sitojun struct ifnet *ifp = ifa->ifa_ifp; 149762587Sitojun 149862587Sitojun dp->dad_ns_tcount++; 149962587Sitojun if ((ifp->if_flags & IFF_UP) == 0) { 150062587Sitojun return; 150162587Sitojun } 1502148887Srwatson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 150362587Sitojun return; 150462587Sitojun } 150562587Sitojun 150662587Sitojun dp->dad_ns_ocount++; 150762587Sitojun nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1); 150862587Sitojun} 150962587Sitojun 151062587Sitojunstatic void 1511171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa) 151253541Sshin{ 151353541Sshin struct in6_ifaddr *ia; 151453541Sshin struct ifnet *ifp; 151578064Sume const struct in6_addr *taddr6; 151653541Sshin struct dadq *dp; 151753541Sshin int duplicate; 151853541Sshin 1519151465Ssuz if (ifa == NULL) 152053541Sshin panic("ifa == NULL in nd6_dad_ns_input"); 152153541Sshin 152253541Sshin ia = (struct in6_ifaddr *)ifa; 152353541Sshin ifp = ifa->ifa_ifp; 152453541Sshin taddr6 = &ia->ia_addr.sin6_addr; 152553541Sshin duplicate = 0; 152653541Sshin dp = nd6_dad_find(ifa); 152753541Sshin 152853541Sshin /* Quickhack - completely ignore DAD NS packets */ 1529181803Sbz if (V_dad_ignore_ns) { 1530165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 153178064Sume nd6log((LOG_INFO, 153278064Sume "nd6_dad_ns_input: ignoring DAD NS packet for " 1533165118Sbz "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6), 153478064Sume if_name(ifa->ifa_ifp))); 153553541Sshin return; 153653541Sshin } 153753541Sshin 153853541Sshin /* 153953541Sshin * if I'm yet to start DAD, someone else started using this address 154053541Sshin * first. I have a duplicate and you win. 154153541Sshin */ 1542151465Ssuz if (dp == NULL || dp->dad_ns_ocount == 0) 154353541Sshin duplicate++; 154453541Sshin 154553541Sshin /* XXX more checks for loopback situation - see nd6_dad_timer too */ 154653541Sshin 154753541Sshin if (duplicate) { 154853541Sshin dp = NULL; /* will be freed in nd6_dad_duplicated() */ 154953541Sshin nd6_dad_duplicated(ifa); 155053541Sshin } else { 155153541Sshin /* 155253541Sshin * not sure if I got a duplicate. 155353541Sshin * increment ns count and see what happens. 155453541Sshin */ 155553541Sshin if (dp) 155653541Sshin dp->dad_ns_icount++; 155753541Sshin } 155853541Sshin} 155953541Sshin 156062587Sitojunstatic void 1561171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa) 156253541Sshin{ 156353541Sshin struct dadq *dp; 156453541Sshin 1565151465Ssuz if (ifa == NULL) 156653541Sshin panic("ifa == NULL in nd6_dad_na_input"); 156753541Sshin 156853541Sshin dp = nd6_dad_find(ifa); 156953541Sshin if (dp) 157053541Sshin dp->dad_na_icount++; 157153541Sshin 157253541Sshin /* remove the address. */ 157353541Sshin nd6_dad_duplicated(ifa); 157453541Sshin} 1575