X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/39236c6e673c41db228275375ab7fdb0f837b292..f427ee49d309d8fc33ebf3042c3a775f2f530ded:/bsd/netinet6/nd6_nbr.c diff --git a/bsd/netinet6/nd6_nbr.c b/bsd/netinet6/nd6_nbr.c index fa8125a07..829c7a7b1 100644 --- a/bsd/netinet6/nd6_nbr.c +++ b/bsd/netinet6/nd6_nbr.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2000-2013 Apple Inc. All rights reserved. + * Copyright (c) 2000-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -11,10 +11,10 @@ * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. - * + * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -22,7 +22,7 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ @@ -69,6 +69,7 @@ #include #include #include +#include #include #include @@ -79,6 +80,8 @@ #include #include #include +#include +#include #include #include @@ -92,75 +95,72 @@ #if IPSEC #include -#if INET6 #include #endif -extern int ipsec_bypass; -#endif struct dadq; -static struct dadq *nd6_dad_find(struct ifaddr *); +static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *); void nd6_dad_stoptimer(struct ifaddr *); static void nd6_dad_timer(struct ifaddr *); static void nd6_dad_ns_output(struct dadq *, struct ifaddr *); -static void nd6_dad_ns_input(struct mbuf *, struct ifaddr *); +static void nd6_dad_ns_input(struct ifaddr *, char *, int, struct nd_opt_nonce *); static struct mbuf *nd6_dad_na_input(struct mbuf *, struct ifnet *, struct in6_addr *, caddr_t, int); static void dad_addref(struct dadq *, int); static void dad_remref(struct dadq *); static struct dadq *nd6_dad_attach(struct dadq *, struct ifaddr *); static void nd6_dad_detach(struct dadq *, struct ifaddr *); +static void nd6_dad_duplicated(struct ifaddr *); -static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ - -static unsigned int dad_size; /* size of zone element */ -static struct zone *dad_zone; /* zone for dadq */ +static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ -#define DAD_ZONE_MAX 64 /* maximum elements in zone */ -#define DAD_ZONE_NAME "nd6_dad" /* zone name */ +#define DAD_LOCK_ASSERT_HELD(_dp) \ + LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED) -#define DAD_LOCK_ASSERT_HELD(_dp) \ - lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED) +#define DAD_LOCK_ASSERT_NOTHELD(_dp) \ + LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED) -#define DAD_LOCK_ASSERT_NOTHELD(_dp) \ - lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED) - -#define DAD_LOCK(_dp) \ +#define DAD_LOCK(_dp) \ lck_mtx_lock(&(_dp)->dad_lock) -#define DAD_LOCK_SPIN(_dp) \ +#define DAD_LOCK_SPIN(_dp) \ lck_mtx_lock_spin(&(_dp)->dad_lock) -#define DAD_CONVERT_LOCK(_dp) do { \ - DAD_LOCK_ASSERT_HELD(_dp); \ - lck_mtx_convert_spin(&(_dp)->dad_lock); \ +#define DAD_CONVERT_LOCK(_dp) do { \ + DAD_LOCK_ASSERT_HELD(_dp); \ + lck_mtx_convert_spin(&(_dp)->dad_lock); \ } while (0) -#define DAD_UNLOCK(_dp) \ +#define DAD_UNLOCK(_dp) \ lck_mtx_unlock(&(_dp)->dad_lock) -#define DAD_ADDREF(_dp) \ +#define DAD_ADDREF(_dp) \ dad_addref(_dp, 0) -#define DAD_ADDREF_LOCKED(_dp) \ +#define DAD_ADDREF_LOCKED(_dp) \ dad_addref(_dp, 1) -#define DAD_REMREF(_dp) \ +#define DAD_REMREF(_dp) \ dad_remref(_dp) extern lck_mtx_t *dad6_mutex; extern lck_mtx_t *nd6_mutex; -static int nd6_llreach_base = (LL_BASE_REACHABLE / 1000); /* seconds */ +static int nd6_llreach_base = 30; /* seconds */ static struct sockaddr_in6 hostrtmask; SYSCTL_DECL(_net_inet6_icmp6); - SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_llreach_base, - CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_llreach_base, LL_BASE_REACHABLE, + CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_llreach_base, 0, "default ND6 link-layer reachability max lifetime (in seconds)"); +int dad_enhanced = ND6_DAD_ENHANCED_DEFAULT; +SYSCTL_DECL(_net_inet6_ip6); +SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_RW | CTLFLAG_LOCKED, + &dad_enhanced, 0, + "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD."); + /* * Obtain a link-layer source cache entry for the sender. * @@ -175,7 +175,7 @@ nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr, if (nd6_llreach_base != 0 && (ln->ln_expire != 0 || (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) && !(rt->rt_ifp->if_flags & IFF_LOOPBACK) && - ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */ + ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */ alen == ifp->if_addrlen) { struct if_llreach *lr; const char *why = NULL, *type = ""; @@ -200,7 +200,7 @@ nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr, why = " for different target HW address; " "using new llreach record"; } else { - lr->lr_probes = 0; /* reset probe count */ + lr->lr_probes = 0; /* reset probe count */ IFLR_UNLOCK(lr); if (solicited) { why = " for same target HW address; " @@ -213,18 +213,19 @@ nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr, lr = ln->ln_llreach = ifnet_llreach_alloc(ifp, ETHERTYPE_IPV6, addr, alen, nd6_llreach_base); if (lr != NULL) { - lr->lr_probes = 0; /* reset probe count */ - if (why == NULL) + lr->lr_probes = 0; /* reset probe count */ + if (why == NULL) { why = "creating new llreach record"; + } } } if (nd6_debug && lr != NULL && why != NULL) { char tmp[MAX_IPv6_STR_LEN]; - nd6log((LOG_DEBUG, "%s: %s%s for %s\n", if_name(ifp), + nd6log(debug, "%s: %s%s for %s\n", if_name(ifp), type, why, inet_ntop(AF_INET6, - &SIN6(rt_key(rt))->sin6_addr, tmp, sizeof (tmp)))); + &SIN6(rt_key(rt))->sin6_addr, tmp, sizeof(tmp))); } } } @@ -232,8 +233,9 @@ nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr, void nd6_llreach_use(struct llinfo_nd6 *ln) { - if (ln->ln_llreach != NULL) + if (ln->ln_llreach != NULL) { ln->ln_lastused = net_uptime(); + } } /* @@ -250,51 +252,40 @@ nd6_ns_input( { struct ifnet *ifp = m->m_pkthdr.rcvif; struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); - struct nd_neighbor_solicit *nd_ns; + struct nd_neighbor_solicit *nd_ns = NULL; struct in6_addr saddr6 = ip6->ip6_src; struct in6_addr daddr6 = ip6->ip6_dst; - struct in6_addr taddr6; - struct in6_addr myaddr6; + struct in6_addr taddr6 = {}; + struct in6_addr myaddr6 = {}; char *lladdr = NULL; struct ifaddr *ifa = NULL; int lladdrlen = 0; int anycast = 0, proxy = 0, dadprogress = 0; - int tlladdr; - union nd_opts ndopts; - struct sockaddr_dl proxydl; - boolean_t advrouter; - boolean_t is_dad_probe; - - if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) { - nd6log((LOG_INFO, "nd6_ns_input: on ND6ALT interface!\n")); - return; - } + int tlladdr = 0; + union nd_opts ndopts = {}; + struct sockaddr_dl proxydl = {}; + boolean_t advrouter = FALSE; + boolean_t is_dad_probe = FALSE; + int oflgclr = 0; /* Expect 32-bit aligned data pointer on strict-align platforms */ MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); -#ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, icmp6len, return); + IP6_EXTHDR_CHECK(m, off, icmp6len, return ); + ip6 = mtod(m, struct ip6_hdr *); nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off); -#else - IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len); - if (nd_ns == NULL) { - icmp6stat.icp6s_tooshort++; - return; - } -#endif m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE; - ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */ taddr6 = nd_ns->nd_ns_target; - if (in6_setscope(&taddr6, ifp, NULL) != 0) + if (in6_setscope(&taddr6, ifp, NULL) != 0) { goto bad; + } if (ip6->ip6_hlim != IPV6_MAXHLIM) { - nd6log((LOG_ERR, + nd6log(error, "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n", ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), - ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); + ip6_sprintf(&ip6->ip6_dst), if_name(ifp)); goto bad; } @@ -308,8 +299,8 @@ nd6_ns_input( daddr6.s6_addr8[12] == 0xff) { ; /* good */ } else { - nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " - "(wrong ip6 dst)\n")); + nd6log(info, "nd6_ns_input: bad DAD packet " + "(wrong ip6 dst)\n"); goto bad; } } else if (!nd6_onlink_ns_rfc4861) { @@ -326,22 +317,22 @@ nd6_ns_input( src_sa6.sin6_len = sizeof(src_sa6); src_sa6.sin6_addr = saddr6; if (!nd6_is_addr_neighbor(&src_sa6, ifp, 0)) { - nd6log((LOG_INFO, "nd6_ns_input: " - "NS packet from non-neighbor\n")); + nd6log(info, "nd6_ns_input: " + "NS packet from non-neighbor\n"); goto bad; } } if (IN6_IS_ADDR_MULTICAST(&taddr6)) { - nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n")); + nd6log(info, "nd6_ns_input: bad NS target (multicast)\n"); goto bad; } icmp6len -= sizeof(*nd_ns); nd6_option_init(nd_ns + 1, icmp6len, &ndopts); if (nd6_options(&ndopts) < 0) { - nd6log((LOG_INFO, - "nd6_ns_input: invalid ND option, ignored\n")); + nd6log(info, + "nd6_ns_input: invalid ND option, ignored\n"); /* nd6_options have incremented stats */ goto freeit; } @@ -352,8 +343,8 @@ nd6_ns_input( } if (is_dad_probe && lladdr) { - nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " - "(link-layer address option)\n")); + nd6log(info, "nd6_ns_input: bad DAD packet " + "(link-layer address option)\n"); goto bad; } @@ -367,10 +358,11 @@ nd6_ns_input( * In implementation, we add target link-layer address by default. * We do not add one in MUST NOT cases. */ - if (!IN6_IS_ADDR_MULTICAST(&daddr6)) + if (!IN6_IS_ADDR_MULTICAST(&daddr6)) { tlladdr = 0; - else + } else { tlladdr = 1; + } /* * Target address (taddr6) must be either: @@ -402,7 +394,7 @@ nd6_ns_input( * proxy NDP for single entry */ ifa = (struct ifaddr *)in6ifa_ifpforlinklocal( - ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); + ifp, IN6_IFF_NOTREADY | IN6_IFF_ANYCAST); if (ifa) { proxy = 1; proxydl = *SDL(rt->rt_gateway); @@ -427,9 +419,12 @@ nd6_ns_input( * * Forwarding associated with NDPRF_PRPROXY may apply. */ - if (ip6_forwarding && nd6_prproxy) + if (ip6_forwarding && nd6_prproxy) { nd6_prproxy_ns_input(ifp, &saddr6, lladdr, - lladdrlen, &daddr6, &taddr6); + lladdrlen, &daddr6, &taddr6, + (ndopts.nd_opts_nonce == NULL) ? NULL : + ndopts.nd_opts_nonce->nd_opt_nonce); + } goto freeit; } IFA_LOCK(ifa); @@ -444,17 +439,17 @@ nd6_ns_input( IFA_UNLOCK(ifa); if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { - nd6log((LOG_INFO, + nd6log(info, "nd6_ns_input: lladdrlen mismatch for %s " "(if %d, NS packet %d)\n", - ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2)); + ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2); goto bad; } if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { - nd6log((LOG_INFO, - "nd6_ns_input: duplicate IP6 address %s\n", - ip6_sprintf(&saddr6))); + nd6log(info, + "nd6_ns_input: duplicate IP6 address %s\n", + ip6_sprintf(&saddr6)); goto freeit; } @@ -465,7 +460,7 @@ nd6_ns_input( * src addr how to process? * --- --- * multicast of course, invalid (rejected in ip6_input) - * unicast somebody is doing address resolution -> ignore + * unicast somebody is doing address resolution * unspec dup address detection * * The processing is defined in the "draft standard" RFC 4862 (and by @@ -479,16 +474,24 @@ nd6_ns_input( * duplicate address detection. * * If not, the packet is for addess resolution; - * silently ignore it. + * silently ignore it when not optimistic + * + * Per RFC 4429 the reply for an optimistic address must + * have the Override flag cleared */ - if (is_dad_probe) - nd6_dad_ns_input(m, ifa); + if (!is_dad_probe && (dadprogress & IN6_IFF_OPTIMISTIC) != 0) { + oflgclr = 1; + } else { + if (is_dad_probe) { + nd6_dad_ns_input(ifa, lladdr, lladdrlen, ndopts.nd_opts_nonce); + } - goto freeit; + goto freeit; + } } /* Are we an advertising router on this interface? */ - advrouter = (ifp->if_eflags & IFEF_IPV6_ROUTER); + advrouter = (ifp->if_ipv6_router_mode != IPV6_ROUTER_MODE_DISABLED); /* * If the source address is unspecified address, entries must not @@ -501,14 +504,16 @@ nd6_ns_input( */ if (is_dad_probe) { saddr6 = in6addr_linklocal_allnodes; - if (in6_setscope(&saddr6, ifp, NULL) != 0) + if (in6_setscope(&saddr6, ifp, NULL) != 0) { goto bad; - if ((dadprogress & IN6_IFF_OPTIMISTIC) == 0) + } + if ((dadprogress & IN6_IFF_OPTIMISTIC) == 0) { nd6_na_output(ifp, &saddr6, &taddr6, ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | (advrouter ? ND_NA_FLAG_ROUTER : 0), tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL); + } goto freeit; } @@ -516,23 +521,25 @@ nd6_ns_input( ND_NEIGHBOR_SOLICIT, 0); nd6_na_output(ifp, &saddr6, &taddr6, - ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | + ((anycast || proxy || !tlladdr || oflgclr) ? 0 : ND_NA_FLAG_OVERRIDE) | (advrouter ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL); - freeit: +freeit: m_freem(m); - if (ifa != NULL) + if (ifa != NULL) { IFA_REMREF(ifa); + } return; - bad: - nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6))); - nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6))); - nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6))); +bad: + nd6log(error, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)); + nd6log(error, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)); + nd6log(error, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)); icmp6stat.icp6s_badns++; m_freem(m); - if (ifa != NULL) + if (ifa != NULL) { IFA_REMREF(ifa); + } } /* @@ -553,8 +560,8 @@ nd6_ns_output( struct ifnet *ifp, const struct in6_addr *daddr6, const struct in6_addr *taddr6, - struct llinfo_nd6 *ln, /* for source address determination */ - int dad) /* duplicated address detection */ + struct llinfo_nd6 *ln, /* for source address determination */ + uint8_t *nonce) /* duplicated address detection */ { struct mbuf *m; struct ip6_hdr *ip6; @@ -562,22 +569,28 @@ nd6_ns_output( struct in6_ifaddr *ia = NULL; struct in6_addr *src, src_in, src_storage; struct ip6_moptions *im6o = NULL; - struct ifnet *outif = NULL; + struct ifnet *outif = NULL; int icmp6len; int maxlen; int flags; caddr_t mac; struct route_in6 ro; - struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 }, - IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR, 0 }; + struct ip6_out_args ip6oa; u_int32_t rtflags = 0; + boolean_t is_optimistic = FALSE; - if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) || IN6_IS_ADDR_MULTICAST(taddr6)) + if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) || IN6_IS_ADDR_MULTICAST(taddr6)) { return; + } bzero(&ro, sizeof(ro)); - + bzero(&ip6oa, sizeof(ip6oa)); ip6oa.ip6oa_boundif = ifp->if_index; + ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR | + IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED; + ip6oa.ip6oa_sotc = SO_TC_UNSPEC; + ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; + ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; /* estimate the size of message */ @@ -591,7 +604,7 @@ nd6_ns_output( return; } - MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ + MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ if (m && max_linkhdr + maxlen >= MHLEN) { MCLGET(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) { @@ -599,14 +612,15 @@ nd6_ns_output( m = NULL; } } - if (m == NULL) + if (m == NULL) { return; + } m->m_pkthdr.rcvif = NULL; if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) { m->m_flags |= M_MCAST; - im6o = ip6_allocmoptions(M_DONTWAIT); + im6o = ip6_allocmoptions(Z_NOWAIT); if (im6o == NULL) { m_freem(m); return; @@ -619,7 +633,7 @@ nd6_ns_output( icmp6len = sizeof(*nd_ns); m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; - m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ + m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ /* fill neighbor solicitation packet */ ip6 = mtod(m, struct ip6_hdr *); @@ -629,19 +643,20 @@ nd6_ns_output( /* ip6->ip6_plen will be set later */ ip6->ip6_nxt = IPPROTO_ICMPV6; ip6->ip6_hlim = IPV6_MAXHLIM; - if (daddr6) + if (daddr6) { ip6->ip6_dst = *daddr6; - else { + } else { ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; ip6->ip6_dst.s6_addr16[1] = 0; ip6->ip6_dst.s6_addr32[1] = 0; ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; ip6->ip6_dst.s6_addr8[12] = 0xff; - if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) + if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) { goto bad; + } } - if (!dad) { + if (nonce == NULL) { /* * RFC2461 7.2.2: * "If the source address of the packet prompting the @@ -657,7 +672,7 @@ nd6_ns_output( * - saddr6 belongs to the outgoing interface. * Otherwise, we perform the source address selection as usual. */ - struct ip6_hdr *hip6; /* hold ip6 */ + struct ip6_hdr *hip6; /* hold ip6 */ struct in6_addr *hsrc = NULL; /* Caller holds ref on this route */ @@ -670,10 +685,11 @@ nd6_ns_output( if (ln->ln_hold != NULL) { hip6 = mtod(ln->ln_hold, struct ip6_hdr *); /* XXX pullup? */ - if (sizeof (*hip6) < ln->ln_hold->m_len) + if (sizeof(*hip6) < ln->ln_hold->m_len) { hsrc = &hip6->ip6_src; - else + } else { hsrc = NULL; + } } /* Update probe count, if applicable */ if (ln->ln_llreach != NULL) { @@ -700,11 +716,11 @@ nd6_ns_output( NULL, &ro, NULL, &src_storage, ip6oa.ip6oa_boundif, &error); if (src == NULL) { - nd6log((LOG_DEBUG, + nd6log(debug, "nd6_ns_output: source can't be " "determined: dst=%s, error=%d\n", ip6_sprintf(&dst_sa.sin6_addr), - error)); + error); goto bad; } @@ -712,15 +728,29 @@ nd6_ns_output( IFA_REMREF(&ia->ia_ifa); ia = NULL; } - + /* + * RFC 4429 section 3.2: + * When a node has a unicast packet to send + * from an Optimistic Address to a neighbor, + * but does not know the neighbor's link-layer + * address, it MUST NOT perform Address + * Resolution. + */ ia = in6ifa_ifpwithaddr(ifp, src); - if (!ia || (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) { - nd6log((LOG_DEBUG, + if (ia == NULL) { + nd6log(debug, "nd6_ns_output: no preferred source " "available: dst=%s\n", - ip6_sprintf(&dst_sa.sin6_addr))); + ip6_sprintf(&dst_sa.sin6_addr)); goto bad; } + if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) { + is_optimistic = TRUE; + nd6log(debug, + "nd6_ns_output: preferred source " + "available is optimistic: dst=%s\n", + ip6_sprintf(&dst_sa.sin6_addr)); + } } } else { /* @@ -748,13 +778,20 @@ nd6_ns_output( * spec implementation * --- --- * DAD packet MUST NOT do not add the option + * Source is optimistic MUST NOT do not add the option * there's no link layer address: * impossible do not add the option * there's link layer address: * Multicast NS MUST add one add the option * Unicast NS SHOULD add one add the option + * + * XXX We deviate from RFC 4429 and still use optimistic DAD as source + * for address resolution. However to ensure that we do not interfere + * with neighbor cache entries of other neighbors, we MUST ensure + * that SLLAO is not sent. Also note, sending multicast NS without SLLAO + * is also a deviation from RFC 4861. */ - if (!dad && (mac = nd6_ifptomac(ifp))) { + if (nonce == NULL && (mac = nd6_ifptomac(ifp)) && !is_optimistic) { int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); /* 8 byte alignments... */ @@ -765,21 +802,35 @@ nd6_ns_output( icmp6len += optlen; bzero((caddr_t)nd_opt, optlen); nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; - nd_opt->nd_opt_len = optlen >> 3; + nd_opt->nd_opt_len = (uint8_t)(optlen >> 3); bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); } + /* + * Add a Nonce option (RFC 3971) to detect looped back NS messages. + * This behavior is documented as Enhanced Duplicate Address + * Detection in draft-ietf-6man-enhanced-dad-13. + * net.inet6.ip6.dad_enhanced=0 disables this. + */ + if (dad_enhanced != 0 && nonce != NULL && !(ifp->if_flags & IFF_POINTOPOINT)) { + int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN; + struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); + /* 8-byte alignment is required. */ + optlen = (optlen + 7) & ~7; + m->m_pkthdr.len += optlen; + m->m_len += optlen; + icmp6len += optlen; + bzero((caddr_t)nd_opt, optlen); + nd_opt->nd_opt_type = ND_OPT_NONCE; + nd_opt->nd_opt_len = (uint8_t)(optlen >> 3); + bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN); + } ip6->ip6_plen = htons((u_short)icmp6len); nd_ns->nd_ns_cksum = 0; nd_ns->nd_ns_cksum - = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); + = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); -#if IPSEC - /* Don't lookup socket */ - if (ipsec_bypass == 0) - (void) ipsec_setsocket(m, NULL); -#endif - flags = dad ? IPV6_UNSPECSRC : 0; + flags = nonce ? IPV6_UNSPECSRC : 0; flags |= IPV6_OUTARGS; /* @@ -793,8 +844,9 @@ nd6_ns_output( * the packet accordingly so that the driver can find out, * in case it needs to perform driver-specific action(s). */ - if (rtflags & RTF_ROUTER) + if (rtflags & RTF_ROUTER) { m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR; + } if (ifp->if_eflags & IFEF_TXSTART) { /* @@ -804,6 +856,8 @@ nd6_ns_output( (void) m_set_service_class(m, MBUF_SC_CTL); } + ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF; + ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG; ip6_output(m, NULL, NULL, flags, im6o, &outif, &ip6oa); if (outif) { icmp6_ifstat_inc(outif, ifs6_out_msg); @@ -813,13 +867,15 @@ nd6_ns_output( icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++; exit: - if (im6o != NULL) + if (im6o != NULL) { IM6O_REMREF(im6o); + } - ROUTE_RELEASE(&ro); /* we don't cache this route. */ + ROUTE_RELEASE(&ro); /* we don't cache this route. */ - if (ia != NULL) + if (ia != NULL) { IFA_REMREF(&ia->ia_ifa); + } return; bad: @@ -857,33 +913,27 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) struct sockaddr_dl *sdl; union nd_opts ndopts; uint64_t timenow; + bool send_nc_alive_kev = false; if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) { - nd6log((LOG_INFO, "nd6_na_input: on ND6ALT interface!\n")); - return; + nd6log(info, "nd6_na_input: on ND6ALT interface!\n"); + goto freeit; } /* Expect 32-bit aligned data pointer on strict-align platforms */ MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); if (ip6->ip6_hlim != IPV6_MAXHLIM) { - nd6log((LOG_ERR, + nd6log(error, "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), - ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); + ip6_sprintf(&ip6->ip6_dst), if_name(ifp)); goto bad; } -#ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, icmp6len, return); + IP6_EXTHDR_CHECK(m, off, icmp6len, return ); + ip6 = mtod(m, struct ip6_hdr *); nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); -#else - IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len); - if (nd_na == NULL) { - icmp6stat.icp6s_tooshort++; - return; - } -#endif m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE; flags = nd_na->nd_na_flags_reserved; @@ -892,27 +942,28 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); taddr6 = nd_na->nd_na_target; - if (in6_setscope(&taddr6, ifp, NULL)) - goto bad; /* XXX: impossible */ - + if (in6_setscope(&taddr6, ifp, NULL)) { + goto bad; /* XXX: impossible */ + } if (IN6_IS_ADDR_MULTICAST(&taddr6)) { - nd6log((LOG_ERR, + nd6log(error, "nd6_na_input: invalid target address %s\n", - ip6_sprintf(&taddr6))); + ip6_sprintf(&taddr6)); goto bad; } - if (IN6_IS_ADDR_MULTICAST(&daddr6)) + if (IN6_IS_ADDR_MULTICAST(&daddr6)) { if (is_solicited) { - nd6log((LOG_ERR, - "nd6_na_input: a solicited adv is multicasted\n")); + nd6log(error, + "nd6_na_input: a solicited adv is multicasted\n"); goto bad; } + } icmp6len -= sizeof(*nd_na); nd6_option_init(nd_na + 1, icmp6len, &ndopts); if (nd6_options(&ndopts) < 0) { - nd6log((LOG_INFO, - "nd6_na_input: invalid ND option, ignored\n")); + nd6log(info, + "nd6_na_input: invalid ND option, ignored\n"); /* nd6_options have incremented stats */ goto freeit; } @@ -922,22 +973,24 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; if (((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { - nd6log((LOG_INFO, + nd6log(info, "nd6_na_input: lladdrlen mismatch for %s " "(if %d, NA packet %d)\n", - ip6_sprintf(&taddr6), ifp->if_addrlen, - lladdrlen - 2)); + ip6_sprintf(&taddr6), ifp->if_addrlen, + lladdrlen - 2); goto bad; } } m = nd6_dad_na_input(m, ifp, &taddr6, lladdr, lladdrlen); - if (m == NULL) + if (m == NULL) { return; + } /* Forwarding associated with NDPRF_PRPROXY may apply. */ - if (ip6_forwarding && nd6_prproxy) + if (ip6_forwarding && nd6_prproxy) { nd6_prproxy_na_input(ifp, &saddr6, &daddr6, &taddr6, flags); + } /* * If no neighbor cache entry is found, NA SHOULD silently be @@ -946,19 +999,22 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) * another interface (in case we are doing prefix proxying.) */ if ((rt = nd6_lookup(&taddr6, 0, ifp, 0)) == NULL) { - if (!ip6_forwarding || !ip6_doscopedroute || !nd6_prproxy) + if (!ip6_forwarding || !nd6_prproxy) { goto freeit; + } - if ((rt = nd6_lookup(&taddr6, 0, NULL, 0)) == NULL) + if ((rt = nd6_lookup(&taddr6, 0, NULL, 0)) == NULL) { goto freeit; + } RT_LOCK_ASSERT_HELD(rt); if (rt->rt_ifp != ifp) { /* * Purge any link-layer info caching. */ - if (rt->rt_llinfo_purge != NULL) + if (rt->rt_llinfo_purge != NULL) { rt->rt_llinfo_purge(rt); + } /* Adjust route ref count for the interfaces */ if (rt->rt_if_ref_fn != NULL) { @@ -973,8 +1029,9 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) * If rmx_mtu is not locked, update it * to the MTU used by the new interface. */ - if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) + if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) { rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; + } } } @@ -1005,17 +1062,16 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) sdl->sdl_alen = ifp->if_addrlen; bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); if (is_solicited) { - ln->ln_state = ND6_LLINFO_REACHABLE; + send_nc_alive_kev = (rt->rt_flags & RTF_ROUTER) ? true : false; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE); if (ln->ln_expire != 0) { - struct nd_ifinfo *ndi; + struct nd_ifinfo *ndi = NULL; - lck_rw_lock_shared(nd_if_rwlock); ndi = ND_IFINFO(rt->rt_ifp); VERIFY(ndi != NULL && ndi->initialized); lck_mtx_lock(&ndi->lock); ln_setexpire(ln, timenow + ndi->reachable); lck_mtx_unlock(&ndi->lock); - lck_rw_done(nd_if_rwlock); RT_UNLOCK(rt); lck_mtx_lock(rnh_lock); nd6_sched_timeout(NULL, NULL); @@ -1023,41 +1079,65 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) RT_LOCK(rt); } } else { - ln->ln_state = ND6_LLINFO_STALE; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE); ln_setexpire(ln, timenow + nd6_gctimer); } - if ((ln->ln_router = is_router) != 0) { + + + /* + * Enqueue work item to invoke callback for this + * route entry + */ + route_event_enqueue_nwk_wq_entry(rt, NULL, + ROUTE_LLENTRY_RESOLVED, NULL, TRUE); + + if ((ln->ln_router = (short)is_router) != 0) { + struct radix_node_head *rnh = NULL; + struct route_event rt_ev; + route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_RESOLVED); /* * This means a router's state has changed from * non-reachable to probably reachable, and might * affect the status of associated prefixes.. + * We already have a reference on rt. Don't need to + * take one for the unlock/lock. */ RT_UNLOCK(rt); + lck_mtx_lock(rnh_lock); + rnh = rt_tables[AF_INET6]; + + if (rnh != NULL) { + (void) rnh->rnh_walktree(rnh, route_event_walktree, + (void *)&rt_ev); + } + lck_mtx_unlock(rnh_lock); lck_mtx_lock(nd6_mutex); pfxlist_onlink_check(); lck_mtx_unlock(nd6_mutex); RT_LOCK(rt); } } else { - int llchange; + int llchange = 0; /* * Check if the link-layer address has changed or not. */ - if (!lladdr) + if (lladdr == NULL) { llchange = 0; - else { + } else { if (sdl->sdl_alen) { - if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) + if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) { llchange = 1; - else + } else { llchange = 0; - } else + } + } else { llchange = 1; + } } /* - * This is VERY complex. Look at it with care. + * This is VERY complex. Look at it with care. * * override solicit lladdr llchange action * (L: record lladdr) @@ -1081,15 +1161,15 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) * no other updates should be done. */ if (ln->ln_state == ND6_LLINFO_REACHABLE) { - ln->ln_state = ND6_LLINFO_STALE; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE); ln_setexpire(ln, timenow + nd6_gctimer); } RT_REMREF_LOCKED(rt); RT_UNLOCK(rt); goto freeit; - } else if (is_override /* (2a) */ - || (!is_override && (lladdr && !llchange)) /* (2b) */ - || !lladdr) { /* (2c) */ + } else if (is_override /* (2a) */ + || (!is_override && (lladdr && !llchange)) /* (2b) */ + || !lladdr) { /* (2c) */ /* * Update link-local address, if any. */ @@ -1104,18 +1184,16 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) * changed, make it STALE. */ if (is_solicited) { - ln->ln_state = ND6_LLINFO_REACHABLE; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE); if (ln->ln_expire != 0) { - struct nd_ifinfo *ndi; + struct nd_ifinfo *ndi = NULL; - lck_rw_lock_shared(nd_if_rwlock); ndi = ND_IFINFO(ifp); VERIFY(ndi != NULL && ndi->initialized); lck_mtx_lock(&ndi->lock); ln_setexpire(ln, timenow + ndi->reachable); lck_mtx_unlock(&ndi->lock); - lck_rw_done(nd_if_rwlock); RT_UNLOCK(rt); lck_mtx_lock(rnh_lock); nd6_sched_timeout(NULL, NULL); @@ -1124,10 +1202,50 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) } } else { if (lladdr && llchange) { - ln->ln_state = ND6_LLINFO_STALE; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE); ln_setexpire(ln, timenow + nd6_gctimer); } } + + /* + * XXX + * The above is somewhat convoluted, for now just + * issue a callback for LLENTRY changed. + */ + /* Enqueue work item to invoke callback for this route entry */ + if (llchange) { + route_event_enqueue_nwk_wq_entry(rt, NULL, + ROUTE_LLENTRY_CHANGED, NULL, TRUE); + } + + /* + * If the router's link-layer address has changed, + * notify routes using this as gateway so they can + * update any cached information. + */ + if (ln->ln_router && is_router && llchange) { + struct radix_node_head *rnh = NULL; + struct route_event rt_ev; + route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_CHANGED); + /* + * This means a router's state has changed from + * non-reachable to probably reachable, and might + * affect the status of associated prefixes.. + * + * We already have a valid rt reference here. + * We don't need to take another one for unlock/lock. + */ + RT_UNLOCK(rt); + lck_mtx_lock(rnh_lock); + rnh = rt_tables[AF_INET6]; + + if (rnh != NULL) { + (void) rnh->rnh_walktree(rnh, route_event_walktree, + (void *)&rt_ev); + } + lck_mtx_unlock(rnh_lock); + RT_LOCK(rt); + } } if (ln->ln_router && !is_router) { @@ -1145,28 +1263,54 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) RT_UNLOCK(rt); lck_mtx_lock(nd6_mutex); - dr = defrouter_lookup(in6, rt_ifp); + /* + * XXX Handle router lists for route information option + * as well. + */ + dr = defrouter_lookup(NULL, in6, rt_ifp); if (dr) { - defrtrlist_del(dr); + TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry); + defrtrlist_del(dr, NULL); + NDDR_REMREF(dr); /* remove list reference */ NDDR_REMREF(dr); lck_mtx_unlock(nd6_mutex); } else { lck_mtx_unlock(nd6_mutex); - if (ip6_doscopedroute || !ip6_forwarding) { - /* - * Even if the neighbor is not in the - * default router list, the neighbor - * may be used as a next hop for some - * destinations (e.g. redirect case). - * So we must call rt6_flush explicitly. - */ - rt6_flush(&ip6->ip6_src, rt_ifp); - } + /* + * Even if the neighbor is not in the + * default router list, the neighbor + * may be used as a next hop for some + * destinations (e.g. redirect case). + * So we must call rt6_flush explicitly. + */ + rt6_flush(&ip6->ip6_src, rt_ifp); } RT_LOCK(rt); } - ln->ln_router = is_router; + ln->ln_router = (short)is_router; + } + + if (send_nc_alive_kev && (ifp->if_addrlen == IF_LLREACH_MAXLEN)) { + struct kev_msg ev_msg; + struct kev_nd6_ndalive nd6_ndalive; + bzero(&ev_msg, sizeof(ev_msg)); + bzero(&nd6_ndalive, sizeof(nd6_ndalive)); + ev_msg.vendor_code = KEV_VENDOR_APPLE; + ev_msg.kev_class = KEV_NETWORK_CLASS; + ev_msg.kev_subclass = KEV_ND6_SUBCLASS; + ev_msg.event_code = KEV_ND6_NDALIVE; + + nd6_ndalive.link_data.if_family = ifp->if_family; + nd6_ndalive.link_data.if_unit = ifp->if_unit; + strlcpy(nd6_ndalive.link_data.if_name, + ifp->if_name, + sizeof(nd6_ndalive.link_data.if_name)); + ev_msg.dv[0].data_ptr = &nd6_ndalive; + ev_msg.dv[0].data_length = + sizeof(nd6_ndalive); + dlil_post_complete_msg(NULL, &ev_msg); } + RT_LOCK_ASSERT_HELD(rt); rt->rt_flags &= ~RTF_REJECT; @@ -1185,8 +1329,9 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) * prevent a ln_hold lookup in nd6_output() * (wouldn't happen, though...) */ - for (m_hold = ln->ln_hold; - m_hold; m_hold = m_hold_next) { + m_hold = ln->ln_hold; + ln->ln_hold = NULL; + for (; m_hold; m_hold = m_hold_next) { m_hold_next = m_hold->m_nextpkt; m_hold->m_nextpkt = NULL; /* @@ -1197,18 +1342,18 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len) nd6_output(ifp, ifp, m_hold, &sin6, rt, NULL); RT_LOCK_SPIN(rt); } - ln->ln_hold = NULL; - } RT_REMREF_LOCKED(rt); RT_UNLOCK(rt); + m_freem(m); + return; bad: icmp6stat.icp6s_badna++; /* fall through */ - freeit: m_freem(m); + return; } /* @@ -1229,8 +1374,8 @@ nd6_na_output( const struct in6_addr *daddr6_0, const struct in6_addr *taddr6, uint32_t flags, - int tlladdr, /* 1 if include target link-layer address */ - struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */ + int tlladdr, /* 1 if include target link-layer address */ + struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */ { struct mbuf *m; struct ip6_hdr *ip6; @@ -1242,15 +1387,20 @@ nd6_na_output( struct in6_ifaddr *ia; struct sockaddr_in6 dst_sa; int icmp6len, maxlen, error; - struct ifnet *outif = NULL; - struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 }, - IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR, 0 }; + struct ifnet *outif = NULL; + struct ip6_out_args ip6oa; bzero(&ro, sizeof(ro)); - daddr6 = *daddr6_0; /* make a local copy for modification */ + daddr6 = *daddr6_0; /* make a local copy for modification */ + bzero(&ip6oa, sizeof(ip6oa)); ip6oa.ip6oa_boundif = ifp->if_index; + ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR | + IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED; + ip6oa.ip6oa_sotc = SO_TC_UNSPEC; + ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; + ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; /* estimate the size of message */ @@ -1264,7 +1414,7 @@ nd6_na_output( return; } - MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ + MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ if (m && max_linkhdr + maxlen >= MHLEN) { MCLGET(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) { @@ -1272,14 +1422,15 @@ nd6_na_output( m = NULL; } } - if (m == NULL) + if (m == NULL) { return; + } m->m_pkthdr.rcvif = NULL; if (IN6_IS_ADDR_MULTICAST(&daddr6)) { m->m_flags |= M_MCAST; - im6o = ip6_allocmoptions(M_DONTWAIT); + im6o = ip6_allocmoptions(Z_NOWAIT); if (im6o == NULL) { m_freem(m); return; @@ -1292,7 +1443,7 @@ nd6_na_output( icmp6len = sizeof(*nd_na); m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len; - m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ + m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ /* fill neighbor advertisement packet */ ip6 = mtod(m, struct ip6_hdr *); @@ -1308,12 +1459,14 @@ nd6_na_output( daddr6.s6_addr32[1] = 0; daddr6.s6_addr32[2] = 0; daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE; - if (in6_setscope(&daddr6, ifp, NULL)) + if (in6_setscope(&daddr6, ifp, NULL)) { goto bad; + } flags &= ~ND_NA_FLAG_SOLICITED; - } else + } else { ip6->ip6_dst = daddr6; + } bzero(&dst_sa, sizeof(struct sockaddr_in6)); dst_sa.sin6_family = AF_INET6; @@ -1327,9 +1480,9 @@ nd6_na_output( src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &src_storage, ip6oa.ip6oa_boundif, &error); if (src == NULL) { - nd6log((LOG_DEBUG, "nd6_na_output: source can't be " + nd6log(debug, "nd6_na_output: source can't be " "determined: dst=%s, error=%d\n", - ip6_sprintf(&dst_sa.sin6_addr), error)); + ip6_sprintf(&dst_sa.sin6_addr), error); goto bad; } ip6->ip6_src = *src; @@ -1340,8 +1493,9 @@ nd6_na_output( */ ia = in6ifa_ifpwithaddr(ifp, src); if (ia != NULL) { - if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) + if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) { flags &= ~ND_NA_FLAG_OVERRIDE; + } IFA_REMREF(&ia->ia_ifa); } @@ -1363,13 +1517,14 @@ nd6_na_output( * lladdr in sdl0. If we are not proxying (sending NA for * my address) use lladdr configured for the interface. */ - if (sdl0 == NULL) + if (sdl0 == NULL) { mac = nd6_ifptomac(ifp); - else if (sdl0->sa_family == AF_LINK) { + } else if (sdl0->sa_family == AF_LINK) { struct sockaddr_dl *sdl; sdl = (struct sockaddr_dl *)(void *)sdl0; - if (sdl->sdl_alen == ifp->if_addrlen) + if (sdl->sdl_alen == ifp->if_addrlen) { mac = LLADDR(sdl); + } } } if (tlladdr && mac) { @@ -1384,22 +1539,18 @@ nd6_na_output( icmp6len += optlen; bzero((caddr_t)nd_opt, optlen); nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; - nd_opt->nd_opt_len = optlen >> 3; + nd_opt->nd_opt_len = (uint8_t)(optlen >> 3); bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); - } else + } else { flags &= ~ND_NA_FLAG_OVERRIDE; + } ip6->ip6_plen = htons((u_short)icmp6len); nd_na->nd_na_flags_reserved = flags; nd_na->nd_na_cksum = 0; nd_na->nd_na_cksum = - in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); + in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); -#if IPSEC - /* Don't lookup socket */ - if (ipsec_bypass == 0) - (void) ipsec_setsocket(m, NULL); -#endif m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE; if (ifp->if_eflags & IFEF_TXSTART) { @@ -1409,6 +1560,8 @@ nd6_na_output( (void) m_set_service_class(m, MBUF_SC_CTL); } + ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF; + ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG; ip6_output(m, NULL, NULL, IPV6_OUTARGS, im6o, &outif, &ip6oa); if (outif) { icmp6_ifstat_inc(outif, ifs6_out_msg); @@ -1418,8 +1571,9 @@ nd6_na_output( icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++; exit: - if (im6o != NULL) + if (im6o != NULL) { IM6O_REMREF(im6o); + } ROUTE_RELEASE(&ro); return; @@ -1450,7 +1604,8 @@ nd6_ifptomac( #endif case IFT_BRIDGE: case IFT_ISO88025: - return ((caddr_t)IF_LLADDR(ifp)); + case IFT_6LOWPAN: + return (caddr_t)IF_LLADDR(ifp); default: return NULL; } @@ -1459,68 +1614,86 @@ nd6_ifptomac( TAILQ_HEAD(dadq_head, dadq); struct dadq { decl_lck_mtx_data(, dad_lock); - u_int32_t dad_refcount; /* reference count */ + u_int32_t dad_refcount; /* reference count */ int dad_attached; TAILQ_ENTRY(dadq) dad_list; struct ifaddr *dad_ifa; - int dad_count; /* max NS to send */ - int dad_ns_tcount; /* # of trials to send NS */ - int dad_ns_ocount; /* NS sent so far */ + int dad_count; /* max NS to send */ + int dad_ns_tcount; /* # of trials to send NS */ + int dad_ns_ocount; /* NS sent so far */ int dad_ns_icount; int dad_na_icount; - int dad_nd_ixcount; /* Count of IFDISABLED eligible ND rx'd */ + int dad_ns_lcount; /* looped back NS */ + int dad_loopbackprobe; /* probing state for loopback detection */ + uint8_t dad_lladdr[ETHER_ADDR_LEN]; + uint8_t dad_lladdrlen; +#define ND_OPT_NONCE_LEN32 \ + ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t)) + uint32_t dad_nonce[ND_OPT_NONCE_LEN32]; }; +static ZONE_DECLARE(dad_zone, "nd6_dad", sizeof(struct dadq), ZC_ZFREE_CLEARMEM); static struct dadq_head dadq; void nd6_nbr_init(void) { int i; - - TAILQ_INIT(&dadq); - dad_size = sizeof (struct dadq); - dad_zone = zinit(dad_size, DAD_ZONE_MAX * dad_size, 0, DAD_ZONE_NAME); - if (dad_zone == NULL) { - panic("%s: failed allocating %s", __func__, DAD_ZONE_NAME); - /* NOTREACHED */ - } - zone_change(dad_zone, Z_EXPAND, TRUE); - zone_change(dad_zone, Z_CALLERACCT, FALSE); + TAILQ_INIT(&dadq); bzero(&hostrtmask, sizeof hostrtmask); hostrtmask.sin6_family = AF_INET6; hostrtmask.sin6_len = sizeof hostrtmask; - for (i = 0; i < sizeof hostrtmask.sin6_addr; ++i) + for (i = 0; i < sizeof hostrtmask.sin6_addr; ++i) { hostrtmask.sin6_addr.s6_addr[i] = 0xff; + } } static struct dadq * -nd6_dad_find(struct ifaddr *ifa) +nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce) { struct dadq *dp; lck_mtx_lock(dad6_mutex); for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) { DAD_LOCK_SPIN(dp); - if (dp->dad_ifa == ifa) { - DAD_ADDREF_LOCKED(dp); + if (dp->dad_ifa != ifa) { + DAD_UNLOCK(dp); + continue; + } + + /* + * Skip if the nonce matches the received one. + * +2 in the length is required because of type and + * length fields are included in a header. + */ + if (nonce != NULL && + nonce->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 && + memcmp(&nonce->nd_opt_nonce[0], &dp->dad_nonce[0], + ND_OPT_NONCE_LEN) == 0) { + nd6log(error, "%s: a looped back NS message is " + "detected during DAD for %s. Ignoring.\n", + if_name(ifa->ifa_ifp), + ip6_sprintf(IFA_IN6(ifa))); + dp->dad_ns_lcount++; + ++ip6stat.ip6s_dad_loopcount; DAD_UNLOCK(dp); - lck_mtx_unlock(dad6_mutex); - return (dp); + continue; } + + DAD_ADDREF_LOCKED(dp); DAD_UNLOCK(dp); + break; } lck_mtx_unlock(dad6_mutex); - return (NULL); + return dp; } void nd6_dad_stoptimer( struct ifaddr *ifa) { - untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa); } @@ -1530,11 +1703,17 @@ nd6_dad_stoptimer( void nd6_dad_start( struct ifaddr *ifa, - int *tick_delay) /* minimum delay ticks for IFF_UP event */ + int *tick_delay) /* minimum delay ticks for IFF_UP event */ { struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; struct dadq *dp; + nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n", + __func__, + ip6_sprintf(&ia->ia_addr.sin6_addr), + if_name(ia->ia_ifp), + ia->ia6_flags); + /* * If we don't need DAD, don't do it. * There are several cases: @@ -1543,11 +1722,11 @@ nd6_dad_start( */ IFA_LOCK(&ia->ia_ifa); if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) { - log(LOG_DEBUG, - "nd6_dad_start: not a tentative or optimistic address " - "%s(%s)\n", - ip6_sprintf(&ia->ia_addr.sin6_addr), - ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); + nd6log0(debug, + "nd6_dad_start: not a tentative or optimistic address " + "%s(%s)\n", + ip6_sprintf(&ia->ia_addr.sin6_addr), + ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); IFA_UNLOCK(&ia->ia_ifa); return; } @@ -1557,36 +1736,30 @@ nd6_dad_start( return; } IFA_UNLOCK(&ia->ia_ifa); - if (ifa->ifa_ifp == NULL) + if (ifa->ifa_ifp == NULL) { panic("nd6_dad_start: ifa->ifa_ifp == NULL"); + } if (!(ifa->ifa_ifp->if_flags & IFF_UP) || (ifa->ifa_ifp->if_eflags & IFEF_IPV6_ND6ALT)) { return; } - if ((dp = nd6_dad_find(ifa)) != NULL) { + if ((dp = nd6_dad_find(ifa, NULL)) != NULL) { DAD_REMREF(dp); /* DAD already in progress */ return; } - dp = zalloc(dad_zone); - if (dp == NULL) { - log(LOG_ERR, "nd6_dad_start: memory allocation failed for " - "%s(%s)\n", - ip6_sprintf(&ia->ia_addr.sin6_addr), - ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); - return; - } - bzero(dp, dad_size); + dp = zalloc_flags(dad_zone, Z_WAITOK | Z_ZERO); lck_mtx_init(&dp->dad_lock, ifa_mtx_grp, ifa_mtx_attr); /* Callee adds one reference for us */ dp = nd6_dad_attach(dp, ifa); - nd6log((LOG_DEBUG, "%s: starting %sDAD for %s\n", + nd6log0(debug, "%s: starting %sDAD %sfor %s\n", if_name(ifa->ifa_ifp), - (ia->ia_flags & IN6_IFF_OPTIMISTIC) ? "optimistic " : "", - ip6_sprintf(&ia->ia_addr.sin6_addr))); + (ia->ia6_flags & IN6_IFF_OPTIMISTIC) ? "optimistic " : "", + (tick_delay == NULL) ? "immediately " : "", + ip6_sprintf(&ia->ia_addr.sin6_addr)); /* * Send NS packet for DAD, ip6_dad_count times. @@ -1596,30 +1769,29 @@ nd6_dad_start( */ if (tick_delay == NULL) { u_int32_t retrans; - struct nd_ifinfo *ndi; + struct nd_ifinfo *ndi = NULL; nd6_dad_ns_output(dp, ifa); - lck_rw_lock_shared(nd_if_rwlock); ndi = ND_IFINFO(ifa->ifa_ifp); VERIFY(ndi != NULL && ndi->initialized); lck_mtx_lock(&ndi->lock); retrans = ndi->retrans * hz / 1000; lck_mtx_unlock(&ndi->lock); - lck_rw_done(nd_if_rwlock); timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans); } else { int ntick; - if (*tick_delay == 0) + if (*tick_delay == 0) { ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz); - else + } else { ntick = *tick_delay + random() % (hz / 2); + } *tick_delay = ntick; timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, - ntick); + ntick); } - DAD_REMREF(dp); /* drop our reference */ + DAD_REMREF(dp); /* drop our reference */ } static struct dadq * @@ -1628,20 +1800,21 @@ nd6_dad_attach(struct dadq *dp, struct ifaddr *ifa) lck_mtx_lock(dad6_mutex); DAD_LOCK(dp); dp->dad_ifa = ifa; - IFA_ADDREF(ifa); /* for dad_ifa */ + IFA_ADDREF(ifa); /* for dad_ifa */ dp->dad_count = ip6_dad_count; dp->dad_ns_icount = dp->dad_na_icount = 0; dp->dad_ns_ocount = dp->dad_ns_tcount = 0; - dp->dad_nd_ixcount = 0; + dp->dad_ns_lcount = dp->dad_loopbackprobe = 0; VERIFY(!dp->dad_attached); dp->dad_attached = 1; - DAD_ADDREF_LOCKED(dp); /* for caller */ - DAD_ADDREF_LOCKED(dp); /* for dadq_head list */ + dp->dad_lladdrlen = 0; + DAD_ADDREF_LOCKED(dp); /* for caller */ + DAD_ADDREF_LOCKED(dp); /* for dadq_head list */ TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); DAD_UNLOCK(dp); lck_mtx_unlock(dad6_mutex); - return (dp); + return dp; } static void @@ -1661,7 +1834,7 @@ nd6_dad_detach(struct dadq *dp, struct ifaddr *ifa) DAD_UNLOCK(dp); lck_mtx_unlock(dad6_mutex); if (detached) { - DAD_REMREF(dp); /* drop dadq_head reference */ + DAD_REMREF(dp); /* drop dadq_head reference */ } } @@ -1673,7 +1846,7 @@ nd6_dad_stop(struct ifaddr *ifa) { struct dadq *dp; - dp = nd6_dad_find(ifa); + dp = nd6_dad_find(ifa, NULL); if (!dp) { /* DAD wasn't started yet */ return; @@ -1682,10 +1855,9 @@ nd6_dad_stop(struct ifaddr *ifa) untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa); nd6_dad_detach(dp, ifa); - DAD_REMREF(dp); /* drop our reference */ + DAD_REMREF(dp); /* drop our reference */ } - static void nd6_unsol_na_output(struct ifaddr *ifa) { @@ -1695,20 +1867,23 @@ nd6_unsol_na_output(struct ifaddr *ifa) if ((ifp->if_flags & IFF_UP) == 0 || (ifp->if_flags & IFF_RUNNING) == 0 || - (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) + (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) { return; + } IFA_LOCK_SPIN(&ia->ia_ifa); taddr6 = ia->ia_addr.sin6_addr; IFA_UNLOCK(&ia->ia_ifa); - if (in6_setscope(&taddr6, ifp, NULL) != 0) + if (in6_setscope(&taddr6, ifp, NULL) != 0) { return; + } saddr6 = in6addr_linklocal_allnodes; - if (in6_setscope(&saddr6, ifp, NULL) != 0) + if (in6_setscope(&saddr6, ifp, NULL) != 0) { return; + } - nd6log((LOG_INFO, "%s: sending unsolicited NA\n", - if_name(ifa->ifa_ifp))); + nd6log(info, "%s: sending unsolicited NA\n", + if_name(ifa->ifa_ifp)); nd6_na_output(ifp, &saddr6, &taddr6, ND_NA_FLAG_OVERRIDE, 1, NULL); } @@ -1718,31 +1893,40 @@ nd6_dad_timer(struct ifaddr *ifa) { struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; struct dadq *dp = NULL; + struct nd_ifinfo *ndi = NULL; + u_int32_t retrans; /* Sanity check */ if (ia == NULL) { - log(LOG_ERR, "nd6_dad_timer: called with null parameter\n"); + nd6log0(error, "nd6_dad_timer: called with null parameter\n"); goto done; } - dp = nd6_dad_find(ifa); + + nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n", + __func__, + ip6_sprintf(&ia->ia_addr.sin6_addr), + if_name(ia->ia_ifp), + ia->ia6_flags); + + dp = nd6_dad_find(ifa, NULL); if (dp == NULL) { - log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n"); + nd6log0(error, "nd6_dad_timer: DAD structure not found\n"); goto done; } IFA_LOCK(&ia->ia_ifa); if (ia->ia6_flags & IN6_IFF_DUPLICATED) { - log(LOG_ERR, "nd6_dad_timer: called with duplicated address " - "%s(%s)\n", - ip6_sprintf(&ia->ia_addr.sin6_addr), - ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); + nd6log0(error, "nd6_dad_timer: called with duplicated address " + "%s(%s)\n", + ip6_sprintf(&ia->ia_addr.sin6_addr), + ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); IFA_UNLOCK(&ia->ia_ifa); goto done; } if ((ia->ia6_flags & IN6_IFF_DADPROGRESS) == 0) { - log(LOG_ERR, "nd6_dad_timer: not a tentative or optimistic " - "address %s(%s)\n", - ip6_sprintf(&ia->ia_addr.sin6_addr), - ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); + nd6log0(error, "nd6_dad_timer: not a tentative or optimistic " + "address %s(%s)\n", + ip6_sprintf(&ia->ia_addr.sin6_addr), + ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); IFA_UNLOCK(&ia->ia_ifa); goto done; } @@ -1752,8 +1936,8 @@ nd6_dad_timer(struct ifaddr *ifa) DAD_LOCK(dp); if (dp->dad_ns_tcount > dad_maxtry) { DAD_UNLOCK(dp); - nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n", - if_name(ifa->ifa_ifp))); + nd6log0(info, "%s: could not run DAD, driver problem?\n", + if_name(ifa->ifa_ifp)); nd6_dad_detach(dp, ifa); goto done; @@ -1761,55 +1945,63 @@ nd6_dad_timer(struct ifaddr *ifa) /* Need more checks? */ if (dp->dad_ns_ocount < dp->dad_count) { - u_int32_t retrans; - struct nd_ifinfo *ndi; - DAD_UNLOCK(dp); /* * We have more NS to go. Send NS packet for DAD. */ nd6_dad_ns_output(dp, ifa); - lck_rw_lock_shared(nd_if_rwlock); ndi = ND_IFINFO(ifa->ifa_ifp); VERIFY(ndi != NULL && ndi->initialized); lck_mtx_lock(&ndi->lock); retrans = ndi->retrans * hz / 1000; lck_mtx_unlock(&ndi->lock); - lck_rw_done(nd_if_rwlock); timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans); } else { /* * We have transmitted sufficient number of DAD packets. * See what we've got. */ - int duplicate; - boolean_t candisable; - - duplicate = 0; - candisable = dp->dad_nd_ixcount > 0; + if (dp->dad_na_icount > 0 || dp->dad_ns_icount) { + /* We've seen NS or NA, means DAD has failed. */ + DAD_UNLOCK(dp); + nd6log0(info, + "%s: duplicate IPv6 address %s if:%s [timer]\n", + __func__, ip6_sprintf(&ia->ia_addr.sin6_addr), + if_name(ia->ia_ifp)); + nd6_dad_duplicated(ifa); + /* (*dp) will be freed in nd6_dad_duplicated() */ + } else if (dad_enhanced != 0 && + dp->dad_ns_lcount > 0 && + dp->dad_ns_lcount > dp->dad_loopbackprobe) { + dp->dad_loopbackprobe = dp->dad_ns_lcount; + dp->dad_count = + dp->dad_ns_ocount + dad_maxtry - 1; + DAD_UNLOCK(dp); + ndi = ND_IFINFO(ifa->ifa_ifp); + VERIFY(ndi != NULL && ndi->initialized); + lck_mtx_lock(&ndi->lock); + retrans = ndi->retrans * hz / 1000; + lck_mtx_unlock(&ndi->lock); - if (dp->dad_na_icount) { /* - * the check is in nd6_dad_na_input(), - * but just in case + * Sec. 4.1 in RFC 7527 requires transmission of + * additional probes until the loopback condition + * becomes clear when a looped back probe is detected. */ - duplicate++; - } - - if (dp->dad_ns_icount) { - /* We've seen NS, means DAD has failed. */ - duplicate++; - } - DAD_UNLOCK(dp); - - if (duplicate) { - nd6log((LOG_INFO, - "%s: duplicate IPv6 address %s [timer]\n", + nd6log0(info, + "%s: a looped back NS message is detected during DAD for %s. Another DAD probe is being sent on interface %s.\n", __func__, ip6_sprintf(&ia->ia_addr.sin6_addr), - if_name(ia->ia_ifp))); - nd6_dad_duplicated(ifa); - /* (*dp) will be freed in nd6_dad_duplicated() */ + if_name(ia->ia_ifp)); + /* + * Send an NS immediately and increase dad_count by + * nd6_mmaxtries - 1. + */ + nd6_dad_ns_output(dp, ifa); + timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans); + goto done; } else { + boolean_t txunsolna; + DAD_UNLOCK(dp); /* * We are done with DAD. No NA came, no NS came. * No duplicate address found. @@ -1818,48 +2010,135 @@ nd6_dad_timer(struct ifaddr *ifa) ia->ia6_flags &= ~IN6_IFF_DADPROGRESS; IFA_UNLOCK(&ia->ia_ifa); - nd6log((LOG_DEBUG, - "%s: DAD complete for %s - no duplicates found\n", + ndi = ND_IFINFO(ifa->ifa_ifp); + VERIFY(ndi != NULL && ndi->initialized); + lck_mtx_lock(&ndi->lock); + txunsolna = (ndi->flags & ND6_IFF_REPLICATED) != 0; + lck_mtx_unlock(&ndi->lock); + + if (txunsolna) { + nd6_unsol_na_output(ifa); + } + + nd6log0(debug, + "%s: DAD complete for %s - no duplicates found %s\n", if_name(ifa->ifa_ifp), - ip6_sprintf(&ia->ia_addr.sin6_addr))); - /* - * Send an Unsolicited Neighbor Advertisement so that - * other machines on the network are aware of us - * (important when we are waking from sleep). - */ - nd6_unsol_na_output(ifa); - in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia); + ip6_sprintf(&ia->ia_addr.sin6_addr), + txunsolna ? ", tx unsolicited NA with O=1" : "."); + + if (dp->dad_ns_lcount > 0) { + nd6log0(debug, + "%s: DAD completed while " + "a looped back NS message is detected " + "during DAD for %s om interface %s\n", + __func__, + ip6_sprintf(&ia->ia_addr.sin6_addr), + if_name(ia->ia_ifp)); + } + + in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia, + dp->dad_lladdr); nd6_dad_detach(dp, ifa); } } done: - if (dp != NULL) - DAD_REMREF(dp); /* drop our reference */ + if (dp != NULL) { + DAD_REMREF(dp); /* drop our reference */ + } } -void +static void nd6_dad_duplicated(struct ifaddr *ifa) { struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; struct dadq *dp; struct ifnet *ifp = ifa->ifa_ifp; - boolean_t disable; + boolean_t candisable; - dp = nd6_dad_find(ifa); + dp = nd6_dad_find(ifa, NULL); if (dp == NULL) { log(LOG_ERR, "%s: DAD structure not found.\n", __func__); return; } IFA_LOCK(&ia->ia_ifa); DAD_LOCK(dp); - nd6log((LOG_ERR, "%s: NS in/out=%d/%d, NA in=%d, ND x=%d\n", - __func__, dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount, - dp->dad_nd_ixcount)); - disable = dp->dad_nd_ixcount > 0; + nd6log(error, "%s: NS in/out/loopback=%d/%d/%d, NA in=%d\n", + __func__, dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount, + dp->dad_na_icount); + candisable = FALSE; + + if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) && + !(ia->ia6_flags & IN6_IFF_SECURED)) { + struct in6_addr in6; + struct ifaddr *llifa = NULL; + struct sockaddr_dl *sdl = NULL; + uint8_t *lladdr = dp->dad_lladdr; + uint8_t lladdrlen = dp->dad_lladdrlen; + + /* + * To avoid over-reaction, we only apply this logic when we are + * very sure that hardware addresses are supposed to be unique. + */ + switch (ifp->if_type) { + case IFT_BRIDGE: + case IFT_ETHER: + case IFT_FDDI: + case IFT_ATM: + case IFT_IEEE1394: +#ifdef IFT_IEEE80211 + case IFT_IEEE80211: +#endif + /* + * Check if our hardware address matches the + * link layer information received in the + * NS/NA + */ + llifa = ifp->if_lladdr; + IFA_LOCK(llifa); + sdl = (struct sockaddr_dl *)(void *) + llifa->ifa_addr; + if (lladdrlen == sdl->sdl_alen && + bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) { + candisable = TRUE; + } + IFA_UNLOCK(llifa); + + in6 = ia->ia_addr.sin6_addr; + if (in6_iid_from_hw(ifp, &in6) != 0) { + break; + } + + /* Refine decision about whether IPv6 can be disabled */ + if (candisable && + !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { + /* + * Apply this logic only to the embedded MAC + * address form of link-local IPv6 address. + */ + candisable = FALSE; + } else if (lladdr == NULL && + IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { + /* + * We received a NA with no target link-layer + * address option. This means that someone else + * has our address. Mark it as a hardware + * duplicate so we disable IPv6 later on. + */ + candisable = TRUE; + } + break; + default: + break; + } + } DAD_UNLOCK(dp); + ia->ia6_flags &= ~IN6_IFF_DADPROGRESS; ia->ia6_flags |= IN6_IFF_DUPLICATED; + in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DUPLICATED, + ia->ia_ifa.ifa_ifp, &ia->ia_addr.sin6_addr, + 0); IFA_UNLOCK(&ia->ia_ifa); /* increment DAD collision counter */ @@ -1873,26 +2152,28 @@ nd6_dad_duplicated(struct ifaddr *ifa) if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr)); IFA_UNLOCK(&ia->ia_ifa); - if (disable) { + if (candisable) { + struct nd_ifinfo *ndi = ND_IFINFO(ifp); log(LOG_ERR, "%s: possible hardware address duplication " "detected, disabling IPv6 for interface.\n", if_name(ifp)); - lck_rw_lock_shared(nd_if_rwlock); - nd_ifinfo[ifp->if_index].flags |= ND6_IFF_IFDISABLED; - lck_rw_done(nd_if_rwlock); + VERIFY((NULL != ndi) && (TRUE == ndi->initialized)); + ndi->flags |= ND6_IFF_IFDISABLED; /* Make sure to set IFEF_IPV6_DISABLED too */ nd6_if_disable(ifp, TRUE); } - log(LOG_ERR, "%s: manual intervention required!\n", if_name(ifp)); + log(LOG_ERR, + "%s: manual intervention may be required.\n", + if_name(ifp)); /* Send an event to the configuration agent so that the * duplicate address will be notified to the user and will * be removed. */ - in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia); + in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia, dp->dad_lladdr); nd6_dad_detach(dp, ifa); - DAD_REMREF(dp); /* drop our reference */ + DAD_REMREF(dp); /* drop our reference */ } static void @@ -1900,6 +2181,7 @@ nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa) { struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; struct ifnet *ifp = ifa->ifa_ifp; + int i = 0; struct in6_addr taddr6; DAD_LOCK(dp); @@ -1918,122 +2200,101 @@ nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa) IFA_LOCK_SPIN(&ia->ia_ifa); taddr6 = ia->ia_addr.sin6_addr; IFA_UNLOCK(&ia->ia_ifa); - nd6_ns_output(ifp, NULL, &taddr6, NULL, 1); + if (dad_enhanced != 0 && !(ifp->if_flags & IFF_POINTOPOINT)) { + for (i = 0; i < ND_OPT_NONCE_LEN32; i++) { + dp->dad_nonce[i] = RandomULong(); + } + /* + * XXXHRS: Note that in the case that + * DupAddrDetectTransmits > 1, multiple NS messages with + * different nonces can be looped back in an unexpected + * order. The current implementation recognizes only + * the latest nonce on the sender side. Practically it + * should work well in almost all cases. + */ + } + nd6_ns_output(ifp, NULL, &taddr6, NULL, + (uint8_t *)&dp->dad_nonce[0]); } +/* + * @brief Called to process DAD NS + * + * @param ifa is the pointer to the interface's address + * @param lladdr is source link layer information + * @param lladdrlen is source's linklayer length + * + * @return void + */ static void -nd6_dad_ns_input(struct mbuf *m, struct ifaddr *ifa) +nd6_dad_ns_input(struct ifaddr *ifa, char *lladdr, + int lladdrlen, struct nd_opt_nonce *ndopt_nonce) { struct dadq *dp; - struct in6_ifaddr *ia; - boolean_t candisable, dadstarted; - VERIFY(ifa != NULL); - candisable = FALSE; - IFA_LOCK(ifa); - ia = (struct in6_ifaddr *) ifa; - if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { - struct ip6aux *ip6a; - - candisable = TRUE; - ip6a = ip6_findaux(m); - - if (ip6a && (ip6a->ip6a_flags & IP6A_HASEEN) != 0) { - struct in6_addr in6 = ia->ia_addr.sin6_addr; - - nd6log((LOG_INFO, - "%s: eh_src=%02x:%02x:%02x:%02x:%02x:%02x -> %s\n", - __func__, - ip6a->ip6a_ehsrc[0], ip6a->ip6a_ehsrc[1], - ip6a->ip6a_ehsrc[2], ip6a->ip6a_ehsrc[3], - ip6a->ip6a_ehsrc[4], ip6a->ip6a_ehsrc[5], - if_name(ifa->ifa_ifp))); - - in6.s6_addr8[8] = ip6a->ip6a_ehsrc[0] ^ ND6_EUI64_UBIT; - in6.s6_addr8[9] = ip6a->ip6a_ehsrc[1]; - in6.s6_addr8[10] = ip6a->ip6a_ehsrc[2]; - in6.s6_addr8[11] = 0xff; - in6.s6_addr8[12] = 0xfe; - in6.s6_addr8[13] = ip6a->ip6a_ehsrc[3]; - in6.s6_addr8[14] = ip6a->ip6a_ehsrc[4]; - in6.s6_addr8[15] = ip6a->ip6a_ehsrc[5]; - - if (!IN6_ARE_ADDR_EQUAL(&in6, &ia->ia_addr.sin6_addr)) { - nd6log((LOG_ERR, "%s: DAD NS for %s on %s " - "is from another MAC address.\n", __func__, - ip6_sprintf(&ia->ia_addr.sin6_addr), - if_name(ifa->ifa_ifp))); - candisable = FALSE; - } - } else { - nd6log((LOG_INFO, - "%s: no eh_src for DAD NS %s at %s.\n", __func__, - ip6_sprintf(&ia->ia_addr.sin6_addr), - if_name(ifa->ifa_ifp))); - } - } - IFA_UNLOCK(ifa); - /* If DAD has not yet started, then this DAD NS probe is proof that - * another node has started first. Otherwise, it could be a multicast - * loopback, in which case it should be counted and handled later in - * the DAD timer callback. - */ - dadstarted = FALSE; - dp = nd6_dad_find(ifa); - if (dp != NULL) { - DAD_LOCK(dp); - ++dp->dad_ns_icount; - if (candisable) - ++dp->dad_nd_ixcount; - if (dp->dad_ns_ocount > 0) - dadstarted = TRUE; - DAD_UNLOCK(dp); - DAD_REMREF(dp); - dp = NULL; + /* Ignore Nonce option when Enhanced DAD is disabled. */ + if (dad_enhanced == 0) { + ndopt_nonce = NULL; } - nd6log((LOG_INFO, "%s: dadstarted=%d candisable=%d\n", - __func__, dadstarted, candisable)); + dp = nd6_dad_find(ifa, ndopt_nonce); + if (dp == NULL) { + return; + } - if (!dadstarted) { - nd6log((LOG_INFO, - "%s: duplicate IPv6 address %s [processing NS on %s]\n", - __func__, ip6_sprintf(&ia->ia_addr.sin6_addr), - if_name(ifa->ifa_ifp))); - nd6_dad_duplicated(ifa); + DAD_LOCK(dp); + ++dp->dad_ns_icount; + if (lladdr && lladdrlen >= ETHER_ADDR_LEN) { + memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN); + /* fine to truncate as it is compared against sdl_alen */ + dp->dad_lladdrlen = (uint8_t)lladdrlen; } + DAD_UNLOCK(dp); + DAD_REMREF(dp); } +/* + * @brief Called to process received NA for DAD + * + * @param m is the pointer to the packet's mbuf + * @param ifp is the pointer to the interface on which packet + * was receicved. + * @param taddr is pointer to target's IPv6 address + * @param lladdr is target's link layer information + * @param lladdrlen is target's linklayer length + * + * @return NULL if the packet is consumed by DAD processing, else + * pointer to the mbuf. + */ static struct mbuf * nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr, caddr_t lladdr, int lladdrlen) { - struct ifaddr *ifa; - struct in6_ifaddr *ia; - struct dadq *dp; - struct nd_ifinfo *ndi; - boolean_t candisable, ignoring; + struct ifaddr *ifa = NULL; + struct in6_ifaddr *ia = NULL; + struct dadq *dp = NULL; + struct nd_ifinfo *ndi = NULL; + boolean_t replicated; ifa = (struct ifaddr *) in6ifa_ifpwithaddr(ifp, taddr); - if (ifa == NULL) + if (ifa == NULL) { return m; + } - candisable = FALSE; - ignoring = FALSE; + replicated = FALSE; - /* The ND6_IFF_IGNORE_NA flag is here for legacy reasons. */ - lck_rw_lock_shared(nd_if_rwlock); + /* Get the ND6_IFF_REPLICATED flag. */ ndi = ND_IFINFO(ifp); if (ndi != NULL && ndi->initialized) { lck_mtx_lock(&ndi->lock); - ignoring = !!(ndi->flags & ND6_IFF_IGNORE_NA); + replicated = !!(ndi->flags & ND6_IFF_REPLICATED); lck_mtx_unlock(&ndi->lock); } - lck_rw_done(nd_if_rwlock); - if (ignoring) { - nd6log((LOG_INFO, "%s: ignoring duplicate NA on " - "%s [ND6_IFF_IGNORE_NA]\n", __func__, if_name(ifp))); + + if (replicated) { + nd6log(info, "%s: ignoring duplicate NA on " + "replicated interface %s\n", __func__, if_name(ifp)); goto done; } @@ -2041,21 +2302,11 @@ nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr, IFA_LOCK(ifa); ia = (struct in6_ifaddr *) ifa; - /* - * If the address is a link-local address formed from an interface - * identifier based on the hardware address which is supposed to be - * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP - * operation on the interface SHOULD be disabled according to RFC 4862, - * section 5.4.5, but here we decide not to disable if the target - * hardware address is not also ours, which is a transitory possibility - * in the presence of network-resident sleep proxies on the local link. - */ - if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) { IFA_UNLOCK(ifa); - nd6log((LOG_INFO, "%s: ignoring duplicate NA on " + nd6log(info, "%s: ignoring duplicate NA on " "%s [DAD not in progress]\n", __func__, - if_name(ifp))); + if_name(ifp)); goto done; } @@ -2064,100 +2315,39 @@ nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr, * the L2-header source address, if we have seen it, with the target * address, and ignoring the NA if they don't match. */ - if (lladdr != NULL && lladdrlen == ETHER_ADDR_LEN) { + if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) { struct ip6aux *ip6a = ip6_findaux(m); if (ip6a && (ip6a->ip6a_flags & IP6A_HASEEN) != 0 && bcmp(ip6a->ip6a_ehsrc, lladdr, ETHER_ADDR_LEN) != 0) { IFA_UNLOCK(ifa); - nd6log((LOG_ERR, "%s: ignoring duplicate NA on %s " - "[eh_src != tgtlladdr]\n", __func__, if_name(ifp))); + nd6log(error, "%s: ignoring duplicate NA on %s " + "[eh_src != tgtlladdr]\n", __func__, if_name(ifp)); goto done; } } IFA_UNLOCK(ifa); - if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) && - !(ia->ia6_flags & IN6_IFF_SECURED)) { - struct in6_addr in6; - - /* - * To avoid over-reaction, we only apply this logic when we are - * very sure that hardware addresses are supposed to be unique. - */ - switch (ifp->if_type) { - case IFT_BRIDGE: - case IFT_ETHER: - case IFT_FDDI: - case IFT_ATM: - case IFT_IEEE1394: -#ifdef IFT_IEEE80211 - case IFT_IEEE80211: -#endif - /* Check if our hardware address matches the target */ - if (lladdr != NULL && lladdrlen > 0) { - struct ifaddr *llifa; - struct sockaddr_dl *sdl; - - llifa = ifp->if_lladdr; - IFA_LOCK(llifa); - sdl = (struct sockaddr_dl *)(void *) - llifa->ifa_addr; - if (lladdrlen == sdl->sdl_alen && - bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) - candisable = TRUE; - IFA_UNLOCK(llifa); - } - in6 = ia->ia_addr.sin6_addr; - if (in6_iid_from_hw(ifp, &in6) != 0) - break; - - /* Refine decision about whether IPv6 can be disabled */ - IFA_LOCK(ifa); - if (candisable && - !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { - /* - * Apply this logic only to the embedded MAC - * address form of link-local IPv6 address. - */ - candisable = FALSE; - } else if (lladdr == NULL && - IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { - /* - * We received a NA with no target link-layer - * address option. This means that someone else - * has our address. Mark it as a hardware - * duplicate so we disable IPv6 later on. - */ - candisable = TRUE; - } - IFA_UNLOCK(ifa); - break; - default: - break; - } - } - - dp = nd6_dad_find(ifa); + dp = nd6_dad_find(ifa, NULL); if (dp == NULL) { - nd6log((LOG_INFO, "%s: no DAD structure for %s on %s.\n", - __func__, ip6_sprintf(taddr), if_name(ifp))); + nd6log(info, "%s: no DAD structure for %s on %s.\n", + __func__, ip6_sprintf(taddr), if_name(ifp)); goto done; } DAD_LOCK_SPIN(dp); + if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) { + memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN); + dp->dad_lladdrlen = (uint8_t)lladdrlen; + } dp->dad_na_icount++; - if (candisable) - dp->dad_nd_ixcount++; DAD_UNLOCK(dp); DAD_REMREF(dp); /* remove the address. */ - nd6log((LOG_INFO, + nd6log(info, "%s: duplicate IPv6 address %s [processing NA on %s]\n", __func__, - ip6_sprintf(taddr), if_name(ifp))); - nd6_dad_duplicated(ifa); - + ip6_sprintf(taddr), if_name(ifp)); done: IFA_LOCK_ASSERT_NOTHELD(ifa); IFA_REMREF(ifa); @@ -2168,17 +2358,19 @@ done: static void dad_addref(struct dadq *dp, int locked) { - if (!locked) + if (!locked) { DAD_LOCK_SPIN(dp); - else + } else { DAD_LOCK_ASSERT_HELD(dp); + } if (++dp->dad_refcount == 0) { panic("%s: dad %p wraparound refcnt\n", __func__, dp); /* NOTREACHED */ } - if (!locked) + if (!locked) { DAD_UNLOCK(dp); + } } static void @@ -2187,8 +2379,9 @@ dad_remref(struct dadq *dp) struct ifaddr *ifa; DAD_LOCK_SPIN(dp); - if (dp->dad_refcount == 0) + if (dp->dad_refcount == 0) { panic("%s: dad %p negative refcnt\n", __func__, dp); + } --dp->dad_refcount; if (dp->dad_refcount > 0) { DAD_UNLOCK(dp); @@ -2203,7 +2396,7 @@ dad_remref(struct dadq *dp) } if ((ifa = dp->dad_ifa) != NULL) { - IFA_REMREF(ifa); /* drop dad_ifa reference */ + IFA_REMREF(ifa); /* drop dad_ifa reference */ dp->dad_ifa = NULL; } @@ -2215,8 +2408,9 @@ void nd6_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen) { /* Nothing more to do if it's disabled */ - if (nd6_llreach_base == 0) + if (nd6_llreach_base == 0) { return; + } ifnet_llreach_set_reachable(ifp, ETHERTYPE_IPV6, addr, alen); } @@ -2232,11 +2426,11 @@ nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa, VERIFY(sdl && (void *)sa != (void *)sdl); VERIFY(sin6 && (void *)sa != (void *)sin6); - bzero(sin6, sizeof *sin6); + bzero(sin6, sizeof(*sin6)); sin6->sin6_len = sizeof *sin6; sin6->sin6_family = AF_INET6; - bzero(sdl, sizeof *sdl); + bzero(sdl, sizeof(*sdl)); sdl->sdl_len = sizeof *sdl; sdl->sdl_family = AF_LINK; sdl->sdl_type = ifp->if_type; @@ -2248,8 +2442,9 @@ nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa, struct in6_addr *in6 = &sin6a->sin6_addr; VERIFY(sa->sa_len == sizeof *sin6); + VERIFY(strlen(ifp->if_name) <= IFNAMSIZ); - sdl->sdl_nlen = strlen(ifp->if_name); + sdl->sdl_nlen = (u_char)strlen(ifp->if_name); bcopy(ifp->if_name, sdl->sdl_data, sdl->sdl_nlen); if (in6->s6_addr[11] == 0xff && in6->s6_addr[12] == 0xfe) { sdl->sdl_alen = ETHER_ADDR_LEN; @@ -2272,17 +2467,18 @@ nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa, struct in6_addr *in6 = &sin6->sin6_addr; caddr_t lla = LLADDR(sdla); - VERIFY(sa->sa_len <= sizeof *sdl); + VERIFY(sa->sa_len <= sizeof(*sdl)); bcopy(sa, sdl, sa->sa_len); sin6->sin6_scope_id = sdla->sdl_index; - if (sin6->sin6_scope_id == 0) + if (sin6->sin6_scope_id == 0) { sin6->sin6_scope_id = ifp->if_index; + } in6->s6_addr[0] = 0xfe; in6->s6_addr[1] = 0x80; - if (sdla->sdl_alen == EUI64_LENGTH) + if (sdla->sdl_alen == EUI64_LENGTH) { bcopy(lla, &in6->s6_addr[8], EUI64_LENGTH); - else { + } else { VERIFY(sdla->sdl_alen == ETHER_ADDR_LEN); in6->s6_addr[8] = ((uint8_t) lla[0] ^ ND6_EUI64_UBIT); @@ -2303,29 +2499,41 @@ nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa, } } -void +int nd6_alt_node_present(struct ifnet *ifp, struct sockaddr_in6 *sin6, struct sockaddr_dl *sdl, int32_t rssi, int lqm, int npm) { struct rtentry *rt; struct llinfo_nd6 *ln; - struct if_llreach *lr; + struct if_llreach *lr = NULL; + const uint16_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1]; + + if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) && + (temp_embedded_id == 0)) { + sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index); + } nd6_cache_lladdr(ifp, &sin6->sin6_addr, LLADDR(sdl), sdl->sdl_alen, ND_NEIGHBOR_ADVERT, 0); - lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); + LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); lck_mtx_lock(rnh_lock); rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 1, 0, ifp->if_index); + + /* Restore the address that was passed to us */ + if (temp_embedded_id == 0) { + sin6->sin6_addr.s6_addr16[1] = 0; + } + if (rt != NULL) { RT_LOCK(rt); VERIFY(rt->rt_flags & RTF_LLINFO); VERIFY(rt->rt_llinfo); ln = rt->rt_llinfo; - ln->ln_state = ND6_LLINFO_REACHABLE; + ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE); ln_setexpire(ln, 0); lr = ln->ln_llreach; @@ -2346,32 +2554,57 @@ nd6_alt_node_present(struct ifnet *ifp, struct sockaddr_in6 *sin6, if (rt == NULL) { log(LOG_ERR, "%s: failed to add/update host route to %s.\n", __func__, ip6_sprintf(&sin6->sin6_addr)); + return EHOSTUNREACH; } else { - nd6log((LOG_DEBUG, "%s: host route to %s [lr=0x%llx]\n", + nd6log(debug, "%s: host route to %s [lr=0x%llx]\n", __func__, ip6_sprintf(&sin6->sin6_addr), - (uint64_t)VM_KERNEL_ADDRPERM(lr))); + (uint64_t)VM_KERNEL_ADDRPERM(lr)); + return 0; } } void -nd6_alt_node_absent(struct ifnet *ifp, struct sockaddr_in6 *sin6) +nd6_alt_node_absent(struct ifnet *ifp, struct sockaddr_in6 *sin6, struct sockaddr_dl *sdl) { struct rtentry *rt; + const uint16_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1]; + + nd6log(debug, "%s: host route to %s\n", __func__, + ip6_sprintf(&sin6->sin6_addr)); - nd6log((LOG_DEBUG, "%s: host route to %s\n", __func__, - ip6_sprintf(&sin6->sin6_addr))); + if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) && + (temp_embedded_id == 0)) { + sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index); + } - lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); + LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); lck_mtx_lock(rnh_lock); rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0, 0, ifp->if_index); + + /* Restore the address that was passed to us */ + if (temp_embedded_id == 0) { + sin6->sin6_addr.s6_addr16[1] = 0; + } + if (rt != NULL) { RT_LOCK(rt); - if (!(rt->rt_flags & (RTF_CLONING|RTF_PRCLONING)) && - (rt->rt_flags & (RTF_HOST|RTF_LLINFO|RTF_WASCLONED)) == - (RTF_HOST|RTF_LLINFO|RTF_WASCLONED)) { + if (!(rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) && + (rt->rt_flags & (RTF_HOST | RTF_LLINFO | RTF_WASCLONED)) == + (RTF_HOST | RTF_LLINFO | RTF_WASCLONED)) { + /* + * Copy the link layer information in SDL when present + * as it later gets used to issue the kernel event for + * node absence. + */ + if (sdl != NULL && rt->rt_gateway != NULL && + rt->rt_gateway->sa_family == AF_LINK && + SDL(rt->rt_gateway)->sdl_len <= sizeof(*sdl)) { + bcopy(rt->rt_gateway, sdl, SDL(rt->rt_gateway)->sdl_len); + } + rt->rt_flags |= RTF_CONDEMNED; RT_UNLOCK(rt);