2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)if.c 8.3 (Berkeley) 1/4/94
61 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
64 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <kern/locks.h>
72 #include <sys/param.h>
73 #include <sys/malloc.h>
75 #include <sys/systm.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/protosw.h>
80 #include <sys/kernel.h>
81 #include <sys/sockio.h>
82 #include <sys/syslog.h>
83 #include <sys/sysctl.h>
84 #include <sys/mcache.h>
85 #include <kern/zalloc.h>
87 #include <machine/endian.h>
89 #include <pexpert/pexpert.h>
92 #include <net/if_arp.h>
93 #include <net/if_dl.h>
94 #include <net/if_types.h>
95 #include <net/if_var.h>
96 #include <net/net_osdep.h>
97 #include <net/ethernet.h>
99 #include <net/radix.h>
100 #include <net/route.h>
102 #include <net/dlil.h>
103 //#include <string.h>
104 #include <sys/domain.h>
105 #include <libkern/OSAtomic.h>
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/ip6.h>
115 #include <netinet6/in6_var.h>
116 #include <netinet6/in6_ifattach.h>
117 #include <netinet6/ip6_var.h>
122 #include <security/mac_framework.h>
127 * System initialization
130 /* Lock group and attribute for ifaddr lock */
131 lck_attr_t
*ifa_mtx_attr
;
132 lck_grp_t
*ifa_mtx_grp
;
133 static lck_grp_attr_t
*ifa_mtx_grp_attr
;
135 static int ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
);
136 static void if_qflush(struct ifqueue
*);
137 __private_extern__
void link_rtrequest(int, struct rtentry
*, struct sockaddr
*);
138 void if_rtproto_del(struct ifnet
*ifp
, int protocol
);
140 static int if_addmulti_common(struct ifnet
*, const struct sockaddr
*,
141 struct ifmultiaddr
**, int);
142 static int if_delmulti_common(struct ifmultiaddr
*, struct ifnet
*,
143 const struct sockaddr
*, int);
145 static int if_rtmtu(struct radix_node
*, void *);
146 static void if_rtmtu_update(struct ifnet
*);
149 static int if_clone_list(int count
, int * total
, user_addr_t dst
);
150 #endif /* IF_CLONE_LIST */
152 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
154 int ifqmaxlen
= IFQ_MAXLEN
;
155 struct ifnethead ifnet_head
= TAILQ_HEAD_INITIALIZER(ifnet_head
);
157 static int if_cloners_count
;
158 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
160 static struct ifaddr
*ifa_ifwithnet_common(const struct sockaddr
*,
162 static void if_attach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
163 static void if_detach_ifa_common(struct ifnet
*, struct ifaddr
*, int);
165 static void if_attach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
166 static int if_detach_ifma(struct ifnet
*, struct ifmultiaddr
*, int);
168 static struct ifmultiaddr
*ifma_alloc(int);
169 static void ifma_free(struct ifmultiaddr
*);
170 static void ifma_trace(struct ifmultiaddr
*, int);
173 static unsigned int ifma_debug
= 1; /* debugging (enabled) */
175 static unsigned int ifma_debug
; /* debugging (disabled) */
177 static unsigned int ifma_size
; /* size of zone element */
178 static struct zone
*ifma_zone
; /* zone for ifmultiaddr */
180 #define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
183 __private_extern__
unsigned int ifma_trace_hist_size
= IFMA_TRACE_HIST_SIZE
;
185 struct ifmultiaddr_dbg
{
186 struct ifmultiaddr ifma
; /* ifmultiaddr */
187 u_int16_t ifma_refhold_cnt
; /* # of ref */
188 u_int16_t ifma_refrele_cnt
; /* # of rele */
190 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
192 ctrace_t ifma_refhold
[IFMA_TRACE_HIST_SIZE
];
193 ctrace_t ifma_refrele
[IFMA_TRACE_HIST_SIZE
];
197 TAILQ_ENTRY(ifmultiaddr_dbg
) ifma_trash_link
;
200 /* List of trash ifmultiaddr entries protected by ifma_trash_lock */
201 static TAILQ_HEAD(, ifmultiaddr_dbg
) ifma_trash_head
;
202 static decl_lck_mtx_data(, ifma_trash_lock
);
204 #define IFMA_ZONE_MAX 64 /* maximum elements in zone */
205 #define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
209 * XXX: declare here to avoid to include many inet6 related files..
210 * should be more generalized?
212 extern void nd6_setmtu(struct ifnet
*);
213 extern lck_mtx_t
*nd6_mutex
;
220 /* Setup lock group and attribute for ifaddr */
221 ifa_mtx_grp_attr
= lck_grp_attr_alloc_init();
222 ifa_mtx_grp
= lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr
);
223 ifa_mtx_attr
= lck_attr_alloc_init();
225 PE_parse_boot_argn("ifa_debug", &ifma_debug
, sizeof (ifma_debug
));
227 ifma_size
= (ifma_debug
== 0) ? sizeof (struct ifmultiaddr
) :
228 sizeof (struct ifmultiaddr_dbg
);
230 ifma_zone
= zinit(ifma_size
, IFMA_ZONE_MAX
* ifma_size
, 0,
232 if (ifma_zone
== NULL
) {
233 panic("%s: failed allocating %s", __func__
, IFMA_ZONE_NAME
);
236 zone_change(ifma_zone
, Z_EXPAND
, TRUE
);
237 zone_change(ifma_zone
, Z_CALLERACCT
, FALSE
);
239 lck_mtx_init(&ifma_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
240 TAILQ_INIT(&ifma_trash_head
);
244 * Network interface utility routines.
246 * Routines with ifa_ifwith* names take sockaddr *'s as
251 struct ifaddr
**ifnet_addrs
;
252 struct ifnet
**ifindex2ifnet
;
254 __private_extern__
void
255 if_attach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
257 if_attach_ifa_common(ifp
, ifa
, 0);
260 __private_extern__
void
261 if_attach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
263 if_attach_ifa_common(ifp
, ifa
, 1);
267 if_attach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
269 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
270 IFA_LOCK_ASSERT_HELD(ifa
);
272 if (ifa
->ifa_ifp
!= ifp
) {
273 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__
,
276 } else if (ifa
->ifa_debug
& IFD_ATTACHED
) {
277 panic("%s: Attempt to attach an already attached ifa=%p",
280 } else if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
281 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
283 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
284 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
287 IFA_ADDREF_LOCKED(ifa
);
288 ifa
->ifa_debug
|= IFD_ATTACHED
;
290 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
292 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
294 if (ifa
->ifa_attached
!= NULL
)
295 (*ifa
->ifa_attached
)(ifa
);
298 __private_extern__
void
299 if_detach_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
301 if_detach_ifa_common(ifp
, ifa
, 0);
304 __private_extern__
void
305 if_detach_link_ifa(struct ifnet
*ifp
, struct ifaddr
*ifa
)
307 if_detach_ifa_common(ifp
, ifa
, 1);
311 if_detach_ifa_common(struct ifnet
*ifp
, struct ifaddr
*ifa
, int link
)
313 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
314 IFA_LOCK_ASSERT_HELD(ifa
);
316 if (link
&& !(ifa
->ifa_debug
& IFD_LINK
)) {
317 panic("%s: Unexpected non-link address ifa=%p", __func__
, ifa
);
319 } else if (link
&& ifa
!= TAILQ_FIRST(&ifp
->if_addrhead
)) {
320 panic("%s: Link address ifa=%p not first", __func__
, ifa
);
322 } else if (!link
&& (ifa
->ifa_debug
& IFD_LINK
)) {
323 panic("%s: Unexpected link address ifa=%p", __func__
, ifa
);
325 } else if (!(ifa
->ifa_debug
& IFD_ATTACHED
)) {
326 panic("%s: Attempt to detach an unattached address ifa=%p",
329 } else if (ifa
->ifa_ifp
!= ifp
) {
330 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__
,
333 } else if (ifa
->ifa_debug
& IFD_DEBUG
) {
335 TAILQ_FOREACH(ifa2
, &ifp
->if_addrhead
, ifa_link
) {
340 panic("%s: Attempt to detach a stray address ifa=%p",
345 TAILQ_REMOVE(&ifp
->if_addrhead
, ifa
, ifa_link
);
346 /* This must not be the last reference to the ifaddr */
347 if (IFA_REMREF_LOCKED(ifa
) == NULL
) {
348 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
, ifa
);
351 ifa
->ifa_debug
&= ~IFD_ATTACHED
;
353 if (ifa
->ifa_detached
!= NULL
)
354 (*ifa
->ifa_detached
)(ifa
);
357 #define INITIAL_IF_INDEXLIM 8
360 * Function: if_next_index
362 * Return the next available interface index.
363 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
364 * added entry when necessary.
367 * ifnet_addrs[] is indexed by (if_index - 1), whereas
368 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
369 * always allocate one extra element to hold ifindex2ifnet[0], which
372 int if_next_index(void);
374 __private_extern__
int
377 static int if_indexlim
= 0;
380 new_index
= ++if_index
;
381 if (if_index
> if_indexlim
) {
384 caddr_t new_ifnet_addrs
;
385 caddr_t new_ifindex2ifnet
;
386 caddr_t old_ifnet_addrs
;
388 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
389 if (ifnet_addrs
== NULL
) {
390 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
392 new_if_indexlim
= if_indexlim
<< 1;
395 /* allocate space for the larger arrays */
396 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
397 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
398 if (new_ifnet_addrs
== NULL
) {
403 new_ifindex2ifnet
= new_ifnet_addrs
404 + new_if_indexlim
* sizeof(caddr_t
);
405 bzero(new_ifnet_addrs
, n
);
406 if (ifnet_addrs
!= NULL
) {
407 /* copy the existing data */
408 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
409 if_indexlim
* sizeof(caddr_t
));
410 bcopy((caddr_t
)ifindex2ifnet
,
412 (if_indexlim
+ 1) * sizeof(caddr_t
));
415 /* switch to the new tables and size */
416 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
417 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
418 if_indexlim
= new_if_indexlim
;
420 /* release the old data */
421 if (old_ifnet_addrs
!= NULL
) {
422 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
429 * Create a clone network interface.
432 if_clone_create(char *name
, int len
, void *params
)
434 struct if_clone
*ifc
;
437 u_int32_t bytoff
, bitoff
;
441 ifc
= if_clone_lookup(name
, &unit
);
445 if (ifunit(name
) != NULL
)
449 wildcard
= (unit
== UINT32_MAX
);
451 * Find a free unit if none was given.
454 while ((bytoff
< ifc
->ifc_bmlen
)
455 && (ifc
->ifc_units
[bytoff
] == 0xff))
457 if (bytoff
>= ifc
->ifc_bmlen
)
459 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
461 unit
= (bytoff
<< 3) + bitoff
;
464 if (unit
> ifc
->ifc_maxunit
)
467 err
= (*ifc
->ifc_create
)(ifc
, unit
, params
);
473 bitoff
= unit
- (bytoff
<< 3);
477 * Allocate the unit in the bitmap.
479 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
480 ("%s: bit is already set", __func__
));
481 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
483 /* In the wildcard case, we need to update the name. */
485 for (dp
= name
; *dp
!= '\0'; dp
++);
486 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
487 len
- (dp
-name
) - 1) {
489 * This can only be a programmer error and
490 * there's no straightforward way to recover if
493 panic("%s: interface name too long", __func__
);
503 * Destroy a clone network interface.
506 if_clone_destroy(const char *name
)
508 struct if_clone
*ifc
;
513 ifc
= if_clone_lookup(name
, &unit
);
517 if (unit
< ifc
->ifc_minifs
)
524 if (ifc
->ifc_destroy
== NULL
)
527 (*ifc
->ifc_destroy
)(ifp
);
530 * Compute offset in the bitmap and deallocate the unit.
533 bitoff
= unit
- (bytoff
<< 3);
534 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
535 ("%s: bit is already cleared", __func__
));
536 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
541 * Look up a network interface cloner.
544 __private_extern__
struct if_clone
*
545 if_clone_lookup(const char *name
, u_int32_t
*unitp
)
547 struct if_clone
*ifc
;
551 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
552 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
553 if (ifc
->ifc_name
[i
] != *cp
)
558 ifc
= LIST_NEXT(ifc
, ifc_list
);
562 return ((struct if_clone
*)NULL
);
568 for (i
= 0; *cp
!= '\0'; cp
++) {
569 if (*cp
< '0' || *cp
> '9') {
570 /* Bogus unit number. */
573 i
= (i
* 10) + (*cp
- '0');
583 * Register a network interface cloner.
586 if_clone_attach(struct if_clone
*ifc
)
593 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
594 ("%s: %s requested more units then allowed (%d > %d)",
595 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
596 ifc
->ifc_maxunit
+ 1));
598 * Compute bitmap size and allocate it.
600 maxclone
= ifc
->ifc_maxunit
+ 1;
602 if ((len
<< 3) < maxclone
)
604 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
605 if (ifc
->ifc_units
== NULL
)
607 bzero(ifc
->ifc_units
, len
);
608 ifc
->ifc_bmlen
= len
;
610 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
613 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
614 err
= (*ifc
->ifc_create
)(ifc
, unit
, NULL
);
616 ("%s: failed to create required interface %s%d",
617 __func__
, ifc
->ifc_name
, unit
));
619 /* Allocate the unit in the bitmap. */
621 bitoff
= unit
- (bytoff
<< 3);
622 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
629 * Unregister a network interface cloner.
632 if_clone_detach(struct if_clone
*ifc
)
635 LIST_REMOVE(ifc
, ifc_list
);
636 FREE(ifc
->ifc_units
, M_CLONE
);
642 * Provide list of interface cloners to userspace.
645 if_clone_list(int count
, int * total
, user_addr_t dst
)
647 char outbuf
[IFNAMSIZ
];
648 struct if_clone
*ifc
;
651 *total
= if_cloners_count
;
652 if (dst
== USER_ADDR_NULL
) {
653 /* Just asking how many there are. */
660 count
= (if_cloners_count
< count
) ? if_cloners_count
: count
;
662 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
663 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
664 strlcpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
);
665 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
672 #endif /* IF_CLONE_LIST */
675 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
676 * and that it matches only the local (not broadcast) address.
678 __private_extern__
struct in_ifaddr
*
679 ifa_foraddr(unsigned int addr
)
681 return (ifa_foraddr_scoped(addr
, IFSCOPE_NONE
));
685 * Similar to ifa_foraddr, except with the added interface scope
686 * constraint (unless the caller passes in IFSCOPE_NONE in which
687 * case there is no scope restriction).
689 __private_extern__
struct in_ifaddr
*
690 ifa_foraddr_scoped(unsigned int addr
, unsigned int scope
)
692 struct in_ifaddr
*ia
= NULL
;
694 lck_rw_lock_shared(in_ifaddr_rwlock
);
695 TAILQ_FOREACH(ia
, INADDR_HASH(addr
), ia_hash
) {
696 IFA_LOCK_SPIN(&ia
->ia_ifa
);
697 if (ia
->ia_addr
.sin_addr
.s_addr
== addr
&&
698 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
699 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
700 IFA_UNLOCK(&ia
->ia_ifa
);
703 IFA_UNLOCK(&ia
->ia_ifa
);
705 lck_rw_done(in_ifaddr_rwlock
);
711 * Similar to ifa_foraddr, except that this for IPv6.
713 __private_extern__
struct in6_ifaddr
*
714 ifa_foraddr6(struct in6_addr
*addr6
)
716 return (ifa_foraddr6_scoped(addr6
, IFSCOPE_NONE
));
719 __private_extern__
struct in6_ifaddr
*
720 ifa_foraddr6_scoped(struct in6_addr
*addr6
, unsigned int scope
)
722 struct in6_ifaddr
*ia
= NULL
;
724 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
725 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
726 IFA_LOCK(&ia
->ia_ifa
);
727 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, addr6
) &&
728 (scope
== IFSCOPE_NONE
|| ia
->ia_ifp
->if_index
== scope
)) {
729 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
730 IFA_UNLOCK(&ia
->ia_ifa
);
733 IFA_UNLOCK(&ia
->ia_ifa
);
735 lck_rw_done(&in6_ifaddr_rwlock
);
742 * Return the first (primary) address of a given family on an interface.
744 __private_extern__
struct ifaddr
*
745 ifa_ifpgetprimary(struct ifnet
*ifp
, int family
)
749 ifnet_lock_shared(ifp
);
750 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
752 if (ifa
->ifa_addr
->sa_family
== family
) {
753 IFA_ADDREF_LOCKED(ifa
); /* for caller */
759 ifnet_lock_done(ifp
);
765 * Locate an interface based on a complete address.
769 ifa_ifwithaddr(const struct sockaddr
*addr
)
773 struct ifaddr
*result
= NULL
;
775 #define equal(a1, a2) \
776 (bcmp((const void*)(a1), (const void*)(a2), \
777 ((const struct sockaddr *)(a1))->sa_len) == 0)
779 ifnet_head_lock_shared();
780 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
781 ifp
= ifp
->if_link
.tqe_next
) {
782 ifnet_lock_shared(ifp
);
783 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
784 ifa
= ifa
->ifa_link
.tqe_next
) {
786 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
790 if (equal(addr
, ifa
->ifa_addr
)) {
792 IFA_ADDREF_LOCKED(ifa
); /* for caller */
796 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
797 ifa
->ifa_broadaddr
!= NULL
&&
798 /* IP6 doesn't have broadcast */
799 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
800 equal(ifa
->ifa_broadaddr
, addr
)) {
802 IFA_ADDREF_LOCKED(ifa
); /* for caller */
808 ifnet_lock_done(ifp
);
815 * Locate the point to point interface with a given destination address.
819 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
823 struct ifaddr
*result
= NULL
;
825 ifnet_head_lock_shared();
826 for (ifp
= ifnet_head
.tqh_first
; ifp
&& !result
;
827 ifp
= ifp
->if_link
.tqe_next
) {
828 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
829 ifnet_lock_shared(ifp
);
830 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
831 ifa
= ifa
->ifa_link
.tqe_next
) {
833 if (ifa
->ifa_addr
->sa_family
!=
838 if (ifa
->ifa_dstaddr
&&
839 equal(addr
, ifa
->ifa_dstaddr
)) {
841 IFA_ADDREF_LOCKED(ifa
); /* for caller */
847 ifnet_lock_done(ifp
);
855 * Locate the source address of an interface based on a complete address.
858 ifa_ifwithaddr_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
860 struct ifaddr
*result
= NULL
;
863 if (ifscope
== IFSCOPE_NONE
)
864 return (ifa_ifwithaddr(addr
));
866 ifnet_head_lock_shared();
867 if (ifscope
> (unsigned int)if_index
) {
872 ifp
= ifindex2ifnet
[ifscope
];
874 struct ifaddr
*ifa
= NULL
;
877 * This is suboptimal; there should be a better way
878 * to search for a given address of an interface
879 * for any given address family.
881 ifnet_lock_shared(ifp
);
882 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
!= NULL
;
883 ifa
= ifa
->ifa_link
.tqe_next
) {
885 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
) {
889 if (equal(addr
, ifa
->ifa_addr
)) {
891 IFA_ADDREF_LOCKED(ifa
); /* for caller */
895 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
896 ifa
->ifa_broadaddr
!= NULL
&&
897 /* IP6 doesn't have broadcast */
898 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
899 equal(ifa
->ifa_broadaddr
, addr
)) {
901 IFA_ADDREF_LOCKED(ifa
); /* for caller */
907 ifnet_lock_done(ifp
);
915 ifa_ifwithnet(const struct sockaddr
*addr
)
917 return (ifa_ifwithnet_common(addr
, IFSCOPE_NONE
));
921 ifa_ifwithnet_scoped(const struct sockaddr
*addr
, unsigned int ifscope
)
923 return (ifa_ifwithnet_common(addr
, ifscope
));
927 * Find an interface on a specific network. If many, choice
928 * is most specific found.
930 static struct ifaddr
*
931 ifa_ifwithnet_common(const struct sockaddr
*addr
, unsigned int ifscope
)
934 struct ifaddr
*ifa
= NULL
;
935 struct ifaddr
*ifa_maybe
= NULL
;
936 u_int af
= addr
->sa_family
;
937 const char *addr_data
= addr
->sa_data
, *cplim
;
940 if ((af
!= AF_INET
&& af
!= AF_INET6
) ||
941 (af
== AF_INET
&& !ip_doscopedroute
) ||
942 (af
== AF_INET6
&& !ip6_doscopedroute
))
944 if (af
!= AF_INET
|| !ip_doscopedroute
)
946 ifscope
= IFSCOPE_NONE
;
948 ifnet_head_lock_shared();
950 * AF_LINK addresses can be looked up directly by their index number,
951 * so do that if we can.
954 const struct sockaddr_dl
*sdl
= (const struct sockaddr_dl
*)addr
;
955 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
) {
956 ifa
= ifnet_addrs
[sdl
->sdl_index
- 1];
966 * Scan though each interface, looking for ones that have
967 * addresses in this address family.
969 for (ifp
= ifnet_head
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
970 ifnet_lock_shared(ifp
);
971 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
972 ifa
= ifa
->ifa_link
.tqe_next
) {
973 const char *cp
, *cp2
, *cp3
;
976 if (ifa
->ifa_addr
== NULL
||
977 ifa
->ifa_addr
->sa_family
!= af
) {
983 /* This breaks tunneling application trying to install a route with
984 * a specific subnet and the local address as the destination
985 * It's breaks binary compatibility with previous version of MacOS X
988 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
989 addr
->sa_family
!= AF_INET6
&&
991 ifp
->if_flags
& IFF_POINTOPOINT
) {
993 * This is a bit broken as it doesn't
994 * take into account that the remote end may
995 * be a single node in the network we are
997 * The trouble is that we don't know the
998 * netmask for the remote end.
1000 if (ifa
->ifa_dstaddr
!= 0 &&
1001 equal(addr
, ifa
->ifa_dstaddr
)) {
1002 IFA_ADDREF_LOCKED(ifa
);
1008 #endif /* __APPLE__*/
1011 * If we're looking up with a scope,
1012 * find using a matching interface.
1014 if (ifscope
!= IFSCOPE_NONE
&&
1015 ifp
->if_index
!= ifscope
) {
1021 * Scan all the bits in the ifa's address.
1022 * If a bit dissagrees with what we are
1023 * looking for, mask it with the netmask
1024 * to see if it really matters.
1025 * (A byte at a time)
1027 if (ifa
->ifa_netmask
== 0) {
1032 cp2
= ifa
->ifa_addr
->sa_data
;
1033 cp3
= ifa
->ifa_netmask
->sa_data
;
1034 cplim
= ifa
->ifa_netmask
->sa_len
1035 + (char *)ifa
->ifa_netmask
;
1037 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1038 goto next
; /* next address! */
1040 * If the netmask of what we just found
1041 * is more specific than what we had before
1042 * (if we had one) then remember the new one
1043 * before continuing to search
1044 * for an even better one.
1046 if (ifa_maybe
== NULL
||
1047 rn_refines((caddr_t
)ifa
->ifa_netmask
,
1048 (caddr_t
)ifa_maybe
->ifa_netmask
)) {
1049 IFA_ADDREF_LOCKED(ifa
); /* ifa_maybe */
1051 if (ifa_maybe
!= NULL
)
1052 IFA_REMREF(ifa_maybe
);
1058 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1060 ifnet_lock_done(ifp
);
1069 else if (ifa_maybe
!= NULL
)
1070 IFA_REMREF(ifa_maybe
);
1076 * Find an interface address specific to an interface best matching
1080 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1082 struct ifaddr
*ifa
= NULL
;
1083 const char *cp
, *cp2
, *cp3
;
1085 struct ifaddr
*ifa_maybe
= NULL
;
1086 struct ifaddr
*better_ifa_maybe
= NULL
;
1087 u_int af
= addr
->sa_family
;
1092 ifnet_lock_shared(ifp
);
1093 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
1094 ifa
= ifa
->ifa_link
.tqe_next
) {
1096 if (ifa
->ifa_addr
->sa_family
!= af
) {
1100 if (ifa_maybe
== NULL
) {
1101 IFA_ADDREF_LOCKED(ifa
); /* for ifa_maybe */
1104 if (ifa
->ifa_netmask
== 0) {
1105 if (equal(addr
, ifa
->ifa_addr
) || (ifa
->ifa_dstaddr
&&
1106 equal(addr
, ifa
->ifa_dstaddr
))) {
1107 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1114 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1115 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)) {
1116 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1121 if (equal(addr
, ifa
->ifa_addr
)) {
1123 IFA_ADDREF_LOCKED(ifa
); /* for caller */
1128 cp2
= ifa
->ifa_addr
->sa_data
;
1129 cp3
= ifa
->ifa_netmask
->sa_data
;
1130 cplim
= ifa
->ifa_netmask
->sa_len
+
1131 (char *)ifa
->ifa_netmask
;
1132 for (; cp3
< cplim
; cp3
++)
1133 if ((*cp
++ ^ *cp2
++) & *cp3
)
1137 if (better_ifa_maybe
== NULL
) {
1138 /* for better_ifa_maybe */
1139 IFA_ADDREF_LOCKED(ifa
);
1140 better_ifa_maybe
= ifa
;
1148 if (better_ifa_maybe
!= NULL
) {
1149 ifa
= better_ifa_maybe
;
1150 better_ifa_maybe
= NULL
;
1157 ifnet_lock_done(ifp
);
1159 if (better_ifa_maybe
!= NULL
)
1160 IFA_REMREF(better_ifa_maybe
);
1161 if (ifa_maybe
!= NULL
)
1162 IFA_REMREF(ifa_maybe
);
1167 #include <net/route.h>
1170 * Default action when installing a route with a Link Level gateway.
1171 * Lookup an appropriate real ifa to point to.
1172 * This should be moved to /sys/net/link.c eventually.
1175 link_rtrequest(int cmd
, struct rtentry
*rt
, struct sockaddr
*sa
)
1178 struct sockaddr
*dst
;
1180 void (*ifa_rtrequest
)(int, struct rtentry
*, struct sockaddr
*);
1182 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
1183 RT_LOCK_ASSERT_HELD(rt
);
1185 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
1186 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
1189 /* Become a regular mutex, just in case */
1190 RT_CONVERT_LOCK(rt
);
1192 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1196 ifa_rtrequest
= ifa
->ifa_rtrequest
;
1198 if (ifa_rtrequest
!= NULL
&& ifa_rtrequest
!= link_rtrequest
)
1199 ifa_rtrequest(cmd
, rt
, sa
);
1205 * if_updown will set the interface up or down. It will
1206 * prevent other up/down events from occurring until this
1207 * up/down event has completed.
1209 * Caller must lock ifnet. This function will drop the
1210 * lock. This allows ifnet_set_flags to set the rest of
1211 * the flags after we change the up/down state without
1212 * dropping the interface lock between setting the
1213 * up/down state and updating the rest of the flags.
1215 __private_extern__
void
1221 struct ifaddr
**ifa
;
1224 /* Wait until no one else is changing the up/down state */
1225 while ((ifp
->if_eflags
& IFEF_UPDOWNCHANGE
) != 0) {
1227 tv
.tv_nsec
= NSEC_PER_SEC
/ 10;
1228 ifnet_lock_done(ifp
);
1229 msleep(&ifp
->if_eflags
, NULL
, 0, "if_updown", &tv
);
1230 ifnet_lock_exclusive(ifp
);
1233 /* Verify that the interface isn't already in the right state */
1234 if ((!up
&& (ifp
->if_flags
& IFF_UP
) == 0) ||
1235 (up
&& (ifp
->if_flags
& IFF_UP
) == IFF_UP
)) {
1239 /* Indicate that the up/down state is changing */
1240 ifp
->if_eflags
|= IFEF_UPDOWNCHANGE
;
1242 /* Mark interface up or down */
1244 ifp
->if_flags
|= IFF_UP
;
1247 ifp
->if_flags
&= ~IFF_UP
;
1250 ifnet_touch_lastchange(ifp
);
1252 /* Drop the lock to notify addresses and route */
1253 ifnet_lock_done(ifp
);
1254 if (ifnet_get_address_list(ifp
, &ifa
) == 0) {
1255 for (i
= 0; ifa
[i
] != 0; i
++) {
1256 pfctlinput(up
? PRC_IFUP
: PRC_IFDOWN
, ifa
[i
]->ifa_addr
);
1258 ifnet_free_address_list(ifa
);
1262 /* Aquire the lock to clear the changing flag and flush the send queue */
1263 ifnet_lock_exclusive(ifp
);
1265 if_qflush(&ifp
->if_snd
);
1266 ifp
->if_eflags
&= ~IFEF_UPDOWNCHANGE
;
1267 wakeup(&ifp
->if_eflags
);
1273 * Mark an interface down and notify protocols of
1280 ifnet_lock_exclusive(ifp
);
1282 ifnet_lock_done(ifp
);
1286 * Mark an interface up and notify protocols of
1293 ifnet_lock_exclusive(ifp
);
1295 ifnet_lock_done(ifp
);
1299 * Flush an interface queue.
1302 if_qflush(struct ifqueue
*ifq
)
1307 while ((m
= n
) != 0) {
1311 ifq
->ifq_head
= NULL
;
1312 ifq
->ifq_tail
= NULL
;
1317 * Map interface name to
1318 * interface structure pointer.
1321 ifunit(const char *name
)
1323 char namebuf
[IFNAMSIZ
+ 1];
1331 if (len
< 2 || len
> IFNAMSIZ
)
1333 cp
= name
+ len
- 1;
1335 if (c
< '0' || c
> '9')
1336 return (NULL
); /* trailing garbage */
1341 return (NULL
); /* no interface name */
1342 unit
+= (c
- '0') * m
;
1344 return (NULL
); /* number is unreasonable */
1347 } while (c
>= '0' && c
<= '9');
1348 len
= cp
- name
+ 1;
1349 bcopy(name
, namebuf
, len
);
1350 namebuf
[len
] = '\0';
1352 * Now search all the interfaces for this name/number
1354 ifnet_head_lock_shared();
1355 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1356 if (strncmp(ifp
->if_name
, namebuf
, len
))
1358 if (unit
== ifp
->if_unit
)
1367 * Map interface name in a sockaddr_dl to
1368 * interface structure pointer.
1371 if_withname(struct sockaddr
*sa
)
1373 char ifname
[IFNAMSIZ
+1];
1374 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1376 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1377 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1381 * ifunit wants a null-terminated name. It may not be null-terminated
1382 * in the sockaddr. We don't want to change the caller's sockaddr,
1383 * and there might not be room to put the trailing null anyway, so we
1384 * make a local copy that we know we can null terminate safely.
1387 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1388 ifname
[sdl
->sdl_nlen
] = '\0';
1389 return (ifunit(ifname
));
1397 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1404 struct kev_msg ev_msg
;
1405 struct net_event_data ev_data
;
1407 bzero(&ev_data
, sizeof(struct net_event_data
));
1408 bzero(&ev_msg
, sizeof(struct kev_msg
));
1410 case OSIOCGIFCONF32
:
1411 case SIOCGIFCONF32
: {
1412 struct ifconf32
*ifc
= (struct ifconf32
*)data
;
1413 return (ifconf(cmd
, CAST_USER_ADDR_T(ifc
->ifc_req
),
1418 case OSIOCGIFCONF64
: {
1419 struct ifconf64
*ifc
= (struct ifconf64
*)data
;
1420 return (ifconf(cmd
, ifc
->ifc_req
, &ifc
->ifc_len
));
1424 ifr
= (struct ifreq
*)data
;
1428 error
= proc_suser(p
);
1431 return if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1432 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
);
1434 error
= proc_suser(p
);
1437 return if_clone_destroy(ifr
->ifr_name
);
1439 case SIOCIFGCLONERS32
: {
1440 struct if_clonereq32
*ifcr
= (struct if_clonereq32
*)data
;
1441 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1442 CAST_USER_ADDR_T(ifcr
->ifcru_buffer
)));
1446 case SIOCIFGCLONERS64
: {
1447 struct if_clonereq64
*ifcr
= (struct if_clonereq64
*)data
;
1448 return (if_clone_list(ifcr
->ifcr_count
, &ifcr
->ifcr_total
,
1449 ifcr
->ifcru_buffer
));
1452 #endif /* IF_CLONE_LIST */
1456 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
1457 * here to ensure that the ifnet, if found, has been fully attached.
1460 ifp
= ifunit(ifr
->ifr_name
);
1467 ifnet_lock_shared(ifp
);
1468 ifr
->ifr_flags
= ifp
->if_flags
;
1469 ifnet_lock_done(ifp
);
1473 ifnet_lock_shared(ifp
);
1474 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1475 ifr
->ifr_curcap
= ifp
->if_capenable
;
1476 ifnet_lock_done(ifp
);
1481 error
= mac_ifnet_label_get(kauth_cred_get(), ifr
, ifp
);
1485 ifnet_lock_shared(ifp
);
1486 ifr
->ifr_metric
= ifp
->if_metric
;
1487 ifnet_lock_done(ifp
);
1491 ifnet_lock_shared(ifp
);
1492 ifr
->ifr_mtu
= ifp
->if_mtu
;
1493 ifnet_lock_done(ifp
);
1497 ifnet_lock_shared(ifp
);
1498 ifr
->ifr_phys
= ifp
->if_physical
;
1499 ifnet_lock_done(ifp
);
1503 error
= proc_suser(p
);
1507 (void) ifnet_set_flags(ifp
, ifr
->ifr_flags
,
1508 (u_int16_t
)~IFF_CANTCHANGE
);
1511 * Note that we intentionally ignore any error from below
1512 * for the SIOCSIFFLAGS case.
1514 (void) ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1518 * Send the event even upon error from the driver because
1519 * we changed the flags.
1521 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1522 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1523 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1525 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1526 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1527 ev_data
.if_family
= ifp
->if_family
;
1528 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1529 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1530 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1531 ev_msg
.dv
[1].data_length
= 0;
1532 kev_post_msg(&ev_msg
);
1534 ifnet_touch_lastchange(ifp
);
1538 error
= proc_suser(p
);
1542 if ((ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)) {
1546 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1549 ifnet_touch_lastchange(ifp
);
1554 error
= mac_ifnet_label_set(kauth_cred_get(), ifr
, ifp
);
1558 error
= proc_suser(p
);
1562 ifp
->if_metric
= ifr
->ifr_metric
;
1564 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1565 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1566 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1568 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1569 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1570 ev_data
.if_family
= ifp
->if_family
;
1571 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1572 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1573 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1575 ev_msg
.dv
[1].data_length
= 0;
1576 kev_post_msg(&ev_msg
);
1578 ifnet_touch_lastchange(ifp
);
1582 error
= proc_suser(p
);
1586 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1591 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1592 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1593 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1595 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1596 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1597 ev_data
.if_family
= ifp
->if_family
;
1598 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1599 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1600 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1601 ev_msg
.dv
[1].data_length
= 0;
1602 kev_post_msg(&ev_msg
);
1604 ifnet_touch_lastchange(ifp
);
1609 u_int32_t oldmtu
= ifp
->if_mtu
;
1611 error
= proc_suser(p
);
1615 if (ifp
->if_ioctl
== NULL
) {
1619 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
1623 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1628 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1629 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1630 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1632 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1633 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1634 ev_data
.if_family
= ifp
->if_family
;
1635 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1636 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1637 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1638 ev_msg
.dv
[1].data_length
= 0;
1639 kev_post_msg(&ev_msg
);
1641 ifnet_touch_lastchange(ifp
);
1645 * If the link MTU changed, do network layer specific procedure
1646 * and update all route entries associated with the interface,
1647 * so that their MTU metric gets updated.
1649 if (ifp
->if_mtu
!= oldmtu
) {
1650 if_rtmtu_update(ifp
);
1660 error
= proc_suser(p
);
1664 /* Don't allow group membership on non-multicast interfaces. */
1665 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1670 /* Don't let users screw up protocols' entries. */
1671 if (ifr
->ifr_addr
.sa_family
!= AF_UNSPEC
&&
1672 ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
1678 * User is permitted to anonymously join a particular link
1679 * multicast group via SIOCADDMULTI. Subsequent join requested
1680 * for the same record which has an outstanding refcnt from a
1681 * past if_addmulti_anon() will not result in EADDRINUSE error
1682 * (unlike other BSDs.) Anonymously leaving a group is also
1683 * allowed only as long as there is an outstanding refcnt held
1684 * by a previous anonymous request, or else ENOENT (even if the
1685 * link-layer multicast membership exists for a network-layer
1688 if (cmd
== SIOCADDMULTI
) {
1689 error
= if_addmulti_anon(ifp
, &ifr
->ifr_addr
, NULL
);
1690 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1692 error
= if_delmulti_anon(ifp
, &ifr
->ifr_addr
);
1693 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1698 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1699 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1700 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1701 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1703 ev_data
.if_family
= ifp
->if_family
;
1704 ev_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
1705 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1706 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1707 ev_msg
.dv
[1].data_length
= 0;
1708 kev_post_msg(&ev_msg
);
1710 ifnet_touch_lastchange(ifp
);
1713 case SIOCSIFPHYADDR
:
1714 case SIOCDIFPHYADDR
:
1716 case SIOCSIFPHYADDR_IN6_32
:
1717 case SIOCSIFPHYADDR_IN6_64
:
1719 case SIOCSLIFPHYADDR
:
1721 case SIOCSIFGENERIC
:
1726 error
= proc_suser(p
);
1730 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1735 ifnet_touch_lastchange(ifp
);
1739 ifs
= (struct ifstat
*)data
;
1740 ifs
->ascii
[0] = '\0';
1742 case SIOCGIFPSRCADDR
:
1743 case SIOCGIFPDSTADDR
:
1744 case SIOCGLIFPHYADDR
:
1745 case SIOCGIFMEDIA32
:
1746 case SIOCGIFMEDIA64
:
1747 case SIOCGIFGENERIC
:
1749 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1755 error
= ifnet_ioctl(ifp
, so
->so_proto
->pr_domain
->dom_family
,
1759 case SIOCGIFWAKEFLAGS
:
1760 ifnet_lock_shared(ifp
);
1761 ifr
->ifr_wake_flags
= ifnet_get_wake_flags(ifp
);
1762 ifnet_lock_done(ifp
);
1765 case SIOCGIFGETRTREFCNT
:
1766 ifnet_lock_shared(ifp
);
1767 ifr
->ifr_route_refcnt
= ifp
->if_route_refcnt
;
1768 ifnet_lock_done(ifp
);
1772 oif_flags
= ifp
->if_flags
;
1773 if (so
->so_proto
== NULL
) {
1781 case SIOCSIFDSTADDR
:
1783 case SIOCSIFBRDADDR
:
1784 case SIOCSIFNETMASK
:
1785 #if BYTE_ORDER != BIG_ENDIAN
1786 if (ifr
->ifr_addr
.sa_family
== 0 &&
1787 ifr
->ifr_addr
.sa_len
< 16) {
1788 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1789 ifr
->ifr_addr
.sa_len
= 16;
1792 if (ifr
->ifr_addr
.sa_len
== 0)
1793 ifr
->ifr_addr
.sa_len
= 16;
1801 case OSIOCGIFDSTADDR
:
1802 cmd
= SIOCGIFDSTADDR
;
1805 case OSIOCGIFBRDADDR
:
1806 cmd
= SIOCGIFBRDADDR
;
1809 case OSIOCGIFNETMASK
:
1810 cmd
= SIOCGIFNETMASK
;
1814 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1816 socket_unlock(so
, 1);
1820 case OSIOCGIFDSTADDR
:
1821 case OSIOCGIFBRDADDR
:
1822 case OSIOCGIFNETMASK
:
1823 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1827 if (cmd
== SIOCSIFKPI
) {
1828 int temperr
= proc_suser(p
);
1833 if (error
== EOPNOTSUPP
|| error
== ENOTSUP
)
1834 error
= ifnet_ioctl(ifp
,
1835 so
->so_proto
->pr_domain
->dom_family
, cmd
, data
);
1843 ifioctllocked(struct socket
*so
, u_long cmd
, caddr_t data
, struct proc
*p
)
1847 socket_unlock(so
, 0);
1848 error
= ifioctl(so
, cmd
, data
, p
);
1854 * Set/clear promiscuous mode on interface ifp based on the truth value
1855 * of pswitch. The calls are reference counted so that only the first
1856 * "on" request actually has an effect, as does the final "off" request.
1857 * Results are undefined if the "off" and "on" requests are not matched.
1860 ifnet_set_promiscuous(
1868 ifnet_lock_exclusive(ifp
);
1869 oldflags
= ifp
->if_flags
;
1870 ifp
->if_pcount
+= pswitch
? 1 : -1;
1872 if (ifp
->if_pcount
> 0)
1873 ifp
->if_flags
|= IFF_PROMISC
;
1875 ifp
->if_flags
&= ~IFF_PROMISC
;
1877 newflags
= ifp
->if_flags
;
1878 ifnet_lock_done(ifp
);
1880 if (newflags
!= oldflags
&& (newflags
& IFF_UP
) != 0) {
1881 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
1885 ifnet_lock_exclusive(ifp
);
1887 ifp
->if_pcount
-= pswitch
? 1 : -1;
1888 if (ifp
->if_pcount
> 0)
1889 ifp
->if_flags
|= IFF_PROMISC
;
1891 ifp
->if_flags
&= ~IFF_PROMISC
;
1892 ifnet_lock_done(ifp
);
1896 if (newflags
!= oldflags
) {
1897 log(LOG_INFO
, "%s%d: promiscuous mode %s%s\n",
1898 ifp
->if_name
, ifp
->if_unit
,
1899 (newflags
& IFF_PROMISC
) != 0 ? "enable" : "disable",
1900 error
!= 0 ? " failed" : " succeeded");
1906 * Return interface configuration
1907 * of system. List may be used
1908 * in later ioctl's (above) to get
1909 * other information.
1913 ifconf(u_long cmd
, user_addr_t ifrp
, int * ret_space
)
1915 struct ifnet
*ifp
= NULL
;
1922 * Zero the ifr buffer to make sure we don't
1923 * disclose the contents of the stack.
1925 bzero(&ifr
, sizeof(struct ifreq
));
1928 ifnet_head_lock_shared();
1929 for (ifp
= ifnet_head
.tqh_first
; space
> sizeof(ifr
) &&
1930 ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1932 size_t ifnlen
, addrs
;
1934 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1935 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1936 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1937 error
= ENAMETOOLONG
;
1940 strlcpy(ifr
.ifr_name
, workbuf
, IFNAMSIZ
);
1943 ifnet_lock_shared(ifp
);
1946 ifa
= ifp
->if_addrhead
.tqh_first
;
1947 for ( ; space
> sizeof (ifr
) && ifa
;
1948 ifa
= ifa
->ifa_link
.tqe_next
) {
1949 struct sockaddr
*sa
;
1954 if (curproc
->p_prison
&& prison_if(curproc
, sa
)) {
1960 if (cmd
== OSIOCGIFCONF32
|| cmd
== OSIOCGIFCONF64
) {
1961 struct osockaddr
*osa
=
1962 (struct osockaddr
*)&ifr
.ifr_addr
;
1964 osa
->sa_family
= sa
->sa_family
;
1965 error
= copyout((caddr_t
)&ifr
, ifrp
,
1967 ifrp
+= sizeof(struct ifreq
);
1968 } else if (sa
->sa_len
<= sizeof(*sa
)) {
1970 error
= copyout((caddr_t
)&ifr
, ifrp
,
1972 ifrp
+= sizeof(struct ifreq
);
1975 sizeof (ifr
) + sa
->sa_len
- sizeof(*sa
)) {
1979 space
-= sa
->sa_len
- sizeof(*sa
);
1980 error
= copyout((caddr_t
)&ifr
, ifrp
,
1981 sizeof (ifr
.ifr_name
));
1983 error
= copyout((caddr_t
)sa
, (ifrp
+
1984 offsetof(struct ifreq
, ifr_addr
)),
1987 ifrp
+= (sa
->sa_len
+ offsetof(struct ifreq
,
1993 space
-= sizeof (ifr
);
1995 ifnet_lock_done(ifp
);
2000 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
2001 error
= copyout((caddr_t
)&ifr
, ifrp
, sizeof (ifr
));
2004 space
-= sizeof (ifr
);
2005 ifrp
+= sizeof(struct ifreq
);
2009 *ret_space
-= space
;
2014 * Just like if_promisc(), but for all-multicast-reception mode.
2017 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2022 ifnet_lock_exclusive(ifp
);
2025 if (ifp
->if_amcount
++ == 0) {
2026 ifp
->if_flags
|= IFF_ALLMULTI
;
2030 if (ifp
->if_amcount
> 1) {
2033 ifp
->if_amcount
= 0;
2034 ifp
->if_flags
&= ~IFF_ALLMULTI
;
2038 ifnet_lock_done(ifp
);
2041 error
= ifnet_ioctl(ifp
, 0, SIOCSIFFLAGS
, NULL
);
2048 static struct ifmultiaddr
*
2051 struct ifmultiaddr
*ifma
;
2053 ifma
= (how
== M_WAITOK
) ? zalloc(ifma_zone
) :
2054 zalloc_noblock(ifma_zone
);
2057 bzero(ifma
, ifma_size
);
2058 lck_mtx_init(&ifma
->ifma_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2059 ifma
->ifma_debug
|= IFD_ALLOC
;
2060 if (ifma_debug
!= 0) {
2061 ifma
->ifma_debug
|= IFD_DEBUG
;
2062 ifma
->ifma_trace
= ifma_trace
;
2069 ifma_free(struct ifmultiaddr
*ifma
)
2073 if (ifma
->ifma_protospec
!= NULL
) {
2074 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
2076 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2077 ifma
->ifma_anoncnt
!= 0) {
2078 panic("%s: Freeing ifma=%p with outstanding anon req",
2081 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
2082 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
2083 __func__
, ifma
, ifma
->ifma_ifp
);
2085 } else if (!(ifma
->ifma_debug
& IFD_ALLOC
)) {
2086 panic("%s: ifma %p cannot be freed", __func__
, ifma
);
2088 } else if (ifma
->ifma_refcount
!= 0) {
2089 panic("%s: non-zero refcount ifma=%p", __func__
, ifma
);
2091 } else if (ifma
->ifma_reqcnt
!= 0) {
2092 panic("%s: non-zero reqcnt ifma=%p", __func__
, ifma
);
2094 } else if (ifma
->ifma_ifp
!= NULL
) {
2095 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__
,
2096 ifma
->ifma_ifp
, ifma
);
2098 } else if (ifma
->ifma_ll
!= NULL
) {
2099 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__
,
2100 ifma
->ifma_ll
, ifma
);
2103 ifma
->ifma_debug
&= ~IFD_ALLOC
;
2104 if ((ifma
->ifma_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
2105 (IFD_DEBUG
| IFD_TRASHED
)) {
2106 lck_mtx_lock(&ifma_trash_lock
);
2107 TAILQ_REMOVE(&ifma_trash_head
, (struct ifmultiaddr_dbg
*)ifma
,
2109 lck_mtx_unlock(&ifma_trash_lock
);
2110 ifma
->ifma_debug
&= ~IFD_TRASHED
;
2114 if (ifma
->ifma_addr
!= NULL
) {
2115 FREE(ifma
->ifma_addr
, M_IFADDR
);
2116 ifma
->ifma_addr
= NULL
;
2118 lck_mtx_destroy(&ifma
->ifma_lock
, ifa_mtx_grp
);
2119 zfree(ifma_zone
, ifma
);
2123 ifma_trace(struct ifmultiaddr
*ifma
, int refhold
)
2125 struct ifmultiaddr_dbg
*ifma_dbg
= (struct ifmultiaddr_dbg
*)ifma
;
2130 if (!(ifma
->ifma_debug
& IFD_DEBUG
)) {
2131 panic("%s: ifma %p has no debug structure", __func__
, ifma
);
2135 cnt
= &ifma_dbg
->ifma_refhold_cnt
;
2136 tr
= ifma_dbg
->ifma_refhold
;
2138 cnt
= &ifma_dbg
->ifma_refrele_cnt
;
2139 tr
= ifma_dbg
->ifma_refrele
;
2142 idx
= atomic_add_16_ov(cnt
, 1) % IFMA_TRACE_HIST_SIZE
;
2143 ctrace_record(&tr
[idx
]);
2147 ifma_addref(struct ifmultiaddr
*ifma
, int locked
)
2152 IFMA_LOCK_ASSERT_HELD(ifma
);
2154 if (++ifma
->ifma_refcount
== 0) {
2155 panic("%s: ifma=%p wraparound refcnt", __func__
, ifma
);
2157 } else if (ifma
->ifma_trace
!= NULL
) {
2158 (*ifma
->ifma_trace
)(ifma
, TRUE
);
2165 ifma_remref(struct ifmultiaddr
*ifma
)
2167 struct ifmultiaddr
*ll
;
2171 if (ifma
->ifma_refcount
== 0) {
2172 panic("%s: ifma=%p negative refcnt", __func__
, ifma
);
2174 } else if (ifma
->ifma_trace
!= NULL
) {
2175 (*ifma
->ifma_trace
)(ifma
, FALSE
);
2178 --ifma
->ifma_refcount
;
2179 if (ifma
->ifma_refcount
> 0) {
2185 ifma
->ifma_ifp
= NULL
;
2186 ifma
->ifma_ll
= NULL
;
2188 ifma_free(ifma
); /* deallocate it */
2195 if_attach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
2197 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
2198 IFMA_LOCK_ASSERT_HELD(ifma
);
2200 if (ifma
->ifma_ifp
!= ifp
) {
2201 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__
,
2202 ifma
->ifma_ifp
, ifp
);
2204 } else if (ifma
->ifma_debug
& IFD_ATTACHED
) {
2205 panic("%s: Attempt to attach an already attached ifma=%p",
2208 } else if (anon
&& (ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2209 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__
, ifma
);
2211 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
2212 panic("%s: Attempt to reattach a detached ifma=%p",
2217 ifma
->ifma_reqcnt
++;
2218 VERIFY(ifma
->ifma_reqcnt
== 1);
2219 IFMA_ADDREF_LOCKED(ifma
);
2220 ifma
->ifma_debug
|= IFD_ATTACHED
;
2222 ifma
->ifma_anoncnt
++;
2223 VERIFY(ifma
->ifma_anoncnt
== 1);
2224 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
2227 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2231 if_detach_ifma(struct ifnet
*ifp
, struct ifmultiaddr
*ifma
, int anon
)
2233 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
2234 IFMA_LOCK_ASSERT_HELD(ifma
);
2236 if (ifma
->ifma_reqcnt
== 0) {
2237 panic("%s: ifma=%p negative reqcnt", __func__
, ifma
);
2239 } else if (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2240 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__
, ifma
);
2242 } else if (anon
&& ifma
->ifma_anoncnt
== 0) {
2243 panic("%s: ifma=%p negative anonreqcnt", __func__
, ifma
);
2245 } else if (ifma
->ifma_ifp
!= ifp
) {
2246 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__
,
2247 ifma
->ifma_ifp
, ifp
);
2252 --ifma
->ifma_anoncnt
;
2253 if (ifma
->ifma_anoncnt
> 0)
2255 ifma
->ifma_flags
&= ~IFMAF_ANONYMOUS
;
2258 --ifma
->ifma_reqcnt
;
2259 if (ifma
->ifma_reqcnt
> 0)
2262 if (ifma
->ifma_protospec
!= NULL
) {
2263 panic("%s: Protospec not NULL for ifma=%p", __func__
, ifma
);
2265 } else if ((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2266 ifma
->ifma_anoncnt
!= 0) {
2267 panic("%s: Detaching ifma=%p with outstanding anon req",
2270 } else if (!(ifma
->ifma_debug
& IFD_ATTACHED
)) {
2271 panic("%s: Attempt to detach an unattached address ifma=%p",
2274 } else if (ifma
->ifma_debug
& IFD_TRASHED
) {
2275 panic("%s: ifma %p is already in trash list", __func__
, ifma
);
2280 * NOTE: Caller calls IFMA_REMREF
2282 ifma
->ifma_debug
&= ~IFD_ATTACHED
;
2283 LIST_REMOVE(ifma
, ifma_link
);
2284 if (LIST_EMPTY(&ifp
->if_multiaddrs
))
2285 ifp
->if_updatemcasts
= 0;
2287 if (ifma
->ifma_debug
& IFD_DEBUG
) {
2288 /* Become a regular mutex, just in case */
2289 IFMA_CONVERT_LOCK(ifma
);
2290 lck_mtx_lock(&ifma_trash_lock
);
2291 TAILQ_INSERT_TAIL(&ifma_trash_head
,
2292 (struct ifmultiaddr_dbg
*)ifma
, ifma_trash_link
);
2293 lck_mtx_unlock(&ifma_trash_lock
);
2294 ifma
->ifma_debug
|= IFD_TRASHED
;
2301 * Find an ifmultiaddr that matches a socket address on an interface.
2303 * Caller is responsible for holding the ifnet_lock while calling
2307 if_addmulti_doesexist(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2308 struct ifmultiaddr
**retifma
, int anon
)
2310 struct ifmultiaddr
*ifma
;
2312 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
2313 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
2314 IFMA_LOCK_SPIN(ifma
);
2315 if (!equal(sa
, ifma
->ifma_addr
)) {
2320 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2321 ifma
->ifma_anoncnt
!= 0);
2322 VERIFY((ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2323 ifma
->ifma_anoncnt
== 0);
2324 ifma
->ifma_anoncnt
++;
2325 if (!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
)) {
2326 VERIFY(ifma
->ifma_anoncnt
== 1);
2327 ifma
->ifma_flags
|= IFMAF_ANONYMOUS
;
2330 if (!anon
|| ifma
->ifma_anoncnt
== 1) {
2331 ifma
->ifma_reqcnt
++;
2332 VERIFY(ifma
->ifma_reqcnt
> 1);
2334 if (retifma
!= NULL
) {
2336 IFMA_ADDREF_LOCKED(ifma
);
2345 * Radar 3642395, make sure all multicasts are in a standard format.
2347 static struct sockaddr
*
2349 const struct sockaddr
*original
)
2352 const u_char
*aptr
= NULL
;
2353 struct sockaddr
*copy
= NULL
;
2354 struct sockaddr_dl
*sdl_new
= NULL
;
2357 if (original
->sa_family
!= AF_LINK
&&
2358 original
->sa_family
!= AF_UNSPEC
) {
2359 /* Just make a copy */
2360 MALLOC(copy
, struct sockaddr
*, original
->sa_len
, M_IFADDR
, M_WAITOK
);
2362 bcopy(original
, copy
, original
->sa_len
);
2366 switch (original
->sa_family
) {
2368 const struct sockaddr_dl
*sdl_original
=
2369 (const struct sockaddr_dl
*)original
;
2371 if (sdl_original
->sdl_nlen
+ sdl_original
->sdl_alen
+ sdl_original
->sdl_slen
+
2372 offsetof(struct sockaddr_dl
, sdl_data
) > sdl_original
->sdl_len
)
2375 alen
= sdl_original
->sdl_alen
;
2376 aptr
= CONST_LLADDR(sdl_original
);
2381 if (original
->sa_len
< ETHER_ADDR_LEN
+
2382 offsetof(struct sockaddr
, sa_data
)) {
2386 alen
= ETHER_ADDR_LEN
;
2387 aptr
= (const u_char
*)original
->sa_data
;
2392 if (alen
== 0 || aptr
== NULL
)
2395 len
= alen
+ offsetof(struct sockaddr_dl
, sdl_data
);
2396 MALLOC(sdl_new
, struct sockaddr_dl
*, len
, M_IFADDR
, M_WAITOK
);
2398 if (sdl_new
!= NULL
) {
2399 bzero(sdl_new
, len
);
2400 sdl_new
->sdl_len
= len
;
2401 sdl_new
->sdl_family
= AF_LINK
;
2402 sdl_new
->sdl_alen
= alen
;
2403 bcopy(aptr
, LLADDR(sdl_new
), alen
);
2406 return (struct sockaddr
*)sdl_new
;
2410 * Network-layer protocol domains which hold references to the underlying
2411 * link-layer record must use this routine.
2414 if_addmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2415 struct ifmultiaddr
**retifma
)
2417 return (if_addmulti_common(ifp
, sa
, retifma
, 0));
2421 * Anything other than network-layer protocol domains which hold references
2422 * to the underlying link-layer record must use this routine: SIOCADDMULTI
2423 * ioctl, ifnet_add_multicast(), AppleTalk, if_bond.
2426 if_addmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2427 struct ifmultiaddr
**retifma
)
2429 return (if_addmulti_common(ifp
, sa
, retifma
, 1));
2433 * Register an additional multicast address with a network interface.
2435 * - If the address is already present, bump the reference count on the
2436 * address and return.
2437 * - If the address is not link-layer, look up a link layer address.
2438 * - Allocate address structures for one or both addresses, and attach to the
2439 * multicast address list on the interface. If automatically adding a link
2440 * layer address, the protocol address will own a reference to the link
2441 * layer address, to be freed when it is freed.
2442 * - Notify the network device driver of an addition to the multicast address
2445 * 'sa' points to caller-owned memory with the desired multicast address.
2447 * 'retifma' will be used to return a pointer to the resulting multicast
2448 * address reference, if desired.
2450 * 'anon' indicates a link-layer address with no protocol address reference
2451 * made to it. Anything other than network-layer protocol domain requests
2452 * are considered as anonymous.
2455 if_addmulti_common(struct ifnet
*ifp
, const struct sockaddr
*sa
,
2456 struct ifmultiaddr
**retifma
, int anon
)
2458 struct sockaddr_storage storage
;
2459 struct sockaddr
*llsa
= NULL
;
2460 struct sockaddr
*dupsa
= NULL
;
2461 int error
= 0, ll_firstref
= 0, lladdr
;
2462 struct ifmultiaddr
*ifma
= NULL
;
2463 struct ifmultiaddr
*llifma
= NULL
;
2465 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
2466 VERIFY(!anon
|| sa
->sa_family
== AF_UNSPEC
||
2467 sa
->sa_family
== AF_LINK
);
2469 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
2470 if (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
) {
2471 dupsa
= copy_and_normalize(sa
);
2472 if (dupsa
== NULL
) {
2479 ifnet_lock_exclusive(ifp
);
2480 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
2481 error
= EADDRNOTAVAIL
;
2482 ifnet_lock_done(ifp
);
2486 /* If the address is already present, return a new reference to it */
2487 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
2488 ifnet_lock_done(ifp
);
2493 * The address isn't already present; give the link layer a chance
2494 * to accept/reject it, and also find out which AF_LINK address this
2495 * maps to, if it isn't one already.
2497 error
= dlil_resolve_multi(ifp
, sa
, (struct sockaddr
*)&storage
,
2499 if (error
== 0 && storage
.ss_len
!= 0) {
2500 llsa
= copy_and_normalize((struct sockaddr
*)&storage
);
2506 llifma
= ifma_alloc(M_WAITOK
);
2507 if (llifma
== NULL
) {
2513 /* to be similar to FreeBSD */
2514 if (error
== EOPNOTSUPP
)
2516 else if (error
!= 0)
2519 /* Allocate while we aren't holding any locks */
2520 if (dupsa
== NULL
) {
2521 dupsa
= copy_and_normalize(sa
);
2522 if (dupsa
== NULL
) {
2527 ifma
= ifma_alloc(M_WAITOK
);
2533 ifnet_lock_exclusive(ifp
);
2535 * Check again for the matching multicast.
2537 error
= if_addmulti_doesexist(ifp
, sa
, retifma
, anon
);
2539 ifnet_lock_done(ifp
);
2543 if (llifma
!= NULL
) {
2544 VERIFY(!anon
); /* must not get here if "anonymous" */
2545 if (if_addmulti_doesexist(ifp
, llsa
, &ifma
->ifma_ll
, 0) == 0) {
2546 FREE(llsa
, M_IFADDR
);
2550 VERIFY(ifma
->ifma_ll
->ifma_ifp
== ifp
);
2553 llifma
->ifma_addr
= llsa
;
2554 llifma
->ifma_ifp
= ifp
;
2556 if_attach_ifma(ifp
, llifma
, 0);
2557 /* add extra refcnt for ifma */
2558 IFMA_ADDREF_LOCKED(llifma
);
2559 IFMA_UNLOCK(llifma
);
2560 ifma
->ifma_ll
= llifma
;
2564 /* "anonymous" request should not result in network address */
2565 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
2567 ifma
->ifma_addr
= dupsa
;
2568 ifma
->ifma_ifp
= ifp
;
2570 if_attach_ifma(ifp
, ifma
, anon
);
2571 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
2572 if (retifma
!= NULL
) {
2574 IFMA_ADDREF_LOCKED(*retifma
); /* for caller */
2576 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
2577 ifma
->ifma_addr
->sa_family
== AF_LINK
);
2579 ifnet_lock_done(ifp
);
2581 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
2582 IFMA_REMREF(ifma
); /* for this routine */
2585 * We are certain we have added something, so call down to the
2586 * interface to let them know about it. Do this only for newly-
2587 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
2589 if (lladdr
|| ll_firstref
)
2590 (void) ifnet_ioctl(ifp
, 0, SIOCADDMULTI
, NULL
);
2592 if (ifp
->if_updatemcasts
> 0)
2593 ifp
->if_updatemcasts
= 0;
2601 FREE(dupsa
, M_IFADDR
);
2605 FREE(llsa
, M_IFADDR
);
2611 * Delete a multicast group membership by network-layer group address.
2612 * This routine is deprecated.
2615 if_delmulti(struct ifnet
*ifp
, const struct sockaddr
*sa
)
2617 return (if_delmulti_common(NULL
, ifp
, sa
, 0));
2621 * Delete a multicast group membership by group membership pointer.
2622 * Network-layer protocol domains must use this routine.
2625 if_delmulti_ifma(struct ifmultiaddr
*ifma
)
2627 return (if_delmulti_common(ifma
, NULL
, NULL
, 0));
2631 * Anything other than network-layer protocol domains which hold references
2632 * to the underlying link-layer record must use this routine: SIOCDELMULTI
2633 * ioctl, ifnet_remove_multicast(), AppleTalk, if_bond.
2636 if_delmulti_anon(struct ifnet
*ifp
, const struct sockaddr
*sa
)
2638 return (if_delmulti_common(NULL
, ifp
, sa
, 1));
2642 * Delete a multicast group membership by network-layer group address.
2644 * Returns ENOENT if the entry could not be found.
2647 if_delmulti_common(struct ifmultiaddr
*ifma
, struct ifnet
*ifp
,
2648 const struct sockaddr
*sa
, int anon
)
2650 struct sockaddr
*dupsa
= NULL
;
2651 int lastref
, ll_lastref
= 0, lladdr
;
2652 struct ifmultiaddr
*ll
= NULL
;
2654 /* sanity check for callers */
2655 VERIFY(ifma
!= NULL
|| (ifp
!= NULL
&& sa
!= NULL
));
2658 ifp
= ifma
->ifma_ifp
;
2661 (sa
->sa_family
== AF_LINK
|| sa
->sa_family
== AF_UNSPEC
)) {
2662 dupsa
= copy_and_normalize(sa
);
2668 ifnet_lock_exclusive(ifp
);
2670 for (ifma
= LIST_FIRST(&ifp
->if_multiaddrs
); ifma
!= NULL
;
2671 ifma
= LIST_NEXT(ifma
, ifma_link
)) {
2673 if (!equal(sa
, ifma
->ifma_addr
) ||
2674 (anon
&& !(ifma
->ifma_flags
& IFMAF_ANONYMOUS
))) {
2675 VERIFY(!(ifma
->ifma_flags
& IFMAF_ANONYMOUS
) ||
2676 ifma
->ifma_anoncnt
!= 0);
2680 /* found; keep it locked */
2685 FREE(dupsa
, M_IFADDR
);
2686 ifnet_lock_done(ifp
);
2692 IFMA_LOCK_ASSERT_HELD(ifma
);
2693 IFMA_ADDREF_LOCKED(ifma
); /* for this routine */
2694 lastref
= if_detach_ifma(ifp
, ifma
, anon
);
2695 VERIFY(!lastref
|| (!(ifma
->ifma_debug
& IFD_ATTACHED
) &&
2696 ifma
->ifma_reqcnt
== 0));
2697 VERIFY(!anon
|| ifma
->ifma_ll
== NULL
);
2699 lladdr
= (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
||
2700 ifma
->ifma_addr
->sa_family
== AF_LINK
);
2702 if (lastref
&& ll
!= NULL
) {
2704 ll_lastref
= if_detach_ifma(ifp
, ll
, 0);
2707 ifnet_lock_done(ifp
);
2710 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2712 if ((ll
== NULL
&& lastref
&& lladdr
) || ll_lastref
) {
2714 * Make sure the interface driver is notified in the
2715 * case of a link layer mcast group being left. Do
2716 * this only for a AF_LINK/AF_UNSPEC address that has
2717 * been removed from the if_multiaddrs set.
2719 ifnet_ioctl(ifp
, 0, SIOCDELMULTI
, NULL
);
2723 IFMA_REMREF(ifma
); /* for if_multiaddrs list */
2725 IFMA_REMREF(ll
); /* for if_multiaddrs list */
2727 IFMA_REMREF(ifma
); /* for this routine */
2729 FREE(dupsa
, M_IFADDR
);
2735 * We don't use if_setlladdr, our interfaces are responsible for
2736 * handling the SIOCSIFLLADDR ioctl.
2740 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2746 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Link layers");
2747 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Generic link-management");
2751 * Shutdown all network activity. Used boot() when halting
2761 if (ifnet_list_get_all(IFNET_FAMILY_ANY
, &ifp
, &count
) == 0) {
2762 for (i
= 0; i
< count
; i
++) {
2764 dlil_proto_unplumb_all(ifp
[i
]);
2766 ifnet_list_free(ifp
);
2773 * Delete Routes for a Network Interface
2775 * Called for each routing entry via the rnh->rnh_walktree() call above
2776 * to delete all route entries referencing a detaching network interface.
2779 * rn pointer to node in the routing table
2780 * arg argument passed to rnh->rnh_walktree() - detaching interface
2784 * errno failed - reason indicated
2788 if_rtdel(struct radix_node
*rn
, void *arg
)
2790 struct rtentry
*rt
= (struct rtentry
*)rn
;
2791 struct ifnet
*ifp
= arg
;
2797 * Checking against RTF_UP protects against walktree
2798 * recursion problems with cloned routes.
2801 if (rt
->rt_ifp
== ifp
&& (rt
->rt_flags
& RTF_UP
)) {
2803 * Safe to drop rt_lock and use rt_key, rt_gateway,
2804 * since holding rnh_lock here prevents another thread
2805 * from calling rt_setgate() on this route.
2808 err
= rtrequest_locked(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
2809 rt_mask(rt
), rt
->rt_flags
, NULL
);
2811 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
2820 * Removes routing table reference to a given interface
2821 * for a given protocol family
2824 if_rtproto_del(struct ifnet
*ifp
, int protocol
)
2826 struct radix_node_head
*rnh
;
2829 routegenid_update();
2830 if ((protocol
<= AF_MAX
) && (protocol
>= 0) &&
2831 ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
)) {
2832 lck_mtx_lock(rnh_lock
);
2833 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
2834 lck_mtx_unlock(rnh_lock
);
2839 if_rtmtu(struct radix_node
*rn
, void *arg
)
2841 struct rtentry
*rt
= (struct rtentry
*)rn
;
2842 struct ifnet
*ifp
= arg
;
2845 if (rt
->rt_ifp
== ifp
) {
2847 * Update the MTU of this entry only if the MTU
2848 * has not been locked (RTV_MTU is not set) and
2849 * if it was non-zero to begin with.
2851 if (!(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) && rt
->rt_rmx
.rmx_mtu
)
2852 rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
2860 * Update the MTU metric of all route entries in all protocol tables
2861 * associated with a particular interface; this is called when the
2862 * MTU of that interface has changed.
2865 void if_rtmtu_update(struct ifnet
*ifp
)
2867 struct radix_node_head
*rnh
;
2870 for (p
= 0; p
< AF_MAX
+ 1; p
++) {
2871 if ((rnh
= rt_tables
[p
]) == NULL
)
2874 lck_mtx_lock(rnh_lock
);
2875 (void) rnh
->rnh_walktree(rnh
, if_rtmtu
, ifp
);
2876 lck_mtx_unlock(rnh_lock
);
2880 routegenid_update();
2883 __private_extern__
void
2884 if_data_internal_to_if_data(struct ifnet
*ifp
,
2885 const struct if_data_internal
*if_data_int
, struct if_data
*if_data
)
2888 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
2889 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
2890 /* compiler will cast down to 32-bit */
2891 #define COPYFIELD32_ATOMIC(fld) do { \
2892 atomic_get_64(if_data->fld, \
2893 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
2896 COPYFIELD(ifi_type
);
2897 COPYFIELD(ifi_typelen
);
2898 COPYFIELD(ifi_physical
);
2899 COPYFIELD(ifi_addrlen
);
2900 COPYFIELD(ifi_hdrlen
);
2901 COPYFIELD(ifi_recvquota
);
2902 COPYFIELD(ifi_xmitquota
);
2903 if_data
->ifi_unused1
= 0;
2905 COPYFIELD(ifi_metric
);
2906 if (if_data_int
->ifi_baudrate
& 0xFFFFFFFF00000000LL
) {
2907 if_data
->ifi_baudrate
= 0xFFFFFFFF;
2909 COPYFIELD32(ifi_baudrate
);
2912 COPYFIELD32_ATOMIC(ifi_ipackets
);
2913 COPYFIELD32_ATOMIC(ifi_ierrors
);
2914 COPYFIELD32_ATOMIC(ifi_opackets
);
2915 COPYFIELD32_ATOMIC(ifi_oerrors
);
2916 COPYFIELD32_ATOMIC(ifi_collisions
);
2917 COPYFIELD32_ATOMIC(ifi_ibytes
);
2918 COPYFIELD32_ATOMIC(ifi_obytes
);
2919 COPYFIELD32_ATOMIC(ifi_imcasts
);
2920 COPYFIELD32_ATOMIC(ifi_omcasts
);
2921 COPYFIELD32_ATOMIC(ifi_iqdrops
);
2922 COPYFIELD32_ATOMIC(ifi_noproto
);
2924 COPYFIELD(ifi_recvtiming
);
2925 COPYFIELD(ifi_xmittiming
);
2927 if_data
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2928 if_data
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2930 #if IF_LASTCHANGEUPTIME
2931 if_data
->ifi_lastchange
.tv_sec
+= boottime_sec();
2934 if_data
->ifi_unused2
= 0;
2935 COPYFIELD(ifi_hwassist
);
2936 if_data
->ifi_reserved1
= 0;
2937 if_data
->ifi_reserved2
= 0;
2938 #undef COPYFIELD32_ATOMIC
2943 __private_extern__
void
2944 if_data_internal_to_if_data64(struct ifnet
*ifp
,
2945 const struct if_data_internal
*if_data_int
,
2946 struct if_data64
*if_data64
)
2949 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
2950 #define COPYFIELD64_ATOMIC(fld) do { \
2951 atomic_get_64(if_data64->fld, \
2952 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
2955 COPYFIELD64(ifi_type
);
2956 COPYFIELD64(ifi_typelen
);
2957 COPYFIELD64(ifi_physical
);
2958 COPYFIELD64(ifi_addrlen
);
2959 COPYFIELD64(ifi_hdrlen
);
2960 COPYFIELD64(ifi_recvquota
);
2961 COPYFIELD64(ifi_xmitquota
);
2962 if_data64
->ifi_unused1
= 0;
2963 COPYFIELD64(ifi_mtu
);
2964 COPYFIELD64(ifi_metric
);
2965 COPYFIELD64(ifi_baudrate
);
2967 COPYFIELD64_ATOMIC(ifi_ipackets
);
2968 COPYFIELD64_ATOMIC(ifi_ierrors
);
2969 COPYFIELD64_ATOMIC(ifi_opackets
);
2970 COPYFIELD64_ATOMIC(ifi_oerrors
);
2971 COPYFIELD64_ATOMIC(ifi_collisions
);
2972 COPYFIELD64_ATOMIC(ifi_ibytes
);
2973 COPYFIELD64_ATOMIC(ifi_obytes
);
2974 COPYFIELD64_ATOMIC(ifi_imcasts
);
2975 COPYFIELD64_ATOMIC(ifi_omcasts
);
2976 COPYFIELD64_ATOMIC(ifi_iqdrops
);
2977 COPYFIELD64_ATOMIC(ifi_noproto
);
2979 /* Note these two fields are actually 32 bit, so doing COPYFIELD64_ATOMIC will
2980 * cause them to be misaligned
2982 COPYFIELD64(ifi_recvtiming
);
2983 COPYFIELD64(ifi_xmittiming
);
2985 if_data64
->ifi_lastchange
.tv_sec
= if_data_int
->ifi_lastchange
.tv_sec
;
2986 if_data64
->ifi_lastchange
.tv_usec
= if_data_int
->ifi_lastchange
.tv_usec
;
2988 #if IF_LASTCHANGEUPTIME
2989 if_data64
->ifi_lastchange
.tv_sec
+= boottime_sec();
2995 __private_extern__
void
2996 if_copy_traffic_class(struct ifnet
*ifp
,
2997 struct if_traffic_class
*if_tc
)
2999 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
3000 atomic_get_64(if_tc->fld, \
3001 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
3004 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets
);
3005 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes
);
3006 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets
);
3007 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes
);
3008 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets
);
3009 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes
);
3010 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets
);
3011 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes
);
3012 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets
);
3013 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes
);
3014 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets
);
3015 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes
);
3017 #undef COPY_IF_TC_FIELD64_ATOMIC
3022 ifa_remref(struct ifaddr
*ifa
, int locked
)
3027 IFA_LOCK_ASSERT_HELD(ifa
);
3029 if (ifa
->ifa_refcnt
== 0)
3030 panic("%s: ifa %p negative refcnt\n", __func__
, ifa
);
3031 else if (ifa
->ifa_trace
!= NULL
)
3032 (*ifa
->ifa_trace
)(ifa
, FALSE
);
3033 if (--ifa
->ifa_refcnt
== 0) {
3034 if (ifa
->ifa_debug
& IFD_ATTACHED
)
3035 panic("ifa %p attached to ifp is being freed\n", ifa
);
3037 * Some interface addresses are allocated either statically
3038 * or carved out of a larger block; e.g. AppleTalk addresses.
3039 * Only free it if it was allocated via MALLOC or via the
3040 * corresponding per-address family allocator. Otherwise,
3043 if (ifa
->ifa_debug
& IFD_ALLOC
) {
3044 if (ifa
->ifa_free
== NULL
) {
3046 FREE(ifa
, M_IFADDR
);
3048 /* Become a regular mutex */
3049 IFA_CONVERT_LOCK(ifa
);
3050 /* callee will unlock */
3051 (*ifa
->ifa_free
)(ifa
);
3059 if (!locked
&& ifa
!= NULL
)
3066 ifa_addref(struct ifaddr
*ifa
, int locked
)
3071 IFA_LOCK_ASSERT_HELD(ifa
);
3073 if (++ifa
->ifa_refcnt
== 0) {
3074 panic("%s: ifa %p wraparound refcnt\n", __func__
, ifa
);
3076 } else if (ifa
->ifa_trace
!= NULL
) {
3077 (*ifa
->ifa_trace
)(ifa
, TRUE
);
3084 ifa_lock_init(struct ifaddr
*ifa
)
3086 lck_mtx_init(&ifa
->ifa_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3090 ifa_lock_destroy(struct ifaddr
*ifa
)
3092 IFA_LOCK_ASSERT_NOTHELD(ifa
);
3093 lck_mtx_destroy(&ifa
->ifa_lock
, ifa_mtx_grp
);