Linux Audio

Check our new training course

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