1 /* $Id: ppp-deflate.c,v 1.9 2002/12/06 09:49:16 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 Paul Mackerras. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. The name(s) of the authors of this software must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission.
26 * 4. Redistributions of any form whatsoever must retain the following
28 * "This product includes software developed by Paul Mackerras
29 * <paulus@samba.org>".
31 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
32 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
34 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
44 #include <net/ppp_defs.h>
47 #define PACKETPTR struct mbuf *
48 #include <net/ppp-comp.h>
52 #define DEFLATE_DEBUG 1
55 * State for a Deflate (de)compressor.
57 struct deflate_state
{
65 struct compstat stats
;
68 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
70 static void *zalloc
__P((void *, u_int items
, u_int size
));
71 static void zfree
__P((void *, void *ptr
));
72 static void *z_comp_alloc
__P((u_char
*options
, int opt_len
));
73 static void *z_decomp_alloc
__P((u_char
*options
, int opt_len
));
74 static void z_comp_free
__P((void *state
));
75 static void z_decomp_free
__P((void *state
));
76 static int z_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
77 int unit
, int hdrlen
, int debug
));
78 static int z_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
79 int unit
, int hdrlen
, int mru
, int debug
));
80 static int z_compress
__P((void *state
, struct mbuf
**mret
,
81 struct mbuf
*mp
, int slen
, int maxolen
));
82 static void z_incomp
__P((void *state
, struct mbuf
*dmsg
));
83 static int z_decompress
__P((void *state
, struct mbuf
*cmp
,
85 static void z_comp_reset
__P((void *state
));
86 static void z_decomp_reset
__P((void *state
));
87 static void z_comp_stats
__P((void *state
, struct compstat
*stats
));
90 * Procedures exported to if_ppp.c.
92 struct compressor ppp_deflate
= {
93 CI_DEFLATE
, /* compress_proto */
94 z_comp_alloc
, /* comp_alloc */
95 z_comp_free
, /* comp_free */
96 z_comp_init
, /* comp_init */
97 z_comp_reset
, /* comp_reset */
98 z_compress
, /* compress */
99 z_comp_stats
, /* comp_stat */
100 z_decomp_alloc
, /* decomp_alloc */
101 z_decomp_free
, /* decomp_free */
102 z_decomp_init
, /* decomp_init */
103 z_decomp_reset
, /* decomp_reset */
104 z_decompress
, /* decompress */
105 z_incomp
, /* incomp */
106 z_comp_stats
, /* decomp_stat */
109 struct compressor ppp_deflate
= {
110 CI_DEFLATE_DRAFT
, /* compress_proto */
111 z_comp_alloc
, /* comp_alloc */
112 z_comp_free
, /* comp_free */
113 z_comp_init
, /* comp_init */
114 z_comp_reset
, /* comp_reset */
115 z_compress
, /* compress */
116 z_comp_stats
, /* comp_stat */
117 z_decomp_alloc
, /* decomp_alloc */
118 z_decomp_free
, /* decomp_free */
119 z_decomp_init
, /* decomp_init */
120 z_decomp_reset
, /* decomp_reset */
121 z_decompress
, /* decompress */
122 z_incomp
, /* incomp */
123 z_comp_stats
, /* decomp_stat */
127 * Space allocation and freeing routines for use by zlib routines.
130 zalloc(notused
, items
, size
)
136 MALLOC(ptr
, void *, items
* size
, M_DEVBUF
, M_NOWAIT
);
149 * Allocate space for a compressor.
152 z_comp_alloc(options
, opt_len
)
156 struct deflate_state
*state
;
159 if (opt_len
!= CILEN_DEFLATE
160 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
161 || options
[1] != CILEN_DEFLATE
162 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
163 || options
[3] != DEFLATE_CHK_SEQUENCE
)
165 w_size
= DEFLATE_SIZE(options
[2]);
166 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
169 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
174 state
->strm
.next_in
= NULL
;
175 state
->strm
.zalloc
= zalloc
;
176 state
->strm
.zfree
= zfree
;
177 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
178 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
179 FREE(state
, M_DEVBUF
);
183 state
->w_size
= w_size
;
184 bzero(&state
->stats
, sizeof(state
->stats
));
185 return (void *) state
;
192 struct deflate_state
*state
= (struct deflate_state
*) arg
;
194 deflateEnd(&state
->strm
);
195 FREE(state
, M_DEVBUF
);
199 z_comp_init(arg
, options
, opt_len
, unit
, hdrlen
, debug
)
202 int opt_len
, unit
, hdrlen
, debug
;
204 struct deflate_state
*state
= (struct deflate_state
*) arg
;
206 if (opt_len
< CILEN_DEFLATE
207 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
208 || options
[1] != CILEN_DEFLATE
209 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
210 || DEFLATE_SIZE(options
[2]) != state
->w_size
211 || options
[3] != DEFLATE_CHK_SEQUENCE
)
216 state
->hdrlen
= hdrlen
;
217 state
->debug
= debug
;
219 deflateReset(&state
->strm
);
228 struct deflate_state
*state
= (struct deflate_state
*) arg
;
231 deflateReset(&state
->strm
);
235 z_compress(arg
, mret
, mp
, orig_len
, maxolen
)
237 struct mbuf
**mret
; /* compressed packet (out) */
238 struct mbuf
*mp
; /* uncompressed packet (in) */
239 int orig_len
, maxolen
;
241 struct deflate_state
*state
= (struct deflate_state
*) arg
;
243 int proto
, olen
, wspace
, r
, flush
;
247 * Check that the protocol is in the range we handle.
249 rptr
= mtod(mp
, u_char
*);
250 proto
= PPP_PROTOCOL(rptr
);
251 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb) {
256 /* Allocate one mbuf initially. */
257 if (maxolen
> orig_len
)
259 MGET(m
, M_DONTWAIT
, MT_DATA
);
263 if (maxolen
+ state
->hdrlen
> MLEN
)
264 MCLGET(m
, M_DONTWAIT
);
265 wspace
= M_TRAILINGSPACE(m
);
266 if (state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
267 m
->m_data
+= state
->hdrlen
;
268 wspace
-= state
->hdrlen
;
270 wptr
= mtod(m
, u_char
*);
273 * Copy over the PPP header and store the 2-byte sequence number.
275 wptr
[0] = PPP_ADDRESS(rptr
);
276 wptr
[1] = PPP_CONTROL(rptr
);
277 wptr
[2] = PPP_COMP
>> 8;
280 wptr
[0] = state
->seqno
>> 8;
281 wptr
[1] = state
->seqno
;
283 state
->strm
.next_out
= wptr
;
284 state
->strm
.avail_out
= wspace
- (PPP_HDRLEN
+ 2);
286 state
->strm
.next_out
= NULL
;
287 state
->strm
.avail_out
= 1000000;
293 rptr
+= (proto
> 0xff)? 2: 3; /* skip 1st proto byte if 0 */
294 state
->strm
.next_in
= rptr
;
295 state
->strm
.avail_in
= mtod(mp
, u_char
*) + mp
->m_len
- rptr
;
297 flush
= (mp
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
300 r
= deflate(&state
->strm
, flush
);
302 printf("z_compress: deflate returned %d (%s)\n",
303 r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
306 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
307 break; /* all done */
308 if (state
->strm
.avail_in
== 0 && mp
!= NULL
) {
309 state
->strm
.next_in
= mtod(mp
, u_char
*);
310 state
->strm
.avail_in
= mp
->m_len
;
313 flush
= Z_PACKET_FLUSH
;
315 if (state
->strm
.avail_out
== 0) {
319 MGET(m
->m_next
, M_DONTWAIT
, MT_DATA
);
323 if (maxolen
- olen
> MLEN
)
324 MCLGET(m
, M_DONTWAIT
);
325 state
->strm
.next_out
= mtod(m
, u_char
*);
326 state
->strm
.avail_out
= wspace
= M_TRAILINGSPACE(m
);
330 state
->strm
.next_out
= NULL
;
331 state
->strm
.avail_out
= 1000000;
336 olen
+= (m
->m_len
= wspace
- state
->strm
.avail_out
);
339 * See if we managed to reduce the size of the packet.
340 * If the compressor just gave us a single zero byte, it means
341 * the packet was incompressible.
343 if (m
!= NULL
&& olen
< orig_len
344 && !(olen
== PPP_HDRLEN
+ 3 && *wptr
== 0)) {
345 state
->stats
.comp_bytes
+= olen
;
346 state
->stats
.comp_packets
++;
352 state
->stats
.inc_bytes
+= orig_len
;
353 state
->stats
.inc_packets
++;
356 state
->stats
.unc_bytes
+= orig_len
;
357 state
->stats
.unc_packets
++;
363 z_comp_stats(arg
, stats
)
365 struct compstat
*stats
;
367 struct deflate_state
*state
= (struct deflate_state
*) arg
;
370 *stats
= state
->stats
;
371 stats
->ratio
= stats
->unc_bytes
;
372 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
373 if (stats
->ratio
<= 0x7ffffff)
382 * Allocate space for a decompressor.
385 z_decomp_alloc(options
, opt_len
)
389 struct deflate_state
*state
;
392 if (opt_len
!= CILEN_DEFLATE
393 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
394 || options
[1] != CILEN_DEFLATE
395 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
396 || options
[3] != DEFLATE_CHK_SEQUENCE
)
398 w_size
= DEFLATE_SIZE(options
[2]);
399 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
402 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
407 state
->strm
.next_out
= NULL
;
408 state
->strm
.zalloc
= zalloc
;
409 state
->strm
.zfree
= zfree
;
410 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
411 FREE(state
, M_DEVBUF
);
415 state
->w_size
= w_size
;
416 bzero(&state
->stats
, sizeof(state
->stats
));
417 return (void *) state
;
424 struct deflate_state
*state
= (struct deflate_state
*) arg
;
426 inflateEnd(&state
->strm
);
427 FREE(state
, M_DEVBUF
);
431 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
434 int opt_len
, unit
, hdrlen
, mru
, debug
;
436 struct deflate_state
*state
= (struct deflate_state
*) arg
;
438 if (opt_len
< CILEN_DEFLATE
439 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
440 || options
[1] != CILEN_DEFLATE
441 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
442 || DEFLATE_SIZE(options
[2]) != state
->w_size
443 || options
[3] != DEFLATE_CHK_SEQUENCE
)
448 state
->hdrlen
= hdrlen
;
449 state
->debug
= debug
;
452 inflateReset(&state
->strm
);
461 struct deflate_state
*state
= (struct deflate_state
*) arg
;
464 inflateReset(&state
->strm
);
468 * Decompress a Deflate-compressed packet.
470 * Because of patent problems, we return DECOMP_ERROR for errors
471 * found by inspecting the input data and for system problems, but
472 * DECOMP_FATALERROR for any errors which could possibly be said to
473 * be being detected "after" decompression. For DECOMP_ERROR,
474 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
475 * infringing a patent of Motorola's if we do, so we take CCP down
478 * Given that the frame has the correct sequence number and a good FCS,
479 * errors such as invalid codes in the input most likely indicate a
480 * bug, so we return DECOMP_FATALERROR for them in order to turn off
481 * compression, even though they are detected by inspecting the input.
484 z_decompress(arg
, mi
, mop
)
486 struct mbuf
*mi
, **mop
;
488 struct deflate_state
*state
= (struct deflate_state
*) arg
;
489 struct mbuf
*mo
, *mo_head
;
491 int rlen
, olen
, ospace
;
492 int seq
, i
, flush
, r
, decode_proto
;
493 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
496 rptr
= mtod(mi
, u_char
*);
498 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
503 rptr
= mtod(mi
, u_char
*);
510 /* Check the sequence number. */
511 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
512 if (seq
!= state
->seqno
) {
514 printf("z_decompress%d: bad seq # %d, expected %d\n",
515 state
->unit
, seq
, state
->seqno
);
520 /* Allocate an output mbuf. */
521 MGETHDR(mo
, M_DONTWAIT
, MT_DATA
);
527 MCLGET(mo
, M_DONTWAIT
);
528 ospace
= M_TRAILINGSPACE(mo
);
529 if (state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
530 mo
->m_data
+= state
->hdrlen
;
531 ospace
-= state
->hdrlen
;
535 * Fill in the first part of the PPP header. The protocol field
536 * comes from the decompressed data.
538 wptr
= mtod(mo
, u_char
*);
539 wptr
[0] = PPP_ADDRESS(hdr
);
540 wptr
[1] = PPP_CONTROL(hdr
);
544 * Set up to call inflate. We set avail_out to 1 initially so we can
545 * look at the first byte of the output and decide whether we have
546 * a 1-byte or 2-byte protocol field.
548 state
->strm
.next_in
= rptr
;
549 state
->strm
.avail_in
= rlen
;
551 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
552 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
553 state
->strm
.next_out
= wptr
+ 3;
554 state
->strm
.avail_out
= 1;
559 * Call inflate, supplying more input or output as needed.
562 r
= inflate(&state
->strm
, flush
);
567 printf("z_decompress%d: inflate returned %d (%s)\n",
568 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
570 return DECOMP_FATALERROR
;
572 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
573 break; /* all done */
574 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
575 state
->strm
.next_in
= mtod(mi
, u_char
*);
576 state
->strm
.avail_in
= mi
->m_len
;
580 flush
= Z_PACKET_FLUSH
;
582 if (state
->strm
.avail_out
== 0) {
584 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
585 if ((wptr
[3] & 1) == 0) {
586 /* 2-byte protocol field */
588 --state
->strm
.next_out
;
589 ++state
->strm
.avail_out
;
596 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
602 MCLGET(mo
, M_DONTWAIT
);
603 state
->strm
.next_out
= mtod(mo
, u_char
*);
604 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
612 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
614 if (olen
> state
->mru
+ PPP_HDRLEN
)
615 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
616 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
619 state
->stats
.unc_bytes
+= olen
;
620 state
->stats
.unc_packets
++;
621 state
->stats
.comp_bytes
+= rlen
;
622 state
->stats
.comp_packets
++;
629 * Incompressible data has arrived - add it to the history.
636 struct deflate_state
*state
= (struct deflate_state
*) arg
;
641 * Check that the protocol is one we handle.
643 rptr
= mtod(mi
, u_char
*);
644 proto
= PPP_PROTOCOL(rptr
);
645 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
651 * Iterate through the mbufs, adding the characters in them
652 * to the decompressor's history. For the first mbuf, we start
653 * at the either the 1st or 2nd byte of the protocol field,
654 * depending on whether the protocol value is compressible.
657 state
->strm
.next_in
= rptr
+ 3;
658 state
->strm
.avail_in
= rlen
- 3;
660 --state
->strm
.next_in
;
661 ++state
->strm
.avail_in
;
664 r
= inflateIncomp(&state
->strm
);
670 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
671 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
677 state
->strm
.next_in
= mtod(mi
, u_char
*);
678 state
->strm
.avail_in
= mi
->m_len
;
685 state
->stats
.inc_bytes
+= rlen
;
686 state
->stats
.inc_packets
++;
687 state
->stats
.unc_bytes
+= rlen
;
688 state
->stats
.unc_packets
++;
691 #endif /* DO_DEFLATE */