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>
86 #include <net/if_arp.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/if_var.h>
90 #include <net/net_osdep.h>
91 #include <net/ethernet.h>
93 #include <net/radix.h>
94 #include <net/route.h>
98 #include <sys/domain.h>
99 #include <libkern/OSAtomic.h>
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
108 #include <netinet6/in6_var.h>
109 #include <netinet6/in6_ifattach.h>
113 extern u_long route_generation
;
114 extern int use_routegenid
;
115 extern int dlil_multithreaded_input
;
116 extern struct dlil_threading_info
*dlil_lo_thread_ptr
;
119 #include <security/mac_framework.h>
123 * System initialization
126 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
127 static void if_qflush(struct ifqueue
*);
128 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
129 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
131 static int if_rtmtu(struct radix_node
*, void *);
132 static void if_rtmtu_update(struct ifnet
*);
134 static struct if_clone
*if_clone_lookup(const char *, int *);
136 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
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
*);
159 #define M_CLONE M_IFADDR
162 * Network interface utility routines.
164 * Routines with ifa_ifwith* names take sockaddr *'s as
169 struct ifaddr
**ifnet_addrs
;
170 struct ifnet
**ifindex2ifnet
;
172 __private_extern__
void
177 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
178 if (ifa
->ifa_debug
& IFA_ATTACHED
) {
179 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
182 ifa
->ifa_debug
|= IFA_ATTACHED
;
183 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
186 __private_extern__
void
191 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
194 if ((ifa
->ifa_debug
& IFA_ATTACHED
) == 0) {
195 printf("if_detach_ifa: ifa is not attached to any interface! flags=%lu\n", ifa
->ifa_debug
);
200 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
205 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
209 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
210 ifa
->ifa_debug
&= ~IFA_ATTACHED
;
214 #define INITIAL_IF_INDEXLIM 8
217 * Function: if_next_index
219 * Return the next available interface index.
220 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
221 * added entry when necessary.
224 * ifnet_addrs[] is indexed by (if_index - 1), whereas
225 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
226 * always allocate one extra element to hold ifindex2ifnet[0], which
229 int if_next_index(void);
231 __private_extern__
int
234 static int if_indexlim
= 0;
237 new_index
= ++if_index
;
238 if (if_index
> if_indexlim
) {
241 caddr_t new_ifnet_addrs
;
242 caddr_t new_ifindex2ifnet
;
243 caddr_t old_ifnet_addrs
;
245 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
246 if (ifnet_addrs
== NULL
) {
247 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
249 new_if_indexlim
= if_indexlim
<< 1;
252 /* allocate space for the larger arrays */
253 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
254 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
255 new_ifindex2ifnet
= new_ifnet_addrs
256 + new_if_indexlim
* sizeof(caddr_t
);
257 bzero(new_ifnet_addrs
, n
);
258 if (ifnet_addrs
!= NULL
) {
259 /* copy the existing data */
260 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
261 if_indexlim
* sizeof(caddr_t
));
262 bcopy((caddr_t
)ifindex2ifnet
,
264 (if_indexlim
+ 1) * sizeof(caddr_t
));
267 /* switch to the new tables and size */
268 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
269 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
270 if_indexlim
= new_if_indexlim
;
272 /* release the old data */
273 if (old_ifnet_addrs
!= NULL
) {
274 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
281 * Create a clone network interface.
284 if_clone_create(char *name
, int len
)
286 struct if_clone
*ifc
;
288 int wildcard
, bytoff
, bitoff
;
292 ifc
= if_clone_lookup(name
, &unit
);
296 if (ifunit(name
) != NULL
)
300 wildcard
= (unit
< 0);
302 * Find a free unit if none was given.
305 while ((bytoff
< ifc
->ifc_bmlen
)
306 && (ifc
->ifc_units
[bytoff
] == 0xff))
308 if (bytoff
>= ifc
->ifc_bmlen
)
310 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
312 unit
= (bytoff
<< 3) + bitoff
;
315 if (unit
> ifc
->ifc_maxunit
)
318 err
= (*ifc
->ifc_create
)(ifc
, unit
);
324 bitoff
= unit
- (bytoff
<< 3);
328 * Allocate the unit in the bitmap.
330 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
331 ("%s: bit is already set", __func__
));
332 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
334 /* In the wildcard case, we need to update the name. */
336 for (dp
= name
; *dp
!= '\0'; dp
++);
337 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
338 len
- (dp
-name
) - 1) {
340 * This can only be a programmer error and
341 * there's no straightforward way to recover if
344 panic("if_clone_create(): interface name too long");
353 * Destroy a clone network interface.
356 if_clone_destroy(const char *name
)
358 struct if_clone
*ifc
;
363 ifc
= if_clone_lookup(name
, &unit
);
367 if (unit
< ifc
->ifc_minifs
)
374 if (ifc
->ifc_destroy
== NULL
)
377 (*ifc
->ifc_destroy
)(ifp
);
380 * Compute offset in the bitmap and deallocate the unit.
383 bitoff
= unit
- (bytoff
<< 3);
384 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
385 ("%s: bit is already cleared", __func__
));
386 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
391 * Look up a network interface cloner.
394 static struct if_clone
*
395 if_clone_lookup(const char *name
, int *unitp
)
397 struct if_clone
*ifc
;
401 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
402 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
403 if (ifc
->ifc_name
[i
] != *cp
)
408 ifc
= LIST_NEXT(ifc
, ifc_list
);
412 return ((struct if_clone
*)NULL
);
418 for (i
= 0; *cp
!= '\0'; cp
++) {
419 if (*cp
< '0' || *cp
> '9') {
420 /* Bogus unit number. */
423 i
= (i
* 10) + (*cp
- '0');
433 * Register a network interface cloner.
436 if_clone_attach(struct if_clone
*ifc
)
443 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
444 ("%s: %s requested more units then allowed (%d > %d)",
445 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
446 ifc
->ifc_maxunit
+ 1));
448 * Compute bitmap size and allocate it.
450 maxclone
= ifc
->ifc_maxunit
+ 1;
452 if ((len
<< 3) < maxclone
)
454 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
455 bzero(ifc
->ifc_units
, len
);
456 ifc
->ifc_bmlen
= len
;
458 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
461 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
462 err
= (*ifc
->ifc_create
)(ifc
, unit
);
464 ("%s: failed to create required interface %s%d",
465 __func__
, ifc
->ifc_name
, unit
));
467 /* Allocate the unit in the bitmap. */
469 bitoff
= unit
- (bytoff
<< 3);
470 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
475 * Unregister a network interface cloner.
478 if_clone_detach(struct if_clone
*ifc
)
481 LIST_REMOVE(ifc
, ifc_list
);
482 FREE(ifc
->ifc_units
, M_CLONE
);
488 * Provide list of interface cloners to userspace.
491 if_clone_list(int count
, int * total
, user_addr_t dst
)
493 char outbuf
[IFNAMSIZ
];
494 struct if_clone
*ifc
;
497 *total
= if_cloners_count
;
498 if (dst
== USER_ADDR_NULL
) {
499 /* Just asking how many there are. */
506 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
508 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
509 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
510 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
511 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
520 __private_extern__
int
529 ifnet_head_lock_shared();
530 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
531 ifnet_lock_shared(ifp
);
532 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
533 ifa
= ifa
->ifa_link
.tqe_next
) {
534 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
536 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
543 ifnet_lock_done(ifp
);
551 * Return the first (primary) address of a given family on an interface.
553 __private_extern__
struct ifaddr
*
554 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
556 struct ifaddr
*ifa0
= NULL
, *ifa
;
558 ifnet_lock_shared(ifp
);
559 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
560 if (ifa
->ifa_addr
->sa_family
== family
&& ifa0
== NULL
) {
567 ifnet_lock_done(ifp
);
573 * Locate an interface based on a complete address.
578 const struct sockaddr
*addr
)
582 struct ifaddr
*result
= NULL
;
584 #define equal(a1, a2) \
585 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
587 ifnet_head_lock_shared();
588 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
589 ifnet_lock_shared(ifp
);
590 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
591 ifa
= ifa
->ifa_link
.tqe_next
) {
592 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
594 if (equal(addr
, ifa
->ifa_addr
)) {
598 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
599 /* IP6 doesn't have broadcast */
600 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
601 equal(ifa
->ifa_broadaddr
, addr
)) {
608 ifnet_lock_done(ifp
);
615 * Locate the point to point interface with a given destination address.
620 const struct sockaddr
*addr
)
624 struct ifaddr
*result
= NULL
;
626 ifnet_head_lock_shared();
627 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
628 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
629 ifnet_lock_shared(ifp
);
630 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
631 ifa
= ifa
->ifa_link
.tqe_next
) {
632 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
634 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
641 ifnet_lock_done(ifp
);
649 * Locate the source address of an interface based on a complete address.
652 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
654 struct ifaddr
*result
= NULL
;
657 if (ifscope
== IFSCOPE_NONE
)
658 return (ifa_ifwithaddr(addr
));
660 ifnet_head_lock_shared();
661 if (ifscope
> (unsigned int)if_index
) {
666 ifp
= ifindex2ifnet
[ifscope
];
668 struct ifaddr
*ifa
= NULL
;
671 * This is suboptimal; there should be a better way
672 * to search for a given address of an interface.
674 ifnet_lock_shared(ifp
);
675 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
676 ifa
= ifa
->ifa_link
.tqe_next
) {
677 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
679 if (equal(addr
, ifa
->ifa_addr
)) {
683 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
684 ifa
->ifa_broadaddr
!= NULL
&&
685 /* IP6 doesn't have broadcast */
686 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
687 equal(ifa
->ifa_broadaddr
, addr
)) {
694 ifnet_lock_done(ifp
);
702 ifa_ifwithnet(const struct sockaddr
*addr
)
704 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
708 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
710 return (ifa_ifwithnet_common(addr
, ifscope
));
714 * Find an interface on a specific network. If many, choice
715 * is most specific found.
717 static struct ifaddr
*
718 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
721 struct ifaddr
*ifa
= NULL
;
722 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
723 u_int af
= addr
->sa_family
;
724 const char *addr_data
= addr
->sa_data
, *cplim
;
726 if (!ip_doscopedroute
|| addr
->sa_family
!= AF_INET
)
727 ifscope
= IFSCOPE_NONE
;
729 ifnet_head_lock_shared();
731 * AF_LINK addresses can be looked up directly by their index number,
732 * so do that if we can.
735 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
736 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
737 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
748 * Scan though each interface, looking for ones that have
749 * addresses in this address family.
751 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
752 ifnet_lock_shared(ifp
);
753 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
754 ifa
= ifa
->ifa_link
.tqe_next
) {
755 const char *cp
, *cp2
, *cp3
;
757 if (ifa
->ifa_addr
->sa_family
!= af
)
760 /* This breaks tunneling application trying to install a route with
761 * a specific subnet and the local address as the destination
762 * It's breaks binary compatibility with previous version of MacOS X
766 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
767 addr
->sa_family
!= AF_INET6
&&
769 ifp
->if_flags
& IFF_POINTOPOINT
) {
771 * This is a bit broken as it doesn't
772 * take into account that the remote end may
773 * be a single node in the network we are
775 * The trouble is that we don't know the
776 * netmask for the remote end.
778 if (ifa
->ifa_dstaddr
!= 0
779 && equal(addr
, ifa
->ifa_dstaddr
)) {
783 #endif /* __APPLE__*/
786 * If we're looking up with a scope,
787 * find using a matching interface.
789 if (ifscope
!= IFSCOPE_NONE
&&
790 ifp
->if_index
!= ifscope
)
794 * if we have a special address handler,
795 * then use it instead of the generic one.
797 if (ifa
->ifa_claim_addr
) {
798 if (ifa
->ifa_claim_addr(ifa
, addr
)) {
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 (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 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
949 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
951 ifa
= ifaof_ifpforaddr(dst
, ifp
);
954 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
955 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
961 * if_updown will set the interface up or down. It will
962 * prevent other up/down events from occurring until this
963 * up/down event has completed.
965 * Caller must lock ifnet. This function will drop the
966 * lock. This allows ifnet_set_flags to set the rest of
967 * the flags after we change the up/down state without
968 * dropping the interface lock between setting the
969 * up/down state and updating the rest of the flags.
971 __private_extern__
void
980 /* Wait until no one else is changing the up/down state */
981 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
983 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
984 ifnet_lock_done(ifp
);
985 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
986 ifnet_lock_exclusive(ifp
);
989 /* Verify that the interface isn't already in the right state */
990 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
991 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
995 /* Indicate that the up/down state is changing */
996 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
998 /* Mark interface up or down */
1000 ifp
->if_flags
|= IFF_UP
;
1003 ifp
->if_flags
&= ~IFF_UP
;
1006 ifnet_touch_lastchange(ifp
);
1008 /* Drop the lock to notify addresses and route */
1009 ifnet_lock_done(ifp
);
1010 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1011 for (i
= 0; ifa
[i
] != 0; i
++) {
1012 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1014 ifnet_free_address_list(ifa
);
1018 /* Aquire the lock to clear the changing flag and flush the send queue */
1019 ifnet_lock_exclusive(ifp
);
1021 if_qflush(&ifp
->if_snd
);
1022 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1023 wakeup(&ifp
->if_eflags
);
1029 * Mark an interface down and notify protocols of
1036 ifnet_lock_exclusive(ifp
);
1038 ifnet_lock_done(ifp
);
1042 * Mark an interface up and notify protocols of
1049 ifnet_lock_exclusive(ifp
);
1051 ifnet_lock_done(ifp
);
1055 * Flush an interface queue.
1058 if_qflush(struct ifqueue
*ifq
)
1063 while ((m
= n
) != 0) {
1067 ifq
->ifq_head
= NULL
;
1068 ifq
->ifq_tail
= NULL
;
1073 * Map interface name to
1074 * interface structure pointer.
1077 ifunit(const char *name
)
1079 char namebuf
[IFNAMSIZ
+ 1];
1087 if (len
< 2 || len
> IFNAMSIZ
)
1089 cp
= name
+ len
- 1;
1091 if (c
< '0' || c
> '9')
1092 return NULL
; /* trailing garbage */
1097 return NULL
; /* no interface name */
1098 unit
+= (c
- '0') * m
;
1100 return NULL
; /* number is unreasonable */
1103 } while (c
>= '0' && c
<= '9');
1104 len
= cp
- name
+ 1;
1105 bcopy(name
, namebuf
, len
);
1106 namebuf
[len
] = '\0';
1108 * Now search all the interfaces for this name/number
1110 ifnet_head_lock_shared();
1111 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1112 if (strncmp(ifp
->if_name
, namebuf
, len
))
1114 if (unit
== ifp
->if_unit
)
1123 * Map interface name in a sockaddr_dl to
1124 * interface structure pointer.
1127 if_withname(struct sockaddr
*sa
)
1129 char ifname
[IFNAMSIZ
+1];
1130 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1132 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1133 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1137 * ifunit wants a null-terminated name. It may not be null-terminated
1138 * in the sockaddr. We don't want to change the caller's sockaddr,
1139 * and there might not be room to put the trailing null anyway, so we
1140 * make a local copy that we know we can null terminate safely.
1143 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1144 ifname
[sdl
->sdl_nlen
] = '\0';
1145 return ifunit(ifname
);
1153 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1160 struct kev_msg ev_msg
;
1161 struct net_event_data ev_data
;
1168 struct ifconf64
* ifc
= (struct ifconf64
*)data
;
1169 user_addr_t user_addr
;
1171 user_addr
= proc_is64bit(p
)
1172 ? ifc
->ifc_req64
: CAST_USER_ADDR_T(ifc
->ifc_req
);
1173 return (ifconf(cmd
, user_addr
, &ifc
->ifc_len
));
1177 ifr
= (struct ifreq
*)data
;
1181 error
= proc_suser(p
);
1184 return ((cmd
== SIOCIFCREATE
) ?
1185 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1186 if_clone_destroy(ifr
->ifr_name
));
1188 case SIOCIFGCLONERS
:
1189 case SIOCIFGCLONERS64
:
1191 struct if_clonereq64
* ifcr
= (struct if_clonereq64
*)data
;
1192 user_addr
= proc_is64bit(p
)
1193 ? ifcr
->ifcr_ifcru
.ifcru_buffer64
1194 : CAST_USER_ADDR_T(ifcr
->ifcr_ifcru
.ifcru_buffer32
);
1195 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1198 #endif IF_CLONE_LIST
1201 ifp
= ifunit(ifr
->ifr_name
);
1207 ifnet_lock_shared(ifp
);
1208 ifr
->ifr_flags
= ifp
->if_flags
;
1209 ifnet_lock_done(ifp
);
1214 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
1220 ifnet_lock_shared(ifp
);
1221 ifr
->ifr_metric
= ifp
->if_metric
;
1222 ifnet_lock_done(ifp
);
1226 ifnet_lock_shared(ifp
);
1227 ifr
->ifr_mtu
= ifp
->if_mtu
;
1228 ifnet_lock_done(ifp
);
1232 ifnet_lock_shared(ifp
);
1233 ifr
->ifr_phys
= ifp
->if_physical
;
1234 ifnet_lock_done(ifp
);
1238 error
= proc_suser(p
);
1242 ifnet_set_flags(ifp
, ifr
->ifr_flags
, (u_int16_t
)~IFF_CANTCHANGE
);
1244 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1248 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1249 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1250 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1252 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1253 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1254 ev_data
.if_family
= ifp
->if_family
;
1255 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1256 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1257 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1258 ev_msg
.dv
[1].data_length
= 0;
1259 kev_post_msg(&ev_msg
);
1261 ifnet_touch_lastchange(ifp
);
1266 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
1272 error
= proc_suser(p
);
1275 ifp
->if_metric
= ifr
->ifr_metric
;
1278 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1279 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1280 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1282 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1283 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1284 ev_data
.if_family
= ifp
->if_family
;
1285 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1286 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1287 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1289 ev_msg
.dv
[1].data_length
= 0;
1290 kev_post_msg(&ev_msg
);
1292 ifnet_touch_lastchange(ifp
);
1296 error
= proc_suser(p
);
1300 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1304 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1305 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1306 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1308 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1309 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1310 ev_data
.if_family
= ifp
->if_family
;
1311 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1312 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1313 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1314 ev_msg
.dv
[1].data_length
= 0;
1315 kev_post_msg(&ev_msg
);
1317 ifnet_touch_lastchange(ifp
);
1323 u_long oldmtu
= ifp
->if_mtu
;
1325 error
= proc_suser(p
);
1328 if (ifp
->if_ioctl
== NULL
)
1329 return (EOPNOTSUPP
);
1330 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1333 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1337 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1338 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1339 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1341 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1342 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1343 ev_data
.if_family
= ifp
->if_family
;
1344 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1345 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1346 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1347 ev_msg
.dv
[1].data_length
= 0;
1348 kev_post_msg(&ev_msg
);
1350 ifnet_touch_lastchange(ifp
);
1354 * If the link MTU changed, do network layer specific procedure
1355 * and update all route entries associated with the interface,
1356 * so that their MTU metric gets updated.
1358 if (error
== 0 && ifp
->if_mtu
!= oldmtu
) {
1359 if_rtmtu_update(ifp
);
1369 error
= proc_suser(p
);
1373 /* Don't allow group membership on non-multicast interfaces. */
1374 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1378 /* Don't let users screw up protocols' entries. */
1379 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1383 if (cmd
== SIOCADDMULTI
) {
1384 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1385 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1387 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1388 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1391 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1392 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1393 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1394 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1396 ev_data
.if_family
= ifp
->if_family
;
1397 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1398 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1399 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1400 ev_msg
.dv
[1].data_length
= 0;
1401 kev_post_msg(&ev_msg
);
1403 ifnet_touch_lastchange(ifp
);
1407 case SIOCSIFPHYADDR
:
1408 case SIOCDIFPHYADDR
:
1410 case SIOCSIFPHYADDR_IN6
:
1412 case SIOCSLIFPHYADDR
:
1414 case SIOCSIFGENERIC
:
1419 error
= proc_suser(p
);
1423 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1427 ifnet_touch_lastchange(ifp
);
1431 ifs
= (struct ifstat
*)data
;
1432 ifs
->ascii
[0] = '\0';
1434 case SIOCGIFPSRCADDR
:
1435 case SIOCGIFPDSTADDR
:
1436 case SIOCGLIFPHYADDR
:
1438 case SIOCGIFGENERIC
:
1440 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1444 return ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1448 oif_flags
= ifp
->if_flags
;
1449 if (so
->so_proto
== 0)
1450 return (EOPNOTSUPP
);
1456 case SIOCSIFDSTADDR
:
1458 case SIOCSIFBRDADDR
:
1459 case SIOCSIFNETMASK
:
1460 #if BYTE_ORDER != BIG_ENDIAN
1461 if (ifr
->ifr_addr
.sa_family
== 0 &&
1462 ifr
->ifr_addr
.sa_len
< 16) {
1463 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1464 ifr
->ifr_addr
.sa_len
= 16;
1467 if (ifr
->ifr_addr
.sa_len
== 0)
1468 ifr
->ifr_addr
.sa_len
= 16;
1476 case OSIOCGIFDSTADDR
:
1477 cmd
= SIOCGIFDSTADDR
;
1480 case OSIOCGIFBRDADDR
:
1481 cmd
= SIOCGIFBRDADDR
;
1484 case OSIOCGIFNETMASK
:
1485 cmd
= SIOCGIFNETMASK
;
1488 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1490 socket_unlock(so
, 1);
1494 case OSIOCGIFDSTADDR
:
1495 case OSIOCGIFBRDADDR
:
1496 case OSIOCGIFNETMASK
:
1497 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1501 if (cmd
== SIOCSIFKPI
) {
1502 int temperr
= proc_suser(p
);
1507 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1508 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1517 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1521 socket_unlock(so
, 0);
1522 error
= ifioctl(so
, cmd
, data
, p
);
1528 * Set/clear promiscuous mode on interface ifp based on the truth value
1529 * of pswitch. The calls are reference counted so that only the first
1530 * "on" request actually has an effect, as does the final "off" request.
1531 * Results are undefined if the "off" and "on" requests are not matched.
1534 ifnet_set_promiscuous(
1544 ifnet_lock_exclusive(ifp
);
1546 oldflags
= ifp
->if_flags
;
1549 * If the device is not configured up, we cannot put it in
1552 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1556 if (ifp
->if_pcount
++ != 0) {
1559 ifp
->if_flags
|= IFF_PROMISC
;
1561 if (--ifp
->if_pcount
> 0)
1563 ifp
->if_flags
&= ~IFF_PROMISC
;
1565 ifr
.ifr_flags
= ifp
->if_flags
;
1567 ifnet_lock_done(ifp
);
1568 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, &ifr
);
1572 ifp
->if_flags
= oldflags
;
1574 if (locked
) ifnet_lock_done(ifp
);
1576 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1577 ifp
->if_name
, ifp
->if_unit
,
1578 pswitch
!= 0 ? "enabled" : "disabled");
1584 * Return interface configuration
1585 * of system. List may be used
1586 * in later ioctl's (above) to get
1587 * other information.
1591 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1593 struct ifnet
*ifp
= NULL
;
1600 * Zero the ifr buffer to make sure we don't
1601 * disclose the contents of the stack.
1603 bzero(&ifr
, sizeof(struct ifreq
));
1606 ifnet_head_lock_shared();
1607 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1609 size_t ifnlen
, addrs
;
1611 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1612 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1613 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1614 error
= ENAMETOOLONG
;
1617 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
1620 ifnet_lock_shared(ifp
);
1623 ifa
= ifp
->if_addrhead
.tqh_first
;
1624 for ( ; space
> sizeof (ifr
) && ifa
;
1625 ifa
= ifa
->ifa_link
.tqe_next
) {
1626 struct sockaddr
*sa
= ifa
->ifa_addr
;
1628 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1632 if (cmd
== OSIOCGIFCONF
) {
1633 struct osockaddr
*osa
=
1634 (struct osockaddr
*)&ifr
.ifr_addr
;
1636 osa
->sa_family
= sa
->sa_family
;
1637 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1638 ifrp
+= sizeof(struct ifreq
);
1639 } else if (sa
->sa_len
<= sizeof(*sa
)) {
1641 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1642 ifrp
+= sizeof(struct ifreq
);
1644 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1646 space
-= sa
->sa_len
- sizeof(*sa
);
1647 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1649 error
= copyout((caddr_t
)sa
,
1650 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1653 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1657 space
-= sizeof (ifr
);
1659 ifnet_lock_done(ifp
);
1664 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1665 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1668 space
-= sizeof (ifr
);
1669 ifrp
+= sizeof(struct ifreq
);
1673 *ret_space
-= space
;
1678 * Just like if_promisc(), but for all-multicast-reception mode.
1681 if_allmulti(struct ifnet
*ifp
, int onswitch
)
1686 ifnet_lock_exclusive(ifp
);
1689 if (ifp
->if_amcount
++ == 0) {
1690 ifp
->if_flags
|= IFF_ALLMULTI
;
1694 if (ifp
->if_amcount
> 1) {
1697 ifp
->if_amcount
= 0;
1698 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1702 ifnet_lock_done(ifp
);
1705 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
1714 struct ifmultiaddr
*ifma
)
1716 if (OSIncrementAtomic((SInt32
*)&ifma
->ifma_refcount
) <= 0)
1717 panic("ifma_reference: ifma already released or invalid\n");
1722 struct ifmultiaddr
*ifma
)
1725 struct ifmultiaddr
*next
;
1726 int32_t prevValue
= OSDecrementAtomic((SInt32
*)&ifma
->ifma_refcount
);
1728 panic("ifma_release: ifma already released or invalid\n");
1732 /* Allow the allocator of the protospec to free it */
1733 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1734 ifma
->ifma_free(ifma
->ifma_protospec
);
1737 next
= ifma
->ifma_ll
;
1738 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1739 FREE(ifma
, M_IFMADDR
);
1745 * Find an ifmultiaddr that matches a socket address on an interface.
1747 * Caller is responsible for holding the ifnet_lock while calling
1751 if_addmulti_doesexist(
1753 const struct sockaddr
*sa
,
1754 struct ifmultiaddr
**retifma
)
1756 struct ifmultiaddr
*ifma
;
1757 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1758 ifma
= ifma
->ifma_link
.le_next
) {
1759 if (equal(sa
, ifma
->ifma_addr
)) {
1760 ifma
->ifma_usecount
++;
1763 ifma_reference(*retifma
);
1773 * Radar 3642395, make sure all multicasts are in a standard format.
1775 static struct sockaddr
*
1777 const struct sockaddr
*original
)
1780 const u_char
*aptr
= NULL
;
1781 struct sockaddr
*copy
= NULL
;
1782 struct sockaddr_dl
*sdl_new
= NULL
;
1785 if (original
->sa_family
!= AF_LINK
&&
1786 original
->sa_family
!= AF_UNSPEC
) {
1787 /* Just make a copy */
1788 MALLOC(copy
, struct sockaddr
*, original
->sa_len
, M_IFADDR
, M_WAITOK
);
1790 bcopy(original
, copy
, original
->sa_len
);
1794 switch (original
->sa_family
) {
1796 const struct sockaddr_dl
*sdl_original
=
1797 (const struct sockaddr_dl
*)original
;
1799 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+ sdl_original
->sdl_slen
+
1800 offsetof(struct sockaddr_dl
, sdl_data
) > sdl_original
->sdl_len
)
1803 alen
= sdl_original
->sdl_alen
;
1804 aptr
= CONST_LLADDR(sdl_original
);
1809 if (original
->sa_len
< ETHER_ADDR_LEN
+
1810 offsetof(struct sockaddr
, sa_data
)) {
1814 alen
= ETHER_ADDR_LEN
;
1815 aptr
= (const u_char
*)original
->sa_data
;
1820 if (alen
== 0 || aptr
== NULL
)
1823 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
1824 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
1826 if (sdl_new
!= NULL
) {
1827 bzero(sdl_new
, len
);
1828 sdl_new
->sdl_len
= len
;
1829 sdl_new
->sdl_family
= AF_LINK
;
1830 sdl_new
->sdl_alen
= alen
;
1831 bcopy(aptr
, LLADDR(sdl_new
), alen
);
1834 return (struct sockaddr
*)sdl_new
;
1838 * Add a multicast listenership to the interface in question.
1839 * The link layer provides a routine which converts
1843 struct ifnet
*ifp
, /* interface to manipulate */
1844 const struct sockaddr
*sa
, /* address to add */
1845 struct ifmultiaddr
**retifma
)
1847 struct sockaddr_storage storage
;
1848 struct sockaddr
*llsa
= NULL
;
1849 struct sockaddr
*dupsa
= NULL
;
1851 struct ifmultiaddr
*ifma
= NULL
;
1852 struct ifmultiaddr
*llifma
= NULL
;
1854 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
1855 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
1856 dupsa
= copy_and_normalize(sa
);
1857 if (dupsa
== NULL
) {
1863 ifnet_lock_exclusive(ifp
);
1864 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1865 ifnet_lock_done(ifp
);
1872 * Give the link layer a chance to accept/reject it, and also
1873 * find out which AF_LINK address this maps to, if it isn't one
1876 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
1878 if (error
== 0 && storage
.ss_len
!= 0) {
1879 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
1885 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1886 if (llifma
== NULL
) {
1892 /* to be similar to FreeBSD */
1893 if (error
== EOPNOTSUPP
) {
1900 /* Allocate while we aren't holding any locks */
1901 if (dupsa
== NULL
) {
1902 dupsa
= copy_and_normalize(sa
);
1903 if (dupsa
== NULL
) {
1908 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1914 ifnet_lock_exclusive(ifp
);
1916 * Check again for the matching multicast.
1918 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1919 ifnet_lock_done(ifp
);
1923 bzero(ifma
, sizeof(*ifma
));
1924 ifma
->ifma_addr
= dupsa
;
1925 ifma
->ifma_ifp
= ifp
;
1926 ifma
->ifma_usecount
= 1;
1927 ifma
->ifma_refcount
= 1;
1930 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1931 FREE(llsa
, M_IFMADDR
);
1932 FREE(llifma
, M_IFMADDR
);
1934 bzero(llifma
, sizeof(*llifma
));
1935 llifma
->ifma_addr
= llsa
;
1936 llifma
->ifma_ifp
= ifp
;
1937 llifma
->ifma_usecount
= 1;
1938 llifma
->ifma_refcount
= 1;
1939 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1941 ifma
->ifma_ll
= llifma
;
1942 ifma_reference(ifma
->ifma_ll
);
1946 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1950 ifma_reference(*retifma
);
1953 ifnet_lock_done(ifp
);
1956 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1959 * We are certain we have added something, so call down to the
1960 * interface to let them know about it.
1962 ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
1968 FREE(ifma
, M_IFADDR
);
1970 FREE(dupsa
, M_IFADDR
);
1972 FREE(llifma
, M_IFADDR
);
1974 FREE(llsa
, M_IFADDR
);
1981 struct ifmultiaddr
*ifma
,
1985 int do_del_multi
= 0;
1987 ifp
= ifma
->ifma_ifp
;
1989 if (!locked
&& ifp
) {
1990 ifnet_lock_exclusive(ifp
);
1993 while (ifma
!= NULL
) {
1994 struct ifmultiaddr
*ll_ifma
;
1996 if (ifma
->ifma_usecount
> 1) {
1997 ifma
->ifma_usecount
--;
2002 LIST_REMOVE(ifma
, ifma_link
);
2004 ll_ifma
= ifma
->ifma_ll
;
2006 if (ll_ifma
) { /* send a routing msg for network addresses only */
2008 ifnet_lock_done(ifp
);
2009 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2011 ifnet_lock_exclusive(ifp
);
2015 * Make sure the interface driver is notified
2016 * in the case of a link layer mcast group being left.
2019 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
2030 if (!locked
&& ifp
) {
2031 /* This wasn't initially locked, we should unlock it */
2032 ifnet_lock_done(ifp
);
2037 ifnet_lock_done(ifp
);
2038 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
2040 ifnet_lock_exclusive(ifp
);
2047 * Remove a reference to a multicast address on this interface. Yell
2048 * if the request does not match an existing membership.
2053 const struct sockaddr
*sa
)
2055 struct ifmultiaddr
*ifma
;
2056 struct sockaddr
*dupsa
= NULL
;
2059 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
2060 dupsa
= copy_and_normalize(sa
);
2061 if (dupsa
== NULL
) {
2067 ifnet_lock_exclusive(ifp
);
2068 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2069 ifma
= ifma
->ifma_link
.le_next
)
2070 if (equal(sa
, ifma
->ifma_addr
))
2073 ifnet_lock_done(ifp
);
2075 FREE(dupsa
, M_IFADDR
);
2079 retval
= if_delmultiaddr(ifma
, 1);
2080 ifnet_lock_done(ifp
);
2082 FREE(dupsa
, M_IFADDR
);
2089 * We don't use if_setlladdr, our interfaces are responsible for
2090 * handling the SIOCSIFLLADDR ioctl.
2094 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2100 struct ifmultiaddr
*
2101 ifmaof_ifpforaddr(const struct sockaddr
*sa
, struct ifnet
*ifp
)
2103 struct ifmultiaddr
*ifma
;
2105 ifnet_lock_shared(ifp
);
2106 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
2107 ifma
= ifma
->ifma_link
.le_next
)
2108 if (equal(ifma
->ifma_addr
, sa
))
2110 ifnet_lock_done(ifp
);
2115 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
2116 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Generic link-management");
2120 * Shutdown all network activity. Used boot() when halting
2130 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
2131 for (i
= 0; i
< count
; i
++) {
2133 dlil_proto_unplumb_all(ifp
[i
]);
2135 ifnet_list_free(ifp
);
2142 * Delete Routes for a Network Interface
2144 * Called for each routing entry via the rnh->rnh_walktree() call above
2145 * to delete all route entries referencing a detaching network interface.
2148 * rn pointer to node in the routing table
2149 * arg argument passed to rnh->rnh_walktree() - detaching interface
2153 * errno failed - reason indicated
2158 struct radix_node
*rn
,
2161 struct rtentry
*rt
= (struct rtentry
*)rn
;
2162 struct ifnet
*ifp
= arg
;
2165 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
2168 * Protect (sorta) against walktree recursion problems
2169 * with cloned routes
2171 if ((rt
->rt_flags
& RTF_UP
) == 0)
2174 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
2175 rt_mask(rt
), rt
->rt_flags
,
2176 (struct rtentry
**) NULL
);
2178 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
2186 * Removes routing table reference to a given interfacei
2187 * for a given protocol family
2189 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
2191 struct radix_node_head
*rnh
;
2194 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
2195 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
2196 lck_mtx_lock(rt_mtx
);
2197 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
2198 lck_mtx_unlock(rt_mtx
);
2203 if_rtmtu(struct radix_node
*rn
, void *arg
)
2205 struct rtentry
*rt
= (struct rtentry
*)rn
;
2206 struct ifnet
*ifp
= arg
;
2208 if (rt
->rt_ifp
== ifp
) {
2210 * Update the MTU of this entry only if the MTU
2211 * has not been locked (RTV_MTU is not set) and
2212 * if it was non-zero to begin with.
2214 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
2215 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
2222 * Update the MTU metric of all route entries in all protocol tables
2223 * associated with a particular interface; this is called when the
2224 * MTU of that interface has changed.
2227 void if_rtmtu_update(struct ifnet
*ifp
)
2229 struct radix_node_head
*rnh
;
2232 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
2233 if ((rnh
= rt_tables
[p
]) == NULL
)
2236 lck_mtx_lock(rt_mtx
);
2237 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
2238 lck_mtx_unlock(rt_mtx
);
2245 __private_extern__
void
2246 if_data_internal_to_if_data(
2248 const struct if_data_internal
*if_data_int
,
2249 struct if_data
*if_data
)
2251 struct dlil_threading_info
*thread
;
2252 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2253 thread
= dlil_lo_thread_ptr
;
2255 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
2256 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
2257 COPYFIELD(ifi_type
);
2258 COPYFIELD(ifi_typelen
);
2259 COPYFIELD(ifi_physical
);
2260 COPYFIELD(ifi_addrlen
);
2261 COPYFIELD(ifi_hdrlen
);
2262 COPYFIELD(ifi_recvquota
);
2263 COPYFIELD(ifi_xmitquota
);
2264 if_data
->ifi_unused1
= 0;
2266 COPYFIELD(ifi_metric
);
2267 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
2268 if_data
->ifi_baudrate
= 0xFFFFFFFF;
2271 COPYFIELD32(ifi_baudrate
);
2274 lck_mtx_lock(thread
->input_lck
);
2275 COPYFIELD32(ifi_ipackets
);
2276 COPYFIELD32(ifi_ierrors
);
2277 COPYFIELD32(ifi_opackets
);
2278 COPYFIELD32(ifi_oerrors
);
2279 COPYFIELD32(ifi_collisions
);
2280 COPYFIELD32(ifi_ibytes
);
2281 COPYFIELD32(ifi_obytes
);
2282 COPYFIELD32(ifi_imcasts
);
2283 COPYFIELD32(ifi_omcasts
);
2284 COPYFIELD32(ifi_iqdrops
);
2285 COPYFIELD32(ifi_noproto
);
2286 COPYFIELD32(ifi_recvtiming
);
2287 COPYFIELD32(ifi_xmittiming
);
2288 COPYFIELD(ifi_lastchange
);
2289 lck_mtx_unlock(thread
->input_lck
);
2291 #if IF_LASTCHANGEUPTIME
2292 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2295 if_data
->ifi_unused2
= 0;
2296 COPYFIELD(ifi_hwassist
);
2297 if_data
->ifi_reserved1
= 0;
2298 if_data
->ifi_reserved2
= 0;
2303 __private_extern__
void
2304 if_data_internal_to_if_data64(
2306 const struct if_data_internal
*if_data_int
,
2307 struct if_data64
*if_data64
)
2309 struct dlil_threading_info
*thread
;
2310 if ((thread
= ifp
->if_input_thread
) == NULL
|| (dlil_multithreaded_input
== 0))
2311 thread
= dlil_lo_thread_ptr
;
2313 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2314 COPYFIELD(ifi_type
);
2315 COPYFIELD(ifi_typelen
);
2316 COPYFIELD(ifi_physical
);
2317 COPYFIELD(ifi_addrlen
);
2318 COPYFIELD(ifi_hdrlen
);
2319 COPYFIELD(ifi_recvquota
);
2320 COPYFIELD(ifi_xmitquota
);
2321 if_data64
->ifi_unused1
= 0;
2323 COPYFIELD(ifi_metric
);
2324 COPYFIELD(ifi_baudrate
);
2326 lck_mtx_lock(thread
->input_lck
);
2327 COPYFIELD(ifi_ipackets
);
2328 COPYFIELD(ifi_ierrors
);
2329 COPYFIELD(ifi_opackets
);
2330 COPYFIELD(ifi_oerrors
);
2331 COPYFIELD(ifi_collisions
);
2332 COPYFIELD(ifi_ibytes
);
2333 COPYFIELD(ifi_obytes
);
2334 COPYFIELD(ifi_imcasts
);
2335 COPYFIELD(ifi_omcasts
);
2336 COPYFIELD(ifi_iqdrops
);
2337 COPYFIELD(ifi_noproto
);
2338 COPYFIELD(ifi_recvtiming
);
2339 COPYFIELD(ifi_xmittiming
);
2340 COPYFIELD(ifi_lastchange
);
2341 lck_mtx_unlock(thread
->input_lck
);
2343 #if IF_LASTCHANGEUPTIME
2344 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();