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 u8 *scratch = *this_cpu_ptr(ipcomp_scratches);
45 struct crypto_comp *tfm = *this_cpu_ptr(ipcd->tfms);
46 int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
52 if (dlen < (plen + sizeof(struct ip_comp_hdr)))
56 if (len > skb_tailroom(skb))
57 len = skb_tailroom(skb);
62 skb_copy_to_linear_data(skb, scratch, len);
64 while ((scratch += len, dlen -= len) > 0) {
68 if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
71 frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
72 page = alloc_page(GFP_ATOMIC);
77 __skb_frag_set_page(frag, page);
83 skb_frag_off_set(frag, 0);
84 skb_frag_size_set(frag, len);
85 memcpy(skb_frag_address(frag), scratch, len);
91 skb_shinfo(skb)->nr_frags++;
97 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
101 struct ip_comp_hdr *ipch;
103 if (skb_linearize_cow(skb))
106 skb->ip_summed = CHECKSUM_NONE;
108 /* Remove ipcomp header and decompress original payload */
109 ipch = (void *)skb->data;
110 nexthdr = ipch->nexthdr;
112 skb->transport_header = skb->network_header + sizeof(*ipch);
113 __skb_pull(skb, sizeof(*ipch));
114 err = ipcomp_decompress(x, skb);
123 EXPORT_SYMBOL_GPL(ipcomp_input);
125 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
127 struct ipcomp_data *ipcd = x->data;
128 const int plen = skb->len;
129 int dlen = IPCOMP_SCRATCH_SIZE;
130 u8 *start = skb->data;
131 struct crypto_comp *tfm;
136 scratch = *this_cpu_ptr(ipcomp_scratches);
137 tfm = *this_cpu_ptr(ipcd->tfms);
138 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
142 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
147 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
150 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
158 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
161 struct ip_comp_hdr *ipch;
162 struct ipcomp_data *ipcd = x->data;
164 if (skb->len < ipcd->threshold) {
165 /* Don't bother compressing */
169 if (skb_linearize_cow(skb))
172 err = ipcomp_compress(x, skb);
178 /* Install ipcomp header, convert into ipcomp datagram. */
179 ipch = ip_comp_hdr(skb);
180 ipch->nexthdr = *skb_mac_header(skb);
182 ipch->cpi = htons((u16 )ntohl(x->id.spi));
183 *skb_mac_header(skb) = IPPROTO_COMP;
185 skb_push(skb, -skb_network_offset(skb));
188 EXPORT_SYMBOL_GPL(ipcomp_output);
190 static void ipcomp_free_scratches(void)
193 void * __percpu *scratches;
195 if (--ipcomp_scratch_users)
198 scratches = ipcomp_scratches;
202 for_each_possible_cpu(i)
203 vfree(*per_cpu_ptr(scratches, i));
205 free_percpu(scratches);
208 static void * __percpu *ipcomp_alloc_scratches(void)
210 void * __percpu *scratches;
213 if (ipcomp_scratch_users++)
214 return ipcomp_scratches;
216 scratches = alloc_percpu(void *);
220 ipcomp_scratches = scratches;
222 for_each_possible_cpu(i) {
225 scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
228 *per_cpu_ptr(scratches, i) = scratch;
234 static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
236 struct ipcomp_tfms *pos;
239 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
240 if (pos->tfms == tfms)
244 WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list));
249 list_del(&pos->list);
255 for_each_possible_cpu(cpu) {
256 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
257 crypto_free_comp(tfm);
262 static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
264 struct ipcomp_tfms *pos;
265 struct crypto_comp * __percpu *tfms;
269 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
270 struct crypto_comp *tfm;
272 /* This can be any valid CPU ID so we don't need locking. */
273 tfm = this_cpu_read(*pos->tfms);
275 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
281 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
286 INIT_LIST_HEAD(&pos->list);
287 list_add(&pos->list, &ipcomp_tfms_list);
289 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
293 for_each_possible_cpu(cpu) {
294 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
298 *per_cpu_ptr(tfms, cpu) = tfm;
304 ipcomp_free_tfms(tfms);
308 static void ipcomp_free_data(struct ipcomp_data *ipcd)
311 ipcomp_free_tfms(ipcd->tfms);
312 ipcomp_free_scratches();
315 void ipcomp_destroy(struct xfrm_state *x)
317 struct ipcomp_data *ipcd = x->data;
320 xfrm_state_delete_tunnel(x);
321 mutex_lock(&ipcomp_resource_mutex);
322 ipcomp_free_data(ipcd);
323 mutex_unlock(&ipcomp_resource_mutex);
326 EXPORT_SYMBOL_GPL(ipcomp_destroy);
328 int ipcomp_init_state(struct xfrm_state *x)
331 struct ipcomp_data *ipcd;
332 struct xfrm_algo_desc *calg_desc;
342 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
346 mutex_lock(&ipcomp_resource_mutex);
347 if (!ipcomp_alloc_scratches())
350 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
353 mutex_unlock(&ipcomp_resource_mutex);
355 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
357 ipcd->threshold = calg_desc->uinfo.comp.threshold;
364 ipcomp_free_data(ipcd);
365 mutex_unlock(&ipcomp_resource_mutex);
369 EXPORT_SYMBOL_GPL(ipcomp_init_state);
371 MODULE_LICENSE("GPL");
372 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
373 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");