1 /* $OpenBSD: ppp-deflate.c,v 1.12 2015/07/15 22:16:42 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 = mallocarray(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, 0); 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, 0); 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, 0); 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 m_freem(*mret); 346 *mret = NULL; 347 348 state->stats.inc_bytes += orig_len; 349 state->stats.inc_packets++; 350 olen = orig_len; 351 } 352 state->stats.unc_bytes += orig_len; 353 state->stats.unc_packets++; 354 355 return olen; 356 } 357 358 static void 359 z_comp_stats(arg, stats) 360 void *arg; 361 struct compstat *stats; 362 { 363 struct deflate_state *state = (struct deflate_state *) arg; 364 u_int out; 365 366 *stats = state->stats; 367 stats->ratio = stats->unc_bytes; 368 out = stats->comp_bytes + stats->inc_bytes; 369 if (stats->ratio <= 0x7ffffff) 370 stats->ratio <<= 8; 371 else 372 out >>= 8; 373 if (out != 0) 374 stats->ratio /= out; 375 } 376 377 /* 378 * Allocate space for a decompressor. 379 */ 380 static void * 381 z_decomp_alloc(options, opt_len) 382 u_char *options; 383 int opt_len; 384 { 385 struct deflate_state *state; 386 int w_size; 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 || options[3] != DEFLATE_CHK_SEQUENCE) 393 return NULL; 394 w_size = DEFLATE_SIZE(options[2]); 395 if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE) 396 return NULL; 397 398 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT); 399 if (state == NULL) 400 return NULL; 401 402 state->strm.next_out = NULL; 403 state->strm.zalloc = zcalloc; 404 state->strm.zfree = zcfree; 405 if (inflateInit2(&state->strm, -w_size) != Z_OK) { 406 free(state, M_DEVBUF, 0); 407 return NULL; 408 } 409 410 state->w_size = w_size; 411 bzero(&state->stats, sizeof(state->stats)); 412 return (void *) state; 413 } 414 415 static void 416 z_decomp_free(arg) 417 void *arg; 418 { 419 struct deflate_state *state = (struct deflate_state *) arg; 420 421 inflateEnd(&state->strm); 422 free(state, M_DEVBUF, 0); 423 } 424 425 static int 426 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug) 427 void *arg; 428 u_char *options; 429 int opt_len, unit, hdrlen, mru, debug; 430 { 431 struct deflate_state *state = (struct deflate_state *) arg; 432 433 if (opt_len < CILEN_DEFLATE 434 || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) 435 || options[1] != CILEN_DEFLATE 436 || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL 437 || DEFLATE_SIZE(options[2]) != state->w_size 438 || options[3] != DEFLATE_CHK_SEQUENCE) 439 return 0; 440 441 state->seqno = 0; 442 state->unit = unit; 443 state->hdrlen = hdrlen; 444 state->debug = debug; 445 state->mru = mru; 446 447 inflateReset(&state->strm); 448 449 return 1; 450 } 451 452 static void 453 z_decomp_reset(arg) 454 void *arg; 455 { 456 struct deflate_state *state = (struct deflate_state *) arg; 457 458 state->seqno = 0; 459 inflateReset(&state->strm); 460 } 461 462 /* 463 * Decompress a Deflate-compressed packet. 464 * 465 * Because of patent problems, we return DECOMP_ERROR for errors 466 * found by inspecting the input data and for system problems, but 467 * DECOMP_FATALERROR for any errors which could possibly be said to 468 * be being detected "after" decompression. For DECOMP_ERROR, 469 * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be 470 * infringing a patent of Motorola's if we do, so we take CCP down 471 * instead. 472 * 473 * Given that the frame has the correct sequence number and a good FCS, 474 * errors such as invalid codes in the input most likely indicate a 475 * bug, so we return DECOMP_FATALERROR for them in order to turn off 476 * compression, even though they are detected by inspecting the input. 477 */ 478 int 479 z_decompress(arg, mi, mop) 480 void *arg; 481 struct mbuf *mi, **mop; 482 { 483 struct deflate_state *state = (struct deflate_state *) arg; 484 struct mbuf *mo, *mo_head; 485 u_char *rptr, *wptr; 486 int rlen, olen, ospace; 487 int seq, i, flush, r, decode_proto; 488 u_char hdr[PPP_HDRLEN + DEFLATE_OVHD]; 489 490 *mop = NULL; 491 rptr = mtod(mi, u_char *); 492 rlen = mi->m_len; 493 for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) { 494 while (rlen <= 0) { 495 mi = mi->m_next; 496 if (mi == NULL) 497 return DECOMP_ERROR; 498 rptr = mtod(mi, u_char *); 499 rlen = mi->m_len; 500 } 501 hdr[i] = *rptr++; 502 --rlen; 503 } 504 505 /* Check the sequence number. */ 506 seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1]; 507 if (seq != state->seqno) { 508 if (state->debug) 509 printf("z_decompress%d: bad seq # %d, expected %d\n", 510 state->unit, seq, state->seqno); 511 return DECOMP_ERROR; 512 } 513 ++state->seqno; 514 515 /* Allocate an output mbuf. */ 516 MGETHDR(mo, M_DONTWAIT, MT_DATA); 517 if (mo == NULL) 518 return DECOMP_ERROR; 519 mo_head = mo; 520 mo->m_len = 0; 521 mo->m_next = NULL; 522 MCLGET(mo, M_DONTWAIT); 523 ospace = M_TRAILINGSPACE(mo); 524 if (state->hdrlen + PPP_HDRLEN < ospace) { 525 mo->m_data += state->hdrlen; 526 ospace -= state->hdrlen; 527 } 528 529 /* 530 * Fill in the first part of the PPP header. The protocol field 531 * comes from the decompressed data. 532 */ 533 wptr = mtod(mo, u_char *); 534 wptr[0] = PPP_ADDRESS(hdr); 535 wptr[1] = PPP_CONTROL(hdr); 536 wptr[2] = 0; 537 538 /* 539 * Set up to call inflate. We set avail_out to 1 initially so we can 540 * look at the first byte of the output and decide whether we have 541 * a 1-byte or 2-byte protocol field. 542 */ 543 state->strm.next_in = rptr; 544 state->strm.avail_in = rlen; 545 mi = mi->m_next; 546 flush = (mi == NULL)? Z_SYNC_FLUSH: Z_NO_FLUSH; 547 rlen += PPP_HDRLEN + DEFLATE_OVHD; 548 state->strm.next_out = wptr + 3; 549 state->strm.avail_out = 1; 550 decode_proto = 1; 551 olen = PPP_HDRLEN; 552 553 /* 554 * Call inflate, supplying more input or output as needed. 555 */ 556 for (;;) { 557 r = inflate(&state->strm, flush); 558 if (r != Z_OK) { 559 #ifndef DEFLATE_DEBUG 560 if (state->debug) 561 #endif 562 printf("z_decompress%d: inflate returned %d (%s)\n", 563 state->unit, r, (state->strm.msg? state->strm.msg: "")); 564 m_freem(mo_head); 565 return DECOMP_FATALERROR; 566 } 567 if (flush != Z_NO_FLUSH && state->strm.avail_out != 0) 568 break; /* all done */ 569 if (state->strm.avail_in == 0 && mi != NULL) { 570 state->strm.next_in = mtod(mi, u_char *); 571 state->strm.avail_in = mi->m_len; 572 rlen += mi->m_len; 573 mi = mi->m_next; 574 if (mi == NULL) 575 flush = Z_SYNC_FLUSH; 576 } 577 if (state->strm.avail_out == 0) { 578 if (decode_proto) { 579 state->strm.avail_out = ospace - PPP_HDRLEN; 580 if ((wptr[3] & 1) == 0) { 581 /* 2-byte protocol field */ 582 wptr[2] = wptr[3]; 583 --state->strm.next_out; 584 ++state->strm.avail_out; 585 --olen; 586 } 587 decode_proto = 0; 588 } else { 589 mo->m_len = ospace; 590 olen += ospace; 591 MGET(mo->m_next, M_DONTWAIT, MT_DATA); 592 mo = mo->m_next; 593 if (mo == NULL) { 594 m_freem(mo_head); 595 return DECOMP_ERROR; 596 } 597 MCLGET(mo, M_DONTWAIT); 598 state->strm.next_out = mtod(mo, u_char *); 599 state->strm.avail_out = ospace = M_TRAILINGSPACE(mo); 600 } 601 } 602 } 603 if (decode_proto) { 604 m_freem(mo_head); 605 return DECOMP_ERROR; 606 } 607 olen += (mo->m_len = ospace - state->strm.avail_out); 608 #ifdef DEFLATE_DEBUG 609 if (olen > state->mru + PPP_HDRLEN) 610 printf("ppp_deflate%d: exceeded mru (%d > %d)\n", 611 state->unit, olen, state->mru + PPP_HDRLEN); 612 #endif 613 614 state->stats.unc_bytes += olen; 615 state->stats.unc_packets++; 616 state->stats.comp_bytes += rlen; 617 state->stats.comp_packets++; 618 619 *mop = mo_head; 620 return DECOMP_OK; 621 } 622 623 /* 624 * Incompressible data has arrived - add it to the history. 625 */ 626 static void 627 z_incomp(arg, mi) 628 void *arg; 629 struct mbuf *mi; 630 { 631 struct deflate_state *state = (struct deflate_state *) arg; 632 u_char *rptr; 633 int rlen, proto, r; 634 635 /* 636 * Check that the protocol is one we handle. 637 */ 638 rptr = mtod(mi, u_char *); 639 proto = PPP_PROTOCOL(rptr); 640 if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) 641 return; 642 643 ++state->seqno; 644 645 /* 646 * Iterate through the mbufs, adding the characters in them 647 * to the decompressor's history. For the first mbuf, we start 648 * at the either the 1st or 2nd byte of the protocol field, 649 * depending on whether the protocol value is compressible. 650 */ 651 rlen = mi->m_len; 652 state->strm.next_in = rptr + 3; 653 state->strm.avail_in = rlen - 3; 654 if (proto > 0xff) { 655 --state->strm.next_in; 656 ++state->strm.avail_in; 657 } 658 for (;;) { 659 r = inflateInit(&state->strm); 660 if (r != Z_OK) { 661 /* gak! */ 662 #ifndef DEFLATE_DEBUG 663 if (state->debug) 664 #endif 665 printf("z_incomp%d: inflateIncomp returned %d (%s)\n", 666 state->unit, r, (state->strm.msg? state->strm.msg: "")); 667 return; 668 } 669 mi = mi->m_next; 670 if (mi == NULL) 671 break; 672 state->strm.next_in = mtod(mi, u_char *); 673 state->strm.avail_in = mi->m_len; 674 rlen += mi->m_len; 675 } 676 677 /* 678 * Update stats. 679 */ 680 state->stats.inc_bytes += rlen; 681 state->stats.inc_packets++; 682 state->stats.unc_bytes += rlen; 683 state->stats.unc_packets++; 684 } 685 686 #endif /* DO_DEFLATE */ 687