Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 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 |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 13 | #include "ccid.h" |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 14 | #include "ccids/lib/tfrc.h" |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 15 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 16 | static struct ccid_operations *ccids[] = { |
| 17 | &ccid2_ops, |
| 18 | #ifdef CONFIG_IP_DCCP_CCID3 |
| 19 | &ccid3_ops, |
| 20 | #endif |
| 21 | }; |
| 22 | |
| 23 | static 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 */ |
| 34 | bool 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 |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 46 | * |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 47 | * This function allocates memory - caller must see that it is freed after use. |
| 48 | */ |
| 49 | int 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 | |
| 60 | int 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 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 66 | if (ccid_get_builtin_ccids(&ccid_array, &array_len)) |
| 67 | return -ENOBUFS; |
| 68 | |
Gerrit Renker | 69a6a0b | 2010-02-07 20:20:28 +0000 | [diff] [blame] | 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)) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 73 | err = -EFAULT; |
| 74 | |
| 75 | kfree(ccid_array); |
| 76 | return err; |
| 77 | } |
| 78 | |
Andrew Lunn | c7bb868 | 2020-10-28 01:38:48 +0100 | [diff] [blame] | 79 | static __printf(3, 4) struct kmem_cache *ccid_kmem_cache_create(int obj_size, char *slab_name_fmt, const char *fmt,...) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 80 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 81 | struct kmem_cache *slab; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 82 | va_list args; |
| 83 | |
| 84 | va_start(args, fmt); |
Gerrit Renker | 8ed030d | 2010-02-01 02:12:19 +0000 | [diff] [blame] | 85 | vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 86 | va_end(args); |
| 87 | |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 88 | slab = kmem_cache_create(slab_name_fmt, sizeof(struct ccid) + obj_size, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 89 | SLAB_HWCACHE_ALIGN, NULL); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 90 | return slab; |
| 91 | } |
| 92 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 93 | static void ccid_kmem_cache_destroy(struct kmem_cache *slab) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 94 | { |
Julia Lawall | 20471ed | 2015-09-13 14:15:27 +0200 | [diff] [blame] | 95 | kmem_cache_destroy(slab); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Fabian Frederick | e500f48 | 2014-10-01 06:52:06 +0200 | [diff] [blame] | 98 | static int __init ccid_activate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 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, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 104 | ccid_ops->ccid_hc_rx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 105 | "ccid%u_hc_rx_sock", |
| 106 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 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, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 112 | ccid_ops->ccid_hc_tx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 113 | "ccid%u_hc_tx_sock", |
| 114 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 115 | if (ccid_ops->ccid_hc_tx_slab == NULL) |
| 116 | goto out_free_rx_slab; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 117 | |
Gerrit Renker | 1fd9d20 | 2011-07-03 09:51:14 -0600 | [diff] [blame] | 118 | pr_info("DCCP: Activated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 119 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 120 | err = 0; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 121 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 122 | return err; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 123 | out_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; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 129 | static void ccid_deactivate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 130 | { |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 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 | |
Gerrit Renker | 1fd9d20 | 2011-07-03 09:51:14 -0600 | [diff] [blame] | 136 | pr_info("DCCP: Deactivated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 137 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 140 | struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 141 | { |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 142 | struct ccid_operations *ccid_ops = ccid_by_number(id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 143 | struct ccid *ccid = NULL; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 144 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 145 | if (ccid_ops == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 146 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 147 | |
| 148 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 149 | ccid_ops->ccid_hc_tx_slab, gfp_any()); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 150 | if (ccid == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 151 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 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 | } |
| 164 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 165 | return ccid; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 166 | out_free_ccid: |
| 167 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : |
| 168 | ccid_ops->ccid_hc_tx_slab, ccid); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 169 | ccid = NULL; |
| 170 | goto out; |
| 171 | } |
| 172 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 173 | void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 174 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 179 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 182 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) |
| 183 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 191 | int __init ccid_initialize_builtins(void) |
| 192 | { |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 193 | int i, err = tfrc_lib_init(); |
| 194 | |
| 195 | if (err) |
| 196 | return err; |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 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 | |
| 205 | unwind_registrations: |
| 206 | while(--i >= 0) |
| 207 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 208 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 209 | return err; |
| 210 | } |
| 211 | |
| 212 | void ccid_cleanup_builtins(void) |
| 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
| 217 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 218 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 219 | } |