2 * Copyright (c) 2000-2016 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>
82 #include <net/if_types.h>
83 #include <net/route.h>
84 #include <net/kpi_protocol.h>
86 #include <net/if_llatbl.h>
87 #include <net/if_arp.h>
89 #include <net/pfvar.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/igmp_var.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/if_ether.h>
102 static int inctl_associd(struct socket
*, u_long
, caddr_t
);
103 static int inctl_connid(struct socket
*, u_long
, caddr_t
);
104 static int inctl_conninfo(struct socket
*, u_long
, caddr_t
);
105 static int inctl_autoaddr(struct ifnet
*, struct ifreq
*);
106 static int inctl_arpipll(struct ifnet
*, struct ifreq
*);
107 static int inctl_setrouter(struct ifnet
*, struct ifreq
*);
108 static int inctl_ifaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
110 static int inctl_ifdstaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
112 static int inctl_ifbrdaddr(struct ifnet
*, struct in_ifaddr
*, u_long
,
114 static int inctl_ifnetmask(struct ifnet
*, struct in_ifaddr
*, u_long
,
117 static void in_socktrim(struct sockaddr_in
*);
118 static int in_ifinit(struct ifnet
*, struct in_ifaddr
*,
119 struct sockaddr_in
*, int);
121 #define IA_HASH_INIT(ia) { \
122 (ia)->ia_hash.tqe_next = (void *)(uintptr_t)-1; \
123 (ia)->ia_hash.tqe_prev = (void *)(uintptr_t)-1; \
126 #define IA_IS_HASHED(ia) \
127 (!((ia)->ia_hash.tqe_next == (void *)(uintptr_t)-1 || \
128 (ia)->ia_hash.tqe_prev == (void *)(uintptr_t)-1))
130 static void in_iahash_remove(struct in_ifaddr
*);
131 static void in_iahash_insert(struct in_ifaddr
*);
132 static void in_iahash_insert_ptp(struct in_ifaddr
*);
133 static struct in_ifaddr
*in_ifaddr_alloc(int);
134 static void in_ifaddr_attached(struct ifaddr
*);
135 static void in_ifaddr_detached(struct ifaddr
*);
136 static void in_ifaddr_free(struct ifaddr
*);
137 static void in_ifaddr_trace(struct ifaddr
*, int);
139 static int in_getassocids(struct socket
*, uint32_t *, user_addr_t
);
140 static int in_getconnids(struct socket
*, sae_associd_t
, uint32_t *, user_addr_t
);
142 /* IPv4 Layer 2 neighbor cache management routines */
143 static void in_lltable_destroy_lle_unlocked(struct llentry
*lle
);
144 static void in_lltable_destroy_lle(struct llentry
*lle
);
145 static struct llentry
*in_lltable_new(struct in_addr addr4
, u_int flags
);
146 static int in_lltable_match_prefix(const struct sockaddr
*saddr
,
147 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
);
148 static void in_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
);
149 static int in_lltable_rtcheck(struct ifnet
*ifp
, u_int flags
, const struct sockaddr
*l3addr
);
150 static inline uint32_t in_lltable_hash_dst(const struct in_addr dst
, uint32_t hsize
);
151 static uint32_t in_lltable_hash(const struct llentry
*lle
, uint32_t hsize
);
152 static void in_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
);
153 static inline struct llentry
* in_lltable_find_dst(struct lltable
*llt
, struct in_addr dst
);
154 static void in_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
);
155 static struct llentry
* in_lltable_alloc(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
);
156 static struct llentry
* in_lltable_lookup(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
);
157 static int in_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
, struct sysctl_req
*wr
);
158 static struct lltable
* in_lltattach(struct ifnet
*ifp
);
160 static int subnetsarelocal
= 0;
161 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, subnets_are_local
,
162 CTLFLAG_RW
| CTLFLAG_LOCKED
, &subnetsarelocal
, 0, "");
164 /* Track whether or not the SIOCARPIPLL ioctl has been called */
165 u_int32_t ipv4_ll_arp_aware
= 0;
167 #define INIFA_TRACE_HIST_SIZE 32 /* size of trace history */
170 __private_extern__
unsigned int inifa_trace_hist_size
= INIFA_TRACE_HIST_SIZE
;
172 struct in_ifaddr_dbg
{
173 struct in_ifaddr inifa
; /* in_ifaddr */
174 struct in_ifaddr inifa_old
; /* saved in_ifaddr */
175 u_int16_t inifa_refhold_cnt
; /* # of IFA_ADDREF */
176 u_int16_t inifa_refrele_cnt
; /* # of IFA_REMREF */
178 * Alloc and free callers.
180 ctrace_t inifa_alloc
;
183 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
185 ctrace_t inifa_refhold
[INIFA_TRACE_HIST_SIZE
];
186 ctrace_t inifa_refrele
[INIFA_TRACE_HIST_SIZE
];
190 TAILQ_ENTRY(in_ifaddr_dbg
) inifa_trash_link
;
193 /* List of trash in_ifaddr entries protected by inifa_trash_lock */
194 static TAILQ_HEAD(, in_ifaddr_dbg
) inifa_trash_head
;
195 static decl_lck_mtx_data(, inifa_trash_lock
);
198 static unsigned int inifa_debug
= 1; /* debugging (enabled) */
200 static unsigned int inifa_debug
; /* debugging (disabled) */
202 static unsigned int inifa_size
; /* size of zone element */
203 static struct zone
*inifa_zone
; /* zone for in_ifaddr */
205 #define INIFA_ZONE_MAX 64 /* maximum elements in zone */
206 #define INIFA_ZONE_NAME "in_ifaddr" /* zone name */
208 static const unsigned int in_extra_size
= sizeof(struct in_ifextra
);
209 static const unsigned int in_extra_bufsize
= in_extra_size
+
210 sizeof(void *) + sizeof(uint64_t);
213 * Return 1 if the address is
215 * - unicast or multicast link local
216 * - routed via a link level gateway
217 * - belongs to a directly connected (sub)net
220 inaddr_local(struct in_addr in
)
223 struct sockaddr_in sin
;
226 if (ntohl(in
.s_addr
) == INADDR_LOOPBACK
||
227 IN_LINKLOCAL(ntohl(in
.s_addr
))) {
229 } else if (ntohl(in
.s_addr
) >= INADDR_UNSPEC_GROUP
&&
230 ntohl(in
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
) {
233 sin
.sin_family
= AF_INET
;
234 sin
.sin_len
= sizeof(sin
);
236 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0);
240 if (rt
->rt_gateway
->sa_family
== AF_LINK
||
241 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
247 local
= in_localaddr(in
);
254 * Return 1 if an internet address is for a ``local'' host
255 * (one to which we have a connection). If subnetsarelocal
256 * is true, this includes other subnets of the local net,
257 * otherwise, it includes the directly-connected (sub)nets.
258 * The IPv4 link local prefix 169.254/16 is also included.
261 in_localaddr(struct in_addr in
)
263 u_int32_t i
= ntohl(in
.s_addr
);
264 struct in_ifaddr
*ia
;
266 if (IN_LINKLOCAL(i
)) {
270 if (subnetsarelocal
) {
271 lck_rw_lock_shared(in_ifaddr_rwlock
);
272 for (ia
= in_ifaddrhead
.tqh_first
; ia
!= NULL
;
273 ia
= ia
->ia_link
.tqe_next
) {
274 IFA_LOCK(&ia
->ia_ifa
);
275 if ((i
& ia
->ia_netmask
) == ia
->ia_net
) {
276 IFA_UNLOCK(&ia
->ia_ifa
);
277 lck_rw_done(in_ifaddr_rwlock
);
280 IFA_UNLOCK(&ia
->ia_ifa
);
282 lck_rw_done(in_ifaddr_rwlock
);
284 lck_rw_lock_shared(in_ifaddr_rwlock
);
285 for (ia
= in_ifaddrhead
.tqh_first
; ia
!= NULL
;
286 ia
= ia
->ia_link
.tqe_next
) {
287 IFA_LOCK(&ia
->ia_ifa
);
288 if ((i
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
289 IFA_UNLOCK(&ia
->ia_ifa
);
290 lck_rw_done(in_ifaddr_rwlock
);
293 IFA_UNLOCK(&ia
->ia_ifa
);
295 lck_rw_done(in_ifaddr_rwlock
);
301 * Determine whether an IP address is in a reserved set of addresses
302 * that may not be forwarded, or whether datagrams to that destination
306 in_canforward(struct in_addr in
)
308 u_int32_t i
= ntohl(in
.s_addr
);
311 if (IN_EXPERIMENTAL(i
) || IN_MULTICAST(i
)) {
315 net
= i
& IN_CLASSA_NET
;
316 if (net
== 0 || net
== (IN_LOOPBACKNET
<< IN_CLASSA_NSHIFT
)) {
324 * Trim a mask in a sockaddr
327 in_socktrim(struct sockaddr_in
*ap
)
329 char *cplim
= (char *)&ap
->sin_addr
;
330 char *cp
= (char *)(&ap
->sin_addr
+ 1);
333 while (--cp
>= cplim
) {
335 (ap
)->sin_len
= cp
- (char *)(ap
) + 1;
341 static int in_interfaces
; /* number of external internet interfaces */
344 in_domifattach(struct ifnet
*ifp
)
350 if ((error
= proto_plumb(PF_INET
, ifp
)) && error
!= EEXIST
) {
351 log(LOG_ERR
, "%s: proto_plumb returned %d if=%s\n",
352 __func__
, error
, if_name(ifp
));
353 } else if (error
== 0 && ifp
->if_inetdata
== NULL
) {
355 struct in_ifextra
*ext
;
358 if ((ext
= (struct in_ifextra
*)_MALLOC(in_extra_bufsize
,
359 M_IFADDR
, M_WAITOK
| M_ZERO
)) == NULL
) {
361 errorx
= proto_unplumb(PF_INET
, ifp
);
364 "%s: proto_unplumb returned %d if=%s%d\n",
365 __func__
, errorx
, ifp
->if_name
,
371 /* Align on 64-bit boundary */
372 base
= (void *)P2ROUNDUP((intptr_t)ext
+ sizeof(uint64_t),
374 VERIFY(((intptr_t)base
+ in_extra_size
) <=
375 ((intptr_t)ext
+ in_extra_bufsize
));
376 pbuf
= (void **)((intptr_t)base
- sizeof(void *));
378 ifp
->if_inetdata
= base
;
379 IN_IFEXTRA(ifp
)->ii_llt
= in_lltattach(ifp
);
380 VERIFY(IS_P2ALIGNED(ifp
->if_inetdata
, sizeof(uint64_t)));
383 if (error
== 0 && ifp
->if_inetdata
!= NULL
) {
385 * Since the structure is never freed, we need to
386 * zero out its contents to avoid reusing stale data.
387 * A little redundant with allocation above, but it
388 * keeps the code simpler for all cases.
390 bzero(ifp
->if_inetdata
, in_extra_size
);
395 static __attribute__((noinline
)) int
396 inctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
400 struct so_aidreq32 a32
;
401 struct so_aidreq64 a64
;
407 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
408 bcopy(data
, &u
.a32
, sizeof(u
.a32
));
409 error
= in_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
411 bcopy(&u
.a32
, data
, sizeof(u
.a32
));
415 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
416 bcopy(data
, &u
.a64
, sizeof(u
.a64
));
417 error
= in_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
419 bcopy(&u
.a64
, data
, sizeof(u
.a64
));
431 static __attribute__((noinline
)) int
432 inctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
436 struct so_cidreq32 c32
;
437 struct so_cidreq64 c64
;
443 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
444 bcopy(data
, &u
.c32
, sizeof(u
.c32
));
445 error
= in_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
448 bcopy(&u
.c32
, data
, sizeof(u
.c32
));
452 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
453 bcopy(data
, &u
.c64
, sizeof(u
.c64
));
454 error
= in_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
457 bcopy(&u
.c64
, data
, sizeof(u
.c64
));
469 static __attribute__((noinline
)) int
470 inctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
474 struct so_cinforeq32 ci32
;
475 struct so_cinforeq64 ci64
;
481 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
482 bcopy(data
, &u
.ci32
, sizeof(u
.ci32
));
483 error
= in_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
484 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
485 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
486 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
487 &u
.ci32
.scir_aux_len
);
489 bcopy(&u
.ci32
, data
, sizeof(u
.ci32
));
493 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
494 bcopy(data
, &u
.ci64
, sizeof(u
.ci64
));
495 error
= in_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
496 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
497 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
498 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
499 &u
.ci64
.scir_aux_len
);
501 bcopy(&u
.ci64
, data
, sizeof(u
.ci64
));
514 * Caller passes in the ioctl data pointer directly via "ifr", with the
515 * expectation that this routine always uses bcopy() or other byte-aligned
518 static __attribute__((noinline
)) int
519 inctl_autoaddr(struct ifnet
*ifp
, struct ifreq
*ifr
)
521 int error
= 0, intval
;
525 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
527 ifnet_lock_exclusive(ifp
);
530 * An interface in IPv4 router mode implies that it
531 * is configured with a static IP address and should
532 * not act as a DHCP client; prevent SIOCAUTOADDR from
533 * being set in that mode.
535 if (ifp
->if_eflags
& IFEF_IPV4_ROUTER
) {
536 intval
= 0; /* be safe; clear flag if set */
539 ifp
->if_eflags
|= IFEF_AUTOCONFIGURING
;
543 ifp
->if_eflags
&= ~IFEF_AUTOCONFIGURING
;
545 ifnet_lock_done(ifp
);
551 * Caller passes in the ioctl data pointer directly via "ifr", with the
552 * expectation that this routine always uses bcopy() or other byte-aligned
555 static __attribute__((noinline
)) int
556 inctl_arpipll(struct ifnet
*ifp
, struct ifreq
*ifr
)
558 int error
= 0, intval
;
562 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
563 ipv4_ll_arp_aware
= 1;
565 ifnet_lock_exclusive(ifp
);
568 * An interface in IPv4 router mode implies that it
569 * is configured with a static IP address and should
570 * not have to deal with IPv4 Link-Local Address;
571 * prevent SIOCARPIPLL from being set in that mode.
573 if (ifp
->if_eflags
& IFEF_IPV4_ROUTER
) {
574 intval
= 0; /* be safe; clear flag if set */
577 ifp
->if_eflags
|= IFEF_ARPLL
;
581 ifp
->if_eflags
&= ~IFEF_ARPLL
;
583 ifnet_lock_done(ifp
);
589 * Handle SIOCSETROUTERMODE to set or clear the IPv4 router mode flag on
590 * the interface. When in this mode, IPv4 Link-Local Address support is
591 * disabled in ARP, and DHCP client support is disabled in IP input; turning
592 * any of them on would cause an error to be returned. Entering or exiting
593 * this mode will result in the removal of IPv4 addresses currently configured
596 * Caller passes in the ioctl data pointer directly via "ifr", with the
597 * expectation that this routine always uses bcopy() or other byte-aligned
600 static __attribute__((noinline
)) int
601 inctl_setrouter(struct ifnet
*ifp
, struct ifreq
*ifr
)
603 int error
= 0, intval
;
607 /* Router mode isn't valid for loopback */
608 if (ifp
->if_flags
& IFF_LOOPBACK
) {
612 bcopy(&ifr
->ifr_intval
, &intval
, sizeof(intval
));
614 ifnet_lock_exclusive(ifp
);
616 ifp
->if_eflags
|= IFEF_IPV4_ROUTER
;
617 ifp
->if_eflags
&= ~(IFEF_ARPLL
| IFEF_AUTOCONFIGURING
);
619 ifp
->if_eflags
&= ~IFEF_IPV4_ROUTER
;
621 ifnet_lock_done(ifp
);
623 /* purge all IPv4 addresses configured on this interface */
630 * Caller passes in the ioctl data pointer directly via "ifr", with the
631 * expectation that this routine always uses bcopy() or other byte-aligned
634 static __attribute__((noinline
)) int
635 inctl_ifaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
638 struct kev_in_data in_event_data
;
639 struct kev_msg ev_msg
;
640 struct sockaddr_in addr
;
646 bzero(&in_event_data
, sizeof(struct kev_in_data
));
647 bzero(&ev_msg
, sizeof(struct kev_msg
));
650 case SIOCGIFADDR
: /* struct ifreq */
652 error
= EADDRNOTAVAIL
;
655 IFA_LOCK(&ia
->ia_ifa
);
656 bcopy(&ia
->ia_addr
, &ifr
->ifr_addr
, sizeof(addr
));
657 IFA_UNLOCK(&ia
->ia_ifa
);
660 case SIOCSIFADDR
: /* struct ifreq */
662 bcopy(&ifr
->ifr_addr
, &addr
, sizeof(addr
));
664 * If this is a new address, the reference count for the
665 * hash table has been taken at creation time above.
667 error
= in_ifinit(ifp
, ia
, &addr
, 1);
669 (void) ifnet_notify_address(ifp
, AF_INET
);
673 case SIOCAIFADDR
: { /* struct {if,in_}aliasreq */
674 struct in_aliasreq
*ifra
= (struct in_aliasreq
*)ifr
;
675 struct sockaddr_in broadaddr
, mask
;
676 int hostIsNew
, maskIsNew
;
679 bcopy(&ifra
->ifra_addr
, &addr
, sizeof(addr
));
680 bcopy(&ifra
->ifra_broadaddr
, &broadaddr
, sizeof(broadaddr
));
681 bcopy(&ifra
->ifra_mask
, &mask
, sizeof(mask
));
687 IFA_LOCK(&ia
->ia_ifa
);
688 if (ia
->ia_addr
.sin_family
== AF_INET
) {
689 if (addr
.sin_len
== 0) {
692 } else if (addr
.sin_addr
.s_addr
==
693 ia
->ia_addr
.sin_addr
.s_addr
) {
698 IFA_UNLOCK(&ia
->ia_ifa
);
699 in_ifscrub(ifp
, ia
, 0);
700 IFA_LOCK(&ia
->ia_ifa
);
701 ia
->ia_sockmask
= mask
;
703 ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
);
706 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
707 (broadaddr
.sin_family
== AF_INET
)) {
708 IFA_UNLOCK(&ia
->ia_ifa
);
709 in_ifscrub(ifp
, ia
, 0);
710 IFA_LOCK(&ia
->ia_ifa
);
711 ia
->ia_dstaddr
= broadaddr
;
712 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
713 maskIsNew
= 1; /* We lie; but the effect's the same */
715 if (addr
.sin_family
== AF_INET
&& (hostIsNew
|| maskIsNew
)) {
716 IFA_UNLOCK(&ia
->ia_ifa
);
717 error
= in_ifinit(ifp
, ia
, &addr
, 0);
719 IFA_UNLOCK(&ia
->ia_ifa
);
722 (void) ifnet_notify_address(ifp
, AF_INET
);
724 IFA_LOCK(&ia
->ia_ifa
);
725 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
726 (broadaddr
.sin_family
== AF_INET
)) {
727 ia
->ia_broadaddr
= broadaddr
;
733 if ((error
== 0) || (error
== EEXIST
)) {
734 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
735 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
736 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
739 ev_msg
.event_code
= KEV_INET_NEW_ADDR
;
741 ev_msg
.event_code
= KEV_INET_CHANGED_ADDR
;
744 if (ia
->ia_ifa
.ifa_dstaddr
) {
745 in_event_data
.ia_dstaddr
=
746 ((struct sockaddr_in
*)(void *)ia
->
747 ia_ifa
.ifa_dstaddr
)->sin_addr
;
749 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
751 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
752 in_event_data
.ia_net
= ia
->ia_net
;
753 in_event_data
.ia_netmask
= ia
->ia_netmask
;
754 in_event_data
.ia_subnet
= ia
->ia_subnet
;
755 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
756 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
757 IFA_UNLOCK(&ia
->ia_ifa
);
758 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
759 ifp
->if_name
, IFNAMSIZ
);
760 in_event_data
.link_data
.if_family
= ifp
->if_family
;
761 in_event_data
.link_data
.if_unit
= ifp
->if_unit
;
763 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
764 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
765 ev_msg
.dv
[1].data_length
= 0;
767 dlil_post_complete_msg(ifp
, &ev_msg
);
769 IFA_UNLOCK(&ia
->ia_ifa
);
774 case SIOCDIFADDR
: /* struct ifreq */
776 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCDIFADDR
, ia
);
777 if (error
== EOPNOTSUPP
) {
781 /* Reset the detaching flag */
782 IFA_LOCK(&ia
->ia_ifa
);
783 ia
->ia_ifa
.ifa_debug
&= ~IFD_DETACHING
;
784 IFA_UNLOCK(&ia
->ia_ifa
);
788 /* Fill out the kernel event information */
789 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
790 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
791 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
793 ev_msg
.event_code
= KEV_INET_ADDR_DELETED
;
795 IFA_LOCK(&ia
->ia_ifa
);
796 if (ia
->ia_ifa
.ifa_dstaddr
) {
797 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
798 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
800 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
802 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
803 in_event_data
.ia_net
= ia
->ia_net
;
804 in_event_data
.ia_netmask
= ia
->ia_netmask
;
805 in_event_data
.ia_subnet
= ia
->ia_subnet
;
806 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
807 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
808 IFA_UNLOCK(&ia
->ia_ifa
);
809 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
810 ifp
->if_name
, IFNAMSIZ
);
811 in_event_data
.link_data
.if_family
= ifp
->if_family
;
812 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
814 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
815 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
816 ev_msg
.dv
[1].data_length
= 0;
819 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
820 /* Release ia_link reference */
822 TAILQ_REMOVE(&in_ifaddrhead
, ia
, ia_link
);
824 if (IA_IS_HASHED(ia
)) {
825 in_iahash_remove(ia
);
828 lck_rw_done(in_ifaddr_rwlock
);
831 * in_ifscrub kills the interface route.
833 in_ifscrub(ifp
, ia
, 0);
834 ifnet_lock_exclusive(ifp
);
836 /* if_detach_ifa() releases ifa_link reference */
837 if_detach_ifa(ifp
, ifa
);
838 /* Our reference to this address is dropped at the bottom */
841 /* invalidate route caches */
842 routegenid_inet_update();
845 * If the interface supports multicast, and no address is left,
846 * remove the "all hosts" multicast group from that interface.
848 if ((ifp
->if_flags
& IFF_MULTICAST
) ||
849 ifp
->if_allhostsinm
!= NULL
) {
850 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
852 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
858 ifnet_lock_done(ifp
);
860 lck_mtx_lock(&ifp
->if_addrconfig_lock
);
861 if (ifa
== NULL
&& ifp
->if_allhostsinm
!= NULL
) {
862 struct in_multi
*inm
= ifp
->if_allhostsinm
;
863 ifp
->if_allhostsinm
= NULL
;
866 /* release the reference for allhostsinm */
869 lck_mtx_unlock(&ifp
->if_addrconfig_lock
);
871 ifnet_lock_done(ifp
);
874 /* Post the kernel event */
875 dlil_post_complete_msg(ifp
, &ev_msg
);
878 * See if there is any IPV4 address left and if so,
879 * reconfigure KDP to use current primary address.
881 ifa
= ifa_ifpgetprimary(ifp
, AF_INET
);
884 * NOTE: SIOCSIFADDR is defined with struct ifreq
885 * as parameter, but here we are sending it down
886 * to the interface with a pointer to struct ifaddr,
887 * for legacy reasons.
889 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFADDR
, ifa
);
890 if (error
== EOPNOTSUPP
) {
894 /* Release reference from ifa_ifpgetprimary() */
897 (void) ifnet_notify_address(ifp
, AF_INET
);
909 * Caller passes in the ioctl data pointer directly via "ifr", with the
910 * expectation that this routine always uses bcopy() or other byte-aligned
913 static __attribute__((noinline
)) int
914 inctl_ifdstaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
917 struct kev_in_data in_event_data
;
918 struct kev_msg ev_msg
;
919 struct sockaddr_in dstaddr
;
924 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
928 bzero(&in_event_data
, sizeof(struct kev_in_data
));
929 bzero(&ev_msg
, sizeof(struct kev_msg
));
932 case SIOCGIFDSTADDR
: /* struct ifreq */
934 error
= EADDRNOTAVAIL
;
937 IFA_LOCK(&ia
->ia_ifa
);
938 bcopy(&ia
->ia_dstaddr
, &ifr
->ifr_dstaddr
, sizeof(dstaddr
));
939 IFA_UNLOCK(&ia
->ia_ifa
);
942 case SIOCSIFDSTADDR
: /* struct ifreq */
944 IFA_LOCK(&ia
->ia_ifa
);
945 dstaddr
= ia
->ia_dstaddr
;
946 bcopy(&ifr
->ifr_dstaddr
, &ia
->ia_dstaddr
, sizeof(dstaddr
));
947 if (ia
->ia_dstaddr
.sin_family
== AF_INET
) {
948 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
950 IFA_UNLOCK(&ia
->ia_ifa
);
952 * NOTE: SIOCSIFDSTADDR is defined with struct ifreq
953 * as parameter, but here we are sending it down
954 * to the interface with a pointer to struct ifaddr,
955 * for legacy reasons.
957 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFDSTADDR
, ia
);
958 IFA_LOCK(&ia
->ia_ifa
);
959 if (error
== EOPNOTSUPP
) {
963 ia
->ia_dstaddr
= dstaddr
;
964 IFA_UNLOCK(&ia
->ia_ifa
);
967 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
969 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
970 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
971 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
973 ev_msg
.event_code
= KEV_INET_SIFDSTADDR
;
975 if (ia
->ia_ifa
.ifa_dstaddr
) {
976 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
977 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
979 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
982 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
983 in_event_data
.ia_net
= ia
->ia_net
;
984 in_event_data
.ia_netmask
= ia
->ia_netmask
;
985 in_event_data
.ia_subnet
= ia
->ia_subnet
;
986 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
987 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
988 IFA_UNLOCK(&ia
->ia_ifa
);
989 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
990 ifp
->if_name
, IFNAMSIZ
);
991 in_event_data
.link_data
.if_family
= ifp
->if_family
;
992 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
994 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
995 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
996 ev_msg
.dv
[1].data_length
= 0;
998 dlil_post_complete_msg(ifp
, &ev_msg
);
1000 lck_mtx_lock(rnh_lock
);
1001 IFA_LOCK(&ia
->ia_ifa
);
1002 if (ia
->ia_flags
& IFA_ROUTE
) {
1003 ia
->ia_ifa
.ifa_dstaddr
= (struct sockaddr
*)&dstaddr
;
1004 IFA_UNLOCK(&ia
->ia_ifa
);
1005 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
1006 IFA_LOCK(&ia
->ia_ifa
);
1007 ia
->ia_ifa
.ifa_dstaddr
=
1008 (struct sockaddr
*)&ia
->ia_dstaddr
;
1009 IFA_UNLOCK(&ia
->ia_ifa
);
1010 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_ADD
,
1013 IFA_UNLOCK(&ia
->ia_ifa
);
1015 lck_mtx_unlock(rnh_lock
);
1029 * Caller passes in the ioctl data pointer directly via "ifr", with the
1030 * expectation that this routine always uses bcopy() or other byte-aligned
1033 static __attribute__((noinline
)) int
1034 inctl_ifbrdaddr(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
1037 struct kev_in_data in_event_data
;
1038 struct kev_msg ev_msg
;
1041 VERIFY(ifp
!= NULL
);
1044 return EADDRNOTAVAIL
;
1047 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
1051 bzero(&in_event_data
, sizeof(struct kev_in_data
));
1052 bzero(&ev_msg
, sizeof(struct kev_msg
));
1055 case SIOCGIFBRDADDR
: /* struct ifreq */
1056 IFA_LOCK(&ia
->ia_ifa
);
1057 bcopy(&ia
->ia_broadaddr
, &ifr
->ifr_broadaddr
,
1058 sizeof(struct sockaddr_in
));
1059 IFA_UNLOCK(&ia
->ia_ifa
);
1062 case SIOCSIFBRDADDR
: /* struct ifreq */
1063 IFA_LOCK(&ia
->ia_ifa
);
1064 bcopy(&ifr
->ifr_broadaddr
, &ia
->ia_broadaddr
,
1065 sizeof(struct sockaddr_in
));
1067 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1068 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1069 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1071 ev_msg
.event_code
= KEV_INET_SIFBRDADDR
;
1073 if (ia
->ia_ifa
.ifa_dstaddr
) {
1074 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
1075 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
1077 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
1079 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
1080 in_event_data
.ia_net
= ia
->ia_net
;
1081 in_event_data
.ia_netmask
= ia
->ia_netmask
;
1082 in_event_data
.ia_subnet
= ia
->ia_subnet
;
1083 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
1084 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
1085 IFA_UNLOCK(&ia
->ia_ifa
);
1086 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
1087 ifp
->if_name
, IFNAMSIZ
);
1088 in_event_data
.link_data
.if_family
= ifp
->if_family
;
1089 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
1091 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
1092 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
1093 ev_msg
.dv
[1].data_length
= 0;
1095 dlil_post_complete_msg(ifp
, &ev_msg
);
1107 * Caller passes in the ioctl data pointer directly via "ifr", with the
1108 * expectation that this routine always uses bcopy() or other byte-aligned
1111 static __attribute__((noinline
)) int
1112 inctl_ifnetmask(struct ifnet
*ifp
, struct in_ifaddr
*ia
, u_long cmd
,
1115 struct kev_in_data in_event_data
;
1116 struct kev_msg ev_msg
;
1117 struct sockaddr_in mask
;
1120 VERIFY(ifp
!= NULL
);
1122 bzero(&in_event_data
, sizeof(struct kev_in_data
));
1123 bzero(&ev_msg
, sizeof(struct kev_msg
));
1126 case SIOCGIFNETMASK
: /* struct ifreq */
1128 error
= EADDRNOTAVAIL
;
1131 IFA_LOCK(&ia
->ia_ifa
);
1132 bcopy(&ia
->ia_sockmask
, &ifr
->ifr_addr
, sizeof(mask
));
1133 IFA_UNLOCK(&ia
->ia_ifa
);
1136 case SIOCSIFNETMASK
: { /* struct ifreq */
1139 bcopy(&ifr
->ifr_addr
, &mask
, sizeof(mask
));
1140 i
= mask
.sin_addr
.s_addr
;
1143 IFA_LOCK(&ia
->ia_ifa
);
1144 ia
->ia_subnetmask
= ntohl(ia
->ia_sockmask
.sin_addr
.s_addr
= i
);
1145 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1146 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1147 ev_msg
.kev_subclass
= KEV_INET_SUBCLASS
;
1149 ev_msg
.event_code
= KEV_INET_SIFNETMASK
;
1151 if (ia
->ia_ifa
.ifa_dstaddr
) {
1152 in_event_data
.ia_dstaddr
= ((struct sockaddr_in
*)
1153 (void *)ia
->ia_ifa
.ifa_dstaddr
)->sin_addr
;
1155 in_event_data
.ia_dstaddr
.s_addr
= INADDR_ANY
;
1157 in_event_data
.ia_addr
= ia
->ia_addr
.sin_addr
;
1158 in_event_data
.ia_net
= ia
->ia_net
;
1159 in_event_data
.ia_netmask
= ia
->ia_netmask
;
1160 in_event_data
.ia_subnet
= ia
->ia_subnet
;
1161 in_event_data
.ia_subnetmask
= ia
->ia_subnetmask
;
1162 in_event_data
.ia_netbroadcast
= ia
->ia_netbroadcast
;
1163 IFA_UNLOCK(&ia
->ia_ifa
);
1164 (void) strlcpy(&in_event_data
.link_data
.if_name
[0],
1165 ifp
->if_name
, IFNAMSIZ
);
1166 in_event_data
.link_data
.if_family
= ifp
->if_family
;
1167 in_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
1169 ev_msg
.dv
[0].data_ptr
= &in_event_data
;
1170 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in_data
);
1171 ev_msg
.dv
[1].data_length
= 0;
1173 dlil_post_complete_msg(ifp
, &ev_msg
);
1186 * Generic INET control operations (ioctl's).
1188 * ifp is NULL if not an interface-specific ioctl.
1190 * Most of the routines called to handle the ioctls would end up being
1191 * tail-call optimized, which unfortunately causes this routine to
1192 * consume too much stack space; this is the reason for the "noinline"
1193 * attribute used on those routines.
1195 * If called directly from within the networking stack (as opposed to via
1196 * pru_control), the socket parameter may be NULL.
1199 in_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1202 struct ifreq
*ifr
= (struct ifreq
*)(void *)data
;
1203 struct sockaddr_in addr
, dstaddr
;
1204 struct sockaddr_in sin
, *sa
= NULL
;
1205 boolean_t privileged
= (proc_suser(p
) == 0);
1206 boolean_t so_unlocked
= FALSE
;
1207 struct in_ifaddr
*ia
= NULL
;
1211 /* In case it's NULL, make sure it came from the kernel */
1212 VERIFY(so
!= NULL
|| p
== kernproc
);
1215 * ioctls which don't require ifp, but require socket.
1218 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1219 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1220 return inctl_associd(so
, cmd
, data
);
1223 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1224 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1225 return inctl_connid(so
, cmd
, data
);
1228 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1229 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1230 return inctl_conninfo(so
, cmd
, data
);
1235 * The rest of ioctls require ifp; reject if we don't have one;
1236 * return ENXIO to be consistent with ifioctl().
1243 * ioctls which require ifp but not interface address.
1246 case SIOCAUTOADDR
: /* struct ifreq */
1250 return inctl_autoaddr(ifp
, ifr
);
1253 case SIOCARPIPLL
: /* struct ifreq */
1257 return inctl_arpipll(ifp
, ifr
);
1260 case SIOCSETROUTERMODE
: /* struct ifreq */
1264 return inctl_setrouter(ifp
, ifr
);
1267 case SIOCPROTOATTACH
: /* struct ifreq */
1271 return in_domifattach(ifp
);
1274 case SIOCPROTODETACH
: /* struct ifreq */
1280 * If an IPv4 address is still present, refuse to detach.
1282 ifnet_lock_shared(ifp
);
1283 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1285 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
1291 ifnet_lock_done(ifp
);
1292 return (ifa
== NULL
) ? proto_unplumb(PF_INET
, ifp
) : EBUSY
;
1297 * ioctls which require interface address; obtain sockaddr_in.
1300 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1304 bcopy(&((struct in_aliasreq
*)(void *)data
)->ifra_addr
,
1309 case SIOCDIFADDR
: /* struct ifreq */
1310 case SIOCSIFADDR
: /* struct ifreq */
1311 case SIOCSIFDSTADDR
: /* struct ifreq */
1312 case SIOCSIFNETMASK
: /* struct ifreq */
1313 case SIOCSIFBRDADDR
: /* struct ifreq */
1318 case SIOCGIFADDR
: /* struct ifreq */
1319 case SIOCGIFDSTADDR
: /* struct ifreq */
1320 case SIOCGIFNETMASK
: /* struct ifreq */
1321 case SIOCGIFBRDADDR
: /* struct ifreq */
1322 bcopy(&ifr
->ifr_addr
, &sin
, sizeof(sin
));
1328 * Find address for this interface, if it exists.
1330 * If an alias address was specified, find that one instead of
1331 * the first one on the interface, if possible.
1335 struct in_ifaddr
*iap
;
1338 * Any failures from this point on must take into account
1339 * a non-NULL "ia" with an outstanding reference count, and
1340 * therefore requires IFA_REMREF. Jump to "done" label
1341 * instead of calling return if "ia" is valid.
1343 lck_rw_lock_shared(in_ifaddr_rwlock
);
1344 TAILQ_FOREACH(iap
, INADDR_HASH(sa
->sin_addr
.s_addr
), ia_hash
) {
1345 IFA_LOCK(&iap
->ia_ifa
);
1346 if (iap
->ia_ifp
== ifp
&&
1347 iap
->ia_addr
.sin_addr
.s_addr
==
1348 sa
->sin_addr
.s_addr
) {
1350 * Avoid the race condition seen when two
1351 * threads process SIOCDIFADDR command
1352 * at the same time (radar 28942007)
1354 if (cmd
== SIOCDIFADDR
) {
1355 if (iap
->ia_ifa
.ifa_debug
&
1357 IFA_UNLOCK(&iap
->ia_ifa
);
1360 iap
->ia_ifa
.ifa_debug
|=
1365 IFA_ADDREF_LOCKED(&iap
->ia_ifa
);
1366 IFA_UNLOCK(&iap
->ia_ifa
);
1369 IFA_UNLOCK(&iap
->ia_ifa
);
1371 lck_rw_done(in_ifaddr_rwlock
);
1374 ifnet_lock_shared(ifp
);
1375 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1377 IFA_LOCK(&iap
->ia_ifa
);
1378 if (iap
->ia_addr
.sin_family
== AF_INET
) {
1380 IFA_UNLOCK(&iap
->ia_ifa
);
1383 IFA_UNLOCK(&iap
->ia_ifa
);
1385 /* take a reference on ia before releasing lock */
1387 IFA_ADDREF(&ia
->ia_ifa
);
1389 ifnet_lock_done(ifp
);
1394 * Unlock the socket since ifnet_ioctl() may be invoked by
1395 * one of the ioctl handlers below. Socket will be re-locked
1396 * prior to returning.
1399 socket_unlock(so
, 0);
1404 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1405 case SIOCDIFADDR
: /* struct ifreq */
1406 if (cmd
== SIOCAIFADDR
) {
1407 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1408 ifra_addr
, &addr
, sizeof(addr
));
1409 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1410 ifra_dstaddr
, &dstaddr
, sizeof(dstaddr
));
1412 VERIFY(cmd
== SIOCDIFADDR
);
1413 bcopy(&((struct ifreq
*)(void *)data
)->ifr_addr
,
1414 &addr
, sizeof(addr
));
1415 bzero(&dstaddr
, sizeof(dstaddr
));
1418 if (addr
.sin_family
== AF_INET
) {
1419 struct in_ifaddr
*oia
;
1421 lck_rw_lock_shared(in_ifaddr_rwlock
);
1422 for (oia
= ia
; ia
; ia
= ia
->ia_link
.tqe_next
) {
1423 IFA_LOCK(&ia
->ia_ifa
);
1424 if (ia
->ia_ifp
== ifp
&&
1425 ia
->ia_addr
.sin_addr
.s_addr
==
1426 addr
.sin_addr
.s_addr
) {
1427 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1428 IFA_UNLOCK(&ia
->ia_ifa
);
1431 IFA_UNLOCK(&ia
->ia_ifa
);
1433 lck_rw_done(in_ifaddr_rwlock
);
1435 IFA_REMREF(&oia
->ia_ifa
);
1437 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
1438 (cmd
== SIOCAIFADDR
) &&
1439 (dstaddr
.sin_addr
.s_addr
== INADDR_ANY
)) {
1440 error
= EDESTADDRREQ
;
1443 } else if (cmd
== SIOCAIFADDR
) {
1447 if (cmd
== SIOCDIFADDR
&& ia
== NULL
) {
1448 error
= EADDRNOTAVAIL
;
1452 case SIOCSIFADDR
: /* struct ifreq */
1453 case SIOCSIFDSTADDR
: /* struct ifreq */
1454 case SIOCSIFNETMASK
: /* struct ifreq */
1455 if (cmd
== SIOCAIFADDR
) {
1456 /* fell thru from above; just repeat it */
1457 bcopy(&((struct in_aliasreq
*)(void *)data
)->
1458 ifra_addr
, &addr
, sizeof(addr
));
1460 VERIFY(cmd
== SIOCDIFADDR
|| cmd
== SIOCSIFADDR
||
1461 cmd
== SIOCSIFNETMASK
|| cmd
== SIOCSIFDSTADDR
);
1462 bcopy(&((struct ifreq
*)(void *)data
)->ifr_addr
,
1463 &addr
, sizeof(addr
));
1466 if (addr
.sin_family
!= AF_INET
&& cmd
== SIOCSIFADDR
) {
1471 ia
= in_ifaddr_alloc(M_WAITOK
);
1476 ifnet_lock_exclusive(ifp
);
1479 /* Hold a reference for this routine */
1480 IFA_ADDREF_LOCKED(ifa
);
1482 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1483 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
1484 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_sockmask
;
1485 ia
->ia_sockmask
.sin_len
= 8;
1486 if (ifp
->if_flags
& IFF_BROADCAST
) {
1487 ia
->ia_broadaddr
.sin_len
= sizeof(ia
->ia_addr
);
1488 ia
->ia_broadaddr
.sin_family
= AF_INET
;
1491 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
1494 /* if_attach_ifa() holds a reference for ifa_link */
1495 if_attach_ifa(ifp
, ifa
);
1497 * If we have to go through in_ifinit(), make sure
1498 * to avoid installing route(s) based on this address
1499 * via PFC_IFUP event, before the link resolver (ARP)
1502 if (cmd
== SIOCAIFADDR
|| cmd
== SIOCSIFADDR
) {
1503 ifa
->ifa_debug
|= IFD_NOTREADY
;
1506 ifnet_lock_done(ifp
);
1507 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1508 /* Hold a reference for ia_link */
1510 TAILQ_INSERT_TAIL(&in_ifaddrhead
, ia
, ia_link
);
1511 lck_rw_done(in_ifaddr_rwlock
);
1513 (void) in_domifattach(ifp
);
1520 case SIOCGIFDSTADDR
: /* struct ifreq */
1521 case SIOCSIFDSTADDR
: /* struct ifreq */
1522 error
= inctl_ifdstaddr(ifp
, ia
, cmd
, ifr
);
1525 case SIOCGIFBRDADDR
: /* struct ifreq */
1526 case SIOCSIFBRDADDR
: /* struct ifreq */
1527 error
= inctl_ifbrdaddr(ifp
, ia
, cmd
, ifr
);
1530 case SIOCGIFNETMASK
: /* struct ifreq */
1531 case SIOCSIFNETMASK
: /* struct ifreq */
1532 error
= inctl_ifnetmask(ifp
, ia
, cmd
, ifr
);
1535 case SIOCGIFADDR
: /* struct ifreq */
1536 case SIOCSIFADDR
: /* struct ifreq */
1537 case SIOCAIFADDR
: /* struct {if,in_}aliasreq */
1538 case SIOCDIFADDR
: /* struct ifreq */
1539 error
= inctl_ifaddr(ifp
, ia
, cmd
, ifr
);
1548 IFA_REMREF(&ia
->ia_ifa
);
1558 * Delete any existing route for an interface.
1561 in_ifscrub(struct ifnet
*ifp
, struct in_ifaddr
*ia
, int locked
)
1563 IFA_LOCK(&ia
->ia_ifa
);
1564 if ((ia
->ia_flags
& IFA_ROUTE
) == 0) {
1565 IFA_UNLOCK(&ia
->ia_ifa
);
1568 IFA_UNLOCK(&ia
->ia_ifa
);
1570 lck_mtx_lock(rnh_lock
);
1572 if (ifp
->if_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) {
1573 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
);
1575 rtinit_locked(&(ia
->ia_ifa
), (int)RTM_DELETE
, 0);
1577 IFA_LOCK(&ia
->ia_ifa
);
1578 ia
->ia_flags
&= ~IFA_ROUTE
;
1579 IFA_UNLOCK(&ia
->ia_ifa
);
1581 lck_mtx_unlock(rnh_lock
);
1586 * Caller must hold in_ifaddr_rwlock as writer.
1589 in_iahash_remove(struct in_ifaddr
*ia
)
1591 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1592 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1594 if (!IA_IS_HASHED(ia
)) {
1595 panic("attempt to remove wrong ia %p from hash table\n", ia
);
1598 TAILQ_REMOVE(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
), ia
, ia_hash
);
1600 if (IFA_REMREF_LOCKED(&ia
->ia_ifa
) == NULL
) {
1601 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
,
1608 * Caller must hold in_ifaddr_rwlock as writer.
1611 in_iahash_insert(struct in_ifaddr
*ia
)
1613 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1614 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1616 if (ia
->ia_addr
.sin_family
!= AF_INET
) {
1617 panic("attempt to insert wrong ia %p into hash table\n", ia
);
1619 } else if (IA_IS_HASHED(ia
)) {
1620 panic("attempt to double-insert ia %p into hash table\n", ia
);
1623 TAILQ_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1625 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1629 * Some point to point interfaces that are tunnels borrow the address from
1630 * an underlying interface (e.g. VPN server). In order for source address
1631 * selection logic to find the underlying interface first, we add the address
1632 * of borrowing point to point interfaces at the end of the list.
1633 * (see rdar://6733789)
1635 * Caller must hold in_ifaddr_rwlock as writer.
1638 in_iahash_insert_ptp(struct in_ifaddr
*ia
)
1640 struct in_ifaddr
*tmp_ifa
;
1641 struct ifnet
*tmp_ifp
;
1643 LCK_RW_ASSERT(in_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
1644 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1646 if (ia
->ia_addr
.sin_family
!= AF_INET
) {
1647 panic("attempt to insert wrong ia %p into hash table\n", ia
);
1649 } else if (IA_IS_HASHED(ia
)) {
1650 panic("attempt to double-insert ia %p into hash table\n", ia
);
1653 IFA_UNLOCK(&ia
->ia_ifa
);
1654 TAILQ_FOREACH(tmp_ifa
, INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1656 IFA_LOCK(&tmp_ifa
->ia_ifa
);
1657 /* ia->ia_addr won't change, so check without lock */
1658 if (IA_SIN(tmp_ifa
)->sin_addr
.s_addr
==
1659 ia
->ia_addr
.sin_addr
.s_addr
) {
1660 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
1663 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
1665 tmp_ifp
= (tmp_ifa
== NULL
) ? NULL
: tmp_ifa
->ia_ifp
;
1667 IFA_LOCK(&ia
->ia_ifa
);
1668 if (tmp_ifp
== NULL
) {
1669 TAILQ_INSERT_HEAD(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1672 TAILQ_INSERT_TAIL(INADDR_HASH(ia
->ia_addr
.sin_addr
.s_addr
),
1675 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1679 * Initialize an interface's internet address
1680 * and routing table entry.
1683 in_ifinit(struct ifnet
*ifp
, struct in_ifaddr
*ia
, struct sockaddr_in
*sin
,
1686 u_int32_t i
= ntohl(sin
->sin_addr
.s_addr
);
1687 struct sockaddr_in oldaddr
;
1688 int flags
= RTF_UP
, error
;
1689 struct ifaddr
*ifa0
;
1693 /* Take an extra reference for this routine */
1694 IFA_ADDREF(&ia
->ia_ifa
);
1696 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1697 IFA_LOCK(&ia
->ia_ifa
);
1698 oldaddr
= ia
->ia_addr
;
1699 if (IA_IS_HASHED(ia
)) {
1701 in_iahash_remove(ia
);
1705 * Interface addresses should not contain port or sin_zero information.
1707 SIN(&ia
->ia_addr
)->sin_family
= AF_INET
;
1708 SIN(&ia
->ia_addr
)->sin_len
= sizeof(struct sockaddr_in
);
1709 SIN(&ia
->ia_addr
)->sin_port
= 0;
1710 bzero(&SIN(&ia
->ia_addr
)->sin_zero
, sizeof(sin
->sin_zero
));
1711 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1712 in_iahash_insert_ptp(ia
);
1714 in_iahash_insert(ia
);
1716 IFA_UNLOCK(&ia
->ia_ifa
);
1717 lck_rw_done(in_ifaddr_rwlock
);
1720 * Give the interface a chance to initialize if this is its first
1721 * address, and to validate the address if necessary. Send down
1722 * SIOCSIFADDR for first address, and SIOCAIFADDR for alias(es).
1723 * We find the first IPV4 address assigned to it and check if this
1724 * is the same as the one passed into this routine.
1726 ifa0
= ifa_ifpgetprimary(ifp
, AF_INET
);
1727 cmd
= (&ia
->ia_ifa
== ifa0
) ? SIOCSIFADDR
: SIOCAIFADDR
;
1728 error
= ifnet_ioctl(ifp
, PF_INET
, cmd
, ia
);
1729 if (error
== EOPNOTSUPP
) {
1733 * If we've just sent down SIOCAIFADDR, send another ioctl down
1734 * for SIOCSIFADDR for the first IPV4 address of the interface,
1735 * because an address change on one of the addresses will result
1736 * in the removal of the previous first IPV4 address. KDP needs
1737 * be reconfigured with the current primary IPV4 address.
1739 if (error
== 0 && cmd
== SIOCAIFADDR
) {
1741 * NOTE: SIOCSIFADDR is defined with struct ifreq
1742 * as parameter, but here we are sending it down
1743 * to the interface with a pointer to struct ifaddr,
1744 * for legacy reasons.
1746 error
= ifnet_ioctl(ifp
, PF_INET
, SIOCSIFADDR
, ifa0
);
1747 if (error
== EOPNOTSUPP
) {
1752 /* Release reference from ifa_ifpgetprimary() */
1756 lck_rw_lock_exclusive(in_ifaddr_rwlock
);
1757 IFA_LOCK(&ia
->ia_ifa
);
1758 if (IA_IS_HASHED(ia
)) {
1759 in_iahash_remove(ia
);
1761 ia
->ia_addr
= oldaddr
;
1763 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
1764 in_iahash_insert_ptp(ia
);
1766 in_iahash_insert(ia
);
1769 IFA_UNLOCK(&ia
->ia_ifa
);
1770 lck_rw_done(in_ifaddr_rwlock
);
1771 /* Release extra reference taken above */
1772 IFA_REMREF(&ia
->ia_ifa
);
1775 lck_mtx_lock(rnh_lock
);
1776 IFA_LOCK(&ia
->ia_ifa
);
1778 * Address has been initialized by the link resolver (ARP)
1779 * via ifnet_ioctl() above; it may now generate route(s).
1781 ia
->ia_ifa
.ifa_debug
&= ~IFD_NOTREADY
;
1783 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&oldaddr
;
1784 IFA_UNLOCK(&ia
->ia_ifa
);
1785 in_ifscrub(ifp
, ia
, 1);
1786 IFA_LOCK(&ia
->ia_ifa
);
1787 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1789 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
1791 ia
->ia_netmask
= IN_CLASSA_NET
;
1792 } else if (IN_CLASSB(i
)) {
1793 ia
->ia_netmask
= IN_CLASSB_NET
;
1795 ia
->ia_netmask
= IN_CLASSC_NET
;
1798 * The subnet mask usually includes at least the standard network part,
1799 * but may may be smaller in the case of supernetting.
1800 * If it is set, we believe it.
1802 if (ia
->ia_subnetmask
== 0) {
1803 ia
->ia_subnetmask
= ia
->ia_netmask
;
1804 ia
->ia_sockmask
.sin_addr
.s_addr
= htonl(ia
->ia_subnetmask
);
1806 ia
->ia_netmask
&= ia
->ia_subnetmask
;
1808 ia
->ia_net
= i
& ia
->ia_netmask
;
1809 ia
->ia_subnet
= i
& ia
->ia_subnetmask
;
1810 in_socktrim(&ia
->ia_sockmask
);
1812 * Add route for the network.
1814 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1815 if (ifp
->if_flags
& IFF_BROADCAST
) {
1816 ia
->ia_broadaddr
.sin_addr
.s_addr
=
1817 htonl(ia
->ia_subnet
| ~ia
->ia_subnetmask
);
1818 ia
->ia_netbroadcast
.s_addr
=
1819 htonl(ia
->ia_net
| ~ia
->ia_netmask
);
1820 } else if (ifp
->if_flags
& IFF_LOOPBACK
) {
1821 ia
->ia_ifa
.ifa_dstaddr
= ia
->ia_ifa
.ifa_addr
;
1823 } else if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1824 if (ia
->ia_dstaddr
.sin_family
!= AF_INET
) {
1825 IFA_UNLOCK(&ia
->ia_ifa
);
1826 lck_mtx_unlock(rnh_lock
);
1827 /* Release extra reference taken above */
1828 IFA_REMREF(&ia
->ia_ifa
);
1831 ia
->ia_dstaddr
.sin_len
= sizeof(struct sockaddr_in
);
1834 IFA_UNLOCK(&ia
->ia_ifa
);
1836 if ((error
= rtinit_locked(&(ia
->ia_ifa
), (int)RTM_ADD
, flags
)) == 0) {
1837 IFA_LOCK(&ia
->ia_ifa
);
1838 ia
->ia_flags
|= IFA_ROUTE
;
1839 IFA_UNLOCK(&ia
->ia_ifa
);
1841 lck_mtx_unlock(rnh_lock
);
1843 /* XXX check if the subnet route points to the same interface */
1844 if (error
== EEXIST
) {
1849 * If the interface supports multicast, join the "all hosts"
1850 * multicast group on that interface.
1852 if (ifp
->if_flags
& IFF_MULTICAST
) {
1853 struct in_addr addr
;
1855 lck_mtx_lock(&ifp
->if_addrconfig_lock
);
1856 addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1857 if (ifp
->if_allhostsinm
== NULL
) {
1858 struct in_multi
*inm
;
1859 inm
= in_addmulti(&addr
, ifp
);
1863 * Keep the reference on inm added by
1864 * in_addmulti above for storing the
1865 * pointer in allhostsinm.
1867 ifp
->if_allhostsinm
= inm
;
1869 printf("%s: failed to add membership to "
1870 "all-hosts multicast address on %s\n",
1871 __func__
, if_name(ifp
));
1874 lck_mtx_unlock(&ifp
->if_addrconfig_lock
);
1877 /* Release extra reference taken above */
1878 IFA_REMREF(&ia
->ia_ifa
);
1881 /* invalidate route caches */
1882 routegenid_inet_update();
1889 * Return TRUE if the address might be a local broadcast address.
1892 in_broadcast(struct in_addr in
, struct ifnet
*ifp
)
1897 if (in
.s_addr
== INADDR_BROADCAST
|| in
.s_addr
== INADDR_ANY
) {
1900 if (!(ifp
->if_flags
& IFF_BROADCAST
)) {
1903 t
= ntohl(in
.s_addr
);
1906 * Look through the list of addresses for a match
1907 * with a broadcast address.
1909 #define ia ((struct in_ifaddr *)ifa)
1910 ifnet_lock_shared(ifp
);
1911 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1913 if (ifa
->ifa_addr
->sa_family
== AF_INET
&&
1914 (in
.s_addr
== ia
->ia_broadaddr
.sin_addr
.s_addr
||
1915 in
.s_addr
== ia
->ia_netbroadcast
.s_addr
||
1917 * Check for old-style (host 0) broadcast.
1919 t
== ia
->ia_subnet
|| t
== ia
->ia_net
) &&
1921 * Check for an all one subnetmask. These
1922 * only exist when an interface gets a secondary
1925 ia
->ia_subnetmask
!= (u_int32_t
)0xffffffff) {
1927 ifnet_lock_done(ifp
);
1932 ifnet_lock_done(ifp
);
1938 in_purgeaddrs(struct ifnet
*ifp
)
1940 struct ifaddr
**ifap
;
1943 VERIFY(ifp
!= NULL
);
1946 * Be nice, and try the civilized way first. If we can't get
1947 * rid of them this way, then do it the rough way. We must
1948 * only get here during detach time, after the ifnet has been
1949 * removed from the global list and arrays.
1951 err
= ifnet_get_address_list_family_internal(ifp
, &ifap
, AF_INET
, 1,
1953 if (err
== 0 && ifap
!= NULL
) {
1956 bzero(&ifr
, sizeof(ifr
));
1957 (void) snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
),
1958 "%s", if_name(ifp
));
1960 for (i
= 0; ifap
[i
] != NULL
; i
++) {
1965 bcopy(ifa
->ifa_addr
, &ifr
.ifr_addr
,
1966 sizeof(struct sockaddr_in
));
1968 err
= in_control(NULL
, SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
1970 /* if we lost the race, ignore it */
1971 if (err
== EADDRNOTAVAIL
) {
1975 char s_addr
[MAX_IPv4_STR_LEN
];
1976 char s_dstaddr
[MAX_IPv4_STR_LEN
];
1977 struct in_addr
*s
, *d
;
1980 s
= &((struct sockaddr_in
*)
1981 (void *)ifa
->ifa_addr
)->sin_addr
;
1982 d
= &((struct sockaddr_in
*)
1983 (void *)ifa
->ifa_dstaddr
)->sin_addr
;
1984 (void) inet_ntop(AF_INET
, &s
->s_addr
, s_addr
,
1986 (void) inet_ntop(AF_INET
, &d
->s_addr
, s_dstaddr
,
1990 printf("%s: SIOCDIFADDR ifp=%s ifa_addr=%s "
1991 "ifa_dstaddr=%s (err=%d)\n", __func__
,
1992 ifp
->if_xname
, s_addr
, s_dstaddr
, err
);
1995 ifnet_free_address_list(ifap
);
1996 } else if (err
!= 0 && err
!= ENXIO
) {
1997 printf("%s: error retrieving list of AF_INET addresses for "
1998 "ifp=%s (err=%d)\n", __func__
, ifp
->if_xname
, err
);
2003 * Called as part of ip_init
2006 in_ifaddr_init(void)
2010 PE_parse_boot_argn("ifa_debug", &inifa_debug
, sizeof(inifa_debug
));
2012 inifa_size
= (inifa_debug
== 0) ? sizeof(struct in_ifaddr
) :
2013 sizeof(struct in_ifaddr_dbg
);
2015 inifa_zone
= zinit(inifa_size
, INIFA_ZONE_MAX
* inifa_size
,
2016 0, INIFA_ZONE_NAME
);
2017 if (inifa_zone
== NULL
) {
2018 panic("%s: failed allocating %s", __func__
, INIFA_ZONE_NAME
);
2021 zone_change(inifa_zone
, Z_EXPAND
, TRUE
);
2022 zone_change(inifa_zone
, Z_CALLERACCT
, FALSE
);
2024 lck_mtx_init(&inifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
2025 TAILQ_INIT(&inifa_trash_head
);
2028 static struct in_ifaddr
*
2029 in_ifaddr_alloc(int how
)
2031 struct in_ifaddr
*inifa
;
2033 inifa
= (how
== M_WAITOK
) ? zalloc(inifa_zone
) :
2034 zalloc_noblock(inifa_zone
);
2035 if (inifa
!= NULL
) {
2036 bzero(inifa
, inifa_size
);
2037 inifa
->ia_ifa
.ifa_free
= in_ifaddr_free
;
2038 inifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
2039 ifa_lock_init(&inifa
->ia_ifa
);
2040 if (inifa_debug
!= 0) {
2041 struct in_ifaddr_dbg
*inifa_dbg
=
2042 (struct in_ifaddr_dbg
*)inifa
;
2043 inifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
2044 inifa
->ia_ifa
.ifa_trace
= in_ifaddr_trace
;
2045 inifa
->ia_ifa
.ifa_attached
= in_ifaddr_attached
;
2046 inifa
->ia_ifa
.ifa_detached
= in_ifaddr_detached
;
2047 ctrace_record(&inifa_dbg
->inifa_alloc
);
2054 in_ifaddr_free(struct ifaddr
*ifa
)
2056 IFA_LOCK_ASSERT_HELD(ifa
);
2058 if (ifa
->ifa_refcnt
!= 0) {
2059 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
2062 if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
2063 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
2066 if (ifa
->ifa_debug
& IFD_DEBUG
) {
2067 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2068 ctrace_record(&inifa_dbg
->inifa_free
);
2069 bcopy(&inifa_dbg
->inifa
, &inifa_dbg
->inifa_old
,
2070 sizeof(struct in_ifaddr
));
2071 if (ifa
->ifa_debug
& IFD_TRASHED
) {
2072 /* Become a regular mutex, just in case */
2073 IFA_CONVERT_LOCK(ifa
);
2074 lck_mtx_lock(&inifa_trash_lock
);
2075 TAILQ_REMOVE(&inifa_trash_head
, inifa_dbg
,
2077 lck_mtx_unlock(&inifa_trash_lock
);
2078 ifa
->ifa_debug
&= ~IFD_TRASHED
;
2082 ifa_lock_destroy(ifa
);
2083 bzero(ifa
, sizeof(struct in_ifaddr
));
2084 zfree(inifa_zone
, ifa
);
2088 in_ifaddr_attached(struct ifaddr
*ifa
)
2090 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2092 IFA_LOCK_ASSERT_HELD(ifa
);
2094 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
2095 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
2098 if (ifa
->ifa_debug
& IFD_TRASHED
) {
2099 /* Become a regular mutex, just in case */
2100 IFA_CONVERT_LOCK(ifa
);
2101 lck_mtx_lock(&inifa_trash_lock
);
2102 TAILQ_REMOVE(&inifa_trash_head
, inifa_dbg
, inifa_trash_link
);
2103 lck_mtx_unlock(&inifa_trash_lock
);
2104 ifa
->ifa_debug
&= ~IFD_TRASHED
;
2109 in_ifaddr_detached(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
);
2118 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
2119 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
2122 ifa
->ifa_debug
|= IFD_TRASHED
;
2123 /* Become a regular mutex, just in case */
2124 IFA_CONVERT_LOCK(ifa
);
2125 lck_mtx_lock(&inifa_trash_lock
);
2126 TAILQ_INSERT_TAIL(&inifa_trash_head
, inifa_dbg
, inifa_trash_link
);
2127 lck_mtx_unlock(&inifa_trash_lock
);
2131 in_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
2133 struct in_ifaddr_dbg
*inifa_dbg
= (struct in_ifaddr_dbg
*)ifa
;
2138 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
2139 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
2143 cnt
= &inifa_dbg
->inifa_refhold_cnt
;
2144 tr
= inifa_dbg
->inifa_refhold
;
2146 cnt
= &inifa_dbg
->inifa_refrele_cnt
;
2147 tr
= inifa_dbg
->inifa_refrele
;
2150 idx
= atomic_add_16_ov(cnt
, 1) % INIFA_TRACE_HIST_SIZE
;
2151 ctrace_record(&tr
[idx
]);
2155 * Handle SIOCGASSOCIDS ioctl for PF_INET domain.
2158 in_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
2160 struct inpcb
*inp
= sotoinpcb(so
);
2163 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
2167 /* INPCB has no concept of association */
2168 aid
= SAE_ASSOCID_ANY
;
2171 /* just asking how many there are? */
2172 if (aidp
== USER_ADDR_NULL
) {
2176 return copyout(&aid
, aidp
, sizeof(aid
));
2180 * Handle SIOCGCONNIDS ioctl for PF_INET domain.
2183 in_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
2186 struct inpcb
*inp
= sotoinpcb(so
);
2189 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
2193 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
2197 /* if connected, return 1 connection count */
2198 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
2200 /* just asking how many there are? */
2201 if (cidp
== USER_ADDR_NULL
) {
2205 /* if INPCB is connected, assign it connid 1 */
2206 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
2208 return copyout(&cid
, cidp
, sizeof(cid
));
2212 * Handle SIOCGCONNINFO ioctl for PF_INET domain.
2215 in_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
2216 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
2217 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
2218 user_addr_t aux_data
, uint32_t *aux_len
)
2220 struct inpcb
*inp
= sotoinpcb(so
);
2221 struct sockaddr_in sin
;
2222 struct ifnet
*ifp
= NULL
;
2224 u_int32_t copy_len
= 0;
2227 * Don't test for INPCB_STATE_DEAD since this may be called
2228 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
2235 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
2240 ifp
= inp
->inp_last_outifp
;
2241 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
2242 *soerror
= so
->so_error
;
2244 if (so
->so_state
& SS_ISCONNECTED
) {
2245 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
2247 if (inp
->inp_flags
& INP_BOUND_IF
) {
2248 *flags
|= CIF_BOUND_IF
;
2250 if (!(inp
->inp_flags
& INP_INADDR_ANY
)) {
2251 *flags
|= CIF_BOUND_IP
;
2253 if (!(inp
->inp_flags
& INP_ANONPORT
)) {
2254 *flags
|= CIF_BOUND_PORT
;
2257 bzero(&sin
, sizeof(sin
));
2258 sin
.sin_len
= sizeof(sin
);
2259 sin
.sin_family
= AF_INET
;
2261 /* source address and port */
2262 sin
.sin_port
= inp
->inp_lport
;
2263 sin
.sin_addr
.s_addr
= inp
->inp_laddr
.s_addr
;
2264 if (*src_len
== 0) {
2265 *src_len
= sin
.sin_len
;
2267 if (src
!= USER_ADDR_NULL
) {
2268 copy_len
= min(*src_len
, sizeof(sin
));
2269 error
= copyout(&sin
, src
, copy_len
);
2273 *src_len
= copy_len
;
2277 /* destination address and port */
2278 sin
.sin_port
= inp
->inp_fport
;
2279 sin
.sin_addr
.s_addr
= inp
->inp_faddr
.s_addr
;
2280 if (*dst_len
== 0) {
2281 *dst_len
= sin
.sin_len
;
2283 if (dst
!= USER_ADDR_NULL
) {
2284 copy_len
= min(*dst_len
, sizeof(sin
));
2285 error
= copyout(&sin
, dst
, copy_len
);
2289 *dst_len
= copy_len
;
2293 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
2294 struct conninfo_tcp tcp_ci
;
2296 *aux_type
= CIAUX_TCP
;
2297 if (*aux_len
== 0) {
2298 *aux_len
= sizeof(tcp_ci
);
2300 if (aux_data
!= USER_ADDR_NULL
) {
2301 copy_len
= min(*aux_len
, sizeof(tcp_ci
));
2302 bzero(&tcp_ci
, sizeof(tcp_ci
));
2303 tcp_getconninfo(so
, &tcp_ci
);
2304 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
2308 *aux_len
= copy_len
;
2321 struct llentry base
;
2324 #define IN_LLTBL_DEFAULT_HSIZE 32
2325 #define IN_LLTBL_HASH(k, h) \
2326 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
2329 * Do actual deallocation of @lle.
2332 in_lltable_destroy_lle_unlocked(struct llentry
*lle
)
2334 LLE_LOCK_DESTROY(lle
);
2335 LLE_REQ_DESTROY(lle
);
2336 FREE(lle
, M_LLTABLE
);
2340 * Called by LLE_FREE_LOCKED when number of references
2344 in_lltable_destroy_lle(struct llentry
*lle
)
2347 in_lltable_destroy_lle_unlocked(lle
);
2350 static struct llentry
*
2351 in_lltable_new(struct in_addr addr4
, u_int flags
)
2353 #pragma unused(flags)
2354 struct in_llentry
*lle
;
2356 MALLOC(lle
, struct in_llentry
*, sizeof(struct in_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
2357 if (lle
== NULL
) { /* NB: caller generates msg */
2362 * For IPv4 this will trigger "arpresolve" to generate
2365 lle
->base
.la_expire
= net_uptime(); /* mark expired */
2366 lle
->base
.r_l3addr
.addr4
= addr4
;
2367 lle
->base
.lle_refcnt
= 1;
2368 lle
->base
.lle_free
= in_lltable_destroy_lle
;
2370 LLE_LOCK_INIT(&lle
->base
);
2371 LLE_REQ_INIT(&lle
->base
);
2372 //callout_init(&lle->base.lle_timer, 1);
2377 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
2378 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
2381 in_lltable_match_prefix(const struct sockaddr
*saddr
,
2382 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
)
2384 struct in_addr addr
, mask
, lle_addr
;
2386 addr
= ((const struct sockaddr_in
*)(const void *)saddr
)->sin_addr
;
2387 mask
= ((const struct sockaddr_in
*)(const void *)smask
)->sin_addr
;
2388 lle_addr
.s_addr
= ntohl(lle
->r_l3addr
.addr4
.s_addr
);
2390 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0) {
2394 if (lle
->la_flags
& LLE_IFADDR
) {
2396 * Delete LLE_IFADDR records IFF address & flag matches.
2397 * Note that addr is the interface address within prefix
2399 * Note also we should handle 'ifdown' cases without removing
2402 if (addr
.s_addr
== lle_addr
.s_addr
&& (flags
& LLE_STATIC
) != 0) {
2408 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2409 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
)) {
2417 in_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
2420 size_t pkts_dropped
;
2422 LLE_WLOCK_ASSERT(lle
);
2423 KASSERT(llt
!= NULL
, ("lltable is NULL"));
2425 /* Unlink entry from table if not already */
2426 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
2428 IF_AFDATA_WLOCK_ASSERT(ifp
, llt
->llt_af
);
2429 lltable_unlink_entry(llt
, lle
);
2434 if (callout_stop(&lle
->lle_timer
) > 0) {
2438 /* Drop hold queue */
2439 pkts_dropped
= llentry_free(lle
);
2440 arpstat
.dropped
+= pkts_dropped
;
2445 in_lltable_rtcheck(struct ifnet
*ifp
, u_int flags
, const struct sockaddr
*l3addr
)
2447 #pragma unused(flags)
2450 KASSERT(l3addr
->sa_family
== AF_INET
,
2451 ("sin_family %d", l3addr
->sa_family
));
2453 /* XXX rtalloc1 should take a const param */
2454 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
2455 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
2456 log(LOG_INFO
, "IPv4 address: \"%s\" is not on the network\n",
2457 inet_ntoa(((const struct sockaddr_in
*)(const void *)l3addr
)->sin_addr
));
2467 static inline uint32_t
2468 in_lltable_hash_dst(const struct in_addr dst
, uint32_t hsize
)
2470 return IN_LLTBL_HASH(dst
.s_addr
, hsize
);
2474 in_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
2476 return in_lltable_hash_dst(lle
->r_l3addr
.addr4
, hsize
);
2481 in_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
2483 struct sockaddr_in
*sin
;
2485 sin
= (struct sockaddr_in
*)(void *)sa
;
2486 bzero(sin
, sizeof(*sin
));
2487 sin
->sin_family
= AF_INET
;
2488 sin
->sin_len
= sizeof(*sin
);
2489 sin
->sin_addr
= lle
->r_l3addr
.addr4
;
2492 static inline struct llentry
*
2493 in_lltable_find_dst(struct lltable
*llt
, struct in_addr dst
)
2495 struct llentry
*lle
;
2496 struct llentries
*lleh
;
2499 hashidx
= in_lltable_hash_dst(dst
, llt
->llt_hsize
);
2500 lleh
= &llt
->lle_head
[hashidx
];
2501 LIST_FOREACH(lle
, lleh
, lle_next
) {
2502 if (lle
->la_flags
& LLE_DELETED
) {
2505 if (lle
->r_l3addr
.addr4
.s_addr
== dst
.s_addr
) {
2514 in_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
2517 lle
->la_flags
|= LLE_DELETED
;
2518 //EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2520 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
2525 static struct llentry
*
2526 in_lltable_alloc(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
)
2528 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*) (const void *)l3addr
;
2529 struct ifnet
*ifp
= llt
->llt_ifp
;
2530 struct llentry
*lle
;
2532 KASSERT(l3addr
->sa_family
== AF_INET
,
2533 ("sin_family %d", l3addr
->sa_family
));
2536 * A route that covers the given address must have
2537 * been installed 1st because we are doing a resolution,
2540 if (!(flags
& LLE_IFADDR
) &&
2541 in_lltable_rtcheck(ifp
, flags
, l3addr
) != 0) {
2545 lle
= in_lltable_new(sin
->sin_addr
, flags
);
2547 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
2550 lle
->la_flags
= flags
& ~LLE_CREATE
;
2551 if (flags
& LLE_STATIC
) {
2552 lle
->r_flags
|= RLLE_VALID
;
2554 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
2555 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
2556 lle
->la_flags
|= LLE_STATIC
;
2557 lle
->r_flags
|= (RLLE_VALID
| RLLE_IFADDR
);
2563 * Return NULL if not found or marked for deletion.
2564 * If found return lle read locked.
2566 static struct llentry
*
2567 in_lltable_lookup(struct lltable
*llt
, u_int flags
, const struct sockaddr
*l3addr
)
2569 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)(const void *)l3addr
;
2570 struct llentry
*lle
;
2572 IF_AFDATA_WLOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
2574 KASSERT(l3addr
->sa_family
== AF_INET
,
2575 ("sin_family %d", l3addr
->sa_family
));
2576 lle
= in_lltable_find_dst(llt
, sin
->sin_addr
);
2582 KASSERT((flags
& (LLE_UNLOCKED
| LLE_EXCLUSIVE
)) !=
2583 (LLE_UNLOCKED
| LLE_EXCLUSIVE
), ("wrong lle request flags: 0x%X",
2586 if (flags
& LLE_UNLOCKED
) {
2590 if (flags
& LLE_EXCLUSIVE
) {
2600 in_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
2601 struct sysctl_req
*wr
)
2603 struct ifnet
*ifp
= llt
->llt_ifp
;
2606 struct rt_msghdr rtm
;
2607 struct sockaddr_in sin
;
2608 struct sockaddr_dl sdl
;
2610 struct sockaddr_dl
*sdl
;
2613 bzero(&arpc
, sizeof(arpc
));
2614 /* skip deleted entries */
2615 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
) {
2618 /* Skip if jailed and not a valid IP of the prison. */
2619 lltable_fill_sa_entry(lle
, (struct sockaddr
*)&arpc
.sin
);
2621 * produce a msg made of:
2623 * struct sockaddr_in; (IPv4)
2624 * struct sockaddr_dl;
2626 arpc
.rtm
.rtm_msglen
= sizeof(arpc
);
2627 arpc
.rtm
.rtm_version
= RTM_VERSION
;
2628 arpc
.rtm
.rtm_type
= RTM_GET
;
2629 arpc
.rtm
.rtm_flags
= RTF_UP
;
2630 arpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
2633 if (lle
->la_flags
& LLE_PUB
) {
2634 arpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
2638 sdl
->sdl_family
= AF_LINK
;
2639 sdl
->sdl_len
= sizeof(*sdl
);
2640 sdl
->sdl_index
= ifp
->if_index
;
2641 sdl
->sdl_type
= ifp
->if_type
;
2642 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
2643 sdl
->sdl_alen
= ifp
->if_addrlen
;
2644 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
2647 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
2650 arpc
.rtm
.rtm_rmx
.rmx_expire
=
2651 lle
->la_flags
& LLE_STATIC
? 0 : lle
->la_expire
;
2652 arpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
2653 if (lle
->la_flags
& LLE_STATIC
) {
2654 arpc
.rtm
.rtm_flags
|= RTF_STATIC
;
2656 if (lle
->la_flags
& LLE_IFADDR
) {
2657 arpc
.rtm
.rtm_flags
|= RTF_PINNED
;
2659 arpc
.rtm
.rtm_flags
|= RTF_PINNED
;
2660 arpc
.rtm
.rtm_index
= ifp
->if_index
;
2661 error
= SYSCTL_OUT(wr
, &arpc
, sizeof(arpc
));
2666 static struct lltable
*
2667 in_lltattach(struct ifnet
*ifp
)
2669 struct lltable
*llt
;
2671 llt
= lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE
);
2672 llt
->llt_af
= AF_INET
;
2675 llt
->llt_lookup
= in_lltable_lookup
;
2676 llt
->llt_alloc_entry
= in_lltable_alloc
;
2677 llt
->llt_delete_entry
= in_lltable_delete_entry
;
2678 llt
->llt_dump_entry
= in_lltable_dump_entry
;
2679 llt
->llt_hash
= in_lltable_hash
;
2680 llt
->llt_fill_sa_entry
= in_lltable_fill_sa_entry
;
2681 llt
->llt_free_entry
= in_lltable_free_entry
;
2682 llt
->llt_match_prefix
= in_lltable_match_prefix
;
2689 inifa_ifpwithflag(struct ifnet
* ifp
, uint32_t flag
)
2693 ifnet_lock_shared(ifp
);
2694 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_link
)
2697 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2701 if ((((struct in_ifaddr
*)ifa
)->ia_flags
& flag
) == flag
) {
2702 IFA_ADDREF_LOCKED(ifa
);
2708 ifnet_lock_done(ifp
);
2710 return (struct in_ifaddr
*)ifa
;
2714 inifa_ifpclatv4(struct ifnet
* ifp
)
2718 ifnet_lock_shared(ifp
);
2719 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_link
)
2723 if (ifa
->ifa_addr
->sa_family
!= AF_INET
) {
2728 addr
= ntohl(SIN(ifa
->ifa_addr
)->sin_addr
.s_addr
);
2729 if (!IN_LINKLOCAL(addr
) &&
2730 !IN_LOOPBACK(addr
)) {
2731 IFA_ADDREF_LOCKED(ifa
);
2737 ifnet_lock_done(ifp
);
2739 return (struct in_ifaddr
*)ifa
;