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