drbd: Renamed overwrite_peer to primary_force
[linux-2.6/next.git] / net / dccp / ccid.c
blob49d27c556becbb3423fcff11a1561a2b864319cc
1 /*
2 * net/dccp/ccid.c
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * CCID infrastructure
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.
14 #include "ccid.h"
15 #include "ccids/lib/tfrc.h"
17 static struct ccid_operations *ccids[] = {
18 &ccid2_ops,
19 #ifdef CONFIG_IP_DCCP_CCID3
20 &ccid3_ops,
21 #endif
24 static struct ccid_operations *ccid_by_number(const u8 id)
26 int i;
28 for (i = 0; i < ARRAY_SIZE(ccids); i++)
29 if (ccids[i]->ccid_id == id)
30 return ccids[i];
31 return NULL;
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)
37 while (array_len > 0)
38 if (ccid_by_number(ccid_array[--array_len]) == NULL)
39 return false;
40 return true;
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.
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)
53 return -ENOBUFS;
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;
60 int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
61 char __user *optval, int __user *optlen)
63 u8 *ccid_array, array_len;
64 int err = 0;
66 if (ccid_get_builtin_ccids(&ccid_array, &array_len))
67 return -ENOBUFS;
69 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))
73 err = -EFAULT;
75 kfree(ccid_array);
76 return err;
79 static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
81 struct kmem_cache *slab;
82 va_list args;
84 va_start(args, fmt);
85 vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
86 va_end(args);
88 slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
89 SLAB_HWCACHE_ALIGN, NULL);
90 return slab;
93 static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
95 if (slab != NULL)
96 kmem_cache_destroy(slab);
99 static int ccid_activate(struct ccid_operations *ccid_ops)
101 int err = -ENOBUFS;
103 ccid_ops->ccid_hc_rx_slab =
104 ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
105 ccid_ops->ccid_hc_rx_slab_name,
106 "ccid%u_hc_rx_sock",
107 ccid_ops->ccid_id);
108 if (ccid_ops->ccid_hc_rx_slab == NULL)
109 goto out;
111 ccid_ops->ccid_hc_tx_slab =
112 ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
113 ccid_ops->ccid_hc_tx_slab_name,
114 "ccid%u_hc_tx_sock",
115 ccid_ops->ccid_id);
116 if (ccid_ops->ccid_hc_tx_slab == NULL)
117 goto out_free_rx_slab;
119 pr_info("CCID: Activated CCID %d (%s)\n",
120 ccid_ops->ccid_id, ccid_ops->ccid_name);
121 err = 0;
122 out:
123 return err;
124 out_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;
130 static void ccid_deactivate(struct ccid_operations *ccid_ops)
132 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;
137 pr_info("CCID: Deactivated CCID %d (%s)\n",
138 ccid_ops->ccid_id, ccid_ops->ccid_name);
141 struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
143 struct ccid_operations *ccid_ops = ccid_by_number(id);
144 struct ccid *ccid = NULL;
146 if (ccid_ops == NULL)
147 goto out;
149 ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
150 ccid_ops->ccid_hc_tx_slab, gfp_any());
151 if (ccid == NULL)
152 goto out;
153 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;
165 out:
166 return ccid;
167 out_free_ccid:
168 kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
169 ccid_ops->ccid_hc_tx_slab, ccid);
170 ccid = NULL;
171 goto out;
174 void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
176 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);
183 void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
185 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);
192 int __init ccid_initialize_builtins(void)
194 int i, err = tfrc_lib_init();
196 if (err)
197 return err;
199 for (i = 0; i < ARRAY_SIZE(ccids); i++) {
200 err = ccid_activate(ccids[i]);
201 if (err)
202 goto unwind_registrations;
204 return 0;
206 unwind_registrations:
207 while(--i >= 0)
208 ccid_deactivate(ccids[i]);
209 tfrc_lib_exit();
210 return err;
213 void ccid_cleanup_builtins(void)
215 int i;
217 for (i = 0; i < ARRAY_SIZE(ccids); i++)
218 ccid_deactivate(ccids[i]);
219 tfrc_lib_exit();