Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / net / ipv4 / ipcomp.c
1 /*
2  * IP Payload Compression Protocol (IPComp) - RFC3173.
3  *
4  * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option) 
9  * any later version.
10  *
11  * Todo:
12  *   - Tunable compression parameters.
13  *   - Compression stats.
14  *   - Adaptive compression.
15  */
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <asm/scatterlist.h>
19 #include <asm/semaphore.h>
20 #include <linux/crypto.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/percpu.h>
23 #include <linux/smp.h>
24 #include <linux/list.h>
25 #include <linux/vmalloc.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/mutex.h>
28 #include <net/ip.h>
29 #include <net/xfrm.h>
30 #include <net/icmp.h>
31 #include <net/ipcomp.h>
32 #include <net/protocol.h>
33
34 struct ipcomp_tfms {
35         struct list_head list;
36         struct crypto_tfm **tfms;
37         int users;
38 };
39
40 static DEFINE_MUTEX(ipcomp_resource_mutex);
41 static void **ipcomp_scratches;
42 static int ipcomp_scratch_users;
43 static LIST_HEAD(ipcomp_tfms_list);
44
45 static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
46 {
47         int err, plen, dlen;
48         struct iphdr *iph;
49         struct ipcomp_data *ipcd = x->data;
50         u8 *start, *scratch;
51         struct crypto_tfm *tfm;
52         int cpu;
53         
54         plen = skb->len;
55         dlen = IPCOMP_SCRATCH_SIZE;
56         start = skb->data;
57
58         cpu = get_cpu();
59         scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
60         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
61
62         err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
63         if (err)
64                 goto out;
65
66         if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
67                 err = -EINVAL;
68                 goto out;
69         }
70
71         err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
72         if (err)
73                 goto out;
74                 
75         skb_put(skb, dlen - plen);
76         memcpy(skb->data, scratch, dlen);
77         iph = skb->nh.iph;
78         iph->tot_len = htons(dlen + iph->ihl * 4);
79 out:    
80         put_cpu();
81         return err;
82 }
83
84 static int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
85 {
86         u8 nexthdr;
87         int err = -ENOMEM;
88         struct iphdr *iph;
89         union {
90                 struct iphdr    iph;
91                 char            buf[60];
92         } tmp_iph;
93
94
95         if (skb_linearize_cow(skb))
96                 goto out;
97
98         skb->ip_summed = CHECKSUM_NONE;
99
100         /* Remove ipcomp header and decompress original payload */      
101         iph = skb->nh.iph;
102         memcpy(&tmp_iph, iph, iph->ihl * 4);
103         nexthdr = *(u8 *)skb->data;
104         skb_pull(skb, sizeof(struct ip_comp_hdr));
105         skb->nh.raw += sizeof(struct ip_comp_hdr);
106         memcpy(skb->nh.raw, &tmp_iph, tmp_iph.iph.ihl * 4);
107         iph = skb->nh.iph;
108         iph->tot_len = htons(ntohs(iph->tot_len) - sizeof(struct ip_comp_hdr));
109         iph->protocol = nexthdr;
110         skb->h.raw = skb->data;
111         err = ipcomp_decompress(x, skb);
112
113 out:    
114         return err;
115 }
116
117 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
118 {
119         int err, plen, dlen, ihlen;
120         struct iphdr *iph = skb->nh.iph;
121         struct ipcomp_data *ipcd = x->data;
122         u8 *start, *scratch;
123         struct crypto_tfm *tfm;
124         int cpu;
125         
126         ihlen = iph->ihl * 4;
127         plen = skb->len - ihlen;
128         dlen = IPCOMP_SCRATCH_SIZE;
129         start = skb->data + ihlen;
130
131         cpu = get_cpu();
132         scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
133         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
134
135         err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
136         if (err)
137                 goto out;
138
139         if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
140                 err = -EMSGSIZE;
141                 goto out;
142         }
143         
144         memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
145         put_cpu();
146
147         pskb_trim(skb, ihlen + dlen + sizeof(struct ip_comp_hdr));
148         return 0;
149         
150 out:    
151         put_cpu();
152         return err;
153 }
154
155 static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
156 {
157         int err;
158         struct iphdr *iph;
159         struct ip_comp_hdr *ipch;
160         struct ipcomp_data *ipcd = x->data;
161         int hdr_len = 0;
162
163         iph = skb->nh.iph;
164         iph->tot_len = htons(skb->len);
165         hdr_len = iph->ihl * 4;
166         if ((skb->len - hdr_len) < ipcd->threshold) {
167                 /* Don't bother compressing */
168                 goto out_ok;
169         }
170
171         if (skb_linearize_cow(skb))
172                 goto out_ok;
173         
174         err = ipcomp_compress(x, skb);
175         iph = skb->nh.iph;
176
177         if (err) {
178                 goto out_ok;
179         }
180
181         /* Install ipcomp header, convert into ipcomp datagram. */
182         iph->tot_len = htons(skb->len);
183         ipch = (struct ip_comp_hdr *)((char *)iph + iph->ihl * 4);
184         ipch->nexthdr = iph->protocol;
185         ipch->flags = 0;
186         ipch->cpi = htons((u16 )ntohl(x->id.spi));
187         iph->protocol = IPPROTO_COMP;
188         ip_send_check(iph);
189         return 0;
190
191 out_ok:
192         if (x->props.mode)
193                 ip_send_check(iph);
194         return 0;
195 }
196
197 static void ipcomp4_err(struct sk_buff *skb, u32 info)
198 {
199         u32 spi;
200         struct iphdr *iph = (struct iphdr *)skb->data;
201         struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
202         struct xfrm_state *x;
203
204         if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
205             skb->h.icmph->code != ICMP_FRAG_NEEDED)
206                 return;
207
208         spi = htonl(ntohs(ipch->cpi));
209         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr,
210                               spi, IPPROTO_COMP, AF_INET);
211         if (!x)
212                 return;
213         NETDEBUG(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
214                  spi, NIPQUAD(iph->daddr));
215         xfrm_state_put(x);
216 }
217
218 /* We always hold one tunnel user reference to indicate a tunnel */ 
219 static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
220 {
221         struct xfrm_state *t;
222         
223         t = xfrm_state_alloc();
224         if (t == NULL)
225                 goto out;
226
227         t->id.proto = IPPROTO_IPIP;
228         t->id.spi = x->props.saddr.a4;
229         t->id.daddr.a4 = x->id.daddr.a4;
230         memcpy(&t->sel, &x->sel, sizeof(t->sel));
231         t->props.family = AF_INET;
232         t->props.mode = 1;
233         t->props.saddr.a4 = x->props.saddr.a4;
234         t->props.flags = x->props.flags;
235
236         if (xfrm_init_state(t))
237                 goto error;
238
239         atomic_set(&t->tunnel_users, 1);
240 out:
241         return t;
242
243 error:
244         t->km.state = XFRM_STATE_DEAD;
245         xfrm_state_put(t);
246         t = NULL;
247         goto out;
248 }
249
250 /*
251  * Must be protected by xfrm_cfg_mutex.  State and tunnel user references are
252  * always incremented on success.
253  */
254 static int ipcomp_tunnel_attach(struct xfrm_state *x)
255 {
256         int err = 0;
257         struct xfrm_state *t;
258
259         t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
260                               x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
261         if (!t) {
262                 t = ipcomp_tunnel_create(x);
263                 if (!t) {
264                         err = -EINVAL;
265                         goto out;
266                 }
267                 xfrm_state_insert(t);
268                 xfrm_state_hold(t);
269         }
270         x->tunnel = t;
271         atomic_inc(&t->tunnel_users);
272 out:
273         return err;
274 }
275
276 static void ipcomp_free_scratches(void)
277 {
278         int i;
279         void **scratches;
280
281         if (--ipcomp_scratch_users)
282                 return;
283
284         scratches = ipcomp_scratches;
285         if (!scratches)
286                 return;
287
288         for_each_possible_cpu(i)
289                 vfree(*per_cpu_ptr(scratches, i));
290
291         free_percpu(scratches);
292 }
293
294 static void **ipcomp_alloc_scratches(void)
295 {
296         int i;
297         void **scratches;
298
299         if (ipcomp_scratch_users++)
300                 return ipcomp_scratches;
301
302         scratches = alloc_percpu(void *);
303         if (!scratches)
304                 return NULL;
305
306         ipcomp_scratches = scratches;
307
308         for_each_possible_cpu(i) {
309                 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
310                 if (!scratch)
311                         return NULL;
312                 *per_cpu_ptr(scratches, i) = scratch;
313         }
314
315         return scratches;
316 }
317
318 static void ipcomp_free_tfms(struct crypto_tfm **tfms)
319 {
320         struct ipcomp_tfms *pos;
321         int cpu;
322
323         list_for_each_entry(pos, &ipcomp_tfms_list, list) {
324                 if (pos->tfms == tfms)
325                         break;
326         }
327
328         BUG_TRAP(pos);
329
330         if (--pos->users)
331                 return;
332
333         list_del(&pos->list);
334         kfree(pos);
335
336         if (!tfms)
337                 return;
338
339         for_each_possible_cpu(cpu) {
340                 struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
341                 crypto_free_tfm(tfm);
342         }
343         free_percpu(tfms);
344 }
345
346 static struct crypto_tfm **ipcomp_alloc_tfms(const char *alg_name)
347 {
348         struct ipcomp_tfms *pos;
349         struct crypto_tfm **tfms;
350         int cpu;
351
352         /* This can be any valid CPU ID so we don't need locking. */
353         cpu = raw_smp_processor_id();
354
355         list_for_each_entry(pos, &ipcomp_tfms_list, list) {
356                 struct crypto_tfm *tfm;
357
358                 tfms = pos->tfms;
359                 tfm = *per_cpu_ptr(tfms, cpu);
360
361                 if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
362                         pos->users++;
363                         return tfms;
364                 }
365         }
366
367         pos = kmalloc(sizeof(*pos), GFP_KERNEL);
368         if (!pos)
369                 return NULL;
370
371         pos->users = 1;
372         INIT_LIST_HEAD(&pos->list);
373         list_add(&pos->list, &ipcomp_tfms_list);
374
375         pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
376         if (!tfms)
377                 goto error;
378
379         for_each_possible_cpu(cpu) {
380                 struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
381                 if (!tfm)
382                         goto error;
383                 *per_cpu_ptr(tfms, cpu) = tfm;
384         }
385
386         return tfms;
387
388 error:
389         ipcomp_free_tfms(tfms);
390         return NULL;
391 }
392
393 static void ipcomp_free_data(struct ipcomp_data *ipcd)
394 {
395         if (ipcd->tfms)
396                 ipcomp_free_tfms(ipcd->tfms);
397         ipcomp_free_scratches();
398 }
399
400 static void ipcomp_destroy(struct xfrm_state *x)
401 {
402         struct ipcomp_data *ipcd = x->data;
403         if (!ipcd)
404                 return;
405         xfrm_state_delete_tunnel(x);
406         mutex_lock(&ipcomp_resource_mutex);
407         ipcomp_free_data(ipcd);
408         mutex_unlock(&ipcomp_resource_mutex);
409         kfree(ipcd);
410 }
411
412 static int ipcomp_init_state(struct xfrm_state *x)
413 {
414         int err;
415         struct ipcomp_data *ipcd;
416         struct xfrm_algo_desc *calg_desc;
417
418         err = -EINVAL;
419         if (!x->calg)
420                 goto out;
421
422         if (x->encap)
423                 goto out;
424
425         err = -ENOMEM;
426         ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
427         if (!ipcd)
428                 goto out;
429
430         memset(ipcd, 0, sizeof(*ipcd));
431         x->props.header_len = 0;
432         if (x->props.mode)
433                 x->props.header_len += sizeof(struct iphdr);
434
435         mutex_lock(&ipcomp_resource_mutex);
436         if (!ipcomp_alloc_scratches())
437                 goto error;
438
439         ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
440         if (!ipcd->tfms)
441                 goto error;
442         mutex_unlock(&ipcomp_resource_mutex);
443
444         if (x->props.mode) {
445                 err = ipcomp_tunnel_attach(x);
446                 if (err)
447                         goto error_tunnel;
448         }
449
450         calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
451         BUG_ON(!calg_desc);
452         ipcd->threshold = calg_desc->uinfo.comp.threshold;
453         x->data = ipcd;
454         err = 0;
455 out:
456         return err;
457
458 error_tunnel:
459         mutex_lock(&ipcomp_resource_mutex);
460 error:
461         ipcomp_free_data(ipcd);
462         mutex_unlock(&ipcomp_resource_mutex);
463         kfree(ipcd);
464         goto out;
465 }
466
467 static struct xfrm_type ipcomp_type = {
468         .description    = "IPCOMP4",
469         .owner          = THIS_MODULE,
470         .proto          = IPPROTO_COMP,
471         .init_state     = ipcomp_init_state,
472         .destructor     = ipcomp_destroy,
473         .input          = ipcomp_input,
474         .output         = ipcomp_output
475 };
476
477 static struct net_protocol ipcomp4_protocol = {
478         .handler        =       xfrm4_rcv,
479         .err_handler    =       ipcomp4_err,
480         .no_policy      =       1,
481 };
482
483 static int __init ipcomp4_init(void)
484 {
485         if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
486                 printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
487                 return -EAGAIN;
488         }
489         if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
490                 printk(KERN_INFO "ipcomp init: can't add protocol\n");
491                 xfrm_unregister_type(&ipcomp_type, AF_INET);
492                 return -EAGAIN;
493         }
494         return 0;
495 }
496
497 static void __exit ipcomp4_fini(void)
498 {
499         if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
500                 printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
501         if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
502                 printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
503 }
504
505 module_init(ipcomp4_init);
506 module_exit(ipcomp4_fini);
507
508 MODULE_LICENSE("GPL");
509 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
510 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
511