xref: /openbmc/linux/drivers/net/ppp/ppp_deflate.c (revision dc5fa207)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2224cf5adSJeff Kirsher /*
3224cf5adSJeff Kirsher  * ppp_deflate.c - interface the zlib procedures for Deflate compression
4224cf5adSJeff Kirsher  * and decompression (as used by gzip) to the PPP code.
5224cf5adSJeff Kirsher  *
6784db3f0SPaul Mackerras  * Copyright 1994-1998 Paul Mackerras.
7224cf5adSJeff Kirsher  */
8224cf5adSJeff Kirsher 
9224cf5adSJeff Kirsher #include <linux/module.h>
10224cf5adSJeff Kirsher #include <linux/slab.h>
11224cf5adSJeff Kirsher #include <linux/vmalloc.h>
12224cf5adSJeff Kirsher #include <linux/init.h>
13224cf5adSJeff Kirsher #include <linux/string.h>
14224cf5adSJeff Kirsher 
15224cf5adSJeff Kirsher #include <linux/ppp_defs.h>
16224cf5adSJeff Kirsher #include <linux/ppp-comp.h>
17224cf5adSJeff Kirsher 
18224cf5adSJeff Kirsher #include <linux/zlib.h>
19224cf5adSJeff Kirsher #include <asm/unaligned.h>
20224cf5adSJeff Kirsher 
21224cf5adSJeff Kirsher /*
22224cf5adSJeff Kirsher  * State for a Deflate (de)compressor.
23224cf5adSJeff Kirsher  */
24224cf5adSJeff Kirsher struct ppp_deflate_state {
25224cf5adSJeff Kirsher     int		seqno;
26224cf5adSJeff Kirsher     int		w_size;
27224cf5adSJeff Kirsher     int		unit;
28224cf5adSJeff Kirsher     int		mru;
29224cf5adSJeff Kirsher     int		debug;
30224cf5adSJeff Kirsher     z_stream	strm;
31224cf5adSJeff Kirsher     struct compstat stats;
32224cf5adSJeff Kirsher };
33224cf5adSJeff Kirsher 
34224cf5adSJeff Kirsher #define DEFLATE_OVHD	2		/* Deflate overhead/packet */
35224cf5adSJeff Kirsher 
36224cf5adSJeff Kirsher static void	*z_comp_alloc(unsigned char *options, int opt_len);
37224cf5adSJeff Kirsher static void	*z_decomp_alloc(unsigned char *options, int opt_len);
38224cf5adSJeff Kirsher static void	z_comp_free(void *state);
39224cf5adSJeff Kirsher static void	z_decomp_free(void *state);
40224cf5adSJeff Kirsher static int	z_comp_init(void *state, unsigned char *options,
41224cf5adSJeff Kirsher 				 int opt_len,
42224cf5adSJeff Kirsher 				 int unit, int hdrlen, int debug);
43224cf5adSJeff Kirsher static int	z_decomp_init(void *state, unsigned char *options,
44224cf5adSJeff Kirsher 				   int opt_len,
45224cf5adSJeff Kirsher 				   int unit, int hdrlen, int mru, int debug);
46224cf5adSJeff Kirsher static int	z_compress(void *state, unsigned char *rptr,
47224cf5adSJeff Kirsher 				unsigned char *obuf,
48224cf5adSJeff Kirsher 				int isize, int osize);
49224cf5adSJeff Kirsher static void	z_incomp(void *state, unsigned char *ibuf, int icnt);
50224cf5adSJeff Kirsher static int	z_decompress(void *state, unsigned char *ibuf,
51224cf5adSJeff Kirsher 				int isize, unsigned char *obuf, int osize);
52224cf5adSJeff Kirsher static void	z_comp_reset(void *state);
53224cf5adSJeff Kirsher static void	z_decomp_reset(void *state);
54224cf5adSJeff Kirsher static void	z_comp_stats(void *state, struct compstat *stats);
55224cf5adSJeff Kirsher 
56224cf5adSJeff Kirsher /**
57224cf5adSJeff Kirsher  *	z_comp_free - free the memory used by a compressor
58224cf5adSJeff Kirsher  *	@arg:	pointer to the private state for the compressor.
59224cf5adSJeff Kirsher  */
z_comp_free(void * arg)60224cf5adSJeff Kirsher static void z_comp_free(void *arg)
61224cf5adSJeff Kirsher {
62224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
63224cf5adSJeff Kirsher 
64224cf5adSJeff Kirsher 	if (state) {
65224cf5adSJeff Kirsher 		zlib_deflateEnd(&state->strm);
66224cf5adSJeff Kirsher 		vfree(state->strm.workspace);
67224cf5adSJeff Kirsher 		kfree(state);
68224cf5adSJeff Kirsher 	}
69224cf5adSJeff Kirsher }
70224cf5adSJeff Kirsher 
71224cf5adSJeff Kirsher /**
72224cf5adSJeff Kirsher  *	z_comp_alloc - allocate space for a compressor.
73224cf5adSJeff Kirsher  *	@options: pointer to CCP option data
74224cf5adSJeff Kirsher  *	@opt_len: length of the CCP option at @options.
75224cf5adSJeff Kirsher  *
76224cf5adSJeff Kirsher  *	The @options pointer points to the a buffer containing the
77224cf5adSJeff Kirsher  *	CCP option data for the compression being negotiated.  It is
78224cf5adSJeff Kirsher  *	formatted according to RFC1979, and describes the window
79224cf5adSJeff Kirsher  *	size that the peer is requesting that we use in compressing
80224cf5adSJeff Kirsher  *	data to be sent to it.
81224cf5adSJeff Kirsher  *
82224cf5adSJeff Kirsher  *	Returns the pointer to the private state for the compressor,
83224cf5adSJeff Kirsher  *	or NULL if we could not allocate enough memory.
84224cf5adSJeff Kirsher  */
z_comp_alloc(unsigned char * options,int opt_len)85224cf5adSJeff Kirsher static void *z_comp_alloc(unsigned char *options, int opt_len)
86224cf5adSJeff Kirsher {
87224cf5adSJeff Kirsher 	struct ppp_deflate_state *state;
88224cf5adSJeff Kirsher 	int w_size;
89224cf5adSJeff Kirsher 
90224cf5adSJeff Kirsher 	if (opt_len != CILEN_DEFLATE ||
91224cf5adSJeff Kirsher 	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
92224cf5adSJeff Kirsher 	    options[1] != CILEN_DEFLATE ||
93224cf5adSJeff Kirsher 	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
94224cf5adSJeff Kirsher 	    options[3] != DEFLATE_CHK_SEQUENCE)
95224cf5adSJeff Kirsher 		return NULL;
96224cf5adSJeff Kirsher 	w_size = DEFLATE_SIZE(options[2]);
97224cf5adSJeff Kirsher 	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
98224cf5adSJeff Kirsher 		return NULL;
99224cf5adSJeff Kirsher 
100224cf5adSJeff Kirsher 	state = kzalloc(sizeof(*state),
101224cf5adSJeff Kirsher 						     GFP_KERNEL);
102224cf5adSJeff Kirsher 	if (state == NULL)
103224cf5adSJeff Kirsher 		return NULL;
104224cf5adSJeff Kirsher 
105224cf5adSJeff Kirsher 	state->strm.next_in   = NULL;
106224cf5adSJeff Kirsher 	state->w_size         = w_size;
107224cf5adSJeff Kirsher 	state->strm.workspace = vmalloc(zlib_deflate_workspacesize(-w_size, 8));
108224cf5adSJeff Kirsher 	if (state->strm.workspace == NULL)
109224cf5adSJeff Kirsher 		goto out_free;
110224cf5adSJeff Kirsher 
111224cf5adSJeff Kirsher 	if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
112224cf5adSJeff Kirsher 			 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
113224cf5adSJeff Kirsher 	    != Z_OK)
114224cf5adSJeff Kirsher 		goto out_free;
115224cf5adSJeff Kirsher 	return (void *) state;
116224cf5adSJeff Kirsher 
117224cf5adSJeff Kirsher out_free:
118224cf5adSJeff Kirsher 	z_comp_free(state);
119224cf5adSJeff Kirsher 	return NULL;
120224cf5adSJeff Kirsher }
121224cf5adSJeff Kirsher 
122224cf5adSJeff Kirsher /**
123224cf5adSJeff Kirsher  *	z_comp_init - initialize a previously-allocated compressor.
124224cf5adSJeff Kirsher  *	@arg:	pointer to the private state for the compressor
125224cf5adSJeff Kirsher  *	@options: pointer to the CCP option data describing the
126224cf5adSJeff Kirsher  *		compression that was negotiated with the peer
127224cf5adSJeff Kirsher  *	@opt_len: length of the CCP option data at @options
128224cf5adSJeff Kirsher  *	@unit:	PPP unit number for diagnostic messages
129224cf5adSJeff Kirsher  *	@hdrlen: ignored (present for backwards compatibility)
130224cf5adSJeff Kirsher  *	@debug:	debug flag; if non-zero, debug messages are printed.
131224cf5adSJeff Kirsher  *
132224cf5adSJeff Kirsher  *	The CCP options described by @options must match the options
133224cf5adSJeff Kirsher  *	specified when the compressor was allocated.  The compressor
134224cf5adSJeff Kirsher  *	history is reset.  Returns 0 for failure (CCP options don't
135224cf5adSJeff Kirsher  *	match) or 1 for success.
136224cf5adSJeff Kirsher  */
z_comp_init(void * arg,unsigned char * options,int opt_len,int unit,int hdrlen,int debug)137224cf5adSJeff Kirsher static int z_comp_init(void *arg, unsigned char *options, int opt_len,
138224cf5adSJeff Kirsher 		       int unit, int hdrlen, int debug)
139224cf5adSJeff Kirsher {
140224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
141224cf5adSJeff Kirsher 
142224cf5adSJeff Kirsher 	if (opt_len < CILEN_DEFLATE ||
143224cf5adSJeff Kirsher 	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
144224cf5adSJeff Kirsher 	    options[1] != CILEN_DEFLATE ||
145224cf5adSJeff Kirsher 	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
146224cf5adSJeff Kirsher 	    DEFLATE_SIZE(options[2]) != state->w_size ||
147224cf5adSJeff Kirsher 	    options[3] != DEFLATE_CHK_SEQUENCE)
148224cf5adSJeff Kirsher 		return 0;
149224cf5adSJeff Kirsher 
150224cf5adSJeff Kirsher 	state->seqno = 0;
151224cf5adSJeff Kirsher 	state->unit  = unit;
152224cf5adSJeff Kirsher 	state->debug = debug;
153224cf5adSJeff Kirsher 
154224cf5adSJeff Kirsher 	zlib_deflateReset(&state->strm);
155224cf5adSJeff Kirsher 
156224cf5adSJeff Kirsher 	return 1;
157224cf5adSJeff Kirsher }
158224cf5adSJeff Kirsher 
159224cf5adSJeff Kirsher /**
160224cf5adSJeff Kirsher  *	z_comp_reset - reset a previously-allocated compressor.
161224cf5adSJeff Kirsher  *	@arg:	pointer to private state for the compressor.
162224cf5adSJeff Kirsher  *
163224cf5adSJeff Kirsher  *	This clears the history for the compressor and makes it
164224cf5adSJeff Kirsher  *	ready to start emitting a new compressed stream.
165224cf5adSJeff Kirsher  */
z_comp_reset(void * arg)166224cf5adSJeff Kirsher static void z_comp_reset(void *arg)
167224cf5adSJeff Kirsher {
168224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
169224cf5adSJeff Kirsher 
170224cf5adSJeff Kirsher 	state->seqno = 0;
171224cf5adSJeff Kirsher 	zlib_deflateReset(&state->strm);
172224cf5adSJeff Kirsher }
173224cf5adSJeff Kirsher 
174224cf5adSJeff Kirsher /**
175224cf5adSJeff Kirsher  *	z_compress - compress a PPP packet with Deflate compression.
176224cf5adSJeff Kirsher  *	@arg:	pointer to private state for the compressor
177224cf5adSJeff Kirsher  *	@rptr:	uncompressed packet (input)
178224cf5adSJeff Kirsher  *	@obuf:	compressed packet (output)
179224cf5adSJeff Kirsher  *	@isize:	size of uncompressed packet
180224cf5adSJeff Kirsher  *	@osize:	space available at @obuf
181224cf5adSJeff Kirsher  *
182224cf5adSJeff Kirsher  *	Returns the length of the compressed packet, or 0 if the
183224cf5adSJeff Kirsher  *	packet is incompressible.
184224cf5adSJeff Kirsher  */
z_compress(void * arg,unsigned char * rptr,unsigned char * obuf,int isize,int osize)185224cf5adSJeff Kirsher static int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
186224cf5adSJeff Kirsher 	       int isize, int osize)
187224cf5adSJeff Kirsher {
188224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
189224cf5adSJeff Kirsher 	int r, proto, off, olen, oavail;
190224cf5adSJeff Kirsher 	unsigned char *wptr;
191224cf5adSJeff Kirsher 
192224cf5adSJeff Kirsher 	/*
193224cf5adSJeff Kirsher 	 * Check that the protocol is in the range we handle.
194224cf5adSJeff Kirsher 	 */
195224cf5adSJeff Kirsher 	proto = PPP_PROTOCOL(rptr);
196224cf5adSJeff Kirsher 	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
197224cf5adSJeff Kirsher 		return 0;
198224cf5adSJeff Kirsher 
199224cf5adSJeff Kirsher 	/* Don't generate compressed packets which are larger than
200224cf5adSJeff Kirsher 	   the uncompressed packet. */
201224cf5adSJeff Kirsher 	if (osize > isize)
202224cf5adSJeff Kirsher 		osize = isize;
203224cf5adSJeff Kirsher 
204224cf5adSJeff Kirsher 	wptr = obuf;
205224cf5adSJeff Kirsher 
206224cf5adSJeff Kirsher 	/*
207224cf5adSJeff Kirsher 	 * Copy over the PPP header and store the 2-byte sequence number.
208224cf5adSJeff Kirsher 	 */
209224cf5adSJeff Kirsher 	wptr[0] = PPP_ADDRESS(rptr);
210224cf5adSJeff Kirsher 	wptr[1] = PPP_CONTROL(rptr);
211224cf5adSJeff Kirsher 	put_unaligned_be16(PPP_COMP, wptr + 2);
212224cf5adSJeff Kirsher 	wptr += PPP_HDRLEN;
213224cf5adSJeff Kirsher 	put_unaligned_be16(state->seqno, wptr);
214224cf5adSJeff Kirsher 	wptr += DEFLATE_OVHD;
215224cf5adSJeff Kirsher 	olen = PPP_HDRLEN + DEFLATE_OVHD;
216224cf5adSJeff Kirsher 	state->strm.next_out = wptr;
217224cf5adSJeff Kirsher 	state->strm.avail_out = oavail = osize - olen;
218224cf5adSJeff Kirsher 	++state->seqno;
219224cf5adSJeff Kirsher 
220224cf5adSJeff Kirsher 	off = (proto > 0xff) ? 2 : 3;	/* skip 1st proto byte if 0 */
221224cf5adSJeff Kirsher 	rptr += off;
222224cf5adSJeff Kirsher 	state->strm.next_in = rptr;
223224cf5adSJeff Kirsher 	state->strm.avail_in = (isize - off);
224224cf5adSJeff Kirsher 
225224cf5adSJeff Kirsher 	for (;;) {
226224cf5adSJeff Kirsher 		r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
227224cf5adSJeff Kirsher 		if (r != Z_OK) {
228224cf5adSJeff Kirsher 			if (state->debug)
229224cf5adSJeff Kirsher 				printk(KERN_ERR
230224cf5adSJeff Kirsher 				       "z_compress: deflate returned %d\n", r);
231224cf5adSJeff Kirsher 			break;
232224cf5adSJeff Kirsher 		}
233224cf5adSJeff Kirsher 		if (state->strm.avail_out == 0) {
234224cf5adSJeff Kirsher 			olen += oavail;
235224cf5adSJeff Kirsher 			state->strm.next_out = NULL;
236224cf5adSJeff Kirsher 			state->strm.avail_out = oavail = 1000000;
237224cf5adSJeff Kirsher 		} else {
238224cf5adSJeff Kirsher 			break;		/* all done */
239224cf5adSJeff Kirsher 		}
240224cf5adSJeff Kirsher 	}
241224cf5adSJeff Kirsher 	olen += oavail - state->strm.avail_out;
242224cf5adSJeff Kirsher 
243224cf5adSJeff Kirsher 	/*
244224cf5adSJeff Kirsher 	 * See if we managed to reduce the size of the packet.
245224cf5adSJeff Kirsher 	 */
246e2a4800eSFlorian Westphal 	if (olen < isize && olen <= osize) {
247224cf5adSJeff Kirsher 		state->stats.comp_bytes += olen;
248224cf5adSJeff Kirsher 		state->stats.comp_packets++;
249224cf5adSJeff Kirsher 	} else {
250224cf5adSJeff Kirsher 		state->stats.inc_bytes += isize;
251224cf5adSJeff Kirsher 		state->stats.inc_packets++;
252224cf5adSJeff Kirsher 		olen = 0;
253224cf5adSJeff Kirsher 	}
254224cf5adSJeff Kirsher 	state->stats.unc_bytes += isize;
255224cf5adSJeff Kirsher 	state->stats.unc_packets++;
256224cf5adSJeff Kirsher 
257224cf5adSJeff Kirsher 	return olen;
258224cf5adSJeff Kirsher }
259224cf5adSJeff Kirsher 
260224cf5adSJeff Kirsher /**
261224cf5adSJeff Kirsher  *	z_comp_stats - return compression statistics for a compressor
262224cf5adSJeff Kirsher  *		or decompressor.
263224cf5adSJeff Kirsher  *	@arg:	pointer to private space for the (de)compressor
264224cf5adSJeff Kirsher  *	@stats:	pointer to a struct compstat to receive the result.
265224cf5adSJeff Kirsher  */
z_comp_stats(void * arg,struct compstat * stats)266224cf5adSJeff Kirsher static void z_comp_stats(void *arg, struct compstat *stats)
267224cf5adSJeff Kirsher {
268224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
269224cf5adSJeff Kirsher 
270224cf5adSJeff Kirsher 	*stats = state->stats;
271224cf5adSJeff Kirsher }
272224cf5adSJeff Kirsher 
273224cf5adSJeff Kirsher /**
274224cf5adSJeff Kirsher  *	z_decomp_free - Free the memory used by a decompressor.
275224cf5adSJeff Kirsher  *	@arg:	pointer to private space for the decompressor.
276224cf5adSJeff Kirsher  */
z_decomp_free(void * arg)277224cf5adSJeff Kirsher static void z_decomp_free(void *arg)
278224cf5adSJeff Kirsher {
279224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
280224cf5adSJeff Kirsher 
281224cf5adSJeff Kirsher 	if (state) {
282224cf5adSJeff Kirsher 		vfree(state->strm.workspace);
283224cf5adSJeff Kirsher 		kfree(state);
284224cf5adSJeff Kirsher 	}
285224cf5adSJeff Kirsher }
286224cf5adSJeff Kirsher 
287224cf5adSJeff Kirsher /**
288224cf5adSJeff Kirsher  *	z_decomp_alloc - allocate space for a decompressor.
289224cf5adSJeff Kirsher  *	@options: pointer to CCP option data
290224cf5adSJeff Kirsher  *	@opt_len: length of the CCP option at @options.
291224cf5adSJeff Kirsher  *
292224cf5adSJeff Kirsher  *	The @options pointer points to the a buffer containing the
293224cf5adSJeff Kirsher  *	CCP option data for the compression being negotiated.  It is
294224cf5adSJeff Kirsher  *	formatted according to RFC1979, and describes the window
295224cf5adSJeff Kirsher  *	size that we are requesting the peer to use in compressing
296224cf5adSJeff Kirsher  *	data to be sent to us.
297224cf5adSJeff Kirsher  *
298224cf5adSJeff Kirsher  *	Returns the pointer to the private state for the decompressor,
299224cf5adSJeff Kirsher  *	or NULL if we could not allocate enough memory.
300224cf5adSJeff Kirsher  */
z_decomp_alloc(unsigned char * options,int opt_len)301224cf5adSJeff Kirsher static void *z_decomp_alloc(unsigned char *options, int opt_len)
302224cf5adSJeff Kirsher {
303224cf5adSJeff Kirsher 	struct ppp_deflate_state *state;
304224cf5adSJeff Kirsher 	int w_size;
305224cf5adSJeff Kirsher 
306224cf5adSJeff Kirsher 	if (opt_len != CILEN_DEFLATE ||
307224cf5adSJeff Kirsher 	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
308224cf5adSJeff Kirsher 	    options[1] != CILEN_DEFLATE ||
309224cf5adSJeff Kirsher 	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
310224cf5adSJeff Kirsher 	    options[3] != DEFLATE_CHK_SEQUENCE)
311224cf5adSJeff Kirsher 		return NULL;
312224cf5adSJeff Kirsher 	w_size = DEFLATE_SIZE(options[2]);
313224cf5adSJeff Kirsher 	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
314224cf5adSJeff Kirsher 		return NULL;
315224cf5adSJeff Kirsher 
316224cf5adSJeff Kirsher 	state = kzalloc(sizeof(*state), GFP_KERNEL);
317224cf5adSJeff Kirsher 	if (state == NULL)
318224cf5adSJeff Kirsher 		return NULL;
319224cf5adSJeff Kirsher 
320224cf5adSJeff Kirsher 	state->w_size         = w_size;
321224cf5adSJeff Kirsher 	state->strm.next_out  = NULL;
322224cf5adSJeff Kirsher 	state->strm.workspace = vmalloc(zlib_inflate_workspacesize());
323224cf5adSJeff Kirsher 	if (state->strm.workspace == NULL)
324224cf5adSJeff Kirsher 		goto out_free;
325224cf5adSJeff Kirsher 
326224cf5adSJeff Kirsher 	if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
327224cf5adSJeff Kirsher 		goto out_free;
328224cf5adSJeff Kirsher 	return (void *) state;
329224cf5adSJeff Kirsher 
330224cf5adSJeff Kirsher out_free:
331224cf5adSJeff Kirsher 	z_decomp_free(state);
332224cf5adSJeff Kirsher 	return NULL;
333224cf5adSJeff Kirsher }
334224cf5adSJeff Kirsher 
335224cf5adSJeff Kirsher /**
336224cf5adSJeff Kirsher  *	z_decomp_init - initialize a previously-allocated decompressor.
337224cf5adSJeff Kirsher  *	@arg:	pointer to the private state for the decompressor
338224cf5adSJeff Kirsher  *	@options: pointer to the CCP option data describing the
339224cf5adSJeff Kirsher  *		compression that was negotiated with the peer
340224cf5adSJeff Kirsher  *	@opt_len: length of the CCP option data at @options
341224cf5adSJeff Kirsher  *	@unit:	PPP unit number for diagnostic messages
342224cf5adSJeff Kirsher  *	@hdrlen: ignored (present for backwards compatibility)
343224cf5adSJeff Kirsher  *	@mru:	maximum length of decompressed packets
344224cf5adSJeff Kirsher  *	@debug:	debug flag; if non-zero, debug messages are printed.
345224cf5adSJeff Kirsher  *
346224cf5adSJeff Kirsher  *	The CCP options described by @options must match the options
347224cf5adSJeff Kirsher  *	specified when the decompressor was allocated.  The decompressor
348224cf5adSJeff Kirsher  *	history is reset.  Returns 0 for failure (CCP options don't
349224cf5adSJeff Kirsher  *	match) or 1 for success.
350224cf5adSJeff Kirsher  */
z_decomp_init(void * arg,unsigned char * options,int opt_len,int unit,int hdrlen,int mru,int debug)351224cf5adSJeff Kirsher static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
352224cf5adSJeff Kirsher 			 int unit, int hdrlen, int mru, int debug)
353224cf5adSJeff Kirsher {
354224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
355224cf5adSJeff Kirsher 
356224cf5adSJeff Kirsher 	if (opt_len < CILEN_DEFLATE ||
357224cf5adSJeff Kirsher 	    (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
358224cf5adSJeff Kirsher 	    options[1] != CILEN_DEFLATE ||
359224cf5adSJeff Kirsher 	    DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
360224cf5adSJeff Kirsher 	    DEFLATE_SIZE(options[2]) != state->w_size ||
361224cf5adSJeff Kirsher 	    options[3] != DEFLATE_CHK_SEQUENCE)
362224cf5adSJeff Kirsher 		return 0;
363224cf5adSJeff Kirsher 
364224cf5adSJeff Kirsher 	state->seqno = 0;
365224cf5adSJeff Kirsher 	state->unit  = unit;
366224cf5adSJeff Kirsher 	state->debug = debug;
367224cf5adSJeff Kirsher 	state->mru   = mru;
368224cf5adSJeff Kirsher 
369224cf5adSJeff Kirsher 	zlib_inflateReset(&state->strm);
370224cf5adSJeff Kirsher 
371224cf5adSJeff Kirsher 	return 1;
372224cf5adSJeff Kirsher }
373224cf5adSJeff Kirsher 
374224cf5adSJeff Kirsher /**
375224cf5adSJeff Kirsher  *	z_decomp_reset - reset a previously-allocated decompressor.
376224cf5adSJeff Kirsher  *	@arg:	pointer to private state for the decompressor.
377224cf5adSJeff Kirsher  *
378224cf5adSJeff Kirsher  *	This clears the history for the decompressor and makes it
379224cf5adSJeff Kirsher  *	ready to receive a new compressed stream.
380224cf5adSJeff Kirsher  */
z_decomp_reset(void * arg)381224cf5adSJeff Kirsher static void z_decomp_reset(void *arg)
382224cf5adSJeff Kirsher {
383224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
384224cf5adSJeff Kirsher 
385224cf5adSJeff Kirsher 	state->seqno = 0;
386224cf5adSJeff Kirsher 	zlib_inflateReset(&state->strm);
387224cf5adSJeff Kirsher }
388224cf5adSJeff Kirsher 
389224cf5adSJeff Kirsher /**
390224cf5adSJeff Kirsher  *	z_decompress - decompress a Deflate-compressed packet.
391224cf5adSJeff Kirsher  *	@arg:	pointer to private state for the decompressor
392224cf5adSJeff Kirsher  *	@ibuf:	pointer to input (compressed) packet data
393224cf5adSJeff Kirsher  *	@isize:	length of input packet
394224cf5adSJeff Kirsher  *	@obuf:	pointer to space for output (decompressed) packet
395224cf5adSJeff Kirsher  *	@osize:	amount of space available at @obuf
396224cf5adSJeff Kirsher  *
397224cf5adSJeff Kirsher  * Because of patent problems, we return DECOMP_ERROR for errors
398224cf5adSJeff Kirsher  * found by inspecting the input data and for system problems, but
399224cf5adSJeff Kirsher  * DECOMP_FATALERROR for any errors which could possibly be said to
400224cf5adSJeff Kirsher  * be being detected "after" decompression.  For DECOMP_ERROR,
401224cf5adSJeff Kirsher  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
402224cf5adSJeff Kirsher  * infringing a patent of Motorola's if we do, so we take CCP down
403224cf5adSJeff Kirsher  * instead.
404224cf5adSJeff Kirsher  *
405224cf5adSJeff Kirsher  * Given that the frame has the correct sequence number and a good FCS,
406224cf5adSJeff Kirsher  * errors such as invalid codes in the input most likely indicate a
407224cf5adSJeff Kirsher  * bug, so we return DECOMP_FATALERROR for them in order to turn off
408224cf5adSJeff Kirsher  * compression, even though they are detected by inspecting the input.
409224cf5adSJeff Kirsher  */
z_decompress(void * arg,unsigned char * ibuf,int isize,unsigned char * obuf,int osize)410224cf5adSJeff Kirsher static int z_decompress(void *arg, unsigned char *ibuf, int isize,
411224cf5adSJeff Kirsher 		 unsigned char *obuf, int osize)
412224cf5adSJeff Kirsher {
413224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
414224cf5adSJeff Kirsher 	int olen, seq, r;
415224cf5adSJeff Kirsher 	int decode_proto, overflow;
416224cf5adSJeff Kirsher 	unsigned char overflow_buf[1];
417224cf5adSJeff Kirsher 
418224cf5adSJeff Kirsher 	if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
419224cf5adSJeff Kirsher 		if (state->debug)
420224cf5adSJeff Kirsher 			printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
421224cf5adSJeff Kirsher 			       state->unit, isize);
422224cf5adSJeff Kirsher 		return DECOMP_ERROR;
423224cf5adSJeff Kirsher 	}
424224cf5adSJeff Kirsher 
425224cf5adSJeff Kirsher 	/* Check the sequence number. */
426224cf5adSJeff Kirsher 	seq = get_unaligned_be16(ibuf + PPP_HDRLEN);
427224cf5adSJeff Kirsher 	if (seq != (state->seqno & 0xffff)) {
428224cf5adSJeff Kirsher 		if (state->debug)
429224cf5adSJeff Kirsher 			printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
430224cf5adSJeff Kirsher 			       state->unit, seq, state->seqno & 0xffff);
431224cf5adSJeff Kirsher 		return DECOMP_ERROR;
432224cf5adSJeff Kirsher 	}
433224cf5adSJeff Kirsher 	++state->seqno;
434224cf5adSJeff Kirsher 
435224cf5adSJeff Kirsher 	/*
436224cf5adSJeff Kirsher 	 * Fill in the first part of the PPP header.  The protocol field
437224cf5adSJeff Kirsher 	 * comes from the decompressed data.
438224cf5adSJeff Kirsher 	 */
439224cf5adSJeff Kirsher 	obuf[0] = PPP_ADDRESS(ibuf);
440224cf5adSJeff Kirsher 	obuf[1] = PPP_CONTROL(ibuf);
441224cf5adSJeff Kirsher 	obuf[2] = 0;
442224cf5adSJeff Kirsher 
443224cf5adSJeff Kirsher 	/*
444224cf5adSJeff Kirsher 	 * Set up to call inflate.  We set avail_out to 1 initially so we can
445224cf5adSJeff Kirsher 	 * look at the first byte of the output and decide whether we have
446224cf5adSJeff Kirsher 	 * a 1-byte or 2-byte protocol field.
447224cf5adSJeff Kirsher 	 */
448224cf5adSJeff Kirsher 	state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
449224cf5adSJeff Kirsher 	state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
450224cf5adSJeff Kirsher 	state->strm.next_out = obuf + 3;
451224cf5adSJeff Kirsher 	state->strm.avail_out = 1;
452224cf5adSJeff Kirsher 	decode_proto = 1;
453224cf5adSJeff Kirsher 	overflow = 0;
454224cf5adSJeff Kirsher 
455224cf5adSJeff Kirsher 	/*
456224cf5adSJeff Kirsher 	 * Call inflate, supplying more input or output as needed.
457224cf5adSJeff Kirsher 	 */
458224cf5adSJeff Kirsher 	for (;;) {
459224cf5adSJeff Kirsher 		r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
460224cf5adSJeff Kirsher 		if (r != Z_OK) {
461224cf5adSJeff Kirsher 			if (state->debug)
462224cf5adSJeff Kirsher 				printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
463224cf5adSJeff Kirsher 				       state->unit, r, (state->strm.msg? state->strm.msg: ""));
464224cf5adSJeff Kirsher 			return DECOMP_FATALERROR;
465224cf5adSJeff Kirsher 		}
466224cf5adSJeff Kirsher 		if (state->strm.avail_out != 0)
467224cf5adSJeff Kirsher 			break;		/* all done */
468224cf5adSJeff Kirsher 		if (decode_proto) {
469224cf5adSJeff Kirsher 			state->strm.avail_out = osize - PPP_HDRLEN;
470224cf5adSJeff Kirsher 			if ((obuf[3] & 1) == 0) {
471224cf5adSJeff Kirsher 				/* 2-byte protocol field */
472224cf5adSJeff Kirsher 				obuf[2] = obuf[3];
473224cf5adSJeff Kirsher 				--state->strm.next_out;
474224cf5adSJeff Kirsher 				++state->strm.avail_out;
475224cf5adSJeff Kirsher 			}
476224cf5adSJeff Kirsher 			decode_proto = 0;
477224cf5adSJeff Kirsher 		} else if (!overflow) {
478224cf5adSJeff Kirsher 			/*
479224cf5adSJeff Kirsher 			 * We've filled up the output buffer; the only way to
480224cf5adSJeff Kirsher 			 * find out whether inflate has any more characters
481224cf5adSJeff Kirsher 			 * left is to give it another byte of output space.
482224cf5adSJeff Kirsher 			 */
483224cf5adSJeff Kirsher 			state->strm.next_out = overflow_buf;
484224cf5adSJeff Kirsher 			state->strm.avail_out = 1;
485224cf5adSJeff Kirsher 			overflow = 1;
486224cf5adSJeff Kirsher 		} else {
487224cf5adSJeff Kirsher 			if (state->debug)
488224cf5adSJeff Kirsher 				printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
489224cf5adSJeff Kirsher 				       state->unit);
490224cf5adSJeff Kirsher 			return DECOMP_FATALERROR;
491224cf5adSJeff Kirsher 		}
492224cf5adSJeff Kirsher 	}
493224cf5adSJeff Kirsher 
494224cf5adSJeff Kirsher 	if (decode_proto) {
495224cf5adSJeff Kirsher 		if (state->debug)
496224cf5adSJeff Kirsher 			printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
497224cf5adSJeff Kirsher 			       state->unit);
498224cf5adSJeff Kirsher 		return DECOMP_ERROR;
499224cf5adSJeff Kirsher 	}
500224cf5adSJeff Kirsher 
501224cf5adSJeff Kirsher 	olen = osize + overflow - state->strm.avail_out;
502224cf5adSJeff Kirsher 	state->stats.unc_bytes += olen;
503224cf5adSJeff Kirsher 	state->stats.unc_packets++;
504224cf5adSJeff Kirsher 	state->stats.comp_bytes += isize;
505224cf5adSJeff Kirsher 	state->stats.comp_packets++;
506224cf5adSJeff Kirsher 
507224cf5adSJeff Kirsher 	return olen;
508224cf5adSJeff Kirsher }
509224cf5adSJeff Kirsher 
510224cf5adSJeff Kirsher /**
511224cf5adSJeff Kirsher  *	z_incomp - add incompressible input data to the history.
512224cf5adSJeff Kirsher  *	@arg:	pointer to private state for the decompressor
513224cf5adSJeff Kirsher  *	@ibuf:	pointer to input packet data
514224cf5adSJeff Kirsher  *	@icnt:	length of input data.
515224cf5adSJeff Kirsher  */
z_incomp(void * arg,unsigned char * ibuf,int icnt)516224cf5adSJeff Kirsher static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
517224cf5adSJeff Kirsher {
518224cf5adSJeff Kirsher 	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
519224cf5adSJeff Kirsher 	int proto, r;
520224cf5adSJeff Kirsher 
521224cf5adSJeff Kirsher 	/*
522224cf5adSJeff Kirsher 	 * Check that the protocol is one we handle.
523224cf5adSJeff Kirsher 	 */
524224cf5adSJeff Kirsher 	proto = PPP_PROTOCOL(ibuf);
525224cf5adSJeff Kirsher 	if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
526224cf5adSJeff Kirsher 		return;
527224cf5adSJeff Kirsher 
528224cf5adSJeff Kirsher 	++state->seqno;
529224cf5adSJeff Kirsher 
530224cf5adSJeff Kirsher 	/*
531224cf5adSJeff Kirsher 	 * We start at the either the 1st or 2nd byte of the protocol field,
532224cf5adSJeff Kirsher 	 * depending on whether the protocol value is compressible.
533224cf5adSJeff Kirsher 	 */
534224cf5adSJeff Kirsher 	state->strm.next_in = ibuf + 3;
535224cf5adSJeff Kirsher 	state->strm.avail_in = icnt - 3;
536224cf5adSJeff Kirsher 	if (proto > 0xff) {
537224cf5adSJeff Kirsher 		--state->strm.next_in;
538224cf5adSJeff Kirsher 		++state->strm.avail_in;
539224cf5adSJeff Kirsher 	}
540224cf5adSJeff Kirsher 
541224cf5adSJeff Kirsher 	r = zlib_inflateIncomp(&state->strm);
542224cf5adSJeff Kirsher 	if (r != Z_OK) {
543224cf5adSJeff Kirsher 		/* gak! */
544224cf5adSJeff Kirsher 		if (state->debug) {
545224cf5adSJeff Kirsher 			printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
546224cf5adSJeff Kirsher 			       state->unit, r, (state->strm.msg? state->strm.msg: ""));
547224cf5adSJeff Kirsher 		}
548224cf5adSJeff Kirsher 		return;
549224cf5adSJeff Kirsher 	}
550224cf5adSJeff Kirsher 
551224cf5adSJeff Kirsher 	/*
552224cf5adSJeff Kirsher 	 * Update stats.
553224cf5adSJeff Kirsher 	 */
554224cf5adSJeff Kirsher 	state->stats.inc_bytes += icnt;
555224cf5adSJeff Kirsher 	state->stats.inc_packets++;
556224cf5adSJeff Kirsher 	state->stats.unc_bytes += icnt;
557224cf5adSJeff Kirsher 	state->stats.unc_packets++;
558224cf5adSJeff Kirsher }
559224cf5adSJeff Kirsher 
560224cf5adSJeff Kirsher /*************************************************************
561224cf5adSJeff Kirsher  * Module interface table
562224cf5adSJeff Kirsher  *************************************************************/
563224cf5adSJeff Kirsher 
564224cf5adSJeff Kirsher /* These are in ppp_generic.c */
565224cf5adSJeff Kirsher extern int  ppp_register_compressor   (struct compressor *cp);
566224cf5adSJeff Kirsher extern void ppp_unregister_compressor (struct compressor *cp);
567224cf5adSJeff Kirsher 
568224cf5adSJeff Kirsher /*
569224cf5adSJeff Kirsher  * Procedures exported to if_ppp.c.
570224cf5adSJeff Kirsher  */
571224cf5adSJeff Kirsher static struct compressor ppp_deflate = {
572224cf5adSJeff Kirsher 	.compress_proto =	CI_DEFLATE,
573224cf5adSJeff Kirsher 	.comp_alloc =		z_comp_alloc,
574224cf5adSJeff Kirsher 	.comp_free =		z_comp_free,
575224cf5adSJeff Kirsher 	.comp_init =		z_comp_init,
576224cf5adSJeff Kirsher 	.comp_reset =		z_comp_reset,
577224cf5adSJeff Kirsher 	.compress =		z_compress,
578224cf5adSJeff Kirsher 	.comp_stat =		z_comp_stats,
579224cf5adSJeff Kirsher 	.decomp_alloc =		z_decomp_alloc,
580224cf5adSJeff Kirsher 	.decomp_free =		z_decomp_free,
581224cf5adSJeff Kirsher 	.decomp_init =		z_decomp_init,
582224cf5adSJeff Kirsher 	.decomp_reset =		z_decomp_reset,
583224cf5adSJeff Kirsher 	.decompress =		z_decompress,
584224cf5adSJeff Kirsher 	.incomp =		z_incomp,
585224cf5adSJeff Kirsher 	.decomp_stat =		z_comp_stats,
586224cf5adSJeff Kirsher 	.owner =		THIS_MODULE
587224cf5adSJeff Kirsher };
588224cf5adSJeff Kirsher 
589224cf5adSJeff Kirsher static struct compressor ppp_deflate_draft = {
590224cf5adSJeff Kirsher 	.compress_proto =	CI_DEFLATE_DRAFT,
591224cf5adSJeff Kirsher 	.comp_alloc =		z_comp_alloc,
592224cf5adSJeff Kirsher 	.comp_free =		z_comp_free,
593224cf5adSJeff Kirsher 	.comp_init =		z_comp_init,
594224cf5adSJeff Kirsher 	.comp_reset =		z_comp_reset,
595224cf5adSJeff Kirsher 	.compress =		z_compress,
596224cf5adSJeff Kirsher 	.comp_stat =		z_comp_stats,
597224cf5adSJeff Kirsher 	.decomp_alloc =		z_decomp_alloc,
598224cf5adSJeff Kirsher 	.decomp_free =		z_decomp_free,
599224cf5adSJeff Kirsher 	.decomp_init =		z_decomp_init,
600224cf5adSJeff Kirsher 	.decomp_reset =		z_decomp_reset,
601224cf5adSJeff Kirsher 	.decompress =		z_decompress,
602224cf5adSJeff Kirsher 	.incomp =		z_incomp,
603224cf5adSJeff Kirsher 	.decomp_stat =		z_comp_stats,
604224cf5adSJeff Kirsher 	.owner =		THIS_MODULE
605224cf5adSJeff Kirsher };
606224cf5adSJeff Kirsher 
deflate_init(void)607224cf5adSJeff Kirsher static int __init deflate_init(void)
608224cf5adSJeff Kirsher {
6093ebe1bcaSYueHaibing 	int rc;
6103ebe1bcaSYueHaibing 
6113ebe1bcaSYueHaibing 	rc = ppp_register_compressor(&ppp_deflate);
6123ebe1bcaSYueHaibing 	if (rc)
6133ebe1bcaSYueHaibing 		return rc;
6143ebe1bcaSYueHaibing 
6153ebe1bcaSYueHaibing 	rc = ppp_register_compressor(&ppp_deflate_draft);
6163ebe1bcaSYueHaibing 	if (rc) {
6173ebe1bcaSYueHaibing 		ppp_unregister_compressor(&ppp_deflate);
6183ebe1bcaSYueHaibing 		return rc;
6193ebe1bcaSYueHaibing 	}
6203ebe1bcaSYueHaibing 
6213ebe1bcaSYueHaibing 	pr_info("PPP Deflate Compression module registered\n");
6223ebe1bcaSYueHaibing 	return 0;
623224cf5adSJeff Kirsher }
624224cf5adSJeff Kirsher 
deflate_cleanup(void)625224cf5adSJeff Kirsher static void __exit deflate_cleanup(void)
626224cf5adSJeff Kirsher {
627224cf5adSJeff Kirsher 	ppp_unregister_compressor(&ppp_deflate);
628224cf5adSJeff Kirsher 	ppp_unregister_compressor(&ppp_deflate_draft);
629224cf5adSJeff Kirsher }
630224cf5adSJeff Kirsher 
631224cf5adSJeff Kirsher module_init(deflate_init);
632224cf5adSJeff Kirsher module_exit(deflate_cleanup);
633224cf5adSJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
634224cf5adSJeff Kirsher MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
635224cf5adSJeff Kirsher MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));
636