tls_toe.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016-2017, Dave Watson <[email protected]>. 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. #include <linux/list.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/spinlock.h>
  36. #include <net/inet_connection_sock.h>
  37. #include <net/tls.h>
  38. #include <net/tls_toe.h>
  39. #include "tls.h"
  40. static LIST_HEAD(device_list);
  41. static DEFINE_SPINLOCK(device_spinlock);
  42. static void tls_toe_sk_destruct(struct sock *sk)
  43. {
  44. struct inet_connection_sock *icsk = inet_csk(sk);
  45. struct tls_context *ctx = tls_get_ctx(sk);
  46. ctx->sk_destruct(sk);
  47. /* Free ctx */
  48. rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
  49. tls_ctx_free(sk, ctx);
  50. }
  51. int tls_toe_bypass(struct sock *sk)
  52. {
  53. struct tls_toe_device *dev;
  54. struct tls_context *ctx;
  55. int rc = 0;
  56. spin_lock_bh(&device_spinlock);
  57. list_for_each_entry(dev, &device_list, dev_list) {
  58. if (dev->feature && dev->feature(dev)) {
  59. ctx = tls_ctx_create(sk);
  60. if (!ctx)
  61. goto out;
  62. ctx->sk_destruct = sk->sk_destruct;
  63. sk->sk_destruct = tls_toe_sk_destruct;
  64. ctx->rx_conf = TLS_HW_RECORD;
  65. ctx->tx_conf = TLS_HW_RECORD;
  66. update_sk_prot(sk, ctx);
  67. rc = 1;
  68. break;
  69. }
  70. }
  71. out:
  72. spin_unlock_bh(&device_spinlock);
  73. return rc;
  74. }
  75. void tls_toe_unhash(struct sock *sk)
  76. {
  77. struct tls_context *ctx = tls_get_ctx(sk);
  78. struct tls_toe_device *dev;
  79. spin_lock_bh(&device_spinlock);
  80. list_for_each_entry(dev, &device_list, dev_list) {
  81. if (dev->unhash) {
  82. kref_get(&dev->kref);
  83. spin_unlock_bh(&device_spinlock);
  84. dev->unhash(dev, sk);
  85. kref_put(&dev->kref, dev->release);
  86. spin_lock_bh(&device_spinlock);
  87. }
  88. }
  89. spin_unlock_bh(&device_spinlock);
  90. ctx->sk_proto->unhash(sk);
  91. }
  92. int tls_toe_hash(struct sock *sk)
  93. {
  94. struct tls_context *ctx = tls_get_ctx(sk);
  95. struct tls_toe_device *dev;
  96. int err;
  97. err = ctx->sk_proto->hash(sk);
  98. spin_lock_bh(&device_spinlock);
  99. list_for_each_entry(dev, &device_list, dev_list) {
  100. if (dev->hash) {
  101. kref_get(&dev->kref);
  102. spin_unlock_bh(&device_spinlock);
  103. err |= dev->hash(dev, sk);
  104. kref_put(&dev->kref, dev->release);
  105. spin_lock_bh(&device_spinlock);
  106. }
  107. }
  108. spin_unlock_bh(&device_spinlock);
  109. if (err)
  110. tls_toe_unhash(sk);
  111. return err;
  112. }
  113. void tls_toe_register_device(struct tls_toe_device *device)
  114. {
  115. spin_lock_bh(&device_spinlock);
  116. list_add_tail(&device->dev_list, &device_list);
  117. spin_unlock_bh(&device_spinlock);
  118. }
  119. EXPORT_SYMBOL(tls_toe_register_device);
  120. void tls_toe_unregister_device(struct tls_toe_device *device)
  121. {
  122. spin_lock_bh(&device_spinlock);
  123. list_del(&device->dev_list);
  124. spin_unlock_bh(&device_spinlock);
  125. }
  126. EXPORT_SYMBOL(tls_toe_unregister_device);