2 * Copyright (c) 2000-2018 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/radix.h>
104 #include <net/route.h>
105 #include <net/dlil.h>
106 #include <net/nwk_wq.h>
108 #include <sys/domain.h>
109 #include <libkern/OSAtomic.h>
112 #include <netinet/in.h>
113 #include <netinet/in_var.h>
114 #include <netinet/in_tclass.h>
115 #include <netinet/ip_var.h>
116 #include <netinet/ip.h>
117 #include <netinet/ip6.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/tcp.h>
120 #include <netinet/tcp_var.h>
121 #include <netinet/udp.h>
122 #include <netinet/udp_var.h>
124 #include <netinet6/in6_var.h>
125 #include <netinet6/in6_ifattach.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet6/nd6.h>
129 #endif /* INET || INET6 */
132 #include <security/mac_framework.h>
138 * System initialization
141 extern char *proc_name_address(void *);
143 /* Lock group and attribute for ifaddr lock */
144 lck_attr_t
*ifa_mtx_attr
;
145 lck_grp_t
*ifa_mtx_grp
;
146 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
148 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
150 static int ifioctl_ifconf(u_long
, caddr_t
);
151 static int ifioctl_ifclone(u_long
, caddr_t
);
152 static int ifioctl_iforder(u_long
, caddr_t
);
153 static int ifioctl_ifdesc(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
154 static int ifioctl_linkparams(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
155 static int ifioctl_qstats(struct ifnet
*, u_long
, caddr_t
);
156 static int ifioctl_throttle(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
157 static int ifioctl_netsignature(struct ifnet
*, u_long
, caddr_t
);
158 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
159 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
160 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
162 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
163 struct ifmultiaddr
**, int);
164 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
165 const struct sockaddr
*, int);
166 static struct ifnet
*ifunit_common(const char *, boolean_t
);
168 static int if_rtmtu(struct radix_node
*, void *);
169 static void if_rtmtu_update(struct ifnet
*);
171 static int if_clone_list(int, int *, user_addr_t
);
173 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
175 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
177 /* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
178 struct ifnethead ifnet_ordered_head
= TAILQ_HEAD_INITIALIZER(ifnet_ordered_head
);
179 static u_int32_t if_ordered_count
= 0;
181 static int if_cloners_count
;
182 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
184 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
186 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
187 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
189 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
190 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
192 static struct ifmultiaddr
*ifma_alloc(int);
193 static void ifma_free(struct ifmultiaddr
*);
194 static void ifma_trace(struct ifmultiaddr
*, int);
197 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
199 static unsigned int ifma_debug
; /* debugging (disabled) */
201 static unsigned int ifma_size
; /* size of zone element */
202 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
204 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
207 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
209 struct ifmultiaddr_dbg
{
210 struct ifmultiaddr ifma
; /* ifmultiaddr */
211 u_int16_t ifma_refhold_cnt
; /* # of ref */
212 u_int16_t ifma_refrele_cnt
; /* # of rele */
214 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
216 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
217 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
221 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
224 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
225 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
226 static decl_lck_mtx_data(, ifma_trash_lock
);
228 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
229 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
233 * XXX: declare here to avoid to include many inet6 related files..
234 * should be more generalized?
236 extern void nd6_setmtu(struct ifnet
*);
237 extern lck_mtx_t
*nd6_mutex
;
240 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Link layers");
241 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
242 "Generic link-management");
244 SYSCTL_DECL(_net_link_generic_system
);
246 static uint32_t if_verbose
= 0;
247 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, if_verbose
,
248 CTLFLAG_RW
| CTLFLAG_LOCKED
, &if_verbose
, 0, "");
250 boolean_t intcoproc_unrestricted
;
252 /* Eventhandler context for interface events */
253 struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt
;
258 /* Setup lock group and attribute for ifaddr */
259 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
260 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
261 ifa_mtx_attr
= lck_attr_alloc_init();
263 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof(ifma_debug
));
265 ifma_size
= (ifma_debug
== 0) ? sizeof(struct ifmultiaddr
) :
266 sizeof(struct ifmultiaddr_dbg
);
268 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
270 if (ifma_zone
== NULL
) {
271 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
274 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
275 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
277 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
278 TAILQ_INIT(&ifma_trash_head
);
280 PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted
,
281 sizeof(intcoproc_unrestricted
));
285 * Network interface utility routines.
287 * Routines with ifa_ifwith* names take sockaddr *'s as
292 struct ifaddr
**ifnet_addrs
;
293 struct ifnet
**ifindex2ifnet
;
295 __private_extern__
void
296 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
298 if_attach_ifa_common(ifp
, ifa
, 0);
301 __private_extern__
void
302 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
304 if_attach_ifa_common(ifp
, ifa
, 1);
308 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
310 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
311 IFA_LOCK_ASSERT_HELD(ifa
);
313 if (ifa
->ifa_ifp
!= ifp
) {
314 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
317 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
318 panic("%s: Attempt to attach an already attached ifa=%p",
321 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
322 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
324 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
325 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
328 IFA_ADDREF_LOCKED(ifa
);
329 ifa
->ifa_debug
|= IFD_ATTACHED
;
331 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
333 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
336 if (ifa
->ifa_attached
!= NULL
) {
337 (*ifa
->ifa_attached
)(ifa
);
342 __private_extern__
void
343 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
345 if_detach_ifa_common(ifp
, ifa
, 0);
348 __private_extern__
void
349 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
351 if_detach_ifa_common(ifp
, ifa
, 1);
355 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
357 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
358 IFA_LOCK_ASSERT_HELD(ifa
);
360 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
361 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
363 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
364 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
366 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
367 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
369 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
370 panic("%s: Attempt to detach an unattached address ifa=%p",
373 } else if (ifa
->ifa_ifp
!= ifp
) {
374 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
377 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
379 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
385 panic("%s: Attempt to detach a stray address ifa=%p",
390 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
391 /* This must not be the last reference to the ifaddr */
392 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
393 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
396 ifa
->ifa_debug
&= ~(IFD_ATTACHED
| IFD_DETACHING
);
398 if (ifa
->ifa_detached
!= NULL
) {
399 (*ifa
->ifa_detached
)(ifa
);
404 #define INITIAL_IF_INDEXLIM 8
407 * Function: if_next_index
409 * Return the next available interface index.
410 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
411 * added entry when necessary.
414 * ifnet_addrs[] is indexed by (if_index - 1), whereas
415 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
416 * always allocate one extra element to hold ifindex2ifnet[0], which
419 int if_next_index(void);
421 __private_extern__
int
424 static int if_indexlim
= 0;
427 new_index
= ++if_index
;
428 if (if_index
> if_indexlim
) {
431 caddr_t new_ifnet_addrs
;
432 caddr_t new_ifindex2ifnet
;
433 caddr_t old_ifnet_addrs
;
435 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
436 if (ifnet_addrs
== NULL
) {
437 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
439 new_if_indexlim
= if_indexlim
<< 1;
442 /* allocate space for the larger arrays */
443 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
444 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
445 if (new_ifnet_addrs
== NULL
) {
450 new_ifindex2ifnet
= new_ifnet_addrs
451 + new_if_indexlim
* sizeof(caddr_t
);
452 if (ifnet_addrs
!= NULL
) {
453 /* copy the existing data */
454 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
455 if_indexlim
* sizeof(caddr_t
));
456 bcopy((caddr_t
)ifindex2ifnet
,
458 (if_indexlim
+ 1) * sizeof(caddr_t
));
461 /* switch to the new tables and size */
462 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
463 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
464 if_indexlim
= new_if_indexlim
;
466 /* release the old data */
467 if (old_ifnet_addrs
!= NULL
) {
468 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
475 * Create a clone network interface.
478 if_clone_create(char *name
, int len
, void *params
)
480 struct if_clone
*ifc
;
483 u_int32_t bytoff
, bitoff
;
487 ifc
= if_clone_lookup(name
, &unit
);
492 if (ifunit(name
) != NULL
) {
497 wildcard
= (unit
== UINT32_MAX
);
499 * Find a free unit if none was given.
502 while ((bytoff
< ifc
->ifc_bmlen
) &&
503 (ifc
->ifc_units
[bytoff
] == 0xff)) {
506 if (bytoff
>= ifc
->ifc_bmlen
) {
509 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0) {
512 unit
= (bytoff
<< 3) + bitoff
;
515 if (unit
> ifc
->ifc_maxunit
) {
519 lck_mtx_lock(&ifc
->ifc_mutex
);
520 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
522 lck_mtx_unlock(&ifc
->ifc_mutex
);
528 bitoff
= unit
- (bytoff
<< 3);
532 * Allocate the unit in the bitmap.
534 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
535 ("%s: bit is already set", __func__
));
536 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
538 /* In the wildcard case, we need to update the name. */
540 for (dp
= name
; *dp
!= '\0'; dp
++) {
543 if (snprintf(dp
, len
- (dp
- name
), "%d", unit
) >
544 len
- (dp
- name
) - 1) {
546 * This can only be a programmer error and
547 * there's no straightforward way to recover if
550 panic("%s: interface name too long", __func__
);
554 lck_mtx_unlock(&ifc
->ifc_mutex
);
560 * Destroy a clone network interface.
563 if_clone_destroy(const char *name
)
565 struct if_clone
*ifc
= NULL
;
566 struct ifnet
*ifp
= NULL
;
571 ifc
= if_clone_lookup(name
, &unit
);
578 if (unit
< ifc
->ifc_minifs
) {
583 ifp
= ifunit_ref(name
);
589 if (ifc
->ifc_destroy
== NULL
) {
594 lck_mtx_lock(&ifc
->ifc_mutex
);
595 error
= (*ifc
->ifc_destroy
)(ifp
);
598 lck_mtx_unlock(&ifc
->ifc_mutex
);
602 /* Compute offset in the bitmap and deallocate the unit. */
604 bitoff
= unit
- (bytoff
<< 3);
605 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
606 ("%s: bit is already cleared", __func__
));
607 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
608 lck_mtx_unlock(&ifc
->ifc_mutex
);
612 ifnet_decr_iorefcnt(ifp
);
618 * Look up a network interface cloner.
621 __private_extern__
struct if_clone
*
622 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
624 struct if_clone
*ifc
;
628 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
629 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
630 if (ifc
->ifc_name
[i
] != *cp
) {
636 ifc
= LIST_NEXT(ifc
, ifc_list
);
640 return (struct if_clone
*)NULL
;
646 for (i
= 0; *cp
!= '\0'; cp
++) {
647 if (*cp
< '0' || *cp
> '9') {
648 /* Bogus unit number. */
651 i
= (i
* 10) + (*cp
- '0');
662 if_clone_softc_allocate(const struct if_clone
*ifc
)
664 void *p_clone
= NULL
;
668 p_clone
= zalloc(ifc
->ifc_zone
);
669 if (p_clone
!= NULL
) {
670 bzero(p_clone
, ifc
->ifc_softc_size
);
677 if_clone_softc_deallocate(const struct if_clone
*ifc
, void *p_softc
)
679 VERIFY(ifc
!= NULL
&& p_softc
!= NULL
);
680 bzero(p_softc
, ifc
->ifc_softc_size
);
681 zfree(ifc
->ifc_zone
, p_softc
);
685 * Register a network interface cloner.
688 if_clone_attach(struct if_clone
*ifc
)
695 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
696 ("%s: %s requested more units then allowed (%d > %d)",
697 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
698 ifc
->ifc_maxunit
+ 1));
700 * Compute bitmap size and allocate it.
702 maxclone
= ifc
->ifc_maxunit
+ 1;
704 if ((len
<< 3) < maxclone
) {
707 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
708 if (ifc
->ifc_units
== NULL
) {
711 ifc
->ifc_bmlen
= len
;
712 lck_mtx_init(&ifc
->ifc_mutex
, ifnet_lock_group
, ifnet_lock_attr
);
714 if (ifc
->ifc_softc_size
!= 0) {
715 ifc
->ifc_zone
= zinit(ifc
->ifc_softc_size
,
716 ifc
->ifc_zone_max_elem
* ifc
->ifc_softc_size
, 0, ifc
->ifc_name
);
717 if (ifc
->ifc_zone
== NULL
) {
718 FREE(ifc
->ifc_units
, M_CLONE
);
721 zone_change(ifc
->ifc_zone
, Z_EXPAND
, TRUE
);
722 zone_change(ifc
->ifc_zone
, Z_CALLERACCT
, FALSE
);
725 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
728 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
729 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
731 ("%s: failed to create required interface %s%d",
732 __func__
, ifc
->ifc_name
, unit
));
734 /* Allocate the unit in the bitmap. */
736 bitoff
= unit
- (bytoff
<< 3);
737 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
744 * Unregister a network interface cloner.
747 if_clone_detach(struct if_clone
*ifc
)
749 LIST_REMOVE(ifc
, ifc_list
);
750 FREE(ifc
->ifc_units
, M_CLONE
);
751 if (ifc
->ifc_softc_size
!= 0) {
752 zdestroy(ifc
->ifc_zone
);
755 lck_mtx_destroy(&ifc
->ifc_mutex
, ifnet_lock_group
);
760 * Provide list of interface cloners to userspace.
763 if_clone_list(int count
, int *ret_total
, user_addr_t dst
)
765 char outbuf
[IFNAMSIZ
];
766 struct if_clone
*ifc
;
769 *ret_total
= if_cloners_count
;
770 if (dst
== USER_ADDR_NULL
) {
771 /* Just asking how many there are. */
779 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
781 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
782 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
783 bzero(outbuf
, sizeof(outbuf
));
784 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
785 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
795 if_functional_type(struct ifnet
*ifp
, bool exclude_delegate
)
797 u_int32_t ret
= IFRTYPE_FUNCTIONAL_UNKNOWN
;
799 if (ifp
->if_flags
& IFF_LOOPBACK
) {
800 ret
= IFRTYPE_FUNCTIONAL_LOOPBACK
;
801 } else if ((exclude_delegate
&&
802 (ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
)) ||
803 (!exclude_delegate
&& IFNET_IS_WIFI(ifp
))) {
804 if (ifp
->if_eflags
& IFEF_AWDL
) {
805 ret
= IFRTYPE_FUNCTIONAL_WIFI_AWDL
;
807 ret
= IFRTYPE_FUNCTIONAL_WIFI_INFRA
;
809 } else if ((exclude_delegate
&&
810 (ifp
->if_type
== IFT_CELLULAR
)) ||
811 (!exclude_delegate
&& IFNET_IS_CELLULAR(ifp
))) {
812 ret
= IFRTYPE_FUNCTIONAL_CELLULAR
;
813 } else if (IFNET_IS_INTCOPROC(ifp
)) {
814 ret
= IFRTYPE_FUNCTIONAL_INTCOPROC
;
815 } else if ((exclude_delegate
&&
816 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
||
817 ifp
->if_family
== IFNET_FAMILY_BOND
||
818 ifp
->if_family
== IFNET_FAMILY_VLAN
||
819 ifp
->if_family
== IFNET_FAMILY_FIREWIRE
)) ||
820 (!exclude_delegate
&& IFNET_IS_WIRED(ifp
))) {
821 ret
= IFRTYPE_FUNCTIONAL_WIRED
;
829 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
830 * and that it matches only the local (not broadcast) address.
832 __private_extern__
struct in_ifaddr
*
833 ifa_foraddr(unsigned int addr
)
835 return ifa_foraddr_scoped(addr
, IFSCOPE_NONE
);
839 * Similar to ifa_foraddr, except with the added interface scope
840 * constraint (unless the caller passes in IFSCOPE_NONE in which
841 * case there is no scope restriction).
843 __private_extern__
struct in_ifaddr
*
844 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
846 struct in_ifaddr
*ia
= NULL
;
848 lck_rw_lock_shared(in_ifaddr_rwlock
);
849 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
850 IFA_LOCK_SPIN(&ia
->ia_ifa
);
851 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
852 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
853 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
854 IFA_UNLOCK(&ia
->ia_ifa
);
857 IFA_UNLOCK(&ia
->ia_ifa
);
859 lck_rw_done(in_ifaddr_rwlock
);
865 * Similar to ifa_foraddr, except that this for IPv6.
867 __private_extern__
struct in6_ifaddr
*
868 ifa_foraddr6(struct in6_addr
*addr6
)
870 return ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
);
873 __private_extern__
struct in6_ifaddr
*
874 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
876 struct in6_ifaddr
*ia
= NULL
;
878 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
879 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
880 IFA_LOCK(&ia
->ia_ifa
);
881 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
882 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
883 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
884 IFA_UNLOCK(&ia
->ia_ifa
);
887 IFA_UNLOCK(&ia
->ia_ifa
);
889 lck_rw_done(&in6_ifaddr_rwlock
);
896 * Return the first (primary) address of a given family on an interface.
898 __private_extern__
struct ifaddr
*
899 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
903 ifnet_lock_shared(ifp
);
904 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
906 if (ifa
->ifa_addr
->sa_family
== family
) {
907 IFA_ADDREF_LOCKED(ifa
); /* for caller */
913 ifnet_lock_done(ifp
);
919 ifa_equal(const struct sockaddr
*sa1
, const struct sockaddr
*sa2
)
924 if (sa1
->sa_len
!= sa2
->sa_len
) {
928 return bcmp(sa1
, sa2
, sa1
->sa_len
) == 0;
932 * Locate an interface based on a complete address.
935 ifa_ifwithaddr_locked(const struct sockaddr
*addr
)
939 struct ifaddr
*result
= NULL
;
941 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
942 ifp
= ifp
->if_link
.tqe_next
) {
943 ifnet_lock_shared(ifp
);
944 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
945 ifa
= ifa
->ifa_link
.tqe_next
) {
947 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
951 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
953 IFA_ADDREF_LOCKED(ifa
); /* for caller */
957 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
958 ifa
->ifa_broadaddr
!= NULL
&&
959 /* IP6 doesn't have broadcast */
960 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
961 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
963 IFA_ADDREF_LOCKED(ifa
); /* for caller */
969 ifnet_lock_done(ifp
);
976 ifa_ifwithaddr(const struct sockaddr
*addr
)
978 struct ifaddr
*result
= NULL
;
980 ifnet_head_lock_shared();
982 result
= ifa_ifwithaddr_locked(addr
);
989 * Locate the point to point interface with a given destination address.
993 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
997 struct ifaddr
*result
= NULL
;
999 ifnet_head_lock_shared();
1000 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
1001 ifp
= ifp
->if_link
.tqe_next
) {
1002 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1003 ifnet_lock_shared(ifp
);
1004 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1005 ifa
= ifa
->ifa_link
.tqe_next
) {
1007 if (ifa
->ifa_addr
->sa_family
!=
1012 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1014 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1020 ifnet_lock_done(ifp
);
1028 * Locate the source address of an interface based on a complete address.
1031 ifa_ifwithaddr_scoped_locked(const struct sockaddr
*addr
, unsigned int ifscope
)
1033 struct ifaddr
*result
= NULL
;
1036 if (ifscope
== IFSCOPE_NONE
) {
1037 return ifa_ifwithaddr_locked(addr
);
1040 if (ifscope
> (unsigned int)if_index
) {
1044 ifp
= ifindex2ifnet
[ifscope
];
1046 struct ifaddr
*ifa
= NULL
;
1049 * This is suboptimal; there should be a better way
1050 * to search for a given address of an interface
1051 * for any given address family.
1053 ifnet_lock_shared(ifp
);
1054 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
1055 ifa
= ifa
->ifa_link
.tqe_next
) {
1057 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
1061 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1063 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1067 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1068 ifa
->ifa_broadaddr
!= NULL
&&
1069 /* IP6 doesn't have broadcast */
1070 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1071 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
1073 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1079 ifnet_lock_done(ifp
);
1086 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1088 struct ifaddr
*result
= NULL
;
1090 ifnet_head_lock_shared();
1092 result
= ifa_ifwithaddr_scoped_locked(addr
, ifscope
);
1100 ifa_ifwithnet(const struct sockaddr
*addr
)
1102 return ifa_ifwithnet_common(addr
, IFSCOPE_NONE
);
1106 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1108 return ifa_ifwithnet_common(addr
, ifscope
);
1112 * Find an interface on a specific network. If many, choice
1113 * is most specific found.
1115 static struct ifaddr
*
1116 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
1119 struct ifaddr
*ifa
= NULL
;
1120 struct ifaddr
*ifa_maybe
= NULL
;
1121 u_int af
= addr
->sa_family
;
1122 const char *addr_data
= addr
->sa_data
, *cplim
;
1125 if (af
!= AF_INET
&& af
!= AF_INET6
) {
1127 if (af
!= AF_INET
) {
1129 ifscope
= IFSCOPE_NONE
;
1132 ifnet_head_lock_shared();
1134 * AF_LINK addresses can be looked up directly by their index number,
1135 * so do that if we can.
1137 if (af
== AF_LINK
) {
1138 const struct sockaddr_dl
*sdl
=
1139 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
1140 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
1141 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
1152 * Scan though each interface, looking for ones that have
1153 * addresses in this address family.
1155 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1156 ifnet_lock_shared(ifp
);
1157 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1158 ifa
= ifa
->ifa_link
.tqe_next
) {
1159 const char *cp
, *cp2
, *cp3
;
1162 if (ifa
->ifa_addr
== NULL
||
1163 ifa
->ifa_addr
->sa_family
!= af
) {
1169 * If we're looking up with a scope,
1170 * find using a matching interface.
1172 if (ifscope
!= IFSCOPE_NONE
&&
1173 ifp
->if_index
!= ifscope
) {
1179 * Scan all the bits in the ifa's address.
1180 * If a bit dissagrees with what we are
1181 * looking for, mask it with the netmask
1182 * to see if it really matters.
1183 * (A byte at a time)
1185 if (ifa
->ifa_netmask
== 0) {
1190 cp2
= ifa
->ifa_addr
->sa_data
;
1191 cp3
= ifa
->ifa_netmask
->sa_data
;
1192 cplim
= ifa
->ifa_netmask
->sa_len
+
1193 (char *)ifa
->ifa_netmask
;
1194 while (cp3
< cplim
) {
1195 if ((*cp
++ ^ *cp2
++) & *cp3
++) {
1196 goto next
; /* next address! */
1200 * If the netmask of what we just found
1201 * is more specific than what we had before
1202 * (if we had one) then remember the new one
1203 * before continuing to search
1204 * for an even better one.
1206 if (ifa_maybe
== NULL
||
1207 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1208 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1209 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1211 if (ifa_maybe
!= NULL
) {
1212 IFA_REMREF(ifa_maybe
);
1218 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1220 ifnet_lock_done(ifp
);
1230 } else if (ifa_maybe
!= NULL
) {
1231 IFA_REMREF(ifa_maybe
);
1238 * Find an interface address specific to an interface best matching
1239 * a given address applying same source address selection rules
1240 * as done in the kernel for implicit source address binding
1243 ifaof_ifpforaddr_select(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1245 u_int af
= addr
->sa_family
;
1247 if (af
== AF_INET6
) {
1248 return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6
*, addr
), ifp
, 0);
1251 return ifaof_ifpforaddr(addr
, ifp
);
1255 * Find an interface address specific to an interface best matching
1256 * a given address without regards to source address selection.
1258 * This is appropriate for use-cases where we just want to update/init
1259 * some data structure like routing table entries.
1262 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1264 struct ifaddr
*ifa
= NULL
;
1265 const char *cp
, *cp2
, *cp3
;
1267 struct ifaddr
*ifa_maybe
= NULL
;
1268 struct ifaddr
*better_ifa_maybe
= NULL
;
1269 u_int af
= addr
->sa_family
;
1275 ifnet_lock_shared(ifp
);
1276 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1277 ifa
= ifa
->ifa_link
.tqe_next
) {
1279 if (ifa
->ifa_addr
->sa_family
!= af
) {
1283 if (ifa_maybe
== NULL
) {
1284 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1287 if (ifa
->ifa_netmask
== 0) {
1288 if (ifa_equal(addr
, ifa
->ifa_addr
) ||
1289 ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1290 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1297 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1298 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1299 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1304 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1306 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1311 cp2
= ifa
->ifa_addr
->sa_data
;
1312 cp3
= ifa
->ifa_netmask
->sa_data
;
1313 cplim
= ifa
->ifa_netmask
->sa_len
+
1314 (char *)ifa
->ifa_netmask
;
1315 for (; cp3
< cplim
; cp3
++) {
1316 if ((*cp
++ ^ *cp2
++) & *cp3
) {
1322 if (better_ifa_maybe
== NULL
) {
1323 /* for better_ifa_maybe */
1324 IFA_ADDREF_LOCKED(ifa
);
1325 better_ifa_maybe
= ifa
;
1333 if (better_ifa_maybe
!= NULL
) {
1334 ifa
= better_ifa_maybe
;
1335 better_ifa_maybe
= NULL
;
1342 ifnet_lock_done(ifp
);
1344 if (better_ifa_maybe
!= NULL
) {
1345 IFA_REMREF(better_ifa_maybe
);
1347 if (ifa_maybe
!= NULL
) {
1348 IFA_REMREF(ifa_maybe
);
1354 #include <net/route.h>
1357 * Default action when installing a route with a Link Level gateway.
1358 * Lookup an appropriate real ifa to point to.
1359 * This should be moved to /sys/net/link.c eventually.
1362 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1365 struct sockaddr
*dst
;
1367 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1369 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1370 RT_LOCK_ASSERT_HELD(rt
);
1372 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1373 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0)) {
1377 /* Become a regular mutex, just in case */
1378 RT_CONVERT_LOCK(rt
);
1380 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1384 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1386 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
) {
1387 ifa_rtrequest(cmd
, rt
, sa
);
1394 * if_updown will set the interface up or down. It will
1395 * prevent other up/down events from occurring until this
1396 * up/down event has completed.
1398 * Caller must lock ifnet. This function will drop the
1399 * lock. This allows ifnet_set_flags to set the rest of
1400 * the flags after we change the up/down state without
1401 * dropping the interface lock between setting the
1402 * up/down state and updating the rest of the flags.
1404 __private_extern__
void
1405 if_updown( struct ifnet
*ifp
, int up
)
1408 struct ifaddr
**ifa
;
1410 struct ifclassq
*ifq
= &ifp
->if_snd
;
1412 /* Wait until no one else is changing the up/down state */
1413 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1415 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1416 ifnet_lock_done(ifp
);
1417 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1418 ifnet_lock_exclusive(ifp
);
1421 /* Verify that the interface isn't already in the right state */
1422 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1423 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1427 /* Indicate that the up/down state is changing */
1428 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1430 /* Mark interface up or down */
1432 ifp
->if_flags
|= IFF_UP
;
1434 ifp
->if_flags
&= ~IFF_UP
;
1437 ifnet_touch_lastchange(ifp
);
1438 ifnet_touch_lastupdown(ifp
);
1440 /* Drop the lock to notify addresses and route */
1441 ifnet_lock_done(ifp
);
1446 /* Inform all transmit queues about the new link state */
1447 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1450 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1451 for (i
= 0; ifa
[i
] != 0; i
++) {
1452 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1454 ifnet_free_address_list(ifa
);
1458 /* Aquire the lock to clear the changing flag */
1459 ifnet_lock_exclusive(ifp
);
1460 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1461 wakeup(&ifp
->if_eflags
);
1465 * Mark an interface down and notify protocols of
1472 ifnet_lock_exclusive(ifp
);
1474 ifnet_lock_done(ifp
);
1478 * Mark an interface up and notify protocols of
1485 ifnet_lock_exclusive(ifp
);
1487 ifnet_lock_done(ifp
);
1491 * Flush an interface queue.
1494 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1496 struct ifclassq
*ifq
= &ifp
->if_snd
;
1502 if (IFCQ_IS_ENABLED(ifq
)) {
1506 VERIFY(IFCQ_IS_EMPTY(ifq
));
1514 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1515 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1517 struct ifclassq
*ifq
= &ifp
->if_snd
;
1518 u_int32_t cnt
= 0, len
= 0;
1519 u_int32_t a_cnt
= 0, a_len
= 0;
1521 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1528 if (IFCQ_IS_ENABLED(ifq
)) {
1529 IFCQ_PURGE_SC(ifq
, sc
, flow
, cnt
, len
);
1536 if (packets
!= NULL
) {
1537 *packets
= cnt
+ a_cnt
;
1539 if (bytes
!= NULL
) {
1540 *bytes
= len
+ a_len
;
1545 * Extracts interface unit number and name from string, returns -1 upon failure.
1546 * Upon success, returns extracted unit number, and interface name in dst.
1549 ifunit_extract(const char *src
, char *dst
, size_t dstlen
, int *unit
)
1556 if (src
== NULL
|| dst
== NULL
|| dstlen
== 0 || unit
== NULL
) {
1561 if (len
< 2 || len
> dstlen
) {
1566 if (c
< '0' || c
> '9') {
1567 return -1; /* trailing garbage */
1573 return -1; /* no interface name */
1577 return -1; /* number is unreasonable */
1581 } while (c
>= '0' && c
<= '9');
1583 bcopy(src
, dst
, len
);
1591 * Map interface name to
1592 * interface structure pointer.
1594 static struct ifnet
*
1595 ifunit_common(const char *name
, boolean_t hold
)
1597 char namebuf
[IFNAMSIZ
+ 1];
1601 if (ifunit_extract(name
, namebuf
, sizeof(namebuf
), &unit
) < 0) {
1605 /* for safety, since we use strcmp() below */
1606 namebuf
[sizeof(namebuf
) - 1] = '\0';
1609 * Now search all the interfaces for this name/number
1611 ifnet_head_lock_shared();
1612 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1614 * Use strcmp() rather than strncmp() here,
1615 * since we want to match the entire string.
1617 if (strcmp(ifp
->if_name
, namebuf
)) {
1620 if (unit
== ifp
->if_unit
) {
1625 /* if called from ifunit_ref() and ifnet is not attached, bail */
1626 if (hold
&& ifp
!= NULL
&& !ifnet_is_attached(ifp
, 1)) {
1635 ifunit(const char *name
)
1637 return ifunit_common(name
, FALSE
);
1641 * Similar to ifunit(), except that we hold an I/O reference count on an
1642 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1643 * Will return NULL unless interface exists and is fully attached.
1646 ifunit_ref(const char *name
)
1648 return ifunit_common(name
, TRUE
);
1652 * Map interface name in a sockaddr_dl to
1653 * interface structure pointer.
1656 if_withname(struct sockaddr
*sa
)
1658 char ifname
[IFNAMSIZ
+ 1];
1659 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1661 if ((sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1662 (sdl
->sdl_nlen
> IFNAMSIZ
)) {
1667 * ifunit wants a null-terminated name. It may not be null-terminated
1668 * in the sockaddr. We don't want to change the caller's sockaddr,
1669 * and there might not be room to put the trailing null anyway, so we
1670 * make a local copy that we know we can null terminate safely.
1673 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1674 ifname
[sdl
->sdl_nlen
] = '\0';
1675 return ifunit(ifname
);
1678 static __attribute__((noinline
)) int
1679 ifioctl_ifconf(u_long cmd
, caddr_t data
)
1684 case OSIOCGIFCONF32
: /* struct ifconf32 */
1685 case SIOCGIFCONF32
: { /* struct ifconf32 */
1686 struct ifconf32 ifc
;
1687 bcopy(data
, &ifc
, sizeof(ifc
));
1688 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1690 bcopy(&ifc
, data
, sizeof(ifc
));
1694 case SIOCGIFCONF64
: /* struct ifconf64 */
1695 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1696 struct ifconf64 ifc
;
1697 bcopy(data
, &ifc
, sizeof(ifc
));
1698 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1699 bcopy(&ifc
, data
, sizeof(ifc
));
1711 static __attribute__((noinline
)) int
1712 ifioctl_ifclone(u_long cmd
, caddr_t data
)
1717 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1718 struct if_clonereq32 ifcr
;
1719 bcopy(data
, &ifcr
, sizeof(ifcr
));
1720 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1721 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1722 bcopy(&ifcr
, data
, sizeof(ifcr
));
1726 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1727 struct if_clonereq64 ifcr
;
1728 bcopy(data
, &ifcr
, sizeof(ifcr
));
1729 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1731 bcopy(&ifcr
, data
, sizeof(ifcr
));
1743 static __attribute__((noinline
)) int
1744 ifioctl_ifdesc(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1746 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1750 VERIFY(ifp
!= NULL
);
1753 case SIOCSIFDESC
: { /* struct if_descreq */
1754 if ((error
= proc_suser(p
)) != 0) {
1758 ifnet_lock_exclusive(ifp
);
1759 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof(ifdr_len
));
1760 if (ifdr_len
> sizeof(ifdr
->ifdr_desc
) ||
1761 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1763 ifnet_lock_done(ifp
);
1767 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1768 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1769 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1770 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1772 ifnet_lock_done(ifp
);
1776 case SIOCGIFDESC
: { /* struct if_descreq */
1777 ifnet_lock_shared(ifp
);
1778 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof(ifdr
->ifdr_desc
));
1779 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof(ifdr_len
));
1780 bzero(&ifdr
->ifdr_desc
, sizeof(ifdr
->ifdr_desc
));
1782 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1784 ifnet_lock_done(ifp
);
1796 static __attribute__((noinline
)) int
1797 ifioctl_linkparams(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1799 struct if_linkparamsreq
*iflpr
=
1800 (struct if_linkparamsreq
*)(void *)data
;
1801 struct ifclassq
*ifq
;
1804 VERIFY(ifp
!= NULL
);
1808 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1809 struct tb_profile tb
= { 0, 0, 0 };
1811 if ((error
= proc_suser(p
)) != 0) {
1817 if (!IFCQ_IS_READY(ifq
)) {
1822 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1824 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1825 sizeof(tb
.percent
));
1826 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1831 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1832 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1833 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1837 if (IFCQ_IS_ENABLED(ifq
)) {
1838 sched_type
= ifq
->ifcq_type
;
1841 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1842 sizeof(iflpr
->iflpr_output_sched
));
1844 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1845 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1846 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1848 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1849 sizeof(iflpr
->iflpr_output_tbr_rate
));
1850 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1851 sizeof(iflpr
->iflpr_output_tbr_percent
));
1854 if (ifp
->if_output_sched_model
==
1855 IFNET_SCHED_MODEL_DRIVER_MANAGED
) {
1856 flags
|= IFLPRF_DRVMANAGED
;
1858 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof(iflpr
->iflpr_flags
));
1859 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1860 sizeof(iflpr
->iflpr_output_bw
));
1861 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1862 sizeof(iflpr
->iflpr_input_bw
));
1863 bcopy(&ifp
->if_output_lt
, &iflpr
->iflpr_output_lt
,
1864 sizeof(iflpr
->iflpr_output_lt
));
1865 bcopy(&ifp
->if_input_lt
, &iflpr
->iflpr_input_lt
,
1866 sizeof(iflpr
->iflpr_input_lt
));
1878 static __attribute__((noinline
)) int
1879 ifioctl_qstats(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1881 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1882 u_int32_t ifqr_len
, ifqr_slot
;
1885 VERIFY(ifp
!= NULL
);
1888 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1889 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof(ifqr_slot
));
1890 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof(ifqr_len
));
1891 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1892 ifqr
->ifqr_buf
, &ifqr_len
);
1896 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof(ifqr_len
));
1908 static __attribute__((noinline
)) int
1909 ifioctl_throttle(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1911 struct if_throttlereq
*ifthr
= (struct if_throttlereq
*)(void *)data
;
1912 u_int32_t ifthr_level
;
1915 VERIFY(ifp
!= NULL
);
1918 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1920 * XXX: Use priv_check_cred() instead of root check?
1922 if ((error
= proc_suser(p
)) != 0) {
1926 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof(ifthr_level
));
1927 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1928 if (error
== EALREADY
) {
1934 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1935 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1936 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1937 sizeof(ifthr_level
));
1951 ifioctl_getnetagents(struct ifnet
*ifp
, u_int32_t
*count
, user_addr_t uuid_p
)
1954 u_int32_t index
= 0;
1955 u_int32_t valid_netagent_count
= 0;
1958 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_SHARED
);
1960 if (ifp
->if_agentids
!= NULL
) {
1961 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1962 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1963 if (!uuid_is_null(*netagent_uuid
)) {
1964 if (uuid_p
!= USER_ADDR_NULL
) {
1965 error
= copyout(netagent_uuid
,
1966 uuid_p
+ sizeof(uuid_t
) * valid_netagent_count
,
1972 valid_netagent_count
++;
1976 *count
= valid_netagent_count
;
1981 #define IF_MAXAGENTS 64
1982 #define IF_AGENT_INCREMENT 8
1984 if_add_netagent_locked(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
1986 uuid_t
*first_empty_slot
= NULL
;
1987 u_int32_t index
= 0;
1988 bool already_added
= FALSE
;
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_compare(*netagent_uuid
, new_agent_uuid
) == 0) {
1994 /* Already present, ignore */
1995 already_added
= TRUE
;
1998 if (first_empty_slot
== NULL
&&
1999 uuid_is_null(*netagent_uuid
)) {
2000 first_empty_slot
= netagent_uuid
;
2004 if (already_added
) {
2005 /* Already added agent, don't return an error */
2008 if (first_empty_slot
== NULL
) {
2009 if (ifp
->if_agentcount
>= IF_MAXAGENTS
) {
2010 /* No room for another netagent UUID, bail */
2013 /* Calculate new array size */
2014 u_int32_t new_agent_count
=
2015 MIN(ifp
->if_agentcount
+ IF_AGENT_INCREMENT
,
2018 /* Reallocate array */
2019 uuid_t
*new_agent_array
= _REALLOC(ifp
->if_agentids
,
2020 sizeof(uuid_t
) * new_agent_count
, M_NETAGENT
,
2022 if (new_agent_array
== NULL
) {
2026 /* Save new array */
2027 ifp
->if_agentids
= new_agent_array
;
2029 /* Set first empty slot */
2031 &(ifp
->if_agentids
[ifp
->if_agentcount
]);
2033 /* Save new array length */
2034 ifp
->if_agentcount
= new_agent_count
;
2037 uuid_copy(*first_empty_slot
, new_agent_uuid
);
2038 netagent_post_updated_interfaces(new_agent_uuid
);
2043 if_add_netagent(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
2045 VERIFY(ifp
!= NULL
);
2047 ifnet_lock_exclusive(ifp
);
2049 int error
= if_add_netagent_locked(ifp
, new_agent_uuid
);
2051 ifnet_lock_done(ifp
);
2057 if_delete_netagent_locked(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2059 u_int32_t index
= 0;
2060 bool removed_agent_id
= FALSE
;
2062 if (ifp
->if_agentids
!= NULL
) {
2063 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2064 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2065 if (uuid_compare(*netagent_uuid
,
2066 remove_agent_uuid
) == 0) {
2067 uuid_clear(*netagent_uuid
);
2068 removed_agent_id
= TRUE
;
2073 if (removed_agent_id
) {
2074 netagent_post_updated_interfaces(remove_agent_uuid
);
2081 if_delete_netagent(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2083 VERIFY(ifp
!= NULL
);
2085 ifnet_lock_exclusive(ifp
);
2087 int error
= if_delete_netagent_locked(ifp
, remove_agent_uuid
);
2089 ifnet_lock_done(ifp
);
2095 if_check_netagent(struct ifnet
*ifp
, uuid_t find_agent_uuid
)
2097 boolean_t found
= FALSE
;
2099 if (!ifp
|| uuid_is_null(find_agent_uuid
)) {
2103 ifnet_lock_shared(ifp
);
2105 if (ifp
->if_agentids
!= NULL
) {
2106 for (uint32_t index
= 0; index
< ifp
->if_agentcount
; index
++) {
2107 if (uuid_compare(ifp
->if_agentids
[index
], find_agent_uuid
) == 0) {
2114 ifnet_lock_done(ifp
);
2119 static __attribute__((noinline
)) int
2120 ifioctl_netagent(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
2122 struct if_agentidreq
*ifar
= (struct if_agentidreq
*)(void *)data
;
2124 struct if_agentidsreq32 s32
;
2125 struct if_agentidsreq64 s64
;
2129 VERIFY(ifp
!= NULL
);
2131 /* Get an io ref count if the interface is attached */
2132 if (!ifnet_is_attached(ifp
, 1)) {
2136 if (cmd
== SIOCAIFAGENTID
||
2137 cmd
== SIOCDIFAGENTID
) {
2138 ifnet_lock_exclusive(ifp
);
2140 ifnet_lock_shared(ifp
);
2144 case SIOCAIFAGENTID
: { /* struct if_agentidreq */
2145 // TODO: Use priv_check_cred() instead of root check
2146 if ((error
= proc_suser(p
)) != 0) {
2149 error
= if_add_netagent_locked(ifp
, ifar
->ifar_uuid
);
2152 case SIOCDIFAGENTID
: { /* struct if_agentidreq */
2153 // TODO: Use priv_check_cred() instead of root check
2154 if ((error
= proc_suser(p
)) != 0) {
2157 error
= if_delete_netagent_locked(ifp
, ifar
->ifar_uuid
);
2160 case SIOCGIFAGENTIDS32
: { /* struct if_agentidsreq32 */
2161 bcopy(data
, &u
.s32
, sizeof(u
.s32
));
2162 error
= ifioctl_getnetagents(ifp
, &u
.s32
.ifar_count
,
2165 bcopy(&u
.s32
, data
, sizeof(u
.s32
));
2169 case SIOCGIFAGENTIDS64
: { /* struct if_agentidsreq64 */
2170 bcopy(data
, &u
.s64
, sizeof(u
.s64
));
2171 error
= ifioctl_getnetagents(ifp
, &u
.s64
.ifar_count
,
2174 bcopy(&u
.s64
, data
, sizeof(u
.s64
));
2183 ifnet_lock_done(ifp
);
2184 ifnet_decr_iorefcnt(ifp
);
2190 ifnet_clear_netagent(uuid_t netagent_uuid
)
2192 struct ifnet
*ifp
= NULL
;
2193 u_int32_t index
= 0;
2195 ifnet_head_lock_shared();
2197 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2198 ifnet_lock_shared(ifp
);
2199 if (ifp
->if_agentids
!= NULL
) {
2200 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2201 uuid_t
*ifp_netagent_uuid
= &(ifp
->if_agentids
[index
]);
2202 if (uuid_compare(*ifp_netagent_uuid
, netagent_uuid
) == 0) {
2203 uuid_clear(*ifp_netagent_uuid
);
2207 ifnet_lock_done(ifp
);
2214 ifnet_increment_generation(ifnet_t interface
)
2216 OSIncrementAtomic(&interface
->if_generation
);
2220 ifnet_get_generation(ifnet_t interface
)
2222 return interface
->if_generation
;
2226 ifnet_remove_from_ordered_list(struct ifnet
*ifp
)
2228 ifnet_head_assert_exclusive();
2231 TAILQ_REMOVE(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2232 ifp
->if_ordered_link
.tqe_next
= NULL
;
2233 ifp
->if_ordered_link
.tqe_prev
= NULL
;
2235 // Update ordered count
2236 VERIFY(if_ordered_count
> 0);
2241 ifnet_reset_order(u_int32_t
*ordered_indices
, u_int32_t count
)
2243 struct ifnet
*ifp
= NULL
;
2246 ifnet_head_lock_exclusive();
2247 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2248 if (ordered_indices
[order_index
] == IFSCOPE_NONE
||
2249 ordered_indices
[order_index
] > (uint32_t)if_index
) {
2255 // Flush current ordered list
2256 for (ifp
= TAILQ_FIRST(&ifnet_ordered_head
); ifp
!= NULL
;
2257 ifp
= TAILQ_FIRST(&ifnet_ordered_head
)) {
2258 ifnet_lock_exclusive(ifp
);
2259 ifnet_remove_from_ordered_list(ifp
);
2260 ifnet_lock_done(ifp
);
2263 VERIFY(if_ordered_count
== 0);
2265 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2266 u_int32_t interface_index
= ordered_indices
[order_index
];
2267 ifp
= ifindex2ifnet
[interface_index
];
2271 ifnet_lock_exclusive(ifp
);
2272 TAILQ_INSERT_TAIL(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2273 ifnet_lock_done(ifp
);
2279 necp_update_all_clients();
2285 if_set_qosmarking_mode(struct ifnet
*ifp
, u_int32_t mode
)
2288 u_int32_t old_mode
= ifp
->if_qosmarking_mode
;
2291 case IFRTYPE_QOSMARKING_MODE_NONE
:
2292 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
2293 ifp
->if_eflags
&= ~IFEF_QOSMARKING_CAPABLE
;
2295 case IFRTYPE_QOSMARKING_FASTLANE
:
2296 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_FASTLANE
;
2297 ifp
->if_eflags
|= IFEF_QOSMARKING_CAPABLE
;
2298 if (net_qos_policy_capable_enabled
!= 0) {
2299 ifp
->if_eflags
|= IFEF_QOSMARKING_ENABLED
;
2306 if (error
== 0 && old_mode
!= ifp
->if_qosmarking_mode
) {
2307 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_QOS_MODE_CHANGED
,
2308 NULL
, sizeof(struct kev_dl_rrc_state
));
2313 static __attribute__((noinline
)) int
2314 ifioctl_iforder(u_long cmd
, caddr_t data
)
2317 u_int32_t
*ordered_indices
= NULL
;
2323 case SIOCSIFORDER
: { /* struct if_order */
2324 struct if_order
*ifo
= (struct if_order
*)(void *)data
;
2326 if (ifo
->ifo_count
> (u_int32_t
)if_index
) {
2331 size_t length
= (ifo
->ifo_count
* sizeof(u_int32_t
));
2333 if (ifo
->ifo_ordered_indices
== USER_ADDR_NULL
) {
2337 ordered_indices
= _MALLOC(length
, M_NECP
, M_WAITOK
);
2338 if (ordered_indices
== NULL
) {
2343 error
= copyin(ifo
->ifo_ordered_indices
,
2344 ordered_indices
, length
);
2349 /* ordered_indices should not contain duplicates */
2350 bool found_duplicate
= FALSE
;
2351 for (uint32_t i
= 0; i
< (ifo
->ifo_count
- 1) && !found_duplicate
; i
++) {
2352 for (uint32_t j
= i
+ 1; j
< ifo
->ifo_count
&& !found_duplicate
; j
++) {
2353 if (ordered_indices
[j
] == ordered_indices
[i
]) {
2355 found_duplicate
= TRUE
;
2360 if (found_duplicate
) {
2365 error
= ifnet_reset_order(ordered_indices
, ifo
->ifo_count
);
2376 if (ordered_indices
!= NULL
) {
2377 _FREE(ordered_indices
, M_NECP
);
2383 static __attribute__((noinline
)) int
2384 ifioctl_netsignature(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2386 struct if_nsreq
*ifnsr
= (struct if_nsreq
*)(void *)data
;
2390 VERIFY(ifp
!= NULL
);
2393 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2394 if (ifnsr
->ifnsr_len
> sizeof(ifnsr
->ifnsr_data
)) {
2398 bcopy(&ifnsr
->ifnsr_flags
, &flags
, sizeof(flags
));
2399 error
= ifnet_set_netsignature(ifp
, ifnsr
->ifnsr_family
,
2400 ifnsr
->ifnsr_len
, flags
, ifnsr
->ifnsr_data
);
2403 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2404 ifnsr
->ifnsr_len
= sizeof(ifnsr
->ifnsr_data
);
2405 error
= ifnet_get_netsignature(ifp
, ifnsr
->ifnsr_family
,
2406 &ifnsr
->ifnsr_len
, &flags
, ifnsr
->ifnsr_data
);
2408 bcopy(&flags
, &ifnsr
->ifnsr_flags
, sizeof(flags
));
2410 ifnsr
->ifnsr_len
= 0;
2423 static __attribute__((noinline
)) int
2424 ifioctl_nat64prefix(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2426 struct if_nat64req
*ifnat64
= (struct if_nat64req
*)(void *)data
;
2429 VERIFY(ifp
!= NULL
);
2432 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
2433 error
= ifnet_set_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2435 ip6stat
.ip6s_clat464_plat64_pfx_setfail
++;
2439 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
2440 error
= ifnet_get_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2442 ip6stat
.ip6s_clat464_plat64_pfx_getfail
++;
2454 static __attribute__((noinline
)) int
2455 ifioctl_clat46addr(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2457 struct if_clat46req
*ifclat46
= (struct if_clat46req
*)(void *)data
;
2458 struct in6_ifaddr
*ia6_clat
= NULL
;
2461 VERIFY(ifp
!= NULL
);
2464 case SIOCGIFCLAT46ADDR
:
2465 ia6_clat
= in6ifa_ifpwithflag(ifp
, IN6_IFF_CLAT46
);
2466 if (ia6_clat
== NULL
) {
2471 bcopy(&ia6_clat
->ia_addr
.sin6_addr
, &ifclat46
->ifclat46_addr
.v6_address
,
2472 sizeof(ifclat46
->ifclat46_addr
.v6_address
));
2473 ifclat46
->ifclat46_addr
.v6_prefixlen
= ia6_clat
->ia_plen
;
2474 IFA_REMREF(&ia6_clat
->ia_ifa
);
2487 ifioctl_get_protolist(struct ifnet
*ifp
, u_int32_t
* ret_count
,
2490 u_int32_t actual_count
;
2493 u_int32_t
*list
= NULL
;
2495 /* find out how many */
2496 count
= if_get_protolist(ifp
, NULL
, 0);
2497 if (ifpl
== USER_ADDR_NULL
) {
2501 /* copy out how many there's space for */
2502 if (*ret_count
< count
) {
2508 list
= _MALLOC(count
* sizeof(*list
), M_TEMP
, M_WAITOK
| M_ZERO
);
2513 actual_count
= if_get_protolist(ifp
, list
, count
);
2514 if (actual_count
< count
) {
2515 count
= actual_count
;
2518 error
= copyout((caddr_t
)list
, ifpl
, count
* sizeof(*list
));
2523 if_free_protolist(list
);
2529 static __attribute__((noinline
)) int
2530 ifioctl_protolist(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2535 case SIOCGIFPROTOLIST32
: { /* struct if_protolistreq32 */
2536 struct if_protolistreq32 ifpl
;
2538 bcopy(data
, &ifpl
, sizeof(ifpl
));
2539 if (ifpl
.ifpl_reserved
!= 0) {
2543 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2544 CAST_USER_ADDR_T(ifpl
.ifpl_list
));
2545 bcopy(&ifpl
, data
, sizeof(ifpl
));
2548 case SIOCGIFPROTOLIST64
: { /* struct if_protolistreq64 */
2549 struct if_protolistreq64 ifpl
;
2551 bcopy(data
, &ifpl
, sizeof(ifpl
));
2552 if (ifpl
.ifpl_reserved
!= 0) {
2556 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2558 bcopy(&ifpl
, data
, sizeof(ifpl
));
2570 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2573 ifioctl_restrict_intcoproc(unsigned long cmd
, const char *ifname
,
2574 struct ifnet
*ifp
, struct proc
*p
)
2576 if (intcoproc_unrestricted
== TRUE
) {
2579 if (proc_pid(p
) == 0) {
2583 ifp
= ifunit(ifname
);
2588 if (!IFNET_IS_INTCOPROC(ifp
)) {
2592 case SIOCGIFBRDADDR
:
2603 case SIOCGIFFUNCTIONALTYPE
:
2604 case SIOCGIFPSRCADDR
:
2605 case SIOCGIFPDSTADDR
:
2606 case SIOCGIFGENERIC
:
2610 case SIOCGIFWAKEFLAGS
:
2611 case SIOCGIFGETRTREFCNT
:
2612 case SIOCGIFOPPORTUNISTIC
:
2613 case SIOCGIFLINKQUALITYMETRIC
:
2615 case SIOCGIFDELEGATE
:
2616 case SIOCGIFEXPENSIVE
:
2617 case SIOCGIFINTERFACESTATE
:
2618 case SIOCGIFPROBECONNECTIVITY
:
2619 case SIOCGIFTIMESTAMPENABLED
:
2621 case SIOCGQOSMARKINGMODE
:
2622 case SIOCGQOSMARKINGENABLED
:
2623 case SIOCGIFLOWINTERNET
:
2625 case SIOCGIFMEDIA32
:
2626 case SIOCGIFMEDIA64
:
2627 case SIOCGIFXMEDIA32
:
2628 case SIOCGIFXMEDIA64
:
2630 case SIOCGIFLINKPARAMS
:
2631 case SIOCGIFQUEUESTATS
:
2632 case SIOCGIFTHROTTLE
:
2633 case SIOCGIFAGENTIDS32
:
2634 case SIOCGIFAGENTIDS64
:
2635 case SIOCGIFNETSIGNATURE
:
2636 case SIOCGIFINFO_IN6
:
2637 case SIOCGIFAFLAG_IN6
:
2638 case SIOCGNBRINFO_IN6
:
2639 case SIOCGIFALIFETIME_IN6
:
2640 case SIOCGIFNETMASK_IN6
:
2641 case SIOCGIFPROTOLIST32
:
2642 case SIOCGIFPROTOLIST64
:
2645 #if (DEBUG || DEVELOPMENT)
2646 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2647 __func__
, cmd
, proc_pid(p
));
2655 * Given a media word, return one suitable for an application
2656 * using the original encoding.
2659 compat_media(int media
)
2661 if (IFM_TYPE(media
) == IFM_ETHER
&& IFM_SUBTYPE(media
) > IFM_OTHER
) {
2662 media
&= ~IFM_TMASK
;
2669 compat_ifmu_ulist(struct ifnet
*ifp
, u_long cmd
, void *data
)
2671 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2672 user_addr_t user_addr
;
2674 int *media_list
= NULL
;
2676 bool list_modified
= false;
2678 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2679 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2680 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2681 if (user_addr
== USER_ADDR_NULL
|| ifmr
->ifm_count
== 0) {
2684 MALLOC(media_list
, int *, ifmr
->ifm_count
* sizeof(int),
2685 M_TEMP
, M_WAITOK
| M_ZERO
);
2686 if (media_list
== NULL
) {
2687 os_log_error(OS_LOG_DEFAULT
,
2688 "%s: %s MALLOC() failed",
2689 __func__
, ifp
->if_xname
);
2693 error
= copyin(user_addr
, media_list
, ifmr
->ifm_count
* sizeof(int));
2695 os_log_error(OS_LOG_DEFAULT
,
2696 "%s: %s copyin() error %d",
2697 __func__
, ifp
->if_xname
, error
);
2700 for (i
= 0; i
< ifmr
->ifm_count
; i
++) {
2701 int old_media
, new_media
;
2703 old_media
= media_list
[i
];
2705 new_media
= compat_media(old_media
);
2706 if (new_media
== old_media
) {
2709 if (if_verbose
!= 0) {
2710 os_log_info(OS_LOG_DEFAULT
,
2711 "%s: %s converted extended media %08x to compat media %08x",
2712 __func__
, ifp
->if_xname
, old_media
, new_media
);
2714 media_list
[i
] = new_media
;
2715 list_modified
= true;
2717 if (list_modified
) {
2718 error
= copyout(media_list
, user_addr
, ifmr
->ifm_count
* sizeof(int));
2720 os_log_error(OS_LOG_DEFAULT
,
2721 "%s: %s copyout() error %d",
2722 __func__
, ifp
->if_xname
, error
);
2727 if (media_list
!= NULL
) {
2728 FREE(media_list
, M_TEMP
);
2734 compat_ifmediareq(struct ifnet
*ifp
, u_long cmd
, void *data
)
2736 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2739 ifmr
->ifm_active
= compat_media(ifmr
->ifm_active
);
2740 ifmr
->ifm_current
= compat_media(ifmr
->ifm_current
);
2742 error
= compat_ifmu_ulist(ifp
, cmd
, data
);
2748 ifioctl_get_media(struct ifnet
*ifp
, struct socket
*so
, u_long cmd
, caddr_t data
)
2753 * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2754 * media subtypes macros from <net/if_media.h>
2757 case SIOCGIFMEDIA32
:
2758 case SIOCGIFXMEDIA32
:
2759 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA32
, data
);
2761 case SIOCGIFMEDIA64
:
2762 case SIOCGIFXMEDIA64
:
2763 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA64
, data
);
2766 if (if_verbose
!= 0 && error
!= 0) {
2767 os_log(OS_LOG_DEFAULT
, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2768 __func__
, ifp
->if_xname
, cmd
, error
);
2770 if (error
== 0 && (cmd
== SIOCGIFMEDIA32
|| cmd
== SIOCGIFMEDIA64
)) {
2771 error
= compat_ifmediareq(ifp
, cmd
, data
);
2778 * Most of the routines called to handle the ioctls would end up being
2779 * tail-call optimized, which unfortunately causes this routine to
2780 * consume too much stack space; this is the reason for the "noinline"
2781 * attribute used on those routines.
2784 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2786 char ifname
[IFNAMSIZ
+ 1];
2787 struct ifnet
*ifp
= NULL
;
2788 struct ifstat
*ifs
= NULL
;
2791 bzero(ifname
, sizeof(ifname
));
2794 * ioctls which don't require ifp, or ifreq ioctls
2797 case OSIOCGIFCONF32
: /* struct ifconf32 */
2798 case SIOCGIFCONF32
: /* struct ifconf32 */
2799 case SIOCGIFCONF64
: /* struct ifconf64 */
2800 case OSIOCGIFCONF64
: /* struct ifconf64 */
2801 error
= ifioctl_ifconf(cmd
, data
);
2804 case SIOCIFGCLONERS32
: /* struct if_clonereq32 */
2805 case SIOCIFGCLONERS64
: /* struct if_clonereq64 */
2806 error
= ifioctl_ifclone(cmd
, data
);
2809 case SIOCGIFAGENTDATA32
: /* struct netagent_req32 */
2810 case SIOCGIFAGENTDATA64
: /* struct netagent_req64 */
2811 case SIOCGIFAGENTLIST32
: /* struct netagentlist_req32 */
2812 case SIOCGIFAGENTLIST64
: /* struct netagentlist_req64 */
2813 error
= netagent_ioctl(cmd
, data
);
2816 case SIOCSIFORDER
: /* struct if_order */
2817 error
= ifioctl_iforder(cmd
, data
);
2820 case SIOCSIFDSTADDR
: /* struct ifreq */
2821 case SIOCSIFADDR
: /* struct ifreq */
2822 case SIOCSIFBRDADDR
: /* struct ifreq */
2823 case SIOCSIFNETMASK
: /* struct ifreq */
2824 case OSIOCGIFADDR
: /* struct ifreq */
2825 case OSIOCGIFDSTADDR
: /* struct ifreq */
2826 case OSIOCGIFBRDADDR
: /* struct ifreq */
2827 case OSIOCGIFNETMASK
: /* struct ifreq */
2828 case SIOCSIFKPI
: /* struct ifreq */
2829 if (so
->so_proto
== NULL
) {
2834 case SIOCIFCREATE
: /* struct ifreq */
2835 case SIOCIFCREATE2
: /* struct ifreq */
2836 case SIOCIFDESTROY
: /* struct ifreq */
2837 case SIOCGIFFLAGS
: /* struct ifreq */
2838 case SIOCGIFEFLAGS
: /* struct ifreq */
2839 case SIOCGIFCAP
: /* struct ifreq */
2841 case SIOCGIFMAC
: /* struct ifreq */
2842 case SIOCSIFMAC
: /* struct ifreq */
2843 #endif /* CONFIG_MACF_NET */
2844 case SIOCGIFMETRIC
: /* struct ifreq */
2845 case SIOCGIFMTU
: /* struct ifreq */
2846 case SIOCGIFPHYS
: /* struct ifreq */
2847 case SIOCSIFFLAGS
: /* struct ifreq */
2848 case SIOCSIFCAP
: /* struct ifreq */
2849 case SIOCSIFMETRIC
: /* struct ifreq */
2850 case SIOCSIFPHYS
: /* struct ifreq */
2851 case SIOCSIFMTU
: /* struct ifreq */
2852 case SIOCADDMULTI
: /* struct ifreq */
2853 case SIOCDELMULTI
: /* struct ifreq */
2854 case SIOCDIFPHYADDR
: /* struct ifreq */
2855 case SIOCSIFMEDIA
: /* struct ifreq */
2856 case SIOCSIFGENERIC
: /* struct ifreq */
2857 case SIOCSIFLLADDR
: /* struct ifreq */
2858 case SIOCSIFALTMTU
: /* struct ifreq */
2859 case SIOCSIFVLAN
: /* struct ifreq */
2860 case SIOCSIFBOND
: /* struct ifreq */
2861 case SIOCGIFLLADDR
: /* struct ifreq */
2862 case SIOCGIFTYPE
: /* struct ifreq */
2863 case SIOCGIFFUNCTIONALTYPE
: /* struct ifreq */
2864 case SIOCGIFPSRCADDR
: /* struct ifreq */
2865 case SIOCGIFPDSTADDR
: /* struct ifreq */
2866 case SIOCGIFGENERIC
: /* struct ifreq */
2867 case SIOCGIFDEVMTU
: /* struct ifreq */
2868 case SIOCGIFVLAN
: /* struct ifreq */
2869 case SIOCGIFBOND
: /* struct ifreq */
2870 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
2871 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
2872 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
2873 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
2874 case SIOCGIFLINKQUALITYMETRIC
: /* struct ifreq */
2875 case SIOCSIFLOG
: /* struct ifreq */
2876 case SIOCGIFLOG
: /* struct ifreq */
2877 case SIOCGIFDELEGATE
: /* struct ifreq */
2878 case SIOCGIFEXPENSIVE
: /* struct ifreq */
2879 case SIOCSIFEXPENSIVE
: /* struct ifreq */
2880 case SIOCSIF2KCL
: /* struct ifreq */
2881 case SIOCGIF2KCL
: /* struct ifreq */
2882 case SIOCSIFINTERFACESTATE
: /* struct ifreq */
2883 case SIOCGIFINTERFACESTATE
: /* struct ifreq */
2884 case SIOCSIFPROBECONNECTIVITY
: /* struct ifreq */
2885 case SIOCGIFPROBECONNECTIVITY
: /* struct ifreq */
2886 case SIOCGSTARTDELAY
: /* struct ifreq */
2887 case SIOCSIFTIMESTAMPENABLE
: /* struct ifreq */
2888 case SIOCSIFTIMESTAMPDISABLE
: /* struct ifreq */
2889 case SIOCGIFTIMESTAMPENABLED
: /* struct ifreq */
2890 #if (DEBUG || DEVELOPMENT)
2891 case SIOCSIFDISABLEOUTPUT
: /* struct ifreq */
2892 #endif /* (DEBUG || DEVELOPMENT) */
2893 case SIOCGECNMODE
: /* struct ifreq */
2895 case SIOCSQOSMARKINGMODE
: /* struct ifreq */
2896 case SIOCSQOSMARKINGENABLED
: /* struct ifreq */
2897 case SIOCGQOSMARKINGMODE
: /* struct ifreq */
2898 case SIOCGQOSMARKINGENABLED
: /* struct ifreq */
2899 case SIOCSIFLOWINTERNET
: /* struct ifreq */
2900 case SIOCGIFLOWINTERNET
: /* struct ifreq */
2901 case SIOCGIFLOWPOWER
: /* struct ifreq */
2902 case SIOCSIFLOWPOWER
: /* struct ifreq */
2903 { /* struct ifreq */
2905 bcopy(data
, &ifr
, sizeof(ifr
));
2906 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2907 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2908 if (ifioctl_restrict_intcoproc(cmd
, ifname
, NULL
, p
) == true) {
2912 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2913 bcopy(&ifr
, data
, sizeof(ifr
));
2919 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2920 * here to ensure that the ifnet, if found, has been fully attached.
2924 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2925 bcopy(((struct in_aliasreq
*)(void *)data
)->ifra_name
,
2927 ifp
= ifunit(ifname
);
2931 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2932 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
2934 ifp
= ifunit(ifname
);
2937 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2938 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
2940 ifp
= ifunit(ifname
);
2944 case SIOCGIFSTATUS
: /* struct ifstat */
2945 ifs
= _MALLOC(sizeof(*ifs
), M_DEVBUF
, M_WAITOK
);
2951 bcopy(data
, ifs
, sizeof(*ifs
));
2952 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
2953 bcopy(ifs
->ifs_name
, ifname
, IFNAMSIZ
);
2954 ifp
= ifunit(ifname
);
2957 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2958 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
2959 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
2961 ifp
= ifunit(ifname
);
2964 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
2965 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
2966 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
2968 ifp
= ifunit(ifname
);
2971 case SIOCSIFDESC
: /* struct if_descreq */
2972 case SIOCGIFDESC
: /* struct if_descreq */
2973 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
2975 ifp
= ifunit(ifname
);
2978 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
2979 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
2980 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
2982 ifp
= ifunit(ifname
);
2985 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
2986 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
2988 ifp
= ifunit(ifname
);
2991 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
2992 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
2993 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
2995 ifp
= ifunit(ifname
);
2998 case SIOCAIFAGENTID
: /* struct if_agentidreq */
2999 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3000 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3001 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3002 bcopy(((struct if_agentidreq
*)(void *)data
)->ifar_name
,
3004 ifp
= ifunit(ifname
);
3007 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3008 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3009 bcopy(((struct if_nsreq
*)(void *)data
)->ifnsr_name
,
3011 ifp
= ifunit(ifname
);
3014 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3015 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3016 bcopy(((struct if_protolistreq
*)(void *)data
)->ifpl_name
,
3018 ifp
= ifunit(ifname
);
3022 * This is a bad assumption, but the code seems to
3023 * have been doing this in the past; caveat emptor.
3025 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
3027 ifp
= ifunit(ifname
);
3037 if (ifioctl_restrict_intcoproc(cmd
, NULL
, ifp
, p
) == true) {
3042 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
3044 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
3045 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
3047 error
= proc_suser(p
);
3052 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3057 ifnet_touch_lastchange(ifp
);
3060 case SIOCGIFSTATUS
: /* struct ifstat */
3061 VERIFY(ifs
!= NULL
);
3062 ifs
->ascii
[0] = '\0';
3064 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifs
);
3066 bcopy(ifs
, data
, sizeof(*ifs
));
3069 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
3070 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
3071 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
3072 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
3073 error
= ifioctl_get_media(ifp
, so
, cmd
, data
);
3076 case SIOCSIFDESC
: /* struct if_descreq */
3077 case SIOCGIFDESC
: /* struct if_descreq */
3078 error
= ifioctl_ifdesc(ifp
, cmd
, data
, p
);
3081 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
3082 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
3083 error
= ifioctl_linkparams(ifp
, cmd
, data
, p
);
3086 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
3087 error
= ifioctl_qstats(ifp
, cmd
, data
);
3090 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
3091 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
3092 error
= ifioctl_throttle(ifp
, cmd
, data
, p
);
3095 case SIOCAIFAGENTID
: /* struct if_agentidreq */
3096 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3097 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3098 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3099 error
= ifioctl_netagent(ifp
, cmd
, data
, p
);
3102 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3103 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3104 error
= ifioctl_netsignature(ifp
, cmd
, data
);
3108 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
3109 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
3110 error
= ifioctl_nat64prefix(ifp
, cmd
, data
);
3113 case SIOCGIFCLAT46ADDR
: /* struct if_clat46req */
3114 error
= ifioctl_clat46addr(ifp
, cmd
, data
);
3118 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3119 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3120 error
= ifioctl_protolist(ifp
, cmd
, data
);
3124 if (so
->so_proto
== NULL
) {
3130 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3132 socket_unlock(so
, 1);
3134 // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3135 // ifreq as the code expects ifaddr
3136 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3137 !(cmd
== SIOCAIFADDR
|| cmd
== SIOCDIFADDR
)) {
3138 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3145 _FREE(ifs
, M_DEVBUF
);
3149 if (ifname
[0] == '\0') {
3150 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3152 } else if (ifp
!= NULL
) {
3153 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3158 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3159 "%c %lu) error %d\n", __func__
,
3160 proc_name_address(p
), proc_pid(p
),
3161 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3162 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3163 (char)IOCGROUP(cmd
), cmd
& 0xff, error
);
3164 } else if (if_verbose
> 1) {
3165 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3166 "%c %lu) OK\n", __func__
,
3167 proc_name_address(p
), proc_pid(p
),
3168 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3169 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3170 (char)IOCGROUP(cmd
), cmd
& 0xff);
3177 static __attribute__((noinline
)) int
3178 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
3183 struct kev_msg ev_msg
;
3184 struct net_event_data ev_data
;
3186 bzero(&ev_data
, sizeof(struct net_event_data
));
3187 bzero(&ev_msg
, sizeof(struct kev_msg
));
3192 error
= proc_suser(p
);
3196 return if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
3197 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
);
3199 error
= proc_suser(p
);
3203 return if_clone_destroy(ifr
->ifr_name
);
3207 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
3208 * here to ensure that the ifnet, if found, has been fully attached.
3211 ifp
= ifunit(ifr
->ifr_name
);
3220 ifnet_lock_shared(ifp
);
3221 ifr
->ifr_flags
= ifp
->if_flags
;
3222 ifnet_lock_done(ifp
);
3226 ifnet_lock_shared(ifp
);
3227 ifr
->ifr_eflags
= ifp
->if_eflags
;
3228 ifnet_lock_done(ifp
);
3232 ifnet_lock_shared(ifp
);
3233 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
3234 ifr
->ifr_curcap
= ifp
->if_capenable
;
3235 ifnet_lock_done(ifp
);
3240 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
3244 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
3246 #endif /* CONFIG_MACF_NET */
3249 ifnet_lock_shared(ifp
);
3250 ifr
->ifr_metric
= ifp
->if_metric
;
3251 ifnet_lock_done(ifp
);
3255 ifnet_lock_shared(ifp
);
3256 ifr
->ifr_mtu
= ifp
->if_mtu
;
3257 ifnet_lock_done(ifp
);
3261 ifnet_lock_shared(ifp
);
3262 ifr
->ifr_phys
= ifp
->if_physical
;
3263 ifnet_lock_done(ifp
);
3267 error
= proc_suser(p
);
3272 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
3273 (u_int16_t
)~IFF_CANTCHANGE
);
3276 * Note that we intentionally ignore any error from below
3277 * for the SIOCSIFFLAGS case.
3279 (void) ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3282 * Send the event even upon error from the driver because
3283 * we changed the flags.
3285 dlil_post_sifflags_msg(ifp
);
3287 ifnet_touch_lastchange(ifp
);
3291 error
= proc_suser(p
);
3296 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
3300 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3302 ifnet_touch_lastchange(ifp
);
3306 error
= proc_suser(p
);
3311 ifp
->if_metric
= ifr
->ifr_metric
;
3313 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3314 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3315 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3317 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
3318 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3319 ev_data
.if_family
= ifp
->if_family
;
3320 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3321 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3322 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3324 ev_msg
.dv
[1].data_length
= 0;
3325 dlil_post_complete_msg(ifp
, &ev_msg
);
3327 ifnet_touch_lastchange(ifp
);
3331 error
= proc_suser(p
);
3336 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3341 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3342 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3343 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3345 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
3346 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3347 ev_data
.if_family
= ifp
->if_family
;
3348 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3349 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3350 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3351 ev_msg
.dv
[1].data_length
= 0;
3352 dlil_post_complete_msg(ifp
, &ev_msg
);
3354 ifnet_touch_lastchange(ifp
);
3358 u_int32_t oldmtu
= ifp
->if_mtu
;
3359 struct ifclassq
*ifq
= &ifp
->if_snd
;
3361 error
= proc_suser(p
);
3366 if (ifp
->if_ioctl
== NULL
) {
3370 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
3374 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3379 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3380 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3381 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3383 ev_msg
.event_code
= KEV_DL_SIFMTU
;
3384 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3385 ev_data
.if_family
= ifp
->if_family
;
3386 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3387 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3388 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3389 ev_msg
.dv
[1].data_length
= 0;
3390 dlil_post_complete_msg(ifp
, &ev_msg
);
3392 ifnet_touch_lastchange(ifp
);
3396 * If the link MTU changed, do network layer specific procedure
3397 * and update all route entries associated with the interface,
3398 * so that their MTU metric gets updated.
3400 if (ifp
->if_mtu
!= oldmtu
) {
3401 if_rtmtu_update(ifp
);
3405 /* Inform all transmit queues about the new MTU */
3407 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
3415 error
= proc_suser(p
);
3420 /* Don't allow group membership on non-multicast interfaces. */
3421 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
3426 /* Don't let users screw up protocols' entries. */
3427 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
3428 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
3434 * User is permitted to anonymously join a particular link
3435 * multicast group via SIOCADDMULTI. Subsequent join requested
3436 * for the same record which has an outstanding refcnt from a
3437 * past if_addmulti_anon() will not result in EADDRINUSE error
3438 * (unlike other BSDs.) Anonymously leaving a group is also
3439 * allowed only as long as there is an outstanding refcnt held
3440 * by a previous anonymous request, or else ENOENT (even if the
3441 * link-layer multicast membership exists for a network-layer
3444 if (cmd
== SIOCADDMULTI
) {
3445 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
3446 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
3448 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
3449 ev_msg
.event_code
= KEV_DL_DELMULTI
;
3455 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3456 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3457 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3458 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3460 ev_data
.if_family
= ifp
->if_family
;
3461 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3462 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3463 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3464 ev_msg
.dv
[1].data_length
= 0;
3465 dlil_post_complete_msg(ifp
, &ev_msg
);
3467 ifnet_touch_lastchange(ifp
);
3471 error
= proc_suser(p
);
3476 * Silently ignore setting IFM_OTHER
3478 if (ifr
->ifr_media
== IFM_OTHER
) {
3479 os_log_info(OS_LOG_DEFAULT
,
3480 "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3481 __func__
, ifp
->if_xname
);
3485 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3489 ifnet_touch_lastchange(ifp
);
3492 case SIOCDIFPHYADDR
:
3493 case SIOCSIFGENERIC
:
3498 error
= proc_suser(p
);
3503 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3508 ifnet_touch_lastchange(ifp
);
3511 case SIOCGIFLLADDR
: {
3512 struct sockaddr_dl
*sdl
= SDL(ifp
->if_lladdr
->ifa_addr
);
3514 if (sdl
->sdl_alen
== 0) {
3515 error
= EADDRNOTAVAIL
;
3518 /* If larger than 14-bytes we'll need another mechanism */
3519 if (sdl
->sdl_alen
> sizeof(ifr
->ifr_addr
.sa_data
)) {
3523 /* Follow the same convention used by SIOCSIFLLADDR */
3524 bzero(&ifr
->ifr_addr
, sizeof(ifr
->ifr_addr
));
3525 ifr
->ifr_addr
.sa_family
= AF_LINK
;
3526 ifr
->ifr_addr
.sa_len
= sdl
->sdl_alen
;
3527 error
= ifnet_guarded_lladdr_copy_bytes(ifp
,
3528 &ifr
->ifr_addr
.sa_data
, sdl
->sdl_alen
);
3533 ifr
->ifr_type
.ift_type
= ifp
->if_type
;
3534 ifr
->ifr_type
.ift_family
= ifp
->if_family
;
3535 ifr
->ifr_type
.ift_subfamily
= ifp
->if_subfamily
;
3538 case SIOCGIFFUNCTIONALTYPE
:
3539 ifr
->ifr_functional_type
= if_functional_type(ifp
, FALSE
);
3542 case SIOCGIFPSRCADDR
:
3543 case SIOCGIFPDSTADDR
:
3544 case SIOCGIFGENERIC
:
3548 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3551 case SIOCGIFWAKEFLAGS
:
3552 ifnet_lock_shared(ifp
);
3553 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
3554 ifnet_lock_done(ifp
);
3557 case SIOCGIFGETRTREFCNT
:
3558 ifnet_lock_shared(ifp
);
3559 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
3560 ifnet_lock_done(ifp
);
3563 case SIOCSIFOPPORTUNISTIC
:
3564 case SIOCGIFOPPORTUNISTIC
:
3565 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
3568 case SIOCGIFLINKQUALITYMETRIC
:
3569 ifnet_lock_shared(ifp
);
3570 if ((ifp
->if_interface_state
.valid_bitmask
&
3571 IF_INTERFACE_STATE_LQM_STATE_VALID
)) {
3572 ifr
->ifr_link_quality_metric
=
3573 ifp
->if_interface_state
.lqm_state
;
3574 } else if (IF_FULLY_ATTACHED(ifp
)) {
3575 ifr
->ifr_link_quality_metric
=
3576 IFNET_LQM_THRESH_UNKNOWN
;
3578 ifr
->ifr_link_quality_metric
=
3579 IFNET_LQM_THRESH_OFF
;
3581 ifnet_lock_done(ifp
);
3586 error
= ifnet_getset_log(ifp
, cmd
, ifr
, p
);
3589 case SIOCGIFDELEGATE
:
3590 ifnet_lock_shared(ifp
);
3591 ifr
->ifr_delegated
= ((ifp
->if_delegated
.ifp
!= NULL
) ?
3592 ifp
->if_delegated
.ifp
->if_index
: 0);
3593 ifnet_lock_done(ifp
);
3596 case SIOCGIFEXPENSIVE
:
3597 ifnet_lock_shared(ifp
);
3598 if (ifp
->if_eflags
& IFEF_EXPENSIVE
) {
3599 ifr
->ifr_expensive
= 1;
3601 ifr
->ifr_expensive
= 0;
3603 ifnet_lock_done(ifp
);
3606 case SIOCSIFEXPENSIVE
:
3610 if ((error
= priv_check_cred(kauth_cred_get(),
3611 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3614 ifnet_lock_exclusive(ifp
);
3615 if (ifr
->ifr_expensive
) {
3616 ifp
->if_eflags
|= IFEF_EXPENSIVE
;
3618 ifp
->if_eflags
&= ~IFEF_EXPENSIVE
;
3620 ifnet_lock_done(ifp
);
3622 * Update the expensive bit in the delegated interface
3625 ifnet_head_lock_shared();
3626 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3627 ifnet_lock_exclusive(difp
);
3628 if (difp
->if_delegated
.ifp
== ifp
) {
3629 difp
->if_delegated
.expensive
=
3630 ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
3632 ifnet_lock_done(difp
);
3639 ifnet_lock_shared(ifp
);
3640 if (ifp
->if_eflags
& IFEF_2KCL
) {
3645 ifnet_lock_done(ifp
);
3649 if ((error
= priv_check_cred(kauth_cred_get(),
3650 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3653 ifnet_lock_exclusive(ifp
);
3654 if (ifr
->ifr_2kcl
) {
3655 ifp
->if_eflags
|= IFEF_2KCL
;
3657 ifp
->if_eflags
&= ~IFEF_2KCL
;
3659 ifnet_lock_done(ifp
);
3661 case SIOCGSTARTDELAY
:
3662 ifnet_lock_shared(ifp
);
3663 if (ifp
->if_eflags
& IFEF_ENQUEUE_MULTI
) {
3664 ifr
->ifr_start_delay_qlen
=
3665 ifp
->if_start_delay_qlen
;
3666 ifr
->ifr_start_delay_timeout
=
3667 ifp
->if_start_delay_timeout
;
3669 ifr
->ifr_start_delay_qlen
= 0;
3670 ifr
->ifr_start_delay_timeout
= 0;
3672 ifnet_lock_done(ifp
);
3674 case SIOCSIFDSTADDR
:
3676 case SIOCSIFBRDADDR
:
3677 case SIOCSIFNETMASK
:
3679 case OSIOCGIFDSTADDR
:
3680 case OSIOCGIFBRDADDR
:
3681 case OSIOCGIFNETMASK
:
3683 VERIFY(so
->so_proto
!= NULL
);
3685 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
3686 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
3687 #if BYTE_ORDER != BIG_ENDIAN
3688 if (ifr
->ifr_addr
.sa_family
== 0 &&
3689 ifr
->ifr_addr
.sa_len
< 16) {
3690 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
3691 ifr
->ifr_addr
.sa_len
= 16;
3694 if (ifr
->ifr_addr
.sa_len
== 0) {
3695 ifr
->ifr_addr
.sa_len
= 16;
3698 } else if (cmd
== OSIOCGIFADDR
) {
3699 cmd
= SIOCGIFADDR
; /* struct ifreq */
3700 } else if (cmd
== OSIOCGIFDSTADDR
) {
3701 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
3702 } else if (cmd
== OSIOCGIFBRDADDR
) {
3703 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
3704 } else if (cmd
== OSIOCGIFNETMASK
) {
3705 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
3709 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3710 (caddr_t
)ifr
, ifp
, p
));
3711 socket_unlock(so
, 1);
3715 case OSIOCGIFDSTADDR
:
3716 case OSIOCGIFBRDADDR
:
3717 case OSIOCGIFNETMASK
:
3718 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
3722 if (cmd
== SIOCSIFKPI
) {
3723 int temperr
= proc_suser(p
);
3728 // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3729 // with ifreq as the code expects ifaddr
3730 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3731 !(cmd
== SIOCSIFADDR
|| cmd
== SIOCSIFDSTADDR
)) {
3732 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3737 case SIOCGIFINTERFACESTATE
:
3738 if_get_state(ifp
, &ifr
->ifr_interface_state
);
3741 case SIOCSIFINTERFACESTATE
:
3742 if ((error
= priv_check_cred(kauth_cred_get(),
3743 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3747 error
= if_state_update(ifp
, &ifr
->ifr_interface_state
);
3750 case SIOCSIFPROBECONNECTIVITY
:
3751 if ((error
= priv_check_cred(kauth_cred_get(),
3752 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3755 error
= if_probe_connectivity(ifp
,
3756 ifr
->ifr_probe_connectivity
);
3758 case SIOCGIFPROBECONNECTIVITY
:
3759 if ((error
= priv_check_cred(kauth_cred_get(),
3760 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3763 if (ifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
) {
3764 ifr
->ifr_probe_connectivity
= 1;
3766 ifr
->ifr_probe_connectivity
= 0;
3770 if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3772 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_ENABLE
;
3773 } else if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3775 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DISABLE
;
3777 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
3781 if ((error
= priv_check_cred(kauth_cred_get(),
3782 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3785 if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DEFAULT
) {
3786 ifp
->if_eflags
&= ~(IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
);
3787 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_ENABLE
) {
3788 ifp
->if_eflags
|= IFEF_ECN_ENABLE
;
3789 ifp
->if_eflags
&= ~IFEF_ECN_DISABLE
;
3790 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DISABLE
) {
3791 ifp
->if_eflags
|= IFEF_ECN_DISABLE
;
3792 ifp
->if_eflags
&= ~IFEF_ECN_ENABLE
;
3797 case SIOCSIFTIMESTAMPENABLE
:
3798 case SIOCSIFTIMESTAMPDISABLE
:
3799 error
= proc_suser(p
);
3804 ifnet_lock_exclusive(ifp
);
3805 if ((cmd
== SIOCSIFTIMESTAMPENABLE
&&
3806 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) ||
3807 (cmd
== SIOCSIFTIMESTAMPDISABLE
&&
3808 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) == 0)) {
3809 ifnet_lock_done(ifp
);
3812 if (cmd
== SIOCSIFTIMESTAMPENABLE
) {
3813 ifp
->if_xflags
|= IFXF_TIMESTAMP_ENABLED
;
3815 ifp
->if_xflags
&= ~IFXF_TIMESTAMP_ENABLED
;
3817 ifnet_lock_done(ifp
);
3819 * Pass the setting to the interface if it supports either
3820 * software or hardware time stamping
3822 if (ifp
->if_capabilities
& (IFCAP_HW_TIMESTAMP
|
3823 IFCAP_SW_TIMESTAMP
)) {
3824 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3828 case SIOCGIFTIMESTAMPENABLED
: {
3829 if ((ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) {
3830 ifr
->ifr_intval
= 1;
3832 ifr
->ifr_intval
= 0;
3836 case SIOCSQOSMARKINGMODE
:
3837 if ((error
= priv_check_cred(kauth_cred_get(),
3838 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3841 error
= if_set_qosmarking_mode(ifp
, ifr
->ifr_qosmarking_mode
);
3844 case SIOCGQOSMARKINGMODE
:
3845 ifr
->ifr_qosmarking_mode
= ifp
->if_qosmarking_mode
;
3848 case SIOCSQOSMARKINGENABLED
:
3849 if ((error
= priv_check_cred(kauth_cred_get(),
3850 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3853 if (ifr
->ifr_qosmarking_enabled
!= 0) {
3854 ifp
->if_eflags
|= IFEF_QOSMARKING_ENABLED
;
3856 ifp
->if_eflags
&= ~IFEF_QOSMARKING_ENABLED
;
3860 case SIOCGQOSMARKINGENABLED
:
3861 ifr
->ifr_qosmarking_enabled
=
3862 (ifp
->if_eflags
& IFEF_QOSMARKING_ENABLED
) ? 1 : 0;
3865 case SIOCSIFDISABLEOUTPUT
:
3866 #if (DEBUG || DEVELOPMENT)
3867 if (ifr
->ifr_disable_output
== 1) {
3868 error
= ifnet_disable_output(ifp
);
3869 } else if (ifr
->ifr_disable_output
== 0) {
3870 error
= ifnet_enable_output(ifp
);
3876 #endif /* (DEBUG || DEVELOPMENT) */
3878 case SIOCSIFLOWINTERNET
:
3879 if ((error
= priv_check_cred(kauth_cred_get(),
3880 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3884 ifnet_lock_exclusive(ifp
);
3885 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_UL
) {
3886 ifp
->if_xflags
|= IFXF_LOW_INTERNET_UL
;
3888 ifp
->if_xflags
&= ~(IFXF_LOW_INTERNET_UL
);
3890 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_DL
) {
3891 ifp
->if_xflags
|= IFXF_LOW_INTERNET_DL
;
3893 ifp
->if_xflags
&= ~(IFXF_LOW_INTERNET_DL
);
3895 ifnet_lock_done(ifp
);
3897 case SIOCGIFLOWINTERNET
:
3898 ifnet_lock_shared(ifp
);
3899 ifr
->ifr_low_internet
= 0;
3900 if (ifp
->if_xflags
& IFXF_LOW_INTERNET_UL
) {
3901 ifr
->ifr_low_internet
|=
3902 IFRTYPE_LOW_INTERNET_ENABLE_UL
;
3904 if (ifp
->if_xflags
& IFXF_LOW_INTERNET_DL
) {
3905 ifr
->ifr_low_internet
|=
3906 IFRTYPE_LOW_INTERNET_ENABLE_DL
;
3908 ifnet_lock_done(ifp
);
3910 case SIOCGIFLOWPOWER
:
3911 ifr
->ifr_low_power_mode
=
3912 !!(ifp
->if_xflags
& IFXF_LOW_POWER
);
3914 case SIOCSIFLOWPOWER
:
3915 #if (DEVELOPMENT || DEBUG)
3916 error
= if_set_low_power(ifp
, !!(ifr
->ifr_low_power_mode
));
3917 #else /* DEVELOPMENT || DEBUG */
3919 #endif /* DEVELOPMENT || DEBUG */
3930 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
3934 socket_unlock(so
, 0);
3935 error
= ifioctl(so
, cmd
, data
, p
);
3941 * Set/clear promiscuous mode on interface ifp based on the truth value
3942 * of pswitch. The calls are reference counted so that only the first
3943 * "on" request actually has an effect, as does the final "off" request.
3944 * Results are undefined if the "off" and "on" requests are not matched.
3947 ifnet_set_promiscuous(
3955 ifnet_lock_exclusive(ifp
);
3956 oldflags
= ifp
->if_flags
;
3957 ifp
->if_pcount
+= pswitch
? 1 : -1;
3959 if (ifp
->if_pcount
> 0) {
3960 ifp
->if_flags
|= IFF_PROMISC
;
3962 ifp
->if_flags
&= ~IFF_PROMISC
;
3965 newflags
= ifp
->if_flags
;
3966 ifnet_lock_done(ifp
);
3968 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
3969 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
3973 ifnet_lock_exclusive(ifp
);
3975 ifp
->if_pcount
-= pswitch
? 1 : -1;
3976 if (ifp
->if_pcount
> 0) {
3977 ifp
->if_flags
|= IFF_PROMISC
;
3979 ifp
->if_flags
&= ~IFF_PROMISC
;
3981 ifnet_lock_done(ifp
);
3985 if (newflags
!= oldflags
) {
3986 log(LOG_INFO
, "%s: promiscuous mode %s%s\n",
3988 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
3989 error
!= 0 ? " failed" : " succeeded");
3995 * Return interface configuration
3996 * of system. List may be used
3997 * in later ioctl's (above) to get
3998 * other information.
4002 ifconf(u_long cmd
, user_addr_t ifrp
, int *ret_space
)
4004 struct ifnet
*ifp
= NULL
;
4009 net_thread_marks_t marks
;
4011 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
4014 * Zero the ifr buffer to make sure we don't
4015 * disclose the contents of the stack.
4017 bzero(&ifr
, sizeof(struct ifreq
));
4020 ifnet_head_lock_shared();
4021 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) &&
4022 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
4024 size_t ifnlen
, addrs
;
4026 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
4027 "%s", if_name(ifp
));
4028 if (ifnlen
+ 1 > sizeof(ifr
.ifr_name
)) {
4029 error
= ENAMETOOLONG
;
4032 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
4035 ifnet_lock_shared(ifp
);
4038 ifa
= ifp
->if_addrhead
.tqh_first
;
4039 for (; space
> sizeof(ifr
) && ifa
;
4040 ifa
= ifa
->ifa_link
.tqe_next
) {
4041 struct sockaddr
*sa
;
4044 struct sockaddr_dl sdl
;
4045 uint8_t buf
[SOCK_MAXADDRLEN
+ 1];
4049 * Make sure to accomodate the largest possible
4050 * size of SA(if_lladdr)->sa_len.
4052 _CASSERT(sizeof(u
) == (SOCK_MAXADDRLEN
+ 1));
4058 if (ifa
== ifp
->if_lladdr
) {
4059 VERIFY(sa
->sa_family
== AF_LINK
);
4060 bcopy(sa
, &u
, sa
->sa_len
);
4062 ifnet_guarded_lladdr_copy_bytes(ifp
,
4063 LLADDR(&u
.sdl
), u
.sdl
.sdl_alen
);
4068 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
4069 struct osockaddr
*osa
=
4070 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
4072 osa
->sa_family
= sa
->sa_family
;
4073 error
= copyout((caddr_t
)&ifr
, ifrp
,
4075 ifrp
+= sizeof(struct ifreq
);
4076 } else if (sa
->sa_len
<= sizeof(*sa
)) {
4078 error
= copyout((caddr_t
)&ifr
, ifrp
,
4080 ifrp
+= sizeof(struct ifreq
);
4083 sizeof(ifr
) + sa
->sa_len
- sizeof(*sa
)) {
4087 space
-= sa
->sa_len
- sizeof(*sa
);
4088 error
= copyout((caddr_t
)&ifr
, ifrp
,
4089 sizeof(ifr
.ifr_name
));
4091 error
= copyout((caddr_t
)sa
, (ifrp
+
4092 offsetof(struct ifreq
, ifr_addr
)),
4095 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
4102 space
-= sizeof(ifr
);
4104 ifnet_lock_done(ifp
);
4110 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
4111 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
4115 space
-= sizeof(ifr
);
4116 ifrp
+= sizeof(struct ifreq
);
4120 *ret_space
-= space
;
4121 net_thread_marks_pop(marks
);
4126 * Just like if_promisc(), but for all-multicast-reception mode.
4129 if_allmulti(struct ifnet
*ifp
, int onswitch
)
4134 ifnet_lock_exclusive(ifp
);
4137 if (ifp
->if_amcount
++ == 0) {
4138 ifp
->if_flags
|= IFF_ALLMULTI
;
4142 if (ifp
->if_amcount
> 1) {
4145 ifp
->if_amcount
= 0;
4146 ifp
->if_flags
&= ~IFF_ALLMULTI
;
4150 ifnet_lock_done(ifp
);
4153 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
4162 static struct ifmultiaddr
*
4165 struct ifmultiaddr
*ifma
;
4167 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
4168 zalloc_noblock(ifma_zone
);
4171 bzero(ifma
, ifma_size
);
4172 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4173 ifma
->ifma_debug
|= IFD_ALLOC
;
4174 if (ifma_debug
!= 0) {
4175 ifma
->ifma_debug
|= IFD_DEBUG
;
4176 ifma
->ifma_trace
= ifma_trace
;
4183 ifma_free(struct ifmultiaddr
*ifma
)
4187 if (ifma
->ifma_protospec
!= NULL
) {
4188 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4190 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4191 ifma
->ifma_anoncnt
!= 0) {
4192 panic("%s: Freeing ifma=%p with outstanding anon req",
4195 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4196 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4197 __func__
, ifma
, ifma
->ifma_ifp
);
4199 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
4200 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
4202 } else if (ifma
->ifma_refcount
!= 0) {
4203 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
4205 } else if (ifma
->ifma_reqcnt
!= 0) {
4206 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
4208 } else if (ifma
->ifma_ifp
!= NULL
) {
4209 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
4210 ifma
->ifma_ifp
, ifma
);
4212 } else if (ifma
->ifma_ll
!= NULL
) {
4213 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
4214 ifma
->ifma_ll
, ifma
);
4217 ifma
->ifma_debug
&= ~IFD_ALLOC
;
4218 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
4219 (IFD_DEBUG
| IFD_TRASHED
)) {
4220 lck_mtx_lock(&ifma_trash_lock
);
4221 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
4223 lck_mtx_unlock(&ifma_trash_lock
);
4224 ifma
->ifma_debug
&= ~IFD_TRASHED
;
4228 if (ifma
->ifma_addr
!= NULL
) {
4229 FREE(ifma
->ifma_addr
, M_IFADDR
);
4230 ifma
->ifma_addr
= NULL
;
4232 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
4233 zfree(ifma_zone
, ifma
);
4237 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
4239 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
4244 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
4245 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
4249 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
4250 tr
= ifma_dbg
->ifma_refhold
;
4252 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
4253 tr
= ifma_dbg
->ifma_refrele
;
4256 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
4257 ctrace_record(&tr
[idx
]);
4261 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
4266 IFMA_LOCK_ASSERT_HELD(ifma
);
4269 if (++ifma
->ifma_refcount
== 0) {
4270 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
4272 } else if (ifma
->ifma_trace
!= NULL
) {
4273 (*ifma
->ifma_trace
)(ifma
, TRUE
);
4281 ifma_remref(struct ifmultiaddr
*ifma
)
4283 struct ifmultiaddr
*ll
;
4287 if (ifma
->ifma_refcount
== 0) {
4288 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
4290 } else if (ifma
->ifma_trace
!= NULL
) {
4291 (*ifma
->ifma_trace
)(ifma
, FALSE
);
4294 --ifma
->ifma_refcount
;
4295 if (ifma
->ifma_refcount
> 0) {
4301 ifma
->ifma_ifp
= NULL
;
4302 ifma
->ifma_ll
= NULL
;
4304 ifma_free(ifma
); /* deallocate it */
4312 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4314 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4315 IFMA_LOCK_ASSERT_HELD(ifma
);
4317 if (ifma
->ifma_ifp
!= ifp
) {
4318 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
4319 ifma
->ifma_ifp
, ifp
);
4321 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4322 panic("%s: Attempt to attach an already attached ifma=%p",
4325 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4326 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
4328 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4329 panic("%s: Attempt to reattach a detached ifma=%p",
4334 ifma
->ifma_reqcnt
++;
4335 VERIFY(ifma
->ifma_reqcnt
== 1);
4336 IFMA_ADDREF_LOCKED(ifma
);
4337 ifma
->ifma_debug
|= IFD_ATTACHED
;
4339 ifma
->ifma_anoncnt
++;
4340 VERIFY(ifma
->ifma_anoncnt
== 1);
4341 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4344 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
4348 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4350 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4351 IFMA_LOCK_ASSERT_HELD(ifma
);
4353 if (ifma
->ifma_reqcnt
== 0) {
4354 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
4356 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4357 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
4359 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
4360 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
4362 } else if (ifma
->ifma_ifp
!= ifp
) {
4363 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
4364 ifma
->ifma_ifp
, ifp
);
4369 --ifma
->ifma_anoncnt
;
4370 if (ifma
->ifma_anoncnt
> 0) {
4373 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
4376 --ifma
->ifma_reqcnt
;
4377 if (ifma
->ifma_reqcnt
> 0) {
4381 if (ifma
->ifma_protospec
!= NULL
) {
4382 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4384 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4385 ifma
->ifma_anoncnt
!= 0) {
4386 panic("%s: Detaching ifma=%p with outstanding anon req",
4389 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
4390 panic("%s: Attempt to detach an unattached address ifma=%p",
4393 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4394 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
4399 * NOTE: Caller calls IFMA_REMREF
4401 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
4402 LIST_REMOVE(ifma
, ifma_link
);
4403 if (LIST_EMPTY(&ifp
->if_multiaddrs
)) {
4404 ifp
->if_updatemcasts
= 0;
4407 if (ifma
->ifma_debug
& IFD_DEBUG
) {
4408 /* Become a regular mutex, just in case */
4409 IFMA_CONVERT_LOCK(ifma
);
4410 lck_mtx_lock(&ifma_trash_lock
);
4411 TAILQ_INSERT_TAIL(&ifma_trash_head
,
4412 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
4413 lck_mtx_unlock(&ifma_trash_lock
);
4414 ifma
->ifma_debug
|= IFD_TRASHED
;
4421 * Find an ifmultiaddr that matches a socket address on an interface.
4423 * Caller is responsible for holding the ifnet_lock while calling
4427 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4428 struct ifmultiaddr
**retifma
, int anon
)
4430 struct ifmultiaddr
*ifma
;
4432 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4433 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4434 IFMA_LOCK_SPIN(ifma
);
4435 if (!ifa_equal(sa
, ifma
->ifma_addr
)) {
4440 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4441 ifma
->ifma_anoncnt
!= 0);
4442 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4443 ifma
->ifma_anoncnt
== 0);
4444 ifma
->ifma_anoncnt
++;
4445 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4446 VERIFY(ifma
->ifma_anoncnt
== 1);
4447 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4450 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
4451 ifma
->ifma_reqcnt
++;
4452 VERIFY(ifma
->ifma_reqcnt
> 1);
4454 if (retifma
!= NULL
) {
4456 IFMA_ADDREF_LOCKED(ifma
);
4465 * Radar 3642395, make sure all multicasts are in a standard format.
4467 static struct sockaddr
*
4468 copy_and_normalize(const struct sockaddr
*original
)
4471 const u_char
*aptr
= NULL
;
4472 struct sockaddr
*copy
= NULL
;
4473 struct sockaddr_dl
*sdl_new
= NULL
;
4476 if (original
->sa_family
!= AF_LINK
&&
4477 original
->sa_family
!= AF_UNSPEC
) {
4478 /* Just make a copy */
4479 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
4480 M_IFADDR
, M_WAITOK
);
4482 bcopy(original
, copy
, original
->sa_len
);
4487 switch (original
->sa_family
) {
4489 const struct sockaddr_dl
*sdl_original
=
4490 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
4492 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
4493 sdl_original
->sdl_slen
+
4494 offsetof(struct sockaddr_dl
, sdl_data
) >
4495 sdl_original
->sdl_len
) {
4499 alen
= sdl_original
->sdl_alen
;
4500 aptr
= CONST_LLADDR(sdl_original
);
4505 if (original
->sa_len
< ETHER_ADDR_LEN
+
4506 offsetof(struct sockaddr
, sa_data
)) {
4510 alen
= ETHER_ADDR_LEN
;
4511 aptr
= (const u_char
*)original
->sa_data
;
4516 if (alen
== 0 || aptr
== NULL
) {
4520 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
4521 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
4523 if (sdl_new
!= NULL
) {
4524 bzero(sdl_new
, len
);
4525 sdl_new
->sdl_len
= len
;
4526 sdl_new
->sdl_family
= AF_LINK
;
4527 sdl_new
->sdl_alen
= alen
;
4528 bcopy(aptr
, LLADDR(sdl_new
), alen
);
4531 return (struct sockaddr
*)sdl_new
;
4535 * Network-layer protocol domains which hold references to the underlying
4536 * link-layer record must use this routine.
4539 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4540 struct ifmultiaddr
**retifma
)
4542 return if_addmulti_common(ifp
, sa
, retifma
, 0);
4546 * Anything other than network-layer protocol domains which hold references
4547 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4548 * ioctl, ifnet_add_multicast(), if_bond.
4551 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4552 struct ifmultiaddr
**retifma
)
4554 return if_addmulti_common(ifp
, sa
, retifma
, 1);
4558 * Register an additional multicast address with a network interface.
4560 * - If the address is already present, bump the reference count on the
4561 * address and return.
4562 * - If the address is not link-layer, look up a link layer address.
4563 * - Allocate address structures for one or both addresses, and attach to the
4564 * multicast address list on the interface. If automatically adding a link
4565 * layer address, the protocol address will own a reference to the link
4566 * layer address, to be freed when it is freed.
4567 * - Notify the network device driver of an addition to the multicast address
4570 * 'sa' points to caller-owned memory with the desired multicast address.
4572 * 'retifma' will be used to return a pointer to the resulting multicast
4573 * address reference, if desired.
4575 * 'anon' indicates a link-layer address with no protocol address reference
4576 * made to it. Anything other than network-layer protocol domain requests
4577 * are considered as anonymous.
4580 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4581 struct ifmultiaddr
**retifma
, int anon
)
4583 struct sockaddr_storage storage
;
4584 struct sockaddr
*llsa
= NULL
;
4585 struct sockaddr
*dupsa
= NULL
;
4586 int error
= 0, ll_firstref
= 0, lladdr
;
4587 struct ifmultiaddr
*ifma
= NULL
;
4588 struct ifmultiaddr
*llifma
= NULL
;
4590 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4591 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
4592 sa
->sa_family
== AF_LINK
);
4594 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4595 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
4596 dupsa
= copy_and_normalize(sa
);
4597 if (dupsa
== NULL
) {
4604 ifnet_lock_exclusive(ifp
);
4605 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
4606 error
= EADDRNOTAVAIL
;
4607 ifnet_lock_done(ifp
);
4611 /* If the address is already present, return a new reference to it */
4612 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4613 ifnet_lock_done(ifp
);
4619 * The address isn't already present; give the link layer a chance
4620 * to accept/reject it, and also find out which AF_LINK address this
4621 * maps to, if it isn't one already.
4623 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
4625 if (error
== 0 && storage
.ss_len
!= 0) {
4626 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
4632 llifma
= ifma_alloc(M_WAITOK
);
4633 if (llifma
== NULL
) {
4639 /* to be similar to FreeBSD */
4640 if (error
== EOPNOTSUPP
) {
4642 } else if (error
!= 0) {
4646 /* Allocate while we aren't holding any locks */
4647 if (dupsa
== NULL
) {
4648 dupsa
= copy_and_normalize(sa
);
4649 if (dupsa
== NULL
) {
4654 ifma
= ifma_alloc(M_WAITOK
);
4660 ifnet_lock_exclusive(ifp
);
4662 * Check again for the matching multicast.
4664 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4666 ifnet_lock_done(ifp
);
4670 if (llifma
!= NULL
) {
4671 VERIFY(!anon
); /* must not get here if "anonymous" */
4672 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
4673 FREE(llsa
, M_IFADDR
);
4677 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
4680 llifma
->ifma_addr
= llsa
;
4681 llifma
->ifma_ifp
= ifp
;
4683 if_attach_ifma(ifp
, llifma
, 0);
4684 /* add extra refcnt for ifma */
4685 IFMA_ADDREF_LOCKED(llifma
);
4686 IFMA_UNLOCK(llifma
);
4687 ifma
->ifma_ll
= llifma
;
4691 /* "anonymous" request should not result in network address */
4692 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4694 ifma
->ifma_addr
= dupsa
;
4695 ifma
->ifma_ifp
= ifp
;
4697 if_attach_ifma(ifp
, ifma
, anon
);
4698 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4699 if (retifma
!= NULL
) {
4701 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
4703 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4704 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4706 ifnet_lock_done(ifp
);
4708 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
4709 IFMA_REMREF(ifma
); /* for this routine */
4712 * We are certain we have added something, so call down to the
4713 * interface to let them know about it. Do this only for newly-
4714 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4716 if (lladdr
|| ll_firstref
) {
4717 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
4720 if (ifp
->if_updatemcasts
> 0) {
4721 ifp
->if_updatemcasts
= 0;
4730 if (dupsa
!= NULL
) {
4731 FREE(dupsa
, M_IFADDR
);
4733 if (llifma
!= NULL
) {
4737 FREE(llsa
, M_IFADDR
);
4744 * Delete a multicast group membership by network-layer group address.
4745 * This routine is deprecated.
4748 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4750 return if_delmulti_common(NULL
, ifp
, sa
, 0);
4754 * Delete a multicast group membership by group membership pointer.
4755 * Network-layer protocol domains must use this routine.
4758 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
4760 return if_delmulti_common(ifma
, NULL
, NULL
, 0);
4764 * Anything other than network-layer protocol domains which hold references
4765 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4766 * ioctl, ifnet_remove_multicast(), if_bond.
4769 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4771 return if_delmulti_common(NULL
, ifp
, sa
, 1);
4775 * Delete a multicast group membership by network-layer group address.
4777 * Returns ENOENT if the entry could not be found.
4780 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
4781 const struct sockaddr
*sa
, int anon
)
4783 struct sockaddr
*dupsa
= NULL
;
4784 int lastref
, ll_lastref
= 0, lladdr
;
4785 struct ifmultiaddr
*ll
= NULL
;
4787 /* sanity check for callers */
4788 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
4791 ifp
= ifma
->ifma_ifp
;
4795 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
4796 dupsa
= copy_and_normalize(sa
);
4797 if (dupsa
== NULL
) {
4803 ifnet_lock_exclusive(ifp
);
4805 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4806 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4808 if (!ifa_equal(sa
, ifma
->ifma_addr
) ||
4809 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
4810 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4811 ifma
->ifma_anoncnt
!= 0);
4815 /* found; keep it locked */
4819 if (dupsa
!= NULL
) {
4820 FREE(dupsa
, M_IFADDR
);
4822 ifnet_lock_done(ifp
);
4828 IFMA_LOCK_ASSERT_HELD(ifma
);
4829 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4830 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
4831 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
4832 ifma
->ifma_reqcnt
== 0));
4833 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4835 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4836 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4838 if (lastref
&& ll
!= NULL
) {
4840 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
4843 ifnet_lock_done(ifp
);
4846 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
4849 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
4851 * Make sure the interface driver is notified in the
4852 * case of a link layer mcast group being left. Do
4853 * this only for a AF_LINK/AF_UNSPEC address that has
4854 * been removed from the if_multiaddrs set.
4856 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
4860 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
4863 IFMA_REMREF(ll
); /* for if_multiaddrs list */
4865 IFMA_REMREF(ifma
); /* for this routine */
4866 if (dupsa
!= NULL
) {
4867 FREE(dupsa
, M_IFADDR
);
4874 * Shutdown all network activity. Used boot() when halting
4884 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
4885 for (i
= 0; i
< count
; i
++) {
4887 dlil_proto_unplumb_all(ifp
[i
]);
4889 ifnet_list_free(ifp
);
4896 * Delete Routes for a Network Interface
4898 * Called for each routing entry via the rnh->rnh_walktree() call above
4899 * to delete all route entries referencing a detaching network interface.
4902 * rn pointer to node in the routing table
4903 * arg argument passed to rnh->rnh_walktree() - detaching interface
4907 * errno failed - reason indicated
4911 if_rtdel(struct radix_node
*rn
, void *arg
)
4913 struct rtentry
*rt
= (struct rtentry
*)rn
;
4914 struct ifnet
*ifp
= arg
;
4921 * Checking against RTF_UP protects against walktree
4922 * recursion problems with cloned routes.
4925 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
4927 * Safe to drop rt_lock and use rt_key, rt_gateway,
4928 * since holding rnh_lock here prevents another thread
4929 * from calling rt_setgate() on this route.
4932 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4933 rt_mask(rt
), rt
->rt_flags
, NULL
);
4935 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
4944 * Removes routing table reference to a given interface
4945 * for a given protocol family
4948 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
4950 struct radix_node_head
*rnh
;
4952 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
4953 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
4954 lck_mtx_lock(rnh_lock
);
4955 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
4956 lck_mtx_unlock(rnh_lock
);
4961 if_rtmtu(struct radix_node
*rn
, void *arg
)
4963 struct rtentry
*rt
= (struct rtentry
*)rn
;
4964 struct ifnet
*ifp
= arg
;
4967 if (rt
->rt_ifp
== ifp
) {
4969 * Update the MTU of this entry only if the MTU
4970 * has not been locked (RTV_MTU is not set) and
4971 * if it was non-zero to begin with.
4973 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
) {
4974 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
4975 if (rt_key(rt
)->sa_family
== AF_INET
&&
4976 INTF_ADJUST_MTU_FOR_CLAT46(ifp
)) {
4977 rt
->rt_rmx
.rmx_mtu
= IN6_LINKMTU(ifp
);
4978 /* Further adjust the size for CLAT46 expansion */
4979 rt
->rt_rmx
.rmx_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
4989 * Update the MTU metric of all route entries in all protocol tables
4990 * associated with a particular interface; this is called when the
4991 * MTU of that interface has changed.
4994 if_rtmtu_update(struct ifnet
*ifp
)
4996 struct radix_node_head
*rnh
;
4999 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
5000 if ((rnh
= rt_tables
[p
]) == NULL
) {
5004 lck_mtx_lock(rnh_lock
);
5005 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
5006 lck_mtx_unlock(rnh_lock
);
5008 routegenid_update();
5011 __private_extern__
void
5012 if_data_internal_to_if_data(struct ifnet
*ifp
,
5013 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
5016 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
5017 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
5018 /* compiler will cast down to 32-bit */
5019 #define COPYFIELD32_ATOMIC(fld) do { \
5020 atomic_get_64(if_data->fld, \
5021 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5024 COPYFIELD(ifi_type
);
5025 COPYFIELD(ifi_typelen
);
5026 COPYFIELD(ifi_physical
);
5027 COPYFIELD(ifi_addrlen
);
5028 COPYFIELD(ifi_hdrlen
);
5029 COPYFIELD(ifi_recvquota
);
5030 COPYFIELD(ifi_xmitquota
);
5031 if_data
->ifi_unused1
= 0;
5033 COPYFIELD(ifi_metric
);
5034 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
5035 if_data
->ifi_baudrate
= 0xFFFFFFFF;
5037 COPYFIELD32(ifi_baudrate
);
5040 COPYFIELD32_ATOMIC(ifi_ipackets
);
5041 COPYFIELD32_ATOMIC(ifi_ierrors
);
5042 COPYFIELD32_ATOMIC(ifi_opackets
);
5043 COPYFIELD32_ATOMIC(ifi_oerrors
);
5044 COPYFIELD32_ATOMIC(ifi_collisions
);
5045 COPYFIELD32_ATOMIC(ifi_ibytes
);
5046 COPYFIELD32_ATOMIC(ifi_obytes
);
5047 COPYFIELD32_ATOMIC(ifi_imcasts
);
5048 COPYFIELD32_ATOMIC(ifi_omcasts
);
5049 COPYFIELD32_ATOMIC(ifi_iqdrops
);
5050 COPYFIELD32_ATOMIC(ifi_noproto
);
5052 COPYFIELD(ifi_recvtiming
);
5053 COPYFIELD(ifi_xmittiming
);
5055 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5056 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5058 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
5060 if_data
->ifi_unused2
= 0;
5061 COPYFIELD(ifi_hwassist
);
5062 if_data
->ifi_reserved1
= 0;
5063 if_data
->ifi_reserved2
= 0;
5064 #undef COPYFIELD32_ATOMIC
5069 __private_extern__
void
5070 if_data_internal_to_if_data64(struct ifnet
*ifp
,
5071 const struct if_data_internal
*if_data_int
,
5072 struct if_data64
*if_data64
)
5075 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
5076 #define COPYFIELD64_ATOMIC(fld) do { \
5077 atomic_get_64(if_data64->fld, \
5078 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5081 COPYFIELD64(ifi_type
);
5082 COPYFIELD64(ifi_typelen
);
5083 COPYFIELD64(ifi_physical
);
5084 COPYFIELD64(ifi_addrlen
);
5085 COPYFIELD64(ifi_hdrlen
);
5086 COPYFIELD64(ifi_recvquota
);
5087 COPYFIELD64(ifi_xmitquota
);
5088 if_data64
->ifi_unused1
= 0;
5089 COPYFIELD64(ifi_mtu
);
5090 COPYFIELD64(ifi_metric
);
5091 COPYFIELD64(ifi_baudrate
);
5093 COPYFIELD64_ATOMIC(ifi_ipackets
);
5094 COPYFIELD64_ATOMIC(ifi_ierrors
);
5095 COPYFIELD64_ATOMIC(ifi_opackets
);
5096 COPYFIELD64_ATOMIC(ifi_oerrors
);
5097 COPYFIELD64_ATOMIC(ifi_collisions
);
5098 COPYFIELD64_ATOMIC(ifi_ibytes
);
5099 COPYFIELD64_ATOMIC(ifi_obytes
);
5100 COPYFIELD64_ATOMIC(ifi_imcasts
);
5101 COPYFIELD64_ATOMIC(ifi_omcasts
);
5102 COPYFIELD64_ATOMIC(ifi_iqdrops
);
5103 COPYFIELD64_ATOMIC(ifi_noproto
);
5106 * Note these two fields are actually 32 bit, so doing
5107 * COPYFIELD64_ATOMIC will cause them to be misaligned
5109 COPYFIELD64(ifi_recvtiming
);
5110 COPYFIELD64(ifi_xmittiming
);
5112 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5113 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5115 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
5120 __private_extern__
void
5121 if_copy_traffic_class(struct ifnet
*ifp
,
5122 struct if_traffic_class
*if_tc
)
5124 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
5125 atomic_get_64(if_tc->fld, \
5126 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
5129 bzero(if_tc
, sizeof(*if_tc
));
5130 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
5131 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
5132 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
5133 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
5134 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
5135 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
5136 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
5137 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
5138 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
5139 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
5140 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
5141 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
5142 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
5143 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
5144 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
5145 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
5146 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
5147 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
5148 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
5149 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
5151 #undef COPY_IF_TC_FIELD64_ATOMIC
5155 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
5157 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
5158 atomic_get_64(if_de->fld, \
5159 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
5162 bzero(if_de
, sizeof(*if_de
));
5163 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
5164 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes
);
5165 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets
);
5166 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes
);
5168 #undef COPY_IF_DE_FIELD64_ATOMIC
5172 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
5174 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
5175 atomic_get_64(if_ps->ifi_tcp_##fld, \
5176 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
5179 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
5180 atomic_get_64(if_ps->ifi_udp_##fld, \
5181 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
5184 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
5185 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
5186 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
5187 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
5188 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
5189 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
5190 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
5191 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
5192 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
5193 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
5194 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
5195 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
5196 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
5197 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
5199 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
5200 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
5201 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
5202 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
5203 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
5204 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
5205 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
5206 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
5208 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5209 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5213 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
5215 bzero(if_rs
, sizeof(*if_rs
));
5216 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1)) {
5220 /* by now, ifnet will stay attached so if_inp must be valid */
5221 VERIFY(ifp
->if_inp
!= NULL
);
5222 bcopy(&ifp
->if_inp
->pstats
, if_rs
, sizeof(*if_rs
));
5224 /* Release the IO refcnt */
5225 ifnet_decr_iorefcnt(ifp
);
5229 ifa_remref(struct ifaddr
*ifa
, int locked
)
5234 IFA_LOCK_ASSERT_HELD(ifa
);
5237 if (ifa
->ifa_refcnt
== 0) {
5238 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
5239 } else if (ifa
->ifa_trace
!= NULL
) {
5240 (*ifa
->ifa_trace
)(ifa
, FALSE
);
5242 if (--ifa
->ifa_refcnt
== 0) {
5243 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
5244 panic("ifa %p attached to ifp is being freed\n", ifa
);
5247 * Some interface addresses are allocated either statically
5248 * or carved out of a larger block. Only free it if it was
5249 * allocated via MALLOC or via the corresponding per-address
5250 * family allocator. Otherwise, leave it alone.
5252 if (ifa
->ifa_debug
& IFD_ALLOC
) {
5253 if (ifa
->ifa_free
== NULL
) {
5255 FREE(ifa
, M_IFADDR
);
5257 /* Become a regular mutex */
5258 IFA_CONVERT_LOCK(ifa
);
5259 /* callee will unlock */
5260 (*ifa
->ifa_free
)(ifa
);
5268 if (!locked
&& ifa
!= NULL
) {
5276 ifa_addref(struct ifaddr
*ifa
, int locked
)
5281 IFA_LOCK_ASSERT_HELD(ifa
);
5284 if (++ifa
->ifa_refcnt
== 0) {
5285 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
5287 } else if (ifa
->ifa_trace
!= NULL
) {
5288 (*ifa
->ifa_trace
)(ifa
, TRUE
);
5296 ifa_lock_init(struct ifaddr
*ifa
)
5298 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
5302 ifa_lock_destroy(struct ifaddr
*ifa
)
5304 IFA_LOCK_ASSERT_NOTHELD(ifa
);
5305 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);
5311 * The switch statement below does nothing at runtime, as it serves as a
5312 * compile time check to ensure that all of the socket 'i' ioctls (those
5313 * in the 'i' group going thru soo_ioctl) that are made available by the
5314 * networking stack is unique. This works as long as this routine gets
5315 * updated each time a new interface ioctl gets added.
5317 * Any failures at compile time indicates duplicated ioctl values.
5319 static __attribute__((unused
)) void
5320 ifioctl_cassert(void)
5323 * This is equivalent to _CASSERT() and the compiler wouldn't
5324 * generate any instructions, thus for compile time only.
5326 switch ((u_long
)0) {
5329 /* bsd/net/if_ppp.h */
5331 case SIOCGPPPCSTATS
:
5334 /* bsd/netinet6/in6_var.h */
5335 case SIOCSIFADDR_IN6
:
5336 case SIOCGIFADDR_IN6
:
5337 case SIOCSIFDSTADDR_IN6
:
5338 case SIOCSIFNETMASK_IN6
:
5339 case SIOCGIFDSTADDR_IN6
:
5340 case SIOCGIFNETMASK_IN6
:
5341 case SIOCDIFADDR_IN6
:
5342 case SIOCAIFADDR_IN6_32
:
5343 case SIOCAIFADDR_IN6_64
:
5344 case SIOCSIFPHYADDR_IN6_32
:
5345 case SIOCSIFPHYADDR_IN6_64
:
5346 case SIOCGIFPSRCADDR_IN6
:
5347 case SIOCGIFPDSTADDR_IN6
:
5348 case SIOCGIFAFLAG_IN6
:
5349 case SIOCGDRLST_IN6_32
:
5350 case SIOCGDRLST_IN6_64
:
5351 case SIOCGPRLST_IN6_32
:
5352 case SIOCGPRLST_IN6_64
:
5353 case OSIOCGIFINFO_IN6
:
5354 case SIOCGIFINFO_IN6
:
5355 case SIOCSNDFLUSH_IN6
:
5356 case SIOCGNBRINFO_IN6_32
:
5357 case SIOCGNBRINFO_IN6_64
:
5358 case SIOCSPFXFLUSH_IN6
:
5359 case SIOCSRTRFLUSH_IN6
:
5360 case SIOCGIFALIFETIME_IN6
:
5361 case SIOCSIFALIFETIME_IN6
:
5362 case SIOCGIFSTAT_IN6
:
5363 case SIOCGIFSTAT_ICMP6
:
5364 case SIOCSDEFIFACE_IN6_32
:
5365 case SIOCSDEFIFACE_IN6_64
:
5366 case SIOCGDEFIFACE_IN6_32
:
5367 case SIOCGDEFIFACE_IN6_64
:
5368 case SIOCSIFINFO_FLAGS
:
5371 case SIOCGSCOPE6DEF
:
5372 case SIOCSIFPREFIX_IN6
:
5373 case SIOCGIFPREFIX_IN6
:
5374 case SIOCDIFPREFIX_IN6
:
5375 case SIOCAIFPREFIX_IN6
:
5376 case SIOCCIFPREFIX_IN6
:
5377 case SIOCSGIFPREFIX_IN6
:
5378 case SIOCPROTOATTACH_IN6_32
:
5379 case SIOCPROTOATTACH_IN6_64
:
5380 case SIOCPROTODETACH_IN6
:
5381 case SIOCLL_START_32
:
5382 case SIOCLL_START_64
:
5384 case SIOCAUTOCONF_START
:
5385 case SIOCAUTOCONF_STOP
:
5386 case SIOCSETROUTERMODE_IN6
:
5387 case SIOCLL_CGASTART_32
:
5388 case SIOCLL_CGASTART_64
:
5389 case SIOCGIFCGAPREP_IN6
:
5390 case SIOCSIFCGAPREP_IN6
:
5393 /* bsd/sys/sockio.h */
5396 case SIOCSIFDSTADDR
:
5397 case OSIOCGIFDSTADDR
:
5400 case OSIOCGIFBRDADDR
:
5401 case SIOCSIFBRDADDR
:
5402 case OSIOCGIFCONF32
:
5403 case OSIOCGIFCONF64
:
5404 case OSIOCGIFNETMASK
:
5405 case SIOCSIFNETMASK
:
5412 case SIOCGIFDSTADDR
:
5413 case SIOCGIFBRDADDR
:
5416 case SIOCGIFNETMASK
:
5418 case SIOCAUTONETMASK
:
5428 case SIOCGIFMEDIA32
:
5429 case SIOCGIFMEDIA64
:
5430 case SIOCGIFXMEDIA32
:
5431 case SIOCGIFXMEDIA64
:
5432 case SIOCSIFGENERIC
:
5433 case SIOCGIFGENERIC
:
5438 case SIOCSIFPHYADDR
:
5439 case SIOCGIFPSRCADDR
:
5440 case SIOCGIFPDSTADDR
:
5441 case SIOCDIFPHYADDR
:
5449 case SIOCPROTOATTACH
:
5450 case SIOCPROTODETACH
:
5459 case SIOCSDRVSPEC32
:
5460 case SIOCGDRVSPEC32
:
5461 case SIOCSDRVSPEC64
:
5462 case SIOCGDRVSPEC64
:
5467 case SIOCIFGCLONERS32
:
5468 case SIOCIFGCLONERS64
:
5470 case SIOCGIFASYNCMAP
:
5471 case SIOCSIFASYNCMAP
:
5475 #endif /* CONFIG_MACF_NET */
5479 case SIOCGIFWAKEFLAGS
:
5481 case SIOCGIFGETRTREFCNT
:
5482 case SIOCGIFLINKQUALITYMETRIC
:
5483 case SIOCSIFOPPORTUNISTIC
:
5484 case SIOCGIFOPPORTUNISTIC
:
5485 case SIOCSETROUTERMODE
:
5489 case SIOCSIFLINKPARAMS
:
5490 case SIOCGIFLINKPARAMS
:
5491 case SIOCGIFQUEUESTATS
:
5492 case SIOCSIFTHROTTLE
:
5493 case SIOCGIFTHROTTLE
:
5495 case SIOCGASSOCIDS32
:
5496 case SIOCGASSOCIDS64
:
5497 case SIOCGCONNIDS32
:
5498 case SIOCGCONNIDS64
:
5499 case SIOCGCONNINFO32
:
5500 case SIOCGCONNINFO64
:
5501 case SIOCSCONNORDER
:
5502 case SIOCGCONNORDER
:
5506 case SIOCGIFDELEGATE
:
5509 case SIOCGIFEXPENSIVE
:
5510 case SIOCSIFEXPENSIVE
:
5513 case SIOCGSTARTDELAY
:
5515 case SIOCAIFAGENTID
:
5516 case SIOCDIFAGENTID
:
5517 case SIOCGIFAGENTIDS32
:
5518 case SIOCGIFAGENTIDS64
:
5519 case SIOCGIFAGENTDATA32
:
5520 case SIOCGIFAGENTDATA64
:
5522 case SIOCSIFINTERFACESTATE
:
5523 case SIOCGIFINTERFACESTATE
:
5524 case SIOCSIFPROBECONNECTIVITY
:
5525 case SIOCGIFPROBECONNECTIVITY
:
5527 case SIOCGIFFUNCTIONALTYPE
:
5528 case SIOCSIFNETSIGNATURE
:
5529 case SIOCGIFNETSIGNATURE
:
5536 case SIOCSQOSMARKINGMODE
:
5537 case SIOCSQOSMARKINGENABLED
:
5538 case SIOCGQOSMARKINGMODE
:
5539 case SIOCGQOSMARKINGENABLED
:
5541 case SIOCSIFTIMESTAMPENABLE
:
5542 case SIOCSIFTIMESTAMPDISABLE
:
5543 case SIOCGIFTIMESTAMPENABLED
:
5545 case SIOCSIFDISABLEOUTPUT
:
5547 case SIOCGIFAGENTLIST32
:
5548 case SIOCGIFAGENTLIST64
:
5550 case SIOCSIFLOWINTERNET
:
5551 case SIOCGIFLOWINTERNET
:
5554 case SIOCGIFNAT64PREFIX
:
5555 case SIOCSIFNAT64PREFIX
:
5557 case SIOCGIFCLAT46ADDR
:
5560 case SIOCGIFPROTOLIST32
:
5561 case SIOCGIFPROTOLIST64
:
5563 case SIOCGIFLOWPOWER
:
5564 case SIOCSIFLOWPOWER
:
5570 ifnet_mbuf_packetpreamblelen(struct ifnet
*ifp
)
5576 /* The following is used to enqueue work items for interface events */
5579 union sockaddr_in_4_6 addr
;
5580 uint32_t intf_event_code
;
5584 intf_event_callback(void *arg
)
5586 struct intf_event
*p_intf_ev
= (struct intf_event
*)arg
;
5588 /* Call this before we walk the tree */
5589 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt
, ifnet_event
, p_intf_ev
->ifp
,
5590 (struct sockaddr
*)&(p_intf_ev
->addr
), p_intf_ev
->intf_event_code
);
5593 struct intf_event_nwk_wq_entry
{
5594 struct nwk_wq_entry nwk_wqe
;
5595 struct intf_event intf_ev_arg
;
5599 intf_event_enqueue_nwk_wq_entry(struct ifnet
*ifp
, struct sockaddr
*addrp
,
5600 uint32_t intf_event_code
)
5602 #pragma unused(addrp)
5603 struct intf_event_nwk_wq_entry
*p_intf_ev
= NULL
;
5605 MALLOC(p_intf_ev
, struct intf_event_nwk_wq_entry
*,
5606 sizeof(struct intf_event_nwk_wq_entry
),
5607 M_NWKWQ
, M_WAITOK
| M_ZERO
);
5609 p_intf_ev
->intf_ev_arg
.ifp
= ifp
;
5611 * XXX Not using addr in the arg. This will be used
5612 * once we need IP address add/delete events
5614 p_intf_ev
->intf_ev_arg
.intf_event_code
= intf_event_code
;
5615 p_intf_ev
->nwk_wqe
.func
= intf_event_callback
;
5616 p_intf_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
5617 p_intf_ev
->nwk_wqe
.arg
= &p_intf_ev
->intf_ev_arg
;
5618 nwk_wq_enqueue((struct nwk_wq_entry
*)p_intf_ev
);