4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
15 #include "ccids/lib/tfrc.h"
17 static struct ccid_operations *ccids[] = {
19 #ifdef CONFIG_IP_DCCP_CCID3
24 static struct ccid_operations *ccid_by_number(const u8 id)
28 for (i = 0; i < ARRAY_SIZE(ccids); i++)
29 if (ccids[i]->ccid_id == id)
34 /* check that up to @array_len members in @ccid_array are supported */
35 bool ccid_support_check(u8 const *ccid_array, u8 array_len)
38 if (ccid_by_number(ccid_array[--array_len]) == NULL)
44 * ccid_get_builtin_ccids - Populate a list of built-in CCIDs
45 * @ccid_array: pointer to copy into
46 * @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 (len < ARRAY_SIZE(ccids))
69 if (ccid_get_builtin_ccids(&ccid_array, &array_len))
72 if (put_user(array_len, optlen) ||
73 copy_to_user(optval, ccid_array, array_len))
80 static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
82 struct kmem_cache *slab;
83 char slab_name_fmt[32], *slab_name;
87 vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args);
90 slab_name = kstrdup(slab_name_fmt, GFP_KERNEL);
91 if (slab_name == NULL)
93 slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0,
94 SLAB_HWCACHE_ALIGN, NULL);
100 static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
103 const char *name = kmem_cache_name(slab);
105 kmem_cache_destroy(slab);
110 static int ccid_activate(struct ccid_operations *ccid_ops)
114 ccid_ops->ccid_hc_rx_slab =
115 ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
118 if (ccid_ops->ccid_hc_rx_slab == NULL)
121 ccid_ops->ccid_hc_tx_slab =
122 ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
125 if (ccid_ops->ccid_hc_tx_slab == NULL)
126 goto out_free_rx_slab;
128 pr_info("CCID: Activated CCID %d (%s)\n",
129 ccid_ops->ccid_id, ccid_ops->ccid_name);
134 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
135 ccid_ops->ccid_hc_rx_slab = NULL;
139 static void ccid_deactivate(struct ccid_operations *ccid_ops)
141 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
142 ccid_ops->ccid_hc_tx_slab = NULL;
143 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
144 ccid_ops->ccid_hc_rx_slab = NULL;
146 pr_info("CCID: Deactivated CCID %d (%s)\n",
147 ccid_ops->ccid_id, ccid_ops->ccid_name);
150 struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
152 struct ccid_operations *ccid_ops = ccid_by_number(id);
153 struct ccid *ccid = NULL;
155 if (ccid_ops == NULL)
158 ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
159 ccid_ops->ccid_hc_tx_slab, gfp_any());
162 ccid->ccid_ops = ccid_ops;
164 memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
165 if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
166 ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
169 memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
170 if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
171 ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
177 kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
178 ccid_ops->ccid_hc_tx_slab, ccid);
183 void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
186 if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
187 ccid->ccid_ops->ccid_hc_rx_exit(sk);
188 kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
192 void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
195 if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
196 ccid->ccid_ops->ccid_hc_tx_exit(sk);
197 kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
201 int __init ccid_initialize_builtins(void)
203 int i, err = tfrc_lib_init();
208 for (i = 0; i < ARRAY_SIZE(ccids); i++) {
209 err = ccid_activate(ccids[i]);
211 goto unwind_registrations;
215 unwind_registrations:
217 ccid_deactivate(ccids[i]);
222 void ccid_cleanup_builtins(void)
226 for (i = 0; i < ARRAY_SIZE(ccids); i++)
227 ccid_deactivate(ccids[i]);