2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1980, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)if.c 8.3 (Berkeley) 1/4/94
56 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
59 #include <kern/locks.h>
61 #include <sys/param.h>
62 #include <sys/malloc.h>
64 #include <sys/systm.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/protosw.h>
69 #include <sys/kernel.h>
70 #include <sys/sockio.h>
71 #include <sys/syslog.h>
72 #include <sys/sysctl.h>
75 #include <net/if_arp.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/net_osdep.h>
81 #include <net/radix.h>
82 #include <net/route.h>
86 #include <sys/domain.h>
87 #include <libkern/OSAtomic.h>
90 #if defined(INET) || defined(INET6)
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/in6_ifattach.h>
101 * System initialization
104 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
105 static void if_qflush(struct ifqueue
*);
106 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
107 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
109 static struct if_clone
*if_clone_lookup(const char *, int *);
111 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
114 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
115 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
117 int ifqmaxlen
= IFQ_MAXLEN
;
118 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
120 static int if_cloners_count
;
121 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
125 * XXX: declare here to avoid to include many inet6 related files..
126 * should be more generalized?
128 extern void nd6_setmtu(struct ifnet
*);
131 #define M_CLONE M_IFADDR
134 * Network interface utility routines.
136 * Routines with ifa_ifwith* names take sockaddr *'s as
141 struct ifaddr
**ifnet_addrs
;
142 struct ifnet
**ifindex2ifnet
;
144 __private_extern__
void
149 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
150 if (ifa
->ifa_debug
& IFA_ATTACHED
) {
151 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
154 ifa
->ifa_debug
|= IFA_ATTACHED
;
155 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
158 __private_extern__
void
163 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
166 if ((ifa
->ifa_debug
& IFA_ATTACHED
) == 0) {
167 printf("if_detach_ifa: ifa is not attached to any interface! flags=%\n", ifa
->ifa_debug
);
172 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
177 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
181 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
182 ifa
->ifa_debug
&= ~IFA_ATTACHED
;
186 #define INITIAL_IF_INDEXLIM 8
189 * Function: if_next_index
191 * Return the next available interface index.
192 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
193 * added entry when necessary.
196 * ifnet_addrs[] is indexed by (if_index - 1), whereas
197 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
198 * always allocate one extra element to hold ifindex2ifnet[0], which
201 int if_next_index(void);
203 __private_extern__
int
206 static int if_indexlim
= 0;
209 new_index
= ++if_index
;
210 if (if_index
> if_indexlim
) {
213 caddr_t new_ifnet_addrs
;
214 caddr_t new_ifindex2ifnet
;
215 caddr_t old_ifnet_addrs
;
217 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
218 if (ifnet_addrs
== NULL
) {
219 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
221 new_if_indexlim
= if_indexlim
<< 1;
224 /* allocate space for the larger arrays */
225 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
226 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
227 new_ifindex2ifnet
= new_ifnet_addrs
228 + new_if_indexlim
* sizeof(caddr_t
);
229 bzero(new_ifnet_addrs
, n
);
230 if (ifnet_addrs
!= NULL
) {
231 /* copy the existing data */
232 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
233 if_indexlim
* sizeof(caddr_t
));
234 bcopy((caddr_t
)ifindex2ifnet
,
236 (if_indexlim
+ 1) * sizeof(caddr_t
));
239 /* switch to the new tables and size */
240 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
241 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
242 if_indexlim
= new_if_indexlim
;
244 /* release the old data */
245 if (old_ifnet_addrs
!= NULL
) {
246 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
253 * Create a clone network interface.
256 if_clone_create(char *name
, int len
)
258 struct if_clone
*ifc
;
260 int wildcard
, bytoff
, bitoff
;
264 ifc
= if_clone_lookup(name
, &unit
);
268 if (ifunit(name
) != NULL
)
272 wildcard
= (unit
< 0);
274 * Find a free unit if none was given.
277 while ((bytoff
< ifc
->ifc_bmlen
)
278 && (ifc
->ifc_units
[bytoff
] == 0xff))
280 if (bytoff
>= ifc
->ifc_bmlen
)
282 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
284 unit
= (bytoff
<< 3) + bitoff
;
287 if (unit
> ifc
->ifc_maxunit
)
290 err
= (*ifc
->ifc_create
)(ifc
, unit
);
296 bitoff
= unit
- (bytoff
<< 3);
300 * Allocate the unit in the bitmap.
302 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
303 ("%s: bit is already set", __func__
));
304 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
306 /* In the wildcard case, we need to update the name. */
308 for (dp
= name
; *dp
!= '\0'; dp
++);
309 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
310 len
- (dp
-name
) - 1) {
312 * This can only be a programmer error and
313 * there's no straightforward way to recover if
316 panic("if_clone_create(): interface name too long");
325 * Destroy a clone network interface.
328 if_clone_destroy(const char *name
)
330 struct if_clone
*ifc
;
335 ifc
= if_clone_lookup(name
, &unit
);
339 if (unit
< ifc
->ifc_minifs
)
346 if (ifc
->ifc_destroy
== NULL
)
349 (*ifc
->ifc_destroy
)(ifp
);
352 * Compute offset in the bitmap and deallocate the unit.
355 bitoff
= unit
- (bytoff
<< 3);
356 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
357 ("%s: bit is already cleared", __func__
));
358 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
363 * Look up a network interface cloner.
366 static struct if_clone
*
367 if_clone_lookup(const char *name
, int *unitp
)
369 struct if_clone
*ifc
;
373 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
374 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
375 if (ifc
->ifc_name
[i
] != *cp
)
380 ifc
= LIST_NEXT(ifc
, ifc_list
);
384 return ((struct if_clone
*)NULL
);
390 for (i
= 0; *cp
!= '\0'; cp
++) {
391 if (*cp
< '0' || *cp
> '9') {
392 /* Bogus unit number. */
395 i
= (i
* 10) + (*cp
- '0');
405 * Register a network interface cloner.
408 if_clone_attach(struct if_clone
*ifc
)
415 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
416 ("%s: %s requested more units then allowed (%d > %d)",
417 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
418 ifc
->ifc_maxunit
+ 1));
420 * Compute bitmap size and allocate it.
422 maxclone
= ifc
->ifc_maxunit
+ 1;
424 if ((len
<< 3) < maxclone
)
426 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
427 bzero(ifc
->ifc_units
, len
);
428 ifc
->ifc_bmlen
= len
;
430 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
433 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
434 err
= (*ifc
->ifc_create
)(ifc
, unit
);
436 ("%s: failed to create required interface %s%d",
437 __func__
, ifc
->ifc_name
, unit
));
439 /* Allocate the unit in the bitmap. */
441 bitoff
= unit
- (bytoff
<< 3);
442 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
447 * Unregister a network interface cloner.
450 if_clone_detach(struct if_clone
*ifc
)
453 LIST_REMOVE(ifc
, ifc_list
);
454 FREE(ifc
->ifc_units
, M_CLONE
);
460 * Provide list of interface cloners to userspace.
463 if_clone_list(int count
, int * total
, user_addr_t dst
)
465 char outbuf
[IFNAMSIZ
];
466 struct if_clone
*ifc
;
469 *total
= if_cloners_count
;
470 if (dst
== USER_ADDR_NULL
) {
471 /* Just asking how many there are. */
478 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
480 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
481 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
482 strncpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
- 1);
483 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
492 int ifa_foraddr(unsigned int addr
);
493 __private_extern__
int
502 ifnet_head_lock_shared();
503 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
504 ifnet_lock_shared(ifp
);
505 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
506 ifa
= ifa
->ifa_link
.tqe_next
) {
507 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
509 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
516 ifnet_lock_done(ifp
);
524 * Locate an interface based on a complete address.
529 const struct sockaddr
*addr
)
533 struct ifaddr
*result
= 0;
535 #define equal(a1, a2) \
536 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
538 ifnet_head_lock_shared();
539 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
540 ifnet_lock_shared(ifp
);
541 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
542 ifa
= ifa
->ifa_link
.tqe_next
) {
543 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
545 if (equal(addr
, ifa
->ifa_addr
)) {
549 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
550 /* IP6 doesn't have broadcast */
551 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
552 equal(ifa
->ifa_broadaddr
, addr
)) {
559 ifnet_lock_done(ifp
);
566 * Locate the point to point interface with a given destination address.
571 const struct sockaddr
*addr
)
575 struct ifaddr
*result
= 0;
577 ifnet_head_lock_shared();
578 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
579 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
580 ifnet_lock_shared(ifp
);
581 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
582 ifa
= ifa
->ifa_link
.tqe_next
) {
583 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
585 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
592 ifnet_lock_done(ifp
);
600 * Find an interface on a specific network. If many, choice
601 * is most specific found.
605 const struct sockaddr
*addr
)
608 struct ifaddr
*ifa
= NULL
;
609 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
610 u_int af
= addr
->sa_family
;
611 char *addr_data
= addr
->sa_data
, *cplim
;
613 ifnet_head_lock_shared();
615 * AF_LINK addresses can be looked up directly by their index number,
616 * so do that if we can.
619 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
620 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
621 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
632 * Scan though each interface, looking for ones that have
633 * addresses in this address family.
635 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
636 ifnet_lock_shared(ifp
);
637 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
638 ifa
= ifa
->ifa_link
.tqe_next
) {
639 char *cp
, *cp2
, *cp3
;
641 if (ifa
->ifa_addr
->sa_family
!= af
)
644 /* This breaks tunneling application trying to install a route with
645 * a specific subnet and the local address as the destination
646 * It's breaks binary compatibility with previous version of MacOS X
650 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
651 addr
->sa_family
!= AF_INET6
&&
653 ifp
->if_flags
& IFF_POINTOPOINT
) {
655 * This is a bit broken as it doesn't
656 * take into account that the remote end may
657 * be a single node in the network we are
659 * The trouble is that we don't know the
660 * netmask for the remote end.
662 if (ifa
->ifa_dstaddr
!= 0
663 && equal(addr
, ifa
->ifa_dstaddr
)) {
667 #endif /* __APPLE__*/
670 * if we have a special address handler,
671 * then use it instead of the generic one.
673 if (ifa
->ifa_claim_addr
) {
674 if (ifa
->ifa_claim_addr(ifa
, addr
)) {
682 * Scan all the bits in the ifa's address.
683 * If a bit dissagrees with what we are
684 * looking for, mask it with the netmask
685 * to see if it really matters.
688 if (ifa
->ifa_netmask
== 0)
691 cp2
= ifa
->ifa_addr
->sa_data
;
692 cp3
= ifa
->ifa_netmask
->sa_data
;
693 cplim
= ifa
->ifa_netmask
->sa_len
694 + (char *)ifa
->ifa_netmask
;
696 if ((*cp
++ ^ *cp2
++) & *cp3
++)
697 goto next
; /* next address! */
699 * If the netmask of what we just found
700 * is more specific than what we had before
701 * (if we had one) then remember the new one
702 * before continuing to search
703 * for an even better one.
705 if (ifa_maybe
== 0 ||
706 rn_refines((caddr_t
)ifa
->ifa_netmask
,
707 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
721 * ifa is set if we found an exact match.
722 * take a reference to the ifa before
723 * releasing the ifp lock
725 ifnet_lock_done(ifp
);
734 else if (ifa_maybe
) {
742 * Find an interface address specific to an interface best matching
747 const struct sockaddr
*addr
,
750 struct ifaddr
*ifa
= 0;
751 const char *cp
, *cp2
, *cp3
;
753 struct ifaddr
*ifa_maybe
= 0;
754 u_int af
= addr
->sa_family
;
759 ifnet_lock_shared(ifp
);
760 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
761 ifa
= ifa
->ifa_link
.tqe_next
) {
762 if (ifa
->ifa_addr
->sa_family
!= af
)
766 if (ifa
->ifa_netmask
== 0) {
767 if (equal(addr
, ifa
->ifa_addr
) ||
768 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
772 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
773 if (equal(addr
, ifa
->ifa_dstaddr
))
777 cp2
= ifa
->ifa_addr
->sa_data
;
778 cp3
= ifa
->ifa_netmask
->sa_data
;
779 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
780 for (; cp3
< cplim
; cp3
++)
781 if ((*cp
++ ^ *cp2
++) & *cp3
)
788 if (!ifa
) ifa
= ifa_maybe
;
789 if (ifa
) ifaref(ifa
);
791 ifnet_lock_done(ifp
);
795 #include <net/route.h>
798 * Default action when installing a route with a Link Level gateway.
799 * Lookup an appropriate real ifa to point to.
800 * This should be moved to /sys/net/link.c eventually.
803 link_rtrequest(cmd
, rt
, sa
)
809 struct sockaddr
*dst
;
812 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
813 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
815 ifa
= ifaof_ifpforaddr(dst
, ifp
);
818 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
819 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
825 * if_updown will set the interface up or down. It will
826 * prevent other up/down events from occurring until this
827 * up/down event has completed.
829 * Caller must lock ifnet. This function will drop the
830 * lock. This allows ifnet_set_flags to set the rest of
831 * the flags after we change the up/down state without
832 * dropping the interface lock between setting the
833 * up/down state and updating the rest of the flags.
835 __private_extern__
void
844 /* Wait until no one else is changing the up/down state */
845 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
847 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
848 ifnet_lock_done(ifp
);
849 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
850 ifnet_lock_exclusive(ifp
);
853 /* Verify that the interface isn't already in the right state */
854 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
855 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
859 /* Indicate that the up/down state is changing */
860 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
862 /* Mark interface up or down */
864 ifp
->if_flags
|= IFF_UP
;
867 ifp
->if_flags
&= ~IFF_UP
;
870 ifnet_touch_lastchange(ifp
);
872 /* Drop the lock to notify addresses and route */
873 ifnet_lock_done(ifp
);
874 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
875 for (i
= 0; ifa
[i
] != 0; i
++) {
876 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
878 ifnet_free_address_list(ifa
);
882 /* Aquire the lock to clear the changing flag and flush the send queue */
883 ifnet_lock_exclusive(ifp
);
885 if_qflush(&ifp
->if_snd
);
886 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
887 wakeup(&ifp
->if_eflags
);
893 * Mark an interface down and notify protocols of
900 ifnet_lock_exclusive(ifp
);
902 ifnet_lock_done(ifp
);
906 * Mark an interface up and notify protocols of
913 ifnet_lock_exclusive(ifp
);
915 ifnet_lock_done(ifp
);
919 * Flush an interface queue.
928 while ((m
= n
) != 0) {
938 * Map interface name to
939 * interface structure pointer.
942 ifunit(const char *name
)
944 char namebuf
[IFNAMSIZ
+ 1];
952 if (len
< 2 || len
> IFNAMSIZ
)
956 if (c
< '0' || c
> '9')
957 return NULL
; /* trailing garbage */
962 return NULL
; /* no interface name */
963 unit
+= (c
- '0') * m
;
965 return NULL
; /* number is unreasonable */
968 } while (c
>= '0' && c
<= '9');
970 bcopy(name
, namebuf
, len
);
973 * Now search all the interfaces for this name/number
975 ifnet_head_lock_shared();
976 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
977 if (strcmp(ifp
->if_name
, namebuf
))
979 if (unit
== ifp
->if_unit
)
988 * Map interface name in a sockaddr_dl to
989 * interface structure pointer.
995 char ifname
[IFNAMSIZ
+1];
996 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
998 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
999 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1003 * ifunit wants a null-terminated name. It may not be null-terminated
1004 * in the sockaddr. We don't want to change the caller's sockaddr,
1005 * and there might not be room to put the trailing null anyway, so we
1006 * make a local copy that we know we can null terminate safely.
1009 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1010 ifname
[sdl
->sdl_nlen
] = '\0';
1011 return ifunit(ifname
);
1019 ifioctl(so
, cmd
, data
, p
)
1030 struct kev_msg ev_msg
;
1031 struct net_event_data ev_data
;
1038 struct ifconf64
* ifc
= (struct ifconf64
*)data
;
1039 user_addr_t user_addr
;
1041 user_addr
= proc_is64bit(p
)
1042 ? ifc
->ifc_req64
: CAST_USER_ADDR_T(ifc
->ifc_req
);
1043 return (ifconf(cmd
, user_addr
, &ifc
->ifc_len
));
1047 ifr
= (struct ifreq
*)data
;
1051 error
= proc_suser(p
);
1054 return ((cmd
== SIOCIFCREATE
) ?
1055 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1056 if_clone_destroy(ifr
->ifr_name
));
1058 case SIOCIFGCLONERS
:
1059 case SIOCIFGCLONERS64
:
1061 struct if_clonereq64
* ifcr
= (struct if_clonereq64
*)data
;
1062 user_addr
= proc_is64bit(p
)
1063 ? ifcr
->ifcr_ifcru
.ifcru_buffer64
1064 : CAST_USER_ADDR_T(ifcr
->ifcr_ifcru
.ifcru_buffer32
);
1065 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1068 #endif IF_CLONE_LIST
1071 ifp
= ifunit(ifr
->ifr_name
);
1077 ifnet_lock_shared(ifp
);
1078 ifr
->ifr_flags
= ifp
->if_flags
;
1079 ifnet_lock_done(ifp
);
1083 ifnet_lock_shared(ifp
);
1084 ifr
->ifr_metric
= ifp
->if_metric
;
1085 ifnet_lock_done(ifp
);
1089 ifnet_lock_shared(ifp
);
1090 ifr
->ifr_mtu
= ifp
->if_mtu
;
1091 ifnet_lock_done(ifp
);
1095 ifnet_lock_shared(ifp
);
1096 ifr
->ifr_phys
= ifp
->if_physical
;
1097 ifnet_lock_done(ifp
);
1101 error
= proc_suser(p
);
1105 ifnet_set_flags(ifp
, ifr
->ifr_flags
, ~IFF_CANTCHANGE
);
1107 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1108 ifp
, cmd
, (caddr_t
) data
);
1111 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1112 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1113 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1115 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1116 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1117 ev_data
.if_family
= ifp
->if_family
;
1118 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1119 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1120 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1121 ev_msg
.dv
[1].data_length
= 0;
1122 kev_post_msg(&ev_msg
);
1124 ifnet_touch_lastchange(ifp
);
1128 error
= proc_suser(p
);
1131 ifp
->if_metric
= ifr
->ifr_metric
;
1134 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1135 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1136 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1138 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1139 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1140 ev_data
.if_family
= ifp
->if_family
;
1141 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1142 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1143 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1145 ev_msg
.dv
[1].data_length
= 0;
1146 kev_post_msg(&ev_msg
);
1148 ifnet_touch_lastchange(ifp
);
1152 error
= proc_suser(p
);
1156 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1157 ifp
, cmd
, (caddr_t
) data
);
1160 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1161 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1162 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1164 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1165 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1166 ev_data
.if_family
= ifp
->if_family
;
1167 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1168 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1169 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1170 ev_msg
.dv
[1].data_length
= 0;
1171 kev_post_msg(&ev_msg
);
1173 ifnet_touch_lastchange(ifp
);
1179 u_long oldmtu
= ifp
->if_mtu
;
1181 error
= proc_suser(p
);
1184 if (ifp
->if_ioctl
== NULL
)
1185 return (EOPNOTSUPP
);
1186 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1189 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1190 ifp
, cmd
, (caddr_t
) data
);
1193 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1194 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1195 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1197 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1198 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1199 ev_data
.if_family
= ifp
->if_family
;
1200 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1201 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1202 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1203 ev_msg
.dv
[1].data_length
= 0;
1204 kev_post_msg(&ev_msg
);
1206 ifnet_touch_lastchange(ifp
);
1210 * If the link MTU changed, do network layer specific procedure.
1212 if (ifp
->if_mtu
!= oldmtu
) {
1222 error
= proc_suser(p
);
1226 /* Don't allow group membership on non-multicast interfaces. */
1227 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1231 /* Don't let users screw up protocols' entries. */
1232 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1236 if (cmd
== SIOCADDMULTI
) {
1237 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1238 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1240 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1241 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1244 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1245 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1246 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1247 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1249 ev_data
.if_family
= ifp
->if_family
;
1250 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1251 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1252 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1253 ev_msg
.dv
[1].data_length
= 0;
1254 kev_post_msg(&ev_msg
);
1256 ifnet_touch_lastchange(ifp
);
1260 case SIOCSIFPHYADDR
:
1261 case SIOCDIFPHYADDR
:
1263 case SIOCSIFPHYADDR_IN6
:
1265 case SIOCSLIFPHYADDR
:
1267 case SIOCSIFGENERIC
:
1272 error
= proc_suser(p
);
1276 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1277 ifp
, cmd
, (caddr_t
) data
);
1280 ifnet_touch_lastchange(ifp
);
1284 ifs
= (struct ifstat
*)data
;
1285 ifs
->ascii
[0] = '\0';
1287 case SIOCGIFPSRCADDR
:
1288 case SIOCGIFPDSTADDR
:
1289 case SIOCGLIFPHYADDR
:
1291 case SIOCGIFGENERIC
:
1293 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1294 ifp
, cmd
, (caddr_t
) data
);
1297 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1298 ifp
, cmd
, (caddr_t
) data
);
1301 oif_flags
= ifp
->if_flags
;
1302 if (so
->so_proto
== 0)
1303 return (EOPNOTSUPP
);
1304 #if !COMPAT_43_SOCKET
1306 error
=(*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
, p
));
1307 socket_unlock(so
, 1);
1315 case SIOCSIFDSTADDR
:
1317 case SIOCSIFBRDADDR
:
1318 case SIOCSIFNETMASK
:
1319 #if BYTE_ORDER != BIG_ENDIAN
1320 if (ifr
->ifr_addr
.sa_family
== 0 &&
1321 ifr
->ifr_addr
.sa_len
< 16) {
1322 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1323 ifr
->ifr_addr
.sa_len
= 16;
1326 if (ifr
->ifr_addr
.sa_len
== 0)
1327 ifr
->ifr_addr
.sa_len
= 16;
1335 case OSIOCGIFDSTADDR
:
1336 cmd
= SIOCGIFDSTADDR
;
1339 case OSIOCGIFBRDADDR
:
1340 cmd
= SIOCGIFBRDADDR
;
1343 case OSIOCGIFNETMASK
:
1344 cmd
= SIOCGIFNETMASK
;
1347 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1349 socket_unlock(so
, 1);
1353 case OSIOCGIFDSTADDR
:
1354 case OSIOCGIFBRDADDR
:
1355 case OSIOCGIFNETMASK
:
1356 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1360 #endif /* COMPAT_43_SOCKET */
1362 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1363 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1364 ifp
, cmd
, (caddr_t
) data
);
1372 ifioctllocked(so
, cmd
, data
, p
)
1380 socket_unlock(so
, 0);
1381 error
= ifioctl(so
, cmd
, data
, p
);
1387 * Set/clear promiscuous mode on interface ifp based on the truth value
1388 * of pswitch. The calls are reference counted so that only the first
1389 * "on" request actually has an effect, as does the final "off" request.
1390 * Results are undefined if the "off" and "on" requests are not matched.
1393 ifnet_set_promiscuous(
1403 ifnet_lock_exclusive(ifp
);
1405 oldflags
= ifp
->if_flags
;
1408 * If the device is not configured up, we cannot put it in
1411 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1415 if (ifp
->if_pcount
++ != 0) {
1418 ifp
->if_flags
|= IFF_PROMISC
;
1420 if (--ifp
->if_pcount
> 0)
1422 ifp
->if_flags
&= ~IFF_PROMISC
;
1424 ifr
.ifr_flags
= ifp
->if_flags
;
1426 ifnet_lock_done(ifp
);
1427 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
1431 ifp
->if_flags
= oldflags
;
1433 if (locked
) ifnet_lock_done(ifp
);
1435 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1436 ifp
->if_name
, ifp
->if_unit
,
1437 pswitch
!= 0 ? "enabled" : "disabled");
1443 * Return interface configuration
1444 * of system. List may be used
1445 * in later ioctl's (above) to get
1446 * other information.
1450 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1452 struct ifnet
*ifp
= NULL
;
1459 * Zero the ifr buffer to make sure we don't
1460 * disclose the contents of the stack.
1462 bzero(&ifr
, sizeof(struct ifreq
));
1465 ifnet_head_lock_shared();
1466 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1468 size_t ifnlen
, addrs
;
1470 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1471 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1472 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1473 error
= ENAMETOOLONG
;
1476 strcpy(ifr
.ifr_name
, workbuf
);
1479 ifnet_lock_shared(ifp
);
1482 ifa
= ifp
->if_addrhead
.tqh_first
;
1483 for ( ; space
> sizeof (ifr
) && ifa
;
1484 ifa
= ifa
->ifa_link
.tqe_next
) {
1485 struct sockaddr
*sa
= ifa
->ifa_addr
;
1487 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1491 #if COMPAT_43_SOCKET
1492 if (cmd
== OSIOCGIFCONF
) {
1493 struct osockaddr
*osa
=
1494 (struct osockaddr
*)&ifr
.ifr_addr
;
1496 osa
->sa_family
= sa
->sa_family
;
1497 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1498 ifrp
+= sizeof(struct ifreq
);
1501 if (sa
->sa_len
<= sizeof(*sa
)) {
1503 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1504 ifrp
+= sizeof(struct ifreq
);
1506 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1508 space
-= sa
->sa_len
- sizeof(*sa
);
1509 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1511 error
= copyout((caddr_t
)sa
,
1512 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1515 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1519 space
-= sizeof (ifr
);
1521 ifnet_lock_done(ifp
);
1526 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1527 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1530 space
-= sizeof (ifr
);
1531 ifrp
+= sizeof(struct ifreq
);
1535 *ret_space
-= space
;
1540 * Just like if_promisc(), but for all-multicast-reception mode.
1543 if_allmulti(ifp
, onswitch
)
1550 ifnet_lock_exclusive(ifp
);
1553 if (ifp
->if_amcount
++ == 0) {
1554 ifp
->if_flags
|= IFF_ALLMULTI
;
1558 if (ifp
->if_amcount
> 1) {
1561 ifp
->if_amcount
= 0;
1562 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1566 ifnet_lock_done(ifp
);
1569 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1578 struct ifmultiaddr
*ifma
)
1580 if (OSIncrementAtomic((SInt32
*)&ifma
->ifma_refcount
) <= 0)
1581 panic("ifma_reference: ifma already released or invalid\n");
1586 struct ifmultiaddr
*ifma
)
1589 struct ifmultiaddr
*next
;
1590 int32_t prevValue
= OSDecrementAtomic((SInt32
*)&ifma
->ifma_refcount
);
1592 panic("ifma_release: ifma already released or invalid\n");
1596 /* Allow the allocator of the protospec to free it */
1597 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1598 ifma
->ifma_free(ifma
->ifma_protospec
);
1601 next
= ifma
->ifma_ll
;
1602 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1603 FREE(ifma
, M_IFMADDR
);
1609 * Find an ifmultiaddr that matches a socket address on an interface.
1611 * Caller is responsible for holding the ifnet_lock while calling
1615 if_addmulti_doesexist(
1617 const struct sockaddr
*sa
,
1618 struct ifmultiaddr
**retifma
)
1620 struct ifmultiaddr
*ifma
;
1621 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1622 ifma
= ifma
->ifma_link
.le_next
) {
1623 if (equal(sa
, ifma
->ifma_addr
)) {
1624 ifma
->ifma_usecount
++;
1627 ifma_reference(*retifma
);
1637 * Add a multicast listenership to the interface in question.
1638 * The link layer provides a routine which converts
1642 struct ifnet
*ifp
, /* interface to manipulate */
1643 const struct sockaddr
*sa
, /* address to add */
1644 struct ifmultiaddr
**retifma
)
1646 struct sockaddr_storage storage
;
1647 struct sockaddr
*llsa
= NULL
;
1648 struct sockaddr
*dupsa
;
1650 struct ifmultiaddr
*ifma
;
1651 struct ifmultiaddr
*llifma
= NULL
;
1653 ifnet_lock_exclusive(ifp
);
1654 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1655 ifnet_lock_done(ifp
);
1661 * Give the link layer a chance to accept/reject it, and also
1662 * find out which AF_LINK address this maps to, if it isn't one
1665 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
, sizeof(storage
));
1666 if (error
== 0 && storage
.ss_len
!= 0) {
1667 MALLOC(llsa
, struct sockaddr
*, storage
.ss_len
, M_IFMADDR
, M_WAITOK
);
1668 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1669 bcopy(&storage
, llsa
, storage
.ss_len
);
1672 /* to be similar to FreeBSD */
1673 if (error
== EOPNOTSUPP
)
1680 /* Allocate while we aren't holding any locks */
1681 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1682 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1683 bcopy(sa
, dupsa
, sa
->sa_len
);
1685 ifnet_lock_exclusive(ifp
);
1687 * Check again for the matching multicast.
1689 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1690 ifnet_lock_done(ifp
);
1691 FREE(ifma
, M_IFMADDR
);
1692 FREE(dupsa
, M_IFMADDR
);
1694 FREE(llsa
, M_IFMADDR
);
1698 bzero(ifma
, sizeof(*ifma
));
1699 ifma
->ifma_addr
= dupsa
;
1700 ifma
->ifma_ifp
= ifp
;
1701 ifma
->ifma_usecount
= 1;
1702 ifma
->ifma_refcount
= 1;
1705 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1706 FREE(llsa
, M_IFMADDR
);
1707 FREE(llifma
, M_IFMADDR
);
1709 bzero(llifma
, sizeof(*llifma
));
1710 llifma
->ifma_addr
= llsa
;
1711 llifma
->ifma_ifp
= ifp
;
1712 llifma
->ifma_usecount
= 1;
1713 llifma
->ifma_refcount
= 1;
1714 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1716 ifma
->ifma_ll
= llifma
;
1717 ifma_reference(ifma
->ifma_ll
);
1721 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1725 ifma_reference(*retifma
);
1728 ifnet_lock_done(ifp
);
1731 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1734 * We are certain we have added something, so call down to the
1735 * interface to let them know about it.
1737 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1744 struct ifmultiaddr
*ifma
,
1748 int do_del_multi
= 0;
1750 ifp
= ifma
->ifma_ifp
;
1752 if (!locked
&& ifp
) {
1753 ifnet_lock_exclusive(ifp
);
1756 while (ifma
!= NULL
) {
1757 struct ifmultiaddr
*ll_ifma
;
1759 if (ifma
->ifma_usecount
> 1) {
1760 ifma
->ifma_usecount
--;
1765 LIST_REMOVE(ifma
, ifma_link
);
1767 ll_ifma
= ifma
->ifma_ll
;
1769 if (ll_ifma
) { /* send a routing msg for network addresses only */
1771 ifnet_lock_done(ifp
);
1772 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1774 ifnet_lock_exclusive(ifp
);
1778 * Make sure the interface driver is notified
1779 * in the case of a link layer mcast group being left.
1782 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
1793 if (!locked
&& ifp
) {
1794 /* This wasn't initially locked, we should unlock it */
1795 ifnet_lock_done(ifp
);
1800 ifnet_lock_done(ifp
);
1801 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1803 ifnet_lock_exclusive(ifp
);
1810 * Remove a reference to a multicast address on this interface. Yell
1811 * if the request does not match an existing membership.
1816 const struct sockaddr
*sa
)
1818 struct ifmultiaddr
*ifma
;
1821 ifnet_lock_exclusive(ifp
);
1822 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1823 ifma
= ifma
->ifma_link
.le_next
)
1824 if (equal(sa
, ifma
->ifma_addr
))
1827 ifnet_lock_done(ifp
);
1831 retval
= if_delmultiaddr(ifma
, 1);
1832 ifnet_lock_done(ifp
);
1839 * We don't use if_setlladdr, our interfaces are responsible for
1840 * handling the SIOCSIFLLADDR ioctl.
1844 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1850 struct ifmultiaddr
*
1851 ifmaof_ifpforaddr(sa
, ifp
)
1852 const struct sockaddr
*sa
;
1855 struct ifmultiaddr
*ifma
;
1857 ifnet_lock_shared(ifp
);
1858 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1859 ifma
= ifma
->ifma_link
.le_next
)
1860 if (equal(ifma
->ifma_addr
, sa
))
1862 ifnet_lock_done(ifp
);
1867 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1868 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1872 * Shutdown all network activity. Used boot() when halting
1875 int if_down_all(void);
1876 int if_down_all(void)
1882 if (ifnet_list_get(IFNET_FAMILY_ANY
, &ifp
, &count
) != 0) {
1883 for (i
= 0; i
< count
; i
++) {
1886 ifnet_list_free(ifp
);
1893 * Delete Routes for a Network Interface
1895 * Called for each routing entry via the rnh->rnh_walktree() call above
1896 * to delete all route entries referencing a detaching network interface.
1899 * rn pointer to node in the routing table
1900 * arg argument passed to rnh->rnh_walktree() - detaching interface
1904 * errno failed - reason indicated
1909 struct radix_node
*rn
,
1912 struct rtentry
*rt
= (struct rtentry
*)rn
;
1913 struct ifnet
*ifp
= arg
;
1916 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1919 * Protect (sorta) against walktree recursion problems
1920 * with cloned routes
1922 if ((rt
->rt_flags
& RTF_UP
) == 0)
1925 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1926 rt_mask(rt
), rt
->rt_flags
,
1927 (struct rtentry
**) NULL
);
1929 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1937 * Removes routing table reference to a given interfacei
1938 * for a given protocol family
1940 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1942 struct radix_node_head
*rnh
;
1944 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
1945 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
1946 lck_mtx_lock(rt_mtx
);
1947 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
1948 lck_mtx_unlock(rt_mtx
);
1952 extern lck_spin_t
*dlil_input_lock
;
1954 __private_extern__
void
1955 if_data_internal_to_if_data(
1956 const struct if_data_internal
*if_data_int
,
1957 struct if_data
*if_data
)
1959 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
1960 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
1961 COPYFIELD(ifi_type
);
1962 COPYFIELD(ifi_typelen
);
1963 COPYFIELD(ifi_physical
);
1964 COPYFIELD(ifi_addrlen
);
1965 COPYFIELD(ifi_hdrlen
);
1966 COPYFIELD(ifi_recvquota
);
1967 COPYFIELD(ifi_xmitquota
);
1968 if_data
->ifi_unused1
= 0;
1970 COPYFIELD(ifi_metric
);
1971 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
1972 if_data
->ifi_baudrate
= 0xFFFFFFFF;
1975 COPYFIELD32(ifi_baudrate
);
1978 lck_spin_lock(dlil_input_lock
);
1979 COPYFIELD32(ifi_ipackets
);
1980 COPYFIELD32(ifi_ierrors
);
1981 COPYFIELD32(ifi_opackets
);
1982 COPYFIELD32(ifi_oerrors
);
1983 COPYFIELD32(ifi_collisions
);
1984 COPYFIELD32(ifi_ibytes
);
1985 COPYFIELD32(ifi_obytes
);
1986 COPYFIELD32(ifi_imcasts
);
1987 COPYFIELD32(ifi_omcasts
);
1988 COPYFIELD32(ifi_iqdrops
);
1989 COPYFIELD32(ifi_noproto
);
1990 COPYFIELD32(ifi_recvtiming
);
1991 COPYFIELD32(ifi_xmittiming
);
1992 COPYFIELD(ifi_lastchange
);
1993 lck_spin_unlock(dlil_input_lock
);
1995 #if IF_LASTCHANGEUPTIME
1996 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
1999 if_data
->ifi_unused2
= 0;
2000 COPYFIELD(ifi_hwassist
);
2001 if_data
->ifi_reserved1
= 0;
2002 if_data
->ifi_reserved2
= 0;
2007 __private_extern__
void
2008 if_data_internal_to_if_data64(
2009 const struct if_data_internal
*if_data_int
,
2010 struct if_data64
*if_data64
)
2012 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2013 COPYFIELD(ifi_type
);
2014 COPYFIELD(ifi_typelen
);
2015 COPYFIELD(ifi_physical
);
2016 COPYFIELD(ifi_addrlen
);
2017 COPYFIELD(ifi_hdrlen
);
2018 COPYFIELD(ifi_recvquota
);
2019 COPYFIELD(ifi_xmitquota
);
2020 if_data64
->ifi_unused1
= 0;
2022 COPYFIELD(ifi_metric
);
2023 COPYFIELD(ifi_baudrate
);
2025 lck_spin_lock(dlil_input_lock
);
2026 COPYFIELD(ifi_ipackets
);
2027 COPYFIELD(ifi_ierrors
);
2028 COPYFIELD(ifi_opackets
);
2029 COPYFIELD(ifi_oerrors
);
2030 COPYFIELD(ifi_collisions
);
2031 COPYFIELD(ifi_ibytes
);
2032 COPYFIELD(ifi_obytes
);
2033 COPYFIELD(ifi_imcasts
);
2034 COPYFIELD(ifi_omcasts
);
2035 COPYFIELD(ifi_iqdrops
);
2036 COPYFIELD(ifi_noproto
);
2037 COPYFIELD(ifi_recvtiming
);
2038 COPYFIELD(ifi_xmittiming
);
2039 COPYFIELD(ifi_lastchange
);
2040 lck_spin_unlock(dlil_input_lock
);
2042 #if IF_LASTCHANGEUPTIME
2043 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();