2 * Copyright (c) 2000-2019 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) 1982, 1986, 1991, 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 * @(#)in.c 8.4 (Berkeley) 1/9/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sockio.h>
66 #include <sys/socketvar.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
72 #include <sys/kern_event.h>
73 #include <sys/syslog.h>
74 #include <sys/mcache.h>
75 #include <sys/protosw.h>
78 #include <kern/zalloc.h>
79 #include <pexpert/pexpert.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include <net/kpi_protocol.h>
87 #include <net/if_llatbl.h>
88 #include <net/if_arp.h>
90 #include <net/pfvar.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/igmp_var.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/tcp.h>
99 #include <netinet/tcp_timer.h>
100 #include <netinet/tcp_var.h>
101 #include <netinet/if_ether.h>
103 static int inctl_associd(struct socket
*, u_long
, caddr_t
);
104 static int inctl_connid(struct socket
*, u_long
, caddr_t
);
105 static int inctl_conninfo(struct socket
*, u_long
, caddr_t
);
106 static int inctl_autoaddr(struct ifnet
*, struct ifreq
*);
107 static int inctl_arpipll(struct ifnet
*, struct ifreq
*);
108 static int inctl_setrouter(struct ifnet
*, struct ifreq
*);
109 static int inctl_ifaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
111 static int inctl_ifdstaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
113 static int inctl_ifbrdaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
115 static int inctl_ifnetmask(struct ifnet
*, struct in_ifaddr
*, u_long
,
118 static void in_socktrim(struct sockaddr_in
*);
119 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
120 struct sockaddr_in
*, int);
122 #define IA_HASH_INIT(ia) { \
123 (ia)->ia_hash.tqe_next = (void *)(uintptr_t)-1; \
124 (ia)->ia_hash.tqe_prev = (void *)(uintptr_t)-1; \
127 #define IA_IS_HASHED(ia) \
128 (!((ia)->ia_hash.tqe_next == (void *)(uintptr_t)-1 || \
129 (ia)->ia_hash.tqe_prev == (void *)(uintptr_t)-1))
131 static void in_iahash_remove(struct in_ifaddr
*);
132 static void in_iahash_insert(struct in_ifaddr
*);
133 static void in_iahash_insert_ptp(struct in_ifaddr
*);
134 static struct in_ifaddr
*in_ifaddr_alloc(int);
135 static void in_ifaddr_attached(struct ifaddr
*);
136 static void in_ifaddr_detached(struct ifaddr
*);
137 static void in_ifaddr_free(struct ifaddr
*);
138 static void in_ifaddr_trace(struct ifaddr
*, int);
140 static int in_getassocids(struct socket
*, uint32_t *, user_addr_t
);
141 static int in_getconnids(struct socket
*, sae_associd_t
, uint32_t *, user_addr_t
);
143 /* IPv4 Layer 2 neighbor cache management routines */
144 static void in_lltable_destroy_lle_unlocked(struct llentry
*lle
);
145 static void in_lltable_destroy_lle(struct llentry
*lle
);
146 static struct llentry
*in_lltable_new(struct in_addr addr4
, u_int flags
);
147 static int in_lltable_match_prefix(const struct sockaddr
*saddr
,
148 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
);
149 static void in_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
);
150 static int in_lltable_rtcheck(struct ifnet
*ifp
, u_int flags
, const struct sockaddr
*l3addr
);
151 static inline uint32_t in_lltable_hash_dst(const struct in_addr dst
, uint32_t hsize
);
152 static uint32_t in_lltable_hash(const struct llentry
*lle
, uint32_t hsize
);
153 static void in_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
);
154 static inline struct llentry
* in_lltable_find_dst(struct lltable
*llt
, struct in_addr dst
);
155 static void in_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
);
156 static struct llentry
* in_lltable_alloc(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
);
157 static struct llentry
* in_lltable_lookup(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
);
158 static int in_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
, struct sysctl_req
*wr
);
159 static struct lltable
* in_lltattach(struct ifnet
*ifp
);
161 static int subnetsarelocal
= 0;
162 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
,
163 CTLFLAG_RW
| CTLFLAG_LOCKED
, &subnetsarelocal
, 0, "");
165 /* Track whether or not the SIOCARPIPLL ioctl has been called */
166 u_int32_t ipv4_ll_arp_aware
= 0;
168 #define INIFA_TRACE_HIST_SIZE 32 /* size of trace history */
171 __private_extern__
unsigned int inifa_trace_hist_size
= INIFA_TRACE_HIST_SIZE
;
173 struct in_ifaddr_dbg
{
174 struct in_ifaddr inifa
; /* in_ifaddr */
175 struct in_ifaddr inifa_old
; /* saved in_ifaddr */
176 u_int16_t inifa_refhold_cnt
; /* # of IFA_ADDREF */
177 u_int16_t inifa_refrele_cnt
; /* # of IFA_REMREF */
179 * Alloc and free callers.
181 ctrace_t inifa_alloc
;
184 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
186 ctrace_t inifa_refhold
[INIFA_TRACE_HIST_SIZE
];
187 ctrace_t inifa_refrele
[INIFA_TRACE_HIST_SIZE
];
191 TAILQ_ENTRY(in_ifaddr_dbg
) inifa_trash_link
;
194 /* List of trash in_ifaddr entries protected by inifa_trash_lock */
195 static TAILQ_HEAD(, in_ifaddr_dbg
) inifa_trash_head
;
196 static decl_lck_mtx_data(, inifa_trash_lock
);
199 static unsigned int inifa_debug
= 1; /* debugging (enabled) */
201 static unsigned int inifa_debug
; /* debugging (disabled) */
203 static unsigned int inifa_size
; /* size of zone element */
204 static struct zone
*inifa_zone
; /* zone for in_ifaddr */
206 #define INIFA_ZONE_MAX 64 /* maximum elements in zone */
207 #define INIFA_ZONE_NAME "in_ifaddr" /* zone name */
209 static const unsigned int in_extra_size
= sizeof(struct in_ifextra
);
210 static const unsigned int in_extra_bufsize
= in_extra_size
+
211 sizeof(void *) + sizeof(uint64_t);
214 * Return 1 if the address is
216 * - unicast or multicast link local
217 * - routed via a link level gateway
218 * - belongs to a directly connected (sub)net
221 inaddr_local(struct in_addr in
)
224 struct sockaddr_in sin
;
227 if (ntohl(in
.s_addr
) == INADDR_LOOPBACK
||
228 IN_LINKLOCAL(ntohl(in
.s_addr
))) {
230 } else if (ntohl(in
.s_addr
) >= INADDR_UNSPEC_GROUP
&&
231 ntohl(in
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
) {
234 sin
.sin_family
= AF_INET
;
235 sin
.sin_len
= sizeof(sin
);
237 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0);
241 if (rt
->rt_gateway
->sa_family
== AF_LINK
||
242 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
248 local
= in_localaddr(in
);
255 * Return 1 if an internet address is for a ``local'' host
256 * (one to which we have a connection). If subnetsarelocal
257 * is true, this includes other subnets of the local net,
258 * otherwise, it includes the directly-connected (sub)nets.
259 * The IPv4 link local prefix 169.254/16 is also included.
262 in_localaddr(struct in_addr in
)
264 u_int32_t i
= ntohl(in
.s_addr
);
265 struct in_ifaddr
*ia
;
267 if (IN_LINKLOCAL(i
)) {
271 if (subnetsarelocal
) {
272 lck_rw_lock_shared(in_ifaddr_rwlock
);
273 for (ia
= in_ifaddrhead
.tqh_first
; ia
!= NULL
;
274 ia
= ia
->ia_link
.tqe_next
) {
275 IFA_LOCK(&ia
->ia_ifa
);
276 if ((i
& ia
->ia_netmask
) == ia
->ia_net
) {
277 IFA_UNLOCK(&ia
->ia_ifa
);
278 lck_rw_done(in_ifaddr_rwlock
);
281 IFA_UNLOCK(&ia
->ia_ifa
);
283 lck_rw_done(in_ifaddr_rwlock
);
285 lck_rw_lock_shared(in_ifaddr_rwlock
);
286 for (ia
= in_ifaddrhead
.tqh_first
; ia
!= NULL
;
287 ia
= ia
->ia_link
.tqe_next
) {
288 IFA_LOCK(&ia
->ia_ifa
);
289 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
290 IFA_UNLOCK(&ia
->ia_ifa
);
291 lck_rw_done(in_ifaddr_rwlock
);
294 IFA_UNLOCK(&ia
->ia_ifa
);
296 lck_rw_done(in_ifaddr_rwlock
);
302 * Determine whether an IP address is in a reserved set of addresses
303 * that may not be forwarded, or whether datagrams to that destination
307 in_canforward(struct in_addr in
)
309 u_int32_t i
= ntohl(in
.s_addr
);
312 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
)) {
316 net
= i
& IN_CLASSA_NET
;
317 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
)) {
325 * Trim a mask in a sockaddr
328 in_socktrim(struct sockaddr_in
*ap
)
330 char *cplim
= (char *)&ap
->sin_addr
;
331 char *cp
= (char *)(&ap
->sin_addr
+ 1);
334 while (--cp
>= cplim
) {
336 (ap
)->sin_len
= cp
- (char *)(ap
) + 1;
342 static int in_interfaces
; /* number of external internet interfaces */
345 in_domifattach(struct ifnet
*ifp
)
351 if ((error
= proto_plumb(PF_INET
, ifp
)) && error
!= EEXIST
) {
352 log(LOG_ERR
, "%s: proto_plumb returned %d if=%s\n",
353 __func__
, error
, if_name(ifp
));
354 } else if (error
== 0 && ifp
->if_inetdata
== NULL
) {
356 struct in_ifextra
*ext
;
359 if ((ext
= (struct in_ifextra
*)_MALLOC(in_extra_bufsize
,
360 M_IFADDR
, M_WAITOK
| M_ZERO
)) == NULL
) {
362 errorx
= proto_unplumb(PF_INET
, ifp
);
365 "%s: proto_unplumb returned %d if=%s%d\n",
366 __func__
, errorx
, ifp
->if_name
,
372 /* Align on 64-bit boundary */
373 base
= (void *)P2ROUNDUP((intptr_t)ext
+ sizeof(uint64_t),
375 VERIFY(((intptr_t)base
+ in_extra_size
) <=
376 ((intptr_t)ext
+ in_extra_bufsize
));
377 pbuf
= (void **)((intptr_t)base
- sizeof(void *));
379 ifp
->if_inetdata
= base
;
380 IN_IFEXTRA(ifp
)->ii_llt
= in_lltattach(ifp
);
381 VERIFY(IS_P2ALIGNED(ifp
->if_inetdata
, sizeof(uint64_t)));
384 if (error
== 0 && ifp
->if_inetdata
!= NULL
) {
386 * Since the structure is never freed, we need to
387 * zero out its contents to avoid reusing stale data.
388 * A little redundant with allocation above, but it
389 * keeps the code simpler for all cases.
391 bzero(ifp
->if_inetdata
, in_extra_size
);
396 static __attribute__((noinline
)) int
397 inctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
401 struct so_aidreq32 a32
;
402 struct so_aidreq64 a64
;
408 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
409 bcopy(data
, &u
.a32
, sizeof(u
.a32
));
410 error
= in_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
412 bcopy(&u
.a32
, data
, sizeof(u
.a32
));
416 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
417 bcopy(data
, &u
.a64
, sizeof(u
.a64
));
418 error
= in_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
420 bcopy(&u
.a64
, data
, sizeof(u
.a64
));
432 static __attribute__((noinline
)) int
433 inctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
437 struct so_cidreq32 c32
;
438 struct so_cidreq64 c64
;
444 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
445 bcopy(data
, &u
.c32
, sizeof(u
.c32
));
446 error
= in_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
449 bcopy(&u
.c32
, data
, sizeof(u
.c32
));
453 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
454 bcopy(data
, &u
.c64
, sizeof(u
.c64
));
455 error
= in_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
458 bcopy(&u
.c64
, data
, sizeof(u
.c64
));
470 static __attribute__((noinline
)) int
471 inctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
475 struct so_cinforeq32 ci32
;
476 struct so_cinforeq64 ci64
;
482 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
483 bcopy(data
, &u
.ci32
, sizeof(u
.ci32
));
484 error
= in_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
485 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
486 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
487 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
488 &u
.ci32
.scir_aux_len
);
490 bcopy(&u
.ci32
, data
, sizeof(u
.ci32
));
494 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
495 bcopy(data
, &u
.ci64
, sizeof(u
.ci64
));
496 error
= in_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
497 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
498 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
499 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
500 &u
.ci64
.scir_aux_len
);
502 bcopy(&u
.ci64
, data
, sizeof(u
.ci64
));
515 * Caller passes in the ioctl data pointer directly via "ifr", with the
516 * expectation that this routine always uses bcopy() or other byte-aligned
519 static __attribute__((noinline
)) int
520 inctl_autoaddr(struct ifnet
*ifp
, struct ifreq
*ifr
)
522 int error
= 0, intval
;
526 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
528 ifnet_lock_exclusive(ifp
);
531 * An interface in IPv4 router mode implies that it
532 * is configured with a static IP address and should
533 * not act as a DHCP client; prevent SIOCAUTOADDR from
534 * being set in that mode.
536 if (ifp
->if_eflags
& IFEF_IPV4_ROUTER
) {
537 intval
= 0; /* be safe; clear flag if set */
540 ifp
->if_eflags
|= IFEF_AUTOCONFIGURING
;
544 ifp
->if_eflags
&= ~IFEF_AUTOCONFIGURING
;
546 ifnet_lock_done(ifp
);
552 * Caller passes in the ioctl data pointer directly via "ifr", with the
553 * expectation that this routine always uses bcopy() or other byte-aligned
556 static __attribute__((noinline
)) int
557 inctl_arpipll(struct ifnet
*ifp
, struct ifreq
*ifr
)
559 int error
= 0, intval
;
563 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
564 ipv4_ll_arp_aware
= 1;
566 ifnet_lock_exclusive(ifp
);
569 * An interface in IPv4 router mode implies that it
570 * is configured with a static IP address and should
571 * not have to deal with IPv4 Link-Local Address;
572 * prevent SIOCARPIPLL from being set in that mode.
574 if (ifp
->if_eflags
& IFEF_IPV4_ROUTER
) {
575 intval
= 0; /* be safe; clear flag if set */
578 ifp
->if_eflags
|= IFEF_ARPLL
;
582 ifp
->if_eflags
&= ~IFEF_ARPLL
;
584 ifnet_lock_done(ifp
);
590 * Handle SIOCSETROUTERMODE to set or clear the IPv4 router mode flag on
591 * the interface. When in this mode, IPv4 Link-Local Address support is
592 * disabled in ARP, and DHCP client support is disabled in IP input; turning
593 * any of them on would cause an error to be returned. Entering or exiting
594 * this mode will result in the removal of IPv4 addresses currently configured
597 * Caller passes in the ioctl data pointer directly via "ifr", with the
598 * expectation that this routine always uses bcopy() or other byte-aligned
601 static __attribute__((noinline
)) int
602 inctl_setrouter(struct ifnet
*ifp
, struct ifreq
*ifr
)
604 int error
= 0, intval
;
608 /* Router mode isn't valid for loopback */
609 if (ifp
->if_flags
& IFF_LOOPBACK
) {
613 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
615 ifnet_lock_exclusive(ifp
);
617 ifp
->if_eflags
|= IFEF_IPV4_ROUTER
;
618 ifp
->if_eflags
&= ~(IFEF_ARPLL
| IFEF_AUTOCONFIGURING
);
620 ifp
->if_eflags
&= ~IFEF_IPV4_ROUTER
;
622 ifnet_lock_done(ifp
);
624 /* purge all IPv4 addresses configured on this interface */
631 * Caller passes in the ioctl data pointer directly via "ifr", with the
632 * expectation that this routine always uses bcopy() or other byte-aligned
635 static __attribute__((noinline
)) int
636 inctl_ifaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
639 struct kev_in_data in_event_data
;
640 struct kev_msg ev_msg
;
641 struct sockaddr_in addr
;
647 bzero(&in_event_data
, sizeof(struct kev_in_data
));
648 bzero(&ev_msg
, sizeof(struct kev_msg
));
651 case SIOCGIFADDR
: /* struct ifreq */
653 error
= EADDRNOTAVAIL
;
656 IFA_LOCK(&ia
->ia_ifa
);
657 bcopy(&ia
->ia_addr
, &ifr
->ifr_addr
, sizeof(addr
));
658 IFA_UNLOCK(&ia
->ia_ifa
);
661 case SIOCSIFADDR
: /* struct ifreq */
663 bcopy(&ifr
->ifr_addr
, &addr
, sizeof(addr
));
665 * If this is a new address, the reference count for the
666 * hash table has been taken at creation time above.
668 error
= in_ifinit(ifp
, ia
, &addr
, 1);
670 (void) ifnet_notify_address(ifp
, AF_INET
);
674 case SIOCAIFADDR
: { /* struct {if,in_}aliasreq */
675 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)ifr
;
676 struct sockaddr_in broadaddr
, mask
;
677 int hostIsNew
, maskIsNew
;
680 bcopy(&ifra
->ifra_addr
, &addr
, sizeof(addr
));
681 bcopy(&ifra
->ifra_broadaddr
, &broadaddr
, sizeof(broadaddr
));
682 bcopy(&ifra
->ifra_mask
, &mask
, sizeof(mask
));
688 IFA_LOCK(&ia
->ia_ifa
);
689 if (ia
->ia_addr
.sin_family
== AF_INET
) {
690 if (addr
.sin_len
== 0) {
693 } else if (addr
.sin_addr
.s_addr
==
694 ia
->ia_addr
.sin_addr
.s_addr
) {
699 IFA_UNLOCK(&ia
->ia_ifa
);
700 in_ifscrub(ifp
, ia
, 0);
701 IFA_LOCK(&ia
->ia_ifa
);
702 ia
->ia_sockmask
= mask
;
704 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
707 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
708 (broadaddr
.sin_family
== AF_INET
)) {
709 IFA_UNLOCK(&ia
->ia_ifa
);
710 in_ifscrub(ifp
, ia
, 0);
711 IFA_LOCK(&ia
->ia_ifa
);
712 ia
->ia_dstaddr
= broadaddr
;
713 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
714 maskIsNew
= 1; /* We lie; but the effect's the same */
716 if (addr
.sin_family
== AF_INET
&& (hostIsNew
|| maskIsNew
)) {
717 IFA_UNLOCK(&ia
->ia_ifa
);
718 error
= in_ifinit(ifp
, ia
, &addr
, 0);
720 IFA_UNLOCK(&ia
->ia_ifa
);
723 (void) ifnet_notify_address(ifp
, AF_INET
);
725 IFA_LOCK(&ia
->ia_ifa
);
726 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
727 (broadaddr
.sin_family
== AF_INET
)) {
728 ia
->ia_broadaddr
= broadaddr
;
734 if ((error
== 0) || (error
== EEXIST
)) {
735 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
736 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
737 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
740 ev_msg
.event_code
= KEV_INET_NEW_ADDR
;
742 ev_msg
.event_code
= KEV_INET_CHANGED_ADDR
;
745 if (ia
->ia_ifa
.ifa_dstaddr
) {
746 in_event_data
.ia_dstaddr
=
747 ((struct sockaddr_in
*)(void *)ia
->
748 ia_ifa
.ifa_dstaddr
)->sin_addr
;
750 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
752 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
753 in_event_data
.ia_net
= ia
->ia_net
;
754 in_event_data
.ia_netmask
= ia
->ia_netmask
;
755 in_event_data
.ia_subnet
= ia
->ia_subnet
;
756 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
757 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
758 IFA_UNLOCK(&ia
->ia_ifa
);
759 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
760 ifp
->if_name
, IFNAMSIZ
);
761 in_event_data
.link_data
.if_family
= ifp
->if_family
;
762 in_event_data
.link_data
.if_unit
= ifp
->if_unit
;
764 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
765 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
766 ev_msg
.dv
[1].data_length
= 0;
768 dlil_post_complete_msg(ifp
, &ev_msg
);
770 IFA_UNLOCK(&ia
->ia_ifa
);
775 case SIOCDIFADDR
: /* struct ifreq */
777 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCDIFADDR
, ia
);
778 if (error
== EOPNOTSUPP
) {
785 /* Fill out the kernel event information */
786 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
787 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
788 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
790 ev_msg
.event_code
= KEV_INET_ADDR_DELETED
;
792 IFA_LOCK(&ia
->ia_ifa
);
793 if (ia
->ia_ifa
.ifa_dstaddr
) {
794 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
795 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
797 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
799 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
800 in_event_data
.ia_net
= ia
->ia_net
;
801 in_event_data
.ia_netmask
= ia
->ia_netmask
;
802 in_event_data
.ia_subnet
= ia
->ia_subnet
;
803 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
804 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
805 IFA_UNLOCK(&ia
->ia_ifa
);
806 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
807 ifp
->if_name
, IFNAMSIZ
);
808 in_event_data
.link_data
.if_family
= ifp
->if_family
;
809 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
811 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
812 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
813 ev_msg
.dv
[1].data_length
= 0;
816 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
817 /* Release ia_link reference */
819 TAILQ_REMOVE(&in_ifaddrhead
, ia
, ia_link
);
821 if (IA_IS_HASHED(ia
)) {
822 in_iahash_remove(ia
);
825 lck_rw_done(in_ifaddr_rwlock
);
828 * in_ifscrub kills the interface route.
830 in_ifscrub(ifp
, ia
, 0);
831 ifnet_lock_exclusive(ifp
);
833 /* if_detach_ifa() releases ifa_link reference */
834 if_detach_ifa(ifp
, ifa
);
835 /* Our reference to this address is dropped at the bottom */
838 /* invalidate route caches */
839 routegenid_inet_update();
842 * If the interface supports multicast, and no address is left,
843 * remove the "all hosts" multicast group from that interface.
845 if ((ifp
->if_flags
& IFF_MULTICAST
) ||
846 ifp
->if_allhostsinm
!= NULL
) {
847 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
849 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
855 ifnet_lock_done(ifp
);
857 lck_mtx_lock(&ifp
->if_addrconfig_lock
);
858 if (ifa
== NULL
&& ifp
->if_allhostsinm
!= NULL
) {
859 struct in_multi
*inm
= ifp
->if_allhostsinm
;
860 ifp
->if_allhostsinm
= NULL
;
863 /* release the reference for allhostsinm */
866 lck_mtx_unlock(&ifp
->if_addrconfig_lock
);
868 ifnet_lock_done(ifp
);
871 /* Post the kernel event */
872 dlil_post_complete_msg(ifp
, &ev_msg
);
875 * See if there is any IPV4 address left and if so,
876 * reconfigure KDP to use current primary address.
878 ifa
= ifa_ifpgetprimary(ifp
, AF_INET
);
881 * NOTE: SIOCSIFADDR is defined with struct ifreq
882 * as parameter, but here we are sending it down
883 * to the interface with a pointer to struct ifaddr,
884 * for legacy reasons.
886 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFADDR
, ifa
);
887 if (error
== EOPNOTSUPP
) {
891 /* Release reference from ifa_ifpgetprimary() */
894 (void) ifnet_notify_address(ifp
, AF_INET
);
906 * Caller passes in the ioctl data pointer directly via "ifr", with the
907 * expectation that this routine always uses bcopy() or other byte-aligned
910 static __attribute__((noinline
)) int
911 inctl_ifdstaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
914 struct kev_in_data in_event_data
;
915 struct kev_msg ev_msg
;
916 struct sockaddr_in dstaddr
;
921 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
925 bzero(&in_event_data
, sizeof(struct kev_in_data
));
926 bzero(&ev_msg
, sizeof(struct kev_msg
));
929 case SIOCGIFDSTADDR
: /* struct ifreq */
931 error
= EADDRNOTAVAIL
;
934 IFA_LOCK(&ia
->ia_ifa
);
935 bcopy(&ia
->ia_dstaddr
, &ifr
->ifr_dstaddr
, sizeof(dstaddr
));
936 IFA_UNLOCK(&ia
->ia_ifa
);
939 case SIOCSIFDSTADDR
: /* struct ifreq */
941 IFA_LOCK(&ia
->ia_ifa
);
942 dstaddr
= ia
->ia_dstaddr
;
943 bcopy(&ifr
->ifr_dstaddr
, &ia
->ia_dstaddr
, sizeof(dstaddr
));
944 if (ia
->ia_dstaddr
.sin_family
== AF_INET
) {
945 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
947 IFA_UNLOCK(&ia
->ia_ifa
);
949 * NOTE: SIOCSIFDSTADDR is defined with struct ifreq
950 * as parameter, but here we are sending it down
951 * to the interface with a pointer to struct ifaddr,
952 * for legacy reasons.
954 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFDSTADDR
, ia
);
955 IFA_LOCK(&ia
->ia_ifa
);
956 if (error
== EOPNOTSUPP
) {
960 ia
->ia_dstaddr
= dstaddr
;
961 IFA_UNLOCK(&ia
->ia_ifa
);
964 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
966 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
967 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
968 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
970 ev_msg
.event_code
= KEV_INET_SIFDSTADDR
;
972 if (ia
->ia_ifa
.ifa_dstaddr
) {
973 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
974 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
976 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
979 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
980 in_event_data
.ia_net
= ia
->ia_net
;
981 in_event_data
.ia_netmask
= ia
->ia_netmask
;
982 in_event_data
.ia_subnet
= ia
->ia_subnet
;
983 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
984 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
985 IFA_UNLOCK(&ia
->ia_ifa
);
986 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
987 ifp
->if_name
, IFNAMSIZ
);
988 in_event_data
.link_data
.if_family
= ifp
->if_family
;
989 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
991 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
992 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
993 ev_msg
.dv
[1].data_length
= 0;
995 dlil_post_complete_msg(ifp
, &ev_msg
);
997 lck_mtx_lock(rnh_lock
);
998 IFA_LOCK(&ia
->ia_ifa
);
999 if (ia
->ia_flags
& IFA_ROUTE
) {
1000 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&dstaddr
;
1001 IFA_UNLOCK(&ia
->ia_ifa
);
1002 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
1003 IFA_LOCK(&ia
->ia_ifa
);
1004 ia
->ia_ifa
.ifa_dstaddr
=
1005 (struct sockaddr
*)&ia
->ia_dstaddr
;
1006 IFA_UNLOCK(&ia
->ia_ifa
);
1007 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_ADD
,
1010 IFA_UNLOCK(&ia
->ia_ifa
);
1012 lck_mtx_unlock(rnh_lock
);
1026 * Caller passes in the ioctl data pointer directly via "ifr", with the
1027 * expectation that this routine always uses bcopy() or other byte-aligned
1030 static __attribute__((noinline
)) int
1031 inctl_ifbrdaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
1034 struct kev_in_data in_event_data
;
1035 struct kev_msg ev_msg
;
1038 VERIFY(ifp
!= NULL
);
1041 return EADDRNOTAVAIL
;
1044 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
1048 bzero(&in_event_data
, sizeof(struct kev_in_data
));
1049 bzero(&ev_msg
, sizeof(struct kev_msg
));
1052 case SIOCGIFBRDADDR
: /* struct ifreq */
1053 IFA_LOCK(&ia
->ia_ifa
);
1054 bcopy(&ia
->ia_broadaddr
, &ifr
->ifr_broadaddr
,
1055 sizeof(struct sockaddr_in
));
1056 IFA_UNLOCK(&ia
->ia_ifa
);
1059 case SIOCSIFBRDADDR
: /* struct ifreq */
1060 IFA_LOCK(&ia
->ia_ifa
);
1061 bcopy(&ifr
->ifr_broadaddr
, &ia
->ia_broadaddr
,
1062 sizeof(struct sockaddr_in
));
1064 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1065 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1066 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1068 ev_msg
.event_code
= KEV_INET_SIFBRDADDR
;
1070 if (ia
->ia_ifa
.ifa_dstaddr
) {
1071 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
1072 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
1074 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
1076 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
1077 in_event_data
.ia_net
= ia
->ia_net
;
1078 in_event_data
.ia_netmask
= ia
->ia_netmask
;
1079 in_event_data
.ia_subnet
= ia
->ia_subnet
;
1080 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
1081 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
1082 IFA_UNLOCK(&ia
->ia_ifa
);
1083 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
1084 ifp
->if_name
, IFNAMSIZ
);
1085 in_event_data
.link_data
.if_family
= ifp
->if_family
;
1086 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
1088 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
1089 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
1090 ev_msg
.dv
[1].data_length
= 0;
1092 dlil_post_complete_msg(ifp
, &ev_msg
);
1104 * Caller passes in the ioctl data pointer directly via "ifr", with the
1105 * expectation that this routine always uses bcopy() or other byte-aligned
1108 static __attribute__((noinline
)) int
1109 inctl_ifnetmask(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
1112 struct kev_in_data in_event_data
;
1113 struct kev_msg ev_msg
;
1114 struct sockaddr_in mask
;
1117 VERIFY(ifp
!= NULL
);
1119 bzero(&in_event_data
, sizeof(struct kev_in_data
));
1120 bzero(&ev_msg
, sizeof(struct kev_msg
));
1123 case SIOCGIFNETMASK
: /* struct ifreq */
1125 error
= EADDRNOTAVAIL
;
1128 IFA_LOCK(&ia
->ia_ifa
);
1129 bcopy(&ia
->ia_sockmask
, &ifr
->ifr_addr
, sizeof(mask
));
1130 IFA_UNLOCK(&ia
->ia_ifa
);
1133 case SIOCSIFNETMASK
: { /* struct ifreq */
1136 bcopy(&ifr
->ifr_addr
, &mask
, sizeof(mask
));
1137 i
= mask
.sin_addr
.s_addr
;
1140 IFA_LOCK(&ia
->ia_ifa
);
1141 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
= i
);
1142 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1143 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1144 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1146 ev_msg
.event_code
= KEV_INET_SIFNETMASK
;
1148 if (ia
->ia_ifa
.ifa_dstaddr
) {
1149 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
1150 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
1152 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
1154 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
1155 in_event_data
.ia_net
= ia
->ia_net
;
1156 in_event_data
.ia_netmask
= ia
->ia_netmask
;
1157 in_event_data
.ia_subnet
= ia
->ia_subnet
;
1158 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
1159 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
1160 IFA_UNLOCK(&ia
->ia_ifa
);
1161 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
1162 ifp
->if_name
, IFNAMSIZ
);
1163 in_event_data
.link_data
.if_family
= ifp
->if_family
;
1164 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
1166 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
1167 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
1168 ev_msg
.dv
[1].data_length
= 0;
1170 dlil_post_complete_msg(ifp
, &ev_msg
);
1183 * Generic INET control operations (ioctl's).
1185 * ifp is NULL if not an interface-specific ioctl.
1187 * Most of the routines called to handle the ioctls would end up being
1188 * tail-call optimized, which unfortunately causes this routine to
1189 * consume too much stack space; this is the reason for the "noinline"
1190 * attribute used on those routines.
1192 * If called directly from within the networking stack (as opposed to via
1193 * pru_control), the socket parameter may be NULL.
1196 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1199 struct ifreq
*ifr
= (struct ifreq
*)(void *)data
;
1200 struct sockaddr_in addr
, dstaddr
;
1201 struct sockaddr_in sin
, *sa
= NULL
;
1202 boolean_t privileged
= (proc_suser(p
) == 0);
1203 boolean_t so_unlocked
= FALSE
;
1204 struct in_ifaddr
*ia
= NULL
;
1208 /* In case it's NULL, make sure it came from the kernel */
1209 VERIFY(so
!= NULL
|| p
== kernproc
);
1212 * ioctls which don't require ifp, but require socket.
1215 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1216 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1217 return inctl_associd(so
, cmd
, data
);
1220 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1221 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1222 return inctl_connid(so
, cmd
, data
);
1225 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1226 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1227 return inctl_conninfo(so
, cmd
, data
);
1232 * The rest of ioctls require ifp; reject if we don't have one;
1233 * return ENXIO to be consistent with ifioctl().
1240 * ioctls which require ifp but not interface address.
1243 case SIOCAUTOADDR
: /* struct ifreq */
1247 return inctl_autoaddr(ifp
, ifr
);
1250 case SIOCARPIPLL
: /* struct ifreq */
1254 return inctl_arpipll(ifp
, ifr
);
1257 case SIOCSETROUTERMODE
: /* struct ifreq */
1261 return inctl_setrouter(ifp
, ifr
);
1264 case SIOCPROTOATTACH
: /* struct ifreq */
1268 return in_domifattach(ifp
);
1271 case SIOCPROTODETACH
: /* struct ifreq */
1277 * If an IPv4 address is still present, refuse to detach.
1279 ifnet_lock_shared(ifp
);
1280 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1282 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
1288 ifnet_lock_done(ifp
);
1289 return (ifa
== NULL
) ? proto_unplumb(PF_INET
, ifp
) : EBUSY
;
1294 * ioctls which require interface address; obtain sockaddr_in.
1297 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1301 bcopy(&((struct in_aliasreq
*)(void *)data
)->ifra_addr
,
1306 case SIOCDIFADDR
: /* struct ifreq */
1307 case SIOCSIFADDR
: /* struct ifreq */
1308 case SIOCSIFDSTADDR
: /* struct ifreq */
1309 case SIOCSIFNETMASK
: /* struct ifreq */
1310 case SIOCSIFBRDADDR
: /* struct ifreq */
1315 case SIOCGIFADDR
: /* struct ifreq */
1316 case SIOCGIFDSTADDR
: /* struct ifreq */
1317 case SIOCGIFNETMASK
: /* struct ifreq */
1318 case SIOCGIFBRDADDR
: /* struct ifreq */
1319 bcopy(&ifr
->ifr_addr
, &sin
, sizeof(sin
));
1325 * Find address for this interface, if it exists.
1327 * If an alias address was specified, find that one instead of
1328 * the first one on the interface, if possible.
1332 struct in_ifaddr
*iap
;
1335 * Any failures from this point on must take into account
1336 * a non-NULL "ia" with an outstanding reference count, and
1337 * therefore requires IFA_REMREF. Jump to "done" label
1338 * instead of calling return if "ia" is valid.
1340 lck_rw_lock_shared(in_ifaddr_rwlock
);
1341 TAILQ_FOREACH(iap
, INADDR_HASH(sa
->sin_addr
.s_addr
), ia_hash
) {
1342 IFA_LOCK(&iap
->ia_ifa
);
1343 if (iap
->ia_ifp
== ifp
&&
1344 iap
->ia_addr
.sin_addr
.s_addr
==
1345 sa
->sin_addr
.s_addr
) {
1347 IFA_ADDREF_LOCKED(&iap
->ia_ifa
);
1348 IFA_UNLOCK(&iap
->ia_ifa
);
1351 IFA_UNLOCK(&iap
->ia_ifa
);
1353 lck_rw_done(in_ifaddr_rwlock
);
1356 ifnet_lock_shared(ifp
);
1357 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1359 IFA_LOCK(&iap
->ia_ifa
);
1360 if (iap
->ia_addr
.sin_family
== AF_INET
) {
1362 IFA_ADDREF_LOCKED(&iap
->ia_ifa
);
1363 IFA_UNLOCK(&iap
->ia_ifa
);
1366 IFA_UNLOCK(&iap
->ia_ifa
);
1368 ifnet_lock_done(ifp
);
1373 * Unlock the socket since ifnet_ioctl() may be invoked by
1374 * one of the ioctl handlers below. Socket will be re-locked
1375 * prior to returning.
1378 socket_unlock(so
, 0);
1383 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1384 case SIOCDIFADDR
: /* struct ifreq */
1385 if (cmd
== SIOCAIFADDR
) {
1386 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1387 ifra_addr
, &addr
, sizeof(addr
));
1388 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1389 ifra_dstaddr
, &dstaddr
, sizeof(dstaddr
));
1391 VERIFY(cmd
== SIOCDIFADDR
);
1392 bcopy(&((struct ifreq
*)(void *)data
)->ifr_addr
,
1393 &addr
, sizeof(addr
));
1394 bzero(&dstaddr
, sizeof(dstaddr
));
1397 if (addr
.sin_family
== AF_INET
) {
1398 struct in_ifaddr
*oia
;
1400 lck_rw_lock_shared(in_ifaddr_rwlock
);
1401 for (oia
= ia
; ia
; ia
= ia
->ia_link
.tqe_next
) {
1402 IFA_LOCK(&ia
->ia_ifa
);
1403 if (ia
->ia_ifp
== ifp
&&
1404 ia
->ia_addr
.sin_addr
.s_addr
==
1405 addr
.sin_addr
.s_addr
) {
1406 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1407 IFA_UNLOCK(&ia
->ia_ifa
);
1410 IFA_UNLOCK(&ia
->ia_ifa
);
1412 lck_rw_done(in_ifaddr_rwlock
);
1414 IFA_REMREF(&oia
->ia_ifa
);
1416 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
1417 (cmd
== SIOCAIFADDR
) &&
1418 (dstaddr
.sin_addr
.s_addr
== INADDR_ANY
)) {
1419 error
= EDESTADDRREQ
;
1422 } else if (cmd
== SIOCAIFADDR
) {
1426 if (cmd
== SIOCDIFADDR
) {
1428 error
= EADDRNOTAVAIL
;
1432 IFA_LOCK(&ia
->ia_ifa
);
1434 * Avoid the race condition seen when two
1435 * threads process SIOCDIFADDR command
1438 while (ia
->ia_ifa
.ifa_debug
& IFD_DETACHING
) {
1439 os_log(OS_LOG_DEFAULT
,
1440 "Another thread is already attempting to "
1441 "delete IPv4 address: %s on interface %s. "
1442 "Go to sleep and check again after the operation is done",
1443 inet_ntoa(sa
->sin_addr
), ia
->ia_ifp
->if_xname
);
1444 ia
->ia_ifa
.ifa_del_waiters
++;
1445 (void) msleep(ia
->ia_ifa
.ifa_del_wc
, &ia
->ia_ifa
.ifa_lock
, (PZERO
- 1),
1447 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1450 if ((ia
->ia_ifa
.ifa_debug
& IFD_ATTACHED
) == 0) {
1451 error
= EADDRNOTAVAIL
;
1452 IFA_UNLOCK(&ia
->ia_ifa
);
1456 ia
->ia_ifa
.ifa_debug
|= IFD_DETACHING
;
1457 IFA_UNLOCK(&ia
->ia_ifa
);
1461 case SIOCSIFADDR
: /* struct ifreq */
1462 case SIOCSIFDSTADDR
: /* struct ifreq */
1463 case SIOCSIFNETMASK
: /* struct ifreq */
1464 if (cmd
== SIOCAIFADDR
) {
1465 /* fell thru from above; just repeat it */
1466 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1467 ifra_addr
, &addr
, sizeof(addr
));
1469 VERIFY(cmd
== SIOCDIFADDR
|| cmd
== SIOCSIFADDR
||
1470 cmd
== SIOCSIFNETMASK
|| cmd
== SIOCSIFDSTADDR
);
1471 bcopy(&((struct ifreq
*)(void *)data
)->ifr_addr
,
1472 &addr
, sizeof(addr
));
1475 if (addr
.sin_family
!= AF_INET
&& cmd
== SIOCSIFADDR
) {
1480 ia
= in_ifaddr_alloc(M_WAITOK
);
1485 ifnet_lock_exclusive(ifp
);
1488 /* Hold a reference for this routine */
1489 IFA_ADDREF_LOCKED(ifa
);
1491 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1492 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
1493 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
1494 ia
->ia_sockmask
.sin_len
= 8;
1495 if (ifp
->if_flags
& IFF_BROADCAST
) {
1496 ia
->ia_broadaddr
.sin_len
= sizeof(ia
->ia_addr
);
1497 ia
->ia_broadaddr
.sin_family
= AF_INET
;
1500 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
1503 /* if_attach_ifa() holds a reference for ifa_link */
1504 if_attach_ifa(ifp
, ifa
);
1506 * If we have to go through in_ifinit(), make sure
1507 * to avoid installing route(s) based on this address
1508 * via PFC_IFUP event, before the link resolver (ARP)
1511 if (cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) {
1512 ifa
->ifa_debug
|= IFD_NOTREADY
;
1515 ifnet_lock_done(ifp
);
1516 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1517 /* Hold a reference for ia_link */
1519 TAILQ_INSERT_TAIL(&in_ifaddrhead
, ia
, ia_link
);
1520 lck_rw_done(in_ifaddr_rwlock
);
1522 (void) in_domifattach(ifp
);
1529 case SIOCGIFDSTADDR
: /* struct ifreq */
1530 case SIOCSIFDSTADDR
: /* struct ifreq */
1531 error
= inctl_ifdstaddr(ifp
, ia
, cmd
, ifr
);
1534 case SIOCGIFBRDADDR
: /* struct ifreq */
1535 case SIOCSIFBRDADDR
: /* struct ifreq */
1536 error
= inctl_ifbrdaddr(ifp
, ia
, cmd
, ifr
);
1539 case SIOCGIFNETMASK
: /* struct ifreq */
1540 case SIOCSIFNETMASK
: /* struct ifreq */
1541 error
= inctl_ifnetmask(ifp
, ia
, cmd
, ifr
);
1544 case SIOCGIFADDR
: /* struct ifreq */
1545 case SIOCSIFADDR
: /* struct ifreq */
1546 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1547 case SIOCDIFADDR
: /* struct ifreq */
1548 error
= inctl_ifaddr(ifp
, ia
, cmd
, ifr
);
1558 if (cmd
== SIOCDIFADDR
) {
1559 IFA_LOCK(&ia
->ia_ifa
);
1560 ia
->ia_ifa
.ifa_debug
&= ~IFD_DETACHING
;
1561 if (ia
->ia_ifa
.ifa_del_waiters
> 0) {
1562 ia
->ia_ifa
.ifa_del_waiters
= 0;
1563 wakeup(ia
->ia_ifa
.ifa_del_wc
);
1565 IFA_UNLOCK(&ia
->ia_ifa
);
1567 IFA_REMREF(&ia
->ia_ifa
);
1577 * Delete any existing route for an interface.
1580 in_ifscrub(struct ifnet
*ifp
, struct in_ifaddr
*ia
, int locked
)
1582 IFA_LOCK(&ia
->ia_ifa
);
1583 if ((ia
->ia_flags
& IFA_ROUTE
) == 0) {
1584 IFA_UNLOCK(&ia
->ia_ifa
);
1587 IFA_UNLOCK(&ia
->ia_ifa
);
1589 lck_mtx_lock(rnh_lock
);
1591 if (ifp
->if_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) {
1592 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
1594 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, 0);
1596 IFA_LOCK(&ia
->ia_ifa
);
1597 ia
->ia_flags
&= ~IFA_ROUTE
;
1598 IFA_UNLOCK(&ia
->ia_ifa
);
1600 lck_mtx_unlock(rnh_lock
);
1605 * Caller must hold in_ifaddr_rwlock as writer.
1608 in_iahash_remove(struct in_ifaddr
*ia
)
1610 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1611 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1613 if (!IA_IS_HASHED(ia
)) {
1614 panic("attempt to remove wrong ia %p from hash table\n", ia
);
1617 TAILQ_REMOVE(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
), ia
, ia_hash
);
1619 if (IFA_REMREF_LOCKED(&ia
->ia_ifa
) == NULL
) {
1620 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
,
1627 * Caller must hold in_ifaddr_rwlock as writer.
1630 in_iahash_insert(struct in_ifaddr
*ia
)
1632 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1633 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1635 if (ia
->ia_addr
.sin_family
!= AF_INET
) {
1636 panic("attempt to insert wrong ia %p into hash table\n", ia
);
1638 } else if (IA_IS_HASHED(ia
)) {
1639 panic("attempt to double-insert ia %p into hash table\n", ia
);
1642 TAILQ_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1644 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1648 * Some point to point interfaces that are tunnels borrow the address from
1649 * an underlying interface (e.g. VPN server). In order for source address
1650 * selection logic to find the underlying interface first, we add the address
1651 * of borrowing point to point interfaces at the end of the list.
1652 * (see rdar://6733789)
1654 * Caller must hold in_ifaddr_rwlock as writer.
1657 in_iahash_insert_ptp(struct in_ifaddr
*ia
)
1659 struct in_ifaddr
*tmp_ifa
;
1660 struct ifnet
*tmp_ifp
;
1662 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1663 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1665 if (ia
->ia_addr
.sin_family
!= AF_INET
) {
1666 panic("attempt to insert wrong ia %p into hash table\n", ia
);
1668 } else if (IA_IS_HASHED(ia
)) {
1669 panic("attempt to double-insert ia %p into hash table\n", ia
);
1672 IFA_UNLOCK(&ia
->ia_ifa
);
1673 TAILQ_FOREACH(tmp_ifa
, INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1675 IFA_LOCK(&tmp_ifa
->ia_ifa
);
1676 /* ia->ia_addr won't change, so check without lock */
1677 if (IA_SIN(tmp_ifa
)->sin_addr
.s_addr
==
1678 ia
->ia_addr
.sin_addr
.s_addr
) {
1679 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
1682 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
1684 tmp_ifp
= (tmp_ifa
== NULL
) ? NULL
: tmp_ifa
->ia_ifp
;
1686 IFA_LOCK(&ia
->ia_ifa
);
1687 if (tmp_ifp
== NULL
) {
1688 TAILQ_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1691 TAILQ_INSERT_TAIL(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1694 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1698 * Initialize an interface's internet address
1699 * and routing table entry.
1702 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
, struct sockaddr_in
*sin
,
1705 u_int32_t i
= ntohl(sin
->sin_addr
.s_addr
);
1706 struct sockaddr_in oldaddr
;
1707 int flags
= RTF_UP
, error
;
1708 struct ifaddr
*ifa0
;
1712 /* Take an extra reference for this routine */
1713 IFA_ADDREF(&ia
->ia_ifa
);
1715 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1716 IFA_LOCK(&ia
->ia_ifa
);
1717 oldaddr
= ia
->ia_addr
;
1718 if (IA_IS_HASHED(ia
)) {
1720 in_iahash_remove(ia
);
1724 * Interface addresses should not contain port or sin_zero information.
1726 SIN(&ia
->ia_addr
)->sin_family
= AF_INET
;
1727 SIN(&ia
->ia_addr
)->sin_len
= sizeof(struct sockaddr_in
);
1728 SIN(&ia
->ia_addr
)->sin_port
= 0;
1729 bzero(&SIN(&ia
->ia_addr
)->sin_zero
, sizeof(sin
->sin_zero
));
1730 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1731 in_iahash_insert_ptp(ia
);
1733 in_iahash_insert(ia
);
1735 IFA_UNLOCK(&ia
->ia_ifa
);
1736 lck_rw_done(in_ifaddr_rwlock
);
1739 * Give the interface a chance to initialize if this is its first
1740 * address, and to validate the address if necessary. Send down
1741 * SIOCSIFADDR for first address, and SIOCAIFADDR for alias(es).
1742 * We find the first IPV4 address assigned to it and check if this
1743 * is the same as the one passed into this routine.
1745 ifa0
= ifa_ifpgetprimary(ifp
, AF_INET
);
1746 cmd
= (&ia
->ia_ifa
== ifa0
) ? SIOCSIFADDR
: SIOCAIFADDR
;
1747 error
= ifnet_ioctl(ifp
, PF_INET
, cmd
, ia
);
1748 if (error
== EOPNOTSUPP
) {
1752 * If we've just sent down SIOCAIFADDR, send another ioctl down
1753 * for SIOCSIFADDR for the first IPV4 address of the interface,
1754 * because an address change on one of the addresses will result
1755 * in the removal of the previous first IPV4 address. KDP needs
1756 * be reconfigured with the current primary IPV4 address.
1758 if (error
== 0 && cmd
== SIOCAIFADDR
) {
1760 * NOTE: SIOCSIFADDR is defined with struct ifreq
1761 * as parameter, but here we are sending it down
1762 * to the interface with a pointer to struct ifaddr,
1763 * for legacy reasons.
1765 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFADDR
, ifa0
);
1766 if (error
== EOPNOTSUPP
) {
1771 /* Release reference from ifa_ifpgetprimary() */
1775 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1776 IFA_LOCK(&ia
->ia_ifa
);
1777 if (IA_IS_HASHED(ia
)) {
1778 in_iahash_remove(ia
);
1780 ia
->ia_addr
= oldaddr
;
1782 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1783 in_iahash_insert_ptp(ia
);
1785 in_iahash_insert(ia
);
1788 IFA_UNLOCK(&ia
->ia_ifa
);
1789 lck_rw_done(in_ifaddr_rwlock
);
1790 /* Release extra reference taken above */
1791 IFA_REMREF(&ia
->ia_ifa
);
1794 lck_mtx_lock(rnh_lock
);
1795 IFA_LOCK(&ia
->ia_ifa
);
1797 * Address has been initialized by the link resolver (ARP)
1798 * via ifnet_ioctl() above; it may now generate route(s).
1800 ia
->ia_ifa
.ifa_debug
&= ~IFD_NOTREADY
;
1802 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
1803 IFA_UNLOCK(&ia
->ia_ifa
);
1804 in_ifscrub(ifp
, ia
, 1);
1805 IFA_LOCK(&ia
->ia_ifa
);
1806 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1808 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1810 ia
->ia_netmask
= IN_CLASSA_NET
;
1811 } else if (IN_CLASSB(i
)) {
1812 ia
->ia_netmask
= IN_CLASSB_NET
;
1814 ia
->ia_netmask
= IN_CLASSC_NET
;
1817 * The subnet mask usually includes at least the standard network part,
1818 * but may may be smaller in the case of supernetting.
1819 * If it is set, we believe it.
1821 if (ia
->ia_subnetmask
== 0) {
1822 ia
->ia_subnetmask
= ia
->ia_netmask
;
1823 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1825 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1827 ia
->ia_net
= i
& ia
->ia_netmask
;
1828 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1829 in_socktrim(&ia
->ia_sockmask
);
1831 * Add route for the network.
1833 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1834 if (ifp
->if_flags
& IFF_BROADCAST
) {
1835 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1836 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1837 ia
->ia_netbroadcast
.s_addr
=
1838 htonl(ia
->ia_net
| ~ia
->ia_netmask
);
1839 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1840 ia
->ia_ifa
.ifa_dstaddr
= ia
->ia_ifa
.ifa_addr
;
1842 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1843 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
) {
1844 IFA_UNLOCK(&ia
->ia_ifa
);
1845 lck_mtx_unlock(rnh_lock
);
1846 /* Release extra reference taken above */
1847 IFA_REMREF(&ia
->ia_ifa
);
1850 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
1853 IFA_UNLOCK(&ia
->ia_ifa
);
1855 if ((error
= rtinit_locked(&(ia
->ia_ifa
), (int)RTM_ADD
, flags
)) == 0) {
1856 IFA_LOCK(&ia
->ia_ifa
);
1857 ia
->ia_flags
|= IFA_ROUTE
;
1858 IFA_UNLOCK(&ia
->ia_ifa
);
1860 lck_mtx_unlock(rnh_lock
);
1862 /* XXX check if the subnet route points to the same interface */
1863 if (error
== EEXIST
) {
1868 * If the interface supports multicast, join the "all hosts"
1869 * multicast group on that interface.
1871 if (ifp
->if_flags
& IFF_MULTICAST
) {
1872 struct in_addr addr
;
1874 lck_mtx_lock(&ifp
->if_addrconfig_lock
);
1875 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1876 if (ifp
->if_allhostsinm
== NULL
) {
1877 struct in_multi
*inm
;
1878 inm
= in_addmulti(&addr
, ifp
);
1882 * Keep the reference on inm added by
1883 * in_addmulti above for storing the
1884 * pointer in allhostsinm.
1886 ifp
->if_allhostsinm
= inm
;
1888 printf("%s: failed to add membership to "
1889 "all-hosts multicast address on %s\n",
1890 __func__
, if_name(ifp
));
1893 lck_mtx_unlock(&ifp
->if_addrconfig_lock
);
1896 /* Release extra reference taken above */
1897 IFA_REMREF(&ia
->ia_ifa
);
1900 /* invalidate route caches */
1901 routegenid_inet_update();
1908 * Return TRUE if the address might be a local broadcast address.
1911 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
1916 if (in
.s_addr
== INADDR_BROADCAST
|| in
.s_addr
== INADDR_ANY
) {
1919 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
1922 t
= ntohl(in
.s_addr
);
1925 * Look through the list of addresses for a match
1926 * with a broadcast address.
1928 #define ia ((struct in_ifaddr *)ifa)
1929 ifnet_lock_shared(ifp
);
1930 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1932 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1933 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1934 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1936 * Check for old-style (host 0) broadcast.
1938 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1940 * Check for an all one subnetmask. These
1941 * only exist when an interface gets a secondary
1944 ia
->ia_subnetmask
!= (u_int32_t
)0xffffffff) {
1946 ifnet_lock_done(ifp
);
1951 ifnet_lock_done(ifp
);
1957 in_purgeaddrs(struct ifnet
*ifp
)
1959 struct ifaddr
**ifap
;
1962 VERIFY(ifp
!= NULL
);
1965 * Be nice, and try the civilized way first. If we can't get
1966 * rid of them this way, then do it the rough way. We must
1967 * only get here during detach time, after the ifnet has been
1968 * removed from the global list and arrays.
1970 err
= ifnet_get_address_list_family_internal(ifp
, &ifap
, AF_INET
, 1,
1972 if (err
== 0 && ifap
!= NULL
) {
1975 bzero(&ifr
, sizeof(ifr
));
1976 (void) snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
),
1977 "%s", if_name(ifp
));
1979 for (i
= 0; ifap
[i
] != NULL
; i
++) {
1984 bcopy(ifa
->ifa_addr
, &ifr
.ifr_addr
,
1985 sizeof(struct sockaddr_in
));
1987 err
= in_control(NULL
, SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
1989 /* if we lost the race, ignore it */
1990 if (err
== EADDRNOTAVAIL
) {
1994 char s_addr
[MAX_IPv4_STR_LEN
];
1995 char s_dstaddr
[MAX_IPv4_STR_LEN
];
1996 struct in_addr
*s
, *d
;
1999 s
= &((struct sockaddr_in
*)
2000 (void *)ifa
->ifa_addr
)->sin_addr
;
2001 d
= &((struct sockaddr_in
*)
2002 (void *)ifa
->ifa_dstaddr
)->sin_addr
;
2003 (void) inet_ntop(AF_INET
, &s
->s_addr
, s_addr
,
2005 (void) inet_ntop(AF_INET
, &d
->s_addr
, s_dstaddr
,
2009 printf("%s: SIOCDIFADDR ifp=%s ifa_addr=%s "
2010 "ifa_dstaddr=%s (err=%d)\n", __func__
,
2011 ifp
->if_xname
, s_addr
, s_dstaddr
, err
);
2014 ifnet_free_address_list(ifap
);
2015 } else if (err
!= 0 && err
!= ENXIO
) {
2016 printf("%s: error retrieving list of AF_INET addresses for "
2017 "ifp=%s (err=%d)\n", __func__
, ifp
->if_xname
, err
);
2022 * Called as part of ip_init
2025 in_ifaddr_init(void)
2029 PE_parse_boot_argn("ifa_debug", &inifa_debug
, sizeof(inifa_debug
));
2031 inifa_size
= (inifa_debug
== 0) ? sizeof(struct in_ifaddr
) :
2032 sizeof(struct in_ifaddr_dbg
);
2034 inifa_zone
= zinit(inifa_size
, INIFA_ZONE_MAX
* inifa_size
,
2035 0, INIFA_ZONE_NAME
);
2036 if (inifa_zone
== NULL
) {
2037 panic("%s: failed allocating %s", __func__
, INIFA_ZONE_NAME
);
2040 zone_change(inifa_zone
, Z_EXPAND
, TRUE
);
2041 zone_change(inifa_zone
, Z_CALLERACCT
, FALSE
);
2043 lck_mtx_init(&inifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2044 TAILQ_INIT(&inifa_trash_head
);
2047 static struct in_ifaddr
*
2048 in_ifaddr_alloc(int how
)
2050 struct in_ifaddr
*inifa
;
2052 inifa
= (how
== M_WAITOK
) ? zalloc(inifa_zone
) :
2053 zalloc_noblock(inifa_zone
);
2054 if (inifa
!= NULL
) {
2055 bzero(inifa
, inifa_size
);
2056 inifa
->ia_ifa
.ifa_free
= in_ifaddr_free
;
2057 inifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
2058 inifa
->ia_ifa
.ifa_del_wc
= &inifa
->ia_ifa
.ifa_debug
;
2059 inifa
->ia_ifa
.ifa_del_waiters
= 0;
2060 ifa_lock_init(&inifa
->ia_ifa
);
2061 if (inifa_debug
!= 0) {
2062 struct in_ifaddr_dbg
*inifa_dbg
=
2063 (struct in_ifaddr_dbg
*)inifa
;
2064 inifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
2065 inifa
->ia_ifa
.ifa_trace
= in_ifaddr_trace
;
2066 inifa
->ia_ifa
.ifa_attached
= in_ifaddr_attached
;
2067 inifa
->ia_ifa
.ifa_detached
= in_ifaddr_detached
;
2068 ctrace_record(&inifa_dbg
->inifa_alloc
);
2075 in_ifaddr_free(struct ifaddr
*ifa
)
2077 IFA_LOCK_ASSERT_HELD(ifa
);
2079 if (ifa
->ifa_refcnt
!= 0) {
2080 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
2083 if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
2084 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
2087 if (ifa
->ifa_debug
& IFD_DEBUG
) {
2088 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2089 ctrace_record(&inifa_dbg
->inifa_free
);
2090 bcopy(&inifa_dbg
->inifa
, &inifa_dbg
->inifa_old
,
2091 sizeof(struct in_ifaddr
));
2092 if (ifa
->ifa_debug
& IFD_TRASHED
) {
2093 /* Become a regular mutex, just in case */
2094 IFA_CONVERT_LOCK(ifa
);
2095 lck_mtx_lock(&inifa_trash_lock
);
2096 TAILQ_REMOVE(&inifa_trash_head
, inifa_dbg
,
2098 lck_mtx_unlock(&inifa_trash_lock
);
2099 ifa
->ifa_debug
&= ~IFD_TRASHED
;
2103 ifa_lock_destroy(ifa
);
2104 bzero(ifa
, sizeof(struct in_ifaddr
));
2105 zfree(inifa_zone
, ifa
);
2109 in_ifaddr_attached(struct ifaddr
*ifa
)
2111 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2113 IFA_LOCK_ASSERT_HELD(ifa
);
2115 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
2116 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
2119 if (ifa
->ifa_debug
& IFD_TRASHED
) {
2120 /* Become a regular mutex, just in case */
2121 IFA_CONVERT_LOCK(ifa
);
2122 lck_mtx_lock(&inifa_trash_lock
);
2123 TAILQ_REMOVE(&inifa_trash_head
, inifa_dbg
, inifa_trash_link
);
2124 lck_mtx_unlock(&inifa_trash_lock
);
2125 ifa
->ifa_debug
&= ~IFD_TRASHED
;
2130 in_ifaddr_detached(struct ifaddr
*ifa
)
2132 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2134 IFA_LOCK_ASSERT_HELD(ifa
);
2136 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
2137 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
2139 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
2140 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
2143 ifa
->ifa_debug
|= IFD_TRASHED
;
2144 /* Become a regular mutex, just in case */
2145 IFA_CONVERT_LOCK(ifa
);
2146 lck_mtx_lock(&inifa_trash_lock
);
2147 TAILQ_INSERT_TAIL(&inifa_trash_head
, inifa_dbg
, inifa_trash_link
);
2148 lck_mtx_unlock(&inifa_trash_lock
);
2152 in_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
2154 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2159 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
2160 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
2164 cnt
= &inifa_dbg
->inifa_refhold_cnt
;
2165 tr
= inifa_dbg
->inifa_refhold
;
2167 cnt
= &inifa_dbg
->inifa_refrele_cnt
;
2168 tr
= inifa_dbg
->inifa_refrele
;
2171 idx
= atomic_add_16_ov(cnt
, 1) % INIFA_TRACE_HIST_SIZE
;
2172 ctrace_record(&tr
[idx
]);
2176 * Handle SIOCGASSOCIDS ioctl for PF_INET domain.
2179 in_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
2181 struct inpcb
*inp
= sotoinpcb(so
);
2184 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
2188 /* INPCB has no concept of association */
2189 aid
= SAE_ASSOCID_ANY
;
2192 /* just asking how many there are? */
2193 if (aidp
== USER_ADDR_NULL
) {
2197 return copyout(&aid
, aidp
, sizeof(aid
));
2201 * Handle SIOCGCONNIDS ioctl for PF_INET domain.
2204 in_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
2207 struct inpcb
*inp
= sotoinpcb(so
);
2210 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
2214 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
2218 /* if connected, return 1 connection count */
2219 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
2221 /* just asking how many there are? */
2222 if (cidp
== USER_ADDR_NULL
) {
2226 /* if INPCB is connected, assign it connid 1 */
2227 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
2229 return copyout(&cid
, cidp
, sizeof(cid
));
2233 * Handle SIOCGCONNINFO ioctl for PF_INET domain.
2236 in_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
2237 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
2238 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
2239 user_addr_t aux_data
, uint32_t *aux_len
)
2241 struct inpcb
*inp
= sotoinpcb(so
);
2242 struct sockaddr_in sin
;
2243 struct ifnet
*ifp
= NULL
;
2245 u_int32_t copy_len
= 0;
2248 * Don't test for INPCB_STATE_DEAD since this may be called
2249 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
2256 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
2261 ifp
= inp
->inp_last_outifp
;
2262 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
2263 *soerror
= so
->so_error
;
2265 if (so
->so_state
& SS_ISCONNECTED
) {
2266 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
2268 if (inp
->inp_flags
& INP_BOUND_IF
) {
2269 *flags
|= CIF_BOUND_IF
;
2271 if (!(inp
->inp_flags
& INP_INADDR_ANY
)) {
2272 *flags
|= CIF_BOUND_IP
;
2274 if (!(inp
->inp_flags
& INP_ANONPORT
)) {
2275 *flags
|= CIF_BOUND_PORT
;
2278 bzero(&sin
, sizeof(sin
));
2279 sin
.sin_len
= sizeof(sin
);
2280 sin
.sin_family
= AF_INET
;
2282 /* source address and port */
2283 sin
.sin_port
= inp
->inp_lport
;
2284 sin
.sin_addr
.s_addr
= inp
->inp_laddr
.s_addr
;
2285 if (*src_len
== 0) {
2286 *src_len
= sin
.sin_len
;
2288 if (src
!= USER_ADDR_NULL
) {
2289 copy_len
= min(*src_len
, sizeof(sin
));
2290 error
= copyout(&sin
, src
, copy_len
);
2294 *src_len
= copy_len
;
2298 /* destination address and port */
2299 sin
.sin_port
= inp
->inp_fport
;
2300 sin
.sin_addr
.s_addr
= inp
->inp_faddr
.s_addr
;
2301 if (*dst_len
== 0) {
2302 *dst_len
= sin
.sin_len
;
2304 if (dst
!= USER_ADDR_NULL
) {
2305 copy_len
= min(*dst_len
, sizeof(sin
));
2306 error
= copyout(&sin
, dst
, copy_len
);
2310 *dst_len
= copy_len
;
2314 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
2315 struct conninfo_tcp tcp_ci
;
2317 *aux_type
= CIAUX_TCP
;
2318 if (*aux_len
== 0) {
2319 *aux_len
= sizeof(tcp_ci
);
2321 if (aux_data
!= USER_ADDR_NULL
) {
2322 copy_len
= min(*aux_len
, sizeof(tcp_ci
));
2323 bzero(&tcp_ci
, sizeof(tcp_ci
));
2324 tcp_getconninfo(so
, &tcp_ci
);
2325 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
2329 *aux_len
= copy_len
;
2342 struct llentry base
;
2345 #define IN_LLTBL_DEFAULT_HSIZE 32
2346 #define IN_LLTBL_HASH(k, h) \
2347 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
2350 * Do actual deallocation of @lle.
2353 in_lltable_destroy_lle_unlocked(struct llentry
*lle
)
2355 LLE_LOCK_DESTROY(lle
);
2356 LLE_REQ_DESTROY(lle
);
2357 FREE(lle
, M_LLTABLE
);
2361 * Called by LLE_FREE_LOCKED when number of references
2365 in_lltable_destroy_lle(struct llentry
*lle
)
2368 in_lltable_destroy_lle_unlocked(lle
);
2371 static struct llentry
*
2372 in_lltable_new(struct in_addr addr4
, u_int flags
)
2374 #pragma unused(flags)
2375 struct in_llentry
*lle
;
2377 MALLOC(lle
, struct in_llentry
*, sizeof(struct in_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
2378 if (lle
== NULL
) { /* NB: caller generates msg */
2383 * For IPv4 this will trigger "arpresolve" to generate
2386 lle
->base
.la_expire
= net_uptime(); /* mark expired */
2387 lle
->base
.r_l3addr
.addr4
= addr4
;
2388 lle
->base
.lle_refcnt
= 1;
2389 lle
->base
.lle_free
= in_lltable_destroy_lle
;
2391 LLE_LOCK_INIT(&lle
->base
);
2392 LLE_REQ_INIT(&lle
->base
);
2393 //callout_init(&lle->base.lle_timer, 1);
2398 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
2399 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
2402 in_lltable_match_prefix(const struct sockaddr
*saddr
,
2403 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
)
2405 struct in_addr addr
, mask
, lle_addr
;
2407 addr
= ((const struct sockaddr_in
*)(const void *)saddr
)->sin_addr
;
2408 mask
= ((const struct sockaddr_in
*)(const void *)smask
)->sin_addr
;
2409 lle_addr
.s_addr
= ntohl(lle
->r_l3addr
.addr4
.s_addr
);
2411 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0) {
2415 if (lle
->la_flags
& LLE_IFADDR
) {
2417 * Delete LLE_IFADDR records IFF address & flag matches.
2418 * Note that addr is the interface address within prefix
2420 * Note also we should handle 'ifdown' cases without removing
2423 if (addr
.s_addr
== lle_addr
.s_addr
&& (flags
& LLE_STATIC
) != 0) {
2429 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2430 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
)) {
2438 in_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
2441 size_t pkts_dropped
;
2443 LLE_WLOCK_ASSERT(lle
);
2444 KASSERT(llt
!= NULL
, ("lltable is NULL"));
2446 /* Unlink entry from table if not already */
2447 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
2449 IF_AFDATA_WLOCK_ASSERT(ifp
, llt
->llt_af
);
2450 lltable_unlink_entry(llt
, lle
);
2455 if (callout_stop(&lle
->lle_timer
) > 0) {
2459 /* Drop hold queue */
2460 pkts_dropped
= llentry_free(lle
);
2461 arpstat
.dropped
+= pkts_dropped
;
2466 in_lltable_rtcheck(struct ifnet
*ifp
, u_int flags
, const struct sockaddr
*l3addr
)
2468 #pragma unused(flags)
2471 KASSERT(l3addr
->sa_family
== AF_INET
,
2472 ("sin_family %d", l3addr
->sa_family
));
2474 /* XXX rtalloc1 should take a const param */
2475 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
2476 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
2477 log(LOG_INFO
, "IPv4 address: \"%s\" is not on the network\n",
2478 inet_ntoa(((const struct sockaddr_in
*)(const void *)l3addr
)->sin_addr
));
2488 static inline uint32_t
2489 in_lltable_hash_dst(const struct in_addr dst
, uint32_t hsize
)
2491 return IN_LLTBL_HASH(dst
.s_addr
, hsize
);
2495 in_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
2497 return in_lltable_hash_dst(lle
->r_l3addr
.addr4
, hsize
);
2502 in_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
2504 struct sockaddr_in
*sin
;
2506 sin
= (struct sockaddr_in
*)(void *)sa
;
2507 bzero(sin
, sizeof(*sin
));
2508 sin
->sin_family
= AF_INET
;
2509 sin
->sin_len
= sizeof(*sin
);
2510 sin
->sin_addr
= lle
->r_l3addr
.addr4
;
2513 static inline struct llentry
*
2514 in_lltable_find_dst(struct lltable
*llt
, struct in_addr dst
)
2516 struct llentry
*lle
;
2517 struct llentries
*lleh
;
2520 hashidx
= in_lltable_hash_dst(dst
, llt
->llt_hsize
);
2521 lleh
= &llt
->lle_head
[hashidx
];
2522 LIST_FOREACH(lle
, lleh
, lle_next
) {
2523 if (lle
->la_flags
& LLE_DELETED
) {
2526 if (lle
->r_l3addr
.addr4
.s_addr
== dst
.s_addr
) {
2535 in_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
2538 lle
->la_flags
|= LLE_DELETED
;
2539 //EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2541 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
2546 static struct llentry
*
2547 in_lltable_alloc(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
)
2549 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*) (const void *)l3addr
;
2550 struct ifnet
*ifp
= llt
->llt_ifp
;
2551 struct llentry
*lle
;
2553 KASSERT(l3addr
->sa_family
== AF_INET
,
2554 ("sin_family %d", l3addr
->sa_family
));
2557 * A route that covers the given address must have
2558 * been installed 1st because we are doing a resolution,
2561 if (!(flags
& LLE_IFADDR
) &&
2562 in_lltable_rtcheck(ifp
, flags
, l3addr
) != 0) {
2566 lle
= in_lltable_new(sin
->sin_addr
, flags
);
2568 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
2571 lle
->la_flags
= flags
& ~LLE_CREATE
;
2572 if (flags
& LLE_STATIC
) {
2573 lle
->r_flags
|= RLLE_VALID
;
2575 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
2576 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
2577 lle
->la_flags
|= LLE_STATIC
;
2578 lle
->r_flags
|= (RLLE_VALID
| RLLE_IFADDR
);
2584 * Return NULL if not found or marked for deletion.
2585 * If found return lle read locked.
2587 static struct llentry
*
2588 in_lltable_lookup(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
)
2590 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)(const void *)l3addr
;
2591 struct llentry
*lle
;
2593 IF_AFDATA_WLOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
2595 KASSERT(l3addr
->sa_family
== AF_INET
,
2596 ("sin_family %d", l3addr
->sa_family
));
2597 lle
= in_lltable_find_dst(llt
, sin
->sin_addr
);
2603 KASSERT((flags
& (LLE_UNLOCKED
| LLE_EXCLUSIVE
)) !=
2604 (LLE_UNLOCKED
| LLE_EXCLUSIVE
), ("wrong lle request flags: 0x%X",
2607 if (flags
& LLE_UNLOCKED
) {
2611 if (flags
& LLE_EXCLUSIVE
) {
2621 in_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
2622 struct sysctl_req
*wr
)
2624 struct ifnet
*ifp
= llt
->llt_ifp
;
2627 struct rt_msghdr rtm
;
2628 struct sockaddr_in sin
;
2629 struct sockaddr_dl sdl
;
2631 struct sockaddr_dl
*sdl
;
2634 bzero(&arpc
, sizeof(arpc
));
2635 /* skip deleted entries */
2636 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
) {
2639 /* Skip if jailed and not a valid IP of the prison. */
2640 lltable_fill_sa_entry(lle
, (struct sockaddr
*)&arpc
.sin
);
2642 * produce a msg made of:
2644 * struct sockaddr_in; (IPv4)
2645 * struct sockaddr_dl;
2647 arpc
.rtm
.rtm_msglen
= sizeof(arpc
);
2648 arpc
.rtm
.rtm_version
= RTM_VERSION
;
2649 arpc
.rtm
.rtm_type
= RTM_GET
;
2650 arpc
.rtm
.rtm_flags
= RTF_UP
;
2651 arpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
2654 if (lle
->la_flags
& LLE_PUB
) {
2655 arpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
2659 sdl
->sdl_family
= AF_LINK
;
2660 sdl
->sdl_len
= sizeof(*sdl
);
2661 sdl
->sdl_index
= ifp
->if_index
;
2662 sdl
->sdl_type
= ifp
->if_type
;
2663 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
2664 sdl
->sdl_alen
= ifp
->if_addrlen
;
2665 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
2668 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
2671 arpc
.rtm
.rtm_rmx
.rmx_expire
=
2672 lle
->la_flags
& LLE_STATIC
? 0 : lle
->la_expire
;
2673 arpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
2674 if (lle
->la_flags
& LLE_STATIC
) {
2675 arpc
.rtm
.rtm_flags
|= RTF_STATIC
;
2677 if (lle
->la_flags
& LLE_IFADDR
) {
2678 arpc
.rtm
.rtm_flags
|= RTF_PINNED
;
2680 arpc
.rtm
.rtm_flags
|= RTF_PINNED
;
2681 arpc
.rtm
.rtm_index
= ifp
->if_index
;
2682 error
= SYSCTL_OUT(wr
, &arpc
, sizeof(arpc
));
2687 static struct lltable
*
2688 in_lltattach(struct ifnet
*ifp
)
2690 struct lltable
*llt
;
2692 llt
= lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE
);
2693 llt
->llt_af
= AF_INET
;
2696 llt
->llt_lookup
= in_lltable_lookup
;
2697 llt
->llt_alloc_entry
= in_lltable_alloc
;
2698 llt
->llt_delete_entry
= in_lltable_delete_entry
;
2699 llt
->llt_dump_entry
= in_lltable_dump_entry
;
2700 llt
->llt_hash
= in_lltable_hash
;
2701 llt
->llt_fill_sa_entry
= in_lltable_fill_sa_entry
;
2702 llt
->llt_free_entry
= in_lltable_free_entry
;
2703 llt
->llt_match_prefix
= in_lltable_match_prefix
;
2710 inifa_ifpwithflag(struct ifnet
* ifp
, uint32_t flag
)
2714 ifnet_lock_shared(ifp
);
2715 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_link
)
2718 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2722 if ((((struct in_ifaddr
*)ifa
)->ia_flags
& flag
) == flag
) {
2723 IFA_ADDREF_LOCKED(ifa
);
2729 ifnet_lock_done(ifp
);
2731 return (struct in_ifaddr
*)ifa
;
2735 inifa_ifpclatv4(struct ifnet
* ifp
)
2739 ifnet_lock_shared(ifp
);
2740 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_link
)
2744 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2749 addr
= ntohl(SIN(ifa
->ifa_addr
)->sin_addr
.s_addr
);
2750 if (!IN_LINKLOCAL(addr
) &&
2751 !IN_LOOPBACK(addr
)) {
2752 IFA_ADDREF_LOCKED(ifa
);
2758 ifnet_lock_done(ifp
);
2760 return (struct in_ifaddr
*)ifa
;