1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IP Payload Compression Protocol (IPComp) - RFC3173.
4 *
5 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * Todo:
9 * - Tunable compression parameters.
10 * - Compression stats.
11 * - Adaptive compression.
12 */
13
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>
23 #include <net/ip.h>
24 #include <net/ipcomp.h>
25 #include <net/xfrm.h>
26
27 struct ipcomp_tfms {
28 struct list_head list;
29 struct crypto_comp * __percpu *tfms;
30 int users;
31 };
32
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);
37
ipcomp_decompress(struct xfrm_state * x,struct sk_buff * skb)38 static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
39 {
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);
48 int len;
49
50 if (err)
51 goto out;
52
53 if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
54 err = -EINVAL;
55 goto out;
56 }
57
58 len = dlen - plen;
59 if (len > skb_tailroom(skb))
60 len = skb_tailroom(skb);
61
62 __skb_put(skb, len);
63
64 len += plen;
65 skb_copy_to_linear_data(skb, scratch, len);
66
67 while ((scratch += len, dlen -= len) > 0) {
68 skb_frag_t *frag;
69 struct page *page;
70
71 err = -EMSGSIZE;
72 if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
73 goto out;
74
75 frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
76 page = alloc_page(GFP_ATOMIC);
77
78 err = -ENOMEM;
79 if (!page)
80 goto out;
81
82 __skb_frag_set_page(frag, page);
83
84 len = PAGE_SIZE;
85 if (dlen < len)
86 len = dlen;
87
88 skb_frag_off_set(frag, 0);
89 skb_frag_size_set(frag, len);
90 memcpy(skb_frag_address(frag), scratch, len);
91
92 skb->truesize += len;
93 skb->data_len += len;
94 skb->len += len;
95
96 skb_shinfo(skb)->nr_frags++;
97 }
98
99 err = 0;
100
101 out:
102 put_cpu();
103 return err;
104 }
105
ipcomp_input(struct xfrm_state * x,struct sk_buff * skb)106 int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
107 {
108 int nexthdr;
109 int err = -ENOMEM;
110 struct ip_comp_hdr *ipch;
111
112 if (skb_linearize_cow(skb))
113 goto out;
114
115 skb->ip_summed = CHECKSUM_NONE;
116
117 /* Remove ipcomp header and decompress original payload */
118 ipch = (void *)skb->data;
119 nexthdr = ipch->nexthdr;
120
121 skb->transport_header = skb->network_header + sizeof(*ipch);
122 __skb_pull(skb, sizeof(*ipch));
123 err = ipcomp_decompress(x, skb);
124 if (err)
125 goto out;
126
127 err = nexthdr;
128
129 out:
130 return err;
131 }
132 EXPORT_SYMBOL_GPL(ipcomp_input);
133
ipcomp_compress(struct xfrm_state * x,struct sk_buff * skb)134 static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
135 {
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;
141 u8 *scratch;
142 int err;
143
144 local_bh_disable();
145 scratch = *this_cpu_ptr(ipcomp_scratches);
146 tfm = *this_cpu_ptr(ipcd->tfms);
147 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
148 if (err)
149 goto out;
150
151 if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
152 err = -EMSGSIZE;
153 goto out;
154 }
155
156 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
157 local_bh_enable();
158
159 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
160 return 0;
161
162 out:
163 local_bh_enable();
164 return err;
165 }
166
ipcomp_output(struct xfrm_state * x,struct sk_buff * skb)167 int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
168 {
169 int err;
170 struct ip_comp_hdr *ipch;
171 struct ipcomp_data *ipcd = x->data;
172
173 if (skb->len < ipcd->threshold) {
174 /* Don't bother compressing */
175 goto out_ok;
176 }
177
178 if (skb_linearize_cow(skb))
179 goto out_ok;
180
181 err = ipcomp_compress(x, skb);
182
183 if (err) {
184 goto out_ok;
185 }
186
187 /* Install ipcomp header, convert into ipcomp datagram. */
188 ipch = ip_comp_hdr(skb);
189 ipch->nexthdr = *skb_mac_header(skb);
190 ipch->flags = 0;
191 ipch->cpi = htons((u16 )ntohl(x->id.spi));
192 *skb_mac_header(skb) = IPPROTO_COMP;
193 out_ok:
194 skb_push(skb, -skb_network_offset(skb));
195 return 0;
196 }
197 EXPORT_SYMBOL_GPL(ipcomp_output);
198
ipcomp_free_scratches(void)199 static void ipcomp_free_scratches(void)
200 {
201 int i;
202 void * __percpu *scratches;
203
204 if (--ipcomp_scratch_users)
205 return;
206
207 scratches = ipcomp_scratches;
208 if (!scratches)
209 return;
210
211 for_each_possible_cpu(i)
212 vfree(*per_cpu_ptr(scratches, i));
213
214 free_percpu(scratches);
215 ipcomp_scratches = NULL;
216 }
217
ipcomp_alloc_scratches(void)218 static void * __percpu *ipcomp_alloc_scratches(void)
219 {
220 void * __percpu *scratches;
221 int i;
222
223 if (ipcomp_scratch_users++)
224 return ipcomp_scratches;
225
226 scratches = alloc_percpu(void *);
227 if (!scratches)
228 return NULL;
229
230 ipcomp_scratches = scratches;
231
232 for_each_possible_cpu(i) {
233 void *scratch;
234
235 scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
236 if (!scratch)
237 return NULL;
238 *per_cpu_ptr(scratches, i) = scratch;
239 }
240
241 return scratches;
242 }
243
ipcomp_free_tfms(struct crypto_comp * __percpu * tfms)244 static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
245 {
246 struct ipcomp_tfms *pos;
247 int cpu;
248
249 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
250 if (pos->tfms == tfms)
251 break;
252 }
253
254 WARN_ON(list_entry_is_head(pos, &ipcomp_tfms_list, list));
255
256 if (--pos->users)
257 return;
258
259 list_del(&pos->list);
260 kfree(pos);
261
262 if (!tfms)
263 return;
264
265 for_each_possible_cpu(cpu) {
266 struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
267 crypto_free_comp(tfm);
268 }
269 free_percpu(tfms);
270 }
271
ipcomp_alloc_tfms(const char * alg_name)272 static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
273 {
274 struct ipcomp_tfms *pos;
275 struct crypto_comp * __percpu *tfms;
276 int cpu;
277
278
279 list_for_each_entry(pos, &ipcomp_tfms_list, list) {
280 struct crypto_comp *tfm;
281
282 /* This can be any valid CPU ID so we don't need locking. */
283 tfm = this_cpu_read(*pos->tfms);
284
285 if (!strcmp(crypto_comp_name(tfm), alg_name)) {
286 pos->users++;
287 return pos->tfms;
288 }
289 }
290
291 pos = kmalloc(sizeof(*pos), GFP_KERNEL);
292 if (!pos)
293 return NULL;
294
295 pos->users = 1;
296 INIT_LIST_HEAD(&pos->list);
297 list_add(&pos->list, &ipcomp_tfms_list);
298
299 pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
300 if (!tfms)
301 goto error;
302
303 for_each_possible_cpu(cpu) {
304 struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
305 CRYPTO_ALG_ASYNC);
306 if (IS_ERR(tfm))
307 goto error;
308 *per_cpu_ptr(tfms, cpu) = tfm;
309 }
310
311 return tfms;
312
313 error:
314 ipcomp_free_tfms(tfms);
315 return NULL;
316 }
317
ipcomp_free_data(struct ipcomp_data * ipcd)318 static void ipcomp_free_data(struct ipcomp_data *ipcd)
319 {
320 if (ipcd->tfms)
321 ipcomp_free_tfms(ipcd->tfms);
322 ipcomp_free_scratches();
323 }
324
ipcomp_destroy(struct xfrm_state * x)325 void ipcomp_destroy(struct xfrm_state *x)
326 {
327 struct ipcomp_data *ipcd = x->data;
328 if (!ipcd)
329 return;
330 xfrm_state_delete_tunnel(x);
331 mutex_lock(&ipcomp_resource_mutex);
332 ipcomp_free_data(ipcd);
333 mutex_unlock(&ipcomp_resource_mutex);
334 kfree(ipcd);
335 }
336 EXPORT_SYMBOL_GPL(ipcomp_destroy);
337
ipcomp_init_state(struct xfrm_state * x)338 int ipcomp_init_state(struct xfrm_state *x)
339 {
340 int err;
341 struct ipcomp_data *ipcd;
342 struct xfrm_algo_desc *calg_desc;
343
344 err = -EINVAL;
345 if (!x->calg)
346 goto out;
347
348 if (x->encap)
349 goto out;
350
351 err = -ENOMEM;
352 ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
353 if (!ipcd)
354 goto out;
355
356 mutex_lock(&ipcomp_resource_mutex);
357 if (!ipcomp_alloc_scratches())
358 goto error;
359
360 ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
361 if (!ipcd->tfms)
362 goto error;
363 mutex_unlock(&ipcomp_resource_mutex);
364
365 calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
366 BUG_ON(!calg_desc);
367 ipcd->threshold = calg_desc->uinfo.comp.threshold;
368 x->data = ipcd;
369 err = 0;
370 out:
371 return err;
372
373 error:
374 ipcomp_free_data(ipcd);
375 mutex_unlock(&ipcomp_resource_mutex);
376 kfree(ipcd);
377 goto out;
378 }
379 EXPORT_SYMBOL_GPL(ipcomp_init_state);
380
381 MODULE_LICENSE("GPL");
382 MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
383 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
384