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