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/config.h>
17 #include <linux/module.h>
18 #include <asm/scatterlist.h>
19 #include <linux/crypto.h>
20 #include <linux/pfkeyv2.h>
21 #include <net/inet_ecn.h>
25 #include <net/ipcomp.h>
27 static int ipcomp_decompress(struct xfrm_state
*x
, struct sk_buff
*skb
)
31 struct ipcomp_data
*ipcd
= x
->data
;
32 u8
*start
, *scratch
= ipcd
->scratch
;
35 dlen
= IPCOMP_SCRATCH_SIZE
;
38 err
= crypto_comp_decompress(ipcd
->tfm
, start
, plen
, scratch
, &dlen
);
42 if (dlen
< (plen
+ sizeof(struct ip_comp_hdr
))) {
47 err
= pskb_expand_head(skb
, 0, dlen
- plen
, GFP_ATOMIC
);
51 skb_put(skb
, dlen
- plen
);
52 memcpy(skb
->data
, scratch
, dlen
);
54 iph
->tot_len
= htons(dlen
+ iph
->ihl
* 4);
59 static int ipcomp_input(struct xfrm_state
*x
,
60 struct xfrm_decap_state
*decap
, struct sk_buff
*skb
)
71 if ((skb_is_nonlinear(skb
) || skb_cloned(skb
)) &&
72 skb_linearize(skb
, GFP_ATOMIC
) != 0) {
77 skb
->ip_summed
= CHECKSUM_NONE
;
79 /* Remove ipcomp header and decompress original payload */
81 memcpy(&tmp_iph
, iph
, iph
->ihl
* 4);
82 nexthdr
= *(u8
*)skb
->data
;
83 skb_pull(skb
, sizeof(struct ip_comp_hdr
));
84 skb
->nh
.raw
+= sizeof(struct ip_comp_hdr
);
85 memcpy(skb
->nh
.raw
, &tmp_iph
, tmp_iph
.iph
.ihl
* 4);
87 iph
->tot_len
= htons(ntohs(iph
->tot_len
) - sizeof(struct ip_comp_hdr
));
88 iph
->protocol
= nexthdr
;
89 skb
->h
.raw
= skb
->data
;
90 err
= ipcomp_decompress(x
, skb
);
96 static int ipcomp_compress(struct xfrm_state
*x
, struct sk_buff
*skb
)
98 int err
, plen
, dlen
, ihlen
;
99 struct iphdr
*iph
= skb
->nh
.iph
;
100 struct ipcomp_data
*ipcd
= x
->data
;
101 u8
*start
, *scratch
= ipcd
->scratch
;
103 ihlen
= iph
->ihl
* 4;
104 plen
= skb
->len
- ihlen
;
105 dlen
= IPCOMP_SCRATCH_SIZE
;
106 start
= skb
->data
+ ihlen
;
108 err
= crypto_comp_compress(ipcd
->tfm
, start
, plen
, scratch
, &dlen
);
112 if ((dlen
+ sizeof(struct ip_comp_hdr
)) >= plen
) {
117 memcpy(start
, scratch
, dlen
);
118 pskb_trim(skb
, ihlen
+ dlen
);
124 static void ipcomp_tunnel_encap(struct xfrm_state
*x
, struct sk_buff
*skb
)
126 struct dst_entry
*dst
= skb
->dst
;
127 struct iphdr
*iph
, *top_iph
;
130 top_iph
= (struct iphdr
*)skb_push(skb
, sizeof(struct iphdr
));
132 top_iph
->version
= 4;
133 top_iph
->tos
= iph
->tos
;
134 top_iph
->tot_len
= htons(skb
->len
);
135 if (!(iph
->frag_off
&htons(IP_DF
)))
136 __ip_select_ident(top_iph
, dst
, 0);
137 top_iph
->ttl
= iph
->ttl
;
139 top_iph
->saddr
= x
->props
.saddr
.a4
;
140 top_iph
->daddr
= x
->id
.daddr
.a4
;
141 top_iph
->frag_off
= iph
->frag_off
&~htons(IP_MF
|IP_OFFSET
);
142 memset(&(IPCB(skb
)->opt
), 0, sizeof(struct ip_options
));
143 skb
->nh
.raw
= skb
->data
;
146 static int ipcomp_output(struct sk_buff
*skb
)
149 struct dst_entry
*dst
= skb
->dst
;
150 struct xfrm_state
*x
= dst
->xfrm
;
151 struct iphdr
*iph
, *top_iph
;
152 struct ip_comp_hdr
*ipch
;
153 struct ipcomp_data
*ipcd
= x
->data
;
160 if (skb
->ip_summed
== CHECKSUM_HW
&& skb_checksum_help(skb
) == NULL
) {
165 spin_lock_bh(&x
->lock
);
166 err
= xfrm_check_output(x
, skb
, AF_INET
);
170 /* Don't bother compressing */
171 if (!x
->props
.mode
) {
173 hdr_len
= iph
->ihl
* 4;
175 if ((skb
->len
- hdr_len
) < ipcd
->threshold
) {
177 ipcomp_tunnel_encap(x
, skb
);
179 iph
->protocol
= IPPROTO_IPIP
;
186 ipcomp_tunnel_encap(x
, skb
);
188 if ((skb_is_nonlinear(skb
) || skb_cloned(skb
)) &&
189 skb_linearize(skb
, GFP_ATOMIC
) != 0) {
194 err
= ipcomp_compress(x
, skb
);
196 if (err
== -EMSGSIZE
) {
199 iph
->protocol
= IPPROTO_IPIP
;
207 /* Install ipcomp header, convert into ipcomp datagram. */
209 memcpy(&tmp_iph
, iph
, iph
->ihl
* 4);
210 top_iph
= (struct iphdr
*)skb_push(skb
, sizeof(struct ip_comp_hdr
));
211 memcpy(top_iph
, &tmp_iph
, iph
->ihl
* 4);
213 if (x
->props
.mode
&& (x
->props
.flags
& XFRM_STATE_NOECN
))
215 iph
->tot_len
= htons(skb
->len
);
216 iph
->protocol
= IPPROTO_COMP
;
218 ipch
= (struct ip_comp_hdr
*)((char *)iph
+ iph
->ihl
* 4);
219 ipch
->nexthdr
= x
->props
.mode
? IPPROTO_IPIP
: tmp_iph
.iph
.protocol
;
221 ipch
->cpi
= htons((u16
)ntohl(x
->id
.spi
));
223 skb
->nh
.raw
= skb
->data
;
226 x
->curlft
.bytes
+= skb
->len
;
228 spin_unlock_bh(&x
->lock
);
230 if ((skb
->dst
= dst_pop(dst
)) == NULL
) {
234 err
= NET_XMIT_BYPASS
;
239 spin_unlock_bh(&x
->lock
);
245 static void ipcomp4_err(struct sk_buff
*skb
, u32 info
)
248 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
249 struct ip_comp_hdr
*ipch
= (struct ip_comp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
250 struct xfrm_state
*x
;
252 if (skb
->h
.icmph
->type
!= ICMP_DEST_UNREACH
||
253 skb
->h
.icmph
->code
!= ICMP_FRAG_NEEDED
)
256 spi
= ntohl(ntohs(ipch
->cpi
));
257 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
,
258 spi
, IPPROTO_COMP
, AF_INET
);
261 NETDEBUG(printk(KERN_DEBUG
"pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
262 spi
, NIPQUAD(iph
->daddr
)));
266 /* We always hold one tunnel user reference to indicate a tunnel */
267 static struct xfrm_state
*ipcomp_tunnel_create(struct xfrm_state
*x
)
269 struct xfrm_state
*t
;
271 t
= xfrm_state_alloc();
275 t
->id
.proto
= IPPROTO_IPIP
;
276 t
->id
.spi
= x
->props
.saddr
.a4
;
277 t
->id
.daddr
.a4
= x
->id
.daddr
.a4
;
278 memcpy(&t
->sel
, &x
->sel
, sizeof(t
->sel
));
279 t
->props
.family
= AF_INET
;
281 t
->props
.saddr
.a4
= x
->props
.saddr
.a4
;
282 t
->props
.flags
= x
->props
.flags
;
284 t
->type
= xfrm_get_type(IPPROTO_IPIP
, t
->props
.family
);
288 if (t
->type
->init_state(t
, NULL
))
291 t
->km
.state
= XFRM_STATE_VALID
;
292 atomic_set(&t
->tunnel_users
, 1);
297 t
->km
.state
= XFRM_STATE_DEAD
;
304 * Must be protected by xfrm_cfg_sem. State and tunnel user references are
305 * always incremented on success.
307 static int ipcomp_tunnel_attach(struct xfrm_state
*x
)
310 struct xfrm_state
*t
;
312 t
= xfrm_state_lookup((xfrm_address_t
*)&x
->id
.daddr
.a4
,
313 x
->props
.saddr
.a4
, IPPROTO_IPIP
, AF_INET
);
315 t
= ipcomp_tunnel_create(x
);
320 xfrm_state_insert(t
);
324 atomic_inc(&t
->tunnel_users
);
329 static void ipcomp_free_data(struct ipcomp_data
*ipcd
)
332 crypto_free_tfm(ipcd
->tfm
);
334 kfree(ipcd
->scratch
);
337 static void ipcomp_destroy(struct xfrm_state
*x
)
339 struct ipcomp_data
*ipcd
= x
->data
;
342 ipcomp_free_data(ipcd
);
346 static int ipcomp_init_state(struct xfrm_state
*x
, void *args
)
349 struct ipcomp_data
*ipcd
;
350 struct xfrm_algo_desc
*calg_desc
;
357 ipcd
= kmalloc(sizeof(*ipcd
), GFP_KERNEL
);
361 memset(ipcd
, 0, sizeof(*ipcd
));
362 x
->props
.header_len
= sizeof(struct ip_comp_hdr
);
364 x
->props
.header_len
+= sizeof(struct iphdr
);
366 ipcd
->scratch
= kmalloc(IPCOMP_SCRATCH_SIZE
, GFP_KERNEL
);
370 ipcd
->tfm
= crypto_alloc_tfm(x
->calg
->alg_name
, 0);
375 err
= ipcomp_tunnel_attach(x
);
380 calg_desc
= xfrm_calg_get_byname(x
->calg
->alg_name
);
382 ipcd
->threshold
= calg_desc
->uinfo
.comp
.threshold
;
390 ipcomp_free_data(ipcd
);
396 static struct xfrm_type ipcomp_type
= {
397 .description
= "IPCOMP4",
398 .owner
= THIS_MODULE
,
399 .proto
= IPPROTO_COMP
,
400 .init_state
= ipcomp_init_state
,
401 .destructor
= ipcomp_destroy
,
402 .input
= ipcomp_input
,
403 .output
= ipcomp_output
406 static struct inet_protocol ipcomp4_protocol
= {
407 .handler
= xfrm4_rcv
,
408 .err_handler
= ipcomp4_err
,
412 static int __init
ipcomp4_init(void)
414 if (xfrm_register_type(&ipcomp_type
, AF_INET
) < 0) {
415 printk(KERN_INFO
"ipcomp init: can't add xfrm type\n");
418 if (inet_add_protocol(&ipcomp4_protocol
, IPPROTO_COMP
) < 0) {
419 printk(KERN_INFO
"ipcomp init: can't add protocol\n");
420 xfrm_unregister_type(&ipcomp_type
, AF_INET
);
426 static void __exit
ipcomp4_fini(void)
428 if (inet_del_protocol(&ipcomp4_protocol
, IPPROTO_COMP
) < 0)
429 printk(KERN_INFO
"ip ipcomp close: can't remove protocol\n");
430 if (xfrm_unregister_type(&ipcomp_type
, AF_INET
) < 0)
431 printk(KERN_INFO
"ip ipcomp close: can't remove xfrm type\n");
434 module_init(ipcomp4_init
);
435 module_exit(ipcomp4_fini
);
437 MODULE_LICENSE("GPL");
438 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
439 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");