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