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