blob: 49d27c556becbb3423fcff11a1561a2b864319cc [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
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"
Gerrit Renker129fa442009-01-04 21:45:33 -080015#include "ccids/lib/tfrc.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070016
Gerrit Renkerddebc972009-01-04 21:42:53 -080017static struct ccid_operations *ccids[] = {
18 &ccid2_ops,
19#ifdef CONFIG_IP_DCCP_CCID3
20 &ccid3_ops,
21#endif
22};
23
24static struct ccid_operations *ccid_by_number(const u8 id)
25{
26 int i;
27
28 for (i = 0; i < ARRAY_SIZE(ccids); i++)
29 if (ccids[i]->ccid_id == id)
30 return ccids[i];
31 return NULL;
32}
33
34/* check that up to @array_len members in @ccid_array are supported */
35bool ccid_support_check(u8 const *ccid_array, u8 array_len)
36{
37 while (array_len > 0)
38 if (ccid_by_number(ccid_array[--array_len]) == NULL)
39 return false;
40 return true;
41}
42
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 */
49int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
50{
51 *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
52 if (*ccid_array == NULL)
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;
58}
59
60int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
61 char __user *optval, int __user *optlen)
62{
63 u8 *ccid_array, array_len;
64 int err = 0;
65
Gerrit Renkerddebc972009-01-04 21:42:53 -080066 if (ccid_get_builtin_ccids(&ccid_array, &array_len))
67 return -ENOBUFS;
68
Gerrit Renker69a6a0b2010-02-07 20:20:28 +000069 if (put_user(array_len, optlen))
70 err = -EFAULT;
71 else if (len > 0 && copy_to_user(optval, ccid_array,
72 len > array_len ? array_len : len))
Gerrit Renkerddebc972009-01-04 21:42:53 -080073 err = -EFAULT;
74
75 kfree(ccid_array);
76 return err;
77}
78
Neil Hormande4ef862010-01-17 17:16:12 +000079static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070080{
Christoph Lametere18b8902006-12-06 20:33:20 -080081 struct kmem_cache *slab;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080082 va_list args;
83
84 va_start(args, fmt);
Gerrit Renker8ed030d2010-02-01 02:12:19 +000085 vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080086 va_end(args);
87
Neil Hormande4ef862010-01-17 17:16:12 +000088 slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090089 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080090 return slab;
91}
92
Christoph Lametere18b8902006-12-06 20:33:20 -080093static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080094{
Neil Hormande4ef862010-01-17 17:16:12 +000095 if (slab != NULL)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080096 kmem_cache_destroy(slab);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080097}
98
Gerrit Renkerddebc972009-01-04 21:42:53 -080099static int ccid_activate(struct ccid_operations *ccid_ops)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800100{
101 int err = -ENOBUFS;
102
103 ccid_ops->ccid_hc_rx_slab =
104 ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
Neil Hormande4ef862010-01-17 17:16:12 +0000105 ccid_ops->ccid_hc_rx_slab_name,
Gerrit Renker84a97b02007-12-13 23:33:25 -0200106 "ccid%u_hc_rx_sock",
107 ccid_ops->ccid_id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800108 if (ccid_ops->ccid_hc_rx_slab == NULL)
109 goto out;
110
111 ccid_ops->ccid_hc_tx_slab =
112 ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
Neil Hormande4ef862010-01-17 17:16:12 +0000113 ccid_ops->ccid_hc_tx_slab_name,
Gerrit Renker84a97b02007-12-13 23:33:25 -0200114 "ccid%u_hc_tx_sock",
115 ccid_ops->ccid_id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800116 if (ccid_ops->ccid_hc_tx_slab == NULL)
117 goto out_free_rx_slab;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700118
Gerrit Renkerddebc972009-01-04 21:42:53 -0800119 pr_info("CCID: Activated CCID %d (%s)\n",
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800120 ccid_ops->ccid_id, ccid_ops->ccid_name);
Gerrit Renkerddebc972009-01-04 21:42:53 -0800121 err = 0;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800122out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700123 return err;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800124out_free_rx_slab:
125 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
126 ccid_ops->ccid_hc_rx_slab = NULL;
127 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700128}
129
Gerrit Renkerddebc972009-01-04 21:42:53 -0800130static void ccid_deactivate(struct ccid_operations *ccid_ops)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700131{
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800132 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
133 ccid_ops->ccid_hc_tx_slab = NULL;
134 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
135 ccid_ops->ccid_hc_rx_slab = NULL;
136
Gerrit Renkerddebc972009-01-04 21:42:53 -0800137 pr_info("CCID: Deactivated CCID %d (%s)\n",
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800138 ccid_ops->ccid_id, ccid_ops->ccid_name);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700139}
140
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800141struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700142{
Gerrit Renkerddebc972009-01-04 21:42:53 -0800143 struct ccid_operations *ccid_ops = ccid_by_number(id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800144 struct ccid *ccid = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700145
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800146 if (ccid_ops == NULL)
Gerrit Renkerddebc972009-01-04 21:42:53 -0800147 goto out;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800148
149 ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800150 ccid_ops->ccid_hc_tx_slab, gfp_any());
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800151 if (ccid == NULL)
Gerrit Renkerddebc972009-01-04 21:42:53 -0800152 goto out;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800153 ccid->ccid_ops = ccid_ops;
154 if (rx) {
155 memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
156 if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
157 ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
158 goto out_free_ccid;
159 } else {
160 memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
161 if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
162 ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
163 goto out_free_ccid;
164 }
165out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700166 return ccid;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800167out_free_ccid:
168 kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
169 ccid_ops->ccid_hc_tx_slab, ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700170 ccid = NULL;
171 goto out;
172}
173
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800174void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800175{
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800176 if (ccid != NULL) {
177 if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
178 ccid->ccid_ops->ccid_hc_rx_exit(sk);
179 kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700180 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700181}
182
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800183void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
184{
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800185 if (ccid != NULL) {
186 if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
187 ccid->ccid_ops->ccid_hc_tx_exit(sk);
188 kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
189 }
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800190}
191
Gerrit Renkerddebc972009-01-04 21:42:53 -0800192int __init ccid_initialize_builtins(void)
193{
Gerrit Renker129fa442009-01-04 21:45:33 -0800194 int i, err = tfrc_lib_init();
195
196 if (err)
197 return err;
Gerrit Renkerddebc972009-01-04 21:42:53 -0800198
199 for (i = 0; i < ARRAY_SIZE(ccids); i++) {
200 err = ccid_activate(ccids[i]);
201 if (err)
202 goto unwind_registrations;
203 }
204 return 0;
205
206unwind_registrations:
207 while(--i >= 0)
208 ccid_deactivate(ccids[i]);
Gerrit Renker129fa442009-01-04 21:45:33 -0800209 tfrc_lib_exit();
Gerrit Renkerddebc972009-01-04 21:42:53 -0800210 return err;
211}
212
213void ccid_cleanup_builtins(void)
214{
215 int i;
216
217 for (i = 0; i < ARRAY_SIZE(ccids); i++)
218 ccid_deactivate(ccids[i]);
Gerrit Renker129fa442009-01-04 21:45:33 -0800219 tfrc_lib_exit();
Gerrit Renkerddebc972009-01-04 21:42:53 -0800220}