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