]> git.ozlabs.org Git - ppp.git/blob - ultrix/ppp-deflate.c
add a few defs
[ppp.git] / ultrix / ppp-deflate.c
1 /*      $Id: ppp-deflate.c,v 1.3 1997/04/30 06:00:45 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.zalloc_init = (alloc_func) zalloc;
161     state->strm.zfree = (free_func) zfree;
162     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
163                      -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
164         KM_FREE(state, KM_DEVBUF);
165         return NULL;
166     }
167
168     state->w_size = w_size;
169     bzero(&state->stats, sizeof(state->stats));
170     return (void *) state;
171 }
172
173 static void
174 z_comp_free(arg)
175     void *arg;
176 {
177     struct deflate_state *state = (struct deflate_state *) arg;
178
179     deflateEnd(&state->strm);
180     KM_FREE(state, KM_DEVBUF);
181 }
182
183 static int
184 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
185     void *arg;
186     u_char *options;
187     int opt_len, unit, hdrlen, debug;
188 {
189     struct deflate_state *state = (struct deflate_state *) arg;
190
191     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
192         || options[1] != CILEN_DEFLATE
193         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
194         || DEFLATE_SIZE(options[2]) != state->w_size
195         || options[3] != DEFLATE_CHK_SEQUENCE)
196         return 0;
197
198     state->seqno = 0;
199     state->unit = unit;
200     state->hdrlen = hdrlen;
201     state->debug = debug;
202
203     deflateReset(&state->strm);
204
205     return 1;
206 }
207
208 static void
209 z_comp_reset(arg)
210     void *arg;
211 {
212     struct deflate_state *state = (struct deflate_state *) arg;
213
214     state->seqno = 0;
215     deflateReset(&state->strm);
216 }
217
218 int
219 z_compress(arg, mret, mp, orig_len, maxolen)
220     void *arg;
221     struct mbuf **mret;         /* compressed packet (out) */
222     struct mbuf *mp;            /* uncompressed packet (in) */
223     int orig_len, maxolen;
224 {
225     struct deflate_state *state = (struct deflate_state *) arg;
226     u_char *rptr, *wptr;
227     int proto, olen, wspace, r, flush;
228     struct mbuf *m, *clp;
229
230     /*
231      * Check that the protocol is in the range we handle.
232      */
233     rptr = mtod(mp, u_char *);
234     proto = PPP_PROTOCOL(rptr);
235     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
236         *mret = NULL;
237         return orig_len;
238     }
239
240     /* Allocate one mbuf initially. */
241     if (maxolen > orig_len)
242         maxolen = orig_len;
243     MGET(m, M_DONTWAIT, MT_DATA);
244     *mret = m;
245     if (m != NULL) {
246         if (maxolen + state->hdrlen > MLEN) {
247             /* MCLGET is not a single statement!!! */
248             MCLGET(m, clp)
249         }
250         m->m_len = 0;
251         wspace = M_TRAILINGSPACE(m);
252         if (state->hdrlen > 0 && state->hdrlen + PPP_HDRLEN + 2 < wspace) {
253             m->m_off += state->hdrlen;
254             wspace -= state->hdrlen;
255         }
256         wptr = mtod(m, u_char *);
257
258         /*
259          * Copy over the PPP header and store the 2-byte sequence number.
260          */
261         wptr[0] = PPP_ADDRESS(rptr);
262         wptr[1] = PPP_CONTROL(rptr);
263         wptr[2] = PPP_COMP >> 8;
264         wptr[3] = PPP_COMP;
265         wptr += PPP_HDRLEN;
266         wptr[0] = state->seqno >> 8;
267         wptr[1] = state->seqno;
268         wptr += 2;
269         state->strm.next_out = wptr;
270         state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
271     } else {
272         state->strm.next_out = NULL;
273         state->strm.avail_out = 1000000;
274         wptr = NULL;
275         wspace = 0;
276     }
277     ++state->seqno;
278
279     rptr += (proto > 0xff)? 2: 3;       /* skip 1st proto byte if 0 */
280     state->strm.next_in = rptr;
281     state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
282     mp = mp->m_next;
283     flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
284     olen = 0;
285     for (;;) {
286         r = deflate(&state->strm, flush);
287         if (r != Z_OK) {
288             printf("z_compress: deflate returned %d (%s)\n",
289                    r, (state->strm.msg? state->strm.msg: ""));
290             break;
291         }
292         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
293             break;              /* all done */
294         if (state->strm.avail_in == 0 && mp != NULL) {
295             state->strm.next_in = mtod(mp, u_char *);
296             state->strm.avail_in = mp->m_len;
297             mp = mp->m_next;
298             if (mp == NULL)
299                 flush = Z_PACKET_FLUSH;
300         }
301         if (state->strm.avail_out == 0) {
302             if (m != NULL) {
303                 m->m_len = wspace;
304                 olen += wspace;
305                 MGET(m->m_next, M_DONTWAIT, MT_DATA);
306                 m = m->m_next;
307                 if (m != NULL) {
308                     if (maxolen - olen > MLEN) {
309                         MCLGET(m, clp)
310                     }
311                     m->m_len = 0;
312                     state->strm.next_out = mtod(m, u_char *);
313                     state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
314                 }
315             }
316             if (m == NULL) {
317                 state->strm.next_out = NULL;
318                 state->strm.avail_out = 1000000;
319             }
320         }
321     }
322     if (m != NULL)
323         olen += (m->m_len = wspace - state->strm.avail_out);
324
325     /*
326      * See if we managed to reduce the size of the packet.
327      * If the compressor just gave us a single zero byte, it means
328      * the packet was incompressible.
329      */
330     if (m != NULL && olen < orig_len
331         && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
332         state->stats.comp_bytes += olen;
333         state->stats.comp_packets++;
334     } else {
335         if (*mret != NULL) {
336             m_freem(*mret);
337             *mret = NULL;
338         }
339         state->stats.inc_bytes += orig_len;
340         state->stats.inc_packets++;
341         olen = orig_len;
342     }
343     state->stats.unc_bytes += orig_len;
344     state->stats.unc_packets++;
345
346     return olen;
347 }
348
349 static void
350 z_comp_stats(arg, stats)
351     void *arg;
352     struct compstat *stats;
353 {
354     struct deflate_state *state = (struct deflate_state *) arg;
355     u_int out;
356
357     *stats = state->stats;
358     stats->ratio = stats->unc_bytes;
359     out = stats->comp_bytes + stats->inc_bytes;
360     if (stats->ratio <= 0x7ffffff)
361         stats->ratio <<= 8;
362     else
363         out >>= 8;
364     if (out != 0)
365         stats->ratio /= out;
366 }
367
368 /*
369  * Allocate space for a decompressor.
370  */
371 static void *
372 z_decomp_alloc(options, opt_len)
373     u_char *options;
374     int opt_len;
375 {
376     struct 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     KM_ALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
389              KM_DEVBUF, KM_NOARG);
390     if (state == NULL)
391         return NULL;
392     bzero(state, sizeof(struct deflate_state));
393
394     state->strm.next_out = NULL;
395     state->strm.zalloc = (alloc_func) zalloc;
396     state->strm.zalloc_init = (alloc_func) zalloc;
397     state->strm.zfree = (free_func) zfree;
398     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
399         KM_FREE(state, KM_DEVBUF);
400         return NULL;
401     }
402
403     state->w_size = w_size;
404     bzero(&state->stats, sizeof(state->stats));
405     return (void *) state;
406 }
407
408 static void
409 z_decomp_free(arg)
410     void *arg;
411 {
412     struct deflate_state *state = (struct deflate_state *) arg;
413
414     inflateEnd(&state->strm);
415     KM_FREE(state, KM_DEVBUF);
416 }
417
418 static int
419 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
420     void *arg;
421     u_char *options;
422     int opt_len, unit, hdrlen, mru, debug;
423 {
424     struct deflate_state *state = (struct deflate_state *) arg;
425
426     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
427         || options[1] != CILEN_DEFLATE
428         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
429         || DEFLATE_SIZE(options[2]) != state->w_size
430         || options[3] != DEFLATE_CHK_SEQUENCE)
431         return 0;
432
433     state->seqno = 0;
434     state->unit = unit;
435     state->hdrlen = hdrlen;
436     state->debug = debug;
437     state->mru = mru;
438
439     inflateReset(&state->strm);
440
441     return 1;
442 }
443
444 static void
445 z_decomp_reset(arg)
446     void *arg;
447 {
448     struct deflate_state *state = (struct deflate_state *) arg;
449
450     state->seqno = 0;
451     inflateReset(&state->strm);
452 }
453
454 /*
455  * Decompress a Deflate-compressed packet.
456  *
457  * Because of patent problems, we return DECOMP_ERROR for errors
458  * found by inspecting the input data and for system problems, but
459  * DECOMP_FATALERROR for any errors which could possibly be said to
460  * be being detected "after" decompression.  For DECOMP_ERROR,
461  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
462  * infringing a patent of Motorola's if we do, so we take CCP down
463  * instead.
464  *
465  * Given that the frame has the correct sequence number and a good FCS,
466  * errors such as invalid codes in the input most likely indicate a
467  * bug, so we return DECOMP_FATALERROR for them in order to turn off
468  * compression, even though they are detected by inspecting the input.
469  */
470 int
471 z_decompress(arg, mi, mop)
472     void *arg;
473     struct mbuf *mi, **mop;
474 {
475     struct deflate_state *state = (struct deflate_state *) arg;
476     struct mbuf *mo, *mo_head, *clp;
477     u_char *rptr, *wptr;
478     int rlen, olen, ospace;
479     int seq, i, flush, r, decode_proto;
480     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
481
482     *mop = NULL;
483     rptr = mtod(mi, u_char *);
484     rlen = mi->m_len;
485     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
486         while (rlen <= 0) {
487             mi = mi->m_next;
488             if (mi == NULL)
489                 return DECOMP_ERROR;
490             rptr = mtod(mi, u_char *);
491             rlen = mi->m_len;
492         }
493         hdr[i] = *rptr++;
494         --rlen;
495     }
496
497     /* Check the sequence number. */
498     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
499     if (seq != state->seqno) {
500         if (state->debug)
501             printf("z_decompress%d: bad seq # %d, expected %d\n",
502                    state->unit, seq, state->seqno);
503         return DECOMP_ERROR;
504     }
505     ++state->seqno;
506
507     /* Allocate an output mbuf. */
508     MGET(mo, M_DONTWAIT, MT_DATA);
509     if (mo == NULL)
510         return DECOMP_ERROR;
511     mo_head = mo;
512     mo->m_next = NULL;
513     MCLGET(mo, clp)
514     mo->m_len = 0;
515     ospace = M_TRAILINGSPACE(mo);
516     if (state->hdrlen > 0 && state->hdrlen + PPP_HDRLEN < ospace) {
517         mo->m_off += state->hdrlen;
518         ospace -= state->hdrlen;
519     }
520
521     /*
522      * Fill in the first part of the PPP header.  The protocol field
523      * comes from the decompressed data.
524      */
525     wptr = mtod(mo, u_char *);
526     wptr[0] = PPP_ADDRESS(hdr);
527     wptr[1] = PPP_CONTROL(hdr);
528     wptr[2] = 0;
529
530     /*
531      * Set up to call inflate.  We set avail_out to 1 initially so we can
532      * look at the first byte of the output and decide whether we have
533      * a 1-byte or 2-byte protocol field.
534      */
535     state->strm.next_in = rptr;
536     state->strm.avail_in = rlen;
537     mi = mi->m_next;
538     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
539     rlen += PPP_HDRLEN + DEFLATE_OVHD;
540     state->strm.next_out = wptr + 3;
541     state->strm.avail_out = 1;
542     decode_proto = 1;
543     olen = PPP_HDRLEN;
544
545     /*
546      * Call inflate, supplying more input or output as needed.
547      */
548     for (;;) {
549         r = inflate(&state->strm, flush);
550         if (r != Z_OK) {
551             if (state->debug)
552                 printf("z_decompress%d: inflate returned %d (%s)\n",
553                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
554             m_freem(mo_head);
555             return DECOMP_FATALERROR;
556         }
557         if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
558             break;              /* all done */
559         if (state->strm.avail_in == 0 && mi != NULL) {
560             state->strm.next_in = mtod(mi, u_char *);
561             state->strm.avail_in = mi->m_len;
562             rlen += mi->m_len;
563             mi = mi->m_next;
564             if (mi == NULL)
565                 flush = Z_PACKET_FLUSH;
566         }
567         if (state->strm.avail_out == 0) {
568             if (decode_proto) {
569                 state->strm.avail_out = ospace - PPP_HDRLEN;
570                 if ((wptr[3] & 1) == 0) {
571                     /* 2-byte protocol field */
572                     wptr[2] = wptr[3];
573                     --state->strm.next_out;
574                     ++state->strm.avail_out;
575                     --olen;
576                 }
577                 decode_proto = 0;
578             } else {
579                 mo->m_len = ospace;
580                 olen += ospace;
581                 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
582                 mo = mo->m_next;
583                 if (mo == NULL) {
584                     m_freem(mo_head);
585                     return DECOMP_ERROR;
586                 }
587                 MCLGET(mo, clp)
588                 mo->m_len = 0;
589                 state->strm.next_out = mtod(mo, u_char *);
590                 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
591             }
592         }
593     }
594     if (decode_proto) {
595         m_freem(mo_head);
596         return DECOMP_ERROR;
597     }
598     olen += (mo->m_len = ospace - state->strm.avail_out);
599
600     state->stats.unc_bytes += olen;
601     state->stats.unc_packets++;
602     state->stats.comp_bytes += rlen;
603     state->stats.comp_packets++;
604
605     *mop = mo_head;
606     return DECOMP_OK;
607 }
608
609 /*
610  * Incompressible data has arrived - add it to the history.
611  */
612 static void
613 z_incomp(arg, mi)
614     void *arg;
615     struct mbuf *mi;
616 {
617     struct deflate_state *state = (struct deflate_state *) arg;
618     u_char *rptr;
619     int rlen, proto, r;
620
621     /*
622      * Check that the protocol is one we handle.
623      */
624     rptr = mtod(mi, u_char *);
625     proto = PPP_PROTOCOL(rptr);
626     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
627         return;
628
629     ++state->seqno;
630
631     /*
632      * Iterate through the mbufs, adding the characters in them
633      * to the decompressor's history.  For the first mbuf, we start
634      * at the either the 1st or 2nd byte of the protocol field,
635      * depending on whether the protocol value is compressible.
636      */
637     rlen = mi->m_len;
638     state->strm.next_in = rptr + 3;
639     state->strm.avail_in = rlen - 3;
640     if (proto > 0xff) {
641         --state->strm.next_in;
642         ++state->strm.avail_in;
643     }
644     for (;;) {
645         r = inflateIncomp(&state->strm);
646         if (r != Z_OK) {
647             /* gak! */
648             if (state->debug) {
649                 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
650                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
651             }
652             return;
653         }
654         mi = mi->m_next;
655         if (mi == NULL)
656             break;
657         state->strm.next_in = mtod(mi, u_char *);
658         state->strm.avail_in = mi->m_len;
659         rlen += mi->m_len;
660     }
661
662     /*
663      * Update stats.
664      */
665     state->stats.inc_bytes += rlen;
666     state->stats.inc_packets++;
667     state->stats.unc_bytes += rlen;
668     state->stats.unc_packets++;
669 }
670
671 #endif /* DO_DEFLATE */