2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1980, 1986, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)if.c 8.3 (Berkeley) 1/4/94
63 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
66 #include <kern/locks.h>
68 #include <sys/param.h>
69 #include <sys/malloc.h>
71 #include <sys/systm.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/protosw.h>
76 #include <sys/kernel.h>
77 #include <sys/sockio.h>
78 #include <sys/syslog.h>
79 #include <sys/sysctl.h>
82 #include <net/if_arp.h>
83 #include <net/if_dl.h>
84 #include <net/if_types.h>
85 #include <net/if_var.h>
86 #include <net/net_osdep.h>
88 #include <net/radix.h>
89 #include <net/route.h>
93 #include <sys/domain.h>
94 #include <libkern/OSAtomic.h>
97 #if defined(INET) || defined(INET6)
99 #include <netinet/in.h>
100 #include <netinet/in_var.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet6/in6_ifattach.h>
108 * System initialization
111 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
112 static void if_qflush(struct ifqueue
*);
113 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
114 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
116 static struct if_clone
*if_clone_lookup(const char *, int *);
118 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
121 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
122 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
124 int ifqmaxlen
= IFQ_MAXLEN
;
125 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
127 static int if_cloners_count
;
128 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
132 * XXX: declare here to avoid to include many inet6 related files..
133 * should be more generalized?
135 extern void nd6_setmtu(struct ifnet
*);
138 #define M_CLONE M_IFADDR
141 * Network interface utility routines.
143 * Routines with ifa_ifwith* names take sockaddr *'s as
148 struct ifaddr
**ifnet_addrs
;
149 struct ifnet
**ifindex2ifnet
;
151 __private_extern__
void
156 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
157 if (ifa
->ifa_debug
& IFA_ATTACHED
) {
158 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
161 ifa
->ifa_debug
|= IFA_ATTACHED
;
162 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
165 __private_extern__
void
170 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
173 if ((ifa
->ifa_debug
& IFA_ATTACHED
) == 0) {
174 printf("if_detach_ifa: ifa is not attached to any interface! flags=%\n", ifa
->ifa_debug
);
179 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
184 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
188 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
189 ifa
->ifa_debug
&= ~IFA_ATTACHED
;
193 #define INITIAL_IF_INDEXLIM 8
196 * Function: if_next_index
198 * Return the next available interface index.
199 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
200 * added entry when necessary.
203 * ifnet_addrs[] is indexed by (if_index - 1), whereas
204 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
205 * always allocate one extra element to hold ifindex2ifnet[0], which
208 int if_next_index(void);
210 __private_extern__
int
213 static int if_indexlim
= 0;
216 new_index
= ++if_index
;
217 if (if_index
> if_indexlim
) {
220 caddr_t new_ifnet_addrs
;
221 caddr_t new_ifindex2ifnet
;
222 caddr_t old_ifnet_addrs
;
224 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
225 if (ifnet_addrs
== NULL
) {
226 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
228 new_if_indexlim
= if_indexlim
<< 1;
231 /* allocate space for the larger arrays */
232 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
233 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
234 new_ifindex2ifnet
= new_ifnet_addrs
235 + new_if_indexlim
* sizeof(caddr_t
);
236 bzero(new_ifnet_addrs
, n
);
237 if (ifnet_addrs
!= NULL
) {
238 /* copy the existing data */
239 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
240 if_indexlim
* sizeof(caddr_t
));
241 bcopy((caddr_t
)ifindex2ifnet
,
243 (if_indexlim
+ 1) * sizeof(caddr_t
));
246 /* switch to the new tables and size */
247 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
248 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
249 if_indexlim
= new_if_indexlim
;
251 /* release the old data */
252 if (old_ifnet_addrs
!= NULL
) {
253 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
260 * Create a clone network interface.
263 if_clone_create(char *name
, int len
)
265 struct if_clone
*ifc
;
267 int wildcard
, bytoff
, bitoff
;
271 ifc
= if_clone_lookup(name
, &unit
);
275 if (ifunit(name
) != NULL
)
279 wildcard
= (unit
< 0);
281 * Find a free unit if none was given.
284 while ((bytoff
< ifc
->ifc_bmlen
)
285 && (ifc
->ifc_units
[bytoff
] == 0xff))
287 if (bytoff
>= ifc
->ifc_bmlen
)
289 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
291 unit
= (bytoff
<< 3) + bitoff
;
294 if (unit
> ifc
->ifc_maxunit
)
297 err
= (*ifc
->ifc_create
)(ifc
, unit
);
303 bitoff
= unit
- (bytoff
<< 3);
307 * Allocate the unit in the bitmap.
309 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
310 ("%s: bit is already set", __func__
));
311 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
313 /* In the wildcard case, we need to update the name. */
315 for (dp
= name
; *dp
!= '\0'; dp
++);
316 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
317 len
- (dp
-name
) - 1) {
319 * This can only be a programmer error and
320 * there's no straightforward way to recover if
323 panic("if_clone_create(): interface name too long");
332 * Destroy a clone network interface.
335 if_clone_destroy(const char *name
)
337 struct if_clone
*ifc
;
342 ifc
= if_clone_lookup(name
, &unit
);
346 if (unit
< ifc
->ifc_minifs
)
353 if (ifc
->ifc_destroy
== NULL
)
356 (*ifc
->ifc_destroy
)(ifp
);
359 * Compute offset in the bitmap and deallocate the unit.
362 bitoff
= unit
- (bytoff
<< 3);
363 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
364 ("%s: bit is already cleared", __func__
));
365 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
370 * Look up a network interface cloner.
373 static struct if_clone
*
374 if_clone_lookup(const char *name
, int *unitp
)
376 struct if_clone
*ifc
;
380 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
381 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
382 if (ifc
->ifc_name
[i
] != *cp
)
387 ifc
= LIST_NEXT(ifc
, ifc_list
);
391 return ((struct if_clone
*)NULL
);
397 for (i
= 0; *cp
!= '\0'; cp
++) {
398 if (*cp
< '0' || *cp
> '9') {
399 /* Bogus unit number. */
402 i
= (i
* 10) + (*cp
- '0');
412 * Register a network interface cloner.
415 if_clone_attach(struct if_clone
*ifc
)
422 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
423 ("%s: %s requested more units then allowed (%d > %d)",
424 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
425 ifc
->ifc_maxunit
+ 1));
427 * Compute bitmap size and allocate it.
429 maxclone
= ifc
->ifc_maxunit
+ 1;
431 if ((len
<< 3) < maxclone
)
433 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
434 bzero(ifc
->ifc_units
, len
);
435 ifc
->ifc_bmlen
= len
;
437 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
440 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
441 err
= (*ifc
->ifc_create
)(ifc
, unit
);
443 ("%s: failed to create required interface %s%d",
444 __func__
, ifc
->ifc_name
, unit
));
446 /* Allocate the unit in the bitmap. */
448 bitoff
= unit
- (bytoff
<< 3);
449 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
454 * Unregister a network interface cloner.
457 if_clone_detach(struct if_clone
*ifc
)
460 LIST_REMOVE(ifc
, ifc_list
);
461 FREE(ifc
->ifc_units
, M_CLONE
);
467 * Provide list of interface cloners to userspace.
470 if_clone_list(int count
, int * total
, user_addr_t dst
)
472 char outbuf
[IFNAMSIZ
];
473 struct if_clone
*ifc
;
476 *total
= if_cloners_count
;
477 if (dst
== USER_ADDR_NULL
) {
478 /* Just asking how many there are. */
485 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
487 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
488 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
489 strncpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
- 1);
490 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
499 int ifa_foraddr(unsigned int addr
);
500 __private_extern__
int
509 ifnet_head_lock_shared();
510 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
511 ifnet_lock_shared(ifp
);
512 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
513 ifa
= ifa
->ifa_link
.tqe_next
) {
514 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
516 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
523 ifnet_lock_done(ifp
);
531 * Locate an interface based on a complete address.
536 const struct sockaddr
*addr
)
540 struct ifaddr
*result
= 0;
542 #define equal(a1, a2) \
543 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
545 ifnet_head_lock_shared();
546 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
547 ifnet_lock_shared(ifp
);
548 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
549 ifa
= ifa
->ifa_link
.tqe_next
) {
550 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
552 if (equal(addr
, ifa
->ifa_addr
)) {
556 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
557 /* IP6 doesn't have broadcast */
558 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
559 equal(ifa
->ifa_broadaddr
, addr
)) {
566 ifnet_lock_done(ifp
);
573 * Locate the point to point interface with a given destination address.
578 const struct sockaddr
*addr
)
582 struct ifaddr
*result
= 0;
584 ifnet_head_lock_shared();
585 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
586 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
587 ifnet_lock_shared(ifp
);
588 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
589 ifa
= ifa
->ifa_link
.tqe_next
) {
590 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
592 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
599 ifnet_lock_done(ifp
);
607 * Find an interface on a specific network. If many, choice
608 * is most specific found.
612 const struct sockaddr
*addr
)
615 struct ifaddr
*ifa
= NULL
;
616 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
617 u_int af
= addr
->sa_family
;
618 char *addr_data
= addr
->sa_data
, *cplim
;
620 ifnet_head_lock_shared();
622 * AF_LINK addresses can be looked up directly by their index number,
623 * so do that if we can.
626 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
627 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
628 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
639 * Scan though each interface, looking for ones that have
640 * addresses in this address family.
642 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
643 ifnet_lock_shared(ifp
);
644 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
645 ifa
= ifa
->ifa_link
.tqe_next
) {
646 char *cp
, *cp2
, *cp3
;
648 if (ifa
->ifa_addr
->sa_family
!= af
)
651 /* This breaks tunneling application trying to install a route with
652 * a specific subnet and the local address as the destination
653 * It's breaks binary compatibility with previous version of MacOS X
657 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
658 addr
->sa_family
!= AF_INET6
&&
660 ifp
->if_flags
& IFF_POINTOPOINT
) {
662 * This is a bit broken as it doesn't
663 * take into account that the remote end may
664 * be a single node in the network we are
666 * The trouble is that we don't know the
667 * netmask for the remote end.
669 if (ifa
->ifa_dstaddr
!= 0
670 && equal(addr
, ifa
->ifa_dstaddr
)) {
674 #endif /* __APPLE__*/
677 * if we have a special address handler,
678 * then use it instead of the generic one.
680 if (ifa
->ifa_claim_addr
) {
681 if (ifa
->ifa_claim_addr(ifa
, addr
)) {
689 * Scan all the bits in the ifa's address.
690 * If a bit dissagrees with what we are
691 * looking for, mask it with the netmask
692 * to see if it really matters.
695 if (ifa
->ifa_netmask
== 0)
698 cp2
= ifa
->ifa_addr
->sa_data
;
699 cp3
= ifa
->ifa_netmask
->sa_data
;
700 cplim
= ifa
->ifa_netmask
->sa_len
701 + (char *)ifa
->ifa_netmask
;
703 if ((*cp
++ ^ *cp2
++) & *cp3
++)
704 goto next
; /* next address! */
706 * If the netmask of what we just found
707 * is more specific than what we had before
708 * (if we had one) then remember the new one
709 * before continuing to search
710 * for an even better one.
712 if (ifa_maybe
== 0 ||
713 rn_refines((caddr_t
)ifa
->ifa_netmask
,
714 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
728 * ifa is set if we found an exact match.
729 * take a reference to the ifa before
730 * releasing the ifp lock
732 ifnet_lock_done(ifp
);
741 else if (ifa_maybe
) {
749 * Find an interface address specific to an interface best matching
754 const struct sockaddr
*addr
,
757 struct ifaddr
*ifa
= 0;
758 const char *cp
, *cp2
, *cp3
;
760 struct ifaddr
*ifa_maybe
= 0;
761 u_int af
= addr
->sa_family
;
766 ifnet_lock_shared(ifp
);
767 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
768 ifa
= ifa
->ifa_link
.tqe_next
) {
769 if (ifa
->ifa_addr
->sa_family
!= af
)
773 if (ifa
->ifa_netmask
== 0) {
774 if (equal(addr
, ifa
->ifa_addr
) ||
775 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
779 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
780 if (equal(addr
, ifa
->ifa_dstaddr
))
784 cp2
= ifa
->ifa_addr
->sa_data
;
785 cp3
= ifa
->ifa_netmask
->sa_data
;
786 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
787 for (; cp3
< cplim
; cp3
++)
788 if ((*cp
++ ^ *cp2
++) & *cp3
)
795 if (!ifa
) ifa
= ifa_maybe
;
796 if (ifa
) ifaref(ifa
);
798 ifnet_lock_done(ifp
);
802 #include <net/route.h>
805 * Default action when installing a route with a Link Level gateway.
806 * Lookup an appropriate real ifa to point to.
807 * This should be moved to /sys/net/link.c eventually.
810 link_rtrequest(cmd
, rt
, sa
)
816 struct sockaddr
*dst
;
819 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
820 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
822 ifa
= ifaof_ifpforaddr(dst
, ifp
);
825 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
826 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
832 * if_updown will set the interface up or down. It will
833 * prevent other up/down events from occurring until this
834 * up/down event has completed.
836 * Caller must lock ifnet. This function will drop the
837 * lock. This allows ifnet_set_flags to set the rest of
838 * the flags after we change the up/down state without
839 * dropping the interface lock between setting the
840 * up/down state and updating the rest of the flags.
842 __private_extern__
void
851 /* Wait until no one else is changing the up/down state */
852 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
854 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
855 ifnet_lock_done(ifp
);
856 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
857 ifnet_lock_exclusive(ifp
);
860 /* Verify that the interface isn't already in the right state */
861 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
862 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
866 /* Indicate that the up/down state is changing */
867 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
869 /* Mark interface up or down */
871 ifp
->if_flags
|= IFF_UP
;
874 ifp
->if_flags
&= ~IFF_UP
;
877 ifnet_touch_lastchange(ifp
);
879 /* Drop the lock to notify addresses and route */
880 ifnet_lock_done(ifp
);
881 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
882 for (i
= 0; ifa
[i
] != 0; i
++) {
883 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
885 ifnet_free_address_list(ifa
);
889 /* Aquire the lock to clear the changing flag and flush the send queue */
890 ifnet_lock_exclusive(ifp
);
892 if_qflush(&ifp
->if_snd
);
893 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
894 wakeup(&ifp
->if_eflags
);
900 * Mark an interface down and notify protocols of
907 ifnet_lock_exclusive(ifp
);
909 ifnet_lock_done(ifp
);
913 * Mark an interface up and notify protocols of
920 ifnet_lock_exclusive(ifp
);
922 ifnet_lock_done(ifp
);
926 * Flush an interface queue.
935 while ((m
= n
) != 0) {
945 * Map interface name to
946 * interface structure pointer.
949 ifunit(const char *name
)
951 char namebuf
[IFNAMSIZ
+ 1];
959 if (len
< 2 || len
> IFNAMSIZ
)
963 if (c
< '0' || c
> '9')
964 return NULL
; /* trailing garbage */
969 return NULL
; /* no interface name */
970 unit
+= (c
- '0') * m
;
972 return NULL
; /* number is unreasonable */
975 } while (c
>= '0' && c
<= '9');
977 bcopy(name
, namebuf
, len
);
980 * Now search all the interfaces for this name/number
982 ifnet_head_lock_shared();
983 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
984 if (strcmp(ifp
->if_name
, namebuf
))
986 if (unit
== ifp
->if_unit
)
995 * Map interface name in a sockaddr_dl to
996 * interface structure pointer.
1000 struct sockaddr
*sa
;
1002 char ifname
[IFNAMSIZ
+1];
1003 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1005 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1006 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1010 * ifunit wants a null-terminated name. It may not be null-terminated
1011 * in the sockaddr. We don't want to change the caller's sockaddr,
1012 * and there might not be room to put the trailing null anyway, so we
1013 * make a local copy that we know we can null terminate safely.
1016 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1017 ifname
[sdl
->sdl_nlen
] = '\0';
1018 return ifunit(ifname
);
1026 ifioctl(so
, cmd
, data
, p
)
1037 struct kev_msg ev_msg
;
1038 struct net_event_data ev_data
;
1045 struct ifconf64
* ifc
= (struct ifconf64
*)data
;
1046 user_addr_t user_addr
;
1048 user_addr
= proc_is64bit(p
)
1049 ? ifc
->ifc_req64
: CAST_USER_ADDR_T(ifc
->ifc_req
);
1050 return (ifconf(cmd
, user_addr
, &ifc
->ifc_len
));
1054 ifr
= (struct ifreq
*)data
;
1058 error
= proc_suser(p
);
1061 return ((cmd
== SIOCIFCREATE
) ?
1062 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1063 if_clone_destroy(ifr
->ifr_name
));
1065 case SIOCIFGCLONERS
:
1066 case SIOCIFGCLONERS64
:
1068 struct if_clonereq64
* ifcr
= (struct if_clonereq64
*)data
;
1069 user_addr
= proc_is64bit(p
)
1070 ? ifcr
->ifcr_ifcru
.ifcru_buffer64
1071 : CAST_USER_ADDR_T(ifcr
->ifcr_ifcru
.ifcru_buffer32
);
1072 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1075 #endif IF_CLONE_LIST
1078 ifp
= ifunit(ifr
->ifr_name
);
1084 ifnet_lock_shared(ifp
);
1085 ifr
->ifr_flags
= ifp
->if_flags
;
1086 ifnet_lock_done(ifp
);
1090 ifnet_lock_shared(ifp
);
1091 ifr
->ifr_metric
= ifp
->if_metric
;
1092 ifnet_lock_done(ifp
);
1096 ifnet_lock_shared(ifp
);
1097 ifr
->ifr_mtu
= ifp
->if_mtu
;
1098 ifnet_lock_done(ifp
);
1102 ifnet_lock_shared(ifp
);
1103 ifr
->ifr_phys
= ifp
->if_physical
;
1104 ifnet_lock_done(ifp
);
1108 error
= proc_suser(p
);
1112 ifnet_set_flags(ifp
, ifr
->ifr_flags
, ~IFF_CANTCHANGE
);
1114 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1115 ifp
, cmd
, (caddr_t
) data
);
1118 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1119 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1120 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1122 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1123 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1124 ev_data
.if_family
= ifp
->if_family
;
1125 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1126 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1127 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1128 ev_msg
.dv
[1].data_length
= 0;
1129 kev_post_msg(&ev_msg
);
1131 ifnet_touch_lastchange(ifp
);
1135 error
= proc_suser(p
);
1138 ifp
->if_metric
= ifr
->ifr_metric
;
1141 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1142 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1143 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1145 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1146 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1147 ev_data
.if_family
= ifp
->if_family
;
1148 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1149 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1150 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1152 ev_msg
.dv
[1].data_length
= 0;
1153 kev_post_msg(&ev_msg
);
1155 ifnet_touch_lastchange(ifp
);
1159 error
= proc_suser(p
);
1163 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1164 ifp
, cmd
, (caddr_t
) data
);
1167 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1168 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1169 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1171 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1172 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1173 ev_data
.if_family
= ifp
->if_family
;
1174 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1175 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1176 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1177 ev_msg
.dv
[1].data_length
= 0;
1178 kev_post_msg(&ev_msg
);
1180 ifnet_touch_lastchange(ifp
);
1186 u_long oldmtu
= ifp
->if_mtu
;
1188 error
= proc_suser(p
);
1191 if (ifp
->if_ioctl
== NULL
)
1192 return (EOPNOTSUPP
);
1193 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1196 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1197 ifp
, cmd
, (caddr_t
) data
);
1200 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1201 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1202 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1204 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1205 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1206 ev_data
.if_family
= ifp
->if_family
;
1207 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1208 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1209 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1210 ev_msg
.dv
[1].data_length
= 0;
1211 kev_post_msg(&ev_msg
);
1213 ifnet_touch_lastchange(ifp
);
1217 * If the link MTU changed, do network layer specific procedure.
1219 if (ifp
->if_mtu
!= oldmtu
) {
1229 error
= proc_suser(p
);
1233 /* Don't allow group membership on non-multicast interfaces. */
1234 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1238 /* Don't let users screw up protocols' entries. */
1239 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1243 if (cmd
== SIOCADDMULTI
) {
1244 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1245 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1247 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1248 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1251 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1252 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1253 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1254 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1256 ev_data
.if_family
= ifp
->if_family
;
1257 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1258 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1259 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1260 ev_msg
.dv
[1].data_length
= 0;
1261 kev_post_msg(&ev_msg
);
1263 ifnet_touch_lastchange(ifp
);
1267 case SIOCSIFPHYADDR
:
1268 case SIOCDIFPHYADDR
:
1270 case SIOCSIFPHYADDR_IN6
:
1272 case SIOCSLIFPHYADDR
:
1274 case SIOCSIFGENERIC
:
1279 error
= proc_suser(p
);
1283 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1284 ifp
, cmd
, (caddr_t
) data
);
1287 ifnet_touch_lastchange(ifp
);
1291 ifs
= (struct ifstat
*)data
;
1292 ifs
->ascii
[0] = '\0';
1294 case SIOCGIFPSRCADDR
:
1295 case SIOCGIFPDSTADDR
:
1296 case SIOCGLIFPHYADDR
:
1298 case SIOCGIFGENERIC
:
1300 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1301 ifp
, cmd
, (caddr_t
) data
);
1304 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1305 ifp
, cmd
, (caddr_t
) data
);
1308 oif_flags
= ifp
->if_flags
;
1309 if (so
->so_proto
== 0)
1310 return (EOPNOTSUPP
);
1311 #if !COMPAT_43_SOCKET
1313 error
=(*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
, p
));
1314 socket_unlock(so
, 1);
1322 case SIOCSIFDSTADDR
:
1324 case SIOCSIFBRDADDR
:
1325 case SIOCSIFNETMASK
:
1326 #if BYTE_ORDER != BIG_ENDIAN
1327 if (ifr
->ifr_addr
.sa_family
== 0 &&
1328 ifr
->ifr_addr
.sa_len
< 16) {
1329 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1330 ifr
->ifr_addr
.sa_len
= 16;
1333 if (ifr
->ifr_addr
.sa_len
== 0)
1334 ifr
->ifr_addr
.sa_len
= 16;
1342 case OSIOCGIFDSTADDR
:
1343 cmd
= SIOCGIFDSTADDR
;
1346 case OSIOCGIFBRDADDR
:
1347 cmd
= SIOCGIFBRDADDR
;
1350 case OSIOCGIFNETMASK
:
1351 cmd
= SIOCGIFNETMASK
;
1354 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1356 socket_unlock(so
, 1);
1360 case OSIOCGIFDSTADDR
:
1361 case OSIOCGIFBRDADDR
:
1362 case OSIOCGIFNETMASK
:
1363 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1367 #endif /* COMPAT_43_SOCKET */
1369 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1370 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1371 ifp
, cmd
, (caddr_t
) data
);
1379 ifioctllocked(so
, cmd
, data
, p
)
1387 socket_unlock(so
, 0);
1388 error
= ifioctl(so
, cmd
, data
, p
);
1394 * Set/clear promiscuous mode on interface ifp based on the truth value
1395 * of pswitch. The calls are reference counted so that only the first
1396 * "on" request actually has an effect, as does the final "off" request.
1397 * Results are undefined if the "off" and "on" requests are not matched.
1400 ifnet_set_promiscuous(
1410 ifnet_lock_exclusive(ifp
);
1412 oldflags
= ifp
->if_flags
;
1415 * If the device is not configured up, we cannot put it in
1418 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1422 if (ifp
->if_pcount
++ != 0) {
1425 ifp
->if_flags
|= IFF_PROMISC
;
1427 if (--ifp
->if_pcount
> 0)
1429 ifp
->if_flags
&= ~IFF_PROMISC
;
1431 ifr
.ifr_flags
= ifp
->if_flags
;
1433 ifnet_lock_done(ifp
);
1434 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
1438 ifp
->if_flags
= oldflags
;
1440 if (locked
) ifnet_lock_done(ifp
);
1442 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1443 ifp
->if_name
, ifp
->if_unit
,
1444 pswitch
!= 0 ? "enabled" : "disabled");
1450 * Return interface configuration
1451 * of system. List may be used
1452 * in later ioctl's (above) to get
1453 * other information.
1457 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1459 struct ifnet
*ifp
= NULL
;
1466 * Zero the ifr buffer to make sure we don't
1467 * disclose the contents of the stack.
1469 bzero(&ifr
, sizeof(struct ifreq
));
1472 ifnet_head_lock_shared();
1473 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1475 size_t ifnlen
, addrs
;
1477 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1478 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1479 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1480 error
= ENAMETOOLONG
;
1483 strcpy(ifr
.ifr_name
, workbuf
);
1486 ifnet_lock_shared(ifp
);
1489 ifa
= ifp
->if_addrhead
.tqh_first
;
1490 for ( ; space
> sizeof (ifr
) && ifa
;
1491 ifa
= ifa
->ifa_link
.tqe_next
) {
1492 struct sockaddr
*sa
= ifa
->ifa_addr
;
1494 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1498 #if COMPAT_43_SOCKET
1499 if (cmd
== OSIOCGIFCONF
) {
1500 struct osockaddr
*osa
=
1501 (struct osockaddr
*)&ifr
.ifr_addr
;
1503 osa
->sa_family
= sa
->sa_family
;
1504 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1505 ifrp
+= sizeof(struct ifreq
);
1508 if (sa
->sa_len
<= sizeof(*sa
)) {
1510 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1511 ifrp
+= sizeof(struct ifreq
);
1513 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1515 space
-= sa
->sa_len
- sizeof(*sa
);
1516 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1518 error
= copyout((caddr_t
)sa
,
1519 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1522 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1526 space
-= sizeof (ifr
);
1528 ifnet_lock_done(ifp
);
1533 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1534 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1537 space
-= sizeof (ifr
);
1538 ifrp
+= sizeof(struct ifreq
);
1542 *ret_space
-= space
;
1547 * Just like if_promisc(), but for all-multicast-reception mode.
1550 if_allmulti(ifp
, onswitch
)
1557 ifnet_lock_exclusive(ifp
);
1560 if (ifp
->if_amcount
++ == 0) {
1561 ifp
->if_flags
|= IFF_ALLMULTI
;
1565 if (ifp
->if_amcount
> 1) {
1568 ifp
->if_amcount
= 0;
1569 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1573 ifnet_lock_done(ifp
);
1576 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1585 struct ifmultiaddr
*ifma
)
1587 if (OSIncrementAtomic((SInt32
*)&ifma
->ifma_refcount
) <= 0)
1588 panic("ifma_reference: ifma already released or invalid\n");
1593 struct ifmultiaddr
*ifma
)
1596 struct ifmultiaddr
*next
;
1597 int32_t prevValue
= OSDecrementAtomic((SInt32
*)&ifma
->ifma_refcount
);
1599 panic("ifma_release: ifma already released or invalid\n");
1603 /* Allow the allocator of the protospec to free it */
1604 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1605 ifma
->ifma_free(ifma
->ifma_protospec
);
1608 next
= ifma
->ifma_ll
;
1609 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1610 FREE(ifma
, M_IFMADDR
);
1616 * Find an ifmultiaddr that matches a socket address on an interface.
1618 * Caller is responsible for holding the ifnet_lock while calling
1622 if_addmulti_doesexist(
1624 const struct sockaddr
*sa
,
1625 struct ifmultiaddr
**retifma
)
1627 struct ifmultiaddr
*ifma
;
1628 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1629 ifma
= ifma
->ifma_link
.le_next
) {
1630 if (equal(sa
, ifma
->ifma_addr
)) {
1631 ifma
->ifma_usecount
++;
1634 ifma_reference(*retifma
);
1644 * Add a multicast listenership to the interface in question.
1645 * The link layer provides a routine which converts
1649 struct ifnet
*ifp
, /* interface to manipulate */
1650 const struct sockaddr
*sa
, /* address to add */
1651 struct ifmultiaddr
**retifma
)
1653 struct sockaddr_storage storage
;
1654 struct sockaddr
*llsa
= NULL
;
1655 struct sockaddr
*dupsa
;
1657 struct ifmultiaddr
*ifma
;
1658 struct ifmultiaddr
*llifma
= NULL
;
1660 ifnet_lock_exclusive(ifp
);
1661 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1662 ifnet_lock_done(ifp
);
1668 * Give the link layer a chance to accept/reject it, and also
1669 * find out which AF_LINK address this maps to, if it isn't one
1672 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
, sizeof(storage
));
1673 if (error
== 0 && storage
.ss_len
!= 0) {
1674 MALLOC(llsa
, struct sockaddr
*, storage
.ss_len
, M_IFMADDR
, M_WAITOK
);
1675 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1676 bcopy(&storage
, llsa
, storage
.ss_len
);
1679 /* to be similar to FreeBSD */
1680 if (error
== EOPNOTSUPP
)
1687 /* Allocate while we aren't holding any locks */
1688 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1689 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1690 bcopy(sa
, dupsa
, sa
->sa_len
);
1692 ifnet_lock_exclusive(ifp
);
1694 * Check again for the matching multicast.
1696 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1697 ifnet_lock_done(ifp
);
1698 FREE(ifma
, M_IFMADDR
);
1699 FREE(dupsa
, M_IFMADDR
);
1701 FREE(llsa
, M_IFMADDR
);
1705 bzero(ifma
, sizeof(*ifma
));
1706 ifma
->ifma_addr
= dupsa
;
1707 ifma
->ifma_ifp
= ifp
;
1708 ifma
->ifma_usecount
= 1;
1709 ifma
->ifma_refcount
= 1;
1712 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1713 FREE(llsa
, M_IFMADDR
);
1714 FREE(llifma
, M_IFMADDR
);
1716 bzero(llifma
, sizeof(*llifma
));
1717 llifma
->ifma_addr
= llsa
;
1718 llifma
->ifma_ifp
= ifp
;
1719 llifma
->ifma_usecount
= 1;
1720 llifma
->ifma_refcount
= 1;
1721 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1723 ifma
->ifma_ll
= llifma
;
1724 ifma_reference(ifma
->ifma_ll
);
1728 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1732 ifma_reference(*retifma
);
1735 ifnet_lock_done(ifp
);
1738 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1741 * We are certain we have added something, so call down to the
1742 * interface to let them know about it.
1744 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1751 struct ifmultiaddr
*ifma
,
1755 int do_del_multi
= 0;
1757 ifp
= ifma
->ifma_ifp
;
1759 if (!locked
&& ifp
) {
1760 ifnet_lock_exclusive(ifp
);
1763 while (ifma
!= NULL
) {
1764 struct ifmultiaddr
*ll_ifma
;
1766 if (ifma
->ifma_usecount
> 1) {
1767 ifma
->ifma_usecount
--;
1772 LIST_REMOVE(ifma
, ifma_link
);
1774 ll_ifma
= ifma
->ifma_ll
;
1776 if (ll_ifma
) { /* send a routing msg for network addresses only */
1778 ifnet_lock_done(ifp
);
1779 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1781 ifnet_lock_exclusive(ifp
);
1785 * Make sure the interface driver is notified
1786 * in the case of a link layer mcast group being left.
1789 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
1800 if (!locked
&& ifp
) {
1801 /* This wasn't initially locked, we should unlock it */
1802 ifnet_lock_done(ifp
);
1807 ifnet_lock_done(ifp
);
1808 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1810 ifnet_lock_exclusive(ifp
);
1817 * Remove a reference to a multicast address on this interface. Yell
1818 * if the request does not match an existing membership.
1823 const struct sockaddr
*sa
)
1825 struct ifmultiaddr
*ifma
;
1828 ifnet_lock_exclusive(ifp
);
1829 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1830 ifma
= ifma
->ifma_link
.le_next
)
1831 if (equal(sa
, ifma
->ifma_addr
))
1834 ifnet_lock_done(ifp
);
1838 retval
= if_delmultiaddr(ifma
, 1);
1839 ifnet_lock_done(ifp
);
1846 * We don't use if_setlladdr, our interfaces are responsible for
1847 * handling the SIOCSIFLLADDR ioctl.
1851 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1857 struct ifmultiaddr
*
1858 ifmaof_ifpforaddr(sa
, ifp
)
1859 const struct sockaddr
*sa
;
1862 struct ifmultiaddr
*ifma
;
1864 ifnet_lock_shared(ifp
);
1865 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1866 ifma
= ifma
->ifma_link
.le_next
)
1867 if (equal(ifma
->ifma_addr
, sa
))
1869 ifnet_lock_done(ifp
);
1874 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1875 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1879 * Shutdown all network activity. Used boot() when halting
1882 int if_down_all(void);
1883 int if_down_all(void)
1889 if (ifnet_list_get(IFNET_FAMILY_ANY
, &ifp
, &count
) != 0) {
1890 for (i
= 0; i
< count
; i
++) {
1893 ifnet_list_free(ifp
);
1900 * Delete Routes for a Network Interface
1902 * Called for each routing entry via the rnh->rnh_walktree() call above
1903 * to delete all route entries referencing a detaching network interface.
1906 * rn pointer to node in the routing table
1907 * arg argument passed to rnh->rnh_walktree() - detaching interface
1911 * errno failed - reason indicated
1916 struct radix_node
*rn
,
1919 struct rtentry
*rt
= (struct rtentry
*)rn
;
1920 struct ifnet
*ifp
= arg
;
1923 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1926 * Protect (sorta) against walktree recursion problems
1927 * with cloned routes
1929 if ((rt
->rt_flags
& RTF_UP
) == 0)
1932 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1933 rt_mask(rt
), rt
->rt_flags
,
1934 (struct rtentry
**) NULL
);
1936 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1944 * Removes routing table reference to a given interfacei
1945 * for a given protocol family
1947 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1949 struct radix_node_head
*rnh
;
1951 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
1952 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
1953 lck_mtx_lock(rt_mtx
);
1954 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
1955 lck_mtx_unlock(rt_mtx
);
1959 extern lck_spin_t
*dlil_input_lock
;
1961 __private_extern__
void
1962 if_data_internal_to_if_data(
1963 const struct if_data_internal
*if_data_int
,
1964 struct if_data
*if_data
)
1966 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
1967 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
1968 COPYFIELD(ifi_type
);
1969 COPYFIELD(ifi_typelen
);
1970 COPYFIELD(ifi_physical
);
1971 COPYFIELD(ifi_addrlen
);
1972 COPYFIELD(ifi_hdrlen
);
1973 COPYFIELD(ifi_recvquota
);
1974 COPYFIELD(ifi_xmitquota
);
1975 if_data
->ifi_unused1
= 0;
1977 COPYFIELD(ifi_metric
);
1978 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
1979 if_data
->ifi_baudrate
= 0xFFFFFFFF;
1982 COPYFIELD32(ifi_baudrate
);
1985 lck_spin_lock(dlil_input_lock
);
1986 COPYFIELD32(ifi_ipackets
);
1987 COPYFIELD32(ifi_ierrors
);
1988 COPYFIELD32(ifi_opackets
);
1989 COPYFIELD32(ifi_oerrors
);
1990 COPYFIELD32(ifi_collisions
);
1991 COPYFIELD32(ifi_ibytes
);
1992 COPYFIELD32(ifi_obytes
);
1993 COPYFIELD32(ifi_imcasts
);
1994 COPYFIELD32(ifi_omcasts
);
1995 COPYFIELD32(ifi_iqdrops
);
1996 COPYFIELD32(ifi_noproto
);
1997 COPYFIELD32(ifi_recvtiming
);
1998 COPYFIELD32(ifi_xmittiming
);
1999 COPYFIELD(ifi_lastchange
);
2000 lck_spin_unlock(dlil_input_lock
);
2002 #if IF_LASTCHANGEUPTIME
2003 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2006 if_data
->ifi_unused2
= 0;
2007 COPYFIELD(ifi_hwassist
);
2008 if_data
->ifi_reserved1
= 0;
2009 if_data
->ifi_reserved2
= 0;
2014 __private_extern__
void
2015 if_data_internal_to_if_data64(
2016 const struct if_data_internal
*if_data_int
,
2017 struct if_data64
*if_data64
)
2019 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2020 COPYFIELD(ifi_type
);
2021 COPYFIELD(ifi_typelen
);
2022 COPYFIELD(ifi_physical
);
2023 COPYFIELD(ifi_addrlen
);
2024 COPYFIELD(ifi_hdrlen
);
2025 COPYFIELD(ifi_recvquota
);
2026 COPYFIELD(ifi_xmitquota
);
2027 if_data64
->ifi_unused1
= 0;
2029 COPYFIELD(ifi_metric
);
2030 COPYFIELD(ifi_baudrate
);
2032 lck_spin_lock(dlil_input_lock
);
2033 COPYFIELD(ifi_ipackets
);
2034 COPYFIELD(ifi_ierrors
);
2035 COPYFIELD(ifi_opackets
);
2036 COPYFIELD(ifi_oerrors
);
2037 COPYFIELD(ifi_collisions
);
2038 COPYFIELD(ifi_ibytes
);
2039 COPYFIELD(ifi_obytes
);
2040 COPYFIELD(ifi_imcasts
);
2041 COPYFIELD(ifi_omcasts
);
2042 COPYFIELD(ifi_iqdrops
);
2043 COPYFIELD(ifi_noproto
);
2044 COPYFIELD(ifi_recvtiming
);
2045 COPYFIELD(ifi_xmittiming
);
2046 COPYFIELD(ifi_lastchange
);
2047 lck_spin_unlock(dlil_input_lock
);
2049 #if IF_LASTCHANGEUPTIME
2050 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();