blob: cec3be544b69b23431fec2bbfb8981f95d75dc05 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020050#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/ipv6.h>
52#include <linux/icmpv6.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080053#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55struct ipcomp6_tfms {
56 struct list_head list;
57 struct crypto_tfm **tfms;
58 int users;
59};
60
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080061static DEFINE_MUTEX(ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void **ipcomp6_scratches;
63static int ipcomp6_scratch_users;
64static LIST_HEAD(ipcomp6_tfms_list);
65
Herbert Xue6956332006-04-01 00:52:46 -080066static int ipcomp6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct ipv6hdr *iph;
Herbert Xu31a4ab92006-05-27 23:06:13 -070070 struct ipv6_comp_hdr *ipch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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;
Herbert Xu31a4ab92006-05-27 23:06:13 -070087 ipch = (void *)skb->data;
88 skb->h.raw = skb->nh.raw + sizeof(*ipch);
89 __skb_pull(skb, sizeof(*ipch));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
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);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700118 err = ipch->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120out_put_cpu:
121 put_cpu();
122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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
Alexey Dobriyan4195f812006-05-22 16:53:22 -0700194 spi = htonl(ntohs(ipcomph->cpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
196 if (!x)
197 return;
198
Joe Perches46b86a22006-01-13 14:29:07 -0800199 printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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);
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800214 if (!t->id.spi)
215 goto error;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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
Herbert Xu72cb6962005-06-20 13:18:08 -0700223 if (xfrm_init_state(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto error;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 atomic_set(&t->tunnel_users, 1);
227
228out:
229 return t;
230
231error:
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800232 t->km.state = XFRM_STATE_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfrm_state_put(t);
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800234 t = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 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
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700276 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 void *scratch = *per_cpu_ptr(scratches, i);
Jesper Juhl2b191be2006-03-20 17:46:29 -0800278
279 vfree(scratch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
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
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700299 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 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
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700330 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700332 crypto_free_tfm(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
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. */
Herbert Xu6fc8b9e2005-08-18 14:36:59 -0700344 cpu = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
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
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700370 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 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);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800397 mutex_lock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ipcomp6_free_data(ipcd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800399 mutex_unlock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(ipcd);
401
402 xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
403}
404
Herbert Xu72cb6962005-06-20 13:18:08 -0700405static int ipcomp6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
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;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800419 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!ipcd)
421 goto out;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 x->props.header_len = 0;
424 if (x->props.mode)
425 x->props.header_len += sizeof(struct ipv6hdr);
426
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800427 mutex_lock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 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;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800434 mutex_unlock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
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:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800450 mutex_lock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451error:
452 ipcomp6_free_data(ipcd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800453 mutex_unlock(&ipcomp6_resource_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 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