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