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