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);
81 skb_frag_fill_page_desc(frag, page, 0, len);
82 memcpy(skb_frag_address(frag), scratch, len);
88 skb_shinfo(skb)->nr_frags++;
94 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
98 struct ip_comp_hdr *ipch;
100 if (skb_linearize_cow(skb))
103 skb->ip_summed = CHECKSUM_NONE;
105 /* Remove ipcomp header and decompress original payload */
106 ipch = (void *)skb->data;
107 nexthdr = ipch->nexthdr;
109 skb->transport_header = skb->network_header + sizeof(*ipch);
110 __skb_pull(skb, sizeof(*ipch));
111 err = ipcomp_decompress(x, skb);
120 EXPORT_SYMBOL_GPL(ipcomp_input);
122 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
124 struct ipcomp_data *ipcd = x->data;
125 const int plen = skb->len;
126 int dlen = IPCOMP_SCRATCH_SIZE;
127 u8 *start = skb->data;
128 struct crypto_comp *tfm;
133 scratch = *this_cpu_ptr(ipcomp_scratches);
134 tfm = *this_cpu_ptr(ipcd->tfms);
135 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
139 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
144 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
147 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
155 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
158 struct ip_comp_hdr *ipch;
159 struct ipcomp_data *ipcd = x->data;
161 if (skb->len < ipcd->threshold) {
162 /* Don't bother compressing */
166 if (skb_linearize_cow(skb))
169 err = ipcomp_compress(x, skb);
175 /* Install ipcomp header, convert into ipcomp datagram. */
176 ipch = ip_comp_hdr(skb);
177 ipch->nexthdr = *skb_mac_header(skb);
179 ipch->cpi = htons((u16 )ntohl(x->id.spi));
180 *skb_mac_header(skb) = IPPROTO_COMP;
182 skb_push(skb, -skb_network_offset(skb));
185 EXPORT_SYMBOL_GPL(ipcomp_output);
187 static void ipcomp_free_scratches(void)
190 void * __percpu *scratches;
192 if (--ipcomp_scratch_users)
195 scratches = ipcomp_scratches;
199 for_each_possible_cpu(i)
200 vfree(*per_cpu_ptr(scratches, i));
202 free_percpu(scratches);
203 ipcomp_scratches = NULL;
206 static void * __percpu *ipcomp_alloc_scratches(void)
208 void * __percpu *scratches;
211 if (ipcomp_scratch_users++)
212 return ipcomp_scratches;
214 scratches = alloc_percpu(void *);
218 ipcomp_scratches = scratches;
220 for_each_possible_cpu(i) {
223 scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
226 *per_cpu_ptr(scratches, i) = scratch;
232 static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
234 struct ipcomp_tfms *pos;
237 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
238 if (pos->tfms == tfms)
242 WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list));
247 list_del(&pos->list);
253 for_each_possible_cpu(cpu) {
254 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
255 crypto_free_comp(tfm);
260 static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
262 struct ipcomp_tfms *pos;
263 struct crypto_comp * __percpu *tfms;
267 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
268 struct crypto_comp *tfm;
270 /* This can be any valid CPU ID so we don't need locking. */
271 tfm = this_cpu_read(*pos->tfms);
273 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
279 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
284 INIT_LIST_HEAD(&pos->list);
285 list_add(&pos->list, &ipcomp_tfms_list);
287 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
291 for_each_possible_cpu(cpu) {
292 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
296 *per_cpu_ptr(tfms, cpu) = tfm;
302 ipcomp_free_tfms(tfms);
306 static void ipcomp_free_data(struct ipcomp_data *ipcd)
309 ipcomp_free_tfms(ipcd->tfms);
310 ipcomp_free_scratches();
313 void ipcomp_destroy(struct xfrm_state *x)
315 struct ipcomp_data *ipcd = x->data;
318 xfrm_state_delete_tunnel(x);
319 mutex_lock(&ipcomp_resource_mutex);
320 ipcomp_free_data(ipcd);
321 mutex_unlock(&ipcomp_resource_mutex);
324 EXPORT_SYMBOL_GPL(ipcomp_destroy);
326 int ipcomp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
329 struct ipcomp_data *ipcd;
330 struct xfrm_algo_desc *calg_desc;
334 NL_SET_ERR_MSG(extack, "Missing required compression algorithm");
339 NL_SET_ERR_MSG(extack, "IPComp is not compatible with encapsulation");
344 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
348 mutex_lock(&ipcomp_resource_mutex);
349 if (!ipcomp_alloc_scratches())
352 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
355 mutex_unlock(&ipcomp_resource_mutex);
357 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
359 ipcd->threshold = calg_desc->uinfo.comp.threshold;
366 ipcomp_free_data(ipcd);
367 mutex_unlock(&ipcomp_resource_mutex);
371 EXPORT_SYMBOL_GPL(ipcomp_init_state);
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
375 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");