]> git.ozlabs.org Git - ppp.git/blob - netbsd-1.2/ppp-deflate.c
update for recent kernel changes
[ppp.git] / netbsd-1.2 / ppp-deflate.c
1 /*      $NetBSD: ppp-deflate.c,v 1.3 1996/10/13 02:11:08 christos Exp $ */
2 /*      Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 paulus Exp  */
3
4 /*
5  * ppp_deflate.c - interface the zlib procedures for Deflate compression
6  * and decompression (as used by gzip) to the PPP code.
7  * This version is for use with mbufs on BSD-derived systems.
8  *
9  * Copyright (c) 1994 The Australian National University.
10  * All rights reserved.
11  *
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation is hereby granted, provided that the above copyright
14  * notice appears in all copies.  This software is provided without any
15  * warranty, express or implied. The Australian National University
16  * makes no representations about the suitability of this software for
17  * any purpose.
18  *
19  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
20  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
22  * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
23  * OF SUCH DAMAGE.
24  *
25  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
28  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
29  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
30  * OR MODIFICATIONS.
31  */
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <net/ppp_defs.h>
38 #include <net/zlib.h>
39
40 #define PACKETPTR       struct mbuf *
41 #include <net/ppp-comp.h>
42
43 #if DO_DEFLATE
44
45 #define DEFLATE_DEBUG   1
46
47 /*
48  * State for a Deflate (de)compressor.
49  */
50 struct deflate_state {
51     int         seqno;
52     int         w_size;
53     int         unit;
54     int         hdrlen;
55     int         mru;
56     int         debug;
57     z_stream    strm;
58     struct compstat stats;
59 };
60
61 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
62
63 static void     *zalloc __P((void *, u_int items, u_int size));
64 static void     zfree __P((void *, void *ptr));
65 static void     *z_comp_alloc __P((u_char *options, int opt_len));
66 static void     *z_decomp_alloc __P((u_char *options, int opt_len));
67 static void     z_comp_free __P((void *state));
68 static void     z_decomp_free __P((void *state));
69 static int      z_comp_init __P((void *state, u_char *options, int opt_len,
70                                  int unit, int hdrlen, int debug));
71 static int      z_decomp_init __P((void *state, u_char *options, int opt_len,
72                                      int unit, int hdrlen, int mru, int debug));
73 static int      z_compress __P((void *state, struct mbuf **mret,
74                                   struct mbuf *mp, int slen, int maxolen));
75 static void     z_incomp __P((void *state, struct mbuf *dmsg));
76 static int      z_decompress __P((void *state, struct mbuf *cmp,
77                                     struct mbuf **dmpp));
78 static void     z_comp_reset __P((void *state));
79 static void     z_decomp_reset __P((void *state));
80 static void     z_comp_stats __P((void *state, struct compstat *stats));
81
82 /*
83  * Procedures exported to if_ppp.c.
84  */
85 struct compressor ppp_deflate = {
86     CI_DEFLATE,                 /* compress_proto */
87     z_comp_alloc,               /* comp_alloc */
88     z_comp_free,                /* comp_free */
89     z_comp_init,                /* comp_init */
90     z_comp_reset,               /* comp_reset */
91     z_compress,                 /* compress */
92     z_comp_stats,               /* comp_stat */
93     z_decomp_alloc,             /* decomp_alloc */
94     z_decomp_free,              /* decomp_free */
95     z_decomp_init,              /* decomp_init */
96     z_decomp_reset,             /* decomp_reset */
97     z_decompress,               /* decompress */
98     z_incomp,                   /* incomp */
99     z_comp_stats,               /* decomp_stat */
100 };
101
102 /*
103  * Space allocation and freeing routines for use by zlib routines.
104  */
105 void *
106 zalloc(notused, items, size)
107     void *notused;
108     u_int items, size;
109 {
110     void *ptr;
111
112     MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
113     return ptr;
114 }
115
116 void
117 zfree(notused, ptr)
118     void *notused;
119     void *ptr;
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) != 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      */
315     if (m != NULL && olen < orig_len) {
316         state->stats.comp_bytes += olen;
317         state->stats.comp_packets++;
318     } else {
319         if (*mret != NULL) {
320             m_freem(*mret);
321             *mret = NULL;
322         }
323         state->stats.inc_bytes += orig_len;
324         state->stats.inc_packets++;
325         olen = orig_len;
326     }
327     state->stats.unc_bytes += orig_len;
328     state->stats.unc_packets++;
329
330     return olen;
331 }
332
333 static void
334 z_comp_stats(arg, stats)
335     void *arg;
336     struct compstat *stats;
337 {
338     struct deflate_state *state = (struct deflate_state *) arg;
339     u_int out;
340
341     *stats = state->stats;
342     stats->ratio = stats->unc_bytes;
343     out = stats->comp_bytes + stats->inc_bytes;
344     if (stats->ratio <= 0x7ffffff)
345         stats->ratio <<= 8;
346     else
347         out >>= 8;
348     if (out != 0)
349         stats->ratio /= out;
350 }
351
352 /*
353  * Allocate space for a decompressor.
354  */
355 static void *
356 z_decomp_alloc(options, opt_len)
357     u_char *options;
358     int opt_len;
359 {
360     struct deflate_state *state;
361     int w_size;
362
363     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
364         || options[1] != CILEN_DEFLATE
365         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
366         || options[3] != DEFLATE_CHK_SEQUENCE)
367         return NULL;
368     w_size = DEFLATE_SIZE(options[2]);
369     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
370         return NULL;
371
372     MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
373            M_DEVBUF, M_NOWAIT);
374     if (state == NULL)
375         return NULL;
376
377     state->strm.next_out = NULL;
378     state->strm.zalloc = zalloc;
379     state->strm.zfree = zfree;
380     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
381         FREE(state, M_DEVBUF);
382         return NULL;
383     }
384
385     state->w_size = w_size;
386     bzero(&state->stats, sizeof(state->stats));
387     return (void *) state;
388 }
389
390 static void
391 z_decomp_free(arg)
392     void *arg;
393 {
394     struct deflate_state *state = (struct deflate_state *) arg;
395
396     inflateEnd(&state->strm);
397     FREE(state, M_DEVBUF);
398 }
399
400 static int
401 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
402     void *arg;
403     u_char *options;
404     int opt_len, unit, hdrlen, mru, debug;
405 {
406     struct deflate_state *state = (struct deflate_state *) arg;
407
408     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
409         || options[1] != CILEN_DEFLATE
410         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
411         || DEFLATE_SIZE(options[2]) != state->w_size
412         || options[3] != DEFLATE_CHK_SEQUENCE)
413         return 0;
414
415     state->seqno = 0;
416     state->unit = unit;
417     state->hdrlen = hdrlen;
418     state->debug = debug;
419     state->mru = mru;
420
421     inflateReset(&state->strm);
422
423     return 1;
424 }
425
426 static void
427 z_decomp_reset(arg)
428     void *arg;
429 {
430     struct deflate_state *state = (struct deflate_state *) arg;
431
432     state->seqno = 0;
433     inflateReset(&state->strm);
434 }
435
436 /*
437  * Decompress a Deflate-compressed packet.
438  *
439  * Because of patent problems, we return DECOMP_ERROR for errors
440  * found by inspecting the input data and for system problems, but
441  * DECOMP_FATALERROR for any errors which could possibly be said to
442  * be being detected "after" decompression.  For DECOMP_ERROR,
443  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
444  * infringing a patent of Motorola's if we do, so we take CCP down
445  * instead.
446  *
447  * Given that the frame has the correct sequence number and a good FCS,
448  * errors such as invalid codes in the input most likely indicate a
449  * bug, so we return DECOMP_FATALERROR for them in order to turn off
450  * compression, even though they are detected by inspecting the input.
451  */
452 int
453 z_decompress(arg, mi, mop)
454     void *arg;
455     struct mbuf *mi, **mop;
456 {
457     struct deflate_state *state = (struct deflate_state *) arg;
458     struct mbuf *mo, *mo_head;
459     u_char *rptr, *wptr;
460     int rlen, olen, ospace;
461     int seq, i, flush, r, decode_proto;
462     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
463
464     *mop = NULL;
465     rptr = mtod(mi, u_char *);
466     rlen = mi->m_len;
467     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
468         while (rlen <= 0) {
469             mi = mi->m_next;
470             if (mi == NULL)
471                 return DECOMP_ERROR;
472             rptr = mtod(mi, u_char *);
473             rlen = mi->m_len;
474         }
475         hdr[i] = *rptr++;
476         --rlen;
477     }
478
479     /* Check the sequence number. */
480     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
481     if (seq != state->seqno) {
482         if (state->debug)
483             printf("z_decompress%d: bad seq # %d, expected %d\n",
484                    state->unit, seq, state->seqno);
485         return DECOMP_ERROR;
486     }
487     ++state->seqno;
488
489     /* Allocate an output mbuf. */
490     MGETHDR(mo, M_DONTWAIT, MT_DATA);
491     if (mo == NULL)
492         return DECOMP_ERROR;
493     mo_head = mo;
494     mo->m_len = 0;
495     mo->m_next = NULL;
496     MCLGET(mo, M_DONTWAIT);
497     ospace = M_TRAILINGSPACE(mo);
498     if (state->hdrlen + PPP_HDRLEN < ospace) {
499         mo->m_data += state->hdrlen;
500         ospace -= state->hdrlen;
501     }
502
503     /*
504      * Fill in the first part of the PPP header.  The protocol field
505      * comes from the decompressed data.
506      */
507     wptr = mtod(mo, u_char *);
508     wptr[0] = PPP_ADDRESS(hdr);
509     wptr[1] = PPP_CONTROL(hdr);
510     wptr[2] = 0;
511
512     /*
513      * Set up to call inflate.  We set avail_out to 1 initially so we can
514      * look at the first byte of the output and decide whether we have
515      * a 1-byte or 2-byte protocol field.
516      */
517     state->strm.next_in = rptr;
518     state->strm.avail_in = rlen;
519     mi = mi->m_next;
520     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
521     rlen += PPP_HDRLEN + DEFLATE_OVHD;
522     state->strm.next_out = wptr + 3;
523     state->strm.avail_out = 1;
524     decode_proto = 1;
525     olen = PPP_HDRLEN;
526
527     /*
528      * Call inflate, supplying more input or output as needed.
529      */
530     for (;;) {
531         r = inflate(&state->strm, flush);
532         if (r != Z_OK) {
533 #if !DEFLATE_DEBUG
534             if (state->debug)
535 #endif
536                 printf("z_decompress%d: inflate returned %d (%s)\n",
537                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
538             m_freem(mo_head);
539             return DECOMP_FATALERROR;
540         }
541         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
542             break;              /* all done */
543         if (state->strm.avail_in == 0 && mi != NULL) {
544             state->strm.next_in = mtod(mi, u_char *);
545             state->strm.avail_in = mi->m_len;
546             rlen += mi->m_len;
547             mi = mi->m_next;
548             if (mi == NULL)
549                 flush = Z_PACKET_FLUSH;
550         }
551         if (state->strm.avail_out == 0) {
552             if (decode_proto) {
553                 state->strm.avail_out = ospace - PPP_HDRLEN;
554                 if ((wptr[3] & 1) == 0) {
555                     /* 2-byte protocol field */
556                     wptr[2] = wptr[3];
557                     --state->strm.next_out;
558                     ++state->strm.avail_out;
559                     --olen;
560                 }
561                 decode_proto = 0;
562             } else {
563                 mo->m_len = ospace;
564                 olen += ospace;
565                 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
566                 mo = mo->m_next;
567                 if (mo == NULL) {
568                     m_freem(mo_head);
569                     return DECOMP_ERROR;
570                 }
571                 MCLGET(mo, M_DONTWAIT);
572                 state->strm.next_out = mtod(mo, u_char *);
573                 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
574             }
575         }
576     }
577     if (decode_proto) {
578         m_freem(mo_head);
579         return DECOMP_ERROR;
580     }
581     olen += (mo->m_len = ospace - state->strm.avail_out);
582 #if DEFLATE_DEBUG
583     if (olen > state->mru + PPP_HDRLEN)
584         printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
585                state->unit, olen, state->mru + PPP_HDRLEN);
586 #endif
587
588     state->stats.unc_bytes += olen;
589     state->stats.unc_packets++;
590     state->stats.comp_bytes += rlen;
591     state->stats.comp_packets++;
592
593     *mop = mo_head;
594     return DECOMP_OK;
595 }
596
597 /*
598  * Incompressible data has arrived - add it to the history.
599  */
600 static void
601 z_incomp(arg, mi)
602     void *arg;
603     struct mbuf *mi;
604 {
605     struct deflate_state *state = (struct deflate_state *) arg;
606     u_char *rptr;
607     int rlen, proto, r;
608
609     /*
610      * Check that the protocol is one we handle.
611      */
612     rptr = mtod(mi, u_char *);
613     proto = PPP_PROTOCOL(rptr);
614     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
615         return;
616
617     ++state->seqno;
618
619     /*
620      * Iterate through the mbufs, adding the characters in them
621      * to the decompressor's history.  For the first mbuf, we start
622      * at the either the 1st or 2nd byte of the protocol field,
623      * depending on whether the protocol value is compressible.
624      */
625     rlen = mi->m_len;
626     state->strm.next_in = rptr + 3;
627     state->strm.avail_in = rlen - 3;
628     if (proto > 0xff) {
629         --state->strm.next_in;
630         ++state->strm.avail_in;
631     }
632     for (;;) {
633         r = inflateIncomp(&state->strm);
634         if (r != Z_OK) {
635             /* gak! */
636 #if !DEFLATE_DEBUG
637             if (state->debug)
638 #endif
639                 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
640                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
641             return;
642         }
643         mi = mi->m_next;
644         if (mi == NULL)
645             break;
646         state->strm.next_in = mtod(mi, u_char *);
647         state->strm.avail_in = mi->m_len;
648         rlen += mi->m_len;
649     }
650
651     /*
652      * Update stats.
653      */
654     state->stats.inc_bytes += rlen;
655     state->stats.inc_packets++;
656     state->stats.unc_bytes += rlen;
657     state->stats.unc_packets++;
658 }
659
660 #endif /* DO_DEFLATE */