2 * Copyright (c) 2000-2015 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>
89 #include <machine/endian.h>
91 #include <pexpert/pexpert.h>
94 #include <net/if_arp.h>
95 #include <net/if_dl.h>
96 #include <net/if_types.h>
97 #include <net/if_var.h>
98 #include <net/if_ppp.h>
99 #include <net/ethernet.h>
100 #include <net/network_agent.h>
102 #include <net/radix.h>
103 #include <net/route.h>
104 #include <net/dlil.h>
105 #include <sys/domain.h>
106 #include <libkern/OSAtomic.h>
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/ip6.h>
114 #include <netinet/ip_var.h>
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_var.h>
117 #include <netinet/udp.h>
118 #include <netinet/udp_var.h>
120 #include <netinet6/in6_var.h>
121 #include <netinet6/in6_ifattach.h>
122 #include <netinet6/ip6_var.h>
123 #include <netinet6/nd6.h>
125 #endif /* INET || INET6 */
128 #include <security/mac_framework.h>
132 #include <net/altq/if_altq.h>
133 #endif /* !PF_ALTQ */
136 * System initialization
139 extern char *proc_name_address(void *);
141 /* Lock group and attribute for ifaddr lock */
142 lck_attr_t
*ifa_mtx_attr
;
143 lck_grp_t
*ifa_mtx_grp
;
144 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
146 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
148 static int ifioctl_ifconf(u_long
, caddr_t
);
149 static int ifioctl_ifclone(u_long
, caddr_t
);
150 static int ifioctl_ifdesc(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
151 static int ifioctl_linkparams(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
152 static int ifioctl_qstats(struct ifnet
*, u_long
, caddr_t
);
153 static int ifioctl_throttle(struct ifnet
*, u_long
, caddr_t
, struct proc
*);
154 static int ifioctl_netsignature(struct ifnet
*, u_long
, caddr_t
);
155 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
156 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
157 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
159 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
160 struct ifmultiaddr
**, int);
161 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
162 const struct sockaddr
*, int);
164 static int if_rtmtu(struct radix_node
*, void *);
165 static void if_rtmtu_update(struct ifnet
*);
167 static int if_clone_list(int, int *, user_addr_t
);
169 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
171 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
173 static int if_cloners_count
;
174 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
176 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
178 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
179 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
181 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
182 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
184 static struct ifmultiaddr
*ifma_alloc(int);
185 static void ifma_free(struct ifmultiaddr
*);
186 static void ifma_trace(struct ifmultiaddr
*, int);
189 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
191 static unsigned int ifma_debug
; /* debugging (disabled) */
193 static unsigned int ifma_size
; /* size of zone element */
194 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
196 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
199 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
201 struct ifmultiaddr_dbg
{
202 struct ifmultiaddr ifma
; /* ifmultiaddr */
203 u_int16_t ifma_refhold_cnt
; /* # of ref */
204 u_int16_t ifma_refrele_cnt
; /* # of rele */
206 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
208 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
209 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
213 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
216 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
217 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
218 static decl_lck_mtx_data(, ifma_trash_lock
);
220 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
221 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
225 * XXX: declare here to avoid to include many inet6 related files..
226 * should be more generalized?
228 extern void nd6_setmtu(struct ifnet
*);
229 extern lck_mtx_t
*nd6_mutex
;
232 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
233 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
234 "Generic link-management");
236 SYSCTL_DECL(_net_link_generic_system
);
238 static uint32_t if_verbose
= 0;
239 SYSCTL_INT(_net_link_generic_system
, OID_AUTO
, if_verbose
,
240 CTLFLAG_RW
| CTLFLAG_LOCKED
, &if_verbose
, 0, "");
245 /* Setup lock group and attribute for ifaddr */
246 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
247 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
248 ifa_mtx_attr
= lck_attr_alloc_init();
250 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof (ifma_debug
));
252 ifma_size
= (ifma_debug
== 0) ? sizeof (struct ifmultiaddr
) :
253 sizeof (struct ifmultiaddr_dbg
);
255 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
257 if (ifma_zone
== NULL
) {
258 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
261 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
262 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
264 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
265 TAILQ_INIT(&ifma_trash_head
);
269 * Network interface utility routines.
271 * Routines with ifa_ifwith* names take sockaddr *'s as
276 struct ifaddr
**ifnet_addrs
;
277 struct ifnet
**ifindex2ifnet
;
279 __private_extern__
void
280 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
282 if_attach_ifa_common(ifp
, ifa
, 0);
285 __private_extern__
void
286 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
288 if_attach_ifa_common(ifp
, ifa
, 1);
292 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
294 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
295 IFA_LOCK_ASSERT_HELD(ifa
);
297 if (ifa
->ifa_ifp
!= ifp
) {
298 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
301 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
302 panic("%s: Attempt to attach an already attached ifa=%p",
305 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
306 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
308 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
309 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
312 IFA_ADDREF_LOCKED(ifa
);
313 ifa
->ifa_debug
|= IFD_ATTACHED
;
315 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
317 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
319 if (ifa
->ifa_attached
!= NULL
)
320 (*ifa
->ifa_attached
)(ifa
);
323 __private_extern__
void
324 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
326 if_detach_ifa_common(ifp
, ifa
, 0);
329 __private_extern__
void
330 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
332 if_detach_ifa_common(ifp
, ifa
, 1);
336 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
338 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
339 IFA_LOCK_ASSERT_HELD(ifa
);
341 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
342 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
344 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
345 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
347 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
348 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
350 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
351 panic("%s: Attempt to detach an unattached address ifa=%p",
354 } else if (ifa
->ifa_ifp
!= ifp
) {
355 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
358 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
360 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
365 panic("%s: Attempt to detach a stray address ifa=%p",
370 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
371 /* This must not be the last reference to the ifaddr */
372 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
373 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
376 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
378 if (ifa
->ifa_detached
!= NULL
)
379 (*ifa
->ifa_detached
)(ifa
);
382 #define INITIAL_IF_INDEXLIM 8
385 * Function: if_next_index
387 * Return the next available interface index.
388 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
389 * added entry when necessary.
392 * ifnet_addrs[] is indexed by (if_index - 1), whereas
393 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
394 * always allocate one extra element to hold ifindex2ifnet[0], which
397 int if_next_index(void);
399 __private_extern__
int
402 static int if_indexlim
= 0;
405 new_index
= ++if_index
;
406 if (if_index
> if_indexlim
) {
409 caddr_t new_ifnet_addrs
;
410 caddr_t new_ifindex2ifnet
;
411 caddr_t old_ifnet_addrs
;
413 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
414 if (ifnet_addrs
== NULL
) {
415 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
417 new_if_indexlim
= if_indexlim
<< 1;
420 /* allocate space for the larger arrays */
421 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
422 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
423 if (new_ifnet_addrs
== NULL
) {
428 new_ifindex2ifnet
= new_ifnet_addrs
429 + new_if_indexlim
* sizeof(caddr_t
);
430 if (ifnet_addrs
!= NULL
) {
431 /* copy the existing data */
432 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
433 if_indexlim
* sizeof(caddr_t
));
434 bcopy((caddr_t
)ifindex2ifnet
,
436 (if_indexlim
+ 1) * sizeof(caddr_t
));
439 /* switch to the new tables and size */
440 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
441 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
442 if_indexlim
= new_if_indexlim
;
444 /* release the old data */
445 if (old_ifnet_addrs
!= NULL
) {
446 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
453 * Create a clone network interface.
456 if_clone_create(char *name
, int len
, void *params
)
458 struct if_clone
*ifc
;
461 u_int32_t bytoff
, bitoff
;
465 ifc
= if_clone_lookup(name
, &unit
);
469 if (ifunit(name
) != NULL
)
473 wildcard
= (unit
== UINT32_MAX
);
475 * Find a free unit if none was given.
478 while ((bytoff
< ifc
->ifc_bmlen
)
479 && (ifc
->ifc_units
[bytoff
] == 0xff))
481 if (bytoff
>= ifc
->ifc_bmlen
)
483 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
485 unit
= (bytoff
<< 3) + bitoff
;
488 if (unit
> ifc
->ifc_maxunit
)
491 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
497 bitoff
= unit
- (bytoff
<< 3);
501 * Allocate the unit in the bitmap.
503 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
504 ("%s: bit is already set", __func__
));
505 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
507 /* In the wildcard case, we need to update the name. */
509 for (dp
= name
; *dp
!= '\0'; dp
++);
510 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
511 len
- (dp
-name
) - 1) {
513 * This can only be a programmer error and
514 * there's no straightforward way to recover if
517 panic("%s: interface name too long", __func__
);
527 * Destroy a clone network interface.
530 if_clone_destroy(const char *name
)
532 struct if_clone
*ifc
;
537 ifc
= if_clone_lookup(name
, &unit
);
541 if (unit
< ifc
->ifc_minifs
)
548 if (ifc
->ifc_destroy
== NULL
)
551 (*ifc
->ifc_destroy
)(ifp
);
554 * Compute offset in the bitmap and deallocate the unit.
557 bitoff
= unit
- (bytoff
<< 3);
558 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
559 ("%s: bit is already cleared", __func__
));
560 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
565 * Look up a network interface cloner.
568 __private_extern__
struct if_clone
*
569 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
571 struct if_clone
*ifc
;
575 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
576 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
577 if (ifc
->ifc_name
[i
] != *cp
)
582 ifc
= LIST_NEXT(ifc
, ifc_list
);
586 return ((struct if_clone
*)NULL
);
592 for (i
= 0; *cp
!= '\0'; cp
++) {
593 if (*cp
< '0' || *cp
> '9') {
594 /* Bogus unit number. */
597 i
= (i
* 10) + (*cp
- '0');
607 * Register a network interface cloner.
610 if_clone_attach(struct if_clone
*ifc
)
617 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
618 ("%s: %s requested more units then allowed (%d > %d)",
619 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
620 ifc
->ifc_maxunit
+ 1));
622 * Compute bitmap size and allocate it.
624 maxclone
= ifc
->ifc_maxunit
+ 1;
626 if ((len
<< 3) < maxclone
)
628 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
629 if (ifc
->ifc_units
== NULL
)
631 ifc
->ifc_bmlen
= len
;
633 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
636 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
637 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
639 ("%s: failed to create required interface %s%d",
640 __func__
, ifc
->ifc_name
, unit
));
642 /* Allocate the unit in the bitmap. */
644 bitoff
= unit
- (bytoff
<< 3);
645 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
652 * Unregister a network interface cloner.
655 if_clone_detach(struct if_clone
*ifc
)
657 LIST_REMOVE(ifc
, ifc_list
);
658 FREE(ifc
->ifc_units
, M_CLONE
);
663 * Provide list of interface cloners to userspace.
666 if_clone_list(int count
, int *ret_total
, user_addr_t dst
)
668 char outbuf
[IFNAMSIZ
];
669 struct if_clone
*ifc
;
672 *ret_total
= if_cloners_count
;
673 if (dst
== USER_ADDR_NULL
) {
674 /* Just asking how many there are. */
681 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
683 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
684 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
685 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
686 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
695 if_functional_type(struct ifnet
*ifp
)
697 u_int32_t ret
= IFRTYPE_FUNCTIONAL_UNKNOWN
;
699 if (ifp
->if_flags
& IFF_LOOPBACK
) {
700 ret
= IFRTYPE_FUNCTIONAL_LOOPBACK
;
701 } else if (IFNET_IS_WIFI(ifp
)) {
702 if (ifp
->if_eflags
& IFEF_AWDL
)
703 ret
= IFRTYPE_FUNCTIONAL_WIFI_AWDL
;
705 ret
= IFRTYPE_FUNCTIONAL_WIFI_INFRA
;
706 } else if (IFNET_IS_CELLULAR(ifp
)) {
707 ret
= IFRTYPE_FUNCTIONAL_CELLULAR
;
708 } else if (IFNET_IS_WIRED(ifp
)) {
709 ret
= IFRTYPE_FUNCTIONAL_WIRED
;
717 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
718 * and that it matches only the local (not broadcast) address.
720 __private_extern__
struct in_ifaddr
*
721 ifa_foraddr(unsigned int addr
)
723 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
727 * Similar to ifa_foraddr, except with the added interface scope
728 * constraint (unless the caller passes in IFSCOPE_NONE in which
729 * case there is no scope restriction).
731 __private_extern__
struct in_ifaddr
*
732 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
734 struct in_ifaddr
*ia
= NULL
;
736 lck_rw_lock_shared(in_ifaddr_rwlock
);
737 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
738 IFA_LOCK_SPIN(&ia
->ia_ifa
);
739 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
740 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
741 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
742 IFA_UNLOCK(&ia
->ia_ifa
);
745 IFA_UNLOCK(&ia
->ia_ifa
);
747 lck_rw_done(in_ifaddr_rwlock
);
753 * Similar to ifa_foraddr, except that this for IPv6.
755 __private_extern__
struct in6_ifaddr
*
756 ifa_foraddr6(struct in6_addr
*addr6
)
758 return (ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
));
761 __private_extern__
struct in6_ifaddr
*
762 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
764 struct in6_ifaddr
*ia
= NULL
;
766 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
767 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
768 IFA_LOCK(&ia
->ia_ifa
);
769 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
770 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
771 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
772 IFA_UNLOCK(&ia
->ia_ifa
);
775 IFA_UNLOCK(&ia
->ia_ifa
);
777 lck_rw_done(&in6_ifaddr_rwlock
);
784 * Return the first (primary) address of a given family on an interface.
786 __private_extern__
struct ifaddr
*
787 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
791 ifnet_lock_shared(ifp
);
792 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
794 if (ifa
->ifa_addr
->sa_family
== family
) {
795 IFA_ADDREF_LOCKED(ifa
); /* for caller */
801 ifnet_lock_done(ifp
);
807 * Locate an interface based on a complete address.
811 ifa_ifwithaddr(const struct sockaddr
*addr
)
815 struct ifaddr
*result
= NULL
;
817 #define equal(a1, a2) \
818 (bcmp((const void*)(a1), (const void*)(a2), \
819 ((const struct sockaddr *)(a1))->sa_len) == 0)
821 ifnet_head_lock_shared();
822 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
823 ifp
= ifp
->if_link
.tqe_next
) {
824 ifnet_lock_shared(ifp
);
825 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
826 ifa
= ifa
->ifa_link
.tqe_next
) {
828 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
832 if (equal(addr
, ifa
->ifa_addr
)) {
834 IFA_ADDREF_LOCKED(ifa
); /* for caller */
838 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
839 ifa
->ifa_broadaddr
!= NULL
&&
840 /* IP6 doesn't have broadcast */
841 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
842 equal(ifa
->ifa_broadaddr
, addr
)) {
844 IFA_ADDREF_LOCKED(ifa
); /* for caller */
850 ifnet_lock_done(ifp
);
857 * Locate the point to point interface with a given destination address.
861 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
865 struct ifaddr
*result
= NULL
;
867 ifnet_head_lock_shared();
868 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
869 ifp
= ifp
->if_link
.tqe_next
) {
870 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
871 ifnet_lock_shared(ifp
);
872 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
873 ifa
= ifa
->ifa_link
.tqe_next
) {
875 if (ifa
->ifa_addr
->sa_family
!=
880 if (ifa
->ifa_dstaddr
&&
881 equal(addr
, ifa
->ifa_dstaddr
)) {
883 IFA_ADDREF_LOCKED(ifa
); /* for caller */
889 ifnet_lock_done(ifp
);
897 * Locate the source address of an interface based on a complete address.
900 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
902 struct ifaddr
*result
= NULL
;
905 if (ifscope
== IFSCOPE_NONE
)
906 return (ifa_ifwithaddr(addr
));
908 ifnet_head_lock_shared();
909 if (ifscope
> (unsigned int)if_index
) {
914 ifp
= ifindex2ifnet
[ifscope
];
916 struct ifaddr
*ifa
= NULL
;
919 * This is suboptimal; there should be a better way
920 * to search for a given address of an interface
921 * for any given address family.
923 ifnet_lock_shared(ifp
);
924 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
925 ifa
= ifa
->ifa_link
.tqe_next
) {
927 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
931 if (equal(addr
, ifa
->ifa_addr
)) {
933 IFA_ADDREF_LOCKED(ifa
); /* for caller */
937 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
938 ifa
->ifa_broadaddr
!= NULL
&&
939 /* IP6 doesn't have broadcast */
940 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
941 equal(ifa
->ifa_broadaddr
, addr
)) {
943 IFA_ADDREF_LOCKED(ifa
); /* for caller */
949 ifnet_lock_done(ifp
);
957 ifa_ifwithnet(const struct sockaddr
*addr
)
959 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
963 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
965 return (ifa_ifwithnet_common(addr
, ifscope
));
969 * Find an interface on a specific network. If many, choice
970 * is most specific found.
972 static struct ifaddr
*
973 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
976 struct ifaddr
*ifa
= NULL
;
977 struct ifaddr
*ifa_maybe
= NULL
;
978 u_int af
= addr
->sa_family
;
979 const char *addr_data
= addr
->sa_data
, *cplim
;
982 if ((af
!= AF_INET
&& af
!= AF_INET6
) ||
983 (af
== AF_INET
&& !ip_doscopedroute
) ||
984 (af
== AF_INET6
&& !ip6_doscopedroute
))
986 if (af
!= AF_INET
|| !ip_doscopedroute
)
988 ifscope
= IFSCOPE_NONE
;
990 ifnet_head_lock_shared();
992 * AF_LINK addresses can be looked up directly by their index number,
993 * so do that if we can.
996 const struct sockaddr_dl
*sdl
=
997 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
998 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
999 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
1009 * Scan though each interface, looking for ones that have
1010 * addresses in this address family.
1012 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1013 ifnet_lock_shared(ifp
);
1014 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1015 ifa
= ifa
->ifa_link
.tqe_next
) {
1016 const char *cp
, *cp2
, *cp3
;
1019 if (ifa
->ifa_addr
== NULL
||
1020 ifa
->ifa_addr
->sa_family
!= af
) {
1026 * If we're looking up with a scope,
1027 * find using a matching interface.
1029 if (ifscope
!= IFSCOPE_NONE
&&
1030 ifp
->if_index
!= ifscope
) {
1036 * Scan all the bits in the ifa's address.
1037 * If a bit dissagrees with what we are
1038 * looking for, mask it with the netmask
1039 * to see if it really matters.
1040 * (A byte at a time)
1042 if (ifa
->ifa_netmask
== 0) {
1047 cp2
= ifa
->ifa_addr
->sa_data
;
1048 cp3
= ifa
->ifa_netmask
->sa_data
;
1049 cplim
= ifa
->ifa_netmask
->sa_len
+
1050 (char *)ifa
->ifa_netmask
;
1052 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1053 goto next
; /* next address! */
1055 * If the netmask of what we just found
1056 * is more specific than what we had before
1057 * (if we had one) then remember the new one
1058 * before continuing to search
1059 * for an even better one.
1061 if (ifa_maybe
== NULL
||
1062 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1063 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1064 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1066 if (ifa_maybe
!= NULL
)
1067 IFA_REMREF(ifa_maybe
);
1072 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1074 ifnet_lock_done(ifp
);
1083 else if (ifa_maybe
!= NULL
)
1084 IFA_REMREF(ifa_maybe
);
1090 * Find an interface address specific to an interface best matching
1094 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1096 struct ifaddr
*ifa
= NULL
;
1097 const char *cp
, *cp2
, *cp3
;
1099 struct ifaddr
*ifa_maybe
= NULL
;
1100 struct ifaddr
*better_ifa_maybe
= NULL
;
1101 u_int af
= addr
->sa_family
;
1106 ifnet_lock_shared(ifp
);
1107 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1108 ifa
= ifa
->ifa_link
.tqe_next
) {
1110 if (ifa
->ifa_addr
->sa_family
!= af
) {
1114 if (ifa_maybe
== NULL
) {
1115 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1118 if (ifa
->ifa_netmask
== 0) {
1119 if (equal(addr
, ifa
->ifa_addr
) || (ifa
->ifa_dstaddr
&&
1120 equal(addr
, ifa
->ifa_dstaddr
))) {
1121 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1128 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1129 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
1130 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1135 if (equal(addr
, ifa
->ifa_addr
)) {
1137 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1142 cp2
= ifa
->ifa_addr
->sa_data
;
1143 cp3
= ifa
->ifa_netmask
->sa_data
;
1144 cplim
= ifa
->ifa_netmask
->sa_len
+
1145 (char *)ifa
->ifa_netmask
;
1146 for (; cp3
< cplim
; cp3
++)
1147 if ((*cp
++ ^ *cp2
++) & *cp3
)
1151 if (better_ifa_maybe
== NULL
) {
1152 /* for better_ifa_maybe */
1153 IFA_ADDREF_LOCKED(ifa
);
1154 better_ifa_maybe
= ifa
;
1162 if (better_ifa_maybe
!= NULL
) {
1163 ifa
= better_ifa_maybe
;
1164 better_ifa_maybe
= NULL
;
1171 ifnet_lock_done(ifp
);
1173 if (better_ifa_maybe
!= NULL
)
1174 IFA_REMREF(better_ifa_maybe
);
1175 if (ifa_maybe
!= NULL
)
1176 IFA_REMREF(ifa_maybe
);
1181 #include <net/route.h>
1184 * Default action when installing a route with a Link Level gateway.
1185 * Lookup an appropriate real ifa to point to.
1186 * This should be moved to /sys/net/link.c eventually.
1189 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1192 struct sockaddr
*dst
;
1194 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1196 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1197 RT_LOCK_ASSERT_HELD(rt
);
1199 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1200 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
1203 /* Become a regular mutex, just in case */
1204 RT_CONVERT_LOCK(rt
);
1206 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1210 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1212 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
)
1213 ifa_rtrequest(cmd
, rt
, sa
);
1219 * if_updown will set the interface up or down. It will
1220 * prevent other up/down events from occurring until this
1221 * up/down event has completed.
1223 * Caller must lock ifnet. This function will drop the
1224 * lock. This allows ifnet_set_flags to set the rest of
1225 * the flags after we change the up/down state without
1226 * dropping the interface lock between setting the
1227 * up/down state and updating the rest of the flags.
1229 __private_extern__
void
1235 struct ifaddr
**ifa
;
1237 struct ifclassq
*ifq
= &ifp
->if_snd
;
1239 /* Wait until no one else is changing the up/down state */
1240 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1242 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1243 ifnet_lock_done(ifp
);
1244 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1245 ifnet_lock_exclusive(ifp
);
1248 /* Verify that the interface isn't already in the right state */
1249 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1250 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1254 /* Indicate that the up/down state is changing */
1255 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1257 /* Mark interface up or down */
1259 ifp
->if_flags
|= IFF_UP
;
1262 ifp
->if_flags
&= ~IFF_UP
;
1265 ifnet_touch_lastchange(ifp
);
1267 /* Drop the lock to notify addresses and route */
1268 ifnet_lock_done(ifp
);
1269 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1270 for (i
= 0; ifa
[i
] != 0; i
++) {
1271 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1273 ifnet_free_address_list(ifa
);
1280 /* Inform all transmit queues about the new link state */
1282 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1285 /* Aquire the lock to clear the changing flag */
1286 ifnet_lock_exclusive(ifp
);
1287 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1288 wakeup(&ifp
->if_eflags
);
1292 * Mark an interface down and notify protocols of
1299 ifnet_lock_exclusive(ifp
);
1301 ifnet_lock_done(ifp
);
1305 * Mark an interface up and notify protocols of
1312 ifnet_lock_exclusive(ifp
);
1314 ifnet_lock_done(ifp
);
1318 * Flush an interface queue.
1321 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1323 struct ifclassq
*ifq
= &ifp
->if_snd
;
1328 if (IFCQ_IS_ENABLED(ifq
))
1331 if (IFCQ_IS_DRAINING(ifq
))
1332 ifq
->ifcq_drain
= 0;
1333 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1334 ALTQ_PURGE(IFCQ_ALTQ(ifq
));
1335 #endif /* PF_ALTQ */
1337 VERIFY(IFCQ_IS_EMPTY(ifq
));
1344 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1345 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1347 struct ifclassq
*ifq
= &ifp
->if_snd
;
1348 u_int32_t cnt
= 0, len
= 0;
1349 u_int32_t a_cnt
= 0, a_len
= 0;
1351 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1357 if (IFCQ_IS_ENABLED(ifq
))
1358 IFCQ_PURGE_SC(ifq
, sc
, flow
, cnt
, len
);
1360 if (IFCQ_IS_DRAINING(ifq
)) {
1361 VERIFY((signed)(ifq
->ifcq_drain
- cnt
) >= 0);
1362 ifq
->ifcq_drain
-= cnt
;
1364 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1365 ALTQ_PURGE_SC(IFCQ_ALTQ(ifq
), sc
, flow
, a_cnt
, a_len
);
1366 #endif /* PF_ALTQ */
1371 if (packets
!= NULL
)
1372 *packets
= cnt
+ a_cnt
;
1374 *bytes
= len
+ a_len
;
1378 * Map interface name to
1379 * interface structure pointer.
1382 ifunit(const char *name
)
1384 char namebuf
[IFNAMSIZ
+ 1];
1392 if (len
< 2 || len
> IFNAMSIZ
)
1394 cp
= name
+ len
- 1;
1396 if (c
< '0' || c
> '9')
1397 return (NULL
); /* trailing garbage */
1402 return (NULL
); /* no interface name */
1403 unit
+= (c
- '0') * m
;
1405 return (NULL
); /* number is unreasonable */
1408 } while (c
>= '0' && c
<= '9');
1409 len
= cp
- name
+ 1;
1410 bcopy(name
, namebuf
, len
);
1411 namebuf
[len
] = '\0';
1413 * Now search all the interfaces for this name/number
1415 ifnet_head_lock_shared();
1416 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1417 if (strncmp(ifp
->if_name
, namebuf
, len
))
1419 if (unit
== ifp
->if_unit
)
1428 * Map interface name in a sockaddr_dl to
1429 * interface structure pointer.
1432 if_withname(struct sockaddr
*sa
)
1434 char ifname
[IFNAMSIZ
+1];
1435 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1437 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1438 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1442 * ifunit wants a null-terminated name. It may not be null-terminated
1443 * in the sockaddr. We don't want to change the caller's sockaddr,
1444 * and there might not be room to put the trailing null anyway, so we
1445 * make a local copy that we know we can null terminate safely.
1448 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1449 ifname
[sdl
->sdl_nlen
] = '\0';
1450 return (ifunit(ifname
));
1453 static __attribute__((noinline
)) int
1454 ifioctl_ifconf(u_long cmd
, caddr_t data
)
1459 case OSIOCGIFCONF32
: /* struct ifconf32 */
1460 case SIOCGIFCONF32
: { /* struct ifconf32 */
1461 struct ifconf32 ifc
;
1462 bcopy(data
, &ifc
, sizeof (ifc
));
1463 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1465 bcopy(&ifc
, data
, sizeof (ifc
));
1469 case SIOCGIFCONF64
: /* struct ifconf64 */
1470 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1471 struct ifconf64 ifc
;
1472 bcopy(data
, &ifc
, sizeof (ifc
));
1473 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1474 bcopy(&ifc
, data
, sizeof (ifc
));
1486 static __attribute__((noinline
)) int
1487 ifioctl_ifclone(u_long cmd
, caddr_t data
)
1492 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1493 struct if_clonereq32 ifcr
;
1494 bcopy(data
, &ifcr
, sizeof (ifcr
));
1495 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1496 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1497 bcopy(&ifcr
, data
, sizeof (ifcr
));
1501 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1502 struct if_clonereq64 ifcr
;
1503 bcopy(data
, &ifcr
, sizeof (ifcr
));
1504 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1506 bcopy(&ifcr
, data
, sizeof (ifcr
));
1518 static __attribute__((noinline
)) int
1519 ifioctl_ifdesc(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1521 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1525 VERIFY(ifp
!= NULL
);
1528 case SIOCSIFDESC
: { /* struct if_descreq */
1529 if ((error
= proc_suser(p
)) != 0)
1532 ifnet_lock_exclusive(ifp
);
1533 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof (ifdr_len
));
1534 if (ifdr_len
> sizeof (ifdr
->ifdr_desc
) ||
1535 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1537 ifnet_lock_done(ifp
);
1541 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1542 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1543 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1544 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1546 ifnet_lock_done(ifp
);
1550 case SIOCGIFDESC
: { /* struct if_descreq */
1551 ifnet_lock_shared(ifp
);
1552 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof (ifdr
->ifdr_desc
));
1553 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof (ifdr_len
));
1554 bzero(&ifdr
->ifdr_desc
, sizeof (ifdr
->ifdr_desc
));
1556 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1558 ifnet_lock_done(ifp
);
1570 static __attribute__((noinline
)) int
1571 ifioctl_linkparams(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1573 struct if_linkparamsreq
*iflpr
=
1574 (struct if_linkparamsreq
*)(void *)data
;
1575 struct ifclassq
*ifq
;
1578 VERIFY(ifp
!= NULL
);
1582 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1583 struct tb_profile tb
= { 0, 0, 0 };
1585 if ((error
= proc_suser(p
)) != 0)
1589 if (!IFCQ_IS_READY(ifq
)) {
1594 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1596 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1597 sizeof (tb
.percent
));
1598 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1603 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1604 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1605 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1609 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
))) {
1610 sched_type
= IFCQ_ALTQ(ifq
)->altq_type
;
1611 flags
|= IFLPRF_ALTQ
;
1613 #endif /* PF_ALTQ */
1615 if (IFCQ_IS_ENABLED(ifq
))
1616 sched_type
= ifq
->ifcq_type
;
1618 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1619 sizeof (iflpr
->iflpr_output_sched
));
1621 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1622 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1623 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1625 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1626 sizeof (iflpr
->iflpr_output_tbr_rate
));
1627 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1628 sizeof (iflpr
->iflpr_output_tbr_percent
));
1631 if (ifp
->if_output_sched_model
==
1632 IFNET_SCHED_MODEL_DRIVER_MANAGED
)
1633 flags
|= IFLPRF_DRVMANAGED
;
1634 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof (iflpr
->iflpr_flags
));
1635 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1636 sizeof (iflpr
->iflpr_output_bw
));
1637 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1638 sizeof (iflpr
->iflpr_input_bw
));
1639 bcopy(&ifp
->if_output_lt
, &iflpr
->iflpr_output_lt
,
1640 sizeof (iflpr
->iflpr_output_lt
));
1641 bcopy(&ifp
->if_input_lt
, &iflpr
->iflpr_input_lt
,
1642 sizeof (iflpr
->iflpr_input_lt
));
1654 static __attribute__((noinline
)) int
1655 ifioctl_qstats(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1657 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1658 u_int32_t ifqr_len
, ifqr_slot
;
1661 VERIFY(ifp
!= NULL
);
1664 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1665 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof (ifqr_slot
));
1666 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof (ifqr_len
));
1667 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1668 ifqr
->ifqr_buf
, &ifqr_len
);
1671 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof (ifqr_len
));
1683 static __attribute__((noinline
)) int
1684 ifioctl_throttle(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1686 struct if_throttlereq
*ifthr
= (struct if_throttlereq
*)(void *)data
;
1687 u_int32_t ifthr_level
;
1690 VERIFY(ifp
!= NULL
);
1693 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1695 * XXX: Use priv_check_cred() instead of root check?
1697 if ((error
= proc_suser(p
)) != 0)
1700 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof (ifthr_level
));
1701 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1702 if (error
== EALREADY
)
1707 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1708 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1709 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1710 sizeof (ifthr_level
));
1724 ifioctl_getnetagents(struct ifnet
*ifp
, u_int32_t
*count
, user_addr_t uuid_p
)
1728 u_int32_t valid_netagent_count
= 0;
1730 for (index
= 0; index
< IF_MAXAGENTS
; index
++) {
1731 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1732 if (!uuid_is_null(*netagent_uuid
)) {
1733 if (uuid_p
!= USER_ADDR_NULL
) {
1734 if ((error
= copyout(netagent_uuid
,
1735 uuid_p
+ sizeof(uuid_t
) * valid_netagent_count
,
1736 sizeof(uuid_t
))) != 0) {
1740 valid_netagent_count
++;
1743 *count
= valid_netagent_count
;
1748 static __attribute__((noinline
)) int
1749 ifioctl_netagent(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct proc
*p
)
1751 struct if_agentidreq
*ifar
= (struct if_agentidreq
*)(void *)data
;
1753 struct if_agentidsreq32 s32
;
1754 struct if_agentidsreq64 s64
;
1759 VERIFY(ifp
!= NULL
);
1762 case SIOCAIFAGENTID
: { /* struct if_agentidreq */
1763 uuid_t
*first_empty_slot
= NULL
;
1764 // TODO: Use priv_check_cred() instead of root check
1765 if ((error
= proc_suser(p
)) != 0) {
1768 for (index
= 0; index
< IF_MAXAGENTS
; index
++) {
1769 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1770 if (uuid_compare(*netagent_uuid
, ifar
->ifar_uuid
) == 0) {
1771 /* Already present, ignore */
1774 if (first_empty_slot
== NULL
&&
1775 uuid_is_null(*netagent_uuid
)) {
1776 first_empty_slot
= netagent_uuid
;
1779 if (first_empty_slot
== NULL
) {
1780 error
= ENOMEM
; /* No empty slot for a netagent UUID, bail */
1783 uuid_copy(*first_empty_slot
, ifar
->ifar_uuid
);
1784 netagent_post_updated_interfaces(ifar
->ifar_uuid
);
1787 case SIOCDIFAGENTID
: { /* struct if_agentidreq */
1788 bool removed_agent_id
= FALSE
;
1789 // TODO: Use priv_check_cred() instead of root check
1790 if ((error
= proc_suser(p
)) != 0) {
1793 for (index
= 0; index
< IF_MAXAGENTS
; index
++) {
1794 uuid_t
*netagent_uuid
= &(ifp
->if_agentids
[index
]);
1795 if (uuid_compare(*netagent_uuid
, ifar
->ifar_uuid
) == 0) {
1796 uuid_clear(*netagent_uuid
);
1797 removed_agent_id
= TRUE
;
1801 if (removed_agent_id
) {
1802 netagent_post_updated_interfaces(ifar
->ifar_uuid
);
1806 case SIOCGIFAGENTIDS32
: { /* struct if_agentidsreq32 */
1807 bcopy(data
, &u
.s32
, sizeof(u
.s32
));
1808 error
= ifioctl_getnetagents(ifp
, &u
.s32
.ifar_count
, u
.s32
.ifar_uuids
);
1810 bcopy(&u
.s32
, data
, sizeof(u
.s32
));
1814 case SIOCGIFAGENTIDS64
: { /* struct if_agentidsreq64 */
1815 bcopy(data
, &u
.s64
, sizeof(u
.s64
));
1816 error
= ifioctl_getnetagents(ifp
, &u
.s64
.ifar_count
, u
.s64
.ifar_uuids
);
1818 bcopy(&u
.s64
, data
, sizeof(u
.s64
));
1831 ifnet_clear_netagent(uuid_t netagent_uuid
)
1833 struct ifnet
*ifp
= NULL
;
1835 bool removed_agent_id
= FALSE
;
1837 ifnet_head_lock_shared();
1839 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1840 for (index
= 0; index
< IF_MAXAGENTS
; index
++) {
1841 uuid_t
*ifp_netagent_uuid
= &(ifp
->if_agentids
[index
]);
1842 if (uuid_compare(*ifp_netagent_uuid
, netagent_uuid
) == 0) {
1843 uuid_clear(*ifp_netagent_uuid
);
1844 removed_agent_id
= TRUE
;
1852 static __attribute__((noinline
)) int
1853 ifioctl_netsignature(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1855 struct if_nsreq
*ifnsr
= (struct if_nsreq
*)(void *)data
;
1859 VERIFY(ifp
!= NULL
);
1862 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
1863 if (ifnsr
->ifnsr_len
> sizeof (ifnsr
->ifnsr_data
)) {
1867 bcopy(&ifnsr
->ifnsr_flags
, &flags
, sizeof (flags
));
1868 error
= ifnet_set_netsignature(ifp
, ifnsr
->ifnsr_family
,
1869 ifnsr
->ifnsr_len
, flags
, ifnsr
->ifnsr_data
);
1872 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
1873 ifnsr
->ifnsr_len
= sizeof (ifnsr
->ifnsr_data
);
1874 error
= ifnet_get_netsignature(ifp
, ifnsr
->ifnsr_family
,
1875 &ifnsr
->ifnsr_len
, &flags
, ifnsr
->ifnsr_data
);
1877 bcopy(&flags
, &ifnsr
->ifnsr_flags
, sizeof (flags
));
1879 ifnsr
->ifnsr_len
= 0;
1893 * Most of the routines called to handle the ioctls would end up being
1894 * tail-call optimized, which unfortunately causes this routine to
1895 * consume too much stack space; this is the reason for the "noinline"
1896 * attribute used on those routines.
1899 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1901 char ifname
[IFNAMSIZ
+ 1];
1902 struct ifnet
*ifp
= NULL
;
1903 struct ifstat
*ifs
= NULL
;
1906 bzero(ifname
, sizeof (ifname
));
1909 * ioctls which don't require ifp, or ifreq ioctls
1912 case OSIOCGIFCONF32
: /* struct ifconf32 */
1913 case SIOCGIFCONF32
: /* struct ifconf32 */
1914 case SIOCGIFCONF64
: /* struct ifconf64 */
1915 case OSIOCGIFCONF64
: /* struct ifconf64 */
1916 error
= ifioctl_ifconf(cmd
, data
);
1919 case SIOCIFGCLONERS32
: /* struct if_clonereq32 */
1920 case SIOCIFGCLONERS64
: /* struct if_clonereq64 */
1921 error
= ifioctl_ifclone(cmd
, data
);
1924 case SIOCGIFAGENTDATA32
: /* struct netagent_req32 */
1925 case SIOCGIFAGENTDATA64
: /* struct netagent_req64 */
1926 error
= netagent_ioctl(cmd
, data
);
1929 case SIOCSIFDSTADDR
: /* struct ifreq */
1930 case SIOCSIFADDR
: /* struct ifreq */
1931 case SIOCSIFBRDADDR
: /* struct ifreq */
1932 case SIOCSIFNETMASK
: /* struct ifreq */
1933 case OSIOCGIFADDR
: /* struct ifreq */
1934 case OSIOCGIFDSTADDR
: /* struct ifreq */
1935 case OSIOCGIFBRDADDR
: /* struct ifreq */
1936 case OSIOCGIFNETMASK
: /* struct ifreq */
1937 case SIOCSIFKPI
: /* struct ifreq */
1938 if (so
->so_proto
== NULL
) {
1943 case SIOCIFCREATE
: /* struct ifreq */
1944 case SIOCIFCREATE2
: /* struct ifreq */
1945 case SIOCIFDESTROY
: /* struct ifreq */
1946 case SIOCGIFFLAGS
: /* struct ifreq */
1947 case SIOCGIFEFLAGS
: /* struct ifreq */
1948 case SIOCGIFCAP
: /* struct ifreq */
1950 case SIOCGIFMAC
: /* struct ifreq */
1951 case SIOCSIFMAC
: /* struct ifreq */
1952 #endif /* CONFIG_MACF_NET */
1953 case SIOCGIFMETRIC
: /* struct ifreq */
1954 case SIOCGIFMTU
: /* struct ifreq */
1955 case SIOCGIFPHYS
: /* struct ifreq */
1956 case SIOCSIFFLAGS
: /* struct ifreq */
1957 case SIOCSIFCAP
: /* struct ifreq */
1958 case SIOCSIFMETRIC
: /* struct ifreq */
1959 case SIOCSIFPHYS
: /* struct ifreq */
1960 case SIOCSIFMTU
: /* struct ifreq */
1961 case SIOCADDMULTI
: /* struct ifreq */
1962 case SIOCDELMULTI
: /* struct ifreq */
1963 case SIOCDIFPHYADDR
: /* struct ifreq */
1964 case SIOCSIFMEDIA
: /* struct ifreq */
1965 case SIOCSIFGENERIC
: /* struct ifreq */
1966 case SIOCSIFLLADDR
: /* struct ifreq */
1967 case SIOCSIFALTMTU
: /* struct ifreq */
1968 case SIOCSIFVLAN
: /* struct ifreq */
1969 case SIOCSIFBOND
: /* struct ifreq */
1970 case SIOCGIFLLADDR
: /* struct ifreq */
1971 case SIOCGIFTYPE
: /* struct ifreq */
1972 case SIOCGIFFUNCTIONALTYPE
: /* struct ifreq */
1973 case SIOCGIFPSRCADDR
: /* struct ifreq */
1974 case SIOCGIFPDSTADDR
: /* struct ifreq */
1975 case SIOCGIFGENERIC
: /* struct ifreq */
1976 case SIOCGIFDEVMTU
: /* struct ifreq */
1977 case SIOCGIFVLAN
: /* struct ifreq */
1978 case SIOCGIFBOND
: /* struct ifreq */
1979 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
1980 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
1981 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
1982 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
1983 case SIOCGIFLINKQUALITYMETRIC
: /* struct ifreq */
1984 case SIOCSIFLOG
: /* struct ifreq */
1985 case SIOCGIFLOG
: /* struct ifreq */
1986 case SIOCGIFDELEGATE
: /* struct ifreq */
1987 case SIOCGIFEXPENSIVE
: /* struct ifreq */
1988 case SIOCSIFEXPENSIVE
: /* struct ifreq */
1989 case SIOCSIF2KCL
: /* struct ifreq */
1990 case SIOCGIF2KCL
: /* struct ifreq */
1991 case SIOCSIFINTERFACESTATE
: /* struct ifreq */
1992 case SIOCGIFINTERFACESTATE
: /* struct ifreq */
1993 case SIOCSIFPROBECONNECTIVITY
: /* struct ifreq */
1994 case SIOCGIFPROBECONNECTIVITY
: /* struct ifreq */
1995 case SIOCGSTARTDELAY
: { /* struct ifreq */
1997 bcopy(data
, &ifr
, sizeof (ifr
));
1998 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1999 bcopy(&ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2000 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
2001 bcopy(&ifr
, data
, sizeof (ifr
));
2007 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2008 * here to ensure that the ifnet, if found, has been fully attached.
2012 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2013 bcopy(((struct in_aliasreq
*)(void *)data
)->ifra_name
,
2015 ifp
= ifunit(ifname
);
2019 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2020 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
2022 ifp
= ifunit(ifname
);
2025 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2026 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
2028 ifp
= ifunit(ifname
);
2032 case SIOCGIFSTATUS
: /* struct ifstat */
2033 ifs
= _MALLOC(sizeof (*ifs
), M_DEVBUF
, M_WAITOK
);
2039 bcopy(data
, ifs
, sizeof (*ifs
));
2040 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
2041 bcopy(ifs
->ifs_name
, ifname
, IFNAMSIZ
);
2042 ifp
= ifunit(ifname
);
2045 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2046 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
2048 ifp
= ifunit(ifname
);
2051 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
2052 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
2054 ifp
= ifunit(ifname
);
2057 case SIOCSIFDESC
: /* struct if_descreq */
2058 case SIOCGIFDESC
: /* struct if_descreq */
2059 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
2061 ifp
= ifunit(ifname
);
2064 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
2065 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
2066 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
2068 ifp
= ifunit(ifname
);
2071 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
2072 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
2074 ifp
= ifunit(ifname
);
2077 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
2078 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
2079 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
2081 ifp
= ifunit(ifname
);
2084 case SIOCAIFAGENTID
: /* struct if_agentidreq */
2085 case SIOCDIFAGENTID
: /* struct if_agentidreq */
2086 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
2087 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
2088 bcopy(((struct if_agentidreq
*)(void *)data
)->ifar_name
,
2090 ifp
= ifunit(ifname
);
2093 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2094 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2095 bcopy(((struct if_nsreq
*)(void *)data
)->ifnsr_name
,
2097 ifp
= ifunit(ifname
);
2102 * This is a bad assumption, but the code seems to
2103 * have been doing this in the past; caveat emptor.
2105 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
2107 ifp
= ifunit(ifname
);
2118 case SIOCSIFPHYADDR
: /* struct {if,in_}aliasreq */
2120 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
2121 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
2123 error
= proc_suser(p
);
2127 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2131 ifnet_touch_lastchange(ifp
);
2134 case SIOCGIFSTATUS
: /* struct ifstat */
2135 VERIFY(ifs
!= NULL
);
2136 ifs
->ascii
[0] = '\0';
2138 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifs
);
2140 bcopy(ifs
, data
, sizeof (*ifs
));
2143 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
2144 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
2145 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2148 case SIOCSIFDESC
: /* struct if_descreq */
2149 case SIOCGIFDESC
: /* struct if_descreq */
2150 error
= ifioctl_ifdesc(ifp
, cmd
, data
, p
);
2153 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
2154 case SIOCGIFLINKPARAMS
: /* struct if_linkparamsreq */
2155 error
= ifioctl_linkparams(ifp
, cmd
, data
, p
);
2158 case SIOCGIFQUEUESTATS
: /* struct if_qstatsreq */
2159 error
= ifioctl_qstats(ifp
, cmd
, data
);
2162 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
2163 case SIOCGIFTHROTTLE
: /* struct if_throttlereq */
2164 error
= ifioctl_throttle(ifp
, cmd
, data
, p
);
2167 case SIOCAIFAGENTID
: /* struct if_agentidreq */
2168 case SIOCDIFAGENTID
: /* struct if_agentidreq */
2169 case SIOCGIFAGENTIDS32
: /* struct if_agentidsreq32 */
2170 case SIOCGIFAGENTIDS64
: /* struct if_agentidsreq64 */
2171 error
= ifioctl_netagent(ifp
, cmd
, data
, p
);
2174 case SIOCSIFNETSIGNATURE
: /* struct if_nsreq */
2175 case SIOCGIFNETSIGNATURE
: /* struct if_nsreq */
2176 error
= ifioctl_netsignature(ifp
, cmd
, data
);
2180 if (so
->so_proto
== NULL
) {
2186 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
2188 socket_unlock(so
, 1);
2190 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
2191 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, data
);
2198 _FREE(ifs
, M_DEVBUF
);
2201 if (ifname
[0] == '\0')
2202 (void) snprintf(ifname
, sizeof (ifname
), "%s",
2204 else if (ifp
!= NULL
)
2205 (void) snprintf(ifname
, sizeof (ifname
), "%s",
2209 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2210 "%c %lu) error %d\n", __func__
,
2211 proc_name_address(p
), proc_pid(p
),
2212 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
2213 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
2214 (char)IOCGROUP(cmd
), cmd
& 0xff, error
);
2215 } else if (if_verbose
> 1) {
2216 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2217 "%c %lu) OK\n", __func__
,
2218 proc_name_address(p
), proc_pid(p
),
2219 ifname
, cmd
, (cmd
& IOC_IN
) ? 'I' : ' ',
2220 (cmd
& IOC_OUT
) ? 'O' : ' ', IOCPARM_LEN(cmd
),
2221 (char)IOCGROUP(cmd
), cmd
& 0xff);
2228 static __attribute__((noinline
)) int
2229 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
2234 struct kev_msg ev_msg
;
2235 struct net_event_data ev_data
;
2237 bzero(&ev_data
, sizeof (struct net_event_data
));
2238 bzero(&ev_msg
, sizeof (struct kev_msg
));
2243 error
= proc_suser(p
);
2246 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
2247 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
2249 error
= proc_suser(p
);
2252 return (if_clone_destroy(ifr
->ifr_name
));
2256 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2257 * here to ensure that the ifnet, if found, has been fully attached.
2260 ifp
= ifunit(ifr
->ifr_name
);
2268 ifnet_lock_shared(ifp
);
2269 ifr
->ifr_flags
= ifp
->if_flags
;
2270 ifnet_lock_done(ifp
);
2274 ifnet_lock_shared(ifp
);
2275 ifr
->ifr_eflags
= ifp
->if_eflags
;
2276 ifnet_lock_done(ifp
);
2280 ifnet_lock_shared(ifp
);
2281 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
2282 ifr
->ifr_curcap
= ifp
->if_capenable
;
2283 ifnet_lock_done(ifp
);
2288 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
2292 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
2294 #endif /* CONFIG_MACF_NET */
2297 ifnet_lock_shared(ifp
);
2298 ifr
->ifr_metric
= ifp
->if_metric
;
2299 ifnet_lock_done(ifp
);
2303 ifnet_lock_shared(ifp
);
2304 ifr
->ifr_mtu
= ifp
->if_mtu
;
2305 ifnet_lock_done(ifp
);
2309 ifnet_lock_shared(ifp
);
2310 ifr
->ifr_phys
= ifp
->if_physical
;
2311 ifnet_lock_done(ifp
);
2315 error
= proc_suser(p
);
2319 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
2320 (u_int16_t
)~IFF_CANTCHANGE
);
2323 * Note that we intentionally ignore any error from below
2324 * for the SIOCSIFFLAGS case.
2326 (void) ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2329 * Send the event even upon error from the driver because
2330 * we changed the flags.
2332 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2333 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2334 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2336 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
2337 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2338 ev_data
.if_family
= ifp
->if_family
;
2339 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2340 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2341 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2342 ev_msg
.dv
[1].data_length
= 0;
2343 kev_post_msg(&ev_msg
);
2345 ifnet_touch_lastchange(ifp
);
2349 error
= proc_suser(p
);
2353 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
2357 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2359 ifnet_touch_lastchange(ifp
);
2363 error
= proc_suser(p
);
2367 ifp
->if_metric
= ifr
->ifr_metric
;
2369 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2370 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2371 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2373 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
2374 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2375 ev_data
.if_family
= ifp
->if_family
;
2376 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2377 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2378 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2380 ev_msg
.dv
[1].data_length
= 0;
2381 kev_post_msg(&ev_msg
);
2383 ifnet_touch_lastchange(ifp
);
2387 error
= proc_suser(p
);
2391 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2395 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2396 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2397 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2399 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
2400 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2401 ev_data
.if_family
= ifp
->if_family
;
2402 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2403 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2404 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2405 ev_msg
.dv
[1].data_length
= 0;
2406 kev_post_msg(&ev_msg
);
2408 ifnet_touch_lastchange(ifp
);
2412 u_int32_t oldmtu
= ifp
->if_mtu
;
2413 struct ifclassq
*ifq
= &ifp
->if_snd
;
2415 error
= proc_suser(p
);
2419 if (ifp
->if_ioctl
== NULL
) {
2423 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2427 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2431 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2432 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2433 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2435 ev_msg
.event_code
= KEV_DL_SIFMTU
;
2436 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2437 ev_data
.if_family
= ifp
->if_family
;
2438 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2439 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2440 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2441 ev_msg
.dv
[1].data_length
= 0;
2442 kev_post_msg(&ev_msg
);
2444 ifnet_touch_lastchange(ifp
);
2448 * If the link MTU changed, do network layer specific procedure
2449 * and update all route entries associated with the interface,
2450 * so that their MTU metric gets updated.
2452 if (ifp
->if_mtu
!= oldmtu
) {
2453 if_rtmtu_update(ifp
);
2457 /* Inform all transmit queues about the new MTU */
2459 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
2467 error
= proc_suser(p
);
2471 /* Don't allow group membership on non-multicast interfaces. */
2472 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2477 /* Don't let users screw up protocols' entries. */
2478 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
2479 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2485 * User is permitted to anonymously join a particular link
2486 * multicast group via SIOCADDMULTI. Subsequent join requested
2487 * for the same record which has an outstanding refcnt from a
2488 * past if_addmulti_anon() will not result in EADDRINUSE error
2489 * (unlike other BSDs.) Anonymously leaving a group is also
2490 * allowed only as long as there is an outstanding refcnt held
2491 * by a previous anonymous request, or else ENOENT (even if the
2492 * link-layer multicast membership exists for a network-layer
2495 if (cmd
== SIOCADDMULTI
) {
2496 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
2497 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
2499 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
2500 ev_msg
.event_code
= KEV_DL_DELMULTI
;
2505 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2506 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2507 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2508 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2510 ev_data
.if_family
= ifp
->if_family
;
2511 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2512 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2513 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2514 ev_msg
.dv
[1].data_length
= 0;
2515 kev_post_msg(&ev_msg
);
2517 ifnet_touch_lastchange(ifp
);
2520 case SIOCDIFPHYADDR
:
2522 case SIOCSIFGENERIC
:
2527 error
= proc_suser(p
);
2531 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2535 ifnet_touch_lastchange(ifp
);
2538 case SIOCGIFLLADDR
: {
2539 struct sockaddr_dl
*sdl
= SDL(ifp
->if_lladdr
->ifa_addr
);
2541 if (sdl
->sdl_alen
== 0) {
2542 error
= EADDRNOTAVAIL
;
2545 /* If larger than 14-bytes we'll need another mechanism */
2546 if (sdl
->sdl_alen
> sizeof (ifr
->ifr_addr
.sa_data
)) {
2550 /* Follow the same convention used by SIOCSIFLLADDR */
2551 bzero(&ifr
->ifr_addr
, sizeof (ifr
->ifr_addr
));
2552 ifr
->ifr_addr
.sa_family
= AF_LINK
;
2553 ifr
->ifr_addr
.sa_len
= sdl
->sdl_alen
;
2554 error
= ifnet_guarded_lladdr_copy_bytes(ifp
,
2555 &ifr
->ifr_addr
.sa_data
, sdl
->sdl_alen
);
2560 ifr
->ifr_type
.ift_type
= ifp
->if_type
;
2561 ifr
->ifr_type
.ift_family
= ifp
->if_family
;
2562 ifr
->ifr_type
.ift_subfamily
= ifp
->if_subfamily
;
2565 case SIOCGIFFUNCTIONALTYPE
:
2566 ifr
->ifr_functional_type
= if_functional_type(ifp
);
2569 case SIOCGIFPSRCADDR
:
2570 case SIOCGIFPDSTADDR
:
2571 case SIOCGIFGENERIC
:
2575 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
, (caddr_t
)ifr
);
2578 case SIOCGIFWAKEFLAGS
:
2579 ifnet_lock_shared(ifp
);
2580 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
2581 ifnet_lock_done(ifp
);
2584 case SIOCGIFGETRTREFCNT
:
2585 ifnet_lock_shared(ifp
);
2586 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
2587 ifnet_lock_done(ifp
);
2590 case SIOCSIFOPPORTUNISTIC
:
2591 case SIOCGIFOPPORTUNISTIC
:
2592 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
2595 case SIOCGIFLINKQUALITYMETRIC
:
2596 ifnet_lock_shared(ifp
);
2597 if ((ifp
->if_interface_state
.valid_bitmask
&
2598 IF_INTERFACE_STATE_LQM_STATE_VALID
))
2599 ifr
->ifr_link_quality_metric
=
2600 ifp
->if_interface_state
.lqm_state
;
2601 else if ((ifp
->if_refflags
& IFRF_ATTACHED
)) {
2602 ifr
->ifr_link_quality_metric
=
2603 IFNET_LQM_THRESH_UNKNOWN
;
2605 ifr
->ifr_link_quality_metric
=
2606 IFNET_LQM_THRESH_OFF
;
2608 ifnet_lock_done(ifp
);
2613 error
= ifnet_getset_log(ifp
, cmd
, ifr
, p
);
2616 case SIOCGIFDELEGATE
:
2617 ifnet_lock_shared(ifp
);
2618 ifr
->ifr_delegated
= ((ifp
->if_delegated
.ifp
!= NULL
) ?
2619 ifp
->if_delegated
.ifp
->if_index
: 0);
2620 ifnet_lock_done(ifp
);
2623 case SIOCGIFEXPENSIVE
:
2624 ifnet_lock_shared(ifp
);
2625 if (ifp
->if_eflags
& IFEF_EXPENSIVE
)
2626 ifr
->ifr_expensive
= 1;
2628 ifr
->ifr_expensive
= 0;
2629 ifnet_lock_done(ifp
);
2632 case SIOCSIFEXPENSIVE
:
2636 if ((error
= priv_check_cred(kauth_cred_get(),
2637 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
2639 ifnet_lock_exclusive(ifp
);
2640 if (ifr
->ifr_expensive
)
2641 ifp
->if_eflags
|= IFEF_EXPENSIVE
;
2643 ifp
->if_eflags
&= ~IFEF_EXPENSIVE
;
2644 ifnet_lock_done(ifp
);
2646 * Update the expensive bit in the delegated interface
2649 ifnet_head_lock_shared();
2650 TAILQ_FOREACH(difp
, &ifnet_head
, if_link
) {
2651 ifnet_lock_exclusive(difp
);
2652 if (difp
->if_delegated
.ifp
== ifp
) {
2653 difp
->if_delegated
.expensive
=
2654 ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
2657 ifnet_lock_done(difp
);
2664 ifnet_lock_shared(ifp
);
2665 if (ifp
->if_eflags
& IFEF_2KCL
)
2669 ifnet_lock_done(ifp
);
2673 if ((error
= priv_check_cred(kauth_cred_get(),
2674 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
2676 ifnet_lock_exclusive(ifp
);
2678 ifp
->if_eflags
|= IFEF_2KCL
;
2680 ifp
->if_eflags
&= ~IFEF_2KCL
;
2681 ifnet_lock_done(ifp
);
2683 case SIOCGSTARTDELAY
:
2684 ifnet_lock_shared(ifp
);
2685 if (ifp
->if_eflags
& IFEF_ENQUEUE_MULTI
) {
2686 ifr
->ifr_start_delay_qlen
=
2687 ifp
->if_start_delay_qlen
;
2688 ifr
->ifr_start_delay_timeout
=
2689 ifp
->if_start_delay_timeout
;
2691 ifr
->ifr_start_delay_qlen
= 0;
2692 ifr
->ifr_start_delay_timeout
= 0;
2694 ifnet_lock_done(ifp
);
2696 case SIOCSIFDSTADDR
:
2698 case SIOCSIFBRDADDR
:
2699 case SIOCSIFNETMASK
:
2701 case OSIOCGIFDSTADDR
:
2702 case OSIOCGIFBRDADDR
:
2703 case OSIOCGIFNETMASK
:
2705 VERIFY(so
->so_proto
!= NULL
);
2707 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
2708 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
2709 #if BYTE_ORDER != BIG_ENDIAN
2710 if (ifr
->ifr_addr
.sa_family
== 0 &&
2711 ifr
->ifr_addr
.sa_len
< 16) {
2712 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
2713 ifr
->ifr_addr
.sa_len
= 16;
2716 if (ifr
->ifr_addr
.sa_len
== 0)
2717 ifr
->ifr_addr
.sa_len
= 16;
2719 } else if (cmd
== OSIOCGIFADDR
) {
2720 cmd
= SIOCGIFADDR
; /* struct ifreq */
2721 } else if (cmd
== OSIOCGIFDSTADDR
) {
2722 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
2723 } else if (cmd
== OSIOCGIFBRDADDR
) {
2724 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
2725 } else if (cmd
== OSIOCGIFNETMASK
) {
2726 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
2730 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
2731 (caddr_t
)ifr
, ifp
, p
));
2732 socket_unlock(so
, 1);
2736 case OSIOCGIFDSTADDR
:
2737 case OSIOCGIFBRDADDR
:
2738 case OSIOCGIFNETMASK
:
2739 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
2743 if (cmd
== SIOCSIFKPI
) {
2744 int temperr
= proc_suser(p
);
2749 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
2750 error
= ifnet_ioctl(ifp
, SOCK_DOM(so
), cmd
,
2755 case SIOCGIFINTERFACESTATE
:
2756 if_get_state(ifp
, &ifr
->ifr_interface_state
);
2759 case SIOCSIFINTERFACESTATE
:
2760 if ((error
= priv_check_cred(kauth_cred_get(),
2761 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
2764 error
= if_state_update(ifp
, &ifr
->ifr_interface_state
);
2767 case SIOCSIFPROBECONNECTIVITY
:
2768 if ((error
= priv_check_cred(kauth_cred_get(),
2769 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
2771 error
= if_probe_connectivity(ifp
,
2772 ifr
->ifr_probe_connectivity
);
2774 case SIOCGIFPROBECONNECTIVITY
:
2775 if ((error
= priv_check_cred(kauth_cred_get(),
2776 PRIV_NET_INTERFACE_CONTROL
, 0)) != 0)
2778 if (ifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)
2779 ifr
->ifr_probe_connectivity
= 1;
2781 ifr
->ifr_probe_connectivity
= 0;
2792 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2796 socket_unlock(so
, 0);
2797 error
= ifioctl(so
, cmd
, data
, p
);
2803 * Set/clear promiscuous mode on interface ifp based on the truth value
2804 * of pswitch. The calls are reference counted so that only the first
2805 * "on" request actually has an effect, as does the final "off" request.
2806 * Results are undefined if the "off" and "on" requests are not matched.
2809 ifnet_set_promiscuous(
2817 ifnet_lock_exclusive(ifp
);
2818 oldflags
= ifp
->if_flags
;
2819 ifp
->if_pcount
+= pswitch
? 1 : -1;
2821 if (ifp
->if_pcount
> 0)
2822 ifp
->if_flags
|= IFF_PROMISC
;
2824 ifp
->if_flags
&= ~IFF_PROMISC
;
2826 newflags
= ifp
->if_flags
;
2827 ifnet_lock_done(ifp
);
2829 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
2830 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
2834 ifnet_lock_exclusive(ifp
);
2836 ifp
->if_pcount
-= pswitch
? 1 : -1;
2837 if (ifp
->if_pcount
> 0)
2838 ifp
->if_flags
|= IFF_PROMISC
;
2840 ifp
->if_flags
&= ~IFF_PROMISC
;
2841 ifnet_lock_done(ifp
);
2845 if (newflags
!= oldflags
) {
2846 log(LOG_INFO
, "%s: promiscuous mode %s%s\n",
2848 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
2849 error
!= 0 ? " failed" : " succeeded");
2855 * Return interface configuration
2856 * of system. List may be used
2857 * in later ioctl's (above) to get
2858 * other information.
2862 ifconf(u_long cmd
, user_addr_t ifrp
, int *ret_space
)
2864 struct ifnet
*ifp
= NULL
;
2869 net_thread_marks_t marks
;
2871 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
2874 * Zero the ifr buffer to make sure we don't
2875 * disclose the contents of the stack.
2877 bzero(&ifr
, sizeof (struct ifreq
));
2880 ifnet_head_lock_shared();
2881 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof (ifr
) &&
2882 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
2884 size_t ifnlen
, addrs
;
2886 ifnlen
= snprintf(workbuf
, sizeof (workbuf
),
2887 "%s", if_name(ifp
));
2888 if (ifnlen
+ 1 > sizeof (ifr
.ifr_name
)) {
2889 error
= ENAMETOOLONG
;
2892 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
2895 ifnet_lock_shared(ifp
);
2898 ifa
= ifp
->if_addrhead
.tqh_first
;
2899 for (; space
> sizeof (ifr
) && ifa
;
2900 ifa
= ifa
->ifa_link
.tqe_next
) {
2901 struct sockaddr
*sa
;
2904 struct sockaddr_dl sdl
;
2905 uint8_t buf
[SOCK_MAXADDRLEN
+ 1];
2909 * Make sure to accomodate the largest possible
2910 * size of SA(if_lladdr)->sa_len.
2912 _CASSERT(sizeof (u
) == (SOCK_MAXADDRLEN
+ 1));
2918 if (ifa
== ifp
->if_lladdr
) {
2919 VERIFY(sa
->sa_family
== AF_LINK
);
2920 bcopy(sa
, &u
, sa
->sa_len
);
2922 ifnet_guarded_lladdr_copy_bytes(ifp
,
2923 LLADDR(&u
.sdl
), u
.sdl
.sdl_alen
);
2928 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
2929 struct osockaddr
*osa
=
2930 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
2932 osa
->sa_family
= sa
->sa_family
;
2933 error
= copyout((caddr_t
)&ifr
, ifrp
,
2935 ifrp
+= sizeof (struct ifreq
);
2936 } else if (sa
->sa_len
<= sizeof (*sa
)) {
2938 error
= copyout((caddr_t
)&ifr
, ifrp
,
2940 ifrp
+= sizeof (struct ifreq
);
2943 sizeof (ifr
) + sa
->sa_len
- sizeof (*sa
)) {
2947 space
-= sa
->sa_len
- sizeof (*sa
);
2948 error
= copyout((caddr_t
)&ifr
, ifrp
,
2949 sizeof (ifr
.ifr_name
));
2951 error
= copyout((caddr_t
)sa
, (ifrp
+
2952 offsetof(struct ifreq
, ifr_addr
)),
2955 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
2961 space
-= sizeof (ifr
);
2963 ifnet_lock_done(ifp
);
2968 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof (ifr
.ifr_addr
));
2969 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
2972 space
-= sizeof (ifr
);
2973 ifrp
+= sizeof (struct ifreq
);
2977 *ret_space
-= space
;
2978 net_thread_marks_pop(marks
);
2983 * Just like if_promisc(), but for all-multicast-reception mode.
2986 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2991 ifnet_lock_exclusive(ifp
);
2994 if (ifp
->if_amcount
++ == 0) {
2995 ifp
->if_flags
|= IFF_ALLMULTI
;
2999 if (ifp
->if_amcount
> 1) {
3002 ifp
->if_amcount
= 0;
3003 ifp
->if_flags
&= ~IFF_ALLMULTI
;
3007 ifnet_lock_done(ifp
);
3010 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
3017 static struct ifmultiaddr
*
3020 struct ifmultiaddr
*ifma
;
3022 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
3023 zalloc_noblock(ifma_zone
);
3026 bzero(ifma
, ifma_size
);
3027 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3028 ifma
->ifma_debug
|= IFD_ALLOC
;
3029 if (ifma_debug
!= 0) {
3030 ifma
->ifma_debug
|= IFD_DEBUG
;
3031 ifma
->ifma_trace
= ifma_trace
;
3038 ifma_free(struct ifmultiaddr
*ifma
)
3042 if (ifma
->ifma_protospec
!= NULL
) {
3043 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
3045 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3046 ifma
->ifma_anoncnt
!= 0) {
3047 panic("%s: Freeing ifma=%p with outstanding anon req",
3050 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
3051 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
3052 __func__
, ifma
, ifma
->ifma_ifp
);
3054 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
3055 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
3057 } else if (ifma
->ifma_refcount
!= 0) {
3058 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
3060 } else if (ifma
->ifma_reqcnt
!= 0) {
3061 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
3063 } else if (ifma
->ifma_ifp
!= NULL
) {
3064 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
3065 ifma
->ifma_ifp
, ifma
);
3067 } else if (ifma
->ifma_ll
!= NULL
) {
3068 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
3069 ifma
->ifma_ll
, ifma
);
3072 ifma
->ifma_debug
&= ~IFD_ALLOC
;
3073 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3074 (IFD_DEBUG
| IFD_TRASHED
)) {
3075 lck_mtx_lock(&ifma_trash_lock
);
3076 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
3078 lck_mtx_unlock(&ifma_trash_lock
);
3079 ifma
->ifma_debug
&= ~IFD_TRASHED
;
3083 if (ifma
->ifma_addr
!= NULL
) {
3084 FREE(ifma
->ifma_addr
, M_IFADDR
);
3085 ifma
->ifma_addr
= NULL
;
3087 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
3088 zfree(ifma_zone
, ifma
);
3092 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
3094 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
3099 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
3100 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
3104 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
3105 tr
= ifma_dbg
->ifma_refhold
;
3107 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
3108 tr
= ifma_dbg
->ifma_refrele
;
3111 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
3112 ctrace_record(&tr
[idx
]);
3116 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
3121 IFMA_LOCK_ASSERT_HELD(ifma
);
3123 if (++ifma
->ifma_refcount
== 0) {
3124 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
3126 } else if (ifma
->ifma_trace
!= NULL
) {
3127 (*ifma
->ifma_trace
)(ifma
, TRUE
);
3134 ifma_remref(struct ifmultiaddr
*ifma
)
3136 struct ifmultiaddr
*ll
;
3140 if (ifma
->ifma_refcount
== 0) {
3141 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
3143 } else if (ifma
->ifma_trace
!= NULL
) {
3144 (*ifma
->ifma_trace
)(ifma
, FALSE
);
3147 --ifma
->ifma_refcount
;
3148 if (ifma
->ifma_refcount
> 0) {
3154 ifma
->ifma_ifp
= NULL
;
3155 ifma
->ifma_ll
= NULL
;
3157 ifma_free(ifma
); /* deallocate it */
3164 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
3166 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
3167 IFMA_LOCK_ASSERT_HELD(ifma
);
3169 if (ifma
->ifma_ifp
!= ifp
) {
3170 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
3171 ifma
->ifma_ifp
, ifp
);
3173 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
3174 panic("%s: Attempt to attach an already attached ifma=%p",
3177 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3178 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
3180 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
3181 panic("%s: Attempt to reattach a detached ifma=%p",
3186 ifma
->ifma_reqcnt
++;
3187 VERIFY(ifma
->ifma_reqcnt
== 1);
3188 IFMA_ADDREF_LOCKED(ifma
);
3189 ifma
->ifma_debug
|= IFD_ATTACHED
;
3191 ifma
->ifma_anoncnt
++;
3192 VERIFY(ifma
->ifma_anoncnt
== 1);
3193 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
3196 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
3200 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
3202 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
3203 IFMA_LOCK_ASSERT_HELD(ifma
);
3205 if (ifma
->ifma_reqcnt
== 0) {
3206 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
3208 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3209 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
3211 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
3212 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
3214 } else if (ifma
->ifma_ifp
!= ifp
) {
3215 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
3216 ifma
->ifma_ifp
, ifp
);
3221 --ifma
->ifma_anoncnt
;
3222 if (ifma
->ifma_anoncnt
> 0)
3224 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
3227 --ifma
->ifma_reqcnt
;
3228 if (ifma
->ifma_reqcnt
> 0)
3231 if (ifma
->ifma_protospec
!= NULL
) {
3232 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
3234 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3235 ifma
->ifma_anoncnt
!= 0) {
3236 panic("%s: Detaching ifma=%p with outstanding anon req",
3239 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
3240 panic("%s: Attempt to detach an unattached address ifma=%p",
3243 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
3244 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
3249 * NOTE: Caller calls IFMA_REMREF
3251 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
3252 LIST_REMOVE(ifma
, ifma_link
);
3253 if (LIST_EMPTY(&ifp
->if_multiaddrs
))
3254 ifp
->if_updatemcasts
= 0;
3256 if (ifma
->ifma_debug
& IFD_DEBUG
) {
3257 /* Become a regular mutex, just in case */
3258 IFMA_CONVERT_LOCK(ifma
);
3259 lck_mtx_lock(&ifma_trash_lock
);
3260 TAILQ_INSERT_TAIL(&ifma_trash_head
,
3261 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
3262 lck_mtx_unlock(&ifma_trash_lock
);
3263 ifma
->ifma_debug
|= IFD_TRASHED
;
3270 * Find an ifmultiaddr that matches a socket address on an interface.
3272 * Caller is responsible for holding the ifnet_lock while calling
3276 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3277 struct ifmultiaddr
**retifma
, int anon
)
3279 struct ifmultiaddr
*ifma
;
3281 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
3282 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
3283 IFMA_LOCK_SPIN(ifma
);
3284 if (!equal(sa
, ifma
->ifma_addr
)) {
3289 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3290 ifma
->ifma_anoncnt
!= 0);
3291 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3292 ifma
->ifma_anoncnt
== 0);
3293 ifma
->ifma_anoncnt
++;
3294 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
3295 VERIFY(ifma
->ifma_anoncnt
== 1);
3296 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
3299 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
3300 ifma
->ifma_reqcnt
++;
3301 VERIFY(ifma
->ifma_reqcnt
> 1);
3303 if (retifma
!= NULL
) {
3305 IFMA_ADDREF_LOCKED(ifma
);
3314 * Radar 3642395, make sure all multicasts are in a standard format.
3316 static struct sockaddr
*
3317 copy_and_normalize(const struct sockaddr
*original
)
3320 const u_char
*aptr
= NULL
;
3321 struct sockaddr
*copy
= NULL
;
3322 struct sockaddr_dl
*sdl_new
= NULL
;
3325 if (original
->sa_family
!= AF_LINK
&&
3326 original
->sa_family
!= AF_UNSPEC
) {
3327 /* Just make a copy */
3328 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
3329 M_IFADDR
, M_WAITOK
);
3331 bcopy(original
, copy
, original
->sa_len
);
3335 switch (original
->sa_family
) {
3337 const struct sockaddr_dl
*sdl_original
=
3338 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
3340 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
3341 sdl_original
->sdl_slen
+
3342 offsetof(struct sockaddr_dl
, sdl_data
) >
3343 sdl_original
->sdl_len
)
3346 alen
= sdl_original
->sdl_alen
;
3347 aptr
= CONST_LLADDR(sdl_original
);
3352 if (original
->sa_len
< ETHER_ADDR_LEN
+
3353 offsetof(struct sockaddr
, sa_data
)) {
3357 alen
= ETHER_ADDR_LEN
;
3358 aptr
= (const u_char
*)original
->sa_data
;
3363 if (alen
== 0 || aptr
== NULL
)
3366 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
3367 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
3369 if (sdl_new
!= NULL
) {
3370 bzero(sdl_new
, len
);
3371 sdl_new
->sdl_len
= len
;
3372 sdl_new
->sdl_family
= AF_LINK
;
3373 sdl_new
->sdl_alen
= alen
;
3374 bcopy(aptr
, LLADDR(sdl_new
), alen
);
3377 return ((struct sockaddr
*)sdl_new
);
3381 * Network-layer protocol domains which hold references to the underlying
3382 * link-layer record must use this routine.
3385 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3386 struct ifmultiaddr
**retifma
)
3388 return (if_addmulti_common(ifp
, sa
, retifma
, 0));
3392 * Anything other than network-layer protocol domains which hold references
3393 * to the underlying link-layer record must use this routine: SIOCADDMULTI
3394 * ioctl, ifnet_add_multicast(), if_bond.
3397 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3398 struct ifmultiaddr
**retifma
)
3400 return (if_addmulti_common(ifp
, sa
, retifma
, 1));
3404 * Register an additional multicast address with a network interface.
3406 * - If the address is already present, bump the reference count on the
3407 * address and return.
3408 * - If the address is not link-layer, look up a link layer address.
3409 * - Allocate address structures for one or both addresses, and attach to the
3410 * multicast address list on the interface. If automatically adding a link
3411 * layer address, the protocol address will own a reference to the link
3412 * layer address, to be freed when it is freed.
3413 * - Notify the network device driver of an addition to the multicast address
3416 * 'sa' points to caller-owned memory with the desired multicast address.
3418 * 'retifma' will be used to return a pointer to the resulting multicast
3419 * address reference, if desired.
3421 * 'anon' indicates a link-layer address with no protocol address reference
3422 * made to it. Anything other than network-layer protocol domain requests
3423 * are considered as anonymous.
3426 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
3427 struct ifmultiaddr
**retifma
, int anon
)
3429 struct sockaddr_storage storage
;
3430 struct sockaddr
*llsa
= NULL
;
3431 struct sockaddr
*dupsa
= NULL
;
3432 int error
= 0, ll_firstref
= 0, lladdr
;
3433 struct ifmultiaddr
*ifma
= NULL
;
3434 struct ifmultiaddr
*llifma
= NULL
;
3436 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
3437 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
3438 sa
->sa_family
== AF_LINK
);
3440 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
3441 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
3442 dupsa
= copy_and_normalize(sa
);
3443 if (dupsa
== NULL
) {
3450 ifnet_lock_exclusive(ifp
);
3451 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
3452 error
= EADDRNOTAVAIL
;
3453 ifnet_lock_done(ifp
);
3457 /* If the address is already present, return a new reference to it */
3458 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
3459 ifnet_lock_done(ifp
);
3464 * The address isn't already present; give the link layer a chance
3465 * to accept/reject it, and also find out which AF_LINK address this
3466 * maps to, if it isn't one already.
3468 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
3470 if (error
== 0 && storage
.ss_len
!= 0) {
3471 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
3477 llifma
= ifma_alloc(M_WAITOK
);
3478 if (llifma
== NULL
) {
3484 /* to be similar to FreeBSD */
3485 if (error
== EOPNOTSUPP
)
3487 else if (error
!= 0)
3490 /* Allocate while we aren't holding any locks */
3491 if (dupsa
== NULL
) {
3492 dupsa
= copy_and_normalize(sa
);
3493 if (dupsa
== NULL
) {
3498 ifma
= ifma_alloc(M_WAITOK
);
3504 ifnet_lock_exclusive(ifp
);
3506 * Check again for the matching multicast.
3508 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
3510 ifnet_lock_done(ifp
);
3514 if (llifma
!= NULL
) {
3515 VERIFY(!anon
); /* must not get here if "anonymous" */
3516 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
3517 FREE(llsa
, M_IFADDR
);
3521 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
3524 llifma
->ifma_addr
= llsa
;
3525 llifma
->ifma_ifp
= ifp
;
3527 if_attach_ifma(ifp
, llifma
, 0);
3528 /* add extra refcnt for ifma */
3529 IFMA_ADDREF_LOCKED(llifma
);
3530 IFMA_UNLOCK(llifma
);
3531 ifma
->ifma_ll
= llifma
;
3535 /* "anonymous" request should not result in network address */
3536 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
3538 ifma
->ifma_addr
= dupsa
;
3539 ifma
->ifma_ifp
= ifp
;
3541 if_attach_ifma(ifp
, ifma
, anon
);
3542 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
3543 if (retifma
!= NULL
) {
3545 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
3547 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
3548 ifma
->ifma_addr
->sa_family
== AF_LINK
);
3550 ifnet_lock_done(ifp
);
3552 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
3553 IFMA_REMREF(ifma
); /* for this routine */
3556 * We are certain we have added something, so call down to the
3557 * interface to let them know about it. Do this only for newly-
3558 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
3560 if (lladdr
|| ll_firstref
)
3561 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
3563 if (ifp
->if_updatemcasts
> 0)
3564 ifp
->if_updatemcasts
= 0;
3572 FREE(dupsa
, M_IFADDR
);
3576 FREE(llsa
, M_IFADDR
);
3582 * Delete a multicast group membership by network-layer group address.
3583 * This routine is deprecated.
3586 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
3588 return (if_delmulti_common(NULL
, ifp
, sa
, 0));
3592 * Delete a multicast group membership by group membership pointer.
3593 * Network-layer protocol domains must use this routine.
3596 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
3598 return (if_delmulti_common(ifma
, NULL
, NULL
, 0));
3602 * Anything other than network-layer protocol domains which hold references
3603 * to the underlying link-layer record must use this routine: SIOCDELMULTI
3604 * ioctl, ifnet_remove_multicast(), if_bond.
3607 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
3609 return (if_delmulti_common(NULL
, ifp
, sa
, 1));
3613 * Delete a multicast group membership by network-layer group address.
3615 * Returns ENOENT if the entry could not be found.
3618 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
3619 const struct sockaddr
*sa
, int anon
)
3621 struct sockaddr
*dupsa
= NULL
;
3622 int lastref
, ll_lastref
= 0, lladdr
;
3623 struct ifmultiaddr
*ll
= NULL
;
3625 /* sanity check for callers */
3626 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
3629 ifp
= ifma
->ifma_ifp
;
3632 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
3633 dupsa
= copy_and_normalize(sa
);
3639 ifnet_lock_exclusive(ifp
);
3641 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
3642 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
3644 if (!equal(sa
, ifma
->ifma_addr
) ||
3645 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
3646 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3647 ifma
->ifma_anoncnt
!= 0);
3651 /* found; keep it locked */
3656 FREE(dupsa
, M_IFADDR
);
3657 ifnet_lock_done(ifp
);
3663 IFMA_LOCK_ASSERT_HELD(ifma
);
3664 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
3665 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
3666 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
3667 ifma
->ifma_reqcnt
== 0));
3668 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
3670 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
3671 ifma
->ifma_addr
->sa_family
== AF_LINK
);
3673 if (lastref
&& ll
!= NULL
) {
3675 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
3678 ifnet_lock_done(ifp
);
3681 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
3683 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
3685 * Make sure the interface driver is notified in the
3686 * case of a link layer mcast group being left. Do
3687 * this only for a AF_LINK/AF_UNSPEC address that has
3688 * been removed from the if_multiaddrs set.
3690 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
3694 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
3696 IFMA_REMREF(ll
); /* for if_multiaddrs list */
3698 IFMA_REMREF(ifma
); /* for this routine */
3700 FREE(dupsa
, M_IFADDR
);
3706 * Shutdown all network activity. Used boot() when halting
3716 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
3717 for (i
= 0; i
< count
; i
++) {
3719 dlil_proto_unplumb_all(ifp
[i
]);
3721 ifnet_list_free(ifp
);
3728 * Delete Routes for a Network Interface
3730 * Called for each routing entry via the rnh->rnh_walktree() call above
3731 * to delete all route entries referencing a detaching network interface.
3734 * rn pointer to node in the routing table
3735 * arg argument passed to rnh->rnh_walktree() - detaching interface
3739 * errno failed - reason indicated
3743 if_rtdel(struct radix_node
*rn
, void *arg
)
3745 struct rtentry
*rt
= (struct rtentry
*)rn
;
3746 struct ifnet
*ifp
= arg
;
3752 * Checking against RTF_UP protects against walktree
3753 * recursion problems with cloned routes.
3756 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
3758 * Safe to drop rt_lock and use rt_key, rt_gateway,
3759 * since holding rnh_lock here prevents another thread
3760 * from calling rt_setgate() on this route.
3763 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
3764 rt_mask(rt
), rt
->rt_flags
, NULL
);
3766 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
3775 * Removes routing table reference to a given interface
3776 * for a given protocol family
3779 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
3781 struct radix_node_head
*rnh
;
3783 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
3784 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
3785 lck_mtx_lock(rnh_lock
);
3786 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
3787 lck_mtx_unlock(rnh_lock
);
3792 if_rtmtu(struct radix_node
*rn
, void *arg
)
3794 struct rtentry
*rt
= (struct rtentry
*)rn
;
3795 struct ifnet
*ifp
= arg
;
3798 if (rt
->rt_ifp
== ifp
) {
3800 * Update the MTU of this entry only if the MTU
3801 * has not been locked (RTV_MTU is not set) and
3802 * if it was non-zero to begin with.
3804 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
3805 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
3813 * Update the MTU metric of all route entries in all protocol tables
3814 * associated with a particular interface; this is called when the
3815 * MTU of that interface has changed.
3818 void if_rtmtu_update(struct ifnet
*ifp
)
3820 struct radix_node_head
*rnh
;
3823 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
3824 if ((rnh
= rt_tables
[p
]) == NULL
)
3827 lck_mtx_lock(rnh_lock
);
3828 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
3829 lck_mtx_unlock(rnh_lock
);
3831 routegenid_update();
3834 __private_extern__
void
3835 if_data_internal_to_if_data(struct ifnet
*ifp
,
3836 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
3839 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
3840 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
3841 /* compiler will cast down to 32-bit */
3842 #define COPYFIELD32_ATOMIC(fld) do { \
3843 atomic_get_64(if_data->fld, \
3844 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
3847 COPYFIELD(ifi_type
);
3848 COPYFIELD(ifi_typelen
);
3849 COPYFIELD(ifi_physical
);
3850 COPYFIELD(ifi_addrlen
);
3851 COPYFIELD(ifi_hdrlen
);
3852 COPYFIELD(ifi_recvquota
);
3853 COPYFIELD(ifi_xmitquota
);
3854 if_data
->ifi_unused1
= 0;
3856 COPYFIELD(ifi_metric
);
3857 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
3858 if_data
->ifi_baudrate
= 0xFFFFFFFF;
3860 COPYFIELD32(ifi_baudrate
);
3863 COPYFIELD32_ATOMIC(ifi_ipackets
);
3864 COPYFIELD32_ATOMIC(ifi_ierrors
);
3865 COPYFIELD32_ATOMIC(ifi_opackets
);
3866 COPYFIELD32_ATOMIC(ifi_oerrors
);
3867 COPYFIELD32_ATOMIC(ifi_collisions
);
3868 COPYFIELD32_ATOMIC(ifi_ibytes
);
3869 COPYFIELD32_ATOMIC(ifi_obytes
);
3870 COPYFIELD32_ATOMIC(ifi_imcasts
);
3871 COPYFIELD32_ATOMIC(ifi_omcasts
);
3872 COPYFIELD32_ATOMIC(ifi_iqdrops
);
3873 COPYFIELD32_ATOMIC(ifi_noproto
);
3875 COPYFIELD(ifi_recvtiming
);
3876 COPYFIELD(ifi_xmittiming
);
3878 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
3879 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
3881 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
3883 if_data
->ifi_unused2
= 0;
3884 COPYFIELD(ifi_hwassist
);
3885 if_data
->ifi_reserved1
= 0;
3886 if_data
->ifi_reserved2
= 0;
3887 #undef COPYFIELD32_ATOMIC
3892 __private_extern__
void
3893 if_data_internal_to_if_data64(struct ifnet
*ifp
,
3894 const struct if_data_internal
*if_data_int
,
3895 struct if_data64
*if_data64
)
3898 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
3899 #define COPYFIELD64_ATOMIC(fld) do { \
3900 atomic_get_64(if_data64->fld, \
3901 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
3904 COPYFIELD64(ifi_type
);
3905 COPYFIELD64(ifi_typelen
);
3906 COPYFIELD64(ifi_physical
);
3907 COPYFIELD64(ifi_addrlen
);
3908 COPYFIELD64(ifi_hdrlen
);
3909 COPYFIELD64(ifi_recvquota
);
3910 COPYFIELD64(ifi_xmitquota
);
3911 if_data64
->ifi_unused1
= 0;
3912 COPYFIELD64(ifi_mtu
);
3913 COPYFIELD64(ifi_metric
);
3914 COPYFIELD64(ifi_baudrate
);
3916 COPYFIELD64_ATOMIC(ifi_ipackets
);
3917 COPYFIELD64_ATOMIC(ifi_ierrors
);
3918 COPYFIELD64_ATOMIC(ifi_opackets
);
3919 COPYFIELD64_ATOMIC(ifi_oerrors
);
3920 COPYFIELD64_ATOMIC(ifi_collisions
);
3921 COPYFIELD64_ATOMIC(ifi_ibytes
);
3922 COPYFIELD64_ATOMIC(ifi_obytes
);
3923 COPYFIELD64_ATOMIC(ifi_imcasts
);
3924 COPYFIELD64_ATOMIC(ifi_omcasts
);
3925 COPYFIELD64_ATOMIC(ifi_iqdrops
);
3926 COPYFIELD64_ATOMIC(ifi_noproto
);
3928 /* Note these two fields are actually 32 bit, so doing COPYFIELD64_ATOMIC will
3929 * cause them to be misaligned
3931 COPYFIELD64(ifi_recvtiming
);
3932 COPYFIELD64(ifi_xmittiming
);
3934 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
3935 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
3937 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
3942 __private_extern__
void
3943 if_copy_traffic_class(struct ifnet
*ifp
,
3944 struct if_traffic_class
*if_tc
)
3946 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
3947 atomic_get_64(if_tc->fld, \
3948 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
3951 bzero(if_tc
, sizeof (*if_tc
));
3952 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
3953 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
3954 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
3955 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
3956 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
3957 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
3958 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
3959 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
3960 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
3961 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
3962 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
3963 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
3964 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
3965 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
3966 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
3967 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
3968 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
3969 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
3970 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
3971 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
3973 #undef COPY_IF_TC_FIELD64_ATOMIC
3977 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
3979 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
3980 atomic_get_64(if_de->fld, \
3981 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
3984 bzero(if_de
, sizeof (*if_de
));
3985 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
3986 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes
);
3987 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets
);
3988 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes
);
3990 #undef COPY_IF_DE_FIELD64_ATOMIC
3994 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
3996 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
3997 atomic_get_64(if_ps->ifi_tcp_##fld, \
3998 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
4001 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
4002 atomic_get_64(if_ps->ifi_udp_##fld, \
4003 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
4006 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
4007 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
4008 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
4009 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
4010 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
4011 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
4012 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
4013 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
4014 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
4015 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
4016 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
4017 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
4018 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
4019 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
4021 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
4022 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
4023 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
4024 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
4025 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
4026 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
4027 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
4028 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
4030 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
4031 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
4035 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
4037 bzero(if_rs
, sizeof (*if_rs
));
4038 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1))
4041 /* by now, ifnet will stay attached so if_inp must be valid */
4042 VERIFY(ifp
->if_inp
!= NULL
);
4043 bcopy(&ifp
->if_inp
->pstats
, if_rs
, sizeof (*if_rs
));
4045 /* Release the IO refcnt */
4046 ifnet_decr_iorefcnt(ifp
);
4050 ifa_remref(struct ifaddr
*ifa
, int locked
)
4055 IFA_LOCK_ASSERT_HELD(ifa
);
4057 if (ifa
->ifa_refcnt
== 0)
4058 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
4059 else if (ifa
->ifa_trace
!= NULL
)
4060 (*ifa
->ifa_trace
)(ifa
, FALSE
);
4061 if (--ifa
->ifa_refcnt
== 0) {
4062 if (ifa
->ifa_debug
& IFD_ATTACHED
)
4063 panic("ifa %p attached to ifp is being freed\n", ifa
);
4065 * Some interface addresses are allocated either statically
4066 * or carved out of a larger block. Only free it if it was
4067 * allocated via MALLOC or via the corresponding per-address
4068 * family allocator. Otherwise, leave it alone.
4070 if (ifa
->ifa_debug
& IFD_ALLOC
) {
4071 if (ifa
->ifa_free
== NULL
) {
4073 FREE(ifa
, M_IFADDR
);
4075 /* Become a regular mutex */
4076 IFA_CONVERT_LOCK(ifa
);
4077 /* callee will unlock */
4078 (*ifa
->ifa_free
)(ifa
);
4086 if (!locked
&& ifa
!= NULL
)
4093 ifa_addref(struct ifaddr
*ifa
, int locked
)
4098 IFA_LOCK_ASSERT_HELD(ifa
);
4100 if (++ifa
->ifa_refcnt
== 0) {
4101 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
4103 } else if (ifa
->ifa_trace
!= NULL
) {
4104 (*ifa
->ifa_trace
)(ifa
, TRUE
);
4111 ifa_lock_init(struct ifaddr
*ifa
)
4113 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4117 ifa_lock_destroy(struct ifaddr
*ifa
)
4119 IFA_LOCK_ASSERT_NOTHELD(ifa
);
4120 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);
4126 * The switch statement below does nothing at runtime, as it serves as a
4127 * compile time check to ensure that all of the socket 'i' ioctls (those
4128 * in the 'i' group going thru soo_ioctl) that are made available by the
4129 * networking stack is unique. This works as long as this routine gets
4130 * updated each time a new interface ioctl gets added.
4132 * Any failures at compile time indicates duplicated ioctl values.
4134 static __attribute__((unused
)) void
4135 ifioctl_cassert(void)
4138 * This is equivalent to _CASSERT() and the compiler wouldn't
4139 * generate any instructions, thus for compile time only.
4141 switch ((u_long
)0) {
4144 /* bsd/net/if_ppp.h */
4146 case SIOCGPPPCSTATS
:
4149 /* bsd/netinet6/in6_var.h */
4150 case SIOCSIFADDR_IN6
:
4151 case SIOCGIFADDR_IN6
:
4152 case SIOCSIFDSTADDR_IN6
:
4153 case SIOCSIFNETMASK_IN6
:
4154 case SIOCGIFDSTADDR_IN6
:
4155 case SIOCGIFNETMASK_IN6
:
4156 case SIOCDIFADDR_IN6
:
4157 case SIOCAIFADDR_IN6_32
:
4158 case SIOCAIFADDR_IN6_64
:
4159 case SIOCSIFPHYADDR_IN6_32
:
4160 case SIOCSIFPHYADDR_IN6_64
:
4161 case SIOCGIFPSRCADDR_IN6
:
4162 case SIOCGIFPDSTADDR_IN6
:
4163 case SIOCGIFAFLAG_IN6
:
4164 case SIOCGDRLST_IN6_32
:
4165 case SIOCGDRLST_IN6_64
:
4166 case SIOCGPRLST_IN6_32
:
4167 case SIOCGPRLST_IN6_64
:
4168 case OSIOCGIFINFO_IN6
:
4169 case SIOCGIFINFO_IN6
:
4170 case SIOCSNDFLUSH_IN6
:
4171 case SIOCGNBRINFO_IN6_32
:
4172 case SIOCGNBRINFO_IN6_64
:
4173 case SIOCSPFXFLUSH_IN6
:
4174 case SIOCSRTRFLUSH_IN6
:
4175 case SIOCGIFALIFETIME_IN6
:
4176 case SIOCSIFALIFETIME_IN6
:
4177 case SIOCGIFSTAT_IN6
:
4178 case SIOCGIFSTAT_ICMP6
:
4179 case SIOCSDEFIFACE_IN6_32
:
4180 case SIOCSDEFIFACE_IN6_64
:
4181 case SIOCGDEFIFACE_IN6_32
:
4182 case SIOCGDEFIFACE_IN6_64
:
4183 case SIOCSIFINFO_FLAGS
:
4186 case SIOCGSCOPE6DEF
:
4187 case SIOCSIFPREFIX_IN6
:
4188 case SIOCGIFPREFIX_IN6
:
4189 case SIOCDIFPREFIX_IN6
:
4190 case SIOCAIFPREFIX_IN6
:
4191 case SIOCCIFPREFIX_IN6
:
4192 case SIOCSGIFPREFIX_IN6
:
4193 case SIOCPROTOATTACH_IN6_32
:
4194 case SIOCPROTOATTACH_IN6_64
:
4195 case SIOCPROTODETACH_IN6
:
4196 case SIOCLL_START_32
:
4197 case SIOCLL_START_64
:
4199 case SIOCAUTOCONF_START
:
4200 case SIOCAUTOCONF_STOP
:
4201 case SIOCSETROUTERMODE_IN6
:
4202 case SIOCLL_CGASTART_32
:
4203 case SIOCLL_CGASTART_64
:
4206 /* bsd/sys/sockio.h */
4209 case SIOCSIFDSTADDR
:
4210 case OSIOCGIFDSTADDR
:
4213 case OSIOCGIFBRDADDR
:
4214 case SIOCSIFBRDADDR
:
4215 case OSIOCGIFCONF32
:
4216 case OSIOCGIFCONF64
:
4217 case OSIOCGIFNETMASK
:
4218 case SIOCSIFNETMASK
:
4224 case SIOCGIFDSTADDR
:
4225 case SIOCGIFBRDADDR
:
4228 case SIOCGIFNETMASK
:
4230 case SIOCAUTONETMASK
:
4239 case SIOCGIFMEDIA32
:
4240 case SIOCGIFMEDIA64
:
4241 case SIOCSIFGENERIC
:
4242 case SIOCGIFGENERIC
:
4246 case SIOCSIFPHYADDR
:
4247 case SIOCGIFPSRCADDR
:
4248 case SIOCGIFPDSTADDR
:
4249 case SIOCDIFPHYADDR
:
4255 case SIOCPROTOATTACH
:
4256 case SIOCPROTODETACH
:
4262 case SIOCSDRVSPEC32
:
4263 case SIOCGDRVSPEC32
:
4264 case SIOCSDRVSPEC64
:
4265 case SIOCGDRVSPEC64
:
4268 case SIOCIFGCLONERS32
:
4269 case SIOCIFGCLONERS64
:
4270 case SIOCGIFASYNCMAP
:
4271 case SIOCSIFASYNCMAP
:
4275 #endif /* CONFIG_MACF_NET */
4278 case SIOCGIFWAKEFLAGS
:
4279 case SIOCGIFGETRTREFCNT
:
4280 case SIOCGIFLINKQUALITYMETRIC
:
4281 case SIOCSIFOPPORTUNISTIC
:
4282 case SIOCGIFOPPORTUNISTIC
:
4283 case SIOCSETROUTERMODE
:
4287 case SIOCSIFLINKPARAMS
:
4288 case SIOCGIFLINKPARAMS
:
4289 case SIOCGIFQUEUESTATS
:
4290 case SIOCSIFTHROTTLE
:
4291 case SIOCGIFTHROTTLE
:
4294 case SIOCGIFDELEGATE
:
4297 case SIOCGIFFUNCTIONALTYPE
:
4298 case SIOCAIFAGENTID
:
4299 case SIOCDIFAGENTID
:
4300 case SIOCGIFAGENTIDS32
:
4301 case SIOCGIFAGENTIDS64
:
4302 case SIOCGIFAGENTDATA32
:
4303 case SIOCGIFAGENTDATA64
:
4304 case SIOCSIFINTERFACESTATE
:
4305 case SIOCGIFINTERFACESTATE
: