]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / netinet / in.c
CommitLineData
1c79356b 1/*
eb6b6ca3 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)in.c 8.4 (Berkeley) 1/9/95
61 */
62
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>
68#include <sys/proc.h>
69#include <sys/socket.h>
70#include <sys/kernel.h>
71#include <sys/sysctl.h>
72#include <sys/kern_event.h>
91447636 73#include <sys/syslog.h>
6d2010ae 74#include <sys/mcache.h>
39236c6e
A
75#include <sys/protosw.h>
76#include <sys/file.h>
1c79356b 77
39236c6e 78#include <kern/zalloc.h>
2d21ac55 79#include <pexpert/pexpert.h>
cb323159 80#include <os/log.h>
2d21ac55 81
1c79356b 82#include <net/if.h>
1c79356b 83#include <net/if_types.h>
9bccf70c 84#include <net/route.h>
2d21ac55 85#include <net/kpi_protocol.h>
39236c6e 86#include <net/dlil.h>
5ba3f43e
A
87#include <net/if_llatbl.h>
88#include <net/if_arp.h>
39236c6e
A
89#if PF
90#include <net/pfvar.h>
91#endif /* PF */
1c79356b
A
92
93#include <netinet/in.h>
94#include <netinet/in_var.h>
95#include <netinet/in_pcb.h>
1c79356b 96#include <netinet/igmp_var.h>
1c79356b 97#include <netinet/ip_var.h>
1c79356b
A
98#include <netinet/tcp.h>
99#include <netinet/tcp_timer.h>
100#include <netinet/tcp_var.h>
5ba3f43e 101#include <netinet/if_ether.h>
1c79356b 102
39236c6e
A
103static int inctl_associd(struct socket *, u_long, caddr_t);
104static int inctl_connid(struct socket *, u_long, caddr_t);
105static int inctl_conninfo(struct socket *, u_long, caddr_t);
106static int inctl_autoaddr(struct ifnet *, struct ifreq *);
107static int inctl_arpipll(struct ifnet *, struct ifreq *);
108static int inctl_setrouter(struct ifnet *, struct ifreq *);
109static int inctl_ifaddr(struct ifnet *, struct in_ifaddr *, u_long,
110 struct ifreq *);
39236c6e
A
111static int inctl_ifdstaddr(struct ifnet *, struct in_ifaddr *, u_long,
112 struct ifreq *);
113static int inctl_ifbrdaddr(struct ifnet *, struct in_ifaddr *, u_long,
114 struct ifreq *);
115static int inctl_ifnetmask(struct ifnet *, struct in_ifaddr *, u_long,
116 struct ifreq *);
1c79356b 117
39236c6e
A
118static void in_socktrim(struct sockaddr_in *);
119static int in_ifinit(struct ifnet *, struct in_ifaddr *,
120 struct sockaddr_in *, int);
1c79356b 121
0a7de745
A
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; \
b0d623f7
A
125}
126
0a7de745
A
127#define IA_IS_HASHED(ia) \
128 (!((ia)->ia_hash.tqe_next == (void *)(uintptr_t)-1 || \
b0d623f7
A
129 (ia)->ia_hash.tqe_prev == (void *)(uintptr_t)-1))
130
131static void in_iahash_remove(struct in_ifaddr *);
132static void in_iahash_insert(struct in_ifaddr *);
133static void in_iahash_insert_ptp(struct in_ifaddr *);
134static struct in_ifaddr *in_ifaddr_alloc(int);
6d2010ae
A
135static void in_ifaddr_attached(struct ifaddr *);
136static void in_ifaddr_detached(struct ifaddr *);
b0d623f7
A
137static void in_ifaddr_free(struct ifaddr *);
138static void in_ifaddr_trace(struct ifaddr *, int);
139
39236c6e 140static int in_getassocids(struct socket *, uint32_t *, user_addr_t);
3e170ce0 141static int in_getconnids(struct socket *, sae_associd_t, uint32_t *, user_addr_t);
5ba3f43e
A
142
143/* IPv4 Layer 2 neighbor cache management routines */
144static void in_lltable_destroy_lle_unlocked(struct llentry *lle);
145static void in_lltable_destroy_lle(struct llentry *lle);
f427ee49 146static struct llentry *in_lltable_new(struct in_addr addr4, uint16_t flags);
5ba3f43e 147static int in_lltable_match_prefix(const struct sockaddr *saddr,
f427ee49 148 const struct sockaddr *smask, uint16_t flags, struct llentry *lle);
5ba3f43e 149static void in_lltable_free_entry(struct lltable *llt, struct llentry *lle);
f427ee49 150static int in_lltable_rtcheck(struct ifnet *ifp, uint16_t flags, const struct sockaddr *l3addr);
5ba3f43e
A
151static inline uint32_t in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize);
152static uint32_t in_lltable_hash(const struct llentry *lle, uint32_t hsize);
153static void in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
154static inline struct llentry * in_lltable_find_dst(struct lltable *llt, struct in_addr dst);
155static void in_lltable_delete_entry(struct lltable *llt, struct llentry *lle);
f427ee49
A
156static struct llentry * in_lltable_alloc(struct lltable *llt, uint16_t flags, const struct sockaddr *l3addr);
157static struct llentry * in_lltable_lookup(struct lltable *llt, uint16_t flags, const struct sockaddr *l3addr);
5ba3f43e
A
158static int in_lltable_dump_entry(struct lltable *llt, struct llentry *lle, struct sysctl_req *wr);
159static struct lltable * in_lltattach(struct ifnet *ifp);
39236c6e 160
1c79356b 161static int subnetsarelocal = 0;
39236c6e 162SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local,
0a7de745 163 CTLFLAG_RW | CTLFLAG_LOCKED, &subnetsarelocal, 0, "");
1c79356b 164
91447636 165/* Track whether or not the SIOCARPIPLL ioctl has been called */
39236c6e 166u_int32_t ipv4_ll_arp_aware = 0;
1c79356b 167
0a7de745 168#define INIFA_TRACE_HIST_SIZE 32 /* size of trace history */
6d2010ae
A
169
170/* For gdb */
171__private_extern__ unsigned int inifa_trace_hist_size = INIFA_TRACE_HIST_SIZE;
172
b0d623f7 173struct in_ifaddr_dbg {
0a7de745
A
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 */
b0d623f7
A
178 /*
179 * Alloc and free callers.
180 */
0a7de745
A
181 ctrace_t inifa_alloc;
182 ctrace_t inifa_free;
b0d623f7 183 /*
6d2010ae 184 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
b0d623f7 185 */
0a7de745
A
186 ctrace_t inifa_refhold[INIFA_TRACE_HIST_SIZE];
187 ctrace_t inifa_refrele[INIFA_TRACE_HIST_SIZE];
6d2010ae
A
188 /*
189 * Trash list linkage
190 */
191 TAILQ_ENTRY(in_ifaddr_dbg) inifa_trash_link;
b0d623f7
A
192};
193
6d2010ae
A
194/* List of trash in_ifaddr entries protected by inifa_trash_lock */
195static TAILQ_HEAD(, in_ifaddr_dbg) inifa_trash_head;
196static decl_lck_mtx_data(, inifa_trash_lock);
197
198#if DEBUG
0a7de745 199static unsigned int inifa_debug = 1; /* debugging (enabled) */
6d2010ae 200#else
0a7de745 201static unsigned int inifa_debug; /* debugging (disabled) */
6d2010ae 202#endif /* !DEBUG */
0a7de745
A
203static unsigned int inifa_size; /* size of zone element */
204static struct zone *inifa_zone; /* zone for in_ifaddr */
b0d623f7 205
0a7de745 206#define INIFA_ZONE_NAME "in_ifaddr" /* zone name */
b0d623f7 207
0a7de745 208static const unsigned int in_extra_size = sizeof(struct in_ifextra);
3e170ce0 209static const unsigned int in_extra_bufsize = in_extra_size +
0a7de745 210 sizeof(void *) + sizeof(uint64_t);
3e170ce0 211
6d2010ae
A
212/*
213 * Return 1 if the address is
214 * - loopback
215 * - unicast or multicast link local
216 * - routed via a link level gateway
217 * - belongs to a directly connected (sub)net
218 */
2d21ac55
A
219int
220inaddr_local(struct in_addr in)
221{
222 struct rtentry *rt;
223 struct sockaddr_in sin;
224 int local = 0;
225
39236c6e
A
226 if (ntohl(in.s_addr) == INADDR_LOOPBACK ||
227 IN_LINKLOCAL(ntohl(in.s_addr))) {
6d2010ae
A
228 local = 1;
229 } else if (ntohl(in.s_addr) >= INADDR_UNSPEC_GROUP &&
39236c6e 230 ntohl(in.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
0a7de745 231 local = 1;
2d21ac55 232 } else {
6d2010ae 233 sin.sin_family = AF_INET;
0a7de745 234 sin.sin_len = sizeof(sin);
6d2010ae
A
235 sin.sin_addr = in;
236 rt = rtalloc1((struct sockaddr *)&sin, 0, 0);
237
238 if (rt != NULL) {
239 RT_LOCK_SPIN(rt);
240 if (rt->rt_gateway->sa_family == AF_LINK ||
0a7de745 241 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
6d2010ae 242 local = 1;
0a7de745 243 }
6d2010ae
A
244 RT_UNLOCK(rt);
245 rtfree(rt);
246 } else {
247 local = in_localaddr(in);
248 }
2d21ac55 249 }
0a7de745 250 return local;
2d21ac55
A
251}
252
1c79356b
A
253/*
254 * Return 1 if an internet address is for a ``local'' host
255 * (one to which we have a connection). If subnetsarelocal
39037602
A
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.
1c79356b
A
259 */
260int
2d21ac55 261in_localaddr(struct in_addr in)
1c79356b 262{
b0d623f7 263 u_int32_t i = ntohl(in.s_addr);
91447636 264 struct in_ifaddr *ia;
1c79356b 265
0a7de745
A
266 if (IN_LINKLOCAL(i)) {
267 return 1;
268 }
39037602 269
1c79356b 270 if (subnetsarelocal) {
b0d623f7 271 lck_rw_lock_shared(in_ifaddr_rwlock);
39236c6e
A
272 for (ia = in_ifaddrhead.tqh_first; ia != NULL;
273 ia = ia->ia_link.tqe_next) {
6d2010ae 274 IFA_LOCK(&ia->ia_ifa);
91447636 275 if ((i & ia->ia_netmask) == ia->ia_net) {
6d2010ae 276 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 277 lck_rw_done(in_ifaddr_rwlock);
0a7de745 278 return 1;
91447636 279 }
6d2010ae
A
280 IFA_UNLOCK(&ia->ia_ifa);
281 }
b0d623f7 282 lck_rw_done(in_ifaddr_rwlock);
1c79356b 283 } else {
b0d623f7 284 lck_rw_lock_shared(in_ifaddr_rwlock);
39236c6e
A
285 for (ia = in_ifaddrhead.tqh_first; ia != NULL;
286 ia = ia->ia_link.tqe_next) {
6d2010ae 287 IFA_LOCK(&ia->ia_ifa);
91447636 288 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
6d2010ae 289 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 290 lck_rw_done(in_ifaddr_rwlock);
0a7de745 291 return 1;
91447636 292 }
6d2010ae
A
293 IFA_UNLOCK(&ia->ia_ifa);
294 }
b0d623f7 295 lck_rw_done(in_ifaddr_rwlock);
1c79356b 296 }
0a7de745 297 return 0;
1c79356b
A
298}
299
300/*
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
303 * may be forwarded.
304 */
39236c6e 305boolean_t
2d21ac55 306in_canforward(struct in_addr in)
1c79356b 307{
b0d623f7
A
308 u_int32_t i = ntohl(in.s_addr);
309 u_int32_t net;
1c79356b 310
0a7de745
A
311 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) {
312 return FALSE;
313 }
1c79356b
A
314 if (IN_CLASSA(i)) {
315 net = i & IN_CLASSA_NET;
0a7de745
A
316 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) {
317 return FALSE;
318 }
1c79356b 319 }
0a7de745 320 return TRUE;
1c79356b
A
321}
322
323/*
324 * Trim a mask in a sockaddr
325 */
326static void
2d21ac55 327in_socktrim(struct sockaddr_in *ap)
1c79356b 328{
39236c6e
A
329 char *cplim = (char *)&ap->sin_addr;
330 char *cp = (char *)(&ap->sin_addr + 1);
1c79356b 331
39236c6e 332 ap->sin_len = 0;
0a7de745 333 while (--cp >= cplim) {
39236c6e 334 if (*cp) {
f427ee49 335 (ap)->sin_len = (uint8_t)(cp - (char *)(ap) + 1);
39236c6e
A
336 break;
337 }
0a7de745 338 }
1c79356b
A
339}
340
0a7de745 341static int in_interfaces; /* number of external internet interfaces */
1c79356b 342
6d2010ae
A
343static int
344in_domifattach(struct ifnet *ifp)
345{
346 int error;
347
39236c6e
A
348 VERIFY(ifp != NULL);
349
3e170ce0 350 if ((error = proto_plumb(PF_INET, ifp)) && error != EEXIST) {
39236c6e
A
351 log(LOG_ERR, "%s: proto_plumb returned %d if=%s\n",
352 __func__, error, if_name(ifp));
3e170ce0
A
353 } else if (error == 0 && ifp->if_inetdata == NULL) {
354 void **pbuf, *base;
355 struct in_ifextra *ext;
356 int errorx;
357
358 if ((ext = (struct in_ifextra *)_MALLOC(in_extra_bufsize,
0a7de745 359 M_IFADDR, M_WAITOK | M_ZERO)) == NULL) {
3e170ce0
A
360 error = ENOMEM;
361 errorx = proto_unplumb(PF_INET, ifp);
362 if (errorx != 0) {
363 log(LOG_ERR,
364 "%s: proto_unplumb returned %d if=%s%d\n",
365 __func__, errorx, ifp->if_name,
366 ifp->if_unit);
367 }
368 goto done;
369 }
6d2010ae 370
3e170ce0 371 /* Align on 64-bit boundary */
0a7de745
A
372 base = (void *)P2ROUNDUP((intptr_t)ext + sizeof(uint64_t),
373 sizeof(uint64_t));
3e170ce0
A
374 VERIFY(((intptr_t)base + in_extra_size) <=
375 ((intptr_t)ext + in_extra_bufsize));
0a7de745 376 pbuf = (void **)((intptr_t)base - sizeof(void *));
3e170ce0
A
377 *pbuf = ext;
378 ifp->if_inetdata = base;
5ba3f43e 379 IN_IFEXTRA(ifp)->ii_llt = in_lltattach(ifp);
0a7de745 380 VERIFY(IS_P2ALIGNED(ifp->if_inetdata, sizeof(uint64_t)));
3e170ce0
A
381 }
382done:
383 if (error == 0 && ifp->if_inetdata != NULL) {
384 /*
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.
389 */
390 bzero(ifp->if_inetdata, in_extra_size);
391 }
0a7de745 392 return error;
6d2010ae
A
393}
394
39236c6e
A
395static __attribute__((noinline)) int
396inctl_associd(struct socket *so, u_long cmd, caddr_t data)
1c79356b 397{
2d21ac55 398 int error = 0;
39236c6e
A
399 union {
400 struct so_aidreq32 a32;
401 struct so_aidreq64 a64;
402 } u;
316670eb 403
39236c6e 404 VERIFY(so != NULL);
1c79356b 405
1c79356b 406 switch (cmd) {
0a7de745
A
407 case SIOCGASSOCIDS32: /* struct so_aidreq32 */
408 bcopy(data, &u.a32, sizeof(u.a32));
39236c6e 409 error = in_getassocids(so, &u.a32.sar_cnt, u.a32.sar_aidp);
0a7de745
A
410 if (error == 0) {
411 bcopy(&u.a32, data, sizeof(u.a32));
412 }
39236c6e 413 break;
316670eb 414
0a7de745
A
415 case SIOCGASSOCIDS64: /* struct so_aidreq64 */
416 bcopy(data, &u.a64, sizeof(u.a64));
f427ee49 417 error = in_getassocids(so, &u.a64.sar_cnt, (user_addr_t)u.a64.sar_aidp);
0a7de745
A
418 if (error == 0) {
419 bcopy(&u.a64, data, sizeof(u.a64));
420 }
39236c6e 421 break;
316670eb 422
39236c6e
A
423 default:
424 VERIFY(0);
425 /* NOTREACHED */
1c79356b
A
426 }
427
0a7de745 428 return error;
39236c6e 429}
316670eb 430
39236c6e
A
431static __attribute__((noinline)) int
432inctl_connid(struct socket *so, u_long cmd, caddr_t data)
433{
434 int error = 0;
435 union {
436 struct so_cidreq32 c32;
437 struct so_cidreq64 c64;
438 } u;
316670eb 439
39236c6e 440 VERIFY(so != NULL);
316670eb 441
39236c6e 442 switch (cmd) {
0a7de745
A
443 case SIOCGCONNIDS32: /* struct so_cidreq32 */
444 bcopy(data, &u.c32, sizeof(u.c32));
39236c6e
A
445 error = in_getconnids(so, u.c32.scr_aid, &u.c32.scr_cnt,
446 u.c32.scr_cidp);
0a7de745
A
447 if (error == 0) {
448 bcopy(&u.c32, data, sizeof(u.c32));
449 }
39236c6e
A
450 break;
451
0a7de745
A
452 case SIOCGCONNIDS64: /* struct so_cidreq64 */
453 bcopy(data, &u.c64, sizeof(u.c64));
39236c6e 454 error = in_getconnids(so, u.c64.scr_aid, &u.c64.scr_cnt,
f427ee49 455 (user_addr_t)u.c64.scr_cidp);
0a7de745
A
456 if (error == 0) {
457 bcopy(&u.c64, data, sizeof(u.c64));
458 }
39236c6e
A
459 break;
460
461 default:
462 VERIFY(0);
463 /* NOTREACHED */
91447636 464 }
316670eb 465
0a7de745 466 return error;
39236c6e
A
467}
468
469static __attribute__((noinline)) int
470inctl_conninfo(struct socket *so, u_long cmd, caddr_t data)
471{
472 int error = 0;
473 union {
474 struct so_cinforeq32 ci32;
475 struct so_cinforeq64 ci64;
476 } u;
477
478 VERIFY(so != NULL);
479
1c79356b 480 switch (cmd) {
0a7de745
A
481 case SIOCGCONNINFO32: /* struct so_cinforeq32 */
482 bcopy(data, &u.ci32, sizeof(u.ci32));
39236c6e
A
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);
0a7de745
A
488 if (error == 0) {
489 bcopy(&u.ci32, data, sizeof(u.ci32));
490 }
0b4e3aa0 491 break;
1c79356b 492
0a7de745
A
493 case SIOCGCONNINFO64: /* struct so_cinforeq64 */
494 bcopy(data, &u.ci64, sizeof(u.ci64));
39236c6e 495 error = in_getconninfo(so, u.ci64.scir_cid, &u.ci64.scir_flags,
f427ee49
A
496 &u.ci64.scir_ifindex, &u.ci64.scir_error, (user_addr_t)u.ci64.scir_src,
497 &u.ci64.scir_src_len, (user_addr_t)u.ci64.scir_dst, &u.ci64.scir_dst_len,
498 &u.ci64.scir_aux_type, (user_addr_t)u.ci64.scir_aux_data,
39236c6e 499 &u.ci64.scir_aux_len);
0a7de745
A
500 if (error == 0) {
501 bcopy(&u.ci64, data, sizeof(u.ci64));
502 }
39236c6e 503 break;
316670eb 504
39236c6e
A
505 default:
506 VERIFY(0);
507 /* NOTREACHED */
508 }
316670eb 509
0a7de745 510 return error;
39236c6e 511}
316670eb 512
39236c6e
A
513/*
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
516 * memory accesses.
517 */
518static __attribute__((noinline)) int
519inctl_autoaddr(struct ifnet *ifp, struct ifreq *ifr)
520{
521 int error = 0, intval;
2d21ac55 522
39236c6e
A
523 VERIFY(ifp != NULL);
524
0a7de745 525 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
39236c6e
A
526
527 ifnet_lock_exclusive(ifp);
528 if (intval) {
529 /*
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.
534 */
535 if (ifp->if_eflags & IFEF_IPV4_ROUTER) {
0a7de745 536 intval = 0; /* be safe; clear flag if set */
39236c6e
A
537 error = EBUSY;
538 } else {
f427ee49 539 if_set_eflags(ifp, IFEF_AUTOCONFIGURING);
2d21ac55 540 }
316670eb 541 }
0a7de745 542 if (!intval) {
f427ee49 543 if_clear_eflags(ifp, IFEF_AUTOCONFIGURING);
0a7de745 544 }
39236c6e 545 ifnet_lock_done(ifp);
316670eb 546
0a7de745 547 return error;
39236c6e
A
548}
549
550/*
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
553 * memory accesses.
554 */
555static __attribute__((noinline)) int
556inctl_arpipll(struct ifnet *ifp, struct ifreq *ifr)
557{
558 int error = 0, intval;
559
560 VERIFY(ifp != NULL);
561
0a7de745 562 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
39236c6e
A
563 ipv4_ll_arp_aware = 1;
564
565 ifnet_lock_exclusive(ifp);
566 if (intval) {
567 /*
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.
572 */
573 if (ifp->if_eflags & IFEF_IPV4_ROUTER) {
0a7de745 574 intval = 0; /* be safe; clear flag if set */
39236c6e 575 error = EBUSY;
316670eb 576 } else {
f427ee49 577 if_set_eflags(ifp, IFEF_ARPLL);
316670eb 578 }
39236c6e 579 }
0a7de745 580 if (!intval) {
f427ee49 581 if_clear_eflags(ifp, IFEF_ARPLL);
0a7de745 582 }
39236c6e 583 ifnet_lock_done(ifp);
316670eb 584
0a7de745 585 return error;
39236c6e
A
586}
587
588/*
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
594 * on the interface.
595 *
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
598 * memory accesses.
599 */
600static __attribute__((noinline)) int
601inctl_setrouter(struct ifnet *ifp, struct ifreq *ifr)
602{
603 int error = 0, intval;
604
605 VERIFY(ifp != NULL);
606
607 /* Router mode isn't valid for loopback */
0a7de745
A
608 if (ifp->if_flags & IFF_LOOPBACK) {
609 return ENODEV;
610 }
39236c6e 611
0a7de745 612 bcopy(&ifr->ifr_intval, &intval, sizeof(intval));
f427ee49
A
613 switch (intval) {
614 case 0:
615 case 1:
616 break;
617 default:
618 return EINVAL;
619 }
39236c6e 620 ifnet_lock_exclusive(ifp);
f427ee49
A
621 if (intval != 0) {
622 if_set_eflags(ifp, IFEF_IPV4_ROUTER);
623 if_clear_eflags(ifp, (IFEF_ARPLL | IFEF_AUTOCONFIGURING));
39236c6e 624 } else {
f427ee49 625 if_clear_eflags(ifp, IFEF_IPV4_ROUTER);
39236c6e
A
626 }
627 ifnet_lock_done(ifp);
628
629 /* purge all IPv4 addresses configured on this interface */
630 in_purgeaddrs(ifp);
631
0a7de745 632 return error;
39236c6e
A
633}
634
635/*
636 * Caller passes in the ioctl data pointer directly via "ifr", with the
637 * expectation that this routine always uses bcopy() or other byte-aligned
638 * memory accesses.
639 */
640static __attribute__((noinline)) int
641inctl_ifaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
642 struct ifreq *ifr)
643{
644 struct kev_in_data in_event_data;
645 struct kev_msg ev_msg;
646 struct sockaddr_in addr;
647 struct ifaddr *ifa;
648 int error = 0;
649
650 VERIFY(ifp != NULL);
651
0a7de745
A
652 bzero(&in_event_data, sizeof(struct kev_in_data));
653 bzero(&ev_msg, sizeof(struct kev_msg));
39236c6e
A
654
655 switch (cmd) {
0a7de745 656 case SIOCGIFADDR: /* struct ifreq */
39236c6e 657 if (ia == NULL) {
2d21ac55 658 error = EADDRNOTAVAIL;
39236c6e 659 break;
2d21ac55 660 }
6d2010ae 661 IFA_LOCK(&ia->ia_ifa);
0a7de745 662 bcopy(&ia->ia_addr, &ifr->ifr_addr, sizeof(addr));
6d2010ae 663 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
664 break;
665
0a7de745 666 case SIOCSIFADDR: /* struct ifreq */
316670eb 667 VERIFY(ia != NULL);
0a7de745 668 bcopy(&ifr->ifr_addr, &addr, sizeof(addr));
39236c6e
A
669 /*
670 * If this is a new address, the reference count for the
671 * hash table has been taken at creation time above.
672 */
673 error = in_ifinit(ifp, ia, &addr, 1);
674 if (error == 0) {
675 (void) ifnet_notify_address(ifp, AF_INET);
2d21ac55 676 }
1c79356b
A
677 break;
678
0a7de745 679 case SIOCAIFADDR: { /* struct {if,in_}aliasreq */
39236c6e
A
680 struct in_aliasreq *ifra = (struct in_aliasreq *)ifr;
681 struct sockaddr_in broadaddr, mask;
682 int hostIsNew, maskIsNew;
1c79356b 683
316670eb 684 VERIFY(ia != NULL);
0a7de745
A
685 bcopy(&ifra->ifra_addr, &addr, sizeof(addr));
686 bcopy(&ifra->ifra_broadaddr, &broadaddr, sizeof(broadaddr));
687 bcopy(&ifra->ifra_mask, &mask, sizeof(mask));
39236c6e
A
688
689 maskIsNew = 0;
690 hostIsNew = 1;
691 error = 0;
1c79356b 692
6d2010ae 693 IFA_LOCK(&ia->ia_ifa);
39236c6e
A
694 if (ia->ia_addr.sin_family == AF_INET) {
695 if (addr.sin_len == 0) {
696 addr = ia->ia_addr;
697 hostIsNew = 0;
698 } else if (addr.sin_addr.s_addr ==
699 ia->ia_addr.sin_addr.s_addr) {
700 hostIsNew = 0;
701 }
2d21ac55 702 }
eb6b6ca3 703 if (mask.sin_len != 0) {
6d2010ae 704 IFA_UNLOCK(&ia->ia_ifa);
39236c6e
A
705 in_ifscrub(ifp, ia, 0);
706 IFA_LOCK(&ia->ia_ifa);
f427ee49
A
707 ia->ia_sockmask.sin_len = sizeof(struct sockaddr_in);
708 ia->ia_sockmask.sin_family = AF_INET;
709 ia->ia_sockmask.sin_port = 0;
710 ia->ia_sockmask.sin_addr = mask.sin_addr;
711 bzero(&ia->ia_sockmask.sin_zero, sizeof(ia->ia_dstaddr.sin_zero));
39236c6e
A
712 ia->ia_subnetmask =
713 ntohl(ia->ia_sockmask.sin_addr.s_addr);
714 maskIsNew = 1;
1c79356b 715 }
39236c6e
A
716 if ((ifp->if_flags & IFF_POINTOPOINT) &&
717 (broadaddr.sin_family == AF_INET)) {
718 IFA_UNLOCK(&ia->ia_ifa);
719 in_ifscrub(ifp, ia, 0);
720 IFA_LOCK(&ia->ia_ifa);
eb6b6ca3 721 ia->ia_dstaddr.sin_family = AF_INET;
0a7de745 722 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
eb6b6ca3 723 ia->ia_dstaddr.sin_port = 0;
f427ee49 724 ia->ia_dstaddr.sin_addr = broadaddr.sin_addr;
eb6b6ca3 725 bzero(&ia->ia_dstaddr.sin_zero, sizeof(ia->ia_dstaddr.sin_zero));
39236c6e
A
726 maskIsNew = 1; /* We lie; but the effect's the same */
727 }
728 if (addr.sin_family == AF_INET && (hostIsNew || maskIsNew)) {
729 IFA_UNLOCK(&ia->ia_ifa);
730 error = in_ifinit(ifp, ia, &addr, 0);
316670eb 731 } else {
39236c6e 732 IFA_UNLOCK(&ia->ia_ifa);
316670eb 733 }
39236c6e
A
734 if (error == 0) {
735 (void) ifnet_notify_address(ifp, AF_INET);
736 }
737 IFA_LOCK(&ia->ia_ifa);
738 if ((ifp->if_flags & IFF_BROADCAST) &&
0a7de745 739 (broadaddr.sin_family == AF_INET)) {
eb6b6ca3
A
740 ia->ia_broadaddr.sin_family = AF_INET;
741 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
742 ia->ia_broadaddr.sin_port = 0;
743 ia->ia_broadaddr.sin_addr = broadaddr.sin_addr;
744 bzero(&ia->ia_broadaddr.sin_zero, sizeof(ia->ia_broadaddr.sin_zero));
0a7de745 745 }
1c79356b 746
39236c6e
A
747 /*
748 * Report event.
749 */
750 if ((error == 0) || (error == EEXIST)) {
0a7de745
A
751 ev_msg.vendor_code = KEV_VENDOR_APPLE;
752 ev_msg.kev_class = KEV_NETWORK_CLASS;
753 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
1c79356b 754
0a7de745 755 if (hostIsNew) {
39236c6e 756 ev_msg.event_code = KEV_INET_NEW_ADDR;
0a7de745 757 } else {
39236c6e 758 ev_msg.event_code = KEV_INET_CHANGED_ADDR;
0a7de745 759 }
1c79356b 760
39236c6e
A
761 if (ia->ia_ifa.ifa_dstaddr) {
762 in_event_data.ia_dstaddr =
763 ((struct sockaddr_in *)(void *)ia->
764 ia_ifa.ifa_dstaddr)->sin_addr;
765 } else {
766 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
767 }
0a7de745
A
768 in_event_data.ia_addr = ia->ia_addr.sin_addr;
769 in_event_data.ia_net = ia->ia_net;
770 in_event_data.ia_netmask = ia->ia_netmask;
771 in_event_data.ia_subnet = ia->ia_subnet;
772 in_event_data.ia_subnetmask = ia->ia_subnetmask;
773 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
6d2010ae 774 IFA_UNLOCK(&ia->ia_ifa);
fe8ab488 775 (void) strlcpy(&in_event_data.link_data.if_name[0],
39236c6e
A
776 ifp->if_name, IFNAMSIZ);
777 in_event_data.link_data.if_family = ifp->if_family;
778 in_event_data.link_data.if_unit = ifp->if_unit;
779
0a7de745
A
780 ev_msg.dv[0].data_ptr = &in_event_data;
781 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
39236c6e
A
782 ev_msg.dv[1].data_length = 0;
783
39037602 784 dlil_post_complete_msg(ifp, &ev_msg);
6d2010ae
A
785 } else {
786 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
787 }
788 break;
39236c6e 789 }
1c79356b 790
0a7de745 791 case SIOCDIFADDR: /* struct ifreq */
316670eb 792 VERIFY(ia != NULL);
39236c6e 793 error = ifnet_ioctl(ifp, PF_INET, SIOCDIFADDR, ia);
0a7de745 794 if (error == EOPNOTSUPP) {
39236c6e 795 error = 0;
0a7de745 796 }
813fb2f6 797 if (error != 0) {
2d21ac55 798 break;
813fb2f6 799 }
1c79356b 800
39236c6e 801 /* Fill out the kernel event information */
0a7de745
A
802 ev_msg.vendor_code = KEV_VENDOR_APPLE;
803 ev_msg.kev_class = KEV_NETWORK_CLASS;
804 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
6d2010ae 805
0a7de745 806 ev_msg.event_code = KEV_INET_ADDR_DELETED;
1c79356b 807
39236c6e 808 IFA_LOCK(&ia->ia_ifa);
316670eb
A
809 if (ia->ia_ifa.ifa_dstaddr) {
810 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
811 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
812 } else {
813 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
814 }
0a7de745
A
815 in_event_data.ia_addr = ia->ia_addr.sin_addr;
816 in_event_data.ia_net = ia->ia_net;
817 in_event_data.ia_netmask = ia->ia_netmask;
818 in_event_data.ia_subnet = ia->ia_subnet;
819 in_event_data.ia_subnetmask = ia->ia_subnetmask;
820 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
6d2010ae 821 IFA_UNLOCK(&ia->ia_ifa);
fe8ab488 822 (void) strlcpy(&in_event_data.link_data.if_name[0],
316670eb 823 ifp->if_name, IFNAMSIZ);
1c79356b 824 in_event_data.link_data.if_family = ifp->if_family;
39236c6e 825 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1c79356b
A
826
827 ev_msg.dv[0].data_ptr = &in_event_data;
39236c6e 828 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
1c79356b
A
829 ev_msg.dv[1].data_length = 0;
830
39236c6e
A
831 ifa = &ia->ia_ifa;
832 lck_rw_lock_exclusive(in_ifaddr_rwlock);
833 /* Release ia_link reference */
834 IFA_REMREF(ifa);
835 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
836 IFA_LOCK(ifa);
0a7de745 837 if (IA_IS_HASHED(ia)) {
39236c6e 838 in_iahash_remove(ia);
0a7de745 839 }
39236c6e
A
840 IFA_UNLOCK(ifa);
841 lck_rw_done(in_ifaddr_rwlock);
316670eb 842
b0d623f7 843 /*
39236c6e 844 * in_ifscrub kills the interface route.
b0d623f7 845 */
39236c6e
A
846 in_ifscrub(ifp, ia, 0);
847 ifnet_lock_exclusive(ifp);
848 IFA_LOCK(ifa);
849 /* if_detach_ifa() releases ifa_link reference */
850 if_detach_ifa(ifp, ifa);
851 /* Our reference to this address is dropped at the bottom */
852 IFA_UNLOCK(ifa);
1c79356b 853
39236c6e
A
854 /* invalidate route caches */
855 routegenid_inet_update();
6d2010ae 856
39236c6e
A
857 /*
858 * If the interface supports multicast, and no address is left,
859 * remove the "all hosts" multicast group from that interface.
6d2010ae 860 */
39236c6e
A
861 if ((ifp->if_flags & IFF_MULTICAST) ||
862 ifp->if_allhostsinm != NULL) {
39236c6e
A
863 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
864 IFA_LOCK(ifa);
865 if (ifa->ifa_addr->sa_family == AF_INET) {
866 IFA_UNLOCK(ifa);
867 break;
868 }
6d2010ae 869 IFA_UNLOCK(ifa);
6d2010ae 870 }
39236c6e
A
871 ifnet_lock_done(ifp);
872
873 lck_mtx_lock(&ifp->if_addrconfig_lock);
874 if (ifa == NULL && ifp->if_allhostsinm != NULL) {
875 struct in_multi *inm = ifp->if_allhostsinm;
876 ifp->if_allhostsinm = NULL;
877
878 in_delmulti(inm);
879 /* release the reference for allhostsinm */
880 INM_REMREF(inm);
881 }
882 lck_mtx_unlock(&ifp->if_addrconfig_lock);
883 } else {
884 ifnet_lock_done(ifp);
6d2010ae 885 }
39236c6e
A
886
887 /* Post the kernel event */
39037602 888 dlil_post_complete_msg(ifp, &ev_msg);
39236c6e
A
889
890 /*
891 * See if there is any IPV4 address left and if so,
892 * reconfigure KDP to use current primary address.
893 */
894 ifa = ifa_ifpgetprimary(ifp, AF_INET);
6d2010ae 895 if (ifa != NULL) {
39236c6e
A
896 /*
897 * NOTE: SIOCSIFADDR is defined with struct ifreq
898 * as parameter, but here we are sending it down
899 * to the interface with a pointer to struct ifaddr,
900 * for legacy reasons.
901 */
902 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFADDR, ifa);
0a7de745 903 if (error == EOPNOTSUPP) {
39236c6e 904 error = 0;
0a7de745 905 }
55e303ae 906
39236c6e
A
907 /* Release reference from ifa_ifpgetprimary() */
908 IFA_REMREF(ifa);
909 }
910 (void) ifnet_notify_address(ifp, AF_INET);
9bccf70c 911 break;
0b4e3aa0 912
39236c6e
A
913 default:
914 VERIFY(0);
915 /* NOTREACHED */
916 }
316670eb 917
0a7de745 918 return error;
39236c6e 919}
316670eb 920
39236c6e
A
921/*
922 * Caller passes in the ioctl data pointer directly via "ifr", with the
923 * expectation that this routine always uses bcopy() or other byte-aligned
924 * memory accesses.
925 */
926static __attribute__((noinline)) int
927inctl_ifdstaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
928 struct ifreq *ifr)
929{
930 struct kev_in_data in_event_data;
931 struct kev_msg ev_msg;
932 struct sockaddr_in dstaddr;
933 int error = 0;
316670eb 934
39236c6e
A
935 VERIFY(ifp != NULL);
936
0a7de745
A
937 if (!(ifp->if_flags & IFF_POINTOPOINT)) {
938 return EINVAL;
939 }
316670eb 940
0a7de745
A
941 bzero(&in_event_data, sizeof(struct kev_in_data));
942 bzero(&ev_msg, sizeof(struct kev_msg));
39236c6e
A
943
944 switch (cmd) {
0a7de745 945 case SIOCGIFDSTADDR: /* struct ifreq */
39236c6e
A
946 if (ia == NULL) {
947 error = EADDRNOTAVAIL;
948 break;
949 }
950 IFA_LOCK(&ia->ia_ifa);
0a7de745 951 bcopy(&ia->ia_dstaddr, &ifr->ifr_dstaddr, sizeof(dstaddr));
39236c6e
A
952 IFA_UNLOCK(&ia->ia_ifa);
953 break;
6d2010ae 954
0a7de745 955 case SIOCSIFDSTADDR: /* struct ifreq */
39236c6e 956 VERIFY(ia != NULL);
6d2010ae 957 IFA_LOCK(&ia->ia_ifa);
39236c6e 958 dstaddr = ia->ia_dstaddr;
eb6b6ca3 959
eb6b6ca3
A
960 ia->ia_dstaddr.sin_family = AF_INET;
961 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
962 ia->ia_dstaddr.sin_port = 0;
f427ee49
A
963 bcopy(&(SIN(&ifr->ifr_dstaddr)->sin_addr),
964 &ia->ia_dstaddr.sin_addr, sizeof(ia->ia_dstaddr.sin_addr));
eb6b6ca3
A
965 bzero(&ia->ia_dstaddr.sin_zero, sizeof(ia->ia_dstaddr.sin_zero));
966
39236c6e
A
967 IFA_UNLOCK(&ia->ia_ifa);
968 /*
969 * NOTE: SIOCSIFDSTADDR is defined with struct ifreq
970 * as parameter, but here we are sending it down
971 * to the interface with a pointer to struct ifaddr,
972 * for legacy reasons.
973 */
974 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFDSTADDR, ia);
975 IFA_LOCK(&ia->ia_ifa);
0a7de745 976 if (error == EOPNOTSUPP) {
39236c6e 977 error = 0;
0a7de745 978 }
39236c6e
A
979 if (error != 0) {
980 ia->ia_dstaddr = dstaddr;
981 IFA_UNLOCK(&ia->ia_ifa);
982 break;
983 }
984 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
6d2010ae 985
0a7de745
A
986 ev_msg.vendor_code = KEV_VENDOR_APPLE;
987 ev_msg.kev_class = KEV_NETWORK_CLASS;
988 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
39236c6e 989
0a7de745 990 ev_msg.event_code = KEV_INET_SIFDSTADDR;
1c79356b 991
316670eb 992 if (ia->ia_ifa.ifa_dstaddr) {
39236c6e
A
993 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
994 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
316670eb
A
995 } else {
996 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
997 }
39236c6e 998
0a7de745
A
999 in_event_data.ia_addr = ia->ia_addr.sin_addr;
1000 in_event_data.ia_net = ia->ia_net;
1001 in_event_data.ia_netmask = ia->ia_netmask;
1002 in_event_data.ia_subnet = ia->ia_subnet;
1003 in_event_data.ia_subnetmask = ia->ia_subnetmask;
1004 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
6d2010ae 1005 IFA_UNLOCK(&ia->ia_ifa);
fe8ab488 1006 (void) strlcpy(&in_event_data.link_data.if_name[0],
316670eb 1007 ifp->if_name, IFNAMSIZ);
1c79356b 1008 in_event_data.link_data.if_family = ifp->if_family;
39236c6e 1009 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1c79356b
A
1010
1011 ev_msg.dv[0].data_ptr = &in_event_data;
0a7de745 1012 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
1c79356b
A
1013 ev_msg.dv[1].data_length = 0;
1014
39037602 1015 dlil_post_complete_msg(ifp, &ev_msg);
b0d623f7 1016
39236c6e 1017 lck_mtx_lock(rnh_lock);
6d2010ae 1018 IFA_LOCK(&ia->ia_ifa);
39236c6e
A
1019 if (ia->ia_flags & IFA_ROUTE) {
1020 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&dstaddr;
6d2010ae 1021 IFA_UNLOCK(&ia->ia_ifa);
39236c6e 1022 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
6d2010ae 1023 IFA_LOCK(&ia->ia_ifa);
39236c6e
A
1024 ia->ia_ifa.ifa_dstaddr =
1025 (struct sockaddr *)&ia->ia_dstaddr;
6d2010ae 1026 IFA_UNLOCK(&ia->ia_ifa);
39236c6e 1027 rtinit_locked(&(ia->ia_ifa), (int)RTM_ADD,
0a7de745 1028 RTF_HOST | RTF_UP);
6d2010ae
A
1029 } else {
1030 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 1031 }
39236c6e
A
1032 lck_mtx_unlock(rnh_lock);
1033 break;
1c79356b 1034
316670eb 1035
316670eb 1036
39236c6e
A
1037 default:
1038 VERIFY(0);
1039 /* NOTREACHED */
1040 }
316670eb 1041
0a7de745 1042 return error;
39236c6e 1043}
316670eb 1044
39236c6e
A
1045/*
1046 * Caller passes in the ioctl data pointer directly via "ifr", with the
1047 * expectation that this routine always uses bcopy() or other byte-aligned
1048 * memory accesses.
1049 */
1050static __attribute__((noinline)) int
1051inctl_ifbrdaddr(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
1052 struct ifreq *ifr)
1053{
1054 struct kev_in_data in_event_data;
1055 struct kev_msg ev_msg;
1056 int error = 0;
1057
1058 VERIFY(ifp != NULL);
1059
0a7de745
A
1060 if (ia == NULL) {
1061 return EADDRNOTAVAIL;
1062 }
39236c6e 1063
0a7de745
A
1064 if (!(ifp->if_flags & IFF_BROADCAST)) {
1065 return EINVAL;
1066 }
39236c6e 1067
0a7de745
A
1068 bzero(&in_event_data, sizeof(struct kev_in_data));
1069 bzero(&ev_msg, sizeof(struct kev_msg));
39236c6e
A
1070
1071 switch (cmd) {
0a7de745 1072 case SIOCGIFBRDADDR: /* struct ifreq */
39236c6e
A
1073 IFA_LOCK(&ia->ia_ifa);
1074 bcopy(&ia->ia_broadaddr, &ifr->ifr_broadaddr,
0a7de745 1075 sizeof(struct sockaddr_in));
39236c6e
A
1076 IFA_UNLOCK(&ia->ia_ifa);
1077 break;
1078
0a7de745 1079 case SIOCSIFBRDADDR: /* struct ifreq */
39236c6e 1080 IFA_LOCK(&ia->ia_ifa);
39236c6e 1081
eb6b6ca3
A
1082 ia->ia_broadaddr.sin_family = AF_INET;
1083 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
1084 ia->ia_broadaddr.sin_port = 0;
f427ee49
A
1085 bcopy(&(SIN(&ifr->ifr_broadaddr)->sin_addr),
1086 &ia->ia_broadaddr.sin_addr, sizeof(ia->ia_broadaddr.sin_addr));
eb6b6ca3
A
1087 bzero(&ia->ia_broadaddr.sin_zero, sizeof(ia->ia_broadaddr.sin_zero));
1088
0a7de745
A
1089 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1090 ev_msg.kev_class = KEV_NETWORK_CLASS;
1091 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
39236c6e
A
1092
1093 ev_msg.event_code = KEV_INET_SIFBRDADDR;
1094
1095 if (ia->ia_ifa.ifa_dstaddr) {
1096 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
1097 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
6d2010ae 1098 } else {
39236c6e 1099 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
1c79356b 1100 }
0a7de745
A
1101 in_event_data.ia_addr = ia->ia_addr.sin_addr;
1102 in_event_data.ia_net = ia->ia_net;
1103 in_event_data.ia_netmask = ia->ia_netmask;
1104 in_event_data.ia_subnet = ia->ia_subnet;
1105 in_event_data.ia_subnetmask = ia->ia_subnetmask;
1106 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
39236c6e 1107 IFA_UNLOCK(&ia->ia_ifa);
fe8ab488 1108 (void) strlcpy(&in_event_data.link_data.if_name[0],
39236c6e
A
1109 ifp->if_name, IFNAMSIZ);
1110 in_event_data.link_data.if_family = ifp->if_family;
1111 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1112
1113 ev_msg.dv[0].data_ptr = &in_event_data;
0a7de745 1114 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
39236c6e
A
1115 ev_msg.dv[1].data_length = 0;
1116
39037602 1117 dlil_post_complete_msg(ifp, &ev_msg);
2d21ac55 1118 break;
39236c6e
A
1119
1120 default:
1121 VERIFY(0);
1122 /* NOTREACHED */
316670eb 1123 }
1c79356b 1124
0a7de745 1125 return error;
39236c6e
A
1126}
1127
1128/*
1129 * Caller passes in the ioctl data pointer directly via "ifr", with the
1130 * expectation that this routine always uses bcopy() or other byte-aligned
1131 * memory accesses.
1132 */
1133static __attribute__((noinline)) int
1134inctl_ifnetmask(struct ifnet *ifp, struct in_ifaddr *ia, u_long cmd,
1135 struct ifreq *ifr)
1136{
1137 struct kev_in_data in_event_data;
1138 struct kev_msg ev_msg;
1139 struct sockaddr_in mask;
1140 int error = 0;
1141
1142 VERIFY(ifp != NULL);
1143
0a7de745
A
1144 bzero(&in_event_data, sizeof(struct kev_in_data));
1145 bzero(&ev_msg, sizeof(struct kev_msg));
39236c6e
A
1146
1147 switch (cmd) {
0a7de745 1148 case SIOCGIFNETMASK: /* struct ifreq */
39236c6e
A
1149 if (ia == NULL) {
1150 error = EADDRNOTAVAIL;
2d21ac55
A
1151 break;
1152 }
39236c6e 1153 IFA_LOCK(&ia->ia_ifa);
0a7de745 1154 bcopy(&ia->ia_sockmask, &ifr->ifr_addr, sizeof(mask));
39236c6e
A
1155 IFA_UNLOCK(&ia->ia_ifa);
1156 break;
9bccf70c 1157
0a7de745 1158 case SIOCSIFNETMASK: { /* struct ifreq */
39236c6e 1159 in_addr_t i;
6d2010ae 1160
0a7de745 1161 bcopy(&ifr->ifr_addr, &mask, sizeof(mask));
39236c6e 1162 i = mask.sin_addr.s_addr;
1c79356b 1163
39236c6e 1164 VERIFY(ia != NULL);
6d2010ae 1165 IFA_LOCK(&ia->ia_ifa);
39236c6e 1166 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
0a7de745
A
1167 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1168 ev_msg.kev_class = KEV_NETWORK_CLASS;
1169 ev_msg.kev_subclass = KEV_INET_SUBCLASS;
39236c6e
A
1170
1171 ev_msg.event_code = KEV_INET_SIFNETMASK;
1172
316670eb 1173 if (ia->ia_ifa.ifa_dstaddr) {
39236c6e
A
1174 in_event_data.ia_dstaddr = ((struct sockaddr_in *)
1175 (void *)ia->ia_ifa.ifa_dstaddr)->sin_addr;
316670eb
A
1176 } else {
1177 in_event_data.ia_dstaddr.s_addr = INADDR_ANY;
1178 }
0a7de745
A
1179 in_event_data.ia_addr = ia->ia_addr.sin_addr;
1180 in_event_data.ia_net = ia->ia_net;
1181 in_event_data.ia_netmask = ia->ia_netmask;
1182 in_event_data.ia_subnet = ia->ia_subnet;
1183 in_event_data.ia_subnetmask = ia->ia_subnetmask;
1184 in_event_data.ia_netbroadcast = ia->ia_netbroadcast;
6d2010ae 1185 IFA_UNLOCK(&ia->ia_ifa);
fe8ab488 1186 (void) strlcpy(&in_event_data.link_data.if_name[0],
316670eb 1187 ifp->if_name, IFNAMSIZ);
1c79356b 1188 in_event_data.link_data.if_family = ifp->if_family;
39236c6e 1189 in_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
1c79356b
A
1190
1191 ev_msg.dv[0].data_ptr = &in_event_data;
0a7de745 1192 ev_msg.dv[0].data_length = sizeof(struct kev_in_data);
1c79356b
A
1193 ev_msg.dv[1].data_length = 0;
1194
39037602 1195 dlil_post_complete_msg(ifp, &ev_msg);
39236c6e
A
1196 break;
1197 }
1198
1199 default:
1200 VERIFY(0);
1201 /* NOTREACHED */
1202 }
1203
0a7de745 1204 return error;
39236c6e
A
1205}
1206
1207/*
1208 * Generic INET control operations (ioctl's).
1209 *
1210 * ifp is NULL if not an interface-specific ioctl.
1211 *
1212 * Most of the routines called to handle the ioctls would end up being
1213 * tail-call optimized, which unfortunately causes this routine to
1214 * consume too much stack space; this is the reason for the "noinline"
1215 * attribute used on those routines.
1216 *
1217 * If called directly from within the networking stack (as opposed to via
1218 * pru_control), the socket parameter may be NULL.
1219 */
1220int
1221in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
1222 struct proc *p)
1223{
1224 struct ifreq *ifr = (struct ifreq *)(void *)data;
1225 struct sockaddr_in addr, dstaddr;
1226 struct sockaddr_in sin, *sa = NULL;
1227 boolean_t privileged = (proc_suser(p) == 0);
1228 boolean_t so_unlocked = FALSE;
1229 struct in_ifaddr *ia = NULL;
1230 struct ifaddr *ifa;
1231 int error = 0;
f427ee49 1232 int intval;
39236c6e
A
1233
1234 /* In case it's NULL, make sure it came from the kernel */
1235 VERIFY(so != NULL || p == kernproc);
1236
1237 /*
1238 * ioctls which don't require ifp, but require socket.
1239 */
1240 switch (cmd) {
0a7de745
A
1241 case SIOCGASSOCIDS32: /* struct so_aidreq32 */
1242 case SIOCGASSOCIDS64: /* struct so_aidreq64 */
1243 return inctl_associd(so, cmd, data);
1244 /* NOTREACHED */
1245
1246 case SIOCGCONNIDS32: /* struct so_cidreq32 */
1247 case SIOCGCONNIDS64: /* struct so_cidreq64 */
1248 return inctl_connid(so, cmd, data);
1249 /* NOTREACHED */
1250
1251 case SIOCGCONNINFO32: /* struct so_cinforeq32 */
1252 case SIOCGCONNINFO64: /* struct so_cinforeq64 */
1253 return inctl_conninfo(so, cmd, data);
39236c6e
A
1254 /* NOTREACHED */
1255 }
1256
1257 /*
1258 * The rest of ioctls require ifp; reject if we don't have one;
1259 * return ENXIO to be consistent with ifioctl().
1260 */
0a7de745
A
1261 if (ifp == NULL) {
1262 return ENXIO;
1263 }
39236c6e
A
1264
1265 /*
1266 * ioctls which require ifp but not interface address.
1267 */
1268 switch (cmd) {
0a7de745
A
1269 case SIOCAUTOADDR: /* struct ifreq */
1270 if (!privileged) {
1271 return EPERM;
1272 }
1273 return inctl_autoaddr(ifp, ifr);
1274 /* NOTREACHED */
39236c6e 1275
0a7de745
A
1276 case SIOCARPIPLL: /* struct ifreq */
1277 if (!privileged) {
1278 return EPERM;
1279 }
1280 return inctl_arpipll(ifp, ifr);
1281 /* NOTREACHED */
39236c6e 1282
f427ee49
A
1283 case SIOCGETROUTERMODE: /* struct ifreq */
1284 intval = (ifp->if_eflags & IFEF_IPV4_ROUTER) != 0 ? 1 : 0;
1285 bcopy(&intval, &ifr->ifr_intval, sizeof(intval));
1286 return 0;
1287 /* NOTREACHED */
1288
0a7de745
A
1289 case SIOCSETROUTERMODE: /* struct ifreq */
1290 if (!privileged) {
1291 return EPERM;
1292 }
1293 return inctl_setrouter(ifp, ifr);
1294 /* NOTREACHED */
39236c6e 1295
0a7de745
A
1296 case SIOCPROTOATTACH: /* struct ifreq */
1297 if (!privileged) {
1298 return EPERM;
1299 }
1300 return in_domifattach(ifp);
1301 /* NOTREACHED */
39236c6e 1302
0a7de745
A
1303 case SIOCPROTODETACH: /* struct ifreq */
1304 if (!privileged) {
1305 return EPERM;
1306 }
b0d623f7 1307
9bccf70c 1308 /*
39236c6e 1309 * If an IPv4 address is still present, refuse to detach.
9bccf70c 1310 */
39236c6e
A
1311 ifnet_lock_shared(ifp);
1312 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1313 IFA_LOCK(ifa);
1314 if (ifa->ifa_addr->sa_family == AF_INET) {
1315 IFA_UNLOCK(ifa);
1316 break;
1317 }
1318 IFA_UNLOCK(ifa);
1319 }
1320 ifnet_lock_done(ifp);
0a7de745 1321 return (ifa == NULL) ? proto_unplumb(PF_INET, ifp) : EBUSY;
39236c6e 1322 /* NOTREACHED */
39236c6e
A
1323 }
1324
1325 /*
1326 * ioctls which require interface address; obtain sockaddr_in.
1327 */
1328 switch (cmd) {
0a7de745
A
1329 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1330 if (!privileged) {
1331 return EPERM;
1332 }
39236c6e 1333 bcopy(&((struct in_aliasreq *)(void *)data)->ifra_addr,
0a7de745 1334 &sin, sizeof(sin));
39236c6e
A
1335 sa = &sin;
1336 break;
1337
0a7de745
A
1338 case SIOCDIFADDR: /* struct ifreq */
1339 case SIOCSIFADDR: /* struct ifreq */
1340 case SIOCSIFDSTADDR: /* struct ifreq */
1341 case SIOCSIFNETMASK: /* struct ifreq */
1342 case SIOCSIFBRDADDR: /* struct ifreq */
1343 if (!privileged) {
1344 return EPERM;
1345 }
f427ee49 1346 OS_FALLTHROUGH;
0a7de745
A
1347 case SIOCGIFADDR: /* struct ifreq */
1348 case SIOCGIFDSTADDR: /* struct ifreq */
1349 case SIOCGIFNETMASK: /* struct ifreq */
1350 case SIOCGIFBRDADDR: /* struct ifreq */
1351 bcopy(&ifr->ifr_addr, &sin, sizeof(sin));
39236c6e
A
1352 sa = &sin;
1353 break;
1354 }
1355
1356 /*
1357 * Find address for this interface, if it exists.
1358 *
1359 * If an alias address was specified, find that one instead of
1360 * the first one on the interface, if possible.
1361 */
1362 VERIFY(ia == NULL);
1363 if (sa != NULL) {
1364 struct in_ifaddr *iap;
9bccf70c 1365
39236c6e
A
1366 /*
1367 * Any failures from this point on must take into account
1368 * a non-NULL "ia" with an outstanding reference count, and
1369 * therefore requires IFA_REMREF. Jump to "done" label
1370 * instead of calling return if "ia" is valid.
1371 */
1372 lck_rw_lock_shared(in_ifaddr_rwlock);
1373 TAILQ_FOREACH(iap, INADDR_HASH(sa->sin_addr.s_addr), ia_hash) {
1374 IFA_LOCK(&iap->ia_ifa);
1375 if (iap->ia_ifp == ifp &&
1376 iap->ia_addr.sin_addr.s_addr ==
1377 sa->sin_addr.s_addr) {
1378 ia = iap;
813fb2f6 1379 IFA_ADDREF_LOCKED(&iap->ia_ifa);
39236c6e
A
1380 IFA_UNLOCK(&iap->ia_ifa);
1381 break;
1382 }
1383 IFA_UNLOCK(&iap->ia_ifa);
1384 }
39236c6e
A
1385 lck_rw_done(in_ifaddr_rwlock);
1386
1387 if (ia == NULL) {
1388 ifnet_lock_shared(ifp);
6d2010ae 1389 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
39236c6e
A
1390 iap = ifatoia(ifa);
1391 IFA_LOCK(&iap->ia_ifa);
1392 if (iap->ia_addr.sin_family == AF_INET) {
1393 ia = iap;
cb323159 1394 IFA_ADDREF_LOCKED(&iap->ia_ifa);
39236c6e 1395 IFA_UNLOCK(&iap->ia_ifa);
9bccf70c 1396 break;
6d2010ae 1397 }
39236c6e 1398 IFA_UNLOCK(&iap->ia_ifa);
9bccf70c 1399 }
91447636 1400 ifnet_lock_done(ifp);
39236c6e
A
1401 }
1402 }
6d2010ae 1403
39236c6e
A
1404 /*
1405 * Unlock the socket since ifnet_ioctl() may be invoked by
1406 * one of the ioctl handlers below. Socket will be re-locked
1407 * prior to returning.
1408 */
1409 if (so != NULL) {
1410 socket_unlock(so, 0);
1411 so_unlocked = TRUE;
1412 }
6d2010ae 1413
39236c6e 1414 switch (cmd) {
0a7de745
A
1415 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1416 case SIOCDIFADDR: /* struct ifreq */
39236c6e
A
1417 if (cmd == SIOCAIFADDR) {
1418 bcopy(&((struct in_aliasreq *)(void *)data)->
0a7de745 1419 ifra_addr, &addr, sizeof(addr));
39236c6e 1420 bcopy(&((struct in_aliasreq *)(void *)data)->
0a7de745 1421 ifra_dstaddr, &dstaddr, sizeof(dstaddr));
39236c6e
A
1422 } else {
1423 VERIFY(cmd == SIOCDIFADDR);
1424 bcopy(&((struct ifreq *)(void *)data)->ifr_addr,
0a7de745
A
1425 &addr, sizeof(addr));
1426 bzero(&dstaddr, sizeof(dstaddr));
39236c6e
A
1427 }
1428
1429 if (addr.sin_family == AF_INET) {
1430 struct in_ifaddr *oia;
1431
1432 lck_rw_lock_shared(in_ifaddr_rwlock);
1433 for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
1434 IFA_LOCK(&ia->ia_ifa);
1435 if (ia->ia_ifp == ifp &&
1436 ia->ia_addr.sin_addr.s_addr ==
1437 addr.sin_addr.s_addr) {
1438 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1439 IFA_UNLOCK(&ia->ia_ifa);
1440 break;
1441 }
1442 IFA_UNLOCK(&ia->ia_ifa);
6d2010ae 1443 }
39236c6e 1444 lck_rw_done(in_ifaddr_rwlock);
0a7de745 1445 if (oia != NULL) {
39236c6e 1446 IFA_REMREF(&oia->ia_ifa);
0a7de745 1447 }
39236c6e
A
1448 if ((ifp->if_flags & IFF_POINTOPOINT) &&
1449 (cmd == SIOCAIFADDR) &&
1450 (dstaddr.sin_addr.s_addr == INADDR_ANY)) {
1451 error = EDESTADDRREQ;
1452 goto done;
1453 }
1454 } else if (cmd == SIOCAIFADDR) {
1455 error = EINVAL;
1456 goto done;
1457 }
cb323159
A
1458 if (cmd == SIOCDIFADDR) {
1459 if (ia == NULL) {
1460 error = EADDRNOTAVAIL;
1461 goto done;
1462 }
1463
1464 IFA_LOCK(&ia->ia_ifa);
1465 /*
1466 * Avoid the race condition seen when two
1467 * threads process SIOCDIFADDR command
1468 * at the same time.
1469 */
1470 while (ia->ia_ifa.ifa_debug & IFD_DETACHING) {
1471 os_log(OS_LOG_DEFAULT,
1472 "Another thread is already attempting to "
1473 "delete IPv4 address: %s on interface %s. "
1474 "Go to sleep and check again after the operation is done",
1475 inet_ntoa(sa->sin_addr), ia->ia_ifp->if_xname);
1476 ia->ia_ifa.ifa_del_waiters++;
1477 (void) msleep(ia->ia_ifa.ifa_del_wc, &ia->ia_ifa.ifa_lock, (PZERO - 1),
1478 __func__, NULL);
1479 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1480 }
1481
1482 if ((ia->ia_ifa.ifa_debug & IFD_ATTACHED) == 0) {
1483 error = EADDRNOTAVAIL;
1484 IFA_UNLOCK(&ia->ia_ifa);
1485 goto done;
1486 }
1487
1488 ia->ia_ifa.ifa_debug |= IFD_DETACHING;
1489 IFA_UNLOCK(&ia->ia_ifa);
39236c6e 1490 }
cb323159 1491
f427ee49 1492 OS_FALLTHROUGH;
0a7de745
A
1493 case SIOCSIFADDR: /* struct ifreq */
1494 case SIOCSIFDSTADDR: /* struct ifreq */
1495 case SIOCSIFNETMASK: /* struct ifreq */
39236c6e
A
1496 if (cmd == SIOCAIFADDR) {
1497 /* fell thru from above; just repeat it */
1498 bcopy(&((struct in_aliasreq *)(void *)data)->
0a7de745 1499 ifra_addr, &addr, sizeof(addr));
6d2010ae 1500 } else {
39236c6e
A
1501 VERIFY(cmd == SIOCDIFADDR || cmd == SIOCSIFADDR ||
1502 cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR);
1503 bcopy(&((struct ifreq *)(void *)data)->ifr_addr,
0a7de745 1504 &addr, sizeof(addr));
6d2010ae 1505 }
91447636 1506
39236c6e
A
1507 if (addr.sin_family != AF_INET && cmd == SIOCSIFADDR) {
1508 error = EINVAL;
1509 goto done;
1510 }
1511 if (ia == NULL) {
1512 ia = in_ifaddr_alloc(M_WAITOK);
1513 if (ia == NULL) {
1514 error = ENOBUFS;
1515 goto done;
1516 }
1517 ifnet_lock_exclusive(ifp);
1518 ifa = &ia->ia_ifa;
1519 IFA_LOCK(ifa);
1520 /* Hold a reference for this routine */
1521 IFA_ADDREF_LOCKED(ifa);
1522 IA_HASH_INIT(ia);
1523 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
1524 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1525 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
eb6b6ca3 1526 ia->ia_sockmask.sin_len = offsetof(struct sockaddr_in, sin_zero);
39236c6e 1527 if (ifp->if_flags & IFF_BROADCAST) {
0a7de745 1528 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
39236c6e
A
1529 ia->ia_broadaddr.sin_family = AF_INET;
1530 }
1531 ia->ia_ifp = ifp;
0a7de745 1532 if (!(ifp->if_flags & IFF_LOOPBACK)) {
39236c6e 1533 in_interfaces++;
0a7de745 1534 }
39236c6e
A
1535 /* if_attach_ifa() holds a reference for ifa_link */
1536 if_attach_ifa(ifp, ifa);
316670eb 1537 /*
39236c6e
A
1538 * If we have to go through in_ifinit(), make sure
1539 * to avoid installing route(s) based on this address
1540 * via PFC_IFUP event, before the link resolver (ARP)
1541 * initializes it.
316670eb 1542 */
0a7de745 1543 if (cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) {
39236c6e 1544 ifa->ifa_debug |= IFD_NOTREADY;
0a7de745 1545 }
39236c6e
A
1546 IFA_UNLOCK(ifa);
1547 ifnet_lock_done(ifp);
1548 lck_rw_lock_exclusive(in_ifaddr_rwlock);
1549 /* Hold a reference for ia_link */
1550 IFA_ADDREF(ifa);
1551 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
1552 lck_rw_done(in_ifaddr_rwlock);
1553 /* discard error */
1554 (void) in_domifattach(ifp);
1555 error = 0;
2d21ac55 1556 }
1c79356b 1557 break;
39236c6e 1558 }
1c79356b 1559
39236c6e 1560 switch (cmd) {
0a7de745
A
1561 case SIOCGIFDSTADDR: /* struct ifreq */
1562 case SIOCSIFDSTADDR: /* struct ifreq */
39236c6e
A
1563 error = inctl_ifdstaddr(ifp, ia, cmd, ifr);
1564 break;
316670eb 1565
0a7de745
A
1566 case SIOCGIFBRDADDR: /* struct ifreq */
1567 case SIOCSIFBRDADDR: /* struct ifreq */
39236c6e
A
1568 error = inctl_ifbrdaddr(ifp, ia, cmd, ifr);
1569 break;
316670eb 1570
0a7de745
A
1571 case SIOCGIFNETMASK: /* struct ifreq */
1572 case SIOCSIFNETMASK: /* struct ifreq */
39236c6e
A
1573 error = inctl_ifnetmask(ifp, ia, cmd, ifr);
1574 break;
316670eb 1575
0a7de745
A
1576 case SIOCGIFADDR: /* struct ifreq */
1577 case SIOCSIFADDR: /* struct ifreq */
1578 case SIOCAIFADDR: /* struct {if,in_}aliasreq */
1579 case SIOCDIFADDR: /* struct ifreq */
39236c6e 1580 error = inctl_ifaddr(ifp, ia, cmd, ifr);
316670eb 1581 break;
1c79356b
A
1582
1583 default:
2d21ac55 1584 error = EOPNOTSUPP;
39236c6e 1585 break;
1c79356b 1586 }
cb323159 1587
39236c6e 1588done:
0a7de745 1589 if (ia != NULL) {
cb323159
A
1590 if (cmd == SIOCDIFADDR) {
1591 IFA_LOCK(&ia->ia_ifa);
1592 ia->ia_ifa.ifa_debug &= ~IFD_DETACHING;
1593 if (ia->ia_ifa.ifa_del_waiters > 0) {
1594 ia->ia_ifa.ifa_del_waiters = 0;
1595 wakeup(ia->ia_ifa.ifa_del_wc);
1596 }
1597 IFA_UNLOCK(&ia->ia_ifa);
1598 }
6d2010ae 1599 IFA_REMREF(&ia->ia_ifa);
0a7de745
A
1600 }
1601 if (so_unlocked) {
39236c6e 1602 socket_lock(so, 0);
0a7de745 1603 }
39236c6e 1604
0a7de745 1605 return error;
1c79356b
A
1606}
1607
1c79356b
A
1608/*
1609 * Delete any existing route for an interface.
1610 */
1611void
6d2010ae 1612in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, int locked)
1c79356b 1613{
6d2010ae
A
1614 IFA_LOCK(&ia->ia_ifa);
1615 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1616 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 1617 return;
6d2010ae
A
1618 }
1619 IFA_UNLOCK(&ia->ia_ifa);
0a7de745 1620 if (!locked) {
b0d623f7 1621 lck_mtx_lock(rnh_lock);
0a7de745
A
1622 }
1623 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) {
91447636 1624 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
0a7de745 1625 } else {
91447636 1626 rtinit_locked(&(ia->ia_ifa), (int)RTM_DELETE, 0);
0a7de745 1627 }
6d2010ae 1628 IFA_LOCK(&ia->ia_ifa);
1c79356b 1629 ia->ia_flags &= ~IFA_ROUTE;
6d2010ae 1630 IFA_UNLOCK(&ia->ia_ifa);
0a7de745 1631 if (!locked) {
b0d623f7 1632 lck_mtx_unlock(rnh_lock);
0a7de745 1633 }
b0d623f7
A
1634}
1635
1636/*
1637 * Caller must hold in_ifaddr_rwlock as writer.
1638 */
1639static void
1640in_iahash_remove(struct in_ifaddr *ia)
1641{
5ba3f43e 1642 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
6d2010ae 1643 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
b0d623f7 1644
6d2010ae
A
1645 if (!IA_IS_HASHED(ia)) {
1646 panic("attempt to remove wrong ia %p from hash table\n", ia);
1647 /* NOTREACHED */
1648 }
b0d623f7
A
1649 TAILQ_REMOVE(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
1650 IA_HASH_INIT(ia);
6d2010ae
A
1651 if (IFA_REMREF_LOCKED(&ia->ia_ifa) == NULL) {
1652 panic("%s: unexpected (missing) refcnt ifa=%p", __func__,
1653 &ia->ia_ifa);
1654 /* NOTREACHED */
1655 }
b0d623f7
A
1656}
1657
1658/*
1659 * Caller must hold in_ifaddr_rwlock as writer.
1660 */
1661static void
1662in_iahash_insert(struct in_ifaddr *ia)
1663{
5ba3f43e 1664 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
6d2010ae
A
1665 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1666
1667 if (ia->ia_addr.sin_family != AF_INET) {
b0d623f7 1668 panic("attempt to insert wrong ia %p into hash table\n", ia);
6d2010ae
A
1669 /* NOTREACHED */
1670 } else if (IA_IS_HASHED(ia)) {
b0d623f7 1671 panic("attempt to double-insert ia %p into hash table\n", ia);
6d2010ae
A
1672 /* NOTREACHED */
1673 }
39236c6e
A
1674 TAILQ_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1675 ia, ia_hash);
6d2010ae 1676 IFA_ADDREF_LOCKED(&ia->ia_ifa);
b0d623f7
A
1677}
1678
1679/*
39236c6e
A
1680 * Some point to point interfaces that are tunnels borrow the address from
1681 * an underlying interface (e.g. VPN server). In order for source address
1682 * selection logic to find the underlying interface first, we add the address
b0d623f7
A
1683 * of borrowing point to point interfaces at the end of the list.
1684 * (see rdar://6733789)
1685 *
1686 * Caller must hold in_ifaddr_rwlock as writer.
1687 */
1688static void
1689in_iahash_insert_ptp(struct in_ifaddr *ia)
1690{
1691 struct in_ifaddr *tmp_ifa;
1692 struct ifnet *tmp_ifp;
1693
5ba3f43e 1694 LCK_RW_ASSERT(in_ifaddr_rwlock, LCK_RW_ASSERT_EXCLUSIVE);
6d2010ae
A
1695 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
1696
1697 if (ia->ia_addr.sin_family != AF_INET) {
b0d623f7 1698 panic("attempt to insert wrong ia %p into hash table\n", ia);
6d2010ae
A
1699 /* NOTREACHED */
1700 } else if (IA_IS_HASHED(ia)) {
b0d623f7 1701 panic("attempt to double-insert ia %p into hash table\n", ia);
6d2010ae
A
1702 /* NOTREACHED */
1703 }
1704 IFA_UNLOCK(&ia->ia_ifa);
1705 TAILQ_FOREACH(tmp_ifa, INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1706 ia_hash) {
1707 IFA_LOCK(&tmp_ifa->ia_ifa);
1708 /* ia->ia_addr won't change, so check without lock */
1709 if (IA_SIN(tmp_ifa)->sin_addr.s_addr ==
1710 ia->ia_addr.sin_addr.s_addr) {
1711 IFA_UNLOCK(&tmp_ifa->ia_ifa);
b0d623f7 1712 break;
6d2010ae
A
1713 }
1714 IFA_UNLOCK(&tmp_ifa->ia_ifa);
1715 }
b0d623f7
A
1716 tmp_ifp = (tmp_ifa == NULL) ? NULL : tmp_ifa->ia_ifp;
1717
6d2010ae
A
1718 IFA_LOCK(&ia->ia_ifa);
1719 if (tmp_ifp == NULL) {
1720 TAILQ_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1721 ia, ia_hash);
1722 } else {
1723 TAILQ_INSERT_TAIL(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1724 ia, ia_hash);
1725 }
1726 IFA_ADDREF_LOCKED(&ia->ia_ifa);
1c79356b
A
1727}
1728
1729/*
1730 * Initialize an interface's internet address
1731 * and routing table entry.
1732 */
1733static int
39236c6e
A
1734in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
1735 int scrub)
1c79356b 1736{
b0d623f7 1737 u_int32_t i = ntohl(sin->sin_addr.s_addr);
1c79356b 1738 struct sockaddr_in oldaddr;
91447636 1739 int flags = RTF_UP, error;
2d21ac55
A
1740 struct ifaddr *ifa0;
1741 unsigned int cmd;
b0d623f7
A
1742 int oldremoved = 0;
1743
1744 /* Take an extra reference for this routine */
6d2010ae 1745 IFA_ADDREF(&ia->ia_ifa);
1c79356b 1746
b0d623f7 1747 lck_rw_lock_exclusive(in_ifaddr_rwlock);
6d2010ae 1748 IFA_LOCK(&ia->ia_ifa);
1c79356b 1749 oldaddr = ia->ia_addr;
b0d623f7
A
1750 if (IA_IS_HASHED(ia)) {
1751 oldremoved = 1;
1752 in_iahash_remove(ia);
1753 }
1c79356b 1754 ia->ia_addr = *sin;
39236c6e
A
1755 /*
1756 * Interface addresses should not contain port or sin_zero information.
1757 */
1758 SIN(&ia->ia_addr)->sin_family = AF_INET;
0a7de745 1759 SIN(&ia->ia_addr)->sin_len = sizeof(struct sockaddr_in);
39236c6e 1760 SIN(&ia->ia_addr)->sin_port = 0;
0a7de745
A
1761 bzero(&SIN(&ia->ia_addr)->sin_zero, sizeof(sin->sin_zero));
1762 if ((ifp->if_flags & IFF_POINTOPOINT)) {
b0d623f7 1763 in_iahash_insert_ptp(ia);
0a7de745 1764 } else {
b0d623f7 1765 in_iahash_insert(ia);
0a7de745 1766 }
6d2010ae 1767 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 1768 lck_rw_done(in_ifaddr_rwlock);
1c79356b 1769
9bccf70c 1770 /*
2d21ac55
A
1771 * Give the interface a chance to initialize if this is its first
1772 * address, and to validate the address if necessary. Send down
1773 * SIOCSIFADDR for first address, and SIOCAIFADDR for alias(es).
1774 * We find the first IPV4 address assigned to it and check if this
1775 * is the same as the one passed into this routine.
9bccf70c 1776 */
2d21ac55
A
1777 ifa0 = ifa_ifpgetprimary(ifp, AF_INET);
1778 cmd = (&ia->ia_ifa == ifa0) ? SIOCSIFADDR : SIOCAIFADDR;
1779 error = ifnet_ioctl(ifp, PF_INET, cmd, ia);
0a7de745 1780 if (error == EOPNOTSUPP) {
2d21ac55 1781 error = 0;
0a7de745 1782 }
2d21ac55
A
1783 /*
1784 * If we've just sent down SIOCAIFADDR, send another ioctl down
1785 * for SIOCSIFADDR for the first IPV4 address of the interface,
1786 * because an address change on one of the addresses will result
1787 * in the removal of the previous first IPV4 address. KDP needs
1788 * be reconfigured with the current primary IPV4 address.
1789 */
1790 if (error == 0 && cmd == SIOCAIFADDR) {
316670eb
A
1791 /*
1792 * NOTE: SIOCSIFADDR is defined with struct ifreq
1793 * as parameter, but here we are sending it down
1794 * to the interface with a pointer to struct ifaddr,
1795 * for legacy reasons.
1796 */
2d21ac55 1797 error = ifnet_ioctl(ifp, PF_INET, SIOCSIFADDR, ifa0);
0a7de745 1798 if (error == EOPNOTSUPP) {
2d21ac55 1799 error = 0;
0a7de745 1800 }
2d21ac55
A
1801 }
1802
1803 /* Release reference from ifa_ifpgetprimary() */
6d2010ae 1804 IFA_REMREF(ifa0);
2d21ac55 1805
1c79356b 1806 if (error) {
b0d623f7 1807 lck_rw_lock_exclusive(in_ifaddr_rwlock);
6d2010ae 1808 IFA_LOCK(&ia->ia_ifa);
0a7de745 1809 if (IA_IS_HASHED(ia)) {
b0d623f7 1810 in_iahash_remove(ia);
0a7de745 1811 }
1c79356b 1812 ia->ia_addr = oldaddr;
b0d623f7 1813 if (oldremoved) {
0a7de745 1814 if ((ifp->if_flags & IFF_POINTOPOINT)) {
b0d623f7 1815 in_iahash_insert_ptp(ia);
0a7de745 1816 } else {
b0d623f7 1817 in_iahash_insert(ia);
0a7de745 1818 }
b0d623f7 1819 }
6d2010ae 1820 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7
A
1821 lck_rw_done(in_ifaddr_rwlock);
1822 /* Release extra reference taken above */
6d2010ae 1823 IFA_REMREF(&ia->ia_ifa);
0a7de745 1824 return error;
1c79356b 1825 }
b0d623f7 1826 lck_mtx_lock(rnh_lock);
6d2010ae
A
1827 IFA_LOCK(&ia->ia_ifa);
1828 /*
1829 * Address has been initialized by the link resolver (ARP)
1830 * via ifnet_ioctl() above; it may now generate route(s).
1831 */
1832 ia->ia_ifa.ifa_debug &= ~IFD_NOTREADY;
1c79356b
A
1833 if (scrub) {
1834 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
6d2010ae 1835 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 1836 in_ifscrub(ifp, ia, 1);
6d2010ae 1837 IFA_LOCK(&ia->ia_ifa);
1c79356b
A
1838 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1839 }
6d2010ae 1840 IFA_LOCK_ASSERT_HELD(&ia->ia_ifa);
0a7de745 1841 if (IN_CLASSA(i)) {
1c79356b 1842 ia->ia_netmask = IN_CLASSA_NET;
0a7de745 1843 } else if (IN_CLASSB(i)) {
1c79356b 1844 ia->ia_netmask = IN_CLASSB_NET;
0a7de745 1845 } else {
1c79356b 1846 ia->ia_netmask = IN_CLASSC_NET;
0a7de745 1847 }
1c79356b
A
1848 /*
1849 * The subnet mask usually includes at least the standard network part,
1850 * but may may be smaller in the case of supernetting.
1851 * If it is set, we believe it.
1852 */
1853 if (ia->ia_subnetmask == 0) {
1854 ia->ia_subnetmask = ia->ia_netmask;
1855 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
0a7de745 1856 } else {
1c79356b 1857 ia->ia_netmask &= ia->ia_subnetmask;
0a7de745 1858 }
1c79356b
A
1859 ia->ia_net = i & ia->ia_netmask;
1860 ia->ia_subnet = i & ia->ia_subnetmask;
1861 in_socktrim(&ia->ia_sockmask);
1862 /*
1863 * Add route for the network.
1864 */
1865 ia->ia_ifa.ifa_metric = ifp->if_metric;
1866 if (ifp->if_flags & IFF_BROADCAST) {
1867 ia->ia_broadaddr.sin_addr.s_addr =
39236c6e 1868 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1c79356b 1869 ia->ia_netbroadcast.s_addr =
0a7de745 1870 htonl(ia->ia_net | ~ia->ia_netmask);
1c79356b
A
1871 } else if (ifp->if_flags & IFF_LOOPBACK) {
1872 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1873 flags |= RTF_HOST;
1874 } else if (ifp->if_flags & IFF_POINTOPOINT) {
b0d623f7 1875 if (ia->ia_dstaddr.sin_family != AF_INET) {
6d2010ae 1876 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7
A
1877 lck_mtx_unlock(rnh_lock);
1878 /* Release extra reference taken above */
6d2010ae 1879 IFA_REMREF(&ia->ia_ifa);
0a7de745 1880 return 0;
b0d623f7 1881 }
0a7de745 1882 ia->ia_dstaddr.sin_len = sizeof(struct sockaddr_in);
1c79356b
A
1883 flags |= RTF_HOST;
1884 }
6d2010ae 1885 IFA_UNLOCK(&ia->ia_ifa);
39236c6e 1886
6d2010ae
A
1887 if ((error = rtinit_locked(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) {
1888 IFA_LOCK(&ia->ia_ifa);
1c79356b 1889 ia->ia_flags |= IFA_ROUTE;
6d2010ae
A
1890 IFA_UNLOCK(&ia->ia_ifa);
1891 }
b0d623f7
A
1892 lck_mtx_unlock(rnh_lock);
1893
9bccf70c 1894 /* XXX check if the subnet route points to the same interface */
0a7de745 1895 if (error == EEXIST) {
9bccf70c 1896 error = 0;
0a7de745 1897 }
1c79356b
A
1898
1899 /*
1900 * If the interface supports multicast, join the "all hosts"
1901 * multicast group on that interface.
1902 */
1903 if (ifp->if_flags & IFF_MULTICAST) {
1904 struct in_addr addr;
1905
6d2010ae 1906 lck_mtx_lock(&ifp->if_addrconfig_lock);
1c79356b 1907 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
6d2010ae
A
1908 if (ifp->if_allhostsinm == NULL) {
1909 struct in_multi *inm;
1910 inm = in_addmulti(&addr, ifp);
1911
1912 if (inm != NULL) {
39236c6e
A
1913 /*
1914 * Keep the reference on inm added by
1915 * in_addmulti above for storing the
1916 * pointer in allhostsinm.
6d2010ae
A
1917 */
1918 ifp->if_allhostsinm = inm;
1919 } else {
39236c6e
A
1920 printf("%s: failed to add membership to "
1921 "all-hosts multicast address on %s\n",
1922 __func__, if_name(ifp));
6d2010ae
A
1923 }
1924 }
1925 lck_mtx_unlock(&ifp->if_addrconfig_lock);
1c79356b 1926 }
b0d623f7
A
1927
1928 /* Release extra reference taken above */
6d2010ae 1929 IFA_REMREF(&ia->ia_ifa);
39236c6e
A
1930
1931 if (error == 0) {
1932 /* invalidate route caches */
1933 routegenid_inet_update();
1934 }
1935
0a7de745 1936 return error;
1c79356b
A
1937}
1938
1c79356b 1939/*
39236c6e 1940 * Return TRUE if the address might be a local broadcast address.
1c79356b 1941 */
39236c6e 1942boolean_t
6d2010ae 1943in_broadcast(struct in_addr in, struct ifnet *ifp)
1c79356b 1944{
91447636 1945 struct ifaddr *ifa;
b0d623f7 1946 u_int32_t t;
1c79356b 1947
0a7de745
A
1948 if (in.s_addr == INADDR_BROADCAST || in.s_addr == INADDR_ANY) {
1949 return TRUE;
1950 }
1951 if (!(ifp->if_flags & IFF_BROADCAST)) {
1952 return FALSE;
1953 }
1c79356b 1954 t = ntohl(in.s_addr);
39236c6e 1955
1c79356b
A
1956 /*
1957 * Look through the list of addresses for a match
1958 * with a broadcast address.
1959 */
0a7de745 1960#define ia ((struct in_ifaddr *)ifa)
91447636
A
1961 ifnet_lock_shared(ifp);
1962 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
6d2010ae 1963 IFA_LOCK(ifa);
1c79356b
A
1964 if (ifa->ifa_addr->sa_family == AF_INET &&
1965 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
0a7de745
A
1966 in.s_addr == ia->ia_netbroadcast.s_addr ||
1967 /*
1968 * Check for old-style (host 0) broadcast.
1969 */
1970 t == ia->ia_subnet || t == ia->ia_net) &&
1971 /*
1972 * Check for an all one subnetmask. These
1973 * only exist when an interface gets a secondary
1974 * address.
1975 */
1976 ia->ia_subnetmask != (u_int32_t)0xffffffff) {
6d2010ae 1977 IFA_UNLOCK(ifa);
91447636 1978 ifnet_lock_done(ifp);
0a7de745 1979 return TRUE;
91447636 1980 }
6d2010ae 1981 IFA_UNLOCK(ifa);
0b4e3aa0 1982 }
91447636 1983 ifnet_lock_done(ifp);
0a7de745 1984 return FALSE;
1c79356b
A
1985#undef ia
1986}
91447636 1987
6d2010ae
A
1988void
1989in_purgeaddrs(struct ifnet *ifp)
1c79356b 1990{
6d2010ae
A
1991 struct ifaddr **ifap;
1992 int err, i;
1c79356b 1993
39236c6e
A
1994 VERIFY(ifp != NULL);
1995
1c79356b 1996 /*
6d2010ae
A
1997 * Be nice, and try the civilized way first. If we can't get
1998 * rid of them this way, then do it the rough way. We must
1999 * only get here during detach time, after the ifnet has been
2000 * removed from the global list and arrays.
1c79356b 2001 */
6d2010ae 2002 err = ifnet_get_address_list_family_internal(ifp, &ifap, AF_INET, 1,
39236c6e 2003 M_WAITOK, 0);
6d2010ae 2004 if (err == 0 && ifap != NULL) {
39236c6e
A
2005 struct ifreq ifr;
2006
0a7de745
A
2007 bzero(&ifr, sizeof(ifr));
2008 (void) snprintf(ifr.ifr_name, sizeof(ifr.ifr_name),
39236c6e
A
2009 "%s", if_name(ifp));
2010
6d2010ae 2011 for (i = 0; ifap[i] != NULL; i++) {
6d2010ae
A
2012 struct ifaddr *ifa;
2013
2014 ifa = ifap[i];
6d2010ae 2015 IFA_LOCK(ifa);
39236c6e 2016 bcopy(ifa->ifa_addr, &ifr.ifr_addr,
0a7de745 2017 sizeof(struct sockaddr_in));
6d2010ae
A
2018 IFA_UNLOCK(ifa);
2019 err = in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
2020 kernproc);
2021 /* if we lost the race, ignore it */
0a7de745 2022 if (err == EADDRNOTAVAIL) {
6d2010ae 2023 err = 0;
0a7de745 2024 }
6d2010ae
A
2025 if (err != 0) {
2026 char s_addr[MAX_IPv4_STR_LEN];
2027 char s_dstaddr[MAX_IPv4_STR_LEN];
2028 struct in_addr *s, *d;
2029
2030 IFA_LOCK(ifa);
2031 s = &((struct sockaddr_in *)
316670eb 2032 (void *)ifa->ifa_addr)->sin_addr;
6d2010ae 2033 d = &((struct sockaddr_in *)
316670eb 2034 (void *)ifa->ifa_dstaddr)->sin_addr;
6d2010ae 2035 (void) inet_ntop(AF_INET, &s->s_addr, s_addr,
0a7de745 2036 sizeof(s_addr));
6d2010ae 2037 (void) inet_ntop(AF_INET, &d->s_addr, s_dstaddr,
0a7de745 2038 sizeof(s_dstaddr));
6d2010ae
A
2039 IFA_UNLOCK(ifa);
2040
39236c6e
A
2041 printf("%s: SIOCDIFADDR ifp=%s ifa_addr=%s "
2042 "ifa_dstaddr=%s (err=%d)\n", __func__,
2043 ifp->if_xname, s_addr, s_dstaddr, err);
6d2010ae
A
2044 }
2045 }
2046 ifnet_free_address_list(ifap);
2047 } else if (err != 0 && err != ENXIO) {
2048 printf("%s: error retrieving list of AF_INET addresses for "
39236c6e 2049 "ifp=%s (err=%d)\n", __func__, ifp->if_xname, err);
1c79356b 2050 }
1c79356b 2051}
91447636 2052
b0d623f7
A
2053/*
2054 * Called as part of ip_init
2055 */
2056void
2057in_ifaddr_init(void)
2058{
6d2010ae
A
2059 in_multi_init();
2060
0a7de745 2061 PE_parse_boot_argn("ifa_debug", &inifa_debug, sizeof(inifa_debug));
b0d623f7 2062
0a7de745
A
2063 inifa_size = (inifa_debug == 0) ? sizeof(struct in_ifaddr) :
2064 sizeof(struct in_ifaddr_dbg);
b0d623f7 2065
f427ee49 2066 inifa_zone = zone_create(INIFA_ZONE_NAME, inifa_size, ZC_NONE);
6d2010ae
A
2067
2068 lck_mtx_init(&inifa_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
2069 TAILQ_INIT(&inifa_trash_head);
b0d623f7
A
2070}
2071
2072static struct in_ifaddr *
2073in_ifaddr_alloc(int how)
2074{
2075 struct in_ifaddr *inifa;
2076
2077 inifa = (how == M_WAITOK) ? zalloc(inifa_zone) :
2078 zalloc_noblock(inifa_zone);
2079 if (inifa != NULL) {
2080 bzero(inifa, inifa_size);
2081 inifa->ia_ifa.ifa_free = in_ifaddr_free;
2082 inifa->ia_ifa.ifa_debug |= IFD_ALLOC;
cb323159
A
2083 inifa->ia_ifa.ifa_del_wc = &inifa->ia_ifa.ifa_debug;
2084 inifa->ia_ifa.ifa_del_waiters = 0;
6d2010ae 2085 ifa_lock_init(&inifa->ia_ifa);
b0d623f7
A
2086 if (inifa_debug != 0) {
2087 struct in_ifaddr_dbg *inifa_dbg =
2088 (struct in_ifaddr_dbg *)inifa;
2089 inifa->ia_ifa.ifa_debug |= IFD_DEBUG;
2090 inifa->ia_ifa.ifa_trace = in_ifaddr_trace;
6d2010ae
A
2091 inifa->ia_ifa.ifa_attached = in_ifaddr_attached;
2092 inifa->ia_ifa.ifa_detached = in_ifaddr_detached;
b0d623f7
A
2093 ctrace_record(&inifa_dbg->inifa_alloc);
2094 }
2095 }
0a7de745 2096 return inifa;
b0d623f7
A
2097}
2098
2099static void
2100in_ifaddr_free(struct ifaddr *ifa)
2101{
6d2010ae
A
2102 IFA_LOCK_ASSERT_HELD(ifa);
2103
2104 if (ifa->ifa_refcnt != 0) {
b0d623f7 2105 panic("%s: ifa %p bad ref cnt", __func__, ifa);
6d2010ae 2106 /* NOTREACHED */
0a7de745
A
2107 }
2108 if (!(ifa->ifa_debug & IFD_ALLOC)) {
b0d623f7 2109 panic("%s: ifa %p cannot be freed", __func__, ifa);
6d2010ae
A
2110 /* NOTREACHED */
2111 }
b0d623f7
A
2112 if (ifa->ifa_debug & IFD_DEBUG) {
2113 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2114 ctrace_record(&inifa_dbg->inifa_free);
2115 bcopy(&inifa_dbg->inifa, &inifa_dbg->inifa_old,
0a7de745 2116 sizeof(struct in_ifaddr));
6d2010ae
A
2117 if (ifa->ifa_debug & IFD_TRASHED) {
2118 /* Become a regular mutex, just in case */
2119 IFA_CONVERT_LOCK(ifa);
2120 lck_mtx_lock(&inifa_trash_lock);
2121 TAILQ_REMOVE(&inifa_trash_head, inifa_dbg,
2122 inifa_trash_link);
2123 lck_mtx_unlock(&inifa_trash_lock);
2124 ifa->ifa_debug &= ~IFD_TRASHED;
2125 }
b0d623f7 2126 }
6d2010ae
A
2127 IFA_UNLOCK(ifa);
2128 ifa_lock_destroy(ifa);
0a7de745 2129 bzero(ifa, sizeof(struct in_ifaddr));
b0d623f7
A
2130 zfree(inifa_zone, ifa);
2131}
2132
6d2010ae
A
2133static void
2134in_ifaddr_attached(struct ifaddr *ifa)
2135{
2136 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2137
2138 IFA_LOCK_ASSERT_HELD(ifa);
2139
2140 if (!(ifa->ifa_debug & IFD_DEBUG)) {
2141 panic("%s: ifa %p has no debug structure", __func__, ifa);
2142 /* NOTREACHED */
2143 }
2144 if (ifa->ifa_debug & IFD_TRASHED) {
2145 /* Become a regular mutex, just in case */
2146 IFA_CONVERT_LOCK(ifa);
2147 lck_mtx_lock(&inifa_trash_lock);
2148 TAILQ_REMOVE(&inifa_trash_head, inifa_dbg, inifa_trash_link);
2149 lck_mtx_unlock(&inifa_trash_lock);
2150 ifa->ifa_debug &= ~IFD_TRASHED;
2151 }
2152}
2153
2154static void
2155in_ifaddr_detached(struct ifaddr *ifa)
2156{
2157 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2158
2159 IFA_LOCK_ASSERT_HELD(ifa);
2160
2161 if (!(ifa->ifa_debug & IFD_DEBUG)) {
2162 panic("%s: ifa %p has no debug structure", __func__, ifa);
2163 /* NOTREACHED */
2164 } else if (ifa->ifa_debug & IFD_TRASHED) {
2165 panic("%s: ifa %p is already in trash list", __func__, ifa);
2166 /* NOTREACHED */
2167 }
2168 ifa->ifa_debug |= IFD_TRASHED;
2169 /* Become a regular mutex, just in case */
2170 IFA_CONVERT_LOCK(ifa);
2171 lck_mtx_lock(&inifa_trash_lock);
2172 TAILQ_INSERT_TAIL(&inifa_trash_head, inifa_dbg, inifa_trash_link);
2173 lck_mtx_unlock(&inifa_trash_lock);
2174}
2175
b0d623f7
A
2176static void
2177in_ifaddr_trace(struct ifaddr *ifa, int refhold)
2178{
2179 struct in_ifaddr_dbg *inifa_dbg = (struct in_ifaddr_dbg *)ifa;
2180 ctrace_t *tr;
2181 u_int32_t idx;
2182 u_int16_t *cnt;
2183
6d2010ae 2184 if (!(ifa->ifa_debug & IFD_DEBUG)) {
b0d623f7 2185 panic("%s: ifa %p has no debug structure", __func__, ifa);
6d2010ae
A
2186 /* NOTREACHED */
2187 }
b0d623f7
A
2188 if (refhold) {
2189 cnt = &inifa_dbg->inifa_refhold_cnt;
2190 tr = inifa_dbg->inifa_refhold;
2191 } else {
2192 cnt = &inifa_dbg->inifa_refrele_cnt;
2193 tr = inifa_dbg->inifa_refrele;
2194 }
2195
6d2010ae 2196 idx = atomic_add_16_ov(cnt, 1) % INIFA_TRACE_HIST_SIZE;
b0d623f7
A
2197 ctrace_record(&tr[idx]);
2198}
39236c6e
A
2199
2200/*
2201 * Handle SIOCGASSOCIDS ioctl for PF_INET domain.
2202 */
2203static int
2204in_getassocids(struct socket *so, uint32_t *cnt, user_addr_t aidp)
2205{
2206 struct inpcb *inp = sotoinpcb(so);
3e170ce0 2207 sae_associd_t aid;
39236c6e 2208
0a7de745
A
2209 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
2210 return EINVAL;
2211 }
39236c6e
A
2212
2213 /* INPCB has no concept of association */
3e170ce0 2214 aid = SAE_ASSOCID_ANY;
39236c6e
A
2215 *cnt = 0;
2216
2217 /* just asking how many there are? */
0a7de745
A
2218 if (aidp == USER_ADDR_NULL) {
2219 return 0;
2220 }
39236c6e 2221
0a7de745 2222 return copyout(&aid, aidp, sizeof(aid));
39236c6e
A
2223}
2224
2225/*
2226 * Handle SIOCGCONNIDS ioctl for PF_INET domain.
2227 */
2228static int
3e170ce0 2229in_getconnids(struct socket *so, sae_associd_t aid, uint32_t *cnt,
39236c6e
A
2230 user_addr_t cidp)
2231{
2232 struct inpcb *inp = sotoinpcb(so);
3e170ce0 2233 sae_connid_t cid;
39236c6e 2234
0a7de745
A
2235 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
2236 return EINVAL;
2237 }
39236c6e 2238
0a7de745
A
2239 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
2240 return EINVAL;
2241 }
39236c6e
A
2242
2243 /* if connected, return 1 connection count */
2244 *cnt = ((so->so_state & SS_ISCONNECTED) ? 1 : 0);
2245
2246 /* just asking how many there are? */
0a7de745
A
2247 if (cidp == USER_ADDR_NULL) {
2248 return 0;
2249 }
39236c6e
A
2250
2251 /* if INPCB is connected, assign it connid 1 */
3e170ce0 2252 cid = ((*cnt != 0) ? 1 : SAE_CONNID_ANY);
39236c6e 2253
0a7de745 2254 return copyout(&cid, cidp, sizeof(cid));
39236c6e
A
2255}
2256
2257/*
2258 * Handle SIOCGCONNINFO ioctl for PF_INET domain.
2259 */
5ba3f43e 2260int
3e170ce0 2261in_getconninfo(struct socket *so, sae_connid_t cid, uint32_t *flags,
39236c6e
A
2262 uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len,
2263 user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type,
2264 user_addr_t aux_data, uint32_t *aux_len)
2265{
39236c6e
A
2266 struct inpcb *inp = sotoinpcb(so);
2267 struct sockaddr_in sin;
2268 struct ifnet *ifp = NULL;
2269 int error = 0;
2270 u_int32_t copy_len = 0;
2271
2272 /*
2273 * Don't test for INPCB_STATE_DEAD since this may be called
2274 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
2275 */
2276 if (inp == NULL) {
2277 error = EINVAL;
2278 goto out;
2279 }
2280
3e170ce0 2281 if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL && cid != 1) {
39236c6e
A
2282 error = EINVAL;
2283 goto out;
2284 }
2285
2286 ifp = inp->inp_last_outifp;
2287 *ifindex = ((ifp != NULL) ? ifp->if_index : 0);
2288 *soerror = so->so_error;
2289 *flags = 0;
0a7de745 2290 if (so->so_state & SS_ISCONNECTED) {
39236c6e 2291 *flags |= (CIF_CONNECTED | CIF_PREFERRED);
0a7de745
A
2292 }
2293 if (inp->inp_flags & INP_BOUND_IF) {
39236c6e 2294 *flags |= CIF_BOUND_IF;
0a7de745
A
2295 }
2296 if (!(inp->inp_flags & INP_INADDR_ANY)) {
39236c6e 2297 *flags |= CIF_BOUND_IP;
0a7de745
A
2298 }
2299 if (!(inp->inp_flags & INP_ANONPORT)) {
39236c6e 2300 *flags |= CIF_BOUND_PORT;
0a7de745 2301 }
39236c6e 2302
0a7de745
A
2303 bzero(&sin, sizeof(sin));
2304 sin.sin_len = sizeof(sin);
39236c6e
A
2305 sin.sin_family = AF_INET;
2306
2307 /* source address and port */
2308 sin.sin_port = inp->inp_lport;
2309 sin.sin_addr.s_addr = inp->inp_laddr.s_addr;
2310 if (*src_len == 0) {
2311 *src_len = sin.sin_len;
2312 } else {
2313 if (src != USER_ADDR_NULL) {
0a7de745 2314 copy_len = min(*src_len, sizeof(sin));
39236c6e 2315 error = copyout(&sin, src, copy_len);
0a7de745 2316 if (error != 0) {
39236c6e 2317 goto out;
0a7de745 2318 }
39236c6e
A
2319 *src_len = copy_len;
2320 }
2321 }
2322
2323 /* destination address and port */
2324 sin.sin_port = inp->inp_fport;
2325 sin.sin_addr.s_addr = inp->inp_faddr.s_addr;
2326 if (*dst_len == 0) {
2327 *dst_len = sin.sin_len;
2328 } else {
2329 if (dst != USER_ADDR_NULL) {
0a7de745 2330 copy_len = min(*dst_len, sizeof(sin));
39236c6e 2331 error = copyout(&sin, dst, copy_len);
0a7de745 2332 if (error != 0) {
39236c6e 2333 goto out;
0a7de745 2334 }
39236c6e
A
2335 *dst_len = copy_len;
2336 }
2337 }
2338
39236c6e
A
2339 if (SOCK_PROTO(so) == IPPROTO_TCP) {
2340 struct conninfo_tcp tcp_ci;
2341
2342 *aux_type = CIAUX_TCP;
2343 if (*aux_len == 0) {
0a7de745 2344 *aux_len = sizeof(tcp_ci);
39236c6e
A
2345 } else {
2346 if (aux_data != USER_ADDR_NULL) {
0a7de745
A
2347 copy_len = min(*aux_len, sizeof(tcp_ci));
2348 bzero(&tcp_ci, sizeof(tcp_ci));
39236c6e
A
2349 tcp_getconninfo(so, &tcp_ci);
2350 error = copyout(&tcp_ci, aux_data, copy_len);
0a7de745 2351 if (error != 0) {
39236c6e 2352 goto out;
0a7de745 2353 }
39236c6e
A
2354 *aux_len = copy_len;
2355 }
2356 }
5ba3f43e
A
2357 } else {
2358 *aux_type = 0;
2359 *aux_len = 0;
39236c6e
A
2360 }
2361
2362out:
0a7de745 2363 return error;
39236c6e 2364}
5ba3f43e
A
2365
2366struct in_llentry {
2367 struct llentry base;
2368};
2369
2370#define IN_LLTBL_DEFAULT_HSIZE 32
2371#define IN_LLTBL_HASH(k, h) \
2372 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
2373
2374/*
2375 * Do actual deallocation of @lle.
2376 */
2377static void
2378in_lltable_destroy_lle_unlocked(struct llentry *lle)
2379{
2380 LLE_LOCK_DESTROY(lle);
2381 LLE_REQ_DESTROY(lle);
2382 FREE(lle, M_LLTABLE);
2383}
2384
2385/*
2386 * Called by LLE_FREE_LOCKED when number of references
2387 * drops to zero.
2388 */
2389static void
2390in_lltable_destroy_lle(struct llentry *lle)
2391{
2392 LLE_WUNLOCK(lle);
2393 in_lltable_destroy_lle_unlocked(lle);
2394}
2395
2396static struct llentry *
f427ee49 2397in_lltable_new(struct in_addr addr4, uint16_t flags)
5ba3f43e
A
2398{
2399#pragma unused(flags)
2400 struct in_llentry *lle;
2401
2402 MALLOC(lle, struct in_llentry *, sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
0a7de745 2403 if (lle == NULL) { /* NB: caller generates msg */
5ba3f43e 2404 return NULL;
0a7de745 2405 }
5ba3f43e
A
2406
2407 /*
2408 * For IPv4 this will trigger "arpresolve" to generate
2409 * an ARP request.
2410 */
2411 lle->base.la_expire = net_uptime(); /* mark expired */
2412 lle->base.r_l3addr.addr4 = addr4;
2413 lle->base.lle_refcnt = 1;
2414 lle->base.lle_free = in_lltable_destroy_lle;
2415
2416 LLE_LOCK_INIT(&lle->base);
2417 LLE_REQ_INIT(&lle->base);
2418 //callout_init(&lle->base.lle_timer, 1);
2419
0a7de745 2420 return &lle->base;
5ba3f43e
A
2421}
2422
2423#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
2424 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
2425
2426static int
2427in_lltable_match_prefix(const struct sockaddr *saddr,
f427ee49 2428 const struct sockaddr *smask, uint16_t flags, struct llentry *lle)
5ba3f43e
A
2429{
2430 struct in_addr addr, mask, lle_addr;
2431
2432 addr = ((const struct sockaddr_in *)(const void *)saddr)->sin_addr;
2433 mask = ((const struct sockaddr_in *)(const void *)smask)->sin_addr;
2434 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
2435
0a7de745
A
2436 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) {
2437 return 0;
2438 }
5ba3f43e
A
2439
2440 if (lle->la_flags & LLE_IFADDR) {
2441 /*
2442 * Delete LLE_IFADDR records IFF address & flag matches.
2443 * Note that addr is the interface address within prefix
2444 * being matched.
2445 * Note also we should handle 'ifdown' cases without removing
2446 * ifaddr macs.
2447 */
0a7de745
A
2448 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0) {
2449 return 1;
2450 }
2451 return 0;
5ba3f43e
A
2452 }
2453
2454 /* flags & LLE_STATIC means deleting both dynamic and static entries */
0a7de745
A
2455 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) {
2456 return 1;
2457 }
5ba3f43e 2458
0a7de745 2459 return 0;
5ba3f43e
A
2460}
2461
2462static void
2463in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2464{
2465 struct ifnet *ifp;
2466 size_t pkts_dropped;
2467
2468 LLE_WLOCK_ASSERT(lle);
2469 KASSERT(llt != NULL, ("lltable is NULL"));
2470
2471 /* Unlink entry from table if not already */
2472 if ((lle->la_flags & LLE_LINKED) != 0) {
2473 ifp = llt->llt_ifp;
2474 IF_AFDATA_WLOCK_ASSERT(ifp, llt->llt_af);
2475 lltable_unlink_entry(llt, lle);
2476 }
2477
2478#if 0
2479 /* cancel timer */
0a7de745 2480 if (callout_stop(&lle->lle_timer) > 0) {
5ba3f43e 2481 LLE_REMREF(lle);
0a7de745 2482 }
5ba3f43e
A
2483#endif
2484 /* Drop hold queue */
2485 pkts_dropped = llentry_free(lle);
2486 arpstat.dropped += pkts_dropped;
2487}
2488
2489
2490static int
f427ee49 2491in_lltable_rtcheck(struct ifnet *ifp, uint16_t flags, const struct sockaddr *l3addr)
5ba3f43e
A
2492{
2493#pragma unused(flags)
2494 struct rtentry *rt;
2495
2496 KASSERT(l3addr->sa_family == AF_INET,
0a7de745 2497 ("sin_family %d", l3addr->sa_family));
5ba3f43e
A
2498
2499 /* XXX rtalloc1 should take a const param */
2500 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2501 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2502 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
0a7de745
A
2503 inet_ntoa(((const struct sockaddr_in *)(const void *)l3addr)->sin_addr));
2504 if (rt != NULL) {
5ba3f43e 2505 rtfree_locked(rt);
0a7de745
A
2506 }
2507 return EINVAL;
5ba3f43e
A
2508 }
2509 rtfree_locked(rt);
2510 return 0;
2511}
2512
2513static inline uint32_t
2514in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
2515{
0a7de745 2516 return IN_LLTBL_HASH(dst.s_addr, hsize);
5ba3f43e
A
2517}
2518
2519static uint32_t
2520in_lltable_hash(const struct llentry *lle, uint32_t hsize)
2521{
0a7de745 2522 return in_lltable_hash_dst(lle->r_l3addr.addr4, hsize);
5ba3f43e
A
2523}
2524
2525
2526static void
2527in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2528{
2529 struct sockaddr_in *sin;
2530
2531 sin = (struct sockaddr_in *)(void *)sa;
2532 bzero(sin, sizeof(*sin));
2533 sin->sin_family = AF_INET;
2534 sin->sin_len = sizeof(*sin);
2535 sin->sin_addr = lle->r_l3addr.addr4;
2536}
2537
2538static inline struct llentry *
2539in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
2540{
2541 struct llentry *lle;
2542 struct llentries *lleh;
2543 u_int hashidx;
2544
2545 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
2546 lleh = &llt->lle_head[hashidx];
2547 LIST_FOREACH(lle, lleh, lle_next) {
0a7de745 2548 if (lle->la_flags & LLE_DELETED) {
5ba3f43e 2549 continue;
0a7de745
A
2550 }
2551 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) {
5ba3f43e 2552 break;
0a7de745 2553 }
5ba3f43e
A
2554 }
2555
0a7de745 2556 return lle;
5ba3f43e
A
2557}
2558
2559static void
2560in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2561{
2562#pragma unused(llt)
2563 lle->la_flags |= LLE_DELETED;
2564 //EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2565#ifdef DIAGNOSTIC
2566 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2567#endif
2568 llentry_free(lle);
2569}
2570
2571static struct llentry *
f427ee49 2572in_lltable_alloc(struct lltable *llt, uint16_t flags, const struct sockaddr *l3addr)
5ba3f43e
A
2573{
2574 const struct sockaddr_in *sin = (const struct sockaddr_in *) (const void *)l3addr;
2575 struct ifnet *ifp = llt->llt_ifp;
2576 struct llentry *lle;
2577
2578 KASSERT(l3addr->sa_family == AF_INET,
0a7de745 2579 ("sin_family %d", l3addr->sa_family));
5ba3f43e
A
2580
2581 /*
2582 * A route that covers the given address must have
2583 * been installed 1st because we are doing a resolution,
2584 * verify this.
2585 */
2586 if (!(flags & LLE_IFADDR) &&
0a7de745
A
2587 in_lltable_rtcheck(ifp, flags, l3addr) != 0) {
2588 return NULL;
2589 }
5ba3f43e
A
2590
2591 lle = in_lltable_new(sin->sin_addr, flags);
2592 if (lle == NULL) {
2593 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
0a7de745 2594 return NULL;
5ba3f43e
A
2595 }
2596 lle->la_flags = flags & ~LLE_CREATE;
0a7de745 2597 if (flags & LLE_STATIC) {
5ba3f43e 2598 lle->r_flags |= RLLE_VALID;
0a7de745 2599 }
5ba3f43e
A
2600 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2601 lltable_set_entry_addr(ifp, lle, LLADDR(SDL(ifp->if_lladdr->ifa_addr)));
2602 lle->la_flags |= LLE_STATIC;
2603 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
2604 }
0a7de745 2605 return lle;
5ba3f43e
A
2606}
2607
2608/*
2609 * Return NULL if not found or marked for deletion.
2610 * If found return lle read locked.
2611 */
2612static struct llentry *
f427ee49 2613in_lltable_lookup(struct lltable *llt, uint16_t flags, const struct sockaddr *l3addr)
5ba3f43e
A
2614{
2615 const struct sockaddr_in *sin = (const struct sockaddr_in *)(const void *)l3addr;
2616 struct llentry *lle;
2617
2618 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp, llt->llt_af);
2619
2620 KASSERT(l3addr->sa_family == AF_INET,
0a7de745 2621 ("sin_family %d", l3addr->sa_family));
5ba3f43e
A
2622 lle = in_lltable_find_dst(llt, sin->sin_addr);
2623
0a7de745
A
2624 if (lle == NULL) {
2625 return NULL;
2626 }
5ba3f43e 2627
0a7de745
A
2628 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
2629 (LLE_UNLOCKED | LLE_EXCLUSIVE), ("wrong lle request flags: 0x%X",
2630 flags));
5ba3f43e 2631
0a7de745
A
2632 if (flags & LLE_UNLOCKED) {
2633 return lle;
2634 }
5ba3f43e 2635
0a7de745 2636 if (flags & LLE_EXCLUSIVE) {
5ba3f43e 2637 LLE_WLOCK(lle);
0a7de745 2638 } else {
5ba3f43e 2639 LLE_RLOCK(lle);
0a7de745 2640 }
5ba3f43e 2641
0a7de745 2642 return lle;
5ba3f43e
A
2643}
2644
2645static int
2646in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2647 struct sysctl_req *wr)
2648{
2649 struct ifnet *ifp = llt->llt_ifp;
2650 /* XXX stack use */
2651 struct {
2652 struct rt_msghdr rtm;
2653 struct sockaddr_in sin;
2654 struct sockaddr_dl sdl;
2655 } arpc;
2656 struct sockaddr_dl *sdl;
2657 int error;
2658
2659 bzero(&arpc, sizeof(arpc));
2660 /* skip deleted entries */
0a7de745
A
2661 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) {
2662 return 0;
2663 }
5ba3f43e 2664 /* Skip if jailed and not a valid IP of the prison. */
0a7de745 2665 lltable_fill_sa_entry(lle, (struct sockaddr *)&arpc.sin);
5ba3f43e
A
2666 /*
2667 * produce a msg made of:
2668 * struct rt_msghdr;
2669 * struct sockaddr_in; (IPv4)
2670 * struct sockaddr_dl;
2671 */
2672 arpc.rtm.rtm_msglen = sizeof(arpc);
2673 arpc.rtm.rtm_version = RTM_VERSION;
2674 arpc.rtm.rtm_type = RTM_GET;
2675 arpc.rtm.rtm_flags = RTF_UP;
2676 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2677
2678 /* publish */
0a7de745 2679 if (lle->la_flags & LLE_PUB) {
5ba3f43e 2680 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
0a7de745 2681 }
5ba3f43e
A
2682
2683 sdl = &arpc.sdl;
2684 sdl->sdl_family = AF_LINK;
2685 sdl->sdl_len = sizeof(*sdl);
2686 sdl->sdl_index = ifp->if_index;
2687 sdl->sdl_type = ifp->if_type;
2688 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2689 sdl->sdl_alen = ifp->if_addrlen;
2690 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2691 } else {
2692 sdl->sdl_alen = 0;
2693 bzero(LLADDR(sdl), ifp->if_addrlen);
2694 }
2695
2696 arpc.rtm.rtm_rmx.rmx_expire =
f427ee49 2697 lle->la_flags & LLE_STATIC ? 0 : (int32_t)lle->la_expire;
5ba3f43e 2698 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
0a7de745 2699 if (lle->la_flags & LLE_STATIC) {
5ba3f43e 2700 arpc.rtm.rtm_flags |= RTF_STATIC;
0a7de745
A
2701 }
2702 if (lle->la_flags & LLE_IFADDR) {
5ba3f43e 2703 arpc.rtm.rtm_flags |= RTF_PINNED;
0a7de745 2704 }
5ba3f43e
A
2705 arpc.rtm.rtm_flags |= RTF_PINNED;
2706 arpc.rtm.rtm_index = ifp->if_index;
2707 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
2708
0a7de745 2709 return error;
5ba3f43e
A
2710}
2711
2712static struct lltable *
2713in_lltattach(struct ifnet *ifp)
2714{
2715 struct lltable *llt;
2716
2717 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
2718 llt->llt_af = AF_INET;
2719 llt->llt_ifp = ifp;
2720
2721 llt->llt_lookup = in_lltable_lookup;
2722 llt->llt_alloc_entry = in_lltable_alloc;
2723 llt->llt_delete_entry = in_lltable_delete_entry;
2724 llt->llt_dump_entry = in_lltable_dump_entry;
2725 llt->llt_hash = in_lltable_hash;
2726 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
2727 llt->llt_free_entry = in_lltable_free_entry;
2728 llt->llt_match_prefix = in_lltable_match_prefix;
2729 lltable_link(llt);
2730
0a7de745 2731 return llt;
5ba3f43e 2732}
d9a64523
A
2733
2734struct in_ifaddr*
2735inifa_ifpwithflag(struct ifnet * ifp, uint32_t flag)
2736{
2737 struct ifaddr *ifa;
2738
2739 ifnet_lock_shared(ifp);
2740 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link)
2741 {
2742 IFA_LOCK_SPIN(ifa);
2743 if (ifa->ifa_addr->sa_family != AF_INET) {
2744 IFA_UNLOCK(ifa);
2745 continue;
2746 }
2747 if ((((struct in_ifaddr *)ifa)->ia_flags & flag) == flag) {
2748 IFA_ADDREF_LOCKED(ifa);
2749 IFA_UNLOCK(ifa);
2750 break;
2751 }
2752 IFA_UNLOCK(ifa);
2753 }
2754 ifnet_lock_done(ifp);
2755
0a7de745 2756 return (struct in_ifaddr *)ifa;
d9a64523
A
2757}
2758
2759struct in_ifaddr *
2760inifa_ifpclatv4(struct ifnet * ifp)
2761{
2762 struct ifaddr *ifa;
2763
2764 ifnet_lock_shared(ifp);
2765 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_link)
2766 {
2767 uint32_t addr = 0;
2768 IFA_LOCK_SPIN(ifa);
2769 if (ifa->ifa_addr->sa_family != AF_INET) {
2770 IFA_UNLOCK(ifa);
2771 continue;
2772 }
2773
2774 addr = ntohl(SIN(ifa->ifa_addr)->sin_addr.s_addr);
2775 if (!IN_LINKLOCAL(addr) &&
2776 !IN_LOOPBACK(addr)) {
2777 IFA_ADDREF_LOCKED(ifa);
2778 IFA_UNLOCK(ifa);
2779 break;
2780 }
2781 IFA_UNLOCK(ifa);
2782 }
2783 ifnet_lock_done(ifp);
2784
0a7de745 2785 return (struct in_ifaddr *)ifa;
d9a64523 2786}