xref: /openbmc/linux/net/tls/tls_toe.c (revision 6726d552)
1 /*
2  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/list.h>
35 #include <linux/rcupdate.h>
36 #include <linux/spinlock.h>
37 #include <net/inet_connection_sock.h>
38 #include <net/tls.h>
39 #include <net/tls_toe.h>
40 
41 #include "tls.h"
42 
43 static LIST_HEAD(device_list);
44 static DEFINE_SPINLOCK(device_spinlock);
45 
46 static void tls_toe_sk_destruct(struct sock *sk)
47 {
48 	struct inet_connection_sock *icsk = inet_csk(sk);
49 	struct tls_context *ctx = tls_get_ctx(sk);
50 
51 	ctx->sk_destruct(sk);
52 	/* Free ctx */
53 	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
54 	tls_ctx_free(sk, ctx);
55 }
56 
57 int tls_toe_bypass(struct sock *sk)
58 {
59 	struct tls_toe_device *dev;
60 	struct tls_context *ctx;
61 	int rc = 0;
62 
63 	spin_lock_bh(&device_spinlock);
64 	list_for_each_entry(dev, &device_list, dev_list) {
65 		if (dev->feature && dev->feature(dev)) {
66 			ctx = tls_ctx_create(sk);
67 			if (!ctx)
68 				goto out;
69 
70 			ctx->sk_destruct = sk->sk_destruct;
71 			sk->sk_destruct = tls_toe_sk_destruct;
72 			ctx->rx_conf = TLS_HW_RECORD;
73 			ctx->tx_conf = TLS_HW_RECORD;
74 			update_sk_prot(sk, ctx);
75 			rc = 1;
76 			break;
77 		}
78 	}
79 out:
80 	spin_unlock_bh(&device_spinlock);
81 	return rc;
82 }
83 
84 void tls_toe_unhash(struct sock *sk)
85 {
86 	struct tls_context *ctx = tls_get_ctx(sk);
87 	struct tls_toe_device *dev;
88 
89 	spin_lock_bh(&device_spinlock);
90 	list_for_each_entry(dev, &device_list, dev_list) {
91 		if (dev->unhash) {
92 			kref_get(&dev->kref);
93 			spin_unlock_bh(&device_spinlock);
94 			dev->unhash(dev, sk);
95 			kref_put(&dev->kref, dev->release);
96 			spin_lock_bh(&device_spinlock);
97 		}
98 	}
99 	spin_unlock_bh(&device_spinlock);
100 	ctx->sk_proto->unhash(sk);
101 }
102 
103 int tls_toe_hash(struct sock *sk)
104 {
105 	struct tls_context *ctx = tls_get_ctx(sk);
106 	struct tls_toe_device *dev;
107 	int err;
108 
109 	err = ctx->sk_proto->hash(sk);
110 	spin_lock_bh(&device_spinlock);
111 	list_for_each_entry(dev, &device_list, dev_list) {
112 		if (dev->hash) {
113 			kref_get(&dev->kref);
114 			spin_unlock_bh(&device_spinlock);
115 			err |= dev->hash(dev, sk);
116 			kref_put(&dev->kref, dev->release);
117 			spin_lock_bh(&device_spinlock);
118 		}
119 	}
120 	spin_unlock_bh(&device_spinlock);
121 
122 	if (err)
123 		tls_toe_unhash(sk);
124 	return err;
125 }
126 
127 void tls_toe_register_device(struct tls_toe_device *device)
128 {
129 	spin_lock_bh(&device_spinlock);
130 	list_add_tail(&device->dev_list, &device_list);
131 	spin_unlock_bh(&device_spinlock);
132 }
133 EXPORT_SYMBOL(tls_toe_register_device);
134 
135 void tls_toe_unregister_device(struct tls_toe_device *device)
136 {
137 	spin_lock_bh(&device_spinlock);
138 	list_del(&device->dev_list);
139 	spin_unlock_bh(&device_spinlock);
140 }
141 EXPORT_SYMBOL(tls_toe_unregister_device);
142