]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/nd6_nbr.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6_nbr.c
CommitLineData
b0d623f7 1/*
eb6b6ca3 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
b0d623f7
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39037602 14 *
b0d623f7
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
b0d623f7
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39037602 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
1c79356b
A
28
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
1c79356b
A
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/malloc.h>
61#include <sys/mbuf.h>
62#include <sys/socket.h>
63#include <sys/sockio.h>
64#include <sys/time.h>
65#include <sys/kernel.h>
66#include <sys/errno.h>
1c79356b 67#include <sys/syslog.h>
6d2010ae
A
68#include <sys/sysctl.h>
69#include <sys/mcache.h>
70#include <sys/protosw.h>
1c79356b 71#include <kern/queue.h>
39037602 72#include <dev/random/randomdev.h>
1c79356b 73
6d2010ae
A
74#include <kern/locks.h>
75#include <kern/zalloc.h>
76
1c79356b 77#include <net/if.h>
6d2010ae 78#include <net/if_var.h>
1c79356b
A
79#include <net/if_types.h>
80#include <net/if_dl.h>
6d2010ae 81#include <net/if_llreach.h>
1c79356b 82#include <net/route.h>
39037602 83#include <net/dlil.h>
5ba3f43e 84#include <net/nwk_wq.h>
1c79356b
A
85
86#include <netinet/in.h>
87#include <netinet/in_var.h>
88#include <netinet6/in6_var.h>
39236c6e 89#include <netinet6/in6_ifattach.h>
1c79356b
A
90#include <netinet/ip6.h>
91#include <netinet6/ip6_var.h>
92#include <netinet6/nd6.h>
6d2010ae 93#include <netinet6/scope6_var.h>
1c79356b
A
94#include <netinet/icmp6.h>
95
1c79356b
A
96#if IPSEC
97#include <netinet6/ipsec.h>
9bccf70c
A
98#include <netinet6/ipsec6.h>
99#endif
1c79356b 100
1c79356b 101struct dadq;
39037602 102static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
91447636 103void nd6_dad_stoptimer(struct ifaddr *);
91447636
A
104static void nd6_dad_timer(struct ifaddr *);
105static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
39037602 106static void nd6_dad_ns_input(struct ifaddr *, char *, int, struct nd_opt_nonce *);
39236c6e
A
107static struct mbuf *nd6_dad_na_input(struct mbuf *, struct ifnet *,
108 struct in6_addr *, caddr_t, int);
6d2010ae
A
109static void dad_addref(struct dadq *, int);
110static void dad_remref(struct dadq *);
111static struct dadq *nd6_dad_attach(struct dadq *, struct ifaddr *);
112static void nd6_dad_detach(struct dadq *, struct ifaddr *);
f427ee49 113static void nd6_dad_duplicated(struct ifaddr *);
1c79356b 114
0a7de745 115static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
1c79356b 116
0a7de745 117#define DAD_LOCK_ASSERT_HELD(_dp) \
5ba3f43e 118 LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED)
6d2010ae 119
0a7de745 120#define DAD_LOCK_ASSERT_NOTHELD(_dp) \
5ba3f43e 121 LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED)
6d2010ae 122
0a7de745 123#define DAD_LOCK(_dp) \
6d2010ae
A
124 lck_mtx_lock(&(_dp)->dad_lock)
125
0a7de745 126#define DAD_LOCK_SPIN(_dp) \
6d2010ae
A
127 lck_mtx_lock_spin(&(_dp)->dad_lock)
128
0a7de745
A
129#define DAD_CONVERT_LOCK(_dp) do { \
130 DAD_LOCK_ASSERT_HELD(_dp); \
131 lck_mtx_convert_spin(&(_dp)->dad_lock); \
6d2010ae
A
132} while (0)
133
0a7de745 134#define DAD_UNLOCK(_dp) \
6d2010ae
A
135 lck_mtx_unlock(&(_dp)->dad_lock)
136
0a7de745 137#define DAD_ADDREF(_dp) \
6d2010ae
A
138 dad_addref(_dp, 0)
139
0a7de745 140#define DAD_ADDREF_LOCKED(_dp) \
6d2010ae
A
141 dad_addref(_dp, 1)
142
0a7de745 143#define DAD_REMREF(_dp) \
6d2010ae
A
144 dad_remref(_dp)
145
91447636
A
146extern lck_mtx_t *dad6_mutex;
147extern lck_mtx_t *nd6_mutex;
6d2010ae 148
0a7de745 149static int nd6_llreach_base = 30; /* seconds */
6d2010ae 150
316670eb
A
151static struct sockaddr_in6 hostrtmask;
152
6d2010ae 153SYSCTL_DECL(_net_inet6_icmp6);
6d2010ae 154SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_llreach_base,
39037602 155 CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_llreach_base, 0,
6d2010ae
A
156 "default ND6 link-layer reachability max lifetime (in seconds)");
157
f427ee49 158int dad_enhanced = ND6_DAD_ENHANCED_DEFAULT;
39037602
A
159SYSCTL_DECL(_net_inet6_ip6);
160SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_RW | CTLFLAG_LOCKED,
161 &dad_enhanced, 0,
162 "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD.");
163
1c79356b 164/*
6d2010ae
A
165 * Obtain a link-layer source cache entry for the sender.
166 *
167 * NOTE: This is currently only for ND6/Ethernet.
168 */
169void
170nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr,
171 unsigned int alen, boolean_t solicited)
172{
173 struct llinfo_nd6 *ln = rt->rt_llinfo;
174
175 if (nd6_llreach_base != 0 &&
db609669 176 (ln->ln_expire != 0 || (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) &&
39236c6e 177 !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
0a7de745 178 ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */
6d2010ae
A
179 alen == ifp->if_addrlen) {
180 struct if_llreach *lr;
181 const char *why = NULL, *type = "";
182
183 /* Become a regular mutex, just in case */
184 RT_CONVERT_LOCK(rt);
185
186 if ((lr = ln->ln_llreach) != NULL) {
187 type = (solicited ? "ND6 advertisement" :
188 "ND6 unsolicited announcement");
189 /*
190 * If target has changed, create a new record;
191 * otherwise keep existing record.
192 */
193 IFLR_LOCK(lr);
194 if (bcmp(addr, lr->lr_key.addr, alen) != 0) {
195 IFLR_UNLOCK(lr);
196 /* Purge any link-layer info caching */
197 VERIFY(rt->rt_llinfo_purge != NULL);
198 rt->rt_llinfo_purge(rt);
199 lr = NULL;
200 why = " for different target HW address; "
201 "using new llreach record";
202 } else {
0a7de745 203 lr->lr_probes = 0; /* reset probe count */
6d2010ae
A
204 IFLR_UNLOCK(lr);
205 if (solicited) {
206 why = " for same target HW address; "
207 "keeping existing llreach record";
208 }
209 }
210 }
211
212 if (lr == NULL) {
213 lr = ln->ln_llreach = ifnet_llreach_alloc(ifp,
214 ETHERTYPE_IPV6, addr, alen, nd6_llreach_base);
215 if (lr != NULL) {
0a7de745
A
216 lr->lr_probes = 0; /* reset probe count */
217 if (why == NULL) {
6d2010ae 218 why = "creating new llreach record";
0a7de745 219 }
6d2010ae
A
220 }
221 }
222
223 if (nd6_debug && lr != NULL && why != NULL) {
224 char tmp[MAX_IPv6_STR_LEN];
225
cb323159 226 nd6log(debug, "%s: %s%s for %s\n", if_name(ifp),
39236c6e 227 type, why, inet_ntop(AF_INET6,
cb323159 228 &SIN6(rt_key(rt))->sin6_addr, tmp, sizeof(tmp)));
6d2010ae
A
229 }
230 }
231}
232
233void
234nd6_llreach_use(struct llinfo_nd6 *ln)
235{
0a7de745 236 if (ln->ln_llreach != NULL) {
6d2010ae 237 ln->ln_lastused = net_uptime();
0a7de745 238 }
6d2010ae
A
239}
240
241/*
242 * Input a Neighbor Solicitation Message.
1c79356b 243 *
39236c6e
A
244 * Based on RFC 4861
245 * Based on RFC 4862 (duplicate address detection)
1c79356b
A
246 */
247void
91447636
A
248nd6_ns_input(
249 struct mbuf *m,
250 int off,
251 int icmp6len)
1c79356b
A
252{
253 struct ifnet *ifp = m->m_pkthdr.rcvif;
254 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
eb6b6ca3 255 struct nd_neighbor_solicit *nd_ns = NULL;
1c79356b
A
256 struct in6_addr saddr6 = ip6->ip6_src;
257 struct in6_addr daddr6 = ip6->ip6_dst;
eb6b6ca3
A
258 struct in6_addr taddr6 = {};
259 struct in6_addr myaddr6 = {};
1c79356b 260 char *lladdr = NULL;
b0d623f7 261 struct ifaddr *ifa = NULL;
1c79356b 262 int lladdrlen = 0;
316670eb 263 int anycast = 0, proxy = 0, dadprogress = 0;
eb6b6ca3
A
264 int tlladdr = 0;
265 union nd_opts ndopts = {};
266 struct sockaddr_dl proxydl = {};
267 boolean_t advrouter = FALSE;
268 boolean_t is_dad_probe = FALSE;
3e170ce0 269 int oflgclr = 0;
316670eb
A
270
271 /* Expect 32-bit aligned data pointer on strict-align platforms */
272 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
1c79356b 273
0a7de745 274 IP6_EXTHDR_CHECK(m, off, icmp6len, return );
eb6b6ca3 275 ip6 = mtod(m, struct ip6_hdr *);
9bccf70c 276 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
39236c6e
A
277 m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
278
9bccf70c 279 taddr6 = nd_ns->nd_ns_target;
0a7de745 280 if (in6_setscope(&taddr6, ifp, NULL) != 0) {
6d2010ae 281 goto bad;
0a7de745 282 }
9bccf70c 283
316670eb 284 if (ip6->ip6_hlim != IPV6_MAXHLIM) {
cb323159 285 nd6log(error,
9bccf70c
A
286 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
287 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
cb323159 288 ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
9bccf70c 289 goto bad;
1c79356b
A
290 }
291
39236c6e
A
292 is_dad_probe = IN6_IS_ADDR_UNSPECIFIED(&saddr6);
293 if (is_dad_probe) {
6d2010ae
A
294 /* dst has to be a solicited node multicast address. */
295 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
55e303ae 296 /* don't check ifindex portion */
6d2010ae
A
297 daddr6.s6_addr32[1] == 0 &&
298 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
299 daddr6.s6_addr8[12] == 0xff) {
55e303ae 300 ; /* good */
1c79356b 301 } else {
cb323159
A
302 nd6log(info, "nd6_ns_input: bad DAD packet "
303 "(wrong ip6 dst)\n");
1c79356b
A
304 goto bad;
305 }
6d2010ae
A
306 } else if (!nd6_onlink_ns_rfc4861) {
307 struct sockaddr_in6 src_sa6;
308
309 /*
310 * According to recent IETF discussions, it is not a good idea
311 * to accept a NS from an address which would not be deemed
312 * to be a neighbor otherwise. This point is expected to be
313 * clarified in future revisions of the specification.
314 */
315 bzero(&src_sa6, sizeof(src_sa6));
316 src_sa6.sin6_family = AF_INET6;
317 src_sa6.sin6_len = sizeof(src_sa6);
318 src_sa6.sin6_addr = saddr6;
319 if (!nd6_is_addr_neighbor(&src_sa6, ifp, 0)) {
cb323159
A
320 nd6log(info, "nd6_ns_input: "
321 "NS packet from non-neighbor\n");
6d2010ae
A
322 goto bad;
323 }
1c79356b
A
324 }
325
1c79356b 326 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
cb323159 327 nd6log(info, "nd6_ns_input: bad NS target (multicast)\n");
1c79356b
A
328 goto bad;
329 }
330
1c79356b
A
331 icmp6len -= sizeof(*nd_ns);
332 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
333 if (nd6_options(&ndopts) < 0) {
cb323159
A
334 nd6log(info,
335 "nd6_ns_input: invalid ND option, ignored\n");
9bccf70c
A
336 /* nd6_options have incremented stats */
337 goto freeit;
1c79356b
A
338 }
339
340 if (ndopts.nd_opts_src_lladdr) {
55e303ae 341 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
1c79356b
A
342 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
343 }
6d2010ae 344
39236c6e 345 if (is_dad_probe && lladdr) {
cb323159
A
346 nd6log(info, "nd6_ns_input: bad DAD packet "
347 "(link-layer address option)\n");
1c79356b
A
348 goto bad;
349 }
350
351 /*
352 * Attaching target link-layer address to the NA?
353 * (RFC 2461 7.2.4)
354 *
355 * NS IP dst is unicast/anycast MUST NOT add
356 * NS IP dst is solicited-node multicast MUST add
357 *
358 * In implementation, we add target link-layer address by default.
359 * We do not add one in MUST NOT cases.
360 */
0a7de745 361 if (!IN6_IS_ADDR_MULTICAST(&daddr6)) {
1c79356b 362 tlladdr = 0;
0a7de745 363 } else {
1c79356b 364 tlladdr = 1;
0a7de745 365 }
1c79356b
A
366
367 /*
368 * Target address (taddr6) must be either:
369 * (1) Valid unicast/anycast address for my receiving interface,
370 * (2) Unicast address for which I'm offering proxy service, or
316670eb 371 * (3) "tentative" or "optimistic" address [DAD is in progress].
1c79356b
A
372 */
373 /* (1) and (3) check. */
374 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
375
376 /* (2) check. */
6d2010ae 377 if (ifa == NULL) {
1c79356b
A
378 struct rtentry *rt;
379 struct sockaddr_in6 tsin6;
380
6d2010ae 381 bzero(&tsin6, sizeof tsin6);
1c79356b
A
382 tsin6.sin6_len = sizeof(struct sockaddr_in6);
383 tsin6.sin6_family = AF_INET6;
384 tsin6.sin6_addr = taddr6;
385
6d2010ae
A
386 rt = rtalloc1_scoped((struct sockaddr *)&tsin6, 0, 0,
387 ifp->if_index);
388
b0d623f7
A
389 if (rt != NULL) {
390 RT_LOCK(rt);
391 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
392 rt->rt_gateway->sa_family == AF_LINK) {
393 /*
394 * proxy NDP for single entry
395 */
396 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(
0a7de745 397 ifp, IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
b0d623f7
A
398 if (ifa) {
399 proxy = 1;
400 proxydl = *SDL(rt->rt_gateway);
401 }
1c79356b 402 }
b0d623f7 403 RT_UNLOCK(rt);
1c79356b 404 rtfree(rt);
b0d623f7 405 }
1c79356b 406 }
316670eb
A
407 if (ifa == NULL && ip6_forwarding && nd6_prproxy) {
408 /*
409 * Is the target address part of the prefix that is being
410 * proxied and installed on another interface?
411 */
412 ifa = (struct ifaddr *)in6ifa_prproxyaddr(&taddr6);
413 }
6d2010ae 414 if (ifa == NULL) {
1c79356b 415 /*
316670eb
A
416 * We've got an NS packet, and we don't have that address
417 * assigned for us. We MUST silently ignore it on this
418 * interface, c.f. RFC 4861 7.2.3.
419 *
420 * Forwarding associated with NDPRF_PRPROXY may apply.
1c79356b 421 */
0a7de745 422 if (ip6_forwarding && nd6_prproxy) {
316670eb 423 nd6_prproxy_ns_input(ifp, &saddr6, lladdr,
39037602
A
424 lladdrlen, &daddr6, &taddr6,
425 (ndopts.nd_opts_nonce == NULL) ? NULL :
426 ndopts.nd_opts_nonce->nd_opt_nonce);
0a7de745 427 }
1c79356b
A
428 goto freeit;
429 }
6d2010ae 430 IFA_LOCK(ifa);
1c79356b
A
431 myaddr6 = *IFA_IN6(ifa);
432 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
316670eb
A
433 dadprogress =
434 ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DADPROGRESS;
6d2010ae
A
435 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) {
436 IFA_UNLOCK(ifa);
1c79356b 437 goto freeit;
6d2010ae
A
438 }
439 IFA_UNLOCK(ifa);
1c79356b
A
440
441 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
cb323159 442 nd6log(info,
1c79356b
A
443 "nd6_ns_input: lladdrlen mismatch for %s "
444 "(if %d, NS packet %d)\n",
cb323159 445 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
9bccf70c 446 goto bad;
1c79356b
A
447 }
448
449 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
cb323159 450 nd6log(info,
0a7de745 451 "nd6_ns_input: duplicate IP6 address %s\n",
cb323159 452 ip6_sprintf(&saddr6));
1c79356b
A
453 goto freeit;
454 }
455
456 /*
457 * We have neighbor solicitation packet, with target address equals to
316670eb 458 * one of my DAD in-progress addresses.
1c79356b
A
459 *
460 * src addr how to process?
461 * --- ---
462 * multicast of course, invalid (rejected in ip6_input)
3e170ce0 463 * unicast somebody is doing address resolution
1c79356b
A
464 * unspec dup address detection
465 *
39236c6e
A
466 * The processing is defined in the "draft standard" RFC 4862 (and by
467 * RFC 4429, which is a "proposed standard" update to its obsolete
468 * predecessor, RFC 2462) The reason optimistic DAD is not included
469 * in RFC 4862 is entirely due to IETF procedural considerations.
1c79356b 470 */
316670eb 471 if (dadprogress) {
1c79356b
A
472 /*
473 * If source address is unspecified address, it is for
6d2010ae 474 * duplicate address detection.
1c79356b
A
475 *
476 * If not, the packet is for addess resolution;
3e170ce0
A
477 * silently ignore it when not optimistic
478 *
479 * Per RFC 4429 the reply for an optimistic address must
480 * have the Override flag cleared
1c79356b 481 */
3e170ce0
A
482 if (!is_dad_probe && (dadprogress & IN6_IFF_OPTIMISTIC) != 0) {
483 oflgclr = 1;
484 } else {
0a7de745 485 if (is_dad_probe) {
39037602 486 nd6_dad_ns_input(ifa, lladdr, lladdrlen, ndopts.nd_opts_nonce);
0a7de745 487 }
1c79356b 488
3e170ce0
A
489 goto freeit;
490 }
1c79356b
A
491 }
492
316670eb 493 /* Are we an advertising router on this interface? */
f427ee49 494 advrouter = (ifp->if_ipv6_router_mode != IPV6_ROUTER_MODE_DISABLED);
316670eb 495
1c79356b
A
496 /*
497 * If the source address is unspecified address, entries must not
498 * be created or updated.
316670eb
A
499 * It looks that sender is performing DAD. If I'm using the address,
500 * and it's a "preferred" address, i.e. not optimistic, then output NA
501 * toward all-node multicast address, to tell the sender that I'm using
1c79356b
A
502 * the address.
503 * S bit ("solicited") must be zero.
504 */
39236c6e 505 if (is_dad_probe) {
1c79356b 506 saddr6 = in6addr_linklocal_allnodes;
0a7de745 507 if (in6_setscope(&saddr6, ifp, NULL) != 0) {
6d2010ae 508 goto bad;
0a7de745
A
509 }
510 if ((dadprogress & IN6_IFF_OPTIMISTIC) == 0) {
316670eb
A
511 nd6_na_output(ifp, &saddr6, &taddr6,
512 ((anycast || proxy || !tlladdr) ? 0 :
513 ND_NA_FLAG_OVERRIDE) | (advrouter ?
514 ND_NA_FLAG_ROUTER : 0), tlladdr, proxy ?
515 (struct sockaddr *)&proxydl : NULL);
0a7de745 516 }
1c79356b
A
517 goto freeit;
518 }
519
316670eb
A
520 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
521 ND_NEIGHBOR_SOLICIT, 0);
1c79356b
A
522
523 nd6_na_output(ifp, &saddr6, &taddr6,
3e170ce0 524 ((anycast || proxy || !tlladdr || oflgclr) ? 0 : ND_NA_FLAG_OVERRIDE) |
316670eb
A
525 (advrouter ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
526 tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL);
0a7de745 527freeit:
1c79356b 528 m_freem(m);
0a7de745 529 if (ifa != NULL) {
6d2010ae 530 IFA_REMREF(ifa);
0a7de745 531 }
1c79356b
A
532 return;
533
0a7de745 534bad:
cb323159
A
535 nd6log(error, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
536 nd6log(error, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
537 nd6log(error, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
9bccf70c 538 icmp6stat.icp6s_badns++;
1c79356b 539 m_freem(m);
0a7de745 540 if (ifa != NULL) {
6d2010ae 541 IFA_REMREF(ifa);
0a7de745 542 }
1c79356b
A
543}
544
545/*
6d2010ae 546 * Output a Neighbor Solicitation Message. Caller specifies:
1c79356b
A
547 * - ICMP6 header source IP6 address
548 * - ND6 header target IP6 address
549 * - ND6 header source datalink address
550 *
39236c6e
A
551 * Based on RFC 4861
552 * Based on RFC 4862 (duplicate address detection)
553 * Based on RFC 4429 (optimistic duplicate address detection)
b0d623f7
A
554 *
555 * Caller must bump up ln->ln_rt refcnt to make sure 'ln' doesn't go
556 * away if there is a llinfo_nd6 passed in.
1c79356b
A
557 */
558void
91447636
A
559nd6_ns_output(
560 struct ifnet *ifp,
561 const struct in6_addr *daddr6,
562 const struct in6_addr *taddr6,
0a7de745
A
563 struct llinfo_nd6 *ln, /* for source address determination */
564 uint8_t *nonce) /* duplicated address detection */
1c79356b
A
565{
566 struct mbuf *m;
567 struct ip6_hdr *ip6;
568 struct nd_neighbor_solicit *nd_ns;
569 struct in6_ifaddr *ia = NULL;
6d2010ae
A
570 struct in6_addr *src, src_in, src_storage;
571 struct ip6_moptions *im6o = NULL;
0a7de745 572 struct ifnet *outif = NULL;
1c79356b
A
573 int icmp6len;
574 int maxlen;
6d2010ae 575 int flags;
1c79356b 576 caddr_t mac;
6d2010ae 577 struct route_in6 ro;
a39ff7e2 578 struct ip6_out_args ip6oa;
316670eb 579 u_int32_t rtflags = 0;
f427ee49 580 boolean_t is_optimistic = FALSE;
6d2010ae 581
0a7de745 582 if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) || IN6_IS_ADDR_MULTICAST(taddr6)) {
1c79356b 583 return;
0a7de745 584 }
1c79356b 585
316670eb 586 bzero(&ro, sizeof(ro));
a39ff7e2 587 bzero(&ip6oa, sizeof(ip6oa));
6d2010ae 588 ip6oa.ip6oa_boundif = ifp->if_index;
a39ff7e2
A
589 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
590 IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED;
591 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
592 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
593
316670eb 594 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
6d2010ae 595
1c79356b
A
596 /* estimate the size of message */
597 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
598 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
599 if (max_linkhdr + maxlen >= MCLBYTES) {
9bccf70c 600#if DIAGNOSTIC
1c79356b
A
601 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
602 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
603#endif
604 return;
605 }
606
0a7de745 607 MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1c79356b
A
608 if (m && max_linkhdr + maxlen >= MHLEN) {
609 MCLGET(m, M_DONTWAIT);
610 if ((m->m_flags & M_EXT) == 0) {
611 m_free(m);
612 m = NULL;
613 }
614 }
0a7de745 615 if (m == NULL) {
1c79356b 616 return;
0a7de745 617 }
9bccf70c 618 m->m_pkthdr.rcvif = NULL;
1c79356b
A
619
620 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
621 m->m_flags |= M_MCAST;
6d2010ae 622
f427ee49 623 im6o = ip6_allocmoptions(Z_NOWAIT);
6d2010ae
A
624 if (im6o == NULL) {
625 m_freem(m);
626 return;
627 }
628
629 im6o->im6o_multicast_ifp = ifp;
316670eb 630 im6o->im6o_multicast_hlim = IPV6_MAXHLIM;
6d2010ae 631 im6o->im6o_multicast_loop = 0;
1c79356b
A
632 }
633
634 icmp6len = sizeof(*nd_ns);
635 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
0a7de745 636 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
1c79356b
A
637
638 /* fill neighbor solicitation packet */
639 ip6 = mtod(m, struct ip6_hdr *);
640 ip6->ip6_flow = 0;
641 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
642 ip6->ip6_vfc |= IPV6_VERSION;
643 /* ip6->ip6_plen will be set later */
644 ip6->ip6_nxt = IPPROTO_ICMPV6;
316670eb 645 ip6->ip6_hlim = IPV6_MAXHLIM;
0a7de745 646 if (daddr6) {
1c79356b 647 ip6->ip6_dst = *daddr6;
0a7de745 648 } else {
1c79356b 649 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
6d2010ae 650 ip6->ip6_dst.s6_addr16[1] = 0;
1c79356b
A
651 ip6->ip6_dst.s6_addr32[1] = 0;
652 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
653 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
654 ip6->ip6_dst.s6_addr8[12] = 0xff;
0a7de745 655 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) {
6d2010ae 656 goto bad;
0a7de745 657 }
1c79356b 658 }
39037602 659 if (nonce == NULL) {
1c79356b
A
660 /*
661 * RFC2461 7.2.2:
662 * "If the source address of the packet prompting the
663 * solicitation is the same as one of the addresses assigned
664 * to the outgoing interface, that address SHOULD be placed
665 * in the IP Source Address of the outgoing solicitation.
666 * Otherwise, any one of the addresses assigned to the
667 * interface should be used."
668 *
669 * We use the source address for the prompting packet
670 * (saddr6), if:
671 * - saddr6 is given from the caller (by giving "ln"), and
672 * - saddr6 belongs to the outgoing interface.
6d2010ae 673 * Otherwise, we perform the source address selection as usual.
1c79356b 674 */
0a7de745 675 struct ip6_hdr *hip6; /* hold ip6 */
6d2010ae 676 struct in6_addr *hsrc = NULL;
b0d623f7
A
677
678 /* Caller holds ref on this route */
679 if (ln != NULL) {
680 RT_LOCK(ln->ln_rt);
6d2010ae
A
681 /*
682 * assuming every packet in ln_hold has the same IP
683 * header
684 */
b0d623f7
A
685 if (ln->ln_hold != NULL) {
686 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
687 /* XXX pullup? */
0a7de745 688 if (sizeof(*hip6) < ln->ln_hold->m_len) {
6d2010ae 689 hsrc = &hip6->ip6_src;
0a7de745 690 } else {
6d2010ae 691 hsrc = NULL;
0a7de745 692 }
6d2010ae
A
693 }
694 /* Update probe count, if applicable */
695 if (ln->ln_llreach != NULL) {
696 IFLR_LOCK_SPIN(ln->ln_llreach);
697 ln->ln_llreach->lr_probes++;
698 IFLR_UNLOCK(ln->ln_llreach);
b0d623f7 699 }
316670eb 700 rtflags = ln->ln_rt->rt_flags;
b0d623f7 701 RT_UNLOCK(ln->ln_rt);
1c79356b 702 }
316670eb
A
703 if (hsrc != NULL && (ia = in6ifa_ifpwithaddr(ifp, hsrc)) &&
704 (ia->ia6_flags & IN6_IFF_OPTIMISTIC) == 0) {
6d2010ae 705 src = hsrc;
6d2010ae
A
706 } else {
707 int error;
708 struct sockaddr_in6 dst_sa;
709
710 bzero(&dst_sa, sizeof(dst_sa));
711 dst_sa.sin6_family = AF_INET6;
712 dst_sa.sin6_len = sizeof(dst_sa);
713 dst_sa.sin6_addr = ip6->ip6_dst;
714
715 src = in6_selectsrc(&dst_sa, NULL,
716 NULL, &ro, NULL, &src_storage, ip6oa.ip6oa_boundif,
717 &error);
718 if (src == NULL) {
cb323159 719 nd6log(debug,
6d2010ae
A
720 "nd6_ns_output: source can't be "
721 "determined: dst=%s, error=%d\n",
722 ip6_sprintf(&dst_sa.sin6_addr),
cb323159 723 error);
6d2010ae
A
724 goto bad;
725 }
316670eb 726
39236c6e
A
727 if (ia != NULL) {
728 IFA_REMREF(&ia->ia_ifa);
729 ia = NULL;
730 }
fe8ab488
A
731 /*
732 * RFC 4429 section 3.2:
733 * When a node has a unicast packet to send
39037602
A
734 * from an Optimistic Address to a neighbor,
735 * but does not know the neighbor's link-layer
fe8ab488
A
736 * address, it MUST NOT perform Address
737 * Resolution.
738 */
316670eb 739 ia = in6ifa_ifpwithaddr(ifp, src);
f427ee49 740 if (ia == NULL) {
cb323159 741 nd6log(debug,
316670eb
A
742 "nd6_ns_output: no preferred source "
743 "available: dst=%s\n",
cb323159 744 ip6_sprintf(&dst_sa.sin6_addr));
316670eb
A
745 goto bad;
746 }
f427ee49
A
747 if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
748 is_optimistic = TRUE;
749 nd6log(debug,
750 "nd6_ns_output: preferred source "
751 "available is optimistic: dst=%s\n",
752 ip6_sprintf(&dst_sa.sin6_addr));
753 }
6d2010ae 754 }
1c79356b
A
755 } else {
756 /*
757 * Source address for DAD packet must always be IPv6
758 * unspecified address. (0::0)
6d2010ae
A
759 * We actually don't have to 0-clear the address (we did it
760 * above), but we do so here explicitly to make the intention
761 * clearer.
1c79356b 762 */
6d2010ae
A
763 bzero(&src_in, sizeof(src_in));
764 src = &src_in;
316670eb 765 ip6oa.ip6oa_flags &= ~IP6OAF_BOUND_SRCADDR;
1c79356b 766 }
6d2010ae 767 ip6->ip6_src = *src;
1c79356b
A
768 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
769 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
770 nd_ns->nd_ns_code = 0;
771 nd_ns->nd_ns_reserved = 0;
772 nd_ns->nd_ns_target = *taddr6;
6d2010ae 773 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
1c79356b
A
774
775 /*
776 * Add source link-layer address option.
777 *
778 * spec implementation
779 * --- ---
780 * DAD packet MUST NOT do not add the option
f427ee49 781 * Source is optimistic MUST NOT do not add the option
1c79356b
A
782 * there's no link layer address:
783 * impossible do not add the option
784 * there's link layer address:
785 * Multicast NS MUST add one add the option
786 * Unicast NS SHOULD add one add the option
f427ee49
A
787 *
788 * XXX We deviate from RFC 4429 and still use optimistic DAD as source
789 * for address resolution. However to ensure that we do not interfere
790 * with neighbor cache entries of other neighbors, we MUST ensure
791 * that SLLAO is not sent. Also note, sending multicast NS without SLLAO
792 * is also a deviation from RFC 4861.
1c79356b 793 */
f427ee49 794 if (nonce == NULL && (mac = nd6_ifptomac(ifp)) && !is_optimistic) {
1c79356b
A
795 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
796 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
797 /* 8 byte alignments... */
798 optlen = (optlen + 7) & ~7;
316670eb 799
1c79356b
A
800 m->m_pkthdr.len += optlen;
801 m->m_len += optlen;
802 icmp6len += optlen;
803 bzero((caddr_t)nd_opt, optlen);
804 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
f427ee49 805 nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
1c79356b
A
806 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
807 }
39037602
A
808 /*
809 * Add a Nonce option (RFC 3971) to detect looped back NS messages.
810 * This behavior is documented as Enhanced Duplicate Address
811 * Detection in draft-ietf-6man-enhanced-dad-13.
812 * net.inet6.ip6.dad_enhanced=0 disables this.
813 */
814 if (dad_enhanced != 0 && nonce != NULL && !(ifp->if_flags & IFF_POINTOPOINT)) {
815 int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
816 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
817 /* 8-byte alignment is required. */
818 optlen = (optlen + 7) & ~7;
1c79356b 819
39037602
A
820 m->m_pkthdr.len += optlen;
821 m->m_len += optlen;
822 icmp6len += optlen;
823 bzero((caddr_t)nd_opt, optlen);
824 nd_opt->nd_opt_type = ND_OPT_NONCE;
f427ee49 825 nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
39037602
A
826 bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN);
827 }
1c79356b
A
828 ip6->ip6_plen = htons((u_short)icmp6len);
829 nd_ns->nd_ns_cksum = 0;
830 nd_ns->nd_ns_cksum
0a7de745 831 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
1c79356b 832
39037602 833 flags = nonce ? IPV6_UNSPECSRC : 0;
6d2010ae
A
834 flags |= IPV6_OUTARGS;
835
39236c6e
A
836 /*
837 * PKTF_{INET,INET6}_RESOLVE_RTR are mutually exclusive, so make
838 * sure only one of them is set (just in case.)
839 */
840 m->m_pkthdr.pkt_flags &= ~(PKTF_INET_RESOLVE | PKTF_RESOLVE_RTR);
841 m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
316670eb
A
842 /*
843 * If this is a NS for resolving the (default) router, mark
844 * the packet accordingly so that the driver can find out,
845 * in case it needs to perform driver-specific action(s).
846 */
0a7de745 847 if (rtflags & RTF_ROUTER) {
39236c6e 848 m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR;
0a7de745 849 }
316670eb
A
850
851 if (ifp->if_eflags & IFEF_TXSTART) {
39236c6e
A
852 /*
853 * Use control service class if the interface
316670eb
A
854 * supports transmit-start model
855 */
856 (void) m_set_service_class(m, MBUF_SC_CTL);
857 }
858
cb323159 859 ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF;
f427ee49 860 ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
6d2010ae 861 ip6_output(m, NULL, NULL, flags, im6o, &outif, &ip6oa);
1c79356b
A
862 if (outif) {
863 icmp6_ifstat_inc(outif, ifs6_out_msg);
864 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
6d2010ae 865 ifnet_release(outif);
1c79356b
A
866 }
867 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
6d2010ae 868
39236c6e 869exit:
0a7de745 870 if (im6o != NULL) {
6d2010ae 871 IM6O_REMREF(im6o);
0a7de745 872 }
39236c6e 873
0a7de745 874 ROUTE_RELEASE(&ro); /* we don't cache this route. */
39236c6e 875
0a7de745 876 if (ia != NULL) {
6d2010ae 877 IFA_REMREF(&ia->ia_ifa);
0a7de745 878 }
6d2010ae
A
879 return;
880
881bad:
6d2010ae 882 m_freem(m);
39236c6e 883 goto exit;
1c79356b
A
884}
885
886/*
887 * Neighbor advertisement input handling.
888 *
39236c6e
A
889 * Based on RFC 4861
890 * Based on RFC 4862 (duplicate address detection)
1c79356b
A
891 *
892 * the following items are not implemented yet:
39236c6e
A
893 * - anycast advertisement delay rule (RFC 4861 7.2.7, SHOULD)
894 * - proxy advertisement delay rule (RFC 4861 7.2.8, last paragraph, "should")
1c79356b
A
895 */
896void
39236c6e 897nd6_na_input(struct mbuf *m, int off, int icmp6len)
1c79356b
A
898{
899 struct ifnet *ifp = m->m_pkthdr.rcvif;
900 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
901 struct nd_neighbor_advert *nd_na;
316670eb 902 struct in6_addr saddr6 = ip6->ip6_src;
1c79356b
A
903 struct in6_addr daddr6 = ip6->ip6_dst;
904 struct in6_addr taddr6;
905 int flags;
906 int is_router;
907 int is_solicited;
908 int is_override;
909 char *lladdr = NULL;
910 int lladdrlen = 0;
1c79356b
A
911 struct llinfo_nd6 *ln;
912 struct rtentry *rt;
913 struct sockaddr_dl *sdl;
914 union nd_opts ndopts;
39236c6e 915 uint64_t timenow;
3e170ce0 916 bool send_nc_alive_kev = false;
1c79356b 917
db609669 918 if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) {
cb323159 919 nd6log(info, "nd6_na_input: on ND6ALT interface!\n");
0a7de745 920 goto freeit;
db609669
A
921 }
922
316670eb
A
923 /* Expect 32-bit aligned data pointer on strict-align platforms */
924 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
925
926 if (ip6->ip6_hlim != IPV6_MAXHLIM) {
cb323159 927 nd6log(error,
9bccf70c
A
928 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
929 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
cb323159 930 ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
9bccf70c 931 goto bad;
1c79356b
A
932 }
933
0a7de745 934 IP6_EXTHDR_CHECK(m, off, icmp6len, return );
eb6b6ca3 935 ip6 = mtod(m, struct ip6_hdr *);
1c79356b 936 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
39236c6e 937 m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
6d2010ae 938
1c79356b
A
939 flags = nd_na->nd_na_flags_reserved;
940 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
941 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
942 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
943
6d2010ae 944 taddr6 = nd_na->nd_na_target;
0a7de745
A
945 if (in6_setscope(&taddr6, ifp, NULL)) {
946 goto bad; /* XXX: impossible */
947 }
1c79356b 948 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
cb323159 949 nd6log(error,
1c79356b 950 "nd6_na_input: invalid target address %s\n",
cb323159 951 ip6_sprintf(&taddr6));
9bccf70c 952 goto bad;
1c79356b 953 }
0a7de745 954 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1c79356b 955 if (is_solicited) {
cb323159
A
956 nd6log(error,
957 "nd6_na_input: a solicited adv is multicasted\n");
9bccf70c 958 goto bad;
1c79356b 959 }
0a7de745 960 }
1c79356b
A
961
962 icmp6len -= sizeof(*nd_na);
963 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
964 if (nd6_options(&ndopts) < 0) {
cb323159
A
965 nd6log(info,
966 "nd6_na_input: invalid ND option, ignored\n");
9bccf70c 967 /* nd6_options have incremented stats */
1c79356b
A
968 goto freeit;
969 }
970
971 if (ndopts.nd_opts_tgt_lladdr) {
972 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
973 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
1c79356b 974
39236c6e 975 if (((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
cb323159 976 nd6log(info,
39236c6e
A
977 "nd6_na_input: lladdrlen mismatch for %s "
978 "(if %d, NA packet %d)\n",
0a7de745 979 ip6_sprintf(&taddr6), ifp->if_addrlen,
cb323159 980 lladdrlen - 2);
39236c6e 981 goto bad;
6d2010ae 982 }
1c79356b
A
983 }
984
39236c6e 985 m = nd6_dad_na_input(m, ifp, &taddr6, lladdr, lladdrlen);
0a7de745 986 if (m == NULL) {
39236c6e 987 return;
0a7de745 988 }
1c79356b 989
316670eb 990 /* Forwarding associated with NDPRF_PRPROXY may apply. */
0a7de745 991 if (ip6_forwarding && nd6_prproxy) {
316670eb 992 nd6_prproxy_na_input(ifp, &saddr6, &daddr6, &taddr6, flags);
0a7de745 993 }
316670eb 994
1c79356b 995 /*
6d2010ae 996 * If no neighbor cache entry is found, NA SHOULD silently be
316670eb
A
997 * discarded. If we are forwarding (and Scoped Routing is in
998 * effect), try to see if there is a neighbor cache entry on
999 * another interface (in case we are doing prefix proxying.)
1c79356b 1000 */
316670eb 1001 if ((rt = nd6_lookup(&taddr6, 0, ifp, 0)) == NULL) {
0a7de745 1002 if (!ip6_forwarding || !nd6_prproxy) {
316670eb 1003 goto freeit;
0a7de745 1004 }
316670eb 1005
0a7de745 1006 if ((rt = nd6_lookup(&taddr6, 0, NULL, 0)) == NULL) {
316670eb 1007 goto freeit;
0a7de745 1008 }
316670eb
A
1009
1010 RT_LOCK_ASSERT_HELD(rt);
1011 if (rt->rt_ifp != ifp) {
1012 /*
1013 * Purge any link-layer info caching.
1014 */
0a7de745 1015 if (rt->rt_llinfo_purge != NULL) {
316670eb 1016 rt->rt_llinfo_purge(rt);
0a7de745 1017 }
316670eb
A
1018
1019 /* Adjust route ref count for the interfaces */
1020 if (rt->rt_if_ref_fn != NULL) {
1021 rt->rt_if_ref_fn(ifp, 1);
1022 rt->rt_if_ref_fn(rt->rt_ifp, -1);
1023 }
1024
1025 /* Change the interface when the existing route is on */
1026 rt->rt_ifp = ifp;
39236c6e
A
1027
1028 /*
1029 * If rmx_mtu is not locked, update it
1030 * to the MTU used by the new interface.
1031 */
0a7de745 1032 if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
39236c6e 1033 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
0a7de745 1034 }
316670eb
A
1035 }
1036 }
b0d623f7
A
1037
1038 RT_LOCK_ASSERT_HELD(rt);
1039 if ((ln = rt->rt_llinfo) == NULL ||
1040 (sdl = SDL(rt->rt_gateway)) == NULL) {
1041 RT_REMREF_LOCKED(rt);
1042 RT_UNLOCK(rt);
1c79356b 1043 goto freeit;
b0d623f7 1044 }
1c79356b 1045
39236c6e
A
1046 timenow = net_uptime();
1047
1c79356b
A
1048 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1049 /*
1050 * If the link-layer has address, and no lladdr option came,
1051 * discard the packet.
1052 */
b0d623f7
A
1053 if (ifp->if_addrlen && !lladdr) {
1054 RT_REMREF_LOCKED(rt);
1055 RT_UNLOCK(rt);
1c79356b 1056 goto freeit;
b0d623f7 1057 }
1c79356b
A
1058
1059 /*
1060 * Record link-layer address, and update the state.
1061 */
1062 sdl->sdl_alen = ifp->if_addrlen;
1063 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1064 if (is_solicited) {
3e170ce0 1065 send_nc_alive_kev = (rt->rt_flags & RTF_ROUTER) ? true : false;
39037602 1066 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
39236c6e 1067 if (ln->ln_expire != 0) {
3e170ce0 1068 struct nd_ifinfo *ndi = NULL;
316670eb 1069
316670eb
A
1070 ndi = ND_IFINFO(rt->rt_ifp);
1071 VERIFY(ndi != NULL && ndi->initialized);
1072 lck_mtx_lock(&ndi->lock);
39236c6e 1073 ln_setexpire(ln, timenow + ndi->reachable);
316670eb 1074 lck_mtx_unlock(&ndi->lock);
39236c6e
A
1075 RT_UNLOCK(rt);
1076 lck_mtx_lock(rnh_lock);
1077 nd6_sched_timeout(NULL, NULL);
1078 lck_mtx_unlock(rnh_lock);
1079 RT_LOCK(rt);
b0d623f7 1080 }
9bccf70c 1081 } else {
39037602 1082 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
39236c6e 1083 ln_setexpire(ln, timenow + nd6_gctimer);
9bccf70c 1084 }
5ba3f43e
A
1085
1086
1087 /*
1088 * Enqueue work item to invoke callback for this
1089 * route entry
1090 */
1091 route_event_enqueue_nwk_wq_entry(rt, NULL,
1092 ROUTE_LLENTRY_RESOLVED, NULL, TRUE);
1093
f427ee49 1094 if ((ln->ln_router = (short)is_router) != 0) {
5ba3f43e
A
1095 struct radix_node_head *rnh = NULL;
1096 struct route_event rt_ev;
1097 route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_RESOLVED);
9bccf70c
A
1098 /*
1099 * This means a router's state has changed from
1100 * non-reachable to probably reachable, and might
1101 * affect the status of associated prefixes..
5ba3f43e
A
1102 * We already have a reference on rt. Don't need to
1103 * take one for the unlock/lock.
9bccf70c 1104 */
b0d623f7 1105 RT_UNLOCK(rt);
5ba3f43e
A
1106 lck_mtx_lock(rnh_lock);
1107 rnh = rt_tables[AF_INET6];
1108
0a7de745 1109 if (rnh != NULL) {
5ba3f43e
A
1110 (void) rnh->rnh_walktree(rnh, route_event_walktree,
1111 (void *)&rt_ev);
0a7de745 1112 }
5ba3f43e 1113 lck_mtx_unlock(rnh_lock);
6d2010ae
A
1114 lck_mtx_lock(nd6_mutex);
1115 pfxlist_onlink_check();
1116 lck_mtx_unlock(nd6_mutex);
b0d623f7 1117 RT_LOCK(rt);
9bccf70c 1118 }
1c79356b 1119 } else {
5ba3f43e 1120 int llchange = 0;
1c79356b
A
1121
1122 /*
1123 * Check if the link-layer address has changed or not.
1124 */
0a7de745 1125 if (lladdr == NULL) {
1c79356b 1126 llchange = 0;
0a7de745 1127 } else {
1c79356b 1128 if (sdl->sdl_alen) {
0a7de745 1129 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
1c79356b 1130 llchange = 1;
0a7de745 1131 } else {
1c79356b 1132 llchange = 0;
0a7de745
A
1133 }
1134 } else {
1c79356b 1135 llchange = 1;
0a7de745 1136 }
1c79356b
A
1137 }
1138
1139 /*
5ba3f43e 1140 * This is VERY complex. Look at it with care.
1c79356b
A
1141 *
1142 * override solicit lladdr llchange action
1143 * (L: record lladdr)
1144 *
1145 * 0 0 n -- (2c)
1146 * 0 0 y n (2b) L
1147 * 0 0 y y (1) REACHABLE->STALE
1148 * 0 1 n -- (2c) *->REACHABLE
1149 * 0 1 y n (2b) L *->REACHABLE
1150 * 0 1 y y (1) REACHABLE->STALE
1151 * 1 0 n -- (2a)
1152 * 1 0 y n (2a) L
1153 * 1 0 y y (2a) L *->STALE
1154 * 1 1 n -- (2a) *->REACHABLE
1155 * 1 1 y n (2a) L *->REACHABLE
1156 * 1 1 y y (2a) L *->REACHABLE
1157 */
6d2010ae 1158 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
1c79356b
A
1159 /*
1160 * If state is REACHABLE, make it STALE.
1161 * no other updates should be done.
1162 */
9bccf70c 1163 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
39037602 1164 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
39236c6e 1165 ln_setexpire(ln, timenow + nd6_gctimer);
9bccf70c 1166 }
b0d623f7
A
1167 RT_REMREF_LOCKED(rt);
1168 RT_UNLOCK(rt);
1c79356b 1169 goto freeit;
0a7de745
A
1170 } else if (is_override /* (2a) */
1171 || (!is_override && (lladdr && !llchange)) /* (2b) */
1172 || !lladdr) { /* (2c) */
1c79356b
A
1173 /*
1174 * Update link-local address, if any.
1175 */
1176 if (lladdr) {
1177 sdl->sdl_alen = ifp->if_addrlen;
1178 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1179 }
1180
1181 /*
1182 * If solicited, make the state REACHABLE.
1183 * If not solicited and the link-layer address was
1184 * changed, make it STALE.
1185 */
1186 if (is_solicited) {
39037602 1187 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
39236c6e 1188 if (ln->ln_expire != 0) {
3e170ce0 1189 struct nd_ifinfo *ndi = NULL;
316670eb 1190
316670eb
A
1191 ndi = ND_IFINFO(ifp);
1192 VERIFY(ndi != NULL && ndi->initialized);
1193 lck_mtx_lock(&ndi->lock);
39236c6e
A
1194 ln_setexpire(ln,
1195 timenow + ndi->reachable);
316670eb 1196 lck_mtx_unlock(&ndi->lock);
39236c6e
A
1197 RT_UNLOCK(rt);
1198 lck_mtx_lock(rnh_lock);
1199 nd6_sched_timeout(NULL, NULL);
1200 lck_mtx_unlock(rnh_lock);
1201 RT_LOCK(rt);
1c79356b
A
1202 }
1203 } else {
9bccf70c 1204 if (lladdr && llchange) {
39037602 1205 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
39236c6e 1206 ln_setexpire(ln, timenow + nd6_gctimer);
9bccf70c 1207 }
1c79356b 1208 }
5ba3f43e
A
1209
1210 /*
1211 * XXX
1212 * The above is somewhat convoluted, for now just
1213 * issue a callback for LLENTRY changed.
1214 */
1215 /* Enqueue work item to invoke callback for this route entry */
a39ff7e2
A
1216 if (llchange) {
1217 route_event_enqueue_nwk_wq_entry(rt, NULL,
1218 ROUTE_LLENTRY_CHANGED, NULL, TRUE);
1219 }
5ba3f43e
A
1220
1221 /*
a39ff7e2
A
1222 * If the router's link-layer address has changed,
1223 * notify routes using this as gateway so they can
1224 * update any cached information.
5ba3f43e 1225 */
a39ff7e2 1226 if (ln->ln_router && is_router && llchange) {
5ba3f43e
A
1227 struct radix_node_head *rnh = NULL;
1228 struct route_event rt_ev;
1229 route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_CHANGED);
1230 /*
1231 * This means a router's state has changed from
1232 * non-reachable to probably reachable, and might
1233 * affect the status of associated prefixes..
1234 *
1235 * We already have a valid rt reference here.
1236 * We don't need to take another one for unlock/lock.
1237 */
1238 RT_UNLOCK(rt);
1239 lck_mtx_lock(rnh_lock);
1240 rnh = rt_tables[AF_INET6];
1241
0a7de745 1242 if (rnh != NULL) {
5ba3f43e
A
1243 (void) rnh->rnh_walktree(rnh, route_event_walktree,
1244 (void *)&rt_ev);
0a7de745 1245 }
5ba3f43e
A
1246 lck_mtx_unlock(rnh_lock);
1247 RT_LOCK(rt);
1248 }
1c79356b
A
1249 }
1250
1251 if (ln->ln_router && !is_router) {
1252 /*
1253 * The peer dropped the router flag.
1254 * Remove the sender from the Default Router List and
1255 * update the Destination Cache entries.
1256 */
1257 struct nd_defrouter *dr;
1258 struct in6_addr *in6;
b0d623f7 1259 struct ifnet *rt_ifp = rt->rt_ifp;
1c79356b 1260
316670eb
A
1261 in6 = &((struct sockaddr_in6 *)
1262 (void *)rt_key(rt))->sin6_addr;
55e303ae 1263
b0d623f7 1264 RT_UNLOCK(rt);
91447636 1265 lck_mtx_lock(nd6_mutex);
f427ee49
A
1266 /*
1267 * XXX Handle router lists for route information option
1268 * as well.
1269 */
1270 dr = defrouter_lookup(NULL, in6, rt_ifp);
91447636 1271 if (dr) {
f427ee49
A
1272 TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
1273 defrtrlist_del(dr, NULL);
39037602 1274 NDDR_REMREF(dr); /* remove list reference */
6d2010ae 1275 NDDR_REMREF(dr);
91447636 1276 lck_mtx_unlock(nd6_mutex);
316670eb 1277 } else {
91447636 1278 lck_mtx_unlock(nd6_mutex);
39037602
A
1279 /*
1280 * Even if the neighbor is not in the
1281 * default router list, the neighbor
1282 * may be used as a next hop for some
1283 * destinations (e.g. redirect case).
1284 * So we must call rt6_flush explicitly.
1285 */
1286 rt6_flush(&ip6->ip6_src, rt_ifp);
1c79356b 1287 }
b0d623f7 1288 RT_LOCK(rt);
1c79356b 1289 }
f427ee49 1290 ln->ln_router = (short)is_router;
1c79356b 1291 }
3e170ce0
A
1292
1293 if (send_nc_alive_kev && (ifp->if_addrlen == IF_LLREACH_MAXLEN)) {
1294 struct kev_msg ev_msg;
1295 struct kev_nd6_ndalive nd6_ndalive;
1296 bzero(&ev_msg, sizeof(ev_msg));
1297 bzero(&nd6_ndalive, sizeof(nd6_ndalive));
1298 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1299 ev_msg.kev_class = KEV_NETWORK_CLASS;
1300 ev_msg.kev_subclass = KEV_ND6_SUBCLASS;
1301 ev_msg.event_code = KEV_ND6_NDALIVE;
1302
1303 nd6_ndalive.link_data.if_family = ifp->if_family;
1304 nd6_ndalive.link_data.if_unit = ifp->if_unit;
1305 strlcpy(nd6_ndalive.link_data.if_name,
1306 ifp->if_name,
1307 sizeof(nd6_ndalive.link_data.if_name));
1308 ev_msg.dv[0].data_ptr = &nd6_ndalive;
1309 ev_msg.dv[0].data_length =
0a7de745 1310 sizeof(nd6_ndalive);
39037602 1311 dlil_post_complete_msg(NULL, &ev_msg);
3e170ce0
A
1312 }
1313
b0d623f7 1314 RT_LOCK_ASSERT_HELD(rt);
1c79356b 1315 rt->rt_flags &= ~RTF_REJECT;
6d2010ae
A
1316
1317 /* cache the gateway (sender HW) address */
1318 nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, TRUE);
1319
1320 /* update the llinfo, send a queued packet if there is one */
1c79356b 1321 ln->ln_asked = 0;
b0d623f7 1322 if (ln->ln_hold != NULL) {
6d2010ae
A
1323 struct mbuf *m_hold, *m_hold_next;
1324 struct sockaddr_in6 sin6;
1325
1326 rtkey_to_sa6(rt, &sin6);
9bccf70c 1327 /*
6d2010ae
A
1328 * reset the ln_hold in advance, to explicitly
1329 * prevent a ln_hold lookup in nd6_output()
1330 * (wouldn't happen, though...)
9bccf70c 1331 */
3e170ce0
A
1332 m_hold = ln->ln_hold;
1333 ln->ln_hold = NULL;
0a7de745 1334 for (; m_hold; m_hold = m_hold_next) {
6d2010ae
A
1335 m_hold_next = m_hold->m_nextpkt;
1336 m_hold->m_nextpkt = NULL;
1337 /*
1338 * we assume ifp is not a loopback here, so just set
1339 * the 2nd argument as the 1st one.
1340 */
1341 RT_UNLOCK(rt);
316670eb 1342 nd6_output(ifp, ifp, m_hold, &sin6, rt, NULL);
6d2010ae
A
1343 RT_LOCK_SPIN(rt);
1344 }
1c79356b 1345 }
b0d623f7
A
1346 RT_REMREF_LOCKED(rt);
1347 RT_UNLOCK(rt);
5ba3f43e
A
1348 m_freem(m);
1349 return;
1c79356b 1350
6d2010ae 1351bad:
9bccf70c 1352 icmp6stat.icp6s_badna++;
39236c6e 1353 /* fall through */
39236c6e 1354freeit:
9bccf70c 1355 m_freem(m);
5ba3f43e 1356 return;
1c79356b
A
1357}
1358
1359/*
1360 * Neighbor advertisement output handling.
1361 *
1362 * Based on RFC 2461
1363 *
1364 * the following items are not implemented yet:
1365 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
1366 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
6d2010ae
A
1367 *
1368 * tlladdr - 1 if include target link-layer address
1369 * sdl0 - sockaddr_dl (= proxy NA) or NULL
1c79356b
A
1370 */
1371void
91447636
A
1372nd6_na_output(
1373 struct ifnet *ifp,
6d2010ae 1374 const struct in6_addr *daddr6_0,
91447636 1375 const struct in6_addr *taddr6,
b0d623f7 1376 uint32_t flags,
0a7de745
A
1377 int tlladdr, /* 1 if include target link-layer address */
1378 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
1c79356b
A
1379{
1380 struct mbuf *m;
1381 struct ip6_hdr *ip6;
1382 struct nd_neighbor_advert *nd_na;
6d2010ae 1383 struct ip6_moptions *im6o = NULL;
55e303ae 1384 caddr_t mac = NULL;
6d2010ae
A
1385 struct route_in6 ro;
1386 struct in6_addr *src, src_storage, daddr6;
316670eb 1387 struct in6_ifaddr *ia;
6d2010ae
A
1388 struct sockaddr_in6 dst_sa;
1389 int icmp6len, maxlen, error;
0a7de745 1390 struct ifnet *outif = NULL;
6d2010ae 1391
a39ff7e2 1392 struct ip6_out_args ip6oa;
6d2010ae
A
1393 bzero(&ro, sizeof(ro));
1394
0a7de745 1395 daddr6 = *daddr6_0; /* make a local copy for modification */
6d2010ae 1396
a39ff7e2 1397 bzero(&ip6oa, sizeof(ip6oa));
6d2010ae 1398 ip6oa.ip6oa_boundif = ifp->if_index;
a39ff7e2
A
1399 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
1400 IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED;
1401 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
1402 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
1403
316670eb 1404 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
1c79356b
A
1405
1406 /* estimate the size of message */
1407 maxlen = sizeof(*ip6) + sizeof(*nd_na);
1408 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
1409 if (max_linkhdr + maxlen >= MCLBYTES) {
9bccf70c 1410#if DIAGNOSTIC
1c79356b
A
1411 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
1412 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
1413#endif
1414 return;
1415 }
1416
0a7de745 1417 MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1c79356b
A
1418 if (m && max_linkhdr + maxlen >= MHLEN) {
1419 MCLGET(m, M_DONTWAIT);
1420 if ((m->m_flags & M_EXT) == 0) {
1421 m_free(m);
1422 m = NULL;
1423 }
1424 }
0a7de745 1425 if (m == NULL) {
1c79356b 1426 return;
0a7de745 1427 }
9bccf70c 1428 m->m_pkthdr.rcvif = NULL;
1c79356b 1429
6d2010ae 1430 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1c79356b 1431 m->m_flags |= M_MCAST;
6d2010ae 1432
f427ee49 1433 im6o = ip6_allocmoptions(Z_NOWAIT);
6d2010ae
A
1434 if (im6o == NULL) {
1435 m_freem(m);
1436 return;
1437 }
1438
1439 im6o->im6o_multicast_ifp = ifp;
316670eb 1440 im6o->im6o_multicast_hlim = IPV6_MAXHLIM;
6d2010ae 1441 im6o->im6o_multicast_loop = 0;
1c79356b
A
1442 }
1443
1444 icmp6len = sizeof(*nd_na);
1445 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
0a7de745 1446 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
1c79356b
A
1447
1448 /* fill neighbor advertisement packet */
1449 ip6 = mtod(m, struct ip6_hdr *);
1450 ip6->ip6_flow = 0;
1451 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1452 ip6->ip6_vfc |= IPV6_VERSION;
1453 ip6->ip6_nxt = IPPROTO_ICMPV6;
316670eb 1454 ip6->ip6_hlim = IPV6_MAXHLIM;
6d2010ae 1455 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
1c79356b 1456 /* reply to DAD */
6d2010ae
A
1457 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1458 daddr6.s6_addr16[1] = 0;
1459 daddr6.s6_addr32[1] = 0;
1460 daddr6.s6_addr32[2] = 0;
1461 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
0a7de745 1462 if (in6_setscope(&daddr6, ifp, NULL)) {
6d2010ae 1463 goto bad;
0a7de745 1464 }
6d2010ae 1465
1c79356b 1466 flags &= ~ND_NA_FLAG_SOLICITED;
0a7de745 1467 } else {
6d2010ae 1468 ip6->ip6_dst = daddr6;
0a7de745 1469 }
6d2010ae
A
1470
1471 bzero(&dst_sa, sizeof(struct sockaddr_in6));
1472 dst_sa.sin6_family = AF_INET6;
1473 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1474 dst_sa.sin6_addr = daddr6;
1c79356b
A
1475
1476 /*
1477 * Select a source whose scope is the same as that of the dest.
1478 */
6d2010ae
A
1479 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1480 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &src_storage,
1481 ip6oa.ip6oa_boundif, &error);
1482 if (src == NULL) {
cb323159 1483 nd6log(debug, "nd6_na_output: source can't be "
6d2010ae 1484 "determined: dst=%s, error=%d\n",
cb323159 1485 ip6_sprintf(&dst_sa.sin6_addr), error);
6d2010ae 1486 goto bad;
1c79356b 1487 }
6d2010ae 1488 ip6->ip6_src = *src;
b0d623f7 1489
316670eb
A
1490 /*
1491 * RFC 4429 requires not setting "override" flag on NA packets sent
1492 * from optimistic addresses.
1493 */
1494 ia = in6ifa_ifpwithaddr(ifp, src);
1495 if (ia != NULL) {
0a7de745 1496 if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
316670eb 1497 flags &= ~ND_NA_FLAG_OVERRIDE;
0a7de745 1498 }
316670eb
A
1499 IFA_REMREF(&ia->ia_ifa);
1500 }
1501
1c79356b
A
1502 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1503 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1504 nd_na->nd_na_code = 0;
1505 nd_na->nd_na_target = *taddr6;
6d2010ae 1506 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1c79356b
A
1507
1508 /*
1509 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1510 * see nd6_ns_input() for details.
1511 * Basically, if NS packet is sent to unicast/anycast addr,
1512 * target lladdr option SHOULD NOT be included.
1513 */
1514 if (tlladdr) {
1c79356b
A
1515 /*
1516 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1517 * lladdr in sdl0. If we are not proxying (sending NA for
1518 * my address) use lladdr configured for the interface.
1519 */
0a7de745 1520 if (sdl0 == NULL) {
1c79356b 1521 mac = nd6_ifptomac(ifp);
0a7de745 1522 } else if (sdl0->sa_family == AF_LINK) {
1c79356b 1523 struct sockaddr_dl *sdl;
316670eb 1524 sdl = (struct sockaddr_dl *)(void *)sdl0;
0a7de745 1525 if (sdl->sdl_alen == ifp->if_addrlen) {
1c79356b 1526 mac = LLADDR(sdl);
0a7de745 1527 }
1c79356b
A
1528 }
1529 }
1530 if (tlladdr && mac) {
1531 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1532 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
6d2010ae 1533
1c79356b
A
1534 /* roundup to 8 bytes alignment! */
1535 optlen = (optlen + 7) & ~7;
1536
1537 m->m_pkthdr.len += optlen;
1538 m->m_len += optlen;
1539 icmp6len += optlen;
1540 bzero((caddr_t)nd_opt, optlen);
1541 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
f427ee49 1542 nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
1c79356b 1543 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
0a7de745 1544 } else {
1c79356b 1545 flags &= ~ND_NA_FLAG_OVERRIDE;
0a7de745 1546 }
1c79356b
A
1547
1548 ip6->ip6_plen = htons((u_short)icmp6len);
1549 nd_na->nd_na_flags_reserved = flags;
1550 nd_na->nd_na_cksum = 0;
1551 nd_na->nd_na_cksum =
0a7de745 1552 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1c79356b 1553
39236c6e 1554 m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
316670eb
A
1555
1556 if (ifp->if_eflags & IFEF_TXSTART) {
1557 /* Use control service class if the interface supports
1558 * transmit-start model.
1559 */
1560 (void) m_set_service_class(m, MBUF_SC_CTL);
1561 }
1562
cb323159 1563 ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF;
f427ee49 1564 ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
6d2010ae 1565 ip6_output(m, NULL, NULL, IPV6_OUTARGS, im6o, &outif, &ip6oa);
1c79356b
A
1566 if (outif) {
1567 icmp6_ifstat_inc(outif, ifs6_out_msg);
1568 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
6d2010ae 1569 ifnet_release(outif);
1c79356b
A
1570 }
1571 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
6d2010ae 1572
39236c6e 1573exit:
0a7de745 1574 if (im6o != NULL) {
6d2010ae 1575 IM6O_REMREF(im6o);
0a7de745 1576 }
39236c6e
A
1577
1578 ROUTE_RELEASE(&ro);
6d2010ae
A
1579 return;
1580
1581bad:
6d2010ae 1582 m_freem(m);
39236c6e 1583 goto exit;
1c79356b
A
1584}
1585
1586caddr_t
91447636
A
1587nd6_ifptomac(
1588 struct ifnet *ifp)
1c79356b 1589{
6d2010ae
A
1590 switch (ifp->if_type) {
1591 case IFT_ARCNET:
1592 case IFT_ETHER:
1593 case IFT_IEEE8023ADLAG:
1594 case IFT_FDDI:
1595 case IFT_IEEE1394:
1596#ifdef IFT_L2VLAN
1597 case IFT_L2VLAN:
1598#endif
1599#ifdef IFT_IEEE80211
1600 case IFT_IEEE80211:
1601#endif
1602#ifdef IFT_CARP
1603 case IFT_CARP:
1604#endif
1605 case IFT_BRIDGE:
1606 case IFT_ISO88025:
cb323159 1607 case IFT_6LOWPAN:
0a7de745 1608 return (caddr_t)IF_LLADDR(ifp);
6d2010ae
A
1609 default:
1610 return NULL;
1611 }
1c79356b
A
1612}
1613
1614TAILQ_HEAD(dadq_head, dadq);
1615struct dadq {
6d2010ae 1616 decl_lck_mtx_data(, dad_lock);
0a7de745 1617 u_int32_t dad_refcount; /* reference count */
6d2010ae 1618 int dad_attached;
1c79356b
A
1619 TAILQ_ENTRY(dadq) dad_list;
1620 struct ifaddr *dad_ifa;
0a7de745
A
1621 int dad_count; /* max NS to send */
1622 int dad_ns_tcount; /* # of trials to send NS */
1623 int dad_ns_ocount; /* NS sent so far */
1c79356b
A
1624 int dad_ns_icount;
1625 int dad_na_icount;
0a7de745 1626 int dad_ns_lcount; /* looped back NS */
39037602
A
1627 int dad_loopbackprobe; /* probing state for loopback detection */
1628 uint8_t dad_lladdr[ETHER_ADDR_LEN];
1629 uint8_t dad_lladdrlen;
0a7de745 1630#define ND_OPT_NONCE_LEN32 \
39037602
A
1631 ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
1632 uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
1c79356b
A
1633};
1634
f427ee49 1635static ZONE_DECLARE(dad_zone, "nd6_dad", sizeof(struct dadq), ZC_ZFREE_CLEARMEM);
1c79356b 1636static struct dadq_head dadq;
6d2010ae
A
1637
1638void
1639nd6_nbr_init(void)
1640{
316670eb 1641 int i;
39037602 1642
6d2010ae
A
1643 TAILQ_INIT(&dadq);
1644
316670eb
A
1645 bzero(&hostrtmask, sizeof hostrtmask);
1646 hostrtmask.sin6_family = AF_INET6;
1647 hostrtmask.sin6_len = sizeof hostrtmask;
0a7de745 1648 for (i = 0; i < sizeof hostrtmask.sin6_addr; ++i) {
316670eb 1649 hostrtmask.sin6_addr.s6_addr[i] = 0xff;
0a7de745 1650 }
6d2010ae 1651}
1c79356b
A
1652
1653static struct dadq *
39037602 1654nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
1c79356b
A
1655{
1656 struct dadq *dp;
6d2010ae 1657
91447636 1658 lck_mtx_lock(dad6_mutex);
1c79356b 1659 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
6d2010ae 1660 DAD_LOCK_SPIN(dp);
39037602 1661 if (dp->dad_ifa != ifa) {
6d2010ae 1662 DAD_UNLOCK(dp);
39037602 1663 continue;
91447636 1664 }
39037602
A
1665
1666 /*
1667 * Skip if the nonce matches the received one.
1668 * +2 in the length is required because of type and
1669 * length fields are included in a header.
1670 */
1671 if (nonce != NULL &&
1672 nonce->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
1673 memcmp(&nonce->nd_opt_nonce[0], &dp->dad_nonce[0],
1674 ND_OPT_NONCE_LEN) == 0) {
cb323159 1675 nd6log(error, "%s: a looped back NS message is "
39037602
A
1676 "detected during DAD for %s. Ignoring.\n",
1677 if_name(ifa->ifa_ifp),
cb323159 1678 ip6_sprintf(IFA_IN6(ifa)));
39037602
A
1679 dp->dad_ns_lcount++;
1680 ++ip6stat.ip6s_dad_loopcount;
1681 DAD_UNLOCK(dp);
1682 continue;
1683 }
1684
1685 DAD_ADDREF_LOCKED(dp);
6d2010ae 1686 DAD_UNLOCK(dp);
39037602 1687 break;
1c79356b 1688 }
91447636 1689 lck_mtx_unlock(dad6_mutex);
0a7de745 1690 return dp;
1c79356b
A
1691}
1692
9bccf70c 1693void
91447636
A
1694nd6_dad_stoptimer(
1695 struct ifaddr *ifa)
9bccf70c 1696{
91447636 1697 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c 1698}
9bccf70c 1699
1c79356b 1700/*
6d2010ae 1701 * Start Duplicate Address Detection (DAD) for specified interface address.
1c79356b
A
1702 */
1703void
91447636
A
1704nd6_dad_start(
1705 struct ifaddr *ifa,
0a7de745 1706 int *tick_delay) /* minimum delay ticks for IFF_UP event */
1c79356b
A
1707{
1708 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1709 struct dadq *dp;
1c79356b 1710
cb323159 1711 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
fe8ab488
A
1712 __func__,
1713 ip6_sprintf(&ia->ia_addr.sin6_addr),
1714 if_name(ia->ia_ifp),
cb323159 1715 ia->ia6_flags);
fe8ab488 1716
1c79356b
A
1717 /*
1718 * If we don't need DAD, don't do it.
1719 * There are several cases:
1720 * - DAD is disabled (ip6_dad_count == 0)
1721 * - the interface address is anycast
1722 */
6d2010ae 1723 IFA_LOCK(&ia->ia_ifa);
316670eb 1724 if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
cb323159 1725 nd6log0(debug,
0a7de745
A
1726 "nd6_dad_start: not a tentative or optimistic address "
1727 "%s(%s)\n",
1728 ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 1729 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
6d2010ae 1730 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
1731 return;
1732 }
316670eb
A
1733 if (!ip6_dad_count || (ia->ia6_flags & IN6_IFF_ANYCAST) != 0) {
1734 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
6d2010ae 1735 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
1736 return;
1737 }
6d2010ae 1738 IFA_UNLOCK(&ia->ia_ifa);
0a7de745 1739 if (ifa->ifa_ifp == NULL) {
1c79356b 1740 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
0a7de745 1741 }
316670eb
A
1742 if (!(ifa->ifa_ifp->if_flags & IFF_UP) ||
1743 (ifa->ifa_ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
1c79356b 1744 return;
6d2010ae 1745 }
39037602 1746 if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
6d2010ae 1747 DAD_REMREF(dp);
1c79356b
A
1748 /* DAD already in progress */
1749 return;
1750 }
1751
f427ee49 1752 dp = zalloc_flags(dad_zone, Z_WAITOK | Z_ZERO);
6d2010ae
A
1753 lck_mtx_init(&dp->dad_lock, ifa_mtx_grp, ifa_mtx_attr);
1754
1755 /* Callee adds one reference for us */
1756 dp = nd6_dad_attach(dp, ifa);
1c79356b 1757
cb323159 1758 nd6log0(debug, "%s: starting %sDAD %sfor %s\n",
316670eb 1759 if_name(ifa->ifa_ifp),
fe8ab488 1760 (ia->ia6_flags & IN6_IFF_OPTIMISTIC) ? "optimistic " : "",
39037602 1761 (tick_delay == NULL) ? "immediately " : "",
cb323159 1762 ip6_sprintf(&ia->ia_addr.sin6_addr));
1c79356b
A
1763
1764 /*
1765 * Send NS packet for DAD, ip6_dad_count times.
1766 * Note that we must delay the first transmission, if this is the
1767 * first packet to be sent from the interface after interface
1768 * (re)initialization.
1769 */
2d21ac55 1770 if (tick_delay == NULL) {
b0d623f7 1771 u_int32_t retrans;
3e170ce0 1772 struct nd_ifinfo *ndi = NULL;
316670eb 1773
1c79356b 1774 nd6_dad_ns_output(dp, ifa);
316670eb
A
1775 ndi = ND_IFINFO(ifa->ifa_ifp);
1776 VERIFY(ndi != NULL && ndi->initialized);
1777 lck_mtx_lock(&ndi->lock);
1778 retrans = ndi->retrans * hz / 1000;
1779 lck_mtx_unlock(&ndi->lock);
b0d623f7 1780 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1c79356b
A
1781 } else {
1782 int ntick;
1783
0a7de745 1784 if (*tick_delay == 0) {
1c79356b 1785 ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
0a7de745 1786 } else {
2d21ac55 1787 ntick = *tick_delay + random() % (hz / 2);
0a7de745 1788 }
2d21ac55 1789 *tick_delay = ntick;
91447636 1790 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
0a7de745 1791 ntick);
1c79356b 1792 }
6d2010ae 1793
0a7de745 1794 DAD_REMREF(dp); /* drop our reference */
6d2010ae
A
1795}
1796
1797static struct dadq *
1798nd6_dad_attach(struct dadq *dp, struct ifaddr *ifa)
1799{
1800 lck_mtx_lock(dad6_mutex);
1801 DAD_LOCK(dp);
1802 dp->dad_ifa = ifa;
0a7de745 1803 IFA_ADDREF(ifa); /* for dad_ifa */
6d2010ae
A
1804 dp->dad_count = ip6_dad_count;
1805 dp->dad_ns_icount = dp->dad_na_icount = 0;
1806 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
39037602 1807 dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
6d2010ae
A
1808 VERIFY(!dp->dad_attached);
1809 dp->dad_attached = 1;
39037602 1810 dp->dad_lladdrlen = 0;
0a7de745
A
1811 DAD_ADDREF_LOCKED(dp); /* for caller */
1812 DAD_ADDREF_LOCKED(dp); /* for dadq_head list */
6d2010ae
A
1813 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1814 DAD_UNLOCK(dp);
1815 lck_mtx_unlock(dad6_mutex);
1816
0a7de745 1817 return dp;
6d2010ae
A
1818}
1819
1820static void
1821nd6_dad_detach(struct dadq *dp, struct ifaddr *ifa)
1822{
1823 int detached;
1824
1825 lck_mtx_lock(dad6_mutex);
1826 DAD_LOCK(dp);
1827 if ((detached = dp->dad_attached)) {
1828 VERIFY(dp->dad_ifa == ifa);
1829 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1830 dp->dad_list.tqe_next = NULL;
1831 dp->dad_list.tqe_prev = NULL;
1832 dp->dad_attached = 0;
1833 }
1834 DAD_UNLOCK(dp);
1835 lck_mtx_unlock(dad6_mutex);
1836 if (detached) {
0a7de745 1837 DAD_REMREF(dp); /* drop dadq_head reference */
6d2010ae 1838 }
1c79356b
A
1839}
1840
9bccf70c
A
1841/*
1842 * terminate DAD unconditionally. used for address removals.
1843 */
1844void
6d2010ae 1845nd6_dad_stop(struct ifaddr *ifa)
9bccf70c
A
1846{
1847 struct dadq *dp;
1848
39037602 1849 dp = nd6_dad_find(ifa, NULL);
9bccf70c
A
1850 if (!dp) {
1851 /* DAD wasn't started yet */
1852 return;
1853 }
1854
91447636 1855 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c 1856
6d2010ae 1857 nd6_dad_detach(dp, ifa);
0a7de745 1858 DAD_REMREF(dp); /* drop our reference */
9bccf70c
A
1859}
1860
1c79356b 1861static void
6d2010ae 1862nd6_unsol_na_output(struct ifaddr *ifa)
1c79356b 1863{
1c79356b 1864 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
6d2010ae
A
1865 struct ifnet *ifp = ifa->ifa_ifp;
1866 struct in6_addr saddr6, taddr6;
1867
1868 if ((ifp->if_flags & IFF_UP) == 0 ||
316670eb 1869 (ifp->if_flags & IFF_RUNNING) == 0 ||
0a7de745 1870 (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) {
6d2010ae 1871 return;
0a7de745 1872 }
6d2010ae
A
1873
1874 IFA_LOCK_SPIN(&ia->ia_ifa);
1875 taddr6 = ia->ia_addr.sin6_addr;
1876 IFA_UNLOCK(&ia->ia_ifa);
0a7de745 1877 if (in6_setscope(&taddr6, ifp, NULL) != 0) {
6d2010ae 1878 return;
0a7de745 1879 }
6d2010ae 1880 saddr6 = in6addr_linklocal_allnodes;
0a7de745 1881 if (in6_setscope(&saddr6, ifp, NULL) != 0) {
6d2010ae 1882 return;
0a7de745 1883 }
6d2010ae 1884
cb323159
A
1885 nd6log(info, "%s: sending unsolicited NA\n",
1886 if_name(ifa->ifa_ifp));
6d2010ae
A
1887
1888 nd6_na_output(ifp, &saddr6, &taddr6, ND_NA_FLAG_OVERRIDE, 1, NULL);
1889}
1890
1891static void
1892nd6_dad_timer(struct ifaddr *ifa)
1893{
1894 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1895 struct dadq *dp = NULL;
3e170ce0 1896 struct nd_ifinfo *ndi = NULL;
39037602 1897 u_int32_t retrans;
1c79356b 1898
1c79356b
A
1899 /* Sanity check */
1900 if (ia == NULL) {
cb323159 1901 nd6log0(error, "nd6_dad_timer: called with null parameter\n");
1c79356b
A
1902 goto done;
1903 }
fe8ab488 1904
cb323159 1905 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
fe8ab488
A
1906 __func__,
1907 ip6_sprintf(&ia->ia_addr.sin6_addr),
1908 if_name(ia->ia_ifp),
cb323159 1909 ia->ia6_flags);
fe8ab488 1910
39037602 1911 dp = nd6_dad_find(ifa, NULL);
1c79356b 1912 if (dp == NULL) {
cb323159 1913 nd6log0(error, "nd6_dad_timer: DAD structure not found\n");
1c79356b
A
1914 goto done;
1915 }
6d2010ae 1916 IFA_LOCK(&ia->ia_ifa);
1c79356b 1917 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
cb323159 1918 nd6log0(error, "nd6_dad_timer: called with duplicated address "
0a7de745
A
1919 "%s(%s)\n",
1920 ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 1921 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
6d2010ae 1922 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
1923 goto done;
1924 }
316670eb 1925 if ((ia->ia6_flags & IN6_IFF_DADPROGRESS) == 0) {
cb323159 1926 nd6log0(error, "nd6_dad_timer: not a tentative or optimistic "
0a7de745
A
1927 "address %s(%s)\n",
1928 ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 1929 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
6d2010ae 1930 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
1931 goto done;
1932 }
6d2010ae 1933 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
1934
1935 /* timeouted with IFF_{RUNNING,UP} check */
6d2010ae 1936 DAD_LOCK(dp);
1c79356b 1937 if (dp->dad_ns_tcount > dad_maxtry) {
6d2010ae 1938 DAD_UNLOCK(dp);
cb323159
A
1939 nd6log0(info, "%s: could not run DAD, driver problem?\n",
1940 if_name(ifa->ifa_ifp));
1c79356b 1941
6d2010ae 1942 nd6_dad_detach(dp, ifa);
1c79356b
A
1943 goto done;
1944 }
1945
1946 /* Need more checks? */
1947 if (dp->dad_ns_ocount < dp->dad_count) {
6d2010ae 1948 DAD_UNLOCK(dp);
1c79356b
A
1949 /*
1950 * We have more NS to go. Send NS packet for DAD.
1951 */
1952 nd6_dad_ns_output(dp, ifa);
316670eb
A
1953 ndi = ND_IFINFO(ifa->ifa_ifp);
1954 VERIFY(ndi != NULL && ndi->initialized);
1955 lck_mtx_lock(&ndi->lock);
1956 retrans = ndi->retrans * hz / 1000;
1957 lck_mtx_unlock(&ndi->lock);
b0d623f7 1958 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1c79356b
A
1959 } else {
1960 /*
1961 * We have transmitted sufficient number of DAD packets.
1962 * See what we've got.
1963 */
39037602 1964 if (dp->dad_na_icount > 0 || dp->dad_ns_icount) {
0a7de745 1965 /* We've seen NS or NA, means DAD has failed. */
39037602 1966 DAD_UNLOCK(dp);
cb323159
A
1967 nd6log0(info,
1968 "%s: duplicate IPv6 address %s if:%s [timer]\n",
39236c6e 1969 __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 1970 if_name(ia->ia_ifp));
39236c6e 1971 nd6_dad_duplicated(ifa);
1c79356b 1972 /* (*dp) will be freed in nd6_dad_duplicated() */
39037602
A
1973 } else if (dad_enhanced != 0 &&
1974 dp->dad_ns_lcount > 0 &&
1975 dp->dad_ns_lcount > dp->dad_loopbackprobe) {
1976 dp->dad_loopbackprobe = dp->dad_ns_lcount;
1977 dp->dad_count =
0a7de745 1978 dp->dad_ns_ocount + dad_maxtry - 1;
39037602
A
1979 DAD_UNLOCK(dp);
1980 ndi = ND_IFINFO(ifa->ifa_ifp);
1981 VERIFY(ndi != NULL && ndi->initialized);
1982 lck_mtx_lock(&ndi->lock);
1983 retrans = ndi->retrans * hz / 1000;
1984 lck_mtx_unlock(&ndi->lock);
1985
1986 /*
0a7de745
A
1987 * Sec. 4.1 in RFC 7527 requires transmission of
1988 * additional probes until the loopback condition
1989 * becomes clear when a looped back probe is detected.
1990 */
cb323159
A
1991 nd6log0(info,
1992 "%s: a looped back NS message is detected during DAD for %s. Another DAD probe is being sent on interface %s.\n",
39037602 1993 __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 1994 if_name(ia->ia_ifp));
39037602
A
1995 /*
1996 * Send an NS immediately and increase dad_count by
1997 * nd6_mmaxtries - 1.
1998 */
1999 nd6_dad_ns_output(dp, ifa);
2000 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
2001 goto done;
1c79356b 2002 } else {
fe8ab488 2003 boolean_t txunsolna;
39037602 2004 DAD_UNLOCK(dp);
1c79356b
A
2005 /*
2006 * We are done with DAD. No NA came, no NS came.
6d2010ae 2007 * No duplicate address found.
1c79356b 2008 */
6d2010ae 2009 IFA_LOCK_SPIN(&ia->ia_ifa);
316670eb 2010 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
6d2010ae 2011 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 2012
fe8ab488
A
2013 ndi = ND_IFINFO(ifa->ifa_ifp);
2014 VERIFY(ndi != NULL && ndi->initialized);
2015 lck_mtx_lock(&ndi->lock);
2016 txunsolna = (ndi->flags & ND6_IFF_REPLICATED) != 0;
2017 lck_mtx_unlock(&ndi->lock);
fe8ab488
A
2018
2019 if (txunsolna) {
2020 nd6_unsol_na_output(ifa);
2021 }
2022
cb323159
A
2023 nd6log0(debug,
2024 "%s: DAD complete for %s - no duplicates found %s\n",
1c79356b 2025 if_name(ifa->ifa_ifp),
fe8ab488 2026 ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 2027 txunsolna ? ", tx unsolicited NA with O=1" : ".");
39037602 2028
0a7de745 2029 if (dp->dad_ns_lcount > 0) {
cb323159 2030 nd6log0(debug,
39037602
A
2031 "%s: DAD completed while "
2032 "a looped back NS message is detected "
2033 "during DAD for %s om interface %s\n",
2034 __func__,
2035 ip6_sprintf(&ia->ia_addr.sin6_addr),
cb323159 2036 if_name(ia->ia_ifp));
0a7de745 2037 }
39037602
A
2038
2039 in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia,
2040 dp->dad_lladdr);
6d2010ae 2041 nd6_dad_detach(dp, ifa);
1c79356b
A
2042 }
2043 }
2044
2045done:
0a7de745
A
2046 if (dp != NULL) {
2047 DAD_REMREF(dp); /* drop our reference */
2048 }
1c79356b
A
2049}
2050
f427ee49 2051static void
39236c6e 2052nd6_dad_duplicated(struct ifaddr *ifa)
1c79356b
A
2053{
2054 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
2055 struct dadq *dp;
6d2010ae 2056 struct ifnet *ifp = ifa->ifa_ifp;
39037602 2057 boolean_t candisable;
1c79356b 2058
39037602 2059 dp = nd6_dad_find(ifa, NULL);
1c79356b 2060 if (dp == NULL) {
39236c6e 2061 log(LOG_ERR, "%s: DAD structure not found.\n", __func__);
1c79356b
A
2062 return;
2063 }
6d2010ae
A
2064 IFA_LOCK(&ia->ia_ifa);
2065 DAD_LOCK(dp);
cb323159 2066 nd6log(error, "%s: NS in/out/loopback=%d/%d/%d, NA in=%d\n",
39037602 2067 __func__, dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
cb323159 2068 dp->dad_na_icount);
39037602
A
2069 candisable = FALSE;
2070
2071 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
2072 !(ia->ia6_flags & IN6_IFF_SECURED)) {
2073 struct in6_addr in6;
2074 struct ifaddr *llifa = NULL;
2075 struct sockaddr_dl *sdl = NULL;
2076 uint8_t *lladdr = dp->dad_lladdr;
2077 uint8_t lladdrlen = dp->dad_lladdrlen;
2078
2079 /*
2080 * To avoid over-reaction, we only apply this logic when we are
2081 * very sure that hardware addresses are supposed to be unique.
2082 */
2083 switch (ifp->if_type) {
0a7de745
A
2084 case IFT_BRIDGE:
2085 case IFT_ETHER:
2086 case IFT_FDDI:
2087 case IFT_ATM:
2088 case IFT_IEEE1394:
39037602 2089#ifdef IFT_IEEE80211
0a7de745 2090 case IFT_IEEE80211:
39037602 2091#endif
0a7de745
A
2092 /*
2093 * Check if our hardware address matches the
2094 * link layer information received in the
2095 * NS/NA
2096 */
2097 llifa = ifp->if_lladdr;
2098 IFA_LOCK(llifa);
2099 sdl = (struct sockaddr_dl *)(void *)
2100 llifa->ifa_addr;
2101 if (lladdrlen == sdl->sdl_alen &&
2102 bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) {
2103 candisable = TRUE;
2104 }
2105 IFA_UNLOCK(llifa);
2106
2107 in6 = ia->ia_addr.sin6_addr;
2108 if (in6_iid_from_hw(ifp, &in6) != 0) {
2109 break;
2110 }
2111
2112 /* Refine decision about whether IPv6 can be disabled */
2113 if (candisable &&
2114 !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
39037602 2115 /*
0a7de745
A
2116 * Apply this logic only to the embedded MAC
2117 * address form of link-local IPv6 address.
39037602 2118 */
0a7de745
A
2119 candisable = FALSE;
2120 } else if (lladdr == NULL &&
2121 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
2122 /*
2123 * We received a NA with no target link-layer
2124 * address option. This means that someone else
2125 * has our address. Mark it as a hardware
2126 * duplicate so we disable IPv6 later on.
2127 */
2128 candisable = TRUE;
2129 }
2130 break;
2131 default:
2132 break;
39037602
A
2133 }
2134 }
6d2010ae 2135 DAD_UNLOCK(dp);
39037602 2136
316670eb 2137 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
1c79356b 2138 ia->ia6_flags |= IN6_IFF_DUPLICATED;
5ba3f43e
A
2139 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DUPLICATED,
2140 ia->ia_ifa.ifa_ifp, &ia->ia_addr.sin6_addr,
2141 0);
6d2010ae 2142 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 2143
39236c6e
A
2144 /* increment DAD collision counter */
2145 ++ip6stat.ip6s_dad_collide;
2146
1c79356b 2147 /* We are done with DAD, with duplicated address found. (failure) */
91447636 2148 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c 2149
6d2010ae 2150 IFA_LOCK(&ia->ia_ifa);
39236c6e 2151 log(LOG_ERR, "%s: DAD complete for %s - duplicate found.\n",
6d2010ae 2152 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
6d2010ae 2153 IFA_UNLOCK(&ia->ia_ifa);
39236c6e 2154
39037602 2155 if (candisable) {
3e170ce0 2156 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
6d2010ae 2157 log(LOG_ERR, "%s: possible hardware address duplication "
39236c6e
A
2158 "detected, disabling IPv6 for interface.\n", if_name(ifp));
2159
3e170ce0
A
2160 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2161 ndi->flags |= ND6_IFF_IFDISABLED;
39236c6e
A
2162 /* Make sure to set IFEF_IPV6_DISABLED too */
2163 nd6_if_disable(ifp, TRUE);
6d2010ae 2164 }
316670eb 2165
f427ee49
A
2166 log(LOG_ERR,
2167 "%s: manual intervention may be required.\n",
2168 if_name(ifp));
39236c6e 2169
6d2010ae
A
2170 /* Send an event to the configuration agent so that the
2171 * duplicate address will be notified to the user and will
2172 * be removed.
2173 */
39037602 2174 in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia, dp->dad_lladdr);
6d2010ae 2175 nd6_dad_detach(dp, ifa);
0a7de745 2176 DAD_REMREF(dp); /* drop our reference */
1c79356b
A
2177}
2178
2179static void
6d2010ae 2180nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1c79356b
A
2181{
2182 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
2183 struct ifnet *ifp = ifa->ifa_ifp;
39037602 2184 int i = 0;
6d2010ae 2185 struct in6_addr taddr6;
1c79356b 2186
6d2010ae 2187 DAD_LOCK(dp);
1c79356b
A
2188 dp->dad_ns_tcount++;
2189 if ((ifp->if_flags & IFF_UP) == 0) {
6d2010ae 2190 DAD_UNLOCK(dp);
1c79356b
A
2191 return;
2192 }
2193 if ((ifp->if_flags & IFF_RUNNING) == 0) {
6d2010ae 2194 DAD_UNLOCK(dp);
1c79356b
A
2195 return;
2196 }
2197
2198 dp->dad_ns_ocount++;
6d2010ae
A
2199 DAD_UNLOCK(dp);
2200 IFA_LOCK_SPIN(&ia->ia_ifa);
2201 taddr6 = ia->ia_addr.sin6_addr;
2202 IFA_UNLOCK(&ia->ia_ifa);
39037602 2203 if (dad_enhanced != 0 && !(ifp->if_flags & IFF_POINTOPOINT)) {
0a7de745 2204 for (i = 0; i < ND_OPT_NONCE_LEN32; i++) {
39037602 2205 dp->dad_nonce[i] = RandomULong();
0a7de745 2206 }
39037602
A
2207 /*
2208 * XXXHRS: Note that in the case that
2209 * DupAddrDetectTransmits > 1, multiple NS messages with
2210 * different nonces can be looped back in an unexpected
2211 * order. The current implementation recognizes only
2212 * the latest nonce on the sender side. Practically it
2213 * should work well in almost all cases.
2214 */
2215 }
2216 nd6_ns_output(ifp, NULL, &taddr6, NULL,
2217 (uint8_t *)&dp->dad_nonce[0]);
1c79356b
A
2218}
2219
39037602
A
2220/*
2221 * @brief Called to process DAD NS
2222 *
2223 * @param ifa is the pointer to the interface's address
2224 * @param lladdr is source link layer information
2225 * @param lladdrlen is source's linklayer length
2226 *
2227 * @return void
2228 */
1c79356b 2229static void
39037602
A
2230nd6_dad_ns_input(struct ifaddr *ifa, char *lladdr,
2231 int lladdrlen, struct nd_opt_nonce *ndopt_nonce)
1c79356b 2232{
1c79356b 2233 struct dadq *dp;
39236c6e 2234 VERIFY(ifa != NULL);
1c79356b 2235
39037602 2236 /* Ignore Nonce option when Enhanced DAD is disabled. */
0a7de745 2237 if (dad_enhanced == 0) {
39037602 2238 ndopt_nonce = NULL;
0a7de745 2239 }
39236c6e 2240
39037602 2241 dp = nd6_dad_find(ifa, ndopt_nonce);
0a7de745 2242 if (dp == NULL) {
39037602 2243 return;
0a7de745 2244 }
39236c6e 2245
39037602
A
2246 DAD_LOCK(dp);
2247 ++dp->dad_ns_icount;
2248 if (lladdr && lladdrlen >= ETHER_ADDR_LEN) {
2249 memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
f427ee49
A
2250 /* fine to truncate as it is compared against sdl_alen */
2251 dp->dad_lladdrlen = (uint8_t)lladdrlen;
1c79356b 2252 }
39037602
A
2253 DAD_UNLOCK(dp);
2254 DAD_REMREF(dp);
1c79356b
A
2255}
2256
39037602
A
2257/*
2258 * @brief Called to process received NA for DAD
2259 *
2260 * @param m is the pointer to the packet's mbuf
2261 * @param ifp is the pointer to the interface on which packet
0a7de745 2262 * was receicved.
39037602
A
2263 * @param taddr is pointer to target's IPv6 address
2264 * @param lladdr is target's link layer information
2265 * @param lladdrlen is target's linklayer length
2266 *
2267 * @return NULL if the packet is consumed by DAD processing, else
0a7de745 2268 * pointer to the mbuf.
39037602 2269 */
39236c6e
A
2270static struct mbuf *
2271nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr,
2272 caddr_t lladdr, int lladdrlen)
1c79356b 2273{
3e170ce0
A
2274 struct ifaddr *ifa = NULL;
2275 struct in6_ifaddr *ia = NULL;
2276 struct dadq *dp = NULL;
2277 struct nd_ifinfo *ndi = NULL;
39037602 2278 boolean_t replicated;
1c79356b 2279
39236c6e 2280 ifa = (struct ifaddr *) in6ifa_ifpwithaddr(ifp, taddr);
0a7de745 2281 if (ifa == NULL) {
39236c6e 2282 return m;
0a7de745 2283 }
1c79356b 2284
fe8ab488 2285 replicated = FALSE;
39236c6e 2286
fe8ab488 2287 /* Get the ND6_IFF_REPLICATED flag. */
39236c6e
A
2288 ndi = ND_IFINFO(ifp);
2289 if (ndi != NULL && ndi->initialized) {
2290 lck_mtx_lock(&ndi->lock);
fe8ab488 2291 replicated = !!(ndi->flags & ND6_IFF_REPLICATED);
39236c6e 2292 lck_mtx_unlock(&ndi->lock);
6d2010ae 2293 }
3e170ce0 2294
fe8ab488 2295 if (replicated) {
cb323159
A
2296 nd6log(info, "%s: ignoring duplicate NA on "
2297 "replicated interface %s\n", __func__, if_name(ifp));
39236c6e
A
2298 goto done;
2299 }
2300
2301 /* Lock the interface address until done (see label below). */
2302 IFA_LOCK(ifa);
2303 ia = (struct in6_ifaddr *) ifa;
2304
39236c6e 2305 if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
6d2010ae 2306 IFA_UNLOCK(ifa);
cb323159 2307 nd6log(info, "%s: ignoring duplicate NA on "
39236c6e 2308 "%s [DAD not in progress]\n", __func__,
cb323159 2309 if_name(ifp));
39236c6e
A
2310 goto done;
2311 }
2312
2313 /* Some sleep proxies improperly send the client's Ethernet address in
2314 * the target link-layer address option, so detect this by comparing
2315 * the L2-header source address, if we have seen it, with the target
2316 * address, and ignoring the NA if they don't match.
2317 */
fe8ab488 2318 if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
39236c6e
A
2319 struct ip6aux *ip6a = ip6_findaux(m);
2320 if (ip6a && (ip6a->ip6a_flags & IP6A_HASEEN) != 0 &&
0a7de745 2321 bcmp(ip6a->ip6a_ehsrc, lladdr, ETHER_ADDR_LEN) != 0) {
39236c6e 2322 IFA_UNLOCK(ifa);
cb323159
A
2323 nd6log(error, "%s: ignoring duplicate NA on %s "
2324 "[eh_src != tgtlladdr]\n", __func__, if_name(ifp));
39236c6e
A
2325 goto done;
2326 }
2327 }
2328
2329 IFA_UNLOCK(ifa);
2330
39037602 2331 dp = nd6_dad_find(ifa, NULL);
39236c6e 2332 if (dp == NULL) {
cb323159
A
2333 nd6log(info, "%s: no DAD structure for %s on %s.\n",
2334 __func__, ip6_sprintf(taddr), if_name(ifp));
39236c6e
A
2335 goto done;
2336 }
2337
6d2010ae 2338 DAD_LOCK_SPIN(dp);
39037602
A
2339 if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
2340 memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
f427ee49 2341 dp->dad_lladdrlen = (uint8_t)lladdrlen;
39037602 2342 }
6d2010ae 2343 dp->dad_na_icount++;
6d2010ae
A
2344 DAD_UNLOCK(dp);
2345 DAD_REMREF(dp);
39236c6e 2346
1c79356b 2347 /* remove the address. */
cb323159 2348 nd6log(info,
39236c6e 2349 "%s: duplicate IPv6 address %s [processing NA on %s]\n", __func__,
cb323159 2350 ip6_sprintf(taddr), if_name(ifp));
39236c6e
A
2351done:
2352 IFA_LOCK_ASSERT_NOTHELD(ifa);
2353 IFA_REMREF(ifa);
2354 m_freem(m);
2355 return NULL;
6d2010ae
A
2356}
2357
2358static void
2359dad_addref(struct dadq *dp, int locked)
2360{
0a7de745 2361 if (!locked) {
6d2010ae 2362 DAD_LOCK_SPIN(dp);
0a7de745 2363 } else {
6d2010ae 2364 DAD_LOCK_ASSERT_HELD(dp);
0a7de745 2365 }
6d2010ae
A
2366
2367 if (++dp->dad_refcount == 0) {
2368 panic("%s: dad %p wraparound refcnt\n", __func__, dp);
2369 /* NOTREACHED */
2370 }
0a7de745 2371 if (!locked) {
6d2010ae 2372 DAD_UNLOCK(dp);
0a7de745 2373 }
6d2010ae
A
2374}
2375
2376static void
2377dad_remref(struct dadq *dp)
2378{
2379 struct ifaddr *ifa;
2380
2381 DAD_LOCK_SPIN(dp);
0a7de745 2382 if (dp->dad_refcount == 0) {
6d2010ae 2383 panic("%s: dad %p negative refcnt\n", __func__, dp);
0a7de745 2384 }
6d2010ae
A
2385 --dp->dad_refcount;
2386 if (dp->dad_refcount > 0) {
2387 DAD_UNLOCK(dp);
2388 return;
2389 }
2390 DAD_UNLOCK(dp);
2391
2392 if (dp->dad_attached ||
2393 dp->dad_list.tqe_next != NULL || dp->dad_list.tqe_prev != NULL) {
2394 panic("%s: attached dad=%p is being freed", __func__, dp);
2395 /* NOTREACHED */
2396 }
2397
2398 if ((ifa = dp->dad_ifa) != NULL) {
0a7de745 2399 IFA_REMREF(ifa); /* drop dad_ifa reference */
6d2010ae
A
2400 dp->dad_ifa = NULL;
2401 }
2402
2403 lck_mtx_destroy(&dp->dad_lock, ifa_mtx_grp);
2404 zfree(dad_zone, dp);
2405}
2406
2407void
2408nd6_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen)
2409{
2410 /* Nothing more to do if it's disabled */
0a7de745 2411 if (nd6_llreach_base == 0) {
6d2010ae 2412 return;
0a7de745 2413 }
6d2010ae
A
2414
2415 ifnet_llreach_set_reachable(ifp, ETHERTYPE_IPV6, addr, alen);
1c79356b 2416}
316670eb
A
2417
2418void
2419nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa,
2420 struct sockaddr_dl* sdl, struct sockaddr_in6 *sin6)
2421{
2422 static const size_t EUI64_LENGTH = 8;
39236c6e 2423
316670eb
A
2424 VERIFY(nd6_need_cache(ifp));
2425 VERIFY(sa);
2426 VERIFY(sdl && (void *)sa != (void *)sdl);
2427 VERIFY(sin6 && (void *)sa != (void *)sin6);
39236c6e 2428
cb323159 2429 bzero(sin6, sizeof(*sin6));
316670eb
A
2430 sin6->sin6_len = sizeof *sin6;
2431 sin6->sin6_family = AF_INET6;
39236c6e 2432
cb323159 2433 bzero(sdl, sizeof(*sdl));
316670eb
A
2434 sdl->sdl_len = sizeof *sdl;
2435 sdl->sdl_family = AF_LINK;
2436 sdl->sdl_type = ifp->if_type;
2437 sdl->sdl_index = ifp->if_index;
39236c6e 2438
316670eb
A
2439 switch (sa->sa_family) {
2440 case AF_INET6: {
2441 struct sockaddr_in6 *sin6a = (struct sockaddr_in6 *)(void *)sa;
2442 struct in6_addr *in6 = &sin6a->sin6_addr;
39236c6e 2443
316670eb 2444 VERIFY(sa->sa_len == sizeof *sin6);
f427ee49 2445 VERIFY(strlen(ifp->if_name) <= IFNAMSIZ);
39236c6e 2446
f427ee49 2447 sdl->sdl_nlen = (u_char)strlen(ifp->if_name);
316670eb
A
2448 bcopy(ifp->if_name, sdl->sdl_data, sdl->sdl_nlen);
2449 if (in6->s6_addr[11] == 0xff && in6->s6_addr[12] == 0xfe) {
2450 sdl->sdl_alen = ETHER_ADDR_LEN;
2451 LLADDR(sdl)[0] = (in6->s6_addr[8] ^ ND6_EUI64_UBIT);
2452 LLADDR(sdl)[1] = in6->s6_addr[9];
2453 LLADDR(sdl)[2] = in6->s6_addr[10];
2454 LLADDR(sdl)[3] = in6->s6_addr[13];
2455 LLADDR(sdl)[4] = in6->s6_addr[14];
2456 LLADDR(sdl)[5] = in6->s6_addr[15];
39236c6e 2457 } else {
316670eb
A
2458 sdl->sdl_alen = EUI64_LENGTH;
2459 bcopy(&in6->s6_addr[8], LLADDR(sdl), EUI64_LENGTH);
2460 }
39236c6e 2461
316670eb
A
2462 sdl->sdl_slen = 0;
2463 break;
2464 }
2465 case AF_LINK: {
2466 struct sockaddr_dl *sdla = (struct sockaddr_dl *)(void *)sa;
2467 struct in6_addr *in6 = &sin6->sin6_addr;
2468 caddr_t lla = LLADDR(sdla);
39236c6e 2469
cb323159 2470 VERIFY(sa->sa_len <= sizeof(*sdl));
316670eb 2471 bcopy(sa, sdl, sa->sa_len);
39236c6e 2472
316670eb 2473 sin6->sin6_scope_id = sdla->sdl_index;
0a7de745 2474 if (sin6->sin6_scope_id == 0) {
316670eb 2475 sin6->sin6_scope_id = ifp->if_index;
0a7de745 2476 }
316670eb
A
2477 in6->s6_addr[0] = 0xfe;
2478 in6->s6_addr[1] = 0x80;
0a7de745 2479 if (sdla->sdl_alen == EUI64_LENGTH) {
316670eb 2480 bcopy(lla, &in6->s6_addr[8], EUI64_LENGTH);
0a7de745 2481 } else {
316670eb 2482 VERIFY(sdla->sdl_alen == ETHER_ADDR_LEN);
39236c6e 2483
316670eb
A
2484 in6->s6_addr[8] = ((uint8_t) lla[0] ^ ND6_EUI64_UBIT);
2485 in6->s6_addr[9] = (uint8_t) lla[1];
2486 in6->s6_addr[10] = (uint8_t) lla[2];
2487 in6->s6_addr[11] = 0xff;
2488 in6->s6_addr[12] = 0xfe;
2489 in6->s6_addr[13] = (uint8_t) lla[3];
2490 in6->s6_addr[14] = (uint8_t) lla[4];
2491 in6->s6_addr[15] = (uint8_t) lla[5];
2492 }
39236c6e 2493
316670eb
A
2494 break;
2495 }
2496 default:
2497 VERIFY(false);
2498 break;
2499 }
2500}
2501
cb323159 2502int
316670eb
A
2503nd6_alt_node_present(struct ifnet *ifp, struct sockaddr_in6 *sin6,
2504 struct sockaddr_dl *sdl, int32_t rssi, int lqm, int npm)
2505{
2506 struct rtentry *rt;
2507 struct llinfo_nd6 *ln;
0a7de745 2508 struct if_llreach *lr = NULL;
39037602
A
2509 const uint16_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
2510
2511 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) &&
0a7de745 2512 (temp_embedded_id == 0)) {
39037602 2513 sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
0a7de745 2514 }
316670eb 2515
39236c6e
A
2516 nd6_cache_lladdr(ifp, &sin6->sin6_addr, LLADDR(sdl), sdl->sdl_alen,
2517 ND_NEIGHBOR_ADVERT, 0);
316670eb 2518
5ba3f43e 2519 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
316670eb
A
2520 lck_mtx_lock(rnh_lock);
2521
2522 rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 1, 0,
2523 ifp->if_index);
39037602
A
2524
2525 /* Restore the address that was passed to us */
0a7de745 2526 if (temp_embedded_id == 0) {
39037602 2527 sin6->sin6_addr.s6_addr16[1] = 0;
0a7de745 2528 }
39037602 2529
316670eb
A
2530 if (rt != NULL) {
2531 RT_LOCK(rt);
2532 VERIFY(rt->rt_flags & RTF_LLINFO);
2533 VERIFY(rt->rt_llinfo);
2534
2535 ln = rt->rt_llinfo;
39037602 2536 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
39236c6e 2537 ln_setexpire(ln, 0);
316670eb
A
2538
2539 lr = ln->ln_llreach;
2540 if (lr) {
2541 IFLR_LOCK(lr);
2542 lr->lr_rssi = rssi;
2543 lr->lr_lqm = (int32_t) lqm;
2544 lr->lr_npm = (int32_t) npm;
2545 IFLR_UNLOCK(lr);
2546 }
2547
2548 RT_UNLOCK(rt);
2549 RT_REMREF(rt);
2550 }
2551
2552 lck_mtx_unlock(rnh_lock);
2553
2554 if (rt == NULL) {
2555 log(LOG_ERR, "%s: failed to add/update host route to %s.\n",
2556 __func__, ip6_sprintf(&sin6->sin6_addr));
cb323159 2557 return EHOSTUNREACH;
39236c6e 2558 } else {
cb323159 2559 nd6log(debug, "%s: host route to %s [lr=0x%llx]\n",
39236c6e 2560 __func__, ip6_sprintf(&sin6->sin6_addr),
cb323159
A
2561 (uint64_t)VM_KERNEL_ADDRPERM(lr));
2562 return 0;
db609669 2563 }
316670eb
A
2564}
2565
2566void
cb323159 2567nd6_alt_node_absent(struct ifnet *ifp, struct sockaddr_in6 *sin6, struct sockaddr_dl *sdl)
316670eb
A
2568{
2569 struct rtentry *rt;
39037602 2570 const uint16_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
316670eb 2571
cb323159
A
2572 nd6log(debug, "%s: host route to %s\n", __func__,
2573 ip6_sprintf(&sin6->sin6_addr));
db609669 2574
39037602 2575 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) &&
0a7de745 2576 (temp_embedded_id == 0)) {
39037602 2577 sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
0a7de745 2578 }
39037602 2579
5ba3f43e 2580 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
316670eb
A
2581 lck_mtx_lock(rnh_lock);
2582
2583 rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0, 0,
2584 ifp->if_index);
39037602 2585
0a7de745
A
2586 /* Restore the address that was passed to us */
2587 if (temp_embedded_id == 0) {
39037602 2588 sin6->sin6_addr.s6_addr16[1] = 0;
0a7de745 2589 }
39037602 2590
316670eb
A
2591 if (rt != NULL) {
2592 RT_LOCK(rt);
2593
0a7de745
A
2594 if (!(rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
2595 (rt->rt_flags & (RTF_HOST | RTF_LLINFO | RTF_WASCLONED)) ==
2596 (RTF_HOST | RTF_LLINFO | RTF_WASCLONED)) {
cb323159
A
2597 /*
2598 * Copy the link layer information in SDL when present
2599 * as it later gets used to issue the kernel event for
2600 * node absence.
2601 */
2602 if (sdl != NULL && rt->rt_gateway != NULL &&
2603 rt->rt_gateway->sa_family == AF_LINK &&
2604 SDL(rt->rt_gateway)->sdl_len <= sizeof(*sdl)) {
2605 bcopy(rt->rt_gateway, sdl, SDL(rt->rt_gateway)->sdl_len);
2606 }
2607
316670eb
A
2608 rt->rt_flags |= RTF_CONDEMNED;
2609 RT_UNLOCK(rt);
2610
2611 (void) rtrequest_locked(RTM_DELETE, rt_key(rt),
2612 (struct sockaddr *)NULL, rt_mask(rt), 0,
2613 (struct rtentry **)NULL);
2614
2615 rtfree_locked(rt);
39236c6e 2616 } else {
316670eb
A
2617 RT_REMREF_LOCKED(rt);
2618 RT_UNLOCK(rt);
2619 }
2620 }
2621
2622 lck_mtx_unlock(rnh_lock);
2623}