VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 <linux/crypto.h>
20 #include <linux/pfkeyv2.h>
21 #include <net/ip.h>
22 #include <net/xfrm.h>
23 #include <net/icmp.h>
24 #include <net/ipcomp.h>
25
26 static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
27 {
28         int err, plen, dlen;
29         struct iphdr *iph;
30         struct ipcomp_data *ipcd = x->data;
31         u8 *start, *scratch = ipcd->scratch;
32         
33         plen = skb->len;
34         dlen = IPCOMP_SCRATCH_SIZE;
35         start = skb->data;
36
37         err = crypto_comp_decompress(ipcd->tfm, start, plen, scratch, &dlen);
38         if (err)
39                 goto out;
40
41         if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
42                 err = -EINVAL;
43                 goto out;
44         }
45
46         err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
47         if (err)
48                 goto out;
49                 
50         skb_put(skb, dlen - plen);
51         memcpy(skb->data, scratch, dlen);
52         iph = skb->nh.iph;
53         iph->tot_len = htons(dlen + iph->ihl * 4);
54 out:    
55         return err;
56 }
57
58 static int ipcomp_input(struct xfrm_state *x,
59                         struct xfrm_decap_state *decap, struct sk_buff *skb)
60 {
61         u8 nexthdr;
62         int err = 0;
63         struct iphdr *iph;
64         union {
65                 struct iphdr    iph;
66                 char            buf[60];
67         } tmp_iph;
68
69
70         if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
71             skb_linearize(skb, GFP_ATOMIC) != 0) {
72                 err = -ENOMEM;
73                 goto out;
74         }
75
76         skb->ip_summed = CHECKSUM_NONE;
77
78         /* Remove ipcomp header and decompress original payload */      
79         iph = skb->nh.iph;
80         memcpy(&tmp_iph, iph, iph->ihl * 4);
81         nexthdr = *(u8 *)skb->data;
82         skb_pull(skb, sizeof(struct ip_comp_hdr));
83         skb->nh.raw += sizeof(struct ip_comp_hdr);
84         memcpy(skb->nh.raw, &tmp_iph, tmp_iph.iph.ihl * 4);
85         iph = skb->nh.iph;
86         iph->tot_len = htons(ntohs(iph->tot_len) - sizeof(struct ip_comp_hdr));
87         iph->protocol = nexthdr;
88         skb->h.raw = skb->data;
89         err = ipcomp_decompress(x, skb);
90
91 out:    
92         return err;
93 }
94
95 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
96 {
97         int err, plen, dlen, ihlen;
98         struct iphdr *iph = skb->nh.iph;
99         struct ipcomp_data *ipcd = x->data;
100         u8 *start, *scratch = ipcd->scratch;
101         
102         ihlen = iph->ihl * 4;
103         plen = skb->len - ihlen;
104         dlen = IPCOMP_SCRATCH_SIZE;
105         start = skb->data + ihlen;
106
107         err = crypto_comp_compress(ipcd->tfm, start, plen, scratch, &dlen);
108         if (err)
109                 goto out;
110
111         if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
112                 err = -EMSGSIZE;
113                 goto out;
114         }
115         
116         memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
117         pskb_trim(skb, ihlen + dlen + sizeof(struct ip_comp_hdr));
118         
119 out:    
120         return err;
121 }
122
123 static int ipcomp_output(struct sk_buff **pskb)
124 {
125         int err;
126         struct dst_entry *dst = (*pskb)->dst;
127         struct xfrm_state *x = dst->xfrm;
128         struct iphdr *iph;
129         struct ip_comp_hdr *ipch;
130         struct ipcomp_data *ipcd = x->data;
131         int hdr_len = 0;
132
133         iph = (*pskb)->nh.iph;
134         iph->tot_len = htons((*pskb)->len);
135         hdr_len = iph->ihl * 4;
136         if (((*pskb)->len - hdr_len) < ipcd->threshold) {
137                 /* Don't bother compressing */
138                 if (x->props.mode) {
139                         ip_send_check(iph);
140                 }
141                 goto out_ok;
142         }
143
144         if ((skb_is_nonlinear(*pskb) || skb_cloned(*pskb)) &&
145             skb_linearize(*pskb, GFP_ATOMIC) != 0) {
146                 err = -ENOMEM;
147                 goto error;
148         }
149         
150         err = ipcomp_compress(x, *pskb);
151         if (err) {
152                 if (err == -EMSGSIZE) {
153                         if (x->props.mode) {
154                                 iph = (*pskb)->nh.iph;
155                                 ip_send_check(iph);
156                         }
157                         goto out_ok;
158                 }
159                 goto error;
160         }
161
162         /* Install ipcomp header, convert into ipcomp datagram. */
163         iph = (*pskb)->nh.iph;
164         iph->tot_len = htons((*pskb)->len);
165         ipch = (struct ip_comp_hdr *)((char *)iph + iph->ihl * 4);
166         ipch->nexthdr = iph->protocol;
167         ipch->flags = 0;
168         ipch->cpi = htons((u16 )ntohl(x->id.spi));
169         iph->protocol = IPPROTO_COMP;
170         ip_send_check(iph);
171
172 out_ok:
173         err = 0;
174
175 error:
176         return err;
177 }
178
179 static void ipcomp4_err(struct sk_buff *skb, u32 info)
180 {
181         u32 spi;
182         struct iphdr *iph = (struct iphdr *)skb->data;
183         struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
184         struct xfrm_state *x;
185
186         if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
187             skb->h.icmph->code != ICMP_FRAG_NEEDED)
188                 return;
189
190         spi = ntohl(ntohs(ipch->cpi));
191         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr,
192                               spi, IPPROTO_COMP, AF_INET);
193         if (!x)
194                 return;
195         NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
196                spi, NIPQUAD(iph->daddr)));
197         xfrm_state_put(x);
198 }
199
200 /* We always hold one tunnel user reference to indicate a tunnel */ 
201 static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
202 {
203         struct xfrm_state *t;
204         
205         t = xfrm_state_alloc();
206         if (t == NULL)
207                 goto out;
208
209         t->id.proto = IPPROTO_IPIP;
210         t->id.spi = x->props.saddr.a4;
211         t->id.daddr.a4 = x->id.daddr.a4;
212         memcpy(&t->sel, &x->sel, sizeof(t->sel));
213         t->props.family = AF_INET;
214         t->props.mode = 1;
215         t->props.saddr.a4 = x->props.saddr.a4;
216         t->props.flags = x->props.flags;
217         
218         t->type = xfrm_get_type(IPPROTO_IPIP, t->props.family);
219         if (t->type == NULL)
220                 goto error;
221                 
222         if (t->type->init_state(t, NULL))
223                 goto error;
224
225         t->km.state = XFRM_STATE_VALID;
226         atomic_set(&t->tunnel_users, 1);
227 out:
228         return t;
229
230 error:
231         t->km.state = XFRM_STATE_DEAD;
232         xfrm_state_put(t);
233         t = NULL;
234         goto out;
235 }
236
237 /*
238  * Must be protected by xfrm_cfg_sem.  State and tunnel user references are
239  * always incremented on success.
240  */
241 static int ipcomp_tunnel_attach(struct xfrm_state *x)
242 {
243         int err = 0;
244         struct xfrm_state *t;
245
246         t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
247                               x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
248         if (!t) {
249                 t = ipcomp_tunnel_create(x);
250                 if (!t) {
251                         err = -EINVAL;
252                         goto out;
253                 }
254                 xfrm_state_insert(t);
255                 xfrm_state_hold(t);
256         }
257         x->tunnel = t;
258         atomic_inc(&t->tunnel_users);
259 out:
260         return err;
261 }
262
263 static void ipcomp_free_data(struct ipcomp_data *ipcd)
264 {
265         if (ipcd->tfm)
266                 crypto_free_tfm(ipcd->tfm);
267         if (ipcd->scratch)
268                 kfree(ipcd->scratch);   
269 }
270
271 static void ipcomp_destroy(struct xfrm_state *x)
272 {
273         struct ipcomp_data *ipcd = x->data;
274         if (!ipcd)
275                 return;
276         xfrm_state_delete_tunnel(x);
277         ipcomp_free_data(ipcd);
278         kfree(ipcd);
279 }
280
281 static int ipcomp_init_state(struct xfrm_state *x, void *args)
282 {
283         int err;
284         struct ipcomp_data *ipcd;
285         struct xfrm_algo_desc *calg_desc;
286
287         err = -EINVAL;
288         if (!x->calg)
289                 goto out;
290
291         err = -ENOMEM;
292         ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
293         if (!ipcd)
294                 goto error;
295
296         memset(ipcd, 0, sizeof(*ipcd));
297         x->props.header_len = 0;
298         if (x->props.mode)
299                 x->props.header_len += sizeof(struct iphdr);
300
301         ipcd->scratch = kmalloc(IPCOMP_SCRATCH_SIZE, GFP_KERNEL);
302         if (!ipcd->scratch)
303                 goto error;
304         
305         ipcd->tfm = crypto_alloc_tfm(x->calg->alg_name, 0);
306         if (!ipcd->tfm)
307                 goto error;
308
309         if (x->props.mode) {
310                 err = ipcomp_tunnel_attach(x);
311                 if (err)
312                         goto error;
313         }
314
315         calg_desc = xfrm_calg_get_byname(x->calg->alg_name);
316         BUG_ON(!calg_desc);
317         ipcd->threshold = calg_desc->uinfo.comp.threshold;
318         x->data = ipcd;
319         err = 0;
320 out:
321         return err;
322
323 error:
324         if (ipcd) {
325                 ipcomp_free_data(ipcd);
326                 kfree(ipcd);
327         }
328         goto out;
329 }
330
331 static struct xfrm_type ipcomp_type = {
332         .description    = "IPCOMP4",
333         .owner          = THIS_MODULE,
334         .proto          = IPPROTO_COMP,
335         .init_state     = ipcomp_init_state,
336         .destructor     = ipcomp_destroy,
337         .input          = ipcomp_input,
338         .output         = ipcomp_output
339 };
340
341 static struct net_protocol ipcomp4_protocol = {
342         .handler        =       xfrm4_rcv,
343         .err_handler    =       ipcomp4_err,
344         .no_policy      =       1,
345 };
346
347 static int __init ipcomp4_init(void)
348 {
349         if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
350                 printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
351                 return -EAGAIN;
352         }
353         if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
354                 printk(KERN_INFO "ipcomp init: can't add protocol\n");
355                 xfrm_unregister_type(&ipcomp_type, AF_INET);
356                 return -EAGAIN;
357         }
358         return 0;
359 }
360
361 static void __exit ipcomp4_fini(void)
362 {
363         if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
364                 printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
365         if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
366                 printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
367 }
368
369 module_init(ipcomp4_init);
370 module_exit(ipcomp4_fini);
371
372 MODULE_LICENSE("GPL");
373 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
374 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
375