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 #include <linux/config.h>
50 #ifdef CONFIG_INET
51
52 #include <linux/types.h>
53 #include <linux/sched.h>
54 #include <linux/mm.h>
55 #include <linux/string.h>
56 #include <linux/socket.h>
57 #include <linux/sockios.h>
58 #include <linux/termios.h>
59 #include <linux/in.h>
60 #include <linux/fcntl.h>
61 #include <linux/inet.h>
62 #include <linux/netdevice.h>
63 #include "ip.h"
64 #include "protocol.h"
65 #include "icmp.h"
66 #include "tcp.h"
67 #include <linux/skbuff.h>
68 #include "sock.h"
69 #include <linux/errno.h>
70 #include <linux/timer.h>
71 #include <asm/system.h>
72 #include <asm/segment.h>
73 #include <linux/mm.h>
74 #include "slhc.h"
75
76 #ifdef MODULE
77 #include <linux/module.h>
78 #include <linux/version.h>
79 #endif
80
81 int last_retran;
82
83 static unsigned char *encode(unsigned char *cp, unsigned short n);
84 static long decode(unsigned char **cpp);
85 static unsigned char * put16(unsigned char *cp, unsigned short x);
86 static unsigned short pull16(unsigned char **cpp);
87
88 extern int ip_csum(struct iphdr *iph);
89
90
91
92
93
94 struct slcompress *
95 slhc_init(int rslots, int tslots)
96 {
97 register short i;
98 register struct cstate *ts;
99 struct slcompress *comp;
100
101 comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
102 GFP_KERNEL);
103 if (! comp)
104 return NULL;
105
106 memset(comp, 0, sizeof(struct slcompress));
107
108 if ( rslots > 0 && rslots < 256 ) {
109 comp->rstate =
110 (struct cstate *)kmalloc(rslots * sizeof(struct cstate),
111 GFP_KERNEL);
112 if (! comp->rstate)
113 {
114 kfree((unsigned char *)comp);
115 return NULL;
116 }
117 memset(comp->rstate, 0, rslots * sizeof(struct cstate));
118 comp->rslot_limit = rslots - 1;
119 }
120
121 if ( tslots > 0 && tslots < 256 ) {
122 comp->tstate =
123 (struct cstate *)kmalloc(tslots * sizeof(struct cstate),
124 GFP_KERNEL);
125 if (! comp->tstate)
126 {
127 kfree((unsigned char *)comp->rstate);
128 kfree((unsigned char *)comp);
129 return NULL;
130 }
131 memset(comp->tstate, 0, rslots * sizeof(struct cstate));
132 comp->tslot_limit = tslots - 1;
133 }
134
135 comp->xmit_oldest = 0;
136 comp->xmit_current = 255;
137 comp->recv_current = 255;
138
139
140
141
142
143
144 comp->flags |= SLF_TOSS;
145
146 if ( tslots > 0 ) {
147 ts = comp->tstate;
148 for(i = comp->tslot_limit; i > 0; --i){
149 ts[i].cs_this = i;
150 ts[i].next = &(ts[i - 1]);
151 }
152 ts[0].next = &(ts[comp->tslot_limit]);
153 ts[0].cs_this = 0;
154 }
155 #ifdef MODULE
156 MOD_INC_USE_COUNT;
157 #endif
158 return comp;
159 }
160
161
162
163 void
164 slhc_free(struct slcompress *comp)
165 {
166 if ( comp == NULLSLCOMPR )
167 return;
168
169 if ( comp->rstate != NULLSLSTATE )
170 kfree( comp->rstate );
171
172 if ( comp->tstate != NULLSLSTATE )
173 kfree( comp->tstate );
174
175 #ifdef MODULE
176 MOD_DEC_USE_COUNT;
177 #endif
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 ((struct iphdr *)icp)->check = ip_csum((struct iphdr*)icp);
623
624 memcpy(cp, thp, 20);
625 cp += 20;
626
627 if (thp->doff > 5) {
628 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
629 cp += ((thp->doff) - 5) * 4;
630 }
631
632 return len;
633 bad:
634 comp->sls_i_error++;
635 return slhc_toss( comp );
636 }
637
638
639 int
640 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
641 {
642 register struct cstate *cs;
643 short ip_len;
644 struct iphdr *ip;
645 struct tcphdr *thp;
646
647 unsigned char index;
648
649 if(isize < 20) {
650
651 comp->sls_i_runt++;
652 return slhc_toss( comp );
653 }
654
655 ip_len = (icp[0] & 0xf) << 2;
656 if(ip_len < 20){
657
658 comp->sls_i_runt++;
659 return slhc_toss( comp );
660 }
661 index = icp[9];
662 icp[9] = IPPROTO_TCP;
663 ip = (struct iphdr *) icp;
664
665 if (ip_csum(ip)) {
666
667 comp->sls_i_badcheck++;
668 return slhc_toss( comp );
669 }
670 thp = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
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,ip,20);
680 memcpy(&cs->cs_tcp,thp,20);
681 if (ip->ihl > 5)
682 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
683 if (thp->doff > 5)
684 memcpy(cs->cs_tcpopt, thp+1, ((thp->doff) - 5) * 4);
685 cs->cs_hsize = ip->ihl*2 + thp->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 #ifdef MODULE
732 char kernel_version[] = UTS_RELEASE;
733
734 int init_module(void)
735 {
736 printk("CSLIP: code copyright 1989 Regents of the University of California\n");
737 return 0;
738 }
739
740 void cleanup_module(void)
741 {
742 if (MOD_IN_USE) {
743 printk("CSLIP: module in use, remove delayed");
744 }
745 return;
746 }
747 #endif
748 #endif