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