Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  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 <linux/slab.h>
 15
 16#include "ccid.h"
 17#include "ccids/lib/tfrc.h"
 18
 19static 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
 49 *
 50 * 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
 69	if (ccid_get_builtin_ccids(&ccid_array, &array_len))
 70		return -ENOBUFS;
 71
 72	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))
 76		err = -EFAULT;
 77
 78	kfree(ccid_array);
 79	return err;
 80}
 81
 82static struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
 83{
 84	struct kmem_cache *slab;
 85	va_list args;
 86
 87	va_start(args, fmt);
 88	vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
 89	va_end(args);
 90
 91	slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
 92				 SLAB_HWCACHE_ALIGN, NULL);
 93	return slab;
 94}
 95
 96static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
 97{
 98	if (slab != NULL)
 99		kmem_cache_destroy(slab);
100}
101
102static int ccid_activate(struct ccid_operations *ccid_ops)
103{
104	int err = -ENOBUFS;
105
106	ccid_ops->ccid_hc_rx_slab =
107			ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
108					       ccid_ops->ccid_hc_rx_slab_name,
109					       "ccid%u_hc_rx_sock",
110					       ccid_ops->ccid_id);
111	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,
116					       ccid_ops->ccid_hc_tx_slab_name,
117					       "ccid%u_hc_tx_sock",
118					       ccid_ops->ccid_id);
119	if (ccid_ops->ccid_hc_tx_slab == NULL)
120		goto out_free_rx_slab;
121
122	pr_info("DCCP: Activated CCID %d (%s)\n",
123		ccid_ops->ccid_id, ccid_ops->ccid_name);
124	err = 0;
125out:
126	return err;
127out_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;
131}
132
133static void ccid_deactivate(struct ccid_operations *ccid_ops)
134{
135	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
140	pr_info("DCCP: Deactivated CCID %d (%s)\n",
141		ccid_ops->ccid_id, ccid_ops->ccid_name);
142}
143
144struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
145{
146	struct ccid_operations *ccid_ops = ccid_by_number(id);
147	struct ccid *ccid = NULL;
148
149	if (ccid_ops == NULL)
150		goto out;
151
152	ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
153				     ccid_ops->ccid_hc_tx_slab, gfp_any());
154	if (ccid == NULL)
155		goto out;
156	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:
169	return ccid;
170out_free_ccid:
171	kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
172			ccid_ops->ccid_hc_tx_slab, ccid);
173	ccid = NULL;
174	goto out;
175}
176
177void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
178{
179	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);
183	}
184}
185
186void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
187{
188	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	}
193}
194
195int __init ccid_initialize_builtins(void)
196{
197	int i, err = tfrc_lib_init();
198
199	if (err)
200		return err;
201
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]);
212	tfrc_lib_exit();
213	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]);
222	tfrc_lib_exit();
223}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  net/dccp/ccid.c
  4 *
  5 *  An implementation of the DCCP protocol
  6 *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7 *
  8 *  CCID infrastructure
 
 
 
 
  9 */
 10
 11#include <linux/slab.h>
 12
 13#include "ccid.h"
 14#include "ccids/lib/tfrc.h"
 15
 16static struct ccid_operations *ccids[] = {
 17	&ccid2_ops,
 18#ifdef CONFIG_IP_DCCP_CCID3
 19	&ccid3_ops,
 20#endif
 21};
 22
 23static struct ccid_operations *ccid_by_number(const u8 id)
 24{
 25	int i;
 26
 27	for (i = 0; i < ARRAY_SIZE(ccids); i++)
 28		if (ccids[i]->ccid_id == id)
 29			return ccids[i];
 30	return NULL;
 31}
 32
 33/* check that up to @array_len members in @ccid_array are supported */
 34bool ccid_support_check(u8 const *ccid_array, u8 array_len)
 35{
 36	while (array_len > 0)
 37		if (ccid_by_number(ccid_array[--array_len]) == NULL)
 38			return false;
 39	return true;
 40}
 41
 42/**
 43 * ccid_get_builtin_ccids  -  Populate a list of built-in CCIDs
 44 * @ccid_array: pointer to copy into
 45 * @array_len: value to return length into
 46 *
 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
 66	if (ccid_get_builtin_ccids(&ccid_array, &array_len))
 67		return -ENOBUFS;
 68
 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;
 74
 75	kfree(ccid_array);
 76	return err;
 77}
 78
 79static __printf(3, 4) struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...)
 80{
 81	struct kmem_cache *slab;
 82	va_list args;
 83
 84	va_start(args, fmt);
 85	vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args);
 86	va_end(args);
 87
 88	slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0,
 89				 SLAB_HWCACHE_ALIGN, NULL);
 90	return slab;
 91}
 92
 93static void ccid_kmem_cache_destroy(struct kmem_cache *slab)
 94{
 95	kmem_cache_destroy(slab);
 
 96}
 97
 98static int __init ccid_activate(struct ccid_operations *ccid_ops)
 99{
100	int err = -ENOBUFS;
101
102	ccid_ops->ccid_hc_rx_slab =
103			ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size,
104					       ccid_ops->ccid_hc_rx_slab_name,
105					       "ccid%u_hc_rx_sock",
106					       ccid_ops->ccid_id);
107	if (ccid_ops->ccid_hc_rx_slab == NULL)
108		goto out;
109
110	ccid_ops->ccid_hc_tx_slab =
111			ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size,
112					       ccid_ops->ccid_hc_tx_slab_name,
113					       "ccid%u_hc_tx_sock",
114					       ccid_ops->ccid_id);
115	if (ccid_ops->ccid_hc_tx_slab == NULL)
116		goto out_free_rx_slab;
117
118	pr_info("DCCP: Activated CCID %d (%s)\n",
119		ccid_ops->ccid_id, ccid_ops->ccid_name);
120	err = 0;
121out:
122	return err;
123out_free_rx_slab:
124	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
125	ccid_ops->ccid_hc_rx_slab = NULL;
126	goto out;
127}
128
129static void ccid_deactivate(struct ccid_operations *ccid_ops)
130{
131	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
132	ccid_ops->ccid_hc_tx_slab = NULL;
133	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab);
134	ccid_ops->ccid_hc_rx_slab = NULL;
135
136	pr_info("DCCP: Deactivated CCID %d (%s)\n",
137		ccid_ops->ccid_id, ccid_ops->ccid_name);
138}
139
140struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx)
141{
142	struct ccid_operations *ccid_ops = ccid_by_number(id);
143	struct ccid *ccid = NULL;
144
145	if (ccid_ops == NULL)
146		goto out;
147
148	ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
149				     ccid_ops->ccid_hc_tx_slab, gfp_any());
150	if (ccid == NULL)
151		goto out;
152	ccid->ccid_ops = ccid_ops;
153	if (rx) {
154		memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size);
155		if (ccid->ccid_ops->ccid_hc_rx_init != NULL &&
156		    ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0)
157			goto out_free_ccid;
158	} else {
159		memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size);
160		if (ccid->ccid_ops->ccid_hc_tx_init != NULL &&
161		    ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0)
162			goto out_free_ccid;
163	}
164out:
165	return ccid;
166out_free_ccid:
167	kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
168			ccid_ops->ccid_hc_tx_slab, ccid);
169	ccid = NULL;
170	goto out;
171}
172
173void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
174{
175	if (ccid != NULL) {
176		if (ccid->ccid_ops->ccid_hc_rx_exit != NULL)
177			ccid->ccid_ops->ccid_hc_rx_exit(sk);
178		kmem_cache_free(ccid->ccid_ops->ccid_hc_rx_slab, ccid);
179	}
180}
181
182void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk)
183{
184	if (ccid != NULL) {
185		if (ccid->ccid_ops->ccid_hc_tx_exit != NULL)
186			ccid->ccid_ops->ccid_hc_tx_exit(sk);
187		kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid);
188	}
189}
190
191int __init ccid_initialize_builtins(void)
192{
193	int i, err = tfrc_lib_init();
194
195	if (err)
196		return err;
197
198	for (i = 0; i < ARRAY_SIZE(ccids); i++) {
199		err = ccid_activate(ccids[i]);
200		if (err)
201			goto unwind_registrations;
202	}
203	return 0;
204
205unwind_registrations:
206	while(--i >= 0)
207		ccid_deactivate(ccids[i]);
208	tfrc_lib_exit();
209	return err;
210}
211
212void ccid_cleanup_builtins(void)
213{
214	int i;
215
216	for (i = 0; i < ARRAY_SIZE(ccids); i++)
217		ccid_deactivate(ccids[i]);
218	tfrc_lib_exit();
219}