ipsec: ipcomp - Decompress into frags if necessary
[linux-2.6/kvm.git] / net / xfrm / xfrm_ipcomp.c
blob800f669083fb4af63a745778993441981fccf937
1 /*
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)
10 * any later version.
12 * Todo:
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>
28 #include <net/ip.h>
29 #include <net/ipcomp.h>
30 #include <net/xfrm.h>
32 struct ipcomp_tfms {
33 struct list_head list;
34 struct crypto_comp **tfms;
35 int users;
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);
53 int len;
55 if (err)
56 goto out;
58 if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
59 err = -EINVAL;
60 goto out;
63 len = dlen - plen;
64 if (len > skb_tailroom(skb))
65 len = skb_tailroom(skb);
67 skb->truesize += len;
68 __skb_put(skb, len);
70 len += plen;
71 skb_copy_to_linear_data(skb, scratch, len);
73 while ((scratch += len, dlen -= len) > 0) {
74 skb_frag_t *frag;
76 err = -EMSGSIZE;
77 if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
78 goto out;
80 frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
81 frag->page = alloc_page(GFP_ATOMIC);
83 err = -ENOMEM;
84 if (!frag->page)
85 goto out;
87 len = PAGE_SIZE;
88 if (dlen < len)
89 len = dlen;
91 memcpy(page_address(frag->page), scratch, len);
93 frag->page_offset = 0;
94 frag->size = len;
95 skb->truesize += len;
96 skb->data_len += len;
97 skb->len += len;
99 skb_shinfo(skb)->nr_frags++;
102 err = 0;
104 out:
105 put_cpu();
106 return err;
109 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
111 int nexthdr;
112 int err = -ENOMEM;
113 struct ip_comp_hdr *ipch;
115 if (skb_linearize_cow(skb))
116 goto out;
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);
127 if (err)
128 goto out;
130 err = nexthdr;
132 out:
133 return err;
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);
146 int err;
148 local_bh_disable();
149 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
150 local_bh_enable();
151 if (err)
152 goto out;
154 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
155 err = -EMSGSIZE;
156 goto out;
159 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
160 put_cpu();
162 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
163 return 0;
165 out:
166 put_cpu();
167 return err;
170 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
172 int err;
173 struct ip_comp_hdr *ipch;
174 struct ipcomp_data *ipcd = x->data;
176 if (skb->len < ipcd->threshold) {
177 /* Don't bother compressing */
178 goto out_ok;
181 if (skb_linearize_cow(skb))
182 goto out_ok;
184 err = ipcomp_compress(x, skb);
186 if (err) {
187 goto out_ok;
190 /* Install ipcomp header, convert into ipcomp datagram. */
191 ipch = ip_comp_hdr(skb);
192 ipch->nexthdr = *skb_mac_header(skb);
193 ipch->flags = 0;
194 ipch->cpi = htons((u16 )ntohl(x->id.spi));
195 *skb_mac_header(skb) = IPPROTO_COMP;
196 out_ok:
197 skb_push(skb, -skb_network_offset(skb));
198 return 0;
200 EXPORT_SYMBOL_GPL(ipcomp_output);
202 static void ipcomp_free_scratches(void)
204 int i;
205 void **scratches;
207 if (--ipcomp_scratch_users)
208 return;
210 scratches = ipcomp_scratches;
211 if (!scratches)
212 return;
214 for_each_possible_cpu(i)
215 vfree(*per_cpu_ptr(scratches, i));
217 free_percpu(scratches);
220 static void **ipcomp_alloc_scratches(void)
222 int i;
223 void **scratches;
225 if (ipcomp_scratch_users++)
226 return ipcomp_scratches;
228 scratches = alloc_percpu(void *);
229 if (!scratches)
230 return NULL;
232 ipcomp_scratches = scratches;
234 for_each_possible_cpu(i) {
235 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
236 if (!scratch)
237 return NULL;
238 *per_cpu_ptr(scratches, i) = scratch;
241 return scratches;
244 static void ipcomp_free_tfms(struct crypto_comp **tfms)
246 struct ipcomp_tfms *pos;
247 int cpu;
249 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
250 if (pos->tfms == tfms)
251 break;
254 BUG_TRAP(pos);
256 if (--pos->users)
257 return;
259 list_del(&pos->list);
260 kfree(pos);
262 if (!tfms)
263 return;
265 for_each_possible_cpu(cpu) {
266 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
267 crypto_free_comp(tfm);
269 free_percpu(tfms);
272 static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
274 struct ipcomp_tfms *pos;
275 struct crypto_comp **tfms;
276 int cpu;
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;
284 tfms = pos->tfms;
285 tfm = *per_cpu_ptr(tfms, cpu);
287 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
288 pos->users++;
289 return tfms;
293 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
294 if (!pos)
295 return NULL;
297 pos->users = 1;
298 INIT_LIST_HEAD(&pos->list);
299 list_add(&pos->list, &ipcomp_tfms_list);
301 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
302 if (!tfms)
303 goto error;
305 for_each_possible_cpu(cpu) {
306 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
307 CRYPTO_ALG_ASYNC);
308 if (IS_ERR(tfm))
309 goto error;
310 *per_cpu_ptr(tfms, cpu) = tfm;
313 return tfms;
315 error:
316 ipcomp_free_tfms(tfms);
317 return NULL;
320 static void ipcomp_free_data(struct ipcomp_data *ipcd)
322 if (ipcd->tfms)
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;
330 if (!ipcd)
331 return;
332 xfrm_state_delete_tunnel(x);
333 mutex_lock(&ipcomp_resource_mutex);
334 ipcomp_free_data(ipcd);
335 mutex_unlock(&ipcomp_resource_mutex);
336 kfree(ipcd);
338 EXPORT_SYMBOL_GPL(ipcomp_destroy);
340 int ipcomp_init_state(struct xfrm_state *x)
342 int err;
343 struct ipcomp_data *ipcd;
344 struct xfrm_algo_desc *calg_desc;
346 err = -EINVAL;
347 if (!x->calg)
348 goto out;
350 if (x->encap)
351 goto out;
353 err = -ENOMEM;
354 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
355 if (!ipcd)
356 goto out;
358 mutex_lock(&ipcomp_resource_mutex);
359 if (!ipcomp_alloc_scratches())
360 goto error;
362 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
363 if (!ipcd->tfms)
364 goto error;
365 mutex_unlock(&ipcomp_resource_mutex);
367 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
368 BUG_ON(!calg_desc);
369 ipcd->threshold = calg_desc->uinfo.comp.threshold;
370 x->data = ipcd;
371 err = 0;
372 out:
373 return err;
375 error:
376 ipcomp_free_data(ipcd);
377 mutex_unlock(&ipcomp_resource_mutex);
378 kfree(ipcd);
379 goto out;
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>");