1 /* $NetBSD: ppp-deflate.c,v 1.19 2008/11/29 23:15:20 cube 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) 1989-2002 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/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ppp-deflate.c,v 1.19 2008/11/29 23:15:20 cube Exp $");
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/module.h>
49 #include <net/ppp_defs.h>
50 #include <net/if_ppp.h>
53 #define PACKETPTR struct mbuf *
54 #include <net/ppp-comp.h>
58 #define DEFLATE_DEBUG 1
61 * State for a Deflate (de)compressor.
63 struct deflate_state
{
71 struct compstat stats
;
74 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
76 static void *zalloc(void *, u_int items
, u_int size
);
77 static void zfree(void *, void *ptr
);
78 static void *z_comp_alloc(u_char
*options
, int opt_len
);
79 static void *z_decomp_alloc(u_char
*options
, int opt_len
);
80 static void z_comp_free(void *state
);
81 static void z_decomp_free(void *state
);
82 static int z_comp_init(void *state
, u_char
*options
, int opt_len
,
83 int unit
, int hdrlen
, int debug
);
84 static int z_decomp_init(void *state
, u_char
*options
, int opt_len
,
85 int unit
, int hdrlen
, int mru
, int debug
);
86 static int z_compress(void *state
, struct mbuf
**mret
,
87 struct mbuf
*mp
, int slen
, int maxolen
);
88 static void z_incomp(void *state
, struct mbuf
*dmsg
);
89 static int z_decompress(void *state
, struct mbuf
*cmp
,
91 static void z_comp_reset(void *state
);
92 static void z_decomp_reset(void *state
);
93 static void z_comp_stats(void *state
, struct compstat
*stats
);
96 * Procedures exported to if_ppp.c.
98 static struct compressor ppp_deflate
[2] = { {
99 .compress_proto
= CI_DEFLATE
,
100 .comp_alloc
= z_comp_alloc
,
101 .comp_free
= z_comp_free
,
102 .comp_init
= z_comp_init
,
103 .comp_reset
= z_comp_reset
,
104 .compress
= z_compress
,
105 .comp_stat
= z_comp_stats
,
106 .decomp_alloc
= z_decomp_alloc
,
107 .decomp_free
= z_decomp_free
,
108 .decomp_init
= z_decomp_init
,
109 .decomp_reset
= z_decomp_reset
,
110 .decompress
= z_decompress
,
112 .decomp_stat
= z_comp_stats
,
115 .compress_proto
= CI_DEFLATE_DRAFT
,
116 .comp_alloc
= z_comp_alloc
,
117 .comp_free
= z_comp_free
,
118 .comp_init
= z_comp_init
,
119 .comp_reset
= z_comp_reset
,
120 .compress
= z_compress
,
121 .comp_stat
= z_comp_stats
,
122 .decomp_alloc
= z_decomp_alloc
,
123 .decomp_free
= z_decomp_free
,
124 .decomp_init
= z_decomp_init
,
125 .decomp_reset
= z_decomp_reset
,
126 .decompress
= z_decompress
,
128 .decomp_stat
= z_comp_stats
,
131 * Space allocation and freeing routines for use by zlib routines.
134 zalloc(void *notused
, u_int items
, u_int size
)
138 ptr
= malloc(items
* size
, M_DEVBUF
, M_NOWAIT
);
143 zfree(void *notused
, void *ptr
)
149 * Allocate space for a compressor.
152 z_comp_alloc(u_char
*options
, int opt_len
)
154 struct deflate_state
*state
;
157 if (opt_len
!= CILEN_DEFLATE
158 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
159 || options
[1] != CILEN_DEFLATE
160 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
161 || options
[3] != DEFLATE_CHK_SEQUENCE
)
163 w_size
= DEFLATE_SIZE(options
[2]);
164 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
167 state
= malloc(sizeof(struct deflate_state
), M_DEVBUF
, M_NOWAIT
);
171 state
->strm
.next_in
= NULL
;
172 state
->strm
.zalloc
= zalloc
;
173 state
->strm
.zfree
= zfree
;
174 if (deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
, DEFLATE_METHOD_VAL
,
175 -w_size
, 8, Z_DEFAULT_STRATEGY
) != Z_OK
) {
176 free(state
, M_DEVBUF
);
180 state
->w_size
= w_size
;
181 memset(&state
->stats
, 0, sizeof(state
->stats
));
182 return (void *) state
;
186 z_comp_free(void *arg
)
188 struct deflate_state
*state
= (struct deflate_state
*) arg
;
190 deflateEnd(&state
->strm
);
191 free(state
, M_DEVBUF
);
195 z_comp_init(void *arg
, u_char
*options
, int opt_len
, int unit
, int hdrlen
,
198 struct deflate_state
*state
= (struct deflate_state
*) arg
;
200 if (opt_len
< CILEN_DEFLATE
201 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
202 || options
[1] != CILEN_DEFLATE
203 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
204 || DEFLATE_SIZE(options
[2]) != state
->w_size
205 || options
[3] != DEFLATE_CHK_SEQUENCE
)
210 state
->hdrlen
= hdrlen
;
211 state
->debug
= debug
;
213 deflateReset(&state
->strm
);
219 z_comp_reset(void *arg
)
221 struct deflate_state
*state
= (struct deflate_state
*) arg
;
224 deflateReset(&state
->strm
);
228 z_compress(void *arg
,
229 struct mbuf
**mret
/* compressed packet (out) */,
230 struct mbuf
*mp
/* uncompressed packet (in) */,
231 int orig_len
, int maxolen
)
233 struct deflate_state
*state
= (struct deflate_state
*) arg
;
235 int proto
, olen
, wspace
, r
, flush
;
239 * Check that the protocol is in the range we handle.
241 rptr
= mtod(mp
, u_char
*);
242 proto
= PPP_PROTOCOL(rptr
);
243 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb) {
248 /* Allocate one mbuf initially. */
249 if (maxolen
> orig_len
)
251 MGET(m
, M_DONTWAIT
, MT_DATA
);
255 if (maxolen
+ state
->hdrlen
> MLEN
)
256 MCLGET(m
, M_DONTWAIT
);
257 wspace
= M_TRAILINGSPACE(m
);
258 if (state
->hdrlen
+ PPP_HDRLEN
+ 2 < wspace
) {
259 m
->m_data
+= state
->hdrlen
;
260 wspace
-= state
->hdrlen
;
262 wptr
= mtod(m
, u_char
*);
265 * Copy over the PPP header and store the 2-byte sequence number.
267 wptr
[0] = PPP_ADDRESS(rptr
);
268 wptr
[1] = PPP_CONTROL(rptr
);
269 wptr
[2] = PPP_COMP
>> 8;
272 wptr
[0] = state
->seqno
>> 8;
273 wptr
[1] = state
->seqno
;
275 state
->strm
.next_out
= wptr
;
276 state
->strm
.avail_out
= wspace
- (PPP_HDRLEN
+ 2);
278 state
->strm
.next_out
= NULL
;
279 state
->strm
.avail_out
= 1000000;
285 rptr
+= (proto
> 0xff)? 2: 3; /* skip 1st proto byte if 0 */
286 state
->strm
.next_in
= rptr
;
287 state
->strm
.avail_in
= mtod(mp
, u_char
*) + mp
->m_len
- rptr
;
289 flush
= (mp
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
292 r
= deflate(&state
->strm
, flush
);
294 printf("z_compress: deflate returned %d (%s)\n",
295 r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
298 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
299 break; /* all done */
300 if (state
->strm
.avail_in
== 0 && mp
!= NULL
) {
301 state
->strm
.next_in
= mtod(mp
, u_char
*);
302 state
->strm
.avail_in
= mp
->m_len
;
305 flush
= Z_PACKET_FLUSH
;
307 if (state
->strm
.avail_out
== 0) {
311 MGET(m
->m_next
, M_DONTWAIT
, MT_DATA
);
315 if (maxolen
- olen
> MLEN
)
316 MCLGET(m
, M_DONTWAIT
);
317 state
->strm
.next_out
= mtod(m
, u_char
*);
318 state
->strm
.avail_out
= wspace
= M_TRAILINGSPACE(m
);
322 state
->strm
.next_out
= NULL
;
323 state
->strm
.avail_out
= 1000000;
328 olen
+= (m
->m_len
= wspace
- state
->strm
.avail_out
);
331 * See if we managed to reduce the size of the packet.
333 if (m
!= NULL
&& olen
< orig_len
) {
334 state
->stats
.comp_bytes
+= olen
;
335 state
->stats
.comp_packets
++;
341 state
->stats
.inc_bytes
+= orig_len
;
342 state
->stats
.inc_packets
++;
345 state
->stats
.unc_bytes
+= orig_len
;
346 state
->stats
.unc_packets
++;
352 z_comp_stats(void *arg
, struct compstat
*stats
)
354 struct deflate_state
*state
= (struct deflate_state
*) arg
;
357 *stats
= state
->stats
;
358 stats
->ratio
= stats
->unc_bytes
;
359 out
= stats
->comp_bytes
+ stats
->inc_bytes
;
360 if (stats
->ratio
<= 0x7ffffff)
369 * Allocate space for a decompressor.
372 z_decomp_alloc(u_char
*options
, int opt_len
)
374 struct deflate_state
*state
;
377 if (opt_len
!= CILEN_DEFLATE
378 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
379 || options
[1] != CILEN_DEFLATE
380 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
381 || options
[3] != DEFLATE_CHK_SEQUENCE
)
383 w_size
= DEFLATE_SIZE(options
[2]);
384 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
387 state
= malloc(sizeof(struct deflate_state
), M_DEVBUF
, M_NOWAIT
);
391 state
->strm
.next_out
= NULL
;
392 state
->strm
.zalloc
= zalloc
;
393 state
->strm
.zfree
= zfree
;
394 if (inflateInit2(&state
->strm
, -w_size
) != Z_OK
) {
395 free(state
, M_DEVBUF
);
399 state
->w_size
= w_size
;
400 memset(&state
->stats
, 0, sizeof(state
->stats
));
401 return (void *) state
;
405 z_decomp_free(void *arg
)
407 struct deflate_state
*state
= (struct deflate_state
*) arg
;
409 inflateEnd(&state
->strm
);
410 free(state
, M_DEVBUF
);
414 z_decomp_init(void *arg
, u_char
*options
, int opt_len
, int unit
, int hdrlen
,
417 struct deflate_state
*state
= (struct deflate_state
*) arg
;
419 if (opt_len
< CILEN_DEFLATE
420 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
421 || options
[1] != CILEN_DEFLATE
422 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
423 || DEFLATE_SIZE(options
[2]) != state
->w_size
424 || options
[3] != DEFLATE_CHK_SEQUENCE
)
429 state
->hdrlen
= hdrlen
;
430 state
->debug
= debug
;
433 inflateReset(&state
->strm
);
439 z_decomp_reset(void *arg
)
441 struct deflate_state
*state
= (struct deflate_state
*) arg
;
444 inflateReset(&state
->strm
);
448 * Decompress a Deflate-compressed packet.
450 * Because of patent problems, we return DECOMP_ERROR for errors
451 * found by inspecting the input data and for system problems, but
452 * DECOMP_FATALERROR for any errors which could possibly be said to
453 * be being detected "after" decompression. For DECOMP_ERROR,
454 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
455 * infringing a patent of Motorola's if we do, so we take CCP down
458 * Given that the frame has the correct sequence number and a good FCS,
459 * errors such as invalid codes in the input most likely indicate a
460 * bug, so we return DECOMP_FATALERROR for them in order to turn off
461 * compression, even though they are detected by inspecting the input.
464 z_decompress(void *arg
, struct mbuf
*mi
, struct mbuf
**mop
)
466 struct deflate_state
*state
= (struct deflate_state
*) arg
;
467 struct mbuf
*mo
, *mo_head
;
469 int rlen
, olen
, ospace
;
470 int seq
, i
, flush
, r
, decode_proto
;
471 u_char hdr
[PPP_HDRLEN
+ DEFLATE_OVHD
];
474 rptr
= mtod(mi
, u_char
*);
476 for (i
= 0; i
< PPP_HDRLEN
+ DEFLATE_OVHD
; ++i
) {
481 rptr
= mtod(mi
, u_char
*);
488 /* Check the sequence number. */
489 seq
= (hdr
[PPP_HDRLEN
] << 8) + hdr
[PPP_HDRLEN
+1];
490 if (seq
!= state
->seqno
) {
492 printf("z_decompress%d: bad seq # %d, expected %d\n",
493 state
->unit
, seq
, state
->seqno
);
498 /* Allocate an output mbuf. */
499 MGETHDR(mo
, M_DONTWAIT
, MT_DATA
);
505 MCLGET(mo
, M_DONTWAIT
);
506 ospace
= M_TRAILINGSPACE(mo
);
507 if (state
->hdrlen
+ PPP_HDRLEN
< ospace
) {
508 mo
->m_data
+= state
->hdrlen
;
509 ospace
-= state
->hdrlen
;
513 * Fill in the first part of the PPP header. The protocol field
514 * comes from the decompressed data.
516 wptr
= mtod(mo
, u_char
*);
517 wptr
[0] = PPP_ADDRESS(hdr
);
518 wptr
[1] = PPP_CONTROL(hdr
);
522 * Set up to call inflate. We set avail_out to 1 initially so we can
523 * look at the first byte of the output and decide whether we have
524 * a 1-byte or 2-byte protocol field.
526 state
->strm
.next_in
= rptr
;
527 state
->strm
.avail_in
= rlen
;
529 flush
= (mi
== NULL
)? Z_PACKET_FLUSH
: Z_NO_FLUSH
;
530 rlen
+= PPP_HDRLEN
+ DEFLATE_OVHD
;
531 state
->strm
.next_out
= wptr
+ 3;
532 state
->strm
.avail_out
= 1;
537 * Call inflate, supplying more input or output as needed.
540 r
= inflate(&state
->strm
, flush
);
545 printf("z_decompress%d: inflate returned %d (%s)\n",
546 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
548 return DECOMP_FATALERROR
;
550 if (flush
!= Z_NO_FLUSH
&& state
->strm
.avail_out
!= 0)
551 break; /* all done */
552 if (state
->strm
.avail_in
== 0 && mi
!= NULL
) {
553 state
->strm
.next_in
= mtod(mi
, u_char
*);
554 state
->strm
.avail_in
= mi
->m_len
;
558 flush
= Z_PACKET_FLUSH
;
560 if (state
->strm
.avail_out
== 0) {
562 state
->strm
.avail_out
= ospace
- PPP_HDRLEN
;
563 if ((wptr
[3] & 1) == 0) {
564 /* 2-byte protocol field */
566 --state
->strm
.next_out
;
567 ++state
->strm
.avail_out
;
574 MGET(mo
->m_next
, M_DONTWAIT
, MT_DATA
);
580 MCLGET(mo
, M_DONTWAIT
);
581 state
->strm
.next_out
= mtod(mo
, u_char
*);
582 state
->strm
.avail_out
= ospace
= M_TRAILINGSPACE(mo
);
590 olen
+= (mo
->m_len
= ospace
- state
->strm
.avail_out
);
592 if (olen
> state
->mru
+ PPP_HDRLEN
)
593 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
594 state
->unit
, olen
, state
->mru
+ PPP_HDRLEN
);
597 state
->stats
.unc_bytes
+= olen
;
598 state
->stats
.unc_packets
++;
599 state
->stats
.comp_bytes
+= rlen
;
600 state
->stats
.comp_packets
++;
607 * Incompressible data has arrived - add it to the history.
610 z_incomp(void *arg
, struct mbuf
*mi
)
612 struct deflate_state
*state
= (struct deflate_state
*) arg
;
617 * Check that the protocol is one we handle.
619 rptr
= mtod(mi
, u_char
*);
620 proto
= PPP_PROTOCOL(rptr
);
621 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
627 * Iterate through the mbufs, adding the characters in them
628 * to the decompressor's history. For the first mbuf, we start
629 * at the either the 1st or 2nd byte of the protocol field,
630 * depending on whether the protocol value is compressible.
633 state
->strm
.next_in
= rptr
+ 3;
634 state
->strm
.avail_in
= rlen
- 3;
636 --state
->strm
.next_in
;
637 ++state
->strm
.avail_in
;
640 r
= inflateIncomp(&state
->strm
);
646 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
647 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
653 state
->strm
.next_in
= mtod(mi
, u_char
*);
654 state
->strm
.avail_in
= mi
->m_len
;
661 state
->stats
.inc_bytes
+= rlen
;
662 state
->stats
.inc_packets
++;
663 state
->stats
.unc_bytes
+= rlen
;
664 state
->stats
.unc_packets
++;
667 MODULE(MODULE_CLASS_MISC
, ppp_deflate
, NULL
);
670 ppp_deflate_modcmd(modcmd_t cmd
, void *arg
)
674 case MODULE_CMD_INIT
:
675 return ppp_register_compressor(ppp_deflate
, 2);
676 case MODULE_CMD_FINI
:
677 return ppp_unregister_compressor(ppp_deflate
, 2);
678 case MODULE_CMD_STAT
:
686 #endif /* DO_DEFLATE */