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