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