ccid.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * net/dccp/ccid.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * CCID infrastructure
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "ccid.h"
  14. static struct ccid_operations *ccids[CCID_MAX];
  15. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  16. static atomic_t ccids_lockct = ATOMIC_INIT(0);
  17. static DEFINE_SPINLOCK(ccids_lock);
  18. /*
  19. * The strategy is: modifications ccids vector are short, do not sleep and
  20. * veeery rare, but read access should be free of any exclusive locks.
  21. */
  22. static void ccids_write_lock(void)
  23. {
  24. spin_lock(&ccids_lock);
  25. while (atomic_read(&ccids_lockct) != 0) {
  26. spin_unlock(&ccids_lock);
  27. yield();
  28. spin_lock(&ccids_lock);
  29. }
  30. }
  31. static inline void ccids_write_unlock(void)
  32. {
  33. spin_unlock(&ccids_lock);
  34. }
  35. static inline void ccids_read_lock(void)
  36. {
  37. atomic_inc(&ccids_lockct);
  38. spin_unlock_wait(&ccids_lock);
  39. }
  40. static inline void ccids_read_unlock(void)
  41. {
  42. atomic_dec(&ccids_lockct);
  43. }
  44. #else
  45. #define ccids_write_lock() do { } while(0)
  46. #define ccids_write_unlock() do { } while(0)
  47. #define ccids_read_lock() do { } while(0)
  48. #define ccids_read_unlock() do { } while(0)
  49. #endif
  50. static kmem_cache_t *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
  51. {
  52. kmem_cache_t *slab;
  53. char slab_name_fmt[32], *slab_name;
  54. va_list args;
  55. va_start(args, fmt);
  56. vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args);
  57. va_end(args);
  58. slab_name = kstrdup(slab_name_fmt, GFP_KERNEL);
  59. if (slab_name == NULL)
  60. return NULL;
  61. slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0,
  62. SLAB_HWCACHE_ALIGN, NULL, NULL);
  63. if (slab == NULL)
  64. kfree(slab_name);
  65. return slab;
  66. }
  67. static void ccid_kmem_cache_destroy(kmem_cache_t *slab)
  68. {
  69. if (slab != NULL) {
  70. const char *name = kmem_cache_name(slab);
  71. kmem_cache_destroy(slab);
  72. kfree(name);
  73. }
  74. }
  75. int ccid_register(struct ccid_operations *ccid_ops)
  76. {
  77. int err = -ENOBUFS;
  78. ccid_ops->ccid_hc_rx_slab =
  79. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  80. "%s_hc_rx_sock",
  81. ccid_ops->ccid_name);
  82. if (ccid_ops->ccid_hc_rx_slab == NULL)
  83. goto out;
  84. ccid_ops->ccid_hc_tx_slab =
  85. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  86. "%s_hc_tx_sock",
  87. ccid_ops->ccid_name);
  88. if (ccid_ops->ccid_hc_tx_slab == NULL)
  89. goto out_free_rx_slab;
  90. ccids_write_lock();
  91. err = -EEXIST;
  92. if (ccids[ccid_ops->ccid_id] == NULL) {
  93. ccids[ccid_ops->ccid_id] = ccid_ops;
  94. err = 0;
  95. }
  96. ccids_write_unlock();
  97. if (err != 0)
  98. goto out_free_tx_slab;
  99. pr_info("CCID: Registered CCID %d (%s)\n",
  100. ccid_ops->ccid_id, ccid_ops->ccid_name);
  101. out:
  102. return err;
  103. out_free_tx_slab:
  104. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  105. ccid_ops->ccid_hc_tx_slab = NULL;
  106. goto out;
  107. out_free_rx_slab:
  108. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  109. ccid_ops->ccid_hc_rx_slab = NULL;
  110. goto out;
  111. }
  112. EXPORT_SYMBOL_GPL(ccid_register);
  113. int ccid_unregister(struct ccid_operations *ccid_ops)
  114. {
  115. ccids_write_lock();
  116. ccids[ccid_ops->ccid_id] = NULL;
  117. ccids_write_unlock();
  118. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  119. ccid_ops->ccid_hc_tx_slab = NULL;
  120. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  121. ccid_ops->ccid_hc_rx_slab = NULL;
  122. pr_info("CCID: Unregistered CCID %d (%s)\n",
  123. ccid_ops->ccid_id, ccid_ops->ccid_name);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(ccid_unregister);
  127. struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp)
  128. {
  129. struct ccid_operations *ccid_ops;
  130. struct ccid *ccid = NULL;
  131. ccids_read_lock();
  132. #ifdef CONFIG_KMOD
  133. if (ccids[id] == NULL) {
  134. /* We only try to load if in process context */
  135. ccids_read_unlock();
  136. if (gfp & GFP_ATOMIC)
  137. goto out;
  138. request_module("net-dccp-ccid-%d", id);
  139. ccids_read_lock();
  140. }
  141. #endif
  142. ccid_ops = ccids[id];
  143. if (ccid_ops == NULL)
  144. goto out_unlock;
  145. if (!try_module_get(ccid_ops->ccid_owner))
  146. goto out_unlock;
  147. ccids_read_unlock();
  148. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  149. ccid_ops->ccid_hc_tx_slab, gfp);
  150. if (ccid == NULL)
  151. goto out_module_put;
  152. ccid->ccid_ops = ccid_ops;
  153. if (rx) {
  154. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  155. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  156. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  157. goto out_free_ccid;
  158. } else {
  159. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  160. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  161. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  162. goto out_free_ccid;
  163. }
  164. out:
  165. return ccid;
  166. out_unlock:
  167. ccids_read_unlock();
  168. goto out;
  169. out_free_ccid:
  170. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  171. ccid_ops->ccid_hc_tx_slab, ccid);
  172. ccid = NULL;
  173. out_module_put:
  174. module_put(ccid_ops->ccid_owner);
  175. goto out;
  176. }
  177. EXPORT_SYMBOL_GPL(ccid_new);
  178. struct ccid *ccid_hc_rx_new(unsigned char id, struct sock *sk, gfp_t gfp)
  179. {
  180. return ccid_new(id, sk, 1, gfp);
  181. }
  182. EXPORT_SYMBOL_GPL(ccid_hc_rx_new);
  183. struct ccid *ccid_hc_tx_new(unsigned char id,struct sock *sk, gfp_t gfp)
  184. {
  185. return ccid_new(id, sk, 0, gfp);
  186. }
  187. EXPORT_SYMBOL_GPL(ccid_hc_tx_new);
  188. static void ccid_delete(struct ccid *ccid, struct sock *sk, int rx)
  189. {
  190. struct ccid_operations *ccid_ops;
  191. if (ccid == NULL)
  192. return;
  193. ccid_ops = ccid->ccid_ops;
  194. if (rx) {
  195. if (ccid_ops->ccid_hc_rx_exit != NULL)
  196. ccid_ops->ccid_hc_rx_exit(sk);
  197. kmem_cache_free(ccid_ops->ccid_hc_rx_slab, ccid);
  198. } else {
  199. if (ccid_ops->ccid_hc_tx_exit != NULL)
  200. ccid_ops->ccid_hc_tx_exit(sk);
  201. kmem_cache_free(ccid_ops->ccid_hc_tx_slab, ccid);
  202. }
  203. ccids_read_lock();
  204. if (ccids[ccid_ops->ccid_id] != NULL)
  205. module_put(ccid_ops->ccid_owner);
  206. ccids_read_unlock();
  207. }
  208. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  209. {
  210. ccid_delete(ccid, sk, 1);
  211. }
  212. EXPORT_SYMBOL_GPL(ccid_hc_rx_delete);
  213. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  214. {
  215. ccid_delete(ccid, sk, 0);
  216. }
  217. EXPORT_SYMBOL_GPL(ccid_hc_tx_delete);