2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)if.c 8.3 (Berkeley) 1/4/94
61 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
64 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <kern/locks.h>
72 #include <sys/param.h>
73 #include <sys/malloc.h>
75 #include <sys/systm.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/protosw.h>
80 #include <sys/kernel.h>
81 #include <sys/sockio.h>
82 #include <sys/syslog.h>
83 #include <sys/sysctl.h>
84 #include <sys/mcache.h>
85 #include <sys/kauth.h>
87 #include <kern/zalloc.h>
88 #include <mach/boolean.h>
90 #include <machine/endian.h>
92 #include <pexpert/pexpert.h>
95 #include <net/if_arp.h>
96 #include <net/if_dl.h>
97 #include <net/if_types.h>
98 #include <net/if_var.h>
99 #include <net/if_media.h>
100 #include <net/if_ppp.h>
101 #include <net/ethernet.h>
102 #include <net/network_agent.h>
103 #include <net/pktsched/pktsched_netem.h>
104 #include <net/radix.h>
105 #include <net/route.h>
106 #include <net/dlil.h>
107 #include <net/nwk_wq.h>
109 #include <sys/domain.h>
110 #include <libkern/OSAtomic.h>
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
115 #include <netinet/in_tclass.h>
116 #include <netinet/ip_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip6.h>
119 #include <netinet/ip_var.h>
120 #include <netinet/tcp.h>
121 #include <netinet/tcp_var.h>
122 #include <netinet/udp.h>
123 #include <netinet/udp_var.h>
124 #include <netinet6/in6_var.h>
125 #include <netinet6/in6_ifattach.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet6/nd6.h>
134 * System initialization
137 extern char *proc_name_address(void *);
139 /* Lock group and attribute for ifaddr lock */
140 lck_attr_t
*ifa_mtx_attr
;
141 lck_grp_t
*ifa_mtx_grp
;
142 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
144 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
146 static int ifioctl_ifconf(u_long
, caddr_t
);
147 static int ifioctl_ifclone(u_long
, caddr_t
);
148 static int ifioctl_iforder(u_long
, caddr_t
);
149 static int ifioctl_ifdesc(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
150 static int ifioctl_linkparams(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
151 static int ifioctl_qstats(struct ifnet
*, u_long
, caddr_t
);
152 static int ifioctl_throttle(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
153 static int ifioctl_netsignature(struct ifnet
*, u_long
, caddr_t
);
154 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
155 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
156 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
158 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
159 struct ifmultiaddr
**, int);
160 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
161 const struct sockaddr
*, int);
162 static struct ifnet
*ifunit_common(const char *, boolean_t
);
164 static int if_rtmtu(struct radix_node
*, void *);
165 static void if_rtmtu_update(struct ifnet
*);
167 static int if_clone_list(int, int *, user_addr_t
);
169 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
171 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
173 /* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
174 struct ifnethead ifnet_ordered_head
= TAILQ_HEAD_INITIALIZER(ifnet_ordered_head
);
175 static u_int32_t if_ordered_count
= 0;
177 static int if_cloners_count
;
178 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
180 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
182 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
183 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
185 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
186 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
188 static struct ifmultiaddr
*ifma_alloc(int);
189 static void ifma_free(struct ifmultiaddr
*);
190 static void ifma_trace(struct ifmultiaddr
*, int);
193 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
195 static unsigned int ifma_debug
; /* debugging (disabled) */
197 static unsigned int ifma_size
; /* size of zone element */
198 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
200 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
203 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
205 struct ifmultiaddr_dbg
{
206 struct ifmultiaddr ifma
; /* ifmultiaddr */
207 u_int16_t ifma_refhold_cnt
; /* # of ref */
208 u_int16_t ifma_refrele_cnt
; /* # of rele */
210 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
212 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
213 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
217 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
220 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
221 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
222 static decl_lck_mtx_data(, ifma_trash_lock
);
224 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
225 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
228 * XXX: declare here to avoid to include many inet6 related files..
229 * should be more generalized?
231 extern void nd6_setmtu(struct ifnet
*);
232 extern lck_mtx_t
*nd6_mutex
;
234 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Link layers");
235 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
236 "Generic link-management");
238 SYSCTL_DECL(_net_link_generic_system
);
240 static uint32_t if_verbose
= 0;
241 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, if_verbose
,
242 CTLFLAG_RW
| CTLFLAG_LOCKED
, &if_verbose
, 0, "");
244 #if (DEBUG || DEVELOPMENT)
245 static uint32_t default_tcp_kao_max
= 0;
246 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, default_tcp_kao_max
,
247 CTLFLAG_RW
| CTLFLAG_LOCKED
, &default_tcp_kao_max
, 0, "");
249 static const uint32_t default_tcp_kao_max
= 0;
250 #endif /* (DEBUG || DEVELOPMENT) */
252 u_int32_t companion_link_sock_buffer_limit
= 0;
255 sysctl_set_companion_link_sock_buf_limit SYSCTL_HANDLER_ARGS
257 #pragma unused(arg1, arg2)
258 int error
, tmp
= companion_link_sock_buffer_limit
;
259 error
= sysctl_handle_int(oidp
, &tmp
, 0, req
);
263 if ((error
= priv_check_cred(kauth_cred_get(),
264 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
268 u_int32_t new_limit
= tmp
;
269 if (new_limit
== companion_link_sock_buffer_limit
) {
273 bool recover
= new_limit
== 0 ? true : false;
275 error
= inp_recover_companion_link(&tcbinfo
);
277 error
= inp_limit_companion_link(&tcbinfo
, new_limit
);
280 companion_link_sock_buffer_limit
= new_limit
;
285 SYSCTL_PROC(_net_link_generic_system
, OID_AUTO
, companion_sndbuf_limit
,
286 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
| CTLFLAG_ANYBODY
,
287 &companion_link_sock_buffer_limit
, 0, sysctl_set_companion_link_sock_buf_limit
,
288 "I", "set sock send buffer limit of connections using companion links");
290 boolean_t intcoproc_unrestricted
;
292 /* Eventhandler context for interface events */
293 struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt
;
298 /* Setup lock group and attribute for ifaddr */
299 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
300 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
301 ifa_mtx_attr
= lck_attr_alloc_init();
303 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof(ifma_debug
));
305 ifma_size
= (ifma_debug
== 0) ? sizeof(struct ifmultiaddr
) :
306 sizeof(struct ifmultiaddr_dbg
);
308 ifma_zone
= zone_create(IFMA_ZONE_NAME
, ifma_size
, ZC_NONE
);
309 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
310 TAILQ_INIT(&ifma_trash_head
);
312 PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted
,
313 sizeof(intcoproc_unrestricted
));
317 * Network interface utility routines.
319 * Routines with ifa_ifwith* names take sockaddr *'s as
324 struct ifaddr
**ifnet_addrs
;
325 struct ifnet
**ifindex2ifnet
;
327 __private_extern__
void
328 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
330 if_attach_ifa_common(ifp
, ifa
, 0);
333 __private_extern__
void
334 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
336 if_attach_ifa_common(ifp
, ifa
, 1);
340 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
342 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
343 IFA_LOCK_ASSERT_HELD(ifa
);
345 if (ifa
->ifa_ifp
!= ifp
) {
346 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
349 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
350 panic("%s: Attempt to attach an already attached ifa=%p",
353 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
354 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
356 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
357 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
360 IFA_ADDREF_LOCKED(ifa
);
361 ifa
->ifa_debug
|= IFD_ATTACHED
;
363 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
365 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
368 if (ifa
->ifa_attached
!= NULL
) {
369 (*ifa
->ifa_attached
)(ifa
);
374 __private_extern__
void
375 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
377 if_detach_ifa_common(ifp
, ifa
, 0);
380 __private_extern__
void
381 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
383 if_detach_ifa_common(ifp
, ifa
, 1);
387 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
389 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
390 IFA_LOCK_ASSERT_HELD(ifa
);
392 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
393 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
395 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
396 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
398 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
399 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
401 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
402 panic("%s: Attempt to detach an unattached address ifa=%p",
405 } else if (ifa
->ifa_ifp
!= ifp
) {
406 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
409 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
411 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
417 panic("%s: Attempt to detach a stray address ifa=%p",
422 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
423 /* This must not be the last reference to the ifaddr */
424 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
425 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
428 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
430 if (ifa
->ifa_detached
!= NULL
) {
431 (*ifa
->ifa_detached
)(ifa
);
436 #define INITIAL_IF_INDEXLIM 8
439 * Function: if_next_index
441 * Return the next available interface index.
442 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
443 * added entry when necessary.
446 * ifnet_addrs[] is indexed by (if_index - 1), whereas
447 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
448 * always allocate one extra element to hold ifindex2ifnet[0], which
451 int if_next_index(void);
453 __private_extern__
int
456 static int if_indexlim
= 0;
459 new_index
= ++if_index
;
460 if (if_index
> if_indexlim
) {
463 caddr_t new_ifnet_addrs
;
464 caddr_t new_ifindex2ifnet
;
465 caddr_t old_ifnet_addrs
;
467 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
468 if (ifnet_addrs
== NULL
) {
469 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
471 new_if_indexlim
= if_indexlim
<< 1;
474 /* allocate space for the larger arrays */
475 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
476 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
477 if (new_ifnet_addrs
== NULL
) {
482 new_ifindex2ifnet
= new_ifnet_addrs
483 + new_if_indexlim
* sizeof(caddr_t
);
484 if (ifnet_addrs
!= NULL
) {
485 /* copy the existing data */
486 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
487 if_indexlim
* sizeof(caddr_t
));
488 bcopy((caddr_t
)ifindex2ifnet
,
490 (if_indexlim
+ 1) * sizeof(caddr_t
));
493 /* switch to the new tables and size */
494 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
495 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
496 if_indexlim
= new_if_indexlim
;
498 /* release the old data */
499 if (old_ifnet_addrs
!= NULL
) {
500 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
507 * Create a clone network interface.
510 if_clone_create(char *name
, int len
, void *params
)
512 struct if_clone
*ifc
;
515 u_int32_t bytoff
, bitoff
;
519 ifc
= if_clone_lookup(name
, &unit
);
524 if (ifunit(name
) != NULL
) {
529 wildcard
= (unit
== UINT32_MAX
);
531 * Find a free unit if none was given.
534 while ((bytoff
< ifc
->ifc_bmlen
) &&
535 (ifc
->ifc_units
[bytoff
] == 0xff)) {
538 if (bytoff
>= ifc
->ifc_bmlen
) {
541 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0) {
544 unit
= (bytoff
<< 3) + bitoff
;
547 if (unit
> ifc
->ifc_maxunit
) {
551 lck_mtx_lock(&ifc
->ifc_mutex
);
552 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
554 lck_mtx_unlock(&ifc
->ifc_mutex
);
560 bitoff
= unit
- (bytoff
<< 3);
564 * Allocate the unit in the bitmap.
566 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
567 ("%s: bit is already set", __func__
));
568 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
570 /* In the wildcard case, we need to update the name. */
572 for (dp
= name
; *dp
!= '\0'; dp
++) {
575 if (snprintf(dp
, len
- (dp
- name
), "%d", unit
) >
576 len
- (dp
- name
) - 1) {
578 * This can only be a programmer error and
579 * there's no straightforward way to recover if
582 panic("%s: interface name too long", __func__
);
586 lck_mtx_unlock(&ifc
->ifc_mutex
);
592 * Destroy a clone network interface.
595 if_clone_destroy(const char *name
)
597 struct if_clone
*ifc
= NULL
;
598 struct ifnet
*ifp
= NULL
;
603 ifc
= if_clone_lookup(name
, &unit
);
610 if (unit
< ifc
->ifc_minifs
) {
615 ifp
= ifunit_ref(name
);
621 if (ifc
->ifc_destroy
== NULL
) {
626 lck_mtx_lock(&ifc
->ifc_mutex
);
627 error
= (*ifc
->ifc_destroy
)(ifp
);
630 lck_mtx_unlock(&ifc
->ifc_mutex
);
634 /* Compute offset in the bitmap and deallocate the unit. */
636 bitoff
= unit
- (bytoff
<< 3);
637 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
638 ("%s: bit is already cleared", __func__
));
639 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
640 lck_mtx_unlock(&ifc
->ifc_mutex
);
644 ifnet_decr_iorefcnt(ifp
);
650 * Look up a network interface cloner.
653 __private_extern__
struct if_clone
*
654 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
656 struct if_clone
*ifc
;
660 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
661 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
662 if (ifc
->ifc_name
[i
] != *cp
) {
668 ifc
= LIST_NEXT(ifc
, ifc_list
);
672 return (struct if_clone
*)NULL
;
678 for (i
= 0; *cp
!= '\0'; cp
++) {
679 if (*cp
< '0' || *cp
> '9') {
680 /* Bogus unit number. */
683 i
= (i
* 10) + (*cp
- '0');
694 if_clone_softc_allocate(const struct if_clone
*ifc
)
696 void *p_clone
= NULL
;
700 p_clone
= zalloc(ifc
->ifc_zone
);
701 if (p_clone
!= NULL
) {
702 bzero(p_clone
, ifc
->ifc_softc_size
);
709 if_clone_softc_deallocate(const struct if_clone
*ifc
, void *p_softc
)
711 VERIFY(ifc
!= NULL
&& p_softc
!= NULL
);
712 bzero(p_softc
, ifc
->ifc_softc_size
);
713 zfree(ifc
->ifc_zone
, p_softc
);
717 * Register a network interface cloner.
720 if_clone_attach(struct if_clone
*ifc
)
727 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
728 ("%s: %s requested more units then allowed (%d > %d)",
729 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
730 ifc
->ifc_maxunit
+ 1));
732 * Compute bitmap size and allocate it.
734 maxclone
= ifc
->ifc_maxunit
+ 1;
736 if ((len
<< 3) < maxclone
) {
739 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
740 if (ifc
->ifc_units
== NULL
) {
743 ifc
->ifc_bmlen
= len
;
744 lck_mtx_init(&ifc
->ifc_mutex
, ifnet_lock_group
, ifnet_lock_attr
);
746 if (ifc
->ifc_softc_size
!= 0) {
747 ifc
->ifc_zone
= zone_create(ifc
->ifc_name
, ifc
->ifc_softc_size
,
751 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
754 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
755 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
757 ("%s: failed to create required interface %s%d",
758 __func__
, ifc
->ifc_name
, unit
));
760 /* Allocate the unit in the bitmap. */
762 bitoff
= unit
- (bytoff
<< 3);
763 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
770 * Unregister a network interface cloner.
773 if_clone_detach(struct if_clone
*ifc
)
775 LIST_REMOVE(ifc
, ifc_list
);
776 FREE(ifc
->ifc_units
, M_CLONE
);
777 if (ifc
->ifc_softc_size
!= 0) {
778 zdestroy(ifc
->ifc_zone
);
781 lck_mtx_destroy(&ifc
->ifc_mutex
, ifnet_lock_group
);
786 * Provide list of interface cloners to userspace.
789 if_clone_list(int count
, int *ret_total
, user_addr_t dst
)
791 char outbuf
[IFNAMSIZ
];
792 struct if_clone
*ifc
;
795 *ret_total
= if_cloners_count
;
796 if (dst
== USER_ADDR_NULL
) {
797 /* Just asking how many there are. */
805 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
807 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
808 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
809 bzero(outbuf
, sizeof(outbuf
));
810 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
811 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
821 if_functional_type(struct ifnet
*ifp
, bool exclude_delegate
)
823 u_int32_t ret
= IFRTYPE_FUNCTIONAL_UNKNOWN
;
826 if (ifp
->if_flags
& IFF_LOOPBACK
) {
827 ret
= IFRTYPE_FUNCTIONAL_LOOPBACK
;
828 } else if (IFNET_IS_COMPANION_LINK(ifp
)) {
829 ret
= IFRTYPE_FUNCTIONAL_COMPANIONLINK
;
830 } else if ((exclude_delegate
&&
831 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
&&
832 ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
)) ||
833 (!exclude_delegate
&& IFNET_IS_WIFI(ifp
))) {
834 if (ifp
->if_eflags
& IFEF_AWDL
) {
835 ret
= IFRTYPE_FUNCTIONAL_WIFI_AWDL
;
837 ret
= IFRTYPE_FUNCTIONAL_WIFI_INFRA
;
839 } else if ((exclude_delegate
&&
840 (ifp
->if_type
== IFT_CELLULAR
)) ||
841 (!exclude_delegate
&& IFNET_IS_CELLULAR(ifp
))) {
842 ret
= IFRTYPE_FUNCTIONAL_CELLULAR
;
843 } else if (IFNET_IS_INTCOPROC(ifp
)) {
844 ret
= IFRTYPE_FUNCTIONAL_INTCOPROC
;
845 } else if ((exclude_delegate
&&
846 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
||
847 ifp
->if_family
== IFNET_FAMILY_BOND
||
848 ifp
->if_family
== IFNET_FAMILY_VLAN
||
849 ifp
->if_family
== IFNET_FAMILY_FIREWIRE
)) ||
850 (!exclude_delegate
&& IFNET_IS_WIRED(ifp
))) {
851 ret
= IFRTYPE_FUNCTIONAL_WIRED
;
859 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
860 * and that it matches only the local (not broadcast) address.
862 __private_extern__
struct in_ifaddr
*
863 ifa_foraddr(unsigned int addr
)
865 return ifa_foraddr_scoped(addr
, IFSCOPE_NONE
);
869 * Similar to ifa_foraddr, except with the added interface scope
870 * constraint (unless the caller passes in IFSCOPE_NONE in which
871 * case there is no scope restriction).
873 __private_extern__
struct in_ifaddr
*
874 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
876 struct in_ifaddr
*ia
= NULL
;
878 lck_rw_lock_shared(in_ifaddr_rwlock
);
879 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
880 IFA_LOCK_SPIN(&ia
->ia_ifa
);
881 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
882 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
883 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
884 IFA_UNLOCK(&ia
->ia_ifa
);
887 IFA_UNLOCK(&ia
->ia_ifa
);
889 lck_rw_done(in_ifaddr_rwlock
);
894 * Similar to ifa_foraddr, except that this for IPv6.
896 __private_extern__
struct in6_ifaddr
*
897 ifa_foraddr6(struct in6_addr
*addr6
)
899 return ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
);
902 __private_extern__
struct in6_ifaddr
*
903 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
905 struct in6_ifaddr
*ia
= NULL
;
907 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
908 TAILQ_FOREACH(ia
, IN6ADDR_HASH(addr6
), ia6_hash
) {
909 IFA_LOCK(&ia
->ia_ifa
);
910 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
911 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
912 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
913 IFA_UNLOCK(&ia
->ia_ifa
);
916 IFA_UNLOCK(&ia
->ia_ifa
);
918 lck_rw_done(&in6_ifaddr_rwlock
);
924 * Return the first (primary) address of a given family on an interface.
926 __private_extern__
struct ifaddr
*
927 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
931 ifnet_lock_shared(ifp
);
932 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
934 if (ifa
->ifa_addr
->sa_family
== family
) {
935 IFA_ADDREF_LOCKED(ifa
); /* for caller */
941 ifnet_lock_done(ifp
);
947 ifa_equal(const struct sockaddr
*sa1
, const struct sockaddr
*sa2
)
952 if (sa1
->sa_len
!= sa2
->sa_len
) {
956 return bcmp(sa1
, sa2
, sa1
->sa_len
) == 0;
960 * Locate an interface based on a complete address.
963 ifa_ifwithaddr_locked(const struct sockaddr
*addr
)
967 struct ifaddr
*result
= NULL
;
969 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
970 ifp
= ifp
->if_link
.tqe_next
) {
971 ifnet_lock_shared(ifp
);
972 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
973 ifa
= ifa
->ifa_link
.tqe_next
) {
975 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
979 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
981 IFA_ADDREF_LOCKED(ifa
); /* for caller */
985 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
986 ifa
->ifa_broadaddr
!= NULL
&&
987 /* IP6 doesn't have broadcast */
988 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
989 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
991 IFA_ADDREF_LOCKED(ifa
); /* for caller */
997 ifnet_lock_done(ifp
);
1004 ifa_ifwithaddr(const struct sockaddr
*addr
)
1006 struct ifaddr
*result
= NULL
;
1008 ifnet_head_lock_shared();
1010 result
= ifa_ifwithaddr_locked(addr
);
1017 * Locate the point to point interface with a given destination address.
1021 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
1025 struct ifaddr
*result
= NULL
;
1027 ifnet_head_lock_shared();
1028 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
1029 ifp
= ifp
->if_link
.tqe_next
) {
1030 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1031 ifnet_lock_shared(ifp
);
1032 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1033 ifa
= ifa
->ifa_link
.tqe_next
) {
1035 if (ifa
->ifa_addr
->sa_family
!=
1040 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1042 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1048 ifnet_lock_done(ifp
);
1056 * Locate the source address of an interface based on a complete address.
1059 ifa_ifwithaddr_scoped_locked(const struct sockaddr
*addr
, unsigned int ifscope
)
1061 struct ifaddr
*result
= NULL
;
1064 if (ifscope
== IFSCOPE_NONE
) {
1065 return ifa_ifwithaddr_locked(addr
);
1068 if (ifscope
> (unsigned int)if_index
) {
1072 ifp
= ifindex2ifnet
[ifscope
];
1074 struct ifaddr
*ifa
= NULL
;
1077 * This is suboptimal; there should be a better way
1078 * to search for a given address of an interface
1079 * for any given address family.
1081 ifnet_lock_shared(ifp
);
1082 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
1083 ifa
= ifa
->ifa_link
.tqe_next
) {
1085 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
1089 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1091 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1095 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1096 ifa
->ifa_broadaddr
!= NULL
&&
1097 /* IP6 doesn't have broadcast */
1098 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1099 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
1101 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1107 ifnet_lock_done(ifp
);
1114 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1116 struct ifaddr
*result
= NULL
;
1118 ifnet_head_lock_shared();
1120 result
= ifa_ifwithaddr_scoped_locked(addr
, ifscope
);
1128 ifa_ifwithnet(const struct sockaddr
*addr
)
1130 return ifa_ifwithnet_common(addr
, IFSCOPE_NONE
);
1134 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1136 return ifa_ifwithnet_common(addr
, ifscope
);
1140 * Find an interface on a specific network. If many, choice
1141 * is most specific found.
1143 static struct ifaddr
*
1144 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
1147 struct ifaddr
*ifa
= NULL
;
1148 struct ifaddr
*ifa_maybe
= NULL
;
1149 u_int af
= addr
->sa_family
;
1150 const char *addr_data
= addr
->sa_data
, *cplim
;
1152 if (af
!= AF_INET
&& af
!= AF_INET6
) {
1153 ifscope
= IFSCOPE_NONE
;
1156 ifnet_head_lock_shared();
1158 * AF_LINK addresses can be looked up directly by their index number,
1159 * so do that if we can.
1161 if (af
== AF_LINK
) {
1162 const struct sockaddr_dl
*sdl
=
1163 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
1164 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
1165 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
1176 * Scan though each interface, looking for ones that have
1177 * addresses in this address family.
1179 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1180 ifnet_lock_shared(ifp
);
1181 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1182 ifa
= ifa
->ifa_link
.tqe_next
) {
1183 const char *cp
, *cp2
, *cp3
;
1186 if (ifa
->ifa_addr
== NULL
||
1187 ifa
->ifa_addr
->sa_family
!= af
) {
1193 * If we're looking up with a scope,
1194 * find using a matching interface.
1196 if (ifscope
!= IFSCOPE_NONE
&&
1197 ifp
->if_index
!= ifscope
) {
1203 * Scan all the bits in the ifa's address.
1204 * If a bit dissagrees with what we are
1205 * looking for, mask it with the netmask
1206 * to see if it really matters.
1207 * (A byte at a time)
1209 if (ifa
->ifa_netmask
== 0) {
1214 cp2
= ifa
->ifa_addr
->sa_data
;
1215 cp3
= ifa
->ifa_netmask
->sa_data
;
1216 cplim
= ifa
->ifa_netmask
->sa_len
+
1217 (char *)ifa
->ifa_netmask
;
1218 while (cp3
< cplim
) {
1219 if ((*cp
++ ^ *cp2
++) & *cp3
++) {
1220 goto next
; /* next address! */
1224 * If the netmask of what we just found
1225 * is more specific than what we had before
1226 * (if we had one) then remember the new one
1227 * before continuing to search
1228 * for an even better one.
1230 if (ifa_maybe
== NULL
||
1231 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1232 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1233 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1235 if (ifa_maybe
!= NULL
) {
1236 IFA_REMREF(ifa_maybe
);
1242 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1244 ifnet_lock_done(ifp
);
1254 } else if (ifa_maybe
!= NULL
) {
1255 IFA_REMREF(ifa_maybe
);
1262 * Find an interface address specific to an interface best matching
1263 * a given address applying same source address selection rules
1264 * as done in the kernel for implicit source address binding
1267 ifaof_ifpforaddr_select(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1269 u_int af
= addr
->sa_family
;
1271 if (af
== AF_INET6
) {
1272 return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6
*, addr
), ifp
, 0);
1275 return ifaof_ifpforaddr(addr
, ifp
);
1279 * Find an interface address specific to an interface best matching
1280 * a given address without regards to source address selection.
1282 * This is appropriate for use-cases where we just want to update/init
1283 * some data structure like routing table entries.
1286 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1288 struct ifaddr
*ifa
= NULL
;
1289 const char *cp
, *cp2
, *cp3
;
1291 struct ifaddr
*ifa_maybe
= NULL
;
1292 struct ifaddr
*better_ifa_maybe
= NULL
;
1293 u_int af
= addr
->sa_family
;
1299 ifnet_lock_shared(ifp
);
1300 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1301 ifa
= ifa
->ifa_link
.tqe_next
) {
1303 if (ifa
->ifa_addr
->sa_family
!= af
) {
1307 if (ifa_maybe
== NULL
) {
1308 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1311 if (ifa
->ifa_netmask
== 0) {
1312 if (ifa_equal(addr
, ifa
->ifa_addr
) ||
1313 ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1314 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1321 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1322 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1323 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1328 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1330 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1335 cp2
= ifa
->ifa_addr
->sa_data
;
1336 cp3
= ifa
->ifa_netmask
->sa_data
;
1337 cplim
= ifa
->ifa_netmask
->sa_len
+
1338 (char *)ifa
->ifa_netmask
;
1339 for (; cp3
< cplim
; cp3
++) {
1340 if ((*cp
++ ^ *cp2
++) & *cp3
) {
1346 if (better_ifa_maybe
== NULL
) {
1347 /* for better_ifa_maybe */
1348 IFA_ADDREF_LOCKED(ifa
);
1349 better_ifa_maybe
= ifa
;
1357 if (better_ifa_maybe
!= NULL
) {
1358 ifa
= better_ifa_maybe
;
1359 better_ifa_maybe
= NULL
;
1366 ifnet_lock_done(ifp
);
1368 if (better_ifa_maybe
!= NULL
) {
1369 IFA_REMREF(better_ifa_maybe
);
1371 if (ifa_maybe
!= NULL
) {
1372 IFA_REMREF(ifa_maybe
);
1378 #include <net/route.h>
1381 * Default action when installing a route with a Link Level gateway.
1382 * Lookup an appropriate real ifa to point to.
1383 * This should be moved to /sys/net/link.c eventually.
1386 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1389 struct sockaddr
*dst
;
1391 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1393 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1394 RT_LOCK_ASSERT_HELD(rt
);
1396 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1397 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0)) {
1401 /* Become a regular mutex, just in case */
1402 RT_CONVERT_LOCK(rt
);
1404 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1408 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1410 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
) {
1411 ifa_rtrequest(cmd
, rt
, sa
);
1418 * if_updown will set the interface up or down. It will
1419 * prevent other up/down events from occurring until this
1420 * up/down event has completed.
1422 * Caller must lock ifnet. This function will drop the
1423 * lock. This allows ifnet_set_flags to set the rest of
1424 * the flags after we change the up/down state without
1425 * dropping the interface lock between setting the
1426 * up/down state and updating the rest of the flags.
1428 __private_extern__
void
1429 if_updown( struct ifnet
*ifp
, int up
)
1433 struct ifaddr
**ifa
;
1435 struct ifclassq
*ifq
= &ifp
->if_snd
;
1437 /* Wait until no one else is changing the up/down state */
1438 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1440 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1441 ifnet_lock_done(ifp
);
1442 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1443 ifnet_lock_exclusive(ifp
);
1446 /* Verify that the interface isn't already in the right state */
1447 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1448 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1452 /* Indicate that the up/down state is changing */
1453 eflags
= if_set_eflags(ifp
, IFEF_UPDOWNCHANGE
);
1454 ASSERT((eflags
& IFEF_UPDOWNCHANGE
) == 0);
1456 /* Mark interface up or down */
1458 ifp
->if_flags
|= IFF_UP
;
1460 ifp
->if_flags
&= ~IFF_UP
;
1463 ifnet_touch_lastchange(ifp
);
1464 ifnet_touch_lastupdown(ifp
);
1466 /* Drop the lock to notify addresses and route */
1467 ifnet_lock_done(ifp
);
1472 /* Inform all transmit queues about the new link state */
1473 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1476 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1477 for (i
= 0; ifa
[i
] != 0; i
++) {
1478 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1480 ifnet_free_address_list(ifa
);
1484 /* Aquire the lock to clear the changing flag */
1485 ifnet_lock_exclusive(ifp
);
1486 if_clear_eflags(ifp
, IFEF_UPDOWNCHANGE
);
1487 wakeup(&ifp
->if_eflags
);
1491 * Mark an interface down and notify protocols of
1498 ifnet_lock_exclusive(ifp
);
1500 ifnet_lock_done(ifp
);
1504 * Mark an interface up and notify protocols of
1511 ifnet_lock_exclusive(ifp
);
1513 ifnet_lock_done(ifp
);
1517 * Flush an interface queue.
1520 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1522 struct ifclassq
*ifq
= &ifp
->if_snd
;
1528 if (IFCQ_IS_ENABLED(ifq
)) {
1529 fq_if_request_classq(ifq
, CLASSQRQ_PURGE
, NULL
);
1532 VERIFY(IFCQ_IS_EMPTY(ifq
));
1540 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1541 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1543 struct ifclassq
*ifq
= &ifp
->if_snd
;
1544 u_int32_t cnt
= 0, len
= 0;
1546 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1553 if (IFCQ_IS_ENABLED(ifq
)) {
1554 cqrq_purge_sc_t req
= { sc
, flow
, 0, 0 };
1556 fq_if_request_classq(ifq
, CLASSQRQ_PURGE_SC
, &req
);
1565 if (packets
!= NULL
) {
1568 if (bytes
!= NULL
) {
1574 * Extracts interface unit number and name from string, returns -1 upon failure.
1575 * Upon success, returns extracted unit number, and interface name in dst.
1578 ifunit_extract(const char *src
, char *dst
, size_t dstlen
, int *unit
)
1585 if (src
== NULL
|| dst
== NULL
|| dstlen
== 0 || unit
== NULL
) {
1590 if (len
< 2 || len
> dstlen
) {
1595 if (c
< '0' || c
> '9') {
1596 return -1; /* trailing garbage */
1602 return -1; /* no interface name */
1606 return -1; /* number is unreasonable */
1610 } while (c
>= '0' && c
<= '9');
1612 bcopy(src
, dst
, len
);
1620 * Map interface name to
1621 * interface structure pointer.
1623 static struct ifnet
*
1624 ifunit_common(const char *name
, boolean_t hold
)
1626 char namebuf
[IFNAMSIZ
+ 1];
1630 if (ifunit_extract(name
, namebuf
, sizeof(namebuf
), &unit
) < 0) {
1634 /* for safety, since we use strcmp() below */
1635 namebuf
[sizeof(namebuf
) - 1] = '\0';
1638 * Now search all the interfaces for this name/number
1640 ifnet_head_lock_shared();
1641 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1643 * Use strcmp() rather than strncmp() here,
1644 * since we want to match the entire string.
1646 if (strcmp(ifp
->if_name
, namebuf
)) {
1649 if (unit
== ifp
->if_unit
) {
1654 /* if called from ifunit_ref() and ifnet is not attached, bail */
1655 if (hold
&& ifp
!= NULL
&& !ifnet_is_attached(ifp
, 1)) {
1664 ifunit(const char *name
)
1666 return ifunit_common(name
, FALSE
);
1670 * Similar to ifunit(), except that we hold an I/O reference count on an
1671 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1672 * Will return NULL unless interface exists and is fully attached.
1675 ifunit_ref(const char *name
)
1677 return ifunit_common(name
, TRUE
);
1681 * Map interface name in a sockaddr_dl to
1682 * interface structure pointer.
1685 if_withname(struct sockaddr
*sa
)
1687 char ifname
[IFNAMSIZ
+ 1];
1688 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1690 if ((sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1691 (sdl
->sdl_nlen
> IFNAMSIZ
)) {
1696 * ifunit wants a null-terminated name. It may not be null-terminated
1697 * in the sockaddr. We don't want to change the caller's sockaddr,
1698 * and there might not be room to put the trailing null anyway, so we
1699 * make a local copy that we know we can null terminate safely.
1702 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1703 ifname
[sdl
->sdl_nlen
] = '\0';
1704 return ifunit(ifname
);
1707 static __attribute__((noinline
)) int
1708 ifioctl_ifconf(u_long cmd
, caddr_t data
)
1713 case OSIOCGIFCONF32
: /* struct ifconf32 */
1714 case SIOCGIFCONF32
: { /* struct ifconf32 */
1715 struct ifconf32 ifc
;
1716 bcopy(data
, &ifc
, sizeof(ifc
));
1717 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1719 bcopy(&ifc
, data
, sizeof(ifc
));
1723 case SIOCGIFCONF64
: /* struct ifconf64 */
1724 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1725 struct ifconf64 ifc
;
1726 bcopy(data
, &ifc
, sizeof(ifc
));
1727 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1728 bcopy(&ifc
, data
, sizeof(ifc
));
1740 static __attribute__((noinline
)) int
1741 ifioctl_ifclone(u_long cmd
, caddr_t data
)
1746 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1747 struct if_clonereq32 ifcr
;
1748 bcopy(data
, &ifcr
, sizeof(ifcr
));
1749 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1750 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1751 bcopy(&ifcr
, data
, sizeof(ifcr
));
1755 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1756 struct if_clonereq64 ifcr
;
1757 bcopy(data
, &ifcr
, sizeof(ifcr
));
1758 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1760 bcopy(&ifcr
, data
, sizeof(ifcr
));
1772 static __attribute__((noinline
)) int
1773 ifioctl_ifdesc(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1775 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1779 VERIFY(ifp
!= NULL
);
1782 case SIOCSIFDESC
: { /* struct if_descreq */
1783 if ((error
= proc_suser(p
)) != 0) {
1787 ifnet_lock_exclusive(ifp
);
1788 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof(ifdr_len
));
1789 if (ifdr_len
> sizeof(ifdr
->ifdr_desc
) ||
1790 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1792 ifnet_lock_done(ifp
);
1796 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1797 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1798 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1799 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1801 ifnet_lock_done(ifp
);
1805 case SIOCGIFDESC
: { /* struct if_descreq */
1806 ifnet_lock_shared(ifp
);
1807 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof(ifdr
->ifdr_desc
));
1808 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof(ifdr_len
));
1809 bzero(&ifdr
->ifdr_desc
, sizeof(ifdr
->ifdr_desc
));
1811 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1813 ifnet_lock_done(ifp
);
1825 static __attribute__((noinline
)) int
1826 ifioctl_linkparams(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1828 struct if_linkparamsreq
*iflpr
=
1829 (struct if_linkparamsreq
*)(void *)data
;
1830 struct ifclassq
*ifq
;
1833 VERIFY(ifp
!= NULL
);
1837 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1838 struct tb_profile tb
= { .rate
= 0, .percent
= 0, .depth
= 0 };
1840 if ((error
= proc_suser(p
)) != 0) {
1845 char netem_name
[32];
1846 (void) snprintf(netem_name
, sizeof(netem_name
),
1847 "if_output_netem_%s", if_name(ifp
));
1848 error
= netem_config(&ifp
->if_output_netem
, netem_name
,
1849 &iflpr
->iflpr_output_netem
, (void *)ifp
,
1850 ifnet_enqueue_netem
, NETEM_MAX_BATCH_SIZE
);
1856 if (!IFCQ_IS_READY(ifq
)) {
1861 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1863 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1864 sizeof(tb
.percent
));
1865 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1870 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1871 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1872 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1876 if (IFCQ_IS_ENABLED(ifq
)) {
1877 sched_type
= ifq
->ifcq_type
;
1880 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1881 sizeof(iflpr
->iflpr_output_sched
));
1883 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1884 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1885 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1887 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1888 sizeof(iflpr
->iflpr_output_tbr_rate
));
1889 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1890 sizeof(iflpr
->iflpr_output_tbr_percent
));
1893 if (ifp
->if_output_sched_model
==
1894 IFNET_SCHED_MODEL_DRIVER_MANAGED
) {
1895 flags
|= IFLPRF_DRVMANAGED
;
1897 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof(iflpr
->iflpr_flags
));
1898 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1899 sizeof(iflpr
->iflpr_output_bw
));
1900 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1901 sizeof(iflpr
->iflpr_input_bw
));
1902 bcopy(&ifp
->if_output_lt
, &iflpr
->iflpr_output_lt
,
1903 sizeof(iflpr
->iflpr_output_lt
));
1904 bcopy(&ifp
->if_input_lt
, &iflpr
->iflpr_input_lt
,
1905 sizeof(iflpr
->iflpr_input_lt
));
1907 if (ifp
->if_output_netem
!= NULL
) {
1908 netem_get_params(ifp
->if_output_netem
,
1909 &iflpr
->iflpr_output_netem
);
1923 static __attribute__((noinline
)) int
1924 ifioctl_qstats(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1926 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1927 u_int32_t ifqr_len
, ifqr_slot
;
1930 VERIFY(ifp
!= NULL
);
1933 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1934 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof(ifqr_slot
));
1935 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof(ifqr_len
));
1936 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1937 ifqr
->ifqr_buf
, &ifqr_len
);
1941 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof(ifqr_len
));
1953 static __attribute__((noinline
)) int
1954 ifioctl_throttle(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1956 struct if_throttlereq
*ifthr
= (struct if_throttlereq
*)(void *)data
;
1957 u_int32_t ifthr_level
;
1960 VERIFY(ifp
!= NULL
);
1963 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1965 * XXX: Use priv_check_cred() instead of root check?
1967 if ((error
= proc_suser(p
)) != 0) {
1971 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof(ifthr_level
));
1972 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1973 if (error
== EALREADY
) {
1979 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1980 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1981 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1982 sizeof(ifthr_level
));
1996 ifioctl_getnetagents(struct ifnet
*ifp
, u_int32_t
*count
, user_addr_t uuid_p
)
1999 u_int32_t index
= 0;
2000 u_int32_t valid_netagent_count
= 0;
2003 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_SHARED
);
2005 if (ifp
->if_agentids
!= NULL
) {
2006 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2007 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2008 if (!uuid_is_null(*netagent_uuid
)) {
2009 if (uuid_p
!= USER_ADDR_NULL
) {
2010 error
= copyout(netagent_uuid
,
2011 uuid_p
+ sizeof(uuid_t
) * valid_netagent_count
,
2017 valid_netagent_count
++;
2021 *count
= valid_netagent_count
;
2026 #define IF_MAXAGENTS 64
2027 #define IF_AGENT_INCREMENT 8
2029 if_add_netagent_locked(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
2031 VERIFY(ifp
!= NULL
);
2033 uuid_t
*first_empty_slot
= NULL
;
2034 u_int32_t index
= 0;
2035 bool already_added
= FALSE
;
2037 if (ifp
->if_agentids
!= NULL
) {
2038 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2039 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2040 if (uuid_compare(*netagent_uuid
, new_agent_uuid
) == 0) {
2041 /* Already present, ignore */
2042 already_added
= TRUE
;
2045 if (first_empty_slot
== NULL
&&
2046 uuid_is_null(*netagent_uuid
)) {
2047 first_empty_slot
= netagent_uuid
;
2051 if (already_added
) {
2052 /* Already added agent, don't return an error */
2055 if (first_empty_slot
== NULL
) {
2056 if (ifp
->if_agentcount
>= IF_MAXAGENTS
) {
2057 /* No room for another netagent UUID, bail */
2060 /* Calculate new array size */
2061 u_int32_t new_agent_count
=
2062 MIN(ifp
->if_agentcount
+ IF_AGENT_INCREMENT
,
2065 /* Reallocate array */
2066 uuid_t
*new_agent_array
= _REALLOC(ifp
->if_agentids
,
2067 sizeof(uuid_t
) * new_agent_count
, M_NETAGENT
,
2069 if (new_agent_array
== NULL
) {
2073 /* Save new array */
2074 ifp
->if_agentids
= new_agent_array
;
2076 /* Set first empty slot */
2078 &(ifp
->if_agentids
[ifp
->if_agentcount
]);
2080 /* Save new array length */
2081 ifp
->if_agentcount
= new_agent_count
;
2084 uuid_copy(*first_empty_slot
, new_agent_uuid
);
2085 netagent_post_updated_interfaces(new_agent_uuid
);
2090 if_add_netagent(struct ifnet
*ifp
, uuid_t new_agent_uuid
)
2092 VERIFY(ifp
!= NULL
);
2094 ifnet_lock_exclusive(ifp
);
2096 int error
= if_add_netagent_locked(ifp
, new_agent_uuid
);
2098 ifnet_lock_done(ifp
);
2104 if_delete_netagent_locked(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2106 u_int32_t index
= 0;
2107 bool removed_agent_id
= FALSE
;
2109 if (ifp
->if_agentids
!= NULL
) {
2110 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2111 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
2112 if (uuid_compare(*netagent_uuid
,
2113 remove_agent_uuid
) == 0) {
2114 uuid_clear(*netagent_uuid
);
2115 removed_agent_id
= TRUE
;
2120 if (removed_agent_id
) {
2121 netagent_post_updated_interfaces(remove_agent_uuid
);
2128 if_delete_netagent(struct ifnet
*ifp
, uuid_t remove_agent_uuid
)
2130 VERIFY(ifp
!= NULL
);
2132 ifnet_lock_exclusive(ifp
);
2134 int error
= if_delete_netagent_locked(ifp
, remove_agent_uuid
);
2136 ifnet_lock_done(ifp
);
2142 if_check_netagent(struct ifnet
*ifp
, uuid_t find_agent_uuid
)
2144 boolean_t found
= FALSE
;
2146 if (!ifp
|| uuid_is_null(find_agent_uuid
)) {
2150 ifnet_lock_shared(ifp
);
2152 if (ifp
->if_agentids
!= NULL
) {
2153 for (uint32_t index
= 0; index
< ifp
->if_agentcount
; index
++) {
2154 if (uuid_compare(ifp
->if_agentids
[index
], find_agent_uuid
) == 0) {
2161 ifnet_lock_done(ifp
);
2166 static __attribute__((noinline
)) int
2167 ifioctl_netagent(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
2169 struct if_agentidreq
*ifar
= (struct if_agentidreq
*)(void *)data
;
2171 struct if_agentidsreq32 s32
;
2172 struct if_agentidsreq64 s64
;
2176 VERIFY(ifp
!= NULL
);
2178 /* Get an io ref count if the interface is attached */
2179 if (!ifnet_is_attached(ifp
, 1)) {
2183 if (cmd
== SIOCAIFAGENTID
||
2184 cmd
== SIOCDIFAGENTID
) {
2185 ifnet_lock_exclusive(ifp
);
2187 ifnet_lock_shared(ifp
);
2191 case SIOCAIFAGENTID
: { /* struct if_agentidreq */
2192 // TODO: Use priv_check_cred() instead of root check
2193 if ((error
= proc_suser(p
)) != 0) {
2196 error
= if_add_netagent_locked(ifp
, ifar
->ifar_uuid
);
2199 case SIOCDIFAGENTID
: { /* struct if_agentidreq */
2200 // TODO: Use priv_check_cred() instead of root check
2201 if ((error
= proc_suser(p
)) != 0) {
2204 error
= if_delete_netagent_locked(ifp
, ifar
->ifar_uuid
);
2207 case SIOCGIFAGENTIDS32
: { /* struct if_agentidsreq32 */
2208 bcopy(data
, &u
.s32
, sizeof(u
.s32
));
2209 error
= ifioctl_getnetagents(ifp
, &u
.s32
.ifar_count
,
2212 bcopy(&u
.s32
, data
, sizeof(u
.s32
));
2216 case SIOCGIFAGENTIDS64
: { /* struct if_agentidsreq64 */
2217 bcopy(data
, &u
.s64
, sizeof(u
.s64
));
2218 error
= ifioctl_getnetagents(ifp
, &u
.s64
.ifar_count
,
2221 bcopy(&u
.s64
, data
, sizeof(u
.s64
));
2230 ifnet_lock_done(ifp
);
2231 ifnet_decr_iorefcnt(ifp
);
2237 ifnet_clear_netagent(uuid_t netagent_uuid
)
2239 struct ifnet
*ifp
= NULL
;
2240 u_int32_t index
= 0;
2242 ifnet_head_lock_shared();
2244 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2245 ifnet_lock_shared(ifp
);
2246 if (ifp
->if_agentids
!= NULL
) {
2247 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
2248 uuid_t
*ifp_netagent_uuid
= &(ifp
->if_agentids
[index
]);
2249 if (uuid_compare(*ifp_netagent_uuid
, netagent_uuid
) == 0) {
2250 uuid_clear(*ifp_netagent_uuid
);
2254 ifnet_lock_done(ifp
);
2261 ifnet_increment_generation(ifnet_t interface
)
2263 OSIncrementAtomic(&interface
->if_generation
);
2267 ifnet_get_generation(ifnet_t interface
)
2269 return interface
->if_generation
;
2273 ifnet_remove_from_ordered_list(struct ifnet
*ifp
)
2275 ifnet_head_assert_exclusive();
2278 TAILQ_REMOVE(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2279 ifp
->if_ordered_link
.tqe_next
= NULL
;
2280 ifp
->if_ordered_link
.tqe_prev
= NULL
;
2282 // Update ordered count
2283 VERIFY(if_ordered_count
> 0);
2288 ifnet_reset_order(u_int32_t
*ordered_indices
, u_int32_t count
)
2290 struct ifnet
*ifp
= NULL
;
2293 ifnet_head_lock_exclusive();
2294 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2295 if (ordered_indices
[order_index
] == IFSCOPE_NONE
||
2296 ordered_indices
[order_index
] > (uint32_t)if_index
) {
2302 // Flush current ordered list
2303 for (ifp
= TAILQ_FIRST(&ifnet_ordered_head
); ifp
!= NULL
;
2304 ifp
= TAILQ_FIRST(&ifnet_ordered_head
)) {
2305 ifnet_lock_exclusive(ifp
);
2306 ifnet_remove_from_ordered_list(ifp
);
2307 ifnet_lock_done(ifp
);
2310 VERIFY(if_ordered_count
== 0);
2312 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2313 u_int32_t interface_index
= ordered_indices
[order_index
];
2314 ifp
= ifindex2ifnet
[interface_index
];
2318 ifnet_lock_exclusive(ifp
);
2319 TAILQ_INSERT_TAIL(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2320 ifnet_lock_done(ifp
);
2326 necp_update_all_clients();
2332 if_set_qosmarking_mode(struct ifnet
*ifp
, u_int32_t mode
)
2335 u_int32_t old_mode
= ifp
->if_qosmarking_mode
;
2338 case IFRTYPE_QOSMARKING_MODE_NONE
:
2339 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
2341 case IFRTYPE_QOSMARKING_FASTLANE
:
2342 case IFRTYPE_QOSMARKING_RFC4594
:
2343 ifp
->if_qosmarking_mode
= mode
;
2349 if (error
== 0 && old_mode
!= ifp
->if_qosmarking_mode
) {
2350 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_QOS_MODE_CHANGED
,
2356 static __attribute__((noinline
)) int
2357 ifioctl_iforder(u_long cmd
, caddr_t data
)
2360 u_int32_t
*ordered_indices
= NULL
;
2366 case SIOCSIFORDER
: { /* struct if_order */
2367 struct if_order
*ifo
= (struct if_order
*)(void *)data
;
2369 if (ifo
->ifo_count
> (u_int32_t
)if_index
) {
2374 size_t length
= (ifo
->ifo_count
* sizeof(u_int32_t
));
2376 if (ifo
->ifo_ordered_indices
== USER_ADDR_NULL
) {
2380 ordered_indices
= _MALLOC(length
, M_NECP
, M_WAITOK
);
2381 if (ordered_indices
== NULL
) {
2386 error
= copyin(ifo
->ifo_ordered_indices
,
2387 ordered_indices
, length
);
2392 /* ordered_indices should not contain duplicates */
2393 bool found_duplicate
= FALSE
;
2394 for (uint32_t i
= 0; i
< (ifo
->ifo_count
- 1) && !found_duplicate
; i
++) {
2395 for (uint32_t j
= i
+ 1; j
< ifo
->ifo_count
&& !found_duplicate
; j
++) {
2396 if (ordered_indices
[j
] == ordered_indices
[i
]) {
2398 found_duplicate
= TRUE
;
2403 if (found_duplicate
) {
2407 error
= ifnet_reset_order(ordered_indices
, ifo
->ifo_count
);
2410 error
= ifnet_reset_order(NULL
, 0);
2421 if (ordered_indices
!= NULL
) {
2422 _FREE(ordered_indices
, M_NECP
);
2428 static __attribute__((noinline
)) int
2429 ifioctl_networkid(struct ifnet
*ifp
, caddr_t data
)
2431 struct if_netidreq
*ifnetidr
= (struct if_netidreq
*)(void *)data
;
2433 int len
= ifnetidr
->ifnetid_len
;
2435 VERIFY(ifp
!= NULL
);
2437 if (len
> sizeof(ifnetidr
->ifnetid
)) {
2443 bzero(&ifp
->network_id
, sizeof(ifp
->network_id
));
2444 } else if (len
> sizeof(ifp
->network_id
)) {
2449 ifp
->network_id_len
= len
;
2450 bcopy(data
, ifp
->network_id
, len
);
2455 static __attribute__((noinline
)) int
2456 ifioctl_netsignature(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2458 struct if_nsreq
*ifnsr
= (struct if_nsreq
*)(void *)data
;
2462 VERIFY(ifp
!= NULL
);
2465 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2466 if (ifnsr
->ifnsr_len
> sizeof(ifnsr
->ifnsr_data
)) {
2470 bcopy(&ifnsr
->ifnsr_flags
, &flags
, sizeof(flags
));
2471 error
= ifnet_set_netsignature(ifp
, ifnsr
->ifnsr_family
,
2472 ifnsr
->ifnsr_len
, flags
, ifnsr
->ifnsr_data
);
2475 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2476 ifnsr
->ifnsr_len
= sizeof(ifnsr
->ifnsr_data
);
2477 error
= ifnet_get_netsignature(ifp
, ifnsr
->ifnsr_family
,
2478 &ifnsr
->ifnsr_len
, &flags
, ifnsr
->ifnsr_data
);
2480 bcopy(&flags
, &ifnsr
->ifnsr_flags
, sizeof(flags
));
2482 ifnsr
->ifnsr_len
= 0;
2494 static __attribute__((noinline
)) int
2495 ifioctl_nat64prefix(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2497 struct if_nat64req
*ifnat64
= (struct if_nat64req
*)(void *)data
;
2500 VERIFY(ifp
!= NULL
);
2503 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
2504 error
= ifnet_set_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2506 ip6stat
.ip6s_clat464_plat64_pfx_setfail
++;
2510 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
2511 error
= ifnet_get_nat64prefix(ifp
, ifnat64
->ifnat64_prefixes
);
2513 ip6stat
.ip6s_clat464_plat64_pfx_getfail
++;
2525 static __attribute__((noinline
)) int
2526 ifioctl_clat46addr(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2528 struct if_clat46req
*ifclat46
= (struct if_clat46req
*)(void *)data
;
2529 struct in6_ifaddr
*ia6_clat
= NULL
;
2532 VERIFY(ifp
!= NULL
);
2535 case SIOCGIFCLAT46ADDR
:
2536 ia6_clat
= in6ifa_ifpwithflag(ifp
, IN6_IFF_CLAT46
);
2537 if (ia6_clat
== NULL
) {
2542 bcopy(&ia6_clat
->ia_addr
.sin6_addr
, &ifclat46
->ifclat46_addr
.v6_address
,
2543 sizeof(ifclat46
->ifclat46_addr
.v6_address
));
2544 ifclat46
->ifclat46_addr
.v6_prefixlen
= ia6_clat
->ia_plen
;
2545 IFA_REMREF(&ia6_clat
->ia_ifa
);
2557 ifioctl_get_protolist(struct ifnet
*ifp
, u_int32_t
* ret_count
,
2560 u_int32_t actual_count
;
2563 u_int32_t
*list
= NULL
;
2565 /* find out how many */
2566 count
= if_get_protolist(ifp
, NULL
, 0);
2567 if (ifpl
== USER_ADDR_NULL
) {
2571 /* copy out how many there's space for */
2572 if (*ret_count
< count
) {
2578 list
= _MALLOC(count
* sizeof(*list
), M_TEMP
, M_WAITOK
| M_ZERO
);
2583 actual_count
= if_get_protolist(ifp
, list
, count
);
2584 if (actual_count
< count
) {
2585 count
= actual_count
;
2588 error
= copyout((caddr_t
)list
, ifpl
, count
* sizeof(*list
));
2593 if_free_protolist(list
);
2599 static __attribute__((noinline
)) int
2600 ifioctl_protolist(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2605 case SIOCGIFPROTOLIST32
: { /* struct if_protolistreq32 */
2606 struct if_protolistreq32 ifpl
;
2608 bcopy(data
, &ifpl
, sizeof(ifpl
));
2609 if (ifpl
.ifpl_reserved
!= 0) {
2613 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2614 CAST_USER_ADDR_T(ifpl
.ifpl_list
));
2615 bcopy(&ifpl
, data
, sizeof(ifpl
));
2618 case SIOCGIFPROTOLIST64
: { /* struct if_protolistreq64 */
2619 struct if_protolistreq64 ifpl
;
2621 bcopy(data
, &ifpl
, sizeof(ifpl
));
2622 if (ifpl
.ifpl_reserved
!= 0) {
2626 error
= ifioctl_get_protolist(ifp
, &ifpl
.ifpl_count
,
2628 bcopy(&ifpl
, data
, sizeof(ifpl
));
2640 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2643 ifioctl_restrict_intcoproc(unsigned long cmd
, const char *ifname
,
2644 struct ifnet
*ifp
, struct proc
*p
)
2646 if (intcoproc_unrestricted
== TRUE
) {
2649 if (proc_pid(p
) == 0) {
2653 ifp
= ifunit(ifname
);
2658 if (!IFNET_IS_INTCOPROC(ifp
)) {
2662 case SIOCGIFBRDADDR
:
2672 case SIOCGIFFUNCTIONALTYPE
:
2673 case SIOCGIFPSRCADDR
:
2674 case SIOCGIFPDSTADDR
:
2675 case SIOCGIFGENERIC
:
2679 case SIOCGIFWAKEFLAGS
:
2680 case SIOCGIFGETRTREFCNT
:
2681 case SIOCGIFOPPORTUNISTIC
:
2682 case SIOCGIFLINKQUALITYMETRIC
:
2684 case SIOCGIFDELEGATE
:
2685 case SIOCGIFEXPENSIVE
:
2686 case SIOCGIFINTERFACESTATE
:
2687 case SIOCGIFPROBECONNECTIVITY
:
2688 case SIOCGIFTIMESTAMPENABLED
:
2690 case SIOCGQOSMARKINGMODE
:
2691 case SIOCGQOSMARKINGENABLED
:
2692 case SIOCGIFLOWINTERNET
:
2694 case SIOCGIFMEDIA32
:
2695 case SIOCGIFMEDIA64
:
2696 case SIOCGIFXMEDIA32
:
2697 case SIOCGIFXMEDIA64
:
2699 case SIOCGIFLINKPARAMS
:
2700 case SIOCGIFQUEUESTATS
:
2701 case SIOCGIFTHROTTLE
:
2702 case SIOCGIFAGENTIDS32
:
2703 case SIOCGIFAGENTIDS64
:
2704 case SIOCGIFNETSIGNATURE
:
2705 case SIOCGIFINFO_IN6
:
2706 case SIOCGIFAFLAG_IN6
:
2707 case SIOCGNBRINFO_IN6
:
2708 case SIOCGIFALIFETIME_IN6
:
2709 case SIOCGIFNETMASK_IN6
:
2710 case SIOCGIFPROTOLIST32
:
2711 case SIOCGIFPROTOLIST64
:
2715 #if (DEBUG || DEVELOPMENT)
2716 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2717 __func__
, cmd
, proc_pid(p
));
2725 * Given a media word, return one suitable for an application
2726 * using the original encoding.
2729 compat_media(int media
)
2731 if (IFM_TYPE(media
) == IFM_ETHER
&& IFM_SUBTYPE(media
) > IFM_OTHER
) {
2732 media
&= ~IFM_TMASK
;
2739 compat_ifmu_ulist(struct ifnet
*ifp
, u_long cmd
, void *data
)
2741 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2742 user_addr_t user_addr
;
2744 int *media_list
= NULL
;
2746 bool list_modified
= false;
2748 user_addr
= (cmd
== SIOCGIFMEDIA64
) ?
2749 ((struct ifmediareq64
*)ifmr
)->ifmu_ulist
:
2750 CAST_USER_ADDR_T(((struct ifmediareq32
*)ifmr
)->ifmu_ulist
);
2751 if (user_addr
== USER_ADDR_NULL
|| ifmr
->ifm_count
== 0) {
2754 MALLOC(media_list
, int *, ifmr
->ifm_count
* sizeof(int),
2755 M_TEMP
, M_WAITOK
| M_ZERO
);
2756 if (media_list
== NULL
) {
2757 os_log_error(OS_LOG_DEFAULT
,
2758 "%s: %s MALLOC() failed",
2759 __func__
, ifp
->if_xname
);
2763 error
= copyin(user_addr
, media_list
, ifmr
->ifm_count
* sizeof(int));
2765 os_log_error(OS_LOG_DEFAULT
,
2766 "%s: %s copyin() error %d",
2767 __func__
, ifp
->if_xname
, error
);
2770 for (i
= 0; i
< ifmr
->ifm_count
; i
++) {
2771 int old_media
, new_media
;
2773 old_media
= media_list
[i
];
2775 new_media
= compat_media(old_media
);
2776 if (new_media
== old_media
) {
2779 if (if_verbose
!= 0) {
2780 os_log_info(OS_LOG_DEFAULT
,
2781 "%s: %s converted extended media %08x to compat media %08x",
2782 __func__
, ifp
->if_xname
, old_media
, new_media
);
2784 media_list
[i
] = new_media
;
2785 list_modified
= true;
2787 if (list_modified
) {
2788 error
= copyout(media_list
, user_addr
, ifmr
->ifm_count
* sizeof(int));
2790 os_log_error(OS_LOG_DEFAULT
,
2791 "%s: %s copyout() error %d",
2792 __func__
, ifp
->if_xname
, error
);
2797 if (media_list
!= NULL
) {
2798 FREE(media_list
, M_TEMP
);
2804 compat_ifmediareq(struct ifnet
*ifp
, u_long cmd
, void *data
)
2806 struct ifmediareq
*ifmr
= (struct ifmediareq
*)data
;
2809 ifmr
->ifm_active
= compat_media(ifmr
->ifm_active
);
2810 ifmr
->ifm_current
= compat_media(ifmr
->ifm_current
);
2812 error
= compat_ifmu_ulist(ifp
, cmd
, data
);
2818 ifioctl_get_media(struct ifnet
*ifp
, struct socket
*so
, u_long cmd
, caddr_t data
)
2823 * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2824 * media subtypes macros from <net/if_media.h>
2827 case SIOCGIFMEDIA32
:
2828 case SIOCGIFXMEDIA32
:
2829 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA32
, data
);
2831 case SIOCGIFMEDIA64
:
2832 case SIOCGIFXMEDIA64
:
2833 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), SIOCGIFMEDIA64
, data
);
2836 if (if_verbose
!= 0 && error
!= 0) {
2837 os_log(OS_LOG_DEFAULT
, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2838 __func__
, ifp
->if_xname
, cmd
, error
);
2840 if (error
== 0 && (cmd
== SIOCGIFMEDIA32
|| cmd
== SIOCGIFMEDIA64
)) {
2841 error
= compat_ifmediareq(ifp
, cmd
, data
);
2848 * Most of the routines called to handle the ioctls would end up being
2849 * tail-call optimized, which unfortunately causes this routine to
2850 * consume too much stack space; this is the reason for the "noinline"
2851 * attribute used on those routines.
2854 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2856 char ifname
[IFNAMSIZ
+ 1];
2857 struct ifnet
*ifp
= NULL
;
2858 struct ifstat
*ifs
= NULL
;
2861 bzero(ifname
, sizeof(ifname
));
2864 * ioctls which don't require ifp, or ifreq ioctls
2867 case OSIOCGIFCONF32
: /* struct ifconf32 */
2868 case SIOCGIFCONF32
: /* struct ifconf32 */
2869 case SIOCGIFCONF64
: /* struct ifconf64 */
2870 case OSIOCGIFCONF64
: /* struct ifconf64 */
2871 error
= ifioctl_ifconf(cmd
, data
);
2874 case SIOCIFGCLONERS32
: /* struct if_clonereq32 */
2875 case SIOCIFGCLONERS64
: /* struct if_clonereq64 */
2876 error
= ifioctl_ifclone(cmd
, data
);
2879 case SIOCGIFAGENTDATA32
: /* struct netagent_req32 */
2880 case SIOCGIFAGENTDATA64
: /* struct netagent_req64 */
2881 case SIOCGIFAGENTLIST32
: /* struct netagentlist_req32 */
2882 case SIOCGIFAGENTLIST64
: /* struct netagentlist_req64 */
2883 error
= netagent_ioctl(cmd
, data
);
2886 case SIOCSIFORDER
: /* struct if_order */
2887 error
= ifioctl_iforder(cmd
, data
);
2890 case SIOCSIFDSTADDR
: /* struct ifreq */
2891 case SIOCSIFADDR
: /* struct ifreq */
2892 case SIOCSIFBRDADDR
: /* struct ifreq */
2893 case SIOCSIFNETMASK
: /* struct ifreq */
2894 case OSIOCGIFADDR
: /* struct ifreq */
2895 case OSIOCGIFDSTADDR
: /* struct ifreq */
2896 case OSIOCGIFBRDADDR
: /* struct ifreq */
2897 case OSIOCGIFNETMASK
: /* struct ifreq */
2898 case SIOCSIFKPI
: /* struct ifreq */
2899 if (so
->so_proto
== NULL
) {
2904 case SIOCIFCREATE
: /* struct ifreq */
2905 case SIOCIFCREATE2
: /* struct ifreq */
2906 case SIOCIFDESTROY
: /* struct ifreq */
2907 case SIOCGIFFLAGS
: /* struct ifreq */
2908 case SIOCGIFEFLAGS
: /* struct ifreq */
2909 case SIOCGIFCAP
: /* struct ifreq */
2910 case SIOCGIFMETRIC
: /* struct ifreq */
2911 case SIOCGIFMTU
: /* struct ifreq */
2912 case SIOCGIFPHYS
: /* struct ifreq */
2913 case SIOCSIFFLAGS
: /* struct ifreq */
2914 case SIOCSIFCAP
: /* struct ifreq */
2915 case SIOCSIFMETRIC
: /* struct ifreq */
2916 case SIOCSIFPHYS
: /* struct ifreq */
2917 case SIOCSIFMTU
: /* struct ifreq */
2918 case SIOCADDMULTI
: /* struct ifreq */
2919 case SIOCDELMULTI
: /* struct ifreq */
2920 case SIOCDIFPHYADDR
: /* struct ifreq */
2921 case SIOCSIFMEDIA
: /* struct ifreq */
2922 case SIOCSIFGENERIC
: /* struct ifreq */
2923 case SIOCSIFLLADDR
: /* struct ifreq */
2924 case SIOCSIFALTMTU
: /* struct ifreq */
2925 case SIOCSIFVLAN
: /* struct ifreq */
2926 case SIOCSIFBOND
: /* struct ifreq */
2927 case SIOCGIFLLADDR
: /* struct ifreq */
2928 case SIOCGIFTYPE
: /* struct ifreq */
2929 case SIOCGIFFUNCTIONALTYPE
: /* struct ifreq */
2930 case SIOCGIFPSRCADDR
: /* struct ifreq */
2931 case SIOCGIFPDSTADDR
: /* struct ifreq */
2932 case SIOCGIFGENERIC
: /* struct ifreq */
2933 case SIOCGIFDEVMTU
: /* struct ifreq */
2934 case SIOCGIFVLAN
: /* struct ifreq */
2935 case SIOCGIFBOND
: /* struct ifreq */
2936 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
2937 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
2938 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
2939 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
2940 case SIOCGIFLINKQUALITYMETRIC
: /* struct ifreq */
2941 case SIOCSIFLOG
: /* struct ifreq */
2942 case SIOCGIFLOG
: /* struct ifreq */
2943 case SIOCGIFDELEGATE
: /* struct ifreq */
2944 case SIOCGIFEXPENSIVE
: /* struct ifreq */
2945 case SIOCSIFEXPENSIVE
: /* struct ifreq */
2946 case SIOCSIF2KCL
: /* struct ifreq */
2947 case SIOCGIF2KCL
: /* struct ifreq */
2948 case SIOCSIFINTERFACESTATE
: /* struct ifreq */
2949 case SIOCGIFINTERFACESTATE
: /* struct ifreq */
2950 case SIOCSIFPROBECONNECTIVITY
: /* struct ifreq */
2951 case SIOCGIFPROBECONNECTIVITY
: /* struct ifreq */
2952 case SIOCGSTARTDELAY
: /* struct ifreq */
2953 case SIOCSIFTIMESTAMPENABLE
: /* struct ifreq */
2954 case SIOCSIFTIMESTAMPDISABLE
: /* struct ifreq */
2955 case SIOCGIFTIMESTAMPENABLED
: /* struct ifreq */
2956 #if (DEBUG || DEVELOPMENT)
2957 case SIOCSIFDISABLEOUTPUT
: /* struct ifreq */
2958 #endif /* (DEBUG || DEVELOPMENT) */
2959 case SIOCGECNMODE
: /* struct ifreq */
2961 case SIOCSQOSMARKINGMODE
: /* struct ifreq */
2962 case SIOCSQOSMARKINGENABLED
: /* struct ifreq */
2963 case SIOCGQOSMARKINGMODE
: /* struct ifreq */
2964 case SIOCGQOSMARKINGENABLED
: /* struct ifreq */
2965 case SIOCSIFLOWINTERNET
: /* struct ifreq */
2966 case SIOCGIFLOWINTERNET
: /* struct ifreq */
2967 case SIOCGIFLOWPOWER
: /* struct ifreq */
2968 case SIOCSIFLOWPOWER
: /* struct ifreq */
2969 case SIOCSIF6LOWPAN
: /* struct ifreq */
2970 case SIOCGIF6LOWPAN
: /* struct ifreq */
2971 case SIOCGIFMPKLOG
: /* struct ifreq */
2972 case SIOCSIFMPKLOG
: /* struct ifreq */
2973 case SIOCGIFCONSTRAINED
: /* struct ifreq */
2974 case SIOCSIFCONSTRAINED
: /* struct ifreq */
2975 case SIOCGIFXFLAGS
: /* struct ifreq */
2976 case SIOCGIFNOACKPRIO
: /* struct ifreq */
2977 case SIOCSIFNOACKPRIO
: /* struct ifreq */
2978 { /* struct ifreq */
2980 bcopy(data
, &ifr
, sizeof(ifr
));
2981 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2982 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2983 if (ifioctl_restrict_intcoproc(cmd
, ifname
, NULL
, p
) == true) {
2987 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2988 bcopy(&ifr
, data
, sizeof(ifr
));
2994 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2995 * here to ensure that the ifnet, if found, has been fully attached.
2999 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
3000 bcopy(((struct in_aliasreq
*)(void *)data
)->ifra_name
,
3002 ifp
= ifunit_ref(ifname
);
3005 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
3006 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
3008 ifp
= ifunit_ref(ifname
);
3011 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
3012 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
3014 ifp
= ifunit_ref(ifname
);
3017 case SIOCGIFSTATUS
: /* struct ifstat */
3018 ifs
= _MALLOC(sizeof(*ifs
), M_DEVBUF
, M_WAITOK
);
3024 bcopy(data
, ifs
, sizeof(*ifs
));
3025 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
3026 bcopy(ifs
->ifs_name
, ifname
, IFNAMSIZ
);
3027 ifp
= ifunit_ref(ifname
);
3030 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
3031 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
3032 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
3034 ifp
= ifunit_ref(ifname
);
3037 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
3038 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
3039 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
3041 ifp
= ifunit_ref(ifname
);
3044 case SIOCSIFDESC
: /* struct if_descreq */
3045 case SIOCGIFDESC
: /* struct if_descreq */
3046 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
3048 ifp
= ifunit_ref(ifname
);
3051 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
3052 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
3053 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
3055 ifp
= ifunit_ref(ifname
);
3058 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
3059 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
3061 ifp
= ifunit_ref(ifname
);
3064 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
3065 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
3066 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
3068 ifp
= ifunit_ref(ifname
);
3071 case SIOCAIFAGENTID
: /* struct if_agentidreq */
3072 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3073 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3074 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3075 bcopy(((struct if_agentidreq
*)(void *)data
)->ifar_name
,
3077 ifp
= ifunit_ref(ifname
);
3080 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3081 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3082 bcopy(((struct if_nsreq
*)(void *)data
)->ifnsr_name
,
3084 ifp
= ifunit_ref(ifname
);
3087 case SIOCSIFNETWORKID
: /* struct if_netidreq */
3088 bcopy(((struct if_netidreq
*)(void *)data
)->ifnetid_name
,
3090 ifp
= ifunit_ref(ifname
);
3092 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3093 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3094 bcopy(((struct if_protolistreq
*)(void *)data
)->ifpl_name
,
3096 ifp
= ifunit_ref(ifname
);
3100 * This is a bad assumption, but the code seems to
3101 * have been doing this in the past; caveat emptor.
3103 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
3105 ifp
= ifunit_ref(ifname
);
3115 if (ifioctl_restrict_intcoproc(cmd
, NULL
, ifp
, p
) == true) {
3120 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
3121 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
3122 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
3123 error
= proc_suser(p
);
3128 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3133 ifnet_touch_lastchange(ifp
);
3136 case SIOCGIFSTATUS
: /* struct ifstat */
3137 VERIFY(ifs
!= NULL
);
3138 ifs
->ascii
[0] = '\0';
3140 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifs
);
3142 bcopy(ifs
, data
, sizeof(*ifs
));
3145 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
3146 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
3147 case SIOCGIFXMEDIA32
: /* struct ifmediareq32 */
3148 case SIOCGIFXMEDIA64
: /* struct ifmediareq64 */
3149 error
= ifioctl_get_media(ifp
, so
, cmd
, data
);
3152 case SIOCSIFDESC
: /* struct if_descreq */
3153 case SIOCGIFDESC
: /* struct if_descreq */
3154 error
= ifioctl_ifdesc(ifp
, cmd
, data
, p
);
3157 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
3158 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
3159 error
= ifioctl_linkparams(ifp
, cmd
, data
, p
);
3162 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
3163 error
= ifioctl_qstats(ifp
, cmd
, data
);
3166 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
3167 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
3168 error
= ifioctl_throttle(ifp
, cmd
, data
, p
);
3171 case SIOCAIFAGENTID
: /* struct if_agentidreq */
3172 case SIOCDIFAGENTID
: /* struct if_agentidreq */
3173 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
3174 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
3175 error
= ifioctl_netagent(ifp
, cmd
, data
, p
);
3178 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
3179 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
3180 error
= ifioctl_netsignature(ifp
, cmd
, data
);
3183 case SIOCSIFNETWORKID
: /* struct if_netidreq */
3184 error
= ifioctl_networkid(ifp
, data
);
3186 case SIOCSIFNAT64PREFIX
: /* struct if_nat64req */
3187 case SIOCGIFNAT64PREFIX
: /* struct if_nat64req */
3188 error
= ifioctl_nat64prefix(ifp
, cmd
, data
);
3191 case SIOCGIFCLAT46ADDR
: /* struct if_clat46req */
3192 error
= ifioctl_clat46addr(ifp
, cmd
, data
);
3195 case SIOCGIFPROTOLIST32
: /* struct if_protolistreq32 */
3196 case SIOCGIFPROTOLIST64
: /* struct if_protolistreq64 */
3197 error
= ifioctl_protolist(ifp
, cmd
, data
);
3201 if (so
->so_proto
== NULL
) {
3207 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3209 socket_unlock(so
, 1);
3211 // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3212 // ifreq as the code expects ifaddr
3213 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3214 !(cmd
== SIOCAIFADDR
|| cmd
== SIOCDIFADDR
)) {
3215 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
3222 _FREE(ifs
, M_DEVBUF
);
3226 if (ifname
[0] == '\0') {
3227 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3229 } else if (ifp
!= NULL
) {
3230 (void) snprintf(ifname
, sizeof(ifname
), "%s",
3235 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3236 "%c %lu) error %d\n", __func__
,
3237 proc_name_address(p
), proc_pid(p
),
3238 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3239 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3240 (char)IOCGROUP(cmd
), cmd
& 0xff, error
);
3241 } else if (if_verbose
> 1) {
3242 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3243 "%c %lu) OK\n", __func__
,
3244 proc_name_address(p
), proc_pid(p
),
3245 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
3246 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
3247 (char)IOCGROUP(cmd
), cmd
& 0xff);
3252 ifnet_decr_iorefcnt(ifp
);
3257 static __attribute__((noinline
)) int
3258 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
3263 struct kev_msg ev_msg
;
3264 struct net_event_data ev_data
;
3266 bzero(&ev_data
, sizeof(struct net_event_data
));
3267 bzero(&ev_msg
, sizeof(struct kev_msg
));
3272 error
= proc_suser(p
);
3276 return if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
3277 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
);
3279 error
= proc_suser(p
);
3283 return if_clone_destroy(ifr
->ifr_name
);
3287 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
3288 * here to ensure that the ifnet, if found, has been fully attached.
3291 ifp
= ifunit(ifr
->ifr_name
);
3300 ifnet_lock_shared(ifp
);
3301 ifr
->ifr_flags
= ifp
->if_flags
;
3302 ifnet_lock_done(ifp
);
3306 ifnet_lock_shared(ifp
);
3307 ifr
->ifr_eflags
= ifp
->if_eflags
;
3308 ifnet_lock_done(ifp
);
3312 ifnet_lock_shared(ifp
);
3313 ifr
->ifr_xflags
= ifp
->if_xflags
;
3314 ifnet_lock_done(ifp
);
3318 ifnet_lock_shared(ifp
);
3319 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
3320 ifr
->ifr_curcap
= ifp
->if_capenable
;
3321 ifnet_lock_done(ifp
);
3325 ifnet_lock_shared(ifp
);
3326 ifr
->ifr_metric
= ifp
->if_metric
;
3327 ifnet_lock_done(ifp
);
3331 ifnet_lock_shared(ifp
);
3332 ifr
->ifr_mtu
= ifp
->if_mtu
;
3333 ifnet_lock_done(ifp
);
3337 ifnet_lock_shared(ifp
);
3338 ifr
->ifr_phys
= ifp
->if_physical
;
3339 ifnet_lock_done(ifp
);
3343 error
= proc_suser(p
);
3348 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
3349 (u_int16_t
)~IFF_CANTCHANGE
);
3352 * Note that we intentionally ignore any error from below
3353 * for the SIOCSIFFLAGS case.
3355 (void) ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3358 * Send the event even upon error from the driver because
3359 * we changed the flags.
3361 dlil_post_sifflags_msg(ifp
);
3363 ifnet_touch_lastchange(ifp
);
3367 error
= proc_suser(p
);
3372 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
3376 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3378 ifnet_touch_lastchange(ifp
);
3382 error
= proc_suser(p
);
3387 ifp
->if_metric
= ifr
->ifr_metric
;
3389 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3390 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3391 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3393 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
3394 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3395 ev_data
.if_family
= ifp
->if_family
;
3396 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3397 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3398 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3400 ev_msg
.dv
[1].data_length
= 0;
3401 dlil_post_complete_msg(ifp
, &ev_msg
);
3403 ifnet_touch_lastchange(ifp
);
3407 error
= proc_suser(p
);
3412 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3417 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3418 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3419 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3421 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
3422 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3423 ev_data
.if_family
= ifp
->if_family
;
3424 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3425 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3426 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3427 ev_msg
.dv
[1].data_length
= 0;
3428 dlil_post_complete_msg(ifp
, &ev_msg
);
3430 ifnet_touch_lastchange(ifp
);
3434 u_int32_t oldmtu
= ifp
->if_mtu
;
3435 struct ifclassq
*ifq
= &ifp
->if_snd
;
3437 error
= proc_suser(p
);
3442 if (ifp
->if_ioctl
== NULL
) {
3446 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
3450 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3455 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3456 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3457 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3459 ev_msg
.event_code
= KEV_DL_SIFMTU
;
3460 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3461 ev_data
.if_family
= ifp
->if_family
;
3462 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3463 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3464 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3465 ev_msg
.dv
[1].data_length
= 0;
3466 dlil_post_complete_msg(ifp
, &ev_msg
);
3468 ifnet_touch_lastchange(ifp
);
3472 * If the link MTU changed, do network layer specific procedure
3473 * and update all route entries associated with the interface,
3474 * so that their MTU metric gets updated.
3476 if (ifp
->if_mtu
!= oldmtu
) {
3477 if_rtmtu_update(ifp
);
3479 /* Inform all transmit queues about the new MTU */
3481 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
3489 error
= proc_suser(p
);
3494 /* Don't allow group membership on non-multicast interfaces. */
3495 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
3500 /* Don't let users screw up protocols' entries. */
3501 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
3502 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
3506 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
3507 ifr
->ifr_addr
.sa_len
= sizeof(struct sockaddr
);
3511 * User is permitted to anonymously join a particular link
3512 * multicast group via SIOCADDMULTI. Subsequent join requested
3513 * for the same record which has an outstanding refcnt from a
3514 * past if_addmulti_anon() will not result in EADDRINUSE error
3515 * (unlike other BSDs.) Anonymously leaving a group is also
3516 * allowed only as long as there is an outstanding refcnt held
3517 * by a previous anonymous request, or else ENOENT (even if the
3518 * link-layer multicast membership exists for a network-layer
3521 if (cmd
== SIOCADDMULTI
) {
3522 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
3523 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
3525 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
3526 ev_msg
.event_code
= KEV_DL_DELMULTI
;
3532 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3533 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3534 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
3535 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
3537 ev_data
.if_family
= ifp
->if_family
;
3538 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3539 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
3540 ev_msg
.dv
[0].data_ptr
= &ev_data
;
3541 ev_msg
.dv
[1].data_length
= 0;
3542 dlil_post_complete_msg(ifp
, &ev_msg
);
3544 ifnet_touch_lastchange(ifp
);
3548 error
= proc_suser(p
);
3553 * Silently ignore setting IFM_OTHER
3555 if (ifr
->ifr_media
== IFM_OTHER
) {
3556 os_log_info(OS_LOG_DEFAULT
,
3557 "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3558 __func__
, ifp
->if_xname
);
3562 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3566 ifnet_touch_lastchange(ifp
);
3569 case SIOCDIFPHYADDR
:
3570 case SIOCSIFGENERIC
:
3575 case SIOCSIF6LOWPAN
:
3576 error
= proc_suser(p
);
3581 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3586 ifnet_touch_lastchange(ifp
);
3589 case SIOCGIFLLADDR
: {
3590 struct sockaddr_dl
*sdl
= SDL(ifp
->if_lladdr
->ifa_addr
);
3592 if (sdl
->sdl_alen
== 0) {
3593 error
= EADDRNOTAVAIL
;
3596 /* If larger than 14-bytes we'll need another mechanism */
3597 if (sdl
->sdl_alen
> sizeof(ifr
->ifr_addr
.sa_data
)) {
3601 /* Follow the same convention used by SIOCSIFLLADDR */
3602 bzero(&ifr
->ifr_addr
, sizeof(ifr
->ifr_addr
));
3603 ifr
->ifr_addr
.sa_family
= AF_LINK
;
3604 ifr
->ifr_addr
.sa_len
= sdl
->sdl_alen
;
3605 error
= ifnet_guarded_lladdr_copy_bytes(ifp
,
3606 &ifr
->ifr_addr
.sa_data
, sdl
->sdl_alen
);
3611 ifr
->ifr_type
.ift_type
= ifp
->if_type
;
3612 ifr
->ifr_type
.ift_family
= ifp
->if_family
;
3613 ifr
->ifr_type
.ift_subfamily
= ifp
->if_subfamily
;
3616 case SIOCGIFFUNCTIONALTYPE
:
3617 ifr
->ifr_functional_type
= if_functional_type(ifp
, FALSE
);
3620 case SIOCGIFPSRCADDR
:
3621 case SIOCGIFPDSTADDR
:
3622 case SIOCGIFGENERIC
:
3626 case SIOCGIF6LOWPAN
:
3627 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
3630 case SIOCGIFWAKEFLAGS
:
3631 ifnet_lock_shared(ifp
);
3632 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
3633 ifnet_lock_done(ifp
);
3636 case SIOCGIFGETRTREFCNT
:
3637 ifnet_lock_shared(ifp
);
3638 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
3639 ifnet_lock_done(ifp
);
3642 case SIOCSIFOPPORTUNISTIC
:
3643 case SIOCGIFOPPORTUNISTIC
:
3644 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
3647 case SIOCGIFLINKQUALITYMETRIC
:
3648 ifnet_lock_shared(ifp
);
3649 if ((ifp
->if_interface_state
.valid_bitmask
&
3650 IF_INTERFACE_STATE_LQM_STATE_VALID
)) {
3651 ifr
->ifr_link_quality_metric
=
3652 ifp
->if_interface_state
.lqm_state
;
3653 } else if (IF_FULLY_ATTACHED(ifp
)) {
3654 ifr
->ifr_link_quality_metric
=
3655 IFNET_LQM_THRESH_UNKNOWN
;
3657 ifr
->ifr_link_quality_metric
=
3658 IFNET_LQM_THRESH_OFF
;
3660 ifnet_lock_done(ifp
);
3665 error
= ifnet_getset_log(ifp
, cmd
, ifr
, p
);
3668 case SIOCGIFDELEGATE
:
3669 ifnet_lock_shared(ifp
);
3670 ifr
->ifr_delegated
= ((ifp
->if_delegated
.ifp
!= NULL
) ?
3671 ifp
->if_delegated
.ifp
->if_index
: 0);
3672 ifnet_lock_done(ifp
);
3675 case SIOCGIFEXPENSIVE
:
3676 ifnet_lock_shared(ifp
);
3677 if (ifp
->if_eflags
& IFEF_EXPENSIVE
) {
3678 ifr
->ifr_expensive
= 1;
3680 ifr
->ifr_expensive
= 0;
3682 ifnet_lock_done(ifp
);
3685 case SIOCSIFEXPENSIVE
:
3689 if ((error
= priv_check_cred(kauth_cred_get(),
3690 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3693 if (ifr
->ifr_expensive
) {
3694 if_set_eflags(ifp
, IFEF_EXPENSIVE
);
3696 if_clear_eflags(ifp
, IFEF_EXPENSIVE
);
3698 ifnet_increment_generation(ifp
);
3701 * Update the expensive bit in the delegated interface
3704 ifnet_head_lock_shared();
3705 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3706 ifnet_lock_exclusive(difp
);
3707 if (difp
->if_delegated
.ifp
== ifp
) {
3708 difp
->if_delegated
.expensive
=
3709 ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
3710 ifnet_increment_generation(difp
);
3712 ifnet_lock_done(difp
);
3715 necp_update_all_clients();
3719 case SIOCGIFCONSTRAINED
:
3720 if ((ifp
->if_xflags
& IFXF_CONSTRAINED
) != 0) {
3721 ifr
->ifr_constrained
= 1;
3723 ifr
->ifr_constrained
= 0;
3727 case SIOCSIFCONSTRAINED
:
3731 if ((error
= priv_check_cred(kauth_cred_get(),
3732 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3735 if (ifr
->ifr_constrained
) {
3736 if_set_xflags(ifp
, IFXF_CONSTRAINED
);
3738 if_clear_xflags(ifp
, IFXF_CONSTRAINED
);
3740 ifnet_increment_generation(ifp
);
3742 * Update the constrained bit in the delegated interface
3745 ifnet_head_lock_shared();
3746 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3747 ifnet_lock_exclusive(difp
);
3748 if (difp
->if_delegated
.ifp
== ifp
) {
3749 difp
->if_delegated
.constrained
=
3750 ((ifp
->if_xflags
& IFXF_CONSTRAINED
) != 0) ? 1 : 0;
3751 ifnet_increment_generation(difp
);
3753 ifnet_lock_done(difp
);
3756 necp_update_all_clients();
3761 ifnet_lock_shared(ifp
);
3762 if (ifp
->if_eflags
& IFEF_2KCL
) {
3767 ifnet_lock_done(ifp
);
3771 if ((error
= priv_check_cred(kauth_cred_get(),
3772 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3775 if (ifr
->ifr_2kcl
) {
3776 if_set_eflags(ifp
, IFEF_2KCL
);
3778 if_clear_eflags(ifp
, IFEF_2KCL
);
3781 case SIOCGSTARTDELAY
:
3782 ifnet_lock_shared(ifp
);
3783 if (ifp
->if_eflags
& IFEF_ENQUEUE_MULTI
) {
3784 ifr
->ifr_start_delay_qlen
=
3785 ifp
->if_start_delay_qlen
;
3786 ifr
->ifr_start_delay_timeout
=
3787 ifp
->if_start_delay_timeout
;
3789 ifr
->ifr_start_delay_qlen
= 0;
3790 ifr
->ifr_start_delay_timeout
= 0;
3792 ifnet_lock_done(ifp
);
3794 case SIOCSIFDSTADDR
:
3796 case SIOCSIFBRDADDR
:
3797 case SIOCSIFNETMASK
:
3799 case OSIOCGIFDSTADDR
:
3800 case OSIOCGIFBRDADDR
:
3801 case OSIOCGIFNETMASK
:
3803 VERIFY(so
->so_proto
!= NULL
);
3805 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
3806 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
3807 #if BYTE_ORDER != BIG_ENDIAN
3808 if (ifr
->ifr_addr
.sa_family
== 0 &&
3809 ifr
->ifr_addr
.sa_len
< 16) {
3810 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
3811 ifr
->ifr_addr
.sa_len
= 16;
3814 if (ifr
->ifr_addr
.sa_len
== 0) {
3815 ifr
->ifr_addr
.sa_len
= 16;
3818 } else if (cmd
== OSIOCGIFADDR
) {
3819 cmd
= SIOCGIFADDR
; /* struct ifreq */
3820 } else if (cmd
== OSIOCGIFDSTADDR
) {
3821 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
3822 } else if (cmd
== OSIOCGIFBRDADDR
) {
3823 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
3824 } else if (cmd
== OSIOCGIFNETMASK
) {
3825 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
3829 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3830 (caddr_t
)ifr
, ifp
, p
));
3831 socket_unlock(so
, 1);
3835 case OSIOCGIFDSTADDR
:
3836 case OSIOCGIFBRDADDR
:
3837 case OSIOCGIFNETMASK
:
3838 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
3842 if (cmd
== SIOCSIFKPI
) {
3843 int temperr
= proc_suser(p
);
3848 // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3849 // with ifreq as the code expects ifaddr
3850 if ((error
== EOPNOTSUPP
|| error
== ENOTSUP
) &&
3851 !(cmd
== SIOCSIFADDR
|| cmd
== SIOCSIFDSTADDR
)) {
3852 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3857 case SIOCGIFINTERFACESTATE
:
3858 if_get_state(ifp
, &ifr
->ifr_interface_state
);
3861 case SIOCSIFINTERFACESTATE
:
3862 if ((error
= priv_check_cred(kauth_cred_get(),
3863 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3867 error
= if_state_update(ifp
, &ifr
->ifr_interface_state
);
3870 case SIOCSIFPROBECONNECTIVITY
:
3871 if ((error
= priv_check_cred(kauth_cred_get(),
3872 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3875 error
= if_probe_connectivity(ifp
,
3876 ifr
->ifr_probe_connectivity
);
3878 case SIOCGIFPROBECONNECTIVITY
:
3879 if ((error
= priv_check_cred(kauth_cred_get(),
3880 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3883 if (ifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
) {
3884 ifr
->ifr_probe_connectivity
= 1;
3886 ifr
->ifr_probe_connectivity
= 0;
3890 if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3892 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_ENABLE
;
3893 } else if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
)) ==
3895 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DISABLE
;
3897 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
3901 if ((error
= priv_check_cred(kauth_cred_get(),
3902 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3905 if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DEFAULT
) {
3906 if_clear_eflags(ifp
, IFEF_ECN_ENABLE
| IFEF_ECN_DISABLE
);
3907 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_ENABLE
) {
3908 if_set_eflags(ifp
, IFEF_ECN_ENABLE
);
3909 if_clear_eflags(ifp
, IFEF_ECN_DISABLE
);
3910 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DISABLE
) {
3911 if_set_eflags(ifp
, IFEF_ECN_DISABLE
);
3912 if_clear_eflags(ifp
, IFEF_ECN_ENABLE
);
3918 case SIOCSIFTIMESTAMPENABLE
:
3919 case SIOCSIFTIMESTAMPDISABLE
:
3920 error
= proc_suser(p
);
3925 if ((cmd
== SIOCSIFTIMESTAMPENABLE
&&
3926 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) ||
3927 (cmd
== SIOCSIFTIMESTAMPDISABLE
&&
3928 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) == 0)) {
3931 if (cmd
== SIOCSIFTIMESTAMPENABLE
) {
3932 if_set_xflags(ifp
, IFXF_TIMESTAMP_ENABLED
);
3934 if_clear_xflags(ifp
, IFXF_TIMESTAMP_ENABLED
);
3937 * Pass the setting to the interface if it supports either
3938 * software or hardware time stamping
3940 if (ifp
->if_capabilities
& (IFCAP_HW_TIMESTAMP
|
3941 IFCAP_SW_TIMESTAMP
)) {
3942 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3946 case SIOCGIFTIMESTAMPENABLED
: {
3947 if ((ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) {
3948 ifr
->ifr_intval
= 1;
3950 ifr
->ifr_intval
= 0;
3954 case SIOCSQOSMARKINGMODE
:
3955 if ((error
= priv_check_cred(kauth_cred_get(),
3956 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3959 error
= if_set_qosmarking_mode(ifp
, ifr
->ifr_qosmarking_mode
);
3962 case SIOCGQOSMARKINGMODE
:
3963 ifr
->ifr_qosmarking_mode
= ifp
->if_qosmarking_mode
;
3966 case SIOCSQOSMARKINGENABLED
:
3967 if ((error
= priv_check_cred(kauth_cred_get(),
3968 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
3971 if (ifr
->ifr_qosmarking_enabled
!= 0) {
3972 if_set_eflags(ifp
, IFEF_QOSMARKING_ENABLED
);
3974 if_clear_eflags(ifp
, IFEF_QOSMARKING_ENABLED
);
3978 case SIOCGQOSMARKINGENABLED
:
3979 ifr
->ifr_qosmarking_enabled
=
3980 ((ifp
->if_eflags
& IFEF_QOSMARKING_ENABLED
) != 0) ? 1 : 0;
3983 case SIOCSIFDISABLEOUTPUT
:
3984 #if (DEBUG || DEVELOPMENT)
3985 if (ifr
->ifr_disable_output
== 1) {
3986 error
= ifnet_disable_output(ifp
);
3987 } else if (ifr
->ifr_disable_output
== 0) {
3988 error
= ifnet_enable_output(ifp
);
3994 #endif /* (DEBUG || DEVELOPMENT) */
3997 case SIOCSIFSUBFAMILY
:
3998 if ((error
= priv_check_cred(kauth_cred_get(),
3999 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
4002 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
4005 case SIOCSIFLOWINTERNET
:
4006 if ((error
= priv_check_cred(kauth_cred_get(),
4007 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
4011 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_UL
) {
4012 if_set_xflags(ifp
, IFXF_LOW_INTERNET_UL
);
4014 if_clear_xflags(ifp
, IFXF_LOW_INTERNET_UL
);
4016 if (ifr
->ifr_low_internet
& IFRTYPE_LOW_INTERNET_ENABLE_DL
) {
4017 if_set_xflags(ifp
, IFXF_LOW_INTERNET_DL
);
4019 if_clear_xflags(ifp
, IFXF_LOW_INTERNET_DL
);
4022 case SIOCGIFLOWINTERNET
:
4023 ifnet_lock_shared(ifp
);
4024 ifr
->ifr_low_internet
= 0;
4025 if ((ifp
->if_xflags
& IFXF_LOW_INTERNET_UL
) != 0) {
4026 ifr
->ifr_low_internet
|=
4027 IFRTYPE_LOW_INTERNET_ENABLE_UL
;
4029 if ((ifp
->if_xflags
& IFXF_LOW_INTERNET_DL
) != 0) {
4030 ifr
->ifr_low_internet
|=
4031 IFRTYPE_LOW_INTERNET_ENABLE_DL
;
4033 ifnet_lock_done(ifp
);
4035 case SIOCGIFLOWPOWER
:
4036 ifr
->ifr_low_power_mode
=
4037 ((ifp
->if_xflags
& IFXF_LOW_POWER
) != 0);
4039 case SIOCSIFLOWPOWER
:
4040 #if (DEVELOPMENT || DEBUG)
4041 error
= if_set_low_power(ifp
, (ifr
->ifr_low_power_mode
!= 0));
4042 #else /* DEVELOPMENT || DEBUG */
4044 #endif /* DEVELOPMENT || DEBUG */
4048 ifr
->ifr_mpk_log
= ((ifp
->if_xflags
& IFXF_MPK_LOG
) != 0);
4051 if (ifr
->ifr_mpk_log
) {
4052 if_set_xflags(ifp
, IFXF_MPK_LOG
);
4054 if_clear_xflags(ifp
, IFXF_MPK_LOG
);
4057 case SIOCGIFNOACKPRIO
:
4058 if ((ifp
->if_eflags
& IFEF_NOACKPRI
) != 0) {
4059 ifr
->ifr_noack_prio
= 1;
4061 ifr
->ifr_noack_prio
= 0;
4065 case SIOCSIFNOACKPRIO
:
4066 if ((error
= priv_check_cred(kauth_cred_get(),
4067 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0) {
4070 if (ifr
->ifr_noack_prio
) {
4071 if_set_eflags(ifp
, IFEF_NOACKPRI
);
4073 if_clear_eflags(ifp
, IFEF_NOACKPRI
);
4086 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
4090 socket_unlock(so
, 0);
4091 error
= ifioctl(so
, cmd
, data
, p
);
4097 * Set/clear promiscuous mode on interface ifp based on the truth value
4098 * of pswitch. The calls are reference counted so that only the first
4099 * "on" request actually has an effect, as does the final "off" request.
4100 * Results are undefined if the "off" and "on" requests are not matched.
4103 ifnet_set_promiscuous(
4111 ifnet_lock_exclusive(ifp
);
4112 oldflags
= ifp
->if_flags
;
4113 ifp
->if_pcount
+= pswitch
? 1 : -1;
4115 if (ifp
->if_pcount
> 0) {
4116 ifp
->if_flags
|= IFF_PROMISC
;
4118 ifp
->if_flags
&= ~IFF_PROMISC
;
4121 newflags
= ifp
->if_flags
;
4122 ifnet_lock_done(ifp
);
4124 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
4125 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
4129 ifnet_lock_exclusive(ifp
);
4131 ifp
->if_pcount
-= pswitch
? 1 : -1;
4132 if (ifp
->if_pcount
> 0) {
4133 ifp
->if_flags
|= IFF_PROMISC
;
4135 ifp
->if_flags
&= ~IFF_PROMISC
;
4137 ifnet_lock_done(ifp
);
4141 if (newflags
!= oldflags
) {
4142 log(LOG_INFO
, "%s: promiscuous mode %s%s\n",
4144 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
4145 error
!= 0 ? " failed" : " succeeded");
4151 * Return interface configuration
4152 * of system. List may be used
4153 * in later ioctl's (above) to get
4154 * other information.
4158 ifconf(u_long cmd
, user_addr_t ifrp
, int *ret_space
)
4160 struct ifnet
*ifp
= NULL
;
4165 net_thread_marks_t marks
;
4167 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
4170 * Zero the ifr buffer to make sure we don't
4171 * disclose the contents of the stack.
4173 bzero(&ifr
, sizeof(struct ifreq
));
4176 ifnet_head_lock_shared();
4177 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) &&
4178 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
4180 size_t ifnlen
, addrs
;
4182 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
4183 "%s", if_name(ifp
));
4184 if (ifnlen
+ 1 > sizeof(ifr
.ifr_name
)) {
4185 error
= ENAMETOOLONG
;
4188 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
4191 ifnet_lock_shared(ifp
);
4194 ifa
= ifp
->if_addrhead
.tqh_first
;
4195 for (; space
> sizeof(ifr
) && ifa
;
4196 ifa
= ifa
->ifa_link
.tqe_next
) {
4197 struct sockaddr
*sa
;
4200 struct sockaddr_dl sdl
;
4201 uint8_t buf
[SOCK_MAXADDRLEN
+ 1];
4205 * Make sure to accomodate the largest possible
4206 * size of SA(if_lladdr)->sa_len.
4208 _CASSERT(sizeof(u
) == (SOCK_MAXADDRLEN
+ 1));
4214 if (ifa
== ifp
->if_lladdr
) {
4215 VERIFY(sa
->sa_family
== AF_LINK
);
4216 bcopy(sa
, &u
, sa
->sa_len
);
4218 ifnet_guarded_lladdr_copy_bytes(ifp
,
4219 LLADDR(&u
.sdl
), u
.sdl
.sdl_alen
);
4224 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
4225 struct osockaddr
*osa
=
4226 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
4228 osa
->sa_family
= sa
->sa_family
;
4229 error
= copyout((caddr_t
)&ifr
, ifrp
,
4231 ifrp
+= sizeof(struct ifreq
);
4232 } else if (sa
->sa_len
<= sizeof(*sa
)) {
4234 error
= copyout((caddr_t
)&ifr
, ifrp
,
4236 ifrp
+= sizeof(struct ifreq
);
4239 sizeof(ifr
) + sa
->sa_len
- sizeof(*sa
)) {
4243 space
-= sa
->sa_len
- sizeof(*sa
);
4244 error
= copyout((caddr_t
)&ifr
, ifrp
,
4245 sizeof(ifr
.ifr_name
));
4247 error
= copyout((caddr_t
)sa
, (ifrp
+
4248 offsetof(struct ifreq
, ifr_addr
)),
4251 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
4258 space
-= sizeof(ifr
);
4260 ifnet_lock_done(ifp
);
4266 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
4267 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
4271 space
-= sizeof(ifr
);
4272 ifrp
+= sizeof(struct ifreq
);
4276 *ret_space
-= space
;
4277 net_thread_marks_pop(marks
);
4282 * Just like if_promisc(), but for all-multicast-reception mode.
4285 if_allmulti(struct ifnet
*ifp
, int onswitch
)
4290 ifnet_lock_exclusive(ifp
);
4293 if (ifp
->if_amcount
++ == 0) {
4294 ifp
->if_flags
|= IFF_ALLMULTI
;
4298 if (ifp
->if_amcount
> 1) {
4301 ifp
->if_amcount
= 0;
4302 ifp
->if_flags
&= ~IFF_ALLMULTI
;
4306 ifnet_lock_done(ifp
);
4309 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
4318 static struct ifmultiaddr
*
4321 struct ifmultiaddr
*ifma
;
4323 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
4324 zalloc_noblock(ifma_zone
);
4327 bzero(ifma
, ifma_size
);
4328 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4329 ifma
->ifma_debug
|= IFD_ALLOC
;
4330 if (ifma_debug
!= 0) {
4331 ifma
->ifma_debug
|= IFD_DEBUG
;
4332 ifma
->ifma_trace
= ifma_trace
;
4339 ifma_free(struct ifmultiaddr
*ifma
)
4343 if (ifma
->ifma_protospec
!= NULL
) {
4344 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4346 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4347 ifma
->ifma_anoncnt
!= 0) {
4348 panic("%s: Freeing ifma=%p with outstanding anon req",
4351 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4352 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4353 __func__
, ifma
, ifma
->ifma_ifp
);
4355 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
4356 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
4358 } else if (ifma
->ifma_refcount
!= 0) {
4359 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
4361 } else if (ifma
->ifma_reqcnt
!= 0) {
4362 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
4364 } else if (ifma
->ifma_ifp
!= NULL
) {
4365 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
4366 ifma
->ifma_ifp
, ifma
);
4368 } else if (ifma
->ifma_ll
!= NULL
) {
4369 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
4370 ifma
->ifma_ll
, ifma
);
4373 ifma
->ifma_debug
&= ~IFD_ALLOC
;
4374 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
4375 (IFD_DEBUG
| IFD_TRASHED
)) {
4376 lck_mtx_lock(&ifma_trash_lock
);
4377 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
4379 lck_mtx_unlock(&ifma_trash_lock
);
4380 ifma
->ifma_debug
&= ~IFD_TRASHED
;
4384 if (ifma
->ifma_addr
!= NULL
) {
4385 FREE(ifma
->ifma_addr
, M_IFADDR
);
4386 ifma
->ifma_addr
= NULL
;
4388 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
4389 zfree(ifma_zone
, ifma
);
4393 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
4395 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
4400 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
4401 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
4405 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
4406 tr
= ifma_dbg
->ifma_refhold
;
4408 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
4409 tr
= ifma_dbg
->ifma_refrele
;
4412 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
4413 ctrace_record(&tr
[idx
]);
4417 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
4422 IFMA_LOCK_ASSERT_HELD(ifma
);
4425 if (++ifma
->ifma_refcount
== 0) {
4426 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
4428 } else if (ifma
->ifma_trace
!= NULL
) {
4429 (*ifma
->ifma_trace
)(ifma
, TRUE
);
4437 ifma_remref(struct ifmultiaddr
*ifma
)
4439 struct ifmultiaddr
*ll
;
4443 if (ifma
->ifma_refcount
== 0) {
4444 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
4446 } else if (ifma
->ifma_trace
!= NULL
) {
4447 (*ifma
->ifma_trace
)(ifma
, FALSE
);
4450 --ifma
->ifma_refcount
;
4451 if (ifma
->ifma_refcount
> 0) {
4457 ifma
->ifma_ifp
= NULL
;
4458 ifma
->ifma_ll
= NULL
;
4460 ifma_free(ifma
); /* deallocate it */
4468 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4470 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4471 IFMA_LOCK_ASSERT_HELD(ifma
);
4473 if (ifma
->ifma_ifp
!= ifp
) {
4474 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
4475 ifma
->ifma_ifp
, ifp
);
4477 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
4478 panic("%s: Attempt to attach an already attached ifma=%p",
4481 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4482 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
4484 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4485 panic("%s: Attempt to reattach a detached ifma=%p",
4490 ifma
->ifma_reqcnt
++;
4491 VERIFY(ifma
->ifma_reqcnt
== 1);
4492 IFMA_ADDREF_LOCKED(ifma
);
4493 ifma
->ifma_debug
|= IFD_ATTACHED
;
4495 ifma
->ifma_anoncnt
++;
4496 VERIFY(ifma
->ifma_anoncnt
== 1);
4497 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4500 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
4504 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
4506 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
4507 IFMA_LOCK_ASSERT_HELD(ifma
);
4509 if (ifma
->ifma_reqcnt
== 0) {
4510 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
4512 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4513 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
4515 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
4516 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
4518 } else if (ifma
->ifma_ifp
!= ifp
) {
4519 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
4520 ifma
->ifma_ifp
, ifp
);
4525 --ifma
->ifma_anoncnt
;
4526 if (ifma
->ifma_anoncnt
> 0) {
4529 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
4532 --ifma
->ifma_reqcnt
;
4533 if (ifma
->ifma_reqcnt
> 0) {
4537 if (ifma
->ifma_protospec
!= NULL
) {
4538 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
4540 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4541 ifma
->ifma_anoncnt
!= 0) {
4542 panic("%s: Detaching ifma=%p with outstanding anon req",
4545 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
4546 panic("%s: Attempt to detach an unattached address ifma=%p",
4549 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
4550 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
4555 * NOTE: Caller calls IFMA_REMREF
4557 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
4558 LIST_REMOVE(ifma
, ifma_link
);
4559 if (LIST_EMPTY(&ifp
->if_multiaddrs
)) {
4560 ifp
->if_updatemcasts
= 0;
4563 if (ifma
->ifma_debug
& IFD_DEBUG
) {
4564 /* Become a regular mutex, just in case */
4565 IFMA_CONVERT_LOCK(ifma
);
4566 lck_mtx_lock(&ifma_trash_lock
);
4567 TAILQ_INSERT_TAIL(&ifma_trash_head
,
4568 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
4569 lck_mtx_unlock(&ifma_trash_lock
);
4570 ifma
->ifma_debug
|= IFD_TRASHED
;
4577 * Find an ifmultiaddr that matches a socket address on an interface.
4579 * Caller is responsible for holding the ifnet_lock while calling
4583 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4584 struct ifmultiaddr
**retifma
, int anon
)
4586 struct ifmultiaddr
*ifma
;
4588 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4589 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4590 IFMA_LOCK_SPIN(ifma
);
4591 if (!ifa_equal(sa
, ifma
->ifma_addr
)) {
4596 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4597 ifma
->ifma_anoncnt
!= 0);
4598 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4599 ifma
->ifma_anoncnt
== 0);
4600 ifma
->ifma_anoncnt
++;
4601 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
4602 VERIFY(ifma
->ifma_anoncnt
== 1);
4603 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
4606 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
4607 ifma
->ifma_reqcnt
++;
4608 VERIFY(ifma
->ifma_reqcnt
> 1);
4610 if (retifma
!= NULL
) {
4612 IFMA_ADDREF_LOCKED(ifma
);
4621 * Radar 3642395, make sure all multicasts are in a standard format.
4623 static struct sockaddr
*
4624 copy_and_normalize(const struct sockaddr
*original
)
4627 const u_char
*aptr
= NULL
;
4628 struct sockaddr
*copy
= NULL
;
4629 struct sockaddr_dl
*sdl_new
= NULL
;
4632 if (original
->sa_family
!= AF_LINK
&&
4633 original
->sa_family
!= AF_UNSPEC
) {
4634 /* Just make a copy */
4635 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
4636 M_IFADDR
, M_WAITOK
);
4638 bcopy(original
, copy
, original
->sa_len
);
4643 switch (original
->sa_family
) {
4645 const struct sockaddr_dl
*sdl_original
=
4646 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
4648 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
4649 sdl_original
->sdl_slen
+
4650 offsetof(struct sockaddr_dl
, sdl_data
) >
4651 sdl_original
->sdl_len
) {
4655 alen
= sdl_original
->sdl_alen
;
4656 aptr
= CONST_LLADDR(sdl_original
);
4661 if (original
->sa_len
< ETHER_ADDR_LEN
+
4662 offsetof(struct sockaddr
, sa_data
)) {
4666 alen
= ETHER_ADDR_LEN
;
4667 aptr
= (const u_char
*)original
->sa_data
;
4672 if (alen
== 0 || aptr
== NULL
) {
4676 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
4677 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
4679 if (sdl_new
!= NULL
) {
4680 bzero(sdl_new
, len
);
4681 sdl_new
->sdl_len
= len
;
4682 sdl_new
->sdl_family
= AF_LINK
;
4683 sdl_new
->sdl_alen
= alen
;
4684 bcopy(aptr
, LLADDR(sdl_new
), alen
);
4687 return (struct sockaddr
*)sdl_new
;
4691 * Network-layer protocol domains which hold references to the underlying
4692 * link-layer record must use this routine.
4695 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4696 struct ifmultiaddr
**retifma
)
4698 return if_addmulti_common(ifp
, sa
, retifma
, 0);
4702 * Anything other than network-layer protocol domains which hold references
4703 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4704 * ioctl, ifnet_add_multicast(), if_bond.
4707 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4708 struct ifmultiaddr
**retifma
)
4710 return if_addmulti_common(ifp
, sa
, retifma
, 1);
4714 * Register an additional multicast address with a network interface.
4716 * - If the address is already present, bump the reference count on the
4717 * address and return.
4718 * - If the address is not link-layer, look up a link layer address.
4719 * - Allocate address structures for one or both addresses, and attach to the
4720 * multicast address list on the interface. If automatically adding a link
4721 * layer address, the protocol address will own a reference to the link
4722 * layer address, to be freed when it is freed.
4723 * - Notify the network device driver of an addition to the multicast address
4726 * 'sa' points to caller-owned memory with the desired multicast address.
4728 * 'retifma' will be used to return a pointer to the resulting multicast
4729 * address reference, if desired.
4731 * 'anon' indicates a link-layer address with no protocol address reference
4732 * made to it. Anything other than network-layer protocol domain requests
4733 * are considered as anonymous.
4736 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
4737 struct ifmultiaddr
**retifma
, int anon
)
4739 struct sockaddr_storage storage
;
4740 struct sockaddr
*llsa
= NULL
;
4741 struct sockaddr
*dupsa
= NULL
;
4742 int error
= 0, ll_firstref
= 0, lladdr
;
4743 struct ifmultiaddr
*ifma
= NULL
;
4744 struct ifmultiaddr
*llifma
= NULL
;
4746 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4747 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
4748 sa
->sa_family
== AF_LINK
);
4750 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4751 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
4752 dupsa
= copy_and_normalize(sa
);
4753 if (dupsa
== NULL
) {
4760 ifnet_lock_exclusive(ifp
);
4761 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
4762 error
= EADDRNOTAVAIL
;
4763 ifnet_lock_done(ifp
);
4767 /* If the address is already present, return a new reference to it */
4768 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4769 ifnet_lock_done(ifp
);
4775 * The address isn't already present; give the link layer a chance
4776 * to accept/reject it, and also find out which AF_LINK address this
4777 * maps to, if it isn't one already.
4779 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
4781 if (error
== 0 && storage
.ss_len
!= 0) {
4782 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
4788 llifma
= ifma_alloc(M_WAITOK
);
4789 if (llifma
== NULL
) {
4795 /* to be similar to FreeBSD */
4796 if (error
== EOPNOTSUPP
) {
4798 } else if (error
!= 0) {
4802 /* Allocate while we aren't holding any locks */
4803 if (dupsa
== NULL
) {
4804 dupsa
= copy_and_normalize(sa
);
4805 if (dupsa
== NULL
) {
4810 ifma
= ifma_alloc(M_WAITOK
);
4816 ifnet_lock_exclusive(ifp
);
4818 * Check again for the matching multicast.
4820 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
4822 ifnet_lock_done(ifp
);
4826 if (llifma
!= NULL
) {
4827 VERIFY(!anon
); /* must not get here if "anonymous" */
4828 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
4829 FREE(llsa
, M_IFADDR
);
4833 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
4836 llifma
->ifma_addr
= llsa
;
4837 llifma
->ifma_ifp
= ifp
;
4839 if_attach_ifma(ifp
, llifma
, 0);
4840 /* add extra refcnt for ifma */
4841 IFMA_ADDREF_LOCKED(llifma
);
4842 IFMA_UNLOCK(llifma
);
4843 ifma
->ifma_ll
= llifma
;
4847 /* "anonymous" request should not result in network address */
4848 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4850 ifma
->ifma_addr
= dupsa
;
4851 ifma
->ifma_ifp
= ifp
;
4853 if_attach_ifma(ifp
, ifma
, anon
);
4854 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4855 if (retifma
!= NULL
) {
4857 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
4859 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4860 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4862 ifnet_lock_done(ifp
);
4864 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
4865 IFMA_REMREF(ifma
); /* for this routine */
4868 * We are certain we have added something, so call down to the
4869 * interface to let them know about it. Do this only for newly-
4870 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4872 if (lladdr
|| ll_firstref
) {
4873 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
4876 if (ifp
->if_updatemcasts
> 0) {
4877 ifp
->if_updatemcasts
= 0;
4886 if (dupsa
!= NULL
) {
4887 FREE(dupsa
, M_IFADDR
);
4889 if (llifma
!= NULL
) {
4893 FREE(llsa
, M_IFADDR
);
4900 * Delete a multicast group membership by network-layer group address.
4901 * This routine is deprecated.
4904 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4906 return if_delmulti_common(NULL
, ifp
, sa
, 0);
4910 * Delete a multicast group membership by group membership pointer.
4911 * Network-layer protocol domains must use this routine.
4914 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
4916 return if_delmulti_common(ifma
, NULL
, NULL
, 0);
4920 * Anything other than network-layer protocol domains which hold references
4921 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4922 * ioctl, ifnet_remove_multicast(), if_bond.
4925 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4927 return if_delmulti_common(NULL
, ifp
, sa
, 1);
4931 * Delete a multicast group membership by network-layer group address.
4933 * Returns ENOENT if the entry could not be found.
4936 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
4937 const struct sockaddr
*sa
, int anon
)
4939 struct sockaddr
*dupsa
= NULL
;
4940 int lastref
, ll_lastref
= 0, lladdr
;
4941 struct ifmultiaddr
*ll
= NULL
;
4943 /* sanity check for callers */
4944 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
4947 ifp
= ifma
->ifma_ifp
;
4951 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
4952 dupsa
= copy_and_normalize(sa
);
4953 if (dupsa
== NULL
) {
4959 ifnet_lock_exclusive(ifp
);
4961 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4962 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4964 if (!ifa_equal(sa
, ifma
->ifma_addr
) ||
4965 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
4966 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4967 ifma
->ifma_anoncnt
!= 0);
4971 /* found; keep it locked */
4975 if (dupsa
!= NULL
) {
4976 FREE(dupsa
, M_IFADDR
);
4978 ifnet_lock_done(ifp
);
4984 IFMA_LOCK_ASSERT_HELD(ifma
);
4985 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4986 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
4987 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
4988 ifma
->ifma_reqcnt
== 0));
4989 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4991 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4992 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4994 if (lastref
&& ll
!= NULL
) {
4996 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
4999 ifnet_lock_done(ifp
);
5002 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
5005 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
5007 * Make sure the interface driver is notified in the
5008 * case of a link layer mcast group being left. Do
5009 * this only for a AF_LINK/AF_UNSPEC address that has
5010 * been removed from the if_multiaddrs set.
5012 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
5016 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
5019 IFMA_REMREF(ll
); /* for if_multiaddrs list */
5021 IFMA_REMREF(ifma
); /* for this routine */
5022 if (dupsa
!= NULL
) {
5023 FREE(dupsa
, M_IFADDR
);
5030 * Shutdown all network activity. Used boot() when halting
5040 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
5041 for (i
= 0; i
< count
; i
++) {
5043 dlil_proto_unplumb_all(ifp
[i
]);
5045 ifnet_list_free(ifp
);
5052 * Delete Routes for a Network Interface
5054 * Called for each routing entry via the rnh->rnh_walktree() call above
5055 * to delete all route entries referencing a detaching network interface.
5058 * rn pointer to node in the routing table
5059 * arg argument passed to rnh->rnh_walktree() - detaching interface
5063 * errno failed - reason indicated
5067 if_rtdel(struct radix_node
*rn
, void *arg
)
5069 struct rtentry
*rt
= (struct rtentry
*)rn
;
5070 struct ifnet
*ifp
= arg
;
5077 * Checking against RTF_UP protects against walktree
5078 * recursion problems with cloned routes.
5081 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
5083 * Safe to drop rt_lock and use rt_key, rt_gateway,
5084 * since holding rnh_lock here prevents another thread
5085 * from calling rt_setgate() on this route.
5088 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
5089 rt_mask(rt
), rt
->rt_flags
, NULL
);
5091 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
5100 * Removes routing table reference to a given interface
5101 * for a given protocol family
5104 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
5106 struct radix_node_head
*rnh
;
5108 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
5109 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
5110 lck_mtx_lock(rnh_lock
);
5111 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
5112 lck_mtx_unlock(rnh_lock
);
5117 if_rtmtu(struct radix_node
*rn
, void *arg
)
5119 struct rtentry
*rt
= (struct rtentry
*)rn
;
5120 struct ifnet
*ifp
= arg
;
5123 if (rt
->rt_ifp
== ifp
) {
5125 * Update the MTU of this entry only if the MTU
5126 * has not been locked (RTV_MTU is not set) and
5127 * if it was non-zero to begin with.
5129 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
) {
5130 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
5131 if (rt_key(rt
)->sa_family
== AF_INET
&&
5132 INTF_ADJUST_MTU_FOR_CLAT46(ifp
)) {
5133 rt
->rt_rmx
.rmx_mtu
= IN6_LINKMTU(ifp
);
5134 /* Further adjust the size for CLAT46 expansion */
5135 rt
->rt_rmx
.rmx_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
5145 * Update the MTU metric of all route entries in all protocol tables
5146 * associated with a particular interface; this is called when the
5147 * MTU of that interface has changed.
5150 if_rtmtu_update(struct ifnet
*ifp
)
5152 struct radix_node_head
*rnh
;
5155 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
5156 if ((rnh
= rt_tables
[p
]) == NULL
) {
5160 lck_mtx_lock(rnh_lock
);
5161 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
5162 lck_mtx_unlock(rnh_lock
);
5164 routegenid_update();
5167 __private_extern__
void
5168 if_data_internal_to_if_data(struct ifnet
*ifp
,
5169 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
5172 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
5173 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
5174 /* compiler will cast down to 32-bit */
5175 #define COPYFIELD32_ATOMIC(fld) do { \
5176 atomic_get_64(if_data->fld, \
5177 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5180 COPYFIELD(ifi_type
);
5181 COPYFIELD(ifi_typelen
);
5182 COPYFIELD(ifi_physical
);
5183 COPYFIELD(ifi_addrlen
);
5184 COPYFIELD(ifi_hdrlen
);
5185 COPYFIELD(ifi_recvquota
);
5186 COPYFIELD(ifi_xmitquota
);
5187 if_data
->ifi_unused1
= 0;
5189 COPYFIELD(ifi_metric
);
5190 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
5191 if_data
->ifi_baudrate
= 0xFFFFFFFF;
5193 COPYFIELD32(ifi_baudrate
);
5196 COPYFIELD32_ATOMIC(ifi_ipackets
);
5197 COPYFIELD32_ATOMIC(ifi_ierrors
);
5198 COPYFIELD32_ATOMIC(ifi_opackets
);
5199 COPYFIELD32_ATOMIC(ifi_oerrors
);
5200 COPYFIELD32_ATOMIC(ifi_collisions
);
5201 COPYFIELD32_ATOMIC(ifi_ibytes
);
5202 COPYFIELD32_ATOMIC(ifi_obytes
);
5203 COPYFIELD32_ATOMIC(ifi_imcasts
);
5204 COPYFIELD32_ATOMIC(ifi_omcasts
);
5205 COPYFIELD32_ATOMIC(ifi_iqdrops
);
5206 COPYFIELD32_ATOMIC(ifi_noproto
);
5208 COPYFIELD(ifi_recvtiming
);
5209 COPYFIELD(ifi_xmittiming
);
5211 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5212 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5214 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
5216 if_data
->ifi_unused2
= 0;
5217 COPYFIELD(ifi_hwassist
);
5218 if_data
->ifi_reserved1
= 0;
5219 if_data
->ifi_reserved2
= 0;
5220 #undef COPYFIELD32_ATOMIC
5225 __private_extern__
void
5226 if_data_internal_to_if_data64(struct ifnet
*ifp
,
5227 const struct if_data_internal
*if_data_int
,
5228 struct if_data64
*if_data64
)
5231 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
5232 #define COPYFIELD64_ATOMIC(fld) do { \
5233 atomic_get_64(if_data64->fld, \
5234 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
5237 COPYFIELD64(ifi_type
);
5238 COPYFIELD64(ifi_typelen
);
5239 COPYFIELD64(ifi_physical
);
5240 COPYFIELD64(ifi_addrlen
);
5241 COPYFIELD64(ifi_hdrlen
);
5242 COPYFIELD64(ifi_recvquota
);
5243 COPYFIELD64(ifi_xmitquota
);
5244 if_data64
->ifi_unused1
= 0;
5245 COPYFIELD64(ifi_mtu
);
5246 COPYFIELD64(ifi_metric
);
5247 COPYFIELD64(ifi_baudrate
);
5249 COPYFIELD64_ATOMIC(ifi_ipackets
);
5250 COPYFIELD64_ATOMIC(ifi_ierrors
);
5251 COPYFIELD64_ATOMIC(ifi_opackets
);
5252 COPYFIELD64_ATOMIC(ifi_oerrors
);
5253 COPYFIELD64_ATOMIC(ifi_collisions
);
5254 COPYFIELD64_ATOMIC(ifi_ibytes
);
5255 COPYFIELD64_ATOMIC(ifi_obytes
);
5256 COPYFIELD64_ATOMIC(ifi_imcasts
);
5257 COPYFIELD64_ATOMIC(ifi_omcasts
);
5258 COPYFIELD64_ATOMIC(ifi_iqdrops
);
5259 COPYFIELD64_ATOMIC(ifi_noproto
);
5262 * Note these two fields are actually 32 bit, so doing
5263 * COPYFIELD64_ATOMIC will cause them to be misaligned
5265 COPYFIELD64(ifi_recvtiming
);
5266 COPYFIELD64(ifi_xmittiming
);
5268 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
5269 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
5271 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
5276 __private_extern__
void
5277 if_copy_traffic_class(struct ifnet
*ifp
,
5278 struct if_traffic_class
*if_tc
)
5280 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
5281 atomic_get_64(if_tc->fld, \
5282 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
5285 bzero(if_tc
, sizeof(*if_tc
));
5286 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
5287 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
5288 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
5289 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
5290 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
5291 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
5292 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
5293 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
5294 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
5295 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
5296 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
5297 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
5298 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
5299 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
5300 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
5301 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
5302 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
5303 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
5304 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
5305 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
5307 #undef COPY_IF_TC_FIELD64_ATOMIC
5311 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
5313 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
5314 atomic_get_64(if_de->fld, \
5315 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
5318 bzero(if_de
, sizeof(*if_de
));
5319 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
5320 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes
);
5321 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets
);
5322 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes
);
5324 #undef COPY_IF_DE_FIELD64_ATOMIC
5328 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
5330 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
5331 atomic_get_64(if_ps->ifi_tcp_##fld, \
5332 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
5335 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
5336 atomic_get_64(if_ps->ifi_udp_##fld, \
5337 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
5340 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
5341 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
5342 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
5343 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
5344 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
5345 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
5346 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
5347 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
5348 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
5349 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
5350 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
5351 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
5352 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
5353 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
5355 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
5356 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
5357 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
5358 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
5359 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
5360 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
5361 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
5362 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
5364 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5365 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5369 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
5371 bzero(if_rs
, sizeof(*if_rs
));
5372 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1)) {
5375 bcopy(&ifp
->if_poll_pstats
, if_rs
, sizeof(*if_rs
));
5376 /* Release the IO refcnt */
5377 ifnet_decr_iorefcnt(ifp
);
5381 if_copy_netif_stats(struct ifnet
*ifp
, struct if_netif_stats
*if_ns
)
5383 bzero(if_ns
, sizeof(*if_ns
));
5388 ifa_remref(struct ifaddr
*ifa
, int locked
)
5393 IFA_LOCK_ASSERT_HELD(ifa
);
5396 if (ifa
->ifa_refcnt
== 0) {
5397 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
5398 } else if (ifa
->ifa_trace
!= NULL
) {
5399 (*ifa
->ifa_trace
)(ifa
, FALSE
);
5401 if (--ifa
->ifa_refcnt
== 0) {
5402 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
5403 panic("ifa %p attached to ifp is being freed\n", ifa
);
5406 * Some interface addresses are allocated either statically
5407 * or carved out of a larger block. Only free it if it was
5408 * allocated via MALLOC or via the corresponding per-address
5409 * family allocator. Otherwise, leave it alone.
5411 if (ifa
->ifa_debug
& IFD_ALLOC
) {
5412 if (ifa
->ifa_free
== NULL
) {
5414 FREE(ifa
, M_IFADDR
);
5416 /* Become a regular mutex */
5417 IFA_CONVERT_LOCK(ifa
);
5418 /* callee will unlock */
5419 (*ifa
->ifa_free
)(ifa
);
5427 if (!locked
&& ifa
!= NULL
) {
5435 ifa_addref(struct ifaddr
*ifa
, int locked
)
5440 IFA_LOCK_ASSERT_HELD(ifa
);
5443 if (++ifa
->ifa_refcnt
== 0) {
5444 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
5446 } else if (ifa
->ifa_trace
!= NULL
) {
5447 (*ifa
->ifa_trace
)(ifa
, TRUE
);
5455 ifa_lock_init(struct ifaddr
*ifa
)
5457 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
5461 ifa_lock_destroy(struct ifaddr
*ifa
)
5463 IFA_LOCK_ASSERT_NOTHELD(ifa
);
5464 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);
5470 * The switch statement below does nothing at runtime, as it serves as a
5471 * compile time check to ensure that all of the socket 'i' ioctls (those
5472 * in the 'i' group going thru soo_ioctl) that are made available by the
5473 * networking stack is unique. This works as long as this routine gets
5474 * updated each time a new interface ioctl gets added.
5476 * Any failures at compile time indicates duplicated ioctl values.
5478 static __attribute__((unused
)) void
5479 ifioctl_cassert(void)
5482 * This is equivalent to _CASSERT() and the compiler wouldn't
5483 * generate any instructions, thus for compile time only.
5485 switch ((u_long
)0) {
5488 /* bsd/net/if_ppp.h */
5490 case SIOCGPPPCSTATS
:
5492 /* bsd/netinet6/in6_var.h */
5493 case SIOCSIFADDR_IN6
:
5494 case SIOCGIFADDR_IN6
:
5495 case SIOCSIFDSTADDR_IN6
:
5496 case SIOCSIFNETMASK_IN6
:
5497 case SIOCGIFDSTADDR_IN6
:
5498 case SIOCGIFNETMASK_IN6
:
5499 case SIOCDIFADDR_IN6
:
5500 case SIOCAIFADDR_IN6_32
:
5501 case SIOCAIFADDR_IN6_64
:
5502 case SIOCSIFPHYADDR_IN6_32
:
5503 case SIOCSIFPHYADDR_IN6_64
:
5504 case SIOCGIFPSRCADDR_IN6
:
5505 case SIOCGIFPDSTADDR_IN6
:
5506 case SIOCGIFAFLAG_IN6
:
5507 case SIOCGDRLST_IN6_32
:
5508 case SIOCGDRLST_IN6_64
:
5509 case SIOCGPRLST_IN6_32
:
5510 case SIOCGPRLST_IN6_64
:
5511 case OSIOCGIFINFO_IN6
:
5512 case SIOCGIFINFO_IN6
:
5513 case SIOCSNDFLUSH_IN6
:
5514 case SIOCGNBRINFO_IN6_32
:
5515 case SIOCGNBRINFO_IN6_64
:
5516 case SIOCSPFXFLUSH_IN6
:
5517 case SIOCSRTRFLUSH_IN6
:
5518 case SIOCGIFALIFETIME_IN6
:
5519 case SIOCSIFALIFETIME_IN6
:
5520 case SIOCGIFSTAT_IN6
:
5521 case SIOCGIFSTAT_ICMP6
:
5522 case SIOCSDEFIFACE_IN6_32
:
5523 case SIOCSDEFIFACE_IN6_64
:
5524 case SIOCGDEFIFACE_IN6_32
:
5525 case SIOCGDEFIFACE_IN6_64
:
5526 case SIOCSIFINFO_FLAGS
:
5529 case SIOCGSCOPE6DEF
:
5530 case SIOCSIFPREFIX_IN6
:
5531 case SIOCGIFPREFIX_IN6
:
5532 case SIOCDIFPREFIX_IN6
:
5533 case SIOCAIFPREFIX_IN6
:
5534 case SIOCCIFPREFIX_IN6
:
5535 case SIOCSGIFPREFIX_IN6
:
5536 case SIOCPROTOATTACH_IN6_32
:
5537 case SIOCPROTOATTACH_IN6_64
:
5538 case SIOCPROTODETACH_IN6
:
5539 case SIOCLL_START_32
:
5540 case SIOCLL_START_64
:
5542 case SIOCAUTOCONF_START
:
5543 case SIOCAUTOCONF_STOP
:
5544 case SIOCSETROUTERMODE_IN6
:
5545 case SIOCGETROUTERMODE_IN6
:
5546 case SIOCLL_CGASTART_32
:
5547 case SIOCLL_CGASTART_64
:
5548 case SIOCGIFCGAPREP_IN6
:
5549 case SIOCSIFCGAPREP_IN6
:
5551 /* bsd/sys/sockio.h */
5554 case SIOCSIFDSTADDR
:
5555 case OSIOCGIFDSTADDR
:
5558 case OSIOCGIFBRDADDR
:
5559 case SIOCSIFBRDADDR
:
5560 case OSIOCGIFCONF32
:
5561 case OSIOCGIFCONF64
:
5562 case OSIOCGIFNETMASK
:
5563 case SIOCSIFNETMASK
:
5570 case SIOCGIFDSTADDR
:
5571 case SIOCGIFBRDADDR
:
5574 case SIOCGIFNETMASK
:
5576 case SIOCAUTONETMASK
:
5586 case SIOCGIFMEDIA32
:
5587 case SIOCGIFMEDIA64
:
5588 case SIOCGIFXMEDIA32
:
5589 case SIOCGIFXMEDIA64
:
5590 case SIOCSIFGENERIC
:
5591 case SIOCGIFGENERIC
:
5596 case SIOCSIFPHYADDR
:
5597 case SIOCGIFPSRCADDR
:
5598 case SIOCGIFPDSTADDR
:
5599 case SIOCDIFPHYADDR
:
5607 case SIOCPROTOATTACH
:
5608 case SIOCPROTODETACH
:
5617 case SIOCSDRVSPEC32
:
5618 case SIOCGDRVSPEC32
:
5619 case SIOCSDRVSPEC64
:
5620 case SIOCGDRVSPEC64
:
5625 case SIOCIFGCLONERS32
:
5626 case SIOCIFGCLONERS64
:
5628 case SIOCGIFASYNCMAP
:
5629 case SIOCSIFASYNCMAP
:
5633 case SIOCGIFWAKEFLAGS
:
5635 case SIOCGIFGETRTREFCNT
:
5636 case SIOCGIFLINKQUALITYMETRIC
:
5637 case SIOCSIFOPPORTUNISTIC
:
5638 case SIOCGIFOPPORTUNISTIC
:
5639 case SIOCGETROUTERMODE
:
5640 case SIOCSETROUTERMODE
:
5644 case SIOCSIFLINKPARAMS
:
5645 case SIOCGIFLINKPARAMS
:
5646 case SIOCGIFQUEUESTATS
:
5647 case SIOCSIFTHROTTLE
:
5648 case SIOCGIFTHROTTLE
:
5650 case SIOCGASSOCIDS32
:
5651 case SIOCGASSOCIDS64
:
5652 case SIOCGCONNIDS32
:
5653 case SIOCGCONNIDS64
:
5654 case SIOCGCONNINFO32
:
5655 case SIOCGCONNINFO64
:
5656 case SIOCSCONNORDER
:
5657 case SIOCGCONNORDER
:
5661 case SIOCGIFDELEGATE
:
5664 case SIOCGIFEXPENSIVE
:
5665 case SIOCSIFEXPENSIVE
:
5668 case SIOCGSTARTDELAY
:
5670 case SIOCAIFAGENTID
:
5671 case SIOCDIFAGENTID
:
5672 case SIOCGIFAGENTIDS32
:
5673 case SIOCGIFAGENTIDS64
:
5674 case SIOCGIFAGENTDATA32
:
5675 case SIOCGIFAGENTDATA64
:
5677 case SIOCSIFINTERFACESTATE
:
5678 case SIOCGIFINTERFACESTATE
:
5679 case SIOCSIFPROBECONNECTIVITY
:
5680 case SIOCGIFPROBECONNECTIVITY
:
5682 case SIOCGIFFUNCTIONALTYPE
:
5683 case SIOCSIFNETSIGNATURE
:
5684 case SIOCGIFNETSIGNATURE
:
5686 case SIOCSIFNETWORKID
:
5692 case SIOCSQOSMARKINGMODE
:
5693 case SIOCSQOSMARKINGENABLED
:
5694 case SIOCGQOSMARKINGMODE
:
5695 case SIOCGQOSMARKINGENABLED
:
5697 case SIOCSIFTIMESTAMPENABLE
:
5698 case SIOCSIFTIMESTAMPDISABLE
:
5699 case SIOCGIFTIMESTAMPENABLED
:
5701 case SIOCSIFDISABLEOUTPUT
:
5703 case SIOCSIFSUBFAMILY
:
5705 case SIOCGIFAGENTLIST32
:
5706 case SIOCGIFAGENTLIST64
:
5708 case SIOCSIFLOWINTERNET
:
5709 case SIOCGIFLOWINTERNET
:
5711 case SIOCGIFNAT64PREFIX
:
5712 case SIOCSIFNAT64PREFIX
:
5714 case SIOCGIFCLAT46ADDR
:
5716 case SIOCGIFPROTOLIST32
:
5717 case SIOCGIFPROTOLIST64
:
5719 case SIOCSIF6LOWPAN
:
5720 case SIOCGIF6LOWPAN
:
5722 case SIOCGIFLOWPOWER
:
5723 case SIOCSIFLOWPOWER
:
5728 case SIOCGIFCONSTRAINED
:
5729 case SIOCSIFCONSTRAINED
:
5733 case SIOCGIFNOACKPRIO
:
5734 case SIOCSIFNOACKPRIO
:
5740 ifnet_mbuf_packetpreamblelen(struct ifnet
*ifp
)
5746 /* The following is used to enqueue work items for interface events */
5749 union sockaddr_in_4_6 addr
;
5750 uint32_t intf_event_code
;
5754 intf_event_callback(void *arg
)
5756 struct intf_event
*p_intf_ev
= (struct intf_event
*)arg
;
5758 /* Call this before we walk the tree */
5759 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt
, ifnet_event
, p_intf_ev
->ifp
,
5760 (struct sockaddr
*)&(p_intf_ev
->addr
), p_intf_ev
->intf_event_code
);
5763 struct intf_event_nwk_wq_entry
{
5764 struct nwk_wq_entry nwk_wqe
;
5765 struct intf_event intf_ev_arg
;
5769 intf_event_enqueue_nwk_wq_entry(struct ifnet
*ifp
, struct sockaddr
*addrp
,
5770 uint32_t intf_event_code
)
5772 #pragma unused(addrp)
5773 struct intf_event_nwk_wq_entry
*p_intf_ev
= NULL
;
5775 MALLOC(p_intf_ev
, struct intf_event_nwk_wq_entry
*,
5776 sizeof(struct intf_event_nwk_wq_entry
),
5777 M_NWKWQ
, M_WAITOK
| M_ZERO
);
5779 p_intf_ev
->intf_ev_arg
.ifp
= ifp
;
5781 * XXX Not using addr in the arg. This will be used
5782 * once we need IP address add/delete events
5784 p_intf_ev
->intf_ev_arg
.intf_event_code
= intf_event_code
;
5785 p_intf_ev
->nwk_wqe
.func
= intf_event_callback
;
5786 p_intf_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
5787 p_intf_ev
->nwk_wqe
.arg
= &p_intf_ev
->intf_ev_arg
;
5788 nwk_wq_enqueue((struct nwk_wq_entry
*)p_intf_ev
);
5792 if_get_tcp_kao_max(struct ifnet
*ifp
)
5796 if (ifp
->if_tcp_kao_max
== 0) {
5799 memset(&ifr
, 0, sizeof(struct ifreq
));
5800 error
= ifnet_ioctl(ifp
, 0, SIOCGIFTCPKAOMAX
, &ifr
);
5802 ifnet_lock_exclusive(ifp
);
5804 ifp
->if_tcp_kao_max
= ifr
.ifr_tcp_kao_max
;
5805 } else if (error
== EOPNOTSUPP
) {
5806 ifp
->if_tcp_kao_max
= default_tcp_kao_max
;
5808 ifnet_lock_done(ifp
);