Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 2 | /* |
| 3 | * IP Payload Compression Protocol (IPComp) - RFC3173. |
| 4 | * |
| 5 | * Copyright (c) 2003 James Morris <jmorris@intercode.com.au> |
| 6 | * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au> |
| 7 | * |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 8 | * Todo: |
| 9 | * - Tunable compression parameters. |
| 10 | * - Compression stats. |
| 11 | * - Adaptive compression. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/crypto.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/percpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 21 | #include <linux/smp.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <net/ip.h> |
| 24 | #include <net/ipcomp.h> |
| 25 | #include <net/xfrm.h> |
| 26 | |
| 27 | struct ipcomp_tfms { |
| 28 | struct list_head list; |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 29 | struct crypto_comp * __percpu *tfms; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 30 | int users; |
| 31 | }; |
| 32 | |
| 33 | static DEFINE_MUTEX(ipcomp_resource_mutex); |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 34 | static void * __percpu *ipcomp_scratches; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 35 | static int ipcomp_scratch_users; |
| 36 | static LIST_HEAD(ipcomp_tfms_list); |
| 37 | |
| 38 | static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb) |
| 39 | { |
| 40 | struct ipcomp_data *ipcd = x->data; |
| 41 | const int plen = skb->len; |
| 42 | int dlen = IPCOMP_SCRATCH_SIZE; |
| 43 | const u8 *start = skb->data; |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 44 | u8 *scratch = *this_cpu_ptr(ipcomp_scratches); |
| 45 | struct crypto_comp *tfm = *this_cpu_ptr(ipcd->tfms); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 46 | int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen); |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 47 | int len; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 48 | |
| 49 | if (err) |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 50 | return err; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 51 | |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 52 | if (dlen < (plen + sizeof(struct ip_comp_hdr))) |
| 53 | return -EINVAL; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 54 | |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 55 | len = dlen - plen; |
| 56 | if (len > skb_tailroom(skb)) |
| 57 | len = skb_tailroom(skb); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 58 | |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 59 | __skb_put(skb, len); |
| 60 | |
| 61 | len += plen; |
| 62 | skb_copy_to_linear_data(skb, scratch, len); |
| 63 | |
| 64 | while ((scratch += len, dlen -= len) > 0) { |
| 65 | skb_frag_t *frag; |
Ian Campbell | 804cf14 | 2011-08-22 23:45:01 +0000 | [diff] [blame] | 66 | struct page *page; |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 67 | |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 68 | if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 69 | return -EMSGSIZE; |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 70 | |
| 71 | frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags; |
Ian Campbell | 804cf14 | 2011-08-22 23:45:01 +0000 | [diff] [blame] | 72 | page = alloc_page(GFP_ATOMIC); |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 73 | |
Ian Campbell | 804cf14 | 2011-08-22 23:45:01 +0000 | [diff] [blame] | 74 | if (!page) |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 75 | return -ENOMEM; |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 76 | |
Ian Campbell | 804cf14 | 2011-08-22 23:45:01 +0000 | [diff] [blame] | 77 | __skb_frag_set_page(frag, page); |
| 78 | |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 79 | len = PAGE_SIZE; |
| 80 | if (dlen < len) |
| 81 | len = dlen; |
| 82 | |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 83 | skb_frag_off_set(frag, 0); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 84 | skb_frag_size_set(frag, len); |
Ian Campbell | 804cf14 | 2011-08-22 23:45:01 +0000 | [diff] [blame] | 85 | memcpy(skb_frag_address(frag), scratch, len); |
| 86 | |
Herbert Xu | 7d7e5a6 | 2008-07-25 02:55:33 -0700 | [diff] [blame] | 87 | skb->truesize += len; |
| 88 | skb->data_len += len; |
| 89 | skb->len += len; |
| 90 | |
| 91 | skb_shinfo(skb)->nr_frags++; |
| 92 | } |
| 93 | |
Sabrina Dubroca | 747b670 | 2021-04-16 17:11:46 +0200 | [diff] [blame] | 94 | return 0; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb) |
| 98 | { |
| 99 | int nexthdr; |
| 100 | int err = -ENOMEM; |
| 101 | struct ip_comp_hdr *ipch; |
| 102 | |
| 103 | if (skb_linearize_cow(skb)) |
| 104 | goto out; |
| 105 | |
| 106 | skb->ip_summed = CHECKSUM_NONE; |
| 107 | |
| 108 | /* Remove ipcomp header and decompress original payload */ |
| 109 | ipch = (void *)skb->data; |
| 110 | nexthdr = ipch->nexthdr; |
| 111 | |
| 112 | skb->transport_header = skb->network_header + sizeof(*ipch); |
| 113 | __skb_pull(skb, sizeof(*ipch)); |
| 114 | err = ipcomp_decompress(x, skb); |
| 115 | if (err) |
| 116 | goto out; |
| 117 | |
| 118 | err = nexthdr; |
| 119 | |
| 120 | out: |
| 121 | return err; |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(ipcomp_input); |
| 124 | |
| 125 | static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb) |
| 126 | { |
| 127 | struct ipcomp_data *ipcd = x->data; |
| 128 | const int plen = skb->len; |
| 129 | int dlen = IPCOMP_SCRATCH_SIZE; |
| 130 | u8 *start = skb->data; |
Michal Kubecek | 12e3594 | 2013-10-17 15:07:40 +0200 | [diff] [blame] | 131 | struct crypto_comp *tfm; |
| 132 | u8 *scratch; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 133 | int err; |
| 134 | |
| 135 | local_bh_disable(); |
Michal Kubecek | 12e3594 | 2013-10-17 15:07:40 +0200 | [diff] [blame] | 136 | scratch = *this_cpu_ptr(ipcomp_scratches); |
| 137 | tfm = *this_cpu_ptr(ipcd->tfms); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 138 | err = crypto_comp_compress(tfm, start, plen, scratch, &dlen); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 139 | if (err) |
| 140 | goto out; |
| 141 | |
| 142 | if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) { |
| 143 | err = -EMSGSIZE; |
| 144 | goto out; |
| 145 | } |
| 146 | |
| 147 | memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen); |
Michal Kubecek | 12e3594 | 2013-10-17 15:07:40 +0200 | [diff] [blame] | 148 | local_bh_enable(); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 149 | |
| 150 | pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr)); |
| 151 | return 0; |
| 152 | |
| 153 | out: |
Michal Kubecek | 12e3594 | 2013-10-17 15:07:40 +0200 | [diff] [blame] | 154 | local_bh_enable(); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 155 | return err; |
| 156 | } |
| 157 | |
| 158 | int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb) |
| 159 | { |
| 160 | int err; |
| 161 | struct ip_comp_hdr *ipch; |
| 162 | struct ipcomp_data *ipcd = x->data; |
| 163 | |
| 164 | if (skb->len < ipcd->threshold) { |
| 165 | /* Don't bother compressing */ |
| 166 | goto out_ok; |
| 167 | } |
| 168 | |
| 169 | if (skb_linearize_cow(skb)) |
| 170 | goto out_ok; |
| 171 | |
| 172 | err = ipcomp_compress(x, skb); |
| 173 | |
| 174 | if (err) { |
| 175 | goto out_ok; |
| 176 | } |
| 177 | |
| 178 | /* Install ipcomp header, convert into ipcomp datagram. */ |
| 179 | ipch = ip_comp_hdr(skb); |
| 180 | ipch->nexthdr = *skb_mac_header(skb); |
| 181 | ipch->flags = 0; |
| 182 | ipch->cpi = htons((u16 )ntohl(x->id.spi)); |
| 183 | *skb_mac_header(skb) = IPPROTO_COMP; |
| 184 | out_ok: |
| 185 | skb_push(skb, -skb_network_offset(skb)); |
| 186 | return 0; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(ipcomp_output); |
| 189 | |
| 190 | static void ipcomp_free_scratches(void) |
| 191 | { |
| 192 | int i; |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 193 | void * __percpu *scratches; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 194 | |
| 195 | if (--ipcomp_scratch_users) |
| 196 | return; |
| 197 | |
| 198 | scratches = ipcomp_scratches; |
| 199 | if (!scratches) |
| 200 | return; |
| 201 | |
| 202 | for_each_possible_cpu(i) |
| 203 | vfree(*per_cpu_ptr(scratches, i)); |
| 204 | |
| 205 | free_percpu(scratches); |
| 206 | } |
| 207 | |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 208 | static void * __percpu *ipcomp_alloc_scratches(void) |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 209 | { |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 210 | void * __percpu *scratches; |
Eric Dumazet | 5cf4eb5 | 2013-10-18 03:54:16 -0700 | [diff] [blame] | 211 | int i; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 212 | |
| 213 | if (ipcomp_scratch_users++) |
| 214 | return ipcomp_scratches; |
| 215 | |
| 216 | scratches = alloc_percpu(void *); |
| 217 | if (!scratches) |
| 218 | return NULL; |
| 219 | |
| 220 | ipcomp_scratches = scratches; |
| 221 | |
| 222 | for_each_possible_cpu(i) { |
Eric Dumazet | 5cf4eb5 | 2013-10-18 03:54:16 -0700 | [diff] [blame] | 223 | void *scratch; |
| 224 | |
| 225 | scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i)); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 226 | if (!scratch) |
| 227 | return NULL; |
| 228 | *per_cpu_ptr(scratches, i) = scratch; |
| 229 | } |
| 230 | |
| 231 | return scratches; |
| 232 | } |
| 233 | |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 234 | static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms) |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 235 | { |
| 236 | struct ipcomp_tfms *pos; |
| 237 | int cpu; |
| 238 | |
| 239 | list_for_each_entry(pos, &ipcomp_tfms_list, list) { |
| 240 | if (pos->tfms == tfms) |
| 241 | break; |
| 242 | } |
| 243 | |
Harshvardhan Jha | 480e93e | 2021-07-25 23:23:55 +0530 | [diff] [blame] | 244 | WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list)); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 245 | |
| 246 | if (--pos->users) |
| 247 | return; |
| 248 | |
| 249 | list_del(&pos->list); |
| 250 | kfree(pos); |
| 251 | |
| 252 | if (!tfms) |
| 253 | return; |
| 254 | |
| 255 | for_each_possible_cpu(cpu) { |
| 256 | struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu); |
| 257 | crypto_free_comp(tfm); |
| 258 | } |
| 259 | free_percpu(tfms); |
| 260 | } |
| 261 | |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 262 | static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name) |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 263 | { |
| 264 | struct ipcomp_tfms *pos; |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 265 | struct crypto_comp * __percpu *tfms; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 266 | int cpu; |
| 267 | |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 268 | |
| 269 | list_for_each_entry(pos, &ipcomp_tfms_list, list) { |
| 270 | struct crypto_comp *tfm; |
| 271 | |
Shan Wei | f7c83bc | 2012-11-13 20:36:00 +0800 | [diff] [blame] | 272 | /* This can be any valid CPU ID so we don't need locking. */ |
Greg Hackmann | 0dcd787 | 2018-03-07 14:42:53 -0800 | [diff] [blame] | 273 | tfm = this_cpu_read(*pos->tfms); |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 274 | |
| 275 | if (!strcmp(crypto_comp_name(tfm), alg_name)) { |
| 276 | pos->users++; |
Shan Wei | f7c83bc | 2012-11-13 20:36:00 +0800 | [diff] [blame] | 277 | return pos->tfms; |
Herbert Xu | 6fccab6 | 2008-07-25 02:54:40 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | pos = kmalloc(sizeof(*pos), GFP_KERNEL); |
| 282 | if (!pos) |
| 283 | return NULL; |
| 284 | |
| 285 | pos->users = 1; |
| 286 | INIT_LIST_HEAD(&pos->list); |
| 287 | list_add(&pos->list, &ipcomp_tfms_list); |
| 288 | |
| 289 | pos->tfms = tfms = alloc_percpu(struct crypto_comp *); |
| 290 | if (!tfms) |
| 291 | goto error; |
| 292 | |
| 293 | for_each_possible_cpu(cpu) { |
| 294 | struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0, |
| 295 | CRYPTO_ALG_ASYNC); |
| 296 | if (IS_ERR(tfm)) |
| 297 | goto error; |
| 298 | *per_cpu_ptr(tfms, cpu) = tfm; |
| 299 | } |
| 300 | |
| 301 | return tfms; |
| 302 | |
| 303 | error: |
| 304 | ipcomp_free_tfms(tfms); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | static void ipcomp_free_data(struct ipcomp_data *ipcd) |
| 309 | { |
| 310 | if (ipcd->tfms) |
| 311 | ipcomp_free_tfms(ipcd->tfms); |
| 312 | ipcomp_free_scratches(); |
| 313 | } |
| 314 | |
| 315 | void ipcomp_destroy(struct xfrm_state *x) |
| 316 | { |
| 317 | struct ipcomp_data *ipcd = x->data; |
| 318 | if (!ipcd) |
| 319 | return; |
| 320 | xfrm_state_delete_tunnel(x); |
| 321 | mutex_lock(&ipcomp_resource_mutex); |
| 322 | ipcomp_free_data(ipcd); |
| 323 | mutex_unlock(&ipcomp_resource_mutex); |
| 324 | kfree(ipcd); |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(ipcomp_destroy); |
| 327 | |
| 328 | int ipcomp_init_state(struct xfrm_state *x) |
| 329 | { |
| 330 | int err; |
| 331 | struct ipcomp_data *ipcd; |
| 332 | struct xfrm_algo_desc *calg_desc; |
| 333 | |
| 334 | err = -EINVAL; |
| 335 | if (!x->calg) |
| 336 | goto out; |
| 337 | |
| 338 | if (x->encap) |
| 339 | goto out; |
| 340 | |
| 341 | err = -ENOMEM; |
| 342 | ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL); |
| 343 | if (!ipcd) |
| 344 | goto out; |
| 345 | |
| 346 | mutex_lock(&ipcomp_resource_mutex); |
| 347 | if (!ipcomp_alloc_scratches()) |
| 348 | goto error; |
| 349 | |
| 350 | ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name); |
| 351 | if (!ipcd->tfms) |
| 352 | goto error; |
| 353 | mutex_unlock(&ipcomp_resource_mutex); |
| 354 | |
| 355 | calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0); |
| 356 | BUG_ON(!calg_desc); |
| 357 | ipcd->threshold = calg_desc->uinfo.comp.threshold; |
| 358 | x->data = ipcd; |
| 359 | err = 0; |
| 360 | out: |
| 361 | return err; |
| 362 | |
| 363 | error: |
| 364 | ipcomp_free_data(ipcd); |
| 365 | mutex_unlock(&ipcomp_resource_mutex); |
| 366 | kfree(ipcd); |
| 367 | goto out; |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(ipcomp_init_state); |
| 370 | |
| 371 | MODULE_LICENSE("GPL"); |
| 372 | MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173"); |
| 373 | MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); |