diff options
Diffstat (limited to 'net/dccp/ccid.c')
-rw-r--r-- | net/dccp/ccid.c | 254 |
1 files changed, 96 insertions, 158 deletions
diff --git a/net/dccp/ccid.c b/net/dccp/ccid.c index bcc643f992ae..f3e9ba1cfd01 100644 --- a/net/dccp/ccid.c +++ b/net/dccp/ccid.c | |||
@@ -12,56 +12,70 @@ | |||
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "ccid.h" | 14 | #include "ccid.h" |
15 | #include "ccids/lib/tfrc.h" | ||
15 | 16 | ||
16 | static u8 builtin_ccids[] = { | 17 | static struct ccid_operations *ccids[] = { |
17 | DCCPC_CCID2, /* CCID2 is supported by default */ | 18 | &ccid2_ops, |
18 | #if defined(CONFIG_IP_DCCP_CCID3) || defined(CONFIG_IP_DCCP_CCID3_MODULE) | 19 | #ifdef CONFIG_IP_DCCP_CCID3 |
19 | DCCPC_CCID3, | 20 | &ccid3_ops, |
20 | #endif | 21 | #endif |
21 | }; | 22 | }; |
22 | 23 | ||
23 | static struct ccid_operations *ccids[CCID_MAX]; | 24 | static struct ccid_operations *ccid_by_number(const u8 id) |
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 | { | 25 | { |
34 | spin_lock(&ccids_lock); | 26 | int i; |
35 | while (atomic_read(&ccids_lockct) != 0) { | 27 | |
36 | spin_unlock(&ccids_lock); | 28 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
37 | yield(); | 29 | if (ccids[i]->ccid_id == id) |
38 | spin_lock(&ccids_lock); | 30 | return ccids[i]; |
39 | } | 31 | return NULL; |
40 | } | 32 | } |
41 | 33 | ||
42 | static inline void ccids_write_unlock(void) | 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) | ||
43 | { | 36 | { |
44 | spin_unlock(&ccids_lock); | 37 | while (array_len > 0) |
38 | if (ccid_by_number(ccid_array[--array_len]) == NULL) | ||
39 | return false; | ||
40 | return true; | ||
45 | } | 41 | } |
46 | 42 | ||
47 | static inline void ccids_read_lock(void) | 43 | /** |
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. | ||
48 | */ | ||
49 | int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len) | ||
48 | { | 50 | { |
49 | atomic_inc(&ccids_lockct); | 51 | *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any()); |
50 | smp_mb__after_atomic_inc(); | 52 | if (*ccid_array == NULL) |
51 | spin_unlock_wait(&ccids_lock); | 53 | return -ENOBUFS; |
54 | |||
55 | for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1) | ||
56 | (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id; | ||
57 | return 0; | ||
52 | } | 58 | } |
53 | 59 | ||
54 | static inline void ccids_read_unlock(void) | 60 | int ccid_getsockopt_builtin_ccids(struct sock *sk, int len, |
61 | char __user *optval, int __user *optlen) | ||
55 | { | 62 | { |
56 | atomic_dec(&ccids_lockct); | 63 | u8 *ccid_array, array_len; |
57 | } | 64 | int err = 0; |
58 | 65 | ||
59 | #else | 66 | if (len < ARRAY_SIZE(ccids)) |
60 | #define ccids_write_lock() do { } while(0) | 67 | return -EINVAL; |
61 | #define ccids_write_unlock() do { } while(0) | 68 | |
62 | #define ccids_read_lock() do { } while(0) | 69 | if (ccid_get_builtin_ccids(&ccid_array, &array_len)) |
63 | #define ccids_read_unlock() do { } while(0) | 70 | return -ENOBUFS; |
64 | #endif | 71 | |
72 | if (put_user(array_len, optlen) || | ||
73 | copy_to_user(optval, ccid_array, array_len)) | ||
74 | err = -EFAULT; | ||
75 | |||
76 | kfree(ccid_array); | ||
77 | return err; | ||
78 | } | ||
65 | 79 | ||
66 | static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...) | 80 | static struct kmem_cache *ccid_kmem_cache_create(int obj_size, const char *fmt,...) |
67 | { | 81 | { |
@@ -93,48 +107,7 @@ static void ccid_kmem_cache_destroy(struct kmem_cache *slab) | |||
93 | } | 107 | } |
94 | } | 108 | } |
95 | 109 | ||
96 | /* check that up to @array_len members in @ccid_array are supported */ | 110 | static int ccid_activate(struct ccid_operations *ccid_ops) |
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 | { | 111 | { |
139 | int err = -ENOBUFS; | 112 | int err = -ENOBUFS; |
140 | 113 | ||
@@ -152,79 +125,40 @@ int ccid_register(struct ccid_operations *ccid_ops) | |||
152 | if (ccid_ops->ccid_hc_tx_slab == NULL) | 125 | if (ccid_ops->ccid_hc_tx_slab == NULL) |
153 | goto out_free_rx_slab; | 126 | goto out_free_rx_slab; |
154 | 127 | ||
155 | ccids_write_lock(); | 128 | pr_info("CCID: Activated CCID %d (%s)\n", |
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); | 129 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
130 | err = 0; | ||
167 | out: | 131 | out: |
168 | return err; | 132 | 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: | 133 | out_free_rx_slab: |
174 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); | 134 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
175 | ccid_ops->ccid_hc_rx_slab = NULL; | 135 | ccid_ops->ccid_hc_rx_slab = NULL; |
176 | goto out; | 136 | goto out; |
177 | } | 137 | } |
178 | 138 | ||
179 | EXPORT_SYMBOL_GPL(ccid_register); | 139 | static void ccid_deactivate(struct ccid_operations *ccid_ops) |
180 | |||
181 | int ccid_unregister(struct ccid_operations *ccid_ops) | ||
182 | { | 140 | { |
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); | 141 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab); |
188 | ccid_ops->ccid_hc_tx_slab = NULL; | 142 | ccid_ops->ccid_hc_tx_slab = NULL; |
189 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); | 143 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
190 | ccid_ops->ccid_hc_rx_slab = NULL; | 144 | ccid_ops->ccid_hc_rx_slab = NULL; |
191 | 145 | ||
192 | pr_info("CCID: Unregistered CCID %d (%s)\n", | 146 | pr_info("CCID: Deactivated CCID %d (%s)\n", |
193 | ccid_ops->ccid_id, ccid_ops->ccid_name); | 147 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
194 | return 0; | ||
195 | } | 148 | } |
196 | 149 | ||
197 | EXPORT_SYMBOL_GPL(ccid_unregister); | 150 | struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx) |
198 | |||
199 | struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp) | ||
200 | { | 151 | { |
201 | struct ccid_operations *ccid_ops; | 152 | struct ccid_operations *ccid_ops = ccid_by_number(id); |
202 | struct ccid *ccid = NULL; | 153 | struct ccid *ccid = NULL; |
203 | 154 | ||
204 | ccids_read_lock(); | ||
205 | #ifdef CONFIG_MODULES | ||
206 | if (ccids[id] == NULL) { | ||
207 | /* We only try to load if in process context */ | ||
208 | ccids_read_unlock(); | ||
209 | if (gfp & GFP_ATOMIC) | ||
210 | goto out; | ||
211 | request_module("net-dccp-ccid-%d", id); | ||
212 | ccids_read_lock(); | ||
213 | } | ||
214 | #endif | ||
215 | ccid_ops = ccids[id]; | ||
216 | if (ccid_ops == NULL) | 155 | if (ccid_ops == NULL) |
217 | goto out_unlock; | 156 | goto out; |
218 | |||
219 | if (!try_module_get(ccid_ops->ccid_owner)) | ||
220 | goto out_unlock; | ||
221 | |||
222 | ccids_read_unlock(); | ||
223 | 157 | ||
224 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : | 158 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : |
225 | ccid_ops->ccid_hc_tx_slab, gfp); | 159 | ccid_ops->ccid_hc_tx_slab, gfp_any()); |
226 | if (ccid == NULL) | 160 | if (ccid == NULL) |
227 | goto out_module_put; | 161 | goto out; |
228 | ccid->ccid_ops = ccid_ops; | 162 | ccid->ccid_ops = ccid_ops; |
229 | if (rx) { | 163 | if (rx) { |
230 | memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size); | 164 | memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size); |
@@ -239,53 +173,57 @@ struct ccid *ccid_new(unsigned char id, struct sock *sk, int rx, gfp_t gfp) | |||
239 | } | 173 | } |
240 | out: | 174 | out: |
241 | return ccid; | 175 | return ccid; |
242 | out_unlock: | ||
243 | ccids_read_unlock(); | ||
244 | goto out; | ||
245 | out_free_ccid: | 176 | out_free_ccid: |
246 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : | 177 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : |
247 | ccid_ops->ccid_hc_tx_slab, ccid); | 178 | ccid_ops->ccid_hc_tx_slab, ccid); |
248 | ccid = NULL; | 179 | ccid = NULL; |
249 | out_module_put: | ||
250 | module_put(ccid_ops->ccid_owner); | ||
251 | goto out; | 180 | goto out; |
252 | } | 181 | } |
253 | 182 | ||
254 | EXPORT_SYMBOL_GPL(ccid_new); | 183 | void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk) |
255 | |||
256 | static void ccid_delete(struct ccid *ccid, struct sock *sk, int rx) | ||
257 | { | 184 | { |
258 | struct ccid_operations *ccid_ops; | 185 | if (ccid != NULL) { |
259 | 186 | if (ccid->ccid_ops->ccid_hc_rx_exit != NULL) | |
260 | if (ccid == NULL) | 187 | ccid->ccid_ops->ccid_hc_rx_exit(sk); |
261 | return; | 188 | kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid); |
262 | |||
263 | ccid_ops = ccid->ccid_ops; | ||
264 | if (rx) { | ||
265 | if (ccid_ops->ccid_hc_rx_exit != NULL) | ||
266 | ccid_ops->ccid_hc_rx_exit(sk); | ||
267 | kmem_cache_free(ccid_ops->ccid_hc_rx_slab, ccid); | ||
268 | } else { | ||
269 | if (ccid_ops->ccid_hc_tx_exit != NULL) | ||
270 | ccid_ops->ccid_hc_tx_exit(sk); | ||
271 | kmem_cache_free(ccid_ops->ccid_hc_tx_slab, ccid); | ||
272 | } | 189 | } |
273 | ccids_read_lock(); | ||
274 | if (ccids[ccid_ops->ccid_id] != NULL) | ||
275 | module_put(ccid_ops->ccid_owner); | ||
276 | ccids_read_unlock(); | ||
277 | } | 190 | } |
278 | 191 | ||
279 | void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk) | 192 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) |
280 | { | 193 | { |
281 | ccid_delete(ccid, sk, 1); | 194 | if (ccid != NULL) { |
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); | ||
198 | } | ||
282 | } | 199 | } |
283 | 200 | ||
284 | EXPORT_SYMBOL_GPL(ccid_hc_rx_delete); | 201 | int __init ccid_initialize_builtins(void) |
285 | |||
286 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) | ||
287 | { | 202 | { |
288 | ccid_delete(ccid, sk, 0); | 203 | int i, err = tfrc_lib_init(); |
204 | |||
205 | if (err) | ||
206 | return err; | ||
207 | |||
208 | for (i = 0; i < ARRAY_SIZE(ccids); i++) { | ||
209 | err = ccid_activate(ccids[i]); | ||
210 | if (err) | ||
211 | goto unwind_registrations; | ||
212 | } | ||
213 | return 0; | ||
214 | |||
215 | unwind_registrations: | ||
216 | while(--i >= 0) | ||
217 | ccid_deactivate(ccids[i]); | ||
218 | tfrc_lib_exit(); | ||
219 | return err; | ||
289 | } | 220 | } |
290 | 221 | ||
291 | EXPORT_SYMBOL_GPL(ccid_hc_tx_delete); | 222 | void ccid_cleanup_builtins(void) |
223 | { | ||
224 | int i; | ||
225 | |||
226 | for (i = 0; i < ARRAY_SIZE(ccids); i++) | ||
227 | ccid_deactivate(ccids[i]); | ||
228 | tfrc_lib_exit(); | ||
229 | } | ||