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 | |
| 14 | #include "ccid.h" |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 15 | #include "ccids/lib/tfrc.h" |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 16 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 17 | static struct ccid_operations *ccids[] = { |
| 18 | &ccid2_ops, |
| 19 | #ifdef CONFIG_IP_DCCP_CCID3 |
| 20 | &ccid3_ops, |
| 21 | #endif |
| 22 | }; |
| 23 | |
| 24 | static struct ccid_operations *ccid_by_number(const u8 id) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
| 29 | if (ccids[i]->ccid_id == id) |
| 30 | return ccids[i]; |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | /* check that up to @array_len members in @ccid_array are supported */ |
| 35 | bool ccid_support_check(u8 const *ccid_array, u8 array_len) |
| 36 | { |
| 37 | while (array_len > 0) |
| 38 | if (ccid_by_number(ccid_array[--array_len]) == NULL) |
| 39 | return false; |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * ccid_get_builtin_ccids - Populate a list of built-in CCIDs |
| 45 | * @ccid_array: pointer to copy into |
| 46 | * @array_len: value to return length into |
| 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 | |
| 66 | if (len < ARRAY_SIZE(ccids)) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | if (ccid_get_builtin_ccids(&ccid_array, &array_len)) |
| 70 | return -ENOBUFS; |
| 71 | |
| 72 | if (put_user(array_len, optlen) || |
| 73 | copy_to_user(optval, ccid_array, array_len)) |
| 74 | err = -EFAULT; |
| 75 | |
| 76 | kfree(ccid_array); |
| 77 | return err; |
| 78 | } |
| 79 | |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 80 | 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] | 81 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 82 | struct kmem_cache *slab; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 83 | va_list args; |
| 84 | |
| 85 | va_start(args, fmt); |
Gerrit Renker | 8ed030d | 2010-02-01 02:12:19 +0000 | [diff] [blame] | 86 | vsnprintf(slab_name_fmt, CCID_SLAB_NAME_LENGTH, fmt, args); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 87 | va_end(args); |
| 88 | |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 89 | 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] | 90 | SLAB_HWCACHE_ALIGN, NULL); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 91 | return slab; |
| 92 | } |
| 93 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 94 | static void ccid_kmem_cache_destroy(struct kmem_cache *slab) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 95 | { |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 96 | if (slab != NULL) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 97 | kmem_cache_destroy(slab); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 100 | static int ccid_activate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 101 | { |
| 102 | int err = -ENOBUFS; |
| 103 | |
| 104 | ccid_ops->ccid_hc_rx_slab = |
| 105 | ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 106 | ccid_ops->ccid_hc_rx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 107 | "ccid%u_hc_rx_sock", |
| 108 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 109 | if (ccid_ops->ccid_hc_rx_slab == NULL) |
| 110 | goto out; |
| 111 | |
| 112 | ccid_ops->ccid_hc_tx_slab = |
| 113 | ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size, |
Neil Horman | de4ef86 | 2010-01-17 17:16:12 +0000 | [diff] [blame] | 114 | ccid_ops->ccid_hc_tx_slab_name, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 115 | "ccid%u_hc_tx_sock", |
| 116 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 117 | if (ccid_ops->ccid_hc_tx_slab == NULL) |
| 118 | goto out_free_rx_slab; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 119 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 120 | pr_info("CCID: Activated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 121 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 122 | err = 0; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 123 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 124 | return err; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 125 | out_free_rx_slab: |
| 126 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
| 127 | ccid_ops->ccid_hc_rx_slab = NULL; |
| 128 | goto out; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 131 | static void ccid_deactivate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 132 | { |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 133 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab); |
| 134 | ccid_ops->ccid_hc_tx_slab = NULL; |
| 135 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
| 136 | ccid_ops->ccid_hc_rx_slab = NULL; |
| 137 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 138 | pr_info("CCID: Deactivated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 139 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 142 | 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] | 143 | { |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 144 | struct ccid_operations *ccid_ops = ccid_by_number(id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 145 | struct ccid *ccid = NULL; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 146 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 147 | if (ccid_ops == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 148 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 149 | |
| 150 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 151 | ccid_ops->ccid_hc_tx_slab, gfp_any()); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 152 | if (ccid == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 153 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 154 | ccid->ccid_ops = ccid_ops; |
| 155 | if (rx) { |
| 156 | memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size); |
| 157 | if (ccid->ccid_ops->ccid_hc_rx_init != NULL && |
| 158 | ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0) |
| 159 | goto out_free_ccid; |
| 160 | } else { |
| 161 | memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size); |
| 162 | if (ccid->ccid_ops->ccid_hc_tx_init != NULL && |
| 163 | ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0) |
| 164 | goto out_free_ccid; |
| 165 | } |
| 166 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 167 | return ccid; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 168 | out_free_ccid: |
| 169 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : |
| 170 | ccid_ops->ccid_hc_tx_slab, ccid); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 171 | ccid = NULL; |
| 172 | goto out; |
| 173 | } |
| 174 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 175 | 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] | 176 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 177 | if (ccid != NULL) { |
| 178 | if (ccid->ccid_ops->ccid_hc_rx_exit != NULL) |
| 179 | ccid->ccid_ops->ccid_hc_rx_exit(sk); |
| 180 | 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] | 181 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 184 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) |
| 185 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame] | 186 | if (ccid != NULL) { |
| 187 | if (ccid->ccid_ops->ccid_hc_tx_exit != NULL) |
| 188 | ccid->ccid_ops->ccid_hc_tx_exit(sk); |
| 189 | kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid); |
| 190 | } |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 193 | int __init ccid_initialize_builtins(void) |
| 194 | { |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 195 | int i, err = tfrc_lib_init(); |
| 196 | |
| 197 | if (err) |
| 198 | return err; |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 199 | |
| 200 | for (i = 0; i < ARRAY_SIZE(ccids); i++) { |
| 201 | err = ccid_activate(ccids[i]); |
| 202 | if (err) |
| 203 | goto unwind_registrations; |
| 204 | } |
| 205 | return 0; |
| 206 | |
| 207 | unwind_registrations: |
| 208 | while(--i >= 0) |
| 209 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 210 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 211 | return err; |
| 212 | } |
| 213 | |
| 214 | void ccid_cleanup_builtins(void) |
| 215 | { |
| 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
| 219 | ccid_deactivate(ccids[i]); |
Gerrit Renker | 129fa44 | 2009-01-04 21:45:33 -0800 | [diff] [blame] | 220 | tfrc_lib_exit(); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 221 | } |