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/gfp.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/percpu.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/smp.h>
27 #include <linux/vmalloc.h>
29 #include <net/ipcomp.h>
33 struct list_head list
;
34 struct crypto_comp
**tfms
;
38 static DEFINE_MUTEX(ipcomp_resource_mutex
);
39 static void **ipcomp_scratches
;
40 static int ipcomp_scratch_users
;
41 static LIST_HEAD(ipcomp_tfms_list
);
43 static int ipcomp_decompress(struct xfrm_state
*x
, struct sk_buff
*skb
)
45 struct ipcomp_data
*ipcd
= x
->data
;
46 const int plen
= skb
->len
;
47 int dlen
= IPCOMP_SCRATCH_SIZE
;
48 const u8
*start
= skb
->data
;
49 const int cpu
= get_cpu();
50 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
51 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
52 int err
= crypto_comp_decompress(tfm
, start
, plen
, scratch
, &dlen
);
58 if (dlen
< (plen
+ sizeof(struct ip_comp_hdr
))) {
64 if (len
> skb_tailroom(skb
))
65 len
= skb_tailroom(skb
);
71 skb_copy_to_linear_data(skb
, scratch
, len
);
73 while ((scratch
+= len
, dlen
-= len
) > 0) {
77 if (WARN_ON(skb_shinfo(skb
)->nr_frags
>= MAX_SKB_FRAGS
))
80 frag
= skb_shinfo(skb
)->frags
+ skb_shinfo(skb
)->nr_frags
;
81 frag
->page
= alloc_page(GFP_ATOMIC
);
91 memcpy(page_address(frag
->page
), scratch
, len
);
93 frag
->page_offset
= 0;
99 skb_shinfo(skb
)->nr_frags
++;
109 int ipcomp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
113 struct ip_comp_hdr
*ipch
;
115 if (skb_linearize_cow(skb
))
118 skb
->ip_summed
= CHECKSUM_NONE
;
120 /* Remove ipcomp header and decompress original payload */
121 ipch
= (void *)skb
->data
;
122 nexthdr
= ipch
->nexthdr
;
124 skb
->transport_header
= skb
->network_header
+ sizeof(*ipch
);
125 __skb_pull(skb
, sizeof(*ipch
));
126 err
= ipcomp_decompress(x
, skb
);
135 EXPORT_SYMBOL_GPL(ipcomp_input
);
137 static int ipcomp_compress(struct xfrm_state
*x
, struct sk_buff
*skb
)
139 struct ipcomp_data
*ipcd
= x
->data
;
140 const int plen
= skb
->len
;
141 int dlen
= IPCOMP_SCRATCH_SIZE
;
142 u8
*start
= skb
->data
;
143 const int cpu
= get_cpu();
144 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
145 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
149 err
= crypto_comp_compress(tfm
, start
, plen
, scratch
, &dlen
);
154 if ((dlen
+ sizeof(struct ip_comp_hdr
)) >= plen
) {
159 memcpy(start
+ sizeof(struct ip_comp_hdr
), scratch
, dlen
);
162 pskb_trim(skb
, dlen
+ sizeof(struct ip_comp_hdr
));
170 int ipcomp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
173 struct ip_comp_hdr
*ipch
;
174 struct ipcomp_data
*ipcd
= x
->data
;
176 if (skb
->len
< ipcd
->threshold
) {
177 /* Don't bother compressing */
181 if (skb_linearize_cow(skb
))
184 err
= ipcomp_compress(x
, skb
);
190 /* Install ipcomp header, convert into ipcomp datagram. */
191 ipch
= ip_comp_hdr(skb
);
192 ipch
->nexthdr
= *skb_mac_header(skb
);
194 ipch
->cpi
= htons((u16
)ntohl(x
->id
.spi
));
195 *skb_mac_header(skb
) = IPPROTO_COMP
;
197 skb_push(skb
, -skb_network_offset(skb
));
200 EXPORT_SYMBOL_GPL(ipcomp_output
);
202 static void ipcomp_free_scratches(void)
207 if (--ipcomp_scratch_users
)
210 scratches
= ipcomp_scratches
;
214 for_each_possible_cpu(i
)
215 vfree(*per_cpu_ptr(scratches
, i
));
217 free_percpu(scratches
);
220 static void **ipcomp_alloc_scratches(void)
225 if (ipcomp_scratch_users
++)
226 return ipcomp_scratches
;
228 scratches
= alloc_percpu(void *);
232 ipcomp_scratches
= scratches
;
234 for_each_possible_cpu(i
) {
235 void *scratch
= vmalloc(IPCOMP_SCRATCH_SIZE
);
238 *per_cpu_ptr(scratches
, i
) = scratch
;
244 static void ipcomp_free_tfms(struct crypto_comp
**tfms
)
246 struct ipcomp_tfms
*pos
;
249 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
250 if (pos
->tfms
== tfms
)
259 list_del(&pos
->list
);
265 for_each_possible_cpu(cpu
) {
266 struct crypto_comp
*tfm
= *per_cpu_ptr(tfms
, cpu
);
267 crypto_free_comp(tfm
);
272 static struct crypto_comp
**ipcomp_alloc_tfms(const char *alg_name
)
274 struct ipcomp_tfms
*pos
;
275 struct crypto_comp
**tfms
;
278 /* This can be any valid CPU ID so we don't need locking. */
279 cpu
= raw_smp_processor_id();
281 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
282 struct crypto_comp
*tfm
;
285 tfm
= *per_cpu_ptr(tfms
, cpu
);
287 if (!strcmp(crypto_comp_name(tfm
), alg_name
)) {
293 pos
= kmalloc(sizeof(*pos
), GFP_KERNEL
);
298 INIT_LIST_HEAD(&pos
->list
);
299 list_add(&pos
->list
, &ipcomp_tfms_list
);
301 pos
->tfms
= tfms
= alloc_percpu(struct crypto_comp
*);
305 for_each_possible_cpu(cpu
) {
306 struct crypto_comp
*tfm
= crypto_alloc_comp(alg_name
, 0,
310 *per_cpu_ptr(tfms
, cpu
) = tfm
;
316 ipcomp_free_tfms(tfms
);
320 static void ipcomp_free_data(struct ipcomp_data
*ipcd
)
323 ipcomp_free_tfms(ipcd
->tfms
);
324 ipcomp_free_scratches();
327 void ipcomp_destroy(struct xfrm_state
*x
)
329 struct ipcomp_data
*ipcd
= x
->data
;
332 xfrm_state_delete_tunnel(x
);
333 mutex_lock(&ipcomp_resource_mutex
);
334 ipcomp_free_data(ipcd
);
335 mutex_unlock(&ipcomp_resource_mutex
);
338 EXPORT_SYMBOL_GPL(ipcomp_destroy
);
340 int ipcomp_init_state(struct xfrm_state
*x
)
343 struct ipcomp_data
*ipcd
;
344 struct xfrm_algo_desc
*calg_desc
;
354 ipcd
= kzalloc(sizeof(*ipcd
), GFP_KERNEL
);
358 mutex_lock(&ipcomp_resource_mutex
);
359 if (!ipcomp_alloc_scratches())
362 ipcd
->tfms
= ipcomp_alloc_tfms(x
->calg
->alg_name
);
365 mutex_unlock(&ipcomp_resource_mutex
);
367 calg_desc
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
369 ipcd
->threshold
= calg_desc
->uinfo
.comp
.threshold
;
376 ipcomp_free_data(ipcd
);
377 mutex_unlock(&ipcomp_resource_mutex
);
381 EXPORT_SYMBOL_GPL(ipcomp_init_state
);
383 MODULE_LICENSE("GPL");
384 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
385 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");