tcp_ulp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 struct tcp_ulp_ops *tcp_ulp_find_id(const int ulp)
  27. {
  28. struct tcp_ulp_ops *e;
  29. list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
  30. if (e->uid == ulp)
  31. return e;
  32. }
  33. return NULL;
  34. }
  35. static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
  36. {
  37. const struct tcp_ulp_ops *ulp = NULL;
  38. rcu_read_lock();
  39. ulp = tcp_ulp_find(name);
  40. #ifdef CONFIG_MODULES
  41. if (!ulp && capable(CAP_NET_ADMIN)) {
  42. rcu_read_unlock();
  43. request_module("%s", name);
  44. rcu_read_lock();
  45. ulp = tcp_ulp_find(name);
  46. }
  47. #endif
  48. if (!ulp || !try_module_get(ulp->owner))
  49. ulp = NULL;
  50. rcu_read_unlock();
  51. return ulp;
  52. }
  53. static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
  54. {
  55. const struct tcp_ulp_ops *ulp;
  56. rcu_read_lock();
  57. ulp = tcp_ulp_find_id(uid);
  58. if (!ulp || !try_module_get(ulp->owner))
  59. ulp = NULL;
  60. rcu_read_unlock();
  61. return ulp;
  62. }
  63. /* Attach new upper layer protocol to the list
  64. * of available protocols.
  65. */
  66. int tcp_register_ulp(struct tcp_ulp_ops *ulp)
  67. {
  68. int ret = 0;
  69. spin_lock(&tcp_ulp_list_lock);
  70. if (tcp_ulp_find(ulp->name))
  71. ret = -EEXIST;
  72. else
  73. list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
  74. spin_unlock(&tcp_ulp_list_lock);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL_GPL(tcp_register_ulp);
  78. void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
  79. {
  80. spin_lock(&tcp_ulp_list_lock);
  81. list_del_rcu(&ulp->list);
  82. spin_unlock(&tcp_ulp_list_lock);
  83. synchronize_rcu();
  84. }
  85. EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
  86. /* Build string with list of available upper layer protocl values */
  87. void tcp_get_available_ulp(char *buf, size_t maxlen)
  88. {
  89. struct tcp_ulp_ops *ulp_ops;
  90. size_t offs = 0;
  91. *buf = '\0';
  92. rcu_read_lock();
  93. list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
  94. offs += snprintf(buf + offs, maxlen - offs,
  95. "%s%s",
  96. offs == 0 ? "" : " ", ulp_ops->name);
  97. }
  98. rcu_read_unlock();
  99. }
  100. void tcp_cleanup_ulp(struct sock *sk)
  101. {
  102. struct inet_connection_sock *icsk = inet_csk(sk);
  103. if (!icsk->icsk_ulp_ops)
  104. return;
  105. if (icsk->icsk_ulp_ops->release)
  106. icsk->icsk_ulp_ops->release(sk);
  107. module_put(icsk->icsk_ulp_ops->owner);
  108. }
  109. /* Change upper layer protocol for socket */
  110. int tcp_set_ulp(struct sock *sk, const char *name)
  111. {
  112. struct inet_connection_sock *icsk = inet_csk(sk);
  113. const struct tcp_ulp_ops *ulp_ops;
  114. int err = 0;
  115. if (icsk->icsk_ulp_ops)
  116. return -EEXIST;
  117. ulp_ops = __tcp_ulp_find_autoload(name);
  118. if (!ulp_ops)
  119. return -ENOENT;
  120. if (!ulp_ops->user_visible) {
  121. module_put(ulp_ops->owner);
  122. return -ENOENT;
  123. }
  124. err = ulp_ops->init(sk);
  125. if (err) {
  126. module_put(ulp_ops->owner);
  127. return err;
  128. }
  129. icsk->icsk_ulp_ops = ulp_ops;
  130. return 0;
  131. }
  132. int tcp_set_ulp_id(struct sock *sk, int ulp)
  133. {
  134. struct inet_connection_sock *icsk = inet_csk(sk);
  135. const struct tcp_ulp_ops *ulp_ops;
  136. int err;
  137. if (icsk->icsk_ulp_ops)
  138. return -EEXIST;
  139. ulp_ops = __tcp_ulp_lookup(ulp);
  140. if (!ulp_ops)
  141. return -ENOENT;
  142. err = ulp_ops->init(sk);
  143. if (err) {
  144. module_put(ulp_ops->owner);
  145. return err;
  146. }
  147. icsk->icsk_ulp_ops = ulp_ops;
  148. return 0;
  149. }