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/slab.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 * __percpu *tfms;
37 static DEFINE_MUTEX(ipcomp_resource_mutex);
38 static void * __percpu *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);
69 skb_copy_to_linear_data(skb, scratch, len);
71 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 page = alloc_page(GFP_ATOMIC);
86 __skb_frag_set_page(frag, page);
92 frag->page_offset = 0;
93 skb_frag_size_set(frag, len);
94 memcpy(skb_frag_address(frag), scratch, len);
100 skb_shinfo(skb)->nr_frags++;
110 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
114 struct ip_comp_hdr *ipch;
116 if (skb_linearize_cow(skb))
119 skb->ip_summed = CHECKSUM_NONE;
121 /* Remove ipcomp header and decompress original payload */
122 ipch = (void *)skb->data;
123 nexthdr = ipch->nexthdr;
125 skb->transport_header = skb->network_header + sizeof(*ipch);
126 __skb_pull(skb, sizeof(*ipch));
127 err = ipcomp_decompress(x, skb);
136 EXPORT_SYMBOL_GPL(ipcomp_input);
138 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
140 struct ipcomp_data *ipcd = x->data;
141 const int plen = skb->len;
142 int dlen = IPCOMP_SCRATCH_SIZE;
143 u8 *start = skb->data;
144 const int cpu = get_cpu();
145 u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
146 struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
150 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
155 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
160 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
163 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
171 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
174 struct ip_comp_hdr *ipch;
175 struct ipcomp_data *ipcd = x->data;
177 if (skb->len < ipcd->threshold) {
178 /* Don't bother compressing */
182 if (skb_linearize_cow(skb))
185 err = ipcomp_compress(x, skb);
191 /* Install ipcomp header, convert into ipcomp datagram. */
192 ipch = ip_comp_hdr(skb);
193 ipch->nexthdr = *skb_mac_header(skb);
195 ipch->cpi = htons((u16 )ntohl(x->id.spi));
196 *skb_mac_header(skb) = IPPROTO_COMP;
198 skb_push(skb, -skb_network_offset(skb));
201 EXPORT_SYMBOL_GPL(ipcomp_output);
203 static void ipcomp_free_scratches(void)
206 void * __percpu *scratches;
208 if (--ipcomp_scratch_users)
211 scratches = ipcomp_scratches;
215 for_each_possible_cpu(i)
216 vfree(*per_cpu_ptr(scratches, i));
218 free_percpu(scratches);
221 static void * __percpu *ipcomp_alloc_scratches(void)
224 void * __percpu *scratches;
226 if (ipcomp_scratch_users++)
227 return ipcomp_scratches;
229 scratches = alloc_percpu(void *);
233 ipcomp_scratches = scratches;
235 for_each_possible_cpu(i) {
236 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
239 *per_cpu_ptr(scratches, i) = scratch;
245 static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
247 struct ipcomp_tfms *pos;
250 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
251 if (pos->tfms == tfms)
260 list_del(&pos->list);
266 for_each_possible_cpu(cpu) {
267 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
268 crypto_free_comp(tfm);
273 static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
275 struct ipcomp_tfms *pos;
276 struct crypto_comp * __percpu *tfms;
279 /* This can be any valid CPU ID so we don't need locking. */
280 cpu = raw_smp_processor_id();
282 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
283 struct crypto_comp *tfm;
286 tfm = *per_cpu_ptr(tfms, cpu);
288 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
294 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
299 INIT_LIST_HEAD(&pos->list);
300 list_add(&pos->list, &ipcomp_tfms_list);
302 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
306 for_each_possible_cpu(cpu) {
307 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
311 *per_cpu_ptr(tfms, cpu) = tfm;
317 ipcomp_free_tfms(tfms);
321 static void ipcomp_free_data(struct ipcomp_data *ipcd)
324 ipcomp_free_tfms(ipcd->tfms);
325 ipcomp_free_scratches();
328 void ipcomp_destroy(struct xfrm_state *x)
330 struct ipcomp_data *ipcd = x->data;
333 xfrm_state_delete_tunnel(x);
334 mutex_lock(&ipcomp_resource_mutex);
335 ipcomp_free_data(ipcd);
336 mutex_unlock(&ipcomp_resource_mutex);
339 EXPORT_SYMBOL_GPL(ipcomp_destroy);
341 int ipcomp_init_state(struct xfrm_state *x)
344 struct ipcomp_data *ipcd;
345 struct xfrm_algo_desc *calg_desc;
355 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
359 mutex_lock(&ipcomp_resource_mutex);
360 if (!ipcomp_alloc_scratches())
363 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
366 mutex_unlock(&ipcomp_resource_mutex);
368 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
370 ipcd->threshold = calg_desc->uinfo.comp.threshold;
377 ipcomp_free_data(ipcd);
378 mutex_unlock(&ipcomp_resource_mutex);
382 EXPORT_SYMBOL_GPL(ipcomp_init_state);
384 MODULE_LICENSE("GPL");
385 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
386 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");