ccid.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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[] = {
  15. &ccid2_ops,
  16. #ifdef CONFIG_IP_DCCP_CCID3
  17. &ccid3_ops,
  18. #endif
  19. };
  20. static struct ccid_operations *ccid_by_number(const u8 id)
  21. {
  22. int i;
  23. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  24. if (ccids[i]->ccid_id == id)
  25. return ccids[i];
  26. return NULL;
  27. }
  28. /* check that up to @array_len members in @ccid_array are supported */
  29. bool ccid_support_check(u8 const *ccid_array, u8 array_len)
  30. {
  31. while (array_len > 0)
  32. if (ccid_by_number(ccid_array[--array_len]) == NULL)
  33. return false;
  34. return true;
  35. }
  36. /**
  37. * ccid_get_builtin_ccids - Populate a list of built-in CCIDs
  38. * @ccid_array: pointer to copy into
  39. * @array_len: value to return length into
  40. * This function allocates memory - caller must see that it is freed after use.
  41. */
  42. int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
  43. {
  44. *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
  45. if (*ccid_array == NULL)
  46. return -ENOBUFS;
  47. for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1)
  48. (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id;
  49. return 0;
  50. }
  51. int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
  52. char __user *optval, int __user *optlen)
  53. {
  54. u8 *ccid_array, array_len;
  55. int err = 0;
  56. if (len < ARRAY_SIZE(ccids))
  57. return -EINVAL;
  58. if (ccid_get_builtin_ccids(&ccid_array, &array_len))
  59. return -ENOBUFS;
  60. if (put_user(array_len, optlen) ||
  61. copy_to_user(optval, ccid_array, array_len))
  62. err = -EFAULT;
  63. kfree(ccid_array);
  64. return err;
  65. }
  66. #ifdef ___OLD_INTERFACE_TO_BE_REMOVED___
  67. static u8 builtin_ccids[] = {
  68. DCCPC_CCID2, /* CCID2 is supported by default */
  69. #if defined(CONFIG_IP_DCCP_CCID3) || defined(CONFIG_IP_DCCP_CCID3_MODULE)
  70. DCCPC_CCID3,
  71. #endif
  72. };
  73. static struct ccid_operations *ccids[CCID_MAX];
  74. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  75. static atomic_t ccids_lockct = ATOMIC_INIT(0);
  76. static DEFINE_SPINLOCK(ccids_lock);
  77. /*
  78. * The strategy is: modifications ccids vector are short, do not sleep and
  79. * veeery rare, but read access should be free of any exclusive locks.
  80. */
  81. static void ccids_write_lock(void)
  82. {
  83. spin_lock(&ccids_lock);
  84. while (atomic_read(&ccids_lockct) != 0) {
  85. spin_unlock(&ccids_lock);
  86. yield();
  87. spin_lock(&ccids_lock);
  88. }
  89. }
  90. static inline void ccids_write_unlock(void)
  91. {
  92. spin_unlock(&ccids_lock);
  93. }
  94. static inline void ccids_read_lock(void)
  95. {
  96. atomic_inc(&ccids_lockct);
  97. smp_mb__after_atomic_inc();
  98. spin_unlock_wait(&ccids_lock);
  99. }
  100. static inline void ccids_read_unlock(void)
  101. {
  102. atomic_dec(&ccids_lockct);
  103. }
  104. #else
  105. #define ccids_write_lock() do { } while(0)
  106. #define ccids_write_unlock() do { } while(0)
  107. #define ccids_read_lock() do { } while(0)
  108. #define ccids_read_unlock() do { } while(0)
  109. #endif
  110. #endif /* ___OLD_INTERFACE_TO_BE_REMOVED___ */
  111. static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
  112. {
  113. struct kmem_cache *slab;
  114. char slab_name_fmt[32], *slab_name;
  115. va_list args;
  116. va_start(args, fmt);
  117. vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args);
  118. va_end(args);
  119. slab_name = kstrdup(slab_name_fmt, GFP_KERNEL);
  120. if (slab_name == NULL)
  121. return NULL;
  122. slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0,
  123. SLAB_HWCACHE_ALIGN, NULL);
  124. if (slab == NULL)
  125. kfree(slab_name);
  126. return slab;
  127. }
  128. static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
  129. {
  130. if (slab != NULL) {
  131. const char *name = kmem_cache_name(slab);
  132. kmem_cache_destroy(slab);
  133. kfree(name);
  134. }
  135. }
  136. #ifdef ___OLD_INTERFACE_TO_BE_REMOVED___
  137. /* check that up to @array_len members in @ccid_array are supported */
  138. bool ccid_support_check(u8 const *ccid_array, u8 array_len)
  139. {
  140. u8 i, j, found;
  141. for (i = 0, found = 0; i < array_len; i++, found = 0) {
  142. for (j = 0; !found && j < ARRAY_SIZE(builtin_ccids); j++)
  143. found = (ccid_array[i] == builtin_ccids[j]);
  144. if (!found)
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * ccid_get_builtin_ccids - Provide copy of `builtin' CCID array
  151. * @ccid_array: pointer to copy into
  152. * @array_len: value to return length into
  153. * This function allocates memory - caller must see that it is freed after use.
  154. */
  155. int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
  156. {
  157. *ccid_array = kmemdup(builtin_ccids, sizeof(builtin_ccids), gfp_any());
  158. if (*ccid_array == NULL)
  159. return -ENOBUFS;
  160. *array_len = ARRAY_SIZE(builtin_ccids);
  161. return 0;
  162. }
  163. int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
  164. char __user *optval, int __user *optlen)
  165. {
  166. if (len < sizeof(builtin_ccids))
  167. return -EINVAL;
  168. if (put_user(sizeof(builtin_ccids), optlen) ||
  169. copy_to_user(optval, builtin_ccids, sizeof(builtin_ccids)))
  170. return -EFAULT;
  171. return 0;
  172. }
  173. #endif /* ___OLD_INTERFACE_TO_BE_REMOVED___ */
  174. static int ccid_activate(struct ccid_operations *ccid_ops)
  175. {
  176. int err = -ENOBUFS;
  177. ccid_ops->ccid_hc_rx_slab =
  178. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  179. "ccid%u_hc_rx_sock",
  180. ccid_ops->ccid_id);
  181. if (ccid_ops->ccid_hc_rx_slab == NULL)
  182. goto out;
  183. ccid_ops->ccid_hc_tx_slab =
  184. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  185. "ccid%u_hc_tx_sock",
  186. ccid_ops->ccid_id);
  187. if (ccid_ops->ccid_hc_tx_slab == NULL)
  188. goto out_free_rx_slab;
  189. pr_info("CCID: Activated CCID %d (%s)\n",
  190. ccid_ops->ccid_id, ccid_ops->ccid_name);
  191. err = 0;
  192. out:
  193. return err;
  194. out_free_rx_slab:
  195. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  196. ccid_ops->ccid_hc_rx_slab = NULL;
  197. goto out;
  198. }
  199. static void ccid_deactivate(struct ccid_operations *ccid_ops)
  200. {
  201. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  202. ccid_ops->ccid_hc_tx_slab = NULL;
  203. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  204. ccid_ops->ccid_hc_rx_slab = NULL;
  205. pr_info("CCID: Deactivated CCID %d (%s)\n",
  206. ccid_ops->ccid_id, ccid_ops->ccid_name);
  207. }
  208. struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp)
  209. {
  210. struct ccid_operations *ccid_ops = ccid_by_number(id);
  211. struct ccid *ccid = NULL;
  212. if (ccid_ops == NULL)
  213. goto out;
  214. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  215. ccid_ops->ccid_hc_tx_slab, gfp);
  216. if (ccid == NULL)
  217. goto out;
  218. ccid->ccid_ops = ccid_ops;
  219. if (rx) {
  220. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  221. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  222. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  223. goto out_free_ccid;
  224. } else {
  225. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  226. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  227. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  228. goto out_free_ccid;
  229. }
  230. out:
  231. return ccid;
  232. out_free_ccid:
  233. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  234. ccid_ops->ccid_hc_tx_slab, ccid);
  235. ccid = NULL;
  236. goto out;
  237. }
  238. EXPORT_SYMBOL_GPL(ccid_new);
  239. static void ccid_delete(struct ccid *ccid, struct sock *sk, int rx)
  240. {
  241. struct ccid_operations *ccid_ops;
  242. if (ccid == NULL)
  243. return;
  244. ccid_ops = ccid->ccid_ops;
  245. if (rx) {
  246. if (ccid_ops->ccid_hc_rx_exit != NULL)
  247. ccid_ops->ccid_hc_rx_exit(sk);
  248. kmem_cache_free(ccid_ops->ccid_hc_rx_slab, ccid);
  249. } else {
  250. if (ccid_ops->ccid_hc_tx_exit != NULL)
  251. ccid_ops->ccid_hc_tx_exit(sk);
  252. kmem_cache_free(ccid_ops->ccid_hc_tx_slab, ccid);
  253. }
  254. }
  255. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  256. {
  257. ccid_delete(ccid, sk, 1);
  258. }
  259. EXPORT_SYMBOL_GPL(ccid_hc_rx_delete);
  260. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  261. {
  262. ccid_delete(ccid, sk, 0);
  263. }
  264. EXPORT_SYMBOL_GPL(ccid_hc_tx_delete);
  265. int __init ccid_initialize_builtins(void)
  266. {
  267. int i, err;
  268. for (i = 0; i < ARRAY_SIZE(ccids); i++) {
  269. err = ccid_activate(ccids[i]);
  270. if (err)
  271. goto unwind_registrations;
  272. }
  273. return 0;
  274. unwind_registrations:
  275. while(--i >= 0)
  276. ccid_deactivate(ccids[i]);
  277. return err;
  278. }
  279. void ccid_cleanup_builtins(void)
  280. {
  281. int i;
  282. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  283. ccid_deactivate(ccids[i]);
  284. }