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