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