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