xref: /netbsd-src/sys/net/bsd-comp.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1 /*	$NetBSD: bsd-comp.c,v 1.23 2024/07/05 04:31:53 rin Exp $	*/
2 /*	Id: bsd-comp.c,v 1.6 1996/08/28 06:31:58 paulus Exp 	*/
3 
4 /* Because this code is derived from the 4.3BSD compress source:
5  *
6  *
7  * Copyright (c) 1985, 1986 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * James A. Woods, derived from original work by Spencer Thomas
12  * and Joseph Orost.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * This version is for use with mbufs on BSD-derived systems.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: bsd-comp.c,v 1.23 2024/07/05 04:31:53 rin Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/module.h>
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/ppp_defs.h>
54 #include <net/if_ppp.h>
55 
56 #define PACKETPTR	struct mbuf *
57 #include <net/ppp-comp.h>
58 
59 #if DO_BSD_COMPRESS
60 /*
61  * PPP "BSD compress" compression
62  *  The differences between this compression and the classic BSD LZW
63  *  source are obvious from the requirement that the classic code worked
64  *  with files while this handles arbitrarily long streams that
65  *  are broken into packets.  They are:
66  *
67  *	When the code size expands, a block of junk is not emitted by
68  *	    the compressor and not expected by the decompressor.
69  *
70  *	New codes are not necessarily assigned every time an old
71  *	    code is output by the compressor.  This is because a packet
72  *	    end forces a code to be emitted, but does not imply that a
73  *	    new sequence has been seen.
74  *
75  *	The compression ratio is checked at the first end of a packet
76  *	    after the appropriate gap.	Besides simplifying and speeding
77  *	    things up, this makes it more likely that the transmitter
78  *	    and receiver will agree when the dictionary is cleared when
79  *	    compression is not going well.
80  */
81 
82 /*
83  * A dictionary for doing BSD compress.
84  */
85 struct bsd_db {
86     int	    totlen;			/* length of this structure */
87     u_int   hsize;			/* size of the hash table */
88     u_char  hshift;			/* used in hash function */
89     u_char  n_bits;			/* current bits/code */
90     u_char  maxbits;
91     u_char  debug;
92     u_char  unit;
93     uint16_t seqno;			/* sequence # of next packet */
94     u_int   hdrlen;			/* header length to preallocate */
95     u_int   mru;
96     u_int   maxmaxcode;			/* largest valid code */
97     u_int   max_ent;			/* largest code in use */
98     u_int   in_count;			/* uncompressed bytes, aged */
99     u_int   bytes_out;			/* compressed bytes, aged */
100     u_int   ratio;			/* recent compression ratio */
101     u_int   checkpoint;			/* when to next check the ratio */
102     u_int   clear_count;		/* times dictionary cleared */
103     u_int   incomp_count;		/* incompressible packets */
104     u_int   incomp_bytes;		/* incompressible bytes */
105     u_int   uncomp_count;		/* uncompressed packets */
106     u_int   uncomp_bytes;		/* uncompressed bytes */
107     u_int   comp_count;			/* compressed packets */
108     u_int   comp_bytes;			/* compressed bytes */
109     uint16_t *lens;			/* array of lengths of codes */
110     struct bsd_dict {
111 	union {				/* hash value */
112 	    uint32_t	fcode;
113 	    struct {
114 #if BYTE_ORDER == LITTLE_ENDIAN
115 		uint16_t prefix;	/* preceding code */
116 		u_char	suffix;		/* last character of new code */
117 		u_char	pad;
118 #else
119 		u_char	pad;
120 		u_char	suffix;		/* last character of new code */
121 		uint16_t prefix;	/* preceding code */
122 #endif
123 	    } hs;
124 	} f;
125 	uint16_t codem1;		/* output of hash table -1 */
126 	uint16_t cptr;			/* map code to hash table entry */
127     } dict[1];
128 };
129 
130 #define BSD_OVHD	2		/* BSD compress overhead/packet */
131 #define BSD_INIT_BITS	BSD_MIN_BITS
132 
133 static void	*bsd_comp_alloc(u_char *options, int opt_len);
134 static void	*bsd_decomp_alloc(u_char *options, int opt_len);
135 static void	bsd_free(void *state);
136 static int	bsd_comp_init(void *state, u_char *options, int opt_len,
137 			      int unit, int hdrlen, int debug);
138 static int	bsd_decomp_init(void *state, u_char *options, int opt_len,
139 				int unit, int hdrlen, int mru, int debug);
140 static int	bsd_compress(void *state, struct mbuf **mret,
141 			     struct mbuf *mp, int slen, int maxolen);
142 static void	bsd_incomp(void *state, struct mbuf *dmsg);
143 static int	bsd_decompress(void *state, struct mbuf *cmp,
144 			       struct mbuf **dmpp);
145 static void	bsd_reset(void *state);
146 static void	bsd_comp_stats(void *state, struct compstat *stats);
147 
148 /*
149  * Procedures exported to if_ppp.c.
150  */
151 static struct compressor ppp_bsd_compress = {
152     .compress_proto =	CI_BSD_COMPRESS,
153     .comp_alloc =	bsd_comp_alloc,
154     .comp_free =	bsd_free,
155     .comp_init =	bsd_comp_init,
156     .comp_reset =	bsd_reset,
157     .compress =		bsd_compress,
158     .comp_stat =	bsd_comp_stats,
159     .decomp_alloc =	bsd_decomp_alloc,
160     .decomp_free =	bsd_free,
161     .decomp_init =	bsd_decomp_init,
162     .decomp_reset =	bsd_reset,
163     .decompress =	bsd_decompress,
164     .incomp =		bsd_incomp,
165     .decomp_stat =	bsd_comp_stats,
166 };
167 
168 /*
169  * the next two codes should not be changed lightly, as they must not
170  * lie within the contiguous general code space.
171  */
172 #define CLEAR	256			/* table clear output code */
173 #define FIRST	257			/* first free entry */
174 #define LAST	255
175 
176 #define MAXCODE(b)	((1 << (b)) - 1)
177 #define BADCODEM1	MAXCODE(BSD_MAX_BITS)
178 
179 #define BSD_HASH(prefix,suffix,hshift)	((((uint32_t)(suffix)) << (hshift)) \
180 					 ^ (uint32_t)(prefix))
181 #define BSD_KEY(prefix,suffix)		((((uint32_t)(suffix)) << 16) \
182 					 + (uint32_t)(prefix))
183 
184 #define CHECK_GAP	10000		/* Ratio check interval */
185 
186 #define RATIO_SCALE_LOG	8
187 #define RATIO_SCALE	(1<<RATIO_SCALE_LOG)
188 #define RATIO_MAX	(0x7fffffff>>RATIO_SCALE_LOG)
189 
190 static void	bsd_clear(struct bsd_db *);
191 static int	bsd_check(struct bsd_db *);
192 static void	*bsd_alloc(u_char *, int, int);
193 static int	bsd_init(struct bsd_db *, u_char *, int, int, int, int,
194 			 int, int);
195 
196 /*
197  * clear the dictionary
198  */
199 static void
bsd_clear(struct bsd_db * db)200 bsd_clear(struct bsd_db *db)
201 {
202     db->clear_count++;
203     db->max_ent = FIRST-1;
204     db->n_bits = BSD_INIT_BITS;
205     db->ratio = 0;
206     db->bytes_out = 0;
207     db->in_count = 0;
208     db->checkpoint = CHECK_GAP;
209 }
210 
211 /*
212  * If the dictionary is full, then see if it is time to reset it.
213  *
214  * Compute the compression ratio using fixed-point arithmetic
215  * with 8 fractional bits.
216  *
217  * Since we have an infinite stream instead of a single file,
218  * watch only the local compression ratio.
219  *
220  * Since both peers must reset the dictionary at the same time even in
221  * the absence of CLEAR codes (while packets are incompressible), they
222  * must compute the same ratio.
223  */
224 static int				/* 1=output CLEAR */
bsd_check(struct bsd_db * db)225 bsd_check(struct bsd_db *db)
226 {
227     u_int new_ratio;
228 
229     if (db->in_count >= db->checkpoint) {
230 	/* age the ratio by limiting the size of the counts */
231 	if (db->in_count >= RATIO_MAX
232 	    || db->bytes_out >= RATIO_MAX) {
233 	    db->in_count -= db->in_count/4;
234 	    db->bytes_out -= db->bytes_out/4;
235 	}
236 
237 	db->checkpoint = db->in_count + CHECK_GAP;
238 
239 	if (db->max_ent >= db->maxmaxcode) {
240 	    /* Reset the dictionary only if the ratio is worse,
241 	     * or if it looks as if it has been poisoned
242 	     * by incompressible data.
243 	     *
244 	     * This does not overflow, because
245 	     *	db->in_count <= RATIO_MAX.
246 	     */
247 	    new_ratio = db->in_count << RATIO_SCALE_LOG;
248 	    if (db->bytes_out != 0)
249 		new_ratio /= db->bytes_out;
250 
251 	    if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) {
252 		bsd_clear(db);
253 		return 1;
254 	    }
255 	    db->ratio = new_ratio;
256 	}
257     }
258     return 0;
259 }
260 
261 /*
262  * Return statistics.
263  */
264 static void
bsd_comp_stats(void * state,struct compstat * stats)265 bsd_comp_stats(void *state, struct compstat *stats)
266 {
267     struct bsd_db *db = (struct bsd_db *) state;
268     u_int out;
269 
270     stats->unc_bytes = db->uncomp_bytes;
271     stats->unc_packets = db->uncomp_count;
272     stats->comp_bytes = db->comp_bytes;
273     stats->comp_packets = db->comp_count;
274     stats->inc_bytes = db->incomp_bytes;
275     stats->inc_packets = db->incomp_count;
276     stats->ratio = db->in_count;
277     out = db->bytes_out;
278     if (stats->ratio <= 0x7fffff)
279 	stats->ratio <<= 8;
280     else
281 	out >>= 8;
282     if (out != 0)
283 	stats->ratio /= out;
284 }
285 
286 /*
287  * Reset state, as on a CCP ResetReq.
288  */
289 static void
bsd_reset(void * state)290 bsd_reset(void *state)
291 {
292     struct bsd_db *db = (struct bsd_db *) state;
293 
294     db->seqno = 0;
295     bsd_clear(db);
296     db->clear_count = 0;
297 }
298 
299 /*
300  * Allocate space for a (de) compressor.
301  */
302 static void *
bsd_alloc(u_char * options,int opt_len,int decomp)303 bsd_alloc(u_char *options, int opt_len, int decomp)
304 {
305     int bits;
306     u_int newlen, hsize, hshift, maxmaxcode;
307     struct bsd_db *db;
308 
309     if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
310 	|| options[1] != CILEN_BSD_COMPRESS
311 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
312 	return NULL;
313     bits = BSD_NBITS(options[2]);
314     switch (bits) {
315     case 9:			/* needs 82152 for both directions */
316     case 10:			/* needs 84144 */
317     case 11:			/* needs 88240 */
318     case 12:			/* needs 96432 */
319 	hsize = 5003;
320 	hshift = 4;
321 	break;
322     case 13:			/* needs 176784 */
323 	hsize = 9001;
324 	hshift = 5;
325 	break;
326     case 14:			/* needs 353744 */
327 	hsize = 18013;
328 	hshift = 6;
329 	break;
330     case 15:			/* needs 691440 */
331 	hsize = 35023;
332 	hshift = 7;
333 	break;
334     case 16:			/* needs 1366160--far too much, */
335 	/* hsize = 69001; */	/* and 69001 is too big for cptr */
336 	/* hshift = 8; */	/* in struct bsd_db */
337 	/* break; */
338     default:
339 	return NULL;
340     }
341 
342     maxmaxcode = MAXCODE(bits);
343     newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0]));
344     db = malloc(newlen, M_DEVBUF, M_NOWAIT|M_ZERO);
345     if (!db)
346 	return NULL;
347 
348     if (!decomp) {
349 	db->lens = NULL;
350     } else {
351 	db->lens = malloc((maxmaxcode+1) * sizeof(db->lens[0]),
352 	    M_DEVBUF, M_NOWAIT);
353 	if (!db->lens) {
354 	    free(db, M_DEVBUF);
355 	    return NULL;
356 	}
357     }
358 
359     db->totlen = newlen;
360     db->hsize = hsize;
361     db->hshift = hshift;
362     db->maxmaxcode = maxmaxcode;
363     db->maxbits = bits;
364 
365     return (void *) db;
366 }
367 
368 static void
bsd_free(void * state)369 bsd_free(void *state)
370 {
371     struct bsd_db *db = (struct bsd_db *) state;
372 
373     if (db->lens)
374 	free(db->lens, M_DEVBUF);
375     free(db, M_DEVBUF);
376 }
377 
378 static void *
bsd_comp_alloc(u_char * options,int opt_len)379 bsd_comp_alloc(u_char *options, int opt_len)
380 {
381     return bsd_alloc(options, opt_len, 0);
382 }
383 
384 static void *
bsd_decomp_alloc(u_char * options,int opt_len)385 bsd_decomp_alloc(u_char *options, int opt_len)
386 {
387     return bsd_alloc(options, opt_len, 1);
388 }
389 
390 /*
391  * Initialize the database.
392  */
393 static int
bsd_init(struct bsd_db * db,u_char * options,int opt_len,int unit,int hdrlen,int mru,int debug,int decomp)394 bsd_init(struct bsd_db *db, u_char *options, int opt_len, int unit, int hdrlen,
395     int mru, int debug, int decomp)
396 {
397     int i;
398 
399     if (opt_len < CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS
400 	|| options[1] != CILEN_BSD_COMPRESS
401 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION
402 	|| BSD_NBITS(options[2]) != db->maxbits
403 	|| (decomp && db->lens == NULL))
404 	return 0;
405 
406     if (decomp) {
407 	i = LAST+1;
408 	while (i != 0)
409 	    db->lens[--i] = 1;
410     }
411     i = db->hsize;
412     while (i != 0) {
413 	db->dict[--i].codem1 = BADCODEM1;
414 	db->dict[i].cptr = 0;
415     }
416 
417     db->unit = unit;
418     db->hdrlen = hdrlen;
419     db->mru = mru;
420 #ifndef DEBUG
421     if (debug)
422 #endif
423 	db->debug = 1;
424 
425     bsd_reset(db);
426 
427     return 1;
428 }
429 
430 static int
bsd_comp_init(void * state,u_char * options,int opt_len,int unit,int hdrlen,int debug)431 bsd_comp_init(void *state, u_char *options, int opt_len, int unit, int hdrlen,
432               int debug)
433 {
434     return bsd_init((struct bsd_db *) state, options, opt_len,
435 		    unit, hdrlen, 0, debug, 0);
436 }
437 
438 static int
bsd_decomp_init(void * state,u_char * options,int opt_len,int unit,int hdrlen,int mru,int debug)439 bsd_decomp_init(void *state, u_char *options, int opt_len, int unit, int hdrlen,
440                 int mru, int debug)
441 {
442     return bsd_init((struct bsd_db *) state, options, opt_len,
443 		    unit, hdrlen, mru, debug, 1);
444 }
445 
446 
447 /*
448  * compress a packet
449  *	One change from the BSD compress command is that when the
450  *	code size expands, we do not output a bunch of padding.
451  */
452 int					/* new slen */
bsd_compress(void * state,struct mbuf ** mret,struct mbuf * mp,int slen,int maxolen)453 bsd_compress(void *state,
454              struct mbuf **mret /* return compressed mbuf chain here */,
455 	     struct mbuf *mp /* from here */,
456 	     int slen, int maxolen)
457 {
458     struct bsd_db *db = (struct bsd_db *) state;
459     int hshift = db->hshift;
460     u_int max_ent = db->max_ent;
461     u_int n_bits = db->n_bits;
462     u_int bitno = 32;
463     uint32_t accm = 0, fcode;
464     struct bsd_dict *dictp;
465     u_char c;
466     int hval, disp, ent, ilen;
467     u_char *rptr, *wptr;
468     u_char *cp_end;
469     int olen;
470     struct mbuf *m;
471 
472 #define PUTBYTE(v) {					\
473     ++olen;						\
474     if (wptr) {						\
475 	*wptr++ = (v);					\
476 	if (wptr >= cp_end) {				\
477 	    m->m_len = wptr - mtod(m, u_char *);	\
478 	    MGET(m->m_next, M_DONTWAIT, MT_DATA);	\
479 	    m = m->m_next;				\
480 	    if (m) {					\
481 		m->m_len = 0;				\
482 		if (maxolen - olen > MLEN)		\
483 		    MCLGET(m, M_DONTWAIT);		\
484 		wptr = mtod(m, u_char *);		\
485 		cp_end = wptr + M_TRAILINGSPACE(m);	\
486 	    } else					\
487 		wptr = NULL;				\
488 	}						\
489     }							\
490 }
491 
492 #define OUTPUT(ent) {					\
493     bitno -= n_bits;					\
494     accm |= ((ent) << bitno);				\
495     do {						\
496 	PUTBYTE(accm >> 24);				\
497 	accm <<= 8;					\
498 	bitno += 8;					\
499     } while (bitno <= 24);				\
500 }
501 
502     /*
503      * If the protocol is not in the range we're interested in,
504      * just return without compressing the packet.  If it is,
505      * the protocol becomes the first byte to compress.
506      */
507     rptr = mtod(mp, u_char *);
508     ent = PPP_PROTOCOL(rptr);
509     if (ent < 0x21 || ent > 0xf9) {
510 	*mret = NULL;
511 	return slen;
512     }
513 
514     /* Don't generate compressed packets which are larger than
515        the uncompressed packet. */
516     if (maxolen > slen)
517 	maxolen = slen;
518 
519     /* Allocate one mbuf to start with. */
520     MGET(m, M_DONTWAIT, MT_DATA);
521     *mret = m;
522     if (m != NULL) {
523 	m->m_len = 0;
524 	if (maxolen + db->hdrlen > MLEN)
525 	    MCLGET(m, M_DONTWAIT);
526 	m->m_data += db->hdrlen;
527 	wptr = mtod(m, u_char *);
528 	cp_end = wptr + M_TRAILINGSPACE(m);
529     } else
530 	wptr = cp_end = NULL;
531 
532     /*
533      * Copy the PPP header over, changing the protocol,
534      * and install the 2-byte packet sequence number.
535      */
536     if (wptr) {
537 	*wptr++ = PPP_ADDRESS(rptr);	/* assumes the ppp header is */
538 	*wptr++ = PPP_CONTROL(rptr);	/* all in one mbuf */
539 	*wptr++ = 0;			/* change the protocol */
540 	*wptr++ = PPP_COMP;
541 	*wptr++ = db->seqno >> 8;
542 	*wptr++ = db->seqno;
543     }
544     ++db->seqno;
545 
546     olen = 0;
547     rptr += PPP_HDRLEN;
548     slen = mp->m_len - PPP_HDRLEN;
549     ilen = slen + 1;
550     for (;;) {
551 	if (slen <= 0) {
552 	    mp = mp->m_next;
553 	    if (!mp)
554 		break;
555 	    rptr = mtod(mp, u_char *);
556 	    slen = mp->m_len;
557 	    if (!slen)
558 		continue;   /* handle 0-length buffers */
559 	    ilen += slen;
560 	}
561 
562 	slen--;
563 	c = *rptr++;
564 	fcode = BSD_KEY(ent, c);
565 	hval = BSD_HASH(ent, c, hshift);
566 	dictp = &db->dict[hval];
567 
568 	/* Validate and then check the entry. */
569 	if (dictp->codem1 >= max_ent)
570 	    goto nomatch;
571 	if (dictp->f.fcode == fcode) {
572 	    ent = dictp->codem1+1;
573 	    continue;	/* found (prefix,suffix) */
574 	}
575 
576 	/* continue probing until a match or invalid entry */
577 	disp = (hval == 0) ? 1 : hval;
578 	do {
579 	    hval += disp;
580 	    if (hval >= db->hsize)
581 		hval -= db->hsize;
582 	    dictp = &db->dict[hval];
583 	    if (dictp->codem1 >= max_ent)
584 		goto nomatch;
585 	} while (dictp->f.fcode != fcode);
586 	ent = dictp->codem1 + 1;	/* finally found (prefix,suffix) */
587 	continue;
588 
589     nomatch:
590 	OUTPUT(ent);		/* output the prefix */
591 
592 	/* code -> hashtable */
593 	if (max_ent < db->maxmaxcode) {
594 	    struct bsd_dict *dictp2;
595 	    /* expand code size if needed */
596 	    if (max_ent >= MAXCODE(n_bits))
597 		db->n_bits = ++n_bits;
598 
599 	    /* Invalidate old hash table entry using
600 	     * this code, and then take it over.
601 	     */
602 	    dictp2 = &db->dict[max_ent+1];
603 	    if (db->dict[dictp2->cptr].codem1 == max_ent)
604 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
605 	    dictp2->cptr = hval;
606 	    dictp->codem1 = max_ent;
607 	    dictp->f.fcode = fcode;
608 
609 	    db->max_ent = ++max_ent;
610 	}
611 	ent = c;
612     }
613 
614     OUTPUT(ent);		/* output the last code */
615     db->bytes_out += olen;
616     db->in_count += ilen;
617     if (bitno < 32)
618 	++db->bytes_out;	/* count complete bytes */
619 
620     if (bsd_check(db))
621 	OUTPUT(CLEAR);		/* do not count the CLEAR */
622 
623     /*
624      * Pad dribble bits of last code with ones.
625      * Do not emit a completely useless byte of ones.
626      */
627     if (bitno != 32)
628 	PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
629 
630     if (m != NULL) {
631 	m->m_len = wptr - mtod(m, u_char *);
632 	m->m_next = NULL;
633     }
634 
635     /*
636      * Increase code size if we would have without the packet
637      * boundary and as the decompressor will.
638      */
639     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
640 	db->n_bits++;
641 
642     db->uncomp_bytes += ilen;
643     ++db->uncomp_count;
644     if (olen + PPP_HDRLEN + BSD_OVHD > maxolen) {
645 	/* throw away the compressed stuff if it is longer than uncompressed */
646 	m_freem(*mret);
647 	*mret = NULL;
648 	++db->incomp_count;
649 	db->incomp_bytes += ilen;
650     } else {
651 	++db->comp_count;
652 	db->comp_bytes += olen + BSD_OVHD;
653     }
654 
655     return olen + PPP_HDRLEN + BSD_OVHD;
656 #undef OUTPUT
657 #undef PUTBYTE
658 }
659 
660 
661 /*
662  * Update the "BSD Compress" dictionary on the receiver for
663  * incompressible data by pretending to compress the incoming data.
664  */
665 static void
bsd_incomp(void * state,struct mbuf * dmsg)666 bsd_incomp(void *state, struct mbuf *dmsg)
667 {
668     struct bsd_db *db = (struct bsd_db *) state;
669     u_int hshift = db->hshift;
670     u_int max_ent = db->max_ent;
671     u_int n_bits = db->n_bits;
672     struct bsd_dict *dictp;
673     uint32_t fcode;
674     u_char c;
675     uint32_t hval, disp;
676     int slen, ilen;
677     u_int bitno = 7;
678     u_char *rptr;
679     u_int ent;
680 
681     /*
682      * If the protocol is not in the range we're interested in,
683      * just return without looking at the packet.  If it is,
684      * the protocol becomes the first byte to "compress".
685      */
686     rptr = mtod(dmsg, u_char *);
687     ent = PPP_PROTOCOL(rptr);
688     if (ent < 0x21 || ent > 0xf9)
689 	return;
690 
691     db->seqno++;
692     ilen = 1;		/* count the protocol as 1 byte */
693     rptr += PPP_HDRLEN;
694     slen = dmsg->m_len - PPP_HDRLEN;
695     for (;;) {
696 	if (slen <= 0) {
697 	    dmsg = dmsg->m_next;
698 	    if (!dmsg)
699 		break;
700 	    rptr = mtod(dmsg, u_char *);
701 	    slen = dmsg->m_len;
702 	    continue;
703 	}
704 	ilen += slen;
705 
706 	do {
707 	    c = *rptr++;
708 	    fcode = BSD_KEY(ent, c);
709 	    hval = BSD_HASH(ent, c, hshift);
710 	    dictp = &db->dict[hval];
711 
712 	    /* validate and then check the entry */
713 	    if (dictp->codem1 >= max_ent)
714 		goto nomatch;
715 	    if (dictp->f.fcode == fcode) {
716 		ent = dictp->codem1+1;
717 		continue;   /* found (prefix,suffix) */
718 	    }
719 
720 	    /* continue probing until a match or invalid entry */
721 	    disp = (hval == 0) ? 1 : hval;
722 	    do {
723 		hval += disp;
724 		if (hval >= db->hsize)
725 		    hval -= db->hsize;
726 		dictp = &db->dict[hval];
727 		if (dictp->codem1 >= max_ent)
728 		    goto nomatch;
729 	    } while (dictp->f.fcode != fcode);
730 	    ent = dictp->codem1+1;
731 	    continue;	/* finally found (prefix,suffix) */
732 
733 	nomatch:		/* output (count) the prefix */
734 	    bitno += n_bits;
735 
736 	    /* code -> hashtable */
737 	    if (max_ent < db->maxmaxcode) {
738 		struct bsd_dict *dictp2;
739 		/* expand code size if needed */
740 		if (max_ent >= MAXCODE(n_bits))
741 		    db->n_bits = ++n_bits;
742 
743 		/* Invalidate previous hash table entry
744 		 * assigned this code, and then take it over.
745 		 */
746 		dictp2 = &db->dict[max_ent+1];
747 		if (db->dict[dictp2->cptr].codem1 == max_ent)
748 		    db->dict[dictp2->cptr].codem1 = BADCODEM1;
749 		dictp2->cptr = hval;
750 		dictp->codem1 = max_ent;
751 		dictp->f.fcode = fcode;
752 
753 		db->max_ent = ++max_ent;
754 		db->lens[max_ent] = db->lens[ent]+1;
755 	    }
756 	    ent = c;
757 	} while (--slen != 0);
758     }
759     bitno += n_bits;		/* output (count) the last code */
760     db->bytes_out += bitno/8;
761     db->in_count += ilen;
762     (void)bsd_check(db);
763 
764     ++db->incomp_count;
765     db->incomp_bytes += ilen;
766     ++db->uncomp_count;
767     db->uncomp_bytes += ilen;
768 
769     /* Increase code size if we would have without the packet
770      * boundary and as the decompressor will.
771      */
772     if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
773 	db->n_bits++;
774 }
775 
776 
777 /*
778  * Decompress "BSD Compress".
779  *
780  * Because of patent problems, we return DECOMP_ERROR for errors
781  * found by inspecting the input data and for system problems, but
782  * DECOMP_FATALERROR for any errors which could possibly be said to
783  * be being detected "after" decompression.  For DECOMP_ERROR,
784  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
785  * infringing a patent of Motorola's if we do, so we take CCP down
786  * instead.
787  *
788  * Given that the frame has the correct sequence number and a good FCS,
789  * errors such as invalid codes in the input most likely indicate a
790  * bug, so we return DECOMP_FATALERROR for them in order to turn off
791  * compression, even though they are detected by inspecting the input.
792  */
793 int
bsd_decompress(void * state,struct mbuf * cmp,struct mbuf ** dmpp)794 bsd_decompress(void *state, struct mbuf *cmp, struct mbuf **dmpp)
795 {
796     struct bsd_db *db = (struct bsd_db *) state;
797     u_int max_ent = db->max_ent;
798     uint32_t accm = 0;
799     u_int bitno = 32;		/* 1st valid bit in accm */
800     u_int n_bits = db->n_bits;
801     u_int tgtbitno = 32-n_bits;	/* bitno when we have a code */
802     struct bsd_dict *dictp;
803     int explen, i, seq, len;
804     u_int incode, oldcode, finchar;
805     u_char *p, *rptr, *wptr;
806     struct mbuf *m, *dmp, *mret;
807     int adrs, ctrl, ilen;
808     int space, codelen, extra;
809 
810     /*
811      * Save the address/control from the PPP header
812      * and then get the sequence number.
813      */
814     *dmpp = NULL;
815     rptr = mtod(cmp, u_char *);
816     adrs = PPP_ADDRESS(rptr);
817     ctrl = PPP_CONTROL(rptr);
818     rptr += PPP_HDRLEN;
819     len = cmp->m_len - PPP_HDRLEN;
820     seq = 0;
821     for (i = 0; i < 2; ++i) {
822 	while (len <= 0) {
823 	    cmp = cmp->m_next;
824 	    if (cmp == NULL)
825 		return DECOMP_ERROR;
826 	    rptr = mtod(cmp, u_char *);
827 	    len = cmp->m_len;
828 	}
829 	seq = (seq << 8) + *rptr++;
830 	--len;
831     }
832 
833     /*
834      * Check the sequence number and give up if it differs from
835      * the value we're expecting.
836      */
837     if (seq != db->seqno) {
838 	if (db->debug)
839 	    printf("bsd_decomp%d: bad sequence # %d, expected %d\n",
840 		   db->unit, seq, db->seqno - 1);
841 	return DECOMP_ERROR;
842     }
843     ++db->seqno;
844 
845     /*
846      * Allocate one mbuf to start with.
847      */
848     MGETHDR(dmp, M_DONTWAIT, MT_DATA);
849     if (dmp == NULL)
850 	return DECOMP_ERROR;
851     mret = dmp;
852     dmp->m_len = 0;
853     dmp->m_next = NULL;
854     MCLGET(dmp, M_DONTWAIT);
855     dmp->m_data += db->hdrlen;
856     wptr = mtod(dmp, u_char *);
857     space = M_TRAILINGSPACE(dmp) - PPP_HDRLEN + 1;
858 
859     /*
860      * Fill in the ppp header, but not the last byte of the protocol
861      * (that comes from the decompressed data).
862      */
863     wptr[0] = adrs;
864     wptr[1] = ctrl;
865     wptr[2] = 0;
866     wptr += PPP_HDRLEN - 1;
867 
868     ilen = len;
869     oldcode = CLEAR;
870     explen = 0;
871     for (;;) {
872 	if (len == 0) {
873 	    cmp = cmp->m_next;
874 	    if (!cmp)		/* quit at end of message */
875 		break;
876 	    rptr = mtod(cmp, u_char *);
877 	    len = cmp->m_len;
878 	    ilen += len;
879 	    continue;		/* handle 0-length buffers */
880 	}
881 
882 	/*
883 	 * Accumulate bytes until we have a complete code.
884 	 * Then get the next code, relying on the 32-bit,
885 	 * unsigned accm to mask the result.
886 	 */
887 	bitno -= 8;
888 	accm |= *rptr++ << bitno;
889 	--len;
890 	if (tgtbitno < bitno)
891 	    continue;
892 	incode = accm >> tgtbitno;
893 	accm <<= n_bits;
894 	bitno += n_bits;
895 
896 	if (incode == CLEAR) {
897 	    /*
898 	     * The dictionary must only be cleared at
899 	     * the end of a packet.  But there could be an
900 	     * empty mbuf at the end.
901 	     */
902 	    if (len > 0 || cmp->m_next != NULL) {
903 		while ((cmp = cmp->m_next) != NULL)
904 		    len += cmp->m_len;
905 		if (len > 0) {
906 		    m_freem(mret);
907 		    if (db->debug)
908 			printf("bsd_decomp%d: bad CLEAR\n", db->unit);
909 		    return DECOMP_FATALERROR;	/* probably a bug */
910 		}
911 	    }
912 	    bsd_clear(db);
913 	    explen = ilen = 0;
914 	    break;
915 	}
916 
917 	if (incode > max_ent + 2 || incode > db->maxmaxcode
918 	    || (incode > max_ent && oldcode == CLEAR)) {
919 	    m_freem(mret);
920 	    if (db->debug) {
921 		printf("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
922 		       db->unit, incode, oldcode);
923 		printf("max_ent=0x%x explen=%d seqno=%d\n",
924 		       max_ent, explen, db->seqno);
925 	    }
926 	    return DECOMP_FATALERROR;	/* probably a bug */
927 	}
928 
929 	/* Special case for KwKwK string. */
930 	if (incode > max_ent) {
931 	    finchar = oldcode;
932 	    extra = 1;
933 	} else {
934 	    finchar = incode;
935 	    extra = 0;
936 	}
937 
938 	codelen = db->lens[finchar];
939 	explen += codelen + extra;
940 	if (explen > db->mru + 1) {
941 	    m_freem(mret);
942 	    if (db->debug) {
943 		printf("bsd_decomp%d: ran out of mru\n", db->unit);
944 #ifdef DEBUG
945 		while ((cmp = cmp->m_next) != NULL)
946 		    len += cmp->m_len;
947 		printf("  len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
948 		       len, finchar, codelen, explen);
949 #endif
950 	    }
951 	    return DECOMP_FATALERROR;
952 	}
953 
954 	/*
955 	 * For simplicity, the decoded characters go in a single mbuf,
956 	 * so we allocate a single extra cluster mbuf if necessary.
957 	 */
958 	if ((space -= codelen + extra) < 0) {
959 	    dmp->m_len = wptr - mtod(dmp, u_char *);
960 	    MGET(m, M_DONTWAIT, MT_DATA);
961 	    if (m == NULL) {
962 		m_freem(mret);
963 		return DECOMP_ERROR;
964 	    }
965 	    m->m_len = 0;
966 	    m->m_next = NULL;
967 	    dmp->m_next = m;
968 	    MCLGET(m, M_DONTWAIT);
969 	    space = M_TRAILINGSPACE(m) - (codelen + extra);
970 	    if (space < 0) {
971 		/* now that's what I call *compression*. */
972 		m_freem(mret);
973 		return DECOMP_ERROR;
974 	    }
975 	    dmp = m;
976 	    wptr = mtod(dmp, u_char *);
977 	}
978 
979 	/*
980 	 * Decode this code and install it in the decompressed buffer.
981 	 */
982 	p = (wptr += codelen);
983 	while (finchar > LAST) {
984 	    dictp = &db->dict[db->dict[finchar].cptr];
985 #ifdef DEBUG
986 	    if (--codelen <= 0 || dictp->codem1 != finchar-1)
987 		goto bad;
988 #endif
989 	    *--p = dictp->f.hs.suffix;
990 	    finchar = dictp->f.hs.prefix;
991 	}
992 	*--p = finchar;
993 
994 #ifdef DEBUG
995 	if (--codelen != 0)
996 	    printf("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
997 		   db->unit, codelen, incode, max_ent);
998 #endif
999 
1000 	if (extra)		/* the KwKwK case again */
1001 	    *wptr++ = finchar;
1002 
1003 	/*
1004 	 * If not first code in a packet, and
1005 	 * if not out of code space, then allocate a new code.
1006 	 *
1007 	 * Keep the hash table correct so it can be used
1008 	 * with uncompressed packets.
1009 	 */
1010 	if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
1011 	    struct bsd_dict *dictp2;
1012 	    uint32_t fcode;
1013 	    uint32_t hval, disp;
1014 
1015 	    fcode = BSD_KEY(oldcode,finchar);
1016 	    hval = BSD_HASH(oldcode,finchar,db->hshift);
1017 	    dictp = &db->dict[hval];
1018 
1019 	    /* look for a free hash table entry */
1020 	    if (dictp->codem1 < max_ent) {
1021 		disp = (hval == 0) ? 1 : hval;
1022 		do {
1023 		    hval += disp;
1024 		    if (hval >= db->hsize)
1025 			hval -= db->hsize;
1026 		    dictp = &db->dict[hval];
1027 		} while (dictp->codem1 < max_ent);
1028 	    }
1029 
1030 	    /*
1031 	     * Invalidate previous hash table entry
1032 	     * assigned this code, and then take it over
1033 	     */
1034 	    dictp2 = &db->dict[max_ent+1];
1035 	    if (db->dict[dictp2->cptr].codem1 == max_ent) {
1036 		db->dict[dictp2->cptr].codem1 = BADCODEM1;
1037 	    }
1038 	    dictp2->cptr = hval;
1039 	    dictp->codem1 = max_ent;
1040 	    dictp->f.fcode = fcode;
1041 
1042 	    db->max_ent = ++max_ent;
1043 	    db->lens[max_ent] = db->lens[oldcode]+1;
1044 
1045 	    /* Expand code size if needed. */
1046 	    if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
1047 		db->n_bits = ++n_bits;
1048 		tgtbitno = 32-n_bits;
1049 	    }
1050 	}
1051 	oldcode = incode;
1052     }
1053     dmp->m_len = wptr - mtod(dmp, u_char *);
1054 
1055     /*
1056      * Keep the checkpoint right so that incompressible packets
1057      * clear the dictionary at the right times.
1058      */
1059     db->bytes_out += ilen;
1060     db->in_count += explen;
1061     if (bsd_check(db) && db->debug) {
1062 	printf("bsd_decomp%d: peer should have cleared dictionary\n",
1063 	       db->unit);
1064     }
1065 
1066     ++db->comp_count;
1067     db->comp_bytes += ilen + BSD_OVHD;
1068     ++db->uncomp_count;
1069     db->uncomp_bytes += explen;
1070 
1071     *dmpp = mret;
1072     return DECOMP_OK;
1073 
1074 #ifdef DEBUG
1075  bad:
1076     if (codelen <= 0) {
1077 	printf("bsd_decomp%d: fell off end of chain ", db->unit);
1078 	printf("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1079 	       incode, finchar, db->dict[finchar].cptr, max_ent);
1080     } else if (dictp->codem1 != finchar-1) {
1081 	printf("bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",
1082 	       db->unit, incode, finchar);
1083 	printf("oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode,
1084 	       db->dict[finchar].cptr, dictp->codem1);
1085     }
1086     m_freem(mret);
1087     return DECOMP_FATALERROR;
1088 #endif /* DEBUG */
1089 }
1090 
1091 MODULE(MODULE_CLASS_MISC, ppp_bsdcomp, "if_ppp");
1092 
1093 static int
ppp_bsdcomp_modcmd(modcmd_t cmd,void * arg)1094 ppp_bsdcomp_modcmd(modcmd_t cmd, void *arg)
1095 {
1096 
1097 	switch (cmd) {
1098 	case MODULE_CMD_INIT:
1099 		return ppp_register_compressor(&ppp_bsd_compress, 1);
1100 	case MODULE_CMD_FINI:
1101 		return ppp_unregister_compressor(&ppp_bsd_compress, 1);
1102 	case MODULE_CMD_STAT:
1103 		return 0;
1104 	default:
1105 		return ENOTTY;
1106 	}
1107 
1108 	return ENOTTY;
1109 }
1110 #endif /* DO_BSD_COMPRESS */
1111