1 /*
2 * Copyright (c) 2003-2023 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 /*
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)in.c 8.2 (Berkeley) 11/15/93
91 */
92
93
94 #include <sys/param.h>
95 #include <sys/ioctl.h>
96 #include <sys/errno.h>
97 #include <sys/malloc.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/sockio.h>
101 #include <sys/systm.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/kern_event.h>
106 #include <sys/mcache.h>
107 #include <sys/protosw.h>
108
109 #include <kern/locks.h>
110 #include <kern/zalloc.h>
111 #include <libkern/OSAtomic.h>
112 #include <machine/machine_routines.h>
113 #include <mach/boolean.h>
114
115 #include <net/if.h>
116 #include <net/if_types.h>
117 #include <net/if_var.h>
118 #include <net/route.h>
119 #include <net/if_dl.h>
120 #include <net/kpi_protocol.h>
121 #include <net/nwk_wq.h>
122
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 #include <netinet/if_ether.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/icmp6.h>
130 #include <netinet/tcp.h>
131 #include <netinet/tcp_seq.h>
132 #include <netinet/tcp_var.h>
133
134 #include <netinet6/nd6.h>
135 #include <netinet/ip6.h>
136 #include <netinet6/ip6_var.h>
137 #include <netinet6/mld6_var.h>
138 #include <netinet6/in6_ifattach.h>
139 #include <netinet6/scope6_var.h>
140 #include <netinet6/in6_var.h>
141 #include <netinet6/in6_pcb.h>
142
143 #include <net/net_osdep.h>
144
145 #include <net/dlil.h>
146
147 #if PF
148 #include <net/pfvar.h>
149 #endif /* PF */
150
151 #include <net/sockaddr_utils.h>
152
153 /*
154 * Definitions of some constant IP6 addresses.
155 */
156 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
157 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
158 const struct in6_addr in6addr_nodelocal_allnodes =
159 IN6ADDR_NODELOCAL_ALLNODES_INIT;
160 const struct in6_addr in6addr_linklocal_allnodes =
161 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
162 const struct in6_addr in6addr_linklocal_allrouters =
163 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
164 const struct in6_addr in6addr_linklocal_allv2routers =
165 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
166 const struct in6_addr in6addr_multicast_prefix =
167 IN6ADDR_MULTICAST_PREFIX;
168
169 const struct in6_addr in6mask0 = IN6MASK0;
170 const struct in6_addr in6mask7 = IN6MASK7;
171 const struct in6_addr in6mask8 = IN6MASK8;
172 const struct in6_addr in6mask16 = IN6MASK16;
173 const struct in6_addr in6mask32 = IN6MASK32;
174 const struct in6_addr in6mask64 = IN6MASK64;
175 const struct in6_addr in6mask96 = IN6MASK96;
176 const struct in6_addr in6mask128 = IN6MASK128;
177
178 const struct sockaddr_in6 sa6_any = {
179 .sin6_len = sizeof(sa6_any),
180 .sin6_family = AF_INET6,
181 .sin6_port = 0,
182 .sin6_flowinfo = 0,
183 .sin6_addr = IN6ADDR_ANY_INIT,
184 .sin6_scope_id = 0
185 };
186
187 static int in6ctl_associd(struct socket *, u_long, caddr_t __indexable);
188 static int in6ctl_connid(struct socket *, u_long, caddr_t __indexable);
189 static int in6ctl_conninfo(struct socket *, u_long, caddr_t __indexable);
190 static int in6ctl_llstart(struct ifnet *, u_long, caddr_t __indexable);
191 static int in6ctl_llstop(struct ifnet *);
192 static int in6ctl_cgastart(struct ifnet *, u_long, caddr_t __indexable);
193 static int in6ctl_gifaddr(struct ifnet *, struct in6_ifaddr *, u_long,
194 struct in6_ifreq *);
195 static int in6ctl_gifstat(struct ifnet *, u_long, struct in6_ifreq *);
196 static int in6ctl_alifetime(struct in6_ifaddr *, u_long, struct in6_ifreq *,
197 boolean_t);
198 static int in6ctl_aifaddr(struct ifnet *, struct in6_aliasreq *);
199 static void in6ctl_difaddr(struct ifnet *, struct in6_ifaddr *);
200 static int in6_autoconf(struct ifnet *, int);
201 static int in6_setrouter(struct ifnet *, ipv6_router_mode_t);
202 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *, int);
203 static int in6_ifaupdate_aux(struct in6_ifaddr *, struct ifnet *, int);
204 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
205 static struct in6_ifaddr *in6_ifaddr_alloc(zalloc_flags_t);
206 static void in6_ifaddr_free(struct ifaddr *);
207 #if defined(__LP64__)
208 static void in6_cgareq_32_to_64(const struct in6_cgareq_32 *,
209 struct in6_cgareq_64 *);
210 #else
211 static void in6_cgareq_64_to_32(const struct in6_cgareq_64 *,
212 struct in6_cgareq_32 *);
213 #endif
214 static struct in6_aliasreq *in6_aliasreq_to_native(void *__indexable, int,
215 struct in6_aliasreq *);
216 static int in6_to_kamescope(struct sockaddr_in6 *, struct ifnet *);
217 static int in6_getassocids(struct socket *, uint32_t *, user_addr_t);
218 static int in6_getconnids(struct socket *, sae_associd_t, uint32_t *,
219 user_addr_t);
220
221 static void in6_if_up_dad_start(struct ifnet *);
222
223 #define IA6_HASH_INIT(ia) { \
224 (ia)->ia6_hash.tqe_next = __unsafe_forge_single(void *, ~(uintptr_t)0); \
225 (ia)->ia6_hash.tqe_prev = __unsafe_forge_single(void *, ~(uintptr_t)0); \
226 }
227
228 #define IA6_IS_HASHED(ia) \
229 (!((ia)->ia6_hash.tqe_next == __unsafe_forge_single(void *, ~(uintptr_t)0) || \
230 (ia)->ia6_hash.tqe_prev == __unsafe_forge_single(void *, ~(uintptr_t)0)))
231
232 static void in6_iahash_remove(struct in6_ifaddr *);
233 static void in6_iahash_insert(struct in6_ifaddr *);
234 static void in6_iahash_insert_ptp(struct in6_ifaddr *);
235
236 struct eventhandler_lists_ctxt in6_evhdlr_ctxt;
237 struct eventhandler_lists_ctxt in6_clat46_evhdlr_ctxt;
238 /*
239 * Subroutine for in6_ifaddloop() and in6_ifremloop().
240 * This routine does actual work.
241 */
242 static void
in6_ifloop_request(int cmd,struct ifaddr * ifa)243 in6_ifloop_request(int cmd, struct ifaddr *ifa)
244 {
245 struct sockaddr_in6 all1_sa;
246 rtentry_ref_t nrt = NULL;
247 int e;
248
249 SOCKADDR_ZERO(&all1_sa, sizeof(all1_sa));
250 all1_sa.sin6_family = AF_INET6;
251 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
252 all1_sa.sin6_addr = in6mask128;
253
254 /*
255 * We specify the address itself as the gateway, and set the
256 * RTF_LLINFO flag, so that the corresponding host route would have
257 * the flag, and thus applications that assume traditional behavior
258 * would be happy. Note that we assume the caller of the function
259 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
260 * which changes the outgoing interface to the loopback interface.
261 * ifa_addr for INET6 is set once during init; no need to hold lock.
262 */
263 lck_mtx_lock(rnh_lock);
264 e = rtrequest_locked(cmd, ifa->ifa_addr, ifa->ifa_addr,
265 SA(&all1_sa), RTF_UP | RTF_HOST | RTF_LLINFO, &nrt);
266 if (e != 0) {
267 log(LOG_ERR, "in6_ifloop_request: "
268 "%s operation failed for %s (errno=%d)\n",
269 cmd == RTM_ADD ? "ADD" : "DELETE",
270 ip6_sprintf(&(ifatoia6(ifa))->ia_addr.sin6_addr),
271 e);
272 }
273
274 if (nrt != NULL) {
275 RT_LOCK(nrt);
276 }
277 /*
278 * Make sure rt_ifa be equal to IFA, the second argument of the
279 * function.
280 * We need this because when we refer to rt_ifa->ia6_flags in
281 * ip6_input, we assume that the rt_ifa points to the address instead
282 * of the loopback address.
283 */
284 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
285 rtsetifa(nrt, ifa);
286 }
287
288 /*
289 * Report the addition/removal of the address to the routing socket.
290 * XXX: since we called rtinit for a p2p interface with a destination,
291 * we end up reporting twice in such a case. Should we rather
292 * omit the second report?
293 */
294 if (nrt != NULL) {
295 rt_newaddrmsg((u_char)cmd, ifa, e, nrt);
296 if (cmd == RTM_DELETE) {
297 RT_UNLOCK(nrt);
298 rtfree_locked(nrt);
299 } else {
300 /* the cmd must be RTM_ADD here */
301 RT_REMREF_LOCKED(nrt);
302 RT_UNLOCK(nrt);
303 }
304 }
305 lck_mtx_unlock(rnh_lock);
306 }
307
308 /*
309 * Add ownaddr as loopback rtentry. We previously add the route only if
310 * necessary (ex. on a p2p link). However, since we now manage addresses
311 * separately from prefixes, we should always add the route. We can't
312 * rely on the cloning mechanism from the corresponding interface route
313 * any more.
314 */
315 static void
in6_ifaddloop(struct ifaddr * ifa)316 in6_ifaddloop(struct ifaddr *ifa)
317 {
318 rtentry_ref_t rt;
319
320 /*
321 * If there is no loopback entry, allocate one. ifa_addr for
322 * INET6 is set once during init; no need to hold lock.
323 */
324 rt = rtalloc1(ifa->ifa_addr, 0, 0);
325 if (rt != NULL) {
326 RT_LOCK(rt);
327 }
328 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
329 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
330 if (rt != NULL) {
331 RT_REMREF_LOCKED(rt);
332 RT_UNLOCK(rt);
333 }
334 in6_ifloop_request(RTM_ADD, ifa);
335 } else if (rt != NULL) {
336 RT_REMREF_LOCKED(rt);
337 RT_UNLOCK(rt);
338 }
339 }
340
341 /*
342 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
343 * if it exists.
344 */
345 static void
in6_ifremloop(struct ifaddr * ifa)346 in6_ifremloop(struct ifaddr *ifa)
347 {
348 struct in6_ifaddr *__single ia;
349 rtentry_ref_t rt;
350 int ia_count = 0;
351
352 /*
353 * Some of BSD variants do not remove cloned routes
354 * from an interface direct route, when removing the direct route
355 * (see comments in net/net_osdep.h). Even for variants that do remove
356 * cloned routes, they could fail to remove the cloned routes when
357 * we handle multple addresses that share a common prefix.
358 * So, we should remove the route corresponding to the deleted address
359 * regardless of the result of in6_is_ifloop_auto().
360 */
361
362 /*
363 * Delete the entry only if exact one ifa exists. More than one ifa
364 * can exist if we assign a same single address to multiple
365 * (probably p2p) interfaces.
366 * XXX: we should avoid such a configuration in IPv6...
367 */
368 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
369 TAILQ_FOREACH(ia, IN6ADDR_HASH(IFA_IN6(ifa)), ia6_hash) {
370 IFA_LOCK(&ia->ia_ifa);
371 if (in6_are_addr_equal_scoped(IFA_IN6(ifa), &ia->ia_addr.sin6_addr, IFA_SIN6(ifa)->sin6_scope_id, ia->ia_addr.sin6_scope_id)) {
372 ia_count++;
373 if (ia_count > 1) {
374 IFA_UNLOCK(&ia->ia_ifa);
375 break;
376 }
377 }
378 IFA_UNLOCK(&ia->ia_ifa);
379 }
380 lck_rw_done(&in6_ifaddr_rwlock);
381
382 if (ia_count == 1) {
383 /*
384 * Before deleting, check if a corresponding loopbacked host
385 * route surely exists. With this check, we can avoid to
386 * delete an interface direct route whose destination is same
387 * as the address being removed. This can happen when removing
388 * a subnet-router anycast address on an interface attahced
389 * to a shared medium. ifa_addr for INET6 is set once during
390 * init; no need to hold lock.
391 */
392 rt = rtalloc1(ifa->ifa_addr, 0, 0);
393 if (rt != NULL) {
394 RT_LOCK(rt);
395 if ((rt->rt_flags & RTF_HOST) != 0 &&
396 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
397 RT_REMREF_LOCKED(rt);
398 RT_UNLOCK(rt);
399 in6_ifloop_request(RTM_DELETE, ifa);
400 } else {
401 RT_UNLOCK(rt);
402 }
403 }
404 }
405 }
406
407
408 int
409 in6_mask2len(struct in6_addr *mask, u_char *__counted_by(0) lim0)
410 {
411 int x = 0, y;
412 u_char *lim = lim0, *p;
413
414 /* ignore the scope_id part */
415 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) {
416 lim = (u_char *)(struct in6_addr *__indexable)mask + sizeof(*mask);
417 }
418 for (p = (u_char *)(struct in6_addr *__indexable)mask; p < lim; x++, p++) {
419 if (*p != 0xff) {
420 break;
421 }
422 }
423 y = 0;
424 if (p < lim) {
425 for (y = 0; y < 8; y++) {
426 if ((*p & (0x80 >> y)) == 0) {
427 break;
428 }
429 }
430 }
431
432 /*
433 * when the limit pointer is given, do a stricter check on the
434 * remaining bits.
435 */
436 if (p < lim) {
437 if (y != 0 && (*p & (0x00ff >> y)) != 0) {
438 return -1;
439 }
440 for (p = p + 1; p < lim; p++) {
441 if (*p != 0) {
442 return -1;
443 }
444 }
445 }
446
447 return x * 8 + y;
448 }
449
450 void
in6_len2mask(struct in6_addr * mask,int len)451 in6_len2mask(struct in6_addr *mask, int len)
452 {
453 int i;
454
455 bzero(mask, sizeof(*mask));
456 for (i = 0; i < len / 8; i++) {
457 mask->s6_addr8[i] = 0xff;
458 }
459 if (len % 8) {
460 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
461 }
462 }
463
464 void
in6_aliasreq_64_to_32(struct in6_aliasreq_64 * src,struct in6_aliasreq_32 * dst)465 in6_aliasreq_64_to_32(struct in6_aliasreq_64 *src, struct in6_aliasreq_32 *dst)
466 {
467 bzero(dst, sizeof(*dst));
468 bcopy(src->ifra_name, dst->ifra_name, sizeof(dst->ifra_name));
469 dst->ifra_addr = src->ifra_addr;
470 dst->ifra_dstaddr = src->ifra_dstaddr;
471 dst->ifra_prefixmask = src->ifra_prefixmask;
472 dst->ifra_flags = src->ifra_flags;
473 dst->ifra_lifetime.ia6t_expire = (u_int32_t)src->ifra_lifetime.ia6t_expire;
474 dst->ifra_lifetime.ia6t_preferred = (u_int32_t)src->ifra_lifetime.ia6t_preferred;
475 dst->ifra_lifetime.ia6t_vltime = src->ifra_lifetime.ia6t_vltime;
476 dst->ifra_lifetime.ia6t_pltime = src->ifra_lifetime.ia6t_pltime;
477 }
478
479 void
in6_aliasreq_32_to_64(struct in6_aliasreq_32 * src,struct in6_aliasreq_64 * dst)480 in6_aliasreq_32_to_64(struct in6_aliasreq_32 *src, struct in6_aliasreq_64 *dst)
481 {
482 bzero(dst, sizeof(*dst));
483 bcopy(src->ifra_name, dst->ifra_name, sizeof(dst->ifra_name));
484 dst->ifra_addr = src->ifra_addr;
485 dst->ifra_dstaddr = src->ifra_dstaddr;
486 dst->ifra_prefixmask = src->ifra_prefixmask;
487 dst->ifra_flags = src->ifra_flags;
488 dst->ifra_lifetime.ia6t_expire = src->ifra_lifetime.ia6t_expire;
489 dst->ifra_lifetime.ia6t_preferred = src->ifra_lifetime.ia6t_preferred;
490 dst->ifra_lifetime.ia6t_vltime = src->ifra_lifetime.ia6t_vltime;
491 dst->ifra_lifetime.ia6t_pltime = src->ifra_lifetime.ia6t_pltime;
492 }
493
494 #if defined(__LP64__)
495 static void
in6_cgareq_32_to_64(const struct in6_cgareq_32 * src,struct in6_cgareq_64 * dst)496 in6_cgareq_32_to_64(const struct in6_cgareq_32 *src,
497 struct in6_cgareq_64 *dst)
498 {
499 bzero(dst, sizeof(*dst));
500 bcopy(src->cgar_name, dst->cgar_name, sizeof(dst->cgar_name));
501 dst->cgar_flags = src->cgar_flags;
502 bcopy(src->cgar_cgaprep.cga_modifier.octets,
503 dst->cgar_cgaprep.cga_modifier.octets,
504 sizeof(dst->cgar_cgaprep.cga_modifier.octets));
505 dst->cgar_cgaprep.cga_security_level =
506 src->cgar_cgaprep.cga_security_level;
507 dst->cgar_lifetime.ia6t_expire = src->cgar_lifetime.ia6t_expire;
508 dst->cgar_lifetime.ia6t_preferred = src->cgar_lifetime.ia6t_preferred;
509 dst->cgar_lifetime.ia6t_vltime = src->cgar_lifetime.ia6t_vltime;
510 dst->cgar_lifetime.ia6t_pltime = src->cgar_lifetime.ia6t_pltime;
511 dst->cgar_collision_count = src->cgar_collision_count;
512 }
513 #endif
514
515 #if !defined(__LP64__)
516 static void
in6_cgareq_64_to_32(const struct in6_cgareq_64 * src,struct in6_cgareq_32 * dst)517 in6_cgareq_64_to_32(const struct in6_cgareq_64 *src,
518 struct in6_cgareq_32 *dst)
519 {
520 bzero(dst, sizeof(*dst));
521 bcopy(src->cgar_name, dst->cgar_name, sizeof(dst->cgar_name));
522 dst->cgar_flags = src->cgar_flags;
523 bcopy(src->cgar_cgaprep.cga_modifier.octets,
524 dst->cgar_cgaprep.cga_modifier.octets,
525 sizeof(dst->cgar_cgaprep.cga_modifier.octets));
526 dst->cgar_cgaprep.cga_security_level =
527 src->cgar_cgaprep.cga_security_level;
528 dst->cgar_lifetime.ia6t_expire = (u_int32_t)src->cgar_lifetime.ia6t_expire;
529 dst->cgar_lifetime.ia6t_preferred = (u_int32_t)src->cgar_lifetime.ia6t_preferred;
530 dst->cgar_lifetime.ia6t_vltime = src->cgar_lifetime.ia6t_vltime;
531 dst->cgar_lifetime.ia6t_pltime = src->cgar_lifetime.ia6t_pltime;
532 dst->cgar_collision_count = src->cgar_collision_count;
533 }
534 #endif
535
536 static struct in6_aliasreq *
in6_aliasreq_to_native(void * __indexable data,int data_is_64,struct in6_aliasreq * dst)537 in6_aliasreq_to_native(void *__indexable data, int data_is_64, struct in6_aliasreq *dst)
538 {
539 #if defined(__LP64__)
540 if (data_is_64) {
541 bcopy(data, dst, sizeof(*dst));
542 } else {
543 in6_aliasreq_32_to_64((struct in6_aliasreq_32 *)data,
544 (struct in6_aliasreq_64 *)dst);
545 }
546 #else
547 if (data_is_64) {
548 in6_aliasreq_64_to_32((struct in6_aliasreq_64 *)data,
549 (struct in6_aliasreq_32 *)dst);
550 } else {
551 bcopy(data, dst, sizeof(*dst));
552 }
553 #endif /* __LP64__ */
554 return dst;
555 }
556
557 void
in6_cgareq_copy_from_user32(const void * __sized_by (sizeof (struct in6_cgareq_32))user_data,struct in6_cgareq * cgareq)558 in6_cgareq_copy_from_user32(const void *__sized_by(sizeof(struct in6_cgareq_32))user_data, struct in6_cgareq *cgareq)
559 {
560 #if defined(__LP64__)
561 in6_cgareq_32_to_64((const struct in6_cgareq_32 *)user_data,
562 (struct in6_cgareq_64 *)cgareq);
563 #else
564 bcopy(user_data, cgareq, sizeof(*cgareq));
565 #endif /* __LP64__ */
566 }
567
568 void
in6_cgareq_copy_from_user64(const void * __sized_by (sizeof (struct in6_cgareq_64))user_data,struct in6_cgareq * cgareq)569 in6_cgareq_copy_from_user64(const void *__sized_by(sizeof(struct in6_cgareq_64))user_data, struct in6_cgareq *cgareq)
570 {
571 #if defined(__LP64__)
572 bcopy(user_data, cgareq, sizeof(*cgareq));
573 #else
574 in6_cgareq_64_to_32((const struct in6_cgareq_64 *)user_data,
575 (struct in6_cgareq_32 *)cgareq);
576 #endif /* __LP64__ */
577 }
578
579 static __attribute__((noinline)) int
in6ctl_associd(struct socket * so,u_long cmd,caddr_t __indexable data)580 in6ctl_associd(struct socket *so, u_long cmd, caddr_t __indexable data)
581 {
582 int error = 0;
583 union {
584 struct so_aidreq32 a32;
585 struct so_aidreq64 a64;
586 } u;
587
588 VERIFY(so != NULL);
589
590 switch (cmd) {
591 case SIOCGASSOCIDS32: { /* struct so_aidreq32 */
592 bcopy(data, &u.a32, sizeof(u.a32));
593 error = in6_getassocids(so, &u.a32.sar_cnt, u.a32.sar_aidp);
594 if (error == 0) {
595 bcopy(&u.a32, data, sizeof(u.a32));
596 }
597 break;
598 }
599
600 case SIOCGASSOCIDS64: { /* struct so_aidreq64 */
601 bcopy(data, &u.a64, sizeof(u.a64));
602 error = in6_getassocids(so, &u.a64.sar_cnt, (user_addr_t)u.a64.sar_aidp);
603 if (error == 0) {
604 bcopy(&u.a64, data, sizeof(u.a64));
605 }
606 break;
607 }
608
609 default:
610 VERIFY(0);
611 /* NOTREACHED */
612 }
613
614 return error;
615 }
616
617 static __attribute__((noinline)) int
in6ctl_connid(struct socket * so,u_long cmd,caddr_t __indexable data)618 in6ctl_connid(struct socket *so, u_long cmd, caddr_t __indexable data)
619 {
620 int error = 0;
621 union {
622 struct so_cidreq32 c32;
623 struct so_cidreq64 c64;
624 } u;
625
626 VERIFY(so != NULL);
627
628 switch (cmd) {
629 case SIOCGCONNIDS32: { /* struct so_cidreq32 */
630 bcopy(data, &u.c32, sizeof(u.c32));
631 error = in6_getconnids(so, u.c32.scr_aid, &u.c32.scr_cnt,
632 u.c32.scr_cidp);
633 if (error == 0) {
634 bcopy(&u.c32, data, sizeof(u.c32));
635 }
636 break;
637 }
638
639 case SIOCGCONNIDS64: { /* struct so_cidreq64 */
640 bcopy(data, &u.c64, sizeof(u.c64));
641 error = in6_getconnids(so, u.c64.scr_aid, &u.c64.scr_cnt,
642 (user_addr_t)u.c64.scr_cidp);
643 if (error == 0) {
644 bcopy(&u.c64, data, sizeof(u.c64));
645 }
646 break;
647 }
648
649 default:
650 VERIFY(0);
651 /* NOTREACHED */
652 }
653
654 return error;
655 }
656
657 static __attribute__((noinline)) int
in6ctl_conninfo(struct socket * so,u_long cmd,caddr_t __indexable data)658 in6ctl_conninfo(struct socket *so, u_long cmd, caddr_t __indexable data)
659 {
660 int error = 0;
661 union {
662 struct so_cinforeq32 ci32;
663 struct so_cinforeq64 ci64;
664 } u;
665
666 VERIFY(so != NULL);
667
668 switch (cmd) {
669 case SIOCGCONNINFO32: { /* struct so_cinforeq32 */
670 bcopy(data, &u.ci32, sizeof(u.ci32));
671 error = in6_getconninfo(so, u.ci32.scir_cid, &u.ci32.scir_flags,
672 &u.ci32.scir_ifindex, &u.ci32.scir_error, u.ci32.scir_src,
673 &u.ci32.scir_src_len, u.ci32.scir_dst, &u.ci32.scir_dst_len,
674 &u.ci32.scir_aux_type, u.ci32.scir_aux_data,
675 &u.ci32.scir_aux_len);
676 if (error == 0) {
677 bcopy(&u.ci32, data, sizeof(u.ci32));
678 }
679 break;
680 }
681
682 case SIOCGCONNINFO64: { /* struct so_cinforeq64 */
683 bcopy(data, &u.ci64, sizeof(u.ci64));
684 error = in6_getconninfo(so, u.ci64.scir_cid, &u.ci64.scir_flags,
685 &u.ci64.scir_ifindex, &u.ci64.scir_error, (user_addr_t)u.ci64.scir_src,
686 &u.ci64.scir_src_len, (user_addr_t)u.ci64.scir_dst, &u.ci64.scir_dst_len,
687 &u.ci64.scir_aux_type, (user_addr_t)u.ci64.scir_aux_data,
688 &u.ci64.scir_aux_len);
689 if (error == 0) {
690 bcopy(&u.ci64, data, sizeof(u.ci64));
691 }
692 break;
693 }
694
695 default:
696 VERIFY(0);
697 /* NOTREACHED */
698 }
699
700 return error;
701 }
702
703 static __attribute__((noinline)) int
in6ctl_llstart(struct ifnet * ifp,u_long cmd,caddr_t __indexable data)704 in6ctl_llstart(struct ifnet *ifp, u_long cmd, caddr_t __indexable data)
705 {
706 struct in6_aliasreq sifra;
707 struct in6_aliasreq *__single ifra = NULL;
708 boolean_t is64;
709 int error = 0;
710
711 VERIFY(ifp != NULL);
712
713 switch (cmd) {
714 case SIOCLL_START_32: /* struct in6_aliasreq_32 */
715 case SIOCLL_START_64: /* struct in6_aliasreq_64 */
716 is64 = (cmd == SIOCLL_START_64);
717 /*
718 * Convert user ifra to the kernel form, when appropriate.
719 * This allows the conversion between different data models
720 * to be centralized, so that it can be passed around to other
721 * routines that are expecting the kernel form.
722 */
723 ifra = in6_aliasreq_to_native(data, is64, &sifra);
724
725 /*
726 * NOTE: All the interface specific DLIL attachements should
727 * be done here. They are currently done in in6_ifattach_aux()
728 * for the interfaces that need it.
729 */
730 if (ifra->ifra_addr.sin6_family == AF_INET6 &&
731 /* Only check ifra_dstaddr if valid */
732 (ifra->ifra_dstaddr.sin6_len == 0 ||
733 ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
734 /* some interfaces may provide LinkLocal addresses */
735 error = in6_ifattach_aliasreq(ifp, NULL, ifra);
736 } else {
737 error = in6_ifattach_aliasreq(ifp, NULL, NULL);
738 }
739 if (error == 0) {
740 in6_if_up_dad_start(ifp);
741 }
742 break;
743
744 default:
745 VERIFY(0);
746 /* NOTREACHED */
747 }
748
749 return error;
750 }
751
752 static __attribute__((noinline)) int
in6ctl_llstop(struct ifnet * ifp)753 in6ctl_llstop(struct ifnet *ifp)
754 {
755 struct in6_ifaddr *ia;
756 struct nd_prefix pr0, *pr;
757
758 VERIFY(ifp != NULL);
759
760 /* Remove link local addresses from interface */
761 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
762 boolean_t from_begining = TRUE;
763 while (from_begining) {
764 from_begining = FALSE;
765 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
766 if (ia->ia_ifa.ifa_ifp != ifp) {
767 continue;
768 }
769 IFA_LOCK(&ia->ia_ifa);
770 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
771 ifa_addref(&ia->ia_ifa); /* for us */
772 IFA_UNLOCK(&ia->ia_ifa);
773 lck_rw_done(&in6_ifaddr_rwlock);
774 in6_purgeaddr(&ia->ia_ifa);
775 ifa_remref(&ia->ia_ifa); /* for us */
776 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
777 /*
778 * Purging the address caused in6_ifaddr_rwlock
779 * to be dropped and reacquired;
780 * therefore search again from the beginning
781 * of in6_ifaddrs list.
782 */
783 from_begining = TRUE;
784 break;
785 }
786 IFA_UNLOCK(&ia->ia_ifa);
787 }
788 }
789 lck_rw_done(&in6_ifaddr_rwlock);
790
791 /* Delete the link local prefix */
792 bzero(&pr0, sizeof(pr0));
793 pr0.ndpr_plen = 64;
794 pr0.ndpr_ifp = ifp;
795 pr0.ndpr_prefix.sin6_addr.s6_addr16[0] = IPV6_ADDR_INT16_ULL;
796 (void)in6_setscope(&pr0.ndpr_prefix.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&pr0.ndpr_prefix.sin6_scope_id));
797 pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC);
798 if (pr) {
799 lck_mtx_lock(nd6_mutex);
800 NDPR_LOCK(pr);
801 prelist_remove(pr);
802 NDPR_UNLOCK(pr);
803 NDPR_REMREF(pr); /* Drop the reference from lookup */
804 lck_mtx_unlock(nd6_mutex);
805 }
806
807 return 0;
808 }
809
810 /*
811 * This routine configures secure link local address
812 */
813 static __attribute__((noinline)) int
in6ctl_cgastart(struct ifnet * ifp,u_long cmd,caddr_t __indexable data)814 in6ctl_cgastart(struct ifnet *ifp, u_long cmd, caddr_t __indexable data)
815 {
816 struct in6_cgareq llcgasr;
817 int error = 0;
818
819 VERIFY(ifp != NULL);
820
821 switch (cmd) {
822 case SIOCLL_CGASTART_32: /* struct in6_cgareq_32 */
823 case SIOCLL_CGASTART_64: /* struct in6_cgareq_64 */
824 /*
825 * Convert user cgareq to the kernel form, when appropriate.
826 * This allows the conversion between different data models
827 * to be centralized, so that it can be passed around to other
828 * routines that are expecting the kernel form.
829 */
830 if (cmd == SIOCLL_CGASTART_64) {
831 in6_cgareq_copy_from_user64(data, &llcgasr);
832 } else {
833 in6_cgareq_copy_from_user32(data, &llcgasr);
834 }
835
836 /*
837 * NOTE: All the interface specific DLIL attachements
838 * should be done here. They are currently done in
839 * in6_ifattach_cgareq() for the interfaces that
840 * need it.
841 */
842 error = in6_ifattach_llcgareq(ifp, &llcgasr);
843 if (error == 0) {
844 in6_if_up_dad_start(ifp);
845 }
846 break;
847
848 default:
849 VERIFY(0);
850 /* NOTREACHED */
851 }
852
853 return error;
854 }
855
856 /*
857 * Caller passes in the ioctl data pointer directly via "ifr", with the
858 * expectation that this routine always uses bcopy() or other byte-aligned
859 * memory accesses.
860 */
861 static __attribute__((noinline)) int
in6ctl_gifaddr(struct ifnet * ifp,struct in6_ifaddr * ia,u_long cmd,struct in6_ifreq * ifr)862 in6ctl_gifaddr(struct ifnet *ifp, struct in6_ifaddr *ia, u_long cmd,
863 struct in6_ifreq *ifr)
864 {
865 struct sockaddr_in6 addr;
866 int error = 0;
867
868 VERIFY(ifp != NULL);
869
870 if (ia == NULL) {
871 return EADDRNOTAVAIL;
872 }
873
874 switch (cmd) {
875 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
876 IFA_LOCK(&ia->ia_ifa);
877 SOCKADDR_COPY(&ia->ia_addr, &addr, sizeof(addr));
878 IFA_UNLOCK(&ia->ia_ifa);
879 if ((error = sa6_recoverscope(&addr, TRUE)) != 0) {
880 break;
881 }
882 SOCKADDR_COPY(&addr, &ifr->ifr_addr, sizeof(addr));
883 break;
884
885 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */
886 if (!(ifp->if_flags & IFF_POINTOPOINT)) {
887 error = EINVAL;
888 break;
889 }
890 /*
891 * XXX: should we check if ifa_dstaddr is NULL and return
892 * an error?
893 */
894 IFA_LOCK(&ia->ia_ifa);
895 SOCKADDR_COPY(&ia->ia_dstaddr, &addr, sizeof(addr));
896 IFA_UNLOCK(&ia->ia_ifa);
897 if ((error = sa6_recoverscope(&addr, TRUE)) != 0) {
898 break;
899 }
900 SOCKADDR_COPY(&addr, &ifr->ifr_dstaddr, sizeof(addr));
901 break;
902
903 default:
904 VERIFY(0);
905 /* NOTREACHED */
906 }
907
908 return error;
909 }
910
911 /*
912 * Caller passes in the ioctl data pointer directly via "ifr", with the
913 * expectation that this routine always uses bcopy() or other byte-aligned
914 * memory accesses.
915 */
916 static __attribute__((noinline)) int
in6ctl_gifstat(struct ifnet * ifp,u_long cmd,struct in6_ifreq * ifr)917 in6ctl_gifstat(struct ifnet *ifp, u_long cmd, struct in6_ifreq *ifr)
918 {
919 int error = 0, index;
920
921 VERIFY(ifp != NULL);
922 index = ifp->if_index;
923
924 switch (cmd) {
925 case SIOCGIFSTAT_IN6: /* struct in6_ifreq */
926 /* N.B.: if_inet6data is never freed once set. */
927 if (IN6_IFEXTRA(ifp) == NULL) {
928 /* return (EAFNOSUPPORT)? */
929 bzero(&ifr->ifr_ifru.ifru_stat,
930 sizeof(ifr->ifr_ifru.ifru_stat));
931 } else {
932 bcopy(&IN6_IFEXTRA(ifp)->in6_ifstat,
933 &ifr->ifr_ifru.ifru_stat,
934 sizeof(ifr->ifr_ifru.ifru_stat));
935 }
936 break;
937
938 case SIOCGIFSTAT_ICMP6: /* struct in6_ifreq */
939 /* N.B.: if_inet6data is never freed once set. */
940 if (IN6_IFEXTRA(ifp) == NULL) {
941 /* return (EAFNOSUPPORT)? */
942 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
943 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
944 } else {
945 bcopy(&IN6_IFEXTRA(ifp)->icmp6_ifstat,
946 &ifr->ifr_ifru.ifru_icmp6stat,
947 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
948 }
949 break;
950
951 default:
952 VERIFY(0);
953 /* NOTREACHED */
954 }
955
956 return error;
957 }
958
959 /*
960 * Caller passes in the ioctl data pointer directly via "ifr", with the
961 * expectation that this routine always uses bcopy() or other byte-aligned
962 * memory accesses.
963 */
964 static __attribute__((noinline)) int
in6ctl_alifetime(struct in6_ifaddr * ia,u_long cmd,struct in6_ifreq * ifr,boolean_t p64)965 in6ctl_alifetime(struct in6_ifaddr *ia, u_long cmd, struct in6_ifreq *ifr,
966 boolean_t p64)
967 {
968 uint64_t timenow = net_uptime();
969 struct in6_addrlifetime ia6_lt;
970 struct timeval caltime;
971 int error = 0;
972
973 if (ia == NULL) {
974 return EADDRNOTAVAIL;
975 }
976
977 switch (cmd) {
978 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */
979 IFA_LOCK(&ia->ia_ifa);
980 /* retrieve time as calendar time (last arg is 1) */
981 in6ifa_getlifetime(ia, &ia6_lt, 1);
982 if (p64) {
983 struct in6_addrlifetime_64 lt;
984
985 bzero(<, sizeof(lt));
986 lt.ia6t_expire = ia6_lt.ia6t_expire;
987 lt.ia6t_preferred = ia6_lt.ia6t_preferred;
988 lt.ia6t_vltime = ia6_lt.ia6t_vltime;
989 lt.ia6t_pltime = ia6_lt.ia6t_pltime;
990 bcopy(<, &ifr->ifr_ifru.ifru_lifetime, sizeof(ifr->ifr_ifru.ifru_lifetime));
991 } else {
992 struct in6_addrlifetime_32 lt;
993
994 bzero(<, sizeof(lt));
995 lt.ia6t_expire = (uint32_t)ia6_lt.ia6t_expire;
996 lt.ia6t_preferred = (uint32_t)ia6_lt.ia6t_preferred;
997 lt.ia6t_vltime = (uint32_t)ia6_lt.ia6t_vltime;
998 lt.ia6t_pltime = (uint32_t)ia6_lt.ia6t_pltime;
999 /*
1000 * 32-bit userland expects a 32-bit in6_addrlifetime to
1001 * come back:
1002 */
1003 bcopy(<, &ifr->ifr_ifru.ifru_lifetime, sizeof(lt));
1004 }
1005 IFA_UNLOCK(&ia->ia_ifa);
1006 break;
1007
1008 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */
1009 getmicrotime(&caltime);
1010
1011 /* sanity for overflow - beware unsigned */
1012 if (p64) {
1013 struct in6_addrlifetime_64 lt;
1014
1015 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof(lt));
1016 if (lt.ia6t_vltime != ND6_INFINITE_LIFETIME &&
1017 lt.ia6t_vltime + caltime.tv_sec < caltime.tv_sec) {
1018 error = EINVAL;
1019 break;
1020 }
1021 if (lt.ia6t_pltime != ND6_INFINITE_LIFETIME &&
1022 lt.ia6t_pltime + caltime.tv_sec < caltime.tv_sec) {
1023 error = EINVAL;
1024 break;
1025 }
1026 } else {
1027 struct in6_addrlifetime_32 lt;
1028
1029 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof(lt));
1030 if (lt.ia6t_vltime != ND6_INFINITE_LIFETIME &&
1031 lt.ia6t_vltime + caltime.tv_sec < caltime.tv_sec) {
1032 error = EINVAL;
1033 break;
1034 }
1035 if (lt.ia6t_pltime != ND6_INFINITE_LIFETIME &&
1036 lt.ia6t_pltime + caltime.tv_sec < caltime.tv_sec) {
1037 error = EINVAL;
1038 break;
1039 }
1040 }
1041
1042 IFA_LOCK(&ia->ia_ifa);
1043 if (p64) {
1044 struct in6_addrlifetime_64 lt;
1045
1046 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof(lt));
1047 ia6_lt.ia6t_expire = (time_t)lt.ia6t_expire;
1048 ia6_lt.ia6t_preferred = (time_t)lt.ia6t_preferred;
1049 ia6_lt.ia6t_vltime = lt.ia6t_vltime;
1050 ia6_lt.ia6t_pltime = lt.ia6t_pltime;
1051 } else {
1052 struct in6_addrlifetime_32 lt;
1053
1054 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof(lt));
1055 ia6_lt.ia6t_expire = (uint32_t)lt.ia6t_expire;
1056 ia6_lt.ia6t_preferred = (uint32_t)lt.ia6t_preferred;
1057 ia6_lt.ia6t_vltime = lt.ia6t_vltime;
1058 ia6_lt.ia6t_pltime = lt.ia6t_pltime;
1059 }
1060 /* for sanity */
1061 if (ia6_lt.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1062 ia6_lt.ia6t_expire = (time_t)(timenow + ia6_lt.ia6t_vltime);
1063 } else {
1064 ia6_lt.ia6t_expire = 0;
1065 }
1066
1067 if (ia6_lt.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1068 ia6_lt.ia6t_preferred = (time_t)(timenow + ia6_lt.ia6t_pltime);
1069 } else {
1070 ia6_lt.ia6t_preferred = 0;
1071 }
1072
1073 in6ifa_setlifetime(ia, &ia6_lt);
1074 IFA_UNLOCK(&ia->ia_ifa);
1075 break;
1076
1077 default:
1078 VERIFY(0);
1079 /* NOTREACHED */
1080 }
1081
1082 return error;
1083 }
1084
1085 static int
in6ctl_clat46start(struct ifnet * ifp)1086 in6ctl_clat46start(struct ifnet *ifp)
1087 {
1088 struct nd_prefix *__single pr = NULL;
1089 struct nd_prefix *__single next = NULL;
1090 struct in6_ifaddr *__single ia6 = NULL;
1091 int error = 0;
1092
1093 if (ifp == lo_ifp) {
1094 return EINVAL;
1095 }
1096 /*
1097 * Traverse the list of prefixes and find the first non-linklocal
1098 * prefix on the interface.
1099 * For that found eligible prefix, configure a CLAT46 reserved address.
1100 */
1101 lck_mtx_lock(nd6_mutex);
1102 for (pr = nd_prefix.lh_first; pr; pr = next) {
1103 next = pr->ndpr_next;
1104
1105 NDPR_LOCK(pr);
1106 if (pr->ndpr_ifp != ifp) {
1107 NDPR_UNLOCK(pr);
1108 continue;
1109 }
1110
1111 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) {
1112 NDPR_UNLOCK(pr);
1113 continue; /* XXX */
1114 }
1115
1116 if (pr->ndpr_raf_auto == 0) {
1117 NDPR_UNLOCK(pr);
1118 continue;
1119 }
1120
1121 if (pr->ndpr_stateflags & NDPRF_DEFUNCT) {
1122 NDPR_UNLOCK(pr);
1123 continue;
1124 }
1125
1126 if ((pr->ndpr_stateflags & NDPRF_CLAT46) == 0
1127 && pr->ndpr_vltime != 0) {
1128 NDPR_ADDREF(pr); /* Take reference for rest of the processing */
1129 NDPR_UNLOCK(pr);
1130 break;
1131 } else {
1132 NDPR_UNLOCK(pr);
1133 continue;
1134 }
1135 }
1136 lck_mtx_unlock(nd6_mutex);
1137
1138 if (pr != NULL) {
1139 if ((ia6 = in6_pfx_newpersistaddr(pr, FALSE, &error,
1140 TRUE, CLAT46_COLLISION_COUNT_OFFSET)) == NULL) {
1141 nd6log0(error,
1142 "Could not configure CLAT46 address on"
1143 " interface %s.\n", ifp->if_xname);
1144 } else {
1145 IFA_LOCK(&ia6->ia_ifa);
1146 NDPR_LOCK(pr);
1147 ia6->ia6_ndpr = pr;
1148 NDPR_ADDREF(pr); /* for addr reference */
1149 pr->ndpr_stateflags |= NDPRF_CLAT46;
1150 pr->ndpr_addrcnt++;
1151 VERIFY(pr->ndpr_addrcnt != 0);
1152 NDPR_UNLOCK(pr);
1153 IFA_UNLOCK(&ia6->ia_ifa);
1154 ifa_remref(&ia6->ia_ifa);
1155 ia6 = NULL;
1156 /*
1157 * A newly added address might affect the status
1158 * of other addresses, so we check and update it.
1159 * XXX: what if address duplication happens?
1160 */
1161 lck_mtx_lock(nd6_mutex);
1162 pfxlist_onlink_check();
1163 lck_mtx_unlock(nd6_mutex);
1164 }
1165 NDPR_REMREF(pr);
1166 }
1167 return error;
1168 }
1169
1170 static int
in6ctl_clat46stop(struct ifnet * ifp)1171 in6ctl_clat46stop(struct ifnet *ifp)
1172 {
1173 int error = 0;
1174 struct in6_ifaddr *__single ia = NULL;
1175
1176 if (ifp == lo_ifp) {
1177 return EINVAL;
1178 }
1179 if ((ifp->if_eflags & IFEF_CLAT46) == 0) {
1180 /* CLAT46 isn't enabled */
1181 goto done;
1182 }
1183 if_clear_eflags(ifp, IFEF_CLAT46);
1184
1185 /* find CLAT46 address and remove it */
1186 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1187 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
1188 if (ia->ia_ifa.ifa_ifp != ifp) {
1189 continue;
1190 }
1191 IFA_LOCK(&ia->ia_ifa);
1192 if ((ia->ia6_flags & IN6_IFF_CLAT46) != 0) {
1193 ifa_addref(&ia->ia_ifa); /* for us */
1194 IFA_UNLOCK(&ia->ia_ifa);
1195 lck_rw_done(&in6_ifaddr_rwlock);
1196 in6_purgeaddr(&ia->ia_ifa);
1197 ifa_remref(&ia->ia_ifa); /* for us */
1198 goto done;
1199 }
1200 IFA_UNLOCK(&ia->ia_ifa);
1201 }
1202 lck_rw_done(&in6_ifaddr_rwlock);
1203
1204 done:
1205 return error;
1206 }
1207
1208
1209 /*
1210 * Generic INET6 control operations (ioctl's).
1211 *
1212 * ifp is NULL if not an interface-specific ioctl.
1213 *
1214 * Most of the routines called to handle the ioctls would end up being
1215 * tail-call optimized, which unfortunately causes this routine to
1216 * consume too much stack space; this is the reason for the "noinline"
1217 * attribute used on those routines.
1218 *
1219 * If called directly from within the networking stack (as opposed to via
1220 * pru_control), the socket parameter may be NULL.
1221 */
1222 int
in6_control(struct socket * so,u_long cmd,caddr_t __sized_by (IOCPARM_LEN (cmd))data,struct ifnet * ifp,struct proc * p)1223 in6_control(struct socket *so, u_long cmd, caddr_t __sized_by(IOCPARM_LEN(cmd)) data,
1224 struct ifnet *ifp, struct proc *p)
1225 {
1226 struct in6_ifreq *__single ifr = NULL;
1227 struct in6_aliasreq sifra;
1228 struct in6_aliasreq *__single ifra = NULL;
1229 struct in6_ifaddr *__single ia = NULL;
1230 struct sockaddr_in6 sin6, *__single sa6 = NULL;
1231 boolean_t privileged = (proc_suser(p) == 0);
1232 boolean_t p64 = proc_is64bit(p);
1233 boolean_t so_unlocked = FALSE;
1234 int intval, error = 0;
1235
1236 /* In case it's NULL, make sure it came from the kernel */
1237 VERIFY(so != NULL || p == kernproc);
1238
1239 /*
1240 * ioctls which don't require ifp, may require socket.
1241 */
1242 switch (cmd) {
1243 case SIOCAADDRCTL_POLICY: /* struct in6_addrpolicy */
1244 case SIOCDADDRCTL_POLICY: /* struct in6_addrpolicy */
1245 if (!privileged) {
1246 return EPERM;
1247 }
1248 return in6_src_ioctl(cmd, data);
1249 /* NOTREACHED */
1250
1251 case SIOCDRADD_IN6_32: /* struct in6_defrouter_32 */
1252 case SIOCDRADD_IN6_64: /* struct in6_defrouter_64 */
1253 case SIOCDRDEL_IN6_32: /* struct in6_defrouter_32 */
1254 case SIOCDRDEL_IN6_64: /* struct in6_defrouter_64 */
1255 if (!privileged) {
1256 return EPERM;
1257 }
1258 return defrtrlist_ioctl(cmd, data);
1259 /* NOTREACHED */
1260
1261 case SIOCGASSOCIDS32: /* struct so_aidreq32 */
1262 case SIOCGASSOCIDS64: /* struct so_aidreq64 */
1263 return in6ctl_associd(so, cmd, data);
1264 /* NOTREACHED */
1265
1266 case SIOCGCONNIDS32: /* struct so_cidreq32 */
1267 case SIOCGCONNIDS64: /* struct so_cidreq64 */
1268 return in6ctl_connid(so, cmd, data);
1269 /* NOTREACHED */
1270
1271 case SIOCGCONNINFO32: /* struct so_cinforeq32 */
1272 case SIOCGCONNINFO64: /* struct so_cinforeq64 */
1273 return in6ctl_conninfo(so, cmd, data);
1274 /* NOTREACHED */
1275 }
1276
1277 /*
1278 * The rest of ioctls require ifp; reject if we don't have one;
1279 * return ENXIO to be consistent with ifioctl().
1280 */
1281 if (ifp == NULL) {
1282 return ENXIO;
1283 }
1284
1285 /*
1286 * Unlock the socket since ifnet_ioctl() may be invoked by
1287 * one of the ioctl handlers below. Socket will be re-locked
1288 * prior to returning.
1289 */
1290 if (so != NULL) {
1291 socket_unlock(so, 0);
1292 so_unlocked = TRUE;
1293 }
1294
1295 lck_mtx_lock(&ifp->if_inet6_ioctl_lock);
1296 while (ifp->if_inet6_ioctl_busy) {
1297 (void) msleep(&ifp->if_inet6_ioctl_busy, &ifp->if_inet6_ioctl_lock, (PZERO - 1),
1298 __func__, NULL);
1299 LCK_MTX_ASSERT(&ifp->if_inet6_ioctl_lock, LCK_MTX_ASSERT_OWNED);
1300 }
1301 ifp->if_inet6_ioctl_busy = TRUE;
1302 lck_mtx_unlock(&ifp->if_inet6_ioctl_lock);
1303
1304 /*
1305 * ioctls which require ifp but not interface address.
1306 */
1307 switch (cmd) {
1308 case SIOCAUTOCONF_START: /* struct in6_ifreq */
1309 if (!privileged) {
1310 error = EPERM;
1311 goto done;
1312 }
1313 error = in6_autoconf(ifp, TRUE);
1314 goto done;
1315
1316 case SIOCAUTOCONF_STOP: /* struct in6_ifreq */
1317 if (!privileged) {
1318 error = EPERM;
1319 goto done;
1320 }
1321 error = in6_autoconf(ifp, FALSE);
1322 goto done;
1323
1324 case SIOCLL_START_32: /* struct in6_aliasreq_32 */
1325 case SIOCLL_START_64: /* struct in6_aliasreq_64 */
1326 if (!privileged) {
1327 error = EPERM;
1328 goto done;
1329 }
1330 error = in6ctl_llstart(ifp, cmd, data);
1331 goto done;
1332
1333 case SIOCLL_STOP: /* struct in6_ifreq */
1334 if (!privileged) {
1335 error = EPERM;
1336 goto done;
1337 }
1338 error = in6ctl_llstop(ifp);
1339 goto done;
1340
1341 case SIOCCLAT46_START: /* struct in6_ifreq */
1342 if (!privileged) {
1343 error = EPERM;
1344 goto done;
1345 }
1346 error = in6ctl_clat46start(ifp);
1347 if (error == 0) {
1348 if_set_eflags(ifp, IFEF_CLAT46);
1349 }
1350 goto done;
1351
1352 case SIOCCLAT46_STOP: /* struct in6_ifreq */
1353 if (!privileged) {
1354 error = EPERM;
1355 goto done;
1356 }
1357 error = in6ctl_clat46stop(ifp);
1358 goto done;
1359 case SIOCGETROUTERMODE_IN6: /* struct in6_ifreq */
1360 intval = ifp->if_ipv6_router_mode;
1361 bcopy(&intval, &((struct in6_ifreq *)(void *)data)->ifr_intval,
1362 sizeof(intval));
1363 goto done;
1364 case SIOCSETROUTERMODE_IN6: /* struct in6_ifreq */
1365 if (!privileged) {
1366 error = EPERM;
1367 goto done;
1368 }
1369 bcopy(&((struct in6_ifreq *)(void *)data)->ifr_intval,
1370 &intval, sizeof(intval));
1371 switch (intval) {
1372 case IPV6_ROUTER_MODE_DISABLED:
1373 case IPV6_ROUTER_MODE_EXCLUSIVE:
1374 case IPV6_ROUTER_MODE_HYBRID:
1375 break;
1376 default:
1377 error = EINVAL;
1378 goto done;
1379 }
1380 error = in6_setrouter(ifp, (ipv6_router_mode_t)intval);
1381 goto done;
1382
1383 case SIOCPROTOATTACH_IN6_32: /* struct in6_aliasreq_32 */
1384 case SIOCPROTOATTACH_IN6_64: /* struct in6_aliasreq_64 */
1385 if (!privileged) {
1386 error = EPERM;
1387 goto done;
1388 }
1389 error = in6_domifattach(ifp);
1390 goto done;
1391
1392 case SIOCPROTODETACH_IN6: /* struct in6_ifreq */
1393 if (!privileged) {
1394 error = EPERM;
1395 goto done;
1396 }
1397 /* Cleanup interface routes and addresses */
1398 in6_purgeif(ifp);
1399
1400 if ((error = proto_unplumb(PF_INET6, ifp))) {
1401 log(LOG_ERR, "SIOCPROTODETACH_IN6: %s error=%d\n",
1402 if_name(ifp), error);
1403 }
1404 goto done;
1405
1406 case SIOCSNDFLUSH_IN6: /* struct in6_ifreq */
1407 case SIOCSPFXFLUSH_IN6: /* struct in6_ifreq */
1408 case SIOCSRTRFLUSH_IN6: /* struct in6_ifreq */
1409 case SIOCSDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
1410 case SIOCSDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */
1411 case SIOCSIFINFO_FLAGS: /* struct in6_ndireq */
1412 case SIOCGIFCGAPREP_IN6_32: /* struct in6_cgareq_32 */
1413 case SIOCGIFCGAPREP_IN6_64: /* struct in6_cgareq_64 */
1414 case SIOCSIFCGAPREP_IN6_32: /* struct in6_cgareq_32 */
1415 case SIOCSIFCGAPREP_IN6_64: /* struct in6_cgareq_32 */
1416 if (!privileged) {
1417 error = EPERM;
1418 goto done;
1419 }
1420 OS_FALLTHROUGH;
1421 case OSIOCGIFINFO_IN6: /* struct in6_ondireq */
1422 case SIOCGIFINFO_IN6: /* struct in6_ondireq */
1423 case SIOCGNBRINFO_IN6_32: /* struct in6_nbrinfo_32 */
1424 case SIOCGNBRINFO_IN6_64: /* struct in6_nbrinfo_64 */
1425 case SIOCGDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
1426 case SIOCGDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */
1427 error = nd6_ioctl(cmd, data, ifp);
1428 goto done;
1429
1430 case SIOCSIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1431 case SIOCDIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1432 case SIOCAIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1433 case SIOCCIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1434 case SIOCSGIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1435 case SIOCGIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1436 log(LOG_NOTICE,
1437 "prefix ioctls are now invalidated. "
1438 "please use ifconfig.\n");
1439 error = EOPNOTSUPP;
1440 goto done;
1441
1442 case SIOCSSCOPE6: /* struct in6_ifreq (deprecated) */
1443 case SIOCGSCOPE6: /* struct in6_ifreq (deprecated) */
1444 case SIOCGSCOPE6DEF: /* struct in6_ifreq (deprecated) */
1445 error = EOPNOTSUPP;
1446 goto done;
1447
1448 case SIOCLL_CGASTART_32: /* struct in6_cgareq_32 */
1449 case SIOCLL_CGASTART_64: /* struct in6_cgareq_64 */
1450 if (!privileged) {
1451 error = EPERM;
1452 } else {
1453 error = in6ctl_cgastart(ifp, cmd, data);
1454 }
1455 goto done;
1456
1457 case SIOCGIFSTAT_IN6: /* struct in6_ifreq */
1458 case SIOCGIFSTAT_ICMP6: /* struct in6_ifreq */
1459 ifr = (struct in6_ifreq *)(void *)data;
1460 error = in6ctl_gifstat(ifp, cmd, ifr);
1461 goto done;
1462 }
1463
1464 /*
1465 * ioctls which require interface address; obtain sockaddr_in6.
1466 */
1467 switch (cmd) {
1468 case SIOCSIFADDR_IN6: /* struct in6_ifreq (deprecated) */
1469 case SIOCSIFDSTADDR_IN6: /* struct in6_ifreq (deprecated) */
1470 case SIOCSIFNETMASK_IN6: /* struct in6_ifreq (deprecated) */
1471 /*
1472 * Since IPv6 allows a node to assign multiple addresses
1473 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1474 */
1475 /* we decided to obsolete this command (20000704) */
1476 error = EOPNOTSUPP;
1477 goto done;
1478
1479 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1480 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
1481 if (!privileged) {
1482 error = EPERM;
1483 goto done;
1484 }
1485 /*
1486 * Convert user ifra to the kernel form, when appropriate.
1487 * This allows the conversion between different data models
1488 * to be centralized, so that it can be passed around to other
1489 * routines that are expecting the kernel form.
1490 */
1491 ifra = in6_aliasreq_to_native(data,
1492 (cmd == SIOCAIFADDR_IN6_64), &sifra);
1493 SOCKADDR_COPY(&ifra->ifra_addr, &sin6, sizeof(sin6));
1494 sa6 = &sin6;
1495 break;
1496
1497 case SIOCDIFADDR_IN6: /* struct in6_ifreq */
1498 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */
1499 if (!privileged) {
1500 error = EPERM;
1501 goto done;
1502 }
1503 OS_FALLTHROUGH;
1504 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
1505 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */
1506 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */
1507 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */
1508 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */
1509 ifr = (struct in6_ifreq *)(void *)data;
1510 SOCKADDR_COPY(&ifr->ifr_addr, &sin6, sizeof(sin6));
1511 sa6 = &sin6;
1512 break;
1513 case SIOCGIFDSTADDR:
1514 case SIOCSIFDSTADDR:
1515 case SIOCGIFBRDADDR:
1516 case SIOCSIFBRDADDR:
1517 case SIOCGIFNETMASK:
1518 case SIOCSIFNETMASK:
1519 case SIOCGIFADDR:
1520 case SIOCSIFADDR:
1521 case SIOCAIFADDR:
1522 case SIOCDIFADDR:
1523 /* Do not handle these AF_INET commands in AF_INET6 path */
1524 error = EINVAL;
1525 goto done;
1526 }
1527
1528 /*
1529 * Find address for this interface, if it exists.
1530 *
1531 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1532 * only, and used the first interface address as the target of other
1533 * operations (without checking ifra_addr). This was because netinet
1534 * code/API assumed at most 1 interface address per interface.
1535 * Since IPv6 allows a node to assign multiple addresses
1536 * on a single interface, we almost always look and check the
1537 * presence of ifra_addr, and reject invalid ones here.
1538 * It also decreases duplicated code among SIOC*_IN6 operations.
1539 */
1540 VERIFY(ia == NULL);
1541 if (sa6 != NULL && sa6->sin6_family == AF_INET6) {
1542 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
1543 if (in6_embedded_scope) {
1544 if (sa6->sin6_addr.s6_addr16[1] == 0) {
1545 /* link ID is not embedded by the user */
1546 sa6->sin6_addr.s6_addr16[1] =
1547 htons(ifp->if_index);
1548 } else if (sa6->sin6_addr.s6_addr16[1] !=
1549 htons(ifp->if_index)) {
1550 error = EINVAL; /* link ID contradicts */
1551 goto done;
1552 }
1553 if (sa6->sin6_scope_id) {
1554 if (sa6->sin6_scope_id !=
1555 (u_int32_t)ifp->if_index) {
1556 error = EINVAL;
1557 goto done;
1558 }
1559 sa6->sin6_scope_id = 0; /* XXX: good way? */
1560 }
1561 } else {
1562 if (sa6->sin6_scope_id == IFSCOPE_NONE) {
1563 sa6->sin6_scope_id = ifp->if_index;
1564 } else if (sa6->sin6_scope_id != ifp->if_index) {
1565 error = EINVAL; /* link ID contradicts */
1566 goto done;
1567 }
1568 }
1569 }
1570 /*
1571 * Any failures from this point on must take into account
1572 * a non-NULL "ia" with an outstanding reference count, and
1573 * therefore requires ifa_remref. Jump to "done" label
1574 * instead of calling return if "ia" is valid.
1575 */
1576 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
1577 }
1578
1579 /*
1580 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1581 */
1582 switch (cmd) {
1583 case SIOCDIFADDR_IN6: /* struct in6_ifreq */
1584 if (ia == NULL) {
1585 error = EADDRNOTAVAIL;
1586 goto done;
1587 }
1588 OS_FALLTHROUGH;
1589 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1590 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
1591 VERIFY(sa6 != NULL);
1592 /*
1593 * We always require users to specify a valid IPv6 address for
1594 * the corresponding operation. Use "sa6" instead of "ifra"
1595 * since SIOCDIFADDR_IN6 falls thru above.
1596 */
1597 if (sa6->sin6_family != AF_INET6 ||
1598 sa6->sin6_len != sizeof(struct sockaddr_in6)) {
1599 error = EAFNOSUPPORT;
1600 goto done;
1601 }
1602
1603 if ((cmd == SIOCAIFADDR_IN6_32 || cmd == SIOCAIFADDR_IN6_64) &&
1604 (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
1605 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr) ||
1606 IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr) ||
1607 IN6_IS_ADDR_V4COMPAT(&sa6->sin6_addr))) {
1608 error = EINVAL;
1609 goto done;
1610 }
1611 break;
1612 }
1613
1614 /*
1615 * And finally process address-related ioctls.
1616 */
1617 switch (cmd) {
1618 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
1619 /* This interface is basically deprecated. use SIOCGIFCONF. */
1620 /* FALLTHRU */
1621 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */
1622 ifr = (struct in6_ifreq *)(void *)data;
1623 error = in6ctl_gifaddr(ifp, ia, cmd, ifr);
1624 break;
1625
1626 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */
1627 if (ia != NULL) {
1628 ifr = (struct in6_ifreq *)(void *)data;
1629 IFA_LOCK(&ia->ia_ifa);
1630 SOCKADDR_COPY(&ia->ia_prefixmask, &ifr->ifr_addr,
1631 sizeof(struct sockaddr_in6));
1632 IFA_UNLOCK(&ia->ia_ifa);
1633 } else {
1634 error = EADDRNOTAVAIL;
1635 }
1636 break;
1637
1638 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */
1639 if (ia != NULL) {
1640 ifr = (struct in6_ifreq *)(void *)data;
1641 IFA_LOCK(&ia->ia_ifa);
1642 bcopy(&ia->ia6_flags, &ifr->ifr_ifru.ifru_flags6,
1643 sizeof(ifr->ifr_ifru.ifru_flags6));
1644 IFA_UNLOCK(&ia->ia_ifa);
1645 } else {
1646 error = EADDRNOTAVAIL;
1647 }
1648 break;
1649
1650 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */
1651 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */
1652 ifr = (struct in6_ifreq *)(void *)data;
1653 error = in6ctl_alifetime(ia, cmd, ifr, p64);
1654 break;
1655
1656 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1657 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
1658 ifr = (struct in6_ifreq *)(void *)data;
1659 error = in6ctl_aifaddr(ifp, ifra);
1660 break;
1661
1662 case SIOCDIFADDR_IN6:
1663 in6ctl_difaddr(ifp, ia);
1664 break;
1665
1666 default:
1667 error = ifnet_ioctl(ifp, PF_INET6, cmd, data);
1668 break;
1669 }
1670
1671 done:
1672 if (ifp != NULL) {
1673 lck_mtx_lock(&ifp->if_inet6_ioctl_lock);
1674 ifp->if_inet6_ioctl_busy = FALSE;
1675 lck_mtx_unlock(&ifp->if_inet6_ioctl_lock);
1676 wakeup(&ifp->if_inet6_ioctl_busy);
1677 }
1678
1679 if (ia != NULL) {
1680 ifa_remref(&ia->ia_ifa);
1681 }
1682 if (so_unlocked) {
1683 socket_lock(so, 0);
1684 }
1685
1686 return error;
1687 }
1688
1689 static __attribute__((noinline)) int
in6ctl_aifaddr(struct ifnet * ifp,struct in6_aliasreq * ifra)1690 in6ctl_aifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra)
1691 {
1692 int i, error, addtmp;
1693 uint8_t plen;
1694 struct nd_prefix pr0;
1695 struct nd_prefix *__single pr;
1696 struct in6_ifaddr *__single ia;
1697
1698 VERIFY(ifp != NULL && ifra != NULL);
1699 ia = NULL;
1700
1701 /*
1702 * XXX This interface is not meant to be used for static LLA
1703 * configuration.
1704 * Instead one can use SIOCLL_START can be used to configure LLA
1705 * statically.
1706 * For bin-compat reasons though, allow it for now and only make
1707 * sure that scope gets communicated correctly.
1708 */
1709 if (IN6_IS_ADDR_LINKLOCAL(&ifra->ifra_addr.sin6_addr)) {
1710 if (in6_embedded_scope) {
1711 ifra->ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1712 } else {
1713 /*
1714 * XXX May be we should rather also check if sin6_scope_id
1715 * is already set or enforce if set that it is same
1716 * as interface index?
1717 * For now to avoid any unintended consequence, just use
1718 * interface index and set sin6_scope_id.
1719 * Also should we just prohibit this interface to configure
1720 * additional link local and limti LLA configuration through
1721 * other *_start ioctls?
1722 */
1723 ifra->ifra_addr.sin6_addr.s6_addr16[1] = 0;
1724 ifra->ifra_addr.sin6_scope_id = ifp->if_index;
1725 }
1726 }
1727
1728 /* Attempt to attach the protocol, in case it isn't attached */
1729 error = in6_domifattach(ifp);
1730 if (error == 0) {
1731 /* PF_INET6 wasn't previously attached */
1732 error = in6_ifattach_aliasreq(ifp, NULL, NULL);
1733 if (error != 0) {
1734 goto done;
1735 }
1736
1737 in6_if_up_dad_start(ifp);
1738 } else if (error != EEXIST) {
1739 goto done;
1740 }
1741
1742 /*
1743 * First, make or update the interface address structure, and link it
1744 * to the list.
1745 */
1746 error = in6_update_ifa(ifp, ifra, 0, &ia);
1747 if (error != 0) {
1748 goto done;
1749 }
1750 VERIFY(ia != NULL);
1751
1752 /* Now, make the prefix on-link on the interface. */
1753 plen = (uint8_t)in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL);
1754 if (plen == 128) {
1755 goto done;
1756 }
1757
1758 /*
1759 * NOTE: We'd rather create the prefix before the address, but we need
1760 * at least one address to install the corresponding interface route,
1761 * so we configure the address first.
1762 */
1763
1764 /*
1765 * Convert mask to prefix length (prefixmask has already been validated
1766 * in in6_update_ifa().
1767 */
1768 bzero(&pr0, sizeof(pr0));
1769 pr0.ndpr_plen = plen;
1770 pr0.ndpr_ifp = ifp;
1771 pr0.ndpr_prefix = ifra->ifra_addr;
1772 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
1773
1774 /* apply the mask for safety. */
1775 for (i = 0; i < 4; i++) {
1776 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
1777 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
1778 }
1779
1780 /*
1781 * Since we don't have an API to set prefix (not address) lifetimes, we
1782 * just use the same lifetimes as addresses. The (temporarily)
1783 * installed lifetimes can be overridden by later advertised RAs (when
1784 * accept_rtadv is non 0), which is an intended behavior.
1785 */
1786 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
1787 pr0.ndpr_raf_auto = !!(ifra->ifra_flags & IN6_IFF_AUTOCONF);
1788 if (ifra->ifra_flags & (IN6_IFF_AUTOCONF | IN6_IFF_DYNAMIC)) {
1789 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
1790 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
1791 } else {
1792 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
1793 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
1794 }
1795 pr0.ndpr_stateflags |= NDPRF_STATIC;
1796 lck_mtx_init(&pr0.ndpr_lock, &ifa_mtx_grp, &ifa_mtx_attr);
1797
1798 /* add the prefix if there's none. */
1799 if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_NEVER)) == NULL) {
1800 /*
1801 * nd6_prelist_add will install the corresponding interface
1802 * route.
1803 */
1804 error = nd6_prelist_add(&pr0, NULL, &pr, FALSE);
1805 if (error != 0) {
1806 goto done;
1807 }
1808
1809 if (pr == NULL) {
1810 log(LOG_ERR, "%s: nd6_prelist_add okay, but"
1811 " no prefix.\n", __func__);
1812 error = EINVAL;
1813 goto done;
1814 }
1815 }
1816
1817 IFA_LOCK(&ia->ia_ifa);
1818
1819 /* if this is a new autoconfed addr */
1820 addtmp = FALSE;
1821 if (ia->ia6_ndpr == NULL) {
1822 NDPR_LOCK(pr);
1823 ++pr->ndpr_addrcnt;
1824 if (!(ia->ia6_flags & IN6_IFF_NOTMANUAL)) {
1825 ++pr->ndpr_manual_addrcnt;
1826 VERIFY(pr->ndpr_manual_addrcnt != 0);
1827 }
1828 VERIFY(pr->ndpr_addrcnt != 0);
1829 ia->ia6_ndpr = pr;
1830 NDPR_ADDREF(pr); /* for addr reference */
1831
1832 /*
1833 * If this is the first autoconf address from the prefix,
1834 * create a temporary address as well (when specified).
1835 */
1836 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
1837 ip6_use_tempaddr &&
1838 pr->ndpr_addrcnt == 1 &&
1839 (!IN6_IS_ADDR_UNIQUE_LOCAL(&ia->ia_addr.sin6_addr)
1840 || ip6_ula_use_tempaddr)) {
1841 addtmp = true;
1842 }
1843 NDPR_UNLOCK(pr);
1844 }
1845
1846 IFA_UNLOCK(&ia->ia_ifa);
1847
1848 if (addtmp) {
1849 int e;
1850 e = in6_tmpifadd(ia, 1);
1851 if (e != 0) {
1852 log(LOG_NOTICE, "%s: failed to create a"
1853 " temporary address, error=%d\n",
1854 __func__, e);
1855 }
1856 }
1857
1858 /*
1859 * This might affect the status of autoconfigured addresses, that is,
1860 * this address might make other addresses detached.
1861 */
1862 lck_mtx_lock(nd6_mutex);
1863 pfxlist_onlink_check();
1864 lck_mtx_unlock(nd6_mutex);
1865
1866 /* Drop use count held above during lookup/add */
1867 NDPR_REMREF(pr);
1868
1869 done:
1870 if (ia != NULL) {
1871 ifa_remref(&ia->ia_ifa);
1872 }
1873 return error;
1874 }
1875
1876 static __attribute__((noinline)) void
in6ctl_difaddr(struct ifnet * ifp,struct in6_ifaddr * ia)1877 in6ctl_difaddr(struct ifnet *ifp, struct in6_ifaddr *ia)
1878 {
1879 int i = 0;
1880 struct nd_prefix pr0;
1881 struct nd_prefix *__single pr;
1882
1883 VERIFY(ifp != NULL && ia != NULL);
1884
1885 /*
1886 * If the address being deleted is the only one that owns
1887 * the corresponding prefix, expire the prefix as well.
1888 * XXX: theoretically, we don't have to worry about such
1889 * relationship, since we separate the address management
1890 * and the prefix management. We do this, however, to provide
1891 * as much backward compatibility as possible in terms of
1892 * the ioctl operation.
1893 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1894 */
1895 IFA_LOCK(&ia->ia_ifa);
1896 bzero(&pr0, sizeof(pr0));
1897 pr0.ndpr_ifp = ifp;
1898 pr0.ndpr_plen = (uint8_t)in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1899 if (pr0.ndpr_plen == 128) {
1900 IFA_UNLOCK(&ia->ia_ifa);
1901 goto purgeaddr;
1902 }
1903 pr0.ndpr_prefix = ia->ia_addr;
1904 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
1905 for (i = 0; i < 4; i++) {
1906 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
1907 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
1908 }
1909 IFA_UNLOCK(&ia->ia_ifa);
1910
1911 if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC)) != NULL) {
1912 IFA_LOCK(&ia->ia_ifa);
1913 NDPR_LOCK(pr);
1914 if (pr->ndpr_addrcnt == 1) {
1915 /* XXX: just for expiration */
1916 pr->ndpr_expire = 1;
1917 }
1918 NDPR_UNLOCK(pr);
1919 IFA_UNLOCK(&ia->ia_ifa);
1920
1921 /* Drop use count held above during lookup */
1922 NDPR_REMREF(pr);
1923 }
1924
1925 purgeaddr:
1926 in6_purgeaddr(&ia->ia_ifa);
1927 }
1928
1929 static __attribute__((noinline)) int
in6_autoconf(struct ifnet * ifp,int enable)1930 in6_autoconf(struct ifnet *ifp, int enable)
1931 {
1932 int error = 0;
1933
1934 VERIFY(ifp != NULL);
1935
1936 if (ifp->if_flags & IFF_LOOPBACK) {
1937 return EINVAL;
1938 }
1939
1940 if (enable) {
1941 /*
1942 * An interface in IPv6 router mode implies that it
1943 * is either configured with a static IP address or
1944 * autoconfigured via a locally-generated RA. Prevent
1945 * SIOCAUTOCONF_START from being set in that mode.
1946 */
1947 ifnet_lock_exclusive(ifp);
1948 if (ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
1949 if_clear_eflags(ifp, IFEF_ACCEPT_RTADV);
1950 error = EBUSY;
1951 } else {
1952 if_set_eflags(ifp, IFEF_ACCEPT_RTADV);
1953 }
1954 ifnet_lock_done(ifp);
1955 } else {
1956 struct in6_ifaddr *__single ia = NULL;
1957
1958 if_clear_eflags(ifp, IFEF_ACCEPT_RTADV);
1959
1960 /* Remove autoconfigured address from interface */
1961 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1962 boolean_t from_begining = TRUE;
1963 while (from_begining) {
1964 from_begining = FALSE;
1965 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
1966 if (ia->ia_ifa.ifa_ifp != ifp) {
1967 continue;
1968 }
1969 IFA_LOCK(&ia->ia_ifa);
1970 if (ia->ia6_flags & IN6_IFF_AUTOCONF) {
1971 ifa_addref(&ia->ia_ifa); /* for us */
1972 IFA_UNLOCK(&ia->ia_ifa);
1973 lck_rw_done(&in6_ifaddr_rwlock);
1974 in6_purgeaddr(&ia->ia_ifa);
1975 ifa_remref(&ia->ia_ifa); /* for us */
1976 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1977 /*
1978 * Purging the address caused in6_ifaddr_rwlock
1979 * to be dropped and reacquired;
1980 * therefore search again from the beginning
1981 * of in6_ifaddrs list.
1982 */
1983 from_begining = TRUE;
1984 break;
1985 }
1986 IFA_UNLOCK(&ia->ia_ifa);
1987 }
1988 }
1989 lck_rw_done(&in6_ifaddr_rwlock);
1990 }
1991 return error;
1992 }
1993
1994 /*
1995 * Handle SIOCSETROUTERMODE_IN6 to set the IPv6 router mode on the interface
1996 * Entering or exiting IPV6_ROUTER_MODE_EXCLUSIVE will result in the removal of
1997 * autoconfigured IPv6 addresses on the interface.
1998 */
1999 static __attribute__((noinline)) int
in6_setrouter(struct ifnet * ifp,ipv6_router_mode_t mode)2000 in6_setrouter(struct ifnet *ifp, ipv6_router_mode_t mode)
2001 {
2002 int error = 0;
2003 ipv6_router_mode_t prev_mode;
2004
2005 VERIFY(ifp != NULL);
2006
2007 if (ifp->if_flags & IFF_LOOPBACK) {
2008 return ENODEV;
2009 }
2010
2011 prev_mode = ifp->if_ipv6_router_mode;
2012 if (prev_mode == mode) {
2013 /* no mode change, there's nothing to do */
2014 return 0;
2015 }
2016 if (mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
2017 struct nd_ifinfo *__single ndi = NULL;
2018
2019 ndi = ND_IFINFO(ifp);
2020 if (ndi != NULL && ndi->initialized) {
2021 lck_mtx_lock(&ndi->lock);
2022 if (ndi->flags & ND6_IFF_PROXY_PREFIXES) {
2023 /* No proxy if we are an advertising router */
2024 ndi->flags &= ~ND6_IFF_PROXY_PREFIXES;
2025 lck_mtx_unlock(&ndi->lock);
2026 (void) nd6_if_prproxy(ifp, FALSE);
2027 } else {
2028 lck_mtx_unlock(&ndi->lock);
2029 }
2030 }
2031 }
2032
2033 ifp->if_ipv6_router_mode = mode;
2034 lck_mtx_lock(nd6_mutex);
2035 defrouter_select(ifp, NULL);
2036 lck_mtx_unlock(nd6_mutex);
2037 if_allmulti(ifp, (mode == IPV6_ROUTER_MODE_EXCLUSIVE));
2038 if (mode == IPV6_ROUTER_MODE_EXCLUSIVE ||
2039 (prev_mode == IPV6_ROUTER_MODE_EXCLUSIVE
2040 && mode == IPV6_ROUTER_MODE_DISABLED)) {
2041 error = in6_autoconf(ifp, FALSE);
2042 }
2043 return error;
2044 }
2045
2046 static int
in6_to_kamescope(struct sockaddr_in6 * sin6,struct ifnet * ifp)2047 in6_to_kamescope(struct sockaddr_in6 *sin6, struct ifnet *ifp)
2048 {
2049 struct sockaddr_in6 tmp;
2050 int error, id;
2051
2052 VERIFY(sin6 != NULL);
2053 tmp = *sin6;
2054
2055 error = in6_recoverscope(&tmp, &sin6->sin6_addr, ifp);
2056 if (error != 0) {
2057 return error;
2058 }
2059
2060 id = in6_addr2scopeid(ifp, &tmp.sin6_addr);
2061 if (tmp.sin6_scope_id == 0) {
2062 tmp.sin6_scope_id = id;
2063 } else if (tmp.sin6_scope_id != id) {
2064 return EINVAL; /* scope ID mismatch. */
2065 }
2066 error = in6_embedscope(&tmp.sin6_addr, &tmp, NULL, NULL, NULL, IN6_NULL_IF_EMBEDDED_SCOPE(&tmp.sin6_scope_id));
2067 if (error != 0) {
2068 return error;
2069 }
2070
2071 if (in6_embedded_scope || !IN6_IS_SCOPE_EMBED(&tmp.sin6_addr)) {
2072 tmp.sin6_scope_id = 0;
2073 }
2074 *sin6 = tmp;
2075 return 0;
2076 }
2077
2078 /*
2079 * When the address is being configured we should clear out certain flags
2080 * coming in from the caller.
2081 */
2082 #define IN6_IFF_CLR_ADDR_FLAG_MASK (~(IN6_IFF_DEPRECATED | IN6_IFF_DETACHED | IN6_IFF_DUPLICATED))
2083
2084 static int
in6_ifaupdate_aux(struct in6_ifaddr * ia,struct ifnet * ifp,int ifaupflags)2085 in6_ifaupdate_aux(struct in6_ifaddr *ia, struct ifnet *ifp, int ifaupflags)
2086 {
2087 struct sockaddr_in6 mltaddr, mltmask;
2088 struct in6_addr llsol;
2089 struct ifaddr *__single ifa;
2090 struct in6_multi *__single in6m_sol;
2091 struct in6_multi_mship *__single imm;
2092 rtentry_ref_t rt;
2093 int delay, error = 0;
2094
2095 VERIFY(ifp != NULL && ia != NULL);
2096 ifa = &ia->ia_ifa;
2097 in6m_sol = NULL;
2098
2099 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
2100 __func__,
2101 ip6_sprintf(&ia->ia_addr.sin6_addr),
2102 if_name(ia->ia_ifp),
2103 ia->ia6_flags,
2104 ifaupflags);
2105
2106 /*
2107 * Just to be safe, always clear certain flags when address
2108 * is being configured
2109 */
2110 ia->ia6_flags &= IN6_IFF_CLR_ADDR_FLAG_MASK;
2111
2112 /*
2113 * Mark the address as tentative before joining multicast addresses,
2114 * so that corresponding MLD responses would not have a tentative
2115 * source address.
2116 */
2117 if (in6if_do_dad(ifp)) {
2118 in6_ifaddr_set_dadprogress(ia);
2119 /*
2120 * Do not delay sending neighbor solicitations when using optimistic
2121 * duplicate address detection, c.f. RFC 4429.
2122 */
2123 if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
2124 ifaupflags &= ~IN6_IFAUPDATE_DADDELAY;
2125 } else {
2126 ifaupflags |= IN6_IFAUPDATE_DADDELAY;
2127 }
2128 } else {
2129 /*
2130 * If the interface has been marked to not perform
2131 * DAD, make sure to reset DAD in progress flags
2132 * that may come in from the caller.
2133 */
2134 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
2135 }
2136
2137 /* Join necessary multicast groups */
2138 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
2139 /* join solicited multicast addr for new host id */
2140 bzero(&llsol, sizeof(struct in6_addr));
2141 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
2142 llsol.s6_addr32[1] = 0;
2143 llsol.s6_addr32[2] = htonl(1);
2144 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
2145 llsol.s6_addr8[12] = 0xff;
2146 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
2147 /* XXX: should not happen */
2148 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
2149 goto unwind;
2150 }
2151 delay = 0;
2152 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) {
2153 /*
2154 * We need a random delay for DAD on the address
2155 * being configured. It also means delaying
2156 * transmission of the corresponding MLD report to
2157 * avoid report collision. [RFC 4862]
2158 */
2159 delay = random() % MAX_RTR_SOLICITATION_DELAY;
2160 }
2161 imm = in6_joingroup(ifp, &llsol, &error, delay);
2162 if (imm == NULL) {
2163 nd6log(info,
2164 "%s: addmulti failed for %s on %s (errno=%d)\n",
2165 __func__, ip6_sprintf(&llsol), if_name(ifp),
2166 error);
2167 VERIFY(error != 0);
2168 goto unwind;
2169 }
2170 in6m_sol = imm->i6mm_maddr;
2171 /* take a refcount for this routine */
2172 IN6M_ADDREF(in6m_sol);
2173
2174 IFA_LOCK_SPIN(ifa);
2175 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
2176 IFA_UNLOCK(ifa);
2177
2178 SOCKADDR_ZERO(&mltmask, sizeof(mltmask));
2179 mltmask.sin6_len = sizeof(struct sockaddr_in6);
2180 mltmask.sin6_family = AF_INET6;
2181 mltmask.sin6_addr = in6mask32;
2182 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
2183
2184 /*
2185 * join link-local all-nodes address
2186 */
2187 SOCKADDR_ZERO(&mltaddr, sizeof(mltaddr));
2188 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
2189 mltaddr.sin6_family = AF_INET6;
2190 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
2191 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&mltaddr.sin6_scope_id))) != 0) {
2192 goto unwind; /* XXX: should not fail */
2193 }
2194 /*
2195 * XXX: do we really need this automatic routes?
2196 * We should probably reconsider this stuff. Most applications
2197 * actually do not need the routes, since they usually specify
2198 * the outgoing interface.
2199 */
2200 rt = rtalloc1_scoped(SA(&mltaddr), 0, 0UL,
2201 ia->ia_ifp->if_index);
2202 if (rt) {
2203 if (memcmp(&mltaddr.sin6_addr, &SIN6(rt_key(rt))->sin6_addr, MLTMASK_LEN)) {
2204 rtfree(rt);
2205 rt = NULL;
2206 }
2207 }
2208 if (!rt) {
2209 error = rtrequest_scoped(RTM_ADD,
2210 SA(&mltaddr),
2211 SA(&ia->ia_addr),
2212 SA(&mltmask), RTF_UP | RTF_CLONING,
2213 NULL, ia->ia_ifp->if_index);
2214 if (error) {
2215 goto unwind;
2216 }
2217 } else {
2218 rtfree(rt);
2219 }
2220
2221 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
2222 if (!imm) {
2223 nd6log(info,
2224 "%s: addmulti failed for %s on %s (errno=%d)\n",
2225 __func__, ip6_sprintf(&mltaddr.sin6_addr),
2226 if_name(ifp), error);
2227 VERIFY(error != 0);
2228 goto unwind;
2229 }
2230 IFA_LOCK_SPIN(ifa);
2231 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
2232 IFA_UNLOCK(ifa);
2233
2234 /*
2235 * join node information group address
2236 */
2237 delay = 0;
2238 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) {
2239 /*
2240 * The spec doesn't say anything about delay for this
2241 * group, but the same logic should apply.
2242 */
2243 delay = random() % MAX_RTR_SOLICITATION_DELAY;
2244 }
2245 lck_mtx_lock(&hostname_lock);
2246 int n = in6_nigroup(ifp, hostname, strbuflen(hostname),
2247 &mltaddr.sin6_addr, IN6_NULL_IF_EMBEDDED_SCOPE(&mltaddr.sin6_scope_id));
2248 lck_mtx_unlock(&hostname_lock);
2249 if (n == 0) {
2250 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
2251 delay); /* XXX jinmei */
2252 if (!imm) {
2253 nd6log(info,
2254 "%s: addmulti failed for %s on %s "
2255 "(errno=%d)\n",
2256 __func__, ip6_sprintf(&mltaddr.sin6_addr),
2257 if_name(ifp), error);
2258 /* XXX not very fatal, go on... */
2259 error = 0;
2260 } else {
2261 IFA_LOCK_SPIN(ifa);
2262 LIST_INSERT_HEAD(&ia->ia6_memberships,
2263 imm, i6mm_chain);
2264 IFA_UNLOCK(ifa);
2265 }
2266 }
2267
2268 /*
2269 * join interface-local all-nodes address.
2270 * (ff01::1%ifN, and ff01::%ifN/32)
2271 */
2272 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
2273 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, IN6_NULL_IF_EMBEDDED_SCOPE(&mltaddr.sin6_scope_id))) != 0) {
2274 goto unwind; /* XXX: should not fail */
2275 }
2276 /* XXX: again, do we really need the route? */
2277 rt = rtalloc1_scoped(SA(&mltaddr), 0, 0UL,
2278 ia->ia_ifp->if_index);
2279 if (rt) {
2280 if (memcmp(&mltaddr.sin6_addr, &(SIN6(rt_key(rt)))->sin6_addr, MLTMASK_LEN)) {
2281 rtfree(rt);
2282 rt = NULL;
2283 }
2284 }
2285 if (!rt) {
2286 error = rtrequest_scoped(RTM_ADD,
2287 SA(&mltaddr),
2288 SA(&ia->ia_addr),
2289 SA(&mltmask), RTF_UP | RTF_CLONING,
2290 NULL, ia->ia_ifp->if_index);
2291 if (error) {
2292 goto unwind;
2293 }
2294 } else {
2295 rtfree(rt);
2296 }
2297
2298 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
2299 if (!imm) {
2300 nd6log(info,
2301 "%s: addmulti failed for %s on %s (errno=%d)\n",
2302 __func__, ip6_sprintf(&mltaddr.sin6_addr),
2303 if_name(ifp), error);
2304 VERIFY(error != 0);
2305 goto unwind;
2306 }
2307 IFA_LOCK(ifa);
2308 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
2309 IFA_UNLOCK(ifa);
2310 #undef MLTMASK_LEN
2311
2312 /*
2313 * create a ff00::/8 route
2314 */
2315 SOCKADDR_ZERO(&mltmask, sizeof(mltmask));
2316 mltmask.sin6_len = sizeof(struct sockaddr_in6);
2317 mltmask.sin6_family = AF_INET6;
2318 mltmask.sin6_addr = in6mask8;
2319 #define MLTMASK_LEN_8_BITS 1 /* ff00::/8 mltmask's masklen (=8bit=1octet) */
2320
2321 SOCKADDR_ZERO(&mltaddr, sizeof(mltaddr));
2322 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
2323 mltaddr.sin6_family = AF_INET6;
2324 mltaddr.sin6_addr = in6addr_multicast_prefix;
2325
2326 rt = rtalloc1_scoped(SA(&mltaddr), 0, 0UL,
2327 ia->ia_ifp->if_index);
2328 if (rt) {
2329 if (memcmp(&mltaddr.sin6_addr, &(SIN6(rt_key(rt)))->sin6_addr, MLTMASK_LEN_8_BITS)) {
2330 rtfree(rt);
2331 rt = NULL;
2332 }
2333 }
2334 if (!rt) {
2335 error = rtrequest_scoped(RTM_ADD,
2336 SA(&mltaddr),
2337 SA(&ia->ia_addr),
2338 SA(&mltmask), RTF_UP | RTF_CLONING,
2339 NULL, ia->ia_ifp->if_index);
2340 if (error) {
2341 goto unwind;
2342 }
2343 } else {
2344 rtfree(rt);
2345 }
2346 }
2347 #undef MLTMASK_LEN_8_BITS
2348
2349 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2350 ++nd6_sched_timeout_want;
2351
2352 /*
2353 * Perform DAD, if:
2354 * * Interface is marked to perform DAD, AND
2355 * * Address is not marked to skip DAD, AND
2356 * * Address is in a pre-DAD state (Tentative or Optimistic)
2357 */
2358 IFA_LOCK_SPIN(ifa);
2359 if (in6if_do_dad(ifp) && (ia->ia6_flags & IN6_IFF_NODAD) == 0 &&
2360 (ia->ia6_flags & IN6_IFF_DADPROGRESS) != 0) {
2361 int mindelay, maxdelay;
2362 int *delayptr, delayval;
2363
2364 IFA_UNLOCK(ifa);
2365 delayptr = NULL;
2366 /*
2367 * Avoid the DAD delay if the caller wants us to skip it.
2368 * This is not compliant with RFC 2461, but it's only being
2369 * used for signalling and not for actual DAD.
2370 */
2371 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY) &&
2372 !(ia->ia6_flags & IN6_IFF_SWIFTDAD)) {
2373 /*
2374 * We need to impose a delay before sending an NS
2375 * for DAD. Check if we also needed a delay for the
2376 * corresponding MLD message. If we did, the delay
2377 * should be larger than the MLD delay (this could be
2378 * relaxed a bit, but this simple logic is at least
2379 * safe).
2380 */
2381 mindelay = 0;
2382 if (in6m_sol != NULL) {
2383 IN6M_LOCK(in6m_sol);
2384 if (in6m_sol->in6m_state ==
2385 MLD_REPORTING_MEMBER) {
2386 mindelay = in6m_sol->in6m_timer;
2387 }
2388 IN6M_UNLOCK(in6m_sol);
2389 }
2390 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
2391 if (maxdelay - mindelay == 0) {
2392 delayval = 0;
2393 } else {
2394 delayval =
2395 (random() % (maxdelay - mindelay)) +
2396 mindelay;
2397 }
2398 delayptr = &delayval;
2399 }
2400
2401 nd6_dad_start((struct ifaddr *)ia, delayptr);
2402 } else {
2403 IFA_UNLOCK(ifa);
2404 }
2405
2406 goto done;
2407
2408 unwind:
2409 VERIFY(error != 0);
2410 in6_purgeaddr(&ia->ia_ifa);
2411
2412 done:
2413 /* release reference held for this routine */
2414 if (in6m_sol != NULL) {
2415 IN6M_REMREF(in6m_sol);
2416 }
2417 return error;
2418 }
2419
2420 /*
2421 * Request an IPv6 interface address. If the address is new, then it will be
2422 * constructed and appended to the interface address chains. The interface
2423 * address structure is optionally returned with a reference for the caller.
2424 */
2425 int
in6_update_ifa(struct ifnet * ifp,struct in6_aliasreq * ifra,int ifaupflags,struct in6_ifaddr ** iar)2426 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int ifaupflags,
2427 struct in6_ifaddr **iar)
2428 {
2429 struct in6_addrlifetime ia6_lt;
2430 struct in6_ifaddr *__single ia;
2431 struct ifaddr *__single ifa;
2432 struct ifaddr *__single xifa;
2433 struct in6_addrlifetime *__single lt;
2434 uint64_t timenow;
2435 int plen, error;
2436
2437 /* Sanity check parameters and initialize locals */
2438 VERIFY(ifp != NULL && ifra != NULL && iar != NULL);
2439 ia = NULL;
2440 ifa = NULL;
2441 error = 0;
2442
2443 /*
2444 * We always require users to specify a valid IPv6 address for
2445 * the corresponding operation.
2446 */
2447 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
2448 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
2449 error = EAFNOSUPPORT;
2450 goto unwind;
2451 }
2452
2453 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2454 if (ifra->ifra_prefixmask.sin6_len == 0 ||
2455 ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) {
2456 error = EINVAL;
2457 goto unwind;
2458 }
2459
2460 /* Validate prefix length extracted from ifra_prefixmask structure. */
2461 plen = (uint8_t)in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
2462 (u_char *)&ifra->ifra_prefixmask + ifra->ifra_prefixmask.sin6_len);
2463
2464 if (plen <= 0) {
2465 error = EINVAL;
2466 goto unwind;
2467 }
2468
2469 /* Validate lifetimes */
2470 lt = &ifra->ifra_lifetime;
2471 if (lt->ia6t_pltime > lt->ia6t_vltime) {
2472 log(LOG_INFO,
2473 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__,
2474 lt->ia6t_pltime, lt->ia6t_vltime,
2475 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
2476 error = EINVAL;
2477 goto unwind;
2478 }
2479 if (lt->ia6t_vltime == 0) {
2480 /*
2481 * the following log might be noisy, but this is a typical
2482 * configuration mistake or a tool's bug.
2483 */
2484 log(LOG_INFO, "%s: valid lifetime is 0 for %s\n", __func__,
2485 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
2486 }
2487
2488 /*
2489 * Before we lock the ifnet structure, we first check to see if the
2490 * address already exists. If so, then we don't allocate and link a
2491 * new one here.
2492 */
2493 struct sockaddr_in6 lookup_address = ifra->ifra_addr;
2494 if (IN6_IS_ADDR_LINKLOCAL(&lookup_address.sin6_addr)) {
2495 if (in6_embedded_scope) {
2496 if (lookup_address.sin6_addr.s6_addr16[1] == 0) {
2497 /* link ID is not embedded by the user */
2498 lookup_address.sin6_addr.s6_addr16[1] =
2499 htons(ifp->if_index);
2500 } else if (lookup_address.sin6_addr.s6_addr16[1] !=
2501 htons(ifp->if_index)) {
2502 error = EINVAL; /* link ID contradicts */
2503 goto done;
2504 }
2505 } else {
2506 if (lookup_address.sin6_scope_id == IFSCOPE_NONE) {
2507 lookup_address.sin6_scope_id = ifp->if_index;
2508 }
2509 }
2510 if (lookup_address.sin6_scope_id != 0 &&
2511 lookup_address.sin6_scope_id !=
2512 (u_int32_t)ifp->if_index) {
2513 error = EINVAL;
2514 goto done;
2515 }
2516 }
2517
2518 ia = in6ifa_ifpwithaddr(ifp, &lookup_address.sin6_addr);
2519 if (ia != NULL) {
2520 ifa = &ia->ia_ifa;
2521 }
2522
2523 /*
2524 * Validate destination address on interface types that require it.
2525 */
2526 if ((ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) {
2527 switch (ifra->ifra_dstaddr.sin6_family) {
2528 case AF_INET6:
2529 if (plen != 128) {
2530 /* noisy message for diagnostic purposes */
2531 log(LOG_INFO,
2532 "%s: prefix length < 128 with"
2533 " explicit dstaddr.\n", __func__);
2534 error = EINVAL;
2535 goto unwind;
2536 }
2537 break;
2538
2539 case AF_UNSPEC:
2540 break;
2541
2542 default:
2543 error = EAFNOSUPPORT;
2544 goto unwind;
2545 }
2546 } else if (ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) {
2547 log(LOG_INFO,
2548 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2549 __func__);
2550 error = EINVAL;
2551 goto unwind;
2552 }
2553
2554 timenow = net_uptime();
2555
2556 if (ia == NULL) {
2557 zalloc_flags_t how;
2558
2559 /* Is this the first new IPv6 address for the interface? */
2560 ifaupflags |= IN6_IFAUPDATE_NEWADDR;
2561
2562 /* Allocate memory for IPv6 interface address structure. */
2563 how = (ifaupflags & IN6_IFAUPDATE_NOWAIT) ? Z_NOWAIT : Z_WAITOK;
2564 ia = in6_ifaddr_alloc(how);
2565 if (ia == NULL) {
2566 error = ENOBUFS;
2567 goto unwind;
2568 }
2569
2570 ifa = &ia->ia_ifa;
2571
2572 /*
2573 * Initialize interface address structure.
2574 *
2575 * Note well: none of these sockaddr_in6 structures contain a
2576 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2577 * We still embed link-local scope identifiers at the end of an
2578 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2579 * ifa_dstaddr field is always non-NULL on point-to-point and
2580 * loopback interfaces, and conventionally points to a socket
2581 * address of AF_UNSPEC family when there is no destination.
2582 *
2583 * Please enjoy the dancing sea turtle.
2584 */
2585 IA6_HASH_INIT(ia);
2586 ifa->ifa_addr = SA(&ia->ia_addr);
2587 if (ifra->ifra_dstaddr.sin6_family == AF_INET6 ||
2588 (ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
2589 ifa->ifa_dstaddr = SA(&ia->ia_dstaddr);
2590 }
2591 ifa->ifa_netmask = SA(&ia->ia_prefixmask);
2592 ifa->ifa_ifp = ifp;
2593 ifa->ifa_metric = ifp->if_metric;
2594 ifa->ifa_rtrequest = nd6_rtrequest;
2595
2596 LIST_INIT(&ia->ia6_memberships);
2597 ia->ia_addr.sin6_family = AF_INET6;
2598 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
2599 ia->ia_addr.sin6_addr = ifra->ifra_addr.sin6_addr;
2600 ia->ia_prefixmask.sin6_family = AF_INET6;
2601 ia->ia_prefixmask.sin6_len = sizeof(ia->ia_prefixmask);
2602 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
2603 error = in6_to_kamescope(&ia->ia_addr, ifp);
2604 if (error != 0) {
2605 goto unwind;
2606 }
2607 if (ifa->ifa_dstaddr != NULL) {
2608 ia->ia_dstaddr = ifra->ifra_dstaddr;
2609 error = in6_to_kamescope(&ia->ia_dstaddr, ifp);
2610 if (error != 0) {
2611 goto unwind;
2612 }
2613 }
2614
2615 /* Append to address chains */
2616 ifnet_lock_exclusive(ifp);
2617 ifaupflags |= IN6_IFAUPDATE_1STADDR;
2618 TAILQ_FOREACH(xifa, &ifp->if_addrlist, ifa_list) {
2619 IFA_LOCK_SPIN(xifa);
2620 if (xifa->ifa_addr->sa_family != AF_INET6) {
2621 IFA_UNLOCK(xifa);
2622 ifaupflags &= ~IN6_IFAUPDATE_1STADDR;
2623 break;
2624 }
2625 IFA_UNLOCK(xifa);
2626 }
2627
2628 IFA_LOCK_SPIN(ifa);
2629 if_attach_ifa(ifp, ifa); /* holds reference for ifnet link */
2630 IFA_UNLOCK(ifa);
2631 ifnet_lock_done(ifp);
2632
2633 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2634 TAILQ_INSERT_TAIL(&in6_ifaddrhead, ia, ia6_link);
2635 ifa_addref(ifa); /* hold for in6_ifaddrs link */
2636 os_atomic_inc(&in6_ifaddrlist_genid, relaxed);
2637 lck_rw_done(&in6_ifaddr_rwlock);
2638 } else {
2639 ifa = &ia->ia_ifa;
2640 ifaupflags &= ~(IN6_IFAUPDATE_NEWADDR | IN6_IFAUPDATE_1STADDR);
2641 }
2642
2643 VERIFY(ia != NULL && ifa == &ia->ia_ifa);
2644
2645 if (!(ifaupflags & IN6_IFAUPDATE_NEWADDR) && ia->ia6_ndpr != NULL) {
2646 /* If we're flopping between address configuration methods, adjust the counts. */
2647 struct nd_prefix *pr = ia->ia6_ndpr;
2648 NDPR_LOCK(pr);
2649 if ((ia->ia6_flags & IN6_IFF_NOTMANUAL) && !(ifra->ifra_flags & IN6_IFF_NOTMANUAL)) {
2650 log(LOG_DEBUG, "address %s already exists in automatic form", ip6_sprintf(&ia->ia_addr.sin6_addr));
2651 pr->ndpr_manual_addrcnt--;
2652 } else if (!(ia->ia6_flags & IN6_IFF_NOTMANUAL) && (ifra->ifra_flags & IN6_IFF_NOTMANUAL)) {
2653 log(LOG_DEBUG, "address %s already exists in manual form", ip6_sprintf(&ia->ia_addr.sin6_addr));
2654 /* no need to adjust counts here as npdr_addrcnt is always adjusted no matter the interface type */
2655 }
2656 NDPR_UNLOCK(pr);
2657 }
2658
2659 IFA_LOCK(ifa);
2660
2661 /*
2662 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2663 * to see if the address is deprecated or invalidated, but initialize
2664 * these members for applications.
2665 */
2666 ia->ia6_updatetime = ia->ia6_createtime = timenow;
2667 ia6_lt = *lt;
2668 if (ia6_lt.ia6t_vltime != ND6_INFINITE_LIFETIME) {
2669 ia6_lt.ia6t_expire = (time_t)(timenow + ia6_lt.ia6t_vltime);
2670 } else {
2671 ia6_lt.ia6t_expire = 0;
2672 }
2673 if (ia6_lt.ia6t_pltime != ND6_INFINITE_LIFETIME) {
2674 ia6_lt.ia6t_preferred = (time_t)(timenow + ia6_lt.ia6t_pltime);
2675 } else {
2676 ia6_lt.ia6t_preferred = 0;
2677 }
2678 in6ifa_setlifetime(ia, &ia6_lt);
2679
2680 /*
2681 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2682 * userland, make it deprecated.
2683 */
2684 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2685 ia->ia6_lifetime.ia6ti_pltime = 0;
2686 ia->ia6_lifetime.ia6ti_preferred = timenow;
2687 }
2688
2689 /*
2690 * Update flag or prefix length
2691 */
2692 ia->ia_plen = plen;
2693 ia->ia6_flags = ifra->ifra_flags;
2694
2695 /* Release locks (new address available to concurrent tasks) */
2696 IFA_UNLOCK(ifa);
2697
2698 /* Further initialization of the interface address */
2699 error = in6_ifinit(ifp, ia, ifaupflags);
2700 if (error != 0) {
2701 goto unwind;
2702 }
2703
2704 /* Finish updating the address while other tasks are working with it */
2705 error = in6_ifaupdate_aux(ia, ifp, ifaupflags);
2706 if (error != 0) {
2707 goto unwind;
2708 }
2709
2710 /* Return success (optionally w/ address for caller). */
2711 VERIFY(error == 0);
2712 (void) ifnet_notify_address(ifp, AF_INET6);
2713
2714 goto done;
2715
2716 unwind:
2717 VERIFY(error != 0);
2718 if (ia != NULL) {
2719 VERIFY(ifa == &ia->ia_ifa);
2720 ifa_remref(ifa);
2721 ia = NULL;
2722 }
2723
2724 done:
2725 *iar = ia;
2726 return error;
2727 }
2728
2729 void
in6_purgeaddr(struct ifaddr * ifa)2730 in6_purgeaddr(struct ifaddr *ifa)
2731 {
2732 struct ifnet *ifp = ifa->ifa_ifp;
2733 struct in6_ifaddr *ia = ifatoia6(ifa);
2734 struct in6_multi_mship *__single imm;
2735
2736 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2737
2738 /* stop DAD processing */
2739 nd6_dad_stop(ifa);
2740
2741 /*
2742 * delete route to the destination of the address being purged.
2743 * The interface must be p2p or loopback in this case.
2744 */
2745 IFA_LOCK(ifa);
2746 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128) {
2747 int error, rtf;
2748
2749 IFA_UNLOCK(ifa);
2750 rtf = (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0;
2751 error = rtinit(&(ia->ia_ifa), RTM_DELETE, rtf);
2752 if (error != 0) {
2753 log(LOG_ERR, "in6_purgeaddr: failed to remove "
2754 "a route to the p2p destination: %s on %s, "
2755 "errno=%d\n",
2756 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
2757 error);
2758 /* proceed anyway... */
2759 }
2760 IFA_LOCK_SPIN(ifa);
2761 ia->ia_flags &= ~IFA_ROUTE;
2762 }
2763 IFA_UNLOCK(ifa);
2764
2765 /* Remove ownaddr's loopback rtentry, if it exists. */
2766 in6_ifremloop(&(ia->ia_ifa));
2767
2768 /*
2769 * leave from multicast groups we have joined for the interface
2770 */
2771 IFA_LOCK(ifa);
2772 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
2773 LIST_REMOVE(imm, i6mm_chain);
2774 IFA_UNLOCK(ifa);
2775 in6_leavegroup(imm);
2776 IFA_LOCK(ifa);
2777 }
2778 IFA_UNLOCK(ifa);
2779
2780 /* in6_unlink_ifa() will need exclusive access */
2781 in6_unlink_ifa(ia, ifp);
2782 in6_post_msg(ifp, KEV_INET6_ADDR_DELETED, ia, NULL, 0);
2783
2784 (void) ifnet_notify_address(ifp, AF_INET6);
2785 }
2786
2787 static void
in6_unlink_ifa(struct in6_ifaddr * ia,struct ifnet * ifp)2788 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
2789 {
2790 struct in6_ifaddr *__single nia;
2791 struct ifaddr *__single ifa;
2792 int unlinked;
2793
2794 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2795
2796 ifa = &ia->ia_ifa;
2797 ifa_addref(ifa);
2798
2799 ifnet_lock_exclusive(ifp);
2800 IFA_LOCK(ifa);
2801 if (ifa->ifa_debug & IFD_ATTACHED) {
2802 if_detach_ifa(ifp, ifa);
2803 }
2804 IFA_UNLOCK(ifa);
2805 ifnet_lock_done(ifp);
2806
2807 unlinked = 0;
2808 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2809 TAILQ_FOREACH(nia, &in6_ifaddrhead, ia6_link) {
2810 if (ia == nia) {
2811 TAILQ_REMOVE(&in6_ifaddrhead, ia, ia6_link);
2812 os_atomic_inc(&in6_ifaddrlist_genid, relaxed);
2813 IFA_LOCK(ifa);
2814 if (IA6_IS_HASHED(ia)) {
2815 in6_iahash_remove(ia);
2816 }
2817 IFA_UNLOCK(ifa);
2818 unlinked = 1;
2819 break;
2820 }
2821 }
2822
2823 /*
2824 * When IPv6 address is being removed, release the
2825 * reference to the base prefix.
2826 * Also, since the release might, affect the status
2827 * of other (detached) addresses, call
2828 * pfxlist_onlink_check().
2829 */
2830 IFA_LOCK(ifa);
2831 /*
2832 * Only log the below message for addresses other than
2833 * link local.
2834 * Only one LLA (auto-configured or statically) is allowed
2835 * on an interface.
2836 * LLA prefix, while added to the prefix list, is not
2837 * reference counted (as it is the only one).
2838 * The prefix also never expires on its own as LLAs
2839 * have infinite lifetime.
2840 *
2841 * For now quiece down the log message for LLAs.
2842 */
2843 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
2844 if (ia->ia6_ndpr == NULL) {
2845 log(LOG_NOTICE, "in6_unlink_ifa: IPv6 address "
2846 "0x%llx has no prefix\n",
2847 (uint64_t)VM_KERNEL_ADDRPERM(ia));
2848 } else {
2849 struct nd_prefix *__single pr = ia->ia6_ndpr;
2850
2851 NDPR_LOCK(pr);
2852 if (!(ia->ia6_flags & IN6_IFF_NOTMANUAL)) {
2853 VERIFY(pr->ndpr_manual_addrcnt != 0);
2854 pr->ndpr_manual_addrcnt--;
2855 }
2856 ia->ia6_flags &= ~IN6_IFF_AUTOCONF;
2857 ia->ia6_ndpr = NULL;
2858 VERIFY(pr->ndpr_addrcnt != 0);
2859 pr->ndpr_addrcnt--;
2860 if (ia->ia6_flags & IN6_IFF_CLAT46) {
2861 pr->ndpr_stateflags &= ~NDPRF_CLAT46;
2862 }
2863 NDPR_UNLOCK(pr);
2864 NDPR_REMREF(pr); /* release addr reference */
2865 }
2866 }
2867 IFA_UNLOCK(ifa);
2868 lck_rw_done(&in6_ifaddr_rwlock);
2869
2870 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
2871 lck_mtx_lock(nd6_mutex);
2872 pfxlist_onlink_check();
2873 lck_mtx_unlock(nd6_mutex);
2874 }
2875 /*
2876 * release another refcnt for the link from in6_ifaddrs.
2877 * Do this only if it's not already unlinked in the event that we lost
2878 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2879 */
2880 if (unlinked) {
2881 ifa_remref(ifa);
2882 }
2883
2884 /* release reference held for this routine */
2885 ifa_remref(ifa);
2886
2887 /* invalidate route caches */
2888 routegenid_inet6_update();
2889 }
2890
2891 void
in6_purgeif(struct ifnet * ifp)2892 in6_purgeif(struct ifnet *ifp)
2893 {
2894 struct in6_ifaddr *__single ia;
2895
2896 if (ifp == NULL) {
2897 return;
2898 }
2899
2900 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2901
2902 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2903 boolean_t from_begining = TRUE;
2904 while (from_begining) {
2905 from_begining = FALSE;
2906 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
2907 if (ia->ia_ifa.ifa_ifp != ifp) {
2908 continue;
2909 }
2910 ifa_addref(&ia->ia_ifa); /* for us */
2911 lck_rw_done(&in6_ifaddr_rwlock);
2912 in6_purgeaddr(&ia->ia_ifa);
2913 ifa_remref(&ia->ia_ifa); /* for us */
2914 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2915 /*
2916 * Purging the address would have caused
2917 * in6_ifaddr_rwlock to be dropped and reacquired;
2918 * therefore search again from the beginning
2919 * of in6_ifaddrs list.
2920 */
2921 from_begining = TRUE;
2922 break;
2923 }
2924 }
2925 lck_rw_done(&in6_ifaddr_rwlock);
2926
2927 in6_ifdetach(ifp);
2928 }
2929
2930 /*
2931 * Initialize an interface's internet6 address and routing table entry.
2932 */
2933 static int
in6_ifinit(struct ifnet * ifp,struct in6_ifaddr * ia,int ifaupflags)2934 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, int ifaupflags)
2935 {
2936 int error;
2937 struct ifaddr *__single ifa;
2938
2939 error = 0;
2940 ifa = &ia->ia_ifa;
2941
2942 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2943 IFA_LOCK(&ia->ia_ifa);
2944 if (IA6_IS_HASHED(ia)) {
2945 in6_iahash_remove(ia);
2946 }
2947 if ((ifp->if_flags & IFF_POINTOPOINT)) {
2948 in6_iahash_insert_ptp(ia);
2949 } else {
2950 in6_iahash_insert(ia);
2951 }
2952 IFA_UNLOCK(&ia->ia_ifa);
2953 lck_rw_done(&in6_ifaddr_rwlock);
2954
2955 /*
2956 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2957 * but here we are sending it down to the interface with a pointer
2958 * to struct ifaddr, for legacy reasons.
2959 */
2960 if ((ifaupflags & IN6_IFAUPDATE_1STADDR) != 0) {
2961 error = ifnet_ioctl(ifp, PF_INET6, SIOCSIFADDR, ia);
2962 if (error != 0) {
2963 if (error != EOPNOTSUPP) {
2964 goto failed;
2965 }
2966 error = 0;
2967 }
2968 }
2969
2970 IFA_LOCK(ifa);
2971
2972 /*
2973 * Special case:
2974 * If the destination address is specified for a point-to-point
2975 * interface, install a route to the destination as an interface
2976 * direct route.
2977 */
2978 if (!(ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128 &&
2979 ia->ia_dstaddr.sin6_family == AF_INET6) {
2980 IFA_UNLOCK(ifa);
2981 error = rtinit(ifa, RTM_ADD, RTF_UP | RTF_HOST);
2982 if (error != 0) {
2983 goto failed;
2984 }
2985 IFA_LOCK(ifa);
2986 ia->ia_flags |= IFA_ROUTE;
2987 }
2988 IFA_LOCK_ASSERT_HELD(ifa);
2989 if (ia->ia_plen < 128) {
2990 /*
2991 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2992 */
2993 ia->ia_flags |= RTF_CLONING;
2994 }
2995
2996 IFA_UNLOCK(ifa);
2997
2998 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2999 if ((ifaupflags & IN6_IFAUPDATE_NEWADDR) != 0) {
3000 in6_ifaddloop(ifa);
3001 }
3002
3003 /* invalidate route caches */
3004 routegenid_inet6_update();
3005
3006 VERIFY(error == 0);
3007 return 0;
3008 failed:
3009 VERIFY(error != 0);
3010 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
3011 IFA_LOCK(&ia->ia_ifa);
3012 if (IA6_IS_HASHED(ia)) {
3013 in6_iahash_remove(ia);
3014 }
3015 IFA_UNLOCK(&ia->ia_ifa);
3016 lck_rw_done(&in6_ifaddr_rwlock);
3017
3018 return error;
3019 }
3020
3021 void
in6_purgeaddrs(struct ifnet * ifp)3022 in6_purgeaddrs(struct ifnet *ifp)
3023 {
3024 in6_purgeif(ifp);
3025 }
3026
3027 /*
3028 * Find an IPv6 interface link-local address specific to an interface.
3029 */
3030 struct in6_ifaddr *
in6ifa_ifpforlinklocal(struct ifnet * ifp,int ignoreflags)3031 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
3032 {
3033 struct ifaddr *__single ifa;
3034
3035 if (ifp == NULL) {
3036 return NULL;
3037 }
3038
3039 ifnet_lock_shared(ifp);
3040 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
3041 {
3042 IFA_LOCK_SPIN(ifa);
3043 if (ifa->ifa_addr->sa_family != AF_INET6) {
3044 IFA_UNLOCK(ifa);
3045 continue;
3046 }
3047 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
3048 if (((ifatoia6(ifa))->ia6_flags &
3049 ignoreflags) != 0) {
3050 IFA_UNLOCK(ifa);
3051 continue;
3052 }
3053 ifa_addref(ifa); /* for caller */
3054 IFA_UNLOCK(ifa);
3055 break;
3056 }
3057 IFA_UNLOCK(ifa);
3058 }
3059 ifnet_lock_done(ifp);
3060
3061 return ifatoia6(ifa);
3062 }
3063
3064 struct in6_ifaddr *
in6ifa_ifpwithflag(struct ifnet * ifp,int flag)3065 in6ifa_ifpwithflag(struct ifnet * ifp, int flag)
3066 {
3067 struct ifaddr *__single ifa;
3068
3069 ifnet_lock_shared(ifp);
3070 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
3071 {
3072 IFA_LOCK_SPIN(ifa);
3073 if (ifa->ifa_addr->sa_family != AF_INET6) {
3074 IFA_UNLOCK(ifa);
3075 continue;
3076 }
3077 if ((ifatoia6(ifa)->ia6_flags & flag) == flag) {
3078 ifa_addref(ifa);
3079 IFA_UNLOCK(ifa);
3080 break;
3081 }
3082 IFA_UNLOCK(ifa);
3083 }
3084 ifnet_lock_done(ifp);
3085
3086 return ifatoia6(ifa);
3087 }
3088
3089 /*
3090 * find the internet address corresponding to a given interface and address.
3091 */
3092 struct in6_ifaddr *
in6ifa_ifpwithaddr(struct ifnet * ifp,struct in6_addr * addr)3093 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
3094 {
3095 struct ifaddr *__single ifa;
3096
3097 ifnet_lock_shared(ifp);
3098 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
3099 {
3100 IFA_LOCK_SPIN(ifa);
3101 if (ifa->ifa_addr->sa_family != AF_INET6) {
3102 IFA_UNLOCK(ifa);
3103 continue;
3104 }
3105 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
3106 ifa_addref(ifa); /* for caller */
3107 IFA_UNLOCK(ifa);
3108 break;
3109 }
3110 IFA_UNLOCK(ifa);
3111 }
3112 ifnet_lock_done(ifp);
3113
3114 return ifatoia6(ifa);
3115 }
3116
3117 struct in6_ifaddr *
in6ifa_prproxyaddr(struct in6_addr * addr,uint32_t ifscope)3118 in6ifa_prproxyaddr(struct in6_addr *addr, uint32_t ifscope)
3119 {
3120 struct in6_ifaddr *__single ia;
3121
3122 lck_rw_lock_shared(&in6_ifaddr_rwlock);
3123 TAILQ_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
3124 IFA_LOCK(&ia->ia_ifa);
3125 if (in6_are_addr_equal_scoped(addr, IFA_IN6(&ia->ia_ifa), ifscope, ia->ia_ifp->if_index)) {
3126 ifa_addref(&ia->ia_ifa); /* for caller */
3127 IFA_UNLOCK(&ia->ia_ifa);
3128 break;
3129 }
3130 IFA_UNLOCK(&ia->ia_ifa);
3131 }
3132 lck_rw_done(&in6_ifaddr_rwlock);
3133
3134 if (ia != NULL && !nd6_prproxy_ifaddr(ia)) {
3135 ifa_remref(&ia->ia_ifa);
3136 ia = NULL;
3137 }
3138
3139 return ia;
3140 }
3141
3142 void
in6ifa_getlifetime(struct in6_ifaddr * ia6,struct in6_addrlifetime * t_dst,int iscalendar)3143 in6ifa_getlifetime(struct in6_ifaddr *ia6, struct in6_addrlifetime *t_dst,
3144 int iscalendar)
3145 {
3146 struct in6_addrlifetime_i *__single t_src = &ia6->ia6_lifetime;
3147 struct timeval caltime;
3148
3149 t_dst->ia6t_vltime = t_src->ia6ti_vltime;
3150 t_dst->ia6t_pltime = t_src->ia6ti_pltime;
3151 t_dst->ia6t_expire = 0;
3152 t_dst->ia6t_preferred = 0;
3153
3154 /* account for system time change */
3155 getmicrotime(&caltime);
3156 t_src->ia6ti_base_calendartime +=
3157 NET_CALCULATE_CLOCKSKEW(caltime,
3158 t_src->ia6ti_base_calendartime, net_uptime(),
3159 t_src->ia6ti_base_uptime);
3160
3161 if (iscalendar) {
3162 if (t_src->ia6ti_expire != 0 &&
3163 t_src->ia6ti_vltime != ND6_INFINITE_LIFETIME) {
3164 t_dst->ia6t_expire = (time_t)(t_src->ia6ti_base_calendartime +
3165 t_src->ia6ti_expire - t_src->ia6ti_base_uptime);
3166 }
3167
3168 if (t_src->ia6ti_preferred != 0 &&
3169 t_src->ia6ti_pltime != ND6_INFINITE_LIFETIME) {
3170 t_dst->ia6t_preferred = (time_t)(t_src->ia6ti_base_calendartime +
3171 t_src->ia6ti_preferred - t_src->ia6ti_base_uptime);
3172 }
3173 } else {
3174 if (t_src->ia6ti_expire != 0 &&
3175 t_src->ia6ti_vltime != ND6_INFINITE_LIFETIME) {
3176 t_dst->ia6t_expire = (time_t)t_src->ia6ti_expire;
3177 }
3178
3179 if (t_src->ia6ti_preferred != 0 &&
3180 t_src->ia6ti_pltime != ND6_INFINITE_LIFETIME) {
3181 t_dst->ia6t_preferred = (time_t)t_src->ia6ti_preferred;
3182 }
3183 }
3184 }
3185
3186 void
in6ifa_setlifetime(struct in6_ifaddr * ia6,struct in6_addrlifetime * t_src)3187 in6ifa_setlifetime(struct in6_ifaddr *ia6, struct in6_addrlifetime *t_src)
3188 {
3189 struct in6_addrlifetime_i *__single t_dst = &ia6->ia6_lifetime;
3190 struct timeval caltime;
3191
3192 /* account for system time change */
3193 getmicrotime(&caltime);
3194 t_dst->ia6ti_base_calendartime +=
3195 NET_CALCULATE_CLOCKSKEW(caltime,
3196 t_dst->ia6ti_base_calendartime, net_uptime(),
3197 t_dst->ia6ti_base_uptime);
3198
3199 /* trust the caller for the values */
3200 t_dst->ia6ti_expire = t_src->ia6t_expire;
3201 t_dst->ia6ti_preferred = t_src->ia6t_preferred;
3202 t_dst->ia6ti_vltime = t_src->ia6t_vltime;
3203 t_dst->ia6ti_pltime = t_src->ia6t_pltime;
3204 }
3205
3206 /*
3207 * Convert IP6 address to printable (loggable) representation.
3208 */
3209 const char *
ip6_sprintf(const struct in6_addr * addr)3210 ip6_sprintf(const struct in6_addr *addr)
3211 {
3212 static const char digits[] = "0123456789abcdef";
3213 static unsigned int ip6round = 0;
3214 static char ip6buf[8][48];
3215
3216 int i;
3217 char *cp;
3218 const u_short *a = (const u_short *)(const struct in6_addr *__bidi_indexable)addr;
3219 const u_char *d;
3220 u_char n;
3221 int dcolon = 0;
3222 int zpad = 0;
3223 uint8_t saved_round = os_atomic_inc_orig(&ip6round, relaxed) & 7;
3224
3225 cp = ip6buf[saved_round];
3226
3227 for (i = 0; i < 8; i++) {
3228 if (dcolon == 1) {
3229 if (*a == 0) {
3230 if (i == 7) {
3231 *cp++ = ':';
3232 }
3233 a++;
3234 continue;
3235 } else {
3236 dcolon = 2;
3237 }
3238 }
3239 if (*a == 0) {
3240 if (dcolon == 0 && i < 7 && *(a + 1) == 0) {
3241 if (i == 0) {
3242 *cp++ = ':';
3243 }
3244 *cp++ = ':';
3245 dcolon = 1;
3246 } else {
3247 *cp++ = '0';
3248 *cp++ = ':';
3249 }
3250 a++;
3251 continue;
3252 }
3253 d = (const u_char *)a;
3254 zpad = 0;
3255 if ((n = *d >> 4) != 0) {
3256 *cp++ = digits[n];
3257 zpad = 1;
3258 }
3259 if ((n = *d++ & 0xf) != 0 || zpad) {
3260 *cp++ = digits[n];
3261 zpad = 1;
3262 }
3263 if ((n = *d >> 4) != 0 || zpad) {
3264 *cp++ = digits[n];
3265 zpad = 1;
3266 }
3267 if ((n = *d & 0xf) != 0 || zpad) {
3268 *cp++ = digits[n];
3269 }
3270 *cp++ = ':';
3271 a++;
3272 }
3273 *--cp = 0;
3274 return __unsafe_null_terminated_from_indexable(ip6buf[saved_round], cp);
3275 }
3276
3277 int
in6addr_local(struct in6_addr * in6)3278 in6addr_local(struct in6_addr *in6)
3279 {
3280 rtentry_ref_t rt;
3281 struct sockaddr_in6 sin6;
3282 int local = 0;
3283
3284 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_SCOPE_LINKLOCAL(in6)) {
3285 return 1;
3286 }
3287
3288 sin6.sin6_family = AF_INET6;
3289 sin6.sin6_len = sizeof(sin6);
3290 bcopy(in6, &sin6.sin6_addr, sizeof(*in6));
3291 rt = rtalloc1(SA(&sin6), 0, 0);
3292
3293 if (rt != NULL) {
3294 RT_LOCK_SPIN(rt);
3295 if (rt->rt_gateway->sa_family == AF_LINK) {
3296 local = 1;
3297 }
3298 RT_UNLOCK(rt);
3299 rtfree(rt);
3300 } else {
3301 local = in6_localaddr(in6);
3302 }
3303 return local;
3304 }
3305
3306 int
in6_localaddr(struct in6_addr * in6)3307 in6_localaddr(struct in6_addr *in6)
3308 {
3309 struct in6_ifaddr *__single ia;
3310
3311 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6) || IN6_IS_ADDR_MC_UNICAST_BASED_LINKLOCAL(in6)) {
3312 return 1;
3313 }
3314
3315 lck_rw_lock_shared(&in6_ifaddr_rwlock);
3316 TAILQ_FOREACH(ia, &in6_ifaddrhead, ia6_link) {
3317 IFA_LOCK_SPIN(&ia->ia_ifa);
3318 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
3319 &ia->ia_prefixmask.sin6_addr)) {
3320 IFA_UNLOCK(&ia->ia_ifa);
3321 lck_rw_done(&in6_ifaddr_rwlock);
3322 return 1;
3323 }
3324 IFA_UNLOCK(&ia->ia_ifa);
3325 }
3326 lck_rw_done(&in6_ifaddr_rwlock);
3327 return 0;
3328 }
3329
3330 /*
3331 * return length of part which dst and src are equal
3332 * hard coding...
3333 */
3334 int
in6_matchlen(struct in6_addr * src,struct in6_addr * dst)3335 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
3336 {
3337 int match = 0;
3338 u_char *__bidi_indexable s = (u_char *)(struct in6_addr *__bidi_indexable)src;
3339 u_char *__bidi_indexable d = (u_char *)(struct in6_addr *__bidi_indexable)dst;
3340 u_char *lim = s + 16, r;
3341
3342
3343 while (s < lim) {
3344 if ((r = (*d++ ^ *s++)) != 0) {
3345 while (r < 128) {
3346 match++;
3347 r = (u_char)(r << 1);
3348 }
3349 break;
3350 } else {
3351 match += 8;
3352 }
3353 }
3354 return match;
3355 }
3356
3357 /* XXX: to be scope conscious */
3358 int
in6_are_prefix_equal(struct in6_addr * p1,uint32_t ifscope1,struct in6_addr * p2,uint32_t ifscope2,int len)3359 in6_are_prefix_equal(struct in6_addr *p1, uint32_t ifscope1, struct in6_addr *p2, uint32_t ifscope2, int len)
3360 {
3361 int bytelen, bitlen;
3362
3363 /* sanity check */
3364 if (0 > len || len > 128) {
3365 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len);
3366 return 0;
3367 }
3368
3369 bytelen = len / 8;
3370 bitlen = len % 8;
3371
3372 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) {
3373 return 0;
3374 }
3375 if (bitlen != 0 &&
3376 p1->s6_addr[bytelen] >> (8 - bitlen) !=
3377 p2->s6_addr[bytelen] >> (8 - bitlen)) {
3378 return 0;
3379 }
3380
3381 if (IN6_IS_SCOPE_EMBED(p1) && !in6_embedded_scope) {
3382 return ifscope1 == ifscope2;
3383 }
3384
3385 return 1;
3386 }
3387
3388 void
in6_prefixlen2mask(struct in6_addr * maskp,int len)3389 in6_prefixlen2mask(struct in6_addr *maskp, int len)
3390 {
3391 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
3392 int bytelen, bitlen, i;
3393
3394 /* sanity check */
3395 if (0 > len || len > 128) {
3396 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len);
3397 return;
3398 }
3399
3400 bzero(maskp, sizeof(*maskp));
3401 bytelen = len / 8;
3402 bitlen = len % 8;
3403 for (i = 0; i < bytelen; i++) {
3404 maskp->s6_addr[i] = 0xff;
3405 }
3406 if (bitlen) {
3407 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
3408 }
3409 }
3410
3411 /*
3412 * return the best address out of the same scope
3413 */
3414 struct in6_ifaddr *
in6_ifawithscope(struct ifnet * oifp,struct in6_addr * dst)3415 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
3416 {
3417 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
3418 int blen = -1;
3419 struct ifaddr *__single ifa;
3420 ifnet_ref_t ifp;
3421 struct in6_ifaddr *__single ifa_best = NULL;
3422
3423 if (oifp == NULL) {
3424 return NULL;
3425 }
3426
3427 /*
3428 * We search for all addresses on all interfaces from the beginning.
3429 * Comparing an interface with the outgoing interface will be done
3430 * only at the final stage of tiebreaking.
3431 */
3432 ifnet_head_lock_shared();
3433 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
3434 /*
3435 * We can never take an address that breaks the scope zone
3436 * of the destination.
3437 */
3438 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) {
3439 continue;
3440 }
3441
3442 ifnet_lock_shared(ifp);
3443 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3444 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
3445
3446 IFA_LOCK(ifa);
3447 if (ifa->ifa_addr->sa_family != AF_INET6) {
3448 IFA_UNLOCK(ifa);
3449 continue;
3450 }
3451 src_scope = in6_addrscope(IFA_IN6(ifa));
3452
3453 /*
3454 * Don't use an address before completing DAD
3455 * nor a duplicated address.
3456 */
3457 if ((ifatoia6(ifa))->ia6_flags &
3458 (IN6_IFF_NOTREADY | IN6_IFF_CLAT46)) {
3459 IFA_UNLOCK(ifa);
3460 continue;
3461 }
3462 /* XXX: is there any case to allow anycasts? */
3463 if ((ifatoia6(ifa))->ia6_flags &
3464 IN6_IFF_ANYCAST) {
3465 IFA_UNLOCK(ifa);
3466 continue;
3467 }
3468 if ((ifatoia6(ifa))->ia6_flags &
3469 IN6_IFF_DETACHED) {
3470 IFA_UNLOCK(ifa);
3471 continue;
3472 }
3473 /*
3474 * If this is the first address we find,
3475 * keep it anyway.
3476 */
3477 if (ifa_best == NULL) {
3478 goto replace;
3479 }
3480
3481 /*
3482 * ifa_best is never NULL beyond this line except
3483 * within the block labeled "replace".
3484 */
3485
3486 /*
3487 * If ifa_best has a smaller scope than dst and
3488 * the current address has a larger one than
3489 * (or equal to) dst, always replace ifa_best.
3490 * Also, if the current address has a smaller scope
3491 * than dst, ignore it unless ifa_best also has a
3492 * smaller scope.
3493 * Consequently, after the two if-clause below,
3494 * the followings must be satisfied:
3495 * (scope(src) < scope(dst) &&
3496 * scope(best) < scope(dst))
3497 * OR
3498 * (scope(best) >= scope(dst) &&
3499 * scope(src) >= scope(dst))
3500 */
3501 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
3502 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) {
3503 goto replace; /* (A) */
3504 }
3505 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
3506 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) {
3507 IFA_UNLOCK(ifa);
3508 continue; /* (B) */
3509 }
3510 /*
3511 * A deprecated address SHOULD NOT be used in new
3512 * communications if an alternate (non-deprecated)
3513 * address is available and has sufficient scope.
3514 * RFC 4862, Section 5.5.4.
3515 */
3516 if ((ifatoia6(ifa))->ia6_flags &
3517 IN6_IFF_DEPRECATED) {
3518 /*
3519 * Ignore any deprecated addresses if
3520 * specified by configuration.
3521 */
3522 if (!ip6_use_deprecated) {
3523 IFA_UNLOCK(ifa);
3524 continue;
3525 }
3526 /*
3527 * If we have already found a non-deprecated
3528 * candidate, just ignore deprecated addresses.
3529 */
3530 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
3531 == 0) {
3532 IFA_UNLOCK(ifa);
3533 continue;
3534 }
3535 }
3536
3537 /*
3538 * A non-deprecated address is always preferred
3539 * to a deprecated one regardless of scopes and
3540 * address matching (Note invariants ensured by the
3541 * conditions (A) and (B) above.)
3542 */
3543 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
3544 ((ifatoia6(ifa))->ia6_flags &
3545 IN6_IFF_DEPRECATED) == 0) {
3546 goto replace;
3547 }
3548
3549 /*
3550 * When we use temporary addresses described in
3551 * RFC 4941, we prefer temporary addresses to
3552 * public autoconf addresses. Again, note the
3553 * invariants from (A) and (B). Also note that we
3554 * don't have any preference between static addresses
3555 * and autoconf addresses (despite of whether or not
3556 * the latter is temporary or public.)
3557 */
3558 if (ip6_use_tempaddr) {
3559 struct in6_ifaddr *__single ifat;
3560
3561 ifat = ifatoia6(ifa);
3562 if ((ifa_best->ia6_flags &
3563 (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY))
3564 == IN6_IFF_AUTOCONF &&
3565 (ifat->ia6_flags &
3566 (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY))
3567 == (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY)) {
3568 goto replace;
3569 }
3570 if ((ifa_best->ia6_flags &
3571 (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY))
3572 == (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY) &&
3573 (ifat->ia6_flags &
3574 (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY))
3575 == IN6_IFF_AUTOCONF) {
3576 IFA_UNLOCK(ifa);
3577 continue;
3578 }
3579 }
3580
3581 /*
3582 * At this point, we have two cases:
3583 * 1. we are looking at a non-deprecated address,
3584 * and ifa_best is also non-deprecated.
3585 * 2. we are looking at a deprecated address,
3586 * and ifa_best is also deprecated.
3587 * Also, we do not have to consider a case where
3588 * the scope of if_best is larger(smaller) than dst and
3589 * the scope of the current address is smaller(larger)
3590 * than dst. Such a case has already been covered.
3591 * Tiebreaking is done according to the following
3592 * items:
3593 * - the scope comparison between the address and
3594 * dst (dscopecmp)
3595 * - the scope comparison between the address and
3596 * ifa_best (bscopecmp)
3597 * - if the address match dst longer than ifa_best
3598 * (matchcmp)
3599 * - if the address is on the outgoing I/F (outI/F)
3600 *
3601 * Roughly speaking, the selection policy is
3602 * - the most important item is scope. The same scope
3603 * is best. Then search for a larger scope.
3604 * Smaller scopes are the last resort.
3605 * - A deprecated address is chosen only when we have
3606 * no address that has an enough scope, but is
3607 * prefered to any addresses of smaller scopes
3608 * (this must be already done above.)
3609 * - addresses on the outgoing I/F are preferred to
3610 * ones on other interfaces if none of above
3611 * tiebreaks. In the table below, the column "bI"
3612 * means if the best_ifa is on the outgoing
3613 * interface, and the column "sI" means if the ifa
3614 * is on the outgoing interface.
3615 * - If there is no other reasons to choose one,
3616 * longest address match against dst is considered.
3617 *
3618 * The precise decision table is as follows:
3619 * dscopecmp bscopecmp match bI oI | replace?
3620 * N/A equal N/A Y N | No (1)
3621 * N/A equal N/A N Y | Yes (2)
3622 * N/A equal larger N/A | Yes (3)
3623 * N/A equal !larger N/A | No (4)
3624 * larger larger N/A N/A | No (5)
3625 * larger smaller N/A N/A | Yes (6)
3626 * smaller larger N/A N/A | Yes (7)
3627 * smaller smaller N/A N/A | No (8)
3628 * equal smaller N/A N/A | Yes (9)
3629 * equal larger (already done at A above)
3630 */
3631 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
3632 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
3633
3634 if (bscopecmp == 0) {
3635 ifnet_ref_t bifp = ifa_best->ia_ifp;
3636
3637 if (bifp == oifp && ifp != oifp) { /* (1) */
3638 IFA_UNLOCK(ifa);
3639 continue;
3640 }
3641 if (bifp != oifp && ifp == oifp) { /* (2) */
3642 goto replace;
3643 }
3644
3645 /*
3646 * Both bifp and ifp are on the outgoing
3647 * interface, or both two are on a different
3648 * interface from the outgoing I/F.
3649 * now we need address matching against dst
3650 * for tiebreaking.
3651 */
3652 tlen = in6_matchlen(IFA_IN6(ifa), dst);
3653 matchcmp = tlen - blen;
3654 if (matchcmp > 0) { /* (3) */
3655 goto replace;
3656 }
3657 IFA_UNLOCK(ifa);
3658 continue; /* (4) */
3659 }
3660 if (dscopecmp > 0) {
3661 if (bscopecmp > 0) { /* (5) */
3662 IFA_UNLOCK(ifa);
3663 continue;
3664 }
3665 goto replace; /* (6) */
3666 }
3667 if (dscopecmp < 0) {
3668 if (bscopecmp > 0) { /* (7) */
3669 goto replace;
3670 }
3671 IFA_UNLOCK(ifa);
3672 continue; /* (8) */
3673 }
3674
3675 /* now dscopecmp must be 0 */
3676 if (bscopecmp < 0) {
3677 goto replace; /* (9) */
3678 }
3679 replace:
3680 ifa_addref(ifa); /* for ifa_best */
3681 blen = tlen >= 0 ? tlen :
3682 in6_matchlen(IFA_IN6(ifa), dst);
3683 best_scope =
3684 in6_addrscope(&ifatoia6(ifa)->ia_addr.sin6_addr);
3685 IFA_UNLOCK(ifa);
3686 if (ifa_best) {
3687 ifa_remref(&ifa_best->ia_ifa);
3688 }
3689 ifa_best = ifatoia6(ifa);
3690 }
3691 ifnet_lock_done(ifp);
3692 }
3693 ifnet_head_done();
3694
3695 /* count statistics for future improvements */
3696 if (ifa_best == NULL) {
3697 ip6stat.ip6s_sources_none++;
3698 } else {
3699 IFA_LOCK_SPIN(&ifa_best->ia_ifa);
3700 if (oifp == ifa_best->ia_ifp) {
3701 ip6stat.ip6s_sources_sameif[best_scope]++;
3702 } else {
3703 ip6stat.ip6s_sources_otherif[best_scope]++;
3704 }
3705
3706 if (best_scope == dst_scope) {
3707 ip6stat.ip6s_sources_samescope[best_scope]++;
3708 } else {
3709 ip6stat.ip6s_sources_otherscope[best_scope]++;
3710 }
3711
3712 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
3713 ip6stat.ip6s_sources_deprecated[best_scope]++;
3714 }
3715 IFA_UNLOCK(&ifa_best->ia_ifa);
3716 }
3717
3718 return ifa_best;
3719 }
3720
3721 /*
3722 * return the best address out of the same scope. if no address was
3723 * found, return the first valid address from designated IF.
3724 */
3725 struct in6_ifaddr *
in6_ifawithifp(struct ifnet * ifp,struct in6_addr * dst)3726 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
3727 {
3728 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
3729 struct ifaddr *__single ifa;
3730 struct in6_ifaddr *__single besta = NULL;
3731 struct in6_ifaddr *__single dep[2]; /* last-resort: deprecated */
3732
3733 dep[0] = dep[1] = NULL;
3734
3735 /*
3736 * We first look for addresses in the same scope.
3737 * If there is one, return it.
3738 * If two or more, return one which matches the dst longest.
3739 * If none, return one of global addresses assigned other ifs.
3740 */
3741 ifnet_lock_shared(ifp);
3742 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3743 IFA_LOCK(ifa);
3744 if (ifa->ifa_addr->sa_family != AF_INET6) {
3745 IFA_UNLOCK(ifa);
3746 continue;
3747 }
3748 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) {
3749 IFA_UNLOCK(ifa);
3750 continue; /* XXX: is there any case to allow anycast? */
3751 }
3752 if (ifatoia6(ifa)->ia6_flags & (IN6_IFF_NOTREADY | IN6_IFF_CLAT46)) {
3753 IFA_UNLOCK(ifa);
3754 continue; /* don't use this interface */
3755 }
3756 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DETACHED) {
3757 IFA_UNLOCK(ifa);
3758 continue;
3759 }
3760 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
3761 if (ip6_use_deprecated) {
3762 ifa_addref(ifa); /* for dep[0] */
3763 IFA_UNLOCK(ifa);
3764 if (dep[0] != NULL) {
3765 ifa_remref(&dep[0]->ia_ifa);
3766 }
3767 dep[0] = ifatoia6(ifa);
3768 } else {
3769 IFA_UNLOCK(ifa);
3770 }
3771 continue;
3772 }
3773
3774 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
3775 /*
3776 * call in6_matchlen() as few as possible
3777 */
3778 if (besta) {
3779 if (blen == -1) {
3780 IFA_UNLOCK(ifa);
3781 IFA_LOCK(&besta->ia_ifa);
3782 blen = in6_matchlen(
3783 &besta->ia_addr.sin6_addr, (struct in6_addr*__indexable)dst);
3784 IFA_UNLOCK(&besta->ia_ifa);
3785 IFA_LOCK(ifa);
3786 }
3787 tlen = in6_matchlen(IFA_IN6(ifa), (struct in6_addr*__indexable)dst);
3788 if (tlen > blen) {
3789 blen = tlen;
3790 ifa_addref(ifa); /* for besta */
3791 IFA_UNLOCK(ifa);
3792 ifa_remref(&besta->ia_ifa);
3793 besta = ifatoia6(ifa);
3794 } else {
3795 IFA_UNLOCK(ifa);
3796 }
3797 } else {
3798 besta = ifatoia6(ifa);
3799 ifa_addref(ifa); /* for besta */
3800 IFA_UNLOCK(ifa);
3801 }
3802 } else {
3803 IFA_UNLOCK(ifa);
3804 }
3805 }
3806 if (besta) {
3807 ifnet_lock_done(ifp);
3808 if (dep[0] != NULL) {
3809 ifa_remref(&dep[0]->ia_ifa);
3810 }
3811 return besta;
3812 }
3813
3814 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3815 IFA_LOCK(ifa);
3816 if (ifa->ifa_addr->sa_family != AF_INET6) {
3817 IFA_UNLOCK(ifa);
3818 continue;
3819 }
3820 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) {
3821 IFA_UNLOCK(ifa);
3822 continue; /* XXX: is there any case to allow anycast? */
3823 }
3824 if (ifatoia6(ifa)->ia6_flags & (IN6_IFF_NOTREADY | IN6_IFF_CLAT46)) {
3825 IFA_UNLOCK(ifa);
3826 continue; /* don't use this interface */
3827 }
3828 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DETACHED) {
3829 IFA_UNLOCK(ifa);
3830 continue;
3831 }
3832 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
3833 if (ip6_use_deprecated) {
3834 ifa_addref(ifa); /* for dep[1] */
3835 IFA_UNLOCK(ifa);
3836 if (dep[1] != NULL) {
3837 ifa_remref(&dep[1]->ia_ifa);
3838 }
3839 dep[1] = ifatoia6(ifa);
3840 } else {
3841 IFA_UNLOCK(ifa);
3842 }
3843 continue;
3844 }
3845 ifa_addref(ifa); /* for caller */
3846 IFA_UNLOCK(ifa);
3847 ifnet_lock_done(ifp);
3848 if (dep[0] != NULL) {
3849 ifa_remref(&dep[0]->ia_ifa);
3850 }
3851 if (dep[1] != NULL) {
3852 ifa_remref(&dep[1]->ia_ifa);
3853 }
3854 return ifatoia6(ifa);
3855 }
3856 ifnet_lock_done(ifp);
3857
3858 /* use the last-resort values, that are, deprecated addresses */
3859 if (dep[0]) {
3860 if (dep[1] != NULL) {
3861 ifa_remref(&dep[1]->ia_ifa);
3862 }
3863 return dep[0];
3864 }
3865 if (dep[1]) {
3866 return dep[1];
3867 }
3868
3869 return NULL;
3870 }
3871
3872 /*
3873 * perform DAD when interface becomes IFF_UP.
3874 */
3875 static void
in6_if_up_dad_start(struct ifnet * ifp)3876 in6_if_up_dad_start(struct ifnet *ifp)
3877 {
3878 struct ifaddr *__single ifa;
3879 struct nd_ifinfo *__single ndi = NULL;
3880
3881 ndi = ND_IFINFO(ifp);
3882 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
3883 if (!(ndi->flags & ND6_IFF_DAD)) {
3884 return;
3885 }
3886
3887 /* start DAD on all the interface addresses */
3888 ifnet_lock_exclusive(ifp);
3889 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3890 struct in6_ifaddr *ia6;
3891
3892 IFA_LOCK_SPIN(ifa);
3893 if (ifa->ifa_addr->sa_family != AF_INET6) {
3894 IFA_UNLOCK(ifa);
3895 continue;
3896 }
3897 ia6 = ifatoia6(ifa);
3898 if (ia6->ia6_flags & IN6_IFF_DADPROGRESS) {
3899 int delay = 0; /* delay ticks before DAD output */
3900 IFA_UNLOCK(ifa);
3901 nd6_dad_start(ifa, &delay);
3902 } else {
3903 IFA_UNLOCK(ifa);
3904 }
3905 }
3906 ifnet_lock_done(ifp);
3907 }
3908
3909 int
in6if_do_dad(struct ifnet * ifp)3910 in6if_do_dad(
3911 struct ifnet *ifp)
3912 {
3913 struct nd_ifinfo *__single ndi = NULL;
3914
3915 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
3916 return 0;
3917 }
3918
3919 ndi = ND_IFINFO(ifp);
3920 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
3921 if (!(ndi->flags & ND6_IFF_DAD)) {
3922 return 0;
3923 }
3924
3925 /*
3926 * If we are using the alternative neighbor discovery
3927 * interface on this interface, then skip DAD.
3928 *
3929 * Also, skip it for interfaces marked "local private"
3930 * for now, even when not marked as using the alternative
3931 * interface. This is for historical reasons.
3932 */
3933 if (ifp->if_eflags &
3934 (IFEF_IPV6_ND6ALT | IFEF_LOCALNET_PRIVATE | IFEF_DIRECTLINK)) {
3935 return 0;
3936 }
3937
3938 if (ifp->if_family == IFNET_FAMILY_IPSEC ||
3939 ifp->if_family == IFNET_FAMILY_UTUN) {
3940 /*
3941 * Ignore DAD for tunneling virtual interfaces, which get
3942 * their IPv6 address explicitly assigned.
3943 */
3944 return 0;
3945 }
3946
3947 switch (ifp->if_type) {
3948 #if IFT_DUMMY
3949 case IFT_DUMMY:
3950 #endif
3951 case IFT_FAITH:
3952 /*
3953 * These interfaces do not have the IFF_LOOPBACK flag,
3954 * but loop packets back. We do not have to do DAD on such
3955 * interfaces. We should even omit it, because loop-backed
3956 * NS would confuse the DAD procedure.
3957 */
3958 return 0;
3959 default:
3960 /*
3961 * Our DAD routine requires the interface up and running.
3962 * However, some interfaces can be up before the RUNNING
3963 * status. Additionally, users may try to assign addresses
3964 * before the interface becomes up (or running).
3965 * We simply skip DAD in such a case as a work around.
3966 * XXX: we should rather mark "tentative" on such addresses,
3967 * and do DAD after the interface becomes ready.
3968 */
3969 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
3970 (IFF_UP | IFF_RUNNING)) {
3971 return 0;
3972 }
3973
3974 return 1;
3975 }
3976 }
3977
3978 /*
3979 * Calculate max IPv6 MTU through all the interfaces and store it
3980 * to in6_maxmtu.
3981 */
3982 void
in6_setmaxmtu(void)3983 in6_setmaxmtu(void)
3984 {
3985 u_int32_t maxmtu = 0;
3986 ifnet_ref_t ifp;
3987
3988 ifnet_head_lock_shared();
3989 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
3990 struct nd_ifinfo *__single ndi = NULL;
3991
3992 if ((ndi = ND_IFINFO(ifp)) != NULL && !ndi->initialized) {
3993 ndi = NULL;
3994 }
3995 if (ndi != NULL) {
3996 lck_mtx_lock(&ndi->lock);
3997 }
3998 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
3999 IN6_LINKMTU(ifp) > maxmtu) {
4000 maxmtu = IN6_LINKMTU(ifp);
4001 }
4002 if (ndi != NULL) {
4003 lck_mtx_unlock(&ndi->lock);
4004 }
4005 }
4006 ifnet_head_done();
4007 if (maxmtu) { /* update only when maxmtu is positive */
4008 in6_maxmtu = maxmtu;
4009 }
4010 }
4011 /*
4012 * Provide the length of interface identifiers to be used for the link attached
4013 * to the given interface. The length should be defined in "IPv6 over
4014 * xxx-link" document. Note that address architecture might also define
4015 * the length for a particular set of address prefixes, regardless of the
4016 * link type. Also see RFC 4862 for additional background.
4017 */
4018 int
in6_if2idlen(struct ifnet * ifp)4019 in6_if2idlen(struct ifnet *ifp)
4020 {
4021 switch (ifp->if_type) {
4022 case IFT_ETHER: /* RFC2464 */
4023 case IFT_IEEE8023ADLAG: /* IEEE802.3ad Link Aggregate */
4024 #ifdef IFT_PROPVIRTUAL
4025 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
4026 #endif
4027 #ifdef IFT_L2VLAN
4028 case IFT_L2VLAN: /* ditto */
4029 #endif
4030 #ifdef IFT_IEEE80211
4031 case IFT_IEEE80211: /* ditto */
4032 #endif
4033 #ifdef IFT_MIP
4034 case IFT_MIP: /* ditto */
4035 #endif
4036 return 64;
4037 case IFT_FDDI: /* RFC2467 */
4038 return 64;
4039 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
4040 return 64;
4041 case IFT_PPP: /* RFC2472 */
4042 return 64;
4043 case IFT_ARCNET: /* RFC2497 */
4044 return 64;
4045 case IFT_FRELAY: /* RFC2590 */
4046 return 64;
4047 case IFT_IEEE1394: /* RFC3146 */
4048 return 64;
4049 case IFT_GIF:
4050 return 64; /* draft-ietf-v6ops-mech-v2-07 */
4051 case IFT_LOOP:
4052 return 64; /* XXX: is this really correct? */
4053 case IFT_OTHER:
4054 return 64; /* for utun interfaces */
4055 case IFT_CELLULAR:
4056 return 64; /* Packet Data over Cellular */
4057 case IFT_BRIDGE:
4058 return 64; /* Transparent bridge interface */
4059 default:
4060 /*
4061 * Unknown link type:
4062 * It might be controversial to use the today's common constant
4063 * of 64 for these cases unconditionally. For full compliance,
4064 * we should return an error in this case. On the other hand,
4065 * if we simply miss the standard for the link type or a new
4066 * standard is defined for a new link type, the IFID length
4067 * is very likely to be the common constant. As a compromise,
4068 * we always use the constant, but make an explicit notice
4069 * indicating the "unknown" case.
4070 */
4071 log(LOG_NOTICE, "%s: unknown link type (%d)\n", __func__,
4072 ifp->if_type);
4073 return 64;
4074 }
4075 }
4076 /*
4077 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
4078 * v4 mapped addr or v4 compat addr
4079 */
4080 void
in6_sin6_2_sin(struct sockaddr_in * sin,struct sockaddr_in6 * sin6)4081 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
4082 {
4083 SOCKADDR_ZERO(sin, sizeof(*sin));
4084 sin->sin_len = sizeof(struct sockaddr_in);
4085 sin->sin_family = AF_INET;
4086 sin->sin_port = sin6->sin6_port;
4087 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
4088 }
4089
4090 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
4091 void
in6_sin_2_v4mapsin6(struct sockaddr_in * sin,struct sockaddr_in6 * sin6)4092 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
4093 {
4094 SOCKADDR_ZERO(sin6, sizeof(*sin6));
4095 sin6->sin6_len = sizeof(struct sockaddr_in6);
4096 sin6->sin6_family = AF_INET6;
4097 sin6->sin6_port = sin->sin_port;
4098 sin6->sin6_addr.s6_addr32[0] = 0;
4099 sin6->sin6_addr.s6_addr32[1] = 0;
4100 if (sin->sin_addr.s_addr) {
4101 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
4102 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
4103 } else {
4104 sin6->sin6_addr.s6_addr32[2] = 0;
4105 sin6->sin6_addr.s6_addr32[3] = 0;
4106 }
4107 }
4108
4109 /* Convert sockaddr_in6 into sockaddr_in. */
4110 void
in6_sin6_2_sin_in_sock(struct sockaddr * nam)4111 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
4112 {
4113 struct sockaddr_in *__single sin_p;
4114 struct sockaddr_in6 sin6;
4115
4116 /*
4117 * Save original sockaddr_in6 addr and convert it
4118 * to sockaddr_in.
4119 */
4120 sin6 = *SIN6(nam);
4121 sin_p = SIN(nam);
4122 in6_sin6_2_sin(sin_p, &sin6);
4123 }
4124
4125 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
4126 int
in6_sin_2_v4mapsin6_in_sock(struct sockaddr ** nam)4127 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
4128 {
4129 struct sockaddr_in *__single sin_p;
4130 struct sockaddr_in6 *__single sin6_p;
4131
4132 sin6_p = SIN6(alloc_sockaddr(sizeof(*sin6_p),
4133 Z_WAITOK | Z_NOFAIL));
4134
4135 sin_p = SIN(*nam);
4136 in6_sin_2_v4mapsin6(sin_p, sin6_p);
4137 free_sockaddr(*nam);
4138 *nam = SA(sin6_p);
4139
4140 return 0;
4141 }
4142
4143 /*
4144 * Posts in6_event_data message kernel events.
4145 *
4146 * To get the same size of kev_in6_data between ILP32 and LP64 data models
4147 * we are using a special version of the in6_addrlifetime structure that
4148 * uses only 32 bits fields to be compatible with Leopard, and that
4149 * are large enough to span 68 years.
4150 */
4151 void
in6_post_msg(struct ifnet * ifp,u_int32_t event_code,struct in6_ifaddr * ifa,uint8_t * __sized_by (maclen)mac,size_t maclen)4152 in6_post_msg(struct ifnet *ifp, u_int32_t event_code, struct in6_ifaddr *ifa,
4153 uint8_t *__sized_by(maclen)mac, size_t maclen)
4154 {
4155 struct kev_msg ev_msg;
4156 struct kev_in6_data in6_event_data;
4157 struct in6_addrlifetime ia6_lt;
4158
4159 bzero(&in6_event_data, sizeof(struct kev_in6_data));
4160 bzero(&ev_msg, sizeof(struct kev_msg));
4161 ev_msg.vendor_code = KEV_VENDOR_APPLE;
4162 ev_msg.kev_class = KEV_NETWORK_CLASS;
4163 ev_msg.kev_subclass = KEV_INET6_SUBCLASS;
4164 ev_msg.event_code = event_code;
4165
4166 if (ifa) {
4167 IFA_LOCK(&ifa->ia_ifa);
4168 in6_event_data.ia_addr = ifa->ia_addr;
4169 in6_event_data.ia_net = ifa->ia_net;
4170 in6_event_data.ia_dstaddr = ifa->ia_dstaddr;
4171 in6_event_data.ia_prefixmask = ifa->ia_prefixmask;
4172 in6_event_data.ia_plen = ifa->ia_plen;
4173 in6_event_data.ia6_flags = (u_int32_t)ifa->ia6_flags;
4174
4175 /* retrieve time as calendar time (last arg is 1) */
4176 in6ifa_getlifetime(ifa, &ia6_lt, 1);
4177 in6_event_data.ia_lifetime.ia6t_expire = (u_int32_t)ia6_lt.ia6t_expire;
4178 in6_event_data.ia_lifetime.ia6t_preferred = (u_int32_t)ia6_lt.ia6t_preferred;
4179 in6_event_data.ia_lifetime.ia6t_vltime = ia6_lt.ia6t_vltime;
4180 in6_event_data.ia_lifetime.ia6t_pltime = ia6_lt.ia6t_pltime;
4181 IFA_UNLOCK(&ifa->ia_ifa);
4182 }
4183
4184 if (ifp != NULL) {
4185 (void) strlcpy(&in6_event_data.link_data.if_name[0],
4186 ifp->if_name, IFNAMSIZ);
4187 in6_event_data.link_data.if_family = ifp->if_family;
4188 in6_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
4189 }
4190
4191 if (mac != NULL) {
4192 VERIFY(maclen <= sizeof(in6_event_data.ia_mac));
4193 memcpy(&in6_event_data.ia_mac, mac, maclen);
4194 }
4195
4196 ev_msg.dv[0].data_ptr = &in6_event_data;
4197 ev_msg.dv[0].data_length = sizeof(in6_event_data);
4198 ev_msg.dv[1].data_length = 0;
4199
4200 dlil_post_complete_msg(NULL, &ev_msg);
4201 }
4202
4203 /*
4204 * Called as part of ip6_init
4205 */
4206 void
in6_ifaddr_init(void)4207 in6_ifaddr_init(void)
4208 {
4209 in6_cga_init();
4210 }
4211
4212 static struct in6_ifaddr *
in6_ifaddr_alloc(zalloc_flags_t how)4213 in6_ifaddr_alloc(zalloc_flags_t how)
4214 {
4215 struct in6_ifaddr *__single in6ifa;
4216
4217 in6ifa = kalloc_type(struct in6_ifaddr, Z_ZERO | how);
4218 if (in6ifa == NULL) {
4219 return NULL;
4220 }
4221
4222 in6ifa->ia_ifa.ifa_free = in6_ifaddr_free;
4223 in6ifa->ia_ifa.ifa_debug |= IFD_ALLOC;
4224 in6ifa->ia_ifa.ifa_del_wc = &in6ifa->ia_ifa.ifa_debug;
4225 in6ifa->ia_ifa.ifa_del_waiters = 0;
4226 ifa_lock_init(&in6ifa->ia_ifa);
4227 ifa_initref(&in6ifa->ia_ifa);
4228
4229 return in6ifa;
4230 }
4231
4232 static void
in6_ifaddr_free(struct ifaddr * ifa)4233 in6_ifaddr_free(struct ifaddr *ifa)
4234 {
4235 struct in6_ifaddr *in6ifa = ifatoia6(ifa);
4236
4237 IFA_LOCK_ASSERT_HELD(ifa);
4238
4239 if (!(ifa->ifa_debug & IFD_ALLOC)) {
4240 panic("%s: ifa %p cannot be freed", __func__, ifa);
4241 /* NOTREACHED */
4242 }
4243 IFA_UNLOCK(ifa);
4244 ifa_lock_destroy(ifa);
4245
4246 kfree_type(struct in6_ifaddr, in6ifa);
4247 }
4248
4249 /*
4250 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
4251 */
4252 static int
in6_getassocids(struct socket * so,uint32_t * cnt,user_addr_t aidp)4253 in6_getassocids(struct socket *so, uint32_t *cnt, user_addr_t aidp)
4254 {
4255 struct in6pcb *__single in6p = sotoin6pcb(so);
4256 sae_associd_t aid;
4257
4258 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD) {
4259 return EINVAL;
4260 }
4261
4262 /* IN6PCB has no concept of association */
4263 aid = SAE_ASSOCID_ANY;
4264 *cnt = 0;
4265
4266 /* just asking how many there are? */
4267 if (aidp == USER_ADDR_NULL) {
4268 return 0;
4269 }
4270
4271 return copyout(&aid, aidp, sizeof(aid));
4272 }
4273
4274 /*
4275 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
4276 */
4277 static int
in6_getconnids(struct socket * so,sae_associd_t aid,uint32_t * cnt,user_addr_t cidp)4278 in6_getconnids(struct socket *so, sae_associd_t aid, uint32_t *cnt,
4279 user_addr_t cidp)
4280 {
4281 struct in6pcb *__single in6p = sotoin6pcb(so);
4282 sae_connid_t cid;
4283
4284 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD) {
4285 return EINVAL;
4286 }
4287
4288 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
4289 return EINVAL;
4290 }
4291
4292 /* if connected, return 1 connection count */
4293 *cnt = ((so->so_state & SS_ISCONNECTED) ? 1 : 0);
4294
4295 /* just asking how many there are? */
4296 if (cidp == USER_ADDR_NULL) {
4297 return 0;
4298 }
4299
4300 /* if IN6PCB is connected, assign it connid 1 */
4301 cid = ((*cnt != 0) ? 1 : SAE_CONNID_ANY);
4302
4303 return copyout(&cid, cidp, sizeof(cid));
4304 }
4305
4306 /*
4307 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
4308 */
4309 int
in6_getconninfo(struct socket * so,sae_connid_t cid,uint32_t * flags,uint32_t * ifindex,int32_t * soerror,user_addr_t src,socklen_t * src_len,user_addr_t dst,socklen_t * dst_len,uint32_t * aux_type,user_addr_t aux_data,uint32_t * aux_len)4310 in6_getconninfo(struct socket *so, sae_connid_t cid, uint32_t *flags,
4311 uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len,
4312 user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type,
4313 user_addr_t aux_data, uint32_t *aux_len)
4314 {
4315 struct in6pcb *__single in6p = sotoin6pcb(so);
4316 struct sockaddr_in6 sin6;
4317 struct ifnet *ifp = NULL;
4318 int error = 0;
4319 u_int32_t copy_len = 0;
4320
4321 /*
4322 * Don't test for INPCB_STATE_DEAD since this may be called
4323 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
4324 */
4325 if (in6p == NULL) {
4326 error = EINVAL;
4327 goto out;
4328 }
4329
4330 if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL && cid != 1) {
4331 error = EINVAL;
4332 goto out;
4333 }
4334
4335 ifp = in6p->in6p_last_outifp;
4336 *ifindex = ((ifp != NULL) ? ifp->if_index : 0);
4337 *soerror = so->so_error;
4338 *flags = 0;
4339 if (so->so_state & SS_ISCONNECTED) {
4340 *flags |= (CIF_CONNECTED | CIF_PREFERRED);
4341 }
4342 if (in6p->in6p_flags & INP_BOUND_IF) {
4343 *flags |= CIF_BOUND_IF;
4344 }
4345 if (!(in6p->in6p_flags & INP_IN6ADDR_ANY)) {
4346 *flags |= CIF_BOUND_IP;
4347 }
4348 if (!(in6p->in6p_flags & INP_ANONPORT)) {
4349 *flags |= CIF_BOUND_PORT;
4350 }
4351
4352 SOCKADDR_ZERO(&sin6, sizeof(sin6));
4353 sin6.sin6_len = sizeof(sin6);
4354 sin6.sin6_family = AF_INET6;
4355
4356 /* source address and port */
4357 sin6.sin6_port = in6p->in6p_lport;
4358 if (!in6_embedded_scope) {
4359 sin6.sin6_scope_id = in6p->inp_lifscope;
4360 }
4361 in6_recoverscope(&sin6, &in6p->in6p_laddr, NULL);
4362 if (*src_len == 0) {
4363 *src_len = sin6.sin6_len;
4364 } else {
4365 if (src != USER_ADDR_NULL) {
4366 copy_len = min(*src_len, sizeof(sin6));
4367 error = copyout(&sin6, src, copy_len);
4368 if (error != 0) {
4369 goto out;
4370 }
4371 *src_len = copy_len;
4372 }
4373 }
4374
4375 /* destination address and port */
4376 sin6.sin6_port = in6p->in6p_fport;
4377 if (!in6_embedded_scope) {
4378 sin6.sin6_scope_id = in6p->inp_fifscope;
4379 }
4380 in6_recoverscope(&sin6, &in6p->in6p_faddr, NULL);
4381 if (*dst_len == 0) {
4382 *dst_len = sin6.sin6_len;
4383 } else {
4384 if (dst != USER_ADDR_NULL) {
4385 copy_len = min(*dst_len, sizeof(sin6));
4386 error = copyout(&sin6, dst, copy_len);
4387 if (error != 0) {
4388 goto out;
4389 }
4390 *dst_len = copy_len;
4391 }
4392 }
4393
4394 if (SOCK_PROTO(so) == IPPROTO_TCP) {
4395 struct conninfo_tcp tcp_ci;
4396
4397 *aux_type = CIAUX_TCP;
4398 if (*aux_len == 0) {
4399 *aux_len = sizeof(tcp_ci);
4400 } else {
4401 if (aux_data != USER_ADDR_NULL) {
4402 copy_len = min(*aux_len, sizeof(tcp_ci));
4403 bzero(&tcp_ci, sizeof(tcp_ci));
4404 tcp_getconninfo(so, &tcp_ci);
4405 error = copyout(&tcp_ci, aux_data, copy_len);
4406 if (error != 0) {
4407 goto out;
4408 }
4409 *aux_len = copy_len;
4410 }
4411 }
4412 } else {
4413 *aux_type = 0;
4414 *aux_len = 0;
4415 }
4416
4417 out:
4418 return error;
4419 }
4420
4421 /*
4422 * 'u' group ioctls.
4423 *
4424 * The switch statement below does nothing at runtime, as it serves as a
4425 * compile time check to ensure that all of the socket 'u' ioctls (those
4426 * in the 'u' group going thru soo_ioctl) that are made available by the
4427 * networking stack is unique. This works as long as this routine gets
4428 * updated each time a new interface ioctl gets added.
4429 *
4430 * Any failures at compile time indicates duplicated ioctl values.
4431 */
4432 static __attribute__((unused)) void
in6ioctl_cassert(void)4433 in6ioctl_cassert(void)
4434 {
4435 /*
4436 * This is equivalent to _CASSERT() and the compiler wouldn't
4437 * generate any instructions, thus for compile time only.
4438 */
4439 switch ((u_long)0) {
4440 case 0:
4441
4442 /* bsd/netinet6/in6_var.h */
4443 case SIOCAADDRCTL_POLICY:
4444 case SIOCDADDRCTL_POLICY:
4445 case SIOCDRADD_IN6_32:
4446 case SIOCDRADD_IN6_64:
4447 case SIOCDRDEL_IN6_32:
4448 case SIOCDRDEL_IN6_64:
4449 ;
4450 }
4451 }
4452
4453 void
in6_ip6_to_sockaddr(const struct in6_addr * ip6,u_int16_t port,uint32_t ifscope,struct sockaddr_in6 * sin6,u_int32_t maxlen)4454 in6_ip6_to_sockaddr(const struct in6_addr *ip6, u_int16_t port, uint32_t ifscope,
4455 struct sockaddr_in6 *sin6, u_int32_t maxlen)
4456 {
4457 if (maxlen < sizeof(struct sockaddr_in6)) {
4458 return;
4459 }
4460
4461 *sin6 = (struct sockaddr_in6) {
4462 .sin6_family = AF_INET6,
4463 .sin6_len = sizeof(*sin6),
4464 .sin6_port = port,
4465 .sin6_addr = *ip6,
4466 .sin6_scope_id = IN6_IS_SCOPE_EMBED(ip6) ? ifscope : IFSCOPE_NONE,
4467 };
4468
4469 if (IN6_IS_SCOPE_EMBED(&sin6->sin6_addr)) {
4470 in6_verify_ifscope(&sin6->sin6_addr, ifscope);
4471 if (in6_embedded_scope) {
4472 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
4473 sin6->sin6_addr.s6_addr16[1] = 0;
4474 }
4475 }
4476 }
4477
4478 /* IPv6 events */
4479 struct in6_event {
4480 in6_evhdlr_code_t in6_event_code;
4481 struct ifnet *in6_ifp;
4482 struct in6_addr in6_address;
4483 uint32_t val;
4484 };
4485
4486 struct in6_event2kev in6_event2kev_array[IN6_EVENT_MAX] = {
4487 {
4488 .in6_event_code = IN6_ADDR_MARKED_DUPLICATED,
4489 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4490 .in6_event_kev_code = KEV_ND6_DAD_FAILURE,
4491 .in6_event_str = "IN6_ADDR_MARKED_DUPLICATED",
4492 },
4493 {
4494 .in6_event_code = IN6_ADDR_MARKED_DETACHED,
4495 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4496 .in6_event_kev_code = KEV_ND6_ADDR_DETACHED,
4497 .in6_event_str = "IN6_ADDR_MARKED_DETACHED",
4498 },
4499 {
4500 .in6_event_code = IN6_ADDR_MARKED_DEPRECATED,
4501 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4502 .in6_event_kev_code = KEV_ND6_ADDR_DEPRECATED,
4503 .in6_event_str = "IN6_ADDR_MARKED_DEPRECATED",
4504 },
4505 {
4506 .in6_event_code = IN6_NDP_RTR_EXPIRY,
4507 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4508 .in6_event_kev_code = KEV_ND6_RTR_EXPIRED,
4509 .in6_event_str = "IN6_NDP_RTR_EXPIRY",
4510 },
4511 {
4512 .in6_event_code = IN6_NDP_PFX_EXPIRY,
4513 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4514 .in6_event_kev_code = KEV_ND6_PFX_EXPIRED,
4515 .in6_event_str = "IN6_NDP_PFX_EXPIRY",
4516 },
4517 {
4518 .in6_event_code = IN6_NDP_ADDR_EXPIRY,
4519 .in6_event_kev_subclass = KEV_ND6_SUBCLASS,
4520 .in6_event_kev_code = KEV_ND6_ADDR_EXPIRED,
4521 .in6_event_str = "IN6_NDP_ADDR_EXPIRY",
4522 },
4523 };
4524
4525 void
in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused,in6_evhdlr_code_t in6_ev_code,struct ifnet * ifp,struct in6_addr * p_addr6,uint32_t val)4526 in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused,
4527 in6_evhdlr_code_t in6_ev_code, struct ifnet *ifp,
4528 struct in6_addr *p_addr6, uint32_t val)
4529 {
4530 struct kev_msg ev_msg;
4531 struct kev_nd6_event nd6_event;
4532
4533 bzero(&ev_msg, sizeof(ev_msg));
4534 bzero(&nd6_event, sizeof(nd6_event));
4535
4536 nd6log0(info, "%s Event %s received for %s\n",
4537 __func__, in6_event2kev_array[in6_ev_code].in6_event_str,
4538 ip6_sprintf(p_addr6));
4539
4540 ev_msg.vendor_code = KEV_VENDOR_APPLE;
4541 ev_msg.kev_class = KEV_NETWORK_CLASS;
4542 ev_msg.kev_subclass =
4543 in6_event2kev_array[in6_ev_code].in6_event_kev_subclass;
4544 ev_msg.event_code =
4545 in6_event2kev_array[in6_ev_code].in6_event_kev_code;
4546
4547 nd6_event.link_data.if_family = ifp->if_family;
4548 nd6_event.link_data.if_unit = ifp->if_unit;
4549 strlcpy(nd6_event.link_data.if_name, ifp->if_name,
4550 sizeof(nd6_event.link_data.if_name));
4551
4552 VERIFY(p_addr6 != NULL);
4553 bcopy(p_addr6, &nd6_event.in6_address,
4554 sizeof(nd6_event.in6_address));
4555 nd6_event.val = val;
4556
4557 ev_msg.dv[0].data_ptr = &nd6_event;
4558 ev_msg.dv[0].data_length = sizeof(nd6_event);
4559
4560 kev_post_msg(&ev_msg);
4561 }
4562
4563 struct in6_event_nwk_wq_entry {
4564 struct nwk_wq_entry nwk_wqe;
4565 struct in6_event in6_ev_arg;
4566 };
4567
4568 static void
in6_event_callback(struct nwk_wq_entry * nwk_item)4569 in6_event_callback(struct nwk_wq_entry *nwk_item)
4570 {
4571 struct in6_event_nwk_wq_entry *__single p_ev;
4572
4573 p_ev = __container_of(nwk_item, struct in6_event_nwk_wq_entry, nwk_wqe);
4574
4575 EVENTHANDLER_INVOKE(&in6_evhdlr_ctxt, in6_event,
4576 p_ev->in6_ev_arg.in6_event_code, p_ev->in6_ev_arg.in6_ifp,
4577 &p_ev->in6_ev_arg.in6_address, p_ev->in6_ev_arg.val);
4578
4579 kfree_type(struct in6_event_nwk_wq_entry, p_ev);
4580 }
4581
4582 void
in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code,struct ifnet * ifp,struct in6_addr * p_addr6,uint32_t val)4583 in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code,
4584 struct ifnet *ifp, struct in6_addr *p_addr6,
4585 uint32_t val)
4586 {
4587 struct in6_event_nwk_wq_entry *__single p_in6_ev = NULL;
4588
4589 evhlog(debug, "%s: eventhandler enqueuing event of type=in6_evhdlr_code event_code=%s",
4590 __func__, in6_evhdlr_code2str(in6_event_code));
4591
4592 p_in6_ev = kalloc_type(struct in6_event_nwk_wq_entry,
4593 Z_WAITOK | Z_ZERO | Z_NOFAIL);
4594
4595 p_in6_ev->nwk_wqe.func = in6_event_callback;
4596 p_in6_ev->in6_ev_arg.in6_event_code = in6_event_code;
4597 p_in6_ev->in6_ev_arg.in6_ifp = ifp;
4598 if (p_addr6 != NULL) {
4599 bcopy(p_addr6, &p_in6_ev->in6_ev_arg.in6_address,
4600 sizeof(p_in6_ev->in6_ev_arg.in6_address));
4601 }
4602 p_in6_ev->in6_ev_arg.val = val;
4603
4604 nwk_wq_enqueue(&p_in6_ev->nwk_wqe);
4605 }
4606
4607 const char *
in6_evhdlr_code2str(in6_evhdlr_code_t code)4608 in6_evhdlr_code2str(in6_evhdlr_code_t code)
4609 {
4610 switch (code) {
4611 #define IN6_EVHDLR_CODE_STRING(type) case type: return #type;
4612 IN6_EVHDLR_CODE_STRING(IN6_ADDR_MARKED_DUPLICATED)
4613 IN6_EVHDLR_CODE_STRING(IN6_ADDR_MARKED_DETACHED)
4614 IN6_EVHDLR_CODE_STRING(IN6_ADDR_MARKED_DEPRECATED)
4615 IN6_EVHDLR_CODE_STRING(IN6_NDP_RTR_EXPIRY)
4616 IN6_EVHDLR_CODE_STRING(IN6_NDP_PFX_EXPIRY)
4617 IN6_EVHDLR_CODE_STRING(IN6_NDP_ADDR_EXPIRY)
4618 IN6_EVHDLR_CODE_STRING(IN6_EVENT_MAX)
4619 #undef IN6_EVHDLR_CODE_STRING
4620 }
4621 return "UNKNOWN_IN6_EVHDLR_CODE";
4622 }
4623
4624 /*
4625 * Caller must hold in6_ifaddr_rwlock as writer.
4626 */
4627 static void
in6_iahash_remove(struct in6_ifaddr * ia)4628 in6_iahash_remove(struct in6_ifaddr *ia)
4629 {
4630 LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
4631 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
4632
4633 if (!IA6_IS_HASHED(ia)) {
4634 panic("%s: attempt to remove wrong ia %p from ipv6 hash table", __func__, ia);
4635 /* NOTREACHED */
4636 }
4637 TAILQ_REMOVE(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
4638 IA6_HASH_INIT(ia);
4639 ifa_remref(&ia->ia_ifa);
4640 }
4641
4642 /*
4643 * Caller must hold in6_ifaddr_rwlock as writer.
4644 */
4645 static void
in6_iahash_insert(struct in6_ifaddr * ia)4646 in6_iahash_insert(struct in6_ifaddr *ia)
4647 {
4648 LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
4649 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
4650
4651 if (ia->ia_addr.sin6_family != AF_INET6) {
4652 panic("%s: attempt to insert wrong ia %p into hash table", __func__, ia);
4653 /* NOTREACHED */
4654 } else if (IA6_IS_HASHED(ia)) {
4655 panic("%s: attempt to double-insert ia %p into hash table", __func__, ia);
4656 /* NOTREACHED */
4657 }
4658 TAILQ_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr),
4659 ia, ia6_hash);
4660 ifa_addref(&ia->ia_ifa);
4661 }
4662
4663 /*
4664 * Some point to point interfaces that are tunnels borrow the address from
4665 * an underlying interface (e.g. VPN server). In order for source address
4666 * selection logic to find the underlying interface first, we add the address
4667 * of borrowing point to point interfaces at the end of the list.
4668 * (see rdar://6733789)
4669 *
4670 * Caller must hold in6_ifaddr_rwlock as writer.
4671 */
4672 static void
in6_iahash_insert_ptp(struct in6_ifaddr * ia)4673 in6_iahash_insert_ptp(struct in6_ifaddr *ia)
4674 {
4675 struct in6_ifaddr *__single tmp_ifa;
4676 ifnet_ref_t tmp_ifp;
4677
4678 LCK_RW_ASSERT(&in6_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
4679 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
4680
4681 if (ia->ia_addr.sin6_family != AF_INET6) {
4682 panic("%s: attempt to insert wrong ia %p into hash table", __func__, ia);
4683 /* NOTREACHED */
4684 } else if (IA6_IS_HASHED(ia)) {
4685 panic("%s: attempt to double-insert ia %p into hash table", __func__, ia);
4686 /* NOTREACHED */
4687 }
4688 IFA_UNLOCK(&ia->ia_ifa);
4689 TAILQ_FOREACH(tmp_ifa, IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia6_hash) {
4690 IFA_LOCK(&tmp_ifa->ia_ifa);
4691 /* ia->ia_addr won't change, so check without lock */
4692 if (in6_are_addr_equal_scoped(&tmp_ifa->ia_addr.sin6_addr, &ia->ia_addr.sin6_addr, tmp_ifa->ia_addr.sin6_scope_id, ia->ia_addr.sin6_scope_id)) {
4693 IFA_UNLOCK(&tmp_ifa->ia_ifa);
4694 break;
4695 }
4696 IFA_UNLOCK(&tmp_ifa->ia_ifa);
4697 }
4698 tmp_ifp = (tmp_ifa == NULL) ? NULL : tmp_ifa->ia_ifp;
4699
4700 IFA_LOCK(&ia->ia_ifa);
4701 if (tmp_ifp == NULL) {
4702 TAILQ_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr),
4703 ia, ia6_hash);
4704 } else {
4705 TAILQ_INSERT_TAIL(IN6ADDR_HASH(&ia->ia_addr.sin6_addr),
4706 ia, ia6_hash);
4707 }
4708 ifa_addref(&ia->ia_ifa);
4709 }
4710
4711 /*
4712 * ipv6 socket options.
4713 *
4714 * The switch statement below does nothing at runtime, as it serves as a
4715 * compile time check to ensure that all of the ipv6 socket options are
4716 * unique. This works as long as this routine gets updated each time a
4717 * new ipv6 socket option gets added.
4718 *
4719 * Any failures at compile time indicates duplicated ipv6 socket option
4720 * values.
4721 */
4722 static __attribute__((unused)) void
tcpsockopt_cassert(void)4723 tcpsockopt_cassert(void)
4724 {
4725 /*
4726 * This is equivalent to _CASSERT() and the compiler wouldn't
4727 * generate any instructions, thus for compile time only.
4728 */
4729 switch ((int)0) {
4730 case 0:
4731
4732 /* bsd/netinet6/in6.h */
4733 case IPV6_SOCKOPT_RESERVED1:
4734 case IPV6_UNICAST_HOPS:
4735 case IPV6_MULTICAST_IF:
4736 case IPV6_MULTICAST_HOPS:
4737 case IPV6_MULTICAST_LOOP:
4738 case IPV6_JOIN_GROUP:
4739 case IPV6_LEAVE_GROUP:
4740 case IPV6_PORTRANGE:
4741 case ICMP6_FILTER:
4742 case IPV6_2292PKTINFO:
4743 case IPV6_2292HOPLIMIT:
4744 case IPV6_2292NEXTHOP:
4745 case IPV6_2292HOPOPTS:
4746 case IPV6_2292DSTOPTS:
4747 case IPV6_2292RTHDR:
4748 case IPV6_2292PKTOPTIONS:
4749 #ifdef __APPLE_USE_RFC_2292
4750 // #define IPV6_PKTINFO IPV6_3542PKTINFO
4751 // #define IPV6_HOPLIMIT IPV6_3542HOPLIMIT
4752 // #define IPV6_NEXTHOP IPV6_3542NEXTHOP
4753 // #define IPV6_HOPOPTS IPV6_3542HOPOPTS
4754 // #define IPV6_DSTOPTS IPV6_3542DSTOPTS
4755 // #define IPV6_RTHDR IPV6_3542RTHDR
4756 case IPV6_PKTOPTIONS:
4757 #endif /* __APPLE_USE_RFC_2292 */
4758 case IPV6_CHECKSUM:
4759 case IPV6_V6ONLY:
4760 #ifndef KERNEL
4761 // #define IPV6_BINDV6ONLY IPV6_V6ONLY
4762 #endif /* KERNEL */
4763 case IPV6_IPSEC_POLICY:
4764 case IPV6_FAITH:
4765 case IPV6_FW_ADD:
4766 case IPV6_FW_DEL:
4767 case IPV6_FW_FLUSH:
4768 case IPV6_FW_ZERO:
4769 case IPV6_FW_GET:
4770 case IPV6_RECVTCLASS:
4771 case IPV6_TCLASS:
4772 #ifdef __APPLE_USE_RFC_3542
4773 case IPV6_RTHDRDSTOPTS:
4774 case IPV6_RECVPKTINFO:
4775 case IPV6_RECVHOPLIMIT:
4776 case IPV6_RECVRTHDR:
4777 case IPV6_RECVHOPOPTS:
4778 case IPV6_RECVDSTOPTS:
4779 #ifdef KERNEL
4780 case IPV6_RECVRTHDRDSTOPTS:
4781 #endif
4782 case IPV6_USE_MIN_MTU:
4783 case IPV6_RECVPATHMTU:
4784 case IPV6_PATHMTU:
4785 case IPV6_3542PKTINFO:
4786 case IPV6_3542HOPLIMIT:
4787 case IPV6_3542NEXTHOP:
4788 case IPV6_3542HOPOPTS:
4789 case IPV6_3542DSTOPTS:
4790 case IPV6_3542RTHDR:
4791 // #define IPV6_PKTINFO IPV6_3542PKTINFO
4792 // #define IPV6_HOPLIMIT IPV6_3542HOPLIMIT
4793 // #define IPV6_NEXTHOP IPV6_3542NEXTHOP
4794 // #define IPV6_HOPOPTS IPV6_3542HOPOPTS
4795 // #define IPV6_DSTOPTS IPV6_3542DSTOPTS
4796 // #define IPV6_RTHDR IPV6_3542RTHDR
4797 case IPV6_AUTOFLOWLABEL:
4798 case IPV6_DONTFRAG:
4799 case IPV6_PREFER_TEMPADDR:
4800 case IPV6_MSFILTER:
4801 #endif /* __APPLE_USE_RFC_3542 */
4802 case IPV6_BOUND_IF:
4803
4804 /* bsd/netinet6/in6_private.h */
4805 case IPV6_NO_IFT_CELLULAR:
4806 case IPV6_OUT_IF:
4807 ;
4808 }
4809 }
4810