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