kernel: Don't include <sys/mutex.h> in some drivers that don't need it.
[dragonfly.git] / sys / net / ppp_layer / ppp_deflate.c
blobab47de65b6a09525d65dd5eb8ac271e9e5f2c5ed
1 /* $FreeBSD: src/sys/net/ppp_deflate.c,v 1.12.2.1 2002/04/14 21:41:48 luigi Exp $ */
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.
8 * Copyright (c) 1994 The Australian National University.
9 * All rights reserved.
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.
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.
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.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include "ppp_defs.h"
37 #include <net/zlib.h>
39 #define PACKETPTR struct mbuf *
40 #include "ppp_comp.h"
42 #if DO_DEFLATE
44 #define DEFLATE_DEBUG 1
47 * State for a Deflate (de)compressor.
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;
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,
76 struct mbuf **dmpp);
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.
121 static void *
122 z_alloc(void *notused, u_int items, u_int size)
124 void *ptr;
126 ptr = kmalloc(items * size, M_DEVBUF, M_WAITOK);
127 return ptr;
130 static void
131 z_free(void *notused, void *ptr)
133 kfree(ptr, M_DEVBUF);
137 * Allocate space for a compressor.
139 static void *
140 z_comp_alloc(u_char *options, int opt_len)
142 struct deflate_state *state;
143 int w_size;
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)
150 return NULL;
151 w_size = DEFLATE_SIZE(options[2]);
152 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
153 return NULL;
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);
163 return NULL;
166 state->w_size = w_size;
167 bzero(&state->stats, sizeof(state->stats));
168 return (void *) state;
171 static void
172 z_comp_free(void *arg)
174 struct deflate_state *state = (struct deflate_state *) arg;
176 deflateEnd(&state->strm);
177 kfree(state, M_DEVBUF);
180 static int
181 z_comp_init(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
182 int debug)
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)
192 return 0;
194 state->seqno = 0;
195 state->unit = unit;
196 state->hdrlen = hdrlen;
197 state->debug = debug;
199 deflateReset(&state->strm);
201 return 1;
204 static void
205 z_comp_reset(void *arg)
207 struct deflate_state *state = (struct deflate_state *) arg;
209 state->seqno = 0;
210 deflateReset(&state->strm);
214 * Parameters:
215 * mret: compressed packet (out)
216 * mp: uncompressed packet (in)
218 static int
219 z_compress(void *arg, struct mbuf **mret, struct mbuf *mp, int orig_len,
220 int maxolen)
222 struct deflate_state *state = (struct deflate_state *) arg;
223 u_char *rptr, *wptr;
224 int proto, olen, wspace, r, flush;
225 struct mbuf *m;
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) {
233 *mret = NULL;
234 return orig_len;
237 /* Allocate one mbuf initially. */
238 if (maxolen > orig_len)
239 maxolen = orig_len;
240 MGET(m, M_NOWAIT, MT_DATA);
241 *mret = m;
242 if (m != NULL) {
243 m->m_len = 0;
244 if (maxolen + state->hdrlen > MLEN)
245 MCLGET(m, M_NOWAIT);
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;
259 wptr[3] = PPP_COMP;
260 wptr += PPP_HDRLEN;
261 wptr[0] = state->seqno >> 8;
262 wptr[1] = state->seqno;
263 wptr += 2;
264 state->strm.next_out = wptr;
265 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
266 } else {
267 state->strm.next_out = NULL;
268 state->strm.avail_out = 1000000;
269 wptr = NULL;
270 wspace = 0;
272 ++state->seqno;
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;
277 mp = mp->m_next;
278 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
279 olen = 0;
280 for (;;) {
281 r = deflate(&state->strm, flush);
282 if (r != Z_OK) {
283 kprintf("z_compress: deflate returned %d (%s)\n",
284 r, (state->strm.msg? state->strm.msg: ""));
285 break;
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;
292 mp = mp->m_next;
293 if (mp == NULL)
294 flush = Z_PACKET_FLUSH;
296 if (state->strm.avail_out == 0) {
297 if (m != NULL) {
298 m->m_len = wspace;
299 olen += wspace;
300 MGET(m->m_next, M_NOWAIT, MT_DATA);
301 m = m->m_next;
302 if (m != NULL) {
303 m->m_len = 0;
304 if (maxolen - olen > MLEN)
305 MCLGET(m, M_NOWAIT);
306 state->strm.next_out = mtod(m, u_char *);
307 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
310 if (m == NULL) {
311 state->strm.next_out = NULL;
312 state->strm.avail_out = 1000000;
316 if (m != NULL)
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++;
325 } else {
326 if (*mret != NULL) {
327 m_freem(*mret);
328 *mret = NULL;
330 state->stats.inc_bytes += orig_len;
331 state->stats.inc_packets++;
332 olen = orig_len;
334 state->stats.unc_bytes += orig_len;
335 state->stats.unc_packets++;
337 return olen;
340 static void
341 z_comp_stats(void *arg, struct compstat *stats)
343 struct deflate_state *state = (struct deflate_state *) arg;
344 u_int out;
346 *stats = state->stats;
347 stats->ratio = stats->unc_bytes;
348 out = stats->comp_bytes + stats->inc_bytes;
349 if (stats->ratio <= 0x7ffffff)
350 stats->ratio <<= 8;
351 else
352 out >>= 8;
353 if (out != 0)
354 stats->ratio /= out;
358 * Allocate space for a decompressor.
360 static void *
361 z_decomp_alloc(u_char *options, int opt_len)
363 struct deflate_state *state;
364 int w_size;
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)
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;
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);
383 return NULL;
386 state->w_size = w_size;
387 bzero(&state->stats, sizeof(state->stats));
388 return (void *) state;
391 static void
392 z_decomp_free(void *arg)
394 struct deflate_state *state = (struct deflate_state *) arg;
396 inflateEnd(&state->strm);
397 kfree(state, M_DEVBUF);
400 static int
401 z_decomp_init(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
402 int mru, int debug)
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)
412 return 0;
414 state->seqno = 0;
415 state->unit = unit;
416 state->hdrlen = hdrlen;
417 state->debug = debug;
418 state->mru = mru;
420 inflateReset(&state->strm);
422 return 1;
425 static void
426 z_decomp_reset(void *arg)
428 struct deflate_state *state = (struct deflate_state *) arg;
430 state->seqno = 0;
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
443 * instead.
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.
450 static int
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;
455 u_char *rptr, *wptr;
456 int rlen, olen, ospace;
457 int seq, i, flush, r, decode_proto;
458 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
460 *mop = NULL;
461 rptr = mtod(mi, u_char *);
462 rlen = mi->m_len;
463 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
464 while (rlen <= 0) {
465 mi = mi->m_next;
466 if (mi == NULL)
467 return DECOMP_ERROR;
468 rptr = mtod(mi, u_char *);
469 rlen = mi->m_len;
471 hdr[i] = *rptr++;
472 --rlen;
475 /* Check the sequence number. */
476 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
477 if (seq != state->seqno) {
478 if (state->debug)
479 kprintf("z_decompress%d: bad seq # %d, expected %d\n",
480 state->unit, seq, state->seqno);
481 return DECOMP_ERROR;
483 ++state->seqno;
485 /* Allocate an output mbuf. */
486 MGETHDR(mo, M_NOWAIT, MT_DATA);
487 if (mo == NULL)
488 return DECOMP_ERROR;
489 mo_head = mo;
490 mo->m_len = 0;
491 mo->m_next = NULL;
492 MCLGET(mo, M_NOWAIT);
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);
506 wptr[2] = 0;
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;
515 mi = mi->m_next;
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;
520 decode_proto = 1;
521 olen = PPP_HDRLEN;
524 * Call inflate, supplying more input or output as needed.
526 for (;;) {
527 r = inflate(&state->strm, flush);
528 if (r != Z_OK) {
529 #if !DEFLATE_DEBUG
530 if (state->debug)
531 #endif
532 kprintf("z_decompress%d: inflate returned %d (%s)\n",
533 state->unit, r, (state->strm.msg? state->strm.msg: ""));
534 m_freem(mo_head);
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;
542 rlen += mi->m_len;
543 mi = mi->m_next;
544 if (mi == NULL)
545 flush = Z_PACKET_FLUSH;
547 if (state->strm.avail_out == 0) {
548 if (decode_proto) {
549 state->strm.avail_out = ospace - PPP_HDRLEN;
550 if ((wptr[3] & 1) == 0) {
551 /* 2-byte protocol field */
552 wptr[2] = wptr[3];
553 --state->strm.next_out;
554 ++state->strm.avail_out;
555 --olen;
557 decode_proto = 0;
558 } else {
559 mo->m_len = ospace;
560 olen += ospace;
561 MGET(mo->m_next, M_NOWAIT, MT_DATA);
562 mo = mo->m_next;
563 if (mo == NULL) {
564 m_freem(mo_head);
565 return DECOMP_ERROR;
567 MCLGET(mo, M_NOWAIT);
568 state->strm.next_out = mtod(mo, u_char *);
569 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
573 if (decode_proto) {
574 m_freem(mo_head);
575 return DECOMP_ERROR;
577 olen += (mo->m_len = ospace - state->strm.avail_out);
578 #if DEFLATE_DEBUG
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);
582 #endif
584 state->stats.unc_bytes += olen;
585 state->stats.unc_packets++;
586 state->stats.comp_bytes += rlen;
587 state->stats.comp_packets++;
589 *mop = mo_head;
590 return DECOMP_OK;
594 * Incompressible data has arrived - add it to the history.
596 static void
597 z_incomp(void *arg, struct mbuf *mi)
599 struct deflate_state *state = (struct deflate_state *) arg;
600 u_char *rptr;
601 int rlen, proto, r;
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)
609 return;
611 ++state->seqno;
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.
619 rlen = mi->m_len;
620 state->strm.next_in = rptr + 3;
621 state->strm.avail_in = rlen - 3;
622 if (proto > 0xff) {
623 --state->strm.next_in;
624 ++state->strm.avail_in;
626 for (;;) {
627 r = inflateIncomp(&state->strm);
628 if (r != Z_OK) {
629 /* gak! */
630 #if !DEFLATE_DEBUG
631 if (state->debug)
632 #endif
633 kprintf("z_incomp%d: inflateIncomp returned %d (%s)\n",
634 state->unit, r, (state->strm.msg? state->strm.msg: ""));
635 return;
637 mi = mi->m_next;
638 if (mi == NULL)
639 break;
640 state->strm.next_in = mtod(mi, u_char *);
641 state->strm.avail_in = mi->m_len;
642 rlen += mi->m_len;
646 * Update stats.
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 */