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
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
;
984 memset(nx
, 0, sizeof(*nx
));
988 utun_create_fs_provider_and_instance(struct utun_pcb
*pcb
,
989 uint32_t subtype
, const char *type_name
,
991 uuid_t
*provider
, uuid_t
*instance
)
993 nexus_attr_t attr
= NULL
;
994 nexus_controller_t controller
= kern_nexus_shared_controller();
997 struct kern_nexus_init init
;
998 nexus_name_t provider_name
;
1000 err
= kern_nexus_get_builtin_domain_provider(NEXUS_TYPE_FLOW_SWITCH
,
1003 printf("%s can't get %s provider, error %d\n",
1004 __func__
, type_name
, err
);
1008 err
= kern_nexus_attr_create(&attr
);
1010 printf("%s: kern_nexus_attr_create failed: %d\n",
1015 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_EXTENSIONS
, subtype
);
1018 uint64_t slot_buffer_size
= pcb
->utun_slot_size
;
1019 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_SLOT_BUF_SIZE
, slot_buffer_size
);
1022 // Reset ring size for flowswitch nexus to limit memory usage. Larger RX than netif.
1023 uint64_t tx_ring_size
= pcb
->utun_tx_fsw_ring_size
;
1024 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_TX_SLOTS
, tx_ring_size
);
1026 uint64_t rx_ring_size
= pcb
->utun_rx_fsw_ring_size
;
1027 err
= kern_nexus_attr_set(attr
, NEXUS_ATTR_RX_SLOTS
, rx_ring_size
);
1030 snprintf((char *)provider_name
, sizeof(provider_name
),
1031 "com.apple.%s.%s", type_name
, ifname
);
1032 err
= kern_nexus_controller_register_provider(controller
,
1039 kern_nexus_attr_destroy(attr
);
1042 printf("%s register %s provider failed, error %d\n",
1043 __func__
, type_name
, err
);
1046 bzero(&init
, sizeof (init
));
1047 init
.nxi_version
= KERN_NEXUS_CURRENT_VERSION
;
1048 err
= kern_nexus_controller_alloc_provider_instance(controller
,
1053 printf("%s alloc_provider_instance %s failed, %d\n",
1054 __func__
, type_name
, err
);
1055 kern_nexus_controller_deregister_provider(controller
,
1057 uuid_clear(*provider
);
1064 utun_multistack_attach(struct utun_pcb
*pcb
)
1066 nexus_controller_t controller
= kern_nexus_shared_controller();
1068 utun_nx_t nx
= &pcb
->utun_nx
;
1070 // Allocate multistack flowswitch
1071 err
= utun_create_fs_provider_and_instance(pcb
,
1072 NEXUS_EXTENSION_FSW_TYPE_MULTISTACK
,
1074 pcb
->utun_ifp
->if_xname
,
1078 printf("%s: failed to create bridge provider and instance\n",
1083 // Attach multistack to device port
1084 err
= kern_nexus_ifattach(controller
, nx
->ms_instance
,
1085 NULL
, nx
->if_instance
,
1086 FALSE
, &nx
->ms_device
);
1088 printf("%s kern_nexus_ifattach ms device %d\n", __func__
, err
);
1092 // Attach multistack to host port
1093 err
= kern_nexus_ifattach(controller
, nx
->ms_instance
,
1094 NULL
, nx
->if_instance
,
1095 TRUE
, &nx
->ms_host
);
1097 printf("%s kern_nexus_ifattach ms host %d\n", __func__
, err
);
1101 // Extract the agent UUID and save for later
1102 struct kern_nexus
*multistack_nx
= nx_find(nx
->ms_instance
, false);
1103 if (multistack_nx
!= NULL
) {
1104 struct nx_flowswitch
*flowswitch
= NX_FSW_PRIVATE(multistack_nx
);
1105 if (flowswitch
!= NULL
) {
1106 FSW_RLOCK(flowswitch
);
1107 struct fsw_ms_context
*ms_context
= (struct fsw_ms_context
*)flowswitch
->fsw_ops_private
;
1108 if (ms_context
!= NULL
) {
1109 uuid_copy(nx
->ms_agent
, ms_context
->mc_agent_uuid
);
1111 printf("utun_multistack_attach - fsw_ms_context is NULL\n");
1113 FSW_UNLOCK(flowswitch
);
1115 printf("utun_multistack_attach - flowswitch is NULL\n");
1117 nx_release(multistack_nx
);
1119 printf("utun_multistack_attach - unable to find multistack nexus\n");
1125 utun_nexus_detach(pcb
);
1127 errno_t detach_error
= 0;
1128 if ((detach_error
= ifnet_detach(pcb
->utun_ifp
)) != 0) {
1129 panic("utun_multistack_attach - ifnet_detach failed: %d\n", detach_error
);
1137 utun_register_kernel_pipe_nexus(void)
1139 nexus_attr_t nxa
= NULL
;
1142 lck_mtx_lock(&utun_lock
);
1143 if (utun_ncd_refcount
++) {
1144 lck_mtx_unlock(&utun_lock
);
1148 result
= kern_nexus_controller_create(&utun_ncd
);
1150 printf("%s: kern_nexus_controller_create failed: %d\n",
1151 __FUNCTION__
, result
);
1156 result
= kern_nexus_get_builtin_domain_provider(
1157 NEXUS_TYPE_KERNEL_PIPE
, &dom_prov
);
1159 printf("%s: kern_nexus_get_builtin_domain_provider failed: %d\n",
1160 __FUNCTION__
, result
);
1164 struct kern_nexus_provider_init prov_init
= {
1165 .nxpi_version
= KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION
,
1166 .nxpi_flags
= NXPIF_VIRTUAL_DEVICE
,
1167 .nxpi_pre_connect
= utun_nexus_pre_connect
,
1168 .nxpi_connected
= utun_nexus_connected
,
1169 .nxpi_pre_disconnect
= utun_nexus_pre_disconnect
,
1170 .nxpi_disconnected
= utun_nexus_disconnected
,
1171 .nxpi_ring_init
= utun_kpipe_ring_init
,
1172 .nxpi_ring_fini
= utun_kpipe_ring_fini
,
1173 .nxpi_slot_init
= NULL
,
1174 .nxpi_slot_fini
= NULL
,
1175 .nxpi_sync_tx
= utun_kpipe_sync_tx
,
1176 .nxpi_sync_rx
= utun_kpipe_sync_rx
,
1177 .nxpi_tx_doorbell
= NULL
,
1180 result
= kern_nexus_attr_create(&nxa
);
1182 printf("%s: kern_nexus_attr_create failed: %d\n",
1183 __FUNCTION__
, result
);
1187 uint64_t slot_buffer_size
= UTUN_IF_DEFAULT_SLOT_SIZE
;
1188 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_SLOT_BUF_SIZE
, slot_buffer_size
);
1189 VERIFY(result
== 0);
1191 // Reset ring size for kernel pipe nexus to limit memory usage
1192 uint64_t ring_size
= if_utun_ring_size
;
1193 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_TX_SLOTS
, ring_size
);
1194 VERIFY(result
== 0);
1195 result
= kern_nexus_attr_set(nxa
, NEXUS_ATTR_RX_SLOTS
, ring_size
);
1196 VERIFY(result
== 0);
1198 result
= kern_nexus_controller_register_provider(utun_ncd
,
1200 (const uint8_t *)"com.apple.nexus.utun.kpipe",
1206 printf("%s: kern_nexus_controller_register_provider failed: %d\n",
1207 __FUNCTION__
, result
);
1213 kern_nexus_attr_destroy(nxa
);
1218 kern_nexus_controller_destroy(utun_ncd
);
1221 utun_ncd_refcount
= 0;
1224 lck_mtx_unlock(&utun_lock
);
1230 utun_unregister_kernel_pipe_nexus(void)
1232 lck_mtx_lock(&utun_lock
);
1234 VERIFY(utun_ncd_refcount
> 0);
1236 if (--utun_ncd_refcount
== 0) {
1237 kern_nexus_controller_destroy(utun_ncd
);
1241 lck_mtx_unlock(&utun_lock
);
1244 // For use by socket option, not internally
1246 utun_disable_channel(struct utun_pcb
*pcb
)
1252 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1254 enabled
= pcb
->utun_kpipe_enabled
;
1255 uuid_copy(uuid
, pcb
->utun_kpipe_uuid
);
1257 VERIFY(uuid_is_null(pcb
->utun_kpipe_uuid
) == !enabled
);
1259 pcb
->utun_kpipe_enabled
= 0;
1260 uuid_clear(pcb
->utun_kpipe_uuid
);
1262 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1265 result
= kern_nexus_controller_free_provider_instance(utun_ncd
, uuid
);
1271 if (pcb
->utun_kpipe_pp
!= NULL
) {
1272 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1273 pcb
->utun_kpipe_pp
= NULL
;
1275 utun_unregister_kernel_pipe_nexus();
1282 utun_enable_channel(struct utun_pcb
*pcb
, struct proc
*proc
)
1284 struct kern_nexus_init init
;
1285 struct kern_pbufpool_init pp_init
;
1288 result
= utun_register_kernel_pipe_nexus();
1295 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1297 if (pcb
->utun_kpipe_enabled
) {
1298 result
= EEXIST
; // return success instead?
1303 * Make sure we can fit packets in the channel buffers and
1304 * Allow an extra 4 bytes for the protocol number header in the channel
1306 if (pcb
->utun_ifp
->if_mtu
+ UTUN_HEADER_SIZE(pcb
) > pcb
->utun_slot_size
) {
1307 result
= EOPNOTSUPP
;
1311 bzero(&pp_init
, sizeof (pp_init
));
1312 pp_init
.kbi_version
= KERN_PBUFPOOL_CURRENT_VERSION
;
1313 pp_init
.kbi_packets
= pcb
->utun_netif_ring_size
* 2;
1314 pp_init
.kbi_bufsize
= pcb
->utun_slot_size
;
1315 pp_init
.kbi_buf_seg_size
= UTUN_IF_DEFAULT_BUF_SEG_SIZE
;
1316 pp_init
.kbi_max_frags
= 1;
1317 pp_init
.kbi_flags
|= KBIF_QUANTUM
;
1318 (void) snprintf((char *)pp_init
.kbi_name
, sizeof (pp_init
.kbi_name
),
1319 "com.apple.kpipe.%s", pcb
->utun_if_xname
);
1321 result
= kern_pbufpool_create(&pp_init
, &pp_init
, &pcb
->utun_kpipe_pp
,
1324 printf("%s pbufbool create failed, error %d\n", __func__
, result
);
1328 VERIFY(uuid_is_null(pcb
->utun_kpipe_uuid
));
1329 bzero(&init
, sizeof (init
));
1330 init
.nxi_version
= KERN_NEXUS_CURRENT_VERSION
;
1331 init
.nxi_tx_pbufpool
= pcb
->utun_kpipe_pp
;
1332 result
= kern_nexus_controller_alloc_provider_instance(utun_ncd
,
1333 utun_kpipe_uuid
, pcb
, &pcb
->utun_kpipe_uuid
, &init
);
1338 nexus_port_t port
= NEXUS_PORT_KERNEL_PIPE_CLIENT
;
1339 result
= kern_nexus_controller_bind_provider_instance(utun_ncd
,
1340 pcb
->utun_kpipe_uuid
, &port
,
1341 proc_pid(proc
), NULL
, NULL
, 0, NEXUS_BIND_PID
);
1343 kern_nexus_controller_free_provider_instance(utun_ncd
,
1344 pcb
->utun_kpipe_uuid
);
1345 uuid_clear(pcb
->utun_kpipe_uuid
);
1349 pcb
->utun_kpipe_enabled
= 1;
1352 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1355 if (pcb
->utun_kpipe_pp
!= NULL
) {
1356 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1357 pcb
->utun_kpipe_pp
= NULL
;
1359 utun_unregister_kernel_pipe_nexus();
1365 #endif // UTUN_NEXUS
1368 utun_register_control(void)
1370 struct kern_ctl_reg kern_ctl
;
1373 /* Find a unique value for our interface family */
1374 result
= mbuf_tag_id_find(UTUN_CONTROL_NAME
, &utun_family
);
1376 printf("utun_register_control - mbuf_tag_id_find_internal failed: %d\n", result
);
1380 utun_pcb_size
= sizeof(struct utun_pcb
);
1381 utun_pcb_zone
= zinit(utun_pcb_size
,
1382 UTUN_PCB_ZONE_MAX
* utun_pcb_size
,
1383 0, UTUN_PCB_ZONE_NAME
);
1384 if (utun_pcb_zone
== NULL
) {
1385 printf("utun_register_control - zinit(utun_pcb) failed");
1390 utun_register_nexus();
1391 #endif // UTUN_NEXUS
1393 TAILQ_INIT(&utun_head
);
1395 bzero(&kern_ctl
, sizeof(kern_ctl
));
1396 strlcpy(kern_ctl
.ctl_name
, UTUN_CONTROL_NAME
, sizeof(kern_ctl
.ctl_name
));
1397 kern_ctl
.ctl_name
[sizeof(kern_ctl
.ctl_name
) - 1] = 0;
1398 kern_ctl
.ctl_flags
= CTL_FLAG_PRIVILEGED
| CTL_FLAG_REG_EXTENDED
; /* Require root */
1399 kern_ctl
.ctl_sendsize
= 512 * 1024;
1400 kern_ctl
.ctl_recvsize
= 512 * 1024;
1401 kern_ctl
.ctl_bind
= utun_ctl_bind
;
1402 kern_ctl
.ctl_connect
= utun_ctl_connect
;
1403 kern_ctl
.ctl_disconnect
= utun_ctl_disconnect
;
1404 kern_ctl
.ctl_send
= utun_ctl_send
;
1405 kern_ctl
.ctl_setopt
= utun_ctl_setopt
;
1406 kern_ctl
.ctl_getopt
= utun_ctl_getopt
;
1407 kern_ctl
.ctl_rcvd
= utun_ctl_rcvd
;
1409 result
= ctl_register(&kern_ctl
, &utun_kctlref
);
1411 printf("utun_register_control - ctl_register failed: %d\n", result
);
1415 /* Register the protocol plumbers */
1416 if ((result
= proto_register_plumber(PF_INET
, utun_family
,
1417 utun_attach_proto
, NULL
)) != 0) {
1418 printf("utun_register_control - proto_register_plumber(PF_INET, %d) failed: %d\n",
1419 utun_family
, result
);
1420 ctl_deregister(utun_kctlref
);
1424 /* Register the protocol plumbers */
1425 if ((result
= proto_register_plumber(PF_INET6
, utun_family
,
1426 utun_attach_proto
, NULL
)) != 0) {
1427 proto_unregister_plumber(PF_INET
, utun_family
);
1428 ctl_deregister(utun_kctlref
);
1429 printf("utun_register_control - proto_register_plumber(PF_INET6, %d) failed: %d\n",
1430 utun_family
, result
);
1434 utun_lck_attr
= lck_attr_alloc_init();
1435 utun_lck_grp_attr
= lck_grp_attr_alloc_init();
1436 utun_lck_grp
= lck_grp_alloc_init("utun", utun_lck_grp_attr
);
1438 lck_mtx_init(&utun_lock
, utun_lck_grp
, utun_lck_attr
);
1443 /* Kernel control functions */
1446 utun_free_pcb(struct utun_pcb
*pcb
, bool in_list
)
1449 mbuf_freem_list(pcb
->utun_input_chain
);
1450 lck_mtx_destroy(&pcb
->utun_input_chain_lock
, utun_lck_grp
);
1451 #endif // UTUN_NEXUS
1452 lck_rw_destroy(&pcb
->utun_pcb_lock
, utun_lck_grp
);
1454 lck_mtx_lock(&utun_lock
);
1455 TAILQ_REMOVE(&utun_head
, pcb
, utun_chain
);
1456 lck_mtx_unlock(&utun_lock
);
1458 zfree(utun_pcb_zone
, pcb
);
1462 utun_ctl_bind(kern_ctl_ref kctlref
,
1463 struct sockaddr_ctl
*sac
,
1466 struct utun_pcb
*pcb
= zalloc(utun_pcb_zone
);
1467 memset(pcb
, 0, sizeof(*pcb
));
1470 pcb
->utun_ctlref
= kctlref
;
1471 pcb
->utun_unit
= sac
->sc_unit
;
1472 pcb
->utun_max_pending_packets
= 1;
1475 pcb
->utun_use_netif
= false;
1476 pcb
->utun_slot_size
= UTUN_IF_DEFAULT_SLOT_SIZE
;
1477 pcb
->utun_netif_ring_size
= UTUN_IF_DEFAULT_RING_SIZE
;
1478 pcb
->utun_tx_fsw_ring_size
= UTUN_IF_DEFAULT_TX_FSW_RING_SIZE
;
1479 pcb
->utun_rx_fsw_ring_size
= UTUN_IF_DEFAULT_RX_FSW_RING_SIZE
;
1480 #endif // UTUN_NEXUS
1482 lck_mtx_init(&pcb
->utun_input_chain_lock
, utun_lck_grp
, utun_lck_attr
);
1483 lck_rw_init(&pcb
->utun_pcb_lock
, utun_lck_grp
, utun_lck_attr
);
1489 utun_ctl_connect(kern_ctl_ref kctlref
,
1490 struct sockaddr_ctl
*sac
,
1493 struct ifnet_init_eparams utun_init
= {};
1496 if (*unitinfo
== NULL
) {
1497 (void)utun_ctl_bind(kctlref
, sac
, unitinfo
);
1500 struct utun_pcb
*pcb
= *unitinfo
;
1502 lck_mtx_lock(&utun_lock
);
1504 /* Find some open interface id */
1505 u_int32_t chosen_unique_id
= 1;
1506 struct utun_pcb
*next_pcb
= TAILQ_LAST(&utun_head
, utun_list
);
1507 if (next_pcb
!= NULL
) {
1508 /* List was not empty, add one to the last item */
1509 chosen_unique_id
= next_pcb
->utun_unique_id
+ 1;
1513 * If this wrapped the id number, start looking at
1514 * the front of the list for an unused id.
1516 if (chosen_unique_id
== 0) {
1517 /* Find the next unused ID */
1518 chosen_unique_id
= 1;
1519 TAILQ_FOREACH(next_pcb
, &utun_head
, utun_chain
) {
1520 if (next_pcb
->utun_unique_id
> chosen_unique_id
) {
1521 /* We found a gap */
1525 chosen_unique_id
= next_pcb
->utun_unique_id
+ 1;
1530 pcb
->utun_unique_id
= chosen_unique_id
;
1532 if (next_pcb
!= NULL
) {
1533 TAILQ_INSERT_BEFORE(next_pcb
, pcb
, utun_chain
);
1535 TAILQ_INSERT_TAIL(&utun_head
, pcb
, utun_chain
);
1537 lck_mtx_unlock(&utun_lock
);
1539 snprintf(pcb
->utun_if_xname
, sizeof(pcb
->utun_if_xname
), "utun%d", pcb
->utun_unit
- 1);
1540 snprintf(pcb
->utun_unique_name
, sizeof(pcb
->utun_unique_name
), "utunid%d", pcb
->utun_unique_id
- 1);
1541 printf("utun_ctl_connect: creating interface %s (id %s)\n", pcb
->utun_if_xname
, pcb
->utun_unique_name
);
1543 /* Create the interface */
1544 bzero(&utun_init
, sizeof(utun_init
));
1545 utun_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
1546 utun_init
.len
= sizeof (utun_init
);
1549 if (pcb
->utun_use_netif
) {
1550 utun_init
.flags
= (IFNET_INIT_SKYWALK_NATIVE
| IFNET_INIT_NX_NOAUTO
);
1551 utun_init
.tx_headroom
= UTUN_IF_HEADROOM_SIZE
;
1553 #endif // UTUN_NEXUS
1555 utun_init
.flags
= IFNET_INIT_NX_NOAUTO
;
1556 utun_init
.start
= utun_start
;
1557 utun_init
.framer_extended
= utun_framer
;
1559 utun_init
.name
= "utun";
1560 utun_init
.unit
= pcb
->utun_unit
- 1;
1561 utun_init
.uniqueid
= pcb
->utun_unique_name
;
1562 utun_init
.uniqueid_len
= strlen(pcb
->utun_unique_name
);
1563 utun_init
.family
= utun_family
;
1564 utun_init
.subfamily
= IFNET_SUBFAMILY_UTUN
;
1565 utun_init
.type
= IFT_OTHER
;
1566 utun_init
.demux
= utun_demux
;
1567 utun_init
.add_proto
= utun_add_proto
;
1568 utun_init
.del_proto
= utun_del_proto
;
1569 utun_init
.softc
= pcb
;
1570 utun_init
.ioctl
= utun_ioctl
;
1571 utun_init
.detach
= utun_detached
;
1574 if (pcb
->utun_use_netif
) {
1575 result
= utun_nexus_ifattach(pcb
, &utun_init
, &pcb
->utun_ifp
);
1577 printf("utun_ctl_connect - utun_nexus_ifattach failed: %d\n", result
);
1578 utun_free_pcb(pcb
, true);
1583 result
= utun_multistack_attach(pcb
);
1585 printf("utun_ctl_connect - utun_multistack_attach failed: %d\n", result
);
1591 bpfattach(pcb
->utun_ifp
, DLT_RAW
, 0);
1593 #endif // UTUN_NEXUS
1596 * Upon success, this holds an ifnet reference which we will
1597 * release via ifnet_release() at final detach time.
1599 result
= ifnet_allocate_extended(&utun_init
, &pcb
->utun_ifp
);
1601 printf("utun_ctl_connect - ifnet_allocate failed: %d\n", result
);
1602 utun_free_pcb(pcb
, true);
1607 /* Set flags and additional information. */
1608 ifnet_set_mtu(pcb
->utun_ifp
, UTUN_DEFAULT_MTU
);
1609 ifnet_set_flags(pcb
->utun_ifp
, IFF_UP
| IFF_MULTICAST
| IFF_POINTOPOINT
, 0xffff);
1611 /* The interface must generate its own IPv6 LinkLocal address,
1612 * if possible following the recommendation of RFC2472 to the 64bit interface ID
1614 ifnet_set_eflags(pcb
->utun_ifp
, IFEF_NOAUTOIPV6LL
, IFEF_NOAUTOIPV6LL
);
1616 /* Reset the stats in case as the interface may have been recycled */
1617 struct ifnet_stats_param stats
;
1618 bzero(&stats
, sizeof(struct ifnet_stats_param
));
1619 ifnet_set_stat(pcb
->utun_ifp
, &stats
);
1621 /* Attach the interface */
1622 result
= ifnet_attach(pcb
->utun_ifp
, NULL
);
1624 printf("utun_ctl_connect - ifnet_attach failed: %d\n", result
);
1625 /* Release reference now since attach failed */
1626 ifnet_release(pcb
->utun_ifp
);
1627 utun_free_pcb(pcb
, true);
1633 bpfattach(pcb
->utun_ifp
, DLT_NULL
, UTUN_HEADER_SIZE(pcb
));
1636 /* The interfaces resoures allocated, mark it as running */
1637 ifnet_set_flags(pcb
->utun_ifp
, IFF_RUNNING
, IFF_RUNNING
);
1643 utun_detach_ip(ifnet_t interface
,
1644 protocol_family_t protocol
,
1647 errno_t result
= EPROTONOSUPPORT
;
1649 /* Attempt a detach */
1650 if (protocol
== PF_INET
) {
1653 bzero(&ifr
, sizeof(ifr
));
1654 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
1655 ifnet_name(interface
), ifnet_unit(interface
));
1657 result
= sock_ioctl(pf_socket
, SIOCPROTODETACH
, &ifr
);
1658 } else if (protocol
== PF_INET6
) {
1659 struct in6_ifreq ifr6
;
1661 bzero(&ifr6
, sizeof(ifr6
));
1662 snprintf(ifr6
.ifr_name
, sizeof(ifr6
.ifr_name
), "%s%d",
1663 ifnet_name(interface
), ifnet_unit(interface
));
1665 result
= sock_ioctl(pf_socket
, SIOCPROTODETACH_IN6
, &ifr6
);
1672 utun_remove_address(ifnet_t interface
,
1673 protocol_family_t protocol
,
1679 /* Attempt a detach */
1680 if (protocol
== PF_INET
) {
1683 bzero(&ifr
, sizeof(ifr
));
1684 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
1685 ifnet_name(interface
), ifnet_unit(interface
));
1686 result
= ifaddr_address(address
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1688 printf("utun_remove_address - ifaddr_address failed: %d", result
);
1690 result
= sock_ioctl(pf_socket
, SIOCDIFADDR
, &ifr
);
1692 printf("utun_remove_address - SIOCDIFADDR failed: %d", result
);
1695 } else if (protocol
== PF_INET6
) {
1696 struct in6_ifreq ifr6
;
1698 bzero(&ifr6
, sizeof(ifr6
));
1699 snprintf(ifr6
.ifr_name
, sizeof(ifr6
.ifr_name
), "%s%d",
1700 ifnet_name(interface
), ifnet_unit(interface
));
1701 result
= ifaddr_address(address
, (struct sockaddr
*)&ifr6
.ifr_addr
,
1702 sizeof(ifr6
.ifr_addr
));
1704 printf("utun_remove_address - ifaddr_address failed (v6): %d",
1707 result
= sock_ioctl(pf_socket
, SIOCDIFADDR_IN6
, &ifr6
);
1709 printf("utun_remove_address - SIOCDIFADDR_IN6 failed: %d",
1717 utun_cleanup_family(ifnet_t interface
,
1718 protocol_family_t protocol
)
1721 socket_t pf_socket
= NULL
;
1722 ifaddr_t
*addresses
= NULL
;
1725 if (protocol
!= PF_INET
&& protocol
!= PF_INET6
) {
1726 printf("utun_cleanup_family - invalid protocol family %d\n", protocol
);
1730 /* Create a socket for removing addresses and detaching the protocol */
1731 result
= sock_socket(protocol
, SOCK_DGRAM
, 0, NULL
, NULL
, &pf_socket
);
1733 if (result
!= EAFNOSUPPORT
)
1734 printf("utun_cleanup_family - failed to create %s socket: %d\n",
1735 protocol
== PF_INET
? "IP" : "IPv6", result
);
1739 /* always set SS_PRIV, we want to close and detach regardless */
1740 sock_setpriv(pf_socket
, 1);
1742 result
= utun_detach_ip(interface
, protocol
, pf_socket
);
1743 if (result
== 0 || result
== ENXIO
) {
1744 /* We are done! We either detached or weren't attached. */
1746 } else if (result
!= EBUSY
) {
1747 /* Uh, not really sure what happened here... */
1748 printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result
);
1753 * At this point, we received an EBUSY error. This means there are
1754 * addresses attached. We should detach them and then try again.
1756 result
= ifnet_get_address_list_family(interface
, &addresses
, protocol
);
1758 printf("fnet_get_address_list_family(%s%d, 0xblah, %s) - failed: %d\n",
1759 ifnet_name(interface
), ifnet_unit(interface
),
1760 protocol
== PF_INET
? "PF_INET" : "PF_INET6", result
);
1764 for (i
= 0; addresses
[i
] != 0; i
++) {
1765 utun_remove_address(interface
, protocol
, addresses
[i
], pf_socket
);
1767 ifnet_free_address_list(addresses
);
1771 * The addresses should be gone, we should try the remove again.
1773 result
= utun_detach_ip(interface
, protocol
, pf_socket
);
1774 if (result
!= 0 && result
!= ENXIO
) {
1775 printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result
);
1779 if (pf_socket
!= NULL
) {
1780 sock_close(pf_socket
);
1783 if (addresses
!= NULL
) {
1784 ifnet_free_address_list(addresses
);
1789 utun_ctl_disconnect(__unused kern_ctl_ref kctlref
,
1790 __unused u_int32_t unit
,
1793 struct utun_pcb
*pcb
= unitinfo
;
1802 // Tell the nexus to stop all rings
1803 if (pcb
->utun_netif_nexus
!= NULL
) {
1804 kern_nexus_stop(pcb
->utun_netif_nexus
);
1806 #endif // UTUN_NEXUS
1808 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
1812 uuid_copy(kpipe_uuid
, pcb
->utun_kpipe_uuid
);
1813 uuid_clear(pcb
->utun_kpipe_uuid
);
1814 pcb
->utun_kpipe_enabled
= FALSE
;
1815 #endif // UTUN_NEXUS
1817 pcb
->utun_ctlref
= NULL
;
1819 ifp
= pcb
->utun_ifp
;
1822 // Tell the nexus to stop all rings
1823 if (pcb
->utun_netif_nexus
!= NULL
) {
1825 * Quiesce the interface and flush any pending outbound packets.
1829 /* Increment refcnt, but detach interface */
1830 ifnet_incr_iorefcnt(ifp
);
1831 if ((result
= ifnet_detach(ifp
)) != 0) {
1832 panic("utun_ctl_disconnect - ifnet_detach failed: %d\n", result
);
1836 * We want to do everything in our power to ensure that the interface
1837 * really goes away when the socket is closed. We must remove IP/IPv6
1838 * addresses and detach the protocols. Finally, we can remove and
1839 * release the interface.
1841 utun_cleanup_family(ifp
, AF_INET
);
1842 utun_cleanup_family(ifp
, AF_INET6
);
1844 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1846 if (!uuid_is_null(kpipe_uuid
)) {
1847 if (kern_nexus_controller_free_provider_instance(utun_ncd
, kpipe_uuid
) == 0) {
1848 if (pcb
->utun_kpipe_pp
!= NULL
) {
1849 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1850 pcb
->utun_kpipe_pp
= NULL
;
1852 utun_unregister_kernel_pipe_nexus();
1855 utun_nexus_detach(pcb
);
1857 /* Decrement refcnt to finish detaching and freeing */
1858 ifnet_decr_iorefcnt(ifp
);
1860 #endif // UTUN_NEXUS
1862 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1865 if (!uuid_is_null(kpipe_uuid
)) {
1866 if (kern_nexus_controller_free_provider_instance(utun_ncd
, kpipe_uuid
) == 0) {
1867 if (pcb
->utun_kpipe_pp
!= NULL
) {
1868 kern_pbufpool_destroy(pcb
->utun_kpipe_pp
);
1869 pcb
->utun_kpipe_pp
= NULL
;
1871 utun_unregister_kernel_pipe_nexus();
1874 #endif // UTUN_NEXUS
1877 * We want to do everything in our power to ensure that the interface
1878 * really goes away when the socket is closed. We must remove IP/IPv6
1879 * addresses and detach the protocols. Finally, we can remove and
1880 * release the interface.
1882 utun_cleanup_family(ifp
, AF_INET
);
1883 utun_cleanup_family(ifp
, AF_INET6
);
1886 * Detach now; utun_detach() will be called asynchronously once
1887 * the I/O reference count drops to 0. There we will invoke
1890 if ((result
= ifnet_detach(ifp
)) != 0) {
1891 printf("utun_ctl_disconnect - ifnet_detach failed: %d\n", result
);
1895 // Bound, but not connected
1896 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
1897 utun_free_pcb(pcb
, false);
1904 utun_ctl_send(__unused kern_ctl_ref kctlref
,
1905 __unused u_int32_t unit
,
1911 * The userland ABI requires the first four bytes have the protocol family
1912 * in network byte order: swap them
1914 if (m_pktlen(m
) >= (int32_t)UTUN_HEADER_SIZE((struct utun_pcb
*)unitinfo
)) {
1915 *(protocol_family_t
*)mbuf_data(m
) = ntohl(*(protocol_family_t
*)mbuf_data(m
));
1917 printf("%s - unexpected short mbuf pkt len %d\n", __func__
, m_pktlen(m
) );
1920 return utun_pkt_input((struct utun_pcb
*)unitinfo
, m
);
1924 utun_ctl_setopt(__unused kern_ctl_ref kctlref
,
1925 __unused u_int32_t unit
,
1931 struct utun_pcb
*pcb
= unitinfo
;
1933 /* check for privileges for privileged options */
1935 case UTUN_OPT_FLAGS
:
1936 case UTUN_OPT_EXT_IFDATA_STATS
:
1937 case UTUN_OPT_SET_DELEGATE_INTERFACE
:
1938 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
1945 case UTUN_OPT_FLAGS
:
1946 if (len
!= sizeof(u_int32_t
)) {
1949 if (pcb
->utun_ifp
== NULL
) {
1950 // Only can set after connecting
1955 if (pcb
->utun_use_netif
) {
1956 pcb
->utun_flags
= *(u_int32_t
*)data
;
1958 #endif // UTUN_NEXUS
1960 u_int32_t old_flags
= pcb
->utun_flags
;
1961 pcb
->utun_flags
= *(u_int32_t
*)data
;
1962 if (((old_flags
^ pcb
->utun_flags
) & UTUN_FLAGS_ENABLE_PROC_UUID
)) {
1963 // If UTUN_FLAGS_ENABLE_PROC_UUID flag changed, update bpf
1964 bpfdetach(pcb
->utun_ifp
);
1965 bpfattach(pcb
->utun_ifp
, DLT_NULL
, UTUN_HEADER_SIZE(pcb
));
1971 case UTUN_OPT_EXT_IFDATA_STATS
:
1972 if (len
!= sizeof(int)) {
1976 if (pcb
->utun_ifp
== NULL
) {
1977 // Only can set after connecting
1981 pcb
->utun_ext_ifdata_stats
= (*(int *)data
) ? 1 : 0;
1984 case UTUN_OPT_INC_IFDATA_STATS_IN
:
1985 case UTUN_OPT_INC_IFDATA_STATS_OUT
: {
1986 struct utun_stats_param
*utsp
= (struct utun_stats_param
*)data
;
1988 if (utsp
== NULL
|| len
< sizeof(struct utun_stats_param
)) {
1992 if (pcb
->utun_ifp
== NULL
) {
1993 // Only can set after connecting
1997 if (!pcb
->utun_ext_ifdata_stats
) {
2001 if (opt
== UTUN_OPT_INC_IFDATA_STATS_IN
)
2002 ifnet_stat_increment_in(pcb
->utun_ifp
, utsp
->utsp_packets
,
2003 utsp
->utsp_bytes
, utsp
->utsp_errors
);
2005 ifnet_stat_increment_out(pcb
->utun_ifp
, utsp
->utsp_packets
,
2006 utsp
->utsp_bytes
, utsp
->utsp_errors
);
2009 case UTUN_OPT_SET_DELEGATE_INTERFACE
: {
2010 ifnet_t del_ifp
= NULL
;
2011 char name
[IFNAMSIZ
];
2013 if (len
> IFNAMSIZ
- 1) {
2017 if (pcb
->utun_ifp
== NULL
) {
2018 // Only can set after connecting
2022 if (len
!= 0) { /* if len==0, del_ifp will be NULL causing the delegate to be removed */
2023 bcopy(data
, name
, len
);
2025 result
= ifnet_find_by_name(name
, &del_ifp
);
2028 result
= ifnet_set_delegate(pcb
->utun_ifp
, del_ifp
);
2030 ifnet_release(del_ifp
);
2034 case UTUN_OPT_MAX_PENDING_PACKETS
: {
2035 u_int32_t max_pending_packets
= 0;
2036 if (len
!= sizeof(u_int32_t
)) {
2040 max_pending_packets
= *(u_int32_t
*)data
;
2041 if (max_pending_packets
== 0) {
2045 pcb
->utun_max_pending_packets
= max_pending_packets
;
2049 case UTUN_OPT_ENABLE_CHANNEL
: {
2050 if (len
!= sizeof(int)) {
2054 if (pcb
->utun_ifp
== NULL
) {
2055 // Only can set after connecting
2060 result
= utun_enable_channel(pcb
, current_proc());
2062 result
= utun_disable_channel(pcb
);
2066 case UTUN_OPT_ENABLE_FLOWSWITCH
: {
2067 if (len
!= sizeof(int)) {
2071 if (pcb
->utun_ifp
== NULL
) {
2072 // Only can set after connecting
2076 if (!if_is_netagent_enabled()) {
2080 if (uuid_is_null(pcb
->utun_nx
.ms_agent
)) {
2086 if_add_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2087 pcb
->utun_needs_netagent
= true;
2089 pcb
->utun_needs_netagent
= false;
2090 if_delete_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2094 case UTUN_OPT_ENABLE_NETIF
: {
2095 if (len
!= sizeof(int)) {
2099 if (pcb
->utun_ifp
!= NULL
) {
2100 // Only can set before connecting
2104 lck_rw_lock_exclusive(&pcb
->utun_pcb_lock
);
2105 pcb
->utun_use_netif
= !!(*(int *)data
);
2106 lck_rw_unlock_exclusive(&pcb
->utun_pcb_lock
);
2109 case UTUN_OPT_SLOT_SIZE
: {
2110 if (len
!= sizeof(u_int32_t
)) {
2114 if (pcb
->utun_ifp
!= NULL
) {
2115 // Only can set before connecting
2119 u_int32_t slot_size
= *(u_int32_t
*)data
;
2120 if (slot_size
< UTUN_IF_MIN_SLOT_SIZE
||
2121 slot_size
> UTUN_IF_MAX_SLOT_SIZE
) {
2124 pcb
->utun_slot_size
= slot_size
;
2127 case UTUN_OPT_NETIF_RING_SIZE
: {
2128 if (len
!= sizeof(u_int32_t
)) {
2132 if (pcb
->utun_ifp
!= NULL
) {
2133 // Only can set before connecting
2137 u_int32_t ring_size
= *(u_int32_t
*)data
;
2138 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2139 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2142 pcb
->utun_netif_ring_size
= ring_size
;
2145 case UTUN_OPT_TX_FSW_RING_SIZE
: {
2146 if (len
!= sizeof(u_int32_t
)) {
2150 if (pcb
->utun_ifp
!= NULL
) {
2151 // Only can set before connecting
2155 u_int32_t ring_size
= *(u_int32_t
*)data
;
2156 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2157 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2160 pcb
->utun_tx_fsw_ring_size
= ring_size
;
2163 case UTUN_OPT_RX_FSW_RING_SIZE
: {
2164 if (len
!= sizeof(u_int32_t
)) {
2168 if (pcb
->utun_ifp
!= NULL
) {
2169 // Only can set before connecting
2173 u_int32_t ring_size
= *(u_int32_t
*)data
;
2174 if (ring_size
< UTUN_IF_MIN_RING_SIZE
||
2175 ring_size
> UTUN_IF_MAX_RING_SIZE
) {
2178 pcb
->utun_rx_fsw_ring_size
= ring_size
;
2181 #endif // UTUN_NEXUS
2183 result
= ENOPROTOOPT
;
2192 utun_ctl_getopt(__unused kern_ctl_ref kctlref
,
2193 __unused u_int32_t unit
,
2199 struct utun_pcb
*pcb
= unitinfo
;
2203 case UTUN_OPT_FLAGS
:
2204 if (*len
!= sizeof(u_int32_t
)) {
2207 *(u_int32_t
*)data
= pcb
->utun_flags
;
2211 case UTUN_OPT_EXT_IFDATA_STATS
:
2212 if (*len
!= sizeof(int)) {
2215 *(int *)data
= (pcb
->utun_ext_ifdata_stats
) ? 1 : 0;
2219 case UTUN_OPT_IFNAME
:
2220 if (*len
< MIN(strlen(pcb
->utun_if_xname
) + 1, sizeof(pcb
->utun_if_xname
))) {
2223 if (pcb
->utun_ifp
== NULL
) {
2224 // Only can get after connecting
2228 *len
= snprintf(data
, *len
, "%s", pcb
->utun_if_xname
) + 1;
2232 case UTUN_OPT_MAX_PENDING_PACKETS
: {
2233 if (*len
!= sizeof(u_int32_t
)) {
2236 *((u_int32_t
*)data
) = pcb
->utun_max_pending_packets
;
2242 case UTUN_OPT_ENABLE_CHANNEL
: {
2243 if (*len
!= sizeof(int)) {
2246 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2247 *(int *)data
= pcb
->utun_kpipe_enabled
;
2248 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2253 case UTUN_OPT_ENABLE_FLOWSWITCH
: {
2254 if (*len
!= sizeof(int)) {
2257 *(int *)data
= if_check_netagent(pcb
->utun_ifp
, pcb
->utun_nx
.ms_agent
);
2262 case UTUN_OPT_ENABLE_NETIF
: {
2263 if (*len
!= sizeof(int)) {
2266 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2267 *(int *)data
= !!pcb
->utun_use_netif
;
2268 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2273 case UTUN_OPT_GET_CHANNEL_UUID
: {
2274 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2275 if (uuid_is_null(pcb
->utun_kpipe_uuid
)) {
2277 } else if (*len
!= sizeof(uuid_t
)) {
2280 uuid_copy(data
, pcb
->utun_kpipe_uuid
);
2282 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2285 case UTUN_OPT_SLOT_SIZE
: {
2286 if (*len
!= sizeof(u_int32_t
)) {
2289 *(u_int32_t
*)data
= pcb
->utun_slot_size
;
2293 case UTUN_OPT_NETIF_RING_SIZE
: {
2294 if (*len
!= sizeof(u_int32_t
)) {
2297 *(u_int32_t
*)data
= pcb
->utun_netif_ring_size
;
2301 case UTUN_OPT_TX_FSW_RING_SIZE
: {
2302 if (*len
!= sizeof(u_int32_t
)) {
2305 *(u_int32_t
*)data
= pcb
->utun_tx_fsw_ring_size
;
2309 case UTUN_OPT_RX_FSW_RING_SIZE
: {
2310 if (*len
!= sizeof(u_int32_t
)) {
2313 *(u_int32_t
*)data
= pcb
->utun_rx_fsw_ring_size
;
2317 #endif // UTUN_NEXUS
2320 result
= ENOPROTOOPT
;
2328 utun_ctl_rcvd(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
, int flags
)
2330 #pragma unused(flags)
2331 bool reenable_output
= false;
2332 struct utun_pcb
*pcb
= unitinfo
;
2336 ifnet_lock_exclusive(pcb
->utun_ifp
);
2338 u_int32_t utun_packet_cnt
;
2339 errno_t error_pc
= ctl_getenqueuepacketcount(kctlref
, unit
, &utun_packet_cnt
);
2340 if (error_pc
!= 0) {
2341 printf("utun_ctl_rcvd: ctl_getenqueuepacketcount returned error %d\n", error_pc
);
2342 utun_packet_cnt
= 0;
2345 if (utun_packet_cnt
< pcb
->utun_max_pending_packets
) {
2346 reenable_output
= true;
2349 if (reenable_output
) {
2350 errno_t error
= ifnet_enable_output(pcb
->utun_ifp
);
2352 printf("utun_ctl_rcvd: ifnet_enable_output returned error %d\n", error
);
2355 ifnet_lock_done(pcb
->utun_ifp
);
2358 /* Network Interface functions */
2360 utun_start(ifnet_t interface
)
2363 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2365 VERIFY(pcb
!= NULL
);
2368 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2369 if (pcb
->utun_kpipe_enabled
) {
2370 /* It's possible to have channels enabled, but not yet have the channel opened,
2371 * in which case the rxring will not be set
2373 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2374 if (pcb
->utun_kpipe_rxring
!= NULL
) {
2375 kern_channel_notify(pcb
->utun_kpipe_rxring
, 0);
2379 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2380 #endif // UTUN_NEXUS
2383 bool can_accept_packets
= true;
2384 ifnet_lock_shared(pcb
->utun_ifp
);
2386 u_int32_t utun_packet_cnt
;
2387 errno_t error_pc
= ctl_getenqueuepacketcount(pcb
->utun_ctlref
, pcb
->utun_unit
, &utun_packet_cnt
);
2388 if (error_pc
!= 0) {
2389 printf("utun_start: ctl_getenqueuepacketcount returned error %d\n", error_pc
);
2390 utun_packet_cnt
= 0;
2393 can_accept_packets
= (utun_packet_cnt
< pcb
->utun_max_pending_packets
);
2394 if (!can_accept_packets
&& pcb
->utun_ctlref
) {
2395 u_int32_t difference
= 0;
2396 if (ctl_getenqueuereadable(pcb
->utun_ctlref
, pcb
->utun_unit
, &difference
) == 0) {
2397 if (difference
> 0) {
2398 // If the low-water mark has not yet been reached, we still need to enqueue data
2400 can_accept_packets
= true;
2404 if (!can_accept_packets
) {
2405 errno_t error
= ifnet_disable_output(interface
);
2407 printf("utun_start: ifnet_disable_output returned error %d\n", error
);
2409 ifnet_lock_done(pcb
->utun_ifp
);
2412 ifnet_lock_done(pcb
->utun_ifp
);
2413 if (ifnet_dequeue(interface
, &data
) != 0) {
2416 if (utun_output(interface
, data
) != 0) {
2423 utun_output(ifnet_t interface
,
2426 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2429 VERIFY(interface
== pcb
->utun_ifp
);
2432 if (!pcb
->utun_use_netif
)
2433 #endif // UTUN_NEXUS
2435 if (m_pktlen(data
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2436 bpf_tap_out(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
2440 if (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
) {
2446 // otherwise, fall thru to ctl_enqueumbuf
2447 if (pcb
->utun_ctlref
) {
2451 * The ABI requires the protocol in network byte order
2453 if (m_pktlen(data
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2454 *(u_int32_t
*)mbuf_data(data
) = htonl(*(u_int32_t
*)mbuf_data(data
));
2457 length
= mbuf_pkthdr_len(data
);
2458 result
= ctl_enqueuembuf(pcb
->utun_ctlref
, pcb
->utun_unit
, data
, CTL_DATA_EOR
);
2461 printf("utun_output - ctl_enqueuembuf failed: %d\n", result
);
2463 if (!pcb
->utun_use_netif
)
2464 #endif // UTUN_NEXUS
2466 ifnet_stat_increment_out(interface
, 0, 0, 1);
2470 if (!pcb
->utun_use_netif
)
2471 #endif // UTUN_NEXUS
2473 if (!pcb
->utun_ext_ifdata_stats
) {
2474 ifnet_stat_increment_out(interface
, 1, length
, 0);
2486 utun_demux(__unused ifnet_t interface
,
2488 __unused
char *frame_header
,
2489 protocol_family_t
*protocol
)
2491 while (data
!= NULL
&& mbuf_len(data
) < 1) {
2492 data
= mbuf_next(data
);
2500 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2504 if (pcb
->utun_use_netif
) {
2505 ip
= mtod(data
, struct ip
*);
2506 ip_version
= ip
->ip_v
;
2508 switch(ip_version
) {
2510 *protocol
= PF_INET
;
2513 *protocol
= PF_INET6
;
2520 #endif // UTUN_NEXUS
2522 *protocol
= *(u_int32_t
*)mbuf_data(data
);
2529 utun_framer(ifnet_t interface
,
2531 __unused
const struct sockaddr
*dest
,
2532 __unused
const char *desk_linkaddr
,
2533 const char *frame_type
,
2534 u_int32_t
*prepend_len
,
2535 u_int32_t
*postpend_len
)
2537 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2538 VERIFY(interface
== pcb
->utun_ifp
);
2540 u_int32_t header_length
= UTUN_HEADER_SIZE(pcb
);
2541 if (mbuf_prepend(packet
, header_length
, MBUF_DONTWAIT
) != 0) {
2542 printf("utun_framer - ifnet_output prepend failed\n");
2544 ifnet_stat_increment_out(interface
, 0, 0, 1);
2546 // just return, because the buffer was freed in mbuf_prepend
2549 if (prepend_len
!= NULL
) {
2550 *prepend_len
= header_length
;
2552 if (postpend_len
!= NULL
) {
2556 // place protocol number at the beginning of the mbuf
2557 *(protocol_family_t
*)mbuf_data(*packet
) = *(protocol_family_t
*)(uintptr_t)(size_t)frame_type
;
2564 utun_add_proto(__unused ifnet_t interface
,
2565 protocol_family_t protocol
,
2566 __unused
const struct ifnet_demux_desc
*demux_array
,
2567 __unused u_int32_t demux_count
)
2582 utun_del_proto(__unused ifnet_t interface
,
2583 __unused protocol_family_t protocol
)
2589 utun_ioctl(ifnet_t interface
,
2598 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2599 if (pcb
->utun_use_netif
) {
2600 // Make sure we can fit packets in the channel buffers
2601 // Allow for the headroom in the slot
2602 if (((uint64_t)((struct ifreq
*)data
)->ifr_mtu
) + UTUN_IF_HEADROOM_SIZE
> pcb
->utun_slot_size
) {
2605 ifnet_set_mtu(interface
, (uint32_t)((struct ifreq
*)data
)->ifr_mtu
);
2608 #endif // UTUN_NEXUS
2610 ifnet_set_mtu(interface
, ((struct ifreq
*)data
)->ifr_mtu
);
2616 /* ifioctl() takes care of it */
2620 result
= EOPNOTSUPP
;
2627 utun_detached(ifnet_t interface
)
2629 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2630 (void)ifnet_release(interface
);
2631 utun_free_pcb(pcb
, true);
2634 /* Protocol Handlers */
2637 utun_proto_input(__unused ifnet_t interface
,
2638 protocol_family_t protocol
,
2640 __unused
char *frame_header
)
2642 struct utun_pcb
*pcb
= ifnet_softc(interface
);
2644 if (!pcb
->utun_use_netif
)
2645 #endif // UTUN_NEXUS
2647 mbuf_adj(m
, UTUN_HEADER_SIZE(pcb
));
2649 int32_t pktlen
= m
->m_pkthdr
.len
;
2650 if (proto_input(protocol
, m
) != 0) {
2653 if (!pcb
->utun_use_netif
)
2654 #endif // UTUN_NEXUS
2656 ifnet_stat_increment_in(interface
, 0, 0, 1);
2660 if (!pcb
->utun_use_netif
)
2661 #endif // UTUN_NEXUS
2663 ifnet_stat_increment_in(interface
, 1, pktlen
, 0);
2671 utun_proto_pre_output(__unused ifnet_t interface
,
2672 protocol_family_t protocol
,
2673 __unused mbuf_t
*packet
,
2674 __unused
const struct sockaddr
*dest
,
2675 __unused
void *route
,
2677 __unused
char *link_layer_dest
)
2679 *(protocol_family_t
*)(void *)frame_type
= protocol
;
2684 utun_attach_proto(ifnet_t interface
,
2685 protocol_family_t protocol
)
2687 struct ifnet_attach_proto_param proto
;
2689 bzero(&proto
, sizeof(proto
));
2690 proto
.input
= utun_proto_input
;
2691 proto
.pre_output
= utun_proto_pre_output
;
2693 errno_t result
= ifnet_attach_protocol(interface
, protocol
, &proto
);
2694 if (result
!= 0 && result
!= EEXIST
) {
2695 printf("utun_attach_inet - ifnet_attach_protocol %d failed: %d\n",
2703 utun_pkt_input(struct utun_pcb
*pcb
, mbuf_t packet
)
2706 if (pcb
->utun_use_netif
) {
2707 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2709 lck_mtx_lock(&pcb
->utun_input_chain_lock
);
2710 if (pcb
->utun_input_chain
!= NULL
) {
2711 pcb
->utun_input_chain_last
->m_nextpkt
= packet
;
2713 pcb
->utun_input_chain
= packet
;
2715 while (packet
->m_nextpkt
) {
2716 VERIFY(packet
!= packet
->m_nextpkt
);
2717 packet
= packet
->m_nextpkt
;
2719 pcb
->utun_input_chain_last
= packet
;
2720 lck_mtx_unlock(&pcb
->utun_input_chain_lock
);
2722 kern_channel_ring_t rx_ring
= pcb
->utun_netif_rxring
;
2723 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2725 if (rx_ring
!= NULL
) {
2726 kern_channel_notify(rx_ring
, 0);
2731 #endif // IPSEC_NEXUS
2733 mbuf_pkthdr_setrcvif(packet
, pcb
->utun_ifp
);
2735 if (m_pktlen(packet
) >= (int32_t)UTUN_HEADER_SIZE(pcb
)) {
2736 bpf_tap_in(pcb
->utun_ifp
, DLT_NULL
, packet
, 0, 0);
2738 if (pcb
->utun_flags
& UTUN_FLAGS_NO_INPUT
) {
2745 if (!pcb
->utun_ext_ifdata_stats
) {
2746 struct ifnet_stat_increment_param incs
= {};
2747 incs
.packets_in
= 1;
2748 incs
.bytes_in
= mbuf_pkthdr_len(packet
);
2749 result
= ifnet_input(pcb
->utun_ifp
, packet
, &incs
);
2751 result
= ifnet_input(pcb
->utun_ifp
, packet
, NULL
);
2754 ifnet_stat_increment_in(pcb
->utun_ifp
, 0, 0, 1);
2756 printf("%s - ifnet_input failed: %d\n", __FUNCTION__
, result
);
2767 utun_nxdp_init(__unused kern_nexus_domain_provider_t domprov
)
2773 utun_nxdp_fini(__unused kern_nexus_domain_provider_t domprov
)
2779 utun_register_nexus(void)
2781 const struct kern_nexus_domain_provider_init dp_init
= {
2782 .nxdpi_version
= KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION
,
2784 .nxdpi_init
= utun_nxdp_init
,
2785 .nxdpi_fini
= utun_nxdp_fini
2789 /* utun_nxdp_init() is called before this function returns */
2790 err
= kern_nexus_register_domain_provider(NEXUS_TYPE_NET_IF
,
2791 (const uint8_t *) "com.apple.utun",
2792 &dp_init
, sizeof(dp_init
),
2795 printf("%s: failed to register domain provider\n", __func__
);
2801 utun_interface_needs_netagent(ifnet_t interface
)
2803 struct utun_pcb
*pcb
= NULL
;
2805 if (interface
== NULL
) {
2809 pcb
= ifnet_softc(interface
);
2815 return (pcb
->utun_needs_netagent
== true);
2819 utun_ifnet_set_attrs(ifnet_t ifp
)
2821 /* Set flags and additional information. */
2822 ifnet_set_mtu(ifp
, 1500);
2823 ifnet_set_flags(ifp
, IFF_UP
| IFF_MULTICAST
| IFF_POINTOPOINT
, 0xffff);
2825 /* The interface must generate its own IPv6 LinkLocal address,
2826 * if possible following the recommendation of RFC2472 to the 64bit interface ID
2828 ifnet_set_eflags(ifp
, IFEF_NOAUTOIPV6LL
, IFEF_NOAUTOIPV6LL
);
2834 utun_netif_prepare(kern_nexus_t nexus
, ifnet_t ifp
)
2836 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2837 pcb
->utun_netif_nexus
= nexus
;
2838 return (utun_ifnet_set_attrs(ifp
));
2842 utun_nexus_pre_connect(kern_nexus_provider_t nxprov
,
2843 proc_t p
, kern_nexus_t nexus
,
2844 nexus_port_t nexus_port
, kern_channel_t channel
, void **ch_ctx
)
2846 #pragma unused(nxprov, p)
2847 #pragma unused(nexus, nexus_port, channel, ch_ctx)
2852 utun_nexus_connected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2853 kern_channel_t channel
)
2855 #pragma unused(nxprov, channel)
2856 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2857 boolean_t ok
= ifnet_is_attached(pcb
->utun_ifp
, 1);
2858 return (ok
? 0 : ENXIO
);
2862 utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2863 kern_channel_t channel
)
2865 #pragma unused(nxprov, nexus, channel)
2869 utun_netif_pre_disconnect(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2870 kern_channel_t channel
)
2872 #pragma unused(nxprov, nexus, channel)
2876 utun_nexus_disconnected(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2877 kern_channel_t channel
)
2879 #pragma unused(nxprov, channel)
2880 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2881 if (pcb
->utun_netif_nexus
== nexus
) {
2882 pcb
->utun_netif_nexus
= NULL
;
2884 ifnet_decr_iorefcnt(pcb
->utun_ifp
);
2888 utun_kpipe_ring_init(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2889 kern_channel_t channel
, kern_channel_ring_t ring
,
2890 boolean_t is_tx_ring
, void **ring_ctx
)
2892 #pragma unused(nxprov)
2893 #pragma unused(channel)
2894 #pragma unused(ring_ctx)
2895 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2897 VERIFY(pcb
->utun_kpipe_rxring
== NULL
);
2898 pcb
->utun_kpipe_rxring
= ring
;
2900 VERIFY(pcb
->utun_kpipe_txring
== NULL
);
2901 pcb
->utun_kpipe_txring
= ring
;
2907 utun_kpipe_ring_fini(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2908 kern_channel_ring_t ring
)
2910 #pragma unused(nxprov)
2911 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2912 if (pcb
->utun_kpipe_rxring
== ring
) {
2913 pcb
->utun_kpipe_rxring
= NULL
;
2914 } else if (pcb
->utun_kpipe_txring
== ring
) {
2915 pcb
->utun_kpipe_txring
= NULL
;
2920 utun_kpipe_sync_tx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
2921 kern_channel_ring_t tx_ring
, uint32_t flags
)
2923 #pragma unused(nxprov)
2924 #pragma unused(flags)
2925 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
2927 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
2928 int channel_enabled
= pcb
->utun_kpipe_enabled
;
2929 if (!channel_enabled
) {
2930 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2934 if (pcb
->utun_use_netif
) {
2935 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
2936 if (tx_slot
== NULL
) {
2937 // Nothing to write, bail
2938 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2942 // Signal the netif ring to read
2943 kern_channel_ring_t rx_ring
= pcb
->utun_netif_rxring
;
2944 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2945 if (rx_ring
!= NULL
) {
2946 kern_channel_notify(rx_ring
, 0);
2949 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
2951 struct ifnet_stat_increment_param incs
= {};
2952 struct kern_channel_ring_stat_increment tx_ring_stats
= {};
2953 MBUFQ_HEAD(mbufq
) mbq
;
2955 kern_channel_slot_t tx_pslot
= NULL
;
2956 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
2957 while (tx_slot
!= NULL
) {
2958 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
2962 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
2968 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
2969 VERIFY(tx_buf
!= NULL
);
2970 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
2971 VERIFY(tx_baddr
!= 0);
2972 tx_baddr
+= kern_buflet_get_data_offset(tx_buf
);
2974 size_t length
= MIN(kern_packet_get_data_length(tx_ph
),
2975 pcb
->utun_slot_size
);
2978 if (length
>= UTUN_HEADER_SIZE(pcb
) &&
2979 !(pcb
->utun_flags
& UTUN_FLAGS_NO_INPUT
)) {
2980 errno_t error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_HEADER
, &data
);
2982 error
= mbuf_copyback(data
, 0, length
, tx_baddr
, MBUF_WAITOK
);
2985 * The userland ABI requires the first four bytes have
2986 * the protocol family in network byte order: swap them
2988 *(uint32_t *)mbuf_data(data
) = ntohl(*(uint32_t *)mbuf_data(data
));
2989 mbuf_pkthdr_setrcvif(data
, pcb
->utun_ifp
);
2990 bpf_tap_in(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
2992 incs
.bytes_in
+= length
;
2993 MBUFQ_ENQUEUE(&mbq
, data
);
2997 kern_channel_advance_slot(tx_ring
, tx_pslot
);
2998 tx_ring_stats
.kcrsi_slots_transferred
= incs
.packets_in
;
2999 tx_ring_stats
.kcrsi_bytes_transferred
= incs
.bytes_in
;
3000 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
3001 (void) kern_channel_reclaim(tx_ring
);
3003 if (!MBUFQ_EMPTY(&mbq
)) {
3004 (void) ifnet_input_extended(pcb
->utun_ifp
, MBUFQ_FIRST(&mbq
),
3005 MBUFQ_LAST(&mbq
), &incs
);
3014 utun_kpipe_sync_rx(kern_nexus_provider_t nxprov
, kern_nexus_t nexus
,
3015 kern_channel_ring_t rx_ring
, uint32_t flags
)
3017 #pragma unused(nxprov)
3018 #pragma unused(flags)
3019 struct utun_pcb
*pcb
= kern_nexus_get_context(nexus
);
3020 struct kern_channel_ring_stat_increment rx_ring_stats
= {};
3022 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
3024 int channel_enabled
= pcb
->utun_kpipe_enabled
;
3025 if (!channel_enabled
) {
3026 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3030 /* reclaim user-released slots */
3031 (void) kern_channel_reclaim(rx_ring
);
3033 uint32_t avail
= kern_channel_available_slot_count(rx_ring
);
3035 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3039 if (pcb
->utun_use_netif
) {
3040 kern_channel_ring_t tx_ring
= pcb
->utun_netif_txring
;
3041 if (tx_ring
== NULL
||
3042 pcb
->utun_netif_nexus
== NULL
) {
3043 // Net-If TX ring not set up yet, nothing to read
3044 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3048 struct netif_stats
*nifs
= &NX_NETIF_PRIVATE(pcb
->utun_netif_nexus
)->nif_stats
;
3050 // Unlock utun before entering ring
3051 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3053 (void)kr_enter(tx_ring
, TRUE
);
3055 // Lock again after entering and validate
3056 lck_rw_lock_shared(&pcb
->utun_pcb_lock
);
3057 if (tx_ring
!= pcb
->utun_netif_txring
) {
3058 // Ring no longer valid
3059 // Unlock first, then exit ring
3060 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3065 struct kern_channel_ring_stat_increment tx_ring_stats
;
3066 bzero(&tx_ring_stats
, sizeof(tx_ring_stats
));
3067 kern_channel_slot_t tx_pslot
= NULL
;
3068 kern_channel_slot_t tx_slot
= kern_channel_get_next_slot(tx_ring
, NULL
, NULL
);
3069 if (tx_slot
== NULL
) {
3070 // Nothing to read, don't bother signalling
3071 // Unlock first, then exit ring
3072 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3077 struct kern_pbufpool
*rx_pp
= rx_ring
->ckr_pp
;
3078 VERIFY(rx_pp
!= NULL
);
3079 kern_channel_slot_t rx_pslot
= NULL
;
3080 kern_channel_slot_t rx_slot
= kern_channel_get_next_slot(rx_ring
, NULL
, NULL
);
3082 while (rx_slot
!= NULL
&& tx_slot
!= NULL
) {
3084 kern_buflet_t rx_buf
;
3087 kern_packet_t tx_ph
= kern_channel_slot_get_packet(tx_ring
, tx_slot
);
3091 tx_slot
= kern_channel_get_next_slot(tx_ring
, tx_slot
, NULL
);
3093 /* Skip slot if packet is zero-length or marked as dropped (QUMF_DROPPED) */
3098 // Allocate rx packet
3099 kern_packet_t rx_ph
= 0;
3100 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
3101 if (__improbable(error
!= 0)) {
3102 printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
3103 pcb
->utun_ifp
->if_xname
);
3107 kern_buflet_t tx_buf
= kern_packet_get_next_buflet(tx_ph
, NULL
);
3108 VERIFY(tx_buf
!= NULL
);
3109 uint8_t *tx_baddr
= kern_buflet_get_object_address(tx_buf
);
3110 VERIFY(tx_baddr
!= NULL
);
3111 tx_baddr
+= kern_buflet_get_data_offset(tx_buf
);
3113 bpf_tap_packet_out(pcb
->utun_ifp
, DLT_RAW
, tx_ph
, NULL
, 0);
3115 length
= MIN(kern_packet_get_data_length(tx_ph
) + UTUN_HEADER_SIZE(pcb
),
3116 pcb
->utun_slot_size
);
3118 tx_ring_stats
.kcrsi_slots_transferred
++;
3119 tx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3121 if (length
< UTUN_HEADER_SIZE(pcb
) ||
3122 length
> pcb
->utun_slot_size
||
3123 length
> rx_pp
->pp_buflet_size
||
3124 (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
)) {
3126 kern_pbufpool_free(rx_pp
, rx_ph
);
3127 printf("utun_kpipe_sync_rx %s: invalid length %zu header_size %zu\n",
3128 pcb
->utun_ifp
->if_xname
, length
, UTUN_HEADER_SIZE(pcb
));
3129 STATS_INC(nifs
, NETIF_STATS_BADLEN
);
3130 STATS_INC(nifs
, NETIF_STATS_DROPPED
);
3134 /* fillout packet */
3135 rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
3136 VERIFY(rx_buf
!= NULL
);
3137 rx_baddr
= kern_buflet_get_object_address(rx_buf
);
3138 VERIFY(rx_baddr
!= NULL
);
3142 uint8_t vhl
= *(uint8_t *)(tx_baddr
);
3143 u_int ip_version
= (vhl
>> 4);
3144 switch (ip_version
) {
3154 printf("utun_kpipe_sync_rx %s: unknown ip version %u vhl %u header_size %zu\n",
3155 pcb
->utun_ifp
->if_xname
, ip_version
, vhl
, UTUN_HEADER_SIZE(pcb
));
3162 memcpy((void *)rx_baddr
, &af
, sizeof(af
));
3163 if (pcb
->utun_flags
& UTUN_FLAGS_ENABLE_PROC_UUID
) {
3164 kern_packet_get_euuid(tx_ph
, (void *)(rx_baddr
+ sizeof(af
)));
3167 // Copy data from tx to rx
3168 memcpy((void *)(rx_baddr
+ UTUN_HEADER_SIZE(pcb
)), (void *)tx_baddr
, length
- UTUN_HEADER_SIZE(pcb
));
3169 kern_packet_clear_flow_uuid(rx_ph
); // zero flow id
3171 /* finalize and attach the packet */
3172 error
= kern_buflet_set_data_offset(rx_buf
, 0);
3174 error
= kern_buflet_set_data_length(rx_buf
, length
);
3176 error
= kern_packet_finalize(rx_ph
);
3178 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
3181 STATS_INC(nifs
, NETIF_STATS_TXPKTS
);
3182 STATS_INC(nifs
, NETIF_STATS_TXCOPY_DIRECT
);
3184 rx_ring_stats
.kcrsi_slots_transferred
++;
3185 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3188 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
3192 kern_channel_advance_slot(rx_ring
, rx_pslot
);
3193 kern_channel_increment_ring_net_stats(rx_ring
, pcb
->utun_ifp
, &rx_ring_stats
);
3197 kern_channel_advance_slot(tx_ring
, tx_pslot
);
3198 kern_channel_increment_ring_net_stats(tx_ring
, pcb
->utun_ifp
, &tx_ring_stats
);
3199 (void)kern_channel_reclaim(tx_ring
);
3202 /* just like utun_ctl_rcvd(), always reenable output */
3203 errno_t error
= ifnet_enable_output(pcb
->utun_ifp
);
3205 printf("utun_kpipe_sync_rx: ifnet_enable_output returned error %d\n", error
);
3208 // Unlock first, then exit ring
3209 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3211 if (tx_pslot
!= NULL
) {
3212 kern_channel_notify(tx_ring
, 0);
3216 lck_rw_unlock_shared(&pcb
->utun_pcb_lock
);
3218 uint32_t mb_cnt
= 0;
3219 uint32_t mb_len
= 0;
3220 struct mbuf
*mb_head
= NULL
;
3221 struct mbuf
*mb_tail
= NULL
;
3223 if (ifnet_dequeue_multi(pcb
->utun_ifp
, avail
, &mb_head
,
3224 &mb_tail
, &mb_cnt
, &mb_len
) != 0) {
3227 VERIFY(mb_cnt
<= avail
);
3229 struct kern_pbufpool
*rx_pp
= rx_ring
->ckr_pp
;
3230 VERIFY(rx_pp
!= NULL
);
3231 kern_channel_slot_t rx_pslot
= NULL
;
3232 kern_channel_slot_t rx_slot
= kern_channel_get_next_slot(rx_ring
, NULL
, NULL
);
3236 if ((data
= mb_head
) == NULL
) {
3237 VERIFY(mb_cnt
== 0);
3240 mb_head
= mbuf_nextpkt(mb_head
);
3241 mbuf_setnextpkt(data
, NULL
);
3242 VERIFY(mb_cnt
!= 0);
3244 length
= mbuf_pkthdr_len(data
);
3245 if (length
< UTUN_HEADER_SIZE(pcb
) ||
3246 length
> pcb
->utun_slot_size
||
3247 (pcb
->utun_flags
& UTUN_FLAGS_NO_OUTPUT
)) {
3252 bpf_tap_out(pcb
->utun_ifp
, DLT_NULL
, data
, 0, 0);
3254 // Allocate rx packet
3255 kern_packet_t rx_ph
= 0;
3256 errno_t error
= kern_pbufpool_alloc_nosleep(rx_pp
, 1, &rx_ph
);
3257 if (__improbable(error
!= 0)) {
3258 printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
3259 pcb
->utun_ifp
->if_xname
);
3264 * The ABI requires the protocol in network byte order
3266 *(u_int32_t
*)mbuf_data(data
) = htonl(*(u_int32_t
*)mbuf_data(data
));
3268 // Fillout rx packet
3269 kern_buflet_t rx_buf
= kern_packet_get_next_buflet(rx_ph
, NULL
);
3270 VERIFY(rx_buf
!= NULL
);
3271 void *rx_baddr
= kern_buflet_get_object_address(rx_buf
);
3272 VERIFY(rx_baddr
!= NULL
);
3274 // Copy-in data from mbuf to buflet
3275 mbuf_copydata(data
, 0, length
, (void *)rx_baddr
);
3276 kern_packet_clear_flow_uuid(rx_ph
); // Zero flow id
3278 // Finalize and attach the packet
3279 error
= kern_buflet_set_data_offset(rx_buf
, 0);
3281 error
= kern_buflet_set_data_length(rx_buf
, length
);
3283 error
= kern_packet_finalize(rx_ph
);
3285 error
= kern_channel_slot_attach_packet(rx_ring
, rx_slot
, rx_ph
);
3288 rx_ring_stats
.kcrsi_slots_transferred
++;
3289 rx_ring_stats
.kcrsi_bytes_transferred
+= length
;
3291 if (!pcb
->utun_ext_ifdata_stats
) {
3292 ifnet_stat_increment_out(pcb
->utun_ifp
, 1, length
, 0);
3298 rx_slot
= kern_channel_get_next_slot(rx_ring
, rx_slot
, NULL
);
3301 kern_channel_advance_slot(rx_ring
, rx_pslot
);
3302 kern_channel_increment_ring_stats(rx_ring
, &rx_ring_stats
);
3304 if (mb_head
!= NULL
) {
3305 VERIFY(mb_cnt
!= 0);
3306 mbuf_freem_list(mb_head
);
3313 #endif // UTUN_NEXUS
3317 * These are place holders until coreTLS kext stops calling them
3319 errno_t
utun_ctl_register_dtls (void *reg
);
3320 int utun_pkt_dtls_input(struct utun_pcb
*pcb
, mbuf_t
*pkt
, protocol_family_t family
);
3321 void utun_ctl_disable_crypto_dtls(struct utun_pcb
*pcb
);
3324 utun_ctl_register_dtls (void *reg
)
3331 utun_pkt_dtls_input(struct utun_pcb
*pcb
, mbuf_t
*pkt
, protocol_family_t family
)
3335 #pragma unused(family)
3340 utun_ctl_disable_crypto_dtls(struct utun_pcb
*pcb
)