1 // SPDX-License-Identifier: GPL-2.0-only
5 * An implementation of the DCCP protocol
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
11 #include <linux/slab.h>
14 #include "ccids/lib/tfrc.h"
16 static struct ccid_operations *ccids[] = {
18 #ifdef CONFIG_IP_DCCP_CCID3
23 static struct ccid_operations *ccid_by_number(const u8 id)
27 for (i = 0; i < ARRAY_SIZE(ccids); i++)
28 if (ccids[i]->ccid_id == id)
33 /* check that up to @array_len members in @ccid_array are supported */
34 bool ccid_support_check(u8 const *ccid_array, u8 array_len)
37 if (ccid_by_number(ccid_array[--array_len]) == NULL)
43 * ccid_get_builtin_ccids - Populate a list of built-in CCIDs
44 * @ccid_array: pointer to copy into
45 * @array_len: value to return length into
47 * This function allocates memory - caller must see that it is freed after use.
49 int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
51 *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
52 if (*ccid_array == NULL)
55 for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1)
56 (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id;
60 int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
61 char __user *optval, int __user *optlen)
63 u8 *ccid_array, array_len;
66 if (ccid_get_builtin_ccids(&ccid_array, &array_len))
69 if (put_user(array_len, optlen))
71 else if (len > 0 && copy_to_user(optval, ccid_array,
72 len > array_len ? array_len : len))
79 static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
81 struct kmem_cache *slab;
85 vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
88 slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
89 SLAB_HWCACHE_ALIGN, NULL);
93 static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
95 kmem_cache_destroy(slab);
98 static int __init ccid_activate(struct ccid_operations *ccid_ops)
102 ccid_ops->ccid_hc_rx_slab =
103 ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
104 ccid_ops->ccid_hc_rx_slab_name,
107 if (ccid_ops->ccid_hc_rx_slab == NULL)
110 ccid_ops->ccid_hc_tx_slab =
111 ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
112 ccid_ops->ccid_hc_tx_slab_name,
115 if (ccid_ops->ccid_hc_tx_slab == NULL)
116 goto out_free_rx_slab;
118 pr_info("DCCP: Activated CCID %d (%s)\n",
119 ccid_ops->ccid_id, ccid_ops->ccid_name);
124 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
125 ccid_ops->ccid_hc_rx_slab = NULL;
129 static void ccid_deactivate(struct ccid_operations *ccid_ops)
131 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
132 ccid_ops->ccid_hc_tx_slab = NULL;
133 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
134 ccid_ops->ccid_hc_rx_slab = NULL;
136 pr_info("DCCP: Deactivated CCID %d (%s)\n",
137 ccid_ops->ccid_id, ccid_ops->ccid_name);
140 struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
142 struct ccid_operations *ccid_ops = ccid_by_number(id);
143 struct ccid *ccid = NULL;
145 if (ccid_ops == NULL)
148 ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
149 ccid_ops->ccid_hc_tx_slab, gfp_any());
152 ccid->ccid_ops = ccid_ops;
154 memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
155 if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
156 ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
159 memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
160 if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
161 ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
167 kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
168 ccid_ops->ccid_hc_tx_slab, ccid);
173 void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
176 if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
177 ccid->ccid_ops->ccid_hc_rx_exit(sk);
178 kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
182 void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
185 if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
186 ccid->ccid_ops->ccid_hc_tx_exit(sk);
187 kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
191 int __init ccid_initialize_builtins(void)
193 int i, err = tfrc_lib_init();
198 for (i = 0; i < ARRAY_SIZE(ccids); i++) {
199 err = ccid_activate(ccids[i]);
201 goto unwind_registrations;
205 unwind_registrations:
207 ccid_deactivate(ccids[i]);
212 void ccid_cleanup_builtins(void)
216 for (i = 0; i < ARRAY_SIZE(ccids); i++)
217 ccid_deactivate(ccids[i]);