18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
68c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
98c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
128c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
138c2ecf20Sopenharmony_ci *     conditions are met:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
168c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
178c2ecf20Sopenharmony_ci *        disclaimer.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
208c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
218c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
228c2ecf20Sopenharmony_ci *        provided with the distribution.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318c2ecf20Sopenharmony_ci * SOFTWARE.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <linux/list.h>
358c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
368c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
378c2ecf20Sopenharmony_ci#include <net/inet_connection_sock.h>
388c2ecf20Sopenharmony_ci#include <net/tls.h>
398c2ecf20Sopenharmony_ci#include <net/tls_toe.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic LIST_HEAD(device_list);
428c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(device_spinlock);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic void tls_toe_sk_destruct(struct sock *sk)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct inet_connection_sock *icsk = inet_csk(sk);
478c2ecf20Sopenharmony_ci	struct tls_context *ctx = tls_get_ctx(sk);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	ctx->sk_destruct(sk);
508c2ecf20Sopenharmony_ci	/* Free ctx */
518c2ecf20Sopenharmony_ci	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
528c2ecf20Sopenharmony_ci	tls_ctx_free(sk, ctx);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciint tls_toe_bypass(struct sock *sk)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct tls_toe_device *dev;
588c2ecf20Sopenharmony_ci	struct tls_context *ctx;
598c2ecf20Sopenharmony_ci	int rc = 0;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	spin_lock_bh(&device_spinlock);
628c2ecf20Sopenharmony_ci	list_for_each_entry(dev, &device_list, dev_list) {
638c2ecf20Sopenharmony_ci		if (dev->feature && dev->feature(dev)) {
648c2ecf20Sopenharmony_ci			ctx = tls_ctx_create(sk);
658c2ecf20Sopenharmony_ci			if (!ctx)
668c2ecf20Sopenharmony_ci				goto out;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci			ctx->sk_destruct = sk->sk_destruct;
698c2ecf20Sopenharmony_ci			sk->sk_destruct = tls_toe_sk_destruct;
708c2ecf20Sopenharmony_ci			ctx->rx_conf = TLS_HW_RECORD;
718c2ecf20Sopenharmony_ci			ctx->tx_conf = TLS_HW_RECORD;
728c2ecf20Sopenharmony_ci			update_sk_prot(sk, ctx);
738c2ecf20Sopenharmony_ci			rc = 1;
748c2ecf20Sopenharmony_ci			break;
758c2ecf20Sopenharmony_ci		}
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ciout:
788c2ecf20Sopenharmony_ci	spin_unlock_bh(&device_spinlock);
798c2ecf20Sopenharmony_ci	return rc;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_civoid tls_toe_unhash(struct sock *sk)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct tls_context *ctx = tls_get_ctx(sk);
858c2ecf20Sopenharmony_ci	struct tls_toe_device *dev;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	spin_lock_bh(&device_spinlock);
888c2ecf20Sopenharmony_ci	list_for_each_entry(dev, &device_list, dev_list) {
898c2ecf20Sopenharmony_ci		if (dev->unhash) {
908c2ecf20Sopenharmony_ci			kref_get(&dev->kref);
918c2ecf20Sopenharmony_ci			spin_unlock_bh(&device_spinlock);
928c2ecf20Sopenharmony_ci			dev->unhash(dev, sk);
938c2ecf20Sopenharmony_ci			kref_put(&dev->kref, dev->release);
948c2ecf20Sopenharmony_ci			spin_lock_bh(&device_spinlock);
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci	spin_unlock_bh(&device_spinlock);
988c2ecf20Sopenharmony_ci	ctx->sk_proto->unhash(sk);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciint tls_toe_hash(struct sock *sk)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct tls_context *ctx = tls_get_ctx(sk);
1048c2ecf20Sopenharmony_ci	struct tls_toe_device *dev;
1058c2ecf20Sopenharmony_ci	int err;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	err = ctx->sk_proto->hash(sk);
1088c2ecf20Sopenharmony_ci	spin_lock_bh(&device_spinlock);
1098c2ecf20Sopenharmony_ci	list_for_each_entry(dev, &device_list, dev_list) {
1108c2ecf20Sopenharmony_ci		if (dev->hash) {
1118c2ecf20Sopenharmony_ci			kref_get(&dev->kref);
1128c2ecf20Sopenharmony_ci			spin_unlock_bh(&device_spinlock);
1138c2ecf20Sopenharmony_ci			err |= dev->hash(dev, sk);
1148c2ecf20Sopenharmony_ci			kref_put(&dev->kref, dev->release);
1158c2ecf20Sopenharmony_ci			spin_lock_bh(&device_spinlock);
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	spin_unlock_bh(&device_spinlock);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (err)
1218c2ecf20Sopenharmony_ci		tls_toe_unhash(sk);
1228c2ecf20Sopenharmony_ci	return err;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_civoid tls_toe_register_device(struct tls_toe_device *device)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	spin_lock_bh(&device_spinlock);
1288c2ecf20Sopenharmony_ci	list_add_tail(&device->dev_list, &device_list);
1298c2ecf20Sopenharmony_ci	spin_unlock_bh(&device_spinlock);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tls_toe_register_device);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_civoid tls_toe_unregister_device(struct tls_toe_device *device)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	spin_lock_bh(&device_spinlock);
1368c2ecf20Sopenharmony_ci	list_del(&device->dev_list);
1378c2ecf20Sopenharmony_ci	spin_unlock_bh(&device_spinlock);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tls_toe_unregister_device);
140