]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / net / if.c
1 /*
2 * Copyright (c) 2000-2020 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 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if.c 8.3 (Berkeley) 1/4/94
61 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #include <kern/locks.h>
71
72 #include <sys/param.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/protosw.h>
80 #include <sys/kernel.h>
81 #include <sys/sockio.h>
82 #include <sys/syslog.h>
83 #include <sys/sysctl.h>
84 #include <sys/mcache.h>
85 #include <sys/kauth.h>
86 #include <sys/priv.h>
87 #include <kern/zalloc.h>
88 #include <mach/boolean.h>
89
90 #include <machine/endian.h>
91
92 #include <pexpert/pexpert.h>
93
94 #include <net/if.h>
95 #include <net/if_arp.h>
96 #include <net/if_dl.h>
97 #include <net/if_types.h>
98 #include <net/if_var.h>
99 #include <net/if_media.h>
100 #include <net/if_ppp.h>
101 #include <net/ethernet.h>
102 #include <net/network_agent.h>
103 #include <net/pktsched/pktsched_netem.h>
104 #include <net/radix.h>
105 #include <net/route.h>
106 #include <net/dlil.h>
107 #include <net/nwk_wq.h>
108
109 #include <sys/domain.h>
110 #include <libkern/OSAtomic.h>
111
112 #if INET
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
115 #include <netinet/in_tclass.h>
116 #include <netinet/ip_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip6.h>
119 #include <netinet/ip_var.h>
120 #include <netinet/tcp.h>
121 #include <netinet/tcp_var.h>
122 #include <netinet/udp.h>
123 #include <netinet/udp_var.h>
124 #include <netinet6/in6_var.h>
125 #include <netinet6/in6_ifattach.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet6/nd6.h>
128 #endif /* INET */
129
130
131 #include <os/log.h>
132
133 /*
134 * System initialization
135 */
136
137 extern char *proc_name_address(void *);
138
139 /* Lock group and attribute for ifaddr lock */
140 lck_attr_t *ifa_mtx_attr;
141 lck_grp_t *ifa_mtx_grp;
142 static lck_grp_attr_t *ifa_mtx_grp_attr;
143
144 static int ifioctl_ifreq(struct socket *, u_long, struct ifreq *,
145 struct proc *);
146 static int ifioctl_ifconf(u_long, caddr_t);
147 static int ifioctl_ifclone(u_long, caddr_t);
148 static int ifioctl_iforder(u_long, caddr_t);
149 static int ifioctl_ifdesc(struct ifnet *, u_long, caddr_t, struct proc *);
150 static int ifioctl_linkparams(struct ifnet *, u_long, caddr_t, struct proc *);
151 static int ifioctl_qstats(struct ifnet *, u_long, caddr_t);
152 static int ifioctl_throttle(struct ifnet *, u_long, caddr_t, struct proc *);
153 static int ifioctl_netsignature(struct ifnet *, u_long, caddr_t);
154 static int ifconf(u_long cmd, user_addr_t ifrp, int * ret_space);
155 __private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *);
156 void if_rtproto_del(struct ifnet *ifp, int protocol);
157
158 static int if_addmulti_common(struct ifnet *, const struct sockaddr *,
159 struct ifmultiaddr **, int);
160 static int if_delmulti_common(struct ifmultiaddr *, struct ifnet *,
161 const struct sockaddr *, int);
162 static struct ifnet *ifunit_common(const char *, boolean_t);
163
164 static int if_rtmtu(struct radix_node *, void *);
165 static void if_rtmtu_update(struct ifnet *);
166
167 static int if_clone_list(int, int *, user_addr_t);
168
169 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
170
171 struct ifnethead ifnet_head = TAILQ_HEAD_INITIALIZER(ifnet_head);
172
173 /* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
174 struct ifnethead ifnet_ordered_head = TAILQ_HEAD_INITIALIZER(ifnet_ordered_head);
175 static u_int32_t if_ordered_count = 0;
176
177 static int if_cloners_count;
178 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
179
180 static struct ifaddr *ifa_ifwithnet_common(const struct sockaddr *,
181 unsigned int);
182 static void if_attach_ifa_common(struct ifnet *, struct ifaddr *, int);
183 static void if_detach_ifa_common(struct ifnet *, struct ifaddr *, int);
184
185 static void if_attach_ifma(struct ifnet *, struct ifmultiaddr *, int);
186 static int if_detach_ifma(struct ifnet *, struct ifmultiaddr *, int);
187
188 static struct ifmultiaddr *ifma_alloc(int);
189 static void ifma_free(struct ifmultiaddr *);
190 static void ifma_trace(struct ifmultiaddr *, int);
191
192 #if DEBUG
193 static unsigned int ifma_debug = 1; /* debugging (enabled) */
194 #else
195 static unsigned int ifma_debug; /* debugging (disabled) */
196 #endif /* !DEBUG */
197 static unsigned int ifma_size; /* size of zone element */
198 static struct zone *ifma_zone; /* zone for ifmultiaddr */
199
200 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
201
202 /* For gdb */
203 __private_extern__ unsigned int ifma_trace_hist_size = IFMA_TRACE_HIST_SIZE;
204
205 struct ifmultiaddr_dbg {
206 struct ifmultiaddr ifma; /* ifmultiaddr */
207 u_int16_t ifma_refhold_cnt; /* # of ref */
208 u_int16_t ifma_refrele_cnt; /* # of rele */
209 /*
210 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
211 */
212 ctrace_t ifma_refhold[IFMA_TRACE_HIST_SIZE];
213 ctrace_t ifma_refrele[IFMA_TRACE_HIST_SIZE];
214 /*
215 * Trash list linkage
216 */
217 TAILQ_ENTRY(ifmultiaddr_dbg) ifma_trash_link;
218 };
219
220 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
221 static TAILQ_HEAD(, ifmultiaddr_dbg) ifma_trash_head;
222 static decl_lck_mtx_data(, ifma_trash_lock);
223
224 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
225 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
226
227 /*
228 * XXX: declare here to avoid to include many inet6 related files..
229 * should be more generalized?
230 */
231 extern void nd6_setmtu(struct ifnet *);
232 extern lck_mtx_t *nd6_mutex;
233
234 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Link layers");
235 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
236 "Generic link-management");
237
238 SYSCTL_DECL(_net_link_generic_system);
239
240 static uint32_t if_verbose = 0;
241 SYSCTL_INT(_net_link_generic_system, OID_AUTO, if_verbose,
242 CTLFLAG_RW | CTLFLAG_LOCKED, &if_verbose, 0, "");
243
244 #if (DEBUG || DEVELOPMENT)
245 static uint32_t default_tcp_kao_max = 0;
246 SYSCTL_INT(_net_link_generic_system, OID_AUTO, default_tcp_kao_max,
247 CTLFLAG_RW | CTLFLAG_LOCKED, &default_tcp_kao_max, 0, "");
248 #else
249 static const uint32_t default_tcp_kao_max = 0;
250 #endif /* (DEBUG || DEVELOPMENT) */
251
252 u_int32_t companion_link_sock_buffer_limit = 0;
253
254 static int
255 sysctl_set_companion_link_sock_buf_limit SYSCTL_HANDLER_ARGS
256 {
257 #pragma unused(arg1, arg2)
258 int error, tmp = companion_link_sock_buffer_limit;
259 error = sysctl_handle_int(oidp, &tmp, 0, req);
260 if (tmp < 0) {
261 return EINVAL;
262 }
263 if ((error = priv_check_cred(kauth_cred_get(),
264 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
265 return error;
266 }
267
268 u_int32_t new_limit = tmp;
269 if (new_limit == companion_link_sock_buffer_limit) {
270 return 0;
271 }
272
273 bool recover = new_limit == 0 ? true : false;
274 if (recover) {
275 error = inp_recover_companion_link(&tcbinfo);
276 } else {
277 error = inp_limit_companion_link(&tcbinfo, new_limit);
278 }
279 if (!error) {
280 companion_link_sock_buffer_limit = new_limit;
281 }
282 return error;
283 }
284
285 SYSCTL_PROC(_net_link_generic_system, OID_AUTO, companion_sndbuf_limit,
286 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
287 &companion_link_sock_buffer_limit, 0, sysctl_set_companion_link_sock_buf_limit,
288 "I", "set sock send buffer limit of connections using companion links");
289
290 boolean_t intcoproc_unrestricted;
291
292 /* Eventhandler context for interface events */
293 struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt;
294
295 void
296 ifa_init(void)
297 {
298 /* Setup lock group and attribute for ifaddr */
299 ifa_mtx_grp_attr = lck_grp_attr_alloc_init();
300 ifa_mtx_grp = lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr);
301 ifa_mtx_attr = lck_attr_alloc_init();
302
303 PE_parse_boot_argn("ifa_debug", &ifma_debug, sizeof(ifma_debug));
304
305 ifma_size = (ifma_debug == 0) ? sizeof(struct ifmultiaddr) :
306 sizeof(struct ifmultiaddr_dbg);
307
308 ifma_zone = zone_create(IFMA_ZONE_NAME, ifma_size, ZC_NONE);
309 lck_mtx_init(&ifma_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
310 TAILQ_INIT(&ifma_trash_head);
311
312 PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted,
313 sizeof(intcoproc_unrestricted));
314 }
315
316 /*
317 * Network interface utility routines.
318 *
319 * Routines with ifa_ifwith* names take sockaddr *'s as
320 * parameters.
321 */
322
323 int if_index;
324 struct ifaddr **ifnet_addrs;
325 struct ifnet **ifindex2ifnet;
326
327 __private_extern__ void
328 if_attach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
329 {
330 if_attach_ifa_common(ifp, ifa, 0);
331 }
332
333 __private_extern__ void
334 if_attach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
335 {
336 if_attach_ifa_common(ifp, ifa, 1);
337 }
338
339 static void
340 if_attach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
341 {
342 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
343 IFA_LOCK_ASSERT_HELD(ifa);
344
345 if (ifa->ifa_ifp != ifp) {
346 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__,
347 ifa->ifa_ifp, ifp);
348 /* NOTREACHED */
349 } else if (ifa->ifa_debug & IFD_ATTACHED) {
350 panic("%s: Attempt to attach an already attached ifa=%p",
351 __func__, ifa);
352 /* NOTREACHED */
353 } else if (link && !(ifa->ifa_debug & IFD_LINK)) {
354 panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
355 /* NOTREACHED */
356 } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
357 panic("%s: Unexpected link address ifa=%p", __func__, ifa);
358 /* NOTREACHED */
359 }
360 IFA_ADDREF_LOCKED(ifa);
361 ifa->ifa_debug |= IFD_ATTACHED;
362 if (link) {
363 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
364 } else {
365 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
366 }
367
368 if (ifa->ifa_attached != NULL) {
369 (*ifa->ifa_attached)(ifa);
370 }
371
372 }
373
374 __private_extern__ void
375 if_detach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
376 {
377 if_detach_ifa_common(ifp, ifa, 0);
378 }
379
380 __private_extern__ void
381 if_detach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
382 {
383 if_detach_ifa_common(ifp, ifa, 1);
384 }
385
386 static void
387 if_detach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
388 {
389 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
390 IFA_LOCK_ASSERT_HELD(ifa);
391
392 if (link && !(ifa->ifa_debug & IFD_LINK)) {
393 panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
394 /* NOTREACHED */
395 } else if (link && ifa != TAILQ_FIRST(&ifp->if_addrhead)) {
396 panic("%s: Link address ifa=%p not first", __func__, ifa);
397 /* NOTREACHED */
398 } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
399 panic("%s: Unexpected link address ifa=%p", __func__, ifa);
400 /* NOTREACHED */
401 } else if (!(ifa->ifa_debug & IFD_ATTACHED)) {
402 panic("%s: Attempt to detach an unattached address ifa=%p",
403 __func__, ifa);
404 /* NOTREACHED */
405 } else if (ifa->ifa_ifp != ifp) {
406 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__,
407 ifa->ifa_ifp, ifp);
408 /* NOTREACHED */
409 } else if (ifa->ifa_debug & IFD_DEBUG) {
410 struct ifaddr *ifa2;
411 TAILQ_FOREACH(ifa2, &ifp->if_addrhead, ifa_link) {
412 if (ifa2 == ifa) {
413 break;
414 }
415 }
416 if (ifa2 != ifa) {
417 panic("%s: Attempt to detach a stray address ifa=%p",
418 __func__, ifa);
419 /* NOTREACHED */
420 }
421 }
422 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
423 /* This must not be the last reference to the ifaddr */
424 if (IFA_REMREF_LOCKED(ifa) == NULL) {
425 panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
426 /* NOTREACHED */
427 }
428 ifa->ifa_debug &= ~IFD_ATTACHED;
429
430 if (ifa->ifa_detached != NULL) {
431 (*ifa->ifa_detached)(ifa);
432 }
433
434 }
435
436 #define INITIAL_IF_INDEXLIM 8
437
438 /*
439 * Function: if_next_index
440 * Purpose:
441 * Return the next available interface index.
442 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
443 * added entry when necessary.
444 *
445 * Note:
446 * ifnet_addrs[] is indexed by (if_index - 1), whereas
447 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
448 * always allocate one extra element to hold ifindex2ifnet[0], which
449 * is unused.
450 */
451 int if_next_index(void);
452
453 __private_extern__ int
454 if_next_index(void)
455 {
456 static int if_indexlim = 0;
457 int new_index;
458
459 new_index = ++if_index;
460 if (if_index > if_indexlim) {
461 unsigned n;
462 int new_if_indexlim;
463 caddr_t new_ifnet_addrs;
464 caddr_t new_ifindex2ifnet;
465 caddr_t old_ifnet_addrs;
466
467 old_ifnet_addrs = (caddr_t)ifnet_addrs;
468 if (ifnet_addrs == NULL) {
469 new_if_indexlim = INITIAL_IF_INDEXLIM;
470 } else {
471 new_if_indexlim = if_indexlim << 1;
472 }
473
474 /* allocate space for the larger arrays */
475 n = (2 * new_if_indexlim + 1) * sizeof(caddr_t);
476 new_ifnet_addrs = _MALLOC(n, M_IFADDR, M_WAITOK | M_ZERO);
477 if (new_ifnet_addrs == NULL) {
478 --if_index;
479 return -1;
480 }
481
482 new_ifindex2ifnet = new_ifnet_addrs
483 + new_if_indexlim * sizeof(caddr_t);
484 if (ifnet_addrs != NULL) {
485 /* copy the existing data */
486 bcopy((caddr_t)ifnet_addrs, new_ifnet_addrs,
487 if_indexlim * sizeof(caddr_t));
488 bcopy((caddr_t)ifindex2ifnet,
489 new_ifindex2ifnet,
490 (if_indexlim + 1) * sizeof(caddr_t));
491 }
492
493 /* switch to the new tables and size */
494 ifnet_addrs = (struct ifaddr **)(void *)new_ifnet_addrs;
495 ifindex2ifnet = (struct ifnet **)(void *)new_ifindex2ifnet;
496 if_indexlim = new_if_indexlim;
497
498 /* release the old data */
499 if (old_ifnet_addrs != NULL) {
500 _FREE((caddr_t)old_ifnet_addrs, M_IFADDR);
501 }
502 }
503 return new_index;
504 }
505
506 /*
507 * Create a clone network interface.
508 */
509 static int
510 if_clone_create(char *name, int len, void *params)
511 {
512 struct if_clone *ifc;
513 char *dp;
514 int wildcard;
515 u_int32_t bytoff, bitoff;
516 u_int32_t unit;
517 int err;
518
519 ifc = if_clone_lookup(name, &unit);
520 if (ifc == NULL) {
521 return EINVAL;
522 }
523
524 if (ifunit(name) != NULL) {
525 return EEXIST;
526 }
527
528 bytoff = bitoff = 0;
529 wildcard = (unit == UINT32_MAX);
530 /*
531 * Find a free unit if none was given.
532 */
533 if (wildcard) {
534 while ((bytoff < ifc->ifc_bmlen) &&
535 (ifc->ifc_units[bytoff] == 0xff)) {
536 bytoff++;
537 }
538 if (bytoff >= ifc->ifc_bmlen) {
539 return ENOSPC;
540 }
541 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
542 bitoff++;
543 }
544 unit = (bytoff << 3) + bitoff;
545 }
546
547 if (unit > ifc->ifc_maxunit) {
548 return ENXIO;
549 }
550
551 lck_mtx_lock(&ifc->ifc_mutex);
552 err = (*ifc->ifc_create)(ifc, unit, params);
553 if (err != 0) {
554 lck_mtx_unlock(&ifc->ifc_mutex);
555 return err;
556 }
557
558 if (!wildcard) {
559 bytoff = unit >> 3;
560 bitoff = unit - (bytoff << 3);
561 }
562
563 /*
564 * Allocate the unit in the bitmap.
565 */
566 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
567 ("%s: bit is already set", __func__));
568 ifc->ifc_units[bytoff] |= (1 << bitoff);
569
570 /* In the wildcard case, we need to update the name. */
571 if (wildcard) {
572 for (dp = name; *dp != '\0'; dp++) {
573 ;
574 }
575 if (snprintf(dp, len - (dp - name), "%d", unit) >
576 len - (dp - name) - 1) {
577 /*
578 * This can only be a programmer error and
579 * there's no straightforward way to recover if
580 * it happens.
581 */
582 panic("%s: interface name too long", __func__);
583 /* NOTREACHED */
584 }
585 }
586 lck_mtx_unlock(&ifc->ifc_mutex);
587
588 return 0;
589 }
590
591 /*
592 * Destroy a clone network interface.
593 */
594 static int
595 if_clone_destroy(const char *name)
596 {
597 struct if_clone *ifc = NULL;
598 struct ifnet *ifp = NULL;
599 int bytoff, bitoff;
600 u_int32_t unit;
601 int error = 0;
602
603 ifc = if_clone_lookup(name, &unit);
604
605 if (ifc == NULL) {
606 error = EINVAL;
607 goto done;
608 }
609
610 if (unit < ifc->ifc_minifs) {
611 error = EINVAL;
612 goto done;
613 }
614
615 ifp = ifunit_ref(name);
616 if (ifp == NULL) {
617 error = ENXIO;
618 goto done;
619 }
620
621 if (ifc->ifc_destroy == NULL) {
622 error = EOPNOTSUPP;
623 goto done;
624 }
625
626 lck_mtx_lock(&ifc->ifc_mutex);
627 error = (*ifc->ifc_destroy)(ifp);
628
629 if (error) {
630 lck_mtx_unlock(&ifc->ifc_mutex);
631 goto done;
632 }
633
634 /* Compute offset in the bitmap and deallocate the unit. */
635 bytoff = unit >> 3;
636 bitoff = unit - (bytoff << 3);
637 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
638 ("%s: bit is already cleared", __func__));
639 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
640 lck_mtx_unlock(&ifc->ifc_mutex);
641
642 done:
643 if (ifp != NULL) {
644 ifnet_decr_iorefcnt(ifp);
645 }
646 return error;
647 }
648
649 /*
650 * Look up a network interface cloner.
651 */
652
653 __private_extern__ struct if_clone *
654 if_clone_lookup(const char *name, u_int32_t *unitp)
655 {
656 struct if_clone *ifc;
657 const char *cp;
658 u_int32_t i;
659
660 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
661 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
662 if (ifc->ifc_name[i] != *cp) {
663 goto next_ifc;
664 }
665 }
666 goto found_name;
667 next_ifc:
668 ifc = LIST_NEXT(ifc, ifc_list);
669 }
670
671 /* No match. */
672 return (struct if_clone *)NULL;
673
674 found_name:
675 if (*cp == '\0') {
676 i = UINT32_MAX;
677 } else {
678 for (i = 0; *cp != '\0'; cp++) {
679 if (*cp < '0' || *cp > '9') {
680 /* Bogus unit number. */
681 return NULL;
682 }
683 i = (i * 10) + (*cp - '0');
684 }
685 }
686
687 if (unitp != NULL) {
688 *unitp = i;
689 }
690 return ifc;
691 }
692
693 void *
694 if_clone_softc_allocate(const struct if_clone *ifc)
695 {
696 void *p_clone = NULL;
697
698 VERIFY(ifc != NULL);
699
700 p_clone = zalloc(ifc->ifc_zone);
701 if (p_clone != NULL) {
702 bzero(p_clone, ifc->ifc_softc_size);
703 }
704
705 return p_clone;
706 }
707
708 void
709 if_clone_softc_deallocate(const struct if_clone *ifc, void *p_softc)
710 {
711 VERIFY(ifc != NULL && p_softc != NULL);
712 bzero(p_softc, ifc->ifc_softc_size);
713 zfree(ifc->ifc_zone, p_softc);
714 }
715
716 /*
717 * Register a network interface cloner.
718 */
719 int
720 if_clone_attach(struct if_clone *ifc)
721 {
722 int bytoff, bitoff;
723 int err;
724 int len, maxclone;
725 u_int32_t unit;
726
727 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
728 ("%s: %s requested more units then allowed (%d > %d)",
729 __func__, ifc->ifc_name, ifc->ifc_minifs,
730 ifc->ifc_maxunit + 1));
731 /*
732 * Compute bitmap size and allocate it.
733 */
734 maxclone = ifc->ifc_maxunit + 1;
735 len = maxclone >> 3;
736 if ((len << 3) < maxclone) {
737 len++;
738 }
739 ifc->ifc_units = _MALLOC(len, M_CLONE, M_WAITOK | M_ZERO);
740 if (ifc->ifc_units == NULL) {
741 return ENOBUFS;
742 }
743 ifc->ifc_bmlen = len;
744 lck_mtx_init(&ifc->ifc_mutex, ifnet_lock_group, ifnet_lock_attr);
745
746 if (ifc->ifc_softc_size != 0) {
747 ifc->ifc_zone = zone_create(ifc->ifc_name, ifc->ifc_softc_size,
748 ZC_DESTRUCTIBLE);
749 }
750
751 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
752 if_cloners_count++;
753
754 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
755 err = (*ifc->ifc_create)(ifc, unit, NULL);
756 KASSERT(err == 0,
757 ("%s: failed to create required interface %s%d",
758 __func__, ifc->ifc_name, unit));
759
760 /* Allocate the unit in the bitmap. */
761 bytoff = unit >> 3;
762 bitoff = unit - (bytoff << 3);
763 ifc->ifc_units[bytoff] |= (1 << bitoff);
764 }
765
766 return 0;
767 }
768
769 /*
770 * Unregister a network interface cloner.
771 */
772 void
773 if_clone_detach(struct if_clone *ifc)
774 {
775 LIST_REMOVE(ifc, ifc_list);
776 FREE(ifc->ifc_units, M_CLONE);
777 if (ifc->ifc_softc_size != 0) {
778 zdestroy(ifc->ifc_zone);
779 }
780
781 lck_mtx_destroy(&ifc->ifc_mutex, ifnet_lock_group);
782 if_cloners_count--;
783 }
784
785 /*
786 * Provide list of interface cloners to userspace.
787 */
788 static int
789 if_clone_list(int count, int *ret_total, user_addr_t dst)
790 {
791 char outbuf[IFNAMSIZ];
792 struct if_clone *ifc;
793 int error = 0;
794
795 *ret_total = if_cloners_count;
796 if (dst == USER_ADDR_NULL) {
797 /* Just asking how many there are. */
798 return 0;
799 }
800
801 if (count < 0) {
802 return EINVAL;
803 }
804
805 count = (if_cloners_count < count) ? if_cloners_count : count;
806
807 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
808 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
809 bzero(outbuf, sizeof(outbuf));
810 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
811 error = copyout(outbuf, dst, IFNAMSIZ);
812 if (error) {
813 break;
814 }
815 }
816
817 return error;
818 }
819
820 u_int32_t
821 if_functional_type(struct ifnet *ifp, bool exclude_delegate)
822 {
823 u_int32_t ret = IFRTYPE_FUNCTIONAL_UNKNOWN;
824
825 if (ifp != NULL) {
826 if (ifp->if_flags & IFF_LOOPBACK) {
827 ret = IFRTYPE_FUNCTIONAL_LOOPBACK;
828 } else if (IFNET_IS_COMPANION_LINK(ifp)) {
829 ret = IFRTYPE_FUNCTIONAL_COMPANIONLINK;
830 } else if ((exclude_delegate &&
831 (ifp->if_family == IFNET_FAMILY_ETHERNET &&
832 ifp->if_subfamily == IFNET_SUBFAMILY_WIFI)) ||
833 (!exclude_delegate && IFNET_IS_WIFI(ifp))) {
834 if (ifp->if_eflags & IFEF_AWDL) {
835 ret = IFRTYPE_FUNCTIONAL_WIFI_AWDL;
836 } else {
837 ret = IFRTYPE_FUNCTIONAL_WIFI_INFRA;
838 }
839 } else if ((exclude_delegate &&
840 (ifp->if_type == IFT_CELLULAR)) ||
841 (!exclude_delegate && IFNET_IS_CELLULAR(ifp))) {
842 ret = IFRTYPE_FUNCTIONAL_CELLULAR;
843 } else if (IFNET_IS_INTCOPROC(ifp)) {
844 ret = IFRTYPE_FUNCTIONAL_INTCOPROC;
845 } else if ((exclude_delegate &&
846 (ifp->if_family == IFNET_FAMILY_ETHERNET ||
847 ifp->if_family == IFNET_FAMILY_BOND ||
848 ifp->if_family == IFNET_FAMILY_VLAN ||
849 ifp->if_family == IFNET_FAMILY_FIREWIRE)) ||
850 (!exclude_delegate && IFNET_IS_WIRED(ifp))) {
851 ret = IFRTYPE_FUNCTIONAL_WIRED;
852 }
853 }
854
855 return ret;
856 }
857
858 /*
859 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
860 * and that it matches only the local (not broadcast) address.
861 */
862 __private_extern__ struct in_ifaddr *
863 ifa_foraddr(unsigned int addr)
864 {
865 return ifa_foraddr_scoped(addr, IFSCOPE_NONE);
866 }
867
868 /*
869 * Similar to ifa_foraddr, except with the added interface scope
870 * constraint (unless the caller passes in IFSCOPE_NONE in which
871 * case there is no scope restriction).
872 */
873 __private_extern__ struct in_ifaddr *
874 ifa_foraddr_scoped(unsigned int addr, unsigned int scope)
875 {
876 struct in_ifaddr *ia = NULL;
877
878 lck_rw_lock_shared(in_ifaddr_rwlock);
879 TAILQ_FOREACH(ia, INADDR_HASH(addr), ia_hash) {
880 IFA_LOCK_SPIN(&ia->ia_ifa);
881 if (ia->ia_addr.sin_addr.s_addr == addr &&
882 (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
883 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
884 IFA_UNLOCK(&ia->ia_ifa);
885 break;
886 }
887 IFA_UNLOCK(&ia->ia_ifa);
888 }
889 lck_rw_done(in_ifaddr_rwlock);
890 return ia;
891 }
892
893 /*
894 * Similar to ifa_foraddr, except that this for IPv6.
895 */
896 __private_extern__ struct in6_ifaddr *
897 ifa_foraddr6(struct in6_addr *addr6)
898 {
899 return ifa_foraddr6_scoped(addr6, IFSCOPE_NONE);
900 }
901
902 __private_extern__ struct in6_ifaddr *
903 ifa_foraddr6_scoped(struct in6_addr *addr6, unsigned int scope)
904 {
905 struct in6_ifaddr *ia = NULL;
906
907 lck_rw_lock_shared(&in6_ifaddr_rwlock);
908 TAILQ_FOREACH(ia, IN6ADDR_HASH(addr6), ia6_hash) {
909 IFA_LOCK(&ia->ia_ifa);
910 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, addr6) &&
911 (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
912 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
913 IFA_UNLOCK(&ia->ia_ifa);
914 break;
915 }
916 IFA_UNLOCK(&ia->ia_ifa);
917 }
918 lck_rw_done(&in6_ifaddr_rwlock);
919
920 return ia;
921 }
922
923 /*
924 * Return the first (primary) address of a given family on an interface.
925 */
926 __private_extern__ struct ifaddr *
927 ifa_ifpgetprimary(struct ifnet *ifp, int family)
928 {
929 struct ifaddr *ifa;
930
931 ifnet_lock_shared(ifp);
932 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
933 IFA_LOCK_SPIN(ifa);
934 if (ifa->ifa_addr->sa_family == family) {
935 IFA_ADDREF_LOCKED(ifa); /* for caller */
936 IFA_UNLOCK(ifa);
937 break;
938 }
939 IFA_UNLOCK(ifa);
940 }
941 ifnet_lock_done(ifp);
942
943 return ifa;
944 }
945
946 static inline int
947 ifa_equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
948 {
949 if (!sa1 || !sa2) {
950 return 0;
951 }
952 if (sa1->sa_len != sa2->sa_len) {
953 return 0;
954 }
955
956 return bcmp(sa1, sa2, sa1->sa_len) == 0;
957 }
958
959 /*
960 * Locate an interface based on a complete address.
961 */
962 struct ifaddr *
963 ifa_ifwithaddr_locked(const struct sockaddr *addr)
964 {
965 struct ifnet *ifp;
966 struct ifaddr *ifa;
967 struct ifaddr *result = NULL;
968
969 for (ifp = ifnet_head.tqh_first; ifp && !result;
970 ifp = ifp->if_link.tqe_next) {
971 ifnet_lock_shared(ifp);
972 for (ifa = ifp->if_addrhead.tqh_first; ifa;
973 ifa = ifa->ifa_link.tqe_next) {
974 IFA_LOCK_SPIN(ifa);
975 if (ifa->ifa_addr->sa_family != addr->sa_family) {
976 IFA_UNLOCK(ifa);
977 continue;
978 }
979 if (ifa_equal(addr, ifa->ifa_addr)) {
980 result = ifa;
981 IFA_ADDREF_LOCKED(ifa); /* for caller */
982 IFA_UNLOCK(ifa);
983 break;
984 }
985 if ((ifp->if_flags & IFF_BROADCAST) &&
986 ifa->ifa_broadaddr != NULL &&
987 /* IP6 doesn't have broadcast */
988 ifa->ifa_broadaddr->sa_len != 0 &&
989 ifa_equal(ifa->ifa_broadaddr, addr)) {
990 result = ifa;
991 IFA_ADDREF_LOCKED(ifa); /* for caller */
992 IFA_UNLOCK(ifa);
993 break;
994 }
995 IFA_UNLOCK(ifa);
996 }
997 ifnet_lock_done(ifp);
998 }
999
1000 return result;
1001 }
1002
1003 struct ifaddr *
1004 ifa_ifwithaddr(const struct sockaddr *addr)
1005 {
1006 struct ifaddr *result = NULL;
1007
1008 ifnet_head_lock_shared();
1009
1010 result = ifa_ifwithaddr_locked(addr);
1011
1012 ifnet_head_done();
1013
1014 return result;
1015 }
1016 /*
1017 * Locate the point to point interface with a given destination address.
1018 */
1019 /*ARGSUSED*/
1020 struct ifaddr *
1021 ifa_ifwithdstaddr(const struct sockaddr *addr)
1022 {
1023 struct ifnet *ifp;
1024 struct ifaddr *ifa;
1025 struct ifaddr *result = NULL;
1026
1027 ifnet_head_lock_shared();
1028 for (ifp = ifnet_head.tqh_first; ifp && !result;
1029 ifp = ifp->if_link.tqe_next) {
1030 if ((ifp->if_flags & IFF_POINTOPOINT)) {
1031 ifnet_lock_shared(ifp);
1032 for (ifa = ifp->if_addrhead.tqh_first; ifa;
1033 ifa = ifa->ifa_link.tqe_next) {
1034 IFA_LOCK_SPIN(ifa);
1035 if (ifa->ifa_addr->sa_family !=
1036 addr->sa_family) {
1037 IFA_UNLOCK(ifa);
1038 continue;
1039 }
1040 if (ifa_equal(addr, ifa->ifa_dstaddr)) {
1041 result = ifa;
1042 IFA_ADDREF_LOCKED(ifa); /* for caller */
1043 IFA_UNLOCK(ifa);
1044 break;
1045 }
1046 IFA_UNLOCK(ifa);
1047 }
1048 ifnet_lock_done(ifp);
1049 }
1050 }
1051 ifnet_head_done();
1052 return result;
1053 }
1054
1055 /*
1056 * Locate the source address of an interface based on a complete address.
1057 */
1058 struct ifaddr *
1059 ifa_ifwithaddr_scoped_locked(const struct sockaddr *addr, unsigned int ifscope)
1060 {
1061 struct ifaddr *result = NULL;
1062 struct ifnet *ifp;
1063
1064 if (ifscope == IFSCOPE_NONE) {
1065 return ifa_ifwithaddr_locked(addr);
1066 }
1067
1068 if (ifscope > (unsigned int)if_index) {
1069 return NULL;
1070 }
1071
1072 ifp = ifindex2ifnet[ifscope];
1073 if (ifp != NULL) {
1074 struct ifaddr *ifa = NULL;
1075
1076 /*
1077 * This is suboptimal; there should be a better way
1078 * to search for a given address of an interface
1079 * for any given address family.
1080 */
1081 ifnet_lock_shared(ifp);
1082 for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
1083 ifa = ifa->ifa_link.tqe_next) {
1084 IFA_LOCK_SPIN(ifa);
1085 if (ifa->ifa_addr->sa_family != addr->sa_family) {
1086 IFA_UNLOCK(ifa);
1087 continue;
1088 }
1089 if (ifa_equal(addr, ifa->ifa_addr)) {
1090 result = ifa;
1091 IFA_ADDREF_LOCKED(ifa); /* for caller */
1092 IFA_UNLOCK(ifa);
1093 break;
1094 }
1095 if ((ifp->if_flags & IFF_BROADCAST) &&
1096 ifa->ifa_broadaddr != NULL &&
1097 /* IP6 doesn't have broadcast */
1098 ifa->ifa_broadaddr->sa_len != 0 &&
1099 ifa_equal(ifa->ifa_broadaddr, addr)) {
1100 result = ifa;
1101 IFA_ADDREF_LOCKED(ifa); /* for caller */
1102 IFA_UNLOCK(ifa);
1103 break;
1104 }
1105 IFA_UNLOCK(ifa);
1106 }
1107 ifnet_lock_done(ifp);
1108 }
1109
1110 return result;
1111 }
1112
1113 struct ifaddr *
1114 ifa_ifwithaddr_scoped(const struct sockaddr *addr, unsigned int ifscope)
1115 {
1116 struct ifaddr *result = NULL;
1117
1118 ifnet_head_lock_shared();
1119
1120 result = ifa_ifwithaddr_scoped_locked(addr, ifscope);
1121
1122 ifnet_head_done();
1123
1124 return result;
1125 }
1126
1127 struct ifaddr *
1128 ifa_ifwithnet(const struct sockaddr *addr)
1129 {
1130 return ifa_ifwithnet_common(addr, IFSCOPE_NONE);
1131 }
1132
1133 struct ifaddr *
1134 ifa_ifwithnet_scoped(const struct sockaddr *addr, unsigned int ifscope)
1135 {
1136 return ifa_ifwithnet_common(addr, ifscope);
1137 }
1138
1139 /*
1140 * Find an interface on a specific network. If many, choice
1141 * is most specific found.
1142 */
1143 static struct ifaddr *
1144 ifa_ifwithnet_common(const struct sockaddr *addr, unsigned int ifscope)
1145 {
1146 struct ifnet *ifp;
1147 struct ifaddr *ifa = NULL;
1148 struct ifaddr *ifa_maybe = NULL;
1149 u_int af = addr->sa_family;
1150 const char *addr_data = addr->sa_data, *cplim;
1151
1152 if (af != AF_INET && af != AF_INET6) {
1153 ifscope = IFSCOPE_NONE;
1154 }
1155
1156 ifnet_head_lock_shared();
1157 /*
1158 * AF_LINK addresses can be looked up directly by their index number,
1159 * so do that if we can.
1160 */
1161 if (af == AF_LINK) {
1162 const struct sockaddr_dl *sdl =
1163 (const struct sockaddr_dl *)(uintptr_t)(size_t)addr;
1164 if (sdl->sdl_index && sdl->sdl_index <= if_index) {
1165 ifa = ifnet_addrs[sdl->sdl_index - 1];
1166 if (ifa != NULL) {
1167 IFA_ADDREF(ifa);
1168 }
1169
1170 ifnet_head_done();
1171 return ifa;
1172 }
1173 }
1174
1175 /*
1176 * Scan though each interface, looking for ones that have
1177 * addresses in this address family.
1178 */
1179 for (ifp = ifnet_head.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
1180 ifnet_lock_shared(ifp);
1181 for (ifa = ifp->if_addrhead.tqh_first; ifa;
1182 ifa = ifa->ifa_link.tqe_next) {
1183 const char *cp, *cp2, *cp3;
1184
1185 IFA_LOCK(ifa);
1186 if (ifa->ifa_addr == NULL ||
1187 ifa->ifa_addr->sa_family != af) {
1188 next:
1189 IFA_UNLOCK(ifa);
1190 continue;
1191 }
1192 /*
1193 * If we're looking up with a scope,
1194 * find using a matching interface.
1195 */
1196 if (ifscope != IFSCOPE_NONE &&
1197 ifp->if_index != ifscope) {
1198 IFA_UNLOCK(ifa);
1199 continue;
1200 }
1201
1202 /*
1203 * Scan all the bits in the ifa's address.
1204 * If a bit dissagrees with what we are
1205 * looking for, mask it with the netmask
1206 * to see if it really matters.
1207 * (A byte at a time)
1208 */
1209 if (ifa->ifa_netmask == 0) {
1210 IFA_UNLOCK(ifa);
1211 continue;
1212 }
1213 cp = addr_data;
1214 cp2 = ifa->ifa_addr->sa_data;
1215 cp3 = ifa->ifa_netmask->sa_data;
1216 cplim = ifa->ifa_netmask->sa_len +
1217 (char *)ifa->ifa_netmask;
1218 while (cp3 < cplim) {
1219 if ((*cp++ ^ *cp2++) & *cp3++) {
1220 goto next; /* next address! */
1221 }
1222 }
1223 /*
1224 * If the netmask of what we just found
1225 * is more specific than what we had before
1226 * (if we had one) then remember the new one
1227 * before continuing to search
1228 * for an even better one.
1229 */
1230 if (ifa_maybe == NULL ||
1231 rn_refines((caddr_t)ifa->ifa_netmask,
1232 (caddr_t)ifa_maybe->ifa_netmask)) {
1233 IFA_ADDREF_LOCKED(ifa); /* ifa_maybe */
1234 IFA_UNLOCK(ifa);
1235 if (ifa_maybe != NULL) {
1236 IFA_REMREF(ifa_maybe);
1237 }
1238 ifa_maybe = ifa;
1239 } else {
1240 IFA_UNLOCK(ifa);
1241 }
1242 IFA_LOCK_ASSERT_NOTHELD(ifa);
1243 }
1244 ifnet_lock_done(ifp);
1245
1246 if (ifa != NULL) {
1247 break;
1248 }
1249 }
1250 ifnet_head_done();
1251
1252 if (ifa == NULL) {
1253 ifa = ifa_maybe;
1254 } else if (ifa_maybe != NULL) {
1255 IFA_REMREF(ifa_maybe);
1256 }
1257
1258 return ifa;
1259 }
1260
1261 /*
1262 * Find an interface address specific to an interface best matching
1263 * a given address applying same source address selection rules
1264 * as done in the kernel for implicit source address binding
1265 */
1266 struct ifaddr *
1267 ifaof_ifpforaddr_select(const struct sockaddr *addr, struct ifnet *ifp)
1268 {
1269 u_int af = addr->sa_family;
1270
1271 if (af == AF_INET6) {
1272 return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6 *, addr), ifp, 0);
1273 }
1274
1275 return ifaof_ifpforaddr(addr, ifp);
1276 }
1277
1278 /*
1279 * Find an interface address specific to an interface best matching
1280 * a given address without regards to source address selection.
1281 *
1282 * This is appropriate for use-cases where we just want to update/init
1283 * some data structure like routing table entries.
1284 */
1285 struct ifaddr *
1286 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1287 {
1288 struct ifaddr *ifa = NULL;
1289 const char *cp, *cp2, *cp3;
1290 char *cplim;
1291 struct ifaddr *ifa_maybe = NULL;
1292 struct ifaddr *better_ifa_maybe = NULL;
1293 u_int af = addr->sa_family;
1294
1295 if (af >= AF_MAX) {
1296 return NULL;
1297 }
1298
1299 ifnet_lock_shared(ifp);
1300 for (ifa = ifp->if_addrhead.tqh_first; ifa;
1301 ifa = ifa->ifa_link.tqe_next) {
1302 IFA_LOCK(ifa);
1303 if (ifa->ifa_addr->sa_family != af) {
1304 IFA_UNLOCK(ifa);
1305 continue;
1306 }
1307 if (ifa_maybe == NULL) {
1308 IFA_ADDREF_LOCKED(ifa); /* for ifa_maybe */
1309 ifa_maybe = ifa;
1310 }
1311 if (ifa->ifa_netmask == 0) {
1312 if (ifa_equal(addr, ifa->ifa_addr) ||
1313 ifa_equal(addr, ifa->ifa_dstaddr)) {
1314 IFA_ADDREF_LOCKED(ifa); /* for caller */
1315 IFA_UNLOCK(ifa);
1316 break;
1317 }
1318 IFA_UNLOCK(ifa);
1319 continue;
1320 }
1321 if (ifp->if_flags & IFF_POINTOPOINT) {
1322 if (ifa_equal(addr, ifa->ifa_dstaddr)) {
1323 IFA_ADDREF_LOCKED(ifa); /* for caller */
1324 IFA_UNLOCK(ifa);
1325 break;
1326 }
1327 } else {
1328 if (ifa_equal(addr, ifa->ifa_addr)) {
1329 /* exact match */
1330 IFA_ADDREF_LOCKED(ifa); /* for caller */
1331 IFA_UNLOCK(ifa);
1332 break;
1333 }
1334 cp = addr->sa_data;
1335 cp2 = ifa->ifa_addr->sa_data;
1336 cp3 = ifa->ifa_netmask->sa_data;
1337 cplim = ifa->ifa_netmask->sa_len +
1338 (char *)ifa->ifa_netmask;
1339 for (; cp3 < cplim; cp3++) {
1340 if ((*cp++ ^ *cp2++) & *cp3) {
1341 break;
1342 }
1343 }
1344 if (cp3 == cplim) {
1345 /* subnet match */
1346 if (better_ifa_maybe == NULL) {
1347 /* for better_ifa_maybe */
1348 IFA_ADDREF_LOCKED(ifa);
1349 better_ifa_maybe = ifa;
1350 }
1351 }
1352 }
1353 IFA_UNLOCK(ifa);
1354 }
1355
1356 if (ifa == NULL) {
1357 if (better_ifa_maybe != NULL) {
1358 ifa = better_ifa_maybe;
1359 better_ifa_maybe = NULL;
1360 } else {
1361 ifa = ifa_maybe;
1362 ifa_maybe = NULL;
1363 }
1364 }
1365
1366 ifnet_lock_done(ifp);
1367
1368 if (better_ifa_maybe != NULL) {
1369 IFA_REMREF(better_ifa_maybe);
1370 }
1371 if (ifa_maybe != NULL) {
1372 IFA_REMREF(ifa_maybe);
1373 }
1374
1375 return ifa;
1376 }
1377
1378 #include <net/route.h>
1379
1380 /*
1381 * Default action when installing a route with a Link Level gateway.
1382 * Lookup an appropriate real ifa to point to.
1383 * This should be moved to /sys/net/link.c eventually.
1384 */
1385 void
1386 link_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
1387 {
1388 struct ifaddr *ifa;
1389 struct sockaddr *dst;
1390 struct ifnet *ifp;
1391 void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *);
1392
1393 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
1394 RT_LOCK_ASSERT_HELD(rt);
1395
1396 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1397 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) {
1398 return;
1399 }
1400
1401 /* Become a regular mutex, just in case */
1402 RT_CONVERT_LOCK(rt);
1403
1404 ifa = ifaof_ifpforaddr(dst, ifp);
1405 if (ifa) {
1406 rtsetifa(rt, ifa);
1407 IFA_LOCK_SPIN(ifa);
1408 ifa_rtrequest = ifa->ifa_rtrequest;
1409 IFA_UNLOCK(ifa);
1410 if (ifa_rtrequest != NULL && ifa_rtrequest != link_rtrequest) {
1411 ifa_rtrequest(cmd, rt, sa);
1412 }
1413 IFA_REMREF(ifa);
1414 }
1415 }
1416
1417 /*
1418 * if_updown will set the interface up or down. It will
1419 * prevent other up/down events from occurring until this
1420 * up/down event has completed.
1421 *
1422 * Caller must lock ifnet. This function will drop the
1423 * lock. This allows ifnet_set_flags to set the rest of
1424 * the flags after we change the up/down state without
1425 * dropping the interface lock between setting the
1426 * up/down state and updating the rest of the flags.
1427 */
1428 __private_extern__ void
1429 if_updown( struct ifnet *ifp, int up)
1430 {
1431 u_int32_t eflags;
1432 int i;
1433 struct ifaddr **ifa;
1434 struct timespec tv;
1435 struct ifclassq *ifq = &ifp->if_snd;
1436
1437 /* Wait until no one else is changing the up/down state */
1438 while ((ifp->if_eflags & IFEF_UPDOWNCHANGE) != 0) {
1439 tv.tv_sec = 0;
1440 tv.tv_nsec = NSEC_PER_SEC / 10;
1441 ifnet_lock_done(ifp);
1442 msleep(&ifp->if_eflags, NULL, 0, "if_updown", &tv);
1443 ifnet_lock_exclusive(ifp);
1444 }
1445
1446 /* Verify that the interface isn't already in the right state */
1447 if ((!up && (ifp->if_flags & IFF_UP) == 0) ||
1448 (up && (ifp->if_flags & IFF_UP) == IFF_UP)) {
1449 return;
1450 }
1451
1452 /* Indicate that the up/down state is changing */
1453 eflags = if_set_eflags(ifp, IFEF_UPDOWNCHANGE);
1454 ASSERT((eflags & IFEF_UPDOWNCHANGE) == 0);
1455
1456 /* Mark interface up or down */
1457 if (up) {
1458 ifp->if_flags |= IFF_UP;
1459 } else {
1460 ifp->if_flags &= ~IFF_UP;
1461 }
1462
1463 ifnet_touch_lastchange(ifp);
1464 ifnet_touch_lastupdown(ifp);
1465
1466 /* Drop the lock to notify addresses and route */
1467 ifnet_lock_done(ifp);
1468
1469 IFCQ_LOCK(ifq);
1470 if_qflush(ifp, 1);
1471
1472 /* Inform all transmit queues about the new link state */
1473 ifnet_update_sndq(ifq, up ? CLASSQ_EV_LINK_UP : CLASSQ_EV_LINK_DOWN);
1474 IFCQ_UNLOCK(ifq);
1475
1476 if (ifnet_get_address_list(ifp, &ifa) == 0) {
1477 for (i = 0; ifa[i] != 0; i++) {
1478 pfctlinput(up ? PRC_IFUP : PRC_IFDOWN, ifa[i]->ifa_addr);
1479 }
1480 ifnet_free_address_list(ifa);
1481 }
1482 rt_ifmsg(ifp);
1483
1484 /* Aquire the lock to clear the changing flag */
1485 ifnet_lock_exclusive(ifp);
1486 if_clear_eflags(ifp, IFEF_UPDOWNCHANGE);
1487 wakeup(&ifp->if_eflags);
1488 }
1489
1490 /*
1491 * Mark an interface down and notify protocols of
1492 * the transition.
1493 */
1494 void
1495 if_down(
1496 struct ifnet *ifp)
1497 {
1498 ifnet_lock_exclusive(ifp);
1499 if_updown(ifp, 0);
1500 ifnet_lock_done(ifp);
1501 }
1502
1503 /*
1504 * Mark an interface up and notify protocols of
1505 * the transition.
1506 */
1507 void
1508 if_up(
1509 struct ifnet *ifp)
1510 {
1511 ifnet_lock_exclusive(ifp);
1512 if_updown(ifp, 1);
1513 ifnet_lock_done(ifp);
1514 }
1515
1516 /*
1517 * Flush an interface queue.
1518 */
1519 void
1520 if_qflush(struct ifnet *ifp, int ifq_locked)
1521 {
1522 struct ifclassq *ifq = &ifp->if_snd;
1523
1524 if (!ifq_locked) {
1525 IFCQ_LOCK(ifq);
1526 }
1527
1528 if (IFCQ_IS_ENABLED(ifq)) {
1529 fq_if_request_classq(ifq, CLASSQRQ_PURGE, NULL);
1530 }
1531
1532 VERIFY(IFCQ_IS_EMPTY(ifq));
1533
1534 if (!ifq_locked) {
1535 IFCQ_UNLOCK(ifq);
1536 }
1537 }
1538
1539 void
1540 if_qflush_sc(struct ifnet *ifp, mbuf_svc_class_t sc, u_int32_t flow,
1541 u_int32_t *packets, u_int32_t *bytes, int ifq_locked)
1542 {
1543 struct ifclassq *ifq = &ifp->if_snd;
1544 u_int32_t cnt = 0, len = 0;
1545
1546 VERIFY(sc == MBUF_SC_UNSPEC || MBUF_VALID_SC(sc));
1547 VERIFY(flow != 0);
1548
1549 if (!ifq_locked) {
1550 IFCQ_LOCK(ifq);
1551 }
1552
1553 if (IFCQ_IS_ENABLED(ifq)) {
1554 cqrq_purge_sc_t req = { sc, flow, 0, 0 };
1555
1556 fq_if_request_classq(ifq, CLASSQRQ_PURGE_SC, &req);
1557 cnt = req.packets;
1558 len = req.bytes;
1559 }
1560
1561 if (!ifq_locked) {
1562 IFCQ_UNLOCK(ifq);
1563 }
1564
1565 if (packets != NULL) {
1566 *packets = cnt;
1567 }
1568 if (bytes != NULL) {
1569 *bytes = len;
1570 }
1571 }
1572
1573 /*
1574 * Extracts interface unit number and name from string, returns -1 upon failure.
1575 * Upon success, returns extracted unit number, and interface name in dst.
1576 */
1577 int
1578 ifunit_extract(const char *src, char *dst, size_t dstlen, int *unit)
1579 {
1580 const char *cp;
1581 size_t len, m;
1582 char c;
1583 int u;
1584
1585 if (src == NULL || dst == NULL || dstlen == 0 || unit == NULL) {
1586 return -1;
1587 }
1588
1589 len = strlen(src);
1590 if (len < 2 || len > dstlen) {
1591 return -1;
1592 }
1593 cp = src + len - 1;
1594 c = *cp;
1595 if (c < '0' || c > '9') {
1596 return -1; /* trailing garbage */
1597 }
1598 u = 0;
1599 m = 1;
1600 do {
1601 if (cp == src) {
1602 return -1; /* no interface name */
1603 }
1604 u += (c - '0') * m;
1605 if (u > 1000000) {
1606 return -1; /* number is unreasonable */
1607 }
1608 m *= 10;
1609 c = *--cp;
1610 } while (c >= '0' && c <= '9');
1611 len = cp - src + 1;
1612 bcopy(src, dst, len);
1613 dst[len] = '\0';
1614 *unit = u;
1615
1616 return 0;
1617 }
1618
1619 /*
1620 * Map interface name to
1621 * interface structure pointer.
1622 */
1623 static struct ifnet *
1624 ifunit_common(const char *name, boolean_t hold)
1625 {
1626 char namebuf[IFNAMSIZ + 1];
1627 struct ifnet *ifp;
1628 int unit;
1629
1630 if (ifunit_extract(name, namebuf, sizeof(namebuf), &unit) < 0) {
1631 return NULL;
1632 }
1633
1634 /* for safety, since we use strcmp() below */
1635 namebuf[sizeof(namebuf) - 1] = '\0';
1636
1637 /*
1638 * Now search all the interfaces for this name/number
1639 */
1640 ifnet_head_lock_shared();
1641 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
1642 /*
1643 * Use strcmp() rather than strncmp() here,
1644 * since we want to match the entire string.
1645 */
1646 if (strcmp(ifp->if_name, namebuf)) {
1647 continue;
1648 }
1649 if (unit == ifp->if_unit) {
1650 break;
1651 }
1652 }
1653
1654 /* if called from ifunit_ref() and ifnet is not attached, bail */
1655 if (hold && ifp != NULL && !ifnet_is_attached(ifp, 1)) {
1656 ifp = NULL;
1657 }
1658
1659 ifnet_head_done();
1660 return ifp;
1661 }
1662
1663 struct ifnet *
1664 ifunit(const char *name)
1665 {
1666 return ifunit_common(name, FALSE);
1667 }
1668
1669 /*
1670 * Similar to ifunit(), except that we hold an I/O reference count on an
1671 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1672 * Will return NULL unless interface exists and is fully attached.
1673 */
1674 struct ifnet *
1675 ifunit_ref(const char *name)
1676 {
1677 return ifunit_common(name, TRUE);
1678 }
1679
1680 /*
1681 * Map interface name in a sockaddr_dl to
1682 * interface structure pointer.
1683 */
1684 struct ifnet *
1685 if_withname(struct sockaddr *sa)
1686 {
1687 char ifname[IFNAMSIZ + 1];
1688 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(void *)sa;
1689
1690 if ((sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1691 (sdl->sdl_nlen > IFNAMSIZ)) {
1692 return NULL;
1693 }
1694
1695 /*
1696 * ifunit wants a null-terminated name. It may not be null-terminated
1697 * in the sockaddr. We don't want to change the caller's sockaddr,
1698 * and there might not be room to put the trailing null anyway, so we
1699 * make a local copy that we know we can null terminate safely.
1700 */
1701
1702 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1703 ifname[sdl->sdl_nlen] = '\0';
1704 return ifunit(ifname);
1705 }
1706
1707 static __attribute__((noinline)) int
1708 ifioctl_ifconf(u_long cmd, caddr_t data)
1709 {
1710 int error = 0;
1711
1712 switch (cmd) {
1713 case OSIOCGIFCONF32: /* struct ifconf32 */
1714 case SIOCGIFCONF32: { /* struct ifconf32 */
1715 struct ifconf32 ifc;
1716 bcopy(data, &ifc, sizeof(ifc));
1717 error = ifconf(cmd, CAST_USER_ADDR_T(ifc.ifc_req),
1718 &ifc.ifc_len);
1719 bcopy(&ifc, data, sizeof(ifc));
1720 break;
1721 }
1722
1723 case SIOCGIFCONF64: /* struct ifconf64 */
1724 case OSIOCGIFCONF64: { /* struct ifconf64 */
1725 struct ifconf64 ifc;
1726 bcopy(data, &ifc, sizeof(ifc));
1727 error = ifconf(cmd, ifc.ifc_req, &ifc.ifc_len);
1728 bcopy(&ifc, data, sizeof(ifc));
1729 break;
1730 }
1731
1732 default:
1733 VERIFY(0);
1734 /* NOTREACHED */
1735 }
1736
1737 return error;
1738 }
1739
1740 static __attribute__((noinline)) int
1741 ifioctl_ifclone(u_long cmd, caddr_t data)
1742 {
1743 int error = 0;
1744
1745 switch (cmd) {
1746 case SIOCIFGCLONERS32: { /* struct if_clonereq32 */
1747 struct if_clonereq32 ifcr;
1748 bcopy(data, &ifcr, sizeof(ifcr));
1749 error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1750 CAST_USER_ADDR_T(ifcr.ifcru_buffer));
1751 bcopy(&ifcr, data, sizeof(ifcr));
1752 break;
1753 }
1754
1755 case SIOCIFGCLONERS64: { /* struct if_clonereq64 */
1756 struct if_clonereq64 ifcr;
1757 bcopy(data, &ifcr, sizeof(ifcr));
1758 error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1759 ifcr.ifcru_buffer);
1760 bcopy(&ifcr, data, sizeof(ifcr));
1761 break;
1762 }
1763
1764 default:
1765 VERIFY(0);
1766 /* NOTREACHED */
1767 }
1768
1769 return error;
1770 }
1771
1772 static __attribute__((noinline)) int
1773 ifioctl_ifdesc(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1774 {
1775 struct if_descreq *ifdr = (struct if_descreq *)(void *)data;
1776 u_int32_t ifdr_len;
1777 int error = 0;
1778
1779 VERIFY(ifp != NULL);
1780
1781 switch (cmd) {
1782 case SIOCSIFDESC: { /* struct if_descreq */
1783 if ((error = proc_suser(p)) != 0) {
1784 break;
1785 }
1786
1787 ifnet_lock_exclusive(ifp);
1788 bcopy(&ifdr->ifdr_len, &ifdr_len, sizeof(ifdr_len));
1789 if (ifdr_len > sizeof(ifdr->ifdr_desc) ||
1790 ifdr_len > ifp->if_desc.ifd_maxlen) {
1791 error = EINVAL;
1792 ifnet_lock_done(ifp);
1793 break;
1794 }
1795
1796 bzero(ifp->if_desc.ifd_desc, ifp->if_desc.ifd_maxlen);
1797 if ((ifp->if_desc.ifd_len = ifdr_len) > 0) {
1798 bcopy(ifdr->ifdr_desc, ifp->if_desc.ifd_desc,
1799 MIN(ifdr_len, ifp->if_desc.ifd_maxlen));
1800 }
1801 ifnet_lock_done(ifp);
1802 break;
1803 }
1804
1805 case SIOCGIFDESC: { /* struct if_descreq */
1806 ifnet_lock_shared(ifp);
1807 ifdr_len = MIN(ifp->if_desc.ifd_len, sizeof(ifdr->ifdr_desc));
1808 bcopy(&ifdr_len, &ifdr->ifdr_len, sizeof(ifdr_len));
1809 bzero(&ifdr->ifdr_desc, sizeof(ifdr->ifdr_desc));
1810 if (ifdr_len > 0) {
1811 bcopy(ifp->if_desc.ifd_desc, ifdr->ifdr_desc, ifdr_len);
1812 }
1813 ifnet_lock_done(ifp);
1814 break;
1815 }
1816
1817 default:
1818 VERIFY(0);
1819 /* NOTREACHED */
1820 }
1821
1822 return error;
1823 }
1824
1825 static __attribute__((noinline)) int
1826 ifioctl_linkparams(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1827 {
1828 struct if_linkparamsreq *iflpr =
1829 (struct if_linkparamsreq *)(void *)data;
1830 struct ifclassq *ifq;
1831 int error = 0;
1832
1833 VERIFY(ifp != NULL);
1834 ifq = &ifp->if_snd;
1835
1836 switch (cmd) {
1837 case SIOCSIFLINKPARAMS: { /* struct if_linkparamsreq */
1838 struct tb_profile tb = { .rate = 0, .percent = 0, .depth = 0 };
1839
1840 if ((error = proc_suser(p)) != 0) {
1841 break;
1842 }
1843
1844
1845 char netem_name[32];
1846 (void) snprintf(netem_name, sizeof(netem_name),
1847 "if_output_netem_%s", if_name(ifp));
1848 error = netem_config(&ifp->if_output_netem, netem_name,
1849 &iflpr->iflpr_output_netem, (void *)ifp,
1850 ifnet_enqueue_netem, NETEM_MAX_BATCH_SIZE);
1851 if (error != 0) {
1852 break;
1853 }
1854
1855 IFCQ_LOCK(ifq);
1856 if (!IFCQ_IS_READY(ifq)) {
1857 error = ENXIO;
1858 IFCQ_UNLOCK(ifq);
1859 break;
1860 }
1861 bcopy(&iflpr->iflpr_output_tbr_rate, &tb.rate,
1862 sizeof(tb.rate));
1863 bcopy(&iflpr->iflpr_output_tbr_percent, &tb.percent,
1864 sizeof(tb.percent));
1865 error = ifclassq_tbr_set(ifq, &tb, TRUE);
1866 IFCQ_UNLOCK(ifq);
1867 break;
1868 }
1869
1870 case SIOCGIFLINKPARAMS: { /* struct if_linkparamsreq */
1871 u_int32_t sched_type = PKTSCHEDT_NONE, flags = 0;
1872 u_int64_t tbr_bw = 0, tbr_pct = 0;
1873
1874 IFCQ_LOCK(ifq);
1875
1876 if (IFCQ_IS_ENABLED(ifq)) {
1877 sched_type = ifq->ifcq_type;
1878 }
1879
1880 bcopy(&sched_type, &iflpr->iflpr_output_sched,
1881 sizeof(iflpr->iflpr_output_sched));
1882
1883 if (IFCQ_TBR_IS_ENABLED(ifq)) {
1884 tbr_bw = ifq->ifcq_tbr.tbr_rate_raw;
1885 tbr_pct = ifq->ifcq_tbr.tbr_percent;
1886 }
1887 bcopy(&tbr_bw, &iflpr->iflpr_output_tbr_rate,
1888 sizeof(iflpr->iflpr_output_tbr_rate));
1889 bcopy(&tbr_pct, &iflpr->iflpr_output_tbr_percent,
1890 sizeof(iflpr->iflpr_output_tbr_percent));
1891 IFCQ_UNLOCK(ifq);
1892
1893 if (ifp->if_output_sched_model ==
1894 IFNET_SCHED_MODEL_DRIVER_MANAGED) {
1895 flags |= IFLPRF_DRVMANAGED;
1896 }
1897 bcopy(&flags, &iflpr->iflpr_flags, sizeof(iflpr->iflpr_flags));
1898 bcopy(&ifp->if_output_bw, &iflpr->iflpr_output_bw,
1899 sizeof(iflpr->iflpr_output_bw));
1900 bcopy(&ifp->if_input_bw, &iflpr->iflpr_input_bw,
1901 sizeof(iflpr->iflpr_input_bw));
1902 bcopy(&ifp->if_output_lt, &iflpr->iflpr_output_lt,
1903 sizeof(iflpr->iflpr_output_lt));
1904 bcopy(&ifp->if_input_lt, &iflpr->iflpr_input_lt,
1905 sizeof(iflpr->iflpr_input_lt));
1906
1907 if (ifp->if_output_netem != NULL) {
1908 netem_get_params(ifp->if_output_netem,
1909 &iflpr->iflpr_output_netem);
1910 }
1911
1912 break;
1913 }
1914
1915 default:
1916 VERIFY(0);
1917 /* NOTREACHED */
1918 }
1919
1920 return error;
1921 }
1922
1923 static __attribute__((noinline)) int
1924 ifioctl_qstats(struct ifnet *ifp, u_long cmd, caddr_t data)
1925 {
1926 struct if_qstatsreq *ifqr = (struct if_qstatsreq *)(void *)data;
1927 u_int32_t ifqr_len, ifqr_slot;
1928 int error = 0;
1929
1930 VERIFY(ifp != NULL);
1931
1932 switch (cmd) {
1933 case SIOCGIFQUEUESTATS: { /* struct if_qstatsreq */
1934 bcopy(&ifqr->ifqr_slot, &ifqr_slot, sizeof(ifqr_slot));
1935 bcopy(&ifqr->ifqr_len, &ifqr_len, sizeof(ifqr_len));
1936 error = ifclassq_getqstats(&ifp->if_snd, ifqr_slot,
1937 ifqr->ifqr_buf, &ifqr_len);
1938 if (error != 0) {
1939 ifqr_len = 0;
1940 }
1941 bcopy(&ifqr_len, &ifqr->ifqr_len, sizeof(ifqr_len));
1942 break;
1943 }
1944
1945 default:
1946 VERIFY(0);
1947 /* NOTREACHED */
1948 }
1949
1950 return error;
1951 }
1952
1953 static __attribute__((noinline)) int
1954 ifioctl_throttle(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1955 {
1956 struct if_throttlereq *ifthr = (struct if_throttlereq *)(void *)data;
1957 u_int32_t ifthr_level;
1958 int error = 0;
1959
1960 VERIFY(ifp != NULL);
1961
1962 switch (cmd) {
1963 case SIOCSIFTHROTTLE: { /* struct if_throttlereq */
1964 /*
1965 * XXX: Use priv_check_cred() instead of root check?
1966 */
1967 if ((error = proc_suser(p)) != 0) {
1968 break;
1969 }
1970
1971 bcopy(&ifthr->ifthr_level, &ifthr_level, sizeof(ifthr_level));
1972 error = ifnet_set_throttle(ifp, ifthr_level);
1973 if (error == EALREADY) {
1974 error = 0;
1975 }
1976 break;
1977 }
1978
1979 case SIOCGIFTHROTTLE: { /* struct if_throttlereq */
1980 if ((error = ifnet_get_throttle(ifp, &ifthr_level)) == 0) {
1981 bcopy(&ifthr_level, &ifthr->ifthr_level,
1982 sizeof(ifthr_level));
1983 }
1984 break;
1985 }
1986
1987 default:
1988 VERIFY(0);
1989 /* NOTREACHED */
1990 }
1991
1992 return error;
1993 }
1994
1995 static int
1996 ifioctl_getnetagents(struct ifnet *ifp, u_int32_t *count, user_addr_t uuid_p)
1997 {
1998 int error = 0;
1999 u_int32_t index = 0;
2000 u_int32_t valid_netagent_count = 0;
2001 *count = 0;
2002
2003 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_SHARED);
2004
2005 if (ifp->if_agentids != NULL) {
2006 for (index = 0; index < ifp->if_agentcount; index++) {
2007 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2008 if (!uuid_is_null(*netagent_uuid)) {
2009 if (uuid_p != USER_ADDR_NULL) {
2010 error = copyout(netagent_uuid,
2011 uuid_p + sizeof(uuid_t) * valid_netagent_count,
2012 sizeof(uuid_t));
2013 if (error != 0) {
2014 return error;
2015 }
2016 }
2017 valid_netagent_count++;
2018 }
2019 }
2020 }
2021 *count = valid_netagent_count;
2022
2023 return 0;
2024 }
2025
2026 #define IF_MAXAGENTS 64
2027 #define IF_AGENT_INCREMENT 8
2028 int
2029 if_add_netagent_locked(struct ifnet *ifp, uuid_t new_agent_uuid)
2030 {
2031 VERIFY(ifp != NULL);
2032
2033 uuid_t *first_empty_slot = NULL;
2034 u_int32_t index = 0;
2035 bool already_added = FALSE;
2036
2037 if (ifp->if_agentids != NULL) {
2038 for (index = 0; index < ifp->if_agentcount; index++) {
2039 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2040 if (uuid_compare(*netagent_uuid, new_agent_uuid) == 0) {
2041 /* Already present, ignore */
2042 already_added = TRUE;
2043 break;
2044 }
2045 if (first_empty_slot == NULL &&
2046 uuid_is_null(*netagent_uuid)) {
2047 first_empty_slot = netagent_uuid;
2048 }
2049 }
2050 }
2051 if (already_added) {
2052 /* Already added agent, don't return an error */
2053 return 0;
2054 }
2055 if (first_empty_slot == NULL) {
2056 if (ifp->if_agentcount >= IF_MAXAGENTS) {
2057 /* No room for another netagent UUID, bail */
2058 return ENOMEM;
2059 } else {
2060 /* Calculate new array size */
2061 u_int32_t new_agent_count =
2062 MIN(ifp->if_agentcount + IF_AGENT_INCREMENT,
2063 IF_MAXAGENTS);
2064
2065 /* Reallocate array */
2066 uuid_t *new_agent_array = _REALLOC(ifp->if_agentids,
2067 sizeof(uuid_t) * new_agent_count, M_NETAGENT,
2068 M_WAITOK | M_ZERO);
2069 if (new_agent_array == NULL) {
2070 return ENOMEM;
2071 }
2072
2073 /* Save new array */
2074 ifp->if_agentids = new_agent_array;
2075
2076 /* Set first empty slot */
2077 first_empty_slot =
2078 &(ifp->if_agentids[ifp->if_agentcount]);
2079
2080 /* Save new array length */
2081 ifp->if_agentcount = new_agent_count;
2082 }
2083 }
2084 uuid_copy(*first_empty_slot, new_agent_uuid);
2085 netagent_post_updated_interfaces(new_agent_uuid);
2086 return 0;
2087 }
2088
2089 int
2090 if_add_netagent(struct ifnet *ifp, uuid_t new_agent_uuid)
2091 {
2092 VERIFY(ifp != NULL);
2093
2094 ifnet_lock_exclusive(ifp);
2095
2096 int error = if_add_netagent_locked(ifp, new_agent_uuid);
2097
2098 ifnet_lock_done(ifp);
2099
2100 return error;
2101 }
2102
2103 static int
2104 if_delete_netagent_locked(struct ifnet *ifp, uuid_t remove_agent_uuid)
2105 {
2106 u_int32_t index = 0;
2107 bool removed_agent_id = FALSE;
2108
2109 if (ifp->if_agentids != NULL) {
2110 for (index = 0; index < ifp->if_agentcount; index++) {
2111 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2112 if (uuid_compare(*netagent_uuid,
2113 remove_agent_uuid) == 0) {
2114 uuid_clear(*netagent_uuid);
2115 removed_agent_id = TRUE;
2116 break;
2117 }
2118 }
2119 }
2120 if (removed_agent_id) {
2121 netagent_post_updated_interfaces(remove_agent_uuid);
2122 }
2123
2124 return 0;
2125 }
2126
2127 int
2128 if_delete_netagent(struct ifnet *ifp, uuid_t remove_agent_uuid)
2129 {
2130 VERIFY(ifp != NULL);
2131
2132 ifnet_lock_exclusive(ifp);
2133
2134 int error = if_delete_netagent_locked(ifp, remove_agent_uuid);
2135
2136 ifnet_lock_done(ifp);
2137
2138 return error;
2139 }
2140
2141 boolean_t
2142 if_check_netagent(struct ifnet *ifp, uuid_t find_agent_uuid)
2143 {
2144 boolean_t found = FALSE;
2145
2146 if (!ifp || uuid_is_null(find_agent_uuid)) {
2147 return FALSE;
2148 }
2149
2150 ifnet_lock_shared(ifp);
2151
2152 if (ifp->if_agentids != NULL) {
2153 for (uint32_t index = 0; index < ifp->if_agentcount; index++) {
2154 if (uuid_compare(ifp->if_agentids[index], find_agent_uuid) == 0) {
2155 found = TRUE;
2156 break;
2157 }
2158 }
2159 }
2160
2161 ifnet_lock_done(ifp);
2162
2163 return found;
2164 }
2165
2166 static __attribute__((noinline)) int
2167 ifioctl_netagent(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
2168 {
2169 struct if_agentidreq *ifar = (struct if_agentidreq *)(void *)data;
2170 union {
2171 struct if_agentidsreq32 s32;
2172 struct if_agentidsreq64 s64;
2173 } u;
2174 int error = 0;
2175
2176 VERIFY(ifp != NULL);
2177
2178 /* Get an io ref count if the interface is attached */
2179 if (!ifnet_is_attached(ifp, 1)) {
2180 return EOPNOTSUPP;
2181 }
2182
2183 if (cmd == SIOCAIFAGENTID ||
2184 cmd == SIOCDIFAGENTID) {
2185 ifnet_lock_exclusive(ifp);
2186 } else {
2187 ifnet_lock_shared(ifp);
2188 }
2189
2190 switch (cmd) {
2191 case SIOCAIFAGENTID: { /* struct if_agentidreq */
2192 // TODO: Use priv_check_cred() instead of root check
2193 if ((error = proc_suser(p)) != 0) {
2194 break;
2195 }
2196 error = if_add_netagent_locked(ifp, ifar->ifar_uuid);
2197 break;
2198 }
2199 case SIOCDIFAGENTID: { /* struct if_agentidreq */
2200 // TODO: Use priv_check_cred() instead of root check
2201 if ((error = proc_suser(p)) != 0) {
2202 break;
2203 }
2204 error = if_delete_netagent_locked(ifp, ifar->ifar_uuid);
2205 break;
2206 }
2207 case SIOCGIFAGENTIDS32: { /* struct if_agentidsreq32 */
2208 bcopy(data, &u.s32, sizeof(u.s32));
2209 error = ifioctl_getnetagents(ifp, &u.s32.ifar_count,
2210 u.s32.ifar_uuids);
2211 if (error == 0) {
2212 bcopy(&u.s32, data, sizeof(u.s32));
2213 }
2214 break;
2215 }
2216 case SIOCGIFAGENTIDS64: { /* struct if_agentidsreq64 */
2217 bcopy(data, &u.s64, sizeof(u.s64));
2218 error = ifioctl_getnetagents(ifp, &u.s64.ifar_count,
2219 u.s64.ifar_uuids);
2220 if (error == 0) {
2221 bcopy(&u.s64, data, sizeof(u.s64));
2222 }
2223 break;
2224 }
2225 default:
2226 VERIFY(0);
2227 /* NOTREACHED */
2228 }
2229
2230 ifnet_lock_done(ifp);
2231 ifnet_decr_iorefcnt(ifp);
2232
2233 return error;
2234 }
2235
2236 void
2237 ifnet_clear_netagent(uuid_t netagent_uuid)
2238 {
2239 struct ifnet *ifp = NULL;
2240 u_int32_t index = 0;
2241
2242 ifnet_head_lock_shared();
2243
2244 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2245 ifnet_lock_shared(ifp);
2246 if (ifp->if_agentids != NULL) {
2247 for (index = 0; index < ifp->if_agentcount; index++) {
2248 uuid_t *ifp_netagent_uuid = &(ifp->if_agentids[index]);
2249 if (uuid_compare(*ifp_netagent_uuid, netagent_uuid) == 0) {
2250 uuid_clear(*ifp_netagent_uuid);
2251 }
2252 }
2253 }
2254 ifnet_lock_done(ifp);
2255 }
2256
2257 ifnet_head_done();
2258 }
2259
2260 void
2261 ifnet_increment_generation(ifnet_t interface)
2262 {
2263 OSIncrementAtomic(&interface->if_generation);
2264 }
2265
2266 u_int32_t
2267 ifnet_get_generation(ifnet_t interface)
2268 {
2269 return interface->if_generation;
2270 }
2271
2272 void
2273 ifnet_remove_from_ordered_list(struct ifnet *ifp)
2274 {
2275 ifnet_head_assert_exclusive();
2276
2277 // Remove from list
2278 TAILQ_REMOVE(&ifnet_ordered_head, ifp, if_ordered_link);
2279 ifp->if_ordered_link.tqe_next = NULL;
2280 ifp->if_ordered_link.tqe_prev = NULL;
2281
2282 // Update ordered count
2283 VERIFY(if_ordered_count > 0);
2284 if_ordered_count--;
2285 }
2286
2287 static int
2288 ifnet_reset_order(u_int32_t *ordered_indices, u_int32_t count)
2289 {
2290 struct ifnet *ifp = NULL;
2291 int error = 0;
2292
2293 ifnet_head_lock_exclusive();
2294 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2295 if (ordered_indices[order_index] == IFSCOPE_NONE ||
2296 ordered_indices[order_index] > (uint32_t)if_index) {
2297 error = EINVAL;
2298 ifnet_head_done();
2299 return error;
2300 }
2301 }
2302 // Flush current ordered list
2303 for (ifp = TAILQ_FIRST(&ifnet_ordered_head); ifp != NULL;
2304 ifp = TAILQ_FIRST(&ifnet_ordered_head)) {
2305 ifnet_lock_exclusive(ifp);
2306 ifnet_remove_from_ordered_list(ifp);
2307 ifnet_lock_done(ifp);
2308 }
2309
2310 VERIFY(if_ordered_count == 0);
2311
2312 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2313 u_int32_t interface_index = ordered_indices[order_index];
2314 ifp = ifindex2ifnet[interface_index];
2315 if (ifp == NULL) {
2316 continue;
2317 }
2318 ifnet_lock_exclusive(ifp);
2319 TAILQ_INSERT_TAIL(&ifnet_ordered_head, ifp, if_ordered_link);
2320 ifnet_lock_done(ifp);
2321 if_ordered_count++;
2322 }
2323
2324 ifnet_head_done();
2325
2326 necp_update_all_clients();
2327
2328 return error;
2329 }
2330
2331 int
2332 if_set_qosmarking_mode(struct ifnet *ifp, u_int32_t mode)
2333 {
2334 int error = 0;
2335 u_int32_t old_mode = ifp->if_qosmarking_mode;
2336
2337 switch (mode) {
2338 case IFRTYPE_QOSMARKING_MODE_NONE:
2339 ifp->if_qosmarking_mode = IFRTYPE_QOSMARKING_MODE_NONE;
2340 break;
2341 case IFRTYPE_QOSMARKING_FASTLANE:
2342 case IFRTYPE_QOSMARKING_RFC4594:
2343 ifp->if_qosmarking_mode = mode;
2344 break;
2345 default:
2346 error = EINVAL;
2347 break;
2348 }
2349 if (error == 0 && old_mode != ifp->if_qosmarking_mode) {
2350 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_QOS_MODE_CHANGED,
2351 NULL, 0);
2352 }
2353 return error;
2354 }
2355
2356 static __attribute__((noinline)) int
2357 ifioctl_iforder(u_long cmd, caddr_t data)
2358 {
2359 int error = 0;
2360 u_int32_t *ordered_indices = NULL;
2361 if (data == NULL) {
2362 return EINVAL;
2363 }
2364
2365 switch (cmd) {
2366 case SIOCSIFORDER: { /* struct if_order */
2367 struct if_order *ifo = (struct if_order *)(void *)data;
2368
2369 if (ifo->ifo_count > (u_int32_t)if_index) {
2370 error = EINVAL;
2371 break;
2372 }
2373
2374 size_t length = (ifo->ifo_count * sizeof(u_int32_t));
2375 if (length > 0) {
2376 if (ifo->ifo_ordered_indices == USER_ADDR_NULL) {
2377 error = EINVAL;
2378 break;
2379 }
2380 ordered_indices = _MALLOC(length, M_NECP, M_WAITOK);
2381 if (ordered_indices == NULL) {
2382 error = ENOMEM;
2383 break;
2384 }
2385
2386 error = copyin(ifo->ifo_ordered_indices,
2387 ordered_indices, length);
2388 if (error != 0) {
2389 break;
2390 }
2391
2392 /* ordered_indices should not contain duplicates */
2393 bool found_duplicate = FALSE;
2394 for (uint32_t i = 0; i < (ifo->ifo_count - 1) && !found_duplicate; i++) {
2395 for (uint32_t j = i + 1; j < ifo->ifo_count && !found_duplicate; j++) {
2396 if (ordered_indices[j] == ordered_indices[i]) {
2397 error = EINVAL;
2398 found_duplicate = TRUE;
2399 break;
2400 }
2401 }
2402 }
2403 if (found_duplicate) {
2404 break;
2405 }
2406
2407 error = ifnet_reset_order(ordered_indices, ifo->ifo_count);
2408 } else {
2409 // Clear the list
2410 error = ifnet_reset_order(NULL, 0);
2411 }
2412 break;
2413 }
2414
2415 default: {
2416 VERIFY(0);
2417 /* NOTREACHED */
2418 }
2419 }
2420
2421 if (ordered_indices != NULL) {
2422 _FREE(ordered_indices, M_NECP);
2423 }
2424
2425 return error;
2426 }
2427
2428 static __attribute__((noinline)) int
2429 ifioctl_networkid(struct ifnet *ifp, caddr_t data)
2430 {
2431 struct if_netidreq *ifnetidr = (struct if_netidreq *)(void *)data;
2432 int error = 0;
2433 int len = ifnetidr->ifnetid_len;
2434
2435 VERIFY(ifp != NULL);
2436
2437 if (len > sizeof(ifnetidr->ifnetid)) {
2438 error = EINVAL;
2439 goto end;
2440 }
2441
2442 if (len == 0) {
2443 bzero(&ifp->network_id, sizeof(ifp->network_id));
2444 } else if (len > sizeof(ifp->network_id)) {
2445 error = EINVAL;
2446 goto end;
2447 }
2448
2449 ifp->network_id_len = len;
2450 bcopy(data, ifp->network_id, len);
2451 end:
2452 return error;
2453 }
2454
2455 static __attribute__((noinline)) int
2456 ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
2457 {
2458 struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
2459 u_int16_t flags;
2460 int error = 0;
2461
2462 VERIFY(ifp != NULL);
2463
2464 switch (cmd) {
2465 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2466 if (ifnsr->ifnsr_len > sizeof(ifnsr->ifnsr_data)) {
2467 error = EINVAL;
2468 break;
2469 }
2470 bcopy(&ifnsr->ifnsr_flags, &flags, sizeof(flags));
2471 error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
2472 ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
2473 break;
2474
2475 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2476 ifnsr->ifnsr_len = sizeof(ifnsr->ifnsr_data);
2477 error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
2478 &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
2479 if (error == 0) {
2480 bcopy(&flags, &ifnsr->ifnsr_flags, sizeof(flags));
2481 } else {
2482 ifnsr->ifnsr_len = 0;
2483 }
2484 break;
2485
2486 default:
2487 VERIFY(0);
2488 /* NOTREACHED */
2489 }
2490
2491 return error;
2492 }
2493
2494 static __attribute__((noinline)) int
2495 ifioctl_nat64prefix(struct ifnet *ifp, u_long cmd, caddr_t data)
2496 {
2497 struct if_nat64req *ifnat64 = (struct if_nat64req *)(void *)data;
2498 int error = 0;
2499
2500 VERIFY(ifp != NULL);
2501
2502 switch (cmd) {
2503 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
2504 error = ifnet_set_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2505 if (error != 0) {
2506 ip6stat.ip6s_clat464_plat64_pfx_setfail++;
2507 }
2508 break;
2509
2510 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
2511 error = ifnet_get_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2512 if (error != 0) {
2513 ip6stat.ip6s_clat464_plat64_pfx_getfail++;
2514 }
2515 break;
2516
2517 default:
2518 VERIFY(0);
2519 /* NOTREACHED */
2520 }
2521
2522 return error;
2523 }
2524
2525 static __attribute__((noinline)) int
2526 ifioctl_clat46addr(struct ifnet *ifp, u_long cmd, caddr_t data)
2527 {
2528 struct if_clat46req *ifclat46 = (struct if_clat46req *)(void *)data;
2529 struct in6_ifaddr *ia6_clat = NULL;
2530 int error = 0;
2531
2532 VERIFY(ifp != NULL);
2533
2534 switch (cmd) {
2535 case SIOCGIFCLAT46ADDR:
2536 ia6_clat = in6ifa_ifpwithflag(ifp, IN6_IFF_CLAT46);
2537 if (ia6_clat == NULL) {
2538 error = ENOENT;
2539 break;
2540 }
2541
2542 bcopy(&ia6_clat->ia_addr.sin6_addr, &ifclat46->ifclat46_addr.v6_address,
2543 sizeof(ifclat46->ifclat46_addr.v6_address));
2544 ifclat46->ifclat46_addr.v6_prefixlen = ia6_clat->ia_plen;
2545 IFA_REMREF(&ia6_clat->ia_ifa);
2546 break;
2547 default:
2548 VERIFY(0);
2549 /* NOTREACHED */
2550 }
2551
2552 return error;
2553 }
2554
2555
2556 static int
2557 ifioctl_get_protolist(struct ifnet *ifp, u_int32_t * ret_count,
2558 user_addr_t ifpl)
2559 {
2560 u_int32_t actual_count;
2561 u_int32_t count;
2562 int error = 0;
2563 u_int32_t *list = NULL;
2564
2565 /* find out how many */
2566 count = if_get_protolist(ifp, NULL, 0);
2567 if (ifpl == USER_ADDR_NULL) {
2568 goto done;
2569 }
2570
2571 /* copy out how many there's space for */
2572 if (*ret_count < count) {
2573 count = *ret_count;
2574 }
2575 if (count == 0) {
2576 goto done;
2577 }
2578 list = _MALLOC(count * sizeof(*list), M_TEMP, M_WAITOK | M_ZERO);
2579 if (list == NULL) {
2580 error = ENOMEM;
2581 goto done;
2582 }
2583 actual_count = if_get_protolist(ifp, list, count);
2584 if (actual_count < count) {
2585 count = actual_count;
2586 }
2587 if (count != 0) {
2588 error = copyout((caddr_t)list, ifpl, count * sizeof(*list));
2589 }
2590
2591 done:
2592 if (list != NULL) {
2593 if_free_protolist(list);
2594 }
2595 *ret_count = count;
2596 return error;
2597 }
2598
2599 static __attribute__((noinline)) int
2600 ifioctl_protolist(struct ifnet *ifp, u_long cmd, caddr_t data)
2601 {
2602 int error = 0;
2603
2604 switch (cmd) {
2605 case SIOCGIFPROTOLIST32: { /* struct if_protolistreq32 */
2606 struct if_protolistreq32 ifpl;
2607
2608 bcopy(data, &ifpl, sizeof(ifpl));
2609 if (ifpl.ifpl_reserved != 0) {
2610 error = EINVAL;
2611 break;
2612 }
2613 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2614 CAST_USER_ADDR_T(ifpl.ifpl_list));
2615 bcopy(&ifpl, data, sizeof(ifpl));
2616 break;
2617 }
2618 case SIOCGIFPROTOLIST64: { /* struct if_protolistreq64 */
2619 struct if_protolistreq64 ifpl;
2620
2621 bcopy(data, &ifpl, sizeof(ifpl));
2622 if (ifpl.ifpl_reserved != 0) {
2623 error = EINVAL;
2624 break;
2625 }
2626 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2627 ifpl.ifpl_list);
2628 bcopy(&ifpl, data, sizeof(ifpl));
2629 break;
2630 }
2631 default:
2632 VERIFY(0);
2633 /* NOTREACHED */
2634 }
2635
2636 return error;
2637 }
2638
2639 /*
2640 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2641 */
2642 static bool
2643 ifioctl_restrict_intcoproc(unsigned long cmd, const char *ifname,
2644 struct ifnet *ifp, struct proc *p)
2645 {
2646 if (intcoproc_unrestricted == TRUE) {
2647 return false;
2648 }
2649 if (proc_pid(p) == 0) {
2650 return false;
2651 }
2652 if (ifname) {
2653 ifp = ifunit(ifname);
2654 }
2655 if (ifp == NULL) {
2656 return false;
2657 }
2658 if (!IFNET_IS_INTCOPROC(ifp)) {
2659 return false;
2660 }
2661 switch (cmd) {
2662 case SIOCGIFBRDADDR:
2663 case SIOCGIFCONF32:
2664 case SIOCGIFCONF64:
2665 case SIOCGIFFLAGS:
2666 case SIOCGIFEFLAGS:
2667 case SIOCGIFCAP:
2668 case SIOCGIFMETRIC:
2669 case SIOCGIFMTU:
2670 case SIOCGIFPHYS:
2671 case SIOCGIFTYPE:
2672 case SIOCGIFFUNCTIONALTYPE:
2673 case SIOCGIFPSRCADDR:
2674 case SIOCGIFPDSTADDR:
2675 case SIOCGIFGENERIC:
2676 case SIOCGIFDEVMTU:
2677 case SIOCGIFVLAN:
2678 case SIOCGIFBOND:
2679 case SIOCGIFWAKEFLAGS:
2680 case SIOCGIFGETRTREFCNT:
2681 case SIOCGIFOPPORTUNISTIC:
2682 case SIOCGIFLINKQUALITYMETRIC:
2683 case SIOCGIFLOG:
2684 case SIOCGIFDELEGATE:
2685 case SIOCGIFEXPENSIVE:
2686 case SIOCGIFINTERFACESTATE:
2687 case SIOCGIFPROBECONNECTIVITY:
2688 case SIOCGIFTIMESTAMPENABLED:
2689 case SIOCGECNMODE:
2690 case SIOCGQOSMARKINGMODE:
2691 case SIOCGQOSMARKINGENABLED:
2692 case SIOCGIFLOWINTERNET:
2693 case SIOCGIFSTATUS:
2694 case SIOCGIFMEDIA32:
2695 case SIOCGIFMEDIA64:
2696 case SIOCGIFXMEDIA32:
2697 case SIOCGIFXMEDIA64:
2698 case SIOCGIFDESC:
2699 case SIOCGIFLINKPARAMS:
2700 case SIOCGIFQUEUESTATS:
2701 case SIOCGIFTHROTTLE:
2702 case SIOCGIFAGENTIDS32:
2703 case SIOCGIFAGENTIDS64:
2704 case SIOCGIFNETSIGNATURE:
2705 case SIOCGIFINFO_IN6:
2706 case SIOCGIFAFLAG_IN6:
2707 case SIOCGNBRINFO_IN6:
2708 case SIOCGIFALIFETIME_IN6:
2709 case SIOCGIFNETMASK_IN6:
2710 case SIOCGIFPROTOLIST32:
2711 case SIOCGIFPROTOLIST64:
2712 case SIOCGIFXFLAGS:
2713 return false;
2714 default:
2715 #if (DEBUG || DEVELOPMENT)
2716 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2717 __func__, cmd, proc_pid(p));
2718 #endif
2719 return true;
2720 }
2721 return false;
2722 }
2723
2724 /*
2725 * Given a media word, return one suitable for an application
2726 * using the original encoding.
2727 */
2728 static int
2729 compat_media(int media)
2730 {
2731 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
2732 media &= ~IFM_TMASK;
2733 media |= IFM_OTHER;
2734 }
2735 return media;
2736 }
2737
2738 static int
2739 compat_ifmu_ulist(struct ifnet *ifp, u_long cmd, void *data)
2740 {
2741 struct ifmediareq *ifmr = (struct ifmediareq *)data;
2742 user_addr_t user_addr;
2743 int i;
2744 int *media_list = NULL;
2745 int error = 0;
2746 bool list_modified = false;
2747
2748 user_addr = (cmd == SIOCGIFMEDIA64) ?
2749 ((struct ifmediareq64 *)ifmr)->ifmu_ulist :
2750 CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
2751 if (user_addr == USER_ADDR_NULL || ifmr->ifm_count == 0) {
2752 return 0;
2753 }
2754 MALLOC(media_list, int *, ifmr->ifm_count * sizeof(int),
2755 M_TEMP, M_WAITOK | M_ZERO);
2756 if (media_list == NULL) {
2757 os_log_error(OS_LOG_DEFAULT,
2758 "%s: %s MALLOC() failed",
2759 __func__, ifp->if_xname);
2760 error = ENOMEM;
2761 goto done;
2762 }
2763 error = copyin(user_addr, media_list, ifmr->ifm_count * sizeof(int));
2764 if (error != 0) {
2765 os_log_error(OS_LOG_DEFAULT,
2766 "%s: %s copyin() error %d",
2767 __func__, ifp->if_xname, error);
2768 goto done;
2769 }
2770 for (i = 0; i < ifmr->ifm_count; i++) {
2771 int old_media, new_media;
2772
2773 old_media = media_list[i];
2774
2775 new_media = compat_media(old_media);
2776 if (new_media == old_media) {
2777 continue;
2778 }
2779 if (if_verbose != 0) {
2780 os_log_info(OS_LOG_DEFAULT,
2781 "%s: %s converted extended media %08x to compat media %08x",
2782 __func__, ifp->if_xname, old_media, new_media);
2783 }
2784 media_list[i] = new_media;
2785 list_modified = true;
2786 }
2787 if (list_modified) {
2788 error = copyout(media_list, user_addr, ifmr->ifm_count * sizeof(int));
2789 if (error != 0) {
2790 os_log_error(OS_LOG_DEFAULT,
2791 "%s: %s copyout() error %d",
2792 __func__, ifp->if_xname, error);
2793 goto done;
2794 }
2795 }
2796 done:
2797 if (media_list != NULL) {
2798 FREE(media_list, M_TEMP);
2799 }
2800 return error;
2801 }
2802
2803 static int
2804 compat_ifmediareq(struct ifnet *ifp, u_long cmd, void *data)
2805 {
2806 struct ifmediareq *ifmr = (struct ifmediareq *)data;
2807 int error;
2808
2809 ifmr->ifm_active = compat_media(ifmr->ifm_active);
2810 ifmr->ifm_current = compat_media(ifmr->ifm_current);
2811
2812 error = compat_ifmu_ulist(ifp, cmd, data);
2813
2814 return error;
2815 }
2816
2817 static int
2818 ifioctl_get_media(struct ifnet *ifp, struct socket *so, u_long cmd, caddr_t data)
2819 {
2820 int error = 0;
2821
2822 /*
2823 * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2824 * media subtypes macros from <net/if_media.h>
2825 */
2826 switch (cmd) {
2827 case SIOCGIFMEDIA32:
2828 case SIOCGIFXMEDIA32:
2829 error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA32, data);
2830 break;
2831 case SIOCGIFMEDIA64:
2832 case SIOCGIFXMEDIA64:
2833 error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA64, data);
2834 break;
2835 }
2836 if (if_verbose != 0 && error != 0) {
2837 os_log(OS_LOG_DEFAULT, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2838 __func__, ifp->if_xname, cmd, error);
2839 }
2840 if (error == 0 && (cmd == SIOCGIFMEDIA32 || cmd == SIOCGIFMEDIA64)) {
2841 error = compat_ifmediareq(ifp, cmd, data);
2842 }
2843 return error;
2844 }
2845 /*
2846 * Interface ioctls.
2847 *
2848 * Most of the routines called to handle the ioctls would end up being
2849 * tail-call optimized, which unfortunately causes this routine to
2850 * consume too much stack space; this is the reason for the "noinline"
2851 * attribute used on those routines.
2852 */
2853 int
2854 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
2855 {
2856 char ifname[IFNAMSIZ + 1];
2857 struct ifnet *ifp = NULL;
2858 struct ifstat *ifs = NULL;
2859 int error = 0;
2860
2861 bzero(ifname, sizeof(ifname));
2862
2863 /*
2864 * ioctls which don't require ifp, or ifreq ioctls
2865 */
2866 switch (cmd) {
2867 case OSIOCGIFCONF32: /* struct ifconf32 */
2868 case SIOCGIFCONF32: /* struct ifconf32 */
2869 case SIOCGIFCONF64: /* struct ifconf64 */
2870 case OSIOCGIFCONF64: /* struct ifconf64 */
2871 error = ifioctl_ifconf(cmd, data);
2872 goto done;
2873
2874 case SIOCIFGCLONERS32: /* struct if_clonereq32 */
2875 case SIOCIFGCLONERS64: /* struct if_clonereq64 */
2876 error = ifioctl_ifclone(cmd, data);
2877 goto done;
2878
2879 case SIOCGIFAGENTDATA32: /* struct netagent_req32 */
2880 case SIOCGIFAGENTDATA64: /* struct netagent_req64 */
2881 case SIOCGIFAGENTLIST32: /* struct netagentlist_req32 */
2882 case SIOCGIFAGENTLIST64: /* struct netagentlist_req64 */
2883 error = netagent_ioctl(cmd, data);
2884 goto done;
2885
2886 case SIOCSIFORDER: /* struct if_order */
2887 error = ifioctl_iforder(cmd, data);
2888 goto done;
2889
2890 case SIOCSIFDSTADDR: /* struct ifreq */
2891 case SIOCSIFADDR: /* struct ifreq */
2892 case SIOCSIFBRDADDR: /* struct ifreq */
2893 case SIOCSIFNETMASK: /* struct ifreq */
2894 case OSIOCGIFADDR: /* struct ifreq */
2895 case OSIOCGIFDSTADDR: /* struct ifreq */
2896 case OSIOCGIFBRDADDR: /* struct ifreq */
2897 case OSIOCGIFNETMASK: /* struct ifreq */
2898 case SIOCSIFKPI: /* struct ifreq */
2899 if (so->so_proto == NULL) {
2900 error = EOPNOTSUPP;
2901 goto done;
2902 }
2903 OS_FALLTHROUGH;
2904 case SIOCIFCREATE: /* struct ifreq */
2905 case SIOCIFCREATE2: /* struct ifreq */
2906 case SIOCIFDESTROY: /* struct ifreq */
2907 case SIOCGIFFLAGS: /* struct ifreq */
2908 case SIOCGIFEFLAGS: /* struct ifreq */
2909 case SIOCGIFCAP: /* struct ifreq */
2910 case SIOCGIFMETRIC: /* struct ifreq */
2911 case SIOCGIFMTU: /* struct ifreq */
2912 case SIOCGIFPHYS: /* struct ifreq */
2913 case SIOCSIFFLAGS: /* struct ifreq */
2914 case SIOCSIFCAP: /* struct ifreq */
2915 case SIOCSIFMETRIC: /* struct ifreq */
2916 case SIOCSIFPHYS: /* struct ifreq */
2917 case SIOCSIFMTU: /* struct ifreq */
2918 case SIOCADDMULTI: /* struct ifreq */
2919 case SIOCDELMULTI: /* struct ifreq */
2920 case SIOCDIFPHYADDR: /* struct ifreq */
2921 case SIOCSIFMEDIA: /* struct ifreq */
2922 case SIOCSIFGENERIC: /* struct ifreq */
2923 case SIOCSIFLLADDR: /* struct ifreq */
2924 case SIOCSIFALTMTU: /* struct ifreq */
2925 case SIOCSIFVLAN: /* struct ifreq */
2926 case SIOCSIFBOND: /* struct ifreq */
2927 case SIOCGIFLLADDR: /* struct ifreq */
2928 case SIOCGIFTYPE: /* struct ifreq */
2929 case SIOCGIFFUNCTIONALTYPE: /* struct ifreq */
2930 case SIOCGIFPSRCADDR: /* struct ifreq */
2931 case SIOCGIFPDSTADDR: /* struct ifreq */
2932 case SIOCGIFGENERIC: /* struct ifreq */
2933 case SIOCGIFDEVMTU: /* struct ifreq */
2934 case SIOCGIFVLAN: /* struct ifreq */
2935 case SIOCGIFBOND: /* struct ifreq */
2936 case SIOCGIFWAKEFLAGS: /* struct ifreq */
2937 case SIOCGIFGETRTREFCNT: /* struct ifreq */
2938 case SIOCSIFOPPORTUNISTIC: /* struct ifreq */
2939 case SIOCGIFOPPORTUNISTIC: /* struct ifreq */
2940 case SIOCGIFLINKQUALITYMETRIC: /* struct ifreq */
2941 case SIOCSIFLOG: /* struct ifreq */
2942 case SIOCGIFLOG: /* struct ifreq */
2943 case SIOCGIFDELEGATE: /* struct ifreq */
2944 case SIOCGIFEXPENSIVE: /* struct ifreq */
2945 case SIOCSIFEXPENSIVE: /* struct ifreq */
2946 case SIOCSIF2KCL: /* struct ifreq */
2947 case SIOCGIF2KCL: /* struct ifreq */
2948 case SIOCSIFINTERFACESTATE: /* struct ifreq */
2949 case SIOCGIFINTERFACESTATE: /* struct ifreq */
2950 case SIOCSIFPROBECONNECTIVITY: /* struct ifreq */
2951 case SIOCGIFPROBECONNECTIVITY: /* struct ifreq */
2952 case SIOCGSTARTDELAY: /* struct ifreq */
2953 case SIOCSIFTIMESTAMPENABLE: /* struct ifreq */
2954 case SIOCSIFTIMESTAMPDISABLE: /* struct ifreq */
2955 case SIOCGIFTIMESTAMPENABLED: /* struct ifreq */
2956 #if (DEBUG || DEVELOPMENT)
2957 case SIOCSIFDISABLEOUTPUT: /* struct ifreq */
2958 #endif /* (DEBUG || DEVELOPMENT) */
2959 case SIOCGECNMODE: /* struct ifreq */
2960 case SIOCSECNMODE:
2961 case SIOCSQOSMARKINGMODE: /* struct ifreq */
2962 case SIOCSQOSMARKINGENABLED: /* struct ifreq */
2963 case SIOCGQOSMARKINGMODE: /* struct ifreq */
2964 case SIOCGQOSMARKINGENABLED: /* struct ifreq */
2965 case SIOCSIFLOWINTERNET: /* struct ifreq */
2966 case SIOCGIFLOWINTERNET: /* struct ifreq */
2967 case SIOCGIFLOWPOWER: /* struct ifreq */
2968 case SIOCSIFLOWPOWER: /* struct ifreq */
2969 case SIOCSIF6LOWPAN: /* struct ifreq */
2970 case SIOCGIF6LOWPAN: /* struct ifreq */
2971 case SIOCGIFMPKLOG: /* struct ifreq */
2972 case SIOCSIFMPKLOG: /* struct ifreq */
2973 case SIOCGIFCONSTRAINED: /* struct ifreq */
2974 case SIOCSIFCONSTRAINED: /* struct ifreq */
2975 case SIOCGIFXFLAGS: /* struct ifreq */
2976 case SIOCGIFNOACKPRIO: /* struct ifreq */
2977 case SIOCSIFNOACKPRIO: /* struct ifreq */
2978 { /* struct ifreq */
2979 struct ifreq ifr;
2980 bcopy(data, &ifr, sizeof(ifr));
2981 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2982 bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
2983 if (ifioctl_restrict_intcoproc(cmd, ifname, NULL, p) == true) {
2984 error = EPERM;
2985 goto done;
2986 }
2987 error = ifioctl_ifreq(so, cmd, &ifr, p);
2988 bcopy(&ifr, data, sizeof(ifr));
2989 goto done;
2990 }
2991 }
2992
2993 /*
2994 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2995 * here to ensure that the ifnet, if found, has been fully attached.
2996 */
2997 dlil_if_lock();
2998 switch (cmd) {
2999 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
3000 bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
3001 ifname, IFNAMSIZ);
3002 ifp = ifunit_ref(ifname);
3003 break;
3004
3005 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
3006 bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
3007 ifname, IFNAMSIZ);
3008 ifp = ifunit_ref(ifname);
3009 break;
3010
3011 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
3012 bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
3013 ifname, IFNAMSIZ);
3014 ifp = ifunit_ref(ifname);
3015 break;
3016
3017 case SIOCGIFSTATUS: /* struct ifstat */
3018 ifs = _MALLOC(sizeof(*ifs), M_DEVBUF, M_WAITOK);
3019 if (ifs == NULL) {
3020 error = ENOMEM;
3021 dlil_if_unlock();
3022 goto done;
3023 }
3024 bcopy(data, ifs, sizeof(*ifs));
3025 ifs->ifs_name[IFNAMSIZ - 1] = '\0';
3026 bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
3027 ifp = ifunit_ref(ifname);
3028 break;
3029
3030 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
3031 case SIOCGIFXMEDIA32: /* struct ifmediareq32 */
3032 bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
3033 ifname, IFNAMSIZ);
3034 ifp = ifunit_ref(ifname);
3035 break;
3036
3037 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
3038 case SIOCGIFXMEDIA64: /* struct ifmediareq64 */
3039 bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
3040 ifname, IFNAMSIZ);
3041 ifp = ifunit_ref(ifname);
3042 break;
3043
3044 case SIOCSIFDESC: /* struct if_descreq */
3045 case SIOCGIFDESC: /* struct if_descreq */
3046 bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
3047 ifname, IFNAMSIZ);
3048 ifp = ifunit_ref(ifname);
3049 break;
3050
3051 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
3052 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
3053 bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
3054 ifname, IFNAMSIZ);
3055 ifp = ifunit_ref(ifname);
3056 break;
3057
3058 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
3059 bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
3060 ifname, IFNAMSIZ);
3061 ifp = ifunit_ref(ifname);
3062 break;
3063
3064 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
3065 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
3066 bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
3067 ifname, IFNAMSIZ);
3068 ifp = ifunit_ref(ifname);
3069 break;
3070
3071 case SIOCAIFAGENTID: /* struct if_agentidreq */
3072 case SIOCDIFAGENTID: /* struct if_agentidreq */
3073 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
3074 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
3075 bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
3076 ifname, IFNAMSIZ);
3077 ifp = ifunit_ref(ifname);
3078 break;
3079
3080 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
3081 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
3082 bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
3083 ifname, IFNAMSIZ);
3084 ifp = ifunit_ref(ifname);
3085 break;
3086
3087 case SIOCSIFNETWORKID: /* struct if_netidreq */
3088 bcopy(((struct if_netidreq *)(void *)data)->ifnetid_name,
3089 ifname, IFNAMSIZ);
3090 ifp = ifunit_ref(ifname);
3091 break;
3092 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
3093 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
3094 bcopy(((struct if_protolistreq *)(void *)data)->ifpl_name,
3095 ifname, IFNAMSIZ);
3096 ifp = ifunit_ref(ifname);
3097 break;
3098 default:
3099 /*
3100 * This is a bad assumption, but the code seems to
3101 * have been doing this in the past; caveat emptor.
3102 */
3103 bcopy(((struct ifreq *)(void *)data)->ifr_name,
3104 ifname, IFNAMSIZ);
3105 ifp = ifunit_ref(ifname);
3106 break;
3107 }
3108 dlil_if_unlock();
3109
3110 if (ifp == NULL) {
3111 error = ENXIO;
3112 goto done;
3113 }
3114
3115 if (ifioctl_restrict_intcoproc(cmd, NULL, ifp, p) == true) {
3116 error = EPERM;
3117 goto done;
3118 }
3119 switch (cmd) {
3120 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
3121 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
3122 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
3123 error = proc_suser(p);
3124 if (error != 0) {
3125 break;
3126 }
3127
3128 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
3129 if (error != 0) {
3130 break;
3131 }
3132
3133 ifnet_touch_lastchange(ifp);
3134 break;
3135
3136 case SIOCGIFSTATUS: /* struct ifstat */
3137 VERIFY(ifs != NULL);
3138 ifs->ascii[0] = '\0';
3139
3140 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
3141
3142 bcopy(ifs, data, sizeof(*ifs));
3143 break;
3144
3145 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
3146 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
3147 case SIOCGIFXMEDIA32: /* struct ifmediareq32 */
3148 case SIOCGIFXMEDIA64: /* struct ifmediareq64 */
3149 error = ifioctl_get_media(ifp, so, cmd, data);
3150 break;
3151
3152 case SIOCSIFDESC: /* struct if_descreq */
3153 case SIOCGIFDESC: /* struct if_descreq */
3154 error = ifioctl_ifdesc(ifp, cmd, data, p);
3155 break;
3156
3157 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
3158 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
3159 error = ifioctl_linkparams(ifp, cmd, data, p);
3160 break;
3161
3162 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
3163 error = ifioctl_qstats(ifp, cmd, data);
3164 break;
3165
3166 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
3167 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
3168 error = ifioctl_throttle(ifp, cmd, data, p);
3169 break;
3170
3171 case SIOCAIFAGENTID: /* struct if_agentidreq */
3172 case SIOCDIFAGENTID: /* struct if_agentidreq */
3173 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
3174 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
3175 error = ifioctl_netagent(ifp, cmd, data, p);
3176 break;
3177
3178 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
3179 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
3180 error = ifioctl_netsignature(ifp, cmd, data);
3181 break;
3182
3183 case SIOCSIFNETWORKID: /* struct if_netidreq */
3184 error = ifioctl_networkid(ifp, data);
3185 break;
3186 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
3187 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
3188 error = ifioctl_nat64prefix(ifp, cmd, data);
3189 break;
3190
3191 case SIOCGIFCLAT46ADDR: /* struct if_clat46req */
3192 error = ifioctl_clat46addr(ifp, cmd, data);
3193 break;
3194
3195 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
3196 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
3197 error = ifioctl_protolist(ifp, cmd, data);
3198 break;
3199
3200 default:
3201 if (so->so_proto == NULL) {
3202 error = EOPNOTSUPP;
3203 break;
3204 }
3205
3206 socket_lock(so, 1);
3207 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3208 data, ifp, p));
3209 socket_unlock(so, 1);
3210
3211 // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3212 // ifreq as the code expects ifaddr
3213 if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3214 !(cmd == SIOCAIFADDR || cmd == SIOCDIFADDR)) {
3215 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
3216 }
3217 break;
3218 }
3219
3220 done:
3221 if (ifs != NULL) {
3222 _FREE(ifs, M_DEVBUF);
3223 }
3224
3225 if (if_verbose) {
3226 if (ifname[0] == '\0') {
3227 (void) snprintf(ifname, sizeof(ifname), "%s",
3228 "NULL");
3229 } else if (ifp != NULL) {
3230 (void) snprintf(ifname, sizeof(ifname), "%s",
3231 if_name(ifp));
3232 }
3233
3234 if (error != 0) {
3235 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3236 "%c %lu) error %d\n", __func__,
3237 proc_name_address(p), proc_pid(p),
3238 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3239 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3240 (char)IOCGROUP(cmd), cmd & 0xff, error);
3241 } else if (if_verbose > 1) {
3242 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3243 "%c %lu) OK\n", __func__,
3244 proc_name_address(p), proc_pid(p),
3245 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3246 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3247 (char)IOCGROUP(cmd), cmd & 0xff);
3248 }
3249 }
3250
3251 if (ifp != NULL) {
3252 ifnet_decr_iorefcnt(ifp);
3253 }
3254 return error;
3255 }
3256
3257 static __attribute__((noinline)) int
3258 ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
3259 {
3260 struct ifnet *ifp;
3261 u_long ocmd = cmd;
3262 int error = 0;
3263 struct kev_msg ev_msg;
3264 struct net_event_data ev_data;
3265
3266 bzero(&ev_data, sizeof(struct net_event_data));
3267 bzero(&ev_msg, sizeof(struct kev_msg));
3268
3269 switch (cmd) {
3270 case SIOCIFCREATE:
3271 case SIOCIFCREATE2:
3272 error = proc_suser(p);
3273 if (error) {
3274 return error;
3275 }
3276 return if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
3277 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
3278 case SIOCIFDESTROY:
3279 error = proc_suser(p);
3280 if (error) {
3281 return error;
3282 }
3283 return if_clone_destroy(ifr->ifr_name);
3284 }
3285
3286 /*
3287 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
3288 * here to ensure that the ifnet, if found, has been fully attached.
3289 */
3290 dlil_if_lock();
3291 ifp = ifunit(ifr->ifr_name);
3292 dlil_if_unlock();
3293
3294 if (ifp == NULL) {
3295 return ENXIO;
3296 }
3297
3298 switch (cmd) {
3299 case SIOCGIFFLAGS:
3300 ifnet_lock_shared(ifp);
3301 ifr->ifr_flags = ifp->if_flags;
3302 ifnet_lock_done(ifp);
3303 break;
3304
3305 case SIOCGIFEFLAGS:
3306 ifnet_lock_shared(ifp);
3307 ifr->ifr_eflags = ifp->if_eflags;
3308 ifnet_lock_done(ifp);
3309 break;
3310
3311 case SIOCGIFXFLAGS:
3312 ifnet_lock_shared(ifp);
3313 ifr->ifr_xflags = ifp->if_xflags;
3314 ifnet_lock_done(ifp);
3315 break;
3316
3317 case SIOCGIFCAP:
3318 ifnet_lock_shared(ifp);
3319 ifr->ifr_reqcap = ifp->if_capabilities;
3320 ifr->ifr_curcap = ifp->if_capenable;
3321 ifnet_lock_done(ifp);
3322 break;
3323
3324 case SIOCGIFMETRIC:
3325 ifnet_lock_shared(ifp);
3326 ifr->ifr_metric = ifp->if_metric;
3327 ifnet_lock_done(ifp);
3328 break;
3329
3330 case SIOCGIFMTU:
3331 ifnet_lock_shared(ifp);
3332 ifr->ifr_mtu = ifp->if_mtu;
3333 ifnet_lock_done(ifp);
3334 break;
3335
3336 case SIOCGIFPHYS:
3337 ifnet_lock_shared(ifp);
3338 ifr->ifr_phys = ifp->if_physical;
3339 ifnet_lock_done(ifp);
3340 break;
3341
3342 case SIOCSIFFLAGS:
3343 error = proc_suser(p);
3344 if (error != 0) {
3345 break;
3346 }
3347
3348 (void) ifnet_set_flags(ifp, ifr->ifr_flags,
3349 (u_int16_t)~IFF_CANTCHANGE);
3350
3351 /*
3352 * Note that we intentionally ignore any error from below
3353 * for the SIOCSIFFLAGS case.
3354 */
3355 (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3356
3357 /*
3358 * Send the event even upon error from the driver because
3359 * we changed the flags.
3360 */
3361 dlil_post_sifflags_msg(ifp);
3362
3363 ifnet_touch_lastchange(ifp);
3364 break;
3365
3366 case SIOCSIFCAP:
3367 error = proc_suser(p);
3368 if (error != 0) {
3369 break;
3370 }
3371
3372 if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
3373 error = EINVAL;
3374 break;
3375 }
3376 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3377
3378 ifnet_touch_lastchange(ifp);
3379 break;
3380
3381 case SIOCSIFMETRIC:
3382 error = proc_suser(p);
3383 if (error != 0) {
3384 break;
3385 }
3386
3387 ifp->if_metric = ifr->ifr_metric;
3388
3389 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3390 ev_msg.kev_class = KEV_NETWORK_CLASS;
3391 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3392
3393 ev_msg.event_code = KEV_DL_SIFMETRICS;
3394 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3395 ev_data.if_family = ifp->if_family;
3396 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3397 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3398 ev_msg.dv[0].data_ptr = &ev_data;
3399
3400 ev_msg.dv[1].data_length = 0;
3401 dlil_post_complete_msg(ifp, &ev_msg);
3402
3403 ifnet_touch_lastchange(ifp);
3404 break;
3405
3406 case SIOCSIFPHYS:
3407 error = proc_suser(p);
3408 if (error != 0) {
3409 break;
3410 }
3411
3412 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3413 if (error != 0) {
3414 break;
3415 }
3416
3417 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3418 ev_msg.kev_class = KEV_NETWORK_CLASS;
3419 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3420
3421 ev_msg.event_code = KEV_DL_SIFPHYS;
3422 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3423 ev_data.if_family = ifp->if_family;
3424 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3425 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3426 ev_msg.dv[0].data_ptr = &ev_data;
3427 ev_msg.dv[1].data_length = 0;
3428 dlil_post_complete_msg(ifp, &ev_msg);
3429
3430 ifnet_touch_lastchange(ifp);
3431 break;
3432
3433 case SIOCSIFMTU: {
3434 u_int32_t oldmtu = ifp->if_mtu;
3435 struct ifclassq *ifq = &ifp->if_snd;
3436
3437 error = proc_suser(p);
3438 if (error != 0) {
3439 break;
3440 }
3441
3442 if (ifp->if_ioctl == NULL) {
3443 error = EOPNOTSUPP;
3444 break;
3445 }
3446 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
3447 error = EINVAL;
3448 break;
3449 }
3450 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3451 if (error != 0) {
3452 break;
3453 }
3454
3455 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3456 ev_msg.kev_class = KEV_NETWORK_CLASS;
3457 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3458
3459 ev_msg.event_code = KEV_DL_SIFMTU;
3460 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3461 ev_data.if_family = ifp->if_family;
3462 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3463 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3464 ev_msg.dv[0].data_ptr = &ev_data;
3465 ev_msg.dv[1].data_length = 0;
3466 dlil_post_complete_msg(ifp, &ev_msg);
3467
3468 ifnet_touch_lastchange(ifp);
3469 rt_ifmsg(ifp);
3470
3471 /*
3472 * If the link MTU changed, do network layer specific procedure
3473 * and update all route entries associated with the interface,
3474 * so that their MTU metric gets updated.
3475 */
3476 if (ifp->if_mtu != oldmtu) {
3477 if_rtmtu_update(ifp);
3478 nd6_setmtu(ifp);
3479 /* Inform all transmit queues about the new MTU */
3480 IFCQ_LOCK(ifq);
3481 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
3482 IFCQ_UNLOCK(ifq);
3483 }
3484 break;
3485 }
3486
3487 case SIOCADDMULTI:
3488 case SIOCDELMULTI:
3489 error = proc_suser(p);
3490 if (error != 0) {
3491 break;
3492 }
3493
3494 /* Don't allow group membership on non-multicast interfaces. */
3495 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
3496 error = EOPNOTSUPP;
3497 break;
3498 }
3499
3500 /* Don't let users screw up protocols' entries. */
3501 if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
3502 ifr->ifr_addr.sa_family != AF_LINK) {
3503 error = EINVAL;
3504 break;
3505 }
3506 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
3507 ifr->ifr_addr.sa_len = sizeof(struct sockaddr);
3508 }
3509
3510 /*
3511 * User is permitted to anonymously join a particular link
3512 * multicast group via SIOCADDMULTI. Subsequent join requested
3513 * for the same record which has an outstanding refcnt from a
3514 * past if_addmulti_anon() will not result in EADDRINUSE error
3515 * (unlike other BSDs.) Anonymously leaving a group is also
3516 * allowed only as long as there is an outstanding refcnt held
3517 * by a previous anonymous request, or else ENOENT (even if the
3518 * link-layer multicast membership exists for a network-layer
3519 * membership.)
3520 */
3521 if (cmd == SIOCADDMULTI) {
3522 error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
3523 ev_msg.event_code = KEV_DL_ADDMULTI;
3524 } else {
3525 error = if_delmulti_anon(ifp, &ifr->ifr_addr);
3526 ev_msg.event_code = KEV_DL_DELMULTI;
3527 }
3528 if (error != 0) {
3529 break;
3530 }
3531
3532 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3533 ev_msg.kev_class = KEV_NETWORK_CLASS;
3534 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3535 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3536
3537 ev_data.if_family = ifp->if_family;
3538 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3539 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3540 ev_msg.dv[0].data_ptr = &ev_data;
3541 ev_msg.dv[1].data_length = 0;
3542 dlil_post_complete_msg(ifp, &ev_msg);
3543
3544 ifnet_touch_lastchange(ifp);
3545 break;
3546
3547 case SIOCSIFMEDIA:
3548 error = proc_suser(p);
3549 if (error != 0) {
3550 break;
3551 }
3552 /*
3553 * Silently ignore setting IFM_OTHER
3554 */
3555 if (ifr->ifr_media == IFM_OTHER) {
3556 os_log_info(OS_LOG_DEFAULT,
3557 "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3558 __func__, ifp->if_xname);
3559 error = 0;
3560 break;
3561 }
3562 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3563 if (error != 0) {
3564 break;
3565 }
3566 ifnet_touch_lastchange(ifp);
3567 break;
3568
3569 case SIOCDIFPHYADDR:
3570 case SIOCSIFGENERIC:
3571 case SIOCSIFLLADDR:
3572 case SIOCSIFALTMTU:
3573 case SIOCSIFVLAN:
3574 case SIOCSIFBOND:
3575 case SIOCSIF6LOWPAN:
3576 error = proc_suser(p);
3577 if (error != 0) {
3578 break;
3579 }
3580
3581 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3582 if (error != 0) {
3583 break;
3584 }
3585
3586 ifnet_touch_lastchange(ifp);
3587 break;
3588
3589 case SIOCGIFLLADDR: {
3590 struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
3591
3592 if (sdl->sdl_alen == 0) {
3593 error = EADDRNOTAVAIL;
3594 break;
3595 }
3596 /* If larger than 14-bytes we'll need another mechanism */
3597 if (sdl->sdl_alen > sizeof(ifr->ifr_addr.sa_data)) {
3598 error = EMSGSIZE;
3599 break;
3600 }
3601 /* Follow the same convention used by SIOCSIFLLADDR */
3602 bzero(&ifr->ifr_addr, sizeof(ifr->ifr_addr));
3603 ifr->ifr_addr.sa_family = AF_LINK;
3604 ifr->ifr_addr.sa_len = sdl->sdl_alen;
3605 error = ifnet_guarded_lladdr_copy_bytes(ifp,
3606 &ifr->ifr_addr.sa_data, sdl->sdl_alen);
3607 break;
3608 }
3609
3610 case SIOCGIFTYPE:
3611 ifr->ifr_type.ift_type = ifp->if_type;
3612 ifr->ifr_type.ift_family = ifp->if_family;
3613 ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
3614 break;
3615
3616 case SIOCGIFFUNCTIONALTYPE:
3617 ifr->ifr_functional_type = if_functional_type(ifp, FALSE);
3618 break;
3619
3620 case SIOCGIFPSRCADDR:
3621 case SIOCGIFPDSTADDR:
3622 case SIOCGIFGENERIC:
3623 case SIOCGIFDEVMTU:
3624 case SIOCGIFVLAN:
3625 case SIOCGIFBOND:
3626 case SIOCGIF6LOWPAN:
3627 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3628 break;
3629
3630 case SIOCGIFWAKEFLAGS:
3631 ifnet_lock_shared(ifp);
3632 ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
3633 ifnet_lock_done(ifp);
3634 break;
3635
3636 case SIOCGIFGETRTREFCNT:
3637 ifnet_lock_shared(ifp);
3638 ifr->ifr_route_refcnt = ifp->if_route_refcnt;
3639 ifnet_lock_done(ifp);
3640 break;
3641
3642 case SIOCSIFOPPORTUNISTIC:
3643 case SIOCGIFOPPORTUNISTIC:
3644 error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
3645 break;
3646
3647 case SIOCGIFLINKQUALITYMETRIC:
3648 ifnet_lock_shared(ifp);
3649 if ((ifp->if_interface_state.valid_bitmask &
3650 IF_INTERFACE_STATE_LQM_STATE_VALID)) {
3651 ifr->ifr_link_quality_metric =
3652 ifp->if_interface_state.lqm_state;
3653 } else if (IF_FULLY_ATTACHED(ifp)) {
3654 ifr->ifr_link_quality_metric =
3655 IFNET_LQM_THRESH_UNKNOWN;
3656 } else {
3657 ifr->ifr_link_quality_metric =
3658 IFNET_LQM_THRESH_OFF;
3659 }
3660 ifnet_lock_done(ifp);
3661 break;
3662
3663 case SIOCSIFLOG:
3664 case SIOCGIFLOG:
3665 error = ifnet_getset_log(ifp, cmd, ifr, p);
3666 break;
3667
3668 case SIOCGIFDELEGATE:
3669 ifnet_lock_shared(ifp);
3670 ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
3671 ifp->if_delegated.ifp->if_index : 0);
3672 ifnet_lock_done(ifp);
3673 break;
3674
3675 case SIOCGIFEXPENSIVE:
3676 ifnet_lock_shared(ifp);
3677 if (ifp->if_eflags & IFEF_EXPENSIVE) {
3678 ifr->ifr_expensive = 1;
3679 } else {
3680 ifr->ifr_expensive = 0;
3681 }
3682 ifnet_lock_done(ifp);
3683 break;
3684
3685 case SIOCSIFEXPENSIVE:
3686 {
3687 struct ifnet *difp;
3688
3689 if ((error = priv_check_cred(kauth_cred_get(),
3690 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3691 return error;
3692 }
3693 if (ifr->ifr_expensive) {
3694 if_set_eflags(ifp, IFEF_EXPENSIVE);
3695 } else {
3696 if_clear_eflags(ifp, IFEF_EXPENSIVE);
3697 }
3698 ifnet_increment_generation(ifp);
3699
3700 /*
3701 * Update the expensive bit in the delegated interface
3702 * structure.
3703 */
3704 ifnet_head_lock_shared();
3705 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3706 ifnet_lock_exclusive(difp);
3707 if (difp->if_delegated.ifp == ifp) {
3708 difp->if_delegated.expensive =
3709 ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
3710 ifnet_increment_generation(difp);
3711 }
3712 ifnet_lock_done(difp);
3713 }
3714 ifnet_head_done();
3715 necp_update_all_clients();
3716 break;
3717 }
3718
3719 case SIOCGIFCONSTRAINED:
3720 if ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) {
3721 ifr->ifr_constrained = 1;
3722 } else {
3723 ifr->ifr_constrained = 0;
3724 }
3725 break;
3726
3727 case SIOCSIFCONSTRAINED:
3728 {
3729 struct ifnet *difp;
3730
3731 if ((error = priv_check_cred(kauth_cred_get(),
3732 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3733 return error;
3734 }
3735 if (ifr->ifr_constrained) {
3736 if_set_xflags(ifp, IFXF_CONSTRAINED);
3737 } else {
3738 if_clear_xflags(ifp, IFXF_CONSTRAINED);
3739 }
3740 ifnet_increment_generation(ifp);
3741 /*
3742 * Update the constrained bit in the delegated interface
3743 * structure.
3744 */
3745 ifnet_head_lock_shared();
3746 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3747 ifnet_lock_exclusive(difp);
3748 if (difp->if_delegated.ifp == ifp) {
3749 difp->if_delegated.constrained =
3750 ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) ? 1 : 0;
3751 ifnet_increment_generation(difp);
3752 }
3753 ifnet_lock_done(difp);
3754 }
3755 ifnet_head_done();
3756 necp_update_all_clients();
3757 break;
3758 }
3759
3760 case SIOCGIF2KCL:
3761 ifnet_lock_shared(ifp);
3762 if (ifp->if_eflags & IFEF_2KCL) {
3763 ifr->ifr_2kcl = 1;
3764 } else {
3765 ifr->ifr_2kcl = 0;
3766 }
3767 ifnet_lock_done(ifp);
3768 break;
3769
3770 case SIOCSIF2KCL:
3771 if ((error = priv_check_cred(kauth_cred_get(),
3772 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3773 return error;
3774 }
3775 if (ifr->ifr_2kcl) {
3776 if_set_eflags(ifp, IFEF_2KCL);
3777 } else {
3778 if_clear_eflags(ifp, IFEF_2KCL);
3779 }
3780 break;
3781 case SIOCGSTARTDELAY:
3782 ifnet_lock_shared(ifp);
3783 if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
3784 ifr->ifr_start_delay_qlen =
3785 ifp->if_start_delay_qlen;
3786 ifr->ifr_start_delay_timeout =
3787 ifp->if_start_delay_timeout;
3788 } else {
3789 ifr->ifr_start_delay_qlen = 0;
3790 ifr->ifr_start_delay_timeout = 0;
3791 }
3792 ifnet_lock_done(ifp);
3793 break;
3794 case SIOCSIFDSTADDR:
3795 case SIOCSIFADDR:
3796 case SIOCSIFBRDADDR:
3797 case SIOCSIFNETMASK:
3798 case OSIOCGIFADDR:
3799 case OSIOCGIFDSTADDR:
3800 case OSIOCGIFBRDADDR:
3801 case OSIOCGIFNETMASK:
3802 case SIOCSIFKPI:
3803 VERIFY(so->so_proto != NULL);
3804
3805 if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
3806 cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
3807 #if BYTE_ORDER != BIG_ENDIAN
3808 if (ifr->ifr_addr.sa_family == 0 &&
3809 ifr->ifr_addr.sa_len < 16) {
3810 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
3811 ifr->ifr_addr.sa_len = 16;
3812 }
3813 #else
3814 if (ifr->ifr_addr.sa_len == 0) {
3815 ifr->ifr_addr.sa_len = 16;
3816 }
3817 #endif
3818 } else if (cmd == OSIOCGIFADDR) {
3819 cmd = SIOCGIFADDR; /* struct ifreq */
3820 } else if (cmd == OSIOCGIFDSTADDR) {
3821 cmd = SIOCGIFDSTADDR; /* struct ifreq */
3822 } else if (cmd == OSIOCGIFBRDADDR) {
3823 cmd = SIOCGIFBRDADDR; /* struct ifreq */
3824 } else if (cmd == OSIOCGIFNETMASK) {
3825 cmd = SIOCGIFNETMASK; /* struct ifreq */
3826 }
3827
3828 socket_lock(so, 1);
3829 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3830 (caddr_t)ifr, ifp, p));
3831 socket_unlock(so, 1);
3832
3833 switch (ocmd) {
3834 case OSIOCGIFADDR:
3835 case OSIOCGIFDSTADDR:
3836 case OSIOCGIFBRDADDR:
3837 case OSIOCGIFNETMASK:
3838 bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
3839 sizeof(u_short));
3840 }
3841
3842 if (cmd == SIOCSIFKPI) {
3843 int temperr = proc_suser(p);
3844 if (temperr != 0) {
3845 error = temperr;
3846 }
3847 }
3848 // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3849 // with ifreq as the code expects ifaddr
3850 if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3851 !(cmd == SIOCSIFADDR || cmd == SIOCSIFDSTADDR)) {
3852 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3853 (caddr_t)ifr);
3854 }
3855 break;
3856
3857 case SIOCGIFINTERFACESTATE:
3858 if_get_state(ifp, &ifr->ifr_interface_state);
3859 break;
3860
3861 case SIOCSIFINTERFACESTATE:
3862 if ((error = priv_check_cred(kauth_cred_get(),
3863 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3864 return error;
3865 }
3866
3867 error = if_state_update(ifp, &ifr->ifr_interface_state);
3868
3869 break;
3870 case SIOCSIFPROBECONNECTIVITY:
3871 if ((error = priv_check_cred(kauth_cred_get(),
3872 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3873 return error;
3874 }
3875 error = if_probe_connectivity(ifp,
3876 ifr->ifr_probe_connectivity);
3877 break;
3878 case SIOCGIFPROBECONNECTIVITY:
3879 if ((error = priv_check_cred(kauth_cred_get(),
3880 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3881 return error;
3882 }
3883 if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY) {
3884 ifr->ifr_probe_connectivity = 1;
3885 } else {
3886 ifr->ifr_probe_connectivity = 0;
3887 }
3888 break;
3889 case SIOCGECNMODE:
3890 if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3891 IFEF_ECN_ENABLE) {
3892 ifr->ifr_ecn_mode = IFRTYPE_ECN_ENABLE;
3893 } else if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3894 IFEF_ECN_DISABLE) {
3895 ifr->ifr_ecn_mode = IFRTYPE_ECN_DISABLE;
3896 } else {
3897 ifr->ifr_ecn_mode = IFRTYPE_ECN_DEFAULT;
3898 }
3899 break;
3900 case SIOCSECNMODE:
3901 if ((error = priv_check_cred(kauth_cred_get(),
3902 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3903 return error;
3904 }
3905 if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DEFAULT) {
3906 if_clear_eflags(ifp, IFEF_ECN_ENABLE | IFEF_ECN_DISABLE);
3907 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_ENABLE) {
3908 if_set_eflags(ifp, IFEF_ECN_ENABLE);
3909 if_clear_eflags(ifp, IFEF_ECN_DISABLE);
3910 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DISABLE) {
3911 if_set_eflags(ifp, IFEF_ECN_DISABLE);
3912 if_clear_eflags(ifp, IFEF_ECN_ENABLE);
3913 } else {
3914 error = EINVAL;
3915 }
3916 break;
3917
3918 case SIOCSIFTIMESTAMPENABLE:
3919 case SIOCSIFTIMESTAMPDISABLE:
3920 error = proc_suser(p);
3921 if (error != 0) {
3922 break;
3923 }
3924
3925 if ((cmd == SIOCSIFTIMESTAMPENABLE &&
3926 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) ||
3927 (cmd == SIOCSIFTIMESTAMPDISABLE &&
3928 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) == 0)) {
3929 break;
3930 }
3931 if (cmd == SIOCSIFTIMESTAMPENABLE) {
3932 if_set_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
3933 } else {
3934 if_clear_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
3935 }
3936 /*
3937 * Pass the setting to the interface if it supports either
3938 * software or hardware time stamping
3939 */
3940 if (ifp->if_capabilities & (IFCAP_HW_TIMESTAMP |
3941 IFCAP_SW_TIMESTAMP)) {
3942 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3943 (caddr_t)ifr);
3944 }
3945 break;
3946 case SIOCGIFTIMESTAMPENABLED: {
3947 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
3948 ifr->ifr_intval = 1;
3949 } else {
3950 ifr->ifr_intval = 0;
3951 }
3952 break;
3953 }
3954 case SIOCSQOSMARKINGMODE:
3955 if ((error = priv_check_cred(kauth_cred_get(),
3956 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3957 return error;
3958 }
3959 error = if_set_qosmarking_mode(ifp, ifr->ifr_qosmarking_mode);
3960 break;
3961
3962 case SIOCGQOSMARKINGMODE:
3963 ifr->ifr_qosmarking_mode = ifp->if_qosmarking_mode;
3964 break;
3965
3966 case SIOCSQOSMARKINGENABLED:
3967 if ((error = priv_check_cred(kauth_cred_get(),
3968 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3969 return error;
3970 }
3971 if (ifr->ifr_qosmarking_enabled != 0) {
3972 if_set_eflags(ifp, IFEF_QOSMARKING_ENABLED);
3973 } else {
3974 if_clear_eflags(ifp, IFEF_QOSMARKING_ENABLED);
3975 }
3976 break;
3977
3978 case SIOCGQOSMARKINGENABLED:
3979 ifr->ifr_qosmarking_enabled =
3980 ((ifp->if_eflags & IFEF_QOSMARKING_ENABLED) != 0) ? 1 : 0;
3981 break;
3982
3983 case SIOCSIFDISABLEOUTPUT:
3984 #if (DEBUG || DEVELOPMENT)
3985 if (ifr->ifr_disable_output == 1) {
3986 error = ifnet_disable_output(ifp);
3987 } else if (ifr->ifr_disable_output == 0) {
3988 error = ifnet_enable_output(ifp);
3989 } else {
3990 error = EINVAL;
3991 }
3992 #else
3993 error = EINVAL;
3994 #endif /* (DEBUG || DEVELOPMENT) */
3995 break;
3996
3997 case SIOCSIFSUBFAMILY:
3998 if ((error = priv_check_cred(kauth_cred_get(),
3999 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4000 return error;
4001 }
4002 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
4003 break;
4004
4005 case SIOCSIFLOWINTERNET:
4006 if ((error = priv_check_cred(kauth_cred_get(),
4007 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4008 return error;
4009 }
4010
4011 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_UL) {
4012 if_set_xflags(ifp, IFXF_LOW_INTERNET_UL);
4013 } else {
4014 if_clear_xflags(ifp, IFXF_LOW_INTERNET_UL);
4015 }
4016 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_DL) {
4017 if_set_xflags(ifp, IFXF_LOW_INTERNET_DL);
4018 } else {
4019 if_clear_xflags(ifp, IFXF_LOW_INTERNET_DL);
4020 }
4021 break;
4022 case SIOCGIFLOWINTERNET:
4023 ifnet_lock_shared(ifp);
4024 ifr->ifr_low_internet = 0;
4025 if ((ifp->if_xflags & IFXF_LOW_INTERNET_UL) != 0) {
4026 ifr->ifr_low_internet |=
4027 IFRTYPE_LOW_INTERNET_ENABLE_UL;
4028 }
4029 if ((ifp->if_xflags & IFXF_LOW_INTERNET_DL) != 0) {
4030 ifr->ifr_low_internet |=
4031 IFRTYPE_LOW_INTERNET_ENABLE_DL;
4032 }
4033 ifnet_lock_done(ifp);
4034 break;
4035 case SIOCGIFLOWPOWER:
4036 ifr->ifr_low_power_mode =
4037 ((ifp->if_xflags & IFXF_LOW_POWER) != 0);
4038 break;
4039 case SIOCSIFLOWPOWER:
4040 #if (DEVELOPMENT || DEBUG)
4041 error = if_set_low_power(ifp, (ifr->ifr_low_power_mode != 0));
4042 #else /* DEVELOPMENT || DEBUG */
4043 error = EOPNOTSUPP;
4044 #endif /* DEVELOPMENT || DEBUG */
4045 break;
4046
4047 case SIOCGIFMPKLOG:
4048 ifr->ifr_mpk_log = ((ifp->if_xflags & IFXF_MPK_LOG) != 0);
4049 break;
4050 case SIOCSIFMPKLOG:
4051 if (ifr->ifr_mpk_log) {
4052 if_set_xflags(ifp, IFXF_MPK_LOG);
4053 } else {
4054 if_clear_xflags(ifp, IFXF_MPK_LOG);
4055 }
4056 break;
4057 case SIOCGIFNOACKPRIO:
4058 if ((ifp->if_eflags & IFEF_NOACKPRI) != 0) {
4059 ifr->ifr_noack_prio = 1;
4060 } else {
4061 ifr->ifr_noack_prio = 0;
4062 }
4063 break;
4064
4065 case SIOCSIFNOACKPRIO:
4066 if ((error = priv_check_cred(kauth_cred_get(),
4067 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4068 return error;
4069 }
4070 if (ifr->ifr_noack_prio) {
4071 if_set_eflags(ifp, IFEF_NOACKPRI);
4072 } else {
4073 if_clear_eflags(ifp, IFEF_NOACKPRI);
4074 }
4075 break;
4076
4077 default:
4078 VERIFY(0);
4079 /* NOTREACHED */
4080 }
4081
4082 return error;
4083 }
4084
4085 int
4086 ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
4087 {
4088 int error;
4089
4090 socket_unlock(so, 0);
4091 error = ifioctl(so, cmd, data, p);
4092 socket_lock(so, 0);
4093 return error;
4094 }
4095
4096 /*
4097 * Set/clear promiscuous mode on interface ifp based on the truth value
4098 * of pswitch. The calls are reference counted so that only the first
4099 * "on" request actually has an effect, as does the final "off" request.
4100 * Results are undefined if the "off" and "on" requests are not matched.
4101 */
4102 errno_t
4103 ifnet_set_promiscuous(
4104 ifnet_t ifp,
4105 int pswitch)
4106 {
4107 int error = 0;
4108 int oldflags = 0;
4109 int newflags = 0;
4110
4111 ifnet_lock_exclusive(ifp);
4112 oldflags = ifp->if_flags;
4113 ifp->if_pcount += pswitch ? 1 : -1;
4114
4115 if (ifp->if_pcount > 0) {
4116 ifp->if_flags |= IFF_PROMISC;
4117 } else {
4118 ifp->if_flags &= ~IFF_PROMISC;
4119 }
4120
4121 newflags = ifp->if_flags;
4122 ifnet_lock_done(ifp);
4123
4124 if (newflags != oldflags && (newflags & IFF_UP) != 0) {
4125 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
4126 if (error == 0) {
4127 rt_ifmsg(ifp);
4128 } else {
4129 ifnet_lock_exclusive(ifp);
4130 // revert the flags
4131 ifp->if_pcount -= pswitch ? 1 : -1;
4132 if (ifp->if_pcount > 0) {
4133 ifp->if_flags |= IFF_PROMISC;
4134 } else {
4135 ifp->if_flags &= ~IFF_PROMISC;
4136 }
4137 ifnet_lock_done(ifp);
4138 }
4139 }
4140
4141 if (newflags != oldflags) {
4142 log(LOG_INFO, "%s: promiscuous mode %s%s\n",
4143 if_name(ifp),
4144 (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
4145 error != 0 ? " failed" : " succeeded");
4146 }
4147 return error;
4148 }
4149
4150 /*
4151 * Return interface configuration
4152 * of system. List may be used
4153 * in later ioctl's (above) to get
4154 * other information.
4155 */
4156 /*ARGSUSED*/
4157 static int
4158 ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
4159 {
4160 struct ifnet *ifp = NULL;
4161 struct ifaddr *ifa;
4162 struct ifreq ifr;
4163 int error = 0;
4164 size_t space;
4165 net_thread_marks_t marks;
4166
4167 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
4168
4169 /*
4170 * Zero the ifr buffer to make sure we don't
4171 * disclose the contents of the stack.
4172 */
4173 bzero(&ifr, sizeof(struct ifreq));
4174
4175 space = *ret_space;
4176 ifnet_head_lock_shared();
4177 for (ifp = ifnet_head.tqh_first; space > sizeof(ifr) &&
4178 ifp; ifp = ifp->if_link.tqe_next) {
4179 char workbuf[64];
4180 size_t ifnlen, addrs;
4181
4182 ifnlen = snprintf(workbuf, sizeof(workbuf),
4183 "%s", if_name(ifp));
4184 if (ifnlen + 1 > sizeof(ifr.ifr_name)) {
4185 error = ENAMETOOLONG;
4186 break;
4187 } else {
4188 strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
4189 }
4190
4191 ifnet_lock_shared(ifp);
4192
4193 addrs = 0;
4194 ifa = ifp->if_addrhead.tqh_first;
4195 for (; space > sizeof(ifr) && ifa;
4196 ifa = ifa->ifa_link.tqe_next) {
4197 struct sockaddr *sa;
4198 union {
4199 struct sockaddr sa;
4200 struct sockaddr_dl sdl;
4201 uint8_t buf[SOCK_MAXADDRLEN + 1];
4202 } u;
4203
4204 /*
4205 * Make sure to accomodate the largest possible
4206 * size of SA(if_lladdr)->sa_len.
4207 */
4208 _CASSERT(sizeof(u) == (SOCK_MAXADDRLEN + 1));
4209
4210 IFA_LOCK(ifa);
4211 sa = ifa->ifa_addr;
4212 addrs++;
4213
4214 if (ifa == ifp->if_lladdr) {
4215 VERIFY(sa->sa_family == AF_LINK);
4216 bcopy(sa, &u, sa->sa_len);
4217 IFA_UNLOCK(ifa);
4218 ifnet_guarded_lladdr_copy_bytes(ifp,
4219 LLADDR(&u.sdl), u.sdl.sdl_alen);
4220 IFA_LOCK(ifa);
4221 sa = &u.sa;
4222 }
4223
4224 if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
4225 struct osockaddr *osa =
4226 (struct osockaddr *)(void *)&ifr.ifr_addr;
4227 ifr.ifr_addr = *sa;
4228 osa->sa_family = sa->sa_family;
4229 error = copyout((caddr_t)&ifr, ifrp,
4230 sizeof(ifr));
4231 ifrp += sizeof(struct ifreq);
4232 } else if (sa->sa_len <= sizeof(*sa)) {
4233 ifr.ifr_addr = *sa;
4234 error = copyout((caddr_t)&ifr, ifrp,
4235 sizeof(ifr));
4236 ifrp += sizeof(struct ifreq);
4237 } else {
4238 if (space <
4239 sizeof(ifr) + sa->sa_len - sizeof(*sa)) {
4240 IFA_UNLOCK(ifa);
4241 break;
4242 }
4243 space -= sa->sa_len - sizeof(*sa);
4244 error = copyout((caddr_t)&ifr, ifrp,
4245 sizeof(ifr.ifr_name));
4246 if (error == 0) {
4247 error = copyout((caddr_t)sa, (ifrp +
4248 offsetof(struct ifreq, ifr_addr)),
4249 sa->sa_len);
4250 }
4251 ifrp += (sa->sa_len + offsetof(struct ifreq,
4252 ifr_addr));
4253 }
4254 IFA_UNLOCK(ifa);
4255 if (error) {
4256 break;
4257 }
4258 space -= sizeof(ifr);
4259 }
4260 ifnet_lock_done(ifp);
4261
4262 if (error) {
4263 break;
4264 }
4265 if (!addrs) {
4266 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
4267 error = copyout((caddr_t)&ifr, ifrp, sizeof(ifr));
4268 if (error) {
4269 break;
4270 }
4271 space -= sizeof(ifr);
4272 ifrp += sizeof(struct ifreq);
4273 }
4274 }
4275 ifnet_head_done();
4276 *ret_space -= space;
4277 net_thread_marks_pop(marks);
4278 return error;
4279 }
4280
4281 /*
4282 * Just like if_promisc(), but for all-multicast-reception mode.
4283 */
4284 int
4285 if_allmulti(struct ifnet *ifp, int onswitch)
4286 {
4287 int error = 0;
4288 int modified = 0;
4289
4290 ifnet_lock_exclusive(ifp);
4291
4292 if (onswitch) {
4293 if (ifp->if_amcount++ == 0) {
4294 ifp->if_flags |= IFF_ALLMULTI;
4295 modified = 1;
4296 }
4297 } else {
4298 if (ifp->if_amcount > 1) {
4299 ifp->if_amcount--;
4300 } else {
4301 ifp->if_amcount = 0;
4302 ifp->if_flags &= ~IFF_ALLMULTI;
4303 modified = 1;
4304 }
4305 }
4306 ifnet_lock_done(ifp);
4307
4308 if (modified) {
4309 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
4310 }
4311
4312 if (error == 0) {
4313 rt_ifmsg(ifp);
4314 }
4315 return error;
4316 }
4317
4318 static struct ifmultiaddr *
4319 ifma_alloc(int how)
4320 {
4321 struct ifmultiaddr *ifma;
4322
4323 ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
4324 zalloc_noblock(ifma_zone);
4325
4326 if (ifma != NULL) {
4327 bzero(ifma, ifma_size);
4328 lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
4329 ifma->ifma_debug |= IFD_ALLOC;
4330 if (ifma_debug != 0) {
4331 ifma->ifma_debug |= IFD_DEBUG;
4332 ifma->ifma_trace = ifma_trace;
4333 }
4334 }
4335 return ifma;
4336 }
4337
4338 static void
4339 ifma_free(struct ifmultiaddr *ifma)
4340 {
4341 IFMA_LOCK(ifma);
4342
4343 if (ifma->ifma_protospec != NULL) {
4344 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4345 /* NOTREACHED */
4346 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4347 ifma->ifma_anoncnt != 0) {
4348 panic("%s: Freeing ifma=%p with outstanding anon req",
4349 __func__, ifma);
4350 /* NOTREACHED */
4351 } else if (ifma->ifma_debug & IFD_ATTACHED) {
4352 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4353 __func__, ifma, ifma->ifma_ifp);
4354 /* NOTREACHED */
4355 } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
4356 panic("%s: ifma %p cannot be freed", __func__, ifma);
4357 /* NOTREACHED */
4358 } else if (ifma->ifma_refcount != 0) {
4359 panic("%s: non-zero refcount ifma=%p", __func__, ifma);
4360 /* NOTREACHED */
4361 } else if (ifma->ifma_reqcnt != 0) {
4362 panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
4363 /* NOTREACHED */
4364 } else if (ifma->ifma_ifp != NULL) {
4365 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
4366 ifma->ifma_ifp, ifma);
4367 /* NOTREACHED */
4368 } else if (ifma->ifma_ll != NULL) {
4369 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
4370 ifma->ifma_ll, ifma);
4371 /* NOTREACHED */
4372 }
4373 ifma->ifma_debug &= ~IFD_ALLOC;
4374 if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
4375 (IFD_DEBUG | IFD_TRASHED)) {
4376 lck_mtx_lock(&ifma_trash_lock);
4377 TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
4378 ifma_trash_link);
4379 lck_mtx_unlock(&ifma_trash_lock);
4380 ifma->ifma_debug &= ~IFD_TRASHED;
4381 }
4382 IFMA_UNLOCK(ifma);
4383
4384 if (ifma->ifma_addr != NULL) {
4385 FREE(ifma->ifma_addr, M_IFADDR);
4386 ifma->ifma_addr = NULL;
4387 }
4388 lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
4389 zfree(ifma_zone, ifma);
4390 }
4391
4392 static void
4393 ifma_trace(struct ifmultiaddr *ifma, int refhold)
4394 {
4395 struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
4396 ctrace_t *tr;
4397 u_int32_t idx;
4398 u_int16_t *cnt;
4399
4400 if (!(ifma->ifma_debug & IFD_DEBUG)) {
4401 panic("%s: ifma %p has no debug structure", __func__, ifma);
4402 /* NOTREACHED */
4403 }
4404 if (refhold) {
4405 cnt = &ifma_dbg->ifma_refhold_cnt;
4406 tr = ifma_dbg->ifma_refhold;
4407 } else {
4408 cnt = &ifma_dbg->ifma_refrele_cnt;
4409 tr = ifma_dbg->ifma_refrele;
4410 }
4411
4412 idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
4413 ctrace_record(&tr[idx]);
4414 }
4415
4416 void
4417 ifma_addref(struct ifmultiaddr *ifma, int locked)
4418 {
4419 if (!locked) {
4420 IFMA_LOCK(ifma);
4421 } else {
4422 IFMA_LOCK_ASSERT_HELD(ifma);
4423 }
4424
4425 if (++ifma->ifma_refcount == 0) {
4426 panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
4427 /* NOTREACHED */
4428 } else if (ifma->ifma_trace != NULL) {
4429 (*ifma->ifma_trace)(ifma, TRUE);
4430 }
4431 if (!locked) {
4432 IFMA_UNLOCK(ifma);
4433 }
4434 }
4435
4436 void
4437 ifma_remref(struct ifmultiaddr *ifma)
4438 {
4439 struct ifmultiaddr *ll;
4440
4441 IFMA_LOCK(ifma);
4442
4443 if (ifma->ifma_refcount == 0) {
4444 panic("%s: ifma=%p negative refcnt", __func__, ifma);
4445 /* NOTREACHED */
4446 } else if (ifma->ifma_trace != NULL) {
4447 (*ifma->ifma_trace)(ifma, FALSE);
4448 }
4449
4450 --ifma->ifma_refcount;
4451 if (ifma->ifma_refcount > 0) {
4452 IFMA_UNLOCK(ifma);
4453 return;
4454 }
4455
4456 ll = ifma->ifma_ll;
4457 ifma->ifma_ifp = NULL;
4458 ifma->ifma_ll = NULL;
4459 IFMA_UNLOCK(ifma);
4460 ifma_free(ifma); /* deallocate it */
4461
4462 if (ll != NULL) {
4463 IFMA_REMREF(ll);
4464 }
4465 }
4466
4467 static void
4468 if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4469 {
4470 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4471 IFMA_LOCK_ASSERT_HELD(ifma);
4472
4473 if (ifma->ifma_ifp != ifp) {
4474 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
4475 ifma->ifma_ifp, ifp);
4476 /* NOTREACHED */
4477 } else if (ifma->ifma_debug & IFD_ATTACHED) {
4478 panic("%s: Attempt to attach an already attached ifma=%p",
4479 __func__, ifma);
4480 /* NOTREACHED */
4481 } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4482 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
4483 /* NOTREACHED */
4484 } else if (ifma->ifma_debug & IFD_TRASHED) {
4485 panic("%s: Attempt to reattach a detached ifma=%p",
4486 __func__, ifma);
4487 /* NOTREACHED */
4488 }
4489
4490 ifma->ifma_reqcnt++;
4491 VERIFY(ifma->ifma_reqcnt == 1);
4492 IFMA_ADDREF_LOCKED(ifma);
4493 ifma->ifma_debug |= IFD_ATTACHED;
4494 if (anon) {
4495 ifma->ifma_anoncnt++;
4496 VERIFY(ifma->ifma_anoncnt == 1);
4497 ifma->ifma_flags |= IFMAF_ANONYMOUS;
4498 }
4499
4500 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
4501 }
4502
4503 static int
4504 if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4505 {
4506 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4507 IFMA_LOCK_ASSERT_HELD(ifma);
4508
4509 if (ifma->ifma_reqcnt == 0) {
4510 panic("%s: ifma=%p negative reqcnt", __func__, ifma);
4511 /* NOTREACHED */
4512 } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4513 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
4514 /* NOTREACHED */
4515 } else if (anon && ifma->ifma_anoncnt == 0) {
4516 panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
4517 /* NOTREACHED */
4518 } else if (ifma->ifma_ifp != ifp) {
4519 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
4520 ifma->ifma_ifp, ifp);
4521 /* NOTREACHED */
4522 }
4523
4524 if (anon) {
4525 --ifma->ifma_anoncnt;
4526 if (ifma->ifma_anoncnt > 0) {
4527 return 0;
4528 }
4529 ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
4530 }
4531
4532 --ifma->ifma_reqcnt;
4533 if (ifma->ifma_reqcnt > 0) {
4534 return 0;
4535 }
4536
4537 if (ifma->ifma_protospec != NULL) {
4538 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4539 /* NOTREACHED */
4540 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4541 ifma->ifma_anoncnt != 0) {
4542 panic("%s: Detaching ifma=%p with outstanding anon req",
4543 __func__, ifma);
4544 /* NOTREACHED */
4545 } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
4546 panic("%s: Attempt to detach an unattached address ifma=%p",
4547 __func__, ifma);
4548 /* NOTREACHED */
4549 } else if (ifma->ifma_debug & IFD_TRASHED) {
4550 panic("%s: ifma %p is already in trash list", __func__, ifma);
4551 /* NOTREACHED */
4552 }
4553
4554 /*
4555 * NOTE: Caller calls IFMA_REMREF
4556 */
4557 ifma->ifma_debug &= ~IFD_ATTACHED;
4558 LIST_REMOVE(ifma, ifma_link);
4559 if (LIST_EMPTY(&ifp->if_multiaddrs)) {
4560 ifp->if_updatemcasts = 0;
4561 }
4562
4563 if (ifma->ifma_debug & IFD_DEBUG) {
4564 /* Become a regular mutex, just in case */
4565 IFMA_CONVERT_LOCK(ifma);
4566 lck_mtx_lock(&ifma_trash_lock);
4567 TAILQ_INSERT_TAIL(&ifma_trash_head,
4568 (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
4569 lck_mtx_unlock(&ifma_trash_lock);
4570 ifma->ifma_debug |= IFD_TRASHED;
4571 }
4572
4573 return 1;
4574 }
4575
4576 /*
4577 * Find an ifmultiaddr that matches a socket address on an interface.
4578 *
4579 * Caller is responsible for holding the ifnet_lock while calling
4580 * this function.
4581 */
4582 static int
4583 if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
4584 struct ifmultiaddr **retifma, int anon)
4585 {
4586 struct ifmultiaddr *ifma;
4587
4588 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4589 ifma = LIST_NEXT(ifma, ifma_link)) {
4590 IFMA_LOCK_SPIN(ifma);
4591 if (!ifa_equal(sa, ifma->ifma_addr)) {
4592 IFMA_UNLOCK(ifma);
4593 continue;
4594 }
4595 if (anon) {
4596 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4597 ifma->ifma_anoncnt != 0);
4598 VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4599 ifma->ifma_anoncnt == 0);
4600 ifma->ifma_anoncnt++;
4601 if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4602 VERIFY(ifma->ifma_anoncnt == 1);
4603 ifma->ifma_flags |= IFMAF_ANONYMOUS;
4604 }
4605 }
4606 if (!anon || ifma->ifma_anoncnt == 1) {
4607 ifma->ifma_reqcnt++;
4608 VERIFY(ifma->ifma_reqcnt > 1);
4609 }
4610 if (retifma != NULL) {
4611 *retifma = ifma;
4612 IFMA_ADDREF_LOCKED(ifma);
4613 }
4614 IFMA_UNLOCK(ifma);
4615 return 0;
4616 }
4617 return ENOENT;
4618 }
4619
4620 /*
4621 * Radar 3642395, make sure all multicasts are in a standard format.
4622 */
4623 static struct sockaddr *
4624 copy_and_normalize(const struct sockaddr *original)
4625 {
4626 int alen = 0;
4627 const u_char *aptr = NULL;
4628 struct sockaddr *copy = NULL;
4629 struct sockaddr_dl *sdl_new = NULL;
4630 int len = 0;
4631
4632 if (original->sa_family != AF_LINK &&
4633 original->sa_family != AF_UNSPEC) {
4634 /* Just make a copy */
4635 MALLOC(copy, struct sockaddr *, original->sa_len,
4636 M_IFADDR, M_WAITOK);
4637 if (copy != NULL) {
4638 bcopy(original, copy, original->sa_len);
4639 }
4640 return copy;
4641 }
4642
4643 switch (original->sa_family) {
4644 case AF_LINK: {
4645 const struct sockaddr_dl *sdl_original =
4646 (struct sockaddr_dl *)(uintptr_t)(size_t)original;
4647
4648 if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
4649 sdl_original->sdl_slen +
4650 offsetof(struct sockaddr_dl, sdl_data) >
4651 sdl_original->sdl_len) {
4652 return NULL;
4653 }
4654
4655 alen = sdl_original->sdl_alen;
4656 aptr = CONST_LLADDR(sdl_original);
4657 }
4658 break;
4659
4660 case AF_UNSPEC: {
4661 if (original->sa_len < ETHER_ADDR_LEN +
4662 offsetof(struct sockaddr, sa_data)) {
4663 return NULL;
4664 }
4665
4666 alen = ETHER_ADDR_LEN;
4667 aptr = (const u_char *)original->sa_data;
4668 }
4669 break;
4670 }
4671
4672 if (alen == 0 || aptr == NULL) {
4673 return NULL;
4674 }
4675
4676 len = alen + offsetof(struct sockaddr_dl, sdl_data);
4677 MALLOC(sdl_new, struct sockaddr_dl *, len, M_IFADDR, M_WAITOK);
4678
4679 if (sdl_new != NULL) {
4680 bzero(sdl_new, len);
4681 sdl_new->sdl_len = len;
4682 sdl_new->sdl_family = AF_LINK;
4683 sdl_new->sdl_alen = alen;
4684 bcopy(aptr, LLADDR(sdl_new), alen);
4685 }
4686
4687 return (struct sockaddr *)sdl_new;
4688 }
4689
4690 /*
4691 * Network-layer protocol domains which hold references to the underlying
4692 * link-layer record must use this routine.
4693 */
4694 int
4695 if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
4696 struct ifmultiaddr **retifma)
4697 {
4698 return if_addmulti_common(ifp, sa, retifma, 0);
4699 }
4700
4701 /*
4702 * Anything other than network-layer protocol domains which hold references
4703 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4704 * ioctl, ifnet_add_multicast(), if_bond.
4705 */
4706 int
4707 if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
4708 struct ifmultiaddr **retifma)
4709 {
4710 return if_addmulti_common(ifp, sa, retifma, 1);
4711 }
4712
4713 /*
4714 * Register an additional multicast address with a network interface.
4715 *
4716 * - If the address is already present, bump the reference count on the
4717 * address and return.
4718 * - If the address is not link-layer, look up a link layer address.
4719 * - Allocate address structures for one or both addresses, and attach to the
4720 * multicast address list on the interface. If automatically adding a link
4721 * layer address, the protocol address will own a reference to the link
4722 * layer address, to be freed when it is freed.
4723 * - Notify the network device driver of an addition to the multicast address
4724 * list.
4725 *
4726 * 'sa' points to caller-owned memory with the desired multicast address.
4727 *
4728 * 'retifma' will be used to return a pointer to the resulting multicast
4729 * address reference, if desired.
4730 *
4731 * 'anon' indicates a link-layer address with no protocol address reference
4732 * made to it. Anything other than network-layer protocol domain requests
4733 * are considered as anonymous.
4734 */
4735 static int
4736 if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
4737 struct ifmultiaddr **retifma, int anon)
4738 {
4739 struct sockaddr_storage storage;
4740 struct sockaddr *llsa = NULL;
4741 struct sockaddr *dupsa = NULL;
4742 int error = 0, ll_firstref = 0, lladdr;
4743 struct ifmultiaddr *ifma = NULL;
4744 struct ifmultiaddr *llifma = NULL;
4745
4746 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4747 VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
4748 sa->sa_family == AF_LINK);
4749
4750 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4751 if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
4752 dupsa = copy_and_normalize(sa);
4753 if (dupsa == NULL) {
4754 error = ENOMEM;
4755 goto cleanup;
4756 }
4757 sa = dupsa;
4758 }
4759
4760 ifnet_lock_exclusive(ifp);
4761 if (!(ifp->if_flags & IFF_MULTICAST)) {
4762 error = EADDRNOTAVAIL;
4763 ifnet_lock_done(ifp);
4764 goto cleanup;
4765 }
4766
4767 /* If the address is already present, return a new reference to it */
4768 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4769 ifnet_lock_done(ifp);
4770 if (error == 0) {
4771 goto cleanup;
4772 }
4773
4774 /*
4775 * The address isn't already present; give the link layer a chance
4776 * to accept/reject it, and also find out which AF_LINK address this
4777 * maps to, if it isn't one already.
4778 */
4779 error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
4780 sizeof(storage));
4781 if (error == 0 && storage.ss_len != 0) {
4782 llsa = copy_and_normalize((struct sockaddr *)&storage);
4783 if (llsa == NULL) {
4784 error = ENOMEM;
4785 goto cleanup;
4786 }
4787
4788 llifma = ifma_alloc(M_WAITOK);
4789 if (llifma == NULL) {
4790 error = ENOMEM;
4791 goto cleanup;
4792 }
4793 }
4794
4795 /* to be similar to FreeBSD */
4796 if (error == EOPNOTSUPP) {
4797 error = 0;
4798 } else if (error != 0) {
4799 goto cleanup;
4800 }
4801
4802 /* Allocate while we aren't holding any locks */
4803 if (dupsa == NULL) {
4804 dupsa = copy_and_normalize(sa);
4805 if (dupsa == NULL) {
4806 error = ENOMEM;
4807 goto cleanup;
4808 }
4809 }
4810 ifma = ifma_alloc(M_WAITOK);
4811 if (ifma == NULL) {
4812 error = ENOMEM;
4813 goto cleanup;
4814 }
4815
4816 ifnet_lock_exclusive(ifp);
4817 /*
4818 * Check again for the matching multicast.
4819 */
4820 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4821 if (error == 0) {
4822 ifnet_lock_done(ifp);
4823 goto cleanup;
4824 }
4825
4826 if (llifma != NULL) {
4827 VERIFY(!anon); /* must not get here if "anonymous" */
4828 if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
4829 FREE(llsa, M_IFADDR);
4830 llsa = NULL;
4831 ifma_free(llifma);
4832 llifma = NULL;
4833 VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
4834 } else {
4835 ll_firstref = 1;
4836 llifma->ifma_addr = llsa;
4837 llifma->ifma_ifp = ifp;
4838 IFMA_LOCK(llifma);
4839 if_attach_ifma(ifp, llifma, 0);
4840 /* add extra refcnt for ifma */
4841 IFMA_ADDREF_LOCKED(llifma);
4842 IFMA_UNLOCK(llifma);
4843 ifma->ifma_ll = llifma;
4844 }
4845 }
4846
4847 /* "anonymous" request should not result in network address */
4848 VERIFY(!anon || ifma->ifma_ll == NULL);
4849
4850 ifma->ifma_addr = dupsa;
4851 ifma->ifma_ifp = ifp;
4852 IFMA_LOCK(ifma);
4853 if_attach_ifma(ifp, ifma, anon);
4854 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4855 if (retifma != NULL) {
4856 *retifma = ifma;
4857 IFMA_ADDREF_LOCKED(*retifma); /* for caller */
4858 }
4859 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4860 ifma->ifma_addr->sa_family == AF_LINK);
4861 IFMA_UNLOCK(ifma);
4862 ifnet_lock_done(ifp);
4863
4864 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
4865 IFMA_REMREF(ifma); /* for this routine */
4866
4867 /*
4868 * We are certain we have added something, so call down to the
4869 * interface to let them know about it. Do this only for newly-
4870 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4871 */
4872 if (lladdr || ll_firstref) {
4873 (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
4874 }
4875
4876 if (ifp->if_updatemcasts > 0) {
4877 ifp->if_updatemcasts = 0;
4878 }
4879
4880 return 0;
4881
4882 cleanup:
4883 if (ifma != NULL) {
4884 ifma_free(ifma);
4885 }
4886 if (dupsa != NULL) {
4887 FREE(dupsa, M_IFADDR);
4888 }
4889 if (llifma != NULL) {
4890 ifma_free(llifma);
4891 }
4892 if (llsa != NULL) {
4893 FREE(llsa, M_IFADDR);
4894 }
4895
4896 return error;
4897 }
4898
4899 /*
4900 * Delete a multicast group membership by network-layer group address.
4901 * This routine is deprecated.
4902 */
4903 int
4904 if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
4905 {
4906 return if_delmulti_common(NULL, ifp, sa, 0);
4907 }
4908
4909 /*
4910 * Delete a multicast group membership by group membership pointer.
4911 * Network-layer protocol domains must use this routine.
4912 */
4913 int
4914 if_delmulti_ifma(struct ifmultiaddr *ifma)
4915 {
4916 return if_delmulti_common(ifma, NULL, NULL, 0);
4917 }
4918
4919 /*
4920 * Anything other than network-layer protocol domains which hold references
4921 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4922 * ioctl, ifnet_remove_multicast(), if_bond.
4923 */
4924 int
4925 if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
4926 {
4927 return if_delmulti_common(NULL, ifp, sa, 1);
4928 }
4929
4930 /*
4931 * Delete a multicast group membership by network-layer group address.
4932 *
4933 * Returns ENOENT if the entry could not be found.
4934 */
4935 static int
4936 if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
4937 const struct sockaddr *sa, int anon)
4938 {
4939 struct sockaddr *dupsa = NULL;
4940 int lastref, ll_lastref = 0, lladdr;
4941 struct ifmultiaddr *ll = NULL;
4942
4943 /* sanity check for callers */
4944 VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
4945
4946 if (ifma != NULL) {
4947 ifp = ifma->ifma_ifp;
4948 }
4949
4950 if (sa != NULL &&
4951 (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
4952 dupsa = copy_and_normalize(sa);
4953 if (dupsa == NULL) {
4954 return ENOMEM;
4955 }
4956 sa = dupsa;
4957 }
4958
4959 ifnet_lock_exclusive(ifp);
4960 if (ifma == NULL) {
4961 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4962 ifma = LIST_NEXT(ifma, ifma_link)) {
4963 IFMA_LOCK(ifma);
4964 if (!ifa_equal(sa, ifma->ifma_addr) ||
4965 (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
4966 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4967 ifma->ifma_anoncnt != 0);
4968 IFMA_UNLOCK(ifma);
4969 continue;
4970 }
4971 /* found; keep it locked */
4972 break;
4973 }
4974 if (ifma == NULL) {
4975 if (dupsa != NULL) {
4976 FREE(dupsa, M_IFADDR);
4977 }
4978 ifnet_lock_done(ifp);
4979 return ENOENT;
4980 }
4981 } else {
4982 IFMA_LOCK(ifma);
4983 }
4984 IFMA_LOCK_ASSERT_HELD(ifma);
4985 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4986 lastref = if_detach_ifma(ifp, ifma, anon);
4987 VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
4988 ifma->ifma_reqcnt == 0));
4989 VERIFY(!anon || ifma->ifma_ll == NULL);
4990 ll = ifma->ifma_ll;
4991 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4992 ifma->ifma_addr->sa_family == AF_LINK);
4993 IFMA_UNLOCK(ifma);
4994 if (lastref && ll != NULL) {
4995 IFMA_LOCK(ll);
4996 ll_lastref = if_detach_ifma(ifp, ll, 0);
4997 IFMA_UNLOCK(ll);
4998 }
4999 ifnet_lock_done(ifp);
5000
5001 if (lastref) {
5002 rt_newmaddrmsg(RTM_DELMADDR, ifma);
5003 }
5004
5005 if ((ll == NULL && lastref && lladdr) || ll_lastref) {
5006 /*
5007 * Make sure the interface driver is notified in the
5008 * case of a link layer mcast group being left. Do
5009 * this only for a AF_LINK/AF_UNSPEC address that has
5010 * been removed from the if_multiaddrs set.
5011 */
5012 ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
5013 }
5014
5015 if (lastref) {
5016 IFMA_REMREF(ifma); /* for if_multiaddrs list */
5017 }
5018 if (ll_lastref) {
5019 IFMA_REMREF(ll); /* for if_multiaddrs list */
5020 }
5021 IFMA_REMREF(ifma); /* for this routine */
5022 if (dupsa != NULL) {
5023 FREE(dupsa, M_IFADDR);
5024 }
5025
5026 return 0;
5027 }
5028
5029 /*
5030 * Shutdown all network activity. Used boot() when halting
5031 * system.
5032 */
5033 int
5034 if_down_all(void)
5035 {
5036 struct ifnet **ifp;
5037 u_int32_t count;
5038 u_int32_t i;
5039
5040 if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
5041 for (i = 0; i < count; i++) {
5042 if_down(ifp[i]);
5043 dlil_proto_unplumb_all(ifp[i]);
5044 }
5045 ifnet_list_free(ifp);
5046 }
5047
5048 return 0;
5049 }
5050
5051 /*
5052 * Delete Routes for a Network Interface
5053 *
5054 * Called for each routing entry via the rnh->rnh_walktree() call above
5055 * to delete all route entries referencing a detaching network interface.
5056 *
5057 * Arguments:
5058 * rn pointer to node in the routing table
5059 * arg argument passed to rnh->rnh_walktree() - detaching interface
5060 *
5061 * Returns:
5062 * 0 successful
5063 * errno failed - reason indicated
5064 *
5065 */
5066 static int
5067 if_rtdel(struct radix_node *rn, void *arg)
5068 {
5069 struct rtentry *rt = (struct rtentry *)rn;
5070 struct ifnet *ifp = arg;
5071 int err;
5072
5073 if (rt == NULL) {
5074 return 0;
5075 }
5076 /*
5077 * Checking against RTF_UP protects against walktree
5078 * recursion problems with cloned routes.
5079 */
5080 RT_LOCK(rt);
5081 if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
5082 /*
5083 * Safe to drop rt_lock and use rt_key, rt_gateway,
5084 * since holding rnh_lock here prevents another thread
5085 * from calling rt_setgate() on this route.
5086 */
5087 RT_UNLOCK(rt);
5088 err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
5089 rt_mask(rt), rt->rt_flags, NULL);
5090 if (err) {
5091 log(LOG_WARNING, "if_rtdel: error %d\n", err);
5092 }
5093 } else {
5094 RT_UNLOCK(rt);
5095 }
5096 return 0;
5097 }
5098
5099 /*
5100 * Removes routing table reference to a given interface
5101 * for a given protocol family
5102 */
5103 void
5104 if_rtproto_del(struct ifnet *ifp, int protocol)
5105 {
5106 struct radix_node_head *rnh;
5107
5108 if ((protocol <= AF_MAX) && (protocol >= 0) &&
5109 ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
5110 lck_mtx_lock(rnh_lock);
5111 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
5112 lck_mtx_unlock(rnh_lock);
5113 }
5114 }
5115
5116 static int
5117 if_rtmtu(struct radix_node *rn, void *arg)
5118 {
5119 struct rtentry *rt = (struct rtentry *)rn;
5120 struct ifnet *ifp = arg;
5121
5122 RT_LOCK(rt);
5123 if (rt->rt_ifp == ifp) {
5124 /*
5125 * Update the MTU of this entry only if the MTU
5126 * has not been locked (RTV_MTU is not set) and
5127 * if it was non-zero to begin with.
5128 */
5129 if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu) {
5130 rt->rt_rmx.rmx_mtu = ifp->if_mtu;
5131 if (rt_key(rt)->sa_family == AF_INET &&
5132 INTF_ADJUST_MTU_FOR_CLAT46(ifp)) {
5133 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(ifp);
5134 /* Further adjust the size for CLAT46 expansion */
5135 rt->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
5136 }
5137 }
5138 }
5139 RT_UNLOCK(rt);
5140
5141 return 0;
5142 }
5143
5144 /*
5145 * Update the MTU metric of all route entries in all protocol tables
5146 * associated with a particular interface; this is called when the
5147 * MTU of that interface has changed.
5148 */
5149 static void
5150 if_rtmtu_update(struct ifnet *ifp)
5151 {
5152 struct radix_node_head *rnh;
5153 int p;
5154
5155 for (p = 0; p < AF_MAX + 1; p++) {
5156 if ((rnh = rt_tables[p]) == NULL) {
5157 continue;
5158 }
5159
5160 lck_mtx_lock(rnh_lock);
5161 (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
5162 lck_mtx_unlock(rnh_lock);
5163 }
5164 routegenid_update();
5165 }
5166
5167 __private_extern__ void
5168 if_data_internal_to_if_data(struct ifnet *ifp,
5169 const struct if_data_internal *if_data_int, struct if_data *if_data)
5170 {
5171 #pragma unused(ifp)
5172 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
5173 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
5174 /* compiler will cast down to 32-bit */
5175 #define COPYFIELD32_ATOMIC(fld) do { \
5176 atomic_get_64(if_data->fld, \
5177 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5178 } while (0)
5179
5180 COPYFIELD(ifi_type);
5181 COPYFIELD(ifi_typelen);
5182 COPYFIELD(ifi_physical);
5183 COPYFIELD(ifi_addrlen);
5184 COPYFIELD(ifi_hdrlen);
5185 COPYFIELD(ifi_recvquota);
5186 COPYFIELD(ifi_xmitquota);
5187 if_data->ifi_unused1 = 0;
5188 COPYFIELD(ifi_mtu);
5189 COPYFIELD(ifi_metric);
5190 if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
5191 if_data->ifi_baudrate = 0xFFFFFFFF;
5192 } else {
5193 COPYFIELD32(ifi_baudrate);
5194 }
5195
5196 COPYFIELD32_ATOMIC(ifi_ipackets);
5197 COPYFIELD32_ATOMIC(ifi_ierrors);
5198 COPYFIELD32_ATOMIC(ifi_opackets);
5199 COPYFIELD32_ATOMIC(ifi_oerrors);
5200 COPYFIELD32_ATOMIC(ifi_collisions);
5201 COPYFIELD32_ATOMIC(ifi_ibytes);
5202 COPYFIELD32_ATOMIC(ifi_obytes);
5203 COPYFIELD32_ATOMIC(ifi_imcasts);
5204 COPYFIELD32_ATOMIC(ifi_omcasts);
5205 COPYFIELD32_ATOMIC(ifi_iqdrops);
5206 COPYFIELD32_ATOMIC(ifi_noproto);
5207
5208 COPYFIELD(ifi_recvtiming);
5209 COPYFIELD(ifi_xmittiming);
5210
5211 if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5212 if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
5213
5214 if_data->ifi_lastchange.tv_sec += boottime_sec();
5215
5216 if_data->ifi_unused2 = 0;
5217 COPYFIELD(ifi_hwassist);
5218 if_data->ifi_reserved1 = 0;
5219 if_data->ifi_reserved2 = 0;
5220 #undef COPYFIELD32_ATOMIC
5221 #undef COPYFIELD32
5222 #undef COPYFIELD
5223 }
5224
5225 __private_extern__ void
5226 if_data_internal_to_if_data64(struct ifnet *ifp,
5227 const struct if_data_internal *if_data_int,
5228 struct if_data64 *if_data64)
5229 {
5230 #pragma unused(ifp)
5231 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
5232 #define COPYFIELD64_ATOMIC(fld) do { \
5233 atomic_get_64(if_data64->fld, \
5234 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5235 } while (0)
5236
5237 COPYFIELD64(ifi_type);
5238 COPYFIELD64(ifi_typelen);
5239 COPYFIELD64(ifi_physical);
5240 COPYFIELD64(ifi_addrlen);
5241 COPYFIELD64(ifi_hdrlen);
5242 COPYFIELD64(ifi_recvquota);
5243 COPYFIELD64(ifi_xmitquota);
5244 if_data64->ifi_unused1 = 0;
5245 COPYFIELD64(ifi_mtu);
5246 COPYFIELD64(ifi_metric);
5247 COPYFIELD64(ifi_baudrate);
5248
5249 COPYFIELD64_ATOMIC(ifi_ipackets);
5250 COPYFIELD64_ATOMIC(ifi_ierrors);
5251 COPYFIELD64_ATOMIC(ifi_opackets);
5252 COPYFIELD64_ATOMIC(ifi_oerrors);
5253 COPYFIELD64_ATOMIC(ifi_collisions);
5254 COPYFIELD64_ATOMIC(ifi_ibytes);
5255 COPYFIELD64_ATOMIC(ifi_obytes);
5256 COPYFIELD64_ATOMIC(ifi_imcasts);
5257 COPYFIELD64_ATOMIC(ifi_omcasts);
5258 COPYFIELD64_ATOMIC(ifi_iqdrops);
5259 COPYFIELD64_ATOMIC(ifi_noproto);
5260
5261 /*
5262 * Note these two fields are actually 32 bit, so doing
5263 * COPYFIELD64_ATOMIC will cause them to be misaligned
5264 */
5265 COPYFIELD64(ifi_recvtiming);
5266 COPYFIELD64(ifi_xmittiming);
5267
5268 if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5269 if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
5270
5271 if_data64->ifi_lastchange.tv_sec += boottime_sec();
5272
5273 #undef COPYFIELD64
5274 }
5275
5276 __private_extern__ void
5277 if_copy_traffic_class(struct ifnet *ifp,
5278 struct if_traffic_class *if_tc)
5279 {
5280 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
5281 atomic_get_64(if_tc->fld, \
5282 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
5283 } while (0)
5284
5285 bzero(if_tc, sizeof(*if_tc));
5286 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
5287 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
5288 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
5289 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
5290 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
5291 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
5292 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
5293 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
5294 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
5295 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
5296 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
5297 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
5298 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
5299 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
5300 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
5301 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
5302 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
5303 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
5304 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
5305 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
5306
5307 #undef COPY_IF_TC_FIELD64_ATOMIC
5308 }
5309
5310 void
5311 if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
5312 {
5313 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
5314 atomic_get_64(if_de->fld, \
5315 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
5316 } while (0)
5317
5318 bzero(if_de, sizeof(*if_de));
5319 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
5320 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
5321 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
5322 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
5323
5324 #undef COPY_IF_DE_FIELD64_ATOMIC
5325 }
5326
5327 void
5328 if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
5329 {
5330 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
5331 atomic_get_64(if_ps->ifi_tcp_##fld, \
5332 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
5333 } while (0)
5334
5335 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
5336 atomic_get_64(if_ps->ifi_udp_##fld, \
5337 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
5338 } while (0)
5339
5340 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
5341 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
5342 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
5343 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
5344 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
5345 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
5346 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
5347 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
5348 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
5349 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
5350 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
5351 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
5352 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
5353 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
5354
5355 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
5356 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
5357 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
5358 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
5359 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
5360 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
5361 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
5362 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
5363
5364 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5365 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5366 }
5367
5368 void
5369 if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
5370 {
5371 bzero(if_rs, sizeof(*if_rs));
5372 if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1)) {
5373 return;
5374 }
5375 bcopy(&ifp->if_poll_pstats, if_rs, sizeof(*if_rs));
5376 /* Release the IO refcnt */
5377 ifnet_decr_iorefcnt(ifp);
5378 }
5379
5380 void
5381 if_copy_netif_stats(struct ifnet *ifp, struct if_netif_stats *if_ns)
5382 {
5383 bzero(if_ns, sizeof(*if_ns));
5384 #pragma unused(ifp)
5385 }
5386
5387 struct ifaddr *
5388 ifa_remref(struct ifaddr *ifa, int locked)
5389 {
5390 if (!locked) {
5391 IFA_LOCK_SPIN(ifa);
5392 } else {
5393 IFA_LOCK_ASSERT_HELD(ifa);
5394 }
5395
5396 if (ifa->ifa_refcnt == 0) {
5397 panic("%s: ifa %p negative refcnt\n", __func__, ifa);
5398 } else if (ifa->ifa_trace != NULL) {
5399 (*ifa->ifa_trace)(ifa, FALSE);
5400 }
5401 if (--ifa->ifa_refcnt == 0) {
5402 if (ifa->ifa_debug & IFD_ATTACHED) {
5403 panic("ifa %p attached to ifp is being freed\n", ifa);
5404 }
5405 /*
5406 * Some interface addresses are allocated either statically
5407 * or carved out of a larger block. Only free it if it was
5408 * allocated via MALLOC or via the corresponding per-address
5409 * family allocator. Otherwise, leave it alone.
5410 */
5411 if (ifa->ifa_debug & IFD_ALLOC) {
5412 if (ifa->ifa_free == NULL) {
5413 IFA_UNLOCK(ifa);
5414 FREE(ifa, M_IFADDR);
5415 } else {
5416 /* Become a regular mutex */
5417 IFA_CONVERT_LOCK(ifa);
5418 /* callee will unlock */
5419 (*ifa->ifa_free)(ifa);
5420 }
5421 } else {
5422 IFA_UNLOCK(ifa);
5423 }
5424 ifa = NULL;
5425 }
5426
5427 if (!locked && ifa != NULL) {
5428 IFA_UNLOCK(ifa);
5429 }
5430
5431 return ifa;
5432 }
5433
5434 void
5435 ifa_addref(struct ifaddr *ifa, int locked)
5436 {
5437 if (!locked) {
5438 IFA_LOCK_SPIN(ifa);
5439 } else {
5440 IFA_LOCK_ASSERT_HELD(ifa);
5441 }
5442
5443 if (++ifa->ifa_refcnt == 0) {
5444 panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
5445 /* NOTREACHED */
5446 } else if (ifa->ifa_trace != NULL) {
5447 (*ifa->ifa_trace)(ifa, TRUE);
5448 }
5449 if (!locked) {
5450 IFA_UNLOCK(ifa);
5451 }
5452 }
5453
5454 void
5455 ifa_lock_init(struct ifaddr *ifa)
5456 {
5457 lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
5458 }
5459
5460 void
5461 ifa_lock_destroy(struct ifaddr *ifa)
5462 {
5463 IFA_LOCK_ASSERT_NOTHELD(ifa);
5464 lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
5465 }
5466
5467 /*
5468 * 'i' group ioctls.
5469 *
5470 * The switch statement below does nothing at runtime, as it serves as a
5471 * compile time check to ensure that all of the socket 'i' ioctls (those
5472 * in the 'i' group going thru soo_ioctl) that are made available by the
5473 * networking stack is unique. This works as long as this routine gets
5474 * updated each time a new interface ioctl gets added.
5475 *
5476 * Any failures at compile time indicates duplicated ioctl values.
5477 */
5478 static __attribute__((unused)) void
5479 ifioctl_cassert(void)
5480 {
5481 /*
5482 * This is equivalent to _CASSERT() and the compiler wouldn't
5483 * generate any instructions, thus for compile time only.
5484 */
5485 switch ((u_long)0) {
5486 case 0:
5487
5488 /* bsd/net/if_ppp.h */
5489 case SIOCGPPPSTATS:
5490 case SIOCGPPPCSTATS:
5491
5492 /* bsd/netinet6/in6_var.h */
5493 case SIOCSIFADDR_IN6:
5494 case SIOCGIFADDR_IN6:
5495 case SIOCSIFDSTADDR_IN6:
5496 case SIOCSIFNETMASK_IN6:
5497 case SIOCGIFDSTADDR_IN6:
5498 case SIOCGIFNETMASK_IN6:
5499 case SIOCDIFADDR_IN6:
5500 case SIOCAIFADDR_IN6_32:
5501 case SIOCAIFADDR_IN6_64:
5502 case SIOCSIFPHYADDR_IN6_32:
5503 case SIOCSIFPHYADDR_IN6_64:
5504 case SIOCGIFPSRCADDR_IN6:
5505 case SIOCGIFPDSTADDR_IN6:
5506 case SIOCGIFAFLAG_IN6:
5507 case SIOCGDRLST_IN6_32:
5508 case SIOCGDRLST_IN6_64:
5509 case SIOCGPRLST_IN6_32:
5510 case SIOCGPRLST_IN6_64:
5511 case OSIOCGIFINFO_IN6:
5512 case SIOCGIFINFO_IN6:
5513 case SIOCSNDFLUSH_IN6:
5514 case SIOCGNBRINFO_IN6_32:
5515 case SIOCGNBRINFO_IN6_64:
5516 case SIOCSPFXFLUSH_IN6:
5517 case SIOCSRTRFLUSH_IN6:
5518 case SIOCGIFALIFETIME_IN6:
5519 case SIOCSIFALIFETIME_IN6:
5520 case SIOCGIFSTAT_IN6:
5521 case SIOCGIFSTAT_ICMP6:
5522 case SIOCSDEFIFACE_IN6_32:
5523 case SIOCSDEFIFACE_IN6_64:
5524 case SIOCGDEFIFACE_IN6_32:
5525 case SIOCGDEFIFACE_IN6_64:
5526 case SIOCSIFINFO_FLAGS:
5527 case SIOCSSCOPE6:
5528 case SIOCGSCOPE6:
5529 case SIOCGSCOPE6DEF:
5530 case SIOCSIFPREFIX_IN6:
5531 case SIOCGIFPREFIX_IN6:
5532 case SIOCDIFPREFIX_IN6:
5533 case SIOCAIFPREFIX_IN6:
5534 case SIOCCIFPREFIX_IN6:
5535 case SIOCSGIFPREFIX_IN6:
5536 case SIOCPROTOATTACH_IN6_32:
5537 case SIOCPROTOATTACH_IN6_64:
5538 case SIOCPROTODETACH_IN6:
5539 case SIOCLL_START_32:
5540 case SIOCLL_START_64:
5541 case SIOCLL_STOP:
5542 case SIOCAUTOCONF_START:
5543 case SIOCAUTOCONF_STOP:
5544 case SIOCSETROUTERMODE_IN6:
5545 case SIOCGETROUTERMODE_IN6:
5546 case SIOCLL_CGASTART_32:
5547 case SIOCLL_CGASTART_64:
5548 case SIOCGIFCGAPREP_IN6:
5549 case SIOCSIFCGAPREP_IN6:
5550
5551 /* bsd/sys/sockio.h */
5552 case SIOCSIFADDR:
5553 case OSIOCGIFADDR:
5554 case SIOCSIFDSTADDR:
5555 case OSIOCGIFDSTADDR:
5556 case SIOCSIFFLAGS:
5557 case SIOCGIFFLAGS:
5558 case OSIOCGIFBRDADDR:
5559 case SIOCSIFBRDADDR:
5560 case OSIOCGIFCONF32:
5561 case OSIOCGIFCONF64:
5562 case OSIOCGIFNETMASK:
5563 case SIOCSIFNETMASK:
5564 case SIOCGIFMETRIC:
5565 case SIOCSIFMETRIC:
5566 case SIOCDIFADDR:
5567 case SIOCAIFADDR:
5568
5569 case SIOCGIFADDR:
5570 case SIOCGIFDSTADDR:
5571 case SIOCGIFBRDADDR:
5572 case SIOCGIFCONF32:
5573 case SIOCGIFCONF64:
5574 case SIOCGIFNETMASK:
5575 case SIOCAUTOADDR:
5576 case SIOCAUTONETMASK:
5577 case SIOCARPIPLL:
5578
5579 case SIOCADDMULTI:
5580 case SIOCDELMULTI:
5581 case SIOCGIFMTU:
5582 case SIOCSIFMTU:
5583 case SIOCGIFPHYS:
5584 case SIOCSIFPHYS:
5585 case SIOCSIFMEDIA:
5586 case SIOCGIFMEDIA32:
5587 case SIOCGIFMEDIA64:
5588 case SIOCGIFXMEDIA32:
5589 case SIOCGIFXMEDIA64:
5590 case SIOCSIFGENERIC:
5591 case SIOCGIFGENERIC:
5592 case SIOCRSLVMULTI:
5593
5594 case SIOCSIFLLADDR:
5595 case SIOCGIFSTATUS:
5596 case SIOCSIFPHYADDR:
5597 case SIOCGIFPSRCADDR:
5598 case SIOCGIFPDSTADDR:
5599 case SIOCDIFPHYADDR:
5600
5601 case SIOCGIFDEVMTU:
5602 case SIOCSIFALTMTU:
5603 case SIOCGIFALTMTU:
5604 case SIOCSIFBOND:
5605 case SIOCGIFBOND:
5606
5607 case SIOCPROTOATTACH:
5608 case SIOCPROTODETACH:
5609
5610 case SIOCSIFCAP:
5611 case SIOCGIFCAP:
5612
5613 case SIOCIFCREATE:
5614 case SIOCIFDESTROY:
5615 case SIOCIFCREATE2:
5616
5617 case SIOCSDRVSPEC32:
5618 case SIOCGDRVSPEC32:
5619 case SIOCSDRVSPEC64:
5620 case SIOCGDRVSPEC64:
5621
5622 case SIOCSIFVLAN:
5623 case SIOCGIFVLAN:
5624
5625 case SIOCIFGCLONERS32:
5626 case SIOCIFGCLONERS64:
5627
5628 case SIOCGIFASYNCMAP:
5629 case SIOCSIFASYNCMAP:
5630 case SIOCSIFKPI:
5631 case SIOCGIFKPI:
5632
5633 case SIOCGIFWAKEFLAGS:
5634
5635 case SIOCGIFGETRTREFCNT:
5636 case SIOCGIFLINKQUALITYMETRIC:
5637 case SIOCSIFOPPORTUNISTIC:
5638 case SIOCGIFOPPORTUNISTIC:
5639 case SIOCGETROUTERMODE:
5640 case SIOCSETROUTERMODE:
5641 case SIOCGIFEFLAGS:
5642 case SIOCSIFDESC:
5643 case SIOCGIFDESC:
5644 case SIOCSIFLINKPARAMS:
5645 case SIOCGIFLINKPARAMS:
5646 case SIOCGIFQUEUESTATS:
5647 case SIOCSIFTHROTTLE:
5648 case SIOCGIFTHROTTLE:
5649
5650 case SIOCGASSOCIDS32:
5651 case SIOCGASSOCIDS64:
5652 case SIOCGCONNIDS32:
5653 case SIOCGCONNIDS64:
5654 case SIOCGCONNINFO32:
5655 case SIOCGCONNINFO64:
5656 case SIOCSCONNORDER:
5657 case SIOCGCONNORDER:
5658
5659 case SIOCSIFLOG:
5660 case SIOCGIFLOG:
5661 case SIOCGIFDELEGATE:
5662 case SIOCGIFLLADDR:
5663 case SIOCGIFTYPE:
5664 case SIOCGIFEXPENSIVE:
5665 case SIOCSIFEXPENSIVE:
5666 case SIOCGIF2KCL:
5667 case SIOCSIF2KCL:
5668 case SIOCGSTARTDELAY:
5669
5670 case SIOCAIFAGENTID:
5671 case SIOCDIFAGENTID:
5672 case SIOCGIFAGENTIDS32:
5673 case SIOCGIFAGENTIDS64:
5674 case SIOCGIFAGENTDATA32:
5675 case SIOCGIFAGENTDATA64:
5676
5677 case SIOCSIFINTERFACESTATE:
5678 case SIOCGIFINTERFACESTATE:
5679 case SIOCSIFPROBECONNECTIVITY:
5680 case SIOCGIFPROBECONNECTIVITY:
5681
5682 case SIOCGIFFUNCTIONALTYPE:
5683 case SIOCSIFNETSIGNATURE:
5684 case SIOCGIFNETSIGNATURE:
5685
5686 case SIOCSIFNETWORKID:
5687 case SIOCGECNMODE:
5688 case SIOCSECNMODE:
5689
5690 case SIOCSIFORDER:
5691
5692 case SIOCSQOSMARKINGMODE:
5693 case SIOCSQOSMARKINGENABLED:
5694 case SIOCGQOSMARKINGMODE:
5695 case SIOCGQOSMARKINGENABLED:
5696
5697 case SIOCSIFTIMESTAMPENABLE:
5698 case SIOCSIFTIMESTAMPDISABLE:
5699 case SIOCGIFTIMESTAMPENABLED:
5700
5701 case SIOCSIFDISABLEOUTPUT:
5702
5703 case SIOCSIFSUBFAMILY:
5704
5705 case SIOCGIFAGENTLIST32:
5706 case SIOCGIFAGENTLIST64:
5707
5708 case SIOCSIFLOWINTERNET:
5709 case SIOCGIFLOWINTERNET:
5710
5711 case SIOCGIFNAT64PREFIX:
5712 case SIOCSIFNAT64PREFIX:
5713
5714 case SIOCGIFCLAT46ADDR:
5715
5716 case SIOCGIFPROTOLIST32:
5717 case SIOCGIFPROTOLIST64:
5718
5719 case SIOCSIF6LOWPAN:
5720 case SIOCGIF6LOWPAN:
5721
5722 case SIOCGIFLOWPOWER:
5723 case SIOCSIFLOWPOWER:
5724
5725 case SIOCGIFMPKLOG:
5726 case SIOCSIFMPKLOG:
5727
5728 case SIOCGIFCONSTRAINED:
5729 case SIOCSIFCONSTRAINED:
5730
5731 case SIOCGIFXFLAGS:
5732
5733 case SIOCGIFNOACKPRIO:
5734 case SIOCSIFNOACKPRIO:
5735 ;
5736 }
5737 }
5738
5739 uint32_t
5740 ifnet_mbuf_packetpreamblelen(struct ifnet *ifp)
5741 {
5742 #pragma unused(ifp)
5743 return 0;
5744 }
5745
5746 /* The following is used to enqueue work items for interface events */
5747 struct intf_event {
5748 struct ifnet *ifp;
5749 union sockaddr_in_4_6 addr;
5750 uint32_t intf_event_code;
5751 };
5752
5753 static void
5754 intf_event_callback(void *arg)
5755 {
5756 struct intf_event *p_intf_ev = (struct intf_event *)arg;
5757
5758 /* Call this before we walk the tree */
5759 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt, ifnet_event, p_intf_ev->ifp,
5760 (struct sockaddr *)&(p_intf_ev->addr), p_intf_ev->intf_event_code);
5761 }
5762
5763 struct intf_event_nwk_wq_entry {
5764 struct nwk_wq_entry nwk_wqe;
5765 struct intf_event intf_ev_arg;
5766 };
5767
5768 void
5769 intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp, struct sockaddr *addrp,
5770 uint32_t intf_event_code)
5771 {
5772 #pragma unused(addrp)
5773 struct intf_event_nwk_wq_entry *p_intf_ev = NULL;
5774
5775 MALLOC(p_intf_ev, struct intf_event_nwk_wq_entry *,
5776 sizeof(struct intf_event_nwk_wq_entry),
5777 M_NWKWQ, M_WAITOK | M_ZERO);
5778
5779 p_intf_ev->intf_ev_arg.ifp = ifp;
5780 /*
5781 * XXX Not using addr in the arg. This will be used
5782 * once we need IP address add/delete events
5783 */
5784 p_intf_ev->intf_ev_arg.intf_event_code = intf_event_code;
5785 p_intf_ev->nwk_wqe.func = intf_event_callback;
5786 p_intf_ev->nwk_wqe.is_arg_managed = TRUE;
5787 p_intf_ev->nwk_wqe.arg = &p_intf_ev->intf_ev_arg;
5788 nwk_wq_enqueue((struct nwk_wq_entry*)p_intf_ev);
5789 }
5790
5791 int
5792 if_get_tcp_kao_max(struct ifnet *ifp)
5793 {
5794 int error = 0;
5795
5796 if (ifp->if_tcp_kao_max == 0) {
5797 struct ifreq ifr;
5798
5799 memset(&ifr, 0, sizeof(struct ifreq));
5800 error = ifnet_ioctl(ifp, 0, SIOCGIFTCPKAOMAX, &ifr);
5801
5802 ifnet_lock_exclusive(ifp);
5803 if (error == 0) {
5804 ifp->if_tcp_kao_max = ifr.ifr_tcp_kao_max;
5805 } else if (error == EOPNOTSUPP) {
5806 ifp->if_tcp_kao_max = default_tcp_kao_max;
5807 }
5808 ifnet_lock_done(ifp);
5809 }
5810 return error;
5811 }