xref: /openbmc/linux/net/tls/tls_device.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e8f69799SIlya Lesokhin /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
2e8f69799SIlya Lesokhin  *
3e8f69799SIlya Lesokhin  * This software is available to you under a choice of one of two
4e8f69799SIlya Lesokhin  * licenses.  You may choose to be licensed under the terms of the GNU
5e8f69799SIlya Lesokhin  * General Public License (GPL) Version 2, available from the file
6e8f69799SIlya Lesokhin  * COPYING in the main directory of this source tree, or the
7e8f69799SIlya Lesokhin  * OpenIB.org BSD license below:
8e8f69799SIlya Lesokhin  *
9e8f69799SIlya Lesokhin  *     Redistribution and use in source and binary forms, with or
10e8f69799SIlya Lesokhin  *     without modification, are permitted provided that the following
11e8f69799SIlya Lesokhin  *     conditions are met:
12e8f69799SIlya Lesokhin  *
13e8f69799SIlya Lesokhin  *      - Redistributions of source code must retain the above
14e8f69799SIlya Lesokhin  *        copyright notice, this list of conditions and the following
15e8f69799SIlya Lesokhin  *        disclaimer.
16e8f69799SIlya Lesokhin  *
17e8f69799SIlya Lesokhin  *      - Redistributions in binary form must reproduce the above
18e8f69799SIlya Lesokhin  *        copyright notice, this list of conditions and the following
19e8f69799SIlya Lesokhin  *        disclaimer in the documentation and/or other materials
20e8f69799SIlya Lesokhin  *        provided with the distribution.
21e8f69799SIlya Lesokhin  *
22e8f69799SIlya Lesokhin  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23e8f69799SIlya Lesokhin  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24e8f69799SIlya Lesokhin  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25e8f69799SIlya Lesokhin  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26e8f69799SIlya Lesokhin  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27e8f69799SIlya Lesokhin  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28e8f69799SIlya Lesokhin  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29e8f69799SIlya Lesokhin  * SOFTWARE.
30e8f69799SIlya Lesokhin  */
31e8f69799SIlya Lesokhin 
32e8f69799SIlya Lesokhin #include <crypto/aead.h>
33e8f69799SIlya Lesokhin #include <linux/highmem.h>
34e8f69799SIlya Lesokhin #include <linux/module.h>
35e8f69799SIlya Lesokhin #include <linux/netdevice.h>
36e8f69799SIlya Lesokhin #include <net/dst.h>
37e8f69799SIlya Lesokhin #include <net/inet_connection_sock.h>
38e8f69799SIlya Lesokhin #include <net/tcp.h>
39e8f69799SIlya Lesokhin #include <net/tls.h>
40e8f69799SIlya Lesokhin 
4158790314SJakub Kicinski #include "tls.h"
428538d29cSJakub Kicinski #include "trace.h"
438538d29cSJakub Kicinski 
44e8f69799SIlya Lesokhin /* device_offload_lock is used to synchronize tls_dev_add
45e8f69799SIlya Lesokhin  * against NETDEV_DOWN notifications.
46e8f69799SIlya Lesokhin  */
47e8f69799SIlya Lesokhin static DECLARE_RWSEM(device_offload_lock);
48e8f69799SIlya Lesokhin 
497adc91e0STariq Toukan static struct workqueue_struct *destruct_wq __read_mostly;
50e8f69799SIlya Lesokhin 
51e8f69799SIlya Lesokhin static LIST_HEAD(tls_device_list);
52c55dcdd4SMaxim Mikityanskiy static LIST_HEAD(tls_device_down_list);
53e8f69799SIlya Lesokhin static DEFINE_SPINLOCK(tls_device_lock);
54e8f69799SIlya Lesokhin 
556b47808fSJakub Kicinski static struct page *dummy_page;
566b47808fSJakub Kicinski 
tls_device_free_ctx(struct tls_context * ctx)57e8f69799SIlya Lesokhin static void tls_device_free_ctx(struct tls_context *ctx)
58e8f69799SIlya Lesokhin {
595a03bc73SJakub Kicinski 	if (ctx->tx_conf == TLS_HW) {
60d80a1b9dSBoris Pismenny 		kfree(tls_offload_ctx_tx(ctx));
615a03bc73SJakub Kicinski 		kfree(ctx->tx.rec_seq);
625a03bc73SJakub Kicinski 		kfree(ctx->tx.iv);
635a03bc73SJakub Kicinski 	}
64e8f69799SIlya Lesokhin 
654799ac81SBoris Pismenny 	if (ctx->rx_conf == TLS_HW)
664799ac81SBoris Pismenny 		kfree(tls_offload_ctx_rx(ctx));
674799ac81SBoris Pismenny 
6815a7dea7SJakub Kicinski 	tls_ctx_free(NULL, ctx);
69e8f69799SIlya Lesokhin }
70e8f69799SIlya Lesokhin 
tls_device_tx_del_task(struct work_struct * work)717adc91e0STariq Toukan static void tls_device_tx_del_task(struct work_struct *work)
72e8f69799SIlya Lesokhin {
737adc91e0STariq Toukan 	struct tls_offload_context_tx *offload_ctx =
747adc91e0STariq Toukan 		container_of(work, struct tls_offload_context_tx, destruct_work);
757adc91e0STariq Toukan 	struct tls_context *ctx = offload_ctx->ctx;
7694ce3b64SMaxim Mikityanskiy 	struct net_device *netdev;
7794ce3b64SMaxim Mikityanskiy 
7894ce3b64SMaxim Mikityanskiy 	/* Safe, because this is the destroy flow, refcount is 0, so
7994ce3b64SMaxim Mikityanskiy 	 * tls_device_down can't store this field in parallel.
8094ce3b64SMaxim Mikityanskiy 	 */
8194ce3b64SMaxim Mikityanskiy 	netdev = rcu_dereference_protected(ctx->netdev,
8294ce3b64SMaxim Mikityanskiy 					   !refcount_read(&ctx->refcount));
83e8f69799SIlya Lesokhin 
847adc91e0STariq Toukan 	netdev->tlsdev_ops->tls_dev_del(netdev, ctx, TLS_OFFLOAD_CTX_DIR_TX);
85e8f69799SIlya Lesokhin 	dev_put(netdev);
864799ac81SBoris Pismenny 	ctx->netdev = NULL;
87e8f69799SIlya Lesokhin 	tls_device_free_ctx(ctx);
88e8f69799SIlya Lesokhin }
89e8f69799SIlya Lesokhin 
tls_device_queue_ctx_destruction(struct tls_context * ctx)90e8f69799SIlya Lesokhin static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
91e8f69799SIlya Lesokhin {
9294ce3b64SMaxim Mikityanskiy 	struct net_device *netdev;
93e8f69799SIlya Lesokhin 	unsigned long flags;
94113671b2STariq Toukan 	bool async_cleanup;
95e8f69799SIlya Lesokhin 
96e8f69799SIlya Lesokhin 	spin_lock_irqsave(&tls_device_lock, flags);
97113671b2STariq Toukan 	if (unlikely(!refcount_dec_and_test(&ctx->refcount))) {
98113671b2STariq Toukan 		spin_unlock_irqrestore(&tls_device_lock, flags);
99113671b2STariq Toukan 		return;
100113671b2STariq Toukan 	}
101f08d8c1bSTariq Toukan 
1027adc91e0STariq Toukan 	list_del(&ctx->list); /* Remove from tls_device_list / tls_device_down_list */
10394ce3b64SMaxim Mikityanskiy 
10494ce3b64SMaxim Mikityanskiy 	/* Safe, because this is the destroy flow, refcount is 0, so
10594ce3b64SMaxim Mikityanskiy 	 * tls_device_down can't store this field in parallel.
10694ce3b64SMaxim Mikityanskiy 	 */
10794ce3b64SMaxim Mikityanskiy 	netdev = rcu_dereference_protected(ctx->netdev,
10894ce3b64SMaxim Mikityanskiy 					   !refcount_read(&ctx->refcount));
10994ce3b64SMaxim Mikityanskiy 
11094ce3b64SMaxim Mikityanskiy 	async_cleanup = netdev && ctx->tx_conf == TLS_HW;
111113671b2STariq Toukan 	if (async_cleanup) {
1127adc91e0STariq Toukan 		struct tls_offload_context_tx *offload_ctx = tls_offload_ctx_tx(ctx);
113e8f69799SIlya Lesokhin 
1147adc91e0STariq Toukan 		/* queue_work inside the spinlock
115e8f69799SIlya Lesokhin 		 * to make sure tls_device_down waits for that work.
116e8f69799SIlya Lesokhin 		 */
1177adc91e0STariq Toukan 		queue_work(destruct_wq, &offload_ctx->destruct_work);
118113671b2STariq Toukan 	}
119e8f69799SIlya Lesokhin 	spin_unlock_irqrestore(&tls_device_lock, flags);
120113671b2STariq Toukan 
121113671b2STariq Toukan 	if (!async_cleanup)
122113671b2STariq Toukan 		tls_device_free_ctx(ctx);
123e8f69799SIlya Lesokhin }
124e8f69799SIlya Lesokhin 
125e8f69799SIlya Lesokhin /* We assume that the socket is already connected */
get_netdev_for_sock(struct sock * sk)126e8f69799SIlya Lesokhin static struct net_device *get_netdev_for_sock(struct sock *sk)
127e8f69799SIlya Lesokhin {
128e8f69799SIlya Lesokhin 	struct dst_entry *dst = sk_dst_get(sk);
129e8f69799SIlya Lesokhin 	struct net_device *netdev = NULL;
130e8f69799SIlya Lesokhin 
131e8f69799SIlya Lesokhin 	if (likely(dst)) {
132153cbd13STariq Toukan 		netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
133e8f69799SIlya Lesokhin 		dev_hold(netdev);
134e8f69799SIlya Lesokhin 	}
135e8f69799SIlya Lesokhin 
136e8f69799SIlya Lesokhin 	dst_release(dst);
137e8f69799SIlya Lesokhin 
138e8f69799SIlya Lesokhin 	return netdev;
139e8f69799SIlya Lesokhin }
140e8f69799SIlya Lesokhin 
destroy_record(struct tls_record_info * record)141e8f69799SIlya Lesokhin static void destroy_record(struct tls_record_info *record)
142e8f69799SIlya Lesokhin {
1437ccd4519SJakub Kicinski 	int i;
144e8f69799SIlya Lesokhin 
1457ccd4519SJakub Kicinski 	for (i = 0; i < record->num_frags; i++)
146c420c989SMatteo Croce 		__skb_frag_unref(&record->frags[i], false);
147e8f69799SIlya Lesokhin 	kfree(record);
148e8f69799SIlya Lesokhin }
149e8f69799SIlya Lesokhin 
delete_all_records(struct tls_offload_context_tx * offload_ctx)150d80a1b9dSBoris Pismenny static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
151e8f69799SIlya Lesokhin {
152e8f69799SIlya Lesokhin 	struct tls_record_info *info, *temp;
153e8f69799SIlya Lesokhin 
154e8f69799SIlya Lesokhin 	list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
155e8f69799SIlya Lesokhin 		list_del(&info->list);
156e8f69799SIlya Lesokhin 		destroy_record(info);
157e8f69799SIlya Lesokhin 	}
158e8f69799SIlya Lesokhin 
159e8f69799SIlya Lesokhin 	offload_ctx->retransmit_hint = NULL;
160e8f69799SIlya Lesokhin }
161e8f69799SIlya Lesokhin 
tls_icsk_clean_acked(struct sock * sk,u32 acked_seq)162e8f69799SIlya Lesokhin static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
163e8f69799SIlya Lesokhin {
164e8f69799SIlya Lesokhin 	struct tls_context *tls_ctx = tls_get_ctx(sk);
165e8f69799SIlya Lesokhin 	struct tls_record_info *info, *temp;
166d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *ctx;
167e8f69799SIlya Lesokhin 	u64 deleted_records = 0;
168e8f69799SIlya Lesokhin 	unsigned long flags;
169e8f69799SIlya Lesokhin 
170e8f69799SIlya Lesokhin 	if (!tls_ctx)
171e8f69799SIlya Lesokhin 		return;
172e8f69799SIlya Lesokhin 
173d80a1b9dSBoris Pismenny 	ctx = tls_offload_ctx_tx(tls_ctx);
174e8f69799SIlya Lesokhin 
175e8f69799SIlya Lesokhin 	spin_lock_irqsave(&ctx->lock, flags);
176e8f69799SIlya Lesokhin 	info = ctx->retransmit_hint;
1776e3d02b6SJakub Kicinski 	if (info && !before(acked_seq, info->end_seq))
178e8f69799SIlya Lesokhin 		ctx->retransmit_hint = NULL;
179e8f69799SIlya Lesokhin 
180e8f69799SIlya Lesokhin 	list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
181e8f69799SIlya Lesokhin 		if (before(acked_seq, info->end_seq))
182e8f69799SIlya Lesokhin 			break;
183e8f69799SIlya Lesokhin 		list_del(&info->list);
184e8f69799SIlya Lesokhin 
185e8f69799SIlya Lesokhin 		destroy_record(info);
186e8f69799SIlya Lesokhin 		deleted_records++;
187e8f69799SIlya Lesokhin 	}
188e8f69799SIlya Lesokhin 
189e8f69799SIlya Lesokhin 	ctx->unacked_record_sn += deleted_records;
190e8f69799SIlya Lesokhin 	spin_unlock_irqrestore(&ctx->lock, flags);
191e8f69799SIlya Lesokhin }
192e8f69799SIlya Lesokhin 
193e8f69799SIlya Lesokhin /* At this point, there should be no references on this
194e8f69799SIlya Lesokhin  * socket and no in-flight SKBs associated with this
195e8f69799SIlya Lesokhin  * socket, so it is safe to free all the resources.
196e8f69799SIlya Lesokhin  */
tls_device_sk_destruct(struct sock * sk)1978d5a49e9SJakub Kicinski void tls_device_sk_destruct(struct sock *sk)
198e8f69799SIlya Lesokhin {
199e8f69799SIlya Lesokhin 	struct tls_context *tls_ctx = tls_get_ctx(sk);
200d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
201e8f69799SIlya Lesokhin 
2024799ac81SBoris Pismenny 	tls_ctx->sk_destruct(sk);
2034799ac81SBoris Pismenny 
2044799ac81SBoris Pismenny 	if (tls_ctx->tx_conf == TLS_HW) {
205e8f69799SIlya Lesokhin 		if (ctx->open_record)
206e8f69799SIlya Lesokhin 			destroy_record(ctx->open_record);
207e8f69799SIlya Lesokhin 		delete_all_records(ctx);
208e8f69799SIlya Lesokhin 		crypto_free_aead(ctx->aead_send);
209e8f69799SIlya Lesokhin 		clean_acked_data_disable(inet_csk(sk));
2104799ac81SBoris Pismenny 	}
211e8f69799SIlya Lesokhin 
212e8f69799SIlya Lesokhin 	tls_device_queue_ctx_destruction(tls_ctx);
213e8f69799SIlya Lesokhin }
2148d5a49e9SJakub Kicinski EXPORT_SYMBOL_GPL(tls_device_sk_destruct);
215e8f69799SIlya Lesokhin 
tls_device_free_resources_tx(struct sock * sk)21635b71a34SJakub Kicinski void tls_device_free_resources_tx(struct sock *sk)
21735b71a34SJakub Kicinski {
21835b71a34SJakub Kicinski 	struct tls_context *tls_ctx = tls_get_ctx(sk);
21935b71a34SJakub Kicinski 
22035b71a34SJakub Kicinski 	tls_free_partial_record(sk, tls_ctx);
22135b71a34SJakub Kicinski }
22235b71a34SJakub Kicinski 
tls_offload_tx_resync_request(struct sock * sk,u32 got_seq,u32 exp_seq)2238538d29cSJakub Kicinski void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq)
2248538d29cSJakub Kicinski {
2258538d29cSJakub Kicinski 	struct tls_context *tls_ctx = tls_get_ctx(sk);
2268538d29cSJakub Kicinski 
2278538d29cSJakub Kicinski 	trace_tls_device_tx_resync_req(sk, got_seq, exp_seq);
2288538d29cSJakub Kicinski 	WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
2298538d29cSJakub Kicinski }
2308538d29cSJakub Kicinski EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request);
2318538d29cSJakub Kicinski 
tls_device_resync_tx(struct sock * sk,struct tls_context * tls_ctx,u32 seq)23250180074SJakub Kicinski static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
23350180074SJakub Kicinski 				 u32 seq)
23450180074SJakub Kicinski {
23550180074SJakub Kicinski 	struct net_device *netdev;
23650180074SJakub Kicinski 	struct sk_buff *skb;
237b5d9a834SDirk van der Merwe 	int err = 0;
23850180074SJakub Kicinski 	u8 *rcd_sn;
23950180074SJakub Kicinski 
24050180074SJakub Kicinski 	skb = tcp_write_queue_tail(sk);
24150180074SJakub Kicinski 	if (skb)
24250180074SJakub Kicinski 		TCP_SKB_CB(skb)->eor = 1;
24350180074SJakub Kicinski 
24450180074SJakub Kicinski 	rcd_sn = tls_ctx->tx.rec_seq;
24550180074SJakub Kicinski 
2468538d29cSJakub Kicinski 	trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
24750180074SJakub Kicinski 	down_read(&device_offload_lock);
24894ce3b64SMaxim Mikityanskiy 	netdev = rcu_dereference_protected(tls_ctx->netdev,
24994ce3b64SMaxim Mikityanskiy 					   lockdep_is_held(&device_offload_lock));
25050180074SJakub Kicinski 	if (netdev)
251b5d9a834SDirk van der Merwe 		err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
252b5d9a834SDirk van der Merwe 							 rcd_sn,
25350180074SJakub Kicinski 							 TLS_OFFLOAD_CTX_DIR_TX);
25450180074SJakub Kicinski 	up_read(&device_offload_lock);
255b5d9a834SDirk van der Merwe 	if (err)
256b5d9a834SDirk van der Merwe 		return;
25750180074SJakub Kicinski 
25850180074SJakub Kicinski 	clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
25950180074SJakub Kicinski }
26050180074SJakub Kicinski 
tls_append_frag(struct tls_record_info * record,struct page_frag * pfrag,int size)261e8f69799SIlya Lesokhin static void tls_append_frag(struct tls_record_info *record,
262e8f69799SIlya Lesokhin 			    struct page_frag *pfrag,
263e8f69799SIlya Lesokhin 			    int size)
264e8f69799SIlya Lesokhin {
265e8f69799SIlya Lesokhin 	skb_frag_t *frag;
266e8f69799SIlya Lesokhin 
267e8f69799SIlya Lesokhin 	frag = &record->frags[record->num_frags - 1];
268d8e18a51SMatthew Wilcox (Oracle) 	if (skb_frag_page(frag) == pfrag->page &&
269b54c9d5bSJonathan Lemon 	    skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
270d8e18a51SMatthew Wilcox (Oracle) 		skb_frag_size_add(frag, size);
271e8f69799SIlya Lesokhin 	} else {
272e8f69799SIlya Lesokhin 		++frag;
273b51f4113SYunsheng Lin 		skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset,
274b51f4113SYunsheng Lin 					size);
275e8f69799SIlya Lesokhin 		++record->num_frags;
276e8f69799SIlya Lesokhin 		get_page(pfrag->page);
277e8f69799SIlya Lesokhin 	}
278e8f69799SIlya Lesokhin 
279e8f69799SIlya Lesokhin 	pfrag->offset += size;
280e8f69799SIlya Lesokhin 	record->len += size;
281e8f69799SIlya Lesokhin }
282e8f69799SIlya Lesokhin 
tls_push_record(struct sock * sk,struct tls_context * ctx,struct tls_offload_context_tx * offload_ctx,struct tls_record_info * record,int flags)283e8f69799SIlya Lesokhin static int tls_push_record(struct sock *sk,
284e8f69799SIlya Lesokhin 			   struct tls_context *ctx,
285d80a1b9dSBoris Pismenny 			   struct tls_offload_context_tx *offload_ctx,
286e8f69799SIlya Lesokhin 			   struct tls_record_info *record,
287e7b159a4SJakub Kicinski 			   int flags)
288e8f69799SIlya Lesokhin {
2894509de14SVakul Garg 	struct tls_prot_info *prot = &ctx->prot_info;
290e8f69799SIlya Lesokhin 	struct tcp_sock *tp = tcp_sk(sk);
291e8f69799SIlya Lesokhin 	skb_frag_t *frag;
292e8f69799SIlya Lesokhin 	int i;
293e8f69799SIlya Lesokhin 
294e8f69799SIlya Lesokhin 	record->end_seq = tp->write_seq + record->len;
295d4774ac0SJakub Kicinski 	list_add_tail_rcu(&record->list, &offload_ctx->records_list);
296e8f69799SIlya Lesokhin 	offload_ctx->open_record = NULL;
29750180074SJakub Kicinski 
29850180074SJakub Kicinski 	if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
29950180074SJakub Kicinski 		tls_device_resync_tx(sk, ctx, tp->write_seq);
30050180074SJakub Kicinski 
301fb0f886fSJakub Kicinski 	tls_advance_record_sn(sk, prot, &ctx->tx);
302e8f69799SIlya Lesokhin 
303e8f69799SIlya Lesokhin 	for (i = 0; i < record->num_frags; i++) {
304e8f69799SIlya Lesokhin 		frag = &record->frags[i];
305e8f69799SIlya Lesokhin 		sg_unmark_end(&offload_ctx->sg_tx_data[i]);
306e8f69799SIlya Lesokhin 		sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
307b54c9d5bSJonathan Lemon 			    skb_frag_size(frag), skb_frag_off(frag));
308d8e18a51SMatthew Wilcox (Oracle) 		sk_mem_charge(sk, skb_frag_size(frag));
309e8f69799SIlya Lesokhin 		get_page(skb_frag_page(frag));
310e8f69799SIlya Lesokhin 	}
311e8f69799SIlya Lesokhin 	sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
312e8f69799SIlya Lesokhin 
313e8f69799SIlya Lesokhin 	/* all ready, send */
314e8f69799SIlya Lesokhin 	return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
315e8f69799SIlya Lesokhin }
316e8f69799SIlya Lesokhin 
tls_device_record_close(struct sock * sk,struct tls_context * ctx,struct tls_record_info * record,struct page_frag * pfrag,unsigned char record_type)3176b47808fSJakub Kicinski static void tls_device_record_close(struct sock *sk,
318e7b159a4SJakub Kicinski 				    struct tls_context *ctx,
319e7b159a4SJakub Kicinski 				    struct tls_record_info *record,
320e7b159a4SJakub Kicinski 				    struct page_frag *pfrag,
321e7b159a4SJakub Kicinski 				    unsigned char record_type)
322e7b159a4SJakub Kicinski {
323e7b159a4SJakub Kicinski 	struct tls_prot_info *prot = &ctx->prot_info;
3246b47808fSJakub Kicinski 	struct page_frag dummy_tag_frag;
325e7b159a4SJakub Kicinski 
326e7b159a4SJakub Kicinski 	/* append tag
327e7b159a4SJakub Kicinski 	 * device will fill in the tag, we just need to append a placeholder
328e7b159a4SJakub Kicinski 	 * use socket memory to improve coalescing (re-using a single buffer
329e7b159a4SJakub Kicinski 	 * increases frag count)
3306b47808fSJakub Kicinski 	 * if we can't allocate memory now use the dummy page
331e7b159a4SJakub Kicinski 	 */
3326b47808fSJakub Kicinski 	if (unlikely(pfrag->size - pfrag->offset < prot->tag_size) &&
3336b47808fSJakub Kicinski 	    !skb_page_frag_refill(prot->tag_size, pfrag, sk->sk_allocation)) {
3346b47808fSJakub Kicinski 		dummy_tag_frag.page = dummy_page;
3356b47808fSJakub Kicinski 		dummy_tag_frag.offset = 0;
3366b47808fSJakub Kicinski 		pfrag = &dummy_tag_frag;
337e7b159a4SJakub Kicinski 	}
3386b47808fSJakub Kicinski 	tls_append_frag(record, pfrag, prot->tag_size);
339e7b159a4SJakub Kicinski 
340e7b159a4SJakub Kicinski 	/* fill prepend */
341e7b159a4SJakub Kicinski 	tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
342e7b159a4SJakub Kicinski 			 record->len - prot->overhead_size,
3436942a284SVadim Fedorenko 			 record_type);
344e7b159a4SJakub Kicinski }
345e7b159a4SJakub Kicinski 
tls_create_new_record(struct tls_offload_context_tx * offload_ctx,struct page_frag * pfrag,size_t prepend_size)346d80a1b9dSBoris Pismenny static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
347e8f69799SIlya Lesokhin 				 struct page_frag *pfrag,
348e8f69799SIlya Lesokhin 				 size_t prepend_size)
349e8f69799SIlya Lesokhin {
350e8f69799SIlya Lesokhin 	struct tls_record_info *record;
351e8f69799SIlya Lesokhin 	skb_frag_t *frag;
352e8f69799SIlya Lesokhin 
353e8f69799SIlya Lesokhin 	record = kmalloc(sizeof(*record), GFP_KERNEL);
354e8f69799SIlya Lesokhin 	if (!record)
355e8f69799SIlya Lesokhin 		return -ENOMEM;
356e8f69799SIlya Lesokhin 
357e8f69799SIlya Lesokhin 	frag = &record->frags[0];
358b51f4113SYunsheng Lin 	skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset,
359b51f4113SYunsheng Lin 				prepend_size);
360e8f69799SIlya Lesokhin 
361e8f69799SIlya Lesokhin 	get_page(pfrag->page);
362e8f69799SIlya Lesokhin 	pfrag->offset += prepend_size;
363e8f69799SIlya Lesokhin 
364e8f69799SIlya Lesokhin 	record->num_frags = 1;
365e8f69799SIlya Lesokhin 	record->len = prepend_size;
366e8f69799SIlya Lesokhin 	offload_ctx->open_record = record;
367e8f69799SIlya Lesokhin 	return 0;
368e8f69799SIlya Lesokhin }
369e8f69799SIlya Lesokhin 
tls_do_allocation(struct sock * sk,struct tls_offload_context_tx * offload_ctx,struct page_frag * pfrag,size_t prepend_size)370e8f69799SIlya Lesokhin static int tls_do_allocation(struct sock *sk,
371d80a1b9dSBoris Pismenny 			     struct tls_offload_context_tx *offload_ctx,
372e8f69799SIlya Lesokhin 			     struct page_frag *pfrag,
373e8f69799SIlya Lesokhin 			     size_t prepend_size)
374e8f69799SIlya Lesokhin {
375e8f69799SIlya Lesokhin 	int ret;
376e8f69799SIlya Lesokhin 
377e8f69799SIlya Lesokhin 	if (!offload_ctx->open_record) {
378e8f69799SIlya Lesokhin 		if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
379e8f69799SIlya Lesokhin 						   sk->sk_allocation))) {
380d5bee737SJakub Sitnicki 			READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
381e8f69799SIlya Lesokhin 			sk_stream_moderate_sndbuf(sk);
382e8f69799SIlya Lesokhin 			return -ENOMEM;
383e8f69799SIlya Lesokhin 		}
384e8f69799SIlya Lesokhin 
385e8f69799SIlya Lesokhin 		ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
386e8f69799SIlya Lesokhin 		if (ret)
387e8f69799SIlya Lesokhin 			return ret;
388e8f69799SIlya Lesokhin 
389e8f69799SIlya Lesokhin 		if (pfrag->size > pfrag->offset)
390e8f69799SIlya Lesokhin 			return 0;
391e8f69799SIlya Lesokhin 	}
392e8f69799SIlya Lesokhin 
393e8f69799SIlya Lesokhin 	if (!sk_page_frag_refill(sk, pfrag))
394e8f69799SIlya Lesokhin 		return -ENOMEM;
395e8f69799SIlya Lesokhin 
396e8f69799SIlya Lesokhin 	return 0;
397e8f69799SIlya Lesokhin }
398e8f69799SIlya Lesokhin 
tls_device_copy_data(void * addr,size_t bytes,struct iov_iter * i)399e681cc60SJakub Kicinski static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
400e681cc60SJakub Kicinski {
401e681cc60SJakub Kicinski 	size_t pre_copy, nocache;
402e681cc60SJakub Kicinski 
403e681cc60SJakub Kicinski 	pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
404e681cc60SJakub Kicinski 	if (pre_copy) {
405e681cc60SJakub Kicinski 		pre_copy = min(pre_copy, bytes);
406e681cc60SJakub Kicinski 		if (copy_from_iter(addr, pre_copy, i) != pre_copy)
407e681cc60SJakub Kicinski 			return -EFAULT;
408e681cc60SJakub Kicinski 		bytes -= pre_copy;
409e681cc60SJakub Kicinski 		addr += pre_copy;
410e681cc60SJakub Kicinski 	}
411e681cc60SJakub Kicinski 
412e681cc60SJakub Kicinski 	nocache = round_down(bytes, SMP_CACHE_BYTES);
413e681cc60SJakub Kicinski 	if (copy_from_iter_nocache(addr, nocache, i) != nocache)
414e681cc60SJakub Kicinski 		return -EFAULT;
415e681cc60SJakub Kicinski 	bytes -= nocache;
416e681cc60SJakub Kicinski 	addr += nocache;
417e681cc60SJakub Kicinski 
418e681cc60SJakub Kicinski 	if (bytes && copy_from_iter(addr, bytes, i) != bytes)
419e681cc60SJakub Kicinski 		return -EFAULT;
420e681cc60SJakub Kicinski 
421e681cc60SJakub Kicinski 	return 0;
422e681cc60SJakub Kicinski }
423e681cc60SJakub Kicinski 
tls_push_data(struct sock * sk,struct iov_iter * iter,size_t size,int flags,unsigned char record_type)424e8f69799SIlya Lesokhin static int tls_push_data(struct sock *sk,
4253dc8976cSDavid Howells 			 struct iov_iter *iter,
426e8f69799SIlya Lesokhin 			 size_t size, int flags,
4273dc8976cSDavid Howells 			 unsigned char record_type)
428e8f69799SIlya Lesokhin {
429e8f69799SIlya Lesokhin 	struct tls_context *tls_ctx = tls_get_ctx(sk);
4304509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
431d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
4323afef8c7SJiapeng Chong 	struct tls_record_info *record;
43341477662SJakub Kicinski 	int tls_push_record_flags;
434e8f69799SIlya Lesokhin 	struct page_frag *pfrag;
435e8f69799SIlya Lesokhin 	size_t orig_size = size;
436e8f69799SIlya Lesokhin 	u32 max_open_record_len;
437ea1dd3e9SRohit Maheshwari 	bool more = false;
438e8f69799SIlya Lesokhin 	bool done = false;
439ea1dd3e9SRohit Maheshwari 	int copy, rc = 0;
440e8f69799SIlya Lesokhin 	long timeo;
441e8f69799SIlya Lesokhin 
442e8f69799SIlya Lesokhin 	if (flags &
443c004b0e0SHannes Reinecke 	    ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
444c004b0e0SHannes Reinecke 	      MSG_SPLICE_PAGES | MSG_EOR))
4454a5cdc60SValentin Vidic 		return -EOPNOTSUPP;
446e8f69799SIlya Lesokhin 
447c004b0e0SHannes Reinecke 	if ((flags & (MSG_MORE | MSG_EOR)) == (MSG_MORE | MSG_EOR))
448c004b0e0SHannes Reinecke 		return -EINVAL;
449c004b0e0SHannes Reinecke 
45093277b25SJakub Kicinski 	if (unlikely(sk->sk_err))
451e8f69799SIlya Lesokhin 		return -sk->sk_err;
452e8f69799SIlya Lesokhin 
45341477662SJakub Kicinski 	flags |= MSG_SENDPAGE_DECRYPTED;
454f8dd95b2SDavid Howells 	tls_push_record_flags = flags | MSG_MORE;
45541477662SJakub Kicinski 
456e8f69799SIlya Lesokhin 	timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
45794850257SBoris Pismenny 	if (tls_is_partially_sent_record(tls_ctx)) {
45894850257SBoris Pismenny 		rc = tls_push_partial_record(sk, tls_ctx, flags);
459e8f69799SIlya Lesokhin 		if (rc < 0)
460e8f69799SIlya Lesokhin 			return rc;
46194850257SBoris Pismenny 	}
462e8f69799SIlya Lesokhin 
463e8f69799SIlya Lesokhin 	pfrag = sk_page_frag(sk);
464e8f69799SIlya Lesokhin 
465e8f69799SIlya Lesokhin 	/* TLS_HEADER_SIZE is not counted as part of the TLS record, and
466e8f69799SIlya Lesokhin 	 * we need to leave room for an authentication tag.
467e8f69799SIlya Lesokhin 	 */
468e8f69799SIlya Lesokhin 	max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
4694509de14SVakul Garg 			      prot->prepend_size;
470e8f69799SIlya Lesokhin 	do {
47134ef1ed1SJakub Kicinski 		rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
47234ef1ed1SJakub Kicinski 		if (unlikely(rc)) {
473e8f69799SIlya Lesokhin 			rc = sk_stream_wait_memory(sk, &timeo);
474e8f69799SIlya Lesokhin 			if (!rc)
475e8f69799SIlya Lesokhin 				continue;
476e8f69799SIlya Lesokhin 
477e8f69799SIlya Lesokhin 			record = ctx->open_record;
478e8f69799SIlya Lesokhin 			if (!record)
479e8f69799SIlya Lesokhin 				break;
480e8f69799SIlya Lesokhin handle_error:
481e8f69799SIlya Lesokhin 			if (record_type != TLS_RECORD_TYPE_DATA) {
482e8f69799SIlya Lesokhin 				/* avoid sending partial
483e8f69799SIlya Lesokhin 				 * record with type !=
484e8f69799SIlya Lesokhin 				 * application_data
485e8f69799SIlya Lesokhin 				 */
486e8f69799SIlya Lesokhin 				size = orig_size;
487e8f69799SIlya Lesokhin 				destroy_record(record);
488e8f69799SIlya Lesokhin 				ctx->open_record = NULL;
4894509de14SVakul Garg 			} else if (record->len > prot->prepend_size) {
490e8f69799SIlya Lesokhin 				goto last_record;
491e8f69799SIlya Lesokhin 			}
492e8f69799SIlya Lesokhin 
493e8f69799SIlya Lesokhin 			break;
494e8f69799SIlya Lesokhin 		}
495e8f69799SIlya Lesokhin 
496e8f69799SIlya Lesokhin 		record = ctx->open_record;
497e8f69799SIlya Lesokhin 
498c1318b39SBoris Pismenny 		copy = min_t(size_t, size, max_open_record_len - record->len);
4993dc8976cSDavid Howells 		if (copy && (flags & MSG_SPLICE_PAGES)) {
50024763c9cSDavid Howells 			struct page_frag zc_pfrag;
50124763c9cSDavid Howells 			struct page **pages = &zc_pfrag.page;
50224763c9cSDavid Howells 			size_t off;
50324763c9cSDavid Howells 
5043dc8976cSDavid Howells 			rc = iov_iter_extract_pages(iter, &pages,
5053dc8976cSDavid Howells 						    copy, 1, 0, &off);
50624763c9cSDavid Howells 			if (rc <= 0) {
50724763c9cSDavid Howells 				if (rc == 0)
50824763c9cSDavid Howells 					rc = -EIO;
50924763c9cSDavid Howells 				goto handle_error;
51024763c9cSDavid Howells 			}
51124763c9cSDavid Howells 			copy = rc;
51224763c9cSDavid Howells 
51324763c9cSDavid Howells 			if (WARN_ON_ONCE(!sendpage_ok(zc_pfrag.page))) {
5143dc8976cSDavid Howells 				iov_iter_revert(iter, copy);
51524763c9cSDavid Howells 				rc = -EIO;
51624763c9cSDavid Howells 				goto handle_error;
51724763c9cSDavid Howells 			}
51824763c9cSDavid Howells 
51924763c9cSDavid Howells 			zc_pfrag.offset = off;
52024763c9cSDavid Howells 			zc_pfrag.size = copy;
52124763c9cSDavid Howells 			tls_append_frag(record, &zc_pfrag, copy);
522c1318b39SBoris Pismenny 		} else if (copy) {
523c1318b39SBoris Pismenny 			copy = min_t(size_t, copy, pfrag->size - pfrag->offset);
524c1318b39SBoris Pismenny 
525e681cc60SJakub Kicinski 			rc = tls_device_copy_data(page_address(pfrag->page) +
526c1318b39SBoris Pismenny 						  pfrag->offset, copy,
5273dc8976cSDavid Howells 						  iter);
528e681cc60SJakub Kicinski 			if (rc)
529e8f69799SIlya Lesokhin 				goto handle_error;
530e8f69799SIlya Lesokhin 			tls_append_frag(record, pfrag, copy);
531a0df7194SMaxim Mikityanskiy 		}
532e8f69799SIlya Lesokhin 
533e8f69799SIlya Lesokhin 		size -= copy;
534e8f69799SIlya Lesokhin 		if (!size) {
535e8f69799SIlya Lesokhin last_record:
536e8f69799SIlya Lesokhin 			tls_push_record_flags = flags;
537f8dd95b2SDavid Howells 			if (flags & MSG_MORE) {
538ea1dd3e9SRohit Maheshwari 				more = true;
539e8f69799SIlya Lesokhin 				break;
540e8f69799SIlya Lesokhin 			}
541e8f69799SIlya Lesokhin 
542e8f69799SIlya Lesokhin 			done = true;
543e8f69799SIlya Lesokhin 		}
544e8f69799SIlya Lesokhin 
545e8f69799SIlya Lesokhin 		if (done || record->len >= max_open_record_len ||
546e8f69799SIlya Lesokhin 		    (record->num_frags >= MAX_SKB_FRAGS - 1)) {
5476b47808fSJakub Kicinski 			tls_device_record_close(sk, tls_ctx, record,
548e7b159a4SJakub Kicinski 						pfrag, record_type);
549e7b159a4SJakub Kicinski 
550e8f69799SIlya Lesokhin 			rc = tls_push_record(sk,
551e8f69799SIlya Lesokhin 					     tls_ctx,
552e8f69799SIlya Lesokhin 					     ctx,
553e8f69799SIlya Lesokhin 					     record,
554e7b159a4SJakub Kicinski 					     tls_push_record_flags);
555e8f69799SIlya Lesokhin 			if (rc < 0)
556e8f69799SIlya Lesokhin 				break;
557e8f69799SIlya Lesokhin 		}
558e8f69799SIlya Lesokhin 	} while (!done);
559e8f69799SIlya Lesokhin 
560ea1dd3e9SRohit Maheshwari 	tls_ctx->pending_open_record_frags = more;
561ea1dd3e9SRohit Maheshwari 
562e8f69799SIlya Lesokhin 	if (orig_size - size > 0)
563e8f69799SIlya Lesokhin 		rc = orig_size - size;
564e8f69799SIlya Lesokhin 
565e8f69799SIlya Lesokhin 	return rc;
566e8f69799SIlya Lesokhin }
567e8f69799SIlya Lesokhin 
tls_device_sendmsg(struct sock * sk,struct msghdr * msg,size_t size)568e8f69799SIlya Lesokhin int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
569e8f69799SIlya Lesokhin {
570e8f69799SIlya Lesokhin 	unsigned char record_type = TLS_RECORD_TYPE_DATA;
57179ffe608SJakub Kicinski 	struct tls_context *tls_ctx = tls_get_ctx(sk);
572e8f69799SIlya Lesokhin 	int rc;
573e8f69799SIlya Lesokhin 
57424763c9cSDavid Howells 	if (!tls_ctx->zerocopy_sendfile)
57524763c9cSDavid Howells 		msg->msg_flags &= ~MSG_SPLICE_PAGES;
57624763c9cSDavid Howells 
57779ffe608SJakub Kicinski 	mutex_lock(&tls_ctx->tx_lock);
578e8f69799SIlya Lesokhin 	lock_sock(sk);
579e8f69799SIlya Lesokhin 
580e8f69799SIlya Lesokhin 	if (unlikely(msg->msg_controllen)) {
58158790314SJakub Kicinski 		rc = tls_process_cmsg(sk, msg, &record_type);
582e8f69799SIlya Lesokhin 		if (rc)
583e8f69799SIlya Lesokhin 			goto out;
584e8f69799SIlya Lesokhin 	}
585e8f69799SIlya Lesokhin 
5863dc8976cSDavid Howells 	rc = tls_push_data(sk, &msg->msg_iter, size, msg->msg_flags,
5873dc8976cSDavid Howells 			   record_type);
588e8f69799SIlya Lesokhin 
589e8f69799SIlya Lesokhin out:
590e8f69799SIlya Lesokhin 	release_sock(sk);
59179ffe608SJakub Kicinski 	mutex_unlock(&tls_ctx->tx_lock);
592e8f69799SIlya Lesokhin 	return rc;
593e8f69799SIlya Lesokhin }
594e8f69799SIlya Lesokhin 
tls_device_splice_eof(struct socket * sock)595d4c1e80bSDavid Howells void tls_device_splice_eof(struct socket *sock)
596d4c1e80bSDavid Howells {
597d4c1e80bSDavid Howells 	struct sock *sk = sock->sk;
598d4c1e80bSDavid Howells 	struct tls_context *tls_ctx = tls_get_ctx(sk);
5993dc8976cSDavid Howells 	struct iov_iter iter = {};
600d4c1e80bSDavid Howells 
601d4c1e80bSDavid Howells 	if (!tls_is_partially_sent_record(tls_ctx))
602d4c1e80bSDavid Howells 		return;
603d4c1e80bSDavid Howells 
604d4c1e80bSDavid Howells 	mutex_lock(&tls_ctx->tx_lock);
605d4c1e80bSDavid Howells 	lock_sock(sk);
606d4c1e80bSDavid Howells 
607d4c1e80bSDavid Howells 	if (tls_is_partially_sent_record(tls_ctx)) {
6083dc8976cSDavid Howells 		iov_iter_bvec(&iter, ITER_SOURCE, NULL, 0, 0);
6093dc8976cSDavid Howells 		tls_push_data(sk, &iter, 0, 0, TLS_RECORD_TYPE_DATA);
610d4c1e80bSDavid Howells 	}
611d4c1e80bSDavid Howells 
612d4c1e80bSDavid Howells 	release_sock(sk);
613d4c1e80bSDavid Howells 	mutex_unlock(&tls_ctx->tx_lock);
614d4c1e80bSDavid Howells }
615d4c1e80bSDavid Howells 
tls_get_record(struct tls_offload_context_tx * context,u32 seq,u64 * p_record_sn)616d80a1b9dSBoris Pismenny struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
617e8f69799SIlya Lesokhin 				       u32 seq, u64 *p_record_sn)
618e8f69799SIlya Lesokhin {
619e8f69799SIlya Lesokhin 	u64 record_sn = context->hint_record_sn;
62006f5201cSRohit Maheshwari 	struct tls_record_info *info, *last;
621e8f69799SIlya Lesokhin 
622e8f69799SIlya Lesokhin 	info = context->retransmit_hint;
623e8f69799SIlya Lesokhin 	if (!info ||
624e8f69799SIlya Lesokhin 	    before(seq, info->end_seq - info->len)) {
625e8f69799SIlya Lesokhin 		/* if retransmit_hint is irrelevant start
62672a0f6d0SWang Hai 		 * from the beginning of the list
627e8f69799SIlya Lesokhin 		 */
628d4774ac0SJakub Kicinski 		info = list_first_entry_or_null(&context->records_list,
629e8f69799SIlya Lesokhin 						struct tls_record_info, list);
630d4774ac0SJakub Kicinski 		if (!info)
631d4774ac0SJakub Kicinski 			return NULL;
63206f5201cSRohit Maheshwari 		/* send the start_marker record if seq number is before the
63306f5201cSRohit Maheshwari 		 * tls offload start marker sequence number. This record is
63406f5201cSRohit Maheshwari 		 * required to handle TCP packets which are before TLS offload
63506f5201cSRohit Maheshwari 		 * started.
63606f5201cSRohit Maheshwari 		 *  And if it's not start marker, look if this seq number
63706f5201cSRohit Maheshwari 		 * belongs to the list.
63806f5201cSRohit Maheshwari 		 */
63906f5201cSRohit Maheshwari 		if (likely(!tls_record_is_start_marker(info))) {
64006f5201cSRohit Maheshwari 			/* we have the first record, get the last record to see
64106f5201cSRohit Maheshwari 			 * if this seq number belongs to the list.
64206f5201cSRohit Maheshwari 			 */
64306f5201cSRohit Maheshwari 			last = list_last_entry(&context->records_list,
64406f5201cSRohit Maheshwari 					       struct tls_record_info, list);
64506f5201cSRohit Maheshwari 
64606f5201cSRohit Maheshwari 			if (!between(seq, tls_record_start_seq(info),
64706f5201cSRohit Maheshwari 				     last->end_seq))
64806f5201cSRohit Maheshwari 				return NULL;
64906f5201cSRohit Maheshwari 		}
650e8f69799SIlya Lesokhin 		record_sn = context->unacked_record_sn;
651e8f69799SIlya Lesokhin 	}
652e8f69799SIlya Lesokhin 
653d4774ac0SJakub Kicinski 	/* We just need the _rcu for the READ_ONCE() */
654d4774ac0SJakub Kicinski 	rcu_read_lock();
655d4774ac0SJakub Kicinski 	list_for_each_entry_from_rcu(info, &context->records_list, list) {
656e8f69799SIlya Lesokhin 		if (before(seq, info->end_seq)) {
657e8f69799SIlya Lesokhin 			if (!context->retransmit_hint ||
658e8f69799SIlya Lesokhin 			    after(info->end_seq,
659e8f69799SIlya Lesokhin 				  context->retransmit_hint->end_seq)) {
660e8f69799SIlya Lesokhin 				context->hint_record_sn = record_sn;
661e8f69799SIlya Lesokhin 				context->retransmit_hint = info;
662e8f69799SIlya Lesokhin 			}
663e8f69799SIlya Lesokhin 			*p_record_sn = record_sn;
664d4774ac0SJakub Kicinski 			goto exit_rcu_unlock;
665e8f69799SIlya Lesokhin 		}
666e8f69799SIlya Lesokhin 		record_sn++;
667e8f69799SIlya Lesokhin 	}
668d4774ac0SJakub Kicinski 	info = NULL;
669e8f69799SIlya Lesokhin 
670d4774ac0SJakub Kicinski exit_rcu_unlock:
671d4774ac0SJakub Kicinski 	rcu_read_unlock();
672d4774ac0SJakub Kicinski 	return info;
673e8f69799SIlya Lesokhin }
674e8f69799SIlya Lesokhin EXPORT_SYMBOL(tls_get_record);
675e8f69799SIlya Lesokhin 
tls_device_push_pending_record(struct sock * sk,int flags)676e8f69799SIlya Lesokhin static int tls_device_push_pending_record(struct sock *sk, int flags)
677e8f69799SIlya Lesokhin {
6783dc8976cSDavid Howells 	struct iov_iter iter;
679e8f69799SIlya Lesokhin 
6803dc8976cSDavid Howells 	iov_iter_kvec(&iter, ITER_SOURCE, NULL, 0, 0);
6813dc8976cSDavid Howells 	return tls_push_data(sk, &iter, 0, flags, TLS_RECORD_TYPE_DATA);
682e8f69799SIlya Lesokhin }
683e8f69799SIlya Lesokhin 
tls_device_write_space(struct sock * sk,struct tls_context * ctx)6847463d3a2SBoris Pismenny void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
6857463d3a2SBoris Pismenny {
68602b1fa07SJakub Kicinski 	if (tls_is_partially_sent_record(ctx)) {
6877463d3a2SBoris Pismenny 		gfp_t sk_allocation = sk->sk_allocation;
6887463d3a2SBoris Pismenny 
68902b1fa07SJakub Kicinski 		WARN_ON_ONCE(sk->sk_write_pending);
69002b1fa07SJakub Kicinski 
6917463d3a2SBoris Pismenny 		sk->sk_allocation = GFP_ATOMIC;
69241477662SJakub Kicinski 		tls_push_partial_record(sk, ctx,
69341477662SJakub Kicinski 					MSG_DONTWAIT | MSG_NOSIGNAL |
69441477662SJakub Kicinski 					MSG_SENDPAGE_DECRYPTED);
6957463d3a2SBoris Pismenny 		sk->sk_allocation = sk_allocation;
6967463d3a2SBoris Pismenny 	}
6977463d3a2SBoris Pismenny }
6987463d3a2SBoris Pismenny 
tls_device_resync_rx(struct tls_context * tls_ctx,struct sock * sk,u32 seq,u8 * rcd_sn)699e52972c1SJakub Kicinski static void tls_device_resync_rx(struct tls_context *tls_ctx,
70089fec474SJakub Kicinski 				 struct sock *sk, u32 seq, u8 *rcd_sn)
701e52972c1SJakub Kicinski {
7028538d29cSJakub Kicinski 	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
703e52972c1SJakub Kicinski 	struct net_device *netdev;
704e52972c1SJakub Kicinski 
7058538d29cSJakub Kicinski 	trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type);
70605fc8b6cSMaxim Mikityanskiy 	rcu_read_lock();
70794ce3b64SMaxim Mikityanskiy 	netdev = rcu_dereference(tls_ctx->netdev);
708e52972c1SJakub Kicinski 	if (netdev)
709eeb2efafSJakub Kicinski 		netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
710eeb2efafSJakub Kicinski 						   TLS_OFFLOAD_CTX_DIR_RX);
71105fc8b6cSMaxim Mikityanskiy 	rcu_read_unlock();
712a4d26fdbSJakub Kicinski 	TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
713e52972c1SJakub Kicinski }
714e52972c1SJakub Kicinski 
715ed9b7646SBoris Pismenny static bool
tls_device_rx_resync_async(struct tls_offload_resync_async * resync_async,s64 resync_req,u32 * seq,u16 * rcd_delta)716ed9b7646SBoris Pismenny tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
717138559b9STariq Toukan 			   s64 resync_req, u32 *seq, u16 *rcd_delta)
718ed9b7646SBoris Pismenny {
719ed9b7646SBoris Pismenny 	u32 is_async = resync_req & RESYNC_REQ_ASYNC;
720ed9b7646SBoris Pismenny 	u32 req_seq = resync_req >> 32;
721ed9b7646SBoris Pismenny 	u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
722138559b9STariq Toukan 	u16 i;
723138559b9STariq Toukan 
724138559b9STariq Toukan 	*rcd_delta = 0;
725ed9b7646SBoris Pismenny 
726ed9b7646SBoris Pismenny 	if (is_async) {
727138559b9STariq Toukan 		/* shouldn't get to wraparound:
728138559b9STariq Toukan 		 * too long in async stage, something bad happened
729138559b9STariq Toukan 		 */
730138559b9STariq Toukan 		if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX))
731138559b9STariq Toukan 			return false;
732138559b9STariq Toukan 
733ed9b7646SBoris Pismenny 		/* asynchronous stage: log all headers seq such that
734ed9b7646SBoris Pismenny 		 * req_seq <= seq <= end_seq, and wait for real resync request
735ed9b7646SBoris Pismenny 		 */
736138559b9STariq Toukan 		if (before(*seq, req_seq))
737138559b9STariq Toukan 			return false;
738138559b9STariq Toukan 		if (!after(*seq, req_end) &&
739ed9b7646SBoris Pismenny 		    resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
740ed9b7646SBoris Pismenny 			resync_async->log[resync_async->loglen++] = *seq;
741ed9b7646SBoris Pismenny 
742138559b9STariq Toukan 		resync_async->rcd_delta++;
743138559b9STariq Toukan 
744ed9b7646SBoris Pismenny 		return false;
745ed9b7646SBoris Pismenny 	}
746ed9b7646SBoris Pismenny 
747ed9b7646SBoris Pismenny 	/* synchronous stage: check against the logged entries and
748ed9b7646SBoris Pismenny 	 * proceed to check the next entries if no match was found
749ed9b7646SBoris Pismenny 	 */
750138559b9STariq Toukan 	for (i = 0; i < resync_async->loglen; i++)
751138559b9STariq Toukan 		if (req_seq == resync_async->log[i] &&
752138559b9STariq Toukan 		    atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) {
753138559b9STariq Toukan 			*rcd_delta = resync_async->rcd_delta - i;
754ed9b7646SBoris Pismenny 			*seq = req_seq;
755138559b9STariq Toukan 			resync_async->loglen = 0;
756138559b9STariq Toukan 			resync_async->rcd_delta = 0;
757ed9b7646SBoris Pismenny 			return true;
758ed9b7646SBoris Pismenny 		}
759138559b9STariq Toukan 
760138559b9STariq Toukan 	resync_async->loglen = 0;
761138559b9STariq Toukan 	resync_async->rcd_delta = 0;
762ed9b7646SBoris Pismenny 
763ed9b7646SBoris Pismenny 	if (req_seq == *seq &&
764ed9b7646SBoris Pismenny 	    atomic64_try_cmpxchg(&resync_async->req,
765ed9b7646SBoris Pismenny 				 &resync_req, 0))
766ed9b7646SBoris Pismenny 		return true;
767ed9b7646SBoris Pismenny 
768ed9b7646SBoris Pismenny 	return false;
769ed9b7646SBoris Pismenny }
770ed9b7646SBoris Pismenny 
tls_device_rx_resync_new_rec(struct sock * sk,u32 rcd_len,u32 seq)771f953d33bSJakub Kicinski void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
7724799ac81SBoris Pismenny {
7734799ac81SBoris Pismenny 	struct tls_context *tls_ctx = tls_get_ctx(sk);
7744799ac81SBoris Pismenny 	struct tls_offload_context_rx *rx_ctx;
775f953d33bSJakub Kicinski 	u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
776acb5a07aSBoris Pismenny 	u32 sock_data, is_req_pending;
777f953d33bSJakub Kicinski 	struct tls_prot_info *prot;
7784799ac81SBoris Pismenny 	s64 resync_req;
779138559b9STariq Toukan 	u16 rcd_delta;
7804799ac81SBoris Pismenny 	u32 req_seq;
7814799ac81SBoris Pismenny 
7824799ac81SBoris Pismenny 	if (tls_ctx->rx_conf != TLS_HW)
7834799ac81SBoris Pismenny 		return;
784c55dcdd4SMaxim Mikityanskiy 	if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags)))
785c55dcdd4SMaxim Mikityanskiy 		return;
7864799ac81SBoris Pismenny 
787f953d33bSJakub Kicinski 	prot = &tls_ctx->prot_info;
7884799ac81SBoris Pismenny 	rx_ctx = tls_offload_ctx_rx(tls_ctx);
789f953d33bSJakub Kicinski 	memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
790f953d33bSJakub Kicinski 
791f953d33bSJakub Kicinski 	switch (rx_ctx->resync_type) {
792f953d33bSJakub Kicinski 	case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
7934799ac81SBoris Pismenny 		resync_req = atomic64_read(&rx_ctx->resync_req);
79449673739SJakub Kicinski 		req_seq = resync_req >> 32;
79549673739SJakub Kicinski 		seq += TLS_HEADER_SIZE - 1;
796acb5a07aSBoris Pismenny 		is_req_pending = resync_req;
7974799ac81SBoris Pismenny 
798acb5a07aSBoris Pismenny 		if (likely(!is_req_pending) || req_seq != seq ||
799f953d33bSJakub Kicinski 		    !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
800f953d33bSJakub Kicinski 			return;
801f953d33bSJakub Kicinski 		break;
802f953d33bSJakub Kicinski 	case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
803f953d33bSJakub Kicinski 		if (likely(!rx_ctx->resync_nh_do_now))
804f953d33bSJakub Kicinski 			return;
805f953d33bSJakub Kicinski 
806f953d33bSJakub Kicinski 		/* head of next rec is already in, note that the sock_inq will
807f953d33bSJakub Kicinski 		 * include the currently parsed message when called from parser
808f953d33bSJakub Kicinski 		 */
8098538d29cSJakub Kicinski 		sock_data = tcp_inq(sk);
8108538d29cSJakub Kicinski 		if (sock_data > rcd_len) {
8118538d29cSJakub Kicinski 			trace_tls_device_rx_resync_nh_delay(sk, sock_data,
8128538d29cSJakub Kicinski 							    rcd_len);
813f953d33bSJakub Kicinski 			return;
8148538d29cSJakub Kicinski 		}
815f953d33bSJakub Kicinski 
816f953d33bSJakub Kicinski 		rx_ctx->resync_nh_do_now = 0;
817f953d33bSJakub Kicinski 		seq += rcd_len;
818f953d33bSJakub Kicinski 		tls_bigint_increment(rcd_sn, prot->rec_seq_size);
819f953d33bSJakub Kicinski 		break;
820ed9b7646SBoris Pismenny 	case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
821ed9b7646SBoris Pismenny 		resync_req = atomic64_read(&rx_ctx->resync_async->req);
822ed9b7646SBoris Pismenny 		is_req_pending = resync_req;
823ed9b7646SBoris Pismenny 		if (likely(!is_req_pending))
824ed9b7646SBoris Pismenny 			return;
825ed9b7646SBoris Pismenny 
826ed9b7646SBoris Pismenny 		if (!tls_device_rx_resync_async(rx_ctx->resync_async,
827138559b9STariq Toukan 						resync_req, &seq, &rcd_delta))
828ed9b7646SBoris Pismenny 			return;
829138559b9STariq Toukan 		tls_bigint_subtract(rcd_sn, rcd_delta);
830ed9b7646SBoris Pismenny 		break;
831f953d33bSJakub Kicinski 	}
832f953d33bSJakub Kicinski 
833f953d33bSJakub Kicinski 	tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
834f953d33bSJakub Kicinski }
835f953d33bSJakub Kicinski 
tls_device_core_ctrl_rx_resync(struct tls_context * tls_ctx,struct tls_offload_context_rx * ctx,struct sock * sk,struct sk_buff * skb)836f953d33bSJakub Kicinski static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
837f953d33bSJakub Kicinski 					   struct tls_offload_context_rx *ctx,
838f953d33bSJakub Kicinski 					   struct sock *sk, struct sk_buff *skb)
839f953d33bSJakub Kicinski {
840f953d33bSJakub Kicinski 	struct strp_msg *rxm;
841f953d33bSJakub Kicinski 
842f953d33bSJakub Kicinski 	/* device will request resyncs by itself based on stream scan */
843f953d33bSJakub Kicinski 	if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
844f953d33bSJakub Kicinski 		return;
845f953d33bSJakub Kicinski 	/* already scheduled */
846f953d33bSJakub Kicinski 	if (ctx->resync_nh_do_now)
847f953d33bSJakub Kicinski 		return;
848f953d33bSJakub Kicinski 	/* seen decrypted fragments since last fully-failed record */
849f953d33bSJakub Kicinski 	if (ctx->resync_nh_reset) {
850f953d33bSJakub Kicinski 		ctx->resync_nh_reset = 0;
851f953d33bSJakub Kicinski 		ctx->resync_nh.decrypted_failed = 1;
852f953d33bSJakub Kicinski 		ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
853f953d33bSJakub Kicinski 		return;
854f953d33bSJakub Kicinski 	}
855f953d33bSJakub Kicinski 
856f953d33bSJakub Kicinski 	if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
857f953d33bSJakub Kicinski 		return;
858f953d33bSJakub Kicinski 
859f953d33bSJakub Kicinski 	/* doing resync, bump the next target in case it fails */
860f953d33bSJakub Kicinski 	if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
861f953d33bSJakub Kicinski 		ctx->resync_nh.decrypted_tgt *= 2;
862f953d33bSJakub Kicinski 	else
863f953d33bSJakub Kicinski 		ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
864f953d33bSJakub Kicinski 
865f953d33bSJakub Kicinski 	rxm = strp_msg(skb);
866f953d33bSJakub Kicinski 
867f953d33bSJakub Kicinski 	/* head of next rec is already in, parser will sync for us */
868f953d33bSJakub Kicinski 	if (tcp_inq(sk) > rxm->full_len) {
8698538d29cSJakub Kicinski 		trace_tls_device_rx_resync_nh_schedule(sk);
870f953d33bSJakub Kicinski 		ctx->resync_nh_do_now = 1;
871f953d33bSJakub Kicinski 	} else {
872f953d33bSJakub Kicinski 		struct tls_prot_info *prot = &tls_ctx->prot_info;
873f953d33bSJakub Kicinski 		u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
874f953d33bSJakub Kicinski 
875f953d33bSJakub Kicinski 		memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
876f953d33bSJakub Kicinski 		tls_bigint_increment(rcd_sn, prot->rec_seq_size);
877f953d33bSJakub Kicinski 
878f953d33bSJakub Kicinski 		tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
879f953d33bSJakub Kicinski 				     rcd_sn);
880f953d33bSJakub Kicinski 	}
88138030d7cSJakub Kicinski }
8824799ac81SBoris Pismenny 
883541cc48bSJakub Kicinski static int
tls_device_reencrypt(struct sock * sk,struct tls_context * tls_ctx)884ea7a9d88SGal Pressman tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
8854799ac81SBoris Pismenny {
886ea7a9d88SGal Pressman 	struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
8878db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc;
8888b3c59a7SJakub Kicinski 	int err, offset, copy, data_len, pos;
8898b3c59a7SJakub Kicinski 	struct sk_buff *skb, *skb_iter;
8904799ac81SBoris Pismenny 	struct scatterlist sg[1];
891541cc48bSJakub Kicinski 	struct strp_msg *rxm;
8924799ac81SBoris Pismenny 	char *orig_buf, *buf;
8934799ac81SBoris Pismenny 
894ea7a9d88SGal Pressman 	switch (tls_ctx->crypto_recv.info.cipher_type) {
895ea7a9d88SGal Pressman 	case TLS_CIPHER_AES_GCM_128:
89656e5a6d3SGal Pressman 	case TLS_CIPHER_AES_GCM_256:
897ea7a9d88SGal Pressman 		break;
898ea7a9d88SGal Pressman 	default:
899ea7a9d88SGal Pressman 		return -EINVAL;
900ea7a9d88SGal Pressman 	}
9018db44ab2SSabrina Dubroca 	cipher_desc = get_cipher_desc(tls_ctx->crypto_recv.info.cipher_type);
902ea7a9d88SGal Pressman 
9038b3c59a7SJakub Kicinski 	rxm = strp_msg(tls_strp_msg(sw_ctx));
9048db44ab2SSabrina Dubroca 	orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv,
905ea7a9d88SGal Pressman 			   sk->sk_allocation);
9064799ac81SBoris Pismenny 	if (!orig_buf)
9074799ac81SBoris Pismenny 		return -ENOMEM;
9084799ac81SBoris Pismenny 	buf = orig_buf;
9094799ac81SBoris Pismenny 
9108b3c59a7SJakub Kicinski 	err = tls_strp_msg_cow(sw_ctx);
9118b3c59a7SJakub Kicinski 	if (unlikely(err))
9124799ac81SBoris Pismenny 		goto free_buf;
9138b3c59a7SJakub Kicinski 
9148b3c59a7SJakub Kicinski 	skb = tls_strp_msg(sw_ctx);
9158b3c59a7SJakub Kicinski 	rxm = strp_msg(skb);
9168b3c59a7SJakub Kicinski 	offset = rxm->offset;
9174799ac81SBoris Pismenny 
9184799ac81SBoris Pismenny 	sg_init_table(sg, 1);
9194799ac81SBoris Pismenny 	sg_set_buf(&sg[0], buf,
9208db44ab2SSabrina Dubroca 		   rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv);
9218db44ab2SSabrina Dubroca 	err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_desc->iv);
922aeb11ff0SJakub Kicinski 	if (err)
923aeb11ff0SJakub Kicinski 		goto free_buf;
9244799ac81SBoris Pismenny 
9254799ac81SBoris Pismenny 	/* We are interested only in the decrypted data not the auth */
926541cc48bSJakub Kicinski 	err = decrypt_skb(sk, sg);
9274799ac81SBoris Pismenny 	if (err != -EBADMSG)
9284799ac81SBoris Pismenny 		goto free_buf;
9294799ac81SBoris Pismenny 	else
9304799ac81SBoris Pismenny 		err = 0;
9314799ac81SBoris Pismenny 
9328db44ab2SSabrina Dubroca 	data_len = rxm->full_len - cipher_desc->tag;
933eb3d38d5SJakub Kicinski 
93497e1caa5SJakub Kicinski 	if (skb_pagelen(skb) > offset) {
935eb3d38d5SJakub Kicinski 		copy = min_t(int, skb_pagelen(skb) - offset, data_len);
9364799ac81SBoris Pismenny 
937aeb11ff0SJakub Kicinski 		if (skb->decrypted) {
938aeb11ff0SJakub Kicinski 			err = skb_store_bits(skb, offset, buf, copy);
939aeb11ff0SJakub Kicinski 			if (err)
940aeb11ff0SJakub Kicinski 				goto free_buf;
941aeb11ff0SJakub Kicinski 		}
9424799ac81SBoris Pismenny 
9434799ac81SBoris Pismenny 		offset += copy;
9444799ac81SBoris Pismenny 		buf += copy;
94597e1caa5SJakub Kicinski 	}
9464799ac81SBoris Pismenny 
947eb3d38d5SJakub Kicinski 	pos = skb_pagelen(skb);
9484799ac81SBoris Pismenny 	skb_walk_frags(skb, skb_iter) {
949eb3d38d5SJakub Kicinski 		int frag_pos;
950eb3d38d5SJakub Kicinski 
951eb3d38d5SJakub Kicinski 		/* Practically all frags must belong to msg if reencrypt
952eb3d38d5SJakub Kicinski 		 * is needed with current strparser and coalescing logic,
953eb3d38d5SJakub Kicinski 		 * but strparser may "get optimized", so let's be safe.
954eb3d38d5SJakub Kicinski 		 */
955eb3d38d5SJakub Kicinski 		if (pos + skb_iter->len <= offset)
956eb3d38d5SJakub Kicinski 			goto done_with_frag;
957eb3d38d5SJakub Kicinski 		if (pos >= data_len + rxm->offset)
958eb3d38d5SJakub Kicinski 			break;
959eb3d38d5SJakub Kicinski 
960eb3d38d5SJakub Kicinski 		frag_pos = offset - pos;
961eb3d38d5SJakub Kicinski 		copy = min_t(int, skb_iter->len - frag_pos,
962eb3d38d5SJakub Kicinski 			     data_len + rxm->offset - offset);
9634799ac81SBoris Pismenny 
964aeb11ff0SJakub Kicinski 		if (skb_iter->decrypted) {
965aeb11ff0SJakub Kicinski 			err = skb_store_bits(skb_iter, frag_pos, buf, copy);
966aeb11ff0SJakub Kicinski 			if (err)
967aeb11ff0SJakub Kicinski 				goto free_buf;
968aeb11ff0SJakub Kicinski 		}
9694799ac81SBoris Pismenny 
9704799ac81SBoris Pismenny 		offset += copy;
9714799ac81SBoris Pismenny 		buf += copy;
972eb3d38d5SJakub Kicinski done_with_frag:
973eb3d38d5SJakub Kicinski 		pos += skb_iter->len;
9744799ac81SBoris Pismenny 	}
9754799ac81SBoris Pismenny 
9764799ac81SBoris Pismenny free_buf:
9774799ac81SBoris Pismenny 	kfree(orig_buf);
9784799ac81SBoris Pismenny 	return err;
9794799ac81SBoris Pismenny }
9804799ac81SBoris Pismenny 
tls_device_decrypted(struct sock * sk,struct tls_context * tls_ctx)981541cc48bSJakub Kicinski int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
9824799ac81SBoris Pismenny {
9834799ac81SBoris Pismenny 	struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
984541cc48bSJakub Kicinski 	struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
985541cc48bSJakub Kicinski 	struct sk_buff *skb = tls_strp_msg(sw_ctx);
986541cc48bSJakub Kicinski 	struct strp_msg *rxm = strp_msg(skb);
987eca9bfafSJakub Kicinski 	int is_decrypted, is_encrypted;
9884799ac81SBoris Pismenny 
989eca9bfafSJakub Kicinski 	if (!tls_strp_msg_mixed_decrypted(sw_ctx)) {
990eca9bfafSJakub Kicinski 		is_decrypted = skb->decrypted;
991eca9bfafSJakub Kicinski 		is_encrypted = !is_decrypted;
992eca9bfafSJakub Kicinski 	} else {
993eca9bfafSJakub Kicinski 		is_decrypted = 0;
994eca9bfafSJakub Kicinski 		is_encrypted = 0;
9954799ac81SBoris Pismenny 	}
9964799ac81SBoris Pismenny 
9979ec1c6acSJakub Kicinski 	trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
9989ec1c6acSJakub Kicinski 				   tls_ctx->rx.rec_seq, rxm->full_len,
9999ec1c6acSJakub Kicinski 				   is_encrypted, is_decrypted);
10009ec1c6acSJakub Kicinski 
1001c55dcdd4SMaxim Mikityanskiy 	if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) {
1002c55dcdd4SMaxim Mikityanskiy 		if (likely(is_encrypted || is_decrypted))
100371471ca3SJakub Kicinski 			return is_decrypted;
1004c55dcdd4SMaxim Mikityanskiy 
1005c55dcdd4SMaxim Mikityanskiy 		/* After tls_device_down disables the offload, the next SKB will
1006c55dcdd4SMaxim Mikityanskiy 		 * likely have initial fragments decrypted, and final ones not
1007c55dcdd4SMaxim Mikityanskiy 		 * decrypted. We need to reencrypt that single SKB.
1008c55dcdd4SMaxim Mikityanskiy 		 */
1009ea7a9d88SGal Pressman 		return tls_device_reencrypt(sk, tls_ctx);
1010c55dcdd4SMaxim Mikityanskiy 	}
1011c55dcdd4SMaxim Mikityanskiy 
1012f953d33bSJakub Kicinski 	/* Return immediately if the record is either entirely plaintext or
10134799ac81SBoris Pismenny 	 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
10144799ac81SBoris Pismenny 	 * record.
10154799ac81SBoris Pismenny 	 */
1016f953d33bSJakub Kicinski 	if (is_decrypted) {
1017f953d33bSJakub Kicinski 		ctx->resync_nh_reset = 1;
101871471ca3SJakub Kicinski 		return is_decrypted;
1019f953d33bSJakub Kicinski 	}
1020f953d33bSJakub Kicinski 	if (is_encrypted) {
1021f953d33bSJakub Kicinski 		tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
1022f953d33bSJakub Kicinski 		return 0;
1023f953d33bSJakub Kicinski 	}
1024f953d33bSJakub Kicinski 
1025f953d33bSJakub Kicinski 	ctx->resync_nh_reset = 1;
1026ea7a9d88SGal Pressman 	return tls_device_reencrypt(sk, tls_ctx);
10274799ac81SBoris Pismenny }
10284799ac81SBoris Pismenny 
tls_device_attach(struct tls_context * ctx,struct sock * sk,struct net_device * netdev)10299e995797SJakub Kicinski static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
10309e995797SJakub Kicinski 			      struct net_device *netdev)
10319e995797SJakub Kicinski {
10329e995797SJakub Kicinski 	if (sk->sk_destruct != tls_device_sk_destruct) {
10339e995797SJakub Kicinski 		refcount_set(&ctx->refcount, 1);
10349e995797SJakub Kicinski 		dev_hold(netdev);
103594ce3b64SMaxim Mikityanskiy 		RCU_INIT_POINTER(ctx->netdev, netdev);
10369e995797SJakub Kicinski 		spin_lock_irq(&tls_device_lock);
10379e995797SJakub Kicinski 		list_add_tail(&ctx->list, &tls_device_list);
10389e995797SJakub Kicinski 		spin_unlock_irq(&tls_device_lock);
10399e995797SJakub Kicinski 
10409e995797SJakub Kicinski 		ctx->sk_destruct = sk->sk_destruct;
10418d5a49e9SJakub Kicinski 		smp_store_release(&sk->sk_destruct, tls_device_sk_destruct);
10429e995797SJakub Kicinski 	}
10439e995797SJakub Kicinski }
10449e995797SJakub Kicinski 
tls_set_device_offload(struct sock * sk,struct tls_context * ctx)1045e8f69799SIlya Lesokhin int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
1046e8f69799SIlya Lesokhin {
10474509de14SVakul Garg 	struct tls_context *tls_ctx = tls_get_ctx(sk);
10484509de14SVakul Garg 	struct tls_prot_info *prot = &tls_ctx->prot_info;
10498db44ab2SSabrina Dubroca 	const struct tls_cipher_desc *cipher_desc;
1050e8f69799SIlya Lesokhin 	struct tls_record_info *start_marker_record;
1051d80a1b9dSBoris Pismenny 	struct tls_offload_context_tx *offload_ctx;
1052e8f69799SIlya Lesokhin 	struct tls_crypto_info *crypto_info;
1053e8f69799SIlya Lesokhin 	struct net_device *netdev;
1054e8f69799SIlya Lesokhin 	char *iv, *rec_seq;
1055e8f69799SIlya Lesokhin 	struct sk_buff *skb;
1056e8f69799SIlya Lesokhin 	__be64 rcd_sn;
105790962b48SJakub Kicinski 	int rc;
1058e8f69799SIlya Lesokhin 
1059e8f69799SIlya Lesokhin 	if (!ctx)
106090962b48SJakub Kicinski 		return -EINVAL;
1061e8f69799SIlya Lesokhin 
106290962b48SJakub Kicinski 	if (ctx->priv_ctx_tx)
106390962b48SJakub Kicinski 		return -EEXIST;
1064e8f69799SIlya Lesokhin 
1065b1a6f56bSZiyang Xuan 	netdev = get_netdev_for_sock(sk);
1066b1a6f56bSZiyang Xuan 	if (!netdev) {
1067b1a6f56bSZiyang Xuan 		pr_err_ratelimited("%s: netdev not found\n", __func__);
1068b1a6f56bSZiyang Xuan 		return -EINVAL;
1069b1a6f56bSZiyang Xuan 	}
1070e8f69799SIlya Lesokhin 
1071b1a6f56bSZiyang Xuan 	if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
1072b1a6f56bSZiyang Xuan 		rc = -EOPNOTSUPP;
1073b1a6f56bSZiyang Xuan 		goto release_netdev;
1074e8f69799SIlya Lesokhin 	}
1075e8f69799SIlya Lesokhin 
107686029d10SSabrina Dubroca 	crypto_info = &ctx->crypto_send.info;
1077618bac45SJakub Kicinski 	if (crypto_info->version != TLS_1_2_VERSION) {
1078618bac45SJakub Kicinski 		rc = -EOPNOTSUPP;
1079b1a6f56bSZiyang Xuan 		goto release_netdev;
1080618bac45SJakub Kicinski 	}
1081618bac45SJakub Kicinski 
10828db44ab2SSabrina Dubroca 	cipher_desc = get_cipher_desc(crypto_info->cipher_type);
1083*3524dd4dSSabrina Dubroca 	if (!cipher_desc || !cipher_desc->offloadable) {
108489fec474SJakub Kicinski 		rc = -EINVAL;
1085b1a6f56bSZiyang Xuan 		goto release_netdev;
108689fec474SJakub Kicinski 	}
108789fec474SJakub Kicinski 
1088*3524dd4dSSabrina Dubroca 	iv = crypto_info_iv(crypto_info, cipher_desc);
1089*3524dd4dSSabrina Dubroca 	rec_seq = crypto_info_rec_seq(crypto_info, cipher_desc);
1090*3524dd4dSSabrina Dubroca 
1091ab232e61SJakub Kicinski 	prot->version = crypto_info->version;
1092ab232e61SJakub Kicinski 	prot->cipher_type = crypto_info->cipher_type;
10938db44ab2SSabrina Dubroca 	prot->prepend_size = TLS_HEADER_SIZE + cipher_desc->iv;
10948db44ab2SSabrina Dubroca 	prot->tag_size = cipher_desc->tag;
10954509de14SVakul Garg 	prot->overhead_size = prot->prepend_size + prot->tag_size;
10968db44ab2SSabrina Dubroca 	prot->iv_size = cipher_desc->iv;
10978db44ab2SSabrina Dubroca 	prot->salt_size = cipher_desc->salt;
10988db44ab2SSabrina Dubroca 	ctx->tx.iv = kmalloc(cipher_desc->iv + cipher_desc->salt, GFP_KERNEL);
1099e8f69799SIlya Lesokhin 	if (!ctx->tx.iv) {
1100e8f69799SIlya Lesokhin 		rc = -ENOMEM;
1101b1a6f56bSZiyang Xuan 		goto release_netdev;
1102e8f69799SIlya Lesokhin 	}
1103e8f69799SIlya Lesokhin 
11048db44ab2SSabrina Dubroca 	memcpy(ctx->tx.iv + cipher_desc->salt, iv, cipher_desc->iv);
1105e8f69799SIlya Lesokhin 
11068db44ab2SSabrina Dubroca 	prot->rec_seq_size = cipher_desc->rec_seq;
11078db44ab2SSabrina Dubroca 	ctx->tx.rec_seq = kmemdup(rec_seq, cipher_desc->rec_seq, GFP_KERNEL);
1108e8f69799SIlya Lesokhin 	if (!ctx->tx.rec_seq) {
1109e8f69799SIlya Lesokhin 		rc = -ENOMEM;
1110e8f69799SIlya Lesokhin 		goto free_iv;
1111e8f69799SIlya Lesokhin 	}
1112e8f69799SIlya Lesokhin 
1113b1a6f56bSZiyang Xuan 	start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
1114b1a6f56bSZiyang Xuan 	if (!start_marker_record) {
1115b1a6f56bSZiyang Xuan 		rc = -ENOMEM;
1116b1a6f56bSZiyang Xuan 		goto free_rec_seq;
1117b1a6f56bSZiyang Xuan 	}
1118b1a6f56bSZiyang Xuan 
1119b1a6f56bSZiyang Xuan 	offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL);
1120b1a6f56bSZiyang Xuan 	if (!offload_ctx) {
1121b1a6f56bSZiyang Xuan 		rc = -ENOMEM;
1122b1a6f56bSZiyang Xuan 		goto free_marker_record;
1123b1a6f56bSZiyang Xuan 	}
1124b1a6f56bSZiyang Xuan 
1125e8f69799SIlya Lesokhin 	rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
1126e8f69799SIlya Lesokhin 	if (rc)
1127b1a6f56bSZiyang Xuan 		goto free_offload_ctx;
1128e8f69799SIlya Lesokhin 
1129e8f69799SIlya Lesokhin 	/* start at rec_seq - 1 to account for the start marker record */
1130e8f69799SIlya Lesokhin 	memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
1131e8f69799SIlya Lesokhin 	offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
1132e8f69799SIlya Lesokhin 
1133e8f69799SIlya Lesokhin 	start_marker_record->end_seq = tcp_sk(sk)->write_seq;
1134e8f69799SIlya Lesokhin 	start_marker_record->len = 0;
1135e8f69799SIlya Lesokhin 	start_marker_record->num_frags = 0;
1136e8f69799SIlya Lesokhin 
11377adc91e0STariq Toukan 	INIT_WORK(&offload_ctx->destruct_work, tls_device_tx_del_task);
11387adc91e0STariq Toukan 	offload_ctx->ctx = ctx;
11397adc91e0STariq Toukan 
1140e8f69799SIlya Lesokhin 	INIT_LIST_HEAD(&offload_ctx->records_list);
1141e8f69799SIlya Lesokhin 	list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
1142e8f69799SIlya Lesokhin 	spin_lock_init(&offload_ctx->lock);
1143895262d8SBoris Pismenny 	sg_init_table(offload_ctx->sg_tx_data,
1144895262d8SBoris Pismenny 		      ARRAY_SIZE(offload_ctx->sg_tx_data));
1145e8f69799SIlya Lesokhin 
1146e8f69799SIlya Lesokhin 	clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
1147e8f69799SIlya Lesokhin 	ctx->push_pending_record = tls_device_push_pending_record;
1148e8f69799SIlya Lesokhin 
1149e8f69799SIlya Lesokhin 	/* TLS offload is greatly simplified if we don't send
1150e8f69799SIlya Lesokhin 	 * SKBs where only part of the payload needs to be encrypted.
1151e8f69799SIlya Lesokhin 	 * So mark the last skb in the write queue as end of record.
1152e8f69799SIlya Lesokhin 	 */
1153e8f69799SIlya Lesokhin 	skb = tcp_write_queue_tail(sk);
1154e8f69799SIlya Lesokhin 	if (skb)
1155e8f69799SIlya Lesokhin 		TCP_SKB_CB(skb)->eor = 1;
1156e8f69799SIlya Lesokhin 
1157e8f69799SIlya Lesokhin 	/* Avoid offloading if the device is down
1158e8f69799SIlya Lesokhin 	 * We don't want to offload new flows after
1159e8f69799SIlya Lesokhin 	 * the NETDEV_DOWN event
11603544c98aSJakub Kicinski 	 *
11613544c98aSJakub Kicinski 	 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
11623544c98aSJakub Kicinski 	 * handler thus protecting from the device going down before
11633544c98aSJakub Kicinski 	 * ctx was added to tls_device_list.
1164e8f69799SIlya Lesokhin 	 */
11653544c98aSJakub Kicinski 	down_read(&device_offload_lock);
1166e8f69799SIlya Lesokhin 	if (!(netdev->flags & IFF_UP)) {
1167e8f69799SIlya Lesokhin 		rc = -EINVAL;
11683544c98aSJakub Kicinski 		goto release_lock;
1169e8f69799SIlya Lesokhin 	}
1170e8f69799SIlya Lesokhin 
1171e8f69799SIlya Lesokhin 	ctx->priv_ctx_tx = offload_ctx;
1172e8f69799SIlya Lesokhin 	rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
117386029d10SSabrina Dubroca 					     &ctx->crypto_send.info,
1174e8f69799SIlya Lesokhin 					     tcp_sk(sk)->write_seq);
11758538d29cSJakub Kicinski 	trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX,
11768538d29cSJakub Kicinski 				     tcp_sk(sk)->write_seq, rec_seq, rc);
1177e8f69799SIlya Lesokhin 	if (rc)
11783544c98aSJakub Kicinski 		goto release_lock;
1179e8f69799SIlya Lesokhin 
11804799ac81SBoris Pismenny 	tls_device_attach(ctx, sk, netdev);
11813544c98aSJakub Kicinski 	up_read(&device_offload_lock);
1182e8f69799SIlya Lesokhin 
1183ed3c9a2fSJakub Kicinski 	/* following this assignment tls_is_skb_tx_device_offloaded
1184e8f69799SIlya Lesokhin 	 * will return true and the context might be accessed
1185e8f69799SIlya Lesokhin 	 * by the netdev's xmit function.
1186e8f69799SIlya Lesokhin 	 */
11874799ac81SBoris Pismenny 	smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
11884799ac81SBoris Pismenny 	dev_put(netdev);
118990962b48SJakub Kicinski 
119090962b48SJakub Kicinski 	return 0;
1191e8f69799SIlya Lesokhin 
1192e8f69799SIlya Lesokhin release_lock:
1193e8f69799SIlya Lesokhin 	up_read(&device_offload_lock);
1194e8f69799SIlya Lesokhin 	clean_acked_data_disable(inet_csk(sk));
1195e8f69799SIlya Lesokhin 	crypto_free_aead(offload_ctx->aead_send);
1196e8f69799SIlya Lesokhin free_offload_ctx:
1197e8f69799SIlya Lesokhin 	kfree(offload_ctx);
1198e8f69799SIlya Lesokhin 	ctx->priv_ctx_tx = NULL;
1199e8f69799SIlya Lesokhin free_marker_record:
1200e8f69799SIlya Lesokhin 	kfree(start_marker_record);
1201b1a6f56bSZiyang Xuan free_rec_seq:
1202b1a6f56bSZiyang Xuan 	kfree(ctx->tx.rec_seq);
1203b1a6f56bSZiyang Xuan free_iv:
1204b1a6f56bSZiyang Xuan 	kfree(ctx->tx.iv);
1205b1a6f56bSZiyang Xuan release_netdev:
1206b1a6f56bSZiyang Xuan 	dev_put(netdev);
1207e8f69799SIlya Lesokhin 	return rc;
1208e8f69799SIlya Lesokhin }
1209e8f69799SIlya Lesokhin 
tls_set_device_offload_rx(struct sock * sk,struct tls_context * ctx)12104799ac81SBoris Pismenny int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
12114799ac81SBoris Pismenny {
12128538d29cSJakub Kicinski 	struct tls12_crypto_info_aes_gcm_128 *info;
12134799ac81SBoris Pismenny 	struct tls_offload_context_rx *context;
12144799ac81SBoris Pismenny 	struct net_device *netdev;
12154799ac81SBoris Pismenny 	int rc = 0;
12164799ac81SBoris Pismenny 
1217618bac45SJakub Kicinski 	if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
1218618bac45SJakub Kicinski 		return -EOPNOTSUPP;
1219618bac45SJakub Kicinski 
12204799ac81SBoris Pismenny 	netdev = get_netdev_for_sock(sk);
12214799ac81SBoris Pismenny 	if (!netdev) {
12224799ac81SBoris Pismenny 		pr_err_ratelimited("%s: netdev not found\n", __func__);
12233544c98aSJakub Kicinski 		return -EINVAL;
12244799ac81SBoris Pismenny 	}
12254799ac81SBoris Pismenny 
12264799ac81SBoris Pismenny 	if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
12274a5cdc60SValentin Vidic 		rc = -EOPNOTSUPP;
12284799ac81SBoris Pismenny 		goto release_netdev;
12294799ac81SBoris Pismenny 	}
12304799ac81SBoris Pismenny 
12314799ac81SBoris Pismenny 	/* Avoid offloading if the device is down
12324799ac81SBoris Pismenny 	 * We don't want to offload new flows after
12334799ac81SBoris Pismenny 	 * the NETDEV_DOWN event
12343544c98aSJakub Kicinski 	 *
12353544c98aSJakub Kicinski 	 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
12363544c98aSJakub Kicinski 	 * handler thus protecting from the device going down before
12373544c98aSJakub Kicinski 	 * ctx was added to tls_device_list.
12384799ac81SBoris Pismenny 	 */
12393544c98aSJakub Kicinski 	down_read(&device_offload_lock);
12404799ac81SBoris Pismenny 	if (!(netdev->flags & IFF_UP)) {
12414799ac81SBoris Pismenny 		rc = -EINVAL;
12423544c98aSJakub Kicinski 		goto release_lock;
12434799ac81SBoris Pismenny 	}
12444799ac81SBoris Pismenny 
12454799ac81SBoris Pismenny 	context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
12464799ac81SBoris Pismenny 	if (!context) {
12474799ac81SBoris Pismenny 		rc = -ENOMEM;
12483544c98aSJakub Kicinski 		goto release_lock;
12494799ac81SBoris Pismenny 	}
1250f953d33bSJakub Kicinski 	context->resync_nh_reset = 1;
12514799ac81SBoris Pismenny 
12524799ac81SBoris Pismenny 	ctx->priv_ctx_rx = context;
12534799ac81SBoris Pismenny 	rc = tls_set_sw_offload(sk, ctx, 0);
12544799ac81SBoris Pismenny 	if (rc)
12554799ac81SBoris Pismenny 		goto release_ctx;
12564799ac81SBoris Pismenny 
12574799ac81SBoris Pismenny 	rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
125886029d10SSabrina Dubroca 					     &ctx->crypto_recv.info,
12594799ac81SBoris Pismenny 					     tcp_sk(sk)->copied_seq);
12608538d29cSJakub Kicinski 	info = (void *)&ctx->crypto_recv.info;
12618538d29cSJakub Kicinski 	trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX,
12628538d29cSJakub Kicinski 				     tcp_sk(sk)->copied_seq, info->rec_seq, rc);
1263e49d268dSJakub Kicinski 	if (rc)
12644799ac81SBoris Pismenny 		goto free_sw_resources;
12654799ac81SBoris Pismenny 
12664799ac81SBoris Pismenny 	tls_device_attach(ctx, sk, netdev);
126790962b48SJakub Kicinski 	up_read(&device_offload_lock);
126890962b48SJakub Kicinski 
126990962b48SJakub Kicinski 	dev_put(netdev);
127090962b48SJakub Kicinski 
127190962b48SJakub Kicinski 	return 0;
12724799ac81SBoris Pismenny 
12734799ac81SBoris Pismenny free_sw_resources:
127462ef81d5SJakub Kicinski 	up_read(&device_offload_lock);
12754799ac81SBoris Pismenny 	tls_sw_free_resources_rx(sk);
127662ef81d5SJakub Kicinski 	down_read(&device_offload_lock);
12774799ac81SBoris Pismenny release_ctx:
12784799ac81SBoris Pismenny 	ctx->priv_ctx_rx = NULL;
12794799ac81SBoris Pismenny release_lock:
12804799ac81SBoris Pismenny 	up_read(&device_offload_lock);
12813544c98aSJakub Kicinski release_netdev:
12823544c98aSJakub Kicinski 	dev_put(netdev);
12834799ac81SBoris Pismenny 	return rc;
12844799ac81SBoris Pismenny }
12854799ac81SBoris Pismenny 
tls_device_offload_cleanup_rx(struct sock * sk)12864799ac81SBoris Pismenny void tls_device_offload_cleanup_rx(struct sock *sk)
12874799ac81SBoris Pismenny {
12884799ac81SBoris Pismenny 	struct tls_context *tls_ctx = tls_get_ctx(sk);
12894799ac81SBoris Pismenny 	struct net_device *netdev;
12904799ac81SBoris Pismenny 
12914799ac81SBoris Pismenny 	down_read(&device_offload_lock);
129294ce3b64SMaxim Mikityanskiy 	netdev = rcu_dereference_protected(tls_ctx->netdev,
129394ce3b64SMaxim Mikityanskiy 					   lockdep_is_held(&device_offload_lock));
12944799ac81SBoris Pismenny 	if (!netdev)
12954799ac81SBoris Pismenny 		goto out;
12964799ac81SBoris Pismenny 
12974799ac81SBoris Pismenny 	netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
12984799ac81SBoris Pismenny 					TLS_OFFLOAD_CTX_DIR_RX);
12994799ac81SBoris Pismenny 
13004799ac81SBoris Pismenny 	if (tls_ctx->tx_conf != TLS_HW) {
13014799ac81SBoris Pismenny 		dev_put(netdev);
130294ce3b64SMaxim Mikityanskiy 		rcu_assign_pointer(tls_ctx->netdev, NULL);
1303025cc2fbSMaxim Mikityanskiy 	} else {
1304025cc2fbSMaxim Mikityanskiy 		set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
13054799ac81SBoris Pismenny 	}
13064799ac81SBoris Pismenny out:
13074799ac81SBoris Pismenny 	up_read(&device_offload_lock);
13084799ac81SBoris Pismenny 	tls_sw_release_resources_rx(sk);
13094799ac81SBoris Pismenny }
13104799ac81SBoris Pismenny 
tls_device_down(struct net_device * netdev)1311e8f69799SIlya Lesokhin static int tls_device_down(struct net_device *netdev)
1312e8f69799SIlya Lesokhin {
1313e8f69799SIlya Lesokhin 	struct tls_context *ctx, *tmp;
1314e8f69799SIlya Lesokhin 	unsigned long flags;
1315e8f69799SIlya Lesokhin 	LIST_HEAD(list);
1316e8f69799SIlya Lesokhin 
1317e8f69799SIlya Lesokhin 	/* Request a write lock to block new offload attempts */
1318e8f69799SIlya Lesokhin 	down_write(&device_offload_lock);
1319e8f69799SIlya Lesokhin 
1320e8f69799SIlya Lesokhin 	spin_lock_irqsave(&tls_device_lock, flags);
1321e8f69799SIlya Lesokhin 	list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
132294ce3b64SMaxim Mikityanskiy 		struct net_device *ctx_netdev =
132394ce3b64SMaxim Mikityanskiy 			rcu_dereference_protected(ctx->netdev,
132494ce3b64SMaxim Mikityanskiy 						  lockdep_is_held(&device_offload_lock));
132594ce3b64SMaxim Mikityanskiy 
132694ce3b64SMaxim Mikityanskiy 		if (ctx_netdev != netdev ||
1327e8f69799SIlya Lesokhin 		    !refcount_inc_not_zero(&ctx->refcount))
1328e8f69799SIlya Lesokhin 			continue;
1329e8f69799SIlya Lesokhin 
1330e8f69799SIlya Lesokhin 		list_move(&ctx->list, &list);
1331e8f69799SIlya Lesokhin 	}
1332e8f69799SIlya Lesokhin 	spin_unlock_irqrestore(&tls_device_lock, flags);
1333e8f69799SIlya Lesokhin 
1334e8f69799SIlya Lesokhin 	list_for_each_entry_safe(ctx, tmp, &list, list)	{
1335c55dcdd4SMaxim Mikityanskiy 		/* Stop offloaded TX and switch to the fallback.
1336ed3c9a2fSJakub Kicinski 		 * tls_is_skb_tx_device_offloaded will return false.
1337c55dcdd4SMaxim Mikityanskiy 		 */
1338c55dcdd4SMaxim Mikityanskiy 		WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
1339c55dcdd4SMaxim Mikityanskiy 
1340c55dcdd4SMaxim Mikityanskiy 		/* Stop the RX and TX resync.
1341c55dcdd4SMaxim Mikityanskiy 		 * tls_dev_resync must not be called after tls_dev_del.
1342c55dcdd4SMaxim Mikityanskiy 		 */
134394ce3b64SMaxim Mikityanskiy 		rcu_assign_pointer(ctx->netdev, NULL);
1344c55dcdd4SMaxim Mikityanskiy 
1345c55dcdd4SMaxim Mikityanskiy 		/* Start skipping the RX resync logic completely. */
1346c55dcdd4SMaxim Mikityanskiy 		set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags);
1347c55dcdd4SMaxim Mikityanskiy 
1348c55dcdd4SMaxim Mikityanskiy 		/* Sync with inflight packets. After this point:
1349c55dcdd4SMaxim Mikityanskiy 		 * TX: no non-encrypted packets will be passed to the driver.
1350c55dcdd4SMaxim Mikityanskiy 		 * RX: resync requests from the driver will be ignored.
1351c55dcdd4SMaxim Mikityanskiy 		 */
1352c55dcdd4SMaxim Mikityanskiy 		synchronize_net();
1353c55dcdd4SMaxim Mikityanskiy 
1354c55dcdd4SMaxim Mikityanskiy 		/* Release the offload context on the driver side. */
13554799ac81SBoris Pismenny 		if (ctx->tx_conf == TLS_HW)
1356e8f69799SIlya Lesokhin 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1357e8f69799SIlya Lesokhin 							TLS_OFFLOAD_CTX_DIR_TX);
1358025cc2fbSMaxim Mikityanskiy 		if (ctx->rx_conf == TLS_HW &&
1359025cc2fbSMaxim Mikityanskiy 		    !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
13604799ac81SBoris Pismenny 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
13614799ac81SBoris Pismenny 							TLS_OFFLOAD_CTX_DIR_RX);
1362e8f69799SIlya Lesokhin 
1363c55dcdd4SMaxim Mikityanskiy 		dev_put(netdev);
1364c55dcdd4SMaxim Mikityanskiy 
1365c55dcdd4SMaxim Mikityanskiy 		/* Move the context to a separate list for two reasons:
1366c55dcdd4SMaxim Mikityanskiy 		 * 1. When the context is deallocated, list_del is called.
1367c55dcdd4SMaxim Mikityanskiy 		 * 2. It's no longer an offloaded context, so we don't want to
1368c55dcdd4SMaxim Mikityanskiy 		 *    run offload-specific code on this context.
1369c55dcdd4SMaxim Mikityanskiy 		 */
1370c55dcdd4SMaxim Mikityanskiy 		spin_lock_irqsave(&tls_device_lock, flags);
1371c55dcdd4SMaxim Mikityanskiy 		list_move_tail(&ctx->list, &tls_device_down_list);
1372c55dcdd4SMaxim Mikityanskiy 		spin_unlock_irqrestore(&tls_device_lock, flags);
1373c55dcdd4SMaxim Mikityanskiy 
1374c55dcdd4SMaxim Mikityanskiy 		/* Device contexts for RX and TX will be freed in on sk_destruct
1375c55dcdd4SMaxim Mikityanskiy 		 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
13763740651bSMaxim Mikityanskiy 		 * Now release the ref taken above.
1377c55dcdd4SMaxim Mikityanskiy 		 */
1378f6336724SMaxim Mikityanskiy 		if (refcount_dec_and_test(&ctx->refcount)) {
1379f6336724SMaxim Mikityanskiy 			/* sk_destruct ran after tls_device_down took a ref, and
1380f6336724SMaxim Mikityanskiy 			 * it returned early. Complete the destruction here.
1381f6336724SMaxim Mikityanskiy 			 */
1382f6336724SMaxim Mikityanskiy 			list_del(&ctx->list);
13833740651bSMaxim Mikityanskiy 			tls_device_free_ctx(ctx);
1384e8f69799SIlya Lesokhin 		}
1385f6336724SMaxim Mikityanskiy 	}
1386e8f69799SIlya Lesokhin 
1387e8f69799SIlya Lesokhin 	up_write(&device_offload_lock);
1388e8f69799SIlya Lesokhin 
13897adc91e0STariq Toukan 	flush_workqueue(destruct_wq);
1390e8f69799SIlya Lesokhin 
1391e8f69799SIlya Lesokhin 	return NOTIFY_DONE;
1392e8f69799SIlya Lesokhin }
1393e8f69799SIlya Lesokhin 
tls_dev_event(struct notifier_block * this,unsigned long event,void * ptr)1394e8f69799SIlya Lesokhin static int tls_dev_event(struct notifier_block *this, unsigned long event,
1395e8f69799SIlya Lesokhin 			 void *ptr)
1396e8f69799SIlya Lesokhin {
1397e8f69799SIlya Lesokhin 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1398e8f69799SIlya Lesokhin 
1399c3f4a6c3SJakub Kicinski 	if (!dev->tlsdev_ops &&
1400c3f4a6c3SJakub Kicinski 	    !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
1401e8f69799SIlya Lesokhin 		return NOTIFY_DONE;
1402e8f69799SIlya Lesokhin 
1403e8f69799SIlya Lesokhin 	switch (event) {
1404e8f69799SIlya Lesokhin 	case NETDEV_REGISTER:
1405e8f69799SIlya Lesokhin 	case NETDEV_FEAT_CHANGE:
14064e5a7332STariq Toukan 		if (netif_is_bond_master(dev))
14074e5a7332STariq Toukan 			return NOTIFY_DONE;
14084799ac81SBoris Pismenny 		if ((dev->features & NETIF_F_HW_TLS_RX) &&
1409eeb2efafSJakub Kicinski 		    !dev->tlsdev_ops->tls_dev_resync)
14104799ac81SBoris Pismenny 			return NOTIFY_BAD;
14114799ac81SBoris Pismenny 
1412e8f69799SIlya Lesokhin 		if  (dev->tlsdev_ops &&
1413e8f69799SIlya Lesokhin 		     dev->tlsdev_ops->tls_dev_add &&
1414e8f69799SIlya Lesokhin 		     dev->tlsdev_ops->tls_dev_del)
1415e8f69799SIlya Lesokhin 			return NOTIFY_DONE;
1416e8f69799SIlya Lesokhin 		else
1417e8f69799SIlya Lesokhin 			return NOTIFY_BAD;
1418e8f69799SIlya Lesokhin 	case NETDEV_DOWN:
1419e8f69799SIlya Lesokhin 		return tls_device_down(dev);
1420e8f69799SIlya Lesokhin 	}
1421e8f69799SIlya Lesokhin 	return NOTIFY_DONE;
1422e8f69799SIlya Lesokhin }
1423e8f69799SIlya Lesokhin 
1424e8f69799SIlya Lesokhin static struct notifier_block tls_dev_notifier = {
1425e8f69799SIlya Lesokhin 	.notifier_call	= tls_dev_event,
1426e8f69799SIlya Lesokhin };
1427e8f69799SIlya Lesokhin 
tls_device_init(void)14283d8c51b2STariq Toukan int __init tls_device_init(void)
1429e8f69799SIlya Lesokhin {
14307adc91e0STariq Toukan 	int err;
14317adc91e0STariq Toukan 
14326b47808fSJakub Kicinski 	dummy_page = alloc_page(GFP_KERNEL);
14336b47808fSJakub Kicinski 	if (!dummy_page)
14347adc91e0STariq Toukan 		return -ENOMEM;
14357adc91e0STariq Toukan 
14366b47808fSJakub Kicinski 	destruct_wq = alloc_workqueue("ktls_device_destruct", 0, 0);
14376b47808fSJakub Kicinski 	if (!destruct_wq) {
14386b47808fSJakub Kicinski 		err = -ENOMEM;
14396b47808fSJakub Kicinski 		goto err_free_dummy;
14406b47808fSJakub Kicinski 	}
14416b47808fSJakub Kicinski 
14427adc91e0STariq Toukan 	err = register_netdevice_notifier(&tls_dev_notifier);
14437adc91e0STariq Toukan 	if (err)
14446b47808fSJakub Kicinski 		goto err_destroy_wq;
14457adc91e0STariq Toukan 
14466b47808fSJakub Kicinski 	return 0;
14476b47808fSJakub Kicinski 
14486b47808fSJakub Kicinski err_destroy_wq:
14496b47808fSJakub Kicinski 	destroy_workqueue(destruct_wq);
14506b47808fSJakub Kicinski err_free_dummy:
14516b47808fSJakub Kicinski 	put_page(dummy_page);
14527adc91e0STariq Toukan 	return err;
1453e8f69799SIlya Lesokhin }
1454e8f69799SIlya Lesokhin 
tls_device_cleanup(void)1455e8f69799SIlya Lesokhin void __exit tls_device_cleanup(void)
1456e8f69799SIlya Lesokhin {
1457e8f69799SIlya Lesokhin 	unregister_netdevice_notifier(&tls_dev_notifier);
14587adc91e0STariq Toukan 	destroy_workqueue(destruct_wq);
1459494bc1d2SJakub Kicinski 	clean_acked_data_flush();
14606b47808fSJakub Kicinski 	put_page(dummy_page);
1461e8f69799SIlya Lesokhin }
1462