1 /* $NetBSD: ppp-deflate.c,v 1.6 1998/05/02 14:34:25 christos Exp $ */
2 /* Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 paulus 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 Paul Mackerras. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
23 * 3. The name(s) of the authors of this software must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission.
27 * 4. Redistributions of any form whatsoever must retain the following
29 * "This product includes software developed by Paul Mackerras
30 * <paulus@samba.org>".
32 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
33 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
34 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
35 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
36 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
37 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
38 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
45 #include <net/ppp_defs.h>
48 #define PACKETPTR struct mbuf *
49 #include <net/ppp-comp.h>
53 #define DEFLATE_DEBUG 1
56 * State for a Deflate (de)compressor.
58 struct deflate_state
{
66 struct compstat stats
;
69 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
71 static void *zalloc
__P((void *, u_int items
, u_int size
));
72 static void zfree
__P((void *, void *ptr
));
73 static void *z_comp_alloc
__P((u_char
*options
, int opt_len
));
74 static void *z_decomp_alloc
__P((u_char
*options
, int opt_len
));
75 static void z_comp_free
__P((void *state
));
76 static void z_decomp_free
__P((void *state
));
77 static int z_comp_init
__P((void *state
, u_char
*options
, int opt_len
,
78 int unit
, int hdrlen
, int debug
));
79 static int z_decomp_init
__P((void *state
, u_char
*options
, int opt_len
,
80 int unit
, int hdrlen
, int mru
, int debug
));
81 static int z_compress
__P((void *state
, struct mbuf
**mret
,
82 struct mbuf
*mp
, int slen
, int maxolen
));
83 static void z_incomp
__P((void *state
, struct mbuf
*dmsg
));
84 static int z_decompress
__P((void *state
, struct mbuf
*cmp
,
86 static void z_comp_reset
__P((void *state
));
87 static void z_decomp_reset
__P((void *state
));
88 static void z_comp_stats
__P((void *state
, struct compstat
*stats
));
91 * Procedures exported to if_ppp.c.
93 struct compressor ppp_deflate
= {
94 CI_DEFLATE
, /* compress_proto */
95 z_comp_alloc
, /* comp_alloc */
96 z_comp_free
, /* comp_free */
97 z_comp_init
, /* comp_init */
98 z_comp_reset
, /* comp_reset */
99 z_compress
, /* compress */
100 z_comp_stats
, /* comp_stat */
101 z_decomp_alloc
, /* decomp_alloc */
102 z_decomp_free
, /* decomp_free */
103 z_decomp_init
, /* decomp_init */
104 z_decomp_reset
, /* decomp_reset */
105 z_decompress
, /* decompress */
106 z_incomp
, /* incomp */
107 z_comp_stats
, /* decomp_stat */
110 struct compressor ppp_deflate_draft
= {
111 CI_DEFLATE_DRAFT
, /* compress_proto */
112 z_comp_alloc
, /* comp_alloc */
113 z_comp_free
, /* comp_free */
114 z_comp_init
, /* comp_init */
115 z_comp_reset
, /* comp_reset */
116 z_compress
, /* compress */
117 z_comp_stats
, /* comp_stat */
118 z_decomp_alloc
, /* decomp_alloc */
119 z_decomp_free
, /* decomp_free */
120 z_decomp_init
, /* decomp_init */
121 z_decomp_reset
, /* decomp_reset */
122 z_decompress
, /* decompress */
123 z_incomp
, /* incomp */
124 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.
341 if (m
!= NULL
&& olen
< orig_len
) {
342 state
->stats
.comp_bytes
+= olen
;
343 state
->stats
.comp_packets
++;
349 state
->stats
.inc_bytes
+= orig_len
;
350 state
->stats
.inc_packets
++;
353 state
->stats
.unc_bytes
+= orig_len
;
354 state
->stats
.unc_packets
++;
360 z_comp_stats(arg
, stats
)
362 struct compstat
*stats
;
364 struct deflate_state
*state
= (struct deflate_state
*) arg
;
367 *stats
= state
->stats
;
368 stats
->ratio
= stats
->unc_bytes
;
369 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
370 if (stats
->ratio
<= 0x7ffffff)
379 * Allocate space for a decompressor.
382 z_decomp_alloc(options
, opt_len
)
386 struct deflate_state
*state
;
389 if (opt_len
!= CILEN_DEFLATE
390 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
391 || options
[1] != CILEN_DEFLATE
392 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
393 || options
[3] != DEFLATE_CHK_SEQUENCE
)
395 w_size
= DEFLATE_SIZE(options
[2]);
396 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
399 MALLOC(state
, struct deflate_state
*, sizeof(struct deflate_state
),
404 state
->strm
.next_out
= NULL
;
405 state
->strm
.zalloc
= zalloc
;
406 state
->strm
.zfree
= zfree
;
407 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
408 FREE(state
, M_DEVBUF
);
412 state
->w_size
= w_size
;
413 bzero(&state
->stats
, sizeof(state
->stats
));
414 return (void *) state
;
421 struct deflate_state
*state
= (struct deflate_state
*) arg
;
423 inflateEnd(&state
->strm
);
424 FREE(state
, M_DEVBUF
);
428 z_decomp_init(arg
, options
, opt_len
, unit
, hdrlen
, mru
, debug
)
431 int opt_len
, unit
, hdrlen
, mru
, debug
;
433 struct deflate_state
*state
= (struct deflate_state
*) arg
;
435 if (opt_len
< CILEN_DEFLATE
436 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
437 || options
[1] != CILEN_DEFLATE
438 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
439 || DEFLATE_SIZE(options
[2]) != state
->w_size
440 || options
[3] != DEFLATE_CHK_SEQUENCE
)
445 state
->hdrlen
= hdrlen
;
446 state
->debug
= debug
;
449 inflateReset(&state
->strm
);
458 struct deflate_state
*state
= (struct deflate_state
*) arg
;
461 inflateReset(&state
->strm
);
465 * Decompress a Deflate-compressed packet.
467 * Because of patent problems, we return DECOMP_ERROR for errors
468 * found by inspecting the input data and for system problems, but
469 * DECOMP_FATALERROR for any errors which could possibly be said to
470 * be being detected "after" decompression. For DECOMP_ERROR,
471 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
472 * infringing a patent of Motorola's if we do, so we take CCP down
475 * Given that the frame has the correct sequence number and a good FCS,
476 * errors such as invalid codes in the input most likely indicate a
477 * bug, so we return DECOMP_FATALERROR for them in order to turn off
478 * compression, even though they are detected by inspecting the input.
481 z_decompress(arg
, mi
, mop
)
483 struct mbuf
*mi
, **mop
;
485 struct deflate_state
*state
= (struct deflate_state
*) arg
;
486 struct mbuf
*mo
, *mo_head
;
488 int rlen
, olen
, ospace
;
489 int seq
, i
, flush
, r
, decode_proto
;
490 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
493 rptr
= mtod(mi
, u_char
*);
495 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
500 rptr
= mtod(mi
, u_char
*);
507 /* Check the sequence number. */
508 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
509 if (seq
!= state
->seqno
) {
511 printf("z_decompress%d: bad seq # %d, expected %d\n",
512 state
->unit
, seq
, state
->seqno
);
517 /* Allocate an output mbuf. */
518 MGETHDR(mo
, M_DONTWAIT
, MT_DATA
);
524 MCLGET(mo
, M_DONTWAIT
);
525 ospace
= M_TRAILINGSPACE(mo
);
526 if (state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
527 mo
->m_data
+= state
->hdrlen
;
528 ospace
-= state
->hdrlen
;
532 * Fill in the first part of the PPP header. The protocol field
533 * comes from the decompressed data.
535 wptr
= mtod(mo
, u_char
*);
536 wptr
[0] = PPP_ADDRESS(hdr
);
537 wptr
[1] = PPP_CONTROL(hdr
);
541 * Set up to call inflate. We set avail_out to 1 initially so we can
542 * look at the first byte of the output and decide whether we have
543 * a 1-byte or 2-byte protocol field.
545 state
->strm
.next_in
= rptr
;
546 state
->strm
.avail_in
= rlen
;
548 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
549 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
550 state
->strm
.next_out
= wptr
+ 3;
551 state
->strm
.avail_out
= 1;
556 * Call inflate, supplying more input or output as needed.
559 r
= inflate(&state
->strm
, flush
);
564 printf("z_decompress%d: inflate returned %d (%s)\n",
565 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
567 return DECOMP_FATALERROR
;
569 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
570 break; /* all done */
571 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
572 state
->strm
.next_in
= mtod(mi
, u_char
*);
573 state
->strm
.avail_in
= mi
->m_len
;
577 flush
= Z_PACKET_FLUSH
;
579 if (state
->strm
.avail_out
== 0) {
581 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
582 if ((wptr
[3] & 1) == 0) {
583 /* 2-byte protocol field */
585 --state
->strm
.next_out
;
586 ++state
->strm
.avail_out
;
593 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
599 MCLGET(mo
, M_DONTWAIT
);
600 state
->strm
.next_out
= mtod(mo
, u_char
*);
601 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
609 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
611 if (olen
> state
->mru
+ PPP_HDRLEN
)
612 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
613 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
616 state
->stats
.unc_bytes
+= olen
;
617 state
->stats
.unc_packets
++;
618 state
->stats
.comp_bytes
+= rlen
;
619 state
->stats
.comp_packets
++;
626 * Incompressible data has arrived - add it to the history.
633 struct deflate_state
*state
= (struct deflate_state
*) arg
;
638 * Check that the protocol is one we handle.
640 rptr
= mtod(mi
, u_char
*);
641 proto
= PPP_PROTOCOL(rptr
);
642 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
648 * Iterate through the mbufs, adding the characters in them
649 * to the decompressor's history. For the first mbuf, we start
650 * at the either the 1st or 2nd byte of the protocol field,
651 * depending on whether the protocol value is compressible.
654 state
->strm
.next_in
= rptr
+ 3;
655 state
->strm
.avail_in
= rlen
- 3;
657 --state
->strm
.next_in
;
658 ++state
->strm
.avail_in
;
661 r
= inflateIncomp(&state
->strm
);
667 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
668 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
674 state
->strm
.next_in
= mtod(mi
, u_char
*);
675 state
->strm
.avail_in
= mi
->m_len
;
682 state
->stats
.inc_bytes
+= rlen
;
683 state
->stats
.inc_packets
++;
684 state
->stats
.unc_bytes
+= rlen
;
685 state
->stats
.unc_packets
++;
688 #endif /* DO_DEFLATE */