ccid.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. kmem_cache_destroy(slab);
  83. }
  84. static int __init ccid_activate(struct ccid_operations *ccid_ops)
  85. {
  86. int err = -ENOBUFS;
  87. ccid_ops->ccid_hc_rx_slab =
  88. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  89. ccid_ops->ccid_hc_rx_slab_name,
  90. "ccid%u_hc_rx_sock",
  91. ccid_ops->ccid_id);
  92. if (ccid_ops->ccid_hc_rx_slab == NULL)
  93. goto out;
  94. ccid_ops->ccid_hc_tx_slab =
  95. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  96. ccid_ops->ccid_hc_tx_slab_name,
  97. "ccid%u_hc_tx_sock",
  98. ccid_ops->ccid_id);
  99. if (ccid_ops->ccid_hc_tx_slab == NULL)
  100. goto out_free_rx_slab;
  101. pr_info("DCCP: Activated CCID %d (%s)\n",
  102. ccid_ops->ccid_id, ccid_ops->ccid_name);
  103. err = 0;
  104. out:
  105. return err;
  106. out_free_rx_slab:
  107. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  108. ccid_ops->ccid_hc_rx_slab = NULL;
  109. goto out;
  110. }
  111. static void ccid_deactivate(struct ccid_operations *ccid_ops)
  112. {
  113. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  114. ccid_ops->ccid_hc_tx_slab = NULL;
  115. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  116. ccid_ops->ccid_hc_rx_slab = NULL;
  117. pr_info("DCCP: Deactivated CCID %d (%s)\n",
  118. ccid_ops->ccid_id, ccid_ops->ccid_name);
  119. }
  120. struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
  121. {
  122. struct ccid_operations *ccid_ops = ccid_by_number(id);
  123. struct ccid *ccid = NULL;
  124. if (ccid_ops == NULL)
  125. goto out;
  126. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  127. ccid_ops->ccid_hc_tx_slab, gfp_any());
  128. if (ccid == NULL)
  129. goto out;
  130. ccid->ccid_ops = ccid_ops;
  131. if (rx) {
  132. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  133. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  134. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  135. goto out_free_ccid;
  136. } else {
  137. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  138. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  139. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  140. goto out_free_ccid;
  141. }
  142. out:
  143. return ccid;
  144. out_free_ccid:
  145. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  146. ccid_ops->ccid_hc_tx_slab, ccid);
  147. ccid = NULL;
  148. goto out;
  149. }
  150. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  151. {
  152. if (ccid != NULL) {
  153. if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
  154. ccid->ccid_ops->ccid_hc_rx_exit(sk);
  155. kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
  156. }
  157. }
  158. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  159. {
  160. if (ccid != NULL) {
  161. if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
  162. ccid->ccid_ops->ccid_hc_tx_exit(sk);
  163. kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
  164. }
  165. }
  166. int __init ccid_initialize_builtins(void)
  167. {
  168. int i, err = tfrc_lib_init();
  169. if (err)
  170. return err;
  171. for (i = 0; i < ARRAY_SIZE(ccids); i++) {
  172. err = ccid_activate(ccids[i]);
  173. if (err)
  174. goto unwind_registrations;
  175. }
  176. return 0;
  177. unwind_registrations:
  178. while(--i >= 0)
  179. ccid_deactivate(ccids[i]);
  180. tfrc_lib_exit();
  181. return err;
  182. }
  183. void ccid_cleanup_builtins(void)
  184. {
  185. int i;
  186. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  187. ccid_deactivate(ccids[i]);
  188. tfrc_lib_exit();
  189. }