]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/ppp_deflate.c
Linux-2.6.12-rc2
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ppp_deflate.c
CommitLineData
1da177e4
LT
1/*
2 * ==FILEVERSION 980319==
3 *
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.
7 *
8 * Copyright (c) 1994 The Australian National University.
9 * All rights reserved.
10 *
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
16 * any purpose.
17 *
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
22 * OF SUCH DAMAGE.
23 *
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,
29 * OR MODIFICATIONS.
30 *
31 * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
32 */
33
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>
39
40#include <linux/ppp_defs.h>
41#include <linux/ppp-comp.h>
42
43#include <linux/zlib.h>
44
45/*
46 * State for a Deflate (de)compressor.
47 */
48struct ppp_deflate_state {
49 int seqno;
50 int w_size;
51 int unit;
52 int mru;
53 int debug;
54 z_stream strm;
55 struct compstat stats;
56};
57
58#define DEFLATE_OVHD 2 /* Deflate overhead/packet */
59
60static void *z_comp_alloc(unsigned char *options, int opt_len);
61static void *z_decomp_alloc(unsigned char *options, int opt_len);
62static void z_comp_free(void *state);
63static void z_decomp_free(void *state);
64static int z_comp_init(void *state, unsigned char *options,
65 int opt_len,
66 int unit, int hdrlen, int debug);
67static int z_decomp_init(void *state, unsigned char *options,
68 int opt_len,
69 int unit, int hdrlen, int mru, int debug);
70static int z_compress(void *state, unsigned char *rptr,
71 unsigned char *obuf,
72 int isize, int osize);
73static void z_incomp(void *state, unsigned char *ibuf, int icnt);
74static int z_decompress(void *state, unsigned char *ibuf,
75 int isize, unsigned char *obuf, int osize);
76static void z_comp_reset(void *state);
77static void z_decomp_reset(void *state);
78static void z_comp_stats(void *state, struct compstat *stats);
79
80/**
81 * z_comp_free - free the memory used by a compressor
82 * @arg: pointer to the private state for the compressor.
83 */
84static void z_comp_free(void *arg)
85{
86 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
87
88 if (state) {
89 zlib_deflateEnd(&state->strm);
90 if (state->strm.workspace)
91 vfree(state->strm.workspace);
92 kfree(state);
93 }
94}
95
96/**
97 * z_comp_alloc - allocate space for a compressor.
98 * @options: pointer to CCP option data
99 * @opt_len: length of the CCP option at @options.
100 *
101 * The @options pointer points to the a buffer containing the
102 * CCP option data for the compression being negotiated. It is
103 * formatted according to RFC1979, and describes the window
104 * size that the peer is requesting that we use in compressing
105 * data to be sent to it.
106 *
107 * Returns the pointer to the private state for the compressor,
108 * or NULL if we could not allocate enough memory.
109 */
110static void *z_comp_alloc(unsigned char *options, int opt_len)
111{
112 struct ppp_deflate_state *state;
113 int w_size;
114
115 if (opt_len != CILEN_DEFLATE
116 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
117 || options[1] != CILEN_DEFLATE
118 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
119 || options[3] != DEFLATE_CHK_SEQUENCE)
120 return NULL;
121 w_size = DEFLATE_SIZE(options[2]);
122 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
123 return NULL;
124
125 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state),
126 GFP_KERNEL);
127 if (state == NULL)
128 return NULL;
129
130 memset (state, 0, sizeof (struct ppp_deflate_state));
131 state->strm.next_in = NULL;
132 state->w_size = w_size;
133 state->strm.workspace = vmalloc(zlib_deflate_workspacesize());
134 if (state->strm.workspace == NULL)
135 goto out_free;
136
137 if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
138 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
139 != Z_OK)
140 goto out_free;
141 return (void *) state;
142
143out_free:
144 z_comp_free(state);
145 return NULL;
146}
147
148/**
149 * z_comp_init - initialize a previously-allocated compressor.
150 * @arg: pointer to the private state for the compressor
151 * @options: pointer to the CCP option data describing the
152 * compression that was negotiated with the peer
153 * @opt_len: length of the CCP option data at @options
154 * @unit: PPP unit number for diagnostic messages
155 * @hdrlen: ignored (present for backwards compatibility)
156 * @debug: debug flag; if non-zero, debug messages are printed.
157 *
158 * The CCP options described by @options must match the options
159 * specified when the compressor was allocated. The compressor
160 * history is reset. Returns 0 for failure (CCP options don't
161 * match) or 1 for success.
162 */
163static int z_comp_init(void *arg, unsigned char *options, int opt_len,
164 int unit, int hdrlen, int debug)
165{
166 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
167
168 if (opt_len < CILEN_DEFLATE
169 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
170 || options[1] != CILEN_DEFLATE
171 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
172 || DEFLATE_SIZE(options[2]) != state->w_size
173 || options[3] != DEFLATE_CHK_SEQUENCE)
174 return 0;
175
176 state->seqno = 0;
177 state->unit = unit;
178 state->debug = debug;
179
180 zlib_deflateReset(&state->strm);
181
182 return 1;
183}
184
185/**
186 * z_comp_reset - reset a previously-allocated compressor.
187 * @arg: pointer to private state for the compressor.
188 *
189 * This clears the history for the compressor and makes it
190 * ready to start emitting a new compressed stream.
191 */
192static void z_comp_reset(void *arg)
193{
194 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
195
196 state->seqno = 0;
197 zlib_deflateReset(&state->strm);
198}
199
200/**
201 * z_compress - compress a PPP packet with Deflate compression.
202 * @arg: pointer to private state for the compressor
203 * @rptr: uncompressed packet (input)
204 * @obuf: compressed packet (output)
205 * @isize: size of uncompressed packet
206 * @osize: space available at @obuf
207 *
208 * Returns the length of the compressed packet, or 0 if the
209 * packet is incompressible.
210 */
211int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
212 int isize, int osize)
213{
214 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
215 int r, proto, off, olen, oavail;
216 unsigned char *wptr;
217
218 /*
219 * Check that the protocol is in the range we handle.
220 */
221 proto = PPP_PROTOCOL(rptr);
222 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
223 return 0;
224
225 /* Don't generate compressed packets which are larger than
226 the uncompressed packet. */
227 if (osize > isize)
228 osize = isize;
229
230 wptr = obuf;
231
232 /*
233 * Copy over the PPP header and store the 2-byte sequence number.
234 */
235 wptr[0] = PPP_ADDRESS(rptr);
236 wptr[1] = PPP_CONTROL(rptr);
237 wptr[2] = PPP_COMP >> 8;
238 wptr[3] = PPP_COMP;
239 wptr += PPP_HDRLEN;
240 wptr[0] = state->seqno >> 8;
241 wptr[1] = state->seqno;
242 wptr += DEFLATE_OVHD;
243 olen = PPP_HDRLEN + DEFLATE_OVHD;
244 state->strm.next_out = wptr;
245 state->strm.avail_out = oavail = osize - olen;
246 ++state->seqno;
247
248 off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
249 rptr += off;
250 state->strm.next_in = rptr;
251 state->strm.avail_in = (isize - off);
252
253 for (;;) {
254 r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
255 if (r != Z_OK) {
256 if (state->debug)
257 printk(KERN_ERR
258 "z_compress: deflate returned %d\n", r);
259 break;
260 }
261 if (state->strm.avail_out == 0) {
262 olen += oavail;
263 state->strm.next_out = NULL;
264 state->strm.avail_out = oavail = 1000000;
265 } else {
266 break; /* all done */
267 }
268 }
269 olen += oavail - state->strm.avail_out;
270
271 /*
272 * See if we managed to reduce the size of the packet.
273 */
274 if (olen < isize) {
275 state->stats.comp_bytes += olen;
276 state->stats.comp_packets++;
277 } else {
278 state->stats.inc_bytes += isize;
279 state->stats.inc_packets++;
280 olen = 0;
281 }
282 state->stats.unc_bytes += isize;
283 state->stats.unc_packets++;
284
285 return olen;
286}
287
288/**
289 * z_comp_stats - return compression statistics for a compressor
290 * or decompressor.
291 * @arg: pointer to private space for the (de)compressor
292 * @stats: pointer to a struct compstat to receive the result.
293 */
294static void z_comp_stats(void *arg, struct compstat *stats)
295{
296 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
297
298 *stats = state->stats;
299}
300
301/**
302 * z_decomp_free - Free the memory used by a decompressor.
303 * @arg: pointer to private space for the decompressor.
304 */
305static void z_decomp_free(void *arg)
306{
307 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
308
309 if (state) {
310 zlib_inflateEnd(&state->strm);
311 if (state->strm.workspace)
312 kfree(state->strm.workspace);
313 kfree(state);
314 }
315}
316
317/**
318 * z_decomp_alloc - allocate space for a decompressor.
319 * @options: pointer to CCP option data
320 * @opt_len: length of the CCP option at @options.
321 *
322 * The @options pointer points to the a buffer containing the
323 * CCP option data for the compression being negotiated. It is
324 * formatted according to RFC1979, and describes the window
325 * size that we are requesting the peer to use in compressing
326 * data to be sent to us.
327 *
328 * Returns the pointer to the private state for the decompressor,
329 * or NULL if we could not allocate enough memory.
330 */
331static void *z_decomp_alloc(unsigned char *options, int opt_len)
332{
333 struct ppp_deflate_state *state;
334 int w_size;
335
336 if (opt_len != CILEN_DEFLATE
337 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
338 || options[1] != CILEN_DEFLATE
339 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
340 || options[3] != DEFLATE_CHK_SEQUENCE)
341 return NULL;
342 w_size = DEFLATE_SIZE(options[2]);
343 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
344 return NULL;
345
346 state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
347 if (state == NULL)
348 return NULL;
349
350 memset (state, 0, sizeof (struct ppp_deflate_state));
351 state->w_size = w_size;
352 state->strm.next_out = NULL;
353 state->strm.workspace = kmalloc(zlib_inflate_workspacesize(),
354 GFP_KERNEL|__GFP_REPEAT);
355 if (state->strm.workspace == NULL)
356 goto out_free;
357
358 if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
359 goto out_free;
360 return (void *) state;
361
362out_free:
363 z_decomp_free(state);
364 return NULL;
365}
366
367/**
368 * z_decomp_init - initialize a previously-allocated decompressor.
369 * @arg: pointer to the private state for the decompressor
370 * @options: pointer to the CCP option data describing the
371 * compression that was negotiated with the peer
372 * @opt_len: length of the CCP option data at @options
373 * @unit: PPP unit number for diagnostic messages
374 * @hdrlen: ignored (present for backwards compatibility)
375 * @mru: maximum length of decompressed packets
376 * @debug: debug flag; if non-zero, debug messages are printed.
377 *
378 * The CCP options described by @options must match the options
379 * specified when the decompressor was allocated. The decompressor
380 * history is reset. Returns 0 for failure (CCP options don't
381 * match) or 1 for success.
382 */
383static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
384 int unit, int hdrlen, int mru, int debug)
385{
386 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
387
388 if (opt_len < CILEN_DEFLATE
389 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
390 || options[1] != CILEN_DEFLATE
391 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
392 || DEFLATE_SIZE(options[2]) != state->w_size
393 || options[3] != DEFLATE_CHK_SEQUENCE)
394 return 0;
395
396 state->seqno = 0;
397 state->unit = unit;
398 state->debug = debug;
399 state->mru = mru;
400
401 zlib_inflateReset(&state->strm);
402
403 return 1;
404}
405
406/**
407 * z_decomp_reset - reset a previously-allocated decompressor.
408 * @arg: pointer to private state for the decompressor.
409 *
410 * This clears the history for the decompressor and makes it
411 * ready to receive a new compressed stream.
412 */
413static void z_decomp_reset(void *arg)
414{
415 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
416
417 state->seqno = 0;
418 zlib_inflateReset(&state->strm);
419}
420
421/**
422 * z_decompress - decompress a Deflate-compressed packet.
423 * @arg: pointer to private state for the decompressor
424 * @ibuf: pointer to input (compressed) packet data
425 * @isize: length of input packet
426 * @obuf: pointer to space for output (decompressed) packet
427 * @osize: amount of space available at @obuf
428 *
429 * Because of patent problems, we return DECOMP_ERROR for errors
430 * found by inspecting the input data and for system problems, but
431 * DECOMP_FATALERROR for any errors which could possibly be said to
432 * be being detected "after" decompression. For DECOMP_ERROR,
433 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
434 * infringing a patent of Motorola's if we do, so we take CCP down
435 * instead.
436 *
437 * Given that the frame has the correct sequence number and a good FCS,
438 * errors such as invalid codes in the input most likely indicate a
439 * bug, so we return DECOMP_FATALERROR for them in order to turn off
440 * compression, even though they are detected by inspecting the input.
441 */
442int z_decompress(void *arg, unsigned char *ibuf, int isize,
443 unsigned char *obuf, int osize)
444{
445 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
446 int olen, seq, r;
447 int decode_proto, overflow;
448 unsigned char overflow_buf[1];
449
450 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
451 if (state->debug)
452 printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
453 state->unit, isize);
454 return DECOMP_ERROR;
455 }
456
457 /* Check the sequence number. */
458 seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
459 if (seq != (state->seqno & 0xffff)) {
460 if (state->debug)
461 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
462 state->unit, seq, state->seqno & 0xffff);
463 return DECOMP_ERROR;
464 }
465 ++state->seqno;
466
467 /*
468 * Fill in the first part of the PPP header. The protocol field
469 * comes from the decompressed data.
470 */
471 obuf[0] = PPP_ADDRESS(ibuf);
472 obuf[1] = PPP_CONTROL(ibuf);
473 obuf[2] = 0;
474
475 /*
476 * Set up to call inflate. We set avail_out to 1 initially so we can
477 * look at the first byte of the output and decide whether we have
478 * a 1-byte or 2-byte protocol field.
479 */
480 state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
481 state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
482 state->strm.next_out = obuf + 3;
483 state->strm.avail_out = 1;
484 decode_proto = 1;
485 overflow = 0;
486
487 /*
488 * Call inflate, supplying more input or output as needed.
489 */
490 for (;;) {
491 r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
492 if (r != Z_OK) {
493 if (state->debug)
494 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
495 state->unit, r, (state->strm.msg? state->strm.msg: ""));
496 return DECOMP_FATALERROR;
497 }
498 if (state->strm.avail_out != 0)
499 break; /* all done */
500 if (decode_proto) {
501 state->strm.avail_out = osize - PPP_HDRLEN;
502 if ((obuf[3] & 1) == 0) {
503 /* 2-byte protocol field */
504 obuf[2] = obuf[3];
505 --state->strm.next_out;
506 ++state->strm.avail_out;
507 }
508 decode_proto = 0;
509 } else if (!overflow) {
510 /*
511 * We've filled up the output buffer; the only way to
512 * find out whether inflate has any more characters
513 * left is to give it another byte of output space.
514 */
515 state->strm.next_out = overflow_buf;
516 state->strm.avail_out = 1;
517 overflow = 1;
518 } else {
519 if (state->debug)
520 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
521 state->unit);
522 return DECOMP_FATALERROR;
523 }
524 }
525
526 if (decode_proto) {
527 if (state->debug)
528 printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
529 state->unit);
530 return DECOMP_ERROR;
531 }
532
533 olen = osize + overflow - state->strm.avail_out;
534 state->stats.unc_bytes += olen;
535 state->stats.unc_packets++;
536 state->stats.comp_bytes += isize;
537 state->stats.comp_packets++;
538
539 return olen;
540}
541
542/**
543 * z_incomp - add incompressible input data to the history.
544 * @arg: pointer to private state for the decompressor
545 * @ibuf: pointer to input packet data
546 * @icnt: length of input data.
547 */
548static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
549{
550 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
551 int proto, r;
552
553 /*
554 * Check that the protocol is one we handle.
555 */
556 proto = PPP_PROTOCOL(ibuf);
557 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
558 return;
559
560 ++state->seqno;
561
562 /*
563 * We start at the either the 1st or 2nd byte of the protocol field,
564 * depending on whether the protocol value is compressible.
565 */
566 state->strm.next_in = ibuf + 3;
567 state->strm.avail_in = icnt - 3;
568 if (proto > 0xff) {
569 --state->strm.next_in;
570 ++state->strm.avail_in;
571 }
572
573 r = zlib_inflateIncomp(&state->strm);
574 if (r != Z_OK) {
575 /* gak! */
576 if (state->debug) {
577 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
578 state->unit, r, (state->strm.msg? state->strm.msg: ""));
579 }
580 return;
581 }
582
583 /*
584 * Update stats.
585 */
586 state->stats.inc_bytes += icnt;
587 state->stats.inc_packets++;
588 state->stats.unc_bytes += icnt;
589 state->stats.unc_packets++;
590}
591
592/*************************************************************
593 * Module interface table
594 *************************************************************/
595
596/* These are in ppp_generic.c */
597extern int ppp_register_compressor (struct compressor *cp);
598extern void ppp_unregister_compressor (struct compressor *cp);
599
600/*
601 * Procedures exported to if_ppp.c.
602 */
603static struct compressor ppp_deflate = {
604 .compress_proto = CI_DEFLATE,
605 .comp_alloc = z_comp_alloc,
606 .comp_free = z_comp_free,
607 .comp_init = z_comp_init,
608 .comp_reset = z_comp_reset,
609 .compress = z_compress,
610 .comp_stat = z_comp_stats,
611 .decomp_alloc = z_decomp_alloc,
612 .decomp_free = z_decomp_free,
613 .decomp_init = z_decomp_init,
614 .decomp_reset = z_decomp_reset,
615 .decompress = z_decompress,
616 .incomp = z_incomp,
617 .decomp_stat = z_comp_stats,
618 .owner = THIS_MODULE
619};
620
621static struct compressor ppp_deflate_draft = {
622 .compress_proto = CI_DEFLATE_DRAFT,
623 .comp_alloc = z_comp_alloc,
624 .comp_free = z_comp_free,
625 .comp_init = z_comp_init,
626 .comp_reset = z_comp_reset,
627 .compress = z_compress,
628 .comp_stat = z_comp_stats,
629 .decomp_alloc = z_decomp_alloc,
630 .decomp_free = z_decomp_free,
631 .decomp_init = z_decomp_init,
632 .decomp_reset = z_decomp_reset,
633 .decompress = z_decompress,
634 .incomp = z_incomp,
635 .decomp_stat = z_comp_stats,
636 .owner = THIS_MODULE
637};
638
639static int __init deflate_init(void)
640{
641 int answer = ppp_register_compressor(&ppp_deflate);
642 if (answer == 0)
643 printk(KERN_INFO
644 "PPP Deflate Compression module registered\n");
645 ppp_register_compressor(&ppp_deflate_draft);
646 return answer;
647}
648
649static void __exit deflate_cleanup(void)
650{
651 ppp_unregister_compressor(&ppp_deflate);
652 ppp_unregister_compressor(&ppp_deflate_draft);
653}
654
655module_init(deflate_init);
656module_exit(deflate_cleanup);
657MODULE_LICENSE("Dual BSD/GPL");
658MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
659MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));