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