This source file includes following definitions.
- slhc_init
- slhc_free
- put16
- encode
- pull16
- decode
- slhc_compress
- slhc_uncompress
- slhc_remember
- slhc_toss
- slhc_i_status
- slhc_o_status
- export_slhc_syms
- init_module
- cleanup_module
- slhc_install
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 #include <linux/config.h>
54 #ifdef CONFIG_INET
55
56 #include <linux/module.h>
57
58 #include <linux/types.h>
59 #include <linux/sched.h>
60 #include <linux/mm.h>
61 #include <linux/string.h>
62 #include <linux/socket.h>
63 #include <linux/sockios.h>
64 #include <linux/termios.h>
65 #include <linux/in.h>
66 #include <linux/fcntl.h>
67 #include <linux/inet.h>
68 #include <linux/netdevice.h>
69 #include <net/ip.h>
70 #include <net/protocol.h>
71 #include <net/icmp.h>
72 #include <net/tcp.h>
73 #include <linux/skbuff.h>
74 #include <net/sock.h>
75 #include <linux/errno.h>
76 #include <linux/timer.h>
77 #include <asm/system.h>
78 #include <asm/segment.h>
79 #include <linux/mm.h>
80 #include <net/checksum.h>
81 #include "slhc.h"
82
83 #ifdef __alpha__
84 # include <asm/unaligned.h>
85 #endif
86
87 int last_retran;
88
89 static unsigned char *encode(unsigned char *cp, unsigned short n);
90 static long decode(unsigned char **cpp);
91 static unsigned char * put16(unsigned char *cp, unsigned short x);
92 static unsigned short pull16(unsigned char **cpp);
93 static void export_slhc_syms(void);
94
95
96
97
98 struct slcompress *
99 slhc_init(int rslots, int tslots)
100 {
101 register short i;
102 register struct cstate *ts;
103 struct slcompress *comp;
104
105 comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
106 GFP_KERNEL);
107 if (! comp)
108 return NULL;
109
110 memset(comp, 0, sizeof(struct slcompress));
111
112 if ( rslots > 0 && rslots < 256 ) {
113 comp->rstate =
114 (struct cstate *)kmalloc(rslots * sizeof(struct cstate),
115 GFP_KERNEL);
116 if (! comp->rstate)
117 {
118 kfree((unsigned char *)comp);
119 return NULL;
120 }
121 memset(comp->rstate, 0, rslots * sizeof(struct cstate));
122 comp->rslot_limit = rslots - 1;
123 }
124
125 if ( tslots > 0 && tslots < 256 ) {
126 comp->tstate =
127 (struct cstate *)kmalloc(tslots * sizeof(struct cstate),
128 GFP_KERNEL);
129 if (! comp->tstate)
130 {
131 kfree((unsigned char *)comp->rstate);
132 kfree((unsigned char *)comp);
133 return NULL;
134 }
135 memset(comp->tstate, 0, tslots * sizeof(struct cstate));
136 comp->tslot_limit = tslots - 1;
137 }
138
139 comp->xmit_oldest = 0;
140 comp->xmit_current = 255;
141 comp->recv_current = 255;
142
143
144
145
146
147
148 comp->flags |= SLF_TOSS;
149
150 if ( tslots > 0 ) {
151 ts = comp->tstate;
152 for(i = comp->tslot_limit; i > 0; --i){
153 ts[i].cs_this = i;
154 ts[i].next = &(ts[i - 1]);
155 }
156 ts[0].next = &(ts[comp->tslot_limit]);
157 ts[0].cs_this = 0;
158 }
159 MOD_INC_USE_COUNT;
160 return comp;
161 }
162
163
164
165 void
166 slhc_free(struct slcompress *comp)
167 {
168 if ( comp == NULLSLCOMPR )
169 return;
170
171 if ( comp->rstate != NULLSLSTATE )
172 kfree( comp->rstate );
173
174 if ( comp->tstate != NULLSLSTATE )
175 kfree( comp->tstate );
176
177 MOD_DEC_USE_COUNT;
178 kfree( comp );
179 }
180
181
182
183 static inline unsigned char *
184 put16(unsigned char *cp, unsigned short x)
185 {
186 *cp++ = x >> 8;
187 *cp++ = x;
188
189 return cp;
190 }
191
192
193
194 unsigned char *
195 encode(unsigned char *cp, unsigned short n)
196 {
197 if(n >= 256 || n == 0){
198 *cp++ = 0;
199 cp = put16(cp,n);
200 } else {
201 *cp++ = n;
202 }
203 return cp;
204 }
205
206
207 static unsigned short
208 pull16(unsigned char **cpp)
209 {
210 short rval;
211
212 rval = *(*cpp)++;
213 rval <<= 8;
214 rval |= *(*cpp)++;
215 return rval;
216 }
217
218
219 long
220 decode(unsigned char **cpp)
221 {
222 register int x;
223
224 x = *(*cpp)++;
225 if(x == 0){
226 return pull16(cpp) & 0xffff;
227 } else {
228 return x & 0xff;
229 }
230 }
231
232
233
234
235
236
237
238
239 int
240 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
241 unsigned char *ocp, unsigned char **cpp, int compress_cid)
242 {
243 register struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
244 register struct cstate *lcs = ocs;
245 register struct cstate *cs = lcs->next;
246 register unsigned long deltaS, deltaA;
247 register short changes = 0;
248 int hlen;
249 unsigned char new_seq[16];
250 register unsigned char *cp = new_seq;
251 struct iphdr *ip;
252 struct tcphdr *th, *oth;
253
254 ip = (struct iphdr *) icp;
255
256
257 if(ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x1fff) ||
258 (ip->frag_off & 32)){
259
260 if(ip->protocol != IPPROTO_TCP)
261 comp->sls_o_nontcp++;
262 else
263 comp->sls_o_tcp++;
264 return isize;
265 }
266
267
268 th = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
269 hlen = ip->ihl*4 + th->doff*4;
270
271
272
273
274 if(th->syn || th->fin || th->rst ||
275 ! (th->ack)){
276
277 comp->sls_o_tcp++;
278 return isize;
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 for ( ; ; ) {
295 if( ip->saddr == cs->cs_ip.saddr
296 && ip->daddr == cs->cs_ip.daddr
297 && th->source == cs->cs_tcp.source
298 && th->dest == cs->cs_tcp.dest)
299 goto found;
300
301
302 if ( cs == ocs )
303 break;
304 lcs = cs;
305 cs = cs->next;
306 comp->sls_o_searches++;
307 };
308
309
310
311
312
313
314
315
316
317 comp->sls_o_misses++;
318 comp->xmit_oldest = lcs->cs_this;
319 goto uncompressed;
320
321 found:
322
323
324
325 if(lcs == ocs) {
326
327 } else if (cs == ocs) {
328
329 comp->xmit_oldest = lcs->cs_this;
330 } else {
331
332 lcs->next = cs->next;
333 cs->next = ocs->next;
334 ocs->next = cs;
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348
349 oth = &cs->cs_tcp;
350
351 if(last_retran
352 || ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
353 || ip->tos != cs->cs_ip.tos
354 || (ip->frag_off & 64) != (cs->cs_ip.frag_off & 64)
355 || ip->ttl != cs->cs_ip.ttl
356 || th->doff != cs->cs_tcp.doff
357 || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
358 || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4) != 0)){
359 goto uncompressed;
360 }
361
362
363
364
365
366
367
368 if(th->urg){
369 deltaS = ntohs(th->urg_ptr);
370 cp = encode(cp,deltaS);
371 changes |= NEW_U;
372 } else if(th->urg_ptr != oth->urg_ptr){
373
374
375
376
377 goto uncompressed;
378 }
379 if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
380 cp = encode(cp,deltaS);
381 changes |= NEW_W;
382 }
383 if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
384 if(deltaA > 0x0000ffff)
385 goto uncompressed;
386 cp = encode(cp,deltaA);
387 changes |= NEW_A;
388 }
389 if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
390 if(deltaS > 0x0000ffff)
391 goto uncompressed;
392 cp = encode(cp,deltaS);
393 changes |= NEW_S;
394 }
395
396 switch(changes){
397 case 0:
398
399
400
401
402
403
404 if(ip->tot_len != cs->cs_ip.tot_len &&
405 ntohs(cs->cs_ip.tot_len) == hlen)
406 break;
407 goto uncompressed;
408 break;
409 case SPECIAL_I:
410 case SPECIAL_D:
411
412
413
414 goto uncompressed;
415 case NEW_S|NEW_A:
416 if(deltaS == deltaA &&
417 deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
418
419 changes = SPECIAL_I;
420 cp = new_seq;
421 }
422 break;
423 case NEW_S:
424 if(deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
425
426 changes = SPECIAL_D;
427 cp = new_seq;
428 }
429 break;
430 }
431 deltaS = ntohs(ip->id) - ntohs(cs->cs_ip.id);
432 if(deltaS != 1){
433 cp = encode(cp,deltaS);
434 changes |= NEW_I;
435 }
436 if(th->psh)
437 changes |= TCP_PUSH_BIT;
438
439
440
441 deltaA = ntohs(th->check);
442 memcpy(&cs->cs_ip,ip,20);
443 memcpy(&cs->cs_tcp,th,20);
444
445
446
447
448
449
450 deltaS = cp - new_seq;
451 if(compress_cid == 0 || comp->xmit_current != cs->cs_this){
452 cp = ocp;
453 *cpp = ocp;
454 *cp++ = changes | NEW_C;
455 *cp++ = cs->cs_this;
456 comp->xmit_current = cs->cs_this;
457 } else {
458 cp = ocp;
459 *cpp = ocp;
460 *cp++ = changes;
461 }
462 cp = put16(cp,(short)deltaA);
463
464 memcpy(cp,new_seq,deltaS);
465 memcpy(cp+deltaS,icp+hlen,isize-hlen);
466 comp->sls_o_compressed++;
467 ocp[0] |= SL_TYPE_COMPRESSED_TCP;
468 return isize - hlen + deltaS + (cp - ocp);
469
470
471
472
473
474 uncompressed:
475 memcpy(&cs->cs_ip,ip,20);
476 memcpy(&cs->cs_tcp,th,20);
477 if (ip->ihl > 5)
478 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
479 if (th->doff > 5)
480 memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
481 comp->xmit_current = cs->cs_this;
482 comp->sls_o_uncompressed++;
483 memcpy(ocp, icp, isize);
484 *cpp = ocp;
485 ocp[9] = cs->cs_this;
486 ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
487 return isize;
488 }
489
490
491 int
492 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
493 {
494 register int changes;
495 long x;
496 register struct tcphdr *thp;
497 register struct iphdr *ip;
498 register struct cstate *cs;
499 int len, hdrlen;
500 unsigned char *cp = icp;
501
502
503 comp->sls_i_compressed++;
504 if(isize < 3){
505 comp->sls_i_error++;
506 return 0;
507 }
508 changes = *cp++;
509 if(changes & NEW_C){
510
511
512
513 x = *cp++;
514 if(x < 0 || x > comp->rslot_limit)
515 goto bad;
516
517 comp->flags &=~ SLF_TOSS;
518 comp->recv_current = x;
519 } else {
520
521
522
523 if(comp->flags & SLF_TOSS){
524 comp->sls_i_tossed++;
525 return 0;
526 }
527 }
528 cs = &comp->rstate[comp->recv_current];
529 thp = &cs->cs_tcp;
530 ip = &cs->cs_ip;
531
532 if((x = pull16(&cp)) == -1) {
533 goto bad;
534 }
535 thp->check = htons(x);
536
537 thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
538
539
540
541
542
543
544 hdrlen = ip->ihl * 4 + thp->doff * 4;
545
546 switch(changes & SPECIALS_MASK){
547 case SPECIAL_I:
548 {
549 register short i;
550 i = ntohs(ip->tot_len) - hdrlen;
551 thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
552 thp->seq = htonl( ntohl(thp->seq) + i);
553 }
554 break;
555
556 case SPECIAL_D:
557 thp->seq = htonl( ntohl(thp->seq) +
558 ntohs(ip->tot_len) - hdrlen);
559 break;
560
561 default:
562 if(changes & NEW_U){
563 thp->urg = 1;
564 if((x = decode(&cp)) == -1) {
565 goto bad;
566 }
567 thp->urg_ptr = htons(x);
568 } else
569 thp->urg = 0;
570 if(changes & NEW_W){
571 if((x = decode(&cp)) == -1) {
572 goto bad;
573 }
574 thp->window = htons( ntohs(thp->window) + x);
575 }
576 if(changes & NEW_A){
577 if((x = decode(&cp)) == -1) {
578 goto bad;
579 }
580 thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
581 }
582 if(changes & NEW_S){
583 if((x = decode(&cp)) == -1) {
584 goto bad;
585 }
586 thp->seq = htonl( ntohl(thp->seq) + x);
587 }
588 break;
589 }
590 if(changes & NEW_I){
591 if((x = decode(&cp)) == -1) {
592 goto bad;
593 }
594 ip->id = htons (ntohs (ip->id) + x);
595 } else
596 ip->id = htons (ntohs (ip->id) + 1);
597
598
599
600
601
602
603
604 len = isize - (cp - icp);
605 if (len < 0)
606 goto bad;
607 len += hdrlen;
608 ip->tot_len = htons(len);
609 ip->check = 0;
610
611 memmove(icp + hdrlen, cp, len - hdrlen);
612
613 cp = icp;
614 memcpy(cp, ip, 20);
615 cp += 20;
616
617 if (ip->ihl > 5) {
618 memcpy(cp, cs->cs_ipopt, (ip->ihl - 5) * 4);
619 cp += (ip->ihl - 5) * 4;
620 }
621
622 #ifdef __alpha__
623 stw_u(ip_fast_csum(icp, ip->ihl), &((struct iphdr *)icp)->check);
624 #else
625 ((struct iphdr *)icp)->check = ip_fast_csum(icp, ((struct iphdr*)icp)->ihl);
626 #endif
627
628 memcpy(cp, thp, 20);
629 cp += 20;
630
631 if (thp->doff > 5) {
632 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
633 cp += ((thp->doff) - 5) * 4;
634 }
635
636 return len;
637 bad:
638 comp->sls_i_error++;
639 return slhc_toss( comp );
640 }
641
642
643 int
644 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
645 {
646 register struct cstate *cs;
647 unsigned ihl;
648
649 unsigned char index;
650
651 if(isize < 20) {
652
653 comp->sls_i_runt++;
654 return slhc_toss( comp );
655 }
656
657 ihl = icp[0] & 0xf;
658 if(ihl < 20 / 4){
659
660 comp->sls_i_runt++;
661 return slhc_toss( comp );
662 }
663 index = icp[9];
664 icp[9] = IPPROTO_TCP;
665
666 if (ip_fast_csum(icp, ihl)) {
667
668 comp->sls_i_badcheck++;
669 return slhc_toss( comp );
670 }
671 if(index > comp->rslot_limit) {
672 comp->sls_i_error++;
673 return slhc_toss(comp);
674 }
675
676
677 cs = &comp->rstate[comp->recv_current = index];
678 comp->flags &=~ SLF_TOSS;
679 memcpy(&cs->cs_ip,icp,20);
680 memcpy(&cs->cs_tcp,icp + ihl*4,20);
681 if (ihl > 5)
682 memcpy(cs->cs_ipopt, icp + sizeof(struct iphdr), (ihl - 5) * 4);
683 if (cs->cs_tcp.doff > 5)
684 memcpy(cs->cs_tcpopt, icp + ihl*4 + sizeof(struct tcphdr), (cs->cs_tcp.doff - 5) * 4);
685 cs->cs_hsize = ihl*2 + cs->cs_tcp.doff*2;
686
687
688
689 comp->sls_i_uncompressed++;
690 return isize;
691 }
692
693
694 int
695 slhc_toss(struct slcompress *comp)
696 {
697 if ( comp == NULLSLCOMPR )
698 return 0;
699
700 comp->flags |= SLF_TOSS;
701 return 0;
702 }
703
704
705 void slhc_i_status(struct slcompress *comp)
706 {
707 if (comp != NULLSLCOMPR) {
708 printk("\t%ld Cmp, %ld Uncmp, %ld Bad, %ld Tossed\n",
709 comp->sls_i_compressed,
710 comp->sls_i_uncompressed,
711 comp->sls_i_error,
712 comp->sls_i_tossed);
713 }
714 }
715
716
717 void slhc_o_status(struct slcompress *comp)
718 {
719 if (comp != NULLSLCOMPR) {
720 printk("\t%ld Cmp, %ld Uncmp, %ld AsIs, %ld NotTCP\n",
721 comp->sls_o_compressed,
722 comp->sls_o_uncompressed,
723 comp->sls_o_tcp,
724 comp->sls_o_nontcp);
725 printk("\t%10ld Searches, %10ld Misses\n",
726 comp->sls_o_searches,
727 comp->sls_o_misses);
728 }
729 }
730
731 static struct symbol_table slhc_syms = {
732
733 #include <linux/symtab_begin.h>
734
735 X(slhc_init),
736 X(slhc_free),
737 X(slhc_remember),
738 X(slhc_compress),
739 X(slhc_uncompress),
740 X(slhc_toss),
741 #include <linux/symtab_end.h>
742 };
743
744 static void export_slhc_syms(void)
745 {
746 register_symtab(&slhc_syms);
747 }
748
749 #ifdef MODULE
750
751 int init_module(void)
752 {
753 printk("CSLIP: code copyright 1989 Regents of the University of California\n");
754 export_slhc_syms();
755 return 0;
756 }
757
758 void cleanup_module(void)
759 {
760 return;
761 }
762 #else
763
764 void slhc_install(void)
765 {
766 export_slhc_syms();
767 }
768 #endif
769 #endif