aboutsummaryrefslogtreecommitdiffstats
path: root/net/tls/tls_device.c
diff options
context:
space:
mode:
authorBoris Pismenny <borisp@mellanox.com>2018-07-13 07:33:43 -0400
committerDavid S. Miller <davem@davemloft.net>2018-07-16 03:13:11 -0400
commit4799ac81e52a72a6404827bf2738337bb581a174 (patch)
tree0d75fbecd761c35507d05122d9d26855c7e6c4de /net/tls/tls_device.c
parentb190a587c634a8559e4ceabeb0468e93db49789a (diff)
tls: Add rx inline crypto offload
This patch completes the generic infrastructure to offload TLS crypto to a network device. It enables the kernel to skip decryption and authentication of some skbs marked as decrypted by the NIC. In the fast path, all packets received are decrypted by the NIC and the performance is comparable to plain TCP. This infrastructure doesn't require a TCP offload engine. Instead, the NIC only decrypts packets that contain the expected TCP sequence number. Out-Of-Order TCP packets are provided unmodified. As a result, at the worst case a received TLS record consists of both plaintext and ciphertext packets. These partially decrypted records must be reencrypted, only to be decrypted. The notable differences between SW KTLS Rx and this offload are as follows: 1. Partial decryption - Software must handle the case of a TLS record that was only partially decrypted by HW. This can happen due to packet reordering. 2. Resynchronization - tls_read_size calls the device driver to resynchronize HW after HW lost track of TLS record framing in the TCP stream. Signed-off-by: Boris Pismenny <borisp@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/tls/tls_device.c')
-rw-r--r--net/tls/tls_device.c278
1 files changed, 256 insertions, 22 deletions
diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
index 332a5d1459b6..4995d84d228d 100644
--- a/net/tls/tls_device.c
+++ b/net/tls/tls_device.c
@@ -52,7 +52,11 @@ static DEFINE_SPINLOCK(tls_device_lock);
52 52
53static void tls_device_free_ctx(struct tls_context *ctx) 53static void tls_device_free_ctx(struct tls_context *ctx)
54{ 54{
55 kfree(tls_offload_ctx_tx(ctx)); 55 if (ctx->tx_conf == TLS_HW)
56 kfree(tls_offload_ctx_tx(ctx));
57
58 if (ctx->rx_conf == TLS_HW)
59 kfree(tls_offload_ctx_rx(ctx));
56 60
57 kfree(ctx); 61 kfree(ctx);
58} 62}
@@ -70,10 +74,11 @@ static void tls_device_gc_task(struct work_struct *work)
70 list_for_each_entry_safe(ctx, tmp, &gc_list, list) { 74 list_for_each_entry_safe(ctx, tmp, &gc_list, list) {
71 struct net_device *netdev = ctx->netdev; 75 struct net_device *netdev = ctx->netdev;
72 76
73 if (netdev) { 77 if (netdev && ctx->tx_conf == TLS_HW) {
74 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, 78 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
75 TLS_OFFLOAD_CTX_DIR_TX); 79 TLS_OFFLOAD_CTX_DIR_TX);
76 dev_put(netdev); 80 dev_put(netdev);
81 ctx->netdev = NULL;
77 } 82 }
78 83
79 list_del(&ctx->list); 84 list_del(&ctx->list);
@@ -81,6 +86,22 @@ static void tls_device_gc_task(struct work_struct *work)
81 } 86 }
82} 87}
83 88
89static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
90 struct net_device *netdev)
91{
92 if (sk->sk_destruct != tls_device_sk_destruct) {
93 refcount_set(&ctx->refcount, 1);
94 dev_hold(netdev);
95 ctx->netdev = netdev;
96 spin_lock_irq(&tls_device_lock);
97 list_add_tail(&ctx->list, &tls_device_list);
98 spin_unlock_irq(&tls_device_lock);
99
100 ctx->sk_destruct = sk->sk_destruct;
101 sk->sk_destruct = tls_device_sk_destruct;
102 }
103}
104
84static void tls_device_queue_ctx_destruction(struct tls_context *ctx) 105static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
85{ 106{
86 unsigned long flags; 107 unsigned long flags;
@@ -180,13 +201,15 @@ void tls_device_sk_destruct(struct sock *sk)
180 struct tls_context *tls_ctx = tls_get_ctx(sk); 201 struct tls_context *tls_ctx = tls_get_ctx(sk);
181 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx); 202 struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
182 203
183 if (ctx->open_record) 204 tls_ctx->sk_destruct(sk);
184 destroy_record(ctx->open_record);
185 205
186 delete_all_records(ctx); 206 if (tls_ctx->tx_conf == TLS_HW) {
187 crypto_free_aead(ctx->aead_send); 207 if (ctx->open_record)
188 ctx->sk_destruct(sk); 208 destroy_record(ctx->open_record);
189 clean_acked_data_disable(inet_csk(sk)); 209 delete_all_records(ctx);
210 crypto_free_aead(ctx->aead_send);
211 clean_acked_data_disable(inet_csk(sk));
212 }
190 213
191 if (refcount_dec_and_test(&tls_ctx->refcount)) 214 if (refcount_dec_and_test(&tls_ctx->refcount))
192 tls_device_queue_ctx_destruction(tls_ctx); 215 tls_device_queue_ctx_destruction(tls_ctx);
@@ -519,6 +542,118 @@ static int tls_device_push_pending_record(struct sock *sk, int flags)
519 return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA); 542 return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA);
520} 543}
521 544
545void handle_device_resync(struct sock *sk, u32 seq, u64 rcd_sn)
546{
547 struct tls_context *tls_ctx = tls_get_ctx(sk);
548 struct net_device *netdev = tls_ctx->netdev;
549 struct tls_offload_context_rx *rx_ctx;
550 u32 is_req_pending;
551 s64 resync_req;
552 u32 req_seq;
553
554 if (tls_ctx->rx_conf != TLS_HW)
555 return;
556
557 rx_ctx = tls_offload_ctx_rx(tls_ctx);
558 resync_req = atomic64_read(&rx_ctx->resync_req);
559 req_seq = ntohl(resync_req >> 32) - ((u32)TLS_HEADER_SIZE - 1);
560 is_req_pending = resync_req;
561
562 if (unlikely(is_req_pending) && req_seq == seq &&
563 atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
564 netdev->tlsdev_ops->tls_dev_resync_rx(netdev, sk,
565 seq + TLS_HEADER_SIZE - 1,
566 rcd_sn);
567}
568
569static int tls_device_reencrypt(struct sock *sk, struct sk_buff *skb)
570{
571 struct strp_msg *rxm = strp_msg(skb);
572 int err = 0, offset = rxm->offset, copy, nsg;
573 struct sk_buff *skb_iter, *unused;
574 struct scatterlist sg[1];
575 char *orig_buf, *buf;
576
577 orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE +
578 TLS_CIPHER_AES_GCM_128_IV_SIZE, sk->sk_allocation);
579 if (!orig_buf)
580 return -ENOMEM;
581 buf = orig_buf;
582
583 nsg = skb_cow_data(skb, 0, &unused);
584 if (unlikely(nsg < 0)) {
585 err = nsg;
586 goto free_buf;
587 }
588
589 sg_init_table(sg, 1);
590 sg_set_buf(&sg[0], buf,
591 rxm->full_len + TLS_HEADER_SIZE +
592 TLS_CIPHER_AES_GCM_128_IV_SIZE);
593 skb_copy_bits(skb, offset, buf,
594 TLS_HEADER_SIZE + TLS_CIPHER_AES_GCM_128_IV_SIZE);
595
596 /* We are interested only in the decrypted data not the auth */
597 err = decrypt_skb(sk, skb, sg);
598 if (err != -EBADMSG)
599 goto free_buf;
600 else
601 err = 0;
602
603 copy = min_t(int, skb_pagelen(skb) - offset,
604 rxm->full_len - TLS_CIPHER_AES_GCM_128_TAG_SIZE);
605
606 if (skb->decrypted)
607 skb_store_bits(skb, offset, buf, copy);
608
609 offset += copy;
610 buf += copy;
611
612 skb_walk_frags(skb, skb_iter) {
613 copy = min_t(int, skb_iter->len,
614 rxm->full_len - offset + rxm->offset -
615 TLS_CIPHER_AES_GCM_128_TAG_SIZE);
616
617 if (skb_iter->decrypted)
618 skb_store_bits(skb, offset, buf, copy);
619
620 offset += copy;
621 buf += copy;
622 }
623
624free_buf:
625 kfree(orig_buf);
626 return err;
627}
628
629int tls_device_decrypted(struct sock *sk, struct sk_buff *skb)
630{
631 struct tls_context *tls_ctx = tls_get_ctx(sk);
632 struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
633 int is_decrypted = skb->decrypted;
634 int is_encrypted = !is_decrypted;
635 struct sk_buff *skb_iter;
636
637 /* Skip if it is already decrypted */
638 if (ctx->sw.decrypted)
639 return 0;
640
641 /* Check if all the data is decrypted already */
642 skb_walk_frags(skb, skb_iter) {
643 is_decrypted &= skb_iter->decrypted;
644 is_encrypted &= !skb_iter->decrypted;
645 }
646
647 ctx->sw.decrypted |= is_decrypted;
648
649 /* Return immedeatly if the record is either entirely plaintext or
650 * entirely ciphertext. Otherwise handle reencrypt partially decrypted
651 * record.
652 */
653 return (is_encrypted || is_decrypted) ? 0 :
654 tls_device_reencrypt(sk, skb);
655}
656
522int tls_set_device_offload(struct sock *sk, struct tls_context *ctx) 657int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
523{ 658{
524 u16 nonce_size, tag_size, iv_size, rec_seq_size; 659 u16 nonce_size, tag_size, iv_size, rec_seq_size;
@@ -608,7 +743,6 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
608 743
609 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked); 744 clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
610 ctx->push_pending_record = tls_device_push_pending_record; 745 ctx->push_pending_record = tls_device_push_pending_record;
611 offload_ctx->sk_destruct = sk->sk_destruct;
612 746
613 /* TLS offload is greatly simplified if we don't send 747 /* TLS offload is greatly simplified if we don't send
614 * SKBs where only part of the payload needs to be encrypted. 748 * SKBs where only part of the payload needs to be encrypted.
@@ -618,8 +752,6 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
618 if (skb) 752 if (skb)
619 TCP_SKB_CB(skb)->eor = 1; 753 TCP_SKB_CB(skb)->eor = 1;
620 754
621 refcount_set(&ctx->refcount, 1);
622
623 /* We support starting offload on multiple sockets 755 /* We support starting offload on multiple sockets
624 * concurrently, so we only need a read lock here. 756 * concurrently, so we only need a read lock here.
625 * This lock must precede get_netdev_for_sock to prevent races between 757 * This lock must precede get_netdev_for_sock to prevent races between
@@ -654,19 +786,14 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
654 if (rc) 786 if (rc)
655 goto release_netdev; 787 goto release_netdev;
656 788
657 ctx->netdev = netdev; 789 tls_device_attach(ctx, sk, netdev);
658 790
659 spin_lock_irq(&tls_device_lock);
660 list_add_tail(&ctx->list, &tls_device_list);
661 spin_unlock_irq(&tls_device_lock);
662
663 sk->sk_validate_xmit_skb = tls_validate_xmit_skb;
664 /* following this assignment tls_is_sk_tx_device_offloaded 791 /* following this assignment tls_is_sk_tx_device_offloaded
665 * will return true and the context might be accessed 792 * will return true and the context might be accessed
666 * by the netdev's xmit function. 793 * by the netdev's xmit function.
667 */ 794 */
668 smp_store_release(&sk->sk_destruct, 795 smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
669 &tls_device_sk_destruct); 796 dev_put(netdev);
670 up_read(&device_offload_lock); 797 up_read(&device_offload_lock);
671 goto out; 798 goto out;
672 799
@@ -689,6 +816,105 @@ out:
689 return rc; 816 return rc;
690} 817}
691 818
819int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
820{
821 struct tls_offload_context_rx *context;
822 struct net_device *netdev;
823 int rc = 0;
824
825 /* We support starting offload on multiple sockets
826 * concurrently, so we only need a read lock here.
827 * This lock must precede get_netdev_for_sock to prevent races between
828 * NETDEV_DOWN and setsockopt.
829 */
830 down_read(&device_offload_lock);
831 netdev = get_netdev_for_sock(sk);
832 if (!netdev) {
833 pr_err_ratelimited("%s: netdev not found\n", __func__);
834 rc = -EINVAL;
835 goto release_lock;
836 }
837
838 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
839 pr_err_ratelimited("%s: netdev %s with no TLS offload\n",
840 __func__, netdev->name);
841 rc = -ENOTSUPP;
842 goto release_netdev;
843 }
844
845 /* Avoid offloading if the device is down
846 * We don't want to offload new flows after
847 * the NETDEV_DOWN event
848 */
849 if (!(netdev->flags & IFF_UP)) {
850 rc = -EINVAL;
851 goto release_netdev;
852 }
853
854 context = kzalloc(TLS_OFFLOAD_CONTEXT_SIZE_RX, GFP_KERNEL);
855 if (!context) {
856 rc = -ENOMEM;
857 goto release_netdev;
858 }
859
860 ctx->priv_ctx_rx = context;
861 rc = tls_set_sw_offload(sk, ctx, 0);
862 if (rc)
863 goto release_ctx;
864
865 rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
866 &ctx->crypto_recv,
867 tcp_sk(sk)->copied_seq);
868 if (rc) {
869 pr_err_ratelimited("%s: The netdev has refused to offload this socket\n",
870 __func__);
871 goto free_sw_resources;
872 }
873
874 tls_device_attach(ctx, sk, netdev);
875 goto release_netdev;
876
877free_sw_resources:
878 tls_sw_free_resources_rx(sk);
879release_ctx:
880 ctx->priv_ctx_rx = NULL;
881release_netdev:
882 dev_put(netdev);
883release_lock:
884 up_read(&device_offload_lock);
885 return rc;
886}
887
888void tls_device_offload_cleanup_rx(struct sock *sk)
889{
890 struct tls_context *tls_ctx = tls_get_ctx(sk);
891 struct net_device *netdev;
892
893 down_read(&device_offload_lock);
894 netdev = tls_ctx->netdev;
895 if (!netdev)
896 goto out;
897
898 if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
899 pr_err_ratelimited("%s: device is missing NETIF_F_HW_TLS_RX cap\n",
900 __func__);
901 goto out;
902 }
903
904 netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
905 TLS_OFFLOAD_CTX_DIR_RX);
906
907 if (tls_ctx->tx_conf != TLS_HW) {
908 dev_put(netdev);
909 tls_ctx->netdev = NULL;
910 }
911out:
912 up_read(&device_offload_lock);
913 kfree(tls_ctx->rx.rec_seq);
914 kfree(tls_ctx->rx.iv);
915 tls_sw_release_resources_rx(sk);
916}
917
692static int tls_device_down(struct net_device *netdev) 918static int tls_device_down(struct net_device *netdev)
693{ 919{
694 struct tls_context *ctx, *tmp; 920 struct tls_context *ctx, *tmp;
@@ -709,8 +935,12 @@ static int tls_device_down(struct net_device *netdev)
709 spin_unlock_irqrestore(&tls_device_lock, flags); 935 spin_unlock_irqrestore(&tls_device_lock, flags);
710 936
711 list_for_each_entry_safe(ctx, tmp, &list, list) { 937 list_for_each_entry_safe(ctx, tmp, &list, list) {
712 netdev->tlsdev_ops->tls_dev_del(netdev, ctx, 938 if (ctx->tx_conf == TLS_HW)
713 TLS_OFFLOAD_CTX_DIR_TX); 939 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
940 TLS_OFFLOAD_CTX_DIR_TX);
941 if (ctx->rx_conf == TLS_HW)
942 netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
943 TLS_OFFLOAD_CTX_DIR_RX);
714 ctx->netdev = NULL; 944 ctx->netdev = NULL;
715 dev_put(netdev); 945 dev_put(netdev);
716 list_del_init(&ctx->list); 946 list_del_init(&ctx->list);
@@ -731,12 +961,16 @@ static int tls_dev_event(struct notifier_block *this, unsigned long event,
731{ 961{
732 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 962 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
733 963
734 if (!(dev->features & NETIF_F_HW_TLS_TX)) 964 if (!(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
735 return NOTIFY_DONE; 965 return NOTIFY_DONE;
736 966
737 switch (event) { 967 switch (event) {
738 case NETDEV_REGISTER: 968 case NETDEV_REGISTER:
739 case NETDEV_FEAT_CHANGE: 969 case NETDEV_FEAT_CHANGE:
970 if ((dev->features & NETIF_F_HW_TLS_RX) &&
971 !dev->tlsdev_ops->tls_dev_resync_rx)
972 return NOTIFY_BAD;
973
740 if (dev->tlsdev_ops && 974 if (dev->tlsdev_ops &&
741 dev->tlsdev_ops->tls_dev_add && 975 dev->tlsdev_ops->tls_dev_add &&
742 dev->tlsdev_ops->tls_dev_del) 976 dev->tlsdev_ops->tls_dev_del)