blob: 0f034c3bc37d7416371e74d82fccf6f50679d70f [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* Copyright (c) 2018, Mellanox Technologies All rights reserved.
2 *
3 * This software is available to you under a choice of one of two
4 * licenses. You may choose to be licensed under the terms of the GNU
5 * General Public License (GPL) Version 2, available from the file
6 * COPYING in the main directory of this source tree, or the
7 * OpenIB.org BSD license below:
8 *
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 *
13 * - Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer.
16 *
17 * - Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
30 */
31
32#include <crypto/aead.h>
33#include <linux/highmem.h>
34#include <linux/module.h>
35#include <linux/netdevice.h>
36#include <net/dst.h>
37#include <net/inet_connection_sock.h>
38#include <net/tcp.h>
39#include <net/tls.h>
40
41/* device_offload_lock is used to synchronize tls_dev_add
42 * against NETDEV_DOWN notifications.
43 */
44static DECLARE_RWSEM(device_offload_lock);
45
46static void tls_device_gc_task(struct work_struct *work);
47
48static DECLARE_WORK(tls_device_gc_work, tls_device_gc_task);
49static LIST_HEAD(tls_device_gc_list);
50static LIST_HEAD(tls_device_list);
51static DEFINE_SPINLOCK(tls_device_lock);
52
53static void tls_device_free_ctx(struct tls_context *ctx)
54{
David Brazdil0f672f62019-12-10 10:32:29 +000055 if (ctx->tx_conf == TLS_HW) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056 kfree(tls_offload_ctx_tx(ctx));
David Brazdil0f672f62019-12-10 10:32:29 +000057 kfree(ctx->tx.rec_seq);
58 kfree(ctx->tx.iv);
59 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060
61 if (ctx->rx_conf == TLS_HW)
62 kfree(tls_offload_ctx_rx(ctx));
63
David Brazdil0f672f62019-12-10 10:32:29 +000064 tls_ctx_free(NULL, ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065}
66
67static void tls_device_gc_task(struct work_struct *work)
68{
69 struct tls_context *ctx, *tmp;
70 unsigned long flags;
71 LIST_HEAD(gc_list);
72
73 spin_lock_irqsave(&tls_device_lock, flags);
74 list_splice_init(&tls_device_gc_list, &gc_list);
75 spin_unlock_irqrestore(&tls_device_lock, flags);
76
77 list_for_each_entry_safe(ctx, tmp, &gc_list, list) {
78 struct net_device *netdev = ctx->netdev;
79
80 if (netdev && ctx->tx_conf == TLS_HW) {
81 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
82 TLS_OFFLOAD_CTX_DIR_TX);
83 dev_put(netdev);
84 ctx->netdev = NULL;
85 }
86
87 list_del(&ctx->list);
88 tls_device_free_ctx(ctx);
89 }
90}
91
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
93{
94 unsigned long flags;
95
96 spin_lock_irqsave(&tls_device_lock, flags);
97 list_move_tail(&ctx->list, &tls_device_gc_list);
98
99 /* schedule_work inside the spinlock
100 * to make sure tls_device_down waits for that work.
101 */
102 schedule_work(&tls_device_gc_work);
103
104 spin_unlock_irqrestore(&tls_device_lock, flags);
105}
106
107/* We assume that the socket is already connected */
108static struct net_device *get_netdev_for_sock(struct sock *sk)
109{
110 struct dst_entry *dst = sk_dst_get(sk);
111 struct net_device *netdev = NULL;
112
113 if (likely(dst)) {
114 netdev = dst->dev;
115 dev_hold(netdev);
116 }
117
118 dst_release(dst);
119
120 return netdev;
121}
122
123static void destroy_record(struct tls_record_info *record)
124{
David Brazdil0f672f62019-12-10 10:32:29 +0000125 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126
David Brazdil0f672f62019-12-10 10:32:29 +0000127 for (i = 0; i < record->num_frags; i++)
128 __skb_frag_unref(&record->frags[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129 kfree(record);
130}
131
132static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
133{
134 struct tls_record_info *info, *temp;
135
136 list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
137 list_del(&info->list);
138 destroy_record(info);
139 }
140
141 offload_ctx->retransmit_hint = NULL;
142}
143
144static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
145{
146 struct tls_context *tls_ctx = tls_get_ctx(sk);
147 struct tls_record_info *info, *temp;
148 struct tls_offload_context_tx *ctx;
149 u64 deleted_records = 0;
150 unsigned long flags;
151
152 if (!tls_ctx)
153 return;
154
155 ctx = tls_offload_ctx_tx(tls_ctx);
156
157 spin_lock_irqsave(&ctx->lock, flags);
158 info = ctx->retransmit_hint;
David Brazdil0f672f62019-12-10 10:32:29 +0000159 if (info && !before(acked_seq, info->end_seq))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 ctx->retransmit_hint = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161
162 list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
163 if (before(acked_seq, info->end_seq))
164 break;
165 list_del(&info->list);
166
167 destroy_record(info);
168 deleted_records++;
169 }
170
171 ctx->unacked_record_sn += deleted_records;
172 spin_unlock_irqrestore(&ctx->lock, flags);
173}
174
175/* At this point, there should be no references on this
176 * socket and no in-flight SKBs associated with this
177 * socket, so it is safe to free all the resources.
178 */
David Brazdil0f672f62019-12-10 10:32:29 +0000179static void tls_device_sk_destruct(struct sock *sk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180{
181 struct tls_context *tls_ctx = tls_get_ctx(sk);
182 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
183
184 tls_ctx->sk_destruct(sk);
185
186 if (tls_ctx->tx_conf == TLS_HW) {
187 if (ctx->open_record)
188 destroy_record(ctx->open_record);
189 delete_all_records(ctx);
190 crypto_free_aead(ctx->aead_send);
191 clean_acked_data_disable(inet_csk(sk));
192 }
193
194 if (refcount_dec_and_test(&tls_ctx->refcount))
195 tls_device_queue_ctx_destruction(tls_ctx);
196}
David Brazdil0f672f62019-12-10 10:32:29 +0000197
198void tls_device_free_resources_tx(struct sock *sk)
199{
200 struct tls_context *tls_ctx = tls_get_ctx(sk);
201
202 tls_free_partial_record(sk, tls_ctx);
203}
204
205static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
206 u32 seq)
207{
208 struct net_device *netdev;
209 struct sk_buff *skb;
210 int err = 0;
211 u8 *rcd_sn;
212
213 skb = tcp_write_queue_tail(sk);
214 if (skb)
215 TCP_SKB_CB(skb)->eor = 1;
216
217 rcd_sn = tls_ctx->tx.rec_seq;
218
219 down_read(&device_offload_lock);
220 netdev = tls_ctx->netdev;
221 if (netdev)
222 err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
223 rcd_sn,
224 TLS_OFFLOAD_CTX_DIR_TX);
225 up_read(&device_offload_lock);
226 if (err)
227 return;
228
229 clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
230}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231
232static void tls_append_frag(struct tls_record_info *record,
233 struct page_frag *pfrag,
234 int size)
235{
236 skb_frag_t *frag;
237
238 frag = &record->frags[record->num_frags - 1];
David Brazdil0f672f62019-12-10 10:32:29 +0000239 if (skb_frag_page(frag) == pfrag->page &&
240 skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
241 skb_frag_size_add(frag, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242 } else {
243 ++frag;
David Brazdil0f672f62019-12-10 10:32:29 +0000244 __skb_frag_set_page(frag, pfrag->page);
245 skb_frag_off_set(frag, pfrag->offset);
246 skb_frag_size_set(frag, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 ++record->num_frags;
248 get_page(pfrag->page);
249 }
250
251 pfrag->offset += size;
252 record->len += size;
253}
254
255static int tls_push_record(struct sock *sk,
256 struct tls_context *ctx,
257 struct tls_offload_context_tx *offload_ctx,
258 struct tls_record_info *record,
David Brazdil0f672f62019-12-10 10:32:29 +0000259 int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260{
David Brazdil0f672f62019-12-10 10:32:29 +0000261 struct tls_prot_info *prot = &ctx->prot_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 struct tcp_sock *tp = tcp_sk(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263 skb_frag_t *frag;
264 int i;
265
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 record->end_seq = tp->write_seq + record->len;
David Brazdil0f672f62019-12-10 10:32:29 +0000267 list_add_tail_rcu(&record->list, &offload_ctx->records_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 offload_ctx->open_record = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000269
270 if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
271 tls_device_resync_tx(sk, ctx, tp->write_seq);
272
273 tls_advance_record_sn(sk, prot, &ctx->tx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274
275 for (i = 0; i < record->num_frags; i++) {
276 frag = &record->frags[i];
277 sg_unmark_end(&offload_ctx->sg_tx_data[i]);
278 sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
David Brazdil0f672f62019-12-10 10:32:29 +0000279 skb_frag_size(frag), skb_frag_off(frag));
280 sk_mem_charge(sk, skb_frag_size(frag));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 get_page(skb_frag_page(frag));
282 }
283 sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
284
285 /* all ready, send */
286 return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
287}
288
David Brazdil0f672f62019-12-10 10:32:29 +0000289static int tls_device_record_close(struct sock *sk,
290 struct tls_context *ctx,
291 struct tls_record_info *record,
292 struct page_frag *pfrag,
293 unsigned char record_type)
294{
295 struct tls_prot_info *prot = &ctx->prot_info;
296 int ret;
297
298 /* append tag
299 * device will fill in the tag, we just need to append a placeholder
300 * use socket memory to improve coalescing (re-using a single buffer
301 * increases frag count)
302 * if we can't allocate memory now, steal some back from data
303 */
304 if (likely(skb_page_frag_refill(prot->tag_size, pfrag,
305 sk->sk_allocation))) {
306 ret = 0;
307 tls_append_frag(record, pfrag, prot->tag_size);
308 } else {
309 ret = prot->tag_size;
310 if (record->len <= prot->overhead_size)
311 return -ENOMEM;
312 }
313
314 /* fill prepend */
315 tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
316 record->len - prot->overhead_size,
317 record_type, prot->version);
318 return ret;
319}
320
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
322 struct page_frag *pfrag,
323 size_t prepend_size)
324{
325 struct tls_record_info *record;
326 skb_frag_t *frag;
327
328 record = kmalloc(sizeof(*record), GFP_KERNEL);
329 if (!record)
330 return -ENOMEM;
331
332 frag = &record->frags[0];
333 __skb_frag_set_page(frag, pfrag->page);
David Brazdil0f672f62019-12-10 10:32:29 +0000334 skb_frag_off_set(frag, pfrag->offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335 skb_frag_size_set(frag, prepend_size);
336
337 get_page(pfrag->page);
338 pfrag->offset += prepend_size;
339
340 record->num_frags = 1;
341 record->len = prepend_size;
342 offload_ctx->open_record = record;
343 return 0;
344}
345
346static int tls_do_allocation(struct sock *sk,
347 struct tls_offload_context_tx *offload_ctx,
348 struct page_frag *pfrag,
349 size_t prepend_size)
350{
351 int ret;
352
353 if (!offload_ctx->open_record) {
354 if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
355 sk->sk_allocation))) {
356 sk->sk_prot->enter_memory_pressure(sk);
357 sk_stream_moderate_sndbuf(sk);
358 return -ENOMEM;
359 }
360
361 ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
362 if (ret)
363 return ret;
364
365 if (pfrag->size > pfrag->offset)
366 return 0;
367 }
368
369 if (!sk_page_frag_refill(sk, pfrag))
370 return -ENOMEM;
371
372 return 0;
373}
374
David Brazdil0f672f62019-12-10 10:32:29 +0000375static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
376{
377 size_t pre_copy, nocache;
378
379 pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
380 if (pre_copy) {
381 pre_copy = min(pre_copy, bytes);
382 if (copy_from_iter(addr, pre_copy, i) != pre_copy)
383 return -EFAULT;
384 bytes -= pre_copy;
385 addr += pre_copy;
386 }
387
388 nocache = round_down(bytes, SMP_CACHE_BYTES);
389 if (copy_from_iter_nocache(addr, nocache, i) != nocache)
390 return -EFAULT;
391 bytes -= nocache;
392 addr += nocache;
393
394 if (bytes && copy_from_iter(addr, bytes, i) != bytes)
395 return -EFAULT;
396
397 return 0;
398}
399
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400static int tls_push_data(struct sock *sk,
401 struct iov_iter *msg_iter,
402 size_t size, int flags,
403 unsigned char record_type)
404{
405 struct tls_context *tls_ctx = tls_get_ctx(sk);
David Brazdil0f672f62019-12-10 10:32:29 +0000406 struct tls_prot_info *prot = &tls_ctx->prot_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 struct tls_record_info *record = ctx->open_record;
David Brazdil0f672f62019-12-10 10:32:29 +0000409 int tls_push_record_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410 struct page_frag *pfrag;
411 size_t orig_size = size;
412 u32 max_open_record_len;
Olivier Deprez0e641232021-09-23 10:07:05 +0200413 bool more = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414 bool done = false;
Olivier Deprez0e641232021-09-23 10:07:05 +0200415 int copy, rc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416 long timeo;
417
418 if (flags &
419 ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST))
Olivier Deprez0e641232021-09-23 10:07:05 +0200420 return -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000421
422 if (sk->sk_err)
423 return -sk->sk_err;
424
David Brazdil0f672f62019-12-10 10:32:29 +0000425 flags |= MSG_SENDPAGE_DECRYPTED;
426 tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST;
427
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000428 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
David Brazdil0f672f62019-12-10 10:32:29 +0000429 if (tls_is_partially_sent_record(tls_ctx)) {
430 rc = tls_push_partial_record(sk, tls_ctx, flags);
431 if (rc < 0)
432 return rc;
433 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434
435 pfrag = sk_page_frag(sk);
436
437 /* TLS_HEADER_SIZE is not counted as part of the TLS record, and
438 * we need to leave room for an authentication tag.
439 */
440 max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
David Brazdil0f672f62019-12-10 10:32:29 +0000441 prot->prepend_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 do {
443 rc = tls_do_allocation(sk, ctx, pfrag,
David Brazdil0f672f62019-12-10 10:32:29 +0000444 prot->prepend_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445 if (rc) {
446 rc = sk_stream_wait_memory(sk, &timeo);
447 if (!rc)
448 continue;
449
450 record = ctx->open_record;
451 if (!record)
452 break;
453handle_error:
454 if (record_type != TLS_RECORD_TYPE_DATA) {
455 /* avoid sending partial
456 * record with type !=
457 * application_data
458 */
459 size = orig_size;
460 destroy_record(record);
461 ctx->open_record = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000462 } else if (record->len > prot->prepend_size) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463 goto last_record;
464 }
465
466 break;
467 }
468
469 record = ctx->open_record;
470 copy = min_t(size_t, size, (pfrag->size - pfrag->offset));
471 copy = min_t(size_t, copy, (max_open_record_len - record->len));
472
David Brazdil0f672f62019-12-10 10:32:29 +0000473 rc = tls_device_copy_data(page_address(pfrag->page) +
474 pfrag->offset, copy, msg_iter);
475 if (rc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476 goto handle_error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477 tls_append_frag(record, pfrag, copy);
478
479 size -= copy;
480 if (!size) {
481last_record:
482 tls_push_record_flags = flags;
Olivier Deprez0e641232021-09-23 10:07:05 +0200483 if (flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE)) {
484 more = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485 break;
486 }
487
488 done = true;
489 }
490
491 if (done || record->len >= max_open_record_len ||
492 (record->num_frags >= MAX_SKB_FRAGS - 1)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000493 rc = tls_device_record_close(sk, tls_ctx, record,
494 pfrag, record_type);
495 if (rc) {
496 if (rc > 0) {
497 size += rc;
498 } else {
499 size = orig_size;
500 destroy_record(record);
501 ctx->open_record = NULL;
502 break;
503 }
504 }
505
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506 rc = tls_push_record(sk,
507 tls_ctx,
508 ctx,
509 record,
David Brazdil0f672f62019-12-10 10:32:29 +0000510 tls_push_record_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511 if (rc < 0)
512 break;
513 }
514 } while (!done);
515
Olivier Deprez0e641232021-09-23 10:07:05 +0200516 tls_ctx->pending_open_record_frags = more;
517
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000518 if (orig_size - size > 0)
519 rc = orig_size - size;
520
521 return rc;
522}
523
524int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
525{
526 unsigned char record_type = TLS_RECORD_TYPE_DATA;
David Brazdil0f672f62019-12-10 10:32:29 +0000527 struct tls_context *tls_ctx = tls_get_ctx(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528 int rc;
529
David Brazdil0f672f62019-12-10 10:32:29 +0000530 mutex_lock(&tls_ctx->tx_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000531 lock_sock(sk);
532
533 if (unlikely(msg->msg_controllen)) {
534 rc = tls_proccess_cmsg(sk, msg, &record_type);
535 if (rc)
536 goto out;
537 }
538
539 rc = tls_push_data(sk, &msg->msg_iter, size,
540 msg->msg_flags, record_type);
541
542out:
543 release_sock(sk);
David Brazdil0f672f62019-12-10 10:32:29 +0000544 mutex_unlock(&tls_ctx->tx_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545 return rc;
546}
547
548int tls_device_sendpage(struct sock *sk, struct page *page,
549 int offset, size_t size, int flags)
550{
David Brazdil0f672f62019-12-10 10:32:29 +0000551 struct tls_context *tls_ctx = tls_get_ctx(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000552 struct iov_iter msg_iter;
Olivier Deprez0e641232021-09-23 10:07:05 +0200553 char *kaddr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000554 struct kvec iov;
555 int rc;
556
557 if (flags & MSG_SENDPAGE_NOTLAST)
558 flags |= MSG_MORE;
559
David Brazdil0f672f62019-12-10 10:32:29 +0000560 mutex_lock(&tls_ctx->tx_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000561 lock_sock(sk);
562
563 if (flags & MSG_OOB) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200564 rc = -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565 goto out;
566 }
567
Olivier Deprez0e641232021-09-23 10:07:05 +0200568 kaddr = kmap(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000569 iov.iov_base = kaddr + offset;
570 iov.iov_len = size;
David Brazdil0f672f62019-12-10 10:32:29 +0000571 iov_iter_kvec(&msg_iter, WRITE, &iov, 1, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572 rc = tls_push_data(sk, &msg_iter, size,
573 flags, TLS_RECORD_TYPE_DATA);
574 kunmap(page);
575
576out:
577 release_sock(sk);
David Brazdil0f672f62019-12-10 10:32:29 +0000578 mutex_unlock(&tls_ctx->tx_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 return rc;
580}
581
582struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
583 u32 seq, u64 *p_record_sn)
584{
585 u64 record_sn = context->hint_record_sn;
Olivier Deprez0e641232021-09-23 10:07:05 +0200586 struct tls_record_info *info, *last;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587
588 info = context->retransmit_hint;
589 if (!info ||
590 before(seq, info->end_seq - info->len)) {
591 /* if retransmit_hint is irrelevant start
592 * from the beggining of the list
593 */
David Brazdil0f672f62019-12-10 10:32:29 +0000594 info = list_first_entry_or_null(&context->records_list,
595 struct tls_record_info, list);
596 if (!info)
597 return NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200598 /* send the start_marker record if seq number is before the
599 * tls offload start marker sequence number. This record is
600 * required to handle TCP packets which are before TLS offload
601 * started.
602 * And if it's not start marker, look if this seq number
603 * belongs to the list.
604 */
605 if (likely(!tls_record_is_start_marker(info))) {
606 /* we have the first record, get the last record to see
607 * if this seq number belongs to the list.
608 */
609 last = list_last_entry(&context->records_list,
610 struct tls_record_info, list);
611
612 if (!between(seq, tls_record_start_seq(info),
613 last->end_seq))
614 return NULL;
615 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000616 record_sn = context->unacked_record_sn;
617 }
618
David Brazdil0f672f62019-12-10 10:32:29 +0000619 /* We just need the _rcu for the READ_ONCE() */
620 rcu_read_lock();
621 list_for_each_entry_from_rcu(info, &context->records_list, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622 if (before(seq, info->end_seq)) {
623 if (!context->retransmit_hint ||
624 after(info->end_seq,
625 context->retransmit_hint->end_seq)) {
626 context->hint_record_sn = record_sn;
627 context->retransmit_hint = info;
628 }
629 *p_record_sn = record_sn;
David Brazdil0f672f62019-12-10 10:32:29 +0000630 goto exit_rcu_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631 }
632 record_sn++;
633 }
David Brazdil0f672f62019-12-10 10:32:29 +0000634 info = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635
David Brazdil0f672f62019-12-10 10:32:29 +0000636exit_rcu_unlock:
637 rcu_read_unlock();
638 return info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639}
640EXPORT_SYMBOL(tls_get_record);
641
642static int tls_device_push_pending_record(struct sock *sk, int flags)
643{
644 struct iov_iter msg_iter;
645
David Brazdil0f672f62019-12-10 10:32:29 +0000646 iov_iter_kvec(&msg_iter, WRITE, NULL, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000647 return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA);
648}
649
David Brazdil0f672f62019-12-10 10:32:29 +0000650void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
651{
652 if (tls_is_partially_sent_record(ctx)) {
653 gfp_t sk_allocation = sk->sk_allocation;
654
655 WARN_ON_ONCE(sk->sk_write_pending);
656
657 sk->sk_allocation = GFP_ATOMIC;
658 tls_push_partial_record(sk, ctx,
659 MSG_DONTWAIT | MSG_NOSIGNAL |
660 MSG_SENDPAGE_DECRYPTED);
661 sk->sk_allocation = sk_allocation;
662 }
663}
664
665static void tls_device_resync_rx(struct tls_context *tls_ctx,
666 struct sock *sk, u32 seq, u8 *rcd_sn)
667{
668 struct net_device *netdev;
669
670 if (WARN_ON(test_and_set_bit(TLS_RX_SYNC_RUNNING, &tls_ctx->flags)))
671 return;
672 netdev = READ_ONCE(tls_ctx->netdev);
673 if (netdev)
674 netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
675 TLS_OFFLOAD_CTX_DIR_RX);
676 clear_bit_unlock(TLS_RX_SYNC_RUNNING, &tls_ctx->flags);
677}
678
679void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000680{
681 struct tls_context *tls_ctx = tls_get_ctx(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682 struct tls_offload_context_rx *rx_ctx;
David Brazdil0f672f62019-12-10 10:32:29 +0000683 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
684 struct tls_prot_info *prot;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685 u32 is_req_pending;
686 s64 resync_req;
687 u32 req_seq;
688
689 if (tls_ctx->rx_conf != TLS_HW)
690 return;
691
David Brazdil0f672f62019-12-10 10:32:29 +0000692 prot = &tls_ctx->prot_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693 rx_ctx = tls_offload_ctx_rx(tls_ctx);
David Brazdil0f672f62019-12-10 10:32:29 +0000694 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695
David Brazdil0f672f62019-12-10 10:32:29 +0000696 switch (rx_ctx->resync_type) {
697 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
698 resync_req = atomic64_read(&rx_ctx->resync_req);
699 req_seq = resync_req >> 32;
700 seq += TLS_HEADER_SIZE - 1;
701 is_req_pending = resync_req;
702
703 if (likely(!is_req_pending) || req_seq != seq ||
704 !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
705 return;
706 break;
707 case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
708 if (likely(!rx_ctx->resync_nh_do_now))
709 return;
710
711 /* head of next rec is already in, note that the sock_inq will
712 * include the currently parsed message when called from parser
713 */
714 if (tcp_inq(sk) > rcd_len)
715 return;
716
717 rx_ctx->resync_nh_do_now = 0;
718 seq += rcd_len;
719 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
720 break;
721 }
722
723 tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
724}
725
726static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
727 struct tls_offload_context_rx *ctx,
728 struct sock *sk, struct sk_buff *skb)
729{
730 struct strp_msg *rxm;
731
732 /* device will request resyncs by itself based on stream scan */
733 if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
734 return;
735 /* already scheduled */
736 if (ctx->resync_nh_do_now)
737 return;
738 /* seen decrypted fragments since last fully-failed record */
739 if (ctx->resync_nh_reset) {
740 ctx->resync_nh_reset = 0;
741 ctx->resync_nh.decrypted_failed = 1;
742 ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
743 return;
744 }
745
746 if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
747 return;
748
749 /* doing resync, bump the next target in case it fails */
750 if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
751 ctx->resync_nh.decrypted_tgt *= 2;
752 else
753 ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
754
755 rxm = strp_msg(skb);
756
757 /* head of next rec is already in, parser will sync for us */
758 if (tcp_inq(sk) > rxm->full_len) {
759 ctx->resync_nh_do_now = 1;
760 } else {
761 struct tls_prot_info *prot = &tls_ctx->prot_info;
762 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
763
764 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
765 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
766
767 tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
768 rcd_sn);
769 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770}
771
772static int tls_device_reencrypt(struct sock *sk, struct sk_buff *skb)
773{
774 struct strp_msg *rxm = strp_msg(skb);
David Brazdil0f672f62019-12-10 10:32:29 +0000775 int err = 0, offset = rxm->offset, copy, nsg, data_len, pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776 struct sk_buff *skb_iter, *unused;
777 struct scatterlist sg[1];
778 char *orig_buf, *buf;
779
780 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE +
781 TLS_CIPHER_AES_GCM_128_IV_SIZE, sk->sk_allocation);
782 if (!orig_buf)
783 return -ENOMEM;
784 buf = orig_buf;
785
786 nsg = skb_cow_data(skb, 0, &unused);
787 if (unlikely(nsg < 0)) {
788 err = nsg;
789 goto free_buf;
790 }
791
792 sg_init_table(sg, 1);
793 sg_set_buf(&sg[0], buf,
794 rxm->full_len + TLS_HEADER_SIZE +
795 TLS_CIPHER_AES_GCM_128_IV_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +0000796 err = skb_copy_bits(skb, offset, buf,
797 TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE);
798 if (err)
799 goto free_buf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800
801 /* We are interested only in the decrypted data not the auth */
802 err = decrypt_skb(sk, skb, sg);
803 if (err != -EBADMSG)
804 goto free_buf;
805 else
806 err = 0;
807
David Brazdil0f672f62019-12-10 10:32:29 +0000808 data_len = rxm->full_len - TLS_CIPHER_AES_GCM_128_TAG_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809
David Brazdil0f672f62019-12-10 10:32:29 +0000810 if (skb_pagelen(skb) > offset) {
811 copy = min_t(int, skb_pagelen(skb) - offset, data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812
David Brazdil0f672f62019-12-10 10:32:29 +0000813 if (skb->decrypted) {
814 err = skb_store_bits(skb, offset, buf, copy);
815 if (err)
816 goto free_buf;
817 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000818
819 offset += copy;
820 buf += copy;
821 }
822
David Brazdil0f672f62019-12-10 10:32:29 +0000823 pos = skb_pagelen(skb);
824 skb_walk_frags(skb, skb_iter) {
825 int frag_pos;
826
827 /* Practically all frags must belong to msg if reencrypt
828 * is needed with current strparser and coalescing logic,
829 * but strparser may "get optimized", so let's be safe.
830 */
831 if (pos + skb_iter->len <= offset)
832 goto done_with_frag;
833 if (pos >= data_len + rxm->offset)
834 break;
835
836 frag_pos = offset - pos;
837 copy = min_t(int, skb_iter->len - frag_pos,
838 data_len + rxm->offset - offset);
839
840 if (skb_iter->decrypted) {
841 err = skb_store_bits(skb_iter, frag_pos, buf, copy);
842 if (err)
843 goto free_buf;
844 }
845
846 offset += copy;
847 buf += copy;
848done_with_frag:
849 pos += skb_iter->len;
850 }
851
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000852free_buf:
853 kfree(orig_buf);
854 return err;
855}
856
857int tls_device_decrypted(struct sock *sk, struct sk_buff *skb)
858{
859 struct tls_context *tls_ctx = tls_get_ctx(sk);
860 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
861 int is_decrypted = skb->decrypted;
862 int is_encrypted = !is_decrypted;
863 struct sk_buff *skb_iter;
864
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000865 /* Check if all the data is decrypted already */
866 skb_walk_frags(skb, skb_iter) {
867 is_decrypted &= skb_iter->decrypted;
868 is_encrypted &= !skb_iter->decrypted;
869 }
870
871 ctx->sw.decrypted |= is_decrypted;
872
David Brazdil0f672f62019-12-10 10:32:29 +0000873 /* Return immediately if the record is either entirely plaintext or
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000874 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
875 * record.
876 */
David Brazdil0f672f62019-12-10 10:32:29 +0000877 if (is_decrypted) {
878 ctx->resync_nh_reset = 1;
879 return 0;
880 }
881 if (is_encrypted) {
882 tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
883 return 0;
884 }
885
886 ctx->resync_nh_reset = 1;
887 return tls_device_reencrypt(sk, skb);
888}
889
890static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
891 struct net_device *netdev)
892{
893 if (sk->sk_destruct != tls_device_sk_destruct) {
894 refcount_set(&ctx->refcount, 1);
895 dev_hold(netdev);
896 ctx->netdev = netdev;
897 spin_lock_irq(&tls_device_lock);
898 list_add_tail(&ctx->list, &tls_device_list);
899 spin_unlock_irq(&tls_device_lock);
900
901 ctx->sk_destruct = sk->sk_destruct;
902 sk->sk_destruct = tls_device_sk_destruct;
903 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000904}
905
906int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
907{
908 u16 nonce_size, tag_size, iv_size, rec_seq_size;
David Brazdil0f672f62019-12-10 10:32:29 +0000909 struct tls_context *tls_ctx = tls_get_ctx(sk);
910 struct tls_prot_info *prot = &tls_ctx->prot_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911 struct tls_record_info *start_marker_record;
912 struct tls_offload_context_tx *offload_ctx;
913 struct tls_crypto_info *crypto_info;
914 struct net_device *netdev;
915 char *iv, *rec_seq;
916 struct sk_buff *skb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000917 __be64 rcd_sn;
David Brazdil0f672f62019-12-10 10:32:29 +0000918 int rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919
920 if (!ctx)
David Brazdil0f672f62019-12-10 10:32:29 +0000921 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922
David Brazdil0f672f62019-12-10 10:32:29 +0000923 if (ctx->priv_ctx_tx)
924 return -EEXIST;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925
926 start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +0000927 if (!start_marker_record)
928 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929
930 offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL);
931 if (!offload_ctx) {
932 rc = -ENOMEM;
933 goto free_marker_record;
934 }
935
936 crypto_info = &ctx->crypto_send.info;
David Brazdil0f672f62019-12-10 10:32:29 +0000937 if (crypto_info->version != TLS_1_2_VERSION) {
938 rc = -EOPNOTSUPP;
939 goto free_offload_ctx;
940 }
941
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942 switch (crypto_info->cipher_type) {
943 case TLS_CIPHER_AES_GCM_128:
944 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
945 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
946 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
947 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
948 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
949 rec_seq =
950 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
951 break;
952 default:
953 rc = -EINVAL;
954 goto free_offload_ctx;
955 }
956
David Brazdil0f672f62019-12-10 10:32:29 +0000957 /* Sanity-check the rec_seq_size for stack allocations */
958 if (rec_seq_size > TLS_MAX_REC_SEQ_SIZE) {
959 rc = -EINVAL;
960 goto free_offload_ctx;
961 }
962
963 prot->version = crypto_info->version;
964 prot->cipher_type = crypto_info->cipher_type;
965 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
966 prot->tag_size = tag_size;
967 prot->overhead_size = prot->prepend_size + prot->tag_size;
968 prot->iv_size = iv_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000969 ctx->tx.iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
970 GFP_KERNEL);
971 if (!ctx->tx.iv) {
972 rc = -ENOMEM;
973 goto free_offload_ctx;
974 }
975
976 memcpy(ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
977
David Brazdil0f672f62019-12-10 10:32:29 +0000978 prot->rec_seq_size = rec_seq_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000979 ctx->tx.rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
980 if (!ctx->tx.rec_seq) {
981 rc = -ENOMEM;
982 goto free_iv;
983 }
984
985 rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
986 if (rc)
987 goto free_rec_seq;
988
989 /* start at rec_seq - 1 to account for the start marker record */
990 memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
991 offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
992
993 start_marker_record->end_seq = tcp_sk(sk)->write_seq;
994 start_marker_record->len = 0;
995 start_marker_record->num_frags = 0;
996
997 INIT_LIST_HEAD(&offload_ctx->records_list);
998 list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
999 spin_lock_init(&offload_ctx->lock);
1000 sg_init_table(offload_ctx->sg_tx_data,
1001 ARRAY_SIZE(offload_ctx->sg_tx_data));
1002
1003 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
1004 ctx->push_pending_record = tls_device_push_pending_record;
1005
1006 /* TLS offload is greatly simplified if we don't send
1007 * SKBs where only part of the payload needs to be encrypted.
1008 * So mark the last skb in the write queue as end of record.
1009 */
1010 skb = tcp_write_queue_tail(sk);
1011 if (skb)
1012 TCP_SKB_CB(skb)->eor = 1;
1013
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001014 netdev = get_netdev_for_sock(sk);
1015 if (!netdev) {
1016 pr_err_ratelimited("%s: netdev not found\n", __func__);
1017 rc = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001018 goto disable_cad;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019 }
1020
1021 if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001022 rc = -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001023 goto release_netdev;
1024 }
1025
1026 /* Avoid offloading if the device is down
1027 * We don't want to offload new flows after
1028 * the NETDEV_DOWN event
David Brazdil0f672f62019-12-10 10:32:29 +00001029 *
1030 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1031 * handler thus protecting from the device going down before
1032 * ctx was added to tls_device_list.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 */
David Brazdil0f672f62019-12-10 10:32:29 +00001034 down_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 if (!(netdev->flags & IFF_UP)) {
1036 rc = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001037 goto release_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 }
1039
1040 ctx->priv_ctx_tx = offload_ctx;
1041 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
1042 &ctx->crypto_send.info,
1043 tcp_sk(sk)->write_seq);
1044 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00001045 goto release_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001046
1047 tls_device_attach(ctx, sk, netdev);
David Brazdil0f672f62019-12-10 10:32:29 +00001048 up_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001049
1050 /* following this assignment tls_is_sk_tx_device_offloaded
1051 * will return true and the context might be accessed
1052 * by the netdev's xmit function.
1053 */
1054 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
1055 dev_put(netdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056
David Brazdil0f672f62019-12-10 10:32:29 +00001057 return 0;
1058
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001059release_lock:
1060 up_read(&device_offload_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001061release_netdev:
1062 dev_put(netdev);
1063disable_cad:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001064 clean_acked_data_disable(inet_csk(sk));
1065 crypto_free_aead(offload_ctx->aead_send);
1066free_rec_seq:
1067 kfree(ctx->tx.rec_seq);
1068free_iv:
1069 kfree(ctx->tx.iv);
1070free_offload_ctx:
1071 kfree(offload_ctx);
1072 ctx->priv_ctx_tx = NULL;
1073free_marker_record:
1074 kfree(start_marker_record);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001075 return rc;
1076}
1077
1078int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
1079{
1080 struct tls_offload_context_rx *context;
1081 struct net_device *netdev;
1082 int rc = 0;
1083
David Brazdil0f672f62019-12-10 10:32:29 +00001084 if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
1085 return -EOPNOTSUPP;
1086
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001087 netdev = get_netdev_for_sock(sk);
1088 if (!netdev) {
1089 pr_err_ratelimited("%s: netdev not found\n", __func__);
David Brazdil0f672f62019-12-10 10:32:29 +00001090 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001091 }
1092
1093 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001094 rc = -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095 goto release_netdev;
1096 }
1097
1098 /* Avoid offloading if the device is down
1099 * We don't want to offload new flows after
1100 * the NETDEV_DOWN event
David Brazdil0f672f62019-12-10 10:32:29 +00001101 *
1102 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1103 * handler thus protecting from the device going down before
1104 * ctx was added to tls_device_list.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001105 */
David Brazdil0f672f62019-12-10 10:32:29 +00001106 down_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107 if (!(netdev->flags & IFF_UP)) {
1108 rc = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001109 goto release_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001110 }
1111
1112 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
1113 if (!context) {
1114 rc = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00001115 goto release_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116 }
David Brazdil0f672f62019-12-10 10:32:29 +00001117 context->resync_nh_reset = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118
1119 ctx->priv_ctx_rx = context;
1120 rc = tls_set_sw_offload(sk, ctx, 0);
1121 if (rc)
1122 goto release_ctx;
1123
1124 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
1125 &ctx->crypto_recv.info,
1126 tcp_sk(sk)->copied_seq);
David Brazdil0f672f62019-12-10 10:32:29 +00001127 if (rc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001128 goto free_sw_resources;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129
1130 tls_device_attach(ctx, sk, netdev);
David Brazdil0f672f62019-12-10 10:32:29 +00001131 up_read(&device_offload_lock);
1132
1133 dev_put(netdev);
1134
1135 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001136
1137free_sw_resources:
David Brazdil0f672f62019-12-10 10:32:29 +00001138 up_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139 tls_sw_free_resources_rx(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00001140 down_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141release_ctx:
1142 ctx->priv_ctx_rx = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143release_lock:
1144 up_read(&device_offload_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001145release_netdev:
1146 dev_put(netdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001147 return rc;
1148}
1149
1150void tls_device_offload_cleanup_rx(struct sock *sk)
1151{
1152 struct tls_context *tls_ctx = tls_get_ctx(sk);
1153 struct net_device *netdev;
1154
1155 down_read(&device_offload_lock);
1156 netdev = tls_ctx->netdev;
1157 if (!netdev)
1158 goto out;
1159
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
1161 TLS_OFFLOAD_CTX_DIR_RX);
1162
1163 if (tls_ctx->tx_conf != TLS_HW) {
1164 dev_put(netdev);
1165 tls_ctx->netdev = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001166 } else {
1167 set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001168 }
1169out:
1170 up_read(&device_offload_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171 tls_sw_release_resources_rx(sk);
1172}
1173
1174static int tls_device_down(struct net_device *netdev)
1175{
1176 struct tls_context *ctx, *tmp;
1177 unsigned long flags;
1178 LIST_HEAD(list);
1179
1180 /* Request a write lock to block new offload attempts */
1181 down_write(&device_offload_lock);
1182
1183 spin_lock_irqsave(&tls_device_lock, flags);
1184 list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
1185 if (ctx->netdev != netdev ||
1186 !refcount_inc_not_zero(&ctx->refcount))
1187 continue;
1188
1189 list_move(&ctx->list, &list);
1190 }
1191 spin_unlock_irqrestore(&tls_device_lock, flags);
1192
1193 list_for_each_entry_safe(ctx, tmp, &list, list) {
1194 if (ctx->tx_conf == TLS_HW)
1195 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1196 TLS_OFFLOAD_CTX_DIR_TX);
Olivier Deprez0e641232021-09-23 10:07:05 +02001197 if (ctx->rx_conf == TLS_HW &&
1198 !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001199 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1200 TLS_OFFLOAD_CTX_DIR_RX);
David Brazdil0f672f62019-12-10 10:32:29 +00001201 WRITE_ONCE(ctx->netdev, NULL);
1202 smp_mb__before_atomic(); /* pairs with test_and_set_bit() */
1203 while (test_bit(TLS_RX_SYNC_RUNNING, &ctx->flags))
1204 usleep_range(10, 200);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001205 dev_put(netdev);
1206 list_del_init(&ctx->list);
1207
1208 if (refcount_dec_and_test(&ctx->refcount))
1209 tls_device_free_ctx(ctx);
1210 }
1211
1212 up_write(&device_offload_lock);
1213
1214 flush_work(&tls_device_gc_work);
1215
1216 return NOTIFY_DONE;
1217}
1218
1219static int tls_dev_event(struct notifier_block *this, unsigned long event,
1220 void *ptr)
1221{
1222 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1223
David Brazdil0f672f62019-12-10 10:32:29 +00001224 if (!dev->tlsdev_ops &&
1225 !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001226 return NOTIFY_DONE;
1227
1228 switch (event) {
1229 case NETDEV_REGISTER:
1230 case NETDEV_FEAT_CHANGE:
1231 if ((dev->features & NETIF_F_HW_TLS_RX) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001232 !dev->tlsdev_ops->tls_dev_resync)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001233 return NOTIFY_BAD;
1234
1235 if (dev->tlsdev_ops &&
1236 dev->tlsdev_ops->tls_dev_add &&
1237 dev->tlsdev_ops->tls_dev_del)
1238 return NOTIFY_DONE;
1239 else
1240 return NOTIFY_BAD;
1241 case NETDEV_DOWN:
1242 return tls_device_down(dev);
1243 }
1244 return NOTIFY_DONE;
1245}
1246
1247static struct notifier_block tls_dev_notifier = {
1248 .notifier_call = tls_dev_event,
1249};
1250
1251void __init tls_device_init(void)
1252{
1253 register_netdevice_notifier(&tls_dev_notifier);
1254}
1255
1256void __exit tls_device_cleanup(void)
1257{
1258 unregister_netdevice_notifier(&tls_dev_notifier);
1259 flush_work(&tls_device_gc_work);
David Brazdil0f672f62019-12-10 10:32:29 +00001260 clean_acked_data_flush();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001261}