xref: /openbmc/linux/net/tls/tls.h (revision b755c25f)
1 /*
2  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
3  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef _TLS_INT_H
36 #define _TLS_INT_H
37 
38 #include <asm/byteorder.h>
39 #include <linux/types.h>
40 #include <linux/skmsg.h>
41 #include <net/tls.h>
42 
43 #define TLS_PAGE_ORDER	(min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER,	\
44 			       TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT))
45 
46 #define __TLS_INC_STATS(net, field)				\
47 	__SNMP_INC_STATS((net)->mib.tls_statistics, field)
48 #define TLS_INC_STATS(net, field)				\
49 	SNMP_INC_STATS((net)->mib.tls_statistics, field)
50 #define TLS_DEC_STATS(net, field)				\
51 	SNMP_DEC_STATS((net)->mib.tls_statistics, field)
52 
53 /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
54  * allocated or mapped for each TLS record. After encryption, the records are
55  * stores in a linked list.
56  */
57 struct tls_rec {
58 	struct list_head list;
59 	int tx_ready;
60 	int tx_flags;
61 
62 	struct sk_msg msg_plaintext;
63 	struct sk_msg msg_encrypted;
64 
65 	/* AAD | msg_plaintext.sg.data | sg_tag */
66 	struct scatterlist sg_aead_in[2];
67 	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
68 	struct scatterlist sg_aead_out[2];
69 
70 	char content_type;
71 	struct scatterlist sg_content_type;
72 
73 	struct sock *sk;
74 
75 	char aad_space[TLS_AAD_SPACE_SIZE];
76 	u8 iv_data[MAX_IV_SIZE];
77 	struct aead_request aead_req;
78 	u8 aead_req_ctx[];
79 };
80 
81 int __net_init tls_proc_init(struct net *net);
82 void __net_exit tls_proc_fini(struct net *net);
83 
84 struct tls_context *tls_ctx_create(struct sock *sk);
85 void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
86 void update_sk_prot(struct sock *sk, struct tls_context *ctx);
87 
88 int wait_on_pending_writer(struct sock *sk, long *timeo);
89 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
90 		 int __user *optlen);
91 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
92 		  unsigned int optlen);
93 void tls_err_abort(struct sock *sk, int err);
94 
95 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
96 void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
97 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
98 void tls_sw_strparser_done(struct tls_context *tls_ctx);
99 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
100 void tls_sw_splice_eof(struct socket *sock);
101 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
102 void tls_sw_release_resources_tx(struct sock *sk);
103 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
104 void tls_sw_free_resources_rx(struct sock *sk);
105 void tls_sw_release_resources_rx(struct sock *sk);
106 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
107 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
108 		   int flags, int *addr_len);
109 bool tls_sw_sock_is_readable(struct sock *sk);
110 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
111 			   struct pipe_inode_info *pipe,
112 			   size_t len, unsigned int flags);
113 
114 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
115 void tls_device_splice_eof(struct socket *sock);
116 int tls_tx_records(struct sock *sk, int flags);
117 
118 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
119 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
120 
121 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
122 		     unsigned char *record_type);
123 int decrypt_skb(struct sock *sk, struct scatterlist *sgout);
124 
125 int tls_sw_fallback_init(struct sock *sk,
126 			 struct tls_offload_context_tx *offload_ctx,
127 			 struct tls_crypto_info *crypto_info);
128 
129 int tls_strp_dev_init(void);
130 void tls_strp_dev_exit(void);
131 
132 void tls_strp_done(struct tls_strparser *strp);
133 void tls_strp_stop(struct tls_strparser *strp);
134 int tls_strp_init(struct tls_strparser *strp, struct sock *sk);
135 void tls_strp_data_ready(struct tls_strparser *strp);
136 
137 void tls_strp_check_rcv(struct tls_strparser *strp);
138 void tls_strp_msg_done(struct tls_strparser *strp);
139 
140 int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb);
141 void tls_rx_msg_ready(struct tls_strparser *strp);
142 
143 void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
144 int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
145 struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
146 int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst);
147 
148 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
149 {
150 	struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb;
151 
152 	return &scb->tls;
153 }
154 
155 static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx)
156 {
157 	DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len);
158 	return ctx->strp.anchor;
159 }
160 
161 static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
162 {
163 	return ctx->strp.msg_ready;
164 }
165 
166 static inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx)
167 {
168 	return ctx->strp.mixed_decrypted;
169 }
170 
171 #ifdef CONFIG_TLS_DEVICE
172 int tls_device_init(void);
173 void tls_device_cleanup(void);
174 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
175 void tls_device_free_resources_tx(struct sock *sk);
176 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
177 void tls_device_offload_cleanup_rx(struct sock *sk);
178 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
179 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx);
180 #else
181 static inline int tls_device_init(void) { return 0; }
182 static inline void tls_device_cleanup(void) {}
183 
184 static inline int
185 tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
186 {
187 	return -EOPNOTSUPP;
188 }
189 
190 static inline void tls_device_free_resources_tx(struct sock *sk) {}
191 
192 static inline int
193 tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
194 {
195 	return -EOPNOTSUPP;
196 }
197 
198 static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
199 static inline void
200 tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
201 
202 static inline int
203 tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
204 {
205 	return 0;
206 }
207 #endif
208 
209 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
210 		struct scatterlist *sg, u16 first_offset,
211 		int flags);
212 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
213 			    int flags);
214 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
215 
216 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
217 {
218 	return !!ctx->partially_sent_record;
219 }
220 
221 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
222 {
223 	return tls_ctx->pending_open_record_frags;
224 }
225 
226 static inline bool tls_bigint_increment(unsigned char *seq, int len)
227 {
228 	int i;
229 
230 	for (i = len - 1; i >= 0; i--) {
231 		++seq[i];
232 		if (seq[i] != 0)
233 			break;
234 	}
235 
236 	return (i == -1);
237 }
238 
239 static inline void tls_bigint_subtract(unsigned char *seq, int  n)
240 {
241 	u64 rcd_sn;
242 	__be64 *p;
243 
244 	BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
245 
246 	p = (__be64 *)seq;
247 	rcd_sn = be64_to_cpu(*p);
248 	*p = cpu_to_be64(rcd_sn - n);
249 }
250 
251 static inline void
252 tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot,
253 		      struct cipher_context *ctx)
254 {
255 	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
256 		tls_err_abort(sk, -EBADMSG);
257 
258 	if (prot->version != TLS_1_3_VERSION &&
259 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
260 		tls_bigint_increment(ctx->iv + prot->salt_size,
261 				     prot->iv_size);
262 }
263 
264 static inline void
265 tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
266 {
267 	int i;
268 
269 	if (prot->version == TLS_1_3_VERSION ||
270 	    prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
271 		for (i = 0; i < 8; i++)
272 			iv[i + 4] ^= seq[i];
273 	}
274 }
275 
276 static inline void
277 tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len,
278 		 unsigned char record_type)
279 {
280 	struct tls_prot_info *prot = &ctx->prot_info;
281 	size_t pkt_len, iv_size = prot->iv_size;
282 
283 	pkt_len = plaintext_len + prot->tag_size;
284 	if (prot->version != TLS_1_3_VERSION &&
285 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
286 		pkt_len += iv_size;
287 
288 		memcpy(buf + TLS_NONCE_OFFSET,
289 		       ctx->tx.iv + prot->salt_size, iv_size);
290 	}
291 
292 	/* we cover nonce explicit here as well, so buf should be of
293 	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
294 	 */
295 	buf[0] = prot->version == TLS_1_3_VERSION ?
296 		   TLS_RECORD_TYPE_DATA : record_type;
297 	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
298 	buf[1] = TLS_1_2_VERSION_MINOR;
299 	buf[2] = TLS_1_2_VERSION_MAJOR;
300 	/* we can use IV for nonce explicit according to spec */
301 	buf[3] = pkt_len >> 8;
302 	buf[4] = pkt_len & 0xFF;
303 }
304 
305 static inline
306 void tls_make_aad(char *buf, size_t size, char *record_sequence,
307 		  unsigned char record_type, struct tls_prot_info *prot)
308 {
309 	if (prot->version != TLS_1_3_VERSION) {
310 		memcpy(buf, record_sequence, prot->rec_seq_size);
311 		buf += 8;
312 	} else {
313 		size += prot->tag_size;
314 	}
315 
316 	buf[0] = prot->version == TLS_1_3_VERSION ?
317 		  TLS_RECORD_TYPE_DATA : record_type;
318 	buf[1] = TLS_1_2_VERSION_MAJOR;
319 	buf[2] = TLS_1_2_VERSION_MINOR;
320 	buf[3] = size >> 8;
321 	buf[4] = size & 0xFF;
322 }
323 
324 #endif
325