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