2 * ==FILEVERSION 970522==
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>
35 #include <linux/version.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
44 #include <linux/malloc.h>
47 /* a nice define to generate linux version numbers */
48 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
50 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
51 #include <linux/vmalloc.h>
54 #include <linux/errno.h>
55 #include <linux/sched.h> /* to get the struct task_struct */
56 #include <linux/string.h> /* used in new tty drivers */
57 #include <linux/signal.h> /* used in new tty drivers */
59 #include <asm/system.h>
61 #include <linux/netdevice.h>
62 #include <linux/skbuff.h>
63 #include <linux/inet.h>
64 #include <linux/ioctl.h>
66 #include <linux/ppp_defs.h>
67 #include <linux/ppp-comp.h>
72 * State for a Deflate (de)compressor.
74 struct ppp_deflate_state {
81 struct compstat stats;
84 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
86 static void *zalloc __P((void *, unsigned int items, unsigned int size));
87 static void *zalloc_init __P((void *, unsigned int items,
89 static void zfree __P((void *, void *ptr, unsigned int nb));
90 static void *z_comp_alloc __P((unsigned char *options, int opt_len));
91 static void *z_decomp_alloc __P((unsigned char *options, int opt_len));
92 static void z_comp_free __P((void *state));
93 static void z_decomp_free __P((void *state));
94 static int z_comp_init __P((void *state, unsigned char *options,
96 int unit, int hdrlen, int debug));
97 static int z_decomp_init __P((void *state, unsigned char *options,
99 int unit, int hdrlen, int mru, int debug));
100 static int z_compress __P((void *state, unsigned char *rptr,
102 int isize, int osize));
103 static void z_incomp __P((void *state, unsigned char *ibuf, int icnt));
104 static int z_decompress __P((void *state, unsigned char *ibuf,
105 int isize, unsigned char *obuf, int osize));
106 static void z_comp_reset __P((void *state));
107 static void z_decomp_reset __P((void *state));
108 static void z_comp_stats __P((void *state, struct compstat *stats));
110 struct chunk_header {
111 unsigned size; /* amount of space following header */
112 int valloced; /* allocated with valloc, not kmalloc */
115 #define MIN_VMALLOC 2048 /* use kmalloc for blocks < this */
118 * Space allocation and freeing routines for use by zlib routines.
121 zfree(arg, ptr, nbytes)
126 struct chunk_header *hdr = ((struct chunk_header *)ptr) - 1;
135 zalloc(arg, items, size)
137 unsigned int items, size;
139 struct chunk_header *hdr;
142 nbytes = items * size + sizeof(*hdr);
143 hdr = kmalloc(nbytes, GFP_ATOMIC);
148 return (void *) (hdr + 1);
152 zalloc_init(arg, items, size)
154 unsigned int items, size;
156 struct chunk_header *hdr;
159 nbytes = items * size + sizeof(*hdr);
160 if (nbytes >= MIN_VMALLOC)
161 hdr = vmalloc(nbytes);
163 hdr = kmalloc(nbytes, GFP_KERNEL);
167 hdr->valloced = nbytes >= MIN_VMALLOC;
168 return (void *) (hdr + 1);
175 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
178 deflateEnd(&state->strm);
185 * Allocate space for a compressor.
188 z_comp_alloc(options, opt_len)
189 unsigned char *options;
192 struct ppp_deflate_state *state;
195 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
196 || options[1] != CILEN_DEFLATE
197 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
198 || options[3] != DEFLATE_CHK_SEQUENCE)
200 w_size = DEFLATE_SIZE(options[2]);
201 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
204 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
209 memset (state, 0, sizeof (struct ppp_deflate_state));
210 state->strm.next_in = NULL;
211 state->strm.zalloc = zalloc;
212 state->strm.zalloc_init = zalloc_init;
213 state->strm.zfree = zfree;
214 state->w_size = w_size;
216 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
217 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
221 return (void *) state;
225 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
227 unsigned char *options;
228 int opt_len, unit, hdrlen, debug;
230 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
232 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
233 || options[1] != CILEN_DEFLATE
234 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
235 || DEFLATE_SIZE(options[2]) != state->w_size
236 || options[3] != DEFLATE_CHK_SEQUENCE)
241 state->debug = debug;
243 deflateReset(&state->strm);
252 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
255 deflateReset(&state->strm);
259 z_compress(arg, rptr, obuf, isize, osize)
261 unsigned char *rptr; /* uncompressed packet (in) */
262 unsigned char *obuf; /* compressed packet (out) */
265 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
266 int r, proto, off, olen;
270 * Check that the protocol is in the range we handle.
272 proto = PPP_PROTOCOL(rptr);
273 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
276 /* Don't generate compressed packets which are larger than
277 the uncompressed packet. */
284 * Copy over the PPP header and store the 2-byte sequence number.
286 wptr[0] = PPP_ADDRESS(rptr);
287 wptr[1] = PPP_CONTROL(rptr);
288 wptr[2] = PPP_COMP >> 8;
291 wptr[0] = state->seqno >> 8;
292 wptr[1] = state->seqno;
294 state->strm.next_out = wptr;
295 state->strm.avail_out = osize - (PPP_HDRLEN + 2);
298 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
300 state->strm.next_in = rptr;
301 state->strm.avail_in = (isize - off);
305 r = deflate(&state->strm, Z_PACKET_FLUSH);
308 printk(KERN_DEBUG "z_compress: deflate returned %d (%s)\n",
309 r, (state->strm.msg? state->strm.msg: ""));
312 if (state->strm.avail_out == 0) {
314 state->strm.next_out = NULL;
315 state->strm.avail_out = 1000000;
317 break; /* all done */
321 olen += osize - state->strm.avail_out;
324 * See if we managed to reduce the size of the packet.
325 * If the compressor just gave us a single zero byte, it means
326 * the packet was incompressible.
328 if (olen < isize && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
329 state->stats.comp_bytes += olen;
330 state->stats.comp_packets++;
332 state->stats.inc_bytes += isize;
333 state->stats.inc_packets++;
336 state->stats.unc_bytes += isize;
337 state->stats.unc_packets++;
343 z_comp_stats(arg, stats)
345 struct compstat *stats;
347 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
349 *stats = state->stats;
356 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
359 inflateEnd(&state->strm);
366 * Allocate space for a decompressor.
369 z_decomp_alloc(options, opt_len)
370 unsigned char *options;
373 struct ppp_deflate_state *state;
376 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
377 || options[1] != CILEN_DEFLATE
378 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
379 || options[3] != DEFLATE_CHK_SEQUENCE)
381 w_size = DEFLATE_SIZE(options[2]);
382 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
385 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
390 memset (state, 0, sizeof (struct ppp_deflate_state));
391 state->w_size = w_size;
392 state->strm.next_out = NULL;
393 state->strm.zalloc = zalloc;
394 state->strm.zalloc_init = zalloc_init;
395 state->strm.zfree = zfree;
397 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
398 z_decomp_free(state);
401 return (void *) state;
405 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
407 unsigned char *options;
408 int opt_len, unit, hdrlen, mru, debug;
410 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
412 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
413 || options[1] != CILEN_DEFLATE
414 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
415 || DEFLATE_SIZE(options[2]) != state->w_size
416 || options[3] != DEFLATE_CHK_SEQUENCE)
421 state->debug = debug;
424 inflateReset(&state->strm);
433 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
436 inflateReset(&state->strm);
440 * Decompress a Deflate-compressed packet.
442 * Because of patent problems, we return DECOMP_ERROR for errors
443 * found by inspecting the input data and for system problems, but
444 * DECOMP_FATALERROR for any errors which could possibly be said to
445 * be being detected "after" decompression. For DECOMP_ERROR,
446 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
447 * infringing a patent of Motorola's if we do, so we take CCP down
450 * Given that the frame has the correct sequence number and a good FCS,
451 * errors such as invalid codes in the input most likely indicate a
452 * bug, so we return DECOMP_FATALERROR for them in order to turn off
453 * compression, even though they are detected by inspecting the input.
456 z_decompress(arg, ibuf, isize, obuf, osize)
463 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
465 int decode_proto, overflow;
466 unsigned char overflow_buf[1];
468 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
470 printk(KERN_DEBUG "z_decompress%d: short packet (len=%d)\n",
475 /* Check the sequence number. */
476 seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
477 if (seq != state->seqno) {
479 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
480 state->unit, seq, state->seqno);
486 * Fill in the first part of the PPP header. The protocol field
487 * comes from the decompressed data.
489 obuf[0] = PPP_ADDRESS(ibuf);
490 obuf[1] = PPP_CONTROL(ibuf);
494 * Set up to call inflate. We set avail_out to 1 initially so we can
495 * look at the first byte of the output and decide whether we have
496 * a 1-byte or 2-byte protocol field.
498 state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
499 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
500 state->strm.next_out = obuf + 3;
501 state->strm.avail_out = 1;
506 * Call inflate, supplying more input or output as needed.
509 r = inflate(&state->strm, Z_PACKET_FLUSH);
512 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
513 state->unit, r, (state->strm.msg? state->strm.msg: ""));
514 return DECOMP_FATALERROR;
516 if (state->strm.avail_out != 0)
517 break; /* all done */
519 state->strm.avail_out = osize - PPP_HDRLEN;
520 if ((obuf[3] & 1) == 0) {
521 /* 2-byte protocol field */
523 --state->strm.next_out;
524 ++state->strm.avail_out;
527 } else if (!overflow) {
529 * We've filled up the output buffer; the only way to
530 * find out whether inflate has any more characters left
531 * is to give it another byte of output space.
533 state->strm.next_out = overflow_buf;
534 state->strm.avail_out = 1;
538 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
540 return DECOMP_FATALERROR;
547 olen = osize + overflow - state->strm.avail_out;
548 state->stats.unc_bytes += olen;
549 state->stats.unc_packets++;
550 state->stats.comp_bytes += isize;
551 state->stats.comp_packets++;
557 * Incompressible data has arrived - add it to the history.
560 z_incomp(arg, ibuf, icnt)
565 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
569 * Check that the protocol is one we handle.
571 proto = PPP_PROTOCOL(ibuf);
572 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
578 * Iterate through the message blocks, adding the characters in them
579 * to the decompressor's history. For the first block, we start
580 * at the either the 1st or 2nd byte of the protocol field,
581 * depending on whether the protocol value is compressible.
583 state->strm.next_in = ibuf + 3;
584 state->strm.avail_in = icnt - 3;
586 --state->strm.next_in;
587 ++state->strm.avail_in;
590 r = inflateIncomp(&state->strm);
594 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
595 state->unit, r, (state->strm.msg? state->strm.msg: ""));
603 state->stats.inc_bytes += icnt;
604 state->stats.inc_packets++;
605 state->stats.unc_bytes += icnt;
606 state->stats.unc_packets++;
609 /*************************************************************
610 * Module interface table
611 *************************************************************/
613 /* These are in ppp.c */
614 extern int ppp_register_compressor (struct compressor *cp);
615 extern void ppp_unregister_compressor (struct compressor *cp);
618 * Procedures exported to if_ppp.c.
620 struct compressor ppp_deflate = {
621 CI_DEFLATE, /* compress_proto */
622 z_comp_alloc, /* comp_alloc */
623 z_comp_free, /* comp_free */
624 z_comp_init, /* comp_init */
625 z_comp_reset, /* comp_reset */
626 z_compress, /* compress */
627 z_comp_stats, /* comp_stat */
628 z_decomp_alloc, /* decomp_alloc */
629 z_decomp_free, /* decomp_free */
630 z_decomp_init, /* decomp_init */
631 z_decomp_reset, /* decomp_reset */
632 z_decompress, /* decompress */
633 z_incomp, /* incomp */
634 z_comp_stats, /* decomp_stat */
638 /*************************************************************
639 * Module support routines
640 *************************************************************/
645 int answer = ppp_register_compressor (&ppp_deflate);
648 "PPP Deflate Compression module registered\n");
657 "Deflate Compression module busy, remove delayed\n");
659 ppp_unregister_compressor (&ppp_deflate);