2 * IP Payload Compression Protocol (IPComp) - RFC3173.
4 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
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)
12 * - Tunable compression parameters.
13 * - Compression stats.
14 * - Adaptive compression.
16 #include <linux/module.h>
17 #include <linux/crypto.h>
18 #include <linux/err.h>
19 #include <linux/pfkeyv2.h>
20 #include <linux/percpu.h>
21 #include <linux/smp.h>
22 #include <linux/list.h>
23 #include <linux/vmalloc.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/mutex.h>
29 #include <net/ipcomp.h>
30 #include <net/protocol.h>
33 struct list_head list
;
34 struct crypto_comp
**tfms
;
38 static DEFINE_MUTEX(ipcomp_resource_mutex
);
39 static void **ipcomp_scratches
;
40 static int ipcomp_scratch_users
;
41 static LIST_HEAD(ipcomp_tfms_list
);
43 static int ipcomp_decompress(struct xfrm_state
*x
, struct sk_buff
*skb
)
45 struct ipcomp_data
*ipcd
= x
->data
;
46 const int plen
= skb
->len
;
47 int dlen
= IPCOMP_SCRATCH_SIZE
;
48 const u8
*start
= skb
->data
;
49 const int cpu
= get_cpu();
50 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
51 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
52 int err
= crypto_comp_decompress(tfm
, start
, plen
, scratch
, &dlen
);
57 if (dlen
< (plen
+ sizeof(struct ip_comp_hdr
))) {
62 err
= pskb_expand_head(skb
, 0, dlen
- plen
, GFP_ATOMIC
);
66 skb
->truesize
+= dlen
- plen
;
67 __skb_put(skb
, dlen
- plen
);
68 skb_copy_to_linear_data(skb
, scratch
, dlen
);
74 static int ipcomp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
78 struct ip_comp_hdr
*ipch
;
80 if (skb_linearize_cow(skb
))
83 skb
->ip_summed
= CHECKSUM_NONE
;
85 /* Remove ipcomp header and decompress original payload */
86 ipch
= (void *)skb
->data
;
87 nexthdr
= ipch
->nexthdr
;
89 skb
->transport_header
= skb
->network_header
+ sizeof(*ipch
);
90 __skb_pull(skb
, sizeof(*ipch
));
91 err
= ipcomp_decompress(x
, skb
);
101 static int ipcomp_compress(struct xfrm_state
*x
, struct sk_buff
*skb
)
103 struct ipcomp_data
*ipcd
= x
->data
;
104 const int plen
= skb
->len
;
105 int dlen
= IPCOMP_SCRATCH_SIZE
;
106 u8
*start
= skb
->data
;
107 const int cpu
= get_cpu();
108 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
109 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
113 err
= crypto_comp_compress(tfm
, start
, plen
, scratch
, &dlen
);
118 if ((dlen
+ sizeof(struct ip_comp_hdr
)) >= plen
) {
123 memcpy(start
+ sizeof(struct ip_comp_hdr
), scratch
, dlen
);
126 pskb_trim(skb
, dlen
+ sizeof(struct ip_comp_hdr
));
134 static int ipcomp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
137 struct ip_comp_hdr
*ipch
;
138 struct ipcomp_data
*ipcd
= x
->data
;
140 if (skb
->len
< ipcd
->threshold
) {
141 /* Don't bother compressing */
145 if (skb_linearize_cow(skb
))
148 err
= ipcomp_compress(x
, skb
);
154 /* Install ipcomp header, convert into ipcomp datagram. */
155 ipch
= ip_comp_hdr(skb
);
156 ipch
->nexthdr
= *skb_mac_header(skb
);
158 ipch
->cpi
= htons((u16
)ntohl(x
->id
.spi
));
159 *skb_mac_header(skb
) = IPPROTO_COMP
;
161 skb_push(skb
, -skb_network_offset(skb
));
165 static void ipcomp4_err(struct sk_buff
*skb
, u32 info
)
168 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
169 struct ip_comp_hdr
*ipch
= (struct ip_comp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
170 struct xfrm_state
*x
;
172 if (icmp_hdr(skb
)->type
!= ICMP_DEST_UNREACH
||
173 icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
176 spi
= htonl(ntohs(ipch
->cpi
));
177 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
,
178 spi
, IPPROTO_COMP
, AF_INET
);
181 NETDEBUG(KERN_DEBUG
"pmtu discovery on SA IPCOMP/%08x/" NIPQUAD_FMT
"\n",
182 spi
, NIPQUAD(iph
->daddr
));
186 /* We always hold one tunnel user reference to indicate a tunnel */
187 static struct xfrm_state
*ipcomp_tunnel_create(struct xfrm_state
*x
)
189 struct xfrm_state
*t
;
191 t
= xfrm_state_alloc();
195 t
->id
.proto
= IPPROTO_IPIP
;
196 t
->id
.spi
= x
->props
.saddr
.a4
;
197 t
->id
.daddr
.a4
= x
->id
.daddr
.a4
;
198 memcpy(&t
->sel
, &x
->sel
, sizeof(t
->sel
));
199 t
->props
.family
= AF_INET
;
200 t
->props
.mode
= x
->props
.mode
;
201 t
->props
.saddr
.a4
= x
->props
.saddr
.a4
;
202 t
->props
.flags
= x
->props
.flags
;
204 if (xfrm_init_state(t
))
207 atomic_set(&t
->tunnel_users
, 1);
212 t
->km
.state
= XFRM_STATE_DEAD
;
219 * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
220 * always incremented on success.
222 static int ipcomp_tunnel_attach(struct xfrm_state
*x
)
225 struct xfrm_state
*t
;
227 t
= xfrm_state_lookup((xfrm_address_t
*)&x
->id
.daddr
.a4
,
228 x
->props
.saddr
.a4
, IPPROTO_IPIP
, AF_INET
);
230 t
= ipcomp_tunnel_create(x
);
235 xfrm_state_insert(t
);
239 atomic_inc(&t
->tunnel_users
);
244 static void ipcomp_free_scratches(void)
249 if (--ipcomp_scratch_users
)
252 scratches
= ipcomp_scratches
;
256 for_each_possible_cpu(i
)
257 vfree(*per_cpu_ptr(scratches
, i
));
259 free_percpu(scratches
);
262 static void **ipcomp_alloc_scratches(void)
267 if (ipcomp_scratch_users
++)
268 return ipcomp_scratches
;
270 scratches
= alloc_percpu(void *);
274 ipcomp_scratches
= scratches
;
276 for_each_possible_cpu(i
) {
277 void *scratch
= vmalloc(IPCOMP_SCRATCH_SIZE
);
280 *per_cpu_ptr(scratches
, i
) = scratch
;
286 static void ipcomp_free_tfms(struct crypto_comp
**tfms
)
288 struct ipcomp_tfms
*pos
;
291 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
292 if (pos
->tfms
== tfms
)
301 list_del(&pos
->list
);
307 for_each_possible_cpu(cpu
) {
308 struct crypto_comp
*tfm
= *per_cpu_ptr(tfms
, cpu
);
309 crypto_free_comp(tfm
);
314 static struct crypto_comp
**ipcomp_alloc_tfms(const char *alg_name
)
316 struct ipcomp_tfms
*pos
;
317 struct crypto_comp
**tfms
;
320 /* This can be any valid CPU ID so we don't need locking. */
321 cpu
= raw_smp_processor_id();
323 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
324 struct crypto_comp
*tfm
;
327 tfm
= *per_cpu_ptr(tfms
, cpu
);
329 if (!strcmp(crypto_comp_name(tfm
), alg_name
)) {
335 pos
= kmalloc(sizeof(*pos
), GFP_KERNEL
);
340 INIT_LIST_HEAD(&pos
->list
);
341 list_add(&pos
->list
, &ipcomp_tfms_list
);
343 pos
->tfms
= tfms
= alloc_percpu(struct crypto_comp
*);
347 for_each_possible_cpu(cpu
) {
348 struct crypto_comp
*tfm
= crypto_alloc_comp(alg_name
, 0,
352 *per_cpu_ptr(tfms
, cpu
) = tfm
;
358 ipcomp_free_tfms(tfms
);
362 static void ipcomp_free_data(struct ipcomp_data
*ipcd
)
365 ipcomp_free_tfms(ipcd
->tfms
);
366 ipcomp_free_scratches();
369 static void ipcomp_destroy(struct xfrm_state
*x
)
371 struct ipcomp_data
*ipcd
= x
->data
;
374 xfrm_state_delete_tunnel(x
);
375 mutex_lock(&ipcomp_resource_mutex
);
376 ipcomp_free_data(ipcd
);
377 mutex_unlock(&ipcomp_resource_mutex
);
381 static int ipcomp_init_state(struct xfrm_state
*x
)
384 struct ipcomp_data
*ipcd
;
385 struct xfrm_algo_desc
*calg_desc
;
394 x
->props
.header_len
= 0;
395 switch (x
->props
.mode
) {
396 case XFRM_MODE_TRANSPORT
:
398 case XFRM_MODE_TUNNEL
:
399 x
->props
.header_len
+= sizeof(struct iphdr
);
406 ipcd
= kzalloc(sizeof(*ipcd
), GFP_KERNEL
);
410 mutex_lock(&ipcomp_resource_mutex
);
411 if (!ipcomp_alloc_scratches())
414 ipcd
->tfms
= ipcomp_alloc_tfms(x
->calg
->alg_name
);
417 mutex_unlock(&ipcomp_resource_mutex
);
419 if (x
->props
.mode
== XFRM_MODE_TUNNEL
) {
420 err
= ipcomp_tunnel_attach(x
);
425 calg_desc
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
427 ipcd
->threshold
= calg_desc
->uinfo
.comp
.threshold
;
434 mutex_lock(&ipcomp_resource_mutex
);
436 ipcomp_free_data(ipcd
);
437 mutex_unlock(&ipcomp_resource_mutex
);
442 static const struct xfrm_type ipcomp_type
= {
443 .description
= "IPCOMP4",
444 .owner
= THIS_MODULE
,
445 .proto
= IPPROTO_COMP
,
446 .init_state
= ipcomp_init_state
,
447 .destructor
= ipcomp_destroy
,
448 .input
= ipcomp_input
,
449 .output
= ipcomp_output
452 static struct net_protocol ipcomp4_protocol
= {
453 .handler
= xfrm4_rcv
,
454 .err_handler
= ipcomp4_err
,
458 static int __init
ipcomp4_init(void)
460 if (xfrm_register_type(&ipcomp_type
, AF_INET
) < 0) {
461 printk(KERN_INFO
"ipcomp init: can't add xfrm type\n");
464 if (inet_add_protocol(&ipcomp4_protocol
, IPPROTO_COMP
) < 0) {
465 printk(KERN_INFO
"ipcomp init: can't add protocol\n");
466 xfrm_unregister_type(&ipcomp_type
, AF_INET
);
472 static void __exit
ipcomp4_fini(void)
474 if (inet_del_protocol(&ipcomp4_protocol
, IPPROTO_COMP
) < 0)
475 printk(KERN_INFO
"ip ipcomp close: can't remove protocol\n");
476 if (xfrm_unregister_type(&ipcomp_type
, AF_INET
) < 0)
477 printk(KERN_INFO
"ip ipcomp close: can't remove xfrm type\n");
480 module_init(ipcomp4_init
);
481 module_exit(ipcomp4_fini
);
483 MODULE_LICENSE("GPL");
484 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
485 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
487 MODULE_ALIAS_XFRM_TYPE(AF_INET
, XFRM_PROTO_COMP
);