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/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);
57 if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
63 if (len > skb_tailroom(skb))
64 len = skb_tailroom(skb);
70 skb_copy_to_linear_data(skb, scratch, len);
72 while ((scratch += len, dlen -= len) > 0) {
76 if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
79 frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
80 frag->page = alloc_page(GFP_ATOMIC);
90 memcpy(page_address(frag->page), scratch, len);
92 frag->page_offset = 0;
98 skb_shinfo(skb)->nr_frags++;
108 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
112 struct ip_comp_hdr *ipch;
114 if (skb_linearize_cow(skb))
117 skb->ip_summed = CHECKSUM_NONE;
119 /* Remove ipcomp header and decompress original payload */
120 ipch = (void *)skb->data;
121 nexthdr = ipch->nexthdr;
123 skb->transport_header = skb->network_header + sizeof(*ipch);
124 __skb_pull(skb, sizeof(*ipch));
125 err = ipcomp_decompress(x, skb);
134 EXPORT_SYMBOL_GPL(ipcomp_input);
136 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
138 struct ipcomp_data *ipcd = x->data;
139 const int plen = skb->len;
140 int dlen = IPCOMP_SCRATCH_SIZE;
141 u8 *start = skb->data;
142 const int cpu = get_cpu();
143 u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
144 struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
148 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
153 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
158 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
161 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
169 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
172 struct ip_comp_hdr *ipch;
173 struct ipcomp_data *ipcd = x->data;
175 if (skb->len < ipcd->threshold) {
176 /* Don't bother compressing */
180 if (skb_linearize_cow(skb))
183 err = ipcomp_compress(x, skb);
189 /* Install ipcomp header, convert into ipcomp datagram. */
190 ipch = ip_comp_hdr(skb);
191 ipch->nexthdr = *skb_mac_header(skb);
193 ipch->cpi = htons((u16 )ntohl(x->id.spi));
194 *skb_mac_header(skb) = IPPROTO_COMP;
196 skb_push(skb, -skb_network_offset(skb));
199 EXPORT_SYMBOL_GPL(ipcomp_output);
201 static void ipcomp_free_scratches(void)
206 if (--ipcomp_scratch_users)
209 scratches = ipcomp_scratches;
213 for_each_possible_cpu(i)
214 vfree(*per_cpu_ptr(scratches, i));
216 free_percpu(scratches);
219 static void **ipcomp_alloc_scratches(void)
224 if (ipcomp_scratch_users++)
225 return ipcomp_scratches;
227 scratches = alloc_percpu(void *);
231 ipcomp_scratches = scratches;
233 for_each_possible_cpu(i) {
234 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
237 *per_cpu_ptr(scratches, i) = scratch;
243 static void ipcomp_free_tfms(struct crypto_comp **tfms)
245 struct ipcomp_tfms *pos;
248 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
249 if (pos->tfms == tfms)
258 list_del(&pos->list);
264 for_each_possible_cpu(cpu) {
265 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
266 crypto_free_comp(tfm);
271 static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
273 struct ipcomp_tfms *pos;
274 struct crypto_comp **tfms;
277 /* This can be any valid CPU ID so we don't need locking. */
278 cpu = raw_smp_processor_id();
280 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
281 struct crypto_comp *tfm;
284 tfm = *per_cpu_ptr(tfms, cpu);
286 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
292 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
297 INIT_LIST_HEAD(&pos->list);
298 list_add(&pos->list, &ipcomp_tfms_list);
300 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
304 for_each_possible_cpu(cpu) {
305 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
309 *per_cpu_ptr(tfms, cpu) = tfm;
315 ipcomp_free_tfms(tfms);
319 static void ipcomp_free_data(struct ipcomp_data *ipcd)
322 ipcomp_free_tfms(ipcd->tfms);
323 ipcomp_free_scratches();
326 void ipcomp_destroy(struct xfrm_state *x)
328 struct ipcomp_data *ipcd = x->data;
331 xfrm_state_delete_tunnel(x);
332 mutex_lock(&ipcomp_resource_mutex);
333 ipcomp_free_data(ipcd);
334 mutex_unlock(&ipcomp_resource_mutex);
337 EXPORT_SYMBOL_GPL(ipcomp_destroy);
339 int ipcomp_init_state(struct xfrm_state *x)
342 struct ipcomp_data *ipcd;
343 struct xfrm_algo_desc *calg_desc;
353 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
357 mutex_lock(&ipcomp_resource_mutex);
358 if (!ipcomp_alloc_scratches())
361 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
364 mutex_unlock(&ipcomp_resource_mutex);
366 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
368 ipcd->threshold = calg_desc->uinfo.comp.threshold;
375 ipcomp_free_data(ipcd);
376 mutex_unlock(&ipcomp_resource_mutex);
380 EXPORT_SYMBOL_GPL(ipcomp_init_state);
382 MODULE_LICENSE("GPL");
383 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
384 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");