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