2 * Copyright (c) 2000-2016 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_ppp.h>
100 #include <net/ethernet.h>
101 #include <net/network_agent.h>
103 #include <net/radix.h>
104 #include <net/route.h>
105 #include <net/dlil.h>
106 #include <sys/domain.h>
107 #include <libkern/OSAtomic.h>
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112 #include <netinet/in_tclass.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/ip6.h>
115 #include <netinet/ip_var.h>
116 #include <netinet/tcp.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/udp.h>
119 #include <netinet/udp_var.h>
121 #include <netinet6/in6_var.h>
122 #include <netinet6/in6_ifattach.h>
123 #include <netinet6/ip6_var.h>
124 #include <netinet6/nd6.h>
126 #endif /* INET || INET6 */
129 #include <security/mac_framework.h>
133 #include <net/altq/if_altq.h>
134 #endif /* !PF_ALTQ */
137 * System initialization
140 extern char *proc_name_address(void *);
142 /* Lock group and attribute for ifaddr lock */
143 lck_attr_t
*ifa_mtx_attr
;
144 lck_grp_t
*ifa_mtx_grp
;
145 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
147 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
149 static int ifioctl_ifconf(u_long
, caddr_t
);
150 static int ifioctl_ifclone(u_long
, caddr_t
);
151 static int ifioctl_iforder(u_long
, caddr_t
);
152 static int ifioctl_ifdesc(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
153 static int ifioctl_linkparams(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
154 static int ifioctl_qstats(struct ifnet
*, u_long
, caddr_t
);
155 static int ifioctl_throttle(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
156 static int ifioctl_netsignature(struct ifnet
*, u_long
, caddr_t
);
157 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
158 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
159 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
161 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
162 struct ifmultiaddr
**, int);
163 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
164 const struct sockaddr
*, int);
165 static struct ifnet
*ifunit_common(const char *, boolean_t
);
167 static int if_rtmtu(struct radix_node
*, void *);
168 static void if_rtmtu_update(struct ifnet
*);
170 static int if_clone_list(int, int *, user_addr_t
);
172 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
174 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
176 /* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
177 struct ifnethead ifnet_ordered_head
= TAILQ_HEAD_INITIALIZER(ifnet_ordered_head
);
178 static u_int32_t if_ordered_count
= 0;
180 static int if_cloners_count
;
181 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
183 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
185 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
186 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
188 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
189 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
191 static struct ifmultiaddr
*ifma_alloc(int);
192 static void ifma_free(struct ifmultiaddr
*);
193 static void ifma_trace(struct ifmultiaddr
*, int);
196 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
198 static unsigned int ifma_debug
; /* debugging (disabled) */
200 static unsigned int ifma_size
; /* size of zone element */
201 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
203 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
206 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
208 struct ifmultiaddr_dbg
{
209 struct ifmultiaddr ifma
; /* ifmultiaddr */
210 u_int16_t ifma_refhold_cnt
; /* # of ref */
211 u_int16_t ifma_refrele_cnt
; /* # of rele */
213 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
215 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
216 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
220 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
223 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
224 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
225 static decl_lck_mtx_data(, ifma_trash_lock
);
227 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
228 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
232 * XXX: declare here to avoid to include many inet6 related files..
233 * should be more generalized?
235 extern void nd6_setmtu(struct ifnet
*);
236 extern lck_mtx_t
*nd6_mutex
;
239 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
240 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
241 "Generic link-management");
243 SYSCTL_DECL(_net_link_generic_system
);
245 static uint32_t if_verbose
= 0;
246 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, if_verbose
,
247 CTLFLAG_RW
| CTLFLAG_LOCKED
, &if_verbose
, 0, "");
252 /* Setup lock group and attribute for ifaddr */
253 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
254 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
255 ifa_mtx_attr
= lck_attr_alloc_init();
257 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof (ifma_debug
));
259 ifma_size
= (ifma_debug
== 0) ? sizeof (struct ifmultiaddr
) :
260 sizeof (struct ifmultiaddr_dbg
);
262 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
264 if (ifma_zone
== NULL
) {
265 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
268 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
269 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
271 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
272 TAILQ_INIT(&ifma_trash_head
);
276 * Network interface utility routines.
278 * Routines with ifa_ifwith* names take sockaddr *'s as
283 struct ifaddr
**ifnet_addrs
;
284 struct ifnet
**ifindex2ifnet
;
286 __private_extern__
void
287 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
289 if_attach_ifa_common(ifp
, ifa
, 0);
292 __private_extern__
void
293 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
295 if_attach_ifa_common(ifp
, ifa
, 1);
299 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
301 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
302 IFA_LOCK_ASSERT_HELD(ifa
);
304 if (ifa
->ifa_ifp
!= ifp
) {
305 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
308 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
309 panic("%s: Attempt to attach an already attached ifa=%p",
312 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
313 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
315 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
316 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
319 IFA_ADDREF_LOCKED(ifa
);
320 ifa
->ifa_debug
|= IFD_ATTACHED
;
322 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
324 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
326 if (ifa
->ifa_attached
!= NULL
)
327 (*ifa
->ifa_attached
)(ifa
);
330 __private_extern__
void
331 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
333 if_detach_ifa_common(ifp
, ifa
, 0);
336 __private_extern__
void
337 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
339 if_detach_ifa_common(ifp
, ifa
, 1);
343 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
345 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
346 IFA_LOCK_ASSERT_HELD(ifa
);
348 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
349 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
351 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
352 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
354 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
355 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
357 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
358 panic("%s: Attempt to detach an unattached address ifa=%p",
361 } else if (ifa
->ifa_ifp
!= ifp
) {
362 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
365 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
367 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
372 panic("%s: Attempt to detach a stray address ifa=%p",
377 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
378 /* This must not be the last reference to the ifaddr */
379 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
380 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
383 ifa
->ifa_debug
&= ~(IFD_ATTACHED
| IFD_DETACHING
);
385 if (ifa
->ifa_detached
!= NULL
)
386 (*ifa
->ifa_detached
)(ifa
);
389 #define INITIAL_IF_INDEXLIM 8
392 * Function: if_next_index
394 * Return the next available interface index.
395 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
396 * added entry when necessary.
399 * ifnet_addrs[] is indexed by (if_index - 1), whereas
400 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
401 * always allocate one extra element to hold ifindex2ifnet[0], which
404 int if_next_index(void);
406 __private_extern__
int
409 static int if_indexlim
= 0;
412 new_index
= ++if_index
;
413 if (if_index
> if_indexlim
) {
416 caddr_t new_ifnet_addrs
;
417 caddr_t new_ifindex2ifnet
;
418 caddr_t old_ifnet_addrs
;
420 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
421 if (ifnet_addrs
== NULL
) {
422 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
424 new_if_indexlim
= if_indexlim
<< 1;
427 /* allocate space for the larger arrays */
428 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
429 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
430 if (new_ifnet_addrs
== NULL
) {
435 new_ifindex2ifnet
= new_ifnet_addrs
436 + new_if_indexlim
* sizeof(caddr_t
);
437 if (ifnet_addrs
!= NULL
) {
438 /* copy the existing data */
439 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
440 if_indexlim
* sizeof(caddr_t
));
441 bcopy((caddr_t
)ifindex2ifnet
,
443 (if_indexlim
+ 1) * sizeof(caddr_t
));
446 /* switch to the new tables and size */
447 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
448 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
449 if_indexlim
= new_if_indexlim
;
451 /* release the old data */
452 if (old_ifnet_addrs
!= NULL
) {
453 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
460 * Create a clone network interface.
463 if_clone_create(char *name
, int len
, void *params
)
465 struct if_clone
*ifc
;
468 u_int32_t bytoff
, bitoff
;
472 ifc
= if_clone_lookup(name
, &unit
);
476 if (ifunit(name
) != NULL
)
480 wildcard
= (unit
== UINT32_MAX
);
482 * Find a free unit if none was given.
485 while ((bytoff
< ifc
->ifc_bmlen
) &&
486 (ifc
->ifc_units
[bytoff
] == 0xff))
488 if (bytoff
>= ifc
->ifc_bmlen
)
490 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
492 unit
= (bytoff
<< 3) + bitoff
;
495 if (unit
> ifc
->ifc_maxunit
)
498 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
504 bitoff
= unit
- (bytoff
<< 3);
508 * Allocate the unit in the bitmap.
510 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
511 ("%s: bit is already set", __func__
));
512 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
514 /* In the wildcard case, we need to update the name. */
516 for (dp
= name
; *dp
!= '\0'; dp
++);
517 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
518 len
- (dp
-name
) - 1) {
520 * This can only be a programmer error and
521 * there's no straightforward way to recover if
524 panic("%s: interface name too long", __func__
);
534 * Destroy a clone network interface.
537 if_clone_destroy(const char *name
)
539 struct if_clone
*ifc
;
544 ifc
= if_clone_lookup(name
, &unit
);
548 if (unit
< ifc
->ifc_minifs
)
555 if (ifc
->ifc_destroy
== NULL
)
558 (*ifc
->ifc_destroy
)(ifp
);
561 * Compute offset in the bitmap and deallocate the unit.
564 bitoff
= unit
- (bytoff
<< 3);
565 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
566 ("%s: bit is already cleared", __func__
));
567 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
572 * Look up a network interface cloner.
575 __private_extern__
struct if_clone
*
576 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
578 struct if_clone
*ifc
;
582 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
; ) {
583 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
584 if (ifc
->ifc_name
[i
] != *cp
)
589 ifc
= LIST_NEXT(ifc
, ifc_list
);
593 return ((struct if_clone
*)NULL
);
599 for (i
= 0; *cp
!= '\0'; cp
++) {
600 if (*cp
< '0' || *cp
> '9') {
601 /* Bogus unit number. */
604 i
= (i
* 10) + (*cp
- '0');
614 * Register a network interface cloner.
617 if_clone_attach(struct if_clone
*ifc
)
624 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
625 ("%s: %s requested more units then allowed (%d > %d)",
626 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
627 ifc
->ifc_maxunit
+ 1));
629 * Compute bitmap size and allocate it.
631 maxclone
= ifc
->ifc_maxunit
+ 1;
633 if ((len
<< 3) < maxclone
)
635 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
636 if (ifc
->ifc_units
== NULL
)
638 ifc
->ifc_bmlen
= len
;
640 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
643 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
644 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
646 ("%s: failed to create required interface %s%d",
647 __func__
, ifc
->ifc_name
, unit
));
649 /* Allocate the unit in the bitmap. */
651 bitoff
= unit
- (bytoff
<< 3);
652 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
659 * Unregister a network interface cloner.
662 if_clone_detach(struct if_clone
*ifc
)
664 LIST_REMOVE(ifc
, ifc_list
);
665 FREE(ifc
->ifc_units
, M_CLONE
);
670 * Provide list of interface cloners to userspace.
673 if_clone_list(int count
, int *ret_total
, user_addr_t dst
)
675 char outbuf
[IFNAMSIZ
];
676 struct if_clone
*ifc
;
679 *ret_total
= if_cloners_count
;
680 if (dst
== USER_ADDR_NULL
) {
681 /* Just asking how many there are. */
688 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
690 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
691 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
692 bzero(outbuf
, sizeof(outbuf
));
693 strlcpy(outbuf
, ifc
->ifc_name
,
694 min(strlen(ifc
->ifc_name
), IFNAMSIZ
));
695 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
704 if_functional_type(struct ifnet
*ifp
, bool exclude_delegate
)
706 u_int32_t ret
= IFRTYPE_FUNCTIONAL_UNKNOWN
;
708 if (ifp
->if_flags
& IFF_LOOPBACK
) {
709 ret
= IFRTYPE_FUNCTIONAL_LOOPBACK
;
710 } else if ((exclude_delegate
&&
711 (ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
)) ||
712 (!exclude_delegate
&& IFNET_IS_WIFI(ifp
))) {
713 if (ifp
->if_eflags
& IFEF_AWDL
)
714 ret
= IFRTYPE_FUNCTIONAL_WIFI_AWDL
;
716 ret
= IFRTYPE_FUNCTIONAL_WIFI_INFRA
;
717 } else if ((exclude_delegate
&&
718 (ifp
->if_type
== IFT_CELLULAR
)) ||
719 (!exclude_delegate
&& IFNET_IS_CELLULAR(ifp
))) {
720 ret
= IFRTYPE_FUNCTIONAL_CELLULAR
;
721 } else if (IFNET_IS_INTCOPROC(ifp
)) {
722 ret
= IFRTYPE_FUNCTIONAL_INTCOPROC
;
723 } else if ((exclude_delegate
&&
724 (ifp
->if_family
== IFNET_FAMILY_ETHERNET
||
725 ifp
->if_family
== IFNET_FAMILY_FIREWIRE
)) ||
726 (!exclude_delegate
&& IFNET_IS_WIRED(ifp
))) {
727 ret
= IFRTYPE_FUNCTIONAL_WIRED
;
735 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
736 * and that it matches only the local (not broadcast) address.
738 __private_extern__
struct in_ifaddr
*
739 ifa_foraddr(unsigned int addr
)
741 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
745 * Similar to ifa_foraddr, except with the added interface scope
746 * constraint (unless the caller passes in IFSCOPE_NONE in which
747 * case there is no scope restriction).
749 __private_extern__
struct in_ifaddr
*
750 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
752 struct in_ifaddr
*ia
= NULL
;
754 lck_rw_lock_shared(in_ifaddr_rwlock
);
755 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
756 IFA_LOCK_SPIN(&ia
->ia_ifa
);
757 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
758 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
759 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
760 IFA_UNLOCK(&ia
->ia_ifa
);
763 IFA_UNLOCK(&ia
->ia_ifa
);
765 lck_rw_done(in_ifaddr_rwlock
);
771 * Similar to ifa_foraddr, except that this for IPv6.
773 __private_extern__
struct in6_ifaddr
*
774 ifa_foraddr6(struct in6_addr
*addr6
)
776 return (ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
));
779 __private_extern__
struct in6_ifaddr
*
780 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
782 struct in6_ifaddr
*ia
= NULL
;
784 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
785 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
786 IFA_LOCK(&ia
->ia_ifa
);
787 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
788 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
789 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
790 IFA_UNLOCK(&ia
->ia_ifa
);
793 IFA_UNLOCK(&ia
->ia_ifa
);
795 lck_rw_done(&in6_ifaddr_rwlock
);
802 * Return the first (primary) address of a given family on an interface.
804 __private_extern__
struct ifaddr
*
805 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
809 ifnet_lock_shared(ifp
);
810 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
812 if (ifa
->ifa_addr
->sa_family
== family
) {
813 IFA_ADDREF_LOCKED(ifa
); /* for caller */
819 ifnet_lock_done(ifp
);
825 ifa_equal(const struct sockaddr
*sa1
, const struct sockaddr
*sa2
)
830 if (sa1
->sa_len
!= sa2
->sa_len
)
833 return (bcmp(sa1
, sa2
, sa1
->sa_len
) == 0);
837 * Locate an interface based on a complete address.
840 ifa_ifwithaddr_locked(const struct sockaddr
*addr
)
844 struct ifaddr
*result
= NULL
;
846 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
847 ifp
= ifp
->if_link
.tqe_next
) {
848 ifnet_lock_shared(ifp
);
849 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
850 ifa
= ifa
->ifa_link
.tqe_next
) {
852 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
856 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
858 IFA_ADDREF_LOCKED(ifa
); /* for caller */
862 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
863 ifa
->ifa_broadaddr
!= NULL
&&
864 /* IP6 doesn't have broadcast */
865 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
866 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
868 IFA_ADDREF_LOCKED(ifa
); /* for caller */
874 ifnet_lock_done(ifp
);
881 ifa_ifwithaddr(const struct sockaddr
*addr
)
883 struct ifaddr
*result
= NULL
;
885 ifnet_head_lock_shared();
887 result
= ifa_ifwithaddr_locked(addr
);
894 * Locate the point to point interface with a given destination address.
898 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
902 struct ifaddr
*result
= NULL
;
904 ifnet_head_lock_shared();
905 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
906 ifp
= ifp
->if_link
.tqe_next
) {
907 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
908 ifnet_lock_shared(ifp
);
909 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
910 ifa
= ifa
->ifa_link
.tqe_next
) {
912 if (ifa
->ifa_addr
->sa_family
!=
917 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
919 IFA_ADDREF_LOCKED(ifa
); /* for caller */
925 ifnet_lock_done(ifp
);
933 * Locate the source address of an interface based on a complete address.
936 ifa_ifwithaddr_scoped_locked(const struct sockaddr
*addr
, unsigned int ifscope
)
938 struct ifaddr
*result
= NULL
;
941 if (ifscope
== IFSCOPE_NONE
)
942 return (ifa_ifwithaddr_locked(addr
));
944 if (ifscope
> (unsigned int)if_index
) {
948 ifp
= ifindex2ifnet
[ifscope
];
950 struct ifaddr
*ifa
= NULL
;
953 * This is suboptimal; there should be a better way
954 * to search for a given address of an interface
955 * for any given address family.
957 ifnet_lock_shared(ifp
);
958 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
959 ifa
= ifa
->ifa_link
.tqe_next
) {
961 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
965 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
967 IFA_ADDREF_LOCKED(ifa
); /* for caller */
971 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
972 ifa
->ifa_broadaddr
!= NULL
&&
973 /* IP6 doesn't have broadcast */
974 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
975 ifa_equal(ifa
->ifa_broadaddr
, addr
)) {
977 IFA_ADDREF_LOCKED(ifa
); /* for caller */
983 ifnet_lock_done(ifp
);
990 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
992 struct ifaddr
*result
= NULL
;
994 ifnet_head_lock_shared();
996 result
= ifa_ifwithaddr_scoped_locked(addr
, ifscope
);
1004 ifa_ifwithnet(const struct sockaddr
*addr
)
1006 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
1010 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
1012 return (ifa_ifwithnet_common(addr
, ifscope
));
1016 * Find an interface on a specific network. If many, choice
1017 * is most specific found.
1019 static struct ifaddr
*
1020 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
1023 struct ifaddr
*ifa
= NULL
;
1024 struct ifaddr
*ifa_maybe
= NULL
;
1025 u_int af
= addr
->sa_family
;
1026 const char *addr_data
= addr
->sa_data
, *cplim
;
1029 if (af
!= AF_INET
&& af
!= AF_INET6
)
1033 ifscope
= IFSCOPE_NONE
;
1035 ifnet_head_lock_shared();
1037 * AF_LINK addresses can be looked up directly by their index number,
1038 * so do that if we can.
1040 if (af
== AF_LINK
) {
1041 const struct sockaddr_dl
*sdl
=
1042 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
1043 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
1044 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
1054 * Scan though each interface, looking for ones that have
1055 * addresses in this address family.
1057 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1058 ifnet_lock_shared(ifp
);
1059 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1060 ifa
= ifa
->ifa_link
.tqe_next
) {
1061 const char *cp
, *cp2
, *cp3
;
1064 if (ifa
->ifa_addr
== NULL
||
1065 ifa
->ifa_addr
->sa_family
!= af
) {
1071 * If we're looking up with a scope,
1072 * find using a matching interface.
1074 if (ifscope
!= IFSCOPE_NONE
&&
1075 ifp
->if_index
!= ifscope
) {
1081 * Scan all the bits in the ifa's address.
1082 * If a bit dissagrees with what we are
1083 * looking for, mask it with the netmask
1084 * to see if it really matters.
1085 * (A byte at a time)
1087 if (ifa
->ifa_netmask
== 0) {
1092 cp2
= ifa
->ifa_addr
->sa_data
;
1093 cp3
= ifa
->ifa_netmask
->sa_data
;
1094 cplim
= ifa
->ifa_netmask
->sa_len
+
1095 (char *)ifa
->ifa_netmask
;
1097 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1098 goto next
; /* next address! */
1100 * If the netmask of what we just found
1101 * is more specific than what we had before
1102 * (if we had one) then remember the new one
1103 * before continuing to search
1104 * for an even better one.
1106 if (ifa_maybe
== NULL
||
1107 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1108 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1109 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1111 if (ifa_maybe
!= NULL
)
1112 IFA_REMREF(ifa_maybe
);
1117 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1119 ifnet_lock_done(ifp
);
1128 else if (ifa_maybe
!= NULL
)
1129 IFA_REMREF(ifa_maybe
);
1135 * Find an interface address specific to an interface best matching
1139 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1141 struct ifaddr
*ifa
= NULL
;
1142 const char *cp
, *cp2
, *cp3
;
1144 struct ifaddr
*ifa_maybe
= NULL
;
1145 struct ifaddr
*better_ifa_maybe
= NULL
;
1146 u_int af
= addr
->sa_family
;
1151 ifnet_lock_shared(ifp
);
1152 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1153 ifa
= ifa
->ifa_link
.tqe_next
) {
1155 if (ifa
->ifa_addr
->sa_family
!= af
) {
1159 if (ifa_maybe
== NULL
) {
1160 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1163 if (ifa
->ifa_netmask
== 0) {
1164 if (ifa_equal(addr
, ifa
->ifa_addr
) ||
1165 ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1166 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1173 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1174 if (ifa_equal(addr
, ifa
->ifa_dstaddr
)) {
1175 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1180 if (ifa_equal(addr
, ifa
->ifa_addr
)) {
1182 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1187 cp2
= ifa
->ifa_addr
->sa_data
;
1188 cp3
= ifa
->ifa_netmask
->sa_data
;
1189 cplim
= ifa
->ifa_netmask
->sa_len
+
1190 (char *)ifa
->ifa_netmask
;
1191 for (; cp3
< cplim
; cp3
++)
1192 if ((*cp
++ ^ *cp2
++) & *cp3
)
1196 if (better_ifa_maybe
== NULL
) {
1197 /* for better_ifa_maybe */
1198 IFA_ADDREF_LOCKED(ifa
);
1199 better_ifa_maybe
= ifa
;
1207 if (better_ifa_maybe
!= NULL
) {
1208 ifa
= better_ifa_maybe
;
1209 better_ifa_maybe
= NULL
;
1216 ifnet_lock_done(ifp
);
1218 if (better_ifa_maybe
!= NULL
)
1219 IFA_REMREF(better_ifa_maybe
);
1220 if (ifa_maybe
!= NULL
)
1221 IFA_REMREF(ifa_maybe
);
1226 #include <net/route.h>
1229 * Default action when installing a route with a Link Level gateway.
1230 * Lookup an appropriate real ifa to point to.
1231 * This should be moved to /sys/net/link.c eventually.
1234 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1237 struct sockaddr
*dst
;
1239 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1241 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1242 RT_LOCK_ASSERT_HELD(rt
);
1244 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1245 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
1248 /* Become a regular mutex, just in case */
1249 RT_CONVERT_LOCK(rt
);
1251 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1255 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1257 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
)
1258 ifa_rtrequest(cmd
, rt
, sa
);
1264 * if_updown will set the interface up or down. It will
1265 * prevent other up/down events from occurring until this
1266 * up/down event has completed.
1268 * Caller must lock ifnet. This function will drop the
1269 * lock. This allows ifnet_set_flags to set the rest of
1270 * the flags after we change the up/down state without
1271 * dropping the interface lock between setting the
1272 * up/down state and updating the rest of the flags.
1274 __private_extern__
void
1275 if_updown( struct ifnet
*ifp
, int up
)
1278 struct ifaddr
**ifa
;
1280 struct ifclassq
*ifq
= &ifp
->if_snd
;
1282 /* Wait until no one else is changing the up/down state */
1283 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1285 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1286 ifnet_lock_done(ifp
);
1287 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1288 ifnet_lock_exclusive(ifp
);
1291 /* Verify that the interface isn't already in the right state */
1292 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1293 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1297 /* Indicate that the up/down state is changing */
1298 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1300 /* Mark interface up or down */
1302 ifp
->if_flags
|= IFF_UP
;
1304 ifp
->if_flags
&= ~IFF_UP
;
1307 ifnet_touch_lastchange(ifp
);
1309 /* Drop the lock to notify addresses and route */
1310 ifnet_lock_done(ifp
);
1315 /* Inform all transmit queues about the new link state */
1316 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1319 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1320 for (i
= 0; ifa
[i
] != 0; i
++) {
1321 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1323 ifnet_free_address_list(ifa
);
1327 /* Aquire the lock to clear the changing flag */
1328 ifnet_lock_exclusive(ifp
);
1329 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1330 wakeup(&ifp
->if_eflags
);
1334 * Mark an interface down and notify protocols of
1341 ifnet_lock_exclusive(ifp
);
1343 ifnet_lock_done(ifp
);
1347 * Mark an interface up and notify protocols of
1354 ifnet_lock_exclusive(ifp
);
1356 ifnet_lock_done(ifp
);
1360 * Flush an interface queue.
1363 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1365 struct ifclassq
*ifq
= &ifp
->if_snd
;
1370 if (IFCQ_IS_ENABLED(ifq
))
1373 if (IFCQ_IS_DRAINING(ifq
))
1374 ifq
->ifcq_drain
= 0;
1375 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1376 ALTQ_PURGE(IFCQ_ALTQ(ifq
));
1377 #endif /* PF_ALTQ */
1379 VERIFY(IFCQ_IS_EMPTY(ifq
));
1386 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1387 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1389 struct ifclassq
*ifq
= &ifp
->if_snd
;
1390 u_int32_t cnt
= 0, len
= 0;
1391 u_int32_t a_cnt
= 0, a_len
= 0;
1393 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1399 if (IFCQ_IS_ENABLED(ifq
))
1400 IFCQ_PURGE_SC(ifq
, sc
, flow
, cnt
, len
);
1402 if (IFCQ_IS_DRAINING(ifq
)) {
1403 VERIFY((signed)(ifq
->ifcq_drain
- cnt
) >= 0);
1404 ifq
->ifcq_drain
-= cnt
;
1406 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1407 ALTQ_PURGE_SC(IFCQ_ALTQ(ifq
), sc
, flow
, a_cnt
, a_len
);
1408 #endif /* PF_ALTQ */
1413 if (packets
!= NULL
)
1414 *packets
= cnt
+ a_cnt
;
1416 *bytes
= len
+ a_len
;
1420 * Extracts interface unit number and name from string, returns -1 upon failure.
1421 * Upon success, returns extracted unit number, and interface name in dst.
1424 ifunit_extract(const char *src
, char *dst
, size_t dstlen
, int *unit
)
1431 if (src
== NULL
|| dst
== NULL
|| dstlen
== 0 || unit
== NULL
)
1435 if (len
< 2 || len
> dstlen
)
1439 if (c
< '0' || c
> '9')
1440 return (-1); /* trailing garbage */
1445 return (-1); /* no interface name */
1448 return (-1); /* number is unreasonable */
1451 } while (c
>= '0' && c
<= '9');
1453 bcopy(src
, dst
, len
);
1461 * Map interface name to
1462 * interface structure pointer.
1464 static struct ifnet
*
1465 ifunit_common(const char *name
, boolean_t hold
)
1467 char namebuf
[IFNAMSIZ
+ 1];
1471 if (ifunit_extract(name
, namebuf
, sizeof (namebuf
), &unit
) < 0)
1474 /* for safety, since we use strcmp() below */
1475 namebuf
[sizeof (namebuf
) - 1] = '\0';
1478 * Now search all the interfaces for this name/number
1480 ifnet_head_lock_shared();
1481 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1483 * Use strcmp() rather than strncmp() here,
1484 * since we want to match the entire string.
1486 if (strcmp(ifp
->if_name
, namebuf
))
1488 if (unit
== ifp
->if_unit
)
1492 /* if called from ifunit_ref() and ifnet is not attached, bail */
1493 if (hold
&& ifp
!= NULL
&& !ifnet_is_attached(ifp
, 1))
1501 ifunit(const char *name
)
1503 return (ifunit_common(name
, FALSE
));
1507 * Similar to ifunit(), except that we hold an I/O reference count on an
1508 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1509 * Will return NULL unless interface exists and is fully attached.
1512 ifunit_ref(const char *name
)
1514 return (ifunit_common(name
, TRUE
));
1518 * Map interface name in a sockaddr_dl to
1519 * interface structure pointer.
1522 if_withname(struct sockaddr
*sa
)
1524 char ifname
[IFNAMSIZ
+1];
1525 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1527 if ((sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1528 (sdl
->sdl_nlen
> IFNAMSIZ
))
1532 * ifunit wants a null-terminated name. It may not be null-terminated
1533 * in the sockaddr. We don't want to change the caller's sockaddr,
1534 * and there might not be room to put the trailing null anyway, so we
1535 * make a local copy that we know we can null terminate safely.
1538 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1539 ifname
[sdl
->sdl_nlen
] = '\0';
1540 return (ifunit(ifname
));
1543 static __attribute__((noinline
)) int
1544 ifioctl_ifconf(u_long cmd
, caddr_t data
)
1549 case OSIOCGIFCONF32
: /* struct ifconf32 */
1550 case SIOCGIFCONF32
: { /* struct ifconf32 */
1551 struct ifconf32 ifc
;
1552 bcopy(data
, &ifc
, sizeof (ifc
));
1553 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1555 bcopy(&ifc
, data
, sizeof (ifc
));
1559 case SIOCGIFCONF64
: /* struct ifconf64 */
1560 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1561 struct ifconf64 ifc
;
1562 bcopy(data
, &ifc
, sizeof (ifc
));
1563 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1564 bcopy(&ifc
, data
, sizeof (ifc
));
1576 static __attribute__((noinline
)) int
1577 ifioctl_ifclone(u_long cmd
, caddr_t data
)
1582 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1583 struct if_clonereq32 ifcr
;
1584 bcopy(data
, &ifcr
, sizeof (ifcr
));
1585 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1586 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1587 bcopy(&ifcr
, data
, sizeof (ifcr
));
1591 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1592 struct if_clonereq64 ifcr
;
1593 bcopy(data
, &ifcr
, sizeof (ifcr
));
1594 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1596 bcopy(&ifcr
, data
, sizeof (ifcr
));
1608 static __attribute__((noinline
)) int
1609 ifioctl_ifdesc(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1611 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1615 VERIFY(ifp
!= NULL
);
1618 case SIOCSIFDESC
: { /* struct if_descreq */
1619 if ((error
= proc_suser(p
)) != 0)
1622 ifnet_lock_exclusive(ifp
);
1623 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof (ifdr_len
));
1624 if (ifdr_len
> sizeof (ifdr
->ifdr_desc
) ||
1625 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1627 ifnet_lock_done(ifp
);
1631 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1632 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1633 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1634 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1636 ifnet_lock_done(ifp
);
1640 case SIOCGIFDESC
: { /* struct if_descreq */
1641 ifnet_lock_shared(ifp
);
1642 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof (ifdr
->ifdr_desc
));
1643 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof (ifdr_len
));
1644 bzero(&ifdr
->ifdr_desc
, sizeof (ifdr
->ifdr_desc
));
1646 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1648 ifnet_lock_done(ifp
);
1660 static __attribute__((noinline
)) int
1661 ifioctl_linkparams(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1663 struct if_linkparamsreq
*iflpr
=
1664 (struct if_linkparamsreq
*)(void *)data
;
1665 struct ifclassq
*ifq
;
1668 VERIFY(ifp
!= NULL
);
1672 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1673 struct tb_profile tb
= { 0, 0, 0 };
1675 if ((error
= proc_suser(p
)) != 0)
1679 if (!IFCQ_IS_READY(ifq
)) {
1684 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1686 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1687 sizeof (tb
.percent
));
1688 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1693 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1694 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1695 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1699 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
))) {
1700 sched_type
= IFCQ_ALTQ(ifq
)->altq_type
;
1701 flags
|= IFLPRF_ALTQ
;
1703 #endif /* PF_ALTQ */
1705 if (IFCQ_IS_ENABLED(ifq
))
1706 sched_type
= ifq
->ifcq_type
;
1708 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1709 sizeof (iflpr
->iflpr_output_sched
));
1711 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1712 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1713 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1715 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1716 sizeof (iflpr
->iflpr_output_tbr_rate
));
1717 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1718 sizeof (iflpr
->iflpr_output_tbr_percent
));
1721 if (ifp
->if_output_sched_model
==
1722 IFNET_SCHED_MODEL_DRIVER_MANAGED
)
1723 flags
|= IFLPRF_DRVMANAGED
;
1724 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof (iflpr
->iflpr_flags
));
1725 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1726 sizeof (iflpr
->iflpr_output_bw
));
1727 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1728 sizeof (iflpr
->iflpr_input_bw
));
1729 bcopy(&ifp
->if_output_lt
, &iflpr
->iflpr_output_lt
,
1730 sizeof (iflpr
->iflpr_output_lt
));
1731 bcopy(&ifp
->if_input_lt
, &iflpr
->iflpr_input_lt
,
1732 sizeof (iflpr
->iflpr_input_lt
));
1744 static __attribute__((noinline
)) int
1745 ifioctl_qstats(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1747 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1748 u_int32_t ifqr_len
, ifqr_slot
;
1751 VERIFY(ifp
!= NULL
);
1754 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1755 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof (ifqr_slot
));
1756 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof (ifqr_len
));
1757 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1758 ifqr
->ifqr_buf
, &ifqr_len
);
1761 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof (ifqr_len
));
1773 static __attribute__((noinline
)) int
1774 ifioctl_throttle(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1776 struct if_throttlereq
*ifthr
= (struct if_throttlereq
*)(void *)data
;
1777 u_int32_t ifthr_level
;
1780 VERIFY(ifp
!= NULL
);
1783 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1785 * XXX: Use priv_check_cred() instead of root check?
1787 if ((error
= proc_suser(p
)) != 0)
1790 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof (ifthr_level
));
1791 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1792 if (error
== EALREADY
)
1797 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1798 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1799 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1800 sizeof (ifthr_level
));
1814 ifioctl_getnetagents(struct ifnet
*ifp
, u_int32_t
*count
, user_addr_t uuid_p
)
1817 u_int32_t index
= 0;
1818 u_int32_t valid_netagent_count
= 0;
1821 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_SHARED
);
1823 if (ifp
->if_agentids
!= NULL
) {
1824 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1825 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1826 if (!uuid_is_null(*netagent_uuid
)) {
1827 if (uuid_p
!= USER_ADDR_NULL
) {
1828 error
= copyout(netagent_uuid
,
1829 uuid_p
+ sizeof(uuid_t
) * valid_netagent_count
,
1835 valid_netagent_count
++;
1839 *count
= valid_netagent_count
;
1844 #define IF_MAXAGENTS 64
1845 #define IF_AGENT_INCREMENT 8
1846 static __attribute__((noinline
)) int
1847 ifioctl_netagent(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1849 struct if_agentidreq
*ifar
= (struct if_agentidreq
*)(void *)data
;
1851 struct if_agentidsreq32 s32
;
1852 struct if_agentidsreq64 s64
;
1855 u_int32_t index
= 0;
1857 VERIFY(ifp
!= NULL
);
1859 /* Get an io ref count if the interface is attached */
1860 if (!ifnet_is_attached(ifp
, 1)) {
1861 return (EOPNOTSUPP
);
1864 if (cmd
== SIOCAIFAGENTID
||
1865 cmd
== SIOCDIFAGENTID
) {
1866 ifnet_lock_exclusive(ifp
);
1868 ifnet_lock_shared(ifp
);
1872 case SIOCAIFAGENTID
: { /* struct if_agentidreq */
1873 uuid_t
*first_empty_slot
= NULL
;
1874 // TODO: Use priv_check_cred() instead of root check
1875 if ((error
= proc_suser(p
)) != 0) {
1878 bool already_added
= FALSE
;
1879 if (ifp
->if_agentids
!= NULL
) {
1880 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1881 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1882 if (uuid_compare(*netagent_uuid
, ifar
->ifar_uuid
) == 0) {
1883 /* Already present, ignore */
1884 already_added
= TRUE
;
1887 if (first_empty_slot
== NULL
&&
1888 uuid_is_null(*netagent_uuid
)) {
1889 first_empty_slot
= netagent_uuid
;
1893 if (already_added
) {
1894 /* Already added agent, don't return an error */
1897 if (first_empty_slot
== NULL
) {
1898 if (ifp
->if_agentcount
>= IF_MAXAGENTS
) {
1899 /* No room for another netagent UUID, bail */
1903 /* Calculate new array size */
1904 u_int32_t new_agent_count
=
1905 MIN(ifp
->if_agentcount
+ IF_AGENT_INCREMENT
, IF_MAXAGENTS
);
1907 /* Reallocate array */
1908 uuid_t
*new_agent_array
= _REALLOC(ifp
->if_agentids
,
1909 sizeof(uuid_t
) * new_agent_count
, M_NETAGENT
,
1911 if (new_agent_array
== NULL
) {
1916 /* Save new array */
1917 ifp
->if_agentids
= new_agent_array
;
1919 /* Set first empty slot */
1920 first_empty_slot
= &(ifp
->if_agentids
[ifp
->if_agentcount
]);
1922 /* Save new array length */
1923 ifp
->if_agentcount
= new_agent_count
;
1926 uuid_copy(*first_empty_slot
, ifar
->ifar_uuid
);
1927 netagent_post_updated_interfaces(ifar
->ifar_uuid
);
1930 case SIOCDIFAGENTID
: { /* struct if_agentidreq */
1931 bool removed_agent_id
= FALSE
;
1932 // TODO: Use priv_check_cred() instead of root check
1933 if ((error
= proc_suser(p
)) != 0) {
1936 if (ifp
->if_agentids
!= NULL
) {
1937 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1938 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1939 if (uuid_compare(*netagent_uuid
, ifar
->ifar_uuid
) == 0) {
1940 uuid_clear(*netagent_uuid
);
1941 removed_agent_id
= TRUE
;
1946 if (removed_agent_id
) {
1947 netagent_post_updated_interfaces(ifar
->ifar_uuid
);
1951 case SIOCGIFAGENTIDS32
: { /* struct if_agentidsreq32 */
1952 bcopy(data
, &u
.s32
, sizeof(u
.s32
));
1953 error
= ifioctl_getnetagents(ifp
, &u
.s32
.ifar_count
,
1956 bcopy(&u
.s32
, data
, sizeof(u
.s32
));
1960 case SIOCGIFAGENTIDS64
: { /* struct if_agentidsreq64 */
1961 bcopy(data
, &u
.s64
, sizeof(u
.s64
));
1962 error
= ifioctl_getnetagents(ifp
, &u
.s64
.ifar_count
,
1965 bcopy(&u
.s64
, data
, sizeof(u
.s64
));
1974 ifnet_lock_done(ifp
);
1975 ifnet_decr_iorefcnt(ifp
);
1981 ifnet_clear_netagent(uuid_t netagent_uuid
)
1983 struct ifnet
*ifp
= NULL
;
1984 u_int32_t index
= 0;
1986 ifnet_head_lock_shared();
1988 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1989 ifnet_lock_shared(ifp
);
1990 if (ifp
->if_agentids
!= NULL
) {
1991 for (index
= 0; index
< ifp
->if_agentcount
; index
++) {
1992 uuid_t
*ifp_netagent_uuid
= &(ifp
->if_agentids
[index
]);
1993 if (uuid_compare(*ifp_netagent_uuid
, netagent_uuid
) == 0) {
1994 uuid_clear(*ifp_netagent_uuid
);
1998 ifnet_lock_done(ifp
);
2005 ifnet_increment_generation(ifnet_t interface
)
2007 OSIncrementAtomic(&interface
->if_generation
);
2011 ifnet_get_generation(ifnet_t interface
)
2013 return (interface
->if_generation
);
2017 ifnet_remove_from_ordered_list(struct ifnet
*ifp
)
2019 ifnet_head_assert_exclusive();
2022 TAILQ_REMOVE(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2023 ifp
->if_ordered_link
.tqe_next
= NULL
;
2024 ifp
->if_ordered_link
.tqe_prev
= NULL
;
2026 // Update ordered count
2027 VERIFY(if_ordered_count
> 0);
2032 ifnet_reset_order(u_int32_t
*ordered_indices
, u_int32_t count
)
2034 struct ifnet
*ifp
= NULL
;
2037 ifnet_head_lock_exclusive();
2039 // Flush current ordered list
2040 for (ifp
= TAILQ_FIRST(&ifnet_ordered_head
); ifp
!= NULL
;
2041 ifp
= TAILQ_FIRST(&ifnet_ordered_head
)) {
2042 ifnet_lock_exclusive(ifp
);
2043 ifnet_remove_from_ordered_list(ifp
);
2044 ifnet_lock_done(ifp
);
2047 VERIFY(if_ordered_count
== 0);
2049 for (u_int32_t order_index
= 0; order_index
< count
; order_index
++) {
2050 u_int32_t interface_index
= ordered_indices
[order_index
];
2051 if (interface_index
== IFSCOPE_NONE
||
2052 interface_index
> (uint32_t)if_index
) {
2055 ifp
= ifindex2ifnet
[interface_index
];
2059 ifnet_lock_exclusive(ifp
);
2060 TAILQ_INSERT_TAIL(&ifnet_ordered_head
, ifp
, if_ordered_link
);
2061 ifnet_lock_done(ifp
);
2067 necp_update_all_clients();
2073 if_set_qosmarking_mode(struct ifnet
*ifp
, u_int32_t mode
)
2076 u_int32_t old_mode
= ifp
->if_qosmarking_mode
;
2079 case IFRTYPE_QOSMARKING_MODE_NONE
:
2080 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
2081 ifp
->if_eflags
&= ~IFEF_QOSMARKING_CAPABLE
;
2083 case IFRTYPE_QOSMARKING_FASTLANE
:
2084 ifp
->if_qosmarking_mode
= IFRTYPE_QOSMARKING_FASTLANE
;
2085 ifp
->if_eflags
|= IFEF_QOSMARKING_CAPABLE
;
2086 if (net_qos_policy_capable_enabled
!= 0)
2087 ifp
->if_eflags
|= IFEF_QOSMARKING_ENABLED
;
2093 if (error
== 0 && old_mode
!= ifp
->if_qosmarking_mode
) {
2094 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_QOS_MODE_CHANGED
,
2095 NULL
, sizeof(struct kev_dl_rrc_state
));
2101 static __attribute__((noinline
)) int
2102 ifioctl_iforder(u_long cmd
, caddr_t data
)
2105 u_int32_t
*ordered_indices
= NULL
;
2112 case SIOCSIFORDER
: { /* struct if_order */
2113 struct if_order
*ifo
= (struct if_order
*)(void *)data
;
2115 if (ifo
->ifo_count
> (u_int32_t
)if_index
) {
2120 size_t length
= (ifo
->ifo_count
* sizeof(u_int32_t
));
2122 if (ifo
->ifo_ordered_indices
== USER_ADDR_NULL
) {
2126 ordered_indices
= _MALLOC(length
, M_NECP
, M_WAITOK
);
2127 if (ordered_indices
== NULL
) {
2132 error
= copyin(ifo
->ifo_ordered_indices
,
2133 ordered_indices
, length
);
2139 error
= ifnet_reset_order(ordered_indices
, ifo
->ifo_count
);
2143 case SIOCGIFORDER
: { /* struct if_order */
2144 struct if_order
*ifo
= (struct if_order
*)(void *)data
;
2146 u_int32_t ordered_count
= if_ordered_count
;
2148 if (ifo
->ifo_count
== 0 ||
2149 ordered_count
== 0) {
2150 ifo
->ifo_count
= ordered_count
;
2151 } else if (ifo
->ifo_ordered_indices
!= USER_ADDR_NULL
) {
2152 u_int32_t count_to_copy
=
2153 MIN(ordered_count
, ifo
->ifo_count
);
2154 size_t length
= (count_to_copy
* sizeof(u_int32_t
));
2155 struct ifnet
*ifp
= NULL
;
2156 u_int32_t cursor
= 0;
2158 ordered_indices
= _MALLOC(length
, M_NECP
, M_WAITOK
);
2159 if (ordered_indices
== NULL
) {
2164 ifnet_head_lock_shared();
2165 TAILQ_FOREACH(ifp
, &ifnet_ordered_head
, if_ordered_link
) {
2166 if (cursor
>= count_to_copy
) {
2169 ordered_indices
[cursor
] = ifp
->if_index
;
2174 ifo
->ifo_count
= count_to_copy
;
2175 error
= copyout(ordered_indices
,
2176 ifo
->ifo_ordered_indices
, length
);
2189 if (ordered_indices
!= NULL
) {
2190 _FREE(ordered_indices
, M_NECP
);
2196 static __attribute__((noinline
)) int
2197 ifioctl_netsignature(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
2199 struct if_nsreq
*ifnsr
= (struct if_nsreq
*)(void *)data
;
2203 VERIFY(ifp
!= NULL
);
2206 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2207 if (ifnsr
->ifnsr_len
> sizeof (ifnsr
->ifnsr_data
)) {
2211 bcopy(&ifnsr
->ifnsr_flags
, &flags
, sizeof (flags
));
2212 error
= ifnet_set_netsignature(ifp
, ifnsr
->ifnsr_family
,
2213 ifnsr
->ifnsr_len
, flags
, ifnsr
->ifnsr_data
);
2216 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2217 ifnsr
->ifnsr_len
= sizeof (ifnsr
->ifnsr_data
);
2218 error
= ifnet_get_netsignature(ifp
, ifnsr
->ifnsr_family
,
2219 &ifnsr
->ifnsr_len
, &flags
, ifnsr
->ifnsr_data
);
2221 bcopy(&flags
, &ifnsr
->ifnsr_flags
, sizeof (flags
));
2223 ifnsr
->ifnsr_len
= 0;
2237 * Most of the routines called to handle the ioctls would end up being
2238 * tail-call optimized, which unfortunately causes this routine to
2239 * consume too much stack space; this is the reason for the "noinline"
2240 * attribute used on those routines.
2243 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2245 char ifname
[IFNAMSIZ
+ 1];
2246 struct ifnet
*ifp
= NULL
;
2247 struct ifstat
*ifs
= NULL
;
2250 bzero(ifname
, sizeof (ifname
));
2253 * ioctls which don't require ifp, or ifreq ioctls
2256 case OSIOCGIFCONF32
: /* struct ifconf32 */
2257 case SIOCGIFCONF32
: /* struct ifconf32 */
2258 case SIOCGIFCONF64
: /* struct ifconf64 */
2259 case OSIOCGIFCONF64
: /* struct ifconf64 */
2260 error
= ifioctl_ifconf(cmd
, data
);
2263 case SIOCIFGCLONERS32
: /* struct if_clonereq32 */
2264 case SIOCIFGCLONERS64
: /* struct if_clonereq64 */
2265 error
= ifioctl_ifclone(cmd
, data
);
2268 case SIOCGIFAGENTDATA32
: /* struct netagent_req32 */
2269 case SIOCGIFAGENTDATA64
: /* struct netagent_req64 */
2270 case SIOCGIFAGENTLIST32
: /* struct netagentlist_req32 */
2271 case SIOCGIFAGENTLIST64
: /* struct netagentlist_req64 */
2272 error
= netagent_ioctl(cmd
, data
);
2275 case SIOCSIFORDER
: /* struct if_order */
2276 case SIOCGIFORDER
: /* struct if_order */
2277 error
= ifioctl_iforder(cmd
, data
);
2280 case SIOCSIFDSTADDR
: /* struct ifreq */
2281 case SIOCSIFADDR
: /* struct ifreq */
2282 case SIOCSIFBRDADDR
: /* struct ifreq */
2283 case SIOCSIFNETMASK
: /* struct ifreq */
2284 case OSIOCGIFADDR
: /* struct ifreq */
2285 case OSIOCGIFDSTADDR
: /* struct ifreq */
2286 case OSIOCGIFBRDADDR
: /* struct ifreq */
2287 case OSIOCGIFNETMASK
: /* struct ifreq */
2288 case SIOCSIFKPI
: /* struct ifreq */
2289 if (so
->so_proto
== NULL
) {
2294 case SIOCIFCREATE
: /* struct ifreq */
2295 case SIOCIFCREATE2
: /* struct ifreq */
2296 case SIOCIFDESTROY
: /* struct ifreq */
2297 case SIOCGIFFLAGS
: /* struct ifreq */
2298 case SIOCGIFEFLAGS
: /* struct ifreq */
2299 case SIOCGIFCAP
: /* struct ifreq */
2301 case SIOCGIFMAC
: /* struct ifreq */
2302 case SIOCSIFMAC
: /* struct ifreq */
2303 #endif /* CONFIG_MACF_NET */
2304 case SIOCGIFMETRIC
: /* struct ifreq */
2305 case SIOCGIFMTU
: /* struct ifreq */
2306 case SIOCGIFPHYS
: /* struct ifreq */
2307 case SIOCSIFFLAGS
: /* struct ifreq */
2308 case SIOCSIFCAP
: /* struct ifreq */
2309 case SIOCSIFMETRIC
: /* struct ifreq */
2310 case SIOCSIFPHYS
: /* struct ifreq */
2311 case SIOCSIFMTU
: /* struct ifreq */
2312 case SIOCADDMULTI
: /* struct ifreq */
2313 case SIOCDELMULTI
: /* struct ifreq */
2314 case SIOCDIFPHYADDR
: /* struct ifreq */
2315 case SIOCSIFMEDIA
: /* struct ifreq */
2316 case SIOCSIFGENERIC
: /* struct ifreq */
2317 case SIOCSIFLLADDR
: /* struct ifreq */
2318 case SIOCSIFALTMTU
: /* struct ifreq */
2319 case SIOCSIFVLAN
: /* struct ifreq */
2320 case SIOCSIFBOND
: /* struct ifreq */
2321 case SIOCGIFLLADDR
: /* struct ifreq */
2322 case SIOCGIFTYPE
: /* struct ifreq */
2323 case SIOCGIFFUNCTIONALTYPE
: /* struct ifreq */
2324 case SIOCGIFPSRCADDR
: /* struct ifreq */
2325 case SIOCGIFPDSTADDR
: /* struct ifreq */
2326 case SIOCGIFGENERIC
: /* struct ifreq */
2327 case SIOCGIFDEVMTU
: /* struct ifreq */
2328 case SIOCGIFVLAN
: /* struct ifreq */
2329 case SIOCGIFBOND
: /* struct ifreq */
2330 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
2331 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
2332 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
2333 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
2334 case SIOCGIFLINKQUALITYMETRIC
: /* struct ifreq */
2335 case SIOCSIFLOG
: /* struct ifreq */
2336 case SIOCGIFLOG
: /* struct ifreq */
2337 case SIOCGIFDELEGATE
: /* struct ifreq */
2338 case SIOCGIFEXPENSIVE
: /* struct ifreq */
2339 case SIOCSIFEXPENSIVE
: /* struct ifreq */
2340 case SIOCSIF2KCL
: /* struct ifreq */
2341 case SIOCGIF2KCL
: /* struct ifreq */
2342 case SIOCSIFINTERFACESTATE
: /* struct ifreq */
2343 case SIOCGIFINTERFACESTATE
: /* struct ifreq */
2344 case SIOCSIFPROBECONNECTIVITY
: /* struct ifreq */
2345 case SIOCGIFPROBECONNECTIVITY
: /* struct ifreq */
2346 case SIOCGSTARTDELAY
: /* struct ifreq */
2347 case SIOCSIFTIMESTAMPENABLE
: /* struct ifreq */
2348 case SIOCSIFTIMESTAMPDISABLE
: /* struct ifreq */
2349 case SIOCGIFTIMESTAMPENABLED
: /* struct ifreq */
2350 #if (DEBUG || DEVELOPMENT)
2351 case SIOCSIFDISABLEOUTPUT
: /* struct ifreq */
2352 #endif /* (DEBUG || DEVELOPMENT) */
2353 case SIOCGECNMODE
: /* struct ifreq */
2354 case SIOCSECNMODE
: { /* struct ifreq */
2356 bcopy(data
, &ifr
, sizeof (ifr
));
2357 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2358 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2359 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2360 bcopy(&ifr
, data
, sizeof (ifr
));
2363 case SIOCSQOSMARKINGMODE
: /* struct ifreq */
2364 case SIOCSQOSMARKINGENABLED
: /* struct ifreq */
2365 case SIOCGQOSMARKINGMODE
: /* struct ifreq */
2366 case SIOCGQOSMARKINGENABLED
: /* struct ifreq */
2367 { /* struct ifreq */
2369 bcopy(data
, &ifr
, sizeof (ifr
));
2370 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2371 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2372 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2373 bcopy(&ifr
, data
, sizeof (ifr
));
2379 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2380 * here to ensure that the ifnet, if found, has been fully attached.
2384 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2385 bcopy(((struct in_aliasreq
*)(void *)data
)->ifra_name
,
2387 ifp
= ifunit(ifname
);
2391 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2392 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
2394 ifp
= ifunit(ifname
);
2397 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2398 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
2400 ifp
= ifunit(ifname
);
2404 case SIOCGIFSTATUS
: /* struct ifstat */
2405 ifs
= _MALLOC(sizeof (*ifs
), M_DEVBUF
, M_WAITOK
);
2411 bcopy(data
, ifs
, sizeof (*ifs
));
2412 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
2413 bcopy(ifs
->ifs_name
, ifname
, IFNAMSIZ
);
2414 ifp
= ifunit(ifname
);
2417 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2418 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
2420 ifp
= ifunit(ifname
);
2423 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
2424 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
2426 ifp
= ifunit(ifname
);
2429 case SIOCSIFDESC
: /* struct if_descreq */
2430 case SIOCGIFDESC
: /* struct if_descreq */
2431 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
2433 ifp
= ifunit(ifname
);
2436 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
2437 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
2438 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
2440 ifp
= ifunit(ifname
);
2443 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
2444 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
2446 ifp
= ifunit(ifname
);
2449 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
2450 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
2451 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
2453 ifp
= ifunit(ifname
);
2456 case SIOCAIFAGENTID
: /* struct if_agentidreq */
2457 case SIOCDIFAGENTID
: /* struct if_agentidreq */
2458 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
2459 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
2460 bcopy(((struct if_agentidreq
*)(void *)data
)->ifar_name
,
2462 ifp
= ifunit(ifname
);
2465 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2466 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2467 bcopy(((struct if_nsreq
*)(void *)data
)->ifnsr_name
,
2469 ifp
= ifunit(ifname
);
2474 * This is a bad assumption, but the code seems to
2475 * have been doing this in the past; caveat emptor.
2477 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
2479 ifp
= ifunit(ifname
);
2490 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2492 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2493 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2495 error
= proc_suser(p
);
2499 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2503 ifnet_touch_lastchange(ifp
);
2506 case SIOCGIFSTATUS
: /* struct ifstat */
2507 VERIFY(ifs
!= NULL
);
2508 ifs
->ascii
[0] = '\0';
2510 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifs
);
2512 bcopy(ifs
, data
, sizeof (*ifs
));
2515 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2516 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
2517 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2520 case SIOCSIFDESC
: /* struct if_descreq */
2521 case SIOCGIFDESC
: /* struct if_descreq */
2522 error
= ifioctl_ifdesc(ifp
, cmd
, data
, p
);
2525 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
2526 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
2527 error
= ifioctl_linkparams(ifp
, cmd
, data
, p
);
2530 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
2531 error
= ifioctl_qstats(ifp
, cmd
, data
);
2534 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
2535 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
2536 error
= ifioctl_throttle(ifp
, cmd
, data
, p
);
2539 case SIOCAIFAGENTID
: /* struct if_agentidreq */
2540 case SIOCDIFAGENTID
: /* struct if_agentidreq */
2541 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
2542 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
2543 error
= ifioctl_netagent(ifp
, cmd
, data
, p
);
2546 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2547 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2548 error
= ifioctl_netsignature(ifp
, cmd
, data
);
2552 if (so
->so_proto
== NULL
) {
2558 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
2560 socket_unlock(so
, 1);
2562 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
2563 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2570 _FREE(ifs
, M_DEVBUF
);
2573 if (ifname
[0] == '\0')
2574 (void) snprintf(ifname
, sizeof (ifname
), "%s",
2576 else if (ifp
!= NULL
)
2577 (void) snprintf(ifname
, sizeof (ifname
), "%s",
2581 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2582 "%c %lu) error %d\n", __func__
,
2583 proc_name_address(p
), proc_pid(p
),
2584 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
2585 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
2586 (char)IOCGROUP(cmd
), cmd
& 0xff, error
);
2587 } else if (if_verbose
> 1) {
2588 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2589 "%c %lu) OK\n", __func__
,
2590 proc_name_address(p
), proc_pid(p
),
2591 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
2592 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
2593 (char)IOCGROUP(cmd
), cmd
& 0xff);
2600 static __attribute__((noinline
)) int
2601 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
2606 struct kev_msg ev_msg
;
2607 struct net_event_data ev_data
;
2609 bzero(&ev_data
, sizeof (struct net_event_data
));
2610 bzero(&ev_msg
, sizeof (struct kev_msg
));
2615 error
= proc_suser(p
);
2618 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
2619 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
2621 error
= proc_suser(p
);
2624 return (if_clone_destroy(ifr
->ifr_name
));
2628 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2629 * here to ensure that the ifnet, if found, has been fully attached.
2632 ifp
= ifunit(ifr
->ifr_name
);
2640 ifnet_lock_shared(ifp
);
2641 ifr
->ifr_flags
= ifp
->if_flags
;
2642 ifnet_lock_done(ifp
);
2646 ifnet_lock_shared(ifp
);
2647 ifr
->ifr_eflags
= ifp
->if_eflags
;
2648 ifnet_lock_done(ifp
);
2652 ifnet_lock_shared(ifp
);
2653 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
2654 ifr
->ifr_curcap
= ifp
->if_capenable
;
2655 ifnet_lock_done(ifp
);
2660 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
2664 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
2666 #endif /* CONFIG_MACF_NET */
2669 ifnet_lock_shared(ifp
);
2670 ifr
->ifr_metric
= ifp
->if_metric
;
2671 ifnet_lock_done(ifp
);
2675 ifnet_lock_shared(ifp
);
2676 ifr
->ifr_mtu
= ifp
->if_mtu
;
2677 ifnet_lock_done(ifp
);
2681 ifnet_lock_shared(ifp
);
2682 ifr
->ifr_phys
= ifp
->if_physical
;
2683 ifnet_lock_done(ifp
);
2687 error
= proc_suser(p
);
2691 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
2692 (u_int16_t
)~IFF_CANTCHANGE
);
2695 * Note that we intentionally ignore any error from below
2696 * for the SIOCSIFFLAGS case.
2698 (void) ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2701 * Send the event even upon error from the driver because
2702 * we changed the flags.
2704 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2705 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2706 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2708 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
2709 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2710 ev_data
.if_family
= ifp
->if_family
;
2711 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2712 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2713 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2714 ev_msg
.dv
[1].data_length
= 0;
2715 dlil_post_complete_msg(ifp
, &ev_msg
);
2717 ifnet_touch_lastchange(ifp
);
2721 error
= proc_suser(p
);
2725 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
2729 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2731 ifnet_touch_lastchange(ifp
);
2735 error
= proc_suser(p
);
2739 ifp
->if_metric
= ifr
->ifr_metric
;
2741 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2742 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2743 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2745 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
2746 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2747 ev_data
.if_family
= ifp
->if_family
;
2748 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2749 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2750 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2752 ev_msg
.dv
[1].data_length
= 0;
2753 dlil_post_complete_msg(ifp
, &ev_msg
);
2755 ifnet_touch_lastchange(ifp
);
2759 error
= proc_suser(p
);
2763 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2767 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2768 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2769 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2771 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
2772 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2773 ev_data
.if_family
= ifp
->if_family
;
2774 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2775 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2776 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2777 ev_msg
.dv
[1].data_length
= 0;
2778 dlil_post_complete_msg(ifp
, &ev_msg
);
2780 ifnet_touch_lastchange(ifp
);
2784 u_int32_t oldmtu
= ifp
->if_mtu
;
2785 struct ifclassq
*ifq
= &ifp
->if_snd
;
2787 error
= proc_suser(p
);
2791 if (ifp
->if_ioctl
== NULL
) {
2795 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2799 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2803 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2804 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2805 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2807 ev_msg
.event_code
= KEV_DL_SIFMTU
;
2808 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2809 ev_data
.if_family
= ifp
->if_family
;
2810 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2811 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2812 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2813 ev_msg
.dv
[1].data_length
= 0;
2814 dlil_post_complete_msg(ifp
, &ev_msg
);
2816 ifnet_touch_lastchange(ifp
);
2820 * If the link MTU changed, do network layer specific procedure
2821 * and update all route entries associated with the interface,
2822 * so that their MTU metric gets updated.
2824 if (ifp
->if_mtu
!= oldmtu
) {
2825 if_rtmtu_update(ifp
);
2829 /* Inform all transmit queues about the new MTU */
2831 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
2839 error
= proc_suser(p
);
2843 /* Don't allow group membership on non-multicast interfaces. */
2844 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2849 /* Don't let users screw up protocols' entries. */
2850 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
2851 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2857 * User is permitted to anonymously join a particular link
2858 * multicast group via SIOCADDMULTI. Subsequent join requested
2859 * for the same record which has an outstanding refcnt from a
2860 * past if_addmulti_anon() will not result in EADDRINUSE error
2861 * (unlike other BSDs.) Anonymously leaving a group is also
2862 * allowed only as long as there is an outstanding refcnt held
2863 * by a previous anonymous request, or else ENOENT (even if the
2864 * link-layer multicast membership exists for a network-layer
2867 if (cmd
== SIOCADDMULTI
) {
2868 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
2869 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
2871 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
2872 ev_msg
.event_code
= KEV_DL_DELMULTI
;
2877 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2878 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2879 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2880 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2882 ev_data
.if_family
= ifp
->if_family
;
2883 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2884 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2885 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2886 ev_msg
.dv
[1].data_length
= 0;
2887 dlil_post_complete_msg(ifp
, &ev_msg
);
2889 ifnet_touch_lastchange(ifp
);
2892 case SIOCDIFPHYADDR
:
2894 case SIOCSIFGENERIC
:
2899 error
= proc_suser(p
);
2903 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2907 ifnet_touch_lastchange(ifp
);
2910 case SIOCGIFLLADDR
: {
2911 struct sockaddr_dl
*sdl
= SDL(ifp
->if_lladdr
->ifa_addr
);
2913 if (sdl
->sdl_alen
== 0) {
2914 error
= EADDRNOTAVAIL
;
2917 /* If larger than 14-bytes we'll need another mechanism */
2918 if (sdl
->sdl_alen
> sizeof (ifr
->ifr_addr
.sa_data
)) {
2922 /* Follow the same convention used by SIOCSIFLLADDR */
2923 bzero(&ifr
->ifr_addr
, sizeof (ifr
->ifr_addr
));
2924 ifr
->ifr_addr
.sa_family
= AF_LINK
;
2925 ifr
->ifr_addr
.sa_len
= sdl
->sdl_alen
;
2926 error
= ifnet_guarded_lladdr_copy_bytes(ifp
,
2927 &ifr
->ifr_addr
.sa_data
, sdl
->sdl_alen
);
2932 ifr
->ifr_type
.ift_type
= ifp
->if_type
;
2933 ifr
->ifr_type
.ift_family
= ifp
->if_family
;
2934 ifr
->ifr_type
.ift_subfamily
= ifp
->if_subfamily
;
2937 case SIOCGIFFUNCTIONALTYPE
:
2938 ifr
->ifr_functional_type
= if_functional_type(ifp
, FALSE
);
2941 case SIOCGIFPSRCADDR
:
2942 case SIOCGIFPDSTADDR
:
2943 case SIOCGIFGENERIC
:
2947 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2950 case SIOCGIFWAKEFLAGS
:
2951 ifnet_lock_shared(ifp
);
2952 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
2953 ifnet_lock_done(ifp
);
2956 case SIOCGIFGETRTREFCNT
:
2957 ifnet_lock_shared(ifp
);
2958 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
2959 ifnet_lock_done(ifp
);
2962 case SIOCSIFOPPORTUNISTIC
:
2963 case SIOCGIFOPPORTUNISTIC
:
2964 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
2967 case SIOCGIFLINKQUALITYMETRIC
:
2968 ifnet_lock_shared(ifp
);
2969 if ((ifp
->if_interface_state
.valid_bitmask
&
2970 IF_INTERFACE_STATE_LQM_STATE_VALID
))
2971 ifr
->ifr_link_quality_metric
=
2972 ifp
->if_interface_state
.lqm_state
;
2973 else if ((ifp
->if_refflags
& IFRF_ATTACHED
)) {
2974 ifr
->ifr_link_quality_metric
=
2975 IFNET_LQM_THRESH_UNKNOWN
;
2977 ifr
->ifr_link_quality_metric
=
2978 IFNET_LQM_THRESH_OFF
;
2980 ifnet_lock_done(ifp
);
2985 error
= ifnet_getset_log(ifp
, cmd
, ifr
, p
);
2988 case SIOCGIFDELEGATE
:
2989 ifnet_lock_shared(ifp
);
2990 ifr
->ifr_delegated
= ((ifp
->if_delegated
.ifp
!= NULL
) ?
2991 ifp
->if_delegated
.ifp
->if_index
: 0);
2992 ifnet_lock_done(ifp
);
2995 case SIOCGIFEXPENSIVE
:
2996 ifnet_lock_shared(ifp
);
2997 if (ifp
->if_eflags
& IFEF_EXPENSIVE
)
2998 ifr
->ifr_expensive
= 1;
3000 ifr
->ifr_expensive
= 0;
3001 ifnet_lock_done(ifp
);
3004 case SIOCSIFEXPENSIVE
:
3008 if ((error
= priv_check_cred(kauth_cred_get(),
3009 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3011 ifnet_lock_exclusive(ifp
);
3012 if (ifr
->ifr_expensive
)
3013 ifp
->if_eflags
|= IFEF_EXPENSIVE
;
3015 ifp
->if_eflags
&= ~IFEF_EXPENSIVE
;
3016 ifnet_lock_done(ifp
);
3018 * Update the expensive bit in the delegated interface
3021 ifnet_head_lock_shared();
3022 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
3023 ifnet_lock_exclusive(difp
);
3024 if (difp
->if_delegated
.ifp
== ifp
) {
3025 difp
->if_delegated
.expensive
=
3026 ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
3029 ifnet_lock_done(difp
);
3036 ifnet_lock_shared(ifp
);
3037 if (ifp
->if_eflags
& IFEF_2KCL
)
3041 ifnet_lock_done(ifp
);
3045 if ((error
= priv_check_cred(kauth_cred_get(),
3046 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3048 ifnet_lock_exclusive(ifp
);
3050 ifp
->if_eflags
|= IFEF_2KCL
;
3052 ifp
->if_eflags
&= ~IFEF_2KCL
;
3053 ifnet_lock_done(ifp
);
3055 case SIOCGSTARTDELAY
:
3056 ifnet_lock_shared(ifp
);
3057 if (ifp
->if_eflags
& IFEF_ENQUEUE_MULTI
) {
3058 ifr
->ifr_start_delay_qlen
=
3059 ifp
->if_start_delay_qlen
;
3060 ifr
->ifr_start_delay_timeout
=
3061 ifp
->if_start_delay_timeout
;
3063 ifr
->ifr_start_delay_qlen
= 0;
3064 ifr
->ifr_start_delay_timeout
= 0;
3066 ifnet_lock_done(ifp
);
3068 case SIOCSIFDSTADDR
:
3070 case SIOCSIFBRDADDR
:
3071 case SIOCSIFNETMASK
:
3073 case OSIOCGIFDSTADDR
:
3074 case OSIOCGIFBRDADDR
:
3075 case OSIOCGIFNETMASK
:
3077 VERIFY(so
->so_proto
!= NULL
);
3079 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
3080 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
3081 #if BYTE_ORDER != BIG_ENDIAN
3082 if (ifr
->ifr_addr
.sa_family
== 0 &&
3083 ifr
->ifr_addr
.sa_len
< 16) {
3084 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
3085 ifr
->ifr_addr
.sa_len
= 16;
3088 if (ifr
->ifr_addr
.sa_len
== 0)
3089 ifr
->ifr_addr
.sa_len
= 16;
3091 } else if (cmd
== OSIOCGIFADDR
) {
3092 cmd
= SIOCGIFADDR
; /* struct ifreq */
3093 } else if (cmd
== OSIOCGIFDSTADDR
) {
3094 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
3095 } else if (cmd
== OSIOCGIFBRDADDR
) {
3096 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
3097 } else if (cmd
== OSIOCGIFNETMASK
) {
3098 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
3102 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
3103 (caddr_t
)ifr
, ifp
, p
));
3104 socket_unlock(so
, 1);
3108 case OSIOCGIFDSTADDR
:
3109 case OSIOCGIFBRDADDR
:
3110 case OSIOCGIFNETMASK
:
3111 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
3115 if (cmd
== SIOCSIFKPI
) {
3116 int temperr
= proc_suser(p
);
3121 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
3122 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3127 case SIOCGIFINTERFACESTATE
:
3128 if_get_state(ifp
, &ifr
->ifr_interface_state
);
3131 case SIOCSIFINTERFACESTATE
:
3132 if ((error
= priv_check_cred(kauth_cred_get(),
3133 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3136 error
= if_state_update(ifp
, &ifr
->ifr_interface_state
);
3139 case SIOCSIFPROBECONNECTIVITY
:
3140 if ((error
= priv_check_cred(kauth_cred_get(),
3141 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3143 error
= if_probe_connectivity(ifp
,
3144 ifr
->ifr_probe_connectivity
);
3146 case SIOCGIFPROBECONNECTIVITY
:
3147 if ((error
= priv_check_cred(kauth_cred_get(),
3148 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3150 if (ifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)
3151 ifr
->ifr_probe_connectivity
= 1;
3153 ifr
->ifr_probe_connectivity
= 0;
3156 if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
|IFEF_ECN_DISABLE
)) ==
3158 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_ENABLE
;
3159 else if ((ifp
->if_eflags
& (IFEF_ECN_ENABLE
|IFEF_ECN_DISABLE
)) ==
3161 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DISABLE
;
3163 ifr
->ifr_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
3166 if ((error
= priv_check_cred(kauth_cred_get(),
3167 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3169 if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DEFAULT
) {
3170 ifp
->if_eflags
&= ~(IFEF_ECN_ENABLE
|IFEF_ECN_DISABLE
);
3171 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_ENABLE
) {
3172 ifp
->if_eflags
|= IFEF_ECN_ENABLE
;
3173 ifp
->if_eflags
&= ~IFEF_ECN_DISABLE
;
3174 } else if (ifr
->ifr_ecn_mode
== IFRTYPE_ECN_DISABLE
) {
3175 ifp
->if_eflags
|= IFEF_ECN_DISABLE
;
3176 ifp
->if_eflags
&= ~IFEF_ECN_ENABLE
;
3180 case SIOCSIFTIMESTAMPENABLE
:
3181 case SIOCSIFTIMESTAMPDISABLE
:
3182 error
= proc_suser(p
);
3186 ifnet_lock_exclusive(ifp
);
3187 if ((cmd
== SIOCSIFTIMESTAMPENABLE
&&
3188 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0) ||
3189 (cmd
== SIOCSIFTIMESTAMPDISABLE
&&
3190 (ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) == 0)) {
3191 ifnet_lock_done(ifp
);
3194 if (cmd
== SIOCSIFTIMESTAMPENABLE
)
3195 ifp
->if_xflags
|= IFXF_TIMESTAMP_ENABLED
;
3197 ifp
->if_xflags
&= ~IFXF_TIMESTAMP_ENABLED
;
3198 ifnet_lock_done(ifp
);
3200 * Pass the setting to the interface if it supports either
3201 * software or hardware time stamping
3203 if (ifp
->if_capabilities
& (IFCAP_HW_TIMESTAMP
|
3204 IFCAP_SW_TIMESTAMP
)) {
3205 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
3209 case SIOCGIFTIMESTAMPENABLED
: {
3210 if ((ifp
->if_xflags
& IFXF_TIMESTAMP_ENABLED
) != 0)
3211 ifr
->ifr_intval
= 1;
3213 ifr
->ifr_intval
= 0;
3216 case SIOCSQOSMARKINGMODE
:
3217 if ((error
= priv_check_cred(kauth_cred_get(),
3218 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3220 error
= if_set_qosmarking_mode(ifp
, ifr
->ifr_qosmarking_mode
);
3223 case SIOCGQOSMARKINGMODE
:
3224 ifr
->ifr_qosmarking_mode
= ifp
->if_qosmarking_mode
;
3227 case SIOCSQOSMARKINGENABLED
:
3228 if ((error
= priv_check_cred(kauth_cred_get(),
3229 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
3231 if (ifr
->ifr_qosmarking_enabled
!= 0)
3232 ifp
->if_eflags
|= IFEF_QOSMARKING_ENABLED
;
3234 ifp
->if_eflags
&= ~IFEF_QOSMARKING_ENABLED
;
3237 case SIOCGQOSMARKINGENABLED
:
3238 ifr
->ifr_qosmarking_enabled
=
3239 (ifp
->if_eflags
& IFEF_QOSMARKING_ENABLED
) ? 1 : 0;
3242 case SIOCSIFDISABLEOUTPUT
:
3243 #if (DEBUG || DEVELOPMENT)
3244 if (ifr
->ifr_disable_output
== 1) {
3245 error
= ifnet_disable_output(ifp
);
3246 } else if (ifr
->ifr_disable_output
== 0) {
3247 error
= ifnet_enable_output(ifp
);
3253 #endif /* (DEBUG || DEVELOPMENT) */
3264 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
3268 socket_unlock(so
, 0);
3269 error
= ifioctl(so
, cmd
, data
, p
);
3275 * Set/clear promiscuous mode on interface ifp based on the truth value
3276 * of pswitch. The calls are reference counted so that only the first
3277 * "on" request actually has an effect, as does the final "off" request.
3278 * Results are undefined if the "off" and "on" requests are not matched.
3281 ifnet_set_promiscuous(
3289 ifnet_lock_exclusive(ifp
);
3290 oldflags
= ifp
->if_flags
;
3291 ifp
->if_pcount
+= pswitch
? 1 : -1;
3293 if (ifp
->if_pcount
> 0)
3294 ifp
->if_flags
|= IFF_PROMISC
;
3296 ifp
->if_flags
&= ~IFF_PROMISC
;
3298 newflags
= ifp
->if_flags
;
3299 ifnet_lock_done(ifp
);
3301 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
3302 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
3306 ifnet_lock_exclusive(ifp
);
3308 ifp
->if_pcount
-= pswitch
? 1 : -1;
3309 if (ifp
->if_pcount
> 0)
3310 ifp
->if_flags
|= IFF_PROMISC
;
3312 ifp
->if_flags
&= ~IFF_PROMISC
;
3313 ifnet_lock_done(ifp
);
3317 if (newflags
!= oldflags
) {
3318 log(LOG_INFO
, "%s: promiscuous mode %s%s\n",
3320 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
3321 error
!= 0 ? " failed" : " succeeded");
3327 * Return interface configuration
3328 * of system. List may be used
3329 * in later ioctl's (above) to get
3330 * other information.
3334 ifconf(u_long cmd
, user_addr_t ifrp
, int *ret_space
)
3336 struct ifnet
*ifp
= NULL
;
3341 net_thread_marks_t marks
;
3343 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
3346 * Zero the ifr buffer to make sure we don't
3347 * disclose the contents of the stack.
3349 bzero(&ifr
, sizeof (struct ifreq
));
3352 ifnet_head_lock_shared();
3353 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof (ifr
) &&
3354 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
3356 size_t ifnlen
, addrs
;
3358 ifnlen
= snprintf(workbuf
, sizeof (workbuf
),
3359 "%s", if_name(ifp
));
3360 if (ifnlen
+ 1 > sizeof (ifr
.ifr_name
)) {
3361 error
= ENAMETOOLONG
;
3364 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
3367 ifnet_lock_shared(ifp
);
3370 ifa
= ifp
->if_addrhead
.tqh_first
;
3371 for (; space
> sizeof (ifr
) && ifa
;
3372 ifa
= ifa
->ifa_link
.tqe_next
) {
3373 struct sockaddr
*sa
;
3376 struct sockaddr_dl sdl
;
3377 uint8_t buf
[SOCK_MAXADDRLEN
+ 1];
3381 * Make sure to accomodate the largest possible
3382 * size of SA(if_lladdr)->sa_len.
3384 _CASSERT(sizeof (u
) == (SOCK_MAXADDRLEN
+ 1));
3390 if (ifa
== ifp
->if_lladdr
) {
3391 VERIFY(sa
->sa_family
== AF_LINK
);
3392 bcopy(sa
, &u
, sa
->sa_len
);
3394 ifnet_guarded_lladdr_copy_bytes(ifp
,
3395 LLADDR(&u
.sdl
), u
.sdl
.sdl_alen
);
3400 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
3401 struct osockaddr
*osa
=
3402 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
3404 osa
->sa_family
= sa
->sa_family
;
3405 error
= copyout((caddr_t
)&ifr
, ifrp
,
3407 ifrp
+= sizeof (struct ifreq
);
3408 } else if (sa
->sa_len
<= sizeof (*sa
)) {
3410 error
= copyout((caddr_t
)&ifr
, ifrp
,
3412 ifrp
+= sizeof (struct ifreq
);
3415 sizeof (ifr
) + sa
->sa_len
- sizeof (*sa
)) {
3419 space
-= sa
->sa_len
- sizeof (*sa
);
3420 error
= copyout((caddr_t
)&ifr
, ifrp
,
3421 sizeof (ifr
.ifr_name
));
3423 error
= copyout((caddr_t
)sa
, (ifrp
+
3424 offsetof(struct ifreq
, ifr_addr
)),
3427 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
3433 space
-= sizeof (ifr
);
3435 ifnet_lock_done(ifp
);
3440 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof (ifr
.ifr_addr
));
3441 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
3444 space
-= sizeof (ifr
);
3445 ifrp
+= sizeof (struct ifreq
);
3449 *ret_space
-= space
;
3450 net_thread_marks_pop(marks
);
3455 * Just like if_promisc(), but for all-multicast-reception mode.
3458 if_allmulti(struct ifnet
*ifp
, int onswitch
)
3463 ifnet_lock_exclusive(ifp
);
3466 if (ifp
->if_amcount
++ == 0) {
3467 ifp
->if_flags
|= IFF_ALLMULTI
;
3471 if (ifp
->if_amcount
> 1) {
3474 ifp
->if_amcount
= 0;
3475 ifp
->if_flags
&= ~IFF_ALLMULTI
;
3479 ifnet_lock_done(ifp
);
3482 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
3489 static struct ifmultiaddr
*
3492 struct ifmultiaddr
*ifma
;
3494 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
3495 zalloc_noblock(ifma_zone
);
3498 bzero(ifma
, ifma_size
);
3499 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3500 ifma
->ifma_debug
|= IFD_ALLOC
;
3501 if (ifma_debug
!= 0) {
3502 ifma
->ifma_debug
|= IFD_DEBUG
;
3503 ifma
->ifma_trace
= ifma_trace
;
3510 ifma_free(struct ifmultiaddr
*ifma
)
3514 if (ifma
->ifma_protospec
!= NULL
) {
3515 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
3517 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3518 ifma
->ifma_anoncnt
!= 0) {
3519 panic("%s: Freeing ifma=%p with outstanding anon req",
3522 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
3523 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
3524 __func__
, ifma
, ifma
->ifma_ifp
);
3526 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
3527 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
3529 } else if (ifma
->ifma_refcount
!= 0) {
3530 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
3532 } else if (ifma
->ifma_reqcnt
!= 0) {
3533 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
3535 } else if (ifma
->ifma_ifp
!= NULL
) {
3536 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
3537 ifma
->ifma_ifp
, ifma
);
3539 } else if (ifma
->ifma_ll
!= NULL
) {
3540 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
3541 ifma
->ifma_ll
, ifma
);
3544 ifma
->ifma_debug
&= ~IFD_ALLOC
;
3545 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3546 (IFD_DEBUG
| IFD_TRASHED
)) {
3547 lck_mtx_lock(&ifma_trash_lock
);
3548 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
3550 lck_mtx_unlock(&ifma_trash_lock
);
3551 ifma
->ifma_debug
&= ~IFD_TRASHED
;
3555 if (ifma
->ifma_addr
!= NULL
) {
3556 FREE(ifma
->ifma_addr
, M_IFADDR
);
3557 ifma
->ifma_addr
= NULL
;
3559 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
3560 zfree(ifma_zone
, ifma
);
3564 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
3566 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
3571 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
3572 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
3576 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
3577 tr
= ifma_dbg
->ifma_refhold
;
3579 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
3580 tr
= ifma_dbg
->ifma_refrele
;
3583 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
3584 ctrace_record(&tr
[idx
]);
3588 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
3593 IFMA_LOCK_ASSERT_HELD(ifma
);
3595 if (++ifma
->ifma_refcount
== 0) {
3596 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
3598 } else if (ifma
->ifma_trace
!= NULL
) {
3599 (*ifma
->ifma_trace
)(ifma
, TRUE
);
3606 ifma_remref(struct ifmultiaddr
*ifma
)
3608 struct ifmultiaddr
*ll
;
3612 if (ifma
->ifma_refcount
== 0) {
3613 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
3615 } else if (ifma
->ifma_trace
!= NULL
) {
3616 (*ifma
->ifma_trace
)(ifma
, FALSE
);
3619 --ifma
->ifma_refcount
;
3620 if (ifma
->ifma_refcount
> 0) {
3626 ifma
->ifma_ifp
= NULL
;
3627 ifma
->ifma_ll
= NULL
;
3629 ifma_free(ifma
); /* deallocate it */
3636 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
3638 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
3639 IFMA_LOCK_ASSERT_HELD(ifma
);
3641 if (ifma
->ifma_ifp
!= ifp
) {
3642 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
3643 ifma
->ifma_ifp
, ifp
);
3645 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
3646 panic("%s: Attempt to attach an already attached ifma=%p",
3649 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3650 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
3652 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
3653 panic("%s: Attempt to reattach a detached ifma=%p",
3658 ifma
->ifma_reqcnt
++;
3659 VERIFY(ifma
->ifma_reqcnt
== 1);
3660 IFMA_ADDREF_LOCKED(ifma
);
3661 ifma
->ifma_debug
|= IFD_ATTACHED
;
3663 ifma
->ifma_anoncnt
++;
3664 VERIFY(ifma
->ifma_anoncnt
== 1);
3665 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
3668 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
3672 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
3674 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
3675 IFMA_LOCK_ASSERT_HELD(ifma
);
3677 if (ifma
->ifma_reqcnt
== 0) {
3678 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
3680 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3681 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
3683 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
3684 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
3686 } else if (ifma
->ifma_ifp
!= ifp
) {
3687 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
3688 ifma
->ifma_ifp
, ifp
);
3693 --ifma
->ifma_anoncnt
;
3694 if (ifma
->ifma_anoncnt
> 0)
3696 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
3699 --ifma
->ifma_reqcnt
;
3700 if (ifma
->ifma_reqcnt
> 0)
3703 if (ifma
->ifma_protospec
!= NULL
) {
3704 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
3706 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3707 ifma
->ifma_anoncnt
!= 0) {
3708 panic("%s: Detaching ifma=%p with outstanding anon req",
3711 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
3712 panic("%s: Attempt to detach an unattached address ifma=%p",
3715 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
3716 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
3721 * NOTE: Caller calls IFMA_REMREF
3723 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
3724 LIST_REMOVE(ifma
, ifma_link
);
3725 if (LIST_EMPTY(&ifp
->if_multiaddrs
))
3726 ifp
->if_updatemcasts
= 0;
3728 if (ifma
->ifma_debug
& IFD_DEBUG
) {
3729 /* Become a regular mutex, just in case */
3730 IFMA_CONVERT_LOCK(ifma
);
3731 lck_mtx_lock(&ifma_trash_lock
);
3732 TAILQ_INSERT_TAIL(&ifma_trash_head
,
3733 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
3734 lck_mtx_unlock(&ifma_trash_lock
);
3735 ifma
->ifma_debug
|= IFD_TRASHED
;
3742 * Find an ifmultiaddr that matches a socket address on an interface.
3744 * Caller is responsible for holding the ifnet_lock while calling
3748 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3749 struct ifmultiaddr
**retifma
, int anon
)
3751 struct ifmultiaddr
*ifma
;
3753 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
3754 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
3755 IFMA_LOCK_SPIN(ifma
);
3756 if (!ifa_equal(sa
, ifma
->ifma_addr
)) {
3761 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3762 ifma
->ifma_anoncnt
!= 0);
3763 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3764 ifma
->ifma_anoncnt
== 0);
3765 ifma
->ifma_anoncnt
++;
3766 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3767 VERIFY(ifma
->ifma_anoncnt
== 1);
3768 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
3771 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
3772 ifma
->ifma_reqcnt
++;
3773 VERIFY(ifma
->ifma_reqcnt
> 1);
3775 if (retifma
!= NULL
) {
3777 IFMA_ADDREF_LOCKED(ifma
);
3786 * Radar 3642395, make sure all multicasts are in a standard format.
3788 static struct sockaddr
*
3789 copy_and_normalize(const struct sockaddr
*original
)
3792 const u_char
*aptr
= NULL
;
3793 struct sockaddr
*copy
= NULL
;
3794 struct sockaddr_dl
*sdl_new
= NULL
;
3797 if (original
->sa_family
!= AF_LINK
&&
3798 original
->sa_family
!= AF_UNSPEC
) {
3799 /* Just make a copy */
3800 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
3801 M_IFADDR
, M_WAITOK
);
3803 bcopy(original
, copy
, original
->sa_len
);
3807 switch (original
->sa_family
) {
3809 const struct sockaddr_dl
*sdl_original
=
3810 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
3812 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
3813 sdl_original
->sdl_slen
+
3814 offsetof(struct sockaddr_dl
, sdl_data
) >
3815 sdl_original
->sdl_len
)
3818 alen
= sdl_original
->sdl_alen
;
3819 aptr
= CONST_LLADDR(sdl_original
);
3824 if (original
->sa_len
< ETHER_ADDR_LEN
+
3825 offsetof(struct sockaddr
, sa_data
)) {
3829 alen
= ETHER_ADDR_LEN
;
3830 aptr
= (const u_char
*)original
->sa_data
;
3835 if (alen
== 0 || aptr
== NULL
)
3838 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
3839 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
3841 if (sdl_new
!= NULL
) {
3842 bzero(sdl_new
, len
);
3843 sdl_new
->sdl_len
= len
;
3844 sdl_new
->sdl_family
= AF_LINK
;
3845 sdl_new
->sdl_alen
= alen
;
3846 bcopy(aptr
, LLADDR(sdl_new
), alen
);
3849 return ((struct sockaddr
*)sdl_new
);
3853 * Network-layer protocol domains which hold references to the underlying
3854 * link-layer record must use this routine.
3857 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3858 struct ifmultiaddr
**retifma
)
3860 return (if_addmulti_common(ifp
, sa
, retifma
, 0));
3864 * Anything other than network-layer protocol domains which hold references
3865 * to the underlying link-layer record must use this routine: SIOCADDMULTI
3866 * ioctl, ifnet_add_multicast(), if_bond.
3869 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3870 struct ifmultiaddr
**retifma
)
3872 return (if_addmulti_common(ifp
, sa
, retifma
, 1));
3876 * Register an additional multicast address with a network interface.
3878 * - If the address is already present, bump the reference count on the
3879 * address and return.
3880 * - If the address is not link-layer, look up a link layer address.
3881 * - Allocate address structures for one or both addresses, and attach to the
3882 * multicast address list on the interface. If automatically adding a link
3883 * layer address, the protocol address will own a reference to the link
3884 * layer address, to be freed when it is freed.
3885 * - Notify the network device driver of an addition to the multicast address
3888 * 'sa' points to caller-owned memory with the desired multicast address.
3890 * 'retifma' will be used to return a pointer to the resulting multicast
3891 * address reference, if desired.
3893 * 'anon' indicates a link-layer address with no protocol address reference
3894 * made to it. Anything other than network-layer protocol domain requests
3895 * are considered as anonymous.
3898 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3899 struct ifmultiaddr
**retifma
, int anon
)
3901 struct sockaddr_storage storage
;
3902 struct sockaddr
*llsa
= NULL
;
3903 struct sockaddr
*dupsa
= NULL
;
3904 int error
= 0, ll_firstref
= 0, lladdr
;
3905 struct ifmultiaddr
*ifma
= NULL
;
3906 struct ifmultiaddr
*llifma
= NULL
;
3908 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
3909 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
3910 sa
->sa_family
== AF_LINK
);
3912 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
3913 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
3914 dupsa
= copy_and_normalize(sa
);
3915 if (dupsa
== NULL
) {
3922 ifnet_lock_exclusive(ifp
);
3923 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
3924 error
= EADDRNOTAVAIL
;
3925 ifnet_lock_done(ifp
);
3929 /* If the address is already present, return a new reference to it */
3930 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
3931 ifnet_lock_done(ifp
);
3936 * The address isn't already present; give the link layer a chance
3937 * to accept/reject it, and also find out which AF_LINK address this
3938 * maps to, if it isn't one already.
3940 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
3942 if (error
== 0 && storage
.ss_len
!= 0) {
3943 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
3949 llifma
= ifma_alloc(M_WAITOK
);
3950 if (llifma
== NULL
) {
3956 /* to be similar to FreeBSD */
3957 if (error
== EOPNOTSUPP
)
3959 else if (error
!= 0)
3962 /* Allocate while we aren't holding any locks */
3963 if (dupsa
== NULL
) {
3964 dupsa
= copy_and_normalize(sa
);
3965 if (dupsa
== NULL
) {
3970 ifma
= ifma_alloc(M_WAITOK
);
3976 ifnet_lock_exclusive(ifp
);
3978 * Check again for the matching multicast.
3980 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
3982 ifnet_lock_done(ifp
);
3986 if (llifma
!= NULL
) {
3987 VERIFY(!anon
); /* must not get here if "anonymous" */
3988 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
3989 FREE(llsa
, M_IFADDR
);
3993 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
3996 llifma
->ifma_addr
= llsa
;
3997 llifma
->ifma_ifp
= ifp
;
3999 if_attach_ifma(ifp
, llifma
, 0);
4000 /* add extra refcnt for ifma */
4001 IFMA_ADDREF_LOCKED(llifma
);
4002 IFMA_UNLOCK(llifma
);
4003 ifma
->ifma_ll
= llifma
;
4007 /* "anonymous" request should not result in network address */
4008 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4010 ifma
->ifma_addr
= dupsa
;
4011 ifma
->ifma_ifp
= ifp
;
4013 if_attach_ifma(ifp
, ifma
, anon
);
4014 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4015 if (retifma
!= NULL
) {
4017 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
4019 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4020 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4022 ifnet_lock_done(ifp
);
4024 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
4025 IFMA_REMREF(ifma
); /* for this routine */
4028 * We are certain we have added something, so call down to the
4029 * interface to let them know about it. Do this only for newly-
4030 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4032 if (lladdr
|| ll_firstref
)
4033 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
4035 if (ifp
->if_updatemcasts
> 0)
4036 ifp
->if_updatemcasts
= 0;
4044 FREE(dupsa
, M_IFADDR
);
4048 FREE(llsa
, M_IFADDR
);
4054 * Delete a multicast group membership by network-layer group address.
4055 * This routine is deprecated.
4058 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4060 return (if_delmulti_common(NULL
, ifp
, sa
, 0));
4064 * Delete a multicast group membership by group membership pointer.
4065 * Network-layer protocol domains must use this routine.
4068 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
4070 return (if_delmulti_common(ifma
, NULL
, NULL
, 0));
4074 * Anything other than network-layer protocol domains which hold references
4075 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4076 * ioctl, ifnet_remove_multicast(), if_bond.
4079 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
4081 return (if_delmulti_common(NULL
, ifp
, sa
, 1));
4085 * Delete a multicast group membership by network-layer group address.
4087 * Returns ENOENT if the entry could not be found.
4090 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
4091 const struct sockaddr
*sa
, int anon
)
4093 struct sockaddr
*dupsa
= NULL
;
4094 int lastref
, ll_lastref
= 0, lladdr
;
4095 struct ifmultiaddr
*ll
= NULL
;
4097 /* sanity check for callers */
4098 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
4101 ifp
= ifma
->ifma_ifp
;
4104 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
4105 dupsa
= copy_and_normalize(sa
);
4111 ifnet_lock_exclusive(ifp
);
4113 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
4114 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
4116 if (!ifa_equal(sa
, ifma
->ifma_addr
) ||
4117 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
4118 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
4119 ifma
->ifma_anoncnt
!= 0);
4123 /* found; keep it locked */
4128 FREE(dupsa
, M_IFADDR
);
4129 ifnet_lock_done(ifp
);
4135 IFMA_LOCK_ASSERT_HELD(ifma
);
4136 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
4137 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
4138 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
4139 ifma
->ifma_reqcnt
== 0));
4140 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
4142 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
4143 ifma
->ifma_addr
->sa_family
== AF_LINK
);
4145 if (lastref
&& ll
!= NULL
) {
4147 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
4150 ifnet_lock_done(ifp
);
4153 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
4155 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
4157 * Make sure the interface driver is notified in the
4158 * case of a link layer mcast group being left. Do
4159 * this only for a AF_LINK/AF_UNSPEC address that has
4160 * been removed from the if_multiaddrs set.
4162 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
4166 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
4168 IFMA_REMREF(ll
); /* for if_multiaddrs list */
4170 IFMA_REMREF(ifma
); /* for this routine */
4172 FREE(dupsa
, M_IFADDR
);
4178 * Shutdown all network activity. Used boot() when halting
4188 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
4189 for (i
= 0; i
< count
; i
++) {
4191 dlil_proto_unplumb_all(ifp
[i
]);
4193 ifnet_list_free(ifp
);
4200 * Delete Routes for a Network Interface
4202 * Called for each routing entry via the rnh->rnh_walktree() call above
4203 * to delete all route entries referencing a detaching network interface.
4206 * rn pointer to node in the routing table
4207 * arg argument passed to rnh->rnh_walktree() - detaching interface
4211 * errno failed - reason indicated
4215 if_rtdel(struct radix_node
*rn
, void *arg
)
4217 struct rtentry
*rt
= (struct rtentry
*)rn
;
4218 struct ifnet
*ifp
= arg
;
4224 * Checking against RTF_UP protects against walktree
4225 * recursion problems with cloned routes.
4228 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
4230 * Safe to drop rt_lock and use rt_key, rt_gateway,
4231 * since holding rnh_lock here prevents another thread
4232 * from calling rt_setgate() on this route.
4235 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
4236 rt_mask(rt
), rt
->rt_flags
, NULL
);
4238 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
4247 * Removes routing table reference to a given interface
4248 * for a given protocol family
4251 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
4253 struct radix_node_head
*rnh
;
4255 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
4256 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
4257 lck_mtx_lock(rnh_lock
);
4258 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
4259 lck_mtx_unlock(rnh_lock
);
4264 if_rtmtu(struct radix_node
*rn
, void *arg
)
4266 struct rtentry
*rt
= (struct rtentry
*)rn
;
4267 struct ifnet
*ifp
= arg
;
4270 if (rt
->rt_ifp
== ifp
) {
4272 * Update the MTU of this entry only if the MTU
4273 * has not been locked (RTV_MTU is not set) and
4274 * if it was non-zero to begin with.
4276 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
4277 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
4285 * Update the MTU metric of all route entries in all protocol tables
4286 * associated with a particular interface; this is called when the
4287 * MTU of that interface has changed.
4290 if_rtmtu_update(struct ifnet
*ifp
)
4292 struct radix_node_head
*rnh
;
4295 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
4296 if ((rnh
= rt_tables
[p
]) == NULL
)
4299 lck_mtx_lock(rnh_lock
);
4300 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
4301 lck_mtx_unlock(rnh_lock
);
4303 routegenid_update();
4306 __private_extern__
void
4307 if_data_internal_to_if_data(struct ifnet
*ifp
,
4308 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
4311 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
4312 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
4313 /* compiler will cast down to 32-bit */
4314 #define COPYFIELD32_ATOMIC(fld) do { \
4315 atomic_get_64(if_data->fld, \
4316 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4319 COPYFIELD(ifi_type
);
4320 COPYFIELD(ifi_typelen
);
4321 COPYFIELD(ifi_physical
);
4322 COPYFIELD(ifi_addrlen
);
4323 COPYFIELD(ifi_hdrlen
);
4324 COPYFIELD(ifi_recvquota
);
4325 COPYFIELD(ifi_xmitquota
);
4326 if_data
->ifi_unused1
= 0;
4328 COPYFIELD(ifi_metric
);
4329 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
4330 if_data
->ifi_baudrate
= 0xFFFFFFFF;
4332 COPYFIELD32(ifi_baudrate
);
4335 COPYFIELD32_ATOMIC(ifi_ipackets
);
4336 COPYFIELD32_ATOMIC(ifi_ierrors
);
4337 COPYFIELD32_ATOMIC(ifi_opackets
);
4338 COPYFIELD32_ATOMIC(ifi_oerrors
);
4339 COPYFIELD32_ATOMIC(ifi_collisions
);
4340 COPYFIELD32_ATOMIC(ifi_ibytes
);
4341 COPYFIELD32_ATOMIC(ifi_obytes
);
4342 COPYFIELD32_ATOMIC(ifi_imcasts
);
4343 COPYFIELD32_ATOMIC(ifi_omcasts
);
4344 COPYFIELD32_ATOMIC(ifi_iqdrops
);
4345 COPYFIELD32_ATOMIC(ifi_noproto
);
4347 COPYFIELD(ifi_recvtiming
);
4348 COPYFIELD(ifi_xmittiming
);
4350 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
4351 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
4353 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
4355 if_data
->ifi_unused2
= 0;
4356 COPYFIELD(ifi_hwassist
);
4357 if_data
->ifi_reserved1
= 0;
4358 if_data
->ifi_reserved2
= 0;
4359 #undef COPYFIELD32_ATOMIC
4364 __private_extern__
void
4365 if_data_internal_to_if_data64(struct ifnet
*ifp
,
4366 const struct if_data_internal
*if_data_int
,
4367 struct if_data64
*if_data64
)
4370 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
4371 #define COPYFIELD64_ATOMIC(fld) do { \
4372 atomic_get_64(if_data64->fld, \
4373 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4376 COPYFIELD64(ifi_type
);
4377 COPYFIELD64(ifi_typelen
);
4378 COPYFIELD64(ifi_physical
);
4379 COPYFIELD64(ifi_addrlen
);
4380 COPYFIELD64(ifi_hdrlen
);
4381 COPYFIELD64(ifi_recvquota
);
4382 COPYFIELD64(ifi_xmitquota
);
4383 if_data64
->ifi_unused1
= 0;
4384 COPYFIELD64(ifi_mtu
);
4385 COPYFIELD64(ifi_metric
);
4386 COPYFIELD64(ifi_baudrate
);
4388 COPYFIELD64_ATOMIC(ifi_ipackets
);
4389 COPYFIELD64_ATOMIC(ifi_ierrors
);
4390 COPYFIELD64_ATOMIC(ifi_opackets
);
4391 COPYFIELD64_ATOMIC(ifi_oerrors
);
4392 COPYFIELD64_ATOMIC(ifi_collisions
);
4393 COPYFIELD64_ATOMIC(ifi_ibytes
);
4394 COPYFIELD64_ATOMIC(ifi_obytes
);
4395 COPYFIELD64_ATOMIC(ifi_imcasts
);
4396 COPYFIELD64_ATOMIC(ifi_omcasts
);
4397 COPYFIELD64_ATOMIC(ifi_iqdrops
);
4398 COPYFIELD64_ATOMIC(ifi_noproto
);
4401 * Note these two fields are actually 32 bit, so doing
4402 * COPYFIELD64_ATOMIC will cause them to be misaligned
4404 COPYFIELD64(ifi_recvtiming
);
4405 COPYFIELD64(ifi_xmittiming
);
4407 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
4408 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
4410 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
4415 __private_extern__
void
4416 if_copy_traffic_class(struct ifnet
*ifp
,
4417 struct if_traffic_class
*if_tc
)
4419 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
4420 atomic_get_64(if_tc->fld, \
4421 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
4424 bzero(if_tc
, sizeof (*if_tc
));
4425 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
4426 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
4427 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
4428 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
4429 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
4430 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
4431 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
4432 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
4433 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
4434 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
4435 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
4436 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
4437 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
4438 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
4439 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
4440 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
4441 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
4442 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
4443 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
4444 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
4446 #undef COPY_IF_TC_FIELD64_ATOMIC
4450 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
4452 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
4453 atomic_get_64(if_de->fld, \
4454 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
4457 bzero(if_de
, sizeof (*if_de
));
4458 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
4459 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes
);
4460 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets
);
4461 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes
);
4463 #undef COPY_IF_DE_FIELD64_ATOMIC
4467 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
4469 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
4470 atomic_get_64(if_ps->ifi_tcp_##fld, \
4471 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
4474 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
4475 atomic_get_64(if_ps->ifi_udp_##fld, \
4476 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
4479 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
4480 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
4481 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
4482 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
4483 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
4484 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
4485 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
4486 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
4487 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
4488 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
4489 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
4490 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
4491 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
4492 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
4494 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
4495 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
4496 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
4497 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
4498 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
4499 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
4500 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
4501 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
4503 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
4504 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
4508 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
4510 bzero(if_rs
, sizeof (*if_rs
));
4511 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1))
4514 /* by now, ifnet will stay attached so if_inp must be valid */
4515 VERIFY(ifp
->if_inp
!= NULL
);
4516 bcopy(&ifp
->if_inp
->pstats
, if_rs
, sizeof (*if_rs
));
4518 /* Release the IO refcnt */
4519 ifnet_decr_iorefcnt(ifp
);
4523 ifa_remref(struct ifaddr
*ifa
, int locked
)
4528 IFA_LOCK_ASSERT_HELD(ifa
);
4530 if (ifa
->ifa_refcnt
== 0)
4531 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
4532 else if (ifa
->ifa_trace
!= NULL
)
4533 (*ifa
->ifa_trace
)(ifa
, FALSE
);
4534 if (--ifa
->ifa_refcnt
== 0) {
4535 if (ifa
->ifa_debug
& IFD_ATTACHED
)
4536 panic("ifa %p attached to ifp is being freed\n", ifa
);
4538 * Some interface addresses are allocated either statically
4539 * or carved out of a larger block. Only free it if it was
4540 * allocated via MALLOC or via the corresponding per-address
4541 * family allocator. Otherwise, leave it alone.
4543 if (ifa
->ifa_debug
& IFD_ALLOC
) {
4544 if (ifa
->ifa_free
== NULL
) {
4546 FREE(ifa
, M_IFADDR
);
4548 /* Become a regular mutex */
4549 IFA_CONVERT_LOCK(ifa
);
4550 /* callee will unlock */
4551 (*ifa
->ifa_free
)(ifa
);
4559 if (!locked
&& ifa
!= NULL
)
4566 ifa_addref(struct ifaddr
*ifa
, int locked
)
4571 IFA_LOCK_ASSERT_HELD(ifa
);
4573 if (++ifa
->ifa_refcnt
== 0) {
4574 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
4576 } else if (ifa
->ifa_trace
!= NULL
) {
4577 (*ifa
->ifa_trace
)(ifa
, TRUE
);
4584 ifa_lock_init(struct ifaddr
*ifa
)
4586 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4590 ifa_lock_destroy(struct ifaddr
*ifa
)
4592 IFA_LOCK_ASSERT_NOTHELD(ifa
);
4593 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);
4599 * The switch statement below does nothing at runtime, as it serves as a
4600 * compile time check to ensure that all of the socket 'i' ioctls (those
4601 * in the 'i' group going thru soo_ioctl) that are made available by the
4602 * networking stack is unique. This works as long as this routine gets
4603 * updated each time a new interface ioctl gets added.
4605 * Any failures at compile time indicates duplicated ioctl values.
4607 static __attribute__((unused
)) void
4608 ifioctl_cassert(void)
4611 * This is equivalent to _CASSERT() and the compiler wouldn't
4612 * generate any instructions, thus for compile time only.
4614 switch ((u_long
)0) {
4617 /* bsd/net/if_ppp.h */
4619 case SIOCGPPPCSTATS
:
4622 /* bsd/netinet6/in6_var.h */
4623 case SIOCSIFADDR_IN6
:
4624 case SIOCGIFADDR_IN6
:
4625 case SIOCSIFDSTADDR_IN6
:
4626 case SIOCSIFNETMASK_IN6
:
4627 case SIOCGIFDSTADDR_IN6
:
4628 case SIOCGIFNETMASK_IN6
:
4629 case SIOCDIFADDR_IN6
:
4630 case SIOCAIFADDR_IN6_32
:
4631 case SIOCAIFADDR_IN6_64
:
4632 case SIOCSIFPHYADDR_IN6_32
:
4633 case SIOCSIFPHYADDR_IN6_64
:
4634 case SIOCGIFPSRCADDR_IN6
:
4635 case SIOCGIFPDSTADDR_IN6
:
4636 case SIOCGIFAFLAG_IN6
:
4637 case SIOCGDRLST_IN6_32
:
4638 case SIOCGDRLST_IN6_64
:
4639 case SIOCGPRLST_IN6_32
:
4640 case SIOCGPRLST_IN6_64
:
4641 case OSIOCGIFINFO_IN6
:
4642 case SIOCGIFINFO_IN6
:
4643 case SIOCSNDFLUSH_IN6
:
4644 case SIOCGNBRINFO_IN6_32
:
4645 case SIOCGNBRINFO_IN6_64
:
4646 case SIOCSPFXFLUSH_IN6
:
4647 case SIOCSRTRFLUSH_IN6
:
4648 case SIOCGIFALIFETIME_IN6
:
4649 case SIOCSIFALIFETIME_IN6
:
4650 case SIOCGIFSTAT_IN6
:
4651 case SIOCGIFSTAT_ICMP6
:
4652 case SIOCSDEFIFACE_IN6_32
:
4653 case SIOCSDEFIFACE_IN6_64
:
4654 case SIOCGDEFIFACE_IN6_32
:
4655 case SIOCGDEFIFACE_IN6_64
:
4656 case SIOCSIFINFO_FLAGS
:
4659 case SIOCGSCOPE6DEF
:
4660 case SIOCSIFPREFIX_IN6
:
4661 case SIOCGIFPREFIX_IN6
:
4662 case SIOCDIFPREFIX_IN6
:
4663 case SIOCAIFPREFIX_IN6
:
4664 case SIOCCIFPREFIX_IN6
:
4665 case SIOCSGIFPREFIX_IN6
:
4666 case SIOCPROTOATTACH_IN6_32
:
4667 case SIOCPROTOATTACH_IN6_64
:
4668 case SIOCPROTODETACH_IN6
:
4669 case SIOCLL_START_32
:
4670 case SIOCLL_START_64
:
4672 case SIOCAUTOCONF_START
:
4673 case SIOCAUTOCONF_STOP
:
4674 case SIOCSETROUTERMODE_IN6
:
4675 case SIOCLL_CGASTART_32
:
4676 case SIOCLL_CGASTART_64
:
4677 case SIOCGIFCGAPREP_IN6
:
4678 case SIOCSIFCGAPREP_IN6
:
4681 /* bsd/sys/sockio.h */
4684 case SIOCSIFDSTADDR
:
4685 case OSIOCGIFDSTADDR
:
4688 case OSIOCGIFBRDADDR
:
4689 case SIOCSIFBRDADDR
:
4690 case OSIOCGIFCONF32
:
4691 case OSIOCGIFCONF64
:
4692 case OSIOCGIFNETMASK
:
4693 case SIOCSIFNETMASK
:
4700 case SIOCGIFDSTADDR
:
4701 case SIOCGIFBRDADDR
:
4704 case SIOCGIFNETMASK
:
4706 case SIOCAUTONETMASK
:
4716 case SIOCGIFMEDIA32
:
4717 case SIOCGIFMEDIA64
:
4718 case SIOCSIFGENERIC
:
4719 case SIOCGIFGENERIC
:
4724 case SIOCSIFPHYADDR
:
4725 case SIOCGIFPSRCADDR
:
4726 case SIOCGIFPDSTADDR
:
4727 case SIOCDIFPHYADDR
:
4735 case SIOCPROTOATTACH
:
4736 case SIOCPROTODETACH
:
4745 case SIOCSDRVSPEC32
:
4746 case SIOCGDRVSPEC32
:
4747 case SIOCSDRVSPEC64
:
4748 case SIOCGDRVSPEC64
:
4753 case SIOCIFGCLONERS32
:
4754 case SIOCIFGCLONERS64
:
4756 case SIOCGIFASYNCMAP
:
4757 case SIOCSIFASYNCMAP
:
4761 #endif /* CONFIG_MACF_NET */
4765 case SIOCGIFWAKEFLAGS
:
4767 case SIOCGIFGETRTREFCNT
:
4768 case SIOCGIFLINKQUALITYMETRIC
:
4769 case SIOCSIFOPPORTUNISTIC
:
4770 case SIOCGIFOPPORTUNISTIC
:
4771 case SIOCSETROUTERMODE
:
4775 case SIOCSIFLINKPARAMS
:
4776 case SIOCGIFLINKPARAMS
:
4777 case SIOCGIFQUEUESTATS
:
4778 case SIOCSIFTHROTTLE
:
4779 case SIOCGIFTHROTTLE
:
4781 case SIOCGASSOCIDS32
:
4782 case SIOCGASSOCIDS64
:
4783 case SIOCGCONNIDS32
:
4784 case SIOCGCONNIDS64
:
4785 case SIOCGCONNINFO32
:
4786 case SIOCGCONNINFO64
:
4787 case SIOCSCONNORDER
:
4788 case SIOCGCONNORDER
:
4792 case SIOCGIFDELEGATE
:
4795 case SIOCGIFEXPENSIVE
:
4796 case SIOCSIFEXPENSIVE
:
4799 case SIOCGSTARTDELAY
:
4801 case SIOCAIFAGENTID
:
4802 case SIOCDIFAGENTID
:
4803 case SIOCGIFAGENTIDS32
:
4804 case SIOCGIFAGENTIDS64
:
4805 case SIOCGIFAGENTDATA32
:
4806 case SIOCGIFAGENTDATA64
:
4807 case SIOCGIFAGENTLIST32
:
4808 case SIOCGIFAGENTLIST64
:
4811 case SIOCSIFINTERFACESTATE
:
4812 case SIOCGIFINTERFACESTATE
:
4813 case SIOCSIFPROBECONNECTIVITY
:
4814 case SIOCGIFPROBECONNECTIVITY
:
4816 case SIOCGIFFUNCTIONALTYPE
:
4817 case SIOCSIFNETSIGNATURE
:
4818 case SIOCGIFNETSIGNATURE
:
4823 case SIOCSQOSMARKINGMODE
:
4824 case SIOCSQOSMARKINGENABLED
:
4825 case SIOCGQOSMARKINGMODE
:
4826 case SIOCGQOSMARKINGENABLED
: