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