2 * Copyright (c) 2000 Apple Computer, 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 #include <kern/locks.h>
66 #include <sys/param.h>
67 #include <sys/malloc.h>
69 #include <sys/systm.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/protosw.h>
74 #include <sys/kernel.h>
75 #include <sys/sockio.h>
76 #include <sys/syslog.h>
77 #include <sys/sysctl.h>
80 #include <net/if_arp.h>
81 #include <net/if_dl.h>
82 #include <net/if_types.h>
83 #include <net/if_var.h>
84 #include <net/net_osdep.h>
86 #include <net/radix.h>
87 #include <net/route.h>
91 #include <sys/domain.h>
92 #include <libkern/OSAtomic.h>
95 #if defined(INET) || defined(INET6)
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet6/in6_ifattach.h>
106 * System initialization
109 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
110 static void if_qflush(struct ifqueue
*);
111 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
112 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
114 static struct if_clone
*if_clone_lookup(const char *, int *);
116 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
119 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
120 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
122 int ifqmaxlen
= IFQ_MAXLEN
;
123 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
125 static int if_cloners_count
;
126 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
130 * XXX: declare here to avoid to include many inet6 related files..
131 * should be more generalized?
133 extern void nd6_setmtu(struct ifnet
*);
136 #define M_CLONE M_IFADDR
139 * Network interface utility routines.
141 * Routines with ifa_ifwith* names take sockaddr *'s as
146 struct ifaddr
**ifnet_addrs
;
147 struct ifnet
**ifindex2ifnet
;
149 __private_extern__
void
154 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
155 if (ifa
->ifa_debug
& IFA_ATTACHED
) {
156 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
159 ifa
->ifa_debug
|= IFA_ATTACHED
;
160 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
163 __private_extern__
void
168 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
171 if ((ifa
->ifa_debug
& IFA_ATTACHED
) == 0) {
172 printf("if_detach_ifa: ifa is not attached to any interface! flags=%\n", ifa
->ifa_debug
);
177 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
182 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
186 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
187 ifa
->ifa_debug
&= ~IFA_ATTACHED
;
191 #define INITIAL_IF_INDEXLIM 8
194 * Function: if_next_index
196 * Return the next available interface index.
197 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
198 * added entry when necessary.
201 * ifnet_addrs[] is indexed by (if_index - 1), whereas
202 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
203 * always allocate one extra element to hold ifindex2ifnet[0], which
206 int if_next_index(void);
208 __private_extern__
int
211 static int if_indexlim
= 0;
214 new_index
= ++if_index
;
215 if (if_index
> if_indexlim
) {
218 caddr_t new_ifnet_addrs
;
219 caddr_t new_ifindex2ifnet
;
220 caddr_t old_ifnet_addrs
;
222 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
223 if (ifnet_addrs
== NULL
) {
224 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
226 new_if_indexlim
= if_indexlim
<< 1;
229 /* allocate space for the larger arrays */
230 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
231 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
232 new_ifindex2ifnet
= new_ifnet_addrs
233 + new_if_indexlim
* sizeof(caddr_t
);
234 bzero(new_ifnet_addrs
, n
);
235 if (ifnet_addrs
!= NULL
) {
236 /* copy the existing data */
237 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
238 if_indexlim
* sizeof(caddr_t
));
239 bcopy((caddr_t
)ifindex2ifnet
,
241 (if_indexlim
+ 1) * sizeof(caddr_t
));
244 /* switch to the new tables and size */
245 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
246 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
247 if_indexlim
= new_if_indexlim
;
249 /* release the old data */
250 if (old_ifnet_addrs
!= NULL
) {
251 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
258 * Create a clone network interface.
261 if_clone_create(char *name
, int len
)
263 struct if_clone
*ifc
;
265 int wildcard
, bytoff
, bitoff
;
269 ifc
= if_clone_lookup(name
, &unit
);
273 if (ifunit(name
) != NULL
)
277 wildcard
= (unit
< 0);
279 * Find a free unit if none was given.
282 while ((bytoff
< ifc
->ifc_bmlen
)
283 && (ifc
->ifc_units
[bytoff
] == 0xff))
285 if (bytoff
>= ifc
->ifc_bmlen
)
287 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
289 unit
= (bytoff
<< 3) + bitoff
;
292 if (unit
> ifc
->ifc_maxunit
)
295 err
= (*ifc
->ifc_create
)(ifc
, unit
);
301 bitoff
= unit
- (bytoff
<< 3);
305 * Allocate the unit in the bitmap.
307 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
308 ("%s: bit is already set", __func__
));
309 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
311 /* In the wildcard case, we need to update the name. */
313 for (dp
= name
; *dp
!= '\0'; dp
++);
314 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
315 len
- (dp
-name
) - 1) {
317 * This can only be a programmer error and
318 * there's no straightforward way to recover if
321 panic("if_clone_create(): interface name too long");
330 * Destroy a clone network interface.
333 if_clone_destroy(const char *name
)
335 struct if_clone
*ifc
;
340 ifc
= if_clone_lookup(name
, &unit
);
344 if (unit
< ifc
->ifc_minifs
)
351 if (ifc
->ifc_destroy
== NULL
)
354 (*ifc
->ifc_destroy
)(ifp
);
357 * Compute offset in the bitmap and deallocate the unit.
360 bitoff
= unit
- (bytoff
<< 3);
361 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
362 ("%s: bit is already cleared", __func__
));
363 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
368 * Look up a network interface cloner.
371 static struct if_clone
*
372 if_clone_lookup(const char *name
, int *unitp
)
374 struct if_clone
*ifc
;
378 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
379 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
380 if (ifc
->ifc_name
[i
] != *cp
)
385 ifc
= LIST_NEXT(ifc
, ifc_list
);
389 return ((struct if_clone
*)NULL
);
395 for (i
= 0; *cp
!= '\0'; cp
++) {
396 if (*cp
< '0' || *cp
> '9') {
397 /* Bogus unit number. */
400 i
= (i
* 10) + (*cp
- '0');
410 * Register a network interface cloner.
413 if_clone_attach(struct if_clone
*ifc
)
420 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
421 ("%s: %s requested more units then allowed (%d > %d)",
422 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
423 ifc
->ifc_maxunit
+ 1));
425 * Compute bitmap size and allocate it.
427 maxclone
= ifc
->ifc_maxunit
+ 1;
429 if ((len
<< 3) < maxclone
)
431 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
432 bzero(ifc
->ifc_units
, len
);
433 ifc
->ifc_bmlen
= len
;
435 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
438 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
439 err
= (*ifc
->ifc_create
)(ifc
, unit
);
441 ("%s: failed to create required interface %s%d",
442 __func__
, ifc
->ifc_name
, unit
));
444 /* Allocate the unit in the bitmap. */
446 bitoff
= unit
- (bytoff
<< 3);
447 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
452 * Unregister a network interface cloner.
455 if_clone_detach(struct if_clone
*ifc
)
458 LIST_REMOVE(ifc
, ifc_list
);
459 FREE(ifc
->ifc_units
, M_CLONE
);
465 * Provide list of interface cloners to userspace.
468 if_clone_list(int count
, int * total
, user_addr_t dst
)
470 char outbuf
[IFNAMSIZ
];
471 struct if_clone
*ifc
;
474 *total
= if_cloners_count
;
475 if (dst
== USER_ADDR_NULL
) {
476 /* Just asking how many there are. */
483 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
485 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
486 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
487 strncpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
- 1);
488 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
497 int ifa_foraddr(unsigned int addr
);
498 __private_extern__
int
507 ifnet_head_lock_shared();
508 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
509 ifnet_lock_shared(ifp
);
510 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
511 ifa
= ifa
->ifa_link
.tqe_next
) {
512 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
514 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
521 ifnet_lock_done(ifp
);
529 * Locate an interface based on a complete address.
534 const struct sockaddr
*addr
)
538 struct ifaddr
*result
= 0;
540 #define equal(a1, a2) \
541 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
543 ifnet_head_lock_shared();
544 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
545 ifnet_lock_shared(ifp
);
546 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
547 ifa
= ifa
->ifa_link
.tqe_next
) {
548 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
550 if (equal(addr
, ifa
->ifa_addr
)) {
554 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
555 /* IP6 doesn't have broadcast */
556 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
557 equal(ifa
->ifa_broadaddr
, addr
)) {
564 ifnet_lock_done(ifp
);
571 * Locate the point to point interface with a given destination address.
576 const struct sockaddr
*addr
)
580 struct ifaddr
*result
= 0;
582 ifnet_head_lock_shared();
583 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
584 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
585 ifnet_lock_shared(ifp
);
586 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
587 ifa
= ifa
->ifa_link
.tqe_next
) {
588 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
590 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
597 ifnet_lock_done(ifp
);
605 * Find an interface on a specific network. If many, choice
606 * is most specific found.
610 const struct sockaddr
*addr
)
613 struct ifaddr
*ifa
= NULL
;
614 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
615 u_int af
= addr
->sa_family
;
616 char *addr_data
= addr
->sa_data
, *cplim
;
618 ifnet_head_lock_shared();
620 * AF_LINK addresses can be looked up directly by their index number,
621 * so do that if we can.
624 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
625 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
626 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
637 * Scan though each interface, looking for ones that have
638 * addresses in this address family.
640 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
641 ifnet_lock_shared(ifp
);
642 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
643 ifa
= ifa
->ifa_link
.tqe_next
) {
644 char *cp
, *cp2
, *cp3
;
646 if (ifa
->ifa_addr
->sa_family
!= af
)
649 /* This breaks tunneling application trying to install a route with
650 * a specific subnet and the local address as the destination
651 * It's breaks binary compatibility with previous version of MacOS X
655 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
656 addr
->sa_family
!= AF_INET6
&&
658 ifp
->if_flags
& IFF_POINTOPOINT
) {
660 * This is a bit broken as it doesn't
661 * take into account that the remote end may
662 * be a single node in the network we are
664 * The trouble is that we don't know the
665 * netmask for the remote end.
667 if (ifa
->ifa_dstaddr
!= 0
668 && equal(addr
, ifa
->ifa_dstaddr
)) {
672 #endif /* __APPLE__*/
675 * if we have a special address handler,
676 * then use it instead of the generic one.
678 if (ifa
->ifa_claim_addr
) {
679 if (ifa
->ifa_claim_addr(ifa
, addr
)) {
687 * Scan all the bits in the ifa's address.
688 * If a bit dissagrees with what we are
689 * looking for, mask it with the netmask
690 * to see if it really matters.
693 if (ifa
->ifa_netmask
== 0)
696 cp2
= ifa
->ifa_addr
->sa_data
;
697 cp3
= ifa
->ifa_netmask
->sa_data
;
698 cplim
= ifa
->ifa_netmask
->sa_len
699 + (char *)ifa
->ifa_netmask
;
701 if ((*cp
++ ^ *cp2
++) & *cp3
++)
702 goto next
; /* next address! */
704 * If the netmask of what we just found
705 * is more specific than what we had before
706 * (if we had one) then remember the new one
707 * before continuing to search
708 * for an even better one.
710 if (ifa_maybe
== 0 ||
711 rn_refines((caddr_t
)ifa
->ifa_netmask
,
712 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
726 * ifa is set if we found an exact match.
727 * take a reference to the ifa before
728 * releasing the ifp lock
730 ifnet_lock_done(ifp
);
739 else if (ifa_maybe
) {
747 * Find an interface address specific to an interface best matching
752 const struct sockaddr
*addr
,
755 struct ifaddr
*ifa
= 0;
756 const char *cp
, *cp2
, *cp3
;
758 struct ifaddr
*ifa_maybe
= 0;
759 u_int af
= addr
->sa_family
;
764 ifnet_lock_shared(ifp
);
765 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
766 ifa
= ifa
->ifa_link
.tqe_next
) {
767 if (ifa
->ifa_addr
->sa_family
!= af
)
771 if (ifa
->ifa_netmask
== 0) {
772 if (equal(addr
, ifa
->ifa_addr
) ||
773 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
777 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
778 if (equal(addr
, ifa
->ifa_dstaddr
))
782 cp2
= ifa
->ifa_addr
->sa_data
;
783 cp3
= ifa
->ifa_netmask
->sa_data
;
784 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
785 for (; cp3
< cplim
; cp3
++)
786 if ((*cp
++ ^ *cp2
++) & *cp3
)
793 if (!ifa
) ifa
= ifa_maybe
;
794 if (ifa
) ifaref(ifa
);
796 ifnet_lock_done(ifp
);
800 #include <net/route.h>
803 * Default action when installing a route with a Link Level gateway.
804 * Lookup an appropriate real ifa to point to.
805 * This should be moved to /sys/net/link.c eventually.
808 link_rtrequest(cmd
, rt
, sa
)
814 struct sockaddr
*dst
;
817 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
818 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
820 ifa
= ifaof_ifpforaddr(dst
, ifp
);
823 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
824 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
830 * if_updown will set the interface up or down. It will
831 * prevent other up/down events from occurring until this
832 * up/down event has completed.
834 * Caller must lock ifnet. This function will drop the
835 * lock. This allows ifnet_set_flags to set the rest of
836 * the flags after we change the up/down state without
837 * dropping the interface lock between setting the
838 * up/down state and updating the rest of the flags.
840 __private_extern__
void
849 /* Wait until no one else is changing the up/down state */
850 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
852 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
853 ifnet_lock_done(ifp
);
854 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
855 ifnet_lock_exclusive(ifp
);
858 /* Verify that the interface isn't already in the right state */
859 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
860 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
864 /* Indicate that the up/down state is changing */
865 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
867 /* Mark interface up or down */
869 ifp
->if_flags
|= IFF_UP
;
872 ifp
->if_flags
&= ~IFF_UP
;
875 ifnet_touch_lastchange(ifp
);
877 /* Drop the lock to notify addresses and route */
878 ifnet_lock_done(ifp
);
879 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
880 for (i
= 0; ifa
[i
] != 0; i
++) {
881 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
883 ifnet_free_address_list(ifa
);
887 /* Aquire the lock to clear the changing flag and flush the send queue */
888 ifnet_lock_exclusive(ifp
);
890 if_qflush(&ifp
->if_snd
);
891 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
892 wakeup(&ifp
->if_eflags
);
898 * Mark an interface down and notify protocols of
905 ifnet_lock_exclusive(ifp
);
907 ifnet_lock_done(ifp
);
911 * Mark an interface up and notify protocols of
918 ifnet_lock_exclusive(ifp
);
920 ifnet_lock_done(ifp
);
924 * Flush an interface queue.
933 while ((m
= n
) != 0) {
943 * Map interface name to
944 * interface structure pointer.
947 ifunit(const char *name
)
949 char namebuf
[IFNAMSIZ
+ 1];
957 if (len
< 2 || len
> IFNAMSIZ
)
961 if (c
< '0' || c
> '9')
962 return NULL
; /* trailing garbage */
967 return NULL
; /* no interface name */
968 unit
+= (c
- '0') * m
;
970 return NULL
; /* number is unreasonable */
973 } while (c
>= '0' && c
<= '9');
975 bcopy(name
, namebuf
, len
);
978 * Now search all the interfaces for this name/number
980 ifnet_head_lock_shared();
981 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
982 if (strcmp(ifp
->if_name
, namebuf
))
984 if (unit
== ifp
->if_unit
)
993 * Map interface name in a sockaddr_dl to
994 * interface structure pointer.
1000 char ifname
[IFNAMSIZ
+1];
1001 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1003 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1004 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1008 * ifunit wants a null-terminated name. It may not be null-terminated
1009 * in the sockaddr. We don't want to change the caller's sockaddr,
1010 * and there might not be room to put the trailing null anyway, so we
1011 * make a local copy that we know we can null terminate safely.
1014 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1015 ifname
[sdl
->sdl_nlen
] = '\0';
1016 return ifunit(ifname
);
1024 ifioctl(so
, cmd
, data
, p
)
1035 struct kev_msg ev_msg
;
1036 struct net_event_data ev_data
;
1043 struct ifconf64
* ifc
= (struct ifconf64
*)data
;
1044 user_addr_t user_addr
;
1046 user_addr
= proc_is64bit(p
)
1047 ? ifc
->ifc_req64
: CAST_USER_ADDR_T(ifc
->ifc_req
);
1048 return (ifconf(cmd
, user_addr
, &ifc
->ifc_len
));
1052 ifr
= (struct ifreq
*)data
;
1056 error
= proc_suser(p
);
1059 return ((cmd
== SIOCIFCREATE
) ?
1060 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1061 if_clone_destroy(ifr
->ifr_name
));
1063 case SIOCIFGCLONERS
:
1064 case SIOCIFGCLONERS64
:
1066 struct if_clonereq64
* ifcr
= (struct if_clonereq64
*)data
;
1067 user_addr
= proc_is64bit(p
)
1068 ? ifcr
->ifcr_ifcru
.ifcru_buffer64
1069 : CAST_USER_ADDR_T(ifcr
->ifcr_ifcru
.ifcru_buffer32
);
1070 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1073 #endif IF_CLONE_LIST
1076 ifp
= ifunit(ifr
->ifr_name
);
1082 ifnet_lock_shared(ifp
);
1083 ifr
->ifr_flags
= ifp
->if_flags
;
1084 ifnet_lock_done(ifp
);
1088 ifnet_lock_shared(ifp
);
1089 ifr
->ifr_metric
= ifp
->if_metric
;
1090 ifnet_lock_done(ifp
);
1094 ifnet_lock_shared(ifp
);
1095 ifr
->ifr_mtu
= ifp
->if_mtu
;
1096 ifnet_lock_done(ifp
);
1100 ifnet_lock_shared(ifp
);
1101 ifr
->ifr_phys
= ifp
->if_physical
;
1102 ifnet_lock_done(ifp
);
1106 error
= proc_suser(p
);
1110 ifnet_set_flags(ifp
, ifr
->ifr_flags
, ~IFF_CANTCHANGE
);
1112 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1113 ifp
, cmd
, (caddr_t
) data
);
1116 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1117 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1118 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1120 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1121 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1122 ev_data
.if_family
= ifp
->if_family
;
1123 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1124 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1125 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1126 ev_msg
.dv
[1].data_length
= 0;
1127 kev_post_msg(&ev_msg
);
1129 ifnet_touch_lastchange(ifp
);
1133 error
= proc_suser(p
);
1136 ifp
->if_metric
= ifr
->ifr_metric
;
1139 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1140 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1141 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1143 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1144 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1145 ev_data
.if_family
= ifp
->if_family
;
1146 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1147 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1148 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1150 ev_msg
.dv
[1].data_length
= 0;
1151 kev_post_msg(&ev_msg
);
1153 ifnet_touch_lastchange(ifp
);
1157 error
= proc_suser(p
);
1161 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1162 ifp
, cmd
, (caddr_t
) data
);
1165 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1166 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1167 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1169 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1170 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1171 ev_data
.if_family
= ifp
->if_family
;
1172 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1173 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1174 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1175 ev_msg
.dv
[1].data_length
= 0;
1176 kev_post_msg(&ev_msg
);
1178 ifnet_touch_lastchange(ifp
);
1184 u_long oldmtu
= ifp
->if_mtu
;
1186 error
= proc_suser(p
);
1189 if (ifp
->if_ioctl
== NULL
)
1190 return (EOPNOTSUPP
);
1191 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1194 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1195 ifp
, cmd
, (caddr_t
) data
);
1198 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1199 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1200 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1202 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1203 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1204 ev_data
.if_family
= ifp
->if_family
;
1205 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1206 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1207 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1208 ev_msg
.dv
[1].data_length
= 0;
1209 kev_post_msg(&ev_msg
);
1211 ifnet_touch_lastchange(ifp
);
1215 * If the link MTU changed, do network layer specific procedure.
1217 if (ifp
->if_mtu
!= oldmtu
) {
1227 error
= proc_suser(p
);
1231 /* Don't allow group membership on non-multicast interfaces. */
1232 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1236 /* Don't let users screw up protocols' entries. */
1237 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1241 if (cmd
== SIOCADDMULTI
) {
1242 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1243 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1245 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1246 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1249 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1250 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1251 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1252 strncpy(&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
);
1265 case SIOCSIFPHYADDR
:
1266 case SIOCDIFPHYADDR
:
1268 case SIOCSIFPHYADDR_IN6
:
1270 case SIOCSLIFPHYADDR
:
1272 case SIOCSIFGENERIC
:
1277 error
= proc_suser(p
);
1281 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1282 ifp
, cmd
, (caddr_t
) data
);
1285 ifnet_touch_lastchange(ifp
);
1289 ifs
= (struct ifstat
*)data
;
1290 ifs
->ascii
[0] = '\0';
1292 case SIOCGIFPSRCADDR
:
1293 case SIOCGIFPDSTADDR
:
1294 case SIOCGLIFPHYADDR
:
1296 case SIOCGIFGENERIC
:
1298 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1299 ifp
, cmd
, (caddr_t
) data
);
1302 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1303 ifp
, cmd
, (caddr_t
) data
);
1306 oif_flags
= ifp
->if_flags
;
1307 if (so
->so_proto
== 0)
1308 return (EOPNOTSUPP
);
1309 #if !COMPAT_43_SOCKET
1311 error
=(*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
, p
));
1312 socket_unlock(so
, 1);
1320 case SIOCSIFDSTADDR
:
1322 case SIOCSIFBRDADDR
:
1323 case SIOCSIFNETMASK
:
1324 #if BYTE_ORDER != BIG_ENDIAN
1325 if (ifr
->ifr_addr
.sa_family
== 0 &&
1326 ifr
->ifr_addr
.sa_len
< 16) {
1327 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1328 ifr
->ifr_addr
.sa_len
= 16;
1331 if (ifr
->ifr_addr
.sa_len
== 0)
1332 ifr
->ifr_addr
.sa_len
= 16;
1340 case OSIOCGIFDSTADDR
:
1341 cmd
= SIOCGIFDSTADDR
;
1344 case OSIOCGIFBRDADDR
:
1345 cmd
= SIOCGIFBRDADDR
;
1348 case OSIOCGIFNETMASK
:
1349 cmd
= SIOCGIFNETMASK
;
1352 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1354 socket_unlock(so
, 1);
1358 case OSIOCGIFDSTADDR
:
1359 case OSIOCGIFBRDADDR
:
1360 case OSIOCGIFNETMASK
:
1361 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1365 #endif /* COMPAT_43_SOCKET */
1367 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1368 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1369 ifp
, cmd
, (caddr_t
) data
);
1377 ifioctllocked(so
, cmd
, data
, p
)
1385 socket_unlock(so
, 0);
1386 error
= ifioctl(so
, cmd
, data
, p
);
1392 * Set/clear promiscuous mode on interface ifp based on the truth value
1393 * of pswitch. The calls are reference counted so that only the first
1394 * "on" request actually has an effect, as does the final "off" request.
1395 * Results are undefined if the "off" and "on" requests are not matched.
1398 ifnet_set_promiscuous(
1408 ifnet_lock_exclusive(ifp
);
1410 oldflags
= ifp
->if_flags
;
1413 * If the device is not configured up, we cannot put it in
1416 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1420 if (ifp
->if_pcount
++ != 0) {
1423 ifp
->if_flags
|= IFF_PROMISC
;
1425 if (--ifp
->if_pcount
> 0)
1427 ifp
->if_flags
&= ~IFF_PROMISC
;
1429 ifr
.ifr_flags
= ifp
->if_flags
;
1431 ifnet_lock_done(ifp
);
1432 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
1436 ifp
->if_flags
= oldflags
;
1438 if (locked
) ifnet_lock_done(ifp
);
1440 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1441 ifp
->if_name
, ifp
->if_unit
,
1442 pswitch
!= 0 ? "enabled" : "disabled");
1448 * Return interface configuration
1449 * of system. List may be used
1450 * in later ioctl's (above) to get
1451 * other information.
1455 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1457 struct ifnet
*ifp
= NULL
;
1464 * Zero the ifr buffer to make sure we don't
1465 * disclose the contents of the stack.
1467 bzero(&ifr
, sizeof(struct ifreq
));
1470 ifnet_head_lock_shared();
1471 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1473 size_t ifnlen
, addrs
;
1475 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1476 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1477 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1478 error
= ENAMETOOLONG
;
1481 strcpy(ifr
.ifr_name
, workbuf
);
1484 ifnet_lock_shared(ifp
);
1487 ifa
= ifp
->if_addrhead
.tqh_first
;
1488 for ( ; space
> sizeof (ifr
) && ifa
;
1489 ifa
= ifa
->ifa_link
.tqe_next
) {
1490 struct sockaddr
*sa
= ifa
->ifa_addr
;
1492 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1496 #if COMPAT_43_SOCKET
1497 if (cmd
== OSIOCGIFCONF
) {
1498 struct osockaddr
*osa
=
1499 (struct osockaddr
*)&ifr
.ifr_addr
;
1501 osa
->sa_family
= sa
->sa_family
;
1502 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1503 ifrp
+= sizeof(struct ifreq
);
1506 if (sa
->sa_len
<= sizeof(*sa
)) {
1508 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1509 ifrp
+= sizeof(struct ifreq
);
1511 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1513 space
-= sa
->sa_len
- sizeof(*sa
);
1514 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1516 error
= copyout((caddr_t
)sa
,
1517 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1520 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1524 space
-= sizeof (ifr
);
1526 ifnet_lock_done(ifp
);
1531 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1532 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1535 space
-= sizeof (ifr
);
1536 ifrp
+= sizeof(struct ifreq
);
1540 *ret_space
-= space
;
1545 * Just like if_promisc(), but for all-multicast-reception mode.
1548 if_allmulti(ifp
, onswitch
)
1555 ifnet_lock_exclusive(ifp
);
1558 if (ifp
->if_amcount
++ == 0) {
1559 ifp
->if_flags
|= IFF_ALLMULTI
;
1563 if (ifp
->if_amcount
> 1) {
1566 ifp
->if_amcount
= 0;
1567 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1571 ifnet_lock_done(ifp
);
1574 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1583 struct ifmultiaddr
*ifma
)
1585 if (OSIncrementAtomic((SInt32
*)&ifma
->ifma_refcount
) <= 0)
1586 panic("ifma_reference: ifma already released or invalid\n");
1591 struct ifmultiaddr
*ifma
)
1594 struct ifmultiaddr
*next
;
1595 int32_t prevValue
= OSDecrementAtomic((SInt32
*)&ifma
->ifma_refcount
);
1597 panic("ifma_release: ifma already released or invalid\n");
1601 /* Allow the allocator of the protospec to free it */
1602 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1603 ifma
->ifma_free(ifma
->ifma_protospec
);
1606 next
= ifma
->ifma_ll
;
1607 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1608 FREE(ifma
, M_IFMADDR
);
1614 * Find an ifmultiaddr that matches a socket address on an interface.
1616 * Caller is responsible for holding the ifnet_lock while calling
1620 if_addmulti_doesexist(
1622 const struct sockaddr
*sa
,
1623 struct ifmultiaddr
**retifma
)
1625 struct ifmultiaddr
*ifma
;
1626 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1627 ifma
= ifma
->ifma_link
.le_next
) {
1628 if (equal(sa
, ifma
->ifma_addr
)) {
1629 ifma
->ifma_usecount
++;
1632 ifma_reference(*retifma
);
1642 * Add a multicast listenership to the interface in question.
1643 * The link layer provides a routine which converts
1647 struct ifnet
*ifp
, /* interface to manipulate */
1648 const struct sockaddr
*sa
, /* address to add */
1649 struct ifmultiaddr
**retifma
)
1651 struct sockaddr_storage storage
;
1652 struct sockaddr
*llsa
= NULL
;
1653 struct sockaddr
*dupsa
;
1655 struct ifmultiaddr
*ifma
;
1656 struct ifmultiaddr
*llifma
= NULL
;
1658 ifnet_lock_exclusive(ifp
);
1659 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1660 ifnet_lock_done(ifp
);
1666 * Give the link layer a chance to accept/reject it, and also
1667 * find out which AF_LINK address this maps to, if it isn't one
1670 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
, sizeof(storage
));
1671 if (error
== 0 && storage
.ss_len
!= 0) {
1672 MALLOC(llsa
, struct sockaddr
*, storage
.ss_len
, M_IFMADDR
, M_WAITOK
);
1673 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1674 bcopy(&storage
, llsa
, storage
.ss_len
);
1677 /* to be similar to FreeBSD */
1678 if (error
== EOPNOTSUPP
)
1685 /* Allocate while we aren't holding any locks */
1686 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1687 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1688 bcopy(sa
, dupsa
, sa
->sa_len
);
1690 ifnet_lock_exclusive(ifp
);
1692 * Check again for the matching multicast.
1694 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1695 ifnet_lock_done(ifp
);
1696 FREE(ifma
, M_IFMADDR
);
1697 FREE(dupsa
, M_IFMADDR
);
1699 FREE(llsa
, M_IFMADDR
);
1703 bzero(ifma
, sizeof(*ifma
));
1704 ifma
->ifma_addr
= dupsa
;
1705 ifma
->ifma_ifp
= ifp
;
1706 ifma
->ifma_usecount
= 1;
1707 ifma
->ifma_refcount
= 1;
1710 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1711 FREE(llsa
, M_IFMADDR
);
1712 FREE(llifma
, M_IFMADDR
);
1714 bzero(llifma
, sizeof(*llifma
));
1715 llifma
->ifma_addr
= llsa
;
1716 llifma
->ifma_ifp
= ifp
;
1717 llifma
->ifma_usecount
= 1;
1718 llifma
->ifma_refcount
= 1;
1719 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1721 ifma
->ifma_ll
= llifma
;
1722 ifma_reference(ifma
->ifma_ll
);
1726 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1730 ifma_reference(*retifma
);
1733 ifnet_lock_done(ifp
);
1736 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1739 * We are certain we have added something, so call down to the
1740 * interface to let them know about it.
1742 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1749 struct ifmultiaddr
*ifma
,
1753 int do_del_multi
= 0;
1755 ifp
= ifma
->ifma_ifp
;
1757 if (!locked
&& ifp
) {
1758 ifnet_lock_exclusive(ifp
);
1761 while (ifma
!= NULL
) {
1762 struct ifmultiaddr
*ll_ifma
;
1764 if (ifma
->ifma_usecount
> 1) {
1765 ifma
->ifma_usecount
--;
1770 LIST_REMOVE(ifma
, ifma_link
);
1772 ll_ifma
= ifma
->ifma_ll
;
1774 if (ll_ifma
) { /* send a routing msg for network addresses only */
1776 ifnet_lock_done(ifp
);
1777 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1779 ifnet_lock_exclusive(ifp
);
1783 * Make sure the interface driver is notified
1784 * in the case of a link layer mcast group being left.
1787 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
1798 if (!locked
&& ifp
) {
1799 /* This wasn't initially locked, we should unlock it */
1800 ifnet_lock_done(ifp
);
1805 ifnet_lock_done(ifp
);
1806 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1808 ifnet_lock_exclusive(ifp
);
1815 * Remove a reference to a multicast address on this interface. Yell
1816 * if the request does not match an existing membership.
1821 const struct sockaddr
*sa
)
1823 struct ifmultiaddr
*ifma
;
1826 ifnet_lock_exclusive(ifp
);
1827 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1828 ifma
= ifma
->ifma_link
.le_next
)
1829 if (equal(sa
, ifma
->ifma_addr
))
1832 ifnet_lock_done(ifp
);
1836 retval
= if_delmultiaddr(ifma
, 1);
1837 ifnet_lock_done(ifp
);
1844 * We don't use if_setlladdr, our interfaces are responsible for
1845 * handling the SIOCSIFLLADDR ioctl.
1849 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1855 struct ifmultiaddr
*
1856 ifmaof_ifpforaddr(sa
, ifp
)
1857 const struct sockaddr
*sa
;
1860 struct ifmultiaddr
*ifma
;
1862 ifnet_lock_shared(ifp
);
1863 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1864 ifma
= ifma
->ifma_link
.le_next
)
1865 if (equal(ifma
->ifma_addr
, sa
))
1867 ifnet_lock_done(ifp
);
1872 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1873 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1877 * Shutdown all network activity. Used boot() when halting
1880 int if_down_all(void);
1881 int if_down_all(void)
1887 if (ifnet_list_get(IFNET_FAMILY_ANY
, &ifp
, &count
) != 0) {
1888 for (i
= 0; i
< count
; i
++) {
1891 ifnet_list_free(ifp
);
1898 * Delete Routes for a Network Interface
1900 * Called for each routing entry via the rnh->rnh_walktree() call above
1901 * to delete all route entries referencing a detaching network interface.
1904 * rn pointer to node in the routing table
1905 * arg argument passed to rnh->rnh_walktree() - detaching interface
1909 * errno failed - reason indicated
1914 struct radix_node
*rn
,
1917 struct rtentry
*rt
= (struct rtentry
*)rn
;
1918 struct ifnet
*ifp
= arg
;
1921 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1924 * Protect (sorta) against walktree recursion problems
1925 * with cloned routes
1927 if ((rt
->rt_flags
& RTF_UP
) == 0)
1930 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1931 rt_mask(rt
), rt
->rt_flags
,
1932 (struct rtentry
**) NULL
);
1934 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1942 * Removes routing table reference to a given interfacei
1943 * for a given protocol family
1945 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1947 struct radix_node_head
*rnh
;
1949 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
1950 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
1951 lck_mtx_lock(rt_mtx
);
1952 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
1953 lck_mtx_unlock(rt_mtx
);
1957 extern lck_spin_t
*dlil_input_lock
;
1959 __private_extern__
void
1960 if_data_internal_to_if_data(
1961 const struct if_data_internal
*if_data_int
,
1962 struct if_data
*if_data
)
1964 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
1965 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
1966 COPYFIELD(ifi_type
);
1967 COPYFIELD(ifi_typelen
);
1968 COPYFIELD(ifi_physical
);
1969 COPYFIELD(ifi_addrlen
);
1970 COPYFIELD(ifi_hdrlen
);
1971 COPYFIELD(ifi_recvquota
);
1972 COPYFIELD(ifi_xmitquota
);
1973 if_data
->ifi_unused1
= 0;
1975 COPYFIELD(ifi_metric
);
1976 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
1977 if_data
->ifi_baudrate
= 0xFFFFFFFF;
1980 COPYFIELD32(ifi_baudrate
);
1983 lck_spin_lock(dlil_input_lock
);
1984 COPYFIELD32(ifi_ipackets
);
1985 COPYFIELD32(ifi_ierrors
);
1986 COPYFIELD32(ifi_opackets
);
1987 COPYFIELD32(ifi_oerrors
);
1988 COPYFIELD32(ifi_collisions
);
1989 COPYFIELD32(ifi_ibytes
);
1990 COPYFIELD32(ifi_obytes
);
1991 COPYFIELD32(ifi_imcasts
);
1992 COPYFIELD32(ifi_omcasts
);
1993 COPYFIELD32(ifi_iqdrops
);
1994 COPYFIELD32(ifi_noproto
);
1995 COPYFIELD32(ifi_recvtiming
);
1996 COPYFIELD32(ifi_xmittiming
);
1997 COPYFIELD(ifi_lastchange
);
1998 lck_spin_unlock(dlil_input_lock
);
2000 #if IF_LASTCHANGEUPTIME
2001 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2004 if_data
->ifi_unused2
= 0;
2005 COPYFIELD(ifi_hwassist
);
2006 if_data
->ifi_reserved1
= 0;
2007 if_data
->ifi_reserved2
= 0;
2012 __private_extern__
void
2013 if_data_internal_to_if_data64(
2014 const struct if_data_internal
*if_data_int
,
2015 struct if_data64
*if_data64
)
2017 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2018 COPYFIELD(ifi_type
);
2019 COPYFIELD(ifi_typelen
);
2020 COPYFIELD(ifi_physical
);
2021 COPYFIELD(ifi_addrlen
);
2022 COPYFIELD(ifi_hdrlen
);
2023 COPYFIELD(ifi_recvquota
);
2024 COPYFIELD(ifi_xmitquota
);
2025 if_data64
->ifi_unused1
= 0;
2027 COPYFIELD(ifi_metric
);
2028 COPYFIELD(ifi_baudrate
);
2030 lck_spin_lock(dlil_input_lock
);
2031 COPYFIELD(ifi_ipackets
);
2032 COPYFIELD(ifi_ierrors
);
2033 COPYFIELD(ifi_opackets
);
2034 COPYFIELD(ifi_oerrors
);
2035 COPYFIELD(ifi_collisions
);
2036 COPYFIELD(ifi_ibytes
);
2037 COPYFIELD(ifi_obytes
);
2038 COPYFIELD(ifi_imcasts
);
2039 COPYFIELD(ifi_omcasts
);
2040 COPYFIELD(ifi_iqdrops
);
2041 COPYFIELD(ifi_noproto
);
2042 COPYFIELD(ifi_recvtiming
);
2043 COPYFIELD(ifi_xmittiming
);
2044 COPYFIELD(ifi_lastchange
);
2045 lck_spin_unlock(dlil_input_lock
);
2047 #if IF_LASTCHANGEUPTIME
2048 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();