This source file includes following definitions.
- slhc_init
- slhc_free
- put16
- encode
- pull16
- decode
- slhc_compress
- slhc_uncompress
- slhc_remember
- slhc_toss
- slhc_toss
- slhc_uncompress
- slhc_compress
- slhc_remember
- slhc_free
- slhc_init
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/module.h>
54 #include <linux/slab.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/errno.h>
58 #include <linux/kernel.h>
59 #include <net/slhc_vj.h>
60
61 #ifdef CONFIG_INET
62
63 #include <linux/mm.h>
64 #include <linux/socket.h>
65 #include <linux/sockios.h>
66 #include <linux/termios.h>
67 #include <linux/in.h>
68 #include <linux/fcntl.h>
69 #include <linux/inet.h>
70 #include <linux/netdevice.h>
71 #include <net/ip.h>
72 #include <net/protocol.h>
73 #include <net/icmp.h>
74 #include <net/tcp.h>
75 #include <linux/skbuff.h>
76 #include <net/sock.h>
77 #include <linux/timer.h>
78 #include <linux/uaccess.h>
79 #include <net/checksum.h>
80 #include <asm/unaligned.h>
81
82 static unsigned char *encode(unsigned char *cp, unsigned short n);
83 static long decode(unsigned char **cpp);
84 static unsigned char * put16(unsigned char *cp, unsigned short x);
85 static unsigned short pull16(unsigned char **cpp);
86
87
88
89
90
91 struct slcompress *
92 slhc_init(int rslots, int tslots)
93 {
94 short i;
95 struct cstate *ts;
96 struct slcompress *comp;
97
98 if (rslots < 0 || rslots > 255 || tslots < 0 || tslots > 255)
99 return ERR_PTR(-EINVAL);
100
101 comp = kzalloc(sizeof(struct slcompress), GFP_KERNEL);
102 if (! comp)
103 goto out_fail;
104
105 if (rslots > 0) {
106 size_t rsize = rslots * sizeof(struct cstate);
107 comp->rstate = kzalloc(rsize, GFP_KERNEL);
108 if (! comp->rstate)
109 goto out_free;
110 comp->rslot_limit = rslots - 1;
111 }
112
113 if (tslots > 0) {
114 size_t tsize = tslots * sizeof(struct cstate);
115 comp->tstate = kzalloc(tsize, GFP_KERNEL);
116 if (! comp->tstate)
117 goto out_free2;
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 out_free2:
144 kfree(comp->rstate);
145 out_free:
146 kfree(comp);
147 out_fail:
148 return ERR_PTR(-ENOMEM);
149 }
150
151
152
153 void
154 slhc_free(struct slcompress *comp)
155 {
156 if ( IS_ERR_OR_NULL(comp) )
157 return;
158
159 if ( comp->tstate != NULLSLSTATE )
160 kfree( comp->tstate );
161
162 if ( comp->rstate != NULLSLSTATE )
163 kfree( comp->rstate );
164
165 kfree( comp );
166 }
167
168
169
170 static inline unsigned char *
171 put16(unsigned char *cp, unsigned short x)
172 {
173 *cp++ = x >> 8;
174 *cp++ = x;
175
176 return cp;
177 }
178
179
180
181 static unsigned char *
182 encode(unsigned char *cp, unsigned short n)
183 {
184 if(n >= 256 || n == 0){
185 *cp++ = 0;
186 cp = put16(cp,n);
187 } else {
188 *cp++ = n;
189 }
190 return cp;
191 }
192
193
194 static unsigned short
195 pull16(unsigned char **cpp)
196 {
197 short rval;
198
199 rval = *(*cpp)++;
200 rval <<= 8;
201 rval |= *(*cpp)++;
202 return rval;
203 }
204
205
206 static long
207 decode(unsigned char **cpp)
208 {
209 int x;
210
211 x = *(*cpp)++;
212 if(x == 0){
213 return pull16(cpp) & 0xffff;
214 } else {
215 return x & 0xff;
216 }
217 }
218
219
220
221
222
223
224
225
226 int
227 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
228 unsigned char *ocp, unsigned char **cpp, int compress_cid)
229 {
230 struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
231 struct cstate *lcs = ocs;
232 struct cstate *cs = lcs->next;
233 unsigned long deltaS, deltaA;
234 short changes = 0;
235 int nlen, hlen;
236 unsigned char new_seq[16];
237 unsigned char *cp = new_seq;
238 struct iphdr *ip;
239 struct tcphdr *th, *oth;
240 __sum16 csum;
241
242
243
244
245
246
247 if(isize<sizeof(struct iphdr))
248 return isize;
249
250 ip = (struct iphdr *) icp;
251 if (ip->version != 4 || ip->ihl < 5)
252 return isize;
253
254
255 if (ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x3fff)) {
256
257 if(ip->protocol != IPPROTO_TCP)
258 comp->sls_o_nontcp++;
259 else
260 comp->sls_o_tcp++;
261 return isize;
262 }
263 nlen = ip->ihl * 4;
264 if (isize < nlen + sizeof(*th))
265 return isize;
266
267 th = (struct tcphdr *)(icp + nlen);
268 if (th->doff < sizeof(struct tcphdr) / 4)
269 return isize;
270 hlen = nlen + th->doff * 4;
271
272
273
274
275
276 if(hlen > isize || 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(ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
354 || ip->tos != cs->cs_ip.tos
355 || (ip->frag_off & htons(0x4000)) != (cs->cs_ip.frag_off & htons(0x4000))
356 || ip->ttl != cs->cs_ip.ttl
357 || th->doff != cs->cs_tcp.doff
358 || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
359 || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4) != 0)){
360 goto uncompressed;
361 }
362
363
364
365
366
367
368
369 if(th->urg){
370 deltaS = ntohs(th->urg_ptr);
371 cp = encode(cp,deltaS);
372 changes |= NEW_U;
373 } else if(th->urg_ptr != oth->urg_ptr){
374
375
376
377
378 goto uncompressed;
379 }
380 if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
381 cp = encode(cp,deltaS);
382 changes |= NEW_W;
383 }
384 if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
385 if(deltaA > 0x0000ffff)
386 goto uncompressed;
387 cp = encode(cp,deltaA);
388 changes |= NEW_A;
389 }
390 if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
391 if(deltaS > 0x0000ffff)
392 goto uncompressed;
393 cp = encode(cp,deltaS);
394 changes |= NEW_S;
395 }
396
397 switch(changes){
398 case 0:
399
400
401
402
403
404
405 if(ip->tot_len != cs->cs_ip.tot_len &&
406 ntohs(cs->cs_ip.tot_len) == hlen)
407 break;
408 goto uncompressed;
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 csum = 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 *(__sum16 *)cp = csum;
463 cp += 2;
464
465 memcpy(cp,new_seq,deltaS);
466 memcpy(cp+deltaS,icp+hlen,isize-hlen);
467 comp->sls_o_compressed++;
468 ocp[0] |= SL_TYPE_COMPRESSED_TCP;
469 return isize - hlen + deltaS + (cp - ocp);
470
471
472
473
474
475 uncompressed:
476 memcpy(&cs->cs_ip,ip,20);
477 memcpy(&cs->cs_tcp,th,20);
478 if (ip->ihl > 5)
479 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
480 if (th->doff > 5)
481 memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
482 comp->xmit_current = cs->cs_this;
483 comp->sls_o_uncompressed++;
484 memcpy(ocp, icp, isize);
485 *cpp = ocp;
486 ocp[9] = cs->cs_this;
487 ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
488 return isize;
489 }
490
491
492 int
493 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
494 {
495 int changes;
496 long x;
497 struct tcphdr *thp;
498 struct iphdr *ip;
499 struct cstate *cs;
500 int len, hdrlen;
501 unsigned char *cp = icp;
502
503
504 comp->sls_i_compressed++;
505 if(isize < 3){
506 comp->sls_i_error++;
507 return 0;
508 }
509 changes = *cp++;
510 if(changes & NEW_C){
511
512
513
514 x = *cp++;
515 if(x < 0 || x > comp->rslot_limit)
516 goto bad;
517
518
519 if (!comp->rstate[x].initialized)
520 goto bad;
521
522 comp->flags &=~ SLF_TOSS;
523 comp->recv_current = x;
524 } else {
525
526
527
528 if(comp->flags & SLF_TOSS){
529 comp->sls_i_tossed++;
530 return 0;
531 }
532 }
533 cs = &comp->rstate[comp->recv_current];
534 thp = &cs->cs_tcp;
535 ip = &cs->cs_ip;
536
537 thp->check = *(__sum16 *)cp;
538 cp += 2;
539
540 thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
541
542
543
544
545
546
547 hdrlen = ip->ihl * 4 + thp->doff * 4;
548
549 switch(changes & SPECIALS_MASK){
550 case SPECIAL_I:
551 {
552 short i;
553 i = ntohs(ip->tot_len) - hdrlen;
554 thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
555 thp->seq = htonl( ntohl(thp->seq) + i);
556 }
557 break;
558
559 case SPECIAL_D:
560 thp->seq = htonl( ntohl(thp->seq) +
561 ntohs(ip->tot_len) - hdrlen);
562 break;
563
564 default:
565 if(changes & NEW_U){
566 thp->urg = 1;
567 if((x = decode(&cp)) == -1) {
568 goto bad;
569 }
570 thp->urg_ptr = htons(x);
571 } else
572 thp->urg = 0;
573 if(changes & NEW_W){
574 if((x = decode(&cp)) == -1) {
575 goto bad;
576 }
577 thp->window = htons( ntohs(thp->window) + x);
578 }
579 if(changes & NEW_A){
580 if((x = decode(&cp)) == -1) {
581 goto bad;
582 }
583 thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
584 }
585 if(changes & NEW_S){
586 if((x = decode(&cp)) == -1) {
587 goto bad;
588 }
589 thp->seq = htonl( ntohl(thp->seq) + x);
590 }
591 break;
592 }
593 if(changes & NEW_I){
594 if((x = decode(&cp)) == -1) {
595 goto bad;
596 }
597 ip->id = htons (ntohs (ip->id) + x);
598 } else
599 ip->id = htons (ntohs (ip->id) + 1);
600
601
602
603
604
605
606
607 len = isize - (cp - icp);
608 if (len < 0)
609 goto bad;
610 len += hdrlen;
611 ip->tot_len = htons(len);
612 ip->check = 0;
613
614 memmove(icp + hdrlen, cp, len - hdrlen);
615
616 cp = icp;
617 memcpy(cp, ip, 20);
618 cp += 20;
619
620 if (ip->ihl > 5) {
621 memcpy(cp, cs->cs_ipopt, (ip->ihl - 5) * 4);
622 cp += (ip->ihl - 5) * 4;
623 }
624
625 put_unaligned(ip_fast_csum(icp, ip->ihl),
626 &((struct iphdr *)icp)->check);
627
628 memcpy(cp, thp, 20);
629 cp += 20;
630
631 if (thp->doff > 5) {
632 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
633 cp += ((thp->doff) - 5) * 4;
634 }
635
636 return len;
637 bad:
638 comp->sls_i_error++;
639 return slhc_toss( comp );
640 }
641
642
643 int
644 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
645 {
646 struct cstate *cs;
647 unsigned ihl;
648
649 unsigned char index;
650
651 if(isize < 20) {
652
653 comp->sls_i_runt++;
654 return slhc_toss( comp );
655 }
656
657 ihl = icp[0] & 0xf;
658 if(ihl < 20 / 4){
659
660 comp->sls_i_runt++;
661 return slhc_toss( comp );
662 }
663 index = icp[9];
664 icp[9] = IPPROTO_TCP;
665
666 if (ip_fast_csum(icp, ihl)) {
667
668 comp->sls_i_badcheck++;
669 return slhc_toss( comp );
670 }
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,icp,20);
680 memcpy(&cs->cs_tcp,icp + ihl*4,20);
681 if (ihl > 5)
682 memcpy(cs->cs_ipopt, icp + sizeof(struct iphdr), (ihl - 5) * 4);
683 if (cs->cs_tcp.doff > 5)
684 memcpy(cs->cs_tcpopt, icp + ihl*4 + sizeof(struct tcphdr), (cs->cs_tcp.doff - 5) * 4);
685 cs->cs_hsize = ihl*2 + cs->cs_tcp.doff*2;
686 cs->initialized = true;
687
688
689
690 comp->sls_i_uncompressed++;
691 return isize;
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 #else
705
706 int
707 slhc_toss(struct slcompress *comp)
708 {
709 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_toss");
710 return -EINVAL;
711 }
712 int
713 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
714 {
715 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_uncompress");
716 return -EINVAL;
717 }
718 int
719 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
720 unsigned char *ocp, unsigned char **cpp, int compress_cid)
721 {
722 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_compress");
723 return -EINVAL;
724 }
725
726 int
727 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
728 {
729 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_remember");
730 return -EINVAL;
731 }
732
733 void
734 slhc_free(struct slcompress *comp)
735 {
736 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_free");
737 }
738 struct slcompress *
739 slhc_init(int rslots, int tslots)
740 {
741 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_init");
742 return NULL;
743 }
744
745 #endif
746
747
748 EXPORT_SYMBOL(slhc_init);
749 EXPORT_SYMBOL(slhc_free);
750 EXPORT_SYMBOL(slhc_remember);
751 EXPORT_SYMBOL(slhc_compress);
752 EXPORT_SYMBOL(slhc_uncompress);
753 EXPORT_SYMBOL(slhc_toss);
754
755 MODULE_LICENSE("Dual BSD/GPL");