1 /* $FreeBSD: src/sys/net/ppp_deflate.c,v 1.12.2.1 2002/04/14 21:41:48 luigi Exp $ */
2 /* $DragonFly: src/sys/net/ppp_layer/ppp_deflate.c,v 1.6 2004/06/02 14:42:59 eirikn Exp $ */
5 * ppp_deflate.c - interface the zlib procedures for Deflate compression
6 * and decompression (as used by gzip) to the PPP code.
7 * This version is for use with mbufs on BSD-derived systems.
9 * Copyright (c) 1994 The Australian National University.
10 * All rights reserved.
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation is hereby granted, provided that the above copyright
14 * notice appears in all copies. This software is provided without any
15 * warranty, express or implied. The Australian National University
16 * makes no representations about the suitability of this software for
19 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
20 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
22 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
25 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
28 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
29 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
40 #define PACKETPTR struct mbuf *
45 #define DEFLATE_DEBUG 1
48 * State for a Deflate (de)compressor.
50 struct deflate_state {
58 struct compstat stats;
61 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
63 static void *z_alloc (void *, u_int items, u_int size);
64 static void z_free (void *, void *ptr);
65 static void *z_comp_alloc (u_char *options, int opt_len);
66 static void *z_decomp_alloc (u_char *options, int opt_len);
67 static void z_comp_free (void *state);
68 static void z_decomp_free (void *state);
69 static int z_comp_init (void *state, u_char *options, int opt_len,
70 int unit, int hdrlen, int debug);
71 static int z_decomp_init (void *state, u_char *options, int opt_len,
72 int unit, int hdrlen, int mru, int debug);
73 static int z_compress (void *state, struct mbuf **mret,
74 struct mbuf *mp, int slen, int maxolen);
75 static void z_incomp (void *state, struct mbuf *dmsg);
76 static int z_decompress (void *state, struct mbuf *cmp,
78 static void z_comp_reset (void *state);
79 static void z_decomp_reset (void *state);
80 static void z_comp_stats (void *state, struct compstat *stats);
83 * Procedures exported to if_ppp.c.
85 struct compressor ppp_deflate = {
86 CI_DEFLATE, /* compress_proto */
87 z_comp_alloc, /* comp_alloc */
88 z_comp_free, /* comp_free */
89 z_comp_init, /* comp_init */
90 z_comp_reset, /* comp_reset */
91 z_compress, /* compress */
92 z_comp_stats, /* comp_stat */
93 z_decomp_alloc, /* decomp_alloc */
94 z_decomp_free, /* decomp_free */
95 z_decomp_init, /* decomp_init */
96 z_decomp_reset, /* decomp_reset */
97 z_decompress, /* decompress */
98 z_incomp, /* incomp */
99 z_comp_stats, /* decomp_stat */
102 struct compressor ppp_deflate_draft = {
103 CI_DEFLATE_DRAFT, /* compress_proto */
104 z_comp_alloc, /* comp_alloc */
105 z_comp_free, /* comp_free */
106 z_comp_init, /* comp_init */
107 z_comp_reset, /* comp_reset */
108 z_compress, /* compress */
109 z_comp_stats, /* comp_stat */
110 z_decomp_alloc, /* decomp_alloc */
111 z_decomp_free, /* decomp_free */
112 z_decomp_init, /* decomp_init */
113 z_decomp_reset, /* decomp_reset */
114 z_decompress, /* decompress */
115 z_incomp, /* incomp */
116 z_comp_stats, /* decomp_stat */
120 * Space allocation and freeing routines for use by zlib routines.
123 z_alloc(notused, items, size)
129 MALLOC(ptr, void *, items * size, M_DEVBUF, M_WAITOK);
142 * Allocate space for a compressor.
145 z_comp_alloc(options, opt_len)
149 struct deflate_state *state;
152 if (opt_len != CILEN_DEFLATE
153 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
154 || options[1] != CILEN_DEFLATE
155 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
156 || options[3] != DEFLATE_CHK_SEQUENCE)
158 w_size = DEFLATE_SIZE(options[2]);
159 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
162 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
165 state->strm.next_in = NULL;
166 state->strm.zalloc = z_alloc;
167 state->strm.zfree = z_free;
168 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
169 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
170 free(state, M_DEVBUF);
174 state->w_size = w_size;
175 bzero(&state->stats, sizeof(state->stats));
176 return (void *) state;
183 struct deflate_state *state = (struct deflate_state *) arg;
185 deflateEnd(&state->strm);
186 free(state, M_DEVBUF);
190 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
193 int opt_len, unit, hdrlen, debug;
195 struct deflate_state *state = (struct deflate_state *) arg;
197 if (opt_len < CILEN_DEFLATE
198 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
199 || options[1] != CILEN_DEFLATE
200 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
201 || DEFLATE_SIZE(options[2]) != state->w_size
202 || options[3] != DEFLATE_CHK_SEQUENCE)
207 state->hdrlen = hdrlen;
208 state->debug = debug;
210 deflateReset(&state->strm);
219 struct deflate_state *state = (struct deflate_state *) arg;
222 deflateReset(&state->strm);
226 z_compress(arg, mret, mp, orig_len, maxolen)
228 struct mbuf **mret; /* compressed packet (out) */
229 struct mbuf *mp; /* uncompressed packet (in) */
230 int orig_len, maxolen;
232 struct deflate_state *state = (struct deflate_state *) arg;
234 int proto, olen, wspace, r, flush;
238 * Check that the protocol is in the range we handle.
240 rptr = mtod(mp, u_char *);
241 proto = PPP_PROTOCOL(rptr);
242 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
247 /* Allocate one mbuf initially. */
248 if (maxolen > orig_len)
250 MGET(m, MB_DONTWAIT, MT_DATA);
254 if (maxolen + state->hdrlen > MLEN)
255 MCLGET(m, MB_DONTWAIT);
256 wspace = M_TRAILINGSPACE(m);
257 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
258 m->m_data += state->hdrlen;
259 wspace -= state->hdrlen;
261 wptr = mtod(m, u_char *);
264 * Copy over the PPP header and store the 2-byte sequence number.
266 wptr[0] = PPP_ADDRESS(rptr);
267 wptr[1] = PPP_CONTROL(rptr);
268 wptr[2] = PPP_COMP >> 8;
271 wptr[0] = state->seqno >> 8;
272 wptr[1] = state->seqno;
274 state->strm.next_out = wptr;
275 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
277 state->strm.next_out = NULL;
278 state->strm.avail_out = 1000000;
284 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
285 state->strm.next_in = rptr;
286 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
288 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
291 r = deflate(&state->strm, flush);
293 printf("z_compress: deflate returned %d (%s)\n",
294 r, (state->strm.msg? state->strm.msg: ""));
297 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
298 break; /* all done */
299 if (state->strm.avail_in == 0 && mp != NULL) {
300 state->strm.next_in = mtod(mp, u_char *);
301 state->strm.avail_in = mp->m_len;
304 flush = Z_PACKET_FLUSH;
306 if (state->strm.avail_out == 0) {
310 MGET(m->m_next, MB_DONTWAIT, MT_DATA);
314 if (maxolen - olen > MLEN)
315 MCLGET(m, MB_DONTWAIT);
316 state->strm.next_out = mtod(m, u_char *);
317 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
321 state->strm.next_out = NULL;
322 state->strm.avail_out = 1000000;
327 olen += (m->m_len = wspace - state->strm.avail_out);
330 * See if we managed to reduce the size of the packet.
332 if (m != NULL && olen < orig_len) {
333 state->stats.comp_bytes += olen;
334 state->stats.comp_packets++;
340 state->stats.inc_bytes += orig_len;
341 state->stats.inc_packets++;
344 state->stats.unc_bytes += orig_len;
345 state->stats.unc_packets++;
351 z_comp_stats(arg, stats)
353 struct compstat *stats;
355 struct deflate_state *state = (struct deflate_state *) arg;
358 *stats = state->stats;
359 stats->ratio = stats->unc_bytes;
360 out = stats->comp_bytes + stats->inc_bytes;
361 if (stats->ratio <= 0x7ffffff)
370 * Allocate space for a decompressor.
373 z_decomp_alloc(options, opt_len)
377 struct deflate_state *state;
380 if (opt_len != CILEN_DEFLATE
381 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
382 || options[1] != CILEN_DEFLATE
383 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
384 || options[3] != DEFLATE_CHK_SEQUENCE)
386 w_size = DEFLATE_SIZE(options[2]);
387 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
390 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
393 state->strm.next_out = NULL;
394 state->strm.zalloc = z_alloc;
395 state->strm.zfree = z_free;
396 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
397 free(state, M_DEVBUF);
401 state->w_size = w_size;
402 bzero(&state->stats, sizeof(state->stats));
403 return (void *) state;
410 struct deflate_state *state = (struct deflate_state *) arg;
412 inflateEnd(&state->strm);
413 free(state, M_DEVBUF);
417 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
420 int opt_len, unit, hdrlen, mru, debug;
422 struct deflate_state *state = (struct deflate_state *) arg;
424 if (opt_len < CILEN_DEFLATE
425 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
426 || options[1] != CILEN_DEFLATE
427 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
428 || DEFLATE_SIZE(options[2]) != state->w_size
429 || options[3] != DEFLATE_CHK_SEQUENCE)
434 state->hdrlen = hdrlen;
435 state->debug = debug;
438 inflateReset(&state->strm);
447 struct deflate_state *state = (struct deflate_state *) arg;
450 inflateReset(&state->strm);
454 * Decompress a Deflate-compressed packet.
456 * Because of patent problems, we return DECOMP_ERROR for errors
457 * found by inspecting the input data and for system problems, but
458 * DECOMP_FATALERROR for any errors which could possibly be said to
459 * be being detected "after" decompression. For DECOMP_ERROR,
460 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
461 * infringing a patent of Motorola's if we do, so we take CCP down
464 * Given that the frame has the correct sequence number and a good FCS,
465 * errors such as invalid codes in the input most likely indicate a
466 * bug, so we return DECOMP_FATALERROR for them in order to turn off
467 * compression, even though they are detected by inspecting the input.
470 z_decompress(arg, mi, mop)
472 struct mbuf *mi, **mop;
474 struct deflate_state *state = (struct deflate_state *) arg;
475 struct mbuf *mo, *mo_head;
477 int rlen, olen, ospace;
478 int seq, i, flush, r, decode_proto;
479 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
482 rptr = mtod(mi, u_char *);
484 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
489 rptr = mtod(mi, u_char *);
496 /* Check the sequence number. */
497 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
498 if (seq != state->seqno) {
500 printf("z_decompress%d: bad seq # %d, expected %d\n",
501 state->unit, seq, state->seqno);
506 /* Allocate an output mbuf. */
507 MGETHDR(mo, MB_DONTWAIT, MT_DATA);
513 MCLGET(mo, MB_DONTWAIT);
514 ospace = M_TRAILINGSPACE(mo);
515 if (state->hdrlen + PPP_HDRLEN < ospace) {
516 mo->m_data += state->hdrlen;
517 ospace -= state->hdrlen;
521 * Fill in the first part of the PPP header. The protocol field
522 * comes from the decompressed data.
524 wptr = mtod(mo, u_char *);
525 wptr[0] = PPP_ADDRESS(hdr);
526 wptr[1] = PPP_CONTROL(hdr);
530 * Set up to call inflate. We set avail_out to 1 initially so we can
531 * look at the first byte of the output and decide whether we have
532 * a 1-byte or 2-byte protocol field.
534 state->strm.next_in = rptr;
535 state->strm.avail_in = rlen;
537 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
538 rlen += PPP_HDRLEN + DEFLATE_OVHD;
539 state->strm.next_out = wptr + 3;
540 state->strm.avail_out = 1;
545 * Call inflate, supplying more input or output as needed.
548 r = inflate(&state->strm, flush);
553 printf("z_decompress%d: inflate returned %d (%s)\n",
554 state->unit, r, (state->strm.msg? state->strm.msg: ""));
556 return DECOMP_FATALERROR;
558 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
559 break; /* all done */
560 if (state->strm.avail_in == 0 && mi != NULL) {
561 state->strm.next_in = mtod(mi, u_char *);
562 state->strm.avail_in = mi->m_len;
566 flush = Z_PACKET_FLUSH;
568 if (state->strm.avail_out == 0) {
570 state->strm.avail_out = ospace - PPP_HDRLEN;
571 if ((wptr[3] & 1) == 0) {
572 /* 2-byte protocol field */
574 --state->strm.next_out;
575 ++state->strm.avail_out;
582 MGET(mo->m_next, MB_DONTWAIT, MT_DATA);
588 MCLGET(mo, MB_DONTWAIT);
589 state->strm.next_out = mtod(mo, u_char *);
590 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
598 olen += (mo->m_len = ospace - state->strm.avail_out);
600 if (state->debug && olen > state->mru + PPP_HDRLEN)
601 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
602 state->unit, olen, state->mru + PPP_HDRLEN);
605 state->stats.unc_bytes += olen;
606 state->stats.unc_packets++;
607 state->stats.comp_bytes += rlen;
608 state->stats.comp_packets++;
615 * Incompressible data has arrived - add it to the history.
622 struct deflate_state *state = (struct deflate_state *) arg;
627 * Check that the protocol is one we handle.
629 rptr = mtod(mi, u_char *);
630 proto = PPP_PROTOCOL(rptr);
631 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
637 * Iterate through the mbufs, adding the characters in them
638 * to the decompressor's history. For the first mbuf, we start
639 * at the either the 1st or 2nd byte of the protocol field,
640 * depending on whether the protocol value is compressible.
643 state->strm.next_in = rptr + 3;
644 state->strm.avail_in = rlen - 3;
646 --state->strm.next_in;
647 ++state->strm.avail_in;
650 r = inflateIncomp(&state->strm);
656 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
657 state->unit, r, (state->strm.msg? state->strm.msg: ""));
663 state->strm.next_in = mtod(mi, u_char *);
664 state->strm.avail_in = mi->m_len;
671 state->stats.inc_bytes += rlen;
672 state->stats.inc_packets++;
673 state->stats.unc_bytes += rlen;
674 state->stats.unc_packets++;
677 #endif /* DO_DEFLATE */