1 /*
2 * Copyright (c) 2004-2022 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include "kpi_interface.h"
30
31 #include <sys/queue.h>
32 #include <sys/param.h> /* for definition of NULL */
33 #include <kern/debug.h> /* for panic */
34 #include <sys/errno.h>
35 #include <sys/socket.h>
36 #include <sys/kern_event.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/kpi_mbuf.h>
40 #include <sys/mcache.h>
41 #include <sys/protosw.h>
42 #include <sys/syslog.h>
43 #include <net/if_var.h>
44 #include <net/if_dl.h>
45 #include <net/dlil.h>
46 #include <net/if_types.h>
47 #include <net/if_dl.h>
48 #include <net/if_arp.h>
49 #include <net/if_llreach.h>
50 #include <net/if_ether.h>
51 #include <net/net_api_stats.h>
52 #include <net/route.h>
53 #include <net/if_ports_used.h>
54 #include <libkern/libkern.h>
55 #include <libkern/OSAtomic.h>
56 #include <kern/locks.h>
57 #include <kern/clock.h>
58 #include <sys/sockio.h>
59 #include <sys/proc.h>
60 #include <sys/sysctl.h>
61 #include <sys/mbuf.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/udp.h>
64 #include <netinet/udp_var.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/in_pcb.h>
68 #ifdef INET
69 #include <netinet/igmp_var.h>
70 #endif
71 #include <netinet6/mld6_var.h>
72 #include <netkey/key.h>
73 #include <stdbool.h>
74
75 #include "net/net_str_id.h"
76
77 #if CONFIG_MACF
78 #include <sys/kauth.h>
79 #include <security/mac_framework.h>
80 #endif
81
82 #if SKYWALK
83 #include <skywalk/os_skywalk_private.h>
84 #include <skywalk/nexus/netif/nx_netif.h>
85 #endif /* SKYWALK */
86
87 #undef ifnet_allocate
88 errno_t ifnet_allocate(const struct ifnet_init_params *init,
89 ifnet_t *ifp);
90
91 static errno_t ifnet_allocate_common(const struct ifnet_init_params *init,
92 ifnet_t *ifp, bool is_internal);
93
94
95 #define TOUCHLASTCHANGE(__if_lastchange) { \
96 (__if_lastchange)->tv_sec = (time_t)net_uptime(); \
97 (__if_lastchange)->tv_usec = 0; \
98 }
99
100 static errno_t ifnet_defrouter_llreachinfo(ifnet_t, sa_family_t,
101 struct ifnet_llreach_info *);
102 static void ifnet_kpi_free(ifnet_t);
103 static errno_t ifnet_list_get_common(ifnet_family_t, boolean_t, ifnet_t **,
104 u_int32_t *);
105 static errno_t ifnet_set_lladdr_internal(ifnet_t, const void *, size_t,
106 u_char, int);
107 static errno_t ifnet_awdl_check_eflags(ifnet_t, u_int32_t *, u_int32_t *);
108
109 /*
110 * Temporary work around until we have real reference counting
111 *
112 * We keep the bits about calling dlil_if_release (which should be
113 * called recycle) transparent by calling it from our if_free function
114 * pointer. We have to keep the client's original detach function
115 * somewhere so we can call it.
116 */
117 static void
ifnet_kpi_free(ifnet_t ifp)118 ifnet_kpi_free(ifnet_t ifp)
119 {
120 if ((ifp->if_refflags & IFRF_EMBRYONIC) == 0) {
121 ifnet_detached_func detach_func;
122
123 detach_func = ifp->if_detach;
124 if (detach_func != NULL) {
125 (*detach_func)(ifp);
126 }
127 }
128
129 ifnet_dispose(ifp);
130 }
131
132 errno_t
ifnet_allocate_common(const struct ifnet_init_params * init,ifnet_t * ifp,bool is_internal)133 ifnet_allocate_common(const struct ifnet_init_params *init,
134 ifnet_t *ifp, bool is_internal)
135 {
136 struct ifnet_init_eparams einit;
137
138 bzero(&einit, sizeof(einit));
139
140 einit.ver = IFNET_INIT_CURRENT_VERSION;
141 einit.len = sizeof(einit);
142 einit.flags = IFNET_INIT_LEGACY | IFNET_INIT_NX_NOAUTO;
143 if (!is_internal) {
144 einit.flags |= IFNET_INIT_ALLOC_KPI;
145 }
146 einit.uniqueid = init->uniqueid;
147 einit.uniqueid_len = init->uniqueid_len;
148 einit.name = init->name;
149 einit.unit = init->unit;
150 einit.family = init->family;
151 einit.type = init->type;
152 einit.output = init->output;
153 einit.demux = init->demux;
154 einit.add_proto = init->add_proto;
155 einit.del_proto = init->del_proto;
156 einit.check_multi = init->check_multi;
157 einit.framer = init->framer;
158 einit.softc = init->softc;
159 einit.ioctl = init->ioctl;
160 einit.set_bpf_tap = init->set_bpf_tap;
161 einit.detach = init->detach;
162 einit.event = init->event;
163 einit.broadcast_addr = init->broadcast_addr;
164 einit.broadcast_len = init->broadcast_len;
165
166 return ifnet_allocate_extended(&einit, ifp);
167 }
168
169 errno_t
ifnet_allocate_internal(const struct ifnet_init_params * init,ifnet_t * ifp)170 ifnet_allocate_internal(const struct ifnet_init_params *init, ifnet_t *ifp)
171 {
172 return ifnet_allocate_common(init, ifp, true);
173 }
174
175 errno_t
ifnet_allocate(const struct ifnet_init_params * init,ifnet_t * ifp)176 ifnet_allocate(const struct ifnet_init_params *init, ifnet_t *ifp)
177 {
178 return ifnet_allocate_common(init, ifp, false);
179 }
180
181 static void
ifnet_set_broadcast_addr(ifnet_t ifp,const void * broadcast_addr,u_int32_t broadcast_len)182 ifnet_set_broadcast_addr(ifnet_t ifp, const void * broadcast_addr,
183 u_int32_t broadcast_len)
184 {
185 if (broadcast_len == 0 || broadcast_addr == NULL) {
186 /* no broadcast address */
187 bzero(&ifp->if_broadcast, sizeof(ifp->if_broadcast));
188 } else if (broadcast_len > sizeof(ifp->if_broadcast.u.buffer)) {
189 ifp->if_broadcast.u.ptr
190 = (u_char *)kalloc_data(broadcast_len,
191 Z_WAITOK | Z_NOFAIL);
192 bcopy(broadcast_addr,
193 ifp->if_broadcast.u.ptr,
194 broadcast_len);
195 } else {
196 bcopy(broadcast_addr,
197 ifp->if_broadcast.u.buffer,
198 broadcast_len);
199 }
200 ifp->if_broadcast.length = broadcast_len;
201 }
202
203 errno_t
ifnet_allocate_extended(const struct ifnet_init_eparams * einit0,ifnet_t * interface)204 ifnet_allocate_extended(const struct ifnet_init_eparams *einit0,
205 ifnet_t *interface)
206 {
207 #if SKYWALK
208 ifnet_start_func ostart = NULL;
209 #endif /* SKYWALK */
210 struct ifnet_init_eparams einit;
211 struct ifnet *ifp = NULL;
212 char if_xname[IFXNAMSIZ] = {0};
213 int error;
214
215 einit = *einit0;
216
217 if (einit.ver != IFNET_INIT_CURRENT_VERSION ||
218 einit.len < sizeof(einit)) {
219 return EINVAL;
220 }
221
222 if (einit.family == 0 || einit.name == NULL ||
223 strlen(einit.name) >= IFNAMSIZ ||
224 (einit.type & 0xFFFFFF00) != 0 || einit.type == 0) {
225 return EINVAL;
226 }
227
228 #if SKYWALK
229 /* headroom must be a multiple of 8 bytes */
230 if ((einit.tx_headroom & 0x7) != 0) {
231 return EINVAL;
232 }
233 if ((einit.flags & IFNET_INIT_SKYWALK_NATIVE) == 0) {
234 /*
235 * Currently Interface advisory reporting is supported only
236 * for skywalk interface.
237 */
238 if ((einit.flags & IFNET_INIT_IF_ADV) != 0) {
239 return EINVAL;
240 }
241 }
242 #endif /* SKYWALK */
243
244 if (einit.flags & IFNET_INIT_LEGACY) {
245 #if SKYWALK
246 if (einit.flags & IFNET_INIT_SKYWALK_NATIVE) {
247 return EINVAL;
248 }
249 #endif /* SKYWALK */
250 if (einit.output == NULL ||
251 (einit.flags & IFNET_INIT_INPUT_POLL)) {
252 return EINVAL;
253 }
254 einit.pre_enqueue = NULL;
255 einit.start = NULL;
256 einit.output_ctl = NULL;
257 einit.output_sched_model = IFNET_SCHED_MODEL_NORMAL;
258 einit.input_poll = NULL;
259 einit.input_ctl = NULL;
260 } else {
261 #if SKYWALK
262 /*
263 * For native Skywalk drivers, steer all start requests
264 * to ifp_if_start() until the netif device adapter is
265 * fully activated, at which point we will point it to
266 * nx_netif_doorbell().
267 */
268 if (einit.flags & IFNET_INIT_SKYWALK_NATIVE) {
269 if (einit.start != NULL) {
270 return EINVAL;
271 }
272 /* override output start callback */
273 ostart = einit.start = ifp_if_start;
274 } else {
275 ostart = einit.start;
276 }
277 #endif /* SKYWALK */
278 if (einit.start == NULL) {
279 return EINVAL;
280 }
281
282 einit.output = NULL;
283 if (einit.output_sched_model >= IFNET_SCHED_MODEL_MAX) {
284 return EINVAL;
285 }
286
287 if (einit.flags & IFNET_INIT_INPUT_POLL) {
288 if (einit.input_poll == NULL || einit.input_ctl == NULL) {
289 return EINVAL;
290 }
291 } else {
292 einit.input_poll = NULL;
293 einit.input_ctl = NULL;
294 }
295 }
296
297 if (einit.type > UCHAR_MAX) {
298 return EINVAL;
299 }
300
301 if (einit.unit > SHRT_MAX) {
302 return EINVAL;
303 }
304
305 /* Initialize external name (name + unit) */
306 (void) snprintf(if_xname, sizeof(if_xname), "%s%d",
307 einit.name, einit.unit);
308
309 if (einit.uniqueid == NULL) {
310 einit.uniqueid = if_xname;
311 einit.uniqueid_len = (uint32_t)strlen(if_xname);
312 }
313
314 error = dlil_if_acquire(einit.family, einit.uniqueid,
315 einit.uniqueid_len, if_xname, &ifp);
316
317 if (error == 0) {
318 uint64_t br;
319
320 /*
321 * Cast ifp->if_name as non const. dlil_if_acquire sets it up
322 * to point to storage of at least IFNAMSIZ bytes. It is safe
323 * to write to this.
324 */
325 strlcpy(__DECONST(char *, ifp->if_name), einit.name, IFNAMSIZ);
326 ifp->if_type = (u_char)einit.type;
327 ifp->if_family = einit.family;
328 ifp->if_subfamily = einit.subfamily;
329 ifp->if_unit = (short)einit.unit;
330 ifp->if_output = einit.output;
331 ifp->if_pre_enqueue = einit.pre_enqueue;
332 ifp->if_start = einit.start;
333 ifp->if_output_ctl = einit.output_ctl;
334 ifp->if_output_sched_model = einit.output_sched_model;
335 ifp->if_output_bw.eff_bw = einit.output_bw;
336 ifp->if_output_bw.max_bw = einit.output_bw_max;
337 ifp->if_output_lt.eff_lt = einit.output_lt;
338 ifp->if_output_lt.max_lt = einit.output_lt_max;
339 ifp->if_input_poll = einit.input_poll;
340 ifp->if_input_ctl = einit.input_ctl;
341 ifp->if_input_bw.eff_bw = einit.input_bw;
342 ifp->if_input_bw.max_bw = einit.input_bw_max;
343 ifp->if_input_lt.eff_lt = einit.input_lt;
344 ifp->if_input_lt.max_lt = einit.input_lt_max;
345 ifp->if_demux = einit.demux;
346 ifp->if_add_proto = einit.add_proto;
347 ifp->if_del_proto = einit.del_proto;
348 ifp->if_check_multi = einit.check_multi;
349 ifp->if_framer_legacy = einit.framer;
350 ifp->if_framer = einit.framer_extended;
351 ifp->if_softc = einit.softc;
352 ifp->if_ioctl = einit.ioctl;
353 ifp->if_set_bpf_tap = einit.set_bpf_tap;
354 ifp->if_free = (einit.free != NULL) ? einit.free : ifnet_kpi_free;
355 ifp->if_event = einit.event;
356 ifp->if_detach = einit.detach;
357
358 /* Initialize Network ID */
359 ifp->network_id_len = 0;
360 bzero(&ifp->network_id, sizeof(ifp->network_id));
361
362 /* Initialize external name (name + unit) */
363 snprintf(__DECONST(char *, ifp->if_xname), IFXNAMSIZ,
364 "%s", if_xname);
365
366 /*
367 * On embedded, framer() is already in the extended form;
368 * we simply use it as is, unless the caller specifies
369 * framer_extended() which will then override it.
370 *
371 * On non-embedded, framer() has long been exposed as part
372 * of the public KPI, and therefore its signature must
373 * remain the same (without the pre- and postpend length
374 * parameters.) We special case ether_frameout, such that
375 * it gets mapped to its extended variant. All other cases
376 * utilize the stub routine which will simply return zeroes
377 * for those new parameters.
378 *
379 * Internally, DLIL will only use the extended callback
380 * variant which is represented by if_framer.
381 */
382 #if !XNU_TARGET_OS_OSX
383 if (ifp->if_framer == NULL && ifp->if_framer_legacy != NULL) {
384 ifp->if_framer = ifp->if_framer_legacy;
385 }
386 #else /* XNU_TARGET_OS_OSX */
387 if (ifp->if_framer == NULL && ifp->if_framer_legacy != NULL) {
388 if (ifp->if_framer_legacy == ether_frameout) {
389 ifp->if_framer = ether_frameout_extended;
390 } else {
391 ifp->if_framer = ifnet_framer_stub;
392 }
393 }
394 #endif /* XNU_TARGET_OS_OSX */
395
396 if (ifp->if_output_bw.eff_bw > ifp->if_output_bw.max_bw) {
397 ifp->if_output_bw.max_bw = ifp->if_output_bw.eff_bw;
398 } else if (ifp->if_output_bw.eff_bw == 0) {
399 ifp->if_output_bw.eff_bw = ifp->if_output_bw.max_bw;
400 }
401
402 if (ifp->if_input_bw.eff_bw > ifp->if_input_bw.max_bw) {
403 ifp->if_input_bw.max_bw = ifp->if_input_bw.eff_bw;
404 } else if (ifp->if_input_bw.eff_bw == 0) {
405 ifp->if_input_bw.eff_bw = ifp->if_input_bw.max_bw;
406 }
407
408 if (ifp->if_output_bw.max_bw == 0) {
409 ifp->if_output_bw = ifp->if_input_bw;
410 } else if (ifp->if_input_bw.max_bw == 0) {
411 ifp->if_input_bw = ifp->if_output_bw;
412 }
413
414 /* Pin if_baudrate to 32 bits */
415 br = MAX(ifp->if_output_bw.max_bw, ifp->if_input_bw.max_bw);
416 if (br != 0) {
417 ifp->if_baudrate = (br > UINT32_MAX) ? UINT32_MAX : (uint32_t)br;
418 }
419
420 if (ifp->if_output_lt.eff_lt > ifp->if_output_lt.max_lt) {
421 ifp->if_output_lt.max_lt = ifp->if_output_lt.eff_lt;
422 } else if (ifp->if_output_lt.eff_lt == 0) {
423 ifp->if_output_lt.eff_lt = ifp->if_output_lt.max_lt;
424 }
425
426 if (ifp->if_input_lt.eff_lt > ifp->if_input_lt.max_lt) {
427 ifp->if_input_lt.max_lt = ifp->if_input_lt.eff_lt;
428 } else if (ifp->if_input_lt.eff_lt == 0) {
429 ifp->if_input_lt.eff_lt = ifp->if_input_lt.max_lt;
430 }
431
432 if (ifp->if_output_lt.max_lt == 0) {
433 ifp->if_output_lt = ifp->if_input_lt;
434 } else if (ifp->if_input_lt.max_lt == 0) {
435 ifp->if_input_lt = ifp->if_output_lt;
436 }
437
438 if (ifp->if_ioctl == NULL) {
439 ifp->if_ioctl = ifp_if_ioctl;
440 }
441
442 if_clear_eflags(ifp, -1);
443 if (ifp->if_start != NULL) {
444 if_set_eflags(ifp, IFEF_TXSTART);
445 if (ifp->if_pre_enqueue == NULL) {
446 ifp->if_pre_enqueue = ifnet_enqueue;
447 }
448 ifp->if_output = ifp->if_pre_enqueue;
449 }
450
451 if (ifp->if_input_poll != NULL) {
452 if_set_eflags(ifp, IFEF_RXPOLL);
453 }
454
455 ifp->if_output_dlil = dlil_output_handler;
456 ifp->if_input_dlil = dlil_input_handler;
457
458 VERIFY(!(einit.flags & IFNET_INIT_LEGACY) ||
459 (ifp->if_pre_enqueue == NULL && ifp->if_start == NULL &&
460 ifp->if_output_ctl == NULL && ifp->if_input_poll == NULL &&
461 ifp->if_input_ctl == NULL));
462 VERIFY(!(einit.flags & IFNET_INIT_INPUT_POLL) ||
463 (ifp->if_input_poll != NULL && ifp->if_input_ctl != NULL));
464
465 ifnet_set_broadcast_addr(ifp, einit.broadcast_addr,
466 einit.broadcast_len);
467
468 if_clear_xflags(ifp, -1);
469 #if SKYWALK
470 ifp->if_tx_headroom = 0;
471 ifp->if_tx_trailer = 0;
472 ifp->if_rx_mit_ival = 0;
473 ifp->if_save_start = ostart;
474 if (einit.flags & IFNET_INIT_SKYWALK_NATIVE) {
475 VERIFY(ifp->if_eflags & IFEF_TXSTART);
476 VERIFY(!(einit.flags & IFNET_INIT_LEGACY));
477 if_set_eflags(ifp, IFEF_SKYWALK_NATIVE);
478 ifp->if_tx_headroom = einit.tx_headroom;
479 ifp->if_tx_trailer = einit.tx_trailer;
480 ifp->if_rx_mit_ival = einit.rx_mit_ival;
481 /*
482 * For native Skywalk drivers, make sure packets
483 * emitted by the BSD stack get dropped until the
484 * interface is in service. When the netif host
485 * adapter is fully activated, we'll point it to
486 * nx_netif_output().
487 */
488 ifp->if_output = ifp_if_output;
489 /*
490 * Override driver-supplied parameters
491 * and force IFEF_ENQUEUE_MULTI?
492 */
493 if (sk_netif_native_txmodel ==
494 NETIF_NATIVE_TXMODEL_ENQUEUE_MULTI) {
495 einit.start_delay_qlen = sk_tx_delay_qlen;
496 einit.start_delay_timeout = sk_tx_delay_timeout;
497 }
498 /* netif comes with native interfaces */
499 VERIFY((ifp->if_xflags & IFXF_LEGACY) == 0);
500 } else if (!ifnet_needs_compat(ifp)) {
501 /*
502 * If we're told not to plumb in netif compat
503 * for this interface, set IFXF_NX_NOAUTO to
504 * prevent DLIL from auto-attaching the nexus.
505 */
506 einit.flags |= IFNET_INIT_NX_NOAUTO;
507 /* legacy (non-netif) interface */
508 if_set_xflags(ifp, IFXF_LEGACY);
509 }
510
511 ifp->if_save_output = ifp->if_output;
512 if ((einit.flags & IFNET_INIT_NX_NOAUTO) != 0) {
513 if_set_xflags(ifp, IFXF_NX_NOAUTO);
514 }
515 if ((einit.flags & IFNET_INIT_IF_ADV) != 0) {
516 if_set_eflags(ifp, IFEF_ADV_REPORT);
517 }
518 #else /* !SKYWALK */
519 /* legacy interface */
520 if_set_xflags(ifp, IFXF_LEGACY);
521 #endif /* !SKYWALK */
522
523 if ((ifp->if_snd = ifclassq_alloc()) == NULL) {
524 panic_plain("%s: ifp=%p couldn't allocate class queues",
525 __func__, ifp);
526 /* NOTREACHED */
527 }
528
529 /*
530 * output target queue delay is specified in millisecond
531 * convert it to nanoseconds
532 */
533 IFCQ_TARGET_QDELAY(ifp->if_snd) =
534 einit.output_target_qdelay * 1000 * 1000;
535 IFCQ_MAXLEN(ifp->if_snd) = einit.sndq_maxlen;
536
537 ifnet_enqueue_multi_setup(ifp, einit.start_delay_qlen,
538 einit.start_delay_timeout);
539
540 IFCQ_PKT_DROP_LIMIT(ifp->if_snd) = IFCQ_DEFAULT_PKT_DROP_LIMIT;
541
542 /*
543 * Set embryonic flag; this will be cleared
544 * later when it is fully attached.
545 */
546 ifp->if_refflags = IFRF_EMBRYONIC;
547
548 /*
549 * Count the newly allocated ifnet
550 */
551 OSIncrementAtomic64(&net_api_stats.nas_ifnet_alloc_count);
552 INC_ATOMIC_INT64_LIM(net_api_stats.nas_ifnet_alloc_total);
553 if ((einit.flags & IFNET_INIT_ALLOC_KPI) != 0) {
554 if_set_xflags(ifp, IFXF_ALLOC_KPI);
555 } else {
556 OSIncrementAtomic64(
557 &net_api_stats.nas_ifnet_alloc_os_count);
558 INC_ATOMIC_INT64_LIM(
559 net_api_stats.nas_ifnet_alloc_os_total);
560 }
561
562 if (ifp->if_subfamily == IFNET_SUBFAMILY_MANAGEMENT) {
563 if_set_xflags(ifp, IFXF_MANAGEMENT);
564 if_management_interface_check_needed = true;
565 }
566
567 *interface = ifp;
568 }
569 return error;
570 }
571
572 errno_t
ifnet_reference(ifnet_t ifp)573 ifnet_reference(ifnet_t ifp)
574 {
575 return dlil_if_ref(ifp);
576 }
577
578 void
ifnet_dispose(ifnet_t ifp)579 ifnet_dispose(ifnet_t ifp)
580 {
581 dlil_if_release(ifp);
582 }
583
584 errno_t
ifnet_release(ifnet_t ifp)585 ifnet_release(ifnet_t ifp)
586 {
587 return dlil_if_free(ifp);
588 }
589
590 errno_t
ifnet_interface_family_find(const char * module_string,ifnet_family_t * family_id)591 ifnet_interface_family_find(const char *module_string,
592 ifnet_family_t *family_id)
593 {
594 if (module_string == NULL || family_id == NULL) {
595 return EINVAL;
596 }
597
598 return net_str_id_find_internal(module_string, family_id,
599 NSI_IF_FAM_ID, 1);
600 }
601
602 void *
ifnet_softc(ifnet_t interface)603 ifnet_softc(ifnet_t interface)
604 {
605 return (interface == NULL) ? NULL : interface->if_softc;
606 }
607
608 const char *
ifnet_name(ifnet_t interface)609 ifnet_name(ifnet_t interface)
610 {
611 return (interface == NULL) ? NULL : interface->if_name;
612 }
613
614 ifnet_family_t
ifnet_family(ifnet_t interface)615 ifnet_family(ifnet_t interface)
616 {
617 return (interface == NULL) ? 0 : interface->if_family;
618 }
619
620 ifnet_subfamily_t
ifnet_subfamily(ifnet_t interface)621 ifnet_subfamily(ifnet_t interface)
622 {
623 return (interface == NULL) ? 0 : interface->if_subfamily;
624 }
625
626 u_int32_t
ifnet_unit(ifnet_t interface)627 ifnet_unit(ifnet_t interface)
628 {
629 return (interface == NULL) ? (u_int32_t)0xffffffff :
630 (u_int32_t)interface->if_unit;
631 }
632
633 u_int32_t
ifnet_index(ifnet_t interface)634 ifnet_index(ifnet_t interface)
635 {
636 return (interface == NULL) ? (u_int32_t)0xffffffff :
637 interface->if_index;
638 }
639
640 errno_t
ifnet_set_flags(ifnet_t interface,u_int16_t new_flags,u_int16_t mask)641 ifnet_set_flags(ifnet_t interface, u_int16_t new_flags, u_int16_t mask)
642 {
643 bool set_IFF_UP;
644 bool change_IFF_UP;
645 uint16_t old_flags;
646
647 if (interface == NULL) {
648 return EINVAL;
649 }
650 set_IFF_UP = (new_flags & IFF_UP) != 0;
651 change_IFF_UP = (mask & IFF_UP) != 0;
652 #if SKYWALK
653 if (set_IFF_UP && change_IFF_UP) {
654 /*
655 * When a native skywalk interface is marked IFF_UP, ensure
656 * the flowswitch is attached.
657 */
658 ifnet_attach_native_flowswitch(interface);
659 }
660 #endif /* SKYWALK */
661
662 ifnet_lock_exclusive(interface);
663
664 /* If we are modifying the up/down state, call if_updown */
665 if (change_IFF_UP) {
666 if_updown(interface, set_IFF_UP);
667 }
668
669 old_flags = interface->if_flags;
670 interface->if_flags = (new_flags & mask) | (interface->if_flags & ~mask);
671 /* If we are modifying the multicast flag, set/unset the silent flag */
672 if ((old_flags & IFF_MULTICAST) !=
673 (interface->if_flags & IFF_MULTICAST)) {
674 #if INET
675 if (IGMP_IFINFO(interface) != NULL) {
676 igmp_initsilent(interface, IGMP_IFINFO(interface));
677 }
678 #endif /* INET */
679 if (MLD_IFINFO(interface) != NULL) {
680 mld6_initsilent(interface, MLD_IFINFO(interface));
681 }
682 }
683
684 ifnet_lock_done(interface);
685
686 return 0;
687 }
688
689 u_int16_t
ifnet_flags(ifnet_t interface)690 ifnet_flags(ifnet_t interface)
691 {
692 return (interface == NULL) ? 0 : interface->if_flags;
693 }
694
695 /*
696 * This routine ensures the following:
697 *
698 * If IFEF_AWDL is set by the caller, also set the rest of flags as
699 * defined in IFEF_AWDL_MASK.
700 *
701 * If IFEF_AWDL has been set on the interface and the caller attempts
702 * to clear one or more of the associated flags in IFEF_AWDL_MASK,
703 * return failure.
704 *
705 * If IFEF_AWDL_RESTRICTED is set by the caller, make sure IFEF_AWDL is set
706 * on the interface.
707 *
708 * All other flags not associated with AWDL are not affected.
709 *
710 * See <net/if.h> for current definition of IFEF_AWDL_MASK.
711 */
712 static errno_t
ifnet_awdl_check_eflags(ifnet_t ifp,u_int32_t * new_eflags,u_int32_t * mask)713 ifnet_awdl_check_eflags(ifnet_t ifp, u_int32_t *new_eflags, u_int32_t *mask)
714 {
715 u_int32_t eflags;
716
717 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
718
719 eflags = (*new_eflags & *mask) | (ifp->if_eflags & ~(*mask));
720
721 if (ifp->if_eflags & IFEF_AWDL) {
722 if (eflags & IFEF_AWDL) {
723 if ((eflags & IFEF_AWDL_MASK) != IFEF_AWDL_MASK) {
724 return EINVAL;
725 }
726 } else {
727 *new_eflags &= ~IFEF_AWDL_MASK;
728 *mask |= IFEF_AWDL_MASK;
729 }
730 } else if (eflags & IFEF_AWDL) {
731 *new_eflags |= IFEF_AWDL_MASK;
732 *mask |= IFEF_AWDL_MASK;
733 } else if (eflags & IFEF_AWDL_RESTRICTED &&
734 !(ifp->if_eflags & IFEF_AWDL)) {
735 return EINVAL;
736 }
737
738 return 0;
739 }
740
741 errno_t
ifnet_set_eflags(ifnet_t interface,u_int32_t new_flags,u_int32_t mask)742 ifnet_set_eflags(ifnet_t interface, u_int32_t new_flags, u_int32_t mask)
743 {
744 uint32_t oeflags;
745 struct kev_msg ev_msg;
746 struct net_event_data ev_data;
747
748 if (interface == NULL) {
749 return EINVAL;
750 }
751
752 bzero(&ev_msg, sizeof(ev_msg));
753 ifnet_lock_exclusive(interface);
754 /*
755 * Sanity checks for IFEF_AWDL and its related flags.
756 */
757 if (ifnet_awdl_check_eflags(interface, &new_flags, &mask) != 0) {
758 ifnet_lock_done(interface);
759 return EINVAL;
760 }
761 /*
762 * Currently Interface advisory reporting is supported only for
763 * skywalk interface.
764 */
765 if ((((new_flags & mask) & IFEF_ADV_REPORT) != 0) &&
766 ((interface->if_eflags & IFEF_SKYWALK_NATIVE) == 0)) {
767 ifnet_lock_done(interface);
768 return EINVAL;
769 }
770 oeflags = interface->if_eflags;
771 if_clear_eflags(interface, mask);
772 if (new_flags != 0) {
773 if_set_eflags(interface, (new_flags & mask));
774 }
775 ifnet_lock_done(interface);
776 if (interface->if_eflags & IFEF_AWDL_RESTRICTED &&
777 !(oeflags & IFEF_AWDL_RESTRICTED)) {
778 ev_msg.event_code = KEV_DL_AWDL_RESTRICTED;
779 /*
780 * The interface is now restricted to applications that have
781 * the entitlement.
782 * The check for the entitlement will be done in the data
783 * path, so we don't have to do anything here.
784 */
785 } else if (oeflags & IFEF_AWDL_RESTRICTED &&
786 !(interface->if_eflags & IFEF_AWDL_RESTRICTED)) {
787 ev_msg.event_code = KEV_DL_AWDL_UNRESTRICTED;
788 }
789 /*
790 * Notify configd so that it has a chance to perform better
791 * reachability detection.
792 */
793 if (ev_msg.event_code) {
794 bzero(&ev_data, sizeof(ev_data));
795 ev_msg.vendor_code = KEV_VENDOR_APPLE;
796 ev_msg.kev_class = KEV_NETWORK_CLASS;
797 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
798 strlcpy(ev_data.if_name, interface->if_name, IFNAMSIZ);
799 ev_data.if_family = interface->if_family;
800 ev_data.if_unit = interface->if_unit;
801 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
802 ev_msg.dv[0].data_ptr = &ev_data;
803 ev_msg.dv[1].data_length = 0;
804 dlil_post_complete_msg(interface, &ev_msg);
805 }
806
807 return 0;
808 }
809
810 u_int32_t
ifnet_eflags(ifnet_t interface)811 ifnet_eflags(ifnet_t interface)
812 {
813 return (interface == NULL) ? 0 : interface->if_eflags;
814 }
815
816 errno_t
ifnet_set_idle_flags_locked(ifnet_t ifp,u_int32_t new_flags,u_int32_t mask)817 ifnet_set_idle_flags_locked(ifnet_t ifp, u_int32_t new_flags, u_int32_t mask)
818 {
819 if (ifp == NULL) {
820 return EINVAL;
821 }
822 ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE);
823
824 /*
825 * If this is called prior to ifnet attach, the actual work will
826 * be done at attach time. Otherwise, if it is called after
827 * ifnet detach, then it is a no-op.
828 */
829 if (!ifnet_is_attached(ifp, 0)) {
830 ifp->if_idle_new_flags = new_flags;
831 ifp->if_idle_new_flags_mask = mask;
832 return 0;
833 } else {
834 ifp->if_idle_new_flags = ifp->if_idle_new_flags_mask = 0;
835 }
836
837 ifp->if_idle_flags = (new_flags & mask) | (ifp->if_idle_flags & ~mask);
838 return 0;
839 }
840
841 errno_t
ifnet_set_idle_flags(ifnet_t ifp,u_int32_t new_flags,u_int32_t mask)842 ifnet_set_idle_flags(ifnet_t ifp, u_int32_t new_flags, u_int32_t mask)
843 {
844 errno_t err;
845
846 ifnet_lock_exclusive(ifp);
847 err = ifnet_set_idle_flags_locked(ifp, new_flags, mask);
848 ifnet_lock_done(ifp);
849
850 return err;
851 }
852
853 u_int32_t
ifnet_idle_flags(ifnet_t ifp)854 ifnet_idle_flags(ifnet_t ifp)
855 {
856 return (ifp == NULL) ? 0 : ifp->if_idle_flags;
857 }
858
859 errno_t
ifnet_set_link_quality(ifnet_t ifp,int quality)860 ifnet_set_link_quality(ifnet_t ifp, int quality)
861 {
862 errno_t err = 0;
863
864 if (ifp == NULL || quality < IFNET_LQM_MIN || quality > IFNET_LQM_MAX) {
865 err = EINVAL;
866 goto done;
867 }
868
869 if (!ifnet_is_attached(ifp, 0)) {
870 err = ENXIO;
871 goto done;
872 }
873
874 if_lqm_update(ifp, quality, 0);
875
876 done:
877 return err;
878 }
879
880 int
ifnet_link_quality(ifnet_t ifp)881 ifnet_link_quality(ifnet_t ifp)
882 {
883 int lqm;
884
885 if (ifp == NULL) {
886 return IFNET_LQM_THRESH_OFF;
887 }
888
889 ifnet_lock_shared(ifp);
890 lqm = ifp->if_interface_state.lqm_state;
891 ifnet_lock_done(ifp);
892
893 return lqm;
894 }
895
896 errno_t
ifnet_set_interface_state(ifnet_t ifp,struct if_interface_state * if_interface_state)897 ifnet_set_interface_state(ifnet_t ifp,
898 struct if_interface_state *if_interface_state)
899 {
900 errno_t err = 0;
901
902 if (ifp == NULL || if_interface_state == NULL) {
903 err = EINVAL;
904 goto done;
905 }
906
907 if (!ifnet_is_attached(ifp, 0)) {
908 err = ENXIO;
909 goto done;
910 }
911
912 if_state_update(ifp, if_interface_state);
913
914 done:
915 return err;
916 }
917
918 errno_t
ifnet_get_interface_state(ifnet_t ifp,struct if_interface_state * if_interface_state)919 ifnet_get_interface_state(ifnet_t ifp,
920 struct if_interface_state *if_interface_state)
921 {
922 errno_t err = 0;
923
924 if (ifp == NULL || if_interface_state == NULL) {
925 err = EINVAL;
926 goto done;
927 }
928
929 if (!ifnet_is_attached(ifp, 0)) {
930 err = ENXIO;
931 goto done;
932 }
933
934 if_get_state(ifp, if_interface_state);
935
936 done:
937 return err;
938 }
939
940
941 static errno_t
ifnet_defrouter_llreachinfo(ifnet_t ifp,sa_family_t af,struct ifnet_llreach_info * iflri)942 ifnet_defrouter_llreachinfo(ifnet_t ifp, sa_family_t af,
943 struct ifnet_llreach_info *iflri)
944 {
945 if (ifp == NULL || iflri == NULL) {
946 return EINVAL;
947 }
948
949 VERIFY(af == AF_INET || af == AF_INET6);
950
951 return ifnet_llreach_get_defrouter(ifp, af, iflri);
952 }
953
954 errno_t
ifnet_inet_defrouter_llreachinfo(ifnet_t ifp,struct ifnet_llreach_info * iflri)955 ifnet_inet_defrouter_llreachinfo(ifnet_t ifp, struct ifnet_llreach_info *iflri)
956 {
957 return ifnet_defrouter_llreachinfo(ifp, AF_INET, iflri);
958 }
959
960 errno_t
ifnet_inet6_defrouter_llreachinfo(ifnet_t ifp,struct ifnet_llreach_info * iflri)961 ifnet_inet6_defrouter_llreachinfo(ifnet_t ifp, struct ifnet_llreach_info *iflri)
962 {
963 return ifnet_defrouter_llreachinfo(ifp, AF_INET6, iflri);
964 }
965
966 errno_t
ifnet_set_capabilities_supported(ifnet_t ifp,u_int32_t new_caps,u_int32_t mask)967 ifnet_set_capabilities_supported(ifnet_t ifp, u_int32_t new_caps,
968 u_int32_t mask)
969 {
970 errno_t error = 0;
971 int tmp;
972
973 if (ifp == NULL) {
974 return EINVAL;
975 }
976
977 ifnet_lock_exclusive(ifp);
978 tmp = (new_caps & mask) | (ifp->if_capabilities & ~mask);
979 if ((tmp & ~IFCAP_VALID)) {
980 error = EINVAL;
981 } else {
982 ifp->if_capabilities = tmp;
983 }
984 ifnet_lock_done(ifp);
985
986 return error;
987 }
988
989 u_int32_t
ifnet_capabilities_supported(ifnet_t ifp)990 ifnet_capabilities_supported(ifnet_t ifp)
991 {
992 return (ifp == NULL) ? 0 : ifp->if_capabilities;
993 }
994
995
996 errno_t
ifnet_set_capabilities_enabled(ifnet_t ifp,u_int32_t new_caps,u_int32_t mask)997 ifnet_set_capabilities_enabled(ifnet_t ifp, u_int32_t new_caps,
998 u_int32_t mask)
999 {
1000 errno_t error = 0;
1001 int tmp;
1002 struct kev_msg ev_msg;
1003 struct net_event_data ev_data;
1004
1005 if (ifp == NULL) {
1006 return EINVAL;
1007 }
1008
1009 ifnet_lock_exclusive(ifp);
1010 tmp = (new_caps & mask) | (ifp->if_capenable & ~mask);
1011 if ((tmp & ~IFCAP_VALID) || (tmp & ~ifp->if_capabilities)) {
1012 error = EINVAL;
1013 } else {
1014 ifp->if_capenable = tmp;
1015 }
1016 ifnet_lock_done(ifp);
1017
1018 /* Notify application of the change */
1019 bzero(&ev_data, sizeof(struct net_event_data));
1020 bzero(&ev_msg, sizeof(struct kev_msg));
1021 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1022 ev_msg.kev_class = KEV_NETWORK_CLASS;
1023 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
1024
1025 ev_msg.event_code = KEV_DL_IFCAP_CHANGED;
1026 strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
1027 ev_data.if_family = ifp->if_family;
1028 ev_data.if_unit = (u_int32_t)ifp->if_unit;
1029 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
1030 ev_msg.dv[0].data_ptr = &ev_data;
1031 ev_msg.dv[1].data_length = 0;
1032 dlil_post_complete_msg(ifp, &ev_msg);
1033
1034 return error;
1035 }
1036
1037 u_int32_t
ifnet_capabilities_enabled(ifnet_t ifp)1038 ifnet_capabilities_enabled(ifnet_t ifp)
1039 {
1040 return (ifp == NULL) ? 0 : ifp->if_capenable;
1041 }
1042
1043 static const ifnet_offload_t offload_mask =
1044 (IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP | IFNET_CSUM_FRAGMENT |
1045 IFNET_IP_FRAGMENT | IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6 |
1046 IFNET_IPV6_FRAGMENT | IFNET_CSUM_PARTIAL | IFNET_CSUM_ZERO_INVERT |
1047 IFNET_VLAN_TAGGING | IFNET_VLAN_MTU | IFNET_MULTIPAGES |
1048 IFNET_TSO_IPV4 | IFNET_TSO_IPV6 | IFNET_TX_STATUS | IFNET_HW_TIMESTAMP |
1049 IFNET_SW_TIMESTAMP);
1050
1051 static const ifnet_offload_t any_offload_csum = IFNET_CHECKSUMF;
1052
1053 errno_t
ifnet_set_offload(ifnet_t interface,ifnet_offload_t offload)1054 ifnet_set_offload(ifnet_t interface, ifnet_offload_t offload)
1055 {
1056 u_int32_t ifcaps = 0;
1057
1058 if (interface == NULL) {
1059 return EINVAL;
1060 }
1061
1062 ifnet_lock_exclusive(interface);
1063 interface->if_hwassist = (offload & offload_mask);
1064
1065 #if SKYWALK
1066 /* preserve skywalk capability */
1067 if ((interface->if_capabilities & IFCAP_SKYWALK) != 0) {
1068 ifcaps |= IFCAP_SKYWALK;
1069 }
1070 #endif /* SKYWALK */
1071 /*
1072 * Hardware capable of partial checksum offload is
1073 * flexible enough to handle any transports utilizing
1074 * Internet Checksumming. Include those transports
1075 * here, and leave the final decision to IP.
1076 */
1077 if (interface->if_hwassist & IFNET_CSUM_PARTIAL) {
1078 interface->if_hwassist |= (IFNET_CSUM_TCP | IFNET_CSUM_UDP |
1079 IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6);
1080 }
1081 if (dlil_verbose) {
1082 log(LOG_DEBUG, "%s: set offload flags=%b\n",
1083 if_name(interface),
1084 interface->if_hwassist, IFNET_OFFLOADF_BITS);
1085 }
1086 ifnet_lock_done(interface);
1087
1088 if ((offload & any_offload_csum)) {
1089 ifcaps |= IFCAP_HWCSUM;
1090 }
1091 if ((offload & IFNET_TSO_IPV4)) {
1092 ifcaps |= IFCAP_TSO4;
1093 }
1094 if ((offload & IFNET_TSO_IPV6)) {
1095 ifcaps |= IFCAP_TSO6;
1096 }
1097 if ((offload & IFNET_LRO)) {
1098 ifcaps |= IFCAP_LRO;
1099 }
1100 if ((offload & IFNET_VLAN_MTU)) {
1101 ifcaps |= IFCAP_VLAN_MTU;
1102 }
1103 if ((offload & IFNET_VLAN_TAGGING)) {
1104 ifcaps |= IFCAP_VLAN_HWTAGGING;
1105 }
1106 if ((offload & IFNET_TX_STATUS)) {
1107 ifcaps |= IFCAP_TXSTATUS;
1108 }
1109 if ((offload & IFNET_HW_TIMESTAMP)) {
1110 ifcaps |= IFCAP_HW_TIMESTAMP;
1111 }
1112 if ((offload & IFNET_SW_TIMESTAMP)) {
1113 ifcaps |= IFCAP_SW_TIMESTAMP;
1114 }
1115 if ((offload & IFNET_CSUM_PARTIAL)) {
1116 ifcaps |= IFCAP_CSUM_PARTIAL;
1117 }
1118 if ((offload & IFNET_CSUM_ZERO_INVERT)) {
1119 ifcaps |= IFCAP_CSUM_ZERO_INVERT;
1120 }
1121 if (ifcaps != 0) {
1122 (void) ifnet_set_capabilities_supported(interface, ifcaps,
1123 IFCAP_VALID);
1124 (void) ifnet_set_capabilities_enabled(interface, ifcaps,
1125 IFCAP_VALID);
1126 }
1127
1128 return 0;
1129 }
1130
1131 ifnet_offload_t
ifnet_offload(ifnet_t interface)1132 ifnet_offload(ifnet_t interface)
1133 {
1134 return (interface == NULL) ?
1135 0 : (interface->if_hwassist & offload_mask);
1136 }
1137
1138 errno_t
ifnet_set_tso_mtu(ifnet_t interface,sa_family_t family,u_int32_t mtuLen)1139 ifnet_set_tso_mtu(ifnet_t interface, sa_family_t family, u_int32_t mtuLen)
1140 {
1141 errno_t error = 0;
1142
1143 if (interface == NULL || mtuLen < interface->if_mtu) {
1144 return EINVAL;
1145 }
1146 if (mtuLen > IP_MAXPACKET) {
1147 return EINVAL;
1148 }
1149
1150 switch (family) {
1151 case AF_INET:
1152 if (interface->if_hwassist & IFNET_TSO_IPV4) {
1153 interface->if_tso_v4_mtu = mtuLen;
1154 } else {
1155 error = EINVAL;
1156 }
1157 break;
1158
1159 case AF_INET6:
1160 if (interface->if_hwassist & IFNET_TSO_IPV6) {
1161 interface->if_tso_v6_mtu = mtuLen;
1162 } else {
1163 error = EINVAL;
1164 }
1165 break;
1166
1167 default:
1168 error = EPROTONOSUPPORT;
1169 break;
1170 }
1171
1172 if (error == 0) {
1173 struct ifclassq *ifq = interface->if_snd;
1174 ASSERT(ifq != NULL);
1175 /* Inform all transmit queues about the new TSO MTU */
1176 IFCQ_LOCK(ifq);
1177 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
1178 IFCQ_UNLOCK(ifq);
1179 }
1180
1181 return error;
1182 }
1183
1184 errno_t
ifnet_get_tso_mtu(ifnet_t interface,sa_family_t family,u_int32_t * mtuLen)1185 ifnet_get_tso_mtu(ifnet_t interface, sa_family_t family, u_int32_t *mtuLen)
1186 {
1187 errno_t error = 0;
1188
1189 if (interface == NULL || mtuLen == NULL) {
1190 return EINVAL;
1191 }
1192
1193 switch (family) {
1194 case AF_INET:
1195 if (interface->if_hwassist & IFNET_TSO_IPV4) {
1196 *mtuLen = interface->if_tso_v4_mtu;
1197 } else {
1198 error = EINVAL;
1199 }
1200 break;
1201
1202 case AF_INET6:
1203 if (interface->if_hwassist & IFNET_TSO_IPV6) {
1204 *mtuLen = interface->if_tso_v6_mtu;
1205 } else {
1206 error = EINVAL;
1207 }
1208 break;
1209
1210 default:
1211 error = EPROTONOSUPPORT;
1212 break;
1213 }
1214
1215 return error;
1216 }
1217
1218 errno_t
ifnet_set_wake_flags(ifnet_t interface,u_int32_t properties,u_int32_t mask)1219 ifnet_set_wake_flags(ifnet_t interface, u_int32_t properties, u_int32_t mask)
1220 {
1221 struct kev_msg ev_msg;
1222 struct net_event_data ev_data;
1223
1224 bzero(&ev_data, sizeof(struct net_event_data));
1225 bzero(&ev_msg, sizeof(struct kev_msg));
1226
1227 if (interface == NULL) {
1228 return EINVAL;
1229 }
1230
1231 /* Do not accept wacky values */
1232 if ((properties & mask) & ~IF_WAKE_VALID_FLAGS) {
1233 return EINVAL;
1234 }
1235
1236 if ((mask & IF_WAKE_ON_MAGIC_PACKET) != 0) {
1237 if ((properties & IF_WAKE_ON_MAGIC_PACKET) != 0) {
1238 if_set_xflags(interface, IFXF_WAKE_ON_MAGIC_PACKET);
1239 } else {
1240 if_clear_xflags(interface, IFXF_WAKE_ON_MAGIC_PACKET);
1241 }
1242 }
1243
1244 (void) ifnet_touch_lastchange(interface);
1245
1246 /* Notify application of the change */
1247 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1248 ev_msg.kev_class = KEV_NETWORK_CLASS;
1249 ev_msg.kev_subclass = KEV_DL_SUBCLASS;
1250
1251 ev_msg.event_code = KEV_DL_WAKEFLAGS_CHANGED;
1252 strlcpy(&ev_data.if_name[0], interface->if_name, IFNAMSIZ);
1253 ev_data.if_family = interface->if_family;
1254 ev_data.if_unit = (u_int32_t)interface->if_unit;
1255 ev_msg.dv[0].data_length = sizeof(struct net_event_data);
1256 ev_msg.dv[0].data_ptr = &ev_data;
1257 ev_msg.dv[1].data_length = 0;
1258 dlil_post_complete_msg(interface, &ev_msg);
1259
1260 return 0;
1261 }
1262
1263 u_int32_t
ifnet_get_wake_flags(ifnet_t interface)1264 ifnet_get_wake_flags(ifnet_t interface)
1265 {
1266 u_int32_t flags = 0;
1267
1268 if (interface == NULL) {
1269 return 0;
1270 }
1271
1272 if ((interface->if_xflags & IFXF_WAKE_ON_MAGIC_PACKET) != 0) {
1273 flags |= IF_WAKE_ON_MAGIC_PACKET;
1274 }
1275
1276 return flags;
1277 }
1278
1279 /*
1280 * Should MIB data store a copy?
1281 */
1282 errno_t
ifnet_set_link_mib_data(ifnet_t interface,void * mibData,uint32_t mibLen)1283 ifnet_set_link_mib_data(ifnet_t interface, void *mibData, uint32_t mibLen)
1284 {
1285 if (interface == NULL) {
1286 return EINVAL;
1287 }
1288
1289 ifnet_lock_exclusive(interface);
1290 interface->if_linkmib = (void*)mibData;
1291 interface->if_linkmiblen = mibLen;
1292 ifnet_lock_done(interface);
1293 return 0;
1294 }
1295
1296 errno_t
ifnet_get_link_mib_data(ifnet_t interface,void * mibData,uint32_t * mibLen)1297 ifnet_get_link_mib_data(ifnet_t interface, void *mibData, uint32_t *mibLen)
1298 {
1299 errno_t result = 0;
1300
1301 if (interface == NULL) {
1302 return EINVAL;
1303 }
1304
1305 ifnet_lock_shared(interface);
1306 if (*mibLen < interface->if_linkmiblen) {
1307 result = EMSGSIZE;
1308 }
1309 if (result == 0 && interface->if_linkmib == NULL) {
1310 result = ENOTSUP;
1311 }
1312
1313 if (result == 0) {
1314 *mibLen = interface->if_linkmiblen;
1315 bcopy(interface->if_linkmib, mibData, *mibLen);
1316 }
1317 ifnet_lock_done(interface);
1318
1319 return result;
1320 }
1321
1322 uint32_t
ifnet_get_link_mib_data_length(ifnet_t interface)1323 ifnet_get_link_mib_data_length(ifnet_t interface)
1324 {
1325 return (interface == NULL) ? 0 : interface->if_linkmiblen;
1326 }
1327
1328 errno_t
ifnet_output(ifnet_t interface,protocol_family_t protocol_family,mbuf_t m,void * route,const struct sockaddr * dest)1329 ifnet_output(ifnet_t interface, protocol_family_t protocol_family,
1330 mbuf_t m, void *route, const struct sockaddr *dest)
1331 {
1332 if (interface == NULL || protocol_family == 0 || m == NULL) {
1333 if (m != NULL) {
1334 mbuf_freem_list(m);
1335 }
1336 return EINVAL;
1337 }
1338 return dlil_output(interface, protocol_family, m, route, dest, 0, NULL);
1339 }
1340
1341 errno_t
ifnet_output_raw(ifnet_t interface,protocol_family_t protocol_family,mbuf_t m)1342 ifnet_output_raw(ifnet_t interface, protocol_family_t protocol_family, mbuf_t m)
1343 {
1344 if (interface == NULL || m == NULL) {
1345 if (m != NULL) {
1346 mbuf_freem_list(m);
1347 }
1348 return EINVAL;
1349 }
1350 return dlil_output(interface, protocol_family, m, NULL, NULL, 1, NULL);
1351 }
1352
1353 errno_t
ifnet_set_mtu(ifnet_t interface,u_int32_t mtu)1354 ifnet_set_mtu(ifnet_t interface, u_int32_t mtu)
1355 {
1356 if (interface == NULL) {
1357 return EINVAL;
1358 }
1359
1360 interface->if_mtu = mtu;
1361 return 0;
1362 }
1363
1364 u_int32_t
ifnet_mtu(ifnet_t interface)1365 ifnet_mtu(ifnet_t interface)
1366 {
1367 return (interface == NULL) ? 0 : interface->if_mtu;
1368 }
1369
1370 u_char
ifnet_type(ifnet_t interface)1371 ifnet_type(ifnet_t interface)
1372 {
1373 return (interface == NULL) ? 0 : interface->if_data.ifi_type;
1374 }
1375
1376 errno_t
ifnet_set_addrlen(ifnet_t interface,u_char addrlen)1377 ifnet_set_addrlen(ifnet_t interface, u_char addrlen)
1378 {
1379 if (interface == NULL) {
1380 return EINVAL;
1381 }
1382
1383 interface->if_data.ifi_addrlen = addrlen;
1384 return 0;
1385 }
1386
1387 u_char
ifnet_addrlen(ifnet_t interface)1388 ifnet_addrlen(ifnet_t interface)
1389 {
1390 return (interface == NULL) ? 0 : interface->if_data.ifi_addrlen;
1391 }
1392
1393 errno_t
ifnet_set_hdrlen(ifnet_t interface,u_char hdrlen)1394 ifnet_set_hdrlen(ifnet_t interface, u_char hdrlen)
1395 {
1396 if (interface == NULL) {
1397 return EINVAL;
1398 }
1399
1400 interface->if_data.ifi_hdrlen = hdrlen;
1401 return 0;
1402 }
1403
1404 u_char
ifnet_hdrlen(ifnet_t interface)1405 ifnet_hdrlen(ifnet_t interface)
1406 {
1407 return (interface == NULL) ? 0 : interface->if_data.ifi_hdrlen;
1408 }
1409
1410 errno_t
ifnet_set_metric(ifnet_t interface,u_int32_t metric)1411 ifnet_set_metric(ifnet_t interface, u_int32_t metric)
1412 {
1413 if (interface == NULL) {
1414 return EINVAL;
1415 }
1416
1417 interface->if_data.ifi_metric = metric;
1418 return 0;
1419 }
1420
1421 u_int32_t
ifnet_metric(ifnet_t interface)1422 ifnet_metric(ifnet_t interface)
1423 {
1424 return (interface == NULL) ? 0 : interface->if_data.ifi_metric;
1425 }
1426
1427 errno_t
ifnet_set_baudrate(struct ifnet * ifp,uint64_t baudrate)1428 ifnet_set_baudrate(struct ifnet *ifp, uint64_t baudrate)
1429 {
1430 if (ifp == NULL) {
1431 return EINVAL;
1432 }
1433
1434 ifp->if_output_bw.max_bw = ifp->if_input_bw.max_bw =
1435 ifp->if_output_bw.eff_bw = ifp->if_input_bw.eff_bw = baudrate;
1436
1437 /* Pin if_baudrate to 32 bits until we can change the storage size */
1438 ifp->if_baudrate = (baudrate > UINT32_MAX) ? UINT32_MAX : (uint32_t)baudrate;
1439
1440 return 0;
1441 }
1442
1443 u_int64_t
ifnet_baudrate(struct ifnet * ifp)1444 ifnet_baudrate(struct ifnet *ifp)
1445 {
1446 return (ifp == NULL) ? 0 : ifp->if_baudrate;
1447 }
1448
1449 errno_t
ifnet_set_bandwidths(struct ifnet * ifp,struct if_bandwidths * output_bw,struct if_bandwidths * input_bw)1450 ifnet_set_bandwidths(struct ifnet *ifp, struct if_bandwidths *output_bw,
1451 struct if_bandwidths *input_bw)
1452 {
1453 if (ifp == NULL) {
1454 return EINVAL;
1455 }
1456
1457 /* set input values first (if any), as output values depend on them */
1458 if (input_bw != NULL) {
1459 (void) ifnet_set_input_bandwidths(ifp, input_bw);
1460 }
1461
1462 if (output_bw != NULL) {
1463 (void) ifnet_set_output_bandwidths(ifp, output_bw, FALSE);
1464 }
1465
1466 return 0;
1467 }
1468
1469 static void
ifnet_set_link_status_outbw(struct ifnet * ifp)1470 ifnet_set_link_status_outbw(struct ifnet *ifp)
1471 {
1472 struct if_wifi_status_v1 *sr;
1473 sr = &ifp->if_link_status->ifsr_u.ifsr_wifi.if_wifi_u.if_status_v1;
1474 if (ifp->if_output_bw.eff_bw != 0) {
1475 sr->valid_bitmask |=
1476 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID;
1477 sr->ul_effective_bandwidth =
1478 ifp->if_output_bw.eff_bw > UINT32_MAX ?
1479 UINT32_MAX :
1480 (uint32_t)ifp->if_output_bw.eff_bw;
1481 }
1482 if (ifp->if_output_bw.max_bw != 0) {
1483 sr->valid_bitmask |=
1484 IF_WIFI_UL_MAX_BANDWIDTH_VALID;
1485 sr->ul_max_bandwidth =
1486 ifp->if_output_bw.max_bw > UINT32_MAX ?
1487 UINT32_MAX :
1488 (uint32_t)ifp->if_output_bw.max_bw;
1489 }
1490 }
1491
1492 errno_t
ifnet_set_output_bandwidths(struct ifnet * ifp,struct if_bandwidths * bw,boolean_t locked)1493 ifnet_set_output_bandwidths(struct ifnet *ifp, struct if_bandwidths *bw,
1494 boolean_t locked)
1495 {
1496 struct if_bandwidths old_bw;
1497 struct ifclassq *ifq;
1498 u_int64_t br;
1499
1500 VERIFY(ifp != NULL && bw != NULL);
1501
1502 ifq = ifp->if_snd;
1503 if (!locked) {
1504 IFCQ_LOCK(ifq);
1505 }
1506 IFCQ_LOCK_ASSERT_HELD(ifq);
1507
1508 old_bw = ifp->if_output_bw;
1509 if (bw->eff_bw != 0) {
1510 ifp->if_output_bw.eff_bw = bw->eff_bw;
1511 }
1512 if (bw->max_bw != 0) {
1513 ifp->if_output_bw.max_bw = bw->max_bw;
1514 }
1515 if (ifp->if_output_bw.eff_bw > ifp->if_output_bw.max_bw) {
1516 ifp->if_output_bw.max_bw = ifp->if_output_bw.eff_bw;
1517 } else if (ifp->if_output_bw.eff_bw == 0) {
1518 ifp->if_output_bw.eff_bw = ifp->if_output_bw.max_bw;
1519 }
1520
1521 /* Pin if_baudrate to 32 bits */
1522 br = MAX(ifp->if_output_bw.max_bw, ifp->if_input_bw.max_bw);
1523 if (br != 0) {
1524 ifp->if_baudrate = (br > UINT32_MAX) ? UINT32_MAX : (uint32_t)br;
1525 }
1526
1527 /* Adjust queue parameters if needed */
1528 if (old_bw.eff_bw != ifp->if_output_bw.eff_bw ||
1529 old_bw.max_bw != ifp->if_output_bw.max_bw) {
1530 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_BANDWIDTH);
1531 }
1532
1533 if (!locked) {
1534 IFCQ_UNLOCK(ifq);
1535 }
1536
1537 /*
1538 * If this is a Wifi interface, update the values in
1539 * if_link_status structure also.
1540 */
1541 if (IFNET_IS_WIFI(ifp) && ifp->if_link_status != NULL) {
1542 lck_rw_lock_exclusive(&ifp->if_link_status_lock);
1543 ifnet_set_link_status_outbw(ifp);
1544 lck_rw_done(&ifp->if_link_status_lock);
1545 }
1546
1547 return 0;
1548 }
1549
1550 static void
ifnet_set_link_status_inbw(struct ifnet * ifp)1551 ifnet_set_link_status_inbw(struct ifnet *ifp)
1552 {
1553 struct if_wifi_status_v1 *sr;
1554
1555 sr = &ifp->if_link_status->ifsr_u.ifsr_wifi.if_wifi_u.if_status_v1;
1556 if (ifp->if_input_bw.eff_bw != 0) {
1557 sr->valid_bitmask |=
1558 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID;
1559 sr->dl_effective_bandwidth =
1560 ifp->if_input_bw.eff_bw > UINT32_MAX ?
1561 UINT32_MAX :
1562 (uint32_t)ifp->if_input_bw.eff_bw;
1563 }
1564 if (ifp->if_input_bw.max_bw != 0) {
1565 sr->valid_bitmask |=
1566 IF_WIFI_DL_MAX_BANDWIDTH_VALID;
1567 sr->dl_max_bandwidth = ifp->if_input_bw.max_bw > UINT32_MAX ?
1568 UINT32_MAX :
1569 (uint32_t)ifp->if_input_bw.max_bw;
1570 }
1571 }
1572
1573 errno_t
ifnet_set_input_bandwidths(struct ifnet * ifp,struct if_bandwidths * bw)1574 ifnet_set_input_bandwidths(struct ifnet *ifp, struct if_bandwidths *bw)
1575 {
1576 struct if_bandwidths old_bw;
1577
1578 VERIFY(ifp != NULL && bw != NULL);
1579
1580 old_bw = ifp->if_input_bw;
1581 if (bw->eff_bw != 0) {
1582 ifp->if_input_bw.eff_bw = bw->eff_bw;
1583 }
1584 if (bw->max_bw != 0) {
1585 ifp->if_input_bw.max_bw = bw->max_bw;
1586 }
1587 if (ifp->if_input_bw.eff_bw > ifp->if_input_bw.max_bw) {
1588 ifp->if_input_bw.max_bw = ifp->if_input_bw.eff_bw;
1589 } else if (ifp->if_input_bw.eff_bw == 0) {
1590 ifp->if_input_bw.eff_bw = ifp->if_input_bw.max_bw;
1591 }
1592
1593 if (IFNET_IS_WIFI(ifp) && ifp->if_link_status != NULL) {
1594 lck_rw_lock_exclusive(&ifp->if_link_status_lock);
1595 ifnet_set_link_status_inbw(ifp);
1596 lck_rw_done(&ifp->if_link_status_lock);
1597 }
1598
1599 if (old_bw.eff_bw != ifp->if_input_bw.eff_bw ||
1600 old_bw.max_bw != ifp->if_input_bw.max_bw) {
1601 ifnet_update_rcv(ifp, CLASSQ_EV_LINK_BANDWIDTH);
1602 }
1603
1604 return 0;
1605 }
1606
1607 u_int64_t
ifnet_output_linkrate(struct ifnet * ifp)1608 ifnet_output_linkrate(struct ifnet *ifp)
1609 {
1610 struct ifclassq *ifq = ifp->if_snd;
1611 u_int64_t rate;
1612
1613 IFCQ_LOCK_ASSERT_HELD(ifq);
1614
1615 rate = ifp->if_output_bw.eff_bw;
1616 if (IFCQ_TBR_IS_ENABLED(ifq)) {
1617 u_int64_t tbr_rate = ifq->ifcq_tbr.tbr_rate_raw;
1618 VERIFY(tbr_rate > 0);
1619 rate = MIN(rate, ifq->ifcq_tbr.tbr_rate_raw);
1620 }
1621
1622 return rate;
1623 }
1624
1625 u_int64_t
ifnet_input_linkrate(struct ifnet * ifp)1626 ifnet_input_linkrate(struct ifnet *ifp)
1627 {
1628 return ifp->if_input_bw.eff_bw;
1629 }
1630
1631 errno_t
ifnet_bandwidths(struct ifnet * ifp,struct if_bandwidths * output_bw,struct if_bandwidths * input_bw)1632 ifnet_bandwidths(struct ifnet *ifp, struct if_bandwidths *output_bw,
1633 struct if_bandwidths *input_bw)
1634 {
1635 if (ifp == NULL) {
1636 return EINVAL;
1637 }
1638
1639 if (output_bw != NULL) {
1640 *output_bw = ifp->if_output_bw;
1641 }
1642 if (input_bw != NULL) {
1643 *input_bw = ifp->if_input_bw;
1644 }
1645
1646 return 0;
1647 }
1648
1649 errno_t
ifnet_set_latencies(struct ifnet * ifp,struct if_latencies * output_lt,struct if_latencies * input_lt)1650 ifnet_set_latencies(struct ifnet *ifp, struct if_latencies *output_lt,
1651 struct if_latencies *input_lt)
1652 {
1653 if (ifp == NULL) {
1654 return EINVAL;
1655 }
1656
1657 if (output_lt != NULL) {
1658 (void) ifnet_set_output_latencies(ifp, output_lt, FALSE);
1659 }
1660
1661 if (input_lt != NULL) {
1662 (void) ifnet_set_input_latencies(ifp, input_lt);
1663 }
1664
1665 return 0;
1666 }
1667
1668 errno_t
ifnet_set_output_latencies(struct ifnet * ifp,struct if_latencies * lt,boolean_t locked)1669 ifnet_set_output_latencies(struct ifnet *ifp, struct if_latencies *lt,
1670 boolean_t locked)
1671 {
1672 struct if_latencies old_lt;
1673 struct ifclassq *ifq;
1674
1675 VERIFY(ifp != NULL && lt != NULL);
1676
1677 ifq = ifp->if_snd;
1678 if (!locked) {
1679 IFCQ_LOCK(ifq);
1680 }
1681 IFCQ_LOCK_ASSERT_HELD(ifq);
1682
1683 old_lt = ifp->if_output_lt;
1684 if (lt->eff_lt != 0) {
1685 ifp->if_output_lt.eff_lt = lt->eff_lt;
1686 }
1687 if (lt->max_lt != 0) {
1688 ifp->if_output_lt.max_lt = lt->max_lt;
1689 }
1690 if (ifp->if_output_lt.eff_lt > ifp->if_output_lt.max_lt) {
1691 ifp->if_output_lt.max_lt = ifp->if_output_lt.eff_lt;
1692 } else if (ifp->if_output_lt.eff_lt == 0) {
1693 ifp->if_output_lt.eff_lt = ifp->if_output_lt.max_lt;
1694 }
1695
1696 /* Adjust queue parameters if needed */
1697 if (old_lt.eff_lt != ifp->if_output_lt.eff_lt ||
1698 old_lt.max_lt != ifp->if_output_lt.max_lt) {
1699 ifnet_update_sndq(ifq, CLASSQ_EV_LINK_LATENCY);
1700 }
1701
1702 if (!locked) {
1703 IFCQ_UNLOCK(ifq);
1704 }
1705
1706 return 0;
1707 }
1708
1709 errno_t
ifnet_set_input_latencies(struct ifnet * ifp,struct if_latencies * lt)1710 ifnet_set_input_latencies(struct ifnet *ifp, struct if_latencies *lt)
1711 {
1712 struct if_latencies old_lt;
1713
1714 VERIFY(ifp != NULL && lt != NULL);
1715
1716 old_lt = ifp->if_input_lt;
1717 if (lt->eff_lt != 0) {
1718 ifp->if_input_lt.eff_lt = lt->eff_lt;
1719 }
1720 if (lt->max_lt != 0) {
1721 ifp->if_input_lt.max_lt = lt->max_lt;
1722 }
1723 if (ifp->if_input_lt.eff_lt > ifp->if_input_lt.max_lt) {
1724 ifp->if_input_lt.max_lt = ifp->if_input_lt.eff_lt;
1725 } else if (ifp->if_input_lt.eff_lt == 0) {
1726 ifp->if_input_lt.eff_lt = ifp->if_input_lt.max_lt;
1727 }
1728
1729 if (old_lt.eff_lt != ifp->if_input_lt.eff_lt ||
1730 old_lt.max_lt != ifp->if_input_lt.max_lt) {
1731 ifnet_update_rcv(ifp, CLASSQ_EV_LINK_LATENCY);
1732 }
1733
1734 return 0;
1735 }
1736
1737 errno_t
ifnet_latencies(struct ifnet * ifp,struct if_latencies * output_lt,struct if_latencies * input_lt)1738 ifnet_latencies(struct ifnet *ifp, struct if_latencies *output_lt,
1739 struct if_latencies *input_lt)
1740 {
1741 if (ifp == NULL) {
1742 return EINVAL;
1743 }
1744
1745 if (output_lt != NULL) {
1746 *output_lt = ifp->if_output_lt;
1747 }
1748 if (input_lt != NULL) {
1749 *input_lt = ifp->if_input_lt;
1750 }
1751
1752 return 0;
1753 }
1754
1755 errno_t
ifnet_set_poll_params(struct ifnet * ifp,struct ifnet_poll_params * p)1756 ifnet_set_poll_params(struct ifnet *ifp, struct ifnet_poll_params *p)
1757 {
1758 errno_t err;
1759
1760 if (ifp == NULL) {
1761 return EINVAL;
1762 } else if (!ifnet_is_attached(ifp, 1)) {
1763 return ENXIO;
1764 }
1765
1766 #if SKYWALK
1767 if (SKYWALK_CAPABLE(ifp)) {
1768 err = netif_rxpoll_set_params(ifp, p, FALSE);
1769 ifnet_decr_iorefcnt(ifp);
1770 return err;
1771 }
1772 #endif /* SKYWALK */
1773 err = dlil_rxpoll_set_params(ifp, p, FALSE);
1774
1775 /* Release the io ref count */
1776 ifnet_decr_iorefcnt(ifp);
1777
1778 return err;
1779 }
1780
1781 errno_t
ifnet_poll_params(struct ifnet * ifp,struct ifnet_poll_params * p)1782 ifnet_poll_params(struct ifnet *ifp, struct ifnet_poll_params *p)
1783 {
1784 errno_t err;
1785
1786 if (ifp == NULL || p == NULL) {
1787 return EINVAL;
1788 } else if (!ifnet_is_attached(ifp, 1)) {
1789 return ENXIO;
1790 }
1791
1792 err = dlil_rxpoll_get_params(ifp, p);
1793
1794 /* Release the io ref count */
1795 ifnet_decr_iorefcnt(ifp);
1796
1797 return err;
1798 }
1799
1800 errno_t
ifnet_stat_increment(struct ifnet * ifp,const struct ifnet_stat_increment_param * s)1801 ifnet_stat_increment(struct ifnet *ifp,
1802 const struct ifnet_stat_increment_param *s)
1803 {
1804 if (ifp == NULL) {
1805 return EINVAL;
1806 }
1807
1808 if (s->packets_in != 0) {
1809 atomic_add_64(&ifp->if_data.ifi_ipackets, s->packets_in);
1810 }
1811 if (s->bytes_in != 0) {
1812 atomic_add_64(&ifp->if_data.ifi_ibytes, s->bytes_in);
1813 }
1814 if (s->errors_in != 0) {
1815 atomic_add_64(&ifp->if_data.ifi_ierrors, s->errors_in);
1816 }
1817
1818 if (s->packets_out != 0) {
1819 atomic_add_64(&ifp->if_data.ifi_opackets, s->packets_out);
1820 }
1821 if (s->bytes_out != 0) {
1822 atomic_add_64(&ifp->if_data.ifi_obytes, s->bytes_out);
1823 }
1824 if (s->errors_out != 0) {
1825 atomic_add_64(&ifp->if_data.ifi_oerrors, s->errors_out);
1826 }
1827
1828 if (s->collisions != 0) {
1829 atomic_add_64(&ifp->if_data.ifi_collisions, s->collisions);
1830 }
1831 if (s->dropped != 0) {
1832 atomic_add_64(&ifp->if_data.ifi_iqdrops, s->dropped);
1833 }
1834
1835 /* Touch the last change time. */
1836 TOUCHLASTCHANGE(&ifp->if_lastchange);
1837
1838 if (ifp->if_data_threshold != 0) {
1839 ifnet_notify_data_threshold(ifp);
1840 }
1841
1842 return 0;
1843 }
1844
1845 errno_t
ifnet_stat_increment_in(struct ifnet * ifp,u_int32_t packets_in,u_int32_t bytes_in,u_int32_t errors_in)1846 ifnet_stat_increment_in(struct ifnet *ifp, u_int32_t packets_in,
1847 u_int32_t bytes_in, u_int32_t errors_in)
1848 {
1849 if (ifp == NULL) {
1850 return EINVAL;
1851 }
1852
1853 if (packets_in != 0) {
1854 atomic_add_64(&ifp->if_data.ifi_ipackets, packets_in);
1855 }
1856 if (bytes_in != 0) {
1857 atomic_add_64(&ifp->if_data.ifi_ibytes, bytes_in);
1858 }
1859 if (errors_in != 0) {
1860 atomic_add_64(&ifp->if_data.ifi_ierrors, errors_in);
1861 }
1862
1863 TOUCHLASTCHANGE(&ifp->if_lastchange);
1864
1865 if (ifp->if_data_threshold != 0) {
1866 ifnet_notify_data_threshold(ifp);
1867 }
1868
1869 return 0;
1870 }
1871
1872 errno_t
ifnet_stat_increment_out(struct ifnet * ifp,u_int32_t packets_out,u_int32_t bytes_out,u_int32_t errors_out)1873 ifnet_stat_increment_out(struct ifnet *ifp, u_int32_t packets_out,
1874 u_int32_t bytes_out, u_int32_t errors_out)
1875 {
1876 if (ifp == NULL) {
1877 return EINVAL;
1878 }
1879
1880 if (packets_out != 0) {
1881 atomic_add_64(&ifp->if_data.ifi_opackets, packets_out);
1882 }
1883 if (bytes_out != 0) {
1884 atomic_add_64(&ifp->if_data.ifi_obytes, bytes_out);
1885 }
1886 if (errors_out != 0) {
1887 atomic_add_64(&ifp->if_data.ifi_oerrors, errors_out);
1888 }
1889
1890 TOUCHLASTCHANGE(&ifp->if_lastchange);
1891
1892 if (ifp->if_data_threshold != 0) {
1893 ifnet_notify_data_threshold(ifp);
1894 }
1895
1896 return 0;
1897 }
1898
1899 errno_t
ifnet_set_stat(struct ifnet * ifp,const struct ifnet_stats_param * s)1900 ifnet_set_stat(struct ifnet *ifp, const struct ifnet_stats_param *s)
1901 {
1902 if (ifp == NULL) {
1903 return EINVAL;
1904 }
1905
1906 atomic_set_64(&ifp->if_data.ifi_ipackets, s->packets_in);
1907 atomic_set_64(&ifp->if_data.ifi_ibytes, s->bytes_in);
1908 atomic_set_64(&ifp->if_data.ifi_imcasts, s->multicasts_in);
1909 atomic_set_64(&ifp->if_data.ifi_ierrors, s->errors_in);
1910
1911 atomic_set_64(&ifp->if_data.ifi_opackets, s->packets_out);
1912 atomic_set_64(&ifp->if_data.ifi_obytes, s->bytes_out);
1913 atomic_set_64(&ifp->if_data.ifi_omcasts, s->multicasts_out);
1914 atomic_set_64(&ifp->if_data.ifi_oerrors, s->errors_out);
1915
1916 atomic_set_64(&ifp->if_data.ifi_collisions, s->collisions);
1917 atomic_set_64(&ifp->if_data.ifi_iqdrops, s->dropped);
1918 atomic_set_64(&ifp->if_data.ifi_noproto, s->no_protocol);
1919
1920 /* Touch the last change time. */
1921 TOUCHLASTCHANGE(&ifp->if_lastchange);
1922
1923 if (ifp->if_data_threshold != 0) {
1924 ifnet_notify_data_threshold(ifp);
1925 }
1926
1927 return 0;
1928 }
1929
1930 errno_t
ifnet_stat(struct ifnet * ifp,struct ifnet_stats_param * s)1931 ifnet_stat(struct ifnet *ifp, struct ifnet_stats_param *s)
1932 {
1933 if (ifp == NULL) {
1934 return EINVAL;
1935 }
1936
1937 atomic_get_64(s->packets_in, &ifp->if_data.ifi_ipackets);
1938 atomic_get_64(s->bytes_in, &ifp->if_data.ifi_ibytes);
1939 atomic_get_64(s->multicasts_in, &ifp->if_data.ifi_imcasts);
1940 atomic_get_64(s->errors_in, &ifp->if_data.ifi_ierrors);
1941
1942 atomic_get_64(s->packets_out, &ifp->if_data.ifi_opackets);
1943 atomic_get_64(s->bytes_out, &ifp->if_data.ifi_obytes);
1944 atomic_get_64(s->multicasts_out, &ifp->if_data.ifi_omcasts);
1945 atomic_get_64(s->errors_out, &ifp->if_data.ifi_oerrors);
1946
1947 atomic_get_64(s->collisions, &ifp->if_data.ifi_collisions);
1948 atomic_get_64(s->dropped, &ifp->if_data.ifi_iqdrops);
1949 atomic_get_64(s->no_protocol, &ifp->if_data.ifi_noproto);
1950
1951 if (ifp->if_data_threshold != 0) {
1952 ifnet_notify_data_threshold(ifp);
1953 }
1954
1955 return 0;
1956 }
1957
1958 errno_t
ifnet_touch_lastchange(ifnet_t interface)1959 ifnet_touch_lastchange(ifnet_t interface)
1960 {
1961 if (interface == NULL) {
1962 return EINVAL;
1963 }
1964
1965 TOUCHLASTCHANGE(&interface->if_lastchange);
1966
1967 return 0;
1968 }
1969
1970 errno_t
ifnet_lastchange(ifnet_t interface,struct timeval * last_change)1971 ifnet_lastchange(ifnet_t interface, struct timeval *last_change)
1972 {
1973 if (interface == NULL) {
1974 return EINVAL;
1975 }
1976
1977 *last_change = interface->if_data.ifi_lastchange;
1978 /* Crude conversion from uptime to calendar time */
1979 last_change->tv_sec += boottime_sec();
1980
1981 return 0;
1982 }
1983
1984 errno_t
ifnet_touch_lastupdown(ifnet_t interface)1985 ifnet_touch_lastupdown(ifnet_t interface)
1986 {
1987 if (interface == NULL) {
1988 return EINVAL;
1989 }
1990
1991 TOUCHLASTCHANGE(&interface->if_lastupdown);
1992
1993 return 0;
1994 }
1995
1996 errno_t
ifnet_updown_delta(ifnet_t interface,struct timeval * updown_delta)1997 ifnet_updown_delta(ifnet_t interface, struct timeval *updown_delta)
1998 {
1999 if (interface == NULL) {
2000 return EINVAL;
2001 }
2002
2003 /* Calculate the delta */
2004 updown_delta->tv_sec = (time_t)net_uptime();
2005 if (updown_delta->tv_sec > interface->if_data.ifi_lastupdown.tv_sec) {
2006 updown_delta->tv_sec -= interface->if_data.ifi_lastupdown.tv_sec;
2007 } else {
2008 updown_delta->tv_sec = 0;
2009 }
2010 updown_delta->tv_usec = 0;
2011
2012 return 0;
2013 }
2014
2015 errno_t
ifnet_get_address_list(ifnet_t interface,ifaddr_t ** addresses)2016 ifnet_get_address_list(ifnet_t interface, ifaddr_t **addresses)
2017 {
2018 return addresses == NULL ? EINVAL :
2019 ifnet_get_address_list_family(interface, addresses, 0);
2020 }
2021
2022 struct ifnet_addr_list {
2023 SLIST_ENTRY(ifnet_addr_list) ifal_le;
2024 struct ifaddr *ifal_ifa;
2025 };
2026
2027 errno_t
ifnet_get_address_list_family(ifnet_t interface,ifaddr_t ** addresses,sa_family_t family)2028 ifnet_get_address_list_family(ifnet_t interface, ifaddr_t **addresses,
2029 sa_family_t family)
2030 {
2031 return ifnet_get_address_list_family_internal(interface, addresses,
2032 family, 0, Z_WAITOK, 0);
2033 }
2034
2035 errno_t
ifnet_get_inuse_address_list(ifnet_t interface,ifaddr_t ** addresses)2036 ifnet_get_inuse_address_list(ifnet_t interface, ifaddr_t **addresses)
2037 {
2038 return addresses == NULL ? EINVAL :
2039 ifnet_get_address_list_family_internal(interface, addresses,
2040 0, 0, Z_WAITOK, 1);
2041 }
2042
2043 extern uint32_t tcp_find_anypcb_byaddr(struct ifaddr *ifa);
2044
2045 extern uint32_t udp_find_anypcb_byaddr(struct ifaddr *ifa);
2046
2047 __private_extern__ errno_t
ifnet_get_address_list_family_internal(ifnet_t interface,ifaddr_t ** addresses,sa_family_t family,int detached,int how,int return_inuse_addrs)2048 ifnet_get_address_list_family_internal(ifnet_t interface, ifaddr_t **addresses,
2049 sa_family_t family, int detached, int how, int return_inuse_addrs)
2050 {
2051 SLIST_HEAD(, ifnet_addr_list) ifal_head;
2052 struct ifnet_addr_list *ifal, *ifal_tmp;
2053 struct ifnet *ifp;
2054 int count = 0;
2055 errno_t err = 0;
2056 int usecount = 0;
2057 int index = 0;
2058
2059 SLIST_INIT(&ifal_head);
2060
2061 if (addresses == NULL) {
2062 err = EINVAL;
2063 goto done;
2064 }
2065 *addresses = NULL;
2066
2067 if (detached) {
2068 /*
2069 * Interface has been detached, so skip the lookup
2070 * at ifnet_head and go directly to inner loop.
2071 */
2072 ifp = interface;
2073 if (ifp == NULL) {
2074 err = EINVAL;
2075 goto done;
2076 }
2077 goto one;
2078 }
2079
2080 ifnet_head_lock_shared();
2081 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2082 if (interface != NULL && ifp != interface) {
2083 continue;
2084 }
2085 one:
2086 ifnet_lock_shared(ifp);
2087 if (interface == NULL || interface == ifp) {
2088 struct ifaddr *ifa;
2089 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2090 IFA_LOCK(ifa);
2091 if (family != 0 &&
2092 ifa->ifa_addr->sa_family != family) {
2093 IFA_UNLOCK(ifa);
2094 continue;
2095 }
2096 ifal = kalloc_type(struct ifnet_addr_list, how);
2097 if (ifal == NULL) {
2098 IFA_UNLOCK(ifa);
2099 ifnet_lock_done(ifp);
2100 if (!detached) {
2101 ifnet_head_done();
2102 }
2103 err = ENOMEM;
2104 goto done;
2105 }
2106 ifal->ifal_ifa = ifa;
2107 IFA_ADDREF_LOCKED(ifa);
2108 SLIST_INSERT_HEAD(&ifal_head, ifal, ifal_le);
2109 ++count;
2110 IFA_UNLOCK(ifa);
2111 }
2112 }
2113 ifnet_lock_done(ifp);
2114 if (detached) {
2115 break;
2116 }
2117 }
2118 if (!detached) {
2119 ifnet_head_done();
2120 }
2121
2122 if (count == 0) {
2123 err = ENXIO;
2124 goto done;
2125 }
2126
2127 *addresses = kalloc_type(ifaddr_t, count + 1, how | Z_ZERO);
2128 if (*addresses == NULL) {
2129 err = ENOMEM;
2130 goto done;
2131 }
2132
2133 done:
2134 SLIST_FOREACH_SAFE(ifal, &ifal_head, ifal_le, ifal_tmp) {
2135 SLIST_REMOVE(&ifal_head, ifal, ifnet_addr_list, ifal_le);
2136 if (err == 0) {
2137 if (return_inuse_addrs) {
2138 usecount = tcp_find_anypcb_byaddr(ifal->ifal_ifa);
2139 usecount += udp_find_anypcb_byaddr(ifal->ifal_ifa);
2140 if (usecount) {
2141 (*addresses)[index] = ifal->ifal_ifa;
2142 index++;
2143 } else {
2144 IFA_REMREF(ifal->ifal_ifa);
2145 }
2146 } else {
2147 (*addresses)[--count] = ifal->ifal_ifa;
2148 }
2149 } else {
2150 IFA_REMREF(ifal->ifal_ifa);
2151 }
2152 kfree_type(struct ifnet_addr_list, ifal);
2153 }
2154
2155 VERIFY(err == 0 || *addresses == NULL);
2156 if ((err == 0) && (count) && ((*addresses)[0] == NULL)) {
2157 VERIFY(return_inuse_addrs == 1);
2158 kfree_type(ifaddr_t, count + 1, *addresses);
2159 err = ENXIO;
2160 }
2161 return err;
2162 }
2163
2164 void
ifnet_free_address_list(ifaddr_t * addresses)2165 ifnet_free_address_list(ifaddr_t *addresses)
2166 {
2167 int i;
2168
2169 if (addresses == NULL) {
2170 return;
2171 }
2172
2173 for (i = 0; addresses[i] != NULL; i++) {
2174 IFA_REMREF(addresses[i]);
2175 }
2176
2177 kfree_type(ifaddr_t, i + 1, addresses);
2178 }
2179
2180 void *
ifnet_lladdr(ifnet_t interface)2181 ifnet_lladdr(ifnet_t interface)
2182 {
2183 struct ifaddr *ifa;
2184 void *lladdr;
2185
2186 if (interface == NULL) {
2187 return NULL;
2188 }
2189
2190 /*
2191 * if_lladdr points to the permanent link address of
2192 * the interface and it never gets deallocated; internal
2193 * code should simply use IF_LLADDR() for performance.
2194 */
2195 ifa = interface->if_lladdr;
2196 IFA_LOCK_SPIN(ifa);
2197 lladdr = LLADDR(SDL((void *)ifa->ifa_addr));
2198 IFA_UNLOCK(ifa);
2199
2200 return lladdr;
2201 }
2202
2203 errno_t
ifnet_llbroadcast_copy_bytes(ifnet_t interface,void * addr,size_t buffer_len,size_t * out_len)2204 ifnet_llbroadcast_copy_bytes(ifnet_t interface, void *addr, size_t buffer_len,
2205 size_t *out_len)
2206 {
2207 if (interface == NULL || addr == NULL || out_len == NULL) {
2208 return EINVAL;
2209 }
2210
2211 *out_len = interface->if_broadcast.length;
2212
2213 if (buffer_len < interface->if_broadcast.length) {
2214 return EMSGSIZE;
2215 }
2216
2217 if (interface->if_broadcast.length == 0) {
2218 return ENXIO;
2219 }
2220
2221 if (interface->if_broadcast.length <=
2222 sizeof(interface->if_broadcast.u.buffer)) {
2223 bcopy(interface->if_broadcast.u.buffer, addr,
2224 interface->if_broadcast.length);
2225 } else {
2226 bcopy(interface->if_broadcast.u.ptr, addr,
2227 interface->if_broadcast.length);
2228 }
2229
2230 return 0;
2231 }
2232
2233 static errno_t
ifnet_lladdr_copy_bytes_internal(ifnet_t interface,void * lladdr,size_t lladdr_len,kauth_cred_t * credp)2234 ifnet_lladdr_copy_bytes_internal(ifnet_t interface, void *lladdr,
2235 size_t lladdr_len, kauth_cred_t *credp)
2236 {
2237 const u_int8_t *bytes;
2238 size_t bytes_len;
2239 struct ifaddr *ifa;
2240 uint8_t sdlbuf[SOCK_MAXADDRLEN + 1];
2241 errno_t error = 0;
2242
2243 /*
2244 * Make sure to accomodate the largest possible
2245 * size of SA(if_lladdr)->sa_len.
2246 */
2247 _CASSERT(sizeof(sdlbuf) == (SOCK_MAXADDRLEN + 1));
2248
2249 if (interface == NULL || lladdr == NULL) {
2250 return EINVAL;
2251 }
2252
2253 ifa = interface->if_lladdr;
2254 IFA_LOCK_SPIN(ifa);
2255 bcopy(ifa->ifa_addr, &sdlbuf, SDL(ifa->ifa_addr)->sdl_len);
2256 IFA_UNLOCK(ifa);
2257
2258 bytes = dlil_ifaddr_bytes(SDL(&sdlbuf), &bytes_len, credp);
2259 if (bytes_len != lladdr_len) {
2260 bzero(lladdr, lladdr_len);
2261 error = EMSGSIZE;
2262 } else {
2263 bcopy(bytes, lladdr, bytes_len);
2264 }
2265
2266 return error;
2267 }
2268
2269 errno_t
ifnet_lladdr_copy_bytes(ifnet_t interface,void * lladdr,size_t length)2270 ifnet_lladdr_copy_bytes(ifnet_t interface, void *lladdr, size_t length)
2271 {
2272 return ifnet_lladdr_copy_bytes_internal(interface, lladdr, length,
2273 NULL);
2274 }
2275
2276 errno_t
ifnet_guarded_lladdr_copy_bytes(ifnet_t interface,void * lladdr,size_t length)2277 ifnet_guarded_lladdr_copy_bytes(ifnet_t interface, void *lladdr, size_t length)
2278 {
2279 #if CONFIG_MACF
2280 kauth_cred_t cred;
2281 net_thread_marks_t marks;
2282 #endif
2283 kauth_cred_t *credp;
2284 errno_t error;
2285
2286 credp = NULL;
2287 #if CONFIG_MACF
2288 marks = net_thread_marks_push(NET_THREAD_CKREQ_LLADDR);
2289 cred = kauth_cred_proc_ref(current_proc());
2290 credp = &cred;
2291 #else
2292 credp = NULL;
2293 #endif
2294
2295 error = ifnet_lladdr_copy_bytes_internal(interface, lladdr, length,
2296 credp);
2297
2298 #if CONFIG_MACF
2299 kauth_cred_unref(credp);
2300 net_thread_marks_pop(marks);
2301 #endif
2302
2303 return error;
2304 }
2305
2306 static errno_t
ifnet_set_lladdr_internal(ifnet_t interface,const void * lladdr,size_t lladdr_len,u_char new_type,int apply_type)2307 ifnet_set_lladdr_internal(ifnet_t interface, const void *lladdr,
2308 size_t lladdr_len, u_char new_type, int apply_type)
2309 {
2310 struct ifaddr *ifa;
2311 errno_t error = 0;
2312
2313 if (interface == NULL) {
2314 return EINVAL;
2315 }
2316
2317 ifnet_head_lock_shared();
2318 ifnet_lock_exclusive(interface);
2319 if (lladdr_len != 0 &&
2320 (lladdr_len != interface->if_addrlen || lladdr == 0)) {
2321 ifnet_lock_done(interface);
2322 ifnet_head_done();
2323 return EINVAL;
2324 }
2325 ifa = ifnet_addrs[interface->if_index - 1];
2326 if (ifa != NULL) {
2327 struct sockaddr_dl *sdl;
2328
2329 IFA_LOCK_SPIN(ifa);
2330 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
2331 if (lladdr_len != 0) {
2332 bcopy(lladdr, LLADDR(sdl), lladdr_len);
2333 } else {
2334 bzero(LLADDR(sdl), interface->if_addrlen);
2335 }
2336 /* lladdr_len-check with if_addrlen makes sure it fits in u_char */
2337 sdl->sdl_alen = (u_char)lladdr_len;
2338
2339 if (apply_type) {
2340 sdl->sdl_type = new_type;
2341 }
2342 IFA_UNLOCK(ifa);
2343 } else {
2344 error = ENXIO;
2345 }
2346 ifnet_lock_done(interface);
2347 ifnet_head_done();
2348
2349 /* Generate a kernel event */
2350 if (error == 0) {
2351 intf_event_enqueue_nwk_wq_entry(interface, NULL,
2352 INTF_EVENT_CODE_LLADDR_UPDATE);
2353 dlil_post_msg(interface, KEV_DL_SUBCLASS,
2354 KEV_DL_LINK_ADDRESS_CHANGED, NULL, 0, FALSE);
2355 }
2356
2357 return error;
2358 }
2359
2360 errno_t
ifnet_set_lladdr(ifnet_t interface,const void * lladdr,size_t lladdr_len)2361 ifnet_set_lladdr(ifnet_t interface, const void* lladdr, size_t lladdr_len)
2362 {
2363 return ifnet_set_lladdr_internal(interface, lladdr, lladdr_len, 0, 0);
2364 }
2365
2366 errno_t
ifnet_set_lladdr_and_type(ifnet_t interface,const void * lladdr,size_t lladdr_len,u_char type)2367 ifnet_set_lladdr_and_type(ifnet_t interface, const void* lladdr,
2368 size_t lladdr_len, u_char type)
2369 {
2370 return ifnet_set_lladdr_internal(interface, lladdr,
2371 lladdr_len, type, 1);
2372 }
2373
2374 errno_t
ifnet_add_multicast(ifnet_t interface,const struct sockaddr * maddr,ifmultiaddr_t * ifmap)2375 ifnet_add_multicast(ifnet_t interface, const struct sockaddr *maddr,
2376 ifmultiaddr_t *ifmap)
2377 {
2378 if (interface == NULL || maddr == NULL) {
2379 return EINVAL;
2380 }
2381
2382 /* Don't let users screw up protocols' entries. */
2383 switch (maddr->sa_family) {
2384 case AF_LINK: {
2385 const struct sockaddr_dl *sdl =
2386 (const struct sockaddr_dl *)(uintptr_t)maddr;
2387 if (sdl->sdl_len < sizeof(struct sockaddr_dl) ||
2388 (sdl->sdl_nlen + sdl->sdl_alen + sdl->sdl_slen +
2389 offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)) {
2390 return EINVAL;
2391 }
2392 break;
2393 }
2394 case AF_UNSPEC:
2395 if (maddr->sa_len < ETHER_ADDR_LEN +
2396 offsetof(struct sockaddr, sa_data)) {
2397 return EINVAL;
2398 }
2399 break;
2400 default:
2401 return EINVAL;
2402 }
2403
2404 return if_addmulti_anon(interface, maddr, ifmap);
2405 }
2406
2407 errno_t
ifnet_remove_multicast(ifmultiaddr_t ifma)2408 ifnet_remove_multicast(ifmultiaddr_t ifma)
2409 {
2410 struct sockaddr *maddr;
2411
2412 if (ifma == NULL) {
2413 return EINVAL;
2414 }
2415
2416 maddr = ifma->ifma_addr;
2417 /* Don't let users screw up protocols' entries. */
2418 if (maddr->sa_family != AF_UNSPEC && maddr->sa_family != AF_LINK) {
2419 return EINVAL;
2420 }
2421
2422 return if_delmulti_anon(ifma->ifma_ifp, maddr);
2423 }
2424
2425 errno_t
ifnet_get_multicast_list(ifnet_t ifp,ifmultiaddr_t ** addresses)2426 ifnet_get_multicast_list(ifnet_t ifp, ifmultiaddr_t **addresses)
2427 {
2428 int count = 0;
2429 int cmax = 0;
2430 struct ifmultiaddr *addr;
2431
2432 if (ifp == NULL || addresses == NULL) {
2433 return EINVAL;
2434 }
2435
2436 ifnet_lock_shared(ifp);
2437 LIST_FOREACH(addr, &ifp->if_multiaddrs, ifma_link) {
2438 cmax++;
2439 }
2440
2441 *addresses = kalloc_type(ifmultiaddr_t, cmax + 1, Z_WAITOK);
2442 if (*addresses == NULL) {
2443 ifnet_lock_done(ifp);
2444 return ENOMEM;
2445 }
2446
2447 LIST_FOREACH(addr, &ifp->if_multiaddrs, ifma_link) {
2448 if (count + 1 > cmax) {
2449 break;
2450 }
2451 (*addresses)[count] = (ifmultiaddr_t)addr;
2452 ifmaddr_reference((*addresses)[count]);
2453 count++;
2454 }
2455 (*addresses)[cmax] = NULL;
2456 ifnet_lock_done(ifp);
2457
2458 return 0;
2459 }
2460
2461 void
ifnet_free_multicast_list(ifmultiaddr_t * addresses)2462 ifnet_free_multicast_list(ifmultiaddr_t *addresses)
2463 {
2464 int i;
2465
2466 if (addresses == NULL) {
2467 return;
2468 }
2469
2470 for (i = 0; addresses[i] != NULL; i++) {
2471 ifmaddr_release(addresses[i]);
2472 }
2473
2474 kfree_type(ifmultiaddr_t, i + 1, addresses);
2475 }
2476
2477 errno_t
ifnet_find_by_name(const char * ifname,ifnet_t * ifpp)2478 ifnet_find_by_name(const char *ifname, ifnet_t *ifpp)
2479 {
2480 struct ifnet *ifp;
2481 size_t namelen;
2482
2483 if (ifname == NULL) {
2484 return EINVAL;
2485 }
2486
2487 namelen = strlen(ifname);
2488
2489 *ifpp = NULL;
2490
2491 ifnet_head_lock_shared();
2492 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2493 struct ifaddr *ifa;
2494 struct sockaddr_dl *ll_addr;
2495
2496 ifa = ifnet_addrs[ifp->if_index - 1];
2497 if (ifa == NULL) {
2498 continue;
2499 }
2500
2501 IFA_LOCK(ifa);
2502 ll_addr = (struct sockaddr_dl *)(void *)ifa->ifa_addr;
2503
2504 if (namelen == ll_addr->sdl_nlen && strncmp(ll_addr->sdl_data,
2505 ifname, ll_addr->sdl_nlen) == 0) {
2506 IFA_UNLOCK(ifa);
2507 *ifpp = ifp;
2508 ifnet_reference(*ifpp);
2509 break;
2510 }
2511 IFA_UNLOCK(ifa);
2512 }
2513 ifnet_head_done();
2514
2515 return (ifp == NULL) ? ENXIO : 0;
2516 }
2517
2518 errno_t
ifnet_list_get(ifnet_family_t family,ifnet_t ** list,u_int32_t * count)2519 ifnet_list_get(ifnet_family_t family, ifnet_t **list, u_int32_t *count)
2520 {
2521 return ifnet_list_get_common(family, FALSE, list, count);
2522 }
2523
2524 __private_extern__ errno_t
ifnet_list_get_all(ifnet_family_t family,ifnet_t ** list,u_int32_t * count)2525 ifnet_list_get_all(ifnet_family_t family, ifnet_t **list, u_int32_t *count)
2526 {
2527 return ifnet_list_get_common(family, TRUE, list, count);
2528 }
2529
2530 struct ifnet_list {
2531 SLIST_ENTRY(ifnet_list) ifl_le;
2532 struct ifnet *ifl_ifp;
2533 };
2534
2535 static errno_t
ifnet_list_get_common(ifnet_family_t family,boolean_t get_all,ifnet_t ** list,u_int32_t * count)2536 ifnet_list_get_common(ifnet_family_t family, boolean_t get_all, ifnet_t **list,
2537 u_int32_t *count)
2538 {
2539 #pragma unused(get_all)
2540 SLIST_HEAD(, ifnet_list) ifl_head;
2541 struct ifnet_list *ifl, *ifl_tmp;
2542 struct ifnet *ifp;
2543 int cnt = 0;
2544 errno_t err = 0;
2545
2546 SLIST_INIT(&ifl_head);
2547
2548 if (list == NULL || count == NULL) {
2549 err = EINVAL;
2550 goto done;
2551 }
2552 *count = 0;
2553 *list = NULL;
2554
2555 ifnet_head_lock_shared();
2556 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2557 if (family == IFNET_FAMILY_ANY || ifp->if_family == family) {
2558 ifl = kalloc_type(struct ifnet_list, Z_NOWAIT);
2559 if (ifl == NULL) {
2560 ifnet_head_done();
2561 err = ENOMEM;
2562 goto done;
2563 }
2564 ifl->ifl_ifp = ifp;
2565 ifnet_reference(ifp);
2566 SLIST_INSERT_HEAD(&ifl_head, ifl, ifl_le);
2567 ++cnt;
2568 }
2569 }
2570 ifnet_head_done();
2571
2572 if (cnt == 0) {
2573 err = ENXIO;
2574 goto done;
2575 }
2576
2577 *list = kalloc_type(ifnet_t, cnt + 1, Z_WAITOK | Z_ZERO);
2578 if (*list == NULL) {
2579 err = ENOMEM;
2580 goto done;
2581 }
2582 *count = cnt;
2583
2584 done:
2585 SLIST_FOREACH_SAFE(ifl, &ifl_head, ifl_le, ifl_tmp) {
2586 SLIST_REMOVE(&ifl_head, ifl, ifnet_list, ifl_le);
2587 if (err == 0) {
2588 (*list)[--cnt] = ifl->ifl_ifp;
2589 } else {
2590 ifnet_release(ifl->ifl_ifp);
2591 }
2592 kfree_type(struct ifnet_list, ifl);
2593 }
2594
2595 return err;
2596 }
2597
2598 void
ifnet_list_free(ifnet_t * interfaces)2599 ifnet_list_free(ifnet_t *interfaces)
2600 {
2601 int i;
2602
2603 if (interfaces == NULL) {
2604 return;
2605 }
2606
2607 for (i = 0; interfaces[i]; i++) {
2608 ifnet_release(interfaces[i]);
2609 }
2610
2611 kfree_type(ifnet_t, i + 1, interfaces);
2612 }
2613
2614 /*************************************************************************/
2615 /* ifaddr_t accessors */
2616 /*************************************************************************/
2617
2618 errno_t
ifaddr_reference(ifaddr_t ifa)2619 ifaddr_reference(ifaddr_t ifa)
2620 {
2621 if (ifa == NULL) {
2622 return EINVAL;
2623 }
2624
2625 IFA_ADDREF(ifa);
2626 return 0;
2627 }
2628
2629 errno_t
ifaddr_release(ifaddr_t ifa)2630 ifaddr_release(ifaddr_t ifa)
2631 {
2632 if (ifa == NULL) {
2633 return EINVAL;
2634 }
2635
2636 IFA_REMREF(ifa);
2637 return 0;
2638 }
2639
2640 sa_family_t
ifaddr_address_family(ifaddr_t ifa)2641 ifaddr_address_family(ifaddr_t ifa)
2642 {
2643 sa_family_t family = 0;
2644
2645 if (ifa != NULL) {
2646 IFA_LOCK_SPIN(ifa);
2647 if (ifa->ifa_addr != NULL) {
2648 family = ifa->ifa_addr->sa_family;
2649 }
2650 IFA_UNLOCK(ifa);
2651 }
2652 return family;
2653 }
2654
2655 errno_t
ifaddr_address(ifaddr_t ifa,struct sockaddr * out_addr,u_int32_t addr_size)2656 ifaddr_address(ifaddr_t ifa, struct sockaddr *out_addr, u_int32_t addr_size)
2657 {
2658 u_int32_t copylen;
2659
2660 if (ifa == NULL || out_addr == NULL) {
2661 return EINVAL;
2662 }
2663
2664 IFA_LOCK_SPIN(ifa);
2665 if (ifa->ifa_addr == NULL) {
2666 IFA_UNLOCK(ifa);
2667 return ENOTSUP;
2668 }
2669
2670 copylen = (addr_size >= ifa->ifa_addr->sa_len) ?
2671 ifa->ifa_addr->sa_len : addr_size;
2672 bcopy(ifa->ifa_addr, out_addr, copylen);
2673
2674 if (ifa->ifa_addr->sa_len > addr_size) {
2675 IFA_UNLOCK(ifa);
2676 return EMSGSIZE;
2677 }
2678
2679 IFA_UNLOCK(ifa);
2680 return 0;
2681 }
2682
2683 errno_t
ifaddr_dstaddress(ifaddr_t ifa,struct sockaddr * out_addr,u_int32_t addr_size)2684 ifaddr_dstaddress(ifaddr_t ifa, struct sockaddr *out_addr, u_int32_t addr_size)
2685 {
2686 u_int32_t copylen;
2687
2688 if (ifa == NULL || out_addr == NULL) {
2689 return EINVAL;
2690 }
2691
2692 IFA_LOCK_SPIN(ifa);
2693 if (ifa->ifa_dstaddr == NULL) {
2694 IFA_UNLOCK(ifa);
2695 return ENOTSUP;
2696 }
2697
2698 copylen = (addr_size >= ifa->ifa_dstaddr->sa_len) ?
2699 ifa->ifa_dstaddr->sa_len : addr_size;
2700 bcopy(ifa->ifa_dstaddr, out_addr, copylen);
2701
2702 if (ifa->ifa_dstaddr->sa_len > addr_size) {
2703 IFA_UNLOCK(ifa);
2704 return EMSGSIZE;
2705 }
2706
2707 IFA_UNLOCK(ifa);
2708 return 0;
2709 }
2710
2711 errno_t
ifaddr_netmask(ifaddr_t ifa,struct sockaddr * out_addr,u_int32_t addr_size)2712 ifaddr_netmask(ifaddr_t ifa, struct sockaddr *out_addr, u_int32_t addr_size)
2713 {
2714 u_int32_t copylen;
2715
2716 if (ifa == NULL || out_addr == NULL) {
2717 return EINVAL;
2718 }
2719
2720 IFA_LOCK_SPIN(ifa);
2721 if (ifa->ifa_netmask == NULL) {
2722 IFA_UNLOCK(ifa);
2723 return ENOTSUP;
2724 }
2725
2726 copylen = addr_size >= ifa->ifa_netmask->sa_len ?
2727 ifa->ifa_netmask->sa_len : addr_size;
2728 bcopy(ifa->ifa_netmask, out_addr, copylen);
2729
2730 if (ifa->ifa_netmask->sa_len > addr_size) {
2731 IFA_UNLOCK(ifa);
2732 return EMSGSIZE;
2733 }
2734
2735 IFA_UNLOCK(ifa);
2736 return 0;
2737 }
2738
2739 ifnet_t
ifaddr_ifnet(ifaddr_t ifa)2740 ifaddr_ifnet(ifaddr_t ifa)
2741 {
2742 struct ifnet *ifp;
2743
2744 if (ifa == NULL) {
2745 return NULL;
2746 }
2747
2748 /* ifa_ifp is set once at creation time; it is never changed */
2749 ifp = ifa->ifa_ifp;
2750
2751 return ifp;
2752 }
2753
2754 ifaddr_t
ifaddr_withaddr(const struct sockaddr * address)2755 ifaddr_withaddr(const struct sockaddr *address)
2756 {
2757 if (address == NULL) {
2758 return NULL;
2759 }
2760
2761 return ifa_ifwithaddr(address);
2762 }
2763
2764 ifaddr_t
ifaddr_withdstaddr(const struct sockaddr * address)2765 ifaddr_withdstaddr(const struct sockaddr *address)
2766 {
2767 if (address == NULL) {
2768 return NULL;
2769 }
2770
2771 return ifa_ifwithdstaddr(address);
2772 }
2773
2774 ifaddr_t
ifaddr_withnet(const struct sockaddr * net)2775 ifaddr_withnet(const struct sockaddr *net)
2776 {
2777 if (net == NULL) {
2778 return NULL;
2779 }
2780
2781 return ifa_ifwithnet(net);
2782 }
2783
2784 ifaddr_t
ifaddr_withroute(int flags,const struct sockaddr * destination,const struct sockaddr * gateway)2785 ifaddr_withroute(int flags, const struct sockaddr *destination,
2786 const struct sockaddr *gateway)
2787 {
2788 if (destination == NULL || gateway == NULL) {
2789 return NULL;
2790 }
2791
2792 return ifa_ifwithroute(flags, destination, gateway);
2793 }
2794
2795 ifaddr_t
ifaddr_findbestforaddr(const struct sockaddr * addr,ifnet_t interface)2796 ifaddr_findbestforaddr(const struct sockaddr *addr, ifnet_t interface)
2797 {
2798 if (addr == NULL || interface == NULL) {
2799 return NULL;
2800 }
2801
2802 return ifaof_ifpforaddr_select(addr, interface);
2803 }
2804
2805 errno_t
ifmaddr_reference(ifmultiaddr_t ifmaddr)2806 ifmaddr_reference(ifmultiaddr_t ifmaddr)
2807 {
2808 if (ifmaddr == NULL) {
2809 return EINVAL;
2810 }
2811
2812 IFMA_ADDREF(ifmaddr);
2813 return 0;
2814 }
2815
2816 errno_t
ifmaddr_release(ifmultiaddr_t ifmaddr)2817 ifmaddr_release(ifmultiaddr_t ifmaddr)
2818 {
2819 if (ifmaddr == NULL) {
2820 return EINVAL;
2821 }
2822
2823 IFMA_REMREF(ifmaddr);
2824 return 0;
2825 }
2826
2827 errno_t
ifmaddr_address(ifmultiaddr_t ifma,struct sockaddr * out_addr,u_int32_t addr_size)2828 ifmaddr_address(ifmultiaddr_t ifma, struct sockaddr *out_addr,
2829 u_int32_t addr_size)
2830 {
2831 u_int32_t copylen;
2832
2833 if (ifma == NULL || out_addr == NULL) {
2834 return EINVAL;
2835 }
2836
2837 IFMA_LOCK(ifma);
2838 if (ifma->ifma_addr == NULL) {
2839 IFMA_UNLOCK(ifma);
2840 return ENOTSUP;
2841 }
2842
2843 copylen = (addr_size >= ifma->ifma_addr->sa_len ?
2844 ifma->ifma_addr->sa_len : addr_size);
2845 bcopy(ifma->ifma_addr, out_addr, copylen);
2846
2847 if (ifma->ifma_addr->sa_len > addr_size) {
2848 IFMA_UNLOCK(ifma);
2849 return EMSGSIZE;
2850 }
2851 IFMA_UNLOCK(ifma);
2852 return 0;
2853 }
2854
2855 errno_t
ifmaddr_lladdress(ifmultiaddr_t ifma,struct sockaddr * out_addr,u_int32_t addr_size)2856 ifmaddr_lladdress(ifmultiaddr_t ifma, struct sockaddr *out_addr,
2857 u_int32_t addr_size)
2858 {
2859 struct ifmultiaddr *ifma_ll;
2860
2861 if (ifma == NULL || out_addr == NULL) {
2862 return EINVAL;
2863 }
2864 if ((ifma_ll = ifma->ifma_ll) == NULL) {
2865 return ENOTSUP;
2866 }
2867
2868 return ifmaddr_address(ifma_ll, out_addr, addr_size);
2869 }
2870
2871 ifnet_t
ifmaddr_ifnet(ifmultiaddr_t ifma)2872 ifmaddr_ifnet(ifmultiaddr_t ifma)
2873 {
2874 return (ifma == NULL) ? NULL : ifma->ifma_ifp;
2875 }
2876
2877 /**************************************************************************/
2878 /* interface cloner */
2879 /**************************************************************************/
2880
2881 errno_t
ifnet_clone_attach(struct ifnet_clone_params * cloner_params,if_clone_t * ifcloner)2882 ifnet_clone_attach(struct ifnet_clone_params *cloner_params,
2883 if_clone_t *ifcloner)
2884 {
2885 errno_t error = 0;
2886 struct if_clone *ifc = NULL;
2887 size_t namelen;
2888
2889 if (cloner_params == NULL || ifcloner == NULL ||
2890 cloner_params->ifc_name == NULL ||
2891 cloner_params->ifc_create == NULL ||
2892 cloner_params->ifc_destroy == NULL ||
2893 (namelen = strlen(cloner_params->ifc_name)) >= IFNAMSIZ) {
2894 error = EINVAL;
2895 goto fail;
2896 }
2897
2898 if (if_clone_lookup(cloner_params->ifc_name, NULL) != NULL) {
2899 printf("%s: already a cloner for %s\n", __func__,
2900 cloner_params->ifc_name);
2901 error = EEXIST;
2902 goto fail;
2903 }
2904
2905 ifc = kalloc_type(struct if_clone, Z_WAITOK | Z_ZERO | Z_NOFAIL);
2906 strlcpy(ifc->ifc_name, cloner_params->ifc_name, IFNAMSIZ + 1);
2907 ifc->ifc_namelen = (uint8_t)namelen;
2908 ifc->ifc_maxunit = IF_MAXUNIT;
2909 ifc->ifc_create = cloner_params->ifc_create;
2910 ifc->ifc_destroy = cloner_params->ifc_destroy;
2911
2912 error = if_clone_attach(ifc);
2913 if (error != 0) {
2914 printf("%s: if_clone_attach failed %d\n", __func__, error);
2915 goto fail;
2916 }
2917 *ifcloner = ifc;
2918
2919 return 0;
2920 fail:
2921 if (ifc != NULL) {
2922 kfree_type(struct if_clone, ifc);
2923 }
2924 return error;
2925 }
2926
2927 errno_t
ifnet_clone_detach(if_clone_t ifcloner)2928 ifnet_clone_detach(if_clone_t ifcloner)
2929 {
2930 errno_t error = 0;
2931 struct if_clone *ifc = ifcloner;
2932
2933 if (ifc == NULL) {
2934 return EINVAL;
2935 }
2936
2937 if ((if_clone_lookup(ifc->ifc_name, NULL)) == NULL) {
2938 printf("%s: no cloner for %s\n", __func__, ifc->ifc_name);
2939 error = EINVAL;
2940 goto fail;
2941 }
2942
2943 if_clone_detach(ifc);
2944
2945 kfree_type(struct if_clone, ifc);
2946
2947 fail:
2948 return error;
2949 }
2950
2951 /**************************************************************************/
2952 /* misc */
2953 /**************************************************************************/
2954
2955 errno_t
ifnet_get_local_ports_extended(ifnet_t ifp,protocol_family_t protocol,u_int32_t flags,u_int8_t * bitfield)2956 ifnet_get_local_ports_extended(ifnet_t ifp, protocol_family_t protocol,
2957 u_int32_t flags, u_int8_t *bitfield)
2958 {
2959 u_int32_t ifindex;
2960
2961 if (bitfield == NULL) {
2962 return EINVAL;
2963 }
2964
2965 switch (protocol) {
2966 case PF_UNSPEC:
2967 case PF_INET:
2968 case PF_INET6:
2969 break;
2970 default:
2971 return EINVAL;
2972 }
2973
2974 /* bit string is long enough to hold 16-bit port values */
2975 bzero(bitfield, bitstr_size(IP_PORTRANGE_SIZE));
2976
2977 /* no point in continuing if no address is assigned */
2978 if (ifp != NULL && TAILQ_EMPTY(&ifp->if_addrhead)) {
2979 return 0;
2980 }
2981
2982 if_ports_used_update_wakeuuid(ifp);
2983
2984 #if SKYWALK
2985 if (netns_is_enabled()) {
2986 netns_get_local_ports(ifp, protocol, flags, bitfield);
2987 }
2988 #endif /* SKYWALK */
2989
2990 ifindex = (ifp != NULL) ? ifp->if_index : 0;
2991
2992 if (!(flags & IFNET_GET_LOCAL_PORTS_TCPONLY)) {
2993 udp_get_ports_used(ifp, protocol, flags,
2994 bitfield);
2995 }
2996
2997 if (!(flags & IFNET_GET_LOCAL_PORTS_UDPONLY)) {
2998 tcp_get_ports_used(ifp, protocol, flags,
2999 bitfield);
3000 }
3001
3002 return 0;
3003 }
3004
3005 errno_t
ifnet_get_local_ports(ifnet_t ifp,u_int8_t * bitfield)3006 ifnet_get_local_ports(ifnet_t ifp, u_int8_t *bitfield)
3007 {
3008 u_int32_t flags = IFNET_GET_LOCAL_PORTS_WILDCARDOK;
3009 return ifnet_get_local_ports_extended(ifp, PF_UNSPEC, flags,
3010 bitfield);
3011 }
3012
3013 errno_t
ifnet_notice_node_presence(ifnet_t ifp,struct sockaddr * sa,int32_t rssi,int lqm,int npm,u_int8_t srvinfo[48])3014 ifnet_notice_node_presence(ifnet_t ifp, struct sockaddr *sa, int32_t rssi,
3015 int lqm, int npm, u_int8_t srvinfo[48])
3016 {
3017 if (ifp == NULL || sa == NULL || srvinfo == NULL) {
3018 return EINVAL;
3019 }
3020 if (sa->sa_len > sizeof(struct sockaddr_storage)) {
3021 return EINVAL;
3022 }
3023 if (sa->sa_family != AF_LINK && sa->sa_family != AF_INET6) {
3024 return EINVAL;
3025 }
3026
3027 return dlil_node_present(ifp, sa, rssi, lqm, npm, srvinfo);
3028 }
3029
3030 errno_t
ifnet_notice_node_presence_v2(ifnet_t ifp,struct sockaddr * sa,struct sockaddr_dl * sdl,int32_t rssi,int lqm,int npm,u_int8_t srvinfo[48])3031 ifnet_notice_node_presence_v2(ifnet_t ifp, struct sockaddr *sa, struct sockaddr_dl *sdl,
3032 int32_t rssi, int lqm, int npm, u_int8_t srvinfo[48])
3033 {
3034 /* Support older version if sdl is NULL */
3035 if (sdl == NULL) {
3036 return ifnet_notice_node_presence(ifp, sa, rssi, lqm, npm, srvinfo);
3037 }
3038
3039 if (ifp == NULL || sa == NULL || srvinfo == NULL) {
3040 return EINVAL;
3041 }
3042 if (sa->sa_len > sizeof(struct sockaddr_storage)) {
3043 return EINVAL;
3044 }
3045
3046 if (sa->sa_family != AF_INET6) {
3047 return EINVAL;
3048 }
3049
3050 if (sdl->sdl_family != AF_LINK) {
3051 return EINVAL;
3052 }
3053
3054 return dlil_node_present_v2(ifp, sa, sdl, rssi, lqm, npm, srvinfo);
3055 }
3056
3057 errno_t
ifnet_notice_node_absence(ifnet_t ifp,struct sockaddr * sa)3058 ifnet_notice_node_absence(ifnet_t ifp, struct sockaddr *sa)
3059 {
3060 if (ifp == NULL || sa == NULL) {
3061 return EINVAL;
3062 }
3063 if (sa->sa_len > sizeof(struct sockaddr_storage)) {
3064 return EINVAL;
3065 }
3066 if (sa->sa_family != AF_LINK && sa->sa_family != AF_INET6) {
3067 return EINVAL;
3068 }
3069
3070 dlil_node_absent(ifp, sa);
3071 return 0;
3072 }
3073
3074 errno_t
ifnet_notice_primary_elected(ifnet_t ifp)3075 ifnet_notice_primary_elected(ifnet_t ifp)
3076 {
3077 if (ifp == NULL) {
3078 return EINVAL;
3079 }
3080
3081 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PRIMARY_ELECTED, NULL, 0, FALSE);
3082 return 0;
3083 }
3084
3085 errno_t
ifnet_tx_compl_status(ifnet_t ifp,mbuf_t m,tx_compl_val_t val)3086 ifnet_tx_compl_status(ifnet_t ifp, mbuf_t m, tx_compl_val_t val)
3087 {
3088 #pragma unused(val)
3089
3090 m_do_tx_compl_callback(m, ifp);
3091
3092 return 0;
3093 }
3094
3095 errno_t
ifnet_tx_compl(ifnet_t ifp,mbuf_t m)3096 ifnet_tx_compl(ifnet_t ifp, mbuf_t m)
3097 {
3098 m_do_tx_compl_callback(m, ifp);
3099
3100 return 0;
3101 }
3102
3103 errno_t
ifnet_report_issues(ifnet_t ifp,u_int8_t modid[IFNET_MODIDLEN],u_int8_t info[IFNET_MODARGLEN])3104 ifnet_report_issues(ifnet_t ifp, u_int8_t modid[IFNET_MODIDLEN],
3105 u_int8_t info[IFNET_MODARGLEN])
3106 {
3107 if (ifp == NULL || modid == NULL) {
3108 return EINVAL;
3109 }
3110
3111 dlil_report_issues(ifp, modid, info);
3112 return 0;
3113 }
3114
3115 errno_t
ifnet_set_delegate(ifnet_t ifp,ifnet_t delegated_ifp)3116 ifnet_set_delegate(ifnet_t ifp, ifnet_t delegated_ifp)
3117 {
3118 ifnet_t odifp = NULL;
3119
3120 if (ifp == NULL) {
3121 return EINVAL;
3122 } else if (!ifnet_is_attached(ifp, 1)) {
3123 return ENXIO;
3124 }
3125
3126 ifnet_lock_exclusive(ifp);
3127 odifp = ifp->if_delegated.ifp;
3128 if (odifp != NULL && odifp == delegated_ifp) {
3129 /* delegate info is unchanged; nothing more to do */
3130 ifnet_lock_done(ifp);
3131 goto done;
3132 }
3133 // Test if this delegate interface would cause a loop
3134 ifnet_t delegate_check_ifp = delegated_ifp;
3135 while (delegate_check_ifp != NULL) {
3136 if (delegate_check_ifp == ifp) {
3137 printf("%s: delegating to %s would cause a loop\n",
3138 ifp->if_xname, delegated_ifp->if_xname);
3139 ifnet_lock_done(ifp);
3140 goto done;
3141 }
3142 delegate_check_ifp = delegate_check_ifp->if_delegated.ifp;
3143 }
3144 bzero(&ifp->if_delegated, sizeof(ifp->if_delegated));
3145 if (delegated_ifp != NULL && ifp != delegated_ifp) {
3146 uint32_t set_eflags;
3147
3148 ifp->if_delegated.ifp = delegated_ifp;
3149 ifnet_reference(delegated_ifp);
3150 ifp->if_delegated.type = delegated_ifp->if_type;
3151 ifp->if_delegated.family = delegated_ifp->if_family;
3152 ifp->if_delegated.subfamily = delegated_ifp->if_subfamily;
3153 ifp->if_delegated.expensive =
3154 delegated_ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
3155 ifp->if_delegated.constrained =
3156 delegated_ifp->if_xflags & IFXF_CONSTRAINED ? 1 : 0;
3157
3158 /*
3159 * Propogate flags related to ECN from delegated interface
3160 */
3161 if_clear_eflags(ifp, IFEF_ECN_ENABLE | IFEF_ECN_DISABLE);
3162 set_eflags = (delegated_ifp->if_eflags &
3163 (IFEF_ECN_ENABLE | IFEF_ECN_DISABLE));
3164 if_set_eflags(ifp, set_eflags);
3165 printf("%s: is now delegating %s (type 0x%x, family %u, "
3166 "sub-family %u)\n", ifp->if_xname, delegated_ifp->if_xname,
3167 delegated_ifp->if_type, delegated_ifp->if_family,
3168 delegated_ifp->if_subfamily);
3169 }
3170
3171 ifnet_lock_done(ifp);
3172
3173 if (odifp != NULL) {
3174 if (odifp != delegated_ifp) {
3175 printf("%s: is no longer delegating %s\n",
3176 ifp->if_xname, odifp->if_xname);
3177 }
3178 ifnet_release(odifp);
3179 }
3180
3181 /* Generate a kernel event */
3182 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IFDELEGATE_CHANGED, NULL, 0, FALSE);
3183
3184 done:
3185 /* Release the io ref count */
3186 ifnet_decr_iorefcnt(ifp);
3187
3188 return 0;
3189 }
3190
3191 errno_t
ifnet_get_delegate(ifnet_t ifp,ifnet_t * pdelegated_ifp)3192 ifnet_get_delegate(ifnet_t ifp, ifnet_t *pdelegated_ifp)
3193 {
3194 if (ifp == NULL || pdelegated_ifp == NULL) {
3195 return EINVAL;
3196 } else if (!ifnet_is_attached(ifp, 1)) {
3197 return ENXIO;
3198 }
3199
3200 ifnet_lock_shared(ifp);
3201 if (ifp->if_delegated.ifp != NULL) {
3202 ifnet_reference(ifp->if_delegated.ifp);
3203 }
3204 *pdelegated_ifp = ifp->if_delegated.ifp;
3205 ifnet_lock_done(ifp);
3206
3207 /* Release the io ref count */
3208 ifnet_decr_iorefcnt(ifp);
3209
3210 return 0;
3211 }
3212
3213 errno_t
ifnet_get_keepalive_offload_frames(ifnet_t ifp,struct ifnet_keepalive_offload_frame * frames_array,u_int32_t frames_array_count,size_t frame_data_offset,u_int32_t * used_frames_count)3214 ifnet_get_keepalive_offload_frames(ifnet_t ifp,
3215 struct ifnet_keepalive_offload_frame *frames_array,
3216 u_int32_t frames_array_count, size_t frame_data_offset,
3217 u_int32_t *used_frames_count)
3218 {
3219 u_int32_t i;
3220
3221 if (frames_array == NULL || used_frames_count == NULL ||
3222 frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
3223 return EINVAL;
3224 }
3225
3226 /* frame_data_offset should be 32-bit aligned */
3227 if (P2ROUNDUP(frame_data_offset, sizeof(u_int32_t)) !=
3228 frame_data_offset) {
3229 return EINVAL;
3230 }
3231
3232 *used_frames_count = 0;
3233 if (frames_array_count == 0) {
3234 return 0;
3235 }
3236
3237 /* Keep-alive offload not required for CLAT interface */
3238 if (IS_INTF_CLAT46(ifp)) {
3239 return 0;
3240 }
3241
3242 for (i = 0; i < frames_array_count; i++) {
3243 struct ifnet_keepalive_offload_frame *frame = frames_array + i;
3244
3245 bzero(frame, sizeof(struct ifnet_keepalive_offload_frame));
3246 }
3247
3248 /* First collect IPsec related keep-alive frames */
3249 *used_frames_count = key_fill_offload_frames_for_savs(ifp,
3250 frames_array, frames_array_count, frame_data_offset);
3251
3252 /* If there is more room, collect other UDP keep-alive frames */
3253 if (*used_frames_count < frames_array_count) {
3254 udp_fill_keepalive_offload_frames(ifp, frames_array,
3255 frames_array_count, frame_data_offset,
3256 used_frames_count);
3257 }
3258
3259 /* If there is more room, collect other TCP keep-alive frames */
3260 if (*used_frames_count < frames_array_count) {
3261 tcp_fill_keepalive_offload_frames(ifp, frames_array,
3262 frames_array_count, frame_data_offset,
3263 used_frames_count);
3264 }
3265
3266 VERIFY(*used_frames_count <= frames_array_count);
3267
3268 return 0;
3269 }
3270
3271 errno_t
ifnet_notify_tcp_keepalive_offload_timeout(ifnet_t ifp,struct ifnet_keepalive_offload_frame * frame)3272 ifnet_notify_tcp_keepalive_offload_timeout(ifnet_t ifp,
3273 struct ifnet_keepalive_offload_frame *frame)
3274 {
3275 errno_t error = 0;
3276
3277 if (ifp == NULL || frame == NULL) {
3278 return EINVAL;
3279 }
3280
3281 if (frame->type != IFNET_KEEPALIVE_OFFLOAD_FRAME_TCP) {
3282 return EINVAL;
3283 }
3284 if (frame->ether_type != IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4 &&
3285 frame->ether_type != IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6) {
3286 return EINVAL;
3287 }
3288 if (frame->local_port == 0 || frame->remote_port == 0) {
3289 return EINVAL;
3290 }
3291
3292 error = tcp_notify_kao_timeout(ifp, frame);
3293
3294 return error;
3295 }
3296
3297 errno_t
ifnet_link_status_report(ifnet_t ifp,const void * buffer,size_t buffer_len)3298 ifnet_link_status_report(ifnet_t ifp, const void *buffer,
3299 size_t buffer_len)
3300 {
3301 struct if_link_status *ifsr;
3302 errno_t err = 0;
3303
3304 if (ifp == NULL || buffer == NULL || buffer_len == 0) {
3305 return EINVAL;
3306 }
3307
3308 ifnet_lock_shared(ifp);
3309
3310 /*
3311 * Make sure that the interface is attached but there is no need
3312 * to take a reference because this call is coming from the driver.
3313 */
3314 if (!ifnet_is_attached(ifp, 0)) {
3315 ifnet_lock_done(ifp);
3316 return ENXIO;
3317 }
3318
3319 lck_rw_lock_exclusive(&ifp->if_link_status_lock);
3320
3321 /*
3322 * If this is the first status report then allocate memory
3323 * to store it.
3324 */
3325 if (ifp->if_link_status == NULL) {
3326 ifp->if_link_status = kalloc_type(struct if_link_status, Z_ZERO);
3327 if (ifp->if_link_status == NULL) {
3328 err = ENOMEM;
3329 goto done;
3330 }
3331 }
3332
3333 ifsr = __DECONST(struct if_link_status *, buffer);
3334
3335 if (ifp->if_type == IFT_CELLULAR) {
3336 struct if_cellular_status_v1 *if_cell_sr, *new_cell_sr;
3337 /*
3338 * Currently we have a single version -- if it does
3339 * not match, just return.
3340 */
3341 if (ifsr->ifsr_version !=
3342 IF_CELLULAR_STATUS_REPORT_CURRENT_VERSION) {
3343 err = ENOTSUP;
3344 goto done;
3345 }
3346
3347 if (ifsr->ifsr_len != sizeof(*if_cell_sr)) {
3348 err = EINVAL;
3349 goto done;
3350 }
3351
3352 if_cell_sr =
3353 &ifp->if_link_status->ifsr_u.ifsr_cell.if_cell_u.if_status_v1;
3354 new_cell_sr = &ifsr->ifsr_u.ifsr_cell.if_cell_u.if_status_v1;
3355 /* Check if we need to act on any new notifications */
3356 if ((new_cell_sr->valid_bitmask &
3357 IF_CELL_UL_MSS_RECOMMENDED_VALID) &&
3358 new_cell_sr->mss_recommended !=
3359 if_cell_sr->mss_recommended) {
3360 atomic_bitset_32(&tcbinfo.ipi_flags,
3361 INPCBINFO_UPDATE_MSS);
3362 inpcb_timer_sched(&tcbinfo, INPCB_TIMER_FAST);
3363 #if NECP
3364 necp_update_all_clients();
3365 #endif
3366 }
3367
3368 /* Finally copy the new information */
3369 ifp->if_link_status->ifsr_version = ifsr->ifsr_version;
3370 ifp->if_link_status->ifsr_len = ifsr->ifsr_len;
3371 if_cell_sr->valid_bitmask = 0;
3372 bcopy(new_cell_sr, if_cell_sr, sizeof(*if_cell_sr));
3373 } else if (IFNET_IS_WIFI(ifp)) {
3374 struct if_wifi_status_v1 *if_wifi_sr, *new_wifi_sr;
3375
3376 /* Check version */
3377 if (ifsr->ifsr_version !=
3378 IF_WIFI_STATUS_REPORT_CURRENT_VERSION) {
3379 err = ENOTSUP;
3380 goto done;
3381 }
3382
3383 if (ifsr->ifsr_len != sizeof(*if_wifi_sr)) {
3384 err = EINVAL;
3385 goto done;
3386 }
3387
3388 if_wifi_sr =
3389 &ifp->if_link_status->ifsr_u.ifsr_wifi.if_wifi_u.if_status_v1;
3390 new_wifi_sr =
3391 &ifsr->ifsr_u.ifsr_wifi.if_wifi_u.if_status_v1;
3392 ifp->if_link_status->ifsr_version = ifsr->ifsr_version;
3393 ifp->if_link_status->ifsr_len = ifsr->ifsr_len;
3394 if_wifi_sr->valid_bitmask = 0;
3395 bcopy(new_wifi_sr, if_wifi_sr, sizeof(*if_wifi_sr));
3396
3397 /*
3398 * Update the bandwidth values if we got recent values
3399 * reported through the other KPI.
3400 */
3401 if (!(new_wifi_sr->valid_bitmask &
3402 IF_WIFI_UL_MAX_BANDWIDTH_VALID) &&
3403 ifp->if_output_bw.max_bw > 0) {
3404 if_wifi_sr->valid_bitmask |=
3405 IF_WIFI_UL_MAX_BANDWIDTH_VALID;
3406 if_wifi_sr->ul_max_bandwidth =
3407 ifp->if_output_bw.max_bw > UINT32_MAX ?
3408 UINT32_MAX :
3409 (uint32_t)ifp->if_output_bw.max_bw;
3410 }
3411 if (!(new_wifi_sr->valid_bitmask &
3412 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID) &&
3413 ifp->if_output_bw.eff_bw > 0) {
3414 if_wifi_sr->valid_bitmask |=
3415 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID;
3416 if_wifi_sr->ul_effective_bandwidth =
3417 ifp->if_output_bw.eff_bw > UINT32_MAX ?
3418 UINT32_MAX :
3419 (uint32_t)ifp->if_output_bw.eff_bw;
3420 }
3421 if (!(new_wifi_sr->valid_bitmask &
3422 IF_WIFI_DL_MAX_BANDWIDTH_VALID) &&
3423 ifp->if_input_bw.max_bw > 0) {
3424 if_wifi_sr->valid_bitmask |=
3425 IF_WIFI_DL_MAX_BANDWIDTH_VALID;
3426 if_wifi_sr->dl_max_bandwidth =
3427 ifp->if_input_bw.max_bw > UINT32_MAX ?
3428 UINT32_MAX :
3429 (uint32_t)ifp->if_input_bw.max_bw;
3430 }
3431 if (!(new_wifi_sr->valid_bitmask &
3432 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID) &&
3433 ifp->if_input_bw.eff_bw > 0) {
3434 if_wifi_sr->valid_bitmask |=
3435 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID;
3436 if_wifi_sr->dl_effective_bandwidth =
3437 ifp->if_input_bw.eff_bw > UINT32_MAX ?
3438 UINT32_MAX :
3439 (uint32_t)ifp->if_input_bw.eff_bw;
3440 }
3441 }
3442
3443 done:
3444 lck_rw_done(&ifp->if_link_status_lock);
3445 ifnet_lock_done(ifp);
3446 return err;
3447 }
3448
3449 /*************************************************************************/
3450 /* Fastlane QoS Ca */
3451 /*************************************************************************/
3452
3453 errno_t
ifnet_set_fastlane_capable(ifnet_t interface,boolean_t capable)3454 ifnet_set_fastlane_capable(ifnet_t interface, boolean_t capable)
3455 {
3456 if (interface == NULL) {
3457 return EINVAL;
3458 }
3459
3460 if_set_qosmarking_mode(interface,
3461 capable ? IFRTYPE_QOSMARKING_FASTLANE : IFRTYPE_QOSMARKING_MODE_NONE);
3462
3463 return 0;
3464 }
3465
3466 errno_t
ifnet_get_fastlane_capable(ifnet_t interface,boolean_t * capable)3467 ifnet_get_fastlane_capable(ifnet_t interface, boolean_t *capable)
3468 {
3469 if (interface == NULL || capable == NULL) {
3470 return EINVAL;
3471 }
3472 if (interface->if_qosmarking_mode == IFRTYPE_QOSMARKING_FASTLANE) {
3473 *capable = true;
3474 } else {
3475 *capable = false;
3476 }
3477 return 0;
3478 }
3479
3480 errno_t
ifnet_get_unsent_bytes(ifnet_t interface,int64_t * unsent_bytes)3481 ifnet_get_unsent_bytes(ifnet_t interface, int64_t *unsent_bytes)
3482 {
3483 int64_t bytes;
3484
3485 if (interface == NULL || unsent_bytes == NULL) {
3486 return EINVAL;
3487 }
3488
3489 bytes = *unsent_bytes = 0;
3490
3491 if (!IF_FULLY_ATTACHED(interface)) {
3492 return ENXIO;
3493 }
3494
3495 bytes = interface->if_sndbyte_unsent;
3496
3497 if (interface->if_eflags & IFEF_TXSTART) {
3498 bytes += IFCQ_BYTES(interface->if_snd);
3499 }
3500 *unsent_bytes = bytes;
3501
3502 return 0;
3503 }
3504
3505 errno_t
ifnet_get_buffer_status(const ifnet_t ifp,ifnet_buffer_status_t * buf_status)3506 ifnet_get_buffer_status(const ifnet_t ifp, ifnet_buffer_status_t *buf_status)
3507 {
3508 if (ifp == NULL || buf_status == NULL) {
3509 return EINVAL;
3510 }
3511
3512 bzero(buf_status, sizeof(*buf_status));
3513
3514 if (!IF_FULLY_ATTACHED(ifp)) {
3515 return ENXIO;
3516 }
3517
3518 if (ifp->if_eflags & IFEF_TXSTART) {
3519 buf_status->buf_interface = IFCQ_BYTES(ifp->if_snd);
3520 }
3521
3522 buf_status->buf_sndbuf = ((buf_status->buf_interface != 0) ||
3523 (ifp->if_sndbyte_unsent != 0)) ? 1 : 0;
3524
3525 return 0;
3526 }
3527
3528 void
ifnet_normalise_unsent_data(void)3529 ifnet_normalise_unsent_data(void)
3530 {
3531 struct ifnet *ifp;
3532
3533 ifnet_head_lock_shared();
3534 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
3535 ifnet_lock_exclusive(ifp);
3536 if (!IF_FULLY_ATTACHED(ifp)) {
3537 ifnet_lock_done(ifp);
3538 continue;
3539 }
3540 if (!(ifp->if_eflags & IFEF_TXSTART)) {
3541 ifnet_lock_done(ifp);
3542 continue;
3543 }
3544
3545 if (ifp->if_sndbyte_total > 0 ||
3546 IFCQ_BYTES(ifp->if_snd) > 0) {
3547 ifp->if_unsent_data_cnt++;
3548 }
3549
3550 ifnet_lock_done(ifp);
3551 }
3552 ifnet_head_done();
3553 }
3554
3555 errno_t
ifnet_set_low_power_mode(ifnet_t ifp,boolean_t on)3556 ifnet_set_low_power_mode(ifnet_t ifp, boolean_t on)
3557 {
3558 errno_t error;
3559
3560 error = if_set_low_power(ifp, on);
3561
3562 return error;
3563 }
3564
3565 errno_t
ifnet_get_low_power_mode(ifnet_t ifp,boolean_t * on)3566 ifnet_get_low_power_mode(ifnet_t ifp, boolean_t *on)
3567 {
3568 if (ifp == NULL || on == NULL) {
3569 return EINVAL;
3570 }
3571
3572 *on = ((ifp->if_xflags & IFXF_LOW_POWER) != 0);
3573 return 0;
3574 }
3575