162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. 362306a36Sopenharmony_ci * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This software is available to you under a choice of one of two 662306a36Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the 962306a36Sopenharmony_ci * OpenIB.org BSD license below: 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or 1262306a36Sopenharmony_ci * without modification, are permitted provided that the following 1362306a36Sopenharmony_ci * conditions are met: 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * - Redistributions of source code must retain the above 1662306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 1762306a36Sopenharmony_ci * disclaimer. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * - Redistributions in binary form must reproduce the above 2062306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2162306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials 2262306a36Sopenharmony_ci * provided with the distribution. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3162306a36Sopenharmony_ci * SOFTWARE. 3262306a36Sopenharmony_ci */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#include <linux/list.h> 3562306a36Sopenharmony_ci#include <linux/rcupdate.h> 3662306a36Sopenharmony_ci#include <linux/spinlock.h> 3762306a36Sopenharmony_ci#include <net/inet_connection_sock.h> 3862306a36Sopenharmony_ci#include <net/tls.h> 3962306a36Sopenharmony_ci#include <net/tls_toe.h> 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#include "tls.h" 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic LIST_HEAD(device_list); 4462306a36Sopenharmony_cistatic DEFINE_SPINLOCK(device_spinlock); 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic void tls_toe_sk_destruct(struct sock *sk) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 4962306a36Sopenharmony_ci struct tls_context *ctx = tls_get_ctx(sk); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci ctx->sk_destruct(sk); 5262306a36Sopenharmony_ci /* Free ctx */ 5362306a36Sopenharmony_ci rcu_assign_pointer(icsk->icsk_ulp_data, NULL); 5462306a36Sopenharmony_ci tls_ctx_free(sk, ctx); 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciint tls_toe_bypass(struct sock *sk) 5862306a36Sopenharmony_ci{ 5962306a36Sopenharmony_ci struct tls_toe_device *dev; 6062306a36Sopenharmony_ci struct tls_context *ctx; 6162306a36Sopenharmony_ci int rc = 0; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 6462306a36Sopenharmony_ci list_for_each_entry(dev, &device_list, dev_list) { 6562306a36Sopenharmony_ci if (dev->feature && dev->feature(dev)) { 6662306a36Sopenharmony_ci ctx = tls_ctx_create(sk); 6762306a36Sopenharmony_ci if (!ctx) 6862306a36Sopenharmony_ci goto out; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci ctx->sk_destruct = sk->sk_destruct; 7162306a36Sopenharmony_ci sk->sk_destruct = tls_toe_sk_destruct; 7262306a36Sopenharmony_ci ctx->rx_conf = TLS_HW_RECORD; 7362306a36Sopenharmony_ci ctx->tx_conf = TLS_HW_RECORD; 7462306a36Sopenharmony_ci update_sk_prot(sk, ctx); 7562306a36Sopenharmony_ci rc = 1; 7662306a36Sopenharmony_ci break; 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci } 7962306a36Sopenharmony_ciout: 8062306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 8162306a36Sopenharmony_ci return rc; 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_civoid tls_toe_unhash(struct sock *sk) 8562306a36Sopenharmony_ci{ 8662306a36Sopenharmony_ci struct tls_context *ctx = tls_get_ctx(sk); 8762306a36Sopenharmony_ci struct tls_toe_device *dev; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 9062306a36Sopenharmony_ci list_for_each_entry(dev, &device_list, dev_list) { 9162306a36Sopenharmony_ci if (dev->unhash) { 9262306a36Sopenharmony_ci kref_get(&dev->kref); 9362306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 9462306a36Sopenharmony_ci dev->unhash(dev, sk); 9562306a36Sopenharmony_ci kref_put(&dev->kref, dev->release); 9662306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 9762306a36Sopenharmony_ci } 9862306a36Sopenharmony_ci } 9962306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 10062306a36Sopenharmony_ci ctx->sk_proto->unhash(sk); 10162306a36Sopenharmony_ci} 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ciint tls_toe_hash(struct sock *sk) 10462306a36Sopenharmony_ci{ 10562306a36Sopenharmony_ci struct tls_context *ctx = tls_get_ctx(sk); 10662306a36Sopenharmony_ci struct tls_toe_device *dev; 10762306a36Sopenharmony_ci int err; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci err = ctx->sk_proto->hash(sk); 11062306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 11162306a36Sopenharmony_ci list_for_each_entry(dev, &device_list, dev_list) { 11262306a36Sopenharmony_ci if (dev->hash) { 11362306a36Sopenharmony_ci kref_get(&dev->kref); 11462306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 11562306a36Sopenharmony_ci err |= dev->hash(dev, sk); 11662306a36Sopenharmony_ci kref_put(&dev->kref, dev->release); 11762306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci } 12062306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci if (err) 12362306a36Sopenharmony_ci tls_toe_unhash(sk); 12462306a36Sopenharmony_ci return err; 12562306a36Sopenharmony_ci} 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_civoid tls_toe_register_device(struct tls_toe_device *device) 12862306a36Sopenharmony_ci{ 12962306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 13062306a36Sopenharmony_ci list_add_tail(&device->dev_list, &device_list); 13162306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 13262306a36Sopenharmony_ci} 13362306a36Sopenharmony_ciEXPORT_SYMBOL(tls_toe_register_device); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_civoid tls_toe_unregister_device(struct tls_toe_device *device) 13662306a36Sopenharmony_ci{ 13762306a36Sopenharmony_ci spin_lock_bh(&device_spinlock); 13862306a36Sopenharmony_ci list_del(&device->dev_list); 13962306a36Sopenharmony_ci spin_unlock_bh(&device_spinlock); 14062306a36Sopenharmony_ci} 14162306a36Sopenharmony_ciEXPORT_SYMBOL(tls_toe_unregister_device); 142