2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2014-2019 Netflix Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/domainset.h>
40 #include <sys/endian.h>
44 #include <sys/mutex.h>
45 #include <sys/rmlock.h>
47 #include <sys/protosw.h>
48 #include <sys/refcount.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
54 #include <sys/kthread.h>
56 #include <sys/vmmeter.h>
57 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
58 #include <machine/pcb.h>
60 #include <machine/vmparam.h>
62 #include <net/if_var.h>
64 #include <net/netisr.h>
65 #include <net/rss_config.h>
67 #include <net/route.h>
68 #include <net/route/nhop.h>
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #include <netinet/in_pcb.h>
73 #include <netinet/tcp_var.h>
75 #include <netinet/tcp_offload.h>
77 #include <opencrypto/cryptodev.h>
78 #include <opencrypto/ktls.h>
79 #include <vm/uma_dbg.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pagequeue.h>
87 STAILQ_HEAD(, mbuf) m_head;
88 STAILQ_HEAD(, socket) so_head;
91 } __aligned(CACHE_LINE_SIZE);
93 struct ktls_alloc_thread {
100 struct ktls_domain_info {
103 struct ktls_alloc_thread alloc_td;
106 struct ktls_domain_info ktls_domains[MAXMEMDOM];
107 static struct ktls_wq *ktls_wq;
108 static struct proc *ktls_proc;
109 static uma_zone_t ktls_session_zone;
110 static uma_zone_t ktls_buffer_zone;
111 static uint16_t ktls_cpuid_lookup[MAXCPU];
112 static int ktls_init_state;
113 static struct sx ktls_init_lock;
114 SX_SYSINIT(ktls_init_lock, &ktls_init_lock, "ktls init");
116 SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
117 "Kernel TLS offload");
118 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
119 "Kernel TLS offload stats");
122 static int ktls_bind_threads = 1;
124 static int ktls_bind_threads;
126 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN,
127 &ktls_bind_threads, 0,
128 "Bind crypto threads to cores (1) or cores and domains (2) at boot");
130 static u_int ktls_maxlen = 16384;
131 SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RDTUN,
132 &ktls_maxlen, 0, "Maximum TLS record size");
134 static int ktls_number_threads;
135 SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD,
136 &ktls_number_threads, 0,
137 "Number of TLS threads in thread-pool");
139 unsigned int ktls_ifnet_max_rexmit_pct = 2;
140 SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, ifnet_max_rexmit_pct, CTLFLAG_RWTUN,
141 &ktls_ifnet_max_rexmit_pct, 2,
142 "Max percent bytes retransmitted before ifnet TLS is disabled");
144 static bool ktls_offload_enable;
145 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RWTUN,
146 &ktls_offload_enable, 0,
147 "Enable support for kernel TLS offload");
149 static bool ktls_cbc_enable = true;
150 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RWTUN,
152 "Enable Support of AES-CBC crypto for kernel TLS");
154 static bool ktls_sw_buffer_cache = true;
155 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, sw_buffer_cache, CTLFLAG_RDTUN,
156 &ktls_sw_buffer_cache, 1,
157 "Enable caching of output buffers for SW encryption");
159 static int ktls_max_alloc = 128;
160 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, max_alloc, CTLFLAG_RWTUN,
161 &ktls_max_alloc, 128,
162 "Max number of 16k buffers to allocate in thread context");
164 static COUNTER_U64_DEFINE_EARLY(ktls_tasks_active);
165 SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD,
166 &ktls_tasks_active, "Number of active tasks");
168 static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_pending);
169 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_pending, CTLFLAG_RD,
170 &ktls_cnt_tx_pending,
171 "Number of TLS 1.0 records waiting for earlier TLS records");
173 static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_queued);
174 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_inqueue, CTLFLAG_RD,
176 "Number of TLS records in queue to tasks for SW encryption");
178 static COUNTER_U64_DEFINE_EARLY(ktls_cnt_rx_queued);
179 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_rx_inqueue, CTLFLAG_RD,
181 "Number of TLS sockets in queue to tasks for SW decryption");
183 static COUNTER_U64_DEFINE_EARLY(ktls_offload_total);
184 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total,
185 CTLFLAG_RD, &ktls_offload_total,
186 "Total successful TLS setups (parameters set)");
188 static COUNTER_U64_DEFINE_EARLY(ktls_offload_enable_calls);
189 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls,
190 CTLFLAG_RD, &ktls_offload_enable_calls,
191 "Total number of TLS enable calls made");
193 static COUNTER_U64_DEFINE_EARLY(ktls_offload_active);
194 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD,
195 &ktls_offload_active, "Total Active TLS sessions");
197 static COUNTER_U64_DEFINE_EARLY(ktls_offload_corrupted_records);
198 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, corrupted_records, CTLFLAG_RD,
199 &ktls_offload_corrupted_records, "Total corrupted TLS records received");
201 static COUNTER_U64_DEFINE_EARLY(ktls_offload_failed_crypto);
202 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD,
203 &ktls_offload_failed_crypto, "Total TLS crypto failures");
205 static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_ifnet);
206 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD,
207 &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet");
209 static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_sw);
210 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD,
211 &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW");
213 static COUNTER_U64_DEFINE_EARLY(ktls_switch_failed);
214 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD,
215 &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet");
217 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_fail);
218 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_failed, CTLFLAG_RD,
219 &ktls_ifnet_disable_fail, "TLS sessions unable to switch to SW from ifnet");
221 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_ok);
222 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_ok, CTLFLAG_RD,
223 &ktls_ifnet_disable_ok, "TLS sessions able to switch to SW from ifnet");
225 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
226 "Software TLS session stats");
227 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
228 "Hardware (ifnet) TLS session stats");
230 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
231 "TOE TLS session stats");
234 static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc);
235 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc,
236 "Active number of software TLS sessions using AES-CBC");
238 static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm);
239 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm,
240 "Active number of software TLS sessions using AES-GCM");
242 static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20);
243 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD,
245 "Active number of software TLS sessions using Chacha20-Poly1305");
247 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc);
248 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD,
250 "Active number of ifnet TLS sessions using AES-CBC");
252 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm);
253 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD,
255 "Active number of ifnet TLS sessions using AES-GCM");
257 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20);
258 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD,
259 &ktls_ifnet_chacha20,
260 "Active number of ifnet TLS sessions using Chacha20-Poly1305");
262 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset);
263 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD,
264 &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag");
266 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped);
267 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD,
268 &ktls_ifnet_reset_dropped,
269 "TLS sessions dropped after failing to update ifnet send tag");
271 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed);
272 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD,
273 &ktls_ifnet_reset_failed,
274 "TLS sessions that failed to allocate a new ifnet send tag");
276 static int ktls_ifnet_permitted;
277 SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN,
278 &ktls_ifnet_permitted, 1,
279 "Whether to permit hardware (ifnet) TLS sessions");
282 static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc);
283 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD,
285 "Active number of TOE TLS sessions using AES-CBC");
287 static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm);
288 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD,
290 "Active number of TOE TLS sessions using AES-GCM");
292 static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20);
293 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD,
295 "Active number of TOE TLS sessions using Chacha20-Poly1305");
298 static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS");
300 static void ktls_cleanup(struct ktls_session *tls);
301 #if defined(INET) || defined(INET6)
302 static void ktls_reset_send_tag(void *context, int pending);
304 static void ktls_work_thread(void *ctx);
305 static void ktls_alloc_thread(void *ctx);
307 #if defined(INET) || defined(INET6)
309 ktls_get_cpu(struct socket *so)
313 struct ktls_domain_info *di;
319 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
320 if (cpuid != NETISR_CPUID_NONE)
324 * Just use the flowid to shard connections in a repeatable
325 * fashion. Note that TLS 1.0 sessions rely on the
326 * serialization provided by having the same connection use
330 if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) {
331 di = &ktls_domains[inp->inp_numa_domain];
332 cpuid = di->cpu[inp->inp_flowid % di->count];
335 cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads];
341 ktls_buffer_import(void *arg, void **store, int count, int domain, int flags)
346 KASSERT((ktls_maxlen & PAGE_MASK) == 0,
347 ("%s: ktls max length %d is not page size-aligned",
348 __func__, ktls_maxlen));
350 req = VM_ALLOC_WIRED | VM_ALLOC_NODUMP | malloc2vm_flags(flags);
351 for (i = 0; i < count; i++) {
352 m = vm_page_alloc_noobj_contig_domain(domain, req,
353 atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0,
357 store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
363 ktls_buffer_release(void *arg __unused, void **store, int count)
368 for (i = 0; i < count; i++) {
369 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i]));
370 for (j = 0; j < atop(ktls_maxlen); j++) {
371 (void)vm_page_unwire_noq(m + j);
378 ktls_free_mext_contig(struct mbuf *m)
381 uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0]));
389 int count, domain, error, i;
391 ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS,
394 ktls_session_zone = uma_zcreate("ktls_session",
395 sizeof(struct ktls_session),
396 NULL, NULL, NULL, NULL,
399 if (ktls_sw_buffer_cache) {
400 ktls_buffer_zone = uma_zcache_create("ktls_buffers",
401 roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL,
402 ktls_buffer_import, ktls_buffer_release, NULL,
403 UMA_ZONE_FIRSTTOUCH);
407 * Initialize the workqueues to run the TLS work. We create a
408 * work queue for each CPU.
411 STAILQ_INIT(&ktls_wq[i].m_head);
412 STAILQ_INIT(&ktls_wq[i].so_head);
413 mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF);
414 if (ktls_bind_threads > 1) {
416 domain = pc->pc_domain;
417 count = ktls_domains[domain].count;
418 ktls_domains[domain].cpu[count] = i;
419 ktls_domains[domain].count++;
421 ktls_cpuid_lookup[ktls_number_threads] = i;
422 ktls_number_threads++;
426 * If we somehow have an empty domain, fall back to choosing
427 * among all KTLS threads.
429 if (ktls_bind_threads > 1) {
430 for (i = 0; i < vm_ndomains; i++) {
431 if (ktls_domains[i].count == 0) {
432 ktls_bind_threads = 1;
438 /* Start kthreads for each workqueue. */
440 error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i],
441 &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i);
443 printf("Can't add KTLS thread %d error %d\n", i, error);
449 * Start an allocation thread per-domain to perform blocking allocations
450 * of 16k physically contiguous TLS crypto destination buffers.
452 if (ktls_sw_buffer_cache) {
453 for (domain = 0; domain < vm_ndomains; domain++) {
454 if (VM_DOMAIN_EMPTY(domain))
456 if (CPU_EMPTY(&cpuset_domain[domain]))
458 error = kproc_kthread_add(ktls_alloc_thread,
459 &ktls_domains[domain], &ktls_proc,
460 &ktls_domains[domain].alloc_td.td,
461 0, 0, "KTLS", "alloc_%d", domain);
463 printf("Can't add KTLS alloc thread %d error %d\n",
471 printf("KTLS: Initialized %d threads\n", ktls_number_threads);
476 ktls_start_kthreads(void)
481 state = atomic_load_acq_int(&ktls_init_state);
482 if (__predict_true(state > 0))
487 sx_xlock(&ktls_init_lock);
488 if (ktls_init_state != 0) {
489 sx_xunlock(&ktls_init_lock);
498 atomic_store_rel_int(&ktls_init_state, state);
499 sx_xunlock(&ktls_init_lock);
503 #if defined(INET) || defined(INET6)
505 ktls_create_session(struct socket *so, struct tls_enable *en,
506 struct ktls_session **tlsp)
508 struct ktls_session *tls;
511 /* Only TLS 1.0 - 1.3 are supported. */
512 if (en->tls_vmajor != TLS_MAJOR_VER_ONE)
514 if (en->tls_vminor < TLS_MINOR_VER_ZERO ||
515 en->tls_vminor > TLS_MINOR_VER_THREE)
518 if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE)
520 if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE)
522 if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv))
525 /* All supported algorithms require a cipher key. */
526 if (en->cipher_key_len == 0)
529 /* No flags are currently supported. */
533 /* Common checks for supported algorithms. */
534 switch (en->cipher_algorithm) {
535 case CRYPTO_AES_NIST_GCM_16:
537 * auth_algorithm isn't used, but permit GMAC values
540 switch (en->auth_algorithm) {
542 #ifdef COMPAT_FREEBSD12
543 /* XXX: Really 13.0-current COMPAT. */
544 case CRYPTO_AES_128_NIST_GMAC:
545 case CRYPTO_AES_192_NIST_GMAC:
546 case CRYPTO_AES_256_NIST_GMAC:
552 if (en->auth_key_len != 0)
554 switch (en->tls_vminor) {
555 case TLS_MINOR_VER_TWO:
556 if (en->iv_len != TLS_AEAD_GCM_LEN)
559 case TLS_MINOR_VER_THREE:
560 if (en->iv_len != TLS_1_3_GCM_IV_LEN)
568 switch (en->auth_algorithm) {
569 case CRYPTO_SHA1_HMAC:
571 case CRYPTO_SHA2_256_HMAC:
572 case CRYPTO_SHA2_384_HMAC:
573 if (en->tls_vminor != TLS_MINOR_VER_TWO)
579 if (en->auth_key_len == 0)
583 * TLS 1.0 requires an implicit IV. TLS 1.1 and 1.2
586 switch (en->tls_vminor) {
587 case TLS_MINOR_VER_ZERO:
588 if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN)
591 case TLS_MINOR_VER_ONE:
592 case TLS_MINOR_VER_TWO:
593 /* Ignore any supplied IV. */
600 case CRYPTO_CHACHA20_POLY1305:
601 if (en->auth_algorithm != 0 || en->auth_key_len != 0)
603 if (en->tls_vminor != TLS_MINOR_VER_TWO &&
604 en->tls_vminor != TLS_MINOR_VER_THREE)
606 if (en->iv_len != TLS_CHACHA20_IV_LEN)
613 error = ktls_start_kthreads();
617 tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
619 counter_u64_add(ktls_offload_active, 1);
621 refcount_init(&tls->refcount, 1);
622 TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls);
624 tls->wq_index = ktls_get_cpu(so);
626 tls->params.cipher_algorithm = en->cipher_algorithm;
627 tls->params.auth_algorithm = en->auth_algorithm;
628 tls->params.tls_vmajor = en->tls_vmajor;
629 tls->params.tls_vminor = en->tls_vminor;
630 tls->params.flags = en->flags;
631 tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen);
633 /* Set the header and trailer lengths. */
634 tls->params.tls_hlen = sizeof(struct tls_record_layer);
635 switch (en->cipher_algorithm) {
636 case CRYPTO_AES_NIST_GCM_16:
638 * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte
639 * nonce. TLS 1.3 uses a 12 byte implicit IV.
641 if (en->tls_vminor < TLS_MINOR_VER_THREE)
642 tls->params.tls_hlen += sizeof(uint64_t);
643 tls->params.tls_tlen = AES_GMAC_HASH_LEN;
644 tls->params.tls_bs = 1;
647 switch (en->auth_algorithm) {
648 case CRYPTO_SHA1_HMAC:
649 if (en->tls_vminor == TLS_MINOR_VER_ZERO) {
650 /* Implicit IV, no nonce. */
651 tls->sequential_records = true;
652 tls->next_seqno = be64dec(en->rec_seq);
653 STAILQ_INIT(&tls->pending_records);
655 tls->params.tls_hlen += AES_BLOCK_LEN;
657 tls->params.tls_tlen = AES_BLOCK_LEN +
660 case CRYPTO_SHA2_256_HMAC:
661 tls->params.tls_hlen += AES_BLOCK_LEN;
662 tls->params.tls_tlen = AES_BLOCK_LEN +
665 case CRYPTO_SHA2_384_HMAC:
666 tls->params.tls_hlen += AES_BLOCK_LEN;
667 tls->params.tls_tlen = AES_BLOCK_LEN +
671 panic("invalid hmac");
673 tls->params.tls_bs = AES_BLOCK_LEN;
675 case CRYPTO_CHACHA20_POLY1305:
677 * Chacha20 uses a 12 byte implicit IV.
679 tls->params.tls_tlen = POLY1305_HASH_LEN;
680 tls->params.tls_bs = 1;
683 panic("invalid cipher");
687 * TLS 1.3 includes optional padding which we do not support,
688 * and also puts the "real" record type at the end of the
691 if (en->tls_vminor == TLS_MINOR_VER_THREE)
692 tls->params.tls_tlen += sizeof(uint8_t);
694 KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN,
695 ("TLS header length too long: %d", tls->params.tls_hlen));
696 KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN,
697 ("TLS trailer length too long: %d", tls->params.tls_tlen));
699 if (en->auth_key_len != 0) {
700 tls->params.auth_key_len = en->auth_key_len;
701 tls->params.auth_key = malloc(en->auth_key_len, M_KTLS,
703 error = copyin(en->auth_key, tls->params.auth_key,
709 tls->params.cipher_key_len = en->cipher_key_len;
710 tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK);
711 error = copyin(en->cipher_key, tls->params.cipher_key,
717 * This holds the implicit portion of the nonce for AEAD
718 * ciphers and the initial implicit IV for TLS 1.0. The
719 * explicit portions of the IV are generated in ktls_frame().
721 if (en->iv_len != 0) {
722 tls->params.iv_len = en->iv_len;
723 error = copyin(en->iv, tls->params.iv, en->iv_len);
728 * For TLS 1.2 with GCM, generate an 8-byte nonce as a
729 * counter to generate unique explicit IVs.
731 * Store this counter in the last 8 bytes of the IV
732 * array so that it is 8-byte aligned.
734 if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
735 en->tls_vminor == TLS_MINOR_VER_TWO)
736 arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0);
747 static struct ktls_session *
748 ktls_clone_session(struct ktls_session *tls)
750 struct ktls_session *tls_new;
752 tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
754 counter_u64_add(ktls_offload_active, 1);
756 refcount_init(&tls_new->refcount, 1);
757 TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_send_tag, tls_new);
759 /* Copy fields from existing session. */
760 tls_new->params = tls->params;
761 tls_new->wq_index = tls->wq_index;
763 /* Deep copy keys. */
764 if (tls_new->params.auth_key != NULL) {
765 tls_new->params.auth_key = malloc(tls->params.auth_key_len,
767 memcpy(tls_new->params.auth_key, tls->params.auth_key,
768 tls->params.auth_key_len);
771 tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS,
773 memcpy(tls_new->params.cipher_key, tls->params.cipher_key,
774 tls->params.cipher_key_len);
781 ktls_cleanup(struct ktls_session *tls)
784 counter_u64_add(ktls_offload_active, -1);
786 case TCP_TLS_MODE_SW:
787 switch (tls->params.cipher_algorithm) {
789 counter_u64_add(ktls_sw_cbc, -1);
791 case CRYPTO_AES_NIST_GCM_16:
792 counter_u64_add(ktls_sw_gcm, -1);
794 case CRYPTO_CHACHA20_POLY1305:
795 counter_u64_add(ktls_sw_chacha20, -1);
799 case TCP_TLS_MODE_IFNET:
800 switch (tls->params.cipher_algorithm) {
802 counter_u64_add(ktls_ifnet_cbc, -1);
804 case CRYPTO_AES_NIST_GCM_16:
805 counter_u64_add(ktls_ifnet_gcm, -1);
807 case CRYPTO_CHACHA20_POLY1305:
808 counter_u64_add(ktls_ifnet_chacha20, -1);
811 if (tls->snd_tag != NULL)
812 m_snd_tag_rele(tls->snd_tag);
815 case TCP_TLS_MODE_TOE:
816 switch (tls->params.cipher_algorithm) {
818 counter_u64_add(ktls_toe_cbc, -1);
820 case CRYPTO_AES_NIST_GCM_16:
821 counter_u64_add(ktls_toe_gcm, -1);
823 case CRYPTO_CHACHA20_POLY1305:
824 counter_u64_add(ktls_toe_chacha20, -1);
830 if (tls->ocf_session != NULL)
832 if (tls->params.auth_key != NULL) {
833 zfree(tls->params.auth_key, M_KTLS);
834 tls->params.auth_key = NULL;
835 tls->params.auth_key_len = 0;
837 if (tls->params.cipher_key != NULL) {
838 zfree(tls->params.cipher_key, M_KTLS);
839 tls->params.cipher_key = NULL;
840 tls->params.cipher_key_len = 0;
842 explicit_bzero(tls->params.iv, sizeof(tls->params.iv));
845 #if defined(INET) || defined(INET6)
849 ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction)
857 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
861 if (inp->inp_socket == NULL) {
866 if (!(tp->t_flags & TF_TOE)) {
871 error = tcp_offload_alloc_tls_session(tp, tls, direction);
874 tls->mode = TCP_TLS_MODE_TOE;
875 switch (tls->params.cipher_algorithm) {
877 counter_u64_add(ktls_toe_cbc, 1);
879 case CRYPTO_AES_NIST_GCM_16:
880 counter_u64_add(ktls_toe_gcm, 1);
882 case CRYPTO_CHACHA20_POLY1305:
883 counter_u64_add(ktls_toe_chacha20, 1);
892 * Common code used when first enabling ifnet TLS on a connection or
893 * when allocating a new ifnet TLS session due to a routing change.
894 * This function allocates a new TLS send tag on whatever interface
895 * the connection is currently routed over.
898 ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force,
899 struct m_snd_tag **mstp)
901 union if_snd_tag_alloc_params params;
903 struct nhop_object *nh;
908 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
912 if (inp->inp_socket == NULL) {
919 * Check administrative controls on ifnet TLS to determine if
920 * ifnet TLS should be denied.
922 * - Always permit 'force' requests.
923 * - ktls_ifnet_permitted == 0: always deny.
925 if (!force && ktls_ifnet_permitted == 0) {
931 * XXX: Use the cached route in the inpcb to find the
932 * interface. This should perhaps instead use
933 * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only
934 * enabled after a connection has completed key negotiation in
935 * userland, the cached route will be present in practice.
937 nh = inp->inp_route.ro_nh;
946 * Allocate a TLS + ratelimit tag if the connection has an
947 * existing pacing rate.
949 if (tp->t_pacing_rate != -1 &&
950 (ifp->if_capenable & IFCAP_TXTLS_RTLMT) != 0) {
951 params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT;
952 params.tls_rate_limit.inp = inp;
953 params.tls_rate_limit.tls = tls;
954 params.tls_rate_limit.max_rate = tp->t_pacing_rate;
956 params.hdr.type = IF_SND_TAG_TYPE_TLS;
957 params.tls.inp = inp;
958 params.tls.tls = tls;
960 params.hdr.flowid = inp->inp_flowid;
961 params.hdr.flowtype = inp->inp_flowtype;
962 params.hdr.numa_domain = inp->inp_numa_domain;
965 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
969 if (inp->inp_vflag & INP_IPV6) {
970 if ((ifp->if_capenable & IFCAP_TXTLS6) == 0) {
975 if ((ifp->if_capenable & IFCAP_TXTLS4) == 0) {
980 error = m_snd_tag_alloc(ifp, ¶ms, mstp);
987 ktls_try_ifnet(struct socket *so, struct ktls_session *tls, bool force)
989 struct m_snd_tag *mst;
992 error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst);
994 tls->mode = TCP_TLS_MODE_IFNET;
996 switch (tls->params.cipher_algorithm) {
998 counter_u64_add(ktls_ifnet_cbc, 1);
1000 case CRYPTO_AES_NIST_GCM_16:
1001 counter_u64_add(ktls_ifnet_gcm, 1);
1003 case CRYPTO_CHACHA20_POLY1305:
1004 counter_u64_add(ktls_ifnet_chacha20, 1);
1012 ktls_use_sw(struct ktls_session *tls)
1014 tls->mode = TCP_TLS_MODE_SW;
1015 switch (tls->params.cipher_algorithm) {
1016 case CRYPTO_AES_CBC:
1017 counter_u64_add(ktls_sw_cbc, 1);
1019 case CRYPTO_AES_NIST_GCM_16:
1020 counter_u64_add(ktls_sw_gcm, 1);
1022 case CRYPTO_CHACHA20_POLY1305:
1023 counter_u64_add(ktls_sw_chacha20, 1);
1029 ktls_try_sw(struct socket *so, struct ktls_session *tls, int direction)
1033 error = ktls_ocf_try(so, tls, direction);
1041 * KTLS RX stores data in the socket buffer as a list of TLS records,
1042 * where each record is stored as a control message containg the TLS
1043 * header followed by data mbufs containing the decrypted data. This
1044 * is different from KTLS TX which always uses an mb_ext_pgs mbuf for
1045 * both encrypted and decrypted data. TLS records decrypted by a NIC
1046 * should be queued to the socket buffer as records, but encrypted
1047 * data which needs to be decrypted by software arrives as a stream of
1048 * regular mbufs which need to be converted. In addition, there may
1049 * already be pending encrypted data in the socket buffer when KTLS RX
1052 * To manage not-yet-decrypted data for KTLS RX, the following scheme
1055 * - A single chain of NOTREADY mbufs is hung off of sb_mtls.
1057 * - ktls_check_rx checks this chain of mbufs reading the TLS header
1058 * from the first mbuf. Once all of the data for that TLS record is
1059 * queued, the socket is queued to a worker thread.
1061 * - The worker thread calls ktls_decrypt to decrypt TLS records in
1062 * the TLS chain. Each TLS record is detached from the TLS chain,
1063 * decrypted, and inserted into the regular socket buffer chain as
1064 * record starting with a control message holding the TLS header and
1065 * a chain of mbufs holding the encrypted data.
1069 sb_mark_notready(struct sockbuf *sb)
1076 sb->sb_mbtail = NULL;
1077 sb->sb_lastrecord = NULL;
1078 for (; m != NULL; m = m->m_next) {
1079 KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL",
1081 KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail",
1083 KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len",
1085 m->m_flags |= M_NOTREADY;
1086 sb->sb_acc -= m->m_len;
1087 sb->sb_tlscc += m->m_len;
1088 sb->sb_mtlstail = m;
1090 KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc,
1091 ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc,
1096 * Return information about the pending TLS data in a socket
1097 * buffer. On return, 'seqno' is set to the sequence number
1098 * of the next TLS record to be received, 'resid' is set to
1099 * the amount of bytes still needed for the last pending
1100 * record. The function returns 'false' if the last pending
1101 * record contains a partial TLS header. In that case, 'resid'
1102 * is the number of bytes needed to complete the TLS header.
1105 ktls_pending_rx_info(struct sockbuf *sb, uint64_t *seqnop, size_t *residp)
1107 struct tls_record_layer hdr;
1111 u_int offset, record_len;
1113 SOCKBUF_LOCK_ASSERT(sb);
1114 MPASS(sb->sb_flags & SB_TLS_RX);
1115 seqno = sb->sb_tls_seqno;
1116 resid = sb->sb_tlscc;
1129 if (resid < sizeof(hdr)) {
1131 *residp = sizeof(hdr) - resid;
1135 m_copydata(m, offset, sizeof(hdr), (void *)&hdr);
1137 record_len = sizeof(hdr) + ntohs(hdr.tls_length);
1138 if (resid <= record_len) {
1140 *residp = record_len - resid;
1143 resid -= record_len;
1145 while (record_len != 0) {
1146 if (m->m_len - offset > record_len) {
1147 offset += record_len;
1151 record_len -= (m->m_len - offset);
1159 ktls_enable_rx(struct socket *so, struct tls_enable *en)
1161 struct ktls_session *tls;
1164 if (!ktls_offload_enable)
1166 if (SOLISTENING(so))
1169 counter_u64_add(ktls_offload_enable_calls, 1);
1172 * This should always be true since only the TCP socket option
1173 * invokes this function.
1175 if (so->so_proto->pr_protocol != IPPROTO_TCP)
1179 * XXX: Don't overwrite existing sessions. We should permit
1180 * this to support rekeying in the future.
1182 if (so->so_rcv.sb_tls_info != NULL)
1185 if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
1188 error = ktls_create_session(so, en, &tls);
1192 error = ktls_ocf_try(so, tls, KTLS_RX);
1198 /* Mark the socket as using TLS offload. */
1199 SOCKBUF_LOCK(&so->so_rcv);
1200 so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq);
1201 so->so_rcv.sb_tls_info = tls;
1202 so->so_rcv.sb_flags |= SB_TLS_RX;
1204 /* Mark existing data as not ready until it can be decrypted. */
1205 sb_mark_notready(&so->so_rcv);
1206 ktls_check_rx(&so->so_rcv);
1207 SOCKBUF_UNLOCK(&so->so_rcv);
1210 error = ktls_try_toe(so, tls, KTLS_RX);
1215 counter_u64_add(ktls_offload_total, 1);
1221 ktls_enable_tx(struct socket *so, struct tls_enable *en)
1223 struct ktls_session *tls;
1227 if (!ktls_offload_enable)
1229 if (SOLISTENING(so))
1232 counter_u64_add(ktls_offload_enable_calls, 1);
1235 * This should always be true since only the TCP socket option
1236 * invokes this function.
1238 if (so->so_proto->pr_protocol != IPPROTO_TCP)
1242 * XXX: Don't overwrite existing sessions. We should permit
1243 * this to support rekeying in the future.
1245 if (so->so_snd.sb_tls_info != NULL)
1248 if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
1251 /* TLS requires ext pgs */
1252 if (mb_use_ext_pgs == 0)
1255 error = ktls_create_session(so, en, &tls);
1259 /* Prefer TOE -> ifnet TLS -> software TLS. */
1261 error = ktls_try_toe(so, tls, KTLS_TX);
1264 error = ktls_try_ifnet(so, tls, false);
1266 error = ktls_try_sw(so, tls, KTLS_TX);
1273 error = SOCK_IO_SEND_LOCK(so, SBL_WAIT);
1280 * Write lock the INP when setting sb_tls_info so that
1281 * routines in tcp_ratelimit.c can read sb_tls_info while
1282 * holding the INP lock.
1286 SOCKBUF_LOCK(&so->so_snd);
1287 so->so_snd.sb_tls_seqno = be64dec(en->rec_seq);
1288 so->so_snd.sb_tls_info = tls;
1289 if (tls->mode != TCP_TLS_MODE_SW)
1290 so->so_snd.sb_flags |= SB_TLS_IFNET;
1291 SOCKBUF_UNLOCK(&so->so_snd);
1293 SOCK_IO_SEND_UNLOCK(so);
1295 counter_u64_add(ktls_offload_total, 1);
1301 ktls_get_rx_mode(struct socket *so, int *modep)
1303 struct ktls_session *tls;
1304 struct inpcb *inp __diagused;
1306 if (SOLISTENING(so))
1309 INP_WLOCK_ASSERT(inp);
1310 SOCK_RECVBUF_LOCK(so);
1311 tls = so->so_rcv.sb_tls_info;
1313 *modep = TCP_TLS_MODE_NONE;
1316 SOCK_RECVBUF_UNLOCK(so);
1321 * ktls_get_rx_sequence - get the next TCP- and TLS- sequence number.
1323 * This function gets information about the next TCP- and TLS-
1324 * sequence number to be processed by the TLS receive worker
1325 * thread. The information is extracted from the given "inpcb"
1326 * structure. The values are stored in host endian format at the two
1327 * given output pointer locations. The TCP sequence number points to
1328 * the beginning of the TLS header.
1330 * This function returns zero on success, else a non-zero error code
1334 ktls_get_rx_sequence(struct inpcb *inp, uint32_t *tcpseq, uint64_t *tlsseq)
1340 so = inp->inp_socket;
1341 if (__predict_false(so == NULL)) {
1345 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1347 return (ECONNRESET);
1350 tp = intotcpcb(inp);
1353 SOCKBUF_LOCK(&so->so_rcv);
1354 *tcpseq = tp->rcv_nxt - so->so_rcv.sb_tlscc;
1355 *tlsseq = so->so_rcv.sb_tls_seqno;
1356 SOCKBUF_UNLOCK(&so->so_rcv);
1364 ktls_get_tx_mode(struct socket *so, int *modep)
1366 struct ktls_session *tls;
1367 struct inpcb *inp __diagused;
1369 if (SOLISTENING(so))
1372 INP_WLOCK_ASSERT(inp);
1373 SOCK_SENDBUF_LOCK(so);
1374 tls = so->so_snd.sb_tls_info;
1376 *modep = TCP_TLS_MODE_NONE;
1379 SOCK_SENDBUF_UNLOCK(so);
1384 * Switch between SW and ifnet TLS sessions as requested.
1387 ktls_set_tx_mode(struct socket *so, int mode)
1389 struct ktls_session *tls, *tls_new;
1393 if (SOLISTENING(so))
1396 case TCP_TLS_MODE_SW:
1397 case TCP_TLS_MODE_IFNET:
1404 INP_WLOCK_ASSERT(inp);
1405 SOCKBUF_LOCK(&so->so_snd);
1406 tls = so->so_snd.sb_tls_info;
1408 SOCKBUF_UNLOCK(&so->so_snd);
1412 if (tls->mode == mode) {
1413 SOCKBUF_UNLOCK(&so->so_snd);
1417 tls = ktls_hold(tls);
1418 SOCKBUF_UNLOCK(&so->so_snd);
1421 tls_new = ktls_clone_session(tls);
1423 if (mode == TCP_TLS_MODE_IFNET)
1424 error = ktls_try_ifnet(so, tls_new, true);
1426 error = ktls_try_sw(so, tls_new, KTLS_TX);
1428 counter_u64_add(ktls_switch_failed, 1);
1435 error = SOCK_IO_SEND_LOCK(so, SBL_WAIT);
1437 counter_u64_add(ktls_switch_failed, 1);
1445 * If we raced with another session change, keep the existing
1448 if (tls != so->so_snd.sb_tls_info) {
1449 counter_u64_add(ktls_switch_failed, 1);
1450 SOCK_IO_SEND_UNLOCK(so);
1458 SOCKBUF_LOCK(&so->so_snd);
1459 so->so_snd.sb_tls_info = tls_new;
1460 if (tls_new->mode != TCP_TLS_MODE_SW)
1461 so->so_snd.sb_flags |= SB_TLS_IFNET;
1462 SOCKBUF_UNLOCK(&so->so_snd);
1463 SOCK_IO_SEND_UNLOCK(so);
1466 * Drop two references on 'tls'. The first is for the
1467 * ktls_hold() above. The second drops the reference from the
1470 KASSERT(tls->refcount >= 2, ("too few references on old session"));
1474 if (mode == TCP_TLS_MODE_IFNET)
1475 counter_u64_add(ktls_switch_to_ifnet, 1);
1477 counter_u64_add(ktls_switch_to_sw, 1);
1483 * Try to allocate a new TLS send tag. This task is scheduled when
1484 * ip_output detects a route change while trying to transmit a packet
1485 * holding a TLS record. If a new tag is allocated, replace the tag
1486 * in the TLS session. Subsequent packets on the connection will use
1487 * the new tag. If a new tag cannot be allocated, drop the
1491 ktls_reset_send_tag(void *context, int pending)
1493 struct epoch_tracker et;
1494 struct ktls_session *tls;
1495 struct m_snd_tag *old, *new;
1500 MPASS(pending == 1);
1506 * Free the old tag first before allocating a new one.
1507 * ip[6]_output_send() will treat a NULL send tag the same as
1508 * an ifp mismatch and drop packets until a new tag is
1511 * Write-lock the INP when changing tls->snd_tag since
1512 * ip[6]_output_send() holds a read-lock when reading the
1517 tls->snd_tag = NULL;
1520 m_snd_tag_rele(old);
1522 error = ktls_alloc_snd_tag(inp, tls, true, &new);
1527 mtx_pool_lock(mtxpool_sleep, tls);
1528 tls->reset_pending = false;
1529 mtx_pool_unlock(mtxpool_sleep, tls);
1530 if (!in_pcbrele_wlocked(inp))
1533 counter_u64_add(ktls_ifnet_reset, 1);
1536 * XXX: Should we kick tcp_output explicitly now that
1537 * the send tag is fixed or just rely on timers?
1540 NET_EPOCH_ENTER(et);
1542 if (!in_pcbrele_wlocked(inp)) {
1543 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1544 !(inp->inp_flags & INP_DROPPED)) {
1545 tp = intotcpcb(inp);
1546 CURVNET_SET(tp->t_vnet);
1547 tp = tcp_drop(tp, ECONNABORTED);
1551 counter_u64_add(ktls_ifnet_reset_dropped, 1);
1557 counter_u64_add(ktls_ifnet_reset_failed, 1);
1560 * Leave reset_pending true to avoid future tasks while
1561 * the socket goes away.
1569 ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls)
1575 INP_LOCK_ASSERT(inp);
1578 * See if we should schedule a task to update the send tag for
1581 mtx_pool_lock(mtxpool_sleep, tls);
1582 if (!tls->reset_pending) {
1583 (void) ktls_hold(tls);
1586 tls->reset_pending = true;
1587 taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task);
1589 mtx_pool_unlock(mtxpool_sleep, tls);
1595 ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate)
1597 union if_snd_tag_modify_params params = {
1598 .rate_limit.max_rate = max_pacing_rate,
1599 .rate_limit.flags = M_NOWAIT,
1601 struct m_snd_tag *mst;
1603 /* Can't get to the inp, but it should be locked. */
1604 /* INP_LOCK_ASSERT(inp); */
1606 MPASS(tls->mode == TCP_TLS_MODE_IFNET);
1608 if (tls->snd_tag == NULL) {
1610 * Resetting send tag, ignore this change. The
1611 * pending reset may or may not see this updated rate
1612 * in the tcpcb. If it doesn't, we will just lose
1618 MPASS(tls->snd_tag != NULL);
1619 MPASS(tls->snd_tag->sw->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT);
1622 return (mst->sw->snd_tag_modify(mst, ¶ms));
1628 ktls_destroy(struct ktls_session *tls)
1631 if (tls->sequential_records) {
1635 STAILQ_FOREACH_SAFE(m, &tls->pending_records, m_epg_stailq, n) {
1636 page_count = m->m_epg_enc_cnt;
1637 while (page_count > 0) {
1638 KASSERT(page_count >= m->m_epg_nrdy,
1639 ("%s: too few pages", __func__));
1640 page_count -= m->m_epg_nrdy;
1646 uma_zfree(ktls_session_zone, tls);
1650 ktls_seq(struct sockbuf *sb, struct mbuf *m)
1653 for (; m != NULL; m = m->m_next) {
1654 KASSERT((m->m_flags & M_EXTPG) != 0,
1655 ("ktls_seq: mapped mbuf %p", m));
1657 m->m_epg_seqno = sb->sb_tls_seqno;
1663 * Add TLS framing (headers and trailers) to a chain of mbufs. Each
1664 * mbuf in the chain must be an unmapped mbuf. The payload of the
1665 * mbuf must be populated with the payload of each TLS record.
1667 * The record_type argument specifies the TLS record type used when
1668 * populating the TLS header.
1670 * The enq_count argument on return is set to the number of pages of
1671 * payload data for this entire chain that need to be encrypted via SW
1672 * encryption. The returned value should be passed to ktls_enqueue
1673 * when scheduling encryption of this chain of mbufs. To handle the
1674 * special case of empty fragments for TLS 1.0 sessions, an empty
1675 * fragment counts as one page.
1678 ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt,
1679 uint8_t record_type)
1681 struct tls_record_layer *tlshdr;
1685 int maxlen __diagused;
1687 maxlen = tls->params.max_frame_len;
1689 for (m = top; m != NULL; m = m->m_next) {
1691 * All mbufs in the chain should be TLS records whose
1692 * payload does not exceed the maximum frame length.
1694 * Empty TLS 1.0 records are permitted when using CBC.
1696 KASSERT(m->m_len <= maxlen && m->m_len >= 0 &&
1697 (m->m_len > 0 || ktls_permit_empty_frames(tls)),
1698 ("ktls_frame: m %p len %d", m, m->m_len));
1701 * TLS frames require unmapped mbufs to store session
1704 KASSERT((m->m_flags & M_EXTPG) != 0,
1705 ("ktls_frame: mapped mbuf %p (top = %p)", m, top));
1709 /* Save a reference to the session. */
1710 m->m_epg_tls = ktls_hold(tls);
1712 m->m_epg_hdrlen = tls->params.tls_hlen;
1713 m->m_epg_trllen = tls->params.tls_tlen;
1714 if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) {
1718 * AES-CBC pads messages to a multiple of the
1719 * block size. Note that the padding is
1720 * applied after the digest and the encryption
1721 * is done on the "plaintext || mac || padding".
1722 * At least one byte of padding is always
1725 * Compute the final trailer length assuming
1726 * at most one block of padding.
1727 * tls->params.tls_tlen is the maximum
1728 * possible trailer length (padding + digest).
1729 * delta holds the number of excess padding
1730 * bytes if the maximum were used. Those
1731 * extra bytes are removed.
1733 bs = tls->params.tls_bs;
1734 delta = (tls_len + tls->params.tls_tlen) & (bs - 1);
1735 m->m_epg_trllen -= delta;
1737 m->m_len += m->m_epg_hdrlen + m->m_epg_trllen;
1739 /* Populate the TLS header. */
1740 tlshdr = (void *)m->m_epg_hdr;
1741 tlshdr->tls_vmajor = tls->params.tls_vmajor;
1744 * TLS 1.3 masquarades as TLS 1.2 with a record type
1745 * of TLS_RLTYPE_APP.
1747 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE &&
1748 tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) {
1749 tlshdr->tls_vminor = TLS_MINOR_VER_TWO;
1750 tlshdr->tls_type = TLS_RLTYPE_APP;
1751 /* save the real record type for later */
1752 m->m_epg_record_type = record_type;
1753 m->m_epg_trail[0] = record_type;
1755 tlshdr->tls_vminor = tls->params.tls_vminor;
1756 tlshdr->tls_type = record_type;
1758 tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr));
1761 * Store nonces / explicit IVs after the end of the
1764 * For GCM with TLS 1.2, an 8 byte nonce is copied
1765 * from the end of the IV. The nonce is then
1766 * incremented for use by the next record.
1768 * For CBC, a random nonce is inserted for TLS 1.1+.
1770 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
1771 tls->params.tls_vminor == TLS_MINOR_VER_TWO) {
1772 noncep = (uint64_t *)(tls->params.iv + 8);
1773 be64enc(tlshdr + 1, *noncep);
1775 } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC &&
1776 tls->params.tls_vminor >= TLS_MINOR_VER_ONE)
1777 arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0);
1780 * When using SW encryption, mark the mbuf not ready.
1781 * It will be marked ready via sbready() after the
1782 * record has been encrypted.
1784 * When using ifnet TLS, unencrypted TLS records are
1785 * sent down the stack to the NIC.
1787 if (tls->mode == TCP_TLS_MODE_SW) {
1788 m->m_flags |= M_NOTREADY;
1789 if (__predict_false(tls_len == 0)) {
1790 /* TLS 1.0 empty fragment. */
1793 m->m_epg_nrdy = m->m_epg_npgs;
1794 *enq_cnt += m->m_epg_nrdy;
1800 ktls_permit_empty_frames(struct ktls_session *tls)
1802 return (tls->params.cipher_algorithm == CRYPTO_AES_CBC &&
1803 tls->params.tls_vminor == TLS_MINOR_VER_ZERO);
1807 ktls_check_rx(struct sockbuf *sb)
1809 struct tls_record_layer hdr;
1814 SOCKBUF_LOCK_ASSERT(sb);
1815 KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX",
1817 so = __containerof(sb, struct socket, so_rcv);
1819 if (sb->sb_flags & SB_TLS_RX_RUNNING)
1822 /* Is there enough queued for a TLS header? */
1823 if (sb->sb_tlscc < sizeof(hdr)) {
1824 if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0)
1825 so->so_error = EMSGSIZE;
1829 m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr);
1831 /* Is the entire record queued? */
1832 if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) {
1833 if ((sb->sb_state & SBS_CANTRCVMORE) != 0)
1834 so->so_error = EMSGSIZE;
1838 sb->sb_flags |= SB_TLS_RX_RUNNING;
1841 wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index];
1843 STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list);
1844 running = wq->running;
1845 mtx_unlock(&wq->mtx);
1848 counter_u64_add(ktls_cnt_rx_queued, 1);
1851 static struct mbuf *
1852 ktls_detach_record(struct sockbuf *sb, int len)
1854 struct mbuf *m, *n, *top;
1857 SOCKBUF_LOCK_ASSERT(sb);
1858 MPASS(len <= sb->sb_tlscc);
1861 * If TLS chain is the exact size of the record,
1862 * just grab the whole record.
1865 if (sb->sb_tlscc == len) {
1867 sb->sb_mtlstail = NULL;
1872 * While it would be nice to use m_split() here, we need
1873 * to know exactly what m_split() allocates to update the
1874 * accounting, so do it inline instead.
1877 for (m = top; remain > m->m_len; m = m->m_next)
1880 /* Easy case: don't have to split 'm'. */
1881 if (remain == m->m_len) {
1882 sb->sb_mtls = m->m_next;
1883 if (sb->sb_mtls == NULL)
1884 sb->sb_mtlstail = NULL;
1890 * Need to allocate an mbuf to hold the remainder of 'm'. Try
1891 * with M_NOWAIT first.
1893 n = m_get(M_NOWAIT, MT_DATA);
1896 * Use M_WAITOK with socket buffer unlocked. If
1897 * 'sb_mtls' changes while the lock is dropped, return
1898 * NULL to force the caller to retry.
1902 n = m_get(M_WAITOK, MT_DATA);
1905 if (sb->sb_mtls != top) {
1910 n->m_flags |= M_NOTREADY;
1912 /* Store remainder in 'n'. */
1913 n->m_len = m->m_len - remain;
1914 if (m->m_flags & M_EXT) {
1915 n->m_data = m->m_data + remain;
1918 bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len);
1921 /* Trim 'm' and update accounting. */
1922 m->m_len -= n->m_len;
1923 sb->sb_tlscc -= n->m_len;
1924 sb->sb_ccc -= n->m_len;
1926 /* Account for 'n'. */
1927 sballoc_ktls_rx(sb, n);
1929 /* Insert 'n' into the TLS chain. */
1931 n->m_next = m->m_next;
1932 if (sb->sb_mtlstail == m)
1933 sb->sb_mtlstail = n;
1935 /* Detach the record from the TLS chain. */
1939 MPASS(m_length(top, NULL) == len);
1940 for (m = top; m != NULL; m = m->m_next)
1941 sbfree_ktls_rx(sb, m);
1942 sb->sb_tlsdcc = len;
1949 * Determine the length of the trailing zero padding and find the real
1950 * record type in the byte before the padding.
1952 * Walking the mbuf chain backwards is clumsy, so another option would
1953 * be to scan forwards remembering the last non-zero byte before the
1954 * trailer. However, it would be expensive to scan the entire record.
1955 * Instead, find the last non-zero byte of each mbuf in the chain
1956 * keeping track of the relative offset of that nonzero byte.
1958 * trail_len is the size of the MAC/tag on input and is set to the
1959 * size of the full trailer including padding and the record type on
1963 tls13_find_record_type(struct ktls_session *tls, struct mbuf *m, int tls_len,
1964 int *trailer_len, uint8_t *record_typep)
1967 u_int digest_start, last_offset, m_len, offset;
1968 uint8_t record_type;
1970 digest_start = tls_len - *trailer_len;
1973 for (; m != NULL && offset < digest_start;
1974 offset += m->m_len, m = m->m_next) {
1975 /* Don't look for padding in the tag. */
1976 m_len = min(digest_start - offset, m->m_len);
1977 cp = mtod(m, char *);
1979 /* Find last non-zero byte in this mbuf. */
1980 while (m_len > 0 && cp[m_len - 1] == 0)
1983 record_type = cp[m_len - 1];
1984 last_offset = offset + m_len;
1987 if (last_offset < tls->params.tls_hlen)
1990 *record_typep = record_type;
1991 *trailer_len = tls_len - last_offset + 1;
1996 ktls_decrypt(struct socket *so)
1998 char tls_header[MBUF_PEXT_HDR_LEN];
1999 struct ktls_session *tls;
2001 struct tls_record_layer *hdr;
2002 struct tls_get_record tgr;
2003 struct mbuf *control, *data, *m;
2005 int error, remain, tls_len, trail_len;
2007 uint8_t vminor, record_type;
2009 hdr = (struct tls_record_layer *)tls_header;
2012 KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING,
2013 ("%s: socket %p not running", __func__, so));
2015 tls = sb->sb_tls_info;
2018 tls13 = (tls->params.tls_vminor == TLS_MINOR_VER_THREE);
2020 vminor = TLS_MINOR_VER_TWO;
2022 vminor = tls->params.tls_vminor;
2024 /* Is there enough queued for a TLS header? */
2025 if (sb->sb_tlscc < tls->params.tls_hlen)
2028 m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header);
2029 tls_len = sizeof(*hdr) + ntohs(hdr->tls_length);
2031 if (hdr->tls_vmajor != tls->params.tls_vmajor ||
2032 hdr->tls_vminor != vminor)
2034 else if (tls13 && hdr->tls_type != TLS_RLTYPE_APP)
2036 else if (tls_len < tls->params.tls_hlen || tls_len >
2037 tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 +
2038 tls->params.tls_tlen)
2042 if (__predict_false(error != 0)) {
2044 * We have a corrupted record and are likely
2045 * out of sync. The connection isn't
2046 * recoverable at this point, so abort it.
2049 counter_u64_add(ktls_offload_corrupted_records, 1);
2051 CURVNET_SET(so->so_vnet);
2052 so->so_proto->pr_usrreqs->pru_abort(so);
2053 so->so_error = error;
2058 /* Is the entire record queued? */
2059 if (sb->sb_tlscc < tls_len)
2063 * Split out the portion of the mbuf chain containing
2066 data = ktls_detach_record(sb, tls_len);
2069 MPASS(sb->sb_tlsdcc == tls_len);
2071 seqno = sb->sb_tls_seqno;
2076 error = tls->sw_decrypt(tls, hdr, data, seqno, &trail_len);
2079 error = tls13_find_record_type(tls, data,
2080 tls_len, &trail_len, &record_type);
2082 record_type = hdr->tls_type;
2085 counter_u64_add(ktls_offload_failed_crypto, 1);
2088 if (sb->sb_tlsdcc == 0) {
2090 * sbcut/drop/flush discarded these
2098 * Drop this TLS record's data, but keep
2099 * decrypting subsequent records.
2101 sb->sb_ccc -= tls_len;
2104 CURVNET_SET(so->so_vnet);
2105 so->so_error = EBADMSG;
2106 sorwakeup_locked(so);
2115 /* Allocate the control mbuf. */
2116 memset(&tgr, 0, sizeof(tgr));
2117 tgr.tls_type = record_type;
2118 tgr.tls_vmajor = hdr->tls_vmajor;
2119 tgr.tls_vminor = hdr->tls_vminor;
2120 tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen -
2122 control = sbcreatecontrol_how(&tgr, sizeof(tgr),
2123 TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK);
2126 if (sb->sb_tlsdcc == 0) {
2127 /* sbcut/drop/flush discarded these mbufs. */
2128 MPASS(sb->sb_tlscc == 0);
2135 * Clear the 'dcc' accounting in preparation for
2136 * adding the decrypted record.
2138 sb->sb_ccc -= tls_len;
2142 /* If there is no payload, drop all of the data. */
2143 if (tgr.tls_length == htobe16(0)) {
2148 remain = tls->params.tls_hlen;
2149 while (remain > 0) {
2150 if (data->m_len > remain) {
2151 data->m_data += remain;
2152 data->m_len -= remain;
2155 remain -= data->m_len;
2156 data = m_free(data);
2159 /* Trim trailer and clear M_NOTREADY. */
2160 remain = be16toh(tgr.tls_length);
2162 for (m = data; remain > m->m_len; m = m->m_next) {
2163 m->m_flags &= ~M_NOTREADY;
2169 m->m_flags &= ~M_NOTREADY;
2171 /* Set EOR on the final mbuf. */
2172 m->m_flags |= M_EOR;
2175 sbappendcontrol_locked(sb, data, control, 0);
2178 sb->sb_flags &= ~SB_TLS_RX_RUNNING;
2180 if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0)
2181 so->so_error = EMSGSIZE;
2183 sorwakeup_locked(so);
2186 SOCKBUF_UNLOCK_ASSERT(sb);
2188 CURVNET_SET(so->so_vnet);
2194 ktls_enqueue_to_free(struct mbuf *m)
2199 /* Mark it for freeing. */
2200 m->m_epg_flags |= EPG_FLAG_2FREE;
2201 wq = &ktls_wq[m->m_epg_tls->wq_index];
2203 STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq);
2204 running = wq->running;
2205 mtx_unlock(&wq->mtx);
2211 ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m)
2214 int domain, running;
2216 if (m->m_epg_npgs <= 2)
2218 if (ktls_buffer_zone == NULL)
2220 if ((u_int)(ticks - wq->lastallocfail) < hz) {
2222 * Rate-limit allocation attempts after a failure.
2223 * ktls_buffer_import() will acquire a per-domain mutex to check
2224 * the free page queues and may fail consistently if memory is
2229 buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM);
2231 domain = PCPU_GET(domain);
2232 wq->lastallocfail = ticks;
2235 * Note that this check is "racy", but the races are
2236 * harmless, and are either a spurious wakeup if
2237 * multiple threads fail allocations before the alloc
2238 * thread wakes, or waiting an extra second in case we
2239 * see an old value of running == true.
2241 if (!VM_DOMAIN_EMPTY(domain)) {
2242 running = atomic_load_int(&ktls_domains[domain].alloc_td.running);
2244 wakeup(&ktls_domains[domain].alloc_td);
2251 ktls_encrypt_record(struct ktls_wq *wq, struct mbuf *m,
2252 struct ktls_session *tls, struct ktls_ocf_encrypt_state *state)
2255 int error, i, len, off;
2257 KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == (M_EXTPG | M_NOTREADY),
2258 ("%p not unready & nomap mbuf\n", m));
2259 KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen,
2260 ("page count %d larger than maximum frame length %d", m->m_epg_npgs,
2263 /* Anonymous mbufs are encrypted in place. */
2264 if ((m->m_epg_flags & EPG_FLAG_ANON) != 0)
2265 return (tls->sw_encrypt(state, tls, m, NULL, 0));
2268 * For file-backed mbufs (from sendfile), anonymous wired
2269 * pages are allocated and used as the encryption destination.
2271 if ((state->cbuf = ktls_buffer_alloc(wq, m)) != NULL) {
2272 len = ptoa(m->m_epg_npgs - 1) + m->m_epg_last_len -
2274 state->dst_iov[0].iov_base = (char *)state->cbuf +
2276 state->dst_iov[0].iov_len = len;
2277 state->parray[0] = DMAP_TO_PHYS((vm_offset_t)state->cbuf);
2280 off = m->m_epg_1st_off;
2281 for (i = 0; i < m->m_epg_npgs; i++, off = 0) {
2282 pg = vm_page_alloc_noobj(VM_ALLOC_NODUMP |
2283 VM_ALLOC_WIRED | VM_ALLOC_WAITOK);
2284 len = m_epg_pagelen(m, i, off);
2285 state->parray[i] = VM_PAGE_TO_PHYS(pg);
2286 state->dst_iov[i].iov_base =
2287 (char *)PHYS_TO_DMAP(state->parray[i]) + off;
2288 state->dst_iov[i].iov_len = len;
2291 KASSERT(i + 1 <= nitems(state->dst_iov), ("dst_iov is too small"));
2292 state->dst_iov[i].iov_base = m->m_epg_trail;
2293 state->dst_iov[i].iov_len = m->m_epg_trllen;
2295 error = tls->sw_encrypt(state, tls, m, state->dst_iov, i + 1);
2297 if (__predict_false(error != 0)) {
2298 /* Free the anonymous pages. */
2299 if (state->cbuf != NULL)
2300 uma_zfree(ktls_buffer_zone, state->cbuf);
2302 for (i = 0; i < m->m_epg_npgs; i++) {
2303 pg = PHYS_TO_VM_PAGE(state->parray[i]);
2304 (void)vm_page_unwire_noq(pg);
2312 /* Number of TLS records in a batch passed to ktls_enqueue(). */
2314 ktls_batched_records(struct mbuf *m)
2316 int page_count, records;
2319 page_count = m->m_epg_enc_cnt;
2320 while (page_count > 0) {
2322 page_count -= m->m_epg_nrdy;
2325 KASSERT(page_count == 0, ("%s: mismatched page count", __func__));
2330 ktls_enqueue(struct mbuf *m, struct socket *so, int page_count)
2332 struct ktls_session *tls;
2337 KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) ==
2338 (M_EXTPG | M_NOTREADY)),
2339 ("ktls_enqueue: %p not unready & nomap mbuf\n", m));
2340 KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count"));
2342 KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf"));
2344 m->m_epg_enc_cnt = page_count;
2347 * Save a pointer to the socket. The caller is responsible
2348 * for taking an additional reference via soref().
2354 wq = &ktls_wq[tls->wq_index];
2356 if (__predict_false(tls->sequential_records)) {
2358 * For TLS 1.0, records must be encrypted
2359 * sequentially. For a given connection, all records
2360 * queued to the associated work queue are processed
2361 * sequentially. However, sendfile(2) might complete
2362 * I/O requests spanning multiple TLS records out of
2363 * order. Here we ensure TLS records are enqueued to
2364 * the work queue in FIFO order.
2366 * tls->next_seqno holds the sequence number of the
2367 * next TLS record that should be enqueued to the work
2368 * queue. If this next record is not tls->next_seqno,
2369 * it must be a future record, so insert it, sorted by
2370 * TLS sequence number, into tls->pending_records and
2373 * If this TLS record matches tls->next_seqno, place
2374 * it in the work queue and then check
2375 * tls->pending_records to see if any
2376 * previously-queued records are now ready for
2379 if (m->m_epg_seqno != tls->next_seqno) {
2383 STAILQ_FOREACH(n, &tls->pending_records, m_epg_stailq) {
2384 if (n->m_epg_seqno > m->m_epg_seqno)
2389 STAILQ_INSERT_TAIL(&tls->pending_records, m,
2392 STAILQ_INSERT_HEAD(&tls->pending_records, m,
2395 STAILQ_INSERT_AFTER(&tls->pending_records, p, m,
2397 mtx_unlock(&wq->mtx);
2398 counter_u64_add(ktls_cnt_tx_pending, 1);
2402 tls->next_seqno += ktls_batched_records(m);
2403 STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq);
2405 while (!STAILQ_EMPTY(&tls->pending_records)) {
2408 n = STAILQ_FIRST(&tls->pending_records);
2409 if (n->m_epg_seqno != tls->next_seqno)
2413 STAILQ_REMOVE_HEAD(&tls->pending_records, m_epg_stailq);
2414 tls->next_seqno += ktls_batched_records(n);
2415 STAILQ_INSERT_TAIL(&wq->m_head, n, m_epg_stailq);
2417 counter_u64_add(ktls_cnt_tx_pending, -(queued - 1));
2419 STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq);
2421 running = wq->running;
2422 mtx_unlock(&wq->mtx);
2425 counter_u64_add(ktls_cnt_tx_queued, queued);
2429 * Once a file-backed mbuf (from sendfile) has been encrypted, free
2430 * the pages from the file and replace them with the anonymous pages
2431 * allocated in ktls_encrypt_record().
2434 ktls_finish_nonanon(struct mbuf *m, struct ktls_ocf_encrypt_state *state)
2438 MPASS((m->m_epg_flags & EPG_FLAG_ANON) == 0);
2440 /* Free the old pages. */
2441 m->m_ext.ext_free(m);
2443 /* Replace them with the new pages. */
2444 if (state->cbuf != NULL) {
2445 for (i = 0; i < m->m_epg_npgs; i++)
2446 m->m_epg_pa[i] = state->parray[0] + ptoa(i);
2448 /* Contig pages should go back to the cache. */
2449 m->m_ext.ext_free = ktls_free_mext_contig;
2451 for (i = 0; i < m->m_epg_npgs; i++)
2452 m->m_epg_pa[i] = state->parray[i];
2454 /* Use the basic free routine. */
2455 m->m_ext.ext_free = mb_free_mext_pgs;
2458 /* Pages are now writable. */
2459 m->m_epg_flags |= EPG_FLAG_ANON;
2462 static __noinline void
2463 ktls_encrypt(struct ktls_wq *wq, struct mbuf *top)
2465 struct ktls_ocf_encrypt_state state;
2466 struct ktls_session *tls;
2469 int error, npages, total_pages;
2472 tls = top->m_epg_tls;
2473 KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top));
2474 KASSERT(so != NULL, ("so = NULL, top = %p\n", top));
2476 top->m_epg_so = NULL;
2478 total_pages = top->m_epg_enc_cnt;
2482 * Encrypt the TLS records in the chain of mbufs starting with
2483 * 'top'. 'total_pages' gives us a total count of pages and is
2484 * used to know when we have finished encrypting the TLS
2485 * records originally queued with 'top'.
2487 * NB: These mbufs are queued in the socket buffer and
2488 * 'm_next' is traversing the mbufs in the socket buffer. The
2489 * socket buffer lock is not held while traversing this chain.
2490 * Since the mbufs are all marked M_NOTREADY their 'm_next'
2491 * pointers should be stable. However, the 'm_next' of the
2492 * last mbuf encrypted is not necessarily NULL. It can point
2493 * to other mbufs appended while 'top' was on the TLS work
2496 * Each mbuf holds an entire TLS record.
2499 for (m = top; npages != total_pages; m = m->m_next) {
2500 KASSERT(m->m_epg_tls == tls,
2501 ("different TLS sessions in a single mbuf chain: %p vs %p",
2502 tls, m->m_epg_tls));
2503 KASSERT(npages + m->m_epg_npgs <= total_pages,
2504 ("page count mismatch: top %p, total_pages %d, m %p", top,
2507 error = ktls_encrypt_record(wq, m, tls, &state);
2509 counter_u64_add(ktls_offload_failed_crypto, 1);
2513 if ((m->m_epg_flags & EPG_FLAG_ANON) == 0)
2514 ktls_finish_nonanon(m, &state);
2516 npages += m->m_epg_nrdy;
2519 * Drop a reference to the session now that it is no
2520 * longer needed. Existing code depends on encrypted
2521 * records having no associated session vs
2522 * yet-to-be-encrypted records having an associated
2525 m->m_epg_tls = NULL;
2529 CURVNET_SET(so->so_vnet);
2531 (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, top, npages);
2533 so->so_proto->pr_usrreqs->pru_abort(so);
2535 mb_free_notready(top, total_pages);
2543 ktls_encrypt_cb(struct ktls_ocf_encrypt_state *state, int error)
2545 struct ktls_session *tls;
2552 if ((m->m_epg_flags & EPG_FLAG_ANON) == 0)
2553 ktls_finish_nonanon(m, state);
2556 free(state, M_KTLS);
2559 * Drop a reference to the session now that it is no longer
2560 * needed. Existing code depends on encrypted records having
2561 * no associated session vs yet-to-be-encrypted records having
2562 * an associated session.
2565 m->m_epg_tls = NULL;
2569 counter_u64_add(ktls_offload_failed_crypto, 1);
2571 CURVNET_SET(so->so_vnet);
2572 npages = m->m_epg_nrdy;
2575 (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, m, npages);
2577 so->so_proto->pr_usrreqs->pru_abort(so);
2579 mb_free_notready(m, npages);
2587 * Similar to ktls_encrypt, but used with asynchronous OCF backends
2588 * (coprocessors) where encryption does not use host CPU resources and
2589 * it can be beneficial to queue more requests than CPUs.
2591 static __noinline void
2592 ktls_encrypt_async(struct ktls_wq *wq, struct mbuf *top)
2594 struct ktls_ocf_encrypt_state *state;
2595 struct ktls_session *tls;
2598 int error, mpages, npages, total_pages;
2601 tls = top->m_epg_tls;
2602 KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top));
2603 KASSERT(so != NULL, ("so = NULL, top = %p\n", top));
2605 top->m_epg_so = NULL;
2607 total_pages = top->m_epg_enc_cnt;
2611 for (m = top; npages != total_pages; m = n) {
2612 KASSERT(m->m_epg_tls == tls,
2613 ("different TLS sessions in a single mbuf chain: %p vs %p",
2614 tls, m->m_epg_tls));
2615 KASSERT(npages + m->m_epg_npgs <= total_pages,
2616 ("page count mismatch: top %p, total_pages %d, m %p", top,
2619 state = malloc(sizeof(*state), M_KTLS, M_WAITOK | M_ZERO);
2624 mpages = m->m_epg_nrdy;
2627 error = ktls_encrypt_record(wq, m, tls, state);
2629 counter_u64_add(ktls_offload_failed_crypto, 1);
2630 free(state, M_KTLS);
2631 CURVNET_SET(so->so_vnet);
2640 CURVNET_SET(so->so_vnet);
2642 so->so_proto->pr_usrreqs->pru_abort(so);
2644 mb_free_notready(m, total_pages - npages);
2652 ktls_bind_domain(int domain)
2656 error = cpuset_setthread(curthread->td_tid, &cpuset_domain[domain]);
2659 curthread->td_domain.dr_policy = DOMAINSET_PREF(domain);
2664 ktls_alloc_thread(void *ctx)
2666 struct ktls_domain_info *ktls_domain = ctx;
2667 struct ktls_alloc_thread *sc = &ktls_domain->alloc_td;
2669 struct sysctl_oid *oid;
2671 int domain, error, i, nbufs;
2673 domain = ktls_domain - ktls_domains;
2675 printf("Starting KTLS alloc thread for domain %d\n", domain);
2676 error = ktls_bind_domain(domain);
2678 printf("Unable to bind KTLS alloc thread for domain %d: error %d\n",
2680 snprintf(name, sizeof(name), "domain%d", domain);
2681 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern_ipc_tls), OID_AUTO,
2682 name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
2683 SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "allocs",
2684 CTLFLAG_RD, &sc->allocs, 0, "buffers allocated");
2685 SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "wakeups",
2686 CTLFLAG_RD, &sc->wakeups, 0, "thread wakeups");
2687 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "running",
2688 CTLFLAG_RD, &sc->running, 0, "thread running");
2693 atomic_store_int(&sc->running, 0);
2694 tsleep(sc, PZERO | PNOLOCK, "-", 0);
2695 atomic_store_int(&sc->running, 1);
2697 if (nbufs != ktls_max_alloc) {
2699 nbufs = atomic_load_int(&ktls_max_alloc);
2700 buf = malloc(sizeof(void *) * nbufs, M_KTLS,
2704 * Below we allocate nbufs with different allocation
2705 * flags than we use when allocating normally during
2706 * encryption in the ktls worker thread. We specify
2707 * M_NORECLAIM in the worker thread. However, we omit
2708 * that flag here and add M_WAITOK so that the VM
2709 * system is permitted to perform expensive work to
2710 * defragment memory. We do this here, as it does not
2711 * matter if this thread blocks. If we block a ktls
2712 * worker thread, we risk developing backlogs of
2713 * buffers to be encrypted, leading to surges of
2714 * traffic and potential NIC output drops.
2716 for (i = 0; i < nbufs; i++) {
2717 buf[i] = uma_zalloc(ktls_buffer_zone, M_WAITOK);
2720 for (i = 0; i < nbufs; i++) {
2721 uma_zfree(ktls_buffer_zone, buf[i]);
2728 ktls_work_thread(void *ctx)
2730 struct ktls_wq *wq = ctx;
2732 struct socket *so, *son;
2733 STAILQ_HEAD(, mbuf) local_m_head;
2734 STAILQ_HEAD(, socket) local_so_head;
2739 printf("Starting KTLS worker thread for CPU %d\n", cpu);
2742 * Bind to a core. If ktls_bind_threads is > 1, then
2743 * we bind to the NUMA domain instead.
2745 if (ktls_bind_threads) {
2748 if (ktls_bind_threads > 1) {
2749 struct pcpu *pc = pcpu_find(cpu);
2751 error = ktls_bind_domain(pc->pc_domain);
2755 CPU_SETOF(cpu, &mask);
2756 error = cpuset_setthread(curthread->td_tid, &mask);
2759 printf("Unable to bind KTLS worker thread for CPU %d: error %d\n",
2762 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
2767 while (STAILQ_EMPTY(&wq->m_head) &&
2768 STAILQ_EMPTY(&wq->so_head)) {
2769 wq->running = false;
2770 mtx_sleep(wq, &wq->mtx, 0, "-", 0);
2774 STAILQ_INIT(&local_m_head);
2775 STAILQ_CONCAT(&local_m_head, &wq->m_head);
2776 STAILQ_INIT(&local_so_head);
2777 STAILQ_CONCAT(&local_so_head, &wq->so_head);
2778 mtx_unlock(&wq->mtx);
2780 STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) {
2781 if (m->m_epg_flags & EPG_FLAG_2FREE) {
2782 ktls_free(m->m_epg_tls);
2785 if (m->m_epg_tls->sync_dispatch)
2786 ktls_encrypt(wq, m);
2788 ktls_encrypt_async(wq, m);
2789 counter_u64_add(ktls_cnt_tx_queued, -1);
2793 STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) {
2795 counter_u64_add(ktls_cnt_rx_queued, -1);
2800 #if defined(INET) || defined(INET6)
2802 ktls_disable_ifnet_help(void *context, int pending __unused)
2804 struct ktls_session *tls;
2815 so = inp->inp_socket;
2817 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2821 if (so->so_snd.sb_tls_info != NULL)
2822 err = ktls_set_tx_mode(so, TCP_TLS_MODE_SW);
2826 counter_u64_add(ktls_ifnet_disable_ok, 1);
2827 /* ktls_set_tx_mode() drops inp wlock, so recheck flags */
2828 if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0 &&
2829 (tp = intotcpcb(inp)) != NULL &&
2830 tp->t_fb->tfb_hwtls_change != NULL)
2831 (*tp->t_fb->tfb_hwtls_change)(tp, 0);
2833 counter_u64_add(ktls_ifnet_disable_fail, 1);
2838 if (!in_pcbrele_wlocked(inp))
2844 * Called when re-transmits are becoming a substantial portion of the
2845 * sends on this connection. When this happens, we transition the
2846 * connection to software TLS. This is needed because most inline TLS
2847 * NICs keep crypto state only for in-order transmits. This means
2848 * that to handle a TCP rexmit (which is out-of-order), the NIC must
2849 * re-DMA the entire TLS record up to and including the current
2850 * segment. This means that when re-transmitting the last ~1448 byte
2851 * segment of a 16KB TLS record, we could wind up re-DMA'ing an order
2852 * of magnitude more data than we are sending. This can cause the
2853 * PCIe link to saturate well before the network, which can cause
2854 * output drops, and a general loss of capacity.
2857 ktls_disable_ifnet(void *arg)
2862 struct ktls_session *tls;
2866 INP_WLOCK_ASSERT(inp);
2867 so = inp->inp_socket;
2869 tls = so->so_snd.sb_tls_info;
2870 if (tls->disable_ifnet_pending) {
2876 * note that disable_ifnet_pending is never cleared; disabling
2877 * ifnet can only be done once per session, so we never want
2881 (void)ktls_hold(tls);
2884 tls->disable_ifnet_pending = true;
2887 TASK_INIT(&tls->disable_ifnet_task, 0, ktls_disable_ifnet_help, tls);
2888 (void)taskqueue_enqueue(taskqueue_thread, &tls->disable_ifnet_task);