2 * Copyright (c) 2004-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include "kpi_interface.h"
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>
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>
60 #include <sys/sysctl.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>
69 #include <netinet/igmp_var.h>
72 #include <netinet6/mld6_var.h>
74 #include <netkey/key.h>
77 #include "net/net_str_id.h"
80 #include <sys/kauth.h>
81 #include <security/mac_framework.h>
86 errno_t
ifnet_allocate(const struct ifnet_init_params
*init
,
89 static errno_t
ifnet_allocate_common(const struct ifnet_init_params
*init
,
90 ifnet_t
*ifp
, bool is_internal
);
93 #define TOUCHLASTCHANGE(__if_lastchange) { \
94 (__if_lastchange)->tv_sec = net_uptime(); \
95 (__if_lastchange)->tv_usec = 0; \
98 static errno_t
ifnet_defrouter_llreachinfo(ifnet_t
, int,
99 struct ifnet_llreach_info
*);
100 static void ifnet_kpi_free(ifnet_t
);
101 static errno_t
ifnet_list_get_common(ifnet_family_t
, boolean_t
, ifnet_t
**,
103 static errno_t
ifnet_set_lladdr_internal(ifnet_t
, const void *, size_t,
105 static errno_t
ifnet_awdl_check_eflags(ifnet_t
, u_int32_t
*, u_int32_t
*);
108 * Temporary work around until we have real reference counting
110 * We keep the bits about calling dlil_if_release (which should be
111 * called recycle) transparent by calling it from our if_free function
112 * pointer. We have to keep the client's original detach function
113 * somewhere so we can call it.
116 ifnet_kpi_free(ifnet_t ifp
)
118 ifnet_detached_func detach_func
= ifp
->if_kpi_storage
;
120 if (detach_func
!= NULL
)
123 if (ifp
->if_broadcast
.length
> sizeof (ifp
->if_broadcast
.u
.buffer
)) {
124 FREE(ifp
->if_broadcast
.u
.ptr
, M_IFADDR
);
125 ifp
->if_broadcast
.u
.ptr
= NULL
;
128 dlil_if_release(ifp
);
132 ifnet_allocate_common(const struct ifnet_init_params
*init
,
133 ifnet_t
*ifp
, bool is_internal
)
135 struct ifnet_init_eparams einit
;
137 bzero(&einit
, sizeof (einit
));
139 einit
.ver
= IFNET_INIT_CURRENT_VERSION
;
140 einit
.len
= sizeof (einit
);
141 einit
.flags
= IFNET_INIT_LEGACY
| IFNET_INIT_NX_NOAUTO
;
143 einit
.flags
|= IFNET_INIT_ALLOC_KPI
;
145 einit
.uniqueid
= init
->uniqueid
;
146 einit
.uniqueid_len
= init
->uniqueid_len
;
147 einit
.name
= init
->name
;
148 einit
.unit
= init
->unit
;
149 einit
.family
= init
->family
;
150 einit
.type
= init
->type
;
151 einit
.output
= init
->output
;
152 einit
.demux
= init
->demux
;
153 einit
.add_proto
= init
->add_proto
;
154 einit
.del_proto
= init
->del_proto
;
155 einit
.check_multi
= init
->check_multi
;
156 einit
.framer
= init
->framer
;
157 einit
.softc
= init
->softc
;
158 einit
.ioctl
= init
->ioctl
;
159 einit
.set_bpf_tap
= init
->set_bpf_tap
;
160 einit
.detach
= init
->detach
;
161 einit
.event
= init
->event
;
162 einit
.broadcast_addr
= init
->broadcast_addr
;
163 einit
.broadcast_len
= init
->broadcast_len
;
165 return (ifnet_allocate_extended(&einit
, ifp
));
169 ifnet_allocate_internal(const struct ifnet_init_params
*init
, ifnet_t
*ifp
)
171 return (ifnet_allocate_common(init
, ifp
, true));
175 ifnet_allocate(const struct ifnet_init_params
*init
, ifnet_t
*ifp
)
177 return (ifnet_allocate_common(init
, ifp
, false));
181 ifnet_allocate_extended(const struct ifnet_init_eparams
*einit0
,
184 struct ifnet_init_eparams einit
;
185 struct ifnet
*ifp
= NULL
;
186 char if_xname
[IFXNAMSIZ
] = {0};
191 if (einit
.ver
!= IFNET_INIT_CURRENT_VERSION
||
192 einit
.len
< sizeof (einit
))
195 if (einit
.family
== 0 || einit
.name
== NULL
||
196 strlen(einit
.name
) >= IFNAMSIZ
||
197 (einit
.type
& 0xFFFFFF00) != 0 || einit
.type
== 0)
201 if (einit
.flags
& IFNET_INIT_LEGACY
) {
202 if (einit
.output
== NULL
||
203 (einit
.flags
& IFNET_INIT_INPUT_POLL
))
206 einit
.pre_enqueue
= NULL
;
208 einit
.output_ctl
= NULL
;
209 einit
.output_sched_model
= IFNET_SCHED_MODEL_NORMAL
;
210 einit
.input_poll
= NULL
;
211 einit
.input_ctl
= NULL
;
213 if (einit
.start
== NULL
)
217 if (einit
.output_sched_model
>= IFNET_SCHED_MODEL_MAX
)
220 if (einit
.flags
& IFNET_INIT_INPUT_POLL
) {
221 if (einit
.input_poll
== NULL
|| einit
.input_ctl
== NULL
)
224 einit
.input_poll
= NULL
;
225 einit
.input_ctl
= NULL
;
230 /* Initialize external name (name + unit) */
231 (void) snprintf(if_xname
, sizeof (if_xname
), "%s%d",
232 einit
.name
, einit
.unit
);
234 if (einit
.uniqueid
== NULL
) {
235 einit
.uniqueid
= if_xname
;
236 einit
.uniqueid_len
= strlen(if_xname
);
239 error
= dlil_if_acquire(einit
.family
, einit
.uniqueid
,
240 einit
.uniqueid_len
, if_xname
, &ifp
);
246 * Cast ifp->if_name as non const. dlil_if_acquire sets it up
247 * to point to storage of at least IFNAMSIZ bytes. It is safe
250 strlcpy(__DECONST(char *, ifp
->if_name
), einit
.name
, IFNAMSIZ
);
251 ifp
->if_type
= einit
.type
;
252 ifp
->if_family
= einit
.family
;
253 ifp
->if_subfamily
= einit
.subfamily
;
254 ifp
->if_unit
= einit
.unit
;
255 ifp
->if_output
= einit
.output
;
256 ifp
->if_pre_enqueue
= einit
.pre_enqueue
;
257 ifp
->if_start
= einit
.start
;
258 ifp
->if_output_ctl
= einit
.output_ctl
;
259 ifp
->if_output_sched_model
= einit
.output_sched_model
;
260 ifp
->if_output_bw
.eff_bw
= einit
.output_bw
;
261 ifp
->if_output_bw
.max_bw
= einit
.output_bw_max
;
262 ifp
->if_output_lt
.eff_lt
= einit
.output_lt
;
263 ifp
->if_output_lt
.max_lt
= einit
.output_lt_max
;
264 ifp
->if_input_poll
= einit
.input_poll
;
265 ifp
->if_input_ctl
= einit
.input_ctl
;
266 ifp
->if_input_bw
.eff_bw
= einit
.input_bw
;
267 ifp
->if_input_bw
.max_bw
= einit
.input_bw_max
;
268 ifp
->if_input_lt
.eff_lt
= einit
.input_lt
;
269 ifp
->if_input_lt
.max_lt
= einit
.input_lt_max
;
270 ifp
->if_demux
= einit
.demux
;
271 ifp
->if_add_proto
= einit
.add_proto
;
272 ifp
->if_del_proto
= einit
.del_proto
;
273 ifp
->if_check_multi
= einit
.check_multi
;
274 ifp
->if_framer_legacy
= einit
.framer
;
275 ifp
->if_framer
= einit
.framer_extended
;
276 ifp
->if_softc
= einit
.softc
;
277 ifp
->if_ioctl
= einit
.ioctl
;
278 ifp
->if_set_bpf_tap
= einit
.set_bpf_tap
;
279 ifp
->if_free
= ifnet_kpi_free
;
280 ifp
->if_event
= einit
.event
;
281 ifp
->if_kpi_storage
= einit
.detach
;
283 /* Initialize external name (name + unit) */
284 snprintf(__DECONST(char *, ifp
->if_xname
), IFXNAMSIZ
,
288 * On embedded, framer() is already in the extended form;
289 * we simply use it as is, unless the caller specifies
290 * framer_extended() which will then override it.
292 * On non-embedded, framer() has long been exposed as part
293 * of the public KPI, and therefore its signature must
294 * remain the same (without the pre- and postpend length
295 * parameters.) We special case ether_frameout, such that
296 * it gets mapped to its extended variant. All other cases
297 * utilize the stub routine which will simply return zeroes
298 * for those new parameters.
300 * Internally, DLIL will only use the extended callback
301 * variant which is represented by if_framer.
304 if (ifp
->if_framer
== NULL
&& ifp
->if_framer_legacy
!= NULL
)
305 ifp
->if_framer
= ifp
->if_framer_legacy
;
306 #else /* !CONFIG_EMBEDDED */
307 if (ifp
->if_framer
== NULL
&& ifp
->if_framer_legacy
!= NULL
) {
308 if (ifp
->if_framer_legacy
== ether_frameout
)
309 ifp
->if_framer
= ether_frameout_extended
;
311 ifp
->if_framer
= ifnet_framer_stub
;
313 #endif /* !CONFIG_EMBEDDED */
315 if (ifp
->if_output_bw
.eff_bw
> ifp
->if_output_bw
.max_bw
)
316 ifp
->if_output_bw
.max_bw
= ifp
->if_output_bw
.eff_bw
;
317 else if (ifp
->if_output_bw
.eff_bw
== 0)
318 ifp
->if_output_bw
.eff_bw
= ifp
->if_output_bw
.max_bw
;
320 if (ifp
->if_input_bw
.eff_bw
> ifp
->if_input_bw
.max_bw
)
321 ifp
->if_input_bw
.max_bw
= ifp
->if_input_bw
.eff_bw
;
322 else if (ifp
->if_input_bw
.eff_bw
== 0)
323 ifp
->if_input_bw
.eff_bw
= ifp
->if_input_bw
.max_bw
;
325 if (ifp
->if_output_bw
.max_bw
== 0)
326 ifp
->if_output_bw
= ifp
->if_input_bw
;
327 else if (ifp
->if_input_bw
.max_bw
== 0)
328 ifp
->if_input_bw
= ifp
->if_output_bw
;
330 /* Pin if_baudrate to 32 bits */
331 br
= MAX(ifp
->if_output_bw
.max_bw
, ifp
->if_input_bw
.max_bw
);
333 ifp
->if_baudrate
= (br
> 0xFFFFFFFF) ? 0xFFFFFFFF : br
;
335 if (ifp
->if_output_lt
.eff_lt
> ifp
->if_output_lt
.max_lt
)
336 ifp
->if_output_lt
.max_lt
= ifp
->if_output_lt
.eff_lt
;
337 else if (ifp
->if_output_lt
.eff_lt
== 0)
338 ifp
->if_output_lt
.eff_lt
= ifp
->if_output_lt
.max_lt
;
340 if (ifp
->if_input_lt
.eff_lt
> ifp
->if_input_lt
.max_lt
)
341 ifp
->if_input_lt
.max_lt
= ifp
->if_input_lt
.eff_lt
;
342 else if (ifp
->if_input_lt
.eff_lt
== 0)
343 ifp
->if_input_lt
.eff_lt
= ifp
->if_input_lt
.max_lt
;
345 if (ifp
->if_output_lt
.max_lt
== 0)
346 ifp
->if_output_lt
= ifp
->if_input_lt
;
347 else if (ifp
->if_input_lt
.max_lt
== 0)
348 ifp
->if_input_lt
= ifp
->if_output_lt
;
350 if (ifp
->if_ioctl
== NULL
)
351 ifp
->if_ioctl
= ifp_if_ioctl
;
354 if (ifp
->if_start
!= NULL
) {
355 ifp
->if_eflags
|= IFEF_TXSTART
;
356 if (ifp
->if_pre_enqueue
== NULL
)
357 ifp
->if_pre_enqueue
= ifnet_enqueue
;
358 ifp
->if_output
= ifp
->if_pre_enqueue
;
360 ifp
->if_eflags
&= ~IFEF_TXSTART
;
363 if (ifp
->if_input_poll
!= NULL
)
364 ifp
->if_eflags
|= IFEF_RXPOLL
;
366 ifp
->if_eflags
&= ~IFEF_RXPOLL
;
368 ifp
->if_output_dlil
= dlil_output_handler
;
369 ifp
->if_input_dlil
= dlil_input_handler
;
371 VERIFY(!(einit
.flags
& IFNET_INIT_LEGACY
) ||
372 (ifp
->if_pre_enqueue
== NULL
&& ifp
->if_start
== NULL
&&
373 ifp
->if_output_ctl
== NULL
&& ifp
->if_input_poll
== NULL
&&
374 ifp
->if_input_ctl
== NULL
));
375 VERIFY(!(einit
.flags
& IFNET_INIT_INPUT_POLL
) ||
376 (ifp
->if_input_poll
!= NULL
&& ifp
->if_input_ctl
!= NULL
));
378 if (einit
.broadcast_len
&& einit
.broadcast_addr
) {
379 if (einit
.broadcast_len
>
380 sizeof (ifp
->if_broadcast
.u
.buffer
)) {
381 MALLOC(ifp
->if_broadcast
.u
.ptr
, u_char
*,
382 einit
.broadcast_len
, M_IFADDR
, M_NOWAIT
);
383 if (ifp
->if_broadcast
.u
.ptr
== NULL
) {
386 bcopy(einit
.broadcast_addr
,
387 ifp
->if_broadcast
.u
.ptr
,
388 einit
.broadcast_len
);
391 bcopy(einit
.broadcast_addr
,
392 ifp
->if_broadcast
.u
.buffer
,
393 einit
.broadcast_len
);
395 ifp
->if_broadcast
.length
= einit
.broadcast_len
;
397 bzero(&ifp
->if_broadcast
, sizeof (ifp
->if_broadcast
));
403 * output target queue delay is specified in millisecond
404 * convert it to nanoseconds
406 IFCQ_TARGET_QDELAY(&ifp
->if_snd
) =
407 einit
.output_target_qdelay
* 1000 * 1000;
408 IFCQ_MAXLEN(&ifp
->if_snd
) = einit
.sndq_maxlen
;
410 ifnet_enqueue_multi_setup(ifp
, einit
.start_delay_qlen
,
411 einit
.start_delay_timeout
);
413 IFCQ_PKT_DROP_LIMIT(&ifp
->if_snd
) = IFCQ_DEFAULT_PKT_DROP_LIMIT
;
416 * Set embryonic flag; this will be cleared
417 * later when it is fully attached.
419 ifp
->if_refflags
= IFRF_EMBRYONIC
;
422 * Count the newly allocated ifnet
424 OSIncrementAtomic64(&net_api_stats
.nas_ifnet_alloc_count
);
425 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_ifnet_alloc_total
);
426 if (einit
.flags
& IFNET_INIT_ALLOC_KPI
) {
427 ifp
->if_xflags
|= IFXF_ALLOC_KPI
;
430 &net_api_stats
.nas_ifnet_alloc_os_count
);
431 INC_ATOMIC_INT64_LIM(
432 net_api_stats
.nas_ifnet_alloc_os_total
);
437 // temporary - this should be done in dlil_if_acquire
438 ifnet_reference(ifp
);
440 dlil_if_release(ifp
);
448 ifnet_reference(ifnet_t ifp
)
450 return (dlil_if_ref(ifp
));
454 ifnet_release(ifnet_t ifp
)
456 return (dlil_if_free(ifp
));
460 ifnet_interface_family_find(const char *module_string
,
461 ifnet_family_t
*family_id
)
463 if (module_string
== NULL
|| family_id
== NULL
)
466 return (net_str_id_find_internal(module_string
, family_id
,
471 ifnet_softc(ifnet_t interface
)
473 return ((interface
== NULL
) ? NULL
: interface
->if_softc
);
477 ifnet_name(ifnet_t interface
)
479 return ((interface
== NULL
) ? NULL
: interface
->if_name
);
483 ifnet_family(ifnet_t interface
)
485 return ((interface
== NULL
) ? 0 : interface
->if_family
);
489 ifnet_subfamily(ifnet_t interface
)
491 return ((interface
== NULL
) ? 0 : interface
->if_subfamily
);
495 ifnet_unit(ifnet_t interface
)
497 return ((interface
== NULL
) ? (u_int32_t
)0xffffffff :
498 (u_int32_t
)interface
->if_unit
);
502 ifnet_index(ifnet_t interface
)
504 return ((interface
== NULL
) ? (u_int32_t
)0xffffffff :
505 interface
->if_index
);
509 ifnet_set_flags(ifnet_t interface
, u_int16_t new_flags
, u_int16_t mask
)
513 if (interface
== NULL
)
516 ifnet_lock_exclusive(interface
);
518 /* If we are modifying the up/down state, call if_updown */
519 if ((mask
& IFF_UP
) != 0) {
520 if_updown(interface
, (new_flags
& IFF_UP
) == IFF_UP
);
523 old_flags
= interface
->if_flags
;
524 interface
->if_flags
= (new_flags
& mask
) | (interface
->if_flags
& ~mask
);
525 /* If we are modifying the multicast flag, set/unset the silent flag */
526 if ((old_flags
& IFF_MULTICAST
) !=
527 (interface
->if_flags
& IFF_MULTICAST
)) {
529 if (IGMP_IFINFO(interface
) != NULL
)
530 igmp_initsilent(interface
, IGMP_IFINFO(interface
));
533 if (MLD_IFINFO(interface
) != NULL
)
534 mld6_initsilent(interface
, MLD_IFINFO(interface
));
538 ifnet_lock_done(interface
);
544 ifnet_flags(ifnet_t interface
)
546 return ((interface
== NULL
) ? 0 : interface
->if_flags
);
550 * This routine ensures the following:
552 * If IFEF_AWDL is set by the caller, also set the rest of flags as
553 * defined in IFEF_AWDL_MASK.
555 * If IFEF_AWDL has been set on the interface and the caller attempts
556 * to clear one or more of the associated flags in IFEF_AWDL_MASK,
559 * If IFEF_AWDL_RESTRICTED is set by the caller, make sure IFEF_AWDL is set
562 * All other flags not associated with AWDL are not affected.
564 * See <net/if.h> for current definition of IFEF_AWDL_MASK.
567 ifnet_awdl_check_eflags(ifnet_t ifp
, u_int32_t
*new_eflags
, u_int32_t
*mask
)
571 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
573 eflags
= (*new_eflags
& *mask
) | (ifp
->if_eflags
& ~(*mask
));
575 if (ifp
->if_eflags
& IFEF_AWDL
) {
576 if (eflags
& IFEF_AWDL
) {
577 if ((eflags
& IFEF_AWDL_MASK
) != IFEF_AWDL_MASK
)
580 *new_eflags
&= ~IFEF_AWDL_MASK
;
581 *mask
|= IFEF_AWDL_MASK
;
583 } else if (eflags
& IFEF_AWDL
) {
584 *new_eflags
|= IFEF_AWDL_MASK
;
585 *mask
|= IFEF_AWDL_MASK
;
586 } else if (eflags
& IFEF_AWDL_RESTRICTED
&&
587 !(ifp
->if_eflags
& IFEF_AWDL
))
594 ifnet_set_eflags(ifnet_t interface
, u_int32_t new_flags
, u_int32_t mask
)
597 struct kev_msg ev_msg
;
598 struct net_event_data ev_data
;
600 if (interface
== NULL
)
603 bzero(&ev_msg
, sizeof(ev_msg
));
604 ifnet_lock_exclusive(interface
);
606 * Sanity checks for IFEF_AWDL and its related flags.
608 if (ifnet_awdl_check_eflags(interface
, &new_flags
, &mask
) != 0) {
609 ifnet_lock_done(interface
);
612 oeflags
= interface
->if_eflags
;
613 interface
->if_eflags
=
614 (new_flags
& mask
) | (interface
->if_eflags
& ~mask
);
615 ifnet_lock_done(interface
);
616 if (interface
->if_eflags
& IFEF_AWDL_RESTRICTED
&&
617 !(oeflags
& IFEF_AWDL_RESTRICTED
)) {
618 ev_msg
.event_code
= KEV_DL_AWDL_RESTRICTED
;
620 * The interface is now restricted to applications that have
622 * The check for the entitlement will be done in the data
623 * path, so we don't have to do anything here.
625 } else if (oeflags
& IFEF_AWDL_RESTRICTED
&&
626 !(interface
->if_eflags
& IFEF_AWDL_RESTRICTED
))
627 ev_msg
.event_code
= KEV_DL_AWDL_UNRESTRICTED
;
629 * Notify configd so that it has a chance to perform better
630 * reachability detection.
632 if (ev_msg
.event_code
) {
633 bzero(&ev_data
, sizeof(ev_data
));
634 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
635 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
636 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
637 strlcpy(ev_data
.if_name
, interface
->if_name
, IFNAMSIZ
);
638 ev_data
.if_family
= interface
->if_family
;
639 ev_data
.if_unit
= interface
->if_unit
;
640 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
641 ev_msg
.dv
[0].data_ptr
= &ev_data
;
642 ev_msg
.dv
[1].data_length
= 0;
643 dlil_post_complete_msg(interface
, &ev_msg
);
650 ifnet_eflags(ifnet_t interface
)
652 return ((interface
== NULL
) ? 0 : interface
->if_eflags
);
656 ifnet_set_idle_flags_locked(ifnet_t ifp
, u_int32_t new_flags
, u_int32_t mask
)
663 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
664 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_EXCLUSIVE
);
667 * If this is called prior to ifnet attach, the actual work will
668 * be done at attach time. Otherwise, if it is called after
669 * ifnet detach, then it is a no-op.
671 if (!ifnet_is_attached(ifp
, 0)) {
672 ifp
->if_idle_new_flags
= new_flags
;
673 ifp
->if_idle_new_flags_mask
= mask
;
676 ifp
->if_idle_new_flags
= ifp
->if_idle_new_flags_mask
= 0;
679 before
= ifp
->if_idle_flags
;
680 ifp
->if_idle_flags
= (new_flags
& mask
) | (ifp
->if_idle_flags
& ~mask
);
681 after
= ifp
->if_idle_flags
;
683 if ((after
- before
) < 0 && ifp
->if_idle_flags
== 0 &&
684 ifp
->if_want_aggressive_drain
!= 0) {
685 ifp
->if_want_aggressive_drain
= 0;
686 } else if ((after
- before
) > 0 && ifp
->if_want_aggressive_drain
== 0) {
687 ifp
->if_want_aggressive_drain
++;
694 ifnet_set_idle_flags(ifnet_t ifp
, u_int32_t new_flags
, u_int32_t mask
)
698 lck_mtx_lock(rnh_lock
);
699 ifnet_lock_exclusive(ifp
);
700 err
= ifnet_set_idle_flags_locked(ifp
, new_flags
, mask
);
701 ifnet_lock_done(ifp
);
702 lck_mtx_unlock(rnh_lock
);
708 ifnet_idle_flags(ifnet_t ifp
)
710 return ((ifp
== NULL
) ? 0 : ifp
->if_idle_flags
);
714 ifnet_set_link_quality(ifnet_t ifp
, int quality
)
718 if (ifp
== NULL
|| quality
< IFNET_LQM_MIN
|| quality
> IFNET_LQM_MAX
) {
723 if (!ifnet_is_attached(ifp
, 0)) {
728 if_lqm_update(ifp
, quality
, 0);
735 ifnet_link_quality(ifnet_t ifp
)
740 return (IFNET_LQM_THRESH_OFF
);
742 ifnet_lock_shared(ifp
);
743 lqm
= ifp
->if_interface_state
.lqm_state
;
744 ifnet_lock_done(ifp
);
750 ifnet_set_interface_state(ifnet_t ifp
,
751 struct if_interface_state
*if_interface_state
)
755 if (ifp
== NULL
|| if_interface_state
== NULL
) {
760 if (!ifnet_is_attached(ifp
, 0)) {
765 if_state_update(ifp
, if_interface_state
);
772 ifnet_get_interface_state(ifnet_t ifp
,
773 struct if_interface_state
*if_interface_state
)
777 if (ifp
== NULL
|| if_interface_state
== NULL
) {
782 if (!ifnet_is_attached(ifp
, 0)) {
787 if_get_state(ifp
, if_interface_state
);
795 ifnet_defrouter_llreachinfo(ifnet_t ifp
, int af
,
796 struct ifnet_llreach_info
*iflri
)
798 if (ifp
== NULL
|| iflri
== NULL
)
801 VERIFY(af
== AF_INET
|| af
== AF_INET6
);
803 return (ifnet_llreach_get_defrouter(ifp
, af
, iflri
));
807 ifnet_inet_defrouter_llreachinfo(ifnet_t ifp
, struct ifnet_llreach_info
*iflri
)
809 return (ifnet_defrouter_llreachinfo(ifp
, AF_INET
, iflri
));
813 ifnet_inet6_defrouter_llreachinfo(ifnet_t ifp
, struct ifnet_llreach_info
*iflri
)
815 return (ifnet_defrouter_llreachinfo(ifp
, AF_INET6
, iflri
));
819 ifnet_set_capabilities_supported(ifnet_t ifp
, u_int32_t new_caps
,
828 ifnet_lock_exclusive(ifp
);
829 tmp
= (new_caps
& mask
) | (ifp
->if_capabilities
& ~mask
);
830 if ((tmp
& ~IFCAP_VALID
))
833 ifp
->if_capabilities
= tmp
;
834 ifnet_lock_done(ifp
);
840 ifnet_capabilities_supported(ifnet_t ifp
)
842 return ((ifp
== NULL
) ? 0 : ifp
->if_capabilities
);
847 ifnet_set_capabilities_enabled(ifnet_t ifp
, u_int32_t new_caps
,
852 struct kev_msg ev_msg
;
853 struct net_event_data ev_data
;
858 ifnet_lock_exclusive(ifp
);
859 tmp
= (new_caps
& mask
) | (ifp
->if_capenable
& ~mask
);
860 if ((tmp
& ~IFCAP_VALID
) || (tmp
& ~ifp
->if_capabilities
))
863 ifp
->if_capenable
= tmp
;
864 ifnet_lock_done(ifp
);
866 /* Notify application of the change */
867 bzero(&ev_data
, sizeof (struct net_event_data
));
868 bzero(&ev_msg
, sizeof (struct kev_msg
));
869 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
870 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
871 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
873 ev_msg
.event_code
= KEV_DL_IFCAP_CHANGED
;
874 strlcpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
875 ev_data
.if_family
= ifp
->if_family
;
876 ev_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
877 ev_msg
.dv
[0].data_length
= sizeof (struct net_event_data
);
878 ev_msg
.dv
[0].data_ptr
= &ev_data
;
879 ev_msg
.dv
[1].data_length
= 0;
880 dlil_post_complete_msg(ifp
, &ev_msg
);
886 ifnet_capabilities_enabled(ifnet_t ifp
)
888 return ((ifp
== NULL
) ? 0 : ifp
->if_capenable
);
891 static const ifnet_offload_t offload_mask
=
892 (IFNET_CSUM_IP
| IFNET_CSUM_TCP
| IFNET_CSUM_UDP
| IFNET_CSUM_FRAGMENT
|
893 IFNET_IP_FRAGMENT
| IFNET_CSUM_TCPIPV6
| IFNET_CSUM_UDPIPV6
|
894 IFNET_IPV6_FRAGMENT
| IFNET_CSUM_PARTIAL
| IFNET_CSUM_ZERO_INVERT
|
895 IFNET_VLAN_TAGGING
| IFNET_VLAN_MTU
| IFNET_MULTIPAGES
|
896 IFNET_TSO_IPV4
| IFNET_TSO_IPV6
| IFNET_TX_STATUS
| IFNET_HW_TIMESTAMP
|
899 static const ifnet_offload_t any_offload_csum
= IFNET_CHECKSUMF
;
902 ifnet_set_offload(ifnet_t interface
, ifnet_offload_t offload
)
904 u_int32_t ifcaps
= 0;
906 if (interface
== NULL
)
909 ifnet_lock_exclusive(interface
);
910 interface
->if_hwassist
= (offload
& offload_mask
);
913 * Hardware capable of partial checksum offload is
914 * flexible enough to handle any transports utilizing
915 * Internet Checksumming. Include those transports
916 * here, and leave the final decision to IP.
918 if (interface
->if_hwassist
& IFNET_CSUM_PARTIAL
) {
919 interface
->if_hwassist
|= (IFNET_CSUM_TCP
| IFNET_CSUM_UDP
|
920 IFNET_CSUM_TCPIPV6
| IFNET_CSUM_UDPIPV6
);
923 log(LOG_DEBUG
, "%s: set offload flags=%b\n",
925 interface
->if_hwassist
, IFNET_OFFLOADF_BITS
);
927 ifnet_lock_done(interface
);
929 if ((offload
& any_offload_csum
))
930 ifcaps
|= IFCAP_HWCSUM
;
931 if ((offload
& IFNET_TSO_IPV4
))
932 ifcaps
|= IFCAP_TSO4
;
933 if ((offload
& IFNET_TSO_IPV6
))
934 ifcaps
|= IFCAP_TSO6
;
935 if ((offload
& IFNET_VLAN_MTU
))
936 ifcaps
|= IFCAP_VLAN_MTU
;
937 if ((offload
& IFNET_VLAN_TAGGING
))
938 ifcaps
|= IFCAP_VLAN_HWTAGGING
;
939 if ((offload
& IFNET_TX_STATUS
))
940 ifcaps
|= IFCAP_TXSTATUS
;
941 if ((offload
& IFNET_HW_TIMESTAMP
))
942 ifcaps
|= IFCAP_HW_TIMESTAMP
;
943 if ((offload
& IFNET_SW_TIMESTAMP
))
944 ifcaps
|= IFCAP_SW_TIMESTAMP
;
945 if ((offload
& IFNET_CSUM_PARTIAL
))
946 ifcaps
|= IFCAP_CSUM_PARTIAL
;
947 if ((offload
& IFNET_CSUM_ZERO_INVERT
))
948 ifcaps
|= IFCAP_CSUM_ZERO_INVERT
;
950 (void) ifnet_set_capabilities_supported(interface
, ifcaps
,
952 (void) ifnet_set_capabilities_enabled(interface
, ifcaps
,
960 ifnet_offload(ifnet_t interface
)
962 return ((interface
== NULL
) ?
963 0 : (interface
->if_hwassist
& offload_mask
));
967 ifnet_set_tso_mtu(ifnet_t interface
, sa_family_t family
, u_int32_t mtuLen
)
971 if (interface
== NULL
|| mtuLen
< interface
->if_mtu
)
976 if (interface
->if_hwassist
& IFNET_TSO_IPV4
)
977 interface
->if_tso_v4_mtu
= mtuLen
;
983 if (interface
->if_hwassist
& IFNET_TSO_IPV6
)
984 interface
->if_tso_v6_mtu
= mtuLen
;
990 error
= EPROTONOSUPPORT
;
998 ifnet_get_tso_mtu(ifnet_t interface
, sa_family_t family
, u_int32_t
*mtuLen
)
1002 if (interface
== NULL
|| mtuLen
== NULL
)
1007 if (interface
->if_hwassist
& IFNET_TSO_IPV4
)
1008 *mtuLen
= interface
->if_tso_v4_mtu
;
1014 if (interface
->if_hwassist
& IFNET_TSO_IPV6
)
1015 *mtuLen
= interface
->if_tso_v6_mtu
;
1021 error
= EPROTONOSUPPORT
;
1029 ifnet_set_wake_flags(ifnet_t interface
, u_int32_t properties
, u_int32_t mask
)
1031 struct kev_msg ev_msg
;
1032 struct net_event_data ev_data
;
1034 bzero(&ev_data
, sizeof (struct net_event_data
));
1035 bzero(&ev_msg
, sizeof (struct kev_msg
));
1037 if (interface
== NULL
)
1040 /* Do not accept wacky values */
1041 if ((properties
& mask
) & ~IF_WAKE_VALID_FLAGS
)
1044 ifnet_lock_exclusive(interface
);
1046 if (mask
& IF_WAKE_ON_MAGIC_PACKET
) {
1047 if (properties
& IF_WAKE_ON_MAGIC_PACKET
)
1048 interface
->if_xflags
|= IFXF_WAKE_ON_MAGIC_PACKET
;
1050 interface
->if_xflags
&= ~IFXF_WAKE_ON_MAGIC_PACKET
;
1053 ifnet_lock_done(interface
);
1055 (void) ifnet_touch_lastchange(interface
);
1057 /* Notify application of the change */
1058 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1059 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1060 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1062 ev_msg
.event_code
= KEV_DL_WAKEFLAGS_CHANGED
;
1063 strlcpy(&ev_data
.if_name
[0], interface
->if_name
, IFNAMSIZ
);
1064 ev_data
.if_family
= interface
->if_family
;
1065 ev_data
.if_unit
= (u_int32_t
)interface
->if_unit
;
1066 ev_msg
.dv
[0].data_length
= sizeof (struct net_event_data
);
1067 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1068 ev_msg
.dv
[1].data_length
= 0;
1069 dlil_post_complete_msg(interface
, &ev_msg
);
1075 ifnet_get_wake_flags(ifnet_t interface
)
1077 u_int32_t flags
= 0;
1079 if (interface
== NULL
)
1082 if (interface
->if_xflags
& IFXF_WAKE_ON_MAGIC_PACKET
)
1083 flags
|= IF_WAKE_ON_MAGIC_PACKET
;
1089 * Should MIB data store a copy?
1092 ifnet_set_link_mib_data(ifnet_t interface
, void *mibData
, u_int32_t mibLen
)
1094 if (interface
== NULL
)
1097 ifnet_lock_exclusive(interface
);
1098 interface
->if_linkmib
= (void*)mibData
;
1099 interface
->if_linkmiblen
= mibLen
;
1100 ifnet_lock_done(interface
);
1105 ifnet_get_link_mib_data(ifnet_t interface
, void *mibData
, u_int32_t
*mibLen
)
1109 if (interface
== NULL
)
1112 ifnet_lock_shared(interface
);
1113 if (*mibLen
< interface
->if_linkmiblen
)
1115 if (result
== 0 && interface
->if_linkmib
== NULL
)
1119 *mibLen
= interface
->if_linkmiblen
;
1120 bcopy(interface
->if_linkmib
, mibData
, *mibLen
);
1122 ifnet_lock_done(interface
);
1128 ifnet_get_link_mib_data_length(ifnet_t interface
)
1130 return ((interface
== NULL
) ? 0 : interface
->if_linkmiblen
);
1134 ifnet_output(ifnet_t interface
, protocol_family_t protocol_family
,
1135 mbuf_t m
, void *route
, const struct sockaddr
*dest
)
1137 if (interface
== NULL
|| protocol_family
== 0 || m
== NULL
) {
1142 return (dlil_output(interface
, protocol_family
, m
, route
, dest
, 0, NULL
));
1146 ifnet_output_raw(ifnet_t interface
, protocol_family_t protocol_family
, mbuf_t m
)
1148 if (interface
== NULL
|| m
== NULL
) {
1153 return (dlil_output(interface
, protocol_family
, m
, NULL
, NULL
, 1, NULL
));
1157 ifnet_set_mtu(ifnet_t interface
, u_int32_t mtu
)
1159 if (interface
== NULL
)
1162 interface
->if_mtu
= mtu
;
1167 ifnet_mtu(ifnet_t interface
)
1169 return ((interface
== NULL
) ? 0 : interface
->if_mtu
);
1173 ifnet_type(ifnet_t interface
)
1175 return ((interface
== NULL
) ? 0 : interface
->if_data
.ifi_type
);
1179 ifnet_set_addrlen(ifnet_t interface
, u_char addrlen
)
1181 if (interface
== NULL
)
1184 interface
->if_data
.ifi_addrlen
= addrlen
;
1189 ifnet_addrlen(ifnet_t interface
)
1191 return ((interface
== NULL
) ? 0 : interface
->if_data
.ifi_addrlen
);
1195 ifnet_set_hdrlen(ifnet_t interface
, u_char hdrlen
)
1197 if (interface
== NULL
)
1200 interface
->if_data
.ifi_hdrlen
= hdrlen
;
1205 ifnet_hdrlen(ifnet_t interface
)
1207 return ((interface
== NULL
) ? 0 : interface
->if_data
.ifi_hdrlen
);
1211 ifnet_set_metric(ifnet_t interface
, u_int32_t metric
)
1213 if (interface
== NULL
)
1216 interface
->if_data
.ifi_metric
= metric
;
1221 ifnet_metric(ifnet_t interface
)
1223 return ((interface
== NULL
) ? 0 : interface
->if_data
.ifi_metric
);
1227 ifnet_set_baudrate(struct ifnet
*ifp
, u_int64_t baudrate
)
1232 ifp
->if_output_bw
.max_bw
= ifp
->if_input_bw
.max_bw
=
1233 ifp
->if_output_bw
.eff_bw
= ifp
->if_input_bw
.eff_bw
= baudrate
;
1235 /* Pin if_baudrate to 32 bits until we can change the storage size */
1236 ifp
->if_baudrate
= (baudrate
> 0xFFFFFFFF) ? 0xFFFFFFFF : baudrate
;
1242 ifnet_baudrate(struct ifnet
*ifp
)
1244 return ((ifp
== NULL
) ? 0 : ifp
->if_baudrate
);
1248 ifnet_set_bandwidths(struct ifnet
*ifp
, struct if_bandwidths
*output_bw
,
1249 struct if_bandwidths
*input_bw
)
1254 /* set input values first (if any), as output values depend on them */
1255 if (input_bw
!= NULL
)
1256 (void) ifnet_set_input_bandwidths(ifp
, input_bw
);
1258 if (output_bw
!= NULL
)
1259 (void) ifnet_set_output_bandwidths(ifp
, output_bw
, FALSE
);
1265 ifnet_set_link_status_outbw(struct ifnet
*ifp
)
1267 struct if_wifi_status_v1
*sr
;
1268 sr
= &ifp
->if_link_status
->ifsr_u
.ifsr_wifi
.if_wifi_u
.if_status_v1
;
1269 if (ifp
->if_output_bw
.eff_bw
!= 0) {
1270 sr
->valid_bitmask
|=
1271 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID
;
1272 sr
->ul_effective_bandwidth
=
1273 ifp
->if_output_bw
.eff_bw
;
1275 if (ifp
->if_output_bw
.max_bw
!= 0) {
1276 sr
->valid_bitmask
|=
1277 IF_WIFI_UL_MAX_BANDWIDTH_VALID
;
1278 sr
->ul_max_bandwidth
=
1279 ifp
->if_output_bw
.max_bw
;
1284 ifnet_set_output_bandwidths(struct ifnet
*ifp
, struct if_bandwidths
*bw
,
1287 struct if_bandwidths old_bw
;
1288 struct ifclassq
*ifq
;
1291 VERIFY(ifp
!= NULL
&& bw
!= NULL
);
1296 IFCQ_LOCK_ASSERT_HELD(ifq
);
1298 old_bw
= ifp
->if_output_bw
;
1299 if (bw
->eff_bw
!= 0)
1300 ifp
->if_output_bw
.eff_bw
= bw
->eff_bw
;
1301 if (bw
->max_bw
!= 0)
1302 ifp
->if_output_bw
.max_bw
= bw
->max_bw
;
1303 if (ifp
->if_output_bw
.eff_bw
> ifp
->if_output_bw
.max_bw
)
1304 ifp
->if_output_bw
.max_bw
= ifp
->if_output_bw
.eff_bw
;
1305 else if (ifp
->if_output_bw
.eff_bw
== 0)
1306 ifp
->if_output_bw
.eff_bw
= ifp
->if_output_bw
.max_bw
;
1308 /* Pin if_baudrate to 32 bits */
1309 br
= MAX(ifp
->if_output_bw
.max_bw
, ifp
->if_input_bw
.max_bw
);
1311 ifp
->if_baudrate
= (br
> 0xFFFFFFFF) ? 0xFFFFFFFF : br
;
1313 /* Adjust queue parameters if needed */
1314 if (old_bw
.eff_bw
!= ifp
->if_output_bw
.eff_bw
||
1315 old_bw
.max_bw
!= ifp
->if_output_bw
.max_bw
)
1316 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_BANDWIDTH
);
1322 * If this is a Wifi interface, update the values in
1323 * if_link_status structure also.
1325 if (IFNET_IS_WIFI(ifp
) && ifp
->if_link_status
!= NULL
) {
1326 lck_rw_lock_exclusive(&ifp
->if_link_status_lock
);
1327 ifnet_set_link_status_outbw(ifp
);
1328 lck_rw_done(&ifp
->if_link_status_lock
);
1335 ifnet_set_link_status_inbw(struct ifnet
*ifp
)
1337 struct if_wifi_status_v1
*sr
;
1339 sr
= &ifp
->if_link_status
->ifsr_u
.ifsr_wifi
.if_wifi_u
.if_status_v1
;
1340 if (ifp
->if_input_bw
.eff_bw
!= 0) {
1341 sr
->valid_bitmask
|=
1342 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID
;
1343 sr
->dl_effective_bandwidth
=
1344 ifp
->if_input_bw
.eff_bw
;
1346 if (ifp
->if_input_bw
.max_bw
!= 0) {
1347 sr
->valid_bitmask
|=
1348 IF_WIFI_DL_MAX_BANDWIDTH_VALID
;
1349 sr
->dl_max_bandwidth
= ifp
->if_input_bw
.max_bw
;
1354 ifnet_set_input_bandwidths(struct ifnet
*ifp
, struct if_bandwidths
*bw
)
1356 struct if_bandwidths old_bw
;
1358 VERIFY(ifp
!= NULL
&& bw
!= NULL
);
1360 old_bw
= ifp
->if_input_bw
;
1361 if (bw
->eff_bw
!= 0)
1362 ifp
->if_input_bw
.eff_bw
= bw
->eff_bw
;
1363 if (bw
->max_bw
!= 0)
1364 ifp
->if_input_bw
.max_bw
= bw
->max_bw
;
1365 if (ifp
->if_input_bw
.eff_bw
> ifp
->if_input_bw
.max_bw
)
1366 ifp
->if_input_bw
.max_bw
= ifp
->if_input_bw
.eff_bw
;
1367 else if (ifp
->if_input_bw
.eff_bw
== 0)
1368 ifp
->if_input_bw
.eff_bw
= ifp
->if_input_bw
.max_bw
;
1370 if (IFNET_IS_WIFI(ifp
) && ifp
->if_link_status
!= NULL
) {
1371 lck_rw_lock_exclusive(&ifp
->if_link_status_lock
);
1372 ifnet_set_link_status_inbw(ifp
);
1373 lck_rw_done(&ifp
->if_link_status_lock
);
1376 if (old_bw
.eff_bw
!= ifp
->if_input_bw
.eff_bw
||
1377 old_bw
.max_bw
!= ifp
->if_input_bw
.max_bw
)
1378 ifnet_update_rcv(ifp
, CLASSQ_EV_LINK_BANDWIDTH
);
1384 ifnet_output_linkrate(struct ifnet
*ifp
)
1386 struct ifclassq
*ifq
= &ifp
->if_snd
;
1389 IFCQ_LOCK_ASSERT_HELD(ifq
);
1391 rate
= ifp
->if_output_bw
.eff_bw
;
1392 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
1393 u_int64_t tbr_rate
= ifp
->if_snd
.ifcq_tbr
.tbr_rate_raw
;
1394 VERIFY(tbr_rate
> 0);
1395 rate
= MIN(rate
, ifp
->if_snd
.ifcq_tbr
.tbr_rate_raw
);
1402 ifnet_input_linkrate(struct ifnet
*ifp
)
1404 return (ifp
->if_input_bw
.eff_bw
);
1408 ifnet_bandwidths(struct ifnet
*ifp
, struct if_bandwidths
*output_bw
,
1409 struct if_bandwidths
*input_bw
)
1414 if (output_bw
!= NULL
)
1415 *output_bw
= ifp
->if_output_bw
;
1416 if (input_bw
!= NULL
)
1417 *input_bw
= ifp
->if_input_bw
;
1423 ifnet_set_latencies(struct ifnet
*ifp
, struct if_latencies
*output_lt
,
1424 struct if_latencies
*input_lt
)
1429 if (output_lt
!= NULL
)
1430 (void) ifnet_set_output_latencies(ifp
, output_lt
, FALSE
);
1432 if (input_lt
!= NULL
)
1433 (void) ifnet_set_input_latencies(ifp
, input_lt
);
1439 ifnet_set_output_latencies(struct ifnet
*ifp
, struct if_latencies
*lt
,
1442 struct if_latencies old_lt
;
1443 struct ifclassq
*ifq
;
1445 VERIFY(ifp
!= NULL
&& lt
!= NULL
);
1450 IFCQ_LOCK_ASSERT_HELD(ifq
);
1452 old_lt
= ifp
->if_output_lt
;
1453 if (lt
->eff_lt
!= 0)
1454 ifp
->if_output_lt
.eff_lt
= lt
->eff_lt
;
1455 if (lt
->max_lt
!= 0)
1456 ifp
->if_output_lt
.max_lt
= lt
->max_lt
;
1457 if (ifp
->if_output_lt
.eff_lt
> ifp
->if_output_lt
.max_lt
)
1458 ifp
->if_output_lt
.max_lt
= ifp
->if_output_lt
.eff_lt
;
1459 else if (ifp
->if_output_lt
.eff_lt
== 0)
1460 ifp
->if_output_lt
.eff_lt
= ifp
->if_output_lt
.max_lt
;
1462 /* Adjust queue parameters if needed */
1463 if (old_lt
.eff_lt
!= ifp
->if_output_lt
.eff_lt
||
1464 old_lt
.max_lt
!= ifp
->if_output_lt
.max_lt
)
1465 ifnet_update_sndq(ifq
, CLASSQ_EV_LINK_LATENCY
);
1474 ifnet_set_input_latencies(struct ifnet
*ifp
, struct if_latencies
*lt
)
1476 struct if_latencies old_lt
;
1478 VERIFY(ifp
!= NULL
&& lt
!= NULL
);
1480 old_lt
= ifp
->if_input_lt
;
1481 if (lt
->eff_lt
!= 0)
1482 ifp
->if_input_lt
.eff_lt
= lt
->eff_lt
;
1483 if (lt
->max_lt
!= 0)
1484 ifp
->if_input_lt
.max_lt
= lt
->max_lt
;
1485 if (ifp
->if_input_lt
.eff_lt
> ifp
->if_input_lt
.max_lt
)
1486 ifp
->if_input_lt
.max_lt
= ifp
->if_input_lt
.eff_lt
;
1487 else if (ifp
->if_input_lt
.eff_lt
== 0)
1488 ifp
->if_input_lt
.eff_lt
= ifp
->if_input_lt
.max_lt
;
1490 if (old_lt
.eff_lt
!= ifp
->if_input_lt
.eff_lt
||
1491 old_lt
.max_lt
!= ifp
->if_input_lt
.max_lt
)
1492 ifnet_update_rcv(ifp
, CLASSQ_EV_LINK_LATENCY
);
1498 ifnet_latencies(struct ifnet
*ifp
, struct if_latencies
*output_lt
,
1499 struct if_latencies
*input_lt
)
1504 if (output_lt
!= NULL
)
1505 *output_lt
= ifp
->if_output_lt
;
1506 if (input_lt
!= NULL
)
1507 *input_lt
= ifp
->if_input_lt
;
1513 ifnet_set_poll_params(struct ifnet
*ifp
, struct ifnet_poll_params
*p
)
1519 else if (!ifnet_is_attached(ifp
, 1))
1522 err
= dlil_rxpoll_set_params(ifp
, p
, FALSE
);
1524 /* Release the io ref count */
1525 ifnet_decr_iorefcnt(ifp
);
1531 ifnet_poll_params(struct ifnet
*ifp
, struct ifnet_poll_params
*p
)
1535 if (ifp
== NULL
|| p
== NULL
)
1537 else if (!ifnet_is_attached(ifp
, 1))
1540 err
= dlil_rxpoll_get_params(ifp
, p
);
1542 /* Release the io ref count */
1543 ifnet_decr_iorefcnt(ifp
);
1549 ifnet_stat_increment(struct ifnet
*ifp
,
1550 const struct ifnet_stat_increment_param
*s
)
1555 if (s
->packets_in
!= 0)
1556 atomic_add_64(&ifp
->if_data
.ifi_ipackets
, s
->packets_in
);
1557 if (s
->bytes_in
!= 0)
1558 atomic_add_64(&ifp
->if_data
.ifi_ibytes
, s
->bytes_in
);
1559 if (s
->errors_in
!= 0)
1560 atomic_add_64(&ifp
->if_data
.ifi_ierrors
, s
->errors_in
);
1562 if (s
->packets_out
!= 0)
1563 atomic_add_64(&ifp
->if_data
.ifi_opackets
, s
->packets_out
);
1564 if (s
->bytes_out
!= 0)
1565 atomic_add_64(&ifp
->if_data
.ifi_obytes
, s
->bytes_out
);
1566 if (s
->errors_out
!= 0)
1567 atomic_add_64(&ifp
->if_data
.ifi_oerrors
, s
->errors_out
);
1569 if (s
->collisions
!= 0)
1570 atomic_add_64(&ifp
->if_data
.ifi_collisions
, s
->collisions
);
1571 if (s
->dropped
!= 0)
1572 atomic_add_64(&ifp
->if_data
.ifi_iqdrops
, s
->dropped
);
1574 /* Touch the last change time. */
1575 TOUCHLASTCHANGE(&ifp
->if_lastchange
);
1577 if (ifp
->if_data_threshold
!= 0)
1578 ifnet_notify_data_threshold(ifp
);
1584 ifnet_stat_increment_in(struct ifnet
*ifp
, u_int32_t packets_in
,
1585 u_int32_t bytes_in
, u_int32_t errors_in
)
1590 if (packets_in
!= 0)
1591 atomic_add_64(&ifp
->if_data
.ifi_ipackets
, packets_in
);
1593 atomic_add_64(&ifp
->if_data
.ifi_ibytes
, bytes_in
);
1595 atomic_add_64(&ifp
->if_data
.ifi_ierrors
, errors_in
);
1597 TOUCHLASTCHANGE(&ifp
->if_lastchange
);
1599 if (ifp
->if_data_threshold
!= 0)
1600 ifnet_notify_data_threshold(ifp
);
1606 ifnet_stat_increment_out(struct ifnet
*ifp
, u_int32_t packets_out
,
1607 u_int32_t bytes_out
, u_int32_t errors_out
)
1612 if (packets_out
!= 0)
1613 atomic_add_64(&ifp
->if_data
.ifi_opackets
, packets_out
);
1615 atomic_add_64(&ifp
->if_data
.ifi_obytes
, bytes_out
);
1616 if (errors_out
!= 0)
1617 atomic_add_64(&ifp
->if_data
.ifi_oerrors
, errors_out
);
1619 TOUCHLASTCHANGE(&ifp
->if_lastchange
);
1621 if (ifp
->if_data_threshold
!= 0)
1622 ifnet_notify_data_threshold(ifp
);
1628 ifnet_set_stat(struct ifnet
*ifp
, const struct ifnet_stats_param
*s
)
1633 atomic_set_64(&ifp
->if_data
.ifi_ipackets
, s
->packets_in
);
1634 atomic_set_64(&ifp
->if_data
.ifi_ibytes
, s
->bytes_in
);
1635 atomic_set_64(&ifp
->if_data
.ifi_imcasts
, s
->multicasts_in
);
1636 atomic_set_64(&ifp
->if_data
.ifi_ierrors
, s
->errors_in
);
1638 atomic_set_64(&ifp
->if_data
.ifi_opackets
, s
->packets_out
);
1639 atomic_set_64(&ifp
->if_data
.ifi_obytes
, s
->bytes_out
);
1640 atomic_set_64(&ifp
->if_data
.ifi_omcasts
, s
->multicasts_out
);
1641 atomic_set_64(&ifp
->if_data
.ifi_oerrors
, s
->errors_out
);
1643 atomic_set_64(&ifp
->if_data
.ifi_collisions
, s
->collisions
);
1644 atomic_set_64(&ifp
->if_data
.ifi_iqdrops
, s
->dropped
);
1645 atomic_set_64(&ifp
->if_data
.ifi_noproto
, s
->no_protocol
);
1647 /* Touch the last change time. */
1648 TOUCHLASTCHANGE(&ifp
->if_lastchange
);
1650 if (ifp
->if_data_threshold
!= 0)
1651 ifnet_notify_data_threshold(ifp
);
1657 ifnet_stat(struct ifnet
*ifp
, struct ifnet_stats_param
*s
)
1662 atomic_get_64(s
->packets_in
, &ifp
->if_data
.ifi_ipackets
);
1663 atomic_get_64(s
->bytes_in
, &ifp
->if_data
.ifi_ibytes
);
1664 atomic_get_64(s
->multicasts_in
, &ifp
->if_data
.ifi_imcasts
);
1665 atomic_get_64(s
->errors_in
, &ifp
->if_data
.ifi_ierrors
);
1667 atomic_get_64(s
->packets_out
, &ifp
->if_data
.ifi_opackets
);
1668 atomic_get_64(s
->bytes_out
, &ifp
->if_data
.ifi_obytes
);
1669 atomic_get_64(s
->multicasts_out
, &ifp
->if_data
.ifi_omcasts
);
1670 atomic_get_64(s
->errors_out
, &ifp
->if_data
.ifi_oerrors
);
1672 atomic_get_64(s
->collisions
, &ifp
->if_data
.ifi_collisions
);
1673 atomic_get_64(s
->dropped
, &ifp
->if_data
.ifi_iqdrops
);
1674 atomic_get_64(s
->no_protocol
, &ifp
->if_data
.ifi_noproto
);
1676 if (ifp
->if_data_threshold
!= 0)
1677 ifnet_notify_data_threshold(ifp
);
1683 ifnet_touch_lastchange(ifnet_t interface
)
1685 if (interface
== NULL
)
1688 TOUCHLASTCHANGE(&interface
->if_lastchange
);
1694 ifnet_lastchange(ifnet_t interface
, struct timeval
*last_change
)
1696 if (interface
== NULL
)
1699 *last_change
= interface
->if_data
.ifi_lastchange
;
1700 /* Crude conversion from uptime to calendar time */
1701 last_change
->tv_sec
+= boottime_sec();
1707 ifnet_touch_lastupdown(ifnet_t interface
)
1709 if (interface
== NULL
) {
1713 TOUCHLASTCHANGE(&interface
->if_lastupdown
);
1719 ifnet_updown_delta(ifnet_t interface
, struct timeval
*updown_delta
)
1721 if (interface
== NULL
) {
1725 /* Calculate the delta */
1726 updown_delta
->tv_sec
= net_uptime();
1727 if (updown_delta
->tv_sec
> interface
->if_data
.ifi_lastupdown
.tv_sec
) {
1728 updown_delta
->tv_sec
-= interface
->if_data
.ifi_lastupdown
.tv_sec
;
1730 updown_delta
->tv_usec
= 0;
1736 ifnet_get_address_list(ifnet_t interface
, ifaddr_t
**addresses
)
1738 return (addresses
== NULL
? EINVAL
:
1739 ifnet_get_address_list_family(interface
, addresses
, 0));
1742 struct ifnet_addr_list
{
1743 SLIST_ENTRY(ifnet_addr_list
) ifal_le
;
1744 struct ifaddr
*ifal_ifa
;
1748 ifnet_get_address_list_family(ifnet_t interface
, ifaddr_t
**addresses
,
1751 return (ifnet_get_address_list_family_internal(interface
, addresses
,
1752 family
, 0, M_NOWAIT
, 0));
1756 ifnet_get_inuse_address_list(ifnet_t interface
, ifaddr_t
**addresses
)
1758 return (addresses
== NULL
? EINVAL
:
1759 ifnet_get_address_list_family_internal(interface
, addresses
,
1760 0, 0, M_NOWAIT
, 1));
1763 extern uint32_t tcp_find_anypcb_byaddr(struct ifaddr
*ifa
);
1765 extern uint32_t udp_find_anypcb_byaddr(struct ifaddr
*ifa
);
1767 __private_extern__ errno_t
1768 ifnet_get_address_list_family_internal(ifnet_t interface
, ifaddr_t
**addresses
,
1769 sa_family_t family
, int detached
, int how
, int return_inuse_addrs
)
1771 SLIST_HEAD(, ifnet_addr_list
) ifal_head
;
1772 struct ifnet_addr_list
*ifal
, *ifal_tmp
;
1779 SLIST_INIT(&ifal_head
);
1781 if (addresses
== NULL
) {
1789 * Interface has been detached, so skip the lookup
1790 * at ifnet_head and go directly to inner loop.
1800 ifnet_head_lock_shared();
1801 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
1802 if (interface
!= NULL
&& ifp
!= interface
)
1805 ifnet_lock_shared(ifp
);
1806 if (interface
== NULL
|| interface
== ifp
) {
1808 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
) {
1811 ifa
->ifa_addr
->sa_family
!= family
) {
1815 MALLOC(ifal
, struct ifnet_addr_list
*,
1816 sizeof (*ifal
), M_TEMP
, how
);
1819 ifnet_lock_done(ifp
);
1825 ifal
->ifal_ifa
= ifa
;
1826 IFA_ADDREF_LOCKED(ifa
);
1827 SLIST_INSERT_HEAD(&ifal_head
, ifal
, ifal_le
);
1832 ifnet_lock_done(ifp
);
1843 MALLOC(*addresses
, ifaddr_t
*, sizeof (ifaddr_t
) * (count
+ 1),
1845 if (*addresses
== NULL
) {
1849 bzero(*addresses
, sizeof (ifaddr_t
) * (count
+ 1));
1852 SLIST_FOREACH_SAFE(ifal
, &ifal_head
, ifal_le
, ifal_tmp
) {
1853 SLIST_REMOVE(&ifal_head
, ifal
, ifnet_addr_list
, ifal_le
);
1855 if (return_inuse_addrs
) {
1856 usecount
= tcp_find_anypcb_byaddr(ifal
->ifal_ifa
);
1857 usecount
+= udp_find_anypcb_byaddr(ifal
->ifal_ifa
);
1859 (*addresses
)[index
] = ifal
->ifal_ifa
;
1862 IFA_REMREF(ifal
->ifal_ifa
);
1865 (*addresses
)[--count
] = ifal
->ifal_ifa
;
1868 IFA_REMREF(ifal
->ifal_ifa
);
1873 VERIFY(err
== 0 || *addresses
== NULL
);
1874 if ((err
== 0) && (count
) && ((*addresses
)[0] == NULL
)) {
1875 VERIFY(return_inuse_addrs
== 1);
1876 FREE(*addresses
, M_TEMP
);
1883 ifnet_free_address_list(ifaddr_t
*addresses
)
1887 if (addresses
== NULL
)
1890 for (i
= 0; addresses
[i
] != NULL
; i
++)
1891 IFA_REMREF(addresses
[i
]);
1893 FREE(addresses
, M_TEMP
);
1897 ifnet_lladdr(ifnet_t interface
)
1902 if (interface
== NULL
)
1906 * if_lladdr points to the permanent link address of
1907 * the interface and it never gets deallocated; internal
1908 * code should simply use IF_LLADDR() for performance.
1910 ifa
= interface
->if_lladdr
;
1912 lladdr
= LLADDR(SDL((void *)ifa
->ifa_addr
));
1919 ifnet_llbroadcast_copy_bytes(ifnet_t interface
, void *addr
, size_t buffer_len
,
1922 if (interface
== NULL
|| addr
== NULL
|| out_len
== NULL
)
1925 *out_len
= interface
->if_broadcast
.length
;
1927 if (buffer_len
< interface
->if_broadcast
.length
)
1930 if (interface
->if_broadcast
.length
== 0)
1933 if (interface
->if_broadcast
.length
<=
1934 sizeof (interface
->if_broadcast
.u
.buffer
)) {
1935 bcopy(interface
->if_broadcast
.u
.buffer
, addr
,
1936 interface
->if_broadcast
.length
);
1938 bcopy(interface
->if_broadcast
.u
.ptr
, addr
,
1939 interface
->if_broadcast
.length
);
1946 ifnet_lladdr_copy_bytes_internal(ifnet_t interface
, void *lladdr
,
1947 size_t lladdr_len
, kauth_cred_t
*credp
)
1949 const u_int8_t
*bytes
;
1952 uint8_t sdlbuf
[SOCK_MAXADDRLEN
+ 1];
1956 * Make sure to accomodate the largest possible
1957 * size of SA(if_lladdr)->sa_len.
1959 _CASSERT(sizeof (sdlbuf
) == (SOCK_MAXADDRLEN
+ 1));
1961 if (interface
== NULL
|| lladdr
== NULL
)
1964 ifa
= interface
->if_lladdr
;
1966 bcopy(ifa
->ifa_addr
, &sdlbuf
, SDL(ifa
->ifa_addr
)->sdl_len
);
1969 bytes
= dlil_ifaddr_bytes(SDL(&sdlbuf
), &bytes_len
, credp
);
1970 if (bytes_len
!= lladdr_len
) {
1971 bzero(lladdr
, lladdr_len
);
1974 bcopy(bytes
, lladdr
, bytes_len
);
1981 ifnet_lladdr_copy_bytes(ifnet_t interface
, void *lladdr
, size_t length
)
1983 return (ifnet_lladdr_copy_bytes_internal(interface
, lladdr
, length
,
1988 ifnet_guarded_lladdr_copy_bytes(ifnet_t interface
, void *lladdr
, size_t length
)
1992 net_thread_marks_t marks
;
1994 kauth_cred_t
*credp
;
1999 marks
= net_thread_marks_push(NET_THREAD_CKREQ_LLADDR
);
2000 cred
= kauth_cred_proc_ref(current_proc());
2006 error
= ifnet_lladdr_copy_bytes_internal(interface
, lladdr
, length
,
2010 kauth_cred_unref(credp
);
2011 net_thread_marks_pop(marks
);
2018 ifnet_set_lladdr_internal(ifnet_t interface
, const void *lladdr
,
2019 size_t lladdr_len
, u_char new_type
, int apply_type
)
2024 if (interface
== NULL
)
2027 ifnet_head_lock_shared();
2028 ifnet_lock_exclusive(interface
);
2029 if (lladdr_len
!= 0 &&
2030 (lladdr_len
!= interface
->if_addrlen
|| lladdr
== 0)) {
2031 ifnet_lock_done(interface
);
2035 ifa
= ifnet_addrs
[interface
->if_index
- 1];
2037 struct sockaddr_dl
*sdl
;
2040 sdl
= (struct sockaddr_dl
*)(void *)ifa
->ifa_addr
;
2041 if (lladdr_len
!= 0) {
2042 bcopy(lladdr
, LLADDR(sdl
), lladdr_len
);
2044 bzero(LLADDR(sdl
), interface
->if_addrlen
);
2046 sdl
->sdl_alen
= lladdr_len
;
2049 sdl
->sdl_type
= new_type
;
2055 ifnet_lock_done(interface
);
2058 /* Generate a kernel event */
2060 intf_event_enqueue_nwk_wq_entry(interface
, NULL
,
2061 INTF_EVENT_CODE_LLADDR_UPDATE
);
2062 dlil_post_msg(interface
, KEV_DL_SUBCLASS
,
2063 KEV_DL_LINK_ADDRESS_CHANGED
, NULL
, 0);
2070 ifnet_set_lladdr(ifnet_t interface
, const void* lladdr
, size_t lladdr_len
)
2072 return (ifnet_set_lladdr_internal(interface
, lladdr
, lladdr_len
, 0, 0));
2076 ifnet_set_lladdr_and_type(ifnet_t interface
, const void* lladdr
,
2077 size_t lladdr_len
, u_char type
)
2079 return (ifnet_set_lladdr_internal(interface
, lladdr
,
2080 lladdr_len
, type
, 1));
2084 ifnet_add_multicast(ifnet_t interface
, const struct sockaddr
*maddr
,
2085 ifmultiaddr_t
*ifmap
)
2087 if (interface
== NULL
|| maddr
== NULL
)
2090 /* Don't let users screw up protocols' entries. */
2091 if (maddr
->sa_family
!= AF_UNSPEC
&& maddr
->sa_family
!= AF_LINK
)
2094 return (if_addmulti_anon(interface
, maddr
, ifmap
));
2098 ifnet_remove_multicast(ifmultiaddr_t ifma
)
2100 struct sockaddr
*maddr
;
2105 maddr
= ifma
->ifma_addr
;
2106 /* Don't let users screw up protocols' entries. */
2107 if (maddr
->sa_family
!= AF_UNSPEC
&& maddr
->sa_family
!= AF_LINK
)
2110 return (if_delmulti_anon(ifma
->ifma_ifp
, maddr
));
2114 ifnet_get_multicast_list(ifnet_t ifp
, ifmultiaddr_t
**addresses
)
2118 struct ifmultiaddr
*addr
;
2120 if (ifp
== NULL
|| addresses
== NULL
)
2123 ifnet_lock_shared(ifp
);
2124 LIST_FOREACH(addr
, &ifp
->if_multiaddrs
, ifma_link
) {
2128 MALLOC(*addresses
, ifmultiaddr_t
*, sizeof (ifmultiaddr_t
) * (cmax
+ 1),
2130 if (*addresses
== NULL
) {
2131 ifnet_lock_done(ifp
);
2135 LIST_FOREACH(addr
, &ifp
->if_multiaddrs
, ifma_link
) {
2136 if (count
+ 1 > cmax
)
2138 (*addresses
)[count
] = (ifmultiaddr_t
)addr
;
2139 ifmaddr_reference((*addresses
)[count
]);
2142 (*addresses
)[cmax
] = NULL
;
2143 ifnet_lock_done(ifp
);
2149 ifnet_free_multicast_list(ifmultiaddr_t
*addresses
)
2153 if (addresses
== NULL
)
2156 for (i
= 0; addresses
[i
] != NULL
; i
++)
2157 ifmaddr_release(addresses
[i
]);
2159 FREE(addresses
, M_TEMP
);
2163 ifnet_find_by_name(const char *ifname
, ifnet_t
*ifpp
)
2171 namelen
= strlen(ifname
);
2175 ifnet_head_lock_shared();
2176 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2178 struct sockaddr_dl
*ll_addr
;
2180 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2185 ll_addr
= (struct sockaddr_dl
*)(void *)ifa
->ifa_addr
;
2187 if (namelen
== ll_addr
->sdl_nlen
&& strncmp(ll_addr
->sdl_data
,
2188 ifname
, ll_addr
->sdl_nlen
) == 0) {
2191 ifnet_reference(*ifpp
);
2198 return ((ifp
== NULL
) ? ENXIO
: 0);
2202 ifnet_list_get(ifnet_family_t family
, ifnet_t
**list
, u_int32_t
*count
)
2204 return (ifnet_list_get_common(family
, FALSE
, list
, count
));
2207 __private_extern__ errno_t
2208 ifnet_list_get_all(ifnet_family_t family
, ifnet_t
**list
, u_int32_t
*count
)
2210 return (ifnet_list_get_common(family
, TRUE
, list
, count
));
2214 SLIST_ENTRY(ifnet_list
) ifl_le
;
2215 struct ifnet
*ifl_ifp
;
2219 ifnet_list_get_common(ifnet_family_t family
, boolean_t get_all
, ifnet_t
**list
,
2222 #pragma unused(get_all)
2223 SLIST_HEAD(, ifnet_list
) ifl_head
;
2224 struct ifnet_list
*ifl
, *ifl_tmp
;
2229 SLIST_INIT(&ifl_head
);
2231 if (list
== NULL
|| count
== NULL
) {
2238 ifnet_head_lock_shared();
2239 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2240 if (family
== IFNET_FAMILY_ANY
|| ifp
->if_family
== family
) {
2241 MALLOC(ifl
, struct ifnet_list
*, sizeof (*ifl
),
2249 ifnet_reference(ifp
);
2250 SLIST_INSERT_HEAD(&ifl_head
, ifl
, ifl_le
);
2261 MALLOC(*list
, ifnet_t
*, sizeof (ifnet_t
) * (cnt
+ 1),
2263 if (*list
== NULL
) {
2267 bzero(*list
, sizeof (ifnet_t
) * (cnt
+ 1));
2271 SLIST_FOREACH_SAFE(ifl
, &ifl_head
, ifl_le
, ifl_tmp
) {
2272 SLIST_REMOVE(&ifl_head
, ifl
, ifnet_list
, ifl_le
);
2274 (*list
)[--cnt
] = ifl
->ifl_ifp
;
2276 ifnet_release(ifl
->ifl_ifp
);
2284 ifnet_list_free(ifnet_t
*interfaces
)
2288 if (interfaces
== NULL
)
2291 for (i
= 0; interfaces
[i
]; i
++)
2292 ifnet_release(interfaces
[i
]);
2294 FREE(interfaces
, M_TEMP
);
2297 /*************************************************************************/
2298 /* ifaddr_t accessors */
2299 /*************************************************************************/
2302 ifaddr_reference(ifaddr_t ifa
)
2312 ifaddr_release(ifaddr_t ifa
)
2322 ifaddr_address_family(ifaddr_t ifa
)
2324 sa_family_t family
= 0;
2328 if (ifa
->ifa_addr
!= NULL
)
2329 family
= ifa
->ifa_addr
->sa_family
;
2336 ifaddr_address(ifaddr_t ifa
, struct sockaddr
*out_addr
, u_int32_t addr_size
)
2340 if (ifa
== NULL
|| out_addr
== NULL
)
2344 if (ifa
->ifa_addr
== NULL
) {
2349 copylen
= (addr_size
>= ifa
->ifa_addr
->sa_len
) ?
2350 ifa
->ifa_addr
->sa_len
: addr_size
;
2351 bcopy(ifa
->ifa_addr
, out_addr
, copylen
);
2353 if (ifa
->ifa_addr
->sa_len
> addr_size
) {
2363 ifaddr_dstaddress(ifaddr_t ifa
, struct sockaddr
*out_addr
, u_int32_t addr_size
)
2367 if (ifa
== NULL
|| out_addr
== NULL
)
2371 if (ifa
->ifa_dstaddr
== NULL
) {
2376 copylen
= (addr_size
>= ifa
->ifa_dstaddr
->sa_len
) ?
2377 ifa
->ifa_dstaddr
->sa_len
: addr_size
;
2378 bcopy(ifa
->ifa_dstaddr
, out_addr
, copylen
);
2380 if (ifa
->ifa_dstaddr
->sa_len
> addr_size
) {
2390 ifaddr_netmask(ifaddr_t ifa
, struct sockaddr
*out_addr
, u_int32_t addr_size
)
2394 if (ifa
== NULL
|| out_addr
== NULL
)
2398 if (ifa
->ifa_netmask
== NULL
) {
2403 copylen
= addr_size
>= ifa
->ifa_netmask
->sa_len
?
2404 ifa
->ifa_netmask
->sa_len
: addr_size
;
2405 bcopy(ifa
->ifa_netmask
, out_addr
, copylen
);
2407 if (ifa
->ifa_netmask
->sa_len
> addr_size
) {
2417 ifaddr_ifnet(ifaddr_t ifa
)
2424 /* ifa_ifp is set once at creation time; it is never changed */
2431 ifaddr_withaddr(const struct sockaddr
*address
)
2433 if (address
== NULL
)
2436 return (ifa_ifwithaddr(address
));
2440 ifaddr_withdstaddr(const struct sockaddr
*address
)
2442 if (address
== NULL
)
2445 return (ifa_ifwithdstaddr(address
));
2449 ifaddr_withnet(const struct sockaddr
*net
)
2454 return (ifa_ifwithnet(net
));
2458 ifaddr_withroute(int flags
, const struct sockaddr
*destination
,
2459 const struct sockaddr
*gateway
)
2461 if (destination
== NULL
|| gateway
== NULL
)
2464 return (ifa_ifwithroute(flags
, destination
, gateway
));
2468 ifaddr_findbestforaddr(const struct sockaddr
*addr
, ifnet_t interface
)
2470 if (addr
== NULL
|| interface
== NULL
)
2473 return (ifaof_ifpforaddr_select(addr
, interface
));
2477 ifmaddr_reference(ifmultiaddr_t ifmaddr
)
2479 if (ifmaddr
== NULL
)
2482 IFMA_ADDREF(ifmaddr
);
2487 ifmaddr_release(ifmultiaddr_t ifmaddr
)
2489 if (ifmaddr
== NULL
)
2492 IFMA_REMREF(ifmaddr
);
2497 ifmaddr_address(ifmultiaddr_t ifma
, struct sockaddr
*out_addr
,
2498 u_int32_t addr_size
)
2502 if (ifma
== NULL
|| out_addr
== NULL
)
2506 if (ifma
->ifma_addr
== NULL
) {
2511 copylen
= (addr_size
>= ifma
->ifma_addr
->sa_len
?
2512 ifma
->ifma_addr
->sa_len
: addr_size
);
2513 bcopy(ifma
->ifma_addr
, out_addr
, copylen
);
2515 if (ifma
->ifma_addr
->sa_len
> addr_size
) {
2524 ifmaddr_lladdress(ifmultiaddr_t ifma
, struct sockaddr
*out_addr
,
2525 u_int32_t addr_size
)
2527 struct ifmultiaddr
*ifma_ll
;
2529 if (ifma
== NULL
|| out_addr
== NULL
)
2531 if ((ifma_ll
= ifma
->ifma_ll
) == NULL
)
2534 return (ifmaddr_address(ifma_ll
, out_addr
, addr_size
));
2538 ifmaddr_ifnet(ifmultiaddr_t ifma
)
2540 return ((ifma
== NULL
) ? NULL
: ifma
->ifma_ifp
);
2543 /**************************************************************************/
2544 /* interface cloner */
2545 /**************************************************************************/
2548 ifnet_clone_attach(struct ifnet_clone_params
*cloner_params
,
2549 if_clone_t
*ifcloner
)
2552 struct if_clone
*ifc
= NULL
;
2555 if (cloner_params
== NULL
|| ifcloner
== NULL
||
2556 cloner_params
->ifc_name
== NULL
||
2557 cloner_params
->ifc_create
== NULL
||
2558 cloner_params
->ifc_destroy
== NULL
||
2559 (namelen
= strlen(cloner_params
->ifc_name
)) >= IFNAMSIZ
) {
2564 if (if_clone_lookup(cloner_params
->ifc_name
, NULL
) != NULL
) {
2565 printf("%s: already a cloner for %s\n", __func__
,
2566 cloner_params
->ifc_name
);
2571 /* Make room for name string */
2572 ifc
= _MALLOC(sizeof (struct if_clone
) + IFNAMSIZ
+ 1, M_CLONE
,
2575 printf("%s: _MALLOC failed\n", __func__
);
2579 strlcpy((char *)(ifc
+ 1), cloner_params
->ifc_name
, IFNAMSIZ
+ 1);
2580 ifc
->ifc_name
= (char *)(ifc
+ 1);
2581 ifc
->ifc_namelen
= namelen
;
2582 ifc
->ifc_maxunit
= IF_MAXUNIT
;
2583 ifc
->ifc_create
= cloner_params
->ifc_create
;
2584 ifc
->ifc_destroy
= cloner_params
->ifc_destroy
;
2586 error
= if_clone_attach(ifc
);
2588 printf("%s: if_clone_attach failed %d\n", __func__
, error
);
2601 ifnet_clone_detach(if_clone_t ifcloner
)
2604 struct if_clone
*ifc
= ifcloner
;
2606 if (ifc
== NULL
|| ifc
->ifc_name
== NULL
)
2609 if ((if_clone_lookup(ifc
->ifc_name
, NULL
)) == NULL
) {
2610 printf("%s: no cloner for %s\n", __func__
, ifc
->ifc_name
);
2615 if_clone_detach(ifc
);
2623 /**************************************************************************/
2625 /**************************************************************************/
2628 ifnet_get_local_ports_extended(ifnet_t ifp
, protocol_family_t protocol
,
2629 u_int32_t flags
, u_int8_t
*bitfield
)
2632 u_int32_t inp_flags
= 0;
2634 if (bitfield
== NULL
)
2646 /* bit string is long enough to hold 16-bit port values */
2647 bzero(bitfield
, bitstr_size(IP_PORTRANGE_SIZE
));
2649 if_ports_used_update_wakeuuid(ifp
);
2652 inp_flags
|= ((flags
& IFNET_GET_LOCAL_PORTS_WILDCARDOK
) ?
2653 INPCB_GET_PORTS_USED_WILDCARDOK
: 0);
2654 inp_flags
|= ((flags
& IFNET_GET_LOCAL_PORTS_NOWAKEUPOK
) ?
2655 INPCB_GET_PORTS_USED_NOWAKEUPOK
: 0);
2656 inp_flags
|= ((flags
& IFNET_GET_LOCAL_PORTS_RECVANYIFONLY
) ?
2657 INPCB_GET_PORTS_USED_RECVANYIFONLY
: 0);
2658 inp_flags
|= ((flags
& IFNET_GET_LOCAL_PORTS_EXTBGIDLEONLY
) ?
2659 INPCB_GET_PORTS_USED_EXTBGIDLEONLY
: 0);
2660 inp_flags
|= ((flags
& IFNET_GET_LOCAL_PORTS_ACTIVEONLY
) ?
2661 INPCB_GET_PORTS_USED_ACTIVEONLY
: 0);
2663 ifindex
= (ifp
!= NULL
) ? ifp
->if_index
: 0;
2665 if (!(flags
& IFNET_GET_LOCAL_PORTS_TCPONLY
))
2666 udp_get_ports_used(ifindex
, protocol
, inp_flags
,
2669 if (!(flags
& IFNET_GET_LOCAL_PORTS_UDPONLY
))
2670 tcp_get_ports_used(ifindex
, protocol
, inp_flags
,
2677 ifnet_get_local_ports(ifnet_t ifp
, u_int8_t
*bitfield
)
2679 u_int32_t flags
= IFNET_GET_LOCAL_PORTS_WILDCARDOK
;
2680 return (ifnet_get_local_ports_extended(ifp
, PF_UNSPEC
, flags
,
2685 ifnet_notice_node_presence(ifnet_t ifp
, struct sockaddr
*sa
, int32_t rssi
,
2686 int lqm
, int npm
, u_int8_t srvinfo
[48])
2688 if (ifp
== NULL
|| sa
== NULL
|| srvinfo
== NULL
)
2690 if (sa
->sa_len
> sizeof(struct sockaddr_storage
))
2692 if (sa
->sa_family
!= AF_LINK
&& sa
->sa_family
!= AF_INET6
)
2695 dlil_node_present(ifp
, sa
, rssi
, lqm
, npm
, srvinfo
);
2700 ifnet_notice_node_absence(ifnet_t ifp
, struct sockaddr
*sa
)
2702 if (ifp
== NULL
|| sa
== NULL
)
2704 if (sa
->sa_len
> sizeof(struct sockaddr_storage
))
2706 if (sa
->sa_family
!= AF_LINK
&& sa
->sa_family
!= AF_INET6
)
2709 dlil_node_absent(ifp
, sa
);
2714 ifnet_notice_master_elected(ifnet_t ifp
)
2719 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_MASTER_ELECTED
, NULL
, 0);
2724 ifnet_tx_compl_status(ifnet_t ifp
, mbuf_t m
, tx_compl_val_t val
)
2728 m_do_tx_compl_callback(m
, ifp
);
2734 ifnet_tx_compl(ifnet_t ifp
, mbuf_t m
)
2736 m_do_tx_compl_callback(m
, ifp
);
2742 ifnet_report_issues(ifnet_t ifp
, u_int8_t modid
[IFNET_MODIDLEN
],
2743 u_int8_t info
[IFNET_MODARGLEN
])
2745 if (ifp
== NULL
|| modid
== NULL
)
2748 dlil_report_issues(ifp
, modid
, info
);
2753 ifnet_set_delegate(ifnet_t ifp
, ifnet_t delegated_ifp
)
2755 ifnet_t odifp
= NULL
;
2759 else if (!ifnet_is_attached(ifp
, 1))
2762 ifnet_lock_exclusive(ifp
);
2763 odifp
= ifp
->if_delegated
.ifp
;
2764 if (odifp
!= NULL
&& odifp
== delegated_ifp
) {
2765 /* delegate info is unchanged; nothing more to do */
2766 ifnet_lock_done(ifp
);
2769 // Test if this delegate interface would cause a loop
2770 ifnet_t delegate_check_ifp
= delegated_ifp
;
2771 while (delegate_check_ifp
!= NULL
) {
2772 if (delegate_check_ifp
== ifp
) {
2773 printf("%s: delegating to %s would cause a loop\n",
2774 ifp
->if_xname
, delegated_ifp
->if_xname
);
2775 ifnet_lock_done(ifp
);
2778 delegate_check_ifp
= delegate_check_ifp
->if_delegated
.ifp
;
2780 bzero(&ifp
->if_delegated
, sizeof (ifp
->if_delegated
));
2781 if (delegated_ifp
!= NULL
&& ifp
!= delegated_ifp
) {
2782 ifp
->if_delegated
.ifp
= delegated_ifp
;
2783 ifnet_reference(delegated_ifp
);
2784 ifp
->if_delegated
.type
= delegated_ifp
->if_type
;
2785 ifp
->if_delegated
.family
= delegated_ifp
->if_family
;
2786 ifp
->if_delegated
.subfamily
= delegated_ifp
->if_subfamily
;
2787 ifp
->if_delegated
.expensive
=
2788 delegated_ifp
->if_eflags
& IFEF_EXPENSIVE
? 1 : 0;
2791 * Propogate flags related to ECN from delegated interface
2793 ifp
->if_eflags
&= ~(IFEF_ECN_ENABLE
|IFEF_ECN_DISABLE
);
2794 ifp
->if_eflags
|= (delegated_ifp
->if_eflags
&
2795 (IFEF_ECN_ENABLE
|IFEF_ECN_DISABLE
));
2797 printf("%s: is now delegating %s (type 0x%x, family %u, "
2798 "sub-family %u)\n", ifp
->if_xname
, delegated_ifp
->if_xname
,
2799 delegated_ifp
->if_type
, delegated_ifp
->if_family
,
2800 delegated_ifp
->if_subfamily
);
2803 ifnet_lock_done(ifp
);
2805 if (odifp
!= NULL
) {
2806 if (odifp
!= delegated_ifp
) {
2807 printf("%s: is no longer delegating %s\n",
2808 ifp
->if_xname
, odifp
->if_xname
);
2810 ifnet_release(odifp
);
2813 /* Generate a kernel event */
2814 dlil_post_msg(ifp
, KEV_DL_SUBCLASS
, KEV_DL_IFDELEGATE_CHANGED
, NULL
, 0);
2817 /* Release the io ref count */
2818 ifnet_decr_iorefcnt(ifp
);
2824 ifnet_get_delegate(ifnet_t ifp
, ifnet_t
*pdelegated_ifp
)
2826 if (ifp
== NULL
|| pdelegated_ifp
== NULL
)
2828 else if (!ifnet_is_attached(ifp
, 1))
2831 ifnet_lock_shared(ifp
);
2832 if (ifp
->if_delegated
.ifp
!= NULL
)
2833 ifnet_reference(ifp
->if_delegated
.ifp
);
2834 *pdelegated_ifp
= ifp
->if_delegated
.ifp
;
2835 ifnet_lock_done(ifp
);
2837 /* Release the io ref count */
2838 ifnet_decr_iorefcnt(ifp
);
2844 ifnet_get_keepalive_offload_frames(ifnet_t ifp
,
2845 struct ifnet_keepalive_offload_frame
*frames_array
,
2846 u_int32_t frames_array_count
, size_t frame_data_offset
,
2847 u_int32_t
*used_frames_count
)
2851 if (frames_array
== NULL
|| used_frames_count
== NULL
||
2852 frame_data_offset
>= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE
)
2855 /* frame_data_offset should be 32-bit aligned */
2856 if (P2ROUNDUP(frame_data_offset
, sizeof(u_int32_t
)) !=
2860 *used_frames_count
= 0;
2861 if (frames_array_count
== 0)
2864 for (i
= 0; i
< frames_array_count
; i
++) {
2865 struct ifnet_keepalive_offload_frame
*frame
= frames_array
+ i
;
2867 bzero(frame
, sizeof(struct ifnet_keepalive_offload_frame
));
2870 /* First collect IPSec related keep-alive frames */
2871 *used_frames_count
= key_fill_offload_frames_for_savs(ifp
,
2872 frames_array
, frames_array_count
, frame_data_offset
);
2874 /* If there is more room, collect other UDP keep-alive frames */
2875 if (*used_frames_count
< frames_array_count
)
2876 udp_fill_keepalive_offload_frames(ifp
, frames_array
,
2877 frames_array_count
, frame_data_offset
,
2880 /* If there is more room, collect other TCP keep-alive frames */
2881 if (*used_frames_count
< frames_array_count
)
2882 tcp_fill_keepalive_offload_frames(ifp
, frames_array
,
2883 frames_array_count
, frame_data_offset
,
2886 VERIFY(*used_frames_count
<= frames_array_count
);
2892 ifnet_link_status_report(ifnet_t ifp
, const void *buffer
,
2895 struct if_link_status
*ifsr
;
2898 if (ifp
== NULL
|| buffer
== NULL
|| buffer_len
== 0)
2901 ifnet_lock_shared(ifp
);
2904 * Make sure that the interface is attached but there is no need
2905 * to take a reference because this call is coming from the driver.
2907 if (!ifnet_is_attached(ifp
, 0)) {
2908 ifnet_lock_done(ifp
);
2912 lck_rw_lock_exclusive(&ifp
->if_link_status_lock
);
2915 * If this is the first status report then allocate memory
2918 if (ifp
->if_link_status
== NULL
) {
2919 MALLOC(ifp
->if_link_status
, struct if_link_status
*,
2920 sizeof(struct if_link_status
), M_TEMP
, M_ZERO
);
2921 if (ifp
->if_link_status
== NULL
) {
2927 ifsr
= __DECONST(struct if_link_status
*, buffer
);
2929 if (ifp
->if_type
== IFT_CELLULAR
) {
2930 struct if_cellular_status_v1
*if_cell_sr
, *new_cell_sr
;
2932 * Currently we have a single version -- if it does
2933 * not match, just return.
2935 if (ifsr
->ifsr_version
!=
2936 IF_CELLULAR_STATUS_REPORT_CURRENT_VERSION
) {
2941 if (ifsr
->ifsr_len
!= sizeof(*if_cell_sr
)) {
2947 &ifp
->if_link_status
->ifsr_u
.ifsr_cell
.if_cell_u
.if_status_v1
;
2948 new_cell_sr
= &ifsr
->ifsr_u
.ifsr_cell
.if_cell_u
.if_status_v1
;
2949 /* Check if we need to act on any new notifications */
2950 if ((new_cell_sr
->valid_bitmask
&
2951 IF_CELL_UL_MSS_RECOMMENDED_VALID
) &&
2952 new_cell_sr
->mss_recommended
!=
2953 if_cell_sr
->mss_recommended
) {
2954 atomic_bitset_32(&tcbinfo
.ipi_flags
,
2955 INPCBINFO_UPDATE_MSS
);
2956 inpcb_timer_sched(&tcbinfo
, INPCB_TIMER_FAST
);
2958 necp_update_all_clients();
2962 /* Finally copy the new information */
2963 ifp
->if_link_status
->ifsr_version
= ifsr
->ifsr_version
;
2964 ifp
->if_link_status
->ifsr_len
= ifsr
->ifsr_len
;
2965 if_cell_sr
->valid_bitmask
= 0;
2966 bcopy(new_cell_sr
, if_cell_sr
, sizeof(*if_cell_sr
));
2968 } else if (ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
) {
2969 struct if_wifi_status_v1
*if_wifi_sr
, *new_wifi_sr
;
2972 if (ifsr
->ifsr_version
!=
2973 IF_WIFI_STATUS_REPORT_CURRENT_VERSION
) {
2978 if (ifsr
->ifsr_len
!= sizeof(*if_wifi_sr
)) {
2984 &ifp
->if_link_status
->ifsr_u
.ifsr_wifi
.if_wifi_u
.if_status_v1
;
2986 &ifsr
->ifsr_u
.ifsr_wifi
.if_wifi_u
.if_status_v1
;
2987 ifp
->if_link_status
->ifsr_version
= ifsr
->ifsr_version
;
2988 ifp
->if_link_status
->ifsr_len
= ifsr
->ifsr_len
;
2989 if_wifi_sr
->valid_bitmask
= 0;
2990 bcopy(new_wifi_sr
, if_wifi_sr
, sizeof(*if_wifi_sr
));
2993 * Update the bandwidth values if we got recent values
2994 * reported through the other KPI.
2996 if (!(new_wifi_sr
->valid_bitmask
&
2997 IF_WIFI_UL_MAX_BANDWIDTH_VALID
) &&
2998 ifp
->if_output_bw
.max_bw
> 0) {
2999 if_wifi_sr
->valid_bitmask
|=
3000 IF_WIFI_UL_MAX_BANDWIDTH_VALID
;
3001 if_wifi_sr
->ul_max_bandwidth
=
3002 ifp
->if_output_bw
.max_bw
;
3004 if (!(new_wifi_sr
->valid_bitmask
&
3005 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID
) &&
3006 ifp
->if_output_bw
.eff_bw
> 0) {
3007 if_wifi_sr
->valid_bitmask
|=
3008 IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID
;
3009 if_wifi_sr
->ul_effective_bandwidth
=
3010 ifp
->if_output_bw
.eff_bw
;
3012 if (!(new_wifi_sr
->valid_bitmask
&
3013 IF_WIFI_DL_MAX_BANDWIDTH_VALID
) &&
3014 ifp
->if_input_bw
.max_bw
> 0) {
3015 if_wifi_sr
->valid_bitmask
|=
3016 IF_WIFI_DL_MAX_BANDWIDTH_VALID
;
3017 if_wifi_sr
->dl_max_bandwidth
=
3018 ifp
->if_input_bw
.max_bw
;
3020 if (!(new_wifi_sr
->valid_bitmask
&
3021 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID
) &&
3022 ifp
->if_input_bw
.eff_bw
> 0) {
3023 if_wifi_sr
->valid_bitmask
|=
3024 IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID
;
3025 if_wifi_sr
->dl_effective_bandwidth
=
3026 ifp
->if_input_bw
.eff_bw
;
3031 lck_rw_done(&ifp
->if_link_status_lock
);
3032 ifnet_lock_done(ifp
);
3036 /*************************************************************************/
3037 /* Fastlane QoS Ca */
3038 /*************************************************************************/
3041 ifnet_set_fastlane_capable(ifnet_t interface
, boolean_t capable
)
3043 if (interface
== NULL
)
3046 if_set_qosmarking_mode(interface
,
3047 capable
? IFRTYPE_QOSMARKING_FASTLANE
: IFRTYPE_QOSMARKING_MODE_NONE
);
3053 ifnet_get_fastlane_capable(ifnet_t interface
, boolean_t
*capable
)
3055 if (interface
== NULL
|| capable
== NULL
)
3057 if (interface
->if_eflags
& IFEF_QOSMARKING_CAPABLE
)
3065 ifnet_get_unsent_bytes(ifnet_t interface
, int64_t *unsent_bytes
)
3069 if (interface
== NULL
|| unsent_bytes
== NULL
)
3072 bytes
= *unsent_bytes
= 0;
3074 if (!IF_FULLY_ATTACHED(interface
))
3077 bytes
= interface
->if_sndbyte_unsent
;
3079 if (interface
->if_eflags
& IFEF_TXSTART
)
3080 bytes
+= IFCQ_BYTES(&interface
->if_snd
);
3081 *unsent_bytes
= bytes
;
3087 ifnet_get_buffer_status(const ifnet_t ifp
, ifnet_buffer_status_t
*buf_status
)
3089 if (ifp
== NULL
|| buf_status
== NULL
)
3092 bzero(buf_status
, sizeof (*buf_status
));
3094 if (!IF_FULLY_ATTACHED(ifp
))
3097 if (ifp
->if_eflags
& IFEF_TXSTART
)
3098 buf_status
->buf_interface
= IFCQ_BYTES(&ifp
->if_snd
);
3100 buf_status
->buf_sndbuf
= ((buf_status
->buf_interface
!= 0) ||
3101 (ifp
->if_sndbyte_unsent
!= 0)) ? 1 : 0;
3107 ifnet_normalise_unsent_data(void)
3111 ifnet_head_lock_shared();
3112 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
3113 ifnet_lock_exclusive(ifp
);
3114 if (!IF_FULLY_ATTACHED(ifp
)) {
3115 ifnet_lock_done(ifp
);
3118 if (!(ifp
->if_eflags
& IFEF_TXSTART
)) {
3119 ifnet_lock_done(ifp
);
3123 if (ifp
->if_sndbyte_total
> 0 ||
3124 IFCQ_BYTES(&ifp
->if_snd
) > 0)
3125 ifp
->if_unsent_data_cnt
++;
3127 ifnet_lock_done(ifp
);