[NET] sem2mutex: net/
[linux-2.6.git] / 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 xfrm_decap_state *decap, struct sk_buff *skb)
67 {
68         int err = 0;
69         u8 nexthdr = 0;
70         int hdr_len = skb->h.raw - skb->nh.raw;
71         unsigned char *tmp_hdr = NULL;
72         struct ipv6hdr *iph;
73         int plen, dlen;
74         struct ipcomp_data *ipcd = x->data;
75         u8 *start, *scratch;
76         struct crypto_tfm *tfm;
77         int cpu;
78
79         if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
80                 skb_linearize(skb, GFP_ATOMIC) != 0) {
81                 err = -ENOMEM;
82                 goto out;
83         }
84
85         skb->ip_summed = CHECKSUM_NONE;
86
87         /* Remove ipcomp header and decompress original payload */
88         iph = skb->nh.ipv6h;
89         tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
90         if (!tmp_hdr)
91                 goto out;
92         memcpy(tmp_hdr, iph, hdr_len);
93         nexthdr = *(u8 *)skb->data;
94         skb_pull(skb, sizeof(struct ipv6_comp_hdr)); 
95         skb->nh.raw += sizeof(struct ipv6_comp_hdr);
96         memcpy(skb->nh.raw, tmp_hdr, hdr_len);
97         iph = skb->nh.ipv6h;
98         iph->payload_len = htons(ntohs(iph->payload_len) - sizeof(struct ipv6_comp_hdr));
99         skb->h.raw = skb->data;
100
101         /* decompression */
102         plen = skb->len;
103         dlen = IPCOMP_SCRATCH_SIZE;
104         start = skb->data;
105
106         cpu = get_cpu();
107         scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
108         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
109
110         err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
111         if (err) {
112                 err = -EINVAL;
113                 goto out_put_cpu;
114         }
115
116         if (dlen < (plen + sizeof(struct ipv6_comp_hdr))) {
117                 err = -EINVAL;
118                 goto out_put_cpu;
119         }
120
121         err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
122         if (err) {
123                 goto out_put_cpu;
124         }
125
126         skb_put(skb, dlen - plen);
127         memcpy(skb->data, scratch, dlen);
128
129         iph = skb->nh.ipv6h;
130         iph->payload_len = htons(skb->len);
131         
132 out_put_cpu:
133         put_cpu();
134 out:
135         kfree(tmp_hdr);
136         if (err)
137                 goto error_out;
138         return nexthdr;
139 error_out:
140         return err;
141 }
142
143 static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
144 {
145         int err;
146         struct ipv6hdr *top_iph;
147         int hdr_len;
148         struct ipv6_comp_hdr *ipch;
149         struct ipcomp_data *ipcd = x->data;
150         int plen, dlen;
151         u8 *start, *scratch;
152         struct crypto_tfm *tfm;
153         int cpu;
154
155         hdr_len = skb->h.raw - skb->data;
156
157         /* check whether datagram len is larger than threshold */
158         if ((skb->len - hdr_len) < ipcd->threshold) {
159                 goto out_ok;
160         }
161
162         if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
163                 skb_linearize(skb, GFP_ATOMIC) != 0) {
164                 goto out_ok;
165         }
166
167         /* compression */
168         plen = skb->len - hdr_len;
169         dlen = IPCOMP_SCRATCH_SIZE;
170         start = skb->h.raw;
171
172         cpu = get_cpu();
173         scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
174         tfm = *per_cpu_ptr(ipcd->tfms, cpu);
175
176         err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
177         if (err || (dlen + sizeof(struct ipv6_comp_hdr)) >= plen) {
178                 put_cpu();
179                 goto out_ok;
180         }
181         memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
182         put_cpu();
183         pskb_trim(skb, hdr_len + dlen + sizeof(struct ip_comp_hdr));
184
185         /* insert ipcomp header and replace datagram */
186         top_iph = (struct ipv6hdr *)skb->data;
187
188         top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
189
190         ipch = (struct ipv6_comp_hdr *)start;
191         ipch->nexthdr = *skb->nh.raw;
192         ipch->flags = 0;
193         ipch->cpi = htons((u16 )ntohl(x->id.spi));
194         *skb->nh.raw = IPPROTO_COMP;
195
196 out_ok:
197         return 0;
198 }
199
200 static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
201                                 int type, int code, int offset, __u32 info)
202 {
203         u32 spi;
204         struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
205         struct ipv6_comp_hdr *ipcomph = (struct ipv6_comp_hdr*)(skb->data+offset);
206         struct xfrm_state *x;
207
208         if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
209                 return;
210
211         spi = ntohl(ntohs(ipcomph->cpi));
212         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
213         if (!x)
214                 return;
215
216         printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
217                         spi, NIP6(iph->daddr));
218         xfrm_state_put(x);
219 }
220
221 static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
222 {
223         struct xfrm_state *t = NULL;
224
225         t = xfrm_state_alloc();
226         if (!t)
227                 goto out;
228
229         t->id.proto = IPPROTO_IPV6;
230         t->id.spi = xfrm6_tunnel_alloc_spi((xfrm_address_t *)&x->props.saddr);
231         memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
232         memcpy(&t->sel, &x->sel, sizeof(t->sel));
233         t->props.family = AF_INET6;
234         t->props.mode = 1;
235         memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
236
237         if (xfrm_init_state(t))
238                 goto error;
239
240         atomic_set(&t->tunnel_users, 1);
241
242 out:
243         return t;
244
245 error:
246         xfrm_state_put(t);
247         goto out;
248 }
249
250 static int ipcomp6_tunnel_attach(struct xfrm_state *x)
251 {
252         int err = 0;
253         struct xfrm_state *t = NULL;
254         u32 spi;
255
256         spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&x->props.saddr);
257         if (spi)
258                 t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr,
259                                               spi, IPPROTO_IPV6, AF_INET6);
260         if (!t) {
261                 t = ipcomp6_tunnel_create(x);
262                 if (!t) {
263                         err = -EINVAL;
264                         goto out;
265                 }
266                 xfrm_state_insert(t);
267                 xfrm_state_hold(t);
268         }
269         x->tunnel = t;
270         atomic_inc(&t->tunnel_users);
271
272 out:
273         return err;
274 }
275
276 static void ipcomp6_free_scratches(void)
277 {
278         int i;
279         void **scratches;
280
281         if (--ipcomp6_scratch_users)
282                 return;
283
284         scratches = ipcomp6_scratches;
285         if (!scratches)
286                 return;
287
288         for_each_cpu(i) {
289                 void *scratch = *per_cpu_ptr(scratches, i);
290
291                 vfree(scratch);
292         }
293
294         free_percpu(scratches);
295 }
296
297 static void **ipcomp6_alloc_scratches(void)
298 {
299         int i;
300         void **scratches;
301
302         if (ipcomp6_scratch_users++)
303                 return ipcomp6_scratches;
304
305         scratches = alloc_percpu(void *);
306         if (!scratches)
307                 return NULL;
308
309         ipcomp6_scratches = scratches;
310
311         for_each_cpu(i) {
312                 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
313                 if (!scratch)
314                         return NULL;
315                 *per_cpu_ptr(scratches, i) = scratch;
316         }
317
318         return scratches;
319 }
320
321 static void ipcomp6_free_tfms(struct crypto_tfm **tfms)
322 {
323         struct ipcomp6_tfms *pos;
324         int cpu;
325
326         list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
327                 if (pos->tfms == tfms)
328                         break;
329         }
330
331         BUG_TRAP(pos);
332
333         if (--pos->users)
334                 return;
335
336         list_del(&pos->list);
337         kfree(pos);
338
339         if (!tfms)
340                 return;
341
342         for_each_cpu(cpu) {
343                 struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
344                 crypto_free_tfm(tfm);
345         }
346         free_percpu(tfms);
347 }
348
349 static struct crypto_tfm **ipcomp6_alloc_tfms(const char *alg_name)
350 {
351         struct ipcomp6_tfms *pos;
352         struct crypto_tfm **tfms;
353         int cpu;
354
355         /* This can be any valid CPU ID so we don't need locking. */
356         cpu = raw_smp_processor_id();
357
358         list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
359                 struct crypto_tfm *tfm;
360
361                 tfms = pos->tfms;
362                 tfm = *per_cpu_ptr(tfms, cpu);
363
364                 if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
365                         pos->users++;
366                         return tfms;
367                 }
368         }
369
370         pos = kmalloc(sizeof(*pos), GFP_KERNEL);
371         if (!pos)
372                 return NULL;
373
374         pos->users = 1;
375         INIT_LIST_HEAD(&pos->list);
376         list_add(&pos->list, &ipcomp6_tfms_list);
377
378         pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
379         if (!tfms)
380                 goto error;
381
382         for_each_cpu(cpu) {
383                 struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
384                 if (!tfm)
385                         goto error;
386                 *per_cpu_ptr(tfms, cpu) = tfm;
387         }
388
389         return tfms;
390
391 error:
392         ipcomp6_free_tfms(tfms);
393         return NULL;
394 }
395
396 static void ipcomp6_free_data(struct ipcomp_data *ipcd)
397 {
398         if (ipcd->tfms)
399                 ipcomp6_free_tfms(ipcd->tfms);
400         ipcomp6_free_scratches();
401 }
402
403 static void ipcomp6_destroy(struct xfrm_state *x)
404 {
405         struct ipcomp_data *ipcd = x->data;
406         if (!ipcd)
407                 return;
408         xfrm_state_delete_tunnel(x);
409         mutex_lock(&ipcomp6_resource_mutex);
410         ipcomp6_free_data(ipcd);
411         mutex_unlock(&ipcomp6_resource_mutex);
412         kfree(ipcd);
413
414         xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
415 }
416
417 static int ipcomp6_init_state(struct xfrm_state *x)
418 {
419         int err;
420         struct ipcomp_data *ipcd;
421         struct xfrm_algo_desc *calg_desc;
422
423         err = -EINVAL;
424         if (!x->calg)
425                 goto out;
426
427         if (x->encap)
428                 goto out;
429
430         err = -ENOMEM;
431         ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
432         if (!ipcd)
433                 goto out;
434
435         memset(ipcd, 0, sizeof(*ipcd));
436         x->props.header_len = 0;
437         if (x->props.mode)
438                 x->props.header_len += sizeof(struct ipv6hdr);
439         
440         mutex_lock(&ipcomp6_resource_mutex);
441         if (!ipcomp6_alloc_scratches())
442                 goto error;
443
444         ipcd->tfms = ipcomp6_alloc_tfms(x->calg->alg_name);
445         if (!ipcd->tfms)
446                 goto error;
447         mutex_unlock(&ipcomp6_resource_mutex);
448
449         if (x->props.mode) {
450                 err = ipcomp6_tunnel_attach(x);
451                 if (err)
452                         goto error_tunnel;
453         }
454
455         calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
456         BUG_ON(!calg_desc);
457         ipcd->threshold = calg_desc->uinfo.comp.threshold;
458         x->data = ipcd;
459         err = 0;
460 out:
461         return err;
462 error_tunnel:
463         mutex_lock(&ipcomp6_resource_mutex);
464 error:
465         ipcomp6_free_data(ipcd);
466         mutex_unlock(&ipcomp6_resource_mutex);
467         kfree(ipcd);
468
469         goto out;
470 }
471
472 static struct xfrm_type ipcomp6_type = 
473 {
474         .description    = "IPCOMP6",
475         .owner          = THIS_MODULE,
476         .proto          = IPPROTO_COMP,
477         .init_state     = ipcomp6_init_state,
478         .destructor     = ipcomp6_destroy,
479         .input          = ipcomp6_input,
480         .output         = ipcomp6_output,
481 };
482
483 static struct inet6_protocol ipcomp6_protocol = 
484 {
485         .handler        = xfrm6_rcv,
486         .err_handler    = ipcomp6_err,
487         .flags          = INET6_PROTO_NOPOLICY,
488 };
489
490 static int __init ipcomp6_init(void)
491 {
492         if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
493                 printk(KERN_INFO "ipcomp6 init: can't add xfrm type\n");
494                 return -EAGAIN;
495         }
496         if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
497                 printk(KERN_INFO "ipcomp6 init: can't add protocol\n");
498                 xfrm_unregister_type(&ipcomp6_type, AF_INET6);
499                 return -EAGAIN;
500         }
501         return 0;
502 }
503
504 static void __exit ipcomp6_fini(void)
505 {
506         if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) 
507                 printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol\n");
508         if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
509                 printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type\n");
510 }
511
512 module_init(ipcomp6_init);
513 module_exit(ipcomp6_fini);
514 MODULE_LICENSE("GPL");
515 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
516 MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
517
518