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