Home | History | Annotate | Line # | Download | only in net
ppp-deflate.c revision 1.17.6.1
      1 /*	$NetBSD: ppp-deflate.c,v 1.17.6.1 2008/12/13 01:15:26 haad 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.17.6.1 2008/12/13 01:15:26 haad 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     MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
    168 	   M_DEVBUF, M_NOWAIT);
    169     if (state == NULL)
    170 	return NULL;
    171 
    172     state->strm.next_in = NULL;
    173     state->strm.zalloc = zalloc;
    174     state->strm.zfree = zfree;
    175     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
    176 		     -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
    177 	FREE(state, M_DEVBUF);
    178 	return NULL;
    179     }
    180 
    181     state->w_size = w_size;
    182     memset(&state->stats, 0, sizeof(state->stats));
    183     return (void *) state;
    184 }
    185 
    186 static void
    187 z_comp_free(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(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
    197             int debug)
    198 {
    199     struct deflate_state *state = (struct deflate_state *) arg;
    200 
    201     if (opt_len < CILEN_DEFLATE
    202 	|| (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
    203 	|| options[1] != CILEN_DEFLATE
    204 	|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
    205 	|| DEFLATE_SIZE(options[2]) != state->w_size
    206 	|| options[3] != DEFLATE_CHK_SEQUENCE)
    207 	return 0;
    208 
    209     state->seqno = 0;
    210     state->unit = unit;
    211     state->hdrlen = hdrlen;
    212     state->debug = debug;
    213 
    214     deflateReset(&state->strm);
    215 
    216     return 1;
    217 }
    218 
    219 static void
    220 z_comp_reset(void *arg)
    221 {
    222     struct deflate_state *state = (struct deflate_state *) arg;
    223 
    224     state->seqno = 0;
    225     deflateReset(&state->strm);
    226 }
    227 
    228 int
    229 z_compress(void *arg,
    230            struct mbuf **mret /* compressed packet (out) */,
    231 	   struct mbuf *mp /* uncompressed packet (in) */,
    232 	   int orig_len, int maxolen)
    233 {
    234     struct deflate_state *state = (struct deflate_state *) arg;
    235     u_char *rptr, *wptr;
    236     int proto, olen, wspace, r, flush;
    237     struct mbuf *m;
    238 
    239     /*
    240      * Check that the protocol is in the range we handle.
    241      */
    242     rptr = mtod(mp, u_char *);
    243     proto = PPP_PROTOCOL(rptr);
    244     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
    245 	*mret = NULL;
    246 	return orig_len;
    247     }
    248 
    249     /* Allocate one mbuf initially. */
    250     if (maxolen > orig_len)
    251 	maxolen = orig_len;
    252     MGET(m, M_DONTWAIT, MT_DATA);
    253     *mret = m;
    254     if (m != NULL) {
    255 	m->m_len = 0;
    256 	if (maxolen + state->hdrlen > MLEN)
    257 	    MCLGET(m, M_DONTWAIT);
    258 	wspace = M_TRAILINGSPACE(m);
    259 	if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
    260 	    m->m_data += state->hdrlen;
    261 	    wspace -= state->hdrlen;
    262 	}
    263 	wptr = mtod(m, u_char *);
    264 
    265 	/*
    266 	 * Copy over the PPP header and store the 2-byte sequence number.
    267 	 */
    268 	wptr[0] = PPP_ADDRESS(rptr);
    269 	wptr[1] = PPP_CONTROL(rptr);
    270 	wptr[2] = PPP_COMP >> 8;
    271 	wptr[3] = PPP_COMP;
    272 	wptr += PPP_HDRLEN;
    273 	wptr[0] = state->seqno >> 8;
    274 	wptr[1] = state->seqno;
    275 	wptr += 2;
    276 	state->strm.next_out = wptr;
    277 	state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
    278     } else {
    279 	state->strm.next_out = NULL;
    280 	state->strm.avail_out = 1000000;
    281 	wptr = NULL;
    282 	wspace = 0;
    283     }
    284     ++state->seqno;
    285 
    286     rptr += (proto > 0xff)? 2: 3;	/* skip 1st proto byte if 0 */
    287     state->strm.next_in = rptr;
    288     state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
    289     mp = mp->m_next;
    290     flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
    291     olen = 0;
    292     for (;;) {
    293 	r = deflate(&state->strm, flush);
    294 	if (r != Z_OK) {
    295 	    printf("z_compress: deflate returned %d (%s)\n",
    296 		   r, (state->strm.msg? state->strm.msg: ""));
    297 	    break;
    298 	}
    299 	if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
    300 	    break;		/* all done */
    301 	if (state->strm.avail_in == 0 && mp != NULL) {
    302 	    state->strm.next_in = mtod(mp, u_char *);
    303 	    state->strm.avail_in = mp->m_len;
    304 	    mp = mp->m_next;
    305 	    if (mp == NULL)
    306 		flush = Z_PACKET_FLUSH;
    307 	}
    308 	if (state->strm.avail_out == 0) {
    309 	    if (m != NULL) {
    310 		m->m_len = wspace;
    311 		olen += wspace;
    312 		MGET(m->m_next, M_DONTWAIT, MT_DATA);
    313 		m = m->m_next;
    314 		if (m != NULL) {
    315 		    m->m_len = 0;
    316 		    if (maxolen - olen > MLEN)
    317 			MCLGET(m, M_DONTWAIT);
    318 		    state->strm.next_out = mtod(m, u_char *);
    319 		    state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
    320 		}
    321 	    }
    322 	    if (m == NULL) {
    323 		state->strm.next_out = NULL;
    324 		state->strm.avail_out = 1000000;
    325 	    }
    326 	}
    327     }
    328     if (m != NULL)
    329 	olen += (m->m_len = wspace - state->strm.avail_out);
    330 
    331     /*
    332      * See if we managed to reduce the size of the packet.
    333      */
    334     if (m != NULL && olen < orig_len) {
    335 	state->stats.comp_bytes += olen;
    336 	state->stats.comp_packets++;
    337     } else {
    338 	if (*mret != NULL) {
    339 	    m_freem(*mret);
    340 	    *mret = NULL;
    341 	}
    342 	state->stats.inc_bytes += orig_len;
    343 	state->stats.inc_packets++;
    344 	olen = orig_len;
    345     }
    346     state->stats.unc_bytes += orig_len;
    347     state->stats.unc_packets++;
    348 
    349     return olen;
    350 }
    351 
    352 static void
    353 z_comp_stats(void *arg, struct compstat *stats)
    354 {
    355     struct deflate_state *state = (struct deflate_state *) arg;
    356     u_int out;
    357 
    358     *stats = state->stats;
    359     stats->ratio = stats->unc_bytes;
    360     out = stats->comp_bytes + stats->inc_bytes;
    361     if (stats->ratio <= 0x7ffffff)
    362 	stats->ratio <<= 8;
    363     else
    364 	out >>= 8;
    365     if (out != 0)
    366 	stats->ratio /= out;
    367 }
    368 
    369 /*
    370  * Allocate space for a decompressor.
    371  */
    372 static void *
    373 z_decomp_alloc(u_char *options, int opt_len)
    374 {
    375     struct deflate_state *state;
    376     int w_size;
    377 
    378     if (opt_len != CILEN_DEFLATE
    379 	|| (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
    380 	|| options[1] != CILEN_DEFLATE
    381 	|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
    382 	|| options[3] != DEFLATE_CHK_SEQUENCE)
    383 	return NULL;
    384     w_size = DEFLATE_SIZE(options[2]);
    385     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
    386 	return NULL;
    387 
    388     MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
    389 	   M_DEVBUF, M_NOWAIT);
    390     if (state == NULL)
    391 	return NULL;
    392 
    393     state->strm.next_out = NULL;
    394     state->strm.zalloc = zalloc;
    395     state->strm.zfree = zfree;
    396     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
    397 	FREE(state, M_DEVBUF);
    398 	return NULL;
    399     }
    400 
    401     state->w_size = w_size;
    402     memset(&state->stats, 0, sizeof(state->stats));
    403     return (void *) state;
    404 }
    405 
    406 static void
    407 z_decomp_free(void *arg)
    408 {
    409     struct deflate_state *state = (struct deflate_state *) arg;
    410 
    411     inflateEnd(&state->strm);
    412     FREE(state, M_DEVBUF);
    413 }
    414 
    415 static int
    416 z_decomp_init(void *arg, u_char *options, int opt_len, int unit, int hdrlen,
    417               int mru, int debug)
    418 {
    419     struct deflate_state *state = (struct deflate_state *) arg;
    420 
    421     if (opt_len < CILEN_DEFLATE
    422 	|| (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
    423 	|| options[1] != CILEN_DEFLATE
    424 	|| DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
    425 	|| DEFLATE_SIZE(options[2]) != state->w_size
    426 	|| options[3] != DEFLATE_CHK_SEQUENCE)
    427 	return 0;
    428 
    429     state->seqno = 0;
    430     state->unit = unit;
    431     state->hdrlen = hdrlen;
    432     state->debug = debug;
    433     state->mru = mru;
    434 
    435     inflateReset(&state->strm);
    436 
    437     return 1;
    438 }
    439 
    440 static void
    441 z_decomp_reset(void *arg)
    442 {
    443     struct deflate_state *state = (struct deflate_state *) arg;
    444 
    445     state->seqno = 0;
    446     inflateReset(&state->strm);
    447 }
    448 
    449 /*
    450  * Decompress a Deflate-compressed packet.
    451  *
    452  * Because of patent problems, we return DECOMP_ERROR for errors
    453  * found by inspecting the input data and for system problems, but
    454  * DECOMP_FATALERROR for any errors which could possibly be said to
    455  * be being detected "after" decompression.  For DECOMP_ERROR,
    456  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
    457  * infringing a patent of Motorola's if we do, so we take CCP down
    458  * instead.
    459  *
    460  * Given that the frame has the correct sequence number and a good FCS,
    461  * errors such as invalid codes in the input most likely indicate a
    462  * bug, so we return DECOMP_FATALERROR for them in order to turn off
    463  * compression, even though they are detected by inspecting the input.
    464  */
    465 int
    466 z_decompress(void *arg, struct mbuf *mi, struct mbuf **mop)
    467 {
    468     struct deflate_state *state = (struct deflate_state *) arg;
    469     struct mbuf *mo, *mo_head;
    470     u_char *rptr, *wptr;
    471     int rlen, olen, ospace;
    472     int seq, i, flush, r, decode_proto;
    473     u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
    474 
    475     *mop = NULL;
    476     rptr = mtod(mi, u_char *);
    477     rlen = mi->m_len;
    478     for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
    479 	while (rlen <= 0) {
    480 	    mi = mi->m_next;
    481 	    if (mi == NULL)
    482 		return DECOMP_ERROR;
    483 	    rptr = mtod(mi, u_char *);
    484 	    rlen = mi->m_len;
    485 	}
    486 	hdr[i] = *rptr++;
    487 	--rlen;
    488     }
    489 
    490     /* Check the sequence number. */
    491     seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
    492     if (seq != state->seqno) {
    493 	if (state->debug)
    494 	    printf("z_decompress%d: bad seq # %d, expected %d\n",
    495 		   state->unit, seq, state->seqno);
    496 	return DECOMP_ERROR;
    497     }
    498     ++state->seqno;
    499 
    500     /* Allocate an output mbuf. */
    501     MGETHDR(mo, M_DONTWAIT, MT_DATA);
    502     if (mo == NULL)
    503 	return DECOMP_ERROR;
    504     mo_head = mo;
    505     mo->m_len = 0;
    506     mo->m_next = NULL;
    507     MCLGET(mo, M_DONTWAIT);
    508     ospace = M_TRAILINGSPACE(mo);
    509     if (state->hdrlen + PPP_HDRLEN < ospace) {
    510 	mo->m_data += state->hdrlen;
    511 	ospace -= state->hdrlen;
    512     }
    513 
    514     /*
    515      * Fill in the first part of the PPP header.  The protocol field
    516      * comes from the decompressed data.
    517      */
    518     wptr = mtod(mo, u_char *);
    519     wptr[0] = PPP_ADDRESS(hdr);
    520     wptr[1] = PPP_CONTROL(hdr);
    521     wptr[2] = 0;
    522 
    523     /*
    524      * Set up to call inflate.  We set avail_out to 1 initially so we can
    525      * look at the first byte of the output and decide whether we have
    526      * a 1-byte or 2-byte protocol field.
    527      */
    528     state->strm.next_in = rptr;
    529     state->strm.avail_in = rlen;
    530     mi = mi->m_next;
    531     flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
    532     rlen += PPP_HDRLEN + DEFLATE_OVHD;
    533     state->strm.next_out = wptr + 3;
    534     state->strm.avail_out = 1;
    535     decode_proto = 1;
    536     olen = 0;
    537 
    538     /*
    539      * Call inflate, supplying more input or output as needed.
    540      */
    541     for (;;) {
    542 	r = inflate(&state->strm, flush);
    543 	if (r != Z_OK) {
    544 #if !DEFLATE_DEBUG
    545 	    if (state->debug)
    546 #endif
    547 		printf("z_decompress%d: inflate returned %d (%s)\n",
    548 		       state->unit, r, (state->strm.msg? state->strm.msg: ""));
    549 	    m_freem(mo_head);
    550 	    return DECOMP_FATALERROR;
    551 	}
    552 	if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
    553 	    break;		/* all done */
    554 	if (state->strm.avail_in == 0 && mi != NULL) {
    555 	    state->strm.next_in = mtod(mi, u_char *);
    556 	    state->strm.avail_in = mi->m_len;
    557 	    rlen += mi->m_len;
    558 	    mi = mi->m_next;
    559 	    if (mi == NULL)
    560 		flush = Z_PACKET_FLUSH;
    561 	}
    562 	if (state->strm.avail_out == 0) {
    563 	    if (decode_proto) {
    564 		state->strm.avail_out = ospace - PPP_HDRLEN;
    565 		if ((wptr[3] & 1) == 0) {
    566 		    /* 2-byte protocol field */
    567 		    wptr[2] = wptr[3];
    568 		    --state->strm.next_out;
    569 		    ++state->strm.avail_out;
    570 		    --olen;
    571 		}
    572 		decode_proto = 0;
    573 	    } else {
    574 		mo->m_len = ospace;
    575 		olen += ospace;
    576 		MGET(mo->m_next, M_DONTWAIT, MT_DATA);
    577 		mo = mo->m_next;
    578 		if (mo == NULL) {
    579 		    m_freem(mo_head);
    580 		    return DECOMP_ERROR;
    581 		}
    582 		MCLGET(mo, M_DONTWAIT);
    583 		state->strm.next_out = mtod(mo, u_char *);
    584 		state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
    585 	    }
    586 	}
    587     }
    588     if (decode_proto) {
    589 	m_freem(mo_head);
    590 	return DECOMP_ERROR;
    591     }
    592     olen += (mo->m_len = ospace - state->strm.avail_out);
    593 #if DEFLATE_DEBUG
    594     if (olen > state->mru + PPP_HDRLEN)
    595 	printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
    596 	       state->unit, olen, state->mru + PPP_HDRLEN);
    597 #endif
    598 
    599     state->stats.unc_bytes += olen;
    600     state->stats.unc_packets++;
    601     state->stats.comp_bytes += rlen;
    602     state->stats.comp_packets++;
    603 
    604     *mop = mo_head;
    605     return DECOMP_OK;
    606 }
    607 
    608 /*
    609  * Incompressible data has arrived - add it to the history.
    610  */
    611 static void
    612 z_incomp(void *arg, struct mbuf *mi)
    613 {
    614     struct deflate_state *state = (struct deflate_state *) arg;
    615     u_char *rptr;
    616     int rlen, proto, r;
    617 
    618     /*
    619      * Check that the protocol is one we handle.
    620      */
    621     rptr = mtod(mi, u_char *);
    622     proto = PPP_PROTOCOL(rptr);
    623     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
    624 	return;
    625 
    626     ++state->seqno;
    627 
    628     /*
    629      * Iterate through the mbufs, adding the characters in them
    630      * to the decompressor's history.  For the first mbuf, we start
    631      * at the either the 1st or 2nd byte of the protocol field,
    632      * depending on whether the protocol value is compressible.
    633      */
    634     rlen = mi->m_len;
    635     state->strm.next_in = rptr + 3;
    636     state->strm.avail_in = rlen - 3;
    637     if (proto > 0xff) {
    638 	--state->strm.next_in;
    639 	++state->strm.avail_in;
    640     }
    641     for (;;) {
    642 	r = inflateIncomp(&state->strm);
    643 	if (r != Z_OK) {
    644 	    /* gak! */
    645 #if !DEFLATE_DEBUG
    646 	    if (state->debug)
    647 #endif
    648 		printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
    649 		       state->unit, r, (state->strm.msg? state->strm.msg: ""));
    650 	    return;
    651 	}
    652 	mi = mi->m_next;
    653 	if (mi == NULL)
    654 	    break;
    655 	state->strm.next_in = mtod(mi, u_char *);
    656 	state->strm.avail_in = mi->m_len;
    657 	rlen += mi->m_len;
    658     }
    659 
    660     /*
    661      * Update stats.
    662      */
    663     state->stats.inc_bytes += rlen;
    664     state->stats.inc_packets++;
    665     state->stats.unc_bytes += rlen;
    666     state->stats.unc_packets++;
    667 }
    668 
    669 MODULE(MODULE_CLASS_MISC, ppp_deflate, NULL);
    670 
    671 static int
    672 ppp_deflate_modcmd(modcmd_t cmd, void *arg)
    673 {
    674 
    675 	switch (cmd) {
    676 	case MODULE_CMD_INIT:
    677 		return ppp_register_compressor(ppp_deflate, 2);
    678 	case MODULE_CMD_FINI:
    679 		return ppp_unregister_compressor(ppp_deflate, 2);
    680 	case MODULE_CMD_STAT:
    681 		return 0;
    682 	default:
    683 		return ENOTTY;
    684 	}
    685 
    686 	return ENOTTY;
    687 }
    688 #endif /* DO_DEFLATE */
    689