MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / ppp_deflate.c
blobc51291a3c40ac73b0a9a49a125ea9c6cdd2c988e
1 /*
2 * ==FILEVERSION 980319==
4 * ppp_deflate.c - interface the zlib procedures for Deflate compression
5 * and decompression (as used by gzip) to the PPP code.
6 * This version is for use with Linux kernel 1.3.X.
8 * Copyright (c) 1994 The Australian National University.
9 * All rights reserved.
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation is hereby granted, provided that the above copyright
13 * notice appears in all copies. This software is provided without any
14 * warranty, express or implied. The Australian National University
15 * makes no representations about the suitability of this software for
16 * any purpose.
18 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
19 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
22 * OF SUCH DAMAGE.
24 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
27 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
28 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 * OR MODIFICATIONS.
31 * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/init.h>
38 #include <linux/string.h>
40 #include <linux/ppp_defs.h>
41 #include <linux/ppp-comp.h>
43 #include <linux/zlib.h>
46 * State for a Deflate (de)compressor.
48 struct ppp_deflate_state {
49 int seqno;
50 int w_size;
51 int unit;
52 int mru;
53 int debug;
54 z_stream strm;
55 struct compstat stats;
58 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
60 static void *z_comp_alloc(unsigned char *options, int opt_len);
61 static void *z_decomp_alloc(unsigned char *options, int opt_len);
62 static void z_comp_free(void *state);
63 static void z_decomp_free(void *state);
64 static int z_comp_init(void *state, unsigned char *options,
65 int opt_len,
66 int unit, int hdrlen, int debug);
67 static int z_decomp_init(void *state, unsigned char *options,
68 int opt_len,
69 int unit, int hdrlen, int mru, int debug);
70 static int z_compress(void *state, unsigned char *rptr,
71 unsigned char *obuf,
72 int isize, int osize);
73 static void z_incomp(void *state, unsigned char *ibuf, int icnt);
74 static int z_decompress(void *state, unsigned char *ibuf,
75 int isize, unsigned char *obuf, int osize);
76 static void z_comp_reset(void *state);
77 static void z_decomp_reset(void *state);
78 static void z_comp_stats(void *state, struct compstat *stats);
80 /**
81 * z_comp_free - free the memory used by a compressor
82 * @arg: pointer to the private state for the compressor.
84 static void z_comp_free(void *arg)
86 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
88 if (state) {
89 zlib_deflateEnd(&state->strm);
90 if (state->strm.workspace)
91 vfree(state->strm.workspace);
92 kfree(state);
96 /**
97 * z_comp_alloc - allocate space for a compressor.
98 * @options: pointer to CCP option data
99 * @opt_len: length of the CCP option at @options.
101 * The @options pointer points to the a buffer containing the
102 * CCP option data for the compression being negotiated. It is
103 * formatted according to RFC1979, and describes the window
104 * size that the peer is requesting that we use in compressing
105 * data to be sent to it.
107 * Returns the pointer to the private state for the compressor,
108 * or NULL if we could not allocate enough memory.
110 static void *z_comp_alloc(unsigned char *options, int opt_len)
112 struct ppp_deflate_state *state;
113 int w_size;
115 if (opt_len != CILEN_DEFLATE
116 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
117 || options[1] != CILEN_DEFLATE
118 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
119 || options[3] != DEFLATE_CHK_SEQUENCE)
120 return NULL;
121 w_size = DEFLATE_SIZE(options[2]);
122 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
123 return NULL;
125 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state),
126 GFP_KERNEL);
127 if (state == NULL)
128 return NULL;
130 memset (state, 0, sizeof (struct ppp_deflate_state));
131 state->strm.next_in = NULL;
132 state->w_size = w_size;
133 state->strm.workspace = vmalloc(zlib_deflate_workspacesize());
134 if (state->strm.workspace == NULL)
135 goto out_free;
137 if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
138 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
139 != Z_OK)
140 goto out_free;
141 return (void *) state;
143 out_free:
144 z_comp_free(state);
145 return NULL;
149 * z_comp_init - initialize a previously-allocated compressor.
150 * @arg: pointer to the private state for the compressor
151 * @options: pointer to the CCP option data describing the
152 * compression that was negotiated with the peer
153 * @opt_len: length of the CCP option data at @options
154 * @unit: PPP unit number for diagnostic messages
155 * @hdrlen: ignored (present for backwards compatibility)
156 * @debug: debug flag; if non-zero, debug messages are printed.
158 * The CCP options described by @options must match the options
159 * specified when the compressor was allocated. The compressor
160 * history is reset. Returns 0 for failure (CCP options don't
161 * match) or 1 for success.
163 static int z_comp_init(void *arg, unsigned char *options, int opt_len,
164 int unit, int hdrlen, int debug)
166 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
168 if (opt_len < CILEN_DEFLATE
169 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
170 || options[1] != CILEN_DEFLATE
171 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
172 || DEFLATE_SIZE(options[2]) != state->w_size
173 || options[3] != DEFLATE_CHK_SEQUENCE)
174 return 0;
176 state->seqno = 0;
177 state->unit = unit;
178 state->debug = debug;
180 zlib_deflateReset(&state->strm);
182 return 1;
186 * z_comp_reset - reset a previously-allocated compressor.
187 * @arg: pointer to private state for the compressor.
189 * This clears the history for the compressor and makes it
190 * ready to start emitting a new compressed stream.
192 static void z_comp_reset(void *arg)
194 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
196 state->seqno = 0;
197 zlib_deflateReset(&state->strm);
201 * z_compress - compress a PPP packet with Deflate compression.
202 * @arg: pointer to private state for the compressor
203 * @rptr: uncompressed packet (input)
204 * @obuf: compressed packet (output)
205 * @isize: size of uncompressed packet
206 * @osize: space available at @obuf
208 * Returns the length of the compressed packet, or 0 if the
209 * packet is incompressible.
211 int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
212 int isize, int osize)
214 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
215 int r, proto, off, olen, oavail;
216 unsigned char *wptr;
219 * Check that the protocol is in the range we handle.
221 proto = PPP_PROTOCOL(rptr);
222 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
223 return 0;
225 /* Don't generate compressed packets which are larger than
226 the uncompressed packet. */
227 if (osize > isize)
228 osize = isize;
230 wptr = obuf;
233 * Copy over the PPP header and store the 2-byte sequence number.
235 wptr[0] = PPP_ADDRESS(rptr);
236 wptr[1] = PPP_CONTROL(rptr);
237 wptr[2] = PPP_COMP >> 8;
238 wptr[3] = PPP_COMP;
239 wptr += PPP_HDRLEN;
240 wptr[0] = state->seqno >> 8;
241 wptr[1] = state->seqno;
242 wptr += DEFLATE_OVHD;
243 olen = PPP_HDRLEN + DEFLATE_OVHD;
244 state->strm.next_out = wptr;
245 state->strm.avail_out = oavail = osize - olen;
246 ++state->seqno;
248 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
249 rptr += off;
250 state->strm.next_in = rptr;
251 state->strm.avail_in = (isize - off);
253 for (;;) {
254 r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
255 if (r != Z_OK) {
256 if (state->debug)
257 printk(KERN_ERR
258 "z_compress: deflate returned %d\n", r);
259 break;
261 if (state->strm.avail_out == 0) {
262 olen += oavail;
263 state->strm.next_out = NULL;
264 state->strm.avail_out = oavail = 1000000;
265 } else {
266 break; /* all done */
269 olen += oavail - state->strm.avail_out;
272 * See if we managed to reduce the size of the packet.
274 if (olen < isize) {
275 state->stats.comp_bytes += olen;
276 state->stats.comp_packets++;
277 } else {
278 state->stats.inc_bytes += isize;
279 state->stats.inc_packets++;
280 olen = 0;
282 state->stats.unc_bytes += isize;
283 state->stats.unc_packets++;
285 return olen;
289 * z_comp_stats - return compression statistics for a compressor
290 * or decompressor.
291 * @arg: pointer to private space for the (de)compressor
292 * @stats: pointer to a struct compstat to receive the result.
294 static void z_comp_stats(void *arg, struct compstat *stats)
296 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
298 *stats = state->stats;
302 * z_decomp_free - Free the memory used by a decompressor.
303 * @arg: pointer to private space for the decompressor.
305 static void z_decomp_free(void *arg)
307 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
309 if (state) {
310 zlib_inflateEnd(&state->strm);
311 if (state->strm.workspace)
312 kfree(state->strm.workspace);
313 kfree(state);
318 * z_decomp_alloc - allocate space for a decompressor.
319 * @options: pointer to CCP option data
320 * @opt_len: length of the CCP option at @options.
322 * The @options pointer points to the a buffer containing the
323 * CCP option data for the compression being negotiated. It is
324 * formatted according to RFC1979, and describes the window
325 * size that we are requesting the peer to use in compressing
326 * data to be sent to us.
328 * Returns the pointer to the private state for the decompressor,
329 * or NULL if we could not allocate enough memory.
331 static void *z_decomp_alloc(unsigned char *options, int opt_len)
333 struct ppp_deflate_state *state;
334 int w_size;
336 if (opt_len != CILEN_DEFLATE
337 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
338 || options[1] != CILEN_DEFLATE
339 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
340 || options[3] != DEFLATE_CHK_SEQUENCE)
341 return NULL;
342 w_size = DEFLATE_SIZE(options[2]);
343 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
344 return NULL;
346 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
347 if (state == NULL)
348 return NULL;
350 memset (state, 0, sizeof (struct ppp_deflate_state));
351 state->w_size = w_size;
352 state->strm.next_out = NULL;
353 state->strm.workspace = kmalloc(zlib_inflate_workspacesize(),
354 GFP_KERNEL|__GFP_REPEAT);
355 if (state->strm.workspace == NULL)
356 goto out_free;
358 if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
359 goto out_free;
360 return (void *) state;
362 out_free:
363 z_decomp_free(state);
364 return NULL;
368 * z_decomp_init - initialize a previously-allocated decompressor.
369 * @arg: pointer to the private state for the decompressor
370 * @options: pointer to the CCP option data describing the
371 * compression that was negotiated with the peer
372 * @opt_len: length of the CCP option data at @options
373 * @unit: PPP unit number for diagnostic messages
374 * @hdrlen: ignored (present for backwards compatibility)
375 * @mru: maximum length of decompressed packets
376 * @debug: debug flag; if non-zero, debug messages are printed.
378 * The CCP options described by @options must match the options
379 * specified when the decompressor was allocated. The decompressor
380 * history is reset. Returns 0 for failure (CCP options don't
381 * match) or 1 for success.
383 static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
384 int unit, int hdrlen, int mru, int debug)
386 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
388 if (opt_len < CILEN_DEFLATE
389 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
390 || options[1] != CILEN_DEFLATE
391 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
392 || DEFLATE_SIZE(options[2]) != state->w_size
393 || options[3] != DEFLATE_CHK_SEQUENCE)
394 return 0;
396 state->seqno = 0;
397 state->unit = unit;
398 state->debug = debug;
399 state->mru = mru;
401 zlib_inflateReset(&state->strm);
403 return 1;
407 * z_decomp_reset - reset a previously-allocated decompressor.
408 * @arg: pointer to private state for the decompressor.
410 * This clears the history for the decompressor and makes it
411 * ready to receive a new compressed stream.
413 static void z_decomp_reset(void *arg)
415 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
417 state->seqno = 0;
418 zlib_inflateReset(&state->strm);
422 * z_decompress - decompress a Deflate-compressed packet.
423 * @arg: pointer to private state for the decompressor
424 * @ibuf: pointer to input (compressed) packet data
425 * @isize: length of input packet
426 * @obuf: pointer to space for output (decompressed) packet
427 * @osize: amount of space available at @obuf
429 * Because of patent problems, we return DECOMP_ERROR for errors
430 * found by inspecting the input data and for system problems, but
431 * DECOMP_FATALERROR for any errors which could possibly be said to
432 * be being detected "after" decompression. For DECOMP_ERROR,
433 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
434 * infringing a patent of Motorola's if we do, so we take CCP down
435 * instead.
437 * Given that the frame has the correct sequence number and a good FCS,
438 * errors such as invalid codes in the input most likely indicate a
439 * bug, so we return DECOMP_FATALERROR for them in order to turn off
440 * compression, even though they are detected by inspecting the input.
442 int z_decompress(void *arg, unsigned char *ibuf, int isize,
443 unsigned char *obuf, int osize)
445 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
446 int olen, seq, r;
447 int decode_proto, overflow;
448 unsigned char overflow_buf[1];
450 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
451 if (state->debug)
452 printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
453 state->unit, isize);
454 return DECOMP_ERROR;
457 /* Check the sequence number. */
458 seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
459 if (seq != (state->seqno & 0xffff)) {
460 if (state->debug)
461 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
462 state->unit, seq, state->seqno & 0xffff);
463 return DECOMP_ERROR;
465 ++state->seqno;
468 * Fill in the first part of the PPP header. The protocol field
469 * comes from the decompressed data.
471 obuf[0] = PPP_ADDRESS(ibuf);
472 obuf[1] = PPP_CONTROL(ibuf);
473 obuf[2] = 0;
476 * Set up to call inflate. We set avail_out to 1 initially so we can
477 * look at the first byte of the output and decide whether we have
478 * a 1-byte or 2-byte protocol field.
480 state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
481 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
482 state->strm.next_out = obuf + 3;
483 state->strm.avail_out = 1;
484 decode_proto = 1;
485 overflow = 0;
488 * Call inflate, supplying more input or output as needed.
490 for (;;) {
491 r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
492 if (r != Z_OK) {
493 if (state->debug)
494 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
495 state->unit, r, (state->strm.msg? state->strm.msg: ""));
496 return DECOMP_FATALERROR;
498 if (state->strm.avail_out != 0)
499 break; /* all done */
500 if (decode_proto) {
501 state->strm.avail_out = osize - PPP_HDRLEN;
502 if ((obuf[3] & 1) == 0) {
503 /* 2-byte protocol field */
504 obuf[2] = obuf[3];
505 --state->strm.next_out;
506 ++state->strm.avail_out;
508 decode_proto = 0;
509 } else if (!overflow) {
511 * We've filled up the output buffer; the only way to
512 * find out whether inflate has any more characters
513 * left is to give it another byte of output space.
515 state->strm.next_out = overflow_buf;
516 state->strm.avail_out = 1;
517 overflow = 1;
518 } else {
519 if (state->debug)
520 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
521 state->unit);
522 return DECOMP_FATALERROR;
526 if (decode_proto) {
527 if (state->debug)
528 printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
529 state->unit);
530 return DECOMP_ERROR;
533 olen = osize + overflow - state->strm.avail_out;
534 state->stats.unc_bytes += olen;
535 state->stats.unc_packets++;
536 state->stats.comp_bytes += isize;
537 state->stats.comp_packets++;
539 return olen;
543 * z_incomp - add incompressible input data to the history.
544 * @arg: pointer to private state for the decompressor
545 * @ibuf: pointer to input packet data
546 * @icnt: length of input data.
548 static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
550 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
551 int proto, r;
554 * Check that the protocol is one we handle.
556 proto = PPP_PROTOCOL(ibuf);
557 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
558 return;
560 ++state->seqno;
563 * We start at the either the 1st or 2nd byte of the protocol field,
564 * depending on whether the protocol value is compressible.
566 state->strm.next_in = ibuf + 3;
567 state->strm.avail_in = icnt - 3;
568 if (proto > 0xff) {
569 --state->strm.next_in;
570 ++state->strm.avail_in;
573 r = zlib_inflateIncomp(&state->strm);
574 if (r != Z_OK) {
575 /* gak! */
576 if (state->debug) {
577 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
578 state->unit, r, (state->strm.msg? state->strm.msg: ""));
580 return;
584 * Update stats.
586 state->stats.inc_bytes += icnt;
587 state->stats.inc_packets++;
588 state->stats.unc_bytes += icnt;
589 state->stats.unc_packets++;
592 /*************************************************************
593 * Module interface table
594 *************************************************************/
596 /* These are in ppp_generic.c */
597 extern int ppp_register_compressor (struct compressor *cp);
598 extern void ppp_unregister_compressor (struct compressor *cp);
601 * Procedures exported to if_ppp.c.
603 struct compressor ppp_deflate = {
604 .compress_proto = CI_DEFLATE,
605 .comp_alloc = z_comp_alloc,
606 .comp_free = z_comp_free,
607 .comp_init = z_comp_init,
608 .comp_reset = z_comp_reset,
609 .compress = z_compress,
610 .comp_stat = z_comp_stats,
611 .decomp_alloc = z_decomp_alloc,
612 .decomp_free = z_decomp_free,
613 .decomp_init = z_decomp_init,
614 .decomp_reset = z_decomp_reset,
615 .decompress = z_decompress,
616 .incomp = z_incomp,
617 .decomp_stat = z_comp_stats,
618 .owner = THIS_MODULE
621 struct compressor ppp_deflate_draft = {
622 .compress_proto = CI_DEFLATE_DRAFT,
623 .comp_alloc = z_comp_alloc,
624 .comp_free = z_comp_free,
625 .comp_init = z_comp_init,
626 .comp_reset = z_comp_reset,
627 .compress = z_compress,
628 .comp_stat = z_comp_stats,
629 .decomp_alloc = z_decomp_alloc,
630 .decomp_free = z_decomp_free,
631 .decomp_init = z_decomp_init,
632 .decomp_reset = z_decomp_reset,
633 .decompress = z_decompress,
634 .incomp = z_incomp,
635 .decomp_stat = z_comp_stats,
636 .owner = THIS_MODULE
639 int __init deflate_init(void)
641 int answer = ppp_register_compressor(&ppp_deflate);
642 if (answer == 0)
643 printk(KERN_INFO
644 "PPP Deflate Compression module registered\n");
645 ppp_register_compressor(&ppp_deflate_draft);
646 return answer;
649 void __exit deflate_cleanup(void)
651 ppp_unregister_compressor(&ppp_deflate);
652 ppp_unregister_compressor(&ppp_deflate_draft);
655 module_init(deflate_init);
656 module_exit(deflate_cleanup);
657 MODULE_LICENSE("Dual BSD/GPL");
658 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
659 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));