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