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