1 /*
2  * BSD compression module
3  *
4  * Patched version for ISDN syncPPP written 1997/1998 by Michael Hipp
5  * The whole module is now SKB based.
6  *
7  */
8 
9 /*
10  * Update: The Berkeley copyright was changed, and the change
11  * is retroactive to all "true" BSD software (ie everything
12  * from UCB as opposed to other peoples code that just carried
13  * the same license). The new copyright doesn't clash with the
14  * GPL, so the module-only restriction has been removed..
15  */
16 
17 /*
18  * Original copyright notice:
19  *
20  * Copyright (c) 1985, 1986 The Regents of the University of California.
21  * All rights reserved.
22  *
23  * This code is derived from software contributed to Berkeley by
24  * James A. Woods, derived from original work by Spencer Thomas
25  * and Joseph Orost.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  * 1. Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in the
34  *    documentation and/or other materials provided with the distribution.
35  * 3. All advertising materials mentioning features or use of this software
36  *    must display the following acknowledgement:
37  *	This product includes software developed by the University of
38  *	California, Berkeley and its contributors.
39  * 4. Neither the name of the University nor the names of its contributors
40  *    may be used to endorse or promote products derived from this software
41  *    without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/kernel.h>
59 #include <linux/types.h>
60 #include <linux/fcntl.h>
61 #include <linux/interrupt.h>
62 #include <linux/ptrace.h>
63 #include <linux/ioport.h>
64 #include <linux/in.h>
65 #include <linux/slab.h>
66 #include <linux/tty.h>
67 #include <linux/errno.h>
68 #include <linux/string.h>	/* used in new tty drivers */
69 #include <linux/signal.h>	/* used in new tty drivers */
70 #include <linux/bitops.h>
71 
72 #include <asm/byteorder.h>
73 #include <asm/types.h>
74 
75 #include <linux/if.h>
76 
77 #include <linux/if_ether.h>
78 #include <linux/netdevice.h>
79 #include <linux/skbuff.h>
80 #include <linux/inet.h>
81 #include <linux/ioctl.h>
82 #include <linux/vmalloc.h>
83 
84 #include <linux/ppp_defs.h>
85 
86 #include <linux/isdn.h>
87 #include <linux/isdn_ppp.h>
88 #include <linux/ip.h>
89 #include <linux/tcp.h>
90 #include <linux/if_arp.h>
91 #include <linux/ppp-comp.h>
92 
93 #include "isdn_ppp.h"
94 
95 MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN");
96 MODULE_LICENSE("Dual BSD/GPL");
97 
98 #define BSD_VERSION(x)	((x) >> 5)
99 #define BSD_NBITS(x)	((x) & 0x1F)
100 
101 #define BSD_CURRENT_VERSION	1
102 
103 #define DEBUG 1
104 
105 /*
106  * A dictionary for doing BSD compress.
107  */
108 
109 struct bsd_dict {
110 	u32 fcode;
111 	u16 codem1;		/* output of hash table -1 */
112 	u16 cptr;		/* map code to hash table entry */
113 };
114 
115 struct bsd_db {
116 	int            totlen;		/* length of this structure */
117 	unsigned int   hsize;		/* size of the hash table */
118 	unsigned char  hshift;		/* used in hash function */
119 	unsigned char  n_bits;		/* current bits/code */
120 	unsigned char  maxbits;		/* maximum bits/code */
121 	unsigned char  debug;		/* non-zero if debug desired */
122 	unsigned char  unit;		/* ppp unit number */
123 	u16 seqno;			/* sequence # of next packet */
124 	unsigned int   mru;		/* size of receive (decompress) bufr */
125 	unsigned int   maxmaxcode;	/* largest valid code */
126 	unsigned int   max_ent;		/* largest code in use */
127 	unsigned int   in_count;	/* uncompressed bytes, aged */
128 	unsigned int   bytes_out;	/* compressed bytes, aged */
129 	unsigned int   ratio;		/* recent compression ratio */
130 	unsigned int   checkpoint;	/* when to next check the ratio */
131 	unsigned int   clear_count;	/* times dictionary cleared */
132 	unsigned int   incomp_count;	/* incompressible packets */
133 	unsigned int   incomp_bytes;	/* incompressible bytes */
134 	unsigned int   uncomp_count;	/* uncompressed packets */
135 	unsigned int   uncomp_bytes;	/* uncompressed bytes */
136 	unsigned int   comp_count;	/* compressed packets */
137 	unsigned int   comp_bytes;	/* compressed bytes */
138 	unsigned short  *lens;		/* array of lengths of codes */
139 	struct bsd_dict *dict;		/* dictionary */
140 	int xmit;
141 };
142 
143 #define BSD_OVHD	2		/* BSD compress overhead/packet */
144 #define MIN_BSD_BITS	9
145 #define BSD_INIT_BITS	MIN_BSD_BITS
146 #define MAX_BSD_BITS	15
147 
148 /*
149  * the next two codes should not be changed lightly, as they must not
150  * lie within the contiguous general code space.
151  */
152 #define CLEAR	256			/* table clear output code */
153 #define FIRST	257			/* first free entry */
154 #define LAST	255
155 
156 #define MAXCODE(b)	((1 << (b)) - 1)
157 #define BADCODEM1	MAXCODE(MAX_BSD_BITS)
158 
159 #define BSD_HASH(prefix, suffix, hshift) ((((unsigned long)(suffix)) << (hshift)) \
160 					  ^ (unsigned long)(prefix))
161 #define BSD_KEY(prefix, suffix)		((((unsigned long)(suffix)) << 16) \
162 					 + (unsigned long)(prefix))
163 
164 #define CHECK_GAP	10000		/* Ratio check interval */
165 
166 #define RATIO_SCALE_LOG	8
167 #define RATIO_SCALE	(1 << RATIO_SCALE_LOG)
168 #define RATIO_MAX	(0x7fffffff >> RATIO_SCALE_LOG)
169 
170 /*
171  * clear the dictionary
172  */
173 
bsd_clear(struct bsd_db * db)174 static void bsd_clear(struct bsd_db *db)
175 {
176 	db->clear_count++;
177 	db->max_ent      = FIRST - 1;
178 	db->n_bits       = BSD_INIT_BITS;
179 	db->bytes_out    = 0;
180 	db->in_count     = 0;
181 	db->incomp_count = 0;
182 	db->ratio	     = 0;
183 	db->checkpoint   = CHECK_GAP;
184 }
185 
186 /*
187  * If the dictionary is full, then see if it is time to reset it.
188  *
189  * Compute the compression ratio using fixed-point arithmetic
190  * with 8 fractional bits.
191  *
192  * Since we have an infinite stream instead of a single file,
193  * watch only the local compression ratio.
194  *
195  * Since both peers must reset the dictionary at the same time even in
196  * the absence of CLEAR codes (while packets are incompressible), they
197  * must compute the same ratio.
198  */
bsd_check(struct bsd_db * db)199 static int bsd_check(struct bsd_db *db)	/* 1=output CLEAR */
200 {
201 	unsigned int new_ratio;
202 
203 	if (db->in_count >= db->checkpoint)
204 	{
205 		/* age the ratio by limiting the size of the counts */
206 		if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
207 		{
208 			db->in_count  -= (db->in_count  >> 2);
209 			db->bytes_out -= (db->bytes_out >> 2);
210 		}
211 
212 		db->checkpoint = db->in_count + CHECK_GAP;
213 
214 		if (db->max_ent >= db->maxmaxcode)
215 		{
216 			/* Reset the dictionary only if the ratio is worse,
217 			 * or if it looks as if it has been poisoned
218 			 * by incompressible data.
219 			 *
220 			 * This does not overflow, because
221 			 *	db->in_count <= RATIO_MAX.
222 			 */
223 
224 			new_ratio = db->in_count << RATIO_SCALE_LOG;
225 			if (db->bytes_out != 0)
226 			{
227 				new_ratio /= db->bytes_out;
228 			}
229 
230 			if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
231 			{
232 				bsd_clear(db);
233 				return 1;
234 			}
235 			db->ratio = new_ratio;
236 		}
237 	}
238 	return 0;
239 }
240 
241 /*
242  * Return statistics.
243  */
244 
bsd_stats(void * state,struct compstat * stats)245 static void bsd_stats(void *state, struct compstat *stats)
246 {
247 	struct bsd_db *db = (struct bsd_db *) state;
248 
249 	stats->unc_bytes    = db->uncomp_bytes;
250 	stats->unc_packets  = db->uncomp_count;
251 	stats->comp_bytes   = db->comp_bytes;
252 	stats->comp_packets = db->comp_count;
253 	stats->inc_bytes    = db->incomp_bytes;
254 	stats->inc_packets  = db->incomp_count;
255 	stats->in_count     = db->in_count;
256 	stats->bytes_out    = db->bytes_out;
257 }
258 
259 /*
260  * Reset state, as on a CCP ResetReq.
261  */
bsd_reset(void * state,unsigned char code,unsigned char id,unsigned char * data,unsigned len,struct isdn_ppp_resetparams * rsparm)262 static void bsd_reset(void *state, unsigned char code, unsigned char id,
263 		      unsigned char *data, unsigned len,
264 		      struct isdn_ppp_resetparams *rsparm)
265 {
266 	struct bsd_db *db = (struct bsd_db *) state;
267 
268 	bsd_clear(db);
269 	db->seqno       = 0;
270 	db->clear_count = 0;
271 }
272 
273 /*
274  * Release the compression structure
275  */
bsd_free(void * state)276 static void bsd_free(void *state)
277 {
278 	struct bsd_db *db = (struct bsd_db *) state;
279 
280 	if (db) {
281 		/*
282 		 * Release the dictionary
283 		 */
284 		vfree(db->dict);
285 		db->dict = NULL;
286 
287 		/*
288 		 * Release the string buffer
289 		 */
290 		vfree(db->lens);
291 		db->lens = NULL;
292 
293 		/*
294 		 * Finally release the structure itself.
295 		 */
296 		kfree(db);
297 	}
298 }
299 
300 
301 /*
302  * Allocate space for a (de) compressor.
303  */
bsd_alloc(struct isdn_ppp_comp_data * data)304 static void *bsd_alloc(struct isdn_ppp_comp_data *data)
305 {
306 	int bits;
307 	unsigned int hsize, hshift, maxmaxcode;
308 	struct bsd_db *db;
309 	int decomp;
310 
311 	static unsigned int htab[][2] = {
312 		{ 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } ,
313 		{ 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 }
314 	};
315 
316 	if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
317 	    || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
318 		return NULL;
319 
320 	bits = BSD_NBITS(data->options[0]);
321 
322 	if (bits < 9 || bits > 15)
323 		return NULL;
324 
325 	hsize = htab[bits - 9][0];
326 	hshift = htab[bits - 9][1];
327 
328 	/*
329 	 * Allocate the main control structure for this instance.
330 	 */
331 	maxmaxcode = MAXCODE(bits);
332 	db = kzalloc(sizeof(struct bsd_db), GFP_KERNEL);
333 	if (!db)
334 		return NULL;
335 
336 	db->xmit = data->flags & IPPP_COMP_FLAG_XMIT;
337 	decomp = db->xmit ? 0 : 1;
338 
339 	/*
340 	 * Allocate space for the dictionary. This may be more than one page in
341 	 * length.
342 	 */
343 	db->dict = vmalloc(array_size(hsize, sizeof(struct bsd_dict)));
344 	if (!db->dict) {
345 		bsd_free(db);
346 		return NULL;
347 	}
348 
349 	/*
350 	 * If this is the compression buffer then there is no length data.
351 	 * For decompression, the length information is needed as well.
352 	 */
353 	if (!decomp)
354 		db->lens = NULL;
355 	else {
356 		db->lens = vmalloc(array_size(sizeof(db->lens[0]),
357 					      maxmaxcode + 1));
358 		if (!db->lens) {
359 			bsd_free(db);
360 			return (NULL);
361 		}
362 	}
363 
364 	/*
365 	 * Initialize the data information for the compression code
366 	 */
367 	db->totlen = sizeof(struct bsd_db) + (sizeof(struct bsd_dict) * hsize);
368 	db->hsize = hsize;
369 	db->hshift = hshift;
370 	db->maxmaxcode = maxmaxcode;
371 	db->maxbits = bits;
372 
373 	return (void *)db;
374 }
375 
376 /*
377  * Initialize the database.
378  */
bsd_init(void * state,struct isdn_ppp_comp_data * data,int unit,int debug)379 static int bsd_init(void *state, struct isdn_ppp_comp_data *data, int unit, int debug)
380 {
381 	struct bsd_db *db = state;
382 	int indx;
383 	int decomp;
384 
385 	if (!state || !data) {
386 		printk(KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n", unit, (long)state, (long)data);
387 		return 0;
388 	}
389 
390 	decomp = db->xmit ? 0 : 1;
391 
392 	if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
393 	    || (BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
394 	    || (BSD_NBITS(data->options[0]) != db->maxbits)
395 	    || (decomp && db->lens == NULL)) {
396 		printk(KERN_ERR "isdn_bsd: %d %d %d %d %lx\n", data->optlen, data->num, data->options[0], decomp, (unsigned long)db->lens);
397 		return 0;
398 	}
399 
400 	if (decomp)
401 		for (indx = LAST; indx >= 0; indx--)
402 			db->lens[indx] = 1;
403 
404 	indx = db->hsize;
405 	while (indx-- != 0) {
406 		db->dict[indx].codem1 = BADCODEM1;
407 		db->dict[indx].cptr   = 0;
408 	}
409 
410 	db->unit = unit;
411 	db->mru  = 0;
412 
413 	db->debug = 1;
414 
415 	bsd_reset(db, 0, 0, NULL, 0, NULL);
416 
417 	return 1;
418 }
419 
420 /*
421  * Obtain pointers to the various structures in the compression tables
422  */
423 
424 #define dict_ptrx(p, idx) &(p->dict[idx])
425 #define lens_ptrx(p, idx) &(p->lens[idx])
426 
427 #ifdef DEBUG
lens_ptr(struct bsd_db * db,int idx)428 static unsigned short *lens_ptr(struct bsd_db *db, int idx)
429 {
430 	if ((unsigned int) idx > (unsigned int) db->maxmaxcode) {
431 		printk(KERN_DEBUG "<9>ppp: lens_ptr(%d) > max\n", idx);
432 		idx = 0;
433 	}
434 	return lens_ptrx(db, idx);
435 }
436 
dict_ptr(struct bsd_db * db,int idx)437 static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx)
438 {
439 	if ((unsigned int) idx >= (unsigned int) db->hsize) {
440 		printk(KERN_DEBUG "<9>ppp: dict_ptr(%d) > max\n", idx);
441 		idx = 0;
442 	}
443 	return dict_ptrx(db, idx);
444 }
445 
446 #else
447 #define lens_ptr(db, idx) lens_ptrx(db, idx)
448 #define dict_ptr(db, idx) dict_ptrx(db, idx)
449 #endif
450 
451 /*
452  * compress a packet
453  */
bsd_compress(void * state,struct sk_buff * skb_in,struct sk_buff * skb_out,int proto)454 static int bsd_compress(void *state, struct sk_buff *skb_in, struct sk_buff *skb_out, int proto)
455 {
456 	struct bsd_db *db;
457 	int hshift;
458 	unsigned int max_ent;
459 	unsigned int n_bits;
460 	unsigned int bitno;
461 	unsigned long accm;
462 	int ent;
463 	unsigned long fcode;
464 	struct bsd_dict *dictp;
465 	unsigned char c;
466 	int hval, disp, ilen, mxcode;
467 	unsigned char *rptr = skb_in->data;
468 	int isize = skb_in->len;
469 
470 #define OUTPUT(ent)							\
471 	{								\
472 		bitno -= n_bits;					\
473 		accm |= ((ent) << bitno);				\
474 		do	{						\
475 			if (skb_out && skb_tailroom(skb_out) > 0)	\
476 				skb_put_u8(skb_out, (u8)(accm >> 24));	\
477 			accm <<= 8;					\
478 			bitno += 8;					\
479 		} while (bitno <= 24);					\
480 	}
481 
482 	/*
483 	 * If the protocol is not in the range we're interested in,
484 	 * just return without compressing the packet.  If it is,
485 	 * the protocol becomes the first byte to compress.
486 	 */
487 	printk(KERN_DEBUG "bsd_compress called with %x\n", proto);
488 
489 	ent = proto;
490 	if (proto < 0x21 || proto > 0xf9 || !(proto & 0x1))
491 		return 0;
492 
493 	db      = (struct bsd_db *) state;
494 	hshift  = db->hshift;
495 	max_ent = db->max_ent;
496 	n_bits  = db->n_bits;
497 	bitno   = 32;
498 	accm    = 0;
499 	mxcode  = MAXCODE(n_bits);
500 
501 	/* This is the PPP header information */
502 	if (skb_out && skb_tailroom(skb_out) >= 2) {
503 		char *v = skb_put(skb_out, 2);
504 		/* we only push our own data on the header,
505 		   AC,PC and protos is pushed by caller  */
506 		v[0] = db->seqno >> 8;
507 		v[1] = db->seqno;
508 	}
509 
510 	ilen = ++isize; /* This is off by one, but that is what is in draft! */
511 
512 	while (--ilen > 0) {
513 		c = *rptr++;
514 		fcode = BSD_KEY(ent, c);
515 		hval = BSD_HASH(ent, c, hshift);
516 		dictp = dict_ptr(db, hval);
517 
518 		/* Validate and then check the entry. */
519 		if (dictp->codem1 >= max_ent)
520 			goto nomatch;
521 
522 		if (dictp->fcode == fcode) {
523 			ent = dictp->codem1 + 1;
524 			continue;	/* found (prefix,suffix) */
525 		}
526 
527 		/* continue probing until a match or invalid entry */
528 		disp = (hval == 0) ? 1 : hval;
529 
530 		do {
531 			hval += disp;
532 			if (hval >= db->hsize)
533 				hval -= db->hsize;
534 			dictp = dict_ptr(db, hval);
535 			if (dictp->codem1 >= max_ent)
536 				goto nomatch;
537 		} while (dictp->fcode != fcode);
538 
539 		ent = dictp->codem1 + 1;	/* finally found (prefix,suffix) */
540 		continue;
541 
542 	nomatch:
543 		OUTPUT(ent);		/* output the prefix */
544 
545 		/* code -> hashtable */
546 		if (max_ent < db->maxmaxcode) {
547 			struct bsd_dict *dictp2;
548 			struct bsd_dict *dictp3;
549 			int indx;
550 
551 			/* expand code size if needed */
552 			if (max_ent >= mxcode) {
553 				db->n_bits = ++n_bits;
554 				mxcode = MAXCODE(n_bits);
555 			}
556 
557 			/*
558 			 * Invalidate old hash table entry using
559 			 * this code, and then take it over.
560 			 */
561 			dictp2 = dict_ptr(db, max_ent + 1);
562 			indx   = dictp2->cptr;
563 			dictp3 = dict_ptr(db, indx);
564 
565 			if (dictp3->codem1 == max_ent)
566 				dictp3->codem1 = BADCODEM1;
567 
568 			dictp2->cptr   = hval;
569 			dictp->codem1  = max_ent;
570 			dictp->fcode = fcode;
571 			db->max_ent    = ++max_ent;
572 
573 			if (db->lens) {
574 				unsigned short *len1 = lens_ptr(db, max_ent);
575 				unsigned short *len2 = lens_ptr(db, ent);
576 				*len1 = *len2 + 1;
577 			}
578 		}
579 		ent = c;
580 	}
581 
582 	OUTPUT(ent);		/* output the last code */
583 
584 	if (skb_out)
585 		db->bytes_out    += skb_out->len; /* Do not count bytes from here */
586 	db->uncomp_bytes += isize;
587 	db->in_count     += isize;
588 	++db->uncomp_count;
589 	++db->seqno;
590 
591 	if (bitno < 32)
592 		++db->bytes_out; /* must be set before calling bsd_check */
593 
594 	/*
595 	 * Generate the clear command if needed
596 	 */
597 
598 	if (bsd_check(db))
599 		OUTPUT(CLEAR);
600 
601 	/*
602 	 * Pad dribble bits of last code with ones.
603 	 * Do not emit a completely useless byte of ones.
604 	 */
605 	if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
606 		skb_put_u8(skb_out,
607 			   (unsigned char)((accm | (0xff << (bitno - 8))) >> 24));
608 
609 	/*
610 	 * Increase code size if we would have without the packet
611 	 * boundary because the decompressor will do so.
612 	 */
613 	if (max_ent >= mxcode && max_ent < db->maxmaxcode)
614 		db->n_bits++;
615 
616 	/* If output length is too large then this is an incompressible frame. */
617 	if (!skb_out || skb_out->len >= skb_in->len) {
618 		++db->incomp_count;
619 		db->incomp_bytes += isize;
620 		return 0;
621 	}
622 
623 	/* Count the number of compressed frames */
624 	++db->comp_count;
625 	db->comp_bytes += skb_out->len;
626 	return skb_out->len;
627 
628 #undef OUTPUT
629 }
630 
631 /*
632  * Update the "BSD Compress" dictionary on the receiver for
633  * incompressible data by pretending to compress the incoming data.
634  */
bsd_incomp(void * state,struct sk_buff * skb_in,int proto)635 static void bsd_incomp(void *state, struct sk_buff *skb_in, int proto)
636 {
637 	bsd_compress(state, skb_in, NULL, proto);
638 }
639 
640 /*
641  * Decompress "BSD Compress".
642  */
bsd_decompress(void * state,struct sk_buff * skb_in,struct sk_buff * skb_out,struct isdn_ppp_resetparams * rsparm)643 static int bsd_decompress(void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,
644 			  struct isdn_ppp_resetparams *rsparm)
645 {
646 	struct bsd_db *db;
647 	unsigned int max_ent;
648 	unsigned long accm;
649 	unsigned int bitno;		/* 1st valid bit in accm */
650 	unsigned int n_bits;
651 	unsigned int tgtbitno;	/* bitno when we have a code */
652 	struct bsd_dict *dictp;
653 	int seq;
654 	unsigned int incode;
655 	unsigned int oldcode;
656 	unsigned int finchar;
657 	unsigned char *p, *ibuf;
658 	int ilen;
659 	int codelen;
660 	int extra;
661 
662 	db       = (struct bsd_db *) state;
663 	max_ent  = db->max_ent;
664 	accm     = 0;
665 	bitno    = 32;		/* 1st valid bit in accm */
666 	n_bits   = db->n_bits;
667 	tgtbitno = 32 - n_bits;	/* bitno when we have a code */
668 
669 	printk(KERN_DEBUG "bsd_decompress called\n");
670 
671 	if (!skb_in || !skb_out) {
672 		printk(KERN_ERR "bsd_decompress called with NULL parameter\n");
673 		return DECOMP_ERROR;
674 	}
675 
676 	/*
677 	 * Get the sequence number.
678 	 */
679 	if ((p = skb_pull(skb_in, 2)) == NULL) {
680 		return DECOMP_ERROR;
681 	}
682 	p -= 2;
683 	seq = (p[0] << 8) + p[1];
684 	ilen = skb_in->len;
685 	ibuf = skb_in->data;
686 
687 	/*
688 	 * Check the sequence number and give up if it differs from
689 	 * the value we're expecting.
690 	 */
691 	if (seq != db->seqno) {
692 		if (db->debug) {
693 			printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n",
694 			       db->unit, seq, db->seqno - 1);
695 		}
696 		return DECOMP_ERROR;
697 	}
698 
699 	++db->seqno;
700 	db->bytes_out += ilen;
701 
702 	if (skb_tailroom(skb_out) > 0)
703 		skb_put_u8(skb_out, 0);
704 	else
705 		return DECOMP_ERR_NOMEM;
706 
707 	oldcode = CLEAR;
708 
709 	/*
710 	 * Keep the checkpoint correctly so that incompressible packets
711 	 * clear the dictionary at the proper times.
712 	 */
713 
714 	for (;;) {
715 		if (ilen-- <= 0) {
716 			db->in_count += (skb_out->len - 1); /* don't count the header */
717 			break;
718 		}
719 
720 		/*
721 		 * Accumulate bytes until we have a complete code.
722 		 * Then get the next code, relying on the 32-bit,
723 		 * unsigned accm to mask the result.
724 		 */
725 
726 		bitno -= 8;
727 		accm  |= *ibuf++ << bitno;
728 		if (tgtbitno < bitno)
729 			continue;
730 
731 		incode = accm >> tgtbitno;
732 		accm <<= n_bits;
733 		bitno += n_bits;
734 
735 		/*
736 		 * The dictionary must only be cleared at the end of a packet.
737 		 */
738 
739 		if (incode == CLEAR) {
740 			if (ilen > 0) {
741 				if (db->debug)
742 					printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit);
743 				return DECOMP_FATALERROR;	/* probably a bug */
744 			}
745 			bsd_clear(db);
746 			break;
747 		}
748 
749 		if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
750 		    || (incode > max_ent && oldcode == CLEAR)) {
751 			if (db->debug) {
752 				printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
753 				       db->unit, incode, oldcode);
754 				printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n",
755 				       max_ent, skb_out->len, db->seqno);
756 			}
757 			return DECOMP_FATALERROR;	/* probably a bug */
758 		}
759 
760 		/* Special case for KwKwK string. */
761 		if (incode > max_ent) {
762 			finchar = oldcode;
763 			extra   = 1;
764 		} else {
765 			finchar = incode;
766 			extra   = 0;
767 		}
768 
769 		codelen = *(lens_ptr(db, finchar));
770 		if (skb_tailroom(skb_out) < codelen + extra) {
771 			if (db->debug) {
772 				printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit);
773 #ifdef DEBUG
774 				printk(KERN_DEBUG "  len=%d, finchar=0x%x, codelen=%d,skblen=%d\n",
775 				       ilen, finchar, codelen, skb_out->len);
776 #endif
777 			}
778 			return DECOMP_FATALERROR;
779 		}
780 
781 		/*
782 		 * Decode this code and install it in the decompressed buffer.
783 		 */
784 
785 		p = skb_put(skb_out, codelen);
786 		p += codelen;
787 		while (finchar > LAST) {
788 			struct bsd_dict *dictp2 = dict_ptr(db, finchar);
789 
790 			dictp = dict_ptr(db, dictp2->cptr);
791 
792 #ifdef DEBUG
793 			if (--codelen <= 0 || dictp->codem1 != finchar - 1) {
794 				if (codelen <= 0) {
795 					printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit);
796 					printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent);
797 				} else {
798 					if (dictp->codem1 != finchar - 1) {
799 						printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ", db->unit, incode, finchar);
800 						printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1);
801 					}
802 				}
803 				return DECOMP_FATALERROR;
804 			}
805 #endif
806 
807 			{
808 				u32 fcode = dictp->fcode;
809 				*--p    = (fcode >> 16) & 0xff;
810 				finchar = fcode & 0xffff;
811 			}
812 		}
813 		*--p = finchar;
814 
815 #ifdef DEBUG
816 		if (--codelen != 0)
817 			printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent);
818 #endif
819 
820 		if (extra)		/* the KwKwK case again */
821 			skb_put_u8(skb_out, finchar);
822 
823 		/*
824 		 * If not first code in a packet, and
825 		 * if not out of code space, then allocate a new code.
826 		 *
827 		 * Keep the hash table correct so it can be used
828 		 * with uncompressed packets.
829 		 */
830 		if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
831 			struct bsd_dict *dictp2, *dictp3;
832 			u16 *lens1, *lens2;
833 			unsigned long fcode;
834 			int hval, disp, indx;
835 
836 			fcode = BSD_KEY(oldcode, finchar);
837 			hval  = BSD_HASH(oldcode, finchar, db->hshift);
838 			dictp = dict_ptr(db, hval);
839 
840 			/* look for a free hash table entry */
841 			if (dictp->codem1 < max_ent) {
842 				disp = (hval == 0) ? 1 : hval;
843 				do {
844 					hval += disp;
845 					if (hval >= db->hsize)
846 						hval -= db->hsize;
847 					dictp = dict_ptr(db, hval);
848 				} while (dictp->codem1 < max_ent);
849 			}
850 
851 			/*
852 			 * Invalidate previous hash table entry
853 			 * assigned this code, and then take it over
854 			 */
855 
856 			dictp2 = dict_ptr(db, max_ent + 1);
857 			indx   = dictp2->cptr;
858 			dictp3 = dict_ptr(db, indx);
859 
860 			if (dictp3->codem1 == max_ent)
861 				dictp3->codem1 = BADCODEM1;
862 
863 			dictp2->cptr   = hval;
864 			dictp->codem1  = max_ent;
865 			dictp->fcode = fcode;
866 			db->max_ent    = ++max_ent;
867 
868 			/* Update the length of this string. */
869 			lens1  = lens_ptr(db, max_ent);
870 			lens2  = lens_ptr(db, oldcode);
871 			*lens1 = *lens2 + 1;
872 
873 			/* Expand code size if needed. */
874 			if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
875 				db->n_bits = ++n_bits;
876 				tgtbitno   = 32-n_bits;
877 			}
878 		}
879 		oldcode = incode;
880 	}
881 
882 	++db->comp_count;
883 	++db->uncomp_count;
884 	db->comp_bytes   += skb_in->len - BSD_OVHD;
885 	db->uncomp_bytes += skb_out->len;
886 
887 	if (bsd_check(db)) {
888 		if (db->debug)
889 			printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n",
890 			       db->unit, db->seqno - 1);
891 	}
892 	return skb_out->len;
893 }
894 
895 /*************************************************************
896  * Table of addresses for the BSD compression module
897  *************************************************************/
898 
899 static struct isdn_ppp_compressor ippp_bsd_compress = {
900 	.owner          = THIS_MODULE,
901 	.num            = CI_BSD_COMPRESS,
902 	.alloc          = bsd_alloc,
903 	.free           = bsd_free,
904 	.init           = bsd_init,
905 	.reset          = bsd_reset,
906 	.compress       = bsd_compress,
907 	.decompress     = bsd_decompress,
908 	.incomp         = bsd_incomp,
909 	.stat           = bsd_stats,
910 };
911 
912 /*************************************************************
913  * Module support routines
914  *************************************************************/
915 
isdn_bsdcomp_init(void)916 static int __init isdn_bsdcomp_init(void)
917 {
918 	int answer = isdn_ppp_register_compressor(&ippp_bsd_compress);
919 	if (answer == 0)
920 		printk(KERN_INFO "PPP BSD Compression module registered\n");
921 	return answer;
922 }
923 
isdn_bsdcomp_exit(void)924 static void __exit isdn_bsdcomp_exit(void)
925 {
926 	isdn_ppp_unregister_compressor(&ippp_bsd_compress);
927 }
928 
929 module_init(isdn_bsdcomp_init);
930 module_exit(isdn_bsdcomp_exit);
931