]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/ktls.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb, and openmp
[FreeBSD/FreeBSD.git] / sys / sys / ktls.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 REGENTS 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  * $FreeBSD$
28  */
29 #ifndef _SYS_KTLS_H_
30 #define _SYS_KTLS_H_
31
32 #include <sys/refcount.h>
33 #include <sys/_task.h>
34
35 struct tls_record_layer {
36         uint8_t  tls_type;
37         uint8_t  tls_vmajor;
38         uint8_t  tls_vminor;
39         uint16_t tls_length;
40         uint8_t  tls_data[0];
41 } __attribute__ ((packed));
42
43 #define TLS_MAX_MSG_SIZE_V10_2  16384
44 #define TLS_MAX_PARAM_SIZE      1024    /* Max key/mac/iv in sockopt */
45 #define TLS_AEAD_GCM_LEN        4
46 #define TLS_CBC_IMPLICIT_IV_LEN 16
47
48 /* Type values for the record layer */
49 #define TLS_RLTYPE_APP          23
50
51 /*
52  * Nonce for GCM.
53  */
54 struct tls_nonce_data {
55         uint8_t fixed[TLS_AEAD_GCM_LEN];
56         uint64_t seq;
57 } __packed; 
58
59 /*
60  * AEAD additional data format per RFC.
61  */
62 struct tls_aead_data {
63         uint64_t seq;   /* In network order */
64         uint8_t type;
65         uint8_t tls_vmajor;
66         uint8_t tls_vminor;
67         uint16_t tls_length;    
68 } __packed;
69
70 /*
71  * Stream Cipher MAC additional data input.  This does not match the
72  * exact data on the wire (the sequence number is not placed on the
73  * wire, and any explicit IV after the record header is not covered by
74  * the MAC).
75  */
76 struct tls_mac_data {
77         uint64_t seq;
78         uint8_t type;
79         uint8_t tls_vmajor;
80         uint8_t tls_vminor;
81         uint16_t tls_length;    
82 } __packed;
83
84 #define TLS_MAJOR_VER_ONE       3
85 #define TLS_MINOR_VER_ZERO      1       /* 3, 1 */
86 #define TLS_MINOR_VER_ONE       2       /* 3, 2 */
87 #define TLS_MINOR_VER_TWO       3       /* 3, 3 */
88
89 /* For TCP_TXTLS_ENABLE */
90 struct tls_enable {
91         const uint8_t *cipher_key;
92         const uint8_t *iv;              /* Implicit IV. */
93         const uint8_t *auth_key;
94         int     cipher_algorithm;       /* e.g. CRYPTO_AES_CBC */
95         int     cipher_key_len;
96         int     iv_len;
97         int     auth_algorithm;         /* e.g. CRYPTO_SHA2_256_HMAC */
98         int     auth_key_len;
99         int     flags;
100         uint8_t tls_vmajor;
101         uint8_t tls_vminor;
102 };
103
104 struct tls_session_params {
105         uint8_t *cipher_key;
106         uint8_t *auth_key;
107         uint8_t iv[TLS_CBC_IMPLICIT_IV_LEN];
108         int     cipher_algorithm;
109         int     auth_algorithm;
110         uint16_t cipher_key_len;
111         uint16_t iv_len;
112         uint16_t auth_key_len;
113         uint16_t max_frame_len;
114         uint8_t tls_vmajor;
115         uint8_t tls_vminor;
116         uint8_t tls_hlen;
117         uint8_t tls_tlen;
118         uint8_t tls_bs;
119         uint8_t flags;
120 };
121
122 #ifdef _KERNEL
123
124 #define KTLS_API_VERSION 5
125
126 struct iovec;
127 struct ktls_session;
128 struct m_snd_tag;
129 struct mbuf;
130 struct mbuf_ext_pgs;
131 struct sockbuf;
132 struct socket;
133
134 struct ktls_crypto_backend {
135         LIST_ENTRY(ktls_crypto_backend) next;
136         int (*try)(struct socket *so, struct ktls_session *tls);
137         int prio;
138         int api_version;
139         int use_count;
140         const char *name;
141 };
142
143 struct ktls_session {
144         int     (*sw_encrypt)(struct ktls_session *tls,
145             const struct tls_record_layer *hdr, uint8_t *trailer,
146             struct iovec *src, struct iovec *dst, int iovcnt,
147             uint64_t seqno);
148         union {
149                 void *cipher;
150                 struct m_snd_tag *snd_tag;
151         };
152         struct ktls_crypto_backend *be;
153         void (*free)(struct ktls_session *tls);
154         struct tls_session_params params;
155         u_int   wq_index;
156         volatile u_int refcount;
157
158         struct task reset_tag_task;
159         struct inpcb *inp;
160         bool reset_pending;
161 } __aligned(CACHE_LINE_SIZE);
162
163 int ktls_crypto_backend_register(struct ktls_crypto_backend *be);
164 int ktls_crypto_backend_deregister(struct ktls_crypto_backend *be);
165 int ktls_enable_tx(struct socket *so, struct tls_enable *en);
166 void ktls_destroy(struct ktls_session *tls);
167 int ktls_frame(struct mbuf *m, struct ktls_session *tls, int *enqueue_cnt,
168     uint8_t record_type);
169 void ktls_seq(struct sockbuf *sb, struct mbuf *m);
170 void ktls_enqueue(struct mbuf *m, struct socket *so, int page_count);
171 void ktls_enqueue_to_free(struct mbuf_ext_pgs *pgs);
172 int ktls_set_tx_mode(struct socket *so, int mode);
173 int ktls_get_tx_mode(struct socket *so);
174 int ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls);
175
176 static inline struct ktls_session *
177 ktls_hold(struct ktls_session *tls)
178 {
179
180         if (tls != NULL)
181                 refcount_acquire(&tls->refcount);
182         return (tls);
183 }
184
185 static inline void
186 ktls_free(struct ktls_session *tls)
187 {
188
189         if (refcount_release(&tls->refcount))
190                 ktls_destroy(tls);
191 }
192
193 #endif /* !_KERNEL */
194 #endif /* !_SYS_KTLS_H_ */