xref: /openbmc/linux/include/net/tls.h (revision fb0f886f)
1 /*
2  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #ifndef _TLS_OFFLOAD_H
35 #define _TLS_OFFLOAD_H
36 
37 #include <linux/types.h>
38 #include <asm/byteorder.h>
39 #include <linux/crypto.h>
40 #include <linux/socket.h>
41 #include <linux/tcp.h>
42 #include <linux/skmsg.h>
43 
44 #include <net/tcp.h>
45 #include <net/strparser.h>
46 #include <crypto/aead.h>
47 #include <uapi/linux/tls.h>
48 
49 
50 /* Maximum data size carried in a TLS record */
51 #define TLS_MAX_PAYLOAD_SIZE		((size_t)1 << 14)
52 
53 #define TLS_HEADER_SIZE			5
54 #define TLS_NONCE_OFFSET		TLS_HEADER_SIZE
55 
56 #define TLS_CRYPTO_INFO_READY(info)	((info)->cipher_type)
57 
58 #define TLS_RECORD_TYPE_DATA		0x17
59 
60 #define TLS_AAD_SPACE_SIZE		13
61 #define TLS_DEVICE_NAME_MAX		32
62 
63 #define MAX_IV_SIZE			16
64 
65 /* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes.
66  *
67  * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
68  *
69  * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
70  * Hence b0 contains (3 - 1) = 2.
71  */
72 #define TLS_AES_CCM_IV_B0_BYTE		2
73 
74 /*
75  * This structure defines the routines for Inline TLS driver.
76  * The following routines are optional and filled with a
77  * null pointer if not defined.
78  *
79  * @name: Its the name of registered Inline tls device
80  * @dev_list: Inline tls device list
81  * int (*feature)(struct tls_device *device);
82  *     Called to return Inline TLS driver capability
83  *
84  * int (*hash)(struct tls_device *device, struct sock *sk);
85  *     This function sets Inline driver for listen and program
86  *     device specific functioanlity as required
87  *
88  * void (*unhash)(struct tls_device *device, struct sock *sk);
89  *     This function cleans listen state set by Inline TLS driver
90  *
91  * void (*release)(struct kref *kref);
92  *     Release the registered device and allocated resources
93  * @kref: Number of reference to tls_device
94  */
95 struct tls_device {
96 	char name[TLS_DEVICE_NAME_MAX];
97 	struct list_head dev_list;
98 	int  (*feature)(struct tls_device *device);
99 	int  (*hash)(struct tls_device *device, struct sock *sk);
100 	void (*unhash)(struct tls_device *device, struct sock *sk);
101 	void (*release)(struct kref *kref);
102 	struct kref kref;
103 };
104 
105 enum {
106 	TLS_BASE,
107 	TLS_SW,
108 #ifdef CONFIG_TLS_DEVICE
109 	TLS_HW,
110 #endif
111 	TLS_HW_RECORD,
112 	TLS_NUM_CONFIG,
113 };
114 
115 /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
116  * allocated or mapped for each TLS record. After encryption, the records are
117  * stores in a linked list.
118  */
119 struct tls_rec {
120 	struct list_head list;
121 	int tx_ready;
122 	int tx_flags;
123 	int inplace_crypto;
124 
125 	struct sk_msg msg_plaintext;
126 	struct sk_msg msg_encrypted;
127 
128 	/* AAD | msg_plaintext.sg.data | sg_tag */
129 	struct scatterlist sg_aead_in[2];
130 	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
131 	struct scatterlist sg_aead_out[2];
132 
133 	char content_type;
134 	struct scatterlist sg_content_type;
135 
136 	char aad_space[TLS_AAD_SPACE_SIZE];
137 	u8 iv_data[MAX_IV_SIZE];
138 	struct aead_request aead_req;
139 	u8 aead_req_ctx[];
140 };
141 
142 struct tls_msg {
143 	struct strp_msg rxm;
144 	u8 control;
145 };
146 
147 struct tx_work {
148 	struct delayed_work work;
149 	struct sock *sk;
150 };
151 
152 struct tls_sw_context_tx {
153 	struct crypto_aead *aead_send;
154 	struct crypto_wait async_wait;
155 	struct tx_work tx_work;
156 	struct tls_rec *open_rec;
157 	struct list_head tx_list;
158 	atomic_t encrypt_pending;
159 	int async_notify;
160 	int async_capable;
161 
162 #define BIT_TX_SCHEDULED	0
163 	unsigned long tx_bitmask;
164 };
165 
166 struct tls_sw_context_rx {
167 	struct crypto_aead *aead_recv;
168 	struct crypto_wait async_wait;
169 	struct strparser strp;
170 	struct sk_buff_head rx_list;	/* list of decrypted 'data' records */
171 	void (*saved_data_ready)(struct sock *sk);
172 
173 	struct sk_buff *recv_pkt;
174 	u8 control;
175 	int async_capable;
176 	bool decrypted;
177 	atomic_t decrypt_pending;
178 	bool async_notify;
179 };
180 
181 struct tls_record_info {
182 	struct list_head list;
183 	u32 end_seq;
184 	int len;
185 	int num_frags;
186 	skb_frag_t frags[MAX_SKB_FRAGS];
187 };
188 
189 struct tls_offload_context_tx {
190 	struct crypto_aead *aead_send;
191 	spinlock_t lock;	/* protects records list */
192 	struct list_head records_list;
193 	struct tls_record_info *open_record;
194 	struct tls_record_info *retransmit_hint;
195 	u64 hint_record_sn;
196 	u64 unacked_record_sn;
197 
198 	struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
199 	void (*sk_destruct)(struct sock *sk);
200 	u8 driver_state[];
201 	/* The TLS layer reserves room for driver specific state
202 	 * Currently the belief is that there is not enough
203 	 * driver specific state to justify another layer of indirection
204 	 */
205 #define TLS_DRIVER_STATE_SIZE (max_t(size_t, 8, sizeof(void *)))
206 };
207 
208 #define TLS_OFFLOAD_CONTEXT_SIZE_TX                                            \
209 	(ALIGN(sizeof(struct tls_offload_context_tx), sizeof(void *)) +        \
210 	 TLS_DRIVER_STATE_SIZE)
211 
212 struct cipher_context {
213 	char *iv;
214 	char *rec_seq;
215 };
216 
217 union tls_crypto_context {
218 	struct tls_crypto_info info;
219 	union {
220 		struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
221 		struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
222 	};
223 };
224 
225 struct tls_prot_info {
226 	u16 version;
227 	u16 cipher_type;
228 	u16 prepend_size;
229 	u16 tag_size;
230 	u16 overhead_size;
231 	u16 iv_size;
232 	u16 salt_size;
233 	u16 rec_seq_size;
234 	u16 aad_size;
235 	u16 tail_size;
236 };
237 
238 struct tls_context {
239 	/* read-only cache line */
240 	struct tls_prot_info prot_info;
241 
242 	u8 tx_conf:3;
243 	u8 rx_conf:3;
244 
245 	int (*push_pending_record)(struct sock *sk, int flags);
246 	void (*sk_write_space)(struct sock *sk);
247 
248 	void *priv_ctx_tx;
249 	void *priv_ctx_rx;
250 
251 	struct net_device *netdev;
252 
253 	/* rw cache line */
254 	struct cipher_context tx;
255 	struct cipher_context rx;
256 
257 	struct scatterlist *partially_sent_record;
258 	u16 partially_sent_offset;
259 
260 	bool in_tcp_sendpages;
261 	bool pending_open_record_frags;
262 	unsigned long flags;
263 
264 	/* cache cold stuff */
265 	void (*sk_destruct)(struct sock *sk);
266 	void (*sk_proto_close)(struct sock *sk, long timeout);
267 
268 	int  (*setsockopt)(struct sock *sk, int level,
269 			   int optname, char __user *optval,
270 			   unsigned int optlen);
271 	int  (*getsockopt)(struct sock *sk, int level,
272 			   int optname, char __user *optval,
273 			   int __user *optlen);
274 	int  (*hash)(struct sock *sk);
275 	void (*unhash)(struct sock *sk);
276 
277 	union tls_crypto_context crypto_send;
278 	union tls_crypto_context crypto_recv;
279 
280 	struct list_head list;
281 	refcount_t refcount;
282 };
283 
284 enum tls_offload_ctx_dir {
285 	TLS_OFFLOAD_CTX_DIR_RX,
286 	TLS_OFFLOAD_CTX_DIR_TX,
287 };
288 
289 struct tlsdev_ops {
290 	int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
291 			   enum tls_offload_ctx_dir direction,
292 			   struct tls_crypto_info *crypto_info,
293 			   u32 start_offload_tcp_sn);
294 	void (*tls_dev_del)(struct net_device *netdev,
295 			    struct tls_context *ctx,
296 			    enum tls_offload_ctx_dir direction);
297 	void (*tls_dev_resync_rx)(struct net_device *netdev,
298 				  struct sock *sk, u32 seq, u64 rcd_sn);
299 };
300 
301 struct tls_offload_context_rx {
302 	/* sw must be the first member of tls_offload_context_rx */
303 	struct tls_sw_context_rx sw;
304 	atomic64_t resync_req;
305 	u8 driver_state[];
306 	/* The TLS layer reserves room for driver specific state
307 	 * Currently the belief is that there is not enough
308 	 * driver specific state to justify another layer of indirection
309 	 */
310 };
311 
312 #define TLS_OFFLOAD_CONTEXT_SIZE_RX					\
313 	(ALIGN(sizeof(struct tls_offload_context_rx), sizeof(void *)) + \
314 	 TLS_DRIVER_STATE_SIZE)
315 
316 int wait_on_pending_writer(struct sock *sk, long *timeo);
317 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
318 		int __user *optlen);
319 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
320 		  unsigned int optlen);
321 
322 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
323 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
324 int tls_sw_sendpage(struct sock *sk, struct page *page,
325 		    int offset, size_t size, int flags);
326 void tls_sw_close(struct sock *sk, long timeout);
327 void tls_sw_free_resources_tx(struct sock *sk);
328 void tls_sw_free_resources_rx(struct sock *sk);
329 void tls_sw_release_resources_rx(struct sock *sk);
330 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
331 		   int nonblock, int flags, int *addr_len);
332 bool tls_sw_stream_read(const struct sock *sk);
333 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
334 			   struct pipe_inode_info *pipe,
335 			   size_t len, unsigned int flags);
336 
337 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
338 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
339 int tls_device_sendpage(struct sock *sk, struct page *page,
340 			int offset, size_t size, int flags);
341 void tls_device_free_resources_tx(struct sock *sk);
342 void tls_device_init(void);
343 void tls_device_cleanup(void);
344 int tls_tx_records(struct sock *sk, int flags);
345 
346 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
347 				       u32 seq, u64 *p_record_sn);
348 
349 static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
350 {
351 	return rec->len == 0;
352 }
353 
354 static inline u32 tls_record_start_seq(struct tls_record_info *rec)
355 {
356 	return rec->end_seq - rec->len;
357 }
358 
359 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
360 		struct scatterlist *sg, u16 first_offset,
361 		int flags);
362 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
363 			    int flags);
364 bool tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
365 
366 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
367 {
368 	return (struct tls_msg *)strp_msg(skb);
369 }
370 
371 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
372 {
373 	return !!ctx->partially_sent_record;
374 }
375 
376 static inline int tls_complete_pending_work(struct sock *sk,
377 					    struct tls_context *ctx,
378 					    int flags, long *timeo)
379 {
380 	int rc = 0;
381 
382 	if (unlikely(sk->sk_write_pending))
383 		rc = wait_on_pending_writer(sk, timeo);
384 
385 	if (!rc && tls_is_partially_sent_record(ctx))
386 		rc = tls_push_partial_record(sk, ctx, flags);
387 
388 	return rc;
389 }
390 
391 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
392 {
393 	return tls_ctx->pending_open_record_frags;
394 }
395 
396 static inline bool is_tx_ready(struct tls_sw_context_tx *ctx)
397 {
398 	struct tls_rec *rec;
399 
400 	rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
401 	if (!rec)
402 		return false;
403 
404 	return READ_ONCE(rec->tx_ready);
405 }
406 
407 struct sk_buff *
408 tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
409 		      struct sk_buff *skb);
410 
411 static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
412 {
413 #ifdef CONFIG_SOCK_VALIDATE_XMIT
414 	return sk_fullsock(sk) &&
415 	       (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
416 	       &tls_validate_xmit_skb);
417 #else
418 	return false;
419 #endif
420 }
421 
422 static inline void tls_err_abort(struct sock *sk, int err)
423 {
424 	sk->sk_err = err;
425 	sk->sk_error_report(sk);
426 }
427 
428 static inline bool tls_bigint_increment(unsigned char *seq, int len)
429 {
430 	int i;
431 
432 	for (i = len - 1; i >= 0; i--) {
433 		++seq[i];
434 		if (seq[i] != 0)
435 			break;
436 	}
437 
438 	return (i == -1);
439 }
440 
441 static inline struct tls_context *tls_get_ctx(const struct sock *sk)
442 {
443 	struct inet_connection_sock *icsk = inet_csk(sk);
444 
445 	return icsk->icsk_ulp_data;
446 }
447 
448 static inline void tls_advance_record_sn(struct sock *sk,
449 					 struct tls_prot_info *prot,
450 					 struct cipher_context *ctx)
451 {
452 	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
453 		tls_err_abort(sk, EBADMSG);
454 
455 	if (prot->version != TLS_1_3_VERSION)
456 		tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
457 				     prot->iv_size);
458 }
459 
460 static inline void tls_fill_prepend(struct tls_context *ctx,
461 			     char *buf,
462 			     size_t plaintext_len,
463 			     unsigned char record_type,
464 			     int version)
465 {
466 	struct tls_prot_info *prot = &ctx->prot_info;
467 	size_t pkt_len, iv_size = prot->iv_size;
468 
469 	pkt_len = plaintext_len + prot->tag_size;
470 	if (version != TLS_1_3_VERSION) {
471 		pkt_len += iv_size;
472 
473 		memcpy(buf + TLS_NONCE_OFFSET,
474 		       ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
475 	}
476 
477 	/* we cover nonce explicit here as well, so buf should be of
478 	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
479 	 */
480 	buf[0] = version == TLS_1_3_VERSION ?
481 		   TLS_RECORD_TYPE_DATA : record_type;
482 	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
483 	buf[1] = TLS_1_2_VERSION_MINOR;
484 	buf[2] = TLS_1_2_VERSION_MAJOR;
485 	/* we can use IV for nonce explicit according to spec */
486 	buf[3] = pkt_len >> 8;
487 	buf[4] = pkt_len & 0xFF;
488 }
489 
490 static inline void tls_make_aad(char *buf,
491 				size_t size,
492 				char *record_sequence,
493 				int record_sequence_size,
494 				unsigned char record_type,
495 				int version)
496 {
497 	if (version != TLS_1_3_VERSION) {
498 		memcpy(buf, record_sequence, record_sequence_size);
499 		buf += 8;
500 	} else {
501 		size += TLS_CIPHER_AES_GCM_128_TAG_SIZE;
502 	}
503 
504 	buf[0] = version == TLS_1_3_VERSION ?
505 		  TLS_RECORD_TYPE_DATA : record_type;
506 	buf[1] = TLS_1_2_VERSION_MAJOR;
507 	buf[2] = TLS_1_2_VERSION_MINOR;
508 	buf[3] = size >> 8;
509 	buf[4] = size & 0xFF;
510 }
511 
512 static inline void xor_iv_with_seq(int version, char *iv, char *seq)
513 {
514 	int i;
515 
516 	if (version == TLS_1_3_VERSION) {
517 		for (i = 0; i < 8; i++)
518 			iv[i + 4] ^= seq[i];
519 	}
520 }
521 
522 
523 static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
524 		const struct tls_context *tls_ctx)
525 {
526 	return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
527 }
528 
529 static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
530 		const struct tls_context *tls_ctx)
531 {
532 	return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
533 }
534 
535 static inline struct tls_offload_context_tx *
536 tls_offload_ctx_tx(const struct tls_context *tls_ctx)
537 {
538 	return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
539 }
540 
541 static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
542 {
543 	struct tls_context *ctx = tls_get_ctx(sk);
544 
545 	if (!ctx)
546 		return false;
547 	return !!tls_sw_ctx_tx(ctx);
548 }
549 
550 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
551 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
552 
553 static inline struct tls_offload_context_rx *
554 tls_offload_ctx_rx(const struct tls_context *tls_ctx)
555 {
556 	return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
557 }
558 
559 /* The TLS context is valid until sk_destruct is called */
560 static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
561 {
562 	struct tls_context *tls_ctx = tls_get_ctx(sk);
563 	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
564 
565 	atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | 1);
566 }
567 
568 
569 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
570 		      unsigned char *record_type);
571 void tls_register_device(struct tls_device *device);
572 void tls_unregister_device(struct tls_device *device);
573 int tls_device_decrypted(struct sock *sk, struct sk_buff *skb);
574 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
575 		struct scatterlist *sgout);
576 
577 struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
578 				      struct net_device *dev,
579 				      struct sk_buff *skb);
580 
581 int tls_sw_fallback_init(struct sock *sk,
582 			 struct tls_offload_context_tx *offload_ctx,
583 			 struct tls_crypto_info *crypto_info);
584 
585 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
586 
587 void tls_device_offload_cleanup_rx(struct sock *sk);
588 void handle_device_resync(struct sock *sk, u32 seq, u64 rcd_sn);
589 
590 #endif /* _TLS_OFFLOAD_H */
591