[NET]: Add netif_tx_lock
[deliverable/linux.git] / net / ipv6 / ipcomp6.c
CommitLineData
1da177e4
LT
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>
14c85021 50#include <net/protocol.h>
1da177e4
LT
51#include <linux/ipv6.h>
52#include <linux/icmpv6.h>
4a3e2f71 53#include <linux/mutex.h>
1da177e4
LT
54
55struct ipcomp6_tfms {
56 struct list_head list;
57 struct crypto_tfm **tfms;
58 int users;
59};
60
4a3e2f71 61static DEFINE_MUTEX(ipcomp6_resource_mutex);
1da177e4
LT
62static void **ipcomp6_scratches;
63static int ipcomp6_scratch_users;
64static LIST_HEAD(ipcomp6_tfms_list);
65
e695633e 66static int ipcomp6_input(struct xfrm_state *x, struct sk_buff *skb)
1da177e4
LT
67{
68 int err = 0;
1da177e4 69 struct ipv6hdr *iph;
31a4ab93 70 struct ipv6_comp_hdr *ipch;
1da177e4
LT
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;
31a4ab93
HX
87 ipch = (void *)skb->data;
88 skb->h.raw = skb->nh.raw + sizeof(*ipch);
89 __skb_pull(skb, sizeof(*ipch));
1da177e4
LT
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);
31a4ab93 118 err = ipch->nexthdr;
1da177e4 119
1da177e4
LT
120out_put_cpu:
121 put_cpu();
122out:
1da177e4
LT
123 return err;
124}
125
126static 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
179out_ok:
180 return 0;
181}
182
183static 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
4195f814 194 spi = htonl(ntohs(ipcomph->cpi));
1da177e4
LT
195 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
196 if (!x)
197 return;
198
46b86a2d 199 printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
1da177e4
LT
200 spi, NIP6(iph->daddr));
201 xfrm_state_put(x);
202}
203
204static 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);
6abaaaae
HX
214 if (!t->id.spi)
215 goto error;
216
1da177e4
LT
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
72cb6962 223 if (xfrm_init_state(t))
1da177e4
LT
224 goto error;
225
1da177e4
LT
226 atomic_set(&t->tunnel_users, 1);
227
228out:
229 return t;
230
231error:
6abaaaae 232 t->km.state = XFRM_STATE_DEAD;
1da177e4 233 xfrm_state_put(t);
6abaaaae 234 t = NULL;
1da177e4
LT
235 goto out;
236}
237
238static 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
260out:
261 return err;
262}
263
264static 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
6f912042 276 for_each_possible_cpu(i) {
1da177e4 277 void *scratch = *per_cpu_ptr(scratches, i);
2b191bef
JJ
278
279 vfree(scratch);
1da177e4
LT
280 }
281
282 free_percpu(scratches);
283}
284
285static 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
6f912042 299 for_each_possible_cpu(i) {
1da177e4
LT
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
309static 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
6f912042 330 for_each_possible_cpu(cpu) {
1da177e4 331 struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
573dbd95 332 crypto_free_tfm(tfm);
1da177e4
LT
333 }
334 free_percpu(tfms);
335}
336
337static 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. */
6fc8b9e7 344 cpu = raw_smp_processor_id();
1da177e4
LT
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
6f912042 370 for_each_possible_cpu(cpu) {
1da177e4
LT
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
379error:
380 ipcomp6_free_tfms(tfms);
381 return NULL;
382}
383
384static 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
391static 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);
4a3e2f71 397 mutex_lock(&ipcomp6_resource_mutex);
1da177e4 398 ipcomp6_free_data(ipcd);
4a3e2f71 399 mutex_unlock(&ipcomp6_resource_mutex);
1da177e4
LT
400 kfree(ipcd);
401
402 xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
403}
404
72cb6962 405static int ipcomp6_init_state(struct xfrm_state *x)
1da177e4
LT
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;
0c600eda 419 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
1da177e4
LT
420 if (!ipcd)
421 goto out;
422
1da177e4
LT
423 x->props.header_len = 0;
424 if (x->props.mode)
425 x->props.header_len += sizeof(struct ipv6hdr);
426
4a3e2f71 427 mutex_lock(&ipcomp6_resource_mutex);
1da177e4
LT
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;
4a3e2f71 434 mutex_unlock(&ipcomp6_resource_mutex);
1da177e4
LT
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;
447out:
448 return err;
449error_tunnel:
4a3e2f71 450 mutex_lock(&ipcomp6_resource_mutex);
1da177e4
LT
451error:
452 ipcomp6_free_data(ipcd);
4a3e2f71 453 mutex_unlock(&ipcomp6_resource_mutex);
1da177e4
LT
454 kfree(ipcd);
455
456 goto out;
457}
458
459static 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
470static struct inet6_protocol ipcomp6_protocol =
471{
472 .handler = xfrm6_rcv,
473 .err_handler = ipcomp6_err,
474 .flags = INET6_PROTO_NOPOLICY,
475};
476
477static 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
491static 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
499module_init(ipcomp6_init);
500module_exit(ipcomp6_fini);
501MODULE_LICENSE("GPL");
502MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
503MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
504
505
This page took 0.151535 seconds and 5 git commands to generate.