ccid.c 5.1 KB

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