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