ccid.c 5.3 KB

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