]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/if_utun.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / net / if_utun.c
index ffaa1124c9e82e4326fd578ea9f9866df5691842..416ef153757dd4023d51a45c08235f21a677aacd 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
+ * Copyright (c) 2008-2018 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * unlawful or unlicensed copies of an Apple operating system, or to
  * circumvent, violate, or enable the circumvention or violation of, any
  * terms of an Apple operating system software license agreement.
- * 
+ *
  * Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 
 
 /* ----------------------------------------------------------------------------------
-Application of kernel control for interface creation
-
-Theory of operation:
-utun (user tunnel) acts as glue between kernel control sockets and network interfaces. 
-This kernel control will register an interface for every client that connects. 
----------------------------------------------------------------------------------- */
+ *   Application of kernel control for interface creation
+ *
+ *   Theory of operation:
+ *   utun (user tunnel) acts as glue between kernel control sockets and network interfaces.
+ *   This kernel control will register an interface for every client that connects.
+ *   ---------------------------------------------------------------------------------- */
 
 #include <sys/systm.h>
 #include <sys/kern_control.h>
@@ -45,7 +45,7 @@ This kernel control will register an interface for every client that connects.
 #include <net/if_types.h>
 #include <net/bpf.h>
 #include <net/if_utun.h>
-#include <sys/mbuf.h> 
+#include <sys/mbuf.h>
 #include <sys/sockio.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
@@ -57,8 +57,6 @@ This kernel control will register an interface for every client that connects.
 
 #define UTUN_NEXUS 0
 
-extern unsigned int if_enable_netagent;
-
 #if UTUN_NEXUS
 static nexus_controller_t utun_ncd;
 static int utun_ncd_refcount;
@@ -79,82 +77,85 @@ typedef struct utun_nx {
 
 /* Control block allocated for each kernel control connection */
 struct utun_pcb {
-       TAILQ_ENTRY(utun_pcb)   utun_chain;
-       kern_ctl_ref    utun_ctlref;
-       ifnet_t                 utun_ifp;
-       u_int32_t               utun_unit;
-       u_int32_t               utun_unique_id;
-       u_int32_t               utun_flags;
-       int                             utun_ext_ifdata_stats;
-       u_int32_t               utun_max_pending_packets;
-       char                    utun_if_xname[IFXNAMSIZ];
-       char                    utun_unique_name[IFXNAMSIZ];
+       TAILQ_ENTRY(utun_pcb)   utun_chain;
+       kern_ctl_ref    utun_ctlref;
+       ifnet_t                 utun_ifp;
+       u_int32_t               utun_unit;
+       u_int32_t               utun_unique_id;
+       u_int32_t               utun_flags;
+       int                             utun_ext_ifdata_stats;
+       u_int32_t               utun_max_pending_packets;
+       char                    utun_if_xname[IFXNAMSIZ];
+       char                    utun_unique_name[IFXNAMSIZ];
        // PCB lock protects state fields and rings
        decl_lck_rw_data(, utun_pcb_lock);
-       struct mbuf *   utun_input_chain;
-       struct mbuf *   utun_input_chain_last;
+       struct mbuf *   utun_input_chain;
+       struct mbuf *   utun_input_chain_last;
        // Input chain lock protects the list of input mbufs
        // The input chain lock must be taken AFTER the PCB lock if both are held
-       lck_mtx_t               utun_input_chain_lock;
+       lck_mtx_t               utun_input_chain_lock;
 
 #if UTUN_NEXUS
-       struct utun_nx  utun_nx;
-       int                             utun_kpipe_enabled;
-       uuid_t                  utun_kpipe_uuid;
-       void *                  utun_kpipe_rxring;
-       void *                  utun_kpipe_txring;
-
-       kern_nexus_t    utun_netif_nexus;
-       void *                  utun_netif_rxring;
-       void *                  utun_netif_txring;
-       uint64_t                utun_netif_txring_size;
-
-       u_int32_t               utun_slot_size;
-       u_int32_t               utun_netif_ring_size;
-       u_int32_t               utun_tx_fsw_ring_size;
-       u_int32_t               utun_rx_fsw_ring_size;
-       bool                    utun_use_netif;
+       struct utun_nx  utun_nx;
+       int                             utun_kpipe_enabled;
+       uuid_t                  utun_kpipe_uuid;
+       void *                  utun_kpipe_rxring;
+       void *                  utun_kpipe_txring;
+       kern_pbufpool_t         utun_kpipe_pp;
+
+       kern_nexus_t    utun_netif_nexus;
+       kern_pbufpool_t         utun_netif_pp;
+       void *                  utun_netif_rxring;
+       void *                  utun_netif_txring;
+       uint64_t                utun_netif_txring_size;
+
+       u_int32_t               utun_slot_size;
+       u_int32_t               utun_netif_ring_size;
+       u_int32_t               utun_tx_fsw_ring_size;
+       u_int32_t               utun_rx_fsw_ring_size;
+       bool                    utun_use_netif;
+       bool                    utun_needs_netagent;
 #endif // UTUN_NEXUS
 };
 
 /* Kernel Control functions */
-static errno_t utun_ctl_bind(kern_ctl_ref kctlref, struct sockaddr_ctl *sac,
-                                                         void **unitinfo);
-static errno_t utun_ctl_connect(kern_ctl_ref kctlref, struct sockaddr_ctl *sac,
-                                                                void **unitinfo);
-static errno_t utun_ctl_disconnect(kern_ctl_ref kctlref, u_int32_t unit,
-                                                                       void *unitinfo);
-static errno_t utun_ctl_send(kern_ctl_ref kctlref, u_int32_t unit,
-                                                          void *unitinfo, mbuf_t m, int flags);
-static errno_t utun_ctl_getopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
-                                                                int opt, void *data, size_t *len);
-static errno_t utun_ctl_setopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
-                                                                int opt, void *data, size_t len);
-static void            utun_ctl_rcvd(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
-                                                               int flags);
+static errno_t  utun_ctl_bind(kern_ctl_ref kctlref, struct sockaddr_ctl *sac,
+    void **unitinfo);
+static errno_t  utun_ctl_connect(kern_ctl_ref kctlref, struct sockaddr_ctl *sac,
+    void **unitinfo);
+static errno_t  utun_ctl_disconnect(kern_ctl_ref kctlref, u_int32_t unit,
+    void *unitinfo);
+static errno_t  utun_ctl_send(kern_ctl_ref kctlref, u_int32_t unit,
+    void *unitinfo, mbuf_t m, int flags);
+static errno_t  utun_ctl_getopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
+    int opt, void *data, size_t *len);
+static errno_t  utun_ctl_setopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
+    int opt, void *data, size_t len);
+static void             utun_ctl_rcvd(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
+    int flags);
 
 /* Network Interface functions */
 static void     utun_start(ifnet_t interface);
-static errno_t utun_framer(ifnet_t interface, mbuf_t *packet,
-                                                       const struct sockaddr *dest, const char *desk_linkaddr,
-                                                       const char *frame_type, u_int32_t *prepend_len, u_int32_t *postpend_len);
-static errno_t utun_output(ifnet_t interface, mbuf_t data);
-static errno_t utun_demux(ifnet_t interface, mbuf_t data, char *frame_header,
-                                                  protocol_family_t *protocol);
-static errno_t utun_add_proto(ifnet_t interface, protocol_family_t protocol,
-                                                          const struct ifnet_demux_desc *demux_array,
-                                                          u_int32_t demux_count);
-static errno_t utun_del_proto(ifnet_t interface, protocol_family_t protocol);
-static errno_t utun_ioctl(ifnet_t interface, u_long cmd, void *data);
-static void            utun_detached(ifnet_t interface);
+static errno_t  utun_framer(ifnet_t interface, mbuf_t *packet,
+    const struct sockaddr *dest, const char *desk_linkaddr,
+    const char *frame_type, u_int32_t *prepend_len, u_int32_t *postpend_len);
+static errno_t  utun_output(ifnet_t interface, mbuf_t data);
+static errno_t  utun_demux(ifnet_t interface, mbuf_t data, char *frame_header,
+    protocol_family_t *protocol);
+static errno_t  utun_add_proto(ifnet_t interface, protocol_family_t protocol,
+    const struct ifnet_demux_desc *demux_array,
+    u_int32_t demux_count);
+static errno_t  utun_del_proto(ifnet_t interface, protocol_family_t protocol);
+static errno_t  utun_ioctl(ifnet_t interface, u_long cmd, void *data);
+static void             utun_detached(ifnet_t interface);
 
 /* Protocol handlers */
-static errno_t utun_attach_proto(ifnet_t interface, protocol_family_t proto);
-static errno_t utun_proto_input(ifnet_t interface, protocol_family_t protocol,
-                                                                mbuf_t m, char *frame_header);
-static errno_t utun_proto_pre_output(ifnet_t interface, protocol_family_t protocol, 
-                                        mbuf_t *packet, const struct sockaddr *dest, void *route,
-                                        char *frame_type, char *link_layer_dest);
+static errno_t  utun_attach_proto(ifnet_t interface, protocol_family_t proto);
+static errno_t  utun_proto_input(ifnet_t interface, protocol_family_t protocol,
+    mbuf_t m, char *frame_header);
+static errno_t utun_proto_pre_output(ifnet_t interface, protocol_family_t protocol,
+    mbuf_t *packet, const struct sockaddr *dest, void *route,
+    char *frame_type, char *link_layer_dest);
 static errno_t utun_pkt_input(struct utun_pcb *pcb, mbuf_t m);
 
 #if UTUN_NEXUS
@@ -163,6 +164,7 @@ static errno_t utun_pkt_input(struct utun_pcb *pcb, mbuf_t m);
 #define UTUN_IF_DEFAULT_RING_SIZE 64
 #define UTUN_IF_DEFAULT_TX_FSW_RING_SIZE 64
 #define UTUN_IF_DEFAULT_RX_FSW_RING_SIZE 128
+#define UTUN_IF_DEFAULT_BUF_SEG_SIZE    skmem_usr_buf_seg_size
 #define UTUN_IF_HEADROOM_SIZE 32
 
 #define UTUN_IF_MIN_RING_SIZE 16
@@ -183,11 +185,11 @@ SYSCTL_DECL(_net_utun);
 SYSCTL_NODE(_net, OID_AUTO, utun, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "UTun");
 
 SYSCTL_PROC(_net_utun, OID_AUTO, ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
-                       &if_utun_ring_size, UTUN_IF_DEFAULT_RING_SIZE, &sysctl_if_utun_ring_size, "I", "");
+    &if_utun_ring_size, UTUN_IF_DEFAULT_RING_SIZE, &sysctl_if_utun_ring_size, "I", "");
 SYSCTL_PROC(_net_utun, OID_AUTO, tx_fsw_ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
-                       &if_utun_tx_fsw_ring_size, UTUN_IF_DEFAULT_TX_FSW_RING_SIZE, &sysctl_if_utun_tx_fsw_ring_size, "I", "");
+    &if_utun_tx_fsw_ring_size, UTUN_IF_DEFAULT_TX_FSW_RING_SIZE, &sysctl_if_utun_tx_fsw_ring_size, "I", "");
 SYSCTL_PROC(_net_utun, OID_AUTO, rx_fsw_ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
-                       &if_utun_rx_fsw_ring_size, UTUN_IF_DEFAULT_RX_FSW_RING_SIZE, &sysctl_if_utun_rx_fsw_ring_size, "I", "");
+    &if_utun_rx_fsw_ring_size, UTUN_IF_DEFAULT_RX_FSW_RING_SIZE, &sysctl_if_utun_rx_fsw_ring_size, "I", "");
 
 static errno_t
 utun_register_nexus(void);
@@ -206,7 +208,7 @@ utun_netif_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
     kern_channel_t channel);
 static void
 utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-       kern_channel_t channel);
+    kern_channel_t channel);
 static void
 utun_nexus_disconnected(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
     kern_channel_t channel);
@@ -228,8 +230,8 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 #define UTUN_DEFAULT_MTU 1500
 #define UTUN_HEADER_SIZE(_pcb) (sizeof(u_int32_t) + (((_pcb)->utun_flags & UTUN_FLAGS_ENABLE_PROC_UUID) ? sizeof(uuid_t) : 0))
 
-static kern_ctl_ref    utun_kctlref;
-static u_int32_t       utun_family;
+static kern_ctl_ref     utun_kctlref;
+static u_int32_t        utun_family;
 static lck_attr_t *utun_lck_attr;
 static lck_grp_attr_t *utun_lck_grp_attr;
 static lck_grp_t *utun_lck_grp;
@@ -237,11 +239,11 @@ static lck_mtx_t utun_lock;
 
 TAILQ_HEAD(utun_list, utun_pcb) utun_head;
 
-#define        UTUN_PCB_ZONE_MAX               32
-#define        UTUN_PCB_ZONE_NAME              "net.if_utun"
+#define UTUN_PCB_ZONE_MAX               32
+#define UTUN_PCB_ZONE_NAME              "net.if_utun"
 
-static unsigned int utun_pcb_size;             /* size of zone element */
-static struct zone *utun_pcb_zone;             /* zone for utun_pcb */
+static unsigned int utun_pcb_size;              /* size of zone element */
+static struct zone *utun_pcb_zone;              /* zone for utun_pcb */
 
 #if UTUN_NEXUS
 
@@ -253,17 +255,17 @@ sysctl_if_utun_ring_size SYSCTL_HANDLER_ARGS
 
        int error = sysctl_handle_int(oidp, &value, 0, req);
        if (error || !req->newptr) {
-               return (error);
+               return error;
        }
 
        if (value < UTUN_IF_MIN_RING_SIZE ||
-               value > UTUN_IF_MAX_RING_SIZE) {
-               return (EINVAL);
+           value > UTUN_IF_MAX_RING_SIZE) {
+               return EINVAL;
        }
 
        if_utun_ring_size = value;
 
-       return (0);
+       return 0;
 }
 
 static int
@@ -274,17 +276,17 @@ sysctl_if_utun_tx_fsw_ring_size SYSCTL_HANDLER_ARGS
 
        int error = sysctl_handle_int(oidp, &value, 0, req);
        if (error || !req->newptr) {
-               return (error);
+               return error;
        }
 
        if (value < UTUN_IF_MIN_RING_SIZE ||
-               value > UTUN_IF_MAX_RING_SIZE) {
-               return (EINVAL);
+           value > UTUN_IF_MAX_RING_SIZE) {
+               return EINVAL;
        }
 
        if_utun_tx_fsw_ring_size = value;
 
-       return (0);
+       return 0;
 }
 
 static int
@@ -295,23 +297,23 @@ sysctl_if_utun_rx_fsw_ring_size SYSCTL_HANDLER_ARGS
 
        int error = sysctl_handle_int(oidp, &value, 0, req);
        if (error || !req->newptr) {
-               return (error);
+               return error;
        }
 
        if (value < UTUN_IF_MIN_RING_SIZE ||
-               value > UTUN_IF_MAX_RING_SIZE) {
-               return (EINVAL);
+           value > UTUN_IF_MAX_RING_SIZE) {
+               return EINVAL;
        }
 
        if_utun_rx_fsw_ring_size = value;
 
-       return (0);
+       return 0;
 }
 
 static errno_t
 utun_netif_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                        kern_channel_t channel, kern_channel_ring_t ring, boolean_t is_tx_ring,
-                                        void **ring_ctx)
+    kern_channel_t channel, kern_channel_ring_t ring, boolean_t is_tx_ring,
+    void **ring_ctx)
 {
 #pragma unused(nxprov)
 #pragma unused(channel)
@@ -329,7 +331,7 @@ utun_netif_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static void
 utun_netif_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                        kern_channel_ring_t ring)
+    kern_channel_ring_t ring)
 {
 #pragma unused(nxprov)
        struct utun_pcb *pcb = kern_nexus_get_context(nexus);
@@ -342,7 +344,7 @@ utun_netif_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static errno_t
 utun_netif_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                  kern_channel_ring_t tx_ring, uint32_t flags)
+    kern_channel_ring_t tx_ring, uint32_t flags)
 {
 #pragma unused(nxprov)
 #pragma unused(flags)
@@ -415,20 +417,20 @@ utun_netif_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                uint8_t vhl = *(uint8_t *)(tx_baddr + tx_offset);
                u_int ip_version = (vhl >> 4);
                switch (ip_version) {
-                       case 4: {
-                               af = AF_INET;
-                               break;
-                       }
-                       case 6: {
-                               af = AF_INET6;
-                               break;
-                       }
-                       default: {
-                               printf("utun_netif_sync_tx %s: unknown ip version %u vhl %u tx_offset %u len %u header_size %zu\n",
-                                          pcb->utun_ifp->if_xname, ip_version, vhl, tx_offset, tx_length,
-                                          UTUN_HEADER_SIZE(pcb));
-                               break;
-                       }
+               case 4: {
+                       af = AF_INET;
+                       break;
+               }
+               case 6: {
+                       af = AF_INET6;
+                       break;
+               }
+               default: {
+                       printf("utun_netif_sync_tx %s: unknown ip version %u vhl %u tx_offset %u len %u header_size %zu\n",
+                           pcb->utun_ifp->if_xname, ip_version, vhl, tx_offset, tx_length,
+                           UTUN_HEADER_SIZE(pcb));
+                       break;
+               }
                }
 
                tx_offset -= UTUN_HEADER_SIZE(pcb);
@@ -496,7 +498,7 @@ utun_netif_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static errno_t
 utun_netif_tx_doorbell(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                          kern_channel_ring_t ring, __unused uint32_t flags)
+    kern_channel_ring_t ring, __unused uint32_t flags)
 {
 #pragma unused(nxprov)
        struct utun_pcb *pcb = kern_nexus_get_context(nexus);
@@ -519,7 +521,7 @@ utun_netif_tx_doorbell(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
        if (pcb->utun_kpipe_enabled) {
                uint32_t tx_available = kern_channel_available_slot_count(ring);
                if (pcb->utun_netif_txring_size > 0 &&
-                       tx_available >= pcb->utun_netif_txring_size - 1) {
+                   tx_available >= pcb->utun_netif_txring_size - 1) {
                        // No room left in tx ring, disable output for now
                        errno_t error = ifnet_disable_output(pcb->utun_ifp);
                        if (error != 0) {
@@ -543,12 +545,12 @@ utun_netif_tx_doorbell(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
        kr_exit(ring);
 
-       return (0);
+       return 0;
 }
 
 static errno_t
 utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                  kern_channel_ring_t rx_ring, uint32_t flags)
+    kern_channel_ring_t rx_ring, uint32_t flags)
 {
 #pragma unused(nxprov)
 #pragma unused(flags)
@@ -588,7 +590,7 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                // Allocate rx packet
                kern_packet_t rx_ph = 0;
                errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
-               if (unlikely(error != 0)) {
+               if (__improbable(error != 0)) {
                        STATS_INC(nifs, NETIF_STATS_NOMEM_PKT);
                        STATS_INC(nifs, NETIF_STATS_DROPPED);
                        lck_mtx_unlock(&pcb->utun_input_chain_lock);
@@ -613,7 +615,7 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        STATS_INC(nifs, NETIF_STATS_BADLEN);
                        STATS_INC(nifs, NETIF_STATS_DROPPED);
                        printf("utun_netif_sync_rx %s: legacy packet length too short for header %zu < %zu\n",
-                                  pcb->utun_ifp->if_xname, length, header_offset);
+                           pcb->utun_ifp->if_xname, length, header_offset);
                        continue;
                }
 
@@ -625,7 +627,7 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        STATS_INC(nifs, NETIF_STATS_BADLEN);
                        STATS_INC(nifs, NETIF_STATS_DROPPED);
                        printf("utun_netif_sync_rx %s: legacy packet length %zu > %u\n",
-                                  pcb->utun_ifp->if_xname, length, rx_pp->pp_buflet_size);
+                           pcb->utun_ifp->if_xname, length, rx_pp->pp_buflet_size);
                        continue;
                }
 
@@ -639,7 +641,7 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
                // Copy-in data from mbuf to buflet
                mbuf_copydata(data, header_offset, length, (void *)rx_baddr);
-               kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
+               kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
 
                // Finalize and attach the packet
                error = kern_buflet_set_data_offset(rx_buf, 0);
@@ -710,8 +712,11 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        continue;
                }
 
+               /* XXX We could try this alloc before advancing the slot to avoid
+                * dropping the packet on failure to allocate.
+                */
                errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
-               if (unlikely(error != 0)) {
+               if (__improbable(error != 0)) {
                        STATS_INC(nifs, NETIF_STATS_NOMEM_PKT);
                        STATS_INC(nifs, NETIF_STATS_DROPPED);
                        break;
@@ -732,12 +737,12 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        STATS_INC(nifs, NETIF_STATS_BADLEN);
                        STATS_INC(nifs, NETIF_STATS_DROPPED);
                        printf("utun_netif_sync_rx %s: packet length too short for header %u < %zu\n",
-                                  pcb->utun_ifp->if_xname, tx_length, header_offset);
+                           pcb->utun_ifp->if_xname, tx_length, header_offset);
                        continue;
                }
 
                size_t length = MIN(tx_length - header_offset,
-                                                       pcb->utun_slot_size);
+                   pcb->utun_slot_size);
 
                tx_ring_stats.kcrsi_slots_transferred++;
                tx_ring_stats.kcrsi_bytes_transferred += length;
@@ -750,7 +755,7 @@ utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
                // Copy-in data from tx to rx
                memcpy((void *)rx_baddr, (void *)(tx_baddr + header_offset), length);
-               kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
+               kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
 
                // Finalize and attach the packet
                error = kern_buflet_set_data_offset(rx_buf, 0);
@@ -803,16 +808,17 @@ done:
 
 static errno_t
 utun_nexus_ifattach(struct utun_pcb *pcb,
-                                       struct ifnet_init_eparams *init_params,
-                                       struct ifnet **ifp)
+    struct ifnet_init_eparams *init_params,
+    struct ifnet **ifp)
 {
        errno_t err;
        nexus_controller_t controller = kern_nexus_shared_controller();
        struct kern_nexus_net_init net_init;
+       struct kern_pbufpool_init pp_init;
 
        nexus_name_t provider_name;
        snprintf((char *)provider_name, sizeof(provider_name),
-                        "com.apple.netif.utun%d", pcb->utun_unit);
+           "com.apple.netif.%s", pcb->utun_if_xname);
 
        struct kern_nexus_provider_init prov_init = {
                .nxpi_version = KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION,
@@ -834,7 +840,7 @@ utun_nexus_ifattach(struct utun_pcb *pcb,
        err = kern_nexus_attr_create(&nxa);
        if (err != 0) {
                printf("%s: kern_nexus_attr_create failed: %d\n",
-                          __func__, err);
+                   __func__, err);
                goto failed;
        }
 
@@ -851,16 +857,31 @@ utun_nexus_ifattach(struct utun_pcb *pcb,
 
        pcb->utun_netif_txring_size = ring_size;
 
+       bzero(&pp_init, sizeof(pp_init));
+       pp_init.kbi_version = KERN_PBUFPOOL_CURRENT_VERSION;
+       pp_init.kbi_packets = pcb->utun_netif_ring_size * 2;
+       pp_init.kbi_bufsize = pcb->utun_slot_size;
+       pp_init.kbi_buf_seg_size = UTUN_IF_DEFAULT_BUF_SEG_SIZE;
+       pp_init.kbi_max_frags = 1;
+       (void) snprintf((char *)pp_init.kbi_name, sizeof(pp_init.kbi_name),
+           "%s", provider_name);
+
+       err = kern_pbufpool_create(&pp_init, &pp_init, &pcb->utun_netif_pp, NULL);
+       if (err != 0) {
+               printf("%s pbufbool create failed, error %d\n", __func__, err);
+               goto failed;
+       }
+
        err = kern_nexus_controller_register_provider(controller,
-                                                                                                 utun_nx_dom_prov,
-                                                                                                 provider_name,
-                                                                                                 &prov_init,
-                                                                                                 sizeof(prov_init),
-                                                                                                 nxa,
-                                                                                                 &pcb->utun_nx.if_provider);
+           utun_nx_dom_prov,
+           provider_name,
+           &prov_init,
+           sizeof(prov_init),
+           nxa,
+           &pcb->utun_nx.if_provider);
        if (err != 0) {
                printf("%s register provider failed, error %d\n",
-                          __func__, err);
+                   __func__, err);
                goto failed;
        }
 
@@ -870,17 +891,18 @@ utun_nexus_ifattach(struct utun_pcb *pcb,
        net_init.nxneti_eparams = init_params;
        net_init.nxneti_lladdr = NULL;
        net_init.nxneti_prepare = utun_netif_prepare;
+       net_init.nxneti_tx_pbufpool = pcb->utun_netif_pp;
        err = kern_nexus_controller_alloc_net_provider_instance(controller,
-                                                                                                                       pcb->utun_nx.if_provider,
-                                                                                                                       pcb,
-                                                                                                                       &pcb->utun_nx.if_instance,
-                                                                                                                       &net_init,
-                                                                                                                       ifp);
+           pcb->utun_nx.if_provider,
+           pcb,
+           &pcb->utun_nx.if_instance,
+           &net_init,
+           ifp);
        if (err != 0) {
                printf("%s alloc_net_provider_instance failed, %d\n",
-                          __func__, err);
+                   __func__, err);
                kern_nexus_controller_deregister_provider(controller,
-                                                                                                 pcb->utun_nx.if_provider);
+                   pcb->utun_nx.if_provider);
                uuid_clear(pcb->utun_nx.if_provider);
                goto failed;
        }
@@ -889,27 +911,31 @@ failed:
        if (nxa) {
                kern_nexus_attr_destroy(nxa);
        }
-       return (err);
+       if (err && pcb->utun_netif_pp != NULL) {
+               kern_pbufpool_destroy(pcb->utun_netif_pp);
+               pcb->utun_netif_pp = NULL;
+       }
+       return err;
 }
 
 static void
 utun_detach_provider_and_instance(uuid_t provider, uuid_t instance)
 {
        nexus_controller_t controller = kern_nexus_shared_controller();
-       errno_t err;
+       errno_t err;
 
        if (!uuid_is_null(instance)) {
                err = kern_nexus_controller_free_provider_instance(controller,
-                                                                                                                  instance);
+                   instance);
                if (err != 0) {
                        printf("%s free_provider_instance failed %d\n",
-                                  __func__, err);
+                           __func__, err);
                }
                uuid_clear(instance);
        }
        if (!uuid_is_null(provider)) {
                err = kern_nexus_controller_deregister_provider(controller,
-                                                                                                               provider);
+                   provider);
                if (err != 0) {
                        printf("%s deregister_provider %d\n", __func__, err);
                }
@@ -919,64 +945,69 @@ utun_detach_provider_and_instance(uuid_t provider, uuid_t instance)
 }
 
 static void
-utun_nexus_detach(utun_nx_t nx)
+utun_nexus_detach(struct utun_pcb *pcb)
 {
+       utun_nx_t nx = &pcb->utun_nx;
        nexus_controller_t controller = kern_nexus_shared_controller();
-       errno_t err;
+       errno_t err;
 
        if (!uuid_is_null(nx->ms_host)) {
                err = kern_nexus_ifdetach(controller,
-                                                                 nx->ms_instance,
-                                                                 nx->ms_host);
+                   nx->ms_instance,
+                   nx->ms_host);
                if (err != 0) {
                        printf("%s: kern_nexus_ifdetach ms host failed %d\n",
-                                  __func__, err);
+                           __func__, err);
                }
        }
 
        if (!uuid_is_null(nx->ms_device)) {
                err = kern_nexus_ifdetach(controller,
-                                                                 nx->ms_instance,
-                                                                 nx->ms_device);
+                   nx->ms_instance,
+                   nx->ms_device);
                if (err != 0) {
                        printf("%s: kern_nexus_ifdetach ms device failed %d\n",
-                                  __func__, err);
+                           __func__, err);
                }
        }
 
        utun_detach_provider_and_instance(nx->if_provider,
-                                                                         nx->if_instance);
+           nx->if_instance);
        utun_detach_provider_and_instance(nx->ms_provider,
-                                                                         nx->ms_instance);
+           nx->ms_instance);
 
+       if (pcb->utun_netif_pp != NULL) {
+               kern_pbufpool_destroy(pcb->utun_netif_pp);
+               pcb->utun_netif_pp = NULL;
+       }
        memset(nx, 0, sizeof(*nx));
 }
 
 static errno_t
 utun_create_fs_provider_and_instance(struct utun_pcb *pcb,
-                                                                        uint32_t subtype, const char *type_name,
-                                                                        const char *ifname,
-                                                                        uuid_t *provider, uuid_t *instance)
+    uint32_t subtype, const char *type_name,
+    const char *ifname,
+    uuid_t *provider, uuid_t *instance)
 {
        nexus_attr_t attr = NULL;
        nexus_controller_t controller = kern_nexus_shared_controller();
        uuid_t dom_prov;
        errno_t err;
        struct kern_nexus_init init;
-       nexus_name_t    provider_name;
+       nexus_name_t    provider_name;
 
        err = kern_nexus_get_builtin_domain_provider(NEXUS_TYPE_FLOW_SWITCH,
-                                                                                                &dom_prov);
+           &dom_prov);
        if (err != 0) {
                printf("%s can't get %s provider, error %d\n",
-                          __func__, type_name, err);
+                   __func__, type_name, err);
                goto failed;
        }
 
        err = kern_nexus_attr_create(&attr);
        if (err != 0) {
                printf("%s: kern_nexus_attr_create failed: %d\n",
-                          __func__, err);
+                   __func__, err);
                goto failed;
        }
 
@@ -996,36 +1027,36 @@ utun_create_fs_provider_and_instance(struct utun_pcb *pcb,
        VERIFY(err == 0);
 
        snprintf((char *)provider_name, sizeof(provider_name),
-                        "com.apple.%s.%s", type_name, ifname);
+           "com.apple.%s.%s", type_name, ifname);
        err = kern_nexus_controller_register_provider(controller,
-                                                                                                 dom_prov,
-                                                                                                 provider_name,
-                                                                                                 NULL,
-                                                                                                 0,
-                                                                                                 attr,
-                                                                                                 provider);
+           dom_prov,
+           provider_name,
+           NULL,
+           0,
+           attr,
+           provider);
        kern_nexus_attr_destroy(attr);
        attr = NULL;
        if (err != 0) {
                printf("%s register %s provider failed, error %d\n",
-                          __func__, type_name, err);
+                   __func__, type_name, err);
                goto failed;
        }
-       bzero(&init, sizeof (init));
+       bzero(&init, sizeof(init));
        init.nxi_version = KERN_NEXUS_CURRENT_VERSION;
        err = kern_nexus_controller_alloc_provider_instance(controller,
-                                                                                                               *provider,
-                                                                                                               NULL,
-                                                                                                               instance, &init);
+           *provider,
+           NULL,
+           instance, &init);
        if (err != 0) {
                printf("%s alloc_provider_instance %s failed, %d\n",
-                          __func__, type_name, err);
+                   __func__, type_name, err);
                kern_nexus_controller_deregister_provider(controller,
-                                                                                                 *provider);
+                   *provider);
                uuid_clear(*provider);
        }
 failed:
-       return (err);
+       return err;
 }
 
 static errno_t
@@ -1037,21 +1068,21 @@ utun_multistack_attach(struct utun_pcb *pcb)
 
        // Allocate multistack flowswitch
        err = utun_create_fs_provider_and_instance(pcb,
-                                                                                          NEXUS_EXTENSION_FSW_TYPE_MULTISTACK,
-                                                                                          "multistack",
-                                                                                          pcb->utun_ifp->if_xname,
-                                                                                          &nx->ms_provider,
-                                                                                          &nx->ms_instance);
+           NEXUS_EXTENSION_FSW_TYPE_MULTISTACK,
+           "multistack",
+           pcb->utun_ifp->if_xname,
+           &nx->ms_provider,
+           &nx->ms_instance);
        if (err != 0) {
                printf("%s: failed to create bridge provider and instance\n",
-                          __func__);
+                   __func__);
                goto failed;
        }
 
        // Attach multistack to device port
        err = kern_nexus_ifattach(controller, nx->ms_instance,
-                                                         NULL, nx->if_instance,
-                                                         FALSE, &nx->ms_device);
+           NULL, nx->if_instance,
+           FALSE, &nx->ms_device);
        if (err != 0) {
                printf("%s kern_nexus_ifattach ms device %d\n", __func__, err);
                goto failed;
@@ -1059,8 +1090,8 @@ utun_multistack_attach(struct utun_pcb *pcb)
 
        // Attach multistack to host port
        err = kern_nexus_ifattach(controller, nx->ms_instance,
-                                                         NULL, nx->if_instance,
-                                                         TRUE, &nx->ms_host);
+           NULL, nx->if_instance,
+           TRUE, &nx->ms_host);
        if (err != 0) {
                printf("%s kern_nexus_ifattach ms host %d\n", __func__, err);
                goto failed;
@@ -1087,18 +1118,18 @@ utun_multistack_attach(struct utun_pcb *pcb)
                printf("utun_multistack_attach - unable to find multistack nexus\n");
        }
 
-       return (0);
+       return 0;
 
 failed:
-       utun_nexus_detach(nx);
+       utun_nexus_detach(pcb);
 
        errno_t detach_error = 0;
        if ((detach_error = ifnet_detach(pcb->utun_ifp)) != 0) {
                panic("utun_multistack_attach - ifnet_detach failed: %d\n", detach_error);
                /* NOT REACHED */
        }
-       
-       return (err);
+
+       return err;
 }
 
 static errno_t
@@ -1116,7 +1147,7 @@ utun_register_kernel_pipe_nexus(void)
        result = kern_nexus_controller_create(&utun_ncd);
        if (result) {
                printf("%s: kern_nexus_controller_create failed: %d\n",
-                       __FUNCTION__, result);
+                   __FUNCTION__, result);
                goto done;
        }
 
@@ -1125,7 +1156,7 @@ utun_register_kernel_pipe_nexus(void)
                NEXUS_TYPE_KERNEL_PIPE, &dom_prov);
        if (result) {
                printf("%s: kern_nexus_get_builtin_domain_provider failed: %d\n",
-                       __FUNCTION__, result);
+                   __FUNCTION__, result);
                goto done;
        }
 
@@ -1148,7 +1179,7 @@ utun_register_kernel_pipe_nexus(void)
        result = kern_nexus_attr_create(&nxa);
        if (result) {
                printf("%s: kern_nexus_attr_create failed: %d\n",
-                       __FUNCTION__, result);
+                   __FUNCTION__, result);
                goto done;
        }
 
@@ -1164,15 +1195,15 @@ utun_register_kernel_pipe_nexus(void)
        VERIFY(result == 0);
 
        result = kern_nexus_controller_register_provider(utun_ncd,
-                                                                                                        dom_prov,
-                                                                                                        (const uint8_t *)"com.apple.nexus.utun.kpipe",
-                                                                                                        &prov_init,
-                                                                                                        sizeof(prov_init),
-                                                                                                        nxa,
-                                                                                                        &utun_kpipe_uuid);
+           dom_prov,
+           (const uint8_t *)"com.apple.nexus.utun.kpipe",
+           &prov_init,
+           sizeof(prov_init),
+           nxa,
+           &utun_kpipe_uuid);
        if (result) {
                printf("%s: kern_nexus_controller_register_provider failed: %d\n",
-                       __FUNCTION__, result);
+                   __FUNCTION__, result);
                goto done;
        }
 
@@ -1236,6 +1267,10 @@ utun_disable_channel(struct utun_pcb *pcb)
        }
 
        if (!result) {
+               if (pcb->utun_kpipe_pp != NULL) {
+                       kern_pbufpool_destroy(pcb->utun_kpipe_pp);
+                       pcb->utun_kpipe_pp = NULL;
+               }
                utun_unregister_kernel_pipe_nexus();
        }
 
@@ -1246,8 +1281,15 @@ static errno_t
 utun_enable_channel(struct utun_pcb *pcb, struct proc *proc)
 {
        struct kern_nexus_init init;
+       struct kern_pbufpool_init pp_init;
        errno_t result;
 
+       kauth_cred_t cred = kauth_cred_get();
+       result = priv_check_cred(cred, PRIV_SKYWALK_REGISTER_KERNEL_PIPE, 0);
+       if (result) {
+               return result;
+       }
+
        result = utun_register_kernel_pipe_nexus();
        if (result) {
                return result;
@@ -1271,22 +1313,40 @@ utun_enable_channel(struct utun_pcb *pcb, struct proc *proc)
                goto done;
        }
 
+       bzero(&pp_init, sizeof(pp_init));
+       pp_init.kbi_version = KERN_PBUFPOOL_CURRENT_VERSION;
+       pp_init.kbi_packets = pcb->utun_netif_ring_size * 2;
+       pp_init.kbi_bufsize = pcb->utun_slot_size;
+       pp_init.kbi_buf_seg_size = UTUN_IF_DEFAULT_BUF_SEG_SIZE;
+       pp_init.kbi_max_frags = 1;
+       pp_init.kbi_flags |= KBIF_QUANTUM;
+       (void) snprintf((char *)pp_init.kbi_name, sizeof(pp_init.kbi_name),
+           "com.apple.kpipe.%s", pcb->utun_if_xname);
+
+       result = kern_pbufpool_create(&pp_init, &pp_init, &pcb->utun_kpipe_pp,
+           NULL);
+       if (result != 0) {
+               printf("%s pbufbool create failed, error %d\n", __func__, result);
+               goto done;
+       }
+
        VERIFY(uuid_is_null(pcb->utun_kpipe_uuid));
-       bzero(&init, sizeof (init));
+       bzero(&init, sizeof(init));
        init.nxi_version = KERN_NEXUS_CURRENT_VERSION;
+       init.nxi_tx_pbufpool = pcb->utun_kpipe_pp;
        result = kern_nexus_controller_alloc_provider_instance(utun_ncd,
-               utun_kpipe_uuid, pcb, &pcb->utun_kpipe_uuid, &init);
+           utun_kpipe_uuid, pcb, &pcb->utun_kpipe_uuid, &init);
        if (result) {
                goto done;
        }
 
        nexus_port_t port = NEXUS_PORT_KERNEL_PIPE_CLIENT;
        result = kern_nexus_controller_bind_provider_instance(utun_ncd,
-               pcb->utun_kpipe_uuid, &port,
-               proc_pid(proc), NULL, NULL, 0, NEXUS_BIND_PID);
+           pcb->utun_kpipe_uuid, &port,
+           proc_pid(proc), NULL, NULL, 0, NEXUS_BIND_PID);
        if (result) {
                kern_nexus_controller_free_provider_instance(utun_ncd,
-                       pcb->utun_kpipe_uuid);
+                   pcb->utun_kpipe_uuid);
                uuid_clear(pcb->utun_kpipe_uuid);
                goto done;
        }
@@ -1297,6 +1357,10 @@ done:
        lck_rw_unlock_exclusive(&pcb->utun_pcb_lock);
 
        if (result) {
+               if (pcb->utun_kpipe_pp != NULL) {
+                       kern_pbufpool_destroy(pcb->utun_kpipe_pp);
+                       pcb->utun_kpipe_pp = NULL;
+               }
                utun_unregister_kernel_pipe_nexus();
        }
 
@@ -1310,7 +1374,7 @@ utun_register_control(void)
 {
        struct kern_ctl_reg kern_ctl;
        errno_t result = 0;
-       
+
        /* Find a unique value for our interface family */
        result = mbuf_tag_id_find(UTUN_CONTROL_NAME, &utun_family);
        if (result != 0) {
@@ -1320,8 +1384,8 @@ utun_register_control(void)
 
        utun_pcb_size = sizeof(struct utun_pcb);
        utun_pcb_zone = zinit(utun_pcb_size,
-                                                 UTUN_PCB_ZONE_MAX * utun_pcb_size,
-                                                 0, UTUN_PCB_ZONE_NAME);
+           UTUN_PCB_ZONE_MAX * utun_pcb_size,
+           0, UTUN_PCB_ZONE_NAME);
        if (utun_pcb_zone == NULL) {
                printf("utun_register_control - zinit(utun_pcb) failed");
                return ENOMEM;
@@ -1332,7 +1396,7 @@ utun_register_control(void)
 #endif // UTUN_NEXUS
 
        TAILQ_INIT(&utun_head);
-       
+
        bzero(&kern_ctl, sizeof(kern_ctl));
        strlcpy(kern_ctl.ctl_name, UTUN_CONTROL_NAME, sizeof(kern_ctl.ctl_name));
        kern_ctl.ctl_name[sizeof(kern_ctl.ctl_name) - 1] = 0;
@@ -1352,29 +1416,29 @@ utun_register_control(void)
                printf("utun_register_control - ctl_register failed: %d\n", result);
                return result;
        }
-       
+
        /* Register the protocol plumbers */
        if ((result = proto_register_plumber(PF_INET, utun_family,
-                                                                                utun_attach_proto, NULL)) != 0) {
+           utun_attach_proto, NULL)) != 0) {
                printf("utun_register_control - proto_register_plumber(PF_INET, %d) failed: %d\n",
-                          utun_family, result);
+                   utun_family, result);
                ctl_deregister(utun_kctlref);
                return result;
        }
-       
+
        /* Register the protocol plumbers */
        if ((result = proto_register_plumber(PF_INET6, utun_family,
-                                                                                utun_attach_proto, NULL)) != 0) {
+           utun_attach_proto, NULL)) != 0) {
                proto_unregister_plumber(PF_INET, utun_family);
                ctl_deregister(utun_kctlref);
                printf("utun_register_control - proto_register_plumber(PF_INET6, %d) failed: %d\n",
-                          utun_family, result);
+                   utun_family, result);
                return result;
        }
 
        utun_lck_attr = lck_attr_alloc_init();
        utun_lck_grp_attr = lck_grp_attr_alloc_init();
-       utun_lck_grp = lck_grp_alloc_init("utun",  utun_lck_grp_attr);
+       utun_lck_grp = lck_grp_alloc_init("utun", utun_lck_grp_attr);
 
        lck_mtx_init(&utun_lock, utun_lck_grp, utun_lck_attr);
 
@@ -1401,8 +1465,8 @@ utun_free_pcb(struct utun_pcb *pcb, bool in_list)
 
 static errno_t
 utun_ctl_bind(kern_ctl_ref kctlref,
-                         struct sockaddr_ctl *sac,
-                         void **unitinfo)
+    struct sockaddr_ctl *sac,
+    void **unitinfo)
 {
        struct utun_pcb *pcb = zalloc(utun_pcb_zone);
        memset(pcb, 0, sizeof(*pcb));
@@ -1423,13 +1487,13 @@ utun_ctl_bind(kern_ctl_ref kctlref,
        lck_mtx_init(&pcb->utun_input_chain_lock, utun_lck_grp, utun_lck_attr);
        lck_rw_init(&pcb->utun_pcb_lock, utun_lck_grp, utun_lck_attr);
 
-       return (0);
+       return 0;
 }
 
 static errno_t
 utun_ctl_connect(kern_ctl_ref kctlref,
-                                struct sockaddr_ctl *sac,
-                                void **unitinfo)
+    struct sockaddr_ctl *sac,
+    void **unitinfo)
 {
        struct ifnet_init_eparams utun_init = {};
        errno_t result = 0;
@@ -1437,7 +1501,7 @@ utun_ctl_connect(kern_ctl_ref kctlref,
        if (*unitinfo == NULL) {
                (void)utun_ctl_bind(kctlref, sac, unitinfo);
        }
-       
+
        struct utun_pcb *pcb = *unitinfo;
 
        lck_mtx_lock(&utun_lock);
@@ -1484,7 +1548,7 @@ utun_ctl_connect(kern_ctl_ref kctlref,
        /* Create the interface */
        bzero(&utun_init, sizeof(utun_init));
        utun_init.ver = IFNET_INIT_CURRENT_VERSION;
-       utun_init.len = sizeof (utun_init);
+       utun_init.len = sizeof(utun_init);
 
 #if UTUN_NEXUS
        if (pcb->utun_use_netif) {
@@ -1567,7 +1631,7 @@ utun_ctl_connect(kern_ctl_ref kctlref,
                        ifnet_release(pcb->utun_ifp);
                        utun_free_pcb(pcb, true);
                        *unitinfo = NULL;
-                       return (result);
+                       return result;
                }
 
                /* Attach to bpf */
@@ -1582,48 +1646,48 @@ utun_ctl_connect(kern_ctl_ref kctlref,
 
 static errno_t
 utun_detach_ip(ifnet_t interface,
-                          protocol_family_t protocol,
-                          socket_t pf_socket)
+    protocol_family_t protocol,
+    socket_t pf_socket)
 {
        errno_t result = EPROTONOSUPPORT;
-       
+
        /* Attempt a detach */
        if (protocol == PF_INET) {
-               struct ifreq    ifr;
-               
+               struct ifreq    ifr;
+
                bzero(&ifr, sizeof(ifr));
                snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d",
-                                ifnet_name(interface), ifnet_unit(interface));
-               
+                   ifnet_name(interface), ifnet_unit(interface));
+
                result = sock_ioctl(pf_socket, SIOCPROTODETACH, &ifr);
        } else if (protocol == PF_INET6) {
-               struct in6_ifreq        ifr6;
-               
+               struct in6_ifreq        ifr6;
+
                bzero(&ifr6, sizeof(ifr6));
                snprintf(ifr6.ifr_name, sizeof(ifr6.ifr_name), "%s%d",
-                                ifnet_name(interface), ifnet_unit(interface));
-               
+                   ifnet_name(interface), ifnet_unit(interface));
+
                result = sock_ioctl(pf_socket, SIOCPROTODETACH_IN6, &ifr6);
        }
-       
+
        return result;
 }
 
 static void
 utun_remove_address(ifnet_t interface,
-                                       protocol_family_t protocol,
-                                       ifaddr_t address,
-                                       socket_t pf_socket)
+    protocol_family_t protocol,
+    ifaddr_t address,
+    socket_t pf_socket)
 {
        errno_t result = 0;
-       
+
        /* Attempt a detach */
        if (protocol == PF_INET) {
                struct ifreq ifr;
-               
+
                bzero(&ifr, sizeof(ifr));
                snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d",
-                                ifnet_name(interface), ifnet_unit(interface));
+                   ifnet_name(interface), ifnet_unit(interface));
                result = ifaddr_address(address, &ifr.ifr_addr, sizeof(ifr.ifr_addr));
                if (result != 0) {
                        printf("utun_remove_address - ifaddr_address failed: %d", result);
@@ -1635,20 +1699,20 @@ utun_remove_address(ifnet_t interface,
                }
        } else if (protocol == PF_INET6) {
                struct in6_ifreq ifr6;
-               
+
                bzero(&ifr6, sizeof(ifr6));
                snprintf(ifr6.ifr_name, sizeof(ifr6.ifr_name), "%s%d",
-                                ifnet_name(interface), ifnet_unit(interface));
+                   ifnet_name(interface), ifnet_unit(interface));
                result = ifaddr_address(address, (struct sockaddr*)&ifr6.ifr_addr,
-                                                               sizeof(ifr6.ifr_addr));
+                   sizeof(ifr6.ifr_addr));
                if (result != 0) {
                        printf("utun_remove_address - ifaddr_address failed (v6): %d",
-                                  result);
+                           result);
                } else {
                        result = sock_ioctl(pf_socket, SIOCDIFADDR_IN6, &ifr6);
                        if (result != 0) {
                                printf("utun_remove_address - SIOCDIFADDR_IN6 failed: %d",
-                                          result);
+                                   result);
                        }
                }
        }
@@ -1656,29 +1720,30 @@ utun_remove_address(ifnet_t interface,
 
 static void
 utun_cleanup_family(ifnet_t interface,
-                                       protocol_family_t protocol)
+    protocol_family_t protocol)
 {
        errno_t result = 0;
        socket_t pf_socket = NULL;
        ifaddr_t *addresses = NULL;
        int i;
-       
+
        if (protocol != PF_INET && protocol != PF_INET6) {
                printf("utun_cleanup_family - invalid protocol family %d\n", protocol);
                return;
        }
-       
+
        /* Create a socket for removing addresses and detaching the protocol */
        result = sock_socket(protocol, SOCK_DGRAM, 0, NULL, NULL, &pf_socket);
        if (result != 0) {
-               if (result != EAFNOSUPPORT)
+               if (result != EAFNOSUPPORT) {
                        printf("utun_cleanup_family - failed to create %s socket: %d\n",
-                               protocol == PF_INET ? "IP" : "IPv6", result);
+                           protocol == PF_INET ? "IP" : "IPv6", result);
+               }
                goto cleanup;
        }
-       
-        /* always set SS_PRIV, we want to close and detach regardless */
-        sock_setpriv(pf_socket, 1);
+
+       /* always set SS_PRIV, we want to close and detach regardless */
+       sock_setpriv(pf_socket, 1);
 
        result = utun_detach_ip(interface, protocol, pf_socket);
        if (result == 0 || result == ENXIO) {
@@ -1689,7 +1754,7 @@ utun_cleanup_family(ifnet_t interface,
                printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result);
                goto cleanup;
        }
-       
+
        /*
         * At this point, we received an EBUSY error. This means there are
         * addresses attached. We should detach them and then try again.
@@ -1697,17 +1762,17 @@ utun_cleanup_family(ifnet_t interface,
        result = ifnet_get_address_list_family(interface, &addresses, protocol);
        if (result != 0) {
                printf("fnet_get_address_list_family(%s%d, 0xblah, %s) - failed: %d\n",
-                       ifnet_name(interface), ifnet_unit(interface), 
-                       protocol == PF_INET ? "PF_INET" : "PF_INET6", result);
+                   ifnet_name(interface), ifnet_unit(interface),
+                   protocol == PF_INET ? "PF_INET" : "PF_INET6", result);
                goto cleanup;
        }
-       
+
        for (i = 0; addresses[i] != 0; i++) {
                utun_remove_address(interface, protocol, addresses[i], pf_socket);
        }
        ifnet_free_address_list(addresses);
        addresses = NULL;
-       
+
        /*
         * The addresses should be gone, we should try the remove again.
         */
@@ -1715,12 +1780,12 @@ utun_cleanup_family(ifnet_t interface,
        if (result != 0 && result != ENXIO) {
                printf("utun_cleanup_family - utun_detach_ip failed: %d\n", result);
        }
-       
+
 cleanup:
        if (pf_socket != NULL) {
                sock_close(pf_socket);
        }
-       
+
        if (addresses != NULL) {
                ifnet_free_address_list(addresses);
        }
@@ -1728,10 +1793,10 @@ cleanup:
 
 static errno_t
 utun_ctl_disconnect(__unused kern_ctl_ref kctlref,
-                                       __unused u_int32_t unit,
-                                       void *unitinfo)
+    __unused u_int32_t unit,
+    void *unitinfo)
 {
-       struct utun_pcb *pcb = unitinfo;
+       struct utun_pcb *pcb = unitinfo;
        ifnet_t ifp = NULL;
        errno_t result = 0;
 
@@ -1786,10 +1851,14 @@ utun_ctl_disconnect(__unused kern_ctl_ref kctlref,
 
                        if (!uuid_is_null(kpipe_uuid)) {
                                if (kern_nexus_controller_free_provider_instance(utun_ncd, kpipe_uuid) == 0) {
+                                       if (pcb->utun_kpipe_pp != NULL) {
+                                               kern_pbufpool_destroy(pcb->utun_kpipe_pp);
+                                               pcb->utun_kpipe_pp = NULL;
+                                       }
                                        utun_unregister_kernel_pipe_nexus();
                                }
                        }
-                       utun_nexus_detach(&pcb->utun_nx);
+                       utun_nexus_detach(pcb);
 
                        /* Decrement refcnt to finish detaching and freeing */
                        ifnet_decr_iorefcnt(ifp);
@@ -1801,6 +1870,10 @@ utun_ctl_disconnect(__unused kern_ctl_ref kctlref,
 #if UTUN_NEXUS
                        if (!uuid_is_null(kpipe_uuid)) {
                                if (kern_nexus_controller_free_provider_instance(utun_ncd, kpipe_uuid) == 0) {
+                                       if (pcb->utun_kpipe_pp != NULL) {
+                                               kern_pbufpool_destroy(pcb->utun_kpipe_pp);
+                                               pcb->utun_kpipe_pp = NULL;
+                                       }
                                        utun_unregister_kernel_pipe_nexus();
                                }
                        }
@@ -1829,25 +1902,25 @@ utun_ctl_disconnect(__unused kern_ctl_ref kctlref,
                lck_rw_unlock_exclusive(&pcb->utun_pcb_lock);
                utun_free_pcb(pcb, false);
        }
-       
+
        return 0;
 }
 
 static errno_t
 utun_ctl_send(__unused kern_ctl_ref kctlref,
-                         __unused u_int32_t unit,
-                         void *unitinfo,
-                         mbuf_t m,
-                         __unused int flags)
+    __unused u_int32_t unit,
+    void *unitinfo,
+    mbuf_t m,
+    __unused int flags)
 {
        /*
-        * The userland ABI requires the first four bytes have the protocol family 
+        * The userland ABI requires the first four bytes have the protocol family
         * in network byte order: swap them
         */
        if (m_pktlen(m) >= (int32_t)UTUN_HEADER_SIZE((struct utun_pcb *)unitinfo)) {
                *(protocol_family_t *)mbuf_data(m) = ntohl(*(protocol_family_t *)mbuf_data(m));
        } else {
-               printf("%s - unexpected short mbuf pkt len %d\n", __func__, m_pktlen(m) );
+               printf("%s - unexpected short mbuf pkt len %d\n", __func__, m_pktlen(m));
        }
 
        return utun_pkt_input((struct utun_pcb *)unitinfo, m);
@@ -1855,263 +1928,269 @@ utun_ctl_send(__unused kern_ctl_ref kctlref,
 
 static errno_t
 utun_ctl_setopt(__unused kern_ctl_ref kctlref,
-                               __unused u_int32_t unit,
-                               void *unitinfo,
-                               int opt,
-                               void *data,
-                               size_t len)
+    __unused u_int32_t unit,
+    void *unitinfo,
+    int opt,
+    void *data,
+    size_t len)
 {
        struct utun_pcb *pcb = unitinfo;
        errno_t result = 0;
        /* check for privileges for privileged options */
        switch (opt) {
-               case UTUN_OPT_FLAGS:
-               case UTUN_OPT_EXT_IFDATA_STATS:
-               case UTUN_OPT_SET_DELEGATE_INTERFACE:
-                       if (kauth_cred_issuser(kauth_cred_get()) == 0) {
-                               return EPERM;
-                       }
-                       break;
+       case UTUN_OPT_FLAGS:
+       case UTUN_OPT_EXT_IFDATA_STATS:
+       case UTUN_OPT_SET_DELEGATE_INTERFACE:
+               if (kauth_cred_issuser(kauth_cred_get()) == 0) {
+                       return EPERM;
+               }
+               break;
        }
 
        switch (opt) {
-               case UTUN_OPT_FLAGS:
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               if (pcb->utun_ifp == NULL) {
-                                       // Only can set after connecting
-                                       result = EINVAL;
-                                       break;
-                               }
+       case UTUN_OPT_FLAGS:
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       if (pcb->utun_ifp == NULL) {
+                               // Only can set after connecting
+                               result = EINVAL;
+                               break;
+                       }
 #if UTUN_NEXUS
-                               if (pcb->utun_use_netif) {
-                                       pcb->utun_flags = *(u_int32_t *)data;
-                               } else
+                       if (pcb->utun_use_netif) {
+                               pcb->utun_flags = *(u_int32_t *)data;
+                       } else
 #endif // UTUN_NEXUS
-                               {
-                                       u_int32_t old_flags = pcb->utun_flags;
-                                       pcb->utun_flags = *(u_int32_t *)data;
-                                       if (((old_flags ^ pcb->utun_flags) & UTUN_FLAGS_ENABLE_PROC_UUID)) {
-                                               // If UTUN_FLAGS_ENABLE_PROC_UUID flag changed, update bpf
-                                               bpfdetach(pcb->utun_ifp);
-                                               bpfattach(pcb->utun_ifp, DLT_NULL, UTUN_HEADER_SIZE(pcb));
-                                       }
+                       {
+                               u_int32_t old_flags = pcb->utun_flags;
+                               pcb->utun_flags = *(u_int32_t *)data;
+                               if (((old_flags ^ pcb->utun_flags) & UTUN_FLAGS_ENABLE_PROC_UUID)) {
+                                       // If UTUN_FLAGS_ENABLE_PROC_UUID flag changed, update bpf
+                                       bpfdetach(pcb->utun_ifp);
+                                       bpfattach(pcb->utun_ifp, DLT_NULL, UTUN_HEADER_SIZE(pcb));
                                }
                        }
+               }
+               break;
+
+       case UTUN_OPT_EXT_IFDATA_STATS:
+               if (len != sizeof(int)) {
+                       result = EMSGSIZE;
                        break;
+               }
+               if (pcb->utun_ifp == NULL) {
+                       // Only can set after connecting
+                       result = EINVAL;
+                       break;
+               }
+               pcb->utun_ext_ifdata_stats = (*(int *)data) ? 1 : 0;
+               break;
 
-               case UTUN_OPT_EXT_IFDATA_STATS:
-                       if (len != sizeof(int)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp == NULL) {
-                               // Only can set after connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       pcb->utun_ext_ifdata_stats = (*(int *)data) ? 1 : 0;
+       case UTUN_OPT_INC_IFDATA_STATS_IN:
+       case UTUN_OPT_INC_IFDATA_STATS_OUT: {
+               struct utun_stats_param *utsp = (struct utun_stats_param *)data;
+
+               if (utsp == NULL || len < sizeof(struct utun_stats_param)) {
+                       result = EINVAL;
                        break;
-                       
-               case UTUN_OPT_INC_IFDATA_STATS_IN:
-               case UTUN_OPT_INC_IFDATA_STATS_OUT: {
-                       struct utun_stats_param *utsp = (struct utun_stats_param *)data;
-                       
-                       if (utsp == NULL || len < sizeof(struct utun_stats_param)) {
-                               result = EINVAL;
-                               break;
-                       }
-                       if (pcb->utun_ifp == NULL) {
-                               // Only can set after connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       if (!pcb->utun_ext_ifdata_stats) {
-                               result = EINVAL;
-                               break;
-                       }
-                       if (opt == UTUN_OPT_INC_IFDATA_STATS_IN)
-                               ifnet_stat_increment_in(pcb->utun_ifp, utsp->utsp_packets, 
-                                       utsp->utsp_bytes, utsp->utsp_errors);
-                       else
-                               ifnet_stat_increment_out(pcb->utun_ifp, utsp->utsp_packets, 
-                                       utsp->utsp_bytes, utsp->utsp_errors);
+               }
+               if (pcb->utun_ifp == NULL) {
+                       // Only can set after connecting
+                       result = EINVAL;
+                       break;
+               }
+               if (!pcb->utun_ext_ifdata_stats) {
+                       result = EINVAL;
                        break;
                }
-               case UTUN_OPT_SET_DELEGATE_INTERFACE: {
-                       ifnet_t         del_ifp = NULL;
-                       char            name[IFNAMSIZ];
+               if (opt == UTUN_OPT_INC_IFDATA_STATS_IN) {
+                       ifnet_stat_increment_in(pcb->utun_ifp, utsp->utsp_packets,
+                           utsp->utsp_bytes, utsp->utsp_errors);
+               } else {
+                       ifnet_stat_increment_out(pcb->utun_ifp, utsp->utsp_packets,
+                           utsp->utsp_bytes, utsp->utsp_errors);
+               }
+               break;
+       }
+       case UTUN_OPT_SET_DELEGATE_INTERFACE: {
+               ifnet_t         del_ifp = NULL;
+               char            name[IFNAMSIZ];
 
-                       if (len > IFNAMSIZ - 1) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp == NULL) {
-                               // Only can set after connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       if (len != 0) {    /* if len==0, del_ifp will be NULL causing the delegate to be removed */
-                               bcopy(data, name, len);
-                               name[len] = 0;
-                               result = ifnet_find_by_name(name, &del_ifp);
-                       }
-                       if (result == 0) {
-                               result = ifnet_set_delegate(pcb->utun_ifp, del_ifp);
-                               if (del_ifp)
-                                       ifnet_release(del_ifp);            
-                       }
+               if (len > IFNAMSIZ - 1) {
+                       result = EMSGSIZE;
                        break;
                }
-               case UTUN_OPT_MAX_PENDING_PACKETS: {
-                       u_int32_t max_pending_packets = 0;
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       max_pending_packets = *(u_int32_t *)data;
-                       if (max_pending_packets == 0) {
-                               result = EINVAL;
-                               break;
+               if (pcb->utun_ifp == NULL) {
+                       // Only can set after connecting
+                       result = EINVAL;
+                       break;
+               }
+               if (len != 0) {            /* if len==0, del_ifp will be NULL causing the delegate to be removed */
+                       bcopy(data, name, len);
+                       name[len] = 0;
+                       result = ifnet_find_by_name(name, &del_ifp);
+               }
+               if (result == 0) {
+                       result = ifnet_set_delegate(pcb->utun_ifp, del_ifp);
+                       if (del_ifp) {
+                               ifnet_release(del_ifp);
                        }
-                       pcb->utun_max_pending_packets = max_pending_packets;
+               }
+               break;
+       }
+       case UTUN_OPT_MAX_PENDING_PACKETS: {
+               u_int32_t max_pending_packets = 0;
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+                       break;
+               }
+               max_pending_packets = *(u_int32_t *)data;
+               if (max_pending_packets == 0) {
+                       result = EINVAL;
                        break;
                }
+               pcb->utun_max_pending_packets = max_pending_packets;
+               break;
+       }
 #if UTUN_NEXUS
-               case UTUN_OPT_ENABLE_CHANNEL: {
-                       if (len != sizeof(int)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp == NULL) {
-                               // Only can set after connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       if (*(int *)data) {
-                               result = utun_enable_channel(pcb, current_proc());
-                       } else {
-                               result = utun_disable_channel(pcb);
-                       }
+       case UTUN_OPT_ENABLE_CHANNEL: {
+               if (len != sizeof(int)) {
+                       result = EMSGSIZE;
+                       break;
+               }
+               if (pcb->utun_ifp == NULL) {
+                       // Only can set after connecting
+                       result = EINVAL;
+                       break;
+               }
+               if (*(int *)data) {
+                       result = utun_enable_channel(pcb, current_proc());
+               } else {
+                       result = utun_disable_channel(pcb);
+               }
+               break;
+       }
+       case UTUN_OPT_ENABLE_FLOWSWITCH: {
+               if (len != sizeof(int)) {
+                       result = EMSGSIZE;
+                       break;
+               }
+               if (pcb->utun_ifp == NULL) {
+                       // Only can set after connecting
+                       result = EINVAL;
+                       break;
+               }
+               if (!if_is_netagent_enabled()) {
+                       result = ENOTSUP;
+                       break;
+               }
+               if (uuid_is_null(pcb->utun_nx.ms_agent)) {
+                       result = ENOENT;
                        break;
                }
-               case UTUN_OPT_ENABLE_FLOWSWITCH: {
-                       if (len != sizeof(int)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp == NULL) {
-                               // Only can set after connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       if (!if_enable_netagent) {
-                               result = ENOTSUP;
-                               break;
-                       }
-                       if (uuid_is_null(pcb->utun_nx.ms_agent)) {
-                               result = ENOENT;
-                               break;
-                       }
 
-                       if (*(int *)data) {
-                               if_add_netagent(pcb->utun_ifp, pcb->utun_nx.ms_agent);
-                       } else {
-                               if_delete_netagent(pcb->utun_ifp, pcb->utun_nx.ms_agent);
-                       }
+               if (*(int *)data) {
+                       if_add_netagent(pcb->utun_ifp, pcb->utun_nx.ms_agent);
+                       pcb->utun_needs_netagent = true;
+               } else {
+                       pcb->utun_needs_netagent = false;
+                       if_delete_netagent(pcb->utun_ifp, pcb->utun_nx.ms_agent);
+               }
+               break;
+       }
+       case UTUN_OPT_ENABLE_NETIF: {
+               if (len != sizeof(int)) {
+                       result = EMSGSIZE;
                        break;
                }
-               case UTUN_OPT_ENABLE_NETIF: {
-                       if (len != sizeof(int)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp != NULL) {
-                               // Only can set before connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       pcb->utun_use_netif = true;
+               if (pcb->utun_ifp != NULL) {
+                       // Only can set before connecting
+                       result = EINVAL;
                        break;
                }
-               case UTUN_OPT_SLOT_SIZE: {
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp != NULL) {
-                               // Only can set before connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       u_int32_t slot_size = *(u_int32_t *)data;
-                       if (slot_size < UTUN_IF_MIN_SLOT_SIZE ||
-                               slot_size > UTUN_IF_MAX_SLOT_SIZE) {
-                               return (EINVAL);
-                       }
-                       pcb->utun_slot_size = slot_size;
+               lck_rw_lock_exclusive(&pcb->utun_pcb_lock);
+               pcb->utun_use_netif = !!(*(int *)data);
+               lck_rw_unlock_exclusive(&pcb->utun_pcb_lock);
+               break;
+       }
+       case UTUN_OPT_SLOT_SIZE: {
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
                        break;
                }
-               case UTUN_OPT_NETIF_RING_SIZE: {
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp != NULL) {
-                               // Only can set before connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       u_int32_t ring_size = *(u_int32_t *)data;
-                       if (ring_size < UTUN_IF_MIN_RING_SIZE ||
-                               ring_size > UTUN_IF_MAX_RING_SIZE) {
-                               return (EINVAL);
-                       }
-                       pcb->utun_netif_ring_size = ring_size;
+               if (pcb->utun_ifp != NULL) {
+                       // Only can set before connecting
+                       result = EINVAL;
                        break;
                }
-               case UTUN_OPT_TX_FSW_RING_SIZE: {
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp != NULL) {
-                               // Only can set before connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       u_int32_t ring_size = *(u_int32_t *)data;
-                       if (ring_size < UTUN_IF_MIN_RING_SIZE ||
-                               ring_size > UTUN_IF_MAX_RING_SIZE) {
-                               return (EINVAL);
-                       }
-                       pcb->utun_tx_fsw_ring_size = ring_size;
+               u_int32_t slot_size = *(u_int32_t *)data;
+               if (slot_size < UTUN_IF_MIN_SLOT_SIZE ||
+                   slot_size > UTUN_IF_MAX_SLOT_SIZE) {
+                       return EINVAL;
+               }
+               pcb->utun_slot_size = slot_size;
+               break;
+       }
+       case UTUN_OPT_NETIF_RING_SIZE: {
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
                        break;
                }
-               case UTUN_OPT_RX_FSW_RING_SIZE: {
-                       if (len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                               break;
-                       }
-                       if (pcb->utun_ifp != NULL) {
-                               // Only can set before connecting
-                               result = EINVAL;
-                               break;
-                       }
-                       u_int32_t ring_size = *(u_int32_t *)data;
-                       if (ring_size < UTUN_IF_MIN_RING_SIZE ||
-                               ring_size > UTUN_IF_MAX_RING_SIZE) {
-                               return (EINVAL);
-                       }
-                       pcb->utun_rx_fsw_ring_size = ring_size;
+               if (pcb->utun_ifp != NULL) {
+                       // Only can set before connecting
+                       result = EINVAL;
                        break;
                }
-#endif // UTUN_NEXUS
-               default: {
-                       result = ENOPROTOOPT;
+               u_int32_t ring_size = *(u_int32_t *)data;
+               if (ring_size < UTUN_IF_MIN_RING_SIZE ||
+                   ring_size > UTUN_IF_MAX_RING_SIZE) {
+                       return EINVAL;
+               }
+               pcb->utun_netif_ring_size = ring_size;
+               break;
+       }
+       case UTUN_OPT_TX_FSW_RING_SIZE: {
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+                       break;
+               }
+               if (pcb->utun_ifp != NULL) {
+                       // Only can set before connecting
+                       result = EINVAL;
                        break;
                }
+               u_int32_t ring_size = *(u_int32_t *)data;
+               if (ring_size < UTUN_IF_MIN_RING_SIZE ||
+                   ring_size > UTUN_IF_MAX_RING_SIZE) {
+                       return EINVAL;
+               }
+               pcb->utun_tx_fsw_ring_size = ring_size;
+               break;
+       }
+       case UTUN_OPT_RX_FSW_RING_SIZE: {
+               if (len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+                       break;
+               }
+               if (pcb->utun_ifp != NULL) {
+                       // Only can set before connecting
+                       result = EINVAL;
+                       break;
+               }
+               u_int32_t ring_size = *(u_int32_t *)data;
+               if (ring_size < UTUN_IF_MIN_RING_SIZE ||
+                   ring_size > UTUN_IF_MAX_RING_SIZE) {
+                       return EINVAL;
+               }
+               pcb->utun_rx_fsw_ring_size = ring_size;
+               break;
+       }
+#endif // UTUN_NEXUS
+       default: {
+               result = ENOPROTOOPT;
+               break;
+       }
        }
 
        return result;
@@ -2119,106 +2198,137 @@ utun_ctl_setopt(__unused kern_ctl_ref kctlref,
 
 static errno_t
 utun_ctl_getopt(__unused kern_ctl_ref kctlref,
-                               __unused u_int32_t unit,
-                               void *unitinfo,
-                               int opt,
-                               void *data,
-                               size_t *len)
+    __unused u_int32_t unit,
+    void *unitinfo,
+    int opt,
+    void *data,
+    size_t *len)
 {
        struct utun_pcb *pcb = unitinfo;
        errno_t result = 0;
-       
+
        switch (opt) {
-               case UTUN_OPT_FLAGS:
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(u_int32_t *)data = pcb->utun_flags;
-                       }
-                       break;
+       case UTUN_OPT_FLAGS:
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(u_int32_t *)data = pcb->utun_flags;
+               }
+               break;
 
-               case UTUN_OPT_EXT_IFDATA_STATS:
-                       if (*len != sizeof(int)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(int *)data = (pcb->utun_ext_ifdata_stats) ? 1 : 0;
-                       }
-                       break;
-               
-               case UTUN_OPT_IFNAME:
-                       if (*len < MIN(strlen(pcb->utun_if_xname) + 1, sizeof(pcb->utun_if_xname))) {
-                               result = EMSGSIZE;
-                       } else {
-                               if (pcb->utun_ifp == NULL) {
-                                       // Only can get after connecting
-                                       result = EINVAL;
-                                       break;
-                               }
-                               *len = snprintf(data, *len, "%s", pcb->utun_if_xname) + 1;
-                       }
-                       break;
+       case UTUN_OPT_EXT_IFDATA_STATS:
+               if (*len != sizeof(int)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(int *)data = (pcb->utun_ext_ifdata_stats) ? 1 : 0;
+               }
+               break;
 
-               case UTUN_OPT_MAX_PENDING_PACKETS: {
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *((u_int32_t *)data) = pcb->utun_max_pending_packets;
+       case UTUN_OPT_IFNAME:
+               if (*len < MIN(strlen(pcb->utun_if_xname) + 1, sizeof(pcb->utun_if_xname))) {
+                       result = EMSGSIZE;
+               } else {
+                       if (pcb->utun_ifp == NULL) {
+                               // Only can get after connecting
+                               result = EINVAL;
+                               break;
                        }
-                       break;
+                       *len = snprintf(data, *len, "%s", pcb->utun_if_xname) + 1;
+               }
+               break;
+
+       case UTUN_OPT_MAX_PENDING_PACKETS: {
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *((u_int32_t *)data) = pcb->utun_max_pending_packets;
                }
+               break;
+       }
 
 #if UTUN_NEXUS
-               case UTUN_OPT_GET_CHANNEL_UUID: {
+       case UTUN_OPT_ENABLE_CHANNEL: {
+               if (*len != sizeof(int)) {
+                       result = EMSGSIZE;
+               } else {
                        lck_rw_lock_shared(&pcb->utun_pcb_lock);
-                       if (uuid_is_null(pcb->utun_kpipe_uuid)) {
-                               result = ENXIO;
-                       } else if (*len != sizeof(uuid_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               uuid_copy(data, pcb->utun_kpipe_uuid);
-                       }
+                       *(int *)data = pcb->utun_kpipe_enabled;
                        lck_rw_unlock_shared(&pcb->utun_pcb_lock);
-                       break;
                }
-               case UTUN_OPT_SLOT_SIZE: {
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(u_int32_t *)data = pcb->utun_slot_size;
-                       }
-                       break;
+               break;
+       }
+
+       case UTUN_OPT_ENABLE_FLOWSWITCH: {
+               if (*len != sizeof(int)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(int *)data = if_check_netagent(pcb->utun_ifp, pcb->utun_nx.ms_agent);
                }
-               case UTUN_OPT_NETIF_RING_SIZE: {
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(u_int32_t *)data = pcb->utun_netif_ring_size;
-                       }
-                       break;
+               break;
+       }
+
+       case UTUN_OPT_ENABLE_NETIF: {
+               if (*len != sizeof(int)) {
+                       result = EMSGSIZE;
+               } else {
+                       lck_rw_lock_shared(&pcb->utun_pcb_lock);
+                       *(int *)data = !!pcb->utun_use_netif;
+                       lck_rw_unlock_shared(&pcb->utun_pcb_lock);
                }
-               case UTUN_OPT_TX_FSW_RING_SIZE: {
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(u_int32_t *)data = pcb->utun_tx_fsw_ring_size;
-                       }
-                       break;
+               break;
+       }
+
+       case UTUN_OPT_GET_CHANNEL_UUID: {
+               lck_rw_lock_shared(&pcb->utun_pcb_lock);
+               if (uuid_is_null(pcb->utun_kpipe_uuid)) {
+                       result = ENXIO;
+               } else if (*len != sizeof(uuid_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       uuid_copy(data, pcb->utun_kpipe_uuid);
                }
-               case UTUN_OPT_RX_FSW_RING_SIZE: {
-                       if (*len != sizeof(u_int32_t)) {
-                               result = EMSGSIZE;
-                       } else {
-                               *(u_int32_t *)data = pcb->utun_rx_fsw_ring_size;
-                       }
-                       break;
+               lck_rw_unlock_shared(&pcb->utun_pcb_lock);
+               break;
+       }
+       case UTUN_OPT_SLOT_SIZE: {
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(u_int32_t *)data = pcb->utun_slot_size;
+               }
+               break;
+       }
+       case UTUN_OPT_NETIF_RING_SIZE: {
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(u_int32_t *)data = pcb->utun_netif_ring_size;
+               }
+               break;
+       }
+       case UTUN_OPT_TX_FSW_RING_SIZE: {
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(u_int32_t *)data = pcb->utun_tx_fsw_ring_size;
+               }
+               break;
+       }
+       case UTUN_OPT_RX_FSW_RING_SIZE: {
+               if (*len != sizeof(u_int32_t)) {
+                       result = EMSGSIZE;
+               } else {
+                       *(u_int32_t *)data = pcb->utun_rx_fsw_ring_size;
                }
+               break;
+       }
 #endif // UTUN_NEXUS
 
-               default:
-                       result = ENOPROTOOPT;
-                       break;
+       default:
+               result = ENOPROTOOPT;
+               break;
        }
-       
+
        return result;
 }
 
@@ -2318,10 +2428,10 @@ utun_start(ifnet_t interface)
 }
 
 static errno_t
-utun_output(ifnet_t    interface,
-                       mbuf_t data)
+utun_output(ifnet_t     interface,
+    mbuf_t data)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
+       struct utun_pcb *pcb = ifnet_softc(interface);
        errno_t result;
 
        VERIFY(interface == pcb->utun_ifp);
@@ -2343,7 +2453,7 @@ utun_output(ifnet_t       interface,
 
        // otherwise, fall thru to ctl_enqueumbuf
        if (pcb->utun_ctlref) {
-               int     length;
+               int     length;
 
                /*
                 * The ABI requires the protocol in network byte order
@@ -2376,19 +2486,21 @@ utun_output(ifnet_t     interface,
        } else {
                mbuf_freem(data);
        }
-       
+
        return 0;
 }
 
 static errno_t
 utun_demux(__unused ifnet_t interface,
-                  mbuf_t data,
-                  __unused char *frame_header,
-                  protocol_family_t *protocol)
+    mbuf_t data,
+    __unused char *frame_header,
+    protocol_family_t *protocol)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
+#if UTUN_NEXUS
+       struct utun_pcb *pcb = ifnet_softc(interface);
        struct ip *ip;
        u_int ip_version;
+#endif
 
        while (data != NULL && mbuf_len(data) < 1) {
                data = mbuf_next(data);
@@ -2403,16 +2515,16 @@ utun_demux(__unused ifnet_t interface,
                ip = mtod(data, struct ip *);
                ip_version = ip->ip_v;
 
-               switch(ip_version) {
-                       case 4:
-                               *protocol = PF_INET;
-                               return 0;
-                       case 6:
-                               *protocol = PF_INET6;
-                               return 0;
-                       default:
-                               *protocol = 0;
-                               break;
+               switch (ip_version) {
+               case 4:
+                       *protocol = PF_INET;
+                       return 0;
+               case 6:
+                       *protocol = PF_INET6;
+                       return 0;
+               default:
+                       *protocol = 0;
+                       break;
                }
        } else
 #endif // UTUN_NEXUS
@@ -2425,106 +2537,108 @@ utun_demux(__unused ifnet_t interface,
 
 static errno_t
 utun_framer(ifnet_t interface,
-                       mbuf_t *packet,
-                       __unused const struct sockaddr *dest,
-                       __unused const char *desk_linkaddr,
-                       const char *frame_type,
-                       u_int32_t *prepend_len,
-                       u_int32_t *postpend_len)
+    mbuf_t *packet,
+    __unused const struct sockaddr *dest,
+    __unused const char *desk_linkaddr,
+    const char *frame_type,
+    u_int32_t *prepend_len,
+    u_int32_t *postpend_len)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
+       struct utun_pcb *pcb = ifnet_softc(interface);
        VERIFY(interface == pcb->utun_ifp);
 
        u_int32_t header_length = UTUN_HEADER_SIZE(pcb);
-    if (mbuf_prepend(packet, header_length, MBUF_DONTWAIT) != 0) {
+       if (mbuf_prepend(packet, header_length, MBUF_DONTWAIT) != 0) {
                printf("utun_framer - ifnet_output prepend failed\n");
 
                ifnet_stat_increment_out(interface, 0, 0, 1);
 
                // just return, because the buffer was freed in mbuf_prepend
-        return EJUSTRETURN;    
-    }
+               return EJUSTRETURN;
+       }
        if (prepend_len != NULL) {
                *prepend_len = header_length;
        }
        if (postpend_len != NULL) {
                *postpend_len = 0;
        }
-       
-    // place protocol number at the beginning of the mbuf
-    *(protocol_family_t *)mbuf_data(*packet) = *(protocol_family_t *)(uintptr_t)(size_t)frame_type;
+
+       // place protocol number at the beginning of the mbuf
+       *(protocol_family_t *)mbuf_data(*packet) = *(protocol_family_t *)(uintptr_t)(size_t)frame_type;
 
 
-    return 0;
+       return 0;
 }
 
 static errno_t
 utun_add_proto(__unused ifnet_t interface,
-                          protocol_family_t protocol,
-                          __unused const struct ifnet_demux_desc *demux_array,
-                          __unused u_int32_t demux_count)
+    protocol_family_t protocol,
+    __unused const struct ifnet_demux_desc *demux_array,
+    __unused u_int32_t demux_count)
 {
-       switch(protocol) {
-               case PF_INET:
-                       return 0;
-               case PF_INET6:
-                       return 0;
-               default:
-                       break;
+       switch (protocol) {
+       case PF_INET:
+               return 0;
+       case PF_INET6:
+               return 0;
+       default:
+               break;
        }
-       
+
        return ENOPROTOOPT;
 }
 
 static errno_t
 utun_del_proto(__unused ifnet_t interface,
-                          __unused protocol_family_t protocol)
+    __unused protocol_family_t protocol)
 {
        return 0;
 }
 
 static errno_t
 utun_ioctl(ifnet_t interface,
-                  u_long command,
-                  void *data)
+    u_long command,
+    void *data)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
-       errno_t result = 0;
-       
-       switch(command) {
-               case SIOCSIFMTU: {
 #if UTUN_NEXUS
-                       if (pcb->utun_use_netif) {
-                               // Make sure we can fit packets in the channel buffers
-                               // Allow for the headroom in the slot
-                               if (((uint64_t)((struct ifreq*)data)->ifr_mtu) + UTUN_IF_HEADROOM_SIZE > pcb->utun_slot_size) {
-                                       result = EINVAL;
-                               } else {
-                                       ifnet_set_mtu(interface, (uint32_t)((struct ifreq*)data)->ifr_mtu);
-                               }
-                       } else
-#endif // UTUN_NEXUS
-                       {
-                               ifnet_set_mtu(interface, ((struct ifreq*)data)->ifr_mtu);
+       struct utun_pcb *pcb = ifnet_softc(interface);
+#endif
+       errno_t result = 0;
+
+       switch (command) {
+       case SIOCSIFMTU: {
+#if UTUN_NEXUS
+               if (pcb->utun_use_netif) {
+                       // Make sure we can fit packets in the channel buffers
+                       // Allow for the headroom in the slot
+                       if (((uint64_t)((struct ifreq*)data)->ifr_mtu) + UTUN_IF_HEADROOM_SIZE > pcb->utun_slot_size) {
+                               result = EINVAL;
+                       } else {
+                               ifnet_set_mtu(interface, (uint32_t)((struct ifreq*)data)->ifr_mtu);
                        }
-                       break;
+               } else
+#endif // UTUN_NEXUS
+               {
+                       ifnet_set_mtu(interface, ((struct ifreq*)data)->ifr_mtu);
                }
-                       
-               case SIOCSIFFLAGS:
-                       /* ifioctl() takes care of it */
-                       break;
-                       
-               default:
-                       result = EOPNOTSUPP;
+               break;
        }
-       
+
+       case SIOCSIFFLAGS:
+               /* ifioctl() takes care of it */
+               break;
+
+       default:
+               result = EOPNOTSUPP;
+       }
+
        return result;
 }
 
 static void
 utun_detached(ifnet_t interface)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
+       struct utun_pcb *pcb = ifnet_softc(interface);
        (void)ifnet_release(interface);
        utun_free_pcb(pcb, true);
 }
@@ -2533,17 +2647,18 @@ utun_detached(ifnet_t interface)
 
 static errno_t
 utun_proto_input(__unused ifnet_t interface,
-                                protocol_family_t protocol,
-                                mbuf_t m,
-                                __unused char *frame_header)
+    protocol_family_t protocol,
+    mbuf_t m,
+    __unused char *frame_header)
 {
-       struct utun_pcb *pcb = ifnet_softc(interface);
+       struct utun_pcb *pcb = ifnet_softc(interface);
 #if UTUN_NEXUS
        if (!pcb->utun_use_netif)
 #endif // UTUN_NEXUS
        {
                mbuf_adj(m, UTUN_HEADER_SIZE(pcb));
        }
+       int32_t pktlen = m->m_pkthdr.len;
        if (proto_input(protocol, m) != 0) {
                m_freem(m);
 #if UTUN_NEXUS
@@ -2557,21 +2672,21 @@ utun_proto_input(__unused ifnet_t interface,
                if (!pcb->utun_use_netif)
 #endif // UTUN_NEXUS
                {
-                       ifnet_stat_increment_in(interface, 1, m->m_pkthdr.len, 0);
+                       ifnet_stat_increment_in(interface, 1, pktlen, 0);
                }
        }
-       
+
        return 0;
 }
 
 static errno_t
 utun_proto_pre_output(__unused ifnet_t interface,
-                                         protocol_family_t protocol,
-                                         __unused mbuf_t *packet,
-                                         __unused const struct sockaddr *dest,
-                                         __unused void *route,
-                                         char *frame_type,
-                                         __unused char *link_layer_dest)
+    protocol_family_t protocol,
+    __unused mbuf_t *packet,
+    __unused const struct sockaddr *dest,
+    __unused void *route,
+    char *frame_type,
+    __unused char *link_layer_dest)
 {
        *(protocol_family_t *)(void *)frame_type = protocol;
        return 0;
@@ -2579,10 +2694,10 @@ utun_proto_pre_output(__unused ifnet_t interface,
 
 static errno_t
 utun_attach_proto(ifnet_t interface,
-                                 protocol_family_t protocol)
+    protocol_family_t protocol)
 {
-       struct ifnet_attach_proto_param proto;
-       
+       struct ifnet_attach_proto_param proto;
+
        bzero(&proto, sizeof(proto));
        proto.input = utun_proto_input;
        proto.pre_output = utun_proto_pre_output;
@@ -2590,9 +2705,9 @@ utun_attach_proto(ifnet_t interface,
        errno_t result = ifnet_attach_protocol(interface, protocol, &proto);
        if (result != 0 && result != EEXIST) {
                printf("utun_attach_inet - ifnet_attach_protocol %d failed: %d\n",
-                       protocol, result);
+                   protocol, result);
        }
-       
+
        return result;
 }
 
@@ -2623,13 +2738,13 @@ utun_pkt_input(struct utun_pcb *pcb, mbuf_t packet)
                        kern_channel_notify(rx_ring, 0);
                }
 
-               return (0);
+               return 0;
        } else
 #endif // IPSEC_NEXUS
        {
                mbuf_pkthdr_setrcvif(packet, pcb->utun_ifp);
 
-               if (m_pktlen(packet) >= (int32_t)UTUN_HEADER_SIZE(pcb))  {
+               if (m_pktlen(packet) >= (int32_t)UTUN_HEADER_SIZE(pcb)) {
                        bpf_tap_in(pcb->utun_ifp, DLT_NULL, packet, 0, 0);
                }
                if (pcb->utun_flags & UTUN_FLAGS_NO_INPUT) {
@@ -2638,7 +2753,7 @@ utun_pkt_input(struct utun_pcb *pcb, mbuf_t packet)
                        return 0;
                }
 
-               errno_t result = 0;
+               errno_t result = 0;
                if (!pcb->utun_ext_ifdata_stats) {
                        struct ifnet_stat_increment_param incs = {};
                        incs.packets_in = 1;
@@ -2654,7 +2769,7 @@ utun_pkt_input(struct utun_pcb *pcb, mbuf_t packet)
                        mbuf_freem(packet);
                }
 
-               return (0);
+               return 0;
        }
 }
 
@@ -2685,14 +2800,31 @@ utun_register_nexus(void)
 
        /* utun_nxdp_init() is called before this function returns */
        err = kern_nexus_register_domain_provider(NEXUS_TYPE_NET_IF,
-                                                                                         (const uint8_t *) "com.apple.utun",
-                                                                                         &dp_init, sizeof(dp_init),
-                                                                                         &utun_nx_dom_prov);
+           (const uint8_t *) "com.apple.utun",
+           &dp_init, sizeof(dp_init),
+           &utun_nx_dom_prov);
        if (err != 0) {
                printf("%s: failed to register domain provider\n", __func__);
-               return (err);
+               return err;
        }
-       return (0);
+       return 0;
+}
+boolean_t
+utun_interface_needs_netagent(ifnet_t interface)
+{
+       struct utun_pcb *pcb = NULL;
+
+       if (interface == NULL) {
+               return FALSE;
+       }
+
+       pcb = ifnet_softc(interface);
+
+       if (pcb == NULL) {
+               return FALSE;
+       }
+
+       return pcb->utun_needs_netagent == true;
 }
 
 static errno_t
@@ -2707,7 +2839,7 @@ utun_ifnet_set_attrs(ifnet_t ifp)
         */
        ifnet_set_eflags(ifp, IFEF_NOAUTOIPV6LL, IFEF_NOAUTOIPV6LL);
 
-       return (0);
+       return 0;
 }
 
 static errno_t
@@ -2715,7 +2847,7 @@ utun_netif_prepare(kern_nexus_t nexus, ifnet_t ifp)
 {
        struct utun_pcb *pcb = kern_nexus_get_context(nexus);
        pcb->utun_netif_nexus = nexus;
-       return (utun_ifnet_set_attrs(ifp));
+       return utun_ifnet_set_attrs(ifp);
 }
 
 static errno_t
@@ -2725,7 +2857,7 @@ utun_nexus_pre_connect(kern_nexus_provider_t nxprov,
 {
 #pragma unused(nxprov, p)
 #pragma unused(nexus, nexus_port, channel, ch_ctx)
-       return (0);
+       return 0;
 }
 
 static errno_t
@@ -2735,7 +2867,7 @@ utun_nexus_connected(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 #pragma unused(nxprov, channel)
        struct utun_pcb *pcb = kern_nexus_get_context(nexus);
        boolean_t ok = ifnet_is_attached(pcb->utun_ifp, 1);
-       return (ok ? 0 : ENXIO);
+       return ok ? 0 : ENXIO;
 }
 
 static void
@@ -2747,7 +2879,7 @@ utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static void
 utun_netif_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                                 kern_channel_t channel)
+    kern_channel_t channel)
 {
 #pragma unused(nxprov, nexus, channel)
 }
@@ -2766,8 +2898,8 @@ utun_nexus_disconnected(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static errno_t
 utun_kpipe_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                        kern_channel_t channel, kern_channel_ring_t ring,
-                                        boolean_t is_tx_ring, void **ring_ctx)
+    kern_channel_t channel, kern_channel_ring_t ring,
+    boolean_t is_tx_ring, void **ring_ctx)
 {
 #pragma unused(nxprov)
 #pragma unused(channel)
@@ -2785,7 +2917,7 @@ utun_kpipe_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static void
 utun_kpipe_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                        kern_channel_ring_t ring)
+    kern_channel_ring_t ring)
 {
 #pragma unused(nxprov)
        struct utun_pcb *pcb = kern_nexus_get_context(nexus);
@@ -2798,7 +2930,7 @@ utun_kpipe_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static errno_t
 utun_kpipe_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                  kern_channel_ring_t tx_ring, uint32_t flags)
+    kern_channel_ring_t tx_ring, uint32_t flags)
 {
 #pragma unused(nxprov)
 #pragma unused(flags)
@@ -2852,11 +2984,11 @@ utun_kpipe_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        tx_baddr += kern_buflet_get_data_offset(tx_buf);
 
                        size_t length = MIN(kern_packet_get_data_length(tx_ph),
-                                                               pcb->utun_slot_size);
+                           pcb->utun_slot_size);
 
                        mbuf_t data = NULL;
                        if (length >= UTUN_HEADER_SIZE(pcb) &&
-                               !(pcb->utun_flags & UTUN_FLAGS_NO_INPUT)) {
+                           !(pcb->utun_flags & UTUN_FLAGS_NO_INPUT)) {
                                errno_t error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_HEADER, &data);
                                VERIFY(0 == error);
                                error = mbuf_copyback(data, 0, length, tx_baddr, MBUF_WAITOK);
@@ -2882,7 +3014,7 @@ utun_kpipe_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                }
                if (!MBUFQ_EMPTY(&mbq)) {
                        (void) ifnet_input_extended(pcb->utun_ifp, MBUFQ_FIRST(&mbq),
-                                                                               MBUFQ_LAST(&mbq), &incs);
+                           MBUFQ_LAST(&mbq), &incs);
                        MBUFQ_INIT(&mbq);
                }
        }
@@ -2892,7 +3024,7 @@ utun_kpipe_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
 static errno_t
 utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
-                                  kern_channel_ring_t rx_ring, uint32_t flags)
+    kern_channel_ring_t rx_ring, uint32_t flags)
 {
 #pragma unused(nxprov)
 #pragma unused(flags)
@@ -2919,7 +3051,7 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
        if (pcb->utun_use_netif) {
                kern_channel_ring_t tx_ring = pcb->utun_netif_txring;
                if (tx_ring == NULL ||
-                       pcb->utun_netif_nexus == NULL) {
+                   pcb->utun_netif_nexus == NULL) {
                        // Net-If TX ring not set up yet, nothing to read
                        lck_rw_unlock_shared(&pcb->utun_pcb_lock);
                        return 0;
@@ -2978,9 +3110,9 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        // Allocate rx packet
                        kern_packet_t rx_ph = 0;
                        errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
-                       if (unlikely(error != 0)) {
+                       if (__improbable(error != 0)) {
                                printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
-                                          pcb->utun_ifp->if_xname);
+                                   pcb->utun_ifp->if_xname);
                                break;
                        }
 
@@ -2993,19 +3125,19 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        bpf_tap_packet_out(pcb->utun_ifp, DLT_RAW, tx_ph, NULL, 0);
 
                        length = MIN(kern_packet_get_data_length(tx_ph) + UTUN_HEADER_SIZE(pcb),
-                                                pcb->utun_slot_size);
+                           pcb->utun_slot_size);
 
                        tx_ring_stats.kcrsi_slots_transferred++;
                        tx_ring_stats.kcrsi_bytes_transferred += length;
 
                        if (length < UTUN_HEADER_SIZE(pcb) ||
-                               length > pcb->utun_slot_size ||
-                               length > rx_pp->pp_buflet_size ||
-                               (pcb->utun_flags & UTUN_FLAGS_NO_OUTPUT)) {
+                           length > pcb->utun_slot_size ||
+                           length > rx_pp->pp_buflet_size ||
+                           (pcb->utun_flags & UTUN_FLAGS_NO_OUTPUT)) {
                                /* flush data */
                                kern_pbufpool_free(rx_pp, rx_ph);
                                printf("utun_kpipe_sync_rx %s: invalid length %zu header_size %zu\n",
-                                          pcb->utun_ifp->if_xname, length, UTUN_HEADER_SIZE(pcb));
+                                   pcb->utun_ifp->if_xname, length, UTUN_HEADER_SIZE(pcb));
                                STATS_INC(nifs, NETIF_STATS_BADLEN);
                                STATS_INC(nifs, NETIF_STATS_DROPPED);
                                continue;
@@ -3022,19 +3154,19 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        uint8_t vhl = *(uint8_t *)(tx_baddr);
                        u_int ip_version = (vhl >> 4);
                        switch (ip_version) {
-                               case 4: {
-                                       af = AF_INET;
-                                       break;
-                               }
-                               case 6: {
-                                       af = AF_INET6;
-                                       break;
-                               }
-                               default: {
-                                       printf("utun_kpipe_sync_rx %s: unknown ip version %u vhl %u header_size %zu\n",
-                                                  pcb->utun_ifp->if_xname, ip_version, vhl, UTUN_HEADER_SIZE(pcb));
-                                       break;
-                               }
+                       case 4: {
+                               af = AF_INET;
+                               break;
+                       }
+                       case 6: {
+                               af = AF_INET6;
+                               break;
+                       }
+                       default: {
+                               printf("utun_kpipe_sync_rx %s: unknown ip version %u vhl %u header_size %zu\n",
+                                   pcb->utun_ifp->if_xname, ip_version, vhl, UTUN_HEADER_SIZE(pcb));
+                               break;
+                       }
                        }
 
                        // Copy header
@@ -3101,7 +3233,7 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                struct mbuf *mb_tail = NULL;
 
                if (ifnet_dequeue_multi(pcb->utun_ifp, avail, &mb_head,
-                                                               &mb_tail, &mb_cnt, &mb_len) != 0) {
+                   &mb_tail, &mb_cnt, &mb_len) != 0) {
                        return 0;
                }
                VERIFY(mb_cnt <= avail);
@@ -3123,8 +3255,8 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        --mb_cnt;
                        length = mbuf_pkthdr_len(data);
                        if (length < UTUN_HEADER_SIZE(pcb) ||
-                               length > pcb->utun_slot_size ||
-                               (pcb->utun_flags & UTUN_FLAGS_NO_OUTPUT)) {
+                           length > pcb->utun_slot_size ||
+                           (pcb->utun_flags & UTUN_FLAGS_NO_OUTPUT)) {
                                /* flush data */
                                mbuf_freem(data);
                                continue;
@@ -3134,9 +3266,9 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
                        // Allocate rx packet
                        kern_packet_t rx_ph = 0;
                        errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
-                       if (unlikely(error != 0)) {
+                       if (__improbable(error != 0)) {
                                printf("utun_kpipe_sync_rx %s: failed to allocate packet\n",
-                                          pcb->utun_ifp->if_xname);
+                                   pcb->utun_ifp->if_xname);
                                break;
                        }
 
@@ -3153,7 +3285,7 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 
                        // Copy-in data from mbuf to buflet
                        mbuf_copydata(data, 0, length, (void *)rx_baddr);
-                       kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
+                       kern_packet_clear_flow_uuid(rx_ph);     // Zero flow id
 
                        // Finalize and attach the packet
                        error = kern_buflet_set_data_offset(rx_buf, 0);
@@ -3196,12 +3328,12 @@ utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
 /*
  * These are place holders until coreTLS kext stops calling them
  */
-errno_t utun_ctl_register_dtls (void *reg);
+errno_t utun_ctl_register_dtls(void *reg);
 int utun_pkt_dtls_input(struct utun_pcb *pcb, mbuf_t *pkt, protocol_family_t family);
 void utun_ctl_disable_crypto_dtls(struct utun_pcb   *pcb);
 
 errno_t
-utun_ctl_register_dtls (void *reg)
+utun_ctl_register_dtls(void *reg)
 {
 #pragma unused(reg)
        return 0;