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