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