ccid.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 (ccid_get_builtin_ccids(&ccid_array, &array_len))
  58. return -ENOBUFS;
  59. if (put_user(array_len, optlen))
  60. err = -EFAULT;
  61. else if (len > 0 && copy_to_user(optval, ccid_array,
  62. len > array_len ? array_len : len))
  63. err = -EFAULT;
  64. kfree(ccid_array);
  65. return err;
  66. }
  67. static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
  68. {
  69. struct kmem_cache *slab;
  70. va_list args;
  71. va_start(args, fmt);
  72. vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
  73. va_end(args);
  74. slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
  75. SLAB_HWCACHE_ALIGN, NULL);
  76. return slab;
  77. }
  78. static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
  79. {
  80. if (slab != NULL)
  81. kmem_cache_destroy(slab);
  82. }
  83. static int ccid_activate(struct ccid_operations *ccid_ops)
  84. {
  85. int err = -ENOBUFS;
  86. ccid_ops->ccid_hc_rx_slab =
  87. ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
  88. ccid_ops->ccid_hc_rx_slab_name,
  89. "ccid%u_hc_rx_sock",
  90. ccid_ops->ccid_id);
  91. if (ccid_ops->ccid_hc_rx_slab == NULL)
  92. goto out;
  93. ccid_ops->ccid_hc_tx_slab =
  94. ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
  95. ccid_ops->ccid_hc_tx_slab_name,
  96. "ccid%u_hc_tx_sock",
  97. ccid_ops->ccid_id);
  98. if (ccid_ops->ccid_hc_tx_slab == NULL)
  99. goto out_free_rx_slab;
  100. pr_info("CCID: Activated CCID %d (%s)\n",
  101. ccid_ops->ccid_id, ccid_ops->ccid_name);
  102. err = 0;
  103. out:
  104. return err;
  105. out_free_rx_slab:
  106. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  107. ccid_ops->ccid_hc_rx_slab = NULL;
  108. goto out;
  109. }
  110. static void ccid_deactivate(struct ccid_operations *ccid_ops)
  111. {
  112. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
  113. ccid_ops->ccid_hc_tx_slab = NULL;
  114. ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
  115. ccid_ops->ccid_hc_rx_slab = NULL;
  116. pr_info("CCID: Deactivated CCID %d (%s)\n",
  117. ccid_ops->ccid_id, ccid_ops->ccid_name);
  118. }
  119. struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
  120. {
  121. struct ccid_operations *ccid_ops = ccid_by_number(id);
  122. struct ccid *ccid = NULL;
  123. if (ccid_ops == NULL)
  124. goto out;
  125. ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
  126. ccid_ops->ccid_hc_tx_slab, gfp_any());
  127. if (ccid == NULL)
  128. goto out;
  129. ccid->ccid_ops = ccid_ops;
  130. if (rx) {
  131. memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
  132. if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
  133. ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
  134. goto out_free_ccid;
  135. } else {
  136. memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
  137. if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
  138. ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
  139. goto out_free_ccid;
  140. }
  141. out:
  142. return ccid;
  143. out_free_ccid:
  144. kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
  145. ccid_ops->ccid_hc_tx_slab, ccid);
  146. ccid = NULL;
  147. goto out;
  148. }
  149. void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
  150. {
  151. if (ccid != NULL) {
  152. if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
  153. ccid->ccid_ops->ccid_hc_rx_exit(sk);
  154. kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
  155. }
  156. }
  157. void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
  158. {
  159. if (ccid != NULL) {
  160. if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
  161. ccid->ccid_ops->ccid_hc_tx_exit(sk);
  162. kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
  163. }
  164. }
  165. int __init ccid_initialize_builtins(void)
  166. {
  167. int i, err = tfrc_lib_init();
  168. if (err)
  169. return err;
  170. for (i = 0; i < ARRAY_SIZE(ccids); i++) {
  171. err = ccid_activate(ccids[i]);
  172. if (err)
  173. goto unwind_registrations;
  174. }
  175. return 0;
  176. unwind_registrations:
  177. while(--i >= 0)
  178. ccid_deactivate(ccids[i]);
  179. tfrc_lib_exit();
  180. return err;
  181. }
  182. void ccid_cleanup_builtins(void)
  183. {
  184. int i;
  185. for (i = 0; i < ARRAY_SIZE(ccids); i++)
  186. ccid_deactivate(ccids[i]);
  187. tfrc_lib_exit();
  188. }