1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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>
69#include <linux/signal.h>
70#include <linux/bitops.h>
71
72#include <asm/system.h>
73#include <asm/byteorder.h>
74#include <asm/types.h>
75
76#include <linux/if.h>
77
78#include <linux/if_ether.h>
79#include <linux/netdevice.h>
80#include <linux/skbuff.h>
81#include <linux/inet.h>
82#include <linux/ioctl.h>
83#include <linux/vmalloc.h>
84
85#include <linux/ppp_defs.h>
86
87#include <linux/isdn.h>
88#include <linux/isdn_ppp.h>
89#include <linux/ip.h>
90#include <linux/tcp.h>
91#include <linux/if_arp.h>
92#include <linux/ppp-comp.h>
93
94#include "isdn_ppp.h"
95
96MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN");
97MODULE_LICENSE("Dual BSD/GPL");
98
99#define BSD_VERSION(x) ((x) >> 5)
100#define BSD_NBITS(x) ((x) & 0x1F)
101
102#define BSD_CURRENT_VERSION 1
103
104#define DEBUG 1
105
106
107
108
109
110struct bsd_dict {
111 u32 fcode;
112 u16 codem1;
113 u16 cptr;
114};
115
116struct bsd_db {
117 int totlen;
118 unsigned int hsize;
119 unsigned char hshift;
120 unsigned char n_bits;
121 unsigned char maxbits;
122 unsigned char debug;
123 unsigned char unit;
124 u16 seqno;
125 unsigned int mru;
126 unsigned int maxmaxcode;
127 unsigned int max_ent;
128 unsigned int in_count;
129 unsigned int bytes_out;
130 unsigned int ratio;
131 unsigned int checkpoint;
132 unsigned int clear_count;
133 unsigned int incomp_count;
134 unsigned int incomp_bytes;
135 unsigned int uncomp_count;
136 unsigned int uncomp_bytes;
137 unsigned int comp_count;
138 unsigned int comp_bytes;
139 unsigned short *lens;
140 struct bsd_dict *dict;
141 int xmit;
142};
143
144#define BSD_OVHD 2
145#define MIN_BSD_BITS 9
146#define BSD_INIT_BITS MIN_BSD_BITS
147#define MAX_BSD_BITS 15
148
149
150
151
152
153#define CLEAR 256
154#define FIRST 257
155#define LAST 255
156
157#define MAXCODE(b) ((1 << (b)) - 1)
158#define BADCODEM1 MAXCODE(MAX_BSD_BITS);
159
160#define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
161 ^ (unsigned long)(prefix))
162#define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \
163 + (unsigned long)(prefix))
164
165#define CHECK_GAP 10000
166
167#define RATIO_SCALE_LOG 8
168#define RATIO_SCALE (1<<RATIO_SCALE_LOG)
169#define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
170
171
172
173
174
175static void bsd_clear(struct bsd_db *db)
176{
177 db->clear_count++;
178 db->max_ent = FIRST-1;
179 db->n_bits = BSD_INIT_BITS;
180 db->bytes_out = 0;
181 db->in_count = 0;
182 db->incomp_count = 0;
183 db->ratio = 0;
184 db->checkpoint = CHECK_GAP;
185}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200static int bsd_check (struct bsd_db *db)
201{
202 unsigned int new_ratio;
203
204 if (db->in_count >= db->checkpoint)
205 {
206
207 if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
208 {
209 db->in_count -= (db->in_count >> 2);
210 db->bytes_out -= (db->bytes_out >> 2);
211 }
212
213 db->checkpoint = db->in_count + CHECK_GAP;
214
215 if (db->max_ent >= db->maxmaxcode)
216 {
217
218
219
220
221
222
223
224
225 new_ratio = db->in_count << RATIO_SCALE_LOG;
226 if (db->bytes_out != 0)
227 {
228 new_ratio /= db->bytes_out;
229 }
230
231 if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
232 {
233 bsd_clear (db);
234 return 1;
235 }
236 db->ratio = new_ratio;
237 }
238 }
239 return 0;
240}
241
242
243
244
245
246static void bsd_stats (void *state, struct compstat *stats)
247{
248 struct bsd_db *db = (struct bsd_db *) state;
249
250 stats->unc_bytes = db->uncomp_bytes;
251 stats->unc_packets = db->uncomp_count;
252 stats->comp_bytes = db->comp_bytes;
253 stats->comp_packets = db->comp_count;
254 stats->inc_bytes = db->incomp_bytes;
255 stats->inc_packets = db->incomp_count;
256 stats->in_count = db->in_count;
257 stats->bytes_out = db->bytes_out;
258}
259
260
261
262
263static void bsd_reset (void *state,unsigned char code, unsigned char id,
264 unsigned char *data, unsigned len,
265 struct isdn_ppp_resetparams *rsparm)
266{
267 struct bsd_db *db = (struct bsd_db *) state;
268
269 bsd_clear(db);
270 db->seqno = 0;
271 db->clear_count = 0;
272}
273
274
275
276
277static void bsd_free (void *state)
278{
279 struct bsd_db *db = (struct bsd_db *) state;
280
281 if (db) {
282
283
284
285 vfree(db->dict);
286 db->dict = NULL;
287
288
289
290
291 vfree(db->lens);
292 db->lens = NULL;
293
294
295
296
297 kfree(db);
298 }
299}
300
301
302
303
304
305static void *bsd_alloc (struct isdn_ppp_comp_data *data)
306{
307 int bits;
308 unsigned int hsize, hshift, maxmaxcode;
309 struct bsd_db *db;
310 int decomp;
311
312 static unsigned int htab[][2] = {
313 { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } ,
314 { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 }
315 };
316
317 if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
318 || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
319 return NULL;
320
321 bits = BSD_NBITS(data->options[0]);
322
323 if(bits < 9 || bits > 15)
324 return NULL;
325
326 hsize = htab[bits-9][0];
327 hshift = htab[bits-9][1];
328
329
330
331
332 maxmaxcode = MAXCODE(bits);
333 db = kzalloc (sizeof (struct bsd_db),GFP_KERNEL);
334 if (!db)
335 return NULL;
336
337 db->xmit = data->flags & IPPP_COMP_FLAG_XMIT;
338 decomp = db->xmit ? 0 : 1;
339
340
341
342
343
344 db->dict = vmalloc(hsize * sizeof(struct bsd_dict));
345 if (!db->dict) {
346 bsd_free (db);
347 return NULL;
348 }
349
350
351
352
353
354 if (!decomp)
355 db->lens = NULL;
356 else {
357 db->lens = vmalloc((maxmaxcode + 1) * sizeof(db->lens[0]));
358 if (!db->lens) {
359 bsd_free (db);
360 return (NULL);
361 }
362 }
363
364
365
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
378
379static 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
422
423
424#define dict_ptrx(p,idx) &(p->dict[idx])
425#define lens_ptrx(p,idx) &(p->lens[idx])
426
427#ifdef DEBUG
428static 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
437static 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
453
454static 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(skb_out,1)) = (unsigned char) (accm>>24); \
477 accm <<= 8; \
478 bitno += 8; \
479 } while (bitno <= 24); \
480 }
481
482
483
484
485
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
502 if(skb_out && skb_tailroom(skb_out) >= 2) {
503 char *v = skb_put(skb_out,2);
504
505
506 v[0] = db->seqno >> 8;
507 v[1] = db->seqno;
508 }
509
510 ilen = ++isize;
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
519 if (dictp->codem1 >= max_ent)
520 goto nomatch;
521
522 if (dictp->fcode == fcode) {
523 ent = dictp->codem1 + 1;
524 continue;
525 }
526
527
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;
540 continue;
541
542nomatch:
543 OUTPUT(ent);
544
545
546 if (max_ent < db->maxmaxcode) {
547 struct bsd_dict *dictp2;
548 struct bsd_dict *dictp3;
549 int indx;
550
551
552 if (max_ent >= mxcode) {
553 db->n_bits = ++n_bits;
554 mxcode = MAXCODE (n_bits);
555 }
556
557
558
559
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);
583
584 if(skb_out)
585 db->bytes_out += skb_out->len;
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;
593
594
595
596
597
598 if (bsd_check(db))
599 OUTPUT (CLEAR);
600
601
602
603
604
605 if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
606 *(skb_put(skb_out,1)) = (unsigned char) ((accm | (0xff << (bitno-8))) >> 24);
607
608
609
610
611
612 if (max_ent >= mxcode && max_ent < db->maxmaxcode)
613 db->n_bits++;
614
615
616 if (!skb_out || (skb_out && skb_out->len >= skb_in->len) ) {
617 ++db->incomp_count;
618 db->incomp_bytes += isize;
619 return 0;
620 }
621
622
623 ++db->comp_count;
624 db->comp_bytes += skb_out->len;
625 return skb_out->len;
626
627#undef OUTPUT
628}
629
630
631
632
633
634static void bsd_incomp (void *state, struct sk_buff *skb_in,int proto)
635{
636 bsd_compress (state, skb_in, NULL, proto);
637}
638
639
640
641
642static int bsd_decompress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,
643 struct isdn_ppp_resetparams *rsparm)
644{
645 struct bsd_db *db;
646 unsigned int max_ent;
647 unsigned long accm;
648 unsigned int bitno;
649 unsigned int n_bits;
650 unsigned int tgtbitno;
651 struct bsd_dict *dictp;
652 int seq;
653 unsigned int incode;
654 unsigned int oldcode;
655 unsigned int finchar;
656 unsigned char *p,*ibuf;
657 int ilen;
658 int codelen;
659 int extra;
660
661 db = (struct bsd_db *) state;
662 max_ent = db->max_ent;
663 accm = 0;
664 bitno = 32;
665 n_bits = db->n_bits;
666 tgtbitno = 32 - n_bits;
667
668 printk(KERN_DEBUG "bsd_decompress called\n");
669
670 if(!skb_in || !skb_out) {
671 printk(KERN_ERR "bsd_decompress called with NULL parameter\n");
672 return DECOMP_ERROR;
673 }
674
675
676
677
678 if( (p = skb_pull(skb_in,2)) == NULL) {
679 return DECOMP_ERROR;
680 }
681 p-=2;
682 seq = (p[0] << 8) + p[1];
683 ilen = skb_in->len;
684 ibuf = skb_in->data;
685
686
687
688
689
690 if (seq != db->seqno) {
691 if (db->debug) {
692 printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n",
693 db->unit, seq, db->seqno - 1);
694 }
695 return DECOMP_ERROR;
696 }
697
698 ++db->seqno;
699 db->bytes_out += ilen;
700
701 if(skb_tailroom(skb_out) > 0)
702 *(skb_put(skb_out,1)) = 0;
703 else
704 return DECOMP_ERR_NOMEM;
705
706 oldcode = CLEAR;
707
708
709
710
711
712
713 for (;;) {
714 if (ilen-- <= 0) {
715 db->in_count += (skb_out->len - 1);
716 break;
717 }
718
719
720
721
722
723
724
725 bitno -= 8;
726 accm |= *ibuf++ << bitno;
727 if (tgtbitno < bitno)
728 continue;
729
730 incode = accm >> tgtbitno;
731 accm <<= n_bits;
732 bitno += n_bits;
733
734
735
736
737
738 if (incode == CLEAR) {
739 if (ilen > 0) {
740 if (db->debug)
741 printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit);
742 return DECOMP_FATALERROR;
743 }
744 bsd_clear(db);
745 break;
746 }
747
748 if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
749 || (incode > max_ent && oldcode == CLEAR)) {
750 if (db->debug) {
751 printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
752 db->unit, incode, oldcode);
753 printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n",
754 max_ent, skb_out->len, db->seqno);
755 }
756 return DECOMP_FATALERROR;
757 }
758
759
760 if (incode > max_ent) {
761 finchar = oldcode;
762 extra = 1;
763 } else {
764 finchar = incode;
765 extra = 0;
766 }
767
768 codelen = *(lens_ptr (db, finchar));
769 if( skb_tailroom(skb_out) < codelen + extra) {
770 if (db->debug) {
771 printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit);
772#ifdef DEBUG
773 printk(KERN_DEBUG " len=%d, finchar=0x%x, codelen=%d,skblen=%d\n",
774 ilen, finchar, codelen, skb_out->len);
775#endif
776 }
777 return DECOMP_FATALERROR;
778 }
779
780
781
782
783
784 p = skb_put(skb_out,codelen);
785 p += codelen;
786 while (finchar > LAST) {
787 struct bsd_dict *dictp2 = dict_ptr (db, finchar);
788
789 dictp = dict_ptr (db, dictp2->cptr);
790
791#ifdef DEBUG
792 if (--codelen <= 0 || dictp->codem1 != finchar-1) {
793 if (codelen <= 0) {
794 printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit);
795 printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent);
796 } else {
797 if (dictp->codem1 != finchar-1) {
798 printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",db->unit, incode, finchar);
799 printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1);
800 }
801 }
802 return DECOMP_FATALERROR;
803 }
804#endif
805
806 {
807 u32 fcode = dictp->fcode;
808 *--p = (fcode >> 16) & 0xff;
809 finchar = fcode & 0xffff;
810 }
811 }
812 *--p = finchar;
813
814#ifdef DEBUG
815 if (--codelen != 0)
816 printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent);
817#endif
818
819 if (extra)
820 *(skb_put(skb_out,1)) = finchar;
821
822
823
824
825
826
827
828
829 if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
830 struct bsd_dict *dictp2, *dictp3;
831 u16 *lens1, *lens2;
832 unsigned long fcode;
833 int hval, disp, indx;
834
835 fcode = BSD_KEY(oldcode,finchar);
836 hval = BSD_HASH(oldcode,finchar,db->hshift);
837 dictp = dict_ptr (db, hval);
838
839
840 if (dictp->codem1 < max_ent) {
841 disp = (hval == 0) ? 1 : hval;
842 do {
843 hval += disp;
844 if (hval >= db->hsize)
845 hval -= db->hsize;
846 dictp = dict_ptr (db, hval);
847 } while (dictp->codem1 < max_ent);
848 }
849
850
851
852
853
854
855 dictp2 = dict_ptr (db, max_ent + 1);
856 indx = dictp2->cptr;
857 dictp3 = dict_ptr (db, indx);
858
859 if (dictp3->codem1 == max_ent)
860 dictp3->codem1 = BADCODEM1;
861
862 dictp2->cptr = hval;
863 dictp->codem1 = max_ent;
864 dictp->fcode = fcode;
865 db->max_ent = ++max_ent;
866
867
868 lens1 = lens_ptr (db, max_ent);
869 lens2 = lens_ptr (db, oldcode);
870 *lens1 = *lens2 + 1;
871
872
873 if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
874 db->n_bits = ++n_bits;
875 tgtbitno = 32-n_bits;
876 }
877 }
878 oldcode = incode;
879 }
880
881 ++db->comp_count;
882 ++db->uncomp_count;
883 db->comp_bytes += skb_in->len - BSD_OVHD;
884 db->uncomp_bytes += skb_out->len;
885
886 if (bsd_check(db)) {
887 if (db->debug)
888 printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n",
889 db->unit, db->seqno - 1);
890 }
891 return skb_out->len;
892}
893
894
895
896
897
898static struct isdn_ppp_compressor ippp_bsd_compress = {
899 .owner = THIS_MODULE,
900 .num = CI_BSD_COMPRESS,
901 .alloc = bsd_alloc,
902 .free = bsd_free,
903 .init = bsd_init,
904 .reset = bsd_reset,
905 .compress = bsd_compress,
906 .decompress = bsd_decompress,
907 .incomp = bsd_incomp,
908 .stat = bsd_stats,
909};
910
911
912
913
914
915static int __init isdn_bsdcomp_init(void)
916{
917 int answer = isdn_ppp_register_compressor (&ippp_bsd_compress);
918 if (answer == 0)
919 printk (KERN_INFO "PPP BSD Compression module registered\n");
920 return answer;
921}
922
923static void __exit isdn_bsdcomp_exit(void)
924{
925 isdn_ppp_unregister_compressor (&ippp_bsd_compress);
926}
927
928module_init(isdn_bsdcomp_init);
929module_exit(isdn_bsdcomp_exit);
930