4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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 <linux/slab.h>
17 #include "ccids/lib/tfrc.h"
19 static struct ccid_operations
*ccids
[] = {
21 #ifdef CONFIG_IP_DCCP_CCID3
26 static struct ccid_operations
*ccid_by_number(const u8 id
)
30 for (i
= 0; i
< ARRAY_SIZE(ccids
); i
++)
31 if (ccids
[i
]->ccid_id
== id
)
36 /* check that up to @array_len members in @ccid_array are supported */
37 bool ccid_support_check(u8
const *ccid_array
, u8 array_len
)
40 if (ccid_by_number(ccid_array
[--array_len
]) == NULL
)
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
50 * This function allocates memory - caller must see that it is freed after use.
52 int ccid_get_builtin_ccids(u8
**ccid_array
, u8
*array_len
)
54 *ccid_array
= kmalloc(ARRAY_SIZE(ccids
), gfp_any());
55 if (*ccid_array
== NULL
)
58 for (*array_len
= 0; *array_len
< ARRAY_SIZE(ccids
); *array_len
+= 1)
59 (*ccid_array
)[*array_len
] = ccids
[*array_len
]->ccid_id
;
63 int ccid_getsockopt_builtin_ccids(struct sock
*sk
, int len
,
64 char __user
*optval
, int __user
*optlen
)
66 u8
*ccid_array
, array_len
;
69 if (ccid_get_builtin_ccids(&ccid_array
, &array_len
))
72 if (put_user(array_len
, optlen
))
74 else if (len
> 0 && copy_to_user(optval
, ccid_array
,
75 len
> array_len
? array_len
: len
))
82 static struct kmem_cache
*ccid_kmem_cache_create(int obj_size
, char *slab_name_fmt
, const char *fmt
,...)
84 struct kmem_cache
*slab
;
88 vsnprintf(slab_name_fmt
, CCID_SLAB_NAME_LENGTH
, fmt
, args
);
91 slab
= kmem_cache_create(slab_name_fmt
, sizeof(struct ccid
) + obj_size
, 0,
92 SLAB_HWCACHE_ALIGN
, NULL
);
96 static void ccid_kmem_cache_destroy(struct kmem_cache
*slab
)
98 kmem_cache_destroy(slab
);
101 static int __init
ccid_activate(struct ccid_operations
*ccid_ops
)
105 ccid_ops
->ccid_hc_rx_slab
=
106 ccid_kmem_cache_create(ccid_ops
->ccid_hc_rx_obj_size
,
107 ccid_ops
->ccid_hc_rx_slab_name
,
110 if (ccid_ops
->ccid_hc_rx_slab
== NULL
)
113 ccid_ops
->ccid_hc_tx_slab
=
114 ccid_kmem_cache_create(ccid_ops
->ccid_hc_tx_obj_size
,
115 ccid_ops
->ccid_hc_tx_slab_name
,
118 if (ccid_ops
->ccid_hc_tx_slab
== NULL
)
119 goto out_free_rx_slab
;
121 pr_info("DCCP: Activated CCID %d (%s)\n",
122 ccid_ops
->ccid_id
, ccid_ops
->ccid_name
);
127 ccid_kmem_cache_destroy(ccid_ops
->ccid_hc_rx_slab
);
128 ccid_ops
->ccid_hc_rx_slab
= NULL
;
132 static void ccid_deactivate(struct ccid_operations
*ccid_ops
)
134 ccid_kmem_cache_destroy(ccid_ops
->ccid_hc_tx_slab
);
135 ccid_ops
->ccid_hc_tx_slab
= NULL
;
136 ccid_kmem_cache_destroy(ccid_ops
->ccid_hc_rx_slab
);
137 ccid_ops
->ccid_hc_rx_slab
= NULL
;
139 pr_info("DCCP: Deactivated CCID %d (%s)\n",
140 ccid_ops
->ccid_id
, ccid_ops
->ccid_name
);
143 struct ccid
*ccid_new(const u8 id
, struct sock
*sk
, bool rx
)
145 struct ccid_operations
*ccid_ops
= ccid_by_number(id
);
146 struct ccid
*ccid
= NULL
;
148 if (ccid_ops
== NULL
)
151 ccid
= kmem_cache_alloc(rx
? ccid_ops
->ccid_hc_rx_slab
:
152 ccid_ops
->ccid_hc_tx_slab
, gfp_any());
155 ccid
->ccid_ops
= ccid_ops
;
157 memset(ccid
+ 1, 0, ccid_ops
->ccid_hc_rx_obj_size
);
158 if (ccid
->ccid_ops
->ccid_hc_rx_init
!= NULL
&&
159 ccid
->ccid_ops
->ccid_hc_rx_init(ccid
, sk
) != 0)
162 memset(ccid
+ 1, 0, ccid_ops
->ccid_hc_tx_obj_size
);
163 if (ccid
->ccid_ops
->ccid_hc_tx_init
!= NULL
&&
164 ccid
->ccid_ops
->ccid_hc_tx_init(ccid
, sk
) != 0)
170 kmem_cache_free(rx
? ccid_ops
->ccid_hc_rx_slab
:
171 ccid_ops
->ccid_hc_tx_slab
, ccid
);
176 void ccid_hc_rx_delete(struct ccid
*ccid
, struct sock
*sk
)
179 if (ccid
->ccid_ops
->ccid_hc_rx_exit
!= NULL
)
180 ccid
->ccid_ops
->ccid_hc_rx_exit(sk
);
181 kmem_cache_free(ccid
->ccid_ops
->ccid_hc_rx_slab
, ccid
);
185 void ccid_hc_tx_delete(struct ccid
*ccid
, struct sock
*sk
)
188 if (ccid
->ccid_ops
->ccid_hc_tx_exit
!= NULL
)
189 ccid
->ccid_ops
->ccid_hc_tx_exit(sk
);
190 kmem_cache_free(ccid
->ccid_ops
->ccid_hc_tx_slab
, ccid
);
194 int __init
ccid_initialize_builtins(void)
196 int i
, err
= tfrc_lib_init();
201 for (i
= 0; i
< ARRAY_SIZE(ccids
); i
++) {
202 err
= ccid_activate(ccids
[i
]);
204 goto unwind_registrations
;
208 unwind_registrations
:
210 ccid_deactivate(ccids
[i
]);
215 void ccid_cleanup_builtins(void)
219 for (i
= 0; i
< ARRAY_SIZE(ccids
); i
++)
220 ccid_deactivate(ccids
[i
]);