ccid.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <linux/slab.h>
  14. #include "ccid.h"
  15. #include "ccids/lib/tfrc.h"
  16. static struct ccid_operations *ccids[] = {
  17. &ccid2_ops,
  18. #ifdef CONFIG_IP_DCCP_CCID3
  19. &ccid3_ops,
  20. #endif
  21. };
  22. static struct ccid_operations *ccid_by_number(const u8 id)
  23. {
  24. int i;
  25. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  26. if (ccids[i]->ccid_id == id)
  27. return ccids[i];
  28. return NULL;
  29. }
  30. /* check that up to @array_len members in @ccid_array are supported */
  31. bool ccid_support_check(u8 const *ccid_array, u8 array_len)
  32. {
  33. while (array_len > 0)
  34. if (ccid_by_number(ccid_array[--array_len]) == NULL)
  35. return false;
  36. return true;
  37. }
  38. /**
  39. * ccid_get_builtin_ccids - Populate a list of built-in CCIDs
  40. * @ccid_array: pointer to copy into
  41. * @array_len: value to return length into
  42. *
  43. * This function allocates memory - caller must see that it is freed after use.
  44. */
  45. int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
  46. {
  47. *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
  48. if (*ccid_array == NULL)
  49. return -ENOBUFS;
  50. for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1)
  51. (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id;
  52. return 0;
  53. }
  54. int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
  55. char __user *optval, int __user *optlen)
  56. {
  57. u8 *ccid_array, array_len;
  58. int err = 0;
  59. if (ccid_get_builtin_ccids(&ccid_array, &array_len))
  60. return -ENOBUFS;
  61. if (put_user(array_len, optlen))
  62. err = -EFAULT;
  63. else if (len > 0 && copy_to_user(optval, ccid_array,
  64. len > array_len ? array_len : len))
  65. err = -EFAULT;
  66. kfree(ccid_array);
  67. return err;
  68. }
  69. static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
  70. {
  71. struct kmem_cache *slab;
  72. va_list args;
  73. va_start(args, fmt);
  74. vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
  75. va_end(args);
  76. slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
  77. SLAB_HWCACHE_ALIGN, NULL);
  78. return slab;
  79. }
  80. static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
  81. {
  82. if (slab != NULL)
  83. kmem_cache_destroy(slab);
  84. }
  85. static int __init ccid_activate(struct ccid_operations *ccid_ops)
  86. {
  87. int err = -ENOBUFS;
  88. ccid_ops->ccid_hc_rx_slab =
  89. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  90. ccid_ops->ccid_hc_rx_slab_name,
  91. "ccid%u_hc_rx_sock",
  92. ccid_ops->ccid_id);
  93. if (ccid_ops->ccid_hc_rx_slab == NULL)
  94. goto out;
  95. ccid_ops->ccid_hc_tx_slab =
  96. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  97. ccid_ops->ccid_hc_tx_slab_name,
  98. "ccid%u_hc_tx_sock",
  99. ccid_ops->ccid_id);
  100. if (ccid_ops->ccid_hc_tx_slab == NULL)
  101. goto out_free_rx_slab;
  102. pr_info("DCCP: Activated CCID %d (%s)\n",
  103. ccid_ops->ccid_id, ccid_ops->ccid_name);
  104. err = 0;
  105. out:
  106. return err;
  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. static void ccid_deactivate(struct ccid_operations *ccid_ops)
  113. {
  114. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  115. ccid_ops->ccid_hc_tx_slab = NULL;
  116. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  117. ccid_ops->ccid_hc_rx_slab = NULL;
  118. pr_info("DCCP: Deactivated CCID %d (%s)\n",
  119. ccid_ops->ccid_id, ccid_ops->ccid_name);
  120. }
  121. struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
  122. {
  123. struct ccid_operations *ccid_ops = ccid_by_number(id);
  124. struct ccid *ccid = NULL;
  125. if (ccid_ops == NULL)
  126. goto out;
  127. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  128. ccid_ops->ccid_hc_tx_slab, gfp_any());
  129. if (ccid == NULL)
  130. goto out;
  131. ccid->ccid_ops = ccid_ops;
  132. if (rx) {
  133. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  134. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  135. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  136. goto out_free_ccid;
  137. } else {
  138. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  139. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  140. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  141. goto out_free_ccid;
  142. }
  143. out:
  144. return ccid;
  145. out_free_ccid:
  146. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  147. ccid_ops->ccid_hc_tx_slab, ccid);
  148. ccid = NULL;
  149. goto out;
  150. }
  151. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  152. {
  153. if (ccid != NULL) {
  154. if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
  155. ccid->ccid_ops->ccid_hc_rx_exit(sk);
  156. kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
  157. }
  158. }
  159. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  160. {
  161. if (ccid != NULL) {
  162. if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
  163. ccid->ccid_ops->ccid_hc_tx_exit(sk);
  164. kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
  165. }
  166. }
  167. int __init ccid_initialize_builtins(void)
  168. {
  169. int i, err = tfrc_lib_init();
  170. if (err)
  171. return err;
  172. for (i = 0; i < ARRAY_SIZE(ccids); i++) {
  173. err = ccid_activate(ccids[i]);
  174. if (err)
  175. goto unwind_registrations;
  176. }
  177. return 0;
  178. unwind_registrations:
  179. while(--i >= 0)
  180. ccid_deactivate(ccids[i]);
  181. tfrc_lib_exit();
  182. return err;
  183. }
  184. void ccid_cleanup_builtins(void)
  185. {
  186. int i;
  187. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  188. ccid_deactivate(ccids[i]);
  189. tfrc_lib_exit();
  190. }