tcp_ulp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Pluggable TCP upper layer protocol support.
  4. *
  5. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2016-2017, Dave Watson <[email protected]>. All rights reserved.
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <linux/gfp.h>
  14. #include <net/tcp.h>
  15. static DEFINE_SPINLOCK(tcp_ulp_list_lock);
  16. static LIST_HEAD(tcp_ulp_list);
  17. /* Simple linear search, don't expect many entries! */
  18. static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
  19. {
  20. struct tcp_ulp_ops *e;
  21. list_for_each_entry_rcu(e, &tcp_ulp_list, list,
  22. lockdep_is_held(&tcp_ulp_list_lock)) {
  23. if (strcmp(e->name, name) == 0)
  24. return e;
  25. }
  26. return NULL;
  27. }
  28. static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
  29. {
  30. const struct tcp_ulp_ops *ulp = NULL;
  31. rcu_read_lock();
  32. ulp = tcp_ulp_find(name);
  33. #ifdef CONFIG_MODULES
  34. if (!ulp && capable(CAP_NET_ADMIN)) {
  35. rcu_read_unlock();
  36. request_module("tcp-ulp-%s", name);
  37. rcu_read_lock();
  38. ulp = tcp_ulp_find(name);
  39. }
  40. #endif
  41. if (!ulp || !try_module_get(ulp->owner))
  42. ulp = NULL;
  43. rcu_read_unlock();
  44. return ulp;
  45. }
  46. /* Attach new upper layer protocol to the list
  47. * of available protocols.
  48. */
  49. int tcp_register_ulp(struct tcp_ulp_ops *ulp)
  50. {
  51. int ret = 0;
  52. spin_lock(&tcp_ulp_list_lock);
  53. if (tcp_ulp_find(ulp->name))
  54. ret = -EEXIST;
  55. else
  56. list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
  57. spin_unlock(&tcp_ulp_list_lock);
  58. return ret;
  59. }
  60. EXPORT_SYMBOL_GPL(tcp_register_ulp);
  61. void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
  62. {
  63. spin_lock(&tcp_ulp_list_lock);
  64. list_del_rcu(&ulp->list);
  65. spin_unlock(&tcp_ulp_list_lock);
  66. synchronize_rcu();
  67. }
  68. EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
  69. /* Build string with list of available upper layer protocl values */
  70. void tcp_get_available_ulp(char *buf, size_t maxlen)
  71. {
  72. struct tcp_ulp_ops *ulp_ops;
  73. size_t offs = 0;
  74. *buf = '\0';
  75. rcu_read_lock();
  76. list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
  77. offs += snprintf(buf + offs, maxlen - offs,
  78. "%s%s",
  79. offs == 0 ? "" : " ", ulp_ops->name);
  80. if (WARN_ON_ONCE(offs >= maxlen))
  81. break;
  82. }
  83. rcu_read_unlock();
  84. }
  85. void tcp_update_ulp(struct sock *sk, struct proto *proto,
  86. void (*write_space)(struct sock *sk))
  87. {
  88. struct inet_connection_sock *icsk = inet_csk(sk);
  89. if (icsk->icsk_ulp_ops->update)
  90. icsk->icsk_ulp_ops->update(sk, proto, write_space);
  91. }
  92. void tcp_cleanup_ulp(struct sock *sk)
  93. {
  94. struct inet_connection_sock *icsk = inet_csk(sk);
  95. /* No sock_owned_by_me() check here as at the time the
  96. * stack calls this function, the socket is dead and
  97. * about to be destroyed.
  98. */
  99. if (!icsk->icsk_ulp_ops)
  100. return;
  101. if (icsk->icsk_ulp_ops->release)
  102. icsk->icsk_ulp_ops->release(sk);
  103. module_put(icsk->icsk_ulp_ops->owner);
  104. icsk->icsk_ulp_ops = NULL;
  105. }
  106. static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
  107. {
  108. struct inet_connection_sock *icsk = inet_csk(sk);
  109. int err;
  110. err = -EEXIST;
  111. if (icsk->icsk_ulp_ops)
  112. goto out_err;
  113. if (sk->sk_socket)
  114. clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
  115. err = -ENOTCONN;
  116. if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
  117. goto out_err;
  118. err = ulp_ops->init(sk);
  119. if (err)
  120. goto out_err;
  121. icsk->icsk_ulp_ops = ulp_ops;
  122. return 0;
  123. out_err:
  124. module_put(ulp_ops->owner);
  125. return err;
  126. }
  127. int tcp_set_ulp(struct sock *sk, const char *name)
  128. {
  129. const struct tcp_ulp_ops *ulp_ops;
  130. sock_owned_by_me(sk);
  131. ulp_ops = __tcp_ulp_find_autoload(name);
  132. if (!ulp_ops)
  133. return -ENOENT;
  134. return __tcp_set_ulp(sk, ulp_ops);
  135. }