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; << 204 } 215 } 205 216 206 static void * __percpu *ipcomp_alloc_scratches 217 static void * __percpu *ipcomp_alloc_scratches(void) 207 { 218 { 208 void * __percpu *scratches; 219 void * __percpu *scratches; 209 int i; 220 int i; 210 221 211 if (ipcomp_scratch_users++) 222 if (ipcomp_scratch_users++) 212 return ipcomp_scratches; 223 return ipcomp_scratches; 213 224 214 scratches = alloc_percpu(void *); 225 scratches = alloc_percpu(void *); 215 if (!scratches) 226 if (!scratches) 216 return NULL; 227 return NULL; 217 228 218 ipcomp_scratches = scratches; 229 ipcomp_scratches = scratches; 219 230 220 for_each_possible_cpu(i) { 231 for_each_possible_cpu(i) { 221 void *scratch; 232 void *scratch; 222 233 223 scratch = vmalloc_node(IPCOMP_ 234 scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i)); 224 if (!scratch) 235 if (!scratch) 225 return NULL; 236 return NULL; 226 *per_cpu_ptr(scratches, i) = s 237 *per_cpu_ptr(scratches, i) = scratch; 227 } 238 } 228 239 229 return scratches; 240 return scratches; 230 } 241 } 231 242 232 static void ipcomp_free_tfms(struct crypto_com 243 static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms) 233 { 244 { 234 struct ipcomp_tfms *pos; 245 struct ipcomp_tfms *pos; 235 int cpu; 246 int cpu; 236 247 237 list_for_each_entry(pos, &ipcomp_tfms_ 248 list_for_each_entry(pos, &ipcomp_tfms_list, list) { 238 if (pos->tfms == tfms) 249 if (pos->tfms == tfms) 239 break; 250 break; 240 } 251 } 241 252 242 WARN_ON(list_entry_is_head(pos, &ipcom !! 253 WARN_ON(!pos); 243 254 244 if (--pos->users) 255 if (--pos->users) 245 return; 256 return; 246 257 247 list_del(&pos->list); 258 list_del(&pos->list); 248 kfree(pos); 259 kfree(pos); 249 260 250 if (!tfms) 261 if (!tfms) 251 return; 262 return; 252 263 253 for_each_possible_cpu(cpu) { 264 for_each_possible_cpu(cpu) { 254 struct crypto_comp *tfm = *per 265 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu); 255 crypto_free_comp(tfm); 266 crypto_free_comp(tfm); 256 } 267 } 257 free_percpu(tfms); 268 free_percpu(tfms); 258 } 269 } 259 270 260 static struct crypto_comp * __percpu *ipcomp_a 271 static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name) 261 { 272 { 262 struct ipcomp_tfms *pos; 273 struct ipcomp_tfms *pos; 263 struct crypto_comp * __percpu *tfms; 274 struct crypto_comp * __percpu *tfms; 264 int cpu; 275 int cpu; 265 276 266 277 267 list_for_each_entry(pos, &ipcomp_tfms_ 278 list_for_each_entry(pos, &ipcomp_tfms_list, list) { 268 struct crypto_comp *tfm; 279 struct crypto_comp *tfm; 269 280 270 /* This can be any valid CPU I 281 /* This can be any valid CPU ID so we don't need locking. */ 271 tfm = this_cpu_read(*pos->tfms 282 tfm = this_cpu_read(*pos->tfms); 272 283 273 if (!strcmp(crypto_comp_name(t 284 if (!strcmp(crypto_comp_name(tfm), alg_name)) { 274 pos->users++; 285 pos->users++; 275 return pos->tfms; 286 return pos->tfms; 276 } 287 } 277 } 288 } 278 289 279 pos = kmalloc(sizeof(*pos), GFP_KERNEL 290 pos = kmalloc(sizeof(*pos), GFP_KERNEL); 280 if (!pos) 291 if (!pos) 281 return NULL; 292 return NULL; 282 293 283 pos->users = 1; 294 pos->users = 1; 284 INIT_LIST_HEAD(&pos->list); 295 INIT_LIST_HEAD(&pos->list); 285 list_add(&pos->list, &ipcomp_tfms_list 296 list_add(&pos->list, &ipcomp_tfms_list); 286 297 287 pos->tfms = tfms = alloc_percpu(struct 298 pos->tfms = tfms = alloc_percpu(struct crypto_comp *); 288 if (!tfms) 299 if (!tfms) 289 goto error; 300 goto error; 290 301 291 for_each_possible_cpu(cpu) { 302 for_each_possible_cpu(cpu) { 292 struct crypto_comp *tfm = cryp 303 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0, 293 304 CRYPTO_ALG_ASYNC); 294 if (IS_ERR(tfm)) 305 if (IS_ERR(tfm)) 295 goto error; 306 goto error; 296 *per_cpu_ptr(tfms, cpu) = tfm; 307 *per_cpu_ptr(tfms, cpu) = tfm; 297 } 308 } 298 309 299 return tfms; 310 return tfms; 300 311 301 error: 312 error: 302 ipcomp_free_tfms(tfms); 313 ipcomp_free_tfms(tfms); 303 return NULL; 314 return NULL; 304 } 315 } 305 316 306 static void ipcomp_free_data(struct ipcomp_dat 317 static void ipcomp_free_data(struct ipcomp_data *ipcd) 307 { 318 { 308 if (ipcd->tfms) 319 if (ipcd->tfms) 309 ipcomp_free_tfms(ipcd->tfms); 320 ipcomp_free_tfms(ipcd->tfms); 310 ipcomp_free_scratches(); 321 ipcomp_free_scratches(); 311 } 322 } 312 323 313 void ipcomp_destroy(struct xfrm_state *x) 324 void ipcomp_destroy(struct xfrm_state *x) 314 { 325 { 315 struct ipcomp_data *ipcd = x->data; 326 struct ipcomp_data *ipcd = x->data; 316 if (!ipcd) 327 if (!ipcd) 317 return; 328 return; 318 xfrm_state_delete_tunnel(x); 329 xfrm_state_delete_tunnel(x); 319 mutex_lock(&ipcomp_resource_mutex); 330 mutex_lock(&ipcomp_resource_mutex); 320 ipcomp_free_data(ipcd); 331 ipcomp_free_data(ipcd); 321 mutex_unlock(&ipcomp_resource_mutex); 332 mutex_unlock(&ipcomp_resource_mutex); 322 kfree(ipcd); 333 kfree(ipcd); 323 } 334 } 324 EXPORT_SYMBOL_GPL(ipcomp_destroy); 335 EXPORT_SYMBOL_GPL(ipcomp_destroy); 325 336 326 int ipcomp_init_state(struct xfrm_state *x, st !! 337 int ipcomp_init_state(struct xfrm_state *x) 327 { 338 { 328 int err; 339 int err; 329 struct ipcomp_data *ipcd; 340 struct ipcomp_data *ipcd; 330 struct xfrm_algo_desc *calg_desc; 341 struct xfrm_algo_desc *calg_desc; 331 342 332 err = -EINVAL; 343 err = -EINVAL; 333 if (!x->calg) { !! 344 if (!x->calg) 334 NL_SET_ERR_MSG(extack, "Missin << 335 goto out; 345 goto out; 336 } << 337 346 338 if (x->encap) { !! 347 if (x->encap) 339 NL_SET_ERR_MSG(extack, "IPComp << 340 goto out; 348 goto out; 341 } << 342 349 343 err = -ENOMEM; 350 err = -ENOMEM; 344 ipcd = kzalloc(sizeof(*ipcd), GFP_KERN 351 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL); 345 if (!ipcd) 352 if (!ipcd) 346 goto out; 353 goto out; 347 354 348 mutex_lock(&ipcomp_resource_mutex); 355 mutex_lock(&ipcomp_resource_mutex); 349 if (!ipcomp_alloc_scratches()) 356 if (!ipcomp_alloc_scratches()) 350 goto error; 357 goto error; 351 358 352 ipcd->tfms = ipcomp_alloc_tfms(x->calg 359 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name); 353 if (!ipcd->tfms) 360 if (!ipcd->tfms) 354 goto error; 361 goto error; 355 mutex_unlock(&ipcomp_resource_mutex); 362 mutex_unlock(&ipcomp_resource_mutex); 356 363 357 calg_desc = xfrm_calg_get_byname(x->ca 364 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0); 358 BUG_ON(!calg_desc); 365 BUG_ON(!calg_desc); 359 ipcd->threshold = calg_desc->uinfo.com 366 ipcd->threshold = calg_desc->uinfo.comp.threshold; 360 x->data = ipcd; 367 x->data = ipcd; 361 err = 0; 368 err = 0; 362 out: 369 out: 363 return err; 370 return err; 364 371 365 error: 372 error: 366 ipcomp_free_data(ipcd); 373 ipcomp_free_data(ipcd); 367 mutex_unlock(&ipcomp_resource_mutex); 374 mutex_unlock(&ipcomp_resource_mutex); 368 kfree(ipcd); 375 kfree(ipcd); 369 goto out; 376 goto out; 370 } 377 } 371 EXPORT_SYMBOL_GPL(ipcomp_init_state); 378 EXPORT_SYMBOL_GPL(ipcomp_init_state); 372 379 373 MODULE_LICENSE("GPL"); 380 MODULE_LICENSE("GPL"); 374 MODULE_DESCRIPTION("IP Payload Compression Pro 381 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173"); 375 MODULE_AUTHOR("James Morris <jmorris@intercode 382 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); 376 383
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.