]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / net / if.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) 1980, 1986, 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 * @(#)if.c 8.3 (Berkeley) 1/4/94
9bccf70c 61 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
1c79356b 62 */
2d21ac55
A
63/*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
1c79356b 69
91447636
A
70#include <kern/locks.h>
71
1c79356b
A
72#include <sys/param.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.h>
75#include <sys/systm.h>
76#include <sys/proc.h>
77#include <sys/socket.h>
78#include <sys/socketvar.h>
79#include <sys/protosw.h>
80#include <sys/kernel.h>
81#include <sys/sockio.h>
82#include <sys/syslog.h>
83#include <sys/sysctl.h>
6d2010ae 84#include <sys/mcache.h>
fe8ab488
A
85#include <sys/kauth.h>
86#include <sys/priv.h>
6d2010ae 87#include <kern/zalloc.h>
39037602 88#include <mach/boolean.h>
9bccf70c 89
b0d623f7
A
90#include <machine/endian.h>
91
6d2010ae
A
92#include <pexpert/pexpert.h>
93
1c79356b 94#include <net/if.h>
9bccf70c 95#include <net/if_arp.h>
1c79356b 96#include <net/if_dl.h>
9bccf70c
A
97#include <net/if_types.h>
98#include <net/if_var.h>
0a7de745 99#include <net/if_media.h>
39236c6e 100#include <net/if_ppp.h>
2d21ac55 101#include <net/ethernet.h>
3e170ce0 102#include <net/network_agent.h>
cb323159 103#include <net/pktsched/pktsched_netem.h>
1c79356b 104#include <net/radix.h>
9bccf70c 105#include <net/route.h>
1c79356b 106#include <net/dlil.h>
5ba3f43e
A
107#include <net/nwk_wq.h>
108
1c79356b 109#include <sys/domain.h>
91447636 110#include <libkern/OSAtomic.h>
9bccf70c 111
f427ee49 112#if INET
9bccf70c
A
113#include <netinet/in.h>
114#include <netinet/in_var.h>
39037602 115#include <netinet/in_tclass.h>
c910b4d9 116#include <netinet/ip_var.h>
d9a64523 117#include <netinet/ip.h>
6d2010ae 118#include <netinet/ip6.h>
316670eb
A
119#include <netinet/ip_var.h>
120#include <netinet/tcp.h>
121#include <netinet/tcp_var.h>
122#include <netinet/udp.h>
123#include <netinet/udp_var.h>
9bccf70c
A
124#include <netinet6/in6_var.h>
125#include <netinet6/in6_ifattach.h>
6d2010ae 126#include <netinet6/ip6_var.h>
39236c6e 127#include <netinet6/nd6.h>
f427ee49 128#endif /* INET */
2d21ac55 129
cb323159 130
0a7de745
A
131#include <os/log.h>
132
1c79356b
A
133/*
134 * System initialization
135 */
136
39236c6e
A
137extern char *proc_name_address(void *);
138
6d2010ae 139/* Lock group and attribute for ifaddr lock */
0a7de745
A
140lck_attr_t *ifa_mtx_attr;
141lck_grp_t *ifa_mtx_grp;
142static lck_grp_attr_t *ifa_mtx_grp_attr;
6d2010ae 143
316670eb
A
144static int ifioctl_ifreq(struct socket *, u_long, struct ifreq *,
145 struct proc *);
39236c6e
A
146static int ifioctl_ifconf(u_long, caddr_t);
147static int ifioctl_ifclone(u_long, caddr_t);
39037602 148static int ifioctl_iforder(u_long, caddr_t);
39236c6e
A
149static int ifioctl_ifdesc(struct ifnet *, u_long, caddr_t, struct proc *);
150static int ifioctl_linkparams(struct ifnet *, u_long, caddr_t, struct proc *);
151static int ifioctl_qstats(struct ifnet *, u_long, caddr_t);
152static int ifioctl_throttle(struct ifnet *, u_long, caddr_t, struct proc *);
3e170ce0 153static int ifioctl_netsignature(struct ifnet *, u_long, caddr_t);
91447636 154static int ifconf(u_long cmd, user_addr_t ifrp, int * ret_space);
91447636
A
155__private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *);
156void if_rtproto_del(struct ifnet *ifp, int protocol);
1c79356b 157
6d2010ae
A
158static int if_addmulti_common(struct ifnet *, const struct sockaddr *,
159 struct ifmultiaddr **, int);
160static int if_delmulti_common(struct ifmultiaddr *, struct ifnet *,
161 const struct sockaddr *, int);
39037602 162static struct ifnet *ifunit_common(const char *, boolean_t);
6d2010ae 163
2d21ac55
A
164static int if_rtmtu(struct radix_node *, void *);
165static void if_rtmtu_update(struct ifnet *);
166
39236c6e 167static int if_clone_list(int, int *, user_addr_t);
4a249263 168
1c79356b 169MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
1c79356b 170
0a7de745 171struct ifnethead ifnet_head = TAILQ_HEAD_INITIALIZER(ifnet_head);
1c79356b 172
39037602 173/* ifnet_ordered_head and if_ordered_count are protected by the ifnet_head lock */
0a7de745
A
174struct ifnethead ifnet_ordered_head = TAILQ_HEAD_INITIALIZER(ifnet_ordered_head);
175static u_int32_t if_ordered_count = 0;
39037602 176
0a7de745 177static int if_cloners_count;
4a249263
A
178LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
179
c910b4d9
A
180static struct ifaddr *ifa_ifwithnet_common(const struct sockaddr *,
181 unsigned int);
6d2010ae
A
182static void if_attach_ifa_common(struct ifnet *, struct ifaddr *, int);
183static void if_detach_ifa_common(struct ifnet *, struct ifaddr *, int);
184
185static void if_attach_ifma(struct ifnet *, struct ifmultiaddr *, int);
186static int if_detach_ifma(struct ifnet *, struct ifmultiaddr *, int);
187
188static struct ifmultiaddr *ifma_alloc(int);
189static void ifma_free(struct ifmultiaddr *);
190static void ifma_trace(struct ifmultiaddr *, int);
191
192#if DEBUG
0a7de745 193static unsigned int ifma_debug = 1; /* debugging (enabled) */
6d2010ae 194#else
0a7de745 195static unsigned int ifma_debug; /* debugging (disabled) */
6d2010ae 196#endif /* !DEBUG */
0a7de745
A
197static unsigned int ifma_size; /* size of zone element */
198static struct zone *ifma_zone; /* zone for ifmultiaddr */
6d2010ae 199
0a7de745 200#define IFMA_TRACE_HIST_SIZE 32 /* size of trace history */
6d2010ae
A
201
202/* For gdb */
203__private_extern__ unsigned int ifma_trace_hist_size = IFMA_TRACE_HIST_SIZE;
204
205struct ifmultiaddr_dbg {
0a7de745
A
206 struct ifmultiaddr ifma; /* ifmultiaddr */
207 u_int16_t ifma_refhold_cnt; /* # of ref */
208 u_int16_t ifma_refrele_cnt; /* # of rele */
6d2010ae
A
209 /*
210 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
211 */
0a7de745
A
212 ctrace_t ifma_refhold[IFMA_TRACE_HIST_SIZE];
213 ctrace_t ifma_refrele[IFMA_TRACE_HIST_SIZE];
6d2010ae
A
214 /*
215 * Trash list linkage
216 */
217 TAILQ_ENTRY(ifmultiaddr_dbg) ifma_trash_link;
218};
219
220/* List of trash ifmultiaddr entries protected by ifma_trash_lock */
221static TAILQ_HEAD(, ifmultiaddr_dbg) ifma_trash_head;
222static decl_lck_mtx_data(, ifma_trash_lock);
223
0a7de745
A
224#define IFMA_ZONE_MAX 64 /* maximum elements in zone */
225#define IFMA_ZONE_NAME "ifmultiaddr" /* zone name */
c910b4d9 226
1c79356b
A
227/*
228 * XXX: declare here to avoid to include many inet6 related files..
229 * should be more generalized?
230 */
0a7de745 231extern void nd6_setmtu(struct ifnet *);
6d2010ae 232extern lck_mtx_t *nd6_mutex;
1c79356b 233
0a7de745
A
234SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Link layers");
235SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
236 "Generic link-management");
39236c6e
A
237
238SYSCTL_DECL(_net_link_generic_system);
239
240static uint32_t if_verbose = 0;
241SYSCTL_INT(_net_link_generic_system, OID_AUTO, if_verbose,
242 CTLFLAG_RW | CTLFLAG_LOCKED, &if_verbose, 0, "");
4a249263 243
cb323159
A
244#if (DEBUG || DEVELOPMENT)
245static uint32_t default_tcp_kao_max = 0;
246SYSCTL_INT(_net_link_generic_system, OID_AUTO, default_tcp_kao_max,
247 CTLFLAG_RW | CTLFLAG_LOCKED, &default_tcp_kao_max, 0, "");
248#else
249static const uint32_t default_tcp_kao_max = 0;
250#endif /* (DEBUG || DEVELOPMENT) */
251
f427ee49
A
252u_int32_t companion_link_sock_buffer_limit = 0;
253
254static int
255sysctl_set_companion_link_sock_buf_limit SYSCTL_HANDLER_ARGS
256{
257#pragma unused(arg1, arg2)
258 int error, tmp = companion_link_sock_buffer_limit;
259 error = sysctl_handle_int(oidp, &tmp, 0, req);
260 if (tmp < 0) {
261 return EINVAL;
262 }
263 if ((error = priv_check_cred(kauth_cred_get(),
264 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
265 return error;
266 }
267
268 u_int32_t new_limit = tmp;
269 if (new_limit == companion_link_sock_buffer_limit) {
270 return 0;
271 }
272
273 bool recover = new_limit == 0 ? true : false;
274 if (recover) {
275 error = inp_recover_companion_link(&tcbinfo);
276 } else {
277 error = inp_limit_companion_link(&tcbinfo, new_limit);
278 }
279 if (!error) {
280 companion_link_sock_buffer_limit = new_limit;
281 }
282 return error;
283}
284
285SYSCTL_PROC(_net_link_generic_system, OID_AUTO, companion_sndbuf_limit,
286 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY,
287 &companion_link_sock_buffer_limit, 0, sysctl_set_companion_link_sock_buf_limit,
288 "I", "set sock send buffer limit of connections using companion links");
289
5c9f4661
A
290boolean_t intcoproc_unrestricted;
291
5ba3f43e
A
292/* Eventhandler context for interface events */
293struct eventhandler_lists_ctxt ifnet_evhdlr_ctxt;
294
6d2010ae
A
295void
296ifa_init(void)
297{
298 /* Setup lock group and attribute for ifaddr */
299 ifa_mtx_grp_attr = lck_grp_attr_alloc_init();
300 ifa_mtx_grp = lck_grp_alloc_init("ifaddr", ifa_mtx_grp_attr);
301 ifa_mtx_attr = lck_attr_alloc_init();
302
0a7de745 303 PE_parse_boot_argn("ifa_debug", &ifma_debug, sizeof(ifma_debug));
6d2010ae 304
0a7de745
A
305 ifma_size = (ifma_debug == 0) ? sizeof(struct ifmultiaddr) :
306 sizeof(struct ifmultiaddr_dbg);
6d2010ae 307
f427ee49 308 ifma_zone = zone_create(IFMA_ZONE_NAME, ifma_size, ZC_NONE);
6d2010ae
A
309 lck_mtx_init(&ifma_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
310 TAILQ_INIT(&ifma_trash_head);
5c9f4661
A
311
312 PE_parse_boot_argn("intcoproc_unrestricted", &intcoproc_unrestricted,
0a7de745 313 sizeof(intcoproc_unrestricted));
6d2010ae
A
314}
315
1c79356b
A
316/*
317 * Network interface utility routines.
318 *
319 * Routines with ifa_ifwith* names take sockaddr *'s as
320 * parameters.
1c79356b 321 */
1c79356b 322
ab86ba33 323int if_index;
1c79356b 324struct ifaddr **ifnet_addrs;
ab86ba33 325struct ifnet **ifindex2ifnet;
1c79356b 326
91447636 327__private_extern__ void
6d2010ae
A
328if_attach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
329{
330 if_attach_ifa_common(ifp, ifa, 0);
331}
332
333__private_extern__ void
334if_attach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
335{
336 if_attach_ifa_common(ifp, ifa, 1);
337}
338
339static void
340if_attach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
91447636 341{
6d2010ae
A
342 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
343 IFA_LOCK_ASSERT_HELD(ifa);
344
345 if (ifa->ifa_ifp != ifp) {
346 panic("%s: Mismatch ifa_ifp=%p != ifp=%p", __func__,
347 ifa->ifa_ifp, ifp);
348 /* NOTREACHED */
349 } else if (ifa->ifa_debug & IFD_ATTACHED) {
350 panic("%s: Attempt to attach an already attached ifa=%p",
351 __func__, ifa);
352 /* NOTREACHED */
353 } else if (link && !(ifa->ifa_debug & IFD_LINK)) {
354 panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
355 /* NOTREACHED */
356 } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
357 panic("%s: Unexpected link address ifa=%p", __func__, ifa);
358 /* NOTREACHED */
91447636 359 }
6d2010ae 360 IFA_ADDREF_LOCKED(ifa);
b0d623f7 361 ifa->ifa_debug |= IFD_ATTACHED;
0a7de745 362 if (link) {
6d2010ae 363 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
0a7de745 364 } else {
6d2010ae 365 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
0a7de745 366 }
6d2010ae 367
0a7de745 368 if (ifa->ifa_attached != NULL) {
6d2010ae 369 (*ifa->ifa_attached)(ifa);
0a7de745 370 }
a39ff7e2 371
91447636
A
372}
373
374__private_extern__ void
6d2010ae
A
375if_detach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
376{
377 if_detach_ifa_common(ifp, ifa, 0);
378}
379
380__private_extern__ void
381if_detach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
382{
383 if_detach_ifa_common(ifp, ifa, 1);
384}
385
386static void
387if_detach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
388{
389 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
390 IFA_LOCK_ASSERT_HELD(ifa);
391
392 if (link && !(ifa->ifa_debug & IFD_LINK)) {
393 panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
394 /* NOTREACHED */
395 } else if (link && ifa != TAILQ_FIRST(&ifp->if_addrhead)) {
396 panic("%s: Link address ifa=%p not first", __func__, ifa);
397 /* NOTREACHED */
398 } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
399 panic("%s: Unexpected link address ifa=%p", __func__, ifa);
400 /* NOTREACHED */
401 } else if (!(ifa->ifa_debug & IFD_ATTACHED)) {
402 panic("%s: Attempt to detach an unattached address ifa=%p",
403 __func__, ifa);
404 /* NOTREACHED */
405 } else if (ifa->ifa_ifp != ifp) {
406 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__,
407 ifa->ifa_ifp, ifp);
408 /* NOTREACHED */
409 } else if (ifa->ifa_debug & IFD_DEBUG) {
91447636
A
410 struct ifaddr *ifa2;
411 TAILQ_FOREACH(ifa2, &ifp->if_addrhead, ifa_link) {
0a7de745 412 if (ifa2 == ifa) {
91447636 413 break;
0a7de745 414 }
91447636
A
415 }
416 if (ifa2 != ifa) {
6d2010ae
A
417 panic("%s: Attempt to detach a stray address ifa=%p",
418 __func__, ifa);
419 /* NOTREACHED */
420 }
91447636 421 }
91447636 422 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
6d2010ae
A
423 /* This must not be the last reference to the ifaddr */
424 if (IFA_REMREF_LOCKED(ifa) == NULL) {
425 panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
426 /* NOTREACHED */
427 }
cb323159 428 ifa->ifa_debug &= ~IFD_ATTACHED;
6d2010ae 429
0a7de745 430 if (ifa->ifa_detached != NULL) {
6d2010ae 431 (*ifa->ifa_detached)(ifa);
0a7de745 432 }
5ba3f43e 433
91447636
A
434}
435
0a7de745 436#define INITIAL_IF_INDEXLIM 8
ab86ba33
A
437
438/*
439 * Function: if_next_index
440 * Purpose:
39037602
A
441 * Return the next available interface index.
442 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
ab86ba33
A
443 * added entry when necessary.
444 *
445 * Note:
446 * ifnet_addrs[] is indexed by (if_index - 1), whereas
447 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
448 * always allocate one extra element to hold ifindex2ifnet[0], which
449 * is unused.
450 */
91447636
A
451int if_next_index(void);
452
453__private_extern__ int
ab86ba33
A
454if_next_index(void)
455{
0a7de745
A
456 static int if_indexlim = 0;
457 int new_index;
ab86ba33 458
ab86ba33
A
459 new_index = ++if_index;
460 if (if_index > if_indexlim) {
0a7de745
A
461 unsigned n;
462 int new_if_indexlim;
463 caddr_t new_ifnet_addrs;
464 caddr_t new_ifindex2ifnet;
465 caddr_t old_ifnet_addrs;
ab86ba33 466
ab86ba33
A
467 old_ifnet_addrs = (caddr_t)ifnet_addrs;
468 if (ifnet_addrs == NULL) {
469 new_if_indexlim = INITIAL_IF_INDEXLIM;
470 } else {
471 new_if_indexlim = if_indexlim << 1;
472 }
473
474 /* allocate space for the larger arrays */
475 n = (2 * new_if_indexlim + 1) * sizeof(caddr_t);
3e170ce0 476 new_ifnet_addrs = _MALLOC(n, M_IFADDR, M_WAITOK | M_ZERO);
b0d623f7
A
477 if (new_ifnet_addrs == NULL) {
478 --if_index;
0a7de745 479 return -1;
b0d623f7
A
480 }
481
39037602 482 new_ifindex2ifnet = new_ifnet_addrs
0a7de745 483 + new_if_indexlim * sizeof(caddr_t);
ab86ba33
A
484 if (ifnet_addrs != NULL) {
485 /* copy the existing data */
486 bcopy((caddr_t)ifnet_addrs, new_ifnet_addrs,
39037602 487 if_indexlim * sizeof(caddr_t));
ab86ba33 488 bcopy((caddr_t)ifindex2ifnet,
39037602
A
489 new_ifindex2ifnet,
490 (if_indexlim + 1) * sizeof(caddr_t));
ab86ba33
A
491 }
492
493 /* switch to the new tables and size */
316670eb
A
494 ifnet_addrs = (struct ifaddr **)(void *)new_ifnet_addrs;
495 ifindex2ifnet = (struct ifnet **)(void *)new_ifindex2ifnet;
ab86ba33
A
496 if_indexlim = new_if_indexlim;
497
498 /* release the old data */
499 if (old_ifnet_addrs != NULL) {
500 _FREE((caddr_t)old_ifnet_addrs, M_IFADDR);
501 }
ab86ba33 502 }
0a7de745 503 return new_index;
1c79356b 504}
9bccf70c 505
4a249263
A
506/*
507 * Create a clone network interface.
508 */
509static int
d1ecb069 510if_clone_create(char *name, int len, void *params)
4a249263
A
511{
512 struct if_clone *ifc;
513 char *dp;
d1ecb069
A
514 int wildcard;
515 u_int32_t bytoff, bitoff;
516 u_int32_t unit;
4a249263
A
517 int err;
518
519 ifc = if_clone_lookup(name, &unit);
0a7de745
A
520 if (ifc == NULL) {
521 return EINVAL;
522 }
4a249263 523
0a7de745
A
524 if (ifunit(name) != NULL) {
525 return EEXIST;
526 }
4a249263
A
527
528 bytoff = bitoff = 0;
d1ecb069 529 wildcard = (unit == UINT32_MAX);
4a249263
A
530 /*
531 * Find a free unit if none was given.
532 */
533 if (wildcard) {
39037602 534 while ((bytoff < ifc->ifc_bmlen) &&
0a7de745 535 (ifc->ifc_units[bytoff] == 0xff)) {
4a249263 536 bytoff++;
0a7de745
A
537 }
538 if (bytoff >= ifc->ifc_bmlen) {
539 return ENOSPC;
540 }
541 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
4a249263 542 bitoff++;
0a7de745 543 }
4a249263
A
544 unit = (bytoff << 3) + bitoff;
545 }
546
0a7de745
A
547 if (unit > ifc->ifc_maxunit) {
548 return ENXIO;
549 }
4a249263 550
d9a64523 551 lck_mtx_lock(&ifc->ifc_mutex);
d1ecb069 552 err = (*ifc->ifc_create)(ifc, unit, params);
d9a64523
A
553 if (err != 0) {
554 lck_mtx_unlock(&ifc->ifc_mutex);
0a7de745 555 return err;
d9a64523 556 }
4a249263
A
557
558 if (!wildcard) {
559 bytoff = unit >> 3;
560 bitoff = unit - (bytoff << 3);
561 }
562
563 /*
564 * Allocate the unit in the bitmap.
565 */
566 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
567 ("%s: bit is already set", __func__));
568 ifc->ifc_units[bytoff] |= (1 << bitoff);
569
570 /* In the wildcard case, we need to update the name. */
571 if (wildcard) {
0a7de745
A
572 for (dp = name; *dp != '\0'; dp++) {
573 ;
574 }
575 if (snprintf(dp, len - (dp - name), "%d", unit) >
576 len - (dp - name) - 1) {
4a249263
A
577 /*
578 * This can only be a programmer error and
579 * there's no straightforward way to recover if
580 * it happens.
581 */
6d2010ae
A
582 panic("%s: interface name too long", __func__);
583 /* NOTREACHED */
4a249263 584 }
4a249263 585 }
d9a64523 586 lck_mtx_unlock(&ifc->ifc_mutex);
4a249263 587
0a7de745 588 return 0;
4a249263
A
589}
590
591/*
592 * Destroy a clone network interface.
593 */
91447636 594static int
4a249263
A
595if_clone_destroy(const char *name)
596{
d9a64523
A
597 struct if_clone *ifc = NULL;
598 struct ifnet *ifp = NULL;
4a249263 599 int bytoff, bitoff;
d1ecb069 600 u_int32_t unit;
d9a64523 601 int error = 0;
4a249263
A
602
603 ifc = if_clone_lookup(name, &unit);
4a249263 604
d9a64523
A
605 if (ifc == NULL) {
606 error = EINVAL;
607 goto done;
608 }
4a249263 609
d9a64523
A
610 if (unit < ifc->ifc_minifs) {
611 error = EINVAL;
612 goto done;
613 }
4a249263 614
d9a64523
A
615 ifp = ifunit_ref(name);
616 if (ifp == NULL) {
617 error = ENXIO;
618 goto done;
619 }
620
621 if (ifc->ifc_destroy == NULL) {
622 error = EOPNOTSUPP;
623 goto done;
624 }
4a249263 625
d9a64523
A
626 lck_mtx_lock(&ifc->ifc_mutex);
627 error = (*ifc->ifc_destroy)(ifp);
4a249263 628
d9a64523
A
629 if (error) {
630 lck_mtx_unlock(&ifc->ifc_mutex);
631 goto done;
632 }
633
634 /* Compute offset in the bitmap and deallocate the unit. */
4a249263
A
635 bytoff = unit >> 3;
636 bitoff = unit - (bytoff << 3);
637 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
638 ("%s: bit is already cleared", __func__));
639 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
d9a64523
A
640 lck_mtx_unlock(&ifc->ifc_mutex);
641
642done:
0a7de745 643 if (ifp != NULL) {
d9a64523 644 ifnet_decr_iorefcnt(ifp);
0a7de745
A
645 }
646 return error;
4a249263
A
647}
648
649/*
650 * Look up a network interface cloner.
651 */
652
d1ecb069
A
653__private_extern__ struct if_clone *
654if_clone_lookup(const char *name, u_int32_t *unitp)
4a249263
A
655{
656 struct if_clone *ifc;
657 const char *cp;
39236c6e 658 u_int32_t i;
4a249263 659
0a7de745 660 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
4a249263 661 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
0a7de745 662 if (ifc->ifc_name[i] != *cp) {
4a249263 663 goto next_ifc;
0a7de745 664 }
4a249263
A
665 }
666 goto found_name;
39037602 667next_ifc:
4a249263
A
668 ifc = LIST_NEXT(ifc, ifc_list);
669 }
670
671 /* No match. */
0a7de745 672 return (struct if_clone *)NULL;
4a249263 673
39037602 674found_name:
4a249263 675 if (*cp == '\0') {
39236c6e 676 i = UINT32_MAX;
4a249263
A
677 } else {
678 for (i = 0; *cp != '\0'; cp++) {
679 if (*cp < '0' || *cp > '9') {
680 /* Bogus unit number. */
0a7de745 681 return NULL;
4a249263
A
682 }
683 i = (i * 10) + (*cp - '0');
684 }
685 }
686
0a7de745 687 if (unitp != NULL) {
4a249263 688 *unitp = i;
0a7de745
A
689 }
690 return ifc;
4a249263
A
691}
692
d9a64523
A
693void *
694if_clone_softc_allocate(const struct if_clone *ifc)
695{
696 void *p_clone = NULL;
697
698 VERIFY(ifc != NULL);
699
700 p_clone = zalloc(ifc->ifc_zone);
0a7de745 701 if (p_clone != NULL) {
d9a64523 702 bzero(p_clone, ifc->ifc_softc_size);
0a7de745 703 }
d9a64523 704
0a7de745 705 return p_clone;
d9a64523
A
706}
707
708void
709if_clone_softc_deallocate(const struct if_clone *ifc, void *p_softc)
710{
711 VERIFY(ifc != NULL && p_softc != NULL);
712 bzero(p_softc, ifc->ifc_softc_size);
713 zfree(ifc->ifc_zone, p_softc);
714}
715
4a249263
A
716/*
717 * Register a network interface cloner.
718 */
b0d623f7 719int
4a249263
A
720if_clone_attach(struct if_clone *ifc)
721{
722 int bytoff, bitoff;
723 int err;
724 int len, maxclone;
d1ecb069 725 u_int32_t unit;
4a249263
A
726
727 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
728 ("%s: %s requested more units then allowed (%d > %d)",
729 __func__, ifc->ifc_name, ifc->ifc_minifs,
730 ifc->ifc_maxunit + 1));
731 /*
732 * Compute bitmap size and allocate it.
733 */
734 maxclone = ifc->ifc_maxunit + 1;
735 len = maxclone >> 3;
0a7de745 736 if ((len << 3) < maxclone) {
4a249263 737 len++;
0a7de745 738 }
4a249263 739 ifc->ifc_units = _MALLOC(len, M_CLONE, M_WAITOK | M_ZERO);
0a7de745
A
740 if (ifc->ifc_units == NULL) {
741 return ENOBUFS;
742 }
4a249263 743 ifc->ifc_bmlen = len;
d9a64523
A
744 lck_mtx_init(&ifc->ifc_mutex, ifnet_lock_group, ifnet_lock_attr);
745
746 if (ifc->ifc_softc_size != 0) {
f427ee49
A
747 ifc->ifc_zone = zone_create(ifc->ifc_name, ifc->ifc_softc_size,
748 ZC_DESTRUCTIBLE);
d9a64523 749 }
4a249263
A
750
751 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
752 if_cloners_count++;
753
754 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
d1ecb069 755 err = (*ifc->ifc_create)(ifc, unit, NULL);
4a249263
A
756 KASSERT(err == 0,
757 ("%s: failed to create required interface %s%d",
758 __func__, ifc->ifc_name, unit));
759
760 /* Allocate the unit in the bitmap. */
761 bytoff = unit >> 3;
762 bitoff = unit - (bytoff << 3);
763 ifc->ifc_units[bytoff] |= (1 << bitoff);
764 }
b0d623f7 765
0a7de745 766 return 0;
4a249263
A
767}
768
769/*
770 * Unregister a network interface cloner.
771 */
772void
773if_clone_detach(struct if_clone *ifc)
774{
4a249263
A
775 LIST_REMOVE(ifc, ifc_list);
776 FREE(ifc->ifc_units, M_CLONE);
0a7de745 777 if (ifc->ifc_softc_size != 0) {
d9a64523 778 zdestroy(ifc->ifc_zone);
0a7de745 779 }
d9a64523
A
780
781 lck_mtx_destroy(&ifc->ifc_mutex, ifnet_lock_group);
4a249263
A
782 if_cloners_count--;
783}
784
785/*
786 * Provide list of interface cloners to userspace.
787 */
788static int
39236c6e 789if_clone_list(int count, int *ret_total, user_addr_t dst)
4a249263 790{
91447636 791 char outbuf[IFNAMSIZ];
4a249263 792 struct if_clone *ifc;
91447636 793 int error = 0;
4a249263 794
39236c6e 795 *ret_total = if_cloners_count;
91447636 796 if (dst == USER_ADDR_NULL) {
4a249263 797 /* Just asking how many there are. */
0a7de745 798 return 0;
4a249263
A
799 }
800
0a7de745
A
801 if (count < 0) {
802 return EINVAL;
803 }
4a249263 804
91447636 805 count = (if_cloners_count < count) ? if_cloners_count : count;
4a249263
A
806
807 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
39037602 808 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
490019cf 809 bzero(outbuf, sizeof(outbuf));
a39ff7e2 810 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
4a249263 811 error = copyout(outbuf, dst, IFNAMSIZ);
0a7de745 812 if (error) {
4a249263 813 break;
0a7de745 814 }
4a249263
A
815 }
816
0a7de745 817 return error;
4a249263
A
818}
819
3e170ce0 820u_int32_t
39037602 821if_functional_type(struct ifnet *ifp, bool exclude_delegate)
3e170ce0
A
822{
823 u_int32_t ret = IFRTYPE_FUNCTIONAL_UNKNOWN;
cb323159 824
3e170ce0
A
825 if (ifp != NULL) {
826 if (ifp->if_flags & IFF_LOOPBACK) {
827 ret = IFRTYPE_FUNCTIONAL_LOOPBACK;
cb323159
A
828 } else if (IFNET_IS_COMPANION_LINK(ifp)) {
829 ret = IFRTYPE_FUNCTIONAL_COMPANIONLINK;
39037602 830 } else if ((exclude_delegate &&
cb323159
A
831 (ifp->if_family == IFNET_FAMILY_ETHERNET &&
832 ifp->if_subfamily == IFNET_SUBFAMILY_WIFI)) ||
39037602 833 (!exclude_delegate && IFNET_IS_WIFI(ifp))) {
0a7de745 834 if (ifp->if_eflags & IFEF_AWDL) {
3e170ce0 835 ret = IFRTYPE_FUNCTIONAL_WIFI_AWDL;
0a7de745 836 } else {
3e170ce0 837 ret = IFRTYPE_FUNCTIONAL_WIFI_INFRA;
0a7de745 838 }
39037602
A
839 } else if ((exclude_delegate &&
840 (ifp->if_type == IFT_CELLULAR)) ||
841 (!exclude_delegate && IFNET_IS_CELLULAR(ifp))) {
3e170ce0 842 ret = IFRTYPE_FUNCTIONAL_CELLULAR;
743345f9
A
843 } else if (IFNET_IS_INTCOPROC(ifp)) {
844 ret = IFRTYPE_FUNCTIONAL_INTCOPROC;
39037602
A
845 } else if ((exclude_delegate &&
846 (ifp->if_family == IFNET_FAMILY_ETHERNET ||
d9a64523
A
847 ifp->if_family == IFNET_FAMILY_BOND ||
848 ifp->if_family == IFNET_FAMILY_VLAN ||
39037602
A
849 ifp->if_family == IFNET_FAMILY_FIREWIRE)) ||
850 (!exclude_delegate && IFNET_IS_WIRED(ifp))) {
3e170ce0
A
851 ret = IFRTYPE_FUNCTIONAL_WIRED;
852 }
853 }
854
0a7de745 855 return ret;
3e170ce0
A
856}
857
b0d623f7
A
858/*
859 * Similar to ifa_ifwithaddr, except that this is IPv4 specific
860 * and that it matches only the local (not broadcast) address.
861 */
862__private_extern__ struct in_ifaddr *
863ifa_foraddr(unsigned int addr)
55e303ae 864{
0a7de745 865 return ifa_foraddr_scoped(addr, IFSCOPE_NONE);
b0d623f7
A
866}
867
868/*
869 * Similar to ifa_foraddr, except with the added interface scope
870 * constraint (unless the caller passes in IFSCOPE_NONE in which
871 * case there is no scope restriction).
872 */
873__private_extern__ struct in_ifaddr *
874ifa_foraddr_scoped(unsigned int addr, unsigned int scope)
875{
876 struct in_ifaddr *ia = NULL;
877
878 lck_rw_lock_shared(in_ifaddr_rwlock);
879 TAILQ_FOREACH(ia, INADDR_HASH(addr), ia_hash) {
6d2010ae 880 IFA_LOCK_SPIN(&ia->ia_ifa);
b0d623f7 881 if (ia->ia_addr.sin_addr.s_addr == addr &&
6d2010ae 882 (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
0a7de745 883 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
6d2010ae 884 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 885 break;
6d2010ae
A
886 }
887 IFA_UNLOCK(&ia->ia_ifa);
55e303ae 888 }
b0d623f7 889 lck_rw_done(in_ifaddr_rwlock);
0a7de745 890 return ia;
55e303ae
A
891}
892
6d2010ae
A
893/*
894 * Similar to ifa_foraddr, except that this for IPv6.
895 */
896__private_extern__ struct in6_ifaddr *
897ifa_foraddr6(struct in6_addr *addr6)
898{
0a7de745 899 return ifa_foraddr6_scoped(addr6, IFSCOPE_NONE);
6d2010ae
A
900}
901
902__private_extern__ struct in6_ifaddr *
903ifa_foraddr6_scoped(struct in6_addr *addr6, unsigned int scope)
904{
905 struct in6_ifaddr *ia = NULL;
906
907 lck_rw_lock_shared(&in6_ifaddr_rwlock);
eb6b6ca3 908 TAILQ_FOREACH(ia, IN6ADDR_HASH(addr6), ia6_hash) {
6d2010ae
A
909 IFA_LOCK(&ia->ia_ifa);
910 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, addr6) &&
911 (scope == IFSCOPE_NONE || ia->ia_ifp->if_index == scope)) {
912 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */
913 IFA_UNLOCK(&ia->ia_ifa);
914 break;
915 }
916 IFA_UNLOCK(&ia->ia_ifa);
917 }
918 lck_rw_done(&in6_ifaddr_rwlock);
919
0a7de745 920 return ia;
6d2010ae 921}
6d2010ae 922
2d21ac55
A
923/*
924 * Return the first (primary) address of a given family on an interface.
925 */
926__private_extern__ struct ifaddr *
927ifa_ifpgetprimary(struct ifnet *ifp, int family)
928{
6d2010ae 929 struct ifaddr *ifa;
2d21ac55
A
930
931 ifnet_lock_shared(ifp);
932 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
6d2010ae
A
933 IFA_LOCK_SPIN(ifa);
934 if (ifa->ifa_addr->sa_family == family) {
0a7de745 935 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 936 IFA_UNLOCK(ifa);
2d21ac55
A
937 break;
938 }
6d2010ae 939 IFA_UNLOCK(ifa);
2d21ac55 940 }
2d21ac55
A
941 ifnet_lock_done(ifp);
942
0a7de745 943 return ifa;
2d21ac55
A
944}
945
39037602
A
946static inline int
947ifa_equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
948{
0a7de745 949 if (!sa1 || !sa2) {
39037602 950 return 0;
0a7de745
A
951 }
952 if (sa1->sa_len != sa2->sa_len) {
39037602 953 return 0;
0a7de745 954 }
39037602 955
0a7de745 956 return bcmp(sa1, sa2, sa1->sa_len) == 0;
39037602
A
957}
958
1c79356b
A
959/*
960 * Locate an interface based on a complete address.
961 */
1c79356b 962struct ifaddr *
39037602 963ifa_ifwithaddr_locked(const struct sockaddr *addr)
1c79356b 964{
91447636
A
965 struct ifnet *ifp;
966 struct ifaddr *ifa;
2d21ac55 967 struct ifaddr *result = NULL;
1c79356b 968
6d2010ae
A
969 for (ifp = ifnet_head.tqh_first; ifp && !result;
970 ifp = ifp->if_link.tqe_next) {
91447636
A
971 ifnet_lock_shared(ifp);
972 for (ifa = ifp->if_addrhead.tqh_first; ifa;
6d2010ae
A
973 ifa = ifa->ifa_link.tqe_next) {
974 IFA_LOCK_SPIN(ifa);
975 if (ifa->ifa_addr->sa_family != addr->sa_family) {
976 IFA_UNLOCK(ifa);
91447636 977 continue;
6d2010ae 978 }
39037602 979 if (ifa_equal(addr, ifa->ifa_addr)) {
91447636 980 result = ifa;
0a7de745 981 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 982 IFA_UNLOCK(ifa);
91447636
A
983 break;
984 }
6d2010ae
A
985 if ((ifp->if_flags & IFF_BROADCAST) &&
986 ifa->ifa_broadaddr != NULL &&
987 /* IP6 doesn't have broadcast */
988 ifa->ifa_broadaddr->sa_len != 0 &&
39037602 989 ifa_equal(ifa->ifa_broadaddr, addr)) {
91447636 990 result = ifa;
0a7de745 991 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 992 IFA_UNLOCK(ifa);
91447636
A
993 break;
994 }
6d2010ae 995 IFA_UNLOCK(ifa);
91447636 996 }
91447636 997 ifnet_lock_done(ifp);
1c79356b 998 }
39037602 999
0a7de745 1000 return result;
39037602
A
1001}
1002
1003struct ifaddr *
1004ifa_ifwithaddr(const struct sockaddr *addr)
1005{
1006 struct ifaddr *result = NULL;
1007
1008 ifnet_head_lock_shared();
743345f9 1009
39037602 1010 result = ifa_ifwithaddr_locked(addr);
743345f9 1011
91447636 1012 ifnet_head_done();
6d2010ae 1013
0a7de745 1014 return result;
1c79356b
A
1015}
1016/*
1017 * Locate the point to point interface with a given destination address.
1018 */
1019/*ARGSUSED*/
1020struct ifaddr *
6d2010ae 1021ifa_ifwithdstaddr(const struct sockaddr *addr)
1c79356b 1022{
91447636
A
1023 struct ifnet *ifp;
1024 struct ifaddr *ifa;
2d21ac55 1025 struct ifaddr *result = NULL;
91447636
A
1026
1027 ifnet_head_lock_shared();
6d2010ae
A
1028 for (ifp = ifnet_head.tqh_first; ifp && !result;
1029 ifp = ifp->if_link.tqe_next) {
0a7de745 1030 if ((ifp->if_flags & IFF_POINTOPOINT)) {
91447636
A
1031 ifnet_lock_shared(ifp);
1032 for (ifa = ifp->if_addrhead.tqh_first; ifa;
6d2010ae
A
1033 ifa = ifa->ifa_link.tqe_next) {
1034 IFA_LOCK_SPIN(ifa);
1035 if (ifa->ifa_addr->sa_family !=
1036 addr->sa_family) {
1037 IFA_UNLOCK(ifa);
91447636 1038 continue;
6d2010ae 1039 }
39037602 1040 if (ifa_equal(addr, ifa->ifa_dstaddr)) {
91447636 1041 result = ifa;
0a7de745 1042 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 1043 IFA_UNLOCK(ifa);
91447636
A
1044 break;
1045 }
6d2010ae 1046 IFA_UNLOCK(ifa);
91447636 1047 }
91447636
A
1048 ifnet_lock_done(ifp);
1049 }
1c79356b 1050 }
91447636 1051 ifnet_head_done();
0a7de745 1052 return result;
1c79356b
A
1053}
1054
c910b4d9
A
1055/*
1056 * Locate the source address of an interface based on a complete address.
1057 */
1058struct ifaddr *
39037602 1059ifa_ifwithaddr_scoped_locked(const struct sockaddr *addr, unsigned int ifscope)
c910b4d9
A
1060{
1061 struct ifaddr *result = NULL;
1062 struct ifnet *ifp;
1063
0a7de745
A
1064 if (ifscope == IFSCOPE_NONE) {
1065 return ifa_ifwithaddr_locked(addr);
1066 }
c910b4d9 1067
c910b4d9 1068 if (ifscope > (unsigned int)if_index) {
0a7de745 1069 return NULL;
c910b4d9
A
1070 }
1071
1072 ifp = ifindex2ifnet[ifscope];
1073 if (ifp != NULL) {
1074 struct ifaddr *ifa = NULL;
1075
1076 /*
1077 * This is suboptimal; there should be a better way
b0d623f7
A
1078 * to search for a given address of an interface
1079 * for any given address family.
c910b4d9
A
1080 */
1081 ifnet_lock_shared(ifp);
1082 for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
1083 ifa = ifa->ifa_link.tqe_next) {
6d2010ae
A
1084 IFA_LOCK_SPIN(ifa);
1085 if (ifa->ifa_addr->sa_family != addr->sa_family) {
1086 IFA_UNLOCK(ifa);
c910b4d9 1087 continue;
6d2010ae 1088 }
39037602 1089 if (ifa_equal(addr, ifa->ifa_addr)) {
c910b4d9 1090 result = ifa;
0a7de745 1091 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 1092 IFA_UNLOCK(ifa);
c910b4d9
A
1093 break;
1094 }
1095 if ((ifp->if_flags & IFF_BROADCAST) &&
1096 ifa->ifa_broadaddr != NULL &&
1097 /* IP6 doesn't have broadcast */
1098 ifa->ifa_broadaddr->sa_len != 0 &&
39037602 1099 ifa_equal(ifa->ifa_broadaddr, addr)) {
c910b4d9 1100 result = ifa;
0a7de745 1101 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 1102 IFA_UNLOCK(ifa);
c910b4d9
A
1103 break;
1104 }
6d2010ae 1105 IFA_UNLOCK(ifa);
c910b4d9 1106 }
c910b4d9
A
1107 ifnet_lock_done(ifp);
1108 }
39037602 1109
0a7de745 1110 return result;
743345f9
A
1111}
1112
39037602
A
1113struct ifaddr *
1114ifa_ifwithaddr_scoped(const struct sockaddr *addr, unsigned int ifscope)
1115{
1116 struct ifaddr *result = NULL;
1117
1118 ifnet_head_lock_shared();
743345f9 1119
39037602 1120 result = ifa_ifwithaddr_scoped_locked(addr, ifscope);
743345f9 1121
c910b4d9
A
1122 ifnet_head_done();
1123
0a7de745 1124 return result;
c910b4d9
A
1125}
1126
1127struct ifaddr *
1128ifa_ifwithnet(const struct sockaddr *addr)
1129{
0a7de745 1130 return ifa_ifwithnet_common(addr, IFSCOPE_NONE);
c910b4d9
A
1131}
1132
1133struct ifaddr *
1134ifa_ifwithnet_scoped(const struct sockaddr *addr, unsigned int ifscope)
1135{
0a7de745 1136 return ifa_ifwithnet_common(addr, ifscope);
c910b4d9
A
1137}
1138
1c79356b
A
1139/*
1140 * Find an interface on a specific network. If many, choice
1141 * is most specific found.
1142 */
c910b4d9
A
1143static struct ifaddr *
1144ifa_ifwithnet_common(const struct sockaddr *addr, unsigned int ifscope)
1c79356b 1145{
91447636
A
1146 struct ifnet *ifp;
1147 struct ifaddr *ifa = NULL;
6d2010ae 1148 struct ifaddr *ifa_maybe = NULL;
1c79356b 1149 u_int af = addr->sa_family;
cf7d32b8 1150 const char *addr_data = addr->sa_data, *cplim;
1c79356b 1151
0a7de745 1152 if (af != AF_INET && af != AF_INET6) {
c910b4d9 1153 ifscope = IFSCOPE_NONE;
0a7de745 1154 }
c910b4d9 1155
91447636 1156 ifnet_head_lock_shared();
1c79356b
A
1157 /*
1158 * AF_LINK addresses can be looked up directly by their index number,
1159 * so do that if we can.
1160 */
1161 if (af == AF_LINK) {
316670eb
A
1162 const struct sockaddr_dl *sdl =
1163 (const struct sockaddr_dl *)(uintptr_t)(size_t)addr;
6d2010ae 1164 if (sdl->sdl_index && sdl->sdl_index <= if_index) {
91447636 1165 ifa = ifnet_addrs[sdl->sdl_index - 1];
0a7de745 1166 if (ifa != NULL) {
6d2010ae 1167 IFA_ADDREF(ifa);
0a7de745 1168 }
6d2010ae 1169
91447636 1170 ifnet_head_done();
0a7de745 1171 return ifa;
91447636 1172 }
1c79356b
A
1173 }
1174
9bccf70c 1175 /*
1c79356b
A
1176 * Scan though each interface, looking for ones that have
1177 * addresses in this address family.
1178 */
91447636
A
1179 for (ifp = ifnet_head.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
1180 ifnet_lock_shared(ifp);
1c79356b 1181 for (ifa = ifp->if_addrhead.tqh_first; ifa;
0a7de745 1182 ifa = ifa->ifa_link.tqe_next) {
cf7d32b8 1183 const char *cp, *cp2, *cp3;
1c79356b 1184
6d2010ae
A
1185 IFA_LOCK(ifa);
1186 if (ifa->ifa_addr == NULL ||
1187 ifa->ifa_addr->sa_family != af) {
1188next:
1189 IFA_UNLOCK(ifa);
1190 continue;
1191 }
39236c6e
A
1192 /*
1193 * If we're looking up with a scope,
1194 * find using a matching interface.
1195 */
1196 if (ifscope != IFSCOPE_NONE &&
1197 ifp->if_index != ifscope) {
6d2010ae 1198 IFA_UNLOCK(ifa);
39236c6e
A
1199 continue;
1200 }
c910b4d9 1201
39236c6e
A
1202 /*
1203 * Scan all the bits in the ifa's address.
1204 * If a bit dissagrees with what we are
1205 * looking for, mask it with the netmask
1206 * to see if it really matters.
1207 * (A byte at a time)
1208 */
1209 if (ifa->ifa_netmask == 0) {
1210 IFA_UNLOCK(ifa);
1211 continue;
1212 }
1213 cp = addr_data;
1214 cp2 = ifa->ifa_addr->sa_data;
1215 cp3 = ifa->ifa_netmask->sa_data;
1216 cplim = ifa->ifa_netmask->sa_len +
1217 (char *)ifa->ifa_netmask;
0a7de745
A
1218 while (cp3 < cplim) {
1219 if ((*cp++ ^ *cp2++) & *cp3++) {
39236c6e 1220 goto next; /* next address! */
0a7de745
A
1221 }
1222 }
39236c6e
A
1223 /*
1224 * If the netmask of what we just found
1225 * is more specific than what we had before
1226 * (if we had one) then remember the new one
1227 * before continuing to search
1228 * for an even better one.
1229 */
1230 if (ifa_maybe == NULL ||
1231 rn_refines((caddr_t)ifa->ifa_netmask,
1232 (caddr_t)ifa_maybe->ifa_netmask)) {
0a7de745 1233 IFA_ADDREF_LOCKED(ifa); /* ifa_maybe */
39236c6e 1234 IFA_UNLOCK(ifa);
0a7de745 1235 if (ifa_maybe != NULL) {
39236c6e 1236 IFA_REMREF(ifa_maybe);
0a7de745 1237 }
39236c6e
A
1238 ifa_maybe = ifa;
1239 } else {
1240 IFA_UNLOCK(ifa);
1c79356b 1241 }
6d2010ae 1242 IFA_LOCK_ASSERT_NOTHELD(ifa);
1c79356b 1243 }
91447636 1244 ifnet_lock_done(ifp);
6d2010ae 1245
0a7de745 1246 if (ifa != NULL) {
91447636 1247 break;
0a7de745 1248 }
91447636
A
1249 }
1250 ifnet_head_done();
6d2010ae 1251
0a7de745 1252 if (ifa == NULL) {
91447636 1253 ifa = ifa_maybe;
0a7de745 1254 } else if (ifa_maybe != NULL) {
6d2010ae 1255 IFA_REMREF(ifa_maybe);
0a7de745 1256 }
6d2010ae 1257
0a7de745 1258 return ifa;
1c79356b
A
1259}
1260
1261/*
1262 * Find an interface address specific to an interface best matching
9d749ea3
A
1263 * a given address applying same source address selection rules
1264 * as done in the kernel for implicit source address binding
1265 */
1266struct ifaddr *
1267ifaof_ifpforaddr_select(const struct sockaddr *addr, struct ifnet *ifp)
1268{
1269 u_int af = addr->sa_family;
1270
0a7de745
A
1271 if (af == AF_INET6) {
1272 return in6_selectsrc_core_ifa(__DECONST(struct sockaddr_in6 *, addr), ifp, 0);
1273 }
9d749ea3 1274
0a7de745 1275 return ifaof_ifpforaddr(addr, ifp);
9d749ea3
A
1276}
1277
1278/*
1279 * Find an interface address specific to an interface best matching
1280 * a given address without regards to source address selection.
1281 *
1282 * This is appropriate for use-cases where we just want to update/init
1283 * some data structure like routing table entries.
1c79356b
A
1284 */
1285struct ifaddr *
6d2010ae 1286ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1c79356b 1287{
2d21ac55 1288 struct ifaddr *ifa = NULL;
91447636
A
1289 const char *cp, *cp2, *cp3;
1290 char *cplim;
2d21ac55
A
1291 struct ifaddr *ifa_maybe = NULL;
1292 struct ifaddr *better_ifa_maybe = NULL;
1c79356b
A
1293 u_int af = addr->sa_family;
1294
0a7de745
A
1295 if (af >= AF_MAX) {
1296 return NULL;
1297 }
6d2010ae 1298
91447636 1299 ifnet_lock_shared(ifp);
9bccf70c 1300 for (ifa = ifp->if_addrhead.tqh_first; ifa;
0a7de745 1301 ifa = ifa->ifa_link.tqe_next) {
6d2010ae
A
1302 IFA_LOCK(ifa);
1303 if (ifa->ifa_addr->sa_family != af) {
1304 IFA_UNLOCK(ifa);
1c79356b 1305 continue;
6d2010ae
A
1306 }
1307 if (ifa_maybe == NULL) {
0a7de745 1308 IFA_ADDREF_LOCKED(ifa); /* for ifa_maybe */
1c79356b 1309 ifa_maybe = ifa;
6d2010ae 1310 }
1c79356b 1311 if (ifa->ifa_netmask == 0) {
39037602
A
1312 if (ifa_equal(addr, ifa->ifa_addr) ||
1313 ifa_equal(addr, ifa->ifa_dstaddr)) {
0a7de745 1314 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae
A
1315 IFA_UNLOCK(ifa);
1316 break;
1317 }
1318 IFA_UNLOCK(ifa);
1c79356b
A
1319 continue;
1320 }
1321 if (ifp->if_flags & IFF_POINTOPOINT) {
39037602 1322 if (ifa_equal(addr, ifa->ifa_dstaddr)) {
0a7de745 1323 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 1324 IFA_UNLOCK(ifa);
91447636 1325 break;
6d2010ae 1326 }
1c79356b 1327 } else {
39037602 1328 if (ifa_equal(addr, ifa->ifa_addr)) {
2d21ac55 1329 /* exact match */
0a7de745 1330 IFA_ADDREF_LOCKED(ifa); /* for caller */
6d2010ae 1331 IFA_UNLOCK(ifa);
2d21ac55
A
1332 break;
1333 }
1c79356b
A
1334 cp = addr->sa_data;
1335 cp2 = ifa->ifa_addr->sa_data;
1336 cp3 = ifa->ifa_netmask->sa_data;
6d2010ae
A
1337 cplim = ifa->ifa_netmask->sa_len +
1338 (char *)ifa->ifa_netmask;
0a7de745
A
1339 for (; cp3 < cplim; cp3++) {
1340 if ((*cp++ ^ *cp2++) & *cp3) {
1c79356b 1341 break;
0a7de745
A
1342 }
1343 }
2d21ac55
A
1344 if (cp3 == cplim) {
1345 /* subnet match */
1346 if (better_ifa_maybe == NULL) {
6d2010ae
A
1347 /* for better_ifa_maybe */
1348 IFA_ADDREF_LOCKED(ifa);
2d21ac55
A
1349 better_ifa_maybe = ifa;
1350 }
1351 }
1c79356b 1352 }
6d2010ae 1353 IFA_UNLOCK(ifa);
1c79356b 1354 }
6d2010ae 1355
2d21ac55
A
1356 if (ifa == NULL) {
1357 if (better_ifa_maybe != NULL) {
1358 ifa = better_ifa_maybe;
6d2010ae 1359 better_ifa_maybe = NULL;
2d21ac55
A
1360 } else {
1361 ifa = ifa_maybe;
6d2010ae 1362 ifa_maybe = NULL;
2d21ac55
A
1363 }
1364 }
6d2010ae 1365
91447636 1366 ifnet_lock_done(ifp);
6d2010ae 1367
0a7de745 1368 if (better_ifa_maybe != NULL) {
6d2010ae 1369 IFA_REMREF(better_ifa_maybe);
0a7de745
A
1370 }
1371 if (ifa_maybe != NULL) {
6d2010ae 1372 IFA_REMREF(ifa_maybe);
0a7de745 1373 }
6d2010ae 1374
0a7de745 1375 return ifa;
1c79356b
A
1376}
1377
1378#include <net/route.h>
1379
1380/*
1381 * Default action when installing a route with a Link Level gateway.
1382 * Lookup an appropriate real ifa to point to.
1383 * This should be moved to /sys/net/link.c eventually.
1384 */
91447636 1385void
2d21ac55 1386link_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
1c79356b 1387{
91447636 1388 struct ifaddr *ifa;
1c79356b
A
1389 struct sockaddr *dst;
1390 struct ifnet *ifp;
6d2010ae 1391 void (*ifa_rtrequest)(int, struct rtentry *, struct sockaddr *);
1c79356b 1392
5ba3f43e 1393 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
1394 RT_LOCK_ASSERT_HELD(rt);
1395
1c79356b 1396 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
0a7de745 1397 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) {
1c79356b 1398 return;
0a7de745 1399 }
6d2010ae
A
1400
1401 /* Become a regular mutex, just in case */
1402 RT_CONVERT_LOCK(rt);
1403
1c79356b
A
1404 ifa = ifaof_ifpforaddr(dst, ifp);
1405 if (ifa) {
9bccf70c 1406 rtsetifa(rt, ifa);
6d2010ae
A
1407 IFA_LOCK_SPIN(ifa);
1408 ifa_rtrequest = ifa->ifa_rtrequest;
1409 IFA_UNLOCK(ifa);
0a7de745 1410 if (ifa_rtrequest != NULL && ifa_rtrequest != link_rtrequest) {
6d2010ae 1411 ifa_rtrequest(cmd, rt, sa);
0a7de745 1412 }
6d2010ae 1413 IFA_REMREF(ifa);
1c79356b
A
1414 }
1415}
1416
1417/*
91447636
A
1418 * if_updown will set the interface up or down. It will
1419 * prevent other up/down events from occurring until this
1420 * up/down event has completed.
1421 *
1422 * Caller must lock ifnet. This function will drop the
1423 * lock. This allows ifnet_set_flags to set the rest of
1424 * the flags after we change the up/down state without
1425 * dropping the interface lock between setting the
1426 * up/down state and updating the rest of the flags.
1c79356b 1427 */
91447636 1428__private_extern__ void
743345f9 1429if_updown( struct ifnet *ifp, int up)
1c79356b 1430{
f427ee49 1431 u_int32_t eflags;
91447636
A
1432 int i;
1433 struct ifaddr **ifa;
0a7de745 1434 struct timespec tv;
316670eb 1435 struct ifclassq *ifq = &ifp->if_snd;
91447636
A
1436
1437 /* Wait until no one else is changing the up/down state */
1438 while ((ifp->if_eflags & IFEF_UPDOWNCHANGE) != 0) {
1439 tv.tv_sec = 0;
1440 tv.tv_nsec = NSEC_PER_SEC / 10;
1441 ifnet_lock_done(ifp);
1442 msleep(&ifp->if_eflags, NULL, 0, "if_updown", &tv);
1443 ifnet_lock_exclusive(ifp);
1444 }
316670eb 1445
91447636
A
1446 /* Verify that the interface isn't already in the right state */
1447 if ((!up && (ifp->if_flags & IFF_UP) == 0) ||
0a7de745 1448 (up && (ifp->if_flags & IFF_UP) == IFF_UP)) {
91447636
A
1449 return;
1450 }
316670eb 1451
91447636 1452 /* Indicate that the up/down state is changing */
f427ee49
A
1453 eflags = if_set_eflags(ifp, IFEF_UPDOWNCHANGE);
1454 ASSERT((eflags & IFEF_UPDOWNCHANGE) == 0);
316670eb 1455
91447636
A
1456 /* Mark interface up or down */
1457 if (up) {
1458 ifp->if_flags |= IFF_UP;
39037602 1459 } else {
91447636
A
1460 ifp->if_flags &= ~IFF_UP;
1461 }
316670eb 1462
91447636 1463 ifnet_touch_lastchange(ifp);
9d749ea3 1464 ifnet_touch_lastupdown(ifp);
316670eb 1465
91447636
A
1466 /* Drop the lock to notify addresses and route */
1467 ifnet_lock_done(ifp);
743345f9
A
1468
1469 IFCQ_LOCK(ifq);
1470 if_qflush(ifp, 1);
1471
1472 /* Inform all transmit queues about the new link state */
1473 ifnet_update_sndq(ifq, up ? CLASSQ_EV_LINK_UP : CLASSQ_EV_LINK_DOWN);
1474 IFCQ_UNLOCK(ifq);
1475
91447636
A
1476 if (ifnet_get_address_list(ifp, &ifa) == 0) {
1477 for (i = 0; ifa[i] != 0; i++) {
1478 pfctlinput(up ? PRC_IFUP : PRC_IFDOWN, ifa[i]->ifa_addr);
1479 }
1480 ifnet_free_address_list(ifa);
1481 }
1c79356b 1482 rt_ifmsg(ifp);
316670eb 1483
316670eb
A
1484 /* Aquire the lock to clear the changing flag */
1485 ifnet_lock_exclusive(ifp);
f427ee49 1486 if_clear_eflags(ifp, IFEF_UPDOWNCHANGE);
91447636 1487 wakeup(&ifp->if_eflags);
1c79356b
A
1488}
1489
1490/*
1491 * Mark an interface down and notify protocols of
1492 * the transition.
1c79356b
A
1493 */
1494void
91447636
A
1495if_down(
1496 struct ifnet *ifp)
1c79356b 1497{
91447636
A
1498 ifnet_lock_exclusive(ifp);
1499 if_updown(ifp, 0);
1500 ifnet_lock_done(ifp);
1c79356b
A
1501}
1502
1503/*
1504 * Mark an interface up and notify protocols of
1505 * the transition.
1c79356b
A
1506 */
1507void
91447636
A
1508if_up(
1509 struct ifnet *ifp)
1c79356b 1510{
91447636
A
1511 ifnet_lock_exclusive(ifp);
1512 if_updown(ifp, 1);
1513 ifnet_lock_done(ifp);
1c79356b
A
1514}
1515
1516/*
1517 * Flush an interface queue.
1518 */
316670eb
A
1519void
1520if_qflush(struct ifnet *ifp, int ifq_locked)
1c79356b 1521{
316670eb
A
1522 struct ifclassq *ifq = &ifp->if_snd;
1523
0a7de745 1524 if (!ifq_locked) {
316670eb 1525 IFCQ_LOCK(ifq);
0a7de745 1526 }
316670eb 1527
0a7de745 1528 if (IFCQ_IS_ENABLED(ifq)) {
f427ee49 1529 fq_if_request_classq(ifq, CLASSQRQ_PURGE, NULL);
0a7de745 1530 }
1c79356b 1531
316670eb
A
1532 VERIFY(IFCQ_IS_EMPTY(ifq));
1533
0a7de745 1534 if (!ifq_locked) {
316670eb 1535 IFCQ_UNLOCK(ifq);
0a7de745 1536 }
316670eb
A
1537}
1538
1539void
1540if_qflush_sc(struct ifnet *ifp, mbuf_svc_class_t sc, u_int32_t flow,
1541 u_int32_t *packets, u_int32_t *bytes, int ifq_locked)
1542{
1543 struct ifclassq *ifq = &ifp->if_snd;
1544 u_int32_t cnt = 0, len = 0;
316670eb
A
1545
1546 VERIFY(sc == MBUF_SC_UNSPEC || MBUF_VALID_SC(sc));
1547 VERIFY(flow != 0);
1548
0a7de745 1549 if (!ifq_locked) {
316670eb 1550 IFCQ_LOCK(ifq);
0a7de745 1551 }
316670eb 1552
0a7de745 1553 if (IFCQ_IS_ENABLED(ifq)) {
f427ee49
A
1554 cqrq_purge_sc_t req = { sc, flow, 0, 0 };
1555
1556 fq_if_request_classq(ifq, CLASSQRQ_PURGE_SC, &req);
1557 cnt = req.packets;
1558 len = req.bytes;
0a7de745 1559 }
316670eb 1560
0a7de745 1561 if (!ifq_locked) {
316670eb 1562 IFCQ_UNLOCK(ifq);
0a7de745 1563 }
316670eb 1564
0a7de745 1565 if (packets != NULL) {
f427ee49 1566 *packets = cnt;
0a7de745
A
1567 }
1568 if (bytes != NULL) {
f427ee49 1569 *bytes = len;
0a7de745 1570 }
1c79356b
A
1571}
1572
1c79356b 1573/*
39037602
A
1574 * Extracts interface unit number and name from string, returns -1 upon failure.
1575 * Upon success, returns extracted unit number, and interface name in dst.
1c79356b 1576 */
39037602
A
1577int
1578ifunit_extract(const char *src, char *dst, size_t dstlen, int *unit)
1c79356b 1579{
9bccf70c 1580 const char *cp;
39037602 1581 size_t len, m;
9bccf70c 1582 char c;
39037602 1583 int u;
1c79356b 1584
0a7de745
A
1585 if (src == NULL || dst == NULL || dstlen == 0 || unit == NULL) {
1586 return -1;
1587 }
39037602
A
1588
1589 len = strlen(src);
0a7de745
A
1590 if (len < 2 || len > dstlen) {
1591 return -1;
1592 }
39037602 1593 cp = src + len - 1;
9bccf70c 1594 c = *cp;
0a7de745
A
1595 if (c < '0' || c > '9') {
1596 return -1; /* trailing garbage */
1597 }
39037602 1598 u = 0;
9bccf70c
A
1599 m = 1;
1600 do {
0a7de745
A
1601 if (cp == src) {
1602 return -1; /* no interface name */
1603 }
39037602 1604 u += (c - '0') * m;
0a7de745
A
1605 if (u > 1000000) {
1606 return -1; /* number is unreasonable */
1607 }
9bccf70c
A
1608 m *= 10;
1609 c = *--cp;
1610 } while (c >= '0' && c <= '9');
39037602
A
1611 len = cp - src + 1;
1612 bcopy(src, dst, len);
1613 dst[len] = '\0';
1614 *unit = u;
1615
0a7de745 1616 return 0;
39037602
A
1617}
1618
1619/*
1620 * Map interface name to
1621 * interface structure pointer.
1622 */
1623static struct ifnet *
1624ifunit_common(const char *name, boolean_t hold)
1625{
1626 char namebuf[IFNAMSIZ + 1];
1627 struct ifnet *ifp;
1628 int unit;
1629
0a7de745
A
1630 if (ifunit_extract(name, namebuf, sizeof(namebuf), &unit) < 0) {
1631 return NULL;
1632 }
39037602
A
1633
1634 /* for safety, since we use strcmp() below */
0a7de745 1635 namebuf[sizeof(namebuf) - 1] = '\0';
39037602 1636
1c79356b
A
1637 /*
1638 * Now search all the interfaces for this name/number
1639 */
91447636
A
1640 ifnet_head_lock_shared();
1641 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
39037602
A
1642 /*
1643 * Use strcmp() rather than strncmp() here,
1644 * since we want to match the entire string.
1645 */
0a7de745 1646 if (strcmp(ifp->if_name, namebuf)) {
1c79356b 1647 continue;
0a7de745
A
1648 }
1649 if (unit == ifp->if_unit) {
1c79356b 1650 break;
0a7de745 1651 }
1c79356b 1652 }
39037602
A
1653
1654 /* if called from ifunit_ref() and ifnet is not attached, bail */
0a7de745 1655 if (hold && ifp != NULL && !ifnet_is_attached(ifp, 1)) {
39037602 1656 ifp = NULL;
0a7de745 1657 }
39037602 1658
91447636 1659 ifnet_head_done();
0a7de745 1660 return ifp;
1c79356b
A
1661}
1662
39037602
A
1663struct ifnet *
1664ifunit(const char *name)
1665{
0a7de745 1666 return ifunit_common(name, FALSE);
39037602
A
1667}
1668
1669/*
1670 * Similar to ifunit(), except that we hold an I/O reference count on an
1671 * attached interface, which must later be released via ifnet_decr_iorefcnt().
1672 * Will return NULL unless interface exists and is fully attached.
1673 */
1674struct ifnet *
1675ifunit_ref(const char *name)
1676{
0a7de745 1677 return ifunit_common(name, TRUE);
39037602 1678}
1c79356b
A
1679
1680/*
1681 * Map interface name in a sockaddr_dl to
1682 * interface structure pointer.
1683 */
1684struct ifnet *
2d21ac55 1685if_withname(struct sockaddr *sa)
1c79356b 1686{
0a7de745 1687 char ifname[IFNAMSIZ + 1];
316670eb 1688 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(void *)sa;
1c79356b 1689
39037602 1690 if ((sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
0a7de745
A
1691 (sdl->sdl_nlen > IFNAMSIZ)) {
1692 return NULL;
1693 }
1c79356b
A
1694
1695 /*
1696 * ifunit wants a null-terminated name. It may not be null-terminated
1697 * in the sockaddr. We don't want to change the caller's sockaddr,
1698 * and there might not be room to put the trailing null anyway, so we
1699 * make a local copy that we know we can null terminate safely.
1700 */
1701
1702 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1703 ifname[sdl->sdl_nlen] = '\0';
0a7de745 1704 return ifunit(ifname);
1c79356b
A
1705}
1706
39236c6e
A
1707static __attribute__((noinline)) int
1708ifioctl_ifconf(u_long cmd, caddr_t data)
1c79356b 1709{
1c79356b 1710 int error = 0;
1c79356b
A
1711
1712 switch (cmd) {
0a7de745
A
1713 case OSIOCGIFCONF32: /* struct ifconf32 */
1714 case SIOCGIFCONF32: { /* struct ifconf32 */
316670eb 1715 struct ifconf32 ifc;
0a7de745 1716 bcopy(data, &ifc, sizeof(ifc));
316670eb
A
1717 error = ifconf(cmd, CAST_USER_ADDR_T(ifc.ifc_req),
1718 &ifc.ifc_len);
0a7de745 1719 bcopy(&ifc, data, sizeof(ifc));
39236c6e 1720 break;
b0d623f7 1721 }
316670eb 1722
0a7de745
A
1723 case SIOCGIFCONF64: /* struct ifconf64 */
1724 case OSIOCGIFCONF64: { /* struct ifconf64 */
316670eb 1725 struct ifconf64 ifc;
0a7de745 1726 bcopy(data, &ifc, sizeof(ifc));
316670eb 1727 error = ifconf(cmd, ifc.ifc_req, &ifc.ifc_len);
0a7de745 1728 bcopy(&ifc, data, sizeof(ifc));
39236c6e 1729 break;
b0d623f7 1730 }
316670eb 1731
39236c6e
A
1732 default:
1733 VERIFY(0);
1734 /* NOTREACHED */
1735 }
1736
0a7de745 1737 return error;
39236c6e
A
1738}
1739
1740static __attribute__((noinline)) int
1741ifioctl_ifclone(u_long cmd, caddr_t data)
1742{
1743 int error = 0;
1744
1745 switch (cmd) {
0a7de745 1746 case SIOCIFGCLONERS32: { /* struct if_clonereq32 */
316670eb 1747 struct if_clonereq32 ifcr;
0a7de745 1748 bcopy(data, &ifcr, sizeof(ifcr));
316670eb
A
1749 error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1750 CAST_USER_ADDR_T(ifcr.ifcru_buffer));
0a7de745 1751 bcopy(&ifcr, data, sizeof(ifcr));
39236c6e 1752 break;
316670eb
A
1753 }
1754
0a7de745 1755 case SIOCIFGCLONERS64: { /* struct if_clonereq64 */
316670eb 1756 struct if_clonereq64 ifcr;
0a7de745 1757 bcopy(data, &ifcr, sizeof(ifcr));
316670eb
A
1758 error = if_clone_list(ifcr.ifcr_count, &ifcr.ifcr_total,
1759 ifcr.ifcru_buffer);
0a7de745 1760 bcopy(&ifcr, data, sizeof(ifcr));
39236c6e
A
1761 break;
1762 }
1763
1764 default:
1765 VERIFY(0);
1766 /* NOTREACHED */
1767 }
1768
0a7de745 1769 return error;
39236c6e
A
1770}
1771
1772static __attribute__((noinline)) int
1773ifioctl_ifdesc(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1774{
1775 struct if_descreq *ifdr = (struct if_descreq *)(void *)data;
1776 u_int32_t ifdr_len;
1777 int error = 0;
1778
1779 VERIFY(ifp != NULL);
1780
1781 switch (cmd) {
0a7de745
A
1782 case SIOCSIFDESC: { /* struct if_descreq */
1783 if ((error = proc_suser(p)) != 0) {
39037602 1784 break;
0a7de745 1785 }
39236c6e
A
1786
1787 ifnet_lock_exclusive(ifp);
0a7de745
A
1788 bcopy(&ifdr->ifdr_len, &ifdr_len, sizeof(ifdr_len));
1789 if (ifdr_len > sizeof(ifdr->ifdr_desc) ||
39236c6e
A
1790 ifdr_len > ifp->if_desc.ifd_maxlen) {
1791 error = EINVAL;
1792 ifnet_lock_done(ifp);
1793 break;
1794 }
1795
1796 bzero(ifp->if_desc.ifd_desc, ifp->if_desc.ifd_maxlen);
1797 if ((ifp->if_desc.ifd_len = ifdr_len) > 0) {
1798 bcopy(ifdr->ifdr_desc, ifp->if_desc.ifd_desc,
1799 MIN(ifdr_len, ifp->if_desc.ifd_maxlen));
1800 }
1801 ifnet_lock_done(ifp);
1802 break;
1803 }
1804
0a7de745 1805 case SIOCGIFDESC: { /* struct if_descreq */
39236c6e 1806 ifnet_lock_shared(ifp);
0a7de745
A
1807 ifdr_len = MIN(ifp->if_desc.ifd_len, sizeof(ifdr->ifdr_desc));
1808 bcopy(&ifdr_len, &ifdr->ifdr_len, sizeof(ifdr_len));
1809 bzero(&ifdr->ifdr_desc, sizeof(ifdr->ifdr_desc));
39236c6e
A
1810 if (ifdr_len > 0) {
1811 bcopy(ifp->if_desc.ifd_desc, ifdr->ifdr_desc, ifdr_len);
1812 }
1813 ifnet_lock_done(ifp);
1814 break;
1815 }
1816
1817 default:
1818 VERIFY(0);
1819 /* NOTREACHED */
1820 }
1821
0a7de745 1822 return error;
39236c6e
A
1823}
1824
1825static __attribute__((noinline)) int
1826ifioctl_linkparams(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1827{
1828 struct if_linkparamsreq *iflpr =
1829 (struct if_linkparamsreq *)(void *)data;
1830 struct ifclassq *ifq;
1831 int error = 0;
1832
1833 VERIFY(ifp != NULL);
1834 ifq = &ifp->if_snd;
1835
1836 switch (cmd) {
0a7de745 1837 case SIOCSIFLINKPARAMS: { /* struct if_linkparamsreq */
cb323159 1838 struct tb_profile tb = { .rate = 0, .percent = 0, .depth = 0 };
39236c6e 1839
0a7de745 1840 if ((error = proc_suser(p)) != 0) {
a39ff7e2 1841 break;
0a7de745 1842 }
a39ff7e2 1843
39236c6e 1844
cb323159
A
1845 char netem_name[32];
1846 (void) snprintf(netem_name, sizeof(netem_name),
1847 "if_output_netem_%s", if_name(ifp));
1848 error = netem_config(&ifp->if_output_netem, netem_name,
1849 &iflpr->iflpr_output_netem, (void *)ifp,
1850 ifnet_enqueue_netem, NETEM_MAX_BATCH_SIZE);
1851 if (error != 0) {
1852 break;
1853 }
1854
39236c6e
A
1855 IFCQ_LOCK(ifq);
1856 if (!IFCQ_IS_READY(ifq)) {
1857 error = ENXIO;
1858 IFCQ_UNLOCK(ifq);
1859 break;
1860 }
1861 bcopy(&iflpr->iflpr_output_tbr_rate, &tb.rate,
0a7de745 1862 sizeof(tb.rate));
39236c6e 1863 bcopy(&iflpr->iflpr_output_tbr_percent, &tb.percent,
0a7de745 1864 sizeof(tb.percent));
39236c6e
A
1865 error = ifclassq_tbr_set(ifq, &tb, TRUE);
1866 IFCQ_UNLOCK(ifq);
1867 break;
1868 }
1869
0a7de745 1870 case SIOCGIFLINKPARAMS: { /* struct if_linkparamsreq */
39236c6e
A
1871 u_int32_t sched_type = PKTSCHEDT_NONE, flags = 0;
1872 u_int64_t tbr_bw = 0, tbr_pct = 0;
1873
1874 IFCQ_LOCK(ifq);
5ba3f43e 1875
0a7de745 1876 if (IFCQ_IS_ENABLED(ifq)) {
5ba3f43e 1877 sched_type = ifq->ifcq_type;
0a7de745 1878 }
5ba3f43e 1879
39236c6e 1880 bcopy(&sched_type, &iflpr->iflpr_output_sched,
0a7de745 1881 sizeof(iflpr->iflpr_output_sched));
39236c6e
A
1882
1883 if (IFCQ_TBR_IS_ENABLED(ifq)) {
1884 tbr_bw = ifq->ifcq_tbr.tbr_rate_raw;
1885 tbr_pct = ifq->ifcq_tbr.tbr_percent;
1886 }
1887 bcopy(&tbr_bw, &iflpr->iflpr_output_tbr_rate,
0a7de745 1888 sizeof(iflpr->iflpr_output_tbr_rate));
39236c6e 1889 bcopy(&tbr_pct, &iflpr->iflpr_output_tbr_percent,
0a7de745 1890 sizeof(iflpr->iflpr_output_tbr_percent));
39236c6e
A
1891 IFCQ_UNLOCK(ifq);
1892
1893 if (ifp->if_output_sched_model ==
0a7de745 1894 IFNET_SCHED_MODEL_DRIVER_MANAGED) {
39236c6e 1895 flags |= IFLPRF_DRVMANAGED;
0a7de745
A
1896 }
1897 bcopy(&flags, &iflpr->iflpr_flags, sizeof(iflpr->iflpr_flags));
39236c6e 1898 bcopy(&ifp->if_output_bw, &iflpr->iflpr_output_bw,
0a7de745 1899 sizeof(iflpr->iflpr_output_bw));
39236c6e 1900 bcopy(&ifp->if_input_bw, &iflpr->iflpr_input_bw,
0a7de745 1901 sizeof(iflpr->iflpr_input_bw));
39236c6e 1902 bcopy(&ifp->if_output_lt, &iflpr->iflpr_output_lt,
0a7de745 1903 sizeof(iflpr->iflpr_output_lt));
39236c6e 1904 bcopy(&ifp->if_input_lt, &iflpr->iflpr_input_lt,
0a7de745 1905 sizeof(iflpr->iflpr_input_lt));
cb323159
A
1906
1907 if (ifp->if_output_netem != NULL) {
1908 netem_get_params(ifp->if_output_netem,
1909 &iflpr->iflpr_output_netem);
1910 }
1911
39236c6e
A
1912 break;
1913 }
1914
1915 default:
1916 VERIFY(0);
1917 /* NOTREACHED */
1c79356b 1918 }
39236c6e 1919
0a7de745 1920 return error;
39236c6e
A
1921}
1922
1923static __attribute__((noinline)) int
1924ifioctl_qstats(struct ifnet *ifp, u_long cmd, caddr_t data)
1925{
1926 struct if_qstatsreq *ifqr = (struct if_qstatsreq *)(void *)data;
1927 u_int32_t ifqr_len, ifqr_slot;
1928 int error = 0;
1929
1930 VERIFY(ifp != NULL);
1931
1932 switch (cmd) {
0a7de745
A
1933 case SIOCGIFQUEUESTATS: { /* struct if_qstatsreq */
1934 bcopy(&ifqr->ifqr_slot, &ifqr_slot, sizeof(ifqr_slot));
1935 bcopy(&ifqr->ifqr_len, &ifqr_len, sizeof(ifqr_len));
39236c6e
A
1936 error = ifclassq_getqstats(&ifp->if_snd, ifqr_slot,
1937 ifqr->ifqr_buf, &ifqr_len);
0a7de745 1938 if (error != 0) {
39236c6e 1939 ifqr_len = 0;
0a7de745
A
1940 }
1941 bcopy(&ifqr_len, &ifqr->ifqr_len, sizeof(ifqr_len));
39236c6e
A
1942 break;
1943 }
1944
1945 default:
1946 VERIFY(0);
1947 /* NOTREACHED */
1948 }
1949
0a7de745 1950 return error;
39236c6e
A
1951}
1952
1953static __attribute__((noinline)) int
1954ifioctl_throttle(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1955{
1956 struct if_throttlereq *ifthr = (struct if_throttlereq *)(void *)data;
1957 u_int32_t ifthr_level;
1958 int error = 0;
1959
1960 VERIFY(ifp != NULL);
1961
1962 switch (cmd) {
0a7de745 1963 case SIOCSIFTHROTTLE: { /* struct if_throttlereq */
39236c6e
A
1964 /*
1965 * XXX: Use priv_check_cred() instead of root check?
1966 */
0a7de745 1967 if ((error = proc_suser(p)) != 0) {
39037602 1968 break;
0a7de745 1969 }
39236c6e 1970
0a7de745 1971 bcopy(&ifthr->ifthr_level, &ifthr_level, sizeof(ifthr_level));
39236c6e 1972 error = ifnet_set_throttle(ifp, ifthr_level);
0a7de745 1973 if (error == EALREADY) {
39236c6e 1974 error = 0;
0a7de745 1975 }
39236c6e
A
1976 break;
1977 }
1978
0a7de745 1979 case SIOCGIFTHROTTLE: { /* struct if_throttlereq */
39236c6e
A
1980 if ((error = ifnet_get_throttle(ifp, &ifthr_level)) == 0) {
1981 bcopy(&ifthr_level, &ifthr->ifthr_level,
0a7de745 1982 sizeof(ifthr_level));
39236c6e
A
1983 }
1984 break;
1985 }
1986
1987 default:
1988 VERIFY(0);
1989 /* NOTREACHED */
1990 }
1991
0a7de745 1992 return error;
39236c6e
A
1993}
1994
3e170ce0
A
1995static int
1996ifioctl_getnetagents(struct ifnet *ifp, u_int32_t *count, user_addr_t uuid_p)
1997{
1998 int error = 0;
39037602 1999 u_int32_t index = 0;
3e170ce0
A
2000 u_int32_t valid_netagent_count = 0;
2001 *count = 0;
39037602
A
2002
2003 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_SHARED);
2004
2005 if (ifp->if_agentids != NULL) {
2006 for (index = 0; index < ifp->if_agentcount; index++) {
2007 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2008 if (!uuid_is_null(*netagent_uuid)) {
2009 if (uuid_p != USER_ADDR_NULL) {
2010 error = copyout(netagent_uuid,
0a7de745
A
2011 uuid_p + sizeof(uuid_t) * valid_netagent_count,
2012 sizeof(uuid_t));
39037602 2013 if (error != 0) {
0a7de745 2014 return error;
39037602 2015 }
3e170ce0 2016 }
39037602 2017 valid_netagent_count++;
3e170ce0 2018 }
3e170ce0
A
2019 }
2020 }
2021 *count = valid_netagent_count;
2022
0a7de745 2023 return 0;
3e170ce0
A
2024}
2025
0a7de745
A
2026#define IF_MAXAGENTS 64
2027#define IF_AGENT_INCREMENT 8
cb323159 2028int
5ba3f43e
A
2029if_add_netagent_locked(struct ifnet *ifp, uuid_t new_agent_uuid)
2030{
cb323159
A
2031 VERIFY(ifp != NULL);
2032
5ba3f43e
A
2033 uuid_t *first_empty_slot = NULL;
2034 u_int32_t index = 0;
2035 bool already_added = FALSE;
2036
2037 if (ifp->if_agentids != NULL) {
2038 for (index = 0; index < ifp->if_agentcount; index++) {
2039 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2040 if (uuid_compare(*netagent_uuid, new_agent_uuid) == 0) {
2041 /* Already present, ignore */
2042 already_added = TRUE;
2043 break;
2044 }
2045 if (first_empty_slot == NULL &&
0a7de745 2046 uuid_is_null(*netagent_uuid)) {
5ba3f43e
A
2047 first_empty_slot = netagent_uuid;
2048 }
2049 }
2050 }
2051 if (already_added) {
2052 /* Already added agent, don't return an error */
0a7de745 2053 return 0;
5ba3f43e
A
2054 }
2055 if (first_empty_slot == NULL) {
2056 if (ifp->if_agentcount >= IF_MAXAGENTS) {
2057 /* No room for another netagent UUID, bail */
0a7de745 2058 return ENOMEM;
5ba3f43e
A
2059 } else {
2060 /* Calculate new array size */
2061 u_int32_t new_agent_count =
0a7de745 2062 MIN(ifp->if_agentcount + IF_AGENT_INCREMENT,
5ba3f43e
A
2063 IF_MAXAGENTS);
2064
2065 /* Reallocate array */
2066 uuid_t *new_agent_array = _REALLOC(ifp->if_agentids,
2067 sizeof(uuid_t) * new_agent_count, M_NETAGENT,
2068 M_WAITOK | M_ZERO);
2069 if (new_agent_array == NULL) {
0a7de745 2070 return ENOMEM;
5ba3f43e
A
2071 }
2072
2073 /* Save new array */
2074 ifp->if_agentids = new_agent_array;
2075
2076 /* Set first empty slot */
2077 first_empty_slot =
2078 &(ifp->if_agentids[ifp->if_agentcount]);
2079
2080 /* Save new array length */
2081 ifp->if_agentcount = new_agent_count;
2082 }
2083 }
2084 uuid_copy(*first_empty_slot, new_agent_uuid);
2085 netagent_post_updated_interfaces(new_agent_uuid);
0a7de745 2086 return 0;
5ba3f43e
A
2087}
2088
2089int
2090if_add_netagent(struct ifnet *ifp, uuid_t new_agent_uuid)
2091{
2092 VERIFY(ifp != NULL);
2093
2094 ifnet_lock_exclusive(ifp);
2095
2096 int error = if_add_netagent_locked(ifp, new_agent_uuid);
2097
2098 ifnet_lock_done(ifp);
2099
0a7de745 2100 return error;
5ba3f43e
A
2101}
2102
2103static int
2104if_delete_netagent_locked(struct ifnet *ifp, uuid_t remove_agent_uuid)
2105{
2106 u_int32_t index = 0;
2107 bool removed_agent_id = FALSE;
2108
2109 if (ifp->if_agentids != NULL) {
2110 for (index = 0; index < ifp->if_agentcount; index++) {
2111 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
2112 if (uuid_compare(*netagent_uuid,
2113 remove_agent_uuid) == 0) {
2114 uuid_clear(*netagent_uuid);
2115 removed_agent_id = TRUE;
2116 break;
2117 }
2118 }
2119 }
0a7de745 2120 if (removed_agent_id) {
5ba3f43e 2121 netagent_post_updated_interfaces(remove_agent_uuid);
0a7de745 2122 }
5ba3f43e 2123
0a7de745 2124 return 0;
5ba3f43e
A
2125}
2126
2127int
2128if_delete_netagent(struct ifnet *ifp, uuid_t remove_agent_uuid)
2129{
2130 VERIFY(ifp != NULL);
2131
2132 ifnet_lock_exclusive(ifp);
2133
2134 int error = if_delete_netagent_locked(ifp, remove_agent_uuid);
2135
2136 ifnet_lock_done(ifp);
2137
0a7de745 2138 return error;
5ba3f43e
A
2139}
2140
a39ff7e2
A
2141boolean_t
2142if_check_netagent(struct ifnet *ifp, uuid_t find_agent_uuid)
2143{
2144 boolean_t found = FALSE;
2145
0a7de745 2146 if (!ifp || uuid_is_null(find_agent_uuid)) {
a39ff7e2 2147 return FALSE;
0a7de745 2148 }
a39ff7e2
A
2149
2150 ifnet_lock_shared(ifp);
2151
2152 if (ifp->if_agentids != NULL) {
2153 for (uint32_t index = 0; index < ifp->if_agentcount; index++) {
2154 if (uuid_compare(ifp->if_agentids[index], find_agent_uuid) == 0) {
2155 found = TRUE;
2156 break;
2157 }
2158 }
2159 }
2160
2161 ifnet_lock_done(ifp);
2162
2163 return found;
2164}
2165
3e170ce0
A
2166static __attribute__((noinline)) int
2167ifioctl_netagent(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
2168{
2169 struct if_agentidreq *ifar = (struct if_agentidreq *)(void *)data;
2170 union {
2171 struct if_agentidsreq32 s32;
2172 struct if_agentidsreq64 s64;
2173 } u;
2174 int error = 0;
3e170ce0
A
2175
2176 VERIFY(ifp != NULL);
2177
39037602
A
2178 /* Get an io ref count if the interface is attached */
2179 if (!ifnet_is_attached(ifp, 1)) {
0a7de745 2180 return EOPNOTSUPP;
39037602
A
2181 }
2182
2183 if (cmd == SIOCAIFAGENTID ||
0a7de745 2184 cmd == SIOCDIFAGENTID) {
39037602
A
2185 ifnet_lock_exclusive(ifp);
2186 } else {
2187 ifnet_lock_shared(ifp);
2188 }
2189
3e170ce0 2190 switch (cmd) {
0a7de745
A
2191 case SIOCAIFAGENTID: { /* struct if_agentidreq */
2192 // TODO: Use priv_check_cred() instead of root check
2193 if ((error = proc_suser(p)) != 0) {
3e170ce0
A
2194 break;
2195 }
0a7de745
A
2196 error = if_add_netagent_locked(ifp, ifar->ifar_uuid);
2197 break;
2198 }
2199 case SIOCDIFAGENTID: { /* struct if_agentidreq */
2200 // TODO: Use priv_check_cred() instead of root check
2201 if ((error = proc_suser(p)) != 0) {
3e170ce0
A
2202 break;
2203 }
0a7de745
A
2204 error = if_delete_netagent_locked(ifp, ifar->ifar_uuid);
2205 break;
2206 }
2207 case SIOCGIFAGENTIDS32: { /* struct if_agentidsreq32 */
2208 bcopy(data, &u.s32, sizeof(u.s32));
2209 error = ifioctl_getnetagents(ifp, &u.s32.ifar_count,
2210 u.s32.ifar_uuids);
2211 if (error == 0) {
2212 bcopy(&u.s32, data, sizeof(u.s32));
3e170ce0 2213 }
0a7de745
A
2214 break;
2215 }
2216 case SIOCGIFAGENTIDS64: { /* struct if_agentidsreq64 */
2217 bcopy(data, &u.s64, sizeof(u.s64));
2218 error = ifioctl_getnetagents(ifp, &u.s64.ifar_count,
2219 u.s64.ifar_uuids);
2220 if (error == 0) {
2221 bcopy(&u.s64, data, sizeof(u.s64));
3e170ce0 2222 }
0a7de745
A
2223 break;
2224 }
2225 default:
2226 VERIFY(0);
2227 /* NOTREACHED */
3e170ce0
A
2228 }
2229
39037602
A
2230 ifnet_lock_done(ifp);
2231 ifnet_decr_iorefcnt(ifp);
2232
0a7de745 2233 return error;
3e170ce0
A
2234}
2235
2236void
2237ifnet_clear_netagent(uuid_t netagent_uuid)
2238{
2239 struct ifnet *ifp = NULL;
39037602 2240 u_int32_t index = 0;
3e170ce0
A
2241
2242 ifnet_head_lock_shared();
2243
2244 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
39037602
A
2245 ifnet_lock_shared(ifp);
2246 if (ifp->if_agentids != NULL) {
2247 for (index = 0; index < ifp->if_agentcount; index++) {
2248 uuid_t *ifp_netagent_uuid = &(ifp->if_agentids[index]);
2249 if (uuid_compare(*ifp_netagent_uuid, netagent_uuid) == 0) {
2250 uuid_clear(*ifp_netagent_uuid);
2251 }
3e170ce0
A
2252 }
2253 }
39037602 2254 ifnet_lock_done(ifp);
3e170ce0
A
2255 }
2256
2257 ifnet_head_done();
2258}
2259
39037602
A
2260void
2261ifnet_increment_generation(ifnet_t interface)
2262{
2263 OSIncrementAtomic(&interface->if_generation);
2264}
2265
2266u_int32_t
2267ifnet_get_generation(ifnet_t interface)
2268{
0a7de745 2269 return interface->if_generation;
39037602
A
2270}
2271
2272void
2273ifnet_remove_from_ordered_list(struct ifnet *ifp)
2274{
2275 ifnet_head_assert_exclusive();
2276
2277 // Remove from list
2278 TAILQ_REMOVE(&ifnet_ordered_head, ifp, if_ordered_link);
2279 ifp->if_ordered_link.tqe_next = NULL;
2280 ifp->if_ordered_link.tqe_prev = NULL;
2281
2282 // Update ordered count
2283 VERIFY(if_ordered_count > 0);
2284 if_ordered_count--;
2285}
2286
2287static int
2288ifnet_reset_order(u_int32_t *ordered_indices, u_int32_t count)
2289{
2290 struct ifnet *ifp = NULL;
2291 int error = 0;
2292
2293 ifnet_head_lock_exclusive();
cc8bc92a
A
2294 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2295 if (ordered_indices[order_index] == IFSCOPE_NONE ||
2296 ordered_indices[order_index] > (uint32_t)if_index) {
2297 error = EINVAL;
2298 ifnet_head_done();
0a7de745 2299 return error;
cc8bc92a
A
2300 }
2301 }
39037602
A
2302 // Flush current ordered list
2303 for (ifp = TAILQ_FIRST(&ifnet_ordered_head); ifp != NULL;
2304 ifp = TAILQ_FIRST(&ifnet_ordered_head)) {
2305 ifnet_lock_exclusive(ifp);
2306 ifnet_remove_from_ordered_list(ifp);
2307 ifnet_lock_done(ifp);
2308 }
2309
2310 VERIFY(if_ordered_count == 0);
2311
2312 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2313 u_int32_t interface_index = ordered_indices[order_index];
39037602
A
2314 ifp = ifindex2ifnet[interface_index];
2315 if (ifp == NULL) {
2316 continue;
2317 }
2318 ifnet_lock_exclusive(ifp);
2319 TAILQ_INSERT_TAIL(&ifnet_ordered_head, ifp, if_ordered_link);
2320 ifnet_lock_done(ifp);
2321 if_ordered_count++;
2322 }
2323
2324 ifnet_head_done();
2325
2326 necp_update_all_clients();
2327
0a7de745 2328 return error;
39037602
A
2329}
2330
2331int
2332if_set_qosmarking_mode(struct ifnet *ifp, u_int32_t mode)
2333{
2334 int error = 0;
2335 u_int32_t old_mode = ifp->if_qosmarking_mode;
2336
2337 switch (mode) {
0a7de745
A
2338 case IFRTYPE_QOSMARKING_MODE_NONE:
2339 ifp->if_qosmarking_mode = IFRTYPE_QOSMARKING_MODE_NONE;
0a7de745
A
2340 break;
2341 case IFRTYPE_QOSMARKING_FASTLANE:
cb323159
A
2342 case IFRTYPE_QOSMARKING_RFC4594:
2343 ifp->if_qosmarking_mode = mode;
0a7de745
A
2344 break;
2345 default:
2346 error = EINVAL;
2347 break;
39037602
A
2348 }
2349 if (error == 0 && old_mode != ifp->if_qosmarking_mode) {
2350 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_QOS_MODE_CHANGED,
cb323159 2351 NULL, 0);
39037602 2352 }
0a7de745 2353 return error;
39037602
A
2354}
2355
2356static __attribute__((noinline)) int
2357ifioctl_iforder(u_long cmd, caddr_t data)
2358{
2359 int error = 0;
2360 u_int32_t *ordered_indices = NULL;
39037602 2361 if (data == NULL) {
0a7de745 2362 return EINVAL;
39037602
A
2363 }
2364
2365 switch (cmd) {
0a7de745 2366 case SIOCSIFORDER: { /* struct if_order */
39037602
A
2367 struct if_order *ifo = (struct if_order *)(void *)data;
2368
a39ff7e2 2369 if (ifo->ifo_count > (u_int32_t)if_index) {
39037602
A
2370 error = EINVAL;
2371 break;
2372 }
2373
0a7de745 2374 size_t length = (ifo->ifo_count * sizeof(u_int32_t));
39037602
A
2375 if (length > 0) {
2376 if (ifo->ifo_ordered_indices == USER_ADDR_NULL) {
2377 error = EINVAL;
2378 break;
2379 }
2380 ordered_indices = _MALLOC(length, M_NECP, M_WAITOK);
2381 if (ordered_indices == NULL) {
2382 error = ENOMEM;
2383 break;
2384 }
2385
2386 error = copyin(ifo->ifo_ordered_indices,
2387 ordered_indices, length);
2388 if (error != 0) {
2389 break;
2390 }
a39ff7e2
A
2391
2392 /* ordered_indices should not contain duplicates */
2393 bool found_duplicate = FALSE;
0a7de745
A
2394 for (uint32_t i = 0; i < (ifo->ifo_count - 1) && !found_duplicate; i++) {
2395 for (uint32_t j = i + 1; j < ifo->ifo_count && !found_duplicate; j++) {
2396 if (ordered_indices[j] == ordered_indices[i]) {
a39ff7e2
A
2397 error = EINVAL;
2398 found_duplicate = TRUE;
2399 break;
2400 }
cc8bc92a
A
2401 }
2402 }
a39ff7e2
A
2403 if (found_duplicate) {
2404 break;
2405 }
cc8bc92a 2406
cb323159
A
2407 error = ifnet_reset_order(ordered_indices, ifo->ifo_count);
2408 } else {
2409 // Clear the list
2410 error = ifnet_reset_order(NULL, 0);
2411 }
39037602
A
2412 break;
2413 }
2414
39037602
A
2415 default: {
2416 VERIFY(0);
2417 /* NOTREACHED */
2418 }
2419 }
2420
2421 if (ordered_indices != NULL) {
2422 _FREE(ordered_indices, M_NECP);
2423 }
2424
0a7de745 2425 return error;
39037602
A
2426}
2427
f427ee49
A
2428static __attribute__((noinline)) int
2429ifioctl_networkid(struct ifnet *ifp, caddr_t data)
2430{
2431 struct if_netidreq *ifnetidr = (struct if_netidreq *)(void *)data;
2432 int error = 0;
2433 int len = ifnetidr->ifnetid_len;
2434
2435 VERIFY(ifp != NULL);
2436
2437 if (len > sizeof(ifnetidr->ifnetid)) {
2438 error = EINVAL;
2439 goto end;
2440 }
2441
2442 if (len == 0) {
2443 bzero(&ifp->network_id, sizeof(ifp->network_id));
2444 } else if (len > sizeof(ifp->network_id)) {
2445 error = EINVAL;
2446 goto end;
2447 }
2448
2449 ifp->network_id_len = len;
2450 bcopy(data, ifp->network_id, len);
2451end:
2452 return error;
2453}
2454
3e170ce0
A
2455static __attribute__((noinline)) int
2456ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
2457{
2458 struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
2459 u_int16_t flags;
2460 int error = 0;
2461
2462 VERIFY(ifp != NULL);
2463
2464 switch (cmd) {
0a7de745
A
2465 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2466 if (ifnsr->ifnsr_len > sizeof(ifnsr->ifnsr_data)) {
3e170ce0
A
2467 error = EINVAL;
2468 break;
2469 }
0a7de745 2470 bcopy(&ifnsr->ifnsr_flags, &flags, sizeof(flags));
3e170ce0
A
2471 error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
2472 ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
2473 break;
2474
0a7de745
A
2475 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2476 ifnsr->ifnsr_len = sizeof(ifnsr->ifnsr_data);
3e170ce0
A
2477 error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
2478 &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
0a7de745
A
2479 if (error == 0) {
2480 bcopy(&flags, &ifnsr->ifnsr_flags, sizeof(flags));
2481 } else {
3e170ce0 2482 ifnsr->ifnsr_len = 0;
0a7de745 2483 }
3e170ce0
A
2484 break;
2485
2486 default:
2487 VERIFY(0);
2488 /* NOTREACHED */
2489 }
2490
0a7de745 2491 return error;
3e170ce0
A
2492}
2493
5ba3f43e
A
2494static __attribute__((noinline)) int
2495ifioctl_nat64prefix(struct ifnet *ifp, u_long cmd, caddr_t data)
2496{
2497 struct if_nat64req *ifnat64 = (struct if_nat64req *)(void *)data;
2498 int error = 0;
2499
2500 VERIFY(ifp != NULL);
2501
2502 switch (cmd) {
0a7de745 2503 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
5ba3f43e 2504 error = ifnet_set_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
0a7de745 2505 if (error != 0) {
d9a64523 2506 ip6stat.ip6s_clat464_plat64_pfx_setfail++;
0a7de745 2507 }
5ba3f43e
A
2508 break;
2509
0a7de745 2510 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
5ba3f43e 2511 error = ifnet_get_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
0a7de745 2512 if (error != 0) {
d9a64523 2513 ip6stat.ip6s_clat464_plat64_pfx_getfail++;
0a7de745 2514 }
5ba3f43e
A
2515 break;
2516
2517 default:
2518 VERIFY(0);
2519 /* NOTREACHED */
2520 }
2521
0a7de745 2522 return error;
5ba3f43e 2523}
d9a64523
A
2524
2525static __attribute__((noinline)) int
2526ifioctl_clat46addr(struct ifnet *ifp, u_long cmd, caddr_t data)
2527{
2528 struct if_clat46req *ifclat46 = (struct if_clat46req *)(void *)data;
2529 struct in6_ifaddr *ia6_clat = NULL;
2530 int error = 0;
2531
2532 VERIFY(ifp != NULL);
2533
2534 switch (cmd) {
0a7de745
A
2535 case SIOCGIFCLAT46ADDR:
2536 ia6_clat = in6ifa_ifpwithflag(ifp, IN6_IFF_CLAT46);
2537 if (ia6_clat == NULL) {
2538 error = ENOENT;
d9a64523 2539 break;
0a7de745
A
2540 }
2541
2542 bcopy(&ia6_clat->ia_addr.sin6_addr, &ifclat46->ifclat46_addr.v6_address,
2543 sizeof(ifclat46->ifclat46_addr.v6_address));
2544 ifclat46->ifclat46_addr.v6_prefixlen = ia6_clat->ia_plen;
2545 IFA_REMREF(&ia6_clat->ia_ifa);
2546 break;
2547 default:
2548 VERIFY(0);
2549 /* NOTREACHED */
d9a64523
A
2550 }
2551
0a7de745 2552 return error;
d9a64523 2553}
5ba3f43e
A
2554
2555
a39ff7e2
A
2556static int
2557ifioctl_get_protolist(struct ifnet *ifp, u_int32_t * ret_count,
2558 user_addr_t ifpl)
2559{
0a7de745
A
2560 u_int32_t actual_count;
2561 u_int32_t count;
2562 int error = 0;
2563 u_int32_t *list = NULL;
a39ff7e2
A
2564
2565 /* find out how many */
2566 count = if_get_protolist(ifp, NULL, 0);
2567 if (ifpl == USER_ADDR_NULL) {
2568 goto done;
2569 }
2570
2571 /* copy out how many there's space for */
2572 if (*ret_count < count) {
2573 count = *ret_count;
2574 }
2575 if (count == 0) {
2576 goto done;
2577 }
d9a64523 2578 list = _MALLOC(count * sizeof(*list), M_TEMP, M_WAITOK | M_ZERO);
a39ff7e2
A
2579 if (list == NULL) {
2580 error = ENOMEM;
2581 goto done;
2582 }
2583 actual_count = if_get_protolist(ifp, list, count);
2584 if (actual_count < count) {
2585 count = actual_count;
2586 }
2587 if (count != 0) {
2588 error = copyout((caddr_t)list, ifpl, count * sizeof(*list));
2589 }
2590
0a7de745 2591done:
a39ff7e2
A
2592 if (list != NULL) {
2593 if_free_protolist(list);
2594 }
2595 *ret_count = count;
0a7de745 2596 return error;
a39ff7e2
A
2597}
2598
2599static __attribute__((noinline)) int
2600ifioctl_protolist(struct ifnet *ifp, u_long cmd, caddr_t data)
2601{
2602 int error = 0;
2603
2604 switch (cmd) {
0a7de745
A
2605 case SIOCGIFPROTOLIST32: { /* struct if_protolistreq32 */
2606 struct if_protolistreq32 ifpl;
a39ff7e2
A
2607
2608 bcopy(data, &ifpl, sizeof(ifpl));
2609 if (ifpl.ifpl_reserved != 0) {
2610 error = EINVAL;
2611 break;
2612 }
2613 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2614 CAST_USER_ADDR_T(ifpl.ifpl_list));
2615 bcopy(&ifpl, data, sizeof(ifpl));
2616 break;
2617 }
0a7de745
A
2618 case SIOCGIFPROTOLIST64: { /* struct if_protolistreq64 */
2619 struct if_protolistreq64 ifpl;
a39ff7e2
A
2620
2621 bcopy(data, &ifpl, sizeof(ifpl));
2622 if (ifpl.ifpl_reserved != 0) {
2623 error = EINVAL;
2624 break;
2625 }
2626 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2627 ifpl.ifpl_list);
2628 bcopy(&ifpl, data, sizeof(ifpl));
2629 break;
2630 }
2631 default:
2632 VERIFY(0);
2633 /* NOTREACHED */
2634 }
2635
0a7de745 2636 return error;
a39ff7e2
A
2637}
2638
5c9f4661
A
2639/*
2640 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2641 */
2642static bool
2643ifioctl_restrict_intcoproc(unsigned long cmd, const char *ifname,
2644 struct ifnet *ifp, struct proc *p)
2645{
5c9f4661 2646 if (intcoproc_unrestricted == TRUE) {
0a7de745 2647 return false;
5c9f4661
A
2648 }
2649 if (proc_pid(p) == 0) {
0a7de745 2650 return false;
5c9f4661
A
2651 }
2652 if (ifname) {
2653 ifp = ifunit(ifname);
2654 }
2655 if (ifp == NULL) {
0a7de745 2656 return false;
5c9f4661
A
2657 }
2658 if (!IFNET_IS_INTCOPROC(ifp)) {
0a7de745 2659 return false;
5c9f4661
A
2660 }
2661 switch (cmd) {
2662 case SIOCGIFBRDADDR:
2663 case SIOCGIFCONF32:
2664 case SIOCGIFCONF64:
2665 case SIOCGIFFLAGS:
2666 case SIOCGIFEFLAGS:
2667 case SIOCGIFCAP:
5c9f4661
A
2668 case SIOCGIFMETRIC:
2669 case SIOCGIFMTU:
2670 case SIOCGIFPHYS:
2671 case SIOCGIFTYPE:
2672 case SIOCGIFFUNCTIONALTYPE:
2673 case SIOCGIFPSRCADDR:
2674 case SIOCGIFPDSTADDR:
2675 case SIOCGIFGENERIC:
2676 case SIOCGIFDEVMTU:
2677 case SIOCGIFVLAN:
2678 case SIOCGIFBOND:
2679 case SIOCGIFWAKEFLAGS:
2680 case SIOCGIFGETRTREFCNT:
2681 case SIOCGIFOPPORTUNISTIC:
2682 case SIOCGIFLINKQUALITYMETRIC:
2683 case SIOCGIFLOG:
2684 case SIOCGIFDELEGATE:
2685 case SIOCGIFEXPENSIVE:
2686 case SIOCGIFINTERFACESTATE:
2687 case SIOCGIFPROBECONNECTIVITY:
2688 case SIOCGIFTIMESTAMPENABLED:
2689 case SIOCGECNMODE:
2690 case SIOCGQOSMARKINGMODE:
2691 case SIOCGQOSMARKINGENABLED:
2692 case SIOCGIFLOWINTERNET:
2693 case SIOCGIFSTATUS:
2694 case SIOCGIFMEDIA32:
2695 case SIOCGIFMEDIA64:
0a7de745
A
2696 case SIOCGIFXMEDIA32:
2697 case SIOCGIFXMEDIA64:
5c9f4661
A
2698 case SIOCGIFDESC:
2699 case SIOCGIFLINKPARAMS:
2700 case SIOCGIFQUEUESTATS:
2701 case SIOCGIFTHROTTLE:
2702 case SIOCGIFAGENTIDS32:
2703 case SIOCGIFAGENTIDS64:
2704 case SIOCGIFNETSIGNATURE:
2705 case SIOCGIFINFO_IN6:
2706 case SIOCGIFAFLAG_IN6:
2707 case SIOCGNBRINFO_IN6:
2708 case SIOCGIFALIFETIME_IN6:
2709 case SIOCGIFNETMASK_IN6:
a39ff7e2
A
2710 case SIOCGIFPROTOLIST32:
2711 case SIOCGIFPROTOLIST64:
cb323159 2712 case SIOCGIFXFLAGS:
0a7de745 2713 return false;
5c9f4661
A
2714 default:
2715#if (DEBUG || DEVELOPMENT)
2716 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2717 __func__, cmd, proc_pid(p));
2718#endif
0a7de745
A
2719 return true;
2720 }
2721 return false;
2722}
2723
2724/*
2725 * Given a media word, return one suitable for an application
2726 * using the original encoding.
2727 */
2728static int
2729compat_media(int media)
2730{
2731 if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
2732 media &= ~IFM_TMASK;
2733 media |= IFM_OTHER;
5c9f4661 2734 }
0a7de745
A
2735 return media;
2736}
2737
2738static int
2739compat_ifmu_ulist(struct ifnet *ifp, u_long cmd, void *data)
2740{
2741 struct ifmediareq *ifmr = (struct ifmediareq *)data;
2742 user_addr_t user_addr;
2743 int i;
2744 int *media_list = NULL;
2745 int error = 0;
2746 bool list_modified = false;
2747
2748 user_addr = (cmd == SIOCGIFMEDIA64) ?
2749 ((struct ifmediareq64 *)ifmr)->ifmu_ulist :
2750 CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
2751 if (user_addr == USER_ADDR_NULL || ifmr->ifm_count == 0) {
2752 return 0;
2753 }
2754 MALLOC(media_list, int *, ifmr->ifm_count * sizeof(int),
2755 M_TEMP, M_WAITOK | M_ZERO);
2756 if (media_list == NULL) {
2757 os_log_error(OS_LOG_DEFAULT,
2758 "%s: %s MALLOC() failed",
2759 __func__, ifp->if_xname);
2760 error = ENOMEM;
2761 goto done;
2762 }
2763 error = copyin(user_addr, media_list, ifmr->ifm_count * sizeof(int));
2764 if (error != 0) {
2765 os_log_error(OS_LOG_DEFAULT,
2766 "%s: %s copyin() error %d",
2767 __func__, ifp->if_xname, error);
2768 goto done;
2769 }
2770 for (i = 0; i < ifmr->ifm_count; i++) {
2771 int old_media, new_media;
2772
2773 old_media = media_list[i];
2774
2775 new_media = compat_media(old_media);
2776 if (new_media == old_media) {
2777 continue;
2778 }
2779 if (if_verbose != 0) {
2780 os_log_info(OS_LOG_DEFAULT,
2781 "%s: %s converted extended media %08x to compat media %08x",
2782 __func__, ifp->if_xname, old_media, new_media);
2783 }
2784 media_list[i] = new_media;
2785 list_modified = true;
2786 }
2787 if (list_modified) {
2788 error = copyout(media_list, user_addr, ifmr->ifm_count * sizeof(int));
2789 if (error != 0) {
2790 os_log_error(OS_LOG_DEFAULT,
2791 "%s: %s copyout() error %d",
2792 __func__, ifp->if_xname, error);
2793 goto done;
2794 }
2795 }
2796done:
2797 if (media_list != NULL) {
2798 FREE(media_list, M_TEMP);
2799 }
2800 return error;
2801}
2802
2803static int
2804compat_ifmediareq(struct ifnet *ifp, u_long cmd, void *data)
2805{
2806 struct ifmediareq *ifmr = (struct ifmediareq *)data;
2807 int error;
2808
2809 ifmr->ifm_active = compat_media(ifmr->ifm_active);
2810 ifmr->ifm_current = compat_media(ifmr->ifm_current);
2811
2812 error = compat_ifmu_ulist(ifp, cmd, data);
2813
2814 return error;
5c9f4661
A
2815}
2816
0a7de745
A
2817static int
2818ifioctl_get_media(struct ifnet *ifp, struct socket *so, u_long cmd, caddr_t data)
2819{
2820 int error = 0;
2821
2822 /*
2823 * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
2824 * media subtypes macros from <net/if_media.h>
2825 */
2826 switch (cmd) {
2827 case SIOCGIFMEDIA32:
2828 case SIOCGIFXMEDIA32:
2829 error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA32, data);
2830 break;
2831 case SIOCGIFMEDIA64:
2832 case SIOCGIFXMEDIA64:
2833 error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA64, data);
2834 break;
2835 }
2836 if (if_verbose != 0 && error != 0) {
2837 os_log(OS_LOG_DEFAULT, "%s: first ifnet_ioctl(%s, %08lx) error %d",
2838 __func__, ifp->if_xname, cmd, error);
2839 }
2840 if (error == 0 && (cmd == SIOCGIFMEDIA32 || cmd == SIOCGIFMEDIA64)) {
2841 error = compat_ifmediareq(ifp, cmd, data);
2842 }
2843 return error;
2844}
39236c6e
A
2845/*
2846 * Interface ioctls.
2847 *
2848 * Most of the routines called to handle the ioctls would end up being
2849 * tail-call optimized, which unfortunately causes this routine to
2850 * consume too much stack space; this is the reason for the "noinline"
2851 * attribute used on those routines.
2852 */
2853int
2854ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
2855{
2856 char ifname[IFNAMSIZ + 1];
2857 struct ifnet *ifp = NULL;
2858 struct ifstat *ifs = NULL;
2859 int error = 0;
2860
0a7de745 2861 bzero(ifname, sizeof(ifname));
39236c6e
A
2862
2863 /*
2864 * ioctls which don't require ifp, or ifreq ioctls
2865 */
2866 switch (cmd) {
0a7de745
A
2867 case OSIOCGIFCONF32: /* struct ifconf32 */
2868 case SIOCGIFCONF32: /* struct ifconf32 */
2869 case SIOCGIFCONF64: /* struct ifconf64 */
2870 case OSIOCGIFCONF64: /* struct ifconf64 */
39236c6e
A
2871 error = ifioctl_ifconf(cmd, data);
2872 goto done;
2873
0a7de745
A
2874 case SIOCIFGCLONERS32: /* struct if_clonereq32 */
2875 case SIOCIFGCLONERS64: /* struct if_clonereq64 */
39236c6e
A
2876 error = ifioctl_ifclone(cmd, data);
2877 goto done;
316670eb 2878
0a7de745
A
2879 case SIOCGIFAGENTDATA32: /* struct netagent_req32 */
2880 case SIOCGIFAGENTDATA64: /* struct netagent_req64 */
39037602
A
2881 case SIOCGIFAGENTLIST32: /* struct netagentlist_req32 */
2882 case SIOCGIFAGENTLIST64: /* struct netagentlist_req64 */
3e170ce0
A
2883 error = netagent_ioctl(cmd, data);
2884 goto done;
2885
0a7de745 2886 case SIOCSIFORDER: /* struct if_order */
39037602
A
2887 error = ifioctl_iforder(cmd, data);
2888 goto done;
2889
0a7de745
A
2890 case SIOCSIFDSTADDR: /* struct ifreq */
2891 case SIOCSIFADDR: /* struct ifreq */
2892 case SIOCSIFBRDADDR: /* struct ifreq */
2893 case SIOCSIFNETMASK: /* struct ifreq */
2894 case OSIOCGIFADDR: /* struct ifreq */
2895 case OSIOCGIFDSTADDR: /* struct ifreq */
2896 case OSIOCGIFBRDADDR: /* struct ifreq */
2897 case OSIOCGIFNETMASK: /* struct ifreq */
2898 case SIOCSIFKPI: /* struct ifreq */
316670eb
A
2899 if (so->so_proto == NULL) {
2900 error = EOPNOTSUPP;
2901 goto done;
2902 }
f427ee49 2903 OS_FALLTHROUGH;
0a7de745
A
2904 case SIOCIFCREATE: /* struct ifreq */
2905 case SIOCIFCREATE2: /* struct ifreq */
2906 case SIOCIFDESTROY: /* struct ifreq */
2907 case SIOCGIFFLAGS: /* struct ifreq */
2908 case SIOCGIFEFLAGS: /* struct ifreq */
2909 case SIOCGIFCAP: /* struct ifreq */
0a7de745
A
2910 case SIOCGIFMETRIC: /* struct ifreq */
2911 case SIOCGIFMTU: /* struct ifreq */
2912 case SIOCGIFPHYS: /* struct ifreq */
2913 case SIOCSIFFLAGS: /* struct ifreq */
2914 case SIOCSIFCAP: /* struct ifreq */
2915 case SIOCSIFMETRIC: /* struct ifreq */
2916 case SIOCSIFPHYS: /* struct ifreq */
2917 case SIOCSIFMTU: /* struct ifreq */
2918 case SIOCADDMULTI: /* struct ifreq */
2919 case SIOCDELMULTI: /* struct ifreq */
2920 case SIOCDIFPHYADDR: /* struct ifreq */
2921 case SIOCSIFMEDIA: /* struct ifreq */
2922 case SIOCSIFGENERIC: /* struct ifreq */
2923 case SIOCSIFLLADDR: /* struct ifreq */
2924 case SIOCSIFALTMTU: /* struct ifreq */
2925 case SIOCSIFVLAN: /* struct ifreq */
2926 case SIOCSIFBOND: /* struct ifreq */
2927 case SIOCGIFLLADDR: /* struct ifreq */
2928 case SIOCGIFTYPE: /* struct ifreq */
2929 case SIOCGIFFUNCTIONALTYPE: /* struct ifreq */
2930 case SIOCGIFPSRCADDR: /* struct ifreq */
2931 case SIOCGIFPDSTADDR: /* struct ifreq */
2932 case SIOCGIFGENERIC: /* struct ifreq */
2933 case SIOCGIFDEVMTU: /* struct ifreq */
2934 case SIOCGIFVLAN: /* struct ifreq */
2935 case SIOCGIFBOND: /* struct ifreq */
2936 case SIOCGIFWAKEFLAGS: /* struct ifreq */
2937 case SIOCGIFGETRTREFCNT: /* struct ifreq */
2938 case SIOCSIFOPPORTUNISTIC: /* struct ifreq */
2939 case SIOCGIFOPPORTUNISTIC: /* struct ifreq */
2940 case SIOCGIFLINKQUALITYMETRIC: /* struct ifreq */
2941 case SIOCSIFLOG: /* struct ifreq */
2942 case SIOCGIFLOG: /* struct ifreq */
2943 case SIOCGIFDELEGATE: /* struct ifreq */
2944 case SIOCGIFEXPENSIVE: /* struct ifreq */
2945 case SIOCSIFEXPENSIVE: /* struct ifreq */
2946 case SIOCSIF2KCL: /* struct ifreq */
2947 case SIOCGIF2KCL: /* struct ifreq */
2948 case SIOCSIFINTERFACESTATE: /* struct ifreq */
2949 case SIOCGIFINTERFACESTATE: /* struct ifreq */
2950 case SIOCSIFPROBECONNECTIVITY: /* struct ifreq */
2951 case SIOCGIFPROBECONNECTIVITY: /* struct ifreq */
2952 case SIOCGSTARTDELAY: /* struct ifreq */
2953 case SIOCSIFTIMESTAMPENABLE: /* struct ifreq */
2954 case SIOCSIFTIMESTAMPDISABLE: /* struct ifreq */
2955 case SIOCGIFTIMESTAMPENABLED: /* struct ifreq */
39037602 2956#if (DEBUG || DEVELOPMENT)
0a7de745 2957 case SIOCSIFDISABLEOUTPUT: /* struct ifreq */
39037602 2958#endif /* (DEBUG || DEVELOPMENT) */
0a7de745 2959 case SIOCGECNMODE: /* struct ifreq */
5ba3f43e 2960 case SIOCSECNMODE:
0a7de745
A
2961 case SIOCSQOSMARKINGMODE: /* struct ifreq */
2962 case SIOCSQOSMARKINGENABLED: /* struct ifreq */
2963 case SIOCGQOSMARKINGMODE: /* struct ifreq */
2964 case SIOCGQOSMARKINGENABLED: /* struct ifreq */
2965 case SIOCSIFLOWINTERNET: /* struct ifreq */
2966 case SIOCGIFLOWINTERNET: /* struct ifreq */
2967 case SIOCGIFLOWPOWER: /* struct ifreq */
2968 case SIOCSIFLOWPOWER: /* struct ifreq */
cb323159
A
2969 case SIOCSIF6LOWPAN: /* struct ifreq */
2970 case SIOCGIF6LOWPAN: /* struct ifreq */
2971 case SIOCGIFMPKLOG: /* struct ifreq */
2972 case SIOCSIFMPKLOG: /* struct ifreq */
2973 case SIOCGIFCONSTRAINED: /* struct ifreq */
2974 case SIOCSIFCONSTRAINED: /* struct ifreq */
2975 case SIOCGIFXFLAGS: /* struct ifreq */
2976 case SIOCGIFNOACKPRIO: /* struct ifreq */
2977 case SIOCSIFNOACKPRIO: /* struct ifreq */
0a7de745 2978 { /* struct ifreq */
39037602 2979 struct ifreq ifr;
0a7de745 2980 bcopy(data, &ifr, sizeof(ifr));
39037602
A
2981 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2982 bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
5c9f4661
A
2983 if (ifioctl_restrict_intcoproc(cmd, ifname, NULL, p) == true) {
2984 error = EPERM;
2985 goto done;
2986 }
39037602 2987 error = ifioctl_ifreq(so, cmd, &ifr, p);
0a7de745 2988 bcopy(&ifr, data, sizeof(ifr));
39037602
A
2989 goto done;
2990 }
316670eb
A
2991 }
2992
2993 /*
2994 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2995 * here to ensure that the ifnet, if found, has been fully attached.
2996 */
2997 dlil_if_lock();
2998 switch (cmd) {
0a7de745 2999 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
39236c6e 3000 bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
316670eb 3001 ifname, IFNAMSIZ);
cb323159 3002 ifp = ifunit_ref(ifname);
316670eb 3003 break;
316670eb 3004
0a7de745 3005 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
316670eb
A
3006 bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
3007 ifname, IFNAMSIZ);
cb323159 3008 ifp = ifunit_ref(ifname);
316670eb 3009 break;
316670eb 3010
0a7de745 3011 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
316670eb
A
3012 bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
3013 ifname, IFNAMSIZ);
cb323159 3014 ifp = ifunit_ref(ifname);
316670eb 3015 break;
316670eb 3016
0a7de745
A
3017 case SIOCGIFSTATUS: /* struct ifstat */
3018 ifs = _MALLOC(sizeof(*ifs), M_DEVBUF, M_WAITOK);
316670eb
A
3019 if (ifs == NULL) {
3020 error = ENOMEM;
3021 dlil_if_unlock();
3022 goto done;
3023 }
0a7de745 3024 bcopy(data, ifs, sizeof(*ifs));
316670eb 3025 ifs->ifs_name[IFNAMSIZ - 1] = '\0';
39236c6e 3026 bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
cb323159 3027 ifp = ifunit_ref(ifname);
316670eb 3028 break;
316670eb 3029
0a7de745
A
3030 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
3031 case SIOCGIFXMEDIA32: /* struct ifmediareq32 */
316670eb
A
3032 bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
3033 ifname, IFNAMSIZ);
cb323159 3034 ifp = ifunit_ref(ifname);
316670eb 3035 break;
316670eb 3036
0a7de745
A
3037 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
3038 case SIOCGIFXMEDIA64: /* struct ifmediareq64 */
316670eb
A
3039 bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
3040 ifname, IFNAMSIZ);
cb323159 3041 ifp = ifunit_ref(ifname);
316670eb 3042 break;
316670eb 3043
0a7de745
A
3044 case SIOCSIFDESC: /* struct if_descreq */
3045 case SIOCGIFDESC: /* struct if_descreq */
316670eb
A
3046 bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
3047 ifname, IFNAMSIZ);
cb323159 3048 ifp = ifunit_ref(ifname);
316670eb 3049 break;
316670eb 3050
0a7de745
A
3051 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
3052 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
316670eb
A
3053 bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
3054 ifname, IFNAMSIZ);
cb323159 3055 ifp = ifunit_ref(ifname);
316670eb 3056 break;
316670eb 3057
0a7de745 3058 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
316670eb
A
3059 bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
3060 ifname, IFNAMSIZ);
cb323159 3061 ifp = ifunit_ref(ifname);
316670eb 3062 break;
316670eb 3063
0a7de745
A
3064 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
3065 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
316670eb
A
3066 bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
3067 ifname, IFNAMSIZ);
cb323159 3068 ifp = ifunit_ref(ifname);
316670eb 3069 break;
316670eb 3070
0a7de745
A
3071 case SIOCAIFAGENTID: /* struct if_agentidreq */
3072 case SIOCDIFAGENTID: /* struct if_agentidreq */
3073 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
3074 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
3e170ce0 3075 bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
39037602 3076 ifname, IFNAMSIZ);
cb323159 3077 ifp = ifunit_ref(ifname);
3e170ce0
A
3078 break;
3079
0a7de745
A
3080 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
3081 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
3e170ce0 3082 bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
39037602 3083 ifname, IFNAMSIZ);
cb323159 3084 ifp = ifunit_ref(ifname);
3e170ce0
A
3085 break;
3086
f427ee49
A
3087 case SIOCSIFNETWORKID: /* struct if_netidreq */
3088 bcopy(((struct if_netidreq *)(void *)data)->ifnetid_name,
3089 ifname, IFNAMSIZ);
3090 ifp = ifunit_ref(ifname);
3091 break;
0a7de745
A
3092 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
3093 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
a39ff7e2
A
3094 bcopy(((struct if_protolistreq *)(void *)data)->ifpl_name,
3095 ifname, IFNAMSIZ);
cb323159 3096 ifp = ifunit_ref(ifname);
a39ff7e2 3097 break;
39236c6e 3098 default:
316670eb
A
3099 /*
3100 * This is a bad assumption, but the code seems to
3101 * have been doing this in the past; caveat emptor.
39236c6e
A
3102 */
3103 bcopy(((struct ifreq *)(void *)data)->ifr_name,
3104 ifname, IFNAMSIZ);
cb323159 3105 ifp = ifunit_ref(ifname);
316670eb
A
3106 break;
3107 }
39236c6e 3108 dlil_if_unlock();
316670eb 3109
39236c6e
A
3110 if (ifp == NULL) {
3111 error = ENXIO;
3112 goto done;
3113 }
316670eb 3114
5c9f4661
A
3115 if (ifioctl_restrict_intcoproc(cmd, NULL, ifp, p) == true) {
3116 error = EPERM;
3117 goto done;
3118 }
39236c6e 3119 switch (cmd) {
0a7de745 3120 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
0a7de745
A
3121 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
3122 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
39236c6e 3123 error = proc_suser(p);
0a7de745 3124 if (error != 0) {
39236c6e 3125 break;
0a7de745 3126 }
316670eb 3127
39236c6e 3128 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
0a7de745 3129 if (error != 0) {
39236c6e 3130 break;
0a7de745 3131 }
316670eb 3132
39236c6e 3133 ifnet_touch_lastchange(ifp);
316670eb 3134 break;
316670eb 3135
0a7de745 3136 case SIOCGIFSTATUS: /* struct ifstat */
39236c6e
A
3137 VERIFY(ifs != NULL);
3138 ifs->ascii[0] = '\0';
316670eb 3139
39236c6e
A
3140 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
3141
0a7de745 3142 bcopy(ifs, data, sizeof(*ifs));
316670eb 3143 break;
316670eb 3144
cb323159
A
3145 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
3146 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
0a7de745
A
3147 case SIOCGIFXMEDIA32: /* struct ifmediareq32 */
3148 case SIOCGIFXMEDIA64: /* struct ifmediareq64 */
3149 error = ifioctl_get_media(ifp, so, cmd, data);
39236c6e 3150 break;
316670eb 3151
0a7de745
A
3152 case SIOCSIFDESC: /* struct if_descreq */
3153 case SIOCGIFDESC: /* struct if_descreq */
39236c6e
A
3154 error = ifioctl_ifdesc(ifp, cmd, data, p);
3155 break;
316670eb 3156
0a7de745
A
3157 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
3158 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
39236c6e 3159 error = ifioctl_linkparams(ifp, cmd, data, p);
316670eb 3160 break;
316670eb 3161
0a7de745 3162 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
39236c6e
A
3163 error = ifioctl_qstats(ifp, cmd, data);
3164 break;
316670eb 3165
0a7de745
A
3166 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
3167 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
39236c6e 3168 error = ifioctl_throttle(ifp, cmd, data, p);
316670eb 3169 break;
316670eb 3170
0a7de745
A
3171 case SIOCAIFAGENTID: /* struct if_agentidreq */
3172 case SIOCDIFAGENTID: /* struct if_agentidreq */
3173 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
3174 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
3e170ce0
A
3175 error = ifioctl_netagent(ifp, cmd, data, p);
3176 break;
3177
0a7de745
A
3178 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
3179 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
3e170ce0
A
3180 error = ifioctl_netsignature(ifp, cmd, data);
3181 break;
3182
f427ee49
A
3183 case SIOCSIFNETWORKID: /* struct if_netidreq */
3184 error = ifioctl_networkid(ifp, data);
3185 break;
0a7de745
A
3186 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
3187 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
5ba3f43e
A
3188 error = ifioctl_nat64prefix(ifp, cmd, data);
3189 break;
d9a64523 3190
0a7de745 3191 case SIOCGIFCLAT46ADDR: /* struct if_clat46req */
d9a64523
A
3192 error = ifioctl_clat46addr(ifp, cmd, data);
3193 break;
a39ff7e2 3194
0a7de745
A
3195 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
3196 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
a39ff7e2
A
3197 error = ifioctl_protolist(ifp, cmd, data);
3198 break;
3199
316670eb
A
3200 default:
3201 if (so->so_proto == NULL) {
3202 error = EOPNOTSUPP;
3203 break;
3204 }
3205
3206 socket_lock(so, 1);
3207 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3208 data, ifp, p));
3209 socket_unlock(so, 1);
3210
0a7de745
A
3211 // Don't allow to call SIOCAIFADDR and SIOCDIFADDR with
3212 // ifreq as the code expects ifaddr
3213 if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3214 !(cmd == SIOCAIFADDR || cmd == SIOCDIFADDR)) {
39236c6e 3215 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
316670eb
A
3216 }
3217 break;
3218 }
3219
3220done:
0a7de745 3221 if (ifs != NULL) {
316670eb 3222 _FREE(ifs, M_DEVBUF);
0a7de745 3223 }
316670eb 3224
39236c6e 3225 if (if_verbose) {
0a7de745
A
3226 if (ifname[0] == '\0') {
3227 (void) snprintf(ifname, sizeof(ifname), "%s",
39236c6e 3228 "NULL");
0a7de745
A
3229 } else if (ifp != NULL) {
3230 (void) snprintf(ifname, sizeof(ifname), "%s",
39236c6e 3231 if_name(ifp));
0a7de745 3232 }
39236c6e
A
3233
3234 if (error != 0) {
3235 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3236 "%c %lu) error %d\n", __func__,
3237 proc_name_address(p), proc_pid(p),
3238 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3239 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3240 (char)IOCGROUP(cmd), cmd & 0xff, error);
3241 } else if (if_verbose > 1) {
3242 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
3243 "%c %lu) OK\n", __func__,
3244 proc_name_address(p), proc_pid(p),
3245 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
3246 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
3247 (char)IOCGROUP(cmd), cmd & 0xff);
3248 }
3249 }
3250
cb323159
A
3251 if (ifp != NULL) {
3252 ifnet_decr_iorefcnt(ifp);
3253 }
0a7de745 3254 return error;
316670eb
A
3255}
3256
39236c6e 3257static __attribute__((noinline)) int
316670eb
A
3258ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
3259{
3260 struct ifnet *ifp;
3261 u_long ocmd = cmd;
3262 int error = 0;
3263 struct kev_msg ev_msg;
3264 struct net_event_data ev_data;
3265
0a7de745
A
3266 bzero(&ev_data, sizeof(struct net_event_data));
3267 bzero(&ev_msg, sizeof(struct kev_msg));
316670eb 3268
4a249263
A
3269 switch (cmd) {
3270 case SIOCIFCREATE:
d1ecb069 3271 case SIOCIFCREATE2:
39037602 3272 error = proc_suser(p);
0a7de745
A
3273 if (error) {
3274 return error;
3275 }
3276 return if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
3277 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
4a249263 3278 case SIOCIFDESTROY:
91447636 3279 error = proc_suser(p);
0a7de745
A
3280 if (error) {
3281 return error;
3282 }
3283 return if_clone_destroy(ifr->ifr_name);
4a249263
A
3284 }
3285
39236c6e
A
3286 /*
3287 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
3288 * here to ensure that the ifnet, if found, has been fully attached.
3289 */
3290 dlil_if_lock();
1c79356b 3291 ifp = ifunit(ifr->ifr_name);
39236c6e
A
3292 dlil_if_unlock();
3293
0a7de745
A
3294 if (ifp == NULL) {
3295 return ENXIO;
3296 }
1c79356b 3297
6d2010ae 3298 switch (cmd) {
1c79356b 3299 case SIOCGIFFLAGS:
91447636 3300 ifnet_lock_shared(ifp);
1c79356b 3301 ifr->ifr_flags = ifp->if_flags;
91447636 3302 ifnet_lock_done(ifp);
1c79356b
A
3303 break;
3304
316670eb
A
3305 case SIOCGIFEFLAGS:
3306 ifnet_lock_shared(ifp);
3307 ifr->ifr_eflags = ifp->if_eflags;
3308 ifnet_lock_done(ifp);
3309 break;
3310
cb323159
A
3311 case SIOCGIFXFLAGS:
3312 ifnet_lock_shared(ifp);
3313 ifr->ifr_xflags = ifp->if_xflags;
3314 ifnet_lock_done(ifp);
3315 break;
3316
6d2010ae
A
3317 case SIOCGIFCAP:
3318 ifnet_lock_shared(ifp);
3319 ifr->ifr_reqcap = ifp->if_capabilities;
3320 ifr->ifr_curcap = ifp->if_capenable;
3321 ifnet_lock_done(ifp);
3322 break;
3323
1c79356b 3324 case SIOCGIFMETRIC:
91447636 3325 ifnet_lock_shared(ifp);
1c79356b 3326 ifr->ifr_metric = ifp->if_metric;
91447636 3327 ifnet_lock_done(ifp);
1c79356b
A
3328 break;
3329
3330 case SIOCGIFMTU:
91447636 3331 ifnet_lock_shared(ifp);
1c79356b 3332 ifr->ifr_mtu = ifp->if_mtu;
91447636 3333 ifnet_lock_done(ifp);
1c79356b
A
3334 break;
3335
3336 case SIOCGIFPHYS:
91447636 3337 ifnet_lock_shared(ifp);
1c79356b 3338 ifr->ifr_phys = ifp->if_physical;
91447636 3339 ifnet_lock_done(ifp);
1c79356b
A
3340 break;
3341
3342 case SIOCSIFFLAGS:
91447636 3343 error = proc_suser(p);
0a7de745 3344 if (error != 0) {
6d2010ae 3345 break;
0a7de745 3346 }
1c79356b 3347
6d2010ae
A
3348 (void) ifnet_set_flags(ifp, ifr->ifr_flags,
3349 (u_int16_t)~IFF_CANTCHANGE);
2d21ac55 3350
6d2010ae
A
3351 /*
3352 * Note that we intentionally ignore any error from below
3353 * for the SIOCSIFFLAGS case.
3354 */
39236c6e 3355 (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
1c79356b 3356
6d2010ae
A
3357 /*
3358 * Send the event even upon error from the driver because
3359 * we changed the flags.
3360 */
a39ff7e2 3361 dlil_post_sifflags_msg(ifp);
b0d623f7 3362
91447636 3363 ifnet_touch_lastchange(ifp);
1c79356b
A
3364 break;
3365
6d2010ae
A
3366 case SIOCSIFCAP:
3367 error = proc_suser(p);
0a7de745 3368 if (error != 0) {
6d2010ae 3369 break;
0a7de745 3370 }
6d2010ae
A
3371
3372 if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
3373 error = EINVAL;
3374 break;
3375 }
39236c6e 3376 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
6d2010ae
A
3377
3378 ifnet_touch_lastchange(ifp);
3379 break;
3380
1c79356b 3381 case SIOCSIFMETRIC:
91447636 3382 error = proc_suser(p);
0a7de745 3383 if (error != 0) {
6d2010ae 3384 break;
0a7de745 3385 }
1c79356b 3386
6d2010ae 3387 ifp->if_metric = ifr->ifr_metric;
1c79356b
A
3388
3389 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3390 ev_msg.kev_class = KEV_NETWORK_CLASS;
3391 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
6d2010ae 3392
1c79356b 3393 ev_msg.event_code = KEV_DL_SIFMETRICS;
2d21ac55 3394 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
1c79356b 3395 ev_data.if_family = ifp->if_family;
b0d623f7 3396 ev_data.if_unit = (u_int32_t) ifp->if_unit;
1c79356b
A
3397 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3398 ev_msg.dv[0].data_ptr = &ev_data;
3399
3400 ev_msg.dv[1].data_length = 0;
39037602 3401 dlil_post_complete_msg(ifp, &ev_msg);
1c79356b 3402
91447636 3403 ifnet_touch_lastchange(ifp);
1c79356b
A
3404 break;
3405
3406 case SIOCSIFPHYS:
91447636 3407 error = proc_suser(p);
0a7de745 3408 if (error != 0) {
6d2010ae 3409 break;
0a7de745 3410 }
1c79356b 3411
39236c6e 3412 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
0a7de745 3413 if (error != 0) {
6d2010ae 3414 break;
0a7de745 3415 }
1c79356b 3416
6d2010ae
A
3417 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3418 ev_msg.kev_class = KEV_NETWORK_CLASS;
3419 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3420
3421 ev_msg.event_code = KEV_DL_SIFPHYS;
3422 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3423 ev_data.if_family = ifp->if_family;
3424 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3425 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3426 ev_msg.dv[0].data_ptr = &ev_data;
3427 ev_msg.dv[1].data_length = 0;
39037602 3428 dlil_post_complete_msg(ifp, &ev_msg);
6d2010ae
A
3429
3430 ifnet_touch_lastchange(ifp);
3431 break;
1c79356b 3432
316670eb 3433 case SIOCSIFMTU: {
b0d623f7 3434 u_int32_t oldmtu = ifp->if_mtu;
316670eb 3435 struct ifclassq *ifq = &ifp->if_snd;
1c79356b 3436
91447636 3437 error = proc_suser(p);
0a7de745 3438 if (error != 0) {
6d2010ae 3439 break;
0a7de745 3440 }
1c79356b 3441
6d2010ae
A
3442 if (ifp->if_ioctl == NULL) {
3443 error = EOPNOTSUPP;
3444 break;
3445 }
3446 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
3447 error = EINVAL;
3448 break;
1c79356b 3449 }
39236c6e 3450 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
0a7de745 3451 if (error != 0) {
6d2010ae 3452 break;
0a7de745 3453 }
6d2010ae
A
3454
3455 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3456 ev_msg.kev_class = KEV_NETWORK_CLASS;
3457 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3458
3459 ev_msg.event_code = KEV_DL_SIFMTU;
3460 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3461 ev_data.if_family = ifp->if_family;
3462 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3463 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3464 ev_msg.dv[0].data_ptr = &ev_data;
3465 ev_msg.dv[1].data_length = 0;
39037602 3466 dlil_post_complete_msg(ifp, &ev_msg);
6d2010ae
A
3467
3468 ifnet_touch_lastchange(ifp);
3469 rt_ifmsg(ifp);
3470
1c79356b 3471 /*
2d21ac55
A
3472 * If the link MTU changed, do network layer specific procedure
3473 * and update all route entries associated with the interface,
3474 * so that their MTU metric gets updated.
1c79356b 3475 */
6d2010ae 3476 if (ifp->if_mtu != oldmtu) {
2d21ac55 3477 if_rtmtu_update(ifp);
9bccf70c 3478 nd6_setmtu(ifp);
316670eb
A
3479 /* Inform all transmit queues about the new MTU */
3480 IFCQ_LOCK(ifq);
3481 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
3482 IFCQ_UNLOCK(ifq);
1c79356b 3483 }
6d2010ae 3484 break;
9bccf70c 3485 }
1c79356b
A
3486
3487 case SIOCADDMULTI:
3488 case SIOCDELMULTI:
91447636 3489 error = proc_suser(p);
0a7de745 3490 if (error != 0) {
6d2010ae 3491 break;
0a7de745 3492 }
1c79356b
A
3493
3494 /* Don't allow group membership on non-multicast interfaces. */
6d2010ae
A
3495 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
3496 error = EOPNOTSUPP;
3497 break;
3498 }
1c79356b 3499
9bccf70c 3500 /* Don't let users screw up protocols' entries. */
6d2010ae
A
3501 if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
3502 ifr->ifr_addr.sa_family != AF_LINK) {
3503 error = EINVAL;
3504 break;
3505 }
f427ee49
A
3506 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
3507 ifr->ifr_addr.sa_len = sizeof(struct sockaddr);
3508 }
9bccf70c 3509
6d2010ae
A
3510 /*
3511 * User is permitted to anonymously join a particular link
3512 * multicast group via SIOCADDMULTI. Subsequent join requested
3513 * for the same record which has an outstanding refcnt from a
3514 * past if_addmulti_anon() will not result in EADDRINUSE error
3515 * (unlike other BSDs.) Anonymously leaving a group is also
3516 * allowed only as long as there is an outstanding refcnt held
3517 * by a previous anonymous request, or else ENOENT (even if the
3518 * link-layer multicast membership exists for a network-layer
3519 * membership.)
3520 */
1c79356b 3521 if (cmd == SIOCADDMULTI) {
6d2010ae 3522 error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
1c79356b
A
3523 ev_msg.event_code = KEV_DL_ADDMULTI;
3524 } else {
6d2010ae 3525 error = if_delmulti_anon(ifp, &ifr->ifr_addr);
1c79356b
A
3526 ev_msg.event_code = KEV_DL_DELMULTI;
3527 }
0a7de745 3528 if (error != 0) {
6d2010ae 3529 break;
0a7de745 3530 }
6d2010ae
A
3531
3532 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3533 ev_msg.kev_class = KEV_NETWORK_CLASS;
3534 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3535 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3536
3537 ev_data.if_family = ifp->if_family;
3538 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3539 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3540 ev_msg.dv[0].data_ptr = &ev_data;
3541 ev_msg.dv[1].data_length = 0;
39037602 3542 dlil_post_complete_msg(ifp, &ev_msg);
6d2010ae
A
3543
3544 ifnet_touch_lastchange(ifp);
3545 break;
1c79356b 3546
9bccf70c 3547 case SIOCSIFMEDIA:
0a7de745
A
3548 error = proc_suser(p);
3549 if (error != 0) {
3550 break;
3551 }
3552 /*
3553 * Silently ignore setting IFM_OTHER
3554 */
3555 if (ifr->ifr_media == IFM_OTHER) {
3556 os_log_info(OS_LOG_DEFAULT,
3557 "%s: %s SIOCSIFMEDIA ignore IFM_OTHER",
3558 __func__, ifp->if_xname);
3559 error = 0;
3560 break;
3561 }
3562 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3563 if (error != 0) {
3564 break;
3565 }
3566 ifnet_touch_lastchange(ifp);
3567 break;
3568
3569 case SIOCDIFPHYADDR:
1c79356b 3570 case SIOCSIFGENERIC:
9bccf70c 3571 case SIOCSIFLLADDR:
91447636
A
3572 case SIOCSIFALTMTU:
3573 case SIOCSIFVLAN:
3574 case SIOCSIFBOND:
cb323159 3575 case SIOCSIF6LOWPAN:
91447636 3576 error = proc_suser(p);
0a7de745 3577 if (error != 0) {
6d2010ae 3578 break;
0a7de745 3579 }
1c79356b 3580
39236c6e 3581 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
0a7de745 3582 if (error != 0) {
6d2010ae 3583 break;
0a7de745 3584 }
1c79356b 3585
6d2010ae
A
3586 ifnet_touch_lastchange(ifp);
3587 break;
1c79356b 3588
39236c6e
A
3589 case SIOCGIFLLADDR: {
3590 struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
3591
3592 if (sdl->sdl_alen == 0) {
3593 error = EADDRNOTAVAIL;
3594 break;
3595 }
3596 /* If larger than 14-bytes we'll need another mechanism */
0a7de745 3597 if (sdl->sdl_alen > sizeof(ifr->ifr_addr.sa_data)) {
39236c6e
A
3598 error = EMSGSIZE;
3599 break;
3600 }
3601 /* Follow the same convention used by SIOCSIFLLADDR */
0a7de745 3602 bzero(&ifr->ifr_addr, sizeof(ifr->ifr_addr));
39236c6e
A
3603 ifr->ifr_addr.sa_family = AF_LINK;
3604 ifr->ifr_addr.sa_len = sdl->sdl_alen;
3605 error = ifnet_guarded_lladdr_copy_bytes(ifp,
3606 &ifr->ifr_addr.sa_data, sdl->sdl_alen);
3607 break;
3608 }
3609
3610 case SIOCGIFTYPE:
3611 ifr->ifr_type.ift_type = ifp->if_type;
3612 ifr->ifr_type.ift_family = ifp->if_family;
3613 ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
3614 break;
3615
3e170ce0 3616 case SIOCGIFFUNCTIONALTYPE:
39037602 3617 ifr->ifr_functional_type = if_functional_type(ifp, FALSE);
3e170ce0
A
3618 break;
3619
9bccf70c
A
3620 case SIOCGIFPSRCADDR:
3621 case SIOCGIFPDSTADDR:
1c79356b 3622 case SIOCGIFGENERIC:
91447636 3623 case SIOCGIFDEVMTU:
91447636
A
3624 case SIOCGIFVLAN:
3625 case SIOCGIFBOND:
cb323159 3626 case SIOCGIF6LOWPAN:
39236c6e
A
3627 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3628 break;
3629
3630 case SIOCGIFWAKEFLAGS:
3631 ifnet_lock_shared(ifp);
3632 ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
3633 ifnet_lock_done(ifp);
3634 break;
3635
3636 case SIOCGIFGETRTREFCNT:
3637 ifnet_lock_shared(ifp);
3638 ifr->ifr_route_refcnt = ifp->if_route_refcnt;
3639 ifnet_lock_done(ifp);
6d2010ae 3640 break;
1c79356b 3641
316670eb
A
3642 case SIOCSIFOPPORTUNISTIC:
3643 case SIOCGIFOPPORTUNISTIC:
3644 error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
b0d623f7 3645 break;
d1ecb069 3646
39236c6e
A
3647 case SIOCGIFLINKQUALITYMETRIC:
3648 ifnet_lock_shared(ifp);
39037602 3649 if ((ifp->if_interface_state.valid_bitmask &
5ba3f43e 3650 IF_INTERFACE_STATE_LQM_STATE_VALID)) {
3e170ce0 3651 ifr->ifr_link_quality_metric =
39037602 3652 ifp->if_interface_state.lqm_state;
5ba3f43e 3653 } else if (IF_FULLY_ATTACHED(ifp)) {
3e170ce0
A
3654 ifr->ifr_link_quality_metric =
3655 IFNET_LQM_THRESH_UNKNOWN;
3656 } else {
3657 ifr->ifr_link_quality_metric =
3658 IFNET_LQM_THRESH_OFF;
3659 }
39236c6e
A
3660 ifnet_lock_done(ifp);
3661 break;
3662
3663 case SIOCSIFLOG:
3664 case SIOCGIFLOG:
3665 error = ifnet_getset_log(ifp, cmd, ifr, p);
3666 break;
3667
3668 case SIOCGIFDELEGATE:
3669 ifnet_lock_shared(ifp);
3670 ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
3671 ifp->if_delegated.ifp->if_index : 0);
3672 ifnet_lock_done(ifp);
3673 break;
3674
fe8ab488
A
3675 case SIOCGIFEXPENSIVE:
3676 ifnet_lock_shared(ifp);
0a7de745 3677 if (ifp->if_eflags & IFEF_EXPENSIVE) {
fe8ab488 3678 ifr->ifr_expensive = 1;
0a7de745 3679 } else {
fe8ab488 3680 ifr->ifr_expensive = 0;
0a7de745 3681 }
fe8ab488
A
3682 ifnet_lock_done(ifp);
3683 break;
3684
3685 case SIOCSIFEXPENSIVE:
3686 {
3687 struct ifnet *difp;
3688
3689 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3690 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3691 return error;
3692 }
0a7de745 3693 if (ifr->ifr_expensive) {
f427ee49 3694 if_set_eflags(ifp, IFEF_EXPENSIVE);
0a7de745 3695 } else {
f427ee49 3696 if_clear_eflags(ifp, IFEF_EXPENSIVE);
0a7de745 3697 }
cb323159 3698 ifnet_increment_generation(ifp);
f427ee49 3699
fe8ab488
A
3700 /*
3701 * Update the expensive bit in the delegated interface
3702 * structure.
3703 */
3704 ifnet_head_lock_shared();
3705 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3706 ifnet_lock_exclusive(difp);
3707 if (difp->if_delegated.ifp == ifp) {
39037602 3708 difp->if_delegated.expensive =
fe8ab488 3709 ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
cb323159 3710 ifnet_increment_generation(difp);
fe8ab488
A
3711 }
3712 ifnet_lock_done(difp);
3713 }
3714 ifnet_head_done();
cb323159
A
3715 necp_update_all_clients();
3716 break;
3717 }
3718
3719 case SIOCGIFCONSTRAINED:
f427ee49 3720 if ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) {
cb323159
A
3721 ifr->ifr_constrained = 1;
3722 } else {
3723 ifr->ifr_constrained = 0;
3724 }
cb323159
A
3725 break;
3726
3727 case SIOCSIFCONSTRAINED:
3728 {
3729 struct ifnet *difp;
3730
3731 if ((error = priv_check_cred(kauth_cred_get(),
3732 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3733 return error;
3734 }
cb323159 3735 if (ifr->ifr_constrained) {
f427ee49 3736 if_set_xflags(ifp, IFXF_CONSTRAINED);
cb323159 3737 } else {
f427ee49 3738 if_clear_xflags(ifp, IFXF_CONSTRAINED);
cb323159
A
3739 }
3740 ifnet_increment_generation(ifp);
cb323159
A
3741 /*
3742 * Update the constrained bit in the delegated interface
3743 * structure.
3744 */
3745 ifnet_head_lock_shared();
3746 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3747 ifnet_lock_exclusive(difp);
3748 if (difp->if_delegated.ifp == ifp) {
3749 difp->if_delegated.constrained =
f427ee49 3750 ((ifp->if_xflags & IFXF_CONSTRAINED) != 0) ? 1 : 0;
cb323159
A
3751 ifnet_increment_generation(difp);
3752 }
3753 ifnet_lock_done(difp);
3754 }
3755 ifnet_head_done();
3756 necp_update_all_clients();
fe8ab488
A
3757 break;
3758 }
3759
3760 case SIOCGIF2KCL:
3761 ifnet_lock_shared(ifp);
0a7de745 3762 if (ifp->if_eflags & IFEF_2KCL) {
fe8ab488 3763 ifr->ifr_2kcl = 1;
0a7de745 3764 } else {
fe8ab488 3765 ifr->ifr_2kcl = 0;
0a7de745 3766 }
fe8ab488
A
3767 ifnet_lock_done(ifp);
3768 break;
3769
3770 case SIOCSIF2KCL:
3771 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3772 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3773 return error;
3774 }
0a7de745 3775 if (ifr->ifr_2kcl) {
f427ee49 3776 if_set_eflags(ifp, IFEF_2KCL);
0a7de745 3777 } else {
f427ee49 3778 if_clear_eflags(ifp, IFEF_2KCL);
0a7de745 3779 }
fe8ab488 3780 break;
3e170ce0
A
3781 case SIOCGSTARTDELAY:
3782 ifnet_lock_shared(ifp);
3783 if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
3784 ifr->ifr_start_delay_qlen =
3785 ifp->if_start_delay_qlen;
3786 ifr->ifr_start_delay_timeout =
3787 ifp->if_start_delay_timeout;
3788 } else {
3789 ifr->ifr_start_delay_qlen = 0;
3790 ifr->ifr_start_delay_timeout = 0;
3791 }
3792 ifnet_lock_done(ifp);
3793 break;
316670eb
A
3794 case SIOCSIFDSTADDR:
3795 case SIOCSIFADDR:
3796 case SIOCSIFBRDADDR:
3797 case SIOCSIFNETMASK:
3798 case OSIOCGIFADDR:
3799 case OSIOCGIFDSTADDR:
3800 case OSIOCGIFBRDADDR:
3801 case OSIOCGIFNETMASK:
3802 case SIOCSIFKPI:
3803 VERIFY(so->so_proto != NULL);
3804
3805 if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
3806 cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
1c79356b
A
3807#if BYTE_ORDER != BIG_ENDIAN
3808 if (ifr->ifr_addr.sa_family == 0 &&
3809 ifr->ifr_addr.sa_len < 16) {
3810 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
3811 ifr->ifr_addr.sa_len = 16;
3812 }
3813#else
0a7de745 3814 if (ifr->ifr_addr.sa_len == 0) {
1c79356b 3815 ifr->ifr_addr.sa_len = 16;
0a7de745 3816 }
1c79356b 3817#endif
316670eb 3818 } else if (cmd == OSIOCGIFADDR) {
0a7de745 3819 cmd = SIOCGIFADDR; /* struct ifreq */
316670eb 3820 } else if (cmd == OSIOCGIFDSTADDR) {
0a7de745 3821 cmd = SIOCGIFDSTADDR; /* struct ifreq */
316670eb 3822 } else if (cmd == OSIOCGIFBRDADDR) {
0a7de745 3823 cmd = SIOCGIFBRDADDR; /* struct ifreq */
316670eb 3824 } else if (cmd == OSIOCGIFNETMASK) {
0a7de745 3825 cmd = SIOCGIFNETMASK; /* struct ifreq */
1c79356b 3826 }
6d2010ae 3827
91447636 3828 socket_lock(so, 1);
6d2010ae 3829 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
316670eb 3830 (caddr_t)ifr, ifp, p));
91447636 3831 socket_unlock(so, 1);
1c79356b 3832
6d2010ae 3833 switch (ocmd) {
1c79356b
A
3834 case OSIOCGIFADDR:
3835 case OSIOCGIFDSTADDR:
3836 case OSIOCGIFBRDADDR:
3837 case OSIOCGIFNETMASK:
316670eb 3838 bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
0a7de745 3839 sizeof(u_short));
1c79356b 3840 }
316670eb 3841
2d21ac55
A
3842 if (cmd == SIOCSIFKPI) {
3843 int temperr = proc_suser(p);
0a7de745 3844 if (temperr != 0) {
2d21ac55 3845 error = temperr;
0a7de745 3846 }
2d21ac55 3847 }
0a7de745
A
3848 // Don't allow to call SIOCSIFADDR and SIOCSIFDSTADDR
3849 // with ifreq as the code expects ifaddr
3850 if ((error == EOPNOTSUPP || error == ENOTSUP) &&
3851 !(cmd == SIOCSIFADDR || cmd == SIOCSIFDSTADDR)) {
39236c6e 3852 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
316670eb
A
3853 (caddr_t)ifr);
3854 }
6d2010ae 3855 break;
316670eb 3856
3e170ce0
A
3857 case SIOCGIFINTERFACESTATE:
3858 if_get_state(ifp, &ifr->ifr_interface_state);
3e170ce0 3859 break;
f427ee49 3860
3e170ce0
A
3861 case SIOCSIFINTERFACESTATE:
3862 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3863 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3864 return error;
3865 }
3e170ce0
A
3866
3867 error = if_state_update(ifp, &ifr->ifr_interface_state);
3868
3869 break;
3870 case SIOCSIFPROBECONNECTIVITY:
3871 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3872 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3873 return error;
3874 }
3e170ce0
A
3875 error = if_probe_connectivity(ifp,
3876 ifr->ifr_probe_connectivity);
3877 break;
3878 case SIOCGIFPROBECONNECTIVITY:
3879 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3880 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3881 return error;
3882 }
3883 if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY) {
3e170ce0 3884 ifr->ifr_probe_connectivity = 1;
0a7de745 3885 } else {
3e170ce0 3886 ifr->ifr_probe_connectivity = 0;
0a7de745 3887 }
3e170ce0 3888 break;
4bd07ac2 3889 case SIOCGECNMODE:
0a7de745
A
3890 if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3891 IFEF_ECN_ENABLE) {
4bd07ac2 3892 ifr->ifr_ecn_mode = IFRTYPE_ECN_ENABLE;
0a7de745
A
3893 } else if ((ifp->if_eflags & (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE)) ==
3894 IFEF_ECN_DISABLE) {
4bd07ac2 3895 ifr->ifr_ecn_mode = IFRTYPE_ECN_DISABLE;
0a7de745 3896 } else {
4bd07ac2 3897 ifr->ifr_ecn_mode = IFRTYPE_ECN_DEFAULT;
0a7de745 3898 }
4bd07ac2
A
3899 break;
3900 case SIOCSECNMODE:
39037602 3901 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3902 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3903 return error;
3904 }
4bd07ac2 3905 if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DEFAULT) {
f427ee49 3906 if_clear_eflags(ifp, IFEF_ECN_ENABLE | IFEF_ECN_DISABLE);
4bd07ac2 3907 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_ENABLE) {
f427ee49
A
3908 if_set_eflags(ifp, IFEF_ECN_ENABLE);
3909 if_clear_eflags(ifp, IFEF_ECN_DISABLE);
4bd07ac2 3910 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DISABLE) {
f427ee49
A
3911 if_set_eflags(ifp, IFEF_ECN_DISABLE);
3912 if_clear_eflags(ifp, IFEF_ECN_ENABLE);
0a7de745 3913 } else {
4bd07ac2 3914 error = EINVAL;
0a7de745 3915 }
4bd07ac2 3916 break;
cb323159 3917
39037602
A
3918 case SIOCSIFTIMESTAMPENABLE:
3919 case SIOCSIFTIMESTAMPDISABLE:
3920 error = proc_suser(p);
0a7de745 3921 if (error != 0) {
39037602 3922 break;
0a7de745 3923 }
39037602 3924
39037602
A
3925 if ((cmd == SIOCSIFTIMESTAMPENABLE &&
3926 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) ||
3927 (cmd == SIOCSIFTIMESTAMPDISABLE &&
3928 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) == 0)) {
39037602
A
3929 break;
3930 }
0a7de745 3931 if (cmd == SIOCSIFTIMESTAMPENABLE) {
f427ee49 3932 if_set_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
0a7de745 3933 } else {
f427ee49 3934 if_clear_xflags(ifp, IFXF_TIMESTAMP_ENABLED);
0a7de745 3935 }
39037602
A
3936 /*
3937 * Pass the setting to the interface if it supports either
3938 * software or hardware time stamping
3939 */
3940 if (ifp->if_capabilities & (IFCAP_HW_TIMESTAMP |
3941 IFCAP_SW_TIMESTAMP)) {
3942 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3943 (caddr_t)ifr);
3944 }
3945 break;
3946 case SIOCGIFTIMESTAMPENABLED: {
0a7de745 3947 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) {
39037602 3948 ifr->ifr_intval = 1;
0a7de745 3949 } else {
39037602 3950 ifr->ifr_intval = 0;
0a7de745 3951 }
39037602
A
3952 break;
3953 }
3954 case SIOCSQOSMARKINGMODE:
3955 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3956 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3957 return error;
3958 }
39037602
A
3959 error = if_set_qosmarking_mode(ifp, ifr->ifr_qosmarking_mode);
3960 break;
3961
3962 case SIOCGQOSMARKINGMODE:
3963 ifr->ifr_qosmarking_mode = ifp->if_qosmarking_mode;
3964 break;
3965
3966 case SIOCSQOSMARKINGENABLED:
3967 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
3968 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
3969 return error;
3970 }
3971 if (ifr->ifr_qosmarking_enabled != 0) {
f427ee49 3972 if_set_eflags(ifp, IFEF_QOSMARKING_ENABLED);
0a7de745 3973 } else {
f427ee49 3974 if_clear_eflags(ifp, IFEF_QOSMARKING_ENABLED);
0a7de745 3975 }
39037602
A
3976 break;
3977
3978 case SIOCGQOSMARKINGENABLED:
3979 ifr->ifr_qosmarking_enabled =
f427ee49 3980 ((ifp->if_eflags & IFEF_QOSMARKING_ENABLED) != 0) ? 1 : 0;
39037602
A
3981 break;
3982
3983 case SIOCSIFDISABLEOUTPUT:
3984#if (DEBUG || DEVELOPMENT)
3985 if (ifr->ifr_disable_output == 1) {
3986 error = ifnet_disable_output(ifp);
3987 } else if (ifr->ifr_disable_output == 0) {
3988 error = ifnet_enable_output(ifp);
3989 } else {
3990 error = EINVAL;
3991 }
3992#else
3993 error = EINVAL;
3994#endif /* (DEBUG || DEVELOPMENT) */
3995 break;
cb323159
A
3996
3997 case SIOCSIFSUBFAMILY:
3998 if ((error = priv_check_cred(kauth_cred_get(),
3999 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4000 return error;
4001 }
4002 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
4003 break;
4004
5ba3f43e
A
4005 case SIOCSIFLOWINTERNET:
4006 if ((error = priv_check_cred(kauth_cred_get(),
0a7de745
A
4007 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4008 return error;
4009 }
5ba3f43e 4010
0a7de745 4011 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_UL) {
f427ee49 4012 if_set_xflags(ifp, IFXF_LOW_INTERNET_UL);
0a7de745 4013 } else {
f427ee49 4014 if_clear_xflags(ifp, IFXF_LOW_INTERNET_UL);
0a7de745
A
4015 }
4016 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_DL) {
f427ee49 4017 if_set_xflags(ifp, IFXF_LOW_INTERNET_DL);
0a7de745 4018 } else {
f427ee49 4019 if_clear_xflags(ifp, IFXF_LOW_INTERNET_DL);
0a7de745 4020 }
5ba3f43e
A
4021 break;
4022 case SIOCGIFLOWINTERNET:
4023 ifnet_lock_shared(ifp);
4024 ifr->ifr_low_internet = 0;
f427ee49 4025 if ((ifp->if_xflags & IFXF_LOW_INTERNET_UL) != 0) {
5ba3f43e
A
4026 ifr->ifr_low_internet |=
4027 IFRTYPE_LOW_INTERNET_ENABLE_UL;
0a7de745 4028 }
f427ee49 4029 if ((ifp->if_xflags & IFXF_LOW_INTERNET_DL) != 0) {
5ba3f43e
A
4030 ifr->ifr_low_internet |=
4031 IFRTYPE_LOW_INTERNET_ENABLE_DL;
0a7de745 4032 }
5ba3f43e
A
4033 ifnet_lock_done(ifp);
4034 break;
d9a64523
A
4035 case SIOCGIFLOWPOWER:
4036 ifr->ifr_low_power_mode =
f427ee49 4037 ((ifp->if_xflags & IFXF_LOW_POWER) != 0);
d9a64523
A
4038 break;
4039 case SIOCSIFLOWPOWER:
4040#if (DEVELOPMENT || DEBUG)
f427ee49 4041 error = if_set_low_power(ifp, (ifr->ifr_low_power_mode != 0));
d9a64523
A
4042#else /* DEVELOPMENT || DEBUG */
4043 error = EOPNOTSUPP;
4044#endif /* DEVELOPMENT || DEBUG */
4045 break;
cb323159
A
4046
4047 case SIOCGIFMPKLOG:
f427ee49 4048 ifr->ifr_mpk_log = ((ifp->if_xflags & IFXF_MPK_LOG) != 0);
cb323159
A
4049 break;
4050 case SIOCSIFMPKLOG:
4051 if (ifr->ifr_mpk_log) {
f427ee49 4052 if_set_xflags(ifp, IFXF_MPK_LOG);
cb323159 4053 } else {
f427ee49 4054 if_clear_xflags(ifp, IFXF_MPK_LOG);
cb323159
A
4055 }
4056 break;
4057 case SIOCGIFNOACKPRIO:
f427ee49 4058 if ((ifp->if_eflags & IFEF_NOACKPRI) != 0) {
cb323159
A
4059 ifr->ifr_noack_prio = 1;
4060 } else {
4061 ifr->ifr_noack_prio = 0;
4062 }
cb323159
A
4063 break;
4064
4065 case SIOCSIFNOACKPRIO:
4066 if ((error = priv_check_cred(kauth_cred_get(),
4067 PRIV_NET_INTERFACE_CONTROL, 0)) != 0) {
4068 return error;
4069 }
cb323159 4070 if (ifr->ifr_noack_prio) {
f427ee49 4071 if_set_eflags(ifp, IFEF_NOACKPRI);
cb323159 4072 } else {
f427ee49 4073 if_clear_eflags(ifp, IFEF_NOACKPRI);
cb323159 4074 }
cb323159
A
4075 break;
4076
316670eb
A
4077 default:
4078 VERIFY(0);
4079 /* NOTREACHED */
1c79356b 4080 }
316670eb 4081
0a7de745 4082 return error;
1c79356b
A
4083}
4084
91447636 4085int
2d21ac55 4086ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
91447636
A
4087{
4088 int error;
4089
4090 socket_unlock(so, 0);
4091 error = ifioctl(so, cmd, data, p);
4092 socket_lock(so, 0);
0a7de745 4093 return error;
91447636 4094}
39037602 4095
1c79356b
A
4096/*
4097 * Set/clear promiscuous mode on interface ifp based on the truth value
4098 * of pswitch. The calls are reference counted so that only the first
4099 * "on" request actually has an effect, as does the final "off" request.
4100 * Results are undefined if the "off" and "on" requests are not matched.
4101 */
91447636
A
4102errno_t
4103ifnet_set_promiscuous(
0a7de745 4104 ifnet_t ifp,
91447636 4105 int pswitch)
1c79356b 4106{
91447636 4107 int error = 0;
6d2010ae
A
4108 int oldflags = 0;
4109 int newflags = 0;
1c79356b 4110
91447636 4111 ifnet_lock_exclusive(ifp);
9bccf70c 4112 oldflags = ifp->if_flags;
6d2010ae 4113 ifp->if_pcount += pswitch ? 1 : -1;
39037602 4114
0a7de745 4115 if (ifp->if_pcount > 0) {
1c79356b 4116 ifp->if_flags |= IFF_PROMISC;
0a7de745 4117 } else {
1c79356b 4118 ifp->if_flags &= ~IFF_PROMISC;
0a7de745 4119 }
39037602 4120
6d2010ae 4121 newflags = ifp->if_flags;
91447636 4122 ifnet_lock_done(ifp);
39037602 4123
6d2010ae
A
4124 if (newflags != oldflags && (newflags & IFF_UP) != 0) {
4125 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
4126 if (error == 0) {
4127 rt_ifmsg(ifp);
4128 } else {
4129 ifnet_lock_exclusive(ifp);
4130 // revert the flags
4131 ifp->if_pcount -= pswitch ? 1 : -1;
0a7de745
A
4132 if (ifp->if_pcount > 0) {
4133 ifp->if_flags |= IFF_PROMISC;
4134 } else {
4135 ifp->if_flags &= ~IFF_PROMISC;
4136 }
6d2010ae
A
4137 ifnet_lock_done(ifp);
4138 }
4139 }
39037602 4140
6d2010ae 4141 if (newflags != oldflags) {
39236c6e
A
4142 log(LOG_INFO, "%s: promiscuous mode %s%s\n",
4143 if_name(ifp),
6d2010ae
A
4144 (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
4145 error != 0 ? " failed" : " succeeded");
91447636 4146 }
0a7de745 4147 return error;
1c79356b
A
4148}
4149
4150/*
4151 * Return interface configuration
4152 * of system. List may be used
4153 * in later ioctl's (above) to get
4154 * other information.
4155 */
4156/*ARGSUSED*/
4157static int
39236c6e 4158ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
1c79356b 4159{
91447636
A
4160 struct ifnet *ifp = NULL;
4161 struct ifaddr *ifa;
4162 struct ifreq ifr;
4163 int error = 0;
4164 size_t space;
39236c6e
A
4165 net_thread_marks_t marks;
4166
4167 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
6d2010ae 4168
3a60a9f5
A
4169 /*
4170 * Zero the ifr buffer to make sure we don't
4171 * disclose the contents of the stack.
4172 */
0a7de745 4173 bzero(&ifr, sizeof(struct ifreq));
3a60a9f5 4174
91447636
A
4175 space = *ret_space;
4176 ifnet_head_lock_shared();
0a7de745 4177 for (ifp = ifnet_head.tqh_first; space > sizeof(ifr) &&
6d2010ae 4178 ifp; ifp = ifp->if_link.tqe_next) {
1c79356b 4179 char workbuf[64];
91447636 4180 size_t ifnlen, addrs;
1c79356b 4181
0a7de745 4182 ifnlen = snprintf(workbuf, sizeof(workbuf),
39236c6e 4183 "%s", if_name(ifp));
0a7de745 4184 if (ifnlen + 1 > sizeof(ifr.ifr_name)) {
1c79356b 4185 error = ENAMETOOLONG;
9bccf70c 4186 break;
1c79356b 4187 } else {
2d21ac55 4188 strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
1c79356b 4189 }
6d2010ae 4190
91447636 4191 ifnet_lock_shared(ifp);
1c79356b 4192
9bccf70c
A
4193 addrs = 0;
4194 ifa = ifp->if_addrhead.tqh_first;
0a7de745 4195 for (; space > sizeof(ifr) && ifa;
9bccf70c 4196 ifa = ifa->ifa_link.tqe_next) {
6d2010ae 4197 struct sockaddr *sa;
39236c6e
A
4198 union {
4199 struct sockaddr sa;
4200 struct sockaddr_dl sdl;
4201 uint8_t buf[SOCK_MAXADDRLEN + 1];
4202 } u;
4203
4204 /*
4205 * Make sure to accomodate the largest possible
4206 * size of SA(if_lladdr)->sa_len.
4207 */
0a7de745 4208 _CASSERT(sizeof(u) == (SOCK_MAXADDRLEN + 1));
6d2010ae
A
4209
4210 IFA_LOCK(ifa);
4211 sa = ifa->ifa_addr;
39236c6e
A
4212 addrs++;
4213
4214 if (ifa == ifp->if_lladdr) {
4215 VERIFY(sa->sa_family == AF_LINK);
4216 bcopy(sa, &u, sa->sa_len);
6d2010ae 4217 IFA_UNLOCK(ifa);
39236c6e
A
4218 ifnet_guarded_lladdr_copy_bytes(ifp,
4219 LLADDR(&u.sdl), u.sdl.sdl_alen);
4220 IFA_LOCK(ifa);
4221 sa = &u.sa;
6d2010ae 4222 }
39236c6e 4223
b0d623f7 4224 if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
1c79356b 4225 struct osockaddr *osa =
316670eb 4226 (struct osockaddr *)(void *)&ifr.ifr_addr;
1c79356b
A
4227 ifr.ifr_addr = *sa;
4228 osa->sa_family = sa->sa_family;
6d2010ae 4229 error = copyout((caddr_t)&ifr, ifrp,
0a7de745
A
4230 sizeof(ifr));
4231 ifrp += sizeof(struct ifreq);
4232 } else if (sa->sa_len <= sizeof(*sa)) {
1c79356b 4233 ifr.ifr_addr = *sa;
6d2010ae 4234 error = copyout((caddr_t)&ifr, ifrp,
0a7de745
A
4235 sizeof(ifr));
4236 ifrp += sizeof(struct ifreq);
1c79356b 4237 } else {
6d2010ae 4238 if (space <
0a7de745 4239 sizeof(ifr) + sa->sa_len - sizeof(*sa)) {
6d2010ae 4240 IFA_UNLOCK(ifa);
1c79356b 4241 break;
6d2010ae 4242 }
0a7de745 4243 space -= sa->sa_len - sizeof(*sa);
6d2010ae 4244 error = copyout((caddr_t)&ifr, ifrp,
0a7de745 4245 sizeof(ifr.ifr_name));
91447636 4246 if (error == 0) {
39236c6e
A
4247 error = copyout((caddr_t)sa, (ifrp +
4248 offsetof(struct ifreq, ifr_addr)),
4249 sa->sa_len);
91447636 4250 }
6d2010ae
A
4251 ifrp += (sa->sa_len + offsetof(struct ifreq,
4252 ifr_addr));
1c79356b 4253 }
6d2010ae 4254 IFA_UNLOCK(ifa);
0a7de745 4255 if (error) {
1c79356b 4256 break;
0a7de745
A
4257 }
4258 space -= sizeof(ifr);
1c79356b 4259 }
91447636 4260 ifnet_lock_done(ifp);
6d2010ae 4261
0a7de745 4262 if (error) {
9bccf70c 4263 break;
0a7de745 4264 }
9bccf70c 4265 if (!addrs) {
0a7de745
A
4266 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
4267 error = copyout((caddr_t)&ifr, ifrp, sizeof(ifr));
4268 if (error) {
9bccf70c 4269 break;
0a7de745
A
4270 }
4271 space -= sizeof(ifr);
4272 ifrp += sizeof(struct ifreq);
9bccf70c 4273 }
1c79356b 4274 }
91447636
A
4275 ifnet_head_done();
4276 *ret_space -= space;
39236c6e 4277 net_thread_marks_pop(marks);
0a7de745 4278 return error;
1c79356b
A
4279}
4280
4281/*
4282 * Just like if_promisc(), but for all-multicast-reception mode.
4283 */
4284int
2d21ac55 4285if_allmulti(struct ifnet *ifp, int onswitch)
1c79356b
A
4286{
4287 int error = 0;
0a7de745 4288 int modified = 0;
316670eb 4289
91447636 4290 ifnet_lock_exclusive(ifp);
1c79356b
A
4291
4292 if (onswitch) {
4293 if (ifp->if_amcount++ == 0) {
4294 ifp->if_flags |= IFF_ALLMULTI;
91447636 4295 modified = 1;
1c79356b
A
4296 }
4297 } else {
4298 if (ifp->if_amcount > 1) {
4299 ifp->if_amcount--;
4300 } else {
4301 ifp->if_amcount = 0;
4302 ifp->if_flags &= ~IFF_ALLMULTI;
91447636 4303 modified = 1;
1c79356b
A
4304 }
4305 }
91447636 4306 ifnet_lock_done(ifp);
316670eb 4307
0a7de745 4308 if (modified) {
2d21ac55 4309 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
0a7de745 4310 }
1c79356b 4311
0a7de745 4312 if (error == 0) {
1c79356b 4313 rt_ifmsg(ifp);
0a7de745
A
4314 }
4315 return error;
1c79356b
A
4316}
4317
6d2010ae
A
4318static struct ifmultiaddr *
4319ifma_alloc(int how)
4320{
4321 struct ifmultiaddr *ifma;
4322
4323 ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
4324 zalloc_noblock(ifma_zone);
4325
4326 if (ifma != NULL) {
4327 bzero(ifma, ifma_size);
4328 lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
4329 ifma->ifma_debug |= IFD_ALLOC;
4330 if (ifma_debug != 0) {
4331 ifma->ifma_debug |= IFD_DEBUG;
4332 ifma->ifma_trace = ifma_trace;
4333 }
4334 }
0a7de745 4335 return ifma;
6d2010ae
A
4336}
4337
4338static void
4339ifma_free(struct ifmultiaddr *ifma)
4340{
4341 IFMA_LOCK(ifma);
4342
4343 if (ifma->ifma_protospec != NULL) {
4344 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4345 /* NOTREACHED */
4346 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4347 ifma->ifma_anoncnt != 0) {
4348 panic("%s: Freeing ifma=%p with outstanding anon req",
4349 __func__, ifma);
4350 /* NOTREACHED */
4351 } else if (ifma->ifma_debug & IFD_ATTACHED) {
4352 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
4353 __func__, ifma, ifma->ifma_ifp);
4354 /* NOTREACHED */
4355 } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
4356 panic("%s: ifma %p cannot be freed", __func__, ifma);
4357 /* NOTREACHED */
4358 } else if (ifma->ifma_refcount != 0) {
4359 panic("%s: non-zero refcount ifma=%p", __func__, ifma);
4360 /* NOTREACHED */
4361 } else if (ifma->ifma_reqcnt != 0) {
4362 panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
4363 /* NOTREACHED */
4364 } else if (ifma->ifma_ifp != NULL) {
4365 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
4366 ifma->ifma_ifp, ifma);
4367 /* NOTREACHED */
4368 } else if (ifma->ifma_ll != NULL) {
4369 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
4370 ifma->ifma_ll, ifma);
4371 /* NOTREACHED */
4372 }
4373 ifma->ifma_debug &= ~IFD_ALLOC;
4374 if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
4375 (IFD_DEBUG | IFD_TRASHED)) {
4376 lck_mtx_lock(&ifma_trash_lock);
4377 TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
4378 ifma_trash_link);
4379 lck_mtx_unlock(&ifma_trash_lock);
4380 ifma->ifma_debug &= ~IFD_TRASHED;
4381 }
4382 IFMA_UNLOCK(ifma);
4383
4384 if (ifma->ifma_addr != NULL) {
4385 FREE(ifma->ifma_addr, M_IFADDR);
4386 ifma->ifma_addr = NULL;
4387 }
4388 lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
4389 zfree(ifma_zone, ifma);
4390}
4391
4392static void
4393ifma_trace(struct ifmultiaddr *ifma, int refhold)
4394{
4395 struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
4396 ctrace_t *tr;
4397 u_int32_t idx;
4398 u_int16_t *cnt;
4399
4400 if (!(ifma->ifma_debug & IFD_DEBUG)) {
4401 panic("%s: ifma %p has no debug structure", __func__, ifma);
4402 /* NOTREACHED */
4403 }
4404 if (refhold) {
4405 cnt = &ifma_dbg->ifma_refhold_cnt;
4406 tr = ifma_dbg->ifma_refhold;
4407 } else {
4408 cnt = &ifma_dbg->ifma_refrele_cnt;
4409 tr = ifma_dbg->ifma_refrele;
4410 }
4411
4412 idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
4413 ctrace_record(&tr[idx]);
4414}
4415
91447636 4416void
6d2010ae 4417ifma_addref(struct ifmultiaddr *ifma, int locked)
1c79356b 4418{
0a7de745 4419 if (!locked) {
6d2010ae 4420 IFMA_LOCK(ifma);
0a7de745 4421 } else {
6d2010ae 4422 IFMA_LOCK_ASSERT_HELD(ifma);
0a7de745 4423 }
6d2010ae
A
4424
4425 if (++ifma->ifma_refcount == 0) {
4426 panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
4427 /* NOTREACHED */
4428 } else if (ifma->ifma_trace != NULL) {
4429 (*ifma->ifma_trace)(ifma, TRUE);
4430 }
0a7de745 4431 if (!locked) {
6d2010ae 4432 IFMA_UNLOCK(ifma);
0a7de745 4433 }
91447636 4434}
1c79356b 4435
91447636 4436void
6d2010ae
A
4437ifma_remref(struct ifmultiaddr *ifma)
4438{
4439 struct ifmultiaddr *ll;
4440
4441 IFMA_LOCK(ifma);
4442
4443 if (ifma->ifma_refcount == 0) {
4444 panic("%s: ifma=%p negative refcnt", __func__, ifma);
4445 /* NOTREACHED */
4446 } else if (ifma->ifma_trace != NULL) {
4447 (*ifma->ifma_trace)(ifma, FALSE);
4448 }
4449
4450 --ifma->ifma_refcount;
4451 if (ifma->ifma_refcount > 0) {
4452 IFMA_UNLOCK(ifma);
4453 return;
91447636 4454 }
6d2010ae
A
4455
4456 ll = ifma->ifma_ll;
4457 ifma->ifma_ifp = NULL;
4458 ifma->ifma_ll = NULL;
4459 IFMA_UNLOCK(ifma);
0a7de745 4460 ifma_free(ifma); /* deallocate it */
6d2010ae 4461
0a7de745 4462 if (ll != NULL) {
6d2010ae 4463 IFMA_REMREF(ll);
0a7de745 4464 }
6d2010ae
A
4465}
4466
4467static void
4468if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4469{
4470 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4471 IFMA_LOCK_ASSERT_HELD(ifma);
4472
4473 if (ifma->ifma_ifp != ifp) {
4474 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
4475 ifma->ifma_ifp, ifp);
4476 /* NOTREACHED */
4477 } else if (ifma->ifma_debug & IFD_ATTACHED) {
4478 panic("%s: Attempt to attach an already attached ifma=%p",
4479 __func__, ifma);
4480 /* NOTREACHED */
4481 } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4482 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
4483 /* NOTREACHED */
4484 } else if (ifma->ifma_debug & IFD_TRASHED) {
4485 panic("%s: Attempt to reattach a detached ifma=%p",
4486 __func__, ifma);
4487 /* NOTREACHED */
4488 }
4489
4490 ifma->ifma_reqcnt++;
4491 VERIFY(ifma->ifma_reqcnt == 1);
4492 IFMA_ADDREF_LOCKED(ifma);
4493 ifma->ifma_debug |= IFD_ATTACHED;
4494 if (anon) {
4495 ifma->ifma_anoncnt++;
4496 VERIFY(ifma->ifma_anoncnt == 1);
4497 ifma->ifma_flags |= IFMAF_ANONYMOUS;
4498 }
4499
4500 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
91447636
A
4501}
4502
91447636 4503static int
6d2010ae
A
4504if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
4505{
4506 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
4507 IFMA_LOCK_ASSERT_HELD(ifma);
4508
4509 if (ifma->ifma_reqcnt == 0) {
4510 panic("%s: ifma=%p negative reqcnt", __func__, ifma);
4511 /* NOTREACHED */
4512 } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4513 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
4514 /* NOTREACHED */
4515 } else if (anon && ifma->ifma_anoncnt == 0) {
4516 panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
4517 /* NOTREACHED */
4518 } else if (ifma->ifma_ifp != ifp) {
4519 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
4520 ifma->ifma_ifp, ifp);
4521 /* NOTREACHED */
4522 }
4523
4524 if (anon) {
4525 --ifma->ifma_anoncnt;
0a7de745
A
4526 if (ifma->ifma_anoncnt > 0) {
4527 return 0;
4528 }
6d2010ae
A
4529 ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
4530 }
4531
4532 --ifma->ifma_reqcnt;
0a7de745
A
4533 if (ifma->ifma_reqcnt > 0) {
4534 return 0;
4535 }
6d2010ae
A
4536
4537 if (ifma->ifma_protospec != NULL) {
4538 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4539 /* NOTREACHED */
4540 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4541 ifma->ifma_anoncnt != 0) {
4542 panic("%s: Detaching ifma=%p with outstanding anon req",
4543 __func__, ifma);
4544 /* NOTREACHED */
4545 } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
4546 panic("%s: Attempt to detach an unattached address ifma=%p",
4547 __func__, ifma);
4548 /* NOTREACHED */
4549 } else if (ifma->ifma_debug & IFD_TRASHED) {
4550 panic("%s: ifma %p is already in trash list", __func__, ifma);
4551 /* NOTREACHED */
4552 }
4553
4554 /*
4555 * NOTE: Caller calls IFMA_REMREF
4556 */
4557 ifma->ifma_debug &= ~IFD_ATTACHED;
4558 LIST_REMOVE(ifma, ifma_link);
0a7de745 4559 if (LIST_EMPTY(&ifp->if_multiaddrs)) {
6d2010ae 4560 ifp->if_updatemcasts = 0;
0a7de745 4561 }
6d2010ae
A
4562
4563 if (ifma->ifma_debug & IFD_DEBUG) {
4564 /* Become a regular mutex, just in case */
4565 IFMA_CONVERT_LOCK(ifma);
4566 lck_mtx_lock(&ifma_trash_lock);
4567 TAILQ_INSERT_TAIL(&ifma_trash_head,
4568 (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
4569 lck_mtx_unlock(&ifma_trash_lock);
4570 ifma->ifma_debug |= IFD_TRASHED;
4571 }
4572
0a7de745 4573 return 1;
6d2010ae
A
4574}
4575
4576/*
39037602 4577 * Find an ifmultiaddr that matches a socket address on an interface.
6d2010ae
A
4578 *
4579 * Caller is responsible for holding the ifnet_lock while calling
4580 * this function.
4581 */
4582static int
4583if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
4584 struct ifmultiaddr **retifma, int anon)
91447636
A
4585{
4586 struct ifmultiaddr *ifma;
6d2010ae
A
4587
4588 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
39037602 4589 ifma = LIST_NEXT(ifma, ifma_link)) {
6d2010ae 4590 IFMA_LOCK_SPIN(ifma);
39037602 4591 if (!ifa_equal(sa, ifma->ifma_addr)) {
6d2010ae
A
4592 IFMA_UNLOCK(ifma);
4593 continue;
4594 }
4595 if (anon) {
4596 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4597 ifma->ifma_anoncnt != 0);
4598 VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4599 ifma->ifma_anoncnt == 0);
4600 ifma->ifma_anoncnt++;
4601 if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4602 VERIFY(ifma->ifma_anoncnt == 1);
4603 ifma->ifma_flags |= IFMAF_ANONYMOUS;
91447636 4604 }
1c79356b 4605 }
6d2010ae
A
4606 if (!anon || ifma->ifma_anoncnt == 1) {
4607 ifma->ifma_reqcnt++;
4608 VERIFY(ifma->ifma_reqcnt > 1);
4609 }
4610 if (retifma != NULL) {
4611 *retifma = ifma;
4612 IFMA_ADDREF_LOCKED(ifma);
4613 }
4614 IFMA_UNLOCK(ifma);
0a7de745 4615 return 0;
1c79356b 4616 }
0a7de745 4617 return ENOENT;
91447636
A
4618}
4619
2d21ac55
A
4620/*
4621 * Radar 3642395, make sure all multicasts are in a standard format.
4622 */
39037602 4623static struct sockaddr *
316670eb 4624copy_and_normalize(const struct sockaddr *original)
2d21ac55 4625{
0a7de745
A
4626 int alen = 0;
4627 const u_char *aptr = NULL;
4628 struct sockaddr *copy = NULL;
4629 struct sockaddr_dl *sdl_new = NULL;
4630 int len = 0;
316670eb 4631
2d21ac55 4632 if (original->sa_family != AF_LINK &&
316670eb 4633 original->sa_family != AF_UNSPEC) {
2d21ac55 4634 /* Just make a copy */
39037602 4635 MALLOC(copy, struct sockaddr *, original->sa_len,
316670eb 4636 M_IFADDR, M_WAITOK);
0a7de745 4637 if (copy != NULL) {
2d21ac55 4638 bcopy(original, copy, original->sa_len);
0a7de745
A
4639 }
4640 return copy;
2d21ac55 4641 }
316670eb 4642
2d21ac55 4643 switch (original->sa_family) {
0a7de745
A
4644 case AF_LINK: {
4645 const struct sockaddr_dl *sdl_original =
4646 (struct sockaddr_dl *)(uintptr_t)(size_t)original;
4647
4648 if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
4649 sdl_original->sdl_slen +
4650 offsetof(struct sockaddr_dl, sdl_data) >
4651 sdl_original->sdl_len) {
4652 return NULL;
2d21ac55 4653 }
316670eb 4654
0a7de745
A
4655 alen = sdl_original->sdl_alen;
4656 aptr = CONST_LLADDR(sdl_original);
4657 }
4658 break;
316670eb 4659
0a7de745
A
4660 case AF_UNSPEC: {
4661 if (original->sa_len < ETHER_ADDR_LEN +
4662 offsetof(struct sockaddr, sa_data)) {
4663 return NULL;
2d21ac55 4664 }
0a7de745
A
4665
4666 alen = ETHER_ADDR_LEN;
4667 aptr = (const u_char *)original->sa_data;
4668 }
4669 break;
2d21ac55 4670 }
316670eb 4671
0a7de745
A
4672 if (alen == 0 || aptr == NULL) {
4673 return NULL;
4674 }
316670eb 4675
2d21ac55 4676 len = alen + offsetof(struct sockaddr_dl, sdl_data);
39037602 4677 MALLOC(sdl_new, struct sockaddr_dl *, len, M_IFADDR, M_WAITOK);
316670eb 4678
2d21ac55
A
4679 if (sdl_new != NULL) {
4680 bzero(sdl_new, len);
4681 sdl_new->sdl_len = len;
4682 sdl_new->sdl_family = AF_LINK;
4683 sdl_new->sdl_alen = alen;
4684 bcopy(aptr, LLADDR(sdl_new), alen);
4685 }
316670eb 4686
0a7de745 4687 return (struct sockaddr *)sdl_new;
2d21ac55
A
4688}
4689
91447636 4690/*
6d2010ae
A
4691 * Network-layer protocol domains which hold references to the underlying
4692 * link-layer record must use this routine.
91447636
A
4693 */
4694int
6d2010ae
A
4695if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
4696 struct ifmultiaddr **retifma)
4697{
0a7de745 4698 return if_addmulti_common(ifp, sa, retifma, 0);
6d2010ae
A
4699}
4700
4701/*
4702 * Anything other than network-layer protocol domains which hold references
4703 * to the underlying link-layer record must use this routine: SIOCADDMULTI
39236c6e 4704 * ioctl, ifnet_add_multicast(), if_bond.
6d2010ae
A
4705 */
4706int
4707if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
4708 struct ifmultiaddr **retifma)
4709{
0a7de745 4710 return if_addmulti_common(ifp, sa, retifma, 1);
6d2010ae
A
4711}
4712
4713/*
4714 * Register an additional multicast address with a network interface.
4715 *
4716 * - If the address is already present, bump the reference count on the
4717 * address and return.
4718 * - If the address is not link-layer, look up a link layer address.
4719 * - Allocate address structures for one or both addresses, and attach to the
4720 * multicast address list on the interface. If automatically adding a link
4721 * layer address, the protocol address will own a reference to the link
4722 * layer address, to be freed when it is freed.
4723 * - Notify the network device driver of an addition to the multicast address
4724 * list.
4725 *
4726 * 'sa' points to caller-owned memory with the desired multicast address.
4727 *
4728 * 'retifma' will be used to return a pointer to the resulting multicast
4729 * address reference, if desired.
4730 *
4731 * 'anon' indicates a link-layer address with no protocol address reference
4732 * made to it. Anything other than network-layer protocol domain requests
4733 * are considered as anonymous.
4734 */
4735static int
4736if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
4737 struct ifmultiaddr **retifma, int anon)
91447636
A
4738{
4739 struct sockaddr_storage storage;
4740 struct sockaddr *llsa = NULL;
2d21ac55 4741 struct sockaddr *dupsa = NULL;
6d2010ae 4742 int error = 0, ll_firstref = 0, lladdr;
2d21ac55 4743 struct ifmultiaddr *ifma = NULL;
91447636 4744 struct ifmultiaddr *llifma = NULL;
6d2010ae
A
4745
4746 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4747 VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
4748 sa->sa_family == AF_LINK);
4749
2d21ac55
A
4750 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4751 if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
4752 dupsa = copy_and_normalize(sa);
4753 if (dupsa == NULL) {
6d2010ae
A
4754 error = ENOMEM;
4755 goto cleanup;
2d21ac55
A
4756 }
4757 sa = dupsa;
4758 }
6d2010ae 4759
91447636 4760 ifnet_lock_exclusive(ifp);
6d2010ae
A
4761 if (!(ifp->if_flags & IFF_MULTICAST)) {
4762 error = EADDRNOTAVAIL;
4763 ifnet_lock_done(ifp);
2d21ac55
A
4764 goto cleanup;
4765 }
1c79356b 4766
6d2010ae
A
4767 /* If the address is already present, return a new reference to it */
4768 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4769 ifnet_lock_done(ifp);
0a7de745 4770 if (error == 0) {
6d2010ae 4771 goto cleanup;
0a7de745 4772 }
6d2010ae 4773
1c79356b 4774 /*
6d2010ae
A
4775 * The address isn't already present; give the link layer a chance
4776 * to accept/reject it, and also find out which AF_LINK address this
4777 * maps to, if it isn't one already.
1c79356b 4778 */
6d2010ae 4779 error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
0a7de745 4780 sizeof(storage));
91447636 4781 if (error == 0 && storage.ss_len != 0) {
6d2010ae 4782 llsa = copy_and_normalize((struct sockaddr *)&storage);
2d21ac55
A
4783 if (llsa == NULL) {
4784 error = ENOMEM;
4785 goto cleanup;
4786 }
6d2010ae
A
4787
4788 llifma = ifma_alloc(M_WAITOK);
2d21ac55
A
4789 if (llifma == NULL) {
4790 error = ENOMEM;
4791 goto cleanup;
4792 }
91447636 4793 }
6d2010ae 4794
9bccf70c 4795 /* to be similar to FreeBSD */
0a7de745 4796 if (error == EOPNOTSUPP) {
9bccf70c 4797 error = 0;
0a7de745 4798 } else if (error != 0) {
2d21ac55 4799 goto cleanup;
0a7de745 4800 }
1c79356b 4801
91447636 4802 /* Allocate while we aren't holding any locks */
2d21ac55
A
4803 if (dupsa == NULL) {
4804 dupsa = copy_and_normalize(sa);
4805 if (dupsa == NULL) {
4806 error = ENOMEM;
4807 goto cleanup;
4808 }
4809 }
6d2010ae 4810 ifma = ifma_alloc(M_WAITOK);
2d21ac55
A
4811 if (ifma == NULL) {
4812 error = ENOMEM;
4813 goto cleanup;
4814 }
6d2010ae 4815
91447636
A
4816 ifnet_lock_exclusive(ifp);
4817 /*
4818 * Check again for the matching multicast.
4819 */
6d2010ae
A
4820 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4821 if (error == 0) {
91447636 4822 ifnet_lock_done(ifp);
2d21ac55 4823 goto cleanup;
91447636 4824 }
1c79356b 4825
6d2010ae 4826 if (llifma != NULL) {
0a7de745 4827 VERIFY(!anon); /* must not get here if "anonymous" */
6d2010ae
A
4828 if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
4829 FREE(llsa, M_IFADDR);
4830 llsa = NULL;
4831 ifma_free(llifma);
4832 llifma = NULL;
4833 VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
1c79356b 4834 } else {
6d2010ae 4835 ll_firstref = 1;
91447636
A
4836 llifma->ifma_addr = llsa;
4837 llifma->ifma_ifp = ifp;
6d2010ae
A
4838 IFMA_LOCK(llifma);
4839 if_attach_ifma(ifp, llifma, 0);
4840 /* add extra refcnt for ifma */
4841 IFMA_ADDREF_LOCKED(llifma);
4842 IFMA_UNLOCK(llifma);
91447636 4843 ifma->ifma_ll = llifma;
1c79356b
A
4844 }
4845 }
6d2010ae
A
4846
4847 /* "anonymous" request should not result in network address */
4848 VERIFY(!anon || ifma->ifma_ll == NULL);
4849
4850 ifma->ifma_addr = dupsa;
4851 ifma->ifma_ifp = ifp;
4852 IFMA_LOCK(ifma);
4853 if_attach_ifma(ifp, ifma, anon);
0a7de745 4854 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
6d2010ae 4855 if (retifma != NULL) {
91447636 4856 *retifma = ifma;
0a7de745 4857 IFMA_ADDREF_LOCKED(*retifma); /* for caller */
91447636 4858 }
6d2010ae
A
4859 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4860 ifma->ifma_addr->sa_family == AF_LINK);
4861 IFMA_UNLOCK(ifma);
91447636 4862 ifnet_lock_done(ifp);
6d2010ae
A
4863
4864 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
0a7de745 4865 IFMA_REMREF(ifma); /* for this routine */
91447636 4866
1c79356b
A
4867 /*
4868 * We are certain we have added something, so call down to the
6d2010ae
A
4869 * interface to let them know about it. Do this only for newly-
4870 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
1c79356b 4871 */
0a7de745 4872 if (lladdr || ll_firstref) {
6d2010ae 4873 (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
0a7de745 4874 }
6d2010ae 4875
0a7de745 4876 if (ifp->if_updatemcasts > 0) {
6d2010ae 4877 ifp->if_updatemcasts = 0;
0a7de745 4878 }
6d2010ae 4879
0a7de745 4880 return 0;
6d2010ae 4881
2d21ac55 4882cleanup:
0a7de745 4883 if (ifma != NULL) {
6d2010ae 4884 ifma_free(ifma);
0a7de745
A
4885 }
4886 if (dupsa != NULL) {
2d21ac55 4887 FREE(dupsa, M_IFADDR);
0a7de745
A
4888 }
4889 if (llifma != NULL) {
6d2010ae 4890 ifma_free(llifma);
0a7de745
A
4891 }
4892 if (llsa != NULL) {
2d21ac55 4893 FREE(llsa, M_IFADDR);
0a7de745 4894 }
6d2010ae 4895
0a7de745 4896 return error;
1c79356b
A
4897}
4898
6d2010ae
A
4899/*
4900 * Delete a multicast group membership by network-layer group address.
4901 * This routine is deprecated.
4902 */
1c79356b 4903int
6d2010ae 4904if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
1c79356b 4905{
0a7de745 4906 return if_delmulti_common(NULL, ifp, sa, 0);
55e303ae 4907}
1c79356b 4908
55e303ae 4909/*
6d2010ae
A
4910 * Delete a multicast group membership by group membership pointer.
4911 * Network-layer protocol domains must use this routine.
55e303ae
A
4912 */
4913int
6d2010ae
A
4914if_delmulti_ifma(struct ifmultiaddr *ifma)
4915{
0a7de745 4916 return if_delmulti_common(ifma, NULL, NULL, 0);
6d2010ae
A
4917}
4918
4919/*
4920 * Anything other than network-layer protocol domains which hold references
4921 * to the underlying link-layer record must use this routine: SIOCDELMULTI
39236c6e 4922 * ioctl, ifnet_remove_multicast(), if_bond.
6d2010ae
A
4923 */
4924int
4925if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
4926{
0a7de745 4927 return if_delmulti_common(NULL, ifp, sa, 1);
6d2010ae
A
4928}
4929
4930/*
4931 * Delete a multicast group membership by network-layer group address.
4932 *
4933 * Returns ENOENT if the entry could not be found.
4934 */
4935static int
4936if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
4937 const struct sockaddr *sa, int anon)
55e303ae 4938{
0a7de745
A
4939 struct sockaddr *dupsa = NULL;
4940 int lastref, ll_lastref = 0, lladdr;
4941 struct ifmultiaddr *ll = NULL;
55e303ae 4942
6d2010ae
A
4943 /* sanity check for callers */
4944 VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
4945
0a7de745 4946 if (ifma != NULL) {
6d2010ae 4947 ifp = ifma->ifma_ifp;
0a7de745 4948 }
6d2010ae
A
4949
4950 if (sa != NULL &&
4951 (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
2d21ac55 4952 dupsa = copy_and_normalize(sa);
0a7de745
A
4953 if (dupsa == NULL) {
4954 return ENOMEM;
4955 }
2d21ac55
A
4956 sa = dupsa;
4957 }
6d2010ae 4958
91447636 4959 ifnet_lock_exclusive(ifp);
6d2010ae
A
4960 if (ifma == NULL) {
4961 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
39037602 4962 ifma = LIST_NEXT(ifma, ifma_link)) {
6d2010ae 4963 IFMA_LOCK(ifma);
39037602 4964 if (!ifa_equal(sa, ifma->ifma_addr) ||
6d2010ae
A
4965 (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
4966 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4967 ifma->ifma_anoncnt != 0);
4968 IFMA_UNLOCK(ifma);
4969 continue;
4970 }
4971 /* found; keep it locked */
55e303ae 4972 break;
6d2010ae
A
4973 }
4974 if (ifma == NULL) {
0a7de745 4975 if (dupsa != NULL) {
6d2010ae 4976 FREE(dupsa, M_IFADDR);
0a7de745 4977 }
6d2010ae 4978 ifnet_lock_done(ifp);
0a7de745 4979 return ENOENT;
6d2010ae
A
4980 }
4981 } else {
4982 IFMA_LOCK(ifma);
4983 }
4984 IFMA_LOCK_ASSERT_HELD(ifma);
0a7de745 4985 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
6d2010ae
A
4986 lastref = if_detach_ifma(ifp, ifma, anon);
4987 VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
4988 ifma->ifma_reqcnt == 0));
4989 VERIFY(!anon || ifma->ifma_ll == NULL);
4990 ll = ifma->ifma_ll;
4991 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4992 ifma->ifma_addr->sa_family == AF_LINK);
4993 IFMA_UNLOCK(ifma);
4994 if (lastref && ll != NULL) {
4995 IFMA_LOCK(ll);
4996 ll_lastref = if_detach_ifma(ifp, ll, 0);
4997 IFMA_UNLOCK(ll);
91447636 4998 }
91447636 4999 ifnet_lock_done(ifp);
6d2010ae 5000
0a7de745 5001 if (lastref) {
6d2010ae 5002 rt_newmaddrmsg(RTM_DELMADDR, ifma);
0a7de745 5003 }
6d2010ae
A
5004
5005 if ((ll == NULL && lastref && lladdr) || ll_lastref) {
5006 /*
5007 * Make sure the interface driver is notified in the
5008 * case of a link layer mcast group being left. Do
5009 * this only for a AF_LINK/AF_UNSPEC address that has
5010 * been removed from the if_multiaddrs set.
5011 */
5012 ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
5013 }
5014
0a7de745
A
5015 if (lastref) {
5016 IFMA_REMREF(ifma); /* for if_multiaddrs list */
5017 }
5018 if (ll_lastref) {
5019 IFMA_REMREF(ll); /* for if_multiaddrs list */
5020 }
5021 IFMA_REMREF(ifma); /* for this routine */
5022 if (dupsa != NULL) {
2d21ac55 5023 FREE(dupsa, M_IFADDR);
0a7de745 5024 }
1c79356b 5025
0a7de745 5026 return 0;
6d2010ae 5027}
9bccf70c 5028
1c79356b
A
5029/*
5030 * Shutdown all network activity. Used boot() when halting
5031 * system.
5032 */
2d21ac55
A
5033int
5034if_down_all(void)
1c79356b 5035{
91447636 5036 struct ifnet **ifp;
0a7de745
A
5037 u_int32_t count;
5038 u_int32_t i;
1c79356b 5039
4a3eedf9 5040 if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
91447636
A
5041 for (i = 0; i < count; i++) {
5042 if_down(ifp[i]);
4a3eedf9 5043 dlil_proto_unplumb_all(ifp[i]);
91447636
A
5044 }
5045 ifnet_list_free(ifp);
5046 }
4a3eedf9 5047
0a7de745 5048 return 0;
1c79356b 5049}
9bccf70c
A
5050
5051/*
5052 * Delete Routes for a Network Interface
39037602 5053 *
9bccf70c
A
5054 * Called for each routing entry via the rnh->rnh_walktree() call above
5055 * to delete all route entries referencing a detaching network interface.
5056 *
5057 * Arguments:
5058 * rn pointer to node in the routing table
5059 * arg argument passed to rnh->rnh_walktree() - detaching interface
5060 *
5061 * Returns:
5062 * 0 successful
5063 * errno failed - reason indicated
5064 *
5065 */
5066static int
b0d623f7 5067if_rtdel(struct radix_node *rn, void *arg)
9bccf70c 5068{
0a7de745
A
5069 struct rtentry *rt = (struct rtentry *)rn;
5070 struct ifnet *ifp = arg;
5071 int err;
9bccf70c 5072
0a7de745
A
5073 if (rt == NULL) {
5074 return 0;
5075 }
b0d623f7
A
5076 /*
5077 * Checking against RTF_UP protects against walktree
5078 * recursion problems with cloned routes.
5079 */
5080 RT_LOCK(rt);
5081 if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
9bccf70c 5082 /*
b0d623f7
A
5083 * Safe to drop rt_lock and use rt_key, rt_gateway,
5084 * since holding rnh_lock here prevents another thread
5085 * from calling rt_setgate() on this route.
9bccf70c 5086 */
b0d623f7 5087 RT_UNLOCK(rt);
91447636 5088 err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
b0d623f7 5089 rt_mask(rt), rt->rt_flags, NULL);
9bccf70c
A
5090 if (err) {
5091 log(LOG_WARNING, "if_rtdel: error %d\n", err);
5092 }
b0d623f7
A
5093 } else {
5094 RT_UNLOCK(rt);
9bccf70c 5095 }
0a7de745 5096 return 0;
9bccf70c
A
5097}
5098
5099/*
b0d623f7 5100 * Removes routing table reference to a given interface
9bccf70c
A
5101 * for a given protocol family
5102 */
b0d623f7
A
5103void
5104if_rtproto_del(struct ifnet *ifp, int protocol)
9bccf70c 5105{
3a60a9f5 5106 struct radix_node_head *rnh;
b0d623f7 5107
3a60a9f5 5108 if ((protocol <= AF_MAX) && (protocol >= 0) &&
0a7de745 5109 ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
b0d623f7 5110 lck_mtx_lock(rnh_lock);
9bccf70c 5111 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
b0d623f7 5112 lck_mtx_unlock(rnh_lock);
91447636
A
5113 }
5114}
5115
2d21ac55
A
5116static int
5117if_rtmtu(struct radix_node *rn, void *arg)
5118{
5119 struct rtentry *rt = (struct rtentry *)rn;
5120 struct ifnet *ifp = arg;
5121
b0d623f7 5122 RT_LOCK(rt);
2d21ac55
A
5123 if (rt->rt_ifp == ifp) {
5124 /*
5125 * Update the MTU of this entry only if the MTU
5126 * has not been locked (RTV_MTU is not set) and
5127 * if it was non-zero to begin with.
5128 */
d9a64523 5129 if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu) {
2d21ac55 5130 rt->rt_rmx.rmx_mtu = ifp->if_mtu;
d9a64523
A
5131 if (rt_key(rt)->sa_family == AF_INET &&
5132 INTF_ADJUST_MTU_FOR_CLAT46(ifp)) {
5133 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(ifp);
5134 /* Further adjust the size for CLAT46 expansion */
5135 rt->rt_rmx.rmx_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
5136 }
5137 }
2d21ac55 5138 }
b0d623f7 5139 RT_UNLOCK(rt);
2d21ac55 5140
0a7de745 5141 return 0;
2d21ac55
A
5142}
5143
5144/*
5145 * Update the MTU metric of all route entries in all protocol tables
5146 * associated with a particular interface; this is called when the
5147 * MTU of that interface has changed.
5148 */
39037602
A
5149static void
5150if_rtmtu_update(struct ifnet *ifp)
2d21ac55
A
5151{
5152 struct radix_node_head *rnh;
5153 int p;
5154
5155 for (p = 0; p < AF_MAX + 1; p++) {
0a7de745 5156 if ((rnh = rt_tables[p]) == NULL) {
2d21ac55 5157 continue;
0a7de745 5158 }
2d21ac55 5159
b0d623f7 5160 lck_mtx_lock(rnh_lock);
2d21ac55 5161 (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
b0d623f7 5162 lck_mtx_unlock(rnh_lock);
2d21ac55 5163 }
39236c6e 5164 routegenid_update();
2d21ac55 5165}
91447636
A
5166
5167__private_extern__ void
6d2010ae
A
5168if_data_internal_to_if_data(struct ifnet *ifp,
5169 const struct if_data_internal *if_data_int, struct if_data *if_data)
91447636 5170{
6d2010ae 5171#pragma unused(ifp)
0a7de745
A
5172#define COPYFIELD(fld) if_data->fld = if_data_int->fld
5173#define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
6d2010ae 5174/* compiler will cast down to 32-bit */
0a7de745
A
5175#define COPYFIELD32_ATOMIC(fld) do { \
5176 atomic_get_64(if_data->fld, \
5177 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
6d2010ae
A
5178} while (0)
5179
91447636
A
5180 COPYFIELD(ifi_type);
5181 COPYFIELD(ifi_typelen);
5182 COPYFIELD(ifi_physical);
5183 COPYFIELD(ifi_addrlen);
5184 COPYFIELD(ifi_hdrlen);
5185 COPYFIELD(ifi_recvquota);
5186 COPYFIELD(ifi_xmitquota);
5187 if_data->ifi_unused1 = 0;
5188 COPYFIELD(ifi_mtu);
5189 COPYFIELD(ifi_metric);
5190 if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
5191 if_data->ifi_baudrate = 0xFFFFFFFF;
6d2010ae 5192 } else {
91447636
A
5193 COPYFIELD32(ifi_baudrate);
5194 }
6d2010ae
A
5195
5196 COPYFIELD32_ATOMIC(ifi_ipackets);
5197 COPYFIELD32_ATOMIC(ifi_ierrors);
5198 COPYFIELD32_ATOMIC(ifi_opackets);
5199 COPYFIELD32_ATOMIC(ifi_oerrors);
5200 COPYFIELD32_ATOMIC(ifi_collisions);
5201 COPYFIELD32_ATOMIC(ifi_ibytes);
5202 COPYFIELD32_ATOMIC(ifi_obytes);
5203 COPYFIELD32_ATOMIC(ifi_imcasts);
5204 COPYFIELD32_ATOMIC(ifi_omcasts);
5205 COPYFIELD32_ATOMIC(ifi_iqdrops);
5206 COPYFIELD32_ATOMIC(ifi_noproto);
5207
5208 COPYFIELD(ifi_recvtiming);
5209 COPYFIELD(ifi_xmittiming);
316670eb 5210
b0d623f7
A
5211 if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5212 if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
6d2010ae 5213
91447636 5214 if_data->ifi_lastchange.tv_sec += boottime_sec();
91447636
A
5215
5216 if_data->ifi_unused2 = 0;
5217 COPYFIELD(ifi_hwassist);
5218 if_data->ifi_reserved1 = 0;
5219 if_data->ifi_reserved2 = 0;
6d2010ae 5220#undef COPYFIELD32_ATOMIC
91447636
A
5221#undef COPYFIELD32
5222#undef COPYFIELD
5223}
5224
5225__private_extern__ void
6d2010ae
A
5226if_data_internal_to_if_data64(struct ifnet *ifp,
5227 const struct if_data_internal *if_data_int,
5228 struct if_data64 *if_data64)
91447636 5229{
6d2010ae 5230#pragma unused(ifp)
0a7de745
A
5231#define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
5232#define COPYFIELD64_ATOMIC(fld) do { \
5233 atomic_get_64(if_data64->fld, \
5234 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
6d2010ae
A
5235} while (0)
5236
5237 COPYFIELD64(ifi_type);
5238 COPYFIELD64(ifi_typelen);
5239 COPYFIELD64(ifi_physical);
5240 COPYFIELD64(ifi_addrlen);
5241 COPYFIELD64(ifi_hdrlen);
5242 COPYFIELD64(ifi_recvquota);
5243 COPYFIELD64(ifi_xmitquota);
91447636 5244 if_data64->ifi_unused1 = 0;
6d2010ae
A
5245 COPYFIELD64(ifi_mtu);
5246 COPYFIELD64(ifi_metric);
5247 COPYFIELD64(ifi_baudrate);
5248
5249 COPYFIELD64_ATOMIC(ifi_ipackets);
5250 COPYFIELD64_ATOMIC(ifi_ierrors);
5251 COPYFIELD64_ATOMIC(ifi_opackets);
5252 COPYFIELD64_ATOMIC(ifi_oerrors);
5253 COPYFIELD64_ATOMIC(ifi_collisions);
5254 COPYFIELD64_ATOMIC(ifi_ibytes);
5255 COPYFIELD64_ATOMIC(ifi_obytes);
5256 COPYFIELD64_ATOMIC(ifi_imcasts);
5257 COPYFIELD64_ATOMIC(ifi_omcasts);
5258 COPYFIELD64_ATOMIC(ifi_iqdrops);
5259 COPYFIELD64_ATOMIC(ifi_noproto);
5260
39037602
A
5261 /*
5262 * Note these two fields are actually 32 bit, so doing
5263 * COPYFIELD64_ATOMIC will cause them to be misaligned
6d2010ae
A
5264 */
5265 COPYFIELD64(ifi_recvtiming);
5266 COPYFIELD64(ifi_xmittiming);
5267
b0d623f7
A
5268 if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
5269 if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
6d2010ae 5270
91447636 5271 if_data64->ifi_lastchange.tv_sec += boottime_sec();
91447636 5272
6d2010ae 5273#undef COPYFIELD64
9bccf70c 5274}
b0d623f7 5275
6d2010ae
A
5276__private_extern__ void
5277if_copy_traffic_class(struct ifnet *ifp,
5278 struct if_traffic_class *if_tc)
b0d623f7 5279{
0a7de745
A
5280#define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
5281 atomic_get_64(if_tc->fld, \
5282 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
6d2010ae
A
5283} while (0)
5284
0a7de745 5285 bzero(if_tc, sizeof(*if_tc));
316670eb
A
5286 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
5287 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
5288 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
5289 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
6d2010ae
A
5290 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
5291 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
5292 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
5293 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
5294 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
5295 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
5296 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
5297 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
5298 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
5299 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
5300 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
5301 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
316670eb
A
5302 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
5303 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
5304 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
5305 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
6d2010ae
A
5306
5307#undef COPY_IF_TC_FIELD64_ATOMIC
5308}
b0d623f7 5309
316670eb
A
5310void
5311if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
5312{
0a7de745
A
5313#define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
5314 atomic_get_64(if_de->fld, \
5315 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
316670eb
A
5316} while (0)
5317
0a7de745 5318 bzero(if_de, sizeof(*if_de));
316670eb 5319 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
39236c6e
A
5320 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
5321 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
5322 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
316670eb
A
5323
5324#undef COPY_IF_DE_FIELD64_ATOMIC
5325}
5326
5327void
5328if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
5329{
0a7de745
A
5330#define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
5331 atomic_get_64(if_ps->ifi_tcp_##fld, \
5332 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
316670eb
A
5333} while (0)
5334
0a7de745
A
5335#define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
5336 atomic_get_64(if_ps->ifi_udp_##fld, \
5337 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
316670eb
A
5338} while (0)
5339
5340 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
5341 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
5342 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
5343 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
5344 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
5345 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
5346 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
5347 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
5348 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
5349 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
5350 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
5351 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
5352 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
5353 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
5354
5355 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
5356 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
5357 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
5358 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
5359 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
5360 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
5361 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
5362 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
5363
5364#undef COPY_IF_PS_TCP_FIELD64_ATOMIC
5365#undef COPY_IF_PS_UDP_FIELD64_ATOMIC
5366}
5367
5368void
5369if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
5370{
0a7de745
A
5371 bzero(if_rs, sizeof(*if_rs));
5372 if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1)) {
316670eb 5373 return;
0a7de745 5374 }
cb323159 5375 bcopy(&ifp->if_poll_pstats, if_rs, sizeof(*if_rs));
316670eb
A
5376 /* Release the IO refcnt */
5377 ifnet_decr_iorefcnt(ifp);
5378}
6d2010ae 5379
cb323159
A
5380void
5381if_copy_netif_stats(struct ifnet *ifp, struct if_netif_stats *if_ns)
5382{
5383 bzero(if_ns, sizeof(*if_ns));
5384#pragma unused(ifp)
5385}
5386
6d2010ae
A
5387struct ifaddr *
5388ifa_remref(struct ifaddr *ifa, int locked)
5389{
0a7de745 5390 if (!locked) {
6d2010ae 5391 IFA_LOCK_SPIN(ifa);
0a7de745 5392 } else {
6d2010ae 5393 IFA_LOCK_ASSERT_HELD(ifa);
0a7de745 5394 }
6d2010ae 5395
0a7de745 5396 if (ifa->ifa_refcnt == 0) {
b0d623f7 5397 panic("%s: ifa %p negative refcnt\n", __func__, ifa);
0a7de745 5398 } else if (ifa->ifa_trace != NULL) {
6d2010ae 5399 (*ifa->ifa_trace)(ifa, FALSE);
0a7de745 5400 }
6d2010ae 5401 if (--ifa->ifa_refcnt == 0) {
0a7de745 5402 if (ifa->ifa_debug & IFD_ATTACHED) {
b0d623f7 5403 panic("ifa %p attached to ifp is being freed\n", ifa);
0a7de745 5404 }
b0d623f7
A
5405 /*
5406 * Some interface addresses are allocated either statically
39236c6e
A
5407 * or carved out of a larger block. Only free it if it was
5408 * allocated via MALLOC or via the corresponding per-address
5409 * family allocator. Otherwise, leave it alone.
b0d623f7
A
5410 */
5411 if (ifa->ifa_debug & IFD_ALLOC) {
6d2010ae
A
5412 if (ifa->ifa_free == NULL) {
5413 IFA_UNLOCK(ifa);
b0d623f7 5414 FREE(ifa, M_IFADDR);
6d2010ae
A
5415 } else {
5416 /* Become a regular mutex */
5417 IFA_CONVERT_LOCK(ifa);
5418 /* callee will unlock */
b0d623f7 5419 (*ifa->ifa_free)(ifa);
6d2010ae
A
5420 }
5421 } else {
5422 IFA_UNLOCK(ifa);
b0d623f7 5423 }
6d2010ae 5424 ifa = NULL;
b0d623f7 5425 }
6d2010ae 5426
0a7de745 5427 if (!locked && ifa != NULL) {
6d2010ae 5428 IFA_UNLOCK(ifa);
0a7de745 5429 }
6d2010ae 5430
0a7de745 5431 return ifa;
b0d623f7
A
5432}
5433
5434void
6d2010ae 5435ifa_addref(struct ifaddr *ifa, int locked)
b0d623f7 5436{
0a7de745 5437 if (!locked) {
6d2010ae 5438 IFA_LOCK_SPIN(ifa);
0a7de745 5439 } else {
6d2010ae 5440 IFA_LOCK_ASSERT_HELD(ifa);
0a7de745 5441 }
b0d623f7 5442
6d2010ae
A
5443 if (++ifa->ifa_refcnt == 0) {
5444 panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
5445 /* NOTREACHED */
5446 } else if (ifa->ifa_trace != NULL) {
b0d623f7 5447 (*ifa->ifa_trace)(ifa, TRUE);
6d2010ae 5448 }
0a7de745 5449 if (!locked) {
6d2010ae 5450 IFA_UNLOCK(ifa);
0a7de745 5451 }
6d2010ae
A
5452}
5453
5454void
5455ifa_lock_init(struct ifaddr *ifa)
5456{
5457 lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
5458}
5459
5460void
5461ifa_lock_destroy(struct ifaddr *ifa)
5462{
5463 IFA_LOCK_ASSERT_NOTHELD(ifa);
5464 lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
b0d623f7 5465}
39236c6e
A
5466
5467/*
5468 * 'i' group ioctls.
5469 *
5470 * The switch statement below does nothing at runtime, as it serves as a
5471 * compile time check to ensure that all of the socket 'i' ioctls (those
5472 * in the 'i' group going thru soo_ioctl) that are made available by the
5473 * networking stack is unique. This works as long as this routine gets
5474 * updated each time a new interface ioctl gets added.
5475 *
5476 * Any failures at compile time indicates duplicated ioctl values.
5477 */
5478static __attribute__((unused)) void
5479ifioctl_cassert(void)
5480{
5481 /*
5482 * This is equivalent to _CASSERT() and the compiler wouldn't
5483 * generate any instructions, thus for compile time only.
5484 */
5485 switch ((u_long)0) {
5486 case 0:
5487
5488 /* bsd/net/if_ppp.h */
5489 case SIOCGPPPSTATS:
5490 case SIOCGPPPCSTATS:
5491
39236c6e
A
5492 /* bsd/netinet6/in6_var.h */
5493 case SIOCSIFADDR_IN6:
5494 case SIOCGIFADDR_IN6:
5495 case SIOCSIFDSTADDR_IN6:
5496 case SIOCSIFNETMASK_IN6:
5497 case SIOCGIFDSTADDR_IN6:
5498 case SIOCGIFNETMASK_IN6:
5499 case SIOCDIFADDR_IN6:
5500 case SIOCAIFADDR_IN6_32:
5501 case SIOCAIFADDR_IN6_64:
5502 case SIOCSIFPHYADDR_IN6_32:
5503 case SIOCSIFPHYADDR_IN6_64:
5504 case SIOCGIFPSRCADDR_IN6:
5505 case SIOCGIFPDSTADDR_IN6:
5506 case SIOCGIFAFLAG_IN6:
5507 case SIOCGDRLST_IN6_32:
5508 case SIOCGDRLST_IN6_64:
5509 case SIOCGPRLST_IN6_32:
5510 case SIOCGPRLST_IN6_64:
5511 case OSIOCGIFINFO_IN6:
5512 case SIOCGIFINFO_IN6:
5513 case SIOCSNDFLUSH_IN6:
5514 case SIOCGNBRINFO_IN6_32:
5515 case SIOCGNBRINFO_IN6_64:
5516 case SIOCSPFXFLUSH_IN6:
5517 case SIOCSRTRFLUSH_IN6:
5518 case SIOCGIFALIFETIME_IN6:
5519 case SIOCSIFALIFETIME_IN6:
5520 case SIOCGIFSTAT_IN6:
5521 case SIOCGIFSTAT_ICMP6:
5522 case SIOCSDEFIFACE_IN6_32:
5523 case SIOCSDEFIFACE_IN6_64:
5524 case SIOCGDEFIFACE_IN6_32:
5525 case SIOCGDEFIFACE_IN6_64:
5526 case SIOCSIFINFO_FLAGS:
5527 case SIOCSSCOPE6:
5528 case SIOCGSCOPE6:
5529 case SIOCGSCOPE6DEF:
5530 case SIOCSIFPREFIX_IN6:
5531 case SIOCGIFPREFIX_IN6:
5532 case SIOCDIFPREFIX_IN6:
5533 case SIOCAIFPREFIX_IN6:
5534 case SIOCCIFPREFIX_IN6:
5535 case SIOCSGIFPREFIX_IN6:
5536 case SIOCPROTOATTACH_IN6_32:
5537 case SIOCPROTOATTACH_IN6_64:
5538 case SIOCPROTODETACH_IN6:
5539 case SIOCLL_START_32:
5540 case SIOCLL_START_64:
5541 case SIOCLL_STOP:
5542 case SIOCAUTOCONF_START:
5543 case SIOCAUTOCONF_STOP:
5544 case SIOCSETROUTERMODE_IN6:
f427ee49 5545 case SIOCGETROUTERMODE_IN6:
39236c6e
A
5546 case SIOCLL_CGASTART_32:
5547 case SIOCLL_CGASTART_64:
39037602
A
5548 case SIOCGIFCGAPREP_IN6:
5549 case SIOCSIFCGAPREP_IN6:
39236c6e
A
5550
5551 /* bsd/sys/sockio.h */
5552 case SIOCSIFADDR:
5553 case OSIOCGIFADDR:
5554 case SIOCSIFDSTADDR:
5555 case OSIOCGIFDSTADDR:
5556 case SIOCSIFFLAGS:
5557 case SIOCGIFFLAGS:
5558 case OSIOCGIFBRDADDR:
5559 case SIOCSIFBRDADDR:
5560 case OSIOCGIFCONF32:
5561 case OSIOCGIFCONF64:
5562 case OSIOCGIFNETMASK:
5563 case SIOCSIFNETMASK:
5564 case SIOCGIFMETRIC:
5565 case SIOCSIFMETRIC:
5566 case SIOCDIFADDR:
5567 case SIOCAIFADDR:
39037602 5568
39236c6e
A
5569 case SIOCGIFADDR:
5570 case SIOCGIFDSTADDR:
5571 case SIOCGIFBRDADDR:
5572 case SIOCGIFCONF32:
5573 case SIOCGIFCONF64:
5574 case SIOCGIFNETMASK:
5575 case SIOCAUTOADDR:
5576 case SIOCAUTONETMASK:
5577 case SIOCARPIPLL:
39037602 5578
39236c6e
A
5579 case SIOCADDMULTI:
5580 case SIOCDELMULTI:
5581 case SIOCGIFMTU:
5582 case SIOCSIFMTU:
5583 case SIOCGIFPHYS:
5584 case SIOCSIFPHYS:
5585 case SIOCSIFMEDIA:
5586 case SIOCGIFMEDIA32:
5587 case SIOCGIFMEDIA64:
0a7de745
A
5588 case SIOCGIFXMEDIA32:
5589 case SIOCGIFXMEDIA64:
39236c6e
A
5590 case SIOCSIFGENERIC:
5591 case SIOCGIFGENERIC:
5592 case SIOCRSLVMULTI:
39037602 5593
39236c6e
A
5594 case SIOCSIFLLADDR:
5595 case SIOCGIFSTATUS:
5596 case SIOCSIFPHYADDR:
5597 case SIOCGIFPSRCADDR:
5598 case SIOCGIFPDSTADDR:
5599 case SIOCDIFPHYADDR:
39037602 5600
39236c6e
A
5601 case SIOCGIFDEVMTU:
5602 case SIOCSIFALTMTU:
5603 case SIOCGIFALTMTU:
5604 case SIOCSIFBOND:
5605 case SIOCGIFBOND:
39037602 5606
39236c6e
A
5607 case SIOCPROTOATTACH:
5608 case SIOCPROTODETACH:
39037602 5609
39236c6e
A
5610 case SIOCSIFCAP:
5611 case SIOCGIFCAP:
39037602 5612
39236c6e
A
5613 case SIOCIFCREATE:
5614 case SIOCIFDESTROY:
5615 case SIOCIFCREATE2:
39037602 5616
39236c6e
A
5617 case SIOCSDRVSPEC32:
5618 case SIOCGDRVSPEC32:
5619 case SIOCSDRVSPEC64:
5620 case SIOCGDRVSPEC64:
39037602 5621
39236c6e
A
5622 case SIOCSIFVLAN:
5623 case SIOCGIFVLAN:
39037602 5624
39236c6e
A
5625 case SIOCIFGCLONERS32:
5626 case SIOCIFGCLONERS64:
39037602 5627
39236c6e
A
5628 case SIOCGIFASYNCMAP:
5629 case SIOCSIFASYNCMAP:
39236c6e
A
5630 case SIOCSIFKPI:
5631 case SIOCGIFKPI:
39037602 5632
39236c6e 5633 case SIOCGIFWAKEFLAGS:
39037602 5634
39236c6e
A
5635 case SIOCGIFGETRTREFCNT:
5636 case SIOCGIFLINKQUALITYMETRIC:
5637 case SIOCSIFOPPORTUNISTIC:
5638 case SIOCGIFOPPORTUNISTIC:
f427ee49 5639 case SIOCGETROUTERMODE:
39236c6e
A
5640 case SIOCSETROUTERMODE:
5641 case SIOCGIFEFLAGS:
5642 case SIOCSIFDESC:
5643 case SIOCGIFDESC:
5644 case SIOCSIFLINKPARAMS:
5645 case SIOCGIFLINKPARAMS:
5646 case SIOCGIFQUEUESTATS:
5647 case SIOCSIFTHROTTLE:
5648 case SIOCGIFTHROTTLE:
39037602
A
5649
5650 case SIOCGASSOCIDS32:
5651 case SIOCGASSOCIDS64:
5652 case SIOCGCONNIDS32:
5653 case SIOCGCONNIDS64:
5654 case SIOCGCONNINFO32:
5655 case SIOCGCONNINFO64:
5656 case SIOCSCONNORDER:
5657 case SIOCGCONNORDER:
5658
39236c6e
A
5659 case SIOCSIFLOG:
5660 case SIOCGIFLOG:
5661 case SIOCGIFDELEGATE:
5662 case SIOCGIFLLADDR:
5663 case SIOCGIFTYPE:
39037602
A
5664 case SIOCGIFEXPENSIVE:
5665 case SIOCSIFEXPENSIVE:
5666 case SIOCGIF2KCL:
5667 case SIOCSIF2KCL:
5668 case SIOCGSTARTDELAY:
5669
3e170ce0
A
5670 case SIOCAIFAGENTID:
5671 case SIOCDIFAGENTID:
5672 case SIOCGIFAGENTIDS32:
5673 case SIOCGIFAGENTIDS64:
5674 case SIOCGIFAGENTDATA32:
5675 case SIOCGIFAGENTDATA64:
39037602 5676
3e170ce0
A
5677 case SIOCSIFINTERFACESTATE:
5678 case SIOCGIFINTERFACESTATE:
4bd07ac2
A
5679 case SIOCSIFPROBECONNECTIVITY:
5680 case SIOCGIFPROBECONNECTIVITY:
39037602
A
5681
5682 case SIOCGIFFUNCTIONALTYPE:
5683 case SIOCSIFNETSIGNATURE:
5684 case SIOCGIFNETSIGNATURE:
5685
f427ee49 5686 case SIOCSIFNETWORKID:
4bd07ac2
A
5687 case SIOCGECNMODE:
5688 case SIOCSECNMODE:
39037602 5689
d9a64523
A
5690 case SIOCSIFORDER:
5691
39037602
A
5692 case SIOCSQOSMARKINGMODE:
5693 case SIOCSQOSMARKINGENABLED:
5694 case SIOCGQOSMARKINGMODE:
5695 case SIOCGQOSMARKINGENABLED:
a39ff7e2 5696
d9a64523
A
5697 case SIOCSIFTIMESTAMPENABLE:
5698 case SIOCSIFTIMESTAMPDISABLE:
5699 case SIOCGIFTIMESTAMPENABLED:
5700
5701 case SIOCSIFDISABLEOUTPUT:
5702
cb323159
A
5703 case SIOCSIFSUBFAMILY:
5704
d9a64523
A
5705 case SIOCGIFAGENTLIST32:
5706 case SIOCGIFAGENTLIST64:
5707
5708 case SIOCSIFLOWINTERNET:
5709 case SIOCGIFLOWINTERNET:
5710
d9a64523
A
5711 case SIOCGIFNAT64PREFIX:
5712 case SIOCSIFNAT64PREFIX:
5713
5714 case SIOCGIFCLAT46ADDR:
d9a64523 5715
a39ff7e2
A
5716 case SIOCGIFPROTOLIST32:
5717 case SIOCGIFPROTOLIST64:
d9a64523 5718
cb323159
A
5719 case SIOCSIF6LOWPAN:
5720 case SIOCGIF6LOWPAN:
5721
d9a64523
A
5722 case SIOCGIFLOWPOWER:
5723 case SIOCSIFLOWPOWER:
cb323159
A
5724
5725 case SIOCGIFMPKLOG:
5726 case SIOCSIFMPKLOG:
5727
5728 case SIOCGIFCONSTRAINED:
5729 case SIOCSIFCONSTRAINED:
5730
5731 case SIOCGIFXFLAGS:
5732
5733 case SIOCGIFNOACKPRIO:
5734 case SIOCSIFNOACKPRIO:
39236c6e
A
5735 ;
5736 }
5737}
5ba3f43e 5738
5ba3f43e
A
5739uint32_t
5740ifnet_mbuf_packetpreamblelen(struct ifnet *ifp)
5741{
5742#pragma unused(ifp)
0a7de745 5743 return 0;
5ba3f43e
A
5744}
5745
5746/* The following is used to enqueue work items for interface events */
5747struct intf_event {
5748 struct ifnet *ifp;
5749 union sockaddr_in_4_6 addr;
5750 uint32_t intf_event_code;
5751};
5752
5753static void
5754intf_event_callback(void *arg)
5755{
5756 struct intf_event *p_intf_ev = (struct intf_event *)arg;
5757
5758 /* Call this before we walk the tree */
5759 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt, ifnet_event, p_intf_ev->ifp,
5760 (struct sockaddr *)&(p_intf_ev->addr), p_intf_ev->intf_event_code);
5761}
5762
5763struct intf_event_nwk_wq_entry {
5764 struct nwk_wq_entry nwk_wqe;
5765 struct intf_event intf_ev_arg;
5766};
5767
5768void
5769intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp, struct sockaddr *addrp,
5770 uint32_t intf_event_code)
5771{
5772#pragma unused(addrp)
5773 struct intf_event_nwk_wq_entry *p_intf_ev = NULL;
5774
5775 MALLOC(p_intf_ev, struct intf_event_nwk_wq_entry *,
5776 sizeof(struct intf_event_nwk_wq_entry),
5777 M_NWKWQ, M_WAITOK | M_ZERO);
5778
5779 p_intf_ev->intf_ev_arg.ifp = ifp;
5780 /*
5781 * XXX Not using addr in the arg. This will be used
5782 * once we need IP address add/delete events
5783 */
5784 p_intf_ev->intf_ev_arg.intf_event_code = intf_event_code;
5785 p_intf_ev->nwk_wqe.func = intf_event_callback;
5786 p_intf_ev->nwk_wqe.is_arg_managed = TRUE;
5787 p_intf_ev->nwk_wqe.arg = &p_intf_ev->intf_ev_arg;
5788 nwk_wq_enqueue((struct nwk_wq_entry*)p_intf_ev);
5789}
cb323159
A
5790
5791int
5792if_get_tcp_kao_max(struct ifnet *ifp)
5793{
5794 int error = 0;
5795
5796 if (ifp->if_tcp_kao_max == 0) {
5797 struct ifreq ifr;
5798
5799 memset(&ifr, 0, sizeof(struct ifreq));
5800 error = ifnet_ioctl(ifp, 0, SIOCGIFTCPKAOMAX, &ifr);
5801
5802 ifnet_lock_exclusive(ifp);
5803 if (error == 0) {
5804 ifp->if_tcp_kao_max = ifr.ifr_tcp_kao_max;
5805 } else if (error == EOPNOTSUPP) {
5806 ifp->if_tcp_kao_max = default_tcp_kao_max;
5807 }
5808 ifnet_lock_done(ifp);
5809 }
5810 return error;
5811}