]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if.c
6b00d6cad9a1cb8d5d277c1f00a2ef9986e0487b
[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
2194 u_int32_t ordered_count = if_ordered_count;
2195
2196 if (ifo->ifo_count == 0 ||
2197 ordered_count == 0) {
2198 ifo->ifo_count = ordered_count;
2199 } else if (ifo->ifo_ordered_indices != USER_ADDR_NULL) {
2200 u_int32_t count_to_copy =
2201 MIN(ordered_count, ifo->ifo_count);
2202 size_t length = (count_to_copy * sizeof(u_int32_t));
2203 struct ifnet *ifp = NULL;
2204 u_int32_t cursor = 0;
2205
2206 ordered_indices = _MALLOC(length, M_NECP, M_WAITOK);
2207 if (ordered_indices == NULL) {
2208 error = ENOMEM;
2209 break;
2210 }
2211
2212 ifnet_head_lock_shared();
2213 TAILQ_FOREACH(ifp, &ifnet_ordered_head, if_ordered_link) {
2214 if (cursor >= count_to_copy) {
2215 break;
2216 }
2217 ordered_indices[cursor] = ifp->if_index;
2218 cursor++;
2219 }
2220 ifnet_head_done();
2221
2222 ifo->ifo_count = count_to_copy;
2223 error = copyout(ordered_indices,
2224 ifo->ifo_ordered_indices, length);
2225 } else {
2226 error = EINVAL;
2227 }
2228 break;
2229 }
2230
2231 default: {
2232 VERIFY(0);
2233 /* NOTREACHED */
2234 }
2235 }
2236
2237 if (ordered_indices != NULL) {
2238 _FREE(ordered_indices, M_NECP);
2239 }
2240
2241 return (error);
2242 }
2243
2244 static __attribute__((noinline)) int
2245 ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
2246 {
2247 struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
2248 u_int16_t flags;
2249 int error = 0;
2250
2251 VERIFY(ifp != NULL);
2252
2253 switch (cmd) {
2254 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2255 if (ifnsr->ifnsr_len > sizeof (ifnsr->ifnsr_data)) {
2256 error = EINVAL;
2257 break;
2258 }
2259 bcopy(&ifnsr->ifnsr_flags, &flags, sizeof (flags));
2260 error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
2261 ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
2262 break;
2263
2264 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2265 ifnsr->ifnsr_len = sizeof (ifnsr->ifnsr_data);
2266 error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
2267 &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
2268 if (error == 0)
2269 bcopy(&flags, &ifnsr->ifnsr_flags, sizeof (flags));
2270 else
2271 ifnsr->ifnsr_len = 0;
2272 break;
2273
2274 default:
2275 VERIFY(0);
2276 /* NOTREACHED */
2277 }
2278
2279 return (error);
2280 }
2281
2282 #if INET6
2283 static __attribute__((noinline)) int
2284 ifioctl_nat64prefix(struct ifnet *ifp, u_long cmd, caddr_t data)
2285 {
2286 struct if_nat64req *ifnat64 = (struct if_nat64req *)(void *)data;
2287 int error = 0;
2288
2289 VERIFY(ifp != NULL);
2290
2291 switch (cmd) {
2292 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
2293 error = ifnet_set_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2294 break;
2295
2296 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
2297 error = ifnet_get_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2298 break;
2299
2300 default:
2301 VERIFY(0);
2302 /* NOTREACHED */
2303 }
2304
2305 return (error);
2306 }
2307 #endif
2308
2309
2310 /*
2311 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2312 */
2313 static bool
2314 ifioctl_restrict_intcoproc(unsigned long cmd, const char *ifname,
2315 struct ifnet *ifp, struct proc *p)
2316 {
2317
2318 if (intcoproc_unrestricted == TRUE) {
2319 return (false);
2320 }
2321 if (proc_pid(p) == 0) {
2322 return (false);
2323 }
2324 if (ifname) {
2325 ifp = ifunit(ifname);
2326 }
2327 if (ifp == NULL) {
2328 return (false);
2329 }
2330 if (!IFNET_IS_INTCOPROC(ifp)) {
2331 return (false);
2332 }
2333 switch (cmd) {
2334 case SIOCGIFBRDADDR:
2335 case SIOCGIFCONF32:
2336 case SIOCGIFCONF64:
2337 case SIOCGIFFLAGS:
2338 case SIOCGIFEFLAGS:
2339 case SIOCGIFCAP:
2340 case SIOCGIFMAC:
2341 case SIOCGIFMETRIC:
2342 case SIOCGIFMTU:
2343 case SIOCGIFPHYS:
2344 case SIOCGIFTYPE:
2345 case SIOCGIFFUNCTIONALTYPE:
2346 case SIOCGIFPSRCADDR:
2347 case SIOCGIFPDSTADDR:
2348 case SIOCGIFGENERIC:
2349 case SIOCGIFDEVMTU:
2350 case SIOCGIFVLAN:
2351 case SIOCGIFBOND:
2352 case SIOCGIFWAKEFLAGS:
2353 case SIOCGIFGETRTREFCNT:
2354 case SIOCGIFOPPORTUNISTIC:
2355 case SIOCGIFLINKQUALITYMETRIC:
2356 case SIOCGIFLOG:
2357 case SIOCGIFDELEGATE:
2358 case SIOCGIFEXPENSIVE:
2359 case SIOCGIFINTERFACESTATE:
2360 case SIOCGIFPROBECONNECTIVITY:
2361 case SIOCGIFTIMESTAMPENABLED:
2362 case SIOCGECNMODE:
2363 case SIOCGQOSMARKINGMODE:
2364 case SIOCGQOSMARKINGENABLED:
2365 case SIOCGIFLOWINTERNET:
2366 case SIOCGIFSTATUS:
2367 case SIOCGIFMEDIA32:
2368 case SIOCGIFMEDIA64:
2369 case SIOCGIFDESC:
2370 case SIOCGIFLINKPARAMS:
2371 case SIOCGIFQUEUESTATS:
2372 case SIOCGIFTHROTTLE:
2373 case SIOCGIFAGENTIDS32:
2374 case SIOCGIFAGENTIDS64:
2375 case SIOCGIFNETSIGNATURE:
2376 case SIOCGIFINFO_IN6:
2377 case SIOCGIFAFLAG_IN6:
2378 case SIOCGNBRINFO_IN6:
2379 case SIOCGIFALIFETIME_IN6:
2380 case SIOCGIFNETMASK_IN6:
2381 return (false);
2382 default:
2383 #if (DEBUG || DEVELOPMENT)
2384 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2385 __func__, cmd, proc_pid(p));
2386 #endif
2387 return (true);
2388 }
2389 return (false);
2390 }
2391
2392 /*
2393 * Interface ioctls.
2394 *
2395 * Most of the routines called to handle the ioctls would end up being
2396 * tail-call optimized, which unfortunately causes this routine to
2397 * consume too much stack space; this is the reason for the "noinline"
2398 * attribute used on those routines.
2399 */
2400 int
2401 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
2402 {
2403 char ifname[IFNAMSIZ + 1];
2404 struct ifnet *ifp = NULL;
2405 struct ifstat *ifs = NULL;
2406 int error = 0;
2407
2408 bzero(ifname, sizeof (ifname));
2409
2410 /*
2411 * ioctls which don't require ifp, or ifreq ioctls
2412 */
2413 switch (cmd) {
2414 case OSIOCGIFCONF32: /* struct ifconf32 */
2415 case SIOCGIFCONF32: /* struct ifconf32 */
2416 case SIOCGIFCONF64: /* struct ifconf64 */
2417 case OSIOCGIFCONF64: /* struct ifconf64 */
2418 error = ifioctl_ifconf(cmd, data);
2419 goto done;
2420
2421 case SIOCIFGCLONERS32: /* struct if_clonereq32 */
2422 case SIOCIFGCLONERS64: /* struct if_clonereq64 */
2423 error = ifioctl_ifclone(cmd, data);
2424 goto done;
2425
2426 case SIOCGIFAGENTDATA32: /* struct netagent_req32 */
2427 case SIOCGIFAGENTDATA64: /* struct netagent_req64 */
2428 case SIOCGIFAGENTLIST32: /* struct netagentlist_req32 */
2429 case SIOCGIFAGENTLIST64: /* struct netagentlist_req64 */
2430 error = netagent_ioctl(cmd, data);
2431 goto done;
2432
2433 case SIOCSIFORDER: /* struct if_order */
2434 case SIOCGIFORDER: /* struct if_order */
2435 error = ifioctl_iforder(cmd, data);
2436 goto done;
2437
2438 case SIOCSIFDSTADDR: /* struct ifreq */
2439 case SIOCSIFADDR: /* struct ifreq */
2440 case SIOCSIFBRDADDR: /* struct ifreq */
2441 case SIOCSIFNETMASK: /* struct ifreq */
2442 case OSIOCGIFADDR: /* struct ifreq */
2443 case OSIOCGIFDSTADDR: /* struct ifreq */
2444 case OSIOCGIFBRDADDR: /* struct ifreq */
2445 case OSIOCGIFNETMASK: /* struct ifreq */
2446 case SIOCSIFKPI: /* struct ifreq */
2447 if (so->so_proto == NULL) {
2448 error = EOPNOTSUPP;
2449 goto done;
2450 }
2451 /* FALLTHRU */
2452 case SIOCIFCREATE: /* struct ifreq */
2453 case SIOCIFCREATE2: /* struct ifreq */
2454 case SIOCIFDESTROY: /* struct ifreq */
2455 case SIOCGIFFLAGS: /* struct ifreq */
2456 case SIOCGIFEFLAGS: /* struct ifreq */
2457 case SIOCGIFCAP: /* struct ifreq */
2458 #if CONFIG_MACF_NET
2459 case SIOCGIFMAC: /* struct ifreq */
2460 case SIOCSIFMAC: /* struct ifreq */
2461 #endif /* CONFIG_MACF_NET */
2462 case SIOCGIFMETRIC: /* struct ifreq */
2463 case SIOCGIFMTU: /* struct ifreq */
2464 case SIOCGIFPHYS: /* struct ifreq */
2465 case SIOCSIFFLAGS: /* struct ifreq */
2466 case SIOCSIFCAP: /* struct ifreq */
2467 case SIOCSIFMETRIC: /* struct ifreq */
2468 case SIOCSIFPHYS: /* struct ifreq */
2469 case SIOCSIFMTU: /* struct ifreq */
2470 case SIOCADDMULTI: /* struct ifreq */
2471 case SIOCDELMULTI: /* struct ifreq */
2472 case SIOCDIFPHYADDR: /* struct ifreq */
2473 case SIOCSIFMEDIA: /* struct ifreq */
2474 case SIOCSIFGENERIC: /* struct ifreq */
2475 case SIOCSIFLLADDR: /* struct ifreq */
2476 case SIOCSIFALTMTU: /* struct ifreq */
2477 case SIOCSIFVLAN: /* struct ifreq */
2478 case SIOCSIFBOND: /* struct ifreq */
2479 case SIOCGIFLLADDR: /* struct ifreq */
2480 case SIOCGIFTYPE: /* struct ifreq */
2481 case SIOCGIFFUNCTIONALTYPE: /* struct ifreq */
2482 case SIOCGIFPSRCADDR: /* struct ifreq */
2483 case SIOCGIFPDSTADDR: /* struct ifreq */
2484 case SIOCGIFGENERIC: /* struct ifreq */
2485 case SIOCGIFDEVMTU: /* struct ifreq */
2486 case SIOCGIFVLAN: /* struct ifreq */
2487 case SIOCGIFBOND: /* struct ifreq */
2488 case SIOCGIFWAKEFLAGS: /* struct ifreq */
2489 case SIOCGIFGETRTREFCNT: /* struct ifreq */
2490 case SIOCSIFOPPORTUNISTIC: /* struct ifreq */
2491 case SIOCGIFOPPORTUNISTIC: /* struct ifreq */
2492 case SIOCGIFLINKQUALITYMETRIC: /* struct ifreq */
2493 case SIOCSIFLOG: /* struct ifreq */
2494 case SIOCGIFLOG: /* struct ifreq */
2495 case SIOCGIFDELEGATE: /* struct ifreq */
2496 case SIOCGIFEXPENSIVE: /* struct ifreq */
2497 case SIOCSIFEXPENSIVE: /* struct ifreq */
2498 case SIOCSIF2KCL: /* struct ifreq */
2499 case SIOCGIF2KCL: /* struct ifreq */
2500 case SIOCSIFINTERFACESTATE: /* struct ifreq */
2501 case SIOCGIFINTERFACESTATE: /* struct ifreq */
2502 case SIOCSIFPROBECONNECTIVITY: /* struct ifreq */
2503 case SIOCGIFPROBECONNECTIVITY: /* struct ifreq */
2504 case SIOCGSTARTDELAY: /* struct ifreq */
2505 case SIOCSIFTIMESTAMPENABLE: /* struct ifreq */
2506 case SIOCSIFTIMESTAMPDISABLE: /* struct ifreq */
2507 case SIOCGIFTIMESTAMPENABLED: /* struct ifreq */
2508 #if (DEBUG || DEVELOPMENT)
2509 case SIOCSIFDISABLEOUTPUT: /* struct ifreq */
2510 #endif /* (DEBUG || DEVELOPMENT) */
2511 case SIOCGECNMODE: /* struct ifreq */
2512 case SIOCSECNMODE:
2513 case SIOCSQOSMARKINGMODE: /* struct ifreq */
2514 case SIOCSQOSMARKINGENABLED: /* struct ifreq */
2515 case SIOCGQOSMARKINGMODE: /* struct ifreq */
2516 case SIOCGQOSMARKINGENABLED: /* struct ifreq */
2517 case SIOCSIFLOWINTERNET: /* struct ifreq */
2518 case SIOCGIFLOWINTERNET: /* struct ifreq */
2519 { /* struct ifreq */
2520 struct ifreq ifr;
2521 bcopy(data, &ifr, sizeof (ifr));
2522 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2523 bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
2524 if (ifioctl_restrict_intcoproc(cmd, ifname, NULL, p) == true) {
2525 error = EPERM;
2526 goto done;
2527 }
2528 error = ifioctl_ifreq(so, cmd, &ifr, p);
2529 bcopy(&ifr, data, sizeof (ifr));
2530 goto done;
2531 }
2532 }
2533
2534 /*
2535 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2536 * here to ensure that the ifnet, if found, has been fully attached.
2537 */
2538 dlil_if_lock();
2539 switch (cmd) {
2540 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2541 bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
2542 ifname, IFNAMSIZ);
2543 ifp = ifunit(ifname);
2544 break;
2545
2546 #if INET6
2547 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
2548 bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
2549 ifname, IFNAMSIZ);
2550 ifp = ifunit(ifname);
2551 break;
2552
2553 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
2554 bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
2555 ifname, IFNAMSIZ);
2556 ifp = ifunit(ifname);
2557 break;
2558 #endif /* INET6 */
2559
2560 case SIOCGIFSTATUS: /* struct ifstat */
2561 ifs = _MALLOC(sizeof (*ifs), M_DEVBUF, M_WAITOK);
2562 if (ifs == NULL) {
2563 error = ENOMEM;
2564 dlil_if_unlock();
2565 goto done;
2566 }
2567 bcopy(data, ifs, sizeof (*ifs));
2568 ifs->ifs_name[IFNAMSIZ - 1] = '\0';
2569 bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
2570 ifp = ifunit(ifname);
2571 break;
2572
2573 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
2574 bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
2575 ifname, IFNAMSIZ);
2576 ifp = ifunit(ifname);
2577 break;
2578
2579 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
2580 bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
2581 ifname, IFNAMSIZ);
2582 ifp = ifunit(ifname);
2583 break;
2584
2585 case SIOCSIFDESC: /* struct if_descreq */
2586 case SIOCGIFDESC: /* struct if_descreq */
2587 bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
2588 ifname, IFNAMSIZ);
2589 ifp = ifunit(ifname);
2590 break;
2591
2592 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
2593 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
2594 bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
2595 ifname, IFNAMSIZ);
2596 ifp = ifunit(ifname);
2597 break;
2598
2599 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
2600 bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
2601 ifname, IFNAMSIZ);
2602 ifp = ifunit(ifname);
2603 break;
2604
2605 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
2606 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
2607 bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
2608 ifname, IFNAMSIZ);
2609 ifp = ifunit(ifname);
2610 break;
2611
2612 case SIOCAIFAGENTID: /* struct if_agentidreq */
2613 case SIOCDIFAGENTID: /* struct if_agentidreq */
2614 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2615 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2616 bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
2617 ifname, IFNAMSIZ);
2618 ifp = ifunit(ifname);
2619 break;
2620
2621 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2622 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2623 bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
2624 ifname, IFNAMSIZ);
2625 ifp = ifunit(ifname);
2626 break;
2627
2628 default:
2629 /*
2630 * This is a bad assumption, but the code seems to
2631 * have been doing this in the past; caveat emptor.
2632 */
2633 bcopy(((struct ifreq *)(void *)data)->ifr_name,
2634 ifname, IFNAMSIZ);
2635 ifp = ifunit(ifname);
2636 break;
2637 }
2638 dlil_if_unlock();
2639
2640 if (ifp == NULL) {
2641 error = ENXIO;
2642 goto done;
2643 }
2644
2645 if (ifioctl_restrict_intcoproc(cmd, NULL, ifp, p) == true) {
2646 error = EPERM;
2647 goto done;
2648 }
2649 switch (cmd) {
2650 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2651 #if INET6
2652 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
2653 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
2654 #endif /* INET6 */
2655 error = proc_suser(p);
2656 if (error != 0)
2657 break;
2658
2659 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2660 if (error != 0)
2661 break;
2662
2663 ifnet_touch_lastchange(ifp);
2664 break;
2665
2666 case SIOCGIFSTATUS: /* struct ifstat */
2667 VERIFY(ifs != NULL);
2668 ifs->ascii[0] = '\0';
2669
2670 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
2671
2672 bcopy(ifs, data, sizeof (*ifs));
2673 break;
2674
2675 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
2676 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
2677 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2678 break;
2679
2680 case SIOCSIFDESC: /* struct if_descreq */
2681 case SIOCGIFDESC: /* struct if_descreq */
2682 error = ifioctl_ifdesc(ifp, cmd, data, p);
2683 break;
2684
2685 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
2686 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
2687 error = ifioctl_linkparams(ifp, cmd, data, p);
2688 break;
2689
2690 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
2691 error = ifioctl_qstats(ifp, cmd, data);
2692 break;
2693
2694 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
2695 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
2696 error = ifioctl_throttle(ifp, cmd, data, p);
2697 break;
2698
2699 case SIOCAIFAGENTID: /* struct if_agentidreq */
2700 case SIOCDIFAGENTID: /* struct if_agentidreq */
2701 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2702 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2703 error = ifioctl_netagent(ifp, cmd, data, p);
2704 break;
2705
2706 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2707 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2708 error = ifioctl_netsignature(ifp, cmd, data);
2709 break;
2710
2711 #if INET6
2712 case SIOCSIFNAT64PREFIX: /* struct if_nsreq */
2713 case SIOCGIFNAT64PREFIX: /* struct if_nsreq */
2714 error = ifioctl_nat64prefix(ifp, cmd, data);
2715 break;
2716 #endif
2717 default:
2718 if (so->so_proto == NULL) {
2719 error = EOPNOTSUPP;
2720 break;
2721 }
2722
2723 socket_lock(so, 1);
2724 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2725 data, ifp, p));
2726 socket_unlock(so, 1);
2727
2728 if (error == EOPNOTSUPP || error == ENOTSUP) {
2729 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2730 }
2731 break;
2732 }
2733
2734 done:
2735 if (ifs != NULL)
2736 _FREE(ifs, M_DEVBUF);
2737
2738 if (if_verbose) {
2739 if (ifname[0] == '\0')
2740 (void) snprintf(ifname, sizeof (ifname), "%s",
2741 "NULL");
2742 else if (ifp != NULL)
2743 (void) snprintf(ifname, sizeof (ifname), "%s",
2744 if_name(ifp));
2745
2746 if (error != 0) {
2747 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2748 "%c %lu) error %d\n", __func__,
2749 proc_name_address(p), proc_pid(p),
2750 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2751 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2752 (char)IOCGROUP(cmd), cmd & 0xff, error);
2753 } else if (if_verbose > 1) {
2754 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2755 "%c %lu) OK\n", __func__,
2756 proc_name_address(p), proc_pid(p),
2757 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2758 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2759 (char)IOCGROUP(cmd), cmd & 0xff);
2760 }
2761 }
2762
2763 return (error);
2764 }
2765
2766 static __attribute__((noinline)) int
2767 ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
2768 {
2769 struct ifnet *ifp;
2770 u_long ocmd = cmd;
2771 int error = 0;
2772 struct kev_msg ev_msg;
2773 struct net_event_data ev_data;
2774
2775 bzero(&ev_data, sizeof (struct net_event_data));
2776 bzero(&ev_msg, sizeof (struct kev_msg));
2777
2778 switch (cmd) {
2779 case SIOCIFCREATE:
2780 case SIOCIFCREATE2:
2781 error = proc_suser(p);
2782 if (error)
2783 return (error);
2784 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2785 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2786 case SIOCIFDESTROY:
2787 error = proc_suser(p);
2788 if (error)
2789 return (error);
2790 return (if_clone_destroy(ifr->ifr_name));
2791 }
2792
2793 /*
2794 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2795 * here to ensure that the ifnet, if found, has been fully attached.
2796 */
2797 dlil_if_lock();
2798 ifp = ifunit(ifr->ifr_name);
2799 dlil_if_unlock();
2800
2801 if (ifp == NULL)
2802 return (ENXIO);
2803
2804 switch (cmd) {
2805 case SIOCGIFFLAGS:
2806 ifnet_lock_shared(ifp);
2807 ifr->ifr_flags = ifp->if_flags;
2808 ifnet_lock_done(ifp);
2809 break;
2810
2811 case SIOCGIFEFLAGS:
2812 ifnet_lock_shared(ifp);
2813 ifr->ifr_eflags = ifp->if_eflags;
2814 ifnet_lock_done(ifp);
2815 break;
2816
2817 case SIOCGIFCAP:
2818 ifnet_lock_shared(ifp);
2819 ifr->ifr_reqcap = ifp->if_capabilities;
2820 ifr->ifr_curcap = ifp->if_capenable;
2821 ifnet_lock_done(ifp);
2822 break;
2823
2824 #if CONFIG_MACF_NET
2825 case SIOCGIFMAC:
2826 error = mac_ifnet_label_get(kauth_cred_get(), ifr, ifp);
2827 break;
2828
2829 case SIOCSIFMAC:
2830 error = mac_ifnet_label_set(kauth_cred_get(), ifr, ifp);
2831 break;
2832 #endif /* CONFIG_MACF_NET */
2833
2834 case SIOCGIFMETRIC:
2835 ifnet_lock_shared(ifp);
2836 ifr->ifr_metric = ifp->if_metric;
2837 ifnet_lock_done(ifp);
2838 break;
2839
2840 case SIOCGIFMTU:
2841 ifnet_lock_shared(ifp);
2842 ifr->ifr_mtu = ifp->if_mtu;
2843 ifnet_lock_done(ifp);
2844 break;
2845
2846 case SIOCGIFPHYS:
2847 ifnet_lock_shared(ifp);
2848 ifr->ifr_phys = ifp->if_physical;
2849 ifnet_lock_done(ifp);
2850 break;
2851
2852 case SIOCSIFFLAGS:
2853 error = proc_suser(p);
2854 if (error != 0)
2855 break;
2856
2857 (void) ifnet_set_flags(ifp, ifr->ifr_flags,
2858 (u_int16_t)~IFF_CANTCHANGE);
2859
2860 /*
2861 * Note that we intentionally ignore any error from below
2862 * for the SIOCSIFFLAGS case.
2863 */
2864 (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2865
2866 /*
2867 * Send the event even upon error from the driver because
2868 * we changed the flags.
2869 */
2870 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2871 ev_msg.kev_class = KEV_NETWORK_CLASS;
2872 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2873
2874 ev_msg.event_code = KEV_DL_SIFFLAGS;
2875 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2876 ev_data.if_family = ifp->if_family;
2877 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2878 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2879 ev_msg.dv[0].data_ptr = &ev_data;
2880 ev_msg.dv[1].data_length = 0;
2881 dlil_post_complete_msg(ifp, &ev_msg);
2882
2883 ifnet_touch_lastchange(ifp);
2884 break;
2885
2886 case SIOCSIFCAP:
2887 error = proc_suser(p);
2888 if (error != 0)
2889 break;
2890
2891 if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
2892 error = EINVAL;
2893 break;
2894 }
2895 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2896
2897 ifnet_touch_lastchange(ifp);
2898 break;
2899
2900 case SIOCSIFMETRIC:
2901 error = proc_suser(p);
2902 if (error != 0)
2903 break;
2904
2905 ifp->if_metric = ifr->ifr_metric;
2906
2907 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2908 ev_msg.kev_class = KEV_NETWORK_CLASS;
2909 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2910
2911 ev_msg.event_code = KEV_DL_SIFMETRICS;
2912 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2913 ev_data.if_family = ifp->if_family;
2914 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2915 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2916 ev_msg.dv[0].data_ptr = &ev_data;
2917
2918 ev_msg.dv[1].data_length = 0;
2919 dlil_post_complete_msg(ifp, &ev_msg);
2920
2921 ifnet_touch_lastchange(ifp);
2922 break;
2923
2924 case SIOCSIFPHYS:
2925 error = proc_suser(p);
2926 if (error != 0)
2927 break;
2928
2929 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2930 if (error != 0)
2931 break;
2932
2933 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2934 ev_msg.kev_class = KEV_NETWORK_CLASS;
2935 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2936
2937 ev_msg.event_code = KEV_DL_SIFPHYS;
2938 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2939 ev_data.if_family = ifp->if_family;
2940 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2941 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2942 ev_msg.dv[0].data_ptr = &ev_data;
2943 ev_msg.dv[1].data_length = 0;
2944 dlil_post_complete_msg(ifp, &ev_msg);
2945
2946 ifnet_touch_lastchange(ifp);
2947 break;
2948
2949 case SIOCSIFMTU: {
2950 u_int32_t oldmtu = ifp->if_mtu;
2951 struct ifclassq *ifq = &ifp->if_snd;
2952
2953 error = proc_suser(p);
2954 if (error != 0)
2955 break;
2956
2957 if (ifp->if_ioctl == NULL) {
2958 error = EOPNOTSUPP;
2959 break;
2960 }
2961 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2962 error = EINVAL;
2963 break;
2964 }
2965 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2966 if (error != 0)
2967 break;
2968
2969 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2970 ev_msg.kev_class = KEV_NETWORK_CLASS;
2971 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
2972
2973 ev_msg.event_code = KEV_DL_SIFMTU;
2974 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
2975 ev_data.if_family = ifp->if_family;
2976 ev_data.if_unit = (u_int32_t) ifp->if_unit;
2977 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
2978 ev_msg.dv[0].data_ptr = &ev_data;
2979 ev_msg.dv[1].data_length = 0;
2980 dlil_post_complete_msg(ifp, &ev_msg);
2981
2982 ifnet_touch_lastchange(ifp);
2983 rt_ifmsg(ifp);
2984
2985 /*
2986 * If the link MTU changed, do network layer specific procedure
2987 * and update all route entries associated with the interface,
2988 * so that their MTU metric gets updated.
2989 */
2990 if (ifp->if_mtu != oldmtu) {
2991 if_rtmtu_update(ifp);
2992 #if INET6
2993 nd6_setmtu(ifp);
2994 #endif /* INET6 */
2995 /* Inform all transmit queues about the new MTU */
2996 IFCQ_LOCK(ifq);
2997 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
2998 IFCQ_UNLOCK(ifq);
2999 }
3000 break;
3001 }
3002
3003 case SIOCADDMULTI:
3004 case SIOCDELMULTI:
3005 error = proc_suser(p);
3006 if (error != 0)
3007 break;
3008
3009 /* Don't allow group membership on non-multicast interfaces. */
3010 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
3011 error = EOPNOTSUPP;
3012 break;
3013 }
3014
3015 /* Don't let users screw up protocols' entries. */
3016 if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
3017 ifr->ifr_addr.sa_family != AF_LINK) {
3018 error = EINVAL;
3019 break;
3020 }
3021
3022 /*
3023 * User is permitted to anonymously join a particular link
3024 * multicast group via SIOCADDMULTI. Subsequent join requested
3025 * for the same record which has an outstanding refcnt from a
3026 * past if_addmulti_anon() will not result in EADDRINUSE error
3027 * (unlike other BSDs.) Anonymously leaving a group is also
3028 * allowed only as long as there is an outstanding refcnt held
3029 * by a previous anonymous request, or else ENOENT (even if the
3030 * link-layer multicast membership exists for a network-layer
3031 * membership.)
3032 */
3033 if (cmd == SIOCADDMULTI) {
3034 error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
3035 ev_msg.event_code = KEV_DL_ADDMULTI;
3036 } else {
3037 error = if_delmulti_anon(ifp, &ifr->ifr_addr);
3038 ev_msg.event_code = KEV_DL_DELMULTI;
3039 }
3040 if (error != 0)
3041 break;
3042
3043 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3044 ev_msg.kev_class = KEV_NETWORK_CLASS;
3045 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3046 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3047
3048 ev_data.if_family = ifp->if_family;
3049 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3050 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3051 ev_msg.dv[0].data_ptr = &ev_data;
3052 ev_msg.dv[1].data_length = 0;
3053 dlil_post_complete_msg(ifp, &ev_msg);
3054
3055 ifnet_touch_lastchange(ifp);
3056 break;
3057
3058 case SIOCDIFPHYADDR:
3059 case SIOCSIFMEDIA:
3060 case SIOCSIFGENERIC:
3061 case SIOCSIFLLADDR:
3062 case SIOCSIFALTMTU:
3063 case SIOCSIFVLAN:
3064 case SIOCSIFBOND:
3065 error = proc_suser(p);
3066 if (error != 0)
3067 break;
3068
3069 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3070 if (error != 0)
3071 break;
3072
3073 ifnet_touch_lastchange(ifp);
3074 break;
3075
3076 case SIOCGIFLLADDR: {
3077 struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
3078
3079 if (sdl->sdl_alen == 0) {
3080 error = EADDRNOTAVAIL;
3081 break;
3082 }
3083 /* If larger than 14-bytes we'll need another mechanism */
3084 if (sdl->sdl_alen > sizeof (ifr->ifr_addr.sa_data)) {
3085 error = EMSGSIZE;
3086 break;
3087 }
3088 /* Follow the same convention used by SIOCSIFLLADDR */
3089 bzero(&ifr->ifr_addr, sizeof (ifr->ifr_addr));
3090 ifr->ifr_addr.sa_family = AF_LINK;
3091 ifr->ifr_addr.sa_len = sdl->sdl_alen;
3092 error = ifnet_guarded_lladdr_copy_bytes(ifp,
3093 &ifr->ifr_addr.sa_data, sdl->sdl_alen);
3094 break;
3095 }
3096
3097 case SIOCGIFTYPE:
3098 ifr->ifr_type.ift_type = ifp->if_type;
3099 ifr->ifr_type.ift_family = ifp->if_family;
3100 ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
3101 break;
3102
3103 case SIOCGIFFUNCTIONALTYPE:
3104 ifr->ifr_functional_type = if_functional_type(ifp, FALSE);
3105 break;
3106
3107 case SIOCGIFPSRCADDR:
3108 case SIOCGIFPDSTADDR:
3109 case SIOCGIFGENERIC:
3110 case SIOCGIFDEVMTU:
3111 case SIOCGIFVLAN:
3112 case SIOCGIFBOND:
3113 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3114 break;
3115
3116 case SIOCGIFWAKEFLAGS:
3117 ifnet_lock_shared(ifp);
3118 ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
3119 ifnet_lock_done(ifp);
3120 break;
3121
3122 case SIOCGIFGETRTREFCNT:
3123 ifnet_lock_shared(ifp);
3124 ifr->ifr_route_refcnt = ifp->if_route_refcnt;
3125 ifnet_lock_done(ifp);
3126 break;
3127
3128 case SIOCSIFOPPORTUNISTIC:
3129 case SIOCGIFOPPORTUNISTIC:
3130 error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
3131 break;
3132
3133 case SIOCGIFLINKQUALITYMETRIC:
3134 ifnet_lock_shared(ifp);
3135 if ((ifp->if_interface_state.valid_bitmask &
3136 IF_INTERFACE_STATE_LQM_STATE_VALID)) {
3137 ifr->ifr_link_quality_metric =
3138 ifp->if_interface_state.lqm_state;
3139 } else if (IF_FULLY_ATTACHED(ifp)) {
3140 ifr->ifr_link_quality_metric =
3141 IFNET_LQM_THRESH_UNKNOWN;
3142 } else {
3143 ifr->ifr_link_quality_metric =
3144 IFNET_LQM_THRESH_OFF;
3145 }
3146 ifnet_lock_done(ifp);
3147 break;
3148
3149 case SIOCSIFLOG:
3150 case SIOCGIFLOG:
3151 error = ifnet_getset_log(ifp, cmd, ifr, p);
3152 break;
3153
3154 case SIOCGIFDELEGATE:
3155 ifnet_lock_shared(ifp);
3156 ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
3157 ifp->if_delegated.ifp->if_index : 0);
3158 ifnet_lock_done(ifp);
3159 break;
3160
3161 case SIOCGIFEXPENSIVE:
3162 ifnet_lock_shared(ifp);
3163 if (ifp->if_eflags & IFEF_EXPENSIVE)
3164 ifr->ifr_expensive = 1;
3165 else
3166 ifr->ifr_expensive = 0;
3167 ifnet_lock_done(ifp);
3168 break;
3169
3170 case SIOCSIFEXPENSIVE:
3171 {
3172 struct ifnet *difp;
3173
3174 if ((error = priv_check_cred(kauth_cred_get(),
3175 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3176 return (error);
3177 ifnet_lock_exclusive(ifp);
3178 if (ifr->ifr_expensive)
3179 ifp->if_eflags |= IFEF_EXPENSIVE;
3180 else
3181 ifp->if_eflags &= ~IFEF_EXPENSIVE;
3182 ifnet_lock_done(ifp);
3183 /*
3184 * Update the expensive bit in the delegated interface
3185 * structure.
3186 */
3187 ifnet_head_lock_shared();
3188 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3189 ifnet_lock_exclusive(difp);
3190 if (difp->if_delegated.ifp == ifp) {
3191 difp->if_delegated.expensive =
3192 ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
3193
3194 }
3195 ifnet_lock_done(difp);
3196 }
3197 ifnet_head_done();
3198 break;
3199 }
3200
3201 case SIOCGIF2KCL:
3202 ifnet_lock_shared(ifp);
3203 if (ifp->if_eflags & IFEF_2KCL)
3204 ifr->ifr_2kcl = 1;
3205 else
3206 ifr->ifr_2kcl = 0;
3207 ifnet_lock_done(ifp);
3208 break;
3209
3210 case SIOCSIF2KCL:
3211 if ((error = priv_check_cred(kauth_cred_get(),
3212 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3213 return (error);
3214 ifnet_lock_exclusive(ifp);
3215 if (ifr->ifr_2kcl)
3216 ifp->if_eflags |= IFEF_2KCL;
3217 else
3218 ifp->if_eflags &= ~IFEF_2KCL;
3219 ifnet_lock_done(ifp);
3220 break;
3221 case SIOCGSTARTDELAY:
3222 ifnet_lock_shared(ifp);
3223 if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
3224 ifr->ifr_start_delay_qlen =
3225 ifp->if_start_delay_qlen;
3226 ifr->ifr_start_delay_timeout =
3227 ifp->if_start_delay_timeout;
3228 } else {
3229 ifr->ifr_start_delay_qlen = 0;
3230 ifr->ifr_start_delay_timeout = 0;
3231 }
3232 ifnet_lock_done(ifp);
3233 break;
3234 case SIOCSIFDSTADDR:
3235 case SIOCSIFADDR:
3236 case SIOCSIFBRDADDR:
3237 case SIOCSIFNETMASK:
3238 case OSIOCGIFADDR:
3239 case OSIOCGIFDSTADDR:
3240 case OSIOCGIFBRDADDR:
3241 case OSIOCGIFNETMASK:
3242 case SIOCSIFKPI:
3243 VERIFY(so->so_proto != NULL);
3244
3245 if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
3246 cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
3247 #if BYTE_ORDER != BIG_ENDIAN
3248 if (ifr->ifr_addr.sa_family == 0 &&
3249 ifr->ifr_addr.sa_len < 16) {
3250 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
3251 ifr->ifr_addr.sa_len = 16;
3252 }
3253 #else
3254 if (ifr->ifr_addr.sa_len == 0)
3255 ifr->ifr_addr.sa_len = 16;
3256 #endif
3257 } else if (cmd == OSIOCGIFADDR) {
3258 cmd = SIOCGIFADDR; /* struct ifreq */
3259 } else if (cmd == OSIOCGIFDSTADDR) {
3260 cmd = SIOCGIFDSTADDR; /* struct ifreq */
3261 } else if (cmd == OSIOCGIFBRDADDR) {
3262 cmd = SIOCGIFBRDADDR; /* struct ifreq */
3263 } else if (cmd == OSIOCGIFNETMASK) {
3264 cmd = SIOCGIFNETMASK; /* struct ifreq */
3265 }
3266
3267 socket_lock(so, 1);
3268 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3269 (caddr_t)ifr, ifp, p));
3270 socket_unlock(so, 1);
3271
3272 switch (ocmd) {
3273 case OSIOCGIFADDR:
3274 case OSIOCGIFDSTADDR:
3275 case OSIOCGIFBRDADDR:
3276 case OSIOCGIFNETMASK:
3277 bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
3278 sizeof (u_short));
3279 }
3280
3281 if (cmd == SIOCSIFKPI) {
3282 int temperr = proc_suser(p);
3283 if (temperr != 0)
3284 error = temperr;
3285 }
3286
3287 if (error == EOPNOTSUPP || error == ENOTSUP) {
3288 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3289 (caddr_t)ifr);
3290 }
3291 break;
3292
3293 case SIOCGIFINTERFACESTATE:
3294 if_get_state(ifp, &ifr->ifr_interface_state);
3295
3296 break;
3297 case SIOCSIFINTERFACESTATE:
3298 if ((error = priv_check_cred(kauth_cred_get(),
3299 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3300 return (error);
3301
3302 error = if_state_update(ifp, &ifr->ifr_interface_state);
3303
3304 break;
3305 case SIOCSIFPROBECONNECTIVITY:
3306 if ((error = priv_check_cred(kauth_cred_get(),
3307 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3308 return (error);
3309 error = if_probe_connectivity(ifp,
3310 ifr->ifr_probe_connectivity);
3311 break;
3312 case SIOCGIFPROBECONNECTIVITY:
3313 if ((error = priv_check_cred(kauth_cred_get(),
3314 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3315 return (error);
3316 if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY)
3317 ifr->ifr_probe_connectivity = 1;
3318 else
3319 ifr->ifr_probe_connectivity = 0;
3320 break;
3321 case SIOCGECNMODE:
3322 if ((ifp->if_eflags & (IFEF_ECN_ENABLE|IFEF_ECN_DISABLE)) ==
3323 IFEF_ECN_ENABLE)
3324 ifr->ifr_ecn_mode = IFRTYPE_ECN_ENABLE;
3325 else if ((ifp->if_eflags & (IFEF_ECN_ENABLE|IFEF_ECN_DISABLE)) ==
3326 IFEF_ECN_DISABLE)
3327 ifr->ifr_ecn_mode = IFRTYPE_ECN_DISABLE;
3328 else
3329 ifr->ifr_ecn_mode = IFRTYPE_ECN_DEFAULT;
3330 break;
3331 case SIOCSECNMODE:
3332 if ((error = priv_check_cred(kauth_cred_get(),
3333 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3334 return (error);
3335 if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DEFAULT) {
3336 ifp->if_eflags &= ~(IFEF_ECN_ENABLE|IFEF_ECN_DISABLE);
3337 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_ENABLE) {
3338 ifp->if_eflags |= IFEF_ECN_ENABLE;
3339 ifp->if_eflags &= ~IFEF_ECN_DISABLE;
3340 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DISABLE) {
3341 ifp->if_eflags |= IFEF_ECN_DISABLE;
3342 ifp->if_eflags &= ~IFEF_ECN_ENABLE;
3343 } else
3344 error = EINVAL;
3345 break;
3346 case SIOCSIFTIMESTAMPENABLE:
3347 case SIOCSIFTIMESTAMPDISABLE:
3348 error = proc_suser(p);
3349 if (error != 0)
3350 break;
3351
3352 ifnet_lock_exclusive(ifp);
3353 if ((cmd == SIOCSIFTIMESTAMPENABLE &&
3354 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) ||
3355 (cmd == SIOCSIFTIMESTAMPDISABLE &&
3356 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) == 0)) {
3357 ifnet_lock_done(ifp);
3358 break;
3359 }
3360 if (cmd == SIOCSIFTIMESTAMPENABLE)
3361 ifp->if_xflags |= IFXF_TIMESTAMP_ENABLED;
3362 else
3363 ifp->if_xflags &= ~IFXF_TIMESTAMP_ENABLED;
3364 ifnet_lock_done(ifp);
3365 /*
3366 * Pass the setting to the interface if it supports either
3367 * software or hardware time stamping
3368 */
3369 if (ifp->if_capabilities & (IFCAP_HW_TIMESTAMP |
3370 IFCAP_SW_TIMESTAMP)) {
3371 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3372 (caddr_t)ifr);
3373 }
3374 break;
3375 case SIOCGIFTIMESTAMPENABLED: {
3376 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0)
3377 ifr->ifr_intval = 1;
3378 else
3379 ifr->ifr_intval = 0;
3380 break;
3381 }
3382 case SIOCSQOSMARKINGMODE:
3383 if ((error = priv_check_cred(kauth_cred_get(),
3384 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3385 return (error);
3386 error = if_set_qosmarking_mode(ifp, ifr->ifr_qosmarking_mode);
3387 break;
3388
3389 case SIOCGQOSMARKINGMODE:
3390 ifr->ifr_qosmarking_mode = ifp->if_qosmarking_mode;
3391 break;
3392
3393 case SIOCSQOSMARKINGENABLED:
3394 if ((error = priv_check_cred(kauth_cred_get(),
3395 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3396 return (error);
3397 if (ifr->ifr_qosmarking_enabled != 0)
3398 ifp->if_eflags |= IFEF_QOSMARKING_ENABLED;
3399 else
3400 ifp->if_eflags &= ~IFEF_QOSMARKING_ENABLED;
3401 break;
3402
3403 case SIOCGQOSMARKINGENABLED:
3404 ifr->ifr_qosmarking_enabled =
3405 (ifp->if_eflags & IFEF_QOSMARKING_ENABLED) ? 1 : 0;
3406 break;
3407
3408 case SIOCSIFDISABLEOUTPUT:
3409 #if (DEBUG || DEVELOPMENT)
3410 if (ifr->ifr_disable_output == 1) {
3411 error = ifnet_disable_output(ifp);
3412 } else if (ifr->ifr_disable_output == 0) {
3413 error = ifnet_enable_output(ifp);
3414 } else {
3415 error = EINVAL;
3416 }
3417 #else
3418 error = EINVAL;
3419 #endif /* (DEBUG || DEVELOPMENT) */
3420 break;
3421 case SIOCSIFLOWINTERNET:
3422 if ((error = priv_check_cred(kauth_cred_get(),
3423 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3424 return (error);
3425
3426 ifnet_lock_exclusive(ifp);
3427 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_UL)
3428 ifp->if_xflags |= IFXF_LOW_INTERNET_UL;
3429 else
3430 ifp->if_xflags &= ~(IFXF_LOW_INTERNET_UL);
3431 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_DL)
3432 ifp->if_xflags |= IFXF_LOW_INTERNET_DL;
3433 else
3434 ifp->if_xflags &= ~(IFXF_LOW_INTERNET_DL);
3435 ifnet_lock_done(ifp);
3436 break;
3437 case SIOCGIFLOWINTERNET:
3438 ifnet_lock_shared(ifp);
3439 ifr->ifr_low_internet = 0;
3440 if (ifp->if_xflags & IFXF_LOW_INTERNET_UL)
3441 ifr->ifr_low_internet |=
3442 IFRTYPE_LOW_INTERNET_ENABLE_UL;
3443 if (ifp->if_xflags & IFXF_LOW_INTERNET_DL)
3444 ifr->ifr_low_internet |=
3445 IFRTYPE_LOW_INTERNET_ENABLE_DL;
3446 ifnet_lock_done(ifp);
3447 break;
3448 default:
3449 VERIFY(0);
3450 /* NOTREACHED */
3451 }
3452
3453 return (error);
3454 }
3455
3456 int
3457 ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
3458 {
3459 int error;
3460
3461 socket_unlock(so, 0);
3462 error = ifioctl(so, cmd, data, p);
3463 socket_lock(so, 0);
3464 return (error);
3465 }
3466
3467 /*
3468 * Set/clear promiscuous mode on interface ifp based on the truth value
3469 * of pswitch. The calls are reference counted so that only the first
3470 * "on" request actually has an effect, as does the final "off" request.
3471 * Results are undefined if the "off" and "on" requests are not matched.
3472 */
3473 errno_t
3474 ifnet_set_promiscuous(
3475 ifnet_t ifp,
3476 int pswitch)
3477 {
3478 int error = 0;
3479 int oldflags = 0;
3480 int newflags = 0;
3481
3482 ifnet_lock_exclusive(ifp);
3483 oldflags = ifp->if_flags;
3484 ifp->if_pcount += pswitch ? 1 : -1;
3485
3486 if (ifp->if_pcount > 0)
3487 ifp->if_flags |= IFF_PROMISC;
3488 else
3489 ifp->if_flags &= ~IFF_PROMISC;
3490
3491 newflags = ifp->if_flags;
3492 ifnet_lock_done(ifp);
3493
3494 if (newflags != oldflags && (newflags & IFF_UP) != 0) {
3495 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
3496 if (error == 0) {
3497 rt_ifmsg(ifp);
3498 } else {
3499 ifnet_lock_exclusive(ifp);
3500 // revert the flags
3501 ifp->if_pcount -= pswitch ? 1 : -1;
3502 if (ifp->if_pcount > 0)
3503 ifp->if_flags |= IFF_PROMISC;
3504 else
3505 ifp->if_flags &= ~IFF_PROMISC;
3506 ifnet_lock_done(ifp);
3507 }
3508 }
3509
3510 if (newflags != oldflags) {
3511 log(LOG_INFO, "%s: promiscuous mode %s%s\n",
3512 if_name(ifp),
3513 (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
3514 error != 0 ? " failed" : " succeeded");
3515 }
3516 return (error);
3517 }
3518
3519 /*
3520 * Return interface configuration
3521 * of system. List may be used
3522 * in later ioctl's (above) to get
3523 * other information.
3524 */
3525 /*ARGSUSED*/
3526 static int
3527 ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
3528 {
3529 struct ifnet *ifp = NULL;
3530 struct ifaddr *ifa;
3531 struct ifreq ifr;
3532 int error = 0;
3533 size_t space;
3534 net_thread_marks_t marks;
3535
3536 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
3537
3538 /*
3539 * Zero the ifr buffer to make sure we don't
3540 * disclose the contents of the stack.
3541 */
3542 bzero(&ifr, sizeof (struct ifreq));
3543
3544 space = *ret_space;
3545 ifnet_head_lock_shared();
3546 for (ifp = ifnet_head.tqh_first; space > sizeof (ifr) &&
3547 ifp; ifp = ifp->if_link.tqe_next) {
3548 char workbuf[64];
3549 size_t ifnlen, addrs;
3550
3551 ifnlen = snprintf(workbuf, sizeof (workbuf),
3552 "%s", if_name(ifp));
3553 if (ifnlen + 1 > sizeof (ifr.ifr_name)) {
3554 error = ENAMETOOLONG;
3555 break;
3556 } else {
3557 strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
3558 }
3559
3560 ifnet_lock_shared(ifp);
3561
3562 addrs = 0;
3563 ifa = ifp->if_addrhead.tqh_first;
3564 for (; space > sizeof (ifr) && ifa;
3565 ifa = ifa->ifa_link.tqe_next) {
3566 struct sockaddr *sa;
3567 union {
3568 struct sockaddr sa;
3569 struct sockaddr_dl sdl;
3570 uint8_t buf[SOCK_MAXADDRLEN + 1];
3571 } u;
3572
3573 /*
3574 * Make sure to accomodate the largest possible
3575 * size of SA(if_lladdr)->sa_len.
3576 */
3577 _CASSERT(sizeof (u) == (SOCK_MAXADDRLEN + 1));
3578
3579 IFA_LOCK(ifa);
3580 sa = ifa->ifa_addr;
3581 addrs++;
3582
3583 if (ifa == ifp->if_lladdr) {
3584 VERIFY(sa->sa_family == AF_LINK);
3585 bcopy(sa, &u, sa->sa_len);
3586 IFA_UNLOCK(ifa);
3587 ifnet_guarded_lladdr_copy_bytes(ifp,
3588 LLADDR(&u.sdl), u.sdl.sdl_alen);
3589 IFA_LOCK(ifa);
3590 sa = &u.sa;
3591 }
3592
3593 if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
3594 struct osockaddr *osa =
3595 (struct osockaddr *)(void *)&ifr.ifr_addr;
3596 ifr.ifr_addr = *sa;
3597 osa->sa_family = sa->sa_family;
3598 error = copyout((caddr_t)&ifr, ifrp,
3599 sizeof (ifr));
3600 ifrp += sizeof (struct ifreq);
3601 } else if (sa->sa_len <= sizeof (*sa)) {
3602 ifr.ifr_addr = *sa;
3603 error = copyout((caddr_t)&ifr, ifrp,
3604 sizeof (ifr));
3605 ifrp += sizeof (struct ifreq);
3606 } else {
3607 if (space <
3608 sizeof (ifr) + sa->sa_len - sizeof (*sa)) {
3609 IFA_UNLOCK(ifa);
3610 break;
3611 }
3612 space -= sa->sa_len - sizeof (*sa);
3613 error = copyout((caddr_t)&ifr, ifrp,
3614 sizeof (ifr.ifr_name));
3615 if (error == 0) {
3616 error = copyout((caddr_t)sa, (ifrp +
3617 offsetof(struct ifreq, ifr_addr)),
3618 sa->sa_len);
3619 }
3620 ifrp += (sa->sa_len + offsetof(struct ifreq,
3621 ifr_addr));
3622 }
3623 IFA_UNLOCK(ifa);
3624 if (error)
3625 break;
3626 space -= sizeof (ifr);
3627 }
3628 ifnet_lock_done(ifp);
3629
3630 if (error)
3631 break;
3632 if (!addrs) {
3633 bzero((caddr_t)&ifr.ifr_addr, sizeof (ifr.ifr_addr));
3634 error = copyout((caddr_t)&ifr, ifrp, sizeof (ifr));
3635 if (error)
3636 break;
3637 space -= sizeof (ifr);
3638 ifrp += sizeof (struct ifreq);
3639 }
3640 }
3641 ifnet_head_done();
3642 *ret_space -= space;
3643 net_thread_marks_pop(marks);
3644 return (error);
3645 }
3646
3647 /*
3648 * Just like if_promisc(), but for all-multicast-reception mode.
3649 */
3650 int
3651 if_allmulti(struct ifnet *ifp, int onswitch)
3652 {
3653 int error = 0;
3654 int modified = 0;
3655
3656 ifnet_lock_exclusive(ifp);
3657
3658 if (onswitch) {
3659 if (ifp->if_amcount++ == 0) {
3660 ifp->if_flags |= IFF_ALLMULTI;
3661 modified = 1;
3662 }
3663 } else {
3664 if (ifp->if_amcount > 1) {
3665 ifp->if_amcount--;
3666 } else {
3667 ifp->if_amcount = 0;
3668 ifp->if_flags &= ~IFF_ALLMULTI;
3669 modified = 1;
3670 }
3671 }
3672 ifnet_lock_done(ifp);
3673
3674 if (modified)
3675 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
3676
3677 if (error == 0)
3678 rt_ifmsg(ifp);
3679 return (error);
3680 }
3681
3682 static struct ifmultiaddr *
3683 ifma_alloc(int how)
3684 {
3685 struct ifmultiaddr *ifma;
3686
3687 ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
3688 zalloc_noblock(ifma_zone);
3689
3690 if (ifma != NULL) {
3691 bzero(ifma, ifma_size);
3692 lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
3693 ifma->ifma_debug |= IFD_ALLOC;
3694 if (ifma_debug != 0) {
3695 ifma->ifma_debug |= IFD_DEBUG;
3696 ifma->ifma_trace = ifma_trace;
3697 }
3698 }
3699 return (ifma);
3700 }
3701
3702 static void
3703 ifma_free(struct ifmultiaddr *ifma)
3704 {
3705 IFMA_LOCK(ifma);
3706
3707 if (ifma->ifma_protospec != NULL) {
3708 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
3709 /* NOTREACHED */
3710 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3711 ifma->ifma_anoncnt != 0) {
3712 panic("%s: Freeing ifma=%p with outstanding anon req",
3713 __func__, ifma);
3714 /* NOTREACHED */
3715 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3716 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
3717 __func__, ifma, ifma->ifma_ifp);
3718 /* NOTREACHED */
3719 } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
3720 panic("%s: ifma %p cannot be freed", __func__, ifma);
3721 /* NOTREACHED */
3722 } else if (ifma->ifma_refcount != 0) {
3723 panic("%s: non-zero refcount ifma=%p", __func__, ifma);
3724 /* NOTREACHED */
3725 } else if (ifma->ifma_reqcnt != 0) {
3726 panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
3727 /* NOTREACHED */
3728 } else if (ifma->ifma_ifp != NULL) {
3729 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
3730 ifma->ifma_ifp, ifma);
3731 /* NOTREACHED */
3732 } else if (ifma->ifma_ll != NULL) {
3733 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
3734 ifma->ifma_ll, ifma);
3735 /* NOTREACHED */
3736 }
3737 ifma->ifma_debug &= ~IFD_ALLOC;
3738 if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
3739 (IFD_DEBUG | IFD_TRASHED)) {
3740 lck_mtx_lock(&ifma_trash_lock);
3741 TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
3742 ifma_trash_link);
3743 lck_mtx_unlock(&ifma_trash_lock);
3744 ifma->ifma_debug &= ~IFD_TRASHED;
3745 }
3746 IFMA_UNLOCK(ifma);
3747
3748 if (ifma->ifma_addr != NULL) {
3749 FREE(ifma->ifma_addr, M_IFADDR);
3750 ifma->ifma_addr = NULL;
3751 }
3752 lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
3753 zfree(ifma_zone, ifma);
3754 }
3755
3756 static void
3757 ifma_trace(struct ifmultiaddr *ifma, int refhold)
3758 {
3759 struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
3760 ctrace_t *tr;
3761 u_int32_t idx;
3762 u_int16_t *cnt;
3763
3764 if (!(ifma->ifma_debug & IFD_DEBUG)) {
3765 panic("%s: ifma %p has no debug structure", __func__, ifma);
3766 /* NOTREACHED */
3767 }
3768 if (refhold) {
3769 cnt = &ifma_dbg->ifma_refhold_cnt;
3770 tr = ifma_dbg->ifma_refhold;
3771 } else {
3772 cnt = &ifma_dbg->ifma_refrele_cnt;
3773 tr = ifma_dbg->ifma_refrele;
3774 }
3775
3776 idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
3777 ctrace_record(&tr[idx]);
3778 }
3779
3780 void
3781 ifma_addref(struct ifmultiaddr *ifma, int locked)
3782 {
3783 if (!locked)
3784 IFMA_LOCK(ifma);
3785 else
3786 IFMA_LOCK_ASSERT_HELD(ifma);
3787
3788 if (++ifma->ifma_refcount == 0) {
3789 panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
3790 /* NOTREACHED */
3791 } else if (ifma->ifma_trace != NULL) {
3792 (*ifma->ifma_trace)(ifma, TRUE);
3793 }
3794 if (!locked)
3795 IFMA_UNLOCK(ifma);
3796 }
3797
3798 void
3799 ifma_remref(struct ifmultiaddr *ifma)
3800 {
3801 struct ifmultiaddr *ll;
3802
3803 IFMA_LOCK(ifma);
3804
3805 if (ifma->ifma_refcount == 0) {
3806 panic("%s: ifma=%p negative refcnt", __func__, ifma);
3807 /* NOTREACHED */
3808 } else if (ifma->ifma_trace != NULL) {
3809 (*ifma->ifma_trace)(ifma, FALSE);
3810 }
3811
3812 --ifma->ifma_refcount;
3813 if (ifma->ifma_refcount > 0) {
3814 IFMA_UNLOCK(ifma);
3815 return;
3816 }
3817
3818 ll = ifma->ifma_ll;
3819 ifma->ifma_ifp = NULL;
3820 ifma->ifma_ll = NULL;
3821 IFMA_UNLOCK(ifma);
3822 ifma_free(ifma); /* deallocate it */
3823
3824 if (ll != NULL)
3825 IFMA_REMREF(ll);
3826 }
3827
3828 static void
3829 if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3830 {
3831 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3832 IFMA_LOCK_ASSERT_HELD(ifma);
3833
3834 if (ifma->ifma_ifp != ifp) {
3835 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
3836 ifma->ifma_ifp, ifp);
3837 /* NOTREACHED */
3838 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3839 panic("%s: Attempt to attach an already attached ifma=%p",
3840 __func__, ifma);
3841 /* NOTREACHED */
3842 } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3843 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
3844 /* NOTREACHED */
3845 } else if (ifma->ifma_debug & IFD_TRASHED) {
3846 panic("%s: Attempt to reattach a detached ifma=%p",
3847 __func__, ifma);
3848 /* NOTREACHED */
3849 }
3850
3851 ifma->ifma_reqcnt++;
3852 VERIFY(ifma->ifma_reqcnt == 1);
3853 IFMA_ADDREF_LOCKED(ifma);
3854 ifma->ifma_debug |= IFD_ATTACHED;
3855 if (anon) {
3856 ifma->ifma_anoncnt++;
3857 VERIFY(ifma->ifma_anoncnt == 1);
3858 ifma->ifma_flags |= IFMAF_ANONYMOUS;
3859 }
3860
3861 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3862 }
3863
3864 static int
3865 if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3866 {
3867 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3868 IFMA_LOCK_ASSERT_HELD(ifma);
3869
3870 if (ifma->ifma_reqcnt == 0) {
3871 panic("%s: ifma=%p negative reqcnt", __func__, ifma);
3872 /* NOTREACHED */
3873 } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3874 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
3875 /* NOTREACHED */
3876 } else if (anon && ifma->ifma_anoncnt == 0) {
3877 panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
3878 /* NOTREACHED */
3879 } else if (ifma->ifma_ifp != ifp) {
3880 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
3881 ifma->ifma_ifp, ifp);
3882 /* NOTREACHED */
3883 }
3884
3885 if (anon) {
3886 --ifma->ifma_anoncnt;
3887 if (ifma->ifma_anoncnt > 0)
3888 return (0);
3889 ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
3890 }
3891
3892 --ifma->ifma_reqcnt;
3893 if (ifma->ifma_reqcnt > 0)
3894 return (0);
3895
3896 if (ifma->ifma_protospec != NULL) {
3897 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
3898 /* NOTREACHED */
3899 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3900 ifma->ifma_anoncnt != 0) {
3901 panic("%s: Detaching ifma=%p with outstanding anon req",
3902 __func__, ifma);
3903 /* NOTREACHED */
3904 } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
3905 panic("%s: Attempt to detach an unattached address ifma=%p",
3906 __func__, ifma);
3907 /* NOTREACHED */
3908 } else if (ifma->ifma_debug & IFD_TRASHED) {
3909 panic("%s: ifma %p is already in trash list", __func__, ifma);
3910 /* NOTREACHED */
3911 }
3912
3913 /*
3914 * NOTE: Caller calls IFMA_REMREF
3915 */
3916 ifma->ifma_debug &= ~IFD_ATTACHED;
3917 LIST_REMOVE(ifma, ifma_link);
3918 if (LIST_EMPTY(&ifp->if_multiaddrs))
3919 ifp->if_updatemcasts = 0;
3920
3921 if (ifma->ifma_debug & IFD_DEBUG) {
3922 /* Become a regular mutex, just in case */
3923 IFMA_CONVERT_LOCK(ifma);
3924 lck_mtx_lock(&ifma_trash_lock);
3925 TAILQ_INSERT_TAIL(&ifma_trash_head,
3926 (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
3927 lck_mtx_unlock(&ifma_trash_lock);
3928 ifma->ifma_debug |= IFD_TRASHED;
3929 }
3930
3931 return (1);
3932 }
3933
3934 /*
3935 * Find an ifmultiaddr that matches a socket address on an interface.
3936 *
3937 * Caller is responsible for holding the ifnet_lock while calling
3938 * this function.
3939 */
3940 static int
3941 if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
3942 struct ifmultiaddr **retifma, int anon)
3943 {
3944 struct ifmultiaddr *ifma;
3945
3946 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
3947 ifma = LIST_NEXT(ifma, ifma_link)) {
3948 IFMA_LOCK_SPIN(ifma);
3949 if (!ifa_equal(sa, ifma->ifma_addr)) {
3950 IFMA_UNLOCK(ifma);
3951 continue;
3952 }
3953 if (anon) {
3954 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3955 ifma->ifma_anoncnt != 0);
3956 VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3957 ifma->ifma_anoncnt == 0);
3958 ifma->ifma_anoncnt++;
3959 if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3960 VERIFY(ifma->ifma_anoncnt == 1);
3961 ifma->ifma_flags |= IFMAF_ANONYMOUS;
3962 }
3963 }
3964 if (!anon || ifma->ifma_anoncnt == 1) {
3965 ifma->ifma_reqcnt++;
3966 VERIFY(ifma->ifma_reqcnt > 1);
3967 }
3968 if (retifma != NULL) {
3969 *retifma = ifma;
3970 IFMA_ADDREF_LOCKED(ifma);
3971 }
3972 IFMA_UNLOCK(ifma);
3973 return (0);
3974 }
3975 return (ENOENT);
3976 }
3977
3978 /*
3979 * Radar 3642395, make sure all multicasts are in a standard format.
3980 */
3981 static struct sockaddr *
3982 copy_and_normalize(const struct sockaddr *original)
3983 {
3984 int alen = 0;
3985 const u_char *aptr = NULL;
3986 struct sockaddr *copy = NULL;
3987 struct sockaddr_dl *sdl_new = NULL;
3988 int len = 0;
3989
3990 if (original->sa_family != AF_LINK &&
3991 original->sa_family != AF_UNSPEC) {
3992 /* Just make a copy */
3993 MALLOC(copy, struct sockaddr *, original->sa_len,
3994 M_IFADDR, M_WAITOK);
3995 if (copy != NULL)
3996 bcopy(original, copy, original->sa_len);
3997 return (copy);
3998 }
3999
4000 switch (original->sa_family) {
4001 case AF_LINK: {
4002 const struct sockaddr_dl *sdl_original =
4003 (struct sockaddr_dl *)(uintptr_t)(size_t)original;
4004
4005 if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
4006 sdl_original->sdl_slen +
4007 offsetof(struct sockaddr_dl, sdl_data) >
4008 sdl_original->sdl_len)
4009 return (NULL);
4010
4011 alen = sdl_original->sdl_alen;
4012 aptr = CONST_LLADDR(sdl_original);
4013 }
4014 break;
4015
4016 case AF_UNSPEC: {
4017 if (original->sa_len < ETHER_ADDR_LEN +
4018 offsetof(struct sockaddr, sa_data)) {
4019 return (NULL);
4020 }
4021
4022 alen = ETHER_ADDR_LEN;
4023 aptr = (const u_char *)original->sa_data;
4024 }
4025 break;
4026 }
4027
4028 if (alen == 0 || aptr == NULL)
4029 return (NULL);
4030
4031 len = alen + offsetof(struct sockaddr_dl, sdl_data);
4032 MALLOC(sdl_new, struct sockaddr_dl *, len, M_IFADDR, M_WAITOK);
4033
4034 if (sdl_new != NULL) {
4035 bzero(sdl_new, len);
4036 sdl_new->sdl_len = len;
4037 sdl_new->sdl_family = AF_LINK;
4038 sdl_new->sdl_alen = alen;
4039 bcopy(aptr, LLADDR(sdl_new), alen);
4040 }
4041
4042 return ((struct sockaddr *)sdl_new);
4043 }
4044
4045 /*
4046 * Network-layer protocol domains which hold references to the underlying
4047 * link-layer record must use this routine.
4048 */
4049 int
4050 if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
4051 struct ifmultiaddr **retifma)
4052 {
4053 return (if_addmulti_common(ifp, sa, retifma, 0));
4054 }
4055
4056 /*
4057 * Anything other than network-layer protocol domains which hold references
4058 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4059 * ioctl, ifnet_add_multicast(), if_bond.
4060 */
4061 int
4062 if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
4063 struct ifmultiaddr **retifma)
4064 {
4065 return (if_addmulti_common(ifp, sa, retifma, 1));
4066 }
4067
4068 /*
4069 * Register an additional multicast address with a network interface.
4070 *
4071 * - If the address is already present, bump the reference count on the
4072 * address and return.
4073 * - If the address is not link-layer, look up a link layer address.
4074 * - Allocate address structures for one or both addresses, and attach to the
4075 * multicast address list on the interface. If automatically adding a link
4076 * layer address, the protocol address will own a reference to the link
4077 * layer address, to be freed when it is freed.
4078 * - Notify the network device driver of an addition to the multicast address
4079 * list.
4080 *
4081 * 'sa' points to caller-owned memory with the desired multicast address.
4082 *
4083 * 'retifma' will be used to return a pointer to the resulting multicast
4084 * address reference, if desired.
4085 *
4086 * 'anon' indicates a link-layer address with no protocol address reference
4087 * made to it. Anything other than network-layer protocol domain requests
4088 * are considered as anonymous.
4089 */
4090 static int
4091 if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
4092 struct ifmultiaddr **retifma, int anon)
4093 {
4094 struct sockaddr_storage storage;
4095 struct sockaddr *llsa = NULL;
4096 struct sockaddr *dupsa = NULL;
4097 int error = 0, ll_firstref = 0, lladdr;
4098 struct ifmultiaddr *ifma = NULL;
4099 struct ifmultiaddr *llifma = NULL;
4100
4101 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4102 VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
4103 sa->sa_family == AF_LINK);
4104
4105 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4106 if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
4107 dupsa = copy_and_normalize(sa);
4108 if (dupsa == NULL) {
4109 error = ENOMEM;
4110 goto cleanup;
4111 }
4112 sa = dupsa;
4113 }
4114
4115 ifnet_lock_exclusive(ifp);
4116 if (!(ifp->if_flags & IFF_MULTICAST)) {
4117 error = EADDRNOTAVAIL;
4118 ifnet_lock_done(ifp);
4119 goto cleanup;
4120 }
4121
4122 /* If the address is already present, return a new reference to it */
4123 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4124 ifnet_lock_done(ifp);
4125 if (error == 0)
4126 goto cleanup;
4127
4128 /*
4129 * The address isn't already present; give the link layer a chance
4130 * to accept/reject it, and also find out which AF_LINK address this
4131 * maps to, if it isn't one already.
4132 */
4133 error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
4134 sizeof (storage));
4135 if (error == 0 && storage.ss_len != 0) {
4136 llsa = copy_and_normalize((struct sockaddr *)&storage);
4137 if (llsa == NULL) {
4138 error = ENOMEM;
4139 goto cleanup;
4140 }
4141
4142 llifma = ifma_alloc(M_WAITOK);
4143 if (llifma == NULL) {
4144 error = ENOMEM;
4145 goto cleanup;
4146 }
4147 }
4148
4149 /* to be similar to FreeBSD */
4150 if (error == EOPNOTSUPP)
4151 error = 0;
4152 else if (error != 0)
4153 goto cleanup;
4154
4155 /* Allocate while we aren't holding any locks */
4156 if (dupsa == NULL) {
4157 dupsa = copy_and_normalize(sa);
4158 if (dupsa == NULL) {
4159 error = ENOMEM;
4160 goto cleanup;
4161 }
4162 }
4163 ifma = ifma_alloc(M_WAITOK);
4164 if (ifma == NULL) {
4165 error = ENOMEM;
4166 goto cleanup;
4167 }
4168
4169 ifnet_lock_exclusive(ifp);
4170 /*
4171 * Check again for the matching multicast.
4172 */
4173 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4174 if (error == 0) {
4175 ifnet_lock_done(ifp);
4176 goto cleanup;
4177 }
4178
4179 if (llifma != NULL) {
4180 VERIFY(!anon); /* must not get here if "anonymous" */
4181 if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
4182 FREE(llsa, M_IFADDR);
4183 llsa = NULL;
4184 ifma_free(llifma);
4185 llifma = NULL;
4186 VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
4187 } else {
4188 ll_firstref = 1;
4189 llifma->ifma_addr = llsa;
4190 llifma->ifma_ifp = ifp;
4191 IFMA_LOCK(llifma);
4192 if_attach_ifma(ifp, llifma, 0);
4193 /* add extra refcnt for ifma */
4194 IFMA_ADDREF_LOCKED(llifma);
4195 IFMA_UNLOCK(llifma);
4196 ifma->ifma_ll = llifma;
4197 }
4198 }
4199
4200 /* "anonymous" request should not result in network address */
4201 VERIFY(!anon || ifma->ifma_ll == NULL);
4202
4203 ifma->ifma_addr = dupsa;
4204 ifma->ifma_ifp = ifp;
4205 IFMA_LOCK(ifma);
4206 if_attach_ifma(ifp, ifma, anon);
4207 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4208 if (retifma != NULL) {
4209 *retifma = ifma;
4210 IFMA_ADDREF_LOCKED(*retifma); /* for caller */
4211 }
4212 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4213 ifma->ifma_addr->sa_family == AF_LINK);
4214 IFMA_UNLOCK(ifma);
4215 ifnet_lock_done(ifp);
4216
4217 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
4218 IFMA_REMREF(ifma); /* for this routine */
4219
4220 /*
4221 * We are certain we have added something, so call down to the
4222 * interface to let them know about it. Do this only for newly-
4223 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4224 */
4225 if (lladdr || ll_firstref)
4226 (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
4227
4228 if (ifp->if_updatemcasts > 0)
4229 ifp->if_updatemcasts = 0;
4230
4231 return (0);
4232
4233 cleanup:
4234 if (ifma != NULL)
4235 ifma_free(ifma);
4236 if (dupsa != NULL)
4237 FREE(dupsa, M_IFADDR);
4238 if (llifma != NULL)
4239 ifma_free(llifma);
4240 if (llsa != NULL)
4241 FREE(llsa, M_IFADDR);
4242
4243 return (error);
4244 }
4245
4246 /*
4247 * Delete a multicast group membership by network-layer group address.
4248 * This routine is deprecated.
4249 */
4250 int
4251 if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
4252 {
4253 return (if_delmulti_common(NULL, ifp, sa, 0));
4254 }
4255
4256 /*
4257 * Delete a multicast group membership by group membership pointer.
4258 * Network-layer protocol domains must use this routine.
4259 */
4260 int
4261 if_delmulti_ifma(struct ifmultiaddr *ifma)
4262 {
4263 return (if_delmulti_common(ifma, NULL, NULL, 0));
4264 }
4265
4266 /*
4267 * Anything other than network-layer protocol domains which hold references
4268 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4269 * ioctl, ifnet_remove_multicast(), if_bond.
4270 */
4271 int
4272 if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
4273 {
4274 return (if_delmulti_common(NULL, ifp, sa, 1));
4275 }
4276
4277 /*
4278 * Delete a multicast group membership by network-layer group address.
4279 *
4280 * Returns ENOENT if the entry could not be found.
4281 */
4282 static int
4283 if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
4284 const struct sockaddr *sa, int anon)
4285 {
4286 struct sockaddr *dupsa = NULL;
4287 int lastref, ll_lastref = 0, lladdr;
4288 struct ifmultiaddr *ll = NULL;
4289
4290 /* sanity check for callers */
4291 VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
4292
4293 if (ifma != NULL)
4294 ifp = ifma->ifma_ifp;
4295
4296 if (sa != NULL &&
4297 (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
4298 dupsa = copy_and_normalize(sa);
4299 if (dupsa == NULL)
4300 return (ENOMEM);
4301 sa = dupsa;
4302 }
4303
4304 ifnet_lock_exclusive(ifp);
4305 if (ifma == NULL) {
4306 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4307 ifma = LIST_NEXT(ifma, ifma_link)) {
4308 IFMA_LOCK(ifma);
4309 if (!ifa_equal(sa, ifma->ifma_addr) ||
4310 (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
4311 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4312 ifma->ifma_anoncnt != 0);
4313 IFMA_UNLOCK(ifma);
4314 continue;
4315 }
4316 /* found; keep it locked */
4317 break;
4318 }
4319 if (ifma == NULL) {
4320 if (dupsa != NULL)
4321 FREE(dupsa, M_IFADDR);
4322 ifnet_lock_done(ifp);
4323 return (ENOENT);
4324 }
4325 } else {
4326 IFMA_LOCK(ifma);
4327 }
4328 IFMA_LOCK_ASSERT_HELD(ifma);
4329 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4330 lastref = if_detach_ifma(ifp, ifma, anon);
4331 VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
4332 ifma->ifma_reqcnt == 0));
4333 VERIFY(!anon || ifma->ifma_ll == NULL);
4334 ll = ifma->ifma_ll;
4335 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4336 ifma->ifma_addr->sa_family == AF_LINK);
4337 IFMA_UNLOCK(ifma);
4338 if (lastref && ll != NULL) {
4339 IFMA_LOCK(ll);
4340 ll_lastref = if_detach_ifma(ifp, ll, 0);
4341 IFMA_UNLOCK(ll);
4342 }
4343 ifnet_lock_done(ifp);
4344
4345 if (lastref)
4346 rt_newmaddrmsg(RTM_DELMADDR, ifma);
4347
4348 if ((ll == NULL && lastref && lladdr) || ll_lastref) {
4349 /*
4350 * Make sure the interface driver is notified in the
4351 * case of a link layer mcast group being left. Do
4352 * this only for a AF_LINK/AF_UNSPEC address that has
4353 * been removed from the if_multiaddrs set.
4354 */
4355 ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
4356 }
4357
4358 if (lastref)
4359 IFMA_REMREF(ifma); /* for if_multiaddrs list */
4360 if (ll_lastref)
4361 IFMA_REMREF(ll); /* for if_multiaddrs list */
4362
4363 IFMA_REMREF(ifma); /* for this routine */
4364 if (dupsa != NULL)
4365 FREE(dupsa, M_IFADDR);
4366
4367 return (0);
4368 }
4369
4370 /*
4371 * Shutdown all network activity. Used boot() when halting
4372 * system.
4373 */
4374 int
4375 if_down_all(void)
4376 {
4377 struct ifnet **ifp;
4378 u_int32_t count;
4379 u_int32_t i;
4380
4381 if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
4382 for (i = 0; i < count; i++) {
4383 if_down(ifp[i]);
4384 dlil_proto_unplumb_all(ifp[i]);
4385 }
4386 ifnet_list_free(ifp);
4387 }
4388
4389 return (0);
4390 }
4391
4392 /*
4393 * Delete Routes for a Network Interface
4394 *
4395 * Called for each routing entry via the rnh->rnh_walktree() call above
4396 * to delete all route entries referencing a detaching network interface.
4397 *
4398 * Arguments:
4399 * rn pointer to node in the routing table
4400 * arg argument passed to rnh->rnh_walktree() - detaching interface
4401 *
4402 * Returns:
4403 * 0 successful
4404 * errno failed - reason indicated
4405 *
4406 */
4407 static int
4408 if_rtdel(struct radix_node *rn, void *arg)
4409 {
4410 struct rtentry *rt = (struct rtentry *)rn;
4411 struct ifnet *ifp = arg;
4412 int err;
4413
4414 if (rt == NULL)
4415 return (0);
4416 /*
4417 * Checking against RTF_UP protects against walktree
4418 * recursion problems with cloned routes.
4419 */
4420 RT_LOCK(rt);
4421 if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
4422 /*
4423 * Safe to drop rt_lock and use rt_key, rt_gateway,
4424 * since holding rnh_lock here prevents another thread
4425 * from calling rt_setgate() on this route.
4426 */
4427 RT_UNLOCK(rt);
4428 err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
4429 rt_mask(rt), rt->rt_flags, NULL);
4430 if (err) {
4431 log(LOG_WARNING, "if_rtdel: error %d\n", err);
4432 }
4433 } else {
4434 RT_UNLOCK(rt);
4435 }
4436 return (0);
4437 }
4438
4439 /*
4440 * Removes routing table reference to a given interface
4441 * for a given protocol family
4442 */
4443 void
4444 if_rtproto_del(struct ifnet *ifp, int protocol)
4445 {
4446 struct radix_node_head *rnh;
4447
4448 if ((protocol <= AF_MAX) && (protocol >= 0) &&
4449 ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
4450 lck_mtx_lock(rnh_lock);
4451 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
4452 lck_mtx_unlock(rnh_lock);
4453 }
4454 }
4455
4456 static int
4457 if_rtmtu(struct radix_node *rn, void *arg)
4458 {
4459 struct rtentry *rt = (struct rtentry *)rn;
4460 struct ifnet *ifp = arg;
4461
4462 RT_LOCK(rt);
4463 if (rt->rt_ifp == ifp) {
4464 /*
4465 * Update the MTU of this entry only if the MTU
4466 * has not been locked (RTV_MTU is not set) and
4467 * if it was non-zero to begin with.
4468 */
4469 if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu)
4470 rt->rt_rmx.rmx_mtu = ifp->if_mtu;
4471 }
4472 RT_UNLOCK(rt);
4473
4474 return (0);
4475 }
4476
4477 /*
4478 * Update the MTU metric of all route entries in all protocol tables
4479 * associated with a particular interface; this is called when the
4480 * MTU of that interface has changed.
4481 */
4482 static void
4483 if_rtmtu_update(struct ifnet *ifp)
4484 {
4485 struct radix_node_head *rnh;
4486 int p;
4487
4488 for (p = 0; p < AF_MAX + 1; p++) {
4489 if ((rnh = rt_tables[p]) == NULL)
4490 continue;
4491
4492 lck_mtx_lock(rnh_lock);
4493 (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
4494 lck_mtx_unlock(rnh_lock);
4495 }
4496 routegenid_update();
4497 }
4498
4499 __private_extern__ void
4500 if_data_internal_to_if_data(struct ifnet *ifp,
4501 const struct if_data_internal *if_data_int, struct if_data *if_data)
4502 {
4503 #pragma unused(ifp)
4504 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
4505 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
4506 /* compiler will cast down to 32-bit */
4507 #define COPYFIELD32_ATOMIC(fld) do { \
4508 atomic_get_64(if_data->fld, \
4509 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4510 } while (0)
4511
4512 COPYFIELD(ifi_type);
4513 COPYFIELD(ifi_typelen);
4514 COPYFIELD(ifi_physical);
4515 COPYFIELD(ifi_addrlen);
4516 COPYFIELD(ifi_hdrlen);
4517 COPYFIELD(ifi_recvquota);
4518 COPYFIELD(ifi_xmitquota);
4519 if_data->ifi_unused1 = 0;
4520 COPYFIELD(ifi_mtu);
4521 COPYFIELD(ifi_metric);
4522 if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
4523 if_data->ifi_baudrate = 0xFFFFFFFF;
4524 } else {
4525 COPYFIELD32(ifi_baudrate);
4526 }
4527
4528 COPYFIELD32_ATOMIC(ifi_ipackets);
4529 COPYFIELD32_ATOMIC(ifi_ierrors);
4530 COPYFIELD32_ATOMIC(ifi_opackets);
4531 COPYFIELD32_ATOMIC(ifi_oerrors);
4532 COPYFIELD32_ATOMIC(ifi_collisions);
4533 COPYFIELD32_ATOMIC(ifi_ibytes);
4534 COPYFIELD32_ATOMIC(ifi_obytes);
4535 COPYFIELD32_ATOMIC(ifi_imcasts);
4536 COPYFIELD32_ATOMIC(ifi_omcasts);
4537 COPYFIELD32_ATOMIC(ifi_iqdrops);
4538 COPYFIELD32_ATOMIC(ifi_noproto);
4539
4540 COPYFIELD(ifi_recvtiming);
4541 COPYFIELD(ifi_xmittiming);
4542
4543 if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
4544 if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
4545
4546 if_data->ifi_lastchange.tv_sec += boottime_sec();
4547
4548 if_data->ifi_unused2 = 0;
4549 COPYFIELD(ifi_hwassist);
4550 if_data->ifi_reserved1 = 0;
4551 if_data->ifi_reserved2 = 0;
4552 #undef COPYFIELD32_ATOMIC
4553 #undef COPYFIELD32
4554 #undef COPYFIELD
4555 }
4556
4557 __private_extern__ void
4558 if_data_internal_to_if_data64(struct ifnet *ifp,
4559 const struct if_data_internal *if_data_int,
4560 struct if_data64 *if_data64)
4561 {
4562 #pragma unused(ifp)
4563 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
4564 #define COPYFIELD64_ATOMIC(fld) do { \
4565 atomic_get_64(if_data64->fld, \
4566 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4567 } while (0)
4568
4569 COPYFIELD64(ifi_type);
4570 COPYFIELD64(ifi_typelen);
4571 COPYFIELD64(ifi_physical);
4572 COPYFIELD64(ifi_addrlen);
4573 COPYFIELD64(ifi_hdrlen);
4574 COPYFIELD64(ifi_recvquota);
4575 COPYFIELD64(ifi_xmitquota);
4576 if_data64->ifi_unused1 = 0;
4577 COPYFIELD64(ifi_mtu);
4578 COPYFIELD64(ifi_metric);
4579 COPYFIELD64(ifi_baudrate);
4580
4581 COPYFIELD64_ATOMIC(ifi_ipackets);
4582 COPYFIELD64_ATOMIC(ifi_ierrors);
4583 COPYFIELD64_ATOMIC(ifi_opackets);
4584 COPYFIELD64_ATOMIC(ifi_oerrors);
4585 COPYFIELD64_ATOMIC(ifi_collisions);
4586 COPYFIELD64_ATOMIC(ifi_ibytes);
4587 COPYFIELD64_ATOMIC(ifi_obytes);
4588 COPYFIELD64_ATOMIC(ifi_imcasts);
4589 COPYFIELD64_ATOMIC(ifi_omcasts);
4590 COPYFIELD64_ATOMIC(ifi_iqdrops);
4591 COPYFIELD64_ATOMIC(ifi_noproto);
4592
4593 /*
4594 * Note these two fields are actually 32 bit, so doing
4595 * COPYFIELD64_ATOMIC will cause them to be misaligned
4596 */
4597 COPYFIELD64(ifi_recvtiming);
4598 COPYFIELD64(ifi_xmittiming);
4599
4600 if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
4601 if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
4602
4603 if_data64->ifi_lastchange.tv_sec += boottime_sec();
4604
4605 #undef COPYFIELD64
4606 }
4607
4608 __private_extern__ void
4609 if_copy_traffic_class(struct ifnet *ifp,
4610 struct if_traffic_class *if_tc)
4611 {
4612 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
4613 atomic_get_64(if_tc->fld, \
4614 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
4615 } while (0)
4616
4617 bzero(if_tc, sizeof (*if_tc));
4618 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
4619 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
4620 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
4621 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
4622 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
4623 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
4624 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
4625 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
4626 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
4627 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
4628 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
4629 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
4630 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
4631 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
4632 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
4633 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
4634 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
4635 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
4636 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
4637 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
4638
4639 #undef COPY_IF_TC_FIELD64_ATOMIC
4640 }
4641
4642 void
4643 if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
4644 {
4645 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
4646 atomic_get_64(if_de->fld, \
4647 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
4648 } while (0)
4649
4650 bzero(if_de, sizeof (*if_de));
4651 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
4652 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
4653 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
4654 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
4655
4656 #undef COPY_IF_DE_FIELD64_ATOMIC
4657 }
4658
4659 void
4660 if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
4661 {
4662 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
4663 atomic_get_64(if_ps->ifi_tcp_##fld, \
4664 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
4665 } while (0)
4666
4667 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
4668 atomic_get_64(if_ps->ifi_udp_##fld, \
4669 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
4670 } while (0)
4671
4672 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
4673 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
4674 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
4675 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
4676 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
4677 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
4678 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
4679 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
4680 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
4681 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
4682 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
4683 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
4684 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
4685 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
4686
4687 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
4688 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
4689 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
4690 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
4691 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
4692 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
4693 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
4694 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
4695
4696 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
4697 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
4698 }
4699
4700 void
4701 if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
4702 {
4703 bzero(if_rs, sizeof (*if_rs));
4704 if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1))
4705 return;
4706
4707 /* by now, ifnet will stay attached so if_inp must be valid */
4708 VERIFY(ifp->if_inp != NULL);
4709 bcopy(&ifp->if_inp->pstats, if_rs, sizeof (*if_rs));
4710
4711 /* Release the IO refcnt */
4712 ifnet_decr_iorefcnt(ifp);
4713 }
4714
4715 struct ifaddr *
4716 ifa_remref(struct ifaddr *ifa, int locked)
4717 {
4718 if (!locked)
4719 IFA_LOCK_SPIN(ifa);
4720 else
4721 IFA_LOCK_ASSERT_HELD(ifa);
4722
4723 if (ifa->ifa_refcnt == 0)
4724 panic("%s: ifa %p negative refcnt\n", __func__, ifa);
4725 else if (ifa->ifa_trace != NULL)
4726 (*ifa->ifa_trace)(ifa, FALSE);
4727 if (--ifa->ifa_refcnt == 0) {
4728 if (ifa->ifa_debug & IFD_ATTACHED)
4729 panic("ifa %p attached to ifp is being freed\n", ifa);
4730 /*
4731 * Some interface addresses are allocated either statically
4732 * or carved out of a larger block. Only free it if it was
4733 * allocated via MALLOC or via the corresponding per-address
4734 * family allocator. Otherwise, leave it alone.
4735 */
4736 if (ifa->ifa_debug & IFD_ALLOC) {
4737 if (ifa->ifa_free == NULL) {
4738 IFA_UNLOCK(ifa);
4739 FREE(ifa, M_IFADDR);
4740 } else {
4741 /* Become a regular mutex */
4742 IFA_CONVERT_LOCK(ifa);
4743 /* callee will unlock */
4744 (*ifa->ifa_free)(ifa);
4745 }
4746 } else {
4747 IFA_UNLOCK(ifa);
4748 }
4749 ifa = NULL;
4750 }
4751
4752 if (!locked && ifa != NULL)
4753 IFA_UNLOCK(ifa);
4754
4755 return (ifa);
4756 }
4757
4758 void
4759 ifa_addref(struct ifaddr *ifa, int locked)
4760 {
4761 if (!locked)
4762 IFA_LOCK_SPIN(ifa);
4763 else
4764 IFA_LOCK_ASSERT_HELD(ifa);
4765
4766 if (++ifa->ifa_refcnt == 0) {
4767 panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
4768 /* NOTREACHED */
4769 } else if (ifa->ifa_trace != NULL) {
4770 (*ifa->ifa_trace)(ifa, TRUE);
4771 }
4772 if (!locked)
4773 IFA_UNLOCK(ifa);
4774 }
4775
4776 void
4777 ifa_lock_init(struct ifaddr *ifa)
4778 {
4779 lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
4780 }
4781
4782 void
4783 ifa_lock_destroy(struct ifaddr *ifa)
4784 {
4785 IFA_LOCK_ASSERT_NOTHELD(ifa);
4786 lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
4787 }
4788
4789 /*
4790 * 'i' group ioctls.
4791 *
4792 * The switch statement below does nothing at runtime, as it serves as a
4793 * compile time check to ensure that all of the socket 'i' ioctls (those
4794 * in the 'i' group going thru soo_ioctl) that are made available by the
4795 * networking stack is unique. This works as long as this routine gets
4796 * updated each time a new interface ioctl gets added.
4797 *
4798 * Any failures at compile time indicates duplicated ioctl values.
4799 */
4800 static __attribute__((unused)) void
4801 ifioctl_cassert(void)
4802 {
4803 /*
4804 * This is equivalent to _CASSERT() and the compiler wouldn't
4805 * generate any instructions, thus for compile time only.
4806 */
4807 switch ((u_long)0) {
4808 case 0:
4809
4810 /* bsd/net/if_ppp.h */
4811 case SIOCGPPPSTATS:
4812 case SIOCGPPPCSTATS:
4813
4814 #if INET6
4815 /* bsd/netinet6/in6_var.h */
4816 case SIOCSIFADDR_IN6:
4817 case SIOCGIFADDR_IN6:
4818 case SIOCSIFDSTADDR_IN6:
4819 case SIOCSIFNETMASK_IN6:
4820 case SIOCGIFDSTADDR_IN6:
4821 case SIOCGIFNETMASK_IN6:
4822 case SIOCDIFADDR_IN6:
4823 case SIOCAIFADDR_IN6_32:
4824 case SIOCAIFADDR_IN6_64:
4825 case SIOCSIFPHYADDR_IN6_32:
4826 case SIOCSIFPHYADDR_IN6_64:
4827 case SIOCGIFPSRCADDR_IN6:
4828 case SIOCGIFPDSTADDR_IN6:
4829 case SIOCGIFAFLAG_IN6:
4830 case SIOCGDRLST_IN6_32:
4831 case SIOCGDRLST_IN6_64:
4832 case SIOCGPRLST_IN6_32:
4833 case SIOCGPRLST_IN6_64:
4834 case OSIOCGIFINFO_IN6:
4835 case SIOCGIFINFO_IN6:
4836 case SIOCSNDFLUSH_IN6:
4837 case SIOCGNBRINFO_IN6_32:
4838 case SIOCGNBRINFO_IN6_64:
4839 case SIOCSPFXFLUSH_IN6:
4840 case SIOCSRTRFLUSH_IN6:
4841 case SIOCGIFALIFETIME_IN6:
4842 case SIOCSIFALIFETIME_IN6:
4843 case SIOCGIFSTAT_IN6:
4844 case SIOCGIFSTAT_ICMP6:
4845 case SIOCSDEFIFACE_IN6_32:
4846 case SIOCSDEFIFACE_IN6_64:
4847 case SIOCGDEFIFACE_IN6_32:
4848 case SIOCGDEFIFACE_IN6_64:
4849 case SIOCSIFINFO_FLAGS:
4850 case SIOCSSCOPE6:
4851 case SIOCGSCOPE6:
4852 case SIOCGSCOPE6DEF:
4853 case SIOCSIFPREFIX_IN6:
4854 case SIOCGIFPREFIX_IN6:
4855 case SIOCDIFPREFIX_IN6:
4856 case SIOCAIFPREFIX_IN6:
4857 case SIOCCIFPREFIX_IN6:
4858 case SIOCSGIFPREFIX_IN6:
4859 case SIOCPROTOATTACH_IN6_32:
4860 case SIOCPROTOATTACH_IN6_64:
4861 case SIOCPROTODETACH_IN6:
4862 case SIOCLL_START_32:
4863 case SIOCLL_START_64:
4864 case SIOCLL_STOP:
4865 case SIOCAUTOCONF_START:
4866 case SIOCAUTOCONF_STOP:
4867 case SIOCSETROUTERMODE_IN6:
4868 case SIOCLL_CGASTART_32:
4869 case SIOCLL_CGASTART_64:
4870 case SIOCGIFCGAPREP_IN6:
4871 case SIOCSIFCGAPREP_IN6:
4872 #endif /* INET6 */
4873
4874 /* bsd/sys/sockio.h */
4875 case SIOCSIFADDR:
4876 case OSIOCGIFADDR:
4877 case SIOCSIFDSTADDR:
4878 case OSIOCGIFDSTADDR:
4879 case SIOCSIFFLAGS:
4880 case SIOCGIFFLAGS:
4881 case OSIOCGIFBRDADDR:
4882 case SIOCSIFBRDADDR:
4883 case OSIOCGIFCONF32:
4884 case OSIOCGIFCONF64:
4885 case OSIOCGIFNETMASK:
4886 case SIOCSIFNETMASK:
4887 case SIOCGIFMETRIC:
4888 case SIOCSIFMETRIC:
4889 case SIOCDIFADDR:
4890 case SIOCAIFADDR:
4891
4892 case SIOCGIFADDR:
4893 case SIOCGIFDSTADDR:
4894 case SIOCGIFBRDADDR:
4895 case SIOCGIFCONF32:
4896 case SIOCGIFCONF64:
4897 case SIOCGIFNETMASK:
4898 case SIOCAUTOADDR:
4899 case SIOCAUTONETMASK:
4900 case SIOCARPIPLL:
4901
4902 case SIOCADDMULTI:
4903 case SIOCDELMULTI:
4904 case SIOCGIFMTU:
4905 case SIOCSIFMTU:
4906 case SIOCGIFPHYS:
4907 case SIOCSIFPHYS:
4908 case SIOCSIFMEDIA:
4909 case SIOCGIFMEDIA32:
4910 case SIOCGIFMEDIA64:
4911 case SIOCSIFGENERIC:
4912 case SIOCGIFGENERIC:
4913 case SIOCRSLVMULTI:
4914
4915 case SIOCSIFLLADDR:
4916 case SIOCGIFSTATUS:
4917 case SIOCSIFPHYADDR:
4918 case SIOCGIFPSRCADDR:
4919 case SIOCGIFPDSTADDR:
4920 case SIOCDIFPHYADDR:
4921
4922 case SIOCGIFDEVMTU:
4923 case SIOCSIFALTMTU:
4924 case SIOCGIFALTMTU:
4925 case SIOCSIFBOND:
4926 case SIOCGIFBOND:
4927
4928 case SIOCPROTOATTACH:
4929 case SIOCPROTODETACH:
4930
4931 case SIOCSIFCAP:
4932 case SIOCGIFCAP:
4933
4934 case SIOCIFCREATE:
4935 case SIOCIFDESTROY:
4936 case SIOCIFCREATE2:
4937
4938 case SIOCSDRVSPEC32:
4939 case SIOCGDRVSPEC32:
4940 case SIOCSDRVSPEC64:
4941 case SIOCGDRVSPEC64:
4942
4943 case SIOCSIFVLAN:
4944 case SIOCGIFVLAN:
4945
4946 case SIOCIFGCLONERS32:
4947 case SIOCIFGCLONERS64:
4948
4949 case SIOCGIFASYNCMAP:
4950 case SIOCSIFASYNCMAP:
4951 #if CONFIG_MACF_NET
4952 case SIOCGIFMAC:
4953 case SIOCSIFMAC:
4954 #endif /* CONFIG_MACF_NET */
4955 case SIOCSIFKPI:
4956 case SIOCGIFKPI:
4957
4958 case SIOCGIFWAKEFLAGS:
4959
4960 case SIOCGIFGETRTREFCNT:
4961 case SIOCGIFLINKQUALITYMETRIC:
4962 case SIOCSIFOPPORTUNISTIC:
4963 case SIOCGIFOPPORTUNISTIC:
4964 case SIOCSETROUTERMODE:
4965 case SIOCGIFEFLAGS:
4966 case SIOCSIFDESC:
4967 case SIOCGIFDESC:
4968 case SIOCSIFLINKPARAMS:
4969 case SIOCGIFLINKPARAMS:
4970 case SIOCGIFQUEUESTATS:
4971 case SIOCSIFTHROTTLE:
4972 case SIOCGIFTHROTTLE:
4973
4974 case SIOCGASSOCIDS32:
4975 case SIOCGASSOCIDS64:
4976 case SIOCGCONNIDS32:
4977 case SIOCGCONNIDS64:
4978 case SIOCGCONNINFO32:
4979 case SIOCGCONNINFO64:
4980 case SIOCSCONNORDER:
4981 case SIOCGCONNORDER:
4982
4983 case SIOCSIFLOG:
4984 case SIOCGIFLOG:
4985 case SIOCGIFDELEGATE:
4986 case SIOCGIFLLADDR:
4987 case SIOCGIFTYPE:
4988 case SIOCGIFEXPENSIVE:
4989 case SIOCSIFEXPENSIVE:
4990 case SIOCGIF2KCL:
4991 case SIOCSIF2KCL:
4992 case SIOCGSTARTDELAY:
4993
4994 case SIOCAIFAGENTID:
4995 case SIOCDIFAGENTID:
4996 case SIOCGIFAGENTIDS32:
4997 case SIOCGIFAGENTIDS64:
4998 case SIOCGIFAGENTDATA32:
4999 case SIOCGIFAGENTDATA64:
5000 case SIOCGIFAGENTLIST32:
5001 case SIOCGIFAGENTLIST64:
5002
5003
5004 case SIOCSIFINTERFACESTATE:
5005 case SIOCGIFINTERFACESTATE:
5006 case SIOCSIFPROBECONNECTIVITY:
5007 case SIOCGIFPROBECONNECTIVITY:
5008
5009 case SIOCGIFFUNCTIONALTYPE:
5010 case SIOCSIFNETSIGNATURE:
5011 case SIOCGIFNETSIGNATURE:
5012
5013 case SIOCGECNMODE:
5014 case SIOCSECNMODE:
5015
5016 case SIOCSQOSMARKINGMODE:
5017 case SIOCSQOSMARKINGENABLED:
5018 case SIOCGQOSMARKINGMODE:
5019 case SIOCGQOSMARKINGENABLED:
5020 ;
5021 }
5022 }
5023
5024 /*
5025 * XXX: This API is only used by BSD stack and for now will always return 0.
5026 * For Skywalk native drivers, preamble space need not be allocated in mbuf
5027 * as the preamble will be reserved in the translated skywalk packet
5028 * which is transmitted to the driver.
5029 * For Skywalk compat drivers currently headroom is always set to zero.
5030 */
5031 uint32_t
5032 ifnet_mbuf_packetpreamblelen(struct ifnet *ifp)
5033 {
5034 #pragma unused(ifp)
5035 return (0);
5036 }
5037
5038 /* The following is used to enqueue work items for interface events */
5039 struct intf_event {
5040 struct ifnet *ifp;
5041 union sockaddr_in_4_6 addr;
5042 uint32_t intf_event_code;
5043 };
5044
5045 static void
5046 intf_event_callback(void *arg)
5047 {
5048 struct intf_event *p_intf_ev = (struct intf_event *)arg;
5049
5050 /* Call this before we walk the tree */
5051 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt, ifnet_event, p_intf_ev->ifp,
5052 (struct sockaddr *)&(p_intf_ev->addr), p_intf_ev->intf_event_code);
5053 }
5054
5055 struct intf_event_nwk_wq_entry {
5056 struct nwk_wq_entry nwk_wqe;
5057 struct intf_event intf_ev_arg;
5058 };
5059
5060 void
5061 intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp, struct sockaddr *addrp,
5062 uint32_t intf_event_code)
5063 {
5064 #pragma unused(addrp)
5065 struct intf_event_nwk_wq_entry *p_intf_ev = NULL;
5066
5067 MALLOC(p_intf_ev, struct intf_event_nwk_wq_entry *,
5068 sizeof(struct intf_event_nwk_wq_entry),
5069 M_NWKWQ, M_WAITOK | M_ZERO);
5070
5071 p_intf_ev->intf_ev_arg.ifp = ifp;
5072 /*
5073 * XXX Not using addr in the arg. This will be used
5074 * once we need IP address add/delete events
5075 */
5076 p_intf_ev->intf_ev_arg.intf_event_code = intf_event_code;
5077 p_intf_ev->nwk_wqe.func = intf_event_callback;
5078 p_intf_ev->nwk_wqe.is_arg_managed = TRUE;
5079 p_intf_ev->nwk_wqe.arg = &p_intf_ev->intf_ev_arg;
5080 nwk_wq_enqueue((struct nwk_wq_entry*)p_intf_ev);
5081 }