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