]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/dccp/ccid.c
e3fb52b4f5c69f7b6711201d3eb654b0e0ead152
[net-next-2.6.git] / net / dccp / ccid.c
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
14 #include "ccid.h"
15
16 static u8 builtin_ccids[] = {
17         DCCPC_CCID2,            /* CCID2 is supported by default */
18 #if defined(CONFIG_IP_DCCP_CCID3) || defined(CONFIG_IP_DCCP_CCID3_MODULE)
19         DCCPC_CCID3,
20 #endif
21 };
22
23 static struct ccid_operations *ccids[CCID_MAX];
24 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
25 static atomic_t ccids_lockct = ATOMIC_INIT(0);
26 static DEFINE_SPINLOCK(ccids_lock);
27
28 /*
29  * The strategy is: modifications ccids vector are short, do not sleep and
30  * veeery rare, but read access should be free of any exclusive locks.
31  */
32 static void ccids_write_lock(void)
33 {
34         spin_lock(&ccids_lock);
35         while (atomic_read(&ccids_lockct) != 0) {
36                 spin_unlock(&ccids_lock);
37                 yield();
38                 spin_lock(&ccids_lock);
39         }
40 }
41
42 static inline void ccids_write_unlock(void)
43 {
44         spin_unlock(&ccids_lock);
45 }
46
47 static inline void ccids_read_lock(void)
48 {
49         atomic_inc(&ccids_lockct);
50         smp_mb__after_atomic_inc();
51         spin_unlock_wait(&ccids_lock);
52 }
53
54 static inline void ccids_read_unlock(void)
55 {
56         atomic_dec(&ccids_lockct);
57 }
58
59 #else
60 #define ccids_write_lock() do { } while(0)
61 #define ccids_write_unlock() do { } while(0)
62 #define ccids_read_lock() do { } while(0)
63 #define ccids_read_unlock() do { } while(0)
64 #endif
65
66 static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
67 {
68         struct kmem_cache *slab;
69         char slab_name_fmt[32], *slab_name;
70         va_list args;
71
72         va_start(args, fmt);
73         vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args);
74         va_end(args);
75
76         slab_name = kstrdup(slab_name_fmt, GFP_KERNEL);
77         if (slab_name == NULL)
78                 return NULL;
79         slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0,
80                                  SLAB_HWCACHE_ALIGN, NULL);
81         if (slab == NULL)
82                 kfree(slab_name);
83         return slab;
84 }
85
86 static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
87 {
88         if (slab != NULL) {
89                 const char *name = kmem_cache_name(slab);
90
91                 kmem_cache_destroy(slab);
92                 kfree(name);
93         }
94 }
95
96 /* check that up to @array_len members in @ccid_array are supported */
97 bool ccid_support_check(u8 const *ccid_array, u8 array_len)
98 {
99         u8 i, j, found;
100
101         for (i = 0, found = 0; i < array_len; i++, found = 0) {
102                 for (j = 0; !found && j < ARRAY_SIZE(builtin_ccids); j++)
103                         found = (ccid_array[i] == builtin_ccids[j]);
104                 if (!found)
105                         return false;
106         }
107         return true;
108 }
109
110 /**
111  * ccid_get_builtin_ccids  -  Provide copy of `builtin' CCID array
112  * @ccid_array: pointer to copy into
113  * @array_len: value to return length into
114  * This function allocates memory - caller must see that it is freed after use.
115  */
116 int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
117 {
118         *ccid_array = kmemdup(builtin_ccids, sizeof(builtin_ccids), gfp_any());
119         if (*ccid_array == NULL)
120                 return -ENOBUFS;
121         *array_len = ARRAY_SIZE(builtin_ccids);
122         return 0;
123 }
124
125 int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
126                                     char __user *optval, int __user *optlen)
127 {
128         if (len < sizeof(builtin_ccids))
129                 return -EINVAL;
130
131         if (put_user(sizeof(builtin_ccids), optlen) ||
132             copy_to_user(optval, builtin_ccids, sizeof(builtin_ccids)))
133                 return -EFAULT;
134         return 0;
135 }
136
137 int ccid_register(struct ccid_operations *ccid_ops)
138 {
139         int err = -ENOBUFS;
140
141         ccid_ops->ccid_hc_rx_slab =
142                         ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
143                                                "ccid%u_hc_rx_sock",
144                                                ccid_ops->ccid_id);
145         if (ccid_ops->ccid_hc_rx_slab == NULL)
146                 goto out;
147
148         ccid_ops->ccid_hc_tx_slab =
149                         ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
150                                                "ccid%u_hc_tx_sock",
151                                                ccid_ops->ccid_id);
152         if (ccid_ops->ccid_hc_tx_slab == NULL)
153                 goto out_free_rx_slab;
154
155         ccids_write_lock();
156         err = -EEXIST;
157         if (ccids[ccid_ops->ccid_id] == NULL) {
158                 ccids[ccid_ops->ccid_id] = ccid_ops;
159                 err = 0;
160         }
161         ccids_write_unlock();
162         if (err != 0)
163                 goto out_free_tx_slab;
164
165         pr_info("CCID: Registered CCID %d (%s)\n",
166                 ccid_ops->ccid_id, ccid_ops->ccid_name);
167 out:
168         return err;
169 out_free_tx_slab:
170         ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
171         ccid_ops->ccid_hc_tx_slab = NULL;
172         goto out;
173 out_free_rx_slab:
174         ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
175         ccid_ops->ccid_hc_rx_slab = NULL;
176         goto out;
177 }
178
179 EXPORT_SYMBOL_GPL(ccid_register);
180
181 int ccid_unregister(struct ccid_operations *ccid_ops)
182 {
183         ccids_write_lock();
184         ccids[ccid_ops->ccid_id] = NULL;
185         ccids_write_unlock();
186
187         ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
188         ccid_ops->ccid_hc_tx_slab = NULL;
189         ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
190         ccid_ops->ccid_hc_rx_slab = NULL;
191
192         pr_info("CCID: Unregistered CCID %d (%s)\n",
193                 ccid_ops->ccid_id, ccid_ops->ccid_name);
194         return 0;
195 }
196
197 EXPORT_SYMBOL_GPL(ccid_unregister);
198
199 /**
200  * ccid_request_module  -  Pre-load CCID module for later use
201  * This should be called only from process context (e.g. during connection
202  * setup) and is necessary for later calls to ccid_new (typically in software
203  * interrupt), so that it has the modules available when they are needed.
204  */
205 static int ccid_request_module(u8 id)
206 {
207         if (!in_atomic()) {
208                 ccids_read_lock();
209                 if (ccids[id] == NULL) {
210                         ccids_read_unlock();
211                         return request_module("net-dccp-ccid-%d", id);
212                 }
213                 ccids_read_unlock();
214         }
215         return 0;
216 }
217
218 int ccid_request_modules(u8 const *ccid_array, u8 array_len)
219 {
220 #ifdef CONFIG_KMOD
221         while (array_len--)
222                 if (ccid_request_module(ccid_array[array_len]))
223                         return -1;
224 #endif
225         return 0;
226 }
227
228 struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp)
229 {
230         struct ccid_operations *ccid_ops;
231         struct ccid *ccid = NULL;
232
233         ccids_read_lock();
234         ccid_ops = ccids[id];
235         if (ccid_ops == NULL)
236                 goto out_unlock;
237
238         if (!try_module_get(ccid_ops->ccid_owner))
239                 goto out_unlock;
240
241         ccids_read_unlock();
242
243         ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
244                                      ccid_ops->ccid_hc_tx_slab, gfp);
245         if (ccid == NULL)
246                 goto out_module_put;
247         ccid->ccid_ops = ccid_ops;
248         if (rx) {
249                 memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
250                 if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
251                     ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
252                         goto out_free_ccid;
253         } else {
254                 memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
255                 if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
256                     ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
257                         goto out_free_ccid;
258         }
259 out:
260         return ccid;
261 out_unlock:
262         ccids_read_unlock();
263         goto out;
264 out_free_ccid:
265         kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
266                         ccid_ops->ccid_hc_tx_slab, ccid);
267         ccid = NULL;
268 out_module_put:
269         module_put(ccid_ops->ccid_owner);
270         goto out;
271 }
272
273 EXPORT_SYMBOL_GPL(ccid_new);
274
275 static void ccid_delete(struct ccid *ccid, struct sock *sk, int rx)
276 {
277         struct ccid_operations *ccid_ops;
278
279         if (ccid == NULL)
280                 return;
281
282         ccid_ops = ccid->ccid_ops;
283         if (rx) {
284                 if (ccid_ops->ccid_hc_rx_exit != NULL)
285                         ccid_ops->ccid_hc_rx_exit(sk);
286                 kmem_cache_free(ccid_ops->ccid_hc_rx_slab,  ccid);
287         } else {
288                 if (ccid_ops->ccid_hc_tx_exit != NULL)
289                         ccid_ops->ccid_hc_tx_exit(sk);
290                 kmem_cache_free(ccid_ops->ccid_hc_tx_slab,  ccid);
291         }
292         ccids_read_lock();
293         if (ccids[ccid_ops->ccid_id] != NULL)
294                 module_put(ccid_ops->ccid_owner);
295         ccids_read_unlock();
296 }
297
298 void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
299 {
300         ccid_delete(ccid, sk, 1);
301 }
302
303 EXPORT_SYMBOL_GPL(ccid_hc_rx_delete);
304
305 void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
306 {
307         ccid_delete(ccid, sk, 0);
308 }
309
310 EXPORT_SYMBOL_GPL(ccid_hc_tx_delete);