]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if.c
xnu-3247.1.106.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 */
1995 case SIOCGSTARTDELAY: { /* struct ifreq */
316670eb
A
1996 struct ifreq ifr;
1997 bcopy(data, &ifr, sizeof (ifr));
39236c6e
A
1998 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
1999 bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
316670eb
A
2000 error = ifioctl_ifreq(so, cmd, &ifr, p);
2001 bcopy(&ifr, data, sizeof (ifr));
2002 goto done;
2003 }
2004 }
2005
2006 /*
2007 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2008 * here to ensure that the ifnet, if found, has been fully attached.
2009 */
2010 dlil_if_lock();
2011 switch (cmd) {
39236c6e
A
2012 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2013 bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
316670eb
A
2014 ifname, IFNAMSIZ);
2015 ifp = ifunit(ifname);
2016 break;
316670eb
A
2017
2018#if INET6
39236c6e 2019 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
316670eb
A
2020 bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
2021 ifname, IFNAMSIZ);
2022 ifp = ifunit(ifname);
2023 break;
316670eb 2024
39236c6e 2025 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
316670eb
A
2026 bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
2027 ifname, IFNAMSIZ);
2028 ifp = ifunit(ifname);
2029 break;
39236c6e 2030#endif /* INET6 */
316670eb 2031
39236c6e 2032 case SIOCGIFSTATUS: /* struct ifstat */
316670eb
A
2033 ifs = _MALLOC(sizeof (*ifs), M_DEVBUF, M_WAITOK);
2034 if (ifs == NULL) {
2035 error = ENOMEM;
2036 dlil_if_unlock();
2037 goto done;
2038 }
2039 bcopy(data, ifs, sizeof (*ifs));
2040 ifs->ifs_name[IFNAMSIZ - 1] = '\0';
39236c6e
A
2041 bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
2042 ifp = ifunit(ifname);
316670eb 2043 break;
316670eb 2044
39236c6e 2045 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
316670eb
A
2046 bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
2047 ifname, IFNAMSIZ);
2048 ifp = ifunit(ifname);
2049 break;
316670eb 2050
39236c6e 2051 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
316670eb
A
2052 bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
2053 ifname, IFNAMSIZ);
2054 ifp = ifunit(ifname);
2055 break;
316670eb
A
2056
2057 case SIOCSIFDESC: /* struct if_descreq */
39236c6e 2058 case SIOCGIFDESC: /* struct if_descreq */
316670eb
A
2059 bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
2060 ifname, IFNAMSIZ);
2061 ifp = ifunit(ifname);
2062 break;
316670eb
A
2063
2064 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
39236c6e 2065 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
316670eb
A
2066 bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
2067 ifname, IFNAMSIZ);
2068 ifp = ifunit(ifname);
2069 break;
316670eb 2070
39236c6e 2071 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
316670eb
A
2072 bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
2073 ifname, IFNAMSIZ);
2074 ifp = ifunit(ifname);
2075 break;
316670eb
A
2076
2077 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
39236c6e 2078 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
316670eb
A
2079 bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
2080 ifname, IFNAMSIZ);
2081 ifp = ifunit(ifname);
2082 break;
316670eb 2083
3e170ce0
A
2084 case SIOCAIFAGENTID: /* struct if_agentidreq */
2085 case SIOCDIFAGENTID: /* struct if_agentidreq */
2086 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2087 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2088 bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
2089 ifname, IFNAMSIZ);
2090 ifp = ifunit(ifname);
2091 break;
2092
2093 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2094 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2095 bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
2096 ifname, IFNAMSIZ);
2097 ifp = ifunit(ifname);
2098 break;
2099
39236c6e 2100 default:
316670eb
A
2101 /*
2102 * This is a bad assumption, but the code seems to
2103 * have been doing this in the past; caveat emptor.
39236c6e
A
2104 */
2105 bcopy(((struct ifreq *)(void *)data)->ifr_name,
2106 ifname, IFNAMSIZ);
2107 ifp = ifunit(ifname);
316670eb
A
2108 break;
2109 }
39236c6e 2110 dlil_if_unlock();
316670eb 2111
39236c6e
A
2112 if (ifp == NULL) {
2113 error = ENXIO;
2114 goto done;
2115 }
316670eb 2116
39236c6e
A
2117 switch (cmd) {
2118 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2119#if INET6
2120 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
2121 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
2122#endif /* INET6 */
39236c6e
A
2123 error = proc_suser(p);
2124 if (error != 0)
2125 break;
316670eb 2126
39236c6e
A
2127 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2128 if (error != 0)
2129 break;
316670eb 2130
39236c6e 2131 ifnet_touch_lastchange(ifp);
316670eb 2132 break;
316670eb 2133
39236c6e
A
2134 case SIOCGIFSTATUS: /* struct ifstat */
2135 VERIFY(ifs != NULL);
2136 ifs->ascii[0] = '\0';
316670eb 2137
39236c6e
A
2138 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
2139
2140 bcopy(ifs, data, sizeof (*ifs));
316670eb 2141 break;
316670eb 2142
39236c6e
A
2143 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
2144 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
2145 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2146 break;
316670eb 2147
39236c6e
A
2148 case SIOCSIFDESC: /* struct if_descreq */
2149 case SIOCGIFDESC: /* struct if_descreq */
2150 error = ifioctl_ifdesc(ifp, cmd, data, p);
2151 break;
316670eb 2152
39236c6e
A
2153 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
2154 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
2155 error = ifioctl_linkparams(ifp, cmd, data, p);
316670eb 2156 break;
316670eb 2157
39236c6e
A
2158 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
2159 error = ifioctl_qstats(ifp, cmd, data);
2160 break;
316670eb 2161
39236c6e
A
2162 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
2163 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
2164 error = ifioctl_throttle(ifp, cmd, data, p);
316670eb 2165 break;
316670eb 2166
3e170ce0
A
2167 case SIOCAIFAGENTID: /* struct if_agentidreq */
2168 case SIOCDIFAGENTID: /* struct if_agentidreq */
2169 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2170 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2171 error = ifioctl_netagent(ifp, cmd, data, p);
2172 break;
2173
2174 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2175 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2176 error = ifioctl_netsignature(ifp, cmd, data);
2177 break;
2178
316670eb
A
2179 default:
2180 if (so->so_proto == NULL) {
2181 error = EOPNOTSUPP;
2182 break;
2183 }
2184
2185 socket_lock(so, 1);
2186 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2187 data, ifp, p));
2188 socket_unlock(so, 1);
2189
2190 if (error == EOPNOTSUPP || error == ENOTSUP) {
39236c6e 2191 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
316670eb
A
2192 }
2193 break;
2194 }
2195
2196done:
2197 if (ifs != NULL)
2198 _FREE(ifs, M_DEVBUF);
2199
39236c6e
A
2200 if (if_verbose) {
2201 if (ifname[0] == '\0')
2202 (void) snprintf(ifname, sizeof (ifname), "%s",
2203 "NULL");
2204 else if (ifp != NULL)
2205 (void) snprintf(ifname, sizeof (ifname), "%s",
2206 if_name(ifp));
2207
2208 if (error != 0) {
2209 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2210 "%c %lu) error %d\n", __func__,
2211 proc_name_address(p), proc_pid(p),
2212 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2213 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2214 (char)IOCGROUP(cmd), cmd & 0xff, error);
2215 } else if (if_verbose > 1) {
2216 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2217 "%c %lu) OK\n", __func__,
2218 proc_name_address(p), proc_pid(p),
2219 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2220 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2221 (char)IOCGROUP(cmd), cmd & 0xff);
2222 }
2223 }
2224
316670eb
A
2225 return (error);
2226}
2227
39236c6e 2228static __attribute__((noinline)) int
316670eb
A
2229ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
2230{
2231 struct ifnet *ifp;
2232 u_long ocmd = cmd;
2233 int error = 0;
2234 struct kev_msg ev_msg;
2235 struct net_event_data ev_data;
2236
2237 bzero(&ev_data, sizeof (struct net_event_data));
2238 bzero(&ev_msg, sizeof (struct kev_msg));
2239
4a249263
A
2240 switch (cmd) {
2241 case SIOCIFCREATE:
d1ecb069
A
2242 case SIOCIFCREATE2:
2243 error = proc_suser(p);
2244 if (error)
2245 return (error);
316670eb
A
2246 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2247 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
4a249263 2248 case SIOCIFDESTROY:
91447636 2249 error = proc_suser(p);
4a249263
A
2250 if (error)
2251 return (error);
316670eb 2252 return (if_clone_destroy(ifr->ifr_name));
4a249263
A
2253 }
2254
39236c6e
A
2255 /*
2256 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2257 * here to ensure that the ifnet, if found, has been fully attached.
2258 */
2259 dlil_if_lock();
1c79356b 2260 ifp = ifunit(ifr->ifr_name);
39236c6e
A
2261 dlil_if_unlock();
2262
6d2010ae 2263 if (ifp == NULL)
1c79356b 2264 return (ENXIO);
1c79356b 2265
6d2010ae 2266 switch (cmd) {
1c79356b 2267 case SIOCGIFFLAGS:
91447636 2268 ifnet_lock_shared(ifp);
1c79356b 2269 ifr->ifr_flags = ifp->if_flags;
91447636 2270 ifnet_lock_done(ifp);
1c79356b
A
2271 break;
2272
316670eb
A
2273 case SIOCGIFEFLAGS:
2274 ifnet_lock_shared(ifp);
2275 ifr->ifr_eflags = ifp->if_eflags;
2276 ifnet_lock_done(ifp);
2277 break;
2278
6d2010ae
A
2279 case SIOCGIFCAP:
2280 ifnet_lock_shared(ifp);
2281 ifr->ifr_reqcap = ifp->if_capabilities;
2282 ifr->ifr_curcap = ifp->if_capenable;
2283 ifnet_lock_done(ifp);
2284 break;
2285
2d21ac55
A
2286#if CONFIG_MACF_NET
2287 case SIOCGIFMAC:
2288 error = mac_ifnet_label_get(kauth_cred_get(), ifr, ifp);
2d21ac55 2289 break;
39236c6e
A
2290
2291 case SIOCSIFMAC:
2292 error = mac_ifnet_label_set(kauth_cred_get(), ifr, ifp);
2293 break;
2294#endif /* CONFIG_MACF_NET */
2295
1c79356b 2296 case SIOCGIFMETRIC:
91447636 2297 ifnet_lock_shared(ifp);
1c79356b 2298 ifr->ifr_metric = ifp->if_metric;
91447636 2299 ifnet_lock_done(ifp);
1c79356b
A
2300 break;
2301
2302 case SIOCGIFMTU:
91447636 2303 ifnet_lock_shared(ifp);
1c79356b 2304 ifr->ifr_mtu = ifp->if_mtu;
91447636 2305 ifnet_lock_done(ifp);
1c79356b
A
2306 break;
2307
2308 case SIOCGIFPHYS:
91447636 2309 ifnet_lock_shared(ifp);
1c79356b 2310 ifr->ifr_phys = ifp->if_physical;
91447636 2311 ifnet_lock_done(ifp);
1c79356b
A
2312 break;
2313
2314 case SIOCSIFFLAGS:
91447636 2315 error = proc_suser(p);
6d2010ae
A
2316 if (error != 0)
2317 break;
1c79356b 2318
6d2010ae
A
2319 (void) ifnet_set_flags(ifp, ifr->ifr_flags,
2320 (u_int16_t)~IFF_CANTCHANGE);
2d21ac55 2321
6d2010ae
A
2322 /*
2323 * Note that we intentionally ignore any error from below
2324 * for the SIOCSIFFLAGS case.
2325 */
39236c6e 2326 (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
1c79356b 2327
6d2010ae
A
2328 /*
2329 * Send the event even upon error from the driver because
2330 * we changed the flags.
2331 */
b0d623f7
A
2332 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2333 ev_msg.kev_class = KEV_NETWORK_CLASS;
2334 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
6d2010ae 2335
b0d623f7
A
2336 ev_msg.event_code = KEV_DL_SIFFLAGS;
2337 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2338 ev_data.if_family = ifp->if_family;
2339 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2340 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2341 ev_msg.dv[0].data_ptr = &ev_data;
2342 ev_msg.dv[1].data_length = 0;
2343 kev_post_msg(&ev_msg);
2344
91447636 2345 ifnet_touch_lastchange(ifp);
1c79356b
A
2346 break;
2347
6d2010ae
A
2348 case SIOCSIFCAP:
2349 error = proc_suser(p);
2350 if (error != 0)
2351 break;
2352
2353 if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
2354 error = EINVAL;
2355 break;
2356 }
39236c6e 2357 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
6d2010ae
A
2358
2359 ifnet_touch_lastchange(ifp);
2360 break;
2361
1c79356b 2362 case SIOCSIFMETRIC:
91447636 2363 error = proc_suser(p);
6d2010ae
A
2364 if (error != 0)
2365 break;
1c79356b 2366
6d2010ae 2367 ifp->if_metric = ifr->ifr_metric;
1c79356b
A
2368
2369 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2370 ev_msg.kev_class = KEV_NETWORK_CLASS;
2371 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
6d2010ae 2372
1c79356b 2373 ev_msg.event_code = KEV_DL_SIFMETRICS;
2d21ac55 2374 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
1c79356b 2375 ev_data.if_family = ifp->if_family;
b0d623f7 2376 ev_data.if_unit = (u_int32_t) ifp->if_unit;
1c79356b
A
2377 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2378 ev_msg.dv[0].data_ptr = &ev_data;
2379
2380 ev_msg.dv[1].data_length = 0;
2381 kev_post_msg(&ev_msg);
2382
91447636 2383 ifnet_touch_lastchange(ifp);
1c79356b
A
2384 break;
2385
2386 case SIOCSIFPHYS:
91447636 2387 error = proc_suser(p);
6d2010ae
A
2388 if (error != 0)
2389 break;
1c79356b 2390
39236c6e 2391 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
6d2010ae
A
2392 if (error != 0)
2393 break;
1c79356b 2394
6d2010ae
A
2395 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2396 ev_msg.kev_class = KEV_NETWORK_CLASS;
2397 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2398
2399 ev_msg.event_code = KEV_DL_SIFPHYS;
2400 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2401 ev_data.if_family = ifp->if_family;
2402 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2403 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2404 ev_msg.dv[0].data_ptr = &ev_data;
2405 ev_msg.dv[1].data_length = 0;
2406 kev_post_msg(&ev_msg);
2407
2408 ifnet_touch_lastchange(ifp);
2409 break;
1c79356b 2410
316670eb 2411 case SIOCSIFMTU: {
b0d623f7 2412 u_int32_t oldmtu = ifp->if_mtu;
316670eb 2413 struct ifclassq *ifq = &ifp->if_snd;
1c79356b 2414
91447636 2415 error = proc_suser(p);
6d2010ae
A
2416 if (error != 0)
2417 break;
1c79356b 2418
6d2010ae
A
2419 if (ifp->if_ioctl == NULL) {
2420 error = EOPNOTSUPP;
2421 break;
2422 }
2423 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2424 error = EINVAL;
2425 break;
1c79356b 2426 }
39236c6e 2427 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
6d2010ae
A
2428 if (error != 0)
2429 break;
2430
2431 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2432 ev_msg.kev_class = KEV_NETWORK_CLASS;
2433 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2434
2435 ev_msg.event_code = KEV_DL_SIFMTU;
2436 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2437 ev_data.if_family = ifp->if_family;
2438 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2439 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2440 ev_msg.dv[0].data_ptr = &ev_data;
2441 ev_msg.dv[1].data_length = 0;
2442 kev_post_msg(&ev_msg);
2443
2444 ifnet_touch_lastchange(ifp);
2445 rt_ifmsg(ifp);
2446
1c79356b 2447 /*
2d21ac55
A
2448 * If the link MTU changed, do network layer specific procedure
2449 * and update all route entries associated with the interface,
2450 * so that their MTU metric gets updated.
1c79356b 2451 */
6d2010ae 2452 if (ifp->if_mtu != oldmtu) {
2d21ac55 2453 if_rtmtu_update(ifp);
9bccf70c
A
2454#if INET6
2455 nd6_setmtu(ifp);
39236c6e 2456#endif /* INET6 */
316670eb
A
2457 /* Inform all transmit queues about the new MTU */
2458 IFCQ_LOCK(ifq);
2459 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
2460 IFCQ_UNLOCK(ifq);
1c79356b 2461 }
6d2010ae 2462 break;
9bccf70c 2463 }
1c79356b
A
2464
2465 case SIOCADDMULTI:
2466 case SIOCDELMULTI:
91447636 2467 error = proc_suser(p);
6d2010ae
A
2468 if (error != 0)
2469 break;
1c79356b
A
2470
2471 /* Don't allow group membership on non-multicast interfaces. */
6d2010ae
A
2472 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2473 error = EOPNOTSUPP;
2474 break;
2475 }
1c79356b 2476
9bccf70c 2477 /* Don't let users screw up protocols' entries. */
6d2010ae
A
2478 if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
2479 ifr->ifr_addr.sa_family != AF_LINK) {
2480 error = EINVAL;
2481 break;
2482 }
9bccf70c 2483
6d2010ae
A
2484 /*
2485 * User is permitted to anonymously join a particular link
2486 * multicast group via SIOCADDMULTI. Subsequent join requested
2487 * for the same record which has an outstanding refcnt from a
2488 * past if_addmulti_anon() will not result in EADDRINUSE error
2489 * (unlike other BSDs.) Anonymously leaving a group is also
2490 * allowed only as long as there is an outstanding refcnt held
2491 * by a previous anonymous request, or else ENOENT (even if the
2492 * link-layer multicast membership exists for a network-layer
2493 * membership.)
2494 */
1c79356b 2495 if (cmd == SIOCADDMULTI) {
6d2010ae 2496 error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
1c79356b
A
2497 ev_msg.event_code = KEV_DL_ADDMULTI;
2498 } else {
6d2010ae 2499 error = if_delmulti_anon(ifp, &ifr->ifr_addr);
1c79356b
A
2500 ev_msg.event_code = KEV_DL_DELMULTI;
2501 }
6d2010ae
A
2502 if (error != 0)
2503 break;
2504
2505 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2506 ev_msg.kev_class = KEV_NETWORK_CLASS;
2507 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2508 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2509
2510 ev_data.if_family = ifp->if_family;
2511 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2512 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2513 ev_msg.dv[0].data_ptr = &ev_data;
2514 ev_msg.dv[1].data_length = 0;
2515 kev_post_msg(&ev_msg);
2516
2517 ifnet_touch_lastchange(ifp);
2518 break;
1c79356b 2519
9bccf70c 2520 case SIOCDIFPHYADDR:
9bccf70c 2521 case SIOCSIFMEDIA:
1c79356b 2522 case SIOCSIFGENERIC:
9bccf70c 2523 case SIOCSIFLLADDR:
91447636
A
2524 case SIOCSIFALTMTU:
2525 case SIOCSIFVLAN:
2526 case SIOCSIFBOND:
2527 error = proc_suser(p);
6d2010ae
A
2528 if (error != 0)
2529 break;
1c79356b 2530
39236c6e 2531 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
6d2010ae
A
2532 if (error != 0)
2533 break;
1c79356b 2534
6d2010ae
A
2535 ifnet_touch_lastchange(ifp);
2536 break;
1c79356b 2537
39236c6e
A
2538 case SIOCGIFLLADDR: {
2539 struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
2540
2541 if (sdl->sdl_alen == 0) {
2542 error = EADDRNOTAVAIL;
2543 break;
2544 }
2545 /* If larger than 14-bytes we'll need another mechanism */
2546 if (sdl->sdl_alen > sizeof (ifr->ifr_addr.sa_data)) {
2547 error = EMSGSIZE;
2548 break;
2549 }
2550 /* Follow the same convention used by SIOCSIFLLADDR */
2551 bzero(&ifr->ifr_addr, sizeof (ifr->ifr_addr));
2552 ifr->ifr_addr.sa_family = AF_LINK;
2553 ifr->ifr_addr.sa_len = sdl->sdl_alen;
2554 error = ifnet_guarded_lladdr_copy_bytes(ifp,
2555 &ifr->ifr_addr.sa_data, sdl->sdl_alen);
2556 break;
2557 }
2558
2559 case SIOCGIFTYPE:
2560 ifr->ifr_type.ift_type = ifp->if_type;
2561 ifr->ifr_type.ift_family = ifp->if_family;
2562 ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
2563 break;
2564
3e170ce0
A
2565 case SIOCGIFFUNCTIONALTYPE:
2566 ifr->ifr_functional_type = if_functional_type(ifp);
2567 break;
2568
9bccf70c
A
2569 case SIOCGIFPSRCADDR:
2570 case SIOCGIFPDSTADDR:
1c79356b 2571 case SIOCGIFGENERIC:
91447636 2572 case SIOCGIFDEVMTU:
91447636
A
2573 case SIOCGIFVLAN:
2574 case SIOCGIFBOND:
39236c6e
A
2575 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2576 break;
2577
2578 case SIOCGIFWAKEFLAGS:
2579 ifnet_lock_shared(ifp);
2580 ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
2581 ifnet_lock_done(ifp);
2582 break;
2583
2584 case SIOCGIFGETRTREFCNT:
2585 ifnet_lock_shared(ifp);
2586 ifr->ifr_route_refcnt = ifp->if_route_refcnt;
2587 ifnet_lock_done(ifp);
6d2010ae 2588 break;
1c79356b 2589
316670eb
A
2590 case SIOCSIFOPPORTUNISTIC:
2591 case SIOCGIFOPPORTUNISTIC:
2592 error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
b0d623f7 2593 break;
d1ecb069 2594
39236c6e
A
2595 case SIOCGIFLINKQUALITYMETRIC:
2596 ifnet_lock_shared(ifp);
3e170ce0
A
2597 if ((ifp->if_interface_state.valid_bitmask &
2598 IF_INTERFACE_STATE_LQM_STATE_VALID))
2599 ifr->ifr_link_quality_metric =
2600 ifp->if_interface_state.lqm_state;
2601 else if ((ifp->if_refflags & IFRF_ATTACHED)) {
2602 ifr->ifr_link_quality_metric =
2603 IFNET_LQM_THRESH_UNKNOWN;
2604 } else {
2605 ifr->ifr_link_quality_metric =
2606 IFNET_LQM_THRESH_OFF;
2607 }
39236c6e
A
2608 ifnet_lock_done(ifp);
2609 break;
2610
2611 case SIOCSIFLOG:
2612 case SIOCGIFLOG:
2613 error = ifnet_getset_log(ifp, cmd, ifr, p);
2614 break;
2615
2616 case SIOCGIFDELEGATE:
2617 ifnet_lock_shared(ifp);
2618 ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
2619 ifp->if_delegated.ifp->if_index : 0);
2620 ifnet_lock_done(ifp);
2621 break;
2622
fe8ab488
A
2623 case SIOCGIFEXPENSIVE:
2624 ifnet_lock_shared(ifp);
2625 if (ifp->if_eflags & IFEF_EXPENSIVE)
2626 ifr->ifr_expensive = 1;
2627 else
2628 ifr->ifr_expensive = 0;
2629 ifnet_lock_done(ifp);
2630 break;
2631
2632 case SIOCSIFEXPENSIVE:
2633 {
2634 struct ifnet *difp;
2635
2636 if ((error = priv_check_cred(kauth_cred_get(),
2637 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
2638 return (error);
2639 ifnet_lock_exclusive(ifp);
2640 if (ifr->ifr_expensive)
2641 ifp->if_eflags |= IFEF_EXPENSIVE;
2642 else
2643 ifp->if_eflags &= ~IFEF_EXPENSIVE;
2644 ifnet_lock_done(ifp);
2645 /*
2646 * Update the expensive bit in the delegated interface
2647 * structure.
2648 */
2649 ifnet_head_lock_shared();
2650 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
2651 ifnet_lock_exclusive(difp);
2652 if (difp->if_delegated.ifp == ifp) {
2653 difp->if_delegated.expensive =
2654 ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
2655
2656 }
2657 ifnet_lock_done(difp);
2658 }
2659 ifnet_head_done();
2660 break;
2661 }
2662
2663 case SIOCGIF2KCL:
2664 ifnet_lock_shared(ifp);
2665 if (ifp->if_eflags & IFEF_2KCL)
2666 ifr->ifr_2kcl = 1;
2667 else
2668 ifr->ifr_2kcl = 0;
2669 ifnet_lock_done(ifp);
2670 break;
2671
2672 case SIOCSIF2KCL:
2673 if ((error = priv_check_cred(kauth_cred_get(),
2674 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
2675 return (error);
2676 ifnet_lock_exclusive(ifp);
2677 if (ifr->ifr_2kcl)
2678 ifp->if_eflags |= IFEF_2KCL;
2679 else
2680 ifp->if_eflags &= ~IFEF_2KCL;
2681 ifnet_lock_done(ifp);
2682 break;
3e170ce0
A
2683 case SIOCGSTARTDELAY:
2684 ifnet_lock_shared(ifp);
2685 if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
2686 ifr->ifr_start_delay_qlen =
2687 ifp->if_start_delay_qlen;
2688 ifr->ifr_start_delay_timeout =
2689 ifp->if_start_delay_timeout;
2690 } else {
2691 ifr->ifr_start_delay_qlen = 0;
2692 ifr->ifr_start_delay_timeout = 0;
2693 }
2694 ifnet_lock_done(ifp);
2695 break;
316670eb
A
2696 case SIOCSIFDSTADDR:
2697 case SIOCSIFADDR:
2698 case SIOCSIFBRDADDR:
2699 case SIOCSIFNETMASK:
2700 case OSIOCGIFADDR:
2701 case OSIOCGIFDSTADDR:
2702 case OSIOCGIFBRDADDR:
2703 case OSIOCGIFNETMASK:
2704 case SIOCSIFKPI:
2705 VERIFY(so->so_proto != NULL);
2706
2707 if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
2708 cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
1c79356b
A
2709#if BYTE_ORDER != BIG_ENDIAN
2710 if (ifr->ifr_addr.sa_family == 0 &&
2711 ifr->ifr_addr.sa_len < 16) {
2712 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2713 ifr->ifr_addr.sa_len = 16;
2714 }
2715#else
2716 if (ifr->ifr_addr.sa_len == 0)
2717 ifr->ifr_addr.sa_len = 16;
2718#endif
316670eb
A
2719 } else if (cmd == OSIOCGIFADDR) {
2720 cmd = SIOCGIFADDR; /* struct ifreq */
2721 } else if (cmd == OSIOCGIFDSTADDR) {
2722 cmd = SIOCGIFDSTADDR; /* struct ifreq */
2723 } else if (cmd == OSIOCGIFBRDADDR) {
2724 cmd = SIOCGIFBRDADDR; /* struct ifreq */
2725 } else if (cmd == OSIOCGIFNETMASK) {
2726 cmd = SIOCGIFNETMASK; /* struct ifreq */
1c79356b 2727 }
6d2010ae 2728
91447636 2729 socket_lock(so, 1);
6d2010ae 2730 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
316670eb 2731 (caddr_t)ifr, ifp, p));
91447636 2732 socket_unlock(so, 1);
1c79356b 2733
6d2010ae 2734 switch (ocmd) {
1c79356b
A
2735 case OSIOCGIFADDR:
2736 case OSIOCGIFDSTADDR:
2737 case OSIOCGIFBRDADDR:
2738 case OSIOCGIFNETMASK:
316670eb
A
2739 bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
2740 sizeof (u_short));
1c79356b 2741 }
316670eb 2742
2d21ac55
A
2743 if (cmd == SIOCSIFKPI) {
2744 int temperr = proc_suser(p);
2745 if (temperr != 0)
2746 error = temperr;
2747 }
1c79356b 2748
316670eb 2749 if (error == EOPNOTSUPP || error == ENOTSUP) {
39236c6e 2750 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
316670eb
A
2751 (caddr_t)ifr);
2752 }
6d2010ae 2753 break;
316670eb 2754
3e170ce0
A
2755 case SIOCGIFINTERFACESTATE:
2756 if_get_state(ifp, &ifr->ifr_interface_state);
2757
2758 break;
2759 case SIOCSIFINTERFACESTATE:
2760 if ((error = priv_check_cred(kauth_cred_get(),
2761 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
2762 return (error);
2763
2764 error = if_state_update(ifp, &ifr->ifr_interface_state);
2765
2766 break;
2767 case SIOCSIFPROBECONNECTIVITY:
2768 if ((error = priv_check_cred(kauth_cred_get(),
2769 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
2770 return (error);
2771 error = if_probe_connectivity(ifp,
2772 ifr->ifr_probe_connectivity);
2773 break;
2774 case SIOCGIFPROBECONNECTIVITY:
2775 if ((error = priv_check_cred(kauth_cred_get(),
2776 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
2777 return (error);
2778 if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY)
2779 ifr->ifr_probe_connectivity = 1;
2780 else
2781 ifr->ifr_probe_connectivity = 0;
2782 break;
316670eb
A
2783 default:
2784 VERIFY(0);
2785 /* NOTREACHED */
1c79356b 2786 }
316670eb 2787
6d2010ae 2788 return (error);
1c79356b
A
2789}
2790
91447636 2791int
2d21ac55 2792ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
91447636
A
2793{
2794 int error;
2795
2796 socket_unlock(so, 0);
2797 error = ifioctl(so, cmd, data, p);
2798 socket_lock(so, 0);
2799 return(error);
2800}
2801
1c79356b
A
2802/*
2803 * Set/clear promiscuous mode on interface ifp based on the truth value
2804 * of pswitch. The calls are reference counted so that only the first
2805 * "on" request actually has an effect, as does the final "off" request.
2806 * Results are undefined if the "off" and "on" requests are not matched.
2807 */
91447636
A
2808errno_t
2809ifnet_set_promiscuous(
2810 ifnet_t ifp,
2811 int pswitch)
1c79356b 2812{
91447636 2813 int error = 0;
6d2010ae
A
2814 int oldflags = 0;
2815 int newflags = 0;
1c79356b 2816
91447636 2817 ifnet_lock_exclusive(ifp);
9bccf70c 2818 oldflags = ifp->if_flags;
6d2010ae
A
2819 ifp->if_pcount += pswitch ? 1 : -1;
2820
2821 if (ifp->if_pcount > 0)
1c79356b 2822 ifp->if_flags |= IFF_PROMISC;
6d2010ae 2823 else
1c79356b 2824 ifp->if_flags &= ~IFF_PROMISC;
6d2010ae
A
2825
2826 newflags = ifp->if_flags;
91447636 2827 ifnet_lock_done(ifp);
6d2010ae
A
2828
2829 if (newflags != oldflags && (newflags & IFF_UP) != 0) {
2830 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
2831 if (error == 0) {
2832 rt_ifmsg(ifp);
2833 } else {
2834 ifnet_lock_exclusive(ifp);
2835 // revert the flags
2836 ifp->if_pcount -= pswitch ? 1 : -1;
2837 if (ifp->if_pcount > 0)
2838 ifp->if_flags |= IFF_PROMISC;
2839 else
2840 ifp->if_flags &= ~IFF_PROMISC;
2841 ifnet_lock_done(ifp);
2842 }
2843 }
2844
2845 if (newflags != oldflags) {
39236c6e
A
2846 log(LOG_INFO, "%s: promiscuous mode %s%s\n",
2847 if_name(ifp),
6d2010ae
A
2848 (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
2849 error != 0 ? " failed" : " succeeded");
91447636 2850 }
1c79356b
A
2851 return error;
2852}
2853
2854/*
2855 * Return interface configuration
2856 * of system. List may be used
2857 * in later ioctl's (above) to get
2858 * other information.
2859 */
2860/*ARGSUSED*/
2861static int
39236c6e 2862ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
1c79356b 2863{
91447636
A
2864 struct ifnet *ifp = NULL;
2865 struct ifaddr *ifa;
2866 struct ifreq ifr;
2867 int error = 0;
2868 size_t space;
39236c6e
A
2869 net_thread_marks_t marks;
2870
2871 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
6d2010ae 2872
3a60a9f5
A
2873 /*
2874 * Zero the ifr buffer to make sure we don't
2875 * disclose the contents of the stack.
2876 */
39236c6e 2877 bzero(&ifr, sizeof (struct ifreq));
3a60a9f5 2878
91447636
A
2879 space = *ret_space;
2880 ifnet_head_lock_shared();
39236c6e 2881 for (ifp = ifnet_head.tqh_first; space > sizeof (ifr) &&
6d2010ae 2882 ifp; ifp = ifp->if_link.tqe_next) {
1c79356b 2883 char workbuf[64];
91447636 2884 size_t ifnlen, addrs;
1c79356b 2885
39236c6e
A
2886 ifnlen = snprintf(workbuf, sizeof (workbuf),
2887 "%s", if_name(ifp));
2888 if (ifnlen + 1 > sizeof (ifr.ifr_name)) {
1c79356b 2889 error = ENAMETOOLONG;
9bccf70c 2890 break;
1c79356b 2891 } else {
2d21ac55 2892 strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
1c79356b 2893 }
6d2010ae 2894
91447636 2895 ifnet_lock_shared(ifp);
1c79356b 2896
9bccf70c
A
2897 addrs = 0;
2898 ifa = ifp->if_addrhead.tqh_first;
39236c6e 2899 for (; space > sizeof (ifr) && ifa;
9bccf70c 2900 ifa = ifa->ifa_link.tqe_next) {
6d2010ae 2901 struct sockaddr *sa;
39236c6e
A
2902 union {
2903 struct sockaddr sa;
2904 struct sockaddr_dl sdl;
2905 uint8_t buf[SOCK_MAXADDRLEN + 1];
2906 } u;
2907
2908 /*
2909 * Make sure to accomodate the largest possible
2910 * size of SA(if_lladdr)->sa_len.
2911 */
2912 _CASSERT(sizeof (u) == (SOCK_MAXADDRLEN + 1));
6d2010ae
A
2913
2914 IFA_LOCK(ifa);
2915 sa = ifa->ifa_addr;
39236c6e
A
2916 addrs++;
2917
2918 if (ifa == ifp->if_lladdr) {
2919 VERIFY(sa->sa_family == AF_LINK);
2920 bcopy(sa, &u, sa->sa_len);
6d2010ae 2921 IFA_UNLOCK(ifa);
39236c6e
A
2922 ifnet_guarded_lladdr_copy_bytes(ifp,
2923 LLADDR(&u.sdl), u.sdl.sdl_alen);
2924 IFA_LOCK(ifa);
2925 sa = &u.sa;
6d2010ae 2926 }
39236c6e 2927
b0d623f7 2928 if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
1c79356b 2929 struct osockaddr *osa =
316670eb 2930 (struct osockaddr *)(void *)&ifr.ifr_addr;
1c79356b
A
2931 ifr.ifr_addr = *sa;
2932 osa->sa_family = sa->sa_family;
6d2010ae
A
2933 error = copyout((caddr_t)&ifr, ifrp,
2934 sizeof (ifr));
39236c6e
A
2935 ifrp += sizeof (struct ifreq);
2936 } else if (sa->sa_len <= sizeof (*sa)) {
1c79356b 2937 ifr.ifr_addr = *sa;
6d2010ae
A
2938 error = copyout((caddr_t)&ifr, ifrp,
2939 sizeof (ifr));
39236c6e 2940 ifrp += sizeof (struct ifreq);
1c79356b 2941 } else {
6d2010ae 2942 if (space <
39236c6e 2943 sizeof (ifr) + sa->sa_len - sizeof (*sa)) {
6d2010ae 2944 IFA_UNLOCK(ifa);
1c79356b 2945 break;
6d2010ae 2946 }
39236c6e 2947 space -= sa->sa_len - sizeof (*sa);
6d2010ae
A
2948 error = copyout((caddr_t)&ifr, ifrp,
2949 sizeof (ifr.ifr_name));
91447636 2950 if (error == 0) {
39236c6e
A
2951 error = copyout((caddr_t)sa, (ifrp +
2952 offsetof(struct ifreq, ifr_addr)),
2953 sa->sa_len);
91447636 2954 }
6d2010ae
A
2955 ifrp += (sa->sa_len + offsetof(struct ifreq,
2956 ifr_addr));
1c79356b 2957 }
6d2010ae 2958 IFA_UNLOCK(ifa);
1c79356b
A
2959 if (error)
2960 break;
2961 space -= sizeof (ifr);
2962 }
91447636 2963 ifnet_lock_done(ifp);
6d2010ae 2964
9bccf70c
A
2965 if (error)
2966 break;
2967 if (!addrs) {
39236c6e 2968 bzero((caddr_t)&ifr.ifr_addr, sizeof (ifr.ifr_addr));
91447636 2969 error = copyout((caddr_t)&ifr, ifrp, sizeof (ifr));
9bccf70c
A
2970 if (error)
2971 break;
2972 space -= sizeof (ifr);
39236c6e 2973 ifrp += sizeof (struct ifreq);
9bccf70c 2974 }
1c79356b 2975 }
91447636
A
2976 ifnet_head_done();
2977 *ret_space -= space;
39236c6e 2978 net_thread_marks_pop(marks);
1c79356b
A
2979 return (error);
2980}
2981
2982/*
2983 * Just like if_promisc(), but for all-multicast-reception mode.
2984 */
2985int
2d21ac55 2986if_allmulti(struct ifnet *ifp, int onswitch)
1c79356b
A
2987{
2988 int error = 0;
91447636 2989 int modified = 0;
316670eb 2990
91447636 2991 ifnet_lock_exclusive(ifp);
1c79356b
A
2992
2993 if (onswitch) {
2994 if (ifp->if_amcount++ == 0) {
2995 ifp->if_flags |= IFF_ALLMULTI;
91447636 2996 modified = 1;
1c79356b
A
2997 }
2998 } else {
2999 if (ifp->if_amcount > 1) {
3000 ifp->if_amcount--;
3001 } else {
3002 ifp->if_amcount = 0;
3003 ifp->if_flags &= ~IFF_ALLMULTI;
91447636 3004 modified = 1;
1c79356b
A
3005 }
3006 }
91447636 3007 ifnet_lock_done(ifp);
316670eb 3008
91447636 3009 if (modified)
2d21ac55 3010 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
1c79356b
A
3011
3012 if (error == 0)
3013 rt_ifmsg(ifp);
3014 return error;
3015}
3016
6d2010ae
A
3017static struct ifmultiaddr *
3018ifma_alloc(int how)
3019{
3020 struct ifmultiaddr *ifma;
3021
3022 ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
3023 zalloc_noblock(ifma_zone);
3024
3025 if (ifma != NULL) {
3026 bzero(ifma, ifma_size);
3027 lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
3028 ifma->ifma_debug |= IFD_ALLOC;
3029 if (ifma_debug != 0) {
3030 ifma->ifma_debug |= IFD_DEBUG;
3031 ifma->ifma_trace = ifma_trace;
3032 }
3033 }
3034 return (ifma);
3035}
3036
3037static void
3038ifma_free(struct ifmultiaddr *ifma)
3039{
3040 IFMA_LOCK(ifma);
3041
3042 if (ifma->ifma_protospec != NULL) {
3043 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
3044 /* NOTREACHED */
3045 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3046 ifma->ifma_anoncnt != 0) {
3047 panic("%s: Freeing ifma=%p with outstanding anon req",
3048 __func__, ifma);
3049 /* NOTREACHED */
3050 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3051 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
3052 __func__, ifma, ifma->ifma_ifp);
3053 /* NOTREACHED */
3054 } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
3055 panic("%s: ifma %p cannot be freed", __func__, ifma);
3056 /* NOTREACHED */
3057 } else if (ifma->ifma_refcount != 0) {
3058 panic("%s: non-zero refcount ifma=%p", __func__, ifma);
3059 /* NOTREACHED */
3060 } else if (ifma->ifma_reqcnt != 0) {
3061 panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
3062 /* NOTREACHED */
3063 } else if (ifma->ifma_ifp != NULL) {
3064 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
3065 ifma->ifma_ifp, ifma);
3066 /* NOTREACHED */
3067 } else if (ifma->ifma_ll != NULL) {
3068 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
3069 ifma->ifma_ll, ifma);
3070 /* NOTREACHED */
3071 }
3072 ifma->ifma_debug &= ~IFD_ALLOC;
3073 if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
3074 (IFD_DEBUG | IFD_TRASHED)) {
3075 lck_mtx_lock(&ifma_trash_lock);
3076 TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
3077 ifma_trash_link);
3078 lck_mtx_unlock(&ifma_trash_lock);
3079 ifma->ifma_debug &= ~IFD_TRASHED;
3080 }
3081 IFMA_UNLOCK(ifma);
3082
3083 if (ifma->ifma_addr != NULL) {
3084 FREE(ifma->ifma_addr, M_IFADDR);
3085 ifma->ifma_addr = NULL;
3086 }
3087 lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
3088 zfree(ifma_zone, ifma);
3089}
3090
3091static void
3092ifma_trace(struct ifmultiaddr *ifma, int refhold)
3093{
3094 struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
3095 ctrace_t *tr;
3096 u_int32_t idx;
3097 u_int16_t *cnt;
3098
3099 if (!(ifma->ifma_debug & IFD_DEBUG)) {
3100 panic("%s: ifma %p has no debug structure", __func__, ifma);
3101 /* NOTREACHED */
3102 }
3103 if (refhold) {
3104 cnt = &ifma_dbg->ifma_refhold_cnt;
3105 tr = ifma_dbg->ifma_refhold;
3106 } else {
3107 cnt = &ifma_dbg->ifma_refrele_cnt;
3108 tr = ifma_dbg->ifma_refrele;
3109 }
3110
3111 idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
3112 ctrace_record(&tr[idx]);
3113}
3114
91447636 3115void
6d2010ae 3116ifma_addref(struct ifmultiaddr *ifma, int locked)
1c79356b 3117{
6d2010ae
A
3118 if (!locked)
3119 IFMA_LOCK(ifma);
3120 else
3121 IFMA_LOCK_ASSERT_HELD(ifma);
3122
3123 if (++ifma->ifma_refcount == 0) {
3124 panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
3125 /* NOTREACHED */
3126 } else if (ifma->ifma_trace != NULL) {
3127 (*ifma->ifma_trace)(ifma, TRUE);
3128 }
3129 if (!locked)
3130 IFMA_UNLOCK(ifma);
91447636 3131}
1c79356b 3132
91447636 3133void
6d2010ae
A
3134ifma_remref(struct ifmultiaddr *ifma)
3135{
3136 struct ifmultiaddr *ll;
3137
3138 IFMA_LOCK(ifma);
3139
3140 if (ifma->ifma_refcount == 0) {
3141 panic("%s: ifma=%p negative refcnt", __func__, ifma);
3142 /* NOTREACHED */
3143 } else if (ifma->ifma_trace != NULL) {
3144 (*ifma->ifma_trace)(ifma, FALSE);
3145 }
3146
3147 --ifma->ifma_refcount;
3148 if (ifma->ifma_refcount > 0) {
3149 IFMA_UNLOCK(ifma);
3150 return;
91447636 3151 }
6d2010ae
A
3152
3153 ll = ifma->ifma_ll;
3154 ifma->ifma_ifp = NULL;
3155 ifma->ifma_ll = NULL;
3156 IFMA_UNLOCK(ifma);
3157 ifma_free(ifma); /* deallocate it */
3158
3159 if (ll != NULL)
3160 IFMA_REMREF(ll);
3161}
3162
3163static void
3164if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3165{
3166 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3167 IFMA_LOCK_ASSERT_HELD(ifma);
3168
3169 if (ifma->ifma_ifp != ifp) {
3170 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
3171 ifma->ifma_ifp, ifp);
3172 /* NOTREACHED */
3173 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3174 panic("%s: Attempt to attach an already attached ifma=%p",
3175 __func__, ifma);
3176 /* NOTREACHED */
3177 } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3178 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
3179 /* NOTREACHED */
3180 } else if (ifma->ifma_debug & IFD_TRASHED) {
3181 panic("%s: Attempt to reattach a detached ifma=%p",
3182 __func__, ifma);
3183 /* NOTREACHED */
3184 }
3185
3186 ifma->ifma_reqcnt++;
3187 VERIFY(ifma->ifma_reqcnt == 1);
3188 IFMA_ADDREF_LOCKED(ifma);
3189 ifma->ifma_debug |= IFD_ATTACHED;
3190 if (anon) {
3191 ifma->ifma_anoncnt++;
3192 VERIFY(ifma->ifma_anoncnt == 1);
3193 ifma->ifma_flags |= IFMAF_ANONYMOUS;
3194 }
3195
3196 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
91447636
A
3197}
3198
91447636 3199static int
6d2010ae
A
3200if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3201{
3202 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3203 IFMA_LOCK_ASSERT_HELD(ifma);
3204
3205 if (ifma->ifma_reqcnt == 0) {
3206 panic("%s: ifma=%p negative reqcnt", __func__, ifma);
3207 /* NOTREACHED */
3208 } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3209 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
3210 /* NOTREACHED */
3211 } else if (anon && ifma->ifma_anoncnt == 0) {
3212 panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
3213 /* NOTREACHED */
3214 } else if (ifma->ifma_ifp != ifp) {
3215 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
3216 ifma->ifma_ifp, ifp);
3217 /* NOTREACHED */
3218 }
3219
3220 if (anon) {
3221 --ifma->ifma_anoncnt;
3222 if (ifma->ifma_anoncnt > 0)
3223 return (0);
3224 ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
3225 }
3226
3227 --ifma->ifma_reqcnt;
3228 if (ifma->ifma_reqcnt > 0)
3229 return (0);
3230
3231 if (ifma->ifma_protospec != NULL) {
3232 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
3233 /* NOTREACHED */
3234 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3235 ifma->ifma_anoncnt != 0) {
3236 panic("%s: Detaching ifma=%p with outstanding anon req",
3237 __func__, ifma);
3238 /* NOTREACHED */
3239 } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
3240 panic("%s: Attempt to detach an unattached address ifma=%p",
3241 __func__, ifma);
3242 /* NOTREACHED */
3243 } else if (ifma->ifma_debug & IFD_TRASHED) {
3244 panic("%s: ifma %p is already in trash list", __func__, ifma);
3245 /* NOTREACHED */
3246 }
3247
3248 /*
3249 * NOTE: Caller calls IFMA_REMREF
3250 */
3251 ifma->ifma_debug &= ~IFD_ATTACHED;
3252 LIST_REMOVE(ifma, ifma_link);
3253 if (LIST_EMPTY(&ifp->if_multiaddrs))
3254 ifp->if_updatemcasts = 0;
3255
3256 if (ifma->ifma_debug & IFD_DEBUG) {
3257 /* Become a regular mutex, just in case */
3258 IFMA_CONVERT_LOCK(ifma);
3259 lck_mtx_lock(&ifma_trash_lock);
3260 TAILQ_INSERT_TAIL(&ifma_trash_head,
3261 (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
3262 lck_mtx_unlock(&ifma_trash_lock);
3263 ifma->ifma_debug |= IFD_TRASHED;
3264 }
3265
3266 return (1);
3267}
3268
3269/*
3270 * Find an ifmultiaddr that matches a socket address on an interface.
3271 *
3272 * Caller is responsible for holding the ifnet_lock while calling
3273 * this function.
3274 */
3275static int
3276if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
3277 struct ifmultiaddr **retifma, int anon)
91447636
A
3278{
3279 struct ifmultiaddr *ifma;
6d2010ae
A
3280
3281 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
3282 ifma = LIST_NEXT(ifma, ifma_link)) {
3283 IFMA_LOCK_SPIN(ifma);
3284 if (!equal(sa, ifma->ifma_addr)) {
3285 IFMA_UNLOCK(ifma);
3286 continue;
3287 }
3288 if (anon) {
3289 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3290 ifma->ifma_anoncnt != 0);
3291 VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3292 ifma->ifma_anoncnt == 0);
3293 ifma->ifma_anoncnt++;
3294 if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3295 VERIFY(ifma->ifma_anoncnt == 1);
3296 ifma->ifma_flags |= IFMAF_ANONYMOUS;
91447636 3297 }
1c79356b 3298 }
6d2010ae
A
3299 if (!anon || ifma->ifma_anoncnt == 1) {
3300 ifma->ifma_reqcnt++;
3301 VERIFY(ifma->ifma_reqcnt > 1);
3302 }
3303 if (retifma != NULL) {
3304 *retifma = ifma;
3305 IFMA_ADDREF_LOCKED(ifma);
3306 }
3307 IFMA_UNLOCK(ifma);
3308 return (0);
1c79356b 3309 }
6d2010ae 3310 return (ENOENT);
91447636
A
3311}
3312
2d21ac55
A
3313/*
3314 * Radar 3642395, make sure all multicasts are in a standard format.
3315 */
3316static struct sockaddr*
316670eb 3317copy_and_normalize(const struct sockaddr *original)
2d21ac55 3318{
316670eb 3319 int alen = 0;
2d21ac55
A
3320 const u_char *aptr = NULL;
3321 struct sockaddr *copy = NULL;
3322 struct sockaddr_dl *sdl_new = NULL;
316670eb
A
3323 int len = 0;
3324
2d21ac55 3325 if (original->sa_family != AF_LINK &&
316670eb 3326 original->sa_family != AF_UNSPEC) {
2d21ac55 3327 /* Just make a copy */
316670eb
A
3328 MALLOC(copy, struct sockaddr*, original->sa_len,
3329 M_IFADDR, M_WAITOK);
2d21ac55
A
3330 if (copy != NULL)
3331 bcopy(original, copy, original->sa_len);
316670eb 3332 return (copy);
2d21ac55 3333 }
316670eb 3334
2d21ac55
A
3335 switch (original->sa_family) {
3336 case AF_LINK: {
316670eb
A
3337 const struct sockaddr_dl *sdl_original =
3338 (struct sockaddr_dl*)(uintptr_t)(size_t)original;
3339
3340 if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
3341 sdl_original->sdl_slen +
3342 offsetof(struct sockaddr_dl, sdl_data) >
3343 sdl_original->sdl_len)
3344 return (NULL);
3345
2d21ac55
A
3346 alen = sdl_original->sdl_alen;
3347 aptr = CONST_LLADDR(sdl_original);
3348 }
3349 break;
316670eb 3350
2d21ac55
A
3351 case AF_UNSPEC: {
3352 if (original->sa_len < ETHER_ADDR_LEN +
316670eb
A
3353 offsetof(struct sockaddr, sa_data)) {
3354 return (NULL);
2d21ac55 3355 }
316670eb 3356
2d21ac55
A
3357 alen = ETHER_ADDR_LEN;
3358 aptr = (const u_char*)original->sa_data;
3359 }
3360 break;
3361 }
316670eb 3362
2d21ac55 3363 if (alen == 0 || aptr == NULL)
316670eb
A
3364 return (NULL);
3365
2d21ac55
A
3366 len = alen + offsetof(struct sockaddr_dl, sdl_data);
3367 MALLOC(sdl_new, struct sockaddr_dl*, len, M_IFADDR, M_WAITOK);
316670eb 3368
2d21ac55
A
3369 if (sdl_new != NULL) {
3370 bzero(sdl_new, len);
3371 sdl_new->sdl_len = len;
3372 sdl_new->sdl_family = AF_LINK;
3373 sdl_new->sdl_alen = alen;
3374 bcopy(aptr, LLADDR(sdl_new), alen);
3375 }
316670eb
A
3376
3377 return ((struct sockaddr*)sdl_new);
2d21ac55
A
3378}
3379
91447636 3380/*
6d2010ae
A
3381 * Network-layer protocol domains which hold references to the underlying
3382 * link-layer record must use this routine.
91447636
A
3383 */
3384int
6d2010ae
A
3385if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
3386 struct ifmultiaddr **retifma)
3387{
3388 return (if_addmulti_common(ifp, sa, retifma, 0));
3389}
3390
3391/*
3392 * Anything other than network-layer protocol domains which hold references
3393 * to the underlying link-layer record must use this routine: SIOCADDMULTI
39236c6e 3394 * ioctl, ifnet_add_multicast(), if_bond.
6d2010ae
A
3395 */
3396int
3397if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
3398 struct ifmultiaddr **retifma)
3399{
3400 return (if_addmulti_common(ifp, sa, retifma, 1));
3401}
3402
3403/*
3404 * Register an additional multicast address with a network interface.
3405 *
3406 * - If the address is already present, bump the reference count on the
3407 * address and return.
3408 * - If the address is not link-layer, look up a link layer address.
3409 * - Allocate address structures for one or both addresses, and attach to the
3410 * multicast address list on the interface. If automatically adding a link
3411 * layer address, the protocol address will own a reference to the link
3412 * layer address, to be freed when it is freed.
3413 * - Notify the network device driver of an addition to the multicast address
3414 * list.
3415 *
3416 * 'sa' points to caller-owned memory with the desired multicast address.
3417 *
3418 * 'retifma' will be used to return a pointer to the resulting multicast
3419 * address reference, if desired.
3420 *
3421 * 'anon' indicates a link-layer address with no protocol address reference
3422 * made to it. Anything other than network-layer protocol domain requests
3423 * are considered as anonymous.
3424 */
3425static int
3426if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
3427 struct ifmultiaddr **retifma, int anon)
91447636
A
3428{
3429 struct sockaddr_storage storage;
3430 struct sockaddr *llsa = NULL;
2d21ac55 3431 struct sockaddr *dupsa = NULL;
6d2010ae 3432 int error = 0, ll_firstref = 0, lladdr;
2d21ac55 3433 struct ifmultiaddr *ifma = NULL;
91447636 3434 struct ifmultiaddr *llifma = NULL;
6d2010ae
A
3435
3436 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
3437 VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
3438 sa->sa_family == AF_LINK);
3439
2d21ac55
A
3440 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
3441 if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
3442 dupsa = copy_and_normalize(sa);
3443 if (dupsa == NULL) {
6d2010ae
A
3444 error = ENOMEM;
3445 goto cleanup;
2d21ac55
A
3446 }
3447 sa = dupsa;
3448 }
6d2010ae 3449
91447636 3450 ifnet_lock_exclusive(ifp);
6d2010ae
A
3451 if (!(ifp->if_flags & IFF_MULTICAST)) {
3452 error = EADDRNOTAVAIL;
3453 ifnet_lock_done(ifp);
2d21ac55
A
3454 goto cleanup;
3455 }
1c79356b 3456
6d2010ae
A
3457 /* If the address is already present, return a new reference to it */
3458 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
3459 ifnet_lock_done(ifp);
3460 if (error == 0)
3461 goto cleanup;
3462
1c79356b 3463 /*
6d2010ae
A
3464 * The address isn't already present; give the link layer a chance
3465 * to accept/reject it, and also find out which AF_LINK address this
3466 * maps to, if it isn't one already.
1c79356b 3467 */
6d2010ae
A
3468 error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
3469 sizeof (storage));
91447636 3470 if (error == 0 && storage.ss_len != 0) {
6d2010ae 3471 llsa = copy_and_normalize((struct sockaddr *)&storage);
2d21ac55
A
3472 if (llsa == NULL) {
3473 error = ENOMEM;
3474 goto cleanup;
3475 }
6d2010ae
A
3476
3477 llifma = ifma_alloc(M_WAITOK);
2d21ac55
A
3478 if (llifma == NULL) {
3479 error = ENOMEM;
3480 goto cleanup;
3481 }
91447636 3482 }
6d2010ae 3483
9bccf70c 3484 /* to be similar to FreeBSD */
6d2010ae 3485 if (error == EOPNOTSUPP)
9bccf70c 3486 error = 0;
6d2010ae 3487 else if (error != 0)
2d21ac55 3488 goto cleanup;
1c79356b 3489
91447636 3490 /* Allocate while we aren't holding any locks */
2d21ac55
A
3491 if (dupsa == NULL) {
3492 dupsa = copy_and_normalize(sa);
3493 if (dupsa == NULL) {
3494 error = ENOMEM;
3495 goto cleanup;
3496 }
3497 }
6d2010ae 3498 ifma = ifma_alloc(M_WAITOK);
2d21ac55
A
3499 if (ifma == NULL) {
3500 error = ENOMEM;
3501 goto cleanup;
3502 }
6d2010ae 3503
91447636
A
3504 ifnet_lock_exclusive(ifp);
3505 /*
3506 * Check again for the matching multicast.
3507 */
6d2010ae
A
3508 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
3509 if (error == 0) {
91447636 3510 ifnet_lock_done(ifp);
2d21ac55 3511 goto cleanup;
91447636 3512 }
1c79356b 3513
6d2010ae
A
3514 if (llifma != NULL) {
3515 VERIFY(!anon); /* must not get here if "anonymous" */
3516 if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
3517 FREE(llsa, M_IFADDR);
3518 llsa = NULL;
3519 ifma_free(llifma);
3520 llifma = NULL;
3521 VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
1c79356b 3522 } else {
6d2010ae 3523 ll_firstref = 1;
91447636
A
3524 llifma->ifma_addr = llsa;
3525 llifma->ifma_ifp = ifp;
6d2010ae
A
3526 IFMA_LOCK(llifma);
3527 if_attach_ifma(ifp, llifma, 0);
3528 /* add extra refcnt for ifma */
3529 IFMA_ADDREF_LOCKED(llifma);
3530 IFMA_UNLOCK(llifma);
91447636 3531 ifma->ifma_ll = llifma;
1c79356b
A
3532 }
3533 }
6d2010ae
A
3534
3535 /* "anonymous" request should not result in network address */
3536 VERIFY(!anon || ifma->ifma_ll == NULL);
3537
3538 ifma->ifma_addr = dupsa;
3539 ifma->ifma_ifp = ifp;
3540 IFMA_LOCK(ifma);
3541 if_attach_ifma(ifp, ifma, anon);
3542 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
3543 if (retifma != NULL) {
91447636 3544 *retifma = ifma;
6d2010ae 3545 IFMA_ADDREF_LOCKED(*retifma); /* for caller */
91447636 3546 }
6d2010ae
A
3547 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
3548 ifma->ifma_addr->sa_family == AF_LINK);
3549 IFMA_UNLOCK(ifma);
91447636 3550 ifnet_lock_done(ifp);
6d2010ae
A
3551
3552 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3553 IFMA_REMREF(ifma); /* for this routine */
91447636 3554
1c79356b
A
3555 /*
3556 * We are certain we have added something, so call down to the
6d2010ae
A
3557 * interface to let them know about it. Do this only for newly-
3558 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
1c79356b 3559 */
6d2010ae
A
3560 if (lladdr || ll_firstref)
3561 (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
3562
3563 if (ifp->if_updatemcasts > 0)
3564 ifp->if_updatemcasts = 0;
3565
3566 return (0);
3567
2d21ac55 3568cleanup:
6d2010ae
A
3569 if (ifma != NULL)
3570 ifma_free(ifma);
3571 if (dupsa != NULL)
2d21ac55 3572 FREE(dupsa, M_IFADDR);
6d2010ae
A
3573 if (llifma != NULL)
3574 ifma_free(llifma);
3575 if (llsa != NULL)
2d21ac55 3576 FREE(llsa, M_IFADDR);
6d2010ae
A
3577
3578 return (error);
1c79356b
A
3579}
3580
6d2010ae
A
3581/*
3582 * Delete a multicast group membership by network-layer group address.
3583 * This routine is deprecated.
3584 */
1c79356b 3585int
6d2010ae 3586if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
1c79356b 3587{
6d2010ae 3588 return (if_delmulti_common(NULL, ifp, sa, 0));
55e303ae 3589}
1c79356b 3590
55e303ae 3591/*
6d2010ae
A
3592 * Delete a multicast group membership by group membership pointer.
3593 * Network-layer protocol domains must use this routine.
55e303ae
A
3594 */
3595int
6d2010ae
A
3596if_delmulti_ifma(struct ifmultiaddr *ifma)
3597{
3598 return (if_delmulti_common(ifma, NULL, NULL, 0));
3599}
3600
3601/*
3602 * Anything other than network-layer protocol domains which hold references
3603 * to the underlying link-layer record must use this routine: SIOCDELMULTI
39236c6e 3604 * ioctl, ifnet_remove_multicast(), if_bond.
6d2010ae
A
3605 */
3606int
3607if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
3608{
3609 return (if_delmulti_common(NULL, ifp, sa, 1));
3610}
3611
3612/*
3613 * Delete a multicast group membership by network-layer group address.
3614 *
3615 * Returns ENOENT if the entry could not be found.
3616 */
3617static int
3618if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
3619 const struct sockaddr *sa, int anon)
55e303ae 3620{
2d21ac55 3621 struct sockaddr *dupsa = NULL;
6d2010ae
A
3622 int lastref, ll_lastref = 0, lladdr;
3623 struct ifmultiaddr *ll = NULL;
55e303ae 3624
6d2010ae
A
3625 /* sanity check for callers */
3626 VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
3627
3628 if (ifma != NULL)
3629 ifp = ifma->ifma_ifp;
3630
3631 if (sa != NULL &&
3632 (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
2d21ac55 3633 dupsa = copy_and_normalize(sa);
6d2010ae
A
3634 if (dupsa == NULL)
3635 return (ENOMEM);
2d21ac55
A
3636 sa = dupsa;
3637 }
6d2010ae 3638
91447636 3639 ifnet_lock_exclusive(ifp);
6d2010ae
A
3640 if (ifma == NULL) {
3641 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
3642 ifma = LIST_NEXT(ifma, ifma_link)) {
3643 IFMA_LOCK(ifma);
3644 if (!equal(sa, ifma->ifma_addr) ||
3645 (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
3646 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3647 ifma->ifma_anoncnt != 0);
3648 IFMA_UNLOCK(ifma);
3649 continue;
3650 }
3651 /* found; keep it locked */
55e303ae 3652 break;
6d2010ae
A
3653 }
3654 if (ifma == NULL) {
3655 if (dupsa != NULL)
3656 FREE(dupsa, M_IFADDR);
3657 ifnet_lock_done(ifp);
3658 return (ENOENT);
3659 }
3660 } else {
3661 IFMA_LOCK(ifma);
3662 }
3663 IFMA_LOCK_ASSERT_HELD(ifma);
3664 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
3665 lastref = if_detach_ifma(ifp, ifma, anon);
3666 VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
3667 ifma->ifma_reqcnt == 0));
3668 VERIFY(!anon || ifma->ifma_ll == NULL);
3669 ll = ifma->ifma_ll;
3670 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
3671 ifma->ifma_addr->sa_family == AF_LINK);
3672 IFMA_UNLOCK(ifma);
3673 if (lastref && ll != NULL) {
3674 IFMA_LOCK(ll);
3675 ll_lastref = if_detach_ifma(ifp, ll, 0);
3676 IFMA_UNLOCK(ll);
91447636 3677 }
91447636 3678 ifnet_lock_done(ifp);
6d2010ae
A
3679
3680 if (lastref)
3681 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3682
3683 if ((ll == NULL && lastref && lladdr) || ll_lastref) {
3684 /*
3685 * Make sure the interface driver is notified in the
3686 * case of a link layer mcast group being left. Do
3687 * this only for a AF_LINK/AF_UNSPEC address that has
3688 * been removed from the if_multiaddrs set.
3689 */
3690 ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
3691 }
3692
3693 if (lastref)
3694 IFMA_REMREF(ifma); /* for if_multiaddrs list */
3695 if (ll_lastref)
3696 IFMA_REMREF(ll); /* for if_multiaddrs list */
3697
3698 IFMA_REMREF(ifma); /* for this routine */
3699 if (dupsa != NULL)
2d21ac55 3700 FREE(dupsa, M_IFADDR);
1c79356b 3701
6d2010ae
A
3702 return (0);
3703}
9bccf70c 3704
1c79356b
A
3705/*
3706 * Shutdown all network activity. Used boot() when halting
3707 * system.
3708 */
2d21ac55
A
3709int
3710if_down_all(void)
1c79356b 3711{
91447636
A
3712 struct ifnet **ifp;
3713 u_int32_t count;
3714 u_int32_t i;
1c79356b 3715
4a3eedf9 3716 if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
91447636
A
3717 for (i = 0; i < count; i++) {
3718 if_down(ifp[i]);
4a3eedf9 3719 dlil_proto_unplumb_all(ifp[i]);
91447636
A
3720 }
3721 ifnet_list_free(ifp);
3722 }
4a3eedf9 3723
91447636 3724 return 0;
1c79356b 3725}
9bccf70c
A
3726
3727/*
3728 * Delete Routes for a Network Interface
3729 *
3730 * Called for each routing entry via the rnh->rnh_walktree() call above
3731 * to delete all route entries referencing a detaching network interface.
3732 *
3733 * Arguments:
3734 * rn pointer to node in the routing table
3735 * arg argument passed to rnh->rnh_walktree() - detaching interface
3736 *
3737 * Returns:
3738 * 0 successful
3739 * errno failed - reason indicated
3740 *
3741 */
3742static int
b0d623f7 3743if_rtdel(struct radix_node *rn, void *arg)
9bccf70c
A
3744{
3745 struct rtentry *rt = (struct rtentry *)rn;
3746 struct ifnet *ifp = arg;
3747 int err;
3748
b0d623f7
A
3749 if (rt == NULL)
3750 return (0);
3751 /*
3752 * Checking against RTF_UP protects against walktree
3753 * recursion problems with cloned routes.
3754 */
3755 RT_LOCK(rt);
3756 if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
9bccf70c 3757 /*
b0d623f7
A
3758 * Safe to drop rt_lock and use rt_key, rt_gateway,
3759 * since holding rnh_lock here prevents another thread
3760 * from calling rt_setgate() on this route.
9bccf70c 3761 */
b0d623f7 3762 RT_UNLOCK(rt);
91447636 3763 err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
b0d623f7 3764 rt_mask(rt), rt->rt_flags, NULL);
9bccf70c
A
3765 if (err) {
3766 log(LOG_WARNING, "if_rtdel: error %d\n", err);
3767 }
b0d623f7
A
3768 } else {
3769 RT_UNLOCK(rt);
9bccf70c 3770 }
9bccf70c
A
3771 return (0);
3772}
3773
3774/*
b0d623f7 3775 * Removes routing table reference to a given interface
9bccf70c
A
3776 * for a given protocol family
3777 */
b0d623f7
A
3778void
3779if_rtproto_del(struct ifnet *ifp, int protocol)
9bccf70c 3780{
3a60a9f5 3781 struct radix_node_head *rnh;
b0d623f7 3782
3a60a9f5
A
3783 if ((protocol <= AF_MAX) && (protocol >= 0) &&
3784 ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
b0d623f7 3785 lck_mtx_lock(rnh_lock);
9bccf70c 3786 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
b0d623f7 3787 lck_mtx_unlock(rnh_lock);
91447636
A
3788 }
3789}
3790
2d21ac55
A
3791static int
3792if_rtmtu(struct radix_node *rn, void *arg)
3793{
3794 struct rtentry *rt = (struct rtentry *)rn;
3795 struct ifnet *ifp = arg;
3796
b0d623f7 3797 RT_LOCK(rt);
2d21ac55
A
3798 if (rt->rt_ifp == ifp) {
3799 /*
3800 * Update the MTU of this entry only if the MTU
3801 * has not been locked (RTV_MTU is not set) and
3802 * if it was non-zero to begin with.
3803 */
3804 if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu)
3805 rt->rt_rmx.rmx_mtu = ifp->if_mtu;
3806 }
b0d623f7 3807 RT_UNLOCK(rt);
2d21ac55
A
3808
3809 return (0);
3810}
3811
3812/*
3813 * Update the MTU metric of all route entries in all protocol tables
3814 * associated with a particular interface; this is called when the
3815 * MTU of that interface has changed.
3816 */
3817static
3818void if_rtmtu_update(struct ifnet *ifp)
3819{
3820 struct radix_node_head *rnh;
3821 int p;
3822
3823 for (p = 0; p < AF_MAX + 1; p++) {
3824 if ((rnh = rt_tables[p]) == NULL)
3825 continue;
3826
b0d623f7 3827 lck_mtx_lock(rnh_lock);
2d21ac55 3828 (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
b0d623f7 3829 lck_mtx_unlock(rnh_lock);
2d21ac55 3830 }
39236c6e 3831 routegenid_update();
2d21ac55 3832}
91447636
A
3833
3834__private_extern__ void
6d2010ae
A
3835if_data_internal_to_if_data(struct ifnet *ifp,
3836 const struct if_data_internal *if_data_int, struct if_data *if_data)
91447636 3837{
6d2010ae
A
3838#pragma unused(ifp)
3839#define COPYFIELD(fld) if_data->fld = if_data_int->fld
91447636 3840#define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
6d2010ae 3841/* compiler will cast down to 32-bit */
316670eb
A
3842#define COPYFIELD32_ATOMIC(fld) do { \
3843 atomic_get_64(if_data->fld, \
3844 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
6d2010ae
A
3845} while (0)
3846
91447636
A
3847 COPYFIELD(ifi_type);
3848 COPYFIELD(ifi_typelen);
3849 COPYFIELD(ifi_physical);
3850 COPYFIELD(ifi_addrlen);
3851 COPYFIELD(ifi_hdrlen);
3852 COPYFIELD(ifi_recvquota);
3853 COPYFIELD(ifi_xmitquota);
3854 if_data->ifi_unused1 = 0;
3855 COPYFIELD(ifi_mtu);
3856 COPYFIELD(ifi_metric);
3857 if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
3858 if_data->ifi_baudrate = 0xFFFFFFFF;
6d2010ae 3859 } else {
91447636
A
3860 COPYFIELD32(ifi_baudrate);
3861 }
6d2010ae
A
3862
3863 COPYFIELD32_ATOMIC(ifi_ipackets);
3864 COPYFIELD32_ATOMIC(ifi_ierrors);
3865 COPYFIELD32_ATOMIC(ifi_opackets);
3866 COPYFIELD32_ATOMIC(ifi_oerrors);
3867 COPYFIELD32_ATOMIC(ifi_collisions);
3868 COPYFIELD32_ATOMIC(ifi_ibytes);
3869 COPYFIELD32_ATOMIC(ifi_obytes);
3870 COPYFIELD32_ATOMIC(ifi_imcasts);
3871 COPYFIELD32_ATOMIC(ifi_omcasts);
3872 COPYFIELD32_ATOMIC(ifi_iqdrops);
3873 COPYFIELD32_ATOMIC(ifi_noproto);
3874
3875 COPYFIELD(ifi_recvtiming);
3876 COPYFIELD(ifi_xmittiming);
316670eb 3877
b0d623f7
A
3878 if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
3879 if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
6d2010ae 3880
91447636 3881 if_data->ifi_lastchange.tv_sec += boottime_sec();
91447636
A
3882
3883 if_data->ifi_unused2 = 0;
3884 COPYFIELD(ifi_hwassist);
3885 if_data->ifi_reserved1 = 0;
3886 if_data->ifi_reserved2 = 0;
6d2010ae 3887#undef COPYFIELD32_ATOMIC
91447636
A
3888#undef COPYFIELD32
3889#undef COPYFIELD
3890}
3891
3892__private_extern__ void
6d2010ae
A
3893if_data_internal_to_if_data64(struct ifnet *ifp,
3894 const struct if_data_internal *if_data_int,
3895 struct if_data64 *if_data64)
91447636 3896{
6d2010ae
A
3897#pragma unused(ifp)
3898#define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
316670eb
A
3899#define COPYFIELD64_ATOMIC(fld) do { \
3900 atomic_get_64(if_data64->fld, \
3901 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
6d2010ae
A
3902} while (0)
3903
3904 COPYFIELD64(ifi_type);
3905 COPYFIELD64(ifi_typelen);
3906 COPYFIELD64(ifi_physical);
3907 COPYFIELD64(ifi_addrlen);
3908 COPYFIELD64(ifi_hdrlen);
3909 COPYFIELD64(ifi_recvquota);
3910 COPYFIELD64(ifi_xmitquota);
91447636 3911 if_data64->ifi_unused1 = 0;
6d2010ae
A
3912 COPYFIELD64(ifi_mtu);
3913 COPYFIELD64(ifi_metric);
3914 COPYFIELD64(ifi_baudrate);
3915
3916 COPYFIELD64_ATOMIC(ifi_ipackets);
3917 COPYFIELD64_ATOMIC(ifi_ierrors);
3918 COPYFIELD64_ATOMIC(ifi_opackets);
3919 COPYFIELD64_ATOMIC(ifi_oerrors);
3920 COPYFIELD64_ATOMIC(ifi_collisions);
3921 COPYFIELD64_ATOMIC(ifi_ibytes);
3922 COPYFIELD64_ATOMIC(ifi_obytes);
3923 COPYFIELD64_ATOMIC(ifi_imcasts);
3924 COPYFIELD64_ATOMIC(ifi_omcasts);
3925 COPYFIELD64_ATOMIC(ifi_iqdrops);
3926 COPYFIELD64_ATOMIC(ifi_noproto);
3927
3928 /* Note these two fields are actually 32 bit, so doing COPYFIELD64_ATOMIC will
3929 * cause them to be misaligned
3930 */
3931 COPYFIELD64(ifi_recvtiming);
3932 COPYFIELD64(ifi_xmittiming);
3933
b0d623f7
A
3934 if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
3935 if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
6d2010ae 3936
91447636 3937 if_data64->ifi_lastchange.tv_sec += boottime_sec();
91447636 3938
6d2010ae 3939#undef COPYFIELD64
9bccf70c 3940}
b0d623f7 3941
6d2010ae
A
3942__private_extern__ void
3943if_copy_traffic_class(struct ifnet *ifp,
3944 struct if_traffic_class *if_tc)
b0d623f7 3945{
316670eb
A
3946#define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
3947 atomic_get_64(if_tc->fld, \
3948 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
6d2010ae
A
3949} while (0)
3950
316670eb
A
3951 bzero(if_tc, sizeof (*if_tc));
3952 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
3953 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
3954 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
3955 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
6d2010ae
A
3956 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
3957 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
3958 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
3959 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
3960 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
3961 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
3962 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
3963 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
3964 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
3965 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
3966 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
3967 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
316670eb
A
3968 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
3969 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
3970 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
3971 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
6d2010ae
A
3972
3973#undef COPY_IF_TC_FIELD64_ATOMIC
3974}
b0d623f7 3975
316670eb
A
3976void
3977if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
3978{
3979#define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
3980 atomic_get_64(if_de->fld, \
3981 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
3982} while (0)
3983
3984 bzero(if_de, sizeof (*if_de));
3985 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
39236c6e
A
3986 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
3987 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
3988 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
316670eb
A
3989
3990#undef COPY_IF_DE_FIELD64_ATOMIC
3991}
3992
3993void
3994if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
3995{
3996#define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
3997 atomic_get_64(if_ps->ifi_tcp_##fld, \
3998 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
3999} while (0)
4000
4001#define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
4002 atomic_get_64(if_ps->ifi_udp_##fld, \
4003 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
4004} while (0)
4005
4006 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
4007 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
4008 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
4009 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
4010 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
4011 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
4012 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
4013 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
4014 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
4015 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
4016 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
4017 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
4018 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
4019 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
4020
4021 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
4022 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
4023 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
4024 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
4025 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
4026 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
4027 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
4028 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
4029
4030#undef COPY_IF_PS_TCP_FIELD64_ATOMIC
4031#undef COPY_IF_PS_UDP_FIELD64_ATOMIC
4032}
4033
4034void
4035if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
4036{
4037 bzero(if_rs, sizeof (*if_rs));
4038 if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1))
4039 return;
4040
4041 /* by now, ifnet will stay attached so if_inp must be valid */
4042 VERIFY(ifp->if_inp != NULL);
4043 bcopy(&ifp->if_inp->pstats, if_rs, sizeof (*if_rs));
4044
4045 /* Release the IO refcnt */
4046 ifnet_decr_iorefcnt(ifp);
4047}
6d2010ae
A
4048
4049struct ifaddr *
4050ifa_remref(struct ifaddr *ifa, int locked)
4051{
4052 if (!locked)
4053 IFA_LOCK_SPIN(ifa);
4054 else
4055 IFA_LOCK_ASSERT_HELD(ifa);
4056
4057 if (ifa->ifa_refcnt == 0)
b0d623f7 4058 panic("%s: ifa %p negative refcnt\n", __func__, ifa);
6d2010ae
A
4059 else if (ifa->ifa_trace != NULL)
4060 (*ifa->ifa_trace)(ifa, FALSE);
4061 if (--ifa->ifa_refcnt == 0) {
4062 if (ifa->ifa_debug & IFD_ATTACHED)
b0d623f7
A
4063 panic("ifa %p attached to ifp is being freed\n", ifa);
4064 /*
4065 * Some interface addresses are allocated either statically
39236c6e
A
4066 * or carved out of a larger block. Only free it if it was
4067 * allocated via MALLOC or via the corresponding per-address
4068 * family allocator. Otherwise, leave it alone.
b0d623f7
A
4069 */
4070 if (ifa->ifa_debug & IFD_ALLOC) {
6d2010ae
A
4071 if (ifa->ifa_free == NULL) {
4072 IFA_UNLOCK(ifa);
b0d623f7 4073 FREE(ifa, M_IFADDR);
6d2010ae
A
4074 } else {
4075 /* Become a regular mutex */
4076 IFA_CONVERT_LOCK(ifa);
4077 /* callee will unlock */
b0d623f7 4078 (*ifa->ifa_free)(ifa);
6d2010ae
A
4079 }
4080 } else {
4081 IFA_UNLOCK(ifa);
b0d623f7 4082 }
6d2010ae 4083 ifa = NULL;
b0d623f7 4084 }
6d2010ae
A
4085
4086 if (!locked && ifa != NULL)
4087 IFA_UNLOCK(ifa);
4088
4089 return (ifa);
b0d623f7
A
4090}
4091
4092void
6d2010ae 4093ifa_addref(struct ifaddr *ifa, int locked)
b0d623f7 4094{
6d2010ae
A
4095 if (!locked)
4096 IFA_LOCK_SPIN(ifa);
4097 else
4098 IFA_LOCK_ASSERT_HELD(ifa);
b0d623f7 4099
6d2010ae
A
4100 if (++ifa->ifa_refcnt == 0) {
4101 panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
4102 /* NOTREACHED */
4103 } else if (ifa->ifa_trace != NULL) {
b0d623f7 4104 (*ifa->ifa_trace)(ifa, TRUE);
6d2010ae
A
4105 }
4106 if (!locked)
4107 IFA_UNLOCK(ifa);
4108}
4109
4110void
4111ifa_lock_init(struct ifaddr *ifa)
4112{
4113 lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
4114}
4115
4116void
4117ifa_lock_destroy(struct ifaddr *ifa)
4118{
4119 IFA_LOCK_ASSERT_NOTHELD(ifa);
4120 lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
b0d623f7 4121}
39236c6e
A
4122
4123/*
4124 * 'i' group ioctls.
4125 *
4126 * The switch statement below does nothing at runtime, as it serves as a
4127 * compile time check to ensure that all of the socket 'i' ioctls (those
4128 * in the 'i' group going thru soo_ioctl) that are made available by the
4129 * networking stack is unique. This works as long as this routine gets
4130 * updated each time a new interface ioctl gets added.
4131 *
4132 * Any failures at compile time indicates duplicated ioctl values.
4133 */
4134static __attribute__((unused)) void
4135ifioctl_cassert(void)
4136{
4137 /*
4138 * This is equivalent to _CASSERT() and the compiler wouldn't
4139 * generate any instructions, thus for compile time only.
4140 */
4141 switch ((u_long)0) {
4142 case 0:
4143
4144 /* bsd/net/if_ppp.h */
4145 case SIOCGPPPSTATS:
4146 case SIOCGPPPCSTATS:
4147
4148#if INET6
4149 /* bsd/netinet6/in6_var.h */
4150 case SIOCSIFADDR_IN6:
4151 case SIOCGIFADDR_IN6:
4152 case SIOCSIFDSTADDR_IN6:
4153 case SIOCSIFNETMASK_IN6:
4154 case SIOCGIFDSTADDR_IN6:
4155 case SIOCGIFNETMASK_IN6:
4156 case SIOCDIFADDR_IN6:
4157 case SIOCAIFADDR_IN6_32:
4158 case SIOCAIFADDR_IN6_64:
4159 case SIOCSIFPHYADDR_IN6_32:
4160 case SIOCSIFPHYADDR_IN6_64:
4161 case SIOCGIFPSRCADDR_IN6:
4162 case SIOCGIFPDSTADDR_IN6:
4163 case SIOCGIFAFLAG_IN6:
4164 case SIOCGDRLST_IN6_32:
4165 case SIOCGDRLST_IN6_64:
4166 case SIOCGPRLST_IN6_32:
4167 case SIOCGPRLST_IN6_64:
4168 case OSIOCGIFINFO_IN6:
4169 case SIOCGIFINFO_IN6:
4170 case SIOCSNDFLUSH_IN6:
4171 case SIOCGNBRINFO_IN6_32:
4172 case SIOCGNBRINFO_IN6_64:
4173 case SIOCSPFXFLUSH_IN6:
4174 case SIOCSRTRFLUSH_IN6:
4175 case SIOCGIFALIFETIME_IN6:
4176 case SIOCSIFALIFETIME_IN6:
4177 case SIOCGIFSTAT_IN6:
4178 case SIOCGIFSTAT_ICMP6:
4179 case SIOCSDEFIFACE_IN6_32:
4180 case SIOCSDEFIFACE_IN6_64:
4181 case SIOCGDEFIFACE_IN6_32:
4182 case SIOCGDEFIFACE_IN6_64:
4183 case SIOCSIFINFO_FLAGS:
4184 case SIOCSSCOPE6:
4185 case SIOCGSCOPE6:
4186 case SIOCGSCOPE6DEF:
4187 case SIOCSIFPREFIX_IN6:
4188 case SIOCGIFPREFIX_IN6:
4189 case SIOCDIFPREFIX_IN6:
4190 case SIOCAIFPREFIX_IN6:
4191 case SIOCCIFPREFIX_IN6:
4192 case SIOCSGIFPREFIX_IN6:
4193 case SIOCPROTOATTACH_IN6_32:
4194 case SIOCPROTOATTACH_IN6_64:
4195 case SIOCPROTODETACH_IN6:
4196 case SIOCLL_START_32:
4197 case SIOCLL_START_64:
4198 case SIOCLL_STOP:
4199 case SIOCAUTOCONF_START:
4200 case SIOCAUTOCONF_STOP:
4201 case SIOCSETROUTERMODE_IN6:
4202 case SIOCLL_CGASTART_32:
4203 case SIOCLL_CGASTART_64:
4204#endif /* INET6 */
4205
4206 /* bsd/sys/sockio.h */
4207 case SIOCSIFADDR:
4208 case OSIOCGIFADDR:
4209 case SIOCSIFDSTADDR:
4210 case OSIOCGIFDSTADDR:
4211 case SIOCSIFFLAGS:
4212 case SIOCGIFFLAGS:
4213 case OSIOCGIFBRDADDR:
4214 case SIOCSIFBRDADDR:
4215 case OSIOCGIFCONF32:
4216 case OSIOCGIFCONF64:
4217 case OSIOCGIFNETMASK:
4218 case SIOCSIFNETMASK:
4219 case SIOCGIFMETRIC:
4220 case SIOCSIFMETRIC:
4221 case SIOCDIFADDR:
4222 case SIOCAIFADDR:
39236c6e
A
4223 case SIOCGIFADDR:
4224 case SIOCGIFDSTADDR:
4225 case SIOCGIFBRDADDR:
4226 case SIOCGIFCONF32:
4227 case SIOCGIFCONF64:
4228 case SIOCGIFNETMASK:
4229 case SIOCAUTOADDR:
4230 case SIOCAUTONETMASK:
4231 case SIOCARPIPLL:
4232 case SIOCADDMULTI:
4233 case SIOCDELMULTI:
4234 case SIOCGIFMTU:
4235 case SIOCSIFMTU:
4236 case SIOCGIFPHYS:
4237 case SIOCSIFPHYS:
4238 case SIOCSIFMEDIA:
4239 case SIOCGIFMEDIA32:
4240 case SIOCGIFMEDIA64:
4241 case SIOCSIFGENERIC:
4242 case SIOCGIFGENERIC:
4243 case SIOCRSLVMULTI:
4244 case SIOCSIFLLADDR:
4245 case SIOCGIFSTATUS:
4246 case SIOCSIFPHYADDR:
4247 case SIOCGIFPSRCADDR:
4248 case SIOCGIFPDSTADDR:
4249 case SIOCDIFPHYADDR:
39236c6e
A
4250 case SIOCGIFDEVMTU:
4251 case SIOCSIFALTMTU:
4252 case SIOCGIFALTMTU:
4253 case SIOCSIFBOND:
4254 case SIOCGIFBOND:
4255 case SIOCPROTOATTACH:
4256 case SIOCPROTODETACH:
4257 case SIOCSIFCAP:
4258 case SIOCGIFCAP:
4259 case SIOCIFCREATE:
4260 case SIOCIFDESTROY:
4261 case SIOCIFCREATE2:
4262 case SIOCSDRVSPEC32:
4263 case SIOCGDRVSPEC32:
4264 case SIOCSDRVSPEC64:
4265 case SIOCGDRVSPEC64:
4266 case SIOCSIFVLAN:
4267 case SIOCGIFVLAN:
4268 case SIOCIFGCLONERS32:
4269 case SIOCIFGCLONERS64:
4270 case SIOCGIFASYNCMAP:
4271 case SIOCSIFASYNCMAP:
4272#if CONFIG_MACF_NET
4273 case SIOCGIFMAC:
4274 case SIOCSIFMAC:
4275#endif /* CONFIG_MACF_NET */
4276 case SIOCSIFKPI:
4277 case SIOCGIFKPI:
4278 case SIOCGIFWAKEFLAGS:
4279 case SIOCGIFGETRTREFCNT:
4280 case SIOCGIFLINKQUALITYMETRIC:
4281 case SIOCSIFOPPORTUNISTIC:
4282 case SIOCGIFOPPORTUNISTIC:
4283 case SIOCSETROUTERMODE:
4284 case SIOCGIFEFLAGS:
4285 case SIOCSIFDESC:
4286 case SIOCGIFDESC:
4287 case SIOCSIFLINKPARAMS:
4288 case SIOCGIFLINKPARAMS:
4289 case SIOCGIFQUEUESTATS:
4290 case SIOCSIFTHROTTLE:
4291 case SIOCGIFTHROTTLE:
4292 case SIOCSIFLOG:
4293 case SIOCGIFLOG:
4294 case SIOCGIFDELEGATE:
4295 case SIOCGIFLLADDR:
4296 case SIOCGIFTYPE:
3e170ce0
A
4297 case SIOCGIFFUNCTIONALTYPE:
4298 case SIOCAIFAGENTID:
4299 case SIOCDIFAGENTID:
4300 case SIOCGIFAGENTIDS32:
4301 case SIOCGIFAGENTIDS64:
4302 case SIOCGIFAGENTDATA32:
4303 case SIOCGIFAGENTDATA64:
4304 case SIOCSIFINTERFACESTATE:
4305 case SIOCGIFINTERFACESTATE:
39236c6e
A
4306 ;
4307 }
4308}