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, 1529a673b71SJohn 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 225c0e4090eSAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_destroy_task); 226c0e4090eSAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, destroy_task, CTLFLAG_RD, 227c0e4090eSAndrew Gallatin &ktls_destroy_task, 228c0e4090eSAndrew Gallatin "Number of times ktls session was destroyed via taskqueue"); 229c0e4090eSAndrew Gallatin 2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 231b2e60773SJohn Baldwin "Software TLS session stats"); 2327029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 233b2e60773SJohn Baldwin "Hardware (ifnet) TLS session stats"); 2349e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2357029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 2369e14430dSJohn Baldwin "TOE TLS session stats"); 2379e14430dSJohn Baldwin #endif 238b2e60773SJohn Baldwin 2391755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc); 240b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc, 241b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-CBC"); 242b2e60773SJohn Baldwin 2431755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm); 244b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm, 245b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-GCM"); 246b2e60773SJohn Baldwin 2479c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20); 2489c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD, 2499c64fc40SJohn Baldwin &ktls_sw_chacha20, 2509c64fc40SJohn Baldwin "Active number of software TLS sessions using Chacha20-Poly1305"); 2519c64fc40SJohn Baldwin 2521755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc); 253b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD, 254b2e60773SJohn Baldwin &ktls_ifnet_cbc, 255b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-CBC"); 256b2e60773SJohn Baldwin 2571755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm); 258b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD, 259b2e60773SJohn Baldwin &ktls_ifnet_gcm, 260b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-GCM"); 261b2e60773SJohn Baldwin 2629c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20); 2639c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD, 2649c64fc40SJohn Baldwin &ktls_ifnet_chacha20, 2659c64fc40SJohn Baldwin "Active number of ifnet TLS sessions using Chacha20-Poly1305"); 2669c64fc40SJohn Baldwin 2671755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset); 268b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD, 269b2e60773SJohn Baldwin &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag"); 270b2e60773SJohn Baldwin 2711755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped); 272b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD, 273b2e60773SJohn Baldwin &ktls_ifnet_reset_dropped, 274b2e60773SJohn Baldwin "TLS sessions dropped after failing to update ifnet send tag"); 275b2e60773SJohn Baldwin 2761755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed); 277b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD, 278b2e60773SJohn Baldwin &ktls_ifnet_reset_failed, 279b2e60773SJohn Baldwin "TLS sessions that failed to allocate a new ifnet send tag"); 280b2e60773SJohn Baldwin 281b2e60773SJohn Baldwin static int ktls_ifnet_permitted; 282b2e60773SJohn Baldwin SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN, 283b2e60773SJohn Baldwin &ktls_ifnet_permitted, 1, 284b2e60773SJohn Baldwin "Whether to permit hardware (ifnet) TLS sessions"); 285b2e60773SJohn Baldwin 2869e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2871755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc); 2889e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD, 2899e14430dSJohn Baldwin &ktls_toe_cbc, 2909e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-CBC"); 2919e14430dSJohn Baldwin 2921755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm); 2939e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD, 2949e14430dSJohn Baldwin &ktls_toe_gcm, 2959e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-GCM"); 2969c64fc40SJohn Baldwin 29790972f04SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20); 2989c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD, 2999c64fc40SJohn Baldwin &ktls_toe_chacha20, 3009c64fc40SJohn Baldwin "Active number of TOE TLS sessions using Chacha20-Poly1305"); 3019e14430dSJohn Baldwin #endif 3029e14430dSJohn Baldwin 303b2e60773SJohn Baldwin static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS"); 304b2e60773SJohn Baldwin 305b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 306fe8c78f0SHans Petter Selasky static void ktls_reset_receive_tag(void *context, int pending); 307b2e60773SJohn Baldwin static void ktls_reset_send_tag(void *context, int pending); 308b2e60773SJohn Baldwin #endif 309b2e60773SJohn Baldwin static void ktls_work_thread(void *ctx); 31098215005SAndrew Gallatin static void ktls_alloc_thread(void *ctx); 311b2e60773SJohn Baldwin 312b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 313a2fba2a7SBjoern A. Zeeb static u_int 314b2e60773SJohn Baldwin ktls_get_cpu(struct socket *so) 315b2e60773SJohn Baldwin { 316b2e60773SJohn Baldwin struct inpcb *inp; 31702bc3865SAndrew Gallatin #ifdef NUMA 31802bc3865SAndrew Gallatin struct ktls_domain_info *di; 31902bc3865SAndrew Gallatin #endif 320a2fba2a7SBjoern A. Zeeb u_int cpuid; 321b2e60773SJohn Baldwin 322b2e60773SJohn Baldwin inp = sotoinpcb(so); 323b2e60773SJohn Baldwin #ifdef RSS 324b2e60773SJohn Baldwin cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype); 325b2e60773SJohn Baldwin if (cpuid != NETISR_CPUID_NONE) 326b2e60773SJohn Baldwin return (cpuid); 327b2e60773SJohn Baldwin #endif 328b2e60773SJohn Baldwin /* 329b2e60773SJohn Baldwin * Just use the flowid to shard connections in a repeatable 33021e3c1fbSJohn Baldwin * fashion. Note that TLS 1.0 sessions rely on the 331b2e60773SJohn Baldwin * serialization provided by having the same connection use 332b2e60773SJohn Baldwin * the same queue. 333b2e60773SJohn Baldwin */ 33402bc3865SAndrew Gallatin #ifdef NUMA 33502bc3865SAndrew Gallatin if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) { 33602bc3865SAndrew Gallatin di = &ktls_domains[inp->inp_numa_domain]; 33702bc3865SAndrew Gallatin cpuid = di->cpu[inp->inp_flowid % di->count]; 33802bc3865SAndrew Gallatin } else 33902bc3865SAndrew Gallatin #endif 340b2e60773SJohn Baldwin cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads]; 341b2e60773SJohn Baldwin return (cpuid); 342b2e60773SJohn Baldwin } 343b2e60773SJohn Baldwin #endif 344b2e60773SJohn Baldwin 34549f6925cSMark Johnston static int 34649f6925cSMark Johnston ktls_buffer_import(void *arg, void **store, int count, int domain, int flags) 34749f6925cSMark Johnston { 34849f6925cSMark Johnston vm_page_t m; 34984c39222SMark Johnston int i, req; 35049f6925cSMark Johnston 35149f6925cSMark Johnston KASSERT((ktls_maxlen & PAGE_MASK) == 0, 35249f6925cSMark Johnston ("%s: ktls max length %d is not page size-aligned", 35349f6925cSMark Johnston __func__, ktls_maxlen)); 35449f6925cSMark Johnston 35584c39222SMark Johnston req = VM_ALLOC_WIRED | VM_ALLOC_NODUMP | malloc2vm_flags(flags); 35649f6925cSMark Johnston for (i = 0; i < count; i++) { 35784c39222SMark Johnston m = vm_page_alloc_noobj_contig_domain(domain, req, 35849f6925cSMark Johnston atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0, 35949f6925cSMark Johnston VM_MEMATTR_DEFAULT); 36049f6925cSMark Johnston if (m == NULL) 36149f6925cSMark Johnston break; 36249f6925cSMark Johnston store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 36349f6925cSMark Johnston } 36449f6925cSMark Johnston return (i); 36549f6925cSMark Johnston } 36649f6925cSMark Johnston 36749f6925cSMark Johnston static void 36849f6925cSMark Johnston ktls_buffer_release(void *arg __unused, void **store, int count) 36949f6925cSMark Johnston { 37049f6925cSMark Johnston vm_page_t m; 37149f6925cSMark Johnston int i, j; 37249f6925cSMark Johnston 37349f6925cSMark Johnston for (i = 0; i < count; i++) { 37449f6925cSMark Johnston m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i])); 37549f6925cSMark Johnston for (j = 0; j < atop(ktls_maxlen); j++) { 37649f6925cSMark Johnston (void)vm_page_unwire_noq(m + j); 37749f6925cSMark Johnston vm_page_free(m + j); 37849f6925cSMark Johnston } 37949f6925cSMark Johnston } 38049f6925cSMark Johnston } 38149f6925cSMark Johnston 38249f6925cSMark Johnston static void 38349f6925cSMark Johnston ktls_free_mext_contig(struct mbuf *m) 38449f6925cSMark Johnston { 38549f6925cSMark Johnston M_ASSERTEXTPG(m); 38649f6925cSMark Johnston uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0])); 38749f6925cSMark Johnston } 38849f6925cSMark Johnston 389a72ee355SJohn Baldwin static int 390a72ee355SJohn Baldwin ktls_init(void) 391b2e60773SJohn Baldwin { 392b2e60773SJohn Baldwin struct thread *td; 393b2e60773SJohn Baldwin struct pcpu *pc; 39402bc3865SAndrew Gallatin int count, domain, error, i; 395b2e60773SJohn Baldwin 396b2e60773SJohn Baldwin ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS, 397b2e60773SJohn Baldwin M_WAITOK | M_ZERO); 398b2e60773SJohn Baldwin 399b2e60773SJohn Baldwin ktls_session_zone = uma_zcreate("ktls_session", 400b2e60773SJohn Baldwin sizeof(struct ktls_session), 401b2e60773SJohn Baldwin NULL, NULL, NULL, NULL, 402b2e60773SJohn Baldwin UMA_ALIGN_CACHE, 0); 403b2e60773SJohn Baldwin 40449f6925cSMark Johnston if (ktls_sw_buffer_cache) { 40549f6925cSMark Johnston ktls_buffer_zone = uma_zcache_create("ktls_buffers", 40649f6925cSMark Johnston roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL, 40749f6925cSMark Johnston ktls_buffer_import, ktls_buffer_release, NULL, 40849f6925cSMark Johnston UMA_ZONE_FIRSTTOUCH); 40949f6925cSMark Johnston } 41049f6925cSMark Johnston 411b2e60773SJohn Baldwin /* 412b2e60773SJohn Baldwin * Initialize the workqueues to run the TLS work. We create a 413b2e60773SJohn Baldwin * work queue for each CPU. 414b2e60773SJohn Baldwin */ 415b2e60773SJohn Baldwin CPU_FOREACH(i) { 4163c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].m_head); 4173c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].so_head); 418b2e60773SJohn Baldwin mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF); 419b2e60773SJohn Baldwin if (ktls_bind_threads > 1) { 420b2e60773SJohn Baldwin pc = pcpu_find(i); 42102bc3865SAndrew Gallatin domain = pc->pc_domain; 42202bc3865SAndrew Gallatin count = ktls_domains[domain].count; 42302bc3865SAndrew Gallatin ktls_domains[domain].cpu[count] = i; 42402bc3865SAndrew Gallatin ktls_domains[domain].count++; 425b2e60773SJohn Baldwin } 426b2e60773SJohn Baldwin ktls_cpuid_lookup[ktls_number_threads] = i; 427b2e60773SJohn Baldwin ktls_number_threads++; 428b2e60773SJohn Baldwin } 42902bc3865SAndrew Gallatin 43002bc3865SAndrew Gallatin /* 431a72ee355SJohn Baldwin * If we somehow have an empty domain, fall back to choosing 432a72ee355SJohn Baldwin * among all KTLS threads. 433a72ee355SJohn Baldwin */ 434a72ee355SJohn Baldwin if (ktls_bind_threads > 1) { 435a72ee355SJohn Baldwin for (i = 0; i < vm_ndomains; i++) { 436a72ee355SJohn Baldwin if (ktls_domains[i].count == 0) { 437a72ee355SJohn Baldwin ktls_bind_threads = 1; 438a72ee355SJohn Baldwin break; 439a72ee355SJohn Baldwin } 440a72ee355SJohn Baldwin } 441a72ee355SJohn Baldwin } 442a72ee355SJohn Baldwin 443a72ee355SJohn Baldwin /* Start kthreads for each workqueue. */ 444a72ee355SJohn Baldwin CPU_FOREACH(i) { 445a72ee355SJohn Baldwin error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i], 446a72ee355SJohn Baldwin &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i); 447a72ee355SJohn Baldwin if (error) { 448a72ee355SJohn Baldwin printf("Can't add KTLS thread %d error %d\n", i, error); 449a72ee355SJohn Baldwin return (error); 450a72ee355SJohn Baldwin } 451a72ee355SJohn Baldwin } 452a72ee355SJohn Baldwin 453a72ee355SJohn Baldwin /* 45498215005SAndrew Gallatin * Start an allocation thread per-domain to perform blocking allocations 45598215005SAndrew Gallatin * of 16k physically contiguous TLS crypto destination buffers. 45698215005SAndrew Gallatin */ 45798215005SAndrew Gallatin if (ktls_sw_buffer_cache) { 45898215005SAndrew Gallatin for (domain = 0; domain < vm_ndomains; domain++) { 45998215005SAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 46098215005SAndrew Gallatin continue; 46198215005SAndrew Gallatin if (CPU_EMPTY(&cpuset_domain[domain])) 46298215005SAndrew Gallatin continue; 46398215005SAndrew Gallatin error = kproc_kthread_add(ktls_alloc_thread, 46498215005SAndrew Gallatin &ktls_domains[domain], &ktls_proc, 46598215005SAndrew Gallatin &ktls_domains[domain].alloc_td.td, 46698215005SAndrew Gallatin 0, 0, "KTLS", "alloc_%d", domain); 467a72ee355SJohn Baldwin if (error) { 468a72ee355SJohn Baldwin printf("Can't add KTLS alloc thread %d error %d\n", 46998215005SAndrew Gallatin domain, error); 470a72ee355SJohn Baldwin return (error); 47102bc3865SAndrew Gallatin } 47202bc3865SAndrew Gallatin } 4734dc1b17dSMark Johnston } 47402bc3865SAndrew Gallatin 47589b65087SMark Johnston if (bootverbose) 476b2e60773SJohn Baldwin printf("KTLS: Initialized %d threads\n", ktls_number_threads); 477a72ee355SJohn Baldwin return (0); 478b2e60773SJohn Baldwin } 479a72ee355SJohn Baldwin 480a72ee355SJohn Baldwin static int 481a72ee355SJohn Baldwin ktls_start_kthreads(void) 482a72ee355SJohn Baldwin { 483a72ee355SJohn Baldwin int error, state; 484a72ee355SJohn Baldwin 485a72ee355SJohn Baldwin start: 486a72ee355SJohn Baldwin state = atomic_load_acq_int(&ktls_init_state); 487a72ee355SJohn Baldwin if (__predict_true(state > 0)) 488a72ee355SJohn Baldwin return (0); 489a72ee355SJohn Baldwin if (state < 0) 490a72ee355SJohn Baldwin return (ENXIO); 491a72ee355SJohn Baldwin 492a72ee355SJohn Baldwin sx_xlock(&ktls_init_lock); 493a72ee355SJohn Baldwin if (ktls_init_state != 0) { 494a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 495a72ee355SJohn Baldwin goto start; 496a72ee355SJohn Baldwin } 497a72ee355SJohn Baldwin 498a72ee355SJohn Baldwin error = ktls_init(); 499a72ee355SJohn Baldwin if (error == 0) 500a72ee355SJohn Baldwin state = 1; 501a72ee355SJohn Baldwin else 502a72ee355SJohn Baldwin state = -1; 503a72ee355SJohn Baldwin atomic_store_rel_int(&ktls_init_state, state); 504a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 505a72ee355SJohn Baldwin return (error); 506a72ee355SJohn Baldwin } 507b2e60773SJohn Baldwin 508b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 509b2e60773SJohn Baldwin static int 510b2e60773SJohn Baldwin ktls_create_session(struct socket *so, struct tls_enable *en, 511fe8c78f0SHans Petter Selasky struct ktls_session **tlsp, int direction) 512b2e60773SJohn Baldwin { 513b2e60773SJohn Baldwin struct ktls_session *tls; 514b2e60773SJohn Baldwin int error; 515b2e60773SJohn Baldwin 5167d29eb9aSJohn Baldwin /* Only TLS 1.0 - 1.3 are supported. */ 517b2e60773SJohn Baldwin if (en->tls_vmajor != TLS_MAJOR_VER_ONE) 518b2e60773SJohn Baldwin return (EINVAL); 519b2e60773SJohn Baldwin if (en->tls_vminor < TLS_MINOR_VER_ZERO || 5206554362cSAndrew Gallatin en->tls_vminor > TLS_MINOR_VER_THREE) 521b2e60773SJohn Baldwin return (EINVAL); 522b2e60773SJohn Baldwin 523b2e60773SJohn Baldwin if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE) 524b2e60773SJohn Baldwin return (EINVAL); 525b2e60773SJohn Baldwin if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE) 526b2e60773SJohn Baldwin return (EINVAL); 5276554362cSAndrew Gallatin if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv)) 528b2e60773SJohn Baldwin return (EINVAL); 529b2e60773SJohn Baldwin 530b2e60773SJohn Baldwin /* All supported algorithms require a cipher key. */ 531b2e60773SJohn Baldwin if (en->cipher_key_len == 0) 532b2e60773SJohn Baldwin return (EINVAL); 533b2e60773SJohn Baldwin 534b2e60773SJohn Baldwin /* No flags are currently supported. */ 535b2e60773SJohn Baldwin if (en->flags != 0) 536b2e60773SJohn Baldwin return (EINVAL); 537b2e60773SJohn Baldwin 538b2e60773SJohn Baldwin /* Common checks for supported algorithms. */ 539b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 540b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 541b2e60773SJohn Baldwin /* 542b2e60773SJohn Baldwin * auth_algorithm isn't used, but permit GMAC values 543b2e60773SJohn Baldwin * for compatibility. 544b2e60773SJohn Baldwin */ 545b2e60773SJohn Baldwin switch (en->auth_algorithm) { 546b2e60773SJohn Baldwin case 0: 547c0341432SJohn Baldwin #ifdef COMPAT_FREEBSD12 548c0341432SJohn Baldwin /* XXX: Really 13.0-current COMPAT. */ 549b2e60773SJohn Baldwin case CRYPTO_AES_128_NIST_GMAC: 550b2e60773SJohn Baldwin case CRYPTO_AES_192_NIST_GMAC: 551b2e60773SJohn Baldwin case CRYPTO_AES_256_NIST_GMAC: 552c0341432SJohn Baldwin #endif 553b2e60773SJohn Baldwin break; 554b2e60773SJohn Baldwin default: 555b2e60773SJohn Baldwin return (EINVAL); 556b2e60773SJohn Baldwin } 557b2e60773SJohn Baldwin if (en->auth_key_len != 0) 558b2e60773SJohn Baldwin return (EINVAL); 559900a28feSJohn Baldwin switch (en->tls_vminor) { 560900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 561900a28feSJohn Baldwin if (en->iv_len != TLS_AEAD_GCM_LEN) 562b2e60773SJohn Baldwin return (EINVAL); 563b2e60773SJohn Baldwin break; 564900a28feSJohn Baldwin case TLS_MINOR_VER_THREE: 565900a28feSJohn Baldwin if (en->iv_len != TLS_1_3_GCM_IV_LEN) 566900a28feSJohn Baldwin return (EINVAL); 567900a28feSJohn Baldwin break; 568900a28feSJohn Baldwin default: 569900a28feSJohn Baldwin return (EINVAL); 570900a28feSJohn Baldwin } 571900a28feSJohn Baldwin break; 572b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 573b2e60773SJohn Baldwin switch (en->auth_algorithm) { 574b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 575b2e60773SJohn Baldwin break; 576b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 577b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 578900a28feSJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO) 579900a28feSJohn Baldwin return (EINVAL); 580b2e60773SJohn Baldwin break; 581b2e60773SJohn Baldwin default: 582b2e60773SJohn Baldwin return (EINVAL); 583b2e60773SJohn Baldwin } 584b2e60773SJohn Baldwin if (en->auth_key_len == 0) 585b2e60773SJohn Baldwin return (EINVAL); 586900a28feSJohn Baldwin 587900a28feSJohn Baldwin /* 588900a28feSJohn Baldwin * TLS 1.0 requires an implicit IV. TLS 1.1 and 1.2 589900a28feSJohn Baldwin * use explicit IVs. 590900a28feSJohn Baldwin */ 591900a28feSJohn Baldwin switch (en->tls_vminor) { 592900a28feSJohn Baldwin case TLS_MINOR_VER_ZERO: 593900a28feSJohn Baldwin if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN) 594a63752ccSJohn Baldwin return (EINVAL); 595b2e60773SJohn Baldwin break; 596900a28feSJohn Baldwin case TLS_MINOR_VER_ONE: 597900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 598900a28feSJohn Baldwin /* Ignore any supplied IV. */ 599900a28feSJohn Baldwin en->iv_len = 0; 600900a28feSJohn Baldwin break; 601900a28feSJohn Baldwin default: 602900a28feSJohn Baldwin return (EINVAL); 603900a28feSJohn Baldwin } 604900a28feSJohn Baldwin break; 6059c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6069c64fc40SJohn Baldwin if (en->auth_algorithm != 0 || en->auth_key_len != 0) 6079c64fc40SJohn Baldwin return (EINVAL); 6089c64fc40SJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO && 6099c64fc40SJohn Baldwin en->tls_vminor != TLS_MINOR_VER_THREE) 6109c64fc40SJohn Baldwin return (EINVAL); 6119c64fc40SJohn Baldwin if (en->iv_len != TLS_CHACHA20_IV_LEN) 6129c64fc40SJohn Baldwin return (EINVAL); 6139c64fc40SJohn Baldwin break; 614b2e60773SJohn Baldwin default: 615b2e60773SJohn Baldwin return (EINVAL); 616b2e60773SJohn Baldwin } 617b2e60773SJohn Baldwin 618a72ee355SJohn Baldwin error = ktls_start_kthreads(); 619a72ee355SJohn Baldwin if (error != 0) 620a72ee355SJohn Baldwin return (error); 621a72ee355SJohn Baldwin 622b2e60773SJohn Baldwin tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 623b2e60773SJohn Baldwin 624b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 625b2e60773SJohn Baldwin 626b2e60773SJohn Baldwin refcount_init(&tls->refcount, 1); 627c0e4090eSAndrew Gallatin if (direction == KTLS_RX) { 628fe8c78f0SHans Petter Selasky TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_receive_tag, tls); 629c0e4090eSAndrew Gallatin } else { 630b2e60773SJohn Baldwin TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls); 631c0e4090eSAndrew Gallatin tls->inp = so->so_pcb; 632c0e4090eSAndrew Gallatin in_pcbref(tls->inp); 633c0e4090eSAndrew Gallatin tls->tx = true; 634c0e4090eSAndrew Gallatin } 635b2e60773SJohn Baldwin 636b2e60773SJohn Baldwin tls->wq_index = ktls_get_cpu(so); 637b2e60773SJohn Baldwin 638b2e60773SJohn Baldwin tls->params.cipher_algorithm = en->cipher_algorithm; 639b2e60773SJohn Baldwin tls->params.auth_algorithm = en->auth_algorithm; 640b2e60773SJohn Baldwin tls->params.tls_vmajor = en->tls_vmajor; 641b2e60773SJohn Baldwin tls->params.tls_vminor = en->tls_vminor; 642b2e60773SJohn Baldwin tls->params.flags = en->flags; 643b2e60773SJohn Baldwin tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen); 644b2e60773SJohn Baldwin 645b2e60773SJohn Baldwin /* Set the header and trailer lengths. */ 646b2e60773SJohn Baldwin tls->params.tls_hlen = sizeof(struct tls_record_layer); 647b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 648b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 6496554362cSAndrew Gallatin /* 6506554362cSAndrew Gallatin * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte 6516554362cSAndrew Gallatin * nonce. TLS 1.3 uses a 12 byte implicit IV. 6526554362cSAndrew Gallatin */ 6536554362cSAndrew Gallatin if (en->tls_vminor < TLS_MINOR_VER_THREE) 6546554362cSAndrew Gallatin tls->params.tls_hlen += sizeof(uint64_t); 655b2e60773SJohn Baldwin tls->params.tls_tlen = AES_GMAC_HASH_LEN; 656b2e60773SJohn Baldwin tls->params.tls_bs = 1; 657b2e60773SJohn Baldwin break; 658b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 659b2e60773SJohn Baldwin switch (en->auth_algorithm) { 660b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 661b2e60773SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_ZERO) { 662b2e60773SJohn Baldwin /* Implicit IV, no nonce. */ 6639f03d2c0SJohn Baldwin tls->sequential_records = true; 6649f03d2c0SJohn Baldwin tls->next_seqno = be64dec(en->rec_seq); 6659f03d2c0SJohn Baldwin STAILQ_INIT(&tls->pending_records); 666b2e60773SJohn Baldwin } else { 667b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 668b2e60773SJohn Baldwin } 669b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 670b2e60773SJohn Baldwin SHA1_HASH_LEN; 671b2e60773SJohn Baldwin break; 672b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 673b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 674b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 675b2e60773SJohn Baldwin SHA2_256_HASH_LEN; 676b2e60773SJohn Baldwin break; 677b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 678b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 679b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 680b2e60773SJohn Baldwin SHA2_384_HASH_LEN; 681b2e60773SJohn Baldwin break; 682b2e60773SJohn Baldwin default: 683b2e60773SJohn Baldwin panic("invalid hmac"); 684b2e60773SJohn Baldwin } 685b2e60773SJohn Baldwin tls->params.tls_bs = AES_BLOCK_LEN; 686b2e60773SJohn Baldwin break; 6879c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6889c64fc40SJohn Baldwin /* 6899c64fc40SJohn Baldwin * Chacha20 uses a 12 byte implicit IV. 6909c64fc40SJohn Baldwin */ 6919c64fc40SJohn Baldwin tls->params.tls_tlen = POLY1305_HASH_LEN; 6929c64fc40SJohn Baldwin tls->params.tls_bs = 1; 6939c64fc40SJohn Baldwin break; 694b2e60773SJohn Baldwin default: 695b2e60773SJohn Baldwin panic("invalid cipher"); 696b2e60773SJohn Baldwin } 697b2e60773SJohn Baldwin 6989c64fc40SJohn Baldwin /* 6999c64fc40SJohn Baldwin * TLS 1.3 includes optional padding which we do not support, 7009c64fc40SJohn Baldwin * and also puts the "real" record type at the end of the 7019c64fc40SJohn Baldwin * encrypted data. 7029c64fc40SJohn Baldwin */ 7039c64fc40SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_THREE) 7049c64fc40SJohn Baldwin tls->params.tls_tlen += sizeof(uint8_t); 7059c64fc40SJohn Baldwin 706b2e60773SJohn Baldwin KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN, 707b2e60773SJohn Baldwin ("TLS header length too long: %d", tls->params.tls_hlen)); 708b2e60773SJohn Baldwin KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN, 709b2e60773SJohn Baldwin ("TLS trailer length too long: %d", tls->params.tls_tlen)); 710b2e60773SJohn Baldwin 711b2e60773SJohn Baldwin if (en->auth_key_len != 0) { 712b2e60773SJohn Baldwin tls->params.auth_key_len = en->auth_key_len; 713b2e60773SJohn Baldwin tls->params.auth_key = malloc(en->auth_key_len, M_KTLS, 714b2e60773SJohn Baldwin M_WAITOK); 715b2e60773SJohn Baldwin error = copyin(en->auth_key, tls->params.auth_key, 716b2e60773SJohn Baldwin en->auth_key_len); 717b2e60773SJohn Baldwin if (error) 718b2e60773SJohn Baldwin goto out; 719b2e60773SJohn Baldwin } 720b2e60773SJohn Baldwin 721b2e60773SJohn Baldwin tls->params.cipher_key_len = en->cipher_key_len; 722b2e60773SJohn Baldwin tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK); 723b2e60773SJohn Baldwin error = copyin(en->cipher_key, tls->params.cipher_key, 724b2e60773SJohn Baldwin en->cipher_key_len); 725b2e60773SJohn Baldwin if (error) 726b2e60773SJohn Baldwin goto out; 727b2e60773SJohn Baldwin 728b2e60773SJohn Baldwin /* 7299c64fc40SJohn Baldwin * This holds the implicit portion of the nonce for AEAD 7309c64fc40SJohn Baldwin * ciphers and the initial implicit IV for TLS 1.0. The 7319c64fc40SJohn Baldwin * explicit portions of the IV are generated in ktls_frame(). 732b2e60773SJohn Baldwin */ 733b2e60773SJohn Baldwin if (en->iv_len != 0) { 734b2e60773SJohn Baldwin tls->params.iv_len = en->iv_len; 735b2e60773SJohn Baldwin error = copyin(en->iv, tls->params.iv, en->iv_len); 736b2e60773SJohn Baldwin if (error) 737b2e60773SJohn Baldwin goto out; 7387d29eb9aSJohn Baldwin 7397d29eb9aSJohn Baldwin /* 7409c64fc40SJohn Baldwin * For TLS 1.2 with GCM, generate an 8-byte nonce as a 7419c64fc40SJohn Baldwin * counter to generate unique explicit IVs. 7427d29eb9aSJohn Baldwin * 7437d29eb9aSJohn Baldwin * Store this counter in the last 8 bytes of the IV 7447d29eb9aSJohn Baldwin * array so that it is 8-byte aligned. 7457d29eb9aSJohn Baldwin */ 7467d29eb9aSJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 7477d29eb9aSJohn Baldwin en->tls_vminor == TLS_MINOR_VER_TWO) 7487d29eb9aSJohn Baldwin arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0); 749b2e60773SJohn Baldwin } 750b2e60773SJohn Baldwin 751b2e60773SJohn Baldwin *tlsp = tls; 752b2e60773SJohn Baldwin return (0); 753b2e60773SJohn Baldwin 754b2e60773SJohn Baldwin out: 755d01db2b8SJohn Baldwin ktls_free(tls); 756b2e60773SJohn Baldwin return (error); 757b2e60773SJohn Baldwin } 758b2e60773SJohn Baldwin 759b2e60773SJohn Baldwin static struct ktls_session * 760fe8c78f0SHans Petter Selasky ktls_clone_session(struct ktls_session *tls, int direction) 761b2e60773SJohn Baldwin { 762b2e60773SJohn Baldwin struct ktls_session *tls_new; 763b2e60773SJohn Baldwin 764b2e60773SJohn Baldwin tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 765b2e60773SJohn Baldwin 766b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 767b2e60773SJohn Baldwin 768b2e60773SJohn Baldwin refcount_init(&tls_new->refcount, 1); 769c0e4090eSAndrew Gallatin if (direction == KTLS_RX) { 770fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_receive_tag, 771fe8c78f0SHans Petter Selasky tls_new); 772c0e4090eSAndrew Gallatin } else { 773fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_send_tag, 774fe8c78f0SHans Petter Selasky tls_new); 775c0e4090eSAndrew Gallatin tls_new->inp = tls->inp; 776c0e4090eSAndrew Gallatin tls_new->tx = true; 777c0e4090eSAndrew Gallatin in_pcbref(tls_new->inp); 778c0e4090eSAndrew Gallatin } 779b2e60773SJohn Baldwin 780b2e60773SJohn Baldwin /* Copy fields from existing session. */ 781b2e60773SJohn Baldwin tls_new->params = tls->params; 782b2e60773SJohn Baldwin tls_new->wq_index = tls->wq_index; 783b2e60773SJohn Baldwin 784b2e60773SJohn Baldwin /* Deep copy keys. */ 785b2e60773SJohn Baldwin if (tls_new->params.auth_key != NULL) { 786b2e60773SJohn Baldwin tls_new->params.auth_key = malloc(tls->params.auth_key_len, 787b2e60773SJohn Baldwin M_KTLS, M_WAITOK); 788b2e60773SJohn Baldwin memcpy(tls_new->params.auth_key, tls->params.auth_key, 789b2e60773SJohn Baldwin tls->params.auth_key_len); 790b2e60773SJohn Baldwin } 791b2e60773SJohn Baldwin 792b2e60773SJohn Baldwin tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS, 793b2e60773SJohn Baldwin M_WAITOK); 794b2e60773SJohn Baldwin memcpy(tls_new->params.cipher_key, tls->params.cipher_key, 795b2e60773SJohn Baldwin tls->params.cipher_key_len); 796b2e60773SJohn Baldwin 797b2e60773SJohn Baldwin return (tls_new); 798b2e60773SJohn Baldwin } 7999e14430dSJohn Baldwin 8009e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 8019e14430dSJohn Baldwin static int 802f1f93475SJohn Baldwin ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction) 8039e14430dSJohn Baldwin { 8049e14430dSJohn Baldwin struct inpcb *inp; 8059e14430dSJohn Baldwin struct tcpcb *tp; 8069e14430dSJohn Baldwin int error; 8079e14430dSJohn Baldwin 8089e14430dSJohn Baldwin inp = so->so_pcb; 8099e14430dSJohn Baldwin INP_WLOCK(inp); 81053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 8119e14430dSJohn Baldwin INP_WUNLOCK(inp); 8129e14430dSJohn Baldwin return (ECONNRESET); 8139e14430dSJohn Baldwin } 8149e14430dSJohn Baldwin if (inp->inp_socket == NULL) { 8159e14430dSJohn Baldwin INP_WUNLOCK(inp); 8169e14430dSJohn Baldwin return (ECONNRESET); 8179e14430dSJohn Baldwin } 8189e14430dSJohn Baldwin tp = intotcpcb(inp); 8196bcf3c46SJohn Baldwin if (!(tp->t_flags & TF_TOE)) { 8209e14430dSJohn Baldwin INP_WUNLOCK(inp); 8219e14430dSJohn Baldwin return (EOPNOTSUPP); 8229e14430dSJohn Baldwin } 8239e14430dSJohn Baldwin 824f1f93475SJohn Baldwin error = tcp_offload_alloc_tls_session(tp, tls, direction); 8259e14430dSJohn Baldwin INP_WUNLOCK(inp); 8269e14430dSJohn Baldwin if (error == 0) { 8279e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_TOE; 8289e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 8299e14430dSJohn Baldwin case CRYPTO_AES_CBC: 8309e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, 1); 8319e14430dSJohn Baldwin break; 8329e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 8339e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, 1); 8349e14430dSJohn Baldwin break; 8359c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 8369c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, 1); 8379c64fc40SJohn Baldwin break; 8389e14430dSJohn Baldwin } 8399e14430dSJohn Baldwin } 8409e14430dSJohn Baldwin return (error); 8419e14430dSJohn Baldwin } 8429e14430dSJohn Baldwin #endif 8439e14430dSJohn Baldwin 844b2e60773SJohn Baldwin /* 845b2e60773SJohn Baldwin * Common code used when first enabling ifnet TLS on a connection or 846b2e60773SJohn Baldwin * when allocating a new ifnet TLS session due to a routing change. 847b2e60773SJohn Baldwin * This function allocates a new TLS send tag on whatever interface 848b2e60773SJohn Baldwin * the connection is currently routed over. 849b2e60773SJohn Baldwin */ 850b2e60773SJohn Baldwin static int 851b2e60773SJohn Baldwin ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force, 852b2e60773SJohn Baldwin struct m_snd_tag **mstp) 853b2e60773SJohn Baldwin { 854b2e60773SJohn Baldwin union if_snd_tag_alloc_params params; 855b2e60773SJohn Baldwin struct ifnet *ifp; 856983066f0SAlexander V. Chernikov struct nhop_object *nh; 857b2e60773SJohn Baldwin struct tcpcb *tp; 858b2e60773SJohn Baldwin int error; 859b2e60773SJohn Baldwin 860b2e60773SJohn Baldwin INP_RLOCK(inp); 86153af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 862b2e60773SJohn Baldwin INP_RUNLOCK(inp); 863b2e60773SJohn Baldwin return (ECONNRESET); 864b2e60773SJohn Baldwin } 865b2e60773SJohn Baldwin if (inp->inp_socket == NULL) { 866b2e60773SJohn Baldwin INP_RUNLOCK(inp); 867b2e60773SJohn Baldwin return (ECONNRESET); 868b2e60773SJohn Baldwin } 869b2e60773SJohn Baldwin tp = intotcpcb(inp); 870b2e60773SJohn Baldwin 871b2e60773SJohn Baldwin /* 872b2e60773SJohn Baldwin * Check administrative controls on ifnet TLS to determine if 873b2e60773SJohn Baldwin * ifnet TLS should be denied. 874b2e60773SJohn Baldwin * 875b2e60773SJohn Baldwin * - Always permit 'force' requests. 876b2e60773SJohn Baldwin * - ktls_ifnet_permitted == 0: always deny. 877b2e60773SJohn Baldwin */ 878b2e60773SJohn Baldwin if (!force && ktls_ifnet_permitted == 0) { 879b2e60773SJohn Baldwin INP_RUNLOCK(inp); 880b2e60773SJohn Baldwin return (ENXIO); 881b2e60773SJohn Baldwin } 882b2e60773SJohn Baldwin 883b2e60773SJohn Baldwin /* 884b2e60773SJohn Baldwin * XXX: Use the cached route in the inpcb to find the 885b2e60773SJohn Baldwin * interface. This should perhaps instead use 886b2e60773SJohn Baldwin * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only 887b2e60773SJohn Baldwin * enabled after a connection has completed key negotiation in 888b2e60773SJohn Baldwin * userland, the cached route will be present in practice. 889b2e60773SJohn Baldwin */ 890983066f0SAlexander V. Chernikov nh = inp->inp_route.ro_nh; 891983066f0SAlexander V. Chernikov if (nh == NULL) { 892b2e60773SJohn Baldwin INP_RUNLOCK(inp); 893b2e60773SJohn Baldwin return (ENXIO); 894b2e60773SJohn Baldwin } 895983066f0SAlexander V. Chernikov ifp = nh->nh_ifp; 896b2e60773SJohn Baldwin if_ref(ifp); 897b2e60773SJohn Baldwin 898521eac97SJohn Baldwin /* 899521eac97SJohn Baldwin * Allocate a TLS + ratelimit tag if the connection has an 900521eac97SJohn Baldwin * existing pacing rate. 901521eac97SJohn Baldwin */ 902521eac97SJohn Baldwin if (tp->t_pacing_rate != -1 && 90308484627SJustin Hibbits (if_getcapenable(ifp) & IFCAP_TXTLS_RTLMT) != 0) { 904521eac97SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT; 905521eac97SJohn Baldwin params.tls_rate_limit.inp = inp; 906521eac97SJohn Baldwin params.tls_rate_limit.tls = tls; 907521eac97SJohn Baldwin params.tls_rate_limit.max_rate = tp->t_pacing_rate; 908521eac97SJohn Baldwin } else { 909b2e60773SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS; 910521eac97SJohn Baldwin params.tls.inp = inp; 911521eac97SJohn Baldwin params.tls.tls = tls; 912521eac97SJohn Baldwin } 913b2e60773SJohn Baldwin params.hdr.flowid = inp->inp_flowid; 914b2e60773SJohn Baldwin params.hdr.flowtype = inp->inp_flowtype; 91598085baeSAndrew Gallatin params.hdr.numa_domain = inp->inp_numa_domain; 916b2e60773SJohn Baldwin INP_RUNLOCK(inp); 917b2e60773SJohn Baldwin 91808484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_MEXTPG) == 0) { 919b2e60773SJohn Baldwin error = EOPNOTSUPP; 920b2e60773SJohn Baldwin goto out; 921b2e60773SJohn Baldwin } 922b2e60773SJohn Baldwin if (inp->inp_vflag & INP_IPV6) { 92308484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXTLS6) == 0) { 924b2e60773SJohn Baldwin error = EOPNOTSUPP; 925b2e60773SJohn Baldwin goto out; 926b2e60773SJohn Baldwin } 927b2e60773SJohn Baldwin } else { 92808484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXTLS4) == 0) { 929b2e60773SJohn Baldwin error = EOPNOTSUPP; 930b2e60773SJohn Baldwin goto out; 931b2e60773SJohn Baldwin } 932b2e60773SJohn Baldwin } 93336e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, mstp); 934b2e60773SJohn Baldwin out: 935b2e60773SJohn Baldwin if_rele(ifp); 936b2e60773SJohn Baldwin return (error); 937b2e60773SJohn Baldwin } 938b2e60773SJohn Baldwin 939fe8c78f0SHans Petter Selasky /* 940fe8c78f0SHans Petter Selasky * Allocate an initial TLS receive tag for doing HW decryption of TLS 941fe8c78f0SHans Petter Selasky * data. 942fe8c78f0SHans Petter Selasky * 943fe8c78f0SHans Petter Selasky * This function allocates a new TLS receive tag on whatever interface 944fe8c78f0SHans Petter Selasky * the connection is currently routed over. If the connection ends up 945fe8c78f0SHans Petter Selasky * using a different interface for receive this will get fixed up via 946fe8c78f0SHans Petter Selasky * ktls_input_ifp_mismatch as future packets arrive. 947fe8c78f0SHans Petter Selasky */ 948b2e60773SJohn Baldwin static int 949fe8c78f0SHans Petter Selasky ktls_alloc_rcv_tag(struct inpcb *inp, struct ktls_session *tls, 950fe8c78f0SHans Petter Selasky struct m_snd_tag **mstp) 951fe8c78f0SHans Petter Selasky { 952fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 953fe8c78f0SHans Petter Selasky struct ifnet *ifp; 954fe8c78f0SHans Petter Selasky struct nhop_object *nh; 955fe8c78f0SHans Petter Selasky int error; 956fe8c78f0SHans Petter Selasky 957fe8c78f0SHans Petter Selasky if (!ktls_ocf_recrypt_supported(tls)) 958fe8c78f0SHans Petter Selasky return (ENXIO); 959fe8c78f0SHans Petter Selasky 960fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 96153af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 962fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 963fe8c78f0SHans Petter Selasky return (ECONNRESET); 964fe8c78f0SHans Petter Selasky } 965fe8c78f0SHans Petter Selasky if (inp->inp_socket == NULL) { 966fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 967fe8c78f0SHans Petter Selasky return (ECONNRESET); 968fe8c78f0SHans Petter Selasky } 969fe8c78f0SHans Petter Selasky 970fe8c78f0SHans Petter Selasky /* 971fe8c78f0SHans Petter Selasky * Check administrative controls on ifnet TLS to determine if 972fe8c78f0SHans Petter Selasky * ifnet TLS should be denied. 973fe8c78f0SHans Petter Selasky */ 974fe8c78f0SHans Petter Selasky if (ktls_ifnet_permitted == 0) { 975fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 976fe8c78f0SHans Petter Selasky return (ENXIO); 977fe8c78f0SHans Petter Selasky } 978fe8c78f0SHans Petter Selasky 979fe8c78f0SHans Petter Selasky /* 980fe8c78f0SHans Petter Selasky * XXX: As with ktls_alloc_snd_tag, use the cached route in 981fe8c78f0SHans Petter Selasky * the inpcb to find the interface. 982fe8c78f0SHans Petter Selasky */ 983fe8c78f0SHans Petter Selasky nh = inp->inp_route.ro_nh; 984fe8c78f0SHans Petter Selasky if (nh == NULL) { 985fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 986fe8c78f0SHans Petter Selasky return (ENXIO); 987fe8c78f0SHans Petter Selasky } 988fe8c78f0SHans Petter Selasky ifp = nh->nh_ifp; 989fe8c78f0SHans Petter Selasky if_ref(ifp); 990fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 991fe8c78f0SHans Petter Selasky 992fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 993fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 994fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 995fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 996fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 997fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 998fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = 0; 999fe8c78f0SHans Petter Selasky 1000fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1001fe8c78f0SHans Petter Selasky 1002fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 100308484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS6) == 0) { 1004fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1005fe8c78f0SHans Petter Selasky goto out; 1006fe8c78f0SHans Petter Selasky } 1007fe8c78f0SHans Petter Selasky } else { 100808484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS4) == 0) { 1009fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1010fe8c78f0SHans Petter Selasky goto out; 1011fe8c78f0SHans Petter Selasky } 1012fe8c78f0SHans Petter Selasky } 1013fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, mstp); 1014fe8c78f0SHans Petter Selasky 1015fe8c78f0SHans Petter Selasky /* 1016fe8c78f0SHans Petter Selasky * If this connection is over a vlan, vlan_snd_tag_alloc 1017fe8c78f0SHans Petter Selasky * rewrites vlan_id with the saved interface. Save the VLAN 1018fe8c78f0SHans Petter Selasky * ID for use in ktls_reset_receive_tag which allocates new 1019fe8c78f0SHans Petter Selasky * receive tags directly from the leaf interface bypassing 1020fe8c78f0SHans Petter Selasky * if_vlan. 1021fe8c78f0SHans Petter Selasky */ 1022fe8c78f0SHans Petter Selasky if (error == 0) 1023fe8c78f0SHans Petter Selasky tls->rx_vlan_id = params.tls_rx.vlan_id; 1024fe8c78f0SHans Petter Selasky out: 1025fe8c78f0SHans Petter Selasky return (error); 1026fe8c78f0SHans Petter Selasky } 1027fe8c78f0SHans Petter Selasky 1028fe8c78f0SHans Petter Selasky static int 1029fe8c78f0SHans Petter Selasky ktls_try_ifnet(struct socket *so, struct ktls_session *tls, int direction, 1030fe8c78f0SHans Petter Selasky bool force) 1031b2e60773SJohn Baldwin { 1032b2e60773SJohn Baldwin struct m_snd_tag *mst; 1033b2e60773SJohn Baldwin int error; 1034b2e60773SJohn Baldwin 1035fe8c78f0SHans Petter Selasky switch (direction) { 1036fe8c78f0SHans Petter Selasky case KTLS_TX: 1037b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst); 1038fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1039fe8c78f0SHans Petter Selasky goto done; 1040fe8c78f0SHans Petter Selasky break; 1041fe8c78f0SHans Petter Selasky case KTLS_RX: 1042fe8c78f0SHans Petter Selasky KASSERT(!force, ("%s: forced receive tag", __func__)); 1043fe8c78f0SHans Petter Selasky error = ktls_alloc_rcv_tag(so->so_pcb, tls, &mst); 1044fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1045fe8c78f0SHans Petter Selasky goto done; 1046fe8c78f0SHans Petter Selasky break; 1047fe8c78f0SHans Petter Selasky default: 1048fe8c78f0SHans Petter Selasky __assert_unreachable(); 1049fe8c78f0SHans Petter Selasky } 1050fe8c78f0SHans Petter Selasky 10519e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_IFNET; 1052b2e60773SJohn Baldwin tls->snd_tag = mst; 1053fe8c78f0SHans Petter Selasky 1054b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1055b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1056b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, 1); 1057b2e60773SJohn Baldwin break; 1058b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1059b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, 1); 1060b2e60773SJohn Baldwin break; 10619c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 10629c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, 1); 10639c64fc40SJohn Baldwin break; 1064fe8c78f0SHans Petter Selasky default: 1065fe8c78f0SHans Petter Selasky break; 1066b2e60773SJohn Baldwin } 1067fe8c78f0SHans Petter Selasky done: 1068b2e60773SJohn Baldwin return (error); 1069b2e60773SJohn Baldwin } 1070b2e60773SJohn Baldwin 107196668a81SJohn Baldwin static void 107296668a81SJohn Baldwin ktls_use_sw(struct ktls_session *tls) 1073b2e60773SJohn Baldwin { 10749e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_SW; 1075b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1076b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1077b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, 1); 1078b2e60773SJohn Baldwin break; 1079b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1080b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, 1); 1081b2e60773SJohn Baldwin break; 10829c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 10839c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, 1); 10849c64fc40SJohn Baldwin break; 1085b2e60773SJohn Baldwin } 108696668a81SJohn Baldwin } 108796668a81SJohn Baldwin 108896668a81SJohn Baldwin static int 108996668a81SJohn Baldwin ktls_try_sw(struct socket *so, struct ktls_session *tls, int direction) 109096668a81SJohn Baldwin { 109196668a81SJohn Baldwin int error; 109296668a81SJohn Baldwin 109396668a81SJohn Baldwin error = ktls_ocf_try(so, tls, direction); 109496668a81SJohn Baldwin if (error) 109596668a81SJohn Baldwin return (error); 109696668a81SJohn Baldwin ktls_use_sw(tls); 1097b2e60773SJohn Baldwin return (0); 1098b2e60773SJohn Baldwin } 1099b2e60773SJohn Baldwin 11003c0e5685SJohn Baldwin /* 11013c0e5685SJohn Baldwin * KTLS RX stores data in the socket buffer as a list of TLS records, 11023c0e5685SJohn Baldwin * where each record is stored as a control message containg the TLS 11033c0e5685SJohn Baldwin * header followed by data mbufs containing the decrypted data. This 11043c0e5685SJohn Baldwin * is different from KTLS TX which always uses an mb_ext_pgs mbuf for 11053c0e5685SJohn Baldwin * both encrypted and decrypted data. TLS records decrypted by a NIC 11063c0e5685SJohn Baldwin * should be queued to the socket buffer as records, but encrypted 11073c0e5685SJohn Baldwin * data which needs to be decrypted by software arrives as a stream of 11083c0e5685SJohn Baldwin * regular mbufs which need to be converted. In addition, there may 11093c0e5685SJohn Baldwin * already be pending encrypted data in the socket buffer when KTLS RX 11103c0e5685SJohn Baldwin * is enabled. 11113c0e5685SJohn Baldwin * 11123c0e5685SJohn Baldwin * To manage not-yet-decrypted data for KTLS RX, the following scheme 11133c0e5685SJohn Baldwin * is used: 11143c0e5685SJohn Baldwin * 11153c0e5685SJohn Baldwin * - A single chain of NOTREADY mbufs is hung off of sb_mtls. 11163c0e5685SJohn Baldwin * 11173c0e5685SJohn Baldwin * - ktls_check_rx checks this chain of mbufs reading the TLS header 11183c0e5685SJohn Baldwin * from the first mbuf. Once all of the data for that TLS record is 11193c0e5685SJohn Baldwin * queued, the socket is queued to a worker thread. 11203c0e5685SJohn Baldwin * 11213c0e5685SJohn Baldwin * - The worker thread calls ktls_decrypt to decrypt TLS records in 11223c0e5685SJohn Baldwin * the TLS chain. Each TLS record is detached from the TLS chain, 11233c0e5685SJohn Baldwin * decrypted, and inserted into the regular socket buffer chain as 11243c0e5685SJohn Baldwin * record starting with a control message holding the TLS header and 11253c0e5685SJohn Baldwin * a chain of mbufs holding the encrypted data. 11263c0e5685SJohn Baldwin */ 11273c0e5685SJohn Baldwin 11283c0e5685SJohn Baldwin static void 11293c0e5685SJohn Baldwin sb_mark_notready(struct sockbuf *sb) 11303c0e5685SJohn Baldwin { 11313c0e5685SJohn Baldwin struct mbuf *m; 11323c0e5685SJohn Baldwin 11333c0e5685SJohn Baldwin m = sb->sb_mb; 11343c0e5685SJohn Baldwin sb->sb_mtls = m; 11353c0e5685SJohn Baldwin sb->sb_mb = NULL; 11363c0e5685SJohn Baldwin sb->sb_mbtail = NULL; 11373c0e5685SJohn Baldwin sb->sb_lastrecord = NULL; 11383c0e5685SJohn Baldwin for (; m != NULL; m = m->m_next) { 11393c0e5685SJohn Baldwin KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL", 11403c0e5685SJohn Baldwin __func__)); 11413c0e5685SJohn Baldwin KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail", 11423c0e5685SJohn Baldwin __func__)); 11433c0e5685SJohn Baldwin KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len", 11443c0e5685SJohn Baldwin __func__)); 11453c0e5685SJohn Baldwin m->m_flags |= M_NOTREADY; 11463c0e5685SJohn Baldwin sb->sb_acc -= m->m_len; 11473c0e5685SJohn Baldwin sb->sb_tlscc += m->m_len; 11483c0e5685SJohn Baldwin sb->sb_mtlstail = m; 11493c0e5685SJohn Baldwin } 11503c0e5685SJohn Baldwin KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc, 11513c0e5685SJohn Baldwin ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc, 11523c0e5685SJohn Baldwin sb->sb_ccc)); 11533c0e5685SJohn Baldwin } 11543c0e5685SJohn Baldwin 1155c57dbec6SJohn Baldwin /* 1156c57dbec6SJohn Baldwin * Return information about the pending TLS data in a socket 1157c57dbec6SJohn Baldwin * buffer. On return, 'seqno' is set to the sequence number 1158c57dbec6SJohn Baldwin * of the next TLS record to be received, 'resid' is set to 1159c57dbec6SJohn Baldwin * the amount of bytes still needed for the last pending 1160c57dbec6SJohn Baldwin * record. The function returns 'false' if the last pending 1161c57dbec6SJohn Baldwin * record contains a partial TLS header. In that case, 'resid' 1162c57dbec6SJohn Baldwin * is the number of bytes needed to complete the TLS header. 1163c57dbec6SJohn Baldwin */ 1164c57dbec6SJohn Baldwin bool 1165c57dbec6SJohn Baldwin ktls_pending_rx_info(struct sockbuf *sb, uint64_t *seqnop, size_t *residp) 1166c57dbec6SJohn Baldwin { 1167c57dbec6SJohn Baldwin struct tls_record_layer hdr; 1168c57dbec6SJohn Baldwin struct mbuf *m; 1169c57dbec6SJohn Baldwin uint64_t seqno; 1170c57dbec6SJohn Baldwin size_t resid; 1171c57dbec6SJohn Baldwin u_int offset, record_len; 1172c57dbec6SJohn Baldwin 1173c57dbec6SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 1174c57dbec6SJohn Baldwin MPASS(sb->sb_flags & SB_TLS_RX); 1175c57dbec6SJohn Baldwin seqno = sb->sb_tls_seqno; 1176c57dbec6SJohn Baldwin resid = sb->sb_tlscc; 1177c57dbec6SJohn Baldwin m = sb->sb_mtls; 1178c57dbec6SJohn Baldwin offset = 0; 1179c57dbec6SJohn Baldwin 1180c57dbec6SJohn Baldwin if (resid == 0) { 1181c57dbec6SJohn Baldwin *seqnop = seqno; 1182c57dbec6SJohn Baldwin *residp = 0; 1183c57dbec6SJohn Baldwin return (true); 1184c57dbec6SJohn Baldwin } 1185c57dbec6SJohn Baldwin 1186c57dbec6SJohn Baldwin for (;;) { 1187c57dbec6SJohn Baldwin seqno++; 1188c57dbec6SJohn Baldwin 1189c57dbec6SJohn Baldwin if (resid < sizeof(hdr)) { 1190c57dbec6SJohn Baldwin *seqnop = seqno; 1191c57dbec6SJohn Baldwin *residp = sizeof(hdr) - resid; 1192c57dbec6SJohn Baldwin return (false); 1193c57dbec6SJohn Baldwin } 1194c57dbec6SJohn Baldwin 1195c57dbec6SJohn Baldwin m_copydata(m, offset, sizeof(hdr), (void *)&hdr); 1196c57dbec6SJohn Baldwin 1197c57dbec6SJohn Baldwin record_len = sizeof(hdr) + ntohs(hdr.tls_length); 1198c57dbec6SJohn Baldwin if (resid <= record_len) { 1199c57dbec6SJohn Baldwin *seqnop = seqno; 1200c57dbec6SJohn Baldwin *residp = record_len - resid; 1201c57dbec6SJohn Baldwin return (true); 1202c57dbec6SJohn Baldwin } 1203c57dbec6SJohn Baldwin resid -= record_len; 1204c57dbec6SJohn Baldwin 1205c57dbec6SJohn Baldwin while (record_len != 0) { 1206c57dbec6SJohn Baldwin if (m->m_len - offset > record_len) { 1207c57dbec6SJohn Baldwin offset += record_len; 1208c57dbec6SJohn Baldwin break; 1209c57dbec6SJohn Baldwin } 1210c57dbec6SJohn Baldwin 1211c57dbec6SJohn Baldwin record_len -= (m->m_len - offset); 1212c57dbec6SJohn Baldwin offset = 0; 1213c57dbec6SJohn Baldwin m = m->m_next; 1214c57dbec6SJohn Baldwin } 1215c57dbec6SJohn Baldwin } 1216c57dbec6SJohn Baldwin } 1217c57dbec6SJohn Baldwin 1218b2e60773SJohn Baldwin int 1219f1f93475SJohn Baldwin ktls_enable_rx(struct socket *so, struct tls_enable *en) 1220f1f93475SJohn Baldwin { 1221f1f93475SJohn Baldwin struct ktls_session *tls; 1222f1f93475SJohn Baldwin int error; 1223f1f93475SJohn Baldwin 1224f1f93475SJohn Baldwin if (!ktls_offload_enable) 1225f1f93475SJohn Baldwin return (ENOTSUP); 1226f1f93475SJohn Baldwin 1227f1f93475SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1228f1f93475SJohn Baldwin 1229f1f93475SJohn Baldwin /* 1230f1f93475SJohn Baldwin * This should always be true since only the TCP socket option 1231f1f93475SJohn Baldwin * invokes this function. 1232f1f93475SJohn Baldwin */ 1233f1f93475SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1234f1f93475SJohn Baldwin return (EINVAL); 1235f1f93475SJohn Baldwin 1236f1f93475SJohn Baldwin /* 1237f1f93475SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1238f1f93475SJohn Baldwin * this to support rekeying in the future. 1239f1f93475SJohn Baldwin */ 1240f1f93475SJohn Baldwin if (so->so_rcv.sb_tls_info != NULL) 1241f1f93475SJohn Baldwin return (EALREADY); 1242f1f93475SJohn Baldwin 1243f1f93475SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1244f1f93475SJohn Baldwin return (ENOTSUP); 1245f1f93475SJohn Baldwin 1246fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_RX); 1247f1f93475SJohn Baldwin if (error) 1248f1f93475SJohn Baldwin return (error); 1249f1f93475SJohn Baldwin 125096668a81SJohn Baldwin error = ktls_ocf_try(so, tls, KTLS_RX); 1251f1f93475SJohn Baldwin if (error) { 1252d01db2b8SJohn Baldwin ktls_free(tls); 1253f1f93475SJohn Baldwin return (error); 1254f1f93475SJohn Baldwin } 1255f1f93475SJohn Baldwin 1256f1f93475SJohn Baldwin /* Mark the socket as using TLS offload. */ 1257*b4b33821SMark Johnston SOCK_RECVBUF_LOCK(so); 1258*b4b33821SMark Johnston if (SOLISTENING(so)) { 1259*b4b33821SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1260*b4b33821SMark Johnston ktls_free(tls); 1261*b4b33821SMark Johnston return (EINVAL); 1262*b4b33821SMark Johnston } 12633c0e5685SJohn Baldwin so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq); 1264f1f93475SJohn Baldwin so->so_rcv.sb_tls_info = tls; 12653c0e5685SJohn Baldwin so->so_rcv.sb_flags |= SB_TLS_RX; 12663c0e5685SJohn Baldwin 12673c0e5685SJohn Baldwin /* Mark existing data as not ready until it can be decrypted. */ 12683c0e5685SJohn Baldwin sb_mark_notready(&so->so_rcv); 12693c0e5685SJohn Baldwin ktls_check_rx(&so->so_rcv); 1270*b4b33821SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1271f1f93475SJohn Baldwin 1272fe8c78f0SHans Petter Selasky /* Prefer TOE -> ifnet TLS -> software TLS. */ 1273d958bc79SJohn Baldwin #ifdef TCP_OFFLOAD 1274d958bc79SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_RX); 1275d958bc79SJohn Baldwin if (error) 1276d958bc79SJohn Baldwin #endif 1277fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_RX, false); 1278fe8c78f0SHans Petter Selasky if (error) 1279d958bc79SJohn Baldwin ktls_use_sw(tls); 1280d958bc79SJohn Baldwin 1281f1f93475SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1282f1f93475SJohn Baldwin 1283f1f93475SJohn Baldwin return (0); 1284f1f93475SJohn Baldwin } 1285f1f93475SJohn Baldwin 1286f1f93475SJohn Baldwin int 1287b2e60773SJohn Baldwin ktls_enable_tx(struct socket *so, struct tls_enable *en) 1288b2e60773SJohn Baldwin { 1289b2e60773SJohn Baldwin struct ktls_session *tls; 1290521eac97SJohn Baldwin struct inpcb *inp; 1291c0e4090eSAndrew Gallatin struct tcpcb *tp; 1292b2e60773SJohn Baldwin int error; 1293b2e60773SJohn Baldwin 1294b2e60773SJohn Baldwin if (!ktls_offload_enable) 1295b2e60773SJohn Baldwin return (ENOTSUP); 1296b2e60773SJohn Baldwin 1297b2e60773SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1298b2e60773SJohn Baldwin 1299b2e60773SJohn Baldwin /* 1300b2e60773SJohn Baldwin * This should always be true since only the TCP socket option 1301b2e60773SJohn Baldwin * invokes this function. 1302b2e60773SJohn Baldwin */ 1303b2e60773SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1304b2e60773SJohn Baldwin return (EINVAL); 1305b2e60773SJohn Baldwin 1306b2e60773SJohn Baldwin /* 1307b2e60773SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1308b2e60773SJohn Baldwin * this to support rekeying in the future. 1309b2e60773SJohn Baldwin */ 1310b2e60773SJohn Baldwin if (so->so_snd.sb_tls_info != NULL) 1311b2e60773SJohn Baldwin return (EALREADY); 1312b2e60773SJohn Baldwin 1313b2e60773SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1314b2e60773SJohn Baldwin return (ENOTSUP); 1315b2e60773SJohn Baldwin 1316b2e60773SJohn Baldwin /* TLS requires ext pgs */ 1317b2e60773SJohn Baldwin if (mb_use_ext_pgs == 0) 1318b2e60773SJohn Baldwin return (ENXIO); 1319b2e60773SJohn Baldwin 1320fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_TX); 1321b2e60773SJohn Baldwin if (error) 1322b2e60773SJohn Baldwin return (error); 1323b2e60773SJohn Baldwin 13249e14430dSJohn Baldwin /* Prefer TOE -> ifnet TLS -> software TLS. */ 13259e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 1326f1f93475SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_TX); 13279e14430dSJohn Baldwin if (error) 13289e14430dSJohn Baldwin #endif 1329fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_TX, false); 1330b2e60773SJohn Baldwin if (error) 13313c0e5685SJohn Baldwin error = ktls_try_sw(so, tls, KTLS_TX); 1332b2e60773SJohn Baldwin 1333b2e60773SJohn Baldwin if (error) { 1334d01db2b8SJohn Baldwin ktls_free(tls); 1335b2e60773SJohn Baldwin return (error); 1336b2e60773SJohn Baldwin } 1337b2e60773SJohn Baldwin 1338*b4b33821SMark Johnston /* 1339*b4b33821SMark Johnston * Serialize with sosend_generic() and make sure that we're not 1340*b4b33821SMark Johnston * operating on a listening socket. 1341*b4b33821SMark Johnston */ 1342f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1343b2e60773SJohn Baldwin if (error) { 1344d01db2b8SJohn Baldwin ktls_free(tls); 1345b2e60773SJohn Baldwin return (error); 1346b2e60773SJohn Baldwin } 1347b2e60773SJohn Baldwin 1348521eac97SJohn Baldwin /* 1349521eac97SJohn Baldwin * Write lock the INP when setting sb_tls_info so that 1350521eac97SJohn Baldwin * routines in tcp_ratelimit.c can read sb_tls_info while 1351521eac97SJohn Baldwin * holding the INP lock. 1352521eac97SJohn Baldwin */ 1353521eac97SJohn Baldwin inp = so->so_pcb; 1354521eac97SJohn Baldwin INP_WLOCK(inp); 1355*b4b33821SMark Johnston SOCK_SENDBUF_LOCK(so); 1356ec1db6e1SJohn Baldwin so->so_snd.sb_tls_seqno = be64dec(en->rec_seq); 1357b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls; 1358c0e4090eSAndrew Gallatin if (tls->mode != TCP_TLS_MODE_SW) { 1359c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 1360c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 0); 1361c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 1; 1362c0e4090eSAndrew Gallatin if (tp->t_fb->tfb_hwtls_change != NULL) 1363c0e4090eSAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 1); 1364c0e4090eSAndrew Gallatin } 1365*b4b33821SMark Johnston SOCK_SENDBUF_UNLOCK(so); 1366521eac97SJohn Baldwin INP_WUNLOCK(inp); 1367f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1368b2e60773SJohn Baldwin 1369b2e60773SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1370b2e60773SJohn Baldwin 1371b2e60773SJohn Baldwin return (0); 1372b2e60773SJohn Baldwin } 1373b2e60773SJohn Baldwin 1374b2e60773SJohn Baldwin int 1375bf256782SMark Johnston ktls_get_rx_mode(struct socket *so, int *modep) 1376f1f93475SJohn Baldwin { 1377f1f93475SJohn Baldwin struct ktls_session *tls; 1378a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1379f1f93475SJohn Baldwin 13806685e259SMichael Tuexen if (SOLISTENING(so)) 13816685e259SMichael Tuexen return (EINVAL); 1382f1f93475SJohn Baldwin inp = so->so_pcb; 1383f1f93475SJohn Baldwin INP_WLOCK_ASSERT(inp); 1384bf256782SMark Johnston SOCK_RECVBUF_LOCK(so); 1385f1f93475SJohn Baldwin tls = so->so_rcv.sb_tls_info; 1386f1f93475SJohn Baldwin if (tls == NULL) 1387bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1388f1f93475SJohn Baldwin else 1389bf256782SMark Johnston *modep = tls->mode; 1390bf256782SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1391bf256782SMark Johnston return (0); 1392f1f93475SJohn Baldwin } 1393f1f93475SJohn Baldwin 13949e2cce7eSHans Petter Selasky /* 13959e2cce7eSHans Petter Selasky * ktls_get_rx_sequence - get the next TCP- and TLS- sequence number. 13969e2cce7eSHans Petter Selasky * 13979e2cce7eSHans Petter Selasky * This function gets information about the next TCP- and TLS- 13989e2cce7eSHans Petter Selasky * sequence number to be processed by the TLS receive worker 13999e2cce7eSHans Petter Selasky * thread. The information is extracted from the given "inpcb" 14009e2cce7eSHans Petter Selasky * structure. The values are stored in host endian format at the two 14019e2cce7eSHans Petter Selasky * given output pointer locations. The TCP sequence number points to 14029e2cce7eSHans Petter Selasky * the beginning of the TLS header. 14039e2cce7eSHans Petter Selasky * 14049e2cce7eSHans Petter Selasky * This function returns zero on success, else a non-zero error code 14059e2cce7eSHans Petter Selasky * is returned. 14069e2cce7eSHans Petter Selasky */ 14079e2cce7eSHans Petter Selasky int 14089e2cce7eSHans Petter Selasky ktls_get_rx_sequence(struct inpcb *inp, uint32_t *tcpseq, uint64_t *tlsseq) 14099e2cce7eSHans Petter Selasky { 14109e2cce7eSHans Petter Selasky struct socket *so; 14119e2cce7eSHans Petter Selasky struct tcpcb *tp; 14129e2cce7eSHans Petter Selasky 14139e2cce7eSHans Petter Selasky INP_RLOCK(inp); 14149e2cce7eSHans Petter Selasky so = inp->inp_socket; 14159e2cce7eSHans Petter Selasky if (__predict_false(so == NULL)) { 14169e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14179e2cce7eSHans Petter Selasky return (EINVAL); 14189e2cce7eSHans Petter Selasky } 141953af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 14209e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14219e2cce7eSHans Petter Selasky return (ECONNRESET); 14229e2cce7eSHans Petter Selasky } 14239e2cce7eSHans Petter Selasky 14249e2cce7eSHans Petter Selasky tp = intotcpcb(inp); 14259e2cce7eSHans Petter Selasky MPASS(tp != NULL); 14269e2cce7eSHans Petter Selasky 14279e2cce7eSHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 14289e2cce7eSHans Petter Selasky *tcpseq = tp->rcv_nxt - so->so_rcv.sb_tlscc; 14299e2cce7eSHans Petter Selasky *tlsseq = so->so_rcv.sb_tls_seqno; 14309e2cce7eSHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 14319e2cce7eSHans Petter Selasky 14329e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 14339e2cce7eSHans Petter Selasky 14349e2cce7eSHans Petter Selasky return (0); 14359e2cce7eSHans Petter Selasky } 14369e2cce7eSHans Petter Selasky 1437f1f93475SJohn Baldwin int 1438bf256782SMark Johnston ktls_get_tx_mode(struct socket *so, int *modep) 1439b2e60773SJohn Baldwin { 1440b2e60773SJohn Baldwin struct ktls_session *tls; 1441a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1442b2e60773SJohn Baldwin 14436685e259SMichael Tuexen if (SOLISTENING(so)) 14446685e259SMichael Tuexen return (EINVAL); 1445b2e60773SJohn Baldwin inp = so->so_pcb; 1446b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1447bf256782SMark Johnston SOCK_SENDBUF_LOCK(so); 1448b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1449b2e60773SJohn Baldwin if (tls == NULL) 1450bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1451b2e60773SJohn Baldwin else 1452bf256782SMark Johnston *modep = tls->mode; 1453bf256782SMark Johnston SOCK_SENDBUF_UNLOCK(so); 1454bf256782SMark Johnston return (0); 1455b2e60773SJohn Baldwin } 1456b2e60773SJohn Baldwin 1457b2e60773SJohn Baldwin /* 1458b2e60773SJohn Baldwin * Switch between SW and ifnet TLS sessions as requested. 1459b2e60773SJohn Baldwin */ 1460b2e60773SJohn Baldwin int 1461b2e60773SJohn Baldwin ktls_set_tx_mode(struct socket *so, int mode) 1462b2e60773SJohn Baldwin { 1463b2e60773SJohn Baldwin struct ktls_session *tls, *tls_new; 1464b2e60773SJohn Baldwin struct inpcb *inp; 1465c0e4090eSAndrew Gallatin struct tcpcb *tp; 1466b2e60773SJohn Baldwin int error; 1467b2e60773SJohn Baldwin 14686685e259SMichael Tuexen if (SOLISTENING(so)) 14696685e259SMichael Tuexen return (EINVAL); 14709e14430dSJohn Baldwin switch (mode) { 14719e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 14729e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 14739e14430dSJohn Baldwin break; 14749e14430dSJohn Baldwin default: 14759e14430dSJohn Baldwin return (EINVAL); 14769e14430dSJohn Baldwin } 1477b2e60773SJohn Baldwin 1478b2e60773SJohn Baldwin inp = so->so_pcb; 1479b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1480c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 1481c0e4090eSAndrew Gallatin 1482c0e4090eSAndrew Gallatin if (mode == TCP_TLS_MODE_IFNET) { 1483c0e4090eSAndrew Gallatin /* Don't allow enabling ifnet ktls multiple times */ 1484c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit) 1485c0e4090eSAndrew Gallatin return (EALREADY); 1486d24b032bSAndrew Gallatin 1487c0e4090eSAndrew Gallatin /* 1488c0e4090eSAndrew Gallatin * Don't enable ifnet ktls if we disabled it due to an 1489c0e4090eSAndrew Gallatin * excessive retransmission rate 1490c0e4090eSAndrew Gallatin */ 1491c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit_dis) 1492c0e4090eSAndrew Gallatin return (ENXIO); 1493c0e4090eSAndrew Gallatin } 1494c0e4090eSAndrew Gallatin 1495b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1496b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1497b2e60773SJohn Baldwin if (tls == NULL) { 1498b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1499b2e60773SJohn Baldwin return (0); 1500b2e60773SJohn Baldwin } 1501b2e60773SJohn Baldwin 15029e14430dSJohn Baldwin if (tls->mode == mode) { 1503b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1504b2e60773SJohn Baldwin return (0); 1505b2e60773SJohn Baldwin } 1506b2e60773SJohn Baldwin 1507b2e60773SJohn Baldwin tls = ktls_hold(tls); 1508b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1509b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1510b2e60773SJohn Baldwin 1511fe8c78f0SHans Petter Selasky tls_new = ktls_clone_session(tls, KTLS_TX); 1512b2e60773SJohn Baldwin 1513b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1514fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls_new, KTLS_TX, true); 1515b2e60773SJohn Baldwin else 15163c0e5685SJohn Baldwin error = ktls_try_sw(so, tls_new, KTLS_TX); 1517b2e60773SJohn Baldwin if (error) { 1518b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1519b2e60773SJohn Baldwin ktls_free(tls_new); 1520b2e60773SJohn Baldwin ktls_free(tls); 1521b2e60773SJohn Baldwin INP_WLOCK(inp); 1522b2e60773SJohn Baldwin return (error); 1523b2e60773SJohn Baldwin } 1524b2e60773SJohn Baldwin 1525f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1526b2e60773SJohn Baldwin if (error) { 1527b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1528b2e60773SJohn Baldwin ktls_free(tls_new); 1529b2e60773SJohn Baldwin ktls_free(tls); 1530b2e60773SJohn Baldwin INP_WLOCK(inp); 1531b2e60773SJohn Baldwin return (error); 1532b2e60773SJohn Baldwin } 1533b2e60773SJohn Baldwin 1534b2e60773SJohn Baldwin /* 1535b2e60773SJohn Baldwin * If we raced with another session change, keep the existing 1536b2e60773SJohn Baldwin * session. 1537b2e60773SJohn Baldwin */ 1538b2e60773SJohn Baldwin if (tls != so->so_snd.sb_tls_info) { 1539b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1540f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1541b2e60773SJohn Baldwin ktls_free(tls_new); 1542b2e60773SJohn Baldwin ktls_free(tls); 1543b2e60773SJohn Baldwin INP_WLOCK(inp); 1544b2e60773SJohn Baldwin return (EBUSY); 1545b2e60773SJohn Baldwin } 1546b2e60773SJohn Baldwin 1547cd0525f6SJohn Baldwin INP_WLOCK(inp); 1548b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1549b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls_new; 1550c0e4090eSAndrew Gallatin if (tls_new->mode != TCP_TLS_MODE_SW) { 1551c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 0); 1552c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 1; 1553c0e4090eSAndrew Gallatin if (tp->t_fb->tfb_hwtls_change != NULL) 1554c0e4090eSAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 1); 1555c0e4090eSAndrew Gallatin } 1556b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1557f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1558b2e60773SJohn Baldwin 1559b2e60773SJohn Baldwin /* 1560b2e60773SJohn Baldwin * Drop two references on 'tls'. The first is for the 1561b2e60773SJohn Baldwin * ktls_hold() above. The second drops the reference from the 1562b2e60773SJohn Baldwin * socket buffer. 1563b2e60773SJohn Baldwin */ 1564b2e60773SJohn Baldwin KASSERT(tls->refcount >= 2, ("too few references on old session")); 1565b2e60773SJohn Baldwin ktls_free(tls); 1566b2e60773SJohn Baldwin ktls_free(tls); 1567b2e60773SJohn Baldwin 1568b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1569b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_ifnet, 1); 1570b2e60773SJohn Baldwin else 1571b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_sw, 1); 1572b2e60773SJohn Baldwin 1573b2e60773SJohn Baldwin return (0); 1574b2e60773SJohn Baldwin } 1575b2e60773SJohn Baldwin 1576b2e60773SJohn Baldwin /* 1577fe8c78f0SHans Petter Selasky * Try to allocate a new TLS receive tag. This task is scheduled when 1578fe8c78f0SHans Petter Selasky * sbappend_ktls_rx detects an input path change. If a new tag is 1579fe8c78f0SHans Petter Selasky * allocated, replace the tag in the TLS session. If a new tag cannot 1580fe8c78f0SHans Petter Selasky * be allocated, let the session fall back to software decryption. 1581fe8c78f0SHans Petter Selasky */ 1582fe8c78f0SHans Petter Selasky static void 1583fe8c78f0SHans Petter Selasky ktls_reset_receive_tag(void *context, int pending) 1584fe8c78f0SHans Petter Selasky { 1585fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 1586fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1587fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 1588fe8c78f0SHans Petter Selasky struct inpcb *inp; 1589fe8c78f0SHans Petter Selasky struct ifnet *ifp; 1590fe8c78f0SHans Petter Selasky struct socket *so; 1591fe8c78f0SHans Petter Selasky int error; 1592fe8c78f0SHans Petter Selasky 1593fe8c78f0SHans Petter Selasky MPASS(pending == 1); 1594fe8c78f0SHans Petter Selasky 1595fe8c78f0SHans Petter Selasky tls = context; 1596fe8c78f0SHans Petter Selasky so = tls->so; 1597fe8c78f0SHans Petter Selasky inp = so->so_pcb; 1598fe8c78f0SHans Petter Selasky ifp = NULL; 1599fe8c78f0SHans Petter Selasky 1600fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 160153af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1602fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1603fe8c78f0SHans Petter Selasky goto out; 1604fe8c78f0SHans Petter Selasky } 1605fe8c78f0SHans Petter Selasky 1606fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 16070e391a31SHans Petter Selasky mst = tls->snd_tag; 1608fe8c78f0SHans Petter Selasky tls->snd_tag = NULL; 16090e391a31SHans Petter Selasky if (mst != NULL) 16100e391a31SHans Petter Selasky m_snd_tag_rele(mst); 1611fe8c78f0SHans Petter Selasky 1612fe8c78f0SHans Petter Selasky ifp = tls->rx_ifp; 1613fe8c78f0SHans Petter Selasky if_ref(ifp); 1614fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1615fe8c78f0SHans Petter Selasky 1616fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 1617fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 1618fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 1619fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 1620fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 1621fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 1622fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = tls->rx_vlan_id; 1623fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1624fe8c78f0SHans Petter Selasky 1625fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 162608484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS6) == 0) 1627fe8c78f0SHans Petter Selasky goto out; 1628fe8c78f0SHans Petter Selasky } else { 162908484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS4) == 0) 1630fe8c78f0SHans Petter Selasky goto out; 1631fe8c78f0SHans Petter Selasky } 1632fe8c78f0SHans Petter Selasky 1633fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, &mst); 1634fe8c78f0SHans Petter Selasky if (error == 0) { 1635fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 1636fe8c78f0SHans Petter Selasky tls->snd_tag = mst; 1637fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1638fe8c78f0SHans Petter Selasky 1639fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset, 1); 1640fe8c78f0SHans Petter Selasky } else { 1641fe8c78f0SHans Petter Selasky /* 1642fe8c78f0SHans Petter Selasky * Just fall back to software decryption if a tag 1643fe8c78f0SHans Petter Selasky * cannot be allocated leaving the connection intact. 1644fe8c78f0SHans Petter Selasky * If a future input path change switches to another 1645fe8c78f0SHans Petter Selasky * interface this connection will resume ifnet TLS. 1646fe8c78f0SHans Petter Selasky */ 1647fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset_failed, 1); 1648fe8c78f0SHans Petter Selasky } 1649fe8c78f0SHans Petter Selasky 1650fe8c78f0SHans Petter Selasky out: 1651fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1652fe8c78f0SHans Petter Selasky tls->reset_pending = false; 1653fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1654fe8c78f0SHans Petter Selasky 1655fe8c78f0SHans Petter Selasky if (ifp != NULL) 1656fe8c78f0SHans Petter Selasky if_rele(ifp); 1657fe8c78f0SHans Petter Selasky sorele(so); 1658fe8c78f0SHans Petter Selasky ktls_free(tls); 1659fe8c78f0SHans Petter Selasky } 1660fe8c78f0SHans Petter Selasky 1661fe8c78f0SHans Petter Selasky /* 1662b2e60773SJohn Baldwin * Try to allocate a new TLS send tag. This task is scheduled when 1663b2e60773SJohn Baldwin * ip_output detects a route change while trying to transmit a packet 1664b2e60773SJohn Baldwin * holding a TLS record. If a new tag is allocated, replace the tag 1665b2e60773SJohn Baldwin * in the TLS session. Subsequent packets on the connection will use 1666b2e60773SJohn Baldwin * the new tag. If a new tag cannot be allocated, drop the 1667b2e60773SJohn Baldwin * connection. 1668b2e60773SJohn Baldwin */ 1669b2e60773SJohn Baldwin static void 1670b2e60773SJohn Baldwin ktls_reset_send_tag(void *context, int pending) 1671b2e60773SJohn Baldwin { 1672b2e60773SJohn Baldwin struct epoch_tracker et; 1673b2e60773SJohn Baldwin struct ktls_session *tls; 1674b2e60773SJohn Baldwin struct m_snd_tag *old, *new; 1675b2e60773SJohn Baldwin struct inpcb *inp; 1676b2e60773SJohn Baldwin struct tcpcb *tp; 1677b2e60773SJohn Baldwin int error; 1678b2e60773SJohn Baldwin 1679b2e60773SJohn Baldwin MPASS(pending == 1); 1680b2e60773SJohn Baldwin 1681b2e60773SJohn Baldwin tls = context; 1682b2e60773SJohn Baldwin inp = tls->inp; 1683b2e60773SJohn Baldwin 1684b2e60773SJohn Baldwin /* 1685b2e60773SJohn Baldwin * Free the old tag first before allocating a new one. 1686b2e60773SJohn Baldwin * ip[6]_output_send() will treat a NULL send tag the same as 1687b2e60773SJohn Baldwin * an ifp mismatch and drop packets until a new tag is 1688b2e60773SJohn Baldwin * allocated. 1689b2e60773SJohn Baldwin * 1690b2e60773SJohn Baldwin * Write-lock the INP when changing tls->snd_tag since 1691b2e60773SJohn Baldwin * ip[6]_output_send() holds a read-lock when reading the 1692b2e60773SJohn Baldwin * pointer. 1693b2e60773SJohn Baldwin */ 1694b2e60773SJohn Baldwin INP_WLOCK(inp); 1695b2e60773SJohn Baldwin old = tls->snd_tag; 1696b2e60773SJohn Baldwin tls->snd_tag = NULL; 1697b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1698b2e60773SJohn Baldwin if (old != NULL) 1699b2e60773SJohn Baldwin m_snd_tag_rele(old); 1700b2e60773SJohn Baldwin 1701b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(inp, tls, true, &new); 1702b2e60773SJohn Baldwin 1703b2e60773SJohn Baldwin if (error == 0) { 1704b2e60773SJohn Baldwin INP_WLOCK(inp); 1705b2e60773SJohn Baldwin tls->snd_tag = new; 1706b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1707b2e60773SJohn Baldwin tls->reset_pending = false; 1708b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1709b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1710b2e60773SJohn Baldwin 1711b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset, 1); 1712b2e60773SJohn Baldwin 1713b2e60773SJohn Baldwin /* 1714b2e60773SJohn Baldwin * XXX: Should we kick tcp_output explicitly now that 1715b2e60773SJohn Baldwin * the send tag is fixed or just rely on timers? 1716b2e60773SJohn Baldwin */ 1717b2e60773SJohn Baldwin } else { 17181a496125SGleb Smirnoff NET_EPOCH_ENTER(et); 1719b2e60773SJohn Baldwin INP_WLOCK(inp); 172053af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1721b2e60773SJohn Baldwin tp = intotcpcb(inp); 17228840ae22SGleb Smirnoff CURVNET_SET(inp->inp_vnet); 1723b2e60773SJohn Baldwin tp = tcp_drop(tp, ECONNABORTED); 17241f69a509SHans Petter Selasky CURVNET_RESTORE(); 1725b2e60773SJohn Baldwin if (tp != NULL) 1726b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_dropped, 1); 1727b2e60773SJohn Baldwin } 1728c0e4090eSAndrew Gallatin INP_WUNLOCK(inp); 17291a496125SGleb Smirnoff NET_EPOCH_EXIT(et); 1730b2e60773SJohn Baldwin 1731b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_failed, 1); 1732b2e60773SJohn Baldwin 1733b2e60773SJohn Baldwin /* 1734b2e60773SJohn Baldwin * Leave reset_pending true to avoid future tasks while 1735b2e60773SJohn Baldwin * the socket goes away. 1736b2e60773SJohn Baldwin */ 1737b2e60773SJohn Baldwin } 1738b2e60773SJohn Baldwin 1739b2e60773SJohn Baldwin ktls_free(tls); 1740b2e60773SJohn Baldwin } 1741b2e60773SJohn Baldwin 1742fe8c78f0SHans Petter Selasky void 1743fe8c78f0SHans Petter Selasky ktls_input_ifp_mismatch(struct sockbuf *sb, struct ifnet *ifp) 1744fe8c78f0SHans Petter Selasky { 1745fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1746fe8c78f0SHans Petter Selasky struct socket *so; 1747fe8c78f0SHans Petter Selasky 1748fe8c78f0SHans Petter Selasky SOCKBUF_LOCK_ASSERT(sb); 1749fe8c78f0SHans Petter Selasky KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 1750fe8c78f0SHans Petter Selasky __func__, sb)); 1751fe8c78f0SHans Petter Selasky so = __containerof(sb, struct socket, so_rcv); 1752fe8c78f0SHans Petter Selasky 1753fe8c78f0SHans Petter Selasky tls = sb->sb_tls_info; 1754fe8c78f0SHans Petter Selasky if_rele(tls->rx_ifp); 1755fe8c78f0SHans Petter Selasky if_ref(ifp); 1756fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 1757fe8c78f0SHans Petter Selasky 1758fe8c78f0SHans Petter Selasky /* 1759fe8c78f0SHans Petter Selasky * See if we should schedule a task to update the receive tag for 1760fe8c78f0SHans Petter Selasky * this session. 1761fe8c78f0SHans Petter Selasky */ 1762fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1763fe8c78f0SHans Petter Selasky if (!tls->reset_pending) { 1764fe8c78f0SHans Petter Selasky (void) ktls_hold(tls); 1765fe8c78f0SHans Petter Selasky soref(so); 1766fe8c78f0SHans Petter Selasky tls->so = so; 1767fe8c78f0SHans Petter Selasky tls->reset_pending = true; 1768fe8c78f0SHans Petter Selasky taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1769fe8c78f0SHans Petter Selasky } 1770fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1771fe8c78f0SHans Petter Selasky } 1772fe8c78f0SHans Petter Selasky 1773b2e60773SJohn Baldwin int 1774b2e60773SJohn Baldwin ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls) 1775b2e60773SJohn Baldwin { 1776b2e60773SJohn Baldwin 1777b2e60773SJohn Baldwin if (inp == NULL) 1778b2e60773SJohn Baldwin return (ENOBUFS); 1779b2e60773SJohn Baldwin 1780b2e60773SJohn Baldwin INP_LOCK_ASSERT(inp); 1781b2e60773SJohn Baldwin 1782b2e60773SJohn Baldwin /* 1783b2e60773SJohn Baldwin * See if we should schedule a task to update the send tag for 1784b2e60773SJohn Baldwin * this session. 1785b2e60773SJohn Baldwin */ 1786b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1787b2e60773SJohn Baldwin if (!tls->reset_pending) { 1788b2e60773SJohn Baldwin (void) ktls_hold(tls); 1789b2e60773SJohn Baldwin tls->reset_pending = true; 1790b2e60773SJohn Baldwin taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1791b2e60773SJohn Baldwin } 1792b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1793b2e60773SJohn Baldwin return (ENOBUFS); 1794b2e60773SJohn Baldwin } 1795521eac97SJohn Baldwin 1796521eac97SJohn Baldwin #ifdef RATELIMIT 1797521eac97SJohn Baldwin int 1798521eac97SJohn Baldwin ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate) 1799521eac97SJohn Baldwin { 1800521eac97SJohn Baldwin union if_snd_tag_modify_params params = { 1801521eac97SJohn Baldwin .rate_limit.max_rate = max_pacing_rate, 1802521eac97SJohn Baldwin .rate_limit.flags = M_NOWAIT, 1803521eac97SJohn Baldwin }; 1804521eac97SJohn Baldwin struct m_snd_tag *mst; 1805521eac97SJohn Baldwin 1806521eac97SJohn Baldwin /* Can't get to the inp, but it should be locked. */ 1807521eac97SJohn Baldwin /* INP_LOCK_ASSERT(inp); */ 1808521eac97SJohn Baldwin 1809521eac97SJohn Baldwin MPASS(tls->mode == TCP_TLS_MODE_IFNET); 1810521eac97SJohn Baldwin 1811521eac97SJohn Baldwin if (tls->snd_tag == NULL) { 1812521eac97SJohn Baldwin /* 1813521eac97SJohn Baldwin * Resetting send tag, ignore this change. The 1814521eac97SJohn Baldwin * pending reset may or may not see this updated rate 1815521eac97SJohn Baldwin * in the tcpcb. If it doesn't, we will just lose 1816521eac97SJohn Baldwin * this rate change. 1817521eac97SJohn Baldwin */ 1818521eac97SJohn Baldwin return (0); 1819521eac97SJohn Baldwin } 1820521eac97SJohn Baldwin 1821521eac97SJohn Baldwin mst = tls->snd_tag; 1822f0fca646SHans Petter Selasky 1823f0fca646SHans Petter Selasky MPASS(mst != NULL); 1824f0fca646SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT); 1825f0fca646SHans Petter Selasky 1826c782ea8bSJohn Baldwin return (mst->sw->snd_tag_modify(mst, ¶ms)); 1827521eac97SJohn Baldwin } 1828521eac97SJohn Baldwin #endif 1829b2e60773SJohn Baldwin #endif 1830b2e60773SJohn Baldwin 1831c0e4090eSAndrew Gallatin static void 1832c0e4090eSAndrew Gallatin ktls_destroy_help(void *context, int pending __unused) 1833c0e4090eSAndrew Gallatin { 1834c0e4090eSAndrew Gallatin ktls_destroy(context); 1835c0e4090eSAndrew Gallatin } 1836c0e4090eSAndrew Gallatin 1837b2e60773SJohn Baldwin void 1838b2e60773SJohn Baldwin ktls_destroy(struct ktls_session *tls) 1839b2e60773SJohn Baldwin { 1840c0e4090eSAndrew Gallatin struct inpcb *inp; 1841c0e4090eSAndrew Gallatin struct tcpcb *tp; 1842c0e4090eSAndrew Gallatin bool wlocked; 1843c0e4090eSAndrew Gallatin 18445920f99dSJohn Baldwin MPASS(tls->refcount == 0); 1845b2e60773SJohn Baldwin 1846c0e4090eSAndrew Gallatin inp = tls->inp; 1847c0e4090eSAndrew Gallatin if (tls->tx) { 1848c0e4090eSAndrew Gallatin wlocked = INP_WLOCKED(inp); 1849c0e4090eSAndrew Gallatin if (!wlocked && !INP_TRY_WLOCK(inp)) { 1850c0e4090eSAndrew Gallatin /* 1851c0e4090eSAndrew Gallatin * rwlocks read locks are anonymous, and there 1852c0e4090eSAndrew Gallatin * is no way to know if our current thread 1853c0e4090eSAndrew Gallatin * holds an rlock on the inp. As a rough 1854c0e4090eSAndrew Gallatin * estimate, check to see if the thread holds 1855c0e4090eSAndrew Gallatin * *any* rlocks at all. If it does not, then we 1856c0e4090eSAndrew Gallatin * know that we don't hold the inp rlock, and 1857c0e4090eSAndrew Gallatin * can safely take the wlock 1858c0e4090eSAndrew Gallatin */ 1859c0e4090eSAndrew Gallatin if (curthread->td_rw_rlocks == 0) { 1860c0e4090eSAndrew Gallatin INP_WLOCK(inp); 1861c0e4090eSAndrew Gallatin } else { 1862c0e4090eSAndrew Gallatin /* 1863c0e4090eSAndrew Gallatin * We might hold the rlock, so let's 1864c0e4090eSAndrew Gallatin * do the destroy in a taskqueue 1865c0e4090eSAndrew Gallatin * context to avoid a potential 1866c0e4090eSAndrew Gallatin * deadlock. This should be very 1867c0e4090eSAndrew Gallatin * rare. 1868c0e4090eSAndrew Gallatin */ 1869c0e4090eSAndrew Gallatin counter_u64_add(ktls_destroy_task, 1); 1870c0e4090eSAndrew Gallatin TASK_INIT(&tls->destroy_task, 0, 1871c0e4090eSAndrew Gallatin ktls_destroy_help, tls); 1872c0e4090eSAndrew Gallatin (void)taskqueue_enqueue(taskqueue_thread, 1873c0e4090eSAndrew Gallatin &tls->destroy_task); 1874c0e4090eSAndrew Gallatin return; 1875c0e4090eSAndrew Gallatin } 1876c0e4090eSAndrew Gallatin } 1877c0e4090eSAndrew Gallatin } 1878c0e4090eSAndrew Gallatin 18799f03d2c0SJohn Baldwin if (tls->sequential_records) { 18809f03d2c0SJohn Baldwin struct mbuf *m, *n; 18819f03d2c0SJohn Baldwin int page_count; 18829f03d2c0SJohn Baldwin 18839f03d2c0SJohn Baldwin STAILQ_FOREACH_SAFE(m, &tls->pending_records, m_epg_stailq, n) { 18849f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 18859f03d2c0SJohn Baldwin while (page_count > 0) { 18869f03d2c0SJohn Baldwin KASSERT(page_count >= m->m_epg_nrdy, 18879f03d2c0SJohn Baldwin ("%s: too few pages", __func__)); 18889f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 18899f03d2c0SJohn Baldwin m = m_free(m); 18909f03d2c0SJohn Baldwin } 18919f03d2c0SJohn Baldwin } 18929f03d2c0SJohn Baldwin } 18935920f99dSJohn Baldwin 18945920f99dSJohn Baldwin counter_u64_add(ktls_offload_active, -1); 18955920f99dSJohn Baldwin switch (tls->mode) { 18965920f99dSJohn Baldwin case TCP_TLS_MODE_SW: 18975920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 18985920f99dSJohn Baldwin case CRYPTO_AES_CBC: 18995920f99dSJohn Baldwin counter_u64_add(ktls_sw_cbc, -1); 19005920f99dSJohn Baldwin break; 19015920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 19025920f99dSJohn Baldwin counter_u64_add(ktls_sw_gcm, -1); 19035920f99dSJohn Baldwin break; 19045920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 19055920f99dSJohn Baldwin counter_u64_add(ktls_sw_chacha20, -1); 19065920f99dSJohn Baldwin break; 19075920f99dSJohn Baldwin } 19085920f99dSJohn Baldwin break; 19095920f99dSJohn Baldwin case TCP_TLS_MODE_IFNET: 19105920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 19115920f99dSJohn Baldwin case CRYPTO_AES_CBC: 19125920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_cbc, -1); 19135920f99dSJohn Baldwin break; 19145920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 19155920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_gcm, -1); 19165920f99dSJohn Baldwin break; 19175920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 19185920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, -1); 19195920f99dSJohn Baldwin break; 19205920f99dSJohn Baldwin } 19215920f99dSJohn Baldwin if (tls->snd_tag != NULL) 19225920f99dSJohn Baldwin m_snd_tag_rele(tls->snd_tag); 19235920f99dSJohn Baldwin if (tls->rx_ifp != NULL) 19245920f99dSJohn Baldwin if_rele(tls->rx_ifp); 1925c0e4090eSAndrew Gallatin if (tls->tx) { 1926c0e4090eSAndrew Gallatin INP_WLOCK_ASSERT(inp); 1927c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 1928c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 1); 1929c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 0; 1930c0e4090eSAndrew Gallatin } 19315920f99dSJohn Baldwin break; 19325920f99dSJohn Baldwin #ifdef TCP_OFFLOAD 19335920f99dSJohn Baldwin case TCP_TLS_MODE_TOE: 19345920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 19355920f99dSJohn Baldwin case CRYPTO_AES_CBC: 19365920f99dSJohn Baldwin counter_u64_add(ktls_toe_cbc, -1); 19375920f99dSJohn Baldwin break; 19385920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 19395920f99dSJohn Baldwin counter_u64_add(ktls_toe_gcm, -1); 19405920f99dSJohn Baldwin break; 19415920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 19425920f99dSJohn Baldwin counter_u64_add(ktls_toe_chacha20, -1); 19435920f99dSJohn Baldwin break; 19445920f99dSJohn Baldwin } 19455920f99dSJohn Baldwin break; 19465920f99dSJohn Baldwin #endif 19475920f99dSJohn Baldwin } 19485920f99dSJohn Baldwin if (tls->ocf_session != NULL) 19495920f99dSJohn Baldwin ktls_ocf_free(tls); 19505920f99dSJohn Baldwin if (tls->params.auth_key != NULL) { 19515920f99dSJohn Baldwin zfree(tls->params.auth_key, M_KTLS); 19525920f99dSJohn Baldwin tls->params.auth_key = NULL; 19535920f99dSJohn Baldwin tls->params.auth_key_len = 0; 19545920f99dSJohn Baldwin } 19555920f99dSJohn Baldwin if (tls->params.cipher_key != NULL) { 19565920f99dSJohn Baldwin zfree(tls->params.cipher_key, M_KTLS); 19575920f99dSJohn Baldwin tls->params.cipher_key = NULL; 19585920f99dSJohn Baldwin tls->params.cipher_key_len = 0; 19595920f99dSJohn Baldwin } 1960c0e4090eSAndrew Gallatin if (tls->tx) { 1961c0e4090eSAndrew Gallatin INP_WLOCK_ASSERT(inp); 1962c0e4090eSAndrew Gallatin if (!in_pcbrele_wlocked(inp) && !wlocked) 1963c0e4090eSAndrew Gallatin INP_WUNLOCK(inp); 1964c0e4090eSAndrew Gallatin } 19655920f99dSJohn Baldwin explicit_bzero(tls->params.iv, sizeof(tls->params.iv)); 19665920f99dSJohn Baldwin 1967b2e60773SJohn Baldwin uma_zfree(ktls_session_zone, tls); 1968b2e60773SJohn Baldwin } 1969b2e60773SJohn Baldwin 1970b2e60773SJohn Baldwin void 1971b2e60773SJohn Baldwin ktls_seq(struct sockbuf *sb, struct mbuf *m) 1972b2e60773SJohn Baldwin { 1973b2e60773SJohn Baldwin 1974b2e60773SJohn Baldwin for (; m != NULL; m = m->m_next) { 19756edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 1976b2e60773SJohn Baldwin ("ktls_seq: mapped mbuf %p", m)); 1977b2e60773SJohn Baldwin 19787b6c99d0SGleb Smirnoff m->m_epg_seqno = sb->sb_tls_seqno; 1979b2e60773SJohn Baldwin sb->sb_tls_seqno++; 1980b2e60773SJohn Baldwin } 1981b2e60773SJohn Baldwin } 1982b2e60773SJohn Baldwin 1983b2e60773SJohn Baldwin /* 1984b2e60773SJohn Baldwin * Add TLS framing (headers and trailers) to a chain of mbufs. Each 1985b2e60773SJohn Baldwin * mbuf in the chain must be an unmapped mbuf. The payload of the 1986b2e60773SJohn Baldwin * mbuf must be populated with the payload of each TLS record. 1987b2e60773SJohn Baldwin * 1988b2e60773SJohn Baldwin * The record_type argument specifies the TLS record type used when 1989b2e60773SJohn Baldwin * populating the TLS header. 1990b2e60773SJohn Baldwin * 1991b2e60773SJohn Baldwin * The enq_count argument on return is set to the number of pages of 1992b2e60773SJohn Baldwin * payload data for this entire chain that need to be encrypted via SW 1993b2e60773SJohn Baldwin * encryption. The returned value should be passed to ktls_enqueue 1994c2a8fd6fSJohn Baldwin * when scheduling encryption of this chain of mbufs. To handle the 1995c2a8fd6fSJohn Baldwin * special case of empty fragments for TLS 1.0 sessions, an empty 1996c2a8fd6fSJohn Baldwin * fragment counts as one page. 1997b2e60773SJohn Baldwin */ 1998f85e1a80SGleb Smirnoff void 1999b2e60773SJohn Baldwin ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt, 2000b2e60773SJohn Baldwin uint8_t record_type) 2001b2e60773SJohn Baldwin { 2002b2e60773SJohn Baldwin struct tls_record_layer *tlshdr; 2003b2e60773SJohn Baldwin struct mbuf *m; 20047d29eb9aSJohn Baldwin uint64_t *noncep; 2005b2e60773SJohn Baldwin uint16_t tls_len; 2006a90b85ddSMateusz Guzik int maxlen __diagused; 2007b2e60773SJohn Baldwin 2008b2e60773SJohn Baldwin maxlen = tls->params.max_frame_len; 2009b2e60773SJohn Baldwin *enq_cnt = 0; 2010b2e60773SJohn Baldwin for (m = top; m != NULL; m = m->m_next) { 2011b2e60773SJohn Baldwin /* 2012c2a8fd6fSJohn Baldwin * All mbufs in the chain should be TLS records whose 2013c2a8fd6fSJohn Baldwin * payload does not exceed the maximum frame length. 2014c2a8fd6fSJohn Baldwin * 20155de79eedSMark Johnston * Empty TLS 1.0 records are permitted when using CBC. 2016b2e60773SJohn Baldwin */ 20175de79eedSMark Johnston KASSERT(m->m_len <= maxlen && m->m_len >= 0 && 20185de79eedSMark Johnston (m->m_len > 0 || ktls_permit_empty_frames(tls)), 20195de79eedSMark Johnston ("ktls_frame: m %p len %d", m, m->m_len)); 2020c2a8fd6fSJohn Baldwin 2021b2e60773SJohn Baldwin /* 2022b2e60773SJohn Baldwin * TLS frames require unmapped mbufs to store session 2023b2e60773SJohn Baldwin * info. 2024b2e60773SJohn Baldwin */ 20256edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 20265de79eedSMark Johnston ("ktls_frame: mapped mbuf %p (top = %p)", m, top)); 2027b2e60773SJohn Baldwin 2028f85e1a80SGleb Smirnoff tls_len = m->m_len; 2029b2e60773SJohn Baldwin 2030b2e60773SJohn Baldwin /* Save a reference to the session. */ 20317b6c99d0SGleb Smirnoff m->m_epg_tls = ktls_hold(tls); 2032b2e60773SJohn Baldwin 20337b6c99d0SGleb Smirnoff m->m_epg_hdrlen = tls->params.tls_hlen; 20347b6c99d0SGleb Smirnoff m->m_epg_trllen = tls->params.tls_tlen; 2035b2e60773SJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) { 2036b2e60773SJohn Baldwin int bs, delta; 2037b2e60773SJohn Baldwin 2038b2e60773SJohn Baldwin /* 2039b2e60773SJohn Baldwin * AES-CBC pads messages to a multiple of the 2040b2e60773SJohn Baldwin * block size. Note that the padding is 2041b2e60773SJohn Baldwin * applied after the digest and the encryption 2042b2e60773SJohn Baldwin * is done on the "plaintext || mac || padding". 2043b2e60773SJohn Baldwin * At least one byte of padding is always 2044b2e60773SJohn Baldwin * present. 2045b2e60773SJohn Baldwin * 2046b2e60773SJohn Baldwin * Compute the final trailer length assuming 2047b2e60773SJohn Baldwin * at most one block of padding. 204821e3c1fbSJohn Baldwin * tls->params.tls_tlen is the maximum 2049b2e60773SJohn Baldwin * possible trailer length (padding + digest). 2050b2e60773SJohn Baldwin * delta holds the number of excess padding 2051b2e60773SJohn Baldwin * bytes if the maximum were used. Those 2052b2e60773SJohn Baldwin * extra bytes are removed. 2053b2e60773SJohn Baldwin */ 2054b2e60773SJohn Baldwin bs = tls->params.tls_bs; 2055b2e60773SJohn Baldwin delta = (tls_len + tls->params.tls_tlen) & (bs - 1); 20567b6c99d0SGleb Smirnoff m->m_epg_trllen -= delta; 2057b2e60773SJohn Baldwin } 20587b6c99d0SGleb Smirnoff m->m_len += m->m_epg_hdrlen + m->m_epg_trllen; 2059b2e60773SJohn Baldwin 2060b2e60773SJohn Baldwin /* Populate the TLS header. */ 20610c103266SGleb Smirnoff tlshdr = (void *)m->m_epg_hdr; 2062b2e60773SJohn Baldwin tlshdr->tls_vmajor = tls->params.tls_vmajor; 20636554362cSAndrew Gallatin 20646554362cSAndrew Gallatin /* 20656554362cSAndrew Gallatin * TLS 1.3 masquarades as TLS 1.2 with a record type 20666554362cSAndrew Gallatin * of TLS_RLTYPE_APP. 20676554362cSAndrew Gallatin */ 20686554362cSAndrew Gallatin if (tls->params.tls_vminor == TLS_MINOR_VER_THREE && 20696554362cSAndrew Gallatin tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) { 20706554362cSAndrew Gallatin tlshdr->tls_vminor = TLS_MINOR_VER_TWO; 20716554362cSAndrew Gallatin tlshdr->tls_type = TLS_RLTYPE_APP; 20726554362cSAndrew Gallatin /* save the real record type for later */ 20737b6c99d0SGleb Smirnoff m->m_epg_record_type = record_type; 20740c103266SGleb Smirnoff m->m_epg_trail[0] = record_type; 20756554362cSAndrew Gallatin } else { 2076b2e60773SJohn Baldwin tlshdr->tls_vminor = tls->params.tls_vminor; 2077b2e60773SJohn Baldwin tlshdr->tls_type = record_type; 20786554362cSAndrew Gallatin } 2079b2e60773SJohn Baldwin tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr)); 2080b2e60773SJohn Baldwin 2081b2e60773SJohn Baldwin /* 20827d29eb9aSJohn Baldwin * Store nonces / explicit IVs after the end of the 20837d29eb9aSJohn Baldwin * TLS header. 20847d29eb9aSJohn Baldwin * 20857d29eb9aSJohn Baldwin * For GCM with TLS 1.2, an 8 byte nonce is copied 20867d29eb9aSJohn Baldwin * from the end of the IV. The nonce is then 20877d29eb9aSJohn Baldwin * incremented for use by the next record. 20887d29eb9aSJohn Baldwin * 20897d29eb9aSJohn Baldwin * For CBC, a random nonce is inserted for TLS 1.1+. 2090b2e60773SJohn Baldwin */ 20917d29eb9aSJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 20927d29eb9aSJohn Baldwin tls->params.tls_vminor == TLS_MINOR_VER_TWO) { 20937d29eb9aSJohn Baldwin noncep = (uint64_t *)(tls->params.iv + 8); 20947d29eb9aSJohn Baldwin be64enc(tlshdr + 1, *noncep); 20957d29eb9aSJohn Baldwin (*noncep)++; 20967d29eb9aSJohn Baldwin } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 2097b2e60773SJohn Baldwin tls->params.tls_vminor >= TLS_MINOR_VER_ONE) 2098b2e60773SJohn Baldwin arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0); 2099b2e60773SJohn Baldwin 2100b2e60773SJohn Baldwin /* 2101b2e60773SJohn Baldwin * When using SW encryption, mark the mbuf not ready. 2102b2e60773SJohn Baldwin * It will be marked ready via sbready() after the 2103b2e60773SJohn Baldwin * record has been encrypted. 2104b2e60773SJohn Baldwin * 2105b2e60773SJohn Baldwin * When using ifnet TLS, unencrypted TLS records are 2106b2e60773SJohn Baldwin * sent down the stack to the NIC. 2107b2e60773SJohn Baldwin */ 21089e14430dSJohn Baldwin if (tls->mode == TCP_TLS_MODE_SW) { 2109b2e60773SJohn Baldwin m->m_flags |= M_NOTREADY; 2110c2a8fd6fSJohn Baldwin if (__predict_false(tls_len == 0)) { 2111c2a8fd6fSJohn Baldwin /* TLS 1.0 empty fragment. */ 2112d16cb228SJohn Baldwin m->m_epg_nrdy = 1; 2113c2a8fd6fSJohn Baldwin } else 2114d16cb228SJohn Baldwin m->m_epg_nrdy = m->m_epg_npgs; 2115d16cb228SJohn Baldwin *enq_cnt += m->m_epg_nrdy; 2116b2e60773SJohn Baldwin } 2117b2e60773SJohn Baldwin } 2118b2e60773SJohn Baldwin } 2119b2e60773SJohn Baldwin 21205de79eedSMark Johnston bool 21215de79eedSMark Johnston ktls_permit_empty_frames(struct ktls_session *tls) 21225de79eedSMark Johnston { 21235de79eedSMark Johnston return (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 21245de79eedSMark Johnston tls->params.tls_vminor == TLS_MINOR_VER_ZERO); 21255de79eedSMark Johnston } 21265de79eedSMark Johnston 2127b2e60773SJohn Baldwin void 21283c0e5685SJohn Baldwin ktls_check_rx(struct sockbuf *sb) 21293c0e5685SJohn Baldwin { 21303c0e5685SJohn Baldwin struct tls_record_layer hdr; 21313c0e5685SJohn Baldwin struct ktls_wq *wq; 21323c0e5685SJohn Baldwin struct socket *so; 21333c0e5685SJohn Baldwin bool running; 21343c0e5685SJohn Baldwin 21353c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 21363c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 21373c0e5685SJohn Baldwin __func__, sb)); 21383c0e5685SJohn Baldwin so = __containerof(sb, struct socket, so_rcv); 21393c0e5685SJohn Baldwin 21403c0e5685SJohn Baldwin if (sb->sb_flags & SB_TLS_RX_RUNNING) 21413c0e5685SJohn Baldwin return; 21423c0e5685SJohn Baldwin 21433c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 21443c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr)) { 21453c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0) 21463c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 21473c0e5685SJohn Baldwin return; 21483c0e5685SJohn Baldwin } 21493c0e5685SJohn Baldwin 21503c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr); 21513c0e5685SJohn Baldwin 21523c0e5685SJohn Baldwin /* Is the entire record queued? */ 21533c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) { 21543c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0) 21553c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 21563c0e5685SJohn Baldwin return; 21573c0e5685SJohn Baldwin } 21583c0e5685SJohn Baldwin 21593c0e5685SJohn Baldwin sb->sb_flags |= SB_TLS_RX_RUNNING; 21603c0e5685SJohn Baldwin 21613c0e5685SJohn Baldwin soref(so); 21623c0e5685SJohn Baldwin wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index]; 21633c0e5685SJohn Baldwin mtx_lock(&wq->mtx); 21643c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list); 21653c0e5685SJohn Baldwin running = wq->running; 21663c0e5685SJohn Baldwin mtx_unlock(&wq->mtx); 21673c0e5685SJohn Baldwin if (!running) 21683c0e5685SJohn Baldwin wakeup(wq); 21693c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, 1); 21703c0e5685SJohn Baldwin } 21713c0e5685SJohn Baldwin 21723c0e5685SJohn Baldwin static struct mbuf * 21733c0e5685SJohn Baldwin ktls_detach_record(struct sockbuf *sb, int len) 21743c0e5685SJohn Baldwin { 21753c0e5685SJohn Baldwin struct mbuf *m, *n, *top; 21763c0e5685SJohn Baldwin int remain; 21773c0e5685SJohn Baldwin 21783c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 21793c0e5685SJohn Baldwin MPASS(len <= sb->sb_tlscc); 21803c0e5685SJohn Baldwin 21813c0e5685SJohn Baldwin /* 21823c0e5685SJohn Baldwin * If TLS chain is the exact size of the record, 21833c0e5685SJohn Baldwin * just grab the whole record. 21843c0e5685SJohn Baldwin */ 21853c0e5685SJohn Baldwin top = sb->sb_mtls; 21863c0e5685SJohn Baldwin if (sb->sb_tlscc == len) { 21873c0e5685SJohn Baldwin sb->sb_mtls = NULL; 21883c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 21893c0e5685SJohn Baldwin goto out; 21903c0e5685SJohn Baldwin } 21913c0e5685SJohn Baldwin 21923c0e5685SJohn Baldwin /* 21933c0e5685SJohn Baldwin * While it would be nice to use m_split() here, we need 21943c0e5685SJohn Baldwin * to know exactly what m_split() allocates to update the 21953c0e5685SJohn Baldwin * accounting, so do it inline instead. 21963c0e5685SJohn Baldwin */ 21973c0e5685SJohn Baldwin remain = len; 21983c0e5685SJohn Baldwin for (m = top; remain > m->m_len; m = m->m_next) 21993c0e5685SJohn Baldwin remain -= m->m_len; 22003c0e5685SJohn Baldwin 22013c0e5685SJohn Baldwin /* Easy case: don't have to split 'm'. */ 22023c0e5685SJohn Baldwin if (remain == m->m_len) { 22033c0e5685SJohn Baldwin sb->sb_mtls = m->m_next; 22043c0e5685SJohn Baldwin if (sb->sb_mtls == NULL) 22053c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 22063c0e5685SJohn Baldwin m->m_next = NULL; 22073c0e5685SJohn Baldwin goto out; 22083c0e5685SJohn Baldwin } 22093c0e5685SJohn Baldwin 22103c0e5685SJohn Baldwin /* 22113c0e5685SJohn Baldwin * Need to allocate an mbuf to hold the remainder of 'm'. Try 22123c0e5685SJohn Baldwin * with M_NOWAIT first. 22133c0e5685SJohn Baldwin */ 22143c0e5685SJohn Baldwin n = m_get(M_NOWAIT, MT_DATA); 22153c0e5685SJohn Baldwin if (n == NULL) { 22163c0e5685SJohn Baldwin /* 22173c0e5685SJohn Baldwin * Use M_WAITOK with socket buffer unlocked. If 22183c0e5685SJohn Baldwin * 'sb_mtls' changes while the lock is dropped, return 22193c0e5685SJohn Baldwin * NULL to force the caller to retry. 22203c0e5685SJohn Baldwin */ 22213c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 22223c0e5685SJohn Baldwin 22233c0e5685SJohn Baldwin n = m_get(M_WAITOK, MT_DATA); 22243c0e5685SJohn Baldwin 22253c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 22263c0e5685SJohn Baldwin if (sb->sb_mtls != top) { 22273c0e5685SJohn Baldwin m_free(n); 22283c0e5685SJohn Baldwin return (NULL); 22293c0e5685SJohn Baldwin } 22303c0e5685SJohn Baldwin } 2231fe8c78f0SHans Petter Selasky n->m_flags |= (m->m_flags & (M_NOTREADY | M_DECRYPTED)); 22323c0e5685SJohn Baldwin 22333c0e5685SJohn Baldwin /* Store remainder in 'n'. */ 22343c0e5685SJohn Baldwin n->m_len = m->m_len - remain; 22353c0e5685SJohn Baldwin if (m->m_flags & M_EXT) { 22363c0e5685SJohn Baldwin n->m_data = m->m_data + remain; 22373c0e5685SJohn Baldwin mb_dupcl(n, m); 22383c0e5685SJohn Baldwin } else { 22393c0e5685SJohn Baldwin bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len); 22403c0e5685SJohn Baldwin } 22413c0e5685SJohn Baldwin 22423c0e5685SJohn Baldwin /* Trim 'm' and update accounting. */ 22433c0e5685SJohn Baldwin m->m_len -= n->m_len; 22443c0e5685SJohn Baldwin sb->sb_tlscc -= n->m_len; 22453c0e5685SJohn Baldwin sb->sb_ccc -= n->m_len; 22463c0e5685SJohn Baldwin 22473c0e5685SJohn Baldwin /* Account for 'n'. */ 22483c0e5685SJohn Baldwin sballoc_ktls_rx(sb, n); 22493c0e5685SJohn Baldwin 22503c0e5685SJohn Baldwin /* Insert 'n' into the TLS chain. */ 22513c0e5685SJohn Baldwin sb->sb_mtls = n; 22523c0e5685SJohn Baldwin n->m_next = m->m_next; 22533c0e5685SJohn Baldwin if (sb->sb_mtlstail == m) 22543c0e5685SJohn Baldwin sb->sb_mtlstail = n; 22553c0e5685SJohn Baldwin 22563c0e5685SJohn Baldwin /* Detach the record from the TLS chain. */ 22573c0e5685SJohn Baldwin m->m_next = NULL; 22583c0e5685SJohn Baldwin 22593c0e5685SJohn Baldwin out: 22603c0e5685SJohn Baldwin MPASS(m_length(top, NULL) == len); 22613c0e5685SJohn Baldwin for (m = top; m != NULL; m = m->m_next) 22623c0e5685SJohn Baldwin sbfree_ktls_rx(sb, m); 22633c0e5685SJohn Baldwin sb->sb_tlsdcc = len; 22643c0e5685SJohn Baldwin sb->sb_ccc += len; 22653c0e5685SJohn Baldwin SBCHECK(sb); 22663c0e5685SJohn Baldwin return (top); 22673c0e5685SJohn Baldwin } 22683c0e5685SJohn Baldwin 226905a1d0f5SJohn Baldwin /* 227005a1d0f5SJohn Baldwin * Determine the length of the trailing zero padding and find the real 227105a1d0f5SJohn Baldwin * record type in the byte before the padding. 227205a1d0f5SJohn Baldwin * 227305a1d0f5SJohn Baldwin * Walking the mbuf chain backwards is clumsy, so another option would 227405a1d0f5SJohn Baldwin * be to scan forwards remembering the last non-zero byte before the 227505a1d0f5SJohn Baldwin * trailer. However, it would be expensive to scan the entire record. 227605a1d0f5SJohn Baldwin * Instead, find the last non-zero byte of each mbuf in the chain 227705a1d0f5SJohn Baldwin * keeping track of the relative offset of that nonzero byte. 227805a1d0f5SJohn Baldwin * 227905a1d0f5SJohn Baldwin * trail_len is the size of the MAC/tag on input and is set to the 228005a1d0f5SJohn Baldwin * size of the full trailer including padding and the record type on 228105a1d0f5SJohn Baldwin * return. 228205a1d0f5SJohn Baldwin */ 228305a1d0f5SJohn Baldwin static int 228405a1d0f5SJohn Baldwin tls13_find_record_type(struct ktls_session *tls, struct mbuf *m, int tls_len, 228505a1d0f5SJohn Baldwin int *trailer_len, uint8_t *record_typep) 228605a1d0f5SJohn Baldwin { 228705a1d0f5SJohn Baldwin char *cp; 228805a1d0f5SJohn Baldwin u_int digest_start, last_offset, m_len, offset; 228905a1d0f5SJohn Baldwin uint8_t record_type; 229005a1d0f5SJohn Baldwin 229105a1d0f5SJohn Baldwin digest_start = tls_len - *trailer_len; 229205a1d0f5SJohn Baldwin last_offset = 0; 229305a1d0f5SJohn Baldwin offset = 0; 229405a1d0f5SJohn Baldwin for (; m != NULL && offset < digest_start; 229505a1d0f5SJohn Baldwin offset += m->m_len, m = m->m_next) { 229605a1d0f5SJohn Baldwin /* Don't look for padding in the tag. */ 229705a1d0f5SJohn Baldwin m_len = min(digest_start - offset, m->m_len); 229805a1d0f5SJohn Baldwin cp = mtod(m, char *); 229905a1d0f5SJohn Baldwin 230005a1d0f5SJohn Baldwin /* Find last non-zero byte in this mbuf. */ 230105a1d0f5SJohn Baldwin while (m_len > 0 && cp[m_len - 1] == 0) 230205a1d0f5SJohn Baldwin m_len--; 230305a1d0f5SJohn Baldwin if (m_len > 0) { 230405a1d0f5SJohn Baldwin record_type = cp[m_len - 1]; 230505a1d0f5SJohn Baldwin last_offset = offset + m_len; 230605a1d0f5SJohn Baldwin } 230705a1d0f5SJohn Baldwin } 230805a1d0f5SJohn Baldwin if (last_offset < tls->params.tls_hlen) 230905a1d0f5SJohn Baldwin return (EBADMSG); 231005a1d0f5SJohn Baldwin 231105a1d0f5SJohn Baldwin *record_typep = record_type; 231205a1d0f5SJohn Baldwin *trailer_len = tls_len - last_offset + 1; 231305a1d0f5SJohn Baldwin return (0); 231405a1d0f5SJohn Baldwin } 231505a1d0f5SJohn Baldwin 2316fe8c78f0SHans Petter Selasky /* 2317fe8c78f0SHans Petter Selasky * Check if a mbuf chain is fully decrypted at the given offset and 2318fe8c78f0SHans Petter Selasky * length. Returns KTLS_MBUF_CRYPTO_ST_DECRYPTED if all data is 2319fe8c78f0SHans Petter Selasky * decrypted. KTLS_MBUF_CRYPTO_ST_MIXED if there is a mix of encrypted 2320fe8c78f0SHans Petter Selasky * and decrypted data. Else KTLS_MBUF_CRYPTO_ST_ENCRYPTED if all data 2321fe8c78f0SHans Petter Selasky * is encrypted. 2322fe8c78f0SHans Petter Selasky */ 2323fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t 2324fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_state(struct mbuf *mb, int offset, int len) 2325fe8c78f0SHans Petter Selasky { 2326fe8c78f0SHans Petter Selasky int m_flags_ored = 0; 2327fe8c78f0SHans Petter Selasky int m_flags_anded = -1; 2328fe8c78f0SHans Petter Selasky 2329fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2330fe8c78f0SHans Petter Selasky if (offset < mb->m_len) 2331fe8c78f0SHans Petter Selasky break; 2332fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2333fe8c78f0SHans Petter Selasky } 2334fe8c78f0SHans Petter Selasky offset += len; 2335fe8c78f0SHans Petter Selasky 2336fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2337fe8c78f0SHans Petter Selasky m_flags_ored |= mb->m_flags; 2338fe8c78f0SHans Petter Selasky m_flags_anded &= mb->m_flags; 2339fe8c78f0SHans Petter Selasky 2340fe8c78f0SHans Petter Selasky if (offset <= mb->m_len) 2341fe8c78f0SHans Petter Selasky break; 2342fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2343fe8c78f0SHans Petter Selasky } 2344fe8c78f0SHans Petter Selasky MPASS(mb != NULL || offset == 0); 2345fe8c78f0SHans Petter Selasky 2346fe8c78f0SHans Petter Selasky if ((m_flags_ored ^ m_flags_anded) & M_DECRYPTED) 2347fe8c78f0SHans Petter Selasky return (KTLS_MBUF_CRYPTO_ST_MIXED); 2348fe8c78f0SHans Petter Selasky else 2349fe8c78f0SHans Petter Selasky return ((m_flags_ored & M_DECRYPTED) ? 2350fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_DECRYPTED : 2351fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_ENCRYPTED); 2352fe8c78f0SHans Petter Selasky } 2353fe8c78f0SHans Petter Selasky 2354fe8c78f0SHans Petter Selasky /* 2355fe8c78f0SHans Petter Selasky * ktls_resync_ifnet - get HW TLS RX back on track after packet loss 2356fe8c78f0SHans Petter Selasky */ 2357fe8c78f0SHans Petter Selasky static int 2358fe8c78f0SHans Petter Selasky ktls_resync_ifnet(struct socket *so, uint32_t tls_len, uint64_t tls_rcd_num) 2359fe8c78f0SHans Petter Selasky { 2360fe8c78f0SHans Petter Selasky union if_snd_tag_modify_params params; 2361fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 2362fe8c78f0SHans Petter Selasky struct inpcb *inp; 2363fe8c78f0SHans Petter Selasky struct tcpcb *tp; 2364fe8c78f0SHans Petter Selasky 2365fe8c78f0SHans Petter Selasky mst = so->so_rcv.sb_tls_info->snd_tag; 2366fe8c78f0SHans Petter Selasky if (__predict_false(mst == NULL)) 2367fe8c78f0SHans Petter Selasky return (EINVAL); 2368fe8c78f0SHans Petter Selasky 2369fe8c78f0SHans Petter Selasky inp = sotoinpcb(so); 2370fe8c78f0SHans Petter Selasky if (__predict_false(inp == NULL)) 2371fe8c78f0SHans Petter Selasky return (EINVAL); 2372fe8c78f0SHans Petter Selasky 2373fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 237453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2375fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2376fe8c78f0SHans Petter Selasky return (ECONNRESET); 2377fe8c78f0SHans Petter Selasky } 2378fe8c78f0SHans Petter Selasky 2379fe8c78f0SHans Petter Selasky tp = intotcpcb(inp); 2380fe8c78f0SHans Petter Selasky MPASS(tp != NULL); 2381fe8c78f0SHans Petter Selasky 2382fe8c78f0SHans Petter Selasky /* Get the TCP sequence number of the next valid TLS header. */ 2383fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 2384fe8c78f0SHans Petter Selasky params.tls_rx.tls_hdr_tcp_sn = 2385fe8c78f0SHans Petter Selasky tp->rcv_nxt - so->so_rcv.sb_tlscc - tls_len; 2386fe8c78f0SHans Petter Selasky params.tls_rx.tls_rec_length = tls_len; 2387fe8c78f0SHans Petter Selasky params.tls_rx.tls_seq_number = tls_rcd_num; 2388fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 2389fe8c78f0SHans Petter Selasky 2390fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2391fe8c78f0SHans Petter Selasky 2392fe8c78f0SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RX); 2393fe8c78f0SHans Petter Selasky return (mst->sw->snd_tag_modify(mst, ¶ms)); 2394fe8c78f0SHans Petter Selasky } 2395fe8c78f0SHans Petter Selasky 23963c0e5685SJohn Baldwin static void 239769542f26SJohn Baldwin ktls_drop(struct socket *so, int error) 239869542f26SJohn Baldwin { 239969542f26SJohn Baldwin struct epoch_tracker et; 240069542f26SJohn Baldwin struct inpcb *inp = sotoinpcb(so); 240169542f26SJohn Baldwin struct tcpcb *tp; 240269542f26SJohn Baldwin 240369542f26SJohn Baldwin NET_EPOCH_ENTER(et); 240469542f26SJohn Baldwin INP_WLOCK(inp); 240569542f26SJohn Baldwin if (!(inp->inp_flags & INP_DROPPED)) { 240669542f26SJohn Baldwin tp = intotcpcb(inp); 240769542f26SJohn Baldwin CURVNET_SET(inp->inp_vnet); 240869542f26SJohn Baldwin tp = tcp_drop(tp, error); 240969542f26SJohn Baldwin CURVNET_RESTORE(); 241069542f26SJohn Baldwin if (tp != NULL) 241169542f26SJohn Baldwin INP_WUNLOCK(inp); 241207be7517SJohn Baldwin } else { 241307be7517SJohn Baldwin so->so_error = error; 241407be7517SJohn Baldwin SOCK_RECVBUF_LOCK(so); 241507be7517SJohn Baldwin sorwakeup_locked(so); 241669542f26SJohn Baldwin INP_WUNLOCK(inp); 241707be7517SJohn Baldwin } 241869542f26SJohn Baldwin NET_EPOCH_EXIT(et); 241969542f26SJohn Baldwin } 242069542f26SJohn Baldwin 242169542f26SJohn Baldwin static void 24223c0e5685SJohn Baldwin ktls_decrypt(struct socket *so) 24233c0e5685SJohn Baldwin { 24243c0e5685SJohn Baldwin char tls_header[MBUF_PEXT_HDR_LEN]; 24253c0e5685SJohn Baldwin struct ktls_session *tls; 24263c0e5685SJohn Baldwin struct sockbuf *sb; 24273c0e5685SJohn Baldwin struct tls_record_layer *hdr; 24283c0e5685SJohn Baldwin struct tls_get_record tgr; 24293c0e5685SJohn Baldwin struct mbuf *control, *data, *m; 2430fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t state; 24313c0e5685SJohn Baldwin uint64_t seqno; 24323c0e5685SJohn Baldwin int error, remain, tls_len, trail_len; 243305a1d0f5SJohn Baldwin bool tls13; 243405a1d0f5SJohn Baldwin uint8_t vminor, record_type; 24353c0e5685SJohn Baldwin 24363c0e5685SJohn Baldwin hdr = (struct tls_record_layer *)tls_header; 24373c0e5685SJohn Baldwin sb = &so->so_rcv; 24383c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 24393c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING, 24403c0e5685SJohn Baldwin ("%s: socket %p not running", __func__, so)); 24413c0e5685SJohn Baldwin 24423c0e5685SJohn Baldwin tls = sb->sb_tls_info; 24433c0e5685SJohn Baldwin MPASS(tls != NULL); 24443c0e5685SJohn Baldwin 244505a1d0f5SJohn Baldwin tls13 = (tls->params.tls_vminor == TLS_MINOR_VER_THREE); 244605a1d0f5SJohn Baldwin if (tls13) 244705a1d0f5SJohn Baldwin vminor = TLS_MINOR_VER_TWO; 244805a1d0f5SJohn Baldwin else 244905a1d0f5SJohn Baldwin vminor = tls->params.tls_vminor; 24503c0e5685SJohn Baldwin for (;;) { 24513c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 24523c0e5685SJohn Baldwin if (sb->sb_tlscc < tls->params.tls_hlen) 24533c0e5685SJohn Baldwin break; 24543c0e5685SJohn Baldwin 24553c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header); 24563c0e5685SJohn Baldwin tls_len = sizeof(*hdr) + ntohs(hdr->tls_length); 24573c0e5685SJohn Baldwin 24583c0e5685SJohn Baldwin if (hdr->tls_vmajor != tls->params.tls_vmajor || 245905a1d0f5SJohn Baldwin hdr->tls_vminor != vminor) 246005a1d0f5SJohn Baldwin error = EINVAL; 246105a1d0f5SJohn Baldwin else if (tls13 && hdr->tls_type != TLS_RLTYPE_APP) 24623c0e5685SJohn Baldwin error = EINVAL; 24633c0e5685SJohn Baldwin else if (tls_len < tls->params.tls_hlen || tls_len > 24643c0e5685SJohn Baldwin tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 + 24653c0e5685SJohn Baldwin tls->params.tls_tlen) 24663c0e5685SJohn Baldwin error = EMSGSIZE; 24673c0e5685SJohn Baldwin else 24683c0e5685SJohn Baldwin error = 0; 24693c0e5685SJohn Baldwin if (__predict_false(error != 0)) { 24703c0e5685SJohn Baldwin /* 24713c0e5685SJohn Baldwin * We have a corrupted record and are likely 24723c0e5685SJohn Baldwin * out of sync. The connection isn't 24733c0e5685SJohn Baldwin * recoverable at this point, so abort it. 24743c0e5685SJohn Baldwin */ 24753c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 24763c0e5685SJohn Baldwin counter_u64_add(ktls_offload_corrupted_records, 1); 24773c0e5685SJohn Baldwin 247869542f26SJohn Baldwin ktls_drop(so, error); 24793c0e5685SJohn Baldwin goto deref; 24803c0e5685SJohn Baldwin } 24813c0e5685SJohn Baldwin 24823c0e5685SJohn Baldwin /* Is the entire record queued? */ 24833c0e5685SJohn Baldwin if (sb->sb_tlscc < tls_len) 24843c0e5685SJohn Baldwin break; 24853c0e5685SJohn Baldwin 24863c0e5685SJohn Baldwin /* 24873c0e5685SJohn Baldwin * Split out the portion of the mbuf chain containing 24883c0e5685SJohn Baldwin * this TLS record. 24893c0e5685SJohn Baldwin */ 24903c0e5685SJohn Baldwin data = ktls_detach_record(sb, tls_len); 24913c0e5685SJohn Baldwin if (data == NULL) 24923c0e5685SJohn Baldwin continue; 24933c0e5685SJohn Baldwin MPASS(sb->sb_tlsdcc == tls_len); 24943c0e5685SJohn Baldwin 24953c0e5685SJohn Baldwin seqno = sb->sb_tls_seqno; 24963c0e5685SJohn Baldwin sb->sb_tls_seqno++; 24973c0e5685SJohn Baldwin SBCHECK(sb); 24983c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 24993c0e5685SJohn Baldwin 2500fe8c78f0SHans Petter Selasky /* get crypto state for this TLS record */ 2501fe8c78f0SHans Petter Selasky state = ktls_mbuf_crypto_state(data, 0, tls_len); 2502fe8c78f0SHans Petter Selasky 2503fe8c78f0SHans Petter Selasky switch (state) { 2504fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_MIXED: 2505fe8c78f0SHans Petter Selasky error = ktls_ocf_recrypt(tls, hdr, data, seqno); 2506fe8c78f0SHans Petter Selasky if (error) 2507fe8c78f0SHans Petter Selasky break; 2508fe8c78f0SHans Petter Selasky /* FALLTHROUGH */ 2509fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_ENCRYPTED: 2510fe8c78f0SHans Petter Selasky error = ktls_ocf_decrypt(tls, hdr, data, seqno, 2511fe8c78f0SHans Petter Selasky &trail_len); 2512fe8c78f0SHans Petter Selasky if (__predict_true(error == 0)) { 2513fe8c78f0SHans Petter Selasky if (tls13) { 251405a1d0f5SJohn Baldwin error = tls13_find_record_type(tls, data, 251505a1d0f5SJohn Baldwin tls_len, &trail_len, &record_type); 2516fe8c78f0SHans Petter Selasky } else { 251705a1d0f5SJohn Baldwin record_type = hdr->tls_type; 251805a1d0f5SJohn Baldwin } 2519fe8c78f0SHans Petter Selasky } 2520fe8c78f0SHans Petter Selasky break; 2521fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_DECRYPTED: 2522fe8c78f0SHans Petter Selasky /* 2523fe8c78f0SHans Petter Selasky * NIC TLS is only supported for AEAD 2524fe8c78f0SHans Petter Selasky * ciphersuites which used a fixed sized 2525fe8c78f0SHans Petter Selasky * trailer. 2526fe8c78f0SHans Petter Selasky */ 2527fe8c78f0SHans Petter Selasky if (tls13) { 2528fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen - 1; 2529fe8c78f0SHans Petter Selasky error = tls13_find_record_type(tls, data, 2530fe8c78f0SHans Petter Selasky tls_len, &trail_len, &record_type); 2531fe8c78f0SHans Petter Selasky } else { 2532fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen; 2533fe8c78f0SHans Petter Selasky error = 0; 2534fe8c78f0SHans Petter Selasky record_type = hdr->tls_type; 2535fe8c78f0SHans Petter Selasky } 2536fe8c78f0SHans Petter Selasky break; 2537fe8c78f0SHans Petter Selasky default: 2538fe8c78f0SHans Petter Selasky error = EINVAL; 2539fe8c78f0SHans Petter Selasky break; 2540fe8c78f0SHans Petter Selasky } 25413c0e5685SJohn Baldwin if (error) { 25423c0e5685SJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 25433c0e5685SJohn Baldwin 25443c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 25453c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 25463c0e5685SJohn Baldwin /* 25473c0e5685SJohn Baldwin * sbcut/drop/flush discarded these 25483c0e5685SJohn Baldwin * mbufs. 25493c0e5685SJohn Baldwin */ 25503c0e5685SJohn Baldwin m_freem(data); 25513c0e5685SJohn Baldwin break; 25523c0e5685SJohn Baldwin } 25533c0e5685SJohn Baldwin 25543c0e5685SJohn Baldwin /* 25553c0e5685SJohn Baldwin * Drop this TLS record's data, but keep 25563c0e5685SJohn Baldwin * decrypting subsequent records. 25573c0e5685SJohn Baldwin */ 25583c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 25593c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 25603c0e5685SJohn Baldwin 25619a673b71SJohn Baldwin if (error != EMSGSIZE) 25629a673b71SJohn Baldwin error = EBADMSG; 25633c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 25649a673b71SJohn Baldwin so->so_error = error; 25653c0e5685SJohn Baldwin sorwakeup_locked(so); 25663c0e5685SJohn Baldwin CURVNET_RESTORE(); 25673c0e5685SJohn Baldwin 25683c0e5685SJohn Baldwin m_freem(data); 25693c0e5685SJohn Baldwin 25703c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 25713c0e5685SJohn Baldwin continue; 25723c0e5685SJohn Baldwin } 25733c0e5685SJohn Baldwin 25743c0e5685SJohn Baldwin /* Allocate the control mbuf. */ 25756be8944dSMark Johnston memset(&tgr, 0, sizeof(tgr)); 257605a1d0f5SJohn Baldwin tgr.tls_type = record_type; 25773c0e5685SJohn Baldwin tgr.tls_vmajor = hdr->tls_vmajor; 25783c0e5685SJohn Baldwin tgr.tls_vminor = hdr->tls_vminor; 25793c0e5685SJohn Baldwin tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen - 25803c0e5685SJohn Baldwin trail_len); 2581b46667c6SGleb Smirnoff control = sbcreatecontrol(&tgr, sizeof(tgr), 25823c0e5685SJohn Baldwin TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK); 25833c0e5685SJohn Baldwin 25843c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 25853c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 25863c0e5685SJohn Baldwin /* sbcut/drop/flush discarded these mbufs. */ 25873c0e5685SJohn Baldwin MPASS(sb->sb_tlscc == 0); 25883c0e5685SJohn Baldwin m_freem(data); 25893c0e5685SJohn Baldwin m_freem(control); 25903c0e5685SJohn Baldwin break; 25913c0e5685SJohn Baldwin } 25923c0e5685SJohn Baldwin 25933c0e5685SJohn Baldwin /* 25943c0e5685SJohn Baldwin * Clear the 'dcc' accounting in preparation for 25953c0e5685SJohn Baldwin * adding the decrypted record. 25963c0e5685SJohn Baldwin */ 25973c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 25983c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 25993c0e5685SJohn Baldwin SBCHECK(sb); 26003c0e5685SJohn Baldwin 26013c0e5685SJohn Baldwin /* If there is no payload, drop all of the data. */ 26023c0e5685SJohn Baldwin if (tgr.tls_length == htobe16(0)) { 26033c0e5685SJohn Baldwin m_freem(data); 26043c0e5685SJohn Baldwin data = NULL; 26053c0e5685SJohn Baldwin } else { 26063c0e5685SJohn Baldwin /* Trim header. */ 26073c0e5685SJohn Baldwin remain = tls->params.tls_hlen; 26083c0e5685SJohn Baldwin while (remain > 0) { 26093c0e5685SJohn Baldwin if (data->m_len > remain) { 26103c0e5685SJohn Baldwin data->m_data += remain; 26113c0e5685SJohn Baldwin data->m_len -= remain; 26123c0e5685SJohn Baldwin break; 26133c0e5685SJohn Baldwin } 26143c0e5685SJohn Baldwin remain -= data->m_len; 26153c0e5685SJohn Baldwin data = m_free(data); 26163c0e5685SJohn Baldwin } 26173c0e5685SJohn Baldwin 26183c0e5685SJohn Baldwin /* Trim trailer and clear M_NOTREADY. */ 26193c0e5685SJohn Baldwin remain = be16toh(tgr.tls_length); 26203c0e5685SJohn Baldwin m = data; 26213c0e5685SJohn Baldwin for (m = data; remain > m->m_len; m = m->m_next) { 2622fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 26233c0e5685SJohn Baldwin remain -= m->m_len; 26243c0e5685SJohn Baldwin } 26253c0e5685SJohn Baldwin m->m_len = remain; 26263c0e5685SJohn Baldwin m_freem(m->m_next); 26273c0e5685SJohn Baldwin m->m_next = NULL; 2628fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 26293c0e5685SJohn Baldwin 26303c0e5685SJohn Baldwin /* Set EOR on the final mbuf. */ 26313c0e5685SJohn Baldwin m->m_flags |= M_EOR; 26323c0e5685SJohn Baldwin } 26333c0e5685SJohn Baldwin 26343c0e5685SJohn Baldwin sbappendcontrol_locked(sb, data, control, 0); 2635fe8c78f0SHans Petter Selasky 2636fe8c78f0SHans Petter Selasky if (__predict_false(state != KTLS_MBUF_CRYPTO_ST_DECRYPTED)) { 2637fe8c78f0SHans Petter Selasky sb->sb_flags |= SB_TLS_RX_RESYNC; 2638fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2639fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, tls_len, seqno); 2640fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2641fe8c78f0SHans Petter Selasky } else if (__predict_false(sb->sb_flags & SB_TLS_RX_RESYNC)) { 2642fe8c78f0SHans Petter Selasky sb->sb_flags &= ~SB_TLS_RX_RESYNC; 2643fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2644fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, 0, seqno); 2645fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2646fe8c78f0SHans Petter Selasky } 26473c0e5685SJohn Baldwin } 26483c0e5685SJohn Baldwin 26493c0e5685SJohn Baldwin sb->sb_flags &= ~SB_TLS_RX_RUNNING; 26503c0e5685SJohn Baldwin 26513c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0) 26523c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 26533c0e5685SJohn Baldwin 26543c0e5685SJohn Baldwin sorwakeup_locked(so); 26553c0e5685SJohn Baldwin 26563c0e5685SJohn Baldwin deref: 26573c0e5685SJohn Baldwin SOCKBUF_UNLOCK_ASSERT(sb); 26583c0e5685SJohn Baldwin 26593c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 26603c0e5685SJohn Baldwin sorele(so); 26613c0e5685SJohn Baldwin CURVNET_RESTORE(); 26623c0e5685SJohn Baldwin } 26633c0e5685SJohn Baldwin 26643c0e5685SJohn Baldwin void 2665d90fe9d0SGleb Smirnoff ktls_enqueue_to_free(struct mbuf *m) 2666b2e60773SJohn Baldwin { 2667b2e60773SJohn Baldwin struct ktls_wq *wq; 2668b2e60773SJohn Baldwin bool running; 2669b2e60773SJohn Baldwin 2670b2e60773SJohn Baldwin /* Mark it for freeing. */ 26717b6c99d0SGleb Smirnoff m->m_epg_flags |= EPG_FLAG_2FREE; 26727b6c99d0SGleb Smirnoff wq = &ktls_wq[m->m_epg_tls->wq_index]; 2673b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 26743c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 2675b2e60773SJohn Baldwin running = wq->running; 2676b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2677b2e60773SJohn Baldwin if (!running) 2678b2e60773SJohn Baldwin wakeup(wq); 2679b2e60773SJohn Baldwin } 2680b2e60773SJohn Baldwin 268149f6925cSMark Johnston static void * 268249f6925cSMark Johnston ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m) 268349f6925cSMark Johnston { 268449f6925cSMark Johnston void *buf; 268598215005SAndrew Gallatin int domain, running; 268649f6925cSMark Johnston 268749f6925cSMark Johnston if (m->m_epg_npgs <= 2) 268849f6925cSMark Johnston return (NULL); 268949f6925cSMark Johnston if (ktls_buffer_zone == NULL) 269049f6925cSMark Johnston return (NULL); 269149f6925cSMark Johnston if ((u_int)(ticks - wq->lastallocfail) < hz) { 269249f6925cSMark Johnston /* 269349f6925cSMark Johnston * Rate-limit allocation attempts after a failure. 269449f6925cSMark Johnston * ktls_buffer_import() will acquire a per-domain mutex to check 269549f6925cSMark Johnston * the free page queues and may fail consistently if memory is 269649f6925cSMark Johnston * fragmented. 269749f6925cSMark Johnston */ 269849f6925cSMark Johnston return (NULL); 269949f6925cSMark Johnston } 270049f6925cSMark Johnston buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM); 270198215005SAndrew Gallatin if (buf == NULL) { 270298215005SAndrew Gallatin domain = PCPU_GET(domain); 270349f6925cSMark Johnston wq->lastallocfail = ticks; 270498215005SAndrew Gallatin 270598215005SAndrew Gallatin /* 270698215005SAndrew Gallatin * Note that this check is "racy", but the races are 270798215005SAndrew Gallatin * harmless, and are either a spurious wakeup if 270898215005SAndrew Gallatin * multiple threads fail allocations before the alloc 270998215005SAndrew Gallatin * thread wakes, or waiting an extra second in case we 271098215005SAndrew Gallatin * see an old value of running == true. 271198215005SAndrew Gallatin */ 271298215005SAndrew Gallatin if (!VM_DOMAIN_EMPTY(domain)) { 271398215005SAndrew Gallatin running = atomic_load_int(&ktls_domains[domain].alloc_td.running); 271498215005SAndrew Gallatin if (!running) 271598215005SAndrew Gallatin wakeup(&ktls_domains[domain].alloc_td); 271698215005SAndrew Gallatin } 271798215005SAndrew Gallatin } 271849f6925cSMark Johnston return (buf); 271949f6925cSMark Johnston } 272049f6925cSMark Johnston 2721470e851cSJohn Baldwin static int 2722470e851cSJohn Baldwin ktls_encrypt_record(struct ktls_wq *wq, struct mbuf *m, 2723470e851cSJohn Baldwin struct ktls_session *tls, struct ktls_ocf_encrypt_state *state) 2724470e851cSJohn Baldwin { 2725470e851cSJohn Baldwin vm_page_t pg; 2726470e851cSJohn Baldwin int error, i, len, off; 2727470e851cSJohn Baldwin 2728470e851cSJohn Baldwin KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == (M_EXTPG | M_NOTREADY), 2729470e851cSJohn Baldwin ("%p not unready & nomap mbuf\n", m)); 2730470e851cSJohn Baldwin KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen, 2731470e851cSJohn Baldwin ("page count %d larger than maximum frame length %d", m->m_epg_npgs, 2732470e851cSJohn Baldwin ktls_maxlen)); 2733470e851cSJohn Baldwin 2734470e851cSJohn Baldwin /* Anonymous mbufs are encrypted in place. */ 2735470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) != 0) 2736a4c5d490SJohn Baldwin return (ktls_ocf_encrypt(state, tls, m, NULL, 0)); 2737470e851cSJohn Baldwin 2738470e851cSJohn Baldwin /* 2739470e851cSJohn Baldwin * For file-backed mbufs (from sendfile), anonymous wired 2740470e851cSJohn Baldwin * pages are allocated and used as the encryption destination. 2741470e851cSJohn Baldwin */ 2742470e851cSJohn Baldwin if ((state->cbuf = ktls_buffer_alloc(wq, m)) != NULL) { 2743470e851cSJohn Baldwin len = ptoa(m->m_epg_npgs - 1) + m->m_epg_last_len - 2744470e851cSJohn Baldwin m->m_epg_1st_off; 2745470e851cSJohn Baldwin state->dst_iov[0].iov_base = (char *)state->cbuf + 2746470e851cSJohn Baldwin m->m_epg_1st_off; 2747470e851cSJohn Baldwin state->dst_iov[0].iov_len = len; 2748470e851cSJohn Baldwin state->parray[0] = DMAP_TO_PHYS((vm_offset_t)state->cbuf); 2749470e851cSJohn Baldwin i = 1; 2750470e851cSJohn Baldwin } else { 2751470e851cSJohn Baldwin off = m->m_epg_1st_off; 2752470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++, off = 0) { 2753a4667e09SMark Johnston pg = vm_page_alloc_noobj(VM_ALLOC_NODUMP | 2754a4667e09SMark Johnston VM_ALLOC_WIRED | VM_ALLOC_WAITOK); 2755470e851cSJohn Baldwin len = m_epg_pagelen(m, i, off); 2756470e851cSJohn Baldwin state->parray[i] = VM_PAGE_TO_PHYS(pg); 2757470e851cSJohn Baldwin state->dst_iov[i].iov_base = 2758470e851cSJohn Baldwin (char *)PHYS_TO_DMAP(state->parray[i]) + off; 2759470e851cSJohn Baldwin state->dst_iov[i].iov_len = len; 2760470e851cSJohn Baldwin } 2761470e851cSJohn Baldwin } 2762470e851cSJohn Baldwin KASSERT(i + 1 <= nitems(state->dst_iov), ("dst_iov is too small")); 2763470e851cSJohn Baldwin state->dst_iov[i].iov_base = m->m_epg_trail; 2764470e851cSJohn Baldwin state->dst_iov[i].iov_len = m->m_epg_trllen; 2765470e851cSJohn Baldwin 2766a4c5d490SJohn Baldwin error = ktls_ocf_encrypt(state, tls, m, state->dst_iov, i + 1); 2767470e851cSJohn Baldwin 2768470e851cSJohn Baldwin if (__predict_false(error != 0)) { 2769470e851cSJohn Baldwin /* Free the anonymous pages. */ 2770470e851cSJohn Baldwin if (state->cbuf != NULL) 2771470e851cSJohn Baldwin uma_zfree(ktls_buffer_zone, state->cbuf); 2772470e851cSJohn Baldwin else { 2773470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) { 2774470e851cSJohn Baldwin pg = PHYS_TO_VM_PAGE(state->parray[i]); 2775470e851cSJohn Baldwin (void)vm_page_unwire_noq(pg); 2776470e851cSJohn Baldwin vm_page_free(pg); 2777470e851cSJohn Baldwin } 2778470e851cSJohn Baldwin } 2779470e851cSJohn Baldwin } 2780470e851cSJohn Baldwin return (error); 2781470e851cSJohn Baldwin } 2782470e851cSJohn Baldwin 27839f03d2c0SJohn Baldwin /* Number of TLS records in a batch passed to ktls_enqueue(). */ 27849f03d2c0SJohn Baldwin static u_int 27859f03d2c0SJohn Baldwin ktls_batched_records(struct mbuf *m) 27869f03d2c0SJohn Baldwin { 27879f03d2c0SJohn Baldwin int page_count, records; 27889f03d2c0SJohn Baldwin 27899f03d2c0SJohn Baldwin records = 0; 27909f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 27919f03d2c0SJohn Baldwin while (page_count > 0) { 27929f03d2c0SJohn Baldwin records++; 27939f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 27949f03d2c0SJohn Baldwin m = m->m_next; 27959f03d2c0SJohn Baldwin } 27969f03d2c0SJohn Baldwin KASSERT(page_count == 0, ("%s: mismatched page count", __func__)); 27979f03d2c0SJohn Baldwin return (records); 27989f03d2c0SJohn Baldwin } 27999f03d2c0SJohn Baldwin 2800b2e60773SJohn Baldwin void 2801b2e60773SJohn Baldwin ktls_enqueue(struct mbuf *m, struct socket *so, int page_count) 2802b2e60773SJohn Baldwin { 28039f03d2c0SJohn Baldwin struct ktls_session *tls; 2804b2e60773SJohn Baldwin struct ktls_wq *wq; 28059f03d2c0SJohn Baldwin int queued; 2806b2e60773SJohn Baldwin bool running; 2807b2e60773SJohn Baldwin 28086edfd179SGleb Smirnoff KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) == 28096edfd179SGleb Smirnoff (M_EXTPG | M_NOTREADY)), 2810b2e60773SJohn Baldwin ("ktls_enqueue: %p not unready & nomap mbuf\n", m)); 2811b2e60773SJohn Baldwin KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count")); 2812b2e60773SJohn Baldwin 28137b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf")); 2814b2e60773SJohn Baldwin 28157b6c99d0SGleb Smirnoff m->m_epg_enc_cnt = page_count; 2816b2e60773SJohn Baldwin 2817b2e60773SJohn Baldwin /* 2818b2e60773SJohn Baldwin * Save a pointer to the socket. The caller is responsible 2819b2e60773SJohn Baldwin * for taking an additional reference via soref(). 2820b2e60773SJohn Baldwin */ 28217b6c99d0SGleb Smirnoff m->m_epg_so = so; 2822b2e60773SJohn Baldwin 28239f03d2c0SJohn Baldwin queued = 1; 28249f03d2c0SJohn Baldwin tls = m->m_epg_tls; 28259f03d2c0SJohn Baldwin wq = &ktls_wq[tls->wq_index]; 2826b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 28279f03d2c0SJohn Baldwin if (__predict_false(tls->sequential_records)) { 28289f03d2c0SJohn Baldwin /* 28299f03d2c0SJohn Baldwin * For TLS 1.0, records must be encrypted 28309f03d2c0SJohn Baldwin * sequentially. For a given connection, all records 28319f03d2c0SJohn Baldwin * queued to the associated work queue are processed 28329f03d2c0SJohn Baldwin * sequentially. However, sendfile(2) might complete 28339f03d2c0SJohn Baldwin * I/O requests spanning multiple TLS records out of 28349f03d2c0SJohn Baldwin * order. Here we ensure TLS records are enqueued to 28359f03d2c0SJohn Baldwin * the work queue in FIFO order. 28369f03d2c0SJohn Baldwin * 28379f03d2c0SJohn Baldwin * tls->next_seqno holds the sequence number of the 28389f03d2c0SJohn Baldwin * next TLS record that should be enqueued to the work 28399f03d2c0SJohn Baldwin * queue. If this next record is not tls->next_seqno, 28409f03d2c0SJohn Baldwin * it must be a future record, so insert it, sorted by 28419f03d2c0SJohn Baldwin * TLS sequence number, into tls->pending_records and 28429f03d2c0SJohn Baldwin * return. 28439f03d2c0SJohn Baldwin * 28449f03d2c0SJohn Baldwin * If this TLS record matches tls->next_seqno, place 28459f03d2c0SJohn Baldwin * it in the work queue and then check 28469f03d2c0SJohn Baldwin * tls->pending_records to see if any 28479f03d2c0SJohn Baldwin * previously-queued records are now ready for 28489f03d2c0SJohn Baldwin * encryption. 28499f03d2c0SJohn Baldwin */ 28509f03d2c0SJohn Baldwin if (m->m_epg_seqno != tls->next_seqno) { 28519f03d2c0SJohn Baldwin struct mbuf *n, *p; 28529f03d2c0SJohn Baldwin 28539f03d2c0SJohn Baldwin p = NULL; 28549f03d2c0SJohn Baldwin STAILQ_FOREACH(n, &tls->pending_records, m_epg_stailq) { 28559f03d2c0SJohn Baldwin if (n->m_epg_seqno > m->m_epg_seqno) 28569f03d2c0SJohn Baldwin break; 28579f03d2c0SJohn Baldwin p = n; 28589f03d2c0SJohn Baldwin } 28599f03d2c0SJohn Baldwin if (n == NULL) 28609f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&tls->pending_records, m, 28619f03d2c0SJohn Baldwin m_epg_stailq); 28629f03d2c0SJohn Baldwin else if (p == NULL) 28639f03d2c0SJohn Baldwin STAILQ_INSERT_HEAD(&tls->pending_records, m, 28649f03d2c0SJohn Baldwin m_epg_stailq); 28659f03d2c0SJohn Baldwin else 28669f03d2c0SJohn Baldwin STAILQ_INSERT_AFTER(&tls->pending_records, p, m, 28679f03d2c0SJohn Baldwin m_epg_stailq); 28689f03d2c0SJohn Baldwin mtx_unlock(&wq->mtx); 28699f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, 1); 28709f03d2c0SJohn Baldwin return; 28719f03d2c0SJohn Baldwin } 28729f03d2c0SJohn Baldwin 28739f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(m); 28743c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 28759f03d2c0SJohn Baldwin 28769f03d2c0SJohn Baldwin while (!STAILQ_EMPTY(&tls->pending_records)) { 28779f03d2c0SJohn Baldwin struct mbuf *n; 28789f03d2c0SJohn Baldwin 28799f03d2c0SJohn Baldwin n = STAILQ_FIRST(&tls->pending_records); 28809f03d2c0SJohn Baldwin if (n->m_epg_seqno != tls->next_seqno) 28819f03d2c0SJohn Baldwin break; 28829f03d2c0SJohn Baldwin 28839f03d2c0SJohn Baldwin queued++; 28849f03d2c0SJohn Baldwin STAILQ_REMOVE_HEAD(&tls->pending_records, m_epg_stailq); 28859f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(n); 28869f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, n, m_epg_stailq); 28879f03d2c0SJohn Baldwin } 28889f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, -(queued - 1)); 28899f03d2c0SJohn Baldwin } else 28909f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 28919f03d2c0SJohn Baldwin 2892b2e60773SJohn Baldwin running = wq->running; 2893b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2894b2e60773SJohn Baldwin if (!running) 2895b2e60773SJohn Baldwin wakeup(wq); 28969f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, queued); 2897b2e60773SJohn Baldwin } 2898b2e60773SJohn Baldwin 2899470e851cSJohn Baldwin /* 2900470e851cSJohn Baldwin * Once a file-backed mbuf (from sendfile) has been encrypted, free 2901470e851cSJohn Baldwin * the pages from the file and replace them with the anonymous pages 2902470e851cSJohn Baldwin * allocated in ktls_encrypt_record(). 2903470e851cSJohn Baldwin */ 2904470e851cSJohn Baldwin static void 2905470e851cSJohn Baldwin ktls_finish_nonanon(struct mbuf *m, struct ktls_ocf_encrypt_state *state) 2906470e851cSJohn Baldwin { 2907470e851cSJohn Baldwin int i; 2908470e851cSJohn Baldwin 2909470e851cSJohn Baldwin MPASS((m->m_epg_flags & EPG_FLAG_ANON) == 0); 2910470e851cSJohn Baldwin 2911470e851cSJohn Baldwin /* Free the old pages. */ 2912470e851cSJohn Baldwin m->m_ext.ext_free(m); 2913470e851cSJohn Baldwin 2914470e851cSJohn Baldwin /* Replace them with the new pages. */ 2915470e851cSJohn Baldwin if (state->cbuf != NULL) { 2916470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 2917470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[0] + ptoa(i); 2918470e851cSJohn Baldwin 2919470e851cSJohn Baldwin /* Contig pages should go back to the cache. */ 2920470e851cSJohn Baldwin m->m_ext.ext_free = ktls_free_mext_contig; 2921470e851cSJohn Baldwin } else { 2922470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 2923470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[i]; 2924470e851cSJohn Baldwin 2925470e851cSJohn Baldwin /* Use the basic free routine. */ 2926470e851cSJohn Baldwin m->m_ext.ext_free = mb_free_mext_pgs; 2927470e851cSJohn Baldwin } 2928470e851cSJohn Baldwin 2929470e851cSJohn Baldwin /* Pages are now writable. */ 2930470e851cSJohn Baldwin m->m_epg_flags |= EPG_FLAG_ANON; 2931470e851cSJohn Baldwin } 29326b313a3aSJohn Baldwin 2933b2e60773SJohn Baldwin static __noinline void 293449f6925cSMark Johnston ktls_encrypt(struct ktls_wq *wq, struct mbuf *top) 2935b2e60773SJohn Baldwin { 2936470e851cSJohn Baldwin struct ktls_ocf_encrypt_state state; 2937b2e60773SJohn Baldwin struct ktls_session *tls; 2938b2e60773SJohn Baldwin struct socket *so; 2939d90fe9d0SGleb Smirnoff struct mbuf *m; 2940470e851cSJohn Baldwin int error, npages, total_pages; 2941b2e60773SJohn Baldwin 29427b6c99d0SGleb Smirnoff so = top->m_epg_so; 29437b6c99d0SGleb Smirnoff tls = top->m_epg_tls; 2944d90fe9d0SGleb Smirnoff KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 2945d90fe9d0SGleb Smirnoff KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 2946b2e60773SJohn Baldwin #ifdef INVARIANTS 29477b6c99d0SGleb Smirnoff top->m_epg_so = NULL; 2948b2e60773SJohn Baldwin #endif 29497b6c99d0SGleb Smirnoff total_pages = top->m_epg_enc_cnt; 2950b2e60773SJohn Baldwin npages = 0; 2951b2e60773SJohn Baldwin 2952b2e60773SJohn Baldwin /* 2953b2e60773SJohn Baldwin * Encrypt the TLS records in the chain of mbufs starting with 2954b2e60773SJohn Baldwin * 'top'. 'total_pages' gives us a total count of pages and is 2955b2e60773SJohn Baldwin * used to know when we have finished encrypting the TLS 2956b2e60773SJohn Baldwin * records originally queued with 'top'. 2957b2e60773SJohn Baldwin * 2958b2e60773SJohn Baldwin * NB: These mbufs are queued in the socket buffer and 2959b2e60773SJohn Baldwin * 'm_next' is traversing the mbufs in the socket buffer. The 2960b2e60773SJohn Baldwin * socket buffer lock is not held while traversing this chain. 2961b2e60773SJohn Baldwin * Since the mbufs are all marked M_NOTREADY their 'm_next' 2962b2e60773SJohn Baldwin * pointers should be stable. However, the 'm_next' of the 2963b2e60773SJohn Baldwin * last mbuf encrypted is not necessarily NULL. It can point 2964b2e60773SJohn Baldwin * to other mbufs appended while 'top' was on the TLS work 2965b2e60773SJohn Baldwin * queue. 2966b2e60773SJohn Baldwin * 2967b2e60773SJohn Baldwin * Each mbuf holds an entire TLS record. 2968b2e60773SJohn Baldwin */ 2969b2e60773SJohn Baldwin error = 0; 2970b2e60773SJohn Baldwin for (m = top; npages != total_pages; m = m->m_next) { 29717b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls == tls, 2972b2e60773SJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 29737b6c99d0SGleb Smirnoff tls, m->m_epg_tls)); 29747b6c99d0SGleb Smirnoff KASSERT(npages + m->m_epg_npgs <= total_pages, 2975b2e60773SJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 2976b2e60773SJohn Baldwin total_pages, m)); 2977b2e60773SJohn Baldwin 2978470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, &state); 297921e3c1fbSJohn Baldwin if (error) { 298021e3c1fbSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 298121e3c1fbSJohn Baldwin break; 298221e3c1fbSJohn Baldwin } 298321e3c1fbSJohn Baldwin 2984470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 2985470e851cSJohn Baldwin ktls_finish_nonanon(m, &state); 2986470e851cSJohn Baldwin 2987d16cb228SJohn Baldwin npages += m->m_epg_nrdy; 2988b2e60773SJohn Baldwin 2989b2e60773SJohn Baldwin /* 2990b2e60773SJohn Baldwin * Drop a reference to the session now that it is no 2991b2e60773SJohn Baldwin * longer needed. Existing code depends on encrypted 2992b2e60773SJohn Baldwin * records having no associated session vs 2993b2e60773SJohn Baldwin * yet-to-be-encrypted records having an associated 2994b2e60773SJohn Baldwin * session. 2995b2e60773SJohn Baldwin */ 29967b6c99d0SGleb Smirnoff m->m_epg_tls = NULL; 2997b2e60773SJohn Baldwin ktls_free(tls); 2998b2e60773SJohn Baldwin } 2999b2e60773SJohn Baldwin 3000b2e60773SJohn Baldwin CURVNET_SET(so->so_vnet); 3001b2e60773SJohn Baldwin if (error == 0) { 3002e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, top, npages); 3003b2e60773SJohn Baldwin } else { 300469542f26SJohn Baldwin ktls_drop(so, EIO); 3005b2e60773SJohn Baldwin mb_free_notready(top, total_pages); 3006b2e60773SJohn Baldwin } 3007b2e60773SJohn Baldwin 3008b2e60773SJohn Baldwin sorele(so); 3009b2e60773SJohn Baldwin CURVNET_RESTORE(); 3010b2e60773SJohn Baldwin } 3011b2e60773SJohn Baldwin 3012470e851cSJohn Baldwin void 3013470e851cSJohn Baldwin ktls_encrypt_cb(struct ktls_ocf_encrypt_state *state, int error) 3014470e851cSJohn Baldwin { 3015470e851cSJohn Baldwin struct ktls_session *tls; 3016470e851cSJohn Baldwin struct socket *so; 3017470e851cSJohn Baldwin struct mbuf *m; 3018470e851cSJohn Baldwin int npages; 3019470e851cSJohn Baldwin 3020470e851cSJohn Baldwin m = state->m; 3021470e851cSJohn Baldwin 3022470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 3023470e851cSJohn Baldwin ktls_finish_nonanon(m, state); 3024470e851cSJohn Baldwin 3025470e851cSJohn Baldwin so = state->so; 3026470e851cSJohn Baldwin free(state, M_KTLS); 3027470e851cSJohn Baldwin 3028470e851cSJohn Baldwin /* 3029470e851cSJohn Baldwin * Drop a reference to the session now that it is no longer 3030470e851cSJohn Baldwin * needed. Existing code depends on encrypted records having 3031470e851cSJohn Baldwin * no associated session vs yet-to-be-encrypted records having 3032470e851cSJohn Baldwin * an associated session. 3033470e851cSJohn Baldwin */ 3034470e851cSJohn Baldwin tls = m->m_epg_tls; 3035470e851cSJohn Baldwin m->m_epg_tls = NULL; 3036470e851cSJohn Baldwin ktls_free(tls); 3037470e851cSJohn Baldwin 3038470e851cSJohn Baldwin if (error != 0) 3039470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 3040470e851cSJohn Baldwin 3041470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3042470e851cSJohn Baldwin npages = m->m_epg_nrdy; 3043470e851cSJohn Baldwin 3044470e851cSJohn Baldwin if (error == 0) { 3045e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, m, npages); 3046470e851cSJohn Baldwin } else { 304769542f26SJohn Baldwin ktls_drop(so, EIO); 3048470e851cSJohn Baldwin mb_free_notready(m, npages); 3049470e851cSJohn Baldwin } 3050470e851cSJohn Baldwin 3051470e851cSJohn Baldwin sorele(so); 3052470e851cSJohn Baldwin CURVNET_RESTORE(); 3053470e851cSJohn Baldwin } 3054470e851cSJohn Baldwin 3055470e851cSJohn Baldwin /* 3056470e851cSJohn Baldwin * Similar to ktls_encrypt, but used with asynchronous OCF backends 3057470e851cSJohn Baldwin * (coprocessors) where encryption does not use host CPU resources and 3058470e851cSJohn Baldwin * it can be beneficial to queue more requests than CPUs. 3059470e851cSJohn Baldwin */ 3060470e851cSJohn Baldwin static __noinline void 3061470e851cSJohn Baldwin ktls_encrypt_async(struct ktls_wq *wq, struct mbuf *top) 3062470e851cSJohn Baldwin { 3063470e851cSJohn Baldwin struct ktls_ocf_encrypt_state *state; 3064470e851cSJohn Baldwin struct ktls_session *tls; 3065470e851cSJohn Baldwin struct socket *so; 3066470e851cSJohn Baldwin struct mbuf *m, *n; 3067470e851cSJohn Baldwin int error, mpages, npages, total_pages; 3068470e851cSJohn Baldwin 3069470e851cSJohn Baldwin so = top->m_epg_so; 3070470e851cSJohn Baldwin tls = top->m_epg_tls; 3071470e851cSJohn Baldwin KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 3072470e851cSJohn Baldwin KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 3073470e851cSJohn Baldwin #ifdef INVARIANTS 3074470e851cSJohn Baldwin top->m_epg_so = NULL; 3075470e851cSJohn Baldwin #endif 3076470e851cSJohn Baldwin total_pages = top->m_epg_enc_cnt; 3077470e851cSJohn Baldwin npages = 0; 3078470e851cSJohn Baldwin 3079470e851cSJohn Baldwin error = 0; 3080470e851cSJohn Baldwin for (m = top; npages != total_pages; m = n) { 3081470e851cSJohn Baldwin KASSERT(m->m_epg_tls == tls, 3082470e851cSJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 3083470e851cSJohn Baldwin tls, m->m_epg_tls)); 3084470e851cSJohn Baldwin KASSERT(npages + m->m_epg_npgs <= total_pages, 3085470e851cSJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 3086470e851cSJohn Baldwin total_pages, m)); 3087470e851cSJohn Baldwin 3088470e851cSJohn Baldwin state = malloc(sizeof(*state), M_KTLS, M_WAITOK | M_ZERO); 3089470e851cSJohn Baldwin soref(so); 3090470e851cSJohn Baldwin state->so = so; 3091470e851cSJohn Baldwin state->m = m; 3092470e851cSJohn Baldwin 3093470e851cSJohn Baldwin mpages = m->m_epg_nrdy; 3094470e851cSJohn Baldwin n = m->m_next; 3095470e851cSJohn Baldwin 3096470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, state); 3097470e851cSJohn Baldwin if (error) { 3098470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 3099470e851cSJohn Baldwin free(state, M_KTLS); 3100470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3101470e851cSJohn Baldwin sorele(so); 3102470e851cSJohn Baldwin CURVNET_RESTORE(); 3103470e851cSJohn Baldwin break; 3104470e851cSJohn Baldwin } 3105470e851cSJohn Baldwin 3106470e851cSJohn Baldwin npages += mpages; 3107470e851cSJohn Baldwin } 3108470e851cSJohn Baldwin 3109470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3110470e851cSJohn Baldwin if (error != 0) { 311169542f26SJohn Baldwin ktls_drop(so, EIO); 3112470e851cSJohn Baldwin mb_free_notready(m, total_pages - npages); 3113470e851cSJohn Baldwin } 3114470e851cSJohn Baldwin 3115470e851cSJohn Baldwin sorele(so); 3116470e851cSJohn Baldwin CURVNET_RESTORE(); 3117470e851cSJohn Baldwin } 3118470e851cSJohn Baldwin 3119a72ee355SJohn Baldwin static int 3120a72ee355SJohn Baldwin ktls_bind_domain(int domain) 3121a72ee355SJohn Baldwin { 3122a72ee355SJohn Baldwin int error; 3123a72ee355SJohn Baldwin 3124a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &cpuset_domain[domain]); 3125a72ee355SJohn Baldwin if (error != 0) 3126a72ee355SJohn Baldwin return (error); 3127a72ee355SJohn Baldwin curthread->td_domain.dr_policy = DOMAINSET_PREF(domain); 3128a72ee355SJohn Baldwin return (0); 3129a72ee355SJohn Baldwin } 3130a72ee355SJohn Baldwin 3131b2e60773SJohn Baldwin static void 313298215005SAndrew Gallatin ktls_alloc_thread(void *ctx) 313398215005SAndrew Gallatin { 313498215005SAndrew Gallatin struct ktls_domain_info *ktls_domain = ctx; 313598215005SAndrew Gallatin struct ktls_alloc_thread *sc = &ktls_domain->alloc_td; 313698215005SAndrew Gallatin void **buf; 313798215005SAndrew Gallatin struct sysctl_oid *oid; 313898215005SAndrew Gallatin char name[80]; 3139a72ee355SJohn Baldwin int domain, error, i, nbufs; 314098215005SAndrew Gallatin 3141a72ee355SJohn Baldwin domain = ktls_domain - ktls_domains; 314298215005SAndrew Gallatin if (bootverbose) 3143a72ee355SJohn Baldwin printf("Starting KTLS alloc thread for domain %d\n", domain); 3144a72ee355SJohn Baldwin error = ktls_bind_domain(domain); 3145a72ee355SJohn Baldwin if (error) 3146a72ee355SJohn Baldwin printf("Unable to bind KTLS alloc thread for domain %d: error %d\n", 3147a72ee355SJohn Baldwin domain, error); 3148a72ee355SJohn Baldwin snprintf(name, sizeof(name), "domain%d", domain); 314998215005SAndrew Gallatin oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern_ipc_tls), OID_AUTO, 315098215005SAndrew Gallatin name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 315198215005SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "allocs", 315298215005SAndrew Gallatin CTLFLAG_RD, &sc->allocs, 0, "buffers allocated"); 315398215005SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "wakeups", 315498215005SAndrew Gallatin CTLFLAG_RD, &sc->wakeups, 0, "thread wakeups"); 315598215005SAndrew Gallatin SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "running", 315698215005SAndrew Gallatin CTLFLAG_RD, &sc->running, 0, "thread running"); 315798215005SAndrew Gallatin 315898215005SAndrew Gallatin buf = NULL; 315998215005SAndrew Gallatin nbufs = 0; 316098215005SAndrew Gallatin for (;;) { 316198215005SAndrew Gallatin atomic_store_int(&sc->running, 0); 316209066b98SAndrew Gallatin tsleep(sc, PZERO | PNOLOCK, "-", 0); 316398215005SAndrew Gallatin atomic_store_int(&sc->running, 1); 316498215005SAndrew Gallatin sc->wakeups++; 316598215005SAndrew Gallatin if (nbufs != ktls_max_alloc) { 316698215005SAndrew Gallatin free(buf, M_KTLS); 316798215005SAndrew Gallatin nbufs = atomic_load_int(&ktls_max_alloc); 316898215005SAndrew Gallatin buf = malloc(sizeof(void *) * nbufs, M_KTLS, 316998215005SAndrew Gallatin M_WAITOK | M_ZERO); 317098215005SAndrew Gallatin } 317198215005SAndrew Gallatin /* 317298215005SAndrew Gallatin * Below we allocate nbufs with different allocation 317398215005SAndrew Gallatin * flags than we use when allocating normally during 317498215005SAndrew Gallatin * encryption in the ktls worker thread. We specify 317598215005SAndrew Gallatin * M_NORECLAIM in the worker thread. However, we omit 317698215005SAndrew Gallatin * that flag here and add M_WAITOK so that the VM 317798215005SAndrew Gallatin * system is permitted to perform expensive work to 317898215005SAndrew Gallatin * defragment memory. We do this here, as it does not 317998215005SAndrew Gallatin * matter if this thread blocks. If we block a ktls 318098215005SAndrew Gallatin * worker thread, we risk developing backlogs of 318198215005SAndrew Gallatin * buffers to be encrypted, leading to surges of 318298215005SAndrew Gallatin * traffic and potential NIC output drops. 318398215005SAndrew Gallatin */ 318498215005SAndrew Gallatin for (i = 0; i < nbufs; i++) { 318598215005SAndrew Gallatin buf[i] = uma_zalloc(ktls_buffer_zone, M_WAITOK); 318698215005SAndrew Gallatin sc->allocs++; 318798215005SAndrew Gallatin } 318898215005SAndrew Gallatin for (i = 0; i < nbufs; i++) { 318998215005SAndrew Gallatin uma_zfree(ktls_buffer_zone, buf[i]); 319098215005SAndrew Gallatin buf[i] = NULL; 319198215005SAndrew Gallatin } 319298215005SAndrew Gallatin } 319398215005SAndrew Gallatin } 319498215005SAndrew Gallatin 319598215005SAndrew Gallatin static void 3196b2e60773SJohn Baldwin ktls_work_thread(void *ctx) 3197b2e60773SJohn Baldwin { 3198b2e60773SJohn Baldwin struct ktls_wq *wq = ctx; 3199d90fe9d0SGleb Smirnoff struct mbuf *m, *n; 32003c0e5685SJohn Baldwin struct socket *so, *son; 32013c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) local_m_head; 32023c0e5685SJohn Baldwin STAILQ_HEAD(, socket) local_so_head; 3203a72ee355SJohn Baldwin int cpu; 3204a72ee355SJohn Baldwin 3205a72ee355SJohn Baldwin cpu = wq - ktls_wq; 3206a72ee355SJohn Baldwin if (bootverbose) 3207a72ee355SJohn Baldwin printf("Starting KTLS worker thread for CPU %d\n", cpu); 3208a72ee355SJohn Baldwin 3209a72ee355SJohn Baldwin /* 3210a72ee355SJohn Baldwin * Bind to a core. If ktls_bind_threads is > 1, then 3211a72ee355SJohn Baldwin * we bind to the NUMA domain instead. 3212a72ee355SJohn Baldwin */ 3213a72ee355SJohn Baldwin if (ktls_bind_threads) { 3214a72ee355SJohn Baldwin int error; 3215b2e60773SJohn Baldwin 321602bc3865SAndrew Gallatin if (ktls_bind_threads > 1) { 3217a72ee355SJohn Baldwin struct pcpu *pc = pcpu_find(cpu); 3218a72ee355SJohn Baldwin 3219a72ee355SJohn Baldwin error = ktls_bind_domain(pc->pc_domain); 3220a72ee355SJohn Baldwin } else { 3221a72ee355SJohn Baldwin cpuset_t mask; 3222a72ee355SJohn Baldwin 3223a72ee355SJohn Baldwin CPU_SETOF(cpu, &mask); 3224a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &mask); 3225a72ee355SJohn Baldwin } 3226a72ee355SJohn Baldwin if (error) 3227a72ee355SJohn Baldwin printf("Unable to bind KTLS worker thread for CPU %d: error %d\n", 3228a72ee355SJohn Baldwin cpu, error); 322902bc3865SAndrew Gallatin } 3230b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 3231b2e60773SJohn Baldwin fpu_kern_thread(0); 3232b2e60773SJohn Baldwin #endif 3233b2e60773SJohn Baldwin for (;;) { 3234b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 32353c0e5685SJohn Baldwin while (STAILQ_EMPTY(&wq->m_head) && 32363c0e5685SJohn Baldwin STAILQ_EMPTY(&wq->so_head)) { 3237b2e60773SJohn Baldwin wq->running = false; 3238b2e60773SJohn Baldwin mtx_sleep(wq, &wq->mtx, 0, "-", 0); 3239b2e60773SJohn Baldwin wq->running = true; 3240b2e60773SJohn Baldwin } 3241b2e60773SJohn Baldwin 32423c0e5685SJohn Baldwin STAILQ_INIT(&local_m_head); 32433c0e5685SJohn Baldwin STAILQ_CONCAT(&local_m_head, &wq->m_head); 32443c0e5685SJohn Baldwin STAILQ_INIT(&local_so_head); 32453c0e5685SJohn Baldwin STAILQ_CONCAT(&local_so_head, &wq->so_head); 3246b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 3247b2e60773SJohn Baldwin 32483c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) { 32497b6c99d0SGleb Smirnoff if (m->m_epg_flags & EPG_FLAG_2FREE) { 32507b6c99d0SGleb Smirnoff ktls_free(m->m_epg_tls); 3251904a08f3SMateusz Guzik m_free_raw(m); 3252eeec8348SGleb Smirnoff } else { 3253470e851cSJohn Baldwin if (m->m_epg_tls->sync_dispatch) 325449f6925cSMark Johnston ktls_encrypt(wq, m); 3255470e851cSJohn Baldwin else 3256470e851cSJohn Baldwin ktls_encrypt_async(wq, m); 32573c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, -1); 3258b2e60773SJohn Baldwin } 3259b2e60773SJohn Baldwin } 32603c0e5685SJohn Baldwin 32613c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) { 32623c0e5685SJohn Baldwin ktls_decrypt(so); 32633c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, -1); 32643c0e5685SJohn Baldwin } 3265b2e60773SJohn Baldwin } 3266b2e60773SJohn Baldwin } 326728d0a740SAndrew Gallatin 32684150a5a8SAndrew Gallatin #if defined(INET) || defined(INET6) 326928d0a740SAndrew Gallatin static void 327028d0a740SAndrew Gallatin ktls_disable_ifnet_help(void *context, int pending __unused) 327128d0a740SAndrew Gallatin { 327228d0a740SAndrew Gallatin struct ktls_session *tls; 327328d0a740SAndrew Gallatin struct inpcb *inp; 327428d0a740SAndrew Gallatin struct tcpcb *tp; 327528d0a740SAndrew Gallatin struct socket *so; 327628d0a740SAndrew Gallatin int err; 327728d0a740SAndrew Gallatin 327828d0a740SAndrew Gallatin tls = context; 327928d0a740SAndrew Gallatin inp = tls->inp; 328028d0a740SAndrew Gallatin if (inp == NULL) 328128d0a740SAndrew Gallatin return; 328228d0a740SAndrew Gallatin INP_WLOCK(inp); 328328d0a740SAndrew Gallatin so = inp->inp_socket; 328428d0a740SAndrew Gallatin MPASS(so != NULL); 328553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 328628d0a740SAndrew Gallatin goto out; 328728d0a740SAndrew Gallatin } 328828d0a740SAndrew Gallatin 328928d0a740SAndrew Gallatin if (so->so_snd.sb_tls_info != NULL) 329028d0a740SAndrew Gallatin err = ktls_set_tx_mode(so, TCP_TLS_MODE_SW); 329128d0a740SAndrew Gallatin else 329228d0a740SAndrew Gallatin err = ENXIO; 329328d0a740SAndrew Gallatin if (err == 0) { 329428d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_ok, 1); 329528d0a740SAndrew Gallatin /* ktls_set_tx_mode() drops inp wlock, so recheck flags */ 329653af6903SGleb Smirnoff if ((inp->inp_flags & INP_DROPPED) == 0 && 329728d0a740SAndrew Gallatin (tp = intotcpcb(inp)) != NULL && 329828d0a740SAndrew Gallatin tp->t_fb->tfb_hwtls_change != NULL) 329928d0a740SAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 0); 330028d0a740SAndrew Gallatin } else { 330128d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_fail, 1); 330228d0a740SAndrew Gallatin } 330328d0a740SAndrew Gallatin 330428d0a740SAndrew Gallatin out: 3305846e4a20SJohn Baldwin CURVNET_SET(so->so_vnet); 330628d0a740SAndrew Gallatin sorele(so); 3307846e4a20SJohn Baldwin CURVNET_RESTORE(); 330828d0a740SAndrew Gallatin INP_WUNLOCK(inp); 330928d0a740SAndrew Gallatin ktls_free(tls); 331028d0a740SAndrew Gallatin } 331128d0a740SAndrew Gallatin 331228d0a740SAndrew Gallatin /* 331328d0a740SAndrew Gallatin * Called when re-transmits are becoming a substantial portion of the 331428d0a740SAndrew Gallatin * sends on this connection. When this happens, we transition the 331528d0a740SAndrew Gallatin * connection to software TLS. This is needed because most inline TLS 331628d0a740SAndrew Gallatin * NICs keep crypto state only for in-order transmits. This means 331728d0a740SAndrew Gallatin * that to handle a TCP rexmit (which is out-of-order), the NIC must 331828d0a740SAndrew Gallatin * re-DMA the entire TLS record up to and including the current 331928d0a740SAndrew Gallatin * segment. This means that when re-transmitting the last ~1448 byte 332028d0a740SAndrew Gallatin * segment of a 16KB TLS record, we could wind up re-DMA'ing an order 332128d0a740SAndrew Gallatin * of magnitude more data than we are sending. This can cause the 332228d0a740SAndrew Gallatin * PCIe link to saturate well before the network, which can cause 332328d0a740SAndrew Gallatin * output drops, and a general loss of capacity. 332428d0a740SAndrew Gallatin */ 332528d0a740SAndrew Gallatin void 332628d0a740SAndrew Gallatin ktls_disable_ifnet(void *arg) 332728d0a740SAndrew Gallatin { 332828d0a740SAndrew Gallatin struct tcpcb *tp; 332928d0a740SAndrew Gallatin struct inpcb *inp; 333028d0a740SAndrew Gallatin struct socket *so; 333128d0a740SAndrew Gallatin struct ktls_session *tls; 333228d0a740SAndrew Gallatin 333328d0a740SAndrew Gallatin tp = arg; 33349eb0e832SGleb Smirnoff inp = tptoinpcb(tp); 333528d0a740SAndrew Gallatin INP_WLOCK_ASSERT(inp); 333628d0a740SAndrew Gallatin so = inp->inp_socket; 333728d0a740SAndrew Gallatin SOCK_LOCK(so); 333828d0a740SAndrew Gallatin tls = so->so_snd.sb_tls_info; 3339c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit_dis == 1) { 334028d0a740SAndrew Gallatin SOCK_UNLOCK(so); 334128d0a740SAndrew Gallatin return; 334228d0a740SAndrew Gallatin } 3343d24b032bSAndrew Gallatin 334428d0a740SAndrew Gallatin /* 3345c0e4090eSAndrew Gallatin * note that t_nic_ktls_xmit_dis is never cleared; disabling 3346c0e4090eSAndrew Gallatin * ifnet can only be done once per connection, so we never want 334728d0a740SAndrew Gallatin * to do it again 334828d0a740SAndrew Gallatin */ 334928d0a740SAndrew Gallatin 335028d0a740SAndrew Gallatin (void)ktls_hold(tls); 335128d0a740SAndrew Gallatin soref(so); 3352c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit_dis = 1; 335328d0a740SAndrew Gallatin SOCK_UNLOCK(so); 335428d0a740SAndrew Gallatin TASK_INIT(&tls->disable_ifnet_task, 0, ktls_disable_ifnet_help, tls); 335528d0a740SAndrew Gallatin (void)taskqueue_enqueue(taskqueue_thread, &tls->disable_ifnet_task); 335628d0a740SAndrew Gallatin } 33574150a5a8SAndrew Gallatin #endif 3358