1 /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
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:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
13 * - Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the following
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.
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
32 #include <crypto/aead.h>
33 #include <linux/highmem.h>
34 #include <linux/module.h>
35 #include <linux/netdevice.h>
37 #include <net/inet_connection_sock.h>
44 /* device_offload_lock is used to synchronize tls_dev_add
45 * against NETDEV_DOWN notifications.
47 static DECLARE_RWSEM(device_offload_lock);
49 static void tls_device_gc_task(struct work_struct *work);
51 static DECLARE_WORK(tls_device_gc_work, tls_device_gc_task);
52 static LIST_HEAD(tls_device_gc_list);
53 static LIST_HEAD(tls_device_list);
54 static LIST_HEAD(tls_device_down_list);
55 static DEFINE_SPINLOCK(tls_device_lock);
57 static void tls_device_free_ctx(struct tls_context *ctx)
59 if (ctx->tx_conf == TLS_HW) {
60 kfree(tls_offload_ctx_tx(ctx));
61 kfree(ctx->tx.rec_seq);
65 if (ctx->rx_conf == TLS_HW)
66 kfree(tls_offload_ctx_rx(ctx));
68 tls_ctx_free(NULL, ctx);
71 static void tls_device_gc_task(struct work_struct *work)
73 struct tls_context *ctx, *tmp;
77 spin_lock_irqsave(&tls_device_lock, flags);
78 list_splice_init(&tls_device_gc_list, &gc_list);
79 spin_unlock_irqrestore(&tls_device_lock, flags);
81 list_for_each_entry_safe(ctx, tmp, &gc_list, list) {
82 struct net_device *netdev = ctx->netdev;
84 if (netdev && ctx->tx_conf == TLS_HW) {
85 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
86 TLS_OFFLOAD_CTX_DIR_TX);
92 tls_device_free_ctx(ctx);
96 static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
100 spin_lock_irqsave(&tls_device_lock, flags);
101 list_move_tail(&ctx->list, &tls_device_gc_list);
103 /* schedule_work inside the spinlock
104 * to make sure tls_device_down waits for that work.
106 schedule_work(&tls_device_gc_work);
108 spin_unlock_irqrestore(&tls_device_lock, flags);
111 /* We assume that the socket is already connected */
112 static struct net_device *get_netdev_for_sock(struct sock *sk)
114 struct dst_entry *dst = sk_dst_get(sk);
115 struct net_device *netdev = NULL;
118 netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
127 static void destroy_record(struct tls_record_info *record)
131 for (i = 0; i < record->num_frags; i++)
132 __skb_frag_unref(&record->frags[i], false);
136 static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
138 struct tls_record_info *info, *temp;
140 list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
141 list_del(&info->list);
142 destroy_record(info);
145 offload_ctx->retransmit_hint = NULL;
148 static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
150 struct tls_context *tls_ctx = tls_get_ctx(sk);
151 struct tls_record_info *info, *temp;
152 struct tls_offload_context_tx *ctx;
153 u64 deleted_records = 0;
159 ctx = tls_offload_ctx_tx(tls_ctx);
161 spin_lock_irqsave(&ctx->lock, flags);
162 info = ctx->retransmit_hint;
163 if (info && !before(acked_seq, info->end_seq))
164 ctx->retransmit_hint = NULL;
166 list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
167 if (before(acked_seq, info->end_seq))
169 list_del(&info->list);
171 destroy_record(info);
175 ctx->unacked_record_sn += deleted_records;
176 spin_unlock_irqrestore(&ctx->lock, flags);
179 /* At this point, there should be no references on this
180 * socket and no in-flight SKBs associated with this
181 * socket, so it is safe to free all the resources.
183 void tls_device_sk_destruct(struct sock *sk)
185 struct tls_context *tls_ctx = tls_get_ctx(sk);
186 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
188 tls_ctx->sk_destruct(sk);
190 if (tls_ctx->tx_conf == TLS_HW) {
191 if (ctx->open_record)
192 destroy_record(ctx->open_record);
193 delete_all_records(ctx);
194 crypto_free_aead(ctx->aead_send);
195 clean_acked_data_disable(inet_csk(sk));
198 if (refcount_dec_and_test(&tls_ctx->refcount))
199 tls_device_queue_ctx_destruction(tls_ctx);
201 EXPORT_SYMBOL_GPL(tls_device_sk_destruct);
203 void tls_device_free_resources_tx(struct sock *sk)
205 struct tls_context *tls_ctx = tls_get_ctx(sk);
207 tls_free_partial_record(sk, tls_ctx);
210 void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq)
212 struct tls_context *tls_ctx = tls_get_ctx(sk);
214 trace_tls_device_tx_resync_req(sk, got_seq, exp_seq);
215 WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
217 EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request);
219 static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
222 struct net_device *netdev;
227 skb = tcp_write_queue_tail(sk);
229 TCP_SKB_CB(skb)->eor = 1;
231 rcd_sn = tls_ctx->tx.rec_seq;
233 trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
234 down_read(&device_offload_lock);
235 netdev = tls_ctx->netdev;
237 err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
239 TLS_OFFLOAD_CTX_DIR_TX);
240 up_read(&device_offload_lock);
244 clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
247 static void tls_append_frag(struct tls_record_info *record,
248 struct page_frag *pfrag,
253 frag = &record->frags[record->num_frags - 1];
254 if (skb_frag_page(frag) == pfrag->page &&
255 skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
256 skb_frag_size_add(frag, size);
259 __skb_frag_set_page(frag, pfrag->page);
260 skb_frag_off_set(frag, pfrag->offset);
261 skb_frag_size_set(frag, size);
263 get_page(pfrag->page);
266 pfrag->offset += size;
270 static int tls_push_record(struct sock *sk,
271 struct tls_context *ctx,
272 struct tls_offload_context_tx *offload_ctx,
273 struct tls_record_info *record,
276 struct tls_prot_info *prot = &ctx->prot_info;
277 struct tcp_sock *tp = tcp_sk(sk);
281 record->end_seq = tp->write_seq + record->len;
282 list_add_tail_rcu(&record->list, &offload_ctx->records_list);
283 offload_ctx->open_record = NULL;
285 if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
286 tls_device_resync_tx(sk, ctx, tp->write_seq);
288 tls_advance_record_sn(sk, prot, &ctx->tx);
290 for (i = 0; i < record->num_frags; i++) {
291 frag = &record->frags[i];
292 sg_unmark_end(&offload_ctx->sg_tx_data[i]);
293 sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
294 skb_frag_size(frag), skb_frag_off(frag));
295 sk_mem_charge(sk, skb_frag_size(frag));
296 get_page(skb_frag_page(frag));
298 sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
300 /* all ready, send */
301 return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
304 static int tls_device_record_close(struct sock *sk,
305 struct tls_context *ctx,
306 struct tls_record_info *record,
307 struct page_frag *pfrag,
308 unsigned char record_type)
310 struct tls_prot_info *prot = &ctx->prot_info;
314 * device will fill in the tag, we just need to append a placeholder
315 * use socket memory to improve coalescing (re-using a single buffer
316 * increases frag count)
317 * if we can't allocate memory now, steal some back from data
319 if (likely(skb_page_frag_refill(prot->tag_size, pfrag,
320 sk->sk_allocation))) {
322 tls_append_frag(record, pfrag, prot->tag_size);
324 ret = prot->tag_size;
325 if (record->len <= prot->overhead_size)
330 tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
331 record->len - prot->overhead_size,
336 static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
337 struct page_frag *pfrag,
340 struct tls_record_info *record;
343 record = kmalloc(sizeof(*record), GFP_KERNEL);
347 frag = &record->frags[0];
348 __skb_frag_set_page(frag, pfrag->page);
349 skb_frag_off_set(frag, pfrag->offset);
350 skb_frag_size_set(frag, prepend_size);
352 get_page(pfrag->page);
353 pfrag->offset += prepend_size;
355 record->num_frags = 1;
356 record->len = prepend_size;
357 offload_ctx->open_record = record;
361 static int tls_do_allocation(struct sock *sk,
362 struct tls_offload_context_tx *offload_ctx,
363 struct page_frag *pfrag,
368 if (!offload_ctx->open_record) {
369 if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
370 sk->sk_allocation))) {
371 READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
372 sk_stream_moderate_sndbuf(sk);
376 ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
380 if (pfrag->size > pfrag->offset)
384 if (!sk_page_frag_refill(sk, pfrag))
390 static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
392 size_t pre_copy, nocache;
394 pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
396 pre_copy = min(pre_copy, bytes);
397 if (copy_from_iter(addr, pre_copy, i) != pre_copy)
403 nocache = round_down(bytes, SMP_CACHE_BYTES);
404 if (copy_from_iter_nocache(addr, nocache, i) != nocache)
409 if (bytes && copy_from_iter(addr, bytes, i) != bytes)
415 union tls_iter_offset {
416 struct iov_iter *msg_iter;
420 static int tls_push_data(struct sock *sk,
421 union tls_iter_offset iter_offset,
422 size_t size, int flags,
423 unsigned char record_type,
424 struct page *zc_page)
426 struct tls_context *tls_ctx = tls_get_ctx(sk);
427 struct tls_prot_info *prot = &tls_ctx->prot_info;
428 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
429 struct tls_record_info *record;
430 int tls_push_record_flags;
431 struct page_frag *pfrag;
432 size_t orig_size = size;
433 u32 max_open_record_len;
440 ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | MSG_SENDPAGE_NOTLAST))
443 if (unlikely(sk->sk_err))
446 flags |= MSG_SENDPAGE_DECRYPTED;
447 tls_push_record_flags = flags | MSG_SENDPAGE_NOTLAST;
449 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
450 if (tls_is_partially_sent_record(tls_ctx)) {
451 rc = tls_push_partial_record(sk, tls_ctx, flags);
456 pfrag = sk_page_frag(sk);
458 /* TLS_HEADER_SIZE is not counted as part of the TLS record, and
459 * we need to leave room for an authentication tag.
461 max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
464 rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
466 rc = sk_stream_wait_memory(sk, &timeo);
470 record = ctx->open_record;
474 if (record_type != TLS_RECORD_TYPE_DATA) {
475 /* avoid sending partial
476 * record with type !=
480 destroy_record(record);
481 ctx->open_record = NULL;
482 } else if (record->len > prot->prepend_size) {
489 record = ctx->open_record;
491 copy = min_t(size_t, size, max_open_record_len - record->len);
492 if (copy && zc_page) {
493 struct page_frag zc_pfrag;
495 zc_pfrag.page = zc_page;
496 zc_pfrag.offset = iter_offset.offset;
497 zc_pfrag.size = copy;
498 tls_append_frag(record, &zc_pfrag, copy);
500 copy = min_t(size_t, copy, pfrag->size - pfrag->offset);
502 rc = tls_device_copy_data(page_address(pfrag->page) +
504 iter_offset.msg_iter);
507 tls_append_frag(record, pfrag, copy);
513 tls_push_record_flags = flags;
514 if (flags & (MSG_SENDPAGE_NOTLAST | MSG_MORE)) {
522 if (done || record->len >= max_open_record_len ||
523 (record->num_frags >= MAX_SKB_FRAGS - 1)) {
524 rc = tls_device_record_close(sk, tls_ctx, record,
531 destroy_record(record);
532 ctx->open_record = NULL;
537 rc = tls_push_record(sk,
541 tls_push_record_flags);
547 tls_ctx->pending_open_record_frags = more;
549 if (orig_size - size > 0)
550 rc = orig_size - size;
555 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
557 unsigned char record_type = TLS_RECORD_TYPE_DATA;
558 struct tls_context *tls_ctx = tls_get_ctx(sk);
559 union tls_iter_offset iter;
562 mutex_lock(&tls_ctx->tx_lock);
565 if (unlikely(msg->msg_controllen)) {
566 rc = tls_process_cmsg(sk, msg, &record_type);
571 iter.msg_iter = &msg->msg_iter;
572 rc = tls_push_data(sk, iter, size, msg->msg_flags, record_type, NULL);
576 mutex_unlock(&tls_ctx->tx_lock);
580 int tls_device_sendpage(struct sock *sk, struct page *page,
581 int offset, size_t size, int flags)
583 struct tls_context *tls_ctx = tls_get_ctx(sk);
584 union tls_iter_offset iter_offset;
585 struct iov_iter msg_iter;
590 if (flags & MSG_SENDPAGE_NOTLAST)
593 mutex_lock(&tls_ctx->tx_lock);
596 if (flags & MSG_OOB) {
601 if (tls_ctx->zerocopy_sendfile) {
602 iter_offset.offset = offset;
603 rc = tls_push_data(sk, iter_offset, size,
604 flags, TLS_RECORD_TYPE_DATA, page);
609 iov.iov_base = kaddr + offset;
611 iov_iter_kvec(&msg_iter, WRITE, &iov, 1, size);
612 iter_offset.msg_iter = &msg_iter;
613 rc = tls_push_data(sk, iter_offset, size, flags, TLS_RECORD_TYPE_DATA,
619 mutex_unlock(&tls_ctx->tx_lock);
623 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
624 u32 seq, u64 *p_record_sn)
626 u64 record_sn = context->hint_record_sn;
627 struct tls_record_info *info, *last;
629 info = context->retransmit_hint;
631 before(seq, info->end_seq - info->len)) {
632 /* if retransmit_hint is irrelevant start
633 * from the beginning of the list
635 info = list_first_entry_or_null(&context->records_list,
636 struct tls_record_info, list);
639 /* send the start_marker record if seq number is before the
640 * tls offload start marker sequence number. This record is
641 * required to handle TCP packets which are before TLS offload
643 * And if it's not start marker, look if this seq number
644 * belongs to the list.
646 if (likely(!tls_record_is_start_marker(info))) {
647 /* we have the first record, get the last record to see
648 * if this seq number belongs to the list.
650 last = list_last_entry(&context->records_list,
651 struct tls_record_info, list);
653 if (!between(seq, tls_record_start_seq(info),
657 record_sn = context->unacked_record_sn;
660 /* We just need the _rcu for the READ_ONCE() */
662 list_for_each_entry_from_rcu(info, &context->records_list, list) {
663 if (before(seq, info->end_seq)) {
664 if (!context->retransmit_hint ||
666 context->retransmit_hint->end_seq)) {
667 context->hint_record_sn = record_sn;
668 context->retransmit_hint = info;
670 *p_record_sn = record_sn;
671 goto exit_rcu_unlock;
681 EXPORT_SYMBOL(tls_get_record);
683 static int tls_device_push_pending_record(struct sock *sk, int flags)
685 union tls_iter_offset iter;
686 struct iov_iter msg_iter;
688 iov_iter_kvec(&msg_iter, WRITE, NULL, 0, 0);
689 iter.msg_iter = &msg_iter;
690 return tls_push_data(sk, iter, 0, flags, TLS_RECORD_TYPE_DATA, NULL);
693 void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
695 if (tls_is_partially_sent_record(ctx)) {
696 gfp_t sk_allocation = sk->sk_allocation;
698 WARN_ON_ONCE(sk->sk_write_pending);
700 sk->sk_allocation = GFP_ATOMIC;
701 tls_push_partial_record(sk, ctx,
702 MSG_DONTWAIT | MSG_NOSIGNAL |
703 MSG_SENDPAGE_DECRYPTED);
704 sk->sk_allocation = sk_allocation;
708 static void tls_device_resync_rx(struct tls_context *tls_ctx,
709 struct sock *sk, u32 seq, u8 *rcd_sn)
711 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
712 struct net_device *netdev;
714 trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type);
716 netdev = READ_ONCE(tls_ctx->netdev);
718 netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
719 TLS_OFFLOAD_CTX_DIR_RX);
721 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
725 tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
726 s64 resync_req, u32 *seq, u16 *rcd_delta)
728 u32 is_async = resync_req & RESYNC_REQ_ASYNC;
729 u32 req_seq = resync_req >> 32;
730 u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
736 /* shouldn't get to wraparound:
737 * too long in async stage, something bad happened
739 if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX))
742 /* asynchronous stage: log all headers seq such that
743 * req_seq <= seq <= end_seq, and wait for real resync request
745 if (before(*seq, req_seq))
747 if (!after(*seq, req_end) &&
748 resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
749 resync_async->log[resync_async->loglen++] = *seq;
751 resync_async->rcd_delta++;
756 /* synchronous stage: check against the logged entries and
757 * proceed to check the next entries if no match was found
759 for (i = 0; i < resync_async->loglen; i++)
760 if (req_seq == resync_async->log[i] &&
761 atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) {
762 *rcd_delta = resync_async->rcd_delta - i;
764 resync_async->loglen = 0;
765 resync_async->rcd_delta = 0;
769 resync_async->loglen = 0;
770 resync_async->rcd_delta = 0;
772 if (req_seq == *seq &&
773 atomic64_try_cmpxchg(&resync_async->req,
780 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
782 struct tls_context *tls_ctx = tls_get_ctx(sk);
783 struct tls_offload_context_rx *rx_ctx;
784 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
785 u32 sock_data, is_req_pending;
786 struct tls_prot_info *prot;
791 if (tls_ctx->rx_conf != TLS_HW)
793 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags)))
796 prot = &tls_ctx->prot_info;
797 rx_ctx = tls_offload_ctx_rx(tls_ctx);
798 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
800 switch (rx_ctx->resync_type) {
801 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
802 resync_req = atomic64_read(&rx_ctx->resync_req);
803 req_seq = resync_req >> 32;
804 seq += TLS_HEADER_SIZE - 1;
805 is_req_pending = resync_req;
807 if (likely(!is_req_pending) || req_seq != seq ||
808 !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
811 case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
812 if (likely(!rx_ctx->resync_nh_do_now))
815 /* head of next rec is already in, note that the sock_inq will
816 * include the currently parsed message when called from parser
818 sock_data = tcp_inq(sk);
819 if (sock_data > rcd_len) {
820 trace_tls_device_rx_resync_nh_delay(sk, sock_data,
825 rx_ctx->resync_nh_do_now = 0;
827 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
829 case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
830 resync_req = atomic64_read(&rx_ctx->resync_async->req);
831 is_req_pending = resync_req;
832 if (likely(!is_req_pending))
835 if (!tls_device_rx_resync_async(rx_ctx->resync_async,
836 resync_req, &seq, &rcd_delta))
838 tls_bigint_subtract(rcd_sn, rcd_delta);
842 tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
845 static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
846 struct tls_offload_context_rx *ctx,
847 struct sock *sk, struct sk_buff *skb)
849 struct strp_msg *rxm;
851 /* device will request resyncs by itself based on stream scan */
852 if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
854 /* already scheduled */
855 if (ctx->resync_nh_do_now)
857 /* seen decrypted fragments since last fully-failed record */
858 if (ctx->resync_nh_reset) {
859 ctx->resync_nh_reset = 0;
860 ctx->resync_nh.decrypted_failed = 1;
861 ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
865 if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
868 /* doing resync, bump the next target in case it fails */
869 if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
870 ctx->resync_nh.decrypted_tgt *= 2;
872 ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
876 /* head of next rec is already in, parser will sync for us */
877 if (tcp_inq(sk) > rxm->full_len) {
878 trace_tls_device_rx_resync_nh_schedule(sk);
879 ctx->resync_nh_do_now = 1;
881 struct tls_prot_info *prot = &tls_ctx->prot_info;
882 u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
884 memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
885 tls_bigint_increment(rcd_sn, prot->rec_seq_size);
887 tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
892 static int tls_device_reencrypt(struct sock *sk, struct sk_buff *skb)
894 struct strp_msg *rxm = strp_msg(skb);
895 int err = 0, offset = rxm->offset, copy, nsg, data_len, pos;
896 struct sk_buff *skb_iter, *unused;
897 struct scatterlist sg[1];
898 char *orig_buf, *buf;
900 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE +
901 TLS_CIPHER_AES_GCM_128_IV_SIZE, sk->sk_allocation);
906 nsg = skb_cow_data(skb, 0, &unused);
907 if (unlikely(nsg < 0)) {
912 sg_init_table(sg, 1);
913 sg_set_buf(&sg[0], buf,
914 rxm->full_len + TLS_HEADER_SIZE +
915 TLS_CIPHER_AES_GCM_128_IV_SIZE);
916 err = skb_copy_bits(skb, offset, buf,
917 TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE);
921 /* We are interested only in the decrypted data not the auth */
922 err = decrypt_skb(sk, skb, sg);
928 data_len = rxm->full_len - TLS_CIPHER_AES_GCM_128_TAG_SIZE;
930 if (skb_pagelen(skb) > offset) {
931 copy = min_t(int, skb_pagelen(skb) - offset, data_len);
933 if (skb->decrypted) {
934 err = skb_store_bits(skb, offset, buf, copy);
943 pos = skb_pagelen(skb);
944 skb_walk_frags(skb, skb_iter) {
947 /* Practically all frags must belong to msg if reencrypt
948 * is needed with current strparser and coalescing logic,
949 * but strparser may "get optimized", so let's be safe.
951 if (pos + skb_iter->len <= offset)
953 if (pos >= data_len + rxm->offset)
956 frag_pos = offset - pos;
957 copy = min_t(int, skb_iter->len - frag_pos,
958 data_len + rxm->offset - offset);
960 if (skb_iter->decrypted) {
961 err = skb_store_bits(skb_iter, frag_pos, buf, copy);
969 pos += skb_iter->len;
977 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
978 struct sk_buff *skb, struct strp_msg *rxm)
980 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
981 int is_decrypted = skb->decrypted;
982 int is_encrypted = !is_decrypted;
983 struct sk_buff *skb_iter;
985 /* Check if all the data is decrypted already */
986 skb_walk_frags(skb, skb_iter) {
987 is_decrypted &= skb_iter->decrypted;
988 is_encrypted &= !skb_iter->decrypted;
991 trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
992 tls_ctx->rx.rec_seq, rxm->full_len,
993 is_encrypted, is_decrypted);
995 if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) {
996 if (likely(is_encrypted || is_decrypted))
999 /* After tls_device_down disables the offload, the next SKB will
1000 * likely have initial fragments decrypted, and final ones not
1001 * decrypted. We need to reencrypt that single SKB.
1003 return tls_device_reencrypt(sk, skb);
1006 /* Return immediately if the record is either entirely plaintext or
1007 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
1011 ctx->resync_nh_reset = 1;
1012 return is_decrypted;
1015 tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
1019 ctx->resync_nh_reset = 1;
1020 return tls_device_reencrypt(sk, skb);
1023 static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
1024 struct net_device *netdev)
1026 if (sk->sk_destruct != tls_device_sk_destruct) {
1027 refcount_set(&ctx->refcount, 1);
1029 ctx->netdev = netdev;
1030 spin_lock_irq(&tls_device_lock);
1031 list_add_tail(&ctx->list, &tls_device_list);
1032 spin_unlock_irq(&tls_device_lock);
1034 ctx->sk_destruct = sk->sk_destruct;
1035 smp_store_release(&sk->sk_destruct, tls_device_sk_destruct);
1039 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
1041 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
1042 struct tls_context *tls_ctx = tls_get_ctx(sk);
1043 struct tls_prot_info *prot = &tls_ctx->prot_info;
1044 struct tls_record_info *start_marker_record;
1045 struct tls_offload_context_tx *offload_ctx;
1046 struct tls_crypto_info *crypto_info;
1047 struct net_device *netdev;
1049 struct sk_buff *skb;
1056 if (ctx->priv_ctx_tx)
1059 netdev = get_netdev_for_sock(sk);
1061 pr_err_ratelimited("%s: netdev not found\n", __func__);
1065 if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
1067 goto release_netdev;
1070 crypto_info = &ctx->crypto_send.info;
1071 if (crypto_info->version != TLS_1_2_VERSION) {
1073 goto release_netdev;
1076 switch (crypto_info->cipher_type) {
1077 case TLS_CIPHER_AES_GCM_128:
1078 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1079 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1080 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1081 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1082 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1083 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
1085 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1089 goto release_netdev;
1092 /* Sanity-check the rec_seq_size for stack allocations */
1093 if (rec_seq_size > TLS_MAX_REC_SEQ_SIZE) {
1095 goto release_netdev;
1098 prot->version = crypto_info->version;
1099 prot->cipher_type = crypto_info->cipher_type;
1100 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
1101 prot->tag_size = tag_size;
1102 prot->overhead_size = prot->prepend_size + prot->tag_size;
1103 prot->iv_size = iv_size;
1104 prot->salt_size = salt_size;
1105 ctx->tx.iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1109 goto release_netdev;
1112 memcpy(ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1114 prot->rec_seq_size = rec_seq_size;
1115 ctx->tx.rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
1116 if (!ctx->tx.rec_seq) {
1121 start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
1122 if (!start_marker_record) {
1127 offload_ctx = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_TX, GFP_KERNEL);
1130 goto free_marker_record;
1133 rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
1135 goto free_offload_ctx;
1137 /* start at rec_seq - 1 to account for the start marker record */
1138 memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
1139 offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
1141 start_marker_record->end_seq = tcp_sk(sk)->write_seq;
1142 start_marker_record->len = 0;
1143 start_marker_record->num_frags = 0;
1145 INIT_LIST_HEAD(&offload_ctx->records_list);
1146 list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
1147 spin_lock_init(&offload_ctx->lock);
1148 sg_init_table(offload_ctx->sg_tx_data,
1149 ARRAY_SIZE(offload_ctx->sg_tx_data));
1151 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
1152 ctx->push_pending_record = tls_device_push_pending_record;
1154 /* TLS offload is greatly simplified if we don't send
1155 * SKBs where only part of the payload needs to be encrypted.
1156 * So mark the last skb in the write queue as end of record.
1158 skb = tcp_write_queue_tail(sk);
1160 TCP_SKB_CB(skb)->eor = 1;
1162 /* Avoid offloading if the device is down
1163 * We don't want to offload new flows after
1164 * the NETDEV_DOWN event
1166 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1167 * handler thus protecting from the device going down before
1168 * ctx was added to tls_device_list.
1170 down_read(&device_offload_lock);
1171 if (!(netdev->flags & IFF_UP)) {
1176 ctx->priv_ctx_tx = offload_ctx;
1177 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
1178 &ctx->crypto_send.info,
1179 tcp_sk(sk)->write_seq);
1180 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX,
1181 tcp_sk(sk)->write_seq, rec_seq, rc);
1185 tls_device_attach(ctx, sk, netdev);
1186 up_read(&device_offload_lock);
1188 /* following this assignment tls_is_sk_tx_device_offloaded
1189 * will return true and the context might be accessed
1190 * by the netdev's xmit function.
1192 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
1198 up_read(&device_offload_lock);
1199 clean_acked_data_disable(inet_csk(sk));
1200 crypto_free_aead(offload_ctx->aead_send);
1203 ctx->priv_ctx_tx = NULL;
1205 kfree(start_marker_record);
1207 kfree(ctx->tx.rec_seq);
1215 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
1217 struct tls12_crypto_info_aes_gcm_128 *info;
1218 struct tls_offload_context_rx *context;
1219 struct net_device *netdev;
1222 if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
1225 netdev = get_netdev_for_sock(sk);
1227 pr_err_ratelimited("%s: netdev not found\n", __func__);
1231 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
1233 goto release_netdev;
1236 /* Avoid offloading if the device is down
1237 * We don't want to offload new flows after
1238 * the NETDEV_DOWN event
1240 * device_offload_lock is taken in tls_devices's NETDEV_DOWN
1241 * handler thus protecting from the device going down before
1242 * ctx was added to tls_device_list.
1244 down_read(&device_offload_lock);
1245 if (!(netdev->flags & IFF_UP)) {
1250 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
1255 context->resync_nh_reset = 1;
1257 ctx->priv_ctx_rx = context;
1258 rc = tls_set_sw_offload(sk, ctx, 0);
1262 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
1263 &ctx->crypto_recv.info,
1264 tcp_sk(sk)->copied_seq);
1265 info = (void *)&ctx->crypto_recv.info;
1266 trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX,
1267 tcp_sk(sk)->copied_seq, info->rec_seq, rc);
1269 goto free_sw_resources;
1271 tls_device_attach(ctx, sk, netdev);
1272 up_read(&device_offload_lock);
1279 up_read(&device_offload_lock);
1280 tls_sw_free_resources_rx(sk);
1281 down_read(&device_offload_lock);
1283 ctx->priv_ctx_rx = NULL;
1285 up_read(&device_offload_lock);
1291 void tls_device_offload_cleanup_rx(struct sock *sk)
1293 struct tls_context *tls_ctx = tls_get_ctx(sk);
1294 struct net_device *netdev;
1296 down_read(&device_offload_lock);
1297 netdev = tls_ctx->netdev;
1301 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
1302 TLS_OFFLOAD_CTX_DIR_RX);
1304 if (tls_ctx->tx_conf != TLS_HW) {
1306 tls_ctx->netdev = NULL;
1308 set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
1311 up_read(&device_offload_lock);
1312 tls_sw_release_resources_rx(sk);
1315 static int tls_device_down(struct net_device *netdev)
1317 struct tls_context *ctx, *tmp;
1318 unsigned long flags;
1321 /* Request a write lock to block new offload attempts */
1322 down_write(&device_offload_lock);
1324 spin_lock_irqsave(&tls_device_lock, flags);
1325 list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
1326 if (ctx->netdev != netdev ||
1327 !refcount_inc_not_zero(&ctx->refcount))
1330 list_move(&ctx->list, &list);
1332 spin_unlock_irqrestore(&tls_device_lock, flags);
1334 list_for_each_entry_safe(ctx, tmp, &list, list) {
1335 /* Stop offloaded TX and switch to the fallback.
1336 * tls_is_sk_tx_device_offloaded will return false.
1338 WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
1340 /* Stop the RX and TX resync.
1341 * tls_dev_resync must not be called after tls_dev_del.
1343 WRITE_ONCE(ctx->netdev, NULL);
1345 /* Start skipping the RX resync logic completely. */
1346 set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags);
1348 /* Sync with inflight packets. After this point:
1349 * TX: no non-encrypted packets will be passed to the driver.
1350 * RX: resync requests from the driver will be ignored.
1354 /* Release the offload context on the driver side. */
1355 if (ctx->tx_conf == TLS_HW)
1356 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1357 TLS_OFFLOAD_CTX_DIR_TX);
1358 if (ctx->rx_conf == TLS_HW &&
1359 !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
1360 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
1361 TLS_OFFLOAD_CTX_DIR_RX);
1365 /* Move the context to a separate list for two reasons:
1366 * 1. When the context is deallocated, list_del is called.
1367 * 2. It's no longer an offloaded context, so we don't want to
1368 * run offload-specific code on this context.
1370 spin_lock_irqsave(&tls_device_lock, flags);
1371 list_move_tail(&ctx->list, &tls_device_down_list);
1372 spin_unlock_irqrestore(&tls_device_lock, flags);
1374 /* Device contexts for RX and TX will be freed in on sk_destruct
1375 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
1376 * Now release the ref taken above.
1378 if (refcount_dec_and_test(&ctx->refcount))
1379 tls_device_free_ctx(ctx);
1382 up_write(&device_offload_lock);
1384 flush_work(&tls_device_gc_work);
1389 static int tls_dev_event(struct notifier_block *this, unsigned long event,
1392 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1394 if (!dev->tlsdev_ops &&
1395 !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
1399 case NETDEV_REGISTER:
1400 case NETDEV_FEAT_CHANGE:
1401 if (netif_is_bond_master(dev))
1403 if ((dev->features & NETIF_F_HW_TLS_RX) &&
1404 !dev->tlsdev_ops->tls_dev_resync)
1407 if (dev->tlsdev_ops &&
1408 dev->tlsdev_ops->tls_dev_add &&
1409 dev->tlsdev_ops->tls_dev_del)
1414 return tls_device_down(dev);
1419 static struct notifier_block tls_dev_notifier = {
1420 .notifier_call = tls_dev_event,
1423 void __init tls_device_init(void)
1425 register_netdevice_notifier(&tls_dev_notifier);
1428 void __exit tls_device_cleanup(void)
1430 unregister_netdevice_notifier(&tls_dev_notifier);
1431 flush_work(&tls_device_gc_work);
1432 clean_acked_data_flush();