2 * ==FILEVERSION 980319==
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 Linux kernel 1.3.X.
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,
31 * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/init.h>
38 #include <linux/string.h>
40 #include <linux/ppp_defs.h>
41 #include <linux/ppp-comp.h>
43 #include <linux/zlib.h>
46 * State for a Deflate (de)compressor.
48 struct ppp_deflate_state
{
55 struct compstat stats
;
58 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
60 static void *z_comp_alloc(unsigned char *options
, int opt_len
);
61 static void *z_decomp_alloc(unsigned char *options
, int opt_len
);
62 static void z_comp_free(void *state
);
63 static void z_decomp_free(void *state
);
64 static int z_comp_init(void *state
, unsigned char *options
,
66 int unit
, int hdrlen
, int debug
);
67 static int z_decomp_init(void *state
, unsigned char *options
,
69 int unit
, int hdrlen
, int mru
, int debug
);
70 static int z_compress(void *state
, unsigned char *rptr
,
72 int isize
, int osize
);
73 static void z_incomp(void *state
, unsigned char *ibuf
, int icnt
);
74 static int z_decompress(void *state
, unsigned char *ibuf
,
75 int isize
, unsigned char *obuf
, int osize
);
76 static void z_comp_reset(void *state
);
77 static void z_decomp_reset(void *state
);
78 static void z_comp_stats(void *state
, struct compstat
*stats
);
81 * z_comp_free - free the memory used by a compressor
82 * @arg: pointer to the private state for the compressor.
84 static void z_comp_free(void *arg
)
86 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
89 zlib_deflateEnd(&state
->strm
);
90 vfree(state
->strm
.workspace
);
96 * z_comp_alloc - allocate space for a compressor.
97 * @options: pointer to CCP option data
98 * @opt_len: length of the CCP option at @options.
100 * The @options pointer points to the a buffer containing the
101 * CCP option data for the compression being negotiated. It is
102 * formatted according to RFC1979, and describes the window
103 * size that the peer is requesting that we use in compressing
104 * data to be sent to it.
106 * Returns the pointer to the private state for the compressor,
107 * or NULL if we could not allocate enough memory.
109 static void *z_comp_alloc(unsigned char *options
, int opt_len
)
111 struct ppp_deflate_state
*state
;
114 if (opt_len
!= CILEN_DEFLATE
115 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
116 || options
[1] != CILEN_DEFLATE
117 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
118 || options
[3] != DEFLATE_CHK_SEQUENCE
)
120 w_size
= DEFLATE_SIZE(options
[2]);
121 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
124 state
= (struct ppp_deflate_state
*) kmalloc(sizeof(*state
),
129 memset (state
, 0, sizeof (struct ppp_deflate_state
));
130 state
->strm
.next_in
= NULL
;
131 state
->w_size
= w_size
;
132 state
->strm
.workspace
= vmalloc(zlib_deflate_workspacesize());
133 if (state
->strm
.workspace
== NULL
)
136 if (zlib_deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
,
137 DEFLATE_METHOD_VAL
, -w_size
, 8, Z_DEFAULT_STRATEGY
)
140 return (void *) state
;
148 * z_comp_init - initialize a previously-allocated compressor.
149 * @arg: pointer to the private state for the compressor
150 * @options: pointer to the CCP option data describing the
151 * compression that was negotiated with the peer
152 * @opt_len: length of the CCP option data at @options
153 * @unit: PPP unit number for diagnostic messages
154 * @hdrlen: ignored (present for backwards compatibility)
155 * @debug: debug flag; if non-zero, debug messages are printed.
157 * The CCP options described by @options must match the options
158 * specified when the compressor was allocated. The compressor
159 * history is reset. Returns 0 for failure (CCP options don't
160 * match) or 1 for success.
162 static int z_comp_init(void *arg
, unsigned char *options
, int opt_len
,
163 int unit
, int hdrlen
, int debug
)
165 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
167 if (opt_len
< CILEN_DEFLATE
168 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
169 || options
[1] != CILEN_DEFLATE
170 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
171 || DEFLATE_SIZE(options
[2]) != state
->w_size
172 || options
[3] != DEFLATE_CHK_SEQUENCE
)
177 state
->debug
= debug
;
179 zlib_deflateReset(&state
->strm
);
185 * z_comp_reset - reset a previously-allocated compressor.
186 * @arg: pointer to private state for the compressor.
188 * This clears the history for the compressor and makes it
189 * ready to start emitting a new compressed stream.
191 static void z_comp_reset(void *arg
)
193 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
196 zlib_deflateReset(&state
->strm
);
200 * z_compress - compress a PPP packet with Deflate compression.
201 * @arg: pointer to private state for the compressor
202 * @rptr: uncompressed packet (input)
203 * @obuf: compressed packet (output)
204 * @isize: size of uncompressed packet
205 * @osize: space available at @obuf
207 * Returns the length of the compressed packet, or 0 if the
208 * packet is incompressible.
210 int z_compress(void *arg
, unsigned char *rptr
, unsigned char *obuf
,
211 int isize
, int osize
)
213 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
214 int r
, proto
, off
, olen
, oavail
;
218 * Check that the protocol is in the range we handle.
220 proto
= PPP_PROTOCOL(rptr
);
221 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
224 /* Don't generate compressed packets which are larger than
225 the uncompressed packet. */
232 * Copy over the PPP header and store the 2-byte sequence number.
234 wptr
[0] = PPP_ADDRESS(rptr
);
235 wptr
[1] = PPP_CONTROL(rptr
);
236 wptr
[2] = PPP_COMP
>> 8;
239 wptr
[0] = state
->seqno
>> 8;
240 wptr
[1] = state
->seqno
;
241 wptr
+= DEFLATE_OVHD
;
242 olen
= PPP_HDRLEN
+ DEFLATE_OVHD
;
243 state
->strm
.next_out
= wptr
;
244 state
->strm
.avail_out
= oavail
= osize
- olen
;
247 off
= (proto
> 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
249 state
->strm
.next_in
= rptr
;
250 state
->strm
.avail_in
= (isize
- off
);
253 r
= zlib_deflate(&state
->strm
, Z_PACKET_FLUSH
);
257 "z_compress: deflate returned %d\n", r
);
260 if (state
->strm
.avail_out
== 0) {
262 state
->strm
.next_out
= NULL
;
263 state
->strm
.avail_out
= oavail
= 1000000;
265 break; /* all done */
268 olen
+= oavail
- state
->strm
.avail_out
;
271 * See if we managed to reduce the size of the packet.
274 state
->stats
.comp_bytes
+= olen
;
275 state
->stats
.comp_packets
++;
277 state
->stats
.inc_bytes
+= isize
;
278 state
->stats
.inc_packets
++;
281 state
->stats
.unc_bytes
+= isize
;
282 state
->stats
.unc_packets
++;
288 * z_comp_stats - return compression statistics for a compressor
290 * @arg: pointer to private space for the (de)compressor
291 * @stats: pointer to a struct compstat to receive the result.
293 static void z_comp_stats(void *arg
, struct compstat
*stats
)
295 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
297 *stats
= state
->stats
;
301 * z_decomp_free - Free the memory used by a decompressor.
302 * @arg: pointer to private space for the decompressor.
304 static void z_decomp_free(void *arg
)
306 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
309 zlib_inflateEnd(&state
->strm
);
310 kfree(state
->strm
.workspace
);
316 * z_decomp_alloc - allocate space for a decompressor.
317 * @options: pointer to CCP option data
318 * @opt_len: length of the CCP option at @options.
320 * The @options pointer points to the a buffer containing the
321 * CCP option data for the compression being negotiated. It is
322 * formatted according to RFC1979, and describes the window
323 * size that we are requesting the peer to use in compressing
324 * data to be sent to us.
326 * Returns the pointer to the private state for the decompressor,
327 * or NULL if we could not allocate enough memory.
329 static void *z_decomp_alloc(unsigned char *options
, int opt_len
)
331 struct ppp_deflate_state
*state
;
334 if (opt_len
!= CILEN_DEFLATE
335 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
336 || options
[1] != CILEN_DEFLATE
337 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
338 || options
[3] != DEFLATE_CHK_SEQUENCE
)
340 w_size
= DEFLATE_SIZE(options
[2]);
341 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
344 state
= (struct ppp_deflate_state
*) kmalloc(sizeof(*state
), GFP_KERNEL
);
348 memset (state
, 0, sizeof (struct ppp_deflate_state
));
349 state
->w_size
= w_size
;
350 state
->strm
.next_out
= NULL
;
351 state
->strm
.workspace
= kmalloc(zlib_inflate_workspacesize(),
352 GFP_KERNEL
|__GFP_REPEAT
);
353 if (state
->strm
.workspace
== NULL
)
356 if (zlib_inflateInit2(&state
->strm
, -w_size
) != Z_OK
)
358 return (void *) state
;
361 z_decomp_free(state
);
366 * z_decomp_init - initialize a previously-allocated decompressor.
367 * @arg: pointer to the private state for the decompressor
368 * @options: pointer to the CCP option data describing the
369 * compression that was negotiated with the peer
370 * @opt_len: length of the CCP option data at @options
371 * @unit: PPP unit number for diagnostic messages
372 * @hdrlen: ignored (present for backwards compatibility)
373 * @mru: maximum length of decompressed packets
374 * @debug: debug flag; if non-zero, debug messages are printed.
376 * The CCP options described by @options must match the options
377 * specified when the decompressor was allocated. The decompressor
378 * history is reset. Returns 0 for failure (CCP options don't
379 * match) or 1 for success.
381 static int z_decomp_init(void *arg
, unsigned char *options
, int opt_len
,
382 int unit
, int hdrlen
, int mru
, int debug
)
384 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
386 if (opt_len
< CILEN_DEFLATE
387 || (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
)
388 || options
[1] != CILEN_DEFLATE
389 || DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
390 || DEFLATE_SIZE(options
[2]) != state
->w_size
391 || options
[3] != DEFLATE_CHK_SEQUENCE
)
396 state
->debug
= debug
;
399 zlib_inflateReset(&state
->strm
);
405 * z_decomp_reset - reset a previously-allocated decompressor.
406 * @arg: pointer to private state for the decompressor.
408 * This clears the history for the decompressor and makes it
409 * ready to receive a new compressed stream.
411 static void z_decomp_reset(void *arg
)
413 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
416 zlib_inflateReset(&state
->strm
);
420 * z_decompress - decompress a Deflate-compressed packet.
421 * @arg: pointer to private state for the decompressor
422 * @ibuf: pointer to input (compressed) packet data
423 * @isize: length of input packet
424 * @obuf: pointer to space for output (decompressed) packet
425 * @osize: amount of space available at @obuf
427 * Because of patent problems, we return DECOMP_ERROR for errors
428 * found by inspecting the input data and for system problems, but
429 * DECOMP_FATALERROR for any errors which could possibly be said to
430 * be being detected "after" decompression. For DECOMP_ERROR,
431 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
432 * infringing a patent of Motorola's if we do, so we take CCP down
435 * Given that the frame has the correct sequence number and a good FCS,
436 * errors such as invalid codes in the input most likely indicate a
437 * bug, so we return DECOMP_FATALERROR for them in order to turn off
438 * compression, even though they are detected by inspecting the input.
440 int z_decompress(void *arg
, unsigned char *ibuf
, int isize
,
441 unsigned char *obuf
, int osize
)
443 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
445 int decode_proto
, overflow
;
446 unsigned char overflow_buf
[1];
448 if (isize
<= PPP_HDRLEN
+ DEFLATE_OVHD
) {
450 printk(KERN_DEBUG
"z_decompress%d: short pkt (%d)\n",
455 /* Check the sequence number. */
456 seq
= (ibuf
[PPP_HDRLEN
] << 8) + ibuf
[PPP_HDRLEN
+1];
457 if (seq
!= (state
->seqno
& 0xffff)) {
459 printk(KERN_DEBUG
"z_decompress%d: bad seq # %d, expected %d\n",
460 state
->unit
, seq
, state
->seqno
& 0xffff);
466 * Fill in the first part of the PPP header. The protocol field
467 * comes from the decompressed data.
469 obuf
[0] = PPP_ADDRESS(ibuf
);
470 obuf
[1] = PPP_CONTROL(ibuf
);
474 * Set up to call inflate. We set avail_out to 1 initially so we can
475 * look at the first byte of the output and decide whether we have
476 * a 1-byte or 2-byte protocol field.
478 state
->strm
.next_in
= ibuf
+ PPP_HDRLEN
+ DEFLATE_OVHD
;
479 state
->strm
.avail_in
= isize
- (PPP_HDRLEN
+ DEFLATE_OVHD
);
480 state
->strm
.next_out
= obuf
+ 3;
481 state
->strm
.avail_out
= 1;
486 * Call inflate, supplying more input or output as needed.
489 r
= zlib_inflate(&state
->strm
, Z_PACKET_FLUSH
);
492 printk(KERN_DEBUG
"z_decompress%d: inflate returned %d (%s)\n",
493 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
494 return DECOMP_FATALERROR
;
496 if (state
->strm
.avail_out
!= 0)
497 break; /* all done */
499 state
->strm
.avail_out
= osize
- PPP_HDRLEN
;
500 if ((obuf
[3] & 1) == 0) {
501 /* 2-byte protocol field */
503 --state
->strm
.next_out
;
504 ++state
->strm
.avail_out
;
507 } else if (!overflow
) {
509 * We've filled up the output buffer; the only way to
510 * find out whether inflate has any more characters
511 * left is to give it another byte of output space.
513 state
->strm
.next_out
= overflow_buf
;
514 state
->strm
.avail_out
= 1;
518 printk(KERN_DEBUG
"z_decompress%d: ran out of mru\n",
520 return DECOMP_FATALERROR
;
526 printk(KERN_DEBUG
"z_decompress%d: didn't get proto\n",
531 olen
= osize
+ overflow
- state
->strm
.avail_out
;
532 state
->stats
.unc_bytes
+= olen
;
533 state
->stats
.unc_packets
++;
534 state
->stats
.comp_bytes
+= isize
;
535 state
->stats
.comp_packets
++;
541 * z_incomp - add incompressible input data to the history.
542 * @arg: pointer to private state for the decompressor
543 * @ibuf: pointer to input packet data
544 * @icnt: length of input data.
546 static void z_incomp(void *arg
, unsigned char *ibuf
, int icnt
)
548 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
552 * Check that the protocol is one we handle.
554 proto
= PPP_PROTOCOL(ibuf
);
555 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
561 * We start at the either the 1st or 2nd byte of the protocol field,
562 * depending on whether the protocol value is compressible.
564 state
->strm
.next_in
= ibuf
+ 3;
565 state
->strm
.avail_in
= icnt
- 3;
567 --state
->strm
.next_in
;
568 ++state
->strm
.avail_in
;
571 r
= zlib_inflateIncomp(&state
->strm
);
575 printk(KERN_DEBUG
"z_incomp%d: inflateIncomp returned %d (%s)\n",
576 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
584 state
->stats
.inc_bytes
+= icnt
;
585 state
->stats
.inc_packets
++;
586 state
->stats
.unc_bytes
+= icnt
;
587 state
->stats
.unc_packets
++;
590 /*************************************************************
591 * Module interface table
592 *************************************************************/
594 /* These are in ppp_generic.c */
595 extern int ppp_register_compressor (struct compressor
*cp
);
596 extern void ppp_unregister_compressor (struct compressor
*cp
);
599 * Procedures exported to if_ppp.c.
601 static struct compressor ppp_deflate
= {
602 .compress_proto
= CI_DEFLATE
,
603 .comp_alloc
= z_comp_alloc
,
604 .comp_free
= z_comp_free
,
605 .comp_init
= z_comp_init
,
606 .comp_reset
= z_comp_reset
,
607 .compress
= z_compress
,
608 .comp_stat
= z_comp_stats
,
609 .decomp_alloc
= z_decomp_alloc
,
610 .decomp_free
= z_decomp_free
,
611 .decomp_init
= z_decomp_init
,
612 .decomp_reset
= z_decomp_reset
,
613 .decompress
= z_decompress
,
615 .decomp_stat
= z_comp_stats
,
619 static struct compressor ppp_deflate_draft
= {
620 .compress_proto
= CI_DEFLATE_DRAFT
,
621 .comp_alloc
= z_comp_alloc
,
622 .comp_free
= z_comp_free
,
623 .comp_init
= z_comp_init
,
624 .comp_reset
= z_comp_reset
,
625 .compress
= z_compress
,
626 .comp_stat
= z_comp_stats
,
627 .decomp_alloc
= z_decomp_alloc
,
628 .decomp_free
= z_decomp_free
,
629 .decomp_init
= z_decomp_init
,
630 .decomp_reset
= z_decomp_reset
,
631 .decompress
= z_decompress
,
633 .decomp_stat
= z_comp_stats
,
637 static int __init
deflate_init(void)
639 int answer
= ppp_register_compressor(&ppp_deflate
);
642 "PPP Deflate Compression module registered\n");
643 ppp_register_compressor(&ppp_deflate_draft
);
647 static void __exit
deflate_cleanup(void)
649 ppp_unregister_compressor(&ppp_deflate
);
650 ppp_unregister_compressor(&ppp_deflate_draft
);
653 module_init(deflate_init
);
654 module_exit(deflate_cleanup
);
655 MODULE_LICENSE("Dual BSD/GPL");
656 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE
));
657 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT
));