tcp_ulp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Pluggable TCP upper layer protocol support.
  3. *
  4. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
  6. *
  7. */
  8. #include<linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/types.h>
  11. #include <linux/list.h>
  12. #include <linux/gfp.h>
  13. #include <net/tcp.h>
  14. static DEFINE_SPINLOCK(tcp_ulp_list_lock);
  15. static LIST_HEAD(tcp_ulp_list);
  16. /* Simple linear search, don't expect many entries! */
  17. static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
  18. {
  19. struct tcp_ulp_ops *e;
  20. list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
  21. if (strcmp(e->name, name) == 0)
  22. return e;
  23. }
  24. return NULL;
  25. }
  26. static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
  27. {
  28. const struct tcp_ulp_ops *ulp = NULL;
  29. rcu_read_lock();
  30. ulp = tcp_ulp_find(name);
  31. #ifdef CONFIG_MODULES
  32. if (!ulp && capable(CAP_NET_ADMIN)) {
  33. rcu_read_unlock();
  34. request_module("%s", name);
  35. rcu_read_lock();
  36. ulp = tcp_ulp_find(name);
  37. }
  38. #endif
  39. if (!ulp || !try_module_get(ulp->owner))
  40. ulp = NULL;
  41. rcu_read_unlock();
  42. return ulp;
  43. }
  44. /* Attach new upper layer protocol to the list
  45. * of available protocols.
  46. */
  47. int tcp_register_ulp(struct tcp_ulp_ops *ulp)
  48. {
  49. int ret = 0;
  50. spin_lock(&tcp_ulp_list_lock);
  51. if (tcp_ulp_find(ulp->name)) {
  52. pr_notice("%s already registered or non-unique name\n",
  53. ulp->name);
  54. ret = -EEXIST;
  55. } else {
  56. list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
  57. }
  58. spin_unlock(&tcp_ulp_list_lock);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL_GPL(tcp_register_ulp);
  62. void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
  63. {
  64. spin_lock(&tcp_ulp_list_lock);
  65. list_del_rcu(&ulp->list);
  66. spin_unlock(&tcp_ulp_list_lock);
  67. synchronize_rcu();
  68. }
  69. EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
  70. /* Build string with list of available upper layer protocl values */
  71. void tcp_get_available_ulp(char *buf, size_t maxlen)
  72. {
  73. struct tcp_ulp_ops *ulp_ops;
  74. size_t offs = 0;
  75. *buf = '\0';
  76. rcu_read_lock();
  77. list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
  78. offs += snprintf(buf + offs, maxlen - offs,
  79. "%s%s",
  80. offs == 0 ? "" : " ", ulp_ops->name);
  81. }
  82. rcu_read_unlock();
  83. }
  84. void tcp_cleanup_ulp(struct sock *sk)
  85. {
  86. struct inet_connection_sock *icsk = inet_csk(sk);
  87. if (!icsk->icsk_ulp_ops)
  88. return;
  89. if (icsk->icsk_ulp_ops->release)
  90. icsk->icsk_ulp_ops->release(sk);
  91. module_put(icsk->icsk_ulp_ops->owner);
  92. }
  93. /* Change upper layer protocol for socket */
  94. int tcp_set_ulp(struct sock *sk, const char *name)
  95. {
  96. struct inet_connection_sock *icsk = inet_csk(sk);
  97. const struct tcp_ulp_ops *ulp_ops;
  98. int err = 0;
  99. if (icsk->icsk_ulp_ops)
  100. return -EEXIST;
  101. ulp_ops = __tcp_ulp_find_autoload(name);
  102. if (!ulp_ops)
  103. err = -ENOENT;
  104. else
  105. err = ulp_ops->init(sk);
  106. if (err)
  107. goto out;
  108. icsk->icsk_ulp_ops = ulp_ops;
  109. out:
  110. return err;
  111. }