1 // SPDX-License-Identifier: GPL-2.0-only
3 * ppp_deflate.c - interface the zlib procedures for Deflate compression
4 * and decompression (as used by gzip) to the PPP code.
6 * Copyright 1994-1998 Paul Mackerras.
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
13 #include <linux/string.h>
15 #include <linux/ppp_defs.h>
16 #include <linux/ppp-comp.h>
18 #include <linux/zlib.h>
19 #include <asm/unaligned.h>
22 * State for a Deflate (de)compressor.
24 struct ppp_deflate_state
{
31 struct compstat stats
;
34 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
36 static void *z_comp_alloc(unsigned char *options
, int opt_len
);
37 static void *z_decomp_alloc(unsigned char *options
, int opt_len
);
38 static void z_comp_free(void *state
);
39 static void z_decomp_free(void *state
);
40 static int z_comp_init(void *state
, unsigned char *options
,
42 int unit
, int hdrlen
, int debug
);
43 static int z_decomp_init(void *state
, unsigned char *options
,
45 int unit
, int hdrlen
, int mru
, int debug
);
46 static int z_compress(void *state
, unsigned char *rptr
,
48 int isize
, int osize
);
49 static void z_incomp(void *state
, unsigned char *ibuf
, int icnt
);
50 static int z_decompress(void *state
, unsigned char *ibuf
,
51 int isize
, unsigned char *obuf
, int osize
);
52 static void z_comp_reset(void *state
);
53 static void z_decomp_reset(void *state
);
54 static void z_comp_stats(void *state
, struct compstat
*stats
);
57 * z_comp_free - free the memory used by a compressor
58 * @arg: pointer to the private state for the compressor.
60 static void z_comp_free(void *arg
)
62 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
65 zlib_deflateEnd(&state
->strm
);
66 vfree(state
->strm
.workspace
);
72 * z_comp_alloc - allocate space for a compressor.
73 * @options: pointer to CCP option data
74 * @opt_len: length of the CCP option at @options.
76 * The @options pointer points to the a buffer containing the
77 * CCP option data for the compression being negotiated. It is
78 * formatted according to RFC1979, and describes the window
79 * size that the peer is requesting that we use in compressing
80 * data to be sent to it.
82 * Returns the pointer to the private state for the compressor,
83 * or NULL if we could not allocate enough memory.
85 static void *z_comp_alloc(unsigned char *options
, int opt_len
)
87 struct ppp_deflate_state
*state
;
90 if (opt_len
!= CILEN_DEFLATE
||
91 (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
) ||
92 options
[1] != CILEN_DEFLATE
||
93 DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
||
94 options
[3] != DEFLATE_CHK_SEQUENCE
)
96 w_size
= DEFLATE_SIZE(options
[2]);
97 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
100 state
= kzalloc(sizeof(*state
),
105 state
->strm
.next_in
= NULL
;
106 state
->w_size
= w_size
;
107 state
->strm
.workspace
= vmalloc(zlib_deflate_workspacesize(-w_size
, 8));
108 if (state
->strm
.workspace
== NULL
)
111 if (zlib_deflateInit2(&state
->strm
, Z_DEFAULT_COMPRESSION
,
112 DEFLATE_METHOD_VAL
, -w_size
, 8, Z_DEFAULT_STRATEGY
)
115 return (void *) state
;
123 * z_comp_init - initialize a previously-allocated compressor.
124 * @arg: pointer to the private state for the compressor
125 * @options: pointer to the CCP option data describing the
126 * compression that was negotiated with the peer
127 * @opt_len: length of the CCP option data at @options
128 * @unit: PPP unit number for diagnostic messages
129 * @hdrlen: ignored (present for backwards compatibility)
130 * @debug: debug flag; if non-zero, debug messages are printed.
132 * The CCP options described by @options must match the options
133 * specified when the compressor was allocated. The compressor
134 * history is reset. Returns 0 for failure (CCP options don't
135 * match) or 1 for success.
137 static int z_comp_init(void *arg
, unsigned char *options
, int opt_len
,
138 int unit
, int hdrlen
, int debug
)
140 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
142 if (opt_len
< CILEN_DEFLATE
||
143 (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
) ||
144 options
[1] != CILEN_DEFLATE
||
145 DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
||
146 DEFLATE_SIZE(options
[2]) != state
->w_size
||
147 options
[3] != DEFLATE_CHK_SEQUENCE
)
152 state
->debug
= debug
;
154 zlib_deflateReset(&state
->strm
);
160 * z_comp_reset - reset a previously-allocated compressor.
161 * @arg: pointer to private state for the compressor.
163 * This clears the history for the compressor and makes it
164 * ready to start emitting a new compressed stream.
166 static void z_comp_reset(void *arg
)
168 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
171 zlib_deflateReset(&state
->strm
);
175 * z_compress - compress a PPP packet with Deflate compression.
176 * @arg: pointer to private state for the compressor
177 * @rptr: uncompressed packet (input)
178 * @obuf: compressed packet (output)
179 * @isize: size of uncompressed packet
180 * @osize: space available at @obuf
182 * Returns the length of the compressed packet, or 0 if the
183 * packet is incompressible.
185 static int z_compress(void *arg
, unsigned char *rptr
, unsigned char *obuf
,
186 int isize
, int osize
)
188 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
189 int r
, proto
, off
, olen
, oavail
;
193 * Check that the protocol is in the range we handle.
195 proto
= PPP_PROTOCOL(rptr
);
196 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
199 /* Don't generate compressed packets which are larger than
200 the uncompressed packet. */
207 * Copy over the PPP header and store the 2-byte sequence number.
209 wptr
[0] = PPP_ADDRESS(rptr
);
210 wptr
[1] = PPP_CONTROL(rptr
);
211 put_unaligned_be16(PPP_COMP
, wptr
+ 2);
213 put_unaligned_be16(state
->seqno
, wptr
);
214 wptr
+= DEFLATE_OVHD
;
215 olen
= PPP_HDRLEN
+ DEFLATE_OVHD
;
216 state
->strm
.next_out
= wptr
;
217 state
->strm
.avail_out
= oavail
= osize
- olen
;
220 off
= (proto
> 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
222 state
->strm
.next_in
= rptr
;
223 state
->strm
.avail_in
= (isize
- off
);
226 r
= zlib_deflate(&state
->strm
, Z_PACKET_FLUSH
);
230 "z_compress: deflate returned %d\n", r
);
233 if (state
->strm
.avail_out
== 0) {
235 state
->strm
.next_out
= NULL
;
236 state
->strm
.avail_out
= oavail
= 1000000;
238 break; /* all done */
241 olen
+= oavail
- state
->strm
.avail_out
;
244 * See if we managed to reduce the size of the packet.
246 if (olen
< isize
&& olen
<= osize
) {
247 state
->stats
.comp_bytes
+= olen
;
248 state
->stats
.comp_packets
++;
250 state
->stats
.inc_bytes
+= isize
;
251 state
->stats
.inc_packets
++;
254 state
->stats
.unc_bytes
+= isize
;
255 state
->stats
.unc_packets
++;
261 * z_comp_stats - return compression statistics for a compressor
263 * @arg: pointer to private space for the (de)compressor
264 * @stats: pointer to a struct compstat to receive the result.
266 static void z_comp_stats(void *arg
, struct compstat
*stats
)
268 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
270 *stats
= state
->stats
;
274 * z_decomp_free - Free the memory used by a decompressor.
275 * @arg: pointer to private space for the decompressor.
277 static void z_decomp_free(void *arg
)
279 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
282 zlib_inflateEnd(&state
->strm
);
283 vfree(state
->strm
.workspace
);
289 * z_decomp_alloc - allocate space for a decompressor.
290 * @options: pointer to CCP option data
291 * @opt_len: length of the CCP option at @options.
293 * The @options pointer points to the a buffer containing the
294 * CCP option data for the compression being negotiated. It is
295 * formatted according to RFC1979, and describes the window
296 * size that we are requesting the peer to use in compressing
297 * data to be sent to us.
299 * Returns the pointer to the private state for the decompressor,
300 * or NULL if we could not allocate enough memory.
302 static void *z_decomp_alloc(unsigned char *options
, int opt_len
)
304 struct ppp_deflate_state
*state
;
307 if (opt_len
!= CILEN_DEFLATE
||
308 (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
) ||
309 options
[1] != CILEN_DEFLATE
||
310 DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
||
311 options
[3] != DEFLATE_CHK_SEQUENCE
)
313 w_size
= DEFLATE_SIZE(options
[2]);
314 if (w_size
< DEFLATE_MIN_SIZE
|| w_size
> DEFLATE_MAX_SIZE
)
317 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
321 state
->w_size
= w_size
;
322 state
->strm
.next_out
= NULL
;
323 state
->strm
.workspace
= vmalloc(zlib_inflate_workspacesize());
324 if (state
->strm
.workspace
== NULL
)
327 if (zlib_inflateInit2(&state
->strm
, -w_size
) != Z_OK
)
329 return (void *) state
;
332 z_decomp_free(state
);
337 * z_decomp_init - initialize a previously-allocated decompressor.
338 * @arg: pointer to the private state for the decompressor
339 * @options: pointer to the CCP option data describing the
340 * compression that was negotiated with the peer
341 * @opt_len: length of the CCP option data at @options
342 * @unit: PPP unit number for diagnostic messages
343 * @hdrlen: ignored (present for backwards compatibility)
344 * @mru: maximum length of decompressed packets
345 * @debug: debug flag; if non-zero, debug messages are printed.
347 * The CCP options described by @options must match the options
348 * specified when the decompressor was allocated. The decompressor
349 * history is reset. Returns 0 for failure (CCP options don't
350 * match) or 1 for success.
352 static int z_decomp_init(void *arg
, unsigned char *options
, int opt_len
,
353 int unit
, int hdrlen
, int mru
, int debug
)
355 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
357 if (opt_len
< CILEN_DEFLATE
||
358 (options
[0] != CI_DEFLATE
&& options
[0] != CI_DEFLATE_DRAFT
) ||
359 options
[1] != CILEN_DEFLATE
||
360 DEFLATE_METHOD(options
[2]) != DEFLATE_METHOD_VAL
||
361 DEFLATE_SIZE(options
[2]) != state
->w_size
||
362 options
[3] != DEFLATE_CHK_SEQUENCE
)
367 state
->debug
= debug
;
370 zlib_inflateReset(&state
->strm
);
376 * z_decomp_reset - reset a previously-allocated decompressor.
377 * @arg: pointer to private state for the decompressor.
379 * This clears the history for the decompressor and makes it
380 * ready to receive a new compressed stream.
382 static void z_decomp_reset(void *arg
)
384 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
387 zlib_inflateReset(&state
->strm
);
391 * z_decompress - decompress a Deflate-compressed packet.
392 * @arg: pointer to private state for the decompressor
393 * @ibuf: pointer to input (compressed) packet data
394 * @isize: length of input packet
395 * @obuf: pointer to space for output (decompressed) packet
396 * @osize: amount of space available at @obuf
398 * Because of patent problems, we return DECOMP_ERROR for errors
399 * found by inspecting the input data and for system problems, but
400 * DECOMP_FATALERROR for any errors which could possibly be said to
401 * be being detected "after" decompression. For DECOMP_ERROR,
402 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
403 * infringing a patent of Motorola's if we do, so we take CCP down
406 * Given that the frame has the correct sequence number and a good FCS,
407 * errors such as invalid codes in the input most likely indicate a
408 * bug, so we return DECOMP_FATALERROR for them in order to turn off
409 * compression, even though they are detected by inspecting the input.
411 static int z_decompress(void *arg
, unsigned char *ibuf
, int isize
,
412 unsigned char *obuf
, int osize
)
414 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
416 int decode_proto
, overflow
;
417 unsigned char overflow_buf
[1];
419 if (isize
<= PPP_HDRLEN
+ DEFLATE_OVHD
) {
421 printk(KERN_DEBUG
"z_decompress%d: short pkt (%d)\n",
426 /* Check the sequence number. */
427 seq
= get_unaligned_be16(ibuf
+ PPP_HDRLEN
);
428 if (seq
!= (state
->seqno
& 0xffff)) {
430 printk(KERN_DEBUG
"z_decompress%d: bad seq # %d, expected %d\n",
431 state
->unit
, seq
, state
->seqno
& 0xffff);
437 * Fill in the first part of the PPP header. The protocol field
438 * comes from the decompressed data.
440 obuf
[0] = PPP_ADDRESS(ibuf
);
441 obuf
[1] = PPP_CONTROL(ibuf
);
445 * Set up to call inflate. We set avail_out to 1 initially so we can
446 * look at the first byte of the output and decide whether we have
447 * a 1-byte or 2-byte protocol field.
449 state
->strm
.next_in
= ibuf
+ PPP_HDRLEN
+ DEFLATE_OVHD
;
450 state
->strm
.avail_in
= isize
- (PPP_HDRLEN
+ DEFLATE_OVHD
);
451 state
->strm
.next_out
= obuf
+ 3;
452 state
->strm
.avail_out
= 1;
457 * Call inflate, supplying more input or output as needed.
460 r
= zlib_inflate(&state
->strm
, Z_PACKET_FLUSH
);
463 printk(KERN_DEBUG
"z_decompress%d: inflate returned %d (%s)\n",
464 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
465 return DECOMP_FATALERROR
;
467 if (state
->strm
.avail_out
!= 0)
468 break; /* all done */
470 state
->strm
.avail_out
= osize
- PPP_HDRLEN
;
471 if ((obuf
[3] & 1) == 0) {
472 /* 2-byte protocol field */
474 --state
->strm
.next_out
;
475 ++state
->strm
.avail_out
;
478 } else if (!overflow
) {
480 * We've filled up the output buffer; the only way to
481 * find out whether inflate has any more characters
482 * left is to give it another byte of output space.
484 state
->strm
.next_out
= overflow_buf
;
485 state
->strm
.avail_out
= 1;
489 printk(KERN_DEBUG
"z_decompress%d: ran out of mru\n",
491 return DECOMP_FATALERROR
;
497 printk(KERN_DEBUG
"z_decompress%d: didn't get proto\n",
502 olen
= osize
+ overflow
- state
->strm
.avail_out
;
503 state
->stats
.unc_bytes
+= olen
;
504 state
->stats
.unc_packets
++;
505 state
->stats
.comp_bytes
+= isize
;
506 state
->stats
.comp_packets
++;
512 * z_incomp - add incompressible input data to the history.
513 * @arg: pointer to private state for the decompressor
514 * @ibuf: pointer to input packet data
515 * @icnt: length of input data.
517 static void z_incomp(void *arg
, unsigned char *ibuf
, int icnt
)
519 struct ppp_deflate_state
*state
= (struct ppp_deflate_state
*) arg
;
523 * Check that the protocol is one we handle.
525 proto
= PPP_PROTOCOL(ibuf
);
526 if (proto
> 0x3fff || proto
== 0xfd || proto
== 0xfb)
532 * We start at the either the 1st or 2nd byte of the protocol field,
533 * depending on whether the protocol value is compressible.
535 state
->strm
.next_in
= ibuf
+ 3;
536 state
->strm
.avail_in
= icnt
- 3;
538 --state
->strm
.next_in
;
539 ++state
->strm
.avail_in
;
542 r
= zlib_inflateIncomp(&state
->strm
);
546 printk(KERN_DEBUG
"z_incomp%d: inflateIncomp returned %d (%s)\n",
547 state
->unit
, r
, (state
->strm
.msg
? state
->strm
.msg
: ""));
555 state
->stats
.inc_bytes
+= icnt
;
556 state
->stats
.inc_packets
++;
557 state
->stats
.unc_bytes
+= icnt
;
558 state
->stats
.unc_packets
++;
561 /*************************************************************
562 * Module interface table
563 *************************************************************/
565 /* These are in ppp_generic.c */
566 extern int ppp_register_compressor (struct compressor
*cp
);
567 extern void ppp_unregister_compressor (struct compressor
*cp
);
570 * Procedures exported to if_ppp.c.
572 static struct compressor ppp_deflate
= {
573 .compress_proto
= CI_DEFLATE
,
574 .comp_alloc
= z_comp_alloc
,
575 .comp_free
= z_comp_free
,
576 .comp_init
= z_comp_init
,
577 .comp_reset
= z_comp_reset
,
578 .compress
= z_compress
,
579 .comp_stat
= z_comp_stats
,
580 .decomp_alloc
= z_decomp_alloc
,
581 .decomp_free
= z_decomp_free
,
582 .decomp_init
= z_decomp_init
,
583 .decomp_reset
= z_decomp_reset
,
584 .decompress
= z_decompress
,
586 .decomp_stat
= z_comp_stats
,
590 static struct compressor ppp_deflate_draft
= {
591 .compress_proto
= CI_DEFLATE_DRAFT
,
592 .comp_alloc
= z_comp_alloc
,
593 .comp_free
= z_comp_free
,
594 .comp_init
= z_comp_init
,
595 .comp_reset
= z_comp_reset
,
596 .compress
= z_compress
,
597 .comp_stat
= z_comp_stats
,
598 .decomp_alloc
= z_decomp_alloc
,
599 .decomp_free
= z_decomp_free
,
600 .decomp_init
= z_decomp_init
,
601 .decomp_reset
= z_decomp_reset
,
602 .decompress
= z_decompress
,
604 .decomp_stat
= z_comp_stats
,
608 static int __init
deflate_init(void)
612 rc
= ppp_register_compressor(&ppp_deflate
);
616 rc
= ppp_register_compressor(&ppp_deflate_draft
);
618 ppp_unregister_compressor(&ppp_deflate
);
622 pr_info("PPP Deflate Compression module registered\n");
626 static void __exit
deflate_cleanup(void)
628 ppp_unregister_compressor(&ppp_deflate
);
629 ppp_unregister_compressor(&ppp_deflate_draft
);
632 module_init(deflate_init
);
633 module_exit(deflate_cleanup
);
634 MODULE_LICENSE("Dual BSD/GPL");
635 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE
));
636 MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT
));