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>
24 #include <net/ipcomp.h>
26 static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
30 struct ipcomp_data *ipcd = x->data;
31 u8 *start, *scratch = ipcd->scratch;
34 dlen = IPCOMP_SCRATCH_SIZE;
37 err = crypto_comp_decompress(ipcd->tfm, start, plen, scratch, &dlen);
41 if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
46 err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
50 skb_put(skb, dlen - plen);
51 memcpy(skb->data, scratch, dlen);
53 iph->tot_len = htons(dlen + iph->ihl * 4);
58 static int ipcomp_input(struct xfrm_state *x,
59 struct xfrm_decap_state *decap, struct sk_buff *skb)
70 if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
71 skb_linearize(skb, GFP_ATOMIC) != 0) {
76 skb->ip_summed = CHECKSUM_NONE;
78 /* Remove ipcomp header and decompress original payload */
80 memcpy(&tmp_iph, iph, iph->ihl * 4);
81 nexthdr = *(u8 *)skb->data;
82 skb_pull(skb, sizeof(struct ip_comp_hdr));
83 skb->nh.raw += sizeof(struct ip_comp_hdr);
84 memcpy(skb->nh.raw, &tmp_iph, tmp_iph.iph.ihl * 4);
86 iph->tot_len = htons(ntohs(iph->tot_len) - sizeof(struct ip_comp_hdr));
87 iph->protocol = nexthdr;
88 skb->h.raw = skb->data;
89 err = ipcomp_decompress(x, skb);
95 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
97 int err, plen, dlen, ihlen;
98 struct iphdr *iph = skb->nh.iph;
99 struct ipcomp_data *ipcd = x->data;
100 u8 *start, *scratch = ipcd->scratch;
102 ihlen = iph->ihl * 4;
103 plen = skb->len - ihlen;
104 dlen = IPCOMP_SCRATCH_SIZE;
105 start = skb->data + ihlen;
107 err = crypto_comp_compress(ipcd->tfm, start, plen, scratch, &dlen);
111 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
116 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
117 pskb_trim(skb, ihlen + dlen + sizeof(struct ip_comp_hdr));
123 static int ipcomp_output(struct sk_buff **pskb)
126 struct dst_entry *dst = (*pskb)->dst;
127 struct xfrm_state *x = dst->xfrm;
129 struct ip_comp_hdr *ipch;
130 struct ipcomp_data *ipcd = x->data;
133 iph = (*pskb)->nh.iph;
134 iph->tot_len = htons((*pskb)->len);
135 hdr_len = iph->ihl * 4;
136 if (((*pskb)->len - hdr_len) < ipcd->threshold) {
137 /* Don't bother compressing */
144 if ((skb_is_nonlinear(*pskb) || skb_cloned(*pskb)) &&
145 skb_linearize(*pskb, GFP_ATOMIC) != 0) {
150 err = ipcomp_compress(x, *pskb);
152 if (err == -EMSGSIZE) {
154 iph = (*pskb)->nh.iph;
162 /* Install ipcomp header, convert into ipcomp datagram. */
163 iph = (*pskb)->nh.iph;
164 iph->tot_len = htons((*pskb)->len);
165 ipch = (struct ip_comp_hdr *)((char *)iph + iph->ihl * 4);
166 ipch->nexthdr = iph->protocol;
168 ipch->cpi = htons((u16 )ntohl(x->id.spi));
169 iph->protocol = IPPROTO_COMP;
179 static void ipcomp4_err(struct sk_buff *skb, u32 info)
182 struct iphdr *iph = (struct iphdr *)skb->data;
183 struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
184 struct xfrm_state *x;
186 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
187 skb->h.icmph->code != ICMP_FRAG_NEEDED)
190 spi = ntohl(ntohs(ipch->cpi));
191 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr,
192 spi, IPPROTO_COMP, AF_INET);
195 NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
196 spi, NIPQUAD(iph->daddr)));
200 /* We always hold one tunnel user reference to indicate a tunnel */
201 static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
203 struct xfrm_state *t;
205 t = xfrm_state_alloc();
209 t->id.proto = IPPROTO_IPIP;
210 t->id.spi = x->props.saddr.a4;
211 t->id.daddr.a4 = x->id.daddr.a4;
212 memcpy(&t->sel, &x->sel, sizeof(t->sel));
213 t->props.family = AF_INET;
215 t->props.saddr.a4 = x->props.saddr.a4;
216 t->props.flags = x->props.flags;
218 t->type = xfrm_get_type(IPPROTO_IPIP, t->props.family);
222 if (t->type->init_state(t, NULL))
225 t->km.state = XFRM_STATE_VALID;
226 atomic_set(&t->tunnel_users, 1);
231 t->km.state = XFRM_STATE_DEAD;
238 * Must be protected by xfrm_cfg_sem. State and tunnel user references are
239 * always incremented on success.
241 static int ipcomp_tunnel_attach(struct xfrm_state *x)
244 struct xfrm_state *t;
246 t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
247 x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
249 t = ipcomp_tunnel_create(x);
254 xfrm_state_insert(t);
258 atomic_inc(&t->tunnel_users);
263 static void ipcomp_free_data(struct ipcomp_data *ipcd)
266 crypto_free_tfm(ipcd->tfm);
268 kfree(ipcd->scratch);
271 static void ipcomp_destroy(struct xfrm_state *x)
273 struct ipcomp_data *ipcd = x->data;
276 xfrm_state_delete_tunnel(x);
277 ipcomp_free_data(ipcd);
281 static int ipcomp_init_state(struct xfrm_state *x, void *args)
284 struct ipcomp_data *ipcd;
285 struct xfrm_algo_desc *calg_desc;
292 ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
296 memset(ipcd, 0, sizeof(*ipcd));
297 x->props.header_len = 0;
299 x->props.header_len += sizeof(struct iphdr);
301 ipcd->scratch = kmalloc(IPCOMP_SCRATCH_SIZE, GFP_KERNEL);
305 ipcd->tfm = crypto_alloc_tfm(x->calg->alg_name, 0);
310 err = ipcomp_tunnel_attach(x);
315 calg_desc = xfrm_calg_get_byname(x->calg->alg_name);
317 ipcd->threshold = calg_desc->uinfo.comp.threshold;
325 ipcomp_free_data(ipcd);
331 static struct xfrm_type ipcomp_type = {
332 .description = "IPCOMP4",
333 .owner = THIS_MODULE,
334 .proto = IPPROTO_COMP,
335 .init_state = ipcomp_init_state,
336 .destructor = ipcomp_destroy,
337 .input = ipcomp_input,
338 .output = ipcomp_output
341 static struct net_protocol ipcomp4_protocol = {
342 .handler = xfrm4_rcv,
343 .err_handler = ipcomp4_err,
347 static int __init ipcomp4_init(void)
349 if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
350 printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
353 if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
354 printk(KERN_INFO "ipcomp init: can't add protocol\n");
355 xfrm_unregister_type(&ipcomp_type, AF_INET);
361 static void __exit ipcomp4_fini(void)
363 if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
364 printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
365 if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
366 printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
369 module_init(ipcomp4_init);
370 module_exit(ipcomp4_fini);
372 MODULE_LICENSE("GPL");
373 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
374 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");