ccid.c 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  net/dccp/ccid.c
 *
 *  An implementation of the DCCP protocol
 *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *
 *  CCID infrastructure
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 */

#include "ccid.h"
15
#include "ccids/lib/tfrc.h"
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static struct ccid_operations *ccids[] = {
	&ccid2_ops,
#ifdef CONFIG_IP_DCCP_CCID3
	&ccid3_ops,
#endif
};

static struct ccid_operations *ccid_by_number(const u8 id)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ccids); i++)
		if (ccids[i]->ccid_id == id)
			return ccids[i];
	return NULL;
}

/* check that up to @array_len members in @ccid_array are supported */
bool ccid_support_check(u8 const *ccid_array, u8 array_len)
{
	while (array_len > 0)
		if (ccid_by_number(ccid_array[--array_len]) == NULL)
			return false;
	return true;
}

/**
 * ccid_get_builtin_ccids  -  Populate a list of built-in CCIDs
 * @ccid_array: pointer to copy into
 * @array_len: value to return length into
 * This function allocates memory - caller must see that it is freed after use.
 */
int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
{
	*ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any());
	if (*ccid_array == NULL)
		return -ENOBUFS;

	for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1)
		(*ccid_array)[*array_len] = ccids[*array_len]->ccid_id;
	return 0;
}

int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
				  char __user *optval, int __user *optlen)
{
	u8 *ccid_array, array_len;
	int err = 0;

	if (len < ARRAY_SIZE(ccids))
		return -EINVAL;

	if (ccid_get_builtin_ccids(&ccid_array, &array_len))
		return -ENOBUFS;

	if (put_user(array_len, optlen) ||
	    copy_to_user(optval, ccid_array, array_len))
		err = -EFAULT;

	kfree(ccid_array);
	return err;
}

80
static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
81
{
82
	struct kmem_cache *slab;
83 84 85
	va_list args;

	va_start(args, fmt);
86
	vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
87 88
	va_end(args);

89
	slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
90
				 SLAB_HWCACHE_ALIGN, NULL);
91 92 93
	return slab;
}

94
static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
95
{
96
	if (slab != NULL)
97 98 99
		kmem_cache_destroy(slab);
}

100
static int ccid_activate(struct ccid_operations *ccid_ops)
101 102 103 104 105
{
	int err = -ENOBUFS;

	ccid_ops->ccid_hc_rx_slab =
			ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
106
					       ccid_ops->ccid_hc_rx_slab_name,
107 108
					       "ccid%u_hc_rx_sock",
					       ccid_ops->ccid_id);
109 110 111 112 113
	if (ccid_ops->ccid_hc_rx_slab == NULL)
		goto out;

	ccid_ops->ccid_hc_tx_slab =
			ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
114
					       ccid_ops->ccid_hc_tx_slab_name,
115 116
					       "ccid%u_hc_tx_sock",
					       ccid_ops->ccid_id);
117 118
	if (ccid_ops->ccid_hc_tx_slab == NULL)
		goto out_free_rx_slab;
119

120
	pr_info("CCID: Activated CCID %d (%s)\n",
121
		ccid_ops->ccid_id, ccid_ops->ccid_name);
122
	err = 0;
123
out:
124
	return err;
125 126 127 128
out_free_rx_slab:
	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
	ccid_ops->ccid_hc_rx_slab = NULL;
	goto out;
129 130
}

131
static void ccid_deactivate(struct ccid_operations *ccid_ops)
132
{
133 134 135 136 137
	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
	ccid_ops->ccid_hc_tx_slab = NULL;
	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
	ccid_ops->ccid_hc_rx_slab = NULL;

138
	pr_info("CCID: Deactivated CCID %d (%s)\n",
139
		ccid_ops->ccid_id, ccid_ops->ccid_name);
140 141
}

142
struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
143
{
144
	struct ccid_operations *ccid_ops = ccid_by_number(id);
145
	struct ccid *ccid = NULL;
146

147
	if (ccid_ops == NULL)
148
		goto out;
149

150
	ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
151
				     ccid_ops->ccid_hc_tx_slab, gfp_any());
152
	if (ccid == NULL)
153
		goto out;
154 155 156 157 158 159 160 161 162 163 164 165
	ccid->ccid_ops = ccid_ops;
	if (rx) {
		memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
		if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
		    ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
			goto out_free_ccid;
	} else {
		memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
		if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
		    ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
			goto out_free_ccid;
	}
166 167
out:
	return ccid;
168 169 170
out_free_ccid:
	kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
			ccid_ops->ccid_hc_tx_slab, ccid);
171 172 173 174
	ccid = NULL;
	goto out;
}

175 176
void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
{
177 178 179 180 181
	if (ccid != NULL) {
		if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
			ccid->ccid_ops->ccid_hc_rx_exit(sk);
		kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
	}
182
}
183

184 185
void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
{
186 187 188 189 190
	if (ccid != NULL) {
		if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
			ccid->ccid_ops->ccid_hc_tx_exit(sk);
		kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
	}
191 192
}

193 194
int __init ccid_initialize_builtins(void)
{
195 196 197 198
	int i, err = tfrc_lib_init();

	if (err)
		return err;
199 200 201 202 203 204 205 206 207 208 209

	for (i = 0; i < ARRAY_SIZE(ccids); i++) {
		err = ccid_activate(ccids[i]);
		if (err)
			goto unwind_registrations;
	}
	return 0;

unwind_registrations:
	while(--i >= 0)
		ccid_deactivate(ccids[i]);
210
	tfrc_lib_exit();
211 212 213 214 215 216 217 218 219
	return err;
}

void ccid_cleanup_builtins(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ccids); i++)
		ccid_deactivate(ccids[i]);
220
	tfrc_lib_exit();
221
}