1b2e60773SJohn Baldwin /*- 2b2e60773SJohn Baldwin * SPDX-License-Identifier: BSD-2-Clause 3b2e60773SJohn Baldwin * 4b2e60773SJohn Baldwin * Copyright (c) 2014-2019 Netflix Inc. 5b2e60773SJohn Baldwin * 6b2e60773SJohn Baldwin * Redistribution and use in source and binary forms, with or without 7b2e60773SJohn Baldwin * modification, are permitted provided that the following conditions 8b2e60773SJohn Baldwin * are met: 9b2e60773SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 10b2e60773SJohn Baldwin * notice, this list of conditions and the following disclaimer. 11b2e60773SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 12b2e60773SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 13b2e60773SJohn Baldwin * documentation and/or other materials provided with the distribution. 14b2e60773SJohn Baldwin * 15b2e60773SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16b2e60773SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b2e60773SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b2e60773SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19b2e60773SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20b2e60773SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21b2e60773SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22b2e60773SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23b2e60773SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24b2e60773SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25b2e60773SJohn Baldwin * SUCH DAMAGE. 26b2e60773SJohn Baldwin */ 27b2e60773SJohn Baldwin 28b2e60773SJohn Baldwin #include <sys/cdefs.h> 29b2e60773SJohn Baldwin __FBSDID("$FreeBSD$"); 30b2e60773SJohn Baldwin 31b2e60773SJohn Baldwin #include "opt_inet.h" 32b2e60773SJohn Baldwin #include "opt_inet6.h" 3328d0a740SAndrew Gallatin #include "opt_kern_tls.h" 34ed5e13cfSAndrew Gallatin #include "opt_ratelimit.h" 35b2e60773SJohn Baldwin #include "opt_rss.h" 36b2e60773SJohn Baldwin 37b2e60773SJohn Baldwin #include <sys/param.h> 38b2e60773SJohn Baldwin #include <sys/kernel.h> 3902bc3865SAndrew Gallatin #include <sys/domainset.h> 40470e851cSJohn Baldwin #include <sys/endian.h> 41b2e60773SJohn Baldwin #include <sys/ktls.h> 42b2e60773SJohn Baldwin #include <sys/lock.h> 43b2e60773SJohn Baldwin #include <sys/mbuf.h> 44b2e60773SJohn Baldwin #include <sys/mutex.h> 45b2e60773SJohn Baldwin #include <sys/rmlock.h> 46b2e60773SJohn Baldwin #include <sys/proc.h> 47b2e60773SJohn Baldwin #include <sys/protosw.h> 48b2e60773SJohn Baldwin #include <sys/refcount.h> 49b2e60773SJohn Baldwin #include <sys/smp.h> 50b2e60773SJohn Baldwin #include <sys/socket.h> 51b2e60773SJohn Baldwin #include <sys/socketvar.h> 52b2e60773SJohn Baldwin #include <sys/sysctl.h> 53b2e60773SJohn Baldwin #include <sys/taskqueue.h> 54b2e60773SJohn Baldwin #include <sys/kthread.h> 55b2e60773SJohn Baldwin #include <sys/uio.h> 56b2e60773SJohn Baldwin #include <sys/vmmeter.h> 57b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 58b2e60773SJohn Baldwin #include <machine/pcb.h> 59b2e60773SJohn Baldwin #endif 60b2e60773SJohn Baldwin #include <machine/vmparam.h> 6190746943SGleb Smirnoff #include <net/if.h> 6290746943SGleb Smirnoff #include <net/if_var.h> 63b2e60773SJohn Baldwin #ifdef RSS 64b2e60773SJohn Baldwin #include <net/netisr.h> 65b2e60773SJohn Baldwin #include <net/rss_config.h> 66b2e60773SJohn Baldwin #endif 67454d3896SAlexander V. Chernikov #include <net/route.h> 68454d3896SAlexander V. Chernikov #include <net/route/nhop.h> 69b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 70b2e60773SJohn Baldwin #include <netinet/in.h> 71b2e60773SJohn Baldwin #include <netinet/in_pcb.h> 72b2e60773SJohn Baldwin #endif 73b2e60773SJohn Baldwin #include <netinet/tcp_var.h> 749e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 759e14430dSJohn Baldwin #include <netinet/tcp_offload.h> 769e14430dSJohn Baldwin #endif 77470e851cSJohn Baldwin #include <opencrypto/cryptodev.h> 78470e851cSJohn Baldwin #include <opencrypto/ktls.h> 79b2e60773SJohn Baldwin #include <vm/uma_dbg.h> 80b2e60773SJohn Baldwin #include <vm/vm.h> 81b2e60773SJohn Baldwin #include <vm/vm_pageout.h> 82b2e60773SJohn Baldwin #include <vm/vm_page.h> 8398215005SAndrew Gallatin #include <vm/vm_pagequeue.h> 84b2e60773SJohn Baldwin 85b2e60773SJohn Baldwin struct ktls_wq { 86b2e60773SJohn Baldwin struct mtx mtx; 873c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) m_head; 883c0e5685SJohn Baldwin STAILQ_HEAD(, socket) so_head; 89b2e60773SJohn Baldwin bool running; 9049f6925cSMark Johnston int lastallocfail; 91b2e60773SJohn Baldwin } __aligned(CACHE_LINE_SIZE); 92b2e60773SJohn Baldwin 9398215005SAndrew Gallatin struct ktls_alloc_thread { 9498215005SAndrew Gallatin uint64_t wakeups; 9598215005SAndrew Gallatin uint64_t allocs; 9698215005SAndrew Gallatin struct thread *td; 9798215005SAndrew Gallatin int running; 9898215005SAndrew Gallatin }; 9998215005SAndrew Gallatin 10002bc3865SAndrew Gallatin struct ktls_domain_info { 10102bc3865SAndrew Gallatin int count; 10202bc3865SAndrew Gallatin int cpu[MAXCPU]; 10398215005SAndrew Gallatin struct ktls_alloc_thread alloc_td; 10402bc3865SAndrew Gallatin }; 10502bc3865SAndrew Gallatin 10602bc3865SAndrew Gallatin struct ktls_domain_info ktls_domains[MAXMEMDOM]; 107b2e60773SJohn Baldwin static struct ktls_wq *ktls_wq; 108b2e60773SJohn Baldwin static struct proc *ktls_proc; 109b2e60773SJohn Baldwin static uma_zone_t ktls_session_zone; 11049f6925cSMark Johnston static uma_zone_t ktls_buffer_zone; 111b2e60773SJohn Baldwin static uint16_t ktls_cpuid_lookup[MAXCPU]; 112a72ee355SJohn Baldwin static int ktls_init_state; 113a72ee355SJohn Baldwin static struct sx ktls_init_lock; 114a72ee355SJohn Baldwin SX_SYSINIT(ktls_init_lock, &ktls_init_lock, "ktls init"); 115b2e60773SJohn Baldwin 1167029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 117b2e60773SJohn Baldwin "Kernel TLS offload"); 1187029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 119b2e60773SJohn Baldwin "Kernel TLS offload stats"); 120b2e60773SJohn Baldwin 121b2e60773SJohn Baldwin #ifdef RSS 122b2e60773SJohn Baldwin static int ktls_bind_threads = 1; 123b2e60773SJohn Baldwin #else 124b2e60773SJohn Baldwin static int ktls_bind_threads; 125b2e60773SJohn Baldwin #endif 126b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN, 127b2e60773SJohn Baldwin &ktls_bind_threads, 0, 1284dc1b17dSMark Johnston "Bind crypto threads to cores (1) or cores and domains (2) at boot"); 129b2e60773SJohn Baldwin 130b2e60773SJohn Baldwin static u_int ktls_maxlen = 16384; 13149f6925cSMark Johnston SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RDTUN, 132b2e60773SJohn Baldwin &ktls_maxlen, 0, "Maximum TLS record size"); 133b2e60773SJohn Baldwin 134b2e60773SJohn Baldwin static int ktls_number_threads; 135b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD, 136b2e60773SJohn Baldwin &ktls_number_threads, 0, 137b2e60773SJohn Baldwin "Number of TLS threads in thread-pool"); 138b2e60773SJohn Baldwin 13928d0a740SAndrew Gallatin unsigned int ktls_ifnet_max_rexmit_pct = 2; 14028d0a740SAndrew Gallatin SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, ifnet_max_rexmit_pct, CTLFLAG_RWTUN, 14128d0a740SAndrew Gallatin &ktls_ifnet_max_rexmit_pct, 2, 14228d0a740SAndrew Gallatin "Max percent bytes retransmitted before ifnet TLS is disabled"); 14328d0a740SAndrew Gallatin 144b2e60773SJohn Baldwin static bool ktls_offload_enable; 145b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RWTUN, 146b2e60773SJohn Baldwin &ktls_offload_enable, 0, 147b2e60773SJohn Baldwin "Enable support for kernel TLS offload"); 148b2e60773SJohn Baldwin 149b2e60773SJohn Baldwin static bool ktls_cbc_enable = true; 150b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RWTUN, 151b2e60773SJohn Baldwin &ktls_cbc_enable, 1, 152b2e60773SJohn Baldwin "Enable Support of AES-CBC crypto for kernel TLS"); 153b2e60773SJohn Baldwin 15449f6925cSMark Johnston static bool ktls_sw_buffer_cache = true; 15549f6925cSMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, sw_buffer_cache, CTLFLAG_RDTUN, 15649f6925cSMark Johnston &ktls_sw_buffer_cache, 1, 15749f6925cSMark Johnston "Enable caching of output buffers for SW encryption"); 15849f6925cSMark Johnston 15998215005SAndrew Gallatin static int ktls_max_alloc = 128; 16098215005SAndrew Gallatin SYSCTL_INT(_kern_ipc_tls, OID_AUTO, max_alloc, CTLFLAG_RWTUN, 16198215005SAndrew Gallatin &ktls_max_alloc, 128, 16298215005SAndrew Gallatin "Max number of 16k buffers to allocate in thread context"); 16398215005SAndrew Gallatin 1641755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_tasks_active); 165b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD, 166b2e60773SJohn Baldwin &ktls_tasks_active, "Number of active tasks"); 167b2e60773SJohn Baldwin 1689f03d2c0SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_pending); 1699f03d2c0SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_pending, CTLFLAG_RD, 1709f03d2c0SJohn Baldwin &ktls_cnt_tx_pending, 1719f03d2c0SJohn Baldwin "Number of TLS 1.0 records waiting for earlier TLS records"); 1729f03d2c0SJohn Baldwin 1731755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_queued); 1743c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_inqueue, CTLFLAG_RD, 1753c0e5685SJohn Baldwin &ktls_cnt_tx_queued, 1763c0e5685SJohn Baldwin "Number of TLS records in queue to tasks for SW encryption"); 1773c0e5685SJohn Baldwin 1781755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_rx_queued); 1793c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_rx_inqueue, CTLFLAG_RD, 1803c0e5685SJohn Baldwin &ktls_cnt_rx_queued, 1813c0e5685SJohn Baldwin "Number of TLS sockets in queue to tasks for SW decryption"); 182b2e60773SJohn Baldwin 1831755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_total); 184b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total, 185b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_total, 186b2e60773SJohn Baldwin "Total successful TLS setups (parameters set)"); 187b2e60773SJohn Baldwin 1881755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_enable_calls); 189b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls, 190b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_enable_calls, 191b2e60773SJohn Baldwin "Total number of TLS enable calls made"); 192b2e60773SJohn Baldwin 1931755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_active); 194b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD, 195b2e60773SJohn Baldwin &ktls_offload_active, "Total Active TLS sessions"); 196b2e60773SJohn Baldwin 1971755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_corrupted_records); 1983c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, corrupted_records, CTLFLAG_RD, 1993c0e5685SJohn Baldwin &ktls_offload_corrupted_records, "Total corrupted TLS records received"); 2003c0e5685SJohn Baldwin 2011755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_failed_crypto); 202b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD, 203b2e60773SJohn Baldwin &ktls_offload_failed_crypto, "Total TLS crypto failures"); 204b2e60773SJohn Baldwin 2051755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_ifnet); 206b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD, 207b2e60773SJohn Baldwin &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet"); 208b2e60773SJohn Baldwin 2091755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_sw); 210b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD, 211b2e60773SJohn Baldwin &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW"); 212b2e60773SJohn Baldwin 2131755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_failed); 214b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD, 215b2e60773SJohn Baldwin &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet"); 216b2e60773SJohn Baldwin 21728d0a740SAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_fail); 21828d0a740SAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_failed, CTLFLAG_RD, 21928d0a740SAndrew Gallatin &ktls_ifnet_disable_fail, "TLS sessions unable to switch to SW from ifnet"); 22028d0a740SAndrew Gallatin 22128d0a740SAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_ok); 22228d0a740SAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_ok, CTLFLAG_RD, 22328d0a740SAndrew Gallatin &ktls_ifnet_disable_ok, "TLS sessions able to switch to SW from ifnet"); 22428d0a740SAndrew Gallatin 2257029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 226b2e60773SJohn Baldwin "Software TLS session stats"); 2277029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 228b2e60773SJohn Baldwin "Hardware (ifnet) TLS session stats"); 2299e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 2319e14430dSJohn Baldwin "TOE TLS session stats"); 2329e14430dSJohn Baldwin #endif 233b2e60773SJohn Baldwin 2341755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc); 235b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc, 236b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-CBC"); 237b2e60773SJohn Baldwin 2381755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm); 239b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm, 240b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-GCM"); 241b2e60773SJohn Baldwin 2429c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20); 2439c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD, 2449c64fc40SJohn Baldwin &ktls_sw_chacha20, 2459c64fc40SJohn Baldwin "Active number of software TLS sessions using Chacha20-Poly1305"); 2469c64fc40SJohn Baldwin 2471755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc); 248b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD, 249b2e60773SJohn Baldwin &ktls_ifnet_cbc, 250b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-CBC"); 251b2e60773SJohn Baldwin 2521755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm); 253b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD, 254b2e60773SJohn Baldwin &ktls_ifnet_gcm, 255b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-GCM"); 256b2e60773SJohn Baldwin 2579c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20); 2589c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD, 2599c64fc40SJohn Baldwin &ktls_ifnet_chacha20, 2609c64fc40SJohn Baldwin "Active number of ifnet TLS sessions using Chacha20-Poly1305"); 2619c64fc40SJohn Baldwin 2621755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset); 263b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD, 264b2e60773SJohn Baldwin &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag"); 265b2e60773SJohn Baldwin 2661755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped); 267b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD, 268b2e60773SJohn Baldwin &ktls_ifnet_reset_dropped, 269b2e60773SJohn Baldwin "TLS sessions dropped after failing to update ifnet send tag"); 270b2e60773SJohn Baldwin 2711755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed); 272b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD, 273b2e60773SJohn Baldwin &ktls_ifnet_reset_failed, 274b2e60773SJohn Baldwin "TLS sessions that failed to allocate a new ifnet send tag"); 275b2e60773SJohn Baldwin 276b2e60773SJohn Baldwin static int ktls_ifnet_permitted; 277b2e60773SJohn Baldwin SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN, 278b2e60773SJohn Baldwin &ktls_ifnet_permitted, 1, 279b2e60773SJohn Baldwin "Whether to permit hardware (ifnet) TLS sessions"); 280b2e60773SJohn Baldwin 2819e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2821755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc); 2839e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD, 2849e14430dSJohn Baldwin &ktls_toe_cbc, 2859e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-CBC"); 2869e14430dSJohn Baldwin 2871755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm); 2889e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD, 2899e14430dSJohn Baldwin &ktls_toe_gcm, 2909e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-GCM"); 2919c64fc40SJohn Baldwin 29290972f04SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20); 2939c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD, 2949c64fc40SJohn Baldwin &ktls_toe_chacha20, 2959c64fc40SJohn Baldwin "Active number of TOE TLS sessions using Chacha20-Poly1305"); 2969e14430dSJohn Baldwin #endif 2979e14430dSJohn Baldwin 298b2e60773SJohn Baldwin static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS"); 299b2e60773SJohn Baldwin 300b2e60773SJohn Baldwin static void ktls_cleanup(struct ktls_session *tls); 301b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 302fe8c78f0SHans Petter Selasky static void ktls_reset_receive_tag(void *context, int pending); 303b2e60773SJohn Baldwin static void ktls_reset_send_tag(void *context, int pending); 304b2e60773SJohn Baldwin #endif 305b2e60773SJohn Baldwin static void ktls_work_thread(void *ctx); 30698215005SAndrew Gallatin static void ktls_alloc_thread(void *ctx); 307b2e60773SJohn Baldwin 308b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 309a2fba2a7SBjoern A. Zeeb static u_int 310b2e60773SJohn Baldwin ktls_get_cpu(struct socket *so) 311b2e60773SJohn Baldwin { 312b2e60773SJohn Baldwin struct inpcb *inp; 31302bc3865SAndrew Gallatin #ifdef NUMA 31402bc3865SAndrew Gallatin struct ktls_domain_info *di; 31502bc3865SAndrew Gallatin #endif 316a2fba2a7SBjoern A. Zeeb u_int cpuid; 317b2e60773SJohn Baldwin 318b2e60773SJohn Baldwin inp = sotoinpcb(so); 319b2e60773SJohn Baldwin #ifdef RSS 320b2e60773SJohn Baldwin cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype); 321b2e60773SJohn Baldwin if (cpuid != NETISR_CPUID_NONE) 322b2e60773SJohn Baldwin return (cpuid); 323b2e60773SJohn Baldwin #endif 324b2e60773SJohn Baldwin /* 325b2e60773SJohn Baldwin * Just use the flowid to shard connections in a repeatable 32621e3c1fbSJohn Baldwin * fashion. Note that TLS 1.0 sessions rely on the 327b2e60773SJohn Baldwin * serialization provided by having the same connection use 328b2e60773SJohn Baldwin * the same queue. 329b2e60773SJohn Baldwin */ 33002bc3865SAndrew Gallatin #ifdef NUMA 33102bc3865SAndrew Gallatin if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) { 33202bc3865SAndrew Gallatin di = &ktls_domains[inp->inp_numa_domain]; 33302bc3865SAndrew Gallatin cpuid = di->cpu[inp->inp_flowid % di->count]; 33402bc3865SAndrew Gallatin } else 33502bc3865SAndrew Gallatin #endif 336b2e60773SJohn Baldwin cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads]; 337b2e60773SJohn Baldwin return (cpuid); 338b2e60773SJohn Baldwin } 339b2e60773SJohn Baldwin #endif 340b2e60773SJohn Baldwin 34149f6925cSMark Johnston static int 34249f6925cSMark Johnston ktls_buffer_import(void *arg, void **store, int count, int domain, int flags) 34349f6925cSMark Johnston { 34449f6925cSMark Johnston vm_page_t m; 34584c39222SMark Johnston int i, req; 34649f6925cSMark Johnston 34749f6925cSMark Johnston KASSERT((ktls_maxlen & PAGE_MASK) == 0, 34849f6925cSMark Johnston ("%s: ktls max length %d is not page size-aligned", 34949f6925cSMark Johnston __func__, ktls_maxlen)); 35049f6925cSMark Johnston 35184c39222SMark Johnston req = VM_ALLOC_WIRED | VM_ALLOC_NODUMP | malloc2vm_flags(flags); 35249f6925cSMark Johnston for (i = 0; i < count; i++) { 35384c39222SMark Johnston m = vm_page_alloc_noobj_contig_domain(domain, req, 35449f6925cSMark Johnston atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0, 35549f6925cSMark Johnston VM_MEMATTR_DEFAULT); 35649f6925cSMark Johnston if (m == NULL) 35749f6925cSMark Johnston break; 35849f6925cSMark Johnston store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 35949f6925cSMark Johnston } 36049f6925cSMark Johnston return (i); 36149f6925cSMark Johnston } 36249f6925cSMark Johnston 36349f6925cSMark Johnston static void 36449f6925cSMark Johnston ktls_buffer_release(void *arg __unused, void **store, int count) 36549f6925cSMark Johnston { 36649f6925cSMark Johnston vm_page_t m; 36749f6925cSMark Johnston int i, j; 36849f6925cSMark Johnston 36949f6925cSMark Johnston for (i = 0; i < count; i++) { 37049f6925cSMark Johnston m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i])); 37149f6925cSMark Johnston for (j = 0; j < atop(ktls_maxlen); j++) { 37249f6925cSMark Johnston (void)vm_page_unwire_noq(m + j); 37349f6925cSMark Johnston vm_page_free(m + j); 37449f6925cSMark Johnston } 37549f6925cSMark Johnston } 37649f6925cSMark Johnston } 37749f6925cSMark Johnston 37849f6925cSMark Johnston static void 37949f6925cSMark Johnston ktls_free_mext_contig(struct mbuf *m) 38049f6925cSMark Johnston { 38149f6925cSMark Johnston M_ASSERTEXTPG(m); 38249f6925cSMark Johnston uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0])); 38349f6925cSMark Johnston } 38449f6925cSMark Johnston 385a72ee355SJohn Baldwin static int 386a72ee355SJohn Baldwin ktls_init(void) 387b2e60773SJohn Baldwin { 388b2e60773SJohn Baldwin struct thread *td; 389b2e60773SJohn Baldwin struct pcpu *pc; 39002bc3865SAndrew Gallatin int count, domain, error, i; 391b2e60773SJohn Baldwin 392b2e60773SJohn Baldwin ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS, 393b2e60773SJohn Baldwin M_WAITOK | M_ZERO); 394b2e60773SJohn Baldwin 395b2e60773SJohn Baldwin ktls_session_zone = uma_zcreate("ktls_session", 396b2e60773SJohn Baldwin sizeof(struct ktls_session), 397b2e60773SJohn Baldwin NULL, NULL, NULL, NULL, 398b2e60773SJohn Baldwin UMA_ALIGN_CACHE, 0); 399b2e60773SJohn Baldwin 40049f6925cSMark Johnston if (ktls_sw_buffer_cache) { 40149f6925cSMark Johnston ktls_buffer_zone = uma_zcache_create("ktls_buffers", 40249f6925cSMark Johnston roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL, 40349f6925cSMark Johnston ktls_buffer_import, ktls_buffer_release, NULL, 40449f6925cSMark Johnston UMA_ZONE_FIRSTTOUCH); 40549f6925cSMark Johnston } 40649f6925cSMark Johnston 407b2e60773SJohn Baldwin /* 408b2e60773SJohn Baldwin * Initialize the workqueues to run the TLS work. We create a 409b2e60773SJohn Baldwin * work queue for each CPU. 410b2e60773SJohn Baldwin */ 411b2e60773SJohn Baldwin CPU_FOREACH(i) { 4123c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].m_head); 4133c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].so_head); 414b2e60773SJohn Baldwin mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF); 415b2e60773SJohn Baldwin if (ktls_bind_threads > 1) { 416b2e60773SJohn Baldwin pc = pcpu_find(i); 41702bc3865SAndrew Gallatin domain = pc->pc_domain; 41802bc3865SAndrew Gallatin count = ktls_domains[domain].count; 41902bc3865SAndrew Gallatin ktls_domains[domain].cpu[count] = i; 42002bc3865SAndrew Gallatin ktls_domains[domain].count++; 421b2e60773SJohn Baldwin } 422b2e60773SJohn Baldwin ktls_cpuid_lookup[ktls_number_threads] = i; 423b2e60773SJohn Baldwin ktls_number_threads++; 424b2e60773SJohn Baldwin } 42502bc3865SAndrew Gallatin 42602bc3865SAndrew Gallatin /* 427a72ee355SJohn Baldwin * If we somehow have an empty domain, fall back to choosing 428a72ee355SJohn Baldwin * among all KTLS threads. 429a72ee355SJohn Baldwin */ 430a72ee355SJohn Baldwin if (ktls_bind_threads > 1) { 431a72ee355SJohn Baldwin for (i = 0; i < vm_ndomains; i++) { 432a72ee355SJohn Baldwin if (ktls_domains[i].count == 0) { 433a72ee355SJohn Baldwin ktls_bind_threads = 1; 434a72ee355SJohn Baldwin break; 435a72ee355SJohn Baldwin } 436a72ee355SJohn Baldwin } 437a72ee355SJohn Baldwin } 438a72ee355SJohn Baldwin 439a72ee355SJohn Baldwin /* Start kthreads for each workqueue. */ 440a72ee355SJohn Baldwin CPU_FOREACH(i) { 441a72ee355SJohn Baldwin error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i], 442a72ee355SJohn Baldwin &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i); 443a72ee355SJohn Baldwin if (error) { 444a72ee355SJohn Baldwin printf("Can't add KTLS thread %d error %d\n", i, error); 445a72ee355SJohn Baldwin return (error); 446a72ee355SJohn Baldwin } 447a72ee355SJohn Baldwin } 448a72ee355SJohn Baldwin 449a72ee355SJohn Baldwin /* 45098215005SAndrew Gallatin * Start an allocation thread per-domain to perform blocking allocations 45198215005SAndrew Gallatin * of 16k physically contiguous TLS crypto destination buffers. 45298215005SAndrew Gallatin */ 45398215005SAndrew Gallatin if (ktls_sw_buffer_cache) { 45498215005SAndrew Gallatin for (domain = 0; domain < vm_ndomains; domain++) { 45598215005SAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 45698215005SAndrew Gallatin continue; 45798215005SAndrew Gallatin if (CPU_EMPTY(&cpuset_domain[domain])) 45898215005SAndrew Gallatin continue; 45998215005SAndrew Gallatin error = kproc_kthread_add(ktls_alloc_thread, 46098215005SAndrew Gallatin &ktls_domains[domain], &ktls_proc, 46198215005SAndrew Gallatin &ktls_domains[domain].alloc_td.td, 46298215005SAndrew Gallatin 0, 0, "KTLS", "alloc_%d", domain); 463a72ee355SJohn Baldwin if (error) { 464a72ee355SJohn Baldwin printf("Can't add KTLS alloc thread %d error %d\n", 46598215005SAndrew Gallatin domain, error); 466a72ee355SJohn Baldwin return (error); 46702bc3865SAndrew Gallatin } 46802bc3865SAndrew Gallatin } 4694dc1b17dSMark Johnston } 47002bc3865SAndrew Gallatin 47189b65087SMark Johnston if (bootverbose) 472b2e60773SJohn Baldwin printf("KTLS: Initialized %d threads\n", ktls_number_threads); 473a72ee355SJohn Baldwin return (0); 474b2e60773SJohn Baldwin } 475a72ee355SJohn Baldwin 476a72ee355SJohn Baldwin static int 477a72ee355SJohn Baldwin ktls_start_kthreads(void) 478a72ee355SJohn Baldwin { 479a72ee355SJohn Baldwin int error, state; 480a72ee355SJohn Baldwin 481a72ee355SJohn Baldwin start: 482a72ee355SJohn Baldwin state = atomic_load_acq_int(&ktls_init_state); 483a72ee355SJohn Baldwin if (__predict_true(state > 0)) 484a72ee355SJohn Baldwin return (0); 485a72ee355SJohn Baldwin if (state < 0) 486a72ee355SJohn Baldwin return (ENXIO); 487a72ee355SJohn Baldwin 488a72ee355SJohn Baldwin sx_xlock(&ktls_init_lock); 489a72ee355SJohn Baldwin if (ktls_init_state != 0) { 490a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 491a72ee355SJohn Baldwin goto start; 492a72ee355SJohn Baldwin } 493a72ee355SJohn Baldwin 494a72ee355SJohn Baldwin error = ktls_init(); 495a72ee355SJohn Baldwin if (error == 0) 496a72ee355SJohn Baldwin state = 1; 497a72ee355SJohn Baldwin else 498a72ee355SJohn Baldwin state = -1; 499a72ee355SJohn Baldwin atomic_store_rel_int(&ktls_init_state, state); 500a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 501a72ee355SJohn Baldwin return (error); 502a72ee355SJohn Baldwin } 503b2e60773SJohn Baldwin 504b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 505b2e60773SJohn Baldwin static int 506b2e60773SJohn Baldwin ktls_create_session(struct socket *so, struct tls_enable *en, 507fe8c78f0SHans Petter Selasky struct ktls_session **tlsp, int direction) 508b2e60773SJohn Baldwin { 509b2e60773SJohn Baldwin struct ktls_session *tls; 510b2e60773SJohn Baldwin int error; 511b2e60773SJohn Baldwin 5127d29eb9aSJohn Baldwin /* Only TLS 1.0 - 1.3 are supported. */ 513b2e60773SJohn Baldwin if (en->tls_vmajor != TLS_MAJOR_VER_ONE) 514b2e60773SJohn Baldwin return (EINVAL); 515b2e60773SJohn Baldwin if (en->tls_vminor < TLS_MINOR_VER_ZERO || 5166554362cSAndrew Gallatin en->tls_vminor > TLS_MINOR_VER_THREE) 517b2e60773SJohn Baldwin return (EINVAL); 518b2e60773SJohn Baldwin 519b2e60773SJohn Baldwin if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE) 520b2e60773SJohn Baldwin return (EINVAL); 521b2e60773SJohn Baldwin if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE) 522b2e60773SJohn Baldwin return (EINVAL); 5236554362cSAndrew Gallatin if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv)) 524b2e60773SJohn Baldwin return (EINVAL); 525b2e60773SJohn Baldwin 526b2e60773SJohn Baldwin /* All supported algorithms require a cipher key. */ 527b2e60773SJohn Baldwin if (en->cipher_key_len == 0) 528b2e60773SJohn Baldwin return (EINVAL); 529b2e60773SJohn Baldwin 530b2e60773SJohn Baldwin /* No flags are currently supported. */ 531b2e60773SJohn Baldwin if (en->flags != 0) 532b2e60773SJohn Baldwin return (EINVAL); 533b2e60773SJohn Baldwin 534b2e60773SJohn Baldwin /* Common checks for supported algorithms. */ 535b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 536b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 537b2e60773SJohn Baldwin /* 538b2e60773SJohn Baldwin * auth_algorithm isn't used, but permit GMAC values 539b2e60773SJohn Baldwin * for compatibility. 540b2e60773SJohn Baldwin */ 541b2e60773SJohn Baldwin switch (en->auth_algorithm) { 542b2e60773SJohn Baldwin case 0: 543c0341432SJohn Baldwin #ifdef COMPAT_FREEBSD12 544c0341432SJohn Baldwin /* XXX: Really 13.0-current COMPAT. */ 545b2e60773SJohn Baldwin case CRYPTO_AES_128_NIST_GMAC: 546b2e60773SJohn Baldwin case CRYPTO_AES_192_NIST_GMAC: 547b2e60773SJohn Baldwin case CRYPTO_AES_256_NIST_GMAC: 548c0341432SJohn Baldwin #endif 549b2e60773SJohn Baldwin break; 550b2e60773SJohn Baldwin default: 551b2e60773SJohn Baldwin return (EINVAL); 552b2e60773SJohn Baldwin } 553b2e60773SJohn Baldwin if (en->auth_key_len != 0) 554b2e60773SJohn Baldwin return (EINVAL); 555900a28feSJohn Baldwin switch (en->tls_vminor) { 556900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 557900a28feSJohn Baldwin if (en->iv_len != TLS_AEAD_GCM_LEN) 558b2e60773SJohn Baldwin return (EINVAL); 559b2e60773SJohn Baldwin break; 560900a28feSJohn Baldwin case TLS_MINOR_VER_THREE: 561900a28feSJohn Baldwin if (en->iv_len != TLS_1_3_GCM_IV_LEN) 562900a28feSJohn Baldwin return (EINVAL); 563900a28feSJohn Baldwin break; 564900a28feSJohn Baldwin default: 565900a28feSJohn Baldwin return (EINVAL); 566900a28feSJohn Baldwin } 567900a28feSJohn Baldwin break; 568b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 569b2e60773SJohn Baldwin switch (en->auth_algorithm) { 570b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 571b2e60773SJohn Baldwin break; 572b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 573b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 574900a28feSJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO) 575900a28feSJohn Baldwin return (EINVAL); 576b2e60773SJohn Baldwin break; 577b2e60773SJohn Baldwin default: 578b2e60773SJohn Baldwin return (EINVAL); 579b2e60773SJohn Baldwin } 580b2e60773SJohn Baldwin if (en->auth_key_len == 0) 581b2e60773SJohn Baldwin return (EINVAL); 582900a28feSJohn Baldwin 583900a28feSJohn Baldwin /* 584900a28feSJohn Baldwin * TLS 1.0 requires an implicit IV. TLS 1.1 and 1.2 585900a28feSJohn Baldwin * use explicit IVs. 586900a28feSJohn Baldwin */ 587900a28feSJohn Baldwin switch (en->tls_vminor) { 588900a28feSJohn Baldwin case TLS_MINOR_VER_ZERO: 589900a28feSJohn Baldwin if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN) 590a63752ccSJohn Baldwin return (EINVAL); 591b2e60773SJohn Baldwin break; 592900a28feSJohn Baldwin case TLS_MINOR_VER_ONE: 593900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 594900a28feSJohn Baldwin /* Ignore any supplied IV. */ 595900a28feSJohn Baldwin en->iv_len = 0; 596900a28feSJohn Baldwin break; 597900a28feSJohn Baldwin default: 598900a28feSJohn Baldwin return (EINVAL); 599900a28feSJohn Baldwin } 600900a28feSJohn Baldwin break; 6019c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6029c64fc40SJohn Baldwin if (en->auth_algorithm != 0 || en->auth_key_len != 0) 6039c64fc40SJohn Baldwin return (EINVAL); 6049c64fc40SJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO && 6059c64fc40SJohn Baldwin en->tls_vminor != TLS_MINOR_VER_THREE) 6069c64fc40SJohn Baldwin return (EINVAL); 6079c64fc40SJohn Baldwin if (en->iv_len != TLS_CHACHA20_IV_LEN) 6089c64fc40SJohn Baldwin return (EINVAL); 6099c64fc40SJohn Baldwin break; 610b2e60773SJohn Baldwin default: 611b2e60773SJohn Baldwin return (EINVAL); 612b2e60773SJohn Baldwin } 613b2e60773SJohn Baldwin 614a72ee355SJohn Baldwin error = ktls_start_kthreads(); 615a72ee355SJohn Baldwin if (error != 0) 616a72ee355SJohn Baldwin return (error); 617a72ee355SJohn Baldwin 618b2e60773SJohn Baldwin tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 619b2e60773SJohn Baldwin 620b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 621b2e60773SJohn Baldwin 622b2e60773SJohn Baldwin refcount_init(&tls->refcount, 1); 623fe8c78f0SHans Petter Selasky if (direction == KTLS_RX) 624fe8c78f0SHans Petter Selasky TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_receive_tag, tls); 625fe8c78f0SHans Petter Selasky else 626b2e60773SJohn Baldwin TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls); 627b2e60773SJohn Baldwin 628b2e60773SJohn Baldwin tls->wq_index = ktls_get_cpu(so); 629b2e60773SJohn Baldwin 630b2e60773SJohn Baldwin tls->params.cipher_algorithm = en->cipher_algorithm; 631b2e60773SJohn Baldwin tls->params.auth_algorithm = en->auth_algorithm; 632b2e60773SJohn Baldwin tls->params.tls_vmajor = en->tls_vmajor; 633b2e60773SJohn Baldwin tls->params.tls_vminor = en->tls_vminor; 634b2e60773SJohn Baldwin tls->params.flags = en->flags; 635b2e60773SJohn Baldwin tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen); 636b2e60773SJohn Baldwin 637b2e60773SJohn Baldwin /* Set the header and trailer lengths. */ 638b2e60773SJohn Baldwin tls->params.tls_hlen = sizeof(struct tls_record_layer); 639b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 640b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 6416554362cSAndrew Gallatin /* 6426554362cSAndrew Gallatin * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte 6436554362cSAndrew Gallatin * nonce. TLS 1.3 uses a 12 byte implicit IV. 6446554362cSAndrew Gallatin */ 6456554362cSAndrew Gallatin if (en->tls_vminor < TLS_MINOR_VER_THREE) 6466554362cSAndrew Gallatin tls->params.tls_hlen += sizeof(uint64_t); 647b2e60773SJohn Baldwin tls->params.tls_tlen = AES_GMAC_HASH_LEN; 648b2e60773SJohn Baldwin tls->params.tls_bs = 1; 649b2e60773SJohn Baldwin break; 650b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 651b2e60773SJohn Baldwin switch (en->auth_algorithm) { 652b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 653b2e60773SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_ZERO) { 654b2e60773SJohn Baldwin /* Implicit IV, no nonce. */ 6559f03d2c0SJohn Baldwin tls->sequential_records = true; 6569f03d2c0SJohn Baldwin tls->next_seqno = be64dec(en->rec_seq); 6579f03d2c0SJohn Baldwin STAILQ_INIT(&tls->pending_records); 658b2e60773SJohn Baldwin } else { 659b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 660b2e60773SJohn Baldwin } 661b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 662b2e60773SJohn Baldwin SHA1_HASH_LEN; 663b2e60773SJohn Baldwin break; 664b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 665b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 666b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 667b2e60773SJohn Baldwin SHA2_256_HASH_LEN; 668b2e60773SJohn Baldwin break; 669b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 670b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 671b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 672b2e60773SJohn Baldwin SHA2_384_HASH_LEN; 673b2e60773SJohn Baldwin break; 674b2e60773SJohn Baldwin default: 675b2e60773SJohn Baldwin panic("invalid hmac"); 676b2e60773SJohn Baldwin } 677b2e60773SJohn Baldwin tls->params.tls_bs = AES_BLOCK_LEN; 678b2e60773SJohn Baldwin break; 6799c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6809c64fc40SJohn Baldwin /* 6819c64fc40SJohn Baldwin * Chacha20 uses a 12 byte implicit IV. 6829c64fc40SJohn Baldwin */ 6839c64fc40SJohn Baldwin tls->params.tls_tlen = POLY1305_HASH_LEN; 6849c64fc40SJohn Baldwin tls->params.tls_bs = 1; 6859c64fc40SJohn Baldwin break; 686b2e60773SJohn Baldwin default: 687b2e60773SJohn Baldwin panic("invalid cipher"); 688b2e60773SJohn Baldwin } 689b2e60773SJohn Baldwin 6909c64fc40SJohn Baldwin /* 6919c64fc40SJohn Baldwin * TLS 1.3 includes optional padding which we do not support, 6929c64fc40SJohn Baldwin * and also puts the "real" record type at the end of the 6939c64fc40SJohn Baldwin * encrypted data. 6949c64fc40SJohn Baldwin */ 6959c64fc40SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_THREE) 6969c64fc40SJohn Baldwin tls->params.tls_tlen += sizeof(uint8_t); 6979c64fc40SJohn Baldwin 698b2e60773SJohn Baldwin KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN, 699b2e60773SJohn Baldwin ("TLS header length too long: %d", tls->params.tls_hlen)); 700b2e60773SJohn Baldwin KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN, 701b2e60773SJohn Baldwin ("TLS trailer length too long: %d", tls->params.tls_tlen)); 702b2e60773SJohn Baldwin 703b2e60773SJohn Baldwin if (en->auth_key_len != 0) { 704b2e60773SJohn Baldwin tls->params.auth_key_len = en->auth_key_len; 705b2e60773SJohn Baldwin tls->params.auth_key = malloc(en->auth_key_len, M_KTLS, 706b2e60773SJohn Baldwin M_WAITOK); 707b2e60773SJohn Baldwin error = copyin(en->auth_key, tls->params.auth_key, 708b2e60773SJohn Baldwin en->auth_key_len); 709b2e60773SJohn Baldwin if (error) 710b2e60773SJohn Baldwin goto out; 711b2e60773SJohn Baldwin } 712b2e60773SJohn Baldwin 713b2e60773SJohn Baldwin tls->params.cipher_key_len = en->cipher_key_len; 714b2e60773SJohn Baldwin tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK); 715b2e60773SJohn Baldwin error = copyin(en->cipher_key, tls->params.cipher_key, 716b2e60773SJohn Baldwin en->cipher_key_len); 717b2e60773SJohn Baldwin if (error) 718b2e60773SJohn Baldwin goto out; 719b2e60773SJohn Baldwin 720b2e60773SJohn Baldwin /* 7219c64fc40SJohn Baldwin * This holds the implicit portion of the nonce for AEAD 7229c64fc40SJohn Baldwin * ciphers and the initial implicit IV for TLS 1.0. The 7239c64fc40SJohn Baldwin * explicit portions of the IV are generated in ktls_frame(). 724b2e60773SJohn Baldwin */ 725b2e60773SJohn Baldwin if (en->iv_len != 0) { 726b2e60773SJohn Baldwin tls->params.iv_len = en->iv_len; 727b2e60773SJohn Baldwin error = copyin(en->iv, tls->params.iv, en->iv_len); 728b2e60773SJohn Baldwin if (error) 729b2e60773SJohn Baldwin goto out; 7307d29eb9aSJohn Baldwin 7317d29eb9aSJohn Baldwin /* 7329c64fc40SJohn Baldwin * For TLS 1.2 with GCM, generate an 8-byte nonce as a 7339c64fc40SJohn Baldwin * counter to generate unique explicit IVs. 7347d29eb9aSJohn Baldwin * 7357d29eb9aSJohn Baldwin * Store this counter in the last 8 bytes of the IV 7367d29eb9aSJohn Baldwin * array so that it is 8-byte aligned. 7377d29eb9aSJohn Baldwin */ 7387d29eb9aSJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 7397d29eb9aSJohn Baldwin en->tls_vminor == TLS_MINOR_VER_TWO) 7407d29eb9aSJohn Baldwin arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0); 741b2e60773SJohn Baldwin } 742b2e60773SJohn Baldwin 743b2e60773SJohn Baldwin *tlsp = tls; 744b2e60773SJohn Baldwin return (0); 745b2e60773SJohn Baldwin 746b2e60773SJohn Baldwin out: 747b2e60773SJohn Baldwin ktls_cleanup(tls); 748b2e60773SJohn Baldwin return (error); 749b2e60773SJohn Baldwin } 750b2e60773SJohn Baldwin 751b2e60773SJohn Baldwin static struct ktls_session * 752fe8c78f0SHans Petter Selasky ktls_clone_session(struct ktls_session *tls, int direction) 753b2e60773SJohn Baldwin { 754b2e60773SJohn Baldwin struct ktls_session *tls_new; 755b2e60773SJohn Baldwin 756b2e60773SJohn Baldwin tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 757b2e60773SJohn Baldwin 758b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 759b2e60773SJohn Baldwin 760b2e60773SJohn Baldwin refcount_init(&tls_new->refcount, 1); 761fe8c78f0SHans Petter Selasky if (direction == KTLS_RX) 762fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_receive_tag, 763fe8c78f0SHans Petter Selasky tls_new); 764fe8c78f0SHans Petter Selasky else 765fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_send_tag, 766fe8c78f0SHans Petter Selasky tls_new); 767b2e60773SJohn Baldwin 768b2e60773SJohn Baldwin /* Copy fields from existing session. */ 769b2e60773SJohn Baldwin tls_new->params = tls->params; 770b2e60773SJohn Baldwin tls_new->wq_index = tls->wq_index; 771b2e60773SJohn Baldwin 772b2e60773SJohn Baldwin /* Deep copy keys. */ 773b2e60773SJohn Baldwin if (tls_new->params.auth_key != NULL) { 774b2e60773SJohn Baldwin tls_new->params.auth_key = malloc(tls->params.auth_key_len, 775b2e60773SJohn Baldwin M_KTLS, M_WAITOK); 776b2e60773SJohn Baldwin memcpy(tls_new->params.auth_key, tls->params.auth_key, 777b2e60773SJohn Baldwin tls->params.auth_key_len); 778b2e60773SJohn Baldwin } 779b2e60773SJohn Baldwin 780b2e60773SJohn Baldwin tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS, 781b2e60773SJohn Baldwin M_WAITOK); 782b2e60773SJohn Baldwin memcpy(tls_new->params.cipher_key, tls->params.cipher_key, 783b2e60773SJohn Baldwin tls->params.cipher_key_len); 784b2e60773SJohn Baldwin 785b2e60773SJohn Baldwin return (tls_new); 786b2e60773SJohn Baldwin } 787b2e60773SJohn Baldwin #endif 788b2e60773SJohn Baldwin 789b2e60773SJohn Baldwin static void 790b2e60773SJohn Baldwin ktls_cleanup(struct ktls_session *tls) 791b2e60773SJohn Baldwin { 792b2e60773SJohn Baldwin 793b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, -1); 7949e14430dSJohn Baldwin switch (tls->mode) { 7959e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 796b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 797b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 798b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, -1); 799b2e60773SJohn Baldwin break; 800b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 801b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, -1); 802b2e60773SJohn Baldwin break; 8039c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 8049c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, -1); 8059c64fc40SJohn Baldwin break; 806b2e60773SJohn Baldwin } 8079e14430dSJohn Baldwin break; 8089e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 809b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 810b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 811b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, -1); 812b2e60773SJohn Baldwin break; 813b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 814b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, -1); 815b2e60773SJohn Baldwin break; 8169c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 8179c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, -1); 8189c64fc40SJohn Baldwin break; 819b2e60773SJohn Baldwin } 8209675d889SAndrew Gallatin if (tls->snd_tag != NULL) 821b2e60773SJohn Baldwin m_snd_tag_rele(tls->snd_tag); 822fe8c78f0SHans Petter Selasky if (tls->rx_ifp != NULL) 823fe8c78f0SHans Petter Selasky if_rele(tls->rx_ifp); 8249e14430dSJohn Baldwin break; 8259e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 8269e14430dSJohn Baldwin case TCP_TLS_MODE_TOE: 8279e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 8289e14430dSJohn Baldwin case CRYPTO_AES_CBC: 8299e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, -1); 8309e14430dSJohn Baldwin break; 8319e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 8329e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, -1); 8339e14430dSJohn Baldwin break; 8349c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 8359c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, -1); 8369c64fc40SJohn Baldwin break; 8379e14430dSJohn Baldwin } 8389e14430dSJohn Baldwin break; 8399e14430dSJohn Baldwin #endif 840b2e60773SJohn Baldwin } 84196668a81SJohn Baldwin if (tls->ocf_session != NULL) 84296668a81SJohn Baldwin ktls_ocf_free(tls); 843b2e60773SJohn Baldwin if (tls->params.auth_key != NULL) { 8444a711b8dSJohn Baldwin zfree(tls->params.auth_key, M_KTLS); 845b2e60773SJohn Baldwin tls->params.auth_key = NULL; 846b2e60773SJohn Baldwin tls->params.auth_key_len = 0; 847b2e60773SJohn Baldwin } 848b2e60773SJohn Baldwin if (tls->params.cipher_key != NULL) { 8494a711b8dSJohn Baldwin zfree(tls->params.cipher_key, M_KTLS); 850b2e60773SJohn Baldwin tls->params.cipher_key = NULL; 851b2e60773SJohn Baldwin tls->params.cipher_key_len = 0; 852b2e60773SJohn Baldwin } 853b2e60773SJohn Baldwin explicit_bzero(tls->params.iv, sizeof(tls->params.iv)); 854b2e60773SJohn Baldwin } 855b2e60773SJohn Baldwin 856b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 8579e14430dSJohn Baldwin 8589e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 8599e14430dSJohn Baldwin static int 860f1f93475SJohn Baldwin ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction) 8619e14430dSJohn Baldwin { 8629e14430dSJohn Baldwin struct inpcb *inp; 8639e14430dSJohn Baldwin struct tcpcb *tp; 8649e14430dSJohn Baldwin int error; 8659e14430dSJohn Baldwin 8669e14430dSJohn Baldwin inp = so->so_pcb; 8679e14430dSJohn Baldwin INP_WLOCK(inp); 8689e14430dSJohn Baldwin if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8699e14430dSJohn Baldwin INP_WUNLOCK(inp); 8709e14430dSJohn Baldwin return (ECONNRESET); 8719e14430dSJohn Baldwin } 8729e14430dSJohn Baldwin if (inp->inp_socket == NULL) { 8739e14430dSJohn Baldwin INP_WUNLOCK(inp); 8749e14430dSJohn Baldwin return (ECONNRESET); 8759e14430dSJohn Baldwin } 8769e14430dSJohn Baldwin tp = intotcpcb(inp); 8776bcf3c46SJohn Baldwin if (!(tp->t_flags & TF_TOE)) { 8789e14430dSJohn Baldwin INP_WUNLOCK(inp); 8799e14430dSJohn Baldwin return (EOPNOTSUPP); 8809e14430dSJohn Baldwin } 8819e14430dSJohn Baldwin 882f1f93475SJohn Baldwin error = tcp_offload_alloc_tls_session(tp, tls, direction); 8839e14430dSJohn Baldwin INP_WUNLOCK(inp); 8849e14430dSJohn Baldwin if (error == 0) { 8859e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_TOE; 8869e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 8879e14430dSJohn Baldwin case CRYPTO_AES_CBC: 8889e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, 1); 8899e14430dSJohn Baldwin break; 8909e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 8919e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, 1); 8929e14430dSJohn Baldwin break; 8939c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 8949c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, 1); 8959c64fc40SJohn Baldwin break; 8969e14430dSJohn Baldwin } 8979e14430dSJohn Baldwin } 8989e14430dSJohn Baldwin return (error); 8999e14430dSJohn Baldwin } 9009e14430dSJohn Baldwin #endif 9019e14430dSJohn Baldwin 902b2e60773SJohn Baldwin /* 903b2e60773SJohn Baldwin * Common code used when first enabling ifnet TLS on a connection or 904b2e60773SJohn Baldwin * when allocating a new ifnet TLS session due to a routing change. 905b2e60773SJohn Baldwin * This function allocates a new TLS send tag on whatever interface 906b2e60773SJohn Baldwin * the connection is currently routed over. 907b2e60773SJohn Baldwin */ 908b2e60773SJohn Baldwin static int 909b2e60773SJohn Baldwin ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force, 910b2e60773SJohn Baldwin struct m_snd_tag **mstp) 911b2e60773SJohn Baldwin { 912b2e60773SJohn Baldwin union if_snd_tag_alloc_params params; 913b2e60773SJohn Baldwin struct ifnet *ifp; 914983066f0SAlexander V. Chernikov struct nhop_object *nh; 915b2e60773SJohn Baldwin struct tcpcb *tp; 916b2e60773SJohn Baldwin int error; 917b2e60773SJohn Baldwin 918b2e60773SJohn Baldwin INP_RLOCK(inp); 919b2e60773SJohn Baldwin if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 920b2e60773SJohn Baldwin INP_RUNLOCK(inp); 921b2e60773SJohn Baldwin return (ECONNRESET); 922b2e60773SJohn Baldwin } 923b2e60773SJohn Baldwin if (inp->inp_socket == NULL) { 924b2e60773SJohn Baldwin INP_RUNLOCK(inp); 925b2e60773SJohn Baldwin return (ECONNRESET); 926b2e60773SJohn Baldwin } 927b2e60773SJohn Baldwin tp = intotcpcb(inp); 928b2e60773SJohn Baldwin 929b2e60773SJohn Baldwin /* 930b2e60773SJohn Baldwin * Check administrative controls on ifnet TLS to determine if 931b2e60773SJohn Baldwin * ifnet TLS should be denied. 932b2e60773SJohn Baldwin * 933b2e60773SJohn Baldwin * - Always permit 'force' requests. 934b2e60773SJohn Baldwin * - ktls_ifnet_permitted == 0: always deny. 935b2e60773SJohn Baldwin */ 936b2e60773SJohn Baldwin if (!force && ktls_ifnet_permitted == 0) { 937b2e60773SJohn Baldwin INP_RUNLOCK(inp); 938b2e60773SJohn Baldwin return (ENXIO); 939b2e60773SJohn Baldwin } 940b2e60773SJohn Baldwin 941b2e60773SJohn Baldwin /* 942b2e60773SJohn Baldwin * XXX: Use the cached route in the inpcb to find the 943b2e60773SJohn Baldwin * interface. This should perhaps instead use 944b2e60773SJohn Baldwin * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only 945b2e60773SJohn Baldwin * enabled after a connection has completed key negotiation in 946b2e60773SJohn Baldwin * userland, the cached route will be present in practice. 947b2e60773SJohn Baldwin */ 948983066f0SAlexander V. Chernikov nh = inp->inp_route.ro_nh; 949983066f0SAlexander V. Chernikov if (nh == NULL) { 950b2e60773SJohn Baldwin INP_RUNLOCK(inp); 951b2e60773SJohn Baldwin return (ENXIO); 952b2e60773SJohn Baldwin } 953983066f0SAlexander V. Chernikov ifp = nh->nh_ifp; 954b2e60773SJohn Baldwin if_ref(ifp); 955b2e60773SJohn Baldwin 956521eac97SJohn Baldwin /* 957521eac97SJohn Baldwin * Allocate a TLS + ratelimit tag if the connection has an 958521eac97SJohn Baldwin * existing pacing rate. 959521eac97SJohn Baldwin */ 960521eac97SJohn Baldwin if (tp->t_pacing_rate != -1 && 961521eac97SJohn Baldwin (ifp->if_capenable & IFCAP_TXTLS_RTLMT) != 0) { 962521eac97SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT; 963521eac97SJohn Baldwin params.tls_rate_limit.inp = inp; 964521eac97SJohn Baldwin params.tls_rate_limit.tls = tls; 965521eac97SJohn Baldwin params.tls_rate_limit.max_rate = tp->t_pacing_rate; 966521eac97SJohn Baldwin } else { 967b2e60773SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS; 968521eac97SJohn Baldwin params.tls.inp = inp; 969521eac97SJohn Baldwin params.tls.tls = tls; 970521eac97SJohn Baldwin } 971b2e60773SJohn Baldwin params.hdr.flowid = inp->inp_flowid; 972b2e60773SJohn Baldwin params.hdr.flowtype = inp->inp_flowtype; 97398085baeSAndrew Gallatin params.hdr.numa_domain = inp->inp_numa_domain; 974b2e60773SJohn Baldwin INP_RUNLOCK(inp); 975b2e60773SJohn Baldwin 9763f43ada9SGleb Smirnoff if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { 977b2e60773SJohn Baldwin error = EOPNOTSUPP; 978b2e60773SJohn Baldwin goto out; 979b2e60773SJohn Baldwin } 980b2e60773SJohn Baldwin if (inp->inp_vflag & INP_IPV6) { 981b2e60773SJohn Baldwin if ((ifp->if_capenable & IFCAP_TXTLS6) == 0) { 982b2e60773SJohn Baldwin error = EOPNOTSUPP; 983b2e60773SJohn Baldwin goto out; 984b2e60773SJohn Baldwin } 985b2e60773SJohn Baldwin } else { 986b2e60773SJohn Baldwin if ((ifp->if_capenable & IFCAP_TXTLS4) == 0) { 987b2e60773SJohn Baldwin error = EOPNOTSUPP; 988b2e60773SJohn Baldwin goto out; 989b2e60773SJohn Baldwin } 990b2e60773SJohn Baldwin } 99136e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, mstp); 992b2e60773SJohn Baldwin out: 993b2e60773SJohn Baldwin if_rele(ifp); 994b2e60773SJohn Baldwin return (error); 995b2e60773SJohn Baldwin } 996b2e60773SJohn Baldwin 997fe8c78f0SHans Petter Selasky /* 998fe8c78f0SHans Petter Selasky * Allocate an initial TLS receive tag for doing HW decryption of TLS 999fe8c78f0SHans Petter Selasky * data. 1000fe8c78f0SHans Petter Selasky * 1001fe8c78f0SHans Petter Selasky * This function allocates a new TLS receive tag on whatever interface 1002fe8c78f0SHans Petter Selasky * the connection is currently routed over. If the connection ends up 1003fe8c78f0SHans Petter Selasky * using a different interface for receive this will get fixed up via 1004fe8c78f0SHans Petter Selasky * ktls_input_ifp_mismatch as future packets arrive. 1005fe8c78f0SHans Petter Selasky */ 1006b2e60773SJohn Baldwin static int 1007fe8c78f0SHans Petter Selasky ktls_alloc_rcv_tag(struct inpcb *inp, struct ktls_session *tls, 1008fe8c78f0SHans Petter Selasky struct m_snd_tag **mstp) 1009fe8c78f0SHans Petter Selasky { 1010fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 1011fe8c78f0SHans Petter Selasky struct ifnet *ifp; 1012fe8c78f0SHans Petter Selasky struct nhop_object *nh; 1013fe8c78f0SHans Petter Selasky int error; 1014fe8c78f0SHans Petter Selasky 1015fe8c78f0SHans Petter Selasky if (!ktls_ocf_recrypt_supported(tls)) 1016fe8c78f0SHans Petter Selasky return (ENXIO); 1017fe8c78f0SHans Petter Selasky 1018fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 1019fe8c78f0SHans Petter Selasky if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1020fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1021fe8c78f0SHans Petter Selasky return (ECONNRESET); 1022fe8c78f0SHans Petter Selasky } 1023fe8c78f0SHans Petter Selasky if (inp->inp_socket == NULL) { 1024fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1025fe8c78f0SHans Petter Selasky return (ECONNRESET); 1026fe8c78f0SHans Petter Selasky } 1027fe8c78f0SHans Petter Selasky 1028fe8c78f0SHans Petter Selasky /* 1029fe8c78f0SHans Petter Selasky * Check administrative controls on ifnet TLS to determine if 1030fe8c78f0SHans Petter Selasky * ifnet TLS should be denied. 1031fe8c78f0SHans Petter Selasky */ 1032fe8c78f0SHans Petter Selasky if (ktls_ifnet_permitted == 0) { 1033fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1034fe8c78f0SHans Petter Selasky return (ENXIO); 1035fe8c78f0SHans Petter Selasky } 1036fe8c78f0SHans Petter Selasky 1037fe8c78f0SHans Petter Selasky /* 1038fe8c78f0SHans Petter Selasky * XXX: As with ktls_alloc_snd_tag, use the cached route in 1039fe8c78f0SHans Petter Selasky * the inpcb to find the interface. 1040fe8c78f0SHans Petter Selasky */ 1041fe8c78f0SHans Petter Selasky nh = inp->inp_route.ro_nh; 1042fe8c78f0SHans Petter Selasky if (nh == NULL) { 1043fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1044fe8c78f0SHans Petter Selasky return (ENXIO); 1045fe8c78f0SHans Petter Selasky } 1046fe8c78f0SHans Petter Selasky ifp = nh->nh_ifp; 1047fe8c78f0SHans Petter Selasky if_ref(ifp); 1048fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 1049fe8c78f0SHans Petter Selasky 1050fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 1051fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 1052fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 1053fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 1054fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 1055fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 1056fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = 0; 1057fe8c78f0SHans Petter Selasky 1058fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1059fe8c78f0SHans Petter Selasky 1060fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 1061fe8c78f0SHans Petter Selasky if ((ifp->if_capenable2 & IFCAP2_RXTLS6) == 0) { 1062fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1063fe8c78f0SHans Petter Selasky goto out; 1064fe8c78f0SHans Petter Selasky } 1065fe8c78f0SHans Petter Selasky } else { 1066fe8c78f0SHans Petter Selasky if ((ifp->if_capenable2 & IFCAP2_RXTLS4) == 0) { 1067fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1068fe8c78f0SHans Petter Selasky goto out; 1069fe8c78f0SHans Petter Selasky } 1070fe8c78f0SHans Petter Selasky } 1071fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, mstp); 1072fe8c78f0SHans Petter Selasky 1073fe8c78f0SHans Petter Selasky /* 1074fe8c78f0SHans Petter Selasky * If this connection is over a vlan, vlan_snd_tag_alloc 1075fe8c78f0SHans Petter Selasky * rewrites vlan_id with the saved interface. Save the VLAN 1076fe8c78f0SHans Petter Selasky * ID for use in ktls_reset_receive_tag which allocates new 1077fe8c78f0SHans Petter Selasky * receive tags directly from the leaf interface bypassing 1078fe8c78f0SHans Petter Selasky * if_vlan. 1079fe8c78f0SHans Petter Selasky */ 1080fe8c78f0SHans Petter Selasky if (error == 0) 1081fe8c78f0SHans Petter Selasky tls->rx_vlan_id = params.tls_rx.vlan_id; 1082fe8c78f0SHans Petter Selasky out: 1083fe8c78f0SHans Petter Selasky return (error); 1084fe8c78f0SHans Petter Selasky } 1085fe8c78f0SHans Petter Selasky 1086fe8c78f0SHans Petter Selasky static int 1087fe8c78f0SHans Petter Selasky ktls_try_ifnet(struct socket *so, struct ktls_session *tls, int direction, 1088fe8c78f0SHans Petter Selasky bool force) 1089b2e60773SJohn Baldwin { 1090b2e60773SJohn Baldwin struct m_snd_tag *mst; 1091b2e60773SJohn Baldwin int error; 1092b2e60773SJohn Baldwin 1093fe8c78f0SHans Petter Selasky switch (direction) { 1094fe8c78f0SHans Petter Selasky case KTLS_TX: 1095b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst); 1096fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1097fe8c78f0SHans Petter Selasky goto done; 1098fe8c78f0SHans Petter Selasky break; 1099fe8c78f0SHans Petter Selasky case KTLS_RX: 1100fe8c78f0SHans Petter Selasky KASSERT(!force, ("%s: forced receive tag", __func__)); 1101fe8c78f0SHans Petter Selasky error = ktls_alloc_rcv_tag(so->so_pcb, tls, &mst); 1102fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1103fe8c78f0SHans Petter Selasky goto done; 1104fe8c78f0SHans Petter Selasky break; 1105fe8c78f0SHans Petter Selasky default: 1106fe8c78f0SHans Petter Selasky __assert_unreachable(); 1107fe8c78f0SHans Petter Selasky } 1108fe8c78f0SHans Petter Selasky 11099e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_IFNET; 1110b2e60773SJohn Baldwin tls->snd_tag = mst; 1111fe8c78f0SHans Petter Selasky 1112b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1113b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1114b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, 1); 1115b2e60773SJohn Baldwin break; 1116b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1117b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, 1); 1118b2e60773SJohn Baldwin break; 11199c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 11209c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, 1); 11219c64fc40SJohn Baldwin break; 1122fe8c78f0SHans Petter Selasky default: 1123fe8c78f0SHans Petter Selasky break; 1124b2e60773SJohn Baldwin } 1125fe8c78f0SHans Petter Selasky done: 1126b2e60773SJohn Baldwin return (error); 1127b2e60773SJohn Baldwin } 1128b2e60773SJohn Baldwin 112996668a81SJohn Baldwin static void 113096668a81SJohn Baldwin ktls_use_sw(struct ktls_session *tls) 1131b2e60773SJohn Baldwin { 11329e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_SW; 1133b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1134b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1135b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, 1); 1136b2e60773SJohn Baldwin break; 1137b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1138b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, 1); 1139b2e60773SJohn Baldwin break; 11409c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 11419c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, 1); 11429c64fc40SJohn Baldwin break; 1143b2e60773SJohn Baldwin } 114496668a81SJohn Baldwin } 114596668a81SJohn Baldwin 114696668a81SJohn Baldwin static int 114796668a81SJohn Baldwin ktls_try_sw(struct socket *so, struct ktls_session *tls, int direction) 114896668a81SJohn Baldwin { 114996668a81SJohn Baldwin int error; 115096668a81SJohn Baldwin 115196668a81SJohn Baldwin error = ktls_ocf_try(so, tls, direction); 115296668a81SJohn Baldwin if (error) 115396668a81SJohn Baldwin return (error); 115496668a81SJohn Baldwin ktls_use_sw(tls); 1155b2e60773SJohn Baldwin return (0); 1156b2e60773SJohn Baldwin } 1157b2e60773SJohn Baldwin 11583c0e5685SJohn Baldwin /* 11593c0e5685SJohn Baldwin * KTLS RX stores data in the socket buffer as a list of TLS records, 11603c0e5685SJohn Baldwin * where each record is stored as a control message containg the TLS 11613c0e5685SJohn Baldwin * header followed by data mbufs containing the decrypted data. This 11623c0e5685SJohn Baldwin * is different from KTLS TX which always uses an mb_ext_pgs mbuf for 11633c0e5685SJohn Baldwin * both encrypted and decrypted data. TLS records decrypted by a NIC 11643c0e5685SJohn Baldwin * should be queued to the socket buffer as records, but encrypted 11653c0e5685SJohn Baldwin * data which needs to be decrypted by software arrives as a stream of 11663c0e5685SJohn Baldwin * regular mbufs which need to be converted. In addition, there may 11673c0e5685SJohn Baldwin * already be pending encrypted data in the socket buffer when KTLS RX 11683c0e5685SJohn Baldwin * is enabled. 11693c0e5685SJohn Baldwin * 11703c0e5685SJohn Baldwin * To manage not-yet-decrypted data for KTLS RX, the following scheme 11713c0e5685SJohn Baldwin * is used: 11723c0e5685SJohn Baldwin * 11733c0e5685SJohn Baldwin * - A single chain of NOTREADY mbufs is hung off of sb_mtls. 11743c0e5685SJohn Baldwin * 11753c0e5685SJohn Baldwin * - ktls_check_rx checks this chain of mbufs reading the TLS header 11763c0e5685SJohn Baldwin * from the first mbuf. Once all of the data for that TLS record is 11773c0e5685SJohn Baldwin * queued, the socket is queued to a worker thread. 11783c0e5685SJohn Baldwin * 11793c0e5685SJohn Baldwin * - The worker thread calls ktls_decrypt to decrypt TLS records in 11803c0e5685SJohn Baldwin * the TLS chain. Each TLS record is detached from the TLS chain, 11813c0e5685SJohn Baldwin * decrypted, and inserted into the regular socket buffer chain as 11823c0e5685SJohn Baldwin * record starting with a control message holding the TLS header and 11833c0e5685SJohn Baldwin * a chain of mbufs holding the encrypted data. 11843c0e5685SJohn Baldwin */ 11853c0e5685SJohn Baldwin 11863c0e5685SJohn Baldwin static void 11873c0e5685SJohn Baldwin sb_mark_notready(struct sockbuf *sb) 11883c0e5685SJohn Baldwin { 11893c0e5685SJohn Baldwin struct mbuf *m; 11903c0e5685SJohn Baldwin 11913c0e5685SJohn Baldwin m = sb->sb_mb; 11923c0e5685SJohn Baldwin sb->sb_mtls = m; 11933c0e5685SJohn Baldwin sb->sb_mb = NULL; 11943c0e5685SJohn Baldwin sb->sb_mbtail = NULL; 11953c0e5685SJohn Baldwin sb->sb_lastrecord = NULL; 11963c0e5685SJohn Baldwin for (; m != NULL; m = m->m_next) { 11973c0e5685SJohn Baldwin KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL", 11983c0e5685SJohn Baldwin __func__)); 11993c0e5685SJohn Baldwin KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail", 12003c0e5685SJohn Baldwin __func__)); 12013c0e5685SJohn Baldwin KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len", 12023c0e5685SJohn Baldwin __func__)); 12033c0e5685SJohn Baldwin m->m_flags |= M_NOTREADY; 12043c0e5685SJohn Baldwin sb->sb_acc -= m->m_len; 12053c0e5685SJohn Baldwin sb->sb_tlscc += m->m_len; 12063c0e5685SJohn Baldwin sb->sb_mtlstail = m; 12073c0e5685SJohn Baldwin } 12083c0e5685SJohn Baldwin KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc, 12093c0e5685SJohn Baldwin ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc, 12103c0e5685SJohn Baldwin sb->sb_ccc)); 12113c0e5685SJohn Baldwin } 12123c0e5685SJohn Baldwin 1213c57dbec6SJohn Baldwin /* 1214c57dbec6SJohn Baldwin * Return information about the pending TLS data in a socket 1215c57dbec6SJohn Baldwin * buffer. On return, 'seqno' is set to the sequence number 1216c57dbec6SJohn Baldwin * of the next TLS record to be received, 'resid' is set to 1217c57dbec6SJohn Baldwin * the amount of bytes still needed for the last pending 1218c57dbec6SJohn Baldwin * record. The function returns 'false' if the last pending 1219c57dbec6SJohn Baldwin * record contains a partial TLS header. In that case, 'resid' 1220c57dbec6SJohn Baldwin * is the number of bytes needed to complete the TLS header. 1221c57dbec6SJohn Baldwin */ 1222c57dbec6SJohn Baldwin bool 1223c57dbec6SJohn Baldwin ktls_pending_rx_info(struct sockbuf *sb, uint64_t *seqnop, size_t *residp) 1224c57dbec6SJohn Baldwin { 1225c57dbec6SJohn Baldwin struct tls_record_layer hdr; 1226c57dbec6SJohn Baldwin struct mbuf *m; 1227c57dbec6SJohn Baldwin uint64_t seqno; 1228c57dbec6SJohn Baldwin size_t resid; 1229c57dbec6SJohn Baldwin u_int offset, record_len; 1230c57dbec6SJohn Baldwin 1231c57dbec6SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 1232c57dbec6SJohn Baldwin MPASS(sb->sb_flags & SB_TLS_RX); 1233c57dbec6SJohn Baldwin seqno = sb->sb_tls_seqno; 1234c57dbec6SJohn Baldwin resid = sb->sb_tlscc; 1235c57dbec6SJohn Baldwin m = sb->sb_mtls; 1236c57dbec6SJohn Baldwin offset = 0; 1237c57dbec6SJohn Baldwin 1238c57dbec6SJohn Baldwin if (resid == 0) { 1239c57dbec6SJohn Baldwin *seqnop = seqno; 1240c57dbec6SJohn Baldwin *residp = 0; 1241c57dbec6SJohn Baldwin return (true); 1242c57dbec6SJohn Baldwin } 1243c57dbec6SJohn Baldwin 1244c57dbec6SJohn Baldwin for (;;) { 1245c57dbec6SJohn Baldwin seqno++; 1246c57dbec6SJohn Baldwin 1247c57dbec6SJohn Baldwin if (resid < sizeof(hdr)) { 1248c57dbec6SJohn Baldwin *seqnop = seqno; 1249c57dbec6SJohn Baldwin *residp = sizeof(hdr) - resid; 1250c57dbec6SJohn Baldwin return (false); 1251c57dbec6SJohn Baldwin } 1252c57dbec6SJohn Baldwin 1253c57dbec6SJohn Baldwin m_copydata(m, offset, sizeof(hdr), (void *)&hdr); 1254c57dbec6SJohn Baldwin 1255c57dbec6SJohn Baldwin record_len = sizeof(hdr) + ntohs(hdr.tls_length); 1256c57dbec6SJohn Baldwin if (resid <= record_len) { 1257c57dbec6SJohn Baldwin *seqnop = seqno; 1258c57dbec6SJohn Baldwin *residp = record_len - resid; 1259c57dbec6SJohn Baldwin return (true); 1260c57dbec6SJohn Baldwin } 1261c57dbec6SJohn Baldwin resid -= record_len; 1262c57dbec6SJohn Baldwin 1263c57dbec6SJohn Baldwin while (record_len != 0) { 1264c57dbec6SJohn Baldwin if (m->m_len - offset > record_len) { 1265c57dbec6SJohn Baldwin offset += record_len; 1266c57dbec6SJohn Baldwin break; 1267c57dbec6SJohn Baldwin } 1268c57dbec6SJohn Baldwin 1269c57dbec6SJohn Baldwin record_len -= (m->m_len - offset); 1270c57dbec6SJohn Baldwin offset = 0; 1271c57dbec6SJohn Baldwin m = m->m_next; 1272c57dbec6SJohn Baldwin } 1273c57dbec6SJohn Baldwin } 1274c57dbec6SJohn Baldwin } 1275c57dbec6SJohn Baldwin 1276b2e60773SJohn Baldwin int 1277f1f93475SJohn Baldwin ktls_enable_rx(struct socket *so, struct tls_enable *en) 1278f1f93475SJohn Baldwin { 1279f1f93475SJohn Baldwin struct ktls_session *tls; 1280f1f93475SJohn Baldwin int error; 1281f1f93475SJohn Baldwin 1282f1f93475SJohn Baldwin if (!ktls_offload_enable) 1283f1f93475SJohn Baldwin return (ENOTSUP); 12846685e259SMichael Tuexen if (SOLISTENING(so)) 12856685e259SMichael Tuexen return (EINVAL); 1286f1f93475SJohn Baldwin 1287f1f93475SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1288f1f93475SJohn Baldwin 1289f1f93475SJohn Baldwin /* 1290f1f93475SJohn Baldwin * This should always be true since only the TCP socket option 1291f1f93475SJohn Baldwin * invokes this function. 1292f1f93475SJohn Baldwin */ 1293f1f93475SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1294f1f93475SJohn Baldwin return (EINVAL); 1295f1f93475SJohn Baldwin 1296f1f93475SJohn Baldwin /* 1297f1f93475SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1298f1f93475SJohn Baldwin * this to support rekeying in the future. 1299f1f93475SJohn Baldwin */ 1300f1f93475SJohn Baldwin if (so->so_rcv.sb_tls_info != NULL) 1301f1f93475SJohn Baldwin return (EALREADY); 1302f1f93475SJohn Baldwin 1303f1f93475SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1304f1f93475SJohn Baldwin return (ENOTSUP); 1305f1f93475SJohn Baldwin 1306fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_RX); 1307f1f93475SJohn Baldwin if (error) 1308f1f93475SJohn Baldwin return (error); 1309f1f93475SJohn Baldwin 131096668a81SJohn Baldwin error = ktls_ocf_try(so, tls, KTLS_RX); 1311f1f93475SJohn Baldwin if (error) { 1312f1f93475SJohn Baldwin ktls_cleanup(tls); 1313f1f93475SJohn Baldwin return (error); 1314f1f93475SJohn Baldwin } 1315f1f93475SJohn Baldwin 1316f1f93475SJohn Baldwin /* Mark the socket as using TLS offload. */ 1317f1f93475SJohn Baldwin SOCKBUF_LOCK(&so->so_rcv); 13183c0e5685SJohn Baldwin so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq); 1319f1f93475SJohn Baldwin so->so_rcv.sb_tls_info = tls; 13203c0e5685SJohn Baldwin so->so_rcv.sb_flags |= SB_TLS_RX; 13213c0e5685SJohn Baldwin 13223c0e5685SJohn Baldwin /* Mark existing data as not ready until it can be decrypted. */ 13233c0e5685SJohn Baldwin sb_mark_notready(&so->so_rcv); 13243c0e5685SJohn Baldwin ktls_check_rx(&so->so_rcv); 1325f1f93475SJohn Baldwin SOCKBUF_UNLOCK(&so->so_rcv); 1326f1f93475SJohn Baldwin 1327fe8c78f0SHans Petter Selasky /* Prefer TOE -> ifnet TLS -> software TLS. */ 1328d958bc79SJohn Baldwin #ifdef TCP_OFFLOAD 1329d958bc79SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_RX); 1330d958bc79SJohn Baldwin if (error) 1331d958bc79SJohn Baldwin #endif 1332fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_RX, false); 1333fe8c78f0SHans Petter Selasky if (error) 1334d958bc79SJohn Baldwin ktls_use_sw(tls); 1335d958bc79SJohn Baldwin 1336f1f93475SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1337f1f93475SJohn Baldwin 1338f1f93475SJohn Baldwin return (0); 1339f1f93475SJohn Baldwin } 1340f1f93475SJohn Baldwin 1341f1f93475SJohn Baldwin int 1342b2e60773SJohn Baldwin ktls_enable_tx(struct socket *so, struct tls_enable *en) 1343b2e60773SJohn Baldwin { 1344b2e60773SJohn Baldwin struct ktls_session *tls; 1345521eac97SJohn Baldwin struct inpcb *inp; 1346b2e60773SJohn Baldwin int error; 1347b2e60773SJohn Baldwin 1348b2e60773SJohn Baldwin if (!ktls_offload_enable) 1349b2e60773SJohn Baldwin return (ENOTSUP); 13506685e259SMichael Tuexen if (SOLISTENING(so)) 13516685e259SMichael Tuexen return (EINVAL); 1352b2e60773SJohn Baldwin 1353b2e60773SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1354b2e60773SJohn Baldwin 1355b2e60773SJohn Baldwin /* 1356b2e60773SJohn Baldwin * This should always be true since only the TCP socket option 1357b2e60773SJohn Baldwin * invokes this function. 1358b2e60773SJohn Baldwin */ 1359b2e60773SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1360b2e60773SJohn Baldwin return (EINVAL); 1361b2e60773SJohn Baldwin 1362b2e60773SJohn Baldwin /* 1363b2e60773SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1364b2e60773SJohn Baldwin * this to support rekeying in the future. 1365b2e60773SJohn Baldwin */ 1366b2e60773SJohn Baldwin if (so->so_snd.sb_tls_info != NULL) 1367b2e60773SJohn Baldwin return (EALREADY); 1368b2e60773SJohn Baldwin 1369b2e60773SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1370b2e60773SJohn Baldwin return (ENOTSUP); 1371b2e60773SJohn Baldwin 1372b2e60773SJohn Baldwin /* TLS requires ext pgs */ 1373b2e60773SJohn Baldwin if (mb_use_ext_pgs == 0) 1374b2e60773SJohn Baldwin return (ENXIO); 1375b2e60773SJohn Baldwin 1376fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_TX); 1377b2e60773SJohn Baldwin if (error) 1378b2e60773SJohn Baldwin return (error); 1379b2e60773SJohn Baldwin 13809e14430dSJohn Baldwin /* Prefer TOE -> ifnet TLS -> software TLS. */ 13819e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 1382f1f93475SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_TX); 13839e14430dSJohn Baldwin if (error) 13849e14430dSJohn Baldwin #endif 1385fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_TX, false); 1386b2e60773SJohn Baldwin if (error) 13873c0e5685SJohn Baldwin error = ktls_try_sw(so, tls, KTLS_TX); 1388b2e60773SJohn Baldwin 1389b2e60773SJohn Baldwin if (error) { 1390b2e60773SJohn Baldwin ktls_cleanup(tls); 1391b2e60773SJohn Baldwin return (error); 1392b2e60773SJohn Baldwin } 1393b2e60773SJohn Baldwin 1394f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1395b2e60773SJohn Baldwin if (error) { 1396b2e60773SJohn Baldwin ktls_cleanup(tls); 1397b2e60773SJohn Baldwin return (error); 1398b2e60773SJohn Baldwin } 1399b2e60773SJohn Baldwin 1400521eac97SJohn Baldwin /* 1401521eac97SJohn Baldwin * Write lock the INP when setting sb_tls_info so that 1402521eac97SJohn Baldwin * routines in tcp_ratelimit.c can read sb_tls_info while 1403521eac97SJohn Baldwin * holding the INP lock. 1404521eac97SJohn Baldwin */ 1405521eac97SJohn Baldwin inp = so->so_pcb; 1406521eac97SJohn Baldwin INP_WLOCK(inp); 1407b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1408ec1db6e1SJohn Baldwin so->so_snd.sb_tls_seqno = be64dec(en->rec_seq); 1409b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls; 14109e14430dSJohn Baldwin if (tls->mode != TCP_TLS_MODE_SW) 1411b2e60773SJohn Baldwin so->so_snd.sb_flags |= SB_TLS_IFNET; 1412b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1413521eac97SJohn Baldwin INP_WUNLOCK(inp); 1414f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1415b2e60773SJohn Baldwin 1416b2e60773SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1417b2e60773SJohn Baldwin 1418b2e60773SJohn Baldwin return (0); 1419b2e60773SJohn Baldwin } 1420b2e60773SJohn Baldwin 1421b2e60773SJohn Baldwin int 1422bf256782SMark Johnston ktls_get_rx_mode(struct socket *so, int *modep) 1423f1f93475SJohn Baldwin { 1424f1f93475SJohn Baldwin struct ktls_session *tls; 1425a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1426f1f93475SJohn Baldwin 14276685e259SMichael Tuexen if (SOLISTENING(so)) 14286685e259SMichael Tuexen return (EINVAL); 1429f1f93475SJohn Baldwin inp = so->so_pcb; 1430f1f93475SJohn Baldwin INP_WLOCK_ASSERT(inp); 1431bf256782SMark Johnston SOCK_RECVBUF_LOCK(so); 1432f1f93475SJohn Baldwin tls = so->so_rcv.sb_tls_info; 1433f1f93475SJohn Baldwin if (tls == NULL) 1434bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1435f1f93475SJohn Baldwin else 1436bf256782SMark Johnston *modep = tls->mode; 1437bf256782SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1438bf256782SMark Johnston return (0); 1439f1f93475SJohn Baldwin } 1440f1f93475SJohn Baldwin 14419e2cce7eSHans Petter Selasky /* 14429e2cce7eSHans Petter Selasky * ktls_get_rx_sequence - get the next TCP- and TLS- sequence number. 14439e2cce7eSHans Petter Selasky * 14449e2cce7eSHans Petter Selasky * This function gets information about the next TCP- and TLS- 14459e2cce7eSHans Petter Selasky * sequence number to be processed by the TLS receive worker 14469e2cce7eSHans Petter Selasky * thread. The information is extracted from the given "inpcb" 14479e2cce7eSHans Petter Selasky * structure. The values are stored in host endian format at the two 14489e2cce7eSHans Petter Selasky * given output pointer locations. The TCP sequence number points to 14499e2cce7eSHans Petter Selasky * the beginning of the TLS header. 14509e2cce7eSHans Petter Selasky * 14519e2cce7eSHans Petter Selasky * This function returns zero on success, else a non-zero error code 14529e2cce7eSHans Petter Selasky * is returned. 14539e2cce7eSHans Petter Selasky */ 14549e2cce7eSHans Petter Selasky int 14559e2cce7eSHans Petter Selasky ktls_get_rx_sequence(struct inpcb *inp, uint32_t *tcpseq, uint64_t *tlsseq) 14569e2cce7eSHans Petter Selasky { 14579e2cce7eSHans Petter Selasky struct socket *so; 14589e2cce7eSHans Petter Selasky struct tcpcb *tp; 14599e2cce7eSHans Petter Selasky 14609e2cce7eSHans Petter Selasky INP_RLOCK(inp); 14619e2cce7eSHans Petter Selasky so = inp->inp_socket; 14629e2cce7eSHans Petter Selasky if (__predict_false(so == NULL)) { 14639e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14649e2cce7eSHans Petter Selasky return (EINVAL); 14659e2cce7eSHans Petter Selasky } 14669e2cce7eSHans Petter Selasky if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 14679e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14689e2cce7eSHans Petter Selasky return (ECONNRESET); 14699e2cce7eSHans Petter Selasky } 14709e2cce7eSHans Petter Selasky 14719e2cce7eSHans Petter Selasky tp = intotcpcb(inp); 14729e2cce7eSHans Petter Selasky MPASS(tp != NULL); 14739e2cce7eSHans Petter Selasky 14749e2cce7eSHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 14759e2cce7eSHans Petter Selasky *tcpseq = tp->rcv_nxt - so->so_rcv.sb_tlscc; 14769e2cce7eSHans Petter Selasky *tlsseq = so->so_rcv.sb_tls_seqno; 14779e2cce7eSHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 14789e2cce7eSHans Petter Selasky 14799e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14809e2cce7eSHans Petter Selasky 14819e2cce7eSHans Petter Selasky return (0); 14829e2cce7eSHans Petter Selasky } 14839e2cce7eSHans Petter Selasky 1484f1f93475SJohn Baldwin int 1485bf256782SMark Johnston ktls_get_tx_mode(struct socket *so, int *modep) 1486b2e60773SJohn Baldwin { 1487b2e60773SJohn Baldwin struct ktls_session *tls; 1488a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1489b2e60773SJohn Baldwin 14906685e259SMichael Tuexen if (SOLISTENING(so)) 14916685e259SMichael Tuexen return (EINVAL); 1492b2e60773SJohn Baldwin inp = so->so_pcb; 1493b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1494bf256782SMark Johnston SOCK_SENDBUF_LOCK(so); 1495b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1496b2e60773SJohn Baldwin if (tls == NULL) 1497bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1498b2e60773SJohn Baldwin else 1499bf256782SMark Johnston *modep = tls->mode; 1500bf256782SMark Johnston SOCK_SENDBUF_UNLOCK(so); 1501bf256782SMark Johnston return (0); 1502b2e60773SJohn Baldwin } 1503b2e60773SJohn Baldwin 1504b2e60773SJohn Baldwin /* 1505b2e60773SJohn Baldwin * Switch between SW and ifnet TLS sessions as requested. 1506b2e60773SJohn Baldwin */ 1507b2e60773SJohn Baldwin int 1508b2e60773SJohn Baldwin ktls_set_tx_mode(struct socket *so, int mode) 1509b2e60773SJohn Baldwin { 1510b2e60773SJohn Baldwin struct ktls_session *tls, *tls_new; 1511b2e60773SJohn Baldwin struct inpcb *inp; 1512b2e60773SJohn Baldwin int error; 1513b2e60773SJohn Baldwin 15146685e259SMichael Tuexen if (SOLISTENING(so)) 15156685e259SMichael Tuexen return (EINVAL); 15169e14430dSJohn Baldwin switch (mode) { 15179e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 15189e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 15199e14430dSJohn Baldwin break; 15209e14430dSJohn Baldwin default: 15219e14430dSJohn Baldwin return (EINVAL); 15229e14430dSJohn Baldwin } 1523b2e60773SJohn Baldwin 1524b2e60773SJohn Baldwin inp = so->so_pcb; 1525b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1526b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1527b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1528b2e60773SJohn Baldwin if (tls == NULL) { 1529b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1530b2e60773SJohn Baldwin return (0); 1531b2e60773SJohn Baldwin } 1532b2e60773SJohn Baldwin 15339e14430dSJohn Baldwin if (tls->mode == mode) { 1534b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1535b2e60773SJohn Baldwin return (0); 1536b2e60773SJohn Baldwin } 1537b2e60773SJohn Baldwin 1538b2e60773SJohn Baldwin tls = ktls_hold(tls); 1539b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1540b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1541b2e60773SJohn Baldwin 1542fe8c78f0SHans Petter Selasky tls_new = ktls_clone_session(tls, KTLS_TX); 1543b2e60773SJohn Baldwin 1544b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1545fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls_new, KTLS_TX, true); 1546b2e60773SJohn Baldwin else 15473c0e5685SJohn Baldwin error = ktls_try_sw(so, tls_new, KTLS_TX); 1548b2e60773SJohn Baldwin if (error) { 1549b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1550b2e60773SJohn Baldwin ktls_free(tls_new); 1551b2e60773SJohn Baldwin ktls_free(tls); 1552b2e60773SJohn Baldwin INP_WLOCK(inp); 1553b2e60773SJohn Baldwin return (error); 1554b2e60773SJohn Baldwin } 1555b2e60773SJohn Baldwin 1556f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1557b2e60773SJohn Baldwin if (error) { 1558b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1559b2e60773SJohn Baldwin ktls_free(tls_new); 1560b2e60773SJohn Baldwin ktls_free(tls); 1561b2e60773SJohn Baldwin INP_WLOCK(inp); 1562b2e60773SJohn Baldwin return (error); 1563b2e60773SJohn Baldwin } 1564b2e60773SJohn Baldwin 1565b2e60773SJohn Baldwin /* 1566b2e60773SJohn Baldwin * If we raced with another session change, keep the existing 1567b2e60773SJohn Baldwin * session. 1568b2e60773SJohn Baldwin */ 1569b2e60773SJohn Baldwin if (tls != so->so_snd.sb_tls_info) { 1570b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1571f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1572b2e60773SJohn Baldwin ktls_free(tls_new); 1573b2e60773SJohn Baldwin ktls_free(tls); 1574b2e60773SJohn Baldwin INP_WLOCK(inp); 1575b2e60773SJohn Baldwin return (EBUSY); 1576b2e60773SJohn Baldwin } 1577b2e60773SJohn Baldwin 1578cd0525f6SJohn Baldwin INP_WLOCK(inp); 1579b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1580b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls_new; 15819e14430dSJohn Baldwin if (tls_new->mode != TCP_TLS_MODE_SW) 1582b2e60773SJohn Baldwin so->so_snd.sb_flags |= SB_TLS_IFNET; 1583b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1584f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1585b2e60773SJohn Baldwin 1586b2e60773SJohn Baldwin /* 1587b2e60773SJohn Baldwin * Drop two references on 'tls'. The first is for the 1588b2e60773SJohn Baldwin * ktls_hold() above. The second drops the reference from the 1589b2e60773SJohn Baldwin * socket buffer. 1590b2e60773SJohn Baldwin */ 1591b2e60773SJohn Baldwin KASSERT(tls->refcount >= 2, ("too few references on old session")); 1592b2e60773SJohn Baldwin ktls_free(tls); 1593b2e60773SJohn Baldwin ktls_free(tls); 1594b2e60773SJohn Baldwin 1595b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1596b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_ifnet, 1); 1597b2e60773SJohn Baldwin else 1598b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_sw, 1); 1599b2e60773SJohn Baldwin 1600b2e60773SJohn Baldwin return (0); 1601b2e60773SJohn Baldwin } 1602b2e60773SJohn Baldwin 1603b2e60773SJohn Baldwin /* 1604fe8c78f0SHans Petter Selasky * Try to allocate a new TLS receive tag. This task is scheduled when 1605fe8c78f0SHans Petter Selasky * sbappend_ktls_rx detects an input path change. If a new tag is 1606fe8c78f0SHans Petter Selasky * allocated, replace the tag in the TLS session. If a new tag cannot 1607fe8c78f0SHans Petter Selasky * be allocated, let the session fall back to software decryption. 1608fe8c78f0SHans Petter Selasky */ 1609fe8c78f0SHans Petter Selasky static void 1610fe8c78f0SHans Petter Selasky ktls_reset_receive_tag(void *context, int pending) 1611fe8c78f0SHans Petter Selasky { 1612fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 1613fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1614fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 1615fe8c78f0SHans Petter Selasky struct inpcb *inp; 1616fe8c78f0SHans Petter Selasky struct ifnet *ifp; 1617fe8c78f0SHans Petter Selasky struct socket *so; 1618fe8c78f0SHans Petter Selasky int error; 1619fe8c78f0SHans Petter Selasky 1620fe8c78f0SHans Petter Selasky MPASS(pending == 1); 1621fe8c78f0SHans Petter Selasky 1622fe8c78f0SHans Petter Selasky tls = context; 1623fe8c78f0SHans Petter Selasky so = tls->so; 1624fe8c78f0SHans Petter Selasky inp = so->so_pcb; 1625fe8c78f0SHans Petter Selasky ifp = NULL; 1626fe8c78f0SHans Petter Selasky 1627fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 1628fe8c78f0SHans Petter Selasky if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1629fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1630fe8c78f0SHans Petter Selasky goto out; 1631fe8c78f0SHans Petter Selasky } 1632fe8c78f0SHans Petter Selasky 1633fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 1634*0e391a31SHans Petter Selasky mst = tls->snd_tag; 1635fe8c78f0SHans Petter Selasky tls->snd_tag = NULL; 1636*0e391a31SHans Petter Selasky if (mst != NULL) 1637*0e391a31SHans Petter Selasky m_snd_tag_rele(mst); 1638fe8c78f0SHans Petter Selasky 1639fe8c78f0SHans Petter Selasky ifp = tls->rx_ifp; 1640fe8c78f0SHans Petter Selasky if_ref(ifp); 1641fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1642fe8c78f0SHans Petter Selasky 1643fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 1644fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 1645fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 1646fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 1647fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 1648fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 1649fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = tls->rx_vlan_id; 1650fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1651fe8c78f0SHans Petter Selasky 1652fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 1653fe8c78f0SHans Petter Selasky if ((ifp->if_capenable2 & IFCAP2_RXTLS6) == 0) 1654fe8c78f0SHans Petter Selasky goto out; 1655fe8c78f0SHans Petter Selasky } else { 1656fe8c78f0SHans Petter Selasky if ((ifp->if_capenable2 & IFCAP2_RXTLS4) == 0) 1657fe8c78f0SHans Petter Selasky goto out; 1658fe8c78f0SHans Petter Selasky } 1659fe8c78f0SHans Petter Selasky 1660fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, &mst); 1661fe8c78f0SHans Petter Selasky if (error == 0) { 1662fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 1663fe8c78f0SHans Petter Selasky tls->snd_tag = mst; 1664fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1665fe8c78f0SHans Petter Selasky 1666fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset, 1); 1667fe8c78f0SHans Petter Selasky } else { 1668fe8c78f0SHans Petter Selasky /* 1669fe8c78f0SHans Petter Selasky * Just fall back to software decryption if a tag 1670fe8c78f0SHans Petter Selasky * cannot be allocated leaving the connection intact. 1671fe8c78f0SHans Petter Selasky * If a future input path change switches to another 1672fe8c78f0SHans Petter Selasky * interface this connection will resume ifnet TLS. 1673fe8c78f0SHans Petter Selasky */ 1674fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset_failed, 1); 1675fe8c78f0SHans Petter Selasky } 1676fe8c78f0SHans Petter Selasky 1677fe8c78f0SHans Petter Selasky out: 1678fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1679fe8c78f0SHans Petter Selasky tls->reset_pending = false; 1680fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1681fe8c78f0SHans Petter Selasky 1682fe8c78f0SHans Petter Selasky if (ifp != NULL) 1683fe8c78f0SHans Petter Selasky if_rele(ifp); 1684fe8c78f0SHans Petter Selasky sorele(so); 1685fe8c78f0SHans Petter Selasky ktls_free(tls); 1686fe8c78f0SHans Petter Selasky } 1687fe8c78f0SHans Petter Selasky 1688fe8c78f0SHans Petter Selasky /* 1689b2e60773SJohn Baldwin * Try to allocate a new TLS send tag. This task is scheduled when 1690b2e60773SJohn Baldwin * ip_output detects a route change while trying to transmit a packet 1691b2e60773SJohn Baldwin * holding a TLS record. If a new tag is allocated, replace the tag 1692b2e60773SJohn Baldwin * in the TLS session. Subsequent packets on the connection will use 1693b2e60773SJohn Baldwin * the new tag. If a new tag cannot be allocated, drop the 1694b2e60773SJohn Baldwin * connection. 1695b2e60773SJohn Baldwin */ 1696b2e60773SJohn Baldwin static void 1697b2e60773SJohn Baldwin ktls_reset_send_tag(void *context, int pending) 1698b2e60773SJohn Baldwin { 1699b2e60773SJohn Baldwin struct epoch_tracker et; 1700b2e60773SJohn Baldwin struct ktls_session *tls; 1701b2e60773SJohn Baldwin struct m_snd_tag *old, *new; 1702b2e60773SJohn Baldwin struct inpcb *inp; 1703b2e60773SJohn Baldwin struct tcpcb *tp; 1704b2e60773SJohn Baldwin int error; 1705b2e60773SJohn Baldwin 1706b2e60773SJohn Baldwin MPASS(pending == 1); 1707b2e60773SJohn Baldwin 1708b2e60773SJohn Baldwin tls = context; 1709b2e60773SJohn Baldwin inp = tls->inp; 1710b2e60773SJohn Baldwin 1711b2e60773SJohn Baldwin /* 1712b2e60773SJohn Baldwin * Free the old tag first before allocating a new one. 1713b2e60773SJohn Baldwin * ip[6]_output_send() will treat a NULL send tag the same as 1714b2e60773SJohn Baldwin * an ifp mismatch and drop packets until a new tag is 1715b2e60773SJohn Baldwin * allocated. 1716b2e60773SJohn Baldwin * 1717b2e60773SJohn Baldwin * Write-lock the INP when changing tls->snd_tag since 1718b2e60773SJohn Baldwin * ip[6]_output_send() holds a read-lock when reading the 1719b2e60773SJohn Baldwin * pointer. 1720b2e60773SJohn Baldwin */ 1721b2e60773SJohn Baldwin INP_WLOCK(inp); 1722b2e60773SJohn Baldwin old = tls->snd_tag; 1723b2e60773SJohn Baldwin tls->snd_tag = NULL; 1724b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1725b2e60773SJohn Baldwin if (old != NULL) 1726b2e60773SJohn Baldwin m_snd_tag_rele(old); 1727b2e60773SJohn Baldwin 1728b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(inp, tls, true, &new); 1729b2e60773SJohn Baldwin 1730b2e60773SJohn Baldwin if (error == 0) { 1731b2e60773SJohn Baldwin INP_WLOCK(inp); 1732b2e60773SJohn Baldwin tls->snd_tag = new; 1733b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1734b2e60773SJohn Baldwin tls->reset_pending = false; 1735b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1736b2e60773SJohn Baldwin if (!in_pcbrele_wlocked(inp)) 1737b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1738b2e60773SJohn Baldwin 1739b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset, 1); 1740b2e60773SJohn Baldwin 1741b2e60773SJohn Baldwin /* 1742b2e60773SJohn Baldwin * XXX: Should we kick tcp_output explicitly now that 1743b2e60773SJohn Baldwin * the send tag is fixed or just rely on timers? 1744b2e60773SJohn Baldwin */ 1745b2e60773SJohn Baldwin } else { 17461a496125SGleb Smirnoff NET_EPOCH_ENTER(et); 1747b2e60773SJohn Baldwin INP_WLOCK(inp); 1748b2e60773SJohn Baldwin if (!in_pcbrele_wlocked(inp)) { 1749b2e60773SJohn Baldwin if (!(inp->inp_flags & INP_TIMEWAIT) && 1750b2e60773SJohn Baldwin !(inp->inp_flags & INP_DROPPED)) { 1751b2e60773SJohn Baldwin tp = intotcpcb(inp); 17521f69a509SHans Petter Selasky CURVNET_SET(tp->t_vnet); 1753b2e60773SJohn Baldwin tp = tcp_drop(tp, ECONNABORTED); 17541f69a509SHans Petter Selasky CURVNET_RESTORE(); 1755b2e60773SJohn Baldwin if (tp != NULL) 1756b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1757b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_dropped, 1); 1758b2e60773SJohn Baldwin } else 1759b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1760b2e60773SJohn Baldwin } 17611a496125SGleb Smirnoff NET_EPOCH_EXIT(et); 1762b2e60773SJohn Baldwin 1763b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_failed, 1); 1764b2e60773SJohn Baldwin 1765b2e60773SJohn Baldwin /* 1766b2e60773SJohn Baldwin * Leave reset_pending true to avoid future tasks while 1767b2e60773SJohn Baldwin * the socket goes away. 1768b2e60773SJohn Baldwin */ 1769b2e60773SJohn Baldwin } 1770b2e60773SJohn Baldwin 1771b2e60773SJohn Baldwin ktls_free(tls); 1772b2e60773SJohn Baldwin } 1773b2e60773SJohn Baldwin 1774fe8c78f0SHans Petter Selasky void 1775fe8c78f0SHans Petter Selasky ktls_input_ifp_mismatch(struct sockbuf *sb, struct ifnet *ifp) 1776fe8c78f0SHans Petter Selasky { 1777fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1778fe8c78f0SHans Petter Selasky struct socket *so; 1779fe8c78f0SHans Petter Selasky 1780fe8c78f0SHans Petter Selasky SOCKBUF_LOCK_ASSERT(sb); 1781fe8c78f0SHans Petter Selasky KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 1782fe8c78f0SHans Petter Selasky __func__, sb)); 1783fe8c78f0SHans Petter Selasky so = __containerof(sb, struct socket, so_rcv); 1784fe8c78f0SHans Petter Selasky 1785fe8c78f0SHans Petter Selasky tls = sb->sb_tls_info; 1786fe8c78f0SHans Petter Selasky if_rele(tls->rx_ifp); 1787fe8c78f0SHans Petter Selasky if_ref(ifp); 1788fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 1789fe8c78f0SHans Petter Selasky 1790fe8c78f0SHans Petter Selasky /* 1791fe8c78f0SHans Petter Selasky * See if we should schedule a task to update the receive tag for 1792fe8c78f0SHans Petter Selasky * this session. 1793fe8c78f0SHans Petter Selasky */ 1794fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1795fe8c78f0SHans Petter Selasky if (!tls->reset_pending) { 1796fe8c78f0SHans Petter Selasky (void) ktls_hold(tls); 1797fe8c78f0SHans Petter Selasky soref(so); 1798fe8c78f0SHans Petter Selasky tls->so = so; 1799fe8c78f0SHans Petter Selasky tls->reset_pending = true; 1800fe8c78f0SHans Petter Selasky taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1801fe8c78f0SHans Petter Selasky } 1802fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1803fe8c78f0SHans Petter Selasky } 1804fe8c78f0SHans Petter Selasky 1805b2e60773SJohn Baldwin int 1806b2e60773SJohn Baldwin ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls) 1807b2e60773SJohn Baldwin { 1808b2e60773SJohn Baldwin 1809b2e60773SJohn Baldwin if (inp == NULL) 1810b2e60773SJohn Baldwin return (ENOBUFS); 1811b2e60773SJohn Baldwin 1812b2e60773SJohn Baldwin INP_LOCK_ASSERT(inp); 1813b2e60773SJohn Baldwin 1814b2e60773SJohn Baldwin /* 1815b2e60773SJohn Baldwin * See if we should schedule a task to update the send tag for 1816b2e60773SJohn Baldwin * this session. 1817b2e60773SJohn Baldwin */ 1818b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1819b2e60773SJohn Baldwin if (!tls->reset_pending) { 1820b2e60773SJohn Baldwin (void) ktls_hold(tls); 1821b2e60773SJohn Baldwin in_pcbref(inp); 1822b2e60773SJohn Baldwin tls->inp = inp; 1823b2e60773SJohn Baldwin tls->reset_pending = true; 1824b2e60773SJohn Baldwin taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1825b2e60773SJohn Baldwin } 1826b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1827b2e60773SJohn Baldwin return (ENOBUFS); 1828b2e60773SJohn Baldwin } 1829521eac97SJohn Baldwin 1830521eac97SJohn Baldwin #ifdef RATELIMIT 1831521eac97SJohn Baldwin int 1832521eac97SJohn Baldwin ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate) 1833521eac97SJohn Baldwin { 1834521eac97SJohn Baldwin union if_snd_tag_modify_params params = { 1835521eac97SJohn Baldwin .rate_limit.max_rate = max_pacing_rate, 1836521eac97SJohn Baldwin .rate_limit.flags = M_NOWAIT, 1837521eac97SJohn Baldwin }; 1838521eac97SJohn Baldwin struct m_snd_tag *mst; 1839521eac97SJohn Baldwin 1840521eac97SJohn Baldwin /* Can't get to the inp, but it should be locked. */ 1841521eac97SJohn Baldwin /* INP_LOCK_ASSERT(inp); */ 1842521eac97SJohn Baldwin 1843521eac97SJohn Baldwin MPASS(tls->mode == TCP_TLS_MODE_IFNET); 1844521eac97SJohn Baldwin 1845521eac97SJohn Baldwin if (tls->snd_tag == NULL) { 1846521eac97SJohn Baldwin /* 1847521eac97SJohn Baldwin * Resetting send tag, ignore this change. The 1848521eac97SJohn Baldwin * pending reset may or may not see this updated rate 1849521eac97SJohn Baldwin * in the tcpcb. If it doesn't, we will just lose 1850521eac97SJohn Baldwin * this rate change. 1851521eac97SJohn Baldwin */ 1852521eac97SJohn Baldwin return (0); 1853521eac97SJohn Baldwin } 1854521eac97SJohn Baldwin 1855521eac97SJohn Baldwin mst = tls->snd_tag; 1856f0fca646SHans Petter Selasky 1857f0fca646SHans Petter Selasky MPASS(mst != NULL); 1858f0fca646SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT); 1859f0fca646SHans Petter Selasky 1860c782ea8bSJohn Baldwin return (mst->sw->snd_tag_modify(mst, ¶ms)); 1861521eac97SJohn Baldwin } 1862521eac97SJohn Baldwin #endif 1863b2e60773SJohn Baldwin #endif 1864b2e60773SJohn Baldwin 1865b2e60773SJohn Baldwin void 1866b2e60773SJohn Baldwin ktls_destroy(struct ktls_session *tls) 1867b2e60773SJohn Baldwin { 1868b2e60773SJohn Baldwin 18699f03d2c0SJohn Baldwin if (tls->sequential_records) { 18709f03d2c0SJohn Baldwin struct mbuf *m, *n; 18719f03d2c0SJohn Baldwin int page_count; 18729f03d2c0SJohn Baldwin 18739f03d2c0SJohn Baldwin STAILQ_FOREACH_SAFE(m, &tls->pending_records, m_epg_stailq, n) { 18749f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 18759f03d2c0SJohn Baldwin while (page_count > 0) { 18769f03d2c0SJohn Baldwin KASSERT(page_count >= m->m_epg_nrdy, 18779f03d2c0SJohn Baldwin ("%s: too few pages", __func__)); 18789f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 18799f03d2c0SJohn Baldwin m = m_free(m); 18809f03d2c0SJohn Baldwin } 18819f03d2c0SJohn Baldwin } 18829f03d2c0SJohn Baldwin } 1883b2e60773SJohn Baldwin ktls_cleanup(tls); 1884b2e60773SJohn Baldwin uma_zfree(ktls_session_zone, tls); 1885b2e60773SJohn Baldwin } 1886b2e60773SJohn Baldwin 1887b2e60773SJohn Baldwin void 1888b2e60773SJohn Baldwin ktls_seq(struct sockbuf *sb, struct mbuf *m) 1889b2e60773SJohn Baldwin { 1890b2e60773SJohn Baldwin 1891b2e60773SJohn Baldwin for (; m != NULL; m = m->m_next) { 18926edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 1893b2e60773SJohn Baldwin ("ktls_seq: mapped mbuf %p", m)); 1894b2e60773SJohn Baldwin 18957b6c99d0SGleb Smirnoff m->m_epg_seqno = sb->sb_tls_seqno; 1896b2e60773SJohn Baldwin sb->sb_tls_seqno++; 1897b2e60773SJohn Baldwin } 1898b2e60773SJohn Baldwin } 1899b2e60773SJohn Baldwin 1900b2e60773SJohn Baldwin /* 1901b2e60773SJohn Baldwin * Add TLS framing (headers and trailers) to a chain of mbufs. Each 1902b2e60773SJohn Baldwin * mbuf in the chain must be an unmapped mbuf. The payload of the 1903b2e60773SJohn Baldwin * mbuf must be populated with the payload of each TLS record. 1904b2e60773SJohn Baldwin * 1905b2e60773SJohn Baldwin * The record_type argument specifies the TLS record type used when 1906b2e60773SJohn Baldwin * populating the TLS header. 1907b2e60773SJohn Baldwin * 1908b2e60773SJohn Baldwin * The enq_count argument on return is set to the number of pages of 1909b2e60773SJohn Baldwin * payload data for this entire chain that need to be encrypted via SW 1910b2e60773SJohn Baldwin * encryption. The returned value should be passed to ktls_enqueue 1911c2a8fd6fSJohn Baldwin * when scheduling encryption of this chain of mbufs. To handle the 1912c2a8fd6fSJohn Baldwin * special case of empty fragments for TLS 1.0 sessions, an empty 1913c2a8fd6fSJohn Baldwin * fragment counts as one page. 1914b2e60773SJohn Baldwin */ 1915f85e1a80SGleb Smirnoff void 1916b2e60773SJohn Baldwin ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt, 1917b2e60773SJohn Baldwin uint8_t record_type) 1918b2e60773SJohn Baldwin { 1919b2e60773SJohn Baldwin struct tls_record_layer *tlshdr; 1920b2e60773SJohn Baldwin struct mbuf *m; 19217d29eb9aSJohn Baldwin uint64_t *noncep; 1922b2e60773SJohn Baldwin uint16_t tls_len; 1923a90b85ddSMateusz Guzik int maxlen __diagused; 1924b2e60773SJohn Baldwin 1925b2e60773SJohn Baldwin maxlen = tls->params.max_frame_len; 1926b2e60773SJohn Baldwin *enq_cnt = 0; 1927b2e60773SJohn Baldwin for (m = top; m != NULL; m = m->m_next) { 1928b2e60773SJohn Baldwin /* 1929c2a8fd6fSJohn Baldwin * All mbufs in the chain should be TLS records whose 1930c2a8fd6fSJohn Baldwin * payload does not exceed the maximum frame length. 1931c2a8fd6fSJohn Baldwin * 19325de79eedSMark Johnston * Empty TLS 1.0 records are permitted when using CBC. 1933b2e60773SJohn Baldwin */ 19345de79eedSMark Johnston KASSERT(m->m_len <= maxlen && m->m_len >= 0 && 19355de79eedSMark Johnston (m->m_len > 0 || ktls_permit_empty_frames(tls)), 19365de79eedSMark Johnston ("ktls_frame: m %p len %d", m, m->m_len)); 1937c2a8fd6fSJohn Baldwin 1938b2e60773SJohn Baldwin /* 1939b2e60773SJohn Baldwin * TLS frames require unmapped mbufs to store session 1940b2e60773SJohn Baldwin * info. 1941b2e60773SJohn Baldwin */ 19426edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 19435de79eedSMark Johnston ("ktls_frame: mapped mbuf %p (top = %p)", m, top)); 1944b2e60773SJohn Baldwin 1945f85e1a80SGleb Smirnoff tls_len = m->m_len; 1946b2e60773SJohn Baldwin 1947b2e60773SJohn Baldwin /* Save a reference to the session. */ 19487b6c99d0SGleb Smirnoff m->m_epg_tls = ktls_hold(tls); 1949b2e60773SJohn Baldwin 19507b6c99d0SGleb Smirnoff m->m_epg_hdrlen = tls->params.tls_hlen; 19517b6c99d0SGleb Smirnoff m->m_epg_trllen = tls->params.tls_tlen; 1952b2e60773SJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) { 1953b2e60773SJohn Baldwin int bs, delta; 1954b2e60773SJohn Baldwin 1955b2e60773SJohn Baldwin /* 1956b2e60773SJohn Baldwin * AES-CBC pads messages to a multiple of the 1957b2e60773SJohn Baldwin * block size. Note that the padding is 1958b2e60773SJohn Baldwin * applied after the digest and the encryption 1959b2e60773SJohn Baldwin * is done on the "plaintext || mac || padding". 1960b2e60773SJohn Baldwin * At least one byte of padding is always 1961b2e60773SJohn Baldwin * present. 1962b2e60773SJohn Baldwin * 1963b2e60773SJohn Baldwin * Compute the final trailer length assuming 1964b2e60773SJohn Baldwin * at most one block of padding. 196521e3c1fbSJohn Baldwin * tls->params.tls_tlen is the maximum 1966b2e60773SJohn Baldwin * possible trailer length (padding + digest). 1967b2e60773SJohn Baldwin * delta holds the number of excess padding 1968b2e60773SJohn Baldwin * bytes if the maximum were used. Those 1969b2e60773SJohn Baldwin * extra bytes are removed. 1970b2e60773SJohn Baldwin */ 1971b2e60773SJohn Baldwin bs = tls->params.tls_bs; 1972b2e60773SJohn Baldwin delta = (tls_len + tls->params.tls_tlen) & (bs - 1); 19737b6c99d0SGleb Smirnoff m->m_epg_trllen -= delta; 1974b2e60773SJohn Baldwin } 19757b6c99d0SGleb Smirnoff m->m_len += m->m_epg_hdrlen + m->m_epg_trllen; 1976b2e60773SJohn Baldwin 1977b2e60773SJohn Baldwin /* Populate the TLS header. */ 19780c103266SGleb Smirnoff tlshdr = (void *)m->m_epg_hdr; 1979b2e60773SJohn Baldwin tlshdr->tls_vmajor = tls->params.tls_vmajor; 19806554362cSAndrew Gallatin 19816554362cSAndrew Gallatin /* 19826554362cSAndrew Gallatin * TLS 1.3 masquarades as TLS 1.2 with a record type 19836554362cSAndrew Gallatin * of TLS_RLTYPE_APP. 19846554362cSAndrew Gallatin */ 19856554362cSAndrew Gallatin if (tls->params.tls_vminor == TLS_MINOR_VER_THREE && 19866554362cSAndrew Gallatin tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) { 19876554362cSAndrew Gallatin tlshdr->tls_vminor = TLS_MINOR_VER_TWO; 19886554362cSAndrew Gallatin tlshdr->tls_type = TLS_RLTYPE_APP; 19896554362cSAndrew Gallatin /* save the real record type for later */ 19907b6c99d0SGleb Smirnoff m->m_epg_record_type = record_type; 19910c103266SGleb Smirnoff m->m_epg_trail[0] = record_type; 19926554362cSAndrew Gallatin } else { 1993b2e60773SJohn Baldwin tlshdr->tls_vminor = tls->params.tls_vminor; 1994b2e60773SJohn Baldwin tlshdr->tls_type = record_type; 19956554362cSAndrew Gallatin } 1996b2e60773SJohn Baldwin tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr)); 1997b2e60773SJohn Baldwin 1998b2e60773SJohn Baldwin /* 19997d29eb9aSJohn Baldwin * Store nonces / explicit IVs after the end of the 20007d29eb9aSJohn Baldwin * TLS header. 20017d29eb9aSJohn Baldwin * 20027d29eb9aSJohn Baldwin * For GCM with TLS 1.2, an 8 byte nonce is copied 20037d29eb9aSJohn Baldwin * from the end of the IV. The nonce is then 20047d29eb9aSJohn Baldwin * incremented for use by the next record. 20057d29eb9aSJohn Baldwin * 20067d29eb9aSJohn Baldwin * For CBC, a random nonce is inserted for TLS 1.1+. 2007b2e60773SJohn Baldwin */ 20087d29eb9aSJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 20097d29eb9aSJohn Baldwin tls->params.tls_vminor == TLS_MINOR_VER_TWO) { 20107d29eb9aSJohn Baldwin noncep = (uint64_t *)(tls->params.iv + 8); 20117d29eb9aSJohn Baldwin be64enc(tlshdr + 1, *noncep); 20127d29eb9aSJohn Baldwin (*noncep)++; 20137d29eb9aSJohn Baldwin } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 2014b2e60773SJohn Baldwin tls->params.tls_vminor >= TLS_MINOR_VER_ONE) 2015b2e60773SJohn Baldwin arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0); 2016b2e60773SJohn Baldwin 2017b2e60773SJohn Baldwin /* 2018b2e60773SJohn Baldwin * When using SW encryption, mark the mbuf not ready. 2019b2e60773SJohn Baldwin * It will be marked ready via sbready() after the 2020b2e60773SJohn Baldwin * record has been encrypted. 2021b2e60773SJohn Baldwin * 2022b2e60773SJohn Baldwin * When using ifnet TLS, unencrypted TLS records are 2023b2e60773SJohn Baldwin * sent down the stack to the NIC. 2024b2e60773SJohn Baldwin */ 20259e14430dSJohn Baldwin if (tls->mode == TCP_TLS_MODE_SW) { 2026b2e60773SJohn Baldwin m->m_flags |= M_NOTREADY; 2027c2a8fd6fSJohn Baldwin if (__predict_false(tls_len == 0)) { 2028c2a8fd6fSJohn Baldwin /* TLS 1.0 empty fragment. */ 2029d16cb228SJohn Baldwin m->m_epg_nrdy = 1; 2030c2a8fd6fSJohn Baldwin } else 2031d16cb228SJohn Baldwin m->m_epg_nrdy = m->m_epg_npgs; 2032d16cb228SJohn Baldwin *enq_cnt += m->m_epg_nrdy; 2033b2e60773SJohn Baldwin } 2034b2e60773SJohn Baldwin } 2035b2e60773SJohn Baldwin } 2036b2e60773SJohn Baldwin 20375de79eedSMark Johnston bool 20385de79eedSMark Johnston ktls_permit_empty_frames(struct ktls_session *tls) 20395de79eedSMark Johnston { 20405de79eedSMark Johnston return (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 20415de79eedSMark Johnston tls->params.tls_vminor == TLS_MINOR_VER_ZERO); 20425de79eedSMark Johnston } 20435de79eedSMark Johnston 2044b2e60773SJohn Baldwin void 20453c0e5685SJohn Baldwin ktls_check_rx(struct sockbuf *sb) 20463c0e5685SJohn Baldwin { 20473c0e5685SJohn Baldwin struct tls_record_layer hdr; 20483c0e5685SJohn Baldwin struct ktls_wq *wq; 20493c0e5685SJohn Baldwin struct socket *so; 20503c0e5685SJohn Baldwin bool running; 20513c0e5685SJohn Baldwin 20523c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 20533c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 20543c0e5685SJohn Baldwin __func__, sb)); 20553c0e5685SJohn Baldwin so = __containerof(sb, struct socket, so_rcv); 20563c0e5685SJohn Baldwin 20573c0e5685SJohn Baldwin if (sb->sb_flags & SB_TLS_RX_RUNNING) 20583c0e5685SJohn Baldwin return; 20593c0e5685SJohn Baldwin 20603c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 20613c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr)) { 20623c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0) 20633c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 20643c0e5685SJohn Baldwin return; 20653c0e5685SJohn Baldwin } 20663c0e5685SJohn Baldwin 20673c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr); 20683c0e5685SJohn Baldwin 20693c0e5685SJohn Baldwin /* Is the entire record queued? */ 20703c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) { 20713c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0) 20723c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 20733c0e5685SJohn Baldwin return; 20743c0e5685SJohn Baldwin } 20753c0e5685SJohn Baldwin 20763c0e5685SJohn Baldwin sb->sb_flags |= SB_TLS_RX_RUNNING; 20773c0e5685SJohn Baldwin 20783c0e5685SJohn Baldwin soref(so); 20793c0e5685SJohn Baldwin wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index]; 20803c0e5685SJohn Baldwin mtx_lock(&wq->mtx); 20813c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list); 20823c0e5685SJohn Baldwin running = wq->running; 20833c0e5685SJohn Baldwin mtx_unlock(&wq->mtx); 20843c0e5685SJohn Baldwin if (!running) 20853c0e5685SJohn Baldwin wakeup(wq); 20863c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, 1); 20873c0e5685SJohn Baldwin } 20883c0e5685SJohn Baldwin 20893c0e5685SJohn Baldwin static struct mbuf * 20903c0e5685SJohn Baldwin ktls_detach_record(struct sockbuf *sb, int len) 20913c0e5685SJohn Baldwin { 20923c0e5685SJohn Baldwin struct mbuf *m, *n, *top; 20933c0e5685SJohn Baldwin int remain; 20943c0e5685SJohn Baldwin 20953c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 20963c0e5685SJohn Baldwin MPASS(len <= sb->sb_tlscc); 20973c0e5685SJohn Baldwin 20983c0e5685SJohn Baldwin /* 20993c0e5685SJohn Baldwin * If TLS chain is the exact size of the record, 21003c0e5685SJohn Baldwin * just grab the whole record. 21013c0e5685SJohn Baldwin */ 21023c0e5685SJohn Baldwin top = sb->sb_mtls; 21033c0e5685SJohn Baldwin if (sb->sb_tlscc == len) { 21043c0e5685SJohn Baldwin sb->sb_mtls = NULL; 21053c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 21063c0e5685SJohn Baldwin goto out; 21073c0e5685SJohn Baldwin } 21083c0e5685SJohn Baldwin 21093c0e5685SJohn Baldwin /* 21103c0e5685SJohn Baldwin * While it would be nice to use m_split() here, we need 21113c0e5685SJohn Baldwin * to know exactly what m_split() allocates to update the 21123c0e5685SJohn Baldwin * accounting, so do it inline instead. 21133c0e5685SJohn Baldwin */ 21143c0e5685SJohn Baldwin remain = len; 21153c0e5685SJohn Baldwin for (m = top; remain > m->m_len; m = m->m_next) 21163c0e5685SJohn Baldwin remain -= m->m_len; 21173c0e5685SJohn Baldwin 21183c0e5685SJohn Baldwin /* Easy case: don't have to split 'm'. */ 21193c0e5685SJohn Baldwin if (remain == m->m_len) { 21203c0e5685SJohn Baldwin sb->sb_mtls = m->m_next; 21213c0e5685SJohn Baldwin if (sb->sb_mtls == NULL) 21223c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 21233c0e5685SJohn Baldwin m->m_next = NULL; 21243c0e5685SJohn Baldwin goto out; 21253c0e5685SJohn Baldwin } 21263c0e5685SJohn Baldwin 21273c0e5685SJohn Baldwin /* 21283c0e5685SJohn Baldwin * Need to allocate an mbuf to hold the remainder of 'm'. Try 21293c0e5685SJohn Baldwin * with M_NOWAIT first. 21303c0e5685SJohn Baldwin */ 21313c0e5685SJohn Baldwin n = m_get(M_NOWAIT, MT_DATA); 21323c0e5685SJohn Baldwin if (n == NULL) { 21333c0e5685SJohn Baldwin /* 21343c0e5685SJohn Baldwin * Use M_WAITOK with socket buffer unlocked. If 21353c0e5685SJohn Baldwin * 'sb_mtls' changes while the lock is dropped, return 21363c0e5685SJohn Baldwin * NULL to force the caller to retry. 21373c0e5685SJohn Baldwin */ 21383c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 21393c0e5685SJohn Baldwin 21403c0e5685SJohn Baldwin n = m_get(M_WAITOK, MT_DATA); 21413c0e5685SJohn Baldwin 21423c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 21433c0e5685SJohn Baldwin if (sb->sb_mtls != top) { 21443c0e5685SJohn Baldwin m_free(n); 21453c0e5685SJohn Baldwin return (NULL); 21463c0e5685SJohn Baldwin } 21473c0e5685SJohn Baldwin } 2148fe8c78f0SHans Petter Selasky n->m_flags |= (m->m_flags & (M_NOTREADY | M_DECRYPTED)); 21493c0e5685SJohn Baldwin 21503c0e5685SJohn Baldwin /* Store remainder in 'n'. */ 21513c0e5685SJohn Baldwin n->m_len = m->m_len - remain; 21523c0e5685SJohn Baldwin if (m->m_flags & M_EXT) { 21533c0e5685SJohn Baldwin n->m_data = m->m_data + remain; 21543c0e5685SJohn Baldwin mb_dupcl(n, m); 21553c0e5685SJohn Baldwin } else { 21563c0e5685SJohn Baldwin bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len); 21573c0e5685SJohn Baldwin } 21583c0e5685SJohn Baldwin 21593c0e5685SJohn Baldwin /* Trim 'm' and update accounting. */ 21603c0e5685SJohn Baldwin m->m_len -= n->m_len; 21613c0e5685SJohn Baldwin sb->sb_tlscc -= n->m_len; 21623c0e5685SJohn Baldwin sb->sb_ccc -= n->m_len; 21633c0e5685SJohn Baldwin 21643c0e5685SJohn Baldwin /* Account for 'n'. */ 21653c0e5685SJohn Baldwin sballoc_ktls_rx(sb, n); 21663c0e5685SJohn Baldwin 21673c0e5685SJohn Baldwin /* Insert 'n' into the TLS chain. */ 21683c0e5685SJohn Baldwin sb->sb_mtls = n; 21693c0e5685SJohn Baldwin n->m_next = m->m_next; 21703c0e5685SJohn Baldwin if (sb->sb_mtlstail == m) 21713c0e5685SJohn Baldwin sb->sb_mtlstail = n; 21723c0e5685SJohn Baldwin 21733c0e5685SJohn Baldwin /* Detach the record from the TLS chain. */ 21743c0e5685SJohn Baldwin m->m_next = NULL; 21753c0e5685SJohn Baldwin 21763c0e5685SJohn Baldwin out: 21773c0e5685SJohn Baldwin MPASS(m_length(top, NULL) == len); 21783c0e5685SJohn Baldwin for (m = top; m != NULL; m = m->m_next) 21793c0e5685SJohn Baldwin sbfree_ktls_rx(sb, m); 21803c0e5685SJohn Baldwin sb->sb_tlsdcc = len; 21813c0e5685SJohn Baldwin sb->sb_ccc += len; 21823c0e5685SJohn Baldwin SBCHECK(sb); 21833c0e5685SJohn Baldwin return (top); 21843c0e5685SJohn Baldwin } 21853c0e5685SJohn Baldwin 218605a1d0f5SJohn Baldwin /* 218705a1d0f5SJohn Baldwin * Determine the length of the trailing zero padding and find the real 218805a1d0f5SJohn Baldwin * record type in the byte before the padding. 218905a1d0f5SJohn Baldwin * 219005a1d0f5SJohn Baldwin * Walking the mbuf chain backwards is clumsy, so another option would 219105a1d0f5SJohn Baldwin * be to scan forwards remembering the last non-zero byte before the 219205a1d0f5SJohn Baldwin * trailer. However, it would be expensive to scan the entire record. 219305a1d0f5SJohn Baldwin * Instead, find the last non-zero byte of each mbuf in the chain 219405a1d0f5SJohn Baldwin * keeping track of the relative offset of that nonzero byte. 219505a1d0f5SJohn Baldwin * 219605a1d0f5SJohn Baldwin * trail_len is the size of the MAC/tag on input and is set to the 219705a1d0f5SJohn Baldwin * size of the full trailer including padding and the record type on 219805a1d0f5SJohn Baldwin * return. 219905a1d0f5SJohn Baldwin */ 220005a1d0f5SJohn Baldwin static int 220105a1d0f5SJohn Baldwin tls13_find_record_type(struct ktls_session *tls, struct mbuf *m, int tls_len, 220205a1d0f5SJohn Baldwin int *trailer_len, uint8_t *record_typep) 220305a1d0f5SJohn Baldwin { 220405a1d0f5SJohn Baldwin char *cp; 220505a1d0f5SJohn Baldwin u_int digest_start, last_offset, m_len, offset; 220605a1d0f5SJohn Baldwin uint8_t record_type; 220705a1d0f5SJohn Baldwin 220805a1d0f5SJohn Baldwin digest_start = tls_len - *trailer_len; 220905a1d0f5SJohn Baldwin last_offset = 0; 221005a1d0f5SJohn Baldwin offset = 0; 221105a1d0f5SJohn Baldwin for (; m != NULL && offset < digest_start; 221205a1d0f5SJohn Baldwin offset += m->m_len, m = m->m_next) { 221305a1d0f5SJohn Baldwin /* Don't look for padding in the tag. */ 221405a1d0f5SJohn Baldwin m_len = min(digest_start - offset, m->m_len); 221505a1d0f5SJohn Baldwin cp = mtod(m, char *); 221605a1d0f5SJohn Baldwin 221705a1d0f5SJohn Baldwin /* Find last non-zero byte in this mbuf. */ 221805a1d0f5SJohn Baldwin while (m_len > 0 && cp[m_len - 1] == 0) 221905a1d0f5SJohn Baldwin m_len--; 222005a1d0f5SJohn Baldwin if (m_len > 0) { 222105a1d0f5SJohn Baldwin record_type = cp[m_len - 1]; 222205a1d0f5SJohn Baldwin last_offset = offset + m_len; 222305a1d0f5SJohn Baldwin } 222405a1d0f5SJohn Baldwin } 222505a1d0f5SJohn Baldwin if (last_offset < tls->params.tls_hlen) 222605a1d0f5SJohn Baldwin return (EBADMSG); 222705a1d0f5SJohn Baldwin 222805a1d0f5SJohn Baldwin *record_typep = record_type; 222905a1d0f5SJohn Baldwin *trailer_len = tls_len - last_offset + 1; 223005a1d0f5SJohn Baldwin return (0); 223105a1d0f5SJohn Baldwin } 223205a1d0f5SJohn Baldwin 2233fe8c78f0SHans Petter Selasky /* 2234fe8c78f0SHans Petter Selasky * Check if a mbuf chain is fully decrypted at the given offset and 2235fe8c78f0SHans Petter Selasky * length. Returns KTLS_MBUF_CRYPTO_ST_DECRYPTED if all data is 2236fe8c78f0SHans Petter Selasky * decrypted. KTLS_MBUF_CRYPTO_ST_MIXED if there is a mix of encrypted 2237fe8c78f0SHans Petter Selasky * and decrypted data. Else KTLS_MBUF_CRYPTO_ST_ENCRYPTED if all data 2238fe8c78f0SHans Petter Selasky * is encrypted. 2239fe8c78f0SHans Petter Selasky */ 2240fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t 2241fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_state(struct mbuf *mb, int offset, int len) 2242fe8c78f0SHans Petter Selasky { 2243fe8c78f0SHans Petter Selasky int m_flags_ored = 0; 2244fe8c78f0SHans Petter Selasky int m_flags_anded = -1; 2245fe8c78f0SHans Petter Selasky 2246fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2247fe8c78f0SHans Petter Selasky if (offset < mb->m_len) 2248fe8c78f0SHans Petter Selasky break; 2249fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2250fe8c78f0SHans Petter Selasky } 2251fe8c78f0SHans Petter Selasky offset += len; 2252fe8c78f0SHans Petter Selasky 2253fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2254fe8c78f0SHans Petter Selasky m_flags_ored |= mb->m_flags; 2255fe8c78f0SHans Petter Selasky m_flags_anded &= mb->m_flags; 2256fe8c78f0SHans Petter Selasky 2257fe8c78f0SHans Petter Selasky if (offset <= mb->m_len) 2258fe8c78f0SHans Petter Selasky break; 2259fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2260fe8c78f0SHans Petter Selasky } 2261fe8c78f0SHans Petter Selasky MPASS(mb != NULL || offset == 0); 2262fe8c78f0SHans Petter Selasky 2263fe8c78f0SHans Petter Selasky if ((m_flags_ored ^ m_flags_anded) & M_DECRYPTED) 2264fe8c78f0SHans Petter Selasky return (KTLS_MBUF_CRYPTO_ST_MIXED); 2265fe8c78f0SHans Petter Selasky else 2266fe8c78f0SHans Petter Selasky return ((m_flags_ored & M_DECRYPTED) ? 2267fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_DECRYPTED : 2268fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_ENCRYPTED); 2269fe8c78f0SHans Petter Selasky } 2270fe8c78f0SHans Petter Selasky 2271fe8c78f0SHans Petter Selasky /* 2272fe8c78f0SHans Petter Selasky * ktls_resync_ifnet - get HW TLS RX back on track after packet loss 2273fe8c78f0SHans Petter Selasky */ 2274fe8c78f0SHans Petter Selasky static int 2275fe8c78f0SHans Petter Selasky ktls_resync_ifnet(struct socket *so, uint32_t tls_len, uint64_t tls_rcd_num) 2276fe8c78f0SHans Petter Selasky { 2277fe8c78f0SHans Petter Selasky union if_snd_tag_modify_params params; 2278fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 2279fe8c78f0SHans Petter Selasky struct inpcb *inp; 2280fe8c78f0SHans Petter Selasky struct tcpcb *tp; 2281fe8c78f0SHans Petter Selasky 2282fe8c78f0SHans Petter Selasky mst = so->so_rcv.sb_tls_info->snd_tag; 2283fe8c78f0SHans Petter Selasky if (__predict_false(mst == NULL)) 2284fe8c78f0SHans Petter Selasky return (EINVAL); 2285fe8c78f0SHans Petter Selasky 2286fe8c78f0SHans Petter Selasky inp = sotoinpcb(so); 2287fe8c78f0SHans Petter Selasky if (__predict_false(inp == NULL)) 2288fe8c78f0SHans Petter Selasky return (EINVAL); 2289fe8c78f0SHans Petter Selasky 2290fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 2291fe8c78f0SHans Petter Selasky if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2292fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2293fe8c78f0SHans Petter Selasky return (ECONNRESET); 2294fe8c78f0SHans Petter Selasky } 2295fe8c78f0SHans Petter Selasky 2296fe8c78f0SHans Petter Selasky tp = intotcpcb(inp); 2297fe8c78f0SHans Petter Selasky MPASS(tp != NULL); 2298fe8c78f0SHans Petter Selasky 2299fe8c78f0SHans Petter Selasky /* Get the TCP sequence number of the next valid TLS header. */ 2300fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 2301fe8c78f0SHans Petter Selasky params.tls_rx.tls_hdr_tcp_sn = 2302fe8c78f0SHans Petter Selasky tp->rcv_nxt - so->so_rcv.sb_tlscc - tls_len; 2303fe8c78f0SHans Petter Selasky params.tls_rx.tls_rec_length = tls_len; 2304fe8c78f0SHans Petter Selasky params.tls_rx.tls_seq_number = tls_rcd_num; 2305fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 2306fe8c78f0SHans Petter Selasky 2307fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2308fe8c78f0SHans Petter Selasky 2309fe8c78f0SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RX); 2310fe8c78f0SHans Petter Selasky return (mst->sw->snd_tag_modify(mst, ¶ms)); 2311fe8c78f0SHans Petter Selasky } 2312fe8c78f0SHans Petter Selasky 23133c0e5685SJohn Baldwin static void 23143c0e5685SJohn Baldwin ktls_decrypt(struct socket *so) 23153c0e5685SJohn Baldwin { 23163c0e5685SJohn Baldwin char tls_header[MBUF_PEXT_HDR_LEN]; 23173c0e5685SJohn Baldwin struct ktls_session *tls; 23183c0e5685SJohn Baldwin struct sockbuf *sb; 23193c0e5685SJohn Baldwin struct tls_record_layer *hdr; 23203c0e5685SJohn Baldwin struct tls_get_record tgr; 23213c0e5685SJohn Baldwin struct mbuf *control, *data, *m; 2322fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t state; 23233c0e5685SJohn Baldwin uint64_t seqno; 23243c0e5685SJohn Baldwin int error, remain, tls_len, trail_len; 232505a1d0f5SJohn Baldwin bool tls13; 232605a1d0f5SJohn Baldwin uint8_t vminor, record_type; 23273c0e5685SJohn Baldwin 23283c0e5685SJohn Baldwin hdr = (struct tls_record_layer *)tls_header; 23293c0e5685SJohn Baldwin sb = &so->so_rcv; 23303c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 23313c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING, 23323c0e5685SJohn Baldwin ("%s: socket %p not running", __func__, so)); 23333c0e5685SJohn Baldwin 23343c0e5685SJohn Baldwin tls = sb->sb_tls_info; 23353c0e5685SJohn Baldwin MPASS(tls != NULL); 23363c0e5685SJohn Baldwin 233705a1d0f5SJohn Baldwin tls13 = (tls->params.tls_vminor == TLS_MINOR_VER_THREE); 233805a1d0f5SJohn Baldwin if (tls13) 233905a1d0f5SJohn Baldwin vminor = TLS_MINOR_VER_TWO; 234005a1d0f5SJohn Baldwin else 234105a1d0f5SJohn Baldwin vminor = tls->params.tls_vminor; 23423c0e5685SJohn Baldwin for (;;) { 23433c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 23443c0e5685SJohn Baldwin if (sb->sb_tlscc < tls->params.tls_hlen) 23453c0e5685SJohn Baldwin break; 23463c0e5685SJohn Baldwin 23473c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header); 23483c0e5685SJohn Baldwin tls_len = sizeof(*hdr) + ntohs(hdr->tls_length); 23493c0e5685SJohn Baldwin 23503c0e5685SJohn Baldwin if (hdr->tls_vmajor != tls->params.tls_vmajor || 235105a1d0f5SJohn Baldwin hdr->tls_vminor != vminor) 235205a1d0f5SJohn Baldwin error = EINVAL; 235305a1d0f5SJohn Baldwin else if (tls13 && hdr->tls_type != TLS_RLTYPE_APP) 23543c0e5685SJohn Baldwin error = EINVAL; 23553c0e5685SJohn Baldwin else if (tls_len < tls->params.tls_hlen || tls_len > 23563c0e5685SJohn Baldwin tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 + 23573c0e5685SJohn Baldwin tls->params.tls_tlen) 23583c0e5685SJohn Baldwin error = EMSGSIZE; 23593c0e5685SJohn Baldwin else 23603c0e5685SJohn Baldwin error = 0; 23613c0e5685SJohn Baldwin if (__predict_false(error != 0)) { 23623c0e5685SJohn Baldwin /* 23633c0e5685SJohn Baldwin * We have a corrupted record and are likely 23643c0e5685SJohn Baldwin * out of sync. The connection isn't 23653c0e5685SJohn Baldwin * recoverable at this point, so abort it. 23663c0e5685SJohn Baldwin */ 23673c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 23683c0e5685SJohn Baldwin counter_u64_add(ktls_offload_corrupted_records, 1); 23693c0e5685SJohn Baldwin 23703c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 2371e7d02be1SGleb Smirnoff so->so_proto->pr_abort(so); 23723c0e5685SJohn Baldwin so->so_error = error; 23733c0e5685SJohn Baldwin CURVNET_RESTORE(); 23743c0e5685SJohn Baldwin goto deref; 23753c0e5685SJohn Baldwin } 23763c0e5685SJohn Baldwin 23773c0e5685SJohn Baldwin /* Is the entire record queued? */ 23783c0e5685SJohn Baldwin if (sb->sb_tlscc < tls_len) 23793c0e5685SJohn Baldwin break; 23803c0e5685SJohn Baldwin 23813c0e5685SJohn Baldwin /* 23823c0e5685SJohn Baldwin * Split out the portion of the mbuf chain containing 23833c0e5685SJohn Baldwin * this TLS record. 23843c0e5685SJohn Baldwin */ 23853c0e5685SJohn Baldwin data = ktls_detach_record(sb, tls_len); 23863c0e5685SJohn Baldwin if (data == NULL) 23873c0e5685SJohn Baldwin continue; 23883c0e5685SJohn Baldwin MPASS(sb->sb_tlsdcc == tls_len); 23893c0e5685SJohn Baldwin 23903c0e5685SJohn Baldwin seqno = sb->sb_tls_seqno; 23913c0e5685SJohn Baldwin sb->sb_tls_seqno++; 23923c0e5685SJohn Baldwin SBCHECK(sb); 23933c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 23943c0e5685SJohn Baldwin 2395fe8c78f0SHans Petter Selasky /* get crypto state for this TLS record */ 2396fe8c78f0SHans Petter Selasky state = ktls_mbuf_crypto_state(data, 0, tls_len); 2397fe8c78f0SHans Petter Selasky 2398fe8c78f0SHans Petter Selasky switch (state) { 2399fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_MIXED: 2400fe8c78f0SHans Petter Selasky error = ktls_ocf_recrypt(tls, hdr, data, seqno); 2401fe8c78f0SHans Petter Selasky if (error) 2402fe8c78f0SHans Petter Selasky break; 2403fe8c78f0SHans Petter Selasky /* FALLTHROUGH */ 2404fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_ENCRYPTED: 2405fe8c78f0SHans Petter Selasky error = ktls_ocf_decrypt(tls, hdr, data, seqno, 2406fe8c78f0SHans Petter Selasky &trail_len); 2407fe8c78f0SHans Petter Selasky if (__predict_true(error == 0)) { 2408fe8c78f0SHans Petter Selasky if (tls13) { 240905a1d0f5SJohn Baldwin error = tls13_find_record_type(tls, data, 241005a1d0f5SJohn Baldwin tls_len, &trail_len, &record_type); 2411fe8c78f0SHans Petter Selasky } else { 241205a1d0f5SJohn Baldwin record_type = hdr->tls_type; 241305a1d0f5SJohn Baldwin } 2414fe8c78f0SHans Petter Selasky } 2415fe8c78f0SHans Petter Selasky break; 2416fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_DECRYPTED: 2417fe8c78f0SHans Petter Selasky /* 2418fe8c78f0SHans Petter Selasky * NIC TLS is only supported for AEAD 2419fe8c78f0SHans Petter Selasky * ciphersuites which used a fixed sized 2420fe8c78f0SHans Petter Selasky * trailer. 2421fe8c78f0SHans Petter Selasky */ 2422fe8c78f0SHans Petter Selasky if (tls13) { 2423fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen - 1; 2424fe8c78f0SHans Petter Selasky error = tls13_find_record_type(tls, data, 2425fe8c78f0SHans Petter Selasky tls_len, &trail_len, &record_type); 2426fe8c78f0SHans Petter Selasky } else { 2427fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen; 2428fe8c78f0SHans Petter Selasky error = 0; 2429fe8c78f0SHans Petter Selasky record_type = hdr->tls_type; 2430fe8c78f0SHans Petter Selasky } 2431fe8c78f0SHans Petter Selasky break; 2432fe8c78f0SHans Petter Selasky default: 2433fe8c78f0SHans Petter Selasky error = EINVAL; 2434fe8c78f0SHans Petter Selasky break; 2435fe8c78f0SHans Petter Selasky } 24363c0e5685SJohn Baldwin if (error) { 24373c0e5685SJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 24383c0e5685SJohn Baldwin 24393c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 24403c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 24413c0e5685SJohn Baldwin /* 24423c0e5685SJohn Baldwin * sbcut/drop/flush discarded these 24433c0e5685SJohn Baldwin * mbufs. 24443c0e5685SJohn Baldwin */ 24453c0e5685SJohn Baldwin m_freem(data); 24463c0e5685SJohn Baldwin break; 24473c0e5685SJohn Baldwin } 24483c0e5685SJohn Baldwin 24493c0e5685SJohn Baldwin /* 24503c0e5685SJohn Baldwin * Drop this TLS record's data, but keep 24513c0e5685SJohn Baldwin * decrypting subsequent records. 24523c0e5685SJohn Baldwin */ 24533c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 24543c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 24553c0e5685SJohn Baldwin 24563c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 24573c0e5685SJohn Baldwin so->so_error = EBADMSG; 24583c0e5685SJohn Baldwin sorwakeup_locked(so); 24593c0e5685SJohn Baldwin CURVNET_RESTORE(); 24603c0e5685SJohn Baldwin 24613c0e5685SJohn Baldwin m_freem(data); 24623c0e5685SJohn Baldwin 24633c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 24643c0e5685SJohn Baldwin continue; 24653c0e5685SJohn Baldwin } 24663c0e5685SJohn Baldwin 24673c0e5685SJohn Baldwin /* Allocate the control mbuf. */ 24686be8944dSMark Johnston memset(&tgr, 0, sizeof(tgr)); 246905a1d0f5SJohn Baldwin tgr.tls_type = record_type; 24703c0e5685SJohn Baldwin tgr.tls_vmajor = hdr->tls_vmajor; 24713c0e5685SJohn Baldwin tgr.tls_vminor = hdr->tls_vminor; 24723c0e5685SJohn Baldwin tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen - 24733c0e5685SJohn Baldwin trail_len); 2474b46667c6SGleb Smirnoff control = sbcreatecontrol(&tgr, sizeof(tgr), 24753c0e5685SJohn Baldwin TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK); 24763c0e5685SJohn Baldwin 24773c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 24783c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 24793c0e5685SJohn Baldwin /* sbcut/drop/flush discarded these mbufs. */ 24803c0e5685SJohn Baldwin MPASS(sb->sb_tlscc == 0); 24813c0e5685SJohn Baldwin m_freem(data); 24823c0e5685SJohn Baldwin m_freem(control); 24833c0e5685SJohn Baldwin break; 24843c0e5685SJohn Baldwin } 24853c0e5685SJohn Baldwin 24863c0e5685SJohn Baldwin /* 24873c0e5685SJohn Baldwin * Clear the 'dcc' accounting in preparation for 24883c0e5685SJohn Baldwin * adding the decrypted record. 24893c0e5685SJohn Baldwin */ 24903c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 24913c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 24923c0e5685SJohn Baldwin SBCHECK(sb); 24933c0e5685SJohn Baldwin 24943c0e5685SJohn Baldwin /* If there is no payload, drop all of the data. */ 24953c0e5685SJohn Baldwin if (tgr.tls_length == htobe16(0)) { 24963c0e5685SJohn Baldwin m_freem(data); 24973c0e5685SJohn Baldwin data = NULL; 24983c0e5685SJohn Baldwin } else { 24993c0e5685SJohn Baldwin /* Trim header. */ 25003c0e5685SJohn Baldwin remain = tls->params.tls_hlen; 25013c0e5685SJohn Baldwin while (remain > 0) { 25023c0e5685SJohn Baldwin if (data->m_len > remain) { 25033c0e5685SJohn Baldwin data->m_data += remain; 25043c0e5685SJohn Baldwin data->m_len -= remain; 25053c0e5685SJohn Baldwin break; 25063c0e5685SJohn Baldwin } 25073c0e5685SJohn Baldwin remain -= data->m_len; 25083c0e5685SJohn Baldwin data = m_free(data); 25093c0e5685SJohn Baldwin } 25103c0e5685SJohn Baldwin 25113c0e5685SJohn Baldwin /* Trim trailer and clear M_NOTREADY. */ 25123c0e5685SJohn Baldwin remain = be16toh(tgr.tls_length); 25133c0e5685SJohn Baldwin m = data; 25143c0e5685SJohn Baldwin for (m = data; remain > m->m_len; m = m->m_next) { 2515fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 25163c0e5685SJohn Baldwin remain -= m->m_len; 25173c0e5685SJohn Baldwin } 25183c0e5685SJohn Baldwin m->m_len = remain; 25193c0e5685SJohn Baldwin m_freem(m->m_next); 25203c0e5685SJohn Baldwin m->m_next = NULL; 2521fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 25223c0e5685SJohn Baldwin 25233c0e5685SJohn Baldwin /* Set EOR on the final mbuf. */ 25243c0e5685SJohn Baldwin m->m_flags |= M_EOR; 25253c0e5685SJohn Baldwin } 25263c0e5685SJohn Baldwin 25273c0e5685SJohn Baldwin sbappendcontrol_locked(sb, data, control, 0); 2528fe8c78f0SHans Petter Selasky 2529fe8c78f0SHans Petter Selasky if (__predict_false(state != KTLS_MBUF_CRYPTO_ST_DECRYPTED)) { 2530fe8c78f0SHans Petter Selasky sb->sb_flags |= SB_TLS_RX_RESYNC; 2531fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2532fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, tls_len, seqno); 2533fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2534fe8c78f0SHans Petter Selasky } else if (__predict_false(sb->sb_flags & SB_TLS_RX_RESYNC)) { 2535fe8c78f0SHans Petter Selasky sb->sb_flags &= ~SB_TLS_RX_RESYNC; 2536fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2537fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, 0, seqno); 2538fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2539fe8c78f0SHans Petter Selasky } 25403c0e5685SJohn Baldwin } 25413c0e5685SJohn Baldwin 25423c0e5685SJohn Baldwin sb->sb_flags &= ~SB_TLS_RX_RUNNING; 25433c0e5685SJohn Baldwin 25443c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0) 25453c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 25463c0e5685SJohn Baldwin 25473c0e5685SJohn Baldwin sorwakeup_locked(so); 25483c0e5685SJohn Baldwin 25493c0e5685SJohn Baldwin deref: 25503c0e5685SJohn Baldwin SOCKBUF_UNLOCK_ASSERT(sb); 25513c0e5685SJohn Baldwin 25523c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 25533c0e5685SJohn Baldwin sorele(so); 25543c0e5685SJohn Baldwin CURVNET_RESTORE(); 25553c0e5685SJohn Baldwin } 25563c0e5685SJohn Baldwin 25573c0e5685SJohn Baldwin void 2558d90fe9d0SGleb Smirnoff ktls_enqueue_to_free(struct mbuf *m) 2559b2e60773SJohn Baldwin { 2560b2e60773SJohn Baldwin struct ktls_wq *wq; 2561b2e60773SJohn Baldwin bool running; 2562b2e60773SJohn Baldwin 2563b2e60773SJohn Baldwin /* Mark it for freeing. */ 25647b6c99d0SGleb Smirnoff m->m_epg_flags |= EPG_FLAG_2FREE; 25657b6c99d0SGleb Smirnoff wq = &ktls_wq[m->m_epg_tls->wq_index]; 2566b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 25673c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 2568b2e60773SJohn Baldwin running = wq->running; 2569b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2570b2e60773SJohn Baldwin if (!running) 2571b2e60773SJohn Baldwin wakeup(wq); 2572b2e60773SJohn Baldwin } 2573b2e60773SJohn Baldwin 257449f6925cSMark Johnston static void * 257549f6925cSMark Johnston ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m) 257649f6925cSMark Johnston { 257749f6925cSMark Johnston void *buf; 257898215005SAndrew Gallatin int domain, running; 257949f6925cSMark Johnston 258049f6925cSMark Johnston if (m->m_epg_npgs <= 2) 258149f6925cSMark Johnston return (NULL); 258249f6925cSMark Johnston if (ktls_buffer_zone == NULL) 258349f6925cSMark Johnston return (NULL); 258449f6925cSMark Johnston if ((u_int)(ticks - wq->lastallocfail) < hz) { 258549f6925cSMark Johnston /* 258649f6925cSMark Johnston * Rate-limit allocation attempts after a failure. 258749f6925cSMark Johnston * ktls_buffer_import() will acquire a per-domain mutex to check 258849f6925cSMark Johnston * the free page queues and may fail consistently if memory is 258949f6925cSMark Johnston * fragmented. 259049f6925cSMark Johnston */ 259149f6925cSMark Johnston return (NULL); 259249f6925cSMark Johnston } 259349f6925cSMark Johnston buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM); 259498215005SAndrew Gallatin if (buf == NULL) { 259598215005SAndrew Gallatin domain = PCPU_GET(domain); 259649f6925cSMark Johnston wq->lastallocfail = ticks; 259798215005SAndrew Gallatin 259898215005SAndrew Gallatin /* 259998215005SAndrew Gallatin * Note that this check is "racy", but the races are 260098215005SAndrew Gallatin * harmless, and are either a spurious wakeup if 260198215005SAndrew Gallatin * multiple threads fail allocations before the alloc 260298215005SAndrew Gallatin * thread wakes, or waiting an extra second in case we 260398215005SAndrew Gallatin * see an old value of running == true. 260498215005SAndrew Gallatin */ 260598215005SAndrew Gallatin if (!VM_DOMAIN_EMPTY(domain)) { 260698215005SAndrew Gallatin running = atomic_load_int(&ktls_domains[domain].alloc_td.running); 260798215005SAndrew Gallatin if (!running) 260898215005SAndrew Gallatin wakeup(&ktls_domains[domain].alloc_td); 260998215005SAndrew Gallatin } 261098215005SAndrew Gallatin } 261149f6925cSMark Johnston return (buf); 261249f6925cSMark Johnston } 261349f6925cSMark Johnston 2614470e851cSJohn Baldwin static int 2615470e851cSJohn Baldwin ktls_encrypt_record(struct ktls_wq *wq, struct mbuf *m, 2616470e851cSJohn Baldwin struct ktls_session *tls, struct ktls_ocf_encrypt_state *state) 2617470e851cSJohn Baldwin { 2618470e851cSJohn Baldwin vm_page_t pg; 2619470e851cSJohn Baldwin int error, i, len, off; 2620470e851cSJohn Baldwin 2621470e851cSJohn Baldwin KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == (M_EXTPG | M_NOTREADY), 2622470e851cSJohn Baldwin ("%p not unready & nomap mbuf\n", m)); 2623470e851cSJohn Baldwin KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen, 2624470e851cSJohn Baldwin ("page count %d larger than maximum frame length %d", m->m_epg_npgs, 2625470e851cSJohn Baldwin ktls_maxlen)); 2626470e851cSJohn Baldwin 2627470e851cSJohn Baldwin /* Anonymous mbufs are encrypted in place. */ 2628470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) != 0) 2629a4c5d490SJohn Baldwin return (ktls_ocf_encrypt(state, tls, m, NULL, 0)); 2630470e851cSJohn Baldwin 2631470e851cSJohn Baldwin /* 2632470e851cSJohn Baldwin * For file-backed mbufs (from sendfile), anonymous wired 2633470e851cSJohn Baldwin * pages are allocated and used as the encryption destination. 2634470e851cSJohn Baldwin */ 2635470e851cSJohn Baldwin if ((state->cbuf = ktls_buffer_alloc(wq, m)) != NULL) { 2636470e851cSJohn Baldwin len = ptoa(m->m_epg_npgs - 1) + m->m_epg_last_len - 2637470e851cSJohn Baldwin m->m_epg_1st_off; 2638470e851cSJohn Baldwin state->dst_iov[0].iov_base = (char *)state->cbuf + 2639470e851cSJohn Baldwin m->m_epg_1st_off; 2640470e851cSJohn Baldwin state->dst_iov[0].iov_len = len; 2641470e851cSJohn Baldwin state->parray[0] = DMAP_TO_PHYS((vm_offset_t)state->cbuf); 2642470e851cSJohn Baldwin i = 1; 2643470e851cSJohn Baldwin } else { 2644470e851cSJohn Baldwin off = m->m_epg_1st_off; 2645470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++, off = 0) { 2646a4667e09SMark Johnston pg = vm_page_alloc_noobj(VM_ALLOC_NODUMP | 2647a4667e09SMark Johnston VM_ALLOC_WIRED | VM_ALLOC_WAITOK); 2648470e851cSJohn Baldwin len = m_epg_pagelen(m, i, off); 2649470e851cSJohn Baldwin state->parray[i] = VM_PAGE_TO_PHYS(pg); 2650470e851cSJohn Baldwin state->dst_iov[i].iov_base = 2651470e851cSJohn Baldwin (char *)PHYS_TO_DMAP(state->parray[i]) + off; 2652470e851cSJohn Baldwin state->dst_iov[i].iov_len = len; 2653470e851cSJohn Baldwin } 2654470e851cSJohn Baldwin } 2655470e851cSJohn Baldwin KASSERT(i + 1 <= nitems(state->dst_iov), ("dst_iov is too small")); 2656470e851cSJohn Baldwin state->dst_iov[i].iov_base = m->m_epg_trail; 2657470e851cSJohn Baldwin state->dst_iov[i].iov_len = m->m_epg_trllen; 2658470e851cSJohn Baldwin 2659a4c5d490SJohn Baldwin error = ktls_ocf_encrypt(state, tls, m, state->dst_iov, i + 1); 2660470e851cSJohn Baldwin 2661470e851cSJohn Baldwin if (__predict_false(error != 0)) { 2662470e851cSJohn Baldwin /* Free the anonymous pages. */ 2663470e851cSJohn Baldwin if (state->cbuf != NULL) 2664470e851cSJohn Baldwin uma_zfree(ktls_buffer_zone, state->cbuf); 2665470e851cSJohn Baldwin else { 2666470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) { 2667470e851cSJohn Baldwin pg = PHYS_TO_VM_PAGE(state->parray[i]); 2668470e851cSJohn Baldwin (void)vm_page_unwire_noq(pg); 2669470e851cSJohn Baldwin vm_page_free(pg); 2670470e851cSJohn Baldwin } 2671470e851cSJohn Baldwin } 2672470e851cSJohn Baldwin } 2673470e851cSJohn Baldwin return (error); 2674470e851cSJohn Baldwin } 2675470e851cSJohn Baldwin 26769f03d2c0SJohn Baldwin /* Number of TLS records in a batch passed to ktls_enqueue(). */ 26779f03d2c0SJohn Baldwin static u_int 26789f03d2c0SJohn Baldwin ktls_batched_records(struct mbuf *m) 26799f03d2c0SJohn Baldwin { 26809f03d2c0SJohn Baldwin int page_count, records; 26819f03d2c0SJohn Baldwin 26829f03d2c0SJohn Baldwin records = 0; 26839f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 26849f03d2c0SJohn Baldwin while (page_count > 0) { 26859f03d2c0SJohn Baldwin records++; 26869f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 26879f03d2c0SJohn Baldwin m = m->m_next; 26889f03d2c0SJohn Baldwin } 26899f03d2c0SJohn Baldwin KASSERT(page_count == 0, ("%s: mismatched page count", __func__)); 26909f03d2c0SJohn Baldwin return (records); 26919f03d2c0SJohn Baldwin } 26929f03d2c0SJohn Baldwin 2693b2e60773SJohn Baldwin void 2694b2e60773SJohn Baldwin ktls_enqueue(struct mbuf *m, struct socket *so, int page_count) 2695b2e60773SJohn Baldwin { 26969f03d2c0SJohn Baldwin struct ktls_session *tls; 2697b2e60773SJohn Baldwin struct ktls_wq *wq; 26989f03d2c0SJohn Baldwin int queued; 2699b2e60773SJohn Baldwin bool running; 2700b2e60773SJohn Baldwin 27016edfd179SGleb Smirnoff KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) == 27026edfd179SGleb Smirnoff (M_EXTPG | M_NOTREADY)), 2703b2e60773SJohn Baldwin ("ktls_enqueue: %p not unready & nomap mbuf\n", m)); 2704b2e60773SJohn Baldwin KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count")); 2705b2e60773SJohn Baldwin 27067b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf")); 2707b2e60773SJohn Baldwin 27087b6c99d0SGleb Smirnoff m->m_epg_enc_cnt = page_count; 2709b2e60773SJohn Baldwin 2710b2e60773SJohn Baldwin /* 2711b2e60773SJohn Baldwin * Save a pointer to the socket. The caller is responsible 2712b2e60773SJohn Baldwin * for taking an additional reference via soref(). 2713b2e60773SJohn Baldwin */ 27147b6c99d0SGleb Smirnoff m->m_epg_so = so; 2715b2e60773SJohn Baldwin 27169f03d2c0SJohn Baldwin queued = 1; 27179f03d2c0SJohn Baldwin tls = m->m_epg_tls; 27189f03d2c0SJohn Baldwin wq = &ktls_wq[tls->wq_index]; 2719b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 27209f03d2c0SJohn Baldwin if (__predict_false(tls->sequential_records)) { 27219f03d2c0SJohn Baldwin /* 27229f03d2c0SJohn Baldwin * For TLS 1.0, records must be encrypted 27239f03d2c0SJohn Baldwin * sequentially. For a given connection, all records 27249f03d2c0SJohn Baldwin * queued to the associated work queue are processed 27259f03d2c0SJohn Baldwin * sequentially. However, sendfile(2) might complete 27269f03d2c0SJohn Baldwin * I/O requests spanning multiple TLS records out of 27279f03d2c0SJohn Baldwin * order. Here we ensure TLS records are enqueued to 27289f03d2c0SJohn Baldwin * the work queue in FIFO order. 27299f03d2c0SJohn Baldwin * 27309f03d2c0SJohn Baldwin * tls->next_seqno holds the sequence number of the 27319f03d2c0SJohn Baldwin * next TLS record that should be enqueued to the work 27329f03d2c0SJohn Baldwin * queue. If this next record is not tls->next_seqno, 27339f03d2c0SJohn Baldwin * it must be a future record, so insert it, sorted by 27349f03d2c0SJohn Baldwin * TLS sequence number, into tls->pending_records and 27359f03d2c0SJohn Baldwin * return. 27369f03d2c0SJohn Baldwin * 27379f03d2c0SJohn Baldwin * If this TLS record matches tls->next_seqno, place 27389f03d2c0SJohn Baldwin * it in the work queue and then check 27399f03d2c0SJohn Baldwin * tls->pending_records to see if any 27409f03d2c0SJohn Baldwin * previously-queued records are now ready for 27419f03d2c0SJohn Baldwin * encryption. 27429f03d2c0SJohn Baldwin */ 27439f03d2c0SJohn Baldwin if (m->m_epg_seqno != tls->next_seqno) { 27449f03d2c0SJohn Baldwin struct mbuf *n, *p; 27459f03d2c0SJohn Baldwin 27469f03d2c0SJohn Baldwin p = NULL; 27479f03d2c0SJohn Baldwin STAILQ_FOREACH(n, &tls->pending_records, m_epg_stailq) { 27489f03d2c0SJohn Baldwin if (n->m_epg_seqno > m->m_epg_seqno) 27499f03d2c0SJohn Baldwin break; 27509f03d2c0SJohn Baldwin p = n; 27519f03d2c0SJohn Baldwin } 27529f03d2c0SJohn Baldwin if (n == NULL) 27539f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&tls->pending_records, m, 27549f03d2c0SJohn Baldwin m_epg_stailq); 27559f03d2c0SJohn Baldwin else if (p == NULL) 27569f03d2c0SJohn Baldwin STAILQ_INSERT_HEAD(&tls->pending_records, m, 27579f03d2c0SJohn Baldwin m_epg_stailq); 27589f03d2c0SJohn Baldwin else 27599f03d2c0SJohn Baldwin STAILQ_INSERT_AFTER(&tls->pending_records, p, m, 27609f03d2c0SJohn Baldwin m_epg_stailq); 27619f03d2c0SJohn Baldwin mtx_unlock(&wq->mtx); 27629f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, 1); 27639f03d2c0SJohn Baldwin return; 27649f03d2c0SJohn Baldwin } 27659f03d2c0SJohn Baldwin 27669f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(m); 27673c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 27689f03d2c0SJohn Baldwin 27699f03d2c0SJohn Baldwin while (!STAILQ_EMPTY(&tls->pending_records)) { 27709f03d2c0SJohn Baldwin struct mbuf *n; 27719f03d2c0SJohn Baldwin 27729f03d2c0SJohn Baldwin n = STAILQ_FIRST(&tls->pending_records); 27739f03d2c0SJohn Baldwin if (n->m_epg_seqno != tls->next_seqno) 27749f03d2c0SJohn Baldwin break; 27759f03d2c0SJohn Baldwin 27769f03d2c0SJohn Baldwin queued++; 27779f03d2c0SJohn Baldwin STAILQ_REMOVE_HEAD(&tls->pending_records, m_epg_stailq); 27789f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(n); 27799f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, n, m_epg_stailq); 27809f03d2c0SJohn Baldwin } 27819f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, -(queued - 1)); 27829f03d2c0SJohn Baldwin } else 27839f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 27849f03d2c0SJohn Baldwin 2785b2e60773SJohn Baldwin running = wq->running; 2786b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2787b2e60773SJohn Baldwin if (!running) 2788b2e60773SJohn Baldwin wakeup(wq); 27899f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, queued); 2790b2e60773SJohn Baldwin } 2791b2e60773SJohn Baldwin 2792470e851cSJohn Baldwin /* 2793470e851cSJohn Baldwin * Once a file-backed mbuf (from sendfile) has been encrypted, free 2794470e851cSJohn Baldwin * the pages from the file and replace them with the anonymous pages 2795470e851cSJohn Baldwin * allocated in ktls_encrypt_record(). 2796470e851cSJohn Baldwin */ 2797470e851cSJohn Baldwin static void 2798470e851cSJohn Baldwin ktls_finish_nonanon(struct mbuf *m, struct ktls_ocf_encrypt_state *state) 2799470e851cSJohn Baldwin { 2800470e851cSJohn Baldwin int i; 2801470e851cSJohn Baldwin 2802470e851cSJohn Baldwin MPASS((m->m_epg_flags & EPG_FLAG_ANON) == 0); 2803470e851cSJohn Baldwin 2804470e851cSJohn Baldwin /* Free the old pages. */ 2805470e851cSJohn Baldwin m->m_ext.ext_free(m); 2806470e851cSJohn Baldwin 2807470e851cSJohn Baldwin /* Replace them with the new pages. */ 2808470e851cSJohn Baldwin if (state->cbuf != NULL) { 2809470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 2810470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[0] + ptoa(i); 2811470e851cSJohn Baldwin 2812470e851cSJohn Baldwin /* Contig pages should go back to the cache. */ 2813470e851cSJohn Baldwin m->m_ext.ext_free = ktls_free_mext_contig; 2814470e851cSJohn Baldwin } else { 2815470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 2816470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[i]; 2817470e851cSJohn Baldwin 2818470e851cSJohn Baldwin /* Use the basic free routine. */ 2819470e851cSJohn Baldwin m->m_ext.ext_free = mb_free_mext_pgs; 2820470e851cSJohn Baldwin } 2821470e851cSJohn Baldwin 2822470e851cSJohn Baldwin /* Pages are now writable. */ 2823470e851cSJohn Baldwin m->m_epg_flags |= EPG_FLAG_ANON; 2824470e851cSJohn Baldwin } 28256b313a3aSJohn Baldwin 2826b2e60773SJohn Baldwin static __noinline void 282749f6925cSMark Johnston ktls_encrypt(struct ktls_wq *wq, struct mbuf *top) 2828b2e60773SJohn Baldwin { 2829470e851cSJohn Baldwin struct ktls_ocf_encrypt_state state; 2830b2e60773SJohn Baldwin struct ktls_session *tls; 2831b2e60773SJohn Baldwin struct socket *so; 2832d90fe9d0SGleb Smirnoff struct mbuf *m; 2833470e851cSJohn Baldwin int error, npages, total_pages; 2834b2e60773SJohn Baldwin 28357b6c99d0SGleb Smirnoff so = top->m_epg_so; 28367b6c99d0SGleb Smirnoff tls = top->m_epg_tls; 2837d90fe9d0SGleb Smirnoff KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 2838d90fe9d0SGleb Smirnoff KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 2839b2e60773SJohn Baldwin #ifdef INVARIANTS 28407b6c99d0SGleb Smirnoff top->m_epg_so = NULL; 2841b2e60773SJohn Baldwin #endif 28427b6c99d0SGleb Smirnoff total_pages = top->m_epg_enc_cnt; 2843b2e60773SJohn Baldwin npages = 0; 2844b2e60773SJohn Baldwin 2845b2e60773SJohn Baldwin /* 2846b2e60773SJohn Baldwin * Encrypt the TLS records in the chain of mbufs starting with 2847b2e60773SJohn Baldwin * 'top'. 'total_pages' gives us a total count of pages and is 2848b2e60773SJohn Baldwin * used to know when we have finished encrypting the TLS 2849b2e60773SJohn Baldwin * records originally queued with 'top'. 2850b2e60773SJohn Baldwin * 2851b2e60773SJohn Baldwin * NB: These mbufs are queued in the socket buffer and 2852b2e60773SJohn Baldwin * 'm_next' is traversing the mbufs in the socket buffer. The 2853b2e60773SJohn Baldwin * socket buffer lock is not held while traversing this chain. 2854b2e60773SJohn Baldwin * Since the mbufs are all marked M_NOTREADY their 'm_next' 2855b2e60773SJohn Baldwin * pointers should be stable. However, the 'm_next' of the 2856b2e60773SJohn Baldwin * last mbuf encrypted is not necessarily NULL. It can point 2857b2e60773SJohn Baldwin * to other mbufs appended while 'top' was on the TLS work 2858b2e60773SJohn Baldwin * queue. 2859b2e60773SJohn Baldwin * 2860b2e60773SJohn Baldwin * Each mbuf holds an entire TLS record. 2861b2e60773SJohn Baldwin */ 2862b2e60773SJohn Baldwin error = 0; 2863b2e60773SJohn Baldwin for (m = top; npages != total_pages; m = m->m_next) { 28647b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls == tls, 2865b2e60773SJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 28667b6c99d0SGleb Smirnoff tls, m->m_epg_tls)); 28677b6c99d0SGleb Smirnoff KASSERT(npages + m->m_epg_npgs <= total_pages, 2868b2e60773SJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 2869b2e60773SJohn Baldwin total_pages, m)); 2870b2e60773SJohn Baldwin 2871470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, &state); 287221e3c1fbSJohn Baldwin if (error) { 287321e3c1fbSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 287421e3c1fbSJohn Baldwin break; 287521e3c1fbSJohn Baldwin } 287621e3c1fbSJohn Baldwin 2877470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 2878470e851cSJohn Baldwin ktls_finish_nonanon(m, &state); 2879470e851cSJohn Baldwin 2880d16cb228SJohn Baldwin npages += m->m_epg_nrdy; 2881b2e60773SJohn Baldwin 2882b2e60773SJohn Baldwin /* 2883b2e60773SJohn Baldwin * Drop a reference to the session now that it is no 2884b2e60773SJohn Baldwin * longer needed. Existing code depends on encrypted 2885b2e60773SJohn Baldwin * records having no associated session vs 2886b2e60773SJohn Baldwin * yet-to-be-encrypted records having an associated 2887b2e60773SJohn Baldwin * session. 2888b2e60773SJohn Baldwin */ 28897b6c99d0SGleb Smirnoff m->m_epg_tls = NULL; 2890b2e60773SJohn Baldwin ktls_free(tls); 2891b2e60773SJohn Baldwin } 2892b2e60773SJohn Baldwin 2893b2e60773SJohn Baldwin CURVNET_SET(so->so_vnet); 2894b2e60773SJohn Baldwin if (error == 0) { 2895e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, top, npages); 2896b2e60773SJohn Baldwin } else { 2897e7d02be1SGleb Smirnoff so->so_proto->pr_abort(so); 2898b2e60773SJohn Baldwin so->so_error = EIO; 2899b2e60773SJohn Baldwin mb_free_notready(top, total_pages); 2900b2e60773SJohn Baldwin } 2901b2e60773SJohn Baldwin 2902b2e60773SJohn Baldwin sorele(so); 2903b2e60773SJohn Baldwin CURVNET_RESTORE(); 2904b2e60773SJohn Baldwin } 2905b2e60773SJohn Baldwin 2906470e851cSJohn Baldwin void 2907470e851cSJohn Baldwin ktls_encrypt_cb(struct ktls_ocf_encrypt_state *state, int error) 2908470e851cSJohn Baldwin { 2909470e851cSJohn Baldwin struct ktls_session *tls; 2910470e851cSJohn Baldwin struct socket *so; 2911470e851cSJohn Baldwin struct mbuf *m; 2912470e851cSJohn Baldwin int npages; 2913470e851cSJohn Baldwin 2914470e851cSJohn Baldwin m = state->m; 2915470e851cSJohn Baldwin 2916470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 2917470e851cSJohn Baldwin ktls_finish_nonanon(m, state); 2918470e851cSJohn Baldwin 2919470e851cSJohn Baldwin so = state->so; 2920470e851cSJohn Baldwin free(state, M_KTLS); 2921470e851cSJohn Baldwin 2922470e851cSJohn Baldwin /* 2923470e851cSJohn Baldwin * Drop a reference to the session now that it is no longer 2924470e851cSJohn Baldwin * needed. Existing code depends on encrypted records having 2925470e851cSJohn Baldwin * no associated session vs yet-to-be-encrypted records having 2926470e851cSJohn Baldwin * an associated session. 2927470e851cSJohn Baldwin */ 2928470e851cSJohn Baldwin tls = m->m_epg_tls; 2929470e851cSJohn Baldwin m->m_epg_tls = NULL; 2930470e851cSJohn Baldwin ktls_free(tls); 2931470e851cSJohn Baldwin 2932470e851cSJohn Baldwin if (error != 0) 2933470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 2934470e851cSJohn Baldwin 2935470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 2936470e851cSJohn Baldwin npages = m->m_epg_nrdy; 2937470e851cSJohn Baldwin 2938470e851cSJohn Baldwin if (error == 0) { 2939e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, m, npages); 2940470e851cSJohn Baldwin } else { 2941e7d02be1SGleb Smirnoff so->so_proto->pr_abort(so); 2942470e851cSJohn Baldwin so->so_error = EIO; 2943470e851cSJohn Baldwin mb_free_notready(m, npages); 2944470e851cSJohn Baldwin } 2945470e851cSJohn Baldwin 2946470e851cSJohn Baldwin sorele(so); 2947470e851cSJohn Baldwin CURVNET_RESTORE(); 2948470e851cSJohn Baldwin } 2949470e851cSJohn Baldwin 2950470e851cSJohn Baldwin /* 2951470e851cSJohn Baldwin * Similar to ktls_encrypt, but used with asynchronous OCF backends 2952470e851cSJohn Baldwin * (coprocessors) where encryption does not use host CPU resources and 2953470e851cSJohn Baldwin * it can be beneficial to queue more requests than CPUs. 2954470e851cSJohn Baldwin */ 2955470e851cSJohn Baldwin static __noinline void 2956470e851cSJohn Baldwin ktls_encrypt_async(struct ktls_wq *wq, struct mbuf *top) 2957470e851cSJohn Baldwin { 2958470e851cSJohn Baldwin struct ktls_ocf_encrypt_state *state; 2959470e851cSJohn Baldwin struct ktls_session *tls; 2960470e851cSJohn Baldwin struct socket *so; 2961470e851cSJohn Baldwin struct mbuf *m, *n; 2962470e851cSJohn Baldwin int error, mpages, npages, total_pages; 2963470e851cSJohn Baldwin 2964470e851cSJohn Baldwin so = top->m_epg_so; 2965470e851cSJohn Baldwin tls = top->m_epg_tls; 2966470e851cSJohn Baldwin KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 2967470e851cSJohn Baldwin KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 2968470e851cSJohn Baldwin #ifdef INVARIANTS 2969470e851cSJohn Baldwin top->m_epg_so = NULL; 2970470e851cSJohn Baldwin #endif 2971470e851cSJohn Baldwin total_pages = top->m_epg_enc_cnt; 2972470e851cSJohn Baldwin npages = 0; 2973470e851cSJohn Baldwin 2974470e851cSJohn Baldwin error = 0; 2975470e851cSJohn Baldwin for (m = top; npages != total_pages; m = n) { 2976470e851cSJohn Baldwin KASSERT(m->m_epg_tls == tls, 2977470e851cSJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 2978470e851cSJohn Baldwin tls, m->m_epg_tls)); 2979470e851cSJohn Baldwin KASSERT(npages + m->m_epg_npgs <= total_pages, 2980470e851cSJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 2981470e851cSJohn Baldwin total_pages, m)); 2982470e851cSJohn Baldwin 2983470e851cSJohn Baldwin state = malloc(sizeof(*state), M_KTLS, M_WAITOK | M_ZERO); 2984470e851cSJohn Baldwin soref(so); 2985470e851cSJohn Baldwin state->so = so; 2986470e851cSJohn Baldwin state->m = m; 2987470e851cSJohn Baldwin 2988470e851cSJohn Baldwin mpages = m->m_epg_nrdy; 2989470e851cSJohn Baldwin n = m->m_next; 2990470e851cSJohn Baldwin 2991470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, state); 2992470e851cSJohn Baldwin if (error) { 2993470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 2994470e851cSJohn Baldwin free(state, M_KTLS); 2995470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 2996470e851cSJohn Baldwin sorele(so); 2997470e851cSJohn Baldwin CURVNET_RESTORE(); 2998470e851cSJohn Baldwin break; 2999470e851cSJohn Baldwin } 3000470e851cSJohn Baldwin 3001470e851cSJohn Baldwin npages += mpages; 3002470e851cSJohn Baldwin } 3003470e851cSJohn Baldwin 3004470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3005470e851cSJohn Baldwin if (error != 0) { 3006e7d02be1SGleb Smirnoff so->so_proto->pr_abort(so); 3007470e851cSJohn Baldwin so->so_error = EIO; 3008470e851cSJohn Baldwin mb_free_notready(m, total_pages - npages); 3009470e851cSJohn Baldwin } 3010470e851cSJohn Baldwin 3011470e851cSJohn Baldwin sorele(so); 3012470e851cSJohn Baldwin CURVNET_RESTORE(); 3013470e851cSJohn Baldwin } 3014470e851cSJohn Baldwin 3015a72ee355SJohn Baldwin static int 3016a72ee355SJohn Baldwin ktls_bind_domain(int domain) 3017a72ee355SJohn Baldwin { 3018a72ee355SJohn Baldwin int error; 3019a72ee355SJohn Baldwin 3020a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &cpuset_domain[domain]); 3021a72ee355SJohn Baldwin if (error != 0) 3022a72ee355SJohn Baldwin return (error); 3023a72ee355SJohn Baldwin curthread->td_domain.dr_policy = DOMAINSET_PREF(domain); 3024a72ee355SJohn Baldwin return (0); 3025a72ee355SJohn Baldwin } 3026a72ee355SJohn Baldwin 3027b2e60773SJohn Baldwin static void 302898215005SAndrew Gallatin ktls_alloc_thread(void *ctx) 302998215005SAndrew Gallatin { 303098215005SAndrew Gallatin struct ktls_domain_info *ktls_domain = ctx; 303198215005SAndrew Gallatin struct ktls_alloc_thread *sc = &ktls_domain->alloc_td; 303298215005SAndrew Gallatin void **buf; 303398215005SAndrew Gallatin struct sysctl_oid *oid; 303498215005SAndrew Gallatin char name[80]; 3035a72ee355SJohn Baldwin int domain, error, i, nbufs; 303698215005SAndrew Gallatin 3037a72ee355SJohn Baldwin domain = ktls_domain - ktls_domains; 303898215005SAndrew Gallatin if (bootverbose) 3039a72ee355SJohn Baldwin printf("Starting KTLS alloc thread for domain %d\n", domain); 3040a72ee355SJohn Baldwin error = ktls_bind_domain(domain); 3041a72ee355SJohn Baldwin if (error) 3042a72ee355SJohn Baldwin printf("Unable to bind KTLS alloc thread for domain %d: error %d\n", 3043a72ee355SJohn Baldwin domain, error); 3044a72ee355SJohn Baldwin snprintf(name, sizeof(name), "domain%d", domain); 304598215005SAndrew Gallatin oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern_ipc_tls), OID_AUTO, 304698215005SAndrew Gallatin name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 304798215005SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "allocs", 304898215005SAndrew Gallatin CTLFLAG_RD, &sc->allocs, 0, "buffers allocated"); 304998215005SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "wakeups", 305098215005SAndrew Gallatin CTLFLAG_RD, &sc->wakeups, 0, "thread wakeups"); 305198215005SAndrew Gallatin SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "running", 305298215005SAndrew Gallatin CTLFLAG_RD, &sc->running, 0, "thread running"); 305398215005SAndrew Gallatin 305498215005SAndrew Gallatin buf = NULL; 305598215005SAndrew Gallatin nbufs = 0; 305698215005SAndrew Gallatin for (;;) { 305798215005SAndrew Gallatin atomic_store_int(&sc->running, 0); 305809066b98SAndrew Gallatin tsleep(sc, PZERO | PNOLOCK, "-", 0); 305998215005SAndrew Gallatin atomic_store_int(&sc->running, 1); 306098215005SAndrew Gallatin sc->wakeups++; 306198215005SAndrew Gallatin if (nbufs != ktls_max_alloc) { 306298215005SAndrew Gallatin free(buf, M_KTLS); 306398215005SAndrew Gallatin nbufs = atomic_load_int(&ktls_max_alloc); 306498215005SAndrew Gallatin buf = malloc(sizeof(void *) * nbufs, M_KTLS, 306598215005SAndrew Gallatin M_WAITOK | M_ZERO); 306698215005SAndrew Gallatin } 306798215005SAndrew Gallatin /* 306898215005SAndrew Gallatin * Below we allocate nbufs with different allocation 306998215005SAndrew Gallatin * flags than we use when allocating normally during 307098215005SAndrew Gallatin * encryption in the ktls worker thread. We specify 307198215005SAndrew Gallatin * M_NORECLAIM in the worker thread. However, we omit 307298215005SAndrew Gallatin * that flag here and add M_WAITOK so that the VM 307398215005SAndrew Gallatin * system is permitted to perform expensive work to 307498215005SAndrew Gallatin * defragment memory. We do this here, as it does not 307598215005SAndrew Gallatin * matter if this thread blocks. If we block a ktls 307698215005SAndrew Gallatin * worker thread, we risk developing backlogs of 307798215005SAndrew Gallatin * buffers to be encrypted, leading to surges of 307898215005SAndrew Gallatin * traffic and potential NIC output drops. 307998215005SAndrew Gallatin */ 308098215005SAndrew Gallatin for (i = 0; i < nbufs; i++) { 308198215005SAndrew Gallatin buf[i] = uma_zalloc(ktls_buffer_zone, M_WAITOK); 308298215005SAndrew Gallatin sc->allocs++; 308398215005SAndrew Gallatin } 308498215005SAndrew Gallatin for (i = 0; i < nbufs; i++) { 308598215005SAndrew Gallatin uma_zfree(ktls_buffer_zone, buf[i]); 308698215005SAndrew Gallatin buf[i] = NULL; 308798215005SAndrew Gallatin } 308898215005SAndrew Gallatin } 308998215005SAndrew Gallatin } 309098215005SAndrew Gallatin 309198215005SAndrew Gallatin static void 3092b2e60773SJohn Baldwin ktls_work_thread(void *ctx) 3093b2e60773SJohn Baldwin { 3094b2e60773SJohn Baldwin struct ktls_wq *wq = ctx; 3095d90fe9d0SGleb Smirnoff struct mbuf *m, *n; 30963c0e5685SJohn Baldwin struct socket *so, *son; 30973c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) local_m_head; 30983c0e5685SJohn Baldwin STAILQ_HEAD(, socket) local_so_head; 3099a72ee355SJohn Baldwin int cpu; 3100a72ee355SJohn Baldwin 3101a72ee355SJohn Baldwin cpu = wq - ktls_wq; 3102a72ee355SJohn Baldwin if (bootverbose) 3103a72ee355SJohn Baldwin printf("Starting KTLS worker thread for CPU %d\n", cpu); 3104a72ee355SJohn Baldwin 3105a72ee355SJohn Baldwin /* 3106a72ee355SJohn Baldwin * Bind to a core. If ktls_bind_threads is > 1, then 3107a72ee355SJohn Baldwin * we bind to the NUMA domain instead. 3108a72ee355SJohn Baldwin */ 3109a72ee355SJohn Baldwin if (ktls_bind_threads) { 3110a72ee355SJohn Baldwin int error; 3111b2e60773SJohn Baldwin 311202bc3865SAndrew Gallatin if (ktls_bind_threads > 1) { 3113a72ee355SJohn Baldwin struct pcpu *pc = pcpu_find(cpu); 3114a72ee355SJohn Baldwin 3115a72ee355SJohn Baldwin error = ktls_bind_domain(pc->pc_domain); 3116a72ee355SJohn Baldwin } else { 3117a72ee355SJohn Baldwin cpuset_t mask; 3118a72ee355SJohn Baldwin 3119a72ee355SJohn Baldwin CPU_SETOF(cpu, &mask); 3120a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &mask); 3121a72ee355SJohn Baldwin } 3122a72ee355SJohn Baldwin if (error) 3123a72ee355SJohn Baldwin printf("Unable to bind KTLS worker thread for CPU %d: error %d\n", 3124a72ee355SJohn Baldwin cpu, error); 312502bc3865SAndrew Gallatin } 3126b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 3127b2e60773SJohn Baldwin fpu_kern_thread(0); 3128b2e60773SJohn Baldwin #endif 3129b2e60773SJohn Baldwin for (;;) { 3130b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 31313c0e5685SJohn Baldwin while (STAILQ_EMPTY(&wq->m_head) && 31323c0e5685SJohn Baldwin STAILQ_EMPTY(&wq->so_head)) { 3133b2e60773SJohn Baldwin wq->running = false; 3134b2e60773SJohn Baldwin mtx_sleep(wq, &wq->mtx, 0, "-", 0); 3135b2e60773SJohn Baldwin wq->running = true; 3136b2e60773SJohn Baldwin } 3137b2e60773SJohn Baldwin 31383c0e5685SJohn Baldwin STAILQ_INIT(&local_m_head); 31393c0e5685SJohn Baldwin STAILQ_CONCAT(&local_m_head, &wq->m_head); 31403c0e5685SJohn Baldwin STAILQ_INIT(&local_so_head); 31413c0e5685SJohn Baldwin STAILQ_CONCAT(&local_so_head, &wq->so_head); 3142b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 3143b2e60773SJohn Baldwin 31443c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) { 31457b6c99d0SGleb Smirnoff if (m->m_epg_flags & EPG_FLAG_2FREE) { 31467b6c99d0SGleb Smirnoff ktls_free(m->m_epg_tls); 3147904a08f3SMateusz Guzik m_free_raw(m); 3148eeec8348SGleb Smirnoff } else { 3149470e851cSJohn Baldwin if (m->m_epg_tls->sync_dispatch) 315049f6925cSMark Johnston ktls_encrypt(wq, m); 3151470e851cSJohn Baldwin else 3152470e851cSJohn Baldwin ktls_encrypt_async(wq, m); 31533c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, -1); 3154b2e60773SJohn Baldwin } 3155b2e60773SJohn Baldwin } 31563c0e5685SJohn Baldwin 31573c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) { 31583c0e5685SJohn Baldwin ktls_decrypt(so); 31593c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, -1); 31603c0e5685SJohn Baldwin } 3161b2e60773SJohn Baldwin } 3162b2e60773SJohn Baldwin } 316328d0a740SAndrew Gallatin 31644150a5a8SAndrew Gallatin #if defined(INET) || defined(INET6) 316528d0a740SAndrew Gallatin static void 316628d0a740SAndrew Gallatin ktls_disable_ifnet_help(void *context, int pending __unused) 316728d0a740SAndrew Gallatin { 316828d0a740SAndrew Gallatin struct ktls_session *tls; 316928d0a740SAndrew Gallatin struct inpcb *inp; 317028d0a740SAndrew Gallatin struct tcpcb *tp; 317128d0a740SAndrew Gallatin struct socket *so; 317228d0a740SAndrew Gallatin int err; 317328d0a740SAndrew Gallatin 317428d0a740SAndrew Gallatin tls = context; 317528d0a740SAndrew Gallatin inp = tls->inp; 317628d0a740SAndrew Gallatin if (inp == NULL) 317728d0a740SAndrew Gallatin return; 317828d0a740SAndrew Gallatin INP_WLOCK(inp); 317928d0a740SAndrew Gallatin so = inp->inp_socket; 318028d0a740SAndrew Gallatin MPASS(so != NULL); 3181db0ac6deSCy Schubert if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 318228d0a740SAndrew Gallatin goto out; 318328d0a740SAndrew Gallatin } 318428d0a740SAndrew Gallatin 318528d0a740SAndrew Gallatin if (so->so_snd.sb_tls_info != NULL) 318628d0a740SAndrew Gallatin err = ktls_set_tx_mode(so, TCP_TLS_MODE_SW); 318728d0a740SAndrew Gallatin else 318828d0a740SAndrew Gallatin err = ENXIO; 318928d0a740SAndrew Gallatin if (err == 0) { 319028d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_ok, 1); 319128d0a740SAndrew Gallatin /* ktls_set_tx_mode() drops inp wlock, so recheck flags */ 319228d0a740SAndrew Gallatin if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0 && 319328d0a740SAndrew Gallatin (tp = intotcpcb(inp)) != NULL && 319428d0a740SAndrew Gallatin tp->t_fb->tfb_hwtls_change != NULL) 319528d0a740SAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 0); 319628d0a740SAndrew Gallatin } else { 319728d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_fail, 1); 319828d0a740SAndrew Gallatin } 319928d0a740SAndrew Gallatin 320028d0a740SAndrew Gallatin out: 320128d0a740SAndrew Gallatin sorele(so); 320228d0a740SAndrew Gallatin if (!in_pcbrele_wlocked(inp)) 320328d0a740SAndrew Gallatin INP_WUNLOCK(inp); 320428d0a740SAndrew Gallatin ktls_free(tls); 320528d0a740SAndrew Gallatin } 320628d0a740SAndrew Gallatin 320728d0a740SAndrew Gallatin /* 320828d0a740SAndrew Gallatin * Called when re-transmits are becoming a substantial portion of the 320928d0a740SAndrew Gallatin * sends on this connection. When this happens, we transition the 321028d0a740SAndrew Gallatin * connection to software TLS. This is needed because most inline TLS 321128d0a740SAndrew Gallatin * NICs keep crypto state only for in-order transmits. This means 321228d0a740SAndrew Gallatin * that to handle a TCP rexmit (which is out-of-order), the NIC must 321328d0a740SAndrew Gallatin * re-DMA the entire TLS record up to and including the current 321428d0a740SAndrew Gallatin * segment. This means that when re-transmitting the last ~1448 byte 321528d0a740SAndrew Gallatin * segment of a 16KB TLS record, we could wind up re-DMA'ing an order 321628d0a740SAndrew Gallatin * of magnitude more data than we are sending. This can cause the 321728d0a740SAndrew Gallatin * PCIe link to saturate well before the network, which can cause 321828d0a740SAndrew Gallatin * output drops, and a general loss of capacity. 321928d0a740SAndrew Gallatin */ 322028d0a740SAndrew Gallatin void 322128d0a740SAndrew Gallatin ktls_disable_ifnet(void *arg) 322228d0a740SAndrew Gallatin { 322328d0a740SAndrew Gallatin struct tcpcb *tp; 322428d0a740SAndrew Gallatin struct inpcb *inp; 322528d0a740SAndrew Gallatin struct socket *so; 322628d0a740SAndrew Gallatin struct ktls_session *tls; 322728d0a740SAndrew Gallatin 322828d0a740SAndrew Gallatin tp = arg; 322928d0a740SAndrew Gallatin inp = tp->t_inpcb; 323028d0a740SAndrew Gallatin INP_WLOCK_ASSERT(inp); 323128d0a740SAndrew Gallatin so = inp->inp_socket; 323228d0a740SAndrew Gallatin SOCK_LOCK(so); 323328d0a740SAndrew Gallatin tls = so->so_snd.sb_tls_info; 323428d0a740SAndrew Gallatin if (tls->disable_ifnet_pending) { 323528d0a740SAndrew Gallatin SOCK_UNLOCK(so); 323628d0a740SAndrew Gallatin return; 323728d0a740SAndrew Gallatin } 323828d0a740SAndrew Gallatin 323928d0a740SAndrew Gallatin /* 324028d0a740SAndrew Gallatin * note that disable_ifnet_pending is never cleared; disabling 324128d0a740SAndrew Gallatin * ifnet can only be done once per session, so we never want 324228d0a740SAndrew Gallatin * to do it again 324328d0a740SAndrew Gallatin */ 324428d0a740SAndrew Gallatin 324528d0a740SAndrew Gallatin (void)ktls_hold(tls); 324628d0a740SAndrew Gallatin in_pcbref(inp); 324728d0a740SAndrew Gallatin soref(so); 324828d0a740SAndrew Gallatin tls->disable_ifnet_pending = true; 324928d0a740SAndrew Gallatin tls->inp = inp; 325028d0a740SAndrew Gallatin SOCK_UNLOCK(so); 325128d0a740SAndrew Gallatin TASK_INIT(&tls->disable_ifnet_task, 0, ktls_disable_ifnet_help, tls); 325228d0a740SAndrew Gallatin (void)taskqueue_enqueue(taskqueue_thread, &tls->disable_ifnet_task); 325328d0a740SAndrew Gallatin } 32544150a5a8SAndrew Gallatin #endif 3255