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" 33ed5e13cfSAndrew Gallatin #include "opt_ratelimit.h" 34b2e60773SJohn Baldwin #include "opt_rss.h" 35b2e60773SJohn Baldwin 36b2e60773SJohn Baldwin #include <sys/param.h> 37b2e60773SJohn Baldwin #include <sys/kernel.h> 3802bc3865SAndrew Gallatin #include <sys/domainset.h> 39b2e60773SJohn Baldwin #include <sys/ktls.h> 40b2e60773SJohn Baldwin #include <sys/lock.h> 41b2e60773SJohn Baldwin #include <sys/mbuf.h> 42b2e60773SJohn Baldwin #include <sys/mutex.h> 43b2e60773SJohn Baldwin #include <sys/rmlock.h> 44b2e60773SJohn Baldwin #include <sys/proc.h> 45b2e60773SJohn Baldwin #include <sys/protosw.h> 46b2e60773SJohn Baldwin #include <sys/refcount.h> 47b2e60773SJohn Baldwin #include <sys/smp.h> 48b2e60773SJohn Baldwin #include <sys/socket.h> 49b2e60773SJohn Baldwin #include <sys/socketvar.h> 50b2e60773SJohn Baldwin #include <sys/sysctl.h> 51b2e60773SJohn Baldwin #include <sys/taskqueue.h> 52b2e60773SJohn Baldwin #include <sys/kthread.h> 53b2e60773SJohn Baldwin #include <sys/uio.h> 54b2e60773SJohn Baldwin #include <sys/vmmeter.h> 55b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 56b2e60773SJohn Baldwin #include <machine/pcb.h> 57b2e60773SJohn Baldwin #endif 58b2e60773SJohn Baldwin #include <machine/vmparam.h> 5990746943SGleb Smirnoff #include <net/if.h> 6090746943SGleb Smirnoff #include <net/if_var.h> 61b2e60773SJohn Baldwin #ifdef RSS 62b2e60773SJohn Baldwin #include <net/netisr.h> 63b2e60773SJohn Baldwin #include <net/rss_config.h> 64b2e60773SJohn Baldwin #endif 65454d3896SAlexander V. Chernikov #include <net/route.h> 66454d3896SAlexander V. Chernikov #include <net/route/nhop.h> 67b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 68b2e60773SJohn Baldwin #include <netinet/in.h> 69b2e60773SJohn Baldwin #include <netinet/in_pcb.h> 70b2e60773SJohn Baldwin #endif 71b2e60773SJohn Baldwin #include <netinet/tcp_var.h> 729e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 739e14430dSJohn Baldwin #include <netinet/tcp_offload.h> 749e14430dSJohn Baldwin #endif 75b2e60773SJohn Baldwin #include <opencrypto/xform.h> 76b2e60773SJohn Baldwin #include <vm/uma_dbg.h> 77b2e60773SJohn Baldwin #include <vm/vm.h> 78b2e60773SJohn Baldwin #include <vm/vm_pageout.h> 79b2e60773SJohn Baldwin #include <vm/vm_page.h> 80b2e60773SJohn Baldwin 81b2e60773SJohn Baldwin struct ktls_wq { 82b2e60773SJohn Baldwin struct mtx mtx; 833c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) m_head; 843c0e5685SJohn Baldwin STAILQ_HEAD(, socket) so_head; 85b2e60773SJohn Baldwin bool running; 8649f6925cSMark Johnston int lastallocfail; 87b2e60773SJohn Baldwin } __aligned(CACHE_LINE_SIZE); 88b2e60773SJohn Baldwin 8902bc3865SAndrew Gallatin struct ktls_domain_info { 9002bc3865SAndrew Gallatin int count; 9102bc3865SAndrew Gallatin int cpu[MAXCPU]; 9202bc3865SAndrew Gallatin }; 9302bc3865SAndrew Gallatin 9402bc3865SAndrew Gallatin struct ktls_domain_info ktls_domains[MAXMEMDOM]; 95b2e60773SJohn Baldwin static struct ktls_wq *ktls_wq; 96b2e60773SJohn Baldwin static struct proc *ktls_proc; 97b2e60773SJohn Baldwin static uma_zone_t ktls_session_zone; 9849f6925cSMark Johnston static uma_zone_t ktls_buffer_zone; 99b2e60773SJohn Baldwin static uint16_t ktls_cpuid_lookup[MAXCPU]; 100b2e60773SJohn Baldwin 1017029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 102b2e60773SJohn Baldwin "Kernel TLS offload"); 1037029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 104b2e60773SJohn Baldwin "Kernel TLS offload stats"); 105b2e60773SJohn Baldwin 106b2e60773SJohn Baldwin #ifdef RSS 107b2e60773SJohn Baldwin static int ktls_bind_threads = 1; 108b2e60773SJohn Baldwin #else 109b2e60773SJohn Baldwin static int ktls_bind_threads; 110b2e60773SJohn Baldwin #endif 111b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN, 112b2e60773SJohn Baldwin &ktls_bind_threads, 0, 1134dc1b17dSMark Johnston "Bind crypto threads to cores (1) or cores and domains (2) at boot"); 114b2e60773SJohn Baldwin 115b2e60773SJohn Baldwin static u_int ktls_maxlen = 16384; 11649f6925cSMark Johnston SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RDTUN, 117b2e60773SJohn Baldwin &ktls_maxlen, 0, "Maximum TLS record size"); 118b2e60773SJohn Baldwin 119b2e60773SJohn Baldwin static int ktls_number_threads; 120b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD, 121b2e60773SJohn Baldwin &ktls_number_threads, 0, 122b2e60773SJohn Baldwin "Number of TLS threads in thread-pool"); 123b2e60773SJohn Baldwin 124b2e60773SJohn Baldwin static bool ktls_offload_enable; 125b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RWTUN, 126b2e60773SJohn Baldwin &ktls_offload_enable, 0, 127b2e60773SJohn Baldwin "Enable support for kernel TLS offload"); 128b2e60773SJohn Baldwin 129b2e60773SJohn Baldwin static bool ktls_cbc_enable = true; 130b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RWTUN, 131b2e60773SJohn Baldwin &ktls_cbc_enable, 1, 132b2e60773SJohn Baldwin "Enable Support of AES-CBC crypto for kernel TLS"); 133b2e60773SJohn Baldwin 13449f6925cSMark Johnston static bool ktls_sw_buffer_cache = true; 13549f6925cSMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, sw_buffer_cache, CTLFLAG_RDTUN, 13649f6925cSMark Johnston &ktls_sw_buffer_cache, 1, 13749f6925cSMark Johnston "Enable caching of output buffers for SW encryption"); 13849f6925cSMark Johnston 1391755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_tasks_active); 140b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD, 141b2e60773SJohn Baldwin &ktls_tasks_active, "Number of active tasks"); 142b2e60773SJohn Baldwin 1431755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_queued); 1443c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_inqueue, CTLFLAG_RD, 1453c0e5685SJohn Baldwin &ktls_cnt_tx_queued, 1463c0e5685SJohn Baldwin "Number of TLS records in queue to tasks for SW encryption"); 1473c0e5685SJohn Baldwin 1481755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_rx_queued); 1493c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_rx_inqueue, CTLFLAG_RD, 1503c0e5685SJohn Baldwin &ktls_cnt_rx_queued, 1513c0e5685SJohn Baldwin "Number of TLS sockets in queue to tasks for SW decryption"); 152b2e60773SJohn Baldwin 1531755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_total); 154b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total, 155b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_total, 156b2e60773SJohn Baldwin "Total successful TLS setups (parameters set)"); 157b2e60773SJohn Baldwin 1581755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_enable_calls); 159b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls, 160b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_enable_calls, 161b2e60773SJohn Baldwin "Total number of TLS enable calls made"); 162b2e60773SJohn Baldwin 1631755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_active); 164b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD, 165b2e60773SJohn Baldwin &ktls_offload_active, "Total Active TLS sessions"); 166b2e60773SJohn Baldwin 1671755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_corrupted_records); 1683c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, corrupted_records, CTLFLAG_RD, 1693c0e5685SJohn Baldwin &ktls_offload_corrupted_records, "Total corrupted TLS records received"); 1703c0e5685SJohn Baldwin 1711755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_failed_crypto); 172b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD, 173b2e60773SJohn Baldwin &ktls_offload_failed_crypto, "Total TLS crypto failures"); 174b2e60773SJohn Baldwin 1751755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_ifnet); 176b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD, 177b2e60773SJohn Baldwin &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet"); 178b2e60773SJohn Baldwin 1791755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_sw); 180b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD, 181b2e60773SJohn Baldwin &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW"); 182b2e60773SJohn Baldwin 1831755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_failed); 184b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD, 185b2e60773SJohn Baldwin &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet"); 186b2e60773SJohn Baldwin 1877029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 188b2e60773SJohn Baldwin "Software TLS session stats"); 1897029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 190b2e60773SJohn Baldwin "Hardware (ifnet) TLS session stats"); 1919e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 1927029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1939e14430dSJohn Baldwin "TOE TLS session stats"); 1949e14430dSJohn Baldwin #endif 195b2e60773SJohn Baldwin 1961755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc); 197b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc, 198b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-CBC"); 199b2e60773SJohn Baldwin 2001755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm); 201b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm, 202b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-GCM"); 203b2e60773SJohn Baldwin 2049c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20); 2059c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD, 2069c64fc40SJohn Baldwin &ktls_sw_chacha20, 2079c64fc40SJohn Baldwin "Active number of software TLS sessions using Chacha20-Poly1305"); 2089c64fc40SJohn Baldwin 2091755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc); 210b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD, 211b2e60773SJohn Baldwin &ktls_ifnet_cbc, 212b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-CBC"); 213b2e60773SJohn Baldwin 2141755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm); 215b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD, 216b2e60773SJohn Baldwin &ktls_ifnet_gcm, 217b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-GCM"); 218b2e60773SJohn Baldwin 2199c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20); 2209c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD, 2219c64fc40SJohn Baldwin &ktls_ifnet_chacha20, 2229c64fc40SJohn Baldwin "Active number of ifnet TLS sessions using Chacha20-Poly1305"); 2239c64fc40SJohn Baldwin 2241755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset); 225b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD, 226b2e60773SJohn Baldwin &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag"); 227b2e60773SJohn Baldwin 2281755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped); 229b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD, 230b2e60773SJohn Baldwin &ktls_ifnet_reset_dropped, 231b2e60773SJohn Baldwin "TLS sessions dropped after failing to update ifnet send tag"); 232b2e60773SJohn Baldwin 2331755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed); 234b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD, 235b2e60773SJohn Baldwin &ktls_ifnet_reset_failed, 236b2e60773SJohn Baldwin "TLS sessions that failed to allocate a new ifnet send tag"); 237b2e60773SJohn Baldwin 238b2e60773SJohn Baldwin static int ktls_ifnet_permitted; 239b2e60773SJohn Baldwin SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN, 240b2e60773SJohn Baldwin &ktls_ifnet_permitted, 1, 241b2e60773SJohn Baldwin "Whether to permit hardware (ifnet) TLS sessions"); 242b2e60773SJohn Baldwin 2439e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2441755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc); 2459e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD, 2469e14430dSJohn Baldwin &ktls_toe_cbc, 2479e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-CBC"); 2489e14430dSJohn Baldwin 2491755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm); 2509e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD, 2519e14430dSJohn Baldwin &ktls_toe_gcm, 2529e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-GCM"); 2539c64fc40SJohn Baldwin 25490972f04SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20); 2559c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD, 2569c64fc40SJohn Baldwin &ktls_toe_chacha20, 2579c64fc40SJohn Baldwin "Active number of TOE TLS sessions using Chacha20-Poly1305"); 2589e14430dSJohn Baldwin #endif 2599e14430dSJohn Baldwin 260b2e60773SJohn Baldwin static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS"); 261b2e60773SJohn Baldwin 262b2e60773SJohn Baldwin static void ktls_cleanup(struct ktls_session *tls); 263b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 264b2e60773SJohn Baldwin static void ktls_reset_send_tag(void *context, int pending); 265b2e60773SJohn Baldwin #endif 266b2e60773SJohn Baldwin static void ktls_work_thread(void *ctx); 267b2e60773SJohn Baldwin 268b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 269a2fba2a7SBjoern A. Zeeb static u_int 270b2e60773SJohn Baldwin ktls_get_cpu(struct socket *so) 271b2e60773SJohn Baldwin { 272b2e60773SJohn Baldwin struct inpcb *inp; 27302bc3865SAndrew Gallatin #ifdef NUMA 27402bc3865SAndrew Gallatin struct ktls_domain_info *di; 27502bc3865SAndrew Gallatin #endif 276a2fba2a7SBjoern A. Zeeb u_int cpuid; 277b2e60773SJohn Baldwin 278b2e60773SJohn Baldwin inp = sotoinpcb(so); 279b2e60773SJohn Baldwin #ifdef RSS 280b2e60773SJohn Baldwin cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype); 281b2e60773SJohn Baldwin if (cpuid != NETISR_CPUID_NONE) 282b2e60773SJohn Baldwin return (cpuid); 283b2e60773SJohn Baldwin #endif 284b2e60773SJohn Baldwin /* 285b2e60773SJohn Baldwin * Just use the flowid to shard connections in a repeatable 28621e3c1fbSJohn Baldwin * fashion. Note that TLS 1.0 sessions rely on the 287b2e60773SJohn Baldwin * serialization provided by having the same connection use 288b2e60773SJohn Baldwin * the same queue. 289b2e60773SJohn Baldwin */ 29002bc3865SAndrew Gallatin #ifdef NUMA 29102bc3865SAndrew Gallatin if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) { 29202bc3865SAndrew Gallatin di = &ktls_domains[inp->inp_numa_domain]; 29302bc3865SAndrew Gallatin cpuid = di->cpu[inp->inp_flowid % di->count]; 29402bc3865SAndrew Gallatin } else 29502bc3865SAndrew Gallatin #endif 296b2e60773SJohn Baldwin cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads]; 297b2e60773SJohn Baldwin return (cpuid); 298b2e60773SJohn Baldwin } 299b2e60773SJohn Baldwin #endif 300b2e60773SJohn Baldwin 30149f6925cSMark Johnston static int 30249f6925cSMark Johnston ktls_buffer_import(void *arg, void **store, int count, int domain, int flags) 30349f6925cSMark Johnston { 30449f6925cSMark Johnston vm_page_t m; 30549f6925cSMark Johnston int i; 30649f6925cSMark Johnston 30749f6925cSMark Johnston KASSERT((ktls_maxlen & PAGE_MASK) == 0, 30849f6925cSMark Johnston ("%s: ktls max length %d is not page size-aligned", 30949f6925cSMark Johnston __func__, ktls_maxlen)); 31049f6925cSMark Johnston 31149f6925cSMark Johnston for (i = 0; i < count; i++) { 31249f6925cSMark Johnston m = vm_page_alloc_contig_domain(NULL, 0, domain, 31349f6925cSMark Johnston VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 31449f6925cSMark Johnston VM_ALLOC_NODUMP | malloc2vm_flags(flags), 31549f6925cSMark Johnston atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0, 31649f6925cSMark Johnston VM_MEMATTR_DEFAULT); 31749f6925cSMark Johnston if (m == NULL) 31849f6925cSMark Johnston break; 31949f6925cSMark Johnston store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 32049f6925cSMark Johnston } 32149f6925cSMark Johnston return (i); 32249f6925cSMark Johnston } 32349f6925cSMark Johnston 32449f6925cSMark Johnston static void 32549f6925cSMark Johnston ktls_buffer_release(void *arg __unused, void **store, int count) 32649f6925cSMark Johnston { 32749f6925cSMark Johnston vm_page_t m; 32849f6925cSMark Johnston int i, j; 32949f6925cSMark Johnston 33049f6925cSMark Johnston for (i = 0; i < count; i++) { 33149f6925cSMark Johnston m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i])); 33249f6925cSMark Johnston for (j = 0; j < atop(ktls_maxlen); j++) { 33349f6925cSMark Johnston (void)vm_page_unwire_noq(m + j); 33449f6925cSMark Johnston vm_page_free(m + j); 33549f6925cSMark Johnston } 33649f6925cSMark Johnston } 33749f6925cSMark Johnston } 33849f6925cSMark Johnston 33949f6925cSMark Johnston static void 34049f6925cSMark Johnston ktls_free_mext_contig(struct mbuf *m) 34149f6925cSMark Johnston { 34249f6925cSMark Johnston M_ASSERTEXTPG(m); 34349f6925cSMark Johnston uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0])); 34449f6925cSMark Johnston } 34549f6925cSMark Johnston 346b2e60773SJohn Baldwin static void 347b2e60773SJohn Baldwin ktls_init(void *dummy __unused) 348b2e60773SJohn Baldwin { 349b2e60773SJohn Baldwin struct thread *td; 350b2e60773SJohn Baldwin struct pcpu *pc; 351b2e60773SJohn Baldwin cpuset_t mask; 35202bc3865SAndrew Gallatin int count, domain, error, i; 353b2e60773SJohn Baldwin 354b2e60773SJohn Baldwin ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS, 355b2e60773SJohn Baldwin M_WAITOK | M_ZERO); 356b2e60773SJohn Baldwin 357b2e60773SJohn Baldwin ktls_session_zone = uma_zcreate("ktls_session", 358b2e60773SJohn Baldwin sizeof(struct ktls_session), 359b2e60773SJohn Baldwin NULL, NULL, NULL, NULL, 360b2e60773SJohn Baldwin UMA_ALIGN_CACHE, 0); 361b2e60773SJohn Baldwin 36249f6925cSMark Johnston if (ktls_sw_buffer_cache) { 36349f6925cSMark Johnston ktls_buffer_zone = uma_zcache_create("ktls_buffers", 36449f6925cSMark Johnston roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL, 36549f6925cSMark Johnston ktls_buffer_import, ktls_buffer_release, NULL, 36649f6925cSMark Johnston UMA_ZONE_FIRSTTOUCH); 36749f6925cSMark Johnston } 36849f6925cSMark Johnston 369b2e60773SJohn Baldwin /* 370b2e60773SJohn Baldwin * Initialize the workqueues to run the TLS work. We create a 371b2e60773SJohn Baldwin * work queue for each CPU. 372b2e60773SJohn Baldwin */ 373b2e60773SJohn Baldwin CPU_FOREACH(i) { 3743c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].m_head); 3753c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].so_head); 376b2e60773SJohn Baldwin mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF); 377b2e60773SJohn Baldwin error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i], 37861b8a4afSAndrew Gallatin &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i); 379b2e60773SJohn Baldwin if (error) 380b2e60773SJohn Baldwin panic("Can't add KTLS thread %d error %d", i, error); 381b2e60773SJohn Baldwin 382b2e60773SJohn Baldwin /* 383b2e60773SJohn Baldwin * Bind threads to cores. If ktls_bind_threads is > 384b2e60773SJohn Baldwin * 1, then we bind to the NUMA domain. 385b2e60773SJohn Baldwin */ 386b2e60773SJohn Baldwin if (ktls_bind_threads) { 387b2e60773SJohn Baldwin if (ktls_bind_threads > 1) { 388b2e60773SJohn Baldwin pc = pcpu_find(i); 38902bc3865SAndrew Gallatin domain = pc->pc_domain; 39002bc3865SAndrew Gallatin CPU_COPY(&cpuset_domain[domain], &mask); 39102bc3865SAndrew Gallatin count = ktls_domains[domain].count; 39202bc3865SAndrew Gallatin ktls_domains[domain].cpu[count] = i; 39302bc3865SAndrew Gallatin ktls_domains[domain].count++; 394b2e60773SJohn Baldwin } else { 395b2e60773SJohn Baldwin CPU_SETOF(i, &mask); 396b2e60773SJohn Baldwin } 397b2e60773SJohn Baldwin error = cpuset_setthread(td->td_tid, &mask); 398b2e60773SJohn Baldwin if (error) 399b2e60773SJohn Baldwin panic( 400b2e60773SJohn Baldwin "Unable to bind KTLS thread for CPU %d error %d", 401b2e60773SJohn Baldwin i, error); 402b2e60773SJohn Baldwin } 403b2e60773SJohn Baldwin ktls_cpuid_lookup[ktls_number_threads] = i; 404b2e60773SJohn Baldwin ktls_number_threads++; 405b2e60773SJohn Baldwin } 40602bc3865SAndrew Gallatin 40702bc3865SAndrew Gallatin /* 40802bc3865SAndrew Gallatin * If we somehow have an empty domain, fall back to choosing 40902bc3865SAndrew Gallatin * among all KTLS threads. 41002bc3865SAndrew Gallatin */ 4114dc1b17dSMark Johnston if (ktls_bind_threads > 1) { 41202bc3865SAndrew Gallatin for (i = 0; i < vm_ndomains; i++) { 41302bc3865SAndrew Gallatin if (ktls_domains[i].count == 0) { 4144dc1b17dSMark Johnston ktls_bind_threads = 1; 41502bc3865SAndrew Gallatin break; 41602bc3865SAndrew Gallatin } 41702bc3865SAndrew Gallatin } 4184dc1b17dSMark Johnston } 41902bc3865SAndrew Gallatin 42089b65087SMark Johnston if (bootverbose) 421b2e60773SJohn Baldwin printf("KTLS: Initialized %d threads\n", ktls_number_threads); 422b2e60773SJohn Baldwin } 423b2e60773SJohn Baldwin SYSINIT(ktls, SI_SUB_SMP + 1, SI_ORDER_ANY, ktls_init, NULL); 424b2e60773SJohn Baldwin 425b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 426b2e60773SJohn Baldwin static int 427b2e60773SJohn Baldwin ktls_create_session(struct socket *so, struct tls_enable *en, 428b2e60773SJohn Baldwin struct ktls_session **tlsp) 429b2e60773SJohn Baldwin { 430b2e60773SJohn Baldwin struct ktls_session *tls; 431b2e60773SJohn Baldwin int error; 432b2e60773SJohn Baldwin 4337d29eb9aSJohn Baldwin /* Only TLS 1.0 - 1.3 are supported. */ 434b2e60773SJohn Baldwin if (en->tls_vmajor != TLS_MAJOR_VER_ONE) 435b2e60773SJohn Baldwin return (EINVAL); 436b2e60773SJohn Baldwin if (en->tls_vminor < TLS_MINOR_VER_ZERO || 4376554362cSAndrew Gallatin en->tls_vminor > TLS_MINOR_VER_THREE) 438b2e60773SJohn Baldwin return (EINVAL); 439b2e60773SJohn Baldwin 440b2e60773SJohn Baldwin if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE) 441b2e60773SJohn Baldwin return (EINVAL); 442b2e60773SJohn Baldwin if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE) 443b2e60773SJohn Baldwin return (EINVAL); 4446554362cSAndrew Gallatin if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv)) 445b2e60773SJohn Baldwin return (EINVAL); 446b2e60773SJohn Baldwin 447b2e60773SJohn Baldwin /* All supported algorithms require a cipher key. */ 448b2e60773SJohn Baldwin if (en->cipher_key_len == 0) 449b2e60773SJohn Baldwin return (EINVAL); 450b2e60773SJohn Baldwin 451b2e60773SJohn Baldwin /* No flags are currently supported. */ 452b2e60773SJohn Baldwin if (en->flags != 0) 453b2e60773SJohn Baldwin return (EINVAL); 454b2e60773SJohn Baldwin 455b2e60773SJohn Baldwin /* Common checks for supported algorithms. */ 456b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 457b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 458b2e60773SJohn Baldwin /* 459b2e60773SJohn Baldwin * auth_algorithm isn't used, but permit GMAC values 460b2e60773SJohn Baldwin * for compatibility. 461b2e60773SJohn Baldwin */ 462b2e60773SJohn Baldwin switch (en->auth_algorithm) { 463b2e60773SJohn Baldwin case 0: 464c0341432SJohn Baldwin #ifdef COMPAT_FREEBSD12 465c0341432SJohn Baldwin /* XXX: Really 13.0-current COMPAT. */ 466b2e60773SJohn Baldwin case CRYPTO_AES_128_NIST_GMAC: 467b2e60773SJohn Baldwin case CRYPTO_AES_192_NIST_GMAC: 468b2e60773SJohn Baldwin case CRYPTO_AES_256_NIST_GMAC: 469c0341432SJohn Baldwin #endif 470b2e60773SJohn Baldwin break; 471b2e60773SJohn Baldwin default: 472b2e60773SJohn Baldwin return (EINVAL); 473b2e60773SJohn Baldwin } 474b2e60773SJohn Baldwin if (en->auth_key_len != 0) 475b2e60773SJohn Baldwin return (EINVAL); 4766554362cSAndrew Gallatin if ((en->tls_vminor == TLS_MINOR_VER_TWO && 4776554362cSAndrew Gallatin en->iv_len != TLS_AEAD_GCM_LEN) || 4786554362cSAndrew Gallatin (en->tls_vminor == TLS_MINOR_VER_THREE && 4796554362cSAndrew Gallatin en->iv_len != TLS_1_3_GCM_IV_LEN)) 480b2e60773SJohn Baldwin return (EINVAL); 481b2e60773SJohn Baldwin break; 482b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 483b2e60773SJohn Baldwin switch (en->auth_algorithm) { 484b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 485b2e60773SJohn Baldwin /* 486b2e60773SJohn Baldwin * TLS 1.0 requires an implicit IV. TLS 1.1+ 487b2e60773SJohn Baldwin * all use explicit IVs. 488b2e60773SJohn Baldwin */ 489b2e60773SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_ZERO) { 490b2e60773SJohn Baldwin if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN) 491b2e60773SJohn Baldwin return (EINVAL); 492b2e60773SJohn Baldwin break; 493b2e60773SJohn Baldwin } 494b2e60773SJohn Baldwin 495b2e60773SJohn Baldwin /* FALLTHROUGH */ 496b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 497b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 498b2e60773SJohn Baldwin /* Ignore any supplied IV. */ 499b2e60773SJohn Baldwin en->iv_len = 0; 500b2e60773SJohn Baldwin break; 501b2e60773SJohn Baldwin default: 502b2e60773SJohn Baldwin return (EINVAL); 503b2e60773SJohn Baldwin } 504b2e60773SJohn Baldwin if (en->auth_key_len == 0) 505b2e60773SJohn Baldwin return (EINVAL); 506b2e60773SJohn Baldwin break; 5079c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 5089c64fc40SJohn Baldwin if (en->auth_algorithm != 0 || en->auth_key_len != 0) 5099c64fc40SJohn Baldwin return (EINVAL); 5109c64fc40SJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO && 5119c64fc40SJohn Baldwin en->tls_vminor != TLS_MINOR_VER_THREE) 5129c64fc40SJohn Baldwin return (EINVAL); 5139c64fc40SJohn Baldwin if (en->iv_len != TLS_CHACHA20_IV_LEN) 5149c64fc40SJohn Baldwin return (EINVAL); 5159c64fc40SJohn Baldwin break; 516b2e60773SJohn Baldwin default: 517b2e60773SJohn Baldwin return (EINVAL); 518b2e60773SJohn Baldwin } 519b2e60773SJohn Baldwin 520b2e60773SJohn Baldwin tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 521b2e60773SJohn Baldwin 522b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 523b2e60773SJohn Baldwin 524b2e60773SJohn Baldwin refcount_init(&tls->refcount, 1); 525b2e60773SJohn Baldwin TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls); 526b2e60773SJohn Baldwin 527b2e60773SJohn Baldwin tls->wq_index = ktls_get_cpu(so); 528b2e60773SJohn Baldwin 529b2e60773SJohn Baldwin tls->params.cipher_algorithm = en->cipher_algorithm; 530b2e60773SJohn Baldwin tls->params.auth_algorithm = en->auth_algorithm; 531b2e60773SJohn Baldwin tls->params.tls_vmajor = en->tls_vmajor; 532b2e60773SJohn Baldwin tls->params.tls_vminor = en->tls_vminor; 533b2e60773SJohn Baldwin tls->params.flags = en->flags; 534b2e60773SJohn Baldwin tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen); 535b2e60773SJohn Baldwin 536b2e60773SJohn Baldwin /* Set the header and trailer lengths. */ 537b2e60773SJohn Baldwin tls->params.tls_hlen = sizeof(struct tls_record_layer); 538b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 539b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 5406554362cSAndrew Gallatin /* 5416554362cSAndrew Gallatin * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte 5426554362cSAndrew Gallatin * nonce. TLS 1.3 uses a 12 byte implicit IV. 5436554362cSAndrew Gallatin */ 5446554362cSAndrew Gallatin if (en->tls_vminor < TLS_MINOR_VER_THREE) 5456554362cSAndrew Gallatin tls->params.tls_hlen += sizeof(uint64_t); 546b2e60773SJohn Baldwin tls->params.tls_tlen = AES_GMAC_HASH_LEN; 547b2e60773SJohn Baldwin tls->params.tls_bs = 1; 548b2e60773SJohn Baldwin break; 549b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 550b2e60773SJohn Baldwin switch (en->auth_algorithm) { 551b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 552b2e60773SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_ZERO) { 553b2e60773SJohn Baldwin /* Implicit IV, no nonce. */ 554b2e60773SJohn Baldwin } else { 555b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 556b2e60773SJohn Baldwin } 557b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 558b2e60773SJohn Baldwin SHA1_HASH_LEN; 559b2e60773SJohn Baldwin break; 560b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 561b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 562b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 563b2e60773SJohn Baldwin SHA2_256_HASH_LEN; 564b2e60773SJohn Baldwin break; 565b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 566b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 567b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 568b2e60773SJohn Baldwin SHA2_384_HASH_LEN; 569b2e60773SJohn Baldwin break; 570b2e60773SJohn Baldwin default: 571b2e60773SJohn Baldwin panic("invalid hmac"); 572b2e60773SJohn Baldwin } 573b2e60773SJohn Baldwin tls->params.tls_bs = AES_BLOCK_LEN; 574b2e60773SJohn Baldwin break; 5759c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 5769c64fc40SJohn Baldwin /* 5779c64fc40SJohn Baldwin * Chacha20 uses a 12 byte implicit IV. 5789c64fc40SJohn Baldwin */ 5799c64fc40SJohn Baldwin tls->params.tls_tlen = POLY1305_HASH_LEN; 5809c64fc40SJohn Baldwin tls->params.tls_bs = 1; 5819c64fc40SJohn Baldwin break; 582b2e60773SJohn Baldwin default: 583b2e60773SJohn Baldwin panic("invalid cipher"); 584b2e60773SJohn Baldwin } 585b2e60773SJohn Baldwin 5869c64fc40SJohn Baldwin /* 5879c64fc40SJohn Baldwin * TLS 1.3 includes optional padding which we do not support, 5889c64fc40SJohn Baldwin * and also puts the "real" record type at the end of the 5899c64fc40SJohn Baldwin * encrypted data. 5909c64fc40SJohn Baldwin */ 5919c64fc40SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_THREE) 5929c64fc40SJohn Baldwin tls->params.tls_tlen += sizeof(uint8_t); 5939c64fc40SJohn Baldwin 594b2e60773SJohn Baldwin KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN, 595b2e60773SJohn Baldwin ("TLS header length too long: %d", tls->params.tls_hlen)); 596b2e60773SJohn Baldwin KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN, 597b2e60773SJohn Baldwin ("TLS trailer length too long: %d", tls->params.tls_tlen)); 598b2e60773SJohn Baldwin 599b2e60773SJohn Baldwin if (en->auth_key_len != 0) { 600b2e60773SJohn Baldwin tls->params.auth_key_len = en->auth_key_len; 601b2e60773SJohn Baldwin tls->params.auth_key = malloc(en->auth_key_len, M_KTLS, 602b2e60773SJohn Baldwin M_WAITOK); 603b2e60773SJohn Baldwin error = copyin(en->auth_key, tls->params.auth_key, 604b2e60773SJohn Baldwin en->auth_key_len); 605b2e60773SJohn Baldwin if (error) 606b2e60773SJohn Baldwin goto out; 607b2e60773SJohn Baldwin } 608b2e60773SJohn Baldwin 609b2e60773SJohn Baldwin tls->params.cipher_key_len = en->cipher_key_len; 610b2e60773SJohn Baldwin tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK); 611b2e60773SJohn Baldwin error = copyin(en->cipher_key, tls->params.cipher_key, 612b2e60773SJohn Baldwin en->cipher_key_len); 613b2e60773SJohn Baldwin if (error) 614b2e60773SJohn Baldwin goto out; 615b2e60773SJohn Baldwin 616b2e60773SJohn Baldwin /* 6179c64fc40SJohn Baldwin * This holds the implicit portion of the nonce for AEAD 6189c64fc40SJohn Baldwin * ciphers and the initial implicit IV for TLS 1.0. The 6199c64fc40SJohn Baldwin * explicit portions of the IV are generated in ktls_frame(). 620b2e60773SJohn Baldwin */ 621b2e60773SJohn Baldwin if (en->iv_len != 0) { 622b2e60773SJohn Baldwin tls->params.iv_len = en->iv_len; 623b2e60773SJohn Baldwin error = copyin(en->iv, tls->params.iv, en->iv_len); 624b2e60773SJohn Baldwin if (error) 625b2e60773SJohn Baldwin goto out; 6267d29eb9aSJohn Baldwin 6277d29eb9aSJohn Baldwin /* 6289c64fc40SJohn Baldwin * For TLS 1.2 with GCM, generate an 8-byte nonce as a 6299c64fc40SJohn Baldwin * counter to generate unique explicit IVs. 6307d29eb9aSJohn Baldwin * 6317d29eb9aSJohn Baldwin * Store this counter in the last 8 bytes of the IV 6327d29eb9aSJohn Baldwin * array so that it is 8-byte aligned. 6337d29eb9aSJohn Baldwin */ 6347d29eb9aSJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 6357d29eb9aSJohn Baldwin en->tls_vminor == TLS_MINOR_VER_TWO) 6367d29eb9aSJohn Baldwin arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0); 637b2e60773SJohn Baldwin } 638b2e60773SJohn Baldwin 639b2e60773SJohn Baldwin *tlsp = tls; 640b2e60773SJohn Baldwin return (0); 641b2e60773SJohn Baldwin 642b2e60773SJohn Baldwin out: 643b2e60773SJohn Baldwin ktls_cleanup(tls); 644b2e60773SJohn Baldwin return (error); 645b2e60773SJohn Baldwin } 646b2e60773SJohn Baldwin 647b2e60773SJohn Baldwin static struct ktls_session * 648b2e60773SJohn Baldwin ktls_clone_session(struct ktls_session *tls) 649b2e60773SJohn Baldwin { 650b2e60773SJohn Baldwin struct ktls_session *tls_new; 651b2e60773SJohn Baldwin 652b2e60773SJohn Baldwin tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 653b2e60773SJohn Baldwin 654b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 655b2e60773SJohn Baldwin 656b2e60773SJohn Baldwin refcount_init(&tls_new->refcount, 1); 657b2e60773SJohn Baldwin 658b2e60773SJohn Baldwin /* Copy fields from existing session. */ 659b2e60773SJohn Baldwin tls_new->params = tls->params; 660b2e60773SJohn Baldwin tls_new->wq_index = tls->wq_index; 661b2e60773SJohn Baldwin 662b2e60773SJohn Baldwin /* Deep copy keys. */ 663b2e60773SJohn Baldwin if (tls_new->params.auth_key != NULL) { 664b2e60773SJohn Baldwin tls_new->params.auth_key = malloc(tls->params.auth_key_len, 665b2e60773SJohn Baldwin M_KTLS, M_WAITOK); 666b2e60773SJohn Baldwin memcpy(tls_new->params.auth_key, tls->params.auth_key, 667b2e60773SJohn Baldwin tls->params.auth_key_len); 668b2e60773SJohn Baldwin } 669b2e60773SJohn Baldwin 670b2e60773SJohn Baldwin tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS, 671b2e60773SJohn Baldwin M_WAITOK); 672b2e60773SJohn Baldwin memcpy(tls_new->params.cipher_key, tls->params.cipher_key, 673b2e60773SJohn Baldwin tls->params.cipher_key_len); 674b2e60773SJohn Baldwin 675b2e60773SJohn Baldwin return (tls_new); 676b2e60773SJohn Baldwin } 677b2e60773SJohn Baldwin #endif 678b2e60773SJohn Baldwin 679b2e60773SJohn Baldwin static void 680b2e60773SJohn Baldwin ktls_cleanup(struct ktls_session *tls) 681b2e60773SJohn Baldwin { 682b2e60773SJohn Baldwin 683b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, -1); 6849e14430dSJohn Baldwin switch (tls->mode) { 6859e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 686b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 687b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 688b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, -1); 689b2e60773SJohn Baldwin break; 690b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 691b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, -1); 692b2e60773SJohn Baldwin break; 6939c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6949c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, -1); 6959c64fc40SJohn Baldwin break; 696b2e60773SJohn Baldwin } 69721e3c1fbSJohn Baldwin ktls_ocf_free(tls); 6989e14430dSJohn Baldwin break; 6999e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 700b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 701b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 702b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, -1); 703b2e60773SJohn Baldwin break; 704b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 705b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, -1); 706b2e60773SJohn Baldwin break; 7079c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 7089c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, -1); 7099c64fc40SJohn Baldwin break; 710b2e60773SJohn Baldwin } 7119675d889SAndrew Gallatin if (tls->snd_tag != NULL) 712b2e60773SJohn Baldwin m_snd_tag_rele(tls->snd_tag); 7139e14430dSJohn Baldwin break; 7149e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 7159e14430dSJohn Baldwin case TCP_TLS_MODE_TOE: 7169e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 7179e14430dSJohn Baldwin case CRYPTO_AES_CBC: 7189e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, -1); 7199e14430dSJohn Baldwin break; 7209e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 7219e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, -1); 7229e14430dSJohn Baldwin break; 7239c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 7249c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, -1); 7259c64fc40SJohn Baldwin break; 7269e14430dSJohn Baldwin } 7279e14430dSJohn Baldwin break; 7289e14430dSJohn Baldwin #endif 729b2e60773SJohn Baldwin } 730b2e60773SJohn Baldwin if (tls->params.auth_key != NULL) { 7314a711b8dSJohn Baldwin zfree(tls->params.auth_key, M_KTLS); 732b2e60773SJohn Baldwin tls->params.auth_key = NULL; 733b2e60773SJohn Baldwin tls->params.auth_key_len = 0; 734b2e60773SJohn Baldwin } 735b2e60773SJohn Baldwin if (tls->params.cipher_key != NULL) { 7364a711b8dSJohn Baldwin zfree(tls->params.cipher_key, M_KTLS); 737b2e60773SJohn Baldwin tls->params.cipher_key = NULL; 738b2e60773SJohn Baldwin tls->params.cipher_key_len = 0; 739b2e60773SJohn Baldwin } 740b2e60773SJohn Baldwin explicit_bzero(tls->params.iv, sizeof(tls->params.iv)); 741b2e60773SJohn Baldwin } 742b2e60773SJohn Baldwin 743b2e60773SJohn Baldwin #if defined(INET) || defined(INET6) 7449e14430dSJohn Baldwin 7459e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 7469e14430dSJohn Baldwin static int 747f1f93475SJohn Baldwin ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction) 7489e14430dSJohn Baldwin { 7499e14430dSJohn Baldwin struct inpcb *inp; 7509e14430dSJohn Baldwin struct tcpcb *tp; 7519e14430dSJohn Baldwin int error; 7529e14430dSJohn Baldwin 7539e14430dSJohn Baldwin inp = so->so_pcb; 7549e14430dSJohn Baldwin INP_WLOCK(inp); 7559e14430dSJohn Baldwin if (inp->inp_flags2 & INP_FREED) { 7569e14430dSJohn Baldwin INP_WUNLOCK(inp); 7579e14430dSJohn Baldwin return (ECONNRESET); 7589e14430dSJohn Baldwin } 7599e14430dSJohn Baldwin if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7609e14430dSJohn Baldwin INP_WUNLOCK(inp); 7619e14430dSJohn Baldwin return (ECONNRESET); 7629e14430dSJohn Baldwin } 7639e14430dSJohn Baldwin if (inp->inp_socket == NULL) { 7649e14430dSJohn Baldwin INP_WUNLOCK(inp); 7659e14430dSJohn Baldwin return (ECONNRESET); 7669e14430dSJohn Baldwin } 7679e14430dSJohn Baldwin tp = intotcpcb(inp); 7686bcf3c46SJohn Baldwin if (!(tp->t_flags & TF_TOE)) { 7699e14430dSJohn Baldwin INP_WUNLOCK(inp); 7709e14430dSJohn Baldwin return (EOPNOTSUPP); 7719e14430dSJohn Baldwin } 7729e14430dSJohn Baldwin 773f1f93475SJohn Baldwin error = tcp_offload_alloc_tls_session(tp, tls, direction); 7749e14430dSJohn Baldwin INP_WUNLOCK(inp); 7759e14430dSJohn Baldwin if (error == 0) { 7769e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_TOE; 7779e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 7789e14430dSJohn Baldwin case CRYPTO_AES_CBC: 7799e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, 1); 7809e14430dSJohn Baldwin break; 7819e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 7829e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, 1); 7839e14430dSJohn Baldwin break; 7849c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 7859c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, 1); 7869c64fc40SJohn Baldwin break; 7879e14430dSJohn Baldwin } 7889e14430dSJohn Baldwin } 7899e14430dSJohn Baldwin return (error); 7909e14430dSJohn Baldwin } 7919e14430dSJohn Baldwin #endif 7929e14430dSJohn Baldwin 793b2e60773SJohn Baldwin /* 794b2e60773SJohn Baldwin * Common code used when first enabling ifnet TLS on a connection or 795b2e60773SJohn Baldwin * when allocating a new ifnet TLS session due to a routing change. 796b2e60773SJohn Baldwin * This function allocates a new TLS send tag on whatever interface 797b2e60773SJohn Baldwin * the connection is currently routed over. 798b2e60773SJohn Baldwin */ 799b2e60773SJohn Baldwin static int 800b2e60773SJohn Baldwin ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force, 801b2e60773SJohn Baldwin struct m_snd_tag **mstp) 802b2e60773SJohn Baldwin { 803b2e60773SJohn Baldwin union if_snd_tag_alloc_params params; 804b2e60773SJohn Baldwin struct ifnet *ifp; 805983066f0SAlexander V. Chernikov struct nhop_object *nh; 806b2e60773SJohn Baldwin struct tcpcb *tp; 807b2e60773SJohn Baldwin int error; 808b2e60773SJohn Baldwin 809b2e60773SJohn Baldwin INP_RLOCK(inp); 810b2e60773SJohn Baldwin if (inp->inp_flags2 & INP_FREED) { 811b2e60773SJohn Baldwin INP_RUNLOCK(inp); 812b2e60773SJohn Baldwin return (ECONNRESET); 813b2e60773SJohn Baldwin } 814b2e60773SJohn Baldwin if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 815b2e60773SJohn Baldwin INP_RUNLOCK(inp); 816b2e60773SJohn Baldwin return (ECONNRESET); 817b2e60773SJohn Baldwin } 818b2e60773SJohn Baldwin if (inp->inp_socket == NULL) { 819b2e60773SJohn Baldwin INP_RUNLOCK(inp); 820b2e60773SJohn Baldwin return (ECONNRESET); 821b2e60773SJohn Baldwin } 822b2e60773SJohn Baldwin tp = intotcpcb(inp); 823b2e60773SJohn Baldwin 824b2e60773SJohn Baldwin /* 825b2e60773SJohn Baldwin * Check administrative controls on ifnet TLS to determine if 826b2e60773SJohn Baldwin * ifnet TLS should be denied. 827b2e60773SJohn Baldwin * 828b2e60773SJohn Baldwin * - Always permit 'force' requests. 829b2e60773SJohn Baldwin * - ktls_ifnet_permitted == 0: always deny. 830b2e60773SJohn Baldwin */ 831b2e60773SJohn Baldwin if (!force && ktls_ifnet_permitted == 0) { 832b2e60773SJohn Baldwin INP_RUNLOCK(inp); 833b2e60773SJohn Baldwin return (ENXIO); 834b2e60773SJohn Baldwin } 835b2e60773SJohn Baldwin 836b2e60773SJohn Baldwin /* 837b2e60773SJohn Baldwin * XXX: Use the cached route in the inpcb to find the 838b2e60773SJohn Baldwin * interface. This should perhaps instead use 839b2e60773SJohn Baldwin * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only 840b2e60773SJohn Baldwin * enabled after a connection has completed key negotiation in 841b2e60773SJohn Baldwin * userland, the cached route will be present in practice. 842b2e60773SJohn Baldwin */ 843983066f0SAlexander V. Chernikov nh = inp->inp_route.ro_nh; 844983066f0SAlexander V. Chernikov if (nh == NULL) { 845b2e60773SJohn Baldwin INP_RUNLOCK(inp); 846b2e60773SJohn Baldwin return (ENXIO); 847b2e60773SJohn Baldwin } 848983066f0SAlexander V. Chernikov ifp = nh->nh_ifp; 849b2e60773SJohn Baldwin if_ref(ifp); 850b2e60773SJohn Baldwin 851521eac97SJohn Baldwin /* 852521eac97SJohn Baldwin * Allocate a TLS + ratelimit tag if the connection has an 853521eac97SJohn Baldwin * existing pacing rate. 854521eac97SJohn Baldwin */ 855521eac97SJohn Baldwin if (tp->t_pacing_rate != -1 && 856521eac97SJohn Baldwin (ifp->if_capenable & IFCAP_TXTLS_RTLMT) != 0) { 857521eac97SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT; 858521eac97SJohn Baldwin params.tls_rate_limit.inp = inp; 859521eac97SJohn Baldwin params.tls_rate_limit.tls = tls; 860521eac97SJohn Baldwin params.tls_rate_limit.max_rate = tp->t_pacing_rate; 861521eac97SJohn Baldwin } else { 862b2e60773SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS; 863521eac97SJohn Baldwin params.tls.inp = inp; 864521eac97SJohn Baldwin params.tls.tls = tls; 865521eac97SJohn Baldwin } 866b2e60773SJohn Baldwin params.hdr.flowid = inp->inp_flowid; 867b2e60773SJohn Baldwin params.hdr.flowtype = inp->inp_flowtype; 86898085baeSAndrew Gallatin params.hdr.numa_domain = inp->inp_numa_domain; 869b2e60773SJohn Baldwin INP_RUNLOCK(inp); 870b2e60773SJohn Baldwin 8713f43ada9SGleb Smirnoff if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { 872b2e60773SJohn Baldwin error = EOPNOTSUPP; 873b2e60773SJohn Baldwin goto out; 874b2e60773SJohn Baldwin } 875b2e60773SJohn Baldwin if (inp->inp_vflag & INP_IPV6) { 876b2e60773SJohn Baldwin if ((ifp->if_capenable & IFCAP_TXTLS6) == 0) { 877b2e60773SJohn Baldwin error = EOPNOTSUPP; 878b2e60773SJohn Baldwin goto out; 879b2e60773SJohn Baldwin } 880b2e60773SJohn Baldwin } else { 881b2e60773SJohn Baldwin if ((ifp->if_capenable & IFCAP_TXTLS4) == 0) { 882b2e60773SJohn Baldwin error = EOPNOTSUPP; 883b2e60773SJohn Baldwin goto out; 884b2e60773SJohn Baldwin } 885b2e60773SJohn Baldwin } 88636e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, mstp); 887b2e60773SJohn Baldwin out: 888b2e60773SJohn Baldwin if_rele(ifp); 889b2e60773SJohn Baldwin return (error); 890b2e60773SJohn Baldwin } 891b2e60773SJohn Baldwin 892b2e60773SJohn Baldwin static int 893b2e60773SJohn Baldwin ktls_try_ifnet(struct socket *so, struct ktls_session *tls, bool force) 894b2e60773SJohn Baldwin { 895b2e60773SJohn Baldwin struct m_snd_tag *mst; 896b2e60773SJohn Baldwin int error; 897b2e60773SJohn Baldwin 898b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst); 899b2e60773SJohn Baldwin if (error == 0) { 9009e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_IFNET; 901b2e60773SJohn Baldwin tls->snd_tag = mst; 902b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 903b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 904b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, 1); 905b2e60773SJohn Baldwin break; 906b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 907b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, 1); 908b2e60773SJohn Baldwin break; 9099c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 9109c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, 1); 9119c64fc40SJohn Baldwin break; 912b2e60773SJohn Baldwin } 913b2e60773SJohn Baldwin } 914b2e60773SJohn Baldwin return (error); 915b2e60773SJohn Baldwin } 916b2e60773SJohn Baldwin 917b2e60773SJohn Baldwin static int 9183c0e5685SJohn Baldwin ktls_try_sw(struct socket *so, struct ktls_session *tls, int direction) 919b2e60773SJohn Baldwin { 92021e3c1fbSJohn Baldwin int error; 921b2e60773SJohn Baldwin 92221e3c1fbSJohn Baldwin error = ktls_ocf_try(so, tls, direction); 92321e3c1fbSJohn Baldwin if (error) 92421e3c1fbSJohn Baldwin return (error); 9259e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_SW; 926b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 927b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 928b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, 1); 929b2e60773SJohn Baldwin break; 930b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 931b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, 1); 932b2e60773SJohn Baldwin break; 9339c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 9349c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, 1); 9359c64fc40SJohn Baldwin break; 936b2e60773SJohn Baldwin } 937b2e60773SJohn Baldwin return (0); 938b2e60773SJohn Baldwin } 939b2e60773SJohn Baldwin 9403c0e5685SJohn Baldwin /* 9413c0e5685SJohn Baldwin * KTLS RX stores data in the socket buffer as a list of TLS records, 9423c0e5685SJohn Baldwin * where each record is stored as a control message containg the TLS 9433c0e5685SJohn Baldwin * header followed by data mbufs containing the decrypted data. This 9443c0e5685SJohn Baldwin * is different from KTLS TX which always uses an mb_ext_pgs mbuf for 9453c0e5685SJohn Baldwin * both encrypted and decrypted data. TLS records decrypted by a NIC 9463c0e5685SJohn Baldwin * should be queued to the socket buffer as records, but encrypted 9473c0e5685SJohn Baldwin * data which needs to be decrypted by software arrives as a stream of 9483c0e5685SJohn Baldwin * regular mbufs which need to be converted. In addition, there may 9493c0e5685SJohn Baldwin * already be pending encrypted data in the socket buffer when KTLS RX 9503c0e5685SJohn Baldwin * is enabled. 9513c0e5685SJohn Baldwin * 9523c0e5685SJohn Baldwin * To manage not-yet-decrypted data for KTLS RX, the following scheme 9533c0e5685SJohn Baldwin * is used: 9543c0e5685SJohn Baldwin * 9553c0e5685SJohn Baldwin * - A single chain of NOTREADY mbufs is hung off of sb_mtls. 9563c0e5685SJohn Baldwin * 9573c0e5685SJohn Baldwin * - ktls_check_rx checks this chain of mbufs reading the TLS header 9583c0e5685SJohn Baldwin * from the first mbuf. Once all of the data for that TLS record is 9593c0e5685SJohn Baldwin * queued, the socket is queued to a worker thread. 9603c0e5685SJohn Baldwin * 9613c0e5685SJohn Baldwin * - The worker thread calls ktls_decrypt to decrypt TLS records in 9623c0e5685SJohn Baldwin * the TLS chain. Each TLS record is detached from the TLS chain, 9633c0e5685SJohn Baldwin * decrypted, and inserted into the regular socket buffer chain as 9643c0e5685SJohn Baldwin * record starting with a control message holding the TLS header and 9653c0e5685SJohn Baldwin * a chain of mbufs holding the encrypted data. 9663c0e5685SJohn Baldwin */ 9673c0e5685SJohn Baldwin 9683c0e5685SJohn Baldwin static void 9693c0e5685SJohn Baldwin sb_mark_notready(struct sockbuf *sb) 9703c0e5685SJohn Baldwin { 9713c0e5685SJohn Baldwin struct mbuf *m; 9723c0e5685SJohn Baldwin 9733c0e5685SJohn Baldwin m = sb->sb_mb; 9743c0e5685SJohn Baldwin sb->sb_mtls = m; 9753c0e5685SJohn Baldwin sb->sb_mb = NULL; 9763c0e5685SJohn Baldwin sb->sb_mbtail = NULL; 9773c0e5685SJohn Baldwin sb->sb_lastrecord = NULL; 9783c0e5685SJohn Baldwin for (; m != NULL; m = m->m_next) { 9793c0e5685SJohn Baldwin KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL", 9803c0e5685SJohn Baldwin __func__)); 9813c0e5685SJohn Baldwin KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail", 9823c0e5685SJohn Baldwin __func__)); 9833c0e5685SJohn Baldwin KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len", 9843c0e5685SJohn Baldwin __func__)); 9853c0e5685SJohn Baldwin m->m_flags |= M_NOTREADY; 9863c0e5685SJohn Baldwin sb->sb_acc -= m->m_len; 9873c0e5685SJohn Baldwin sb->sb_tlscc += m->m_len; 9883c0e5685SJohn Baldwin sb->sb_mtlstail = m; 9893c0e5685SJohn Baldwin } 9903c0e5685SJohn Baldwin KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc, 9913c0e5685SJohn Baldwin ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc, 9923c0e5685SJohn Baldwin sb->sb_ccc)); 9933c0e5685SJohn Baldwin } 9943c0e5685SJohn Baldwin 995b2e60773SJohn Baldwin int 996f1f93475SJohn Baldwin ktls_enable_rx(struct socket *so, struct tls_enable *en) 997f1f93475SJohn Baldwin { 998f1f93475SJohn Baldwin struct ktls_session *tls; 999f1f93475SJohn Baldwin int error; 1000f1f93475SJohn Baldwin 1001f1f93475SJohn Baldwin if (!ktls_offload_enable) 1002f1f93475SJohn Baldwin return (ENOTSUP); 10036685e259SMichael Tuexen if (SOLISTENING(so)) 10046685e259SMichael Tuexen return (EINVAL); 1005f1f93475SJohn Baldwin 1006f1f93475SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1007f1f93475SJohn Baldwin 1008f1f93475SJohn Baldwin /* 1009f1f93475SJohn Baldwin * This should always be true since only the TCP socket option 1010f1f93475SJohn Baldwin * invokes this function. 1011f1f93475SJohn Baldwin */ 1012f1f93475SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1013f1f93475SJohn Baldwin return (EINVAL); 1014f1f93475SJohn Baldwin 1015f1f93475SJohn Baldwin /* 1016f1f93475SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1017f1f93475SJohn Baldwin * this to support rekeying in the future. 1018f1f93475SJohn Baldwin */ 1019f1f93475SJohn Baldwin if (so->so_rcv.sb_tls_info != NULL) 1020f1f93475SJohn Baldwin return (EALREADY); 1021f1f93475SJohn Baldwin 1022f1f93475SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1023f1f93475SJohn Baldwin return (ENOTSUP); 1024f1f93475SJohn Baldwin 10253c0e5685SJohn Baldwin /* TLS 1.3 is not yet supported. */ 10263c0e5685SJohn Baldwin if (en->tls_vmajor == TLS_MAJOR_VER_ONE && 10273c0e5685SJohn Baldwin en->tls_vminor == TLS_MINOR_VER_THREE) 10283c0e5685SJohn Baldwin return (ENOTSUP); 10293c0e5685SJohn Baldwin 1030f1f93475SJohn Baldwin error = ktls_create_session(so, en, &tls); 1031f1f93475SJohn Baldwin if (error) 1032f1f93475SJohn Baldwin return (error); 1033f1f93475SJohn Baldwin 1034f1f93475SJohn Baldwin #ifdef TCP_OFFLOAD 1035f1f93475SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_RX); 10363c0e5685SJohn Baldwin if (error) 1037f1f93475SJohn Baldwin #endif 10383c0e5685SJohn Baldwin error = ktls_try_sw(so, tls, KTLS_RX); 1039f1f93475SJohn Baldwin 1040f1f93475SJohn Baldwin if (error) { 1041f1f93475SJohn Baldwin ktls_cleanup(tls); 1042f1f93475SJohn Baldwin return (error); 1043f1f93475SJohn Baldwin } 1044f1f93475SJohn Baldwin 1045f1f93475SJohn Baldwin /* Mark the socket as using TLS offload. */ 1046f1f93475SJohn Baldwin SOCKBUF_LOCK(&so->so_rcv); 10473c0e5685SJohn Baldwin so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq); 1048f1f93475SJohn Baldwin so->so_rcv.sb_tls_info = tls; 10493c0e5685SJohn Baldwin so->so_rcv.sb_flags |= SB_TLS_RX; 10503c0e5685SJohn Baldwin 10513c0e5685SJohn Baldwin /* Mark existing data as not ready until it can be decrypted. */ 1052faf0224fSJohn Baldwin if (tls->mode != TCP_TLS_MODE_TOE) { 10533c0e5685SJohn Baldwin sb_mark_notready(&so->so_rcv); 10543c0e5685SJohn Baldwin ktls_check_rx(&so->so_rcv); 1055faf0224fSJohn Baldwin } 1056f1f93475SJohn Baldwin SOCKBUF_UNLOCK(&so->so_rcv); 1057f1f93475SJohn Baldwin 1058f1f93475SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1059f1f93475SJohn Baldwin 1060f1f93475SJohn Baldwin return (0); 1061f1f93475SJohn Baldwin } 1062f1f93475SJohn Baldwin 1063f1f93475SJohn Baldwin int 1064b2e60773SJohn Baldwin ktls_enable_tx(struct socket *so, struct tls_enable *en) 1065b2e60773SJohn Baldwin { 1066b2e60773SJohn Baldwin struct ktls_session *tls; 1067521eac97SJohn Baldwin struct inpcb *inp; 1068b2e60773SJohn Baldwin int error; 1069b2e60773SJohn Baldwin 1070b2e60773SJohn Baldwin if (!ktls_offload_enable) 1071b2e60773SJohn Baldwin return (ENOTSUP); 10726685e259SMichael Tuexen if (SOLISTENING(so)) 10736685e259SMichael Tuexen return (EINVAL); 1074b2e60773SJohn Baldwin 1075b2e60773SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1076b2e60773SJohn Baldwin 1077b2e60773SJohn Baldwin /* 1078b2e60773SJohn Baldwin * This should always be true since only the TCP socket option 1079b2e60773SJohn Baldwin * invokes this function. 1080b2e60773SJohn Baldwin */ 1081b2e60773SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1082b2e60773SJohn Baldwin return (EINVAL); 1083b2e60773SJohn Baldwin 1084b2e60773SJohn Baldwin /* 1085b2e60773SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1086b2e60773SJohn Baldwin * this to support rekeying in the future. 1087b2e60773SJohn Baldwin */ 1088b2e60773SJohn Baldwin if (so->so_snd.sb_tls_info != NULL) 1089b2e60773SJohn Baldwin return (EALREADY); 1090b2e60773SJohn Baldwin 1091b2e60773SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1092b2e60773SJohn Baldwin return (ENOTSUP); 1093b2e60773SJohn Baldwin 1094b2e60773SJohn Baldwin /* TLS requires ext pgs */ 1095b2e60773SJohn Baldwin if (mb_use_ext_pgs == 0) 1096b2e60773SJohn Baldwin return (ENXIO); 1097b2e60773SJohn Baldwin 1098b2e60773SJohn Baldwin error = ktls_create_session(so, en, &tls); 1099b2e60773SJohn Baldwin if (error) 1100b2e60773SJohn Baldwin return (error); 1101b2e60773SJohn Baldwin 11029e14430dSJohn Baldwin /* Prefer TOE -> ifnet TLS -> software TLS. */ 11039e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 1104f1f93475SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_TX); 11059e14430dSJohn Baldwin if (error) 11069e14430dSJohn Baldwin #endif 1107b2e60773SJohn Baldwin error = ktls_try_ifnet(so, tls, false); 1108b2e60773SJohn Baldwin if (error) 11093c0e5685SJohn Baldwin error = ktls_try_sw(so, tls, KTLS_TX); 1110b2e60773SJohn Baldwin 1111b2e60773SJohn Baldwin if (error) { 1112b2e60773SJohn Baldwin ktls_cleanup(tls); 1113b2e60773SJohn Baldwin return (error); 1114b2e60773SJohn Baldwin } 1115b2e60773SJohn Baldwin 1116b2e60773SJohn Baldwin error = sblock(&so->so_snd, SBL_WAIT); 1117b2e60773SJohn Baldwin if (error) { 1118b2e60773SJohn Baldwin ktls_cleanup(tls); 1119b2e60773SJohn Baldwin return (error); 1120b2e60773SJohn Baldwin } 1121b2e60773SJohn Baldwin 1122521eac97SJohn Baldwin /* 1123521eac97SJohn Baldwin * Write lock the INP when setting sb_tls_info so that 1124521eac97SJohn Baldwin * routines in tcp_ratelimit.c can read sb_tls_info while 1125521eac97SJohn Baldwin * holding the INP lock. 1126521eac97SJohn Baldwin */ 1127521eac97SJohn Baldwin inp = so->so_pcb; 1128521eac97SJohn Baldwin INP_WLOCK(inp); 1129b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1130ec1db6e1SJohn Baldwin so->so_snd.sb_tls_seqno = be64dec(en->rec_seq); 1131b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls; 11329e14430dSJohn Baldwin if (tls->mode != TCP_TLS_MODE_SW) 1133b2e60773SJohn Baldwin so->so_snd.sb_flags |= SB_TLS_IFNET; 1134b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1135521eac97SJohn Baldwin INP_WUNLOCK(inp); 1136b2e60773SJohn Baldwin sbunlock(&so->so_snd); 1137b2e60773SJohn Baldwin 1138b2e60773SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1139b2e60773SJohn Baldwin 1140b2e60773SJohn Baldwin return (0); 1141b2e60773SJohn Baldwin } 1142b2e60773SJohn Baldwin 1143b2e60773SJohn Baldwin int 1144f1f93475SJohn Baldwin ktls_get_rx_mode(struct socket *so) 1145f1f93475SJohn Baldwin { 1146f1f93475SJohn Baldwin struct ktls_session *tls; 1147f1f93475SJohn Baldwin struct inpcb *inp; 1148f1f93475SJohn Baldwin int mode; 1149f1f93475SJohn Baldwin 11506685e259SMichael Tuexen if (SOLISTENING(so)) 11516685e259SMichael Tuexen return (EINVAL); 1152f1f93475SJohn Baldwin inp = so->so_pcb; 1153f1f93475SJohn Baldwin INP_WLOCK_ASSERT(inp); 1154f1f93475SJohn Baldwin SOCKBUF_LOCK(&so->so_rcv); 1155f1f93475SJohn Baldwin tls = so->so_rcv.sb_tls_info; 1156f1f93475SJohn Baldwin if (tls == NULL) 1157f1f93475SJohn Baldwin mode = TCP_TLS_MODE_NONE; 1158f1f93475SJohn Baldwin else 1159f1f93475SJohn Baldwin mode = tls->mode; 1160f1f93475SJohn Baldwin SOCKBUF_UNLOCK(&so->so_rcv); 1161f1f93475SJohn Baldwin return (mode); 1162f1f93475SJohn Baldwin } 1163f1f93475SJohn Baldwin 1164f1f93475SJohn Baldwin int 1165b2e60773SJohn Baldwin ktls_get_tx_mode(struct socket *so) 1166b2e60773SJohn Baldwin { 1167b2e60773SJohn Baldwin struct ktls_session *tls; 1168b2e60773SJohn Baldwin struct inpcb *inp; 1169b2e60773SJohn Baldwin int mode; 1170b2e60773SJohn Baldwin 11716685e259SMichael Tuexen if (SOLISTENING(so)) 11726685e259SMichael Tuexen return (EINVAL); 1173b2e60773SJohn Baldwin inp = so->so_pcb; 1174b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1175b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1176b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1177b2e60773SJohn Baldwin if (tls == NULL) 1178b2e60773SJohn Baldwin mode = TCP_TLS_MODE_NONE; 1179b2e60773SJohn Baldwin else 11809e14430dSJohn Baldwin mode = tls->mode; 1181b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1182b2e60773SJohn Baldwin return (mode); 1183b2e60773SJohn Baldwin } 1184b2e60773SJohn Baldwin 1185b2e60773SJohn Baldwin /* 1186b2e60773SJohn Baldwin * Switch between SW and ifnet TLS sessions as requested. 1187b2e60773SJohn Baldwin */ 1188b2e60773SJohn Baldwin int 1189b2e60773SJohn Baldwin ktls_set_tx_mode(struct socket *so, int mode) 1190b2e60773SJohn Baldwin { 1191b2e60773SJohn Baldwin struct ktls_session *tls, *tls_new; 1192b2e60773SJohn Baldwin struct inpcb *inp; 1193b2e60773SJohn Baldwin int error; 1194b2e60773SJohn Baldwin 11956685e259SMichael Tuexen if (SOLISTENING(so)) 11966685e259SMichael Tuexen return (EINVAL); 11979e14430dSJohn Baldwin switch (mode) { 11989e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 11999e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 12009e14430dSJohn Baldwin break; 12019e14430dSJohn Baldwin default: 12029e14430dSJohn Baldwin return (EINVAL); 12039e14430dSJohn Baldwin } 1204b2e60773SJohn Baldwin 1205b2e60773SJohn Baldwin inp = so->so_pcb; 1206b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1207b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1208b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1209b2e60773SJohn Baldwin if (tls == NULL) { 1210b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1211b2e60773SJohn Baldwin return (0); 1212b2e60773SJohn Baldwin } 1213b2e60773SJohn Baldwin 12149e14430dSJohn Baldwin if (tls->mode == mode) { 1215b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1216b2e60773SJohn Baldwin return (0); 1217b2e60773SJohn Baldwin } 1218b2e60773SJohn Baldwin 1219b2e60773SJohn Baldwin tls = ktls_hold(tls); 1220b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1221b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1222b2e60773SJohn Baldwin 1223b2e60773SJohn Baldwin tls_new = ktls_clone_session(tls); 1224b2e60773SJohn Baldwin 1225b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1226b2e60773SJohn Baldwin error = ktls_try_ifnet(so, tls_new, true); 1227b2e60773SJohn Baldwin else 12283c0e5685SJohn Baldwin error = ktls_try_sw(so, tls_new, KTLS_TX); 1229b2e60773SJohn Baldwin if (error) { 1230b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1231b2e60773SJohn Baldwin ktls_free(tls_new); 1232b2e60773SJohn Baldwin ktls_free(tls); 1233b2e60773SJohn Baldwin INP_WLOCK(inp); 1234b2e60773SJohn Baldwin return (error); 1235b2e60773SJohn Baldwin } 1236b2e60773SJohn Baldwin 1237b2e60773SJohn Baldwin error = sblock(&so->so_snd, SBL_WAIT); 1238b2e60773SJohn Baldwin if (error) { 1239b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1240b2e60773SJohn Baldwin ktls_free(tls_new); 1241b2e60773SJohn Baldwin ktls_free(tls); 1242b2e60773SJohn Baldwin INP_WLOCK(inp); 1243b2e60773SJohn Baldwin return (error); 1244b2e60773SJohn Baldwin } 1245b2e60773SJohn Baldwin 1246b2e60773SJohn Baldwin /* 1247b2e60773SJohn Baldwin * If we raced with another session change, keep the existing 1248b2e60773SJohn Baldwin * session. 1249b2e60773SJohn Baldwin */ 1250b2e60773SJohn Baldwin if (tls != so->so_snd.sb_tls_info) { 1251b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1252b2e60773SJohn Baldwin sbunlock(&so->so_snd); 1253b2e60773SJohn Baldwin ktls_free(tls_new); 1254b2e60773SJohn Baldwin ktls_free(tls); 1255b2e60773SJohn Baldwin INP_WLOCK(inp); 1256b2e60773SJohn Baldwin return (EBUSY); 1257b2e60773SJohn Baldwin } 1258b2e60773SJohn Baldwin 1259b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1260b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls_new; 12619e14430dSJohn Baldwin if (tls_new->mode != TCP_TLS_MODE_SW) 1262b2e60773SJohn Baldwin so->so_snd.sb_flags |= SB_TLS_IFNET; 1263b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1264b2e60773SJohn Baldwin sbunlock(&so->so_snd); 1265b2e60773SJohn Baldwin 1266b2e60773SJohn Baldwin /* 1267b2e60773SJohn Baldwin * Drop two references on 'tls'. The first is for the 1268b2e60773SJohn Baldwin * ktls_hold() above. The second drops the reference from the 1269b2e60773SJohn Baldwin * socket buffer. 1270b2e60773SJohn Baldwin */ 1271b2e60773SJohn Baldwin KASSERT(tls->refcount >= 2, ("too few references on old session")); 1272b2e60773SJohn Baldwin ktls_free(tls); 1273b2e60773SJohn Baldwin ktls_free(tls); 1274b2e60773SJohn Baldwin 1275b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1276b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_ifnet, 1); 1277b2e60773SJohn Baldwin else 1278b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_sw, 1); 1279b2e60773SJohn Baldwin 1280b2e60773SJohn Baldwin INP_WLOCK(inp); 1281b2e60773SJohn Baldwin return (0); 1282b2e60773SJohn Baldwin } 1283b2e60773SJohn Baldwin 1284b2e60773SJohn Baldwin /* 1285b2e60773SJohn Baldwin * Try to allocate a new TLS send tag. This task is scheduled when 1286b2e60773SJohn Baldwin * ip_output detects a route change while trying to transmit a packet 1287b2e60773SJohn Baldwin * holding a TLS record. If a new tag is allocated, replace the tag 1288b2e60773SJohn Baldwin * in the TLS session. Subsequent packets on the connection will use 1289b2e60773SJohn Baldwin * the new tag. If a new tag cannot be allocated, drop the 1290b2e60773SJohn Baldwin * connection. 1291b2e60773SJohn Baldwin */ 1292b2e60773SJohn Baldwin static void 1293b2e60773SJohn Baldwin ktls_reset_send_tag(void *context, int pending) 1294b2e60773SJohn Baldwin { 1295b2e60773SJohn Baldwin struct epoch_tracker et; 1296b2e60773SJohn Baldwin struct ktls_session *tls; 1297b2e60773SJohn Baldwin struct m_snd_tag *old, *new; 1298b2e60773SJohn Baldwin struct inpcb *inp; 1299b2e60773SJohn Baldwin struct tcpcb *tp; 1300b2e60773SJohn Baldwin int error; 1301b2e60773SJohn Baldwin 1302b2e60773SJohn Baldwin MPASS(pending == 1); 1303b2e60773SJohn Baldwin 1304b2e60773SJohn Baldwin tls = context; 1305b2e60773SJohn Baldwin inp = tls->inp; 1306b2e60773SJohn Baldwin 1307b2e60773SJohn Baldwin /* 1308b2e60773SJohn Baldwin * Free the old tag first before allocating a new one. 1309b2e60773SJohn Baldwin * ip[6]_output_send() will treat a NULL send tag the same as 1310b2e60773SJohn Baldwin * an ifp mismatch and drop packets until a new tag is 1311b2e60773SJohn Baldwin * allocated. 1312b2e60773SJohn Baldwin * 1313b2e60773SJohn Baldwin * Write-lock the INP when changing tls->snd_tag since 1314b2e60773SJohn Baldwin * ip[6]_output_send() holds a read-lock when reading the 1315b2e60773SJohn Baldwin * pointer. 1316b2e60773SJohn Baldwin */ 1317b2e60773SJohn Baldwin INP_WLOCK(inp); 1318b2e60773SJohn Baldwin old = tls->snd_tag; 1319b2e60773SJohn Baldwin tls->snd_tag = NULL; 1320b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1321b2e60773SJohn Baldwin if (old != NULL) 1322b2e60773SJohn Baldwin m_snd_tag_rele(old); 1323b2e60773SJohn Baldwin 1324b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(inp, tls, true, &new); 1325b2e60773SJohn Baldwin 1326b2e60773SJohn Baldwin if (error == 0) { 1327b2e60773SJohn Baldwin INP_WLOCK(inp); 1328b2e60773SJohn Baldwin tls->snd_tag = new; 1329b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1330b2e60773SJohn Baldwin tls->reset_pending = false; 1331b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1332b2e60773SJohn Baldwin if (!in_pcbrele_wlocked(inp)) 1333b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1334b2e60773SJohn Baldwin 1335b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset, 1); 1336b2e60773SJohn Baldwin 1337b2e60773SJohn Baldwin /* 1338b2e60773SJohn Baldwin * XXX: Should we kick tcp_output explicitly now that 1339b2e60773SJohn Baldwin * the send tag is fixed or just rely on timers? 1340b2e60773SJohn Baldwin */ 1341b2e60773SJohn Baldwin } else { 13421a496125SGleb Smirnoff NET_EPOCH_ENTER(et); 1343b2e60773SJohn Baldwin INP_WLOCK(inp); 1344b2e60773SJohn Baldwin if (!in_pcbrele_wlocked(inp)) { 1345b2e60773SJohn Baldwin if (!(inp->inp_flags & INP_TIMEWAIT) && 1346b2e60773SJohn Baldwin !(inp->inp_flags & INP_DROPPED)) { 1347b2e60773SJohn Baldwin tp = intotcpcb(inp); 13481f69a509SHans Petter Selasky CURVNET_SET(tp->t_vnet); 1349b2e60773SJohn Baldwin tp = tcp_drop(tp, ECONNABORTED); 13501f69a509SHans Petter Selasky CURVNET_RESTORE(); 1351b2e60773SJohn Baldwin if (tp != NULL) 1352b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1353b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_dropped, 1); 1354b2e60773SJohn Baldwin } else 1355b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1356b2e60773SJohn Baldwin } 13571a496125SGleb Smirnoff NET_EPOCH_EXIT(et); 1358b2e60773SJohn Baldwin 1359b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_failed, 1); 1360b2e60773SJohn Baldwin 1361b2e60773SJohn Baldwin /* 1362b2e60773SJohn Baldwin * Leave reset_pending true to avoid future tasks while 1363b2e60773SJohn Baldwin * the socket goes away. 1364b2e60773SJohn Baldwin */ 1365b2e60773SJohn Baldwin } 1366b2e60773SJohn Baldwin 1367b2e60773SJohn Baldwin ktls_free(tls); 1368b2e60773SJohn Baldwin } 1369b2e60773SJohn Baldwin 1370b2e60773SJohn Baldwin int 1371b2e60773SJohn Baldwin ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls) 1372b2e60773SJohn Baldwin { 1373b2e60773SJohn Baldwin 1374b2e60773SJohn Baldwin if (inp == NULL) 1375b2e60773SJohn Baldwin return (ENOBUFS); 1376b2e60773SJohn Baldwin 1377b2e60773SJohn Baldwin INP_LOCK_ASSERT(inp); 1378b2e60773SJohn Baldwin 1379b2e60773SJohn Baldwin /* 1380b2e60773SJohn Baldwin * See if we should schedule a task to update the send tag for 1381b2e60773SJohn Baldwin * this session. 1382b2e60773SJohn Baldwin */ 1383b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1384b2e60773SJohn Baldwin if (!tls->reset_pending) { 1385b2e60773SJohn Baldwin (void) ktls_hold(tls); 1386b2e60773SJohn Baldwin in_pcbref(inp); 1387b2e60773SJohn Baldwin tls->inp = inp; 1388b2e60773SJohn Baldwin tls->reset_pending = true; 1389b2e60773SJohn Baldwin taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1390b2e60773SJohn Baldwin } 1391b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1392b2e60773SJohn Baldwin return (ENOBUFS); 1393b2e60773SJohn Baldwin } 1394521eac97SJohn Baldwin 1395521eac97SJohn Baldwin #ifdef RATELIMIT 1396521eac97SJohn Baldwin int 1397521eac97SJohn Baldwin ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate) 1398521eac97SJohn Baldwin { 1399521eac97SJohn Baldwin union if_snd_tag_modify_params params = { 1400521eac97SJohn Baldwin .rate_limit.max_rate = max_pacing_rate, 1401521eac97SJohn Baldwin .rate_limit.flags = M_NOWAIT, 1402521eac97SJohn Baldwin }; 1403521eac97SJohn Baldwin struct m_snd_tag *mst; 1404521eac97SJohn Baldwin struct ifnet *ifp; 1405521eac97SJohn Baldwin 1406521eac97SJohn Baldwin /* Can't get to the inp, but it should be locked. */ 1407521eac97SJohn Baldwin /* INP_LOCK_ASSERT(inp); */ 1408521eac97SJohn Baldwin 1409521eac97SJohn Baldwin MPASS(tls->mode == TCP_TLS_MODE_IFNET); 1410521eac97SJohn Baldwin 1411521eac97SJohn Baldwin if (tls->snd_tag == NULL) { 1412521eac97SJohn Baldwin /* 1413521eac97SJohn Baldwin * Resetting send tag, ignore this change. The 1414521eac97SJohn Baldwin * pending reset may or may not see this updated rate 1415521eac97SJohn Baldwin * in the tcpcb. If it doesn't, we will just lose 1416521eac97SJohn Baldwin * this rate change. 1417521eac97SJohn Baldwin */ 1418521eac97SJohn Baldwin return (0); 1419521eac97SJohn Baldwin } 1420521eac97SJohn Baldwin 1421521eac97SJohn Baldwin MPASS(tls->snd_tag != NULL); 1422521eac97SJohn Baldwin MPASS(tls->snd_tag->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT); 1423521eac97SJohn Baldwin 1424521eac97SJohn Baldwin mst = tls->snd_tag; 1425521eac97SJohn Baldwin ifp = mst->ifp; 1426521eac97SJohn Baldwin return (ifp->if_snd_tag_modify(mst, ¶ms)); 1427521eac97SJohn Baldwin } 1428521eac97SJohn Baldwin #endif 1429b2e60773SJohn Baldwin #endif 1430b2e60773SJohn Baldwin 1431b2e60773SJohn Baldwin void 1432b2e60773SJohn Baldwin ktls_destroy(struct ktls_session *tls) 1433b2e60773SJohn Baldwin { 1434b2e60773SJohn Baldwin 1435b2e60773SJohn Baldwin ktls_cleanup(tls); 1436b2e60773SJohn Baldwin uma_zfree(ktls_session_zone, tls); 1437b2e60773SJohn Baldwin } 1438b2e60773SJohn Baldwin 1439b2e60773SJohn Baldwin void 1440b2e60773SJohn Baldwin ktls_seq(struct sockbuf *sb, struct mbuf *m) 1441b2e60773SJohn Baldwin { 1442b2e60773SJohn Baldwin 1443b2e60773SJohn Baldwin for (; m != NULL; m = m->m_next) { 14446edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 1445b2e60773SJohn Baldwin ("ktls_seq: mapped mbuf %p", m)); 1446b2e60773SJohn Baldwin 14477b6c99d0SGleb Smirnoff m->m_epg_seqno = sb->sb_tls_seqno; 1448b2e60773SJohn Baldwin sb->sb_tls_seqno++; 1449b2e60773SJohn Baldwin } 1450b2e60773SJohn Baldwin } 1451b2e60773SJohn Baldwin 1452b2e60773SJohn Baldwin /* 1453b2e60773SJohn Baldwin * Add TLS framing (headers and trailers) to a chain of mbufs. Each 1454b2e60773SJohn Baldwin * mbuf in the chain must be an unmapped mbuf. The payload of the 1455b2e60773SJohn Baldwin * mbuf must be populated with the payload of each TLS record. 1456b2e60773SJohn Baldwin * 1457b2e60773SJohn Baldwin * The record_type argument specifies the TLS record type used when 1458b2e60773SJohn Baldwin * populating the TLS header. 1459b2e60773SJohn Baldwin * 1460b2e60773SJohn Baldwin * The enq_count argument on return is set to the number of pages of 1461b2e60773SJohn Baldwin * payload data for this entire chain that need to be encrypted via SW 1462b2e60773SJohn Baldwin * encryption. The returned value should be passed to ktls_enqueue 1463c2a8fd6fSJohn Baldwin * when scheduling encryption of this chain of mbufs. To handle the 1464c2a8fd6fSJohn Baldwin * special case of empty fragments for TLS 1.0 sessions, an empty 1465c2a8fd6fSJohn Baldwin * fragment counts as one page. 1466b2e60773SJohn Baldwin */ 1467f85e1a80SGleb Smirnoff void 1468b2e60773SJohn Baldwin ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt, 1469b2e60773SJohn Baldwin uint8_t record_type) 1470b2e60773SJohn Baldwin { 1471b2e60773SJohn Baldwin struct tls_record_layer *tlshdr; 1472b2e60773SJohn Baldwin struct mbuf *m; 14737d29eb9aSJohn Baldwin uint64_t *noncep; 1474b2e60773SJohn Baldwin uint16_t tls_len; 1475b2e60773SJohn Baldwin int maxlen; 1476b2e60773SJohn Baldwin 1477b2e60773SJohn Baldwin maxlen = tls->params.max_frame_len; 1478b2e60773SJohn Baldwin *enq_cnt = 0; 1479b2e60773SJohn Baldwin for (m = top; m != NULL; m = m->m_next) { 1480b2e60773SJohn Baldwin /* 1481c2a8fd6fSJohn Baldwin * All mbufs in the chain should be TLS records whose 1482c2a8fd6fSJohn Baldwin * payload does not exceed the maximum frame length. 1483c2a8fd6fSJohn Baldwin * 1484c2a8fd6fSJohn Baldwin * Empty TLS records are permitted when using CBC. 1485b2e60773SJohn Baldwin */ 1486c2a8fd6fSJohn Baldwin KASSERT(m->m_len <= maxlen && 1487c2a8fd6fSJohn Baldwin (tls->params.cipher_algorithm == CRYPTO_AES_CBC ? 1488c2a8fd6fSJohn Baldwin m->m_len >= 0 : m->m_len > 0), 1489f85e1a80SGleb Smirnoff ("ktls_frame: m %p len %d\n", m, m->m_len)); 1490c2a8fd6fSJohn Baldwin 1491b2e60773SJohn Baldwin /* 1492b2e60773SJohn Baldwin * TLS frames require unmapped mbufs to store session 1493b2e60773SJohn Baldwin * info. 1494b2e60773SJohn Baldwin */ 14956edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 1496b2e60773SJohn Baldwin ("ktls_frame: mapped mbuf %p (top = %p)\n", m, top)); 1497b2e60773SJohn Baldwin 1498f85e1a80SGleb Smirnoff tls_len = m->m_len; 1499b2e60773SJohn Baldwin 1500b2e60773SJohn Baldwin /* Save a reference to the session. */ 15017b6c99d0SGleb Smirnoff m->m_epg_tls = ktls_hold(tls); 1502b2e60773SJohn Baldwin 15037b6c99d0SGleb Smirnoff m->m_epg_hdrlen = tls->params.tls_hlen; 15047b6c99d0SGleb Smirnoff m->m_epg_trllen = tls->params.tls_tlen; 1505b2e60773SJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) { 1506b2e60773SJohn Baldwin int bs, delta; 1507b2e60773SJohn Baldwin 1508b2e60773SJohn Baldwin /* 1509b2e60773SJohn Baldwin * AES-CBC pads messages to a multiple of the 1510b2e60773SJohn Baldwin * block size. Note that the padding is 1511b2e60773SJohn Baldwin * applied after the digest and the encryption 1512b2e60773SJohn Baldwin * is done on the "plaintext || mac || padding". 1513b2e60773SJohn Baldwin * At least one byte of padding is always 1514b2e60773SJohn Baldwin * present. 1515b2e60773SJohn Baldwin * 1516b2e60773SJohn Baldwin * Compute the final trailer length assuming 1517b2e60773SJohn Baldwin * at most one block of padding. 151821e3c1fbSJohn Baldwin * tls->params.tls_tlen is the maximum 1519b2e60773SJohn Baldwin * possible trailer length (padding + digest). 1520b2e60773SJohn Baldwin * delta holds the number of excess padding 1521b2e60773SJohn Baldwin * bytes if the maximum were used. Those 1522b2e60773SJohn Baldwin * extra bytes are removed. 1523b2e60773SJohn Baldwin */ 1524b2e60773SJohn Baldwin bs = tls->params.tls_bs; 1525b2e60773SJohn Baldwin delta = (tls_len + tls->params.tls_tlen) & (bs - 1); 15267b6c99d0SGleb Smirnoff m->m_epg_trllen -= delta; 1527b2e60773SJohn Baldwin } 15287b6c99d0SGleb Smirnoff m->m_len += m->m_epg_hdrlen + m->m_epg_trllen; 1529b2e60773SJohn Baldwin 1530b2e60773SJohn Baldwin /* Populate the TLS header. */ 15310c103266SGleb Smirnoff tlshdr = (void *)m->m_epg_hdr; 1532b2e60773SJohn Baldwin tlshdr->tls_vmajor = tls->params.tls_vmajor; 15336554362cSAndrew Gallatin 15346554362cSAndrew Gallatin /* 15356554362cSAndrew Gallatin * TLS 1.3 masquarades as TLS 1.2 with a record type 15366554362cSAndrew Gallatin * of TLS_RLTYPE_APP. 15376554362cSAndrew Gallatin */ 15386554362cSAndrew Gallatin if (tls->params.tls_vminor == TLS_MINOR_VER_THREE && 15396554362cSAndrew Gallatin tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) { 15406554362cSAndrew Gallatin tlshdr->tls_vminor = TLS_MINOR_VER_TWO; 15416554362cSAndrew Gallatin tlshdr->tls_type = TLS_RLTYPE_APP; 15426554362cSAndrew Gallatin /* save the real record type for later */ 15437b6c99d0SGleb Smirnoff m->m_epg_record_type = record_type; 15440c103266SGleb Smirnoff m->m_epg_trail[0] = record_type; 15456554362cSAndrew Gallatin } else { 1546b2e60773SJohn Baldwin tlshdr->tls_vminor = tls->params.tls_vminor; 1547b2e60773SJohn Baldwin tlshdr->tls_type = record_type; 15486554362cSAndrew Gallatin } 1549b2e60773SJohn Baldwin tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr)); 1550b2e60773SJohn Baldwin 1551b2e60773SJohn Baldwin /* 15527d29eb9aSJohn Baldwin * Store nonces / explicit IVs after the end of the 15537d29eb9aSJohn Baldwin * TLS header. 15547d29eb9aSJohn Baldwin * 15557d29eb9aSJohn Baldwin * For GCM with TLS 1.2, an 8 byte nonce is copied 15567d29eb9aSJohn Baldwin * from the end of the IV. The nonce is then 15577d29eb9aSJohn Baldwin * incremented for use by the next record. 15587d29eb9aSJohn Baldwin * 15597d29eb9aSJohn Baldwin * For CBC, a random nonce is inserted for TLS 1.1+. 1560b2e60773SJohn Baldwin */ 15617d29eb9aSJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 15627d29eb9aSJohn Baldwin tls->params.tls_vminor == TLS_MINOR_VER_TWO) { 15637d29eb9aSJohn Baldwin noncep = (uint64_t *)(tls->params.iv + 8); 15647d29eb9aSJohn Baldwin be64enc(tlshdr + 1, *noncep); 15657d29eb9aSJohn Baldwin (*noncep)++; 15667d29eb9aSJohn Baldwin } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 1567b2e60773SJohn Baldwin tls->params.tls_vminor >= TLS_MINOR_VER_ONE) 1568b2e60773SJohn Baldwin arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0); 1569b2e60773SJohn Baldwin 1570b2e60773SJohn Baldwin /* 1571b2e60773SJohn Baldwin * When using SW encryption, mark the mbuf not ready. 1572b2e60773SJohn Baldwin * It will be marked ready via sbready() after the 1573b2e60773SJohn Baldwin * record has been encrypted. 1574b2e60773SJohn Baldwin * 1575b2e60773SJohn Baldwin * When using ifnet TLS, unencrypted TLS records are 1576b2e60773SJohn Baldwin * sent down the stack to the NIC. 1577b2e60773SJohn Baldwin */ 15789e14430dSJohn Baldwin if (tls->mode == TCP_TLS_MODE_SW) { 1579b2e60773SJohn Baldwin m->m_flags |= M_NOTREADY; 15807b6c99d0SGleb Smirnoff m->m_epg_nrdy = m->m_epg_npgs; 1581c2a8fd6fSJohn Baldwin if (__predict_false(tls_len == 0)) { 1582c2a8fd6fSJohn Baldwin /* TLS 1.0 empty fragment. */ 1583c2a8fd6fSJohn Baldwin *enq_cnt += 1; 1584c2a8fd6fSJohn Baldwin } else 15857b6c99d0SGleb Smirnoff *enq_cnt += m->m_epg_npgs; 1586b2e60773SJohn Baldwin } 1587b2e60773SJohn Baldwin } 1588b2e60773SJohn Baldwin } 1589b2e60773SJohn Baldwin 1590b2e60773SJohn Baldwin void 15913c0e5685SJohn Baldwin ktls_check_rx(struct sockbuf *sb) 15923c0e5685SJohn Baldwin { 15933c0e5685SJohn Baldwin struct tls_record_layer hdr; 15943c0e5685SJohn Baldwin struct ktls_wq *wq; 15953c0e5685SJohn Baldwin struct socket *so; 15963c0e5685SJohn Baldwin bool running; 15973c0e5685SJohn Baldwin 15983c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 15993c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 16003c0e5685SJohn Baldwin __func__, sb)); 16013c0e5685SJohn Baldwin so = __containerof(sb, struct socket, so_rcv); 16023c0e5685SJohn Baldwin 16033c0e5685SJohn Baldwin if (sb->sb_flags & SB_TLS_RX_RUNNING) 16043c0e5685SJohn Baldwin return; 16053c0e5685SJohn Baldwin 16063c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 16073c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr)) { 16083c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0) 16093c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 16103c0e5685SJohn Baldwin return; 16113c0e5685SJohn Baldwin } 16123c0e5685SJohn Baldwin 16133c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr); 16143c0e5685SJohn Baldwin 16153c0e5685SJohn Baldwin /* Is the entire record queued? */ 16163c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) { 16173c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0) 16183c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 16193c0e5685SJohn Baldwin return; 16203c0e5685SJohn Baldwin } 16213c0e5685SJohn Baldwin 16223c0e5685SJohn Baldwin sb->sb_flags |= SB_TLS_RX_RUNNING; 16233c0e5685SJohn Baldwin 16243c0e5685SJohn Baldwin soref(so); 16253c0e5685SJohn Baldwin wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index]; 16263c0e5685SJohn Baldwin mtx_lock(&wq->mtx); 16273c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list); 16283c0e5685SJohn Baldwin running = wq->running; 16293c0e5685SJohn Baldwin mtx_unlock(&wq->mtx); 16303c0e5685SJohn Baldwin if (!running) 16313c0e5685SJohn Baldwin wakeup(wq); 16323c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, 1); 16333c0e5685SJohn Baldwin } 16343c0e5685SJohn Baldwin 16353c0e5685SJohn Baldwin static struct mbuf * 16363c0e5685SJohn Baldwin ktls_detach_record(struct sockbuf *sb, int len) 16373c0e5685SJohn Baldwin { 16383c0e5685SJohn Baldwin struct mbuf *m, *n, *top; 16393c0e5685SJohn Baldwin int remain; 16403c0e5685SJohn Baldwin 16413c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 16423c0e5685SJohn Baldwin MPASS(len <= sb->sb_tlscc); 16433c0e5685SJohn Baldwin 16443c0e5685SJohn Baldwin /* 16453c0e5685SJohn Baldwin * If TLS chain is the exact size of the record, 16463c0e5685SJohn Baldwin * just grab the whole record. 16473c0e5685SJohn Baldwin */ 16483c0e5685SJohn Baldwin top = sb->sb_mtls; 16493c0e5685SJohn Baldwin if (sb->sb_tlscc == len) { 16503c0e5685SJohn Baldwin sb->sb_mtls = NULL; 16513c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 16523c0e5685SJohn Baldwin goto out; 16533c0e5685SJohn Baldwin } 16543c0e5685SJohn Baldwin 16553c0e5685SJohn Baldwin /* 16563c0e5685SJohn Baldwin * While it would be nice to use m_split() here, we need 16573c0e5685SJohn Baldwin * to know exactly what m_split() allocates to update the 16583c0e5685SJohn Baldwin * accounting, so do it inline instead. 16593c0e5685SJohn Baldwin */ 16603c0e5685SJohn Baldwin remain = len; 16613c0e5685SJohn Baldwin for (m = top; remain > m->m_len; m = m->m_next) 16623c0e5685SJohn Baldwin remain -= m->m_len; 16633c0e5685SJohn Baldwin 16643c0e5685SJohn Baldwin /* Easy case: don't have to split 'm'. */ 16653c0e5685SJohn Baldwin if (remain == m->m_len) { 16663c0e5685SJohn Baldwin sb->sb_mtls = m->m_next; 16673c0e5685SJohn Baldwin if (sb->sb_mtls == NULL) 16683c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 16693c0e5685SJohn Baldwin m->m_next = NULL; 16703c0e5685SJohn Baldwin goto out; 16713c0e5685SJohn Baldwin } 16723c0e5685SJohn Baldwin 16733c0e5685SJohn Baldwin /* 16743c0e5685SJohn Baldwin * Need to allocate an mbuf to hold the remainder of 'm'. Try 16753c0e5685SJohn Baldwin * with M_NOWAIT first. 16763c0e5685SJohn Baldwin */ 16773c0e5685SJohn Baldwin n = m_get(M_NOWAIT, MT_DATA); 16783c0e5685SJohn Baldwin if (n == NULL) { 16793c0e5685SJohn Baldwin /* 16803c0e5685SJohn Baldwin * Use M_WAITOK with socket buffer unlocked. If 16813c0e5685SJohn Baldwin * 'sb_mtls' changes while the lock is dropped, return 16823c0e5685SJohn Baldwin * NULL to force the caller to retry. 16833c0e5685SJohn Baldwin */ 16843c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 16853c0e5685SJohn Baldwin 16863c0e5685SJohn Baldwin n = m_get(M_WAITOK, MT_DATA); 16873c0e5685SJohn Baldwin 16883c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 16893c0e5685SJohn Baldwin if (sb->sb_mtls != top) { 16903c0e5685SJohn Baldwin m_free(n); 16913c0e5685SJohn Baldwin return (NULL); 16923c0e5685SJohn Baldwin } 16933c0e5685SJohn Baldwin } 16943c0e5685SJohn Baldwin n->m_flags |= M_NOTREADY; 16953c0e5685SJohn Baldwin 16963c0e5685SJohn Baldwin /* Store remainder in 'n'. */ 16973c0e5685SJohn Baldwin n->m_len = m->m_len - remain; 16983c0e5685SJohn Baldwin if (m->m_flags & M_EXT) { 16993c0e5685SJohn Baldwin n->m_data = m->m_data + remain; 17003c0e5685SJohn Baldwin mb_dupcl(n, m); 17013c0e5685SJohn Baldwin } else { 17023c0e5685SJohn Baldwin bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len); 17033c0e5685SJohn Baldwin } 17043c0e5685SJohn Baldwin 17053c0e5685SJohn Baldwin /* Trim 'm' and update accounting. */ 17063c0e5685SJohn Baldwin m->m_len -= n->m_len; 17073c0e5685SJohn Baldwin sb->sb_tlscc -= n->m_len; 17083c0e5685SJohn Baldwin sb->sb_ccc -= n->m_len; 17093c0e5685SJohn Baldwin 17103c0e5685SJohn Baldwin /* Account for 'n'. */ 17113c0e5685SJohn Baldwin sballoc_ktls_rx(sb, n); 17123c0e5685SJohn Baldwin 17133c0e5685SJohn Baldwin /* Insert 'n' into the TLS chain. */ 17143c0e5685SJohn Baldwin sb->sb_mtls = n; 17153c0e5685SJohn Baldwin n->m_next = m->m_next; 17163c0e5685SJohn Baldwin if (sb->sb_mtlstail == m) 17173c0e5685SJohn Baldwin sb->sb_mtlstail = n; 17183c0e5685SJohn Baldwin 17193c0e5685SJohn Baldwin /* Detach the record from the TLS chain. */ 17203c0e5685SJohn Baldwin m->m_next = NULL; 17213c0e5685SJohn Baldwin 17223c0e5685SJohn Baldwin out: 17233c0e5685SJohn Baldwin MPASS(m_length(top, NULL) == len); 17243c0e5685SJohn Baldwin for (m = top; m != NULL; m = m->m_next) 17253c0e5685SJohn Baldwin sbfree_ktls_rx(sb, m); 17263c0e5685SJohn Baldwin sb->sb_tlsdcc = len; 17273c0e5685SJohn Baldwin sb->sb_ccc += len; 17283c0e5685SJohn Baldwin SBCHECK(sb); 17293c0e5685SJohn Baldwin return (top); 17303c0e5685SJohn Baldwin } 17313c0e5685SJohn Baldwin 17323c0e5685SJohn Baldwin static void 17333c0e5685SJohn Baldwin ktls_decrypt(struct socket *so) 17343c0e5685SJohn Baldwin { 17353c0e5685SJohn Baldwin char tls_header[MBUF_PEXT_HDR_LEN]; 17363c0e5685SJohn Baldwin struct ktls_session *tls; 17373c0e5685SJohn Baldwin struct sockbuf *sb; 17383c0e5685SJohn Baldwin struct tls_record_layer *hdr; 17393c0e5685SJohn Baldwin struct tls_get_record tgr; 17403c0e5685SJohn Baldwin struct mbuf *control, *data, *m; 17413c0e5685SJohn Baldwin uint64_t seqno; 17423c0e5685SJohn Baldwin int error, remain, tls_len, trail_len; 17433c0e5685SJohn Baldwin 17443c0e5685SJohn Baldwin hdr = (struct tls_record_layer *)tls_header; 17453c0e5685SJohn Baldwin sb = &so->so_rcv; 17463c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 17473c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING, 17483c0e5685SJohn Baldwin ("%s: socket %p not running", __func__, so)); 17493c0e5685SJohn Baldwin 17503c0e5685SJohn Baldwin tls = sb->sb_tls_info; 17513c0e5685SJohn Baldwin MPASS(tls != NULL); 17523c0e5685SJohn Baldwin 17533c0e5685SJohn Baldwin for (;;) { 17543c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 17553c0e5685SJohn Baldwin if (sb->sb_tlscc < tls->params.tls_hlen) 17563c0e5685SJohn Baldwin break; 17573c0e5685SJohn Baldwin 17583c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header); 17593c0e5685SJohn Baldwin tls_len = sizeof(*hdr) + ntohs(hdr->tls_length); 17603c0e5685SJohn Baldwin 17613c0e5685SJohn Baldwin if (hdr->tls_vmajor != tls->params.tls_vmajor || 17623c0e5685SJohn Baldwin hdr->tls_vminor != tls->params.tls_vminor) 17633c0e5685SJohn Baldwin error = EINVAL; 17643c0e5685SJohn Baldwin else if (tls_len < tls->params.tls_hlen || tls_len > 17653c0e5685SJohn Baldwin tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 + 17663c0e5685SJohn Baldwin tls->params.tls_tlen) 17673c0e5685SJohn Baldwin error = EMSGSIZE; 17683c0e5685SJohn Baldwin else 17693c0e5685SJohn Baldwin error = 0; 17703c0e5685SJohn Baldwin if (__predict_false(error != 0)) { 17713c0e5685SJohn Baldwin /* 17723c0e5685SJohn Baldwin * We have a corrupted record and are likely 17733c0e5685SJohn Baldwin * out of sync. The connection isn't 17743c0e5685SJohn Baldwin * recoverable at this point, so abort it. 17753c0e5685SJohn Baldwin */ 17763c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 17773c0e5685SJohn Baldwin counter_u64_add(ktls_offload_corrupted_records, 1); 17783c0e5685SJohn Baldwin 17793c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 17803c0e5685SJohn Baldwin so->so_proto->pr_usrreqs->pru_abort(so); 17813c0e5685SJohn Baldwin so->so_error = error; 17823c0e5685SJohn Baldwin CURVNET_RESTORE(); 17833c0e5685SJohn Baldwin goto deref; 17843c0e5685SJohn Baldwin } 17853c0e5685SJohn Baldwin 17863c0e5685SJohn Baldwin /* Is the entire record queued? */ 17873c0e5685SJohn Baldwin if (sb->sb_tlscc < tls_len) 17883c0e5685SJohn Baldwin break; 17893c0e5685SJohn Baldwin 17903c0e5685SJohn Baldwin /* 17913c0e5685SJohn Baldwin * Split out the portion of the mbuf chain containing 17923c0e5685SJohn Baldwin * this TLS record. 17933c0e5685SJohn Baldwin */ 17943c0e5685SJohn Baldwin data = ktls_detach_record(sb, tls_len); 17953c0e5685SJohn Baldwin if (data == NULL) 17963c0e5685SJohn Baldwin continue; 17973c0e5685SJohn Baldwin MPASS(sb->sb_tlsdcc == tls_len); 17983c0e5685SJohn Baldwin 17993c0e5685SJohn Baldwin seqno = sb->sb_tls_seqno; 18003c0e5685SJohn Baldwin sb->sb_tls_seqno++; 18013c0e5685SJohn Baldwin SBCHECK(sb); 18023c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 18033c0e5685SJohn Baldwin 18043c0e5685SJohn Baldwin error = tls->sw_decrypt(tls, hdr, data, seqno, &trail_len); 18053c0e5685SJohn Baldwin if (error) { 18063c0e5685SJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 18073c0e5685SJohn Baldwin 18083c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 18093c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 18103c0e5685SJohn Baldwin /* 18113c0e5685SJohn Baldwin * sbcut/drop/flush discarded these 18123c0e5685SJohn Baldwin * mbufs. 18133c0e5685SJohn Baldwin */ 18143c0e5685SJohn Baldwin m_freem(data); 18153c0e5685SJohn Baldwin break; 18163c0e5685SJohn Baldwin } 18173c0e5685SJohn Baldwin 18183c0e5685SJohn Baldwin /* 18193c0e5685SJohn Baldwin * Drop this TLS record's data, but keep 18203c0e5685SJohn Baldwin * decrypting subsequent records. 18213c0e5685SJohn Baldwin */ 18223c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 18233c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 18243c0e5685SJohn Baldwin 18253c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 18263c0e5685SJohn Baldwin so->so_error = EBADMSG; 18273c0e5685SJohn Baldwin sorwakeup_locked(so); 18283c0e5685SJohn Baldwin CURVNET_RESTORE(); 18293c0e5685SJohn Baldwin 18303c0e5685SJohn Baldwin m_freem(data); 18313c0e5685SJohn Baldwin 18323c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 18333c0e5685SJohn Baldwin continue; 18343c0e5685SJohn Baldwin } 18353c0e5685SJohn Baldwin 18363c0e5685SJohn Baldwin /* Allocate the control mbuf. */ 18373c0e5685SJohn Baldwin tgr.tls_type = hdr->tls_type; 18383c0e5685SJohn Baldwin tgr.tls_vmajor = hdr->tls_vmajor; 18393c0e5685SJohn Baldwin tgr.tls_vminor = hdr->tls_vminor; 18403c0e5685SJohn Baldwin tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen - 18413c0e5685SJohn Baldwin trail_len); 18423c0e5685SJohn Baldwin control = sbcreatecontrol_how(&tgr, sizeof(tgr), 18433c0e5685SJohn Baldwin TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK); 18443c0e5685SJohn Baldwin 18453c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 18463c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 18473c0e5685SJohn Baldwin /* sbcut/drop/flush discarded these mbufs. */ 18483c0e5685SJohn Baldwin MPASS(sb->sb_tlscc == 0); 18493c0e5685SJohn Baldwin m_freem(data); 18503c0e5685SJohn Baldwin m_freem(control); 18513c0e5685SJohn Baldwin break; 18523c0e5685SJohn Baldwin } 18533c0e5685SJohn Baldwin 18543c0e5685SJohn Baldwin /* 18553c0e5685SJohn Baldwin * Clear the 'dcc' accounting in preparation for 18563c0e5685SJohn Baldwin * adding the decrypted record. 18573c0e5685SJohn Baldwin */ 18583c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 18593c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 18603c0e5685SJohn Baldwin SBCHECK(sb); 18613c0e5685SJohn Baldwin 18623c0e5685SJohn Baldwin /* If there is no payload, drop all of the data. */ 18633c0e5685SJohn Baldwin if (tgr.tls_length == htobe16(0)) { 18643c0e5685SJohn Baldwin m_freem(data); 18653c0e5685SJohn Baldwin data = NULL; 18663c0e5685SJohn Baldwin } else { 18673c0e5685SJohn Baldwin /* Trim header. */ 18683c0e5685SJohn Baldwin remain = tls->params.tls_hlen; 18693c0e5685SJohn Baldwin while (remain > 0) { 18703c0e5685SJohn Baldwin if (data->m_len > remain) { 18713c0e5685SJohn Baldwin data->m_data += remain; 18723c0e5685SJohn Baldwin data->m_len -= remain; 18733c0e5685SJohn Baldwin break; 18743c0e5685SJohn Baldwin } 18753c0e5685SJohn Baldwin remain -= data->m_len; 18763c0e5685SJohn Baldwin data = m_free(data); 18773c0e5685SJohn Baldwin } 18783c0e5685SJohn Baldwin 18793c0e5685SJohn Baldwin /* Trim trailer and clear M_NOTREADY. */ 18803c0e5685SJohn Baldwin remain = be16toh(tgr.tls_length); 18813c0e5685SJohn Baldwin m = data; 18823c0e5685SJohn Baldwin for (m = data; remain > m->m_len; m = m->m_next) { 18833c0e5685SJohn Baldwin m->m_flags &= ~M_NOTREADY; 18843c0e5685SJohn Baldwin remain -= m->m_len; 18853c0e5685SJohn Baldwin } 18863c0e5685SJohn Baldwin m->m_len = remain; 18873c0e5685SJohn Baldwin m_freem(m->m_next); 18883c0e5685SJohn Baldwin m->m_next = NULL; 18893c0e5685SJohn Baldwin m->m_flags &= ~M_NOTREADY; 18903c0e5685SJohn Baldwin 18913c0e5685SJohn Baldwin /* Set EOR on the final mbuf. */ 18923c0e5685SJohn Baldwin m->m_flags |= M_EOR; 18933c0e5685SJohn Baldwin } 18943c0e5685SJohn Baldwin 18953c0e5685SJohn Baldwin sbappendcontrol_locked(sb, data, control, 0); 18963c0e5685SJohn Baldwin } 18973c0e5685SJohn Baldwin 18983c0e5685SJohn Baldwin sb->sb_flags &= ~SB_TLS_RX_RUNNING; 18993c0e5685SJohn Baldwin 19003c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0) 19013c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 19023c0e5685SJohn Baldwin 19033c0e5685SJohn Baldwin sorwakeup_locked(so); 19043c0e5685SJohn Baldwin 19053c0e5685SJohn Baldwin deref: 19063c0e5685SJohn Baldwin SOCKBUF_UNLOCK_ASSERT(sb); 19073c0e5685SJohn Baldwin 19083c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 19093c0e5685SJohn Baldwin SOCK_LOCK(so); 19103c0e5685SJohn Baldwin sorele(so); 19113c0e5685SJohn Baldwin CURVNET_RESTORE(); 19123c0e5685SJohn Baldwin } 19133c0e5685SJohn Baldwin 19143c0e5685SJohn Baldwin void 1915d90fe9d0SGleb Smirnoff ktls_enqueue_to_free(struct mbuf *m) 1916b2e60773SJohn Baldwin { 1917b2e60773SJohn Baldwin struct ktls_wq *wq; 1918b2e60773SJohn Baldwin bool running; 1919b2e60773SJohn Baldwin 1920b2e60773SJohn Baldwin /* Mark it for freeing. */ 19217b6c99d0SGleb Smirnoff m->m_epg_flags |= EPG_FLAG_2FREE; 19227b6c99d0SGleb Smirnoff wq = &ktls_wq[m->m_epg_tls->wq_index]; 1923b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 19243c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 1925b2e60773SJohn Baldwin running = wq->running; 1926b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 1927b2e60773SJohn Baldwin if (!running) 1928b2e60773SJohn Baldwin wakeup(wq); 1929b2e60773SJohn Baldwin } 1930b2e60773SJohn Baldwin 193149f6925cSMark Johnston static void * 193249f6925cSMark Johnston ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m) 193349f6925cSMark Johnston { 193449f6925cSMark Johnston void *buf; 193549f6925cSMark Johnston 193649f6925cSMark Johnston if (m->m_epg_npgs <= 2) 193749f6925cSMark Johnston return (NULL); 193849f6925cSMark Johnston if (ktls_buffer_zone == NULL) 193949f6925cSMark Johnston return (NULL); 194049f6925cSMark Johnston if ((u_int)(ticks - wq->lastallocfail) < hz) { 194149f6925cSMark Johnston /* 194249f6925cSMark Johnston * Rate-limit allocation attempts after a failure. 194349f6925cSMark Johnston * ktls_buffer_import() will acquire a per-domain mutex to check 194449f6925cSMark Johnston * the free page queues and may fail consistently if memory is 194549f6925cSMark Johnston * fragmented. 194649f6925cSMark Johnston */ 194749f6925cSMark Johnston return (NULL); 194849f6925cSMark Johnston } 194949f6925cSMark Johnston buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM); 195049f6925cSMark Johnston if (buf == NULL) 195149f6925cSMark Johnston wq->lastallocfail = ticks; 195249f6925cSMark Johnston return (buf); 195349f6925cSMark Johnston } 195449f6925cSMark Johnston 1955b2e60773SJohn Baldwin void 1956b2e60773SJohn Baldwin ktls_enqueue(struct mbuf *m, struct socket *so, int page_count) 1957b2e60773SJohn Baldwin { 1958b2e60773SJohn Baldwin struct ktls_wq *wq; 1959b2e60773SJohn Baldwin bool running; 1960b2e60773SJohn Baldwin 19616edfd179SGleb Smirnoff KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) == 19626edfd179SGleb Smirnoff (M_EXTPG | M_NOTREADY)), 1963b2e60773SJohn Baldwin ("ktls_enqueue: %p not unready & nomap mbuf\n", m)); 1964b2e60773SJohn Baldwin KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count")); 1965b2e60773SJohn Baldwin 19667b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf")); 1967b2e60773SJohn Baldwin 19687b6c99d0SGleb Smirnoff m->m_epg_enc_cnt = page_count; 1969b2e60773SJohn Baldwin 1970b2e60773SJohn Baldwin /* 1971b2e60773SJohn Baldwin * Save a pointer to the socket. The caller is responsible 1972b2e60773SJohn Baldwin * for taking an additional reference via soref(). 1973b2e60773SJohn Baldwin */ 19747b6c99d0SGleb Smirnoff m->m_epg_so = so; 1975b2e60773SJohn Baldwin 19767b6c99d0SGleb Smirnoff wq = &ktls_wq[m->m_epg_tls->wq_index]; 1977b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 19783c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 1979b2e60773SJohn Baldwin running = wq->running; 1980b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 1981b2e60773SJohn Baldwin if (!running) 1982b2e60773SJohn Baldwin wakeup(wq); 19833c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, 1); 1984b2e60773SJohn Baldwin } 1985b2e60773SJohn Baldwin 19866b313a3aSJohn Baldwin #define MAX_TLS_PAGES (1 + btoc(TLS_MAX_MSG_SIZE_V10_2)) 19876b313a3aSJohn Baldwin 1988b2e60773SJohn Baldwin static __noinline void 198949f6925cSMark Johnston ktls_encrypt(struct ktls_wq *wq, struct mbuf *top) 1990b2e60773SJohn Baldwin { 1991b2e60773SJohn Baldwin struct ktls_session *tls; 1992b2e60773SJohn Baldwin struct socket *so; 1993d90fe9d0SGleb Smirnoff struct mbuf *m; 19946b313a3aSJohn Baldwin vm_paddr_t parray[MAX_TLS_PAGES + 1]; 19956b313a3aSJohn Baldwin struct iovec dst_iov[MAX_TLS_PAGES + 2]; 1996b2e60773SJohn Baldwin vm_page_t pg; 199749f6925cSMark Johnston void *cbuf; 1998b2e60773SJohn Baldwin int error, i, len, npages, off, total_pages; 1999b2e60773SJohn Baldwin 20007b6c99d0SGleb Smirnoff so = top->m_epg_so; 20017b6c99d0SGleb Smirnoff tls = top->m_epg_tls; 2002d90fe9d0SGleb Smirnoff KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 2003d90fe9d0SGleb Smirnoff KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 2004b2e60773SJohn Baldwin #ifdef INVARIANTS 20057b6c99d0SGleb Smirnoff top->m_epg_so = NULL; 2006b2e60773SJohn Baldwin #endif 20077b6c99d0SGleb Smirnoff total_pages = top->m_epg_enc_cnt; 2008b2e60773SJohn Baldwin npages = 0; 2009b2e60773SJohn Baldwin 2010b2e60773SJohn Baldwin /* 2011b2e60773SJohn Baldwin * Encrypt the TLS records in the chain of mbufs starting with 2012b2e60773SJohn Baldwin * 'top'. 'total_pages' gives us a total count of pages and is 2013b2e60773SJohn Baldwin * used to know when we have finished encrypting the TLS 2014b2e60773SJohn Baldwin * records originally queued with 'top'. 2015b2e60773SJohn Baldwin * 2016b2e60773SJohn Baldwin * NB: These mbufs are queued in the socket buffer and 2017b2e60773SJohn Baldwin * 'm_next' is traversing the mbufs in the socket buffer. The 2018b2e60773SJohn Baldwin * socket buffer lock is not held while traversing this chain. 2019b2e60773SJohn Baldwin * Since the mbufs are all marked M_NOTREADY their 'm_next' 2020b2e60773SJohn Baldwin * pointers should be stable. However, the 'm_next' of the 2021b2e60773SJohn Baldwin * last mbuf encrypted is not necessarily NULL. It can point 2022b2e60773SJohn Baldwin * to other mbufs appended while 'top' was on the TLS work 2023b2e60773SJohn Baldwin * queue. 2024b2e60773SJohn Baldwin * 2025b2e60773SJohn Baldwin * Each mbuf holds an entire TLS record. 2026b2e60773SJohn Baldwin */ 2027b2e60773SJohn Baldwin error = 0; 2028b2e60773SJohn Baldwin for (m = top; npages != total_pages; m = m->m_next) { 20297b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls == tls, 2030b2e60773SJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 20317b6c99d0SGleb Smirnoff tls, m->m_epg_tls)); 20326edfd179SGleb Smirnoff KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == 20336edfd179SGleb Smirnoff (M_EXTPG | M_NOTREADY), 2034b2e60773SJohn Baldwin ("%p not unready & nomap mbuf (top = %p)\n", m, top)); 20357b6c99d0SGleb Smirnoff KASSERT(npages + m->m_epg_npgs <= total_pages, 2036b2e60773SJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 2037b2e60773SJohn Baldwin total_pages, m)); 203849f6925cSMark Johnston KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen, 203949f6925cSMark Johnston ("page count %d larger than maximum frame length %d", 204049f6925cSMark Johnston m->m_epg_npgs, ktls_maxlen)); 2041b2e60773SJohn Baldwin 2042b2e60773SJohn Baldwin /* 204321e3c1fbSJohn Baldwin * For anonymous mbufs, encryption is done in place. 204421e3c1fbSJohn Baldwin * For file-backed mbufs (from sendfile), anonymous 204521e3c1fbSJohn Baldwin * wired pages are allocated and used as the 204621e3c1fbSJohn Baldwin * encryption destination. 2047b2e60773SJohn Baldwin */ 204821e3c1fbSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) != 0) { 204921e3c1fbSJohn Baldwin error = (*tls->sw_encrypt)(tls, m, NULL, 0); 205021e3c1fbSJohn Baldwin } else { 205121e3c1fbSJohn Baldwin if ((cbuf = ktls_buffer_alloc(wq, m)) != NULL) { 205221e3c1fbSJohn Baldwin len = ptoa(m->m_epg_npgs - 1) + 205321e3c1fbSJohn Baldwin m->m_epg_last_len - m->m_epg_1st_off; 205421e3c1fbSJohn Baldwin dst_iov[0].iov_base = (char *)cbuf + 205549f6925cSMark Johnston m->m_epg_1st_off; 205649f6925cSMark Johnston dst_iov[0].iov_len = len; 205749f6925cSMark Johnston parray[0] = DMAP_TO_PHYS((vm_offset_t)cbuf); 205849f6925cSMark Johnston i = 1; 205949f6925cSMark Johnston } else { 206049f6925cSMark Johnston off = m->m_epg_1st_off; 206149f6925cSMark Johnston for (i = 0; i < m->m_epg_npgs; i++, off = 0) { 206249f6925cSMark Johnston do { 206349f6925cSMark Johnston pg = vm_page_alloc(NULL, 0, 206449f6925cSMark Johnston VM_ALLOC_NORMAL | 206549f6925cSMark Johnston VM_ALLOC_NOOBJ | 206649f6925cSMark Johnston VM_ALLOC_NODUMP | 206749f6925cSMark Johnston VM_ALLOC_WIRED | 206849f6925cSMark Johnston VM_ALLOC_WAITFAIL); 206949f6925cSMark Johnston } while (pg == NULL); 207049f6925cSMark Johnston 207149f6925cSMark Johnston len = m_epg_pagelen(m, i, off); 2072b2e60773SJohn Baldwin parray[i] = VM_PAGE_TO_PHYS(pg); 2073b2e60773SJohn Baldwin dst_iov[i].iov_base = 207449f6925cSMark Johnston (char *)(void *)PHYS_TO_DMAP( 207549f6925cSMark Johnston parray[i]) + off; 2076b2e60773SJohn Baldwin dst_iov[i].iov_len = len; 2077b2e60773SJohn Baldwin } 207849f6925cSMark Johnston } 20796b313a3aSJohn Baldwin KASSERT(i + 1 <= nitems(dst_iov), 20806b313a3aSJohn Baldwin ("dst_iov is too small")); 20816b313a3aSJohn Baldwin dst_iov[i].iov_base = m->m_epg_trail; 20826b313a3aSJohn Baldwin dst_iov[i].iov_len = m->m_epg_trllen; 2083b2e60773SJohn Baldwin 20846b313a3aSJohn Baldwin error = (*tls->sw_encrypt)(tls, m, dst_iov, i + 1); 2085b2e60773SJohn Baldwin 2086b2e60773SJohn Baldwin /* Free the old pages. */ 2087b2e60773SJohn Baldwin m->m_ext.ext_free(m); 2088b2e60773SJohn Baldwin 2089b2e60773SJohn Baldwin /* Replace them with the new pages. */ 209049f6925cSMark Johnston if (cbuf != NULL) { 209149f6925cSMark Johnston for (i = 0; i < m->m_epg_npgs; i++) 209249f6925cSMark Johnston m->m_epg_pa[i] = parray[0] + ptoa(i); 209349f6925cSMark Johnston 209449f6925cSMark Johnston /* Contig pages should go back to the cache. */ 209549f6925cSMark Johnston m->m_ext.ext_free = ktls_free_mext_contig; 209649f6925cSMark Johnston } else { 20977b6c99d0SGleb Smirnoff for (i = 0; i < m->m_epg_npgs; i++) 20980c103266SGleb Smirnoff m->m_epg_pa[i] = parray[i]; 2099b2e60773SJohn Baldwin 2100b2e60773SJohn Baldwin /* Use the basic free routine. */ 2101b2e60773SJohn Baldwin m->m_ext.ext_free = mb_free_mext_pgs; 210249f6925cSMark Johnston } 2103b2dba663SAndrew Gallatin 2104b2dba663SAndrew Gallatin /* Pages are now writable. */ 21057b6c99d0SGleb Smirnoff m->m_epg_flags |= EPG_FLAG_ANON; 2106b2e60773SJohn Baldwin } 210721e3c1fbSJohn Baldwin if (error) { 210821e3c1fbSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 210921e3c1fbSJohn Baldwin break; 211021e3c1fbSJohn Baldwin } 211121e3c1fbSJohn Baldwin 211221e3c1fbSJohn Baldwin if (__predict_false(m->m_epg_npgs == 0)) { 211321e3c1fbSJohn Baldwin /* TLS 1.0 empty fragment. */ 211421e3c1fbSJohn Baldwin npages++; 211521e3c1fbSJohn Baldwin } else 211621e3c1fbSJohn Baldwin npages += m->m_epg_npgs; 2117b2e60773SJohn Baldwin 2118b2e60773SJohn Baldwin /* 2119b2e60773SJohn Baldwin * Drop a reference to the session now that it is no 2120b2e60773SJohn Baldwin * longer needed. Existing code depends on encrypted 2121b2e60773SJohn Baldwin * records having no associated session vs 2122b2e60773SJohn Baldwin * yet-to-be-encrypted records having an associated 2123b2e60773SJohn Baldwin * session. 2124b2e60773SJohn Baldwin */ 21257b6c99d0SGleb Smirnoff m->m_epg_tls = NULL; 2126b2e60773SJohn Baldwin ktls_free(tls); 2127b2e60773SJohn Baldwin } 2128b2e60773SJohn Baldwin 2129b2e60773SJohn Baldwin CURVNET_SET(so->so_vnet); 2130b2e60773SJohn Baldwin if (error == 0) { 2131b2e60773SJohn Baldwin (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, top, npages); 2132b2e60773SJohn Baldwin } else { 2133b2e60773SJohn Baldwin so->so_proto->pr_usrreqs->pru_abort(so); 2134b2e60773SJohn Baldwin so->so_error = EIO; 2135b2e60773SJohn Baldwin mb_free_notready(top, total_pages); 2136b2e60773SJohn Baldwin } 2137b2e60773SJohn Baldwin 2138b2e60773SJohn Baldwin SOCK_LOCK(so); 2139b2e60773SJohn Baldwin sorele(so); 2140b2e60773SJohn Baldwin CURVNET_RESTORE(); 2141b2e60773SJohn Baldwin } 2142b2e60773SJohn Baldwin 2143b2e60773SJohn Baldwin static void 2144b2e60773SJohn Baldwin ktls_work_thread(void *ctx) 2145b2e60773SJohn Baldwin { 2146b2e60773SJohn Baldwin struct ktls_wq *wq = ctx; 2147d90fe9d0SGleb Smirnoff struct mbuf *m, *n; 21483c0e5685SJohn Baldwin struct socket *so, *son; 21493c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) local_m_head; 21503c0e5685SJohn Baldwin STAILQ_HEAD(, socket) local_so_head; 2151b2e60773SJohn Baldwin 215202bc3865SAndrew Gallatin if (ktls_bind_threads > 1) { 215302bc3865SAndrew Gallatin curthread->td_domain.dr_policy = 215402bc3865SAndrew Gallatin DOMAINSET_PREF(PCPU_GET(domain)); 215502bc3865SAndrew Gallatin } 2156b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 2157b2e60773SJohn Baldwin fpu_kern_thread(0); 2158b2e60773SJohn Baldwin #endif 2159b2e60773SJohn Baldwin for (;;) { 2160b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 21613c0e5685SJohn Baldwin while (STAILQ_EMPTY(&wq->m_head) && 21623c0e5685SJohn Baldwin STAILQ_EMPTY(&wq->so_head)) { 2163b2e60773SJohn Baldwin wq->running = false; 2164b2e60773SJohn Baldwin mtx_sleep(wq, &wq->mtx, 0, "-", 0); 2165b2e60773SJohn Baldwin wq->running = true; 2166b2e60773SJohn Baldwin } 2167b2e60773SJohn Baldwin 21683c0e5685SJohn Baldwin STAILQ_INIT(&local_m_head); 21693c0e5685SJohn Baldwin STAILQ_CONCAT(&local_m_head, &wq->m_head); 21703c0e5685SJohn Baldwin STAILQ_INIT(&local_so_head); 21713c0e5685SJohn Baldwin STAILQ_CONCAT(&local_so_head, &wq->so_head); 2172b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2173b2e60773SJohn Baldwin 21743c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) { 21757b6c99d0SGleb Smirnoff if (m->m_epg_flags & EPG_FLAG_2FREE) { 21767b6c99d0SGleb Smirnoff ktls_free(m->m_epg_tls); 2177*904a08f3SMateusz Guzik m_free_raw(m); 2178eeec8348SGleb Smirnoff } else { 217949f6925cSMark Johnston ktls_encrypt(wq, m); 21803c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, -1); 2181b2e60773SJohn Baldwin } 2182b2e60773SJohn Baldwin } 21833c0e5685SJohn Baldwin 21843c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) { 21853c0e5685SJohn Baldwin ktls_decrypt(so); 21863c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, -1); 21873c0e5685SJohn Baldwin } 2188b2e60773SJohn Baldwin } 2189b2e60773SJohn Baldwin } 2190