1 /* $Id: ppp-deflate.c,v 1.8 1998/03/24 23:48:04 paulus Exp $ */
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.
8 * Copyright (c) 1994 The Australian National University.
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
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
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,
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
36 #include <net/ppp_defs.h>
39 #define PACKETPTR struct mbuf *
40 #include <net/ppp-comp.h>
44 #define DEFLATE_DEBUG 1
47 * State for a Deflate (de)compressor.
49 struct deflate_state {
57 struct compstat stats;
60 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
62 static void *zalloc __P((void *, u_int items, u_int size));
63 static void zfree __P((void *, void *ptr));
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,
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));
82 * Procedures exported to if_ppp.c.
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 */
101 struct compressor ppp_deflate = {
102 CI_DEFLATE_DRAFT, /* compress_proto */
103 z_comp_alloc, /* comp_alloc */
104 z_comp_free, /* comp_free */
105 z_comp_init, /* comp_init */
106 z_comp_reset, /* comp_reset */
107 z_compress, /* compress */
108 z_comp_stats, /* comp_stat */
109 z_decomp_alloc, /* decomp_alloc */
110 z_decomp_free, /* decomp_free */
111 z_decomp_init, /* decomp_init */
112 z_decomp_reset, /* decomp_reset */
113 z_decompress, /* decompress */
114 z_incomp, /* incomp */
115 z_comp_stats, /* decomp_stat */
119 * Space allocation and freeing routines for use by zlib routines.
122 zalloc(notused, items, size)
128 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
141 * Allocate space for a compressor.
144 z_comp_alloc(options, opt_len)
148 struct deflate_state *state;
151 if (opt_len != CILEN_DEFLATE
152 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
153 || options[1] != CILEN_DEFLATE
154 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
155 || options[3] != DEFLATE_CHK_SEQUENCE)
157 w_size = DEFLATE_SIZE(options[2]);
158 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
161 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
166 state->strm.next_in = NULL;
167 state->strm.zalloc = zalloc;
168 state->strm.zfree = zfree;
169 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
170 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
171 FREE(state, M_DEVBUF);
175 state->w_size = w_size;
176 bzero(&state->stats, sizeof(state->stats));
177 return (void *) state;
184 struct deflate_state *state = (struct deflate_state *) arg;
186 deflateEnd(&state->strm);
187 FREE(state, M_DEVBUF);
191 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
194 int opt_len, unit, hdrlen, debug;
196 struct deflate_state *state = (struct deflate_state *) arg;
198 if (opt_len < CILEN_DEFLATE
199 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
200 || options[1] != CILEN_DEFLATE
201 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
202 || DEFLATE_SIZE(options[2]) != state->w_size
203 || options[3] != DEFLATE_CHK_SEQUENCE)
208 state->hdrlen = hdrlen;
209 state->debug = debug;
211 deflateReset(&state->strm);
220 struct deflate_state *state = (struct deflate_state *) arg;
223 deflateReset(&state->strm);
227 z_compress(arg, mret, mp, orig_len, maxolen)
229 struct mbuf **mret; /* compressed packet (out) */
230 struct mbuf *mp; /* uncompressed packet (in) */
231 int orig_len, maxolen;
233 struct deflate_state *state = (struct deflate_state *) arg;
235 int proto, olen, wspace, r, flush;
239 * Check that the protocol is in the range we handle.
241 rptr = mtod(mp, u_char *);
242 proto = PPP_PROTOCOL(rptr);
243 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
248 /* Allocate one mbuf initially. */
249 if (maxolen > orig_len)
251 MGET(m, M_DONTWAIT, MT_DATA);
255 if (maxolen + state->hdrlen > MLEN)
256 MCLGET(m, M_DONTWAIT);
257 wspace = M_TRAILINGSPACE(m);
258 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
259 m->m_data += state->hdrlen;
260 wspace -= state->hdrlen;
262 wptr = mtod(m, u_char *);
265 * Copy over the PPP header and store the 2-byte sequence number.
267 wptr[0] = PPP_ADDRESS(rptr);
268 wptr[1] = PPP_CONTROL(rptr);
269 wptr[2] = PPP_COMP >> 8;
272 wptr[0] = state->seqno >> 8;
273 wptr[1] = state->seqno;
275 state->strm.next_out = wptr;
276 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
278 state->strm.next_out = NULL;
279 state->strm.avail_out = 1000000;
285 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
286 state->strm.next_in = rptr;
287 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
289 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
292 r = deflate(&state->strm, flush);
294 printf("z_compress: deflate returned %d (%s)\n",
295 r, (state->strm.msg? state->strm.msg: ""));
298 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
299 break; /* all done */
300 if (state->strm.avail_in == 0 && mp != NULL) {
301 state->strm.next_in = mtod(mp, u_char *);
302 state->strm.avail_in = mp->m_len;
305 flush = Z_PACKET_FLUSH;
307 if (state->strm.avail_out == 0) {
311 MGET(m->m_next, M_DONTWAIT, MT_DATA);
315 if (maxolen - olen > MLEN)
316 MCLGET(m, M_DONTWAIT);
317 state->strm.next_out = mtod(m, u_char *);
318 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
322 state->strm.next_out = NULL;
323 state->strm.avail_out = 1000000;
328 olen += (m->m_len = wspace - state->strm.avail_out);
331 * See if we managed to reduce the size of the packet.
332 * If the compressor just gave us a single zero byte, it means
333 * the packet was incompressible.
335 if (m != NULL && olen < orig_len
336 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
337 state->stats.comp_bytes += olen;
338 state->stats.comp_packets++;
344 state->stats.inc_bytes += orig_len;
345 state->stats.inc_packets++;
348 state->stats.unc_bytes += orig_len;
349 state->stats.unc_packets++;
355 z_comp_stats(arg, stats)
357 struct compstat *stats;
359 struct deflate_state *state = (struct deflate_state *) arg;
362 *stats = state->stats;
363 stats->ratio = stats->unc_bytes;
364 out = stats->comp_bytes + stats->inc_bytes;
365 if (stats->ratio <= 0x7ffffff)
374 * Allocate space for a decompressor.
377 z_decomp_alloc(options, opt_len)
381 struct deflate_state *state;
384 if (opt_len != CILEN_DEFLATE
385 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
386 || options[1] != CILEN_DEFLATE
387 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
388 || options[3] != DEFLATE_CHK_SEQUENCE)
390 w_size = DEFLATE_SIZE(options[2]);
391 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
394 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
399 state->strm.next_out = NULL;
400 state->strm.zalloc = zalloc;
401 state->strm.zfree = zfree;
402 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
403 FREE(state, M_DEVBUF);
407 state->w_size = w_size;
408 bzero(&state->stats, sizeof(state->stats));
409 return (void *) state;
416 struct deflate_state *state = (struct deflate_state *) arg;
418 inflateEnd(&state->strm);
419 FREE(state, M_DEVBUF);
423 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
426 int opt_len, unit, hdrlen, mru, debug;
428 struct deflate_state *state = (struct deflate_state *) arg;
430 if (opt_len < CILEN_DEFLATE
431 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
432 || options[1] != CILEN_DEFLATE
433 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
434 || DEFLATE_SIZE(options[2]) != state->w_size
435 || options[3] != DEFLATE_CHK_SEQUENCE)
440 state->hdrlen = hdrlen;
441 state->debug = debug;
444 inflateReset(&state->strm);
453 struct deflate_state *state = (struct deflate_state *) arg;
456 inflateReset(&state->strm);
460 * Decompress a Deflate-compressed packet.
462 * Because of patent problems, we return DECOMP_ERROR for errors
463 * found by inspecting the input data and for system problems, but
464 * DECOMP_FATALERROR for any errors which could possibly be said to
465 * be being detected "after" decompression. For DECOMP_ERROR,
466 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
467 * infringing a patent of Motorola's if we do, so we take CCP down
470 * Given that the frame has the correct sequence number and a good FCS,
471 * errors such as invalid codes in the input most likely indicate a
472 * bug, so we return DECOMP_FATALERROR for them in order to turn off
473 * compression, even though they are detected by inspecting the input.
476 z_decompress(arg, mi, mop)
478 struct mbuf *mi, **mop;
480 struct deflate_state *state = (struct deflate_state *) arg;
481 struct mbuf *mo, *mo_head;
483 int rlen, olen, ospace;
484 int seq, i, flush, r, decode_proto;
485 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
488 rptr = mtod(mi, u_char *);
490 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
495 rptr = mtod(mi, u_char *);
502 /* Check the sequence number. */
503 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
504 if (seq != state->seqno) {
506 printf("z_decompress%d: bad seq # %d, expected %d\n",
507 state->unit, seq, state->seqno);
512 /* Allocate an output mbuf. */
513 MGETHDR(mo, M_DONTWAIT, MT_DATA);
519 MCLGET(mo, M_DONTWAIT);
520 ospace = M_TRAILINGSPACE(mo);
521 if (state->hdrlen + PPP_HDRLEN < ospace) {
522 mo->m_data += state->hdrlen;
523 ospace -= state->hdrlen;
527 * Fill in the first part of the PPP header. The protocol field
528 * comes from the decompressed data.
530 wptr = mtod(mo, u_char *);
531 wptr[0] = PPP_ADDRESS(hdr);
532 wptr[1] = PPP_CONTROL(hdr);
536 * Set up to call inflate. We set avail_out to 1 initially so we can
537 * look at the first byte of the output and decide whether we have
538 * a 1-byte or 2-byte protocol field.
540 state->strm.next_in = rptr;
541 state->strm.avail_in = rlen;
543 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
544 rlen += PPP_HDRLEN + DEFLATE_OVHD;
545 state->strm.next_out = wptr + 3;
546 state->strm.avail_out = 1;
551 * Call inflate, supplying more input or output as needed.
554 r = inflate(&state->strm, flush);
559 printf("z_decompress%d: inflate returned %d (%s)\n",
560 state->unit, r, (state->strm.msg? state->strm.msg: ""));
562 return DECOMP_FATALERROR;
564 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
565 break; /* all done */
566 if (state->strm.avail_in == 0 && mi != NULL) {
567 state->strm.next_in = mtod(mi, u_char *);
568 state->strm.avail_in = mi->m_len;
572 flush = Z_PACKET_FLUSH;
574 if (state->strm.avail_out == 0) {
576 state->strm.avail_out = ospace - PPP_HDRLEN;
577 if ((wptr[3] & 1) == 0) {
578 /* 2-byte protocol field */
580 --state->strm.next_out;
581 ++state->strm.avail_out;
588 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
594 MCLGET(mo, M_DONTWAIT);
595 state->strm.next_out = mtod(mo, u_char *);
596 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
604 olen += (mo->m_len = ospace - state->strm.avail_out);
606 if (olen > state->mru + PPP_HDRLEN)
607 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
608 state->unit, olen, state->mru + PPP_HDRLEN);
611 state->stats.unc_bytes += olen;
612 state->stats.unc_packets++;
613 state->stats.comp_bytes += rlen;
614 state->stats.comp_packets++;
621 * Incompressible data has arrived - add it to the history.
628 struct deflate_state *state = (struct deflate_state *) arg;
633 * Check that the protocol is one we handle.
635 rptr = mtod(mi, u_char *);
636 proto = PPP_PROTOCOL(rptr);
637 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
643 * Iterate through the mbufs, adding the characters in them
644 * to the decompressor's history. For the first mbuf, we start
645 * at the either the 1st or 2nd byte of the protocol field,
646 * depending on whether the protocol value is compressible.
649 state->strm.next_in = rptr + 3;
650 state->strm.avail_in = rlen - 3;
652 --state->strm.next_in;
653 ++state->strm.avail_in;
656 r = inflateIncomp(&state->strm);
662 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
663 state->unit, r, (state->strm.msg? state->strm.msg: ""));
669 state->strm.next_in = mtod(mi, u_char *);
670 state->strm.avail_in = mi->m_len;
677 state->stats.inc_bytes += rlen;
678 state->stats.inc_packets++;
679 state->stats.unc_bytes += rlen;
680 state->stats.unc_packets++;
683 #endif /* DO_DEFLATE */