]> git.ozlabs.org Git - ppp.git/blob - netbsd-1.1/ppp-deflate.c
allow for COPTS from Makefile.top
[ppp.git] / netbsd-1.1 / ppp-deflate.c
1 /*      $Id: ppp-deflate.c,v 1.3 1996/08/28 06:37:09 paulus Exp $       */
2
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 mbufs on BSD-derived systems.
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
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <net/ppp_defs.h>
37 #include <net/zlib.h>
38
39 #define PACKETPTR       struct mbuf *
40 #include <net/ppp-comp.h>
41
42 #if DO_DEFLATE
43
44 #define DEFLATE_DEBUG   1
45
46 /*
47  * State for a Deflate (de)compressor.
48  */
49 struct deflate_state {
50     int         seqno;
51     int         w_size;
52     int         unit;
53     int         hdrlen;
54     int         mru;
55     int         debug;
56     z_stream    strm;
57     struct compstat stats;
58 };
59
60 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
61
62 static void     *zalloc __P((void *, u_int items, u_int size));
63 static void     zfree __P((void *, void *ptr, u_int nb));
64 static void     *z_comp_alloc __P((u_char *options, int opt_len));
65 static void     *z_decomp_alloc __P((u_char *options, int opt_len));
66 static void     z_comp_free __P((void *state));
67 static void     z_decomp_free __P((void *state));
68 static int      z_comp_init __P((void *state, u_char *options, int opt_len,
69                                  int unit, int hdrlen, int debug));
70 static int      z_decomp_init __P((void *state, u_char *options, int opt_len,
71                                      int unit, int hdrlen, int mru, int debug));
72 static int      z_compress __P((void *state, struct mbuf **mret,
73                                   struct mbuf *mp, int slen, int maxolen));
74 static void     z_incomp __P((void *state, struct mbuf *dmsg));
75 static int      z_decompress __P((void *state, struct mbuf *cmp,
76                                     struct mbuf **dmpp));
77 static void     z_comp_reset __P((void *state));
78 static void     z_decomp_reset __P((void *state));
79 static void     z_comp_stats __P((void *state, struct compstat *stats));
80
81 /*
82  * Procedures exported to if_ppp.c.
83  */
84 struct compressor ppp_deflate = {
85     CI_DEFLATE,                 /* compress_proto */
86     z_comp_alloc,               /* comp_alloc */
87     z_comp_free,                /* comp_free */
88     z_comp_init,                /* comp_init */
89     z_comp_reset,               /* comp_reset */
90     z_compress,                 /* compress */
91     z_comp_stats,               /* comp_stat */
92     z_decomp_alloc,             /* decomp_alloc */
93     z_decomp_free,              /* decomp_free */
94     z_decomp_init,              /* decomp_init */
95     z_decomp_reset,             /* decomp_reset */
96     z_decompress,               /* decompress */
97     z_incomp,                   /* incomp */
98     z_comp_stats,               /* decomp_stat */
99 };
100
101 /*
102  * Space allocation and freeing routines for use by zlib routines.
103  */
104 void *
105 zalloc(notused, items, size)
106     void *notused;
107     u_int items, size;
108 {
109     void *ptr;
110
111     MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
112     return ptr;
113 }
114
115 void
116 zfree(notused, ptr, nbytes)
117     void *notused;
118     void *ptr;
119     u_int nbytes;
120 {
121     FREE(ptr, M_DEVBUF);
122 }
123
124 /*
125  * Allocate space for a compressor.
126  */
127 static void *
128 z_comp_alloc(options, opt_len)
129     u_char *options;
130     int opt_len;
131 {
132     struct deflate_state *state;
133     int w_size;
134
135     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
136         || options[1] != CILEN_DEFLATE
137         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
138         || options[3] != DEFLATE_CHK_SEQUENCE)
139         return NULL;
140     w_size = DEFLATE_SIZE(options[2]);
141     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
142         return NULL;
143
144     MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
145            M_DEVBUF, M_NOWAIT);
146     if (state == NULL)
147         return NULL;
148
149     state->strm.next_in = NULL;
150     state->strm.zalloc = zalloc;
151     state->strm.zfree = zfree;
152     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
153                      -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
154         FREE(state, M_DEVBUF);
155         return NULL;
156     }
157
158     state->w_size = w_size;
159     bzero(&state->stats, sizeof(state->stats));
160     return (void *) state;
161 }
162
163 static void
164 z_comp_free(arg)
165     void *arg;
166 {
167     struct deflate_state *state = (struct deflate_state *) arg;
168
169     deflateEnd(&state->strm);
170     FREE(state, M_DEVBUF);
171 }
172
173 static int
174 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
175     void *arg;
176     u_char *options;
177     int opt_len, unit, hdrlen, debug;
178 {
179     struct deflate_state *state = (struct deflate_state *) arg;
180
181     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
182         || options[1] != CILEN_DEFLATE
183         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
184         || DEFLATE_SIZE(options[2]) != state->w_size
185         || options[3] != DEFLATE_CHK_SEQUENCE)
186         return 0;
187
188     state->seqno = 0;
189     state->unit = unit;
190     state->hdrlen = hdrlen;
191     state->debug = debug;
192
193     deflateReset(&state->strm);
194
195     return 1;
196 }
197
198 static void
199 z_comp_reset(arg)
200     void *arg;
201 {
202     struct deflate_state *state = (struct deflate_state *) arg;
203
204     state->seqno = 0;
205     deflateReset(&state->strm);
206 }
207
208 int
209 z_compress(arg, mret, mp, orig_len, maxolen)
210     void *arg;
211     struct mbuf **mret;         /* compressed packet (out) */
212     struct mbuf *mp;            /* uncompressed packet (in) */
213     int orig_len, maxolen;
214 {
215     struct deflate_state *state = (struct deflate_state *) arg;
216     u_char *rptr, *wptr;
217     int proto, olen, wspace, r, flush;
218     struct mbuf *m;
219
220     /*
221      * Check that the protocol is in the range we handle.
222      */
223     rptr = mtod(mp, u_char *);
224     proto = PPP_PROTOCOL(rptr);
225     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
226         *mret = NULL;
227         return orig_len;
228     }
229
230     /* Allocate one mbuf initially. */
231     if (maxolen > orig_len)
232         maxolen = orig_len;
233     MGET(m, M_DONTWAIT, MT_DATA);
234     *mret = m;
235     if (m != NULL) {
236         m->m_len = 0;
237         if (maxolen + state->hdrlen > MLEN)
238             MCLGET(m, M_DONTWAIT);
239         wspace = M_TRAILINGSPACE(m);
240         if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
241             m->m_data += state->hdrlen;
242             wspace -= state->hdrlen;
243         }
244         wptr = mtod(m, u_char *);
245
246         /*
247          * Copy over the PPP header and store the 2-byte sequence number.
248          */
249         wptr[0] = PPP_ADDRESS(rptr);
250         wptr[1] = PPP_CONTROL(rptr);
251         wptr[2] = PPP_COMP >> 8;
252         wptr[3] = PPP_COMP;
253         wptr += PPP_HDRLEN;
254         wptr[0] = state->seqno >> 8;
255         wptr[1] = state->seqno;
256         wptr += 2;
257         state->strm.next_out = wptr;
258         state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
259     } else {
260         state->strm.next_out = NULL;
261         state->strm.avail_out = 1000000;
262         wptr = NULL;
263         wspace = 0;
264     }
265     ++state->seqno;
266
267     rptr += (proto > 0xff)? 2: 3;       /* skip 1st proto byte if 0 */
268     state->strm.next_in = rptr;
269     state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
270     mp = mp->m_next;
271     flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
272     olen = 0;
273     for (;;) {
274         r = deflate(&state->strm, flush);
275         if (r != Z_OK) {
276             printf("z_compress: deflate returned %d (%s)\n",
277                    r, (state->strm.msg? state->strm.msg: ""));
278             break;
279         }
280         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
281             break;              /* all done */
282         if (state->strm.avail_in == 0 && mp != NULL) {
283             state->strm.next_in = mtod(mp, u_char *);
284             state->strm.avail_in = mp->m_len;
285             mp = mp->m_next;
286             if (mp == NULL)
287                 flush = Z_PACKET_FLUSH;
288         }
289         if (state->strm.avail_out == 0) {
290             if (m != NULL) {
291                 m->m_len = wspace;
292                 olen += wspace;
293                 MGET(m->m_next, M_DONTWAIT, MT_DATA);
294                 m = m->m_next;
295                 if (m != NULL) {
296                     m->m_len = 0;
297                     if (maxolen - olen > MLEN)
298                         MCLGET(m, M_DONTWAIT);
299                     state->strm.next_out = mtod(m, u_char *);
300                     state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
301                 }
302             }
303             if (m == NULL) {
304                 state->strm.next_out = NULL;
305                 state->strm.avail_out = 1000000;
306             }
307         }
308     }
309     if (m != NULL)
310         olen += (m->m_len = wspace - state->strm.avail_out);
311
312     /*
313      * See if we managed to reduce the size of the packet.
314      * If the compressor just gave us a single zero byte, it means
315      * the packet was incompressible.
316      */
317     if (m != NULL && olen < orig_len
318         && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
319         state->stats.comp_bytes += olen;
320         state->stats.comp_packets++;
321     } else {
322         if (*mret != NULL) {
323             m_freem(*mret);
324             *mret = NULL;
325         }
326         state->stats.inc_bytes += orig_len;
327         state->stats.inc_packets++;
328         olen = orig_len;
329     }
330     state->stats.unc_bytes += orig_len;
331     state->stats.unc_packets++;
332
333     return olen;
334 }
335
336 static void
337 z_comp_stats(arg, stats)
338     void *arg;
339     struct compstat *stats;
340 {
341     struct deflate_state *state = (struct deflate_state *) arg;
342     u_int out;
343
344     *stats = state->stats;
345     stats->ratio = stats->unc_bytes;
346     out = stats->comp_bytes + stats->inc_bytes;
347     if (stats->ratio <= 0x7ffffff)
348         stats->ratio <<= 8;
349     else
350         out >>= 8;
351     if (out != 0)
352         stats->ratio /= out;
353 }
354
355 /*
356  * Allocate space for a decompressor.
357  */
358 static void *
359 z_decomp_alloc(options, opt_len)
360     u_char *options;
361     int opt_len;
362 {
363     struct deflate_state *state;
364     int w_size;
365
366     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
367         || options[1] != CILEN_DEFLATE
368         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
369         || options[3] != DEFLATE_CHK_SEQUENCE)
370         return NULL;
371     w_size = DEFLATE_SIZE(options[2]);
372     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
373         return NULL;
374
375     MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
376            M_DEVBUF, M_NOWAIT);
377     if (state == NULL)
378         return NULL;
379
380     state->strm.next_out = NULL;
381     state->strm.zalloc = zalloc;
382     state->strm.zfree = zfree;
383     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
384         FREE(state, M_DEVBUF);
385         return NULL;
386     }
387
388     state->w_size = w_size;
389     bzero(&state->stats, sizeof(state->stats));
390     return (void *) state;
391 }
392
393 static void
394 z_decomp_free(arg)
395     void *arg;
396 {
397     struct deflate_state *state = (struct deflate_state *) arg;
398
399     inflateEnd(&state->strm);
400     FREE(state, M_DEVBUF);
401 }
402
403 static int
404 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
405     void *arg;
406     u_char *options;
407     int opt_len, unit, hdrlen, mru, debug;
408 {
409     struct deflate_state *state = (struct deflate_state *) arg;
410
411     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
412         || options[1] != CILEN_DEFLATE
413         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
414         || DEFLATE_SIZE(options[2]) != state->w_size
415         || options[3] != DEFLATE_CHK_SEQUENCE)
416         return 0;
417
418     state->seqno = 0;
419     state->unit = unit;
420     state->hdrlen = hdrlen;
421     state->debug = debug;
422     state->mru = mru;
423
424     inflateReset(&state->strm);
425
426     return 1;
427 }
428
429 static void
430 z_decomp_reset(arg)
431     void *arg;
432 {
433     struct deflate_state *state = (struct deflate_state *) arg;
434
435     state->seqno = 0;
436     inflateReset(&state->strm);
437 }
438
439 /*
440  * Decompress a Deflate-compressed packet.
441  *
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
448  * instead.
449  *
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.
454  */
455 int
456 z_decompress(arg, mi, mop)
457     void *arg;
458     struct mbuf *mi, **mop;
459 {
460     struct deflate_state *state = (struct deflate_state *) arg;
461     struct mbuf *mo, *mo_head;
462     u_char *rptr, *wptr;
463     int rlen, olen, ospace;
464     int seq, i, flush, r, decode_proto;
465     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
466
467     *mop = NULL;
468     rptr = mtod(mi, u_char *);
469     rlen = mi->m_len;
470     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
471         while (rlen <= 0) {
472             mi = mi->m_next;
473             if (mi == NULL)
474                 return DECOMP_ERROR;
475             rptr = mtod(mi, u_char *);
476             rlen = mi->m_len;
477         }
478         hdr[i] = *rptr++;
479         --rlen;
480     }
481
482     /* Check the sequence number. */
483     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
484     if (seq != state->seqno) {
485         if (state->debug)
486             printf("z_decompress%d: bad seq # %d, expected %d\n",
487                    state->unit, seq, state->seqno);
488         return DECOMP_ERROR;
489     }
490     ++state->seqno;
491
492     /* Allocate an output mbuf. */
493     MGETHDR(mo, M_DONTWAIT, MT_DATA);
494     if (mo == NULL)
495         return DECOMP_ERROR;
496     mo_head = mo;
497     mo->m_len = 0;
498     mo->m_next = NULL;
499     MCLGET(mo, M_DONTWAIT);
500     ospace = M_TRAILINGSPACE(mo);
501     if (state->hdrlen + PPP_HDRLEN < ospace) {
502         mo->m_data += state->hdrlen;
503         ospace -= state->hdrlen;
504     }
505
506     /*
507      * Fill in the first part of the PPP header.  The protocol field
508      * comes from the decompressed data.
509      */
510     wptr = mtod(mo, u_char *);
511     wptr[0] = PPP_ADDRESS(hdr);
512     wptr[1] = PPP_CONTROL(hdr);
513     wptr[2] = 0;
514
515     /*
516      * Set up to call inflate.  We set avail_out to 1 initially so we can
517      * look at the first byte of the output and decide whether we have
518      * a 1-byte or 2-byte protocol field.
519      */
520     state->strm.next_in = rptr;
521     state->strm.avail_in = rlen;
522     mi = mi->m_next;
523     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
524     rlen += PPP_HDRLEN + DEFLATE_OVHD;
525     state->strm.next_out = wptr + 3;
526     state->strm.avail_out = 1;
527     decode_proto = 1;
528     olen = PPP_HDRLEN;
529
530     /*
531      * Call inflate, supplying more input or output as needed.
532      */
533     for (;;) {
534         r = inflate(&state->strm, flush);
535         if (r != Z_OK) {
536 #if !DEFLATE_DEBUG
537             if (state->debug)
538 #endif
539                 printf("z_decompress%d: inflate returned %d (%s)\n",
540                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
541             m_freem(mo_head);
542             return DECOMP_FATALERROR;
543         }
544         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
545             break;              /* all done */
546         if (state->strm.avail_in == 0 && mi != NULL) {
547             state->strm.next_in = mtod(mi, u_char *);
548             state->strm.avail_in = mi->m_len;
549             rlen += mi->m_len;
550             mi = mi->m_next;
551             if (mi == NULL)
552                 flush = Z_PACKET_FLUSH;
553         }
554         if (state->strm.avail_out == 0) {
555             if (decode_proto) {
556                 state->strm.avail_out = ospace - PPP_HDRLEN;
557                 if ((wptr[3] & 1) == 0) {
558                     /* 2-byte protocol field */
559                     wptr[2] = wptr[3];
560                     --state->strm.next_out;
561                     ++state->strm.avail_out;
562                     --olen;
563                 }
564                 decode_proto = 0;
565             } else {
566                 mo->m_len = ospace;
567                 olen += ospace;
568                 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
569                 mo = mo->m_next;
570                 if (mo == NULL) {
571                     m_freem(mo_head);
572                     return DECOMP_ERROR;
573                 }
574                 MCLGET(mo, M_DONTWAIT);
575                 state->strm.next_out = mtod(mo, u_char *);
576                 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
577             }
578         }
579     }
580     if (decode_proto) {
581         m_freem(mo_head);
582         return DECOMP_ERROR;
583     }
584     olen += (mo->m_len = ospace - state->strm.avail_out);
585 #if DEFLATE_DEBUG
586     if (olen > state->mru + PPP_HDRLEN)
587         printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
588                state->unit, olen, state->mru + PPP_HDRLEN);
589 #endif
590
591     state->stats.unc_bytes += olen;
592     state->stats.unc_packets++;
593     state->stats.comp_bytes += rlen;
594     state->stats.comp_packets++;
595
596     *mop = mo_head;
597     return DECOMP_OK;
598 }
599
600 /*
601  * Incompressible data has arrived - add it to the history.
602  */
603 static void
604 z_incomp(arg, mi)
605     void *arg;
606     struct mbuf *mi;
607 {
608     struct deflate_state *state = (struct deflate_state *) arg;
609     u_char *rptr;
610     int rlen, proto, r;
611
612     /*
613      * Check that the protocol is one we handle.
614      */
615     rptr = mtod(mi, u_char *);
616     proto = PPP_PROTOCOL(rptr);
617     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
618         return;
619
620     ++state->seqno;
621
622     /*
623      * Iterate through the mbufs, adding the characters in them
624      * to the decompressor's history.  For the first mbuf, we start
625      * at the either the 1st or 2nd byte of the protocol field,
626      * depending on whether the protocol value is compressible.
627      */
628     rlen = mi->m_len;
629     state->strm.next_in = rptr + 3;
630     state->strm.avail_in = rlen - 3;
631     if (proto > 0xff) {
632         --state->strm.next_in;
633         ++state->strm.avail_in;
634     }
635     for (;;) {
636         r = inflateIncomp(&state->strm);
637         if (r != Z_OK) {
638             /* gak! */
639 #if !DEFLATE_DEBUG
640             if (state->debug)
641 #endif
642                 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
643                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
644             return;
645         }
646         mi = mi->m_next;
647         if (mi == NULL)
648             break;
649         state->strm.next_in = mtod(mi, u_char *);
650         state->strm.avail_in = mi->m_len;
651         rlen += mi->m_len;
652     }
653
654     /*
655      * Update stats.
656      */
657     state->stats.inc_bytes += rlen;
658     state->stats.inc_packets++;
659     state->stats.unc_bytes += rlen;
660     state->stats.unc_packets++;
661 }
662
663 #endif /* DO_DEFLATE */