Linux Audio

Check our new training course

Loading...
v4.17
 
  1/*
  2 * ppp_deflate.c - interface the zlib procedures for Deflate compression
  3 * and decompression (as used by gzip) to the PPP code.
  4 *
  5 * Copyright 1994-1998 Paul Mackerras.
  6 *
  7 *  This program is free software; you can redistribute it and/or
  8 *  modify it under the terms of the GNU General Public License
  9 *  version 2 as published by the Free Software Foundation.
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/slab.h>
 14#include <linux/vmalloc.h>
 15#include <linux/init.h>
 16#include <linux/string.h>
 17
 18#include <linux/ppp_defs.h>
 19#include <linux/ppp-comp.h>
 20
 21#include <linux/zlib.h>
 22#include <asm/unaligned.h>
 23
 24/*
 25 * State for a Deflate (de)compressor.
 26 */
 27struct ppp_deflate_state {
 28    int		seqno;
 29    int		w_size;
 30    int		unit;
 31    int		mru;
 32    int		debug;
 33    z_stream	strm;
 34    struct compstat stats;
 35};
 36
 37#define DEFLATE_OVHD	2		/* Deflate overhead/packet */
 38
 39static void	*z_comp_alloc(unsigned char *options, int opt_len);
 40static void	*z_decomp_alloc(unsigned char *options, int opt_len);
 41static void	z_comp_free(void *state);
 42static void	z_decomp_free(void *state);
 43static int	z_comp_init(void *state, unsigned char *options,
 44				 int opt_len,
 45				 int unit, int hdrlen, int debug);
 46static int	z_decomp_init(void *state, unsigned char *options,
 47				   int opt_len,
 48				   int unit, int hdrlen, int mru, int debug);
 49static int	z_compress(void *state, unsigned char *rptr,
 50				unsigned char *obuf,
 51				int isize, int osize);
 52static void	z_incomp(void *state, unsigned char *ibuf, int icnt);
 53static int	z_decompress(void *state, unsigned char *ibuf,
 54				int isize, unsigned char *obuf, int osize);
 55static void	z_comp_reset(void *state);
 56static void	z_decomp_reset(void *state);
 57static void	z_comp_stats(void *state, struct compstat *stats);
 58
 59/**
 60 *	z_comp_free - free the memory used by a compressor
 61 *	@arg:	pointer to the private state for the compressor.
 62 */
 63static void z_comp_free(void *arg)
 64{
 65	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
 66
 67	if (state) {
 68		zlib_deflateEnd(&state->strm);
 69		vfree(state->strm.workspace);
 70		kfree(state);
 71	}
 72}
 73
 74/**
 75 *	z_comp_alloc - allocate space for a compressor.
 76 *	@options: pointer to CCP option data
 77 *	@opt_len: length of the CCP option at @options.
 78 *
 79 *	The @options pointer points to the a buffer containing the
 80 *	CCP option data for the compression being negotiated.  It is
 81 *	formatted according to RFC1979, and describes the window
 82 *	size that the peer is requesting that we use in compressing
 83 *	data to be sent to it.
 84 *
 85 *	Returns the pointer to the private state for the compressor,
 86 *	or NULL if we could not allocate enough memory.
 87 */
 88static void *z_comp_alloc(unsigned char *options, int opt_len)
 89{
 90	struct ppp_deflate_state *state;
 91	int w_size;
 92
 93	if (opt_len != CILEN_DEFLATE ||
 94	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
 95	    options[1] != CILEN_DEFLATE ||
 96	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
 97	    options[3] != DEFLATE_CHK_SEQUENCE)
 98		return NULL;
 99	w_size = DEFLATE_SIZE(options[2]);
100	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
101		return NULL;
102
103	state = kzalloc(sizeof(*state),
104						     GFP_KERNEL);
105	if (state == NULL)
106		return NULL;
107
108	state->strm.next_in   = NULL;
109	state->w_size         = w_size;
110	state->strm.workspace = vmalloc(zlib_deflate_workspacesize(-w_size, 8));
111	if (state->strm.workspace == NULL)
112		goto out_free;
113
114	if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
115			 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
116	    != Z_OK)
117		goto out_free;
118	return (void *) state;
119
120out_free:
121	z_comp_free(state);
122	return NULL;
123}
124
125/**
126 *	z_comp_init - initialize a previously-allocated compressor.
127 *	@arg:	pointer to the private state for the compressor
128 *	@options: pointer to the CCP option data describing the
129 *		compression that was negotiated with the peer
130 *	@opt_len: length of the CCP option data at @options
131 *	@unit:	PPP unit number for diagnostic messages
132 *	@hdrlen: ignored (present for backwards compatibility)
133 *	@debug:	debug flag; if non-zero, debug messages are printed.
134 *
135 *	The CCP options described by @options must match the options
136 *	specified when the compressor was allocated.  The compressor
137 *	history is reset.  Returns 0 for failure (CCP options don't
138 *	match) or 1 for success.
139 */
140static int z_comp_init(void *arg, unsigned char *options, int opt_len,
141		       int unit, int hdrlen, int debug)
142{
143	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
144
145	if (opt_len < CILEN_DEFLATE ||
146	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
147	    options[1] != CILEN_DEFLATE ||
148	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
149	    DEFLATE_SIZE(options[2]) != state->w_size ||
150	    options[3] != DEFLATE_CHK_SEQUENCE)
151		return 0;
152
153	state->seqno = 0;
154	state->unit  = unit;
155	state->debug = debug;
156
157	zlib_deflateReset(&state->strm);
158
159	return 1;
160}
161
162/**
163 *	z_comp_reset - reset a previously-allocated compressor.
164 *	@arg:	pointer to private state for the compressor.
165 *
166 *	This clears the history for the compressor and makes it
167 *	ready to start emitting a new compressed stream.
168 */
169static void z_comp_reset(void *arg)
170{
171	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
172
173	state->seqno = 0;
174	zlib_deflateReset(&state->strm);
175}
176
177/**
178 *	z_compress - compress a PPP packet with Deflate compression.
179 *	@arg:	pointer to private state for the compressor
180 *	@rptr:	uncompressed packet (input)
181 *	@obuf:	compressed packet (output)
182 *	@isize:	size of uncompressed packet
183 *	@osize:	space available at @obuf
184 *
185 *	Returns the length of the compressed packet, or 0 if the
186 *	packet is incompressible.
187 */
188static int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
189	       int isize, int osize)
190{
191	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
192	int r, proto, off, olen, oavail;
193	unsigned char *wptr;
194
195	/*
196	 * Check that the protocol is in the range we handle.
197	 */
198	proto = PPP_PROTOCOL(rptr);
199	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
200		return 0;
201
202	/* Don't generate compressed packets which are larger than
203	   the uncompressed packet. */
204	if (osize > isize)
205		osize = isize;
206
207	wptr = obuf;
208
209	/*
210	 * Copy over the PPP header and store the 2-byte sequence number.
211	 */
212	wptr[0] = PPP_ADDRESS(rptr);
213	wptr[1] = PPP_CONTROL(rptr);
214	put_unaligned_be16(PPP_COMP, wptr + 2);
215	wptr += PPP_HDRLEN;
216	put_unaligned_be16(state->seqno, wptr);
217	wptr += DEFLATE_OVHD;
218	olen = PPP_HDRLEN + DEFLATE_OVHD;
219	state->strm.next_out = wptr;
220	state->strm.avail_out = oavail = osize - olen;
221	++state->seqno;
222
223	off = (proto > 0xff) ? 2 : 3;	/* skip 1st proto byte if 0 */
224	rptr += off;
225	state->strm.next_in = rptr;
226	state->strm.avail_in = (isize - off);
227
228	for (;;) {
229		r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
230		if (r != Z_OK) {
231			if (state->debug)
232				printk(KERN_ERR
233				       "z_compress: deflate returned %d\n", r);
234			break;
235		}
236		if (state->strm.avail_out == 0) {
237			olen += oavail;
238			state->strm.next_out = NULL;
239			state->strm.avail_out = oavail = 1000000;
240		} else {
241			break;		/* all done */
242		}
243	}
244	olen += oavail - state->strm.avail_out;
245
246	/*
247	 * See if we managed to reduce the size of the packet.
248	 */
249	if (olen < isize && olen <= osize) {
250		state->stats.comp_bytes += olen;
251		state->stats.comp_packets++;
252	} else {
253		state->stats.inc_bytes += isize;
254		state->stats.inc_packets++;
255		olen = 0;
256	}
257	state->stats.unc_bytes += isize;
258	state->stats.unc_packets++;
259
260	return olen;
261}
262
263/**
264 *	z_comp_stats - return compression statistics for a compressor
265 *		or decompressor.
266 *	@arg:	pointer to private space for the (de)compressor
267 *	@stats:	pointer to a struct compstat to receive the result.
268 */
269static void z_comp_stats(void *arg, struct compstat *stats)
270{
271	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
272
273	*stats = state->stats;
274}
275
276/**
277 *	z_decomp_free - Free the memory used by a decompressor.
278 *	@arg:	pointer to private space for the decompressor.
279 */
280static void z_decomp_free(void *arg)
281{
282	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
283
284	if (state) {
285		zlib_inflateEnd(&state->strm);
286		vfree(state->strm.workspace);
287		kfree(state);
288	}
289}
290
291/**
292 *	z_decomp_alloc - allocate space for a decompressor.
293 *	@options: pointer to CCP option data
294 *	@opt_len: length of the CCP option at @options.
295 *
296 *	The @options pointer points to the a buffer containing the
297 *	CCP option data for the compression being negotiated.  It is
298 *	formatted according to RFC1979, and describes the window
299 *	size that we are requesting the peer to use in compressing
300 *	data to be sent to us.
301 *
302 *	Returns the pointer to the private state for the decompressor,
303 *	or NULL if we could not allocate enough memory.
304 */
305static void *z_decomp_alloc(unsigned char *options, int opt_len)
306{
307	struct ppp_deflate_state *state;
308	int w_size;
309
310	if (opt_len != CILEN_DEFLATE ||
311	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
312	    options[1] != CILEN_DEFLATE ||
313	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
314	    options[3] != DEFLATE_CHK_SEQUENCE)
315		return NULL;
316	w_size = DEFLATE_SIZE(options[2]);
317	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
318		return NULL;
319
320	state = kzalloc(sizeof(*state), GFP_KERNEL);
321	if (state == NULL)
322		return NULL;
323
324	state->w_size         = w_size;
325	state->strm.next_out  = NULL;
326	state->strm.workspace = vmalloc(zlib_inflate_workspacesize());
327	if (state->strm.workspace == NULL)
328		goto out_free;
329
330	if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
331		goto out_free;
332	return (void *) state;
333
334out_free:
335	z_decomp_free(state);
336	return NULL;
337}
338
339/**
340 *	z_decomp_init - initialize a previously-allocated decompressor.
341 *	@arg:	pointer to the private state for the decompressor
342 *	@options: pointer to the CCP option data describing the
343 *		compression that was negotiated with the peer
344 *	@opt_len: length of the CCP option data at @options
345 *	@unit:	PPP unit number for diagnostic messages
346 *	@hdrlen: ignored (present for backwards compatibility)
347 *	@mru:	maximum length of decompressed packets
348 *	@debug:	debug flag; if non-zero, debug messages are printed.
349 *
350 *	The CCP options described by @options must match the options
351 *	specified when the decompressor was allocated.  The decompressor
352 *	history is reset.  Returns 0 for failure (CCP options don't
353 *	match) or 1 for success.
354 */
355static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
356			 int unit, int hdrlen, int mru, int debug)
357{
358	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
359
360	if (opt_len < CILEN_DEFLATE ||
361	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
362	    options[1] != CILEN_DEFLATE ||
363	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
364	    DEFLATE_SIZE(options[2]) != state->w_size ||
365	    options[3] != DEFLATE_CHK_SEQUENCE)
366		return 0;
367
368	state->seqno = 0;
369	state->unit  = unit;
370	state->debug = debug;
371	state->mru   = mru;
372
373	zlib_inflateReset(&state->strm);
374
375	return 1;
376}
377
378/**
379 *	z_decomp_reset - reset a previously-allocated decompressor.
380 *	@arg:	pointer to private state for the decompressor.
381 *
382 *	This clears the history for the decompressor and makes it
383 *	ready to receive a new compressed stream.
384 */
385static void z_decomp_reset(void *arg)
386{
387	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
388
389	state->seqno = 0;
390	zlib_inflateReset(&state->strm);
391}
392
393/**
394 *	z_decompress - decompress a Deflate-compressed packet.
395 *	@arg:	pointer to private state for the decompressor
396 *	@ibuf:	pointer to input (compressed) packet data
397 *	@isize:	length of input packet
398 *	@obuf:	pointer to space for output (decompressed) packet
399 *	@osize:	amount of space available at @obuf
400 *
401 * Because of patent problems, we return DECOMP_ERROR for errors
402 * found by inspecting the input data and for system problems, but
403 * DECOMP_FATALERROR for any errors which could possibly be said to
404 * be being detected "after" decompression.  For DECOMP_ERROR,
405 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
406 * infringing a patent of Motorola's if we do, so we take CCP down
407 * instead.
408 *
409 * Given that the frame has the correct sequence number and a good FCS,
410 * errors such as invalid codes in the input most likely indicate a
411 * bug, so we return DECOMP_FATALERROR for them in order to turn off
412 * compression, even though they are detected by inspecting the input.
413 */
414static int z_decompress(void *arg, unsigned char *ibuf, int isize,
415		 unsigned char *obuf, int osize)
416{
417	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
418	int olen, seq, r;
419	int decode_proto, overflow;
420	unsigned char overflow_buf[1];
421
422	if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
423		if (state->debug)
424			printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
425			       state->unit, isize);
426		return DECOMP_ERROR;
427	}
428
429	/* Check the sequence number. */
430	seq = get_unaligned_be16(ibuf + PPP_HDRLEN);
431	if (seq != (state->seqno & 0xffff)) {
432		if (state->debug)
433			printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
434			       state->unit, seq, state->seqno & 0xffff);
435		return DECOMP_ERROR;
436	}
437	++state->seqno;
438
439	/*
440	 * Fill in the first part of the PPP header.  The protocol field
441	 * comes from the decompressed data.
442	 */
443	obuf[0] = PPP_ADDRESS(ibuf);
444	obuf[1] = PPP_CONTROL(ibuf);
445	obuf[2] = 0;
446
447	/*
448	 * Set up to call inflate.  We set avail_out to 1 initially so we can
449	 * look at the first byte of the output and decide whether we have
450	 * a 1-byte or 2-byte protocol field.
451	 */
452	state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
453	state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
454	state->strm.next_out = obuf + 3;
455	state->strm.avail_out = 1;
456	decode_proto = 1;
457	overflow = 0;
458
459	/*
460	 * Call inflate, supplying more input or output as needed.
461	 */
462	for (;;) {
463		r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
464		if (r != Z_OK) {
465			if (state->debug)
466				printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
467				       state->unit, r, (state->strm.msg? state->strm.msg: ""));
468			return DECOMP_FATALERROR;
469		}
470		if (state->strm.avail_out != 0)
471			break;		/* all done */
472		if (decode_proto) {
473			state->strm.avail_out = osize - PPP_HDRLEN;
474			if ((obuf[3] & 1) == 0) {
475				/* 2-byte protocol field */
476				obuf[2] = obuf[3];
477				--state->strm.next_out;
478				++state->strm.avail_out;
479			}
480			decode_proto = 0;
481		} else if (!overflow) {
482			/*
483			 * We've filled up the output buffer; the only way to
484			 * find out whether inflate has any more characters
485			 * left is to give it another byte of output space.
486			 */
487			state->strm.next_out = overflow_buf;
488			state->strm.avail_out = 1;
489			overflow = 1;
490		} else {
491			if (state->debug)
492				printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
493				       state->unit);
494			return DECOMP_FATALERROR;
495		}
496	}
497
498	if (decode_proto) {
499		if (state->debug)
500			printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
501			       state->unit);
502		return DECOMP_ERROR;
503	}
504
505	olen = osize + overflow - state->strm.avail_out;
506	state->stats.unc_bytes += olen;
507	state->stats.unc_packets++;
508	state->stats.comp_bytes += isize;
509	state->stats.comp_packets++;
510
511	return olen;
512}
513
514/**
515 *	z_incomp - add incompressible input data to the history.
516 *	@arg:	pointer to private state for the decompressor
517 *	@ibuf:	pointer to input packet data
518 *	@icnt:	length of input data.
519 */
520static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
521{
522	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
523	int proto, r;
524
525	/*
526	 * Check that the protocol is one we handle.
527	 */
528	proto = PPP_PROTOCOL(ibuf);
529	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
530		return;
531
532	++state->seqno;
533
534	/*
535	 * We start at the either the 1st or 2nd byte of the protocol field,
536	 * depending on whether the protocol value is compressible.
537	 */
538	state->strm.next_in = ibuf + 3;
539	state->strm.avail_in = icnt - 3;
540	if (proto > 0xff) {
541		--state->strm.next_in;
542		++state->strm.avail_in;
543	}
544
545	r = zlib_inflateIncomp(&state->strm);
546	if (r != Z_OK) {
547		/* gak! */
548		if (state->debug) {
549			printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
550			       state->unit, r, (state->strm.msg? state->strm.msg: ""));
551		}
552		return;
553	}
554
555	/*
556	 * Update stats.
557	 */
558	state->stats.inc_bytes += icnt;
559	state->stats.inc_packets++;
560	state->stats.unc_bytes += icnt;
561	state->stats.unc_packets++;
562}
563
564/*************************************************************
565 * Module interface table
566 *************************************************************/
567
568/* These are in ppp_generic.c */
569extern int  ppp_register_compressor   (struct compressor *cp);
570extern void ppp_unregister_compressor (struct compressor *cp);
571
572/*
573 * Procedures exported to if_ppp.c.
574 */
575static struct compressor ppp_deflate = {
576	.compress_proto =	CI_DEFLATE,
577	.comp_alloc =		z_comp_alloc,
578	.comp_free =		z_comp_free,
579	.comp_init =		z_comp_init,
580	.comp_reset =		z_comp_reset,
581	.compress =		z_compress,
582	.comp_stat =		z_comp_stats,
583	.decomp_alloc =		z_decomp_alloc,
584	.decomp_free =		z_decomp_free,
585	.decomp_init =		z_decomp_init,
586	.decomp_reset =		z_decomp_reset,
587	.decompress =		z_decompress,
588	.incomp =		z_incomp,
589	.decomp_stat =		z_comp_stats,
590	.owner =		THIS_MODULE
591};
592
593static struct compressor ppp_deflate_draft = {
594	.compress_proto =	CI_DEFLATE_DRAFT,
595	.comp_alloc =		z_comp_alloc,
596	.comp_free =		z_comp_free,
597	.comp_init =		z_comp_init,
598	.comp_reset =		z_comp_reset,
599	.compress =		z_compress,
600	.comp_stat =		z_comp_stats,
601	.decomp_alloc =		z_decomp_alloc,
602	.decomp_free =		z_decomp_free,
603	.decomp_init =		z_decomp_init,
604	.decomp_reset =		z_decomp_reset,
605	.decompress =		z_decompress,
606	.incomp =		z_incomp,
607	.decomp_stat =		z_comp_stats,
608	.owner =		THIS_MODULE
609};
610
611static int __init deflate_init(void)
612{
613        int answer = ppp_register_compressor(&ppp_deflate);
614        if (answer == 0)
615                printk(KERN_INFO
616		       "PPP Deflate Compression module registered\n");
617	ppp_register_compressor(&ppp_deflate_draft);
618        return answer;
 
 
 
 
 
 
 
 
619}
620
621static void __exit deflate_cleanup(void)
622{
623	ppp_unregister_compressor(&ppp_deflate);
624	ppp_unregister_compressor(&ppp_deflate_draft);
625}
626
627module_init(deflate_init);
628module_exit(deflate_cleanup);
629MODULE_LICENSE("Dual BSD/GPL");
630MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
631MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ppp_deflate.c - interface the zlib procedures for Deflate compression
  4 * and decompression (as used by gzip) to the PPP code.
  5 *
  6 * Copyright 1994-1998 Paul Mackerras.
 
 
 
 
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/slab.h>
 11#include <linux/vmalloc.h>
 12#include <linux/init.h>
 13#include <linux/string.h>
 14
 15#include <linux/ppp_defs.h>
 16#include <linux/ppp-comp.h>
 17
 18#include <linux/zlib.h>
 19#include <asm/unaligned.h>
 20
 21/*
 22 * State for a Deflate (de)compressor.
 23 */
 24struct ppp_deflate_state {
 25    int		seqno;
 26    int		w_size;
 27    int		unit;
 28    int		mru;
 29    int		debug;
 30    z_stream	strm;
 31    struct compstat stats;
 32};
 33
 34#define DEFLATE_OVHD	2		/* Deflate overhead/packet */
 35
 36static void	*z_comp_alloc(unsigned char *options, int opt_len);
 37static void	*z_decomp_alloc(unsigned char *options, int opt_len);
 38static void	z_comp_free(void *state);
 39static void	z_decomp_free(void *state);
 40static int	z_comp_init(void *state, unsigned char *options,
 41				 int opt_len,
 42				 int unit, int hdrlen, int debug);
 43static int	z_decomp_init(void *state, unsigned char *options,
 44				   int opt_len,
 45				   int unit, int hdrlen, int mru, int debug);
 46static int	z_compress(void *state, unsigned char *rptr,
 47				unsigned char *obuf,
 48				int isize, int osize);
 49static void	z_incomp(void *state, unsigned char *ibuf, int icnt);
 50static int	z_decompress(void *state, unsigned char *ibuf,
 51				int isize, unsigned char *obuf, int osize);
 52static void	z_comp_reset(void *state);
 53static void	z_decomp_reset(void *state);
 54static void	z_comp_stats(void *state, struct compstat *stats);
 55
 56/**
 57 *	z_comp_free - free the memory used by a compressor
 58 *	@arg:	pointer to the private state for the compressor.
 59 */
 60static void z_comp_free(void *arg)
 61{
 62	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
 63
 64	if (state) {
 65		zlib_deflateEnd(&state->strm);
 66		vfree(state->strm.workspace);
 67		kfree(state);
 68	}
 69}
 70
 71/**
 72 *	z_comp_alloc - allocate space for a compressor.
 73 *	@options: pointer to CCP option data
 74 *	@opt_len: length of the CCP option at @options.
 75 *
 76 *	The @options pointer points to the a buffer containing the
 77 *	CCP option data for the compression being negotiated.  It is
 78 *	formatted according to RFC1979, and describes the window
 79 *	size that the peer is requesting that we use in compressing
 80 *	data to be sent to it.
 81 *
 82 *	Returns the pointer to the private state for the compressor,
 83 *	or NULL if we could not allocate enough memory.
 84 */
 85static void *z_comp_alloc(unsigned char *options, int opt_len)
 86{
 87	struct ppp_deflate_state *state;
 88	int w_size;
 89
 90	if (opt_len != CILEN_DEFLATE ||
 91	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
 92	    options[1] != CILEN_DEFLATE ||
 93	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
 94	    options[3] != DEFLATE_CHK_SEQUENCE)
 95		return NULL;
 96	w_size = DEFLATE_SIZE(options[2]);
 97	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
 98		return NULL;
 99
100	state = kzalloc(sizeof(*state),
101						     GFP_KERNEL);
102	if (state == NULL)
103		return NULL;
104
105	state->strm.next_in   = NULL;
106	state->w_size         = w_size;
107	state->strm.workspace = vmalloc(zlib_deflate_workspacesize(-w_size, 8));
108	if (state->strm.workspace == NULL)
109		goto out_free;
110
111	if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
112			 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
113	    != Z_OK)
114		goto out_free;
115	return (void *) state;
116
117out_free:
118	z_comp_free(state);
119	return NULL;
120}
121
122/**
123 *	z_comp_init - initialize a previously-allocated compressor.
124 *	@arg:	pointer to the private state for the compressor
125 *	@options: pointer to the CCP option data describing the
126 *		compression that was negotiated with the peer
127 *	@opt_len: length of the CCP option data at @options
128 *	@unit:	PPP unit number for diagnostic messages
129 *	@hdrlen: ignored (present for backwards compatibility)
130 *	@debug:	debug flag; if non-zero, debug messages are printed.
131 *
132 *	The CCP options described by @options must match the options
133 *	specified when the compressor was allocated.  The compressor
134 *	history is reset.  Returns 0 for failure (CCP options don't
135 *	match) or 1 for success.
136 */
137static int z_comp_init(void *arg, unsigned char *options, int opt_len,
138		       int unit, int hdrlen, int debug)
139{
140	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
141
142	if (opt_len < CILEN_DEFLATE ||
143	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
144	    options[1] != CILEN_DEFLATE ||
145	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
146	    DEFLATE_SIZE(options[2]) != state->w_size ||
147	    options[3] != DEFLATE_CHK_SEQUENCE)
148		return 0;
149
150	state->seqno = 0;
151	state->unit  = unit;
152	state->debug = debug;
153
154	zlib_deflateReset(&state->strm);
155
156	return 1;
157}
158
159/**
160 *	z_comp_reset - reset a previously-allocated compressor.
161 *	@arg:	pointer to private state for the compressor.
162 *
163 *	This clears the history for the compressor and makes it
164 *	ready to start emitting a new compressed stream.
165 */
166static void z_comp_reset(void *arg)
167{
168	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
169
170	state->seqno = 0;
171	zlib_deflateReset(&state->strm);
172}
173
174/**
175 *	z_compress - compress a PPP packet with Deflate compression.
176 *	@arg:	pointer to private state for the compressor
177 *	@rptr:	uncompressed packet (input)
178 *	@obuf:	compressed packet (output)
179 *	@isize:	size of uncompressed packet
180 *	@osize:	space available at @obuf
181 *
182 *	Returns the length of the compressed packet, or 0 if the
183 *	packet is incompressible.
184 */
185static int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
186	       int isize, int osize)
187{
188	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
189	int r, proto, off, olen, oavail;
190	unsigned char *wptr;
191
192	/*
193	 * Check that the protocol is in the range we handle.
194	 */
195	proto = PPP_PROTOCOL(rptr);
196	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
197		return 0;
198
199	/* Don't generate compressed packets which are larger than
200	   the uncompressed packet. */
201	if (osize > isize)
202		osize = isize;
203
204	wptr = obuf;
205
206	/*
207	 * Copy over the PPP header and store the 2-byte sequence number.
208	 */
209	wptr[0] = PPP_ADDRESS(rptr);
210	wptr[1] = PPP_CONTROL(rptr);
211	put_unaligned_be16(PPP_COMP, wptr + 2);
212	wptr += PPP_HDRLEN;
213	put_unaligned_be16(state->seqno, wptr);
214	wptr += DEFLATE_OVHD;
215	olen = PPP_HDRLEN + DEFLATE_OVHD;
216	state->strm.next_out = wptr;
217	state->strm.avail_out = oavail = osize - olen;
218	++state->seqno;
219
220	off = (proto > 0xff) ? 2 : 3;	/* skip 1st proto byte if 0 */
221	rptr += off;
222	state->strm.next_in = rptr;
223	state->strm.avail_in = (isize - off);
224
225	for (;;) {
226		r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
227		if (r != Z_OK) {
228			if (state->debug)
229				printk(KERN_ERR
230				       "z_compress: deflate returned %d\n", r);
231			break;
232		}
233		if (state->strm.avail_out == 0) {
234			olen += oavail;
235			state->strm.next_out = NULL;
236			state->strm.avail_out = oavail = 1000000;
237		} else {
238			break;		/* all done */
239		}
240	}
241	olen += oavail - state->strm.avail_out;
242
243	/*
244	 * See if we managed to reduce the size of the packet.
245	 */
246	if (olen < isize && olen <= osize) {
247		state->stats.comp_bytes += olen;
248		state->stats.comp_packets++;
249	} else {
250		state->stats.inc_bytes += isize;
251		state->stats.inc_packets++;
252		olen = 0;
253	}
254	state->stats.unc_bytes += isize;
255	state->stats.unc_packets++;
256
257	return olen;
258}
259
260/**
261 *	z_comp_stats - return compression statistics for a compressor
262 *		or decompressor.
263 *	@arg:	pointer to private space for the (de)compressor
264 *	@stats:	pointer to a struct compstat to receive the result.
265 */
266static void z_comp_stats(void *arg, struct compstat *stats)
267{
268	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
269
270	*stats = state->stats;
271}
272
273/**
274 *	z_decomp_free - Free the memory used by a decompressor.
275 *	@arg:	pointer to private space for the decompressor.
276 */
277static void z_decomp_free(void *arg)
278{
279	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
280
281	if (state) {
282		zlib_inflateEnd(&state->strm);
283		vfree(state->strm.workspace);
284		kfree(state);
285	}
286}
287
288/**
289 *	z_decomp_alloc - allocate space for a decompressor.
290 *	@options: pointer to CCP option data
291 *	@opt_len: length of the CCP option at @options.
292 *
293 *	The @options pointer points to the a buffer containing the
294 *	CCP option data for the compression being negotiated.  It is
295 *	formatted according to RFC1979, and describes the window
296 *	size that we are requesting the peer to use in compressing
297 *	data to be sent to us.
298 *
299 *	Returns the pointer to the private state for the decompressor,
300 *	or NULL if we could not allocate enough memory.
301 */
302static void *z_decomp_alloc(unsigned char *options, int opt_len)
303{
304	struct ppp_deflate_state *state;
305	int w_size;
306
307	if (opt_len != CILEN_DEFLATE ||
308	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
309	    options[1] != CILEN_DEFLATE ||
310	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
311	    options[3] != DEFLATE_CHK_SEQUENCE)
312		return NULL;
313	w_size = DEFLATE_SIZE(options[2]);
314	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
315		return NULL;
316
317	state = kzalloc(sizeof(*state), GFP_KERNEL);
318	if (state == NULL)
319		return NULL;
320
321	state->w_size         = w_size;
322	state->strm.next_out  = NULL;
323	state->strm.workspace = vmalloc(zlib_inflate_workspacesize());
324	if (state->strm.workspace == NULL)
325		goto out_free;
326
327	if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
328		goto out_free;
329	return (void *) state;
330
331out_free:
332	z_decomp_free(state);
333	return NULL;
334}
335
336/**
337 *	z_decomp_init - initialize a previously-allocated decompressor.
338 *	@arg:	pointer to the private state for the decompressor
339 *	@options: pointer to the CCP option data describing the
340 *		compression that was negotiated with the peer
341 *	@opt_len: length of the CCP option data at @options
342 *	@unit:	PPP unit number for diagnostic messages
343 *	@hdrlen: ignored (present for backwards compatibility)
344 *	@mru:	maximum length of decompressed packets
345 *	@debug:	debug flag; if non-zero, debug messages are printed.
346 *
347 *	The CCP options described by @options must match the options
348 *	specified when the decompressor was allocated.  The decompressor
349 *	history is reset.  Returns 0 for failure (CCP options don't
350 *	match) or 1 for success.
351 */
352static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
353			 int unit, int hdrlen, int mru, int debug)
354{
355	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
356
357	if (opt_len < CILEN_DEFLATE ||
358	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
359	    options[1] != CILEN_DEFLATE ||
360	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
361	    DEFLATE_SIZE(options[2]) != state->w_size ||
362	    options[3] != DEFLATE_CHK_SEQUENCE)
363		return 0;
364
365	state->seqno = 0;
366	state->unit  = unit;
367	state->debug = debug;
368	state->mru   = mru;
369
370	zlib_inflateReset(&state->strm);
371
372	return 1;
373}
374
375/**
376 *	z_decomp_reset - reset a previously-allocated decompressor.
377 *	@arg:	pointer to private state for the decompressor.
378 *
379 *	This clears the history for the decompressor and makes it
380 *	ready to receive a new compressed stream.
381 */
382static void z_decomp_reset(void *arg)
383{
384	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
385
386	state->seqno = 0;
387	zlib_inflateReset(&state->strm);
388}
389
390/**
391 *	z_decompress - decompress a Deflate-compressed packet.
392 *	@arg:	pointer to private state for the decompressor
393 *	@ibuf:	pointer to input (compressed) packet data
394 *	@isize:	length of input packet
395 *	@obuf:	pointer to space for output (decompressed) packet
396 *	@osize:	amount of space available at @obuf
397 *
398 * Because of patent problems, we return DECOMP_ERROR for errors
399 * found by inspecting the input data and for system problems, but
400 * DECOMP_FATALERROR for any errors which could possibly be said to
401 * be being detected "after" decompression.  For DECOMP_ERROR,
402 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
403 * infringing a patent of Motorola's if we do, so we take CCP down
404 * instead.
405 *
406 * Given that the frame has the correct sequence number and a good FCS,
407 * errors such as invalid codes in the input most likely indicate a
408 * bug, so we return DECOMP_FATALERROR for them in order to turn off
409 * compression, even though they are detected by inspecting the input.
410 */
411static int z_decompress(void *arg, unsigned char *ibuf, int isize,
412		 unsigned char *obuf, int osize)
413{
414	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
415	int olen, seq, r;
416	int decode_proto, overflow;
417	unsigned char overflow_buf[1];
418
419	if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
420		if (state->debug)
421			printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
422			       state->unit, isize);
423		return DECOMP_ERROR;
424	}
425
426	/* Check the sequence number. */
427	seq = get_unaligned_be16(ibuf + PPP_HDRLEN);
428	if (seq != (state->seqno & 0xffff)) {
429		if (state->debug)
430			printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
431			       state->unit, seq, state->seqno & 0xffff);
432		return DECOMP_ERROR;
433	}
434	++state->seqno;
435
436	/*
437	 * Fill in the first part of the PPP header.  The protocol field
438	 * comes from the decompressed data.
439	 */
440	obuf[0] = PPP_ADDRESS(ibuf);
441	obuf[1] = PPP_CONTROL(ibuf);
442	obuf[2] = 0;
443
444	/*
445	 * Set up to call inflate.  We set avail_out to 1 initially so we can
446	 * look at the first byte of the output and decide whether we have
447	 * a 1-byte or 2-byte protocol field.
448	 */
449	state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
450	state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
451	state->strm.next_out = obuf + 3;
452	state->strm.avail_out = 1;
453	decode_proto = 1;
454	overflow = 0;
455
456	/*
457	 * Call inflate, supplying more input or output as needed.
458	 */
459	for (;;) {
460		r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
461		if (r != Z_OK) {
462			if (state->debug)
463				printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
464				       state->unit, r, (state->strm.msg? state->strm.msg: ""));
465			return DECOMP_FATALERROR;
466		}
467		if (state->strm.avail_out != 0)
468			break;		/* all done */
469		if (decode_proto) {
470			state->strm.avail_out = osize - PPP_HDRLEN;
471			if ((obuf[3] & 1) == 0) {
472				/* 2-byte protocol field */
473				obuf[2] = obuf[3];
474				--state->strm.next_out;
475				++state->strm.avail_out;
476			}
477			decode_proto = 0;
478		} else if (!overflow) {
479			/*
480			 * We've filled up the output buffer; the only way to
481			 * find out whether inflate has any more characters
482			 * left is to give it another byte of output space.
483			 */
484			state->strm.next_out = overflow_buf;
485			state->strm.avail_out = 1;
486			overflow = 1;
487		} else {
488			if (state->debug)
489				printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
490				       state->unit);
491			return DECOMP_FATALERROR;
492		}
493	}
494
495	if (decode_proto) {
496		if (state->debug)
497			printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
498			       state->unit);
499		return DECOMP_ERROR;
500	}
501
502	olen = osize + overflow - state->strm.avail_out;
503	state->stats.unc_bytes += olen;
504	state->stats.unc_packets++;
505	state->stats.comp_bytes += isize;
506	state->stats.comp_packets++;
507
508	return olen;
509}
510
511/**
512 *	z_incomp - add incompressible input data to the history.
513 *	@arg:	pointer to private state for the decompressor
514 *	@ibuf:	pointer to input packet data
515 *	@icnt:	length of input data.
516 */
517static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
518{
519	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
520	int proto, r;
521
522	/*
523	 * Check that the protocol is one we handle.
524	 */
525	proto = PPP_PROTOCOL(ibuf);
526	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
527		return;
528
529	++state->seqno;
530
531	/*
532	 * We start at the either the 1st or 2nd byte of the protocol field,
533	 * depending on whether the protocol value is compressible.
534	 */
535	state->strm.next_in = ibuf + 3;
536	state->strm.avail_in = icnt - 3;
537	if (proto > 0xff) {
538		--state->strm.next_in;
539		++state->strm.avail_in;
540	}
541
542	r = zlib_inflateIncomp(&state->strm);
543	if (r != Z_OK) {
544		/* gak! */
545		if (state->debug) {
546			printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
547			       state->unit, r, (state->strm.msg? state->strm.msg: ""));
548		}
549		return;
550	}
551
552	/*
553	 * Update stats.
554	 */
555	state->stats.inc_bytes += icnt;
556	state->stats.inc_packets++;
557	state->stats.unc_bytes += icnt;
558	state->stats.unc_packets++;
559}
560
561/*************************************************************
562 * Module interface table
563 *************************************************************/
564
565/* These are in ppp_generic.c */
566extern int  ppp_register_compressor   (struct compressor *cp);
567extern void ppp_unregister_compressor (struct compressor *cp);
568
569/*
570 * Procedures exported to if_ppp.c.
571 */
572static struct compressor ppp_deflate = {
573	.compress_proto =	CI_DEFLATE,
574	.comp_alloc =		z_comp_alloc,
575	.comp_free =		z_comp_free,
576	.comp_init =		z_comp_init,
577	.comp_reset =		z_comp_reset,
578	.compress =		z_compress,
579	.comp_stat =		z_comp_stats,
580	.decomp_alloc =		z_decomp_alloc,
581	.decomp_free =		z_decomp_free,
582	.decomp_init =		z_decomp_init,
583	.decomp_reset =		z_decomp_reset,
584	.decompress =		z_decompress,
585	.incomp =		z_incomp,
586	.decomp_stat =		z_comp_stats,
587	.owner =		THIS_MODULE
588};
589
590static struct compressor ppp_deflate_draft = {
591	.compress_proto =	CI_DEFLATE_DRAFT,
592	.comp_alloc =		z_comp_alloc,
593	.comp_free =		z_comp_free,
594	.comp_init =		z_comp_init,
595	.comp_reset =		z_comp_reset,
596	.compress =		z_compress,
597	.comp_stat =		z_comp_stats,
598	.decomp_alloc =		z_decomp_alloc,
599	.decomp_free =		z_decomp_free,
600	.decomp_init =		z_decomp_init,
601	.decomp_reset =		z_decomp_reset,
602	.decompress =		z_decompress,
603	.incomp =		z_incomp,
604	.decomp_stat =		z_comp_stats,
605	.owner =		THIS_MODULE
606};
607
608static int __init deflate_init(void)
609{
610	int rc;
611
612	rc = ppp_register_compressor(&ppp_deflate);
613	if (rc)
614		return rc;
615
616	rc = ppp_register_compressor(&ppp_deflate_draft);
617	if (rc) {
618		ppp_unregister_compressor(&ppp_deflate);
619		return rc;
620	}
621
622	pr_info("PPP Deflate Compression module registered\n");
623	return 0;
624}
625
626static void __exit deflate_cleanup(void)
627{
628	ppp_unregister_compressor(&ppp_deflate);
629	ppp_unregister_compressor(&ppp_deflate_draft);
630}
631
632module_init(deflate_init);
633module_exit(deflate_cleanup);
634MODULE_LICENSE("Dual BSD/GPL");
635MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
636MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));