Facility to register Inline TLS drivers to net/tls. Setup
TLS_FULL_HW prot to listen on offload device.

Cases handled
1. Inline TLS device exists, setup prot for TLS_FULL_HW
2. Atleast one Inline TLS exists, sets TLS_FULL_HW. If
non-inline capable device establish connection move to TLS_SW_TX

Signed-off-by: Atul Gupta <atul.gu...@chelsio.com>
---
 net/tls/tls_main.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 105 insertions(+), 10 deletions(-)

diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index b0d5fce..cb9cda1 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -38,6 +38,7 @@
 #include <linux/highmem.h>
 #include <linux/netdevice.h>
 #include <linux/sched/signal.h>
+#include <linux/inetdevice.h>
 
 #include <net/tls.h>
 
@@ -45,13 +46,9 @@
 MODULE_DESCRIPTION("Transport Layer Security Support");
 MODULE_LICENSE("Dual BSD/GPL");
 
-enum {
-       TLS_BASE_TX,
-       TLS_SW_TX,
-       TLS_NUM_CONFIG,
-};
-
-static struct proto tls_prots[TLS_NUM_CONFIG];
+static LIST_HEAD(device_list);
+static DEFINE_MUTEX(device_mutex);
+struct proto tls_prots[TLS_NUM_CONFIG];
 
 static inline void update_sk_prot(struct sock *sk, struct tls_context *ctx)
 {
@@ -228,7 +225,7 @@ static void tls_sk_proto_close(struct sock *sk, long 
timeout)
        lock_sock(sk);
        sk_proto_close = ctx->sk_proto_close;
 
-       if (ctx->tx_conf == TLS_BASE_TX) {
+       if (ctx->tx_conf == TLS_BASE_TX || ctx->tx_conf == TLS_FULL_HW) {
                kfree(ctx);
                goto skip_tx_cleanup;
        }
@@ -450,12 +447,89 @@ static int tls_setsockopt(struct sock *sk, int level, int 
optname,
        return do_tls_setsockopt(sk, optname, optval, optlen);
 }
 
+struct tls_context *create_ctx(struct sock *sk)
+{
+       struct inet_connection_sock *icsk = inet_csk(sk);
+       struct tls_context *ctx;
+
+       /* allocate tls context */
+       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       if (!ctx)
+               return NULL;
+
+       icsk->icsk_ulp_data = ctx;
+       return ctx;
+}
+
+static int tls_hw_prot(struct sock *sk)
+{
+       struct tls_context *ctx;
+       struct tls_device *dev;
+       int rc = 0;
+
+       mutex_lock(&device_mutex);
+       list_for_each_entry(dev, &device_list, dev_list) {
+               if (dev->feature && dev->feature(dev)) {
+                       ctx = create_ctx(sk);
+                       if (!ctx)
+                               goto out;
+
+                       ctx->hash = sk->sk_prot->hash;
+                       ctx->unhash = sk->sk_prot->unhash;
+                       ctx->sk_proto_close = sk->sk_prot->close;
+                       ctx->tx_conf = TLS_FULL_HW;
+                       update_sk_prot(sk, ctx);
+                       rc = 1;
+                       break;
+               }
+       }
+out:
+       mutex_unlock(&device_mutex);
+       return rc;
+}
+
+static void tls_hw_unhash(struct sock *sk)
+{
+       struct tls_context *ctx = tls_get_ctx(sk);
+       struct tls_device *dev;
+
+       mutex_lock(&device_mutex);
+       list_for_each_entry(dev, &device_list, dev_list) {
+               if (dev->unhash)
+                       dev->unhash(dev, sk);
+       }
+       mutex_unlock(&device_mutex);
+       ctx->unhash(sk);
+}
+
+static int tls_hw_hash(struct sock *sk)
+{
+       struct tls_context *ctx = tls_get_ctx(sk);
+       struct tls_device *dev;
+       int err;
+
+       err = ctx->hash(sk);
+       mutex_lock(&device_mutex);
+       list_for_each_entry(dev, &device_list, dev_list) {
+               if (dev->hash)
+                       err |= dev->hash(dev, sk);
+       }
+       mutex_unlock(&device_mutex);
+
+       if (err)
+               tls_hw_unhash(sk);
+       return err;
+}
+
 static int tls_init(struct sock *sk)
 {
        struct inet_connection_sock *icsk = inet_csk(sk);
        struct tls_context *ctx;
        int rc = 0;
 
+       if (tls_hw_prot(sk))
+               goto out;
+
        /* The TLS ulp is currently supported only for TCP sockets
         * in ESTABLISHED state.
         * Supporting sockets in LISTEN state will require us
@@ -466,12 +540,11 @@ static int tls_init(struct sock *sk)
                return -ENOTSUPP;
 
        /* allocate tls context */
-       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       ctx = create_ctx(sk);
        if (!ctx) {
                rc = -ENOMEM;
                goto out;
        }
-       icsk->icsk_ulp_data = ctx;
        ctx->setsockopt = sk->sk_prot->setsockopt;
        ctx->getsockopt = sk->sk_prot->getsockopt;
        ctx->sk_proto_close = sk->sk_prot->close;
@@ -500,7 +573,28 @@ static void build_protos(struct proto *prot, struct proto 
*base)
        prot[TLS_SW_TX] = prot[TLS_BASE_TX];
        prot[TLS_SW_TX].sendmsg         = tls_sw_sendmsg;
        prot[TLS_SW_TX].sendpage        = tls_sw_sendpage;
+
+       prot[TLS_FULL_HW] = *base;
+       prot[TLS_FULL_HW].hash          = tls_hw_hash;
+       prot[TLS_FULL_HW].unhash        = tls_hw_unhash;
+       prot[TLS_FULL_HW].close         = tls_sk_proto_close;
+}
+
+void tls_register_device(struct tls_device *device)
+{
+       mutex_lock(&device_mutex);
+       list_add_tail(&device->dev_list, &device_list);
+       mutex_unlock(&device_mutex);
+}
+EXPORT_SYMBOL(tls_register_device);
+
+void tls_unregister_device(struct tls_device *device)
+{
+       mutex_lock(&device_mutex);
+       list_del(&device->dev_list);
+       mutex_unlock(&device_mutex);
 }
+EXPORT_SYMBOL(tls_unregister_device);
 
 static int __init tls_register(void)
 {
@@ -510,6 +604,7 @@ static int __init tls_register(void)
 
        return 0;
 }
+EXPORT_SYMBOL(tls_prots);
 
 static void __exit tls_unregister(void)
 {
-- 
1.8.3.1

Reply via email to