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