2 * Copyright (c) 2000-2008 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>
85 #include <machine/endian.h>
88 #include <net/if_arp.h>
89 #include <net/if_dl.h>
90 #include <net/if_types.h>
91 #include <net/if_var.h>
92 #include <net/net_osdep.h>
93 #include <net/ethernet.h>
95 #include <net/radix.h>
96 #include <net/route.h>
100 #include <sys/domain.h>
101 #include <libkern/OSAtomic.h>
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip_var.h>
110 #include <netinet6/in6_var.h>
111 #include <netinet6/in6_ifattach.h>
115 extern int dlil_multithreaded_input
;
116 extern struct dlil_threading_info
*dlil_lo_thread_ptr
;
119 #include <security/mac_framework.h>
124 * System initialization
127 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
128 static void if_qflush(struct ifqueue
*);
129 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
130 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
132 static int if_rtmtu(struct radix_node
*, void *);
133 static void if_rtmtu_update(struct ifnet
*);
135 static struct if_clone
*if_clone_lookup(const char *, int *);
137 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
138 #endif /* IF_CLONE_LIST */
140 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
141 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
143 int ifqmaxlen
= IFQ_MAXLEN
;
144 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
146 static int if_cloners_count
;
147 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
149 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
154 * XXX: declare here to avoid to include many inet6 related files..
155 * should be more generalized?
157 extern void nd6_setmtu(struct ifnet
*);
160 #define M_CLONE M_IFADDR
163 * Network interface utility routines.
165 * Routines with ifa_ifwith* names take sockaddr *'s as
170 struct ifaddr
**ifnet_addrs
;
171 struct ifnet
**ifindex2ifnet
;
173 __private_extern__
void
178 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
179 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
180 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
183 ifa
->ifa_debug
|= IFD_ATTACHED
;
184 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
187 __private_extern__
void
192 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
195 if ((ifa
->ifa_debug
& IFD_ATTACHED
) == 0) {
196 printf("if_detach_ifa: ifa is not attached to any interface! flags=%u\n", ifa
->ifa_debug
);
201 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
206 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
210 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
211 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
215 #define INITIAL_IF_INDEXLIM 8
218 * Function: if_next_index
220 * Return the next available interface index.
221 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
222 * added entry when necessary.
225 * ifnet_addrs[] is indexed by (if_index - 1), whereas
226 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
227 * always allocate one extra element to hold ifindex2ifnet[0], which
230 int if_next_index(void);
232 __private_extern__
int
235 static int if_indexlim
= 0;
238 new_index
= ++if_index
;
239 if (if_index
> if_indexlim
) {
242 caddr_t new_ifnet_addrs
;
243 caddr_t new_ifindex2ifnet
;
244 caddr_t old_ifnet_addrs
;
246 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
247 if (ifnet_addrs
== NULL
) {
248 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
250 new_if_indexlim
= if_indexlim
<< 1;
253 /* allocate space for the larger arrays */
254 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
255 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
256 if (new_ifnet_addrs
== NULL
) {
261 new_ifindex2ifnet
= new_ifnet_addrs
262 + new_if_indexlim
* sizeof(caddr_t
);
263 bzero(new_ifnet_addrs
, n
);
264 if (ifnet_addrs
!= NULL
) {
265 /* copy the existing data */
266 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
267 if_indexlim
* sizeof(caddr_t
));
268 bcopy((caddr_t
)ifindex2ifnet
,
270 (if_indexlim
+ 1) * sizeof(caddr_t
));
273 /* switch to the new tables and size */
274 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
275 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
276 if_indexlim
= new_if_indexlim
;
278 /* release the old data */
279 if (old_ifnet_addrs
!= NULL
) {
280 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
287 * Create a clone network interface.
290 if_clone_create(char *name
, int len
)
292 struct if_clone
*ifc
;
294 int wildcard
, bytoff
, bitoff
;
298 ifc
= if_clone_lookup(name
, &unit
);
302 if (ifunit(name
) != NULL
)
306 wildcard
= (unit
< 0);
308 * Find a free unit if none was given.
311 while ((bytoff
< ifc
->ifc_bmlen
)
312 && (ifc
->ifc_units
[bytoff
] == 0xff))
314 if (bytoff
>= ifc
->ifc_bmlen
)
316 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
318 unit
= (bytoff
<< 3) + bitoff
;
321 if (unit
> ifc
->ifc_maxunit
)
324 err
= (*ifc
->ifc_create
)(ifc
, unit
);
330 bitoff
= unit
- (bytoff
<< 3);
334 * Allocate the unit in the bitmap.
336 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
337 ("%s: bit is already set", __func__
));
338 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
340 /* In the wildcard case, we need to update the name. */
342 for (dp
= name
; *dp
!= '\0'; dp
++);
343 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
344 len
- (dp
-name
) - 1) {
346 * This can only be a programmer error and
347 * there's no straightforward way to recover if
350 panic("if_clone_create(): interface name too long");
359 * Destroy a clone network interface.
362 if_clone_destroy(const char *name
)
364 struct if_clone
*ifc
;
369 ifc
= if_clone_lookup(name
, &unit
);
373 if (unit
< ifc
->ifc_minifs
)
380 if (ifc
->ifc_destroy
== NULL
)
383 (*ifc
->ifc_destroy
)(ifp
);
386 * Compute offset in the bitmap and deallocate the unit.
389 bitoff
= unit
- (bytoff
<< 3);
390 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
391 ("%s: bit is already cleared", __func__
));
392 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
397 * Look up a network interface cloner.
400 static struct if_clone
*
401 if_clone_lookup(const char *name
, int *unitp
)
403 struct if_clone
*ifc
;
407 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
408 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
409 if (ifc
->ifc_name
[i
] != *cp
)
414 ifc
= LIST_NEXT(ifc
, ifc_list
);
418 return ((struct if_clone
*)NULL
);
424 for (i
= 0; *cp
!= '\0'; cp
++) {
425 if (*cp
< '0' || *cp
> '9') {
426 /* Bogus unit number. */
429 i
= (i
* 10) + (*cp
- '0');
439 * Register a network interface cloner.
442 if_clone_attach(struct if_clone
*ifc
)
449 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
450 ("%s: %s requested more units then allowed (%d > %d)",
451 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
452 ifc
->ifc_maxunit
+ 1));
454 * Compute bitmap size and allocate it.
456 maxclone
= ifc
->ifc_maxunit
+ 1;
458 if ((len
<< 3) < maxclone
)
460 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
461 if (ifc
->ifc_units
== NULL
)
463 bzero(ifc
->ifc_units
, len
);
464 ifc
->ifc_bmlen
= len
;
466 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
469 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
470 err
= (*ifc
->ifc_create
)(ifc
, unit
);
472 ("%s: failed to create required interface %s%d",
473 __func__
, ifc
->ifc_name
, unit
));
475 /* Allocate the unit in the bitmap. */
477 bitoff
= unit
- (bytoff
<< 3);
478 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
485 * Unregister a network interface cloner.
488 if_clone_detach(struct if_clone
*ifc
)
491 LIST_REMOVE(ifc
, ifc_list
);
492 FREE(ifc
->ifc_units
, M_CLONE
);
498 * Provide list of interface cloners to userspace.
501 if_clone_list(int count
, int * total
, user_addr_t dst
)
503 char outbuf
[IFNAMSIZ
];
504 struct if_clone
*ifc
;
507 *total
= if_cloners_count
;
508 if (dst
== USER_ADDR_NULL
) {
509 /* Just asking how many there are. */
516 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
518 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
519 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
520 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
521 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
528 #endif /* IF_CLONE_LIST */
531 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
532 * and that it matches only the local (not broadcast) address.
534 __private_extern__
struct in_ifaddr
*
535 ifa_foraddr(unsigned int addr
)
537 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
541 * Similar to ifa_foraddr, except with the added interface scope
542 * constraint (unless the caller passes in IFSCOPE_NONE in which
543 * case there is no scope restriction).
545 __private_extern__
struct in_ifaddr
*
546 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
548 struct in_ifaddr
*ia
= NULL
;
550 lck_rw_lock_shared(in_ifaddr_rwlock
);
551 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
552 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
553 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
))
558 lck_rw_done(in_ifaddr_rwlock
);
563 * Return the first (primary) address of a given family on an interface.
565 __private_extern__
struct ifaddr
*
566 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
568 struct ifaddr
*ifa0
= NULL
, *ifa
;
570 ifnet_lock_shared(ifp
);
571 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
572 if (ifa
->ifa_addr
->sa_family
== family
&& ifa0
== NULL
) {
579 ifnet_lock_done(ifp
);
585 * Locate an interface based on a complete address.
590 const struct sockaddr
*addr
)
594 struct ifaddr
*result
= NULL
;
596 #define equal(a1, a2) \
597 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
599 ifnet_head_lock_shared();
600 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
601 ifnet_lock_shared(ifp
);
602 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
603 ifa
= ifa
->ifa_link
.tqe_next
) {
604 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
606 if (equal(addr
, ifa
->ifa_addr
)) {
610 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
611 /* IP6 doesn't have broadcast */
612 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
613 equal(ifa
->ifa_broadaddr
, addr
)) {
620 ifnet_lock_done(ifp
);
627 * Locate the point to point interface with a given destination address.
632 const struct sockaddr
*addr
)
636 struct ifaddr
*result
= NULL
;
638 ifnet_head_lock_shared();
639 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
640 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
641 ifnet_lock_shared(ifp
);
642 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
643 ifa
= ifa
->ifa_link
.tqe_next
) {
644 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
646 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
653 ifnet_lock_done(ifp
);
661 * Locate the source address of an interface based on a complete address.
664 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
666 struct ifaddr
*result
= NULL
;
669 if (ifscope
== IFSCOPE_NONE
)
670 return (ifa_ifwithaddr(addr
));
672 ifnet_head_lock_shared();
673 if (ifscope
> (unsigned int)if_index
) {
678 ifp
= ifindex2ifnet
[ifscope
];
680 struct ifaddr
*ifa
= NULL
;
683 * This is suboptimal; there should be a better way
684 * to search for a given address of an interface
685 * for any given address family.
687 ifnet_lock_shared(ifp
);
688 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
689 ifa
= ifa
->ifa_link
.tqe_next
) {
690 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
692 if (equal(addr
, ifa
->ifa_addr
)) {
696 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
697 ifa
->ifa_broadaddr
!= NULL
&&
698 /* IP6 doesn't have broadcast */
699 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
700 equal(ifa
->ifa_broadaddr
, addr
)) {
707 ifnet_lock_done(ifp
);
715 ifa_ifwithnet(const struct sockaddr
*addr
)
717 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
721 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
723 return (ifa_ifwithnet_common(addr
, ifscope
));
727 * Find an interface on a specific network. If many, choice
728 * is most specific found.
730 static struct ifaddr
*
731 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
734 struct ifaddr
*ifa
= NULL
;
735 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
736 u_int af
= addr
->sa_family
;
737 const char *addr_data
= addr
->sa_data
, *cplim
;
739 if (!ip_doscopedroute
|| addr
->sa_family
!= AF_INET
)
740 ifscope
= IFSCOPE_NONE
;
742 ifnet_head_lock_shared();
744 * AF_LINK addresses can be looked up directly by their index number,
745 * so do that if we can.
748 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
749 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
750 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
761 * Scan though each interface, looking for ones that have
762 * addresses in this address family.
764 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
765 ifnet_lock_shared(ifp
);
766 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
767 ifa
= ifa
->ifa_link
.tqe_next
) {
768 const char *cp
, *cp2
, *cp3
;
770 if (ifa
->ifa_addr
->sa_family
!= af
)
773 /* This breaks tunneling application trying to install a route with
774 * a specific subnet and the local address as the destination
775 * It's breaks binary compatibility with previous version of MacOS X
779 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
780 addr
->sa_family
!= AF_INET6
&&
782 ifp
->if_flags
& IFF_POINTOPOINT
) {
784 * This is a bit broken as it doesn't
785 * take into account that the remote end may
786 * be a single node in the network we are
788 * The trouble is that we don't know the
789 * netmask for the remote end.
791 if (ifa
->ifa_dstaddr
!= 0
792 && equal(addr
, ifa
->ifa_dstaddr
)) {
796 #endif /* __APPLE__*/
799 * If we're looking up with a scope,
800 * find using a matching interface.
802 if (ifscope
!= IFSCOPE_NONE
&&
803 ifp
->if_index
!= ifscope
)
807 * Scan all the bits in the ifa's address.
808 * If a bit dissagrees with what we are
809 * looking for, mask it with the netmask
810 * to see if it really matters.
813 if (ifa
->ifa_netmask
== 0)
816 cp2
= ifa
->ifa_addr
->sa_data
;
817 cp3
= ifa
->ifa_netmask
->sa_data
;
818 cplim
= ifa
->ifa_netmask
->sa_len
819 + (char *)ifa
->ifa_netmask
;
821 if ((*cp
++ ^ *cp2
++) & *cp3
++)
822 goto next
; /* next address! */
824 * If the netmask of what we just found
825 * is more specific than what we had before
826 * (if we had one) then remember the new one
827 * before continuing to search
828 * for an even better one.
830 if (ifa_maybe
== 0 ||
831 rn_refines((caddr_t
)ifa
->ifa_netmask
,
832 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
846 * ifa is set if we found an exact match.
847 * take a reference to the ifa before
848 * releasing the ifp lock
850 ifnet_lock_done(ifp
);
859 else if (ifa_maybe
) {
867 * Find an interface address specific to an interface best matching
872 const struct sockaddr
*addr
,
875 struct ifaddr
*ifa
= NULL
;
876 const char *cp
, *cp2
, *cp3
;
878 struct ifaddr
*ifa_maybe
= NULL
;
879 struct ifaddr
*better_ifa_maybe
= NULL
;
880 u_int af
= addr
->sa_family
;
885 ifnet_lock_shared(ifp
);
886 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
887 ifa
= ifa
->ifa_link
.tqe_next
) {
888 if (ifa
->ifa_addr
->sa_family
!= af
)
892 if (ifa
->ifa_netmask
== 0) {
893 if (equal(addr
, ifa
->ifa_addr
) ||
894 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
898 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
899 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
))
902 if (equal(addr
, ifa
->ifa_addr
)) {
907 cp2
= ifa
->ifa_addr
->sa_data
;
908 cp3
= ifa
->ifa_netmask
->sa_data
;
909 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
910 for (; cp3
< cplim
; cp3
++)
911 if ((*cp
++ ^ *cp2
++) & *cp3
)
915 if (better_ifa_maybe
== NULL
) {
916 better_ifa_maybe
= ifa
;
923 if (better_ifa_maybe
!= NULL
) {
924 ifa
= better_ifa_maybe
;
929 if (ifa
) ifaref(ifa
);
931 ifnet_lock_done(ifp
);
935 #include <net/route.h>
938 * Default action when installing a route with a Link Level gateway.
939 * Lookup an appropriate real ifa to point to.
940 * This should be moved to /sys/net/link.c eventually.
943 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
946 struct sockaddr
*dst
;
949 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
950 RT_LOCK_ASSERT_HELD(rt
);
952 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
953 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
955 ifa
= ifaof_ifpforaddr(dst
, ifp
);
958 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
959 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
965 * if_updown will set the interface up or down. It will
966 * prevent other up/down events from occurring until this
967 * up/down event has completed.
969 * Caller must lock ifnet. This function will drop the
970 * lock. This allows ifnet_set_flags to set the rest of
971 * the flags after we change the up/down state without
972 * dropping the interface lock between setting the
973 * up/down state and updating the rest of the flags.
975 __private_extern__
void
984 /* Wait until no one else is changing the up/down state */
985 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
987 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
988 ifnet_lock_done(ifp
);
989 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
990 ifnet_lock_exclusive(ifp
);
993 /* Verify that the interface isn't already in the right state */
994 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
995 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
999 /* Indicate that the up/down state is changing */
1000 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1002 /* Mark interface up or down */
1004 ifp
->if_flags
|= IFF_UP
;
1007 ifp
->if_flags
&= ~IFF_UP
;
1010 ifnet_touch_lastchange(ifp
);
1012 /* Drop the lock to notify addresses and route */
1013 ifnet_lock_done(ifp
);
1014 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1015 for (i
= 0; ifa
[i
] != 0; i
++) {
1016 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1018 ifnet_free_address_list(ifa
);
1022 /* Aquire the lock to clear the changing flag and flush the send queue */
1023 ifnet_lock_exclusive(ifp
);
1025 if_qflush(&ifp
->if_snd
);
1026 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1027 wakeup(&ifp
->if_eflags
);
1033 * Mark an interface down and notify protocols of
1040 ifnet_lock_exclusive(ifp
);
1042 ifnet_lock_done(ifp
);
1046 * Mark an interface up and notify protocols of
1053 ifnet_lock_exclusive(ifp
);
1055 ifnet_lock_done(ifp
);
1059 * Flush an interface queue.
1062 if_qflush(struct ifqueue
*ifq
)
1067 while ((m
= n
) != 0) {
1071 ifq
->ifq_head
= NULL
;
1072 ifq
->ifq_tail
= NULL
;
1077 * Map interface name to
1078 * interface structure pointer.
1081 ifunit(const char *name
)
1083 char namebuf
[IFNAMSIZ
+ 1];
1091 if (len
< 2 || len
> IFNAMSIZ
)
1093 cp
= name
+ len
- 1;
1095 if (c
< '0' || c
> '9')
1096 return NULL
; /* trailing garbage */
1101 return NULL
; /* no interface name */
1102 unit
+= (c
- '0') * m
;
1104 return NULL
; /* number is unreasonable */
1107 } while (c
>= '0' && c
<= '9');
1108 len
= cp
- name
+ 1;
1109 bcopy(name
, namebuf
, len
);
1110 namebuf
[len
] = '\0';
1112 * Now search all the interfaces for this name/number
1114 ifnet_head_lock_shared();
1115 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1116 if (strncmp(ifp
->if_name
, namebuf
, len
))
1118 if (unit
== ifp
->if_unit
)
1127 * Map interface name in a sockaddr_dl to
1128 * interface structure pointer.
1131 if_withname(struct sockaddr
*sa
)
1133 char ifname
[IFNAMSIZ
+1];
1134 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1136 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1137 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1141 * ifunit wants a null-terminated name. It may not be null-terminated
1142 * in the sockaddr. We don't want to change the caller's sockaddr,
1143 * and there might not be room to put the trailing null anyway, so we
1144 * make a local copy that we know we can null terminate safely.
1147 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1148 ifname
[sdl
->sdl_nlen
] = '\0';
1149 return ifunit(ifname
);
1157 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1164 struct kev_msg ev_msg
;
1165 struct net_event_data ev_data
;
1168 case OSIOCGIFCONF32
:
1169 case SIOCGIFCONF32
: {
1170 struct ifconf32
*ifc
= (struct ifconf32
*)data
;
1171 return (ifconf(cmd
, CAST_USER_ADDR_T(ifc
->ifc_req
),
1176 case OSIOCGIFCONF64
: {
1177 struct ifconf64
*ifc
= (struct ifconf64
*)data
;
1178 return (ifconf(cmd
, ifc
->ifc_req
, &ifc
->ifc_len
));
1182 ifr
= (struct ifreq
*)data
;
1186 error
= proc_suser(p
);
1189 return ((cmd
== SIOCIFCREATE
) ?
1190 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1191 if_clone_destroy(ifr
->ifr_name
));
1193 case SIOCIFGCLONERS32
: {
1194 struct if_clonereq32
*ifcr
= (struct if_clonereq32
*)data
;
1195 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1196 CAST_USER_ADDR_T(ifcr
->ifcru_buffer
)));
1200 case SIOCIFGCLONERS64
: {
1201 struct if_clonereq64
*ifcr
= (struct if_clonereq64
*)data
;
1202 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1203 ifcr
->ifcru_buffer
));
1206 #endif /* IF_CLONE_LIST */
1209 ifp
= ifunit(ifr
->ifr_name
);
1215 ifnet_lock_shared(ifp
);
1216 ifr
->ifr_flags
= ifp
->if_flags
;
1217 ifnet_lock_done(ifp
);
1222 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
1228 ifnet_lock_shared(ifp
);
1229 ifr
->ifr_metric
= ifp
->if_metric
;
1230 ifnet_lock_done(ifp
);
1234 ifnet_lock_shared(ifp
);
1235 ifr
->ifr_mtu
= ifp
->if_mtu
;
1236 ifnet_lock_done(ifp
);
1240 ifnet_lock_shared(ifp
);
1241 ifr
->ifr_phys
= ifp
->if_physical
;
1242 ifnet_lock_done(ifp
);
1246 error
= proc_suser(p
);
1250 ifnet_set_flags(ifp
, ifr
->ifr_flags
, (u_int16_t
)~IFF_CANTCHANGE
);
1252 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1255 /* Send the event even upon error from the driver because we changed the flags */
1256 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1257 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1258 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1260 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1261 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1262 ev_data
.if_family
= ifp
->if_family
;
1263 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1264 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1265 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1266 ev_msg
.dv
[1].data_length
= 0;
1267 kev_post_msg(&ev_msg
);
1269 ifnet_touch_lastchange(ifp
);
1274 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
1280 error
= proc_suser(p
);
1283 ifp
->if_metric
= ifr
->ifr_metric
;
1286 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1287 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1288 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1290 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1291 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1292 ev_data
.if_family
= ifp
->if_family
;
1293 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1294 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1295 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1297 ev_msg
.dv
[1].data_length
= 0;
1298 kev_post_msg(&ev_msg
);
1300 ifnet_touch_lastchange(ifp
);
1304 error
= proc_suser(p
);
1308 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1312 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1313 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1314 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1316 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1317 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1318 ev_data
.if_family
= ifp
->if_family
;
1319 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1320 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1321 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1322 ev_msg
.dv
[1].data_length
= 0;
1323 kev_post_msg(&ev_msg
);
1325 ifnet_touch_lastchange(ifp
);
1331 u_int32_t oldmtu
= ifp
->if_mtu
;
1333 error
= proc_suser(p
);
1336 if (ifp
->if_ioctl
== NULL
)
1337 return (EOPNOTSUPP
);
1338 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1341 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1345 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1346 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1347 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1349 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1350 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1351 ev_data
.if_family
= ifp
->if_family
;
1352 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1353 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1354 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1355 ev_msg
.dv
[1].data_length
= 0;
1356 kev_post_msg(&ev_msg
);
1358 ifnet_touch_lastchange(ifp
);
1362 * If the link MTU changed, do network layer specific procedure
1363 * and update all route entries associated with the interface,
1364 * so that their MTU metric gets updated.
1366 if (error
== 0 && ifp
->if_mtu
!= oldmtu
) {
1367 if_rtmtu_update(ifp
);
1377 error
= proc_suser(p
);
1381 /* Don't allow group membership on non-multicast interfaces. */
1382 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1386 /* Don't let users screw up protocols' entries. */
1387 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1391 if (cmd
== SIOCADDMULTI
) {
1392 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1393 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1395 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1396 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1399 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1400 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1401 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1402 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1404 ev_data
.if_family
= ifp
->if_family
;
1405 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1406 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1407 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1408 ev_msg
.dv
[1].data_length
= 0;
1409 kev_post_msg(&ev_msg
);
1411 ifnet_touch_lastchange(ifp
);
1415 case SIOCSIFPHYADDR
:
1416 case SIOCDIFPHYADDR
:
1418 case SIOCSIFPHYADDR_IN6_32
:
1419 case SIOCSIFPHYADDR_IN6_64
:
1421 case SIOCSLIFPHYADDR
:
1423 case SIOCSIFGENERIC
:
1428 error
= proc_suser(p
);
1432 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1436 ifnet_touch_lastchange(ifp
);
1440 ifs
= (struct ifstat
*)data
;
1441 ifs
->ascii
[0] = '\0';
1443 case SIOCGIFPSRCADDR
:
1444 case SIOCGIFPDSTADDR
:
1445 case SIOCGLIFPHYADDR
:
1446 case SIOCGIFMEDIA32
:
1447 case SIOCGIFMEDIA64
:
1448 case SIOCGIFGENERIC
:
1450 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1454 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1457 case SIOCGIFWAKEFLAGS
:
1458 ifnet_lock_shared(ifp
);
1459 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
1460 ifnet_lock_done(ifp
);
1464 oif_flags
= ifp
->if_flags
;
1465 if (so
->so_proto
== 0)
1466 return (EOPNOTSUPP
);
1472 case SIOCSIFDSTADDR
:
1474 case SIOCSIFBRDADDR
:
1475 case SIOCSIFNETMASK
:
1476 #if BYTE_ORDER != BIG_ENDIAN
1477 if (ifr
->ifr_addr
.sa_family
== 0 &&
1478 ifr
->ifr_addr
.sa_len
< 16) {
1479 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1480 ifr
->ifr_addr
.sa_len
= 16;
1483 if (ifr
->ifr_addr
.sa_len
== 0)
1484 ifr
->ifr_addr
.sa_len
= 16;
1492 case OSIOCGIFDSTADDR
:
1493 cmd
= SIOCGIFDSTADDR
;
1496 case OSIOCGIFBRDADDR
:
1497 cmd
= SIOCGIFBRDADDR
;
1500 case OSIOCGIFNETMASK
:
1501 cmd
= SIOCGIFNETMASK
;
1504 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1506 socket_unlock(so
, 1);
1510 case OSIOCGIFDSTADDR
:
1511 case OSIOCGIFBRDADDR
:
1512 case OSIOCGIFNETMASK
:
1513 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1517 if (cmd
== SIOCSIFKPI
) {
1518 int temperr
= proc_suser(p
);
1523 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1524 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1533 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1537 socket_unlock(so
, 0);
1538 error
= ifioctl(so
, cmd
, data
, p
);
1544 * Set/clear promiscuous mode on interface ifp based on the truth value
1545 * of pswitch. The calls are reference counted so that only the first
1546 * "on" request actually has an effect, as does the final "off" request.
1547 * Results are undefined if the "off" and "on" requests are not matched.
1550 ifnet_set_promiscuous(
1560 ifnet_lock_exclusive(ifp
);
1562 oldflags
= ifp
->if_flags
;
1565 * If the device is not configured up, we cannot put it in
1568 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1572 if (ifp
->if_pcount
++ != 0) {
1575 ifp
->if_flags
|= IFF_PROMISC
;
1577 if (--ifp
->if_pcount
> 0)
1579 ifp
->if_flags
&= ~IFF_PROMISC
;
1581 ifr
.ifr_flags
= ifp
->if_flags
;
1583 ifnet_lock_done(ifp
);
1584 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, &ifr
);
1588 ifp
->if_flags
= oldflags
;
1590 if (locked
) ifnet_lock_done(ifp
);
1592 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1593 ifp
->if_name
, ifp
->if_unit
,
1594 pswitch
!= 0 ? "enabled" : "disabled");
1600 * Return interface configuration
1601 * of system. List may be used
1602 * in later ioctl's (above) to get
1603 * other information.
1607 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1609 struct ifnet
*ifp
= NULL
;
1616 * Zero the ifr buffer to make sure we don't
1617 * disclose the contents of the stack.
1619 bzero(&ifr
, sizeof(struct ifreq
));
1622 ifnet_head_lock_shared();
1623 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1625 size_t ifnlen
, addrs
;
1627 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1628 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1629 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1630 error
= ENAMETOOLONG
;
1633 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
1636 ifnet_lock_shared(ifp
);
1639 ifa
= ifp
->if_addrhead
.tqh_first
;
1640 for ( ; space
> sizeof (ifr
) && ifa
;
1641 ifa
= ifa
->ifa_link
.tqe_next
) {
1642 struct sockaddr
*sa
= ifa
->ifa_addr
;
1644 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1648 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
1649 struct osockaddr
*osa
=
1650 (struct osockaddr
*)&ifr
.ifr_addr
;
1652 osa
->sa_family
= sa
->sa_family
;
1653 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1654 ifrp
+= sizeof(struct ifreq
);
1655 } else if (sa
->sa_len
<= sizeof(*sa
)) {
1657 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1658 ifrp
+= sizeof(struct ifreq
);
1660 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1662 space
-= sa
->sa_len
- sizeof(*sa
);
1663 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1665 error
= copyout((caddr_t
)sa
,
1666 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1669 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1673 space
-= sizeof (ifr
);
1675 ifnet_lock_done(ifp
);
1680 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1681 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1684 space
-= sizeof (ifr
);
1685 ifrp
+= sizeof(struct ifreq
);
1689 *ret_space
-= space
;
1694 * Just like if_promisc(), but for all-multicast-reception mode.
1697 if_allmulti(struct ifnet
*ifp
, int onswitch
)
1702 ifnet_lock_exclusive(ifp
);
1705 if (ifp
->if_amcount
++ == 0) {
1706 ifp
->if_flags
|= IFF_ALLMULTI
;
1710 if (ifp
->if_amcount
> 1) {
1713 ifp
->if_amcount
= 0;
1714 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1718 ifnet_lock_done(ifp
);
1721 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
1730 struct ifmultiaddr
*ifma
)
1732 if (OSIncrementAtomic(&ifma
->ifma_refcount
) <= 0)
1733 panic("ifma_reference: ifma already released or invalid\n");
1738 struct ifmultiaddr
*ifma
)
1741 struct ifmultiaddr
*next
;
1742 int32_t prevValue
= OSDecrementAtomic(&ifma
->ifma_refcount
);
1744 panic("ifma_release: ifma already released or invalid\n");
1748 /* Allow the allocator of the protospec to free it */
1749 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1750 ifma
->ifma_free(ifma
->ifma_protospec
);
1753 next
= ifma
->ifma_ll
;
1754 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1755 FREE(ifma
, M_IFMADDR
);
1761 * Find an ifmultiaddr that matches a socket address on an interface.
1763 * Caller is responsible for holding the ifnet_lock while calling
1767 if_addmulti_doesexist(
1769 const struct sockaddr
*sa
,
1770 struct ifmultiaddr
**retifma
)
1772 struct ifmultiaddr
*ifma
;
1773 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1774 ifma
= ifma
->ifma_link
.le_next
) {
1775 if (equal(sa
, ifma
->ifma_addr
)) {
1776 ifma
->ifma_usecount
++;
1779 ifma_reference(*retifma
);
1789 * Radar 3642395, make sure all multicasts are in a standard format.
1791 static struct sockaddr
*
1793 const struct sockaddr
*original
)
1796 const u_char
*aptr
= NULL
;
1797 struct sockaddr
*copy
= NULL
;
1798 struct sockaddr_dl
*sdl_new
= NULL
;
1801 if (original
->sa_family
!= AF_LINK
&&
1802 original
->sa_family
!= AF_UNSPEC
) {
1803 /* Just make a copy */
1804 MALLOC(copy
, struct sockaddr
*, original
->sa_len
, M_IFADDR
, M_WAITOK
);
1806 bcopy(original
, copy
, original
->sa_len
);
1810 switch (original
->sa_family
) {
1812 const struct sockaddr_dl
*sdl_original
=
1813 (const struct sockaddr_dl
*)original
;
1815 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+ sdl_original
->sdl_slen
+
1816 offsetof(struct sockaddr_dl
, sdl_data
) > sdl_original
->sdl_len
)
1819 alen
= sdl_original
->sdl_alen
;
1820 aptr
= CONST_LLADDR(sdl_original
);
1825 if (original
->sa_len
< ETHER_ADDR_LEN
+
1826 offsetof(struct sockaddr
, sa_data
)) {
1830 alen
= ETHER_ADDR_LEN
;
1831 aptr
= (const u_char
*)original
->sa_data
;
1836 if (alen
== 0 || aptr
== NULL
)
1839 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
1840 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
1842 if (sdl_new
!= NULL
) {
1843 bzero(sdl_new
, len
);
1844 sdl_new
->sdl_len
= len
;
1845 sdl_new
->sdl_family
= AF_LINK
;
1846 sdl_new
->sdl_alen
= alen
;
1847 bcopy(aptr
, LLADDR(sdl_new
), alen
);
1850 return (struct sockaddr
*)sdl_new
;
1854 * Add a multicast listenership to the interface in question.
1855 * The link layer provides a routine which converts
1859 struct ifnet
*ifp
, /* interface to manipulate */
1860 const struct sockaddr
*sa
, /* address to add */
1861 struct ifmultiaddr
**retifma
)
1863 struct sockaddr_storage storage
;
1864 struct sockaddr
*llsa
= NULL
;
1865 struct sockaddr
*dupsa
= NULL
;
1867 struct ifmultiaddr
*ifma
= NULL
;
1868 struct ifmultiaddr
*llifma
= NULL
;
1870 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
1871 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
1872 dupsa
= copy_and_normalize(sa
);
1873 if (dupsa
== NULL
) {
1879 ifnet_lock_exclusive(ifp
);
1880 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1881 ifnet_lock_done(ifp
);
1888 * Give the link layer a chance to accept/reject it, and also
1889 * find out which AF_LINK address this maps to, if it isn't one
1892 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
1894 if (error
== 0 && storage
.ss_len
!= 0) {
1895 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
1901 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1902 if (llifma
== NULL
) {
1908 /* to be similar to FreeBSD */
1909 if (error
== EOPNOTSUPP
) {
1916 /* Allocate while we aren't holding any locks */
1917 if (dupsa
== NULL
) {
1918 dupsa
= copy_and_normalize(sa
);
1919 if (dupsa
== NULL
) {
1924 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1930 ifnet_lock_exclusive(ifp
);
1932 * Check again for the matching multicast.
1934 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1935 ifnet_lock_done(ifp
);
1939 bzero(ifma
, sizeof(*ifma
));
1940 ifma
->ifma_addr
= dupsa
;
1941 ifma
->ifma_ifp
= ifp
;
1942 ifma
->ifma_usecount
= 1;
1943 ifma
->ifma_refcount
= 1;
1946 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1947 FREE(llsa
, M_IFMADDR
);
1948 FREE(llifma
, M_IFMADDR
);
1950 bzero(llifma
, sizeof(*llifma
));
1951 llifma
->ifma_addr
= llsa
;
1952 llifma
->ifma_ifp
= ifp
;
1953 llifma
->ifma_usecount
= 1;
1954 llifma
->ifma_refcount
= 1;
1955 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1957 ifma
->ifma_ll
= llifma
;
1958 ifma_reference(ifma
->ifma_ll
);
1962 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1966 ifma_reference(*retifma
);
1969 ifnet_lock_done(ifp
);
1972 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1975 * We are certain we have added something, so call down to the
1976 * interface to let them know about it.
1978 ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
1984 FREE(ifma
, M_IFADDR
);
1986 FREE(dupsa
, M_IFADDR
);
1988 FREE(llifma
, M_IFADDR
);
1990 FREE(llsa
, M_IFADDR
);
1997 struct ifmultiaddr
*ifma
,
2001 int do_del_multi
= 0;
2003 ifp
= ifma
->ifma_ifp
;
2005 if (!locked
&& ifp
) {
2006 ifnet_lock_exclusive(ifp
);
2009 while (ifma
!= NULL
) {
2010 struct ifmultiaddr
*ll_ifma
;
2012 if (ifma
->ifma_usecount
> 1) {
2013 ifma
->ifma_usecount
--;
2018 LIST_REMOVE(ifma
, ifma_link
);
2020 ll_ifma
= ifma
->ifma_ll
;
2022 if (ll_ifma
) { /* send a routing msg for network addresses only */
2024 ifnet_lock_done(ifp
);
2025 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2027 ifnet_lock_exclusive(ifp
);
2031 * Make sure the interface driver is notified
2032 * in the case of a link layer mcast group being left.
2035 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
2046 if (!locked
&& ifp
) {
2047 /* This wasn't initially locked, we should unlock it */
2048 ifnet_lock_done(ifp
);
2053 ifnet_lock_done(ifp
);
2054 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
2056 ifnet_lock_exclusive(ifp
);
2063 * Remove a reference to a multicast address on this interface. Yell
2064 * if the request does not match an existing membership.
2069 const struct sockaddr
*sa
)
2071 struct ifmultiaddr
*ifma
;
2072 struct sockaddr
*dupsa
= NULL
;
2075 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
2076 dupsa
= copy_and_normalize(sa
);
2077 if (dupsa
== NULL
) {
2083 ifnet_lock_exclusive(ifp
);
2084 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2085 ifma
= ifma
->ifma_link
.le_next
)
2086 if (equal(sa
, ifma
->ifma_addr
))
2089 ifnet_lock_done(ifp
);
2091 FREE(dupsa
, M_IFADDR
);
2095 retval
= if_delmultiaddr(ifma
, 1);
2096 ifnet_lock_done(ifp
);
2098 FREE(dupsa
, M_IFADDR
);
2105 * We don't use if_setlladdr, our interfaces are responsible for
2106 * handling the SIOCSIFLLADDR ioctl.
2110 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2116 struct ifmultiaddr
*
2117 ifmaof_ifpforaddr(const struct sockaddr
*sa
, struct ifnet
*ifp
)
2119 struct ifmultiaddr
*ifma
;
2121 ifnet_lock_shared(ifp
);
2122 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2123 ifma
= ifma
->ifma_link
.le_next
)
2124 if (equal(ifma
->ifma_addr
, sa
))
2126 ifnet_lock_done(ifp
);
2131 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
2132 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Generic link-management");
2136 * Shutdown all network activity. Used boot() when halting
2146 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
2147 for (i
= 0; i
< count
; i
++) {
2149 dlil_proto_unplumb_all(ifp
[i
]);
2151 ifnet_list_free(ifp
);
2158 * Delete Routes for a Network Interface
2160 * Called for each routing entry via the rnh->rnh_walktree() call above
2161 * to delete all route entries referencing a detaching network interface.
2164 * rn pointer to node in the routing table
2165 * arg argument passed to rnh->rnh_walktree() - detaching interface
2169 * errno failed - reason indicated
2173 if_rtdel(struct radix_node
*rn
, void *arg
)
2175 struct rtentry
*rt
= (struct rtentry
*)rn
;
2176 struct ifnet
*ifp
= arg
;
2182 * Checking against RTF_UP protects against walktree
2183 * recursion problems with cloned routes.
2186 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
2188 * Safe to drop rt_lock and use rt_key, rt_gateway,
2189 * since holding rnh_lock here prevents another thread
2190 * from calling rt_setgate() on this route.
2193 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
2194 rt_mask(rt
), rt
->rt_flags
, NULL
);
2196 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
2205 * Removes routing table reference to a given interface
2206 * for a given protocol family
2209 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
2211 struct radix_node_head
*rnh
;
2214 routegenid_update();
2215 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
2216 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
2217 lck_mtx_lock(rnh_lock
);
2218 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
2219 lck_mtx_unlock(rnh_lock
);
2224 if_rtmtu(struct radix_node
*rn
, void *arg
)
2226 struct rtentry
*rt
= (struct rtentry
*)rn
;
2227 struct ifnet
*ifp
= arg
;
2230 if (rt
->rt_ifp
== ifp
) {
2232 * Update the MTU of this entry only if the MTU
2233 * has not been locked (RTV_MTU is not set) and
2234 * if it was non-zero to begin with.
2236 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
2237 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
2245 * Update the MTU metric of all route entries in all protocol tables
2246 * associated with a particular interface; this is called when the
2247 * MTU of that interface has changed.
2250 void if_rtmtu_update(struct ifnet
*ifp
)
2252 struct radix_node_head
*rnh
;
2255 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
2256 if ((rnh
= rt_tables
[p
]) == NULL
)
2259 lck_mtx_lock(rnh_lock
);
2260 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
2261 lck_mtx_unlock(rnh_lock
);
2265 routegenid_update();
2268 __private_extern__
void
2269 if_data_internal_to_if_data(
2271 const struct if_data_internal
*if_data_int
,
2272 struct if_data
*if_data
)
2274 struct dlil_threading_info
*thread
;
2275 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2276 thread
= dlil_lo_thread_ptr
;
2278 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
2279 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
2280 COPYFIELD(ifi_type
);
2281 COPYFIELD(ifi_typelen
);
2282 COPYFIELD(ifi_physical
);
2283 COPYFIELD(ifi_addrlen
);
2284 COPYFIELD(ifi_hdrlen
);
2285 COPYFIELD(ifi_recvquota
);
2286 COPYFIELD(ifi_xmitquota
);
2287 if_data
->ifi_unused1
= 0;
2289 COPYFIELD(ifi_metric
);
2290 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
2291 if_data
->ifi_baudrate
= 0xFFFFFFFF;
2294 COPYFIELD32(ifi_baudrate
);
2297 lck_mtx_lock(thread
->input_lck
);
2298 COPYFIELD32(ifi_ipackets
);
2299 COPYFIELD32(ifi_ierrors
);
2300 COPYFIELD32(ifi_opackets
);
2301 COPYFIELD32(ifi_oerrors
);
2302 COPYFIELD32(ifi_collisions
);
2303 COPYFIELD32(ifi_ibytes
);
2304 COPYFIELD32(ifi_obytes
);
2305 COPYFIELD32(ifi_imcasts
);
2306 COPYFIELD32(ifi_omcasts
);
2307 COPYFIELD32(ifi_iqdrops
);
2308 COPYFIELD32(ifi_noproto
);
2309 COPYFIELD32(ifi_recvtiming
);
2310 COPYFIELD32(ifi_xmittiming
);
2311 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2312 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2313 lck_mtx_unlock(thread
->input_lck
);
2315 #if IF_LASTCHANGEUPTIME
2316 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2319 if_data
->ifi_unused2
= 0;
2320 COPYFIELD(ifi_hwassist
);
2321 if_data
->ifi_reserved1
= 0;
2322 if_data
->ifi_reserved2
= 0;
2327 __private_extern__
void
2328 if_data_internal_to_if_data64(
2330 const struct if_data_internal
*if_data_int
,
2331 struct if_data64
*if_data64
)
2333 struct dlil_threading_info
*thread
;
2334 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2335 thread
= dlil_lo_thread_ptr
;
2337 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2338 COPYFIELD(ifi_type
);
2339 COPYFIELD(ifi_typelen
);
2340 COPYFIELD(ifi_physical
);
2341 COPYFIELD(ifi_addrlen
);
2342 COPYFIELD(ifi_hdrlen
);
2343 COPYFIELD(ifi_recvquota
);
2344 COPYFIELD(ifi_xmitquota
);
2345 if_data64
->ifi_unused1
= 0;
2347 COPYFIELD(ifi_metric
);
2348 COPYFIELD(ifi_baudrate
);
2350 lck_mtx_lock(thread
->input_lck
);
2351 COPYFIELD(ifi_ipackets
);
2352 COPYFIELD(ifi_ierrors
);
2353 COPYFIELD(ifi_opackets
);
2354 COPYFIELD(ifi_oerrors
);
2355 COPYFIELD(ifi_collisions
);
2356 COPYFIELD(ifi_ibytes
);
2357 COPYFIELD(ifi_obytes
);
2358 COPYFIELD(ifi_imcasts
);
2359 COPYFIELD(ifi_omcasts
);
2360 COPYFIELD(ifi_iqdrops
);
2361 COPYFIELD(ifi_noproto
);
2362 COPYFIELD(ifi_recvtiming
);
2363 COPYFIELD(ifi_xmittiming
);
2364 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2365 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2366 lck_mtx_unlock(thread
->input_lck
);
2368 #if IF_LASTCHANGEUPTIME
2369 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
2376 ifafree(struct ifaddr
*ifa
)
2380 oldval
= OSAddAtomic(-1, &ifa
->ifa_refcnt
);
2381 if (oldval
>= 1 && ifa
->ifa_trace
!= NULL
)
2382 (*ifa
->ifa_trace
)(ifa
, FALSE
);
2384 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
2385 } else if (oldval
== 1) {
2386 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2387 panic("ifa %p attached to ifp is being freed\n", ifa
);
2389 * Some interface addresses are allocated either statically
2390 * or carved out of a larger block; e.g. AppleTalk addresses.
2391 * Only free it if it was allocated via MALLOC or via the
2392 * corresponding per-address family allocator. Otherwise,
2395 if (ifa
->ifa_debug
& IFD_ALLOC
) {
2396 if (ifa
->ifa_free
== NULL
)
2397 FREE(ifa
, M_IFADDR
);
2399 (*ifa
->ifa_free
)(ifa
);
2405 ifaref(struct ifaddr
*ifa
)
2409 oldval
= OSAddAtomic(1, &ifa
->ifa_refcnt
);
2411 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
2412 else if (ifa
->ifa_trace
!= NULL
)
2413 (*ifa
->ifa_trace
)(ifa
, TRUE
);