2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
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 $
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,
70 #include <kern/locks.h>
72 #include <sys/param.h>
73 #include <sys/malloc.h>
75 #include <sys/systm.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>
87 #include <kern/zalloc.h>
88 #include <mach/boolean.h>
90 #include <machine/endian.h>
92 #include <pexpert/pexpert.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>
109 #include <sys/domain.h>
110 #include <libkern/OSAtomic.h>
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>
125 #include <netinet6/in6_var.h>
126 #include <netinet6/in6_ifattach.h>
127 #include <netinet6/ip6_var.h>
128 #include <netinet6/nd6.h>
130 #endif /* INET || INET6 */
133 #include <security/mac_framework.h>
140 * System initialization
143 extern char *proc_name_address(void *);
145 /* Lock group and attribute for ifaddr lock */
146 lck_attr_t
*ifa_mtx_attr
;
147 lck_grp_t
*ifa_mtx_grp
;
148 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
150 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
152 static int ifioctl_ifconf(u_long
, caddr_t
);
153 static int ifioctl_ifclone(u_long
, caddr_t
);
154 static int ifioctl_iforder(u_long
, caddr_t
);
155 static int ifioctl_ifdesc(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
156 static int ifioctl_linkparams(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
157 static int ifioctl_qstats(struct ifnet
*, u_long
, caddr_t
);
158 static int ifioctl_throttle(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
159 static int ifioctl_netsignature(struct ifnet
*, u_long
, caddr_t
);
160 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
161 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
162 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
164 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
165 struct ifmultiaddr
**, int);
166 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
167 const struct sockaddr
*, int);
168 static struct ifnet
*ifunit_common(const char *, boolean_t
);
170 static int if_rtmtu(struct radix_node
*, void *);
171 static void if_rtmtu_update(struct ifnet
*);
173 static int if_clone_list(int, int *, user_addr_t
);
175 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
177 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
179 /* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
180 struct ifnethead ifnet_ordered_head
= TAILQ_HEAD_INITIALIZER(ifnet_ordered_head
);
181 static u_int32_t if_ordered_count
= 0;
183 static int if_cloners_count
;
184 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
186 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
188 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
189 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
191 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
192 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
194 static struct ifmultiaddr
*ifma_alloc(int);
195 static void ifma_free(struct ifmultiaddr
*);
196 static void ifma_trace(struct ifmultiaddr
*, int);
199 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
201 static unsigned int ifma_debug
; /* debugging (disabled) */
203 static unsigned int ifma_size
; /* size of zone element */
204 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
206 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
209 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
211 struct ifmultiaddr_dbg
{
212 struct ifmultiaddr ifma
; /* ifmultiaddr */
213 u_int16_t ifma_refhold_cnt
; /* # of ref */
214 u_int16_t ifma_refrele_cnt
; /* # of rele */
216 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
218 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
219 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
223 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
226 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
227 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
228 static decl_lck_mtx_data(, ifma_trash_lock
);
230 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
231 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
235 * XXX: declare here to avoid to include many inet6 related files..
236 * should be more generalized?
238 extern void nd6_setmtu(struct ifnet
*);
239 extern lck_mtx_t
*nd6_mutex
;
242 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Link layers");
243 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
244 "Generic link-management");
246 SYSCTL_DECL(_net_link_generic_system
);
248 static uint32_t if_verbose
= 0;
249 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, if_verbose
,
250 CTLFLAG_RW
| CTLFLAG_LOCKED
, &if_verbose
, 0, "");
252 #if (DEBUG || DEVELOPMENT)
253 static uint32_t default_tcp_kao_max
= 0;
254 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, default_tcp_kao_max
,
255 CTLFLAG_RW
| CTLFLAG_LOCKED
, &default_tcp_kao_max
, 0, "");
257 static const uint32_t default_tcp_kao_max
= 0;
258 #endif /* (DEBUG || DEVELOPMENT) */
260 boolean_t intcoproc_unrestricted
;
262 /* Eventhandler context for interface events */
263 struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt
;
268 /* Setup lock group and attribute for ifaddr */
269 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
270 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
271 ifa_mtx_attr
= lck_attr_alloc_init();
273 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof(ifma_debug
));
275 ifma_size
= (ifma_debug
== 0) ? sizeof(struct ifmultiaddr
) :
276 sizeof(struct ifmultiaddr_dbg
);
278 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
280 if (ifma_zone
== NULL
) {
281 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
284 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
285 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
287 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
288 TAILQ_INIT(&ifma_trash_head
);
290 PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted
,
291 sizeof(intcoproc_unrestricted
));
295 * Network interface utility routines.
297 * Routines with ifa_ifwith* names take sockaddr *'s as
302 struct ifaddr
**ifnet_addrs
;
303 struct ifnet
**ifindex2ifnet
;
305 __private_extern__
void
306 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
308 if_attach_ifa_common(ifp
, ifa
, 0);
311 __private_extern__
void
312 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
314 if_attach_ifa_common(ifp
, ifa
, 1);
318 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
320 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
321 IFA_LOCK_ASSERT_HELD(ifa
);
323 if (ifa
->ifa_ifp
!= ifp
) {
324 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
327 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
328 panic("%s: Attempt to attach an already attached ifa=%p",
331 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
332 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
334 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
335 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
338 IFA_ADDREF_LOCKED(ifa
);
339 ifa
->ifa_debug
|= IFD_ATTACHED
;
341 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
343 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
346 if (ifa
->ifa_attached
!= NULL
) {
347 (*ifa
->ifa_attached
)(ifa
);
352 __private_extern__
void
353 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
355 if_detach_ifa_common(ifp
, ifa
, 0);
358 __private_extern__
void
359 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
361 if_detach_ifa_common(ifp
, ifa
, 1);
365 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
367 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
368 IFA_LOCK_ASSERT_HELD(ifa
);
370 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
371 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
373 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
374 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
376 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
377 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
379 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
380 panic("%s: Attempt to detach an unattached address ifa=%p",
383 } else if (ifa
->ifa_ifp
!= ifp
) {
384 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
387 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
389 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
395 panic("%s: Attempt to detach a stray address ifa=%p",
400 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
401 /* This must not be the last reference to the ifaddr */
402 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
403 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
406 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
408 if (ifa
->ifa_detached
!= NULL
) {
409 (*ifa
->ifa_detached
)(ifa
);
414 #define INITIAL_IF_INDEXLIM 8
417 * Function: if_next_index
419 * Return the next available interface index.
420 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
421 * added entry when necessary.
424 * ifnet_addrs[] is indexed by (if_index - 1), whereas
425 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
426 * always allocate one extra element to hold ifindex2ifnet[0], which
429 int if_next_index(void);
431 __private_extern__
int
434 static int if_indexlim
= 0;
437 new_index
= ++if_index
;
438 if (if_index
> if_indexlim
) {
441 caddr_t new_ifnet_addrs
;
442 caddr_t new_ifindex2ifnet
;
443 caddr_t old_ifnet_addrs
;
445 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
446 if (ifnet_addrs
== NULL
) {
447 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
449 new_if_indexlim
= if_indexlim
<< 1;
452 /* allocate space for the larger arrays */
453 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
454 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
455 if (new_ifnet_addrs
== NULL
) {
460 new_ifindex2ifnet
= new_ifnet_addrs
461 + new_if_indexlim
* sizeof(caddr_t
);
462 if (ifnet_addrs
!= NULL
) {
463 /* copy the existing data */
464 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
465 if_indexlim
* sizeof(caddr_t
));
466 bcopy((caddr_t
)ifindex2ifnet
,
468 (if_indexlim
+ 1) * sizeof(caddr_t
));
471 /* switch to the new tables and size */
472 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
473 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
474 if_indexlim
= new_if_indexlim
;
476 /* release the old data */
477 if (old_ifnet_addrs
!= NULL
) {
478 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
485 * Create a clone network interface.
488 if_clone_create(char *name
, int len
, void *params
)
490 struct if_clone
*ifc
;
493 u_int32_t bytoff
, bitoff
;
497 ifc
= if_clone_lookup(name
, &unit
);
502 if (ifunit(name
) != NULL
) {
507 wildcard
= (unit
== UINT32_MAX
);
509 * Find a free unit if none was given.
512 while ((bytoff
< ifc
->ifc_bmlen
) &&
513 (ifc
->ifc_units
[bytoff
] == 0xff)) {
516 if (bytoff
>= ifc
->ifc_bmlen
) {
519 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0) {
522 unit
= (bytoff
<< 3) + bitoff
;
525 if (unit
> ifc
->ifc_maxunit
) {
529 lck_mtx_lock(&ifc
->ifc_mutex
);
530 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
532 lck_mtx_unlock(&ifc
->ifc_mutex
);
538 bitoff
= unit
- (bytoff
<< 3);
542 * Allocate the unit in the bitmap.
544 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
545 ("%s: bit is already set", __func__
));
546 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
548 /* In the wildcard case, we need to update the name. */
550 for (dp
= name
; *dp
!= '\0'; dp
++) {
553 if (snprintf(dp
, len
- (dp
- name
), "%d", unit
) >
554 len
- (dp
- name
) - 1) {
556 * This can only be a programmer error and
557 * there's no straightforward way to recover if
560 panic("%s: interface name too long", __func__
);
564 lck_mtx_unlock(&ifc
->ifc_mutex
);
570 * Destroy a clone network interface.
573 if_clone_destroy(const char *name
)
575 struct if_clone
*ifc
= NULL
;
576 struct ifnet
*ifp
= NULL
;
581 ifc
= if_clone_lookup(name
, &unit
);
588 if (unit
< ifc
->ifc_minifs
) {
593 ifp
= ifunit_ref(name
);
599 if (ifc
->ifc_destroy
== NULL
) {
604 lck_mtx_lock(&ifc
->ifc_mutex
);
605 error
= (*ifc
->ifc_destroy
)(ifp
);
608 lck_mtx_unlock(&ifc
->ifc_mutex
);
612 /* Compute offset in the bitmap and deallocate the unit. */
614 bitoff
= unit
- (bytoff
<< 3);
615 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
616 ("%s: bit is already cleared", __func__
));
617 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
618 lck_mtx_unlock(&ifc
->ifc_mutex
);
622 ifnet_decr_iorefcnt(ifp
);
628 * Look up a network interface cloner.
631 __private_extern__
struct if_clone
*
632 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
634 struct if_clone
*ifc
;
638 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
639 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
640 if (ifc
->ifc_name
[i
] != *cp
) {
646 ifc
= LIST_NEXT(ifc
, ifc_list
);
650 return (struct if_clone
*)NULL
;
656 for (i
= 0; *cp
!= '\0'; cp
++) {
657 if (*cp
< '0' || *cp
> '9') {
658 /* Bogus unit number. */
661 i
= (i
* 10) + (*cp
- '0');
672 if_clone_softc_allocate(const struct if_clone
*ifc
)
674 void *p_clone
= NULL
;
678 p_clone
= zalloc(ifc
->ifc_zone
);
679 if (p_clone
!= NULL
) {
680 bzero(p_clone
, ifc
->ifc_softc_size
);
687 if_clone_softc_deallocate(const struct if_clone
*ifc
, void *p_softc
)
689 VERIFY(ifc
!= NULL
&& p_softc
!= NULL
);
690 bzero(p_softc
, ifc
->ifc_softc_size
);
691 zfree(ifc
->ifc_zone
, p_softc
);
695 * Register a network interface cloner.
698 if_clone_attach(struct if_clone
*ifc
)
705 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
706 ("%s: %s requested more units then allowed (%d > %d)",
707 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
708 ifc
->ifc_maxunit
+ 1));
710 * Compute bitmap size and allocate it.
712 maxclone
= ifc
->ifc_maxunit
+ 1;
714 if ((len
<< 3) < maxclone
) {
717 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
718 if (ifc
->ifc_units
== NULL
) {
721 ifc
->ifc_bmlen
= len
;
722 lck_mtx_init(&ifc
->ifc_mutex
, ifnet_lock_group
, ifnet_lock_attr
);
724 if (ifc
->ifc_softc_size
!= 0) {
725 ifc
->ifc_zone
= zinit(ifc
->ifc_softc_size
,
726 ifc
->ifc_zone_max_elem
* ifc
->ifc_softc_size
, 0, ifc
->ifc_name
);
727 if (ifc
->ifc_zone
== NULL
) {
728 FREE(ifc
->ifc_units
, M_CLONE
);
731 zone_change(ifc
->ifc_zone
, Z_EXPAND
, TRUE
);
732 zone_change(ifc
->ifc_zone
, Z_CALLERACCT
, FALSE
);
735 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
738 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
739 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
741 ("%s: failed to create required interface %s%d",
742 __func__
, ifc
->ifc_name
, unit
));
744 /* Allocate the unit in the bitmap. */
746 bitoff
= unit
- (bytoff
<< 3);
747 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
754 * Unregister a network interface cloner.
757 if_clone_detach(struct if_clone
*ifc
)
759 LIST_REMOVE(ifc
, ifc_list
);
760 FREE(ifc
->ifc_units
, M_CLONE
);
761 if (ifc
->ifc_softc_size
!= 0) {
762 zdestroy(ifc
->ifc_zone
);
765 lck_mtx_destroy(&ifc
->ifc_mutex
, ifnet_lock_group
);
770 * Provide list of interface cloners to userspace.
773 if_clone_list(int count
, int *ret_total
, user_addr_t dst
)
775 char outbuf
[IFNAMSIZ
];
776 struct if_clone
*ifc
;
779 *ret_total
= if_cloners_count
;
780 if (dst
== USER_ADDR_NULL
) {
781 /* Just asking how many there are. */
789 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
791 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
792 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
793 bzero(outbuf
, sizeof(outbuf
));
794 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
795 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
805 if_functional_type(struct ifnet
*ifp
, bool exclude_delegate
)
807 u_int32_t ret
= IFRTYPE_FUNCTIONAL_UNKNOWN
;
810 if (ifp
->if_flags
& IFF_LOOPBACK
) {
811 ret
= IFRTYPE_FUNCTIONAL_LOOPBACK
;
812 } else if (IFNET_IS_COMPANION_LINK(ifp
)) {
813 ret
= IFRTYPE_FUNCTIONAL_COMPANIONLINK
;
814 } else if ((exclude_delegate
&&
815 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
&&
816 ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
)) ||
817 (!exclude_delegate
&& IFNET_IS_WIFI(ifp
))) {
818 if (ifp
->if_eflags
& IFEF_AWDL
) {
819 ret
= IFRTYPE_FUNCTIONAL_WIFI_AWDL
;
821 ret
= IFRTYPE_FUNCTIONAL_WIFI_INFRA
;
823 } else if ((exclude_delegate
&&
824 (ifp
->if_type
== IFT_CELLULAR
)) ||
825 (!exclude_delegate
&& IFNET_IS_CELLULAR(ifp
))) {
826 ret
= IFRTYPE_FUNCTIONAL_CELLULAR
;
827 } else if (IFNET_IS_INTCOPROC(ifp
)) {
828 ret
= IFRTYPE_FUNCTIONAL_INTCOPROC
;
829 } else if ((exclude_delegate
&&
830 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
||
831 ifp
->if_family
== IFNET_FAMILY_BOND
||
832 ifp
->if_family
== IFNET_FAMILY_VLAN
||
833 ifp
->if_family
== IFNET_FAMILY_FIREWIRE
)) ||
834 (!exclude_delegate
&& IFNET_IS_WIRED(ifp
))) {
835 ret
= IFRTYPE_FUNCTIONAL_WIRED
;
843 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
844 * and that it matches only the local (not broadcast) address.
846 __private_extern__
struct in_ifaddr
*
847 ifa_foraddr(unsigned int addr
)
849 return ifa_foraddr_scoped(addr
, IFSCOPE_NONE
);
853 * Similar to ifa_foraddr, except with the added interface scope
854 * constraint (unless the caller passes in IFSCOPE_NONE in which
855 * case there is no scope restriction).
857 __private_extern__
struct in_ifaddr
*
858 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
860 struct in_ifaddr
*ia
= NULL
;
862 lck_rw_lock_shared(in_ifaddr_rwlock
);
863 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
864 IFA_LOCK_SPIN(&ia
->ia_ifa
);
865 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
866 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
867 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
868 IFA_UNLOCK(&ia
->ia_ifa
);
871 IFA_UNLOCK(&ia
->ia_ifa
);
873 lck_rw_done(in_ifaddr_rwlock
);
879 * Similar to ifa_foraddr, except that this for IPv6.
881 __private_extern__
struct in6_ifaddr
*
882 ifa_foraddr6(struct in6_addr
*addr6
)
884 return ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
);
887 __private_extern__
struct in6_ifaddr
*
888 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
890 struct in6_ifaddr
*ia
= NULL
;
892 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
893 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
894 IFA_LOCK(&ia
->ia_ifa
);
895 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
896 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
897 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
898 IFA_UNLOCK(&ia
->ia_ifa
);
901 IFA_UNLOCK(&ia
->ia_ifa
);
903 lck_rw_done(&in6_ifaddr_rwlock
);
910 * Return the first (primary) address of a given family on an interface.
912 __private_extern__
struct ifaddr
*
913 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
917 ifnet_lock_shared(ifp
);
918 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
920 if (ifa
->ifa_addr
->sa_family
== family
) {
921 IFA_ADDREF_LOCKED(ifa
); /* for caller */
927 ifnet_lock_done(ifp
);
933 ifa_equal(const struct sockaddr
*sa1
, const struct sockaddr
*sa2
)
938 if (sa1
->sa_len
!= sa2
->sa_len
) {
942 return bcmp(sa1
, sa2
, sa1
->sa_len
) == 0;
946 * Locate an interface based on a complete address.
949 ifa_ifwithaddr_locked(const struct sockaddr
*addr
)
953 struct ifaddr
*result
= NULL
;
955 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
956 ifp
= ifp
->if_link
.tqe_next
) {
957 ifnet_lock_shared(ifp
);
958 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
959 ifa
= ifa
->ifa_link
.tqe_next
) {
961 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
965 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
967 IFA_ADDREF_LOCKED(ifa
); /* for caller */
971 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
972 ifa
->ifa_broadaddr
!= NULL
&&
973 /* IP6 doesn't have broadcast */
974 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
975 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
977 IFA_ADDREF_LOCKED(ifa
); /* for caller */
983 ifnet_lock_done(ifp
);
990 ifa_ifwithaddr(const struct sockaddr
*addr
)
992 struct ifaddr
*result
= NULL
;
994 ifnet_head_lock_shared();
996 result
= ifa_ifwithaddr_locked(addr
);
1003 * Locate the point to point interface with a given destination address.
1007 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
1011 struct ifaddr
*result
= NULL
;
1013 ifnet_head_lock_shared();
1014 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
1015 ifp
= ifp
->if_link
.tqe_next
) {
1016 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1017 ifnet_lock_shared(ifp
);
1018 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1019 ifa
= ifa
->ifa_link
.tqe_next
) {
1021 if (ifa
->ifa_addr
->sa_family
!=
1026 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1028 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1034 ifnet_lock_done(ifp
);
1042 * Locate the source address of an interface based on a complete address.
1045 ifa_ifwithaddr_scoped_locked(const struct sockaddr
*addr
, unsigned int ifscope
)
1047 struct ifaddr
*result
= NULL
;
1050 if (ifscope
== IFSCOPE_NONE
) {
1051 return ifa_ifwithaddr_locked(addr
);
1054 if (ifscope
> (unsigned int)if_index
) {
1058 ifp
= ifindex2ifnet
[ifscope
];
1060 struct ifaddr
*ifa
= NULL
;
1063 * This is suboptimal; there should be a better way
1064 * to search for a given address of an interface
1065 * for any given address family.
1067 ifnet_lock_shared(ifp
);
1068 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
1069 ifa
= ifa
->ifa_link
.tqe_next
) {
1071 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
1075 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1077 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1081 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1082 ifa
->ifa_broadaddr
!= NULL
&&
1083 /* IP6 doesn't have broadcast */
1084 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1085 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
1087 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1093 ifnet_lock_done(ifp
);
1100 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1102 struct ifaddr
*result
= NULL
;
1104 ifnet_head_lock_shared();
1106 result
= ifa_ifwithaddr_scoped_locked(addr
, ifscope
);
1114 ifa_ifwithnet(const struct sockaddr
*addr
)
1116 return ifa_ifwithnet_common(addr
, IFSCOPE_NONE
);
1120 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1122 return ifa_ifwithnet_common(addr
, ifscope
);
1126 * Find an interface on a specific network. If many, choice
1127 * is most specific found.
1129 static struct ifaddr
*
1130 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
1133 struct ifaddr
*ifa
= NULL
;
1134 struct ifaddr
*ifa_maybe
= NULL
;
1135 u_int af
= addr
->sa_family
;
1136 const char *addr_data
= addr
->sa_data
, *cplim
;
1139 if (af
!= AF_INET
&& af
!= AF_INET6
) {
1141 if (af
!= AF_INET
) {
1143 ifscope
= IFSCOPE_NONE
;
1146 ifnet_head_lock_shared();
1148 * AF_LINK addresses can be looked up directly by their index number,
1149 * so do that if we can.
1151 if (af
== AF_LINK
) {
1152 const struct sockaddr_dl
*sdl
=
1153 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
1154 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
1155 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
1166 * Scan though each interface, looking for ones that have
1167 * addresses in this address family.
1169 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1170 ifnet_lock_shared(ifp
);
1171 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1172 ifa
= ifa
->ifa_link
.tqe_next
) {
1173 const char *cp
, *cp2
, *cp3
;
1176 if (ifa
->ifa_addr
== NULL
||
1177 ifa
->ifa_addr
->sa_family
!= af
) {
1183 * If we're looking up with a scope,
1184 * find using a matching interface.
1186 if (ifscope
!= IFSCOPE_NONE
&&
1187 ifp
->if_index
!= ifscope
) {
1193 * Scan all the bits in the ifa's address.
1194 * If a bit dissagrees with what we are
1195 * looking for, mask it with the netmask
1196 * to see if it really matters.
1197 * (A byte at a time)
1199 if (ifa
->ifa_netmask
== 0) {
1204 cp2
= ifa
->ifa_addr
->sa_data
;
1205 cp3
= ifa
->ifa_netmask
->sa_data
;
1206 cplim
= ifa
->ifa_netmask
->sa_len
+
1207 (char *)ifa
->ifa_netmask
;
1208 while (cp3
< cplim
) {
1209 if ((*cp
++ ^ *cp2
++) & *cp3
++) {
1210 goto next
; /* next address! */
1214 * If the netmask of what we just found
1215 * is more specific than what we had before
1216 * (if we had one) then remember the new one
1217 * before continuing to search
1218 * for an even better one.
1220 if (ifa_maybe
== NULL
||
1221 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1222 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1223 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1225 if (ifa_maybe
!= NULL
) {
1226 IFA_REMREF(ifa_maybe
);
1232 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1234 ifnet_lock_done(ifp
);
1244 } else if (ifa_maybe
!= NULL
) {
1245 IFA_REMREF(ifa_maybe
);
1252 * Find an interface address specific to an interface best matching
1253 * a given address applying same source address selection rules
1254 * as done in the kernel for implicit source address binding
1257 ifaof_ifpforaddr_select(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1259 u_int af
= addr
->sa_family
;
1261 if (af
== AF_INET6
) {
1262 return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6
*, addr
), ifp
, 0);
1265 return ifaof_ifpforaddr(addr
, ifp
);
1269 * Find an interface address specific to an interface best matching
1270 * a given address without regards to source address selection.
1272 * This is appropriate for use-cases where we just want to update/init
1273 * some data structure like routing table entries.
1276 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1278 struct ifaddr
*ifa
= NULL
;
1279 const char *cp
, *cp2
, *cp3
;
1281 struct ifaddr
*ifa_maybe
= NULL
;
1282 struct ifaddr
*better_ifa_maybe
= NULL
;
1283 u_int af
= addr
->sa_family
;
1289 ifnet_lock_shared(ifp
);
1290 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1291 ifa
= ifa
->ifa_link
.tqe_next
) {
1293 if (ifa
->ifa_addr
->sa_family
!= af
) {
1297 if (ifa_maybe
== NULL
) {
1298 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1301 if (ifa
->ifa_netmask
== 0) {
1302 if (ifa_equal(addr
, ifa
->ifa_addr
) ||
1303 ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1304 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1311 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1312 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1313 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1318 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1320 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1325 cp2
= ifa
->ifa_addr
->sa_data
;
1326 cp3
= ifa
->ifa_netmask
->sa_data
;
1327 cplim
= ifa
->ifa_netmask
->sa_len
+
1328 (char *)ifa
->ifa_netmask
;
1329 for (; cp3
< cplim
; cp3
++) {
1330 if ((*cp
++ ^ *cp2
++) & *cp3
) {
1336 if (better_ifa_maybe
== NULL
) {
1337 /* for better_ifa_maybe */
1338 IFA_ADDREF_LOCKED(ifa
);
1339 better_ifa_maybe
= ifa
;
1347 if (better_ifa_maybe
!= NULL
) {
1348 ifa
= better_ifa_maybe
;
1349 better_ifa_maybe
= NULL
;
1356 ifnet_lock_done(ifp
);
1358 if (better_ifa_maybe
!= NULL
) {
1359 IFA_REMREF(better_ifa_maybe
);
1361 if (ifa_maybe
!= NULL
) {
1362 IFA_REMREF(ifa_maybe
);
1368 #include <net/route.h>
1371 * Default action when installing a route with a Link Level gateway.
1372 * Lookup an appropriate real ifa to point to.
1373 * This should be moved to /sys/net/link.c eventually.
1376 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1379 struct sockaddr
*dst
;
1381 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1383 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1384 RT_LOCK_ASSERT_HELD(rt
);
1386 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1387 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0)) {
1391 /* Become a regular mutex, just in case */
1392 RT_CONVERT_LOCK(rt
);
1394 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1398 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1400 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
) {
1401 ifa_rtrequest(cmd
, rt
, sa
);
1408 * if_updown will set the interface up or down. It will
1409 * prevent other up/down events from occurring until this
1410 * up/down event has completed.
1412 * Caller must lock ifnet. This function will drop the
1413 * lock. This allows ifnet_set_flags to set the rest of
1414 * the flags after we change the up/down state without
1415 * dropping the interface lock between setting the
1416 * up/down state and updating the rest of the flags.
1418 __private_extern__
void
1419 if_updown( struct ifnet
*ifp
, int up
)
1422 struct ifaddr
**ifa
;
1424 struct ifclassq
*ifq
= &ifp
->if_snd
;
1426 /* Wait until no one else is changing the up/down state */
1427 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1429 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1430 ifnet_lock_done(ifp
);
1431 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1432 ifnet_lock_exclusive(ifp
);
1435 /* Verify that the interface isn't already in the right state */
1436 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1437 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1441 /* Indicate that the up/down state is changing */
1442 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1444 /* Mark interface up or down */
1446 ifp
->if_flags
|= IFF_UP
;
1448 ifp
->if_flags
&= ~IFF_UP
;
1451 ifnet_touch_lastchange(ifp
);
1452 ifnet_touch_lastupdown(ifp
);
1454 /* Drop the lock to notify addresses and route */
1455 ifnet_lock_done(ifp
);
1460 /* Inform all transmit queues about the new link state */
1461 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1464 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1465 for (i
= 0; ifa
[i
] != 0; i
++) {
1466 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1468 ifnet_free_address_list(ifa
);
1472 /* Aquire the lock to clear the changing flag */
1473 ifnet_lock_exclusive(ifp
);
1474 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1475 wakeup(&ifp
->if_eflags
);
1479 * Mark an interface down and notify protocols of
1486 ifnet_lock_exclusive(ifp
);
1488 ifnet_lock_done(ifp
);
1492 * Mark an interface up and notify protocols of
1499 ifnet_lock_exclusive(ifp
);
1501 ifnet_lock_done(ifp
);
1505 * Flush an interface queue.
1508 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1510 struct ifclassq
*ifq
= &ifp
->if_snd
;
1516 if (IFCQ_IS_ENABLED(ifq
)) {
1520 VERIFY(IFCQ_IS_EMPTY(ifq
));
1528 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1529 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1531 struct ifclassq
*ifq
= &ifp
->if_snd
;
1532 u_int32_t cnt
= 0, len
= 0;
1533 u_int32_t a_cnt
= 0, a_len
= 0;
1535 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1542 if (IFCQ_IS_ENABLED(ifq
)) {
1543 IFCQ_PURGE_SC(ifq
, sc
, flow
, cnt
, len
);
1550 if (packets
!= NULL
) {
1551 *packets
= cnt
+ a_cnt
;
1553 if (bytes
!= NULL
) {
1554 *bytes
= len
+ a_len
;
1559 * Extracts interface unit number and name from string, returns -1 upon failure.
1560 * Upon success, returns extracted unit number, and interface name in dst.
1563 ifunit_extract(const char *src
, char *dst
, size_t dstlen
, int *unit
)
1570 if (src
== NULL
|| dst
== NULL
|| dstlen
== 0 || unit
== NULL
) {
1575 if (len
< 2 || len
> dstlen
) {
1580 if (c
< '0' || c
> '9') {
1581 return -1; /* trailing garbage */
1587 return -1; /* no interface name */
1591 return -1; /* number is unreasonable */
1595 } while (c
>= '0' && c
<= '9');
1597 bcopy(src
, dst
, len
);
1605 * Map interface name to
1606 * interface structure pointer.
1608 static struct ifnet
*
1609 ifunit_common(const char *name
, boolean_t hold
)
1611 char namebuf
[IFNAMSIZ
+ 1];
1615 if (ifunit_extract(name
, namebuf
, sizeof(namebuf
), &unit
) < 0) {
1619 /* for safety, since we use strcmp() below */
1620 namebuf
[sizeof(namebuf
) - 1] = '\0';
1623 * Now search all the interfaces for this name/number
1625 ifnet_head_lock_shared();
1626 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1628 * Use strcmp() rather than strncmp() here,
1629 * since we want to match the entire string.
1631 if (strcmp(ifp
->if_name
, namebuf
)) {
1634 if (unit
== ifp
->if_unit
) {
1639 /* if called from ifunit_ref() and ifnet is not attached, bail */
1640 if (hold
&& ifp
!= NULL
&& !ifnet_is_attached(ifp
, 1)) {
1649 ifunit(const char *name
)
1651 return ifunit_common(name
, FALSE
);
1655 * Similar to ifunit(), except that we hold an I/O reference count on an
1656 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1657 * Will return NULL unless interface exists and is fully attached.
1660 ifunit_ref(const char *name
)
1662 return ifunit_common(name
, TRUE
);
1666 * Map interface name in a sockaddr_dl to
1667 * interface structure pointer.
1670 if_withname(struct sockaddr
*sa
)
1672 char ifname
[IFNAMSIZ
+ 1];
1673 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1675 if ((sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1676 (sdl
->sdl_nlen
> IFNAMSIZ
)) {
1681 * ifunit wants a null-terminated name. It may not be null-terminated
1682 * in the sockaddr. We don't want to change the caller's sockaddr,
1683 * and there might not be room to put the trailing null anyway, so we
1684 * make a local copy that we know we can null terminate safely.
1687 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1688 ifname
[sdl
->sdl_nlen
] = '\0';
1689 return ifunit(ifname
);
1692 static __attribute__((noinline
)) int
1693 ifioctl_ifconf(u_long cmd
, caddr_t data
)
1698 case OSIOCGIFCONF32
: /* struct ifconf32 */
1699 case SIOCGIFCONF32
: { /* struct ifconf32 */
1700 struct ifconf32 ifc
;
1701 bcopy(data
, &ifc
, sizeof(ifc
));
1702 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1704 bcopy(&ifc
, data
, sizeof(ifc
));
1708 case SIOCGIFCONF64
: /* struct ifconf64 */
1709 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1710 struct ifconf64 ifc
;
1711 bcopy(data
, &ifc
, sizeof(ifc
));
1712 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1713 bcopy(&ifc
, data
, sizeof(ifc
));
1725 static __attribute__((noinline
)) int
1726 ifioctl_ifclone(u_long cmd
, caddr_t data
)
1731 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1732 struct if_clonereq32 ifcr
;
1733 bcopy(data
, &ifcr
, sizeof(ifcr
));
1734 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1735 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1736 bcopy(&ifcr
, data
, sizeof(ifcr
));
1740 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1741 struct if_clonereq64 ifcr
;
1742 bcopy(data
, &ifcr
, sizeof(ifcr
));
1743 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1745 bcopy(&ifcr
, data
, sizeof(ifcr
));
1757 static __attribute__((noinline
)) int
1758 ifioctl_ifdesc(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1760 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1764 VERIFY(ifp
!= NULL
);
1767 case SIOCSIFDESC
: { /* struct if_descreq */
1768 if ((error
= proc_suser(p
)) != 0) {
1772 ifnet_lock_exclusive(ifp
);
1773 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof(ifdr_len
));
1774 if (ifdr_len
> sizeof(ifdr
->ifdr_desc
) ||
1775 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1777 ifnet_lock_done(ifp
);
1781 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1782 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1783 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1784 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1786 ifnet_lock_done(ifp
);
1790 case SIOCGIFDESC
: { /* struct if_descreq */
1791 ifnet_lock_shared(ifp
);
1792 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof(ifdr
->ifdr_desc
));
1793 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof(ifdr_len
));
1794 bzero(&ifdr
->ifdr_desc
, sizeof(ifdr
->ifdr_desc
));
1796 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1798 ifnet_lock_done(ifp
);
1810 static __attribute__((noinline
)) int
1811 ifioctl_linkparams(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1813 struct if_linkparamsreq
*iflpr
=
1814 (struct if_linkparamsreq
*)(void *)data
;
1815 struct ifclassq
*ifq
;
1818 VERIFY(ifp
!= NULL
);
1822 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1823 struct tb_profile tb
= { .rate
= 0, .percent
= 0, .depth
= 0 };
1825 if ((error
= proc_suser(p
)) != 0) {
1830 char netem_name
[32];
1831 (void) snprintf(netem_name
, sizeof(netem_name
),
1832 "if_output_netem_%s", if_name(ifp
));
1833 error
= netem_config(&ifp
->if_output_netem
, netem_name
,
1834 &iflpr
->iflpr_output_netem
, (void *)ifp
,
1835 ifnet_enqueue_netem
, NETEM_MAX_BATCH_SIZE
);
1841 if (!IFCQ_IS_READY(ifq
)) {
1846 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1848 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1849 sizeof(tb
.percent
));
1850 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1855 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1856 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1857 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1861 if (IFCQ_IS_ENABLED(ifq
)) {
1862 sched_type
= ifq
->ifcq_type
;
1865 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1866 sizeof(iflpr
->iflpr_output_sched
));
1868 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1869 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1870 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1872 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1873 sizeof(iflpr
->iflpr_output_tbr_rate
));
1874 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1875 sizeof(iflpr
->iflpr_output_tbr_percent
));
1878 if (ifp
->if_output_sched_model
==
1879 IFNET_SCHED_MODEL_DRIVER_MANAGED
) {
1880 flags
|= IFLPRF_DRVMANAGED
;
1882 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof(iflpr
->iflpr_flags
));
1883 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1884 sizeof(iflpr
->iflpr_output_bw
));
1885 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1886 sizeof(iflpr
->iflpr_input_bw
));
1887 bcopy(&ifp
->if_output_lt
, &iflpr
->iflpr_output_lt
,
1888 sizeof(iflpr
->iflpr_output_lt
));
1889 bcopy(&ifp
->if_input_lt
, &iflpr
->iflpr_input_lt
,
1890 sizeof(iflpr
->iflpr_input_lt
));
1892 if (ifp
->if_output_netem
!= NULL
) {
1893 netem_get_params(ifp
->if_output_netem
,
1894 &iflpr
->iflpr_output_netem
);
1908 static __attribute__((noinline
)) int
1909 ifioctl_qstats(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1911 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1912 u_int32_t ifqr_len
, ifqr_slot
;
1915 VERIFY(ifp
!= NULL
);
1918 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1919 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof(ifqr_slot
));
1920 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof(ifqr_len
));
1921 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1922 ifqr
->ifqr_buf
, &ifqr_len
);
1926 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof(ifqr_len
));
1938 static __attribute__((noinline
)) int
1939 ifioctl_throttle(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1941 struct if_throttlereq
*ifthr
= (struct if_throttlereq
*)(void *)data
;
1942 u_int32_t ifthr_level
;
1945 VERIFY(ifp
!= NULL
);
1948 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1950 * XXX: Use priv_check_cred() instead of root check?
1952 if ((error
= proc_suser(p
)) != 0) {
1956 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof(ifthr_level
));
1957 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1958 if (error
== EALREADY
) {
1964 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1965 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1966 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1967 sizeof(ifthr_level
));
1981 ifioctl_getnetagents(struct ifnet
*ifp
, u_int32_t
*count
, user_addr_t uuid_p
)
1984 u_int32_t index
= 0;
1985 u_int32_t valid_netagent_count
= 0;
1988 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_SHARED
);
1990 if (ifp
->if_agentids
!= NULL
) {
1991 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1992 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1993 if (!uuid_is_null(*netagent_uuid
)) {
1994 if (uuid_p
!= USER_ADDR_NULL
) {
1995 error
= copyout(netagent_uuid
,
1996 uuid_p
+ sizeof(uuid_t
) * valid_netagent_count
,
2002 valid_netagent_count
++;
2006 *count
= valid_netagent_count
;
2011 #define IF_MAXAGENTS 64
2012 #define IF_AGENT_INCREMENT 8
2014 if_add_netagent_locked(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
2016 VERIFY(ifp
!= NULL
);
2018 uuid_t
*first_empty_slot
= NULL
;
2019 u_int32_t index
= 0;
2020 bool already_added
= FALSE
;
2022 if (ifp
->if_agentids
!= NULL
) {
2023 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2024 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2025 if (uuid_compare(*netagent_uuid
, new_agent_uuid
) == 0) {
2026 /* Already present, ignore */
2027 already_added
= TRUE
;
2030 if (first_empty_slot
== NULL
&&
2031 uuid_is_null(*netagent_uuid
)) {
2032 first_empty_slot
= netagent_uuid
;
2036 if (already_added
) {
2037 /* Already added agent, don't return an error */
2040 if (first_empty_slot
== NULL
) {
2041 if (ifp
->if_agentcount
>= IF_MAXAGENTS
) {
2042 /* No room for another netagent UUID, bail */
2045 /* Calculate new array size */
2046 u_int32_t new_agent_count
=
2047 MIN(ifp
->if_agentcount
+ IF_AGENT_INCREMENT
,
2050 /* Reallocate array */
2051 uuid_t
*new_agent_array
= _REALLOC(ifp
->if_agentids
,
2052 sizeof(uuid_t
) * new_agent_count
, M_NETAGENT
,
2054 if (new_agent_array
== NULL
) {
2058 /* Save new array */
2059 ifp
->if_agentids
= new_agent_array
;
2061 /* Set first empty slot */
2063 &(ifp
->if_agentids
[ifp
->if_agentcount
]);
2065 /* Save new array length */
2066 ifp
->if_agentcount
= new_agent_count
;
2069 uuid_copy(*first_empty_slot
, new_agent_uuid
);
2070 netagent_post_updated_interfaces(new_agent_uuid
);
2075 if_add_netagent(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
2077 VERIFY(ifp
!= NULL
);
2079 ifnet_lock_exclusive(ifp
);
2081 int error
= if_add_netagent_locked(ifp
, new_agent_uuid
);
2083 ifnet_lock_done(ifp
);
2089 if_delete_netagent_locked(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2091 u_int32_t index
= 0;
2092 bool removed_agent_id
= FALSE
;
2094 if (ifp
->if_agentids
!= NULL
) {
2095 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2096 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2097 if (uuid_compare(*netagent_uuid
,
2098 remove_agent_uuid
) == 0) {
2099 uuid_clear(*netagent_uuid
);
2100 removed_agent_id
= TRUE
;
2105 if (removed_agent_id
) {
2106 netagent_post_updated_interfaces(remove_agent_uuid
);
2113 if_delete_netagent(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2115 VERIFY(ifp
!= NULL
);
2117 ifnet_lock_exclusive(ifp
);
2119 int error
= if_delete_netagent_locked(ifp
, remove_agent_uuid
);
2121 ifnet_lock_done(ifp
);
2127 if_check_netagent(struct ifnet
*ifp
, uuid_t find_agent_uuid
)
2129 boolean_t found
= FALSE
;
2131 if (!ifp
|| uuid_is_null(find_agent_uuid
)) {
2135 ifnet_lock_shared(ifp
);
2137 if (ifp
->if_agentids
!= NULL
) {
2138 for (uint32_t index
= 0; index
< ifp
->if_agentcount
; index
++) {
2139 if (uuid_compare(ifp
->if_agentids
[index
], find_agent_uuid
) == 0) {
2146 ifnet_lock_done(ifp
);
2151 static __attribute__((noinline
)) int
2152 ifioctl_netagent(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
2154 struct if_agentidreq
*ifar
= (struct if_agentidreq
*)(void *)data
;
2156 struct if_agentidsreq32 s32
;
2157 struct if_agentidsreq64 s64
;
2161 VERIFY(ifp
!= NULL
);
2163 /* Get an io ref count if the interface is attached */
2164 if (!ifnet_is_attached(ifp
, 1)) {
2168 if (cmd
== SIOCAIFAGENTID
||
2169 cmd
== SIOCDIFAGENTID
) {
2170 ifnet_lock_exclusive(ifp
);
2172 ifnet_lock_shared(ifp
);
2176 case SIOCAIFAGENTID
: { /* struct if_agentidreq */
2177 // TODO: Use priv_check_cred() instead of root check
2178 if ((error
= proc_suser(p
)) != 0) {
2181 error
= if_add_netagent_locked(ifp
, ifar
->ifar_uuid
);
2184 case SIOCDIFAGENTID
: { /* struct if_agentidreq */
2185 // TODO: Use priv_check_cred() instead of root check
2186 if ((error
= proc_suser(p
)) != 0) {
2189 error
= if_delete_netagent_locked(ifp
, ifar
->ifar_uuid
);
2192 case SIOCGIFAGENTIDS32
: { /* struct if_agentidsreq32 */
2193 bcopy(data
, &u
.s32
, sizeof(u
.s32
));
2194 error
= ifioctl_getnetagents(ifp
, &u
.s32
.ifar_count
,
2197 bcopy(&u
.s32
, data
, sizeof(u
.s32
));
2201 case SIOCGIFAGENTIDS64
: { /* struct if_agentidsreq64 */
2202 bcopy(data
, &u
.s64
, sizeof(u
.s64
));
2203 error
= ifioctl_getnetagents(ifp
, &u
.s64
.ifar_count
,
2206 bcopy(&u
.s64
, data
, sizeof(u
.s64
));
2215 ifnet_lock_done(ifp
);
2216 ifnet_decr_iorefcnt(ifp
);
2222 ifnet_clear_netagent(uuid_t netagent_uuid
)
2224 struct ifnet
*ifp
= NULL
;
2225 u_int32_t index
= 0;
2227 ifnet_head_lock_shared();
2229 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2230 ifnet_lock_shared(ifp
);
2231 if (ifp
->if_agentids
!= NULL
) {
2232 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2233 uuid_t
*ifp_netagent_uuid
= &(ifp
->if_agentids
[index
]);
2234 if (uuid_compare(*ifp_netagent_uuid
, netagent_uuid
) == 0) {
2235 uuid_clear(*ifp_netagent_uuid
);
2239 ifnet_lock_done(ifp
);
2246 ifnet_increment_generation(ifnet_t interface
)
2248 OSIncrementAtomic(&interface
->if_generation
);
2252 ifnet_get_generation(ifnet_t interface
)
2254 return interface
->if_generation
;
2258 ifnet_remove_from_ordered_list(struct ifnet
*ifp
)
2260 ifnet_head_assert_exclusive();
2263 TAILQ_REMOVE(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2264 ifp
->if_ordered_link
.tqe_next
= NULL
;
2265 ifp
->if_ordered_link
.tqe_prev
= NULL
;
2267 // Update ordered count
2268 VERIFY(if_ordered_count
> 0);
2273 ifnet_reset_order(u_int32_t
*ordered_indices
, u_int32_t count
)
2275 struct ifnet
*ifp
= NULL
;
2278 ifnet_head_lock_exclusive();
2279 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2280 if (ordered_indices
[order_index
] == IFSCOPE_NONE
||
2281 ordered_indices
[order_index
] > (uint32_t)if_index
) {
2287 // Flush current ordered list
2288 for (ifp
= TAILQ_FIRST(&ifnet_ordered_head
); ifp
!= NULL
;
2289 ifp
= TAILQ_FIRST(&ifnet_ordered_head
)) {
2290 ifnet_lock_exclusive(ifp
);
2291 ifnet_remove_from_ordered_list(ifp
);
2292 ifnet_lock_done(ifp
);
2295 VERIFY(if_ordered_count
== 0);
2297 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2298 u_int32_t interface_index
= ordered_indices
[order_index
];
2299 ifp
= ifindex2ifnet
[interface_index
];
2303 ifnet_lock_exclusive(ifp
);
2304 TAILQ_INSERT_TAIL(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2305 ifnet_lock_done(ifp
);
2311 necp_update_all_clients();
2317 if_set_qosmarking_mode(struct ifnet
*ifp
, u_int32_t mode
)
2320 u_int32_t old_mode
= ifp
->if_qosmarking_mode
;
2323 case IFRTYPE_QOSMARKING_MODE_NONE
:
2324 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
2326 case IFRTYPE_QOSMARKING_FASTLANE
:
2327 case IFRTYPE_QOSMARKING_RFC4594
:
2328 ifp
->if_qosmarking_mode
= mode
;
2334 if (error
== 0 && old_mode
!= ifp
->if_qosmarking_mode
) {
2335 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_QOS_MODE_CHANGED
,
2341 static __attribute__((noinline
)) int
2342 ifioctl_iforder(u_long cmd
, caddr_t data
)
2345 u_int32_t
*ordered_indices
= NULL
;
2351 case SIOCSIFORDER
: { /* struct if_order */
2352 struct if_order
*ifo
= (struct if_order
*)(void *)data
;
2354 if (ifo
->ifo_count
> (u_int32_t
)if_index
) {
2359 size_t length
= (ifo
->ifo_count
* sizeof(u_int32_t
));
2361 if (ifo
->ifo_ordered_indices
== USER_ADDR_NULL
) {
2365 ordered_indices
= _MALLOC(length
, M_NECP
, M_WAITOK
);
2366 if (ordered_indices
== NULL
) {
2371 error
= copyin(ifo
->ifo_ordered_indices
,
2372 ordered_indices
, length
);
2377 /* ordered_indices should not contain duplicates */
2378 bool found_duplicate
= FALSE
;
2379 for (uint32_t i
= 0; i
< (ifo
->ifo_count
- 1) && !found_duplicate
; i
++) {
2380 for (uint32_t j
= i
+ 1; j
< ifo
->ifo_count
&& !found_duplicate
; j
++) {
2381 if (ordered_indices
[j
] == ordered_indices
[i
]) {
2383 found_duplicate
= TRUE
;
2388 if (found_duplicate
) {
2392 error
= ifnet_reset_order(ordered_indices
, ifo
->ifo_count
);
2395 error
= ifnet_reset_order(NULL
, 0);
2406 if (ordered_indices
!= NULL
) {
2407 _FREE(ordered_indices
, M_NECP
);
2413 static __attribute__((noinline
)) int
2414 ifioctl_netsignature(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2416 struct if_nsreq
*ifnsr
= (struct if_nsreq
*)(void *)data
;
2420 VERIFY(ifp
!= NULL
);
2423 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2424 if (ifnsr
->ifnsr_len
> sizeof(ifnsr
->ifnsr_data
)) {
2428 bcopy(&ifnsr
->ifnsr_flags
, &flags
, sizeof(flags
));
2429 error
= ifnet_set_netsignature(ifp
, ifnsr
->ifnsr_family
,
2430 ifnsr
->ifnsr_len
, flags
, ifnsr
->ifnsr_data
);
2433 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2434 ifnsr
->ifnsr_len
= sizeof(ifnsr
->ifnsr_data
);
2435 error
= ifnet_get_netsignature(ifp
, ifnsr
->ifnsr_family
,
2436 &ifnsr
->ifnsr_len
, &flags
, ifnsr
->ifnsr_data
);
2438 bcopy(&flags
, &ifnsr
->ifnsr_flags
, sizeof(flags
));
2440 ifnsr
->ifnsr_len
= 0;
2453 static __attribute__((noinline
)) int
2454 ifioctl_nat64prefix(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2456 struct if_nat64req
*ifnat64
= (struct if_nat64req
*)(void *)data
;
2459 VERIFY(ifp
!= NULL
);
2462 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
2463 error
= ifnet_set_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2465 ip6stat
.ip6s_clat464_plat64_pfx_setfail
++;
2469 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
2470 error
= ifnet_get_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2472 ip6stat
.ip6s_clat464_plat64_pfx_getfail
++;
2484 static __attribute__((noinline
)) int
2485 ifioctl_clat46addr(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2487 struct if_clat46req
*ifclat46
= (struct if_clat46req
*)(void *)data
;
2488 struct in6_ifaddr
*ia6_clat
= NULL
;
2491 VERIFY(ifp
!= NULL
);
2494 case SIOCGIFCLAT46ADDR
:
2495 ia6_clat
= in6ifa_ifpwithflag(ifp
, IN6_IFF_CLAT46
);
2496 if (ia6_clat
== NULL
) {
2501 bcopy(&ia6_clat
->ia_addr
.sin6_addr
, &ifclat46
->ifclat46_addr
.v6_address
,
2502 sizeof(ifclat46
->ifclat46_addr
.v6_address
));
2503 ifclat46
->ifclat46_addr
.v6_prefixlen
= ia6_clat
->ia_plen
;
2504 IFA_REMREF(&ia6_clat
->ia_ifa
);
2517 ifioctl_get_protolist(struct ifnet
*ifp
, u_int32_t
* ret_count
,
2520 u_int32_t actual_count
;
2523 u_int32_t
*list
= NULL
;
2525 /* find out how many */
2526 count
= if_get_protolist(ifp
, NULL
, 0);
2527 if (ifpl
== USER_ADDR_NULL
) {
2531 /* copy out how many there's space for */
2532 if (*ret_count
< count
) {
2538 list
= _MALLOC(count
* sizeof(*list
), M_TEMP
, M_WAITOK
| M_ZERO
);
2543 actual_count
= if_get_protolist(ifp
, list
, count
);
2544 if (actual_count
< count
) {
2545 count
= actual_count
;
2548 error
= copyout((caddr_t
)list
, ifpl
, count
* sizeof(*list
));
2553 if_free_protolist(list
);
2559 static __attribute__((noinline
)) int
2560 ifioctl_protolist(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2565 case SIOCGIFPROTOLIST32
: { /* struct if_protolistreq32 */
2566 struct if_protolistreq32 ifpl
;
2568 bcopy(data
, &ifpl
, sizeof(ifpl
));
2569 if (ifpl
.ifpl_reserved
!= 0) {
2573 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2574 CAST_USER_ADDR_T(ifpl
.ifpl_list
));
2575 bcopy(&ifpl
, data
, sizeof(ifpl
));
2578 case SIOCGIFPROTOLIST64
: { /* struct if_protolistreq64 */
2579 struct if_protolistreq64 ifpl
;
2581 bcopy(data
, &ifpl
, sizeof(ifpl
));
2582 if (ifpl
.ifpl_reserved
!= 0) {
2586 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2588 bcopy(&ifpl
, data
, sizeof(ifpl
));
2600 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2603 ifioctl_restrict_intcoproc(unsigned long cmd
, const char *ifname
,
2604 struct ifnet
*ifp
, struct proc
*p
)
2606 if (intcoproc_unrestricted
== TRUE
) {
2609 if (proc_pid(p
) == 0) {
2613 ifp
= ifunit(ifname
);
2618 if (!IFNET_IS_INTCOPROC(ifp
)) {
2622 case SIOCGIFBRDADDR
:
2633 case SIOCGIFFUNCTIONALTYPE
:
2634 case SIOCGIFPSRCADDR
:
2635 case SIOCGIFPDSTADDR
:
2636 case SIOCGIFGENERIC
:
2640 case SIOCGIFWAKEFLAGS
:
2641 case SIOCGIFGETRTREFCNT
:
2642 case SIOCGIFOPPORTUNISTIC
:
2643 case SIOCGIFLINKQUALITYMETRIC
:
2645 case SIOCGIFDELEGATE
:
2646 case SIOCGIFEXPENSIVE
:
2647 case SIOCGIFINTERFACESTATE
:
2648 case SIOCGIFPROBECONNECTIVITY
:
2649 case SIOCGIFTIMESTAMPENABLED
:
2651 case SIOCGQOSMARKINGMODE
:
2652 case SIOCGQOSMARKINGENABLED
:
2653 case SIOCGIFLOWINTERNET
:
2655 case SIOCGIFMEDIA32
:
2656 case SIOCGIFMEDIA64
:
2657 case SIOCGIFXMEDIA32
:
2658 case SIOCGIFXMEDIA64
:
2660 case SIOCGIFLINKPARAMS
:
2661 case SIOCGIFQUEUESTATS
:
2662 case SIOCGIFTHROTTLE
:
2663 case SIOCGIFAGENTIDS32
:
2664 case SIOCGIFAGENTIDS64
:
2665 case SIOCGIFNETSIGNATURE
:
2666 case SIOCGIFINFO_IN6
:
2667 case SIOCGIFAFLAG_IN6
:
2668 case SIOCGNBRINFO_IN6
:
2669 case SIOCGIFALIFETIME_IN6
:
2670 case SIOCGIFNETMASK_IN6
:
2671 case SIOCGIFPROTOLIST32
:
2672 case SIOCGIFPROTOLIST64
:
2676 #if (DEBUG || DEVELOPMENT)
2677 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2678 __func__
, cmd
, proc_pid(p
));
2686 * Given a media word, return one suitable for an application
2687 * using the original encoding.
2690 compat_media(int media
)
2692 if (IFM_TYPE(media
) == IFM_ETHER
&& IFM_SUBTYPE(media
) > IFM_OTHER
) {
2693 media
&= ~IFM_TMASK
;
2700 compat_ifmu_ulist(struct ifnet
*ifp
, u_long cmd
, void *data
)
2702 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2703 user_addr_t user_addr
;
2705 int *media_list
= NULL
;
2707 bool list_modified
= false;
2709 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2710 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2711 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2712 if (user_addr
== USER_ADDR_NULL
|| ifmr
->ifm_count
== 0) {
2715 MALLOC(media_list
, int *, ifmr
->ifm_count
* sizeof(int),
2716 M_TEMP
, M_WAITOK
| M_ZERO
);
2717 if (media_list
== NULL
) {
2718 os_log_error(OS_LOG_DEFAULT
,
2719 "%s: %s MALLOC() failed",
2720 __func__
, ifp
->if_xname
);
2724 error
= copyin(user_addr
, media_list
, ifmr
->ifm_count
* sizeof(int));
2726 os_log_error(OS_LOG_DEFAULT
,
2727 "%s: %s copyin() error %d",
2728 __func__
, ifp
->if_xname
, error
);
2731 for (i
= 0; i
< ifmr
->ifm_count
; i
++) {
2732 int old_media
, new_media
;
2734 old_media
= media_list
[i
];
2736 new_media
= compat_media(old_media
);
2737 if (new_media
== old_media
) {
2740 if (if_verbose
!= 0) {
2741 os_log_info(OS_LOG_DEFAULT
,
2742 "%s: %s converted extended media %08x to compat media %08x",
2743 __func__
, ifp
->if_xname
, old_media
, new_media
);
2745 media_list
[i
] = new_media
;
2746 list_modified
= true;
2748 if (list_modified
) {
2749 error
= copyout(media_list
, user_addr
, ifmr
->ifm_count
* sizeof(int));
2751 os_log_error(OS_LOG_DEFAULT
,
2752 "%s: %s copyout() error %d",
2753 __func__
, ifp
->if_xname
, error
);
2758 if (media_list
!= NULL
) {
2759 FREE(media_list
, M_TEMP
);
2765 compat_ifmediareq(struct ifnet
*ifp
, u_long cmd
, void *data
)
2767 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2770 ifmr
->ifm_active
= compat_media(ifmr
->ifm_active
);
2771 ifmr
->ifm_current
= compat_media(ifmr
->ifm_current
);
2773 error
= compat_ifmu_ulist(ifp
, cmd
, data
);
2779 ifioctl_get_media(struct ifnet
*ifp
, struct socket
*so
, u_long cmd
, caddr_t data
)
2784 * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2785 * media subtypes macros from <net/if_media.h>
2788 case SIOCGIFMEDIA32
:
2789 case SIOCGIFXMEDIA32
:
2790 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA32
, data
);
2792 case SIOCGIFMEDIA64
:
2793 case SIOCGIFXMEDIA64
:
2794 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA64
, data
);
2797 if (if_verbose
!= 0 && error
!= 0) {
2798 os_log(OS_LOG_DEFAULT
, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2799 __func__
, ifp
->if_xname
, cmd
, error
);
2801 if (error
== 0 && (cmd
== SIOCGIFMEDIA32
|| cmd
== SIOCGIFMEDIA64
)) {
2802 error
= compat_ifmediareq(ifp
, cmd
, data
);
2809 * Most of the routines called to handle the ioctls would end up being
2810 * tail-call optimized, which unfortunately causes this routine to
2811 * consume too much stack space; this is the reason for the "noinline"
2812 * attribute used on those routines.
2815 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2817 char ifname
[IFNAMSIZ
+ 1];
2818 struct ifnet
*ifp
= NULL
;
2819 struct ifstat
*ifs
= NULL
;
2822 bzero(ifname
, sizeof(ifname
));
2825 * ioctls which don't require ifp, or ifreq ioctls
2828 case OSIOCGIFCONF32
: /* struct ifconf32 */
2829 case SIOCGIFCONF32
: /* struct ifconf32 */
2830 case SIOCGIFCONF64
: /* struct ifconf64 */
2831 case OSIOCGIFCONF64
: /* struct ifconf64 */
2832 error
= ifioctl_ifconf(cmd
, data
);
2835 case SIOCIFGCLONERS32
: /* struct if_clonereq32 */
2836 case SIOCIFGCLONERS64
: /* struct if_clonereq64 */
2837 error
= ifioctl_ifclone(cmd
, data
);
2840 case SIOCGIFAGENTDATA32
: /* struct netagent_req32 */
2841 case SIOCGIFAGENTDATA64
: /* struct netagent_req64 */
2842 case SIOCGIFAGENTLIST32
: /* struct netagentlist_req32 */
2843 case SIOCGIFAGENTLIST64
: /* struct netagentlist_req64 */
2844 error
= netagent_ioctl(cmd
, data
);
2847 case SIOCSIFORDER
: /* struct if_order */
2848 error
= ifioctl_iforder(cmd
, data
);
2851 case SIOCSIFDSTADDR
: /* struct ifreq */
2852 case SIOCSIFADDR
: /* struct ifreq */
2853 case SIOCSIFBRDADDR
: /* struct ifreq */
2854 case SIOCSIFNETMASK
: /* struct ifreq */
2855 case OSIOCGIFADDR
: /* struct ifreq */
2856 case OSIOCGIFDSTADDR
: /* struct ifreq */
2857 case OSIOCGIFBRDADDR
: /* struct ifreq */
2858 case OSIOCGIFNETMASK
: /* struct ifreq */
2859 case SIOCSIFKPI
: /* struct ifreq */
2860 if (so
->so_proto
== NULL
) {
2865 case SIOCIFCREATE
: /* struct ifreq */
2866 case SIOCIFCREATE2
: /* struct ifreq */
2867 case SIOCIFDESTROY
: /* struct ifreq */
2868 case SIOCGIFFLAGS
: /* struct ifreq */
2869 case SIOCGIFEFLAGS
: /* struct ifreq */
2870 case SIOCGIFCAP
: /* struct ifreq */
2872 case SIOCGIFMAC
: /* struct ifreq */
2873 case SIOCSIFMAC
: /* struct ifreq */
2874 #endif /* CONFIG_MACF_NET */
2875 case SIOCGIFMETRIC
: /* struct ifreq */
2876 case SIOCGIFMTU
: /* struct ifreq */
2877 case SIOCGIFPHYS
: /* struct ifreq */
2878 case SIOCSIFFLAGS
: /* struct ifreq */
2879 case SIOCSIFCAP
: /* struct ifreq */
2880 case SIOCSIFMETRIC
: /* struct ifreq */
2881 case SIOCSIFPHYS
: /* struct ifreq */
2882 case SIOCSIFMTU
: /* struct ifreq */
2883 case SIOCADDMULTI
: /* struct ifreq */
2884 case SIOCDELMULTI
: /* struct ifreq */
2885 case SIOCDIFPHYADDR
: /* struct ifreq */
2886 case SIOCSIFMEDIA
: /* struct ifreq */
2887 case SIOCSIFGENERIC
: /* struct ifreq */
2888 case SIOCSIFLLADDR
: /* struct ifreq */
2889 case SIOCSIFALTMTU
: /* struct ifreq */
2890 case SIOCSIFVLAN
: /* struct ifreq */
2891 case SIOCSIFBOND
: /* struct ifreq */
2892 case SIOCGIFLLADDR
: /* struct ifreq */
2893 case SIOCGIFTYPE
: /* struct ifreq */
2894 case SIOCGIFFUNCTIONALTYPE
: /* struct ifreq */
2895 case SIOCGIFPSRCADDR
: /* struct ifreq */
2896 case SIOCGIFPDSTADDR
: /* struct ifreq */
2897 case SIOCGIFGENERIC
: /* struct ifreq */
2898 case SIOCGIFDEVMTU
: /* struct ifreq */
2899 case SIOCGIFVLAN
: /* struct ifreq */
2900 case SIOCGIFBOND
: /* struct ifreq */
2901 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
2902 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
2903 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
2904 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
2905 case SIOCGIFLINKQUALITYMETRIC
: /* struct ifreq */
2906 case SIOCSIFLOG
: /* struct ifreq */
2907 case SIOCGIFLOG
: /* struct ifreq */
2908 case SIOCGIFDELEGATE
: /* struct ifreq */
2909 case SIOCGIFEXPENSIVE
: /* struct ifreq */
2910 case SIOCSIFEXPENSIVE
: /* struct ifreq */
2911 case SIOCSIF2KCL
: /* struct ifreq */
2912 case SIOCGIF2KCL
: /* struct ifreq */
2913 case SIOCSIFINTERFACESTATE
: /* struct ifreq */
2914 case SIOCGIFINTERFACESTATE
: /* struct ifreq */
2915 case SIOCSIFPROBECONNECTIVITY
: /* struct ifreq */
2916 case SIOCGIFPROBECONNECTIVITY
: /* struct ifreq */
2917 case SIOCGSTARTDELAY
: /* struct ifreq */
2918 case SIOCSIFTIMESTAMPENABLE
: /* struct ifreq */
2919 case SIOCSIFTIMESTAMPDISABLE
: /* struct ifreq */
2920 case SIOCGIFTIMESTAMPENABLED
: /* struct ifreq */
2921 #if (DEBUG || DEVELOPMENT)
2922 case SIOCSIFDISABLEOUTPUT
: /* struct ifreq */
2923 #endif /* (DEBUG || DEVELOPMENT) */
2924 case SIOCGECNMODE
: /* struct ifreq */
2926 case SIOCSQOSMARKINGMODE
: /* struct ifreq */
2927 case SIOCSQOSMARKINGENABLED
: /* struct ifreq */
2928 case SIOCGQOSMARKINGMODE
: /* struct ifreq */
2929 case SIOCGQOSMARKINGENABLED
: /* struct ifreq */
2930 case SIOCSIFLOWINTERNET
: /* struct ifreq */
2931 case SIOCGIFLOWINTERNET
: /* struct ifreq */
2932 case SIOCGIFLOWPOWER
: /* struct ifreq */
2933 case SIOCSIFLOWPOWER
: /* struct ifreq */
2934 case SIOCSIF6LOWPAN
: /* struct ifreq */
2935 case SIOCGIF6LOWPAN
: /* struct ifreq */
2936 case SIOCGIFMPKLOG
: /* struct ifreq */
2937 case SIOCSIFMPKLOG
: /* struct ifreq */
2938 case SIOCGIFCONSTRAINED
: /* struct ifreq */
2939 case SIOCSIFCONSTRAINED
: /* struct ifreq */
2940 case SIOCGIFXFLAGS
: /* struct ifreq */
2941 case SIOCGIFNOACKPRIO
: /* struct ifreq */
2942 case SIOCSIFNOACKPRIO
: /* struct ifreq */
2943 { /* struct ifreq */
2945 bcopy(data
, &ifr
, sizeof(ifr
));
2946 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2947 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2948 if (ifioctl_restrict_intcoproc(cmd
, ifname
, NULL
, p
) == true) {
2952 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2953 bcopy(&ifr
, data
, sizeof(ifr
));
2959 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2960 * here to ensure that the ifnet, if found, has been fully attached.
2964 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2965 bcopy(((struct in_aliasreq
*)(void *)data
)->ifra_name
,
2967 ifp
= ifunit_ref(ifname
);
2971 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2972 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
2974 ifp
= ifunit_ref(ifname
);
2977 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2978 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
2980 ifp
= ifunit_ref(ifname
);
2984 case SIOCGIFSTATUS
: /* struct ifstat */
2985 ifs
= _MALLOC(sizeof(*ifs
), M_DEVBUF
, M_WAITOK
);
2991 bcopy(data
, ifs
, sizeof(*ifs
));
2992 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
2993 bcopy(ifs
->ifs_name
, ifname
, IFNAMSIZ
);
2994 ifp
= ifunit_ref(ifname
);
2997 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2998 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
2999 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
3001 ifp
= ifunit_ref(ifname
);
3004 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
3005 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
3006 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
3008 ifp
= ifunit_ref(ifname
);
3011 case SIOCSIFDESC
: /* struct if_descreq */
3012 case SIOCGIFDESC
: /* struct if_descreq */
3013 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
3015 ifp
= ifunit_ref(ifname
);
3018 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
3019 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
3020 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
3022 ifp
= ifunit_ref(ifname
);
3025 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
3026 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
3028 ifp
= ifunit_ref(ifname
);
3031 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
3032 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
3033 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
3035 ifp
= ifunit_ref(ifname
);
3038 case SIOCAIFAGENTID
: /* struct if_agentidreq */
3039 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3040 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3041 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3042 bcopy(((struct if_agentidreq
*)(void *)data
)->ifar_name
,
3044 ifp
= ifunit_ref(ifname
);
3047 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3048 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3049 bcopy(((struct if_nsreq
*)(void *)data
)->ifnsr_name
,
3051 ifp
= ifunit_ref(ifname
);
3054 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3055 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3056 bcopy(((struct if_protolistreq
*)(void *)data
)->ifpl_name
,
3058 ifp
= ifunit_ref(ifname
);
3062 * This is a bad assumption, but the code seems to
3063 * have been doing this in the past; caveat emptor.
3065 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
3067 ifp
= ifunit_ref(ifname
);
3077 if (ifioctl_restrict_intcoproc(cmd
, NULL
, ifp
, p
) == true) {
3082 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
3084 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
3085 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
3087 error
= proc_suser(p
);
3092 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3097 ifnet_touch_lastchange(ifp
);
3100 case SIOCGIFSTATUS
: /* struct ifstat */
3101 VERIFY(ifs
!= NULL
);
3102 ifs
->ascii
[0] = '\0';
3104 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifs
);
3106 bcopy(ifs
, data
, sizeof(*ifs
));
3109 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
3110 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
3111 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
3112 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
3113 error
= ifioctl_get_media(ifp
, so
, cmd
, data
);
3116 case SIOCSIFDESC
: /* struct if_descreq */
3117 case SIOCGIFDESC
: /* struct if_descreq */
3118 error
= ifioctl_ifdesc(ifp
, cmd
, data
, p
);
3121 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
3122 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
3123 error
= ifioctl_linkparams(ifp
, cmd
, data
, p
);
3126 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
3127 error
= ifioctl_qstats(ifp
, cmd
, data
);
3130 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
3131 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
3132 error
= ifioctl_throttle(ifp
, cmd
, data
, p
);
3135 case SIOCAIFAGENTID
: /* struct if_agentidreq */
3136 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3137 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3138 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3139 error
= ifioctl_netagent(ifp
, cmd
, data
, p
);
3142 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3143 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3144 error
= ifioctl_netsignature(ifp
, cmd
, data
);
3148 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
3149 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
3150 error
= ifioctl_nat64prefix(ifp
, cmd
, data
);
3153 case SIOCGIFCLAT46ADDR
: /* struct if_clat46req */
3154 error
= ifioctl_clat46addr(ifp
, cmd
, data
);
3158 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3159 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3160 error
= ifioctl_protolist(ifp
, cmd
, data
);
3164 if (so
->so_proto
== NULL
) {
3170 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3172 socket_unlock(so
, 1);
3174 // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3175 // ifreq as the code expects ifaddr
3176 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3177 !(cmd
== SIOCAIFADDR
|| cmd
== SIOCDIFADDR
)) {
3178 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3185 _FREE(ifs
, M_DEVBUF
);
3189 if (ifname
[0] == '\0') {
3190 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3192 } else if (ifp
!= NULL
) {
3193 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3198 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3199 "%c %lu) error %d\n", __func__
,
3200 proc_name_address(p
), proc_pid(p
),
3201 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3202 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3203 (char)IOCGROUP(cmd
), cmd
& 0xff, error
);
3204 } else if (if_verbose
> 1) {
3205 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3206 "%c %lu) OK\n", __func__
,
3207 proc_name_address(p
), proc_pid(p
),
3208 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3209 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3210 (char)IOCGROUP(cmd
), cmd
& 0xff);
3215 ifnet_decr_iorefcnt(ifp
);
3220 static __attribute__((noinline
)) int
3221 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
3226 struct kev_msg ev_msg
;
3227 struct net_event_data ev_data
;
3229 bzero(&ev_data
, sizeof(struct net_event_data
));
3230 bzero(&ev_msg
, sizeof(struct kev_msg
));
3235 error
= proc_suser(p
);
3239 return if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
3240 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
);
3242 error
= proc_suser(p
);
3246 return if_clone_destroy(ifr
->ifr_name
);
3250 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
3251 * here to ensure that the ifnet, if found, has been fully attached.
3254 ifp
= ifunit(ifr
->ifr_name
);
3263 ifnet_lock_shared(ifp
);
3264 ifr
->ifr_flags
= ifp
->if_flags
;
3265 ifnet_lock_done(ifp
);
3269 ifnet_lock_shared(ifp
);
3270 ifr
->ifr_eflags
= ifp
->if_eflags
;
3271 ifnet_lock_done(ifp
);
3275 ifnet_lock_shared(ifp
);
3276 ifr
->ifr_xflags
= ifp
->if_xflags
;
3277 ifnet_lock_done(ifp
);
3281 ifnet_lock_shared(ifp
);
3282 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
3283 ifr
->ifr_curcap
= ifp
->if_capenable
;
3284 ifnet_lock_done(ifp
);
3289 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
3293 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
3295 #endif /* CONFIG_MACF_NET */
3298 ifnet_lock_shared(ifp
);
3299 ifr
->ifr_metric
= ifp
->if_metric
;
3300 ifnet_lock_done(ifp
);
3304 ifnet_lock_shared(ifp
);
3305 ifr
->ifr_mtu
= ifp
->if_mtu
;
3306 ifnet_lock_done(ifp
);
3310 ifnet_lock_shared(ifp
);
3311 ifr
->ifr_phys
= ifp
->if_physical
;
3312 ifnet_lock_done(ifp
);
3316 error
= proc_suser(p
);
3321 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
3322 (u_int16_t
)~IFF_CANTCHANGE
);
3325 * Note that we intentionally ignore any error from below
3326 * for the SIOCSIFFLAGS case.
3328 (void) ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3331 * Send the event even upon error from the driver because
3332 * we changed the flags.
3334 dlil_post_sifflags_msg(ifp
);
3336 ifnet_touch_lastchange(ifp
);
3340 error
= proc_suser(p
);
3345 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
3349 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3351 ifnet_touch_lastchange(ifp
);
3355 error
= proc_suser(p
);
3360 ifp
->if_metric
= ifr
->ifr_metric
;
3362 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3363 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3364 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3366 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
3367 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3368 ev_data
.if_family
= ifp
->if_family
;
3369 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3370 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3371 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3373 ev_msg
.dv
[1].data_length
= 0;
3374 dlil_post_complete_msg(ifp
, &ev_msg
);
3376 ifnet_touch_lastchange(ifp
);
3380 error
= proc_suser(p
);
3385 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3390 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3391 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3392 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3394 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
3395 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3396 ev_data
.if_family
= ifp
->if_family
;
3397 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3398 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3399 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3400 ev_msg
.dv
[1].data_length
= 0;
3401 dlil_post_complete_msg(ifp
, &ev_msg
);
3403 ifnet_touch_lastchange(ifp
);
3407 u_int32_t oldmtu
= ifp
->if_mtu
;
3408 struct ifclassq
*ifq
= &ifp
->if_snd
;
3410 error
= proc_suser(p
);
3415 if (ifp
->if_ioctl
== NULL
) {
3419 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
3423 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3428 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3429 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3430 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3432 ev_msg
.event_code
= KEV_DL_SIFMTU
;
3433 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3434 ev_data
.if_family
= ifp
->if_family
;
3435 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3436 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3437 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3438 ev_msg
.dv
[1].data_length
= 0;
3439 dlil_post_complete_msg(ifp
, &ev_msg
);
3441 ifnet_touch_lastchange(ifp
);
3445 * If the link MTU changed, do network layer specific procedure
3446 * and update all route entries associated with the interface,
3447 * so that their MTU metric gets updated.
3449 if (ifp
->if_mtu
!= oldmtu
) {
3450 if_rtmtu_update(ifp
);
3454 /* Inform all transmit queues about the new MTU */
3456 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
3464 error
= proc_suser(p
);
3469 /* Don't allow group membership on non-multicast interfaces. */
3470 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
3475 /* Don't let users screw up protocols' entries. */
3476 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
3477 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
3483 * User is permitted to anonymously join a particular link
3484 * multicast group via SIOCADDMULTI. Subsequent join requested
3485 * for the same record which has an outstanding refcnt from a
3486 * past if_addmulti_anon() will not result in EADDRINUSE error
3487 * (unlike other BSDs.) Anonymously leaving a group is also
3488 * allowed only as long as there is an outstanding refcnt held
3489 * by a previous anonymous request, or else ENOENT (even if the
3490 * link-layer multicast membership exists for a network-layer
3493 if (cmd
== SIOCADDMULTI
) {
3494 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
3495 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
3497 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
3498 ev_msg
.event_code
= KEV_DL_DELMULTI
;
3504 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3505 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3506 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3507 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3509 ev_data
.if_family
= ifp
->if_family
;
3510 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3511 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3512 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3513 ev_msg
.dv
[1].data_length
= 0;
3514 dlil_post_complete_msg(ifp
, &ev_msg
);
3516 ifnet_touch_lastchange(ifp
);
3520 error
= proc_suser(p
);
3525 * Silently ignore setting IFM_OTHER
3527 if (ifr
->ifr_media
== IFM_OTHER
) {
3528 os_log_info(OS_LOG_DEFAULT
,
3529 "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3530 __func__
, ifp
->if_xname
);
3534 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3538 ifnet_touch_lastchange(ifp
);
3541 case SIOCDIFPHYADDR
:
3542 case SIOCSIFGENERIC
:
3547 case SIOCSIF6LOWPAN
:
3548 error
= proc_suser(p
);
3553 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3558 ifnet_touch_lastchange(ifp
);
3561 case SIOCGIFLLADDR
: {
3562 struct sockaddr_dl
*sdl
= SDL(ifp
->if_lladdr
->ifa_addr
);
3564 if (sdl
->sdl_alen
== 0) {
3565 error
= EADDRNOTAVAIL
;
3568 /* If larger than 14-bytes we'll need another mechanism */
3569 if (sdl
->sdl_alen
> sizeof(ifr
->ifr_addr
.sa_data
)) {
3573 /* Follow the same convention used by SIOCSIFLLADDR */
3574 bzero(&ifr
->ifr_addr
, sizeof(ifr
->ifr_addr
));
3575 ifr
->ifr_addr
.sa_family
= AF_LINK
;
3576 ifr
->ifr_addr
.sa_len
= sdl
->sdl_alen
;
3577 error
= ifnet_guarded_lladdr_copy_bytes(ifp
,
3578 &ifr
->ifr_addr
.sa_data
, sdl
->sdl_alen
);
3583 ifr
->ifr_type
.ift_type
= ifp
->if_type
;
3584 ifr
->ifr_type
.ift_family
= ifp
->if_family
;
3585 ifr
->ifr_type
.ift_subfamily
= ifp
->if_subfamily
;
3588 case SIOCGIFFUNCTIONALTYPE
:
3589 ifr
->ifr_functional_type
= if_functional_type(ifp
, FALSE
);
3592 case SIOCGIFPSRCADDR
:
3593 case SIOCGIFPDSTADDR
:
3594 case SIOCGIFGENERIC
:
3598 case SIOCGIF6LOWPAN
:
3599 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3602 case SIOCGIFWAKEFLAGS
:
3603 ifnet_lock_shared(ifp
);
3604 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
3605 ifnet_lock_done(ifp
);
3608 case SIOCGIFGETRTREFCNT
:
3609 ifnet_lock_shared(ifp
);
3610 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
3611 ifnet_lock_done(ifp
);
3614 case SIOCSIFOPPORTUNISTIC
:
3615 case SIOCGIFOPPORTUNISTIC
:
3616 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
3619 case SIOCGIFLINKQUALITYMETRIC
:
3620 ifnet_lock_shared(ifp
);
3621 if ((ifp
->if_interface_state
.valid_bitmask
&
3622 IF_INTERFACE_STATE_LQM_STATE_VALID
)) {
3623 ifr
->ifr_link_quality_metric
=
3624 ifp
->if_interface_state
.lqm_state
;
3625 } else if (IF_FULLY_ATTACHED(ifp
)) {
3626 ifr
->ifr_link_quality_metric
=
3627 IFNET_LQM_THRESH_UNKNOWN
;
3629 ifr
->ifr_link_quality_metric
=
3630 IFNET_LQM_THRESH_OFF
;
3632 ifnet_lock_done(ifp
);
3637 error
= ifnet_getset_log(ifp
, cmd
, ifr
, p
);
3640 case SIOCGIFDELEGATE
:
3641 ifnet_lock_shared(ifp
);
3642 ifr
->ifr_delegated
= ((ifp
->if_delegated
.ifp
!= NULL
) ?
3643 ifp
->if_delegated
.ifp
->if_index
: 0);
3644 ifnet_lock_done(ifp
);
3647 case SIOCGIFEXPENSIVE
:
3648 ifnet_lock_shared(ifp
);
3649 if (ifp
->if_eflags
& IFEF_EXPENSIVE
) {
3650 ifr
->ifr_expensive
= 1;
3652 ifr
->ifr_expensive
= 0;
3654 ifnet_lock_done(ifp
);
3657 case SIOCSIFEXPENSIVE
:
3661 if ((error
= priv_check_cred(kauth_cred_get(),
3662 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3665 ifnet_lock_exclusive(ifp
);
3666 if (ifr
->ifr_expensive
) {
3667 ifp
->if_eflags
|= IFEF_EXPENSIVE
;
3669 ifp
->if_eflags
&= ~IFEF_EXPENSIVE
;
3671 ifnet_increment_generation(ifp
);
3672 ifnet_lock_done(ifp
);
3674 * Update the expensive bit in the delegated interface
3677 ifnet_head_lock_shared();
3678 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3679 ifnet_lock_exclusive(difp
);
3680 if (difp
->if_delegated
.ifp
== ifp
) {
3681 difp
->if_delegated
.expensive
=
3682 ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
3683 ifnet_increment_generation(difp
);
3685 ifnet_lock_done(difp
);
3688 necp_update_all_clients();
3692 case SIOCGIFCONSTRAINED
:
3693 ifnet_lock_shared(ifp
);
3694 if (ifp
->if_xflags
& IFXF_CONSTRAINED
) {
3695 ifr
->ifr_constrained
= 1;
3697 ifr
->ifr_constrained
= 0;
3699 ifnet_lock_done(ifp
);
3702 case SIOCSIFCONSTRAINED
:
3706 if ((error
= priv_check_cred(kauth_cred_get(),
3707 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3710 ifnet_lock_exclusive(ifp
);
3711 if (ifr
->ifr_constrained
) {
3712 ifp
->if_xflags
|= IFXF_CONSTRAINED
;
3714 ifp
->if_xflags
&= ~IFXF_CONSTRAINED
;
3716 ifnet_increment_generation(ifp
);
3717 ifnet_lock_done(ifp
);
3719 * Update the constrained bit in the delegated interface
3722 ifnet_head_lock_shared();
3723 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3724 ifnet_lock_exclusive(difp
);
3725 if (difp
->if_delegated
.ifp
== ifp
) {
3726 difp
->if_delegated
.constrained
=
3727 ifp
->if_xflags
& IFXF_CONSTRAINED
? 1 : 0;
3728 ifnet_increment_generation(difp
);
3730 ifnet_lock_done(difp
);
3733 necp_update_all_clients();
3738 ifnet_lock_shared(ifp
);
3739 if (ifp
->if_eflags
& IFEF_2KCL
) {
3744 ifnet_lock_done(ifp
);
3748 if ((error
= priv_check_cred(kauth_cred_get(),
3749 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3752 ifnet_lock_exclusive(ifp
);
3753 if (ifr
->ifr_2kcl
) {
3754 ifp
->if_eflags
|= IFEF_2KCL
;
3756 ifp
->if_eflags
&= ~IFEF_2KCL
;
3758 ifnet_lock_done(ifp
);
3760 case SIOCGSTARTDELAY
:
3761 ifnet_lock_shared(ifp
);
3762 if (ifp
->if_eflags
& IFEF_ENQUEUE_MULTI
) {
3763 ifr
->ifr_start_delay_qlen
=
3764 ifp
->if_start_delay_qlen
;
3765 ifr
->ifr_start_delay_timeout
=
3766 ifp
->if_start_delay_timeout
;
3768 ifr
->ifr_start_delay_qlen
= 0;
3769 ifr
->ifr_start_delay_timeout
= 0;
3771 ifnet_lock_done(ifp
);
3773 case SIOCSIFDSTADDR
:
3775 case SIOCSIFBRDADDR
:
3776 case SIOCSIFNETMASK
:
3778 case OSIOCGIFDSTADDR
:
3779 case OSIOCGIFBRDADDR
:
3780 case OSIOCGIFNETMASK
:
3782 VERIFY(so
->so_proto
!= NULL
);
3784 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
3785 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
3786 #if BYTE_ORDER != BIG_ENDIAN
3787 if (ifr
->ifr_addr
.sa_family
== 0 &&
3788 ifr
->ifr_addr
.sa_len
< 16) {
3789 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
3790 ifr
->ifr_addr
.sa_len
= 16;
3793 if (ifr
->ifr_addr
.sa_len
== 0) {
3794 ifr
->ifr_addr
.sa_len
= 16;
3797 } else if (cmd
== OSIOCGIFADDR
) {
3798 cmd
= SIOCGIFADDR
; /* struct ifreq */
3799 } else if (cmd
== OSIOCGIFDSTADDR
) {
3800 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
3801 } else if (cmd
== OSIOCGIFBRDADDR
) {
3802 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
3803 } else if (cmd
== OSIOCGIFNETMASK
) {
3804 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
3808 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3809 (caddr_t
)ifr
, ifp
, p
));
3810 socket_unlock(so
, 1);
3814 case OSIOCGIFDSTADDR
:
3815 case OSIOCGIFBRDADDR
:
3816 case OSIOCGIFNETMASK
:
3817 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
3821 if (cmd
== SIOCSIFKPI
) {
3822 int temperr
= proc_suser(p
);
3827 // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3828 // with ifreq as the code expects ifaddr
3829 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3830 !(cmd
== SIOCSIFADDR
|| cmd
== SIOCSIFDSTADDR
)) {
3831 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3836 case SIOCGIFINTERFACESTATE
:
3837 if_get_state(ifp
, &ifr
->ifr_interface_state
);
3840 case SIOCSIFINTERFACESTATE
:
3841 if ((error
= priv_check_cred(kauth_cred_get(),
3842 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3846 error
= if_state_update(ifp
, &ifr
->ifr_interface_state
);
3849 case SIOCSIFPROBECONNECTIVITY
:
3850 if ((error
= priv_check_cred(kauth_cred_get(),
3851 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3854 error
= if_probe_connectivity(ifp
,
3855 ifr
->ifr_probe_connectivity
);
3857 case SIOCGIFPROBECONNECTIVITY
:
3858 if ((error
= priv_check_cred(kauth_cred_get(),
3859 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3862 if (ifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
) {
3863 ifr
->ifr_probe_connectivity
= 1;
3865 ifr
->ifr_probe_connectivity
= 0;
3869 if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3871 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_ENABLE
;
3872 } else if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3874 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DISABLE
;
3876 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
3880 if ((error
= priv_check_cred(kauth_cred_get(),
3881 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3884 if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DEFAULT
) {
3885 ifp
->if_eflags
&= ~(IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
);
3886 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_ENABLE
) {
3887 ifp
->if_eflags
|= IFEF_ECN_ENABLE
;
3888 ifp
->if_eflags
&= ~IFEF_ECN_DISABLE
;
3889 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DISABLE
) {
3890 ifp
->if_eflags
|= IFEF_ECN_DISABLE
;
3891 ifp
->if_eflags
&= ~IFEF_ECN_ENABLE
;
3897 case SIOCSIFTIMESTAMPENABLE
:
3898 case SIOCSIFTIMESTAMPDISABLE
:
3899 error
= proc_suser(p
);
3904 ifnet_lock_exclusive(ifp
);
3905 if ((cmd
== SIOCSIFTIMESTAMPENABLE
&&
3906 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) ||
3907 (cmd
== SIOCSIFTIMESTAMPDISABLE
&&
3908 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) == 0)) {
3909 ifnet_lock_done(ifp
);
3912 if (cmd
== SIOCSIFTIMESTAMPENABLE
) {
3913 ifp
->if_xflags
|= IFXF_TIMESTAMP_ENABLED
;
3915 ifp
->if_xflags
&= ~IFXF_TIMESTAMP_ENABLED
;
3917 ifnet_lock_done(ifp
);
3919 * Pass the setting to the interface if it supports either
3920 * software or hardware time stamping
3922 if (ifp
->if_capabilities
& (IFCAP_HW_TIMESTAMP
|
3923 IFCAP_SW_TIMESTAMP
)) {
3924 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3928 case SIOCGIFTIMESTAMPENABLED
: {
3929 if ((ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) {
3930 ifr
->ifr_intval
= 1;
3932 ifr
->ifr_intval
= 0;
3936 case SIOCSQOSMARKINGMODE
:
3937 if ((error
= priv_check_cred(kauth_cred_get(),
3938 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3941 error
= if_set_qosmarking_mode(ifp
, ifr
->ifr_qosmarking_mode
);
3944 case SIOCGQOSMARKINGMODE
:
3945 ifr
->ifr_qosmarking_mode
= ifp
->if_qosmarking_mode
;
3948 case SIOCSQOSMARKINGENABLED
:
3949 if ((error
= priv_check_cred(kauth_cred_get(),
3950 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3953 if (ifr
->ifr_qosmarking_enabled
!= 0) {
3954 ifp
->if_eflags
|= IFEF_QOSMARKING_ENABLED
;
3956 ifp
->if_eflags
&= ~IFEF_QOSMARKING_ENABLED
;
3960 case SIOCGQOSMARKINGENABLED
:
3961 ifr
->ifr_qosmarking_enabled
=
3962 (ifp
->if_eflags
& IFEF_QOSMARKING_ENABLED
) ? 1 : 0;
3965 case SIOCSIFDISABLEOUTPUT
:
3966 #if (DEBUG || DEVELOPMENT)
3967 if (ifr
->ifr_disable_output
== 1) {
3968 error
= ifnet_disable_output(ifp
);
3969 } else if (ifr
->ifr_disable_output
== 0) {
3970 error
= ifnet_enable_output(ifp
);
3976 #endif /* (DEBUG || DEVELOPMENT) */
3979 case SIOCSIFSUBFAMILY
:
3980 if ((error
= priv_check_cred(kauth_cred_get(),
3981 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3984 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3987 case SIOCSIFLOWINTERNET
:
3988 if ((error
= priv_check_cred(kauth_cred_get(),
3989 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3993 ifnet_lock_exclusive(ifp
);
3994 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_UL
) {
3995 ifp
->if_xflags
|= IFXF_LOW_INTERNET_UL
;
3997 ifp
->if_xflags
&= ~(IFXF_LOW_INTERNET_UL
);
3999 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_DL
) {
4000 ifp
->if_xflags
|= IFXF_LOW_INTERNET_DL
;
4002 ifp
->if_xflags
&= ~(IFXF_LOW_INTERNET_DL
);
4004 ifnet_lock_done(ifp
);
4006 case SIOCGIFLOWINTERNET
:
4007 ifnet_lock_shared(ifp
);
4008 ifr
->ifr_low_internet
= 0;
4009 if (ifp
->if_xflags
& IFXF_LOW_INTERNET_UL
) {
4010 ifr
->ifr_low_internet
|=
4011 IFRTYPE_LOW_INTERNET_ENABLE_UL
;
4013 if (ifp
->if_xflags
& IFXF_LOW_INTERNET_DL
) {
4014 ifr
->ifr_low_internet
|=
4015 IFRTYPE_LOW_INTERNET_ENABLE_DL
;
4017 ifnet_lock_done(ifp
);
4019 case SIOCGIFLOWPOWER
:
4020 ifr
->ifr_low_power_mode
=
4021 !!(ifp
->if_xflags
& IFXF_LOW_POWER
);
4023 case SIOCSIFLOWPOWER
:
4024 #if (DEVELOPMENT || DEBUG)
4025 error
= if_set_low_power(ifp
, !!(ifr
->ifr_low_power_mode
));
4026 #else /* DEVELOPMENT || DEBUG */
4028 #endif /* DEVELOPMENT || DEBUG */
4032 ifr
->ifr_mpk_log
= !!(ifp
->if_xflags
& IFXF_MPK_LOG
);
4035 if (ifr
->ifr_mpk_log
) {
4036 ifp
->if_xflags
|= IFXF_MPK_LOG
;
4038 ifp
->if_xflags
&= ~IFXF_MPK_LOG
;
4041 case SIOCGIFNOACKPRIO
:
4042 ifnet_lock_shared(ifp
);
4043 if (ifp
->if_eflags
& IFEF_NOACKPRI
) {
4044 ifr
->ifr_noack_prio
= 1;
4046 ifr
->ifr_noack_prio
= 0;
4048 ifnet_lock_done(ifp
);
4051 case SIOCSIFNOACKPRIO
:
4052 if ((error
= priv_check_cred(kauth_cred_get(),
4053 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
4056 ifnet_lock_exclusive(ifp
);
4057 if (ifr
->ifr_noack_prio
) {
4058 ifp
->if_eflags
|= IFEF_NOACKPRI
;
4060 ifp
->if_eflags
&= ~IFEF_NOACKPRI
;
4062 ifnet_lock_done(ifp
);
4074 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
4078 socket_unlock(so
, 0);
4079 error
= ifioctl(so
, cmd
, data
, p
);
4085 * Set/clear promiscuous mode on interface ifp based on the truth value
4086 * of pswitch. The calls are reference counted so that only the first
4087 * "on" request actually has an effect, as does the final "off" request.
4088 * Results are undefined if the "off" and "on" requests are not matched.
4091 ifnet_set_promiscuous(
4099 ifnet_lock_exclusive(ifp
);
4100 oldflags
= ifp
->if_flags
;
4101 ifp
->if_pcount
+= pswitch
? 1 : -1;
4103 if (ifp
->if_pcount
> 0) {
4104 ifp
->if_flags
|= IFF_PROMISC
;
4106 ifp
->if_flags
&= ~IFF_PROMISC
;
4109 newflags
= ifp
->if_flags
;
4110 ifnet_lock_done(ifp
);
4112 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
4113 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
4117 ifnet_lock_exclusive(ifp
);
4119 ifp
->if_pcount
-= pswitch
? 1 : -1;
4120 if (ifp
->if_pcount
> 0) {
4121 ifp
->if_flags
|= IFF_PROMISC
;
4123 ifp
->if_flags
&= ~IFF_PROMISC
;
4125 ifnet_lock_done(ifp
);
4129 if (newflags
!= oldflags
) {
4130 log(LOG_INFO
, "%s: promiscuous mode %s%s\n",
4132 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
4133 error
!= 0 ? " failed" : " succeeded");
4139 * Return interface configuration
4140 * of system. List may be used
4141 * in later ioctl's (above) to get
4142 * other information.
4146 ifconf(u_long cmd
, user_addr_t ifrp
, int *ret_space
)
4148 struct ifnet
*ifp
= NULL
;
4153 net_thread_marks_t marks
;
4155 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
4158 * Zero the ifr buffer to make sure we don't
4159 * disclose the contents of the stack.
4161 bzero(&ifr
, sizeof(struct ifreq
));
4164 ifnet_head_lock_shared();
4165 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) &&
4166 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
4168 size_t ifnlen
, addrs
;
4170 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
4171 "%s", if_name(ifp
));
4172 if (ifnlen
+ 1 > sizeof(ifr
.ifr_name
)) {
4173 error
= ENAMETOOLONG
;
4176 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
4179 ifnet_lock_shared(ifp
);
4182 ifa
= ifp
->if_addrhead
.tqh_first
;
4183 for (; space
> sizeof(ifr
) && ifa
;
4184 ifa
= ifa
->ifa_link
.tqe_next
) {
4185 struct sockaddr
*sa
;
4188 struct sockaddr_dl sdl
;
4189 uint8_t buf
[SOCK_MAXADDRLEN
+ 1];
4193 * Make sure to accomodate the largest possible
4194 * size of SA(if_lladdr)->sa_len.
4196 _CASSERT(sizeof(u
) == (SOCK_MAXADDRLEN
+ 1));
4202 if (ifa
== ifp
->if_lladdr
) {
4203 VERIFY(sa
->sa_family
== AF_LINK
);
4204 bcopy(sa
, &u
, sa
->sa_len
);
4206 ifnet_guarded_lladdr_copy_bytes(ifp
,
4207 LLADDR(&u
.sdl
), u
.sdl
.sdl_alen
);
4212 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
4213 struct osockaddr
*osa
=
4214 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
4216 osa
->sa_family
= sa
->sa_family
;
4217 error
= copyout((caddr_t
)&ifr
, ifrp
,
4219 ifrp
+= sizeof(struct ifreq
);
4220 } else if (sa
->sa_len
<= sizeof(*sa
)) {
4222 error
= copyout((caddr_t
)&ifr
, ifrp
,
4224 ifrp
+= sizeof(struct ifreq
);
4227 sizeof(ifr
) + sa
->sa_len
- sizeof(*sa
)) {
4231 space
-= sa
->sa_len
- sizeof(*sa
);
4232 error
= copyout((caddr_t
)&ifr
, ifrp
,
4233 sizeof(ifr
.ifr_name
));
4235 error
= copyout((caddr_t
)sa
, (ifrp
+
4236 offsetof(struct ifreq
, ifr_addr
)),
4239 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
4246 space
-= sizeof(ifr
);
4248 ifnet_lock_done(ifp
);
4254 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
4255 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
4259 space
-= sizeof(ifr
);
4260 ifrp
+= sizeof(struct ifreq
);
4264 *ret_space
-= space
;
4265 net_thread_marks_pop(marks
);
4270 * Just like if_promisc(), but for all-multicast-reception mode.
4273 if_allmulti(struct ifnet
*ifp
, int onswitch
)
4278 ifnet_lock_exclusive(ifp
);
4281 if (ifp
->if_amcount
++ == 0) {
4282 ifp
->if_flags
|= IFF_ALLMULTI
;
4286 if (ifp
->if_amcount
> 1) {
4289 ifp
->if_amcount
= 0;
4290 ifp
->if_flags
&= ~IFF_ALLMULTI
;
4294 ifnet_lock_done(ifp
);
4297 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
4306 static struct ifmultiaddr
*
4309 struct ifmultiaddr
*ifma
;
4311 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
4312 zalloc_noblock(ifma_zone
);
4315 bzero(ifma
, ifma_size
);
4316 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4317 ifma
->ifma_debug
|= IFD_ALLOC
;
4318 if (ifma_debug
!= 0) {
4319 ifma
->ifma_debug
|= IFD_DEBUG
;
4320 ifma
->ifma_trace
= ifma_trace
;
4327 ifma_free(struct ifmultiaddr
*ifma
)
4331 if (ifma
->ifma_protospec
!= NULL
) {
4332 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4334 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4335 ifma
->ifma_anoncnt
!= 0) {
4336 panic("%s: Freeing ifma=%p with outstanding anon req",
4339 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4340 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4341 __func__
, ifma
, ifma
->ifma_ifp
);
4343 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
4344 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
4346 } else if (ifma
->ifma_refcount
!= 0) {
4347 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
4349 } else if (ifma
->ifma_reqcnt
!= 0) {
4350 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
4352 } else if (ifma
->ifma_ifp
!= NULL
) {
4353 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
4354 ifma
->ifma_ifp
, ifma
);
4356 } else if (ifma
->ifma_ll
!= NULL
) {
4357 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
4358 ifma
->ifma_ll
, ifma
);
4361 ifma
->ifma_debug
&= ~IFD_ALLOC
;
4362 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
4363 (IFD_DEBUG
| IFD_TRASHED
)) {
4364 lck_mtx_lock(&ifma_trash_lock
);
4365 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
4367 lck_mtx_unlock(&ifma_trash_lock
);
4368 ifma
->ifma_debug
&= ~IFD_TRASHED
;
4372 if (ifma
->ifma_addr
!= NULL
) {
4373 FREE(ifma
->ifma_addr
, M_IFADDR
);
4374 ifma
->ifma_addr
= NULL
;
4376 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
4377 zfree(ifma_zone
, ifma
);
4381 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
4383 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
4388 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
4389 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
4393 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
4394 tr
= ifma_dbg
->ifma_refhold
;
4396 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
4397 tr
= ifma_dbg
->ifma_refrele
;
4400 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
4401 ctrace_record(&tr
[idx
]);
4405 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
4410 IFMA_LOCK_ASSERT_HELD(ifma
);
4413 if (++ifma
->ifma_refcount
== 0) {
4414 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
4416 } else if (ifma
->ifma_trace
!= NULL
) {
4417 (*ifma
->ifma_trace
)(ifma
, TRUE
);
4425 ifma_remref(struct ifmultiaddr
*ifma
)
4427 struct ifmultiaddr
*ll
;
4431 if (ifma
->ifma_refcount
== 0) {
4432 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
4434 } else if (ifma
->ifma_trace
!= NULL
) {
4435 (*ifma
->ifma_trace
)(ifma
, FALSE
);
4438 --ifma
->ifma_refcount
;
4439 if (ifma
->ifma_refcount
> 0) {
4445 ifma
->ifma_ifp
= NULL
;
4446 ifma
->ifma_ll
= NULL
;
4448 ifma_free(ifma
); /* deallocate it */
4456 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4458 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4459 IFMA_LOCK_ASSERT_HELD(ifma
);
4461 if (ifma
->ifma_ifp
!= ifp
) {
4462 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
4463 ifma
->ifma_ifp
, ifp
);
4465 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4466 panic("%s: Attempt to attach an already attached ifma=%p",
4469 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4470 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
4472 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4473 panic("%s: Attempt to reattach a detached ifma=%p",
4478 ifma
->ifma_reqcnt
++;
4479 VERIFY(ifma
->ifma_reqcnt
== 1);
4480 IFMA_ADDREF_LOCKED(ifma
);
4481 ifma
->ifma_debug
|= IFD_ATTACHED
;
4483 ifma
->ifma_anoncnt
++;
4484 VERIFY(ifma
->ifma_anoncnt
== 1);
4485 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4488 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
4492 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4494 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4495 IFMA_LOCK_ASSERT_HELD(ifma
);
4497 if (ifma
->ifma_reqcnt
== 0) {
4498 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
4500 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4501 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
4503 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
4504 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
4506 } else if (ifma
->ifma_ifp
!= ifp
) {
4507 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
4508 ifma
->ifma_ifp
, ifp
);
4513 --ifma
->ifma_anoncnt
;
4514 if (ifma
->ifma_anoncnt
> 0) {
4517 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
4520 --ifma
->ifma_reqcnt
;
4521 if (ifma
->ifma_reqcnt
> 0) {
4525 if (ifma
->ifma_protospec
!= NULL
) {
4526 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4528 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4529 ifma
->ifma_anoncnt
!= 0) {
4530 panic("%s: Detaching ifma=%p with outstanding anon req",
4533 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
4534 panic("%s: Attempt to detach an unattached address ifma=%p",
4537 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4538 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
4543 * NOTE: Caller calls IFMA_REMREF
4545 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
4546 LIST_REMOVE(ifma
, ifma_link
);
4547 if (LIST_EMPTY(&ifp
->if_multiaddrs
)) {
4548 ifp
->if_updatemcasts
= 0;
4551 if (ifma
->ifma_debug
& IFD_DEBUG
) {
4552 /* Become a regular mutex, just in case */
4553 IFMA_CONVERT_LOCK(ifma
);
4554 lck_mtx_lock(&ifma_trash_lock
);
4555 TAILQ_INSERT_TAIL(&ifma_trash_head
,
4556 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
4557 lck_mtx_unlock(&ifma_trash_lock
);
4558 ifma
->ifma_debug
|= IFD_TRASHED
;
4565 * Find an ifmultiaddr that matches a socket address on an interface.
4567 * Caller is responsible for holding the ifnet_lock while calling
4571 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4572 struct ifmultiaddr
**retifma
, int anon
)
4574 struct ifmultiaddr
*ifma
;
4576 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4577 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4578 IFMA_LOCK_SPIN(ifma
);
4579 if (!ifa_equal(sa
, ifma
->ifma_addr
)) {
4584 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4585 ifma
->ifma_anoncnt
!= 0);
4586 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4587 ifma
->ifma_anoncnt
== 0);
4588 ifma
->ifma_anoncnt
++;
4589 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4590 VERIFY(ifma
->ifma_anoncnt
== 1);
4591 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4594 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
4595 ifma
->ifma_reqcnt
++;
4596 VERIFY(ifma
->ifma_reqcnt
> 1);
4598 if (retifma
!= NULL
) {
4600 IFMA_ADDREF_LOCKED(ifma
);
4609 * Radar 3642395, make sure all multicasts are in a standard format.
4611 static struct sockaddr
*
4612 copy_and_normalize(const struct sockaddr
*original
)
4615 const u_char
*aptr
= NULL
;
4616 struct sockaddr
*copy
= NULL
;
4617 struct sockaddr_dl
*sdl_new
= NULL
;
4620 if (original
->sa_family
!= AF_LINK
&&
4621 original
->sa_family
!= AF_UNSPEC
) {
4622 /* Just make a copy */
4623 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
4624 M_IFADDR
, M_WAITOK
);
4626 bcopy(original
, copy
, original
->sa_len
);
4631 switch (original
->sa_family
) {
4633 const struct sockaddr_dl
*sdl_original
=
4634 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
4636 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
4637 sdl_original
->sdl_slen
+
4638 offsetof(struct sockaddr_dl
, sdl_data
) >
4639 sdl_original
->sdl_len
) {
4643 alen
= sdl_original
->sdl_alen
;
4644 aptr
= CONST_LLADDR(sdl_original
);
4649 if (original
->sa_len
< ETHER_ADDR_LEN
+
4650 offsetof(struct sockaddr
, sa_data
)) {
4654 alen
= ETHER_ADDR_LEN
;
4655 aptr
= (const u_char
*)original
->sa_data
;
4660 if (alen
== 0 || aptr
== NULL
) {
4664 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
4665 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
4667 if (sdl_new
!= NULL
) {
4668 bzero(sdl_new
, len
);
4669 sdl_new
->sdl_len
= len
;
4670 sdl_new
->sdl_family
= AF_LINK
;
4671 sdl_new
->sdl_alen
= alen
;
4672 bcopy(aptr
, LLADDR(sdl_new
), alen
);
4675 return (struct sockaddr
*)sdl_new
;
4679 * Network-layer protocol domains which hold references to the underlying
4680 * link-layer record must use this routine.
4683 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4684 struct ifmultiaddr
**retifma
)
4686 return if_addmulti_common(ifp
, sa
, retifma
, 0);
4690 * Anything other than network-layer protocol domains which hold references
4691 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4692 * ioctl, ifnet_add_multicast(), if_bond.
4695 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4696 struct ifmultiaddr
**retifma
)
4698 return if_addmulti_common(ifp
, sa
, retifma
, 1);
4702 * Register an additional multicast address with a network interface.
4704 * - If the address is already present, bump the reference count on the
4705 * address and return.
4706 * - If the address is not link-layer, look up a link layer address.
4707 * - Allocate address structures for one or both addresses, and attach to the
4708 * multicast address list on the interface. If automatically adding a link
4709 * layer address, the protocol address will own a reference to the link
4710 * layer address, to be freed when it is freed.
4711 * - Notify the network device driver of an addition to the multicast address
4714 * 'sa' points to caller-owned memory with the desired multicast address.
4716 * 'retifma' will be used to return a pointer to the resulting multicast
4717 * address reference, if desired.
4719 * 'anon' indicates a link-layer address with no protocol address reference
4720 * made to it. Anything other than network-layer protocol domain requests
4721 * are considered as anonymous.
4724 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4725 struct ifmultiaddr
**retifma
, int anon
)
4727 struct sockaddr_storage storage
;
4728 struct sockaddr
*llsa
= NULL
;
4729 struct sockaddr
*dupsa
= NULL
;
4730 int error
= 0, ll_firstref
= 0, lladdr
;
4731 struct ifmultiaddr
*ifma
= NULL
;
4732 struct ifmultiaddr
*llifma
= NULL
;
4734 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4735 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
4736 sa
->sa_family
== AF_LINK
);
4738 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4739 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
4740 dupsa
= copy_and_normalize(sa
);
4741 if (dupsa
== NULL
) {
4748 ifnet_lock_exclusive(ifp
);
4749 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
4750 error
= EADDRNOTAVAIL
;
4751 ifnet_lock_done(ifp
);
4755 /* If the address is already present, return a new reference to it */
4756 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4757 ifnet_lock_done(ifp
);
4763 * The address isn't already present; give the link layer a chance
4764 * to accept/reject it, and also find out which AF_LINK address this
4765 * maps to, if it isn't one already.
4767 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
4769 if (error
== 0 && storage
.ss_len
!= 0) {
4770 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
4776 llifma
= ifma_alloc(M_WAITOK
);
4777 if (llifma
== NULL
) {
4783 /* to be similar to FreeBSD */
4784 if (error
== EOPNOTSUPP
) {
4786 } else if (error
!= 0) {
4790 /* Allocate while we aren't holding any locks */
4791 if (dupsa
== NULL
) {
4792 dupsa
= copy_and_normalize(sa
);
4793 if (dupsa
== NULL
) {
4798 ifma
= ifma_alloc(M_WAITOK
);
4804 ifnet_lock_exclusive(ifp
);
4806 * Check again for the matching multicast.
4808 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4810 ifnet_lock_done(ifp
);
4814 if (llifma
!= NULL
) {
4815 VERIFY(!anon
); /* must not get here if "anonymous" */
4816 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
4817 FREE(llsa
, M_IFADDR
);
4821 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
4824 llifma
->ifma_addr
= llsa
;
4825 llifma
->ifma_ifp
= ifp
;
4827 if_attach_ifma(ifp
, llifma
, 0);
4828 /* add extra refcnt for ifma */
4829 IFMA_ADDREF_LOCKED(llifma
);
4830 IFMA_UNLOCK(llifma
);
4831 ifma
->ifma_ll
= llifma
;
4835 /* "anonymous" request should not result in network address */
4836 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4838 ifma
->ifma_addr
= dupsa
;
4839 ifma
->ifma_ifp
= ifp
;
4841 if_attach_ifma(ifp
, ifma
, anon
);
4842 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4843 if (retifma
!= NULL
) {
4845 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
4847 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4848 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4850 ifnet_lock_done(ifp
);
4852 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
4853 IFMA_REMREF(ifma
); /* for this routine */
4856 * We are certain we have added something, so call down to the
4857 * interface to let them know about it. Do this only for newly-
4858 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4860 if (lladdr
|| ll_firstref
) {
4861 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
4864 if (ifp
->if_updatemcasts
> 0) {
4865 ifp
->if_updatemcasts
= 0;
4874 if (dupsa
!= NULL
) {
4875 FREE(dupsa
, M_IFADDR
);
4877 if (llifma
!= NULL
) {
4881 FREE(llsa
, M_IFADDR
);
4888 * Delete a multicast group membership by network-layer group address.
4889 * This routine is deprecated.
4892 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4894 return if_delmulti_common(NULL
, ifp
, sa
, 0);
4898 * Delete a multicast group membership by group membership pointer.
4899 * Network-layer protocol domains must use this routine.
4902 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
4904 return if_delmulti_common(ifma
, NULL
, NULL
, 0);
4908 * Anything other than network-layer protocol domains which hold references
4909 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4910 * ioctl, ifnet_remove_multicast(), if_bond.
4913 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4915 return if_delmulti_common(NULL
, ifp
, sa
, 1);
4919 * Delete a multicast group membership by network-layer group address.
4921 * Returns ENOENT if the entry could not be found.
4924 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
4925 const struct sockaddr
*sa
, int anon
)
4927 struct sockaddr
*dupsa
= NULL
;
4928 int lastref
, ll_lastref
= 0, lladdr
;
4929 struct ifmultiaddr
*ll
= NULL
;
4931 /* sanity check for callers */
4932 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
4935 ifp
= ifma
->ifma_ifp
;
4939 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
4940 dupsa
= copy_and_normalize(sa
);
4941 if (dupsa
== NULL
) {
4947 ifnet_lock_exclusive(ifp
);
4949 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4950 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4952 if (!ifa_equal(sa
, ifma
->ifma_addr
) ||
4953 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
4954 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4955 ifma
->ifma_anoncnt
!= 0);
4959 /* found; keep it locked */
4963 if (dupsa
!= NULL
) {
4964 FREE(dupsa
, M_IFADDR
);
4966 ifnet_lock_done(ifp
);
4972 IFMA_LOCK_ASSERT_HELD(ifma
);
4973 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4974 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
4975 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
4976 ifma
->ifma_reqcnt
== 0));
4977 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4979 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4980 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4982 if (lastref
&& ll
!= NULL
) {
4984 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
4987 ifnet_lock_done(ifp
);
4990 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
4993 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
4995 * Make sure the interface driver is notified in the
4996 * case of a link layer mcast group being left. Do
4997 * this only for a AF_LINK/AF_UNSPEC address that has
4998 * been removed from the if_multiaddrs set.
5000 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
5004 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
5007 IFMA_REMREF(ll
); /* for if_multiaddrs list */
5009 IFMA_REMREF(ifma
); /* for this routine */
5010 if (dupsa
!= NULL
) {
5011 FREE(dupsa
, M_IFADDR
);
5018 * Shutdown all network activity. Used boot() when halting
5028 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
5029 for (i
= 0; i
< count
; i
++) {
5031 dlil_proto_unplumb_all(ifp
[i
]);
5033 ifnet_list_free(ifp
);
5040 * Delete Routes for a Network Interface
5042 * Called for each routing entry via the rnh->rnh_walktree() call above
5043 * to delete all route entries referencing a detaching network interface.
5046 * rn pointer to node in the routing table
5047 * arg argument passed to rnh->rnh_walktree() - detaching interface
5051 * errno failed - reason indicated
5055 if_rtdel(struct radix_node
*rn
, void *arg
)
5057 struct rtentry
*rt
= (struct rtentry
*)rn
;
5058 struct ifnet
*ifp
= arg
;
5065 * Checking against RTF_UP protects against walktree
5066 * recursion problems with cloned routes.
5069 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
5071 * Safe to drop rt_lock and use rt_key, rt_gateway,
5072 * since holding rnh_lock here prevents another thread
5073 * from calling rt_setgate() on this route.
5076 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
5077 rt_mask(rt
), rt
->rt_flags
, NULL
);
5079 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
5088 * Removes routing table reference to a given interface
5089 * for a given protocol family
5092 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
5094 struct radix_node_head
*rnh
;
5096 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
5097 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
5098 lck_mtx_lock(rnh_lock
);
5099 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
5100 lck_mtx_unlock(rnh_lock
);
5105 if_rtmtu(struct radix_node
*rn
, void *arg
)
5107 struct rtentry
*rt
= (struct rtentry
*)rn
;
5108 struct ifnet
*ifp
= arg
;
5111 if (rt
->rt_ifp
== ifp
) {
5113 * Update the MTU of this entry only if the MTU
5114 * has not been locked (RTV_MTU is not set) and
5115 * if it was non-zero to begin with.
5117 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
) {
5118 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
5119 if (rt_key(rt
)->sa_family
== AF_INET
&&
5120 INTF_ADJUST_MTU_FOR_CLAT46(ifp
)) {
5121 rt
->rt_rmx
.rmx_mtu
= IN6_LINKMTU(ifp
);
5122 /* Further adjust the size for CLAT46 expansion */
5123 rt
->rt_rmx
.rmx_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
5133 * Update the MTU metric of all route entries in all protocol tables
5134 * associated with a particular interface; this is called when the
5135 * MTU of that interface has changed.
5138 if_rtmtu_update(struct ifnet
*ifp
)
5140 struct radix_node_head
*rnh
;
5143 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
5144 if ((rnh
= rt_tables
[p
]) == NULL
) {
5148 lck_mtx_lock(rnh_lock
);
5149 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
5150 lck_mtx_unlock(rnh_lock
);
5152 routegenid_update();
5155 __private_extern__
void
5156 if_data_internal_to_if_data(struct ifnet
*ifp
,
5157 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
5160 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
5161 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
5162 /* compiler will cast down to 32-bit */
5163 #define COPYFIELD32_ATOMIC(fld) do { \
5164 atomic_get_64(if_data->fld, \
5165 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5168 COPYFIELD(ifi_type
);
5169 COPYFIELD(ifi_typelen
);
5170 COPYFIELD(ifi_physical
);
5171 COPYFIELD(ifi_addrlen
);
5172 COPYFIELD(ifi_hdrlen
);
5173 COPYFIELD(ifi_recvquota
);
5174 COPYFIELD(ifi_xmitquota
);
5175 if_data
->ifi_unused1
= 0;
5177 COPYFIELD(ifi_metric
);
5178 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
5179 if_data
->ifi_baudrate
= 0xFFFFFFFF;
5181 COPYFIELD32(ifi_baudrate
);
5184 COPYFIELD32_ATOMIC(ifi_ipackets
);
5185 COPYFIELD32_ATOMIC(ifi_ierrors
);
5186 COPYFIELD32_ATOMIC(ifi_opackets
);
5187 COPYFIELD32_ATOMIC(ifi_oerrors
);
5188 COPYFIELD32_ATOMIC(ifi_collisions
);
5189 COPYFIELD32_ATOMIC(ifi_ibytes
);
5190 COPYFIELD32_ATOMIC(ifi_obytes
);
5191 COPYFIELD32_ATOMIC(ifi_imcasts
);
5192 COPYFIELD32_ATOMIC(ifi_omcasts
);
5193 COPYFIELD32_ATOMIC(ifi_iqdrops
);
5194 COPYFIELD32_ATOMIC(ifi_noproto
);
5196 COPYFIELD(ifi_recvtiming
);
5197 COPYFIELD(ifi_xmittiming
);
5199 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5200 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5202 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
5204 if_data
->ifi_unused2
= 0;
5205 COPYFIELD(ifi_hwassist
);
5206 if_data
->ifi_reserved1
= 0;
5207 if_data
->ifi_reserved2
= 0;
5208 #undef COPYFIELD32_ATOMIC
5213 __private_extern__
void
5214 if_data_internal_to_if_data64(struct ifnet
*ifp
,
5215 const struct if_data_internal
*if_data_int
,
5216 struct if_data64
*if_data64
)
5219 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
5220 #define COPYFIELD64_ATOMIC(fld) do { \
5221 atomic_get_64(if_data64->fld, \
5222 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5225 COPYFIELD64(ifi_type
);
5226 COPYFIELD64(ifi_typelen
);
5227 COPYFIELD64(ifi_physical
);
5228 COPYFIELD64(ifi_addrlen
);
5229 COPYFIELD64(ifi_hdrlen
);
5230 COPYFIELD64(ifi_recvquota
);
5231 COPYFIELD64(ifi_xmitquota
);
5232 if_data64
->ifi_unused1
= 0;
5233 COPYFIELD64(ifi_mtu
);
5234 COPYFIELD64(ifi_metric
);
5235 COPYFIELD64(ifi_baudrate
);
5237 COPYFIELD64_ATOMIC(ifi_ipackets
);
5238 COPYFIELD64_ATOMIC(ifi_ierrors
);
5239 COPYFIELD64_ATOMIC(ifi_opackets
);
5240 COPYFIELD64_ATOMIC(ifi_oerrors
);
5241 COPYFIELD64_ATOMIC(ifi_collisions
);
5242 COPYFIELD64_ATOMIC(ifi_ibytes
);
5243 COPYFIELD64_ATOMIC(ifi_obytes
);
5244 COPYFIELD64_ATOMIC(ifi_imcasts
);
5245 COPYFIELD64_ATOMIC(ifi_omcasts
);
5246 COPYFIELD64_ATOMIC(ifi_iqdrops
);
5247 COPYFIELD64_ATOMIC(ifi_noproto
);
5250 * Note these two fields are actually 32 bit, so doing
5251 * COPYFIELD64_ATOMIC will cause them to be misaligned
5253 COPYFIELD64(ifi_recvtiming
);
5254 COPYFIELD64(ifi_xmittiming
);
5256 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5257 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5259 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
5264 __private_extern__
void
5265 if_copy_traffic_class(struct ifnet
*ifp
,
5266 struct if_traffic_class
*if_tc
)
5268 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
5269 atomic_get_64(if_tc->fld, \
5270 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
5273 bzero(if_tc
, sizeof(*if_tc
));
5274 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
5275 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
5276 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
5277 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
5278 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
5279 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
5280 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
5281 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
5282 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
5283 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
5284 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
5285 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
5286 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
5287 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
5288 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
5289 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
5290 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
5291 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
5292 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
5293 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
5295 #undef COPY_IF_TC_FIELD64_ATOMIC
5299 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
5301 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
5302 atomic_get_64(if_de->fld, \
5303 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
5306 bzero(if_de
, sizeof(*if_de
));
5307 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
5308 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes
);
5309 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets
);
5310 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes
);
5312 #undef COPY_IF_DE_FIELD64_ATOMIC
5316 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
5318 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
5319 atomic_get_64(if_ps->ifi_tcp_##fld, \
5320 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
5323 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
5324 atomic_get_64(if_ps->ifi_udp_##fld, \
5325 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
5328 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
5329 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
5330 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
5331 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
5332 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
5333 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
5334 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
5335 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
5336 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
5337 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
5338 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
5339 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
5340 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
5341 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
5343 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
5344 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
5345 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
5346 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
5347 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
5348 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
5349 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
5350 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
5352 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5353 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5357 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
5359 bzero(if_rs
, sizeof(*if_rs
));
5360 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1)) {
5363 bcopy(&ifp
->if_poll_pstats
, if_rs
, sizeof(*if_rs
));
5364 /* Release the IO refcnt */
5365 ifnet_decr_iorefcnt(ifp
);
5369 if_copy_netif_stats(struct ifnet
*ifp
, struct if_netif_stats
*if_ns
)
5371 bzero(if_ns
, sizeof(*if_ns
));
5376 ifa_remref(struct ifaddr
*ifa
, int locked
)
5381 IFA_LOCK_ASSERT_HELD(ifa
);
5384 if (ifa
->ifa_refcnt
== 0) {
5385 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
5386 } else if (ifa
->ifa_trace
!= NULL
) {
5387 (*ifa
->ifa_trace
)(ifa
, FALSE
);
5389 if (--ifa
->ifa_refcnt
== 0) {
5390 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
5391 panic("ifa %p attached to ifp is being freed\n", ifa
);
5394 * Some interface addresses are allocated either statically
5395 * or carved out of a larger block. Only free it if it was
5396 * allocated via MALLOC or via the corresponding per-address
5397 * family allocator. Otherwise, leave it alone.
5399 if (ifa
->ifa_debug
& IFD_ALLOC
) {
5400 if (ifa
->ifa_free
== NULL
) {
5402 FREE(ifa
, M_IFADDR
);
5404 /* Become a regular mutex */
5405 IFA_CONVERT_LOCK(ifa
);
5406 /* callee will unlock */
5407 (*ifa
->ifa_free
)(ifa
);
5415 if (!locked
&& ifa
!= NULL
) {
5423 ifa_addref(struct ifaddr
*ifa
, int locked
)
5428 IFA_LOCK_ASSERT_HELD(ifa
);
5431 if (++ifa
->ifa_refcnt
== 0) {
5432 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
5434 } else if (ifa
->ifa_trace
!= NULL
) {
5435 (*ifa
->ifa_trace
)(ifa
, TRUE
);
5443 ifa_lock_init(struct ifaddr
*ifa
)
5445 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
5449 ifa_lock_destroy(struct ifaddr
*ifa
)
5451 IFA_LOCK_ASSERT_NOTHELD(ifa
);
5452 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);
5458 * The switch statement below does nothing at runtime, as it serves as a
5459 * compile time check to ensure that all of the socket 'i' ioctls (those
5460 * in the 'i' group going thru soo_ioctl) that are made available by the
5461 * networking stack is unique. This works as long as this routine gets
5462 * updated each time a new interface ioctl gets added.
5464 * Any failures at compile time indicates duplicated ioctl values.
5466 static __attribute__((unused
)) void
5467 ifioctl_cassert(void)
5470 * This is equivalent to _CASSERT() and the compiler wouldn't
5471 * generate any instructions, thus for compile time only.
5473 switch ((u_long
)0) {
5476 /* bsd/net/if_ppp.h */
5478 case SIOCGPPPCSTATS
:
5481 /* bsd/netinet6/in6_var.h */
5482 case SIOCSIFADDR_IN6
:
5483 case SIOCGIFADDR_IN6
:
5484 case SIOCSIFDSTADDR_IN6
:
5485 case SIOCSIFNETMASK_IN6
:
5486 case SIOCGIFDSTADDR_IN6
:
5487 case SIOCGIFNETMASK_IN6
:
5488 case SIOCDIFADDR_IN6
:
5489 case SIOCAIFADDR_IN6_32
:
5490 case SIOCAIFADDR_IN6_64
:
5491 case SIOCSIFPHYADDR_IN6_32
:
5492 case SIOCSIFPHYADDR_IN6_64
:
5493 case SIOCGIFPSRCADDR_IN6
:
5494 case SIOCGIFPDSTADDR_IN6
:
5495 case SIOCGIFAFLAG_IN6
:
5496 case SIOCGDRLST_IN6_32
:
5497 case SIOCGDRLST_IN6_64
:
5498 case SIOCGPRLST_IN6_32
:
5499 case SIOCGPRLST_IN6_64
:
5500 case OSIOCGIFINFO_IN6
:
5501 case SIOCGIFINFO_IN6
:
5502 case SIOCSNDFLUSH_IN6
:
5503 case SIOCGNBRINFO_IN6_32
:
5504 case SIOCGNBRINFO_IN6_64
:
5505 case SIOCSPFXFLUSH_IN6
:
5506 case SIOCSRTRFLUSH_IN6
:
5507 case SIOCGIFALIFETIME_IN6
:
5508 case SIOCSIFALIFETIME_IN6
:
5509 case SIOCGIFSTAT_IN6
:
5510 case SIOCGIFSTAT_ICMP6
:
5511 case SIOCSDEFIFACE_IN6_32
:
5512 case SIOCSDEFIFACE_IN6_64
:
5513 case SIOCGDEFIFACE_IN6_32
:
5514 case SIOCGDEFIFACE_IN6_64
:
5515 case SIOCSIFINFO_FLAGS
:
5518 case SIOCGSCOPE6DEF
:
5519 case SIOCSIFPREFIX_IN6
:
5520 case SIOCGIFPREFIX_IN6
:
5521 case SIOCDIFPREFIX_IN6
:
5522 case SIOCAIFPREFIX_IN6
:
5523 case SIOCCIFPREFIX_IN6
:
5524 case SIOCSGIFPREFIX_IN6
:
5525 case SIOCPROTOATTACH_IN6_32
:
5526 case SIOCPROTOATTACH_IN6_64
:
5527 case SIOCPROTODETACH_IN6
:
5528 case SIOCLL_START_32
:
5529 case SIOCLL_START_64
:
5531 case SIOCAUTOCONF_START
:
5532 case SIOCAUTOCONF_STOP
:
5533 case SIOCSETROUTERMODE_IN6
:
5534 case SIOCLL_CGASTART_32
:
5535 case SIOCLL_CGASTART_64
:
5536 case SIOCGIFCGAPREP_IN6
:
5537 case SIOCSIFCGAPREP_IN6
:
5540 /* bsd/sys/sockio.h */
5543 case SIOCSIFDSTADDR
:
5544 case OSIOCGIFDSTADDR
:
5547 case OSIOCGIFBRDADDR
:
5548 case SIOCSIFBRDADDR
:
5549 case OSIOCGIFCONF32
:
5550 case OSIOCGIFCONF64
:
5551 case OSIOCGIFNETMASK
:
5552 case SIOCSIFNETMASK
:
5559 case SIOCGIFDSTADDR
:
5560 case SIOCGIFBRDADDR
:
5563 case SIOCGIFNETMASK
:
5565 case SIOCAUTONETMASK
:
5575 case SIOCGIFMEDIA32
:
5576 case SIOCGIFMEDIA64
:
5577 case SIOCGIFXMEDIA32
:
5578 case SIOCGIFXMEDIA64
:
5579 case SIOCSIFGENERIC
:
5580 case SIOCGIFGENERIC
:
5585 case SIOCSIFPHYADDR
:
5586 case SIOCGIFPSRCADDR
:
5587 case SIOCGIFPDSTADDR
:
5588 case SIOCDIFPHYADDR
:
5596 case SIOCPROTOATTACH
:
5597 case SIOCPROTODETACH
:
5606 case SIOCSDRVSPEC32
:
5607 case SIOCGDRVSPEC32
:
5608 case SIOCSDRVSPEC64
:
5609 case SIOCGDRVSPEC64
:
5614 case SIOCIFGCLONERS32
:
5615 case SIOCIFGCLONERS64
:
5617 case SIOCGIFASYNCMAP
:
5618 case SIOCSIFASYNCMAP
:
5622 #endif /* CONFIG_MACF_NET */
5626 case SIOCGIFWAKEFLAGS
:
5628 case SIOCGIFGETRTREFCNT
:
5629 case SIOCGIFLINKQUALITYMETRIC
:
5630 case SIOCSIFOPPORTUNISTIC
:
5631 case SIOCGIFOPPORTUNISTIC
:
5632 case SIOCSETROUTERMODE
:
5636 case SIOCSIFLINKPARAMS
:
5637 case SIOCGIFLINKPARAMS
:
5638 case SIOCGIFQUEUESTATS
:
5639 case SIOCSIFTHROTTLE
:
5640 case SIOCGIFTHROTTLE
:
5642 case SIOCGASSOCIDS32
:
5643 case SIOCGASSOCIDS64
:
5644 case SIOCGCONNIDS32
:
5645 case SIOCGCONNIDS64
:
5646 case SIOCGCONNINFO32
:
5647 case SIOCGCONNINFO64
:
5648 case SIOCSCONNORDER
:
5649 case SIOCGCONNORDER
:
5653 case SIOCGIFDELEGATE
:
5656 case SIOCGIFEXPENSIVE
:
5657 case SIOCSIFEXPENSIVE
:
5660 case SIOCGSTARTDELAY
:
5662 case SIOCAIFAGENTID
:
5663 case SIOCDIFAGENTID
:
5664 case SIOCGIFAGENTIDS32
:
5665 case SIOCGIFAGENTIDS64
:
5666 case SIOCGIFAGENTDATA32
:
5667 case SIOCGIFAGENTDATA64
:
5669 case SIOCSIFINTERFACESTATE
:
5670 case SIOCGIFINTERFACESTATE
:
5671 case SIOCSIFPROBECONNECTIVITY
:
5672 case SIOCGIFPROBECONNECTIVITY
:
5674 case SIOCGIFFUNCTIONALTYPE
:
5675 case SIOCSIFNETSIGNATURE
:
5676 case SIOCGIFNETSIGNATURE
:
5683 case SIOCSQOSMARKINGMODE
:
5684 case SIOCSQOSMARKINGENABLED
:
5685 case SIOCGQOSMARKINGMODE
:
5686 case SIOCGQOSMARKINGENABLED
:
5688 case SIOCSIFTIMESTAMPENABLE
:
5689 case SIOCSIFTIMESTAMPDISABLE
:
5690 case SIOCGIFTIMESTAMPENABLED
:
5692 case SIOCSIFDISABLEOUTPUT
:
5694 case SIOCSIFSUBFAMILY
:
5696 case SIOCGIFAGENTLIST32
:
5697 case SIOCGIFAGENTLIST64
:
5699 case SIOCSIFLOWINTERNET
:
5700 case SIOCGIFLOWINTERNET
:
5703 case SIOCGIFNAT64PREFIX
:
5704 case SIOCSIFNAT64PREFIX
:
5706 case SIOCGIFCLAT46ADDR
:
5709 case SIOCGIFPROTOLIST32
:
5710 case SIOCGIFPROTOLIST64
:
5712 case SIOCSIF6LOWPAN
:
5713 case SIOCGIF6LOWPAN
:
5715 case SIOCGIFLOWPOWER
:
5716 case SIOCSIFLOWPOWER
:
5721 case SIOCGIFCONSTRAINED
:
5722 case SIOCSIFCONSTRAINED
:
5726 case SIOCGIFNOACKPRIO
:
5727 case SIOCSIFNOACKPRIO
:
5733 ifnet_mbuf_packetpreamblelen(struct ifnet
*ifp
)
5739 /* The following is used to enqueue work items for interface events */
5742 union sockaddr_in_4_6 addr
;
5743 uint32_t intf_event_code
;
5747 intf_event_callback(void *arg
)
5749 struct intf_event
*p_intf_ev
= (struct intf_event
*)arg
;
5751 /* Call this before we walk the tree */
5752 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt
, ifnet_event
, p_intf_ev
->ifp
,
5753 (struct sockaddr
*)&(p_intf_ev
->addr
), p_intf_ev
->intf_event_code
);
5756 struct intf_event_nwk_wq_entry
{
5757 struct nwk_wq_entry nwk_wqe
;
5758 struct intf_event intf_ev_arg
;
5762 intf_event_enqueue_nwk_wq_entry(struct ifnet
*ifp
, struct sockaddr
*addrp
,
5763 uint32_t intf_event_code
)
5765 #pragma unused(addrp)
5766 struct intf_event_nwk_wq_entry
*p_intf_ev
= NULL
;
5768 MALLOC(p_intf_ev
, struct intf_event_nwk_wq_entry
*,
5769 sizeof(struct intf_event_nwk_wq_entry
),
5770 M_NWKWQ
, M_WAITOK
| M_ZERO
);
5772 p_intf_ev
->intf_ev_arg
.ifp
= ifp
;
5774 * XXX Not using addr in the arg. This will be used
5775 * once we need IP address add/delete events
5777 p_intf_ev
->intf_ev_arg
.intf_event_code
= intf_event_code
;
5778 p_intf_ev
->nwk_wqe
.func
= intf_event_callback
;
5779 p_intf_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
5780 p_intf_ev
->nwk_wqe
.arg
= &p_intf_ev
->intf_ev_arg
;
5781 nwk_wq_enqueue((struct nwk_wq_entry
*)p_intf_ev
);
5785 if_get_tcp_kao_max(struct ifnet
*ifp
)
5789 if (ifp
->if_tcp_kao_max
== 0) {
5792 memset(&ifr
, 0, sizeof(struct ifreq
));
5793 error
= ifnet_ioctl(ifp
, 0, SIOCGIFTCPKAOMAX
, &ifr
);
5795 ifnet_lock_exclusive(ifp
);
5797 ifp
->if_tcp_kao_max
= ifr
.ifr_tcp_kao_max
;
5798 } else if (error
== EOPNOTSUPP
) {
5799 ifp
->if_tcp_kao_max
= default_tcp_kao_max
;
5801 ifnet_lock_done(ifp
);