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"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
40 #include <sys/mutex.h>
41 #include <sys/rmlock.h>
43 #include <sys/protosw.h>
44 #include <sys/refcount.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/taskqueue.h>
50 #include <sys/kthread.h>
52 #include <sys/vmmeter.h>
53 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
54 #include <machine/pcb.h>
56 #include <machine/vmparam.h>
58 #include <net/if_var.h>
60 #include <net/netisr.h>
61 #include <net/rss_config.h>
63 #include <net/route.h>
64 #include <net/route/nhop.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
69 #include <netinet/tcp_var.h>
71 #include <netinet/tcp_offload.h>
73 #include <opencrypto/xform.h>
74 #include <vm/uma_dbg.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_page.h>
81 STAILQ_HEAD(, mbuf) head;
83 } __aligned(CACHE_LINE_SIZE);
85 static struct ktls_wq *ktls_wq;
86 static struct proc *ktls_proc;
87 LIST_HEAD(, ktls_crypto_backend) ktls_backends;
88 static struct rmlock ktls_backends_lock;
89 static uma_zone_t ktls_session_zone;
90 static uint16_t ktls_cpuid_lookup[MAXCPU];
92 SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
93 "Kernel TLS offload");
94 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 "Kernel TLS offload stats");
97 static int ktls_allow_unload;
98 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, allow_unload, CTLFLAG_RDTUN,
99 &ktls_allow_unload, 0, "Allow software crypto modules to unload");
102 static int ktls_bind_threads = 1;
104 static int ktls_bind_threads;
106 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN,
107 &ktls_bind_threads, 0,
108 "Bind crypto threads to cores or domains at boot");
110 static u_int ktls_maxlen = 16384;
111 SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RWTUN,
112 &ktls_maxlen, 0, "Maximum TLS record size");
114 static int ktls_number_threads;
115 SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD,
116 &ktls_number_threads, 0,
117 "Number of TLS threads in thread-pool");
119 static bool ktls_offload_enable;
120 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RW,
121 &ktls_offload_enable, 0,
122 "Enable support for kernel TLS offload");
124 static bool ktls_cbc_enable = true;
125 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RW,
127 "Enable Support of AES-CBC crypto for kernel TLS");
129 static counter_u64_t ktls_tasks_active;
130 SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD,
131 &ktls_tasks_active, "Number of active tasks");
133 static counter_u64_t ktls_cnt_on;
134 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, so_inqueue, CTLFLAG_RD,
135 &ktls_cnt_on, "Number of TLS records in queue to tasks for SW crypto");
137 static counter_u64_t ktls_offload_total;
138 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total,
139 CTLFLAG_RD, &ktls_offload_total,
140 "Total successful TLS setups (parameters set)");
142 static counter_u64_t ktls_offload_enable_calls;
143 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls,
144 CTLFLAG_RD, &ktls_offload_enable_calls,
145 "Total number of TLS enable calls made");
147 static counter_u64_t ktls_offload_active;
148 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD,
149 &ktls_offload_active, "Total Active TLS sessions");
151 static counter_u64_t ktls_offload_failed_crypto;
152 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD,
153 &ktls_offload_failed_crypto, "Total TLS crypto failures");
155 static counter_u64_t ktls_switch_to_ifnet;
156 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD,
157 &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet");
159 static counter_u64_t ktls_switch_to_sw;
160 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD,
161 &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW");
163 static counter_u64_t ktls_switch_failed;
164 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD,
165 &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet");
167 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
168 "Software TLS session stats");
169 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
170 "Hardware (ifnet) TLS session stats");
172 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
173 "TOE TLS session stats");
176 static counter_u64_t ktls_sw_cbc;
177 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc,
178 "Active number of software TLS sessions using AES-CBC");
180 static counter_u64_t ktls_sw_gcm;
181 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm,
182 "Active number of software TLS sessions using AES-GCM");
184 static counter_u64_t ktls_ifnet_cbc;
185 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD,
187 "Active number of ifnet TLS sessions using AES-CBC");
189 static counter_u64_t ktls_ifnet_gcm;
190 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD,
192 "Active number of ifnet TLS sessions using AES-GCM");
194 static counter_u64_t ktls_ifnet_reset;
195 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD,
196 &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag");
198 static counter_u64_t ktls_ifnet_reset_dropped;
199 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD,
200 &ktls_ifnet_reset_dropped,
201 "TLS sessions dropped after failing to update ifnet send tag");
203 static counter_u64_t ktls_ifnet_reset_failed;
204 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD,
205 &ktls_ifnet_reset_failed,
206 "TLS sessions that failed to allocate a new ifnet send tag");
208 static int ktls_ifnet_permitted;
209 SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN,
210 &ktls_ifnet_permitted, 1,
211 "Whether to permit hardware (ifnet) TLS sessions");
214 static counter_u64_t ktls_toe_cbc;
215 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD,
217 "Active number of TOE TLS sessions using AES-CBC");
219 static counter_u64_t ktls_toe_gcm;
220 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD,
222 "Active number of TOE TLS sessions using AES-GCM");
225 static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS");
227 static void ktls_cleanup(struct ktls_session *tls);
228 #if defined(INET) || defined(INET6)
229 static void ktls_reset_send_tag(void *context, int pending);
231 static void ktls_work_thread(void *ctx);
234 ktls_crypto_backend_register(struct ktls_crypto_backend *be)
236 struct ktls_crypto_backend *curr_be, *tmp;
238 if (be->api_version != KTLS_API_VERSION) {
239 printf("KTLS: API version mismatch (%d vs %d) for %s\n",
240 be->api_version, KTLS_API_VERSION,
245 rm_wlock(&ktls_backends_lock);
246 printf("KTLS: Registering crypto method %s with prio %d\n",
248 if (LIST_EMPTY(&ktls_backends)) {
249 LIST_INSERT_HEAD(&ktls_backends, be, next);
251 LIST_FOREACH_SAFE(curr_be, &ktls_backends, next, tmp) {
252 if (curr_be->prio < be->prio) {
253 LIST_INSERT_BEFORE(curr_be, be, next);
256 if (LIST_NEXT(curr_be, next) == NULL) {
257 LIST_INSERT_AFTER(curr_be, be, next);
262 rm_wunlock(&ktls_backends_lock);
267 ktls_crypto_backend_deregister(struct ktls_crypto_backend *be)
269 struct ktls_crypto_backend *tmp;
272 * Don't error if the backend isn't registered. This permits
273 * MOD_UNLOAD handlers to use this function unconditionally.
275 rm_wlock(&ktls_backends_lock);
276 LIST_FOREACH(tmp, &ktls_backends, next) {
281 rm_wunlock(&ktls_backends_lock);
285 if (!ktls_allow_unload) {
286 rm_wunlock(&ktls_backends_lock);
288 "KTLS: Deregistering crypto method %s is not supported\n",
294 rm_wunlock(&ktls_backends_lock);
298 LIST_REMOVE(be, next);
299 rm_wunlock(&ktls_backends_lock);
303 #if defined(INET) || defined(INET6)
305 ktls_get_cpu(struct socket *so)
312 cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
313 if (cpuid != NETISR_CPUID_NONE)
317 * Just use the flowid to shard connections in a repeatable
318 * fashion. Note that some crypto backends rely on the
319 * serialization provided by having the same connection use
322 cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads];
328 ktls_init(void *dummy __unused)
335 ktls_tasks_active = counter_u64_alloc(M_WAITOK);
336 ktls_cnt_on = counter_u64_alloc(M_WAITOK);
337 ktls_offload_total = counter_u64_alloc(M_WAITOK);
338 ktls_offload_enable_calls = counter_u64_alloc(M_WAITOK);
339 ktls_offload_active = counter_u64_alloc(M_WAITOK);
340 ktls_offload_failed_crypto = counter_u64_alloc(M_WAITOK);
341 ktls_switch_to_ifnet = counter_u64_alloc(M_WAITOK);
342 ktls_switch_to_sw = counter_u64_alloc(M_WAITOK);
343 ktls_switch_failed = counter_u64_alloc(M_WAITOK);
344 ktls_sw_cbc = counter_u64_alloc(M_WAITOK);
345 ktls_sw_gcm = counter_u64_alloc(M_WAITOK);
346 ktls_ifnet_cbc = counter_u64_alloc(M_WAITOK);
347 ktls_ifnet_gcm = counter_u64_alloc(M_WAITOK);
348 ktls_ifnet_reset = counter_u64_alloc(M_WAITOK);
349 ktls_ifnet_reset_dropped = counter_u64_alloc(M_WAITOK);
350 ktls_ifnet_reset_failed = counter_u64_alloc(M_WAITOK);
352 ktls_toe_cbc = counter_u64_alloc(M_WAITOK);
353 ktls_toe_gcm = counter_u64_alloc(M_WAITOK);
356 rm_init(&ktls_backends_lock, "ktls backends");
357 LIST_INIT(&ktls_backends);
359 ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS,
362 ktls_session_zone = uma_zcreate("ktls_session",
363 sizeof(struct ktls_session),
364 NULL, NULL, NULL, NULL,
368 * Initialize the workqueues to run the TLS work. We create a
369 * work queue for each CPU.
372 STAILQ_INIT(&ktls_wq[i].head);
373 mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF);
374 error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i],
375 &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i);
377 panic("Can't add KTLS thread %d error %d", i, error);
380 * Bind threads to cores. If ktls_bind_threads is >
381 * 1, then we bind to the NUMA domain.
383 if (ktls_bind_threads) {
384 if (ktls_bind_threads > 1) {
386 CPU_COPY(&cpuset_domain[pc->pc_domain], &mask);
390 error = cpuset_setthread(td->td_tid, &mask);
393 "Unable to bind KTLS thread for CPU %d error %d",
396 ktls_cpuid_lookup[ktls_number_threads] = i;
397 ktls_number_threads++;
399 printf("KTLS: Initialized %d threads\n", ktls_number_threads);
401 SYSINIT(ktls, SI_SUB_SMP + 1, SI_ORDER_ANY, ktls_init, NULL);
403 #if defined(INET) || defined(INET6)
405 ktls_create_session(struct socket *so, struct tls_enable *en,
406 struct ktls_session **tlsp)
408 struct ktls_session *tls;
411 /* Only TLS 1.0 - 1.3 are supported. */
412 if (en->tls_vmajor != TLS_MAJOR_VER_ONE)
414 if (en->tls_vminor < TLS_MINOR_VER_ZERO ||
415 en->tls_vminor > TLS_MINOR_VER_THREE)
418 if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE)
420 if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE)
422 if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv))
425 /* All supported algorithms require a cipher key. */
426 if (en->cipher_key_len == 0)
429 /* No flags are currently supported. */
433 /* Common checks for supported algorithms. */
434 switch (en->cipher_algorithm) {
435 case CRYPTO_AES_NIST_GCM_16:
437 * auth_algorithm isn't used, but permit GMAC values
440 switch (en->auth_algorithm) {
442 #ifdef COMPAT_FREEBSD12
443 /* XXX: Really 13.0-current COMPAT. */
444 case CRYPTO_AES_128_NIST_GMAC:
445 case CRYPTO_AES_192_NIST_GMAC:
446 case CRYPTO_AES_256_NIST_GMAC:
452 if (en->auth_key_len != 0)
454 if ((en->tls_vminor == TLS_MINOR_VER_TWO &&
455 en->iv_len != TLS_AEAD_GCM_LEN) ||
456 (en->tls_vminor == TLS_MINOR_VER_THREE &&
457 en->iv_len != TLS_1_3_GCM_IV_LEN))
461 switch (en->auth_algorithm) {
462 case CRYPTO_SHA1_HMAC:
464 * TLS 1.0 requires an implicit IV. TLS 1.1+
465 * all use explicit IVs.
467 if (en->tls_vminor == TLS_MINOR_VER_ZERO) {
468 if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN)
474 case CRYPTO_SHA2_256_HMAC:
475 case CRYPTO_SHA2_384_HMAC:
476 /* Ignore any supplied IV. */
482 if (en->auth_key_len == 0)
489 tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
491 counter_u64_add(ktls_offload_active, 1);
493 refcount_init(&tls->refcount, 1);
494 TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls);
496 tls->wq_index = ktls_get_cpu(so);
498 tls->params.cipher_algorithm = en->cipher_algorithm;
499 tls->params.auth_algorithm = en->auth_algorithm;
500 tls->params.tls_vmajor = en->tls_vmajor;
501 tls->params.tls_vminor = en->tls_vminor;
502 tls->params.flags = en->flags;
503 tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen);
505 /* Set the header and trailer lengths. */
506 tls->params.tls_hlen = sizeof(struct tls_record_layer);
507 switch (en->cipher_algorithm) {
508 case CRYPTO_AES_NIST_GCM_16:
510 * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte
511 * nonce. TLS 1.3 uses a 12 byte implicit IV.
513 if (en->tls_vminor < TLS_MINOR_VER_THREE)
514 tls->params.tls_hlen += sizeof(uint64_t);
515 tls->params.tls_tlen = AES_GMAC_HASH_LEN;
518 * TLS 1.3 includes optional padding which we
519 * do not support, and also puts the "real" record
520 * type at the end of the encrypted data.
522 if (en->tls_vminor == TLS_MINOR_VER_THREE)
523 tls->params.tls_tlen += sizeof(uint8_t);
525 tls->params.tls_bs = 1;
528 switch (en->auth_algorithm) {
529 case CRYPTO_SHA1_HMAC:
530 if (en->tls_vminor == TLS_MINOR_VER_ZERO) {
531 /* Implicit IV, no nonce. */
533 tls->params.tls_hlen += AES_BLOCK_LEN;
535 tls->params.tls_tlen = AES_BLOCK_LEN +
538 case CRYPTO_SHA2_256_HMAC:
539 tls->params.tls_hlen += AES_BLOCK_LEN;
540 tls->params.tls_tlen = AES_BLOCK_LEN +
543 case CRYPTO_SHA2_384_HMAC:
544 tls->params.tls_hlen += AES_BLOCK_LEN;
545 tls->params.tls_tlen = AES_BLOCK_LEN +
549 panic("invalid hmac");
551 tls->params.tls_bs = AES_BLOCK_LEN;
554 panic("invalid cipher");
557 KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN,
558 ("TLS header length too long: %d", tls->params.tls_hlen));
559 KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN,
560 ("TLS trailer length too long: %d", tls->params.tls_tlen));
562 if (en->auth_key_len != 0) {
563 tls->params.auth_key_len = en->auth_key_len;
564 tls->params.auth_key = malloc(en->auth_key_len, M_KTLS,
566 error = copyin(en->auth_key, tls->params.auth_key,
572 tls->params.cipher_key_len = en->cipher_key_len;
573 tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK);
574 error = copyin(en->cipher_key, tls->params.cipher_key,
580 * This holds the implicit portion of the nonce for GCM and
581 * the initial implicit IV for TLS 1.0. The explicit portions
582 * of the IV are generated in ktls_frame().
584 if (en->iv_len != 0) {
585 tls->params.iv_len = en->iv_len;
586 error = copyin(en->iv, tls->params.iv, en->iv_len);
591 * For TLS 1.2, generate an 8-byte nonce as a counter
592 * to generate unique explicit IVs.
594 * Store this counter in the last 8 bytes of the IV
595 * array so that it is 8-byte aligned.
597 if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
598 en->tls_vminor == TLS_MINOR_VER_TWO)
599 arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0);
610 static struct ktls_session *
611 ktls_clone_session(struct ktls_session *tls)
613 struct ktls_session *tls_new;
615 tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
617 counter_u64_add(ktls_offload_active, 1);
619 refcount_init(&tls_new->refcount, 1);
621 /* Copy fields from existing session. */
622 tls_new->params = tls->params;
623 tls_new->wq_index = tls->wq_index;
625 /* Deep copy keys. */
626 if (tls_new->params.auth_key != NULL) {
627 tls_new->params.auth_key = malloc(tls->params.auth_key_len,
629 memcpy(tls_new->params.auth_key, tls->params.auth_key,
630 tls->params.auth_key_len);
633 tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS,
635 memcpy(tls_new->params.cipher_key, tls->params.cipher_key,
636 tls->params.cipher_key_len);
643 ktls_cleanup(struct ktls_session *tls)
646 counter_u64_add(ktls_offload_active, -1);
648 case TCP_TLS_MODE_SW:
649 MPASS(tls->be != NULL);
650 switch (tls->params.cipher_algorithm) {
652 counter_u64_add(ktls_sw_cbc, -1);
654 case CRYPTO_AES_NIST_GCM_16:
655 counter_u64_add(ktls_sw_gcm, -1);
660 case TCP_TLS_MODE_IFNET:
661 switch (tls->params.cipher_algorithm) {
663 counter_u64_add(ktls_ifnet_cbc, -1);
665 case CRYPTO_AES_NIST_GCM_16:
666 counter_u64_add(ktls_ifnet_gcm, -1);
669 m_snd_tag_rele(tls->snd_tag);
672 case TCP_TLS_MODE_TOE:
673 switch (tls->params.cipher_algorithm) {
675 counter_u64_add(ktls_toe_cbc, -1);
677 case CRYPTO_AES_NIST_GCM_16:
678 counter_u64_add(ktls_toe_gcm, -1);
684 if (tls->params.auth_key != NULL) {
685 zfree(tls->params.auth_key, M_KTLS);
686 tls->params.auth_key = NULL;
687 tls->params.auth_key_len = 0;
689 if (tls->params.cipher_key != NULL) {
690 zfree(tls->params.cipher_key, M_KTLS);
691 tls->params.cipher_key = NULL;
692 tls->params.cipher_key_len = 0;
694 explicit_bzero(tls->params.iv, sizeof(tls->params.iv));
697 #if defined(INET) || defined(INET6)
701 ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction)
709 if (inp->inp_flags2 & INP_FREED) {
713 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
717 if (inp->inp_socket == NULL) {
722 if (tp->tod == NULL) {
727 error = tcp_offload_alloc_tls_session(tp, tls, direction);
730 tls->mode = TCP_TLS_MODE_TOE;
731 switch (tls->params.cipher_algorithm) {
733 counter_u64_add(ktls_toe_cbc, 1);
735 case CRYPTO_AES_NIST_GCM_16:
736 counter_u64_add(ktls_toe_gcm, 1);
745 * Common code used when first enabling ifnet TLS on a connection or
746 * when allocating a new ifnet TLS session due to a routing change.
747 * This function allocates a new TLS send tag on whatever interface
748 * the connection is currently routed over.
751 ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force,
752 struct m_snd_tag **mstp)
754 union if_snd_tag_alloc_params params;
756 struct nhop_object *nh;
761 if (inp->inp_flags2 & INP_FREED) {
765 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
769 if (inp->inp_socket == NULL) {
776 * Check administrative controls on ifnet TLS to determine if
777 * ifnet TLS should be denied.
779 * - Always permit 'force' requests.
780 * - ktls_ifnet_permitted == 0: always deny.
782 if (!force && ktls_ifnet_permitted == 0) {
788 * XXX: Use the cached route in the inpcb to find the
789 * interface. This should perhaps instead use
790 * rtalloc1_fib(dst, 0, 0, fibnum). Since KTLS is only
791 * enabled after a connection has completed key negotiation in
792 * userland, the cached route will be present in practice.
794 nh = inp->inp_route.ro_nh;
802 params.hdr.type = IF_SND_TAG_TYPE_TLS;
803 params.hdr.flowid = inp->inp_flowid;
804 params.hdr.flowtype = inp->inp_flowtype;
805 params.hdr.numa_domain = inp->inp_numa_domain;
806 params.tls.inp = inp;
807 params.tls.tls = tls;
810 if (ifp->if_snd_tag_alloc == NULL) {
814 if ((ifp->if_capenable & IFCAP_NOMAP) == 0) {
818 if (inp->inp_vflag & INP_IPV6) {
819 if ((ifp->if_capenable & IFCAP_TXTLS6) == 0) {
824 if ((ifp->if_capenable & IFCAP_TXTLS4) == 0) {
829 error = ifp->if_snd_tag_alloc(ifp, ¶ms, mstp);
836 ktls_try_ifnet(struct socket *so, struct ktls_session *tls, bool force)
838 struct m_snd_tag *mst;
841 error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst);
843 tls->mode = TCP_TLS_MODE_IFNET;
845 switch (tls->params.cipher_algorithm) {
847 counter_u64_add(ktls_ifnet_cbc, 1);
849 case CRYPTO_AES_NIST_GCM_16:
850 counter_u64_add(ktls_ifnet_gcm, 1);
858 ktls_try_sw(struct socket *so, struct ktls_session *tls)
860 struct rm_priotracker prio;
861 struct ktls_crypto_backend *be;
864 * Choose the best software crypto backend. Backends are
865 * stored in sorted priority order (larget value == most
866 * important at the head of the list), so this just stops on
867 * the first backend that claims the session by returning
870 if (ktls_allow_unload)
871 rm_rlock(&ktls_backends_lock, &prio);
872 LIST_FOREACH(be, &ktls_backends, next) {
873 if (be->try(so, tls) == 0)
875 KASSERT(tls->cipher == NULL,
876 ("ktls backend leaked a cipher pointer"));
879 if (ktls_allow_unload)
883 if (ktls_allow_unload)
884 rm_runlock(&ktls_backends_lock, &prio);
887 tls->mode = TCP_TLS_MODE_SW;
888 switch (tls->params.cipher_algorithm) {
890 counter_u64_add(ktls_sw_cbc, 1);
892 case CRYPTO_AES_NIST_GCM_16:
893 counter_u64_add(ktls_sw_gcm, 1);
900 ktls_enable_rx(struct socket *so, struct tls_enable *en)
902 struct ktls_session *tls;
905 if (!ktls_offload_enable)
908 counter_u64_add(ktls_offload_enable_calls, 1);
911 * This should always be true since only the TCP socket option
912 * invokes this function.
914 if (so->so_proto->pr_protocol != IPPROTO_TCP)
918 * XXX: Don't overwrite existing sessions. We should permit
919 * this to support rekeying in the future.
921 if (so->so_rcv.sb_tls_info != NULL)
924 if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
927 error = ktls_create_session(so, en, &tls);
931 /* TLS RX offload is only supported on TOE currently. */
933 error = ktls_try_toe(so, tls, KTLS_RX);
943 /* Mark the socket as using TLS offload. */
944 SOCKBUF_LOCK(&so->so_rcv);
945 so->so_rcv.sb_tls_info = tls;
946 SOCKBUF_UNLOCK(&so->so_rcv);
948 counter_u64_add(ktls_offload_total, 1);
954 ktls_enable_tx(struct socket *so, struct tls_enable *en)
956 struct ktls_session *tls;
959 if (!ktls_offload_enable)
962 counter_u64_add(ktls_offload_enable_calls, 1);
965 * This should always be true since only the TCP socket option
966 * invokes this function.
968 if (so->so_proto->pr_protocol != IPPROTO_TCP)
972 * XXX: Don't overwrite existing sessions. We should permit
973 * this to support rekeying in the future.
975 if (so->so_snd.sb_tls_info != NULL)
978 if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
981 /* TLS requires ext pgs */
982 if (mb_use_ext_pgs == 0)
985 error = ktls_create_session(so, en, &tls);
989 /* Prefer TOE -> ifnet TLS -> software TLS. */
991 error = ktls_try_toe(so, tls, KTLS_TX);
994 error = ktls_try_ifnet(so, tls, false);
996 error = ktls_try_sw(so, tls);
1003 error = sblock(&so->so_snd, SBL_WAIT);
1009 SOCKBUF_LOCK(&so->so_snd);
1010 so->so_snd.sb_tls_seqno = be64dec(en->rec_seq);
1011 so->so_snd.sb_tls_info = tls;
1012 if (tls->mode != TCP_TLS_MODE_SW)
1013 so->so_snd.sb_flags |= SB_TLS_IFNET;
1014 SOCKBUF_UNLOCK(&so->so_snd);
1015 sbunlock(&so->so_snd);
1017 counter_u64_add(ktls_offload_total, 1);
1023 ktls_get_rx_mode(struct socket *so)
1025 struct ktls_session *tls;
1030 INP_WLOCK_ASSERT(inp);
1031 SOCKBUF_LOCK(&so->so_rcv);
1032 tls = so->so_rcv.sb_tls_info;
1034 mode = TCP_TLS_MODE_NONE;
1037 SOCKBUF_UNLOCK(&so->so_rcv);
1042 ktls_get_tx_mode(struct socket *so)
1044 struct ktls_session *tls;
1049 INP_WLOCK_ASSERT(inp);
1050 SOCKBUF_LOCK(&so->so_snd);
1051 tls = so->so_snd.sb_tls_info;
1053 mode = TCP_TLS_MODE_NONE;
1056 SOCKBUF_UNLOCK(&so->so_snd);
1061 * Switch between SW and ifnet TLS sessions as requested.
1064 ktls_set_tx_mode(struct socket *so, int mode)
1066 struct ktls_session *tls, *tls_new;
1071 case TCP_TLS_MODE_SW:
1072 case TCP_TLS_MODE_IFNET:
1079 INP_WLOCK_ASSERT(inp);
1080 SOCKBUF_LOCK(&so->so_snd);
1081 tls = so->so_snd.sb_tls_info;
1083 SOCKBUF_UNLOCK(&so->so_snd);
1087 if (tls->mode == mode) {
1088 SOCKBUF_UNLOCK(&so->so_snd);
1092 tls = ktls_hold(tls);
1093 SOCKBUF_UNLOCK(&so->so_snd);
1096 tls_new = ktls_clone_session(tls);
1098 if (mode == TCP_TLS_MODE_IFNET)
1099 error = ktls_try_ifnet(so, tls_new, true);
1101 error = ktls_try_sw(so, tls_new);
1103 counter_u64_add(ktls_switch_failed, 1);
1110 error = sblock(&so->so_snd, SBL_WAIT);
1112 counter_u64_add(ktls_switch_failed, 1);
1120 * If we raced with another session change, keep the existing
1123 if (tls != so->so_snd.sb_tls_info) {
1124 counter_u64_add(ktls_switch_failed, 1);
1125 sbunlock(&so->so_snd);
1132 SOCKBUF_LOCK(&so->so_snd);
1133 so->so_snd.sb_tls_info = tls_new;
1134 if (tls_new->mode != TCP_TLS_MODE_SW)
1135 so->so_snd.sb_flags |= SB_TLS_IFNET;
1136 SOCKBUF_UNLOCK(&so->so_snd);
1137 sbunlock(&so->so_snd);
1140 * Drop two references on 'tls'. The first is for the
1141 * ktls_hold() above. The second drops the reference from the
1144 KASSERT(tls->refcount >= 2, ("too few references on old session"));
1148 if (mode == TCP_TLS_MODE_IFNET)
1149 counter_u64_add(ktls_switch_to_ifnet, 1);
1151 counter_u64_add(ktls_switch_to_sw, 1);
1158 * Try to allocate a new TLS send tag. This task is scheduled when
1159 * ip_output detects a route change while trying to transmit a packet
1160 * holding a TLS record. If a new tag is allocated, replace the tag
1161 * in the TLS session. Subsequent packets on the connection will use
1162 * the new tag. If a new tag cannot be allocated, drop the
1166 ktls_reset_send_tag(void *context, int pending)
1168 struct epoch_tracker et;
1169 struct ktls_session *tls;
1170 struct m_snd_tag *old, *new;
1175 MPASS(pending == 1);
1181 * Free the old tag first before allocating a new one.
1182 * ip[6]_output_send() will treat a NULL send tag the same as
1183 * an ifp mismatch and drop packets until a new tag is
1186 * Write-lock the INP when changing tls->snd_tag since
1187 * ip[6]_output_send() holds a read-lock when reading the
1192 tls->snd_tag = NULL;
1195 m_snd_tag_rele(old);
1197 error = ktls_alloc_snd_tag(inp, tls, true, &new);
1202 mtx_pool_lock(mtxpool_sleep, tls);
1203 tls->reset_pending = false;
1204 mtx_pool_unlock(mtxpool_sleep, tls);
1205 if (!in_pcbrele_wlocked(inp))
1208 counter_u64_add(ktls_ifnet_reset, 1);
1211 * XXX: Should we kick tcp_output explicitly now that
1212 * the send tag is fixed or just rely on timers?
1215 NET_EPOCH_ENTER(et);
1217 if (!in_pcbrele_wlocked(inp)) {
1218 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1219 !(inp->inp_flags & INP_DROPPED)) {
1220 tp = intotcpcb(inp);
1221 CURVNET_SET(tp->t_vnet);
1222 tp = tcp_drop(tp, ECONNABORTED);
1226 counter_u64_add(ktls_ifnet_reset_dropped, 1);
1232 counter_u64_add(ktls_ifnet_reset_failed, 1);
1235 * Leave reset_pending true to avoid future tasks while
1236 * the socket goes away.
1244 ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls)
1250 INP_LOCK_ASSERT(inp);
1253 * See if we should schedule a task to update the send tag for
1256 mtx_pool_lock(mtxpool_sleep, tls);
1257 if (!tls->reset_pending) {
1258 (void) ktls_hold(tls);
1261 tls->reset_pending = true;
1262 taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task);
1264 mtx_pool_unlock(mtxpool_sleep, tls);
1270 ktls_destroy(struct ktls_session *tls)
1272 struct rm_priotracker prio;
1275 if (tls->be != NULL && ktls_allow_unload) {
1276 rm_rlock(&ktls_backends_lock, &prio);
1277 tls->be->use_count--;
1278 rm_runlock(&ktls_backends_lock, &prio);
1280 uma_zfree(ktls_session_zone, tls);
1284 ktls_seq(struct sockbuf *sb, struct mbuf *m)
1287 for (; m != NULL; m = m->m_next) {
1288 KASSERT((m->m_flags & M_EXTPG) != 0,
1289 ("ktls_seq: mapped mbuf %p", m));
1291 m->m_epg_seqno = sb->sb_tls_seqno;
1297 * Add TLS framing (headers and trailers) to a chain of mbufs. Each
1298 * mbuf in the chain must be an unmapped mbuf. The payload of the
1299 * mbuf must be populated with the payload of each TLS record.
1301 * The record_type argument specifies the TLS record type used when
1302 * populating the TLS header.
1304 * The enq_count argument on return is set to the number of pages of
1305 * payload data for this entire chain that need to be encrypted via SW
1306 * encryption. The returned value should be passed to ktls_enqueue
1307 * when scheduling encryption of this chain of mbufs.
1310 ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt,
1311 uint8_t record_type)
1313 struct tls_record_layer *tlshdr;
1319 maxlen = tls->params.max_frame_len;
1321 for (m = top; m != NULL; m = m->m_next) {
1323 * All mbufs in the chain should be non-empty TLS
1324 * records whose payload does not exceed the maximum
1327 KASSERT(m->m_len <= maxlen && m->m_len > 0,
1328 ("ktls_frame: m %p len %d\n", m, m->m_len));
1330 * TLS frames require unmapped mbufs to store session
1333 KASSERT((m->m_flags & M_EXTPG) != 0,
1334 ("ktls_frame: mapped mbuf %p (top = %p)\n", m, top));
1338 /* Save a reference to the session. */
1339 m->m_epg_tls = ktls_hold(tls);
1341 m->m_epg_hdrlen = tls->params.tls_hlen;
1342 m->m_epg_trllen = tls->params.tls_tlen;
1343 if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) {
1347 * AES-CBC pads messages to a multiple of the
1348 * block size. Note that the padding is
1349 * applied after the digest and the encryption
1350 * is done on the "plaintext || mac || padding".
1351 * At least one byte of padding is always
1354 * Compute the final trailer length assuming
1355 * at most one block of padding.
1356 * tls->params.sb_tls_tlen is the maximum
1357 * possible trailer length (padding + digest).
1358 * delta holds the number of excess padding
1359 * bytes if the maximum were used. Those
1360 * extra bytes are removed.
1362 bs = tls->params.tls_bs;
1363 delta = (tls_len + tls->params.tls_tlen) & (bs - 1);
1364 m->m_epg_trllen -= delta;
1366 m->m_len += m->m_epg_hdrlen + m->m_epg_trllen;
1368 /* Populate the TLS header. */
1369 tlshdr = (void *)m->m_epg_hdr;
1370 tlshdr->tls_vmajor = tls->params.tls_vmajor;
1373 * TLS 1.3 masquarades as TLS 1.2 with a record type
1374 * of TLS_RLTYPE_APP.
1376 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE &&
1377 tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) {
1378 tlshdr->tls_vminor = TLS_MINOR_VER_TWO;
1379 tlshdr->tls_type = TLS_RLTYPE_APP;
1380 /* save the real record type for later */
1381 m->m_epg_record_type = record_type;
1382 m->m_epg_trail[0] = record_type;
1384 tlshdr->tls_vminor = tls->params.tls_vminor;
1385 tlshdr->tls_type = record_type;
1387 tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr));
1390 * Store nonces / explicit IVs after the end of the
1393 * For GCM with TLS 1.2, an 8 byte nonce is copied
1394 * from the end of the IV. The nonce is then
1395 * incremented for use by the next record.
1397 * For CBC, a random nonce is inserted for TLS 1.1+.
1399 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
1400 tls->params.tls_vminor == TLS_MINOR_VER_TWO) {
1401 noncep = (uint64_t *)(tls->params.iv + 8);
1402 be64enc(tlshdr + 1, *noncep);
1404 } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC &&
1405 tls->params.tls_vminor >= TLS_MINOR_VER_ONE)
1406 arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0);
1409 * When using SW encryption, mark the mbuf not ready.
1410 * It will be marked ready via sbready() after the
1411 * record has been encrypted.
1413 * When using ifnet TLS, unencrypted TLS records are
1414 * sent down the stack to the NIC.
1416 if (tls->mode == TCP_TLS_MODE_SW) {
1417 m->m_flags |= M_NOTREADY;
1418 m->m_epg_nrdy = m->m_epg_npgs;
1419 *enq_cnt += m->m_epg_npgs;
1425 ktls_enqueue_to_free(struct mbuf *m)
1430 /* Mark it for freeing. */
1431 m->m_epg_flags |= EPG_FLAG_2FREE;
1432 wq = &ktls_wq[m->m_epg_tls->wq_index];
1434 STAILQ_INSERT_TAIL(&wq->head, m, m_epg_stailq);
1435 running = wq->running;
1436 mtx_unlock(&wq->mtx);
1442 ktls_enqueue(struct mbuf *m, struct socket *so, int page_count)
1447 KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) ==
1448 (M_EXTPG | M_NOTREADY)),
1449 ("ktls_enqueue: %p not unready & nomap mbuf\n", m));
1450 KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count"));
1452 KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf"));
1454 m->m_epg_enc_cnt = page_count;
1457 * Save a pointer to the socket. The caller is responsible
1458 * for taking an additional reference via soref().
1462 wq = &ktls_wq[m->m_epg_tls->wq_index];
1464 STAILQ_INSERT_TAIL(&wq->head, m, m_epg_stailq);
1465 running = wq->running;
1466 mtx_unlock(&wq->mtx);
1469 counter_u64_add(ktls_cnt_on, 1);
1472 static __noinline void
1473 ktls_encrypt(struct mbuf *top)
1475 struct ktls_session *tls;
1478 vm_paddr_t parray[1 + btoc(TLS_MAX_MSG_SIZE_V10_2)];
1479 struct iovec src_iov[1 + btoc(TLS_MAX_MSG_SIZE_V10_2)];
1480 struct iovec dst_iov[1 + btoc(TLS_MAX_MSG_SIZE_V10_2)];
1482 int error, i, len, npages, off, total_pages;
1486 tls = top->m_epg_tls;
1487 KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top));
1488 KASSERT(so != NULL, ("so = NULL, top = %p\n", top));
1490 top->m_epg_so = NULL;
1492 total_pages = top->m_epg_enc_cnt;
1496 * Encrypt the TLS records in the chain of mbufs starting with
1497 * 'top'. 'total_pages' gives us a total count of pages and is
1498 * used to know when we have finished encrypting the TLS
1499 * records originally queued with 'top'.
1501 * NB: These mbufs are queued in the socket buffer and
1502 * 'm_next' is traversing the mbufs in the socket buffer. The
1503 * socket buffer lock is not held while traversing this chain.
1504 * Since the mbufs are all marked M_NOTREADY their 'm_next'
1505 * pointers should be stable. However, the 'm_next' of the
1506 * last mbuf encrypted is not necessarily NULL. It can point
1507 * to other mbufs appended while 'top' was on the TLS work
1510 * Each mbuf holds an entire TLS record.
1513 for (m = top; npages != total_pages; m = m->m_next) {
1514 KASSERT(m->m_epg_tls == tls,
1515 ("different TLS sessions in a single mbuf chain: %p vs %p",
1516 tls, m->m_epg_tls));
1517 KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) ==
1518 (M_EXTPG | M_NOTREADY),
1519 ("%p not unready & nomap mbuf (top = %p)\n", m, top));
1520 KASSERT(npages + m->m_epg_npgs <= total_pages,
1521 ("page count mismatch: top %p, total_pages %d, m %p", top,
1525 * Generate source and destination ivoecs to pass to
1526 * the SW encryption backend. For writable mbufs, the
1527 * destination iovec is a copy of the source and
1528 * encryption is done in place. For file-backed mbufs
1529 * (from sendfile), anonymous wired pages are
1530 * allocated and assigned to the destination iovec.
1532 is_anon = (m->m_epg_flags & EPG_FLAG_ANON) != 0;
1534 off = m->m_epg_1st_off;
1535 for (i = 0; i < m->m_epg_npgs; i++, off = 0) {
1536 len = m_epg_pagelen(m, i, off);
1537 src_iov[i].iov_len = len;
1538 src_iov[i].iov_base =
1539 (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[i]) +
1543 dst_iov[i].iov_base = src_iov[i].iov_base;
1544 dst_iov[i].iov_len = src_iov[i].iov_len;
1548 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
1549 VM_ALLOC_NOOBJ | VM_ALLOC_NODUMP | VM_ALLOC_WIRED);
1554 parray[i] = VM_PAGE_TO_PHYS(pg);
1555 dst_iov[i].iov_base =
1556 (char *)(void *)PHYS_TO_DMAP(parray[i]) + off;
1557 dst_iov[i].iov_len = len;
1562 error = (*tls->sw_encrypt)(tls,
1563 (const struct tls_record_layer *)m->m_epg_hdr,
1564 m->m_epg_trail, src_iov, dst_iov, i, m->m_epg_seqno,
1565 m->m_epg_record_type);
1567 counter_u64_add(ktls_offload_failed_crypto, 1);
1572 * For file-backed mbufs, release the file-backed
1573 * pages and replace them in the ext_pgs array with
1574 * the anonymous wired pages allocated above.
1577 /* Free the old pages. */
1578 m->m_ext.ext_free(m);
1580 /* Replace them with the new pages. */
1581 for (i = 0; i < m->m_epg_npgs; i++)
1582 m->m_epg_pa[i] = parray[i];
1584 /* Use the basic free routine. */
1585 m->m_ext.ext_free = mb_free_mext_pgs;
1587 /* Pages are now writable. */
1588 m->m_epg_flags |= EPG_FLAG_ANON;
1592 * Drop a reference to the session now that it is no
1593 * longer needed. Existing code depends on encrypted
1594 * records having no associated session vs
1595 * yet-to-be-encrypted records having an associated
1598 m->m_epg_tls = NULL;
1602 CURVNET_SET(so->so_vnet);
1604 (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, top, npages);
1606 so->so_proto->pr_usrreqs->pru_abort(so);
1608 mb_free_notready(top, total_pages);
1617 ktls_work_thread(void *ctx)
1619 struct ktls_wq *wq = ctx;
1621 STAILQ_HEAD(, mbuf) local_head;
1623 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
1628 while (STAILQ_EMPTY(&wq->head)) {
1629 wq->running = false;
1630 mtx_sleep(wq, &wq->mtx, 0, "-", 0);
1634 STAILQ_INIT(&local_head);
1635 STAILQ_CONCAT(&local_head, &wq->head);
1636 mtx_unlock(&wq->mtx);
1638 STAILQ_FOREACH_SAFE(m, &local_head, m_epg_stailq, n) {
1639 if (m->m_epg_flags & EPG_FLAG_2FREE) {
1640 ktls_free(m->m_epg_tls);
1641 uma_zfree(zone_mbuf, m);
1644 counter_u64_add(ktls_cnt_on, -1);