]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_ktls.c
ktls: Reject attempts to enable AES-CBC with TLS 1.3.
[FreeBSD/FreeBSD.git] / sys / kern / uipc_ktls.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014-2019 Netflix Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35 #include "opt_rss.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/domainset.h>
40 #include <sys/endian.h>
41 #include <sys/ktls.h>
42 #include <sys/lock.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/rmlock.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/refcount.h>
49 #include <sys/smp.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>
55 #include <sys/uio.h>
56 #include <sys/vmmeter.h>
57 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
58 #include <machine/pcb.h>
59 #endif
60 #include <machine/vmparam.h>
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #ifdef RSS
64 #include <net/netisr.h>
65 #include <net/rss_config.h>
66 #endif
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>
72 #endif
73 #include <netinet/tcp_var.h>
74 #ifdef TCP_OFFLOAD
75 #include <netinet/tcp_offload.h>
76 #endif
77 #include <opencrypto/cryptodev.h>
78 #include <opencrypto/ktls.h>
79 #include <vm/uma_dbg.h>
80 #include <vm/vm.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pagequeue.h>
84
85 struct ktls_wq {
86         struct mtx      mtx;
87         STAILQ_HEAD(, mbuf) m_head;
88         STAILQ_HEAD(, socket) so_head;
89         bool            running;
90         int             lastallocfail;
91 } __aligned(CACHE_LINE_SIZE);
92
93 struct ktls_alloc_thread {
94         uint64_t wakeups;
95         uint64_t allocs;
96         struct thread *td;
97         int running;
98 };
99
100 struct ktls_domain_info {
101         int count;
102         int cpu[MAXCPU];
103         struct ktls_alloc_thread alloc_td;
104 };
105
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
113 SYSCTL_NODE(_kern_ipc, OID_AUTO, tls, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
114     "Kernel TLS offload");
115 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
116     "Kernel TLS offload stats");
117
118 #ifdef RSS
119 static int ktls_bind_threads = 1;
120 #else
121 static int ktls_bind_threads;
122 #endif
123 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, bind_threads, CTLFLAG_RDTUN,
124     &ktls_bind_threads, 0,
125     "Bind crypto threads to cores (1) or cores and domains (2) at boot");
126
127 static u_int ktls_maxlen = 16384;
128 SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, maxlen, CTLFLAG_RDTUN,
129     &ktls_maxlen, 0, "Maximum TLS record size");
130
131 static int ktls_number_threads;
132 SYSCTL_INT(_kern_ipc_tls_stats, OID_AUTO, threads, CTLFLAG_RD,
133     &ktls_number_threads, 0,
134     "Number of TLS threads in thread-pool");
135
136 unsigned int ktls_ifnet_max_rexmit_pct = 2;
137 SYSCTL_UINT(_kern_ipc_tls, OID_AUTO, ifnet_max_rexmit_pct, CTLFLAG_RWTUN,
138     &ktls_ifnet_max_rexmit_pct, 2,
139     "Max percent bytes retransmitted before ifnet TLS is disabled");
140
141 static bool ktls_offload_enable;
142 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, enable, CTLFLAG_RWTUN,
143     &ktls_offload_enable, 0,
144     "Enable support for kernel TLS offload");
145
146 static bool ktls_cbc_enable = true;
147 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, cbc_enable, CTLFLAG_RWTUN,
148     &ktls_cbc_enable, 1,
149     "Enable Support of AES-CBC crypto for kernel TLS");
150
151 static bool ktls_sw_buffer_cache = true;
152 SYSCTL_BOOL(_kern_ipc_tls, OID_AUTO, sw_buffer_cache, CTLFLAG_RDTUN,
153     &ktls_sw_buffer_cache, 1,
154     "Enable caching of output buffers for SW encryption");
155
156 static int ktls_max_alloc = 128;
157 SYSCTL_INT(_kern_ipc_tls, OID_AUTO, max_alloc, CTLFLAG_RWTUN,
158     &ktls_max_alloc, 128,
159     "Max number of 16k buffers to allocate in thread context");
160
161 static COUNTER_U64_DEFINE_EARLY(ktls_tasks_active);
162 SYSCTL_COUNTER_U64(_kern_ipc_tls, OID_AUTO, tasks_active, CTLFLAG_RD,
163     &ktls_tasks_active, "Number of active tasks");
164
165 static COUNTER_U64_DEFINE_EARLY(ktls_cnt_tx_queued);
166 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_tx_inqueue, CTLFLAG_RD,
167     &ktls_cnt_tx_queued,
168     "Number of TLS records in queue to tasks for SW encryption");
169
170 static COUNTER_U64_DEFINE_EARLY(ktls_cnt_rx_queued);
171 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, sw_rx_inqueue, CTLFLAG_RD,
172     &ktls_cnt_rx_queued,
173     "Number of TLS sockets in queue to tasks for SW decryption");
174
175 static COUNTER_U64_DEFINE_EARLY(ktls_offload_total);
176 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, offload_total,
177     CTLFLAG_RD, &ktls_offload_total,
178     "Total successful TLS setups (parameters set)");
179
180 static COUNTER_U64_DEFINE_EARLY(ktls_offload_enable_calls);
181 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, enable_calls,
182     CTLFLAG_RD, &ktls_offload_enable_calls,
183     "Total number of TLS enable calls made");
184
185 static COUNTER_U64_DEFINE_EARLY(ktls_offload_active);
186 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, active, CTLFLAG_RD,
187     &ktls_offload_active, "Total Active TLS sessions");
188
189 static COUNTER_U64_DEFINE_EARLY(ktls_offload_corrupted_records);
190 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, corrupted_records, CTLFLAG_RD,
191     &ktls_offload_corrupted_records, "Total corrupted TLS records received");
192
193 static COUNTER_U64_DEFINE_EARLY(ktls_offload_failed_crypto);
194 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, failed_crypto, CTLFLAG_RD,
195     &ktls_offload_failed_crypto, "Total TLS crypto failures");
196
197 static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_ifnet);
198 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_ifnet, CTLFLAG_RD,
199     &ktls_switch_to_ifnet, "TLS sessions switched from SW to ifnet");
200
201 static COUNTER_U64_DEFINE_EARLY(ktls_switch_to_sw);
202 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_to_sw, CTLFLAG_RD,
203     &ktls_switch_to_sw, "TLS sessions switched from ifnet to SW");
204
205 static COUNTER_U64_DEFINE_EARLY(ktls_switch_failed);
206 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, switch_failed, CTLFLAG_RD,
207     &ktls_switch_failed, "TLS sessions unable to switch between SW and ifnet");
208
209 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_fail);
210 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_failed, CTLFLAG_RD,
211     &ktls_ifnet_disable_fail, "TLS sessions unable to switch to SW from ifnet");
212
213 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_disable_ok);
214 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats, OID_AUTO, ifnet_disable_ok, CTLFLAG_RD,
215     &ktls_ifnet_disable_ok, "TLS sessions able to switch to SW from ifnet");
216
217 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, sw, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
218     "Software TLS session stats");
219 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, ifnet, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
220     "Hardware (ifnet) TLS session stats");
221 #ifdef TCP_OFFLOAD
222 SYSCTL_NODE(_kern_ipc_tls, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
223     "TOE TLS session stats");
224 #endif
225
226 static COUNTER_U64_DEFINE_EARLY(ktls_sw_cbc);
227 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, cbc, CTLFLAG_RD, &ktls_sw_cbc,
228     "Active number of software TLS sessions using AES-CBC");
229
230 static COUNTER_U64_DEFINE_EARLY(ktls_sw_gcm);
231 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, gcm, CTLFLAG_RD, &ktls_sw_gcm,
232     "Active number of software TLS sessions using AES-GCM");
233
234 static COUNTER_U64_DEFINE_EARLY(ktls_sw_chacha20);
235 SYSCTL_COUNTER_U64(_kern_ipc_tls_sw, OID_AUTO, chacha20, CTLFLAG_RD,
236     &ktls_sw_chacha20,
237     "Active number of software TLS sessions using Chacha20-Poly1305");
238
239 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_cbc);
240 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, cbc, CTLFLAG_RD,
241     &ktls_ifnet_cbc,
242     "Active number of ifnet TLS sessions using AES-CBC");
243
244 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_gcm);
245 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, gcm, CTLFLAG_RD,
246     &ktls_ifnet_gcm,
247     "Active number of ifnet TLS sessions using AES-GCM");
248
249 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_chacha20);
250 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, chacha20, CTLFLAG_RD,
251     &ktls_ifnet_chacha20,
252     "Active number of ifnet TLS sessions using Chacha20-Poly1305");
253
254 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset);
255 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset, CTLFLAG_RD,
256     &ktls_ifnet_reset, "TLS sessions updated to a new ifnet send tag");
257
258 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_dropped);
259 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_dropped, CTLFLAG_RD,
260     &ktls_ifnet_reset_dropped,
261     "TLS sessions dropped after failing to update ifnet send tag");
262
263 static COUNTER_U64_DEFINE_EARLY(ktls_ifnet_reset_failed);
264 SYSCTL_COUNTER_U64(_kern_ipc_tls_ifnet, OID_AUTO, reset_failed, CTLFLAG_RD,
265     &ktls_ifnet_reset_failed,
266     "TLS sessions that failed to allocate a new ifnet send tag");
267
268 static int ktls_ifnet_permitted;
269 SYSCTL_UINT(_kern_ipc_tls_ifnet, OID_AUTO, permitted, CTLFLAG_RWTUN,
270     &ktls_ifnet_permitted, 1,
271     "Whether to permit hardware (ifnet) TLS sessions");
272
273 #ifdef TCP_OFFLOAD
274 static COUNTER_U64_DEFINE_EARLY(ktls_toe_cbc);
275 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, cbc, CTLFLAG_RD,
276     &ktls_toe_cbc,
277     "Active number of TOE TLS sessions using AES-CBC");
278
279 static COUNTER_U64_DEFINE_EARLY(ktls_toe_gcm);
280 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, gcm, CTLFLAG_RD,
281     &ktls_toe_gcm,
282     "Active number of TOE TLS sessions using AES-GCM");
283
284 static COUNTER_U64_DEFINE_EARLY(ktls_toe_chacha20);
285 SYSCTL_COUNTER_U64(_kern_ipc_tls_toe, OID_AUTO, chacha20, CTLFLAG_RD,
286     &ktls_toe_chacha20,
287     "Active number of TOE TLS sessions using Chacha20-Poly1305");
288 #endif
289
290 static MALLOC_DEFINE(M_KTLS, "ktls", "Kernel TLS");
291
292 static void ktls_cleanup(struct ktls_session *tls);
293 #if defined(INET) || defined(INET6)
294 static void ktls_reset_send_tag(void *context, int pending);
295 #endif
296 static void ktls_work_thread(void *ctx);
297 static void ktls_alloc_thread(void *ctx);
298
299 #if defined(INET) || defined(INET6)
300 static u_int
301 ktls_get_cpu(struct socket *so)
302 {
303         struct inpcb *inp;
304 #ifdef NUMA
305         struct ktls_domain_info *di;
306 #endif
307         u_int cpuid;
308
309         inp = sotoinpcb(so);
310 #ifdef RSS
311         cpuid = rss_hash2cpuid(inp->inp_flowid, inp->inp_flowtype);
312         if (cpuid != NETISR_CPUID_NONE)
313                 return (cpuid);
314 #endif
315         /*
316          * Just use the flowid to shard connections in a repeatable
317          * fashion.  Note that TLS 1.0 sessions rely on the
318          * serialization provided by having the same connection use
319          * the same queue.
320          */
321 #ifdef NUMA
322         if (ktls_bind_threads > 1 && inp->inp_numa_domain != M_NODOM) {
323                 di = &ktls_domains[inp->inp_numa_domain];
324                 cpuid = di->cpu[inp->inp_flowid % di->count];
325         } else
326 #endif
327                 cpuid = ktls_cpuid_lookup[inp->inp_flowid % ktls_number_threads];
328         return (cpuid);
329 }
330 #endif
331
332 static int
333 ktls_buffer_import(void *arg, void **store, int count, int domain, int flags)
334 {
335         vm_page_t m;
336         int i;
337
338         KASSERT((ktls_maxlen & PAGE_MASK) == 0,
339             ("%s: ktls max length %d is not page size-aligned",
340             __func__, ktls_maxlen));
341
342         for (i = 0; i < count; i++) {
343                 m = vm_page_alloc_contig_domain(NULL, 0, domain,
344                     VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
345                     VM_ALLOC_NODUMP | malloc2vm_flags(flags),
346                     atop(ktls_maxlen), 0, ~0ul, PAGE_SIZE, 0,
347                     VM_MEMATTR_DEFAULT);
348                 if (m == NULL)
349                         break;
350                 store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
351         }
352         return (i);
353 }
354
355 static void
356 ktls_buffer_release(void *arg __unused, void **store, int count)
357 {
358         vm_page_t m;
359         int i, j;
360
361         for (i = 0; i < count; i++) {
362                 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i]));
363                 for (j = 0; j < atop(ktls_maxlen); j++) {
364                         (void)vm_page_unwire_noq(m + j);
365                         vm_page_free(m + j);
366                 }
367         }
368 }
369
370 static void
371 ktls_free_mext_contig(struct mbuf *m)
372 {
373         M_ASSERTEXTPG(m);
374         uma_zfree(ktls_buffer_zone, (void *)PHYS_TO_DMAP(m->m_epg_pa[0]));
375 }
376
377 static void
378 ktls_init(void *dummy __unused)
379 {
380         struct thread *td;
381         struct pcpu *pc;
382         cpuset_t mask;
383         int count, domain, error, i;
384
385         ktls_wq = malloc(sizeof(*ktls_wq) * (mp_maxid + 1), M_KTLS,
386             M_WAITOK | M_ZERO);
387
388         ktls_session_zone = uma_zcreate("ktls_session",
389             sizeof(struct ktls_session),
390             NULL, NULL, NULL, NULL,
391             UMA_ALIGN_CACHE, 0);
392
393         if (ktls_sw_buffer_cache) {
394                 ktls_buffer_zone = uma_zcache_create("ktls_buffers",
395                     roundup2(ktls_maxlen, PAGE_SIZE), NULL, NULL, NULL, NULL,
396                     ktls_buffer_import, ktls_buffer_release, NULL,
397                     UMA_ZONE_FIRSTTOUCH);
398         }
399
400         /*
401          * Initialize the workqueues to run the TLS work.  We create a
402          * work queue for each CPU.
403          */
404         CPU_FOREACH(i) {
405                 STAILQ_INIT(&ktls_wq[i].m_head);
406                 STAILQ_INIT(&ktls_wq[i].so_head);
407                 mtx_init(&ktls_wq[i].mtx, "ktls work queue", NULL, MTX_DEF);
408                 error = kproc_kthread_add(ktls_work_thread, &ktls_wq[i],
409                     &ktls_proc, &td, 0, 0, "KTLS", "thr_%d", i);
410                 if (error)
411                         panic("Can't add KTLS thread %d error %d", i, error);
412
413                 /*
414                  * Bind threads to cores.  If ktls_bind_threads is >
415                  * 1, then we bind to the NUMA domain.
416                  */
417                 if (ktls_bind_threads) {
418                         if (ktls_bind_threads > 1) {
419                                 pc = pcpu_find(i);
420                                 domain = pc->pc_domain;
421                                 CPU_COPY(&cpuset_domain[domain], &mask);
422                                 count = ktls_domains[domain].count;
423                                 ktls_domains[domain].cpu[count] = i;
424                                 ktls_domains[domain].count++;
425                         } else {
426                                 CPU_SETOF(i, &mask);
427                         }
428                         error = cpuset_setthread(td->td_tid, &mask);
429                         if (error)
430                                 panic(
431                             "Unable to bind KTLS thread for CPU %d error %d",
432                                      i, error);
433                 }
434                 ktls_cpuid_lookup[ktls_number_threads] = i;
435                 ktls_number_threads++;
436         }
437
438         /*
439          * Start an allocation thread per-domain to perform blocking allocations
440          * of 16k physically contiguous TLS crypto destination buffers.
441          */
442         if (ktls_sw_buffer_cache) {
443                 for (domain = 0; domain < vm_ndomains; domain++) {
444                         if (VM_DOMAIN_EMPTY(domain))
445                                 continue;
446                         if (CPU_EMPTY(&cpuset_domain[domain]))
447                                 continue;
448                         error = kproc_kthread_add(ktls_alloc_thread,
449                             &ktls_domains[domain], &ktls_proc,
450                             &ktls_domains[domain].alloc_td.td,
451                             0, 0, "KTLS", "alloc_%d", domain);
452                         if (error)
453                                 panic("Can't add KTLS alloc thread %d error %d",
454                                     domain, error);
455                         CPU_COPY(&cpuset_domain[domain], &mask);
456                         error = cpuset_setthread(ktls_domains[domain].alloc_td.td->td_tid,
457                             &mask);
458                         if (error)
459                                 panic("Unable to bind KTLS alloc %d error %d",
460                                     domain, error);
461                 }
462         }
463
464         /*
465          * If we somehow have an empty domain, fall back to choosing
466          * among all KTLS threads.
467          */
468         if (ktls_bind_threads > 1) {
469                 for (i = 0; i < vm_ndomains; i++) {
470                         if (ktls_domains[i].count == 0) {
471                                 ktls_bind_threads = 1;
472                                 break;
473                         }
474                 }
475         }
476
477         if (bootverbose)
478                 printf("KTLS: Initialized %d threads\n", ktls_number_threads);
479 }
480 SYSINIT(ktls, SI_SUB_SMP + 1, SI_ORDER_ANY, ktls_init, NULL);
481
482 #if defined(INET) || defined(INET6)
483 static int
484 ktls_create_session(struct socket *so, struct tls_enable *en,
485     struct ktls_session **tlsp)
486 {
487         struct ktls_session *tls;
488         int error;
489
490         /* Only TLS 1.0 - 1.3 are supported. */
491         if (en->tls_vmajor != TLS_MAJOR_VER_ONE)
492                 return (EINVAL);
493         if (en->tls_vminor < TLS_MINOR_VER_ZERO ||
494             en->tls_vminor > TLS_MINOR_VER_THREE)
495                 return (EINVAL);
496
497         if (en->auth_key_len < 0 || en->auth_key_len > TLS_MAX_PARAM_SIZE)
498                 return (EINVAL);
499         if (en->cipher_key_len < 0 || en->cipher_key_len > TLS_MAX_PARAM_SIZE)
500                 return (EINVAL);
501         if (en->iv_len < 0 || en->iv_len > sizeof(tls->params.iv))
502                 return (EINVAL);
503
504         /* All supported algorithms require a cipher key. */
505         if (en->cipher_key_len == 0)
506                 return (EINVAL);
507
508         /* No flags are currently supported. */
509         if (en->flags != 0)
510                 return (EINVAL);
511
512         /* Common checks for supported algorithms. */
513         switch (en->cipher_algorithm) {
514         case CRYPTO_AES_NIST_GCM_16:
515                 /*
516                  * auth_algorithm isn't used, but permit GMAC values
517                  * for compatibility.
518                  */
519                 switch (en->auth_algorithm) {
520                 case 0:
521 #ifdef COMPAT_FREEBSD12
522                 /* XXX: Really 13.0-current COMPAT. */
523                 case CRYPTO_AES_128_NIST_GMAC:
524                 case CRYPTO_AES_192_NIST_GMAC:
525                 case CRYPTO_AES_256_NIST_GMAC:
526 #endif
527                         break;
528                 default:
529                         return (EINVAL);
530                 }
531                 if (en->auth_key_len != 0)
532                         return (EINVAL);
533                 if ((en->tls_vminor == TLS_MINOR_VER_TWO &&
534                         en->iv_len != TLS_AEAD_GCM_LEN) ||
535                     (en->tls_vminor == TLS_MINOR_VER_THREE &&
536                         en->iv_len != TLS_1_3_GCM_IV_LEN))
537                         return (EINVAL);
538                 break;
539         case CRYPTO_AES_CBC:
540                 switch (en->auth_algorithm) {
541                 case CRYPTO_SHA1_HMAC:
542                         /*
543                          * TLS 1.0 requires an implicit IV.  TLS 1.1+
544                          * all use explicit IVs.
545                          */
546                         if (en->tls_vminor == TLS_MINOR_VER_ZERO) {
547                                 if (en->iv_len != TLS_CBC_IMPLICIT_IV_LEN)
548                                         return (EINVAL);
549                                 break;
550                         }
551
552                         /* FALLTHROUGH */
553                 case CRYPTO_SHA2_256_HMAC:
554                 case CRYPTO_SHA2_384_HMAC:
555                         /* Ignore any supplied IV. */
556                         en->iv_len = 0;
557                         break;
558                 default:
559                         return (EINVAL);
560                 }
561                 if (en->auth_key_len == 0)
562                         return (EINVAL);
563                 if (en->tls_vminor != TLS_MINOR_VER_ZERO &&
564                     en->tls_vminor != TLS_MINOR_VER_ONE &&
565                     en->tls_vminor != TLS_MINOR_VER_TWO)
566                         return (EINVAL);
567                 break;
568         case CRYPTO_CHACHA20_POLY1305:
569                 if (en->auth_algorithm != 0 || en->auth_key_len != 0)
570                         return (EINVAL);
571                 if (en->tls_vminor != TLS_MINOR_VER_TWO &&
572                     en->tls_vminor != TLS_MINOR_VER_THREE)
573                         return (EINVAL);
574                 if (en->iv_len != TLS_CHACHA20_IV_LEN)
575                         return (EINVAL);
576                 break;
577         default:
578                 return (EINVAL);
579         }
580
581         tls = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
582
583         counter_u64_add(ktls_offload_active, 1);
584
585         refcount_init(&tls->refcount, 1);
586         TASK_INIT(&tls->reset_tag_task, 0, ktls_reset_send_tag, tls);
587
588         tls->wq_index = ktls_get_cpu(so);
589
590         tls->params.cipher_algorithm = en->cipher_algorithm;
591         tls->params.auth_algorithm = en->auth_algorithm;
592         tls->params.tls_vmajor = en->tls_vmajor;
593         tls->params.tls_vminor = en->tls_vminor;
594         tls->params.flags = en->flags;
595         tls->params.max_frame_len = min(TLS_MAX_MSG_SIZE_V10_2, ktls_maxlen);
596
597         /* Set the header and trailer lengths. */
598         tls->params.tls_hlen = sizeof(struct tls_record_layer);
599         switch (en->cipher_algorithm) {
600         case CRYPTO_AES_NIST_GCM_16:
601                 /*
602                  * TLS 1.2 uses a 4 byte implicit IV with an explicit 8 byte
603                  * nonce.  TLS 1.3 uses a 12 byte implicit IV.
604                  */
605                 if (en->tls_vminor < TLS_MINOR_VER_THREE)
606                         tls->params.tls_hlen += sizeof(uint64_t);
607                 tls->params.tls_tlen = AES_GMAC_HASH_LEN;
608                 tls->params.tls_bs = 1;
609                 break;
610         case CRYPTO_AES_CBC:
611                 switch (en->auth_algorithm) {
612                 case CRYPTO_SHA1_HMAC:
613                         if (en->tls_vminor == TLS_MINOR_VER_ZERO) {
614                                 /* Implicit IV, no nonce. */
615                         } else {
616                                 tls->params.tls_hlen += AES_BLOCK_LEN;
617                         }
618                         tls->params.tls_tlen = AES_BLOCK_LEN +
619                             SHA1_HASH_LEN;
620                         break;
621                 case CRYPTO_SHA2_256_HMAC:
622                         tls->params.tls_hlen += AES_BLOCK_LEN;
623                         tls->params.tls_tlen = AES_BLOCK_LEN +
624                             SHA2_256_HASH_LEN;
625                         break;
626                 case CRYPTO_SHA2_384_HMAC:
627                         tls->params.tls_hlen += AES_BLOCK_LEN;
628                         tls->params.tls_tlen = AES_BLOCK_LEN +
629                             SHA2_384_HASH_LEN;
630                         break;
631                 default:
632                         panic("invalid hmac");
633                 }
634                 tls->params.tls_bs = AES_BLOCK_LEN;
635                 break;
636         case CRYPTO_CHACHA20_POLY1305:
637                 /*
638                  * Chacha20 uses a 12 byte implicit IV.
639                  */
640                 tls->params.tls_tlen = POLY1305_HASH_LEN;
641                 tls->params.tls_bs = 1;
642                 break;
643         default:
644                 panic("invalid cipher");
645         }
646
647         /*
648          * TLS 1.3 includes optional padding which we do not support,
649          * and also puts the "real" record type at the end of the
650          * encrypted data.
651          */
652         if (en->tls_vminor == TLS_MINOR_VER_THREE)
653                 tls->params.tls_tlen += sizeof(uint8_t);
654
655         KASSERT(tls->params.tls_hlen <= MBUF_PEXT_HDR_LEN,
656             ("TLS header length too long: %d", tls->params.tls_hlen));
657         KASSERT(tls->params.tls_tlen <= MBUF_PEXT_TRAIL_LEN,
658             ("TLS trailer length too long: %d", tls->params.tls_tlen));
659
660         if (en->auth_key_len != 0) {
661                 tls->params.auth_key_len = en->auth_key_len;
662                 tls->params.auth_key = malloc(en->auth_key_len, M_KTLS,
663                     M_WAITOK);
664                 error = copyin(en->auth_key, tls->params.auth_key,
665                     en->auth_key_len);
666                 if (error)
667                         goto out;
668         }
669
670         tls->params.cipher_key_len = en->cipher_key_len;
671         tls->params.cipher_key = malloc(en->cipher_key_len, M_KTLS, M_WAITOK);
672         error = copyin(en->cipher_key, tls->params.cipher_key,
673             en->cipher_key_len);
674         if (error)
675                 goto out;
676
677         /*
678          * This holds the implicit portion of the nonce for AEAD
679          * ciphers and the initial implicit IV for TLS 1.0.  The
680          * explicit portions of the IV are generated in ktls_frame().
681          */
682         if (en->iv_len != 0) {
683                 tls->params.iv_len = en->iv_len;
684                 error = copyin(en->iv, tls->params.iv, en->iv_len);
685                 if (error)
686                         goto out;
687
688                 /*
689                  * For TLS 1.2 with GCM, generate an 8-byte nonce as a
690                  * counter to generate unique explicit IVs.
691                  *
692                  * Store this counter in the last 8 bytes of the IV
693                  * array so that it is 8-byte aligned.
694                  */
695                 if (en->cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
696                     en->tls_vminor == TLS_MINOR_VER_TWO)
697                         arc4rand(tls->params.iv + 8, sizeof(uint64_t), 0);
698         }
699
700         *tlsp = tls;
701         return (0);
702
703 out:
704         ktls_cleanup(tls);
705         return (error);
706 }
707
708 static struct ktls_session *
709 ktls_clone_session(struct ktls_session *tls)
710 {
711         struct ktls_session *tls_new;
712
713         tls_new = uma_zalloc(ktls_session_zone, M_WAITOK | M_ZERO);
714
715         counter_u64_add(ktls_offload_active, 1);
716
717         refcount_init(&tls_new->refcount, 1);
718         TASK_INIT(&tls_new->reset_tag_task, 0, ktls_reset_send_tag, tls_new);
719
720         /* Copy fields from existing session. */
721         tls_new->params = tls->params;
722         tls_new->wq_index = tls->wq_index;
723
724         /* Deep copy keys. */
725         if (tls_new->params.auth_key != NULL) {
726                 tls_new->params.auth_key = malloc(tls->params.auth_key_len,
727                     M_KTLS, M_WAITOK);
728                 memcpy(tls_new->params.auth_key, tls->params.auth_key,
729                     tls->params.auth_key_len);
730         }
731
732         tls_new->params.cipher_key = malloc(tls->params.cipher_key_len, M_KTLS,
733             M_WAITOK);
734         memcpy(tls_new->params.cipher_key, tls->params.cipher_key,
735             tls->params.cipher_key_len);
736
737         return (tls_new);
738 }
739 #endif
740
741 static void
742 ktls_cleanup(struct ktls_session *tls)
743 {
744
745         counter_u64_add(ktls_offload_active, -1);
746         switch (tls->mode) {
747         case TCP_TLS_MODE_SW:
748                 switch (tls->params.cipher_algorithm) {
749                 case CRYPTO_AES_CBC:
750                         counter_u64_add(ktls_sw_cbc, -1);
751                         break;
752                 case CRYPTO_AES_NIST_GCM_16:
753                         counter_u64_add(ktls_sw_gcm, -1);
754                         break;
755                 case CRYPTO_CHACHA20_POLY1305:
756                         counter_u64_add(ktls_sw_chacha20, -1);
757                         break;
758                 }
759                 ktls_ocf_free(tls);
760                 break;
761         case TCP_TLS_MODE_IFNET:
762                 switch (tls->params.cipher_algorithm) {
763                 case CRYPTO_AES_CBC:
764                         counter_u64_add(ktls_ifnet_cbc, -1);
765                         break;
766                 case CRYPTO_AES_NIST_GCM_16:
767                         counter_u64_add(ktls_ifnet_gcm, -1);
768                         break;
769                 case CRYPTO_CHACHA20_POLY1305:
770                         counter_u64_add(ktls_ifnet_chacha20, -1);
771                         break;
772                 }
773                 if (tls->snd_tag != NULL)
774                         m_snd_tag_rele(tls->snd_tag);
775                 break;
776 #ifdef TCP_OFFLOAD
777         case TCP_TLS_MODE_TOE:
778                 switch (tls->params.cipher_algorithm) {
779                 case CRYPTO_AES_CBC:
780                         counter_u64_add(ktls_toe_cbc, -1);
781                         break;
782                 case CRYPTO_AES_NIST_GCM_16:
783                         counter_u64_add(ktls_toe_gcm, -1);
784                         break;
785                 case CRYPTO_CHACHA20_POLY1305:
786                         counter_u64_add(ktls_toe_chacha20, -1);
787                         break;
788                 }
789                 break;
790 #endif
791         }
792         if (tls->params.auth_key != NULL) {
793                 zfree(tls->params.auth_key, M_KTLS);
794                 tls->params.auth_key = NULL;
795                 tls->params.auth_key_len = 0;
796         }
797         if (tls->params.cipher_key != NULL) {
798                 zfree(tls->params.cipher_key, M_KTLS);
799                 tls->params.cipher_key = NULL;
800                 tls->params.cipher_key_len = 0;
801         }
802         explicit_bzero(tls->params.iv, sizeof(tls->params.iv));
803 }
804
805 #if defined(INET) || defined(INET6)
806
807 #ifdef TCP_OFFLOAD
808 static int
809 ktls_try_toe(struct socket *so, struct ktls_session *tls, int direction)
810 {
811         struct inpcb *inp;
812         struct tcpcb *tp;
813         int error;
814
815         inp = so->so_pcb;
816         INP_WLOCK(inp);
817         if (inp->inp_flags2 & INP_FREED) {
818                 INP_WUNLOCK(inp);
819                 return (ECONNRESET);
820         }
821         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
822                 INP_WUNLOCK(inp);
823                 return (ECONNRESET);
824         }
825         if (inp->inp_socket == NULL) {
826                 INP_WUNLOCK(inp);
827                 return (ECONNRESET);
828         }
829         tp = intotcpcb(inp);
830         if (!(tp->t_flags & TF_TOE)) {
831                 INP_WUNLOCK(inp);
832                 return (EOPNOTSUPP);
833         }
834
835         error = tcp_offload_alloc_tls_session(tp, tls, direction);
836         INP_WUNLOCK(inp);
837         if (error == 0) {
838                 tls->mode = TCP_TLS_MODE_TOE;
839                 switch (tls->params.cipher_algorithm) {
840                 case CRYPTO_AES_CBC:
841                         counter_u64_add(ktls_toe_cbc, 1);
842                         break;
843                 case CRYPTO_AES_NIST_GCM_16:
844                         counter_u64_add(ktls_toe_gcm, 1);
845                         break;
846                 case CRYPTO_CHACHA20_POLY1305:
847                         counter_u64_add(ktls_toe_chacha20, 1);
848                         break;
849                 }
850         }
851         return (error);
852 }
853 #endif
854
855 /*
856  * Common code used when first enabling ifnet TLS on a connection or
857  * when allocating a new ifnet TLS session due to a routing change.
858  * This function allocates a new TLS send tag on whatever interface
859  * the connection is currently routed over.
860  */
861 static int
862 ktls_alloc_snd_tag(struct inpcb *inp, struct ktls_session *tls, bool force,
863     struct m_snd_tag **mstp)
864 {
865         union if_snd_tag_alloc_params params;
866         struct ifnet *ifp;
867         struct nhop_object *nh;
868         struct tcpcb *tp;
869         int error;
870
871         INP_RLOCK(inp);
872         if (inp->inp_flags2 & INP_FREED) {
873                 INP_RUNLOCK(inp);
874                 return (ECONNRESET);
875         }
876         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
877                 INP_RUNLOCK(inp);
878                 return (ECONNRESET);
879         }
880         if (inp->inp_socket == NULL) {
881                 INP_RUNLOCK(inp);
882                 return (ECONNRESET);
883         }
884         tp = intotcpcb(inp);
885
886         /*
887          * Check administrative controls on ifnet TLS to determine if
888          * ifnet TLS should be denied.
889          *
890          * - Always permit 'force' requests.
891          * - ktls_ifnet_permitted == 0: always deny.
892          */
893         if (!force && ktls_ifnet_permitted == 0) {
894                 INP_RUNLOCK(inp);
895                 return (ENXIO);
896         }
897
898         /*
899          * XXX: Use the cached route in the inpcb to find the
900          * interface.  This should perhaps instead use
901          * rtalloc1_fib(dst, 0, 0, fibnum).  Since KTLS is only
902          * enabled after a connection has completed key negotiation in
903          * userland, the cached route will be present in practice.
904          */
905         nh = inp->inp_route.ro_nh;
906         if (nh == NULL) {
907                 INP_RUNLOCK(inp);
908                 return (ENXIO);
909         }
910         ifp = nh->nh_ifp;
911         if_ref(ifp);
912
913         /*
914          * Allocate a TLS + ratelimit tag if the connection has an
915          * existing pacing rate.
916          */
917         if (tp->t_pacing_rate != -1 &&
918             (ifp->if_capenable & IFCAP_TXTLS_RTLMT) != 0) {
919                 params.hdr.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT;
920                 params.tls_rate_limit.inp = inp;
921                 params.tls_rate_limit.tls = tls;
922                 params.tls_rate_limit.max_rate = tp->t_pacing_rate;
923         } else {
924                 params.hdr.type = IF_SND_TAG_TYPE_TLS;
925                 params.tls.inp = inp;
926                 params.tls.tls = tls;
927         }
928         params.hdr.flowid = inp->inp_flowid;
929         params.hdr.flowtype = inp->inp_flowtype;
930         params.hdr.numa_domain = inp->inp_numa_domain;
931         INP_RUNLOCK(inp);
932
933         if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
934                 error = EOPNOTSUPP;
935                 goto out;
936         }
937         if (inp->inp_vflag & INP_IPV6) {
938                 if ((ifp->if_capenable & IFCAP_TXTLS6) == 0) {
939                         error = EOPNOTSUPP;
940                         goto out;
941                 }
942         } else {
943                 if ((ifp->if_capenable & IFCAP_TXTLS4) == 0) {
944                         error = EOPNOTSUPP;
945                         goto out;
946                 }
947         }
948         error = m_snd_tag_alloc(ifp, &params, mstp);
949 out:
950         if_rele(ifp);
951         return (error);
952 }
953
954 static int
955 ktls_try_ifnet(struct socket *so, struct ktls_session *tls, bool force)
956 {
957         struct m_snd_tag *mst;
958         int error;
959
960         error = ktls_alloc_snd_tag(so->so_pcb, tls, force, &mst);
961         if (error == 0) {
962                 tls->mode = TCP_TLS_MODE_IFNET;
963                 tls->snd_tag = mst;
964                 switch (tls->params.cipher_algorithm) {
965                 case CRYPTO_AES_CBC:
966                         counter_u64_add(ktls_ifnet_cbc, 1);
967                         break;
968                 case CRYPTO_AES_NIST_GCM_16:
969                         counter_u64_add(ktls_ifnet_gcm, 1);
970                         break;
971                 case CRYPTO_CHACHA20_POLY1305:
972                         counter_u64_add(ktls_ifnet_chacha20, 1);
973                         break;
974                 }
975         }
976         return (error);
977 }
978
979 static int
980 ktls_try_sw(struct socket *so, struct ktls_session *tls, int direction)
981 {
982         int error;
983
984         error = ktls_ocf_try(so, tls, direction);
985         if (error)
986                 return (error);
987         tls->mode = TCP_TLS_MODE_SW;
988         switch (tls->params.cipher_algorithm) {
989         case CRYPTO_AES_CBC:
990                 counter_u64_add(ktls_sw_cbc, 1);
991                 break;
992         case CRYPTO_AES_NIST_GCM_16:
993                 counter_u64_add(ktls_sw_gcm, 1);
994                 break;
995         case CRYPTO_CHACHA20_POLY1305:
996                 counter_u64_add(ktls_sw_chacha20, 1);
997                 break;
998         }
999         return (0);
1000 }
1001
1002 /*
1003  * KTLS RX stores data in the socket buffer as a list of TLS records,
1004  * where each record is stored as a control message containg the TLS
1005  * header followed by data mbufs containing the decrypted data.  This
1006  * is different from KTLS TX which always uses an mb_ext_pgs mbuf for
1007  * both encrypted and decrypted data.  TLS records decrypted by a NIC
1008  * should be queued to the socket buffer as records, but encrypted
1009  * data which needs to be decrypted by software arrives as a stream of
1010  * regular mbufs which need to be converted.  In addition, there may
1011  * already be pending encrypted data in the socket buffer when KTLS RX
1012  * is enabled.
1013  *
1014  * To manage not-yet-decrypted data for KTLS RX, the following scheme
1015  * is used:
1016  *
1017  * - A single chain of NOTREADY mbufs is hung off of sb_mtls.
1018  *
1019  * - ktls_check_rx checks this chain of mbufs reading the TLS header
1020  *   from the first mbuf.  Once all of the data for that TLS record is
1021  *   queued, the socket is queued to a worker thread.
1022  *
1023  * - The worker thread calls ktls_decrypt to decrypt TLS records in
1024  *   the TLS chain.  Each TLS record is detached from the TLS chain,
1025  *   decrypted, and inserted into the regular socket buffer chain as
1026  *   record starting with a control message holding the TLS header and
1027  *   a chain of mbufs holding the encrypted data.
1028  */
1029
1030 static void
1031 sb_mark_notready(struct sockbuf *sb)
1032 {
1033         struct mbuf *m;
1034
1035         m = sb->sb_mb;
1036         sb->sb_mtls = m;
1037         sb->sb_mb = NULL;
1038         sb->sb_mbtail = NULL;
1039         sb->sb_lastrecord = NULL;
1040         for (; m != NULL; m = m->m_next) {
1041                 KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt != NULL",
1042                     __func__));
1043                 KASSERT((m->m_flags & M_NOTAVAIL) == 0, ("%s: mbuf not avail",
1044                     __func__));
1045                 KASSERT(sb->sb_acc >= m->m_len, ("%s: sb_acc < m->m_len",
1046                     __func__));
1047                 m->m_flags |= M_NOTREADY;
1048                 sb->sb_acc -= m->m_len;
1049                 sb->sb_tlscc += m->m_len;
1050                 sb->sb_mtlstail = m;
1051         }
1052         KASSERT(sb->sb_acc == 0 && sb->sb_tlscc == sb->sb_ccc,
1053             ("%s: acc %u tlscc %u ccc %u", __func__, sb->sb_acc, sb->sb_tlscc,
1054             sb->sb_ccc));
1055 }
1056
1057 int
1058 ktls_enable_rx(struct socket *so, struct tls_enable *en)
1059 {
1060         struct ktls_session *tls;
1061         int error;
1062
1063         if (!ktls_offload_enable)
1064                 return (ENOTSUP);
1065         if (SOLISTENING(so))
1066                 return (EINVAL);
1067
1068         counter_u64_add(ktls_offload_enable_calls, 1);
1069
1070         /*
1071          * This should always be true since only the TCP socket option
1072          * invokes this function.
1073          */
1074         if (so->so_proto->pr_protocol != IPPROTO_TCP)
1075                 return (EINVAL);
1076
1077         /*
1078          * XXX: Don't overwrite existing sessions.  We should permit
1079          * this to support rekeying in the future.
1080          */
1081         if (so->so_rcv.sb_tls_info != NULL)
1082                 return (EALREADY);
1083
1084         if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
1085                 return (ENOTSUP);
1086
1087         /* TLS 1.3 is not yet supported. */
1088         if (en->tls_vmajor == TLS_MAJOR_VER_ONE &&
1089             en->tls_vminor == TLS_MINOR_VER_THREE)
1090                 return (ENOTSUP);
1091
1092         error = ktls_create_session(so, en, &tls);
1093         if (error)
1094                 return (error);
1095
1096 #ifdef TCP_OFFLOAD
1097         error = ktls_try_toe(so, tls, KTLS_RX);
1098         if (error)
1099 #endif
1100                 error = ktls_try_sw(so, tls, KTLS_RX);
1101
1102         if (error) {
1103                 ktls_cleanup(tls);
1104                 return (error);
1105         }
1106
1107         /* Mark the socket as using TLS offload. */
1108         SOCKBUF_LOCK(&so->so_rcv);
1109         so->so_rcv.sb_tls_seqno = be64dec(en->rec_seq);
1110         so->so_rcv.sb_tls_info = tls;
1111         so->so_rcv.sb_flags |= SB_TLS_RX;
1112
1113         /* Mark existing data as not ready until it can be decrypted. */
1114         if (tls->mode != TCP_TLS_MODE_TOE) {
1115                 sb_mark_notready(&so->so_rcv);
1116                 ktls_check_rx(&so->so_rcv);
1117         }
1118         SOCKBUF_UNLOCK(&so->so_rcv);
1119
1120         counter_u64_add(ktls_offload_total, 1);
1121
1122         return (0);
1123 }
1124
1125 int
1126 ktls_enable_tx(struct socket *so, struct tls_enable *en)
1127 {
1128         struct ktls_session *tls;
1129         struct inpcb *inp;
1130         int error;
1131
1132         if (!ktls_offload_enable)
1133                 return (ENOTSUP);
1134         if (SOLISTENING(so))
1135                 return (EINVAL);
1136
1137         counter_u64_add(ktls_offload_enable_calls, 1);
1138
1139         /*
1140          * This should always be true since only the TCP socket option
1141          * invokes this function.
1142          */
1143         if (so->so_proto->pr_protocol != IPPROTO_TCP)
1144                 return (EINVAL);
1145
1146         /*
1147          * XXX: Don't overwrite existing sessions.  We should permit
1148          * this to support rekeying in the future.
1149          */
1150         if (so->so_snd.sb_tls_info != NULL)
1151                 return (EALREADY);
1152
1153         if (en->cipher_algorithm == CRYPTO_AES_CBC && !ktls_cbc_enable)
1154                 return (ENOTSUP);
1155
1156         /* TLS requires ext pgs */
1157         if (mb_use_ext_pgs == 0)
1158                 return (ENXIO);
1159
1160         error = ktls_create_session(so, en, &tls);
1161         if (error)
1162                 return (error);
1163
1164         /* Prefer TOE -> ifnet TLS -> software TLS. */
1165 #ifdef TCP_OFFLOAD
1166         error = ktls_try_toe(so, tls, KTLS_TX);
1167         if (error)
1168 #endif
1169                 error = ktls_try_ifnet(so, tls, false);
1170         if (error)
1171                 error = ktls_try_sw(so, tls, KTLS_TX);
1172
1173         if (error) {
1174                 ktls_cleanup(tls);
1175                 return (error);
1176         }
1177
1178         error = SOCK_IO_SEND_LOCK(so, SBL_WAIT);
1179         if (error) {
1180                 ktls_cleanup(tls);
1181                 return (error);
1182         }
1183
1184         /*
1185          * Write lock the INP when setting sb_tls_info so that
1186          * routines in tcp_ratelimit.c can read sb_tls_info while
1187          * holding the INP lock.
1188          */
1189         inp = so->so_pcb;
1190         INP_WLOCK(inp);
1191         SOCKBUF_LOCK(&so->so_snd);
1192         so->so_snd.sb_tls_seqno = be64dec(en->rec_seq);
1193         so->so_snd.sb_tls_info = tls;
1194         if (tls->mode != TCP_TLS_MODE_SW)
1195                 so->so_snd.sb_flags |= SB_TLS_IFNET;
1196         SOCKBUF_UNLOCK(&so->so_snd);
1197         INP_WUNLOCK(inp);
1198         SOCK_IO_SEND_UNLOCK(so);
1199
1200         counter_u64_add(ktls_offload_total, 1);
1201
1202         return (0);
1203 }
1204
1205 int
1206 ktls_get_rx_mode(struct socket *so, int *modep)
1207 {
1208         struct ktls_session *tls;
1209         struct inpcb *inp;
1210
1211         if (SOLISTENING(so))
1212                 return (EINVAL);
1213         inp = so->so_pcb;
1214         INP_WLOCK_ASSERT(inp);
1215         SOCK_RECVBUF_LOCK(so);
1216         tls = so->so_rcv.sb_tls_info;
1217         if (tls == NULL)
1218                 *modep = TCP_TLS_MODE_NONE;
1219         else
1220                 *modep = tls->mode;
1221         SOCK_RECVBUF_UNLOCK(so);
1222         return (0);
1223 }
1224
1225 int
1226 ktls_get_tx_mode(struct socket *so, int *modep)
1227 {
1228         struct ktls_session *tls;
1229         struct inpcb *inp;
1230
1231         if (SOLISTENING(so))
1232                 return (EINVAL);
1233         inp = so->so_pcb;
1234         INP_WLOCK_ASSERT(inp);
1235         SOCK_SENDBUF_LOCK(so);
1236         tls = so->so_snd.sb_tls_info;
1237         if (tls == NULL)
1238                 *modep = TCP_TLS_MODE_NONE;
1239         else
1240                 *modep = tls->mode;
1241         SOCK_SENDBUF_UNLOCK(so);
1242         return (0);
1243 }
1244
1245 /*
1246  * Switch between SW and ifnet TLS sessions as requested.
1247  */
1248 int
1249 ktls_set_tx_mode(struct socket *so, int mode)
1250 {
1251         struct ktls_session *tls, *tls_new;
1252         struct inpcb *inp;
1253         int error;
1254
1255         if (SOLISTENING(so))
1256                 return (EINVAL);
1257         switch (mode) {
1258         case TCP_TLS_MODE_SW:
1259         case TCP_TLS_MODE_IFNET:
1260                 break;
1261         default:
1262                 return (EINVAL);
1263         }
1264
1265         inp = so->so_pcb;
1266         INP_WLOCK_ASSERT(inp);
1267         SOCKBUF_LOCK(&so->so_snd);
1268         tls = so->so_snd.sb_tls_info;
1269         if (tls == NULL) {
1270                 SOCKBUF_UNLOCK(&so->so_snd);
1271                 return (0);
1272         }
1273
1274         if (tls->mode == mode) {
1275                 SOCKBUF_UNLOCK(&so->so_snd);
1276                 return (0);
1277         }
1278
1279         tls = ktls_hold(tls);
1280         SOCKBUF_UNLOCK(&so->so_snd);
1281         INP_WUNLOCK(inp);
1282
1283         tls_new = ktls_clone_session(tls);
1284
1285         if (mode == TCP_TLS_MODE_IFNET)
1286                 error = ktls_try_ifnet(so, tls_new, true);
1287         else
1288                 error = ktls_try_sw(so, tls_new, KTLS_TX);
1289         if (error) {
1290                 counter_u64_add(ktls_switch_failed, 1);
1291                 ktls_free(tls_new);
1292                 ktls_free(tls);
1293                 INP_WLOCK(inp);
1294                 return (error);
1295         }
1296
1297         error = SOCK_IO_SEND_LOCK(so, SBL_WAIT);
1298         if (error) {
1299                 counter_u64_add(ktls_switch_failed, 1);
1300                 ktls_free(tls_new);
1301                 ktls_free(tls);
1302                 INP_WLOCK(inp);
1303                 return (error);
1304         }
1305
1306         /*
1307          * If we raced with another session change, keep the existing
1308          * session.
1309          */
1310         if (tls != so->so_snd.sb_tls_info) {
1311                 counter_u64_add(ktls_switch_failed, 1);
1312                 SOCK_IO_SEND_UNLOCK(so);
1313                 ktls_free(tls_new);
1314                 ktls_free(tls);
1315                 INP_WLOCK(inp);
1316                 return (EBUSY);
1317         }
1318
1319         SOCKBUF_LOCK(&so->so_snd);
1320         so->so_snd.sb_tls_info = tls_new;
1321         if (tls_new->mode != TCP_TLS_MODE_SW)
1322                 so->so_snd.sb_flags |= SB_TLS_IFNET;
1323         SOCKBUF_UNLOCK(&so->so_snd);
1324         SOCK_IO_SEND_UNLOCK(so);
1325
1326         /*
1327          * Drop two references on 'tls'.  The first is for the
1328          * ktls_hold() above.  The second drops the reference from the
1329          * socket buffer.
1330          */
1331         KASSERT(tls->refcount >= 2, ("too few references on old session"));
1332         ktls_free(tls);
1333         ktls_free(tls);
1334
1335         if (mode == TCP_TLS_MODE_IFNET)
1336                 counter_u64_add(ktls_switch_to_ifnet, 1);
1337         else
1338                 counter_u64_add(ktls_switch_to_sw, 1);
1339
1340         INP_WLOCK(inp);
1341         return (0);
1342 }
1343
1344 /*
1345  * Try to allocate a new TLS send tag.  This task is scheduled when
1346  * ip_output detects a route change while trying to transmit a packet
1347  * holding a TLS record.  If a new tag is allocated, replace the tag
1348  * in the TLS session.  Subsequent packets on the connection will use
1349  * the new tag.  If a new tag cannot be allocated, drop the
1350  * connection.
1351  */
1352 static void
1353 ktls_reset_send_tag(void *context, int pending)
1354 {
1355         struct epoch_tracker et;
1356         struct ktls_session *tls;
1357         struct m_snd_tag *old, *new;
1358         struct inpcb *inp;
1359         struct tcpcb *tp;
1360         int error;
1361
1362         MPASS(pending == 1);
1363
1364         tls = context;
1365         inp = tls->inp;
1366
1367         /*
1368          * Free the old tag first before allocating a new one.
1369          * ip[6]_output_send() will treat a NULL send tag the same as
1370          * an ifp mismatch and drop packets until a new tag is
1371          * allocated.
1372          *
1373          * Write-lock the INP when changing tls->snd_tag since
1374          * ip[6]_output_send() holds a read-lock when reading the
1375          * pointer.
1376          */
1377         INP_WLOCK(inp);
1378         old = tls->snd_tag;
1379         tls->snd_tag = NULL;
1380         INP_WUNLOCK(inp);
1381         if (old != NULL)
1382                 m_snd_tag_rele(old);
1383
1384         error = ktls_alloc_snd_tag(inp, tls, true, &new);
1385
1386         if (error == 0) {
1387                 INP_WLOCK(inp);
1388                 tls->snd_tag = new;
1389                 mtx_pool_lock(mtxpool_sleep, tls);
1390                 tls->reset_pending = false;
1391                 mtx_pool_unlock(mtxpool_sleep, tls);
1392                 if (!in_pcbrele_wlocked(inp))
1393                         INP_WUNLOCK(inp);
1394
1395                 counter_u64_add(ktls_ifnet_reset, 1);
1396
1397                 /*
1398                  * XXX: Should we kick tcp_output explicitly now that
1399                  * the send tag is fixed or just rely on timers?
1400                  */
1401         } else {
1402                 NET_EPOCH_ENTER(et);
1403                 INP_WLOCK(inp);
1404                 if (!in_pcbrele_wlocked(inp)) {
1405                         if (!(inp->inp_flags & INP_TIMEWAIT) &&
1406                             !(inp->inp_flags & INP_DROPPED)) {
1407                                 tp = intotcpcb(inp);
1408                                 CURVNET_SET(tp->t_vnet);
1409                                 tp = tcp_drop(tp, ECONNABORTED);
1410                                 CURVNET_RESTORE();
1411                                 if (tp != NULL)
1412                                         INP_WUNLOCK(inp);
1413                                 counter_u64_add(ktls_ifnet_reset_dropped, 1);
1414                         } else
1415                                 INP_WUNLOCK(inp);
1416                 }
1417                 NET_EPOCH_EXIT(et);
1418
1419                 counter_u64_add(ktls_ifnet_reset_failed, 1);
1420
1421                 /*
1422                  * Leave reset_pending true to avoid future tasks while
1423                  * the socket goes away.
1424                  */
1425         }
1426
1427         ktls_free(tls);
1428 }
1429
1430 int
1431 ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls)
1432 {
1433
1434         if (inp == NULL)
1435                 return (ENOBUFS);
1436
1437         INP_LOCK_ASSERT(inp);
1438
1439         /*
1440          * See if we should schedule a task to update the send tag for
1441          * this session.
1442          */
1443         mtx_pool_lock(mtxpool_sleep, tls);
1444         if (!tls->reset_pending) {
1445                 (void) ktls_hold(tls);
1446                 in_pcbref(inp);
1447                 tls->inp = inp;
1448                 tls->reset_pending = true;
1449                 taskqueue_enqueue(taskqueue_thread, &tls->reset_tag_task);
1450         }
1451         mtx_pool_unlock(mtxpool_sleep, tls);
1452         return (ENOBUFS);
1453 }
1454
1455 #ifdef RATELIMIT
1456 int
1457 ktls_modify_txrtlmt(struct ktls_session *tls, uint64_t max_pacing_rate)
1458 {
1459         union if_snd_tag_modify_params params = {
1460                 .rate_limit.max_rate = max_pacing_rate,
1461                 .rate_limit.flags = M_NOWAIT,
1462         };
1463         struct m_snd_tag *mst;
1464
1465         /* Can't get to the inp, but it should be locked. */
1466         /* INP_LOCK_ASSERT(inp); */
1467
1468         MPASS(tls->mode == TCP_TLS_MODE_IFNET);
1469
1470         if (tls->snd_tag == NULL) {
1471                 /*
1472                  * Resetting send tag, ignore this change.  The
1473                  * pending reset may or may not see this updated rate
1474                  * in the tcpcb.  If it doesn't, we will just lose
1475                  * this rate change.
1476                  */
1477                 return (0);
1478         }
1479
1480         MPASS(tls->snd_tag != NULL);
1481         MPASS(tls->snd_tag->sw->type == IF_SND_TAG_TYPE_TLS_RATE_LIMIT);
1482
1483         mst = tls->snd_tag;
1484         return (mst->sw->snd_tag_modify(mst, &params));
1485 }
1486 #endif
1487 #endif
1488
1489 void
1490 ktls_destroy(struct ktls_session *tls)
1491 {
1492
1493         ktls_cleanup(tls);
1494         uma_zfree(ktls_session_zone, tls);
1495 }
1496
1497 void
1498 ktls_seq(struct sockbuf *sb, struct mbuf *m)
1499 {
1500
1501         for (; m != NULL; m = m->m_next) {
1502                 KASSERT((m->m_flags & M_EXTPG) != 0,
1503                     ("ktls_seq: mapped mbuf %p", m));
1504
1505                 m->m_epg_seqno = sb->sb_tls_seqno;
1506                 sb->sb_tls_seqno++;
1507         }
1508 }
1509
1510 /*
1511  * Add TLS framing (headers and trailers) to a chain of mbufs.  Each
1512  * mbuf in the chain must be an unmapped mbuf.  The payload of the
1513  * mbuf must be populated with the payload of each TLS record.
1514  *
1515  * The record_type argument specifies the TLS record type used when
1516  * populating the TLS header.
1517  *
1518  * The enq_count argument on return is set to the number of pages of
1519  * payload data for this entire chain that need to be encrypted via SW
1520  * encryption.  The returned value should be passed to ktls_enqueue
1521  * when scheduling encryption of this chain of mbufs.  To handle the
1522  * special case of empty fragments for TLS 1.0 sessions, an empty
1523  * fragment counts as one page.
1524  */
1525 void
1526 ktls_frame(struct mbuf *top, struct ktls_session *tls, int *enq_cnt,
1527     uint8_t record_type)
1528 {
1529         struct tls_record_layer *tlshdr;
1530         struct mbuf *m;
1531         uint64_t *noncep;
1532         uint16_t tls_len;
1533         int maxlen;
1534
1535         maxlen = tls->params.max_frame_len;
1536         *enq_cnt = 0;
1537         for (m = top; m != NULL; m = m->m_next) {
1538                 /*
1539                  * All mbufs in the chain should be TLS records whose
1540                  * payload does not exceed the maximum frame length.
1541                  *
1542                  * Empty TLS records are permitted when using CBC.
1543                  */
1544                 KASSERT(m->m_len <= maxlen &&
1545                     (tls->params.cipher_algorithm == CRYPTO_AES_CBC ?
1546                     m->m_len >= 0 : m->m_len > 0),
1547                     ("ktls_frame: m %p len %d\n", m, m->m_len));
1548
1549                 /*
1550                  * TLS frames require unmapped mbufs to store session
1551                  * info.
1552                  */
1553                 KASSERT((m->m_flags & M_EXTPG) != 0,
1554                     ("ktls_frame: mapped mbuf %p (top = %p)\n", m, top));
1555
1556                 tls_len = m->m_len;
1557
1558                 /* Save a reference to the session. */
1559                 m->m_epg_tls = ktls_hold(tls);
1560
1561                 m->m_epg_hdrlen = tls->params.tls_hlen;
1562                 m->m_epg_trllen = tls->params.tls_tlen;
1563                 if (tls->params.cipher_algorithm == CRYPTO_AES_CBC) {
1564                         int bs, delta;
1565
1566                         /*
1567                          * AES-CBC pads messages to a multiple of the
1568                          * block size.  Note that the padding is
1569                          * applied after the digest and the encryption
1570                          * is done on the "plaintext || mac || padding".
1571                          * At least one byte of padding is always
1572                          * present.
1573                          *
1574                          * Compute the final trailer length assuming
1575                          * at most one block of padding.
1576                          * tls->params.tls_tlen is the maximum
1577                          * possible trailer length (padding + digest).
1578                          * delta holds the number of excess padding
1579                          * bytes if the maximum were used.  Those
1580                          * extra bytes are removed.
1581                          */
1582                         bs = tls->params.tls_bs;
1583                         delta = (tls_len + tls->params.tls_tlen) & (bs - 1);
1584                         m->m_epg_trllen -= delta;
1585                 }
1586                 m->m_len += m->m_epg_hdrlen + m->m_epg_trllen;
1587
1588                 /* Populate the TLS header. */
1589                 tlshdr = (void *)m->m_epg_hdr;
1590                 tlshdr->tls_vmajor = tls->params.tls_vmajor;
1591
1592                 /*
1593                  * TLS 1.3 masquarades as TLS 1.2 with a record type
1594                  * of TLS_RLTYPE_APP.
1595                  */
1596                 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE &&
1597                     tls->params.tls_vmajor == TLS_MAJOR_VER_ONE) {
1598                         tlshdr->tls_vminor = TLS_MINOR_VER_TWO;
1599                         tlshdr->tls_type = TLS_RLTYPE_APP;
1600                         /* save the real record type for later */
1601                         m->m_epg_record_type = record_type;
1602                         m->m_epg_trail[0] = record_type;
1603                 } else {
1604                         tlshdr->tls_vminor = tls->params.tls_vminor;
1605                         tlshdr->tls_type = record_type;
1606                 }
1607                 tlshdr->tls_length = htons(m->m_len - sizeof(*tlshdr));
1608
1609                 /*
1610                  * Store nonces / explicit IVs after the end of the
1611                  * TLS header.
1612                  *
1613                  * For GCM with TLS 1.2, an 8 byte nonce is copied
1614                  * from the end of the IV.  The nonce is then
1615                  * incremented for use by the next record.
1616                  *
1617                  * For CBC, a random nonce is inserted for TLS 1.1+.
1618                  */
1619                 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 &&
1620                     tls->params.tls_vminor == TLS_MINOR_VER_TWO) {
1621                         noncep = (uint64_t *)(tls->params.iv + 8);
1622                         be64enc(tlshdr + 1, *noncep);
1623                         (*noncep)++;
1624                 } else if (tls->params.cipher_algorithm == CRYPTO_AES_CBC &&
1625                     tls->params.tls_vminor >= TLS_MINOR_VER_ONE)
1626                         arc4rand(tlshdr + 1, AES_BLOCK_LEN, 0);
1627
1628                 /*
1629                  * When using SW encryption, mark the mbuf not ready.
1630                  * It will be marked ready via sbready() after the
1631                  * record has been encrypted.
1632                  *
1633                  * When using ifnet TLS, unencrypted TLS records are
1634                  * sent down the stack to the NIC.
1635                  */
1636                 if (tls->mode == TCP_TLS_MODE_SW) {
1637                         m->m_flags |= M_NOTREADY;
1638                         if (__predict_false(tls_len == 0)) {
1639                                 /* TLS 1.0 empty fragment. */
1640                                 m->m_epg_nrdy = 1;
1641                         } else
1642                                 m->m_epg_nrdy = m->m_epg_npgs;
1643                         *enq_cnt += m->m_epg_nrdy;
1644                 }
1645         }
1646 }
1647
1648 void
1649 ktls_check_rx(struct sockbuf *sb)
1650 {
1651         struct tls_record_layer hdr;
1652         struct ktls_wq *wq;
1653         struct socket *so;
1654         bool running;
1655
1656         SOCKBUF_LOCK_ASSERT(sb);
1657         KASSERT(sb->sb_flags & SB_TLS_RX, ("%s: sockbuf %p isn't TLS RX",
1658             __func__, sb));
1659         so = __containerof(sb, struct socket, so_rcv);
1660
1661         if (sb->sb_flags & SB_TLS_RX_RUNNING)
1662                 return;
1663
1664         /* Is there enough queued for a TLS header? */
1665         if (sb->sb_tlscc < sizeof(hdr)) {
1666                 if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc != 0)
1667                         so->so_error = EMSGSIZE;
1668                 return;
1669         }
1670
1671         m_copydata(sb->sb_mtls, 0, sizeof(hdr), (void *)&hdr);
1672
1673         /* Is the entire record queued? */
1674         if (sb->sb_tlscc < sizeof(hdr) + ntohs(hdr.tls_length)) {
1675                 if ((sb->sb_state & SBS_CANTRCVMORE) != 0)
1676                         so->so_error = EMSGSIZE;
1677                 return;
1678         }
1679
1680         sb->sb_flags |= SB_TLS_RX_RUNNING;
1681
1682         soref(so);
1683         wq = &ktls_wq[so->so_rcv.sb_tls_info->wq_index];
1684         mtx_lock(&wq->mtx);
1685         STAILQ_INSERT_TAIL(&wq->so_head, so, so_ktls_rx_list);
1686         running = wq->running;
1687         mtx_unlock(&wq->mtx);
1688         if (!running)
1689                 wakeup(wq);
1690         counter_u64_add(ktls_cnt_rx_queued, 1);
1691 }
1692
1693 static struct mbuf *
1694 ktls_detach_record(struct sockbuf *sb, int len)
1695 {
1696         struct mbuf *m, *n, *top;
1697         int remain;
1698
1699         SOCKBUF_LOCK_ASSERT(sb);
1700         MPASS(len <= sb->sb_tlscc);
1701
1702         /*
1703          * If TLS chain is the exact size of the record,
1704          * just grab the whole record.
1705          */
1706         top = sb->sb_mtls;
1707         if (sb->sb_tlscc == len) {
1708                 sb->sb_mtls = NULL;
1709                 sb->sb_mtlstail = NULL;
1710                 goto out;
1711         }
1712
1713         /*
1714          * While it would be nice to use m_split() here, we need
1715          * to know exactly what m_split() allocates to update the
1716          * accounting, so do it inline instead.
1717          */
1718         remain = len;
1719         for (m = top; remain > m->m_len; m = m->m_next)
1720                 remain -= m->m_len;
1721
1722         /* Easy case: don't have to split 'm'. */
1723         if (remain == m->m_len) {
1724                 sb->sb_mtls = m->m_next;
1725                 if (sb->sb_mtls == NULL)
1726                         sb->sb_mtlstail = NULL;
1727                 m->m_next = NULL;
1728                 goto out;
1729         }
1730
1731         /*
1732          * Need to allocate an mbuf to hold the remainder of 'm'.  Try
1733          * with M_NOWAIT first.
1734          */
1735         n = m_get(M_NOWAIT, MT_DATA);
1736         if (n == NULL) {
1737                 /*
1738                  * Use M_WAITOK with socket buffer unlocked.  If
1739                  * 'sb_mtls' changes while the lock is dropped, return
1740                  * NULL to force the caller to retry.
1741                  */
1742                 SOCKBUF_UNLOCK(sb);
1743
1744                 n = m_get(M_WAITOK, MT_DATA);
1745
1746                 SOCKBUF_LOCK(sb);
1747                 if (sb->sb_mtls != top) {
1748                         m_free(n);
1749                         return (NULL);
1750                 }
1751         }
1752         n->m_flags |= M_NOTREADY;
1753
1754         /* Store remainder in 'n'. */
1755         n->m_len = m->m_len - remain;
1756         if (m->m_flags & M_EXT) {
1757                 n->m_data = m->m_data + remain;
1758                 mb_dupcl(n, m);
1759         } else {
1760                 bcopy(mtod(m, caddr_t) + remain, mtod(n, caddr_t), n->m_len);
1761         }
1762
1763         /* Trim 'm' and update accounting. */
1764         m->m_len -= n->m_len;
1765         sb->sb_tlscc -= n->m_len;
1766         sb->sb_ccc -= n->m_len;
1767
1768         /* Account for 'n'. */
1769         sballoc_ktls_rx(sb, n);
1770
1771         /* Insert 'n' into the TLS chain. */
1772         sb->sb_mtls = n;
1773         n->m_next = m->m_next;
1774         if (sb->sb_mtlstail == m)
1775                 sb->sb_mtlstail = n;
1776
1777         /* Detach the record from the TLS chain. */
1778         m->m_next = NULL;
1779
1780 out:
1781         MPASS(m_length(top, NULL) == len);
1782         for (m = top; m != NULL; m = m->m_next)
1783                 sbfree_ktls_rx(sb, m);
1784         sb->sb_tlsdcc = len;
1785         sb->sb_ccc += len;
1786         SBCHECK(sb);
1787         return (top);
1788 }
1789
1790 static void
1791 ktls_decrypt(struct socket *so)
1792 {
1793         char tls_header[MBUF_PEXT_HDR_LEN];
1794         struct ktls_session *tls;
1795         struct sockbuf *sb;
1796         struct tls_record_layer *hdr;
1797         struct tls_get_record tgr;
1798         struct mbuf *control, *data, *m;
1799         uint64_t seqno;
1800         int error, remain, tls_len, trail_len;
1801
1802         hdr = (struct tls_record_layer *)tls_header;
1803         sb = &so->so_rcv;
1804         SOCKBUF_LOCK(sb);
1805         KASSERT(sb->sb_flags & SB_TLS_RX_RUNNING,
1806             ("%s: socket %p not running", __func__, so));
1807
1808         tls = sb->sb_tls_info;
1809         MPASS(tls != NULL);
1810
1811         for (;;) {
1812                 /* Is there enough queued for a TLS header? */
1813                 if (sb->sb_tlscc < tls->params.tls_hlen)
1814                         break;
1815
1816                 m_copydata(sb->sb_mtls, 0, tls->params.tls_hlen, tls_header);
1817                 tls_len = sizeof(*hdr) + ntohs(hdr->tls_length);
1818
1819                 if (hdr->tls_vmajor != tls->params.tls_vmajor ||
1820                     hdr->tls_vminor != tls->params.tls_vminor)
1821                         error = EINVAL;
1822                 else if (tls_len < tls->params.tls_hlen || tls_len >
1823                     tls->params.tls_hlen + TLS_MAX_MSG_SIZE_V10_2 +
1824                     tls->params.tls_tlen)
1825                         error = EMSGSIZE;
1826                 else
1827                         error = 0;
1828                 if (__predict_false(error != 0)) {
1829                         /*
1830                          * We have a corrupted record and are likely
1831                          * out of sync.  The connection isn't
1832                          * recoverable at this point, so abort it.
1833                          */
1834                         SOCKBUF_UNLOCK(sb);
1835                         counter_u64_add(ktls_offload_corrupted_records, 1);
1836
1837                         CURVNET_SET(so->so_vnet);
1838                         so->so_proto->pr_usrreqs->pru_abort(so);
1839                         so->so_error = error;
1840                         CURVNET_RESTORE();
1841                         goto deref;
1842                 }
1843
1844                 /* Is the entire record queued? */
1845                 if (sb->sb_tlscc < tls_len)
1846                         break;
1847
1848                 /*
1849                  * Split out the portion of the mbuf chain containing
1850                  * this TLS record.
1851                  */
1852                 data = ktls_detach_record(sb, tls_len);
1853                 if (data == NULL)
1854                         continue;
1855                 MPASS(sb->sb_tlsdcc == tls_len);
1856
1857                 seqno = sb->sb_tls_seqno;
1858                 sb->sb_tls_seqno++;
1859                 SBCHECK(sb);
1860                 SOCKBUF_UNLOCK(sb);
1861
1862                 error = tls->sw_decrypt(tls, hdr, data, seqno, &trail_len);
1863                 if (error) {
1864                         counter_u64_add(ktls_offload_failed_crypto, 1);
1865
1866                         SOCKBUF_LOCK(sb);
1867                         if (sb->sb_tlsdcc == 0) {
1868                                 /*
1869                                  * sbcut/drop/flush discarded these
1870                                  * mbufs.
1871                                  */
1872                                 m_freem(data);
1873                                 break;
1874                         }
1875
1876                         /*
1877                          * Drop this TLS record's data, but keep
1878                          * decrypting subsequent records.
1879                          */
1880                         sb->sb_ccc -= tls_len;
1881                         sb->sb_tlsdcc = 0;
1882
1883                         CURVNET_SET(so->so_vnet);
1884                         so->so_error = EBADMSG;
1885                         sorwakeup_locked(so);
1886                         CURVNET_RESTORE();
1887
1888                         m_freem(data);
1889
1890                         SOCKBUF_LOCK(sb);
1891                         continue;
1892                 }
1893
1894                 /* Allocate the control mbuf. */
1895                 tgr.tls_type = hdr->tls_type;
1896                 tgr.tls_vmajor = hdr->tls_vmajor;
1897                 tgr.tls_vminor = hdr->tls_vminor;
1898                 tgr.tls_length = htobe16(tls_len - tls->params.tls_hlen -
1899                     trail_len);
1900                 control = sbcreatecontrol_how(&tgr, sizeof(tgr),
1901                     TLS_GET_RECORD, IPPROTO_TCP, M_WAITOK);
1902
1903                 SOCKBUF_LOCK(sb);
1904                 if (sb->sb_tlsdcc == 0) {
1905                         /* sbcut/drop/flush discarded these mbufs. */
1906                         MPASS(sb->sb_tlscc == 0);
1907                         m_freem(data);
1908                         m_freem(control);
1909                         break;
1910                 }
1911
1912                 /*
1913                  * Clear the 'dcc' accounting in preparation for
1914                  * adding the decrypted record.
1915                  */
1916                 sb->sb_ccc -= tls_len;
1917                 sb->sb_tlsdcc = 0;
1918                 SBCHECK(sb);
1919
1920                 /* If there is no payload, drop all of the data. */
1921                 if (tgr.tls_length == htobe16(0)) {
1922                         m_freem(data);
1923                         data = NULL;
1924                 } else {
1925                         /* Trim header. */
1926                         remain = tls->params.tls_hlen;
1927                         while (remain > 0) {
1928                                 if (data->m_len > remain) {
1929                                         data->m_data += remain;
1930                                         data->m_len -= remain;
1931                                         break;
1932                                 }
1933                                 remain -= data->m_len;
1934                                 data = m_free(data);
1935                         }
1936
1937                         /* Trim trailer and clear M_NOTREADY. */
1938                         remain = be16toh(tgr.tls_length);
1939                         m = data;
1940                         for (m = data; remain > m->m_len; m = m->m_next) {
1941                                 m->m_flags &= ~M_NOTREADY;
1942                                 remain -= m->m_len;
1943                         }
1944                         m->m_len = remain;
1945                         m_freem(m->m_next);
1946                         m->m_next = NULL;
1947                         m->m_flags &= ~M_NOTREADY;
1948
1949                         /* Set EOR on the final mbuf. */
1950                         m->m_flags |= M_EOR;
1951                 }
1952
1953                 sbappendcontrol_locked(sb, data, control, 0);
1954         }
1955
1956         sb->sb_flags &= ~SB_TLS_RX_RUNNING;
1957
1958         if ((sb->sb_state & SBS_CANTRCVMORE) != 0 && sb->sb_tlscc > 0)
1959                 so->so_error = EMSGSIZE;
1960
1961         sorwakeup_locked(so);
1962
1963 deref:
1964         SOCKBUF_UNLOCK_ASSERT(sb);
1965
1966         CURVNET_SET(so->so_vnet);
1967         SOCK_LOCK(so);
1968         sorele(so);
1969         CURVNET_RESTORE();
1970 }
1971
1972 void
1973 ktls_enqueue_to_free(struct mbuf *m)
1974 {
1975         struct ktls_wq *wq;
1976         bool running;
1977
1978         /* Mark it for freeing. */
1979         m->m_epg_flags |= EPG_FLAG_2FREE;
1980         wq = &ktls_wq[m->m_epg_tls->wq_index];
1981         mtx_lock(&wq->mtx);
1982         STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq);
1983         running = wq->running;
1984         mtx_unlock(&wq->mtx);
1985         if (!running)
1986                 wakeup(wq);
1987 }
1988
1989 static void *
1990 ktls_buffer_alloc(struct ktls_wq *wq, struct mbuf *m)
1991 {
1992         void *buf;
1993         int domain, running;
1994
1995         if (m->m_epg_npgs <= 2)
1996                 return (NULL);
1997         if (ktls_buffer_zone == NULL)
1998                 return (NULL);
1999         if ((u_int)(ticks - wq->lastallocfail) < hz) {
2000                 /*
2001                  * Rate-limit allocation attempts after a failure.
2002                  * ktls_buffer_import() will acquire a per-domain mutex to check
2003                  * the free page queues and may fail consistently if memory is
2004                  * fragmented.
2005                  */
2006                 return (NULL);
2007         }
2008         buf = uma_zalloc(ktls_buffer_zone, M_NOWAIT | M_NORECLAIM);
2009         if (buf == NULL) {
2010                 domain = PCPU_GET(domain);
2011                 wq->lastallocfail = ticks;
2012
2013                 /*
2014                  * Note that this check is "racy", but the races are
2015                  * harmless, and are either a spurious wakeup if
2016                  * multiple threads fail allocations before the alloc
2017                  * thread wakes, or waiting an extra second in case we
2018                  * see an old value of running == true.
2019                  */
2020                 if (!VM_DOMAIN_EMPTY(domain)) {
2021                         running = atomic_load_int(&ktls_domains[domain].alloc_td.running);
2022                         if (!running)
2023                                 wakeup(&ktls_domains[domain].alloc_td);
2024                 }
2025         }
2026         return (buf);
2027 }
2028
2029 static int
2030 ktls_encrypt_record(struct ktls_wq *wq, struct mbuf *m,
2031     struct ktls_session *tls, struct ktls_ocf_encrypt_state *state)
2032 {
2033         vm_page_t pg;
2034         int error, i, len, off;
2035
2036         KASSERT((m->m_flags & (M_EXTPG | M_NOTREADY)) == (M_EXTPG | M_NOTREADY),
2037             ("%p not unready & nomap mbuf\n", m));
2038         KASSERT(ptoa(m->m_epg_npgs) <= ktls_maxlen,
2039             ("page count %d larger than maximum frame length %d", m->m_epg_npgs,
2040             ktls_maxlen));
2041
2042         /* Anonymous mbufs are encrypted in place. */
2043         if ((m->m_epg_flags & EPG_FLAG_ANON) != 0)
2044                 return (tls->sw_encrypt(state, tls, m, NULL, 0));
2045
2046         /*
2047          * For file-backed mbufs (from sendfile), anonymous wired
2048          * pages are allocated and used as the encryption destination.
2049          */
2050         if ((state->cbuf = ktls_buffer_alloc(wq, m)) != NULL) {
2051                 len = ptoa(m->m_epg_npgs - 1) + m->m_epg_last_len -
2052                     m->m_epg_1st_off;
2053                 state->dst_iov[0].iov_base = (char *)state->cbuf +
2054                     m->m_epg_1st_off;
2055                 state->dst_iov[0].iov_len = len;
2056                 state->parray[0] = DMAP_TO_PHYS((vm_offset_t)state->cbuf);
2057                 i = 1;
2058         } else {
2059                 off = m->m_epg_1st_off;
2060                 for (i = 0; i < m->m_epg_npgs; i++, off = 0) {
2061                         do {
2062                                 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2063                                     VM_ALLOC_NOOBJ | VM_ALLOC_NODUMP |
2064                                     VM_ALLOC_WIRED | VM_ALLOC_WAITFAIL);
2065                         } while (pg == NULL);
2066
2067                         len = m_epg_pagelen(m, i, off);
2068                         state->parray[i] = VM_PAGE_TO_PHYS(pg);
2069                         state->dst_iov[i].iov_base =
2070                             (char *)PHYS_TO_DMAP(state->parray[i]) + off;
2071                         state->dst_iov[i].iov_len = len;
2072                 }
2073         }
2074         KASSERT(i + 1 <= nitems(state->dst_iov), ("dst_iov is too small"));
2075         state->dst_iov[i].iov_base = m->m_epg_trail;
2076         state->dst_iov[i].iov_len = m->m_epg_trllen;
2077
2078         error = tls->sw_encrypt(state, tls, m, state->dst_iov, i + 1);
2079
2080         if (__predict_false(error != 0)) {
2081                 /* Free the anonymous pages. */
2082                 if (state->cbuf != NULL)
2083                         uma_zfree(ktls_buffer_zone, state->cbuf);
2084                 else {
2085                         for (i = 0; i < m->m_epg_npgs; i++) {
2086                                 pg = PHYS_TO_VM_PAGE(state->parray[i]);
2087                                 (void)vm_page_unwire_noq(pg);
2088                                 vm_page_free(pg);
2089                         }
2090                 }
2091         }
2092         return (error);
2093 }
2094
2095 void
2096 ktls_enqueue(struct mbuf *m, struct socket *so, int page_count)
2097 {
2098         struct ktls_wq *wq;
2099         bool running;
2100
2101         KASSERT(((m->m_flags & (M_EXTPG | M_NOTREADY)) ==
2102             (M_EXTPG | M_NOTREADY)),
2103             ("ktls_enqueue: %p not unready & nomap mbuf\n", m));
2104         KASSERT(page_count != 0, ("enqueueing TLS mbuf with zero page count"));
2105
2106         KASSERT(m->m_epg_tls->mode == TCP_TLS_MODE_SW, ("!SW TLS mbuf"));
2107
2108         m->m_epg_enc_cnt = page_count;
2109
2110         /*
2111          * Save a pointer to the socket.  The caller is responsible
2112          * for taking an additional reference via soref().
2113          */
2114         m->m_epg_so = so;
2115
2116         wq = &ktls_wq[m->m_epg_tls->wq_index];
2117         mtx_lock(&wq->mtx);
2118         STAILQ_INSERT_TAIL(&wq->m_head, m, m_epg_stailq);
2119         running = wq->running;
2120         mtx_unlock(&wq->mtx);
2121         if (!running)
2122                 wakeup(wq);
2123         counter_u64_add(ktls_cnt_tx_queued, 1);
2124 }
2125
2126 /*
2127  * Once a file-backed mbuf (from sendfile) has been encrypted, free
2128  * the pages from the file and replace them with the anonymous pages
2129  * allocated in ktls_encrypt_record().
2130  */
2131 static void
2132 ktls_finish_nonanon(struct mbuf *m, struct ktls_ocf_encrypt_state *state)
2133 {
2134         int i;
2135
2136         MPASS((m->m_epg_flags & EPG_FLAG_ANON) == 0);
2137
2138         /* Free the old pages. */
2139         m->m_ext.ext_free(m);
2140
2141         /* Replace them with the new pages. */
2142         if (state->cbuf != NULL) {
2143                 for (i = 0; i < m->m_epg_npgs; i++)
2144                         m->m_epg_pa[i] = state->parray[0] + ptoa(i);
2145
2146                 /* Contig pages should go back to the cache. */
2147                 m->m_ext.ext_free = ktls_free_mext_contig;
2148         } else {
2149                 for (i = 0; i < m->m_epg_npgs; i++)
2150                         m->m_epg_pa[i] = state->parray[i];
2151
2152                 /* Use the basic free routine. */
2153                 m->m_ext.ext_free = mb_free_mext_pgs;
2154         }
2155
2156         /* Pages are now writable. */
2157         m->m_epg_flags |= EPG_FLAG_ANON;
2158 }
2159
2160 static __noinline void
2161 ktls_encrypt(struct ktls_wq *wq, struct mbuf *top)
2162 {
2163         struct ktls_ocf_encrypt_state state;
2164         struct ktls_session *tls;
2165         struct socket *so;
2166         struct mbuf *m;
2167         int error, npages, total_pages;
2168
2169         so = top->m_epg_so;
2170         tls = top->m_epg_tls;
2171         KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top));
2172         KASSERT(so != NULL, ("so = NULL, top = %p\n", top));
2173 #ifdef INVARIANTS
2174         top->m_epg_so = NULL;
2175 #endif
2176         total_pages = top->m_epg_enc_cnt;
2177         npages = 0;
2178
2179         /*
2180          * Encrypt the TLS records in the chain of mbufs starting with
2181          * 'top'.  'total_pages' gives us a total count of pages and is
2182          * used to know when we have finished encrypting the TLS
2183          * records originally queued with 'top'.
2184          *
2185          * NB: These mbufs are queued in the socket buffer and
2186          * 'm_next' is traversing the mbufs in the socket buffer.  The
2187          * socket buffer lock is not held while traversing this chain.
2188          * Since the mbufs are all marked M_NOTREADY their 'm_next'
2189          * pointers should be stable.  However, the 'm_next' of the
2190          * last mbuf encrypted is not necessarily NULL.  It can point
2191          * to other mbufs appended while 'top' was on the TLS work
2192          * queue.
2193          *
2194          * Each mbuf holds an entire TLS record.
2195          */
2196         error = 0;
2197         for (m = top; npages != total_pages; m = m->m_next) {
2198                 KASSERT(m->m_epg_tls == tls,
2199                     ("different TLS sessions in a single mbuf chain: %p vs %p",
2200                     tls, m->m_epg_tls));
2201                 KASSERT(npages + m->m_epg_npgs <= total_pages,
2202                     ("page count mismatch: top %p, total_pages %d, m %p", top,
2203                     total_pages, m));
2204
2205                 error = ktls_encrypt_record(wq, m, tls, &state);
2206                 if (error) {
2207                         counter_u64_add(ktls_offload_failed_crypto, 1);
2208                         break;
2209                 }
2210
2211                 if ((m->m_epg_flags & EPG_FLAG_ANON) == 0)
2212                         ktls_finish_nonanon(m, &state);
2213
2214                 npages += m->m_epg_nrdy;
2215
2216                 /*
2217                  * Drop a reference to the session now that it is no
2218                  * longer needed.  Existing code depends on encrypted
2219                  * records having no associated session vs
2220                  * yet-to-be-encrypted records having an associated
2221                  * session.
2222                  */
2223                 m->m_epg_tls = NULL;
2224                 ktls_free(tls);
2225         }
2226
2227         CURVNET_SET(so->so_vnet);
2228         if (error == 0) {
2229                 (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, top, npages);
2230         } else {
2231                 so->so_proto->pr_usrreqs->pru_abort(so);
2232                 so->so_error = EIO;
2233                 mb_free_notready(top, total_pages);
2234         }
2235
2236         SOCK_LOCK(so);
2237         sorele(so);
2238         CURVNET_RESTORE();
2239 }
2240
2241 void
2242 ktls_encrypt_cb(struct ktls_ocf_encrypt_state *state, int error)
2243 {
2244         struct ktls_session *tls;
2245         struct socket *so;
2246         struct mbuf *m;
2247         int npages;
2248
2249         m = state->m;
2250
2251         if ((m->m_epg_flags & EPG_FLAG_ANON) == 0)
2252                 ktls_finish_nonanon(m, state);
2253
2254         so = state->so;
2255         free(state, M_KTLS);
2256
2257         /*
2258          * Drop a reference to the session now that it is no longer
2259          * needed.  Existing code depends on encrypted records having
2260          * no associated session vs yet-to-be-encrypted records having
2261          * an associated session.
2262          */
2263         tls = m->m_epg_tls;
2264         m->m_epg_tls = NULL;
2265         ktls_free(tls);
2266
2267         if (error != 0)
2268                 counter_u64_add(ktls_offload_failed_crypto, 1);
2269
2270         CURVNET_SET(so->so_vnet);
2271         npages = m->m_epg_nrdy;
2272
2273         if (error == 0) {
2274                 (void)(*so->so_proto->pr_usrreqs->pru_ready)(so, m, npages);
2275         } else {
2276                 so->so_proto->pr_usrreqs->pru_abort(so);
2277                 so->so_error = EIO;
2278                 mb_free_notready(m, npages);
2279         }
2280
2281         SOCK_LOCK(so);
2282         sorele(so);
2283         CURVNET_RESTORE();
2284 }
2285
2286 /*
2287  * Similar to ktls_encrypt, but used with asynchronous OCF backends
2288  * (coprocessors) where encryption does not use host CPU resources and
2289  * it can be beneficial to queue more requests than CPUs.
2290  */
2291 static __noinline void
2292 ktls_encrypt_async(struct ktls_wq *wq, struct mbuf *top)
2293 {
2294         struct ktls_ocf_encrypt_state *state;
2295         struct ktls_session *tls;
2296         struct socket *so;
2297         struct mbuf *m, *n;
2298         int error, mpages, npages, total_pages;
2299
2300         so = top->m_epg_so;
2301         tls = top->m_epg_tls;
2302         KASSERT(tls != NULL, ("tls = NULL, top = %p\n", top));
2303         KASSERT(so != NULL, ("so = NULL, top = %p\n", top));
2304 #ifdef INVARIANTS
2305         top->m_epg_so = NULL;
2306 #endif
2307         total_pages = top->m_epg_enc_cnt;
2308         npages = 0;
2309
2310         error = 0;
2311         for (m = top; npages != total_pages; m = n) {
2312                 KASSERT(m->m_epg_tls == tls,
2313                     ("different TLS sessions in a single mbuf chain: %p vs %p",
2314                     tls, m->m_epg_tls));
2315                 KASSERT(npages + m->m_epg_npgs <= total_pages,
2316                     ("page count mismatch: top %p, total_pages %d, m %p", top,
2317                     total_pages, m));
2318
2319                 state = malloc(sizeof(*state), M_KTLS, M_WAITOK | M_ZERO);
2320                 soref(so);
2321                 state->so = so;
2322                 state->m = m;
2323
2324                 mpages = m->m_epg_nrdy;
2325                 n = m->m_next;
2326
2327                 error = ktls_encrypt_record(wq, m, tls, state);
2328                 if (error) {
2329                         counter_u64_add(ktls_offload_failed_crypto, 1);
2330                         free(state, M_KTLS);
2331                         CURVNET_SET(so->so_vnet);
2332                         SOCK_LOCK(so);
2333                         sorele(so);
2334                         CURVNET_RESTORE();
2335                         break;
2336                 }
2337
2338                 npages += mpages;
2339         }
2340
2341         CURVNET_SET(so->so_vnet);
2342         if (error != 0) {
2343                 so->so_proto->pr_usrreqs->pru_abort(so);
2344                 so->so_error = EIO;
2345                 mb_free_notready(m, total_pages - npages);
2346         }
2347
2348         SOCK_LOCK(so);
2349         sorele(so);
2350         CURVNET_RESTORE();
2351 }
2352
2353 static void
2354 ktls_alloc_thread(void *ctx)
2355 {
2356         struct ktls_domain_info *ktls_domain = ctx;
2357         struct ktls_alloc_thread *sc = &ktls_domain->alloc_td;
2358         void **buf;
2359         struct sysctl_oid *oid;
2360         char name[80];
2361         int i, nbufs;
2362
2363         curthread->td_domain.dr_policy =
2364             DOMAINSET_PREF(PCPU_GET(domain));
2365         snprintf(name, sizeof(name), "domain%d", PCPU_GET(domain));
2366         if (bootverbose)
2367                 printf("Starting KTLS alloc thread for domain %d\n",
2368                     PCPU_GET(domain));
2369         oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_kern_ipc_tls), OID_AUTO,
2370             name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
2371         SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "allocs",
2372             CTLFLAG_RD,  &sc->allocs, 0, "buffers allocated");
2373         SYSCTL_ADD_U64(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "wakeups",
2374             CTLFLAG_RD,  &sc->wakeups, 0, "thread wakeups");
2375         SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(oid), OID_AUTO, "running",
2376             CTLFLAG_RD,  &sc->running, 0, "thread running");
2377
2378         buf = NULL;
2379         nbufs = 0;
2380         for (;;) {
2381                 atomic_store_int(&sc->running, 0);
2382                 tsleep(sc, PZERO | PNOLOCK, "-",  0);
2383                 atomic_store_int(&sc->running, 1);
2384                 sc->wakeups++;
2385                 if (nbufs != ktls_max_alloc) {
2386                         free(buf, M_KTLS);
2387                         nbufs = atomic_load_int(&ktls_max_alloc);
2388                         buf = malloc(sizeof(void *) * nbufs, M_KTLS,
2389                             M_WAITOK | M_ZERO);
2390                 }
2391                 /*
2392                  * Below we allocate nbufs with different allocation
2393                  * flags than we use when allocating normally during
2394                  * encryption in the ktls worker thread.  We specify
2395                  * M_NORECLAIM in the worker thread. However, we omit
2396                  * that flag here and add M_WAITOK so that the VM
2397                  * system is permitted to perform expensive work to
2398                  * defragment memory.  We do this here, as it does not
2399                  * matter if this thread blocks.  If we block a ktls
2400                  * worker thread, we risk developing backlogs of
2401                  * buffers to be encrypted, leading to surges of
2402                  * traffic and potential NIC output drops.
2403                  */
2404                 for (i = 0; i < nbufs; i++) {
2405                         buf[i] = uma_zalloc(ktls_buffer_zone, M_WAITOK);
2406                         sc->allocs++;
2407                 }
2408                 for (i = 0; i < nbufs; i++) {
2409                         uma_zfree(ktls_buffer_zone, buf[i]);
2410                         buf[i] = NULL;
2411                 }
2412         }
2413 }
2414
2415 static void
2416 ktls_work_thread(void *ctx)
2417 {
2418         struct ktls_wq *wq = ctx;
2419         struct mbuf *m, *n;
2420         struct socket *so, *son;
2421         STAILQ_HEAD(, mbuf) local_m_head;
2422         STAILQ_HEAD(, socket) local_so_head;
2423
2424         if (ktls_bind_threads > 1) {
2425                 curthread->td_domain.dr_policy =
2426                         DOMAINSET_PREF(PCPU_GET(domain));
2427         }
2428 #if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
2429         fpu_kern_thread(0);
2430 #endif
2431         for (;;) {
2432                 mtx_lock(&wq->mtx);
2433                 while (STAILQ_EMPTY(&wq->m_head) &&
2434                     STAILQ_EMPTY(&wq->so_head)) {
2435                         wq->running = false;
2436                         mtx_sleep(wq, &wq->mtx, 0, "-", 0);
2437                         wq->running = true;
2438                 }
2439
2440                 STAILQ_INIT(&local_m_head);
2441                 STAILQ_CONCAT(&local_m_head, &wq->m_head);
2442                 STAILQ_INIT(&local_so_head);
2443                 STAILQ_CONCAT(&local_so_head, &wq->so_head);
2444                 mtx_unlock(&wq->mtx);
2445
2446                 STAILQ_FOREACH_SAFE(m, &local_m_head, m_epg_stailq, n) {
2447                         if (m->m_epg_flags & EPG_FLAG_2FREE) {
2448                                 ktls_free(m->m_epg_tls);
2449                                 m_free_raw(m);
2450                         } else {
2451                                 if (m->m_epg_tls->sync_dispatch)
2452                                         ktls_encrypt(wq, m);
2453                                 else
2454                                         ktls_encrypt_async(wq, m);
2455                                 counter_u64_add(ktls_cnt_tx_queued, -1);
2456                         }
2457                 }
2458
2459                 STAILQ_FOREACH_SAFE(so, &local_so_head, so_ktls_rx_list, son) {
2460                         ktls_decrypt(so);
2461                         counter_u64_add(ktls_cnt_rx_queued, -1);
2462                 }
2463         }
2464 }
2465
2466 #if defined(INET) || defined(INET6)
2467 static void
2468 ktls_disable_ifnet_help(void *context, int pending __unused)
2469 {
2470         struct ktls_session *tls;
2471         struct inpcb *inp;
2472         struct tcpcb *tp;
2473         struct socket *so;
2474         int err;
2475
2476         tls = context;
2477         inp = tls->inp;
2478         if (inp == NULL)
2479                 return;
2480         INP_WLOCK(inp);
2481         so = inp->inp_socket;
2482         MPASS(so != NULL);
2483         if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) ||
2484             (inp->inp_flags2 & INP_FREED)) {
2485                 goto out;
2486         }
2487
2488         if (so->so_snd.sb_tls_info != NULL)
2489                 err = ktls_set_tx_mode(so, TCP_TLS_MODE_SW);
2490         else
2491                 err = ENXIO;
2492         if (err == 0) {
2493                 counter_u64_add(ktls_ifnet_disable_ok, 1);
2494                 /* ktls_set_tx_mode() drops inp wlock, so recheck flags */
2495                 if ((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0 &&
2496                     (inp->inp_flags2 & INP_FREED) == 0 &&
2497                     (tp = intotcpcb(inp)) != NULL &&
2498                     tp->t_fb->tfb_hwtls_change != NULL)
2499                         (*tp->t_fb->tfb_hwtls_change)(tp, 0);
2500         } else {
2501                 counter_u64_add(ktls_ifnet_disable_fail, 1);
2502         }
2503
2504 out:
2505         SOCK_LOCK(so);
2506         sorele(so);
2507         if (!in_pcbrele_wlocked(inp))
2508                 INP_WUNLOCK(inp);
2509         ktls_free(tls);
2510 }
2511
2512 /*
2513  * Called when re-transmits are becoming a substantial portion of the
2514  * sends on this connection.  When this happens, we transition the
2515  * connection to software TLS.  This is needed because most inline TLS
2516  * NICs keep crypto state only for in-order transmits.  This means
2517  * that to handle a TCP rexmit (which is out-of-order), the NIC must
2518  * re-DMA the entire TLS record up to and including the current
2519  * segment.  This means that when re-transmitting the last ~1448 byte
2520  * segment of a 16KB TLS record, we could wind up re-DMA'ing an order
2521  * of magnitude more data than we are sending.  This can cause the
2522  * PCIe link to saturate well before the network, which can cause
2523  * output drops, and a general loss of capacity.
2524  */
2525 void
2526 ktls_disable_ifnet(void *arg)
2527 {
2528         struct tcpcb *tp;
2529         struct inpcb *inp;
2530         struct socket *so;
2531         struct ktls_session *tls;
2532
2533         tp = arg;
2534         inp = tp->t_inpcb;
2535         INP_WLOCK_ASSERT(inp);
2536         so = inp->inp_socket;
2537         SOCK_LOCK(so);
2538         tls = so->so_snd.sb_tls_info;
2539         if (tls->disable_ifnet_pending) {
2540                 SOCK_UNLOCK(so);
2541                 return;
2542         }
2543
2544         /*
2545          * note that disable_ifnet_pending is never cleared; disabling
2546          * ifnet can only be done once per session, so we never want
2547          * to do it again
2548          */
2549
2550         (void)ktls_hold(tls);
2551         in_pcbref(inp);
2552         soref(so);
2553         tls->disable_ifnet_pending = true;
2554         tls->inp = inp;
2555         SOCK_UNLOCK(so);
2556         TASK_INIT(&tls->disable_ifnet_task, 0, ktls_disable_ifnet_help, tls);
2557         (void)taskqueue_enqueue(taskqueue_thread, &tls->disable_ifnet_task);
2558 }
2559 #endif