blob: 597557254ddb42dd9e58e8cb5b3594f07fc34d10 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
15
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070016#include "ccid.h"
Gerrit Renker129fa442009-01-04 21:45:33 -080017#include "ccids/lib/tfrc.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070018
Gerrit Renkerddebc972009-01-04 21:42:53 -080019static struct ccid_operations *ccids[] = {
20 &ccid2_ops,
21#ifdef CONFIG_IP_DCCP_CCID3
22 &ccid3_ops,
23#endif
24};
25
26static struct ccid_operations *ccid_by_number(const u8 id)
27{
28 int i;
29
30 for (i = 0; i < ARRAY_SIZE(ccids); i++)
31 if (ccids[i]->ccid_id == id)
32 return ccids[i];
33 return NULL;
34}
35
36/* check that up to @array_len members in @ccid_array are supported */
37bool ccid_support_check(u8 const *ccid_array, u8 array_len)
38{
39 while (array_len > 0)
40 if (ccid_by_number(ccid_array[--array_len]) == NULL)
41 return false;
42 return true;
43}
44
45/**
46 * ccid_get_builtin_ccids - Populate a list of built-in CCIDs
47 * @ccid_array: pointer to copy into
48 * @array_len: value to return length into
Ben Hutchings2c530402012-07-10 10:55:09 +000049 *
Gerrit Renkerddebc972009-01-04 21:42:53 -080050 * This function allocates memory - caller must see that it is freed after use.
51 */
52int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
53{
54 *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
55 if (*ccid_array == NULL)
56 return -ENOBUFS;
57
58 for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1)
59 (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id;
60 return 0;
61}
62
63int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
64 char __user *optval, int __user *optlen)
65{
66 u8 *ccid_array, array_len;
67 int err = 0;
68
Gerrit Renkerddebc972009-01-04 21:42:53 -080069 if (ccid_get_builtin_ccids(&ccid_array, &array_len))
70 return -ENOBUFS;
71
Gerrit Renker69a6a0b2010-02-07 20:20:28 +000072 if (put_user(array_len, optlen))
73 err = -EFAULT;
74 else if (len > 0 && copy_to_user(optval, ccid_array,
75 len > array_len ? array_len : len))
Gerrit Renkerddebc972009-01-04 21:42:53 -080076 err = -EFAULT;
77
78 kfree(ccid_array);
79 return err;
80}
81
Neil Hormande4ef862010-01-17 17:16:12 +000082static 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 -070083{
Christoph Lametere18b8902006-12-06 20:33:20 -080084 struct kmem_cache *slab;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080085 va_list args;
86
87 va_start(args, fmt);
Gerrit Renker8ed030d2010-02-01 02:12:19 +000088 vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080089 va_end(args);
90
Neil Hormande4ef862010-01-17 17:16:12 +000091 slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090092 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080093 return slab;
94}
95
Christoph Lametere18b8902006-12-06 20:33:20 -080096static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080097{
Neil Hormande4ef862010-01-17 17:16:12 +000098 if (slab != NULL)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -080099 kmem_cache_destroy(slab);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800100}
101
Gerrit Renkerddebc972009-01-04 21:42:53 -0800102static int ccid_activate(struct ccid_operations *ccid_ops)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800103{
104 int err = -ENOBUFS;
105
106 ccid_ops->ccid_hc_rx_slab =
107 ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
Neil Hormande4ef862010-01-17 17:16:12 +0000108 ccid_ops->ccid_hc_rx_slab_name,
Gerrit Renker84a97b02007-12-13 23:33:25 -0200109 "ccid%u_hc_rx_sock",
110 ccid_ops->ccid_id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800111 if (ccid_ops->ccid_hc_rx_slab == NULL)
112 goto out;
113
114 ccid_ops->ccid_hc_tx_slab =
115 ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
Neil Hormande4ef862010-01-17 17:16:12 +0000116 ccid_ops->ccid_hc_tx_slab_name,
Gerrit Renker84a97b02007-12-13 23:33:25 -0200117 "ccid%u_hc_tx_sock",
118 ccid_ops->ccid_id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800119 if (ccid_ops->ccid_hc_tx_slab == NULL)
120 goto out_free_rx_slab;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700121
Gerrit Renker1fd9d202011-07-03 09:51:14 -0600122 pr_info("DCCP: Activated CCID %d (%s)\n",
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800123 ccid_ops->ccid_id, ccid_ops->ccid_name);
Gerrit Renkerddebc972009-01-04 21:42:53 -0800124 err = 0;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800125out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700126 return err;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800127out_free_rx_slab:
128 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
129 ccid_ops->ccid_hc_rx_slab = NULL;
130 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700131}
132
Gerrit Renkerddebc972009-01-04 21:42:53 -0800133static void ccid_deactivate(struct ccid_operations *ccid_ops)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700134{
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800135 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
136 ccid_ops->ccid_hc_tx_slab = NULL;
137 ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
138 ccid_ops->ccid_hc_rx_slab = NULL;
139
Gerrit Renker1fd9d202011-07-03 09:51:14 -0600140 pr_info("DCCP: Deactivated CCID %d (%s)\n",
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800141 ccid_ops->ccid_id, ccid_ops->ccid_name);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700142}
143
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800144struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700145{
Gerrit Renkerddebc972009-01-04 21:42:53 -0800146 struct ccid_operations *ccid_ops = ccid_by_number(id);
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800147 struct ccid *ccid = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700148
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800149 if (ccid_ops == NULL)
Gerrit Renkerddebc972009-01-04 21:42:53 -0800150 goto out;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800151
152 ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800153 ccid_ops->ccid_hc_tx_slab, gfp_any());
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800154 if (ccid == NULL)
Gerrit Renkerddebc972009-01-04 21:42:53 -0800155 goto out;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800156 ccid->ccid_ops = ccid_ops;
157 if (rx) {
158 memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
159 if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
160 ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
161 goto out_free_ccid;
162 } else {
163 memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
164 if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
165 ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
166 goto out_free_ccid;
167 }
168out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700169 return ccid;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800170out_free_ccid:
171 kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
172 ccid_ops->ccid_hc_tx_slab, ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700173 ccid = NULL;
174 goto out;
175}
176
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800177void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800178{
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800179 if (ccid != NULL) {
180 if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
181 ccid->ccid_ops->ccid_hc_rx_exit(sk);
182 kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700183 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700184}
185
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800186void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
187{
Gerrit Renkere5fd56c2009-01-04 21:43:23 -0800188 if (ccid != NULL) {
189 if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
190 ccid->ccid_ops->ccid_hc_tx_exit(sk);
191 kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
192 }
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800193}
194
Gerrit Renkerddebc972009-01-04 21:42:53 -0800195int __init ccid_initialize_builtins(void)
196{
Gerrit Renker129fa442009-01-04 21:45:33 -0800197 int i, err = tfrc_lib_init();
198
199 if (err)
200 return err;
Gerrit Renkerddebc972009-01-04 21:42:53 -0800201
202 for (i = 0; i < ARRAY_SIZE(ccids); i++) {
203 err = ccid_activate(ccids[i]);
204 if (err)
205 goto unwind_registrations;
206 }
207 return 0;
208
209unwind_registrations:
210 while(--i >= 0)
211 ccid_deactivate(ccids[i]);
Gerrit Renker129fa442009-01-04 21:45:33 -0800212 tfrc_lib_exit();
Gerrit Renkerddebc972009-01-04 21:42:53 -0800213 return err;
214}
215
216void ccid_cleanup_builtins(void)
217{
218 int i;
219
220 for (i = 0; i < ARRAY_SIZE(ccids); i++)
221 ccid_deactivate(ccids[i]);
Gerrit Renker129fa442009-01-04 21:45:33 -0800222 tfrc_lib_exit();
Gerrit Renkerddebc972009-01-04 21:42:53 -0800223}