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 #include "opt_inet.h" 30b2e60773SJohn Baldwin #include "opt_inet6.h" 3128d0a740SAndrew Gallatin #include "opt_kern_tls.h" 32ed5e13cfSAndrew Gallatin #include "opt_ratelimit.h" 33b2e60773SJohn Baldwin #include "opt_rss.h" 34b2e60773SJohn Baldwin 35b2e60773SJohn Baldwin #include <sys/param.h> 36b2e60773SJohn Baldwin #include <sys/kernel.h> 3702bc3865SAndrew Gallatin #include <sys/domainset.h> 38470e851cSJohn Baldwin #include <sys/endian.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 #include <netinet/in.h> 68b2e60773SJohn Baldwin #include <netinet/in_pcb.h> 69b2e60773SJohn Baldwin #include <netinet/tcp_var.h> 709e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 719e14430dSJohn Baldwin #include <netinet/tcp_offload.h> 729e14430dSJohn Baldwin #endif 73470e851cSJohn Baldwin #include <opencrypto/cryptodev.h> 74470e851cSJohn Baldwin #include <opencrypto/ktls.h> 75b2e60773SJohn Baldwin #include <vm/vm.h> 76b2e60773SJohn Baldwin #include <vm/vm_pageout.h> 77b2e60773SJohn Baldwin #include <vm/vm_page.h> 7898215005SAndrew Gallatin #include <vm/vm_pagequeue.h> 79b2e60773SJohn Baldwin 80b2e60773SJohn Baldwin struct ktls_wq { 81b2e60773SJohn Baldwin struct mtx mtx; 823c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) m_head; 833c0e5685SJohn Baldwin STAILQ_HEAD(, socket) so_head; 84b2e60773SJohn Baldwin bool running; 8549f6925cSMark Johnston int lastallocfail; 86b2e60773SJohn Baldwin } __aligned(CACHE_LINE_SIZE); 87b2e60773SJohn Baldwin 8819855852SAndrew Gallatin struct ktls_reclaim_thread { 8998215005SAndrew Gallatin uint64_t wakeups; 9019855852SAndrew Gallatin uint64_t reclaims; 9198215005SAndrew Gallatin struct thread *td; 9298215005SAndrew Gallatin int running; 9398215005SAndrew Gallatin }; 9498215005SAndrew Gallatin 9502bc3865SAndrew Gallatin struct ktls_domain_info { 9602bc3865SAndrew Gallatin int count; 9702bc3865SAndrew Gallatin int cpu[MAXCPU]; 9819855852SAndrew Gallatin struct ktls_reclaim_thread reclaim_td; 9902bc3865SAndrew Gallatin }; 10002bc3865SAndrew Gallatin 10102bc3865SAndrew Gallatin struct ktls_domain_info ktls_domains[MAXMEMDOM]; 102b2e60773SJohn Baldwin static struct ktls_wq *ktls_wq; 103b2e60773SJohn Baldwin static struct proc *ktls_proc; 104b2e60773SJohn Baldwin static uma_zone_t ktls_session_zone; 10549f6925cSMark Johnston static uma_zone_t ktls_buffer_zone; 106b2e60773SJohn Baldwin static uint16_t ktls_cpuid_lookup[MAXCPU]; 107a72ee355SJohn Baldwin static int ktls_init_state; 108a72ee355SJohn Baldwin static struct sx ktls_init_lock; 109a72ee355SJohn Baldwin SX_SYSINIT(ktls_init_lock, &ktls_init_lock, "ktls init"); 110b2e60773SJohn Baldwin 1117029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 112b2e60773SJohn Baldwin "Kernel TLS offload"); 1137029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 114b2e60773SJohn Baldwin "Kernel TLS offload stats"); 115b2e60773SJohn Baldwin 116b2e60773SJohn Baldwin #ifdef RSS 117b2e60773SJohn Baldwin static int ktls_bind_threads = 1; 118b2e60773SJohn Baldwin #else 119b2e60773SJohn Baldwin static int ktls_bind_threads; 120b2e60773SJohn Baldwin #endif 121b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN, 122b2e60773SJohn Baldwin &ktls_bind_threads, 0, 1234dc1b17dSMark Johnston "Bind crypto threads to cores (1) or cores and domains (2) at boot"); 124b2e60773SJohn Baldwin 125b2e60773SJohn Baldwin static u_int ktls_maxlen = 16384; 12649f6925cSMark Johnston SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RDTUN, 127b2e60773SJohn Baldwin &ktls_maxlen, 0, "Maximum TLS record size"); 128b2e60773SJohn Baldwin 129b2e60773SJohn Baldwin static int ktls_number_threads; 130b2e60773SJohn Baldwin SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD, 131b2e60773SJohn Baldwin &ktls_number_threads, 0, 132b2e60773SJohn Baldwin "Number of TLS threads in thread-pool"); 133b2e60773SJohn Baldwin 13428d0a740SAndrew Gallatin unsigned int ktls_ifnet_max_rexmit_pct = 2; 13528d0a740SAndrew Gallatin SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, ifnet_max_rexmit_pct, CTLFLAG_RWTUN, 13628d0a740SAndrew Gallatin &ktls_ifnet_max_rexmit_pct, 2, 13728d0a740SAndrew Gallatin "Max percent bytes retransmitted before ifnet TLS is disabled"); 13828d0a740SAndrew Gallatin 139b2e60773SJohn Baldwin static bool ktls_offload_enable; 140b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RWTUN, 141b2e60773SJohn Baldwin &ktls_offload_enable, 0, 142b2e60773SJohn Baldwin "Enable support for kernel TLS offload"); 143b2e60773SJohn Baldwin 144b2e60773SJohn Baldwin static bool ktls_cbc_enable = true; 145b5aa9ad4SMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RWTUN, 146b2e60773SJohn Baldwin &ktls_cbc_enable, 1, 1479a673b71SJohn Baldwin "Enable support of AES-CBC crypto for kernel TLS"); 148b2e60773SJohn Baldwin 14949f6925cSMark Johnston static bool ktls_sw_buffer_cache = true; 15049f6925cSMark Johnston SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, sw_buffer_cache, CTLFLAG_RDTUN, 15149f6925cSMark Johnston &ktls_sw_buffer_cache, 1, 15249f6925cSMark Johnston "Enable caching of output buffers for SW encryption"); 15349f6925cSMark Johnston 15419855852SAndrew Gallatin static int ktls_max_reclaim = 1024; 15519855852SAndrew Gallatin SYSCTL_INT(_kern_ipc_tls, OID_AUTO, max_reclaim, CTLFLAG_RWTUN, 15619855852SAndrew Gallatin &ktls_max_reclaim, 128, 15719855852SAndrew Gallatin "Max number of 16k buffers to reclaim in thread context"); 15898215005SAndrew Gallatin 1591755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_tasks_active); 160b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD, 161b2e60773SJohn Baldwin &ktls_tasks_active, "Number of active tasks"); 162b2e60773SJohn Baldwin 1639f03d2c0SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_pending); 1649f03d2c0SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_pending, CTLFLAG_RD, 1659f03d2c0SJohn Baldwin &ktls_cnt_tx_pending, 1669f03d2c0SJohn Baldwin "Number of TLS 1.0 records waiting for earlier TLS records"); 1679f03d2c0SJohn Baldwin 1681755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_queued); 1693c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_inqueue, CTLFLAG_RD, 1703c0e5685SJohn Baldwin &ktls_cnt_tx_queued, 1713c0e5685SJohn Baldwin "Number of TLS records in queue to tasks for SW encryption"); 1723c0e5685SJohn Baldwin 1731755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_cnt_rx_queued); 1743c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_rx_inqueue, CTLFLAG_RD, 1753c0e5685SJohn Baldwin &ktls_cnt_rx_queued, 1763c0e5685SJohn Baldwin "Number of TLS sockets in queue to tasks for SW decryption"); 177b2e60773SJohn Baldwin 1781755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_total); 179b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total, 180b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_total, 181b2e60773SJohn Baldwin "Total successful TLS setups (parameters set)"); 182b2e60773SJohn Baldwin 1831755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_enable_calls); 184b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls, 185b2e60773SJohn Baldwin CTLFLAG_RD, &ktls_offload_enable_calls, 186b2e60773SJohn Baldwin "Total number of TLS enable calls made"); 187b2e60773SJohn Baldwin 1881755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_active); 189b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD, 190b2e60773SJohn Baldwin &ktls_offload_active, "Total Active TLS sessions"); 191b2e60773SJohn Baldwin 1921755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_corrupted_records); 1933c0e5685SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, corrupted_records, CTLFLAG_RD, 1943c0e5685SJohn Baldwin &ktls_offload_corrupted_records, "Total corrupted TLS records received"); 1953c0e5685SJohn Baldwin 1961755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_offload_failed_crypto); 197b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD, 198b2e60773SJohn Baldwin &ktls_offload_failed_crypto, "Total TLS crypto failures"); 199b2e60773SJohn Baldwin 2001755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_ifnet); 201b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD, 202b2e60773SJohn Baldwin &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet"); 203b2e60773SJohn Baldwin 2041755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_sw); 205b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD, 206b2e60773SJohn Baldwin &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW"); 207b2e60773SJohn Baldwin 2081755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_switch_failed); 209b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD, 210b2e60773SJohn Baldwin &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet"); 211b2e60773SJohn Baldwin 21228d0a740SAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_fail); 21328d0a740SAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_failed, CTLFLAG_RD, 21428d0a740SAndrew Gallatin &ktls_ifnet_disable_fail, "TLS sessions unable to switch to SW from ifnet"); 21528d0a740SAndrew Gallatin 21628d0a740SAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_ok); 21728d0a740SAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_ok, CTLFLAG_RD, 21828d0a740SAndrew Gallatin &ktls_ifnet_disable_ok, "TLS sessions able to switch to SW from ifnet"); 21928d0a740SAndrew Gallatin 220c0e4090eSAndrew Gallatin static COUNTER_U64_DEFINE_EARLY(ktls_destroy_task); 221c0e4090eSAndrew Gallatin SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, destroy_task, CTLFLAG_RD, 222c0e4090eSAndrew Gallatin &ktls_destroy_task, 223c0e4090eSAndrew Gallatin "Number of times ktls session was destroyed via taskqueue"); 224c0e4090eSAndrew Gallatin 2257029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 226b2e60773SJohn Baldwin "Software TLS session stats"); 2277029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 228b2e60773SJohn Baldwin "Hardware (ifnet) TLS session stats"); 2299e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 2319e14430dSJohn Baldwin "TOE TLS session stats"); 2329e14430dSJohn Baldwin #endif 233b2e60773SJohn Baldwin 2341755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc); 235b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc, 236b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-CBC"); 237b2e60773SJohn Baldwin 2381755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm); 239b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm, 240b2e60773SJohn Baldwin "Active number of software TLS sessions using AES-GCM"); 241b2e60773SJohn Baldwin 2429c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20); 2439c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD, 2449c64fc40SJohn Baldwin &ktls_sw_chacha20, 2459c64fc40SJohn Baldwin "Active number of software TLS sessions using Chacha20-Poly1305"); 2469c64fc40SJohn Baldwin 2471755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc); 248b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD, 249b2e60773SJohn Baldwin &ktls_ifnet_cbc, 250b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-CBC"); 251b2e60773SJohn Baldwin 2521755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm); 253b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD, 254b2e60773SJohn Baldwin &ktls_ifnet_gcm, 255b2e60773SJohn Baldwin "Active number of ifnet TLS sessions using AES-GCM"); 256b2e60773SJohn Baldwin 2579c64fc40SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20); 2589c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD, 2599c64fc40SJohn Baldwin &ktls_ifnet_chacha20, 2609c64fc40SJohn Baldwin "Active number of ifnet TLS sessions using Chacha20-Poly1305"); 2619c64fc40SJohn Baldwin 2621755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset); 263b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD, 264b2e60773SJohn Baldwin &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag"); 265b2e60773SJohn Baldwin 2661755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped); 267b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD, 268b2e60773SJohn Baldwin &ktls_ifnet_reset_dropped, 269b2e60773SJohn Baldwin "TLS sessions dropped after failing to update ifnet send tag"); 270b2e60773SJohn Baldwin 2711755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed); 272b2e60773SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD, 273b2e60773SJohn Baldwin &ktls_ifnet_reset_failed, 274b2e60773SJohn Baldwin "TLS sessions that failed to allocate a new ifnet send tag"); 275b2e60773SJohn Baldwin 276b2e60773SJohn Baldwin static int ktls_ifnet_permitted; 277b2e60773SJohn Baldwin SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN, 278b2e60773SJohn Baldwin &ktls_ifnet_permitted, 1, 279b2e60773SJohn Baldwin "Whether to permit hardware (ifnet) TLS sessions"); 280b2e60773SJohn Baldwin 2819e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 2821755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc); 2839e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD, 2849e14430dSJohn Baldwin &ktls_toe_cbc, 2859e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-CBC"); 2869e14430dSJohn Baldwin 2871755b2b9SMark Johnston static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm); 2889e14430dSJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD, 2899e14430dSJohn Baldwin &ktls_toe_gcm, 2909e14430dSJohn Baldwin "Active number of TOE TLS sessions using AES-GCM"); 2919c64fc40SJohn Baldwin 29290972f04SJohn Baldwin static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20); 2939c64fc40SJohn Baldwin SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD, 2949c64fc40SJohn Baldwin &ktls_toe_chacha20, 2959c64fc40SJohn Baldwin "Active number of TOE TLS sessions using Chacha20-Poly1305"); 2969e14430dSJohn Baldwin #endif 2979e14430dSJohn Baldwin 298b2e60773SJohn Baldwin static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS"); 299b2e60773SJohn Baldwin 30085df11a1SRichard Scheffenegger static void ktls_reclaim_thread(void *ctx); 301fe8c78f0SHans Petter Selasky static void ktls_reset_receive_tag(void *context, int pending); 302b2e60773SJohn Baldwin static void ktls_reset_send_tag(void *context, int pending); 303b2e60773SJohn Baldwin static void ktls_work_thread(void *ctx); 30485df11a1SRichard Scheffenegger 30585df11a1SRichard Scheffenegger int 30685df11a1SRichard Scheffenegger ktls_copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 30785df11a1SRichard Scheffenegger { 30885df11a1SRichard Scheffenegger struct tls_enable_v0 tls_v0; 30985df11a1SRichard Scheffenegger int error; 31085df11a1SRichard Scheffenegger uint8_t *cipher_key = NULL, *iv = NULL, *auth_key = NULL; 31185df11a1SRichard Scheffenegger 31285df11a1SRichard Scheffenegger if (sopt->sopt_valsize == sizeof(tls_v0)) { 31385df11a1SRichard Scheffenegger error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), sizeof(tls_v0)); 31485df11a1SRichard Scheffenegger if (error != 0) 31585df11a1SRichard Scheffenegger goto done; 31685df11a1SRichard Scheffenegger memset(tls, 0, sizeof(*tls)); 31785df11a1SRichard Scheffenegger tls->cipher_key = tls_v0.cipher_key; 31885df11a1SRichard Scheffenegger tls->iv = tls_v0.iv; 31985df11a1SRichard Scheffenegger tls->auth_key = tls_v0.auth_key; 32085df11a1SRichard Scheffenegger tls->cipher_algorithm = tls_v0.cipher_algorithm; 32185df11a1SRichard Scheffenegger tls->cipher_key_len = tls_v0.cipher_key_len; 32285df11a1SRichard Scheffenegger tls->iv_len = tls_v0.iv_len; 32385df11a1SRichard Scheffenegger tls->auth_algorithm = tls_v0.auth_algorithm; 32485df11a1SRichard Scheffenegger tls->auth_key_len = tls_v0.auth_key_len; 32585df11a1SRichard Scheffenegger tls->flags = tls_v0.flags; 32685df11a1SRichard Scheffenegger tls->tls_vmajor = tls_v0.tls_vmajor; 32785df11a1SRichard Scheffenegger tls->tls_vminor = tls_v0.tls_vminor; 32885df11a1SRichard Scheffenegger } else 32985df11a1SRichard Scheffenegger error = sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)); 33085df11a1SRichard Scheffenegger 33185df11a1SRichard Scheffenegger if (error != 0) 332b5a9299bSRichard Scheffenegger return (error); 333b5a9299bSRichard Scheffenegger 334b5a9299bSRichard Scheffenegger if (tls->cipher_key_len < 0 || tls->cipher_key_len > TLS_MAX_PARAM_SIZE) 335b5a9299bSRichard Scheffenegger return (EINVAL); 336b5a9299bSRichard Scheffenegger if (tls->iv_len < 0 || tls->iv_len > sizeof(((struct ktls_session *)NULL)->params.iv)) 337b5a9299bSRichard Scheffenegger return (EINVAL); 338b5a9299bSRichard Scheffenegger if (tls->auth_key_len < 0 || tls->auth_key_len > TLS_MAX_PARAM_SIZE) 339b5a9299bSRichard Scheffenegger return (EINVAL); 340b5a9299bSRichard Scheffenegger 341b5a9299bSRichard Scheffenegger /* All supported algorithms require a cipher key. */ 342b5a9299bSRichard Scheffenegger if (tls->cipher_key_len == 0) 343b5a9299bSRichard Scheffenegger return (EINVAL); 34485df11a1SRichard Scheffenegger 34585df11a1SRichard Scheffenegger /* 34685df11a1SRichard Scheffenegger * Now do a deep copy of the variable-length arrays in the struct, so that 34785df11a1SRichard Scheffenegger * subsequent consumers of it can reliably assume kernel memory. This 34885df11a1SRichard Scheffenegger * requires doing our own allocations, which we will free in the 34985df11a1SRichard Scheffenegger * error paths so that our caller need only worry about outstanding 35085df11a1SRichard Scheffenegger * allocations existing on successful return. 35185df11a1SRichard Scheffenegger */ 352b5a9299bSRichard Scheffenegger if (tls->cipher_key_len != 0) { 35385df11a1SRichard Scheffenegger cipher_key = malloc(tls->cipher_key_len, M_KTLS, M_WAITOK); 35485df11a1SRichard Scheffenegger if (sopt->sopt_td != NULL) { 35585df11a1SRichard Scheffenegger error = copyin(tls->cipher_key, cipher_key, tls->cipher_key_len); 35685df11a1SRichard Scheffenegger if (error != 0) 35785df11a1SRichard Scheffenegger goto done; 358b5a9299bSRichard Scheffenegger } else { 359b5a9299bSRichard Scheffenegger bcopy(tls->cipher_key, cipher_key, tls->cipher_key_len); 360b5a9299bSRichard Scheffenegger } 361b5a9299bSRichard Scheffenegger } 362b5a9299bSRichard Scheffenegger if (tls->iv_len != 0) { 363b5a9299bSRichard Scheffenegger iv = malloc(tls->iv_len, M_KTLS, M_WAITOK); 364b5a9299bSRichard Scheffenegger if (sopt->sopt_td != NULL) { 36585df11a1SRichard Scheffenegger error = copyin(tls->iv, iv, tls->iv_len); 36685df11a1SRichard Scheffenegger if (error != 0) 36785df11a1SRichard Scheffenegger goto done; 368b5a9299bSRichard Scheffenegger } else { 369b5a9299bSRichard Scheffenegger bcopy(tls->iv, iv, tls->iv_len); 370b5a9299bSRichard Scheffenegger } 371b5a9299bSRichard Scheffenegger } 372b5a9299bSRichard Scheffenegger if (tls->auth_key_len != 0) { 373b5a9299bSRichard Scheffenegger auth_key = malloc(tls->auth_key_len, M_KTLS, M_WAITOK); 374b5a9299bSRichard Scheffenegger if (sopt->sopt_td != NULL) { 37585df11a1SRichard Scheffenegger error = copyin(tls->auth_key, auth_key, tls->auth_key_len); 37685df11a1SRichard Scheffenegger if (error != 0) 37785df11a1SRichard Scheffenegger goto done; 37885df11a1SRichard Scheffenegger } else { 37985df11a1SRichard Scheffenegger bcopy(tls->auth_key, auth_key, tls->auth_key_len); 38085df11a1SRichard Scheffenegger } 381b5a9299bSRichard Scheffenegger } 38285df11a1SRichard Scheffenegger tls->cipher_key = cipher_key; 38385df11a1SRichard Scheffenegger tls->iv = iv; 38485df11a1SRichard Scheffenegger tls->auth_key = auth_key; 38585df11a1SRichard Scheffenegger 38685df11a1SRichard Scheffenegger done: 38785df11a1SRichard Scheffenegger if (error != 0) { 38885df11a1SRichard Scheffenegger zfree(cipher_key, M_KTLS); 38985df11a1SRichard Scheffenegger zfree(iv, M_KTLS); 39085df11a1SRichard Scheffenegger zfree(auth_key, M_KTLS); 39185df11a1SRichard Scheffenegger } 39285df11a1SRichard Scheffenegger 39385df11a1SRichard Scheffenegger return (error); 39485df11a1SRichard Scheffenegger } 39585df11a1SRichard Scheffenegger 39685df11a1SRichard Scheffenegger void 39785df11a1SRichard Scheffenegger ktls_cleanup_tls_enable(struct tls_enable *tls) 39885df11a1SRichard Scheffenegger { 39985df11a1SRichard Scheffenegger zfree(__DECONST(void *, tls->cipher_key), M_KTLS); 40085df11a1SRichard Scheffenegger zfree(__DECONST(void *, tls->iv), M_KTLS); 40185df11a1SRichard Scheffenegger zfree(__DECONST(void *, tls->auth_key), M_KTLS); 40285df11a1SRichard Scheffenegger } 403b2e60773SJohn Baldwin 404a2fba2a7SBjoern A. Zeeb static u_int 405b2e60773SJohn Baldwin ktls_get_cpu(struct socket *so) 406b2e60773SJohn Baldwin { 407b2e60773SJohn Baldwin struct inpcb *inp; 40802bc3865SAndrew Gallatin #ifdef NUMA 40902bc3865SAndrew Gallatin struct ktls_domain_info *di; 41002bc3865SAndrew Gallatin #endif 411a2fba2a7SBjoern A. Zeeb u_int cpuid; 412b2e60773SJohn Baldwin 413b2e60773SJohn Baldwin inp = sotoinpcb(so); 414b2e60773SJohn Baldwin #ifdef RSS 415b2e60773SJohn Baldwin cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype); 416b2e60773SJohn Baldwin if (cpuid != NETISR_CPUID_NONE) 417b2e60773SJohn Baldwin return (cpuid); 418b2e60773SJohn Baldwin #endif 419b2e60773SJohn Baldwin /* 420b2e60773SJohn Baldwin * Just use the flowid to shard connections in a repeatable 42121e3c1fbSJohn Baldwin * fashion. Note that TLS 1.0 sessions rely on the 422b2e60773SJohn Baldwin * serialization provided by having the same connection use 423b2e60773SJohn Baldwin * the same queue. 424b2e60773SJohn Baldwin */ 42502bc3865SAndrew Gallatin #ifdef NUMA 42602bc3865SAndrew Gallatin if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) { 42702bc3865SAndrew Gallatin di = &ktls_domains[inp->inp_numa_domain]; 42802bc3865SAndrew Gallatin cpuid = di->cpu[inp->inp_flowid % di->count]; 42902bc3865SAndrew Gallatin } else 43002bc3865SAndrew Gallatin #endif 431b2e60773SJohn Baldwin cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads]; 432b2e60773SJohn Baldwin return (cpuid); 433b2e60773SJohn Baldwin } 434b2e60773SJohn Baldwin 43549f6925cSMark Johnston static int 43649f6925cSMark Johnston ktls_buffer_import(void *arg, void **store, int count, int domain, int flags) 43749f6925cSMark Johnston { 43849f6925cSMark Johnston vm_page_t m; 43984c39222SMark Johnston int i, req; 44049f6925cSMark Johnston 44149f6925cSMark Johnston KASSERT((ktls_maxlen & PAGE_MASK) == 0, 44249f6925cSMark Johnston ("%s: ktls max length %d is not page size-aligned", 44349f6925cSMark Johnston __func__, ktls_maxlen)); 44449f6925cSMark Johnston 44584c39222SMark Johnston req = VM_ALLOC_WIRED | VM_ALLOC_NODUMP | malloc2vm_flags(flags); 44649f6925cSMark Johnston for (i = 0; i < count; i++) { 44784c39222SMark Johnston m = vm_page_alloc_noobj_contig_domain(domain, req, 44849f6925cSMark Johnston atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0, 44949f6925cSMark Johnston VM_MEMATTR_DEFAULT); 45049f6925cSMark Johnston if (m == NULL) 45149f6925cSMark Johnston break; 45249f6925cSMark Johnston store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 45349f6925cSMark Johnston } 45449f6925cSMark Johnston return (i); 45549f6925cSMark Johnston } 45649f6925cSMark Johnston 45749f6925cSMark Johnston static void 45849f6925cSMark Johnston ktls_buffer_release(void *arg __unused, void **store, int count) 45949f6925cSMark Johnston { 46049f6925cSMark Johnston vm_page_t m; 46149f6925cSMark Johnston int i, j; 46249f6925cSMark Johnston 46349f6925cSMark Johnston for (i = 0; i < count; i++) { 46449f6925cSMark Johnston m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i])); 46549f6925cSMark Johnston for (j = 0; j < atop(ktls_maxlen); j++) { 46649f6925cSMark Johnston (void)vm_page_unwire_noq(m + j); 46749f6925cSMark Johnston vm_page_free(m + j); 46849f6925cSMark Johnston } 46949f6925cSMark Johnston } 47049f6925cSMark Johnston } 47149f6925cSMark Johnston 47249f6925cSMark Johnston static void 47349f6925cSMark Johnston ktls_free_mext_contig(struct mbuf *m) 47449f6925cSMark Johnston { 47549f6925cSMark Johnston M_ASSERTEXTPG(m); 47649f6925cSMark Johnston uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0])); 47749f6925cSMark Johnston } 47849f6925cSMark Johnston 479a72ee355SJohn Baldwin static int 480a72ee355SJohn Baldwin ktls_init(void) 481b2e60773SJohn Baldwin { 482b2e60773SJohn Baldwin struct thread *td; 483b2e60773SJohn Baldwin struct pcpu *pc; 48402bc3865SAndrew Gallatin int count, domain, error, i; 485b2e60773SJohn Baldwin 486b2e60773SJohn Baldwin ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS, 487b2e60773SJohn Baldwin M_WAITOK | M_ZERO); 488b2e60773SJohn Baldwin 489b2e60773SJohn Baldwin ktls_session_zone = uma_zcreate("ktls_session", 490b2e60773SJohn Baldwin sizeof(struct ktls_session), 491b2e60773SJohn Baldwin NULL, NULL, NULL, NULL, 492b2e60773SJohn Baldwin UMA_ALIGN_CACHE, 0); 493b2e60773SJohn Baldwin 49449f6925cSMark Johnston if (ktls_sw_buffer_cache) { 49549f6925cSMark Johnston ktls_buffer_zone = uma_zcache_create("ktls_buffers", 49649f6925cSMark Johnston roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL, 49749f6925cSMark Johnston ktls_buffer_import, ktls_buffer_release, NULL, 49849f6925cSMark Johnston UMA_ZONE_FIRSTTOUCH); 49949f6925cSMark Johnston } 50049f6925cSMark Johnston 501b2e60773SJohn Baldwin /* 502b2e60773SJohn Baldwin * Initialize the workqueues to run the TLS work. We create a 503b2e60773SJohn Baldwin * work queue for each CPU. 504b2e60773SJohn Baldwin */ 505b2e60773SJohn Baldwin CPU_FOREACH(i) { 5063c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].m_head); 5073c0e5685SJohn Baldwin STAILQ_INIT(&ktls_wq[i].so_head); 508b2e60773SJohn Baldwin mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF); 509b2e60773SJohn Baldwin if (ktls_bind_threads > 1) { 510b2e60773SJohn Baldwin pc = pcpu_find(i); 51102bc3865SAndrew Gallatin domain = pc->pc_domain; 51202bc3865SAndrew Gallatin count = ktls_domains[domain].count; 51302bc3865SAndrew Gallatin ktls_domains[domain].cpu[count] = i; 51402bc3865SAndrew Gallatin ktls_domains[domain].count++; 515b2e60773SJohn Baldwin } 516b2e60773SJohn Baldwin ktls_cpuid_lookup[ktls_number_threads] = i; 517b2e60773SJohn Baldwin ktls_number_threads++; 518b2e60773SJohn Baldwin } 51902bc3865SAndrew Gallatin 52002bc3865SAndrew Gallatin /* 521a72ee355SJohn Baldwin * If we somehow have an empty domain, fall back to choosing 522a72ee355SJohn Baldwin * among all KTLS threads. 523a72ee355SJohn Baldwin */ 524a72ee355SJohn Baldwin if (ktls_bind_threads > 1) { 525a72ee355SJohn Baldwin for (i = 0; i < vm_ndomains; i++) { 526a72ee355SJohn Baldwin if (ktls_domains[i].count == 0) { 527a72ee355SJohn Baldwin ktls_bind_threads = 1; 528a72ee355SJohn Baldwin break; 529a72ee355SJohn Baldwin } 530a72ee355SJohn Baldwin } 531a72ee355SJohn Baldwin } 532a72ee355SJohn Baldwin 533a72ee355SJohn Baldwin /* Start kthreads for each workqueue. */ 534a72ee355SJohn Baldwin CPU_FOREACH(i) { 535a72ee355SJohn Baldwin error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i], 536a72ee355SJohn Baldwin &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i); 537a72ee355SJohn Baldwin if (error) { 538a72ee355SJohn Baldwin printf("Can't add KTLS thread %d error %d\n", i, error); 539a72ee355SJohn Baldwin return (error); 540a72ee355SJohn Baldwin } 541a72ee355SJohn Baldwin } 542a72ee355SJohn Baldwin 543a72ee355SJohn Baldwin /* 54498215005SAndrew Gallatin * Start an allocation thread per-domain to perform blocking allocations 54598215005SAndrew Gallatin * of 16k physically contiguous TLS crypto destination buffers. 54698215005SAndrew Gallatin */ 54798215005SAndrew Gallatin if (ktls_sw_buffer_cache) { 54898215005SAndrew Gallatin for (domain = 0; domain < vm_ndomains; domain++) { 54998215005SAndrew Gallatin if (VM_DOMAIN_EMPTY(domain)) 55098215005SAndrew Gallatin continue; 55198215005SAndrew Gallatin if (CPU_EMPTY(&cpuset_domain[domain])) 55298215005SAndrew Gallatin continue; 55319855852SAndrew Gallatin error = kproc_kthread_add(ktls_reclaim_thread, 55498215005SAndrew Gallatin &ktls_domains[domain], &ktls_proc, 55519855852SAndrew Gallatin &ktls_domains[domain].reclaim_td.td, 55619855852SAndrew Gallatin 0, 0, "KTLS", "reclaim_%d", domain); 557a72ee355SJohn Baldwin if (error) { 55819855852SAndrew Gallatin printf("Can't add KTLS reclaim thread %d error %d\n", 55998215005SAndrew Gallatin domain, error); 560a72ee355SJohn Baldwin return (error); 56102bc3865SAndrew Gallatin } 56202bc3865SAndrew Gallatin } 5634dc1b17dSMark Johnston } 56402bc3865SAndrew Gallatin 56589b65087SMark Johnston if (bootverbose) 566b2e60773SJohn Baldwin printf("KTLS: Initialized %d threads\n", ktls_number_threads); 567a72ee355SJohn Baldwin return (0); 568b2e60773SJohn Baldwin } 569a72ee355SJohn Baldwin 570a72ee355SJohn Baldwin static int 571a72ee355SJohn Baldwin ktls_start_kthreads(void) 572a72ee355SJohn Baldwin { 573a72ee355SJohn Baldwin int error, state; 574a72ee355SJohn Baldwin 575a72ee355SJohn Baldwin start: 576a72ee355SJohn Baldwin state = atomic_load_acq_int(&ktls_init_state); 577a72ee355SJohn Baldwin if (__predict_true(state > 0)) 578a72ee355SJohn Baldwin return (0); 579a72ee355SJohn Baldwin if (state < 0) 580a72ee355SJohn Baldwin return (ENXIO); 581a72ee355SJohn Baldwin 582a72ee355SJohn Baldwin sx_xlock(&ktls_init_lock); 583a72ee355SJohn Baldwin if (ktls_init_state != 0) { 584a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 585a72ee355SJohn Baldwin goto start; 586a72ee355SJohn Baldwin } 587a72ee355SJohn Baldwin 588a72ee355SJohn Baldwin error = ktls_init(); 589a72ee355SJohn Baldwin if (error == 0) 590a72ee355SJohn Baldwin state = 1; 591a72ee355SJohn Baldwin else 592a72ee355SJohn Baldwin state = -1; 593a72ee355SJohn Baldwin atomic_store_rel_int(&ktls_init_state, state); 594a72ee355SJohn Baldwin sx_xunlock(&ktls_init_lock); 595a72ee355SJohn Baldwin return (error); 596a72ee355SJohn Baldwin } 597b2e60773SJohn Baldwin 598b2e60773SJohn Baldwin static int 599b2e60773SJohn Baldwin ktls_create_session(struct socket *so, struct tls_enable *en, 600fe8c78f0SHans Petter Selasky struct ktls_session **tlsp, int direction) 601b2e60773SJohn Baldwin { 602b2e60773SJohn Baldwin struct ktls_session *tls; 603b2e60773SJohn Baldwin int error; 604b2e60773SJohn Baldwin 6057d29eb9aSJohn Baldwin /* Only TLS 1.0 - 1.3 are supported. */ 606b2e60773SJohn Baldwin if (en->tls_vmajor != TLS_MAJOR_VER_ONE) 607b2e60773SJohn Baldwin return (EINVAL); 608b2e60773SJohn Baldwin if (en->tls_vminor < TLS_MINOR_VER_ZERO || 6096554362cSAndrew Gallatin en->tls_vminor > TLS_MINOR_VER_THREE) 610b2e60773SJohn Baldwin return (EINVAL); 611b2e60773SJohn Baldwin 612b2e60773SJohn Baldwin 613b2e60773SJohn Baldwin /* No flags are currently supported. */ 614b2e60773SJohn Baldwin if (en->flags != 0) 615b2e60773SJohn Baldwin return (EINVAL); 616b2e60773SJohn Baldwin 617b2e60773SJohn Baldwin /* Common checks for supported algorithms. */ 618b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 619b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 620b2e60773SJohn Baldwin /* 621b2e60773SJohn Baldwin * auth_algorithm isn't used, but permit GMAC values 622b2e60773SJohn Baldwin * for compatibility. 623b2e60773SJohn Baldwin */ 624b2e60773SJohn Baldwin switch (en->auth_algorithm) { 625b2e60773SJohn Baldwin case 0: 626c0341432SJohn Baldwin #ifdef COMPAT_FREEBSD12 627c0341432SJohn Baldwin /* XXX: Really 13.0-current COMPAT. */ 628b2e60773SJohn Baldwin case CRYPTO_AES_128_NIST_GMAC: 629b2e60773SJohn Baldwin case CRYPTO_AES_192_NIST_GMAC: 630b2e60773SJohn Baldwin case CRYPTO_AES_256_NIST_GMAC: 631c0341432SJohn Baldwin #endif 632b2e60773SJohn Baldwin break; 633b2e60773SJohn Baldwin default: 634b2e60773SJohn Baldwin return (EINVAL); 635b2e60773SJohn Baldwin } 636b2e60773SJohn Baldwin if (en->auth_key_len != 0) 637b2e60773SJohn Baldwin return (EINVAL); 638900a28feSJohn Baldwin switch (en->tls_vminor) { 639900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 640900a28feSJohn Baldwin if (en->iv_len != TLS_AEAD_GCM_LEN) 641b2e60773SJohn Baldwin return (EINVAL); 642b2e60773SJohn Baldwin break; 643900a28feSJohn Baldwin case TLS_MINOR_VER_THREE: 644900a28feSJohn Baldwin if (en->iv_len != TLS_1_3_GCM_IV_LEN) 645900a28feSJohn Baldwin return (EINVAL); 646900a28feSJohn Baldwin break; 647900a28feSJohn Baldwin default: 648900a28feSJohn Baldwin return (EINVAL); 649900a28feSJohn Baldwin } 650900a28feSJohn Baldwin break; 651b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 652b2e60773SJohn Baldwin switch (en->auth_algorithm) { 653b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 654b2e60773SJohn Baldwin break; 655b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 656b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 657900a28feSJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO) 658900a28feSJohn Baldwin return (EINVAL); 659b2e60773SJohn Baldwin break; 660b2e60773SJohn Baldwin default: 661b2e60773SJohn Baldwin return (EINVAL); 662b2e60773SJohn Baldwin } 663b2e60773SJohn Baldwin if (en->auth_key_len == 0) 664b2e60773SJohn Baldwin return (EINVAL); 665900a28feSJohn Baldwin 666900a28feSJohn Baldwin /* 667900a28feSJohn Baldwin * TLS 1.0 requires an implicit IV. TLS 1.1 and 1.2 668900a28feSJohn Baldwin * use explicit IVs. 669900a28feSJohn Baldwin */ 670900a28feSJohn Baldwin switch (en->tls_vminor) { 671900a28feSJohn Baldwin case TLS_MINOR_VER_ZERO: 672900a28feSJohn Baldwin if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN) 673a63752ccSJohn Baldwin return (EINVAL); 674b2e60773SJohn Baldwin break; 675900a28feSJohn Baldwin case TLS_MINOR_VER_ONE: 676900a28feSJohn Baldwin case TLS_MINOR_VER_TWO: 677900a28feSJohn Baldwin /* Ignore any supplied IV. */ 678900a28feSJohn Baldwin en->iv_len = 0; 679900a28feSJohn Baldwin break; 680900a28feSJohn Baldwin default: 681900a28feSJohn Baldwin return (EINVAL); 682900a28feSJohn Baldwin } 683900a28feSJohn Baldwin break; 6849c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 6859c64fc40SJohn Baldwin if (en->auth_algorithm != 0 || en->auth_key_len != 0) 6869c64fc40SJohn Baldwin return (EINVAL); 6879c64fc40SJohn Baldwin if (en->tls_vminor != TLS_MINOR_VER_TWO && 6889c64fc40SJohn Baldwin en->tls_vminor != TLS_MINOR_VER_THREE) 6899c64fc40SJohn Baldwin return (EINVAL); 6909c64fc40SJohn Baldwin if (en->iv_len != TLS_CHACHA20_IV_LEN) 6919c64fc40SJohn Baldwin return (EINVAL); 6929c64fc40SJohn Baldwin break; 693b2e60773SJohn Baldwin default: 694b2e60773SJohn Baldwin return (EINVAL); 695b2e60773SJohn Baldwin } 696b2e60773SJohn Baldwin 697a72ee355SJohn Baldwin error = ktls_start_kthreads(); 698a72ee355SJohn Baldwin if (error != 0) 699a72ee355SJohn Baldwin return (error); 700a72ee355SJohn Baldwin 701b2e60773SJohn Baldwin tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 702b2e60773SJohn Baldwin 703b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 704b2e60773SJohn Baldwin 705b2e60773SJohn Baldwin refcount_init(&tls->refcount, 1); 706c0e4090eSAndrew Gallatin if (direction == KTLS_RX) { 707fe8c78f0SHans Petter Selasky TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_receive_tag, tls); 708c0e4090eSAndrew Gallatin } else { 709b2e60773SJohn Baldwin TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls); 710c0e4090eSAndrew Gallatin tls->inp = so->so_pcb; 711c0e4090eSAndrew Gallatin in_pcbref(tls->inp); 712c0e4090eSAndrew Gallatin tls->tx = true; 713c0e4090eSAndrew Gallatin } 714b2e60773SJohn Baldwin 715b2e60773SJohn Baldwin tls->wq_index = ktls_get_cpu(so); 716b2e60773SJohn Baldwin 717b2e60773SJohn Baldwin tls->params.cipher_algorithm = en->cipher_algorithm; 718b2e60773SJohn Baldwin tls->params.auth_algorithm = en->auth_algorithm; 719b2e60773SJohn Baldwin tls->params.tls_vmajor = en->tls_vmajor; 720b2e60773SJohn Baldwin tls->params.tls_vminor = en->tls_vminor; 721b2e60773SJohn Baldwin tls->params.flags = en->flags; 722b2e60773SJohn Baldwin tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen); 723b2e60773SJohn Baldwin 724b2e60773SJohn Baldwin /* Set the header and trailer lengths. */ 725b2e60773SJohn Baldwin tls->params.tls_hlen = sizeof(struct tls_record_layer); 726b2e60773SJohn Baldwin switch (en->cipher_algorithm) { 727b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 7286554362cSAndrew Gallatin /* 7296554362cSAndrew Gallatin * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte 7306554362cSAndrew Gallatin * nonce. TLS 1.3 uses a 12 byte implicit IV. 7316554362cSAndrew Gallatin */ 7326554362cSAndrew Gallatin if (en->tls_vminor < TLS_MINOR_VER_THREE) 7336554362cSAndrew Gallatin tls->params.tls_hlen += sizeof(uint64_t); 734b2e60773SJohn Baldwin tls->params.tls_tlen = AES_GMAC_HASH_LEN; 735b2e60773SJohn Baldwin tls->params.tls_bs = 1; 736b2e60773SJohn Baldwin break; 737b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 738b2e60773SJohn Baldwin switch (en->auth_algorithm) { 739b2e60773SJohn Baldwin case CRYPTO_SHA1_HMAC: 740b2e60773SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_ZERO) { 741b2e60773SJohn Baldwin /* Implicit IV, no nonce. */ 7429f03d2c0SJohn Baldwin tls->sequential_records = true; 7439f03d2c0SJohn Baldwin tls->next_seqno = be64dec(en->rec_seq); 7449f03d2c0SJohn Baldwin STAILQ_INIT(&tls->pending_records); 745b2e60773SJohn Baldwin } else { 746b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 747b2e60773SJohn Baldwin } 748b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 749b2e60773SJohn Baldwin SHA1_HASH_LEN; 750b2e60773SJohn Baldwin break; 751b2e60773SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 752b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 753b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 754b2e60773SJohn Baldwin SHA2_256_HASH_LEN; 755b2e60773SJohn Baldwin break; 756b2e60773SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 757b2e60773SJohn Baldwin tls->params.tls_hlen += AES_BLOCK_LEN; 758b2e60773SJohn Baldwin tls->params.tls_tlen = AES_BLOCK_LEN + 759b2e60773SJohn Baldwin SHA2_384_HASH_LEN; 760b2e60773SJohn Baldwin break; 761b2e60773SJohn Baldwin default: 762b2e60773SJohn Baldwin panic("invalid hmac"); 763b2e60773SJohn Baldwin } 764b2e60773SJohn Baldwin tls->params.tls_bs = AES_BLOCK_LEN; 765b2e60773SJohn Baldwin break; 7669c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 7679c64fc40SJohn Baldwin /* 7689c64fc40SJohn Baldwin * Chacha20 uses a 12 byte implicit IV. 7699c64fc40SJohn Baldwin */ 7709c64fc40SJohn Baldwin tls->params.tls_tlen = POLY1305_HASH_LEN; 7719c64fc40SJohn Baldwin tls->params.tls_bs = 1; 7729c64fc40SJohn Baldwin break; 773b2e60773SJohn Baldwin default: 774b2e60773SJohn Baldwin panic("invalid cipher"); 775b2e60773SJohn Baldwin } 776b2e60773SJohn Baldwin 7779c64fc40SJohn Baldwin /* 7789c64fc40SJohn Baldwin * TLS 1.3 includes optional padding which we do not support, 7799c64fc40SJohn Baldwin * and also puts the "real" record type at the end of the 7809c64fc40SJohn Baldwin * encrypted data. 7819c64fc40SJohn Baldwin */ 7829c64fc40SJohn Baldwin if (en->tls_vminor == TLS_MINOR_VER_THREE) 7839c64fc40SJohn Baldwin tls->params.tls_tlen += sizeof(uint8_t); 7849c64fc40SJohn Baldwin 785b2e60773SJohn Baldwin KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN, 786b2e60773SJohn Baldwin ("TLS header length too long: %d", tls->params.tls_hlen)); 787b2e60773SJohn Baldwin KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN, 788b2e60773SJohn Baldwin ("TLS trailer length too long: %d", tls->params.tls_tlen)); 789b2e60773SJohn Baldwin 790b2e60773SJohn Baldwin if (en->auth_key_len != 0) { 791b2e60773SJohn Baldwin tls->params.auth_key_len = en->auth_key_len; 792b2e60773SJohn Baldwin tls->params.auth_key = malloc(en->auth_key_len, M_KTLS, 793b2e60773SJohn Baldwin M_WAITOK); 79485df11a1SRichard Scheffenegger bcopy(en->auth_key, tls->params.auth_key, en->auth_key_len); 795b2e60773SJohn Baldwin } 796b2e60773SJohn Baldwin 797b2e60773SJohn Baldwin tls->params.cipher_key_len = en->cipher_key_len; 798b2e60773SJohn Baldwin tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK); 79985df11a1SRichard Scheffenegger bcopy(en->cipher_key, tls->params.cipher_key, en->cipher_key_len); 800b2e60773SJohn Baldwin 801b2e60773SJohn Baldwin /* 8029c64fc40SJohn Baldwin * This holds the implicit portion of the nonce for AEAD 8039c64fc40SJohn Baldwin * ciphers and the initial implicit IV for TLS 1.0. The 8049c64fc40SJohn Baldwin * explicit portions of the IV are generated in ktls_frame(). 805b2e60773SJohn Baldwin */ 806b2e60773SJohn Baldwin if (en->iv_len != 0) { 807b2e60773SJohn Baldwin tls->params.iv_len = en->iv_len; 80885df11a1SRichard Scheffenegger bcopy(en->iv, tls->params.iv, en->iv_len); 8097d29eb9aSJohn Baldwin 8107d29eb9aSJohn Baldwin /* 8119c64fc40SJohn Baldwin * For TLS 1.2 with GCM, generate an 8-byte nonce as a 8129c64fc40SJohn Baldwin * counter to generate unique explicit IVs. 8137d29eb9aSJohn Baldwin * 8147d29eb9aSJohn Baldwin * Store this counter in the last 8 bytes of the IV 8157d29eb9aSJohn Baldwin * array so that it is 8-byte aligned. 8167d29eb9aSJohn Baldwin */ 8177d29eb9aSJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 8187d29eb9aSJohn Baldwin en->tls_vminor == TLS_MINOR_VER_TWO) 8197d29eb9aSJohn Baldwin arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0); 820b2e60773SJohn Baldwin } 821b2e60773SJohn Baldwin 822b2e60773SJohn Baldwin *tlsp = tls; 823b2e60773SJohn Baldwin return (0); 824b2e60773SJohn Baldwin } 825b2e60773SJohn Baldwin 826b2e60773SJohn Baldwin static struct ktls_session * 827fe8c78f0SHans Petter Selasky ktls_clone_session(struct ktls_session *tls, int direction) 828b2e60773SJohn Baldwin { 829b2e60773SJohn Baldwin struct ktls_session *tls_new; 830b2e60773SJohn Baldwin 831b2e60773SJohn Baldwin tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO); 832b2e60773SJohn Baldwin 833b2e60773SJohn Baldwin counter_u64_add(ktls_offload_active, 1); 834b2e60773SJohn Baldwin 835b2e60773SJohn Baldwin refcount_init(&tls_new->refcount, 1); 836c0e4090eSAndrew Gallatin if (direction == KTLS_RX) { 837fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_receive_tag, 838fe8c78f0SHans Petter Selasky tls_new); 839c0e4090eSAndrew Gallatin } else { 840fe8c78f0SHans Petter Selasky TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_send_tag, 841fe8c78f0SHans Petter Selasky tls_new); 842c0e4090eSAndrew Gallatin tls_new->inp = tls->inp; 843c0e4090eSAndrew Gallatin tls_new->tx = true; 844c0e4090eSAndrew Gallatin in_pcbref(tls_new->inp); 845c0e4090eSAndrew Gallatin } 846b2e60773SJohn Baldwin 847b2e60773SJohn Baldwin /* Copy fields from existing session. */ 848b2e60773SJohn Baldwin tls_new->params = tls->params; 849b2e60773SJohn Baldwin tls_new->wq_index = tls->wq_index; 850b2e60773SJohn Baldwin 851b2e60773SJohn Baldwin /* Deep copy keys. */ 852b2e60773SJohn Baldwin if (tls_new->params.auth_key != NULL) { 853b2e60773SJohn Baldwin tls_new->params.auth_key = malloc(tls->params.auth_key_len, 854b2e60773SJohn Baldwin M_KTLS, M_WAITOK); 855b2e60773SJohn Baldwin memcpy(tls_new->params.auth_key, tls->params.auth_key, 856b2e60773SJohn Baldwin tls->params.auth_key_len); 857b2e60773SJohn Baldwin } 858b2e60773SJohn Baldwin 859b2e60773SJohn Baldwin tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS, 860b2e60773SJohn Baldwin M_WAITOK); 861b2e60773SJohn Baldwin memcpy(tls_new->params.cipher_key, tls->params.cipher_key, 862b2e60773SJohn Baldwin tls->params.cipher_key_len); 863b2e60773SJohn Baldwin 864b2e60773SJohn Baldwin return (tls_new); 865b2e60773SJohn Baldwin } 8669e14430dSJohn Baldwin 8679e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 8689e14430dSJohn Baldwin static int 869f1f93475SJohn Baldwin ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction) 8709e14430dSJohn Baldwin { 8719e14430dSJohn Baldwin struct inpcb *inp; 8729e14430dSJohn Baldwin struct tcpcb *tp; 8739e14430dSJohn Baldwin int error; 8749e14430dSJohn Baldwin 8759e14430dSJohn Baldwin inp = so->so_pcb; 8769e14430dSJohn Baldwin INP_WLOCK(inp); 87753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 8789e14430dSJohn Baldwin INP_WUNLOCK(inp); 8799e14430dSJohn Baldwin return (ECONNRESET); 8809e14430dSJohn Baldwin } 8819e14430dSJohn Baldwin if (inp->inp_socket == NULL) { 8829e14430dSJohn Baldwin INP_WUNLOCK(inp); 8839e14430dSJohn Baldwin return (ECONNRESET); 8849e14430dSJohn Baldwin } 8859e14430dSJohn Baldwin tp = intotcpcb(inp); 8866bcf3c46SJohn Baldwin if (!(tp->t_flags & TF_TOE)) { 8879e14430dSJohn Baldwin INP_WUNLOCK(inp); 8889e14430dSJohn Baldwin return (EOPNOTSUPP); 8899e14430dSJohn Baldwin } 8909e14430dSJohn Baldwin 891f1f93475SJohn Baldwin error = tcp_offload_alloc_tls_session(tp, tls, direction); 8929e14430dSJohn Baldwin INP_WUNLOCK(inp); 8939e14430dSJohn Baldwin if (error == 0) { 8949e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_TOE; 8959e14430dSJohn Baldwin switch (tls->params.cipher_algorithm) { 8969e14430dSJohn Baldwin case CRYPTO_AES_CBC: 8979e14430dSJohn Baldwin counter_u64_add(ktls_toe_cbc, 1); 8989e14430dSJohn Baldwin break; 8999e14430dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 9009e14430dSJohn Baldwin counter_u64_add(ktls_toe_gcm, 1); 9019e14430dSJohn Baldwin break; 9029c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 9039c64fc40SJohn Baldwin counter_u64_add(ktls_toe_chacha20, 1); 9049c64fc40SJohn Baldwin break; 9059e14430dSJohn Baldwin } 9069e14430dSJohn Baldwin } 9079e14430dSJohn Baldwin return (error); 9089e14430dSJohn Baldwin } 9099e14430dSJohn Baldwin #endif 9109e14430dSJohn Baldwin 911b2e60773SJohn Baldwin /* 912b2e60773SJohn Baldwin * Common code used when first enabling ifnet TLS on a connection or 913b2e60773SJohn Baldwin * when allocating a new ifnet TLS session due to a routing change. 914b2e60773SJohn Baldwin * This function allocates a new TLS send tag on whatever interface 915b2e60773SJohn Baldwin * the connection is currently routed over. 916b2e60773SJohn Baldwin */ 917b2e60773SJohn Baldwin static int 918b2e60773SJohn Baldwin ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force, 919b2e60773SJohn Baldwin struct m_snd_tag **mstp) 920b2e60773SJohn Baldwin { 921b2e60773SJohn Baldwin union if_snd_tag_alloc_params params; 922b2e60773SJohn Baldwin struct ifnet *ifp; 923983066f0SAlexander V. Chernikov struct nhop_object *nh; 924b2e60773SJohn Baldwin struct tcpcb *tp; 925b2e60773SJohn Baldwin int error; 926b2e60773SJohn Baldwin 927b2e60773SJohn Baldwin INP_RLOCK(inp); 92853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 929b2e60773SJohn Baldwin INP_RUNLOCK(inp); 930b2e60773SJohn Baldwin return (ECONNRESET); 931b2e60773SJohn Baldwin } 932b2e60773SJohn Baldwin if (inp->inp_socket == NULL) { 933b2e60773SJohn Baldwin INP_RUNLOCK(inp); 934b2e60773SJohn Baldwin return (ECONNRESET); 935b2e60773SJohn Baldwin } 936b2e60773SJohn Baldwin tp = intotcpcb(inp); 937b2e60773SJohn Baldwin 938b2e60773SJohn Baldwin /* 939b2e60773SJohn Baldwin * Check administrative controls on ifnet TLS to determine if 940b2e60773SJohn Baldwin * ifnet TLS should be denied. 941b2e60773SJohn Baldwin * 942b2e60773SJohn Baldwin * - Always permit 'force' requests. 943b2e60773SJohn Baldwin * - ktls_ifnet_permitted == 0: always deny. 944b2e60773SJohn Baldwin */ 945b2e60773SJohn Baldwin if (!force && ktls_ifnet_permitted == 0) { 946b2e60773SJohn Baldwin INP_RUNLOCK(inp); 947b2e60773SJohn Baldwin return (ENXIO); 948b2e60773SJohn Baldwin } 949b2e60773SJohn Baldwin 950b2e60773SJohn Baldwin /* 951b2e60773SJohn Baldwin * XXX: Use the cached route in the inpcb to find the 952b2e60773SJohn Baldwin * interface. This should perhaps instead use 953b2e60773SJohn Baldwin * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only 954b2e60773SJohn Baldwin * enabled after a connection has completed key negotiation in 955b2e60773SJohn Baldwin * userland, the cached route will be present in practice. 956b2e60773SJohn Baldwin */ 957983066f0SAlexander V. Chernikov nh = inp->inp_route.ro_nh; 958983066f0SAlexander V. Chernikov if (nh == NULL) { 959b2e60773SJohn Baldwin INP_RUNLOCK(inp); 960b2e60773SJohn Baldwin return (ENXIO); 961b2e60773SJohn Baldwin } 962983066f0SAlexander V. Chernikov ifp = nh->nh_ifp; 963b2e60773SJohn Baldwin if_ref(ifp); 964b2e60773SJohn Baldwin 965521eac97SJohn Baldwin /* 966521eac97SJohn Baldwin * Allocate a TLS + ratelimit tag if the connection has an 967521eac97SJohn Baldwin * existing pacing rate. 968521eac97SJohn Baldwin */ 969521eac97SJohn Baldwin if (tp->t_pacing_rate != -1 && 97008484627SJustin Hibbits (if_getcapenable(ifp) & IFCAP_TXTLS_RTLMT) != 0) { 971521eac97SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT; 972521eac97SJohn Baldwin params.tls_rate_limit.inp = inp; 973521eac97SJohn Baldwin params.tls_rate_limit.tls = tls; 974521eac97SJohn Baldwin params.tls_rate_limit.max_rate = tp->t_pacing_rate; 975521eac97SJohn Baldwin } else { 976b2e60773SJohn Baldwin params.hdr.type = IF_SND_TAG_TYPE_TLS; 977521eac97SJohn Baldwin params.tls.inp = inp; 978521eac97SJohn Baldwin params.tls.tls = tls; 979521eac97SJohn Baldwin } 980b2e60773SJohn Baldwin params.hdr.flowid = inp->inp_flowid; 981b2e60773SJohn Baldwin params.hdr.flowtype = inp->inp_flowtype; 98298085baeSAndrew Gallatin params.hdr.numa_domain = inp->inp_numa_domain; 983b2e60773SJohn Baldwin INP_RUNLOCK(inp); 984b2e60773SJohn Baldwin 98508484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_MEXTPG) == 0) { 986b2e60773SJohn Baldwin error = EOPNOTSUPP; 987b2e60773SJohn Baldwin goto out; 988b2e60773SJohn Baldwin } 989b2e60773SJohn Baldwin if (inp->inp_vflag & INP_IPV6) { 99008484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXTLS6) == 0) { 991b2e60773SJohn Baldwin error = EOPNOTSUPP; 992b2e60773SJohn Baldwin goto out; 993b2e60773SJohn Baldwin } 994b2e60773SJohn Baldwin } else { 99508484627SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXTLS4) == 0) { 996b2e60773SJohn Baldwin error = EOPNOTSUPP; 997b2e60773SJohn Baldwin goto out; 998b2e60773SJohn Baldwin } 999b2e60773SJohn Baldwin } 100036e0a362SJohn Baldwin error = m_snd_tag_alloc(ifp, ¶ms, mstp); 1001b2e60773SJohn Baldwin out: 1002b2e60773SJohn Baldwin if_rele(ifp); 1003b2e60773SJohn Baldwin return (error); 1004b2e60773SJohn Baldwin } 1005b2e60773SJohn Baldwin 1006fe8c78f0SHans Petter Selasky /* 1007fe8c78f0SHans Petter Selasky * Allocate an initial TLS receive tag for doing HW decryption of TLS 1008fe8c78f0SHans Petter Selasky * data. 1009fe8c78f0SHans Petter Selasky * 1010fe8c78f0SHans Petter Selasky * This function allocates a new TLS receive tag on whatever interface 1011fe8c78f0SHans Petter Selasky * the connection is currently routed over. If the connection ends up 1012fe8c78f0SHans Petter Selasky * using a different interface for receive this will get fixed up via 1013fe8c78f0SHans Petter Selasky * ktls_input_ifp_mismatch as future packets arrive. 1014fe8c78f0SHans Petter Selasky */ 1015b2e60773SJohn Baldwin static int 1016fe8c78f0SHans Petter Selasky ktls_alloc_rcv_tag(struct inpcb *inp, struct ktls_session *tls, 1017fe8c78f0SHans Petter Selasky struct m_snd_tag **mstp) 1018fe8c78f0SHans Petter Selasky { 1019fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 1020fe8c78f0SHans Petter Selasky struct ifnet *ifp; 1021fe8c78f0SHans Petter Selasky struct nhop_object *nh; 1022fe8c78f0SHans Petter Selasky int error; 1023fe8c78f0SHans Petter Selasky 1024fe8c78f0SHans Petter Selasky if (!ktls_ocf_recrypt_supported(tls)) 1025fe8c78f0SHans Petter Selasky return (ENXIO); 1026fe8c78f0SHans Petter Selasky 1027fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 102853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1029fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1030fe8c78f0SHans Petter Selasky return (ECONNRESET); 1031fe8c78f0SHans Petter Selasky } 1032fe8c78f0SHans Petter Selasky if (inp->inp_socket == NULL) { 1033fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1034fe8c78f0SHans Petter Selasky return (ECONNRESET); 1035fe8c78f0SHans Petter Selasky } 1036fe8c78f0SHans Petter Selasky 1037fe8c78f0SHans Petter Selasky /* 1038fe8c78f0SHans Petter Selasky * Check administrative controls on ifnet TLS to determine if 1039fe8c78f0SHans Petter Selasky * ifnet TLS should be denied. 1040fe8c78f0SHans Petter Selasky */ 1041fe8c78f0SHans Petter Selasky if (ktls_ifnet_permitted == 0) { 1042fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1043fe8c78f0SHans Petter Selasky return (ENXIO); 1044fe8c78f0SHans Petter Selasky } 1045fe8c78f0SHans Petter Selasky 1046fe8c78f0SHans Petter Selasky /* 1047fe8c78f0SHans Petter Selasky * XXX: As with ktls_alloc_snd_tag, use the cached route in 1048fe8c78f0SHans Petter Selasky * the inpcb to find the interface. 1049fe8c78f0SHans Petter Selasky */ 1050fe8c78f0SHans Petter Selasky nh = inp->inp_route.ro_nh; 1051fe8c78f0SHans Petter Selasky if (nh == NULL) { 1052fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1053fe8c78f0SHans Petter Selasky return (ENXIO); 1054fe8c78f0SHans Petter Selasky } 1055fe8c78f0SHans Petter Selasky ifp = nh->nh_ifp; 1056fe8c78f0SHans Petter Selasky if_ref(ifp); 1057fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 1058fe8c78f0SHans Petter Selasky 1059fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 1060fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 1061fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 1062fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 1063fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 1064fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 1065fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = 0; 1066fe8c78f0SHans Petter Selasky 1067fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1068fe8c78f0SHans Petter Selasky 1069fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 1070c721694aSNavdeep Parhar if ((if_getcapenable2(ifp) & IFCAP2_BIT(IFCAP2_RXTLS6)) == 0) { 1071fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1072fe8c78f0SHans Petter Selasky goto out; 1073fe8c78f0SHans Petter Selasky } 1074fe8c78f0SHans Petter Selasky } else { 1075c721694aSNavdeep Parhar if ((if_getcapenable2(ifp) & IFCAP2_BIT(IFCAP2_RXTLS4)) == 0) { 1076fe8c78f0SHans Petter Selasky error = EOPNOTSUPP; 1077fe8c78f0SHans Petter Selasky goto out; 1078fe8c78f0SHans Petter Selasky } 1079fe8c78f0SHans Petter Selasky } 1080fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, mstp); 1081fe8c78f0SHans Petter Selasky 1082fe8c78f0SHans Petter Selasky /* 1083fe8c78f0SHans Petter Selasky * If this connection is over a vlan, vlan_snd_tag_alloc 1084fe8c78f0SHans Petter Selasky * rewrites vlan_id with the saved interface. Save the VLAN 1085fe8c78f0SHans Petter Selasky * ID for use in ktls_reset_receive_tag which allocates new 1086fe8c78f0SHans Petter Selasky * receive tags directly from the leaf interface bypassing 1087fe8c78f0SHans Petter Selasky * if_vlan. 1088fe8c78f0SHans Petter Selasky */ 1089fe8c78f0SHans Petter Selasky if (error == 0) 1090fe8c78f0SHans Petter Selasky tls->rx_vlan_id = params.tls_rx.vlan_id; 1091fe8c78f0SHans Petter Selasky out: 1092fe8c78f0SHans Petter Selasky return (error); 1093fe8c78f0SHans Petter Selasky } 1094fe8c78f0SHans Petter Selasky 1095fe8c78f0SHans Petter Selasky static int 1096fe8c78f0SHans Petter Selasky ktls_try_ifnet(struct socket *so, struct ktls_session *tls, int direction, 1097fe8c78f0SHans Petter Selasky bool force) 1098b2e60773SJohn Baldwin { 1099b2e60773SJohn Baldwin struct m_snd_tag *mst; 1100b2e60773SJohn Baldwin int error; 1101b2e60773SJohn Baldwin 1102fe8c78f0SHans Petter Selasky switch (direction) { 1103fe8c78f0SHans Petter Selasky case KTLS_TX: 1104b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst); 1105fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1106fe8c78f0SHans Petter Selasky goto done; 1107fe8c78f0SHans Petter Selasky break; 1108fe8c78f0SHans Petter Selasky case KTLS_RX: 1109fe8c78f0SHans Petter Selasky KASSERT(!force, ("%s: forced receive tag", __func__)); 1110fe8c78f0SHans Petter Selasky error = ktls_alloc_rcv_tag(so->so_pcb, tls, &mst); 1111fe8c78f0SHans Petter Selasky if (__predict_false(error != 0)) 1112fe8c78f0SHans Petter Selasky goto done; 1113fe8c78f0SHans Petter Selasky break; 1114fe8c78f0SHans Petter Selasky default: 1115fe8c78f0SHans Petter Selasky __assert_unreachable(); 1116fe8c78f0SHans Petter Selasky } 1117fe8c78f0SHans Petter Selasky 11189e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_IFNET; 1119b2e60773SJohn Baldwin tls->snd_tag = mst; 1120fe8c78f0SHans Petter Selasky 1121b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1122b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1123b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_cbc, 1); 1124b2e60773SJohn Baldwin break; 1125b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1126b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_gcm, 1); 1127b2e60773SJohn Baldwin break; 11289c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 11299c64fc40SJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, 1); 11309c64fc40SJohn Baldwin break; 1131fe8c78f0SHans Petter Selasky default: 1132fe8c78f0SHans Petter Selasky break; 1133b2e60773SJohn Baldwin } 1134fe8c78f0SHans Petter Selasky done: 1135b2e60773SJohn Baldwin return (error); 1136b2e60773SJohn Baldwin } 1137b2e60773SJohn Baldwin 113896668a81SJohn Baldwin static void 113996668a81SJohn Baldwin ktls_use_sw(struct ktls_session *tls) 1140b2e60773SJohn Baldwin { 11419e14430dSJohn Baldwin tls->mode = TCP_TLS_MODE_SW; 1142b2e60773SJohn Baldwin switch (tls->params.cipher_algorithm) { 1143b2e60773SJohn Baldwin case CRYPTO_AES_CBC: 1144b2e60773SJohn Baldwin counter_u64_add(ktls_sw_cbc, 1); 1145b2e60773SJohn Baldwin break; 1146b2e60773SJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 1147b2e60773SJohn Baldwin counter_u64_add(ktls_sw_gcm, 1); 1148b2e60773SJohn Baldwin break; 11499c64fc40SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 11509c64fc40SJohn Baldwin counter_u64_add(ktls_sw_chacha20, 1); 11519c64fc40SJohn Baldwin break; 1152b2e60773SJohn Baldwin } 115396668a81SJohn Baldwin } 115496668a81SJohn Baldwin 115596668a81SJohn Baldwin static int 1156*5dfca6c3SMark Johnston ktls_try_sw(struct ktls_session *tls, int direction) 115796668a81SJohn Baldwin { 115896668a81SJohn Baldwin int error; 115996668a81SJohn Baldwin 1160*5dfca6c3SMark Johnston error = ktls_ocf_try(tls, direction); 116196668a81SJohn Baldwin if (error) 116296668a81SJohn Baldwin return (error); 116396668a81SJohn Baldwin ktls_use_sw(tls); 1164b2e60773SJohn Baldwin return (0); 1165b2e60773SJohn Baldwin } 1166b2e60773SJohn Baldwin 11673c0e5685SJohn Baldwin /* 11683c0e5685SJohn Baldwin * KTLS RX stores data in the socket buffer as a list of TLS records, 11693c0e5685SJohn Baldwin * where each record is stored as a control message containg the TLS 11703c0e5685SJohn Baldwin * header followed by data mbufs containing the decrypted data. This 11713c0e5685SJohn Baldwin * is different from KTLS TX which always uses an mb_ext_pgs mbuf for 11723c0e5685SJohn Baldwin * both encrypted and decrypted data. TLS records decrypted by a NIC 11733c0e5685SJohn Baldwin * should be queued to the socket buffer as records, but encrypted 11743c0e5685SJohn Baldwin * data which needs to be decrypted by software arrives as a stream of 11753c0e5685SJohn Baldwin * regular mbufs which need to be converted. In addition, there may 11763c0e5685SJohn Baldwin * already be pending encrypted data in the socket buffer when KTLS RX 11773c0e5685SJohn Baldwin * is enabled. 11783c0e5685SJohn Baldwin * 11793c0e5685SJohn Baldwin * To manage not-yet-decrypted data for KTLS RX, the following scheme 11803c0e5685SJohn Baldwin * is used: 11813c0e5685SJohn Baldwin * 11823c0e5685SJohn Baldwin * - A single chain of NOTREADY mbufs is hung off of sb_mtls. 11833c0e5685SJohn Baldwin * 11843c0e5685SJohn Baldwin * - ktls_check_rx checks this chain of mbufs reading the TLS header 11853c0e5685SJohn Baldwin * from the first mbuf. Once all of the data for that TLS record is 11863c0e5685SJohn Baldwin * queued, the socket is queued to a worker thread. 11873c0e5685SJohn Baldwin * 11883c0e5685SJohn Baldwin * - The worker thread calls ktls_decrypt to decrypt TLS records in 11893c0e5685SJohn Baldwin * the TLS chain. Each TLS record is detached from the TLS chain, 11903c0e5685SJohn Baldwin * decrypted, and inserted into the regular socket buffer chain as 11913c0e5685SJohn Baldwin * record starting with a control message holding the TLS header and 11923c0e5685SJohn Baldwin * a chain of mbufs holding the encrypted data. 11933c0e5685SJohn Baldwin */ 11943c0e5685SJohn Baldwin 11953c0e5685SJohn Baldwin static void 11963c0e5685SJohn Baldwin sb_mark_notready(struct sockbuf *sb) 11973c0e5685SJohn Baldwin { 11983c0e5685SJohn Baldwin struct mbuf *m; 11993c0e5685SJohn Baldwin 12003c0e5685SJohn Baldwin m = sb->sb_mb; 12013c0e5685SJohn Baldwin sb->sb_mtls = m; 12023c0e5685SJohn Baldwin sb->sb_mb = NULL; 12033c0e5685SJohn Baldwin sb->sb_mbtail = NULL; 12043c0e5685SJohn Baldwin sb->sb_lastrecord = NULL; 12053c0e5685SJohn Baldwin for (; m != NULL; m = m->m_next) { 12063c0e5685SJohn Baldwin KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL", 12073c0e5685SJohn Baldwin __func__)); 12083c0e5685SJohn Baldwin KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail", 12093c0e5685SJohn Baldwin __func__)); 12103c0e5685SJohn Baldwin KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len", 12113c0e5685SJohn Baldwin __func__)); 12123c0e5685SJohn Baldwin m->m_flags |= M_NOTREADY; 12133c0e5685SJohn Baldwin sb->sb_acc -= m->m_len; 12143c0e5685SJohn Baldwin sb->sb_tlscc += m->m_len; 12153c0e5685SJohn Baldwin sb->sb_mtlstail = m; 12163c0e5685SJohn Baldwin } 12173c0e5685SJohn Baldwin KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc, 12183c0e5685SJohn Baldwin ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc, 12193c0e5685SJohn Baldwin sb->sb_ccc)); 12203c0e5685SJohn Baldwin } 12213c0e5685SJohn Baldwin 1222c57dbec6SJohn Baldwin /* 1223c57dbec6SJohn Baldwin * Return information about the pending TLS data in a socket 1224c57dbec6SJohn Baldwin * buffer. On return, 'seqno' is set to the sequence number 1225c57dbec6SJohn Baldwin * of the next TLS record to be received, 'resid' is set to 1226c57dbec6SJohn Baldwin * the amount of bytes still needed for the last pending 1227c57dbec6SJohn Baldwin * record. The function returns 'false' if the last pending 1228c57dbec6SJohn Baldwin * record contains a partial TLS header. In that case, 'resid' 1229c57dbec6SJohn Baldwin * is the number of bytes needed to complete the TLS header. 1230c57dbec6SJohn Baldwin */ 1231c57dbec6SJohn Baldwin bool 1232c57dbec6SJohn Baldwin ktls_pending_rx_info(struct sockbuf *sb, uint64_t *seqnop, size_t *residp) 1233c57dbec6SJohn Baldwin { 1234c57dbec6SJohn Baldwin struct tls_record_layer hdr; 1235c57dbec6SJohn Baldwin struct mbuf *m; 1236c57dbec6SJohn Baldwin uint64_t seqno; 1237c57dbec6SJohn Baldwin size_t resid; 1238c57dbec6SJohn Baldwin u_int offset, record_len; 1239c57dbec6SJohn Baldwin 1240c57dbec6SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 1241c57dbec6SJohn Baldwin MPASS(sb->sb_flags & SB_TLS_RX); 1242c57dbec6SJohn Baldwin seqno = sb->sb_tls_seqno; 1243c57dbec6SJohn Baldwin resid = sb->sb_tlscc; 1244c57dbec6SJohn Baldwin m = sb->sb_mtls; 1245c57dbec6SJohn Baldwin offset = 0; 1246c57dbec6SJohn Baldwin 1247c57dbec6SJohn Baldwin if (resid == 0) { 1248c57dbec6SJohn Baldwin *seqnop = seqno; 1249c57dbec6SJohn Baldwin *residp = 0; 1250c57dbec6SJohn Baldwin return (true); 1251c57dbec6SJohn Baldwin } 1252c57dbec6SJohn Baldwin 1253c57dbec6SJohn Baldwin for (;;) { 1254c57dbec6SJohn Baldwin seqno++; 1255c57dbec6SJohn Baldwin 1256c57dbec6SJohn Baldwin if (resid < sizeof(hdr)) { 1257c57dbec6SJohn Baldwin *seqnop = seqno; 1258c57dbec6SJohn Baldwin *residp = sizeof(hdr) - resid; 1259c57dbec6SJohn Baldwin return (false); 1260c57dbec6SJohn Baldwin } 1261c57dbec6SJohn Baldwin 1262c57dbec6SJohn Baldwin m_copydata(m, offset, sizeof(hdr), (void *)&hdr); 1263c57dbec6SJohn Baldwin 1264c57dbec6SJohn Baldwin record_len = sizeof(hdr) + ntohs(hdr.tls_length); 1265c57dbec6SJohn Baldwin if (resid <= record_len) { 1266c57dbec6SJohn Baldwin *seqnop = seqno; 1267c57dbec6SJohn Baldwin *residp = record_len - resid; 1268c57dbec6SJohn Baldwin return (true); 1269c57dbec6SJohn Baldwin } 1270c57dbec6SJohn Baldwin resid -= record_len; 1271c57dbec6SJohn Baldwin 1272c57dbec6SJohn Baldwin while (record_len != 0) { 1273c57dbec6SJohn Baldwin if (m->m_len - offset > record_len) { 1274c57dbec6SJohn Baldwin offset += record_len; 1275c57dbec6SJohn Baldwin break; 1276c57dbec6SJohn Baldwin } 1277c57dbec6SJohn Baldwin 1278c57dbec6SJohn Baldwin record_len -= (m->m_len - offset); 1279c57dbec6SJohn Baldwin offset = 0; 1280c57dbec6SJohn Baldwin m = m->m_next; 1281c57dbec6SJohn Baldwin } 1282c57dbec6SJohn Baldwin } 1283c57dbec6SJohn Baldwin } 1284c57dbec6SJohn Baldwin 1285b2e60773SJohn Baldwin int 1286f1f93475SJohn Baldwin ktls_enable_rx(struct socket *so, struct tls_enable *en) 1287f1f93475SJohn Baldwin { 1288f1f93475SJohn Baldwin struct ktls_session *tls; 1289f1f93475SJohn Baldwin int error; 1290f1f93475SJohn Baldwin 1291f1f93475SJohn Baldwin if (!ktls_offload_enable) 1292f1f93475SJohn Baldwin return (ENOTSUP); 1293f1f93475SJohn Baldwin 1294f1f93475SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1295f1f93475SJohn Baldwin 1296f1f93475SJohn Baldwin /* 1297f1f93475SJohn Baldwin * This should always be true since only the TCP socket option 1298f1f93475SJohn Baldwin * invokes this function. 1299f1f93475SJohn Baldwin */ 1300f1f93475SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1301f1f93475SJohn Baldwin return (EINVAL); 1302f1f93475SJohn Baldwin 1303f1f93475SJohn Baldwin /* 1304f1f93475SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1305f1f93475SJohn Baldwin * this to support rekeying in the future. 1306f1f93475SJohn Baldwin */ 1307f1f93475SJohn Baldwin if (so->so_rcv.sb_tls_info != NULL) 1308f1f93475SJohn Baldwin return (EALREADY); 1309f1f93475SJohn Baldwin 1310f1f93475SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1311f1f93475SJohn Baldwin return (ENOTSUP); 1312f1f93475SJohn Baldwin 1313fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_RX); 1314f1f93475SJohn Baldwin if (error) 1315f1f93475SJohn Baldwin return (error); 1316f1f93475SJohn Baldwin 1317*5dfca6c3SMark Johnston error = ktls_ocf_try(tls, KTLS_RX); 1318f1f93475SJohn Baldwin if (error) { 1319d01db2b8SJohn Baldwin ktls_free(tls); 1320f1f93475SJohn Baldwin return (error); 1321f1f93475SJohn Baldwin } 1322f1f93475SJohn Baldwin 1323163cdf6aSMark Johnston /* 1324163cdf6aSMark Johnston * Serialize with soreceive_generic() and make sure that we're not 1325163cdf6aSMark Johnston * operating on a listening socket. 1326163cdf6aSMark Johnston */ 1327163cdf6aSMark Johnston error = SOCK_IO_RECV_LOCK(so, SBL_WAIT); 1328163cdf6aSMark Johnston if (error) { 1329163cdf6aSMark Johnston ktls_free(tls); 1330163cdf6aSMark Johnston return (error); 1331163cdf6aSMark Johnston } 1332163cdf6aSMark Johnston 1333f1f93475SJohn Baldwin /* Mark the socket as using TLS offload. */ 1334b4b33821SMark Johnston SOCK_RECVBUF_LOCK(so); 1335163cdf6aSMark Johnston if (__predict_false(so->so_rcv.sb_tls_info != NULL)) { 1336b4b33821SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1337163cdf6aSMark Johnston SOCK_IO_RECV_UNLOCK(so); 1338b4b33821SMark Johnston ktls_free(tls); 1339163cdf6aSMark Johnston return (EALREADY); 1340b4b33821SMark Johnston } 13413c0e5685SJohn Baldwin so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq); 1342f1f93475SJohn Baldwin so->so_rcv.sb_tls_info = tls; 13433c0e5685SJohn Baldwin so->so_rcv.sb_flags |= SB_TLS_RX; 13443c0e5685SJohn Baldwin 13453c0e5685SJohn Baldwin /* Mark existing data as not ready until it can be decrypted. */ 13463c0e5685SJohn Baldwin sb_mark_notready(&so->so_rcv); 13473c0e5685SJohn Baldwin ktls_check_rx(&so->so_rcv); 1348b4b33821SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1349163cdf6aSMark Johnston SOCK_IO_RECV_UNLOCK(so); 1350f1f93475SJohn Baldwin 1351fe8c78f0SHans Petter Selasky /* Prefer TOE -> ifnet TLS -> software TLS. */ 1352d958bc79SJohn Baldwin #ifdef TCP_OFFLOAD 1353d958bc79SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_RX); 1354d958bc79SJohn Baldwin if (error) 1355d958bc79SJohn Baldwin #endif 1356fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_RX, false); 1357fe8c78f0SHans Petter Selasky if (error) 1358d958bc79SJohn Baldwin ktls_use_sw(tls); 1359d958bc79SJohn Baldwin 1360f1f93475SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1361f1f93475SJohn Baldwin 1362f1f93475SJohn Baldwin return (0); 1363f1f93475SJohn Baldwin } 1364f1f93475SJohn Baldwin 1365f1f93475SJohn Baldwin int 1366b2e60773SJohn Baldwin ktls_enable_tx(struct socket *so, struct tls_enable *en) 1367b2e60773SJohn Baldwin { 1368b2e60773SJohn Baldwin struct ktls_session *tls; 1369521eac97SJohn Baldwin struct inpcb *inp; 1370c0e4090eSAndrew Gallatin struct tcpcb *tp; 1371b2e60773SJohn Baldwin int error; 1372b2e60773SJohn Baldwin 1373b2e60773SJohn Baldwin if (!ktls_offload_enable) 1374b2e60773SJohn Baldwin return (ENOTSUP); 1375b2e60773SJohn Baldwin 1376b2e60773SJohn Baldwin counter_u64_add(ktls_offload_enable_calls, 1); 1377b2e60773SJohn Baldwin 1378b2e60773SJohn Baldwin /* 1379b2e60773SJohn Baldwin * This should always be true since only the TCP socket option 1380b2e60773SJohn Baldwin * invokes this function. 1381b2e60773SJohn Baldwin */ 1382b2e60773SJohn Baldwin if (so->so_proto->pr_protocol != IPPROTO_TCP) 1383b2e60773SJohn Baldwin return (EINVAL); 1384b2e60773SJohn Baldwin 1385b2e60773SJohn Baldwin /* 1386b2e60773SJohn Baldwin * XXX: Don't overwrite existing sessions. We should permit 1387b2e60773SJohn Baldwin * this to support rekeying in the future. 1388b2e60773SJohn Baldwin */ 1389b2e60773SJohn Baldwin if (so->so_snd.sb_tls_info != NULL) 1390b2e60773SJohn Baldwin return (EALREADY); 1391b2e60773SJohn Baldwin 1392b2e60773SJohn Baldwin if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable) 1393b2e60773SJohn Baldwin return (ENOTSUP); 1394b2e60773SJohn Baldwin 1395b2e60773SJohn Baldwin /* TLS requires ext pgs */ 1396b2e60773SJohn Baldwin if (mb_use_ext_pgs == 0) 1397b2e60773SJohn Baldwin return (ENXIO); 1398b2e60773SJohn Baldwin 1399fe8c78f0SHans Petter Selasky error = ktls_create_session(so, en, &tls, KTLS_TX); 1400b2e60773SJohn Baldwin if (error) 1401b2e60773SJohn Baldwin return (error); 1402b2e60773SJohn Baldwin 14039e14430dSJohn Baldwin /* Prefer TOE -> ifnet TLS -> software TLS. */ 14049e14430dSJohn Baldwin #ifdef TCP_OFFLOAD 1405f1f93475SJohn Baldwin error = ktls_try_toe(so, tls, KTLS_TX); 14069e14430dSJohn Baldwin if (error) 14079e14430dSJohn Baldwin #endif 1408fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls, KTLS_TX, false); 1409b2e60773SJohn Baldwin if (error) 1410*5dfca6c3SMark Johnston error = ktls_try_sw(tls, KTLS_TX); 1411b2e60773SJohn Baldwin 1412b2e60773SJohn Baldwin if (error) { 1413d01db2b8SJohn Baldwin ktls_free(tls); 1414b2e60773SJohn Baldwin return (error); 1415b2e60773SJohn Baldwin } 1416b2e60773SJohn Baldwin 1417b4b33821SMark Johnston /* 1418b4b33821SMark Johnston * Serialize with sosend_generic() and make sure that we're not 1419b4b33821SMark Johnston * operating on a listening socket. 1420b4b33821SMark Johnston */ 1421f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1422b2e60773SJohn Baldwin if (error) { 1423d01db2b8SJohn Baldwin ktls_free(tls); 1424b2e60773SJohn Baldwin return (error); 1425b2e60773SJohn Baldwin } 1426b2e60773SJohn Baldwin 1427521eac97SJohn Baldwin /* 1428521eac97SJohn Baldwin * Write lock the INP when setting sb_tls_info so that 1429521eac97SJohn Baldwin * routines in tcp_ratelimit.c can read sb_tls_info while 1430521eac97SJohn Baldwin * holding the INP lock. 1431521eac97SJohn Baldwin */ 1432521eac97SJohn Baldwin inp = so->so_pcb; 1433521eac97SJohn Baldwin INP_WLOCK(inp); 1434b4b33821SMark Johnston SOCK_SENDBUF_LOCK(so); 1435163cdf6aSMark Johnston if (__predict_false(so->so_snd.sb_tls_info != NULL)) { 1436163cdf6aSMark Johnston SOCK_SENDBUF_UNLOCK(so); 1437163cdf6aSMark Johnston INP_WUNLOCK(inp); 1438163cdf6aSMark Johnston SOCK_IO_SEND_UNLOCK(so); 1439163cdf6aSMark Johnston ktls_free(tls); 1440163cdf6aSMark Johnston return (EALREADY); 1441163cdf6aSMark Johnston } 1442ec1db6e1SJohn Baldwin so->so_snd.sb_tls_seqno = be64dec(en->rec_seq); 1443b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls; 1444c0e4090eSAndrew Gallatin if (tls->mode != TCP_TLS_MODE_SW) { 1445c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 1446c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 0); 1447c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 1; 1448c0e4090eSAndrew Gallatin if (tp->t_fb->tfb_hwtls_change != NULL) 1449c0e4090eSAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 1); 1450c0e4090eSAndrew Gallatin } 1451b4b33821SMark Johnston SOCK_SENDBUF_UNLOCK(so); 1452521eac97SJohn Baldwin INP_WUNLOCK(inp); 1453f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1454b2e60773SJohn Baldwin 1455b2e60773SJohn Baldwin counter_u64_add(ktls_offload_total, 1); 1456b2e60773SJohn Baldwin 1457b2e60773SJohn Baldwin return (0); 1458b2e60773SJohn Baldwin } 1459b2e60773SJohn Baldwin 1460b2e60773SJohn Baldwin int 1461bf256782SMark Johnston ktls_get_rx_mode(struct socket *so, int *modep) 1462f1f93475SJohn Baldwin { 1463f1f93475SJohn Baldwin struct ktls_session *tls; 1464a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1465f1f93475SJohn Baldwin 14666685e259SMichael Tuexen if (SOLISTENING(so)) 14676685e259SMichael Tuexen return (EINVAL); 1468f1f93475SJohn Baldwin inp = so->so_pcb; 1469f1f93475SJohn Baldwin INP_WLOCK_ASSERT(inp); 1470bf256782SMark Johnston SOCK_RECVBUF_LOCK(so); 1471f1f93475SJohn Baldwin tls = so->so_rcv.sb_tls_info; 1472f1f93475SJohn Baldwin if (tls == NULL) 1473bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1474f1f93475SJohn Baldwin else 1475bf256782SMark Johnston *modep = tls->mode; 1476bf256782SMark Johnston SOCK_RECVBUF_UNLOCK(so); 1477bf256782SMark Johnston return (0); 1478f1f93475SJohn Baldwin } 1479f1f93475SJohn Baldwin 14809e2cce7eSHans Petter Selasky /* 14819e2cce7eSHans Petter Selasky * ktls_get_rx_sequence - get the next TCP- and TLS- sequence number. 14829e2cce7eSHans Petter Selasky * 14839e2cce7eSHans Petter Selasky * This function gets information about the next TCP- and TLS- 14849e2cce7eSHans Petter Selasky * sequence number to be processed by the TLS receive worker 14859e2cce7eSHans Petter Selasky * thread. The information is extracted from the given "inpcb" 14869e2cce7eSHans Petter Selasky * structure. The values are stored in host endian format at the two 14879e2cce7eSHans Petter Selasky * given output pointer locations. The TCP sequence number points to 14889e2cce7eSHans Petter Selasky * the beginning of the TLS header. 14899e2cce7eSHans Petter Selasky * 14909e2cce7eSHans Petter Selasky * This function returns zero on success, else a non-zero error code 14919e2cce7eSHans Petter Selasky * is returned. 14929e2cce7eSHans Petter Selasky */ 14939e2cce7eSHans Petter Selasky int 14949e2cce7eSHans Petter Selasky ktls_get_rx_sequence(struct inpcb *inp, uint32_t *tcpseq, uint64_t *tlsseq) 14959e2cce7eSHans Petter Selasky { 14969e2cce7eSHans Petter Selasky struct socket *so; 14979e2cce7eSHans Petter Selasky struct tcpcb *tp; 14989e2cce7eSHans Petter Selasky 14999e2cce7eSHans Petter Selasky INP_RLOCK(inp); 15009e2cce7eSHans Petter Selasky so = inp->inp_socket; 15019e2cce7eSHans Petter Selasky if (__predict_false(so == NULL)) { 15029e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 15039e2cce7eSHans Petter Selasky return (EINVAL); 15049e2cce7eSHans Petter Selasky } 150553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 15069e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 15079e2cce7eSHans Petter Selasky return (ECONNRESET); 15089e2cce7eSHans Petter Selasky } 15099e2cce7eSHans Petter Selasky 15109e2cce7eSHans Petter Selasky tp = intotcpcb(inp); 15119e2cce7eSHans Petter Selasky MPASS(tp != NULL); 15129e2cce7eSHans Petter Selasky 15139e2cce7eSHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 15149e2cce7eSHans Petter Selasky *tcpseq = tp->rcv_nxt - so->so_rcv.sb_tlscc; 15159e2cce7eSHans Petter Selasky *tlsseq = so->so_rcv.sb_tls_seqno; 15169e2cce7eSHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 15179e2cce7eSHans Petter Selasky 15189e2cce7eSHans Petter Selasky INP_RUNLOCK(inp); 15199e2cce7eSHans Petter Selasky 15209e2cce7eSHans Petter Selasky return (0); 15219e2cce7eSHans Petter Selasky } 15229e2cce7eSHans Petter Selasky 1523f1f93475SJohn Baldwin int 1524bf256782SMark Johnston ktls_get_tx_mode(struct socket *so, int *modep) 1525b2e60773SJohn Baldwin { 1526b2e60773SJohn Baldwin struct ktls_session *tls; 1527a90b85ddSMateusz Guzik struct inpcb *inp __diagused; 1528b2e60773SJohn Baldwin 15296685e259SMichael Tuexen if (SOLISTENING(so)) 15306685e259SMichael Tuexen return (EINVAL); 1531b2e60773SJohn Baldwin inp = so->so_pcb; 1532b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1533bf256782SMark Johnston SOCK_SENDBUF_LOCK(so); 1534b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1535b2e60773SJohn Baldwin if (tls == NULL) 1536bf256782SMark Johnston *modep = TCP_TLS_MODE_NONE; 1537b2e60773SJohn Baldwin else 1538bf256782SMark Johnston *modep = tls->mode; 1539bf256782SMark Johnston SOCK_SENDBUF_UNLOCK(so); 1540bf256782SMark Johnston return (0); 1541b2e60773SJohn Baldwin } 1542b2e60773SJohn Baldwin 1543b2e60773SJohn Baldwin /* 1544b2e60773SJohn Baldwin * Switch between SW and ifnet TLS sessions as requested. 1545b2e60773SJohn Baldwin */ 1546b2e60773SJohn Baldwin int 1547b2e60773SJohn Baldwin ktls_set_tx_mode(struct socket *so, int mode) 1548b2e60773SJohn Baldwin { 1549b2e60773SJohn Baldwin struct ktls_session *tls, *tls_new; 1550b2e60773SJohn Baldwin struct inpcb *inp; 1551c0e4090eSAndrew Gallatin struct tcpcb *tp; 1552b2e60773SJohn Baldwin int error; 1553b2e60773SJohn Baldwin 15546685e259SMichael Tuexen if (SOLISTENING(so)) 15556685e259SMichael Tuexen return (EINVAL); 15569e14430dSJohn Baldwin switch (mode) { 15579e14430dSJohn Baldwin case TCP_TLS_MODE_SW: 15589e14430dSJohn Baldwin case TCP_TLS_MODE_IFNET: 15599e14430dSJohn Baldwin break; 15609e14430dSJohn Baldwin default: 15619e14430dSJohn Baldwin return (EINVAL); 15629e14430dSJohn Baldwin } 1563b2e60773SJohn Baldwin 1564b2e60773SJohn Baldwin inp = so->so_pcb; 1565b2e60773SJohn Baldwin INP_WLOCK_ASSERT(inp); 1566c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 1567c0e4090eSAndrew Gallatin 1568c0e4090eSAndrew Gallatin if (mode == TCP_TLS_MODE_IFNET) { 1569c0e4090eSAndrew Gallatin /* Don't allow enabling ifnet ktls multiple times */ 1570c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit) 1571c0e4090eSAndrew Gallatin return (EALREADY); 1572d24b032bSAndrew Gallatin 1573c0e4090eSAndrew Gallatin /* 1574c0e4090eSAndrew Gallatin * Don't enable ifnet ktls if we disabled it due to an 1575c0e4090eSAndrew Gallatin * excessive retransmission rate 1576c0e4090eSAndrew Gallatin */ 1577c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit_dis) 1578c0e4090eSAndrew Gallatin return (ENXIO); 1579c0e4090eSAndrew Gallatin } 1580c0e4090eSAndrew Gallatin 1581b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1582b2e60773SJohn Baldwin tls = so->so_snd.sb_tls_info; 1583b2e60773SJohn Baldwin if (tls == NULL) { 1584b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1585b2e60773SJohn Baldwin return (0); 1586b2e60773SJohn Baldwin } 1587b2e60773SJohn Baldwin 15889e14430dSJohn Baldwin if (tls->mode == mode) { 1589b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1590b2e60773SJohn Baldwin return (0); 1591b2e60773SJohn Baldwin } 1592b2e60773SJohn Baldwin 1593b2e60773SJohn Baldwin tls = ktls_hold(tls); 1594b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1595b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1596b2e60773SJohn Baldwin 1597fe8c78f0SHans Petter Selasky tls_new = ktls_clone_session(tls, KTLS_TX); 1598b2e60773SJohn Baldwin 1599b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1600fe8c78f0SHans Petter Selasky error = ktls_try_ifnet(so, tls_new, KTLS_TX, true); 1601b2e60773SJohn Baldwin else 1602*5dfca6c3SMark Johnston error = ktls_try_sw(tls_new, KTLS_TX); 1603b2e60773SJohn Baldwin if (error) { 1604b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1605b2e60773SJohn Baldwin ktls_free(tls_new); 1606b2e60773SJohn Baldwin ktls_free(tls); 1607b2e60773SJohn Baldwin INP_WLOCK(inp); 1608b2e60773SJohn Baldwin return (error); 1609b2e60773SJohn Baldwin } 1610b2e60773SJohn Baldwin 1611f94acf52SMark Johnston error = SOCK_IO_SEND_LOCK(so, SBL_WAIT); 1612b2e60773SJohn Baldwin if (error) { 1613b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1614b2e60773SJohn Baldwin ktls_free(tls_new); 1615b2e60773SJohn Baldwin ktls_free(tls); 1616b2e60773SJohn Baldwin INP_WLOCK(inp); 1617b2e60773SJohn Baldwin return (error); 1618b2e60773SJohn Baldwin } 1619b2e60773SJohn Baldwin 1620b2e60773SJohn Baldwin /* 1621b2e60773SJohn Baldwin * If we raced with another session change, keep the existing 1622b2e60773SJohn Baldwin * session. 1623b2e60773SJohn Baldwin */ 1624b2e60773SJohn Baldwin if (tls != so->so_snd.sb_tls_info) { 1625b2e60773SJohn Baldwin counter_u64_add(ktls_switch_failed, 1); 1626f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1627b2e60773SJohn Baldwin ktls_free(tls_new); 1628b2e60773SJohn Baldwin ktls_free(tls); 1629b2e60773SJohn Baldwin INP_WLOCK(inp); 1630b2e60773SJohn Baldwin return (EBUSY); 1631b2e60773SJohn Baldwin } 1632b2e60773SJohn Baldwin 1633cd0525f6SJohn Baldwin INP_WLOCK(inp); 1634b2e60773SJohn Baldwin SOCKBUF_LOCK(&so->so_snd); 1635b2e60773SJohn Baldwin so->so_snd.sb_tls_info = tls_new; 1636c0e4090eSAndrew Gallatin if (tls_new->mode != TCP_TLS_MODE_SW) { 1637c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 0); 1638c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 1; 1639c0e4090eSAndrew Gallatin if (tp->t_fb->tfb_hwtls_change != NULL) 1640c0e4090eSAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 1); 1641c0e4090eSAndrew Gallatin } 1642b2e60773SJohn Baldwin SOCKBUF_UNLOCK(&so->so_snd); 1643f94acf52SMark Johnston SOCK_IO_SEND_UNLOCK(so); 1644b2e60773SJohn Baldwin 1645b2e60773SJohn Baldwin /* 1646b2e60773SJohn Baldwin * Drop two references on 'tls'. The first is for the 1647b2e60773SJohn Baldwin * ktls_hold() above. The second drops the reference from the 1648b2e60773SJohn Baldwin * socket buffer. 1649b2e60773SJohn Baldwin */ 1650b2e60773SJohn Baldwin KASSERT(tls->refcount >= 2, ("too few references on old session")); 1651b2e60773SJohn Baldwin ktls_free(tls); 1652b2e60773SJohn Baldwin ktls_free(tls); 1653b2e60773SJohn Baldwin 1654b2e60773SJohn Baldwin if (mode == TCP_TLS_MODE_IFNET) 1655b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_ifnet, 1); 1656b2e60773SJohn Baldwin else 1657b2e60773SJohn Baldwin counter_u64_add(ktls_switch_to_sw, 1); 1658b2e60773SJohn Baldwin 1659b2e60773SJohn Baldwin return (0); 1660b2e60773SJohn Baldwin } 1661b2e60773SJohn Baldwin 1662b2e60773SJohn Baldwin /* 1663fe8c78f0SHans Petter Selasky * Try to allocate a new TLS receive tag. This task is scheduled when 1664fe8c78f0SHans Petter Selasky * sbappend_ktls_rx detects an input path change. If a new tag is 1665fe8c78f0SHans Petter Selasky * allocated, replace the tag in the TLS session. If a new tag cannot 1666fe8c78f0SHans Petter Selasky * be allocated, let the session fall back to software decryption. 1667fe8c78f0SHans Petter Selasky */ 1668fe8c78f0SHans Petter Selasky static void 1669fe8c78f0SHans Petter Selasky ktls_reset_receive_tag(void *context, int pending) 1670fe8c78f0SHans Petter Selasky { 1671fe8c78f0SHans Petter Selasky union if_snd_tag_alloc_params params; 1672fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1673fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 1674fe8c78f0SHans Petter Selasky struct inpcb *inp; 1675fe8c78f0SHans Petter Selasky struct ifnet *ifp; 1676fe8c78f0SHans Petter Selasky struct socket *so; 1677fe8c78f0SHans Petter Selasky int error; 1678fe8c78f0SHans Petter Selasky 1679fe8c78f0SHans Petter Selasky MPASS(pending == 1); 1680fe8c78f0SHans Petter Selasky 1681fe8c78f0SHans Petter Selasky tls = context; 1682fe8c78f0SHans Petter Selasky so = tls->so; 1683fe8c78f0SHans Petter Selasky inp = so->so_pcb; 1684fe8c78f0SHans Petter Selasky ifp = NULL; 1685fe8c78f0SHans Petter Selasky 1686fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 168753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1688fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1689fe8c78f0SHans Petter Selasky goto out; 1690fe8c78f0SHans Petter Selasky } 1691fe8c78f0SHans Petter Selasky 1692fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 16930e391a31SHans Petter Selasky mst = tls->snd_tag; 1694fe8c78f0SHans Petter Selasky tls->snd_tag = NULL; 16950e391a31SHans Petter Selasky if (mst != NULL) 16960e391a31SHans Petter Selasky m_snd_tag_rele(mst); 1697fe8c78f0SHans Petter Selasky 1698fe8c78f0SHans Petter Selasky ifp = tls->rx_ifp; 1699fe8c78f0SHans Petter Selasky if_ref(ifp); 1700fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1701fe8c78f0SHans Petter Selasky 1702fe8c78f0SHans Petter Selasky params.hdr.type = IF_SND_TAG_TYPE_TLS_RX; 1703fe8c78f0SHans Petter Selasky params.hdr.flowid = inp->inp_flowid; 1704fe8c78f0SHans Petter Selasky params.hdr.flowtype = inp->inp_flowtype; 1705fe8c78f0SHans Petter Selasky params.hdr.numa_domain = inp->inp_numa_domain; 1706fe8c78f0SHans Petter Selasky params.tls_rx.inp = inp; 1707fe8c78f0SHans Petter Selasky params.tls_rx.tls = tls; 1708fe8c78f0SHans Petter Selasky params.tls_rx.vlan_id = tls->rx_vlan_id; 1709fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 1710fe8c78f0SHans Petter Selasky 1711fe8c78f0SHans Petter Selasky if (inp->inp_vflag & INP_IPV6) { 171208484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS6) == 0) 1713fe8c78f0SHans Petter Selasky goto out; 1714fe8c78f0SHans Petter Selasky } else { 171508484627SJustin Hibbits if ((if_getcapenable2(ifp) & IFCAP2_RXTLS4) == 0) 1716fe8c78f0SHans Petter Selasky goto out; 1717fe8c78f0SHans Petter Selasky } 1718fe8c78f0SHans Petter Selasky 1719fe8c78f0SHans Petter Selasky error = m_snd_tag_alloc(ifp, ¶ms, &mst); 1720fe8c78f0SHans Petter Selasky if (error == 0) { 1721fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 1722fe8c78f0SHans Petter Selasky tls->snd_tag = mst; 1723fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 1724fe8c78f0SHans Petter Selasky 1725fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset, 1); 1726fe8c78f0SHans Petter Selasky } else { 1727fe8c78f0SHans Petter Selasky /* 1728fe8c78f0SHans Petter Selasky * Just fall back to software decryption if a tag 1729fe8c78f0SHans Petter Selasky * cannot be allocated leaving the connection intact. 1730fe8c78f0SHans Petter Selasky * If a future input path change switches to another 1731fe8c78f0SHans Petter Selasky * interface this connection will resume ifnet TLS. 1732fe8c78f0SHans Petter Selasky */ 1733fe8c78f0SHans Petter Selasky counter_u64_add(ktls_ifnet_reset_failed, 1); 1734fe8c78f0SHans Petter Selasky } 1735fe8c78f0SHans Petter Selasky 1736fe8c78f0SHans Petter Selasky out: 1737fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1738fe8c78f0SHans Petter Selasky tls->reset_pending = false; 1739fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1740fe8c78f0SHans Petter Selasky 1741fe8c78f0SHans Petter Selasky if (ifp != NULL) 1742fe8c78f0SHans Petter Selasky if_rele(ifp); 17430e1d8481SMartin Matuska CURVNET_SET(so->so_vnet); 1744fe8c78f0SHans Petter Selasky sorele(so); 17450e1d8481SMartin Matuska CURVNET_RESTORE(); 1746fe8c78f0SHans Petter Selasky ktls_free(tls); 1747fe8c78f0SHans Petter Selasky } 1748fe8c78f0SHans Petter Selasky 1749fe8c78f0SHans Petter Selasky /* 1750b2e60773SJohn Baldwin * Try to allocate a new TLS send tag. This task is scheduled when 1751b2e60773SJohn Baldwin * ip_output detects a route change while trying to transmit a packet 1752b2e60773SJohn Baldwin * holding a TLS record. If a new tag is allocated, replace the tag 1753b2e60773SJohn Baldwin * in the TLS session. Subsequent packets on the connection will use 1754b2e60773SJohn Baldwin * the new tag. If a new tag cannot be allocated, drop the 1755b2e60773SJohn Baldwin * connection. 1756b2e60773SJohn Baldwin */ 1757b2e60773SJohn Baldwin static void 1758b2e60773SJohn Baldwin ktls_reset_send_tag(void *context, int pending) 1759b2e60773SJohn Baldwin { 1760b2e60773SJohn Baldwin struct epoch_tracker et; 1761b2e60773SJohn Baldwin struct ktls_session *tls; 1762b2e60773SJohn Baldwin struct m_snd_tag *old, *new; 1763b2e60773SJohn Baldwin struct inpcb *inp; 1764b2e60773SJohn Baldwin struct tcpcb *tp; 1765b2e60773SJohn Baldwin int error; 1766b2e60773SJohn Baldwin 1767b2e60773SJohn Baldwin MPASS(pending == 1); 1768b2e60773SJohn Baldwin 1769b2e60773SJohn Baldwin tls = context; 1770b2e60773SJohn Baldwin inp = tls->inp; 1771b2e60773SJohn Baldwin 1772b2e60773SJohn Baldwin /* 1773b2e60773SJohn Baldwin * Free the old tag first before allocating a new one. 1774b2e60773SJohn Baldwin * ip[6]_output_send() will treat a NULL send tag the same as 1775b2e60773SJohn Baldwin * an ifp mismatch and drop packets until a new tag is 1776b2e60773SJohn Baldwin * allocated. 1777b2e60773SJohn Baldwin * 1778b2e60773SJohn Baldwin * Write-lock the INP when changing tls->snd_tag since 1779b2e60773SJohn Baldwin * ip[6]_output_send() holds a read-lock when reading the 1780b2e60773SJohn Baldwin * pointer. 1781b2e60773SJohn Baldwin */ 1782b2e60773SJohn Baldwin INP_WLOCK(inp); 1783b2e60773SJohn Baldwin old = tls->snd_tag; 1784b2e60773SJohn Baldwin tls->snd_tag = NULL; 1785b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1786b2e60773SJohn Baldwin if (old != NULL) 1787b2e60773SJohn Baldwin m_snd_tag_rele(old); 1788b2e60773SJohn Baldwin 1789b2e60773SJohn Baldwin error = ktls_alloc_snd_tag(inp, tls, true, &new); 1790b2e60773SJohn Baldwin 1791b2e60773SJohn Baldwin if (error == 0) { 1792b2e60773SJohn Baldwin INP_WLOCK(inp); 1793b2e60773SJohn Baldwin tls->snd_tag = new; 1794b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1795b2e60773SJohn Baldwin tls->reset_pending = false; 1796b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1797b2e60773SJohn Baldwin INP_WUNLOCK(inp); 1798b2e60773SJohn Baldwin 1799b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset, 1); 1800b2e60773SJohn Baldwin 1801b2e60773SJohn Baldwin /* 1802b2e60773SJohn Baldwin * XXX: Should we kick tcp_output explicitly now that 1803b2e60773SJohn Baldwin * the send tag is fixed or just rely on timers? 1804b2e60773SJohn Baldwin */ 1805b2e60773SJohn Baldwin } else { 18061a496125SGleb Smirnoff NET_EPOCH_ENTER(et); 1807b2e60773SJohn Baldwin INP_WLOCK(inp); 180853af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1809b2e60773SJohn Baldwin tp = intotcpcb(inp); 18108840ae22SGleb Smirnoff CURVNET_SET(inp->inp_vnet); 1811b2e60773SJohn Baldwin tp = tcp_drop(tp, ECONNABORTED); 18121f69a509SHans Petter Selasky CURVNET_RESTORE(); 1813743516d5SGleb Smirnoff if (tp != NULL) { 1814b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_dropped, 1); 1815743516d5SGleb Smirnoff INP_WUNLOCK(inp); 1816b2e60773SJohn Baldwin } 1817743516d5SGleb Smirnoff } else 1818c0e4090eSAndrew Gallatin INP_WUNLOCK(inp); 18191a496125SGleb Smirnoff NET_EPOCH_EXIT(et); 1820b2e60773SJohn Baldwin 1821b2e60773SJohn Baldwin counter_u64_add(ktls_ifnet_reset_failed, 1); 1822b2e60773SJohn Baldwin 1823b2e60773SJohn Baldwin /* 1824b2e60773SJohn Baldwin * Leave reset_pending true to avoid future tasks while 1825b2e60773SJohn Baldwin * the socket goes away. 1826b2e60773SJohn Baldwin */ 1827b2e60773SJohn Baldwin } 1828b2e60773SJohn Baldwin 1829b2e60773SJohn Baldwin ktls_free(tls); 1830b2e60773SJohn Baldwin } 1831b2e60773SJohn Baldwin 1832fe8c78f0SHans Petter Selasky void 1833fe8c78f0SHans Petter Selasky ktls_input_ifp_mismatch(struct sockbuf *sb, struct ifnet *ifp) 1834fe8c78f0SHans Petter Selasky { 1835fe8c78f0SHans Petter Selasky struct ktls_session *tls; 1836fe8c78f0SHans Petter Selasky struct socket *so; 1837fe8c78f0SHans Petter Selasky 1838fe8c78f0SHans Petter Selasky SOCKBUF_LOCK_ASSERT(sb); 1839fe8c78f0SHans Petter Selasky KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 1840fe8c78f0SHans Petter Selasky __func__, sb)); 1841fe8c78f0SHans Petter Selasky so = __containerof(sb, struct socket, so_rcv); 1842fe8c78f0SHans Petter Selasky 1843fe8c78f0SHans Petter Selasky tls = sb->sb_tls_info; 1844fe8c78f0SHans Petter Selasky if_rele(tls->rx_ifp); 1845fe8c78f0SHans Petter Selasky if_ref(ifp); 1846fe8c78f0SHans Petter Selasky tls->rx_ifp = ifp; 1847fe8c78f0SHans Petter Selasky 1848fe8c78f0SHans Petter Selasky /* 1849fe8c78f0SHans Petter Selasky * See if we should schedule a task to update the receive tag for 1850fe8c78f0SHans Petter Selasky * this session. 1851fe8c78f0SHans Petter Selasky */ 1852fe8c78f0SHans Petter Selasky mtx_pool_lock(mtxpool_sleep, tls); 1853fe8c78f0SHans Petter Selasky if (!tls->reset_pending) { 1854fe8c78f0SHans Petter Selasky (void) ktls_hold(tls); 1855fe8c78f0SHans Petter Selasky soref(so); 1856fe8c78f0SHans Petter Selasky tls->so = so; 1857fe8c78f0SHans Petter Selasky tls->reset_pending = true; 1858fe8c78f0SHans Petter Selasky taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1859fe8c78f0SHans Petter Selasky } 1860fe8c78f0SHans Petter Selasky mtx_pool_unlock(mtxpool_sleep, tls); 1861fe8c78f0SHans Petter Selasky } 1862fe8c78f0SHans Petter Selasky 1863b2e60773SJohn Baldwin int 1864b2e60773SJohn Baldwin ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls) 1865b2e60773SJohn Baldwin { 1866b2e60773SJohn Baldwin 1867b2e60773SJohn Baldwin if (inp == NULL) 1868b2e60773SJohn Baldwin return (ENOBUFS); 1869b2e60773SJohn Baldwin 1870b2e60773SJohn Baldwin INP_LOCK_ASSERT(inp); 1871b2e60773SJohn Baldwin 1872b2e60773SJohn Baldwin /* 1873b2e60773SJohn Baldwin * See if we should schedule a task to update the send tag for 1874b2e60773SJohn Baldwin * this session. 1875b2e60773SJohn Baldwin */ 1876b2e60773SJohn Baldwin mtx_pool_lock(mtxpool_sleep, tls); 1877b2e60773SJohn Baldwin if (!tls->reset_pending) { 1878b2e60773SJohn Baldwin (void) ktls_hold(tls); 1879b2e60773SJohn Baldwin tls->reset_pending = true; 1880b2e60773SJohn Baldwin taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task); 1881b2e60773SJohn Baldwin } 1882b2e60773SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, tls); 1883b2e60773SJohn Baldwin return (ENOBUFS); 1884b2e60773SJohn Baldwin } 1885521eac97SJohn Baldwin 1886521eac97SJohn Baldwin #ifdef RATELIMIT 1887521eac97SJohn Baldwin int 1888521eac97SJohn Baldwin ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate) 1889521eac97SJohn Baldwin { 1890521eac97SJohn Baldwin union if_snd_tag_modify_params params = { 1891521eac97SJohn Baldwin .rate_limit.max_rate = max_pacing_rate, 1892521eac97SJohn Baldwin .rate_limit.flags = M_NOWAIT, 1893521eac97SJohn Baldwin }; 1894521eac97SJohn Baldwin struct m_snd_tag *mst; 1895521eac97SJohn Baldwin 1896521eac97SJohn Baldwin /* Can't get to the inp, but it should be locked. */ 1897521eac97SJohn Baldwin /* INP_LOCK_ASSERT(inp); */ 1898521eac97SJohn Baldwin 1899521eac97SJohn Baldwin MPASS(tls->mode == TCP_TLS_MODE_IFNET); 1900521eac97SJohn Baldwin 1901521eac97SJohn Baldwin if (tls->snd_tag == NULL) { 1902521eac97SJohn Baldwin /* 1903521eac97SJohn Baldwin * Resetting send tag, ignore this change. The 1904521eac97SJohn Baldwin * pending reset may or may not see this updated rate 1905521eac97SJohn Baldwin * in the tcpcb. If it doesn't, we will just lose 1906521eac97SJohn Baldwin * this rate change. 1907521eac97SJohn Baldwin */ 1908521eac97SJohn Baldwin return (0); 1909521eac97SJohn Baldwin } 1910521eac97SJohn Baldwin 1911521eac97SJohn Baldwin mst = tls->snd_tag; 1912f0fca646SHans Petter Selasky 1913f0fca646SHans Petter Selasky MPASS(mst != NULL); 1914f0fca646SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT); 1915f0fca646SHans Petter Selasky 1916c782ea8bSJohn Baldwin return (mst->sw->snd_tag_modify(mst, ¶ms)); 1917521eac97SJohn Baldwin } 1918521eac97SJohn Baldwin #endif 1919b2e60773SJohn Baldwin 1920c0e4090eSAndrew Gallatin static void 1921c0e4090eSAndrew Gallatin ktls_destroy_help(void *context, int pending __unused) 1922c0e4090eSAndrew Gallatin { 1923c0e4090eSAndrew Gallatin ktls_destroy(context); 1924c0e4090eSAndrew Gallatin } 1925c0e4090eSAndrew Gallatin 1926b2e60773SJohn Baldwin void 1927b2e60773SJohn Baldwin ktls_destroy(struct ktls_session *tls) 1928b2e60773SJohn Baldwin { 1929c0e4090eSAndrew Gallatin struct inpcb *inp; 1930c0e4090eSAndrew Gallatin struct tcpcb *tp; 1931c0e4090eSAndrew Gallatin bool wlocked; 1932c0e4090eSAndrew Gallatin 19335920f99dSJohn Baldwin MPASS(tls->refcount == 0); 1934b2e60773SJohn Baldwin 1935c0e4090eSAndrew Gallatin inp = tls->inp; 1936c0e4090eSAndrew Gallatin if (tls->tx) { 1937c0e4090eSAndrew Gallatin wlocked = INP_WLOCKED(inp); 1938c0e4090eSAndrew Gallatin if (!wlocked && !INP_TRY_WLOCK(inp)) { 1939c0e4090eSAndrew Gallatin /* 1940c0e4090eSAndrew Gallatin * rwlocks read locks are anonymous, and there 1941c0e4090eSAndrew Gallatin * is no way to know if our current thread 1942c0e4090eSAndrew Gallatin * holds an rlock on the inp. As a rough 1943c0e4090eSAndrew Gallatin * estimate, check to see if the thread holds 1944c0e4090eSAndrew Gallatin * *any* rlocks at all. If it does not, then we 1945c0e4090eSAndrew Gallatin * know that we don't hold the inp rlock, and 1946c0e4090eSAndrew Gallatin * can safely take the wlock 1947c0e4090eSAndrew Gallatin */ 1948c0e4090eSAndrew Gallatin if (curthread->td_rw_rlocks == 0) { 1949c0e4090eSAndrew Gallatin INP_WLOCK(inp); 1950c0e4090eSAndrew Gallatin } else { 1951c0e4090eSAndrew Gallatin /* 1952c0e4090eSAndrew Gallatin * We might hold the rlock, so let's 1953c0e4090eSAndrew Gallatin * do the destroy in a taskqueue 1954c0e4090eSAndrew Gallatin * context to avoid a potential 1955c0e4090eSAndrew Gallatin * deadlock. This should be very 1956c0e4090eSAndrew Gallatin * rare. 1957c0e4090eSAndrew Gallatin */ 1958c0e4090eSAndrew Gallatin counter_u64_add(ktls_destroy_task, 1); 1959c0e4090eSAndrew Gallatin TASK_INIT(&tls->destroy_task, 0, 1960c0e4090eSAndrew Gallatin ktls_destroy_help, tls); 1961c0e4090eSAndrew Gallatin (void)taskqueue_enqueue(taskqueue_thread, 1962c0e4090eSAndrew Gallatin &tls->destroy_task); 1963c0e4090eSAndrew Gallatin return; 1964c0e4090eSAndrew Gallatin } 1965c0e4090eSAndrew Gallatin } 1966c0e4090eSAndrew Gallatin } 1967c0e4090eSAndrew Gallatin 19689f03d2c0SJohn Baldwin if (tls->sequential_records) { 19699f03d2c0SJohn Baldwin struct mbuf *m, *n; 19709f03d2c0SJohn Baldwin int page_count; 19719f03d2c0SJohn Baldwin 19729f03d2c0SJohn Baldwin STAILQ_FOREACH_SAFE(m, &tls->pending_records, m_epg_stailq, n) { 19739f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 19749f03d2c0SJohn Baldwin while (page_count > 0) { 19759f03d2c0SJohn Baldwin KASSERT(page_count >= m->m_epg_nrdy, 19769f03d2c0SJohn Baldwin ("%s: too few pages", __func__)); 19779f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 19789f03d2c0SJohn Baldwin m = m_free(m); 19799f03d2c0SJohn Baldwin } 19809f03d2c0SJohn Baldwin } 19819f03d2c0SJohn Baldwin } 19825920f99dSJohn Baldwin 19835920f99dSJohn Baldwin counter_u64_add(ktls_offload_active, -1); 19845920f99dSJohn Baldwin switch (tls->mode) { 19855920f99dSJohn Baldwin case TCP_TLS_MODE_SW: 19865920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 19875920f99dSJohn Baldwin case CRYPTO_AES_CBC: 19885920f99dSJohn Baldwin counter_u64_add(ktls_sw_cbc, -1); 19895920f99dSJohn Baldwin break; 19905920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 19915920f99dSJohn Baldwin counter_u64_add(ktls_sw_gcm, -1); 19925920f99dSJohn Baldwin break; 19935920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 19945920f99dSJohn Baldwin counter_u64_add(ktls_sw_chacha20, -1); 19955920f99dSJohn Baldwin break; 19965920f99dSJohn Baldwin } 19975920f99dSJohn Baldwin break; 19985920f99dSJohn Baldwin case TCP_TLS_MODE_IFNET: 19995920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 20005920f99dSJohn Baldwin case CRYPTO_AES_CBC: 20015920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_cbc, -1); 20025920f99dSJohn Baldwin break; 20035920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 20045920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_gcm, -1); 20055920f99dSJohn Baldwin break; 20065920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 20075920f99dSJohn Baldwin counter_u64_add(ktls_ifnet_chacha20, -1); 20085920f99dSJohn Baldwin break; 20095920f99dSJohn Baldwin } 20105920f99dSJohn Baldwin if (tls->snd_tag != NULL) 20115920f99dSJohn Baldwin m_snd_tag_rele(tls->snd_tag); 20125920f99dSJohn Baldwin if (tls->rx_ifp != NULL) 20135920f99dSJohn Baldwin if_rele(tls->rx_ifp); 2014c0e4090eSAndrew Gallatin if (tls->tx) { 2015c0e4090eSAndrew Gallatin INP_WLOCK_ASSERT(inp); 2016c0e4090eSAndrew Gallatin tp = intotcpcb(inp); 2017c0e4090eSAndrew Gallatin MPASS(tp->t_nic_ktls_xmit == 1); 2018c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit = 0; 2019c0e4090eSAndrew Gallatin } 20205920f99dSJohn Baldwin break; 20215920f99dSJohn Baldwin #ifdef TCP_OFFLOAD 20225920f99dSJohn Baldwin case TCP_TLS_MODE_TOE: 20235920f99dSJohn Baldwin switch (tls->params.cipher_algorithm) { 20245920f99dSJohn Baldwin case CRYPTO_AES_CBC: 20255920f99dSJohn Baldwin counter_u64_add(ktls_toe_cbc, -1); 20265920f99dSJohn Baldwin break; 20275920f99dSJohn Baldwin case CRYPTO_AES_NIST_GCM_16: 20285920f99dSJohn Baldwin counter_u64_add(ktls_toe_gcm, -1); 20295920f99dSJohn Baldwin break; 20305920f99dSJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 20315920f99dSJohn Baldwin counter_u64_add(ktls_toe_chacha20, -1); 20325920f99dSJohn Baldwin break; 20335920f99dSJohn Baldwin } 20345920f99dSJohn Baldwin break; 20355920f99dSJohn Baldwin #endif 20365920f99dSJohn Baldwin } 20375920f99dSJohn Baldwin if (tls->ocf_session != NULL) 20385920f99dSJohn Baldwin ktls_ocf_free(tls); 20395920f99dSJohn Baldwin if (tls->params.auth_key != NULL) { 20405920f99dSJohn Baldwin zfree(tls->params.auth_key, M_KTLS); 20415920f99dSJohn Baldwin tls->params.auth_key = NULL; 20425920f99dSJohn Baldwin tls->params.auth_key_len = 0; 20435920f99dSJohn Baldwin } 20445920f99dSJohn Baldwin if (tls->params.cipher_key != NULL) { 20455920f99dSJohn Baldwin zfree(tls->params.cipher_key, M_KTLS); 20465920f99dSJohn Baldwin tls->params.cipher_key = NULL; 20475920f99dSJohn Baldwin tls->params.cipher_key_len = 0; 20485920f99dSJohn Baldwin } 2049c0e4090eSAndrew Gallatin if (tls->tx) { 2050c0e4090eSAndrew Gallatin INP_WLOCK_ASSERT(inp); 2051c0e4090eSAndrew Gallatin if (!in_pcbrele_wlocked(inp) && !wlocked) 2052c0e4090eSAndrew Gallatin INP_WUNLOCK(inp); 2053c0e4090eSAndrew Gallatin } 20545920f99dSJohn Baldwin explicit_bzero(tls->params.iv, sizeof(tls->params.iv)); 20555920f99dSJohn Baldwin 2056b2e60773SJohn Baldwin uma_zfree(ktls_session_zone, tls); 2057b2e60773SJohn Baldwin } 2058b2e60773SJohn Baldwin 2059b2e60773SJohn Baldwin void 2060b2e60773SJohn Baldwin ktls_seq(struct sockbuf *sb, struct mbuf *m) 2061b2e60773SJohn Baldwin { 2062b2e60773SJohn Baldwin 2063b2e60773SJohn Baldwin for (; m != NULL; m = m->m_next) { 20646edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 2065b2e60773SJohn Baldwin ("ktls_seq: mapped mbuf %p", m)); 2066b2e60773SJohn Baldwin 20677b6c99d0SGleb Smirnoff m->m_epg_seqno = sb->sb_tls_seqno; 2068b2e60773SJohn Baldwin sb->sb_tls_seqno++; 2069b2e60773SJohn Baldwin } 2070b2e60773SJohn Baldwin } 2071b2e60773SJohn Baldwin 2072b2e60773SJohn Baldwin /* 2073b2e60773SJohn Baldwin * Add TLS framing (headers and trailers) to a chain of mbufs. Each 2074b2e60773SJohn Baldwin * mbuf in the chain must be an unmapped mbuf. The payload of the 2075b2e60773SJohn Baldwin * mbuf must be populated with the payload of each TLS record. 2076b2e60773SJohn Baldwin * 2077b2e60773SJohn Baldwin * The record_type argument specifies the TLS record type used when 2078b2e60773SJohn Baldwin * populating the TLS header. 2079b2e60773SJohn Baldwin * 2080b2e60773SJohn Baldwin * The enq_count argument on return is set to the number of pages of 2081b2e60773SJohn Baldwin * payload data for this entire chain that need to be encrypted via SW 2082b2e60773SJohn Baldwin * encryption. The returned value should be passed to ktls_enqueue 2083c2a8fd6fSJohn Baldwin * when scheduling encryption of this chain of mbufs. To handle the 2084c2a8fd6fSJohn Baldwin * special case of empty fragments for TLS 1.0 sessions, an empty 2085c2a8fd6fSJohn Baldwin * fragment counts as one page. 2086b2e60773SJohn Baldwin */ 2087f85e1a80SGleb Smirnoff void 2088b2e60773SJohn Baldwin ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt, 2089b2e60773SJohn Baldwin uint8_t record_type) 2090b2e60773SJohn Baldwin { 2091b2e60773SJohn Baldwin struct tls_record_layer *tlshdr; 2092b2e60773SJohn Baldwin struct mbuf *m; 20937d29eb9aSJohn Baldwin uint64_t *noncep; 2094b2e60773SJohn Baldwin uint16_t tls_len; 2095a90b85ddSMateusz Guzik int maxlen __diagused; 2096b2e60773SJohn Baldwin 2097b2e60773SJohn Baldwin maxlen = tls->params.max_frame_len; 2098b2e60773SJohn Baldwin *enq_cnt = 0; 2099b2e60773SJohn Baldwin for (m = top; m != NULL; m = m->m_next) { 2100b2e60773SJohn Baldwin /* 2101c2a8fd6fSJohn Baldwin * All mbufs in the chain should be TLS records whose 2102c2a8fd6fSJohn Baldwin * payload does not exceed the maximum frame length. 2103c2a8fd6fSJohn Baldwin * 21045de79eedSMark Johnston * Empty TLS 1.0 records are permitted when using CBC. 2105b2e60773SJohn Baldwin */ 21065de79eedSMark Johnston KASSERT(m->m_len <= maxlen && m->m_len >= 0 && 21075de79eedSMark Johnston (m->m_len > 0 || ktls_permit_empty_frames(tls)), 21085de79eedSMark Johnston ("ktls_frame: m %p len %d", m, m->m_len)); 2109c2a8fd6fSJohn Baldwin 2110b2e60773SJohn Baldwin /* 2111b2e60773SJohn Baldwin * TLS frames require unmapped mbufs to store session 2112b2e60773SJohn Baldwin * info. 2113b2e60773SJohn Baldwin */ 21146edfd179SGleb Smirnoff KASSERT((m->m_flags & M_EXTPG) != 0, 21155de79eedSMark Johnston ("ktls_frame: mapped mbuf %p (top = %p)", m, top)); 2116b2e60773SJohn Baldwin 2117f85e1a80SGleb Smirnoff tls_len = m->m_len; 2118b2e60773SJohn Baldwin 2119b2e60773SJohn Baldwin /* Save a reference to the session. */ 21207b6c99d0SGleb Smirnoff m->m_epg_tls = ktls_hold(tls); 2121b2e60773SJohn Baldwin 21227b6c99d0SGleb Smirnoff m->m_epg_hdrlen = tls->params.tls_hlen; 21237b6c99d0SGleb Smirnoff m->m_epg_trllen = tls->params.tls_tlen; 2124b2e60773SJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) { 2125b2e60773SJohn Baldwin int bs, delta; 2126b2e60773SJohn Baldwin 2127b2e60773SJohn Baldwin /* 2128b2e60773SJohn Baldwin * AES-CBC pads messages to a multiple of the 2129b2e60773SJohn Baldwin * block size. Note that the padding is 2130b2e60773SJohn Baldwin * applied after the digest and the encryption 2131b2e60773SJohn Baldwin * is done on the "plaintext || mac || padding". 2132b2e60773SJohn Baldwin * At least one byte of padding is always 2133b2e60773SJohn Baldwin * present. 2134b2e60773SJohn Baldwin * 2135b2e60773SJohn Baldwin * Compute the final trailer length assuming 2136b2e60773SJohn Baldwin * at most one block of padding. 213721e3c1fbSJohn Baldwin * tls->params.tls_tlen is the maximum 2138b2e60773SJohn Baldwin * possible trailer length (padding + digest). 2139b2e60773SJohn Baldwin * delta holds the number of excess padding 2140b2e60773SJohn Baldwin * bytes if the maximum were used. Those 2141b2e60773SJohn Baldwin * extra bytes are removed. 2142b2e60773SJohn Baldwin */ 2143b2e60773SJohn Baldwin bs = tls->params.tls_bs; 2144b2e60773SJohn Baldwin delta = (tls_len + tls->params.tls_tlen) & (bs - 1); 21457b6c99d0SGleb Smirnoff m->m_epg_trllen -= delta; 2146b2e60773SJohn Baldwin } 21477b6c99d0SGleb Smirnoff m->m_len += m->m_epg_hdrlen + m->m_epg_trllen; 2148b2e60773SJohn Baldwin 2149b2e60773SJohn Baldwin /* Populate the TLS header. */ 21500c103266SGleb Smirnoff tlshdr = (void *)m->m_epg_hdr; 2151b2e60773SJohn Baldwin tlshdr->tls_vmajor = tls->params.tls_vmajor; 21526554362cSAndrew Gallatin 21536554362cSAndrew Gallatin /* 21546554362cSAndrew Gallatin * TLS 1.3 masquarades as TLS 1.2 with a record type 21556554362cSAndrew Gallatin * of TLS_RLTYPE_APP. 21566554362cSAndrew Gallatin */ 21576554362cSAndrew Gallatin if (tls->params.tls_vminor == TLS_MINOR_VER_THREE && 21586554362cSAndrew Gallatin tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) { 21596554362cSAndrew Gallatin tlshdr->tls_vminor = TLS_MINOR_VER_TWO; 21606554362cSAndrew Gallatin tlshdr->tls_type = TLS_RLTYPE_APP; 21616554362cSAndrew Gallatin /* save the real record type for later */ 21627b6c99d0SGleb Smirnoff m->m_epg_record_type = record_type; 21630c103266SGleb Smirnoff m->m_epg_trail[0] = record_type; 21646554362cSAndrew Gallatin } else { 2165b2e60773SJohn Baldwin tlshdr->tls_vminor = tls->params.tls_vminor; 2166b2e60773SJohn Baldwin tlshdr->tls_type = record_type; 21676554362cSAndrew Gallatin } 2168b2e60773SJohn Baldwin tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr)); 2169b2e60773SJohn Baldwin 2170b2e60773SJohn Baldwin /* 21717d29eb9aSJohn Baldwin * Store nonces / explicit IVs after the end of the 21727d29eb9aSJohn Baldwin * TLS header. 21737d29eb9aSJohn Baldwin * 21747d29eb9aSJohn Baldwin * For GCM with TLS 1.2, an 8 byte nonce is copied 21757d29eb9aSJohn Baldwin * from the end of the IV. The nonce is then 21767d29eb9aSJohn Baldwin * incremented for use by the next record. 21777d29eb9aSJohn Baldwin * 21787d29eb9aSJohn Baldwin * For CBC, a random nonce is inserted for TLS 1.1+. 2179b2e60773SJohn Baldwin */ 21807d29eb9aSJohn Baldwin if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 && 21817d29eb9aSJohn Baldwin tls->params.tls_vminor == TLS_MINOR_VER_TWO) { 21827d29eb9aSJohn Baldwin noncep = (uint64_t *)(tls->params.iv + 8); 21837d29eb9aSJohn Baldwin be64enc(tlshdr + 1, *noncep); 21847d29eb9aSJohn Baldwin (*noncep)++; 21857d29eb9aSJohn Baldwin } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 2186b2e60773SJohn Baldwin tls->params.tls_vminor >= TLS_MINOR_VER_ONE) 2187b2e60773SJohn Baldwin arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0); 2188b2e60773SJohn Baldwin 2189b2e60773SJohn Baldwin /* 2190b2e60773SJohn Baldwin * When using SW encryption, mark the mbuf not ready. 2191b2e60773SJohn Baldwin * It will be marked ready via sbready() after the 2192b2e60773SJohn Baldwin * record has been encrypted. 2193b2e60773SJohn Baldwin * 2194b2e60773SJohn Baldwin * When using ifnet TLS, unencrypted TLS records are 2195b2e60773SJohn Baldwin * sent down the stack to the NIC. 2196b2e60773SJohn Baldwin */ 21979e14430dSJohn Baldwin if (tls->mode == TCP_TLS_MODE_SW) { 2198b2e60773SJohn Baldwin m->m_flags |= M_NOTREADY; 2199c2a8fd6fSJohn Baldwin if (__predict_false(tls_len == 0)) { 2200c2a8fd6fSJohn Baldwin /* TLS 1.0 empty fragment. */ 2201d16cb228SJohn Baldwin m->m_epg_nrdy = 1; 2202c2a8fd6fSJohn Baldwin } else 2203d16cb228SJohn Baldwin m->m_epg_nrdy = m->m_epg_npgs; 2204d16cb228SJohn Baldwin *enq_cnt += m->m_epg_nrdy; 2205b2e60773SJohn Baldwin } 2206b2e60773SJohn Baldwin } 2207b2e60773SJohn Baldwin } 2208b2e60773SJohn Baldwin 22095de79eedSMark Johnston bool 22105de79eedSMark Johnston ktls_permit_empty_frames(struct ktls_session *tls) 22115de79eedSMark Johnston { 22125de79eedSMark Johnston return (tls->params.cipher_algorithm == CRYPTO_AES_CBC && 22135de79eedSMark Johnston tls->params.tls_vminor == TLS_MINOR_VER_ZERO); 22145de79eedSMark Johnston } 22155de79eedSMark Johnston 2216b2e60773SJohn Baldwin void 22173c0e5685SJohn Baldwin ktls_check_rx(struct sockbuf *sb) 22183c0e5685SJohn Baldwin { 22193c0e5685SJohn Baldwin struct tls_record_layer hdr; 22203c0e5685SJohn Baldwin struct ktls_wq *wq; 22213c0e5685SJohn Baldwin struct socket *so; 22223c0e5685SJohn Baldwin bool running; 22233c0e5685SJohn Baldwin 22243c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 22253c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX", 22263c0e5685SJohn Baldwin __func__, sb)); 22273c0e5685SJohn Baldwin so = __containerof(sb, struct socket, so_rcv); 22283c0e5685SJohn Baldwin 22293c0e5685SJohn Baldwin if (sb->sb_flags & SB_TLS_RX_RUNNING) 22303c0e5685SJohn Baldwin return; 22313c0e5685SJohn Baldwin 22323c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 22333c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr)) { 22343c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0) 22353c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 22363c0e5685SJohn Baldwin return; 22373c0e5685SJohn Baldwin } 22383c0e5685SJohn Baldwin 22393c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr); 22403c0e5685SJohn Baldwin 22413c0e5685SJohn Baldwin /* Is the entire record queued? */ 22423c0e5685SJohn Baldwin if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) { 22433c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0) 22443c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 22453c0e5685SJohn Baldwin return; 22463c0e5685SJohn Baldwin } 22473c0e5685SJohn Baldwin 22483c0e5685SJohn Baldwin sb->sb_flags |= SB_TLS_RX_RUNNING; 22493c0e5685SJohn Baldwin 22503c0e5685SJohn Baldwin soref(so); 22513c0e5685SJohn Baldwin wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index]; 22523c0e5685SJohn Baldwin mtx_lock(&wq->mtx); 22533c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list); 22543c0e5685SJohn Baldwin running = wq->running; 22553c0e5685SJohn Baldwin mtx_unlock(&wq->mtx); 22563c0e5685SJohn Baldwin if (!running) 22573c0e5685SJohn Baldwin wakeup(wq); 22583c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, 1); 22593c0e5685SJohn Baldwin } 22603c0e5685SJohn Baldwin 22613c0e5685SJohn Baldwin static struct mbuf * 22623c0e5685SJohn Baldwin ktls_detach_record(struct sockbuf *sb, int len) 22633c0e5685SJohn Baldwin { 22643c0e5685SJohn Baldwin struct mbuf *m, *n, *top; 22653c0e5685SJohn Baldwin int remain; 22663c0e5685SJohn Baldwin 22673c0e5685SJohn Baldwin SOCKBUF_LOCK_ASSERT(sb); 22683c0e5685SJohn Baldwin MPASS(len <= sb->sb_tlscc); 22693c0e5685SJohn Baldwin 22703c0e5685SJohn Baldwin /* 22713c0e5685SJohn Baldwin * If TLS chain is the exact size of the record, 22723c0e5685SJohn Baldwin * just grab the whole record. 22733c0e5685SJohn Baldwin */ 22743c0e5685SJohn Baldwin top = sb->sb_mtls; 22753c0e5685SJohn Baldwin if (sb->sb_tlscc == len) { 22763c0e5685SJohn Baldwin sb->sb_mtls = NULL; 22773c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 22783c0e5685SJohn Baldwin goto out; 22793c0e5685SJohn Baldwin } 22803c0e5685SJohn Baldwin 22813c0e5685SJohn Baldwin /* 22823c0e5685SJohn Baldwin * While it would be nice to use m_split() here, we need 22833c0e5685SJohn Baldwin * to know exactly what m_split() allocates to update the 22843c0e5685SJohn Baldwin * accounting, so do it inline instead. 22853c0e5685SJohn Baldwin */ 22863c0e5685SJohn Baldwin remain = len; 22873c0e5685SJohn Baldwin for (m = top; remain > m->m_len; m = m->m_next) 22883c0e5685SJohn Baldwin remain -= m->m_len; 22893c0e5685SJohn Baldwin 22903c0e5685SJohn Baldwin /* Easy case: don't have to split 'm'. */ 22913c0e5685SJohn Baldwin if (remain == m->m_len) { 22923c0e5685SJohn Baldwin sb->sb_mtls = m->m_next; 22933c0e5685SJohn Baldwin if (sb->sb_mtls == NULL) 22943c0e5685SJohn Baldwin sb->sb_mtlstail = NULL; 22953c0e5685SJohn Baldwin m->m_next = NULL; 22963c0e5685SJohn Baldwin goto out; 22973c0e5685SJohn Baldwin } 22983c0e5685SJohn Baldwin 22993c0e5685SJohn Baldwin /* 23003c0e5685SJohn Baldwin * Need to allocate an mbuf to hold the remainder of 'm'. Try 23013c0e5685SJohn Baldwin * with M_NOWAIT first. 23023c0e5685SJohn Baldwin */ 23033c0e5685SJohn Baldwin n = m_get(M_NOWAIT, MT_DATA); 23043c0e5685SJohn Baldwin if (n == NULL) { 23053c0e5685SJohn Baldwin /* 23063c0e5685SJohn Baldwin * Use M_WAITOK with socket buffer unlocked. If 23073c0e5685SJohn Baldwin * 'sb_mtls' changes while the lock is dropped, return 23083c0e5685SJohn Baldwin * NULL to force the caller to retry. 23093c0e5685SJohn Baldwin */ 23103c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 23113c0e5685SJohn Baldwin 23123c0e5685SJohn Baldwin n = m_get(M_WAITOK, MT_DATA); 23133c0e5685SJohn Baldwin 23143c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 23153c0e5685SJohn Baldwin if (sb->sb_mtls != top) { 23163c0e5685SJohn Baldwin m_free(n); 23173c0e5685SJohn Baldwin return (NULL); 23183c0e5685SJohn Baldwin } 23193c0e5685SJohn Baldwin } 2320fe8c78f0SHans Petter Selasky n->m_flags |= (m->m_flags & (M_NOTREADY | M_DECRYPTED)); 23213c0e5685SJohn Baldwin 23223c0e5685SJohn Baldwin /* Store remainder in 'n'. */ 23233c0e5685SJohn Baldwin n->m_len = m->m_len - remain; 23243c0e5685SJohn Baldwin if (m->m_flags & M_EXT) { 23253c0e5685SJohn Baldwin n->m_data = m->m_data + remain; 23263c0e5685SJohn Baldwin mb_dupcl(n, m); 23273c0e5685SJohn Baldwin } else { 23283c0e5685SJohn Baldwin bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len); 23293c0e5685SJohn Baldwin } 23303c0e5685SJohn Baldwin 23313c0e5685SJohn Baldwin /* Trim 'm' and update accounting. */ 23323c0e5685SJohn Baldwin m->m_len -= n->m_len; 23333c0e5685SJohn Baldwin sb->sb_tlscc -= n->m_len; 23343c0e5685SJohn Baldwin sb->sb_ccc -= n->m_len; 23353c0e5685SJohn Baldwin 23363c0e5685SJohn Baldwin /* Account for 'n'. */ 23373c0e5685SJohn Baldwin sballoc_ktls_rx(sb, n); 23383c0e5685SJohn Baldwin 23393c0e5685SJohn Baldwin /* Insert 'n' into the TLS chain. */ 23403c0e5685SJohn Baldwin sb->sb_mtls = n; 23413c0e5685SJohn Baldwin n->m_next = m->m_next; 23423c0e5685SJohn Baldwin if (sb->sb_mtlstail == m) 23433c0e5685SJohn Baldwin sb->sb_mtlstail = n; 23443c0e5685SJohn Baldwin 23453c0e5685SJohn Baldwin /* Detach the record from the TLS chain. */ 23463c0e5685SJohn Baldwin m->m_next = NULL; 23473c0e5685SJohn Baldwin 23483c0e5685SJohn Baldwin out: 23493c0e5685SJohn Baldwin MPASS(m_length(top, NULL) == len); 23503c0e5685SJohn Baldwin for (m = top; m != NULL; m = m->m_next) 23513c0e5685SJohn Baldwin sbfree_ktls_rx(sb, m); 23523c0e5685SJohn Baldwin sb->sb_tlsdcc = len; 23533c0e5685SJohn Baldwin sb->sb_ccc += len; 23543c0e5685SJohn Baldwin SBCHECK(sb); 23553c0e5685SJohn Baldwin return (top); 23563c0e5685SJohn Baldwin } 23573c0e5685SJohn Baldwin 235805a1d0f5SJohn Baldwin /* 235905a1d0f5SJohn Baldwin * Determine the length of the trailing zero padding and find the real 236005a1d0f5SJohn Baldwin * record type in the byte before the padding. 236105a1d0f5SJohn Baldwin * 236205a1d0f5SJohn Baldwin * Walking the mbuf chain backwards is clumsy, so another option would 236305a1d0f5SJohn Baldwin * be to scan forwards remembering the last non-zero byte before the 236405a1d0f5SJohn Baldwin * trailer. However, it would be expensive to scan the entire record. 236505a1d0f5SJohn Baldwin * Instead, find the last non-zero byte of each mbuf in the chain 236605a1d0f5SJohn Baldwin * keeping track of the relative offset of that nonzero byte. 236705a1d0f5SJohn Baldwin * 236805a1d0f5SJohn Baldwin * trail_len is the size of the MAC/tag on input and is set to the 236905a1d0f5SJohn Baldwin * size of the full trailer including padding and the record type on 237005a1d0f5SJohn Baldwin * return. 237105a1d0f5SJohn Baldwin */ 237205a1d0f5SJohn Baldwin static int 237305a1d0f5SJohn Baldwin tls13_find_record_type(struct ktls_session *tls, struct mbuf *m, int tls_len, 237405a1d0f5SJohn Baldwin int *trailer_len, uint8_t *record_typep) 237505a1d0f5SJohn Baldwin { 237605a1d0f5SJohn Baldwin char *cp; 237705a1d0f5SJohn Baldwin u_int digest_start, last_offset, m_len, offset; 237805a1d0f5SJohn Baldwin uint8_t record_type; 237905a1d0f5SJohn Baldwin 238005a1d0f5SJohn Baldwin digest_start = tls_len - *trailer_len; 238105a1d0f5SJohn Baldwin last_offset = 0; 238205a1d0f5SJohn Baldwin offset = 0; 238305a1d0f5SJohn Baldwin for (; m != NULL && offset < digest_start; 238405a1d0f5SJohn Baldwin offset += m->m_len, m = m->m_next) { 238505a1d0f5SJohn Baldwin /* Don't look for padding in the tag. */ 238605a1d0f5SJohn Baldwin m_len = min(digest_start - offset, m->m_len); 238705a1d0f5SJohn Baldwin cp = mtod(m, char *); 238805a1d0f5SJohn Baldwin 238905a1d0f5SJohn Baldwin /* Find last non-zero byte in this mbuf. */ 239005a1d0f5SJohn Baldwin while (m_len > 0 && cp[m_len - 1] == 0) 239105a1d0f5SJohn Baldwin m_len--; 239205a1d0f5SJohn Baldwin if (m_len > 0) { 239305a1d0f5SJohn Baldwin record_type = cp[m_len - 1]; 239405a1d0f5SJohn Baldwin last_offset = offset + m_len; 239505a1d0f5SJohn Baldwin } 239605a1d0f5SJohn Baldwin } 239705a1d0f5SJohn Baldwin if (last_offset < tls->params.tls_hlen) 239805a1d0f5SJohn Baldwin return (EBADMSG); 239905a1d0f5SJohn Baldwin 240005a1d0f5SJohn Baldwin *record_typep = record_type; 240105a1d0f5SJohn Baldwin *trailer_len = tls_len - last_offset + 1; 240205a1d0f5SJohn Baldwin return (0); 240305a1d0f5SJohn Baldwin } 240405a1d0f5SJohn Baldwin 2405fe8c78f0SHans Petter Selasky /* 2406fe8c78f0SHans Petter Selasky * Check if a mbuf chain is fully decrypted at the given offset and 2407fe8c78f0SHans Petter Selasky * length. Returns KTLS_MBUF_CRYPTO_ST_DECRYPTED if all data is 2408fe8c78f0SHans Petter Selasky * decrypted. KTLS_MBUF_CRYPTO_ST_MIXED if there is a mix of encrypted 2409fe8c78f0SHans Petter Selasky * and decrypted data. Else KTLS_MBUF_CRYPTO_ST_ENCRYPTED if all data 2410fe8c78f0SHans Petter Selasky * is encrypted. 2411fe8c78f0SHans Petter Selasky */ 2412fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t 2413fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_state(struct mbuf *mb, int offset, int len) 2414fe8c78f0SHans Petter Selasky { 2415fe8c78f0SHans Petter Selasky int m_flags_ored = 0; 2416fe8c78f0SHans Petter Selasky int m_flags_anded = -1; 2417fe8c78f0SHans Petter Selasky 2418fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2419fe8c78f0SHans Petter Selasky if (offset < mb->m_len) 2420fe8c78f0SHans Petter Selasky break; 2421fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2422fe8c78f0SHans Petter Selasky } 2423fe8c78f0SHans Petter Selasky offset += len; 2424fe8c78f0SHans Petter Selasky 2425fe8c78f0SHans Petter Selasky for (; mb != NULL; mb = mb->m_next) { 2426fe8c78f0SHans Petter Selasky m_flags_ored |= mb->m_flags; 2427fe8c78f0SHans Petter Selasky m_flags_anded &= mb->m_flags; 2428fe8c78f0SHans Petter Selasky 2429fe8c78f0SHans Petter Selasky if (offset <= mb->m_len) 2430fe8c78f0SHans Petter Selasky break; 2431fe8c78f0SHans Petter Selasky offset -= mb->m_len; 2432fe8c78f0SHans Petter Selasky } 2433fe8c78f0SHans Petter Selasky MPASS(mb != NULL || offset == 0); 2434fe8c78f0SHans Petter Selasky 2435fe8c78f0SHans Petter Selasky if ((m_flags_ored ^ m_flags_anded) & M_DECRYPTED) 2436fe8c78f0SHans Petter Selasky return (KTLS_MBUF_CRYPTO_ST_MIXED); 2437fe8c78f0SHans Petter Selasky else 2438fe8c78f0SHans Petter Selasky return ((m_flags_ored & M_DECRYPTED) ? 2439fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_DECRYPTED : 2440fe8c78f0SHans Petter Selasky KTLS_MBUF_CRYPTO_ST_ENCRYPTED); 2441fe8c78f0SHans Petter Selasky } 2442fe8c78f0SHans Petter Selasky 2443fe8c78f0SHans Petter Selasky /* 2444fe8c78f0SHans Petter Selasky * ktls_resync_ifnet - get HW TLS RX back on track after packet loss 2445fe8c78f0SHans Petter Selasky */ 2446fe8c78f0SHans Petter Selasky static int 2447fe8c78f0SHans Petter Selasky ktls_resync_ifnet(struct socket *so, uint32_t tls_len, uint64_t tls_rcd_num) 2448fe8c78f0SHans Petter Selasky { 2449fe8c78f0SHans Petter Selasky union if_snd_tag_modify_params params; 2450fe8c78f0SHans Petter Selasky struct m_snd_tag *mst; 2451fe8c78f0SHans Petter Selasky struct inpcb *inp; 2452fe8c78f0SHans Petter Selasky struct tcpcb *tp; 2453fe8c78f0SHans Petter Selasky 2454fe8c78f0SHans Petter Selasky mst = so->so_rcv.sb_tls_info->snd_tag; 2455fe8c78f0SHans Petter Selasky if (__predict_false(mst == NULL)) 2456fe8c78f0SHans Petter Selasky return (EINVAL); 2457fe8c78f0SHans Petter Selasky 2458fe8c78f0SHans Petter Selasky inp = sotoinpcb(so); 2459fe8c78f0SHans Petter Selasky if (__predict_false(inp == NULL)) 2460fe8c78f0SHans Petter Selasky return (EINVAL); 2461fe8c78f0SHans Petter Selasky 2462fe8c78f0SHans Petter Selasky INP_RLOCK(inp); 246353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 2464fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2465fe8c78f0SHans Petter Selasky return (ECONNRESET); 2466fe8c78f0SHans Petter Selasky } 2467fe8c78f0SHans Petter Selasky 2468fe8c78f0SHans Petter Selasky tp = intotcpcb(inp); 2469fe8c78f0SHans Petter Selasky MPASS(tp != NULL); 2470fe8c78f0SHans Petter Selasky 2471fe8c78f0SHans Petter Selasky /* Get the TCP sequence number of the next valid TLS header. */ 2472fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(&so->so_rcv); 2473fe8c78f0SHans Petter Selasky params.tls_rx.tls_hdr_tcp_sn = 2474fe8c78f0SHans Petter Selasky tp->rcv_nxt - so->so_rcv.sb_tlscc - tls_len; 2475fe8c78f0SHans Petter Selasky params.tls_rx.tls_rec_length = tls_len; 2476fe8c78f0SHans Petter Selasky params.tls_rx.tls_seq_number = tls_rcd_num; 2477fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(&so->so_rcv); 2478fe8c78f0SHans Petter Selasky 2479fe8c78f0SHans Petter Selasky INP_RUNLOCK(inp); 2480fe8c78f0SHans Petter Selasky 2481fe8c78f0SHans Petter Selasky MPASS(mst->sw->type == IF_SND_TAG_TYPE_TLS_RX); 2482fe8c78f0SHans Petter Selasky return (mst->sw->snd_tag_modify(mst, ¶ms)); 2483fe8c78f0SHans Petter Selasky } 2484fe8c78f0SHans Petter Selasky 24853c0e5685SJohn Baldwin static void 248669542f26SJohn Baldwin ktls_drop(struct socket *so, int error) 248769542f26SJohn Baldwin { 248869542f26SJohn Baldwin struct epoch_tracker et; 248969542f26SJohn Baldwin struct inpcb *inp = sotoinpcb(so); 249069542f26SJohn Baldwin struct tcpcb *tp; 249169542f26SJohn Baldwin 249269542f26SJohn Baldwin NET_EPOCH_ENTER(et); 249369542f26SJohn Baldwin INP_WLOCK(inp); 249469542f26SJohn Baldwin if (!(inp->inp_flags & INP_DROPPED)) { 249569542f26SJohn Baldwin tp = intotcpcb(inp); 249669542f26SJohn Baldwin CURVNET_SET(inp->inp_vnet); 249769542f26SJohn Baldwin tp = tcp_drop(tp, error); 249869542f26SJohn Baldwin CURVNET_RESTORE(); 249969542f26SJohn Baldwin if (tp != NULL) 250069542f26SJohn Baldwin INP_WUNLOCK(inp); 250107be7517SJohn Baldwin } else { 250207be7517SJohn Baldwin so->so_error = error; 250307be7517SJohn Baldwin SOCK_RECVBUF_LOCK(so); 250407be7517SJohn Baldwin sorwakeup_locked(so); 250569542f26SJohn Baldwin INP_WUNLOCK(inp); 250607be7517SJohn Baldwin } 250769542f26SJohn Baldwin NET_EPOCH_EXIT(et); 250869542f26SJohn Baldwin } 250969542f26SJohn Baldwin 251069542f26SJohn Baldwin static void 25113c0e5685SJohn Baldwin ktls_decrypt(struct socket *so) 25123c0e5685SJohn Baldwin { 25133c0e5685SJohn Baldwin char tls_header[MBUF_PEXT_HDR_LEN]; 25143c0e5685SJohn Baldwin struct ktls_session *tls; 25153c0e5685SJohn Baldwin struct sockbuf *sb; 25163c0e5685SJohn Baldwin struct tls_record_layer *hdr; 25173c0e5685SJohn Baldwin struct tls_get_record tgr; 25183c0e5685SJohn Baldwin struct mbuf *control, *data, *m; 2519fe8c78f0SHans Petter Selasky ktls_mbuf_crypto_st_t state; 25203c0e5685SJohn Baldwin uint64_t seqno; 25213c0e5685SJohn Baldwin int error, remain, tls_len, trail_len; 252205a1d0f5SJohn Baldwin bool tls13; 252305a1d0f5SJohn Baldwin uint8_t vminor, record_type; 25243c0e5685SJohn Baldwin 25253c0e5685SJohn Baldwin hdr = (struct tls_record_layer *)tls_header; 25263c0e5685SJohn Baldwin sb = &so->so_rcv; 25273c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 25283c0e5685SJohn Baldwin KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING, 25293c0e5685SJohn Baldwin ("%s: socket %p not running", __func__, so)); 25303c0e5685SJohn Baldwin 25313c0e5685SJohn Baldwin tls = sb->sb_tls_info; 25323c0e5685SJohn Baldwin MPASS(tls != NULL); 25333c0e5685SJohn Baldwin 253405a1d0f5SJohn Baldwin tls13 = (tls->params.tls_vminor == TLS_MINOR_VER_THREE); 253505a1d0f5SJohn Baldwin if (tls13) 253605a1d0f5SJohn Baldwin vminor = TLS_MINOR_VER_TWO; 253705a1d0f5SJohn Baldwin else 253805a1d0f5SJohn Baldwin vminor = tls->params.tls_vminor; 25393c0e5685SJohn Baldwin for (;;) { 25403c0e5685SJohn Baldwin /* Is there enough queued for a TLS header? */ 25413c0e5685SJohn Baldwin if (sb->sb_tlscc < tls->params.tls_hlen) 25423c0e5685SJohn Baldwin break; 25433c0e5685SJohn Baldwin 25443c0e5685SJohn Baldwin m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header); 25453c0e5685SJohn Baldwin tls_len = sizeof(*hdr) + ntohs(hdr->tls_length); 25463c0e5685SJohn Baldwin 25473c0e5685SJohn Baldwin if (hdr->tls_vmajor != tls->params.tls_vmajor || 254805a1d0f5SJohn Baldwin hdr->tls_vminor != vminor) 254905a1d0f5SJohn Baldwin error = EINVAL; 255005a1d0f5SJohn Baldwin else if (tls13 && hdr->tls_type != TLS_RLTYPE_APP) 25513c0e5685SJohn Baldwin error = EINVAL; 25523c0e5685SJohn Baldwin else if (tls_len < tls->params.tls_hlen || tls_len > 25533c0e5685SJohn Baldwin tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 + 25543c0e5685SJohn Baldwin tls->params.tls_tlen) 25553c0e5685SJohn Baldwin error = EMSGSIZE; 25563c0e5685SJohn Baldwin else 25573c0e5685SJohn Baldwin error = 0; 25583c0e5685SJohn Baldwin if (__predict_false(error != 0)) { 25593c0e5685SJohn Baldwin /* 25603c0e5685SJohn Baldwin * We have a corrupted record and are likely 25613c0e5685SJohn Baldwin * out of sync. The connection isn't 25623c0e5685SJohn Baldwin * recoverable at this point, so abort it. 25633c0e5685SJohn Baldwin */ 25643c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 25653c0e5685SJohn Baldwin counter_u64_add(ktls_offload_corrupted_records, 1); 25663c0e5685SJohn Baldwin 256769542f26SJohn Baldwin ktls_drop(so, error); 25683c0e5685SJohn Baldwin goto deref; 25693c0e5685SJohn Baldwin } 25703c0e5685SJohn Baldwin 25713c0e5685SJohn Baldwin /* Is the entire record queued? */ 25723c0e5685SJohn Baldwin if (sb->sb_tlscc < tls_len) 25733c0e5685SJohn Baldwin break; 25743c0e5685SJohn Baldwin 25753c0e5685SJohn Baldwin /* 25763c0e5685SJohn Baldwin * Split out the portion of the mbuf chain containing 25773c0e5685SJohn Baldwin * this TLS record. 25783c0e5685SJohn Baldwin */ 25793c0e5685SJohn Baldwin data = ktls_detach_record(sb, tls_len); 25803c0e5685SJohn Baldwin if (data == NULL) 25813c0e5685SJohn Baldwin continue; 25823c0e5685SJohn Baldwin MPASS(sb->sb_tlsdcc == tls_len); 25833c0e5685SJohn Baldwin 25843c0e5685SJohn Baldwin seqno = sb->sb_tls_seqno; 25853c0e5685SJohn Baldwin sb->sb_tls_seqno++; 25863c0e5685SJohn Baldwin SBCHECK(sb); 25873c0e5685SJohn Baldwin SOCKBUF_UNLOCK(sb); 25883c0e5685SJohn Baldwin 2589fe8c78f0SHans Petter Selasky /* get crypto state for this TLS record */ 2590fe8c78f0SHans Petter Selasky state = ktls_mbuf_crypto_state(data, 0, tls_len); 2591fe8c78f0SHans Petter Selasky 2592fe8c78f0SHans Petter Selasky switch (state) { 2593fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_MIXED: 2594fe8c78f0SHans Petter Selasky error = ktls_ocf_recrypt(tls, hdr, data, seqno); 2595fe8c78f0SHans Petter Selasky if (error) 2596fe8c78f0SHans Petter Selasky break; 2597fe8c78f0SHans Petter Selasky /* FALLTHROUGH */ 2598fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_ENCRYPTED: 2599fe8c78f0SHans Petter Selasky error = ktls_ocf_decrypt(tls, hdr, data, seqno, 2600fe8c78f0SHans Petter Selasky &trail_len); 2601fe8c78f0SHans Petter Selasky if (__predict_true(error == 0)) { 2602fe8c78f0SHans Petter Selasky if (tls13) { 260305a1d0f5SJohn Baldwin error = tls13_find_record_type(tls, data, 260405a1d0f5SJohn Baldwin tls_len, &trail_len, &record_type); 2605fe8c78f0SHans Petter Selasky } else { 260605a1d0f5SJohn Baldwin record_type = hdr->tls_type; 260705a1d0f5SJohn Baldwin } 2608fe8c78f0SHans Petter Selasky } 2609fe8c78f0SHans Petter Selasky break; 2610fe8c78f0SHans Petter Selasky case KTLS_MBUF_CRYPTO_ST_DECRYPTED: 2611fe8c78f0SHans Petter Selasky /* 2612fe8c78f0SHans Petter Selasky * NIC TLS is only supported for AEAD 2613fe8c78f0SHans Petter Selasky * ciphersuites which used a fixed sized 2614fe8c78f0SHans Petter Selasky * trailer. 2615fe8c78f0SHans Petter Selasky */ 2616fe8c78f0SHans Petter Selasky if (tls13) { 2617fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen - 1; 2618fe8c78f0SHans Petter Selasky error = tls13_find_record_type(tls, data, 2619fe8c78f0SHans Petter Selasky tls_len, &trail_len, &record_type); 2620fe8c78f0SHans Petter Selasky } else { 2621fe8c78f0SHans Petter Selasky trail_len = tls->params.tls_tlen; 2622fe8c78f0SHans Petter Selasky error = 0; 2623fe8c78f0SHans Petter Selasky record_type = hdr->tls_type; 2624fe8c78f0SHans Petter Selasky } 2625fe8c78f0SHans Petter Selasky break; 2626fe8c78f0SHans Petter Selasky default: 2627fe8c78f0SHans Petter Selasky error = EINVAL; 2628fe8c78f0SHans Petter Selasky break; 2629fe8c78f0SHans Petter Selasky } 26303c0e5685SJohn Baldwin if (error) { 26313c0e5685SJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 26323c0e5685SJohn Baldwin 26333c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 26343c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 26353c0e5685SJohn Baldwin /* 26363c0e5685SJohn Baldwin * sbcut/drop/flush discarded these 26373c0e5685SJohn Baldwin * mbufs. 26383c0e5685SJohn Baldwin */ 26393c0e5685SJohn Baldwin m_freem(data); 26403c0e5685SJohn Baldwin break; 26413c0e5685SJohn Baldwin } 26423c0e5685SJohn Baldwin 26433c0e5685SJohn Baldwin /* 26443c0e5685SJohn Baldwin * Drop this TLS record's data, but keep 26453c0e5685SJohn Baldwin * decrypting subsequent records. 26463c0e5685SJohn Baldwin */ 26473c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 26483c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 26493c0e5685SJohn Baldwin 26509a673b71SJohn Baldwin if (error != EMSGSIZE) 26519a673b71SJohn Baldwin error = EBADMSG; 26523c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 26539a673b71SJohn Baldwin so->so_error = error; 26543c0e5685SJohn Baldwin sorwakeup_locked(so); 26553c0e5685SJohn Baldwin CURVNET_RESTORE(); 26563c0e5685SJohn Baldwin 26573c0e5685SJohn Baldwin m_freem(data); 26583c0e5685SJohn Baldwin 26593c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 26603c0e5685SJohn Baldwin continue; 26613c0e5685SJohn Baldwin } 26623c0e5685SJohn Baldwin 26633c0e5685SJohn Baldwin /* Allocate the control mbuf. */ 26646be8944dSMark Johnston memset(&tgr, 0, sizeof(tgr)); 266505a1d0f5SJohn Baldwin tgr.tls_type = record_type; 26663c0e5685SJohn Baldwin tgr.tls_vmajor = hdr->tls_vmajor; 26673c0e5685SJohn Baldwin tgr.tls_vminor = hdr->tls_vminor; 26683c0e5685SJohn Baldwin tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen - 26693c0e5685SJohn Baldwin trail_len); 2670b46667c6SGleb Smirnoff control = sbcreatecontrol(&tgr, sizeof(tgr), 26713c0e5685SJohn Baldwin TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK); 26723c0e5685SJohn Baldwin 26733c0e5685SJohn Baldwin SOCKBUF_LOCK(sb); 26743c0e5685SJohn Baldwin if (sb->sb_tlsdcc == 0) { 26753c0e5685SJohn Baldwin /* sbcut/drop/flush discarded these mbufs. */ 26763c0e5685SJohn Baldwin MPASS(sb->sb_tlscc == 0); 26773c0e5685SJohn Baldwin m_freem(data); 26783c0e5685SJohn Baldwin m_freem(control); 26793c0e5685SJohn Baldwin break; 26803c0e5685SJohn Baldwin } 26813c0e5685SJohn Baldwin 26823c0e5685SJohn Baldwin /* 26833c0e5685SJohn Baldwin * Clear the 'dcc' accounting in preparation for 26843c0e5685SJohn Baldwin * adding the decrypted record. 26853c0e5685SJohn Baldwin */ 26863c0e5685SJohn Baldwin sb->sb_ccc -= tls_len; 26873c0e5685SJohn Baldwin sb->sb_tlsdcc = 0; 26883c0e5685SJohn Baldwin SBCHECK(sb); 26893c0e5685SJohn Baldwin 26903c0e5685SJohn Baldwin /* If there is no payload, drop all of the data. */ 26913c0e5685SJohn Baldwin if (tgr.tls_length == htobe16(0)) { 26923c0e5685SJohn Baldwin m_freem(data); 26933c0e5685SJohn Baldwin data = NULL; 26943c0e5685SJohn Baldwin } else { 26953c0e5685SJohn Baldwin /* Trim header. */ 26963c0e5685SJohn Baldwin remain = tls->params.tls_hlen; 26973c0e5685SJohn Baldwin while (remain > 0) { 26983c0e5685SJohn Baldwin if (data->m_len > remain) { 26993c0e5685SJohn Baldwin data->m_data += remain; 27003c0e5685SJohn Baldwin data->m_len -= remain; 27013c0e5685SJohn Baldwin break; 27023c0e5685SJohn Baldwin } 27033c0e5685SJohn Baldwin remain -= data->m_len; 27043c0e5685SJohn Baldwin data = m_free(data); 27053c0e5685SJohn Baldwin } 27063c0e5685SJohn Baldwin 27073c0e5685SJohn Baldwin /* Trim trailer and clear M_NOTREADY. */ 27083c0e5685SJohn Baldwin remain = be16toh(tgr.tls_length); 27093c0e5685SJohn Baldwin m = data; 27103c0e5685SJohn Baldwin for (m = data; remain > m->m_len; m = m->m_next) { 2711fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 27123c0e5685SJohn Baldwin remain -= m->m_len; 27133c0e5685SJohn Baldwin } 27143c0e5685SJohn Baldwin m->m_len = remain; 27153c0e5685SJohn Baldwin m_freem(m->m_next); 27163c0e5685SJohn Baldwin m->m_next = NULL; 2717fe8c78f0SHans Petter Selasky m->m_flags &= ~(M_NOTREADY | M_DECRYPTED); 27183c0e5685SJohn Baldwin 27193c0e5685SJohn Baldwin /* Set EOR on the final mbuf. */ 27203c0e5685SJohn Baldwin m->m_flags |= M_EOR; 27213c0e5685SJohn Baldwin } 27223c0e5685SJohn Baldwin 27233c0e5685SJohn Baldwin sbappendcontrol_locked(sb, data, control, 0); 2724fe8c78f0SHans Petter Selasky 2725fe8c78f0SHans Petter Selasky if (__predict_false(state != KTLS_MBUF_CRYPTO_ST_DECRYPTED)) { 2726fe8c78f0SHans Petter Selasky sb->sb_flags |= SB_TLS_RX_RESYNC; 2727fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2728fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, tls_len, seqno); 2729fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2730fe8c78f0SHans Petter Selasky } else if (__predict_false(sb->sb_flags & SB_TLS_RX_RESYNC)) { 2731fe8c78f0SHans Petter Selasky sb->sb_flags &= ~SB_TLS_RX_RESYNC; 2732fe8c78f0SHans Petter Selasky SOCKBUF_UNLOCK(sb); 2733fe8c78f0SHans Petter Selasky ktls_resync_ifnet(so, 0, seqno); 2734fe8c78f0SHans Petter Selasky SOCKBUF_LOCK(sb); 2735fe8c78f0SHans Petter Selasky } 27363c0e5685SJohn Baldwin } 27373c0e5685SJohn Baldwin 27383c0e5685SJohn Baldwin sb->sb_flags &= ~SB_TLS_RX_RUNNING; 27393c0e5685SJohn Baldwin 27403c0e5685SJohn Baldwin if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0) 27413c0e5685SJohn Baldwin so->so_error = EMSGSIZE; 27423c0e5685SJohn Baldwin 27433c0e5685SJohn Baldwin sorwakeup_locked(so); 27443c0e5685SJohn Baldwin 27453c0e5685SJohn Baldwin deref: 27463c0e5685SJohn Baldwin SOCKBUF_UNLOCK_ASSERT(sb); 27473c0e5685SJohn Baldwin 27483c0e5685SJohn Baldwin CURVNET_SET(so->so_vnet); 27493c0e5685SJohn Baldwin sorele(so); 27503c0e5685SJohn Baldwin CURVNET_RESTORE(); 27513c0e5685SJohn Baldwin } 27523c0e5685SJohn Baldwin 27533c0e5685SJohn Baldwin void 2754d90fe9d0SGleb Smirnoff ktls_enqueue_to_free(struct mbuf *m) 2755b2e60773SJohn Baldwin { 2756b2e60773SJohn Baldwin struct ktls_wq *wq; 2757b2e60773SJohn Baldwin bool running; 2758b2e60773SJohn Baldwin 2759b2e60773SJohn Baldwin /* Mark it for freeing. */ 27607b6c99d0SGleb Smirnoff m->m_epg_flags |= EPG_FLAG_2FREE; 27617b6c99d0SGleb Smirnoff wq = &ktls_wq[m->m_epg_tls->wq_index]; 2762b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 27633c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 2764b2e60773SJohn Baldwin running = wq->running; 2765b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2766b2e60773SJohn Baldwin if (!running) 2767b2e60773SJohn Baldwin wakeup(wq); 2768b2e60773SJohn Baldwin } 2769b2e60773SJohn Baldwin 277049f6925cSMark Johnston static void * 277149f6925cSMark Johnston ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m) 277249f6925cSMark Johnston { 277349f6925cSMark Johnston void *buf; 277498215005SAndrew Gallatin int domain, running; 277549f6925cSMark Johnston 277649f6925cSMark Johnston if (m->m_epg_npgs <= 2) 277749f6925cSMark Johnston return (NULL); 277849f6925cSMark Johnston if (ktls_buffer_zone == NULL) 277949f6925cSMark Johnston return (NULL); 278049f6925cSMark Johnston if ((u_int)(ticks - wq->lastallocfail) < hz) { 278149f6925cSMark Johnston /* 278249f6925cSMark Johnston * Rate-limit allocation attempts after a failure. 278349f6925cSMark Johnston * ktls_buffer_import() will acquire a per-domain mutex to check 278449f6925cSMark Johnston * the free page queues and may fail consistently if memory is 278549f6925cSMark Johnston * fragmented. 278649f6925cSMark Johnston */ 278749f6925cSMark Johnston return (NULL); 278849f6925cSMark Johnston } 278949f6925cSMark Johnston buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM); 279098215005SAndrew Gallatin if (buf == NULL) { 279198215005SAndrew Gallatin domain = PCPU_GET(domain); 279249f6925cSMark Johnston wq->lastallocfail = ticks; 279398215005SAndrew Gallatin 279498215005SAndrew Gallatin /* 279598215005SAndrew Gallatin * Note that this check is "racy", but the races are 279698215005SAndrew Gallatin * harmless, and are either a spurious wakeup if 279798215005SAndrew Gallatin * multiple threads fail allocations before the alloc 279898215005SAndrew Gallatin * thread wakes, or waiting an extra second in case we 279998215005SAndrew Gallatin * see an old value of running == true. 280098215005SAndrew Gallatin */ 280198215005SAndrew Gallatin if (!VM_DOMAIN_EMPTY(domain)) { 280219855852SAndrew Gallatin running = atomic_load_int(&ktls_domains[domain].reclaim_td.running); 280398215005SAndrew Gallatin if (!running) 280419855852SAndrew Gallatin wakeup(&ktls_domains[domain].reclaim_td); 280598215005SAndrew Gallatin } 280698215005SAndrew Gallatin } 280749f6925cSMark Johnston return (buf); 280849f6925cSMark Johnston } 280949f6925cSMark Johnston 2810470e851cSJohn Baldwin static int 2811470e851cSJohn Baldwin ktls_encrypt_record(struct ktls_wq *wq, struct mbuf *m, 2812470e851cSJohn Baldwin struct ktls_session *tls, struct ktls_ocf_encrypt_state *state) 2813470e851cSJohn Baldwin { 2814470e851cSJohn Baldwin vm_page_t pg; 2815470e851cSJohn Baldwin int error, i, len, off; 2816470e851cSJohn Baldwin 2817470e851cSJohn Baldwin KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == (M_EXTPG | M_NOTREADY), 2818470e851cSJohn Baldwin ("%p not unready & nomap mbuf\n", m)); 2819470e851cSJohn Baldwin KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen, 2820470e851cSJohn Baldwin ("page count %d larger than maximum frame length %d", m->m_epg_npgs, 2821470e851cSJohn Baldwin ktls_maxlen)); 2822470e851cSJohn Baldwin 2823470e851cSJohn Baldwin /* Anonymous mbufs are encrypted in place. */ 2824470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) != 0) 2825a4c5d490SJohn Baldwin return (ktls_ocf_encrypt(state, tls, m, NULL, 0)); 2826470e851cSJohn Baldwin 2827470e851cSJohn Baldwin /* 2828470e851cSJohn Baldwin * For file-backed mbufs (from sendfile), anonymous wired 2829470e851cSJohn Baldwin * pages are allocated and used as the encryption destination. 2830470e851cSJohn Baldwin */ 2831470e851cSJohn Baldwin if ((state->cbuf = ktls_buffer_alloc(wq, m)) != NULL) { 2832470e851cSJohn Baldwin len = ptoa(m->m_epg_npgs - 1) + m->m_epg_last_len - 2833470e851cSJohn Baldwin m->m_epg_1st_off; 2834470e851cSJohn Baldwin state->dst_iov[0].iov_base = (char *)state->cbuf + 2835470e851cSJohn Baldwin m->m_epg_1st_off; 2836470e851cSJohn Baldwin state->dst_iov[0].iov_len = len; 2837470e851cSJohn Baldwin state->parray[0] = DMAP_TO_PHYS((vm_offset_t)state->cbuf); 2838470e851cSJohn Baldwin i = 1; 2839470e851cSJohn Baldwin } else { 2840470e851cSJohn Baldwin off = m->m_epg_1st_off; 2841470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++, off = 0) { 2842a4667e09SMark Johnston pg = vm_page_alloc_noobj(VM_ALLOC_NODUMP | 2843a4667e09SMark Johnston VM_ALLOC_WIRED | VM_ALLOC_WAITOK); 2844470e851cSJohn Baldwin len = m_epg_pagelen(m, i, off); 2845470e851cSJohn Baldwin state->parray[i] = VM_PAGE_TO_PHYS(pg); 2846470e851cSJohn Baldwin state->dst_iov[i].iov_base = 2847470e851cSJohn Baldwin (char *)PHYS_TO_DMAP(state->parray[i]) + off; 2848470e851cSJohn Baldwin state->dst_iov[i].iov_len = len; 2849470e851cSJohn Baldwin } 2850470e851cSJohn Baldwin } 2851470e851cSJohn Baldwin KASSERT(i + 1 <= nitems(state->dst_iov), ("dst_iov is too small")); 2852470e851cSJohn Baldwin state->dst_iov[i].iov_base = m->m_epg_trail; 2853470e851cSJohn Baldwin state->dst_iov[i].iov_len = m->m_epg_trllen; 2854470e851cSJohn Baldwin 2855a4c5d490SJohn Baldwin error = ktls_ocf_encrypt(state, tls, m, state->dst_iov, i + 1); 2856470e851cSJohn Baldwin 2857470e851cSJohn Baldwin if (__predict_false(error != 0)) { 2858470e851cSJohn Baldwin /* Free the anonymous pages. */ 2859470e851cSJohn Baldwin if (state->cbuf != NULL) 2860470e851cSJohn Baldwin uma_zfree(ktls_buffer_zone, state->cbuf); 2861470e851cSJohn Baldwin else { 2862470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) { 2863470e851cSJohn Baldwin pg = PHYS_TO_VM_PAGE(state->parray[i]); 2864470e851cSJohn Baldwin (void)vm_page_unwire_noq(pg); 2865470e851cSJohn Baldwin vm_page_free(pg); 2866470e851cSJohn Baldwin } 2867470e851cSJohn Baldwin } 2868470e851cSJohn Baldwin } 2869470e851cSJohn Baldwin return (error); 2870470e851cSJohn Baldwin } 2871470e851cSJohn Baldwin 28729f03d2c0SJohn Baldwin /* Number of TLS records in a batch passed to ktls_enqueue(). */ 28739f03d2c0SJohn Baldwin static u_int 28749f03d2c0SJohn Baldwin ktls_batched_records(struct mbuf *m) 28759f03d2c0SJohn Baldwin { 28769f03d2c0SJohn Baldwin int page_count, records; 28779f03d2c0SJohn Baldwin 28789f03d2c0SJohn Baldwin records = 0; 28799f03d2c0SJohn Baldwin page_count = m->m_epg_enc_cnt; 28809f03d2c0SJohn Baldwin while (page_count > 0) { 28819f03d2c0SJohn Baldwin records++; 28829f03d2c0SJohn Baldwin page_count -= m->m_epg_nrdy; 28839f03d2c0SJohn Baldwin m = m->m_next; 28849f03d2c0SJohn Baldwin } 28859f03d2c0SJohn Baldwin KASSERT(page_count == 0, ("%s: mismatched page count", __func__)); 28869f03d2c0SJohn Baldwin return (records); 28879f03d2c0SJohn Baldwin } 28889f03d2c0SJohn Baldwin 2889b2e60773SJohn Baldwin void 2890b2e60773SJohn Baldwin ktls_enqueue(struct mbuf *m, struct socket *so, int page_count) 2891b2e60773SJohn Baldwin { 28929f03d2c0SJohn Baldwin struct ktls_session *tls; 2893b2e60773SJohn Baldwin struct ktls_wq *wq; 28949f03d2c0SJohn Baldwin int queued; 2895b2e60773SJohn Baldwin bool running; 2896b2e60773SJohn Baldwin 28976edfd179SGleb Smirnoff KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) == 28986edfd179SGleb Smirnoff (M_EXTPG | M_NOTREADY)), 2899b2e60773SJohn Baldwin ("ktls_enqueue: %p not unready & nomap mbuf\n", m)); 2900b2e60773SJohn Baldwin KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count")); 2901b2e60773SJohn Baldwin 29027b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf")); 2903b2e60773SJohn Baldwin 29047b6c99d0SGleb Smirnoff m->m_epg_enc_cnt = page_count; 2905b2e60773SJohn Baldwin 2906b2e60773SJohn Baldwin /* 2907b2e60773SJohn Baldwin * Save a pointer to the socket. The caller is responsible 2908b2e60773SJohn Baldwin * for taking an additional reference via soref(). 2909b2e60773SJohn Baldwin */ 29107b6c99d0SGleb Smirnoff m->m_epg_so = so; 2911b2e60773SJohn Baldwin 29129f03d2c0SJohn Baldwin queued = 1; 29139f03d2c0SJohn Baldwin tls = m->m_epg_tls; 29149f03d2c0SJohn Baldwin wq = &ktls_wq[tls->wq_index]; 2915b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 29169f03d2c0SJohn Baldwin if (__predict_false(tls->sequential_records)) { 29179f03d2c0SJohn Baldwin /* 29189f03d2c0SJohn Baldwin * For TLS 1.0, records must be encrypted 29199f03d2c0SJohn Baldwin * sequentially. For a given connection, all records 29209f03d2c0SJohn Baldwin * queued to the associated work queue are processed 29219f03d2c0SJohn Baldwin * sequentially. However, sendfile(2) might complete 29229f03d2c0SJohn Baldwin * I/O requests spanning multiple TLS records out of 29239f03d2c0SJohn Baldwin * order. Here we ensure TLS records are enqueued to 29249f03d2c0SJohn Baldwin * the work queue in FIFO order. 29259f03d2c0SJohn Baldwin * 29269f03d2c0SJohn Baldwin * tls->next_seqno holds the sequence number of the 29279f03d2c0SJohn Baldwin * next TLS record that should be enqueued to the work 29289f03d2c0SJohn Baldwin * queue. If this next record is not tls->next_seqno, 29299f03d2c0SJohn Baldwin * it must be a future record, so insert it, sorted by 29309f03d2c0SJohn Baldwin * TLS sequence number, into tls->pending_records and 29319f03d2c0SJohn Baldwin * return. 29329f03d2c0SJohn Baldwin * 29339f03d2c0SJohn Baldwin * If this TLS record matches tls->next_seqno, place 29349f03d2c0SJohn Baldwin * it in the work queue and then check 29359f03d2c0SJohn Baldwin * tls->pending_records to see if any 29369f03d2c0SJohn Baldwin * previously-queued records are now ready for 29379f03d2c0SJohn Baldwin * encryption. 29389f03d2c0SJohn Baldwin */ 29399f03d2c0SJohn Baldwin if (m->m_epg_seqno != tls->next_seqno) { 29409f03d2c0SJohn Baldwin struct mbuf *n, *p; 29419f03d2c0SJohn Baldwin 29429f03d2c0SJohn Baldwin p = NULL; 29439f03d2c0SJohn Baldwin STAILQ_FOREACH(n, &tls->pending_records, m_epg_stailq) { 29449f03d2c0SJohn Baldwin if (n->m_epg_seqno > m->m_epg_seqno) 29459f03d2c0SJohn Baldwin break; 29469f03d2c0SJohn Baldwin p = n; 29479f03d2c0SJohn Baldwin } 29489f03d2c0SJohn Baldwin if (n == NULL) 29499f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&tls->pending_records, m, 29509f03d2c0SJohn Baldwin m_epg_stailq); 29519f03d2c0SJohn Baldwin else if (p == NULL) 29529f03d2c0SJohn Baldwin STAILQ_INSERT_HEAD(&tls->pending_records, m, 29539f03d2c0SJohn Baldwin m_epg_stailq); 29549f03d2c0SJohn Baldwin else 29559f03d2c0SJohn Baldwin STAILQ_INSERT_AFTER(&tls->pending_records, p, m, 29569f03d2c0SJohn Baldwin m_epg_stailq); 29579f03d2c0SJohn Baldwin mtx_unlock(&wq->mtx); 29589f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, 1); 29599f03d2c0SJohn Baldwin return; 29609f03d2c0SJohn Baldwin } 29619f03d2c0SJohn Baldwin 29629f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(m); 29633c0e5685SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 29649f03d2c0SJohn Baldwin 29659f03d2c0SJohn Baldwin while (!STAILQ_EMPTY(&tls->pending_records)) { 29669f03d2c0SJohn Baldwin struct mbuf *n; 29679f03d2c0SJohn Baldwin 29689f03d2c0SJohn Baldwin n = STAILQ_FIRST(&tls->pending_records); 29699f03d2c0SJohn Baldwin if (n->m_epg_seqno != tls->next_seqno) 29709f03d2c0SJohn Baldwin break; 29719f03d2c0SJohn Baldwin 29729f03d2c0SJohn Baldwin queued++; 29739f03d2c0SJohn Baldwin STAILQ_REMOVE_HEAD(&tls->pending_records, m_epg_stailq); 29749f03d2c0SJohn Baldwin tls->next_seqno += ktls_batched_records(n); 29759f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, n, m_epg_stailq); 29769f03d2c0SJohn Baldwin } 29779f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_pending, -(queued - 1)); 29789f03d2c0SJohn Baldwin } else 29799f03d2c0SJohn Baldwin STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq); 29809f03d2c0SJohn Baldwin 2981b2e60773SJohn Baldwin running = wq->running; 2982b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 2983b2e60773SJohn Baldwin if (!running) 2984b2e60773SJohn Baldwin wakeup(wq); 29859f03d2c0SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, queued); 2986b2e60773SJohn Baldwin } 2987b2e60773SJohn Baldwin 2988470e851cSJohn Baldwin /* 2989470e851cSJohn Baldwin * Once a file-backed mbuf (from sendfile) has been encrypted, free 2990470e851cSJohn Baldwin * the pages from the file and replace them with the anonymous pages 2991470e851cSJohn Baldwin * allocated in ktls_encrypt_record(). 2992470e851cSJohn Baldwin */ 2993470e851cSJohn Baldwin static void 2994470e851cSJohn Baldwin ktls_finish_nonanon(struct mbuf *m, struct ktls_ocf_encrypt_state *state) 2995470e851cSJohn Baldwin { 2996470e851cSJohn Baldwin int i; 2997470e851cSJohn Baldwin 2998470e851cSJohn Baldwin MPASS((m->m_epg_flags & EPG_FLAG_ANON) == 0); 2999470e851cSJohn Baldwin 3000470e851cSJohn Baldwin /* Free the old pages. */ 3001470e851cSJohn Baldwin m->m_ext.ext_free(m); 3002470e851cSJohn Baldwin 3003470e851cSJohn Baldwin /* Replace them with the new pages. */ 3004470e851cSJohn Baldwin if (state->cbuf != NULL) { 3005470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 3006470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[0] + ptoa(i); 3007470e851cSJohn Baldwin 3008470e851cSJohn Baldwin /* Contig pages should go back to the cache. */ 3009470e851cSJohn Baldwin m->m_ext.ext_free = ktls_free_mext_contig; 3010470e851cSJohn Baldwin } else { 3011470e851cSJohn Baldwin for (i = 0; i < m->m_epg_npgs; i++) 3012470e851cSJohn Baldwin m->m_epg_pa[i] = state->parray[i]; 3013470e851cSJohn Baldwin 3014470e851cSJohn Baldwin /* Use the basic free routine. */ 3015470e851cSJohn Baldwin m->m_ext.ext_free = mb_free_mext_pgs; 3016470e851cSJohn Baldwin } 3017470e851cSJohn Baldwin 3018470e851cSJohn Baldwin /* Pages are now writable. */ 3019470e851cSJohn Baldwin m->m_epg_flags |= EPG_FLAG_ANON; 3020470e851cSJohn Baldwin } 30216b313a3aSJohn Baldwin 3022b2e60773SJohn Baldwin static __noinline void 302349f6925cSMark Johnston ktls_encrypt(struct ktls_wq *wq, struct mbuf *top) 3024b2e60773SJohn Baldwin { 3025470e851cSJohn Baldwin struct ktls_ocf_encrypt_state state; 3026b2e60773SJohn Baldwin struct ktls_session *tls; 3027b2e60773SJohn Baldwin struct socket *so; 3028d90fe9d0SGleb Smirnoff struct mbuf *m; 3029470e851cSJohn Baldwin int error, npages, total_pages; 3030b2e60773SJohn Baldwin 30317b6c99d0SGleb Smirnoff so = top->m_epg_so; 30327b6c99d0SGleb Smirnoff tls = top->m_epg_tls; 3033d90fe9d0SGleb Smirnoff KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 3034d90fe9d0SGleb Smirnoff KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 3035b2e60773SJohn Baldwin #ifdef INVARIANTS 30367b6c99d0SGleb Smirnoff top->m_epg_so = NULL; 3037b2e60773SJohn Baldwin #endif 30387b6c99d0SGleb Smirnoff total_pages = top->m_epg_enc_cnt; 3039b2e60773SJohn Baldwin npages = 0; 3040b2e60773SJohn Baldwin 3041b2e60773SJohn Baldwin /* 3042b2e60773SJohn Baldwin * Encrypt the TLS records in the chain of mbufs starting with 3043b2e60773SJohn Baldwin * 'top'. 'total_pages' gives us a total count of pages and is 3044b2e60773SJohn Baldwin * used to know when we have finished encrypting the TLS 3045b2e60773SJohn Baldwin * records originally queued with 'top'. 3046b2e60773SJohn Baldwin * 3047b2e60773SJohn Baldwin * NB: These mbufs are queued in the socket buffer and 3048b2e60773SJohn Baldwin * 'm_next' is traversing the mbufs in the socket buffer. The 3049b2e60773SJohn Baldwin * socket buffer lock is not held while traversing this chain. 3050b2e60773SJohn Baldwin * Since the mbufs are all marked M_NOTREADY their 'm_next' 3051b2e60773SJohn Baldwin * pointers should be stable. However, the 'm_next' of the 3052b2e60773SJohn Baldwin * last mbuf encrypted is not necessarily NULL. It can point 3053b2e60773SJohn Baldwin * to other mbufs appended while 'top' was on the TLS work 3054b2e60773SJohn Baldwin * queue. 3055b2e60773SJohn Baldwin * 3056b2e60773SJohn Baldwin * Each mbuf holds an entire TLS record. 3057b2e60773SJohn Baldwin */ 3058b2e60773SJohn Baldwin error = 0; 3059b2e60773SJohn Baldwin for (m = top; npages != total_pages; m = m->m_next) { 30607b6c99d0SGleb Smirnoff KASSERT(m->m_epg_tls == tls, 3061b2e60773SJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 30627b6c99d0SGleb Smirnoff tls, m->m_epg_tls)); 30637b6c99d0SGleb Smirnoff KASSERT(npages + m->m_epg_npgs <= total_pages, 3064b2e60773SJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 3065b2e60773SJohn Baldwin total_pages, m)); 3066b2e60773SJohn Baldwin 3067470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, &state); 306821e3c1fbSJohn Baldwin if (error) { 306921e3c1fbSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 307021e3c1fbSJohn Baldwin break; 307121e3c1fbSJohn Baldwin } 307221e3c1fbSJohn Baldwin 3073470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 3074470e851cSJohn Baldwin ktls_finish_nonanon(m, &state); 3075470e851cSJohn Baldwin 3076d16cb228SJohn Baldwin npages += m->m_epg_nrdy; 3077b2e60773SJohn Baldwin 3078b2e60773SJohn Baldwin /* 3079b2e60773SJohn Baldwin * Drop a reference to the session now that it is no 3080b2e60773SJohn Baldwin * longer needed. Existing code depends on encrypted 3081b2e60773SJohn Baldwin * records having no associated session vs 3082b2e60773SJohn Baldwin * yet-to-be-encrypted records having an associated 3083b2e60773SJohn Baldwin * session. 3084b2e60773SJohn Baldwin */ 30857b6c99d0SGleb Smirnoff m->m_epg_tls = NULL; 3086b2e60773SJohn Baldwin ktls_free(tls); 3087b2e60773SJohn Baldwin } 3088b2e60773SJohn Baldwin 3089b2e60773SJohn Baldwin CURVNET_SET(so->so_vnet); 3090b2e60773SJohn Baldwin if (error == 0) { 3091e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, top, npages); 3092b2e60773SJohn Baldwin } else { 309369542f26SJohn Baldwin ktls_drop(so, EIO); 3094b2e60773SJohn Baldwin mb_free_notready(top, total_pages); 3095b2e60773SJohn Baldwin } 3096b2e60773SJohn Baldwin 3097b2e60773SJohn Baldwin sorele(so); 3098b2e60773SJohn Baldwin CURVNET_RESTORE(); 3099b2e60773SJohn Baldwin } 3100b2e60773SJohn Baldwin 3101470e851cSJohn Baldwin void 3102470e851cSJohn Baldwin ktls_encrypt_cb(struct ktls_ocf_encrypt_state *state, int error) 3103470e851cSJohn Baldwin { 3104470e851cSJohn Baldwin struct ktls_session *tls; 3105470e851cSJohn Baldwin struct socket *so; 3106470e851cSJohn Baldwin struct mbuf *m; 3107470e851cSJohn Baldwin int npages; 3108470e851cSJohn Baldwin 3109470e851cSJohn Baldwin m = state->m; 3110470e851cSJohn Baldwin 3111470e851cSJohn Baldwin if ((m->m_epg_flags & EPG_FLAG_ANON) == 0) 3112470e851cSJohn Baldwin ktls_finish_nonanon(m, state); 3113470e851cSJohn Baldwin 3114470e851cSJohn Baldwin so = state->so; 3115470e851cSJohn Baldwin free(state, M_KTLS); 3116470e851cSJohn Baldwin 3117470e851cSJohn Baldwin /* 3118470e851cSJohn Baldwin * Drop a reference to the session now that it is no longer 3119470e851cSJohn Baldwin * needed. Existing code depends on encrypted records having 3120470e851cSJohn Baldwin * no associated session vs yet-to-be-encrypted records having 3121470e851cSJohn Baldwin * an associated session. 3122470e851cSJohn Baldwin */ 3123470e851cSJohn Baldwin tls = m->m_epg_tls; 3124470e851cSJohn Baldwin m->m_epg_tls = NULL; 3125470e851cSJohn Baldwin ktls_free(tls); 3126470e851cSJohn Baldwin 3127470e851cSJohn Baldwin if (error != 0) 3128470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 3129470e851cSJohn Baldwin 3130470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3131470e851cSJohn Baldwin npages = m->m_epg_nrdy; 3132470e851cSJohn Baldwin 3133470e851cSJohn Baldwin if (error == 0) { 3134e7d02be1SGleb Smirnoff (void)so->so_proto->pr_ready(so, m, npages); 3135470e851cSJohn Baldwin } else { 313669542f26SJohn Baldwin ktls_drop(so, EIO); 3137470e851cSJohn Baldwin mb_free_notready(m, npages); 3138470e851cSJohn Baldwin } 3139470e851cSJohn Baldwin 3140470e851cSJohn Baldwin sorele(so); 3141470e851cSJohn Baldwin CURVNET_RESTORE(); 3142470e851cSJohn Baldwin } 3143470e851cSJohn Baldwin 3144470e851cSJohn Baldwin /* 3145470e851cSJohn Baldwin * Similar to ktls_encrypt, but used with asynchronous OCF backends 3146470e851cSJohn Baldwin * (coprocessors) where encryption does not use host CPU resources and 3147470e851cSJohn Baldwin * it can be beneficial to queue more requests than CPUs. 3148470e851cSJohn Baldwin */ 3149470e851cSJohn Baldwin static __noinline void 3150470e851cSJohn Baldwin ktls_encrypt_async(struct ktls_wq *wq, struct mbuf *top) 3151470e851cSJohn Baldwin { 3152470e851cSJohn Baldwin struct ktls_ocf_encrypt_state *state; 3153470e851cSJohn Baldwin struct ktls_session *tls; 3154470e851cSJohn Baldwin struct socket *so; 3155470e851cSJohn Baldwin struct mbuf *m, *n; 3156470e851cSJohn Baldwin int error, mpages, npages, total_pages; 3157470e851cSJohn Baldwin 3158470e851cSJohn Baldwin so = top->m_epg_so; 3159470e851cSJohn Baldwin tls = top->m_epg_tls; 3160470e851cSJohn Baldwin KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top)); 3161470e851cSJohn Baldwin KASSERT(so != NULL, ("so = NULL, top = %p\n", top)); 3162470e851cSJohn Baldwin #ifdef INVARIANTS 3163470e851cSJohn Baldwin top->m_epg_so = NULL; 3164470e851cSJohn Baldwin #endif 3165470e851cSJohn Baldwin total_pages = top->m_epg_enc_cnt; 3166470e851cSJohn Baldwin npages = 0; 3167470e851cSJohn Baldwin 3168470e851cSJohn Baldwin error = 0; 3169470e851cSJohn Baldwin for (m = top; npages != total_pages; m = n) { 3170470e851cSJohn Baldwin KASSERT(m->m_epg_tls == tls, 3171470e851cSJohn Baldwin ("different TLS sessions in a single mbuf chain: %p vs %p", 3172470e851cSJohn Baldwin tls, m->m_epg_tls)); 3173470e851cSJohn Baldwin KASSERT(npages + m->m_epg_npgs <= total_pages, 3174470e851cSJohn Baldwin ("page count mismatch: top %p, total_pages %d, m %p", top, 3175470e851cSJohn Baldwin total_pages, m)); 3176470e851cSJohn Baldwin 3177470e851cSJohn Baldwin state = malloc(sizeof(*state), M_KTLS, M_WAITOK | M_ZERO); 3178470e851cSJohn Baldwin soref(so); 3179470e851cSJohn Baldwin state->so = so; 3180470e851cSJohn Baldwin state->m = m; 3181470e851cSJohn Baldwin 3182470e851cSJohn Baldwin mpages = m->m_epg_nrdy; 3183470e851cSJohn Baldwin n = m->m_next; 3184470e851cSJohn Baldwin 3185470e851cSJohn Baldwin error = ktls_encrypt_record(wq, m, tls, state); 3186470e851cSJohn Baldwin if (error) { 3187470e851cSJohn Baldwin counter_u64_add(ktls_offload_failed_crypto, 1); 3188470e851cSJohn Baldwin free(state, M_KTLS); 3189470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3190470e851cSJohn Baldwin sorele(so); 3191470e851cSJohn Baldwin CURVNET_RESTORE(); 3192470e851cSJohn Baldwin break; 3193470e851cSJohn Baldwin } 3194470e851cSJohn Baldwin 3195470e851cSJohn Baldwin npages += mpages; 3196470e851cSJohn Baldwin } 3197470e851cSJohn Baldwin 3198470e851cSJohn Baldwin CURVNET_SET(so->so_vnet); 3199470e851cSJohn Baldwin if (error != 0) { 320069542f26SJohn Baldwin ktls_drop(so, EIO); 3201470e851cSJohn Baldwin mb_free_notready(m, total_pages - npages); 3202470e851cSJohn Baldwin } 3203470e851cSJohn Baldwin 3204470e851cSJohn Baldwin sorele(so); 3205470e851cSJohn Baldwin CURVNET_RESTORE(); 3206470e851cSJohn Baldwin } 3207470e851cSJohn Baldwin 3208a72ee355SJohn Baldwin static int 3209a72ee355SJohn Baldwin ktls_bind_domain(int domain) 3210a72ee355SJohn Baldwin { 3211a72ee355SJohn Baldwin int error; 3212a72ee355SJohn Baldwin 3213a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &cpuset_domain[domain]); 3214a72ee355SJohn Baldwin if (error != 0) 3215a72ee355SJohn Baldwin return (error); 3216a72ee355SJohn Baldwin curthread->td_domain.dr_policy = DOMAINSET_PREF(domain); 3217a72ee355SJohn Baldwin return (0); 3218a72ee355SJohn Baldwin } 3219a72ee355SJohn Baldwin 3220b2e60773SJohn Baldwin static void 322119855852SAndrew Gallatin ktls_reclaim_thread(void *ctx) 322298215005SAndrew Gallatin { 322398215005SAndrew Gallatin struct ktls_domain_info *ktls_domain = ctx; 322419855852SAndrew Gallatin struct ktls_reclaim_thread *sc = &ktls_domain->reclaim_td; 322598215005SAndrew Gallatin struct sysctl_oid *oid; 322698215005SAndrew Gallatin char name[80]; 322719855852SAndrew Gallatin int error, domain; 322898215005SAndrew Gallatin 3229a72ee355SJohn Baldwin domain = ktls_domain - ktls_domains; 323098215005SAndrew Gallatin if (bootverbose) 323119855852SAndrew Gallatin printf("Starting KTLS reclaim thread for domain %d\n", domain); 3232a72ee355SJohn Baldwin error = ktls_bind_domain(domain); 3233a72ee355SJohn Baldwin if (error) 323419855852SAndrew Gallatin printf("Unable to bind KTLS reclaim thread for domain %d: error %d\n", 3235a72ee355SJohn Baldwin domain, error); 3236a72ee355SJohn Baldwin snprintf(name, sizeof(name), "domain%d", domain); 323798215005SAndrew Gallatin oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern_ipc_tls), OID_AUTO, 323898215005SAndrew Gallatin name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 323919855852SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "reclaims", 324019855852SAndrew Gallatin CTLFLAG_RD, &sc->reclaims, 0, "buffers reclaimed"); 324198215005SAndrew Gallatin SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "wakeups", 324298215005SAndrew Gallatin CTLFLAG_RD, &sc->wakeups, 0, "thread wakeups"); 324398215005SAndrew Gallatin SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "running", 324498215005SAndrew Gallatin CTLFLAG_RD, &sc->running, 0, "thread running"); 324598215005SAndrew Gallatin 324698215005SAndrew Gallatin for (;;) { 324798215005SAndrew Gallatin atomic_store_int(&sc->running, 0); 324809066b98SAndrew Gallatin tsleep(sc, PZERO | PNOLOCK, "-", 0); 324998215005SAndrew Gallatin atomic_store_int(&sc->running, 1); 325098215005SAndrew Gallatin sc->wakeups++; 325198215005SAndrew Gallatin /* 325219855852SAndrew Gallatin * Below we attempt to reclaim ktls_max_reclaim 325319855852SAndrew Gallatin * buffers using vm_page_reclaim_contig_domain_ext(). 325419855852SAndrew Gallatin * We do this here, as this function can take several 325519855852SAndrew Gallatin * seconds to scan all of memory and it does not 325619855852SAndrew Gallatin * matter if this thread pauses for a while. If we 325719855852SAndrew Gallatin * block a ktls worker thread, we risk developing 325819855852SAndrew Gallatin * backlogs of buffers to be encrypted, leading to 325919855852SAndrew Gallatin * surges of traffic and potential NIC output drops. 326098215005SAndrew Gallatin */ 32612619c5ccSJason A. Harmening if (vm_page_reclaim_contig_domain_ext(domain, VM_ALLOC_NORMAL, 32622619c5ccSJason A. Harmening atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0, 32632619c5ccSJason A. Harmening ktls_max_reclaim) != 0) { 326419855852SAndrew Gallatin vm_wait_domain(domain); 326519855852SAndrew Gallatin } else { 326619855852SAndrew Gallatin sc->reclaims += ktls_max_reclaim; 326798215005SAndrew Gallatin } 326898215005SAndrew Gallatin } 326998215005SAndrew Gallatin } 327098215005SAndrew Gallatin 327198215005SAndrew Gallatin static void 3272b2e60773SJohn Baldwin ktls_work_thread(void *ctx) 3273b2e60773SJohn Baldwin { 3274b2e60773SJohn Baldwin struct ktls_wq *wq = ctx; 3275d90fe9d0SGleb Smirnoff struct mbuf *m, *n; 32763c0e5685SJohn Baldwin struct socket *so, *son; 32773c0e5685SJohn Baldwin STAILQ_HEAD(, mbuf) local_m_head; 32783c0e5685SJohn Baldwin STAILQ_HEAD(, socket) local_so_head; 3279a72ee355SJohn Baldwin int cpu; 3280a72ee355SJohn Baldwin 3281a72ee355SJohn Baldwin cpu = wq - ktls_wq; 3282a72ee355SJohn Baldwin if (bootverbose) 3283a72ee355SJohn Baldwin printf("Starting KTLS worker thread for CPU %d\n", cpu); 3284a72ee355SJohn Baldwin 3285a72ee355SJohn Baldwin /* 3286a72ee355SJohn Baldwin * Bind to a core. If ktls_bind_threads is > 1, then 3287a72ee355SJohn Baldwin * we bind to the NUMA domain instead. 3288a72ee355SJohn Baldwin */ 3289a72ee355SJohn Baldwin if (ktls_bind_threads) { 3290a72ee355SJohn Baldwin int error; 3291b2e60773SJohn Baldwin 329202bc3865SAndrew Gallatin if (ktls_bind_threads > 1) { 3293a72ee355SJohn Baldwin struct pcpu *pc = pcpu_find(cpu); 3294a72ee355SJohn Baldwin 3295a72ee355SJohn Baldwin error = ktls_bind_domain(pc->pc_domain); 3296a72ee355SJohn Baldwin } else { 3297a72ee355SJohn Baldwin cpuset_t mask; 3298a72ee355SJohn Baldwin 3299a72ee355SJohn Baldwin CPU_SETOF(cpu, &mask); 3300a72ee355SJohn Baldwin error = cpuset_setthread(curthread->td_tid, &mask); 3301a72ee355SJohn Baldwin } 3302a72ee355SJohn Baldwin if (error) 3303a72ee355SJohn Baldwin printf("Unable to bind KTLS worker thread for CPU %d: error %d\n", 3304a72ee355SJohn Baldwin cpu, error); 330502bc3865SAndrew Gallatin } 3306b2e60773SJohn Baldwin #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__) 3307b2e60773SJohn Baldwin fpu_kern_thread(0); 3308b2e60773SJohn Baldwin #endif 3309b2e60773SJohn Baldwin for (;;) { 3310b2e60773SJohn Baldwin mtx_lock(&wq->mtx); 33113c0e5685SJohn Baldwin while (STAILQ_EMPTY(&wq->m_head) && 33123c0e5685SJohn Baldwin STAILQ_EMPTY(&wq->so_head)) { 3313b2e60773SJohn Baldwin wq->running = false; 3314b2e60773SJohn Baldwin mtx_sleep(wq, &wq->mtx, 0, "-", 0); 3315b2e60773SJohn Baldwin wq->running = true; 3316b2e60773SJohn Baldwin } 3317b2e60773SJohn Baldwin 33183c0e5685SJohn Baldwin STAILQ_INIT(&local_m_head); 33193c0e5685SJohn Baldwin STAILQ_CONCAT(&local_m_head, &wq->m_head); 33203c0e5685SJohn Baldwin STAILQ_INIT(&local_so_head); 33213c0e5685SJohn Baldwin STAILQ_CONCAT(&local_so_head, &wq->so_head); 3322b2e60773SJohn Baldwin mtx_unlock(&wq->mtx); 3323b2e60773SJohn Baldwin 33243c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) { 33257b6c99d0SGleb Smirnoff if (m->m_epg_flags & EPG_FLAG_2FREE) { 33267b6c99d0SGleb Smirnoff ktls_free(m->m_epg_tls); 3327904a08f3SMateusz Guzik m_free_raw(m); 3328eeec8348SGleb Smirnoff } else { 3329470e851cSJohn Baldwin if (m->m_epg_tls->sync_dispatch) 333049f6925cSMark Johnston ktls_encrypt(wq, m); 3331470e851cSJohn Baldwin else 3332470e851cSJohn Baldwin ktls_encrypt_async(wq, m); 33333c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_tx_queued, -1); 3334b2e60773SJohn Baldwin } 3335b2e60773SJohn Baldwin } 33363c0e5685SJohn Baldwin 33373c0e5685SJohn Baldwin STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) { 33383c0e5685SJohn Baldwin ktls_decrypt(so); 33393c0e5685SJohn Baldwin counter_u64_add(ktls_cnt_rx_queued, -1); 33403c0e5685SJohn Baldwin } 3341b2e60773SJohn Baldwin } 3342b2e60773SJohn Baldwin } 334328d0a740SAndrew Gallatin 334428d0a740SAndrew Gallatin static void 334528d0a740SAndrew Gallatin ktls_disable_ifnet_help(void *context, int pending __unused) 334628d0a740SAndrew Gallatin { 334728d0a740SAndrew Gallatin struct ktls_session *tls; 334828d0a740SAndrew Gallatin struct inpcb *inp; 334928d0a740SAndrew Gallatin struct tcpcb *tp; 335028d0a740SAndrew Gallatin struct socket *so; 335128d0a740SAndrew Gallatin int err; 335228d0a740SAndrew Gallatin 335328d0a740SAndrew Gallatin tls = context; 335428d0a740SAndrew Gallatin inp = tls->inp; 335528d0a740SAndrew Gallatin if (inp == NULL) 335628d0a740SAndrew Gallatin return; 335728d0a740SAndrew Gallatin INP_WLOCK(inp); 335828d0a740SAndrew Gallatin so = inp->inp_socket; 335928d0a740SAndrew Gallatin MPASS(so != NULL); 336053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 336128d0a740SAndrew Gallatin goto out; 336228d0a740SAndrew Gallatin } 336328d0a740SAndrew Gallatin 336428d0a740SAndrew Gallatin if (so->so_snd.sb_tls_info != NULL) 336528d0a740SAndrew Gallatin err = ktls_set_tx_mode(so, TCP_TLS_MODE_SW); 336628d0a740SAndrew Gallatin else 336728d0a740SAndrew Gallatin err = ENXIO; 336828d0a740SAndrew Gallatin if (err == 0) { 336928d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_ok, 1); 337028d0a740SAndrew Gallatin /* ktls_set_tx_mode() drops inp wlock, so recheck flags */ 337153af6903SGleb Smirnoff if ((inp->inp_flags & INP_DROPPED) == 0 && 337228d0a740SAndrew Gallatin (tp = intotcpcb(inp)) != NULL && 337328d0a740SAndrew Gallatin tp->t_fb->tfb_hwtls_change != NULL) 337428d0a740SAndrew Gallatin (*tp->t_fb->tfb_hwtls_change)(tp, 0); 337528d0a740SAndrew Gallatin } else { 337628d0a740SAndrew Gallatin counter_u64_add(ktls_ifnet_disable_fail, 1); 337728d0a740SAndrew Gallatin } 337828d0a740SAndrew Gallatin 337928d0a740SAndrew Gallatin out: 3380846e4a20SJohn Baldwin CURVNET_SET(so->so_vnet); 338128d0a740SAndrew Gallatin sorele(so); 3382846e4a20SJohn Baldwin CURVNET_RESTORE(); 338328d0a740SAndrew Gallatin INP_WUNLOCK(inp); 338428d0a740SAndrew Gallatin ktls_free(tls); 338528d0a740SAndrew Gallatin } 338628d0a740SAndrew Gallatin 338728d0a740SAndrew Gallatin /* 338828d0a740SAndrew Gallatin * Called when re-transmits are becoming a substantial portion of the 338928d0a740SAndrew Gallatin * sends on this connection. When this happens, we transition the 339028d0a740SAndrew Gallatin * connection to software TLS. This is needed because most inline TLS 339128d0a740SAndrew Gallatin * NICs keep crypto state only for in-order transmits. This means 339228d0a740SAndrew Gallatin * that to handle a TCP rexmit (which is out-of-order), the NIC must 339328d0a740SAndrew Gallatin * re-DMA the entire TLS record up to and including the current 339428d0a740SAndrew Gallatin * segment. This means that when re-transmitting the last ~1448 byte 339528d0a740SAndrew Gallatin * segment of a 16KB TLS record, we could wind up re-DMA'ing an order 339628d0a740SAndrew Gallatin * of magnitude more data than we are sending. This can cause the 339728d0a740SAndrew Gallatin * PCIe link to saturate well before the network, which can cause 339828d0a740SAndrew Gallatin * output drops, and a general loss of capacity. 339928d0a740SAndrew Gallatin */ 340028d0a740SAndrew Gallatin void 340128d0a740SAndrew Gallatin ktls_disable_ifnet(void *arg) 340228d0a740SAndrew Gallatin { 340328d0a740SAndrew Gallatin struct tcpcb *tp; 340428d0a740SAndrew Gallatin struct inpcb *inp; 340528d0a740SAndrew Gallatin struct socket *so; 340628d0a740SAndrew Gallatin struct ktls_session *tls; 340728d0a740SAndrew Gallatin 340828d0a740SAndrew Gallatin tp = arg; 34099eb0e832SGleb Smirnoff inp = tptoinpcb(tp); 341028d0a740SAndrew Gallatin INP_WLOCK_ASSERT(inp); 341128d0a740SAndrew Gallatin so = inp->inp_socket; 341228d0a740SAndrew Gallatin SOCK_LOCK(so); 341328d0a740SAndrew Gallatin tls = so->so_snd.sb_tls_info; 3414c0e4090eSAndrew Gallatin if (tp->t_nic_ktls_xmit_dis == 1) { 341528d0a740SAndrew Gallatin SOCK_UNLOCK(so); 341628d0a740SAndrew Gallatin return; 341728d0a740SAndrew Gallatin } 3418d24b032bSAndrew Gallatin 341928d0a740SAndrew Gallatin /* 3420c0e4090eSAndrew Gallatin * note that t_nic_ktls_xmit_dis is never cleared; disabling 3421c0e4090eSAndrew Gallatin * ifnet can only be done once per connection, so we never want 342228d0a740SAndrew Gallatin * to do it again 342328d0a740SAndrew Gallatin */ 342428d0a740SAndrew Gallatin 342528d0a740SAndrew Gallatin (void)ktls_hold(tls); 342628d0a740SAndrew Gallatin soref(so); 3427c0e4090eSAndrew Gallatin tp->t_nic_ktls_xmit_dis = 1; 342828d0a740SAndrew Gallatin SOCK_UNLOCK(so); 342928d0a740SAndrew Gallatin TASK_INIT(&tls->disable_ifnet_task, 0, ktls_disable_ifnet_help, tls); 343028d0a740SAndrew Gallatin (void)taskqueue_enqueue(taskqueue_thread, &tls->disable_ifnet_task); 343128d0a740SAndrew Gallatin } 3432