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