File: /usr/src/linux/drivers/isdn/isdn_bsdcomp.c

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