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