2 * ==FILEVERSION 960926==
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>
73 #include <linux/ppp-comp.h>
77 #define USEMEMPOOL 0 /* use kmalloc, not memPool routines */
80 * State for a Deflate (de)compressor.
82 struct ppp_deflate_state {
88 int in_alloc; /* set when we're in [de]comp_alloc */
90 struct compstat stats;
93 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
95 static void *zalloc __P((void *, unsigned int items, unsigned int size));
96 static void zfree __P((void *, void *ptr, unsigned int nb));
97 static void *z_comp_alloc __P((unsigned char *options, int opt_len));
98 static void *z_decomp_alloc __P((unsigned char *options, int opt_len));
99 static void z_comp_free __P((void *state));
100 static void z_decomp_free __P((void *state));
101 static int z_comp_init __P((void *state, unsigned char *options,
103 int unit, int hdrlen, int debug));
104 static int z_decomp_init __P((void *state, unsigned char *options,
106 int unit, int hdrlen, int mru, int debug));
107 static int z_compress __P((void *state, unsigned char *rptr,
109 int isize, int osize));
110 static void z_incomp __P((void *state, unsigned char *ibuf, int icnt));
111 static int z_decompress __P((void *state, unsigned char *ibuf,
112 int isize, unsigned char *obuf, int osize));
113 static void z_comp_reset __P((void *state));
114 static void z_decomp_reset __P((void *state));
115 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");
192 #endif /* USEMEMPOOL */
195 * Space allocation and freeing routines for use by zlib routines.
198 zfree(arg, ptr, nbytes)
206 MemPool *memPool = (MemPool *)arg;
207 MemChunk *mprev = 0, *node;
208 MemChunk *new = (void *)(((unsigned char *)ptr) - sizeof(MemChunk));
210 if (!memPool->freePool) {
212 memPool->freePool = new;
215 * Find where this new chunk fits in the free list.
217 for (node = memPool->freePool; node && new > node; node = node->m_next)
220 * Re-combine with the following free chunk if possible.
222 if ((((unsigned char *)new) + new->m_size) == (unsigned char *)node) {
223 new->m_size += node->m_size;
224 new->m_next = node->m_next;
226 if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)new) {
227 mprev->m_size += new->m_size;
228 mprev->m_next = new->m_next;
232 memPool->freePool = new;
234 * Re-combine with the previous free chunk if possible.
236 } else if (mprev && (((unsigned char *)mprev) + mprev->m_size) ==
237 (unsigned char *)new) {
238 mprev->m_size += new->m_size;
239 if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)node) {
240 mprev->m_size += node->m_size;
241 mprev->m_next = node->m_next;
243 mprev->m_next = node;
245 * No luck re-combining, just insert the new chunk into the list.
251 memPool->freePool = new;
255 #endif /* USEMEMPOOL */
259 zalloc(arg, items, size)
261 unsigned int items, size;
264 struct ppp_deflate_state *state = arg;
266 return kmalloc(items * size, state->in_alloc? GFP_KERNEL: GFP_ATOMIC);
268 MemPool *memPool = (MemPool *)arg;
269 MemChunk *mprev = 0, *node;
272 size += sizeof(MemChunk);
274 size = (size + 7) & ~3;
275 for (node = memPool->freePool; node; node = node->m_next) {
276 if (size == node->m_size) {
278 mprev->m_next = node->m_next;
280 memPool->freePool = node->m_next;
281 return (void *)(((unsigned char *)node)+sizeof(MemChunk));
282 } else if (node->m_size > (size + sizeof(MemChunk) + 7)) {
285 new = (void *)(((unsigned char *)node) + size);
286 new->m_size = node->m_size - size;
287 new->m_next = node->m_next;
291 memPool->freePool = new;
295 return (void *)(((unsigned char *)node)+sizeof(MemChunk));
301 "zalloc(%d)... Out of memory in Pool!\n", size - sizeof(MemChunk));
303 #endif /* USEMEMPOOL */
310 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
313 deflateEnd(&state->strm);
315 memPoolFree(&state->strm.opaque);
323 * Allocate space for a compressor.
326 z_comp_alloc(options, opt_len)
327 unsigned char *options;
330 struct ppp_deflate_state *state;
333 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
334 || options[1] != CILEN_DEFLATE
335 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
336 || options[3] != DEFLATE_CHK_SEQUENCE)
338 w_size = DEFLATE_SIZE(options[2]);
339 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
342 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
347 memset (state, 0, sizeof (struct ppp_deflate_state));
348 state->strm.next_in = NULL;
349 state->strm.zalloc = zalloc;
350 state->strm.zfree = zfree;
351 state->w_size = w_size;
355 if (!memPoolAlloc(&state->strm.opaque, 0x50000)) {
360 state->strm.opaque = state;
363 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
364 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
369 return (void *) state;
373 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
375 unsigned char *options;
376 int opt_len, unit, hdrlen, debug;
378 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
380 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
381 || options[1] != CILEN_DEFLATE
382 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
383 || DEFLATE_SIZE(options[2]) != state->w_size
384 || options[3] != DEFLATE_CHK_SEQUENCE)
389 state->debug = debug;
391 deflateReset(&state->strm);
400 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
403 deflateReset(&state->strm);
407 z_compress(arg, rptr, obuf, isize, osize)
409 unsigned char *rptr; /* uncompressed packet (in) */
410 unsigned char *obuf; /* compressed packet (out) */
413 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
414 int r, proto, off, olen;
418 * Check that the protocol is in the range we handle.
420 proto = PPP_PROTOCOL(rptr);
421 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
424 /* Don't generate compressed packets which are larger than
425 the uncompressed packet. */
432 * Copy over the PPP header and store the 2-byte sequence number.
434 wptr[0] = PPP_ADDRESS(rptr);
435 wptr[1] = PPP_CONTROL(rptr);
436 wptr[2] = PPP_COMP >> 8;
439 wptr[0] = state->seqno >> 8;
440 wptr[1] = state->seqno;
442 state->strm.next_out = wptr;
443 state->strm.avail_out = osize - (PPP_HDRLEN + 2);
446 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
448 state->strm.next_in = rptr;
449 state->strm.avail_in = (isize - off);
453 r = deflate(&state->strm, Z_PACKET_FLUSH);
456 printk(KERN_DEBUG "z_compress: deflate returned %d (%s)\n",
457 r, (state->strm.msg? state->strm.msg: ""));
460 if (state->strm.avail_out == 0) {
462 state->strm.next_out = NULL;
463 state->strm.avail_out = 1000000;
465 break; /* all done */
469 olen += osize - state->strm.avail_out;
472 * See if we managed to reduce the size of the packet.
473 * If the compressor just gave us a single zero byte, it means
474 * the packet was incompressible.
476 if (olen < isize && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
477 state->stats.comp_bytes += olen;
478 state->stats.comp_packets++;
480 state->stats.inc_bytes += isize;
481 state->stats.inc_packets++;
484 state->stats.unc_bytes += isize;
485 state->stats.unc_packets++;
491 z_comp_stats(arg, stats)
493 struct compstat *stats;
495 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
497 *stats = state->stats;
504 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
507 inflateEnd(&state->strm);
509 memPoolFree(&state->strm.opaque);
517 * Allocate space for a decompressor.
520 z_decomp_alloc(options, opt_len)
521 unsigned char *options;
524 struct ppp_deflate_state *state;
527 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
528 || options[1] != CILEN_DEFLATE
529 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
530 || options[3] != DEFLATE_CHK_SEQUENCE)
532 w_size = DEFLATE_SIZE(options[2]);
533 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
536 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
541 memset (state, 0, sizeof (struct ppp_deflate_state));
542 state->w_size = w_size;
543 state->strm.next_out = NULL;
544 state->strm.zalloc = zalloc;
545 state->strm.zfree = zfree;
549 if (!memPoolAlloc(&state->strm.opaque, 0x10000)) {
550 z_decomp_free(state);
554 state->strm.opaque = state;
557 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
558 z_decomp_free(state);
562 return (void *) state;
566 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
568 unsigned char *options;
569 int opt_len, unit, hdrlen, mru, debug;
571 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
573 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
574 || options[1] != CILEN_DEFLATE
575 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
576 || DEFLATE_SIZE(options[2]) != state->w_size
577 || options[3] != DEFLATE_CHK_SEQUENCE)
582 state->debug = debug;
585 inflateReset(&state->strm);
594 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
597 inflateReset(&state->strm);
601 * Decompress a Deflate-compressed packet.
603 * Because of patent problems, we return DECOMP_ERROR for errors
604 * found by inspecting the input data and for system problems, but
605 * DECOMP_FATALERROR for any errors which could possibly be said to
606 * be being detected "after" decompression. For DECOMP_ERROR,
607 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
608 * infringing a patent of Motorola's if we do, so we take CCP down
611 * Given that the frame has the correct sequence number and a good FCS,
612 * errors such as invalid codes in the input most likely indicate a
613 * bug, so we return DECOMP_FATALERROR for them in order to turn off
614 * compression, even though they are detected by inspecting the input.
617 z_decompress(arg, ibuf, isize, obuf, osize)
624 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
626 int decode_proto, overflow;
627 unsigned char overflow_buf[1];
629 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
631 printk(KERN_DEBUG "z_decompress%d: short packet (len=%d)\n",
636 /* Check the sequence number. */
637 seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
638 if (seq != state->seqno) {
640 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
641 state->unit, seq, state->seqno);
647 * Fill in the first part of the PPP header. The protocol field
648 * comes from the decompressed data.
650 obuf[0] = PPP_ADDRESS(ibuf);
651 obuf[1] = PPP_CONTROL(ibuf);
655 * Set up to call inflate. We set avail_out to 1 initially so we can
656 * look at the first byte of the output and decide whether we have
657 * a 1-byte or 2-byte protocol field.
659 state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
660 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
661 state->strm.next_out = obuf + 3;
662 state->strm.avail_out = 1;
667 * Call inflate, supplying more input or output as needed.
670 r = inflate(&state->strm, Z_PACKET_FLUSH);
673 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
674 state->unit, r, (state->strm.msg? state->strm.msg: ""));
675 return DECOMP_FATALERROR;
677 if (state->strm.avail_out != 0)
678 break; /* all done */
680 state->strm.avail_out = osize - PPP_HDRLEN;
681 if ((obuf[3] & 1) == 0) {
682 /* 2-byte protocol field */
684 --state->strm.next_out;
685 ++state->strm.avail_out;
688 } else if (!overflow) {
690 * We've filled up the output buffer; the only way to
691 * find out whether inflate has any more characters left
692 * is to give it another byte of output space.
694 state->strm.next_out = overflow_buf;
695 state->strm.avail_out = 1;
699 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
701 return DECOMP_FATALERROR;
708 olen = osize - state->strm.avail_out;
709 state->stats.unc_bytes += olen;
710 state->stats.unc_packets++;
711 state->stats.comp_bytes += isize;
712 state->stats.comp_packets++;
718 * Incompressible data has arrived - add it to the history.
721 z_incomp(arg, ibuf, icnt)
726 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
730 * Check that the protocol is one we handle.
732 proto = PPP_PROTOCOL(ibuf);
733 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
739 * Iterate through the message blocks, adding the characters in them
740 * to the decompressor's history. For the first block, we start
741 * at the either the 1st or 2nd byte of the protocol field,
742 * depending on whether the protocol value is compressible.
744 state->strm.next_in = ibuf + 3;
745 state->strm.avail_in = icnt - 3;
747 --state->strm.next_in;
748 ++state->strm.avail_in;
751 r = inflateIncomp(&state->strm);
755 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
756 state->unit, r, (state->strm.msg? state->strm.msg: ""));
764 state->stats.inc_bytes += icnt;
765 state->stats.inc_packets++;
766 state->stats.unc_bytes += icnt;
767 state->stats.unc_packets++;
770 /*************************************************************
771 * Module interface table
772 *************************************************************/
774 /* These are in ppp.c */
775 extern int ppp_register_compressor (struct compressor *cp);
776 extern void ppp_unregister_compressor (struct compressor *cp);
779 * Procedures exported to if_ppp.c.
781 struct compressor ppp_deflate = {
782 CI_DEFLATE, /* compress_proto */
783 z_comp_alloc, /* comp_alloc */
784 z_comp_free, /* comp_free */
785 z_comp_init, /* comp_init */
786 z_comp_reset, /* comp_reset */
787 z_compress, /* compress */
788 z_comp_stats, /* comp_stat */
789 z_decomp_alloc, /* decomp_alloc */
790 z_decomp_free, /* decomp_free */
791 z_decomp_init, /* decomp_init */
792 z_decomp_reset, /* decomp_reset */
793 z_decompress, /* decompress */
794 z_incomp, /* incomp */
795 z_comp_stats, /* decomp_stat */
799 /*************************************************************
800 * Module support routines
801 *************************************************************/
806 int answer = ppp_register_compressor (&ppp_deflate);
809 "PPP Deflate Compression module registered\n");
818 "Deflate Compression module busy, remove delayed\n");
820 ppp_unregister_compressor (&ppp_deflate);