This source file includes following definitions.
- z_comp_free
- z_comp_alloc
- z_comp_init
- z_comp_reset
- z_compress
- z_comp_stats
- z_decomp_free
- z_decomp_alloc
- z_decomp_init
- z_decomp_reset
- z_decompress
- z_incomp
- deflate_init
- deflate_cleanup
1
2
3
4
5
6
7
8
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>
14
15 #include <linux/ppp_defs.h>
16 #include <linux/ppp-comp.h>
17
18 #include <linux/zlib.h>
19 #include <asm/unaligned.h>
20
21
22
23
24 struct ppp_deflate_state {
25 int seqno;
26 int w_size;
27 int unit;
28 int mru;
29 int debug;
30 z_stream strm;
31 struct compstat stats;
32 };
33
34 #define DEFLATE_OVHD 2
35
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,
41 int opt_len,
42 int unit, int hdrlen, int debug);
43 static int z_decomp_init(void *state, unsigned char *options,
44 int opt_len,
45 int unit, int hdrlen, int mru, int debug);
46 static int z_compress(void *state, unsigned char *rptr,
47 unsigned char *obuf,
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);
55
56
57
58
59
60 static void z_comp_free(void *arg)
61 {
62 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
63
64 if (state) {
65 zlib_deflateEnd(&state->strm);
66 vfree(state->strm.workspace);
67 kfree(state);
68 }
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 static void *z_comp_alloc(unsigned char *options, int opt_len)
86 {
87 struct ppp_deflate_state *state;
88 int w_size;
89
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)
95 return NULL;
96 w_size = DEFLATE_SIZE(options[2]);
97 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
98 return NULL;
99
100 state = kzalloc(sizeof(*state),
101 GFP_KERNEL);
102 if (state == NULL)
103 return NULL;
104
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)
109 goto out_free;
110
111 if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
112 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
113 != Z_OK)
114 goto out_free;
115 return (void *) state;
116
117 out_free:
118 z_comp_free(state);
119 return NULL;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 static int z_comp_init(void *arg, unsigned char *options, int opt_len,
138 int unit, int hdrlen, int debug)
139 {
140 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
141
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)
148 return 0;
149
150 state->seqno = 0;
151 state->unit = unit;
152 state->debug = debug;
153
154 zlib_deflateReset(&state->strm);
155
156 return 1;
157 }
158
159
160
161
162
163
164
165
166 static void z_comp_reset(void *arg)
167 {
168 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
169
170 state->seqno = 0;
171 zlib_deflateReset(&state->strm);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 static int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
186 int isize, int osize)
187 {
188 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
189 int r, proto, off, olen, oavail;
190 unsigned char *wptr;
191
192
193
194
195 proto = PPP_PROTOCOL(rptr);
196 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
197 return 0;
198
199
200
201 if (osize > isize)
202 osize = isize;
203
204 wptr = obuf;
205
206
207
208
209 wptr[0] = PPP_ADDRESS(rptr);
210 wptr[1] = PPP_CONTROL(rptr);
211 put_unaligned_be16(PPP_COMP, wptr + 2);
212 wptr += PPP_HDRLEN;
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;
218 ++state->seqno;
219
220 off = (proto > 0xff) ? 2 : 3;
221 rptr += off;
222 state->strm.next_in = rptr;
223 state->strm.avail_in = (isize - off);
224
225 for (;;) {
226 r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
227 if (r != Z_OK) {
228 if (state->debug)
229 printk(KERN_ERR
230 "z_compress: deflate returned %d\n", r);
231 break;
232 }
233 if (state->strm.avail_out == 0) {
234 olen += oavail;
235 state->strm.next_out = NULL;
236 state->strm.avail_out = oavail = 1000000;
237 } else {
238 break;
239 }
240 }
241 olen += oavail - state->strm.avail_out;
242
243
244
245
246 if (olen < isize && olen <= osize) {
247 state->stats.comp_bytes += olen;
248 state->stats.comp_packets++;
249 } else {
250 state->stats.inc_bytes += isize;
251 state->stats.inc_packets++;
252 olen = 0;
253 }
254 state->stats.unc_bytes += isize;
255 state->stats.unc_packets++;
256
257 return olen;
258 }
259
260
261
262
263
264
265
266 static void z_comp_stats(void *arg, struct compstat *stats)
267 {
268 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
269
270 *stats = state->stats;
271 }
272
273
274
275
276
277 static void z_decomp_free(void *arg)
278 {
279 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
280
281 if (state) {
282 zlib_inflateEnd(&state->strm);
283 vfree(state->strm.workspace);
284 kfree(state);
285 }
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 static void *z_decomp_alloc(unsigned char *options, int opt_len)
303 {
304 struct ppp_deflate_state *state;
305 int w_size;
306
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)
312 return NULL;
313 w_size = DEFLATE_SIZE(options[2]);
314 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
315 return NULL;
316
317 state = kzalloc(sizeof(*state), GFP_KERNEL);
318 if (state == NULL)
319 return NULL;
320
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)
325 goto out_free;
326
327 if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
328 goto out_free;
329 return (void *) state;
330
331 out_free:
332 z_decomp_free(state);
333 return NULL;
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
353 int unit, int hdrlen, int mru, int debug)
354 {
355 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
356
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)
363 return 0;
364
365 state->seqno = 0;
366 state->unit = unit;
367 state->debug = debug;
368 state->mru = mru;
369
370 zlib_inflateReset(&state->strm);
371
372 return 1;
373 }
374
375
376
377
378
379
380
381
382 static void z_decomp_reset(void *arg)
383 {
384 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
385
386 state->seqno = 0;
387 zlib_inflateReset(&state->strm);
388 }
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411 static int z_decompress(void *arg, unsigned char *ibuf, int isize,
412 unsigned char *obuf, int osize)
413 {
414 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
415 int olen, seq, r;
416 int decode_proto, overflow;
417 unsigned char overflow_buf[1];
418
419 if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
420 if (state->debug)
421 printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
422 state->unit, isize);
423 return DECOMP_ERROR;
424 }
425
426
427 seq = get_unaligned_be16(ibuf + PPP_HDRLEN);
428 if (seq != (state->seqno & 0xffff)) {
429 if (state->debug)
430 printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
431 state->unit, seq, state->seqno & 0xffff);
432 return DECOMP_ERROR;
433 }
434 ++state->seqno;
435
436
437
438
439
440 obuf[0] = PPP_ADDRESS(ibuf);
441 obuf[1] = PPP_CONTROL(ibuf);
442 obuf[2] = 0;
443
444
445
446
447
448
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;
453 decode_proto = 1;
454 overflow = 0;
455
456
457
458
459 for (;;) {
460 r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
461 if (r != Z_OK) {
462 if (state->debug)
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;
466 }
467 if (state->strm.avail_out != 0)
468 break;
469 if (decode_proto) {
470 state->strm.avail_out = osize - PPP_HDRLEN;
471 if ((obuf[3] & 1) == 0) {
472
473 obuf[2] = obuf[3];
474 --state->strm.next_out;
475 ++state->strm.avail_out;
476 }
477 decode_proto = 0;
478 } else if (!overflow) {
479
480
481
482
483
484 state->strm.next_out = overflow_buf;
485 state->strm.avail_out = 1;
486 overflow = 1;
487 } else {
488 if (state->debug)
489 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
490 state->unit);
491 return DECOMP_FATALERROR;
492 }
493 }
494
495 if (decode_proto) {
496 if (state->debug)
497 printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
498 state->unit);
499 return DECOMP_ERROR;
500 }
501
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++;
507
508 return olen;
509 }
510
511
512
513
514
515
516
517 static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
518 {
519 struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
520 int proto, r;
521
522
523
524
525 proto = PPP_PROTOCOL(ibuf);
526 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
527 return;
528
529 ++state->seqno;
530
531
532
533
534
535 state->strm.next_in = ibuf + 3;
536 state->strm.avail_in = icnt - 3;
537 if (proto > 0xff) {
538 --state->strm.next_in;
539 ++state->strm.avail_in;
540 }
541
542 r = zlib_inflateIncomp(&state->strm);
543 if (r != Z_OK) {
544
545 if (state->debug) {
546 printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
547 state->unit, r, (state->strm.msg? state->strm.msg: ""));
548 }
549 return;
550 }
551
552
553
554
555 state->stats.inc_bytes += icnt;
556 state->stats.inc_packets++;
557 state->stats.unc_bytes += icnt;
558 state->stats.unc_packets++;
559 }
560
561
562
563
564
565
566 extern int ppp_register_compressor (struct compressor *cp);
567 extern void ppp_unregister_compressor (struct compressor *cp);
568
569
570
571
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,
585 .incomp = z_incomp,
586 .decomp_stat = z_comp_stats,
587 .owner = THIS_MODULE
588 };
589
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,
603 .incomp = z_incomp,
604 .decomp_stat = z_comp_stats,
605 .owner = THIS_MODULE
606 };
607
608 static int __init deflate_init(void)
609 {
610 int rc;
611
612 rc = ppp_register_compressor(&ppp_deflate);
613 if (rc)
614 return rc;
615
616 rc = ppp_register_compressor(&ppp_deflate_draft);
617 if (rc) {
618 ppp_unregister_compressor(&ppp_deflate);
619 return rc;
620 }
621
622 pr_info("PPP Deflate Compression module registered\n");
623 return 0;
624 }
625
626 static void __exit deflate_cleanup(void)
627 {
628 ppp_unregister_compressor(&ppp_deflate);
629 ppp_unregister_compressor(&ppp_deflate_draft);
630 }
631
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));