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