Skip to content

Commit dd0bed1

Browse files
chelsiocryptodavem330
authored andcommitted
tls: support for Inline tls record
Facility to register Inline TLS drivers to net/tls. Setup TLS_HW_RECORD prot to listen on offload device. Cases handled - Inline TLS device exists, setup prot for TLS_HW_RECORD - Atleast one Inline TLS exists, sets TLS_HW_RECORD. - If non-inline device establish connection, move to TLS_SW_TX Signed-off-by: Atul Gupta <atul.gupta@chelsio.com> Reviewed-by: Steve Wise <swise@opengridcomputing.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d4069fe commit dd0bed1

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

include/net/tls.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@
5656
#define TLS_RECORD_TYPE_DATA 0x17
5757

5858
#define TLS_AAD_SPACE_SIZE 13
59+
#define TLS_DEVICE_NAME_MAX 32
60+
61+
/*
62+
* This structure defines the routines for Inline TLS driver.
63+
* The following routines are optional and filled with a
64+
* null pointer if not defined.
65+
*
66+
* @name: Its the name of registered Inline tls device
67+
* @dev_list: Inline tls device list
68+
* int (*feature)(struct tls_device *device);
69+
* Called to return Inline TLS driver capability
70+
*
71+
* int (*hash)(struct tls_device *device, struct sock *sk);
72+
* This function sets Inline driver for listen and program
73+
* device specific functioanlity as required
74+
*
75+
* void (*unhash)(struct tls_device *device, struct sock *sk);
76+
* This function cleans listen state set by Inline TLS driver
77+
*/
78+
struct tls_device {
79+
char name[TLS_DEVICE_NAME_MAX];
80+
struct list_head dev_list;
81+
int (*feature)(struct tls_device *device);
82+
int (*hash)(struct tls_device *device, struct sock *sk);
83+
void (*unhash)(struct tls_device *device, struct sock *sk);
84+
};
5985

6086
struct tls_sw_context {
6187
struct crypto_aead *aead_send;
@@ -114,7 +140,7 @@ struct tls_context {
114140

115141
void *priv_ctx;
116142

117-
u8 conf:2;
143+
u8 conf:3;
118144

119145
struct cipher_context tx;
120146
struct cipher_context rx;
@@ -135,6 +161,8 @@ struct tls_context {
135161
int (*getsockopt)(struct sock *sk, int level,
136162
int optname, char __user *optval,
137163
int __user *optlen);
164+
int (*hash)(struct sock *sk);
165+
void (*unhash)(struct sock *sk);
138166
};
139167

140168
int wait_on_pending_writer(struct sock *sk, long *timeo);
@@ -283,5 +311,7 @@ static inline struct tls_offload_context *tls_offload_ctx(
283311

284312
int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
285313
unsigned char *record_type);
314+
void tls_register_device(struct tls_device *device);
315+
void tls_unregister_device(struct tls_device *device);
286316

287317
#endif /* _TLS_OFFLOAD_H */

net/tls/tls_main.c

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/highmem.h>
3939
#include <linux/netdevice.h>
4040
#include <linux/sched/signal.h>
41+
#include <linux/inetdevice.h>
4142

4243
#include <net/tls.h>
4344

@@ -56,11 +57,14 @@ enum {
5657
TLS_SW_TX,
5758
TLS_SW_RX,
5859
TLS_SW_RXTX,
60+
TLS_HW_RECORD,
5961
TLS_NUM_CONFIG,
6062
};
6163

6264
static struct proto *saved_tcpv6_prot;
6365
static DEFINE_MUTEX(tcpv6_prot_mutex);
66+
static LIST_HEAD(device_list);
67+
static DEFINE_MUTEX(device_mutex);
6468
static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG];
6569
static struct proto_ops tls_sw_proto_ops;
6670

@@ -241,8 +245,12 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
241245
lock_sock(sk);
242246
sk_proto_close = ctx->sk_proto_close;
243247

248+
if (ctx->conf == TLS_HW_RECORD)
249+
goto skip_tx_cleanup;
250+
244251
if (ctx->conf == TLS_BASE) {
245252
kfree(ctx);
253+
ctx = NULL;
246254
goto skip_tx_cleanup;
247255
}
248256

@@ -276,6 +284,11 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
276284
skip_tx_cleanup:
277285
release_sock(sk);
278286
sk_proto_close(sk, timeout);
287+
/* free ctx for TLS_HW_RECORD, used by tcp_set_state
288+
* for sk->sk_prot->unhash [tls_hw_unhash]
289+
*/
290+
if (ctx && ctx->conf == TLS_HW_RECORD)
291+
kfree(ctx);
279292
}
280293

281294
static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
@@ -493,6 +506,79 @@ static int tls_setsockopt(struct sock *sk, int level, int optname,
493506
return do_tls_setsockopt(sk, optname, optval, optlen);
494507
}
495508

509+
static struct tls_context *create_ctx(struct sock *sk)
510+
{
511+
struct inet_connection_sock *icsk = inet_csk(sk);
512+
struct tls_context *ctx;
513+
514+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
515+
if (!ctx)
516+
return NULL;
517+
518+
icsk->icsk_ulp_data = ctx;
519+
return ctx;
520+
}
521+
522+
static int tls_hw_prot(struct sock *sk)
523+
{
524+
struct tls_context *ctx;
525+
struct tls_device *dev;
526+
int rc = 0;
527+
528+
mutex_lock(&device_mutex);
529+
list_for_each_entry(dev, &device_list, dev_list) {
530+
if (dev->feature && dev->feature(dev)) {
531+
ctx = create_ctx(sk);
532+
if (!ctx)
533+
goto out;
534+
535+
ctx->hash = sk->sk_prot->hash;
536+
ctx->unhash = sk->sk_prot->unhash;
537+
ctx->sk_proto_close = sk->sk_prot->close;
538+
ctx->conf = TLS_HW_RECORD;
539+
update_sk_prot(sk, ctx);
540+
rc = 1;
541+
break;
542+
}
543+
}
544+
out:
545+
mutex_unlock(&device_mutex);
546+
return rc;
547+
}
548+
549+
static void tls_hw_unhash(struct sock *sk)
550+
{
551+
struct tls_context *ctx = tls_get_ctx(sk);
552+
struct tls_device *dev;
553+
554+
mutex_lock(&device_mutex);
555+
list_for_each_entry(dev, &device_list, dev_list) {
556+
if (dev->unhash)
557+
dev->unhash(dev, sk);
558+
}
559+
mutex_unlock(&device_mutex);
560+
ctx->unhash(sk);
561+
}
562+
563+
static int tls_hw_hash(struct sock *sk)
564+
{
565+
struct tls_context *ctx = tls_get_ctx(sk);
566+
struct tls_device *dev;
567+
int err;
568+
569+
err = ctx->hash(sk);
570+
mutex_lock(&device_mutex);
571+
list_for_each_entry(dev, &device_list, dev_list) {
572+
if (dev->hash)
573+
err |= dev->hash(dev, sk);
574+
}
575+
mutex_unlock(&device_mutex);
576+
577+
if (err)
578+
tls_hw_unhash(sk);
579+
return err;
580+
}
581+
496582
static void build_protos(struct proto *prot, struct proto *base)
497583
{
498584
prot[TLS_BASE] = *base;
@@ -511,15 +597,22 @@ static void build_protos(struct proto *prot, struct proto *base)
511597
prot[TLS_SW_RXTX] = prot[TLS_SW_TX];
512598
prot[TLS_SW_RXTX].recvmsg = tls_sw_recvmsg;
513599
prot[TLS_SW_RXTX].close = tls_sk_proto_close;
600+
601+
prot[TLS_HW_RECORD] = *base;
602+
prot[TLS_HW_RECORD].hash = tls_hw_hash;
603+
prot[TLS_HW_RECORD].unhash = tls_hw_unhash;
604+
prot[TLS_HW_RECORD].close = tls_sk_proto_close;
514605
}
515606

516607
static int tls_init(struct sock *sk)
517608
{
518609
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
519-
struct inet_connection_sock *icsk = inet_csk(sk);
520610
struct tls_context *ctx;
521611
int rc = 0;
522612

613+
if (tls_hw_prot(sk))
614+
goto out;
615+
523616
/* The TLS ulp is currently supported only for TCP sockets
524617
* in ESTABLISHED state.
525618
* Supporting sockets in LISTEN state will require us
@@ -530,12 +623,11 @@ static int tls_init(struct sock *sk)
530623
return -ENOTSUPP;
531624

532625
/* allocate tls context */
533-
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
626+
ctx = create_ctx(sk);
534627
if (!ctx) {
535628
rc = -ENOMEM;
536629
goto out;
537630
}
538-
icsk->icsk_ulp_data = ctx;
539631
ctx->setsockopt = sk->sk_prot->setsockopt;
540632
ctx->getsockopt = sk->sk_prot->getsockopt;
541633
ctx->sk_proto_close = sk->sk_prot->close;
@@ -557,6 +649,22 @@ static int tls_init(struct sock *sk)
557649
return rc;
558650
}
559651

652+
void tls_register_device(struct tls_device *device)
653+
{
654+
mutex_lock(&device_mutex);
655+
list_add_tail(&device->dev_list, &device_list);
656+
mutex_unlock(&device_mutex);
657+
}
658+
EXPORT_SYMBOL(tls_register_device);
659+
660+
void tls_unregister_device(struct tls_device *device)
661+
{
662+
mutex_lock(&device_mutex);
663+
list_del(&device->dev_list);
664+
mutex_unlock(&device_mutex);
665+
}
666+
EXPORT_SYMBOL(tls_unregister_device);
667+
560668
static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
561669
.name = "tls",
562670
.uid = TCP_ULP_TLS,

0 commit comments

Comments
 (0)