diff options
author | John Baldwin <jhb@FreeBSD.org> | 2019-08-27 00:01:56 +0000 |
---|---|---|
committer | John Baldwin <jhb@FreeBSD.org> | 2019-08-27 00:01:56 +0000 |
commit | b2e60773c6b015f06fcd71510cd20a91eb43bcaa (patch) | |
tree | 7e88792669a12b900d38f75be531ec5f19459f8b /sys/kern/kern_sendfile.c | |
parent | a70e17eecad468b652f0e201bf47c4dd65cddfc5 (diff) |
Add kernel-side support for in-kernel TLS.
KTLS adds support for in-kernel framing and encryption of Transport
Layer Security (1.0-1.2) data on TCP sockets. KTLS only supports
offload of TLS for transmitted data. Key negotation must still be
performed in userland. Once completed, transmit session keys for a
connection are provided to the kernel via a new TCP_TXTLS_ENABLE
socket option. All subsequent data transmitted on the socket is
placed into TLS frames and encrypted using the supplied keys.
Any data written to a KTLS-enabled socket via write(2), aio_write(2),
or sendfile(2) is assumed to be application data and is encoded in TLS
frames with an application data type. Individual records can be sent
with a custom type (e.g. handshake messages) via sendmsg(2) with a new
control message (TLS_SET_RECORD_TYPE) specifying the record type.
At present, rekeying is not supported though the in-kernel framework
should support rekeying.
KTLS makes use of the recently added unmapped mbufs to store TLS
frames in the socket buffer. Each TLS frame is described by a single
ext_pgs mbuf. The ext_pgs structure contains the header of the TLS
record (and trailer for encrypted records) as well as references to
the associated TLS session.
KTLS supports two primary methods of encrypting TLS frames: software
TLS and ifnet TLS.
Software TLS marks mbufs holding socket data as not ready via
M_NOTREADY similar to sendfile(2) when TLS framing information is
added to an unmapped mbuf in ktls_frame(). ktls_enqueue() is then
called to schedule TLS frames for encryption. In the case of
sendfile_iodone() calls ktls_enqueue() instead of pru_ready() leaving
the mbufs marked M_NOTREADY until encryption is completed. For other
writes (vn_sendfile when pages are available, write(2), etc.), the
PRUS_NOTREADY is set when invoking pru_send() along with invoking
ktls_enqueue().
A pool of worker threads (the "KTLS" kernel process) encrypts TLS
frames queued via ktls_enqueue(). Each TLS frame is temporarily
mapped using the direct map and passed to a software encryption
backend to perform the actual encryption.
(Note: The use of PHYS_TO_DMAP could be replaced with sf_bufs if
someone wished to make this work on architectures without a direct
map.)
KTLS supports pluggable software encryption backends. Internally,
Netflix uses proprietary pure-software backends. This commit includes
a simple backend in a new ktls_ocf.ko module that uses the kernel's
OpenCrypto framework to provide AES-GCM encryption of TLS frames. As
a result, software TLS is now a bit of a misnomer as it can make use
of hardware crypto accelerators.
Once software encryption has finished, the TLS frame mbufs are marked
ready via pru_ready(). At this point, the encrypted data appears as
regular payload to the TCP stack stored in unmapped mbufs.
ifnet TLS permits a NIC to offload the TLS encryption and TCP
segmentation. In this mode, a new send tag type (IF_SND_TAG_TYPE_TLS)
is allocated on the interface a socket is routed over and associated
with a TLS session. TLS records for a TLS session using ifnet TLS are
not marked M_NOTREADY but are passed down the stack unencrypted. The
ip_output_send() and ip6_output_send() helper functions that apply
send tags to outbound IP packets verify that the send tag of the TLS
record matches the outbound interface. If so, the packet is tagged
with the TLS send tag and sent to the interface. The NIC device
driver must recognize packets with the TLS send tag and schedule them
for TLS encryption and TCP segmentation. If the the outbound
interface does not match the interface in the TLS send tag, the packet
is dropped. In addition, a task is scheduled to refresh the TLS send
tag for the TLS session. If a new TLS send tag cannot be allocated,
the connection is dropped. If a new TLS send tag is allocated,
however, subsequent packets will be tagged with the correct TLS send
tag. (This latter case has been tested by configuring both ports of a
Chelsio T6 in a lagg and failing over from one port to another. As
the connections migrated to the new port, new TLS send tags were
allocated for the new port and connections resumed without being
dropped.)
ifnet TLS can be enabled and disabled on supported network interfaces
via new '[-]txtls[46]' options to ifconfig(8). ifnet TLS is supported
across both vlan devices and lagg interfaces using failover, lacp with
flowid enabled, or lacp with flowid enabled.
Applications may request the current KTLS mode of a connection via a
new TCP_TXTLS_MODE socket option. They can also use this socket
option to toggle between software and ifnet TLS modes.
In addition, a testing tool is available in tools/tools/switch_tls.
This is modeled on tcpdrop and uses similar syntax. However, instead
of dropping connections, -s is used to force KTLS connections to
switch to software TLS and -i is used to switch to ifnet TLS.
Various sysctls and counters are available under the kern.ipc.tls
sysctl node. The kern.ipc.tls.enable node must be set to true to
enable KTLS (it is off by default). The use of unmapped mbufs must
also be enabled via kern.ipc.mb_use_ext_pgs to enable KTLS.
KTLS is enabled via the KERN_TLS kernel option.
This patch is the culmination of years of work by several folks
including Scott Long and Randall Stewart for the original design and
implementation; Drew Gallatin for several optimizations including the
use of ext_pgs mbufs, the M_NOTREADY mechanism for TLS records
awaiting software encryption, and pluggable software crypto backends;
and John Baldwin for modifications to support hardware TLS offload.
Reviewed by: gallatin, hselasky, rrs
Obtained from: Netflix
Sponsored by: Netflix, Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D21277
Notes
Notes:
svn path=/head/; revision=351522
Diffstat (limited to 'sys/kern/kern_sendfile.c')
-rw-r--r-- | sys/kern/kern_sendfile.c | 101 |
1 files changed, 95 insertions, 6 deletions
diff --git a/sys/kern/kern_sendfile.c b/sys/kern/kern_sendfile.c index 9674e74fcd37..5c8dfa7e5b41 100644 --- a/sys/kern/kern_sendfile.c +++ b/sys/kern/kern_sendfile.c @@ -30,12 +30,15 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include "opt_kern_tls.h" + #include <sys/param.h> #include <sys/systm.h> #include <sys/capsicum.h> #include <sys/kernel.h> #include <netinet/in.h> #include <sys/lock.h> +#include <sys/ktls.h> #include <sys/mutex.h> #include <sys/sysproto.h> #include <sys/malloc.h> @@ -85,6 +88,7 @@ struct sf_io { int npages; struct socket *so; struct mbuf *m; + struct ktls_session *tls; vm_page_t pa[]; }; @@ -262,6 +266,15 @@ sendfile_iodone(void *arg, vm_page_t *pg, int count, int error) if (!refcount_release(&sfio->nios)) return; +#ifdef INVARIANTS + if ((sfio->m->m_flags & M_EXT) != 0 && + sfio->m->m_ext.ext_type == EXT_PGS) + KASSERT(sfio->tls == sfio->m->m_ext.ext_pgs->tls, + ("TLS session mismatch")); + else + KASSERT(sfio->tls == NULL, + ("non-ext_pgs mbuf with TLS session")); +#endif CURVNET_SET(so->so_vnet); if (sfio->error) { /* @@ -279,12 +292,29 @@ sendfile_iodone(void *arg, vm_page_t *pg, int count, int error) so->so_error = EIO; mb_free_notready(sfio->m, sfio->npages); +#ifdef KERN_TLS + } else if (sfio->tls != NULL && sfio->tls->sw_encrypt != NULL) { + /* + * I/O operation is complete, but we still need to + * encrypt. We cannot do this in the interrupt thread + * of the disk controller, so forward the mbufs to a + * different thread. + * + * Donate the socket reference from sfio to rather + * than explicitly invoking soref(). + */ + ktls_enqueue(sfio->m, so, sfio->npages); + goto out_with_ref; +#endif } else (void)(so->so_proto->pr_usrreqs->pru_ready)(so, sfio->m, sfio->npages); SOCK_LOCK(so); sorele(so); +#ifdef KERN_TLS +out_with_ref: +#endif CURVNET_RESTORE(); free(sfio, M_TEMP); } @@ -526,6 +556,9 @@ vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, struct vnode *vp; struct vm_object *obj; struct socket *so; +#ifdef KERN_TLS + struct ktls_session *tls; +#endif struct mbuf_ext_pgs *ext_pgs; struct mbuf *m, *mh, *mhtail; struct sf_buf *sf; @@ -534,12 +567,18 @@ vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, struct vattr va; off_t off, sbytes, rem, obj_size; int bsize, error, ext_pgs_idx, hdrlen, max_pgs, softerr; +#ifdef KERN_TLS + int tls_enq_cnt; +#endif bool use_ext_pgs; obj = NULL; so = NULL; m = mh = NULL; sfs = NULL; +#ifdef KERN_TLS + tls = NULL; +#endif hdrlen = sbytes = 0; softerr = 0; use_ext_pgs = false; @@ -576,6 +615,9 @@ vn_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, * we implement that, but possibly shouldn't. */ (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR); +#ifdef KERN_TLS + tls = ktls_hold(so->so_snd.sb_tls_info); +#endif /* * Loop through the pages of the file, starting with the requested @@ -669,7 +711,14 @@ retry_space: if (hdr_uio != NULL && hdr_uio->uio_resid > 0) { hdr_uio->uio_td = td; hdr_uio->uio_rw = UIO_WRITE; - mh = m_uiotombuf(hdr_uio, M_WAITOK, space, 0, 0); +#ifdef KERN_TLS + if (tls != NULL) + mh = m_uiotombuf(hdr_uio, M_WAITOK, space, + tls->params.max_frame_len, M_NOMAP); + else +#endif + mh = m_uiotombuf(hdr_uio, M_WAITOK, + space, 0, 0); hdrlen = m_length(mh, &mhtail); space -= hdrlen; /* @@ -743,6 +792,15 @@ retry_space: sfio->so = so; sfio->error = 0; +#ifdef KERN_TLS + /* + * This doesn't use ktls_hold() because sfio->m will + * also have a reference on 'tls' that will be valid + * for all of sfio's lifetime. + */ + sfio->tls = tls; +#endif + error = sendfile_swapin(obj, sfio, &nios, off, space, npages, rhpages, flags); if (error != 0) { @@ -763,11 +821,22 @@ retry_space: * bufs are restricted to TCP as that is what has been * tested. In particular, unmapped mbufs have not * been tested with UNIX-domain sockets. + * + * TLS frames always require unmapped mbufs. */ - if (mb_use_ext_pgs && - so->so_proto->pr_protocol == IPPROTO_TCP) { + if ((mb_use_ext_pgs && + so->so_proto->pr_protocol == IPPROTO_TCP) +#ifdef KERN_TLS + || tls != NULL +#endif + ) { use_ext_pgs = true; - max_pgs = MBUF_PEXT_MAX_PGS; +#ifdef KERN_TLS + if (tls != NULL) + max_pgs = num_pages(tls->params.max_frame_len); + else +#endif + max_pgs = MBUF_PEXT_MAX_PGS; /* Start at last index, to wrap on first use. */ ext_pgs_idx = max_pgs - 1; @@ -946,6 +1015,14 @@ prepend_header: __func__, m_length(m, NULL), space, hdrlen)); CURVNET_SET(so->so_vnet); +#ifdef KERN_TLS + if (tls != NULL) { + error = ktls_frame(m, tls, &tls_enq_cnt, + TLS_RLTYPE_APP); + if (error != 0) + goto done; + } +#endif if (nios == 0) { /* * If sendfile_swapin() didn't initiate any I/Os, @@ -954,8 +1031,16 @@ prepend_header: * PRUS_NOTREADY flag. */ free(sfio, M_TEMP); - error = (*so->so_proto->pr_usrreqs->pru_send) - (so, 0, m, NULL, NULL, td); +#ifdef KERN_TLS + if (tls != NULL && tls->sw_encrypt != NULL) { + error = (*so->so_proto->pr_usrreqs->pru_send) + (so, PRUS_NOTREADY, m, NULL, NULL, td); + soref(so); + ktls_enqueue(m, so, tls_enq_cnt); + } else +#endif + error = (*so->so_proto->pr_usrreqs->pru_send) + (so, 0, m, NULL, NULL, td); } else { sfio->npages = npages; soref(so); @@ -1019,6 +1104,10 @@ out: mtx_destroy(&sfs->mtx); free(sfs, M_TEMP); } +#ifdef KERN_TLS + if (tls != NULL) + ktls_free(tls); +#endif if (error == ERESTART) error = EINTR; |