]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if.c
33e626245c7d8a04779e83a472f05293e31fbbf6
[apple/xnu.git] / bsd / net / if.c
1 /*
2 * Copyright (c) 2000-2018 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
336 __private_extern__ void
337 if_detach_ifa(struct ifnet *ifp, struct ifaddr *ifa)
338 {
339 if_detach_ifa_common(ifp, ifa, 0);
340 }
341
342 __private_extern__ void
343 if_detach_link_ifa(struct ifnet *ifp, struct ifaddr *ifa)
344 {
345 if_detach_ifa_common(ifp, ifa, 1);
346 }
347
348 static void
349 if_detach_ifa_common(struct ifnet *ifp, struct ifaddr *ifa, int link)
350 {
351 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
352 IFA_LOCK_ASSERT_HELD(ifa);
353
354 if (link && !(ifa->ifa_debug & IFD_LINK)) {
355 panic("%s: Unexpected non-link address ifa=%p", __func__, ifa);
356 /* NOTREACHED */
357 } else if (link && ifa != TAILQ_FIRST(&ifp->if_addrhead)) {
358 panic("%s: Link address ifa=%p not first", __func__, ifa);
359 /* NOTREACHED */
360 } else if (!link && (ifa->ifa_debug & IFD_LINK)) {
361 panic("%s: Unexpected link address ifa=%p", __func__, ifa);
362 /* NOTREACHED */
363 } else if (!(ifa->ifa_debug & IFD_ATTACHED)) {
364 panic("%s: Attempt to detach an unattached address ifa=%p",
365 __func__, ifa);
366 /* NOTREACHED */
367 } else if (ifa->ifa_ifp != ifp) {
368 panic("%s: Mismatch ifa_ifp=%p, ifp=%p", __func__,
369 ifa->ifa_ifp, ifp);
370 /* NOTREACHED */
371 } else if (ifa->ifa_debug & IFD_DEBUG) {
372 struct ifaddr *ifa2;
373 TAILQ_FOREACH(ifa2, &ifp->if_addrhead, ifa_link) {
374 if (ifa2 == ifa)
375 break;
376 }
377 if (ifa2 != ifa) {
378 panic("%s: Attempt to detach a stray address ifa=%p",
379 __func__, ifa);
380 /* NOTREACHED */
381 }
382 }
383 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
384 /* This must not be the last reference to the ifaddr */
385 if (IFA_REMREF_LOCKED(ifa) == NULL) {
386 panic("%s: unexpected (missing) refcnt ifa=%p", __func__, ifa);
387 /* NOTREACHED */
388 }
389 ifa->ifa_debug &= ~(IFD_ATTACHED | IFD_DETACHING);
390
391 if (ifa->ifa_detached != NULL)
392 (*ifa->ifa_detached)(ifa);
393
394 }
395
396 #define INITIAL_IF_INDEXLIM 8
397
398 /*
399 * Function: if_next_index
400 * Purpose:
401 * Return the next available interface index.
402 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
403 * added entry when necessary.
404 *
405 * Note:
406 * ifnet_addrs[] is indexed by (if_index - 1), whereas
407 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
408 * always allocate one extra element to hold ifindex2ifnet[0], which
409 * is unused.
410 */
411 int if_next_index(void);
412
413 __private_extern__ int
414 if_next_index(void)
415 {
416 static int if_indexlim = 0;
417 int new_index;
418
419 new_index = ++if_index;
420 if (if_index > if_indexlim) {
421 unsigned n;
422 int new_if_indexlim;
423 caddr_t new_ifnet_addrs;
424 caddr_t new_ifindex2ifnet;
425 caddr_t old_ifnet_addrs;
426
427 old_ifnet_addrs = (caddr_t)ifnet_addrs;
428 if (ifnet_addrs == NULL) {
429 new_if_indexlim = INITIAL_IF_INDEXLIM;
430 } else {
431 new_if_indexlim = if_indexlim << 1;
432 }
433
434 /* allocate space for the larger arrays */
435 n = (2 * new_if_indexlim + 1) * sizeof(caddr_t);
436 new_ifnet_addrs = _MALLOC(n, M_IFADDR, M_WAITOK | M_ZERO);
437 if (new_ifnet_addrs == NULL) {
438 --if_index;
439 return (-1);
440 }
441
442 new_ifindex2ifnet = new_ifnet_addrs
443 + new_if_indexlim * sizeof(caddr_t);
444 if (ifnet_addrs != NULL) {
445 /* copy the existing data */
446 bcopy((caddr_t)ifnet_addrs, new_ifnet_addrs,
447 if_indexlim * sizeof(caddr_t));
448 bcopy((caddr_t)ifindex2ifnet,
449 new_ifindex2ifnet,
450 (if_indexlim + 1) * sizeof(caddr_t));
451 }
452
453 /* switch to the new tables and size */
454 ifnet_addrs = (struct ifaddr **)(void *)new_ifnet_addrs;
455 ifindex2ifnet = (struct ifnet **)(void *)new_ifindex2ifnet;
456 if_indexlim = new_if_indexlim;
457
458 /* release the old data */
459 if (old_ifnet_addrs != NULL) {
460 _FREE((caddr_t)old_ifnet_addrs, M_IFADDR);
461 }
462 }
463 return (new_index);
464 }
465
466 /*
467 * Create a clone network interface.
468 */
469 static int
470 if_clone_create(char *name, int len, void *params)
471 {
472 struct if_clone *ifc;
473 char *dp;
474 int wildcard;
475 u_int32_t bytoff, bitoff;
476 u_int32_t unit;
477 int err;
478
479 ifc = if_clone_lookup(name, &unit);
480 if (ifc == NULL)
481 return (EINVAL);
482
483 if (ifunit(name) != NULL)
484 return (EEXIST);
485
486 bytoff = bitoff = 0;
487 wildcard = (unit == UINT32_MAX);
488 /*
489 * Find a free unit if none was given.
490 */
491 if (wildcard) {
492 while ((bytoff < ifc->ifc_bmlen) &&
493 (ifc->ifc_units[bytoff] == 0xff))
494 bytoff++;
495 if (bytoff >= ifc->ifc_bmlen)
496 return (ENOSPC);
497 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
498 bitoff++;
499 unit = (bytoff << 3) + bitoff;
500 }
501
502 if (unit > ifc->ifc_maxunit)
503 return (ENXIO);
504
505 err = (*ifc->ifc_create)(ifc, unit, params);
506 if (err != 0)
507 return (err);
508
509 if (!wildcard) {
510 bytoff = unit >> 3;
511 bitoff = unit - (bytoff << 3);
512 }
513
514 /*
515 * Allocate the unit in the bitmap.
516 */
517 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
518 ("%s: bit is already set", __func__));
519 ifc->ifc_units[bytoff] |= (1 << bitoff);
520
521 /* In the wildcard case, we need to update the name. */
522 if (wildcard) {
523 for (dp = name; *dp != '\0'; dp++);
524 if (snprintf(dp, len - (dp-name), "%d", unit) >
525 len - (dp-name) - 1) {
526 /*
527 * This can only be a programmer error and
528 * there's no straightforward way to recover if
529 * it happens.
530 */
531 panic("%s: interface name too long", __func__);
532 /* NOTREACHED */
533 }
534
535 }
536
537 return (0);
538 }
539
540 /*
541 * Destroy a clone network interface.
542 */
543 static int
544 if_clone_destroy(const char *name)
545 {
546 struct if_clone *ifc;
547 struct ifnet *ifp;
548 int bytoff, bitoff;
549 u_int32_t unit;
550
551 ifc = if_clone_lookup(name, &unit);
552 if (ifc == NULL)
553 return (EINVAL);
554
555 if (unit < ifc->ifc_minifs)
556 return (EINVAL);
557
558 ifp = ifunit(name);
559 if (ifp == NULL)
560 return (ENXIO);
561
562 if (ifc->ifc_destroy == NULL)
563 return (EOPNOTSUPP);
564
565 (*ifc->ifc_destroy)(ifp);
566
567 /*
568 * Compute offset in the bitmap and deallocate the unit.
569 */
570 bytoff = unit >> 3;
571 bitoff = unit - (bytoff << 3);
572 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
573 ("%s: bit is already cleared", __func__));
574 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
575 return (0);
576 }
577
578 /*
579 * Look up a network interface cloner.
580 */
581
582 __private_extern__ struct if_clone *
583 if_clone_lookup(const char *name, u_int32_t *unitp)
584 {
585 struct if_clone *ifc;
586 const char *cp;
587 u_int32_t i;
588
589 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL; ) {
590 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
591 if (ifc->ifc_name[i] != *cp)
592 goto next_ifc;
593 }
594 goto found_name;
595 next_ifc:
596 ifc = LIST_NEXT(ifc, ifc_list);
597 }
598
599 /* No match. */
600 return ((struct if_clone *)NULL);
601
602 found_name:
603 if (*cp == '\0') {
604 i = UINT32_MAX;
605 } else {
606 for (i = 0; *cp != '\0'; cp++) {
607 if (*cp < '0' || *cp > '9') {
608 /* Bogus unit number. */
609 return (NULL);
610 }
611 i = (i * 10) + (*cp - '0');
612 }
613 }
614
615 if (unitp != NULL)
616 *unitp = i;
617 return (ifc);
618 }
619
620 /*
621 * Register a network interface cloner.
622 */
623 int
624 if_clone_attach(struct if_clone *ifc)
625 {
626 int bytoff, bitoff;
627 int err;
628 int len, maxclone;
629 u_int32_t unit;
630
631 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
632 ("%s: %s requested more units then allowed (%d > %d)",
633 __func__, ifc->ifc_name, ifc->ifc_minifs,
634 ifc->ifc_maxunit + 1));
635 /*
636 * Compute bitmap size and allocate it.
637 */
638 maxclone = ifc->ifc_maxunit + 1;
639 len = maxclone >> 3;
640 if ((len << 3) < maxclone)
641 len++;
642 ifc->ifc_units = _MALLOC(len, M_CLONE, M_WAITOK | M_ZERO);
643 if (ifc->ifc_units == NULL)
644 return (ENOBUFS);
645 ifc->ifc_bmlen = len;
646
647 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
648 if_cloners_count++;
649
650 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
651 err = (*ifc->ifc_create)(ifc, unit, NULL);
652 KASSERT(err == 0,
653 ("%s: failed to create required interface %s%d",
654 __func__, ifc->ifc_name, unit));
655
656 /* Allocate the unit in the bitmap. */
657 bytoff = unit >> 3;
658 bitoff = unit - (bytoff << 3);
659 ifc->ifc_units[bytoff] |= (1 << bitoff);
660 }
661
662 return (0);
663 }
664
665 /*
666 * Unregister a network interface cloner.
667 */
668 void
669 if_clone_detach(struct if_clone *ifc)
670 {
671 LIST_REMOVE(ifc, ifc_list);
672 FREE(ifc->ifc_units, M_CLONE);
673 if_cloners_count--;
674 }
675
676 /*
677 * Provide list of interface cloners to userspace.
678 */
679 static int
680 if_clone_list(int count, int *ret_total, user_addr_t dst)
681 {
682 char outbuf[IFNAMSIZ];
683 struct if_clone *ifc;
684 int error = 0;
685
686 *ret_total = if_cloners_count;
687 if (dst == USER_ADDR_NULL) {
688 /* Just asking how many there are. */
689 return (0);
690 }
691
692 if (count < 0)
693 return (EINVAL);
694
695 count = (if_cloners_count < count) ? if_cloners_count : count;
696
697 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
698 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
699 bzero(outbuf, sizeof(outbuf));
700 strlcpy(outbuf, 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
1671 IFCQ_LOCK(ifq);
1672 if (!IFCQ_IS_READY(ifq)) {
1673 error = ENXIO;
1674 IFCQ_UNLOCK(ifq);
1675 break;
1676 }
1677 bcopy(&iflpr->iflpr_output_tbr_rate, &tb.rate,
1678 sizeof (tb.rate));
1679 bcopy(&iflpr->iflpr_output_tbr_percent, &tb.percent,
1680 sizeof (tb.percent));
1681 error = ifclassq_tbr_set(ifq, &tb, TRUE);
1682 IFCQ_UNLOCK(ifq);
1683 break;
1684 }
1685
1686 case SIOCGIFLINKPARAMS: { /* struct if_linkparamsreq */
1687 u_int32_t sched_type = PKTSCHEDT_NONE, flags = 0;
1688 u_int64_t tbr_bw = 0, tbr_pct = 0;
1689
1690 IFCQ_LOCK(ifq);
1691
1692 if (IFCQ_IS_ENABLED(ifq))
1693 sched_type = ifq->ifcq_type;
1694
1695 bcopy(&sched_type, &iflpr->iflpr_output_sched,
1696 sizeof (iflpr->iflpr_output_sched));
1697
1698 if (IFCQ_TBR_IS_ENABLED(ifq)) {
1699 tbr_bw = ifq->ifcq_tbr.tbr_rate_raw;
1700 tbr_pct = ifq->ifcq_tbr.tbr_percent;
1701 }
1702 bcopy(&tbr_bw, &iflpr->iflpr_output_tbr_rate,
1703 sizeof (iflpr->iflpr_output_tbr_rate));
1704 bcopy(&tbr_pct, &iflpr->iflpr_output_tbr_percent,
1705 sizeof (iflpr->iflpr_output_tbr_percent));
1706 IFCQ_UNLOCK(ifq);
1707
1708 if (ifp->if_output_sched_model ==
1709 IFNET_SCHED_MODEL_DRIVER_MANAGED)
1710 flags |= IFLPRF_DRVMANAGED;
1711 bcopy(&flags, &iflpr->iflpr_flags, sizeof (iflpr->iflpr_flags));
1712 bcopy(&ifp->if_output_bw, &iflpr->iflpr_output_bw,
1713 sizeof (iflpr->iflpr_output_bw));
1714 bcopy(&ifp->if_input_bw, &iflpr->iflpr_input_bw,
1715 sizeof (iflpr->iflpr_input_bw));
1716 bcopy(&ifp->if_output_lt, &iflpr->iflpr_output_lt,
1717 sizeof (iflpr->iflpr_output_lt));
1718 bcopy(&ifp->if_input_lt, &iflpr->iflpr_input_lt,
1719 sizeof (iflpr->iflpr_input_lt));
1720 break;
1721 }
1722
1723 default:
1724 VERIFY(0);
1725 /* NOTREACHED */
1726 }
1727
1728 return (error);
1729 }
1730
1731 static __attribute__((noinline)) int
1732 ifioctl_qstats(struct ifnet *ifp, u_long cmd, caddr_t data)
1733 {
1734 struct if_qstatsreq *ifqr = (struct if_qstatsreq *)(void *)data;
1735 u_int32_t ifqr_len, ifqr_slot;
1736 int error = 0;
1737
1738 VERIFY(ifp != NULL);
1739
1740 switch (cmd) {
1741 case SIOCGIFQUEUESTATS: { /* struct if_qstatsreq */
1742 bcopy(&ifqr->ifqr_slot, &ifqr_slot, sizeof (ifqr_slot));
1743 bcopy(&ifqr->ifqr_len, &ifqr_len, sizeof (ifqr_len));
1744 error = ifclassq_getqstats(&ifp->if_snd, ifqr_slot,
1745 ifqr->ifqr_buf, &ifqr_len);
1746 if (error != 0)
1747 ifqr_len = 0;
1748 bcopy(&ifqr_len, &ifqr->ifqr_len, sizeof (ifqr_len));
1749 break;
1750 }
1751
1752 default:
1753 VERIFY(0);
1754 /* NOTREACHED */
1755 }
1756
1757 return (error);
1758 }
1759
1760 static __attribute__((noinline)) int
1761 ifioctl_throttle(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1762 {
1763 struct if_throttlereq *ifthr = (struct if_throttlereq *)(void *)data;
1764 u_int32_t ifthr_level;
1765 int error = 0;
1766
1767 VERIFY(ifp != NULL);
1768
1769 switch (cmd) {
1770 case SIOCSIFTHROTTLE: { /* struct if_throttlereq */
1771 /*
1772 * XXX: Use priv_check_cred() instead of root check?
1773 */
1774 if ((error = proc_suser(p)) != 0)
1775 break;
1776
1777 bcopy(&ifthr->ifthr_level, &ifthr_level, sizeof (ifthr_level));
1778 error = ifnet_set_throttle(ifp, ifthr_level);
1779 if (error == EALREADY)
1780 error = 0;
1781 break;
1782 }
1783
1784 case SIOCGIFTHROTTLE: { /* struct if_throttlereq */
1785 if ((error = ifnet_get_throttle(ifp, &ifthr_level)) == 0) {
1786 bcopy(&ifthr_level, &ifthr->ifthr_level,
1787 sizeof (ifthr_level));
1788 }
1789 break;
1790 }
1791
1792 default:
1793 VERIFY(0);
1794 /* NOTREACHED */
1795 }
1796
1797 return (error);
1798 }
1799
1800 static int
1801 ifioctl_getnetagents(struct ifnet *ifp, u_int32_t *count, user_addr_t uuid_p)
1802 {
1803 int error = 0;
1804 u_int32_t index = 0;
1805 u_int32_t valid_netagent_count = 0;
1806 *count = 0;
1807
1808 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_SHARED);
1809
1810 if (ifp->if_agentids != NULL) {
1811 for (index = 0; index < ifp->if_agentcount; index++) {
1812 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
1813 if (!uuid_is_null(*netagent_uuid)) {
1814 if (uuid_p != USER_ADDR_NULL) {
1815 error = copyout(netagent_uuid,
1816 uuid_p + sizeof(uuid_t) * valid_netagent_count,
1817 sizeof(uuid_t));
1818 if (error != 0) {
1819 return (error);
1820 }
1821 }
1822 valid_netagent_count++;
1823 }
1824 }
1825 }
1826 *count = valid_netagent_count;
1827
1828 return (0);
1829 }
1830
1831 #define IF_MAXAGENTS 64
1832 #define IF_AGENT_INCREMENT 8
1833 static int
1834 if_add_netagent_locked(struct ifnet *ifp, uuid_t new_agent_uuid)
1835 {
1836 uuid_t *first_empty_slot = NULL;
1837 u_int32_t index = 0;
1838 bool already_added = FALSE;
1839
1840 if (ifp->if_agentids != NULL) {
1841 for (index = 0; index < ifp->if_agentcount; index++) {
1842 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
1843 if (uuid_compare(*netagent_uuid, new_agent_uuid) == 0) {
1844 /* Already present, ignore */
1845 already_added = TRUE;
1846 break;
1847 }
1848 if (first_empty_slot == NULL &&
1849 uuid_is_null(*netagent_uuid)) {
1850 first_empty_slot = netagent_uuid;
1851 }
1852 }
1853 }
1854 if (already_added) {
1855 /* Already added agent, don't return an error */
1856 return (0);
1857 }
1858 if (first_empty_slot == NULL) {
1859 if (ifp->if_agentcount >= IF_MAXAGENTS) {
1860 /* No room for another netagent UUID, bail */
1861 return (ENOMEM);
1862 } else {
1863 /* Calculate new array size */
1864 u_int32_t new_agent_count =
1865 MIN(ifp->if_agentcount + IF_AGENT_INCREMENT,
1866 IF_MAXAGENTS);
1867
1868 /* Reallocate array */
1869 uuid_t *new_agent_array = _REALLOC(ifp->if_agentids,
1870 sizeof(uuid_t) * new_agent_count, M_NETAGENT,
1871 M_WAITOK | M_ZERO);
1872 if (new_agent_array == NULL) {
1873 return (ENOMEM);
1874 }
1875
1876 /* Save new array */
1877 ifp->if_agentids = new_agent_array;
1878
1879 /* Set first empty slot */
1880 first_empty_slot =
1881 &(ifp->if_agentids[ifp->if_agentcount]);
1882
1883 /* Save new array length */
1884 ifp->if_agentcount = new_agent_count;
1885 }
1886 }
1887 uuid_copy(*first_empty_slot, new_agent_uuid);
1888 netagent_post_updated_interfaces(new_agent_uuid);
1889 return (0);
1890 }
1891
1892 int
1893 if_add_netagent(struct ifnet *ifp, uuid_t new_agent_uuid)
1894 {
1895 VERIFY(ifp != NULL);
1896
1897 ifnet_lock_exclusive(ifp);
1898
1899 int error = if_add_netagent_locked(ifp, new_agent_uuid);
1900
1901 ifnet_lock_done(ifp);
1902
1903 return (error);
1904 }
1905
1906 static int
1907 if_delete_netagent_locked(struct ifnet *ifp, uuid_t remove_agent_uuid)
1908 {
1909 u_int32_t index = 0;
1910 bool removed_agent_id = FALSE;
1911
1912 if (ifp->if_agentids != NULL) {
1913 for (index = 0; index < ifp->if_agentcount; index++) {
1914 uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
1915 if (uuid_compare(*netagent_uuid,
1916 remove_agent_uuid) == 0) {
1917 uuid_clear(*netagent_uuid);
1918 removed_agent_id = TRUE;
1919 break;
1920 }
1921 }
1922 }
1923 if (removed_agent_id)
1924 netagent_post_updated_interfaces(remove_agent_uuid);
1925
1926 return (0);
1927 }
1928
1929 int
1930 if_delete_netagent(struct ifnet *ifp, uuid_t remove_agent_uuid)
1931 {
1932 VERIFY(ifp != NULL);
1933
1934 ifnet_lock_exclusive(ifp);
1935
1936 int error = if_delete_netagent_locked(ifp, remove_agent_uuid);
1937
1938 ifnet_lock_done(ifp);
1939
1940 return (error);
1941 }
1942
1943 boolean_t
1944 if_check_netagent(struct ifnet *ifp, uuid_t find_agent_uuid)
1945 {
1946 boolean_t found = FALSE;
1947
1948 if (!ifp || uuid_is_null(find_agent_uuid))
1949 return FALSE;
1950
1951 ifnet_lock_shared(ifp);
1952
1953 if (ifp->if_agentids != NULL) {
1954 for (uint32_t index = 0; index < ifp->if_agentcount; index++) {
1955 if (uuid_compare(ifp->if_agentids[index], find_agent_uuid) == 0) {
1956 found = TRUE;
1957 break;
1958 }
1959 }
1960 }
1961
1962 ifnet_lock_done(ifp);
1963
1964 return found;
1965 }
1966
1967 static __attribute__((noinline)) int
1968 ifioctl_netagent(struct ifnet *ifp, u_long cmd, caddr_t data, struct proc *p)
1969 {
1970 struct if_agentidreq *ifar = (struct if_agentidreq *)(void *)data;
1971 union {
1972 struct if_agentidsreq32 s32;
1973 struct if_agentidsreq64 s64;
1974 } u;
1975 int error = 0;
1976
1977 VERIFY(ifp != NULL);
1978
1979 /* Get an io ref count if the interface is attached */
1980 if (!ifnet_is_attached(ifp, 1)) {
1981 return (EOPNOTSUPP);
1982 }
1983
1984 if (cmd == SIOCAIFAGENTID ||
1985 cmd == SIOCDIFAGENTID) {
1986 ifnet_lock_exclusive(ifp);
1987 } else {
1988 ifnet_lock_shared(ifp);
1989 }
1990
1991 switch (cmd) {
1992 case SIOCAIFAGENTID: { /* struct if_agentidreq */
1993 // TODO: Use priv_check_cred() instead of root check
1994 if ((error = proc_suser(p)) != 0) {
1995 break;
1996 }
1997 error = if_add_netagent_locked(ifp, ifar->ifar_uuid);
1998 break;
1999 }
2000 case SIOCDIFAGENTID: { /* struct if_agentidreq */
2001 // TODO: Use priv_check_cred() instead of root check
2002 if ((error = proc_suser(p)) != 0) {
2003 break;
2004 }
2005 error = if_delete_netagent_locked(ifp, ifar->ifar_uuid);
2006 break;
2007 }
2008 case SIOCGIFAGENTIDS32: { /* struct if_agentidsreq32 */
2009 bcopy(data, &u.s32, sizeof(u.s32));
2010 error = ifioctl_getnetagents(ifp, &u.s32.ifar_count,
2011 u.s32.ifar_uuids);
2012 if (error == 0) {
2013 bcopy(&u.s32, data, sizeof(u.s32));
2014 }
2015 break;
2016 }
2017 case SIOCGIFAGENTIDS64: { /* struct if_agentidsreq64 */
2018 bcopy(data, &u.s64, sizeof(u.s64));
2019 error = ifioctl_getnetagents(ifp, &u.s64.ifar_count,
2020 u.s64.ifar_uuids);
2021 if (error == 0) {
2022 bcopy(&u.s64, data, sizeof(u.s64));
2023 }
2024 break;
2025 }
2026 default:
2027 VERIFY(0);
2028 /* NOTREACHED */
2029 }
2030
2031 ifnet_lock_done(ifp);
2032 ifnet_decr_iorefcnt(ifp);
2033
2034 return (error);
2035 }
2036
2037 void
2038 ifnet_clear_netagent(uuid_t netagent_uuid)
2039 {
2040 struct ifnet *ifp = NULL;
2041 u_int32_t index = 0;
2042
2043 ifnet_head_lock_shared();
2044
2045 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2046 ifnet_lock_shared(ifp);
2047 if (ifp->if_agentids != NULL) {
2048 for (index = 0; index < ifp->if_agentcount; index++) {
2049 uuid_t *ifp_netagent_uuid = &(ifp->if_agentids[index]);
2050 if (uuid_compare(*ifp_netagent_uuid, netagent_uuid) == 0) {
2051 uuid_clear(*ifp_netagent_uuid);
2052 }
2053 }
2054 }
2055 ifnet_lock_done(ifp);
2056 }
2057
2058 ifnet_head_done();
2059 }
2060
2061 void
2062 ifnet_increment_generation(ifnet_t interface)
2063 {
2064 OSIncrementAtomic(&interface->if_generation);
2065 }
2066
2067 u_int32_t
2068 ifnet_get_generation(ifnet_t interface)
2069 {
2070 return (interface->if_generation);
2071 }
2072
2073 void
2074 ifnet_remove_from_ordered_list(struct ifnet *ifp)
2075 {
2076 ifnet_head_assert_exclusive();
2077
2078 // Remove from list
2079 TAILQ_REMOVE(&ifnet_ordered_head, ifp, if_ordered_link);
2080 ifp->if_ordered_link.tqe_next = NULL;
2081 ifp->if_ordered_link.tqe_prev = NULL;
2082
2083 // Update ordered count
2084 VERIFY(if_ordered_count > 0);
2085 if_ordered_count--;
2086 }
2087
2088 static int
2089 ifnet_reset_order(u_int32_t *ordered_indices, u_int32_t count)
2090 {
2091 struct ifnet *ifp = NULL;
2092 int error = 0;
2093
2094 ifnet_head_lock_exclusive();
2095 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2096 if (ordered_indices[order_index] == IFSCOPE_NONE ||
2097 ordered_indices[order_index] > (uint32_t)if_index) {
2098 error = EINVAL;
2099 ifnet_head_done();
2100 return (error);
2101 }
2102 }
2103 // Flush current ordered list
2104 for (ifp = TAILQ_FIRST(&ifnet_ordered_head); ifp != NULL;
2105 ifp = TAILQ_FIRST(&ifnet_ordered_head)) {
2106 ifnet_lock_exclusive(ifp);
2107 ifnet_remove_from_ordered_list(ifp);
2108 ifnet_lock_done(ifp);
2109 }
2110
2111 VERIFY(if_ordered_count == 0);
2112
2113 for (u_int32_t order_index = 0; order_index < count; order_index++) {
2114 u_int32_t interface_index = ordered_indices[order_index];
2115 ifp = ifindex2ifnet[interface_index];
2116 if (ifp == NULL) {
2117 continue;
2118 }
2119 ifnet_lock_exclusive(ifp);
2120 TAILQ_INSERT_TAIL(&ifnet_ordered_head, ifp, if_ordered_link);
2121 ifnet_lock_done(ifp);
2122 if_ordered_count++;
2123 }
2124
2125 ifnet_head_done();
2126
2127 necp_update_all_clients();
2128
2129 return (error);
2130 }
2131
2132 int
2133 if_set_qosmarking_mode(struct ifnet *ifp, u_int32_t mode)
2134 {
2135 int error = 0;
2136 u_int32_t old_mode = ifp->if_qosmarking_mode;
2137
2138 switch (mode) {
2139 case IFRTYPE_QOSMARKING_MODE_NONE:
2140 ifp->if_qosmarking_mode = IFRTYPE_QOSMARKING_MODE_NONE;
2141 ifp->if_eflags &= ~IFEF_QOSMARKING_CAPABLE;
2142 break;
2143 case IFRTYPE_QOSMARKING_FASTLANE:
2144 ifp->if_qosmarking_mode = IFRTYPE_QOSMARKING_FASTLANE;
2145 ifp->if_eflags |= IFEF_QOSMARKING_CAPABLE;
2146 if (net_qos_policy_capable_enabled != 0)
2147 ifp->if_eflags |= IFEF_QOSMARKING_ENABLED;
2148 break;
2149 default:
2150 error = EINVAL;
2151 break;
2152 }
2153 if (error == 0 && old_mode != ifp->if_qosmarking_mode) {
2154 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_QOS_MODE_CHANGED,
2155 NULL, sizeof(struct kev_dl_rrc_state));
2156
2157 }
2158 return (error);
2159 }
2160
2161 static __attribute__((noinline)) int
2162 ifioctl_iforder(u_long cmd, caddr_t data)
2163 {
2164 int error = 0;
2165 u_int32_t *ordered_indices = NULL;
2166 if (data == NULL) {
2167 return (EINVAL);
2168 }
2169
2170 switch (cmd) {
2171 case SIOCSIFORDER: { /* struct if_order */
2172 struct if_order *ifo = (struct if_order *)(void *)data;
2173
2174 if (ifo->ifo_count > (u_int32_t)if_index) {
2175 error = EINVAL;
2176 break;
2177 }
2178
2179 size_t length = (ifo->ifo_count * sizeof(u_int32_t));
2180 if (length > 0) {
2181 if (ifo->ifo_ordered_indices == USER_ADDR_NULL) {
2182 error = EINVAL;
2183 break;
2184 }
2185 ordered_indices = _MALLOC(length, M_NECP, M_WAITOK);
2186 if (ordered_indices == NULL) {
2187 error = ENOMEM;
2188 break;
2189 }
2190
2191 error = copyin(ifo->ifo_ordered_indices,
2192 ordered_indices, length);
2193 if (error != 0) {
2194 break;
2195 }
2196
2197 /* ordered_indices should not contain duplicates */
2198 bool found_duplicate = FALSE;
2199 for (uint32_t i = 0; i < (ifo->ifo_count - 1) && !found_duplicate ; i++){
2200 for (uint32_t j = i + 1; j < ifo->ifo_count && !found_duplicate ; j++){
2201 if (ordered_indices[j] == ordered_indices[i]){
2202 error = EINVAL;
2203 found_duplicate = TRUE;
2204 break;
2205 }
2206 }
2207 }
2208 if (found_duplicate) {
2209 break;
2210 }
2211 }
2212
2213 error = ifnet_reset_order(ordered_indices, ifo->ifo_count);
2214
2215 break;
2216 }
2217
2218 case SIOCGIFORDER: { /* struct if_order */
2219 struct if_order *ifo = (struct if_order *)(void *)data;
2220 u_int32_t ordered_count = *((volatile u_int32_t *)&if_ordered_count);
2221
2222 if (ifo->ifo_count == 0 ||
2223 ordered_count == 0) {
2224 ifo->ifo_count = 0;
2225 } else if (ifo->ifo_ordered_indices != USER_ADDR_NULL) {
2226 u_int32_t count_to_copy =
2227 MIN(ordered_count, ifo->ifo_count);
2228 size_t length = (count_to_copy * sizeof(u_int32_t));
2229 struct ifnet *ifp = NULL;
2230 u_int32_t cursor = 0;
2231
2232 ordered_indices = _MALLOC(length, M_NECP, M_WAITOK | M_ZERO);
2233 if (ordered_indices == NULL) {
2234 error = ENOMEM;
2235 break;
2236 }
2237
2238 ifnet_head_lock_shared();
2239 TAILQ_FOREACH(ifp, &ifnet_ordered_head, if_ordered_link) {
2240 if (cursor >= count_to_copy ||
2241 cursor >= if_ordered_count) {
2242 break;
2243 }
2244 ordered_indices[cursor] = ifp->if_index;
2245 cursor++;
2246 }
2247 ifnet_head_done();
2248
2249 /* We might have parsed less than the original length
2250 * because the list could have changed.
2251 */
2252 length = cursor * sizeof(u_int32_t);
2253 ifo->ifo_count = cursor;
2254 error = copyout(ordered_indices,
2255 ifo->ifo_ordered_indices, length);
2256 } else {
2257 error = EINVAL;
2258 }
2259 break;
2260 }
2261
2262 default: {
2263 VERIFY(0);
2264 /* NOTREACHED */
2265 }
2266 }
2267
2268 if (ordered_indices != NULL) {
2269 _FREE(ordered_indices, M_NECP);
2270 }
2271
2272 return (error);
2273 }
2274
2275 static __attribute__((noinline)) int
2276 ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
2277 {
2278 struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
2279 u_int16_t flags;
2280 int error = 0;
2281
2282 VERIFY(ifp != NULL);
2283
2284 switch (cmd) {
2285 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2286 if (ifnsr->ifnsr_len > sizeof (ifnsr->ifnsr_data)) {
2287 error = EINVAL;
2288 break;
2289 }
2290 bcopy(&ifnsr->ifnsr_flags, &flags, sizeof (flags));
2291 error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
2292 ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
2293 break;
2294
2295 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2296 ifnsr->ifnsr_len = sizeof (ifnsr->ifnsr_data);
2297 error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
2298 &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
2299 if (error == 0)
2300 bcopy(&flags, &ifnsr->ifnsr_flags, sizeof (flags));
2301 else
2302 ifnsr->ifnsr_len = 0;
2303 break;
2304
2305 default:
2306 VERIFY(0);
2307 /* NOTREACHED */
2308 }
2309
2310 return (error);
2311 }
2312
2313 #if INET6
2314 static __attribute__((noinline)) int
2315 ifioctl_nat64prefix(struct ifnet *ifp, u_long cmd, caddr_t data)
2316 {
2317 struct if_nat64req *ifnat64 = (struct if_nat64req *)(void *)data;
2318 int error = 0;
2319
2320 VERIFY(ifp != NULL);
2321
2322 switch (cmd) {
2323 case SIOCSIFNAT64PREFIX: /* struct if_nat64req */
2324 error = ifnet_set_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2325 break;
2326
2327 case SIOCGIFNAT64PREFIX: /* struct if_nat64req */
2328 error = ifnet_get_nat64prefix(ifp, ifnat64->ifnat64_prefixes);
2329 break;
2330
2331 default:
2332 VERIFY(0);
2333 /* NOTREACHED */
2334 }
2335
2336 return (error);
2337 }
2338 #endif
2339
2340
2341 static int
2342 ifioctl_get_protolist(struct ifnet *ifp, u_int32_t * ret_count,
2343 user_addr_t ifpl)
2344 {
2345 u_int32_t actual_count;
2346 u_int32_t count;
2347 int error = 0;
2348 u_int32_t *list = NULL;
2349
2350 /* find out how many */
2351 count = if_get_protolist(ifp, NULL, 0);
2352 if (ifpl == USER_ADDR_NULL) {
2353 goto done;
2354 }
2355
2356 /* copy out how many there's space for */
2357 if (*ret_count < count) {
2358 count = *ret_count;
2359 }
2360 if (count == 0) {
2361 goto done;
2362 }
2363 list = _MALLOC(count * sizeof(*list), M_TEMP, M_WAITOK);
2364 if (list == NULL) {
2365 error = ENOMEM;
2366 goto done;
2367 }
2368 actual_count = if_get_protolist(ifp, list, count);
2369 if (actual_count < count) {
2370 count = actual_count;
2371 }
2372 if (count != 0) {
2373 error = copyout((caddr_t)list, ifpl, count * sizeof(*list));
2374 }
2375
2376 done:
2377 if (list != NULL) {
2378 if_free_protolist(list);
2379 }
2380 *ret_count = count;
2381 return (error);
2382 }
2383
2384 static __attribute__((noinline)) int
2385 ifioctl_protolist(struct ifnet *ifp, u_long cmd, caddr_t data)
2386 {
2387 int error = 0;
2388
2389 switch (cmd) {
2390 case SIOCGIFPROTOLIST32: { /* struct if_protolistreq32 */
2391 struct if_protolistreq32 ifpl;
2392
2393 bcopy(data, &ifpl, sizeof(ifpl));
2394 if (ifpl.ifpl_reserved != 0) {
2395 error = EINVAL;
2396 break;
2397 }
2398 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2399 CAST_USER_ADDR_T(ifpl.ifpl_list));
2400 bcopy(&ifpl, data, sizeof(ifpl));
2401 break;
2402 }
2403 case SIOCGIFPROTOLIST64: { /* struct if_protolistreq64 */
2404 struct if_protolistreq64 ifpl;
2405
2406 bcopy(data, &ifpl, sizeof(ifpl));
2407 if (ifpl.ifpl_reserved != 0) {
2408 error = EINVAL;
2409 break;
2410 }
2411 error = ifioctl_get_protolist(ifp, &ifpl.ifpl_count,
2412 ifpl.ifpl_list);
2413 bcopy(&ifpl, data, sizeof(ifpl));
2414 break;
2415 }
2416 default:
2417 VERIFY(0);
2418 /* NOTREACHED */
2419 }
2420
2421 return (error);
2422 }
2423
2424 /*
2425 * List the ioctl()s we can perform on restricted INTCOPROC interfaces.
2426 */
2427 static bool
2428 ifioctl_restrict_intcoproc(unsigned long cmd, const char *ifname,
2429 struct ifnet *ifp, struct proc *p)
2430 {
2431
2432 if (intcoproc_unrestricted == TRUE) {
2433 return (false);
2434 }
2435 if (proc_pid(p) == 0) {
2436 return (false);
2437 }
2438 if (ifname) {
2439 ifp = ifunit(ifname);
2440 }
2441 if (ifp == NULL) {
2442 return (false);
2443 }
2444 if (!IFNET_IS_INTCOPROC(ifp)) {
2445 return (false);
2446 }
2447 switch (cmd) {
2448 case SIOCGIFBRDADDR:
2449 case SIOCGIFCONF32:
2450 case SIOCGIFCONF64:
2451 case SIOCGIFFLAGS:
2452 case SIOCGIFEFLAGS:
2453 case SIOCGIFCAP:
2454 case SIOCGIFMAC:
2455 case SIOCGIFMETRIC:
2456 case SIOCGIFMTU:
2457 case SIOCGIFPHYS:
2458 case SIOCGIFTYPE:
2459 case SIOCGIFFUNCTIONALTYPE:
2460 case SIOCGIFPSRCADDR:
2461 case SIOCGIFPDSTADDR:
2462 case SIOCGIFGENERIC:
2463 case SIOCGIFDEVMTU:
2464 case SIOCGIFVLAN:
2465 case SIOCGIFBOND:
2466 case SIOCGIFWAKEFLAGS:
2467 case SIOCGIFGETRTREFCNT:
2468 case SIOCGIFOPPORTUNISTIC:
2469 case SIOCGIFLINKQUALITYMETRIC:
2470 case SIOCGIFLOG:
2471 case SIOCGIFDELEGATE:
2472 case SIOCGIFEXPENSIVE:
2473 case SIOCGIFINTERFACESTATE:
2474 case SIOCGIFPROBECONNECTIVITY:
2475 case SIOCGIFTIMESTAMPENABLED:
2476 case SIOCGECNMODE:
2477 case SIOCGQOSMARKINGMODE:
2478 case SIOCGQOSMARKINGENABLED:
2479 case SIOCGIFLOWINTERNET:
2480 case SIOCGIFSTATUS:
2481 case SIOCGIFMEDIA32:
2482 case SIOCGIFMEDIA64:
2483 case SIOCGIFDESC:
2484 case SIOCGIFLINKPARAMS:
2485 case SIOCGIFQUEUESTATS:
2486 case SIOCGIFTHROTTLE:
2487 case SIOCGIFAGENTIDS32:
2488 case SIOCGIFAGENTIDS64:
2489 case SIOCGIFNETSIGNATURE:
2490 case SIOCGIFINFO_IN6:
2491 case SIOCGIFAFLAG_IN6:
2492 case SIOCGNBRINFO_IN6:
2493 case SIOCGIFALIFETIME_IN6:
2494 case SIOCGIFNETMASK_IN6:
2495 case SIOCGIFPROTOLIST32:
2496 case SIOCGIFPROTOLIST64:
2497 return (false);
2498 default:
2499 #if (DEBUG || DEVELOPMENT)
2500 printf("%s: cmd 0x%lx not allowed (pid %u)\n",
2501 __func__, cmd, proc_pid(p));
2502 #endif
2503 return (true);
2504 }
2505 return (false);
2506 }
2507
2508 /*
2509 * Interface ioctls.
2510 *
2511 * Most of the routines called to handle the ioctls would end up being
2512 * tail-call optimized, which unfortunately causes this routine to
2513 * consume too much stack space; this is the reason for the "noinline"
2514 * attribute used on those routines.
2515 */
2516 int
2517 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
2518 {
2519 char ifname[IFNAMSIZ + 1];
2520 struct ifnet *ifp = NULL;
2521 struct ifstat *ifs = NULL;
2522 int error = 0;
2523
2524 bzero(ifname, sizeof (ifname));
2525
2526 /*
2527 * ioctls which don't require ifp, or ifreq ioctls
2528 */
2529 switch (cmd) {
2530 case OSIOCGIFCONF32: /* struct ifconf32 */
2531 case SIOCGIFCONF32: /* struct ifconf32 */
2532 case SIOCGIFCONF64: /* struct ifconf64 */
2533 case OSIOCGIFCONF64: /* struct ifconf64 */
2534 error = ifioctl_ifconf(cmd, data);
2535 goto done;
2536
2537 case SIOCIFGCLONERS32: /* struct if_clonereq32 */
2538 case SIOCIFGCLONERS64: /* struct if_clonereq64 */
2539 error = ifioctl_ifclone(cmd, data);
2540 goto done;
2541
2542 case SIOCGIFAGENTDATA32: /* struct netagent_req32 */
2543 case SIOCGIFAGENTDATA64: /* struct netagent_req64 */
2544 case SIOCGIFAGENTLIST32: /* struct netagentlist_req32 */
2545 case SIOCGIFAGENTLIST64: /* struct netagentlist_req64 */
2546 error = netagent_ioctl(cmd, data);
2547 goto done;
2548
2549 case SIOCSIFORDER: /* struct if_order */
2550 case SIOCGIFORDER: /* struct if_order */
2551 error = ifioctl_iforder(cmd, data);
2552 goto done;
2553
2554 case SIOCSIFDSTADDR: /* struct ifreq */
2555 case SIOCSIFADDR: /* struct ifreq */
2556 case SIOCSIFBRDADDR: /* struct ifreq */
2557 case SIOCSIFNETMASK: /* struct ifreq */
2558 case OSIOCGIFADDR: /* struct ifreq */
2559 case OSIOCGIFDSTADDR: /* struct ifreq */
2560 case OSIOCGIFBRDADDR: /* struct ifreq */
2561 case OSIOCGIFNETMASK: /* struct ifreq */
2562 case SIOCSIFKPI: /* struct ifreq */
2563 if (so->so_proto == NULL) {
2564 error = EOPNOTSUPP;
2565 goto done;
2566 }
2567 /* FALLTHRU */
2568 case SIOCIFCREATE: /* struct ifreq */
2569 case SIOCIFCREATE2: /* struct ifreq */
2570 case SIOCIFDESTROY: /* struct ifreq */
2571 case SIOCGIFFLAGS: /* struct ifreq */
2572 case SIOCGIFEFLAGS: /* struct ifreq */
2573 case SIOCGIFCAP: /* struct ifreq */
2574 #if CONFIG_MACF_NET
2575 case SIOCGIFMAC: /* struct ifreq */
2576 case SIOCSIFMAC: /* struct ifreq */
2577 #endif /* CONFIG_MACF_NET */
2578 case SIOCGIFMETRIC: /* struct ifreq */
2579 case SIOCGIFMTU: /* struct ifreq */
2580 case SIOCGIFPHYS: /* struct ifreq */
2581 case SIOCSIFFLAGS: /* struct ifreq */
2582 case SIOCSIFCAP: /* struct ifreq */
2583 case SIOCSIFMETRIC: /* struct ifreq */
2584 case SIOCSIFPHYS: /* struct ifreq */
2585 case SIOCSIFMTU: /* struct ifreq */
2586 case SIOCADDMULTI: /* struct ifreq */
2587 case SIOCDELMULTI: /* struct ifreq */
2588 case SIOCDIFPHYADDR: /* struct ifreq */
2589 case SIOCSIFMEDIA: /* struct ifreq */
2590 case SIOCSIFGENERIC: /* struct ifreq */
2591 case SIOCSIFLLADDR: /* struct ifreq */
2592 case SIOCSIFALTMTU: /* struct ifreq */
2593 case SIOCSIFVLAN: /* struct ifreq */
2594 case SIOCSIFBOND: /* struct ifreq */
2595 case SIOCGIFLLADDR: /* struct ifreq */
2596 case SIOCGIFTYPE: /* struct ifreq */
2597 case SIOCGIFFUNCTIONALTYPE: /* struct ifreq */
2598 case SIOCGIFPSRCADDR: /* struct ifreq */
2599 case SIOCGIFPDSTADDR: /* struct ifreq */
2600 case SIOCGIFGENERIC: /* struct ifreq */
2601 case SIOCGIFDEVMTU: /* struct ifreq */
2602 case SIOCGIFVLAN: /* struct ifreq */
2603 case SIOCGIFBOND: /* struct ifreq */
2604 case SIOCGIFWAKEFLAGS: /* struct ifreq */
2605 case SIOCGIFGETRTREFCNT: /* struct ifreq */
2606 case SIOCSIFOPPORTUNISTIC: /* struct ifreq */
2607 case SIOCGIFOPPORTUNISTIC: /* struct ifreq */
2608 case SIOCGIFLINKQUALITYMETRIC: /* struct ifreq */
2609 case SIOCSIFLOG: /* struct ifreq */
2610 case SIOCGIFLOG: /* struct ifreq */
2611 case SIOCGIFDELEGATE: /* struct ifreq */
2612 case SIOCGIFEXPENSIVE: /* struct ifreq */
2613 case SIOCSIFEXPENSIVE: /* struct ifreq */
2614 case SIOCSIF2KCL: /* struct ifreq */
2615 case SIOCGIF2KCL: /* struct ifreq */
2616 case SIOCSIFINTERFACESTATE: /* struct ifreq */
2617 case SIOCGIFINTERFACESTATE: /* struct ifreq */
2618 case SIOCSIFPROBECONNECTIVITY: /* struct ifreq */
2619 case SIOCGIFPROBECONNECTIVITY: /* struct ifreq */
2620 case SIOCGSTARTDELAY: /* struct ifreq */
2621 case SIOCSIFTIMESTAMPENABLE: /* struct ifreq */
2622 case SIOCSIFTIMESTAMPDISABLE: /* struct ifreq */
2623 case SIOCGIFTIMESTAMPENABLED: /* struct ifreq */
2624 #if (DEBUG || DEVELOPMENT)
2625 case SIOCSIFDISABLEOUTPUT: /* struct ifreq */
2626 #endif /* (DEBUG || DEVELOPMENT) */
2627 case SIOCGECNMODE: /* struct ifreq */
2628 case SIOCSECNMODE:
2629 case SIOCSQOSMARKINGMODE: /* struct ifreq */
2630 case SIOCSQOSMARKINGENABLED: /* struct ifreq */
2631 case SIOCGQOSMARKINGMODE: /* struct ifreq */
2632 case SIOCGQOSMARKINGENABLED: /* struct ifreq */
2633 case SIOCSIFLOWINTERNET: /* struct ifreq */
2634 case SIOCGIFLOWINTERNET: /* struct ifreq */
2635 { /* struct ifreq */
2636 struct ifreq ifr;
2637 bcopy(data, &ifr, sizeof (ifr));
2638 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2639 bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
2640 if (ifioctl_restrict_intcoproc(cmd, ifname, NULL, p) == true) {
2641 error = EPERM;
2642 goto done;
2643 }
2644 error = ifioctl_ifreq(so, cmd, &ifr, p);
2645 bcopy(&ifr, data, sizeof (ifr));
2646 goto done;
2647 }
2648 }
2649
2650 /*
2651 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2652 * here to ensure that the ifnet, if found, has been fully attached.
2653 */
2654 dlil_if_lock();
2655 switch (cmd) {
2656 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2657 bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
2658 ifname, IFNAMSIZ);
2659 ifp = ifunit(ifname);
2660 break;
2661
2662 #if INET6
2663 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
2664 bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
2665 ifname, IFNAMSIZ);
2666 ifp = ifunit(ifname);
2667 break;
2668
2669 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
2670 bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
2671 ifname, IFNAMSIZ);
2672 ifp = ifunit(ifname);
2673 break;
2674 #endif /* INET6 */
2675
2676 case SIOCGIFSTATUS: /* struct ifstat */
2677 ifs = _MALLOC(sizeof (*ifs), M_DEVBUF, M_WAITOK);
2678 if (ifs == NULL) {
2679 error = ENOMEM;
2680 dlil_if_unlock();
2681 goto done;
2682 }
2683 bcopy(data, ifs, sizeof (*ifs));
2684 ifs->ifs_name[IFNAMSIZ - 1] = '\0';
2685 bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
2686 ifp = ifunit(ifname);
2687 break;
2688
2689 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
2690 bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
2691 ifname, IFNAMSIZ);
2692 ifp = ifunit(ifname);
2693 break;
2694
2695 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
2696 bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
2697 ifname, IFNAMSIZ);
2698 ifp = ifunit(ifname);
2699 break;
2700
2701 case SIOCSIFDESC: /* struct if_descreq */
2702 case SIOCGIFDESC: /* struct if_descreq */
2703 bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
2704 ifname, IFNAMSIZ);
2705 ifp = ifunit(ifname);
2706 break;
2707
2708 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
2709 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
2710 bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
2711 ifname, IFNAMSIZ);
2712 ifp = ifunit(ifname);
2713 break;
2714
2715 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
2716 bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
2717 ifname, IFNAMSIZ);
2718 ifp = ifunit(ifname);
2719 break;
2720
2721 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
2722 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
2723 bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
2724 ifname, IFNAMSIZ);
2725 ifp = ifunit(ifname);
2726 break;
2727
2728 case SIOCAIFAGENTID: /* struct if_agentidreq */
2729 case SIOCDIFAGENTID: /* struct if_agentidreq */
2730 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2731 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2732 bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
2733 ifname, IFNAMSIZ);
2734 ifp = ifunit(ifname);
2735 break;
2736
2737 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2738 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2739 bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
2740 ifname, IFNAMSIZ);
2741 ifp = ifunit(ifname);
2742 break;
2743
2744 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
2745 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
2746 bcopy(((struct if_protolistreq *)(void *)data)->ifpl_name,
2747 ifname, IFNAMSIZ);
2748 ifp = ifunit(ifname);
2749 break;
2750 default:
2751 /*
2752 * This is a bad assumption, but the code seems to
2753 * have been doing this in the past; caveat emptor.
2754 */
2755 bcopy(((struct ifreq *)(void *)data)->ifr_name,
2756 ifname, IFNAMSIZ);
2757 ifp = ifunit(ifname);
2758 break;
2759 }
2760 dlil_if_unlock();
2761
2762 if (ifp == NULL) {
2763 error = ENXIO;
2764 goto done;
2765 }
2766
2767 if (ifioctl_restrict_intcoproc(cmd, NULL, ifp, p) == true) {
2768 error = EPERM;
2769 goto done;
2770 }
2771 switch (cmd) {
2772 case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
2773 #if INET6
2774 case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
2775 case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
2776 #endif /* INET6 */
2777 error = proc_suser(p);
2778 if (error != 0)
2779 break;
2780
2781 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2782 if (error != 0)
2783 break;
2784
2785 ifnet_touch_lastchange(ifp);
2786 break;
2787
2788 case SIOCGIFSTATUS: /* struct ifstat */
2789 VERIFY(ifs != NULL);
2790 ifs->ascii[0] = '\0';
2791
2792 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
2793
2794 bcopy(ifs, data, sizeof (*ifs));
2795 break;
2796
2797 case SIOCGIFMEDIA32: /* struct ifmediareq32 */
2798 case SIOCGIFMEDIA64: /* struct ifmediareq64 */
2799 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2800 break;
2801
2802 case SIOCSIFDESC: /* struct if_descreq */
2803 case SIOCGIFDESC: /* struct if_descreq */
2804 error = ifioctl_ifdesc(ifp, cmd, data, p);
2805 break;
2806
2807 case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
2808 case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
2809 error = ifioctl_linkparams(ifp, cmd, data, p);
2810 break;
2811
2812 case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
2813 error = ifioctl_qstats(ifp, cmd, data);
2814 break;
2815
2816 case SIOCSIFTHROTTLE: /* struct if_throttlereq */
2817 case SIOCGIFTHROTTLE: /* struct if_throttlereq */
2818 error = ifioctl_throttle(ifp, cmd, data, p);
2819 break;
2820
2821 case SIOCAIFAGENTID: /* struct if_agentidreq */
2822 case SIOCDIFAGENTID: /* struct if_agentidreq */
2823 case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
2824 case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
2825 error = ifioctl_netagent(ifp, cmd, data, p);
2826 break;
2827
2828 case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
2829 case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
2830 error = ifioctl_netsignature(ifp, cmd, data);
2831 break;
2832
2833 #if INET6
2834 case SIOCSIFNAT64PREFIX: /* struct if_nsreq */
2835 case SIOCGIFNAT64PREFIX: /* struct if_nsreq */
2836 error = ifioctl_nat64prefix(ifp, cmd, data);
2837 break;
2838 #endif
2839
2840 case SIOCGIFPROTOLIST32: /* struct if_protolistreq32 */
2841 case SIOCGIFPROTOLIST64: /* struct if_protolistreq64 */
2842 error = ifioctl_protolist(ifp, cmd, data);
2843 break;
2844
2845 default:
2846 if (so->so_proto == NULL) {
2847 error = EOPNOTSUPP;
2848 break;
2849 }
2850
2851 socket_lock(so, 1);
2852 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2853 data, ifp, p));
2854 socket_unlock(so, 1);
2855
2856 if (error == EOPNOTSUPP || error == ENOTSUP) {
2857 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
2858 }
2859 break;
2860 }
2861
2862 done:
2863 if (ifs != NULL)
2864 _FREE(ifs, M_DEVBUF);
2865
2866 if (if_verbose) {
2867 if (ifname[0] == '\0')
2868 (void) snprintf(ifname, sizeof (ifname), "%s",
2869 "NULL");
2870 else if (ifp != NULL)
2871 (void) snprintf(ifname, sizeof (ifname), "%s",
2872 if_name(ifp));
2873
2874 if (error != 0) {
2875 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2876 "%c %lu) error %d\n", __func__,
2877 proc_name_address(p), proc_pid(p),
2878 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2879 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2880 (char)IOCGROUP(cmd), cmd & 0xff, error);
2881 } else if (if_verbose > 1) {
2882 printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
2883 "%c %lu) OK\n", __func__,
2884 proc_name_address(p), proc_pid(p),
2885 ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
2886 (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
2887 (char)IOCGROUP(cmd), cmd & 0xff);
2888 }
2889 }
2890
2891 return (error);
2892 }
2893
2894 static __attribute__((noinline)) int
2895 ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
2896 {
2897 struct ifnet *ifp;
2898 u_long ocmd = cmd;
2899 int error = 0;
2900 struct kev_msg ev_msg;
2901 struct net_event_data ev_data;
2902
2903 bzero(&ev_data, sizeof (struct net_event_data));
2904 bzero(&ev_msg, sizeof (struct kev_msg));
2905
2906 switch (cmd) {
2907 case SIOCIFCREATE:
2908 case SIOCIFCREATE2:
2909 error = proc_suser(p);
2910 if (error)
2911 return (error);
2912 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2913 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2914 case SIOCIFDESTROY:
2915 error = proc_suser(p);
2916 if (error)
2917 return (error);
2918 return (if_clone_destroy(ifr->ifr_name));
2919 }
2920
2921 /*
2922 * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
2923 * here to ensure that the ifnet, if found, has been fully attached.
2924 */
2925 dlil_if_lock();
2926 ifp = ifunit(ifr->ifr_name);
2927 dlil_if_unlock();
2928
2929 if (ifp == NULL)
2930 return (ENXIO);
2931
2932 switch (cmd) {
2933 case SIOCGIFFLAGS:
2934 ifnet_lock_shared(ifp);
2935 ifr->ifr_flags = ifp->if_flags;
2936 ifnet_lock_done(ifp);
2937 break;
2938
2939 case SIOCGIFEFLAGS:
2940 ifnet_lock_shared(ifp);
2941 ifr->ifr_eflags = ifp->if_eflags;
2942 ifnet_lock_done(ifp);
2943 break;
2944
2945 case SIOCGIFCAP:
2946 ifnet_lock_shared(ifp);
2947 ifr->ifr_reqcap = ifp->if_capabilities;
2948 ifr->ifr_curcap = ifp->if_capenable;
2949 ifnet_lock_done(ifp);
2950 break;
2951
2952 #if CONFIG_MACF_NET
2953 case SIOCGIFMAC:
2954 error = mac_ifnet_label_get(kauth_cred_get(), ifr, ifp);
2955 break;
2956
2957 case SIOCSIFMAC:
2958 error = mac_ifnet_label_set(kauth_cred_get(), ifr, ifp);
2959 break;
2960 #endif /* CONFIG_MACF_NET */
2961
2962 case SIOCGIFMETRIC:
2963 ifnet_lock_shared(ifp);
2964 ifr->ifr_metric = ifp->if_metric;
2965 ifnet_lock_done(ifp);
2966 break;
2967
2968 case SIOCGIFMTU:
2969 ifnet_lock_shared(ifp);
2970 ifr->ifr_mtu = ifp->if_mtu;
2971 ifnet_lock_done(ifp);
2972 break;
2973
2974 case SIOCGIFPHYS:
2975 ifnet_lock_shared(ifp);
2976 ifr->ifr_phys = ifp->if_physical;
2977 ifnet_lock_done(ifp);
2978 break;
2979
2980 case SIOCSIFFLAGS:
2981 error = proc_suser(p);
2982 if (error != 0)
2983 break;
2984
2985 (void) ifnet_set_flags(ifp, ifr->ifr_flags,
2986 (u_int16_t)~IFF_CANTCHANGE);
2987
2988 /*
2989 * Note that we intentionally ignore any error from below
2990 * for the SIOCSIFFLAGS case.
2991 */
2992 (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
2993
2994 /*
2995 * Send the event even upon error from the driver because
2996 * we changed the flags.
2997 */
2998 dlil_post_sifflags_msg(ifp);
2999
3000 ifnet_touch_lastchange(ifp);
3001 break;
3002
3003 case SIOCSIFCAP:
3004 error = proc_suser(p);
3005 if (error != 0)
3006 break;
3007
3008 if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
3009 error = EINVAL;
3010 break;
3011 }
3012 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3013
3014 ifnet_touch_lastchange(ifp);
3015 break;
3016
3017 case SIOCSIFMETRIC:
3018 error = proc_suser(p);
3019 if (error != 0)
3020 break;
3021
3022 ifp->if_metric = ifr->ifr_metric;
3023
3024 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3025 ev_msg.kev_class = KEV_NETWORK_CLASS;
3026 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3027
3028 ev_msg.event_code = KEV_DL_SIFMETRICS;
3029 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3030 ev_data.if_family = ifp->if_family;
3031 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3032 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3033 ev_msg.dv[0].data_ptr = &ev_data;
3034
3035 ev_msg.dv[1].data_length = 0;
3036 dlil_post_complete_msg(ifp, &ev_msg);
3037
3038 ifnet_touch_lastchange(ifp);
3039 break;
3040
3041 case SIOCSIFPHYS:
3042 error = proc_suser(p);
3043 if (error != 0)
3044 break;
3045
3046 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3047 if (error != 0)
3048 break;
3049
3050 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3051 ev_msg.kev_class = KEV_NETWORK_CLASS;
3052 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3053
3054 ev_msg.event_code = KEV_DL_SIFPHYS;
3055 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3056 ev_data.if_family = ifp->if_family;
3057 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3058 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3059 ev_msg.dv[0].data_ptr = &ev_data;
3060 ev_msg.dv[1].data_length = 0;
3061 dlil_post_complete_msg(ifp, &ev_msg);
3062
3063 ifnet_touch_lastchange(ifp);
3064 break;
3065
3066 case SIOCSIFMTU: {
3067 u_int32_t oldmtu = ifp->if_mtu;
3068 struct ifclassq *ifq = &ifp->if_snd;
3069
3070 error = proc_suser(p);
3071 if (error != 0)
3072 break;
3073
3074 if (ifp->if_ioctl == NULL) {
3075 error = EOPNOTSUPP;
3076 break;
3077 }
3078 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
3079 error = EINVAL;
3080 break;
3081 }
3082 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3083 if (error != 0)
3084 break;
3085
3086 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3087 ev_msg.kev_class = KEV_NETWORK_CLASS;
3088 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3089
3090 ev_msg.event_code = KEV_DL_SIFMTU;
3091 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3092 ev_data.if_family = ifp->if_family;
3093 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3094 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3095 ev_msg.dv[0].data_ptr = &ev_data;
3096 ev_msg.dv[1].data_length = 0;
3097 dlil_post_complete_msg(ifp, &ev_msg);
3098
3099 ifnet_touch_lastchange(ifp);
3100 rt_ifmsg(ifp);
3101
3102 /*
3103 * If the link MTU changed, do network layer specific procedure
3104 * and update all route entries associated with the interface,
3105 * so that their MTU metric gets updated.
3106 */
3107 if (ifp->if_mtu != oldmtu) {
3108 if_rtmtu_update(ifp);
3109 #if INET6
3110 nd6_setmtu(ifp);
3111 #endif /* INET6 */
3112 /* Inform all transmit queues about the new MTU */
3113 IFCQ_LOCK(ifq);
3114 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
3115 IFCQ_UNLOCK(ifq);
3116 }
3117 break;
3118 }
3119
3120 case SIOCADDMULTI:
3121 case SIOCDELMULTI:
3122 error = proc_suser(p);
3123 if (error != 0)
3124 break;
3125
3126 /* Don't allow group membership on non-multicast interfaces. */
3127 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
3128 error = EOPNOTSUPP;
3129 break;
3130 }
3131
3132 /* Don't let users screw up protocols' entries. */
3133 if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
3134 ifr->ifr_addr.sa_family != AF_LINK) {
3135 error = EINVAL;
3136 break;
3137 }
3138
3139 /*
3140 * User is permitted to anonymously join a particular link
3141 * multicast group via SIOCADDMULTI. Subsequent join requested
3142 * for the same record which has an outstanding refcnt from a
3143 * past if_addmulti_anon() will not result in EADDRINUSE error
3144 * (unlike other BSDs.) Anonymously leaving a group is also
3145 * allowed only as long as there is an outstanding refcnt held
3146 * by a previous anonymous request, or else ENOENT (even if the
3147 * link-layer multicast membership exists for a network-layer
3148 * membership.)
3149 */
3150 if (cmd == SIOCADDMULTI) {
3151 error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
3152 ev_msg.event_code = KEV_DL_ADDMULTI;
3153 } else {
3154 error = if_delmulti_anon(ifp, &ifr->ifr_addr);
3155 ev_msg.event_code = KEV_DL_DELMULTI;
3156 }
3157 if (error != 0)
3158 break;
3159
3160 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3161 ev_msg.kev_class = KEV_NETWORK_CLASS;
3162 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
3163 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
3164
3165 ev_data.if_family = ifp->if_family;
3166 ev_data.if_unit = (u_int32_t) ifp->if_unit;
3167 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
3168 ev_msg.dv[0].data_ptr = &ev_data;
3169 ev_msg.dv[1].data_length = 0;
3170 dlil_post_complete_msg(ifp, &ev_msg);
3171
3172 ifnet_touch_lastchange(ifp);
3173 break;
3174
3175 case SIOCDIFPHYADDR:
3176 case SIOCSIFMEDIA:
3177 case SIOCSIFGENERIC:
3178 case SIOCSIFLLADDR:
3179 case SIOCSIFALTMTU:
3180 case SIOCSIFVLAN:
3181 case SIOCSIFBOND:
3182 error = proc_suser(p);
3183 if (error != 0)
3184 break;
3185
3186 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3187 if (error != 0)
3188 break;
3189
3190 ifnet_touch_lastchange(ifp);
3191 break;
3192
3193 case SIOCGIFLLADDR: {
3194 struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
3195
3196 if (sdl->sdl_alen == 0) {
3197 error = EADDRNOTAVAIL;
3198 break;
3199 }
3200 /* If larger than 14-bytes we'll need another mechanism */
3201 if (sdl->sdl_alen > sizeof (ifr->ifr_addr.sa_data)) {
3202 error = EMSGSIZE;
3203 break;
3204 }
3205 /* Follow the same convention used by SIOCSIFLLADDR */
3206 bzero(&ifr->ifr_addr, sizeof (ifr->ifr_addr));
3207 ifr->ifr_addr.sa_family = AF_LINK;
3208 ifr->ifr_addr.sa_len = sdl->sdl_alen;
3209 error = ifnet_guarded_lladdr_copy_bytes(ifp,
3210 &ifr->ifr_addr.sa_data, sdl->sdl_alen);
3211 break;
3212 }
3213
3214 case SIOCGIFTYPE:
3215 ifr->ifr_type.ift_type = ifp->if_type;
3216 ifr->ifr_type.ift_family = ifp->if_family;
3217 ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
3218 break;
3219
3220 case SIOCGIFFUNCTIONALTYPE:
3221 ifr->ifr_functional_type = if_functional_type(ifp, FALSE);
3222 break;
3223
3224 case SIOCGIFPSRCADDR:
3225 case SIOCGIFPDSTADDR:
3226 case SIOCGIFGENERIC:
3227 case SIOCGIFDEVMTU:
3228 case SIOCGIFVLAN:
3229 case SIOCGIFBOND:
3230 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
3231 break;
3232
3233 case SIOCGIFWAKEFLAGS:
3234 ifnet_lock_shared(ifp);
3235 ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
3236 ifnet_lock_done(ifp);
3237 break;
3238
3239 case SIOCGIFGETRTREFCNT:
3240 ifnet_lock_shared(ifp);
3241 ifr->ifr_route_refcnt = ifp->if_route_refcnt;
3242 ifnet_lock_done(ifp);
3243 break;
3244
3245 case SIOCSIFOPPORTUNISTIC:
3246 case SIOCGIFOPPORTUNISTIC:
3247 error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
3248 break;
3249
3250 case SIOCGIFLINKQUALITYMETRIC:
3251 ifnet_lock_shared(ifp);
3252 if ((ifp->if_interface_state.valid_bitmask &
3253 IF_INTERFACE_STATE_LQM_STATE_VALID)) {
3254 ifr->ifr_link_quality_metric =
3255 ifp->if_interface_state.lqm_state;
3256 } else if (IF_FULLY_ATTACHED(ifp)) {
3257 ifr->ifr_link_quality_metric =
3258 IFNET_LQM_THRESH_UNKNOWN;
3259 } else {
3260 ifr->ifr_link_quality_metric =
3261 IFNET_LQM_THRESH_OFF;
3262 }
3263 ifnet_lock_done(ifp);
3264 break;
3265
3266 case SIOCSIFLOG:
3267 case SIOCGIFLOG:
3268 error = ifnet_getset_log(ifp, cmd, ifr, p);
3269 break;
3270
3271 case SIOCGIFDELEGATE:
3272 ifnet_lock_shared(ifp);
3273 ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
3274 ifp->if_delegated.ifp->if_index : 0);
3275 ifnet_lock_done(ifp);
3276 break;
3277
3278 case SIOCGIFEXPENSIVE:
3279 ifnet_lock_shared(ifp);
3280 if (ifp->if_eflags & IFEF_EXPENSIVE)
3281 ifr->ifr_expensive = 1;
3282 else
3283 ifr->ifr_expensive = 0;
3284 ifnet_lock_done(ifp);
3285 break;
3286
3287 case SIOCSIFEXPENSIVE:
3288 {
3289 struct ifnet *difp;
3290
3291 if ((error = priv_check_cred(kauth_cred_get(),
3292 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3293 return (error);
3294 ifnet_lock_exclusive(ifp);
3295 if (ifr->ifr_expensive)
3296 ifp->if_eflags |= IFEF_EXPENSIVE;
3297 else
3298 ifp->if_eflags &= ~IFEF_EXPENSIVE;
3299 ifnet_lock_done(ifp);
3300 /*
3301 * Update the expensive bit in the delegated interface
3302 * structure.
3303 */
3304 ifnet_head_lock_shared();
3305 TAILQ_FOREACH(difp, &ifnet_head, if_link) {
3306 ifnet_lock_exclusive(difp);
3307 if (difp->if_delegated.ifp == ifp) {
3308 difp->if_delegated.expensive =
3309 ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
3310
3311 }
3312 ifnet_lock_done(difp);
3313 }
3314 ifnet_head_done();
3315 break;
3316 }
3317
3318 case SIOCGIF2KCL:
3319 ifnet_lock_shared(ifp);
3320 if (ifp->if_eflags & IFEF_2KCL)
3321 ifr->ifr_2kcl = 1;
3322 else
3323 ifr->ifr_2kcl = 0;
3324 ifnet_lock_done(ifp);
3325 break;
3326
3327 case SIOCSIF2KCL:
3328 if ((error = priv_check_cred(kauth_cred_get(),
3329 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3330 return (error);
3331 ifnet_lock_exclusive(ifp);
3332 if (ifr->ifr_2kcl)
3333 ifp->if_eflags |= IFEF_2KCL;
3334 else
3335 ifp->if_eflags &= ~IFEF_2KCL;
3336 ifnet_lock_done(ifp);
3337 break;
3338 case SIOCGSTARTDELAY:
3339 ifnet_lock_shared(ifp);
3340 if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
3341 ifr->ifr_start_delay_qlen =
3342 ifp->if_start_delay_qlen;
3343 ifr->ifr_start_delay_timeout =
3344 ifp->if_start_delay_timeout;
3345 } else {
3346 ifr->ifr_start_delay_qlen = 0;
3347 ifr->ifr_start_delay_timeout = 0;
3348 }
3349 ifnet_lock_done(ifp);
3350 break;
3351 case SIOCSIFDSTADDR:
3352 case SIOCSIFADDR:
3353 case SIOCSIFBRDADDR:
3354 case SIOCSIFNETMASK:
3355 case OSIOCGIFADDR:
3356 case OSIOCGIFDSTADDR:
3357 case OSIOCGIFBRDADDR:
3358 case OSIOCGIFNETMASK:
3359 case SIOCSIFKPI:
3360 VERIFY(so->so_proto != NULL);
3361
3362 if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
3363 cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
3364 #if BYTE_ORDER != BIG_ENDIAN
3365 if (ifr->ifr_addr.sa_family == 0 &&
3366 ifr->ifr_addr.sa_len < 16) {
3367 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
3368 ifr->ifr_addr.sa_len = 16;
3369 }
3370 #else
3371 if (ifr->ifr_addr.sa_len == 0)
3372 ifr->ifr_addr.sa_len = 16;
3373 #endif
3374 } else if (cmd == OSIOCGIFADDR) {
3375 cmd = SIOCGIFADDR; /* struct ifreq */
3376 } else if (cmd == OSIOCGIFDSTADDR) {
3377 cmd = SIOCGIFDSTADDR; /* struct ifreq */
3378 } else if (cmd == OSIOCGIFBRDADDR) {
3379 cmd = SIOCGIFBRDADDR; /* struct ifreq */
3380 } else if (cmd == OSIOCGIFNETMASK) {
3381 cmd = SIOCGIFNETMASK; /* struct ifreq */
3382 }
3383
3384 socket_lock(so, 1);
3385 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
3386 (caddr_t)ifr, ifp, p));
3387 socket_unlock(so, 1);
3388
3389 switch (ocmd) {
3390 case OSIOCGIFADDR:
3391 case OSIOCGIFDSTADDR:
3392 case OSIOCGIFBRDADDR:
3393 case OSIOCGIFNETMASK:
3394 bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
3395 sizeof (u_short));
3396 }
3397
3398 if (cmd == SIOCSIFKPI) {
3399 int temperr = proc_suser(p);
3400 if (temperr != 0)
3401 error = temperr;
3402 }
3403
3404 if (error == EOPNOTSUPP || error == ENOTSUP) {
3405 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3406 (caddr_t)ifr);
3407 }
3408 break;
3409
3410 case SIOCGIFINTERFACESTATE:
3411 if_get_state(ifp, &ifr->ifr_interface_state);
3412
3413 break;
3414 case SIOCSIFINTERFACESTATE:
3415 if ((error = priv_check_cred(kauth_cred_get(),
3416 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3417 return (error);
3418
3419 error = if_state_update(ifp, &ifr->ifr_interface_state);
3420
3421 break;
3422 case SIOCSIFPROBECONNECTIVITY:
3423 if ((error = priv_check_cred(kauth_cred_get(),
3424 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3425 return (error);
3426 error = if_probe_connectivity(ifp,
3427 ifr->ifr_probe_connectivity);
3428 break;
3429 case SIOCGIFPROBECONNECTIVITY:
3430 if ((error = priv_check_cred(kauth_cred_get(),
3431 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3432 return (error);
3433 if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY)
3434 ifr->ifr_probe_connectivity = 1;
3435 else
3436 ifr->ifr_probe_connectivity = 0;
3437 break;
3438 case SIOCGECNMODE:
3439 if ((ifp->if_eflags & (IFEF_ECN_ENABLE|IFEF_ECN_DISABLE)) ==
3440 IFEF_ECN_ENABLE)
3441 ifr->ifr_ecn_mode = IFRTYPE_ECN_ENABLE;
3442 else if ((ifp->if_eflags & (IFEF_ECN_ENABLE|IFEF_ECN_DISABLE)) ==
3443 IFEF_ECN_DISABLE)
3444 ifr->ifr_ecn_mode = IFRTYPE_ECN_DISABLE;
3445 else
3446 ifr->ifr_ecn_mode = IFRTYPE_ECN_DEFAULT;
3447 break;
3448 case SIOCSECNMODE:
3449 if ((error = priv_check_cred(kauth_cred_get(),
3450 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3451 return (error);
3452 if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DEFAULT) {
3453 ifp->if_eflags &= ~(IFEF_ECN_ENABLE|IFEF_ECN_DISABLE);
3454 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_ENABLE) {
3455 ifp->if_eflags |= IFEF_ECN_ENABLE;
3456 ifp->if_eflags &= ~IFEF_ECN_DISABLE;
3457 } else if (ifr->ifr_ecn_mode == IFRTYPE_ECN_DISABLE) {
3458 ifp->if_eflags |= IFEF_ECN_DISABLE;
3459 ifp->if_eflags &= ~IFEF_ECN_ENABLE;
3460 } else
3461 error = EINVAL;
3462 break;
3463 case SIOCSIFTIMESTAMPENABLE:
3464 case SIOCSIFTIMESTAMPDISABLE:
3465 error = proc_suser(p);
3466 if (error != 0)
3467 break;
3468
3469 ifnet_lock_exclusive(ifp);
3470 if ((cmd == SIOCSIFTIMESTAMPENABLE &&
3471 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0) ||
3472 (cmd == SIOCSIFTIMESTAMPDISABLE &&
3473 (ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) == 0)) {
3474 ifnet_lock_done(ifp);
3475 break;
3476 }
3477 if (cmd == SIOCSIFTIMESTAMPENABLE)
3478 ifp->if_xflags |= IFXF_TIMESTAMP_ENABLED;
3479 else
3480 ifp->if_xflags &= ~IFXF_TIMESTAMP_ENABLED;
3481 ifnet_lock_done(ifp);
3482 /*
3483 * Pass the setting to the interface if it supports either
3484 * software or hardware time stamping
3485 */
3486 if (ifp->if_capabilities & (IFCAP_HW_TIMESTAMP |
3487 IFCAP_SW_TIMESTAMP)) {
3488 error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
3489 (caddr_t)ifr);
3490 }
3491 break;
3492 case SIOCGIFTIMESTAMPENABLED: {
3493 if ((ifp->if_xflags & IFXF_TIMESTAMP_ENABLED) != 0)
3494 ifr->ifr_intval = 1;
3495 else
3496 ifr->ifr_intval = 0;
3497 break;
3498 }
3499 case SIOCSQOSMARKINGMODE:
3500 if ((error = priv_check_cred(kauth_cred_get(),
3501 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3502 return (error);
3503 error = if_set_qosmarking_mode(ifp, ifr->ifr_qosmarking_mode);
3504 break;
3505
3506 case SIOCGQOSMARKINGMODE:
3507 ifr->ifr_qosmarking_mode = ifp->if_qosmarking_mode;
3508 break;
3509
3510 case SIOCSQOSMARKINGENABLED:
3511 if ((error = priv_check_cred(kauth_cred_get(),
3512 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3513 return (error);
3514 if (ifr->ifr_qosmarking_enabled != 0)
3515 ifp->if_eflags |= IFEF_QOSMARKING_ENABLED;
3516 else
3517 ifp->if_eflags &= ~IFEF_QOSMARKING_ENABLED;
3518 break;
3519
3520 case SIOCGQOSMARKINGENABLED:
3521 ifr->ifr_qosmarking_enabled =
3522 (ifp->if_eflags & IFEF_QOSMARKING_ENABLED) ? 1 : 0;
3523 break;
3524
3525 case SIOCSIFDISABLEOUTPUT:
3526 #if (DEBUG || DEVELOPMENT)
3527 if (ifr->ifr_disable_output == 1) {
3528 error = ifnet_disable_output(ifp);
3529 } else if (ifr->ifr_disable_output == 0) {
3530 error = ifnet_enable_output(ifp);
3531 } else {
3532 error = EINVAL;
3533 }
3534 #else
3535 error = EINVAL;
3536 #endif /* (DEBUG || DEVELOPMENT) */
3537 break;
3538 case SIOCSIFLOWINTERNET:
3539 if ((error = priv_check_cred(kauth_cred_get(),
3540 PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
3541 return (error);
3542
3543 ifnet_lock_exclusive(ifp);
3544 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_UL)
3545 ifp->if_xflags |= IFXF_LOW_INTERNET_UL;
3546 else
3547 ifp->if_xflags &= ~(IFXF_LOW_INTERNET_UL);
3548 if (ifr->ifr_low_internet & IFRTYPE_LOW_INTERNET_ENABLE_DL)
3549 ifp->if_xflags |= IFXF_LOW_INTERNET_DL;
3550 else
3551 ifp->if_xflags &= ~(IFXF_LOW_INTERNET_DL);
3552 ifnet_lock_done(ifp);
3553 break;
3554 case SIOCGIFLOWINTERNET:
3555 ifnet_lock_shared(ifp);
3556 ifr->ifr_low_internet = 0;
3557 if (ifp->if_xflags & IFXF_LOW_INTERNET_UL)
3558 ifr->ifr_low_internet |=
3559 IFRTYPE_LOW_INTERNET_ENABLE_UL;
3560 if (ifp->if_xflags & IFXF_LOW_INTERNET_DL)
3561 ifr->ifr_low_internet |=
3562 IFRTYPE_LOW_INTERNET_ENABLE_DL;
3563 ifnet_lock_done(ifp);
3564 break;
3565 default:
3566 VERIFY(0);
3567 /* NOTREACHED */
3568 }
3569
3570 return (error);
3571 }
3572
3573 int
3574 ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
3575 {
3576 int error;
3577
3578 socket_unlock(so, 0);
3579 error = ifioctl(so, cmd, data, p);
3580 socket_lock(so, 0);
3581 return (error);
3582 }
3583
3584 /*
3585 * Set/clear promiscuous mode on interface ifp based on the truth value
3586 * of pswitch. The calls are reference counted so that only the first
3587 * "on" request actually has an effect, as does the final "off" request.
3588 * Results are undefined if the "off" and "on" requests are not matched.
3589 */
3590 errno_t
3591 ifnet_set_promiscuous(
3592 ifnet_t ifp,
3593 int pswitch)
3594 {
3595 int error = 0;
3596 int oldflags = 0;
3597 int newflags = 0;
3598
3599 ifnet_lock_exclusive(ifp);
3600 oldflags = ifp->if_flags;
3601 ifp->if_pcount += pswitch ? 1 : -1;
3602
3603 if (ifp->if_pcount > 0)
3604 ifp->if_flags |= IFF_PROMISC;
3605 else
3606 ifp->if_flags &= ~IFF_PROMISC;
3607
3608 newflags = ifp->if_flags;
3609 ifnet_lock_done(ifp);
3610
3611 if (newflags != oldflags && (newflags & IFF_UP) != 0) {
3612 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
3613 if (error == 0) {
3614 rt_ifmsg(ifp);
3615 } else {
3616 ifnet_lock_exclusive(ifp);
3617 // revert the flags
3618 ifp->if_pcount -= pswitch ? 1 : -1;
3619 if (ifp->if_pcount > 0)
3620 ifp->if_flags |= IFF_PROMISC;
3621 else
3622 ifp->if_flags &= ~IFF_PROMISC;
3623 ifnet_lock_done(ifp);
3624 }
3625 }
3626
3627 if (newflags != oldflags) {
3628 log(LOG_INFO, "%s: promiscuous mode %s%s\n",
3629 if_name(ifp),
3630 (newflags & IFF_PROMISC) != 0 ? "enable" : "disable",
3631 error != 0 ? " failed" : " succeeded");
3632 }
3633 return (error);
3634 }
3635
3636 /*
3637 * Return interface configuration
3638 * of system. List may be used
3639 * in later ioctl's (above) to get
3640 * other information.
3641 */
3642 /*ARGSUSED*/
3643 static int
3644 ifconf(u_long cmd, user_addr_t ifrp, int *ret_space)
3645 {
3646 struct ifnet *ifp = NULL;
3647 struct ifaddr *ifa;
3648 struct ifreq ifr;
3649 int error = 0;
3650 size_t space;
3651 net_thread_marks_t marks;
3652
3653 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
3654
3655 /*
3656 * Zero the ifr buffer to make sure we don't
3657 * disclose the contents of the stack.
3658 */
3659 bzero(&ifr, sizeof (struct ifreq));
3660
3661 space = *ret_space;
3662 ifnet_head_lock_shared();
3663 for (ifp = ifnet_head.tqh_first; space > sizeof (ifr) &&
3664 ifp; ifp = ifp->if_link.tqe_next) {
3665 char workbuf[64];
3666 size_t ifnlen, addrs;
3667
3668 ifnlen = snprintf(workbuf, sizeof (workbuf),
3669 "%s", if_name(ifp));
3670 if (ifnlen + 1 > sizeof (ifr.ifr_name)) {
3671 error = ENAMETOOLONG;
3672 break;
3673 } else {
3674 strlcpy(ifr.ifr_name, workbuf, IFNAMSIZ);
3675 }
3676
3677 ifnet_lock_shared(ifp);
3678
3679 addrs = 0;
3680 ifa = ifp->if_addrhead.tqh_first;
3681 for (; space > sizeof (ifr) && ifa;
3682 ifa = ifa->ifa_link.tqe_next) {
3683 struct sockaddr *sa;
3684 union {
3685 struct sockaddr sa;
3686 struct sockaddr_dl sdl;
3687 uint8_t buf[SOCK_MAXADDRLEN + 1];
3688 } u;
3689
3690 /*
3691 * Make sure to accomodate the largest possible
3692 * size of SA(if_lladdr)->sa_len.
3693 */
3694 _CASSERT(sizeof (u) == (SOCK_MAXADDRLEN + 1));
3695
3696 IFA_LOCK(ifa);
3697 sa = ifa->ifa_addr;
3698 addrs++;
3699
3700 if (ifa == ifp->if_lladdr) {
3701 VERIFY(sa->sa_family == AF_LINK);
3702 bcopy(sa, &u, sa->sa_len);
3703 IFA_UNLOCK(ifa);
3704 ifnet_guarded_lladdr_copy_bytes(ifp,
3705 LLADDR(&u.sdl), u.sdl.sdl_alen);
3706 IFA_LOCK(ifa);
3707 sa = &u.sa;
3708 }
3709
3710 if (cmd == OSIOCGIFCONF32 || cmd == OSIOCGIFCONF64) {
3711 struct osockaddr *osa =
3712 (struct osockaddr *)(void *)&ifr.ifr_addr;
3713 ifr.ifr_addr = *sa;
3714 osa->sa_family = sa->sa_family;
3715 error = copyout((caddr_t)&ifr, ifrp,
3716 sizeof (ifr));
3717 ifrp += sizeof (struct ifreq);
3718 } else if (sa->sa_len <= sizeof (*sa)) {
3719 ifr.ifr_addr = *sa;
3720 error = copyout((caddr_t)&ifr, ifrp,
3721 sizeof (ifr));
3722 ifrp += sizeof (struct ifreq);
3723 } else {
3724 if (space <
3725 sizeof (ifr) + sa->sa_len - sizeof (*sa)) {
3726 IFA_UNLOCK(ifa);
3727 break;
3728 }
3729 space -= sa->sa_len - sizeof (*sa);
3730 error = copyout((caddr_t)&ifr, ifrp,
3731 sizeof (ifr.ifr_name));
3732 if (error == 0) {
3733 error = copyout((caddr_t)sa, (ifrp +
3734 offsetof(struct ifreq, ifr_addr)),
3735 sa->sa_len);
3736 }
3737 ifrp += (sa->sa_len + offsetof(struct ifreq,
3738 ifr_addr));
3739 }
3740 IFA_UNLOCK(ifa);
3741 if (error)
3742 break;
3743 space -= sizeof (ifr);
3744 }
3745 ifnet_lock_done(ifp);
3746
3747 if (error)
3748 break;
3749 if (!addrs) {
3750 bzero((caddr_t)&ifr.ifr_addr, sizeof (ifr.ifr_addr));
3751 error = copyout((caddr_t)&ifr, ifrp, sizeof (ifr));
3752 if (error)
3753 break;
3754 space -= sizeof (ifr);
3755 ifrp += sizeof (struct ifreq);
3756 }
3757 }
3758 ifnet_head_done();
3759 *ret_space -= space;
3760 net_thread_marks_pop(marks);
3761 return (error);
3762 }
3763
3764 /*
3765 * Just like if_promisc(), but for all-multicast-reception mode.
3766 */
3767 int
3768 if_allmulti(struct ifnet *ifp, int onswitch)
3769 {
3770 int error = 0;
3771 int modified = 0;
3772
3773 ifnet_lock_exclusive(ifp);
3774
3775 if (onswitch) {
3776 if (ifp->if_amcount++ == 0) {
3777 ifp->if_flags |= IFF_ALLMULTI;
3778 modified = 1;
3779 }
3780 } else {
3781 if (ifp->if_amcount > 1) {
3782 ifp->if_amcount--;
3783 } else {
3784 ifp->if_amcount = 0;
3785 ifp->if_flags &= ~IFF_ALLMULTI;
3786 modified = 1;
3787 }
3788 }
3789 ifnet_lock_done(ifp);
3790
3791 if (modified)
3792 error = ifnet_ioctl(ifp, 0, SIOCSIFFLAGS, NULL);
3793
3794 if (error == 0)
3795 rt_ifmsg(ifp);
3796 return (error);
3797 }
3798
3799 static struct ifmultiaddr *
3800 ifma_alloc(int how)
3801 {
3802 struct ifmultiaddr *ifma;
3803
3804 ifma = (how == M_WAITOK) ? zalloc(ifma_zone) :
3805 zalloc_noblock(ifma_zone);
3806
3807 if (ifma != NULL) {
3808 bzero(ifma, ifma_size);
3809 lck_mtx_init(&ifma->ifma_lock, ifa_mtx_grp, ifa_mtx_attr);
3810 ifma->ifma_debug |= IFD_ALLOC;
3811 if (ifma_debug != 0) {
3812 ifma->ifma_debug |= IFD_DEBUG;
3813 ifma->ifma_trace = ifma_trace;
3814 }
3815 }
3816 return (ifma);
3817 }
3818
3819 static void
3820 ifma_free(struct ifmultiaddr *ifma)
3821 {
3822 IFMA_LOCK(ifma);
3823
3824 if (ifma->ifma_protospec != NULL) {
3825 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
3826 /* NOTREACHED */
3827 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
3828 ifma->ifma_anoncnt != 0) {
3829 panic("%s: Freeing ifma=%p with outstanding anon req",
3830 __func__, ifma);
3831 /* NOTREACHED */
3832 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3833 panic("%s: ifma=%p attached to ifma_ifp=%p is being freed",
3834 __func__, ifma, ifma->ifma_ifp);
3835 /* NOTREACHED */
3836 } else if (!(ifma->ifma_debug & IFD_ALLOC)) {
3837 panic("%s: ifma %p cannot be freed", __func__, ifma);
3838 /* NOTREACHED */
3839 } else if (ifma->ifma_refcount != 0) {
3840 panic("%s: non-zero refcount ifma=%p", __func__, ifma);
3841 /* NOTREACHED */
3842 } else if (ifma->ifma_reqcnt != 0) {
3843 panic("%s: non-zero reqcnt ifma=%p", __func__, ifma);
3844 /* NOTREACHED */
3845 } else if (ifma->ifma_ifp != NULL) {
3846 panic("%s: non-NULL ifma_ifp=%p for ifma=%p", __func__,
3847 ifma->ifma_ifp, ifma);
3848 /* NOTREACHED */
3849 } else if (ifma->ifma_ll != NULL) {
3850 panic("%s: non-NULL ifma_ll=%p for ifma=%p", __func__,
3851 ifma->ifma_ll, ifma);
3852 /* NOTREACHED */
3853 }
3854 ifma->ifma_debug &= ~IFD_ALLOC;
3855 if ((ifma->ifma_debug & (IFD_DEBUG | IFD_TRASHED)) ==
3856 (IFD_DEBUG | IFD_TRASHED)) {
3857 lck_mtx_lock(&ifma_trash_lock);
3858 TAILQ_REMOVE(&ifma_trash_head, (struct ifmultiaddr_dbg *)ifma,
3859 ifma_trash_link);
3860 lck_mtx_unlock(&ifma_trash_lock);
3861 ifma->ifma_debug &= ~IFD_TRASHED;
3862 }
3863 IFMA_UNLOCK(ifma);
3864
3865 if (ifma->ifma_addr != NULL) {
3866 FREE(ifma->ifma_addr, M_IFADDR);
3867 ifma->ifma_addr = NULL;
3868 }
3869 lck_mtx_destroy(&ifma->ifma_lock, ifa_mtx_grp);
3870 zfree(ifma_zone, ifma);
3871 }
3872
3873 static void
3874 ifma_trace(struct ifmultiaddr *ifma, int refhold)
3875 {
3876 struct ifmultiaddr_dbg *ifma_dbg = (struct ifmultiaddr_dbg *)ifma;
3877 ctrace_t *tr;
3878 u_int32_t idx;
3879 u_int16_t *cnt;
3880
3881 if (!(ifma->ifma_debug & IFD_DEBUG)) {
3882 panic("%s: ifma %p has no debug structure", __func__, ifma);
3883 /* NOTREACHED */
3884 }
3885 if (refhold) {
3886 cnt = &ifma_dbg->ifma_refhold_cnt;
3887 tr = ifma_dbg->ifma_refhold;
3888 } else {
3889 cnt = &ifma_dbg->ifma_refrele_cnt;
3890 tr = ifma_dbg->ifma_refrele;
3891 }
3892
3893 idx = atomic_add_16_ov(cnt, 1) % IFMA_TRACE_HIST_SIZE;
3894 ctrace_record(&tr[idx]);
3895 }
3896
3897 void
3898 ifma_addref(struct ifmultiaddr *ifma, int locked)
3899 {
3900 if (!locked)
3901 IFMA_LOCK(ifma);
3902 else
3903 IFMA_LOCK_ASSERT_HELD(ifma);
3904
3905 if (++ifma->ifma_refcount == 0) {
3906 panic("%s: ifma=%p wraparound refcnt", __func__, ifma);
3907 /* NOTREACHED */
3908 } else if (ifma->ifma_trace != NULL) {
3909 (*ifma->ifma_trace)(ifma, TRUE);
3910 }
3911 if (!locked)
3912 IFMA_UNLOCK(ifma);
3913 }
3914
3915 void
3916 ifma_remref(struct ifmultiaddr *ifma)
3917 {
3918 struct ifmultiaddr *ll;
3919
3920 IFMA_LOCK(ifma);
3921
3922 if (ifma->ifma_refcount == 0) {
3923 panic("%s: ifma=%p negative refcnt", __func__, ifma);
3924 /* NOTREACHED */
3925 } else if (ifma->ifma_trace != NULL) {
3926 (*ifma->ifma_trace)(ifma, FALSE);
3927 }
3928
3929 --ifma->ifma_refcount;
3930 if (ifma->ifma_refcount > 0) {
3931 IFMA_UNLOCK(ifma);
3932 return;
3933 }
3934
3935 ll = ifma->ifma_ll;
3936 ifma->ifma_ifp = NULL;
3937 ifma->ifma_ll = NULL;
3938 IFMA_UNLOCK(ifma);
3939 ifma_free(ifma); /* deallocate it */
3940
3941 if (ll != NULL)
3942 IFMA_REMREF(ll);
3943 }
3944
3945 static void
3946 if_attach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3947 {
3948 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3949 IFMA_LOCK_ASSERT_HELD(ifma);
3950
3951 if (ifma->ifma_ifp != ifp) {
3952 panic("%s: Mismatch ifma_ifp=%p != ifp=%p", __func__,
3953 ifma->ifma_ifp, ifp);
3954 /* NOTREACHED */
3955 } else if (ifma->ifma_debug & IFD_ATTACHED) {
3956 panic("%s: Attempt to attach an already attached ifma=%p",
3957 __func__, ifma);
3958 /* NOTREACHED */
3959 } else if (anon && (ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3960 panic("%s: ifma=%p unexpected IFMAF_ANONYMOUS", __func__, ifma);
3961 /* NOTREACHED */
3962 } else if (ifma->ifma_debug & IFD_TRASHED) {
3963 panic("%s: Attempt to reattach a detached ifma=%p",
3964 __func__, ifma);
3965 /* NOTREACHED */
3966 }
3967
3968 ifma->ifma_reqcnt++;
3969 VERIFY(ifma->ifma_reqcnt == 1);
3970 IFMA_ADDREF_LOCKED(ifma);
3971 ifma->ifma_debug |= IFD_ATTACHED;
3972 if (anon) {
3973 ifma->ifma_anoncnt++;
3974 VERIFY(ifma->ifma_anoncnt == 1);
3975 ifma->ifma_flags |= IFMAF_ANONYMOUS;
3976 }
3977
3978 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3979 }
3980
3981 static int
3982 if_detach_ifma(struct ifnet *ifp, struct ifmultiaddr *ifma, int anon)
3983 {
3984 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
3985 IFMA_LOCK_ASSERT_HELD(ifma);
3986
3987 if (ifma->ifma_reqcnt == 0) {
3988 panic("%s: ifma=%p negative reqcnt", __func__, ifma);
3989 /* NOTREACHED */
3990 } else if (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
3991 panic("%s: ifma=%p missing IFMAF_ANONYMOUS", __func__, ifma);
3992 /* NOTREACHED */
3993 } else if (anon && ifma->ifma_anoncnt == 0) {
3994 panic("%s: ifma=%p negative anonreqcnt", __func__, ifma);
3995 /* NOTREACHED */
3996 } else if (ifma->ifma_ifp != ifp) {
3997 panic("%s: Mismatch ifma_ifp=%p, ifp=%p", __func__,
3998 ifma->ifma_ifp, ifp);
3999 /* NOTREACHED */
4000 }
4001
4002 if (anon) {
4003 --ifma->ifma_anoncnt;
4004 if (ifma->ifma_anoncnt > 0)
4005 return (0);
4006 ifma->ifma_flags &= ~IFMAF_ANONYMOUS;
4007 }
4008
4009 --ifma->ifma_reqcnt;
4010 if (ifma->ifma_reqcnt > 0)
4011 return (0);
4012
4013 if (ifma->ifma_protospec != NULL) {
4014 panic("%s: Protospec not NULL for ifma=%p", __func__, ifma);
4015 /* NOTREACHED */
4016 } else if ((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4017 ifma->ifma_anoncnt != 0) {
4018 panic("%s: Detaching ifma=%p with outstanding anon req",
4019 __func__, ifma);
4020 /* NOTREACHED */
4021 } else if (!(ifma->ifma_debug & IFD_ATTACHED)) {
4022 panic("%s: Attempt to detach an unattached address ifma=%p",
4023 __func__, ifma);
4024 /* NOTREACHED */
4025 } else if (ifma->ifma_debug & IFD_TRASHED) {
4026 panic("%s: ifma %p is already in trash list", __func__, ifma);
4027 /* NOTREACHED */
4028 }
4029
4030 /*
4031 * NOTE: Caller calls IFMA_REMREF
4032 */
4033 ifma->ifma_debug &= ~IFD_ATTACHED;
4034 LIST_REMOVE(ifma, ifma_link);
4035 if (LIST_EMPTY(&ifp->if_multiaddrs))
4036 ifp->if_updatemcasts = 0;
4037
4038 if (ifma->ifma_debug & IFD_DEBUG) {
4039 /* Become a regular mutex, just in case */
4040 IFMA_CONVERT_LOCK(ifma);
4041 lck_mtx_lock(&ifma_trash_lock);
4042 TAILQ_INSERT_TAIL(&ifma_trash_head,
4043 (struct ifmultiaddr_dbg *)ifma, ifma_trash_link);
4044 lck_mtx_unlock(&ifma_trash_lock);
4045 ifma->ifma_debug |= IFD_TRASHED;
4046 }
4047
4048 return (1);
4049 }
4050
4051 /*
4052 * Find an ifmultiaddr that matches a socket address on an interface.
4053 *
4054 * Caller is responsible for holding the ifnet_lock while calling
4055 * this function.
4056 */
4057 static int
4058 if_addmulti_doesexist(struct ifnet *ifp, const struct sockaddr *sa,
4059 struct ifmultiaddr **retifma, int anon)
4060 {
4061 struct ifmultiaddr *ifma;
4062
4063 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4064 ifma = LIST_NEXT(ifma, ifma_link)) {
4065 IFMA_LOCK_SPIN(ifma);
4066 if (!ifa_equal(sa, ifma->ifma_addr)) {
4067 IFMA_UNLOCK(ifma);
4068 continue;
4069 }
4070 if (anon) {
4071 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4072 ifma->ifma_anoncnt != 0);
4073 VERIFY((ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4074 ifma->ifma_anoncnt == 0);
4075 ifma->ifma_anoncnt++;
4076 if (!(ifma->ifma_flags & IFMAF_ANONYMOUS)) {
4077 VERIFY(ifma->ifma_anoncnt == 1);
4078 ifma->ifma_flags |= IFMAF_ANONYMOUS;
4079 }
4080 }
4081 if (!anon || ifma->ifma_anoncnt == 1) {
4082 ifma->ifma_reqcnt++;
4083 VERIFY(ifma->ifma_reqcnt > 1);
4084 }
4085 if (retifma != NULL) {
4086 *retifma = ifma;
4087 IFMA_ADDREF_LOCKED(ifma);
4088 }
4089 IFMA_UNLOCK(ifma);
4090 return (0);
4091 }
4092 return (ENOENT);
4093 }
4094
4095 /*
4096 * Radar 3642395, make sure all multicasts are in a standard format.
4097 */
4098 static struct sockaddr *
4099 copy_and_normalize(const struct sockaddr *original)
4100 {
4101 int alen = 0;
4102 const u_char *aptr = NULL;
4103 struct sockaddr *copy = NULL;
4104 struct sockaddr_dl *sdl_new = NULL;
4105 int len = 0;
4106
4107 if (original->sa_family != AF_LINK &&
4108 original->sa_family != AF_UNSPEC) {
4109 /* Just make a copy */
4110 MALLOC(copy, struct sockaddr *, original->sa_len,
4111 M_IFADDR, M_WAITOK);
4112 if (copy != NULL)
4113 bcopy(original, copy, original->sa_len);
4114 return (copy);
4115 }
4116
4117 switch (original->sa_family) {
4118 case AF_LINK: {
4119 const struct sockaddr_dl *sdl_original =
4120 (struct sockaddr_dl *)(uintptr_t)(size_t)original;
4121
4122 if (sdl_original->sdl_nlen + sdl_original->sdl_alen +
4123 sdl_original->sdl_slen +
4124 offsetof(struct sockaddr_dl, sdl_data) >
4125 sdl_original->sdl_len)
4126 return (NULL);
4127
4128 alen = sdl_original->sdl_alen;
4129 aptr = CONST_LLADDR(sdl_original);
4130 }
4131 break;
4132
4133 case AF_UNSPEC: {
4134 if (original->sa_len < ETHER_ADDR_LEN +
4135 offsetof(struct sockaddr, sa_data)) {
4136 return (NULL);
4137 }
4138
4139 alen = ETHER_ADDR_LEN;
4140 aptr = (const u_char *)original->sa_data;
4141 }
4142 break;
4143 }
4144
4145 if (alen == 0 || aptr == NULL)
4146 return (NULL);
4147
4148 len = alen + offsetof(struct sockaddr_dl, sdl_data);
4149 MALLOC(sdl_new, struct sockaddr_dl *, len, M_IFADDR, M_WAITOK);
4150
4151 if (sdl_new != NULL) {
4152 bzero(sdl_new, len);
4153 sdl_new->sdl_len = len;
4154 sdl_new->sdl_family = AF_LINK;
4155 sdl_new->sdl_alen = alen;
4156 bcopy(aptr, LLADDR(sdl_new), alen);
4157 }
4158
4159 return ((struct sockaddr *)sdl_new);
4160 }
4161
4162 /*
4163 * Network-layer protocol domains which hold references to the underlying
4164 * link-layer record must use this routine.
4165 */
4166 int
4167 if_addmulti(struct ifnet *ifp, const struct sockaddr *sa,
4168 struct ifmultiaddr **retifma)
4169 {
4170 return (if_addmulti_common(ifp, sa, retifma, 0));
4171 }
4172
4173 /*
4174 * Anything other than network-layer protocol domains which hold references
4175 * to the underlying link-layer record must use this routine: SIOCADDMULTI
4176 * ioctl, ifnet_add_multicast(), if_bond.
4177 */
4178 int
4179 if_addmulti_anon(struct ifnet *ifp, const struct sockaddr *sa,
4180 struct ifmultiaddr **retifma)
4181 {
4182 return (if_addmulti_common(ifp, sa, retifma, 1));
4183 }
4184
4185 /*
4186 * Register an additional multicast address with a network interface.
4187 *
4188 * - If the address is already present, bump the reference count on the
4189 * address and return.
4190 * - If the address is not link-layer, look up a link layer address.
4191 * - Allocate address structures for one or both addresses, and attach to the
4192 * multicast address list on the interface. If automatically adding a link
4193 * layer address, the protocol address will own a reference to the link
4194 * layer address, to be freed when it is freed.
4195 * - Notify the network device driver of an addition to the multicast address
4196 * list.
4197 *
4198 * 'sa' points to caller-owned memory with the desired multicast address.
4199 *
4200 * 'retifma' will be used to return a pointer to the resulting multicast
4201 * address reference, if desired.
4202 *
4203 * 'anon' indicates a link-layer address with no protocol address reference
4204 * made to it. Anything other than network-layer protocol domain requests
4205 * are considered as anonymous.
4206 */
4207 static int
4208 if_addmulti_common(struct ifnet *ifp, const struct sockaddr *sa,
4209 struct ifmultiaddr **retifma, int anon)
4210 {
4211 struct sockaddr_storage storage;
4212 struct sockaddr *llsa = NULL;
4213 struct sockaddr *dupsa = NULL;
4214 int error = 0, ll_firstref = 0, lladdr;
4215 struct ifmultiaddr *ifma = NULL;
4216 struct ifmultiaddr *llifma = NULL;
4217
4218 /* Only AF_UNSPEC/AF_LINK is allowed for an "anonymous" address */
4219 VERIFY(!anon || sa->sa_family == AF_UNSPEC ||
4220 sa->sa_family == AF_LINK);
4221
4222 /* If sa is a AF_LINK or AF_UNSPEC, duplicate and normalize it */
4223 if (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC) {
4224 dupsa = copy_and_normalize(sa);
4225 if (dupsa == NULL) {
4226 error = ENOMEM;
4227 goto cleanup;
4228 }
4229 sa = dupsa;
4230 }
4231
4232 ifnet_lock_exclusive(ifp);
4233 if (!(ifp->if_flags & IFF_MULTICAST)) {
4234 error = EADDRNOTAVAIL;
4235 ifnet_lock_done(ifp);
4236 goto cleanup;
4237 }
4238
4239 /* If the address is already present, return a new reference to it */
4240 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4241 ifnet_lock_done(ifp);
4242 if (error == 0)
4243 goto cleanup;
4244
4245 /*
4246 * The address isn't already present; give the link layer a chance
4247 * to accept/reject it, and also find out which AF_LINK address this
4248 * maps to, if it isn't one already.
4249 */
4250 error = dlil_resolve_multi(ifp, sa, (struct sockaddr *)&storage,
4251 sizeof (storage));
4252 if (error == 0 && storage.ss_len != 0) {
4253 llsa = copy_and_normalize((struct sockaddr *)&storage);
4254 if (llsa == NULL) {
4255 error = ENOMEM;
4256 goto cleanup;
4257 }
4258
4259 llifma = ifma_alloc(M_WAITOK);
4260 if (llifma == NULL) {
4261 error = ENOMEM;
4262 goto cleanup;
4263 }
4264 }
4265
4266 /* to be similar to FreeBSD */
4267 if (error == EOPNOTSUPP)
4268 error = 0;
4269 else if (error != 0)
4270 goto cleanup;
4271
4272 /* Allocate while we aren't holding any locks */
4273 if (dupsa == NULL) {
4274 dupsa = copy_and_normalize(sa);
4275 if (dupsa == NULL) {
4276 error = ENOMEM;
4277 goto cleanup;
4278 }
4279 }
4280 ifma = ifma_alloc(M_WAITOK);
4281 if (ifma == NULL) {
4282 error = ENOMEM;
4283 goto cleanup;
4284 }
4285
4286 ifnet_lock_exclusive(ifp);
4287 /*
4288 * Check again for the matching multicast.
4289 */
4290 error = if_addmulti_doesexist(ifp, sa, retifma, anon);
4291 if (error == 0) {
4292 ifnet_lock_done(ifp);
4293 goto cleanup;
4294 }
4295
4296 if (llifma != NULL) {
4297 VERIFY(!anon); /* must not get here if "anonymous" */
4298 if (if_addmulti_doesexist(ifp, llsa, &ifma->ifma_ll, 0) == 0) {
4299 FREE(llsa, M_IFADDR);
4300 llsa = NULL;
4301 ifma_free(llifma);
4302 llifma = NULL;
4303 VERIFY(ifma->ifma_ll->ifma_ifp == ifp);
4304 } else {
4305 ll_firstref = 1;
4306 llifma->ifma_addr = llsa;
4307 llifma->ifma_ifp = ifp;
4308 IFMA_LOCK(llifma);
4309 if_attach_ifma(ifp, llifma, 0);
4310 /* add extra refcnt for ifma */
4311 IFMA_ADDREF_LOCKED(llifma);
4312 IFMA_UNLOCK(llifma);
4313 ifma->ifma_ll = llifma;
4314 }
4315 }
4316
4317 /* "anonymous" request should not result in network address */
4318 VERIFY(!anon || ifma->ifma_ll == NULL);
4319
4320 ifma->ifma_addr = dupsa;
4321 ifma->ifma_ifp = ifp;
4322 IFMA_LOCK(ifma);
4323 if_attach_ifma(ifp, ifma, anon);
4324 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4325 if (retifma != NULL) {
4326 *retifma = ifma;
4327 IFMA_ADDREF_LOCKED(*retifma); /* for caller */
4328 }
4329 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4330 ifma->ifma_addr->sa_family == AF_LINK);
4331 IFMA_UNLOCK(ifma);
4332 ifnet_lock_done(ifp);
4333
4334 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
4335 IFMA_REMREF(ifma); /* for this routine */
4336
4337 /*
4338 * We are certain we have added something, so call down to the
4339 * interface to let them know about it. Do this only for newly-
4340 * added AF_LINK/AF_UNSPEC address in the if_multiaddrs set.
4341 */
4342 if (lladdr || ll_firstref)
4343 (void) ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL);
4344
4345 if (ifp->if_updatemcasts > 0)
4346 ifp->if_updatemcasts = 0;
4347
4348 return (0);
4349
4350 cleanup:
4351 if (ifma != NULL)
4352 ifma_free(ifma);
4353 if (dupsa != NULL)
4354 FREE(dupsa, M_IFADDR);
4355 if (llifma != NULL)
4356 ifma_free(llifma);
4357 if (llsa != NULL)
4358 FREE(llsa, M_IFADDR);
4359
4360 return (error);
4361 }
4362
4363 /*
4364 * Delete a multicast group membership by network-layer group address.
4365 * This routine is deprecated.
4366 */
4367 int
4368 if_delmulti(struct ifnet *ifp, const struct sockaddr *sa)
4369 {
4370 return (if_delmulti_common(NULL, ifp, sa, 0));
4371 }
4372
4373 /*
4374 * Delete a multicast group membership by group membership pointer.
4375 * Network-layer protocol domains must use this routine.
4376 */
4377 int
4378 if_delmulti_ifma(struct ifmultiaddr *ifma)
4379 {
4380 return (if_delmulti_common(ifma, NULL, NULL, 0));
4381 }
4382
4383 /*
4384 * Anything other than network-layer protocol domains which hold references
4385 * to the underlying link-layer record must use this routine: SIOCDELMULTI
4386 * ioctl, ifnet_remove_multicast(), if_bond.
4387 */
4388 int
4389 if_delmulti_anon(struct ifnet *ifp, const struct sockaddr *sa)
4390 {
4391 return (if_delmulti_common(NULL, ifp, sa, 1));
4392 }
4393
4394 /*
4395 * Delete a multicast group membership by network-layer group address.
4396 *
4397 * Returns ENOENT if the entry could not be found.
4398 */
4399 static int
4400 if_delmulti_common(struct ifmultiaddr *ifma, struct ifnet *ifp,
4401 const struct sockaddr *sa, int anon)
4402 {
4403 struct sockaddr *dupsa = NULL;
4404 int lastref, ll_lastref = 0, lladdr;
4405 struct ifmultiaddr *ll = NULL;
4406
4407 /* sanity check for callers */
4408 VERIFY(ifma != NULL || (ifp != NULL && sa != NULL));
4409
4410 if (ifma != NULL)
4411 ifp = ifma->ifma_ifp;
4412
4413 if (sa != NULL &&
4414 (sa->sa_family == AF_LINK || sa->sa_family == AF_UNSPEC)) {
4415 dupsa = copy_and_normalize(sa);
4416 if (dupsa == NULL)
4417 return (ENOMEM);
4418 sa = dupsa;
4419 }
4420
4421 ifnet_lock_exclusive(ifp);
4422 if (ifma == NULL) {
4423 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
4424 ifma = LIST_NEXT(ifma, ifma_link)) {
4425 IFMA_LOCK(ifma);
4426 if (!ifa_equal(sa, ifma->ifma_addr) ||
4427 (anon && !(ifma->ifma_flags & IFMAF_ANONYMOUS))) {
4428 VERIFY(!(ifma->ifma_flags & IFMAF_ANONYMOUS) ||
4429 ifma->ifma_anoncnt != 0);
4430 IFMA_UNLOCK(ifma);
4431 continue;
4432 }
4433 /* found; keep it locked */
4434 break;
4435 }
4436 if (ifma == NULL) {
4437 if (dupsa != NULL)
4438 FREE(dupsa, M_IFADDR);
4439 ifnet_lock_done(ifp);
4440 return (ENOENT);
4441 }
4442 } else {
4443 IFMA_LOCK(ifma);
4444 }
4445 IFMA_LOCK_ASSERT_HELD(ifma);
4446 IFMA_ADDREF_LOCKED(ifma); /* for this routine */
4447 lastref = if_detach_ifma(ifp, ifma, anon);
4448 VERIFY(!lastref || (!(ifma->ifma_debug & IFD_ATTACHED) &&
4449 ifma->ifma_reqcnt == 0));
4450 VERIFY(!anon || ifma->ifma_ll == NULL);
4451 ll = ifma->ifma_ll;
4452 lladdr = (ifma->ifma_addr->sa_family == AF_UNSPEC ||
4453 ifma->ifma_addr->sa_family == AF_LINK);
4454 IFMA_UNLOCK(ifma);
4455 if (lastref && ll != NULL) {
4456 IFMA_LOCK(ll);
4457 ll_lastref = if_detach_ifma(ifp, ll, 0);
4458 IFMA_UNLOCK(ll);
4459 }
4460 ifnet_lock_done(ifp);
4461
4462 if (lastref)
4463 rt_newmaddrmsg(RTM_DELMADDR, ifma);
4464
4465 if ((ll == NULL && lastref && lladdr) || ll_lastref) {
4466 /*
4467 * Make sure the interface driver is notified in the
4468 * case of a link layer mcast group being left. Do
4469 * this only for a AF_LINK/AF_UNSPEC address that has
4470 * been removed from the if_multiaddrs set.
4471 */
4472 ifnet_ioctl(ifp, 0, SIOCDELMULTI, NULL);
4473 }
4474
4475 if (lastref)
4476 IFMA_REMREF(ifma); /* for if_multiaddrs list */
4477 if (ll_lastref)
4478 IFMA_REMREF(ll); /* for if_multiaddrs list */
4479
4480 IFMA_REMREF(ifma); /* for this routine */
4481 if (dupsa != NULL)
4482 FREE(dupsa, M_IFADDR);
4483
4484 return (0);
4485 }
4486
4487 /*
4488 * Shutdown all network activity. Used boot() when halting
4489 * system.
4490 */
4491 int
4492 if_down_all(void)
4493 {
4494 struct ifnet **ifp;
4495 u_int32_t count;
4496 u_int32_t i;
4497
4498 if (ifnet_list_get_all(IFNET_FAMILY_ANY, &ifp, &count) == 0) {
4499 for (i = 0; i < count; i++) {
4500 if_down(ifp[i]);
4501 dlil_proto_unplumb_all(ifp[i]);
4502 }
4503 ifnet_list_free(ifp);
4504 }
4505
4506 return (0);
4507 }
4508
4509 /*
4510 * Delete Routes for a Network Interface
4511 *
4512 * Called for each routing entry via the rnh->rnh_walktree() call above
4513 * to delete all route entries referencing a detaching network interface.
4514 *
4515 * Arguments:
4516 * rn pointer to node in the routing table
4517 * arg argument passed to rnh->rnh_walktree() - detaching interface
4518 *
4519 * Returns:
4520 * 0 successful
4521 * errno failed - reason indicated
4522 *
4523 */
4524 static int
4525 if_rtdel(struct radix_node *rn, void *arg)
4526 {
4527 struct rtentry *rt = (struct rtentry *)rn;
4528 struct ifnet *ifp = arg;
4529 int err;
4530
4531 if (rt == NULL)
4532 return (0);
4533 /*
4534 * Checking against RTF_UP protects against walktree
4535 * recursion problems with cloned routes.
4536 */
4537 RT_LOCK(rt);
4538 if (rt->rt_ifp == ifp && (rt->rt_flags & RTF_UP)) {
4539 /*
4540 * Safe to drop rt_lock and use rt_key, rt_gateway,
4541 * since holding rnh_lock here prevents another thread
4542 * from calling rt_setgate() on this route.
4543 */
4544 RT_UNLOCK(rt);
4545 err = rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
4546 rt_mask(rt), rt->rt_flags, NULL);
4547 if (err) {
4548 log(LOG_WARNING, "if_rtdel: error %d\n", err);
4549 }
4550 } else {
4551 RT_UNLOCK(rt);
4552 }
4553 return (0);
4554 }
4555
4556 /*
4557 * Removes routing table reference to a given interface
4558 * for a given protocol family
4559 */
4560 void
4561 if_rtproto_del(struct ifnet *ifp, int protocol)
4562 {
4563 struct radix_node_head *rnh;
4564
4565 if ((protocol <= AF_MAX) && (protocol >= 0) &&
4566 ((rnh = rt_tables[protocol]) != NULL) && (ifp != NULL)) {
4567 lck_mtx_lock(rnh_lock);
4568 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
4569 lck_mtx_unlock(rnh_lock);
4570 }
4571 }
4572
4573 static int
4574 if_rtmtu(struct radix_node *rn, void *arg)
4575 {
4576 struct rtentry *rt = (struct rtentry *)rn;
4577 struct ifnet *ifp = arg;
4578
4579 RT_LOCK(rt);
4580 if (rt->rt_ifp == ifp) {
4581 /*
4582 * Update the MTU of this entry only if the MTU
4583 * has not been locked (RTV_MTU is not set) and
4584 * if it was non-zero to begin with.
4585 */
4586 if (!(rt->rt_rmx.rmx_locks & RTV_MTU) && rt->rt_rmx.rmx_mtu)
4587 rt->rt_rmx.rmx_mtu = ifp->if_mtu;
4588 }
4589 RT_UNLOCK(rt);
4590
4591 return (0);
4592 }
4593
4594 /*
4595 * Update the MTU metric of all route entries in all protocol tables
4596 * associated with a particular interface; this is called when the
4597 * MTU of that interface has changed.
4598 */
4599 static void
4600 if_rtmtu_update(struct ifnet *ifp)
4601 {
4602 struct radix_node_head *rnh;
4603 int p;
4604
4605 for (p = 0; p < AF_MAX + 1; p++) {
4606 if ((rnh = rt_tables[p]) == NULL)
4607 continue;
4608
4609 lck_mtx_lock(rnh_lock);
4610 (void) rnh->rnh_walktree(rnh, if_rtmtu, ifp);
4611 lck_mtx_unlock(rnh_lock);
4612 }
4613 routegenid_update();
4614 }
4615
4616 __private_extern__ void
4617 if_data_internal_to_if_data(struct ifnet *ifp,
4618 const struct if_data_internal *if_data_int, struct if_data *if_data)
4619 {
4620 #pragma unused(ifp)
4621 #define COPYFIELD(fld) if_data->fld = if_data_int->fld
4622 #define COPYFIELD32(fld) if_data->fld = (u_int32_t)(if_data_int->fld)
4623 /* compiler will cast down to 32-bit */
4624 #define COPYFIELD32_ATOMIC(fld) do { \
4625 atomic_get_64(if_data->fld, \
4626 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4627 } while (0)
4628
4629 COPYFIELD(ifi_type);
4630 COPYFIELD(ifi_typelen);
4631 COPYFIELD(ifi_physical);
4632 COPYFIELD(ifi_addrlen);
4633 COPYFIELD(ifi_hdrlen);
4634 COPYFIELD(ifi_recvquota);
4635 COPYFIELD(ifi_xmitquota);
4636 if_data->ifi_unused1 = 0;
4637 COPYFIELD(ifi_mtu);
4638 COPYFIELD(ifi_metric);
4639 if (if_data_int->ifi_baudrate & 0xFFFFFFFF00000000LL) {
4640 if_data->ifi_baudrate = 0xFFFFFFFF;
4641 } else {
4642 COPYFIELD32(ifi_baudrate);
4643 }
4644
4645 COPYFIELD32_ATOMIC(ifi_ipackets);
4646 COPYFIELD32_ATOMIC(ifi_ierrors);
4647 COPYFIELD32_ATOMIC(ifi_opackets);
4648 COPYFIELD32_ATOMIC(ifi_oerrors);
4649 COPYFIELD32_ATOMIC(ifi_collisions);
4650 COPYFIELD32_ATOMIC(ifi_ibytes);
4651 COPYFIELD32_ATOMIC(ifi_obytes);
4652 COPYFIELD32_ATOMIC(ifi_imcasts);
4653 COPYFIELD32_ATOMIC(ifi_omcasts);
4654 COPYFIELD32_ATOMIC(ifi_iqdrops);
4655 COPYFIELD32_ATOMIC(ifi_noproto);
4656
4657 COPYFIELD(ifi_recvtiming);
4658 COPYFIELD(ifi_xmittiming);
4659
4660 if_data->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
4661 if_data->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
4662
4663 if_data->ifi_lastchange.tv_sec += boottime_sec();
4664
4665 if_data->ifi_unused2 = 0;
4666 COPYFIELD(ifi_hwassist);
4667 if_data->ifi_reserved1 = 0;
4668 if_data->ifi_reserved2 = 0;
4669 #undef COPYFIELD32_ATOMIC
4670 #undef COPYFIELD32
4671 #undef COPYFIELD
4672 }
4673
4674 __private_extern__ void
4675 if_data_internal_to_if_data64(struct ifnet *ifp,
4676 const struct if_data_internal *if_data_int,
4677 struct if_data64 *if_data64)
4678 {
4679 #pragma unused(ifp)
4680 #define COPYFIELD64(fld) if_data64->fld = if_data_int->fld
4681 #define COPYFIELD64_ATOMIC(fld) do { \
4682 atomic_get_64(if_data64->fld, \
4683 (u_int64_t *)(void *)(uintptr_t)&if_data_int->fld); \
4684 } while (0)
4685
4686 COPYFIELD64(ifi_type);
4687 COPYFIELD64(ifi_typelen);
4688 COPYFIELD64(ifi_physical);
4689 COPYFIELD64(ifi_addrlen);
4690 COPYFIELD64(ifi_hdrlen);
4691 COPYFIELD64(ifi_recvquota);
4692 COPYFIELD64(ifi_xmitquota);
4693 if_data64->ifi_unused1 = 0;
4694 COPYFIELD64(ifi_mtu);
4695 COPYFIELD64(ifi_metric);
4696 COPYFIELD64(ifi_baudrate);
4697
4698 COPYFIELD64_ATOMIC(ifi_ipackets);
4699 COPYFIELD64_ATOMIC(ifi_ierrors);
4700 COPYFIELD64_ATOMIC(ifi_opackets);
4701 COPYFIELD64_ATOMIC(ifi_oerrors);
4702 COPYFIELD64_ATOMIC(ifi_collisions);
4703 COPYFIELD64_ATOMIC(ifi_ibytes);
4704 COPYFIELD64_ATOMIC(ifi_obytes);
4705 COPYFIELD64_ATOMIC(ifi_imcasts);
4706 COPYFIELD64_ATOMIC(ifi_omcasts);
4707 COPYFIELD64_ATOMIC(ifi_iqdrops);
4708 COPYFIELD64_ATOMIC(ifi_noproto);
4709
4710 /*
4711 * Note these two fields are actually 32 bit, so doing
4712 * COPYFIELD64_ATOMIC will cause them to be misaligned
4713 */
4714 COPYFIELD64(ifi_recvtiming);
4715 COPYFIELD64(ifi_xmittiming);
4716
4717 if_data64->ifi_lastchange.tv_sec = if_data_int->ifi_lastchange.tv_sec;
4718 if_data64->ifi_lastchange.tv_usec = if_data_int->ifi_lastchange.tv_usec;
4719
4720 if_data64->ifi_lastchange.tv_sec += boottime_sec();
4721
4722 #undef COPYFIELD64
4723 }
4724
4725 __private_extern__ void
4726 if_copy_traffic_class(struct ifnet *ifp,
4727 struct if_traffic_class *if_tc)
4728 {
4729 #define COPY_IF_TC_FIELD64_ATOMIC(fld) do { \
4730 atomic_get_64(if_tc->fld, \
4731 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tc.fld); \
4732 } while (0)
4733
4734 bzero(if_tc, sizeof (*if_tc));
4735 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibepackets);
4736 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibebytes);
4737 COPY_IF_TC_FIELD64_ATOMIC(ifi_obepackets);
4738 COPY_IF_TC_FIELD64_ATOMIC(ifi_obebytes);
4739 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkpackets);
4740 COPY_IF_TC_FIELD64_ATOMIC(ifi_ibkbytes);
4741 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkpackets);
4742 COPY_IF_TC_FIELD64_ATOMIC(ifi_obkbytes);
4743 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivipackets);
4744 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivibytes);
4745 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovipackets);
4746 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovibytes);
4747 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivopackets);
4748 COPY_IF_TC_FIELD64_ATOMIC(ifi_ivobytes);
4749 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovopackets);
4750 COPY_IF_TC_FIELD64_ATOMIC(ifi_ovobytes);
4751 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvpackets);
4752 COPY_IF_TC_FIELD64_ATOMIC(ifi_ipvbytes);
4753 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvpackets);
4754 COPY_IF_TC_FIELD64_ATOMIC(ifi_opvbytes);
4755
4756 #undef COPY_IF_TC_FIELD64_ATOMIC
4757 }
4758
4759 void
4760 if_copy_data_extended(struct ifnet *ifp, struct if_data_extended *if_de)
4761 {
4762 #define COPY_IF_DE_FIELD64_ATOMIC(fld) do { \
4763 atomic_get_64(if_de->fld, \
4764 (u_int64_t *)(void *)(uintptr_t)&ifp->if_data.fld); \
4765 } while (0)
4766
4767 bzero(if_de, sizeof (*if_de));
4768 COPY_IF_DE_FIELD64_ATOMIC(ifi_alignerrs);
4769 COPY_IF_DE_FIELD64_ATOMIC(ifi_dt_bytes);
4770 COPY_IF_DE_FIELD64_ATOMIC(ifi_fpackets);
4771 COPY_IF_DE_FIELD64_ATOMIC(ifi_fbytes);
4772
4773 #undef COPY_IF_DE_FIELD64_ATOMIC
4774 }
4775
4776 void
4777 if_copy_packet_stats(struct ifnet *ifp, struct if_packet_stats *if_ps)
4778 {
4779 #define COPY_IF_PS_TCP_FIELD64_ATOMIC(fld) do { \
4780 atomic_get_64(if_ps->ifi_tcp_##fld, \
4781 (u_int64_t *)(void *)(uintptr_t)&ifp->if_tcp_stat->fld); \
4782 } while (0)
4783
4784 #define COPY_IF_PS_UDP_FIELD64_ATOMIC(fld) do { \
4785 atomic_get_64(if_ps->ifi_udp_##fld, \
4786 (u_int64_t *)(void *)(uintptr_t)&ifp->if_udp_stat->fld); \
4787 } while (0)
4788
4789 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformat);
4790 COPY_IF_PS_TCP_FIELD64_ATOMIC(unspecv6);
4791 COPY_IF_PS_TCP_FIELD64_ATOMIC(synfin);
4792 COPY_IF_PS_TCP_FIELD64_ATOMIC(badformatipsec);
4793 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnnolist);
4794 COPY_IF_PS_TCP_FIELD64_ATOMIC(noconnlist);
4795 COPY_IF_PS_TCP_FIELD64_ATOMIC(listbadsyn);
4796 COPY_IF_PS_TCP_FIELD64_ATOMIC(icmp6unreach);
4797 COPY_IF_PS_TCP_FIELD64_ATOMIC(deprecate6);
4798 COPY_IF_PS_TCP_FIELD64_ATOMIC(ooopacket);
4799 COPY_IF_PS_TCP_FIELD64_ATOMIC(rstinsynrcv);
4800 COPY_IF_PS_TCP_FIELD64_ATOMIC(dospacket);
4801 COPY_IF_PS_TCP_FIELD64_ATOMIC(cleanup);
4802 COPY_IF_PS_TCP_FIELD64_ATOMIC(synwindow);
4803
4804 COPY_IF_PS_UDP_FIELD64_ATOMIC(port_unreach);
4805 COPY_IF_PS_UDP_FIELD64_ATOMIC(faithprefix);
4806 COPY_IF_PS_UDP_FIELD64_ATOMIC(port0);
4807 COPY_IF_PS_UDP_FIELD64_ATOMIC(badlength);
4808 COPY_IF_PS_UDP_FIELD64_ATOMIC(badchksum);
4809 COPY_IF_PS_UDP_FIELD64_ATOMIC(badmcast);
4810 COPY_IF_PS_UDP_FIELD64_ATOMIC(cleanup);
4811 COPY_IF_PS_UDP_FIELD64_ATOMIC(badipsec);
4812
4813 #undef COPY_IF_PS_TCP_FIELD64_ATOMIC
4814 #undef COPY_IF_PS_UDP_FIELD64_ATOMIC
4815 }
4816
4817 void
4818 if_copy_rxpoll_stats(struct ifnet *ifp, struct if_rxpoll_stats *if_rs)
4819 {
4820 bzero(if_rs, sizeof (*if_rs));
4821 if (!(ifp->if_eflags & IFEF_RXPOLL) || !ifnet_is_attached(ifp, 1))
4822 return;
4823
4824 /* by now, ifnet will stay attached so if_inp must be valid */
4825 VERIFY(ifp->if_inp != NULL);
4826 bcopy(&ifp->if_inp->pstats, if_rs, sizeof (*if_rs));
4827
4828 /* Release the IO refcnt */
4829 ifnet_decr_iorefcnt(ifp);
4830 }
4831
4832 struct ifaddr *
4833 ifa_remref(struct ifaddr *ifa, int locked)
4834 {
4835 if (!locked)
4836 IFA_LOCK_SPIN(ifa);
4837 else
4838 IFA_LOCK_ASSERT_HELD(ifa);
4839
4840 if (ifa->ifa_refcnt == 0)
4841 panic("%s: ifa %p negative refcnt\n", __func__, ifa);
4842 else if (ifa->ifa_trace != NULL)
4843 (*ifa->ifa_trace)(ifa, FALSE);
4844 if (--ifa->ifa_refcnt == 0) {
4845 if (ifa->ifa_debug & IFD_ATTACHED)
4846 panic("ifa %p attached to ifp is being freed\n", ifa);
4847 /*
4848 * Some interface addresses are allocated either statically
4849 * or carved out of a larger block. Only free it if it was
4850 * allocated via MALLOC or via the corresponding per-address
4851 * family allocator. Otherwise, leave it alone.
4852 */
4853 if (ifa->ifa_debug & IFD_ALLOC) {
4854 if (ifa->ifa_free == NULL) {
4855 IFA_UNLOCK(ifa);
4856 FREE(ifa, M_IFADDR);
4857 } else {
4858 /* Become a regular mutex */
4859 IFA_CONVERT_LOCK(ifa);
4860 /* callee will unlock */
4861 (*ifa->ifa_free)(ifa);
4862 }
4863 } else {
4864 IFA_UNLOCK(ifa);
4865 }
4866 ifa = NULL;
4867 }
4868
4869 if (!locked && ifa != NULL)
4870 IFA_UNLOCK(ifa);
4871
4872 return (ifa);
4873 }
4874
4875 void
4876 ifa_addref(struct ifaddr *ifa, int locked)
4877 {
4878 if (!locked)
4879 IFA_LOCK_SPIN(ifa);
4880 else
4881 IFA_LOCK_ASSERT_HELD(ifa);
4882
4883 if (++ifa->ifa_refcnt == 0) {
4884 panic("%s: ifa %p wraparound refcnt\n", __func__, ifa);
4885 /* NOTREACHED */
4886 } else if (ifa->ifa_trace != NULL) {
4887 (*ifa->ifa_trace)(ifa, TRUE);
4888 }
4889 if (!locked)
4890 IFA_UNLOCK(ifa);
4891 }
4892
4893 void
4894 ifa_lock_init(struct ifaddr *ifa)
4895 {
4896 lck_mtx_init(&ifa->ifa_lock, ifa_mtx_grp, ifa_mtx_attr);
4897 }
4898
4899 void
4900 ifa_lock_destroy(struct ifaddr *ifa)
4901 {
4902 IFA_LOCK_ASSERT_NOTHELD(ifa);
4903 lck_mtx_destroy(&ifa->ifa_lock, ifa_mtx_grp);
4904 }
4905
4906 /*
4907 * 'i' group ioctls.
4908 *
4909 * The switch statement below does nothing at runtime, as it serves as a
4910 * compile time check to ensure that all of the socket 'i' ioctls (those
4911 * in the 'i' group going thru soo_ioctl) that are made available by the
4912 * networking stack is unique. This works as long as this routine gets
4913 * updated each time a new interface ioctl gets added.
4914 *
4915 * Any failures at compile time indicates duplicated ioctl values.
4916 */
4917 static __attribute__((unused)) void
4918 ifioctl_cassert(void)
4919 {
4920 /*
4921 * This is equivalent to _CASSERT() and the compiler wouldn't
4922 * generate any instructions, thus for compile time only.
4923 */
4924 switch ((u_long)0) {
4925 case 0:
4926
4927 /* bsd/net/if_ppp.h */
4928 case SIOCGPPPSTATS:
4929 case SIOCGPPPCSTATS:
4930
4931 #if INET6
4932 /* bsd/netinet6/in6_var.h */
4933 case SIOCSIFADDR_IN6:
4934 case SIOCGIFADDR_IN6:
4935 case SIOCSIFDSTADDR_IN6:
4936 case SIOCSIFNETMASK_IN6:
4937 case SIOCGIFDSTADDR_IN6:
4938 case SIOCGIFNETMASK_IN6:
4939 case SIOCDIFADDR_IN6:
4940 case SIOCAIFADDR_IN6_32:
4941 case SIOCAIFADDR_IN6_64:
4942 case SIOCSIFPHYADDR_IN6_32:
4943 case SIOCSIFPHYADDR_IN6_64:
4944 case SIOCGIFPSRCADDR_IN6:
4945 case SIOCGIFPDSTADDR_IN6:
4946 case SIOCGIFAFLAG_IN6:
4947 case SIOCGDRLST_IN6_32:
4948 case SIOCGDRLST_IN6_64:
4949 case SIOCGPRLST_IN6_32:
4950 case SIOCGPRLST_IN6_64:
4951 case OSIOCGIFINFO_IN6:
4952 case SIOCGIFINFO_IN6:
4953 case SIOCSNDFLUSH_IN6:
4954 case SIOCGNBRINFO_IN6_32:
4955 case SIOCGNBRINFO_IN6_64:
4956 case SIOCSPFXFLUSH_IN6:
4957 case SIOCSRTRFLUSH_IN6:
4958 case SIOCGIFALIFETIME_IN6:
4959 case SIOCSIFALIFETIME_IN6:
4960 case SIOCGIFSTAT_IN6:
4961 case SIOCGIFSTAT_ICMP6:
4962 case SIOCSDEFIFACE_IN6_32:
4963 case SIOCSDEFIFACE_IN6_64:
4964 case SIOCGDEFIFACE_IN6_32:
4965 case SIOCGDEFIFACE_IN6_64:
4966 case SIOCSIFINFO_FLAGS:
4967 case SIOCSSCOPE6:
4968 case SIOCGSCOPE6:
4969 case SIOCGSCOPE6DEF:
4970 case SIOCSIFPREFIX_IN6:
4971 case SIOCGIFPREFIX_IN6:
4972 case SIOCDIFPREFIX_IN6:
4973 case SIOCAIFPREFIX_IN6:
4974 case SIOCCIFPREFIX_IN6:
4975 case SIOCSGIFPREFIX_IN6:
4976 case SIOCPROTOATTACH_IN6_32:
4977 case SIOCPROTOATTACH_IN6_64:
4978 case SIOCPROTODETACH_IN6:
4979 case SIOCLL_START_32:
4980 case SIOCLL_START_64:
4981 case SIOCLL_STOP:
4982 case SIOCAUTOCONF_START:
4983 case SIOCAUTOCONF_STOP:
4984 case SIOCSETROUTERMODE_IN6:
4985 case SIOCLL_CGASTART_32:
4986 case SIOCLL_CGASTART_64:
4987 case SIOCGIFCGAPREP_IN6:
4988 case SIOCSIFCGAPREP_IN6:
4989 #endif /* INET6 */
4990
4991 /* bsd/sys/sockio.h */
4992 case SIOCSIFADDR:
4993 case OSIOCGIFADDR:
4994 case SIOCSIFDSTADDR:
4995 case OSIOCGIFDSTADDR:
4996 case SIOCSIFFLAGS:
4997 case SIOCGIFFLAGS:
4998 case OSIOCGIFBRDADDR:
4999 case SIOCSIFBRDADDR:
5000 case OSIOCGIFCONF32:
5001 case OSIOCGIFCONF64:
5002 case OSIOCGIFNETMASK:
5003 case SIOCSIFNETMASK:
5004 case SIOCGIFMETRIC:
5005 case SIOCSIFMETRIC:
5006 case SIOCDIFADDR:
5007 case SIOCAIFADDR:
5008
5009 case SIOCGIFADDR:
5010 case SIOCGIFDSTADDR:
5011 case SIOCGIFBRDADDR:
5012 case SIOCGIFCONF32:
5013 case SIOCGIFCONF64:
5014 case SIOCGIFNETMASK:
5015 case SIOCAUTOADDR:
5016 case SIOCAUTONETMASK:
5017 case SIOCARPIPLL:
5018
5019 case SIOCADDMULTI:
5020 case SIOCDELMULTI:
5021 case SIOCGIFMTU:
5022 case SIOCSIFMTU:
5023 case SIOCGIFPHYS:
5024 case SIOCSIFPHYS:
5025 case SIOCSIFMEDIA:
5026 case SIOCGIFMEDIA32:
5027 case SIOCGIFMEDIA64:
5028 case SIOCSIFGENERIC:
5029 case SIOCGIFGENERIC:
5030 case SIOCRSLVMULTI:
5031
5032 case SIOCSIFLLADDR:
5033 case SIOCGIFSTATUS:
5034 case SIOCSIFPHYADDR:
5035 case SIOCGIFPSRCADDR:
5036 case SIOCGIFPDSTADDR:
5037 case SIOCDIFPHYADDR:
5038
5039 case SIOCGIFDEVMTU:
5040 case SIOCSIFALTMTU:
5041 case SIOCGIFALTMTU:
5042 case SIOCSIFBOND:
5043 case SIOCGIFBOND:
5044
5045 case SIOCPROTOATTACH:
5046 case SIOCPROTODETACH:
5047
5048 case SIOCSIFCAP:
5049 case SIOCGIFCAP:
5050
5051 case SIOCIFCREATE:
5052 case SIOCIFDESTROY:
5053 case SIOCIFCREATE2:
5054
5055 case SIOCSDRVSPEC32:
5056 case SIOCGDRVSPEC32:
5057 case SIOCSDRVSPEC64:
5058 case SIOCGDRVSPEC64:
5059
5060 case SIOCSIFVLAN:
5061 case SIOCGIFVLAN:
5062
5063 case SIOCIFGCLONERS32:
5064 case SIOCIFGCLONERS64:
5065
5066 case SIOCGIFASYNCMAP:
5067 case SIOCSIFASYNCMAP:
5068 #if CONFIG_MACF_NET
5069 case SIOCGIFMAC:
5070 case SIOCSIFMAC:
5071 #endif /* CONFIG_MACF_NET */
5072 case SIOCSIFKPI:
5073 case SIOCGIFKPI:
5074
5075 case SIOCGIFWAKEFLAGS:
5076
5077 case SIOCGIFGETRTREFCNT:
5078 case SIOCGIFLINKQUALITYMETRIC:
5079 case SIOCSIFOPPORTUNISTIC:
5080 case SIOCGIFOPPORTUNISTIC:
5081 case SIOCSETROUTERMODE:
5082 case SIOCGIFEFLAGS:
5083 case SIOCSIFDESC:
5084 case SIOCGIFDESC:
5085 case SIOCSIFLINKPARAMS:
5086 case SIOCGIFLINKPARAMS:
5087 case SIOCGIFQUEUESTATS:
5088 case SIOCSIFTHROTTLE:
5089 case SIOCGIFTHROTTLE:
5090
5091 case SIOCGASSOCIDS32:
5092 case SIOCGASSOCIDS64:
5093 case SIOCGCONNIDS32:
5094 case SIOCGCONNIDS64:
5095 case SIOCGCONNINFO32:
5096 case SIOCGCONNINFO64:
5097 case SIOCSCONNORDER:
5098 case SIOCGCONNORDER:
5099
5100 case SIOCSIFLOG:
5101 case SIOCGIFLOG:
5102 case SIOCGIFDELEGATE:
5103 case SIOCGIFLLADDR:
5104 case SIOCGIFTYPE:
5105 case SIOCGIFEXPENSIVE:
5106 case SIOCSIFEXPENSIVE:
5107 case SIOCGIF2KCL:
5108 case SIOCSIF2KCL:
5109 case SIOCGSTARTDELAY:
5110
5111 case SIOCAIFAGENTID:
5112 case SIOCDIFAGENTID:
5113 case SIOCGIFAGENTIDS32:
5114 case SIOCGIFAGENTIDS64:
5115 case SIOCGIFAGENTDATA32:
5116 case SIOCGIFAGENTDATA64:
5117 case SIOCGIFAGENTLIST32:
5118 case SIOCGIFAGENTLIST64:
5119
5120
5121 case SIOCSIFINTERFACESTATE:
5122 case SIOCGIFINTERFACESTATE:
5123 case SIOCSIFPROBECONNECTIVITY:
5124 case SIOCGIFPROBECONNECTIVITY:
5125
5126 case SIOCGIFFUNCTIONALTYPE:
5127 case SIOCSIFNETSIGNATURE:
5128 case SIOCGIFNETSIGNATURE:
5129
5130 case SIOCGECNMODE:
5131 case SIOCSECNMODE:
5132
5133 case SIOCSQOSMARKINGMODE:
5134 case SIOCSQOSMARKINGENABLED:
5135 case SIOCGQOSMARKINGMODE:
5136 case SIOCGQOSMARKINGENABLED:
5137
5138 case SIOCGIFPROTOLIST32:
5139 case SIOCGIFPROTOLIST64:
5140 ;
5141 }
5142 }
5143
5144 uint32_t
5145 ifnet_mbuf_packetpreamblelen(struct ifnet *ifp)
5146 {
5147 #pragma unused(ifp)
5148 return (0);
5149 }
5150
5151 /* The following is used to enqueue work items for interface events */
5152 struct intf_event {
5153 struct ifnet *ifp;
5154 union sockaddr_in_4_6 addr;
5155 uint32_t intf_event_code;
5156 };
5157
5158 static void
5159 intf_event_callback(void *arg)
5160 {
5161 struct intf_event *p_intf_ev = (struct intf_event *)arg;
5162
5163 /* Call this before we walk the tree */
5164 EVENTHANDLER_INVOKE(&ifnet_evhdlr_ctxt, ifnet_event, p_intf_ev->ifp,
5165 (struct sockaddr *)&(p_intf_ev->addr), p_intf_ev->intf_event_code);
5166 }
5167
5168 struct intf_event_nwk_wq_entry {
5169 struct nwk_wq_entry nwk_wqe;
5170 struct intf_event intf_ev_arg;
5171 };
5172
5173 void
5174 intf_event_enqueue_nwk_wq_entry(struct ifnet *ifp, struct sockaddr *addrp,
5175 uint32_t intf_event_code)
5176 {
5177 #pragma unused(addrp)
5178 struct intf_event_nwk_wq_entry *p_intf_ev = NULL;
5179
5180 MALLOC(p_intf_ev, struct intf_event_nwk_wq_entry *,
5181 sizeof(struct intf_event_nwk_wq_entry),
5182 M_NWKWQ, M_WAITOK | M_ZERO);
5183
5184 p_intf_ev->intf_ev_arg.ifp = ifp;
5185 /*
5186 * XXX Not using addr in the arg. This will be used
5187 * once we need IP address add/delete events
5188 */
5189 p_intf_ev->intf_ev_arg.intf_event_code = intf_event_code;
5190 p_intf_ev->nwk_wqe.func = intf_event_callback;
5191 p_intf_ev->nwk_wqe.is_arg_managed = TRUE;
5192 p_intf_ev->nwk_wqe.arg = &p_intf_ev->intf_ev_arg;
5193 nwk_wq_enqueue((struct nwk_wq_entry*)p_intf_ev);
5194 }