2 * Copyright (c) 2000-2012 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 <kern/zalloc.h>
87 #include <machine/endian.h>
89 #include <pexpert/pexpert.h>
92 #include <net/if_arp.h>
93 #include <net/if_dl.h>
94 #include <net/if_types.h>
95 #include <net/if_var.h>
96 #include <net/net_osdep.h>
97 #include <net/ethernet.h>
99 #include <net/radix.h>
100 #include <net/route.h>
102 #include <net/dlil.h>
103 //#include <string.h>
104 #include <sys/domain.h>
105 #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>
127 #include <security/mac_framework.h>
131 #include <net/altq/if_altq.h>
132 #endif /* !PF_ALTQ */
135 * System initialization
138 /* Lock group and attribute for ifaddr lock */
139 lck_attr_t
*ifa_mtx_attr
;
140 lck_grp_t
*ifa_mtx_grp
;
141 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
143 static int ifioctl_ifreq(struct socket
*, u_long
, struct ifreq
*,
145 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
146 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
147 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
149 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
150 struct ifmultiaddr
**, int);
151 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
152 const struct sockaddr
*, int);
154 static int if_rtmtu(struct radix_node
*, void *);
155 static void if_rtmtu_update(struct ifnet
*);
158 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
159 #endif /* IF_CLONE_LIST */
161 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
163 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
165 static int if_cloners_count
;
166 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
168 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
170 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
171 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
173 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
174 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
176 static struct ifmultiaddr
*ifma_alloc(int);
177 static void ifma_free(struct ifmultiaddr
*);
178 static void ifma_trace(struct ifmultiaddr
*, int);
181 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
183 static unsigned int ifma_debug
; /* debugging (disabled) */
185 static unsigned int ifma_size
; /* size of zone element */
186 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
188 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
191 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
193 struct ifmultiaddr_dbg
{
194 struct ifmultiaddr ifma
; /* ifmultiaddr */
195 u_int16_t ifma_refhold_cnt
; /* # of ref */
196 u_int16_t ifma_refrele_cnt
; /* # of rele */
198 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
200 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
201 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
205 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
208 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
209 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
210 static decl_lck_mtx_data(, ifma_trash_lock
);
212 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
213 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
217 * XXX: declare here to avoid to include many inet6 related files..
218 * should be more generalized?
220 extern void nd6_setmtu(struct ifnet
*);
221 extern lck_mtx_t
*nd6_mutex
;
228 /* Setup lock group and attribute for ifaddr */
229 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
230 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
231 ifa_mtx_attr
= lck_attr_alloc_init();
233 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof (ifma_debug
));
235 ifma_size
= (ifma_debug
== 0) ? sizeof (struct ifmultiaddr
) :
236 sizeof (struct ifmultiaddr_dbg
);
238 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
240 if (ifma_zone
== NULL
) {
241 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
244 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
245 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
247 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
248 TAILQ_INIT(&ifma_trash_head
);
252 * Network interface utility routines.
254 * Routines with ifa_ifwith* names take sockaddr *'s as
259 struct ifaddr
**ifnet_addrs
;
260 struct ifnet
**ifindex2ifnet
;
262 __private_extern__
void
263 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
265 if_attach_ifa_common(ifp
, ifa
, 0);
268 __private_extern__
void
269 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
271 if_attach_ifa_common(ifp
, ifa
, 1);
275 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
277 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
278 IFA_LOCK_ASSERT_HELD(ifa
);
280 if (ifa
->ifa_ifp
!= ifp
) {
281 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
284 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
285 panic("%s: Attempt to attach an already attached ifa=%p",
288 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
289 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
291 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
292 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
295 IFA_ADDREF_LOCKED(ifa
);
296 ifa
->ifa_debug
|= IFD_ATTACHED
;
298 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
300 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
302 if (ifa
->ifa_attached
!= NULL
)
303 (*ifa
->ifa_attached
)(ifa
);
306 __private_extern__
void
307 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
309 if_detach_ifa_common(ifp
, ifa
, 0);
312 __private_extern__
void
313 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
315 if_detach_ifa_common(ifp
, ifa
, 1);
319 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
321 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
322 IFA_LOCK_ASSERT_HELD(ifa
);
324 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
325 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
327 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
328 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
330 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
331 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
333 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
334 panic("%s: Attempt to detach an unattached address ifa=%p",
337 } else if (ifa
->ifa_ifp
!= ifp
) {
338 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
341 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
343 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
348 panic("%s: Attempt to detach a stray address ifa=%p",
353 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
354 /* This must not be the last reference to the ifaddr */
355 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
356 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
359 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
361 if (ifa
->ifa_detached
!= NULL
)
362 (*ifa
->ifa_detached
)(ifa
);
365 #define INITIAL_IF_INDEXLIM 8
368 * Function: if_next_index
370 * Return the next available interface index.
371 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
372 * added entry when necessary.
375 * ifnet_addrs[] is indexed by (if_index - 1), whereas
376 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
377 * always allocate one extra element to hold ifindex2ifnet[0], which
380 int if_next_index(void);
382 __private_extern__
int
385 static int if_indexlim
= 0;
388 new_index
= ++if_index
;
389 if (if_index
> if_indexlim
) {
392 caddr_t new_ifnet_addrs
;
393 caddr_t new_ifindex2ifnet
;
394 caddr_t old_ifnet_addrs
;
396 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
397 if (ifnet_addrs
== NULL
) {
398 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
400 new_if_indexlim
= if_indexlim
<< 1;
403 /* allocate space for the larger arrays */
404 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
405 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
406 if (new_ifnet_addrs
== NULL
) {
411 new_ifindex2ifnet
= new_ifnet_addrs
412 + new_if_indexlim
* sizeof(caddr_t
);
413 bzero(new_ifnet_addrs
, n
);
414 if (ifnet_addrs
!= NULL
) {
415 /* copy the existing data */
416 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
417 if_indexlim
* sizeof(caddr_t
));
418 bcopy((caddr_t
)ifindex2ifnet
,
420 (if_indexlim
+ 1) * sizeof(caddr_t
));
423 /* switch to the new tables and size */
424 ifnet_addrs
= (struct ifaddr
**)(void *)new_ifnet_addrs
;
425 ifindex2ifnet
= (struct ifnet
**)(void *)new_ifindex2ifnet
;
426 if_indexlim
= new_if_indexlim
;
428 /* release the old data */
429 if (old_ifnet_addrs
!= NULL
) {
430 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
437 * Create a clone network interface.
440 if_clone_create(char *name
, int len
, void *params
)
442 struct if_clone
*ifc
;
445 u_int32_t bytoff
, bitoff
;
449 ifc
= if_clone_lookup(name
, &unit
);
453 if (ifunit(name
) != NULL
)
457 wildcard
= (unit
== UINT32_MAX
);
459 * Find a free unit if none was given.
462 while ((bytoff
< ifc
->ifc_bmlen
)
463 && (ifc
->ifc_units
[bytoff
] == 0xff))
465 if (bytoff
>= ifc
->ifc_bmlen
)
467 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
469 unit
= (bytoff
<< 3) + bitoff
;
472 if (unit
> ifc
->ifc_maxunit
)
475 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
481 bitoff
= unit
- (bytoff
<< 3);
485 * Allocate the unit in the bitmap.
487 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
488 ("%s: bit is already set", __func__
));
489 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
491 /* In the wildcard case, we need to update the name. */
493 for (dp
= name
; *dp
!= '\0'; dp
++);
494 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
495 len
- (dp
-name
) - 1) {
497 * This can only be a programmer error and
498 * there's no straightforward way to recover if
501 panic("%s: interface name too long", __func__
);
511 * Destroy a clone network interface.
514 if_clone_destroy(const char *name
)
516 struct if_clone
*ifc
;
521 ifc
= if_clone_lookup(name
, &unit
);
525 if (unit
< ifc
->ifc_minifs
)
532 if (ifc
->ifc_destroy
== NULL
)
535 (*ifc
->ifc_destroy
)(ifp
);
538 * Compute offset in the bitmap and deallocate the unit.
541 bitoff
= unit
- (bytoff
<< 3);
542 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
543 ("%s: bit is already cleared", __func__
));
544 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
549 * Look up a network interface cloner.
552 __private_extern__
struct if_clone
*
553 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
555 struct if_clone
*ifc
;
559 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
560 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
561 if (ifc
->ifc_name
[i
] != *cp
)
566 ifc
= LIST_NEXT(ifc
, ifc_list
);
570 return ((struct if_clone
*)NULL
);
576 for (i
= 0; *cp
!= '\0'; cp
++) {
577 if (*cp
< '0' || *cp
> '9') {
578 /* Bogus unit number. */
581 i
= (i
* 10) + (*cp
- '0');
591 * Register a network interface cloner.
594 if_clone_attach(struct if_clone
*ifc
)
601 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
602 ("%s: %s requested more units then allowed (%d > %d)",
603 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
604 ifc
->ifc_maxunit
+ 1));
606 * Compute bitmap size and allocate it.
608 maxclone
= ifc
->ifc_maxunit
+ 1;
610 if ((len
<< 3) < maxclone
)
612 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
613 if (ifc
->ifc_units
== NULL
)
615 bzero(ifc
->ifc_units
, len
);
616 ifc
->ifc_bmlen
= len
;
618 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
621 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
622 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
624 ("%s: failed to create required interface %s%d",
625 __func__
, ifc
->ifc_name
, unit
));
627 /* Allocate the unit in the bitmap. */
629 bitoff
= unit
- (bytoff
<< 3);
630 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
637 * Unregister a network interface cloner.
640 if_clone_detach(struct if_clone
*ifc
)
643 LIST_REMOVE(ifc
, ifc_list
);
644 FREE(ifc
->ifc_units
, M_CLONE
);
650 * Provide list of interface cloners to userspace.
653 if_clone_list(int count
, int * total
, user_addr_t dst
)
655 char outbuf
[IFNAMSIZ
];
656 struct if_clone
*ifc
;
659 *total
= if_cloners_count
;
660 if (dst
== USER_ADDR_NULL
) {
661 /* Just asking how many there are. */
668 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
670 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
671 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
672 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
673 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
680 #endif /* IF_CLONE_LIST */
683 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
684 * and that it matches only the local (not broadcast) address.
686 __private_extern__
struct in_ifaddr
*
687 ifa_foraddr(unsigned int addr
)
689 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
693 * Similar to ifa_foraddr, except with the added interface scope
694 * constraint (unless the caller passes in IFSCOPE_NONE in which
695 * case there is no scope restriction).
697 __private_extern__
struct in_ifaddr
*
698 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
700 struct in_ifaddr
*ia
= NULL
;
702 lck_rw_lock_shared(in_ifaddr_rwlock
);
703 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
704 IFA_LOCK_SPIN(&ia
->ia_ifa
);
705 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
706 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
707 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
708 IFA_UNLOCK(&ia
->ia_ifa
);
711 IFA_UNLOCK(&ia
->ia_ifa
);
713 lck_rw_done(in_ifaddr_rwlock
);
719 * Similar to ifa_foraddr, except that this for IPv6.
721 __private_extern__
struct in6_ifaddr
*
722 ifa_foraddr6(struct in6_addr
*addr6
)
724 return (ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
));
727 __private_extern__
struct in6_ifaddr
*
728 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
730 struct in6_ifaddr
*ia
= NULL
;
732 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
733 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
734 IFA_LOCK(&ia
->ia_ifa
);
735 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
736 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
737 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
738 IFA_UNLOCK(&ia
->ia_ifa
);
741 IFA_UNLOCK(&ia
->ia_ifa
);
743 lck_rw_done(&in6_ifaddr_rwlock
);
750 * Return the first (primary) address of a given family on an interface.
752 __private_extern__
struct ifaddr
*
753 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
757 ifnet_lock_shared(ifp
);
758 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
760 if (ifa
->ifa_addr
->sa_family
== family
) {
761 IFA_ADDREF_LOCKED(ifa
); /* for caller */
767 ifnet_lock_done(ifp
);
773 * Locate an interface based on a complete address.
777 ifa_ifwithaddr(const struct sockaddr
*addr
)
781 struct ifaddr
*result
= NULL
;
783 #define equal(a1, a2) \
784 (bcmp((const void*)(a1), (const void*)(a2), \
785 ((const struct sockaddr *)(a1))->sa_len) == 0)
787 ifnet_head_lock_shared();
788 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
789 ifp
= ifp
->if_link
.tqe_next
) {
790 ifnet_lock_shared(ifp
);
791 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
792 ifa
= ifa
->ifa_link
.tqe_next
) {
794 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
798 if (equal(addr
, ifa
->ifa_addr
)) {
800 IFA_ADDREF_LOCKED(ifa
); /* for caller */
804 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
805 ifa
->ifa_broadaddr
!= NULL
&&
806 /* IP6 doesn't have broadcast */
807 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
808 equal(ifa
->ifa_broadaddr
, addr
)) {
810 IFA_ADDREF_LOCKED(ifa
); /* for caller */
816 ifnet_lock_done(ifp
);
823 * Locate the point to point interface with a given destination address.
827 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
831 struct ifaddr
*result
= NULL
;
833 ifnet_head_lock_shared();
834 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
835 ifp
= ifp
->if_link
.tqe_next
) {
836 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
837 ifnet_lock_shared(ifp
);
838 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
839 ifa
= ifa
->ifa_link
.tqe_next
) {
841 if (ifa
->ifa_addr
->sa_family
!=
846 if (ifa
->ifa_dstaddr
&&
847 equal(addr
, ifa
->ifa_dstaddr
)) {
849 IFA_ADDREF_LOCKED(ifa
); /* for caller */
855 ifnet_lock_done(ifp
);
863 * Locate the source address of an interface based on a complete address.
866 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
868 struct ifaddr
*result
= NULL
;
871 if (ifscope
== IFSCOPE_NONE
)
872 return (ifa_ifwithaddr(addr
));
874 ifnet_head_lock_shared();
875 if (ifscope
> (unsigned int)if_index
) {
880 ifp
= ifindex2ifnet
[ifscope
];
882 struct ifaddr
*ifa
= NULL
;
885 * This is suboptimal; there should be a better way
886 * to search for a given address of an interface
887 * for any given address family.
889 ifnet_lock_shared(ifp
);
890 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
891 ifa
= ifa
->ifa_link
.tqe_next
) {
893 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
897 if (equal(addr
, ifa
->ifa_addr
)) {
899 IFA_ADDREF_LOCKED(ifa
); /* for caller */
903 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
904 ifa
->ifa_broadaddr
!= NULL
&&
905 /* IP6 doesn't have broadcast */
906 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
907 equal(ifa
->ifa_broadaddr
, addr
)) {
909 IFA_ADDREF_LOCKED(ifa
); /* for caller */
915 ifnet_lock_done(ifp
);
923 ifa_ifwithnet(const struct sockaddr
*addr
)
925 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
929 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
931 return (ifa_ifwithnet_common(addr
, ifscope
));
935 * Find an interface on a specific network. If many, choice
936 * is most specific found.
938 static struct ifaddr
*
939 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
942 struct ifaddr
*ifa
= NULL
;
943 struct ifaddr
*ifa_maybe
= NULL
;
944 u_int af
= addr
->sa_family
;
945 const char *addr_data
= addr
->sa_data
, *cplim
;
948 if ((af
!= AF_INET
&& af
!= AF_INET6
) ||
949 (af
== AF_INET
&& !ip_doscopedroute
) ||
950 (af
== AF_INET6
&& !ip6_doscopedroute
))
952 if (af
!= AF_INET
|| !ip_doscopedroute
)
954 ifscope
= IFSCOPE_NONE
;
956 ifnet_head_lock_shared();
958 * AF_LINK addresses can be looked up directly by their index number,
959 * so do that if we can.
962 const struct sockaddr_dl
*sdl
=
963 (const struct sockaddr_dl
*)(uintptr_t)(size_t)addr
;
964 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
965 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
975 * Scan though each interface, looking for ones that have
976 * addresses in this address family.
978 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
979 ifnet_lock_shared(ifp
);
980 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
981 ifa
= ifa
->ifa_link
.tqe_next
) {
982 const char *cp
, *cp2
, *cp3
;
985 if (ifa
->ifa_addr
== NULL
||
986 ifa
->ifa_addr
->sa_family
!= af
) {
992 /* This breaks tunneling application trying to install a route with
993 * a specific subnet and the local address as the destination
994 * It's breaks binary compatibility with previous version of MacOS X
997 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
998 addr
->sa_family
!= AF_INET6
&&
1000 ifp
->if_flags
& IFF_POINTOPOINT
) {
1002 * This is a bit broken as it doesn't
1003 * take into account that the remote end may
1004 * be a single node in the network we are
1006 * The trouble is that we don't know the
1007 * netmask for the remote end.
1009 if (ifa
->ifa_dstaddr
!= 0 &&
1010 equal(addr
, ifa
->ifa_dstaddr
)) {
1011 IFA_ADDREF_LOCKED(ifa
);
1017 #endif /* __APPLE__*/
1020 * If we're looking up with a scope,
1021 * find using a matching interface.
1023 if (ifscope
!= IFSCOPE_NONE
&&
1024 ifp
->if_index
!= ifscope
) {
1030 * Scan all the bits in the ifa's address.
1031 * If a bit dissagrees with what we are
1032 * looking for, mask it with the netmask
1033 * to see if it really matters.
1034 * (A byte at a time)
1036 if (ifa
->ifa_netmask
== 0) {
1041 cp2
= ifa
->ifa_addr
->sa_data
;
1042 cp3
= ifa
->ifa_netmask
->sa_data
;
1043 cplim
= ifa
->ifa_netmask
->sa_len
1044 + (char *)ifa
->ifa_netmask
;
1046 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1047 goto next
; /* next address! */
1049 * If the netmask of what we just found
1050 * is more specific than what we had before
1051 * (if we had one) then remember the new one
1052 * before continuing to search
1053 * for an even better one.
1055 if (ifa_maybe
== NULL
||
1056 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1057 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1058 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1060 if (ifa_maybe
!= NULL
)
1061 IFA_REMREF(ifa_maybe
);
1067 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1069 ifnet_lock_done(ifp
);
1078 else if (ifa_maybe
!= NULL
)
1079 IFA_REMREF(ifa_maybe
);
1085 * Find an interface address specific to an interface best matching
1089 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1091 struct ifaddr
*ifa
= NULL
;
1092 const char *cp
, *cp2
, *cp3
;
1094 struct ifaddr
*ifa_maybe
= NULL
;
1095 struct ifaddr
*better_ifa_maybe
= NULL
;
1096 u_int af
= addr
->sa_family
;
1101 ifnet_lock_shared(ifp
);
1102 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1103 ifa
= ifa
->ifa_link
.tqe_next
) {
1105 if (ifa
->ifa_addr
->sa_family
!= af
) {
1109 if (ifa_maybe
== NULL
) {
1110 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1113 if (ifa
->ifa_netmask
== 0) {
1114 if (equal(addr
, ifa
->ifa_addr
) || (ifa
->ifa_dstaddr
&&
1115 equal(addr
, ifa
->ifa_dstaddr
))) {
1116 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1123 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1124 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
1125 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1130 if (equal(addr
, ifa
->ifa_addr
)) {
1132 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1137 cp2
= ifa
->ifa_addr
->sa_data
;
1138 cp3
= ifa
->ifa_netmask
->sa_data
;
1139 cplim
= ifa
->ifa_netmask
->sa_len
+
1140 (char *)ifa
->ifa_netmask
;
1141 for (; cp3
< cplim
; cp3
++)
1142 if ((*cp
++ ^ *cp2
++) & *cp3
)
1146 if (better_ifa_maybe
== NULL
) {
1147 /* for better_ifa_maybe */
1148 IFA_ADDREF_LOCKED(ifa
);
1149 better_ifa_maybe
= ifa
;
1157 if (better_ifa_maybe
!= NULL
) {
1158 ifa
= better_ifa_maybe
;
1159 better_ifa_maybe
= NULL
;
1166 ifnet_lock_done(ifp
);
1168 if (better_ifa_maybe
!= NULL
)
1169 IFA_REMREF(better_ifa_maybe
);
1170 if (ifa_maybe
!= NULL
)
1171 IFA_REMREF(ifa_maybe
);
1176 #include <net/route.h>
1179 * Default action when installing a route with a Link Level gateway.
1180 * Lookup an appropriate real ifa to point to.
1181 * This should be moved to /sys/net/link.c eventually.
1184 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1187 struct sockaddr
*dst
;
1189 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1191 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1192 RT_LOCK_ASSERT_HELD(rt
);
1194 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1195 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
1198 /* Become a regular mutex, just in case */
1199 RT_CONVERT_LOCK(rt
);
1201 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1205 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1207 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
)
1208 ifa_rtrequest(cmd
, rt
, sa
);
1214 * if_updown will set the interface up or down. It will
1215 * prevent other up/down events from occurring until this
1216 * up/down event has completed.
1218 * Caller must lock ifnet. This function will drop the
1219 * lock. This allows ifnet_set_flags to set the rest of
1220 * the flags after we change the up/down state without
1221 * dropping the interface lock between setting the
1222 * up/down state and updating the rest of the flags.
1224 __private_extern__
void
1230 struct ifaddr
**ifa
;
1232 struct ifclassq
*ifq
= &ifp
->if_snd
;
1234 /* Wait until no one else is changing the up/down state */
1235 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1237 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1238 ifnet_lock_done(ifp
);
1239 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1240 ifnet_lock_exclusive(ifp
);
1243 /* Verify that the interface isn't already in the right state */
1244 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1245 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1249 /* Indicate that the up/down state is changing */
1250 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1252 /* Mark interface up or down */
1254 ifp
->if_flags
|= IFF_UP
;
1257 ifp
->if_flags
&= ~IFF_UP
;
1260 ifnet_touch_lastchange(ifp
);
1262 /* Drop the lock to notify addresses and route */
1263 ifnet_lock_done(ifp
);
1264 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1265 for (i
= 0; ifa
[i
] != 0; i
++) {
1266 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1268 ifnet_free_address_list(ifa
);
1275 /* Inform all transmit queues about the new link state */
1277 ifnet_update_sndq(ifq
, up
? CLASSQ_EV_LINK_UP
: CLASSQ_EV_LINK_DOWN
);
1280 /* Aquire the lock to clear the changing flag */
1281 ifnet_lock_exclusive(ifp
);
1282 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1283 wakeup(&ifp
->if_eflags
);
1287 * Mark an interface down and notify protocols of
1294 ifnet_lock_exclusive(ifp
);
1296 ifnet_lock_done(ifp
);
1300 * Mark an interface up and notify protocols of
1307 ifnet_lock_exclusive(ifp
);
1309 ifnet_lock_done(ifp
);
1313 * Flush an interface queue.
1316 if_qflush(struct ifnet
*ifp
, int ifq_locked
)
1318 struct ifclassq
*ifq
= &ifp
->if_snd
;
1323 if (IFCQ_IS_ENABLED(ifq
))
1326 if (IFCQ_IS_DRAINING(ifq
))
1327 ifq
->ifcq_drain
= 0;
1328 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1329 ALTQ_PURGE(IFCQ_ALTQ(ifq
));
1330 #endif /* PF_ALTQ */
1332 VERIFY(IFCQ_IS_EMPTY(ifq
));
1339 if_qflush_sc(struct ifnet
*ifp
, mbuf_svc_class_t sc
, u_int32_t flow
,
1340 u_int32_t
*packets
, u_int32_t
*bytes
, int ifq_locked
)
1342 struct ifclassq
*ifq
= &ifp
->if_snd
;
1343 u_int32_t cnt
= 0, len
= 0;
1344 u_int32_t a_cnt
= 0, a_len
= 0;
1346 VERIFY(sc
== MBUF_SC_UNSPEC
|| MBUF_VALID_SC(sc
));
1352 if (IFCQ_IS_ENABLED(ifq
))
1353 IFCQ_PURGE_SC(ifq
, sc
, flow
, cnt
, len
);
1355 if (IFCQ_IS_DRAINING(ifq
)) {
1356 VERIFY((signed)(ifq
->ifcq_drain
- cnt
) >= 0);
1357 ifq
->ifcq_drain
-= cnt
;
1359 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
1360 ALTQ_PURGE_SC(IFCQ_ALTQ(ifq
), sc
, flow
, a_cnt
, a_len
);
1361 #endif /* PF_ALTQ */
1366 if (packets
!= NULL
)
1367 *packets
= cnt
+ a_cnt
;
1369 *bytes
= len
+ a_len
;
1373 * Map interface name to
1374 * interface structure pointer.
1377 ifunit(const char *name
)
1379 char namebuf
[IFNAMSIZ
+ 1];
1387 if (len
< 2 || len
> IFNAMSIZ
)
1389 cp
= name
+ len
- 1;
1391 if (c
< '0' || c
> '9')
1392 return (NULL
); /* trailing garbage */
1397 return (NULL
); /* no interface name */
1398 unit
+= (c
- '0') * m
;
1400 return (NULL
); /* number is unreasonable */
1403 } while (c
>= '0' && c
<= '9');
1404 len
= cp
- name
+ 1;
1405 bcopy(name
, namebuf
, len
);
1406 namebuf
[len
] = '\0';
1408 * Now search all the interfaces for this name/number
1410 ifnet_head_lock_shared();
1411 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1412 if (strncmp(ifp
->if_name
, namebuf
, len
))
1414 if (unit
== ifp
->if_unit
)
1423 * Map interface name in a sockaddr_dl to
1424 * interface structure pointer.
1427 if_withname(struct sockaddr
*sa
)
1429 char ifname
[IFNAMSIZ
+1];
1430 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(void *)sa
;
1432 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1433 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1437 * ifunit wants a null-terminated name. It may not be null-terminated
1438 * in the sockaddr. We don't want to change the caller's sockaddr,
1439 * and there might not be room to put the trailing null anyway, so we
1440 * make a local copy that we know we can null terminate safely.
1443 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1444 ifname
[sdl
->sdl_nlen
] = '\0';
1445 return (ifunit(ifname
));
1453 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1455 char ifname
[IFNAMSIZ
+ 1];
1456 struct ifnet
*ifp
= NULL
;
1457 struct ifstat
*ifs
= NULL
;
1460 bzero(ifname
, sizeof (ifname
));
1463 * ioctls which don't require ifp, or ifreq ioctls
1466 case OSIOCGIFCONF32
: /* struct ifconf32 */
1467 case SIOCGIFCONF32
: { /* struct ifconf32 */
1468 struct ifconf32 ifc
;
1469 bcopy(data
, &ifc
, sizeof (ifc
));
1470 error
= ifconf(cmd
, CAST_USER_ADDR_T(ifc
.ifc_req
),
1472 bcopy(&ifc
, data
, sizeof (ifc
));
1476 case SIOCGIFCONF64
: /* struct ifconf64 */
1477 case OSIOCGIFCONF64
: { /* struct ifconf64 */
1478 struct ifconf64 ifc
;
1479 bcopy(data
, &ifc
, sizeof (ifc
));
1480 error
= ifconf(cmd
, ifc
.ifc_req
, &ifc
.ifc_len
);
1481 bcopy(&ifc
, data
, sizeof (ifc
));
1486 case SIOCIFGCLONERS32
: { /* struct if_clonereq32 */
1487 struct if_clonereq32 ifcr
;
1488 bcopy(data
, &ifcr
, sizeof (ifcr
));
1489 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1490 CAST_USER_ADDR_T(ifcr
.ifcru_buffer
));
1491 bcopy(&ifcr
, data
, sizeof (ifcr
));
1495 case SIOCIFGCLONERS64
: { /* struct if_clonereq64 */
1496 struct if_clonereq64 ifcr
;
1497 bcopy(data
, &ifcr
, sizeof (ifcr
));
1498 error
= if_clone_list(ifcr
.ifcr_count
, &ifcr
.ifcr_total
,
1500 bcopy(&ifcr
, data
, sizeof (ifcr
));
1503 #endif /* IF_CLONE_LIST */
1505 case SIOCSIFDSTADDR
: /* struct ifreq */
1506 case SIOCSIFADDR
: /* struct ifreq */
1507 case SIOCSIFBRDADDR
: /* struct ifreq */
1508 case SIOCSIFNETMASK
: /* struct ifreq */
1509 case OSIOCGIFADDR
: /* struct ifreq */
1510 case OSIOCGIFDSTADDR
: /* struct ifreq */
1511 case OSIOCGIFBRDADDR
: /* struct ifreq */
1512 case OSIOCGIFNETMASK
: /* struct ifreq */
1513 case SIOCSIFKPI
: /* struct ifreq */
1514 if (so
->so_proto
== NULL
) {
1519 case SIOCIFCREATE
: /* struct ifreq */
1520 case SIOCIFCREATE2
: /* struct ifreq */
1521 case SIOCIFDESTROY
: /* struct ifreq */
1522 case SIOCGIFFLAGS
: /* struct ifreq */
1523 case SIOCGIFEFLAGS
: /* struct ifreq */
1524 case SIOCGIFCAP
: /* struct ifreq */
1525 case SIOCGIFMAC
: /* struct ifreq */
1526 case SIOCGIFMETRIC
: /* struct ifreq */
1527 case SIOCGIFMTU
: /* struct ifreq */
1528 case SIOCGIFPHYS
: /* struct ifreq */
1529 case SIOCSIFFLAGS
: /* struct ifreq */
1530 case SIOCSIFCAP
: /* struct ifreq */
1531 case SIOCSIFPHYS
: /* struct ifreq */
1532 case SIOCSIFMTU
: /* struct ifreq */
1533 case SIOCADDMULTI
: /* struct ifreq */
1534 case SIOCDELMULTI
: /* struct ifreq */
1535 case SIOCDIFPHYADDR
: /* struct ifreq */
1536 case SIOCSIFMEDIA
: /* struct ifreq */
1537 case SIOCSIFGENERIC
: /* struct ifreq */
1538 case SIOCSIFLLADDR
: /* struct ifreq */
1539 case SIOCSIFALTMTU
: /* struct ifreq */
1540 case SIOCSIFVLAN
: /* struct ifreq */
1541 case SIOCSIFBOND
: /* struct ifreq */
1542 case SIOCGIFPSRCADDR
: /* struct ifreq */
1543 case SIOCGIFPDSTADDR
: /* struct ifreq */
1544 case SIOCGIFGENERIC
: /* struct ifreq */
1545 case SIOCGIFDEVMTU
: /* struct ifreq */
1546 case SIOCGIFVLAN
: /* struct ifreq */
1547 case SIOCGIFBOND
: /* struct ifreq */
1548 case SIOCGIFWAKEFLAGS
: /* struct ifreq */
1549 case SIOCGIFGETRTREFCNT
: /* struct ifreq */
1550 case SIOCSIFOPPORTUNISTIC
: /* struct ifreq */
1551 case SIOCGIFOPPORTUNISTIC
: /* struct ifreq */
1552 case SIOCGIFLINKQUALITYMETRIC
: { /* struct ifreq */
1554 bcopy(data
, &ifr
, sizeof (ifr
));
1555 error
= ifioctl_ifreq(so
, cmd
, &ifr
, p
);
1556 bcopy(&ifr
, data
, sizeof (ifr
));
1562 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
1563 * here to ensure that the ifnet, if found, has been fully attached.
1567 case SIOCSIFPHYADDR
: { /* struct ifaliasreq */
1568 bcopy(((struct ifaliasreq
*)(void *)data
)->ifra_name
,
1570 ifp
= ifunit(ifname
);
1575 case SIOCSIFPHYADDR_IN6_32
: { /* struct in6_aliasreq_32 */
1576 bcopy(((struct in6_aliasreq_32
*)(void *)data
)->ifra_name
,
1578 ifp
= ifunit(ifname
);
1582 case SIOCSIFPHYADDR_IN6_64
: { /* struct in6_aliasreq_64 */
1583 bcopy(((struct in6_aliasreq_64
*)(void *)data
)->ifra_name
,
1585 ifp
= ifunit(ifname
);
1590 case SIOCSLIFPHYADDR
: /* struct if_laddrreq */
1591 case SIOCGLIFPHYADDR
: { /* struct if_laddrreq */
1592 bcopy(((struct if_laddrreq
*)(void *)data
)->iflr_name
,
1594 ifp
= ifunit(ifname
);
1598 case SIOCGIFSTATUS
: { /* struct ifstat */
1599 ifs
= _MALLOC(sizeof (*ifs
), M_DEVBUF
, M_WAITOK
);
1605 bcopy(data
, ifs
, sizeof (*ifs
));
1606 ifs
->ifs_name
[IFNAMSIZ
- 1] = '\0';
1607 ifp
= ifunit(ifs
->ifs_name
);
1611 case SIOCGIFMEDIA32
: { /* struct ifmediareq32 */
1612 bcopy(((struct ifmediareq32
*)(void *)data
)->ifm_name
,
1614 ifp
= ifunit(ifname
);
1618 case SIOCGIFMEDIA64
: { /* struct ifmediareq64 */
1619 bcopy(((struct ifmediareq64
*)(void *)data
)->ifm_name
,
1621 ifp
= ifunit(ifname
);
1625 case SIOCSIFDESC
: /* struct if_descreq */
1626 case SIOCGIFDESC
: { /* struct if_descreq */
1627 bcopy(((struct if_descreq
*)(void *)data
)->ifdr_name
,
1629 ifp
= ifunit(ifname
);
1633 case SIOCSIFLINKPARAMS
: /* struct if_linkparamsreq */
1634 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1635 bcopy(((struct if_linkparamsreq
*)(void *)data
)->iflpr_name
,
1637 ifp
= ifunit(ifname
);
1641 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1642 bcopy(((struct if_qstatsreq
*)(void *)data
)->ifqr_name
,
1644 ifp
= ifunit(ifname
);
1648 case SIOCSIFTHROTTLE
: /* struct if_throttlereq */
1649 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1650 bcopy(((struct if_throttlereq
*)(void *)data
)->ifthr_name
,
1652 ifp
= ifunit(ifname
);
1658 * This is a bad assumption, but the code seems to
1659 * have been doing this in the past; caveat emptor.
1661 bcopy(((struct ifreq
*)(void *)data
)->ifr_name
,
1663 ifp
= ifunit(ifname
);
1675 case SIOCSIFPHYADDR
: /* struct ifaliasreq */
1677 case SIOCSIFPHYADDR_IN6_32
: /* struct in6_aliasreq_32 */
1678 case SIOCSIFPHYADDR_IN6_64
: /* struct in6_aliasreq_64 */
1680 case SIOCSLIFPHYADDR
: /* struct if_laddrreq */
1681 error
= proc_suser(p
);
1685 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1690 ifnet_touch_lastchange(ifp
);
1693 case SIOCGIFSTATUS
: /* struct ifstat */
1694 VERIFY(ifs
!= NULL
);
1695 ifs
->ascii
[0] = '\0';
1697 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1700 bcopy(ifs
, data
, sizeof (*ifs
));
1703 case SIOCGLIFPHYADDR
: /* struct if_laddrreq */
1704 case SIOCGIFMEDIA32
: /* struct ifmediareq32 */
1705 case SIOCGIFMEDIA64
: /* struct ifmediareq64 */
1706 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1710 case SIOCSIFDESC
: { /* struct if_descreq */
1711 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1714 if ((error
= proc_suser(p
)) != 0)
1717 ifnet_lock_exclusive(ifp
);
1718 bcopy(&ifdr
->ifdr_len
, &ifdr_len
, sizeof (ifdr_len
));
1719 if (ifdr_len
> sizeof (ifdr
->ifdr_desc
) ||
1720 ifdr_len
> ifp
->if_desc
.ifd_maxlen
) {
1722 ifnet_lock_done(ifp
);
1726 bzero(ifp
->if_desc
.ifd_desc
, ifp
->if_desc
.ifd_maxlen
);
1727 if ((ifp
->if_desc
.ifd_len
= ifdr_len
) > 0) {
1728 bcopy(ifdr
->ifdr_desc
, ifp
->if_desc
.ifd_desc
,
1729 MIN(ifdr_len
, ifp
->if_desc
.ifd_maxlen
));
1731 ifnet_lock_done(ifp
);
1735 case SIOCGIFDESC
: { /* struct if_descreq */
1736 struct if_descreq
*ifdr
= (struct if_descreq
*)(void *)data
;
1739 ifnet_lock_shared(ifp
);
1740 ifdr_len
= MIN(ifp
->if_desc
.ifd_len
, sizeof (ifdr
->ifdr_desc
));
1741 bcopy(&ifdr_len
, &ifdr
->ifdr_len
, sizeof (ifdr_len
));
1742 bzero(&ifdr
->ifdr_desc
, sizeof (ifdr
->ifdr_desc
));
1744 bcopy(ifp
->if_desc
.ifd_desc
, ifdr
->ifdr_desc
, ifdr_len
);
1746 ifnet_lock_done(ifp
);
1750 case SIOCSIFLINKPARAMS
: { /* struct if_linkparamsreq */
1751 struct if_linkparamsreq
*iflpr
=
1752 (struct if_linkparamsreq
*)(void *)data
;
1753 struct ifclassq
*ifq
= &ifp
->if_snd
;
1754 struct tb_profile tb
= { 0, 0, 0 };
1756 if ((error
= proc_suser(p
)) != 0)
1760 if (!IFCQ_IS_READY(ifq
)) {
1765 bcopy(&iflpr
->iflpr_output_tbr_rate
, &tb
.rate
,
1767 bcopy(&iflpr
->iflpr_output_tbr_percent
, &tb
.percent
,
1768 sizeof (tb
.percent
));
1769 error
= ifclassq_tbr_set(ifq
, &tb
, TRUE
);
1774 case SIOCGIFLINKPARAMS
: { /* struct if_linkparamsreq */
1775 struct if_linkparamsreq
*iflpr
=
1776 (struct if_linkparamsreq
*)(void *)data
;
1777 struct ifclassq
*ifq
= &ifp
->if_snd
;
1778 u_int32_t sched_type
= PKTSCHEDT_NONE
, flags
= 0;
1779 u_int64_t tbr_bw
= 0, tbr_pct
= 0;
1783 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
))) {
1784 sched_type
= IFCQ_ALTQ(ifq
)->altq_type
;
1785 flags
|= IFLPRF_ALTQ
;
1787 #endif /* PF_ALTQ */
1789 if (IFCQ_IS_ENABLED(ifq
))
1790 sched_type
= ifq
->ifcq_type
;
1792 bcopy(&sched_type
, &iflpr
->iflpr_output_sched
,
1793 sizeof (iflpr
->iflpr_output_sched
));
1795 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1796 tbr_bw
= ifq
->ifcq_tbr
.tbr_rate_raw
;
1797 tbr_pct
= ifq
->ifcq_tbr
.tbr_percent
;
1799 bcopy(&tbr_bw
, &iflpr
->iflpr_output_tbr_rate
,
1800 sizeof (iflpr
->iflpr_output_tbr_rate
));
1801 bcopy(&tbr_pct
, &iflpr
->iflpr_output_tbr_percent
,
1802 sizeof (iflpr
->iflpr_output_tbr_percent
));
1805 if (ifp
->if_output_sched_model
==
1806 IFNET_SCHED_MODEL_DRIVER_MANAGED
)
1807 flags
|= IFLPRF_DRVMANAGED
;
1808 bcopy(&flags
, &iflpr
->iflpr_flags
, sizeof (iflpr
->iflpr_flags
));
1809 bcopy(&ifp
->if_output_bw
, &iflpr
->iflpr_output_bw
,
1810 sizeof (iflpr
->iflpr_output_bw
));
1811 bcopy(&ifp
->if_input_bw
, &iflpr
->iflpr_input_bw
,
1812 sizeof (iflpr
->iflpr_input_bw
));
1816 case SIOCGIFQUEUESTATS
: { /* struct if_qstatsreq */
1817 struct if_qstatsreq
*ifqr
= (struct if_qstatsreq
*)(void *)data
;
1818 u_int32_t ifqr_len
, ifqr_slot
;
1820 bcopy(&ifqr
->ifqr_slot
, &ifqr_slot
, sizeof (ifqr_slot
));
1821 bcopy(&ifqr
->ifqr_len
, &ifqr_len
, sizeof (ifqr_len
));
1822 error
= ifclassq_getqstats(&ifp
->if_snd
, ifqr_slot
,
1823 ifqr
->ifqr_buf
, &ifqr_len
);
1826 bcopy(&ifqr_len
, &ifqr
->ifqr_len
, sizeof (ifqr_len
));
1830 case SIOCSIFTHROTTLE
: { /* struct if_throttlereq */
1831 struct if_throttlereq
*ifthr
=
1832 (struct if_throttlereq
*)(void *)data
;
1833 u_int32_t ifthr_level
;
1836 * XXX: Use priv_check_cred() instead of root check?
1838 if ((error
= proc_suser(p
)) != 0)
1841 bcopy(&ifthr
->ifthr_level
, &ifthr_level
, sizeof (ifthr_level
));
1842 error
= ifnet_set_throttle(ifp
, ifthr_level
);
1843 if (error
== EALREADY
)
1848 case SIOCGIFTHROTTLE
: { /* struct if_throttlereq */
1849 struct if_throttlereq
*ifthr
=
1850 (struct if_throttlereq
*)(void *)data
;
1851 u_int32_t ifthr_level
;
1853 if ((error
= ifnet_get_throttle(ifp
, &ifthr_level
)) == 0) {
1854 bcopy(&ifthr_level
, &ifthr
->ifthr_level
,
1855 sizeof (ifthr_level
));
1861 if (so
->so_proto
== NULL
) {
1867 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1869 socket_unlock(so
, 1);
1871 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
1872 error
= ifnet_ioctl(ifp
,
1873 so
->so_proto
->pr_domain
->dom_family
, cmd
, data
);
1880 _FREE(ifs
, M_DEVBUF
);
1886 ifioctl_ifreq(struct socket
*so
, u_long cmd
, struct ifreq
*ifr
, struct proc
*p
)
1891 struct kev_msg ev_msg
;
1892 struct net_event_data ev_data
;
1894 bzero(&ev_data
, sizeof (struct net_event_data
));
1895 bzero(&ev_msg
, sizeof (struct kev_msg
));
1897 ifr
->ifr_name
[IFNAMSIZ
- 1] = '\0';
1902 error
= proc_suser(p
);
1905 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1906 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
1908 error
= proc_suser(p
);
1911 return (if_clone_destroy(ifr
->ifr_name
));
1914 ifp
= ifunit(ifr
->ifr_name
);
1920 ifnet_lock_shared(ifp
);
1921 ifr
->ifr_flags
= ifp
->if_flags
;
1922 ifnet_lock_done(ifp
);
1926 ifnet_lock_shared(ifp
);
1927 ifr
->ifr_eflags
= ifp
->if_eflags
;
1928 ifnet_lock_done(ifp
);
1932 ifnet_lock_shared(ifp
);
1933 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1934 ifr
->ifr_curcap
= ifp
->if_capenable
;
1935 ifnet_lock_done(ifp
);
1940 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
1944 ifnet_lock_shared(ifp
);
1945 ifr
->ifr_metric
= ifp
->if_metric
;
1946 ifnet_lock_done(ifp
);
1950 ifnet_lock_shared(ifp
);
1951 ifr
->ifr_mtu
= ifp
->if_mtu
;
1952 ifnet_lock_done(ifp
);
1956 ifnet_lock_shared(ifp
);
1957 ifr
->ifr_phys
= ifp
->if_physical
;
1958 ifnet_lock_done(ifp
);
1961 case SIOCGIFWAKEFLAGS
:
1962 ifnet_lock_shared(ifp
);
1963 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
1964 ifnet_lock_done(ifp
);
1967 case SIOCGIFGETRTREFCNT
:
1968 ifnet_lock_shared(ifp
);
1969 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
1970 ifnet_lock_done(ifp
);
1973 case SIOCGIFLINKQUALITYMETRIC
:
1974 ifnet_lock_shared(ifp
);
1975 ifr
->ifr_link_quality_metric
= ifp
->if_lqm
;
1976 ifnet_lock_done(ifp
);
1980 error
= proc_suser(p
);
1984 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
1985 (u_int16_t
)~IFF_CANTCHANGE
);
1988 * Note that we intentionally ignore any error from below
1989 * for the SIOCSIFFLAGS case.
1991 (void) ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1995 * Send the event even upon error from the driver because
1996 * we changed the flags.
1998 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1999 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2000 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2002 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
2003 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2004 ev_data
.if_family
= ifp
->if_family
;
2005 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2006 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2007 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2008 ev_msg
.dv
[1].data_length
= 0;
2009 kev_post_msg(&ev_msg
);
2011 ifnet_touch_lastchange(ifp
);
2015 error
= proc_suser(p
);
2019 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
2023 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
2026 ifnet_touch_lastchange(ifp
);
2031 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
2035 error
= proc_suser(p
);
2039 ifp
->if_metric
= ifr
->ifr_metric
;
2041 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2042 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2043 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2045 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
2046 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2047 ev_data
.if_family
= ifp
->if_family
;
2048 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2049 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2050 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2052 ev_msg
.dv
[1].data_length
= 0;
2053 kev_post_msg(&ev_msg
);
2055 ifnet_touch_lastchange(ifp
);
2059 error
= proc_suser(p
);
2063 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
2068 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2069 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2070 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2072 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
2073 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2074 ev_data
.if_family
= ifp
->if_family
;
2075 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2076 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2077 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2078 ev_msg
.dv
[1].data_length
= 0;
2079 kev_post_msg(&ev_msg
);
2081 ifnet_touch_lastchange(ifp
);
2085 u_int32_t oldmtu
= ifp
->if_mtu
;
2086 struct ifclassq
*ifq
= &ifp
->if_snd
;
2088 error
= proc_suser(p
);
2092 if (ifp
->if_ioctl
== NULL
) {
2096 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2100 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
2105 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2106 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2107 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2109 ev_msg
.event_code
= KEV_DL_SIFMTU
;
2110 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2111 ev_data
.if_family
= ifp
->if_family
;
2112 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2113 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2114 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2115 ev_msg
.dv
[1].data_length
= 0;
2116 kev_post_msg(&ev_msg
);
2118 ifnet_touch_lastchange(ifp
);
2122 * If the link MTU changed, do network layer specific procedure
2123 * and update all route entries associated with the interface,
2124 * so that their MTU metric gets updated.
2126 if (ifp
->if_mtu
!= oldmtu
) {
2127 if_rtmtu_update(ifp
);
2131 /* Inform all transmit queues about the new MTU */
2133 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_MTU
);
2141 error
= proc_suser(p
);
2145 /* Don't allow group membership on non-multicast interfaces. */
2146 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2151 /* Don't let users screw up protocols' entries. */
2152 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
2153 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2159 * User is permitted to anonymously join a particular link
2160 * multicast group via SIOCADDMULTI. Subsequent join requested
2161 * for the same record which has an outstanding refcnt from a
2162 * past if_addmulti_anon() will not result in EADDRINUSE error
2163 * (unlike other BSDs.) Anonymously leaving a group is also
2164 * allowed only as long as there is an outstanding refcnt held
2165 * by a previous anonymous request, or else ENOENT (even if the
2166 * link-layer multicast membership exists for a network-layer
2169 if (cmd
== SIOCADDMULTI
) {
2170 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
2171 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
2173 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
2174 ev_msg
.event_code
= KEV_DL_DELMULTI
;
2179 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2180 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2181 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
2182 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2184 ev_data
.if_family
= ifp
->if_family
;
2185 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
2186 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
2187 ev_msg
.dv
[0].data_ptr
= &ev_data
;
2188 ev_msg
.dv
[1].data_length
= 0;
2189 kev_post_msg(&ev_msg
);
2191 ifnet_touch_lastchange(ifp
);
2194 case SIOCDIFPHYADDR
:
2196 case SIOCSIFGENERIC
:
2201 error
= proc_suser(p
);
2205 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
2210 ifnet_touch_lastchange(ifp
);
2213 case SIOCGIFPSRCADDR
:
2214 case SIOCGIFPDSTADDR
:
2215 case SIOCGIFGENERIC
:
2219 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
2223 case SIOCSIFOPPORTUNISTIC
:
2224 case SIOCGIFOPPORTUNISTIC
:
2225 error
= ifnet_getset_opportunistic(ifp
, cmd
, ifr
, p
);
2228 case SIOCSIFDSTADDR
:
2230 case SIOCSIFBRDADDR
:
2231 case SIOCSIFNETMASK
:
2233 case OSIOCGIFDSTADDR
:
2234 case OSIOCGIFBRDADDR
:
2235 case OSIOCGIFNETMASK
:
2237 VERIFY(so
->so_proto
!= NULL
);
2239 if (cmd
== SIOCSIFDSTADDR
|| cmd
== SIOCSIFADDR
||
2240 cmd
== SIOCSIFBRDADDR
|| cmd
== SIOCSIFNETMASK
) {
2241 #if BYTE_ORDER != BIG_ENDIAN
2242 if (ifr
->ifr_addr
.sa_family
== 0 &&
2243 ifr
->ifr_addr
.sa_len
< 16) {
2244 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
2245 ifr
->ifr_addr
.sa_len
= 16;
2248 if (ifr
->ifr_addr
.sa_len
== 0)
2249 ifr
->ifr_addr
.sa_len
= 16;
2251 } else if (cmd
== OSIOCGIFADDR
) {
2252 cmd
= SIOCGIFADDR
; /* struct ifreq */
2253 } else if (cmd
== OSIOCGIFDSTADDR
) {
2254 cmd
= SIOCGIFDSTADDR
; /* struct ifreq */
2255 } else if (cmd
== OSIOCGIFBRDADDR
) {
2256 cmd
= SIOCGIFBRDADDR
; /* struct ifreq */
2257 } else if (cmd
== OSIOCGIFNETMASK
) {
2258 cmd
= SIOCGIFNETMASK
; /* struct ifreq */
2262 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
2263 (caddr_t
)ifr
, ifp
, p
));
2264 socket_unlock(so
, 1);
2268 case OSIOCGIFDSTADDR
:
2269 case OSIOCGIFBRDADDR
:
2270 case OSIOCGIFNETMASK
:
2271 bcopy(&ifr
->ifr_addr
.sa_family
, &ifr
->ifr_addr
,
2275 if (cmd
== SIOCSIFKPI
) {
2276 int temperr
= proc_suser(p
);
2281 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
) {
2282 error
= ifnet_ioctl(ifp
,
2283 so
->so_proto
->pr_domain
->dom_family
, cmd
,
2297 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
2301 socket_unlock(so
, 0);
2302 error
= ifioctl(so
, cmd
, data
, p
);
2308 * Set/clear promiscuous mode on interface ifp based on the truth value
2309 * of pswitch. The calls are reference counted so that only the first
2310 * "on" request actually has an effect, as does the final "off" request.
2311 * Results are undefined if the "off" and "on" requests are not matched.
2314 ifnet_set_promiscuous(
2322 ifnet_lock_exclusive(ifp
);
2323 oldflags
= ifp
->if_flags
;
2324 ifp
->if_pcount
+= pswitch
? 1 : -1;
2326 if (ifp
->if_pcount
> 0)
2327 ifp
->if_flags
|= IFF_PROMISC
;
2329 ifp
->if_flags
&= ~IFF_PROMISC
;
2331 newflags
= ifp
->if_flags
;
2332 ifnet_lock_done(ifp
);
2334 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
2335 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
2339 ifnet_lock_exclusive(ifp
);
2341 ifp
->if_pcount
-= pswitch
? 1 : -1;
2342 if (ifp
->if_pcount
> 0)
2343 ifp
->if_flags
|= IFF_PROMISC
;
2345 ifp
->if_flags
&= ~IFF_PROMISC
;
2346 ifnet_lock_done(ifp
);
2350 if (newflags
!= oldflags
) {
2351 log(LOG_INFO
, "%s%d: promiscuous mode %s%s\n",
2352 ifp
->if_name
, ifp
->if_unit
,
2353 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
2354 error
!= 0 ? " failed" : " succeeded");
2360 * Return interface configuration
2361 * of system. List may be used
2362 * in later ioctl's (above) to get
2363 * other information.
2367 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
2369 struct ifnet
*ifp
= NULL
;
2376 * Zero the ifr buffer to make sure we don't
2377 * disclose the contents of the stack.
2379 bzero(&ifr
, sizeof(struct ifreq
));
2382 ifnet_head_lock_shared();
2383 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) &&
2384 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
2386 size_t ifnlen
, addrs
;
2388 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
2389 "%s%d", ifp
->if_name
, ifp
->if_unit
);
2390 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
2391 error
= ENAMETOOLONG
;
2394 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
2397 ifnet_lock_shared(ifp
);
2400 ifa
= ifp
->if_addrhead
.tqh_first
;
2401 for ( ; space
> sizeof (ifr
) && ifa
;
2402 ifa
= ifa
->ifa_link
.tqe_next
) {
2403 struct sockaddr
*sa
;
2408 if (curproc
->p_prison
&& prison_if(curproc
, sa
)) {
2414 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
2415 struct osockaddr
*osa
=
2416 (struct osockaddr
*)(void *)&ifr
.ifr_addr
;
2418 osa
->sa_family
= sa
->sa_family
;
2419 error
= copyout((caddr_t
)&ifr
, ifrp
,
2421 ifrp
+= sizeof(struct ifreq
);
2422 } else if (sa
->sa_len
<= sizeof(*sa
)) {
2424 error
= copyout((caddr_t
)&ifr
, ifrp
,
2426 ifrp
+= sizeof(struct ifreq
);
2429 sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
)) {
2433 space
-= sa
->sa_len
- sizeof(*sa
);
2434 error
= copyout((caddr_t
)&ifr
, ifrp
,
2435 sizeof (ifr
.ifr_name
));
2437 error
= copyout((caddr_t
)sa
, (ifrp
+
2438 offsetof(struct ifreq
, ifr_addr
)),
2441 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
2447 space
-= sizeof (ifr
);
2449 ifnet_lock_done(ifp
);
2454 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
2455 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
2458 space
-= sizeof (ifr
);
2459 ifrp
+= sizeof(struct ifreq
);
2463 *ret_space
-= space
;
2468 * Just like if_promisc(), but for all-multicast-reception mode.
2471 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2476 ifnet_lock_exclusive(ifp
);
2479 if (ifp
->if_amcount
++ == 0) {
2480 ifp
->if_flags
|= IFF_ALLMULTI
;
2484 if (ifp
->if_amcount
> 1) {
2487 ifp
->if_amcount
= 0;
2488 ifp
->if_flags
&= ~IFF_ALLMULTI
;
2492 ifnet_lock_done(ifp
);
2495 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
2502 static struct ifmultiaddr
*
2505 struct ifmultiaddr
*ifma
;
2507 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
2508 zalloc_noblock(ifma_zone
);
2511 bzero(ifma
, ifma_size
);
2512 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2513 ifma
->ifma_debug
|= IFD_ALLOC
;
2514 if (ifma_debug
!= 0) {
2515 ifma
->ifma_debug
|= IFD_DEBUG
;
2516 ifma
->ifma_trace
= ifma_trace
;
2523 ifma_free(struct ifmultiaddr
*ifma
)
2527 if (ifma
->ifma_protospec
!= NULL
) {
2528 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
2530 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2531 ifma
->ifma_anoncnt
!= 0) {
2532 panic("%s: Freeing ifma=%p with outstanding anon req",
2535 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
2536 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
2537 __func__
, ifma
, ifma
->ifma_ifp
);
2539 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
2540 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
2542 } else if (ifma
->ifma_refcount
!= 0) {
2543 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
2545 } else if (ifma
->ifma_reqcnt
!= 0) {
2546 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
2548 } else if (ifma
->ifma_ifp
!= NULL
) {
2549 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
2550 ifma
->ifma_ifp
, ifma
);
2552 } else if (ifma
->ifma_ll
!= NULL
) {
2553 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
2554 ifma
->ifma_ll
, ifma
);
2557 ifma
->ifma_debug
&= ~IFD_ALLOC
;
2558 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
2559 (IFD_DEBUG
| IFD_TRASHED
)) {
2560 lck_mtx_lock(&ifma_trash_lock
);
2561 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
2563 lck_mtx_unlock(&ifma_trash_lock
);
2564 ifma
->ifma_debug
&= ~IFD_TRASHED
;
2568 if (ifma
->ifma_addr
!= NULL
) {
2569 FREE(ifma
->ifma_addr
, M_IFADDR
);
2570 ifma
->ifma_addr
= NULL
;
2572 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
2573 zfree(ifma_zone
, ifma
);
2577 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
2579 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
2584 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
2585 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
2589 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
2590 tr
= ifma_dbg
->ifma_refhold
;
2592 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
2593 tr
= ifma_dbg
->ifma_refrele
;
2596 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
2597 ctrace_record(&tr
[idx
]);
2601 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
2606 IFMA_LOCK_ASSERT_HELD(ifma
);
2608 if (++ifma
->ifma_refcount
== 0) {
2609 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
2611 } else if (ifma
->ifma_trace
!= NULL
) {
2612 (*ifma
->ifma_trace
)(ifma
, TRUE
);
2619 ifma_remref(struct ifmultiaddr
*ifma
)
2621 struct ifmultiaddr
*ll
;
2625 if (ifma
->ifma_refcount
== 0) {
2626 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
2628 } else if (ifma
->ifma_trace
!= NULL
) {
2629 (*ifma
->ifma_trace
)(ifma
, FALSE
);
2632 --ifma
->ifma_refcount
;
2633 if (ifma
->ifma_refcount
> 0) {
2639 ifma
->ifma_ifp
= NULL
;
2640 ifma
->ifma_ll
= NULL
;
2642 ifma_free(ifma
); /* deallocate it */
2649 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
2651 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
2652 IFMA_LOCK_ASSERT_HELD(ifma
);
2654 if (ifma
->ifma_ifp
!= ifp
) {
2655 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
2656 ifma
->ifma_ifp
, ifp
);
2658 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
2659 panic("%s: Attempt to attach an already attached ifma=%p",
2662 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2663 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
2665 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
2666 panic("%s: Attempt to reattach a detached ifma=%p",
2671 ifma
->ifma_reqcnt
++;
2672 VERIFY(ifma
->ifma_reqcnt
== 1);
2673 IFMA_ADDREF_LOCKED(ifma
);
2674 ifma
->ifma_debug
|= IFD_ATTACHED
;
2676 ifma
->ifma_anoncnt
++;
2677 VERIFY(ifma
->ifma_anoncnt
== 1);
2678 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
2681 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2685 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
2687 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
2688 IFMA_LOCK_ASSERT_HELD(ifma
);
2690 if (ifma
->ifma_reqcnt
== 0) {
2691 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
2693 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2694 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
2696 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
2697 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
2699 } else if (ifma
->ifma_ifp
!= ifp
) {
2700 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
2701 ifma
->ifma_ifp
, ifp
);
2706 --ifma
->ifma_anoncnt
;
2707 if (ifma
->ifma_anoncnt
> 0)
2709 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
2712 --ifma
->ifma_reqcnt
;
2713 if (ifma
->ifma_reqcnt
> 0)
2716 if (ifma
->ifma_protospec
!= NULL
) {
2717 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
2719 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2720 ifma
->ifma_anoncnt
!= 0) {
2721 panic("%s: Detaching ifma=%p with outstanding anon req",
2724 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
2725 panic("%s: Attempt to detach an unattached address ifma=%p",
2728 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
2729 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
2734 * NOTE: Caller calls IFMA_REMREF
2736 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
2737 LIST_REMOVE(ifma
, ifma_link
);
2738 if (LIST_EMPTY(&ifp
->if_multiaddrs
))
2739 ifp
->if_updatemcasts
= 0;
2741 if (ifma
->ifma_debug
& IFD_DEBUG
) {
2742 /* Become a regular mutex, just in case */
2743 IFMA_CONVERT_LOCK(ifma
);
2744 lck_mtx_lock(&ifma_trash_lock
);
2745 TAILQ_INSERT_TAIL(&ifma_trash_head
,
2746 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
2747 lck_mtx_unlock(&ifma_trash_lock
);
2748 ifma
->ifma_debug
|= IFD_TRASHED
;
2755 * Find an ifmultiaddr that matches a socket address on an interface.
2757 * Caller is responsible for holding the ifnet_lock while calling
2761 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2762 struct ifmultiaddr
**retifma
, int anon
)
2764 struct ifmultiaddr
*ifma
;
2766 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
2767 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
2768 IFMA_LOCK_SPIN(ifma
);
2769 if (!equal(sa
, ifma
->ifma_addr
)) {
2774 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2775 ifma
->ifma_anoncnt
!= 0);
2776 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2777 ifma
->ifma_anoncnt
== 0);
2778 ifma
->ifma_anoncnt
++;
2779 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2780 VERIFY(ifma
->ifma_anoncnt
== 1);
2781 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
2784 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
2785 ifma
->ifma_reqcnt
++;
2786 VERIFY(ifma
->ifma_reqcnt
> 1);
2788 if (retifma
!= NULL
) {
2790 IFMA_ADDREF_LOCKED(ifma
);
2799 * Radar 3642395, make sure all multicasts are in a standard format.
2801 static struct sockaddr
*
2802 copy_and_normalize(const struct sockaddr
*original
)
2805 const u_char
*aptr
= NULL
;
2806 struct sockaddr
*copy
= NULL
;
2807 struct sockaddr_dl
*sdl_new
= NULL
;
2810 if (original
->sa_family
!= AF_LINK
&&
2811 original
->sa_family
!= AF_UNSPEC
) {
2812 /* Just make a copy */
2813 MALLOC(copy
, struct sockaddr
*, original
->sa_len
,
2814 M_IFADDR
, M_WAITOK
);
2816 bcopy(original
, copy
, original
->sa_len
);
2820 switch (original
->sa_family
) {
2822 const struct sockaddr_dl
*sdl_original
=
2823 (struct sockaddr_dl
*)(uintptr_t)(size_t)original
;
2825 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+
2826 sdl_original
->sdl_slen
+
2827 offsetof(struct sockaddr_dl
, sdl_data
) >
2828 sdl_original
->sdl_len
)
2831 alen
= sdl_original
->sdl_alen
;
2832 aptr
= CONST_LLADDR(sdl_original
);
2837 if (original
->sa_len
< ETHER_ADDR_LEN
+
2838 offsetof(struct sockaddr
, sa_data
)) {
2842 alen
= ETHER_ADDR_LEN
;
2843 aptr
= (const u_char
*)original
->sa_data
;
2848 if (alen
== 0 || aptr
== NULL
)
2851 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
2852 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
2854 if (sdl_new
!= NULL
) {
2855 bzero(sdl_new
, len
);
2856 sdl_new
->sdl_len
= len
;
2857 sdl_new
->sdl_family
= AF_LINK
;
2858 sdl_new
->sdl_alen
= alen
;
2859 bcopy(aptr
, LLADDR(sdl_new
), alen
);
2862 return ((struct sockaddr
*)sdl_new
);
2866 * Network-layer protocol domains which hold references to the underlying
2867 * link-layer record must use this routine.
2870 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2871 struct ifmultiaddr
**retifma
)
2873 return (if_addmulti_common(ifp
, sa
, retifma
, 0));
2877 * Anything other than network-layer protocol domains which hold references
2878 * to the underlying link-layer record must use this routine: SIOCADDMULTI
2879 * ioctl, ifnet_add_multicast(), AppleTalk, if_bond.
2882 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2883 struct ifmultiaddr
**retifma
)
2885 return (if_addmulti_common(ifp
, sa
, retifma
, 1));
2889 * Register an additional multicast address with a network interface.
2891 * - If the address is already present, bump the reference count on the
2892 * address and return.
2893 * - If the address is not link-layer, look up a link layer address.
2894 * - Allocate address structures for one or both addresses, and attach to the
2895 * multicast address list on the interface. If automatically adding a link
2896 * layer address, the protocol address will own a reference to the link
2897 * layer address, to be freed when it is freed.
2898 * - Notify the network device driver of an addition to the multicast address
2901 * 'sa' points to caller-owned memory with the desired multicast address.
2903 * 'retifma' will be used to return a pointer to the resulting multicast
2904 * address reference, if desired.
2906 * 'anon' indicates a link-layer address with no protocol address reference
2907 * made to it. Anything other than network-layer protocol domain requests
2908 * are considered as anonymous.
2911 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2912 struct ifmultiaddr
**retifma
, int anon
)
2914 struct sockaddr_storage storage
;
2915 struct sockaddr
*llsa
= NULL
;
2916 struct sockaddr
*dupsa
= NULL
;
2917 int error
= 0, ll_firstref
= 0, lladdr
;
2918 struct ifmultiaddr
*ifma
= NULL
;
2919 struct ifmultiaddr
*llifma
= NULL
;
2921 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
2922 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
2923 sa
->sa_family
== AF_LINK
);
2925 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
2926 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
2927 dupsa
= copy_and_normalize(sa
);
2928 if (dupsa
== NULL
) {
2935 ifnet_lock_exclusive(ifp
);
2936 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
2937 error
= EADDRNOTAVAIL
;
2938 ifnet_lock_done(ifp
);
2942 /* If the address is already present, return a new reference to it */
2943 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
2944 ifnet_lock_done(ifp
);
2949 * The address isn't already present; give the link layer a chance
2950 * to accept/reject it, and also find out which AF_LINK address this
2951 * maps to, if it isn't one already.
2953 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
2955 if (error
== 0 && storage
.ss_len
!= 0) {
2956 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
2962 llifma
= ifma_alloc(M_WAITOK
);
2963 if (llifma
== NULL
) {
2969 /* to be similar to FreeBSD */
2970 if (error
== EOPNOTSUPP
)
2972 else if (error
!= 0)
2975 /* Allocate while we aren't holding any locks */
2976 if (dupsa
== NULL
) {
2977 dupsa
= copy_and_normalize(sa
);
2978 if (dupsa
== NULL
) {
2983 ifma
= ifma_alloc(M_WAITOK
);
2989 ifnet_lock_exclusive(ifp
);
2991 * Check again for the matching multicast.
2993 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
2995 ifnet_lock_done(ifp
);
2999 if (llifma
!= NULL
) {
3000 VERIFY(!anon
); /* must not get here if "anonymous" */
3001 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
3002 FREE(llsa
, M_IFADDR
);
3006 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
3009 llifma
->ifma_addr
= llsa
;
3010 llifma
->ifma_ifp
= ifp
;
3012 if_attach_ifma(ifp
, llifma
, 0);
3013 /* add extra refcnt for ifma */
3014 IFMA_ADDREF_LOCKED(llifma
);
3015 IFMA_UNLOCK(llifma
);
3016 ifma
->ifma_ll
= llifma
;
3020 /* "anonymous" request should not result in network address */
3021 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
3023 ifma
->ifma_addr
= dupsa
;
3024 ifma
->ifma_ifp
= ifp
;
3026 if_attach_ifma(ifp
, ifma
, anon
);
3027 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
3028 if (retifma
!= NULL
) {
3030 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
3032 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
3033 ifma
->ifma_addr
->sa_family
== AF_LINK
);
3035 ifnet_lock_done(ifp
);
3037 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
3038 IFMA_REMREF(ifma
); /* for this routine */
3041 * We are certain we have added something, so call down to the
3042 * interface to let them know about it. Do this only for newly-
3043 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
3045 if (lladdr
|| ll_firstref
)
3046 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
3048 if (ifp
->if_updatemcasts
> 0)
3049 ifp
->if_updatemcasts
= 0;
3057 FREE(dupsa
, M_IFADDR
);
3061 FREE(llsa
, M_IFADDR
);
3067 * Delete a multicast group membership by network-layer group address.
3068 * This routine is deprecated.
3071 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
3073 return (if_delmulti_common(NULL
, ifp
, sa
, 0));
3077 * Delete a multicast group membership by group membership pointer.
3078 * Network-layer protocol domains must use this routine.
3081 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
3083 return (if_delmulti_common(ifma
, NULL
, NULL
, 0));
3087 * Anything other than network-layer protocol domains which hold references
3088 * to the underlying link-layer record must use this routine: SIOCDELMULTI
3089 * ioctl, ifnet_remove_multicast(), AppleTalk, if_bond.
3092 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
3094 return (if_delmulti_common(NULL
, ifp
, sa
, 1));
3098 * Delete a multicast group membership by network-layer group address.
3100 * Returns ENOENT if the entry could not be found.
3103 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
3104 const struct sockaddr
*sa
, int anon
)
3106 struct sockaddr
*dupsa
= NULL
;
3107 int lastref
, ll_lastref
= 0, lladdr
;
3108 struct ifmultiaddr
*ll
= NULL
;
3110 /* sanity check for callers */
3111 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
3114 ifp
= ifma
->ifma_ifp
;
3117 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
3118 dupsa
= copy_and_normalize(sa
);
3124 ifnet_lock_exclusive(ifp
);
3126 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
3127 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
3129 if (!equal(sa
, ifma
->ifma_addr
) ||
3130 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
3131 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
3132 ifma
->ifma_anoncnt
!= 0);
3136 /* found; keep it locked */
3141 FREE(dupsa
, M_IFADDR
);
3142 ifnet_lock_done(ifp
);
3148 IFMA_LOCK_ASSERT_HELD(ifma
);
3149 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
3150 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
3151 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
3152 ifma
->ifma_reqcnt
== 0));
3153 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
3155 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
3156 ifma
->ifma_addr
->sa_family
== AF_LINK
);
3158 if (lastref
&& ll
!= NULL
) {
3160 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
3163 ifnet_lock_done(ifp
);
3166 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
3168 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
3170 * Make sure the interface driver is notified in the
3171 * case of a link layer mcast group being left. Do
3172 * this only for a AF_LINK/AF_UNSPEC address that has
3173 * been removed from the if_multiaddrs set.
3175 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
3179 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
3181 IFMA_REMREF(ll
); /* for if_multiaddrs list */
3183 IFMA_REMREF(ifma
); /* for this routine */
3185 FREE(dupsa
, M_IFADDR
);
3191 * We don't use if_setlladdr, our interfaces are responsible for
3192 * handling the SIOCSIFLLADDR ioctl.
3196 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
3202 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
3203 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Generic link-management");
3207 * Shutdown all network activity. Used boot() when halting
3217 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
3218 for (i
= 0; i
< count
; i
++) {
3220 dlil_proto_unplumb_all(ifp
[i
]);
3222 ifnet_list_free(ifp
);
3229 * Delete Routes for a Network Interface
3231 * Called for each routing entry via the rnh->rnh_walktree() call above
3232 * to delete all route entries referencing a detaching network interface.
3235 * rn pointer to node in the routing table
3236 * arg argument passed to rnh->rnh_walktree() - detaching interface
3240 * errno failed - reason indicated
3244 if_rtdel(struct radix_node
*rn
, void *arg
)
3246 struct rtentry
*rt
= (struct rtentry
*)rn
;
3247 struct ifnet
*ifp
= arg
;
3253 * Checking against RTF_UP protects against walktree
3254 * recursion problems with cloned routes.
3257 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
3259 * Safe to drop rt_lock and use rt_key, rt_gateway,
3260 * since holding rnh_lock here prevents another thread
3261 * from calling rt_setgate() on this route.
3264 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
3265 rt_mask(rt
), rt
->rt_flags
, NULL
);
3267 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
3276 * Removes routing table reference to a given interface
3277 * for a given protocol family
3280 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
3282 struct radix_node_head
*rnh
;
3285 routegenid_update();
3286 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
3287 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
3288 lck_mtx_lock(rnh_lock
);
3289 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
3290 lck_mtx_unlock(rnh_lock
);
3295 if_rtmtu(struct radix_node
*rn
, void *arg
)
3297 struct rtentry
*rt
= (struct rtentry
*)rn
;
3298 struct ifnet
*ifp
= arg
;
3301 if (rt
->rt_ifp
== ifp
) {
3303 * Update the MTU of this entry only if the MTU
3304 * has not been locked (RTV_MTU is not set) and
3305 * if it was non-zero to begin with.
3307 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
3308 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
3316 * Update the MTU metric of all route entries in all protocol tables
3317 * associated with a particular interface; this is called when the
3318 * MTU of that interface has changed.
3321 void if_rtmtu_update(struct ifnet
*ifp
)
3323 struct radix_node_head
*rnh
;
3326 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
3327 if ((rnh
= rt_tables
[p
]) == NULL
)
3330 lck_mtx_lock(rnh_lock
);
3331 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
3332 lck_mtx_unlock(rnh_lock
);
3336 routegenid_update();
3339 __private_extern__
void
3340 if_data_internal_to_if_data(struct ifnet
*ifp
,
3341 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
3344 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
3345 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
3346 /* compiler will cast down to 32-bit */
3347 #define COPYFIELD32_ATOMIC(fld) do { \
3348 atomic_get_64(if_data->fld, \
3349 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
3352 COPYFIELD(ifi_type
);
3353 COPYFIELD(ifi_typelen
);
3354 COPYFIELD(ifi_physical
);
3355 COPYFIELD(ifi_addrlen
);
3356 COPYFIELD(ifi_hdrlen
);
3357 COPYFIELD(ifi_recvquota
);
3358 COPYFIELD(ifi_xmitquota
);
3359 if_data
->ifi_unused1
= 0;
3361 COPYFIELD(ifi_metric
);
3362 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
3363 if_data
->ifi_baudrate
= 0xFFFFFFFF;
3365 COPYFIELD32(ifi_baudrate
);
3368 COPYFIELD32_ATOMIC(ifi_ipackets
);
3369 COPYFIELD32_ATOMIC(ifi_ierrors
);
3370 COPYFIELD32_ATOMIC(ifi_opackets
);
3371 COPYFIELD32_ATOMIC(ifi_oerrors
);
3372 COPYFIELD32_ATOMIC(ifi_collisions
);
3373 COPYFIELD32_ATOMIC(ifi_ibytes
);
3374 COPYFIELD32_ATOMIC(ifi_obytes
);
3375 COPYFIELD32_ATOMIC(ifi_imcasts
);
3376 COPYFIELD32_ATOMIC(ifi_omcasts
);
3377 COPYFIELD32_ATOMIC(ifi_iqdrops
);
3378 COPYFIELD32_ATOMIC(ifi_noproto
);
3380 COPYFIELD(ifi_recvtiming
);
3381 COPYFIELD(ifi_xmittiming
);
3383 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
3384 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
3386 #if IF_LASTCHANGEUPTIME
3387 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
3390 if_data
->ifi_unused2
= 0;
3391 COPYFIELD(ifi_hwassist
);
3392 if_data
->ifi_reserved1
= 0;
3393 if_data
->ifi_reserved2
= 0;
3394 #undef COPYFIELD32_ATOMIC
3399 __private_extern__
void
3400 if_data_internal_to_if_data64(struct ifnet
*ifp
,
3401 const struct if_data_internal
*if_data_int
,
3402 struct if_data64
*if_data64
)
3405 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
3406 #define COPYFIELD64_ATOMIC(fld) do { \
3407 atomic_get_64(if_data64->fld, \
3408 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
3411 COPYFIELD64(ifi_type
);
3412 COPYFIELD64(ifi_typelen
);
3413 COPYFIELD64(ifi_physical
);
3414 COPYFIELD64(ifi_addrlen
);
3415 COPYFIELD64(ifi_hdrlen
);
3416 COPYFIELD64(ifi_recvquota
);
3417 COPYFIELD64(ifi_xmitquota
);
3418 if_data64
->ifi_unused1
= 0;
3419 COPYFIELD64(ifi_mtu
);
3420 COPYFIELD64(ifi_metric
);
3421 COPYFIELD64(ifi_baudrate
);
3423 COPYFIELD64_ATOMIC(ifi_ipackets
);
3424 COPYFIELD64_ATOMIC(ifi_ierrors
);
3425 COPYFIELD64_ATOMIC(ifi_opackets
);
3426 COPYFIELD64_ATOMIC(ifi_oerrors
);
3427 COPYFIELD64_ATOMIC(ifi_collisions
);
3428 COPYFIELD64_ATOMIC(ifi_ibytes
);
3429 COPYFIELD64_ATOMIC(ifi_obytes
);
3430 COPYFIELD64_ATOMIC(ifi_imcasts
);
3431 COPYFIELD64_ATOMIC(ifi_omcasts
);
3432 COPYFIELD64_ATOMIC(ifi_iqdrops
);
3433 COPYFIELD64_ATOMIC(ifi_noproto
);
3435 /* Note these two fields are actually 32 bit, so doing COPYFIELD64_ATOMIC will
3436 * cause them to be misaligned
3438 COPYFIELD64(ifi_recvtiming
);
3439 COPYFIELD64(ifi_xmittiming
);
3441 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
3442 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
3444 #if IF_LASTCHANGEUPTIME
3445 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
3451 __private_extern__
void
3452 if_copy_traffic_class(struct ifnet
*ifp
,
3453 struct if_traffic_class
*if_tc
)
3455 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
3456 atomic_get_64(if_tc->fld, \
3457 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
3460 bzero(if_tc
, sizeof (*if_tc
));
3461 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets
);
3462 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes
);
3463 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets
);
3464 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes
);
3465 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
3466 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
3467 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
3468 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
3469 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
3470 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
3471 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
3472 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
3473 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
3474 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
3475 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
3476 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
3477 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets
);
3478 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes
);
3479 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets
);
3480 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes
);
3482 #undef COPY_IF_TC_FIELD64_ATOMIC
3486 if_copy_data_extended(struct ifnet
*ifp
, struct if_data_extended
*if_de
)
3488 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
3489 atomic_get_64(if_de->fld, \
3490 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
3493 bzero(if_de
, sizeof (*if_de
));
3494 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs
);
3496 #undef COPY_IF_DE_FIELD64_ATOMIC
3500 if_copy_packet_stats(struct ifnet
*ifp
, struct if_packet_stats
*if_ps
)
3502 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
3503 atomic_get_64(if_ps->ifi_tcp_##fld, \
3504 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
3507 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
3508 atomic_get_64(if_ps->ifi_udp_##fld, \
3509 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
3512 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat
);
3513 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6
);
3514 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin
);
3515 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec
);
3516 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist
);
3517 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist
);
3518 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn
);
3519 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach
);
3520 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6
);
3521 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket
);
3522 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv
);
3523 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket
);
3524 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup
);
3525 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow
);
3527 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach
);
3528 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix
);
3529 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0
);
3530 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength
);
3531 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum
);
3532 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast
);
3533 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup
);
3534 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec
);
3536 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
3537 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
3541 if_copy_rxpoll_stats(struct ifnet
*ifp
, struct if_rxpoll_stats
*if_rs
)
3543 bzero(if_rs
, sizeof (*if_rs
));
3544 if (!(ifp
->if_eflags
& IFEF_RXPOLL
) || !ifnet_is_attached(ifp
, 1))
3547 /* by now, ifnet will stay attached so if_inp must be valid */
3548 VERIFY(ifp
->if_inp
!= NULL
);
3549 bcopy(&ifp
->if_inp
->pstats
, if_rs
, sizeof (*if_rs
));
3551 /* Release the IO refcnt */
3552 ifnet_decr_iorefcnt(ifp
);
3556 ifa_remref(struct ifaddr
*ifa
, int locked
)
3561 IFA_LOCK_ASSERT_HELD(ifa
);
3563 if (ifa
->ifa_refcnt
== 0)
3564 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
3565 else if (ifa
->ifa_trace
!= NULL
)
3566 (*ifa
->ifa_trace
)(ifa
, FALSE
);
3567 if (--ifa
->ifa_refcnt
== 0) {
3568 if (ifa
->ifa_debug
& IFD_ATTACHED
)
3569 panic("ifa %p attached to ifp is being freed\n", ifa
);
3571 * Some interface addresses are allocated either statically
3572 * or carved out of a larger block; e.g. AppleTalk addresses.
3573 * Only free it if it was allocated via MALLOC or via the
3574 * corresponding per-address family allocator. Otherwise,
3577 if (ifa
->ifa_debug
& IFD_ALLOC
) {
3578 if (ifa
->ifa_free
== NULL
) {
3580 FREE(ifa
, M_IFADDR
);
3582 /* Become a regular mutex */
3583 IFA_CONVERT_LOCK(ifa
);
3584 /* callee will unlock */
3585 (*ifa
->ifa_free
)(ifa
);
3593 if (!locked
&& ifa
!= NULL
)
3600 ifa_addref(struct ifaddr
*ifa
, int locked
)
3605 IFA_LOCK_ASSERT_HELD(ifa
);
3607 if (++ifa
->ifa_refcnt
== 0) {
3608 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
3610 } else if (ifa
->ifa_trace
!= NULL
) {
3611 (*ifa
->ifa_trace
)(ifa
, TRUE
);
3618 ifa_lock_init(struct ifaddr
*ifa
)
3620 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3624 ifa_lock_destroy(struct ifaddr
*ifa
)
3626 IFA_LOCK_ASSERT_NOTHELD(ifa
);
3627 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);