Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 16 | #include "ccid.h" |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 17 | #include "ccids/lib/tfrc.h" |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 18 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 19 | static struct ccid_operations *ccids[] = { |
| 20 | &ccid2_ops, |
| 21 | #ifdef CONFIG_IP_DCCP_CCID3 |
| 22 | &ccid3_ops, |
| 23 | #endif |
| 24 | }; |
| 25 | |
| 26 | static 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 */ |
| 37 | bool 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 Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 49 | * |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 50 | * This function allocates memory - caller must see that it is freed after use. |
| 51 | */ |
| 52 | int 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 | |
| 63 | int 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 Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 69 | if (ccid_get_builtin_ccids(&ccid_array, &array_len)) |
| 70 | return -ENOBUFS; |
| 71 | |
Gerrit Renker | 69a6a0b | 2010-02-07 20:20:28 +0000 | [diff] [blame] | 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)) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 76 | err = -EFAULT; |
| 77 | |
| 78 | kfree(ccid_array); |
| 79 | return err; |
| 80 | } |
| 81 | |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 82 | static 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] | 83 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 84 | struct kmem_cache *slab; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 85 | va_list args; |
| 86 | |
| 87 | va_start(args, fmt); |
Gerrit Renker | 8ed030d | 2010-02-01 02:12:19 +0000 | [diff] [blame] | 88 | vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 89 | va_end(args); |
| 90 | |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 91 | 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] | 92 | SLAB_HWCACHE_ALIGN, NULL); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 93 | return slab; |
| 94 | } |
| 95 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 96 | static void ccid_kmem_cache_destroy(struct kmem_cache *slab) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 97 | { |
Julia Lawall | 20471ed | 2015-09-13 14:15:27 +0200 | [diff] [blame] | 98 | kmem_cache_destroy(slab); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Fabian Frederick | e500f48 | 2014-10-01 06:52:06 +0200 | [diff] [blame] | 101 | static int __init ccid_activate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 102 | { |
| 103 | int err = -ENOBUFS; |
| 104 | |
| 105 | ccid_ops->ccid_hc_rx_slab = |
| 106 | ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 107 | ccid_ops->ccid_hc_rx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 108 | "ccid%u_hc_rx_sock", |
| 109 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 110 | if (ccid_ops->ccid_hc_rx_slab == NULL) |
| 111 | goto out; |
| 112 | |
| 113 | ccid_ops->ccid_hc_tx_slab = |
| 114 | ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 115 | ccid_ops->ccid_hc_tx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 116 | "ccid%u_hc_tx_sock", |
| 117 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 118 | if (ccid_ops->ccid_hc_tx_slab == NULL) |
| 119 | goto out_free_rx_slab; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 120 | |
Gerrit Renker | 1fd9d20 | 2011-07-03 09:51:14 -0600 | [diff] [blame] | 121 | pr_info("DCCP: Activated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 122 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 123 | err = 0; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 124 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 125 | return err; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 126 | out_free_rx_slab: |
| 127 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
| 128 | ccid_ops->ccid_hc_rx_slab = NULL; |
| 129 | goto out; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 132 | static void ccid_deactivate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 133 | { |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 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; |
| 138 | |
Gerrit Renker | 1fd9d20 | 2011-07-03 09:51:14 -0600 | [diff] [blame] | 139 | pr_info("DCCP: Deactivated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 140 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 143 | 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] | 144 | { |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 145 | struct ccid_operations *ccid_ops = ccid_by_number(id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 146 | struct ccid *ccid = NULL; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 147 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 148 | if (ccid_ops == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 149 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 150 | |
| 151 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 152 | ccid_ops->ccid_hc_tx_slab, gfp_any()); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 153 | if (ccid == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 154 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 155 | ccid->ccid_ops = ccid_ops; |
| 156 | if (rx) { |
| 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) |
| 160 | goto out_free_ccid; |
| 161 | } else { |
| 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) |
| 165 | goto out_free_ccid; |
| 166 | } |
| 167 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 168 | return ccid; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 169 | out_free_ccid: |
| 170 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : |
| 171 | ccid_ops->ccid_hc_tx_slab, ccid); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 172 | ccid = NULL; |
| 173 | goto out; |
| 174 | } |
| 175 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 176 | 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] | 177 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 178 | if (ccid != NULL) { |
| 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); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 182 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 185 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) |
| 186 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 187 | if (ccid != NULL) { |
| 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); |
| 191 | } |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 194 | int __init ccid_initialize_builtins(void) |
| 195 | { |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 196 | int i, err = tfrc_lib_init(); |
| 197 | |
| 198 | if (err) |
| 199 | return err; |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 200 | |
| 201 | for (i = 0; i < ARRAY_SIZE(ccids); i++) { |
| 202 | err = ccid_activate(ccids[i]); |
| 203 | if (err) |
| 204 | goto unwind_registrations; |
| 205 | } |
| 206 | return 0; |
| 207 | |
| 208 | unwind_registrations: |
| 209 | while(--i >= 0) |
| 210 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 211 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 212 | return err; |
| 213 | } |
| 214 | |
| 215 | void ccid_cleanup_builtins(void) |
| 216 | { |
| 217 | int i; |
| 218 | |
| 219 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
| 220 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 221 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 222 | } |