2 * ==FILEVERSION 960302==
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.
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
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
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,
31 * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
34 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/ioport.h>
45 #include <linux/malloc.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h> /* to get the struct task_struct */
49 #include <linux/string.h> /* used in new tty drivers */
50 #include <linux/signal.h> /* used in new tty drivers */
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <asm/segment.h>
58 #include <linux/if_ether.h>
59 #include <linux/netdevice.h>
60 #include <linux/skbuff.h>
61 #include <linux/inet.h>
62 #include <linux/ioctl.h>
64 #include <linux/ppp_defs.h>
67 #include <linux/netprotocol.h>
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
72 #include <net/if_arp.h>
76 #include <linux/ppp-comp.h>
82 * State for a Deflate (de)compressor.
84 struct ppp_deflate_state {
91 struct compstat stats;
94 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
96 static void *zalloc __P((void *, unsigned int items, unsigned int size));
97 static void zfree __P((void *, void *ptr, unsigned int nb));
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));
119 * This is a small set of memory allocation routines. I created them so
120 * that all memory allocation from the kernel takes place at the
121 * z_(de)comp_alloc and z_(de)comp_free routines. This eliminates worry
122 * about calling valloc() from within an interrupt.
124 * The free list is a single linked list sorted by memory address.
125 * The zfree() function re-combines any segments it can.
127 typedef struct memchunk {
129 struct memchunk *m_next;
137 static int memPoolAlloc __P((void *arg, unsigned int size));
138 static void memPoolFree __P((void *arg));
141 memPoolAlloc(arg, size)
145 MemPool **memPool = arg;
148 if ((*memPool = kmalloc(sizeof(MemPool), GFP_KERNEL)) == NULL) {
149 printk(KERN_DEBUG "Unable to allocate Memory Head\n");
153 if (((*memPool)->memHead = (void *)vmalloc(size)) == NULL) {
154 printk(KERN_DEBUG "Unable to allocate Memory Pool\n");
158 freePool = (*memPool)->freePool = (*memPool)->memHead;
159 freePool->m_size = size;
160 freePool->m_next = 0;
169 MemPool **memPool = arg;
172 vfree((*memPool)->memHead);
179 static void showFreeList __P((MemChunk *));
182 showFreeList(freePool)
187 for (node = freePool; node; node = node->m_next)
188 printk(KERN_DEBUG "{%x,%d}->", node, node->m_size);
189 printk(KERN_DEBUG "\n");
194 * Space allocation and freeing routines for use by zlib routines.
197 zfree(arg, ptr, nbytes)
202 MemPool *memPool = (MemPool *)arg;
203 MemChunk *mprev = 0, *node;
204 MemChunk *new = (void *)(((unsigned char *)ptr) - sizeof(MemChunk));
206 if (!memPool->freePool) {
208 memPool->freePool = new;
211 * Find where this new chunk fits in the free list.
213 for (node = memPool->freePool; node && new > node; node = node->m_next)
216 * Re-combine with the following free chunk if possible.
218 if ((((unsigned char *)new) + new->m_size) == (unsigned char *)node) {
219 new->m_size += node->m_size;
220 new->m_next = node->m_next;
222 if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)new) {
223 mprev->m_size += new->m_size;
224 mprev->m_next = new->m_next;
228 memPool->freePool = new;
230 * Re-combine with the previous free chunk if possible.
232 } else if (mprev && (((unsigned char *)mprev) + mprev->m_size) ==
233 (unsigned char *)new) {
234 mprev->m_size += new->m_size;
235 if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)node) {
236 mprev->m_size += node->m_size;
237 mprev->m_next = node->m_next;
239 mprev->m_next = node;
241 * No luck re-combining, just insert the new chunk into the list.
247 memPool->freePool = new;
254 zalloc(arg, items, size)
256 unsigned int items, size;
258 MemPool *memPool = (MemPool *)arg;
259 MemChunk *mprev = 0, *node;
262 size += sizeof(MemChunk);
264 size = (size + 7) & ~3;
265 for (node = memPool->freePool; node; node = node->m_next) {
266 if (size == node->m_size) {
268 mprev->m_next = node->m_next;
270 memPool->freePool = node->m_next;
271 return (void *)(((unsigned char *)node)+sizeof(MemChunk));
272 } else if (node->m_size > (size + sizeof(MemChunk) + 7)) {
275 new = (void *)(((unsigned char *)node) + size);
276 new->m_size = node->m_size - size;
277 new->m_next = node->m_next;
281 memPool->freePool = new;
285 return (void *)(((unsigned char *)node)+sizeof(MemChunk));
291 "zalloc(%d)... Out of memory in Pool!\n", size - sizeof(MemChunk));
299 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
302 deflateEnd(&state->strm);
303 memPoolFree(&state->strm.opaque);
310 * Allocate space for a compressor.
313 z_comp_alloc(options, opt_len)
314 unsigned char *options;
317 struct ppp_deflate_state *state;
320 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
321 || options[1] != CILEN_DEFLATE
322 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
323 || options[3] != DEFLATE_CHK_SEQUENCE)
325 w_size = DEFLATE_SIZE(options[2]);
326 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
329 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
334 memset (state, 0, sizeof (struct ppp_deflate_state));
335 state->strm.next_in = NULL;
336 state->strm.zalloc = zalloc;
337 state->strm.zfree = zfree;
338 state->w_size = w_size;
340 if (!memPoolAlloc(&state->strm.opaque, 0x50000)) {
345 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
346 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
350 return (void *) state;
354 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
356 unsigned char *options;
357 int opt_len, unit, hdrlen, debug;
359 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
361 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
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)
370 state->debug = debug;
372 deflateReset(&state->strm);
381 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
383 deflateReset(&state->strm);
387 z_compress(arg, rptr, obuf, isize, osize)
389 unsigned char *rptr; /* uncompressed packet (in) */
390 unsigned char *obuf; /* compressed packet (out) */
393 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
394 int r, proto, off, olen;
398 * Check that the protocol is in the range we handle.
400 proto = PPP_PROTOCOL(rptr);
401 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
404 /* Don't generate compressed packets which are larger than
405 the uncompressed packet. */
412 * Copy over the PPP header and store the 2-byte sequence number.
414 wptr[0] = PPP_ADDRESS(rptr);
415 wptr[1] = PPP_CONTROL(rptr);
416 wptr[2] = PPP_COMP >> 8;
419 wptr[0] = state->seqno >> 8;
420 wptr[1] = state->seqno;
422 state->strm.next_out = wptr;
423 state->strm.avail_out = osize - (PPP_HDRLEN + 2);
426 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
428 state->strm.next_in = rptr;
429 state->strm.avail_in = (isize - off);
433 r = deflate(&state->strm, Z_PACKET_FLUSH);
435 printk(KERN_DEBUG "z_compress: deflate returned %d (%s)\n",
436 r, (state->strm.msg? state->strm.msg: ""));
439 if (state->strm.avail_out == 0) {
441 state->strm.next_out = NULL;
442 state->strm.avail_out = 1000000;
444 break; /* all done */
448 olen += osize - state->strm.avail_out;
451 * See if we managed to reduce the size of the packet.
452 * If the compressor just gave us a single zero byte, it means
453 * the packet was incompressible.
455 if (olen < isize && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
456 state->stats.comp_bytes += olen;
457 state->stats.comp_packets++;
459 state->stats.inc_bytes += isize;
460 state->stats.inc_packets++;
463 state->stats.unc_bytes += isize;
464 state->stats.unc_packets++;
470 z_comp_stats(arg, stats)
472 struct compstat *stats;
474 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
476 *stats = state->stats;
483 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
486 inflateEnd(&state->strm);
487 memPoolFree(&state->strm.opaque);
494 * Allocate space for a decompressor.
497 z_decomp_alloc(options, opt_len)
498 unsigned char *options;
501 struct ppp_deflate_state *state;
504 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
505 || options[1] != CILEN_DEFLATE
506 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
507 || options[3] != DEFLATE_CHK_SEQUENCE)
509 w_size = DEFLATE_SIZE(options[2]);
510 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
513 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
518 memset (state, 0, sizeof (struct ppp_deflate_state));
519 state->w_size = w_size;
520 state->strm.next_out = NULL;
521 state->strm.zalloc = zalloc;
522 state->strm.zfree = zfree;
524 if (!memPoolAlloc(&state->strm.opaque, 0x10000)) {
525 z_decomp_free(state);
529 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
530 z_decomp_free(state);
533 return (void *) state;
537 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
539 unsigned char *options;
540 int opt_len, unit, hdrlen, mru, debug;
542 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
544 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
545 || options[1] != CILEN_DEFLATE
546 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
547 || DEFLATE_SIZE(options[2]) != state->w_size
548 || options[3] != DEFLATE_CHK_SEQUENCE)
553 state->debug = debug;
556 inflateReset(&state->strm);
565 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
568 inflateReset(&state->strm);
572 * Decompress a Deflate-compressed packet.
574 * Because of patent problems, we return DECOMP_ERROR for errors
575 * found by inspecting the input data and for system problems, but
576 * DECOMP_FATALERROR for any errors which could possibly be said to
577 * be being detected "after" decompression. For DECOMP_ERROR,
578 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
579 * infringing a patent of Motorola's if we do, so we take CCP down
582 * Given that the frame has the correct sequence number and a good FCS,
583 * errors such as invalid codes in the input most likely indicate a
584 * bug, so we return DECOMP_FATALERROR for them in order to turn off
585 * compression, even though they are detected by inspecting the input.
588 z_decompress(arg, ibuf, isize, obuf, osize)
595 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
596 int olen, seq, i, r, decode_proto;
597 unsigned char hdr[PPP_HDRLEN + DEFLATE_OVHD];
598 unsigned char *rptr, *wptr;
602 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i)
605 /* Check the sequence number. */
606 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
607 if (seq != state->seqno) {
609 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
610 state->unit, seq, state->seqno);
616 * Fill in the first part of the PPP header. The protocol field
617 * comes from the decompressed data.
619 wptr[0] = PPP_ADDRESS(hdr);
620 wptr[1] = PPP_CONTROL(hdr);
624 * Set up to call inflate. We set avail_out to 1 initially so we can
625 * look at the first byte of the output and decide whether we have
626 * a 1-byte or 2-byte protocol field.
628 state->strm.next_in = rptr;
629 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
630 state->strm.next_out = wptr + 3;
631 state->strm.avail_out = 1;
635 * Call inflate, supplying more input or output as needed.
638 r = inflate(&state->strm, Z_PACKET_FLUSH);
641 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
642 state->unit, r, (state->strm.msg? state->strm.msg: ""));
643 return DECOMP_FATALERROR;
645 if (state->strm.avail_out != 0)
646 break; /* all done */
647 if (state->strm.avail_out == 0) {
649 state->strm.avail_out = osize - PPP_HDRLEN;
650 if ((wptr[3] & 1) == 0) {
651 /* 2-byte protocol field */
653 --state->strm.next_out;
654 ++state->strm.avail_out;
659 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
661 return DECOMP_FATALERROR;
669 olen = (osize - state->strm.avail_out);
671 state->stats.unc_bytes += olen;
672 state->stats.unc_packets++;
673 state->stats.comp_bytes += isize;
674 state->stats.comp_packets++;
680 * Incompressible data has arrived - add it to the history.
683 z_incomp(arg, ibuf, icnt)
688 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
689 unsigned char *rptr = ibuf;
693 * Check that the protocol is one we handle.
695 proto = PPP_PROTOCOL(rptr);
696 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
702 * Iterate through the message blocks, adding the characters in them
703 * to the decompressor's history. For the first block, we start
704 * at the either the 1st or 2nd byte of the protocol field,
705 * depending on whether the protocol value is compressible.
707 state->strm.next_in = rptr + 3;
708 state->strm.avail_in = icnt - 3;
710 --state->strm.next_in;
711 ++state->strm.avail_in;
714 r = inflateIncomp(&state->strm);
718 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
719 state->unit, r, (state->strm.msg? state->strm.msg: ""));
727 state->stats.inc_bytes += icnt;
728 state->stats.inc_packets++;
729 state->stats.unc_bytes += icnt;
730 state->stats.unc_packets++;
733 /*************************************************************
734 * Module interface table
735 *************************************************************/
737 /* These are in ppp.c */
738 extern int ppp_register_compressor (struct compressor *cp);
739 extern void ppp_unregister_compressor (struct compressor *cp);
742 * Procedures exported to if_ppp.c.
744 struct compressor ppp_deflate = {
745 CI_DEFLATE, /* compress_proto */
746 z_comp_alloc, /* comp_alloc */
747 z_comp_free, /* comp_free */
748 z_comp_init, /* comp_init */
749 z_comp_reset, /* comp_reset */
750 z_compress, /* compress */
751 z_comp_stats, /* comp_stat */
752 z_decomp_alloc, /* decomp_alloc */
753 z_decomp_free, /* decomp_free */
754 z_decomp_init, /* decomp_init */
755 z_decomp_reset, /* decomp_reset */
756 z_decompress, /* decompress */
757 z_incomp, /* incomp */
758 z_comp_stats, /* decomp_stat */
761 /*************************************************************
762 * Module support routines
763 *************************************************************/
768 int answer = ppp_register_compressor (&ppp_deflate);
771 "PPP Deflate Compression module registered\n");
780 "Deflate Compression module busy, remove delayed\n");
782 ppp_unregister_compressor (&ppp_deflate);