2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1980, 1986, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)if.c 8.3 (Berkeley) 1/4/94
55 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
58 #include <kern/locks.h>
60 #include <sys/param.h>
61 #include <sys/malloc.h>
63 #include <sys/systm.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/protosw.h>
68 #include <sys/kernel.h>
69 #include <sys/sockio.h>
70 #include <sys/syslog.h>
71 #include <sys/sysctl.h>
74 #include <net/if_arp.h>
75 #include <net/if_dl.h>
76 #include <net/if_types.h>
77 #include <net/if_var.h>
78 #include <net/net_osdep.h>
80 #include <net/radix.h>
81 #include <net/route.h>
85 #include <sys/domain.h>
86 #include <libkern/OSAtomic.h>
89 #if defined(INET) || defined(INET6)
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet6/in6_ifattach.h>
100 * System initialization
103 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
104 static void if_qflush(struct ifqueue
*);
105 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
106 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
108 static struct if_clone
*if_clone_lookup(const char *, int *);
110 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
113 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
114 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
116 int ifqmaxlen
= IFQ_MAXLEN
;
117 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
119 static int if_cloners_count
;
120 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
124 * XXX: declare here to avoid to include many inet6 related files..
125 * should be more generalized?
127 extern void nd6_setmtu(struct ifnet
*);
130 #define M_CLONE M_IFADDR
133 * Network interface utility routines.
135 * Routines with ifa_ifwith* names take sockaddr *'s as
140 struct ifaddr
**ifnet_addrs
;
141 struct ifnet
**ifindex2ifnet
;
143 __private_extern__
void
148 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
149 if (ifa
->ifa_debug
& IFA_ATTACHED
) {
150 panic("if_attach_ifa: Attempted to attach address that's already attached!\n");
153 ifa
->ifa_debug
|= IFA_ATTACHED
;
154 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
157 __private_extern__
void
162 ifnet_lock_assert(ifp
, LCK_MTX_ASSERT_OWNED
);
165 if ((ifa
->ifa_debug
& IFA_ATTACHED
) == 0) {
166 printf("if_detach_ifa: ifa is not attached to any interface! flags=%\n", ifa
->ifa_debug
);
171 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
176 printf("if_detach_ifa: Attempted to detach IFA that was not attached!\n");
180 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
181 ifa
->ifa_debug
&= ~IFA_ATTACHED
;
185 #define INITIAL_IF_INDEXLIM 8
188 * Function: if_next_index
190 * Return the next available interface index.
191 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
192 * added entry when necessary.
195 * ifnet_addrs[] is indexed by (if_index - 1), whereas
196 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
197 * always allocate one extra element to hold ifindex2ifnet[0], which
200 int if_next_index(void);
202 __private_extern__
int
205 static int if_indexlim
= 0;
208 new_index
= ++if_index
;
209 if (if_index
> if_indexlim
) {
212 caddr_t new_ifnet_addrs
;
213 caddr_t new_ifindex2ifnet
;
214 caddr_t old_ifnet_addrs
;
216 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
217 if (ifnet_addrs
== NULL
) {
218 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
220 new_if_indexlim
= if_indexlim
<< 1;
223 /* allocate space for the larger arrays */
224 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
225 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
226 new_ifindex2ifnet
= new_ifnet_addrs
227 + new_if_indexlim
* sizeof(caddr_t
);
228 bzero(new_ifnet_addrs
, n
);
229 if (ifnet_addrs
!= NULL
) {
230 /* copy the existing data */
231 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
232 if_indexlim
* sizeof(caddr_t
));
233 bcopy((caddr_t
)ifindex2ifnet
,
235 (if_indexlim
+ 1) * sizeof(caddr_t
));
238 /* switch to the new tables and size */
239 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
240 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
241 if_indexlim
= new_if_indexlim
;
243 /* release the old data */
244 if (old_ifnet_addrs
!= NULL
) {
245 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
252 * Create a clone network interface.
255 if_clone_create(char *name
, int len
)
257 struct if_clone
*ifc
;
259 int wildcard
, bytoff
, bitoff
;
263 ifc
= if_clone_lookup(name
, &unit
);
267 if (ifunit(name
) != NULL
)
271 wildcard
= (unit
< 0);
273 * Find a free unit if none was given.
276 while ((bytoff
< ifc
->ifc_bmlen
)
277 && (ifc
->ifc_units
[bytoff
] == 0xff))
279 if (bytoff
>= ifc
->ifc_bmlen
)
281 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
283 unit
= (bytoff
<< 3) + bitoff
;
286 if (unit
> ifc
->ifc_maxunit
)
289 err
= (*ifc
->ifc_create
)(ifc
, unit
);
295 bitoff
= unit
- (bytoff
<< 3);
299 * Allocate the unit in the bitmap.
301 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
302 ("%s: bit is already set", __func__
));
303 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
305 /* In the wildcard case, we need to update the name. */
307 for (dp
= name
; *dp
!= '\0'; dp
++);
308 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
309 len
- (dp
-name
) - 1) {
311 * This can only be a programmer error and
312 * there's no straightforward way to recover if
315 panic("if_clone_create(): interface name too long");
324 * Destroy a clone network interface.
327 if_clone_destroy(const char *name
)
329 struct if_clone
*ifc
;
334 ifc
= if_clone_lookup(name
, &unit
);
338 if (unit
< ifc
->ifc_minifs
)
345 if (ifc
->ifc_destroy
== NULL
)
348 (*ifc
->ifc_destroy
)(ifp
);
351 * Compute offset in the bitmap and deallocate the unit.
354 bitoff
= unit
- (bytoff
<< 3);
355 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
356 ("%s: bit is already cleared", __func__
));
357 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
362 * Look up a network interface cloner.
365 static struct if_clone
*
366 if_clone_lookup(const char *name
, int *unitp
)
368 struct if_clone
*ifc
;
372 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
373 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
374 if (ifc
->ifc_name
[i
] != *cp
)
379 ifc
= LIST_NEXT(ifc
, ifc_list
);
383 return ((struct if_clone
*)NULL
);
389 for (i
= 0; *cp
!= '\0'; cp
++) {
390 if (*cp
< '0' || *cp
> '9') {
391 /* Bogus unit number. */
394 i
= (i
* 10) + (*cp
- '0');
404 * Register a network interface cloner.
407 if_clone_attach(struct if_clone
*ifc
)
414 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
415 ("%s: %s requested more units then allowed (%d > %d)",
416 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
417 ifc
->ifc_maxunit
+ 1));
419 * Compute bitmap size and allocate it.
421 maxclone
= ifc
->ifc_maxunit
+ 1;
423 if ((len
<< 3) < maxclone
)
425 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
426 bzero(ifc
->ifc_units
, len
);
427 ifc
->ifc_bmlen
= len
;
429 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
432 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
433 err
= (*ifc
->ifc_create
)(ifc
, unit
);
435 ("%s: failed to create required interface %s%d",
436 __func__
, ifc
->ifc_name
, unit
));
438 /* Allocate the unit in the bitmap. */
440 bitoff
= unit
- (bytoff
<< 3);
441 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
446 * Unregister a network interface cloner.
449 if_clone_detach(struct if_clone
*ifc
)
452 LIST_REMOVE(ifc
, ifc_list
);
453 FREE(ifc
->ifc_units
, M_CLONE
);
459 * Provide list of interface cloners to userspace.
462 if_clone_list(int count
, int * total
, user_addr_t dst
)
464 char outbuf
[IFNAMSIZ
];
465 struct if_clone
*ifc
;
468 *total
= if_cloners_count
;
469 if (dst
== USER_ADDR_NULL
) {
470 /* Just asking how many there are. */
477 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
479 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
480 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
481 strncpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
- 1);
482 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
491 int ifa_foraddr(unsigned int addr
);
492 __private_extern__
int
501 ifnet_head_lock_shared();
502 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
503 ifnet_lock_shared(ifp
);
504 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
505 ifa
= ifa
->ifa_link
.tqe_next
) {
506 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
508 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
515 ifnet_lock_done(ifp
);
523 * Locate an interface based on a complete address.
528 const struct sockaddr
*addr
)
532 struct ifaddr
*result
= 0;
534 #define equal(a1, a2) \
535 (bcmp((const void*)(a1), (const void*)(a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
537 ifnet_head_lock_shared();
538 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
539 ifnet_lock_shared(ifp
);
540 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
541 ifa
= ifa
->ifa_link
.tqe_next
) {
542 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
544 if (equal(addr
, ifa
->ifa_addr
)) {
548 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
549 /* IP6 doesn't have broadcast */
550 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
551 equal(ifa
->ifa_broadaddr
, addr
)) {
558 ifnet_lock_done(ifp
);
565 * Locate the point to point interface with a given destination address.
570 const struct sockaddr
*addr
)
574 struct ifaddr
*result
= 0;
576 ifnet_head_lock_shared();
577 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
; ifp
= ifp
->if_link
.tqe_next
) {
578 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
579 ifnet_lock_shared(ifp
);
580 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
581 ifa
= ifa
->ifa_link
.tqe_next
) {
582 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
584 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
591 ifnet_lock_done(ifp
);
599 * Find an interface on a specific network. If many, choice
600 * is most specific found.
604 const struct sockaddr
*addr
)
607 struct ifaddr
*ifa
= NULL
;
608 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
609 u_int af
= addr
->sa_family
;
610 char *addr_data
= addr
->sa_data
, *cplim
;
612 ifnet_head_lock_shared();
614 * AF_LINK addresses can be looked up directly by their index number,
615 * so do that if we can.
618 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
619 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
620 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
631 * Scan though each interface, looking for ones that have
632 * addresses in this address family.
634 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
635 ifnet_lock_shared(ifp
);
636 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
637 ifa
= ifa
->ifa_link
.tqe_next
) {
638 char *cp
, *cp2
, *cp3
;
640 if (ifa
->ifa_addr
->sa_family
!= af
)
643 /* This breaks tunneling application trying to install a route with
644 * a specific subnet and the local address as the destination
645 * It's breaks binary compatibility with previous version of MacOS X
649 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
650 addr
->sa_family
!= AF_INET6
&&
652 ifp
->if_flags
& IFF_POINTOPOINT
) {
654 * This is a bit broken as it doesn't
655 * take into account that the remote end may
656 * be a single node in the network we are
658 * The trouble is that we don't know the
659 * netmask for the remote end.
661 if (ifa
->ifa_dstaddr
!= 0
662 && equal(addr
, ifa
->ifa_dstaddr
)) {
666 #endif /* __APPLE__*/
669 * if we have a special address handler,
670 * then use it instead of the generic one.
672 if (ifa
->ifa_claim_addr
) {
673 if (ifa
->ifa_claim_addr(ifa
, addr
)) {
681 * Scan all the bits in the ifa's address.
682 * If a bit dissagrees with what we are
683 * looking for, mask it with the netmask
684 * to see if it really matters.
687 if (ifa
->ifa_netmask
== 0)
690 cp2
= ifa
->ifa_addr
->sa_data
;
691 cp3
= ifa
->ifa_netmask
->sa_data
;
692 cplim
= ifa
->ifa_netmask
->sa_len
693 + (char *)ifa
->ifa_netmask
;
695 if ((*cp
++ ^ *cp2
++) & *cp3
++)
696 goto next
; /* next address! */
698 * If the netmask of what we just found
699 * is more specific than what we had before
700 * (if we had one) then remember the new one
701 * before continuing to search
702 * for an even better one.
704 if (ifa_maybe
== 0 ||
705 rn_refines((caddr_t
)ifa
->ifa_netmask
,
706 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
720 * ifa is set if we found an exact match.
721 * take a reference to the ifa before
722 * releasing the ifp lock
724 ifnet_lock_done(ifp
);
733 else if (ifa_maybe
) {
741 * Find an interface address specific to an interface best matching
746 const struct sockaddr
*addr
,
749 struct ifaddr
*ifa
= 0;
750 const char *cp
, *cp2
, *cp3
;
752 struct ifaddr
*ifa_maybe
= 0;
753 u_int af
= addr
->sa_family
;
758 ifnet_lock_shared(ifp
);
759 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
760 ifa
= ifa
->ifa_link
.tqe_next
) {
761 if (ifa
->ifa_addr
->sa_family
!= af
)
765 if (ifa
->ifa_netmask
== 0) {
766 if (equal(addr
, ifa
->ifa_addr
) ||
767 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
771 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
772 if (equal(addr
, ifa
->ifa_dstaddr
))
776 cp2
= ifa
->ifa_addr
->sa_data
;
777 cp3
= ifa
->ifa_netmask
->sa_data
;
778 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
779 for (; cp3
< cplim
; cp3
++)
780 if ((*cp
++ ^ *cp2
++) & *cp3
)
787 if (!ifa
) ifa
= ifa_maybe
;
788 if (ifa
) ifaref(ifa
);
790 ifnet_lock_done(ifp
);
794 #include <net/route.h>
797 * Default action when installing a route with a Link Level gateway.
798 * Lookup an appropriate real ifa to point to.
799 * This should be moved to /sys/net/link.c eventually.
802 link_rtrequest(cmd
, rt
, sa
)
808 struct sockaddr
*dst
;
811 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
812 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
814 ifa
= ifaof_ifpforaddr(dst
, ifp
);
817 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
818 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
824 * if_updown will set the interface up or down. It will
825 * prevent other up/down events from occurring until this
826 * up/down event has completed.
828 * Caller must lock ifnet. This function will drop the
829 * lock. This allows ifnet_set_flags to set the rest of
830 * the flags after we change the up/down state without
831 * dropping the interface lock between setting the
832 * up/down state and updating the rest of the flags.
834 __private_extern__
void
843 /* Wait until no one else is changing the up/down state */
844 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
846 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
847 ifnet_lock_done(ifp
);
848 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
849 ifnet_lock_exclusive(ifp
);
852 /* Verify that the interface isn't already in the right state */
853 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
854 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
858 /* Indicate that the up/down state is changing */
859 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
861 /* Mark interface up or down */
863 ifp
->if_flags
|= IFF_UP
;
866 ifp
->if_flags
&= ~IFF_UP
;
869 ifnet_touch_lastchange(ifp
);
871 /* Drop the lock to notify addresses and route */
872 ifnet_lock_done(ifp
);
873 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
874 for (i
= 0; ifa
[i
] != 0; i
++) {
875 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
877 ifnet_free_address_list(ifa
);
881 /* Aquire the lock to clear the changing flag and flush the send queue */
882 ifnet_lock_exclusive(ifp
);
884 if_qflush(&ifp
->if_snd
);
885 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
886 wakeup(&ifp
->if_eflags
);
892 * Mark an interface down and notify protocols of
899 ifnet_lock_exclusive(ifp
);
901 ifnet_lock_done(ifp
);
905 * Mark an interface up and notify protocols of
912 ifnet_lock_exclusive(ifp
);
914 ifnet_lock_done(ifp
);
918 * Flush an interface queue.
927 while ((m
= n
) != 0) {
937 * Map interface name to
938 * interface structure pointer.
941 ifunit(const char *name
)
943 char namebuf
[IFNAMSIZ
+ 1];
951 if (len
< 2 || len
> IFNAMSIZ
)
955 if (c
< '0' || c
> '9')
956 return NULL
; /* trailing garbage */
961 return NULL
; /* no interface name */
962 unit
+= (c
- '0') * m
;
964 return NULL
; /* number is unreasonable */
967 } while (c
>= '0' && c
<= '9');
969 bcopy(name
, namebuf
, len
);
972 * Now search all the interfaces for this name/number
974 ifnet_head_lock_shared();
975 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
976 if (strcmp(ifp
->if_name
, namebuf
))
978 if (unit
== ifp
->if_unit
)
987 * Map interface name in a sockaddr_dl to
988 * interface structure pointer.
994 char ifname
[IFNAMSIZ
+1];
995 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
997 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
998 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1002 * ifunit wants a null-terminated name. It may not be null-terminated
1003 * in the sockaddr. We don't want to change the caller's sockaddr,
1004 * and there might not be room to put the trailing null anyway, so we
1005 * make a local copy that we know we can null terminate safely.
1008 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1009 ifname
[sdl
->sdl_nlen
] = '\0';
1010 return ifunit(ifname
);
1018 ifioctl(so
, cmd
, data
, p
)
1029 struct kev_msg ev_msg
;
1030 struct net_event_data ev_data
;
1037 struct ifconf64
* ifc
= (struct ifconf64
*)data
;
1038 user_addr_t user_addr
;
1040 user_addr
= proc_is64bit(p
)
1041 ? ifc
->ifc_req64
: CAST_USER_ADDR_T(ifc
->ifc_req
);
1042 return (ifconf(cmd
, user_addr
, &ifc
->ifc_len
));
1046 ifr
= (struct ifreq
*)data
;
1050 error
= proc_suser(p
);
1053 return ((cmd
== SIOCIFCREATE
) ?
1054 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1055 if_clone_destroy(ifr
->ifr_name
));
1057 case SIOCIFGCLONERS
:
1058 case SIOCIFGCLONERS64
:
1060 struct if_clonereq64
* ifcr
= (struct if_clonereq64
*)data
;
1061 user_addr
= proc_is64bit(p
)
1062 ? ifcr
->ifcr_ifcru
.ifcru_buffer64
1063 : CAST_USER_ADDR_T(ifcr
->ifcr_ifcru
.ifcru_buffer32
);
1064 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1067 #endif IF_CLONE_LIST
1070 ifp
= ifunit(ifr
->ifr_name
);
1076 ifnet_lock_shared(ifp
);
1077 ifr
->ifr_flags
= ifp
->if_flags
;
1078 ifnet_lock_done(ifp
);
1082 ifnet_lock_shared(ifp
);
1083 ifr
->ifr_metric
= ifp
->if_metric
;
1084 ifnet_lock_done(ifp
);
1088 ifnet_lock_shared(ifp
);
1089 ifr
->ifr_mtu
= ifp
->if_mtu
;
1090 ifnet_lock_done(ifp
);
1094 ifnet_lock_shared(ifp
);
1095 ifr
->ifr_phys
= ifp
->if_physical
;
1096 ifnet_lock_done(ifp
);
1100 error
= proc_suser(p
);
1104 ifnet_set_flags(ifp
, ifr
->ifr_flags
, ~IFF_CANTCHANGE
);
1106 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1107 ifp
, cmd
, (caddr_t
) data
);
1110 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1111 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1112 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1114 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1115 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1116 ev_data
.if_family
= ifp
->if_family
;
1117 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1118 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1119 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1120 ev_msg
.dv
[1].data_length
= 0;
1121 kev_post_msg(&ev_msg
);
1123 ifnet_touch_lastchange(ifp
);
1127 error
= proc_suser(p
);
1130 ifp
->if_metric
= ifr
->ifr_metric
;
1133 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1134 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1135 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1137 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1138 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1139 ev_data
.if_family
= ifp
->if_family
;
1140 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1141 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1142 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1144 ev_msg
.dv
[1].data_length
= 0;
1145 kev_post_msg(&ev_msg
);
1147 ifnet_touch_lastchange(ifp
);
1151 error
= proc_suser(p
);
1155 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1156 ifp
, cmd
, (caddr_t
) data
);
1159 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1160 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1161 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1163 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1164 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1165 ev_data
.if_family
= ifp
->if_family
;
1166 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1167 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1168 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1169 ev_msg
.dv
[1].data_length
= 0;
1170 kev_post_msg(&ev_msg
);
1172 ifnet_touch_lastchange(ifp
);
1178 u_long oldmtu
= ifp
->if_mtu
;
1180 error
= proc_suser(p
);
1183 if (ifp
->if_ioctl
== NULL
)
1184 return (EOPNOTSUPP
);
1185 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1188 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1189 ifp
, cmd
, (caddr_t
) data
);
1192 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1193 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1194 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1196 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1197 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1198 ev_data
.if_family
= ifp
->if_family
;
1199 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1200 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1201 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1202 ev_msg
.dv
[1].data_length
= 0;
1203 kev_post_msg(&ev_msg
);
1205 ifnet_touch_lastchange(ifp
);
1209 * If the link MTU changed, do network layer specific procedure.
1211 if (ifp
->if_mtu
!= oldmtu
) {
1221 error
= proc_suser(p
);
1225 /* Don't allow group membership on non-multicast interfaces. */
1226 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1230 /* Don't let users screw up protocols' entries. */
1231 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1235 if (cmd
== SIOCADDMULTI
) {
1236 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, NULL
);
1237 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1239 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1240 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1243 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1244 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1245 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1246 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1248 ev_data
.if_family
= ifp
->if_family
;
1249 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1250 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1251 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1252 ev_msg
.dv
[1].data_length
= 0;
1253 kev_post_msg(&ev_msg
);
1255 ifnet_touch_lastchange(ifp
);
1259 case SIOCSIFPHYADDR
:
1260 case SIOCDIFPHYADDR
:
1262 case SIOCSIFPHYADDR_IN6
:
1264 case SIOCSLIFPHYADDR
:
1266 case SIOCSIFGENERIC
:
1271 error
= proc_suser(p
);
1275 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1276 ifp
, cmd
, (caddr_t
) data
);
1279 ifnet_touch_lastchange(ifp
);
1283 ifs
= (struct ifstat
*)data
;
1284 ifs
->ascii
[0] = '\0';
1286 case SIOCGIFPSRCADDR
:
1287 case SIOCGIFPDSTADDR
:
1288 case SIOCGLIFPHYADDR
:
1290 case SIOCGIFGENERIC
:
1292 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1293 ifp
, cmd
, (caddr_t
) data
);
1296 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1297 ifp
, cmd
, (caddr_t
) data
);
1300 oif_flags
= ifp
->if_flags
;
1301 if (so
->so_proto
== 0)
1302 return (EOPNOTSUPP
);
1303 #if !COMPAT_43_SOCKET
1305 error
=(*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
, p
));
1306 socket_unlock(so
, 1);
1314 case SIOCSIFDSTADDR
:
1316 case SIOCSIFBRDADDR
:
1317 case SIOCSIFNETMASK
:
1318 #if BYTE_ORDER != BIG_ENDIAN
1319 if (ifr
->ifr_addr
.sa_family
== 0 &&
1320 ifr
->ifr_addr
.sa_len
< 16) {
1321 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1322 ifr
->ifr_addr
.sa_len
= 16;
1325 if (ifr
->ifr_addr
.sa_len
== 0)
1326 ifr
->ifr_addr
.sa_len
= 16;
1334 case OSIOCGIFDSTADDR
:
1335 cmd
= SIOCGIFDSTADDR
;
1338 case OSIOCGIFBRDADDR
:
1339 cmd
= SIOCGIFBRDADDR
;
1342 case OSIOCGIFNETMASK
:
1343 cmd
= SIOCGIFNETMASK
;
1346 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1348 socket_unlock(so
, 1);
1352 case OSIOCGIFDSTADDR
:
1353 case OSIOCGIFBRDADDR
:
1354 case OSIOCGIFNETMASK
:
1355 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1359 #endif /* COMPAT_43_SOCKET */
1361 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1362 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1363 ifp
, cmd
, (caddr_t
) data
);
1371 ifioctllocked(so
, cmd
, data
, p
)
1379 socket_unlock(so
, 0);
1380 error
= ifioctl(so
, cmd
, data
, p
);
1386 * Set/clear promiscuous mode on interface ifp based on the truth value
1387 * of pswitch. The calls are reference counted so that only the first
1388 * "on" request actually has an effect, as does the final "off" request.
1389 * Results are undefined if the "off" and "on" requests are not matched.
1392 ifnet_set_promiscuous(
1402 ifnet_lock_exclusive(ifp
);
1404 oldflags
= ifp
->if_flags
;
1407 * If the device is not configured up, we cannot put it in
1410 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1414 if (ifp
->if_pcount
++ != 0) {
1417 ifp
->if_flags
|= IFF_PROMISC
;
1419 if (--ifp
->if_pcount
> 0)
1421 ifp
->if_flags
&= ~IFF_PROMISC
;
1423 ifr
.ifr_flags
= ifp
->if_flags
;
1425 ifnet_lock_done(ifp
);
1426 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
1430 ifp
->if_flags
= oldflags
;
1432 if (locked
) ifnet_lock_done(ifp
);
1434 log(LOG_INFO
, "%s%d: promiscuous mode %s\n",
1435 ifp
->if_name
, ifp
->if_unit
,
1436 pswitch
!= 0 ? "enabled" : "disabled");
1442 * Return interface configuration
1443 * of system. List may be used
1444 * in later ioctl's (above) to get
1445 * other information.
1449 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1451 struct ifnet
*ifp
= NULL
;
1458 ifnet_head_lock_shared();
1459 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1461 size_t ifnlen
, addrs
;
1463 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1464 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1465 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1466 error
= ENAMETOOLONG
;
1469 strcpy(ifr
.ifr_name
, workbuf
);
1472 ifnet_lock_shared(ifp
);
1475 ifa
= ifp
->if_addrhead
.tqh_first
;
1476 for ( ; space
> sizeof (ifr
) && ifa
;
1477 ifa
= ifa
->ifa_link
.tqe_next
) {
1478 struct sockaddr
*sa
= ifa
->ifa_addr
;
1480 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1484 #if COMPAT_43_SOCKET
1485 if (cmd
== OSIOCGIFCONF
) {
1486 struct osockaddr
*osa
=
1487 (struct osockaddr
*)&ifr
.ifr_addr
;
1489 osa
->sa_family
= sa
->sa_family
;
1490 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1491 ifrp
+= sizeof(struct ifreq
);
1494 if (sa
->sa_len
<= sizeof(*sa
)) {
1496 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof(ifr
));
1497 ifrp
+= sizeof(struct ifreq
);
1499 if (space
< sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
))
1501 space
-= sa
->sa_len
- sizeof(*sa
);
1502 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
.ifr_name
));
1504 error
= copyout((caddr_t
)sa
,
1505 (ifrp
+ offsetof(struct ifreq
, ifr_addr
)),
1508 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
, ifr_addr
));
1512 space
-= sizeof (ifr
);
1514 ifnet_lock_done(ifp
);
1519 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1520 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
1523 space
-= sizeof (ifr
);
1524 ifrp
+= sizeof(struct ifreq
);
1528 *ret_space
-= space
;
1533 * Just like if_promisc(), but for all-multicast-reception mode.
1536 if_allmulti(ifp
, onswitch
)
1543 ifnet_lock_exclusive(ifp
);
1546 if (ifp
->if_amcount
++ == 0) {
1547 ifp
->if_flags
|= IFF_ALLMULTI
;
1551 if (ifp
->if_amcount
> 1) {
1554 ifp
->if_amcount
= 0;
1555 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1559 ifnet_lock_done(ifp
);
1562 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1571 struct ifmultiaddr
*ifma
)
1573 if (OSIncrementAtomic((SInt32
*)&ifma
->ifma_refcount
) <= 0)
1574 panic("ifma_reference: ifma already released or invalid\n");
1579 struct ifmultiaddr
*ifma
)
1582 struct ifmultiaddr
*next
;
1583 int32_t prevValue
= OSDecrementAtomic((SInt32
*)&ifma
->ifma_refcount
);
1585 panic("ifma_release: ifma already released or invalid\n");
1589 /* Allow the allocator of the protospec to free it */
1590 if (ifma
->ifma_protospec
&& ifma
->ifma_free
) {
1591 ifma
->ifma_free(ifma
->ifma_protospec
);
1594 next
= ifma
->ifma_ll
;
1595 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1596 FREE(ifma
, M_IFMADDR
);
1602 * Find an ifmultiaddr that matches a socket address on an interface.
1604 * Caller is responsible for holding the ifnet_lock while calling
1608 if_addmulti_doesexist(
1610 const struct sockaddr
*sa
,
1611 struct ifmultiaddr
**retifma
)
1613 struct ifmultiaddr
*ifma
;
1614 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1615 ifma
= ifma
->ifma_link
.le_next
) {
1616 if (equal(sa
, ifma
->ifma_addr
)) {
1617 ifma
->ifma_usecount
++;
1620 ifma_reference(*retifma
);
1630 * Add a multicast listenership to the interface in question.
1631 * The link layer provides a routine which converts
1635 struct ifnet
*ifp
, /* interface to manipulate */
1636 const struct sockaddr
*sa
, /* address to add */
1637 struct ifmultiaddr
**retifma
)
1639 struct sockaddr_storage storage
;
1640 struct sockaddr
*llsa
= NULL
;
1641 struct sockaddr
*dupsa
;
1643 struct ifmultiaddr
*ifma
;
1644 struct ifmultiaddr
*llifma
= NULL
;
1646 ifnet_lock_exclusive(ifp
);
1647 error
= if_addmulti_doesexist(ifp
, sa
, retifma
);
1648 ifnet_lock_done(ifp
);
1654 * Give the link layer a chance to accept/reject it, and also
1655 * find out which AF_LINK address this maps to, if it isn't one
1658 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
, sizeof(storage
));
1659 if (error
== 0 && storage
.ss_len
!= 0) {
1660 MALLOC(llsa
, struct sockaddr
*, storage
.ss_len
, M_IFMADDR
, M_WAITOK
);
1661 MALLOC(llifma
, struct ifmultiaddr
*, sizeof *llifma
, M_IFMADDR
, M_WAITOK
);
1662 bcopy(&storage
, llsa
, storage
.ss_len
);
1665 /* to be similar to FreeBSD */
1666 if (error
== EOPNOTSUPP
)
1673 /* Allocate while we aren't holding any locks */
1674 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1675 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1676 bcopy(sa
, dupsa
, sa
->sa_len
);
1678 ifnet_lock_exclusive(ifp
);
1680 * Check again for the matching multicast.
1682 if ((error
= if_addmulti_doesexist(ifp
, sa
, retifma
)) == 0) {
1683 ifnet_lock_done(ifp
);
1684 FREE(ifma
, M_IFMADDR
);
1685 FREE(dupsa
, M_IFMADDR
);
1687 FREE(llsa
, M_IFMADDR
);
1691 bzero(ifma
, sizeof(*ifma
));
1692 ifma
->ifma_addr
= dupsa
;
1693 ifma
->ifma_ifp
= ifp
;
1694 ifma
->ifma_usecount
= 1;
1695 ifma
->ifma_refcount
= 1;
1698 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
) == 0) {
1699 FREE(llsa
, M_IFMADDR
);
1700 FREE(llifma
, M_IFMADDR
);
1702 bzero(llifma
, sizeof(*llifma
));
1703 llifma
->ifma_addr
= llsa
;
1704 llifma
->ifma_ifp
= ifp
;
1705 llifma
->ifma_usecount
= 1;
1706 llifma
->ifma_refcount
= 1;
1707 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, llifma
, ifma_link
);
1709 ifma
->ifma_ll
= llifma
;
1710 ifma_reference(ifma
->ifma_ll
);
1714 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1718 ifma_reference(*retifma
);
1721 ifnet_lock_done(ifp
);
1724 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1727 * We are certain we have added something, so call down to the
1728 * interface to let them know about it.
1730 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1737 struct ifmultiaddr
*ifma
,
1741 int do_del_multi
= 0;
1743 ifp
= ifma
->ifma_ifp
;
1745 if (!locked
&& ifp
) {
1746 ifnet_lock_exclusive(ifp
);
1749 while (ifma
!= NULL
) {
1750 struct ifmultiaddr
*ll_ifma
;
1752 if (ifma
->ifma_usecount
> 1) {
1753 ifma
->ifma_usecount
--;
1758 LIST_REMOVE(ifma
, ifma_link
);
1760 ll_ifma
= ifma
->ifma_ll
;
1762 if (ll_ifma
) { /* send a routing msg for network addresses only */
1764 ifnet_lock_done(ifp
);
1765 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1767 ifnet_lock_exclusive(ifp
);
1771 * Make sure the interface driver is notified
1772 * in the case of a link layer mcast group being left.
1775 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
)
1786 if (!locked
&& ifp
) {
1787 /* This wasn't initially locked, we should unlock it */
1788 ifnet_lock_done(ifp
);
1793 ifnet_lock_done(ifp
);
1794 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1796 ifnet_lock_exclusive(ifp
);
1803 * Remove a reference to a multicast address on this interface. Yell
1804 * if the request does not match an existing membership.
1809 const struct sockaddr
*sa
)
1811 struct ifmultiaddr
*ifma
;
1814 ifnet_lock_exclusive(ifp
);
1815 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1816 ifma
= ifma
->ifma_link
.le_next
)
1817 if (equal(sa
, ifma
->ifma_addr
))
1820 ifnet_lock_done(ifp
);
1824 retval
= if_delmultiaddr(ifma
, 1);
1825 ifnet_lock_done(ifp
);
1832 * We don't use if_setlladdr, our interfaces are responsible for
1833 * handling the SIOCSIFLLADDR ioctl.
1837 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1843 struct ifmultiaddr
*
1844 ifmaof_ifpforaddr(sa
, ifp
)
1845 const struct sockaddr
*sa
;
1848 struct ifmultiaddr
*ifma
;
1850 ifnet_lock_shared(ifp
);
1851 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1852 ifma
= ifma
->ifma_link
.le_next
)
1853 if (equal(ifma
->ifma_addr
, sa
))
1855 ifnet_lock_done(ifp
);
1860 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1861 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1865 * Shutdown all network activity. Used boot() when halting
1868 int if_down_all(void);
1869 int if_down_all(void)
1875 if (ifnet_list_get(IFNET_FAMILY_ANY
, &ifp
, &count
) != 0) {
1876 for (i
= 0; i
< count
; i
++) {
1879 ifnet_list_free(ifp
);
1886 * Delete Routes for a Network Interface
1888 * Called for each routing entry via the rnh->rnh_walktree() call above
1889 * to delete all route entries referencing a detaching network interface.
1892 * rn pointer to node in the routing table
1893 * arg argument passed to rnh->rnh_walktree() - detaching interface
1897 * errno failed - reason indicated
1902 struct radix_node
*rn
,
1905 struct rtentry
*rt
= (struct rtentry
*)rn
;
1906 struct ifnet
*ifp
= arg
;
1909 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1912 * Protect (sorta) against walktree recursion problems
1913 * with cloned routes
1915 if ((rt
->rt_flags
& RTF_UP
) == 0)
1918 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1919 rt_mask(rt
), rt
->rt_flags
,
1920 (struct rtentry
**) NULL
);
1922 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1930 * Removes routing table reference to a given interfacei
1931 * for a given protocol family
1933 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1936 struct radix_node_head
*rnh
;
1938 if ((protocol
<= AF_MAX
) && ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
1939 lck_mtx_lock(rt_mtx
);
1940 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
1941 lck_mtx_unlock(rt_mtx
);
1945 extern lck_spin_t
*dlil_input_lock
;
1947 __private_extern__
void
1948 if_data_internal_to_if_data(
1949 const struct if_data_internal
*if_data_int
,
1950 struct if_data
*if_data
)
1952 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
1953 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
1954 COPYFIELD(ifi_type
);
1955 COPYFIELD(ifi_typelen
);
1956 COPYFIELD(ifi_physical
);
1957 COPYFIELD(ifi_addrlen
);
1958 COPYFIELD(ifi_hdrlen
);
1959 COPYFIELD(ifi_recvquota
);
1960 COPYFIELD(ifi_xmitquota
);
1961 if_data
->ifi_unused1
= 0;
1963 COPYFIELD(ifi_metric
);
1964 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
1965 if_data
->ifi_baudrate
= 0xFFFFFFFF;
1968 COPYFIELD32(ifi_baudrate
);
1971 lck_spin_lock(dlil_input_lock
);
1972 COPYFIELD32(ifi_ipackets
);
1973 COPYFIELD32(ifi_ierrors
);
1974 COPYFIELD32(ifi_opackets
);
1975 COPYFIELD32(ifi_oerrors
);
1976 COPYFIELD32(ifi_collisions
);
1977 COPYFIELD32(ifi_ibytes
);
1978 COPYFIELD32(ifi_obytes
);
1979 COPYFIELD32(ifi_imcasts
);
1980 COPYFIELD32(ifi_omcasts
);
1981 COPYFIELD32(ifi_iqdrops
);
1982 COPYFIELD32(ifi_noproto
);
1983 COPYFIELD32(ifi_recvtiming
);
1984 COPYFIELD32(ifi_xmittiming
);
1985 COPYFIELD(ifi_lastchange
);
1986 lck_spin_unlock(dlil_input_lock
);
1988 #if IF_LASTCHANGEUPTIME
1989 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
1992 if_data
->ifi_unused2
= 0;
1993 COPYFIELD(ifi_hwassist
);
1994 if_data
->ifi_reserved1
= 0;
1995 if_data
->ifi_reserved2
= 0;
2000 __private_extern__
void
2001 if_data_internal_to_if_data64(
2002 const struct if_data_internal
*if_data_int
,
2003 struct if_data64
*if_data64
)
2005 #define COPYFIELD(fld) if_data64->fld = if_data_int->fld
2006 COPYFIELD(ifi_type
);
2007 COPYFIELD(ifi_typelen
);
2008 COPYFIELD(ifi_physical
);
2009 COPYFIELD(ifi_addrlen
);
2010 COPYFIELD(ifi_hdrlen
);
2011 COPYFIELD(ifi_recvquota
);
2012 COPYFIELD(ifi_xmitquota
);
2013 if_data64
->ifi_unused1
= 0;
2015 COPYFIELD(ifi_metric
);
2016 COPYFIELD(ifi_baudrate
);
2018 lck_spin_lock(dlil_input_lock
);
2019 COPYFIELD(ifi_ipackets
);
2020 COPYFIELD(ifi_ierrors
);
2021 COPYFIELD(ifi_opackets
);
2022 COPYFIELD(ifi_oerrors
);
2023 COPYFIELD(ifi_collisions
);
2024 COPYFIELD(ifi_ibytes
);
2025 COPYFIELD(ifi_obytes
);
2026 COPYFIELD(ifi_imcasts
);
2027 COPYFIELD(ifi_omcasts
);
2028 COPYFIELD(ifi_iqdrops
);
2029 COPYFIELD(ifi_noproto
);
2030 COPYFIELD(ifi_recvtiming
);
2031 COPYFIELD(ifi_xmittiming
);
2032 COPYFIELD(ifi_lastchange
);
2033 lck_spin_unlock(dlil_input_lock
);
2035 #if IF_LASTCHANGEUPTIME
2036 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();