blob: 28eca6a3b61511c08f5b414ad7833f7a4d12339f [file] [log] [blame]
Dave Watson3c4d7552017-06-14 11:37:39 -07001/*
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>
Dmitry V. Levinb9f3eb42017-11-14 06:30:11 +030038#include <asm/byteorder.h>
Vakul Garga54667f2018-01-31 21:34:37 +053039#include <linux/crypto.h>
Dmitry V. Levinb9f3eb42017-11-14 06:30:11 +030040#include <linux/socket.h>
41#include <linux/tcp.h>
Daniel Borkmannd829e9c2018-10-13 02:45:59 +020042#include <linux/skmsg.h>
Jakub Kicinski2e361172019-06-05 14:11:39 -070043#include <linux/netdevice.h>
Daniel Borkmannd829e9c2018-10-13 02:45:59 +020044
Dmitry V. Levinb9f3eb42017-11-14 06:30:11 +030045#include <net/tcp.h>
Dave Watsonc46234e2018-03-22 10:10:35 -070046#include <net/strparser.h>
Vakul Garga42055e2018-09-21 09:46:13 +053047#include <crypto/aead.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070048#include <uapi/linux/tls.h>
49
50
51/* Maximum data size carried in a TLS record */
52#define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
53
54#define TLS_HEADER_SIZE 5
55#define TLS_NONCE_OFFSET TLS_HEADER_SIZE
56
57#define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type)
58
59#define TLS_RECORD_TYPE_DATA 0x17
60
61#define TLS_AAD_SPACE_SIZE 13
Atul Guptadd0bed12018-03-31 21:41:52 +053062#define TLS_DEVICE_NAME_MAX 32
63
Vakul Gargf295b3a2019-03-20 02:03:36 +000064#define MAX_IV_SIZE 16
Jakub Kicinski89fec472019-06-10 21:40:00 -070065#define TLS_MAX_REC_SEQ_SIZE 8
Vakul Gargf295b3a2019-03-20 02:03:36 +000066
67/* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes.
68 *
69 * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
70 *
71 * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
72 * Hence b0 contains (3 - 1) = 2.
73 */
74#define TLS_AES_CCM_IV_B0_BYTE 2
75
Atul Guptadd0bed12018-03-31 21:41:52 +053076/*
77 * This structure defines the routines for Inline TLS driver.
78 * The following routines are optional and filled with a
79 * null pointer if not defined.
80 *
81 * @name: Its the name of registered Inline tls device
82 * @dev_list: Inline tls device list
83 * int (*feature)(struct tls_device *device);
84 * Called to return Inline TLS driver capability
85 *
86 * int (*hash)(struct tls_device *device, struct sock *sk);
87 * This function sets Inline driver for listen and program
88 * device specific functioanlity as required
89 *
90 * void (*unhash)(struct tls_device *device, struct sock *sk);
91 * This function cleans listen state set by Inline TLS driver
Atul Guptadf9d4a12018-12-11 02:20:09 -080092 *
93 * void (*release)(struct kref *kref);
94 * Release the registered device and allocated resources
95 * @kref: Number of reference to tls_device
Atul Guptadd0bed12018-03-31 21:41:52 +053096 */
97struct tls_device {
98 char name[TLS_DEVICE_NAME_MAX];
99 struct list_head dev_list;
100 int (*feature)(struct tls_device *device);
101 int (*hash)(struct tls_device *device, struct sock *sk);
102 void (*unhash)(struct tls_device *device, struct sock *sk);
Atul Guptadf9d4a12018-12-11 02:20:09 -0800103 void (*release)(struct kref *kref);
104 struct kref kref;
Atul Guptadd0bed12018-03-31 21:41:52 +0530105};
Dave Watson3c4d7552017-06-14 11:37:39 -0700106
Boris Pismenny4799ac82018-07-13 14:33:43 +0300107enum {
108 TLS_BASE,
109 TLS_SW,
110#ifdef CONFIG_TLS_DEVICE
111 TLS_HW,
112#endif
113 TLS_HW_RECORD,
114 TLS_NUM_CONFIG,
115};
116
Vakul Garga42055e2018-09-21 09:46:13 +0530117/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
118 * allocated or mapped for each TLS record. After encryption, the records are
119 * stores in a linked list.
120 */
121struct tls_rec {
122 struct list_head list;
Vakul Garg9932a292018-09-24 15:35:56 +0530123 int tx_ready;
Vakul Garga42055e2018-09-21 09:46:13 +0530124 int tx_flags;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530125 int inplace_crypto;
Dave Watson3c4d7552017-06-14 11:37:39 -0700126
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200127 struct sk_msg msg_plaintext;
128 struct sk_msg msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530129
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200130 /* AAD | msg_plaintext.sg.data | sg_tag */
131 struct scatterlist sg_aead_in[2];
132 /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
133 struct scatterlist sg_aead_out[2];
Vakul Garga42055e2018-09-21 09:46:13 +0530134
Dave Watson130b3922019-01-30 21:58:31 +0000135 char content_type;
136 struct scatterlist sg_content_type;
137
Vakul Garga42055e2018-09-21 09:46:13 +0530138 char aad_space[TLS_AAD_SPACE_SIZE];
Vakul Gargf295b3a2019-03-20 02:03:36 +0000139 u8 iv_data[MAX_IV_SIZE];
Vakul Garga42055e2018-09-21 09:46:13 +0530140 struct aead_request aead_req;
141 u8 aead_req_ctx[];
142};
143
Vakul Garg2b794c42019-02-23 08:42:37 +0000144struct tls_msg {
145 struct strp_msg rxm;
146 u8 control;
147};
148
Vakul Garga42055e2018-09-21 09:46:13 +0530149struct tx_work {
150 struct delayed_work work;
151 struct sock *sk;
152};
153
154struct tls_sw_context_tx {
155 struct crypto_aead *aead_send;
156 struct crypto_wait async_wait;
157 struct tx_work tx_work;
158 struct tls_rec *open_rec;
Vakul Garg9932a292018-09-24 15:35:56 +0530159 struct list_head tx_list;
Vakul Garga42055e2018-09-21 09:46:13 +0530160 atomic_t encrypt_pending;
161 int async_notify;
Dave Watson5b053e12019-01-30 22:08:21 +0000162 int async_capable;
Vakul Garga42055e2018-09-21 09:46:13 +0530163
164#define BIT_TX_SCHEDULED 0
165 unsigned long tx_bitmask;
Dave Watson3c4d7552017-06-14 11:37:39 -0700166};
167
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300168struct tls_sw_context_rx {
169 struct crypto_aead *aead_recv;
170 struct crypto_wait async_wait;
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300171 struct strparser strp;
Vakul Garg692d7b52019-01-16 10:40:16 +0000172 struct sk_buff_head rx_list; /* list of decrypted 'data' records */
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300173 void (*saved_data_ready)(struct sock *sk);
John Fastabend924ad652018-10-13 02:46:00 +0200174
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300175 struct sk_buff *recv_pkt;
176 u8 control;
Vakul Garg692d7b52019-01-16 10:40:16 +0000177 int async_capable;
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300178 bool decrypted;
Vakul Garg94524d82018-08-29 15:26:55 +0530179 atomic_t decrypt_pending;
180 bool async_notify;
181};
182
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300183struct tls_record_info {
184 struct list_head list;
185 u32 end_seq;
186 int len;
187 int num_frags;
188 skb_frag_t frags[MAX_SKB_FRAGS];
189};
190
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300191struct tls_offload_context_tx {
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300192 struct crypto_aead *aead_send;
193 spinlock_t lock; /* protects records list */
194 struct list_head records_list;
195 struct tls_record_info *open_record;
196 struct tls_record_info *retransmit_hint;
197 u64 hint_record_sn;
198 u64 unacked_record_sn;
199
200 struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
201 void (*sk_destruct)(struct sock *sk);
Jakub Kicinski2e361172019-06-05 14:11:39 -0700202 u8 driver_state[] __aligned(8);
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300203 /* The TLS layer reserves room for driver specific state
204 * Currently the belief is that there is not enough
205 * driver specific state to justify another layer of indirection
206 */
Jakub Kicinski2d6b51c2019-06-05 14:11:38 -0700207#define TLS_DRIVER_STATE_SIZE_TX 16
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300208};
209
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300210#define TLS_OFFLOAD_CONTEXT_SIZE_TX \
Jakub Kicinski2e361172019-06-05 14:11:39 -0700211 (sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300212
Jakub Kicinskie52972c2019-06-04 12:00:12 -0700213enum tls_context_flags {
214 TLS_RX_SYNC_RUNNING = 0,
215};
216
Dave Watsondbe42552018-03-22 10:10:06 -0700217struct cipher_context {
Dave Watsondbe42552018-03-22 10:10:06 -0700218 char *iv;
Dave Watsondbe42552018-03-22 10:10:06 -0700219 char *rec_seq;
220};
221
Sabrina Dubroca86029d12018-09-12 17:44:42 +0200222union tls_crypto_context {
223 struct tls_crypto_info info;
Dave Watsonfb99bce2019-01-30 21:58:05 +0000224 union {
225 struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
226 struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
227 };
Sabrina Dubroca86029d12018-09-12 17:44:42 +0200228};
229
Vakul Garg4509de12019-02-14 07:11:35 +0000230struct tls_prot_info {
231 u16 version;
232 u16 cipher_type;
233 u16 prepend_size;
234 u16 tag_size;
235 u16 overhead_size;
236 u16 iv_size;
Vakul Gargf295b3a2019-03-20 02:03:36 +0000237 u16 salt_size;
Vakul Garg4509de12019-02-14 07:11:35 +0000238 u16 rec_seq_size;
239 u16 aad_size;
240 u16 tail_size;
241};
242
Dave Watson3c4d7552017-06-14 11:37:39 -0700243struct tls_context {
Jakub Kicinskif0aaa2c2019-06-03 15:17:04 -0700244 /* read-only cache line */
Vakul Garg4509de12019-02-14 07:11:35 +0000245 struct tls_prot_info prot_info;
246
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300247 u8 tx_conf:3;
248 u8 rx_conf:3;
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200249
Jakub Kicinskif0aaa2c2019-06-03 15:17:04 -0700250 int (*push_pending_record)(struct sock *sk, int flags);
251 void (*sk_write_space)(struct sock *sk);
252
253 void *priv_ctx_tx;
254 void *priv_ctx_rx;
255
256 struct net_device *netdev;
257
258 /* rw cache line */
Dave Watsondbe42552018-03-22 10:10:06 -0700259 struct cipher_context tx;
Dave Watsonc46234e2018-03-22 10:10:35 -0700260 struct cipher_context rx;
Dave Watson3c4d7552017-06-14 11:37:39 -0700261
262 struct scatterlist *partially_sent_record;
263 u16 partially_sent_offset;
Vakul Garga42055e2018-09-21 09:46:13 +0530264
Dave Watsonc212d2c2018-05-01 13:05:39 -0700265 bool in_tcp_sendpages;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200266 bool pending_open_record_frags;
Jakub Kicinskif0aaa2c2019-06-03 15:17:04 -0700267 unsigned long flags;
Dave Watson3c4d7552017-06-14 11:37:39 -0700268
Jakub Kicinskif0aaa2c2019-06-03 15:17:04 -0700269 /* cache cold stuff */
Boris Pismenny4799ac82018-07-13 14:33:43 +0300270 void (*sk_destruct)(struct sock *sk);
Dave Watson3c4d7552017-06-14 11:37:39 -0700271 void (*sk_proto_close)(struct sock *sk, long timeout);
272
273 int (*setsockopt)(struct sock *sk, int level,
274 int optname, char __user *optval,
275 unsigned int optlen);
276 int (*getsockopt)(struct sock *sk, int level,
277 int optname, char __user *optval,
278 int __user *optlen);
Atul Guptadd0bed12018-03-31 21:41:52 +0530279 int (*hash)(struct sock *sk);
280 void (*unhash)(struct sock *sk);
Jakub Kicinskif0aaa2c2019-06-03 15:17:04 -0700281
282 union tls_crypto_context crypto_send;
283 union tls_crypto_context crypto_recv;
284
285 struct list_head list;
286 refcount_t refcount;
Dave Watson3c4d7552017-06-14 11:37:39 -0700287};
288
Jakub Kicinskida68b4a2019-04-25 12:32:03 -0700289enum tls_offload_ctx_dir {
290 TLS_OFFLOAD_CTX_DIR_RX,
291 TLS_OFFLOAD_CTX_DIR_TX,
292};
293
294struct tlsdev_ops {
295 int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
296 enum tls_offload_ctx_dir direction,
297 struct tls_crypto_info *crypto_info,
298 u32 start_offload_tcp_sn);
299 void (*tls_dev_del)(struct net_device *netdev,
300 struct tls_context *ctx,
301 enum tls_offload_ctx_dir direction);
302 void (*tls_dev_resync_rx)(struct net_device *netdev,
Jakub Kicinski89fec472019-06-10 21:40:00 -0700303 struct sock *sk, u32 seq, u8 *rcd_sn);
Jakub Kicinskida68b4a2019-04-25 12:32:03 -0700304};
305
Jakub Kicinskif953d33b2019-06-10 21:40:02 -0700306enum tls_offload_sync_type {
307 TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
308 TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
309};
310
311#define TLS_DEVICE_RESYNC_NH_START_IVAL 2
312#define TLS_DEVICE_RESYNC_NH_MAX_IVAL 128
313
Boris Pismenny4799ac82018-07-13 14:33:43 +0300314struct tls_offload_context_rx {
315 /* sw must be the first member of tls_offload_context_rx */
316 struct tls_sw_context_rx sw;
Jakub Kicinskif953d33b2019-06-10 21:40:02 -0700317 enum tls_offload_sync_type resync_type;
318 /* this member is set regardless of resync_type, to avoid branches */
319 u8 resync_nh_reset:1;
320 /* CORE_NEXT_HINT-only member, but use the hole here */
321 u8 resync_nh_do_now:1;
322 union {
323 /* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ */
324 struct {
325 atomic64_t resync_req;
326 };
327 /* TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT */
328 struct {
329 u32 decrypted_failed;
330 u32 decrypted_tgt;
331 } resync_nh;
332 };
Jakub Kicinski2e361172019-06-05 14:11:39 -0700333 u8 driver_state[] __aligned(8);
Boris Pismenny4799ac82018-07-13 14:33:43 +0300334 /* The TLS layer reserves room for driver specific state
335 * Currently the belief is that there is not enough
336 * driver specific state to justify another layer of indirection
337 */
Jakub Kicinski2d6b51c2019-06-05 14:11:38 -0700338#define TLS_DRIVER_STATE_SIZE_RX 8
Boris Pismenny4799ac82018-07-13 14:33:43 +0300339};
340
341#define TLS_OFFLOAD_CONTEXT_SIZE_RX \
Jakub Kicinski2e361172019-06-05 14:11:39 -0700342 (sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
Boris Pismenny4799ac82018-07-13 14:33:43 +0300343
Dave Watson3c4d7552017-06-14 11:37:39 -0700344int wait_on_pending_writer(struct sock *sk, long *timeo);
345int tls_sk_query(struct sock *sk, int optname, char __user *optval,
346 int __user *optlen);
347int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
348 unsigned int optlen);
349
Dave Watsonc46234e2018-03-22 10:10:35 -0700350int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700351int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
352int tls_sw_sendpage(struct sock *sk, struct page *page,
353 int offset, size_t size, int flags);
354void tls_sw_close(struct sock *sk, long timeout);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300355void tls_sw_free_resources_tx(struct sock *sk);
356void tls_sw_free_resources_rx(struct sock *sk);
Boris Pismenny39f56e12018-07-13 14:33:41 +0300357void tls_sw_release_resources_rx(struct sock *sk);
Dave Watsonc46234e2018-03-22 10:10:35 -0700358int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
359 int nonblock, int flags, int *addr_len);
John Fastabend924ad652018-10-13 02:46:00 +0200360bool tls_sw_stream_read(const struct sock *sk);
Dave Watsonc46234e2018-03-22 10:10:35 -0700361ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
362 struct pipe_inode_info *pipe,
363 size_t len, unsigned int flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700364
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300365int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
366int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
367int tls_device_sendpage(struct sock *sk, struct page *page,
368 int offset, size_t size, int flags);
Jakub Kicinski35b71a342019-04-10 11:04:31 -0700369void tls_device_free_resources_tx(struct sock *sk);
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300370void tls_device_init(void);
371void tls_device_cleanup(void);
Vakul Garga42055e2018-09-21 09:46:13 +0530372int tls_tx_records(struct sock *sk, int flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700373
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300374struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300375 u32 seq, u64 *p_record_sn);
376
377static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
378{
379 return rec->len == 0;
380}
381
382static inline u32 tls_record_start_seq(struct tls_record_info *rec)
383{
384 return rec->end_seq - rec->len;
385}
386
Dave Watson3c4d7552017-06-14 11:37:39 -0700387int tls_push_sg(struct sock *sk, struct tls_context *ctx,
388 struct scatterlist *sg, u16 first_offset,
389 int flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530390int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
391 int flags);
Jakub Kicinski35b71a342019-04-10 11:04:31 -0700392bool tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530393
Vakul Garg2b794c42019-02-23 08:42:37 +0000394static inline struct tls_msg *tls_msg(struct sk_buff *skb)
395{
396 return (struct tls_msg *)strp_msg(skb);
397}
398
Boris Pismenny94850252019-02-27 17:38:03 +0200399static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
Dave Watson3c4d7552017-06-14 11:37:39 -0700400{
Boris Pismenny94850252019-02-27 17:38:03 +0200401 return !!ctx->partially_sent_record;
Dave Watson3c4d7552017-06-14 11:37:39 -0700402}
403
404static inline int tls_complete_pending_work(struct sock *sk,
405 struct tls_context *ctx,
406 int flags, long *timeo)
407{
408 int rc = 0;
409
410 if (unlikely(sk->sk_write_pending))
411 rc = wait_on_pending_writer(sk, timeo);
412
Boris Pismenny94850252019-02-27 17:38:03 +0200413 if (!rc && tls_is_partially_sent_record(ctx))
414 rc = tls_push_partial_record(sk, ctx, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700415
416 return rc;
417}
418
Dave Watson3c4d7552017-06-14 11:37:39 -0700419static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
420{
421 return tls_ctx->pending_open_record_frags;
422}
423
Vakul Garg9932a292018-09-24 15:35:56 +0530424static inline bool is_tx_ready(struct tls_sw_context_tx *ctx)
Vakul Garga42055e2018-09-21 09:46:13 +0530425{
426 struct tls_rec *rec;
Vakul Garga42055e2018-09-21 09:46:13 +0530427
Vakul Garg9932a292018-09-24 15:35:56 +0530428 rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
Vakul Garga42055e2018-09-21 09:46:13 +0530429 if (!rec)
430 return false;
431
Vakul Garg9932a292018-09-24 15:35:56 +0530432 return READ_ONCE(rec->tx_ready);
Vakul Garga42055e2018-09-21 09:46:13 +0530433}
434
Boris Pismenny4799ac82018-07-13 14:33:43 +0300435struct sk_buff *
436tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
437 struct sk_buff *skb);
438
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300439static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
440{
Boris Pismenny4799ac82018-07-13 14:33:43 +0300441#ifdef CONFIG_SOCK_VALIDATE_XMIT
Jakub Kicinskib4f47f32019-04-08 17:59:50 -0700442 return sk_fullsock(sk) &&
Boris Pismenny4799ac82018-07-13 14:33:43 +0300443 (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
444 &tls_validate_xmit_skb);
445#else
446 return false;
447#endif
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300448}
449
Dave Watsonf4a8e432018-03-22 10:10:15 -0700450static inline void tls_err_abort(struct sock *sk, int err)
Dave Watson3c4d7552017-06-14 11:37:39 -0700451{
Dave Watsonf4a8e432018-03-22 10:10:15 -0700452 sk->sk_err = err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700453 sk->sk_error_report(sk);
454}
455
456static inline bool tls_bigint_increment(unsigned char *seq, int len)
457{
458 int i;
459
460 for (i = len - 1; i >= 0; i--) {
461 ++seq[i];
462 if (seq[i] != 0)
463 break;
464 }
465
466 return (i == -1);
467}
468
Vakul Garg4509de12019-02-14 07:11:35 +0000469static inline struct tls_context *tls_get_ctx(const struct sock *sk)
470{
471 struct inet_connection_sock *icsk = inet_csk(sk);
472
473 return icsk->icsk_ulp_data;
474}
475
Dave Watson3c4d7552017-06-14 11:37:39 -0700476static inline void tls_advance_record_sn(struct sock *sk,
Jakub Kicinskifb0f8862019-06-03 15:17:05 -0700477 struct tls_prot_info *prot,
478 struct cipher_context *ctx)
Dave Watson3c4d7552017-06-14 11:37:39 -0700479{
Vakul Garg4509de12019-02-14 07:11:35 +0000480 if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
Dave Watsonf4a8e432018-03-22 10:10:15 -0700481 tls_err_abort(sk, EBADMSG);
Dave Watson130b3922019-01-30 21:58:31 +0000482
Jakub Kicinskifb0f8862019-06-03 15:17:05 -0700483 if (prot->version != TLS_1_3_VERSION)
Dave Watson130b3922019-01-30 21:58:31 +0000484 tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
Vakul Garg4509de12019-02-14 07:11:35 +0000485 prot->iv_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700486}
487
488static inline void tls_fill_prepend(struct tls_context *ctx,
489 char *buf,
490 size_t plaintext_len,
Dave Watson130b3922019-01-30 21:58:31 +0000491 unsigned char record_type,
492 int version)
Dave Watson3c4d7552017-06-14 11:37:39 -0700493{
Vakul Garg4509de12019-02-14 07:11:35 +0000494 struct tls_prot_info *prot = &ctx->prot_info;
495 size_t pkt_len, iv_size = prot->iv_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700496
Vakul Garg4509de12019-02-14 07:11:35 +0000497 pkt_len = plaintext_len + prot->tag_size;
Dave Watson130b3922019-01-30 21:58:31 +0000498 if (version != TLS_1_3_VERSION) {
499 pkt_len += iv_size;
500
501 memcpy(buf + TLS_NONCE_OFFSET,
502 ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
503 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700504
505 /* we cover nonce explicit here as well, so buf should be of
506 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
507 */
Dave Watson130b3922019-01-30 21:58:31 +0000508 buf[0] = version == TLS_1_3_VERSION ?
509 TLS_RECORD_TYPE_DATA : record_type;
510 /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
511 buf[1] = TLS_1_2_VERSION_MINOR;
512 buf[2] = TLS_1_2_VERSION_MAJOR;
Dave Watson3c4d7552017-06-14 11:37:39 -0700513 /* we can use IV for nonce explicit according to spec */
514 buf[3] = pkt_len >> 8;
515 buf[4] = pkt_len & 0xFF;
Dave Watson3c4d7552017-06-14 11:37:39 -0700516}
517
Ilya Lesokhin213ef6e2017-11-13 10:22:47 +0200518static inline void tls_make_aad(char *buf,
519 size_t size,
520 char *record_sequence,
521 int record_sequence_size,
Dave Watson130b3922019-01-30 21:58:31 +0000522 unsigned char record_type,
523 int version)
Ilya Lesokhin213ef6e2017-11-13 10:22:47 +0200524{
Dave Watson130b3922019-01-30 21:58:31 +0000525 if (version != TLS_1_3_VERSION) {
526 memcpy(buf, record_sequence, record_sequence_size);
527 buf += 8;
528 } else {
529 size += TLS_CIPHER_AES_GCM_128_TAG_SIZE;
530 }
Ilya Lesokhin213ef6e2017-11-13 10:22:47 +0200531
Dave Watson130b3922019-01-30 21:58:31 +0000532 buf[0] = version == TLS_1_3_VERSION ?
533 TLS_RECORD_TYPE_DATA : record_type;
534 buf[1] = TLS_1_2_VERSION_MAJOR;
535 buf[2] = TLS_1_2_VERSION_MINOR;
536 buf[3] = size >> 8;
537 buf[4] = size & 0xFF;
538}
539
540static inline void xor_iv_with_seq(int version, char *iv, char *seq)
541{
542 int i;
543
544 if (version == TLS_1_3_VERSION) {
545 for (i = 0; i < 8; i++)
546 iv[i + 4] ^= seq[i];
547 }
Ilya Lesokhin213ef6e2017-11-13 10:22:47 +0200548}
549
Dave Watson3c4d7552017-06-14 11:37:39 -0700550
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300551static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
Dave Watson3c4d7552017-06-14 11:37:39 -0700552 const struct tls_context *tls_ctx)
553{
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300554 return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
555}
556
557static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
558 const struct tls_context *tls_ctx)
559{
560 return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
Dave Watson3c4d7552017-06-14 11:37:39 -0700561}
562
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300563static inline struct tls_offload_context_tx *
564tls_offload_ctx_tx(const struct tls_context *tls_ctx)
Dave Watson3c4d7552017-06-14 11:37:39 -0700565{
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300566 return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
Dave Watson3c4d7552017-06-14 11:37:39 -0700567}
568
John Fastabend0608c692018-12-20 11:35:35 -0800569static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
570{
571 struct tls_context *ctx = tls_get_ctx(sk);
572
573 if (!ctx)
574 return false;
575 return !!tls_sw_ctx_tx(ctx);
576}
577
Boris Pismenny7463d3a2019-02-27 17:38:04 +0200578void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
579void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
580
Boris Pismenny4799ac82018-07-13 14:33:43 +0300581static inline struct tls_offload_context_rx *
582tls_offload_ctx_rx(const struct tls_context *tls_ctx)
583{
584 return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
585}
586
Jakub Kicinski2e361172019-06-05 14:11:39 -0700587#if IS_ENABLED(CONFIG_TLS_DEVICE)
588static inline void *__tls_driver_ctx(struct tls_context *tls_ctx,
589 enum tls_offload_ctx_dir direction)
590{
591 if (direction == TLS_OFFLOAD_CTX_DIR_TX)
592 return tls_offload_ctx_tx(tls_ctx)->driver_state;
593 else
594 return tls_offload_ctx_rx(tls_ctx)->driver_state;
595}
596
597static inline void *
598tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
599{
600 return __tls_driver_ctx(tls_get_ctx(sk), direction);
601}
602#endif
603
Boris Pismenny4799ac82018-07-13 14:33:43 +0300604/* The TLS context is valid until sk_destruct is called */
605static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
606{
607 struct tls_context *tls_ctx = tls_get_ctx(sk);
608 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
609
Jakub Kicinski63a1c952019-04-25 12:32:04 -0700610 atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | 1);
Boris Pismenny4799ac82018-07-13 14:33:43 +0300611}
612
Jakub Kicinskif953d33b2019-06-10 21:40:02 -0700613static inline void
614tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type)
615{
616 struct tls_context *tls_ctx = tls_get_ctx(sk);
617
618 tls_offload_ctx_rx(tls_ctx)->resync_type = type;
619}
Boris Pismenny4799ac82018-07-13 14:33:43 +0300620
Dave Watson3c4d7552017-06-14 11:37:39 -0700621int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
622 unsigned char *record_type);
Atul Guptadd0bed12018-03-31 21:41:52 +0530623void tls_register_device(struct tls_device *device);
624void tls_unregister_device(struct tls_device *device);
Boris Pismenny4799ac82018-07-13 14:33:43 +0300625int tls_device_decrypted(struct sock *sk, struct sk_buff *skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +0300626int decrypt_skb(struct sock *sk, struct sk_buff *skb,
627 struct scatterlist *sgout);
Dirk van der Merweb9727d72019-06-05 14:11:40 -0700628struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
Dave Watson3c4d7552017-06-14 11:37:39 -0700629
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300630struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
631 struct net_device *dev,
632 struct sk_buff *skb);
633
634int tls_sw_fallback_init(struct sock *sk,
Boris Pismennyd80a1b9d2018-07-13 14:33:39 +0300635 struct tls_offload_context_tx *offload_ctx,
Ilya Lesokhine8f69792018-04-30 10:16:16 +0300636 struct tls_crypto_info *crypto_info);
637
Boris Pismenny4799ac82018-07-13 14:33:43 +0300638int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
639
640void tls_device_offload_cleanup_rx(struct sock *sk);
Jakub Kicinskif953d33b2019-06-10 21:40:02 -0700641void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
Boris Pismenny4799ac82018-07-13 14:33:43 +0300642
Dave Watson3c4d7552017-06-14 11:37:39 -0700643#endif /* _TLS_OFFLOAD_H */