[IPSEC] proto: Move transport mode input path into xfrm_mode_transport
[safe/jmp/linux-2.6] / net / ipv6 / ipcomp6.c
1 /*
2  * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
3  *
4  * Copyright (C)2003 USAGI/WIDE Project
5  *
6  * Author       Mitsuru KANDA  <mk@linux-ipv6.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 /* 
23  * [Memo]
24  *
25  * Outbound:
26  *  The compression of IP datagram MUST be done before AH/ESP processing, 
27  *  fragmentation, and the addition of Hop-by-Hop/Routing header. 
28  *
29  * Inbound:
30  *  The decompression of IP datagram MUST be done after the reassembly, 
31  *  AH/ESP processing.
32  */
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <net/ip.h>
36 #include <net/xfrm.h>
37 #include <net/ipcomp.h>
38 #include <asm/scatterlist.h>
39 #include <asm/semaphore.h>
40 #include <linux/crypto.h>
41 #include <linux/pfkeyv2.h>
42 #include <linux/random.h>
43 #include <linux/percpu.h>
44 #include <linux/smp.h>
45 #include <linux/list.h>
46 #include <linux/vmalloc.h>
47 #include <linux/rtnetlink.h>
48 #include <net/icmp.h>
49 #include <net/ipv6.h>
50 #include <net/protocol.h>
51 #include <linux/ipv6.h>
52 #include <linux/icmpv6.h>
53 #include <linux/mutex.h>
54
55 struct ipcomp6_tfms {
56         struct list_head list;
57         struct crypto_tfm **tfms;
58         int users;
59 };
60
61 static DEFINE_MUTEX(ipcomp6_resource_mutex);
62 static void **ipcomp6_scratches;
63 static int ipcomp6_scratch_users;
64 static LIST_HEAD(ipcomp6_tfms_list);
65
66 static int ipcomp6_input(struct xfrm_state *x, struct sk_buff *skb)
67 {
68         int err = 0;
69         struct ipv6hdr *iph;
70         struct ipv6_comp_hdr *ipch;
71         int plen, dlen;
72         struct ipcomp_data *ipcd = x->data;
73         u8 *start, *scratch;
74         struct crypto_tfm *tfm;
75         int cpu;
76
77         if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
78                 skb_linearize(skb, GFP_ATOMIC) != 0) {
79                 err = -ENOMEM;
80                 goto out;
81         }
82
83         skb->ip_summed = CHECKSUM_NONE;
84
85         /* Remove ipcomp header and decompress original payload */
86         iph = skb->nh.ipv6h;
87         ipch = (void *)skb->data;
88         skb->h.raw = skb->nh.raw + sizeof(*ipch);
89         __skb_pull(skb, sizeof(*ipch));
90
91         /* decompression */
92         plen = skb->len;
93         dlen = IPCOMP_SCRATCH_SIZE;
94         start = skb->data;
95
96         cpu = get_cpu();
97         scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
98         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
99
100         err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
101         if (err) {
102                 err = -EINVAL;
103                 goto out_put_cpu;
104         }
105
106         if (dlen < (plen + sizeof(struct ipv6_comp_hdr))) {
107                 err = -EINVAL;
108                 goto out_put_cpu;
109         }
110
111         err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
112         if (err) {
113                 goto out_put_cpu;
114         }
115
116         skb_put(skb, dlen - plen);
117         memcpy(skb->data, scratch, dlen);
118         err = ipch->nexthdr;
119
120 out_put_cpu:
121         put_cpu();
122 out:
123         return err;
124 }
125
126 static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
127 {
128         int err;
129         struct ipv6hdr *top_iph;
130         int hdr_len;
131         struct ipv6_comp_hdr *ipch;
132         struct ipcomp_data *ipcd = x->data;
133         int plen, dlen;
134         u8 *start, *scratch;
135         struct crypto_tfm *tfm;
136         int cpu;
137
138         hdr_len = skb->h.raw - skb->data;
139
140         /* check whether datagram len is larger than threshold */
141         if ((skb->len - hdr_len) < ipcd->threshold) {
142                 goto out_ok;
143         }
144
145         if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
146                 skb_linearize(skb, GFP_ATOMIC) != 0) {
147                 goto out_ok;
148         }
149
150         /* compression */
151         plen = skb->len - hdr_len;
152         dlen = IPCOMP_SCRATCH_SIZE;
153         start = skb->h.raw;
154
155         cpu = get_cpu();
156         scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
157         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
158
159         err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
160         if (err || (dlen + sizeof(struct ipv6_comp_hdr)) >= plen) {
161                 put_cpu();
162                 goto out_ok;
163         }
164         memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
165         put_cpu();
166         pskb_trim(skb, hdr_len + dlen + sizeof(struct ip_comp_hdr));
167
168         /* insert ipcomp header and replace datagram */
169         top_iph = (struct ipv6hdr *)skb->data;
170
171         top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
172
173         ipch = (struct ipv6_comp_hdr *)start;
174         ipch->nexthdr = *skb->nh.raw;
175         ipch->flags = 0;
176         ipch->cpi = htons((u16 )ntohl(x->id.spi));
177         *skb->nh.raw = IPPROTO_COMP;
178
179 out_ok:
180         return 0;
181 }
182
183 static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
184                                 int type, int code, int offset, __u32 info)
185 {
186         u32 spi;
187         struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
188         struct ipv6_comp_hdr *ipcomph = (struct ipv6_comp_hdr*)(skb->data+offset);
189         struct xfrm_state *x;
190
191         if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
192                 return;
193
194         spi = htonl(ntohs(ipcomph->cpi));
195         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
196         if (!x)
197                 return;
198
199         printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
200                         spi, NIP6(iph->daddr));
201         xfrm_state_put(x);
202 }
203
204 static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
205 {
206         struct xfrm_state *t = NULL;
207
208         t = xfrm_state_alloc();
209         if (!t)
210                 goto out;
211
212         t->id.proto = IPPROTO_IPV6;
213         t->id.spi = xfrm6_tunnel_alloc_spi((xfrm_address_t *)&x->props.saddr);
214         if (!t->id.spi)
215                 goto error;
216
217         memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
218         memcpy(&t->sel, &x->sel, sizeof(t->sel));
219         t->props.family = AF_INET6;
220         t->props.mode = 1;
221         memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
222
223         if (xfrm_init_state(t))
224                 goto error;
225
226         atomic_set(&t->tunnel_users, 1);
227
228 out:
229         return t;
230
231 error:
232         t->km.state = XFRM_STATE_DEAD;
233         xfrm_state_put(t);
234         t = NULL;
235         goto out;
236 }
237
238 static int ipcomp6_tunnel_attach(struct xfrm_state *x)
239 {
240         int err = 0;
241         struct xfrm_state *t = NULL;
242         u32 spi;
243
244         spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&x->props.saddr);
245         if (spi)
246                 t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr,
247                                               spi, IPPROTO_IPV6, AF_INET6);
248         if (!t) {
249                 t = ipcomp6_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
260 out:
261         return err;
262 }
263
264 static void ipcomp6_free_scratches(void)
265 {
266         int i;
267         void **scratches;
268
269         if (--ipcomp6_scratch_users)
270                 return;
271
272         scratches = ipcomp6_scratches;
273         if (!scratches)
274                 return;
275
276         for_each_possible_cpu(i) {
277                 void *scratch = *per_cpu_ptr(scratches, i);
278
279                 vfree(scratch);
280         }
281
282         free_percpu(scratches);
283 }
284
285 static void **ipcomp6_alloc_scratches(void)
286 {
287         int i;
288         void **scratches;
289
290         if (ipcomp6_scratch_users++)
291                 return ipcomp6_scratches;
292
293         scratches = alloc_percpu(void *);
294         if (!scratches)
295                 return NULL;
296
297         ipcomp6_scratches = scratches;
298
299         for_each_possible_cpu(i) {
300                 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
301                 if (!scratch)
302                         return NULL;
303                 *per_cpu_ptr(scratches, i) = scratch;
304         }
305
306         return scratches;
307 }
308
309 static void ipcomp6_free_tfms(struct crypto_tfm **tfms)
310 {
311         struct ipcomp6_tfms *pos;
312         int cpu;
313
314         list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
315                 if (pos->tfms == tfms)
316                         break;
317         }
318
319         BUG_TRAP(pos);
320
321         if (--pos->users)
322                 return;
323
324         list_del(&pos->list);
325         kfree(pos);
326
327         if (!tfms)
328                 return;
329
330         for_each_possible_cpu(cpu) {
331                 struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
332                 crypto_free_tfm(tfm);
333         }
334         free_percpu(tfms);
335 }
336
337 static struct crypto_tfm **ipcomp6_alloc_tfms(const char *alg_name)
338 {
339         struct ipcomp6_tfms *pos;
340         struct crypto_tfm **tfms;
341         int cpu;
342
343         /* This can be any valid CPU ID so we don't need locking. */
344         cpu = raw_smp_processor_id();
345
346         list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
347                 struct crypto_tfm *tfm;
348
349                 tfms = pos->tfms;
350                 tfm = *per_cpu_ptr(tfms, cpu);
351
352                 if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
353                         pos->users++;
354                         return tfms;
355                 }
356         }
357
358         pos = kmalloc(sizeof(*pos), GFP_KERNEL);
359         if (!pos)
360                 return NULL;
361
362         pos->users = 1;
363         INIT_LIST_HEAD(&pos->list);
364         list_add(&pos->list, &ipcomp6_tfms_list);
365
366         pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
367         if (!tfms)
368                 goto error;
369
370         for_each_possible_cpu(cpu) {
371                 struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
372                 if (!tfm)
373                         goto error;
374                 *per_cpu_ptr(tfms, cpu) = tfm;
375         }
376
377         return tfms;
378
379 error:
380         ipcomp6_free_tfms(tfms);
381         return NULL;
382 }
383
384 static void ipcomp6_free_data(struct ipcomp_data *ipcd)
385 {
386         if (ipcd->tfms)
387                 ipcomp6_free_tfms(ipcd->tfms);
388         ipcomp6_free_scratches();
389 }
390
391 static void ipcomp6_destroy(struct xfrm_state *x)
392 {
393         struct ipcomp_data *ipcd = x->data;
394         if (!ipcd)
395                 return;
396         xfrm_state_delete_tunnel(x);
397         mutex_lock(&ipcomp6_resource_mutex);
398         ipcomp6_free_data(ipcd);
399         mutex_unlock(&ipcomp6_resource_mutex);
400         kfree(ipcd);
401
402         xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
403 }
404
405 static int ipcomp6_init_state(struct xfrm_state *x)
406 {
407         int err;
408         struct ipcomp_data *ipcd;
409         struct xfrm_algo_desc *calg_desc;
410
411         err = -EINVAL;
412         if (!x->calg)
413                 goto out;
414
415         if (x->encap)
416                 goto out;
417
418         err = -ENOMEM;
419         ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
420         if (!ipcd)
421                 goto out;
422
423         x->props.header_len = 0;
424         if (x->props.mode)
425                 x->props.header_len += sizeof(struct ipv6hdr);
426         
427         mutex_lock(&ipcomp6_resource_mutex);
428         if (!ipcomp6_alloc_scratches())
429                 goto error;
430
431         ipcd->tfms = ipcomp6_alloc_tfms(x->calg->alg_name);
432         if (!ipcd->tfms)
433                 goto error;
434         mutex_unlock(&ipcomp6_resource_mutex);
435
436         if (x->props.mode) {
437                 err = ipcomp6_tunnel_attach(x);
438                 if (err)
439                         goto error_tunnel;
440         }
441
442         calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
443         BUG_ON(!calg_desc);
444         ipcd->threshold = calg_desc->uinfo.comp.threshold;
445         x->data = ipcd;
446         err = 0;
447 out:
448         return err;
449 error_tunnel:
450         mutex_lock(&ipcomp6_resource_mutex);
451 error:
452         ipcomp6_free_data(ipcd);
453         mutex_unlock(&ipcomp6_resource_mutex);
454         kfree(ipcd);
455
456         goto out;
457 }
458
459 static struct xfrm_type ipcomp6_type = 
460 {
461         .description    = "IPCOMP6",
462         .owner          = THIS_MODULE,
463         .proto          = IPPROTO_COMP,
464         .init_state     = ipcomp6_init_state,
465         .destructor     = ipcomp6_destroy,
466         .input          = ipcomp6_input,
467         .output         = ipcomp6_output,
468 };
469
470 static struct inet6_protocol ipcomp6_protocol = 
471 {
472         .handler        = xfrm6_rcv,
473         .err_handler    = ipcomp6_err,
474         .flags          = INET6_PROTO_NOPOLICY,
475 };
476
477 static int __init ipcomp6_init(void)
478 {
479         if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
480                 printk(KERN_INFO "ipcomp6 init: can't add xfrm type\n");
481                 return -EAGAIN;
482         }
483         if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
484                 printk(KERN_INFO "ipcomp6 init: can't add protocol\n");
485                 xfrm_unregister_type(&ipcomp6_type, AF_INET6);
486                 return -EAGAIN;
487         }
488         return 0;
489 }
490
491 static void __exit ipcomp6_fini(void)
492 {
493         if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) 
494                 printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol\n");
495         if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
496                 printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type\n");
497 }
498
499 module_init(ipcomp6_init);
500 module_exit(ipcomp6_fini);
501 MODULE_LICENSE("GPL");
502 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
503 MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
504
505