blob: 2906d520eea7c2b7636fc94f3a60f5131701f57d [file] [log] [blame]
Herbert Xu6fccab62008-07-25 02:54:40 -07001/*
2 * IP Payload Compression Protocol (IPComp) - RFC3173.
3 *
4 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
6 *
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.
11 *
12 * Todo:
13 * - Tunable compression parameters.
14 * - Compression stats.
15 * - Adaptive compression.
16 */
17
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Herbert Xu6fccab62008-07-25 02:54:40 -070025#include <linux/smp.h>
26#include <linux/vmalloc.h>
27#include <net/ip.h>
28#include <net/ipcomp.h>
29#include <net/xfrm.h>
30
31struct ipcomp_tfms {
32 struct list_head list;
Tejun Heo7d720c32010-02-16 15:20:26 +000033 struct crypto_comp * __percpu *tfms;
Herbert Xu6fccab62008-07-25 02:54:40 -070034 int users;
35};
36
37static DEFINE_MUTEX(ipcomp_resource_mutex);
Tejun Heo7d720c32010-02-16 15:20:26 +000038static void * __percpu *ipcomp_scratches;
Herbert Xu6fccab62008-07-25 02:54:40 -070039static int ipcomp_scratch_users;
40static LIST_HEAD(ipcomp_tfms_list);
41
42static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
43{
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);
Herbert Xu7d7e5a62008-07-25 02:55:33 -070052 int len;
Herbert Xu6fccab62008-07-25 02:54:40 -070053
54 if (err)
55 goto out;
56
57 if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
58 err = -EINVAL;
59 goto out;
60 }
61
Herbert Xu7d7e5a62008-07-25 02:55:33 -070062 len = dlen - plen;
63 if (len > skb_tailroom(skb))
64 len = skb_tailroom(skb);
Herbert Xu6fccab62008-07-25 02:54:40 -070065
Herbert Xu7d7e5a62008-07-25 02:55:33 -070066 __skb_put(skb, len);
67
68 len += plen;
69 skb_copy_to_linear_data(skb, scratch, len);
70
71 while ((scratch += len, dlen -= len) > 0) {
72 skb_frag_t *frag;
Ian Campbell804cf142011-08-22 23:45:01 +000073 struct page *page;
Herbert Xu7d7e5a62008-07-25 02:55:33 -070074
75 err = -EMSGSIZE;
76 if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
77 goto out;
78
79 frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
Ian Campbell804cf142011-08-22 23:45:01 +000080 page = alloc_page(GFP_ATOMIC);
Herbert Xu7d7e5a62008-07-25 02:55:33 -070081
82 err = -ENOMEM;
Ian Campbell804cf142011-08-22 23:45:01 +000083 if (!page)
Herbert Xu7d7e5a62008-07-25 02:55:33 -070084 goto out;
85
Ian Campbell804cf142011-08-22 23:45:01 +000086 __skb_frag_set_page(frag, page);
87
Herbert Xu7d7e5a62008-07-25 02:55:33 -070088 len = PAGE_SIZE;
89 if (dlen < len)
90 len = dlen;
91
Herbert Xu7d7e5a62008-07-25 02:55:33 -070092 frag->page_offset = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +000093 skb_frag_size_set(frag, len);
Ian Campbell804cf142011-08-22 23:45:01 +000094 memcpy(skb_frag_address(frag), scratch, len);
95
Herbert Xu7d7e5a62008-07-25 02:55:33 -070096 skb->truesize += len;
97 skb->data_len += len;
98 skb->len += len;
99
100 skb_shinfo(skb)->nr_frags++;
101 }
102
103 err = 0;
104
Herbert Xu6fccab62008-07-25 02:54:40 -0700105out:
106 put_cpu();
107 return err;
108}
109
110int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
111{
112 int nexthdr;
113 int err = -ENOMEM;
114 struct ip_comp_hdr *ipch;
115
116 if (skb_linearize_cow(skb))
117 goto out;
118
119 skb->ip_summed = CHECKSUM_NONE;
120
121 /* Remove ipcomp header and decompress original payload */
122 ipch = (void *)skb->data;
123 nexthdr = ipch->nexthdr;
124
125 skb->transport_header = skb->network_header + sizeof(*ipch);
126 __skb_pull(skb, sizeof(*ipch));
127 err = ipcomp_decompress(x, skb);
128 if (err)
129 goto out;
130
131 err = nexthdr;
132
133out:
134 return err;
135}
136EXPORT_SYMBOL_GPL(ipcomp_input);
137
138static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
139{
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);
147 int err;
148
149 local_bh_disable();
150 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
151 local_bh_enable();
152 if (err)
153 goto out;
154
155 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
156 err = -EMSGSIZE;
157 goto out;
158 }
159
160 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
161 put_cpu();
162
163 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
164 return 0;
165
166out:
167 put_cpu();
168 return err;
169}
170
171int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
172{
173 int err;
174 struct ip_comp_hdr *ipch;
175 struct ipcomp_data *ipcd = x->data;
176
177 if (skb->len < ipcd->threshold) {
178 /* Don't bother compressing */
179 goto out_ok;
180 }
181
182 if (skb_linearize_cow(skb))
183 goto out_ok;
184
185 err = ipcomp_compress(x, skb);
186
187 if (err) {
188 goto out_ok;
189 }
190
191 /* Install ipcomp header, convert into ipcomp datagram. */
192 ipch = ip_comp_hdr(skb);
193 ipch->nexthdr = *skb_mac_header(skb);
194 ipch->flags = 0;
195 ipch->cpi = htons((u16 )ntohl(x->id.spi));
196 *skb_mac_header(skb) = IPPROTO_COMP;
197out_ok:
198 skb_push(skb, -skb_network_offset(skb));
199 return 0;
200}
201EXPORT_SYMBOL_GPL(ipcomp_output);
202
203static void ipcomp_free_scratches(void)
204{
205 int i;
Tejun Heo7d720c32010-02-16 15:20:26 +0000206 void * __percpu *scratches;
Herbert Xu6fccab62008-07-25 02:54:40 -0700207
208 if (--ipcomp_scratch_users)
209 return;
210
211 scratches = ipcomp_scratches;
212 if (!scratches)
213 return;
214
215 for_each_possible_cpu(i)
216 vfree(*per_cpu_ptr(scratches, i));
217
218 free_percpu(scratches);
219}
220
Tejun Heo7d720c32010-02-16 15:20:26 +0000221static void * __percpu *ipcomp_alloc_scratches(void)
Herbert Xu6fccab62008-07-25 02:54:40 -0700222{
223 int i;
Tejun Heo7d720c32010-02-16 15:20:26 +0000224 void * __percpu *scratches;
Herbert Xu6fccab62008-07-25 02:54:40 -0700225
226 if (ipcomp_scratch_users++)
227 return ipcomp_scratches;
228
229 scratches = alloc_percpu(void *);
230 if (!scratches)
231 return NULL;
232
233 ipcomp_scratches = scratches;
234
235 for_each_possible_cpu(i) {
236 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
237 if (!scratch)
238 return NULL;
239 *per_cpu_ptr(scratches, i) = scratch;
240 }
241
242 return scratches;
243}
244
Tejun Heo7d720c32010-02-16 15:20:26 +0000245static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
Herbert Xu6fccab62008-07-25 02:54:40 -0700246{
247 struct ipcomp_tfms *pos;
248 int cpu;
249
250 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
251 if (pos->tfms == tfms)
252 break;
253 }
254
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700255 WARN_ON(!pos);
Herbert Xu6fccab62008-07-25 02:54:40 -0700256
257 if (--pos->users)
258 return;
259
260 list_del(&pos->list);
261 kfree(pos);
262
263 if (!tfms)
264 return;
265
266 for_each_possible_cpu(cpu) {
267 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
268 crypto_free_comp(tfm);
269 }
270 free_percpu(tfms);
271}
272
Tejun Heo7d720c32010-02-16 15:20:26 +0000273static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
Herbert Xu6fccab62008-07-25 02:54:40 -0700274{
275 struct ipcomp_tfms *pos;
Tejun Heo7d720c32010-02-16 15:20:26 +0000276 struct crypto_comp * __percpu *tfms;
Herbert Xu6fccab62008-07-25 02:54:40 -0700277 int cpu;
278
Herbert Xu6fccab62008-07-25 02:54:40 -0700279
280 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
281 struct crypto_comp *tfm;
282
Shan Weif7c83bc2012-11-13 20:36:00 +0800283 /* This can be any valid CPU ID so we don't need locking. */
284 tfm = __this_cpu_read(*pos->tfms);
Herbert Xu6fccab62008-07-25 02:54:40 -0700285
286 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
287 pos->users++;
Shan Weif7c83bc2012-11-13 20:36:00 +0800288 return pos->tfms;
Herbert Xu6fccab62008-07-25 02:54:40 -0700289 }
290 }
291
292 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
293 if (!pos)
294 return NULL;
295
296 pos->users = 1;
297 INIT_LIST_HEAD(&pos->list);
298 list_add(&pos->list, &ipcomp_tfms_list);
299
300 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
301 if (!tfms)
302 goto error;
303
304 for_each_possible_cpu(cpu) {
305 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
306 CRYPTO_ALG_ASYNC);
307 if (IS_ERR(tfm))
308 goto error;
309 *per_cpu_ptr(tfms, cpu) = tfm;
310 }
311
312 return tfms;
313
314error:
315 ipcomp_free_tfms(tfms);
316 return NULL;
317}
318
319static void ipcomp_free_data(struct ipcomp_data *ipcd)
320{
321 if (ipcd->tfms)
322 ipcomp_free_tfms(ipcd->tfms);
323 ipcomp_free_scratches();
324}
325
326void ipcomp_destroy(struct xfrm_state *x)
327{
328 struct ipcomp_data *ipcd = x->data;
329 if (!ipcd)
330 return;
331 xfrm_state_delete_tunnel(x);
332 mutex_lock(&ipcomp_resource_mutex);
333 ipcomp_free_data(ipcd);
334 mutex_unlock(&ipcomp_resource_mutex);
335 kfree(ipcd);
336}
337EXPORT_SYMBOL_GPL(ipcomp_destroy);
338
339int ipcomp_init_state(struct xfrm_state *x)
340{
341 int err;
342 struct ipcomp_data *ipcd;
343 struct xfrm_algo_desc *calg_desc;
344
345 err = -EINVAL;
346 if (!x->calg)
347 goto out;
348
349 if (x->encap)
350 goto out;
351
352 err = -ENOMEM;
353 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
354 if (!ipcd)
355 goto out;
356
357 mutex_lock(&ipcomp_resource_mutex);
358 if (!ipcomp_alloc_scratches())
359 goto error;
360
361 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
362 if (!ipcd->tfms)
363 goto error;
364 mutex_unlock(&ipcomp_resource_mutex);
365
366 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
367 BUG_ON(!calg_desc);
368 ipcd->threshold = calg_desc->uinfo.comp.threshold;
369 x->data = ipcd;
370 err = 0;
371out:
372 return err;
373
374error:
375 ipcomp_free_data(ipcd);
376 mutex_unlock(&ipcomp_resource_mutex);
377 kfree(ipcd);
378 goto out;
379}
380EXPORT_SYMBOL_GPL(ipcomp_init_state);
381
382MODULE_LICENSE("GPL");
383MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
384MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");