2 * IP Payload Compression Protocol (IPComp) - RFC3173.
4 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
13 * - Tunable compression parameters.
14 * - Compression stats.
15 * - Adaptive compression.
18 #include <linux/crypto.h>
19 #include <linux/err.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/percpu.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/smp.h>
26 #include <linux/vmalloc.h>
28 #include <net/ipcomp.h>
32 struct list_head list
;
33 struct crypto_comp
**tfms
;
37 static DEFINE_MUTEX(ipcomp_resource_mutex
);
38 static void **ipcomp_scratches
;
39 static int ipcomp_scratch_users
;
40 static LIST_HEAD(ipcomp_tfms_list
);
42 static int ipcomp_decompress(struct xfrm_state
*x
, struct sk_buff
*skb
)
44 struct ipcomp_data
*ipcd
= x
->data
;
45 const int plen
= skb
->len
;
46 int dlen
= IPCOMP_SCRATCH_SIZE
;
47 const u8
*start
= skb
->data
;
48 const int cpu
= get_cpu();
49 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
50 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
51 int err
= crypto_comp_decompress(tfm
, start
, plen
, scratch
, &dlen
);
56 if (dlen
< (plen
+ sizeof(struct ip_comp_hdr
))) {
61 err
= pskb_expand_head(skb
, 0, dlen
- plen
, GFP_ATOMIC
);
65 skb
->truesize
+= dlen
- plen
;
66 __skb_put(skb
, dlen
- plen
);
67 skb_copy_to_linear_data(skb
, scratch
, dlen
);
73 int ipcomp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
77 struct ip_comp_hdr
*ipch
;
79 if (skb_linearize_cow(skb
))
82 skb
->ip_summed
= CHECKSUM_NONE
;
84 /* Remove ipcomp header and decompress original payload */
85 ipch
= (void *)skb
->data
;
86 nexthdr
= ipch
->nexthdr
;
88 skb
->transport_header
= skb
->network_header
+ sizeof(*ipch
);
89 __skb_pull(skb
, sizeof(*ipch
));
90 err
= ipcomp_decompress(x
, skb
);
99 EXPORT_SYMBOL_GPL(ipcomp_input
);
101 static int ipcomp_compress(struct xfrm_state
*x
, struct sk_buff
*skb
)
103 struct ipcomp_data
*ipcd
= x
->data
;
104 const int plen
= skb
->len
;
105 int dlen
= IPCOMP_SCRATCH_SIZE
;
106 u8
*start
= skb
->data
;
107 const int cpu
= get_cpu();
108 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
109 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
113 err
= crypto_comp_compress(tfm
, start
, plen
, scratch
, &dlen
);
118 if ((dlen
+ sizeof(struct ip_comp_hdr
)) >= plen
) {
123 memcpy(start
+ sizeof(struct ip_comp_hdr
), scratch
, dlen
);
126 pskb_trim(skb
, dlen
+ sizeof(struct ip_comp_hdr
));
134 int ipcomp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
137 struct ip_comp_hdr
*ipch
;
138 struct ipcomp_data
*ipcd
= x
->data
;
140 if (skb
->len
< ipcd
->threshold
) {
141 /* Don't bother compressing */
145 if (skb_linearize_cow(skb
))
148 err
= ipcomp_compress(x
, skb
);
154 /* Install ipcomp header, convert into ipcomp datagram. */
155 ipch
= ip_comp_hdr(skb
);
156 ipch
->nexthdr
= *skb_mac_header(skb
);
158 ipch
->cpi
= htons((u16
)ntohl(x
->id
.spi
));
159 *skb_mac_header(skb
) = IPPROTO_COMP
;
161 skb_push(skb
, -skb_network_offset(skb
));
164 EXPORT_SYMBOL_GPL(ipcomp_output
);
166 static void ipcomp_free_scratches(void)
171 if (--ipcomp_scratch_users
)
174 scratches
= ipcomp_scratches
;
178 for_each_possible_cpu(i
)
179 vfree(*per_cpu_ptr(scratches
, i
));
181 free_percpu(scratches
);
184 static void **ipcomp_alloc_scratches(void)
189 if (ipcomp_scratch_users
++)
190 return ipcomp_scratches
;
192 scratches
= alloc_percpu(void *);
196 ipcomp_scratches
= scratches
;
198 for_each_possible_cpu(i
) {
199 void *scratch
= vmalloc(IPCOMP_SCRATCH_SIZE
);
202 *per_cpu_ptr(scratches
, i
) = scratch
;
208 static void ipcomp_free_tfms(struct crypto_comp
**tfms
)
210 struct ipcomp_tfms
*pos
;
213 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
214 if (pos
->tfms
== tfms
)
223 list_del(&pos
->list
);
229 for_each_possible_cpu(cpu
) {
230 struct crypto_comp
*tfm
= *per_cpu_ptr(tfms
, cpu
);
231 crypto_free_comp(tfm
);
236 static struct crypto_comp
**ipcomp_alloc_tfms(const char *alg_name
)
238 struct ipcomp_tfms
*pos
;
239 struct crypto_comp
**tfms
;
242 /* This can be any valid CPU ID so we don't need locking. */
243 cpu
= raw_smp_processor_id();
245 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
246 struct crypto_comp
*tfm
;
249 tfm
= *per_cpu_ptr(tfms
, cpu
);
251 if (!strcmp(crypto_comp_name(tfm
), alg_name
)) {
257 pos
= kmalloc(sizeof(*pos
), GFP_KERNEL
);
262 INIT_LIST_HEAD(&pos
->list
);
263 list_add(&pos
->list
, &ipcomp_tfms_list
);
265 pos
->tfms
= tfms
= alloc_percpu(struct crypto_comp
*);
269 for_each_possible_cpu(cpu
) {
270 struct crypto_comp
*tfm
= crypto_alloc_comp(alg_name
, 0,
274 *per_cpu_ptr(tfms
, cpu
) = tfm
;
280 ipcomp_free_tfms(tfms
);
284 static void ipcomp_free_data(struct ipcomp_data
*ipcd
)
287 ipcomp_free_tfms(ipcd
->tfms
);
288 ipcomp_free_scratches();
291 void ipcomp_destroy(struct xfrm_state
*x
)
293 struct ipcomp_data
*ipcd
= x
->data
;
296 xfrm_state_delete_tunnel(x
);
297 mutex_lock(&ipcomp_resource_mutex
);
298 ipcomp_free_data(ipcd
);
299 mutex_unlock(&ipcomp_resource_mutex
);
302 EXPORT_SYMBOL_GPL(ipcomp_destroy
);
304 int ipcomp_init_state(struct xfrm_state
*x
)
307 struct ipcomp_data
*ipcd
;
308 struct xfrm_algo_desc
*calg_desc
;
318 ipcd
= kzalloc(sizeof(*ipcd
), GFP_KERNEL
);
322 mutex_lock(&ipcomp_resource_mutex
);
323 if (!ipcomp_alloc_scratches())
326 ipcd
->tfms
= ipcomp_alloc_tfms(x
->calg
->alg_name
);
329 mutex_unlock(&ipcomp_resource_mutex
);
331 calg_desc
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
333 ipcd
->threshold
= calg_desc
->uinfo
.comp
.threshold
;
340 ipcomp_free_data(ipcd
);
341 mutex_unlock(&ipcomp_resource_mutex
);
345 EXPORT_SYMBOL_GPL(ipcomp_init_state
);
347 MODULE_LICENSE("GPL");
348 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
349 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
This page took 0.038645 seconds and 6 git commands to generate.