1 /* $FreeBSD: src/sys/net/ppp_deflate.c,v 1.12.2.1 2002/04/14 21:41:48 luigi 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/systm.h>
34 #include <sys/malloc.h>
39 #define PACKETPTR struct mbuf *
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 *z_alloc (void *, u_int items, u_int size);
63 static void z_free (void *, void *ptr);
64 static void *z_comp_alloc (u_char *options, int opt_len);
65 static void *z_decomp_alloc (u_char *options, int opt_len);
66 static void z_comp_free (void *state);
67 static void z_decomp_free (void *state);
68 static int z_comp_init (void *state, u_char *options, int opt_len,
69 int unit, int hdrlen, int debug);
70 static int z_decomp_init (void *state, u_char *options, int opt_len,
71 int unit, int hdrlen, int mru, int debug);
72 static int z_compress (void *state, struct mbuf **mret,
73 struct mbuf *mp, int slen, int maxolen);
74 static void z_incomp (void *state, struct mbuf *dmsg);
75 static int z_decompress (void *state, struct mbuf *cmp,
77 static void z_comp_reset (void *state);
78 static void z_decomp_reset (void *state);
79 static void z_comp_stats (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_draft = {
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 z_alloc(void *notused, u_int items, u_int size)
126 ptr = kmalloc(items * size, M_DEVBUF, M_WAITOK);
131 z_free(void *notused, void *ptr)
133 kfree(ptr, M_DEVBUF);
137 * Allocate space for a compressor.
140 z_comp_alloc(u_char *options, int opt_len)
142 struct deflate_state *state;
145 if (opt_len != CILEN_DEFLATE
146 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
147 || options[1] != CILEN_DEFLATE
148 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
149 || options[3] != DEFLATE_CHK_SEQUENCE)
151 w_size = DEFLATE_SIZE(options[2]);
152 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
155 state = kmalloc(sizeof(struct deflate_state), M_DEVBUF, M_WAITOK);
157 state->strm.next_in = NULL;
158 state->strm.zalloc = z_alloc;
159 state->strm.zfree = z_free;
160 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
161 -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
162 kfree(state, M_DEVBUF);
166 state->w_size = w_size;
167 bzero(&state->stats, sizeof(state->stats));
168 return (void *) state;
172 z_comp_free(void *arg)
174 struct deflate_state *state = (struct deflate_state *) arg;
176 deflateEnd(&state->strm);
177 kfree(state, M_DEVBUF);
181 z_comp_init(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
184 struct deflate_state *state = (struct deflate_state *) arg;
186 if (opt_len < CILEN_DEFLATE
187 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
188 || options[1] != CILEN_DEFLATE
189 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
190 || DEFLATE_SIZE(options[2]) != state->w_size
191 || options[3] != DEFLATE_CHK_SEQUENCE)
196 state->hdrlen = hdrlen;
197 state->debug = debug;
199 deflateReset(&state->strm);
205 z_comp_reset(void *arg)
207 struct deflate_state *state = (struct deflate_state *) arg;
210 deflateReset(&state->strm);
215 * mret: compressed packet (out)
216 * mp: uncompressed packet (in)
219 z_compress(void *arg, struct mbuf **mret, struct mbuf *mp, int orig_len,
222 struct deflate_state *state = (struct deflate_state *) arg;
224 int proto, olen, wspace, r, flush;
228 * Check that the protocol is in the range we handle.
230 rptr = mtod(mp, u_char *);
231 proto = PPP_PROTOCOL(rptr);
232 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
237 /* Allocate one mbuf initially. */
238 if (maxolen > orig_len)
240 MGET(m, MB_DONTWAIT, MT_DATA);
244 if (maxolen + state->hdrlen > MLEN)
245 MCLGET(m, MB_DONTWAIT);
246 wspace = M_TRAILINGSPACE(m);
247 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
248 m->m_data += state->hdrlen;
249 wspace -= state->hdrlen;
251 wptr = mtod(m, u_char *);
254 * Copy over the PPP header and store the 2-byte sequence number.
256 wptr[0] = PPP_ADDRESS(rptr);
257 wptr[1] = PPP_CONTROL(rptr);
258 wptr[2] = PPP_COMP >> 8;
261 wptr[0] = state->seqno >> 8;
262 wptr[1] = state->seqno;
264 state->strm.next_out = wptr;
265 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
267 state->strm.next_out = NULL;
268 state->strm.avail_out = 1000000;
274 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
275 state->strm.next_in = rptr;
276 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
278 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
281 r = deflate(&state->strm, flush);
283 kprintf("z_compress: deflate returned %d (%s)\n",
284 r, (state->strm.msg? state->strm.msg: ""));
287 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
288 break; /* all done */
289 if (state->strm.avail_in == 0 && mp != NULL) {
290 state->strm.next_in = mtod(mp, u_char *);
291 state->strm.avail_in = mp->m_len;
294 flush = Z_PACKET_FLUSH;
296 if (state->strm.avail_out == 0) {
300 MGET(m->m_next, MB_DONTWAIT, MT_DATA);
304 if (maxolen - olen > MLEN)
305 MCLGET(m, MB_DONTWAIT);
306 state->strm.next_out = mtod(m, u_char *);
307 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
311 state->strm.next_out = NULL;
312 state->strm.avail_out = 1000000;
317 olen += (m->m_len = wspace - state->strm.avail_out);
320 * See if we managed to reduce the size of the packet.
322 if (m != NULL && olen < orig_len) {
323 state->stats.comp_bytes += olen;
324 state->stats.comp_packets++;
330 state->stats.inc_bytes += orig_len;
331 state->stats.inc_packets++;
334 state->stats.unc_bytes += orig_len;
335 state->stats.unc_packets++;
341 z_comp_stats(void *arg, struct compstat *stats)
343 struct deflate_state *state = (struct deflate_state *) arg;
346 *stats = state->stats;
347 stats->ratio = stats->unc_bytes;
348 out = stats->comp_bytes + stats->inc_bytes;
349 if (stats->ratio <= 0x7ffffff)
358 * Allocate space for a decompressor.
361 z_decomp_alloc(u_char *options, int opt_len)
363 struct deflate_state *state;
366 if (opt_len != CILEN_DEFLATE
367 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
368 || options[1] != CILEN_DEFLATE
369 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
370 || options[3] != DEFLATE_CHK_SEQUENCE)
372 w_size = DEFLATE_SIZE(options[2]);
373 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
376 state = kmalloc(sizeof(struct deflate_state), M_DEVBUF, M_WAITOK);
378 state->strm.next_out = NULL;
379 state->strm.zalloc = z_alloc;
380 state->strm.zfree = z_free;
381 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
382 kfree(state, M_DEVBUF);
386 state->w_size = w_size;
387 bzero(&state->stats, sizeof(state->stats));
388 return (void *) state;
392 z_decomp_free(void *arg)
394 struct deflate_state *state = (struct deflate_state *) arg;
396 inflateEnd(&state->strm);
397 kfree(state, M_DEVBUF);
401 z_decomp_init(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
404 struct deflate_state *state = (struct deflate_state *) arg;
406 if (opt_len < CILEN_DEFLATE
407 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
408 || options[1] != CILEN_DEFLATE
409 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
410 || DEFLATE_SIZE(options[2]) != state->w_size
411 || options[3] != DEFLATE_CHK_SEQUENCE)
416 state->hdrlen = hdrlen;
417 state->debug = debug;
420 inflateReset(&state->strm);
426 z_decomp_reset(void *arg)
428 struct deflate_state *state = (struct deflate_state *) arg;
431 inflateReset(&state->strm);
435 * Decompress a Deflate-compressed packet.
437 * Because of patent problems, we return DECOMP_ERROR for errors
438 * found by inspecting the input data and for system problems, but
439 * DECOMP_FATALERROR for any errors which could possibly be said to
440 * be being detected "after" decompression. For DECOMP_ERROR,
441 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
442 * infringing a patent of Motorola's if we do, so we take CCP down
445 * Given that the frame has the correct sequence number and a good FCS,
446 * errors such as invalid codes in the input most likely indicate a
447 * bug, so we return DECOMP_FATALERROR for them in order to turn off
448 * compression, even though they are detected by inspecting the input.
451 z_decompress(void *arg, struct mbuf *mi, struct mbuf **mop)
453 struct deflate_state *state = (struct deflate_state *) arg;
454 struct mbuf *mo, *mo_head;
456 int rlen, olen, ospace;
457 int seq, i, flush, r, decode_proto;
458 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
461 rptr = mtod(mi, u_char *);
463 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
468 rptr = mtod(mi, u_char *);
475 /* Check the sequence number. */
476 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
477 if (seq != state->seqno) {
479 kprintf("z_decompress%d: bad seq # %d, expected %d\n",
480 state->unit, seq, state->seqno);
485 /* Allocate an output mbuf. */
486 MGETHDR(mo, MB_DONTWAIT, MT_DATA);
492 MCLGET(mo, MB_DONTWAIT);
493 ospace = M_TRAILINGSPACE(mo);
494 if (state->hdrlen + PPP_HDRLEN < ospace) {
495 mo->m_data += state->hdrlen;
496 ospace -= state->hdrlen;
500 * Fill in the first part of the PPP header. The protocol field
501 * comes from the decompressed data.
503 wptr = mtod(mo, u_char *);
504 wptr[0] = PPP_ADDRESS(hdr);
505 wptr[1] = PPP_CONTROL(hdr);
509 * Set up to call inflate. We set avail_out to 1 initially so we can
510 * look at the first byte of the output and decide whether we have
511 * a 1-byte or 2-byte protocol field.
513 state->strm.next_in = rptr;
514 state->strm.avail_in = rlen;
516 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
517 rlen += PPP_HDRLEN + DEFLATE_OVHD;
518 state->strm.next_out = wptr + 3;
519 state->strm.avail_out = 1;
524 * Call inflate, supplying more input or output as needed.
527 r = inflate(&state->strm, flush);
532 kprintf("z_decompress%d: inflate returned %d (%s)\n",
533 state->unit, r, (state->strm.msg? state->strm.msg: ""));
535 return DECOMP_FATALERROR;
537 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
538 break; /* all done */
539 if (state->strm.avail_in == 0 && mi != NULL) {
540 state->strm.next_in = mtod(mi, u_char *);
541 state->strm.avail_in = mi->m_len;
545 flush = Z_PACKET_FLUSH;
547 if (state->strm.avail_out == 0) {
549 state->strm.avail_out = ospace - PPP_HDRLEN;
550 if ((wptr[3] & 1) == 0) {
551 /* 2-byte protocol field */
553 --state->strm.next_out;
554 ++state->strm.avail_out;
561 MGET(mo->m_next, MB_DONTWAIT, MT_DATA);
567 MCLGET(mo, MB_DONTWAIT);
568 state->strm.next_out = mtod(mo, u_char *);
569 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
577 olen += (mo->m_len = ospace - state->strm.avail_out);
579 if (state->debug && olen > state->mru + PPP_HDRLEN)
580 kprintf("ppp_deflate%d: exceeded mru (%d > %d)\n",
581 state->unit, olen, state->mru + PPP_HDRLEN);
584 state->stats.unc_bytes += olen;
585 state->stats.unc_packets++;
586 state->stats.comp_bytes += rlen;
587 state->stats.comp_packets++;
594 * Incompressible data has arrived - add it to the history.
597 z_incomp(void *arg, struct mbuf *mi)
599 struct deflate_state *state = (struct deflate_state *) arg;
604 * Check that the protocol is one we handle.
606 rptr = mtod(mi, u_char *);
607 proto = PPP_PROTOCOL(rptr);
608 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
614 * Iterate through the mbufs, adding the characters in them
615 * to the decompressor's history. For the first mbuf, we start
616 * at the either the 1st or 2nd byte of the protocol field,
617 * depending on whether the protocol value is compressible.
620 state->strm.next_in = rptr + 3;
621 state->strm.avail_in = rlen - 3;
623 --state->strm.next_in;
624 ++state->strm.avail_in;
627 r = inflateIncomp(&state->strm);
633 kprintf("z_incomp%d: inflateIncomp returned %d (%s)\n",
634 state->unit, r, (state->strm.msg? state->strm.msg: ""));
640 state->strm.next_in = mtod(mi, u_char *);
641 state->strm.avail_in = mi->m_len;
648 state->stats.inc_bytes += rlen;
649 state->stats.inc_packets++;
650 state->stats.unc_bytes += rlen;
651 state->stats.unc_packets++;
654 #endif /* DO_DEFLATE */