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" |
| 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 |
| 46 | * This function allocates memory - caller must see that it is freed after use. |
| 47 | */ |
| 48 | int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len) |
| 49 | { |
| 50 | *ccid_array = kmalloc(ARRAY_SIZE(ccids), gfp_any()); |
| 51 | if (*ccid_array == NULL) |
| 52 | return -ENOBUFS; |
| 53 | |
| 54 | for (*array_len = 0; *array_len < ARRAY_SIZE(ccids); *array_len += 1) |
| 55 | (*ccid_array)[*array_len] = ccids[*array_len]->ccid_id; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int ccid_getsockopt_builtin_ccids(struct sock *sk, int len, |
| 60 | char __user *optval, int __user *optlen) |
| 61 | { |
| 62 | u8 *ccid_array, array_len; |
| 63 | int err = 0; |
| 64 | |
| 65 | if (len < ARRAY_SIZE(ccids)) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | if (ccid_get_builtin_ccids(&ccid_array, &array_len)) |
| 69 | return -ENOBUFS; |
| 70 | |
| 71 | if (put_user(array_len, optlen) || |
| 72 | copy_to_user(optval, ccid_array, array_len)) |
| 73 | err = -EFAULT; |
| 74 | |
| 75 | kfree(ccid_array); |
| 76 | return err; |
| 77 | } |
| 78 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 79 | static struct kmem_cache *ccid_kmem_cache_create(int obj_size, 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 | char slab_name_fmt[32], *slab_name; |
| 83 | va_list args; |
| 84 | |
| 85 | va_start(args, fmt); |
| 86 | vsnprintf(slab_name_fmt, sizeof(slab_name_fmt), fmt, args); |
| 87 | va_end(args); |
| 88 | |
| 89 | slab_name = kstrdup(slab_name_fmt, GFP_KERNEL); |
| 90 | if (slab_name == NULL) |
| 91 | return NULL; |
| 92 | slab = kmem_cache_create(slab_name, sizeof(struct ccid) + obj_size, 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 93 | SLAB_HWCACHE_ALIGN, NULL); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 94 | if (slab == NULL) |
| 95 | kfree(slab_name); |
| 96 | return slab; |
| 97 | } |
| 98 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 99 | static void ccid_kmem_cache_destroy(struct kmem_cache *slab) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 100 | { |
| 101 | if (slab != NULL) { |
| 102 | const char *name = kmem_cache_name(slab); |
| 103 | |
| 104 | kmem_cache_destroy(slab); |
| 105 | kfree(name); |
| 106 | } |
| 107 | } |
| 108 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 109 | static int ccid_activate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 110 | { |
| 111 | int err = -ENOBUFS; |
| 112 | |
| 113 | ccid_ops->ccid_hc_rx_slab = |
| 114 | ccid_kmem_cache_create(ccid_ops->ccid_hc_rx_obj_size, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 115 | "ccid%u_hc_rx_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_rx_slab == NULL) |
| 118 | goto out; |
| 119 | |
| 120 | ccid_ops->ccid_hc_tx_slab = |
| 121 | ccid_kmem_cache_create(ccid_ops->ccid_hc_tx_obj_size, |
Gerrit Renker | 84a97b0 | 2007-12-13 23:33:25 -0200 | [diff] [blame] | 122 | "ccid%u_hc_tx_sock", |
| 123 | ccid_ops->ccid_id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 124 | if (ccid_ops->ccid_hc_tx_slab == NULL) |
| 125 | goto out_free_rx_slab; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 126 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 127 | pr_info("CCID: Activated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 128 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 129 | err = 0; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 130 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 131 | return err; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 132 | out_free_rx_slab: |
| 133 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
| 134 | ccid_ops->ccid_hc_rx_slab = NULL; |
| 135 | goto out; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 138 | static void ccid_deactivate(struct ccid_operations *ccid_ops) |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 139 | { |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 140 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab); |
| 141 | ccid_ops->ccid_hc_tx_slab = NULL; |
| 142 | ccid_kmem_cache_destroy(ccid_ops->ccid_hc_rx_slab); |
| 143 | ccid_ops->ccid_hc_rx_slab = NULL; |
| 144 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 145 | pr_info("CCID: Deactivated CCID %d (%s)\n", |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 146 | ccid_ops->ccid_id, ccid_ops->ccid_name); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame^] | 149 | 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] | 150 | { |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 151 | struct ccid_operations *ccid_ops = ccid_by_number(id); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 152 | struct ccid *ccid = NULL; |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 153 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 154 | if (ccid_ops == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 155 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 156 | |
| 157 | ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab : |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame^] | 158 | ccid_ops->ccid_hc_tx_slab, gfp_any()); |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 159 | if (ccid == NULL) |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 160 | goto out; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 161 | ccid->ccid_ops = ccid_ops; |
| 162 | if (rx) { |
| 163 | memset(ccid + 1, 0, ccid_ops->ccid_hc_rx_obj_size); |
| 164 | if (ccid->ccid_ops->ccid_hc_rx_init != NULL && |
| 165 | ccid->ccid_ops->ccid_hc_rx_init(ccid, sk) != 0) |
| 166 | goto out_free_ccid; |
| 167 | } else { |
| 168 | memset(ccid + 1, 0, ccid_ops->ccid_hc_tx_obj_size); |
| 169 | if (ccid->ccid_ops->ccid_hc_tx_init != NULL && |
| 170 | ccid->ccid_ops->ccid_hc_tx_init(ccid, sk) != 0) |
| 171 | goto out_free_ccid; |
| 172 | } |
| 173 | out: |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 174 | return ccid; |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 175 | out_free_ccid: |
| 176 | kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab : |
| 177 | ccid_ops->ccid_hc_tx_slab, ccid); |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 178 | ccid = NULL; |
| 179 | goto out; |
| 180 | } |
| 181 | |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame^] | 182 | 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] | 183 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame^] | 184 | if (ccid != NULL) { |
| 185 | if (ccid->ccid_ops->ccid_hc_rx_exit != NULL) |
| 186 | ccid->ccid_ops->ccid_hc_rx_exit(sk); |
| 187 | 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] | 188 | } |
Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 191 | void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk) |
| 192 | { |
Gerrit Renker | e5fd56c | 2009-01-04 21:43:23 -0800 | [diff] [blame^] | 193 | if (ccid != NULL) { |
| 194 | if (ccid->ccid_ops->ccid_hc_tx_exit != NULL) |
| 195 | ccid->ccid_ops->ccid_hc_tx_exit(sk); |
| 196 | kmem_cache_free(ccid->ccid_ops->ccid_hc_tx_slab, ccid); |
| 197 | } |
Arnaldo Carvalho de Melo | 91f0ebf | 2006-03-20 19:21:44 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Gerrit Renker | ddebc97 | 2009-01-04 21:42:53 -0800 | [diff] [blame] | 200 | int __init ccid_initialize_builtins(void) |
| 201 | { |
| 202 | int i, err; |
| 203 | |
| 204 | for (i = 0; i < ARRAY_SIZE(ccids); i++) { |
| 205 | err = ccid_activate(ccids[i]); |
| 206 | if (err) |
| 207 | goto unwind_registrations; |
| 208 | } |
| 209 | return 0; |
| 210 | |
| 211 | unwind_registrations: |
| 212 | while(--i >= 0) |
| 213 | ccid_deactivate(ccids[i]); |
| 214 | return err; |
| 215 | } |
| 216 | |
| 217 | void ccid_cleanup_builtins(void) |
| 218 | { |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < ARRAY_SIZE(ccids); i++) |
| 222 | ccid_deactivate(ccids[i]); |
| 223 | } |