]> git.ozlabs.org Git - ppp.git/blob - linux/ppp_deflate.c
mods from Francis Demierre: add SAY, HANGUP, CLR_ABORT, CLR_REPORT keywords
[ppp.git] / linux / ppp_deflate.c
1 /*
2  *  ==FILEVERSION 970522==
3  *
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.
7  *
8  * Copyright (c) 1994 The Australian National University.
9  * All rights reserved.
10  *
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.
17  *
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.
23  *
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.
30  *
31  * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
32  */
33
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>
43 #include <linux/in.h>
44 #include <linux/malloc.h>
45
46 #undef VERSION
47 /* a nice define to generate linux version numbers */
48 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
49
50 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
51 #include <linux/vmalloc.h>
52 #endif
53
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 */
58
59 #include <asm/system.h>
60
61 #include <linux/netdevice.h>
62 #include <linux/skbuff.h>
63 #include <linux/inet.h>
64 #include <linux/ioctl.h>
65
66 #include <linux/ppp_defs.h>
67 #include <linux/ppp-comp.h>
68
69 #include "zlib.c"
70
71 /*
72  * State for a Deflate (de)compressor.
73  */
74 struct ppp_deflate_state {
75     int         seqno;
76     int         w_size;
77     int         unit;
78     int         mru;
79     int         debug;
80     z_stream    strm;
81     struct compstat stats;
82 };
83
84 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
85
86 static void     *zalloc __P((void *, unsigned int items, unsigned int size));
87 static void     *zalloc_init __P((void *, unsigned int items,
88                                   unsigned int size));
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,
95                                  int opt_len,
96                                  int unit, int hdrlen, int debug));
97 static int      z_decomp_init __P((void *state, unsigned char *options,
98                                    int opt_len,
99                                    int unit, int hdrlen, int mru, int debug));
100 static int      z_compress __P((void *state, unsigned char *rptr,
101                                 unsigned char *obuf,
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));
109
110 struct chunk_header {
111     unsigned size;              /* amount of space following header */
112     int valloced;               /* allocated with valloc, not kmalloc */
113 };
114
115 #define MIN_VMALLOC     2048    /* use kmalloc for blocks < this */
116
117 /*
118  * Space allocation and freeing routines for use by zlib routines.
119  */
120 void
121 zfree(arg, ptr, nbytes)
122     void *arg;
123     void *ptr;
124     unsigned int nbytes;
125 {
126     struct chunk_header *hdr = ((struct chunk_header *)ptr) - 1;
127
128     if (hdr->valloced)
129         vfree(hdr);
130     else
131         kfree(hdr);
132 }
133
134 void *
135 zalloc(arg, items, size)
136     void *arg;
137     unsigned int items, size;
138 {
139     struct chunk_header *hdr;
140     unsigned nbytes;
141
142     nbytes = items * size + sizeof(*hdr);
143     hdr = kmalloc(nbytes, GFP_ATOMIC);
144     if (hdr == 0)
145         return 0;
146     hdr->size = nbytes;
147     hdr->valloced = 0;
148     return (void *) (hdr + 1);
149 }
150
151 void *
152 zalloc_init(arg, items, size)
153     void *arg;
154     unsigned int items, size;
155 {
156     struct chunk_header *hdr;
157     unsigned nbytes;
158
159     nbytes = items * size + sizeof(*hdr);
160     if (nbytes >= MIN_VMALLOC)
161         hdr = vmalloc(nbytes);
162     else
163         hdr = kmalloc(nbytes, GFP_KERNEL);
164     if (hdr == 0)
165         return 0;
166     hdr->size = nbytes;
167     hdr->valloced = nbytes >= MIN_VMALLOC;
168     return (void *) (hdr + 1);
169 }
170
171 static void
172 z_comp_free(arg)
173     void *arg;
174 {
175     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
176
177     if (state) {
178             deflateEnd(&state->strm);
179             kfree(state);
180             MOD_DEC_USE_COUNT;
181     }
182 }
183
184 /*
185  * Allocate space for a compressor.
186  */
187 static void *
188 z_comp_alloc(options, opt_len)
189     unsigned char *options;
190     int opt_len;
191 {
192     struct ppp_deflate_state *state;
193     int w_size;
194
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)
199         return NULL;
200     w_size = DEFLATE_SIZE(options[2]);
201     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
202         return NULL;
203
204     state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
205     if (state == NULL)
206         return NULL;
207
208     MOD_INC_USE_COUNT;
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;
215
216     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
217                      -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
218         z_comp_free(state);
219         return NULL;
220     }
221     return (void *) state;
222 }
223
224 static int
225 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
226     void *arg;
227     unsigned char *options;
228     int opt_len, unit, hdrlen, debug;
229 {
230     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
231
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)
237         return 0;
238
239     state->seqno = 0;
240     state->unit  = unit;
241     state->debug = debug;
242
243     deflateReset(&state->strm);
244
245     return 1;
246 }
247
248 static void
249 z_comp_reset(arg)
250     void *arg;
251 {
252     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
253
254     state->seqno = 0;
255     deflateReset(&state->strm);
256 }
257
258 int
259 z_compress(arg, rptr, obuf, isize, osize)
260     void *arg;
261     unsigned char *rptr;        /* uncompressed packet (in) */
262     unsigned char *obuf;        /* compressed packet (out) */
263     int isize, osize;
264 {
265     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
266     int r, proto, off, olen;
267     unsigned char *wptr;
268
269     /*
270      * Check that the protocol is in the range we handle.
271      */
272     proto = PPP_PROTOCOL(rptr);
273     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
274         return 0;
275
276     /* Don't generate compressed packets which are larger than
277        the uncompressed packet. */
278     if (osize > isize)
279         osize = isize;
280
281     wptr = obuf;
282
283     /*
284      * Copy over the PPP header and store the 2-byte sequence number.
285      */
286     wptr[0] = PPP_ADDRESS(rptr);
287     wptr[1] = PPP_CONTROL(rptr);
288     wptr[2] = PPP_COMP >> 8;
289     wptr[3] = PPP_COMP;
290     wptr += PPP_HDRLEN;
291     wptr[0] = state->seqno >> 8;
292     wptr[1] = state->seqno;
293     wptr += 2;
294     state->strm.next_out = wptr;
295     state->strm.avail_out = osize - (PPP_HDRLEN + 2);
296     ++state->seqno;
297
298     off = (proto > 0xff) ? 2 : 3;       /* skip 1st proto byte if 0 */
299     rptr += off;
300     state->strm.next_in = rptr;
301     state->strm.avail_in = (isize - off);
302
303     olen = 0;
304     for (;;) {
305         r = deflate(&state->strm, Z_PACKET_FLUSH);
306         if (r != Z_OK) {
307             if (state->debug)
308                 printk(KERN_DEBUG "z_compress: deflate returned %d (%s)\n",
309                        r, (state->strm.msg? state->strm.msg: ""));
310             break;
311         }
312         if (state->strm.avail_out == 0) {
313             olen += osize;
314             state->strm.next_out = NULL;
315             state->strm.avail_out = 1000000;
316         } else {
317             break;              /* all done */
318         }
319     }
320     if (olen < osize)
321         olen += osize - state->strm.avail_out;
322
323     /*
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.
327      */
328     if (olen < isize && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
329         state->stats.comp_bytes += olen;
330         state->stats.comp_packets++;
331     } else {
332         state->stats.inc_bytes += isize;
333         state->stats.inc_packets++;
334         olen = 0;
335     }
336     state->stats.unc_bytes += isize;
337     state->stats.unc_packets++;
338
339     return olen;
340 }
341
342 static void
343 z_comp_stats(arg, stats)
344     void *arg;
345     struct compstat *stats;
346 {
347     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
348
349     *stats = state->stats;
350 }
351
352 static void
353 z_decomp_free(arg)
354     void *arg;
355 {
356     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
357
358     if (state) {
359             inflateEnd(&state->strm);
360 #if USEMEMPOOL
361             memPoolFree(&state->strm.opaque);
362 #endif
363             kfree(state);
364             MOD_DEC_USE_COUNT;
365     }
366 }
367
368 /*
369  * Allocate space for a decompressor.
370  */
371 static void *
372 z_decomp_alloc(options, opt_len)
373     unsigned char *options;
374     int opt_len;
375 {
376     struct ppp_deflate_state *state;
377     int w_size;
378
379     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
380         || options[1] != CILEN_DEFLATE
381         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
382         || options[3] != DEFLATE_CHK_SEQUENCE)
383         return NULL;
384     w_size = DEFLATE_SIZE(options[2]);
385     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
386         return NULL;
387
388     state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
389     if (state == NULL)
390         return NULL;
391
392     MOD_INC_USE_COUNT;
393     memset (state, 0, sizeof (struct ppp_deflate_state));
394     state->w_size        = w_size;
395     state->strm.next_out = NULL;
396     state->strm.zalloc   = zalloc;
397     state->strm.zalloc_init = zalloc_init;
398     state->strm.zfree    = zfree;
399
400     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
401         z_decomp_free(state);
402         return NULL;
403     }
404     return (void *) state;
405 }
406
407 static int
408 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
409     void *arg;
410     unsigned char *options;
411     int opt_len, unit, hdrlen, mru, debug;
412 {
413     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
414
415     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
416         || options[1] != CILEN_DEFLATE
417         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
418         || DEFLATE_SIZE(options[2]) != state->w_size
419         || options[3] != DEFLATE_CHK_SEQUENCE)
420         return 0;
421
422     state->seqno = 0;
423     state->unit  = unit;
424     state->debug = debug;
425     state->mru   = mru;
426
427     inflateReset(&state->strm);
428
429     return 1;
430 }
431
432 static void
433 z_decomp_reset(arg)
434     void *arg;
435 {
436     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
437
438     state->seqno = 0;
439     inflateReset(&state->strm);
440 }
441
442 /*
443  * Decompress a Deflate-compressed packet.
444  *
445  * Because of patent problems, we return DECOMP_ERROR for errors
446  * found by inspecting the input data and for system problems, but
447  * DECOMP_FATALERROR for any errors which could possibly be said to
448  * be being detected "after" decompression.  For DECOMP_ERROR,
449  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
450  * infringing a patent of Motorola's if we do, so we take CCP down
451  * instead.
452  *
453  * Given that the frame has the correct sequence number and a good FCS,
454  * errors such as invalid codes in the input most likely indicate a
455  * bug, so we return DECOMP_FATALERROR for them in order to turn off
456  * compression, even though they are detected by inspecting the input.
457  */
458 int
459 z_decompress(arg, ibuf, isize, obuf, osize)
460     void *arg;
461     unsigned char *ibuf;
462     int isize;
463     unsigned char *obuf;
464     int osize;
465 {
466     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
467     int olen, seq, r;
468     int decode_proto, overflow;
469     unsigned char overflow_buf[1];
470
471     if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
472         if (state->debug)
473             printk(KERN_DEBUG "z_decompress%d: short packet (len=%d)\n",
474                    state->unit, isize);
475         return DECOMP_ERROR;
476     }
477
478     /* Check the sequence number. */
479     seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
480     if (seq != state->seqno) {
481         if (state->debug)
482             printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
483                    state->unit, seq, state->seqno);
484         return DECOMP_ERROR;
485     }
486     ++state->seqno;
487
488     /*
489      * Fill in the first part of the PPP header.  The protocol field
490      * comes from the decompressed data.
491      */
492     obuf[0] = PPP_ADDRESS(ibuf);
493     obuf[1] = PPP_CONTROL(ibuf);
494     obuf[2] = 0;
495
496     /*
497      * Set up to call inflate.  We set avail_out to 1 initially so we can
498      * look at the first byte of the output and decide whether we have
499      * a 1-byte or 2-byte protocol field.
500      */
501     state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
502     state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
503     state->strm.next_out = obuf + 3;
504     state->strm.avail_out = 1;
505     decode_proto = 1;
506     overflow = 0;
507
508     /*
509      * Call inflate, supplying more input or output as needed.
510      */
511     for (;;) {
512         r = inflate(&state->strm, Z_PACKET_FLUSH);
513         if (r != Z_OK) {
514             if (state->debug)
515                 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
516                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
517             return DECOMP_FATALERROR;
518         }
519         if (state->strm.avail_out != 0)
520             break;              /* all done */
521         if (decode_proto) {
522             state->strm.avail_out = osize - PPP_HDRLEN;
523             if ((obuf[3] & 1) == 0) {
524                 /* 2-byte protocol field */
525                 obuf[2] = obuf[3];
526                 --state->strm.next_out;
527                 ++state->strm.avail_out;
528             }
529             decode_proto = 0;
530         } else if (!overflow) {
531             /*
532              * We've filled up the output buffer; the only way to
533              * find out whether inflate has any more characters left
534              * is to give it another byte of output space.
535              */
536             state->strm.next_out = overflow_buf;
537             state->strm.avail_out = 1;
538             overflow = 1;
539         } else {
540             if (state->debug)
541                 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
542                        state->unit);
543             return DECOMP_FATALERROR;
544         }
545     }
546
547     if (decode_proto)
548         return DECOMP_ERROR;
549
550     olen = osize + overflow - state->strm.avail_out;
551     state->stats.unc_bytes += olen;
552     state->stats.unc_packets++;
553     state->stats.comp_bytes += isize;
554     state->stats.comp_packets++;
555
556     return olen;
557 }
558
559 /*
560  * Incompressible data has arrived - add it to the history.
561  */
562 static void
563 z_incomp(arg, ibuf, icnt)
564     void *arg;
565     unsigned char *ibuf;
566     int icnt;
567 {
568     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
569     int proto, r;
570
571     /*
572      * Check that the protocol is one we handle.
573      */
574     proto = PPP_PROTOCOL(ibuf);
575     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
576         return;
577
578     ++state->seqno;
579
580     /*
581      * Iterate through the message blocks, adding the characters in them
582      * to the decompressor's history.  For the first block, we start
583      * at the either the 1st or 2nd byte of the protocol field,
584      * depending on whether the protocol value is compressible.
585      */
586     state->strm.next_in = ibuf + 3;
587     state->strm.avail_in = icnt - 3;
588     if (proto > 0xff) {
589         --state->strm.next_in;
590         ++state->strm.avail_in;
591     }
592
593     r = inflateIncomp(&state->strm);
594     if (r != Z_OK) {
595         /* gak! */
596         if (state->debug) {
597             printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
598                    state->unit, r, (state->strm.msg? state->strm.msg: ""));
599         }
600         return;
601     }
602
603     /*
604      * Update stats.
605      */
606     state->stats.inc_bytes += icnt;
607     state->stats.inc_packets++;
608     state->stats.unc_bytes += icnt;
609     state->stats.unc_packets++;
610 }
611
612 /*************************************************************
613  * Module interface table
614  *************************************************************/
615
616 /* These are in ppp.c */
617 extern int  ppp_register_compressor   (struct compressor *cp);
618 extern void ppp_unregister_compressor (struct compressor *cp);
619
620 /*
621  * Procedures exported to if_ppp.c.
622  */
623 struct compressor ppp_deflate = {
624     CI_DEFLATE,                 /* compress_proto */
625     z_comp_alloc,               /* comp_alloc */
626     z_comp_free,                /* comp_free */
627     z_comp_init,                /* comp_init */
628     z_comp_reset,               /* comp_reset */
629     z_compress,                 /* compress */
630     z_comp_stats,               /* comp_stat */
631     z_decomp_alloc,             /* decomp_alloc */
632     z_decomp_free,              /* decomp_free */
633     z_decomp_init,              /* decomp_init */
634     z_decomp_reset,             /* decomp_reset */
635     z_decompress,               /* decompress */
636     z_incomp,                   /* incomp */
637     z_comp_stats,               /* decomp_stat */
638 };
639
640 #ifdef MODULE
641 /*************************************************************
642  * Module support routines
643  *************************************************************/
644
645 int
646 init_module(void)
647 {  
648         int answer = ppp_register_compressor (&ppp_deflate);
649         if (answer == 0)
650                 printk (KERN_INFO
651                         "PPP Deflate Compression module registered\n");
652         return answer;
653 }
654      
655 void
656 cleanup_module(void)
657 {
658         if (MOD_IN_USE)
659                 printk (KERN_INFO
660                         "Deflate Compression module busy, remove delayed\n");
661         else
662                 ppp_unregister_compressor (&ppp_deflate);
663 }
664 #endif