2 * Copyright (c) 2000-2010 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
*);
136 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
137 #endif /* IF_CLONE_LIST */
139 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
140 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
142 int ifqmaxlen
= IFQ_MAXLEN
;
143 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
145 static int if_cloners_count
;
146 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
148 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
153 * XXX: declare here to avoid to include many inet6 related files..
154 * should be more generalized?
156 extern void nd6_setmtu(struct ifnet
*);
161 * Network interface utility routines.
163 * Routines with ifa_ifwith* names take sockaddr *'s as
168 struct ifaddr
**ifnet_addrs
;
169 struct ifnet
**ifindex2ifnet
;
171 __private_extern__
void
176 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
177 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
178 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
181 ifa
->ifa_debug
|= IFD_ATTACHED
;
182 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
185 __private_extern__
void
190 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
193 if ((ifa
->ifa_debug
& IFD_ATTACHED
) == 0) {
194 printf("if_detach_ifa: ifa is not attached to any interface! flags=%u\n", ifa
->ifa_debug
);
199 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
204 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
208 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
209 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
213 #define INITIAL_IF_INDEXLIM 8
216 * Function: if_next_index
218 * Return the next available interface index.
219 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
220 * added entry when necessary.
223 * ifnet_addrs[] is indexed by (if_index - 1), whereas
224 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
225 * always allocate one extra element to hold ifindex2ifnet[0], which
228 int if_next_index(void);
230 __private_extern__
int
233 static int if_indexlim
= 0;
236 new_index
= ++if_index
;
237 if (if_index
> if_indexlim
) {
240 caddr_t new_ifnet_addrs
;
241 caddr_t new_ifindex2ifnet
;
242 caddr_t old_ifnet_addrs
;
244 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
245 if (ifnet_addrs
== NULL
) {
246 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
248 new_if_indexlim
= if_indexlim
<< 1;
251 /* allocate space for the larger arrays */
252 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
253 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
254 if (new_ifnet_addrs
== NULL
) {
259 new_ifindex2ifnet
= new_ifnet_addrs
260 + new_if_indexlim
* sizeof(caddr_t
);
261 bzero(new_ifnet_addrs
, n
);
262 if (ifnet_addrs
!= NULL
) {
263 /* copy the existing data */
264 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
265 if_indexlim
* sizeof(caddr_t
));
266 bcopy((caddr_t
)ifindex2ifnet
,
268 (if_indexlim
+ 1) * sizeof(caddr_t
));
271 /* switch to the new tables and size */
272 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
273 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
274 if_indexlim
= new_if_indexlim
;
276 /* release the old data */
277 if (old_ifnet_addrs
!= NULL
) {
278 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
285 * Create a clone network interface.
288 if_clone_create(char *name
, int len
, void *params
)
290 struct if_clone
*ifc
;
293 u_int32_t bytoff
, bitoff
;
297 ifc
= if_clone_lookup(name
, &unit
);
301 if (ifunit(name
) != NULL
)
305 wildcard
= (unit
== UINT32_MAX
);
307 * Find a free unit if none was given.
310 while ((bytoff
< ifc
->ifc_bmlen
)
311 && (ifc
->ifc_units
[bytoff
] == 0xff))
313 if (bytoff
>= ifc
->ifc_bmlen
)
315 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
317 unit
= (bytoff
<< 3) + bitoff
;
320 if (unit
> ifc
->ifc_maxunit
)
323 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
329 bitoff
= unit
- (bytoff
<< 3);
333 * Allocate the unit in the bitmap.
335 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
336 ("%s: bit is already set", __func__
));
337 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
339 /* In the wildcard case, we need to update the name. */
341 for (dp
= name
; *dp
!= '\0'; dp
++);
342 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
343 len
- (dp
-name
) - 1) {
345 * This can only be a programmer error and
346 * there's no straightforward way to recover if
349 panic("if_clone_create(): interface name too long");
358 * Destroy a clone network interface.
361 if_clone_destroy(const char *name
)
363 struct if_clone
*ifc
;
368 ifc
= if_clone_lookup(name
, &unit
);
372 if (unit
< ifc
->ifc_minifs
)
379 if (ifc
->ifc_destroy
== NULL
)
382 (*ifc
->ifc_destroy
)(ifp
);
385 * Compute offset in the bitmap and deallocate the unit.
388 bitoff
= unit
- (bytoff
<< 3);
389 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
390 ("%s: bit is already cleared", __func__
));
391 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
396 * Look up a network interface cloner.
399 __private_extern__
struct if_clone
*
400 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
402 struct if_clone
*ifc
;
406 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
407 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
408 if (ifc
->ifc_name
[i
] != *cp
)
413 ifc
= LIST_NEXT(ifc
, ifc_list
);
417 return ((struct if_clone
*)NULL
);
423 for (i
= 0; *cp
!= '\0'; cp
++) {
424 if (*cp
< '0' || *cp
> '9') {
425 /* Bogus unit number. */
428 i
= (i
* 10) + (*cp
- '0');
438 * Register a network interface cloner.
441 if_clone_attach(struct if_clone
*ifc
)
448 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
449 ("%s: %s requested more units then allowed (%d > %d)",
450 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
451 ifc
->ifc_maxunit
+ 1));
453 * Compute bitmap size and allocate it.
455 maxclone
= ifc
->ifc_maxunit
+ 1;
457 if ((len
<< 3) < maxclone
)
459 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
460 if (ifc
->ifc_units
== NULL
)
462 bzero(ifc
->ifc_units
, len
);
463 ifc
->ifc_bmlen
= len
;
465 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
468 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
469 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
471 ("%s: failed to create required interface %s%d",
472 __func__
, ifc
->ifc_name
, unit
));
474 /* Allocate the unit in the bitmap. */
476 bitoff
= unit
- (bytoff
<< 3);
477 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
484 * Unregister a network interface cloner.
487 if_clone_detach(struct if_clone
*ifc
)
490 LIST_REMOVE(ifc
, ifc_list
);
491 FREE(ifc
->ifc_units
, M_CLONE
);
497 * Provide list of interface cloners to userspace.
500 if_clone_list(int count
, int * total
, user_addr_t dst
)
502 char outbuf
[IFNAMSIZ
];
503 struct if_clone
*ifc
;
506 *total
= if_cloners_count
;
507 if (dst
== USER_ADDR_NULL
) {
508 /* Just asking how many there are. */
515 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
517 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
518 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
519 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
520 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
527 #endif /* IF_CLONE_LIST */
530 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
531 * and that it matches only the local (not broadcast) address.
533 __private_extern__
struct in_ifaddr
*
534 ifa_foraddr(unsigned int addr
)
536 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
540 * Similar to ifa_foraddr, except with the added interface scope
541 * constraint (unless the caller passes in IFSCOPE_NONE in which
542 * case there is no scope restriction).
544 __private_extern__
struct in_ifaddr
*
545 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
547 struct in_ifaddr
*ia
= NULL
;
549 lck_rw_lock_shared(in_ifaddr_rwlock
);
550 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
551 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
552 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
))
557 lck_rw_done(in_ifaddr_rwlock
);
562 * Return the first (primary) address of a given family on an interface.
564 __private_extern__
struct ifaddr
*
565 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
567 struct ifaddr
*ifa0
= NULL
, *ifa
;
569 ifnet_lock_shared(ifp
);
570 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
571 if (ifa
->ifa_addr
->sa_family
== family
&& ifa0
== NULL
) {
578 ifnet_lock_done(ifp
);
584 * Locate an interface based on a complete address.
589 const struct sockaddr
*addr
)
593 struct ifaddr
*result
= NULL
;
595 #define equal(a1, a2) \
596 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
598 ifnet_head_lock_shared();
599 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
600 ifnet_lock_shared(ifp
);
601 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
602 ifa
= ifa
->ifa_link
.tqe_next
) {
603 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
605 if (equal(addr
, ifa
->ifa_addr
)) {
609 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
610 /* IP6 doesn't have broadcast */
611 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
612 equal(ifa
->ifa_broadaddr
, addr
)) {
619 ifnet_lock_done(ifp
);
626 * Locate the point to point interface with a given destination address.
631 const struct sockaddr
*addr
)
635 struct ifaddr
*result
= NULL
;
637 ifnet_head_lock_shared();
638 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
639 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
640 ifnet_lock_shared(ifp
);
641 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
642 ifa
= ifa
->ifa_link
.tqe_next
) {
643 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
645 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
652 ifnet_lock_done(ifp
);
660 * Locate the source address of an interface based on a complete address.
663 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
665 struct ifaddr
*result
= NULL
;
668 if (ifscope
== IFSCOPE_NONE
)
669 return (ifa_ifwithaddr(addr
));
671 ifnet_head_lock_shared();
672 if (ifscope
> (unsigned int)if_index
) {
677 ifp
= ifindex2ifnet
[ifscope
];
679 struct ifaddr
*ifa
= NULL
;
682 * This is suboptimal; there should be a better way
683 * to search for a given address of an interface
684 * for any given address family.
686 ifnet_lock_shared(ifp
);
687 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
688 ifa
= ifa
->ifa_link
.tqe_next
) {
689 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
691 if (equal(addr
, ifa
->ifa_addr
)) {
695 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
696 ifa
->ifa_broadaddr
!= NULL
&&
697 /* IP6 doesn't have broadcast */
698 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
699 equal(ifa
->ifa_broadaddr
, addr
)) {
706 ifnet_lock_done(ifp
);
714 ifa_ifwithnet(const struct sockaddr
*addr
)
716 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
720 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
722 return (ifa_ifwithnet_common(addr
, ifscope
));
726 * Find an interface on a specific network. If many, choice
727 * is most specific found.
729 static struct ifaddr
*
730 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
733 struct ifaddr
*ifa
= NULL
;
734 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
735 u_int af
= addr
->sa_family
;
736 const char *addr_data
= addr
->sa_data
, *cplim
;
738 if (!ip_doscopedroute
|| addr
->sa_family
!= AF_INET
)
739 ifscope
= IFSCOPE_NONE
;
741 ifnet_head_lock_shared();
743 * AF_LINK addresses can be looked up directly by their index number,
744 * so do that if we can.
747 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
748 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
749 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
760 * Scan though each interface, looking for ones that have
761 * addresses in this address family.
763 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
764 ifnet_lock_shared(ifp
);
765 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
766 ifa
= ifa
->ifa_link
.tqe_next
) {
767 const char *cp
, *cp2
, *cp3
;
769 if (ifa
->ifa_addr
->sa_family
!= af
)
772 /* This breaks tunneling application trying to install a route with
773 * a specific subnet and the local address as the destination
774 * It's breaks binary compatibility with previous version of MacOS X
778 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
779 addr
->sa_family
!= AF_INET6
&&
781 ifp
->if_flags
& IFF_POINTOPOINT
) {
783 * This is a bit broken as it doesn't
784 * take into account that the remote end may
785 * be a single node in the network we are
787 * The trouble is that we don't know the
788 * netmask for the remote end.
790 if (ifa
->ifa_dstaddr
!= 0
791 && equal(addr
, ifa
->ifa_dstaddr
)) {
795 #endif /* __APPLE__*/
798 * If we're looking up with a scope,
799 * find using a matching interface.
801 if (ifscope
!= IFSCOPE_NONE
&&
802 ifp
->if_index
!= ifscope
)
806 * Scan all the bits in the ifa's address.
807 * If a bit dissagrees with what we are
808 * looking for, mask it with the netmask
809 * to see if it really matters.
812 if (ifa
->ifa_netmask
== 0)
815 cp2
= ifa
->ifa_addr
->sa_data
;
816 cp3
= ifa
->ifa_netmask
->sa_data
;
817 cplim
= ifa
->ifa_netmask
->sa_len
818 + (char *)ifa
->ifa_netmask
;
820 if ((*cp
++ ^ *cp2
++) & *cp3
++)
821 goto next
; /* next address! */
823 * If the netmask of what we just found
824 * is more specific than what we had before
825 * (if we had one) then remember the new one
826 * before continuing to search
827 * for an even better one.
829 if (ifa_maybe
== 0 ||
830 rn_refines((caddr_t
)ifa
->ifa_netmask
,
831 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
845 * ifa is set if we found an exact match.
846 * take a reference to the ifa before
847 * releasing the ifp lock
849 ifnet_lock_done(ifp
);
858 else if (ifa_maybe
) {
866 * Find an interface address specific to an interface best matching
871 const struct sockaddr
*addr
,
874 struct ifaddr
*ifa
= NULL
;
875 const char *cp
, *cp2
, *cp3
;
877 struct ifaddr
*ifa_maybe
= NULL
;
878 struct ifaddr
*better_ifa_maybe
= NULL
;
879 u_int af
= addr
->sa_family
;
884 ifnet_lock_shared(ifp
);
885 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
886 ifa
= ifa
->ifa_link
.tqe_next
) {
887 if (ifa
->ifa_addr
->sa_family
!= af
)
891 if (ifa
->ifa_netmask
== 0) {
892 if (equal(addr
, ifa
->ifa_addr
) ||
893 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
897 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
898 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
))
901 if (equal(addr
, ifa
->ifa_addr
)) {
906 cp2
= ifa
->ifa_addr
->sa_data
;
907 cp3
= ifa
->ifa_netmask
->sa_data
;
908 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
909 for (; cp3
< cplim
; cp3
++)
910 if ((*cp
++ ^ *cp2
++) & *cp3
)
914 if (better_ifa_maybe
== NULL
) {
915 better_ifa_maybe
= ifa
;
922 if (better_ifa_maybe
!= NULL
) {
923 ifa
= better_ifa_maybe
;
928 if (ifa
) ifaref(ifa
);
930 ifnet_lock_done(ifp
);
934 #include <net/route.h>
937 * Default action when installing a route with a Link Level gateway.
938 * Lookup an appropriate real ifa to point to.
939 * This should be moved to /sys/net/link.c eventually.
942 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
945 struct sockaddr
*dst
;
948 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
949 RT_LOCK_ASSERT_HELD(rt
);
951 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
952 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
954 ifa
= ifaof_ifpforaddr(dst
, ifp
);
957 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
958 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
964 * if_updown will set the interface up or down. It will
965 * prevent other up/down events from occurring until this
966 * up/down event has completed.
968 * Caller must lock ifnet. This function will drop the
969 * lock. This allows ifnet_set_flags to set the rest of
970 * the flags after we change the up/down state without
971 * dropping the interface lock between setting the
972 * up/down state and updating the rest of the flags.
974 __private_extern__
void
983 /* Wait until no one else is changing the up/down state */
984 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
986 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
987 ifnet_lock_done(ifp
);
988 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
989 ifnet_lock_exclusive(ifp
);
992 /* Verify that the interface isn't already in the right state */
993 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
994 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
998 /* Indicate that the up/down state is changing */
999 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1001 /* Mark interface up or down */
1003 ifp
->if_flags
|= IFF_UP
;
1006 ifp
->if_flags
&= ~IFF_UP
;
1009 ifnet_touch_lastchange(ifp
);
1011 /* Drop the lock to notify addresses and route */
1012 ifnet_lock_done(ifp
);
1013 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1014 for (i
= 0; ifa
[i
] != 0; i
++) {
1015 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1017 ifnet_free_address_list(ifa
);
1021 /* Aquire the lock to clear the changing flag and flush the send queue */
1022 ifnet_lock_exclusive(ifp
);
1024 if_qflush(&ifp
->if_snd
);
1025 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1026 wakeup(&ifp
->if_eflags
);
1032 * Mark an interface down and notify protocols of
1039 ifnet_lock_exclusive(ifp
);
1041 ifnet_lock_done(ifp
);
1045 * Mark an interface up and notify protocols of
1052 ifnet_lock_exclusive(ifp
);
1054 ifnet_lock_done(ifp
);
1058 * Flush an interface queue.
1061 if_qflush(struct ifqueue
*ifq
)
1066 while ((m
= n
) != 0) {
1070 ifq
->ifq_head
= NULL
;
1071 ifq
->ifq_tail
= NULL
;
1076 * Map interface name to
1077 * interface structure pointer.
1080 ifunit(const char *name
)
1082 char namebuf
[IFNAMSIZ
+ 1];
1090 if (len
< 2 || len
> IFNAMSIZ
)
1092 cp
= name
+ len
- 1;
1094 if (c
< '0' || c
> '9')
1095 return NULL
; /* trailing garbage */
1100 return NULL
; /* no interface name */
1101 unit
+= (c
- '0') * m
;
1103 return NULL
; /* number is unreasonable */
1106 } while (c
>= '0' && c
<= '9');
1107 len
= cp
- name
+ 1;
1108 bcopy(name
, namebuf
, len
);
1109 namebuf
[len
] = '\0';
1111 * Now search all the interfaces for this name/number
1113 ifnet_head_lock_shared();
1114 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1115 if (strncmp(ifp
->if_name
, namebuf
, len
))
1117 if (unit
== ifp
->if_unit
)
1126 * Map interface name in a sockaddr_dl to
1127 * interface structure pointer.
1130 if_withname(struct sockaddr
*sa
)
1132 char ifname
[IFNAMSIZ
+1];
1133 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1135 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1136 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1140 * ifunit wants a null-terminated name. It may not be null-terminated
1141 * in the sockaddr. We don't want to change the caller's sockaddr,
1142 * and there might not be room to put the trailing null anyway, so we
1143 * make a local copy that we know we can null terminate safely.
1146 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1147 ifname
[sdl
->sdl_nlen
] = '\0';
1148 return ifunit(ifname
);
1156 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1163 struct kev_msg ev_msg
;
1164 struct net_event_data ev_data
;
1167 case OSIOCGIFCONF32
:
1168 case SIOCGIFCONF32
: {
1169 struct ifconf32
*ifc
= (struct ifconf32
*)data
;
1170 return (ifconf(cmd
, CAST_USER_ADDR_T(ifc
->ifc_req
),
1175 case OSIOCGIFCONF64
: {
1176 struct ifconf64
*ifc
= (struct ifconf64
*)data
;
1177 return (ifconf(cmd
, ifc
->ifc_req
, &ifc
->ifc_len
));
1181 ifr
= (struct ifreq
*)data
;
1185 error
= proc_suser(p
);
1188 return if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1189 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
);
1191 error
= proc_suser(p
);
1194 return if_clone_destroy(ifr
->ifr_name
);
1196 case SIOCIFGCLONERS32
: {
1197 struct if_clonereq32
*ifcr
= (struct if_clonereq32
*)data
;
1198 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1199 CAST_USER_ADDR_T(ifcr
->ifcru_buffer
)));
1203 case SIOCIFGCLONERS64
: {
1204 struct if_clonereq64
*ifcr
= (struct if_clonereq64
*)data
;
1205 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1206 ifcr
->ifcru_buffer
));
1209 #endif /* IF_CLONE_LIST */
1212 ifp
= ifunit(ifr
->ifr_name
);
1218 ifnet_lock_shared(ifp
);
1219 ifr
->ifr_flags
= ifp
->if_flags
;
1220 ifnet_lock_done(ifp
);
1225 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
1231 ifnet_lock_shared(ifp
);
1232 ifr
->ifr_metric
= ifp
->if_metric
;
1233 ifnet_lock_done(ifp
);
1237 ifnet_lock_shared(ifp
);
1238 ifr
->ifr_mtu
= ifp
->if_mtu
;
1239 ifnet_lock_done(ifp
);
1243 ifnet_lock_shared(ifp
);
1244 ifr
->ifr_phys
= ifp
->if_physical
;
1245 ifnet_lock_done(ifp
);
1249 error
= proc_suser(p
);
1253 ifnet_set_flags(ifp
, ifr
->ifr_flags
, (u_int16_t
)~IFF_CANTCHANGE
);
1255 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1258 /* Send the event even upon error from the driver because we changed the flags */
1259 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1260 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1261 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1263 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1264 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1265 ev_data
.if_family
= ifp
->if_family
;
1266 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1267 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1268 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1269 ev_msg
.dv
[1].data_length
= 0;
1270 kev_post_msg(&ev_msg
);
1272 ifnet_touch_lastchange(ifp
);
1277 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
1283 error
= proc_suser(p
);
1286 ifp
->if_metric
= ifr
->ifr_metric
;
1289 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1290 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1291 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1293 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1294 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1295 ev_data
.if_family
= ifp
->if_family
;
1296 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1297 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1298 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1300 ev_msg
.dv
[1].data_length
= 0;
1301 kev_post_msg(&ev_msg
);
1303 ifnet_touch_lastchange(ifp
);
1307 error
= proc_suser(p
);
1311 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1315 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1316 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1317 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1319 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1320 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1321 ev_data
.if_family
= ifp
->if_family
;
1322 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1323 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1324 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1325 ev_msg
.dv
[1].data_length
= 0;
1326 kev_post_msg(&ev_msg
);
1328 ifnet_touch_lastchange(ifp
);
1334 u_int32_t oldmtu
= ifp
->if_mtu
;
1336 error
= proc_suser(p
);
1339 if (ifp
->if_ioctl
== NULL
)
1340 return (EOPNOTSUPP
);
1341 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1344 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1348 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1349 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1350 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1352 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1353 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1354 ev_data
.if_family
= ifp
->if_family
;
1355 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1356 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1357 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1358 ev_msg
.dv
[1].data_length
= 0;
1359 kev_post_msg(&ev_msg
);
1361 ifnet_touch_lastchange(ifp
);
1365 * If the link MTU changed, do network layer specific procedure
1366 * and update all route entries associated with the interface,
1367 * so that their MTU metric gets updated.
1369 if (error
== 0 && ifp
->if_mtu
!= oldmtu
) {
1370 if_rtmtu_update(ifp
);
1380 error
= proc_suser(p
);
1384 /* Don't allow group membership on non-multicast interfaces. */
1385 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1389 /* Don't let users screw up protocols' entries. */
1390 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1394 if (cmd
== SIOCADDMULTI
) {
1395 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1396 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1398 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1399 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1402 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1403 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1404 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1405 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1407 ev_data
.if_family
= ifp
->if_family
;
1408 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1409 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1410 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1411 ev_msg
.dv
[1].data_length
= 0;
1412 kev_post_msg(&ev_msg
);
1414 ifnet_touch_lastchange(ifp
);
1418 case SIOCSIFPHYADDR
:
1419 case SIOCDIFPHYADDR
:
1421 case SIOCSIFPHYADDR_IN6_32
:
1422 case SIOCSIFPHYADDR_IN6_64
:
1424 case SIOCSLIFPHYADDR
:
1426 case SIOCSIFGENERIC
:
1431 error
= proc_suser(p
);
1435 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1439 ifnet_touch_lastchange(ifp
);
1443 ifs
= (struct ifstat
*)data
;
1444 ifs
->ascii
[0] = '\0';
1446 case SIOCGIFPSRCADDR
:
1447 case SIOCGIFPDSTADDR
:
1448 case SIOCGLIFPHYADDR
:
1449 case SIOCGIFMEDIA32
:
1450 case SIOCGIFMEDIA64
:
1451 case SIOCGIFGENERIC
:
1453 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1457 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1460 case SIOCGIFWAKEFLAGS
:
1461 ifnet_lock_shared(ifp
);
1462 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
1463 ifnet_lock_done(ifp
);
1466 case SIOCGIFGETRTREFCNT
:
1467 #if IFNET_ROUTE_REFCNT
1468 ifnet_lock_shared(ifp
);
1469 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
1470 ifnet_lock_done(ifp
);
1473 return (EOPNOTSUPP
);
1474 #endif /* IFNET_ROUTE_REFCNT */
1477 oif_flags
= ifp
->if_flags
;
1478 if (so
->so_proto
== 0)
1479 return (EOPNOTSUPP
);
1485 case SIOCSIFDSTADDR
:
1487 case SIOCSIFBRDADDR
:
1488 case SIOCSIFNETMASK
:
1489 #if BYTE_ORDER != BIG_ENDIAN
1490 if (ifr
->ifr_addr
.sa_family
== 0 &&
1491 ifr
->ifr_addr
.sa_len
< 16) {
1492 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1493 ifr
->ifr_addr
.sa_len
= 16;
1496 if (ifr
->ifr_addr
.sa_len
== 0)
1497 ifr
->ifr_addr
.sa_len
= 16;
1505 case OSIOCGIFDSTADDR
:
1506 cmd
= SIOCGIFDSTADDR
;
1509 case OSIOCGIFBRDADDR
:
1510 cmd
= SIOCGIFBRDADDR
;
1513 case OSIOCGIFNETMASK
:
1514 cmd
= SIOCGIFNETMASK
;
1517 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1519 socket_unlock(so
, 1);
1523 case OSIOCGIFDSTADDR
:
1524 case OSIOCGIFBRDADDR
:
1525 case OSIOCGIFNETMASK
:
1526 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1530 if (cmd
== SIOCSIFKPI
) {
1531 int temperr
= proc_suser(p
);
1536 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1537 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1546 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1550 socket_unlock(so
, 0);
1551 error
= ifioctl(so
, cmd
, data
, p
);
1557 * Set/clear promiscuous mode on interface ifp based on the truth value
1558 * of pswitch. The calls are reference counted so that only the first
1559 * "on" request actually has an effect, as does the final "off" request.
1560 * Results are undefined if the "off" and "on" requests are not matched.
1563 ifnet_set_promiscuous(
1573 ifnet_lock_exclusive(ifp
);
1575 oldflags
= ifp
->if_flags
;
1578 * If the device is not configured up, we cannot put it in
1581 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1585 if (ifp
->if_pcount
++ != 0) {
1588 ifp
->if_flags
|= IFF_PROMISC
;
1590 if (--ifp
->if_pcount
> 0)
1592 ifp
->if_flags
&= ~IFF_PROMISC
;
1594 ifr
.ifr_flags
= ifp
->if_flags
;
1596 ifnet_lock_done(ifp
);
1597 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, &ifr
);
1601 ifp
->if_flags
= oldflags
;
1603 if (locked
) ifnet_lock_done(ifp
);
1605 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1606 ifp
->if_name
, ifp
->if_unit
,
1607 pswitch
!= 0 ? "enabled" : "disabled");
1613 * Return interface configuration
1614 * of system. List may be used
1615 * in later ioctl's (above) to get
1616 * other information.
1620 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1622 struct ifnet
*ifp
= NULL
;
1629 * Zero the ifr buffer to make sure we don't
1630 * disclose the contents of the stack.
1632 bzero(&ifr
, sizeof(struct ifreq
));
1635 ifnet_head_lock_shared();
1636 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1638 size_t ifnlen
, addrs
;
1640 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1641 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1642 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1643 error
= ENAMETOOLONG
;
1646 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
1649 ifnet_lock_shared(ifp
);
1652 ifa
= ifp
->if_addrhead
.tqh_first
;
1653 for ( ; space
> sizeof (ifr
) && ifa
;
1654 ifa
= ifa
->ifa_link
.tqe_next
) {
1655 struct sockaddr
*sa
= ifa
->ifa_addr
;
1657 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1661 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
1662 struct osockaddr
*osa
=
1663 (struct osockaddr
*)&ifr
.ifr_addr
;
1665 osa
->sa_family
= sa
->sa_family
;
1666 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1667 ifrp
+= sizeof(struct ifreq
);
1668 } else if (sa
->sa_len
<= sizeof(*sa
)) {
1670 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1671 ifrp
+= sizeof(struct ifreq
);
1673 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1675 space
-= sa
->sa_len
- sizeof(*sa
);
1676 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1678 error
= copyout((caddr_t
)sa
,
1679 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1682 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1686 space
-= sizeof (ifr
);
1688 ifnet_lock_done(ifp
);
1693 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1694 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1697 space
-= sizeof (ifr
);
1698 ifrp
+= sizeof(struct ifreq
);
1702 *ret_space
-= space
;
1707 * Just like if_promisc(), but for all-multicast-reception mode.
1710 if_allmulti(struct ifnet
*ifp
, int onswitch
)
1715 ifnet_lock_exclusive(ifp
);
1718 if (ifp
->if_amcount
++ == 0) {
1719 ifp
->if_flags
|= IFF_ALLMULTI
;
1723 if (ifp
->if_amcount
> 1) {
1726 ifp
->if_amcount
= 0;
1727 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1731 ifnet_lock_done(ifp
);
1734 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
1743 struct ifmultiaddr
*ifma
)
1745 if (OSIncrementAtomic(&ifma
->ifma_refcount
) <= 0)
1746 panic("ifma_reference: ifma already released or invalid\n");
1751 struct ifmultiaddr
*ifma
)
1754 struct ifmultiaddr
*next
;
1755 int32_t prevValue
= OSDecrementAtomic(&ifma
->ifma_refcount
);
1757 panic("ifma_release: ifma already released or invalid\n");
1761 /* Allow the allocator of the protospec to free it */
1762 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1763 ifma
->ifma_free(ifma
->ifma_protospec
);
1766 next
= ifma
->ifma_ll
;
1767 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1768 FREE(ifma
, M_IFMADDR
);
1774 * Find an ifmultiaddr that matches a socket address on an interface.
1776 * Caller is responsible for holding the ifnet_lock while calling
1780 if_addmulti_doesexist(
1782 const struct sockaddr
*sa
,
1783 struct ifmultiaddr
**retifma
)
1785 struct ifmultiaddr
*ifma
;
1786 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1787 ifma
= ifma
->ifma_link
.le_next
) {
1788 if (equal(sa
, ifma
->ifma_addr
)) {
1789 ifma
->ifma_usecount
++;
1792 ifma_reference(*retifma
);
1802 * Radar 3642395, make sure all multicasts are in a standard format.
1804 static struct sockaddr
*
1806 const struct sockaddr
*original
)
1809 const u_char
*aptr
= NULL
;
1810 struct sockaddr
*copy
= NULL
;
1811 struct sockaddr_dl
*sdl_new
= NULL
;
1814 if (original
->sa_family
!= AF_LINK
&&
1815 original
->sa_family
!= AF_UNSPEC
) {
1816 /* Just make a copy */
1817 MALLOC(copy
, struct sockaddr
*, original
->sa_len
, M_IFADDR
, M_WAITOK
);
1819 bcopy(original
, copy
, original
->sa_len
);
1823 switch (original
->sa_family
) {
1825 const struct sockaddr_dl
*sdl_original
=
1826 (const struct sockaddr_dl
*)original
;
1828 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+ sdl_original
->sdl_slen
+
1829 offsetof(struct sockaddr_dl
, sdl_data
) > sdl_original
->sdl_len
)
1832 alen
= sdl_original
->sdl_alen
;
1833 aptr
= CONST_LLADDR(sdl_original
);
1838 if (original
->sa_len
< ETHER_ADDR_LEN
+
1839 offsetof(struct sockaddr
, sa_data
)) {
1843 alen
= ETHER_ADDR_LEN
;
1844 aptr
= (const u_char
*)original
->sa_data
;
1849 if (alen
== 0 || aptr
== NULL
)
1852 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
1853 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
1855 if (sdl_new
!= NULL
) {
1856 bzero(sdl_new
, len
);
1857 sdl_new
->sdl_len
= len
;
1858 sdl_new
->sdl_family
= AF_LINK
;
1859 sdl_new
->sdl_alen
= alen
;
1860 bcopy(aptr
, LLADDR(sdl_new
), alen
);
1863 return (struct sockaddr
*)sdl_new
;
1867 * Add a multicast listenership to the interface in question.
1868 * The link layer provides a routine which converts
1872 struct ifnet
*ifp
, /* interface to manipulate */
1873 const struct sockaddr
*sa
, /* address to add */
1874 struct ifmultiaddr
**retifma
)
1876 struct sockaddr_storage storage
;
1877 struct sockaddr
*llsa
= NULL
;
1878 struct sockaddr
*dupsa
= NULL
;
1880 struct ifmultiaddr
*ifma
= NULL
;
1881 struct ifmultiaddr
*llifma
= NULL
;
1883 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
1884 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
1885 dupsa
= copy_and_normalize(sa
);
1886 if (dupsa
== NULL
) {
1892 ifnet_lock_exclusive(ifp
);
1893 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1894 ifnet_lock_done(ifp
);
1901 * Give the link layer a chance to accept/reject it, and also
1902 * find out which AF_LINK address this maps to, if it isn't one
1905 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
1907 if (error
== 0 && storage
.ss_len
!= 0) {
1908 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
1914 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1915 if (llifma
== NULL
) {
1921 /* to be similar to FreeBSD */
1922 if (error
== EOPNOTSUPP
) {
1929 /* Allocate while we aren't holding any locks */
1930 if (dupsa
== NULL
) {
1931 dupsa
= copy_and_normalize(sa
);
1932 if (dupsa
== NULL
) {
1937 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1943 ifnet_lock_exclusive(ifp
);
1945 * Check again for the matching multicast.
1947 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1948 ifnet_lock_done(ifp
);
1952 bzero(ifma
, sizeof(*ifma
));
1953 ifma
->ifma_addr
= dupsa
;
1954 ifma
->ifma_ifp
= ifp
;
1955 ifma
->ifma_usecount
= 1;
1956 ifma
->ifma_refcount
= 1;
1959 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1960 FREE(llsa
, M_IFMADDR
);
1961 FREE(llifma
, M_IFMADDR
);
1963 bzero(llifma
, sizeof(*llifma
));
1964 llifma
->ifma_addr
= llsa
;
1965 llifma
->ifma_ifp
= ifp
;
1966 llifma
->ifma_usecount
= 1;
1967 llifma
->ifma_refcount
= 1;
1968 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1970 ifma
->ifma_ll
= llifma
;
1971 ifma_reference(ifma
->ifma_ll
);
1975 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1979 ifma_reference(*retifma
);
1982 ifnet_lock_done(ifp
);
1985 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1988 * We are certain we have added something, so call down to the
1989 * interface to let them know about it.
1991 ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
1997 FREE(ifma
, M_IFADDR
);
1999 FREE(dupsa
, M_IFADDR
);
2001 FREE(llifma
, M_IFADDR
);
2003 FREE(llsa
, M_IFADDR
);
2010 struct ifmultiaddr
*ifma
,
2014 int do_del_multi
= 0;
2016 ifp
= ifma
->ifma_ifp
;
2018 if (!locked
&& ifp
) {
2019 ifnet_lock_exclusive(ifp
);
2022 while (ifma
!= NULL
) {
2023 struct ifmultiaddr
*ll_ifma
;
2025 if (ifma
->ifma_usecount
> 1) {
2026 ifma
->ifma_usecount
--;
2031 LIST_REMOVE(ifma
, ifma_link
);
2033 ll_ifma
= ifma
->ifma_ll
;
2035 if (ll_ifma
) { /* send a routing msg for network addresses only */
2037 ifnet_lock_done(ifp
);
2038 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2040 ifnet_lock_exclusive(ifp
);
2044 * Make sure the interface driver is notified
2045 * in the case of a link layer mcast group being left.
2048 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
2059 if (!locked
&& ifp
) {
2060 /* This wasn't initially locked, we should unlock it */
2061 ifnet_lock_done(ifp
);
2066 ifnet_lock_done(ifp
);
2067 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
2069 ifnet_lock_exclusive(ifp
);
2076 * Remove a reference to a multicast address on this interface. Yell
2077 * if the request does not match an existing membership.
2082 const struct sockaddr
*sa
)
2084 struct ifmultiaddr
*ifma
;
2085 struct sockaddr
*dupsa
= NULL
;
2088 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
2089 dupsa
= copy_and_normalize(sa
);
2090 if (dupsa
== NULL
) {
2096 ifnet_lock_exclusive(ifp
);
2097 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2098 ifma
= ifma
->ifma_link
.le_next
)
2099 if (equal(sa
, ifma
->ifma_addr
))
2102 ifnet_lock_done(ifp
);
2104 FREE(dupsa
, M_IFADDR
);
2108 retval
= if_delmultiaddr(ifma
, 1);
2109 ifnet_lock_done(ifp
);
2111 FREE(dupsa
, M_IFADDR
);
2118 * We don't use if_setlladdr, our interfaces are responsible for
2119 * handling the SIOCSIFLLADDR ioctl.
2123 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2129 struct ifmultiaddr
*
2130 ifmaof_ifpforaddr(const struct sockaddr
*sa
, struct ifnet
*ifp
)
2132 struct ifmultiaddr
*ifma
;
2134 ifnet_lock_shared(ifp
);
2135 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2136 ifma
= ifma
->ifma_link
.le_next
)
2137 if (equal(ifma
->ifma_addr
, sa
))
2139 ifnet_lock_done(ifp
);
2144 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
2145 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Generic link-management");
2149 * Shutdown all network activity. Used boot() when halting
2159 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
2160 for (i
= 0; i
< count
; i
++) {
2162 dlil_proto_unplumb_all(ifp
[i
]);
2164 ifnet_list_free(ifp
);
2171 * Delete Routes for a Network Interface
2173 * Called for each routing entry via the rnh->rnh_walktree() call above
2174 * to delete all route entries referencing a detaching network interface.
2177 * rn pointer to node in the routing table
2178 * arg argument passed to rnh->rnh_walktree() - detaching interface
2182 * errno failed - reason indicated
2186 if_rtdel(struct radix_node
*rn
, void *arg
)
2188 struct rtentry
*rt
= (struct rtentry
*)rn
;
2189 struct ifnet
*ifp
= arg
;
2195 * Checking against RTF_UP protects against walktree
2196 * recursion problems with cloned routes.
2199 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
2201 * Safe to drop rt_lock and use rt_key, rt_gateway,
2202 * since holding rnh_lock here prevents another thread
2203 * from calling rt_setgate() on this route.
2206 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
2207 rt_mask(rt
), rt
->rt_flags
, NULL
);
2209 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
2218 * Removes routing table reference to a given interface
2219 * for a given protocol family
2222 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
2224 struct radix_node_head
*rnh
;
2227 routegenid_update();
2228 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
2229 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
2230 lck_mtx_lock(rnh_lock
);
2231 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
2232 lck_mtx_unlock(rnh_lock
);
2237 if_rtmtu(struct radix_node
*rn
, void *arg
)
2239 struct rtentry
*rt
= (struct rtentry
*)rn
;
2240 struct ifnet
*ifp
= arg
;
2243 if (rt
->rt_ifp
== ifp
) {
2245 * Update the MTU of this entry only if the MTU
2246 * has not been locked (RTV_MTU is not set) and
2247 * if it was non-zero to begin with.
2249 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
2250 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
2258 * Update the MTU metric of all route entries in all protocol tables
2259 * associated with a particular interface; this is called when the
2260 * MTU of that interface has changed.
2263 void if_rtmtu_update(struct ifnet
*ifp
)
2265 struct radix_node_head
*rnh
;
2268 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
2269 if ((rnh
= rt_tables
[p
]) == NULL
)
2272 lck_mtx_lock(rnh_lock
);
2273 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
2274 lck_mtx_unlock(rnh_lock
);
2278 routegenid_update();
2281 __private_extern__
void
2282 if_data_internal_to_if_data(
2284 const struct if_data_internal
*if_data_int
,
2285 struct if_data
*if_data
)
2287 struct dlil_threading_info
*thread
;
2288 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2289 thread
= dlil_lo_thread_ptr
;
2291 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
2292 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
2293 COPYFIELD(ifi_type
);
2294 COPYFIELD(ifi_typelen
);
2295 COPYFIELD(ifi_physical
);
2296 COPYFIELD(ifi_addrlen
);
2297 COPYFIELD(ifi_hdrlen
);
2298 COPYFIELD(ifi_recvquota
);
2299 COPYFIELD(ifi_xmitquota
);
2300 if_data
->ifi_unused1
= 0;
2302 COPYFIELD(ifi_metric
);
2303 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
2304 if_data
->ifi_baudrate
= 0xFFFFFFFF;
2307 COPYFIELD32(ifi_baudrate
);
2310 lck_mtx_lock(thread
->input_lck
);
2311 COPYFIELD32(ifi_ipackets
);
2312 COPYFIELD32(ifi_ierrors
);
2313 COPYFIELD32(ifi_opackets
);
2314 COPYFIELD32(ifi_oerrors
);
2315 COPYFIELD32(ifi_collisions
);
2316 COPYFIELD32(ifi_ibytes
);
2317 COPYFIELD32(ifi_obytes
);
2318 COPYFIELD32(ifi_imcasts
);
2319 COPYFIELD32(ifi_omcasts
);
2320 COPYFIELD32(ifi_iqdrops
);
2321 COPYFIELD32(ifi_noproto
);
2322 COPYFIELD32(ifi_recvtiming
);
2323 COPYFIELD32(ifi_xmittiming
);
2324 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2325 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2326 lck_mtx_unlock(thread
->input_lck
);
2328 #if IF_LASTCHANGEUPTIME
2329 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2332 if_data
->ifi_unused2
= 0;
2333 COPYFIELD(ifi_hwassist
);
2334 if_data
->ifi_reserved1
= 0;
2335 if_data
->ifi_reserved2
= 0;
2340 __private_extern__
void
2341 if_data_internal_to_if_data64(
2343 const struct if_data_internal
*if_data_int
,
2344 struct if_data64
*if_data64
)
2346 struct dlil_threading_info
*thread
;
2347 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2348 thread
= dlil_lo_thread_ptr
;
2350 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2351 COPYFIELD(ifi_type
);
2352 COPYFIELD(ifi_typelen
);
2353 COPYFIELD(ifi_physical
);
2354 COPYFIELD(ifi_addrlen
);
2355 COPYFIELD(ifi_hdrlen
);
2356 COPYFIELD(ifi_recvquota
);
2357 COPYFIELD(ifi_xmitquota
);
2358 if_data64
->ifi_unused1
= 0;
2360 COPYFIELD(ifi_metric
);
2361 COPYFIELD(ifi_baudrate
);
2363 lck_mtx_lock(thread
->input_lck
);
2364 COPYFIELD(ifi_ipackets
);
2365 COPYFIELD(ifi_ierrors
);
2366 COPYFIELD(ifi_opackets
);
2367 COPYFIELD(ifi_oerrors
);
2368 COPYFIELD(ifi_collisions
);
2369 COPYFIELD(ifi_ibytes
);
2370 COPYFIELD(ifi_obytes
);
2371 COPYFIELD(ifi_imcasts
);
2372 COPYFIELD(ifi_omcasts
);
2373 COPYFIELD(ifi_iqdrops
);
2374 COPYFIELD(ifi_noproto
);
2375 COPYFIELD(ifi_recvtiming
);
2376 COPYFIELD(ifi_xmittiming
);
2377 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2378 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2379 lck_mtx_unlock(thread
->input_lck
);
2381 #if IF_LASTCHANGEUPTIME
2382 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
2389 ifafree(struct ifaddr
*ifa
)
2393 oldval
= OSAddAtomic(-1, &ifa
->ifa_refcnt
);
2394 if (oldval
>= 1 && ifa
->ifa_trace
!= NULL
)
2395 (*ifa
->ifa_trace
)(ifa
, FALSE
);
2397 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
2398 } else if (oldval
== 1) {
2399 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2400 panic("ifa %p attached to ifp is being freed\n", ifa
);
2402 * Some interface addresses are allocated either statically
2403 * or carved out of a larger block; e.g. AppleTalk addresses.
2404 * Only free it if it was allocated via MALLOC or via the
2405 * corresponding per-address family allocator. Otherwise,
2408 if (ifa
->ifa_debug
& IFD_ALLOC
) {
2409 if (ifa
->ifa_free
== NULL
)
2410 FREE(ifa
, M_IFADDR
);
2412 (*ifa
->ifa_free
)(ifa
);
2418 ifaref(struct ifaddr
*ifa
)
2422 oldval
= OSAddAtomic(1, &ifa
->ifa_refcnt
);
2424 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
2425 else if (ifa
->ifa_trace
!= NULL
)
2426 (*ifa
->ifa_trace
)(ifa
, TRUE
);