2 * Copyright (c) 2008-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@
31 /* ----------------------------------------------------------------------------------
32 * Application of kernel control for interface creation
34 * Theory of operation:
35 * utun (user tunnel) acts as glue between kernel control sockets and network interfaces.
36 * This kernel control will register an interface for every client that connects.
37 * ---------------------------------------------------------------------------------- */
39 #include <sys/systm.h>
40 #include <sys/kern_control.h>
41 #include <net/kpi_protocol.h>
42 #include <net/kpi_interface.h>
43 #include <sys/socket.h>
45 #include <net/if_types.h>
47 #include <net/if_utun.h>
49 #include <sys/sockio.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet6/in6_var.h>
54 #include <sys/kauth.h>
56 #include <kern/zalloc.h>
61 static nexus_controller_t utun_ncd
;
62 static int utun_ncd_refcount
;
63 static uuid_t utun_kpipe_uuid
;
64 static uuid_t utun_nx_dom_prov
;
66 typedef struct utun_nx
{
78 /* Control block allocated for each kernel control connection */
80 TAILQ_ENTRY(utun_pcb
) utun_chain
;
81 kern_ctl_ref utun_ctlref
;
84 u_int32_t utun_unique_id
;
86 int utun_ext_ifdata_stats
;
87 u_int32_t utun_max_pending_packets
;
88 char utun_if_xname
[IFXNAMSIZ
];
89 char utun_unique_name
[IFXNAMSIZ
];
90 // PCB lock protects state fields and rings
91 decl_lck_rw_data(, utun_pcb_lock
);
92 struct mbuf
* utun_input_chain
;
93 struct mbuf
* utun_input_chain_last
;
94 // Input chain lock protects the list of input mbufs
95 // The input chain lock must be taken AFTER the PCB lock if both are held
96 lck_mtx_t utun_input_chain_lock
;
99 struct utun_nx utun_nx
;
100 int utun_kpipe_enabled
;
101 uuid_t utun_kpipe_uuid
;
102 void * utun_kpipe_rxring
;
103 void * utun_kpipe_txring
;
104 kern_pbufpool_t utun_kpipe_pp
;
106 kern_nexus_t utun_netif_nexus
;
107 kern_pbufpool_t utun_netif_pp
;
108 void * utun_netif_rxring
;
109 void * utun_netif_txring
;
110 uint64_t utun_netif_txring_size
;
112 u_int32_t utun_slot_size
;
113 u_int32_t utun_netif_ring_size
;
114 u_int32_t utun_tx_fsw_ring_size
;
115 u_int32_t utun_rx_fsw_ring_size
;
117 bool utun_needs_netagent
;
121 /* Kernel Control functions */
122 static errno_t
utun_ctl_bind(kern_ctl_ref kctlref
, struct sockaddr_ctl
*sac
,
124 static errno_t
utun_ctl_connect(kern_ctl_ref kctlref
, struct sockaddr_ctl
*sac
,
126 static errno_t
utun_ctl_disconnect(kern_ctl_ref kctlref
, u_int32_t unit
,
128 static errno_t
utun_ctl_send(kern_ctl_ref kctlref
, u_int32_t unit
,
129 void *unitinfo
, mbuf_t m
, int flags
);
130 static errno_t
utun_ctl_getopt(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
131 int opt
, void *data
, size_t *len
);
132 static errno_t
utun_ctl_setopt(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
133 int opt
, void *data
, size_t len
);
134 static void utun_ctl_rcvd(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
137 /* Network Interface functions */
138 static void utun_start(ifnet_t interface
);
139 static errno_t
utun_framer(ifnet_t interface
, mbuf_t
*packet
,
140 const struct sockaddr
*dest
, const char *desk_linkaddr
,
141 const char *frame_type
, u_int32_t
*prepend_len
, u_int32_t
*postpend_len
);
142 static errno_t
utun_output(ifnet_t interface
, mbuf_t data
);
143 static errno_t
utun_demux(ifnet_t interface
, mbuf_t data
, char *frame_header
,
144 protocol_family_t
*protocol
);
145 static errno_t
utun_add_proto(ifnet_t interface
, protocol_family_t protocol
,
146 const struct ifnet_demux_desc
*demux_array
,
147 u_int32_t demux_count
);
148 static errno_t
utun_del_proto(ifnet_t interface
, protocol_family_t protocol
);
149 static errno_t
utun_ioctl(ifnet_t interface
, u_long cmd
, void *data
);
150 static void utun_detached(ifnet_t interface
);
152 /* Protocol handlers */
153 static errno_t
utun_attach_proto(ifnet_t interface
, protocol_family_t proto
);
154 static errno_t
utun_proto_input(ifnet_t interface
, protocol_family_t protocol
,
155 mbuf_t m
, char *frame_header
);
156 static errno_t
utun_proto_pre_output(ifnet_t interface
, protocol_family_t protocol
,
157 mbuf_t
*packet
, const struct sockaddr
*dest
, void *route
,
158 char *frame_type
, char *link_layer_dest
);
159 static errno_t
utun_pkt_input(struct utun_pcb
*pcb
, mbuf_t m
);
163 #define UTUN_IF_DEFAULT_SLOT_SIZE 2048
164 #define UTUN_IF_DEFAULT_RING_SIZE 64
165 #define UTUN_IF_DEFAULT_TX_FSW_RING_SIZE 64
166 #define UTUN_IF_DEFAULT_RX_FSW_RING_SIZE 128
167 #define UTUN_IF_DEFAULT_BUF_SEG_SIZE skmem_usr_buf_seg_size
168 #define UTUN_IF_HEADROOM_SIZE 32
170 #define UTUN_IF_MIN_RING_SIZE 16
171 #define UTUN_IF_MAX_RING_SIZE 1024
173 #define UTUN_IF_MIN_SLOT_SIZE 1024
174 #define UTUN_IF_MAX_SLOT_SIZE 4096
176 static int sysctl_if_utun_ring_size SYSCTL_HANDLER_ARGS
;
177 static int sysctl_if_utun_tx_fsw_ring_size SYSCTL_HANDLER_ARGS
;
178 static int sysctl_if_utun_rx_fsw_ring_size SYSCTL_HANDLER_ARGS
;
180 static int if_utun_ring_size
= UTUN_IF_DEFAULT_RING_SIZE
;
181 static int if_utun_tx_fsw_ring_size
= UTUN_IF_DEFAULT_TX_FSW_RING_SIZE
;
182 static int if_utun_rx_fsw_ring_size
= UTUN_IF_DEFAULT_RX_FSW_RING_SIZE
;
184 SYSCTL_DECL(_net_utun
);
185 SYSCTL_NODE(_net
, OID_AUTO
, utun
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "UTun");
187 SYSCTL_PROC(_net_utun
, OID_AUTO
, ring_size
, CTLTYPE_INT
| CTLFLAG_LOCKED
| CTLFLAG_RW
,
188 &if_utun_ring_size
, UTUN_IF_DEFAULT_RING_SIZE
, &sysctl_if_utun_ring_size
, "I", "");
189 SYSCTL_PROC(_net_utun
, OID_AUTO
, tx_fsw_ring_size
, CTLTYPE_INT
| CTLFLAG_LOCKED
| CTLFLAG_RW
,
190 &if_utun_tx_fsw_ring_size
, UTUN_IF_DEFAULT_TX_FSW_RING_SIZE
, &sysctl_if_utun_tx_fsw_ring_size
, "I", "");
191 SYSCTL_PROC(_net_utun
, OID_AUTO
, rx_fsw_ring_size
, CTLTYPE_INT
| CTLFLAG_LOCKED
| CTLFLAG_RW
,
192 &if_utun_rx_fsw_ring_size
, UTUN_IF_DEFAULT_RX_FSW_RING_SIZE
, &sysctl_if_utun_rx_fsw_ring_size
, "I", "");
195 utun_register_nexus(void);
198 utun_netif_prepare(__unused kern_nexus_t nexus
, ifnet_t ifp
);
200 utun_nexus_pre_connect(kern_nexus_provider_t nxprov
,
201 proc_t p
, kern_nexus_t nexus
,
202 nexus_port_t nexus_port
, kern_channel_t channel
, void **ch_ctx
);
204 utun_nexus_connected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
205 kern_channel_t channel
);
207 utun_netif_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
208 kern_channel_t channel
);
210 utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
211 kern_channel_t channel
);
213 utun_nexus_disconnected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
214 kern_channel_t channel
);
216 utun_kpipe_ring_init(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
217 kern_channel_t channel
, kern_channel_ring_t ring
, boolean_t is_tx_ring
,
220 utun_kpipe_ring_fini(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
221 kern_channel_ring_t ring
);
223 utun_kpipe_sync_tx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
224 kern_channel_ring_t ring
, uint32_t flags
);
226 utun_kpipe_sync_rx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
227 kern_channel_ring_t ring
, uint32_t flags
);
230 #define UTUN_DEFAULT_MTU 1500
231 #define UTUN_HEADER_SIZE(_pcb) (sizeof(u_int32_t) + (((_pcb)->utun_flags & UTUN_FLAGS_ENABLE_PROC_UUID) ? sizeof(uuid_t) : 0))
233 static kern_ctl_ref utun_kctlref
;
234 static u_int32_t utun_family
;
235 static lck_attr_t
*utun_lck_attr
;
236 static lck_grp_attr_t
*utun_lck_grp_attr
;
237 static lck_grp_t
*utun_lck_grp
;
238 static lck_mtx_t utun_lock
;
240 TAILQ_HEAD(utun_list
, utun_pcb
) utun_head
;
242 #define UTUN_PCB_ZONE_MAX 32
243 #define UTUN_PCB_ZONE_NAME "net.if_utun"
245 static unsigned int utun_pcb_size
; /* size of zone element */
246 static struct zone
*utun_pcb_zone
; /* zone for utun_pcb */
251 sysctl_if_utun_ring_size SYSCTL_HANDLER_ARGS
253 #pragma unused(arg1, arg2)
254 int value
= if_utun_ring_size
;
256 int error
= sysctl_handle_int(oidp
, &value
, 0, req
);
257 if (error
|| !req
->newptr
) {
261 if (value
< UTUN_IF_MIN_RING_SIZE
||
262 value
> UTUN_IF_MAX_RING_SIZE
) {
266 if_utun_ring_size
= value
;
272 sysctl_if_utun_tx_fsw_ring_size SYSCTL_HANDLER_ARGS
274 #pragma unused(arg1, arg2)
275 int value
= if_utun_tx_fsw_ring_size
;
277 int error
= sysctl_handle_int(oidp
, &value
, 0, req
);
278 if (error
|| !req
->newptr
) {
282 if (value
< UTUN_IF_MIN_RING_SIZE
||
283 value
> UTUN_IF_MAX_RING_SIZE
) {
287 if_utun_tx_fsw_ring_size
= value
;
293 sysctl_if_utun_rx_fsw_ring_size SYSCTL_HANDLER_ARGS
295 #pragma unused(arg1, arg2)
296 int value
= if_utun_rx_fsw_ring_size
;
298 int error
= sysctl_handle_int(oidp
, &value
, 0, req
);
299 if (error
|| !req
->newptr
) {
303 if (value
< UTUN_IF_MIN_RING_SIZE
||
304 value
> UTUN_IF_MAX_RING_SIZE
) {
308 if_utun_rx_fsw_ring_size
= value
;
314 utun_netif_ring_init(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
315 kern_channel_t channel
, kern_channel_ring_t ring
, boolean_t is_tx_ring
,
318 #pragma unused(nxprov)
319 #pragma unused(channel)
320 #pragma unused(ring_ctx)
321 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
323 VERIFY(pcb
->utun_netif_rxring
== NULL
);
324 pcb
->utun_netif_rxring
= ring
;
326 VERIFY(pcb
->utun_netif_txring
== NULL
);
327 pcb
->utun_netif_txring
= ring
;
333 utun_netif_ring_fini(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
334 kern_channel_ring_t ring
)
336 #pragma unused(nxprov)
337 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
338 if (pcb
->utun_netif_rxring
== ring
) {
339 pcb
->utun_netif_rxring
= NULL
;
340 } else if (pcb
->utun_netif_txring
== ring
) {
341 pcb
->utun_netif_txring
= NULL
;
346 utun_netif_sync_tx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
347 kern_channel_ring_t tx_ring
, uint32_t flags
)
349 #pragma unused(nxprov)
350 #pragma unused(flags)
351 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
353 struct netif_stats
*nifs
= &NX_NETIF_PRIVATE(nexus
)->nif_stats
;
355 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
357 struct kern_channel_ring_stat_increment tx_ring_stats
;
358 bzero(&tx_ring_stats
, sizeof(tx_ring_stats
));
359 kern_channel_slot_t tx_pslot
= NULL
;
360 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
362 STATS_INC(nifs
, NETIF_STATS_TXSYNC
);
364 if (tx_slot
== NULL
) {
365 // Nothing to write, don't bother signalling
366 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
370 if (pcb
->utun_kpipe_enabled
) {
371 kern_channel_ring_t rx_ring
= pcb
->utun_kpipe_rxring
;
372 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
374 // Signal the kernel pipe ring to read
375 if (rx_ring
!= NULL
) {
376 kern_channel_notify(rx_ring
, 0);
381 // If we're here, we're injecting into the utun kernel control socket
382 while (tx_slot
!= NULL
) {
386 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
391 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
394 (void) kern_channel_slot_detach_packet(tx_ring
, tx_slot
, tx_ph
);
398 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
400 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
401 VERIFY(tx_buf
!= NULL
);
403 /* tx_baddr is the absolute buffer address */
404 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
405 VERIFY(tx_baddr
!= 0);
407 bpf_tap_packet_out(pcb
->utun_ifp
, DLT_RAW
, tx_ph
, NULL
, 0);
409 uint16_t tx_offset
= kern_buflet_get_data_offset(tx_buf
);
410 uint32_t tx_length
= kern_buflet_get_data_length(tx_buf
);
412 // The offset must be large enough for the headers
413 VERIFY(tx_offset
>= UTUN_HEADER_SIZE(pcb
));
417 uint8_t vhl
= *(uint8_t *)(tx_baddr
+ tx_offset
);
418 u_int ip_version
= (vhl
>> 4);
419 switch (ip_version
) {
429 printf("utun_netif_sync_tx %s: unknown ip version %u vhl %u tx_offset %u len %u header_size %zu\n",
430 pcb
->utun_ifp
->if_xname
, ip_version
, vhl
, tx_offset
, tx_length
,
431 UTUN_HEADER_SIZE(pcb
));
436 tx_offset
-= UTUN_HEADER_SIZE(pcb
);
437 tx_length
+= UTUN_HEADER_SIZE(pcb
);
438 tx_baddr
+= tx_offset
;
440 length
= MIN(tx_length
, pcb
->utun_slot_size
);
443 memcpy(tx_baddr
, &af
, sizeof(af
));
444 if (pcb
->utun_flags
& UTUN_FLAGS_ENABLE_PROC_UUID
) {
445 kern_packet_get_euuid(tx_ph
, (void *)(tx_baddr
+ sizeof(af
)));
449 errno_t error
= mbuf_gethdr(MBUF_DONTWAIT
, MBUF_TYPE_HEADER
, &data
);
451 error
= mbuf_copyback(data
, 0, length
, tx_baddr
, MBUF_DONTWAIT
);
453 error
= utun_output(pcb
->utun_ifp
, data
);
455 printf("utun_netif_sync_tx %s - utun_output error %d\n", pcb
->utun_ifp
->if_xname
, error
);
458 printf("utun_netif_sync_tx %s - mbuf_copyback(%zu) error %d\n", pcb
->utun_ifp
->if_xname
, length
, error
);
459 STATS_INC(nifs
, NETIF_STATS_NOMEM_MBUF
);
460 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
465 printf("utun_netif_sync_tx %s - mbuf_gethdr error %d\n", pcb
->utun_ifp
->if_xname
, error
);
466 STATS_INC(nifs
, NETIF_STATS_NOMEM_MBUF
);
467 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
470 printf("utun_netif_sync_tx %s - 0 length packet\n", pcb
->utun_ifp
->if_xname
);
471 STATS_INC(nifs
, NETIF_STATS_NOMEM_MBUF
);
472 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
475 kern_pbufpool_free(tx_ring
->ckr_pp
, tx_ph
);
481 STATS_INC(nifs
, NETIF_STATS_TXPKTS
);
482 STATS_INC(nifs
, NETIF_STATS_TXCOPY_MBUF
);
484 tx_ring_stats
.kcrsi_slots_transferred
++;
485 tx_ring_stats
.kcrsi_bytes_transferred
+= length
;
489 kern_channel_advance_slot(tx_ring
, tx_pslot
);
490 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
491 (void)kern_channel_reclaim(tx_ring
);
494 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
500 utun_netif_tx_doorbell(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
501 kern_channel_ring_t ring
, __unused
uint32_t flags
)
503 #pragma unused(nxprov)
504 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
505 boolean_t more
= false;
509 * Refill and sync the ring; we may be racing against another thread doing
510 * an RX sync that also wants to do kr_enter(), and so use the blocking
513 rc
= kern_channel_tx_refill_canblock(ring
, UINT32_MAX
, UINT32_MAX
, true, &more
);
514 if (rc
!= 0 && rc
!= EAGAIN
&& rc
!= EBUSY
) {
515 printf("%s, tx refill failed %d\n", __func__
, rc
);
518 (void) kr_enter(ring
, TRUE
);
519 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
521 if (pcb
->utun_kpipe_enabled
) {
522 uint32_t tx_available
= kern_channel_available_slot_count(ring
);
523 if (pcb
->utun_netif_txring_size
> 0 &&
524 tx_available
>= pcb
->utun_netif_txring_size
- 1) {
525 // No room left in tx ring, disable output for now
526 errno_t error
= ifnet_disable_output(pcb
->utun_ifp
);
528 printf("utun_netif_tx_doorbell: ifnet_disable_output returned error %d\n", error
);
533 if (pcb
->utun_kpipe_enabled
) {
534 kern_channel_ring_t rx_ring
= pcb
->utun_kpipe_rxring
;
536 // Unlock while calling notify
537 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
538 // Signal the kernel pipe ring to read
539 if (rx_ring
!= NULL
) {
540 kern_channel_notify(rx_ring
, 0);
543 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
552 utun_netif_sync_rx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
553 kern_channel_ring_t rx_ring
, uint32_t flags
)
555 #pragma unused(nxprov)
556 #pragma unused(flags)
557 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
558 struct kern_channel_ring_stat_increment rx_ring_stats
;
560 struct netif_stats
*nifs
= &NX_NETIF_PRIVATE(nexus
)->nif_stats
;
562 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
564 // Reclaim user-released slots
565 (void) kern_channel_reclaim(rx_ring
);
567 STATS_INC(nifs
, NETIF_STATS_RXSYNC
);
569 uint32_t avail
= kern_channel_available_slot_count(rx_ring
);
571 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
575 struct kern_pbufpool
*rx_pp
= rx_ring
->ckr_pp
;
576 VERIFY(rx_pp
!= NULL
);
577 bzero(&rx_ring_stats
, sizeof(rx_ring_stats
));
578 kern_channel_slot_t rx_pslot
= NULL
;
579 kern_channel_slot_t rx_slot
= kern_channel_get_next_slot(rx_ring
, NULL
, NULL
);
581 while (rx_slot
!= NULL
) {
582 // Check for a waiting packet
583 lck_mtx_lock(&pcb
->utun_input_chain_lock
);
584 mbuf_t data
= pcb
->utun_input_chain
;
586 lck_mtx_unlock(&pcb
->utun_input_chain_lock
);
590 // Allocate rx packet
591 kern_packet_t rx_ph
= 0;
592 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
593 if (__improbable(error
!= 0)) {
594 STATS_INC(nifs
, NETIF_STATS_NOMEM_PKT
);
595 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
596 lck_mtx_unlock(&pcb
->utun_input_chain_lock
);
600 // Advance waiting packets
601 pcb
->utun_input_chain
= data
->m_nextpkt
;
602 data
->m_nextpkt
= NULL
;
603 if (pcb
->utun_input_chain
== NULL
) {
604 pcb
->utun_input_chain_last
= NULL
;
606 lck_mtx_unlock(&pcb
->utun_input_chain_lock
);
608 size_t header_offset
= UTUN_HEADER_SIZE(pcb
);
609 size_t length
= mbuf_pkthdr_len(data
);
611 if (length
< header_offset
) {
614 kern_pbufpool_free(rx_pp
, rx_ph
);
615 STATS_INC(nifs
, NETIF_STATS_BADLEN
);
616 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
617 printf("utun_netif_sync_rx %s: legacy packet length too short for header %zu < %zu\n",
618 pcb
->utun_ifp
->if_xname
, length
, header_offset
);
622 length
-= header_offset
;
623 if (length
> rx_pp
->pp_buflet_size
) {
626 kern_pbufpool_free(rx_pp
, rx_ph
);
627 STATS_INC(nifs
, NETIF_STATS_BADLEN
);
628 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
629 printf("utun_netif_sync_rx %s: legacy packet length %zu > %u\n",
630 pcb
->utun_ifp
->if_xname
, length
, rx_pp
->pp_buflet_size
);
634 mbuf_pkthdr_setrcvif(data
, pcb
->utun_ifp
);
637 kern_buflet_t rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
638 VERIFY(rx_buf
!= NULL
);
639 void *rx_baddr
= kern_buflet_get_object_address(rx_buf
);
640 VERIFY(rx_baddr
!= NULL
);
642 // Copy-in data from mbuf to buflet
643 mbuf_copydata(data
, header_offset
, length
, (void *)rx_baddr
);
644 kern_packet_clear_flow_uuid(rx_ph
); // Zero flow id
646 // Finalize and attach the packet
647 error
= kern_buflet_set_data_offset(rx_buf
, 0);
649 error
= kern_buflet_set_data_length(rx_buf
, length
);
651 error
= kern_packet_set_link_header_offset(rx_ph
, 0);
653 error
= kern_packet_set_network_header_offset(rx_ph
, 0);
655 error
= kern_packet_finalize(rx_ph
);
657 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
660 STATS_INC(nifs
, NETIF_STATS_RXPKTS
);
661 STATS_INC(nifs
, NETIF_STATS_RXCOPY_MBUF
);
662 bpf_tap_packet_in(pcb
->utun_ifp
, DLT_RAW
, rx_ph
, NULL
, 0);
664 rx_ring_stats
.kcrsi_slots_transferred
++;
665 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
671 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
674 struct kern_channel_ring_stat_increment tx_ring_stats
;
675 bzero(&tx_ring_stats
, sizeof(tx_ring_stats
));
676 kern_channel_ring_t tx_ring
= pcb
->utun_kpipe_txring
;
677 kern_channel_slot_t tx_pslot
= NULL
;
678 kern_channel_slot_t tx_slot
= NULL
;
679 if (tx_ring
== NULL
) {
680 // Net-If TX ring not set up yet, nothing to read
684 // Unlock utun before entering ring
685 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
687 (void)kr_enter(tx_ring
, TRUE
);
689 // Lock again after entering and validate
690 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
691 if (tx_ring
!= pcb
->utun_kpipe_txring
) {
695 tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
696 if (tx_slot
== NULL
) {
697 // Nothing to read, don't bother signalling
701 while (rx_slot
!= NULL
&& tx_slot
!= NULL
) {
702 // Allocate rx packet
703 kern_packet_t rx_ph
= 0;
704 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
708 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
710 /* Skip slot if packet is zero-length or marked as dropped (QUMF_DROPPED) */
715 /* XXX We could try this alloc before advancing the slot to avoid
716 * dropping the packet on failure to allocate.
718 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
719 if (__improbable(error
!= 0)) {
720 STATS_INC(nifs
, NETIF_STATS_NOMEM_PKT
);
721 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
725 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
726 VERIFY(tx_buf
!= NULL
);
727 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
728 VERIFY(tx_baddr
!= 0);
729 tx_baddr
+= kern_buflet_get_data_offset(tx_buf
);
731 // Check packet length
732 size_t header_offset
= UTUN_HEADER_SIZE(pcb
);
733 uint32_t tx_length
= kern_packet_get_data_length(tx_ph
);
734 if (tx_length
< header_offset
) {
735 // Packet is too small
736 kern_pbufpool_free(rx_pp
, rx_ph
);
737 STATS_INC(nifs
, NETIF_STATS_BADLEN
);
738 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
739 printf("utun_netif_sync_rx %s: packet length too short for header %u < %zu\n",
740 pcb
->utun_ifp
->if_xname
, tx_length
, header_offset
);
744 size_t length
= MIN(tx_length
- header_offset
,
745 pcb
->utun_slot_size
);
747 tx_ring_stats
.kcrsi_slots_transferred
++;
748 tx_ring_stats
.kcrsi_bytes_transferred
+= length
;
751 kern_buflet_t rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
752 VERIFY(rx_buf
!= NULL
);
753 void *rx_baddr
= kern_buflet_get_object_address(rx_buf
);
754 VERIFY(rx_baddr
!= NULL
);
756 // Copy-in data from tx to rx
757 memcpy((void *)rx_baddr
, (void *)(tx_baddr
+ header_offset
), length
);
758 kern_packet_clear_flow_uuid(rx_ph
); // Zero flow id
760 // Finalize and attach the packet
761 error
= kern_buflet_set_data_offset(rx_buf
, 0);
763 error
= kern_buflet_set_data_length(rx_buf
, length
);
765 error
= kern_packet_set_link_header_offset(rx_ph
, 0);
767 error
= kern_packet_set_network_header_offset(rx_ph
, 0);
769 error
= kern_packet_finalize(rx_ph
);
771 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
774 STATS_INC(nifs
, NETIF_STATS_RXPKTS
);
775 STATS_INC(nifs
, NETIF_STATS_RXCOPY_DIRECT
);
776 bpf_tap_packet_in(pcb
->utun_ifp
, DLT_RAW
, rx_ph
, NULL
, 0);
778 rx_ring_stats
.kcrsi_slots_transferred
++;
779 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
782 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
787 kern_channel_advance_slot(rx_ring
, rx_pslot
);
788 kern_channel_increment_ring_net_stats(rx_ring
, pcb
->utun_ifp
, &rx_ring_stats
);
792 kern_channel_advance_slot(tx_ring
, tx_pslot
);
793 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
794 (void)kern_channel_reclaim(tx_ring
);
797 // Unlock first, then exit ring
798 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
799 if (tx_ring
!= NULL
) {
800 if (tx_pslot
!= NULL
) {
801 kern_channel_notify(tx_ring
, 0);
810 utun_nexus_ifattach(struct utun_pcb
*pcb
,
811 struct ifnet_init_eparams
*init_params
,
815 nexus_controller_t controller
= kern_nexus_shared_controller();
816 struct kern_nexus_net_init net_init
;
817 struct kern_pbufpool_init pp_init
;
819 nexus_name_t provider_name
;
820 snprintf((char *)provider_name
, sizeof(provider_name
),
821 "com.apple.netif.%s", pcb
->utun_if_xname
);
823 struct kern_nexus_provider_init prov_init
= {
824 .nxpi_version
= KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION
,
825 .nxpi_flags
= NXPIF_VIRTUAL_DEVICE
,
826 .nxpi_pre_connect
= utun_nexus_pre_connect
,
827 .nxpi_connected
= utun_nexus_connected
,
828 .nxpi_pre_disconnect
= utun_netif_pre_disconnect
,
829 .nxpi_disconnected
= utun_nexus_disconnected
,
830 .nxpi_ring_init
= utun_netif_ring_init
,
831 .nxpi_ring_fini
= utun_netif_ring_fini
,
832 .nxpi_slot_init
= NULL
,
833 .nxpi_slot_fini
= NULL
,
834 .nxpi_sync_tx
= utun_netif_sync_tx
,
835 .nxpi_sync_rx
= utun_netif_sync_rx
,
836 .nxpi_tx_doorbell
= utun_netif_tx_doorbell
,
839 nexus_attr_t nxa
= NULL
;
840 err
= kern_nexus_attr_create(&nxa
);
842 printf("%s: kern_nexus_attr_create failed: %d\n",
847 uint64_t slot_buffer_size
= pcb
->utun_slot_size
;
848 err
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_SLOT_BUF_SIZE
, slot_buffer_size
);
851 // Reset ring size for netif nexus to limit memory usage
852 uint64_t ring_size
= pcb
->utun_netif_ring_size
;
853 err
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_TX_SLOTS
, ring_size
);
855 err
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_RX_SLOTS
, ring_size
);
858 pcb
->utun_netif_txring_size
= ring_size
;
860 bzero(&pp_init
, sizeof(pp_init
));
861 pp_init
.kbi_version
= KERN_PBUFPOOL_CURRENT_VERSION
;
862 pp_init
.kbi_packets
= pcb
->utun_netif_ring_size
* 2;
863 pp_init
.kbi_bufsize
= pcb
->utun_slot_size
;
864 pp_init
.kbi_buf_seg_size
= UTUN_IF_DEFAULT_BUF_SEG_SIZE
;
865 pp_init
.kbi_max_frags
= 1;
866 (void) snprintf((char *)pp_init
.kbi_name
, sizeof(pp_init
.kbi_name
),
867 "%s", provider_name
);
869 err
= kern_pbufpool_create(&pp_init
, &pp_init
, &pcb
->utun_netif_pp
, NULL
);
871 printf("%s pbufbool create failed, error %d\n", __func__
, err
);
875 err
= kern_nexus_controller_register_provider(controller
,
881 &pcb
->utun_nx
.if_provider
);
883 printf("%s register provider failed, error %d\n",
888 bzero(&net_init
, sizeof(net_init
));
889 net_init
.nxneti_version
= KERN_NEXUS_NET_CURRENT_VERSION
;
890 net_init
.nxneti_flags
= 0;
891 net_init
.nxneti_eparams
= init_params
;
892 net_init
.nxneti_lladdr
= NULL
;
893 net_init
.nxneti_prepare
= utun_netif_prepare
;
894 net_init
.nxneti_tx_pbufpool
= pcb
->utun_netif_pp
;
895 err
= kern_nexus_controller_alloc_net_provider_instance(controller
,
896 pcb
->utun_nx
.if_provider
,
898 &pcb
->utun_nx
.if_instance
,
902 printf("%s alloc_net_provider_instance failed, %d\n",
904 kern_nexus_controller_deregister_provider(controller
,
905 pcb
->utun_nx
.if_provider
);
906 uuid_clear(pcb
->utun_nx
.if_provider
);
912 kern_nexus_attr_destroy(nxa
);
914 if (err
&& pcb
->utun_netif_pp
!= NULL
) {
915 kern_pbufpool_destroy(pcb
->utun_netif_pp
);
916 pcb
->utun_netif_pp
= NULL
;
922 utun_detach_provider_and_instance(uuid_t provider
, uuid_t instance
)
924 nexus_controller_t controller
= kern_nexus_shared_controller();
927 if (!uuid_is_null(instance
)) {
928 err
= kern_nexus_controller_free_provider_instance(controller
,
931 printf("%s free_provider_instance failed %d\n",
934 uuid_clear(instance
);
936 if (!uuid_is_null(provider
)) {
937 err
= kern_nexus_controller_deregister_provider(controller
,
940 printf("%s deregister_provider %d\n", __func__
, err
);
942 uuid_clear(provider
);
948 utun_nexus_detach(struct utun_pcb
*pcb
)
950 utun_nx_t nx
= &pcb
->utun_nx
;
951 nexus_controller_t controller
= kern_nexus_shared_controller();
954 if (!uuid_is_null(nx
->ms_host
)) {
955 err
= kern_nexus_ifdetach(controller
,
959 printf("%s: kern_nexus_ifdetach ms host failed %d\n",
964 if (!uuid_is_null(nx
->ms_device
)) {
965 err
= kern_nexus_ifdetach(controller
,
969 printf("%s: kern_nexus_ifdetach ms device failed %d\n",
974 utun_detach_provider_and_instance(nx
->if_provider
,
976 utun_detach_provider_and_instance(nx
->ms_provider
,
979 if (pcb
->utun_netif_pp
!= NULL
) {
980 kern_pbufpool_destroy(pcb
->utun_netif_pp
);
981 pcb
->utun_netif_pp
= NULL
;
983 memset(nx
, 0, sizeof(*nx
));
987 utun_create_fs_provider_and_instance(struct utun_pcb
*pcb
,
988 uint32_t subtype
, const char *type_name
,
990 uuid_t
*provider
, uuid_t
*instance
)
992 nexus_attr_t attr
= NULL
;
993 nexus_controller_t controller
= kern_nexus_shared_controller();
996 struct kern_nexus_init init
;
997 nexus_name_t provider_name
;
999 err
= kern_nexus_get_builtin_domain_provider(NEXUS_TYPE_FLOW_SWITCH
,
1002 printf("%s can't get %s provider, error %d\n",
1003 __func__
, type_name
, err
);
1007 err
= kern_nexus_attr_create(&attr
);
1009 printf("%s: kern_nexus_attr_create failed: %d\n",
1014 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_EXTENSIONS
, subtype
);
1017 uint64_t slot_buffer_size
= pcb
->utun_slot_size
;
1018 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_SLOT_BUF_SIZE
, slot_buffer_size
);
1021 // Reset ring size for flowswitch nexus to limit memory usage. Larger RX than netif.
1022 uint64_t tx_ring_size
= pcb
->utun_tx_fsw_ring_size
;
1023 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_TX_SLOTS
, tx_ring_size
);
1025 uint64_t rx_ring_size
= pcb
->utun_rx_fsw_ring_size
;
1026 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_RX_SLOTS
, rx_ring_size
);
1029 snprintf((char *)provider_name
, sizeof(provider_name
),
1030 "com.apple.%s.%s", type_name
, ifname
);
1031 err
= kern_nexus_controller_register_provider(controller
,
1038 kern_nexus_attr_destroy(attr
);
1041 printf("%s register %s provider failed, error %d\n",
1042 __func__
, type_name
, err
);
1045 bzero(&init
, sizeof(init
));
1046 init
.nxi_version
= KERN_NEXUS_CURRENT_VERSION
;
1047 err
= kern_nexus_controller_alloc_provider_instance(controller
,
1052 printf("%s alloc_provider_instance %s failed, %d\n",
1053 __func__
, type_name
, err
);
1054 kern_nexus_controller_deregister_provider(controller
,
1056 uuid_clear(*provider
);
1063 utun_multistack_attach(struct utun_pcb
*pcb
)
1065 nexus_controller_t controller
= kern_nexus_shared_controller();
1067 utun_nx_t nx
= &pcb
->utun_nx
;
1069 // Allocate multistack flowswitch
1070 err
= utun_create_fs_provider_and_instance(pcb
,
1071 NEXUS_EXTENSION_FSW_TYPE_MULTISTACK
,
1073 pcb
->utun_ifp
->if_xname
,
1077 printf("%s: failed to create bridge provider and instance\n",
1082 // Attach multistack to device port
1083 err
= kern_nexus_ifattach(controller
, nx
->ms_instance
,
1084 NULL
, nx
->if_instance
,
1085 FALSE
, &nx
->ms_device
);
1087 printf("%s kern_nexus_ifattach ms device %d\n", __func__
, err
);
1091 // Attach multistack to host port
1092 err
= kern_nexus_ifattach(controller
, nx
->ms_instance
,
1093 NULL
, nx
->if_instance
,
1094 TRUE
, &nx
->ms_host
);
1096 printf("%s kern_nexus_ifattach ms host %d\n", __func__
, err
);
1100 // Extract the agent UUID and save for later
1101 struct kern_nexus
*multistack_nx
= nx_find(nx
->ms_instance
, false);
1102 if (multistack_nx
!= NULL
) {
1103 struct nx_flowswitch
*flowswitch
= NX_FSW_PRIVATE(multistack_nx
);
1104 if (flowswitch
!= NULL
) {
1105 FSW_RLOCK(flowswitch
);
1106 struct fsw_ms_context
*ms_context
= (struct fsw_ms_context
*)flowswitch
->fsw_ops_private
;
1107 if (ms_context
!= NULL
) {
1108 uuid_copy(nx
->ms_agent
, ms_context
->mc_agent_uuid
);
1110 printf("utun_multistack_attach - fsw_ms_context is NULL\n");
1112 FSW_UNLOCK(flowswitch
);
1114 printf("utun_multistack_attach - flowswitch is NULL\n");
1116 nx_release(multistack_nx
);
1118 printf("utun_multistack_attach - unable to find multistack nexus\n");
1124 utun_nexus_detach(pcb
);
1126 errno_t detach_error
= 0;
1127 if ((detach_error
= ifnet_detach(pcb
->utun_ifp
)) != 0) {
1128 panic("utun_multistack_attach - ifnet_detach failed: %d\n", detach_error
);
1136 utun_register_kernel_pipe_nexus(void)
1138 nexus_attr_t nxa
= NULL
;
1141 lck_mtx_lock(&utun_lock
);
1142 if (utun_ncd_refcount
++) {
1143 lck_mtx_unlock(&utun_lock
);
1147 result
= kern_nexus_controller_create(&utun_ncd
);
1149 printf("%s: kern_nexus_controller_create failed: %d\n",
1150 __FUNCTION__
, result
);
1155 result
= kern_nexus_get_builtin_domain_provider(
1156 NEXUS_TYPE_KERNEL_PIPE
, &dom_prov
);
1158 printf("%s: kern_nexus_get_builtin_domain_provider failed: %d\n",
1159 __FUNCTION__
, result
);
1163 struct kern_nexus_provider_init prov_init
= {
1164 .nxpi_version
= KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION
,
1165 .nxpi_flags
= NXPIF_VIRTUAL_DEVICE
,
1166 .nxpi_pre_connect
= utun_nexus_pre_connect
,
1167 .nxpi_connected
= utun_nexus_connected
,
1168 .nxpi_pre_disconnect
= utun_nexus_pre_disconnect
,
1169 .nxpi_disconnected
= utun_nexus_disconnected
,
1170 .nxpi_ring_init
= utun_kpipe_ring_init
,
1171 .nxpi_ring_fini
= utun_kpipe_ring_fini
,
1172 .nxpi_slot_init
= NULL
,
1173 .nxpi_slot_fini
= NULL
,
1174 .nxpi_sync_tx
= utun_kpipe_sync_tx
,
1175 .nxpi_sync_rx
= utun_kpipe_sync_rx
,
1176 .nxpi_tx_doorbell
= NULL
,
1179 result
= kern_nexus_attr_create(&nxa
);
1181 printf("%s: kern_nexus_attr_create failed: %d\n",
1182 __FUNCTION__
, result
);
1186 uint64_t slot_buffer_size
= UTUN_IF_DEFAULT_SLOT_SIZE
;
1187 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_SLOT_BUF_SIZE
, slot_buffer_size
);
1188 VERIFY(result
== 0);
1190 // Reset ring size for kernel pipe nexus to limit memory usage
1191 uint64_t ring_size
= if_utun_ring_size
;
1192 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_TX_SLOTS
, ring_size
);
1193 VERIFY(result
== 0);
1194 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_RX_SLOTS
, ring_size
);
1195 VERIFY(result
== 0);
1197 result
= kern_nexus_controller_register_provider(utun_ncd
,
1199 (const uint8_t *)"com.apple.nexus.utun.kpipe",
1205 printf("%s: kern_nexus_controller_register_provider failed: %d\n",
1206 __FUNCTION__
, result
);
1212 kern_nexus_attr_destroy(nxa
);
1217 kern_nexus_controller_destroy(utun_ncd
);
1220 utun_ncd_refcount
= 0;
1223 lck_mtx_unlock(&utun_lock
);
1229 utun_unregister_kernel_pipe_nexus(void)
1231 lck_mtx_lock(&utun_lock
);
1233 VERIFY(utun_ncd_refcount
> 0);
1235 if (--utun_ncd_refcount
== 0) {
1236 kern_nexus_controller_destroy(utun_ncd
);
1240 lck_mtx_unlock(&utun_lock
);
1243 // For use by socket option, not internally
1245 utun_disable_channel(struct utun_pcb
*pcb
)
1251 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1253 enabled
= pcb
->utun_kpipe_enabled
;
1254 uuid_copy(uuid
, pcb
->utun_kpipe_uuid
);
1256 VERIFY(uuid_is_null(pcb
->utun_kpipe_uuid
) == !enabled
);
1258 pcb
->utun_kpipe_enabled
= 0;
1259 uuid_clear(pcb
->utun_kpipe_uuid
);
1261 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1264 result
= kern_nexus_controller_free_provider_instance(utun_ncd
, uuid
);
1270 if (pcb
->utun_kpipe_pp
!= NULL
) {
1271 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1272 pcb
->utun_kpipe_pp
= NULL
;
1274 utun_unregister_kernel_pipe_nexus();
1281 utun_enable_channel(struct utun_pcb
*pcb
, struct proc
*proc
)
1283 struct kern_nexus_init init
;
1284 struct kern_pbufpool_init pp_init
;
1287 kauth_cred_t cred
= kauth_cred_get();
1288 result
= priv_check_cred(cred
, PRIV_SKYWALK_REGISTER_KERNEL_PIPE
, 0);
1293 result
= utun_register_kernel_pipe_nexus();
1300 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1302 if (pcb
->utun_kpipe_enabled
) {
1303 result
= EEXIST
; // return success instead?
1308 * Make sure we can fit packets in the channel buffers and
1309 * Allow an extra 4 bytes for the protocol number header in the channel
1311 if (pcb
->utun_ifp
->if_mtu
+ UTUN_HEADER_SIZE(pcb
) > pcb
->utun_slot_size
) {
1312 result
= EOPNOTSUPP
;
1316 bzero(&pp_init
, sizeof(pp_init
));
1317 pp_init
.kbi_version
= KERN_PBUFPOOL_CURRENT_VERSION
;
1318 pp_init
.kbi_packets
= pcb
->utun_netif_ring_size
* 2;
1319 pp_init
.kbi_bufsize
= pcb
->utun_slot_size
;
1320 pp_init
.kbi_buf_seg_size
= UTUN_IF_DEFAULT_BUF_SEG_SIZE
;
1321 pp_init
.kbi_max_frags
= 1;
1322 pp_init
.kbi_flags
|= KBIF_QUANTUM
;
1323 (void) snprintf((char *)pp_init
.kbi_name
, sizeof(pp_init
.kbi_name
),
1324 "com.apple.kpipe.%s", pcb
->utun_if_xname
);
1326 result
= kern_pbufpool_create(&pp_init
, &pp_init
, &pcb
->utun_kpipe_pp
,
1329 printf("%s pbufbool create failed, error %d\n", __func__
, result
);
1333 VERIFY(uuid_is_null(pcb
->utun_kpipe_uuid
));
1334 bzero(&init
, sizeof(init
));
1335 init
.nxi_version
= KERN_NEXUS_CURRENT_VERSION
;
1336 init
.nxi_tx_pbufpool
= pcb
->utun_kpipe_pp
;
1337 result
= kern_nexus_controller_alloc_provider_instance(utun_ncd
,
1338 utun_kpipe_uuid
, pcb
, &pcb
->utun_kpipe_uuid
, &init
);
1343 nexus_port_t port
= NEXUS_PORT_KERNEL_PIPE_CLIENT
;
1344 result
= kern_nexus_controller_bind_provider_instance(utun_ncd
,
1345 pcb
->utun_kpipe_uuid
, &port
,
1346 proc_pid(proc
), NULL
, NULL
, 0, NEXUS_BIND_PID
);
1348 kern_nexus_controller_free_provider_instance(utun_ncd
,
1349 pcb
->utun_kpipe_uuid
);
1350 uuid_clear(pcb
->utun_kpipe_uuid
);
1354 pcb
->utun_kpipe_enabled
= 1;
1357 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1360 if (pcb
->utun_kpipe_pp
!= NULL
) {
1361 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1362 pcb
->utun_kpipe_pp
= NULL
;
1364 utun_unregister_kernel_pipe_nexus();
1370 #endif // UTUN_NEXUS
1373 utun_register_control(void)
1375 struct kern_ctl_reg kern_ctl
;
1378 /* Find a unique value for our interface family */
1379 result
= mbuf_tag_id_find(UTUN_CONTROL_NAME
, &utun_family
);
1381 printf("utun_register_control - mbuf_tag_id_find_internal failed: %d\n", result
);
1385 utun_pcb_size
= sizeof(struct utun_pcb
);
1386 utun_pcb_zone
= zinit(utun_pcb_size
,
1387 UTUN_PCB_ZONE_MAX
* utun_pcb_size
,
1388 0, UTUN_PCB_ZONE_NAME
);
1389 if (utun_pcb_zone
== NULL
) {
1390 printf("utun_register_control - zinit(utun_pcb) failed");
1395 utun_register_nexus();
1396 #endif // UTUN_NEXUS
1398 TAILQ_INIT(&utun_head
);
1400 bzero(&kern_ctl
, sizeof(kern_ctl
));
1401 strlcpy(kern_ctl
.ctl_name
, UTUN_CONTROL_NAME
, sizeof(kern_ctl
.ctl_name
));
1402 kern_ctl
.ctl_name
[sizeof(kern_ctl
.ctl_name
) - 1] = 0;
1403 kern_ctl
.ctl_flags
= CTL_FLAG_PRIVILEGED
| CTL_FLAG_REG_EXTENDED
; /* Require root */
1404 kern_ctl
.ctl_sendsize
= 512 * 1024;
1405 kern_ctl
.ctl_recvsize
= 512 * 1024;
1406 kern_ctl
.ctl_bind
= utun_ctl_bind
;
1407 kern_ctl
.ctl_connect
= utun_ctl_connect
;
1408 kern_ctl
.ctl_disconnect
= utun_ctl_disconnect
;
1409 kern_ctl
.ctl_send
= utun_ctl_send
;
1410 kern_ctl
.ctl_setopt
= utun_ctl_setopt
;
1411 kern_ctl
.ctl_getopt
= utun_ctl_getopt
;
1412 kern_ctl
.ctl_rcvd
= utun_ctl_rcvd
;
1414 result
= ctl_register(&kern_ctl
, &utun_kctlref
);
1416 printf("utun_register_control - ctl_register failed: %d\n", result
);
1420 /* Register the protocol plumbers */
1421 if ((result
= proto_register_plumber(PF_INET
, utun_family
,
1422 utun_attach_proto
, NULL
)) != 0) {
1423 printf("utun_register_control - proto_register_plumber(PF_INET, %d) failed: %d\n",
1424 utun_family
, result
);
1425 ctl_deregister(utun_kctlref
);
1429 /* Register the protocol plumbers */
1430 if ((result
= proto_register_plumber(PF_INET6
, utun_family
,
1431 utun_attach_proto
, NULL
)) != 0) {
1432 proto_unregister_plumber(PF_INET
, utun_family
);
1433 ctl_deregister(utun_kctlref
);
1434 printf("utun_register_control - proto_register_plumber(PF_INET6, %d) failed: %d\n",
1435 utun_family
, result
);
1439 utun_lck_attr
= lck_attr_alloc_init();
1440 utun_lck_grp_attr
= lck_grp_attr_alloc_init();
1441 utun_lck_grp
= lck_grp_alloc_init("utun", utun_lck_grp_attr
);
1443 lck_mtx_init(&utun_lock
, utun_lck_grp
, utun_lck_attr
);
1448 /* Kernel control functions */
1451 utun_free_pcb(struct utun_pcb
*pcb
, bool in_list
)
1454 mbuf_freem_list(pcb
->utun_input_chain
);
1455 lck_mtx_destroy(&pcb
->utun_input_chain_lock
, utun_lck_grp
);
1456 #endif // UTUN_NEXUS
1457 lck_rw_destroy(&pcb
->utun_pcb_lock
, utun_lck_grp
);
1459 lck_mtx_lock(&utun_lock
);
1460 TAILQ_REMOVE(&utun_head
, pcb
, utun_chain
);
1461 lck_mtx_unlock(&utun_lock
);
1463 zfree(utun_pcb_zone
, pcb
);
1467 utun_ctl_bind(kern_ctl_ref kctlref
,
1468 struct sockaddr_ctl
*sac
,
1471 struct utun_pcb
*pcb
= zalloc(utun_pcb_zone
);
1472 memset(pcb
, 0, sizeof(*pcb
));
1475 pcb
->utun_ctlref
= kctlref
;
1476 pcb
->utun_unit
= sac
->sc_unit
;
1477 pcb
->utun_max_pending_packets
= 1;
1480 pcb
->utun_use_netif
= false;
1481 pcb
->utun_slot_size
= UTUN_IF_DEFAULT_SLOT_SIZE
;
1482 pcb
->utun_netif_ring_size
= UTUN_IF_DEFAULT_RING_SIZE
;
1483 pcb
->utun_tx_fsw_ring_size
= UTUN_IF_DEFAULT_TX_FSW_RING_SIZE
;
1484 pcb
->utun_rx_fsw_ring_size
= UTUN_IF_DEFAULT_RX_FSW_RING_SIZE
;
1485 #endif // UTUN_NEXUS
1487 lck_mtx_init(&pcb
->utun_input_chain_lock
, utun_lck_grp
, utun_lck_attr
);
1488 lck_rw_init(&pcb
->utun_pcb_lock
, utun_lck_grp
, utun_lck_attr
);
1494 utun_ctl_connect(kern_ctl_ref kctlref
,
1495 struct sockaddr_ctl
*sac
,
1498 struct ifnet_init_eparams utun_init
= {};
1501 if (*unitinfo
== NULL
) {
1502 (void)utun_ctl_bind(kctlref
, sac
, unitinfo
);
1505 struct utun_pcb
*pcb
= *unitinfo
;
1507 lck_mtx_lock(&utun_lock
);
1509 /* Find some open interface id */
1510 u_int32_t chosen_unique_id
= 1;
1511 struct utun_pcb
*next_pcb
= TAILQ_LAST(&utun_head
, utun_list
);
1512 if (next_pcb
!= NULL
) {
1513 /* List was not empty, add one to the last item */
1514 chosen_unique_id
= next_pcb
->utun_unique_id
+ 1;
1518 * If this wrapped the id number, start looking at
1519 * the front of the list for an unused id.
1521 if (chosen_unique_id
== 0) {
1522 /* Find the next unused ID */
1523 chosen_unique_id
= 1;
1524 TAILQ_FOREACH(next_pcb
, &utun_head
, utun_chain
) {
1525 if (next_pcb
->utun_unique_id
> chosen_unique_id
) {
1526 /* We found a gap */
1530 chosen_unique_id
= next_pcb
->utun_unique_id
+ 1;
1535 pcb
->utun_unique_id
= chosen_unique_id
;
1537 if (next_pcb
!= NULL
) {
1538 TAILQ_INSERT_BEFORE(next_pcb
, pcb
, utun_chain
);
1540 TAILQ_INSERT_TAIL(&utun_head
, pcb
, utun_chain
);
1542 lck_mtx_unlock(&utun_lock
);
1544 snprintf(pcb
->utun_if_xname
, sizeof(pcb
->utun_if_xname
), "utun%d", pcb
->utun_unit
- 1);
1545 snprintf(pcb
->utun_unique_name
, sizeof(pcb
->utun_unique_name
), "utunid%d", pcb
->utun_unique_id
- 1);
1546 printf("utun_ctl_connect: creating interface %s (id %s)\n", pcb
->utun_if_xname
, pcb
->utun_unique_name
);
1548 /* Create the interface */
1549 bzero(&utun_init
, sizeof(utun_init
));
1550 utun_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1551 utun_init
.len
= sizeof(utun_init
);
1554 if (pcb
->utun_use_netif
) {
1555 utun_init
.flags
= (IFNET_INIT_SKYWALK_NATIVE
| IFNET_INIT_NX_NOAUTO
);
1556 utun_init
.tx_headroom
= UTUN_IF_HEADROOM_SIZE
;
1558 #endif // UTUN_NEXUS
1560 utun_init
.flags
= IFNET_INIT_NX_NOAUTO
;
1561 utun_init
.start
= utun_start
;
1562 utun_init
.framer_extended
= utun_framer
;
1564 utun_init
.name
= "utun";
1565 utun_init
.unit
= pcb
->utun_unit
- 1;
1566 utun_init
.uniqueid
= pcb
->utun_unique_name
;
1567 utun_init
.uniqueid_len
= strlen(pcb
->utun_unique_name
);
1568 utun_init
.family
= utun_family
;
1569 utun_init
.subfamily
= IFNET_SUBFAMILY_UTUN
;
1570 utun_init
.type
= IFT_OTHER
;
1571 utun_init
.demux
= utun_demux
;
1572 utun_init
.add_proto
= utun_add_proto
;
1573 utun_init
.del_proto
= utun_del_proto
;
1574 utun_init
.softc
= pcb
;
1575 utun_init
.ioctl
= utun_ioctl
;
1576 utun_init
.detach
= utun_detached
;
1579 if (pcb
->utun_use_netif
) {
1580 result
= utun_nexus_ifattach(pcb
, &utun_init
, &pcb
->utun_ifp
);
1582 printf("utun_ctl_connect - utun_nexus_ifattach failed: %d\n", result
);
1583 utun_free_pcb(pcb
, true);
1588 result
= utun_multistack_attach(pcb
);
1590 printf("utun_ctl_connect - utun_multistack_attach failed: %d\n", result
);
1596 bpfattach(pcb
->utun_ifp
, DLT_RAW
, 0);
1598 #endif // UTUN_NEXUS
1601 * Upon success, this holds an ifnet reference which we will
1602 * release via ifnet_release() at final detach time.
1604 result
= ifnet_allocate_extended(&utun_init
, &pcb
->utun_ifp
);
1606 printf("utun_ctl_connect - ifnet_allocate failed: %d\n", result
);
1607 utun_free_pcb(pcb
, true);
1612 /* Set flags and additional information. */
1613 ifnet_set_mtu(pcb
->utun_ifp
, UTUN_DEFAULT_MTU
);
1614 ifnet_set_flags(pcb
->utun_ifp
, IFF_UP
| IFF_MULTICAST
| IFF_POINTOPOINT
, 0xffff);
1616 /* The interface must generate its own IPv6 LinkLocal address,
1617 * if possible following the recommendation of RFC2472 to the 64bit interface ID
1619 ifnet_set_eflags(pcb
->utun_ifp
, IFEF_NOAUTOIPV6LL
, IFEF_NOAUTOIPV6LL
);
1621 /* Reset the stats in case as the interface may have been recycled */
1622 struct ifnet_stats_param stats
;
1623 bzero(&stats
, sizeof(struct ifnet_stats_param
));
1624 ifnet_set_stat(pcb
->utun_ifp
, &stats
);
1626 /* Attach the interface */
1627 result
= ifnet_attach(pcb
->utun_ifp
, NULL
);
1629 printf("utun_ctl_connect - ifnet_attach failed: %d\n", result
);
1630 /* Release reference now since attach failed */
1631 ifnet_release(pcb
->utun_ifp
);
1632 utun_free_pcb(pcb
, true);
1638 bpfattach(pcb
->utun_ifp
, DLT_NULL
, UTUN_HEADER_SIZE(pcb
));
1641 /* The interfaces resoures allocated, mark it as running */
1642 ifnet_set_flags(pcb
->utun_ifp
, IFF_RUNNING
, IFF_RUNNING
);
1648 utun_detach_ip(ifnet_t interface
,
1649 protocol_family_t protocol
,
1652 errno_t result
= EPROTONOSUPPORT
;
1654 /* Attempt a detach */
1655 if (protocol
== PF_INET
) {
1658 bzero(&ifr
, sizeof(ifr
));
1659 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
1660 ifnet_name(interface
), ifnet_unit(interface
));
1662 result
= sock_ioctl(pf_socket
, SIOCPROTODETACH
, &ifr
);
1663 } else if (protocol
== PF_INET6
) {
1664 struct in6_ifreq ifr6
;
1666 bzero(&ifr6
, sizeof(ifr6
));
1667 snprintf(ifr6
.ifr_name
, sizeof(ifr6
.ifr_name
), "%s%d",
1668 ifnet_name(interface
), ifnet_unit(interface
));
1670 result
= sock_ioctl(pf_socket
, SIOCPROTODETACH_IN6
, &ifr6
);
1677 utun_remove_address(ifnet_t interface
,
1678 protocol_family_t protocol
,
1684 /* Attempt a detach */
1685 if (protocol
== PF_INET
) {
1688 bzero(&ifr
, sizeof(ifr
));
1689 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
1690 ifnet_name(interface
), ifnet_unit(interface
));
1691 result
= ifaddr_address(address
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1693 printf("utun_remove_address - ifaddr_address failed: %d", result
);
1695 result
= sock_ioctl(pf_socket
, SIOCDIFADDR
, &ifr
);
1697 printf("utun_remove_address - SIOCDIFADDR failed: %d", result
);
1700 } else if (protocol
== PF_INET6
) {
1701 struct in6_ifreq ifr6
;
1703 bzero(&ifr6
, sizeof(ifr6
));
1704 snprintf(ifr6
.ifr_name
, sizeof(ifr6
.ifr_name
), "%s%d",
1705 ifnet_name(interface
), ifnet_unit(interface
));
1706 result
= ifaddr_address(address
, (struct sockaddr
*)&ifr6
.ifr_addr
,
1707 sizeof(ifr6
.ifr_addr
));
1709 printf("utun_remove_address - ifaddr_address failed (v6): %d",
1712 result
= sock_ioctl(pf_socket
, SIOCDIFADDR_IN6
, &ifr6
);
1714 printf("utun_remove_address - SIOCDIFADDR_IN6 failed: %d",
1722 utun_cleanup_family(ifnet_t interface
,
1723 protocol_family_t protocol
)
1726 socket_t pf_socket
= NULL
;
1727 ifaddr_t
*addresses
= NULL
;
1730 if (protocol
!= PF_INET
&& protocol
!= PF_INET6
) {
1731 printf("utun_cleanup_family - invalid protocol family %d\n", protocol
);
1735 /* Create a socket for removing addresses and detaching the protocol */
1736 result
= sock_socket(protocol
, SOCK_DGRAM
, 0, NULL
, NULL
, &pf_socket
);
1738 if (result
!= EAFNOSUPPORT
) {
1739 printf("utun_cleanup_family - failed to create %s socket: %d\n",
1740 protocol
== PF_INET
? "IP" : "IPv6", result
);
1745 /* always set SS_PRIV, we want to close and detach regardless */
1746 sock_setpriv(pf_socket
, 1);
1748 result
= utun_detach_ip(interface
, protocol
, pf_socket
);
1749 if (result
== 0 || result
== ENXIO
) {
1750 /* We are done! We either detached or weren't attached. */
1752 } else if (result
!= EBUSY
) {
1753 /* Uh, not really sure what happened here... */
1754 printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result
);
1759 * At this point, we received an EBUSY error. This means there are
1760 * addresses attached. We should detach them and then try again.
1762 result
= ifnet_get_address_list_family(interface
, &addresses
, protocol
);
1764 printf("fnet_get_address_list_family(%s%d, 0xblah, %s) - failed: %d\n",
1765 ifnet_name(interface
), ifnet_unit(interface
),
1766 protocol
== PF_INET
? "PF_INET" : "PF_INET6", result
);
1770 for (i
= 0; addresses
[i
] != 0; i
++) {
1771 utun_remove_address(interface
, protocol
, addresses
[i
], pf_socket
);
1773 ifnet_free_address_list(addresses
);
1777 * The addresses should be gone, we should try the remove again.
1779 result
= utun_detach_ip(interface
, protocol
, pf_socket
);
1780 if (result
!= 0 && result
!= ENXIO
) {
1781 printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result
);
1785 if (pf_socket
!= NULL
) {
1786 sock_close(pf_socket
);
1789 if (addresses
!= NULL
) {
1790 ifnet_free_address_list(addresses
);
1795 utun_ctl_disconnect(__unused kern_ctl_ref kctlref
,
1796 __unused u_int32_t unit
,
1799 struct utun_pcb
*pcb
= unitinfo
;
1808 // Tell the nexus to stop all rings
1809 if (pcb
->utun_netif_nexus
!= NULL
) {
1810 kern_nexus_stop(pcb
->utun_netif_nexus
);
1812 #endif // UTUN_NEXUS
1814 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1818 uuid_copy(kpipe_uuid
, pcb
->utun_kpipe_uuid
);
1819 uuid_clear(pcb
->utun_kpipe_uuid
);
1820 pcb
->utun_kpipe_enabled
= FALSE
;
1821 #endif // UTUN_NEXUS
1823 pcb
->utun_ctlref
= NULL
;
1825 ifp
= pcb
->utun_ifp
;
1828 // Tell the nexus to stop all rings
1829 if (pcb
->utun_netif_nexus
!= NULL
) {
1831 * Quiesce the interface and flush any pending outbound packets.
1835 /* Increment refcnt, but detach interface */
1836 ifnet_incr_iorefcnt(ifp
);
1837 if ((result
= ifnet_detach(ifp
)) != 0) {
1838 panic("utun_ctl_disconnect - ifnet_detach failed: %d\n", result
);
1842 * We want to do everything in our power to ensure that the interface
1843 * really goes away when the socket is closed. We must remove IP/IPv6
1844 * addresses and detach the protocols. Finally, we can remove and
1845 * release the interface.
1847 utun_cleanup_family(ifp
, AF_INET
);
1848 utun_cleanup_family(ifp
, AF_INET6
);
1850 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1852 if (!uuid_is_null(kpipe_uuid
)) {
1853 if (kern_nexus_controller_free_provider_instance(utun_ncd
, kpipe_uuid
) == 0) {
1854 if (pcb
->utun_kpipe_pp
!= NULL
) {
1855 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1856 pcb
->utun_kpipe_pp
= NULL
;
1858 utun_unregister_kernel_pipe_nexus();
1861 utun_nexus_detach(pcb
);
1863 /* Decrement refcnt to finish detaching and freeing */
1864 ifnet_decr_iorefcnt(ifp
);
1866 #endif // UTUN_NEXUS
1868 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1871 if (!uuid_is_null(kpipe_uuid
)) {
1872 if (kern_nexus_controller_free_provider_instance(utun_ncd
, kpipe_uuid
) == 0) {
1873 if (pcb
->utun_kpipe_pp
!= NULL
) {
1874 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1875 pcb
->utun_kpipe_pp
= NULL
;
1877 utun_unregister_kernel_pipe_nexus();
1880 #endif // UTUN_NEXUS
1883 * We want to do everything in our power to ensure that the interface
1884 * really goes away when the socket is closed. We must remove IP/IPv6
1885 * addresses and detach the protocols. Finally, we can remove and
1886 * release the interface.
1888 utun_cleanup_family(ifp
, AF_INET
);
1889 utun_cleanup_family(ifp
, AF_INET6
);
1892 * Detach now; utun_detach() will be called asynchronously once
1893 * the I/O reference count drops to 0. There we will invoke
1896 if ((result
= ifnet_detach(ifp
)) != 0) {
1897 printf("utun_ctl_disconnect - ifnet_detach failed: %d\n", result
);
1901 // Bound, but not connected
1902 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1903 utun_free_pcb(pcb
, false);
1910 utun_ctl_send(__unused kern_ctl_ref kctlref
,
1911 __unused u_int32_t unit
,
1917 * The userland ABI requires the first four bytes have the protocol family
1918 * in network byte order: swap them
1920 if (m_pktlen(m
) >= (int32_t)UTUN_HEADER_SIZE((struct utun_pcb
*)unitinfo
)) {
1921 *(protocol_family_t
*)mbuf_data(m
) = ntohl(*(protocol_family_t
*)mbuf_data(m
));
1923 printf("%s - unexpected short mbuf pkt len %d\n", __func__
, m_pktlen(m
));
1926 return utun_pkt_input((struct utun_pcb
*)unitinfo
, m
);
1930 utun_ctl_setopt(__unused kern_ctl_ref kctlref
,
1931 __unused u_int32_t unit
,
1937 struct utun_pcb
*pcb
= unitinfo
;
1939 /* check for privileges for privileged options */
1941 case UTUN_OPT_FLAGS
:
1942 case UTUN_OPT_EXT_IFDATA_STATS
:
1943 case UTUN_OPT_SET_DELEGATE_INTERFACE
:
1944 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
1951 case UTUN_OPT_FLAGS
:
1952 if (len
!= sizeof(u_int32_t
)) {
1955 if (pcb
->utun_ifp
== NULL
) {
1956 // Only can set after connecting
1961 if (pcb
->utun_use_netif
) {
1962 pcb
->utun_flags
= *(u_int32_t
*)data
;
1964 #endif // UTUN_NEXUS
1966 u_int32_t old_flags
= pcb
->utun_flags
;
1967 pcb
->utun_flags
= *(u_int32_t
*)data
;
1968 if (((old_flags
^ pcb
->utun_flags
) & UTUN_FLAGS_ENABLE_PROC_UUID
)) {
1969 // If UTUN_FLAGS_ENABLE_PROC_UUID flag changed, update bpf
1970 bpfdetach(pcb
->utun_ifp
);
1971 bpfattach(pcb
->utun_ifp
, DLT_NULL
, UTUN_HEADER_SIZE(pcb
));
1977 case UTUN_OPT_EXT_IFDATA_STATS
:
1978 if (len
!= sizeof(int)) {
1982 if (pcb
->utun_ifp
== NULL
) {
1983 // Only can set after connecting
1987 pcb
->utun_ext_ifdata_stats
= (*(int *)data
) ? 1 : 0;
1990 case UTUN_OPT_INC_IFDATA_STATS_IN
:
1991 case UTUN_OPT_INC_IFDATA_STATS_OUT
: {
1992 struct utun_stats_param
*utsp
= (struct utun_stats_param
*)data
;
1994 if (utsp
== NULL
|| len
< sizeof(struct utun_stats_param
)) {
1998 if (pcb
->utun_ifp
== NULL
) {
1999 // Only can set after connecting
2003 if (!pcb
->utun_ext_ifdata_stats
) {
2007 if (opt
== UTUN_OPT_INC_IFDATA_STATS_IN
) {
2008 ifnet_stat_increment_in(pcb
->utun_ifp
, utsp
->utsp_packets
,
2009 utsp
->utsp_bytes
, utsp
->utsp_errors
);
2011 ifnet_stat_increment_out(pcb
->utun_ifp
, utsp
->utsp_packets
,
2012 utsp
->utsp_bytes
, utsp
->utsp_errors
);
2016 case UTUN_OPT_SET_DELEGATE_INTERFACE
: {
2017 ifnet_t del_ifp
= NULL
;
2018 char name
[IFNAMSIZ
];
2020 if (len
> IFNAMSIZ
- 1) {
2024 if (pcb
->utun_ifp
== NULL
) {
2025 // Only can set after connecting
2029 if (len
!= 0) { /* if len==0, del_ifp will be NULL causing the delegate to be removed */
2030 bcopy(data
, name
, len
);
2032 result
= ifnet_find_by_name(name
, &del_ifp
);
2035 result
= ifnet_set_delegate(pcb
->utun_ifp
, del_ifp
);
2037 ifnet_release(del_ifp
);
2042 case UTUN_OPT_MAX_PENDING_PACKETS
: {
2043 u_int32_t max_pending_packets
= 0;
2044 if (len
!= sizeof(u_int32_t
)) {
2048 max_pending_packets
= *(u_int32_t
*)data
;
2049 if (max_pending_packets
== 0) {
2053 pcb
->utun_max_pending_packets
= max_pending_packets
;
2057 case UTUN_OPT_ENABLE_CHANNEL
: {
2058 if (len
!= sizeof(int)) {
2062 if (pcb
->utun_ifp
== NULL
) {
2063 // Only can set after connecting
2068 result
= utun_enable_channel(pcb
, current_proc());
2070 result
= utun_disable_channel(pcb
);
2074 case UTUN_OPT_ENABLE_FLOWSWITCH
: {
2075 if (len
!= sizeof(int)) {
2079 if (pcb
->utun_ifp
== NULL
) {
2080 // Only can set after connecting
2084 if (!if_is_netagent_enabled()) {
2088 if (uuid_is_null(pcb
->utun_nx
.ms_agent
)) {
2094 if_add_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2095 pcb
->utun_needs_netagent
= true;
2097 pcb
->utun_needs_netagent
= false;
2098 if_delete_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2102 case UTUN_OPT_ENABLE_NETIF
: {
2103 if (len
!= sizeof(int)) {
2107 if (pcb
->utun_ifp
!= NULL
) {
2108 // Only can set before connecting
2112 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
2113 pcb
->utun_use_netif
= !!(*(int *)data
);
2114 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
2117 case UTUN_OPT_SLOT_SIZE
: {
2118 if (len
!= sizeof(u_int32_t
)) {
2122 if (pcb
->utun_ifp
!= NULL
) {
2123 // Only can set before connecting
2127 u_int32_t slot_size
= *(u_int32_t
*)data
;
2128 if (slot_size
< UTUN_IF_MIN_SLOT_SIZE
||
2129 slot_size
> UTUN_IF_MAX_SLOT_SIZE
) {
2132 pcb
->utun_slot_size
= slot_size
;
2135 case UTUN_OPT_NETIF_RING_SIZE
: {
2136 if (len
!= sizeof(u_int32_t
)) {
2140 if (pcb
->utun_ifp
!= NULL
) {
2141 // Only can set before connecting
2145 u_int32_t ring_size
= *(u_int32_t
*)data
;
2146 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2147 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2150 pcb
->utun_netif_ring_size
= ring_size
;
2153 case UTUN_OPT_TX_FSW_RING_SIZE
: {
2154 if (len
!= sizeof(u_int32_t
)) {
2158 if (pcb
->utun_ifp
!= NULL
) {
2159 // Only can set before connecting
2163 u_int32_t ring_size
= *(u_int32_t
*)data
;
2164 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2165 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2168 pcb
->utun_tx_fsw_ring_size
= ring_size
;
2171 case UTUN_OPT_RX_FSW_RING_SIZE
: {
2172 if (len
!= sizeof(u_int32_t
)) {
2176 if (pcb
->utun_ifp
!= NULL
) {
2177 // Only can set before connecting
2181 u_int32_t ring_size
= *(u_int32_t
*)data
;
2182 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2183 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2186 pcb
->utun_rx_fsw_ring_size
= ring_size
;
2189 #endif // UTUN_NEXUS
2191 result
= ENOPROTOOPT
;
2200 utun_ctl_getopt(__unused kern_ctl_ref kctlref
,
2201 __unused u_int32_t unit
,
2207 struct utun_pcb
*pcb
= unitinfo
;
2211 case UTUN_OPT_FLAGS
:
2212 if (*len
!= sizeof(u_int32_t
)) {
2215 *(u_int32_t
*)data
= pcb
->utun_flags
;
2219 case UTUN_OPT_EXT_IFDATA_STATS
:
2220 if (*len
!= sizeof(int)) {
2223 *(int *)data
= (pcb
->utun_ext_ifdata_stats
) ? 1 : 0;
2227 case UTUN_OPT_IFNAME
:
2228 if (*len
< MIN(strlen(pcb
->utun_if_xname
) + 1, sizeof(pcb
->utun_if_xname
))) {
2231 if (pcb
->utun_ifp
== NULL
) {
2232 // Only can get after connecting
2236 *len
= snprintf(data
, *len
, "%s", pcb
->utun_if_xname
) + 1;
2240 case UTUN_OPT_MAX_PENDING_PACKETS
: {
2241 if (*len
!= sizeof(u_int32_t
)) {
2244 *((u_int32_t
*)data
) = pcb
->utun_max_pending_packets
;
2250 case UTUN_OPT_ENABLE_CHANNEL
: {
2251 if (*len
!= sizeof(int)) {
2254 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2255 *(int *)data
= pcb
->utun_kpipe_enabled
;
2256 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2261 case UTUN_OPT_ENABLE_FLOWSWITCH
: {
2262 if (*len
!= sizeof(int)) {
2265 *(int *)data
= if_check_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2270 case UTUN_OPT_ENABLE_NETIF
: {
2271 if (*len
!= sizeof(int)) {
2274 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2275 *(int *)data
= !!pcb
->utun_use_netif
;
2276 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2281 case UTUN_OPT_GET_CHANNEL_UUID
: {
2282 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2283 if (uuid_is_null(pcb
->utun_kpipe_uuid
)) {
2285 } else if (*len
!= sizeof(uuid_t
)) {
2288 uuid_copy(data
, pcb
->utun_kpipe_uuid
);
2290 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2293 case UTUN_OPT_SLOT_SIZE
: {
2294 if (*len
!= sizeof(u_int32_t
)) {
2297 *(u_int32_t
*)data
= pcb
->utun_slot_size
;
2301 case UTUN_OPT_NETIF_RING_SIZE
: {
2302 if (*len
!= sizeof(u_int32_t
)) {
2305 *(u_int32_t
*)data
= pcb
->utun_netif_ring_size
;
2309 case UTUN_OPT_TX_FSW_RING_SIZE
: {
2310 if (*len
!= sizeof(u_int32_t
)) {
2313 *(u_int32_t
*)data
= pcb
->utun_tx_fsw_ring_size
;
2317 case UTUN_OPT_RX_FSW_RING_SIZE
: {
2318 if (*len
!= sizeof(u_int32_t
)) {
2321 *(u_int32_t
*)data
= pcb
->utun_rx_fsw_ring_size
;
2325 #endif // UTUN_NEXUS
2328 result
= ENOPROTOOPT
;
2336 utun_ctl_rcvd(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
, int flags
)
2338 #pragma unused(flags)
2339 bool reenable_output
= false;
2340 struct utun_pcb
*pcb
= unitinfo
;
2344 ifnet_lock_exclusive(pcb
->utun_ifp
);
2346 u_int32_t utun_packet_cnt
;
2347 errno_t error_pc
= ctl_getenqueuepacketcount(kctlref
, unit
, &utun_packet_cnt
);
2348 if (error_pc
!= 0) {
2349 printf("utun_ctl_rcvd: ctl_getenqueuepacketcount returned error %d\n", error_pc
);
2350 utun_packet_cnt
= 0;
2353 if (utun_packet_cnt
< pcb
->utun_max_pending_packets
) {
2354 reenable_output
= true;
2357 if (reenable_output
) {
2358 errno_t error
= ifnet_enable_output(pcb
->utun_ifp
);
2360 printf("utun_ctl_rcvd: ifnet_enable_output returned error %d\n", error
);
2363 ifnet_lock_done(pcb
->utun_ifp
);
2366 /* Network Interface functions */
2368 utun_start(ifnet_t interface
)
2371 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2373 VERIFY(pcb
!= NULL
);
2376 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2377 if (pcb
->utun_kpipe_enabled
) {
2378 /* It's possible to have channels enabled, but not yet have the channel opened,
2379 * in which case the rxring will not be set
2381 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2382 if (pcb
->utun_kpipe_rxring
!= NULL
) {
2383 kern_channel_notify(pcb
->utun_kpipe_rxring
, 0);
2387 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2388 #endif // UTUN_NEXUS
2391 bool can_accept_packets
= true;
2392 ifnet_lock_shared(pcb
->utun_ifp
);
2394 u_int32_t utun_packet_cnt
;
2395 errno_t error_pc
= ctl_getenqueuepacketcount(pcb
->utun_ctlref
, pcb
->utun_unit
, &utun_packet_cnt
);
2396 if (error_pc
!= 0) {
2397 printf("utun_start: ctl_getenqueuepacketcount returned error %d\n", error_pc
);
2398 utun_packet_cnt
= 0;
2401 can_accept_packets
= (utun_packet_cnt
< pcb
->utun_max_pending_packets
);
2402 if (!can_accept_packets
&& pcb
->utun_ctlref
) {
2403 u_int32_t difference
= 0;
2404 if (ctl_getenqueuereadable(pcb
->utun_ctlref
, pcb
->utun_unit
, &difference
) == 0) {
2405 if (difference
> 0) {
2406 // If the low-water mark has not yet been reached, we still need to enqueue data
2408 can_accept_packets
= true;
2412 if (!can_accept_packets
) {
2413 errno_t error
= ifnet_disable_output(interface
);
2415 printf("utun_start: ifnet_disable_output returned error %d\n", error
);
2417 ifnet_lock_done(pcb
->utun_ifp
);
2420 ifnet_lock_done(pcb
->utun_ifp
);
2421 if (ifnet_dequeue(interface
, &data
) != 0) {
2424 if (utun_output(interface
, data
) != 0) {
2431 utun_output(ifnet_t interface
,
2434 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2437 VERIFY(interface
== pcb
->utun_ifp
);
2440 if (!pcb
->utun_use_netif
)
2441 #endif // UTUN_NEXUS
2443 if (m_pktlen(data
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2444 bpf_tap_out(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
2448 if (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
) {
2454 // otherwise, fall thru to ctl_enqueumbuf
2455 if (pcb
->utun_ctlref
) {
2459 * The ABI requires the protocol in network byte order
2461 if (m_pktlen(data
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2462 *(u_int32_t
*)mbuf_data(data
) = htonl(*(u_int32_t
*)mbuf_data(data
));
2465 length
= mbuf_pkthdr_len(data
);
2466 result
= ctl_enqueuembuf(pcb
->utun_ctlref
, pcb
->utun_unit
, data
, CTL_DATA_EOR
);
2469 printf("utun_output - ctl_enqueuembuf failed: %d\n", result
);
2471 if (!pcb
->utun_use_netif
)
2472 #endif // UTUN_NEXUS
2474 ifnet_stat_increment_out(interface
, 0, 0, 1);
2478 if (!pcb
->utun_use_netif
)
2479 #endif // UTUN_NEXUS
2481 if (!pcb
->utun_ext_ifdata_stats
) {
2482 ifnet_stat_increment_out(interface
, 1, length
, 0);
2494 utun_demux(__unused ifnet_t interface
,
2496 __unused
char *frame_header
,
2497 protocol_family_t
*protocol
)
2500 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2505 while (data
!= NULL
&& mbuf_len(data
) < 1) {
2506 data
= mbuf_next(data
);
2514 if (pcb
->utun_use_netif
) {
2515 ip
= mtod(data
, struct ip
*);
2516 ip_version
= ip
->ip_v
;
2518 switch (ip_version
) {
2520 *protocol
= PF_INET
;
2523 *protocol
= PF_INET6
;
2530 #endif // UTUN_NEXUS
2532 *protocol
= *(u_int32_t
*)mbuf_data(data
);
2539 utun_framer(ifnet_t interface
,
2541 __unused
const struct sockaddr
*dest
,
2542 __unused
const char *desk_linkaddr
,
2543 const char *frame_type
,
2544 u_int32_t
*prepend_len
,
2545 u_int32_t
*postpend_len
)
2547 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2548 VERIFY(interface
== pcb
->utun_ifp
);
2550 u_int32_t header_length
= UTUN_HEADER_SIZE(pcb
);
2551 if (mbuf_prepend(packet
, header_length
, MBUF_DONTWAIT
) != 0) {
2552 printf("utun_framer - ifnet_output prepend failed\n");
2554 ifnet_stat_increment_out(interface
, 0, 0, 1);
2556 // just return, because the buffer was freed in mbuf_prepend
2559 if (prepend_len
!= NULL
) {
2560 *prepend_len
= header_length
;
2562 if (postpend_len
!= NULL
) {
2566 // place protocol number at the beginning of the mbuf
2567 *(protocol_family_t
*)mbuf_data(*packet
) = *(protocol_family_t
*)(uintptr_t)(size_t)frame_type
;
2574 utun_add_proto(__unused ifnet_t interface
,
2575 protocol_family_t protocol
,
2576 __unused
const struct ifnet_demux_desc
*demux_array
,
2577 __unused u_int32_t demux_count
)
2592 utun_del_proto(__unused ifnet_t interface
,
2593 __unused protocol_family_t protocol
)
2599 utun_ioctl(ifnet_t interface
,
2604 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2611 if (pcb
->utun_use_netif
) {
2612 // Make sure we can fit packets in the channel buffers
2613 // Allow for the headroom in the slot
2614 if (((uint64_t)((struct ifreq
*)data
)->ifr_mtu
) + UTUN_IF_HEADROOM_SIZE
> pcb
->utun_slot_size
) {
2617 ifnet_set_mtu(interface
, (uint32_t)((struct ifreq
*)data
)->ifr_mtu
);
2620 #endif // UTUN_NEXUS
2622 ifnet_set_mtu(interface
, ((struct ifreq
*)data
)->ifr_mtu
);
2628 /* ifioctl() takes care of it */
2632 result
= EOPNOTSUPP
;
2639 utun_detached(ifnet_t interface
)
2641 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2642 (void)ifnet_release(interface
);
2643 utun_free_pcb(pcb
, true);
2646 /* Protocol Handlers */
2649 utun_proto_input(__unused ifnet_t interface
,
2650 protocol_family_t protocol
,
2652 __unused
char *frame_header
)
2654 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2656 if (!pcb
->utun_use_netif
)
2657 #endif // UTUN_NEXUS
2659 mbuf_adj(m
, UTUN_HEADER_SIZE(pcb
));
2661 int32_t pktlen
= m
->m_pkthdr
.len
;
2662 if (proto_input(protocol
, m
) != 0) {
2665 if (!pcb
->utun_use_netif
)
2666 #endif // UTUN_NEXUS
2668 ifnet_stat_increment_in(interface
, 0, 0, 1);
2672 if (!pcb
->utun_use_netif
)
2673 #endif // UTUN_NEXUS
2675 ifnet_stat_increment_in(interface
, 1, pktlen
, 0);
2683 utun_proto_pre_output(__unused ifnet_t interface
,
2684 protocol_family_t protocol
,
2685 __unused mbuf_t
*packet
,
2686 __unused
const struct sockaddr
*dest
,
2687 __unused
void *route
,
2689 __unused
char *link_layer_dest
)
2691 *(protocol_family_t
*)(void *)frame_type
= protocol
;
2696 utun_attach_proto(ifnet_t interface
,
2697 protocol_family_t protocol
)
2699 struct ifnet_attach_proto_param proto
;
2701 bzero(&proto
, sizeof(proto
));
2702 proto
.input
= utun_proto_input
;
2703 proto
.pre_output
= utun_proto_pre_output
;
2705 errno_t result
= ifnet_attach_protocol(interface
, protocol
, &proto
);
2706 if (result
!= 0 && result
!= EEXIST
) {
2707 printf("utun_attach_inet - ifnet_attach_protocol %d failed: %d\n",
2715 utun_pkt_input(struct utun_pcb
*pcb
, mbuf_t packet
)
2718 if (pcb
->utun_use_netif
) {
2719 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2721 lck_mtx_lock(&pcb
->utun_input_chain_lock
);
2722 if (pcb
->utun_input_chain
!= NULL
) {
2723 pcb
->utun_input_chain_last
->m_nextpkt
= packet
;
2725 pcb
->utun_input_chain
= packet
;
2727 while (packet
->m_nextpkt
) {
2728 VERIFY(packet
!= packet
->m_nextpkt
);
2729 packet
= packet
->m_nextpkt
;
2731 pcb
->utun_input_chain_last
= packet
;
2732 lck_mtx_unlock(&pcb
->utun_input_chain_lock
);
2734 kern_channel_ring_t rx_ring
= pcb
->utun_netif_rxring
;
2735 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2737 if (rx_ring
!= NULL
) {
2738 kern_channel_notify(rx_ring
, 0);
2743 #endif // IPSEC_NEXUS
2745 mbuf_pkthdr_setrcvif(packet
, pcb
->utun_ifp
);
2747 if (m_pktlen(packet
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2748 bpf_tap_in(pcb
->utun_ifp
, DLT_NULL
, packet
, 0, 0);
2750 if (pcb
->utun_flags
& UTUN_FLAGS_NO_INPUT
) {
2757 if (!pcb
->utun_ext_ifdata_stats
) {
2758 struct ifnet_stat_increment_param incs
= {};
2759 incs
.packets_in
= 1;
2760 incs
.bytes_in
= mbuf_pkthdr_len(packet
);
2761 result
= ifnet_input(pcb
->utun_ifp
, packet
, &incs
);
2763 result
= ifnet_input(pcb
->utun_ifp
, packet
, NULL
);
2766 ifnet_stat_increment_in(pcb
->utun_ifp
, 0, 0, 1);
2768 printf("%s - ifnet_input failed: %d\n", __FUNCTION__
, result
);
2779 utun_nxdp_init(__unused kern_nexus_domain_provider_t domprov
)
2785 utun_nxdp_fini(__unused kern_nexus_domain_provider_t domprov
)
2791 utun_register_nexus(void)
2793 const struct kern_nexus_domain_provider_init dp_init
= {
2794 .nxdpi_version
= KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION
,
2796 .nxdpi_init
= utun_nxdp_init
,
2797 .nxdpi_fini
= utun_nxdp_fini
2801 /* utun_nxdp_init() is called before this function returns */
2802 err
= kern_nexus_register_domain_provider(NEXUS_TYPE_NET_IF
,
2803 (const uint8_t *) "com.apple.utun",
2804 &dp_init
, sizeof(dp_init
),
2807 printf("%s: failed to register domain provider\n", __func__
);
2813 utun_interface_needs_netagent(ifnet_t interface
)
2815 struct utun_pcb
*pcb
= NULL
;
2817 if (interface
== NULL
) {
2821 pcb
= ifnet_softc(interface
);
2827 return pcb
->utun_needs_netagent
== true;
2831 utun_ifnet_set_attrs(ifnet_t ifp
)
2833 /* Set flags and additional information. */
2834 ifnet_set_mtu(ifp
, 1500);
2835 ifnet_set_flags(ifp
, IFF_UP
| IFF_MULTICAST
| IFF_POINTOPOINT
, 0xffff);
2837 /* The interface must generate its own IPv6 LinkLocal address,
2838 * if possible following the recommendation of RFC2472 to the 64bit interface ID
2840 ifnet_set_eflags(ifp
, IFEF_NOAUTOIPV6LL
, IFEF_NOAUTOIPV6LL
);
2846 utun_netif_prepare(kern_nexus_t nexus
, ifnet_t ifp
)
2848 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2849 pcb
->utun_netif_nexus
= nexus
;
2850 return utun_ifnet_set_attrs(ifp
);
2854 utun_nexus_pre_connect(kern_nexus_provider_t nxprov
,
2855 proc_t p
, kern_nexus_t nexus
,
2856 nexus_port_t nexus_port
, kern_channel_t channel
, void **ch_ctx
)
2858 #pragma unused(nxprov, p)
2859 #pragma unused(nexus, nexus_port, channel, ch_ctx)
2864 utun_nexus_connected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2865 kern_channel_t channel
)
2867 #pragma unused(nxprov, channel)
2868 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2869 boolean_t ok
= ifnet_is_attached(pcb
->utun_ifp
, 1);
2870 return ok
? 0 : ENXIO
;
2874 utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2875 kern_channel_t channel
)
2877 #pragma unused(nxprov, nexus, channel)
2881 utun_netif_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2882 kern_channel_t channel
)
2884 #pragma unused(nxprov, nexus, channel)
2888 utun_nexus_disconnected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2889 kern_channel_t channel
)
2891 #pragma unused(nxprov, channel)
2892 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2893 if (pcb
->utun_netif_nexus
== nexus
) {
2894 pcb
->utun_netif_nexus
= NULL
;
2896 ifnet_decr_iorefcnt(pcb
->utun_ifp
);
2900 utun_kpipe_ring_init(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2901 kern_channel_t channel
, kern_channel_ring_t ring
,
2902 boolean_t is_tx_ring
, void **ring_ctx
)
2904 #pragma unused(nxprov)
2905 #pragma unused(channel)
2906 #pragma unused(ring_ctx)
2907 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2909 VERIFY(pcb
->utun_kpipe_rxring
== NULL
);
2910 pcb
->utun_kpipe_rxring
= ring
;
2912 VERIFY(pcb
->utun_kpipe_txring
== NULL
);
2913 pcb
->utun_kpipe_txring
= ring
;
2919 utun_kpipe_ring_fini(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2920 kern_channel_ring_t ring
)
2922 #pragma unused(nxprov)
2923 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2924 if (pcb
->utun_kpipe_rxring
== ring
) {
2925 pcb
->utun_kpipe_rxring
= NULL
;
2926 } else if (pcb
->utun_kpipe_txring
== ring
) {
2927 pcb
->utun_kpipe_txring
= NULL
;
2932 utun_kpipe_sync_tx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2933 kern_channel_ring_t tx_ring
, uint32_t flags
)
2935 #pragma unused(nxprov)
2936 #pragma unused(flags)
2937 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2939 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2940 int channel_enabled
= pcb
->utun_kpipe_enabled
;
2941 if (!channel_enabled
) {
2942 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2946 if (pcb
->utun_use_netif
) {
2947 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
2948 if (tx_slot
== NULL
) {
2949 // Nothing to write, bail
2950 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2954 // Signal the netif ring to read
2955 kern_channel_ring_t rx_ring
= pcb
->utun_netif_rxring
;
2956 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2957 if (rx_ring
!= NULL
) {
2958 kern_channel_notify(rx_ring
, 0);
2961 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2963 struct ifnet_stat_increment_param incs
= {};
2964 struct kern_channel_ring_stat_increment tx_ring_stats
= {};
2965 MBUFQ_HEAD(mbufq
) mbq
;
2967 kern_channel_slot_t tx_pslot
= NULL
;
2968 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
2969 while (tx_slot
!= NULL
) {
2970 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
2974 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
2980 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
2981 VERIFY(tx_buf
!= NULL
);
2982 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
2983 VERIFY(tx_baddr
!= 0);
2984 tx_baddr
+= kern_buflet_get_data_offset(tx_buf
);
2986 size_t length
= MIN(kern_packet_get_data_length(tx_ph
),
2987 pcb
->utun_slot_size
);
2990 if (length
>= UTUN_HEADER_SIZE(pcb
) &&
2991 !(pcb
->utun_flags
& UTUN_FLAGS_NO_INPUT
)) {
2992 errno_t error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_HEADER
, &data
);
2994 error
= mbuf_copyback(data
, 0, length
, tx_baddr
, MBUF_WAITOK
);
2997 * The userland ABI requires the first four bytes have
2998 * the protocol family in network byte order: swap them
3000 *(uint32_t *)mbuf_data(data
) = ntohl(*(uint32_t *)mbuf_data(data
));
3001 mbuf_pkthdr_setrcvif(data
, pcb
->utun_ifp
);
3002 bpf_tap_in(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
3004 incs
.bytes_in
+= length
;
3005 MBUFQ_ENQUEUE(&mbq
, data
);
3009 kern_channel_advance_slot(tx_ring
, tx_pslot
);
3010 tx_ring_stats
.kcrsi_slots_transferred
= incs
.packets_in
;
3011 tx_ring_stats
.kcrsi_bytes_transferred
= incs
.bytes_in
;
3012 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
3013 (void) kern_channel_reclaim(tx_ring
);
3015 if (!MBUFQ_EMPTY(&mbq
)) {
3016 (void) ifnet_input_extended(pcb
->utun_ifp
, MBUFQ_FIRST(&mbq
),
3017 MBUFQ_LAST(&mbq
), &incs
);
3026 utun_kpipe_sync_rx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
3027 kern_channel_ring_t rx_ring
, uint32_t flags
)
3029 #pragma unused(nxprov)
3030 #pragma unused(flags)
3031 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
3032 struct kern_channel_ring_stat_increment rx_ring_stats
= {};
3034 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
3036 int channel_enabled
= pcb
->utun_kpipe_enabled
;
3037 if (!channel_enabled
) {
3038 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3042 /* reclaim user-released slots */
3043 (void) kern_channel_reclaim(rx_ring
);
3045 uint32_t avail
= kern_channel_available_slot_count(rx_ring
);
3047 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3051 if (pcb
->utun_use_netif
) {
3052 kern_channel_ring_t tx_ring
= pcb
->utun_netif_txring
;
3053 if (tx_ring
== NULL
||
3054 pcb
->utun_netif_nexus
== NULL
) {
3055 // Net-If TX ring not set up yet, nothing to read
3056 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3060 struct netif_stats
*nifs
= &NX_NETIF_PRIVATE(pcb
->utun_netif_nexus
)->nif_stats
;
3062 // Unlock utun before entering ring
3063 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3065 (void)kr_enter(tx_ring
, TRUE
);
3067 // Lock again after entering and validate
3068 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
3069 if (tx_ring
!= pcb
->utun_netif_txring
) {
3070 // Ring no longer valid
3071 // Unlock first, then exit ring
3072 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3077 struct kern_channel_ring_stat_increment tx_ring_stats
;
3078 bzero(&tx_ring_stats
, sizeof(tx_ring_stats
));
3079 kern_channel_slot_t tx_pslot
= NULL
;
3080 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
3081 if (tx_slot
== NULL
) {
3082 // Nothing to read, don't bother signalling
3083 // Unlock first, then exit ring
3084 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3089 struct kern_pbufpool
*rx_pp
= rx_ring
->ckr_pp
;
3090 VERIFY(rx_pp
!= NULL
);
3091 kern_channel_slot_t rx_pslot
= NULL
;
3092 kern_channel_slot_t rx_slot
= kern_channel_get_next_slot(rx_ring
, NULL
, NULL
);
3094 while (rx_slot
!= NULL
&& tx_slot
!= NULL
) {
3096 kern_buflet_t rx_buf
;
3099 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
3103 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
3105 /* Skip slot if packet is zero-length or marked as dropped (QUMF_DROPPED) */
3110 // Allocate rx packet
3111 kern_packet_t rx_ph
= 0;
3112 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
3113 if (__improbable(error
!= 0)) {
3114 printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
3115 pcb
->utun_ifp
->if_xname
);
3119 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
3120 VERIFY(tx_buf
!= NULL
);
3121 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
3122 VERIFY(tx_baddr
!= NULL
);
3123 tx_baddr
+= kern_buflet_get_data_offset(tx_buf
);
3125 bpf_tap_packet_out(pcb
->utun_ifp
, DLT_RAW
, tx_ph
, NULL
, 0);
3127 length
= MIN(kern_packet_get_data_length(tx_ph
) + UTUN_HEADER_SIZE(pcb
),
3128 pcb
->utun_slot_size
);
3130 tx_ring_stats
.kcrsi_slots_transferred
++;
3131 tx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3133 if (length
< UTUN_HEADER_SIZE(pcb
) ||
3134 length
> pcb
->utun_slot_size
||
3135 length
> rx_pp
->pp_buflet_size
||
3136 (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
)) {
3138 kern_pbufpool_free(rx_pp
, rx_ph
);
3139 printf("utun_kpipe_sync_rx %s: invalid length %zu header_size %zu\n",
3140 pcb
->utun_ifp
->if_xname
, length
, UTUN_HEADER_SIZE(pcb
));
3141 STATS_INC(nifs
, NETIF_STATS_BADLEN
);
3142 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
3146 /* fillout packet */
3147 rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
3148 VERIFY(rx_buf
!= NULL
);
3149 rx_baddr
= kern_buflet_get_object_address(rx_buf
);
3150 VERIFY(rx_baddr
!= NULL
);
3154 uint8_t vhl
= *(uint8_t *)(tx_baddr
);
3155 u_int ip_version
= (vhl
>> 4);
3156 switch (ip_version
) {
3166 printf("utun_kpipe_sync_rx %s: unknown ip version %u vhl %u header_size %zu\n",
3167 pcb
->utun_ifp
->if_xname
, ip_version
, vhl
, UTUN_HEADER_SIZE(pcb
));
3174 memcpy((void *)rx_baddr
, &af
, sizeof(af
));
3175 if (pcb
->utun_flags
& UTUN_FLAGS_ENABLE_PROC_UUID
) {
3176 kern_packet_get_euuid(tx_ph
, (void *)(rx_baddr
+ sizeof(af
)));
3179 // Copy data from tx to rx
3180 memcpy((void *)(rx_baddr
+ UTUN_HEADER_SIZE(pcb
)), (void *)tx_baddr
, length
- UTUN_HEADER_SIZE(pcb
));
3181 kern_packet_clear_flow_uuid(rx_ph
); // zero flow id
3183 /* finalize and attach the packet */
3184 error
= kern_buflet_set_data_offset(rx_buf
, 0);
3186 error
= kern_buflet_set_data_length(rx_buf
, length
);
3188 error
= kern_packet_finalize(rx_ph
);
3190 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
3193 STATS_INC(nifs
, NETIF_STATS_TXPKTS
);
3194 STATS_INC(nifs
, NETIF_STATS_TXCOPY_DIRECT
);
3196 rx_ring_stats
.kcrsi_slots_transferred
++;
3197 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3200 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
3204 kern_channel_advance_slot(rx_ring
, rx_pslot
);
3205 kern_channel_increment_ring_net_stats(rx_ring
, pcb
->utun_ifp
, &rx_ring_stats
);
3209 kern_channel_advance_slot(tx_ring
, tx_pslot
);
3210 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
3211 (void)kern_channel_reclaim(tx_ring
);
3214 /* just like utun_ctl_rcvd(), always reenable output */
3215 errno_t error
= ifnet_enable_output(pcb
->utun_ifp
);
3217 printf("utun_kpipe_sync_rx: ifnet_enable_output returned error %d\n", error
);
3220 // Unlock first, then exit ring
3221 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3223 if (tx_pslot
!= NULL
) {
3224 kern_channel_notify(tx_ring
, 0);
3228 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3230 uint32_t mb_cnt
= 0;
3231 uint32_t mb_len
= 0;
3232 struct mbuf
*mb_head
= NULL
;
3233 struct mbuf
*mb_tail
= NULL
;
3235 if (ifnet_dequeue_multi(pcb
->utun_ifp
, avail
, &mb_head
,
3236 &mb_tail
, &mb_cnt
, &mb_len
) != 0) {
3239 VERIFY(mb_cnt
<= avail
);
3241 struct kern_pbufpool
*rx_pp
= rx_ring
->ckr_pp
;
3242 VERIFY(rx_pp
!= NULL
);
3243 kern_channel_slot_t rx_pslot
= NULL
;
3244 kern_channel_slot_t rx_slot
= kern_channel_get_next_slot(rx_ring
, NULL
, NULL
);
3248 if ((data
= mb_head
) == NULL
) {
3249 VERIFY(mb_cnt
== 0);
3252 mb_head
= mbuf_nextpkt(mb_head
);
3253 mbuf_setnextpkt(data
, NULL
);
3254 VERIFY(mb_cnt
!= 0);
3256 length
= mbuf_pkthdr_len(data
);
3257 if (length
< UTUN_HEADER_SIZE(pcb
) ||
3258 length
> pcb
->utun_slot_size
||
3259 (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
)) {
3264 bpf_tap_out(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
3266 // Allocate rx packet
3267 kern_packet_t rx_ph
= 0;
3268 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
3269 if (__improbable(error
!= 0)) {
3270 printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
3271 pcb
->utun_ifp
->if_xname
);
3276 * The ABI requires the protocol in network byte order
3278 *(u_int32_t
*)mbuf_data(data
) = htonl(*(u_int32_t
*)mbuf_data(data
));
3280 // Fillout rx packet
3281 kern_buflet_t rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
3282 VERIFY(rx_buf
!= NULL
);
3283 void *rx_baddr
= kern_buflet_get_object_address(rx_buf
);
3284 VERIFY(rx_baddr
!= NULL
);
3286 // Copy-in data from mbuf to buflet
3287 mbuf_copydata(data
, 0, length
, (void *)rx_baddr
);
3288 kern_packet_clear_flow_uuid(rx_ph
); // Zero flow id
3290 // Finalize and attach the packet
3291 error
= kern_buflet_set_data_offset(rx_buf
, 0);
3293 error
= kern_buflet_set_data_length(rx_buf
, length
);
3295 error
= kern_packet_finalize(rx_ph
);
3297 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
3300 rx_ring_stats
.kcrsi_slots_transferred
++;
3301 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3303 if (!pcb
->utun_ext_ifdata_stats
) {
3304 ifnet_stat_increment_out(pcb
->utun_ifp
, 1, length
, 0);
3310 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
3313 kern_channel_advance_slot(rx_ring
, rx_pslot
);
3314 kern_channel_increment_ring_stats(rx_ring
, &rx_ring_stats
);
3316 if (mb_head
!= NULL
) {
3317 VERIFY(mb_cnt
!= 0);
3318 mbuf_freem_list(mb_head
);
3325 #endif // UTUN_NEXUS
3329 * These are place holders until coreTLS kext stops calling them
3331 errno_t
utun_ctl_register_dtls(void *reg
);
3332 int utun_pkt_dtls_input(struct utun_pcb
*pcb
, mbuf_t
*pkt
, protocol_family_t family
);
3333 void utun_ctl_disable_crypto_dtls(struct utun_pcb
*pcb
);
3336 utun_ctl_register_dtls(void *reg
)
3343 utun_pkt_dtls_input(struct utun_pcb
*pcb
, mbuf_t
*pkt
, protocol_family_t family
)
3347 #pragma unused(family)
3352 utun_ctl_disable_crypto_dtls(struct utun_pcb
*pcb
)