ppp-deflate.c revision 1.4 1 /* $NetBSD: ppp-deflate.c,v 1.4 1997/03/12 20:26:56 christos Exp $ */
2 /* Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 paulus Exp */
3
4 /*
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.
8 *
9 * Copyright (c) 1994 The Australian National University.
10 * All rights reserved.
11 *
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation is hereby granted, provided that the above copyright
14 * notice appears in all copies. This software is provided without any
15 * warranty, express or implied. The Australian National University
16 * makes no representations about the suitability of this software for
17 * any purpose.
18 *
19 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
20 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
22 * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
23 * OF SUCH DAMAGE.
24 *
25 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
28 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
29 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
30 * OR MODIFICATIONS.
31 */
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <net/ppp_defs.h>
38 #include <net/zlib.h>
39
40 #define PACKETPTR struct mbuf *
41 #include <net/ppp-comp.h>
42
43 #if DO_DEFLATE
44
45 #define DEFLATE_DEBUG 1
46
47 /*
48 * State for a Deflate (de)compressor.
49 */
50 struct deflate_state {
51 int seqno;
52 int w_size;
53 int unit;
54 int hdrlen;
55 int mru;
56 int debug;
57 z_stream strm;
58 struct compstat stats;
59 };
60
61 #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
62
63 static void *zalloc __P((void *, u_int items, u_int size));
64 static void zfree __P((void *, void *ptr, u_int nb));
65 static void *z_comp_alloc __P((u_char *options, int opt_len));
66 static void *z_decomp_alloc __P((u_char *options, int opt_len));
67 static void z_comp_free __P((void *state));
68 static void z_decomp_free __P((void *state));
69 static int z_comp_init __P((void *state, u_char *options, int opt_len,
70 int unit, int hdrlen, int debug));
71 static int z_decomp_init __P((void *state, u_char *options, int opt_len,
72 int unit, int hdrlen, int mru, int debug));
73 static int z_compress __P((void *state, struct mbuf **mret,
74 struct mbuf *mp, int slen, int maxolen));
75 static void z_incomp __P((void *state, struct mbuf *dmsg));
76 static int z_decompress __P((void *state, struct mbuf *cmp,
77 struct mbuf **dmpp));
78 static void z_comp_reset __P((void *state));
79 static void z_decomp_reset __P((void *state));
80 static void z_comp_stats __P((void *state, struct compstat *stats));
81
82 /*
83 * Procedures exported to if_ppp.c.
84 */
85 struct compressor ppp_deflate = {
86 CI_DEFLATE, /* compress_proto */
87 z_comp_alloc, /* comp_alloc */
88 z_comp_free, /* comp_free */
89 z_comp_init, /* comp_init */
90 z_comp_reset, /* comp_reset */
91 z_compress, /* compress */
92 z_comp_stats, /* comp_stat */
93 z_decomp_alloc, /* decomp_alloc */
94 z_decomp_free, /* decomp_free */
95 z_decomp_init, /* decomp_init */
96 z_decomp_reset, /* decomp_reset */
97 z_decompress, /* decompress */
98 z_incomp, /* incomp */
99 z_comp_stats, /* decomp_stat */
100 };
101
102 /*
103 * Space allocation and freeing routines for use by zlib routines.
104 */
105 void *
106 zalloc(notused, items, size)
107 void *notused;
108 u_int items, size;
109 {
110 void *ptr;
111
112 MALLOC(ptr, void *, items * size, M_DEVBUF, M_NOWAIT);
113 return ptr;
114 }
115
116 void
117 zfree(notused, ptr, nbytes)
118 void *notused;
119 void *ptr;
120 u_int nbytes;
121 {
122 FREE(ptr, M_DEVBUF);
123 }
124
125 /*
126 * Allocate space for a compressor.
127 */
128 static void *
129 z_comp_alloc(options, opt_len)
130 u_char *options;
131 int opt_len;
132 {
133 struct deflate_state *state;
134 int w_size;
135
136 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
137 || options[1] != CILEN_DEFLATE
138 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
139 || options[3] != DEFLATE_CHK_SEQUENCE)
140 return NULL;
141 w_size = DEFLATE_SIZE(options[2]);
142 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
143 return NULL;
144
145 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
146 M_DEVBUF, M_NOWAIT);
147 if (state == NULL)
148 return NULL;
149
150 state->strm.next_in = NULL;
151 state->strm.zalloc = zalloc;
152 state->strm.zfree = zfree;
153 if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
154 -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
155 FREE(state, M_DEVBUF);
156 return NULL;
157 }
158
159 state->w_size = w_size;
160 bzero(&state->stats, sizeof(state->stats));
161 return (void *) state;
162 }
163
164 static void
165 z_comp_free(arg)
166 void *arg;
167 {
168 struct deflate_state *state = (struct deflate_state *) arg;
169
170 deflateEnd(&state->strm);
171 FREE(state, M_DEVBUF);
172 }
173
174 static int
175 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
176 void *arg;
177 u_char *options;
178 int opt_len, unit, hdrlen, debug;
179 {
180 struct deflate_state *state = (struct deflate_state *) arg;
181
182 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
183 || options[1] != CILEN_DEFLATE
184 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
185 || DEFLATE_SIZE(options[2]) != state->w_size
186 || options[3] != DEFLATE_CHK_SEQUENCE)
187 return 0;
188
189 state->seqno = 0;
190 state->unit = unit;
191 state->hdrlen = hdrlen;
192 state->debug = debug;
193
194 deflateReset(&state->strm);
195
196 return 1;
197 }
198
199 static void
200 z_comp_reset(arg)
201 void *arg;
202 {
203 struct deflate_state *state = (struct deflate_state *) arg;
204
205 state->seqno = 0;
206 deflateReset(&state->strm);
207 }
208
209 int
210 z_compress(arg, mret, mp, orig_len, maxolen)
211 void *arg;
212 struct mbuf **mret; /* compressed packet (out) */
213 struct mbuf *mp; /* uncompressed packet (in) */
214 int orig_len, maxolen;
215 {
216 struct deflate_state *state = (struct deflate_state *) arg;
217 u_char *rptr, *wptr;
218 int proto, olen, wspace, r, flush;
219 struct mbuf *m;
220
221 /*
222 * Check that the protocol is in the range we handle.
223 */
224 rptr = mtod(mp, u_char *);
225 proto = PPP_PROTOCOL(rptr);
226 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
227 *mret = NULL;
228 return orig_len;
229 }
230
231 /* Allocate one mbuf initially. */
232 if (maxolen > orig_len)
233 maxolen = orig_len;
234 MGET(m, M_DONTWAIT, MT_DATA);
235 *mret = m;
236 if (m != NULL) {
237 m->m_len = 0;
238 if (maxolen + state->hdrlen > MLEN)
239 MCLGET(m, M_DONTWAIT);
240 wspace = M_TRAILINGSPACE(m);
241 if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
242 m->m_data += state->hdrlen;
243 wspace -= state->hdrlen;
244 }
245 wptr = mtod(m, u_char *);
246
247 /*
248 * Copy over the PPP header and store the 2-byte sequence number.
249 */
250 wptr[0] = PPP_ADDRESS(rptr);
251 wptr[1] = PPP_CONTROL(rptr);
252 wptr[2] = PPP_COMP >> 8;
253 wptr[3] = PPP_COMP;
254 wptr += PPP_HDRLEN;
255 wptr[0] = state->seqno >> 8;
256 wptr[1] = state->seqno;
257 wptr += 2;
258 state->strm.next_out = wptr;
259 state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
260 } else {
261 state->strm.next_out = NULL;
262 state->strm.avail_out = 1000000;
263 wptr = NULL;
264 wspace = 0;
265 }
266 ++state->seqno;
267
268 rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
269 state->strm.next_in = rptr;
270 state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
271 mp = mp->m_next;
272 flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
273 olen = 0;
274 for (;;) {
275 r = deflate(&state->strm, flush);
276 if (r != Z_OK) {
277 printf("z_compress: deflate returned %d (%s)\n",
278 r, (state->strm.msg? state->strm.msg: ""));
279 break;
280 }
281 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
282 break; /* all done */
283 if (state->strm.avail_in == 0 && mp != NULL) {
284 state->strm.next_in = mtod(mp, u_char *);
285 state->strm.avail_in = mp->m_len;
286 mp = mp->m_next;
287 if (mp == NULL)
288 flush = Z_PACKET_FLUSH;
289 }
290 if (state->strm.avail_out == 0) {
291 if (m != NULL) {
292 m->m_len = wspace;
293 olen += wspace;
294 MGET(m->m_next, M_DONTWAIT, MT_DATA);
295 m = m->m_next;
296 if (m != NULL) {
297 m->m_len = 0;
298 if (maxolen - olen > MLEN)
299 MCLGET(m, M_DONTWAIT);
300 state->strm.next_out = mtod(m, u_char *);
301 state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
302 }
303 }
304 if (m == NULL) {
305 state->strm.next_out = NULL;
306 state->strm.avail_out = 1000000;
307 }
308 }
309 }
310 if (m != NULL)
311 olen += (m->m_len = wspace - state->strm.avail_out);
312
313 /*
314 * See if we managed to reduce the size of the packet.
315 * If the compressor just gave us a single zero byte, it means
316 * the packet was incompressible.
317 */
318 if (m != NULL && olen < orig_len
319 && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
320 state->stats.comp_bytes += olen;
321 state->stats.comp_packets++;
322 } else {
323 if (*mret != NULL) {
324 m_freem(*mret);
325 *mret = NULL;
326 }
327 state->stats.inc_bytes += orig_len;
328 state->stats.inc_packets++;
329 olen = orig_len;
330 }
331 state->stats.unc_bytes += orig_len;
332 state->stats.unc_packets++;
333
334 return olen;
335 }
336
337 static void
338 z_comp_stats(arg, stats)
339 void *arg;
340 struct compstat *stats;
341 {
342 struct deflate_state *state = (struct deflate_state *) arg;
343 u_int out;
344
345 *stats = state->stats;
346 stats->ratio = stats->unc_bytes;
347 out = stats->comp_bytes + stats->inc_bytes;
348 if (stats->ratio <= 0x7ffffff)
349 stats->ratio <<= 8;
350 else
351 out >>= 8;
352 if (out != 0)
353 stats->ratio /= out;
354 }
355
356 /*
357 * Allocate space for a decompressor.
358 */
359 static void *
360 z_decomp_alloc(options, opt_len)
361 u_char *options;
362 int opt_len;
363 {
364 struct deflate_state *state;
365 int w_size;
366
367 if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
368 || options[1] != CILEN_DEFLATE
369 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
370 || options[3] != DEFLATE_CHK_SEQUENCE)
371 return NULL;
372 w_size = DEFLATE_SIZE(options[2]);
373 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
374 return NULL;
375
376 MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
377 M_DEVBUF, M_NOWAIT);
378 if (state == NULL)
379 return NULL;
380
381 state->strm.next_out = NULL;
382 state->strm.zalloc = zalloc;
383 state->strm.zfree = zfree;
384 if (inflateInit2(&state->strm, -w_size) != Z_OK) {
385 FREE(state, M_DEVBUF);
386 return NULL;
387 }
388
389 state->w_size = w_size;
390 bzero(&state->stats, sizeof(state->stats));
391 return (void *) state;
392 }
393
394 static void
395 z_decomp_free(arg)
396 void *arg;
397 {
398 struct deflate_state *state = (struct deflate_state *) arg;
399
400 inflateEnd(&state->strm);
401 FREE(state, M_DEVBUF);
402 }
403
404 static int
405 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
406 void *arg;
407 u_char *options;
408 int opt_len, unit, hdrlen, mru, debug;
409 {
410 struct deflate_state *state = (struct deflate_state *) arg;
411
412 if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
413 || options[1] != CILEN_DEFLATE
414 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
415 || DEFLATE_SIZE(options[2]) != state->w_size
416 || options[3] != DEFLATE_CHK_SEQUENCE)
417 return 0;
418
419 state->seqno = 0;
420 state->unit = unit;
421 state->hdrlen = hdrlen;
422 state->debug = debug;
423 state->mru = mru;
424
425 inflateReset(&state->strm);
426
427 return 1;
428 }
429
430 static void
431 z_decomp_reset(arg)
432 void *arg;
433 {
434 struct deflate_state *state = (struct deflate_state *) arg;
435
436 state->seqno = 0;
437 inflateReset(&state->strm);
438 }
439
440 /*
441 * Decompress a Deflate-compressed packet.
442 *
443 * Because of patent problems, we return DECOMP_ERROR for errors
444 * found by inspecting the input data and for system problems, but
445 * DECOMP_FATALERROR for any errors which could possibly be said to
446 * be being detected "after" decompression. For DECOMP_ERROR,
447 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
448 * infringing a patent of Motorola's if we do, so we take CCP down
449 * instead.
450 *
451 * Given that the frame has the correct sequence number and a good FCS,
452 * errors such as invalid codes in the input most likely indicate a
453 * bug, so we return DECOMP_FATALERROR for them in order to turn off
454 * compression, even though they are detected by inspecting the input.
455 */
456 int
457 z_decompress(arg, mi, mop)
458 void *arg;
459 struct mbuf *mi, **mop;
460 {
461 struct deflate_state *state = (struct deflate_state *) arg;
462 struct mbuf *mo, *mo_head;
463 u_char *rptr, *wptr;
464 int rlen, olen, ospace;
465 int seq, i, flush, r, decode_proto;
466 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
467
468 *mop = NULL;
469 rptr = mtod(mi, u_char *);
470 rlen = mi->m_len;
471 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
472 while (rlen <= 0) {
473 mi = mi->m_next;
474 if (mi == NULL)
475 return DECOMP_ERROR;
476 rptr = mtod(mi, u_char *);
477 rlen = mi->m_len;
478 }
479 hdr[i] = *rptr++;
480 --rlen;
481 }
482
483 /* Check the sequence number. */
484 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
485 if (seq != state->seqno) {
486 if (state->debug)
487 printf("z_decompress%d: bad seq # %d, expected %d\n",
488 state->unit, seq, state->seqno);
489 return DECOMP_ERROR;
490 }
491 ++state->seqno;
492
493 /* Allocate an output mbuf. */
494 MGETHDR(mo, M_DONTWAIT, MT_DATA);
495 if (mo == NULL)
496 return DECOMP_ERROR;
497 mo_head = mo;
498 mo->m_len = 0;
499 mo->m_next = NULL;
500 MCLGET(mo, M_DONTWAIT);
501 ospace = M_TRAILINGSPACE(mo);
502 if (state->hdrlen + PPP_HDRLEN < ospace) {
503 mo->m_data += state->hdrlen;
504 ospace -= state->hdrlen;
505 }
506
507 /*
508 * Fill in the first part of the PPP header. The protocol field
509 * comes from the decompressed data.
510 */
511 wptr = mtod(mo, u_char *);
512 wptr[0] = PPP_ADDRESS(hdr);
513 wptr[1] = PPP_CONTROL(hdr);
514 wptr[2] = 0;
515
516 /*
517 * Set up to call inflate. We set avail_out to 1 initially so we can
518 * look at the first byte of the output and decide whether we have
519 * a 1-byte or 2-byte protocol field.
520 */
521 state->strm.next_in = rptr;
522 state->strm.avail_in = rlen;
523 mi = mi->m_next;
524 flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
525 rlen += PPP_HDRLEN + DEFLATE_OVHD;
526 state->strm.next_out = wptr + 3;
527 state->strm.avail_out = 1;
528 decode_proto = 1;
529 olen = PPP_HDRLEN;
530
531 /*
532 * Call inflate, supplying more input or output as needed.
533 */
534 for (;;) {
535 r = inflate(&state->strm, flush);
536 if (r != Z_OK) {
537 #if !DEFLATE_DEBUG
538 if (state->debug)
539 #endif
540 printf("z_decompress%d: inflate returned %d (%s)\n",
541 state->unit, r, (state->strm.msg? state->strm.msg: ""));
542 m_freem(mo_head);
543 return DECOMP_FATALERROR;
544 }
545 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
546 break; /* all done */
547 if (state->strm.avail_in == 0 && mi != NULL) {
548 state->strm.next_in = mtod(mi, u_char *);
549 state->strm.avail_in = mi->m_len;
550 rlen += mi->m_len;
551 mi = mi->m_next;
552 if (mi == NULL)
553 flush = Z_PACKET_FLUSH;
554 }
555 if (state->strm.avail_out == 0) {
556 if (decode_proto) {
557 state->strm.avail_out = ospace - PPP_HDRLEN;
558 if ((wptr[3] & 1) == 0) {
559 /* 2-byte protocol field */
560 wptr[2] = wptr[3];
561 --state->strm.next_out;
562 ++state->strm.avail_out;
563 --olen;
564 }
565 decode_proto = 0;
566 } else {
567 mo->m_len = ospace;
568 olen += ospace;
569 MGET(mo->m_next, M_DONTWAIT, MT_DATA);
570 mo = mo->m_next;
571 if (mo == NULL) {
572 m_freem(mo_head);
573 return DECOMP_ERROR;
574 }
575 MCLGET(mo, M_DONTWAIT);
576 state->strm.next_out = mtod(mo, u_char *);
577 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
578 }
579 }
580 }
581 if (decode_proto) {
582 m_freem(mo_head);
583 return DECOMP_ERROR;
584 }
585 olen += (mo->m_len = ospace - state->strm.avail_out);
586 #if DEFLATE_DEBUG
587 if (olen > state->mru + PPP_HDRLEN)
588 printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
589 state->unit, olen, state->mru + PPP_HDRLEN);
590 #endif
591
592 state->stats.unc_bytes += olen;
593 state->stats.unc_packets++;
594 state->stats.comp_bytes += rlen;
595 state->stats.comp_packets++;
596
597 *mop = mo_head;
598 return DECOMP_OK;
599 }
600
601 /*
602 * Incompressible data has arrived - add it to the history.
603 */
604 static void
605 z_incomp(arg, mi)
606 void *arg;
607 struct mbuf *mi;
608 {
609 struct deflate_state *state = (struct deflate_state *) arg;
610 u_char *rptr;
611 int rlen, proto, r;
612
613 /*
614 * Check that the protocol is one we handle.
615 */
616 rptr = mtod(mi, u_char *);
617 proto = PPP_PROTOCOL(rptr);
618 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
619 return;
620
621 ++state->seqno;
622
623 /*
624 * Iterate through the mbufs, adding the characters in them
625 * to the decompressor's history. For the first mbuf, we start
626 * at the either the 1st or 2nd byte of the protocol field,
627 * depending on whether the protocol value is compressible.
628 */
629 rlen = mi->m_len;
630 state->strm.next_in = rptr + 3;
631 state->strm.avail_in = rlen - 3;
632 if (proto > 0xff) {
633 --state->strm.next_in;
634 ++state->strm.avail_in;
635 }
636 for (;;) {
637 r = inflateIncomp(&state->strm);
638 if (r != Z_OK) {
639 /* gak! */
640 #if !DEFLATE_DEBUG
641 if (state->debug)
642 #endif
643 printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
644 state->unit, r, (state->strm.msg? state->strm.msg: ""));
645 return;
646 }
647 mi = mi->m_next;
648 if (mi == NULL)
649 break;
650 state->strm.next_in = mtod(mi, u_char *);
651 state->strm.avail_in = mi->m_len;
652 rlen += mi->m_len;
653 }
654
655 /*
656 * Update stats.
657 */
658 state->stats.inc_bytes += rlen;
659 state->stats.inc_packets++;
660 state->stats.unc_bytes += rlen;
661 state->stats.unc_packets++;
662 }
663
664 #endif /* DO_DEFLATE */
665