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 and later.
8 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. The name(s) of the authors of this software must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission.
26 * 4. Redistributions of any form whatsoever must retain the following
28 * "This product includes software developed by Paul Mackerras
29 * <paulus@samba.org>".
31 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
32 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
34 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
42 #include <linux/module.h>
43 #include <linux/version.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/types.h>
47 #include <linux/fcntl.h>
48 #include <linux/interrupt.h>
49 #include <linux/ptrace.h>
50 #include <linux/ioport.h>
52 #include <linux/malloc.h>
55 /* a nice define to generate linux version numbers */
56 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
58 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
59 #include <linux/vmalloc.h>
62 #include <linux/errno.h>
63 #include <linux/sched.h> /* to get the struct task_struct */
64 #include <linux/string.h> /* used in new tty drivers */
65 #include <linux/signal.h> /* used in new tty drivers */
67 #include <asm/system.h>
69 #include <linux/netdevice.h>
70 #include <linux/skbuff.h>
71 #include <linux/inet.h>
72 #include <linux/ioctl.h>
74 #include <linux/ppp_defs.h>
75 #include <linux/ppp-comp.h>
80 * State for a Deflate (de)compressor.
82 struct ppp_deflate_state {
89 struct compstat stats;
92 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
94 static void *zalloc __P((void *, unsigned int items, unsigned int size));
95 static void *zalloc_init __P((void *, unsigned int items,
97 static void zfree __P((void *, void *ptr));
98 static void *z_comp_alloc __P((unsigned char *options, int opt_len));
99 static void *z_decomp_alloc __P((unsigned char *options, int opt_len));
100 static void z_comp_free __P((void *state));
101 static void z_decomp_free __P((void *state));
102 static int z_comp_init __P((void *state, unsigned char *options,
104 int unit, int hdrlen, int debug));
105 static int z_decomp_init __P((void *state, unsigned char *options,
107 int unit, int hdrlen, int mru, int debug));
108 static int z_compress __P((void *state, unsigned char *rptr,
110 int isize, int osize));
111 static void z_incomp __P((void *state, unsigned char *ibuf, int icnt));
112 static int z_decompress __P((void *state, unsigned char *ibuf,
113 int isize, unsigned char *obuf, int osize));
114 static void z_comp_reset __P((void *state));
115 static void z_decomp_reset __P((void *state));
116 static void z_comp_stats __P((void *state, struct compstat *stats));
118 struct chunk_header {
119 int valloced; /* allocated with valloc, not kmalloc */
120 int guard; /* check for overwritten header */
123 #define GUARD_MAGIC 0x77a8011a
124 #define MIN_VMALLOC 2048 /* use kmalloc for blocks < this */
127 * Space allocation and freeing routines for use by zlib routines.
134 struct chunk_header *hdr = ((struct chunk_header *)ptr) - 1;
136 if (hdr->guard != GUARD_MAGIC) {
137 printk(KERN_WARNING "zfree: header corrupted (%x %x) at %p\n",
138 hdr->valloced, hdr->guard, hdr);
148 zalloc(arg, items, size)
150 unsigned int items, size;
152 struct chunk_header *hdr;
155 nbytes = items * size + sizeof(*hdr);
156 hdr = kmalloc(nbytes, GFP_ATOMIC);
160 hdr->guard = GUARD_MAGIC;
161 return (void *) (hdr + 1);
165 zalloc_init(arg, items, size)
167 unsigned int items, size;
169 struct chunk_header *hdr;
172 nbytes = items * size + sizeof(*hdr);
173 if (nbytes >= MIN_VMALLOC)
174 hdr = vmalloc(nbytes);
176 hdr = kmalloc(nbytes, GFP_KERNEL);
179 hdr->valloced = nbytes >= MIN_VMALLOC;
180 hdr->guard = GUARD_MAGIC;
181 return (void *) (hdr + 1);
188 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
191 deflateEnd(&state->strm);
198 * Allocate space for a compressor.
201 z_comp_alloc(options, opt_len)
202 unsigned char *options;
205 struct ppp_deflate_state *state;
208 if (opt_len != CILEN_DEFLATE
209 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
210 || options[1] != CILEN_DEFLATE
211 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
212 || options[3] != DEFLATE_CHK_SEQUENCE)
214 w_size = DEFLATE_SIZE(options[2]);
215 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
218 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
223 memset (state, 0, sizeof (struct ppp_deflate_state));
224 state->strm.next_in = NULL;
225 state->strm.zalloc = zalloc_init;
226 state->strm.zfree = zfree;
227 state->w_size = w_size;
229 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
230 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
233 state->strm.zalloc = zalloc;
234 return (void *) state;
243 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
245 unsigned char *options;
246 int opt_len, unit, hdrlen, debug;
248 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
250 if (opt_len < CILEN_DEFLATE
251 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
252 || options[1] != CILEN_DEFLATE
253 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
254 || DEFLATE_SIZE(options[2]) != state->w_size
255 || options[3] != DEFLATE_CHK_SEQUENCE)
260 state->debug = debug;
262 deflateReset(&state->strm);
271 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
274 deflateReset(&state->strm);
278 z_compress(arg, rptr, obuf, isize, osize)
280 unsigned char *rptr; /* uncompressed packet (in) */
281 unsigned char *obuf; /* compressed packet (out) */
284 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
285 int r, proto, off, olen, oavail;
289 * Check that the protocol is in the range we handle.
291 proto = PPP_PROTOCOL(rptr);
292 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
295 /* Don't generate compressed packets which are larger than
296 the uncompressed packet. */
303 * Copy over the PPP header and store the 2-byte sequence number.
305 wptr[0] = PPP_ADDRESS(rptr);
306 wptr[1] = PPP_CONTROL(rptr);
307 wptr[2] = PPP_COMP >> 8;
310 wptr[0] = state->seqno >> 8;
311 wptr[1] = state->seqno;
312 wptr += DEFLATE_OVHD;
313 olen = PPP_HDRLEN + DEFLATE_OVHD;
314 state->strm.next_out = wptr;
315 state->strm.avail_out = oavail = osize - olen;
318 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
320 state->strm.next_in = rptr;
321 state->strm.avail_in = (isize - off);
324 r = deflate(&state->strm, Z_PACKET_FLUSH);
328 "z_compress: deflate returned %d\n", r);
331 if (state->strm.avail_out == 0) {
333 state->strm.next_out = NULL;
334 state->strm.avail_out = oavail = 1000000;
336 break; /* all done */
339 olen += oavail - state->strm.avail_out;
342 * See if we managed to reduce the size of the packet.
345 state->stats.comp_bytes += olen;
346 state->stats.comp_packets++;
348 state->stats.inc_bytes += isize;
349 state->stats.inc_packets++;
352 state->stats.unc_bytes += isize;
353 state->stats.unc_packets++;
359 z_comp_stats(arg, stats)
361 struct compstat *stats;
363 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
365 *stats = state->stats;
372 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
375 inflateEnd(&state->strm);
382 * Allocate space for a decompressor.
385 z_decomp_alloc(options, opt_len)
386 unsigned char *options;
389 struct ppp_deflate_state *state;
392 if (opt_len != CILEN_DEFLATE
393 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
394 || options[1] != CILEN_DEFLATE
395 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
396 || options[3] != DEFLATE_CHK_SEQUENCE)
398 w_size = DEFLATE_SIZE(options[2]);
399 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
402 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
407 memset (state, 0, sizeof (struct ppp_deflate_state));
408 state->w_size = w_size;
409 state->strm.next_out = NULL;
410 state->strm.zalloc = zalloc_init;
411 state->strm.zfree = zfree;
413 if (inflateInit2(&state->strm, -w_size) != Z_OK)
415 state->strm.zalloc = zalloc;
416 return (void *) state;
419 z_decomp_free(state);
425 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
427 unsigned char *options;
428 int opt_len, unit, hdrlen, mru, debug;
430 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
432 if (opt_len < CILEN_DEFLATE
433 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
434 || options[1] != CILEN_DEFLATE
435 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
436 || DEFLATE_SIZE(options[2]) != state->w_size
437 || options[3] != DEFLATE_CHK_SEQUENCE)
442 state->debug = debug;
445 inflateReset(&state->strm);
454 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
457 inflateReset(&state->strm);
461 * Decompress a Deflate-compressed packet.
463 * Because of patent problems, we return DECOMP_ERROR for errors
464 * found by inspecting the input data and for system problems, but
465 * DECOMP_FATALERROR for any errors which could possibly be said to
466 * be being detected "after" decompression. For DECOMP_ERROR,
467 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
468 * infringing a patent of Motorola's if we do, so we take CCP down
471 * Given that the frame has the correct sequence number and a good FCS,
472 * errors such as invalid codes in the input most likely indicate a
473 * bug, so we return DECOMP_FATALERROR for them in order to turn off
474 * compression, even though they are detected by inspecting the input.
477 z_decompress(arg, ibuf, isize, obuf, osize)
484 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
486 int decode_proto, overflow;
487 unsigned char overflow_buf[1];
489 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
491 printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
496 /* Check the sequence number. */
497 seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
498 if (seq != state->seqno) {
500 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
501 state->unit, seq, state->seqno);
507 * Fill in the first part of the PPP header. The protocol field
508 * comes from the decompressed data.
510 obuf[0] = PPP_ADDRESS(ibuf);
511 obuf[1] = PPP_CONTROL(ibuf);
515 * Set up to call inflate. We set avail_out to 1 initially so we can
516 * look at the first byte of the output and decide whether we have
517 * a 1-byte or 2-byte protocol field.
519 state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
520 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
521 state->strm.next_out = obuf + 3;
522 state->strm.avail_out = 1;
527 * Call inflate, supplying more input or output as needed.
530 r = inflate(&state->strm, Z_PACKET_FLUSH);
533 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
534 state->unit, r, (state->strm.msg? state->strm.msg: ""));
535 return DECOMP_FATALERROR;
537 if (state->strm.avail_out != 0)
538 break; /* all done */
540 state->strm.avail_out = osize - PPP_HDRLEN;
541 if ((obuf[3] & 1) == 0) {
542 /* 2-byte protocol field */
544 --state->strm.next_out;
545 ++state->strm.avail_out;
548 } else if (!overflow) {
550 * We've filled up the output buffer; the only way to
551 * find out whether inflate has any more characters
552 * left is to give it another byte of output space.
554 state->strm.next_out = overflow_buf;
555 state->strm.avail_out = 1;
559 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
561 return DECOMP_FATALERROR;
567 printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
572 olen = osize + overflow - state->strm.avail_out;
573 state->stats.unc_bytes += olen;
574 state->stats.unc_packets++;
575 state->stats.comp_bytes += isize;
576 state->stats.comp_packets++;
582 * Incompressible data has arrived - add it to the history.
585 z_incomp(arg, ibuf, icnt)
590 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
594 * Check that the protocol is one we handle.
596 proto = PPP_PROTOCOL(ibuf);
597 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
603 * We start at the either the 1st or 2nd byte of the protocol field,
604 * depending on whether the protocol value is compressible.
606 state->strm.next_in = ibuf + 3;
607 state->strm.avail_in = icnt - 3;
609 --state->strm.next_in;
610 ++state->strm.avail_in;
613 r = inflateIncomp(&state->strm);
617 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
618 state->unit, r, (state->strm.msg? state->strm.msg: ""));
626 state->stats.inc_bytes += icnt;
627 state->stats.inc_packets++;
628 state->stats.unc_bytes += icnt;
629 state->stats.unc_packets++;
632 /*************************************************************
633 * Module interface table
634 *************************************************************/
636 /* These are in ppp.c */
637 extern int ppp_register_compressor (struct compressor *cp);
638 extern void ppp_unregister_compressor (struct compressor *cp);
641 * Procedures exported to if_ppp.c.
643 struct compressor ppp_deflate = {
644 CI_DEFLATE, /* compress_proto */
645 z_comp_alloc, /* comp_alloc */
646 z_comp_free, /* comp_free */
647 z_comp_init, /* comp_init */
648 z_comp_reset, /* comp_reset */
649 z_compress, /* compress */
650 z_comp_stats, /* comp_stat */
651 z_decomp_alloc, /* decomp_alloc */
652 z_decomp_free, /* decomp_free */
653 z_decomp_init, /* decomp_init */
654 z_decomp_reset, /* decomp_reset */
655 z_decompress, /* decompress */
656 z_incomp, /* incomp */
657 z_comp_stats, /* decomp_stat */
660 struct compressor ppp_deflate_draft = {
661 CI_DEFLATE_DRAFT, /* compress_proto */
662 z_comp_alloc, /* comp_alloc */
663 z_comp_free, /* comp_free */
664 z_comp_init, /* comp_init */
665 z_comp_reset, /* comp_reset */
666 z_compress, /* compress */
667 z_comp_stats, /* comp_stat */
668 z_decomp_alloc, /* decomp_alloc */
669 z_decomp_free, /* decomp_free */
670 z_decomp_init, /* decomp_init */
671 z_decomp_reset, /* decomp_reset */
672 z_decompress, /* decompress */
673 z_incomp, /* incomp */
674 z_comp_stats, /* decomp_stat */
678 /*************************************************************
679 * Module support routines
680 *************************************************************/
685 int answer = ppp_register_compressor (&ppp_deflate);
688 "PPP Deflate Compression module registered\n");
689 ppp_register_compressor(&ppp_deflate_draft);
698 "Deflate Compression module busy, remove delayed\n");
700 ppp_unregister_compressor (&ppp_deflate);
701 ppp_unregister_compressor (&ppp_deflate_draft);