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