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