1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IP Payload Compression Protocol (IPComp) - RFC3173.
5 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
9 * - Tunable compression parameters.
10 * - Compression stats.
11 * - Adaptive compression.
14 #include <linux/crypto.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/percpu.h>
20 #include <linux/slab.h>
21 #include <linux/smp.h>
22 #include <linux/vmalloc.h>
24 #include <net/ipcomp.h>
28 struct list_head list
;
29 struct crypto_comp
* __percpu
*tfms
;
33 static DEFINE_MUTEX(ipcomp_resource_mutex
);
34 static void * __percpu
*ipcomp_scratches
;
35 static int ipcomp_scratch_users
;
36 static LIST_HEAD(ipcomp_tfms_list
);
38 static int ipcomp_decompress(struct xfrm_state
*x
, struct sk_buff
*skb
)
40 struct ipcomp_data
*ipcd
= x
->data
;
41 const int plen
= skb
->len
;
42 int dlen
= IPCOMP_SCRATCH_SIZE
;
43 const u8
*start
= skb
->data
;
44 const int cpu
= get_cpu();
45 u8
*scratch
= *per_cpu_ptr(ipcomp_scratches
, cpu
);
46 struct crypto_comp
*tfm
= *per_cpu_ptr(ipcd
->tfms
, cpu
);
47 int err
= crypto_comp_decompress(tfm
, start
, plen
, scratch
, &dlen
);
53 if (dlen
< (plen
+ sizeof(struct ip_comp_hdr
))) {
59 if (len
> skb_tailroom(skb
))
60 len
= skb_tailroom(skb
);
65 skb_copy_to_linear_data(skb
, scratch
, len
);
67 while ((scratch
+= len
, dlen
-= len
) > 0) {
72 if (WARN_ON(skb_shinfo(skb
)->nr_frags
>= MAX_SKB_FRAGS
))
75 frag
= skb_shinfo(skb
)->frags
+ skb_shinfo(skb
)->nr_frags
;
76 page
= alloc_page(GFP_ATOMIC
);
82 __skb_frag_set_page(frag
, page
);
88 skb_frag_off_set(frag
, 0);
89 skb_frag_size_set(frag
, len
);
90 memcpy(skb_frag_address(frag
), scratch
, len
);
96 skb_shinfo(skb
)->nr_frags
++;
106 int ipcomp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
110 struct ip_comp_hdr
*ipch
;
112 if (skb_linearize_cow(skb
))
115 skb
->ip_summed
= CHECKSUM_NONE
;
117 /* Remove ipcomp header and decompress original payload */
118 ipch
= (void *)skb
->data
;
119 nexthdr
= ipch
->nexthdr
;
121 skb
->transport_header
= skb
->network_header
+ sizeof(*ipch
);
122 __skb_pull(skb
, sizeof(*ipch
));
123 err
= ipcomp_decompress(x
, skb
);
132 EXPORT_SYMBOL_GPL(ipcomp_input
);
134 static int ipcomp_compress(struct xfrm_state
*x
, struct sk_buff
*skb
)
136 struct ipcomp_data
*ipcd
= x
->data
;
137 const int plen
= skb
->len
;
138 int dlen
= IPCOMP_SCRATCH_SIZE
;
139 u8
*start
= skb
->data
;
140 struct crypto_comp
*tfm
;
145 scratch
= *this_cpu_ptr(ipcomp_scratches
);
146 tfm
= *this_cpu_ptr(ipcd
->tfms
);
147 err
= crypto_comp_compress(tfm
, start
, plen
, scratch
, &dlen
);
151 if ((dlen
+ sizeof(struct ip_comp_hdr
)) >= plen
) {
156 memcpy(start
+ sizeof(struct ip_comp_hdr
), scratch
, dlen
);
159 pskb_trim(skb
, dlen
+ sizeof(struct ip_comp_hdr
));
167 int ipcomp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
170 struct ip_comp_hdr
*ipch
;
171 struct ipcomp_data
*ipcd
= x
->data
;
173 if (skb
->len
< ipcd
->threshold
) {
174 /* Don't bother compressing */
178 if (skb_linearize_cow(skb
))
181 err
= ipcomp_compress(x
, skb
);
187 /* Install ipcomp header, convert into ipcomp datagram. */
188 ipch
= ip_comp_hdr(skb
);
189 ipch
->nexthdr
= *skb_mac_header(skb
);
191 ipch
->cpi
= htons((u16
)ntohl(x
->id
.spi
));
192 *skb_mac_header(skb
) = IPPROTO_COMP
;
194 skb_push(skb
, -skb_network_offset(skb
));
197 EXPORT_SYMBOL_GPL(ipcomp_output
);
199 static void ipcomp_free_scratches(void)
202 void * __percpu
*scratches
;
204 if (--ipcomp_scratch_users
)
207 scratches
= ipcomp_scratches
;
211 for_each_possible_cpu(i
)
212 vfree(*per_cpu_ptr(scratches
, i
));
214 free_percpu(scratches
);
217 static void * __percpu
*ipcomp_alloc_scratches(void)
219 void * __percpu
*scratches
;
222 if (ipcomp_scratch_users
++)
223 return ipcomp_scratches
;
225 scratches
= alloc_percpu(void *);
229 ipcomp_scratches
= scratches
;
231 for_each_possible_cpu(i
) {
234 scratch
= vmalloc_node(IPCOMP_SCRATCH_SIZE
, cpu_to_node(i
));
237 *per_cpu_ptr(scratches
, i
) = scratch
;
243 static void ipcomp_free_tfms(struct crypto_comp
* __percpu
*tfms
)
245 struct ipcomp_tfms
*pos
;
248 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
249 if (pos
->tfms
== tfms
)
258 list_del(&pos
->list
);
264 for_each_possible_cpu(cpu
) {
265 struct crypto_comp
*tfm
= *per_cpu_ptr(tfms
, cpu
);
266 crypto_free_comp(tfm
);
271 static struct crypto_comp
* __percpu
*ipcomp_alloc_tfms(const char *alg_name
)
273 struct ipcomp_tfms
*pos
;
274 struct crypto_comp
* __percpu
*tfms
;
278 list_for_each_entry(pos
, &ipcomp_tfms_list
, list
) {
279 struct crypto_comp
*tfm
;
281 /* This can be any valid CPU ID so we don't need locking. */
282 tfm
= this_cpu_read(*pos
->tfms
);
284 if (!strcmp(crypto_comp_name(tfm
), alg_name
)) {
290 pos
= kmalloc(sizeof(*pos
), GFP_KERNEL
);
295 INIT_LIST_HEAD(&pos
->list
);
296 list_add(&pos
->list
, &ipcomp_tfms_list
);
298 pos
->tfms
= tfms
= alloc_percpu(struct crypto_comp
*);
302 for_each_possible_cpu(cpu
) {
303 struct crypto_comp
*tfm
= crypto_alloc_comp(alg_name
, 0,
307 *per_cpu_ptr(tfms
, cpu
) = tfm
;
313 ipcomp_free_tfms(tfms
);
317 static void ipcomp_free_data(struct ipcomp_data
*ipcd
)
320 ipcomp_free_tfms(ipcd
->tfms
);
321 ipcomp_free_scratches();
324 void ipcomp_destroy(struct xfrm_state
*x
)
326 struct ipcomp_data
*ipcd
= x
->data
;
329 xfrm_state_delete_tunnel(x
);
330 mutex_lock(&ipcomp_resource_mutex
);
331 ipcomp_free_data(ipcd
);
332 mutex_unlock(&ipcomp_resource_mutex
);
335 EXPORT_SYMBOL_GPL(ipcomp_destroy
);
337 int ipcomp_init_state(struct xfrm_state
*x
)
340 struct ipcomp_data
*ipcd
;
341 struct xfrm_algo_desc
*calg_desc
;
351 ipcd
= kzalloc(sizeof(*ipcd
), GFP_KERNEL
);
355 mutex_lock(&ipcomp_resource_mutex
);
356 if (!ipcomp_alloc_scratches())
359 ipcd
->tfms
= ipcomp_alloc_tfms(x
->calg
->alg_name
);
362 mutex_unlock(&ipcomp_resource_mutex
);
364 calg_desc
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
366 ipcd
->threshold
= calg_desc
->uinfo
.comp
.threshold
;
373 ipcomp_free_data(ipcd
);
374 mutex_unlock(&ipcomp_resource_mutex
);
378 EXPORT_SYMBOL_GPL(ipcomp_init_state
);
380 MODULE_LICENSE("GPL");
381 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
382 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");