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