2 * Routines to compress and uncompress tcp packets (for transmission
3 * over low speed serial lines).
5 * Copyright (c) 1989 Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
21 * - Initial distribution.
24 * modified for KA9Q Internet Software Package by
25 * Katie Stevens (dkstevens@ucdavis.edu)
26 * University of California, Davis
28 * - 01-31-90 initial adaptation (from 1.19)
29 * PPP.05 02-15-90 [ks]
30 * PPP.08 05-02-90 [ks] use PPP protocol field to signal compression
31 * PPP.15 09-90 [ks] improve mbuf handling
32 * PPP.16 11-02 [karn] substantially rewritten to use NOS facilities
34 * - Feb 1991 Bill_Simpson@um.cc.umich.edu
35 * variable number of conversation slots
36 * allow zero or one slots
39 * - Jul 1994 Dmitry Gorodchanin
40 * Fixes for memory leaks.
41 * - Oct 1994 Dmitry Gorodchanin
43 * - Jan 1995 Bjorn Ekwall
44 * Use ip_fast_csum from ip.h
45 * - July 1995 Christos A. Polyzols
46 * Spotted bug in tcp option checking
49 * This module is a difficult issue. It's clearly inet code but it's also clearly
50 * driver code belonging close to PPP and SLIP
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>
62 /* Entire module is for IP only */
64 #include <linux/socket.h>
65 #include <linux/sockios.h>
66 #include <linux/termios.h>
68 #include <linux/fcntl.h>
69 #include <linux/inet.h>
70 #include <linux/netdevice.h>
72 #include <net/protocol.h>
75 #include <linux/skbuff.h>
77 #include <linux/timer.h>
78 #include <asm/system.h>
79 #include <asm/uaccess.h>
80 #include <net/checksum.h>
81 #include <asm/unaligned.h>
83 static unsigned char *encode(unsigned char *cp
, unsigned short n
);
84 static long decode(unsigned char **cpp
);
85 static unsigned char * put16(unsigned char *cp
, unsigned short x
);
86 static unsigned short pull16(unsigned char **cpp
);
88 /* Initialize compression data structure
89 * slots must be in range 0 to 255 (zero meaning no compression)
92 slhc_init(int rslots
, int tslots
)
95 register struct cstate
*ts
;
96 struct slcompress
*comp
;
98 comp
= kzalloc(sizeof(struct slcompress
), GFP_KERNEL
);
102 if ( rslots
> 0 && rslots
< 256 ) {
103 size_t rsize
= rslots
* sizeof(struct cstate
);
104 comp
->rstate
= kzalloc(rsize
, GFP_KERNEL
);
107 comp
->rslot_limit
= rslots
- 1;
110 if ( tslots
> 0 && tslots
< 256 ) {
111 size_t tsize
= tslots
* sizeof(struct cstate
);
112 comp
->tstate
= kzalloc(tsize
, GFP_KERNEL
);
115 comp
->tslot_limit
= tslots
- 1;
118 comp
->xmit_oldest
= 0;
119 comp
->xmit_current
= 255;
120 comp
->recv_current
= 255;
122 * don't accept any packets with implicit index until we get
123 * one with an explicit index. Otherwise the uncompress code
124 * will try to use connection 255, which is almost certainly
127 comp
->flags
|= SLF_TOSS
;
131 for(i
= comp
->tslot_limit
; i
> 0; --i
){
133 ts
[i
].next
= &(ts
[i
- 1]);
135 ts
[0].next
= &(ts
[comp
->tslot_limit
]);
149 /* Free a compression data structure */
151 slhc_free(struct slcompress
*comp
)
153 if ( comp
== NULLSLCOMPR
)
156 if ( comp
->tstate
!= NULLSLSTATE
)
157 kfree( comp
->tstate
);
159 if ( comp
->rstate
!= NULLSLSTATE
)
160 kfree( comp
->rstate
);
166 /* Put a short in host order into a char array in network order */
167 static inline unsigned char *
168 put16(unsigned char *cp
, unsigned short x
)
177 /* Encode a number */
178 static unsigned char *
179 encode(unsigned char *cp
, unsigned short n
)
181 if(n
>= 256 || n
== 0){
190 /* Pull a 16-bit integer in host order from buffer in network byte order */
191 static unsigned short
192 pull16(unsigned char **cpp
)
202 /* Decode a number */
204 decode(unsigned char **cpp
)
210 return pull16(cpp
) & 0xffff; /* pull16 returns -1 on error */
212 return x
& 0xff; /* -1 if PULLCHAR returned error */
217 * icp and isize are the original packet.
218 * ocp is a place to put a copy if necessary.
219 * cpp is initially a pointer to icp. If the copy is used,
224 slhc_compress(struct slcompress
*comp
, unsigned char *icp
, int isize
,
225 unsigned char *ocp
, unsigned char **cpp
, int compress_cid
)
227 register struct cstate
*ocs
= &(comp
->tstate
[comp
->xmit_oldest
]);
228 register struct cstate
*lcs
= ocs
;
229 register struct cstate
*cs
= lcs
->next
;
230 register unsigned long deltaS
, deltaA
;
231 register short changes
= 0;
233 unsigned char new_seq
[16];
234 register unsigned char *cp
= new_seq
;
236 struct tcphdr
*th
, *oth
;
241 * Don't play with runt packets.
244 if(isize
<sizeof(struct iphdr
))
247 ip
= (struct iphdr
*) icp
;
249 /* Bail if this packet isn't TCP, or is an IP fragment */
250 if (ip
->protocol
!= IPPROTO_TCP
|| (ntohs(ip
->frag_off
) & 0x3fff)) {
251 /* Send as regular IP */
252 if(ip
->protocol
!= IPPROTO_TCP
)
253 comp
->sls_o_nontcp
++;
258 /* Extract TCP header */
260 th
= (struct tcphdr
*)(((unsigned char *)ip
) + ip
->ihl
*4);
261 hlen
= ip
->ihl
*4 + th
->doff
*4;
263 /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
264 * some other control bit is set). Also uncompressible if
267 if(hlen
> isize
|| th
->syn
|| th
->fin
|| th
->rst
||
269 /* TCP connection stuff; send as regular IP */
274 * Packet is compressible -- we're going to send either a
275 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
276 * we need to locate (or create) the connection state.
278 * States are kept in a circularly linked list with
279 * xmit_oldest pointing to the end of the list. The
280 * list is kept in lru order by moving a state to the
281 * head of the list whenever it is referenced. Since
282 * the list is short and, empirically, the connection
283 * we want is almost always near the front, we locate
284 * states via linear search. If we don't find a state
285 * for the datagram, the oldest state is (re-)used.
288 if( ip
->saddr
== cs
->cs_ip
.saddr
289 && ip
->daddr
== cs
->cs_ip
.daddr
290 && th
->source
== cs
->cs_tcp
.source
291 && th
->dest
== cs
->cs_tcp
.dest
)
294 /* if current equal oldest, at end of list */
299 comp
->sls_o_searches
++;
302 * Didn't find it -- re-use oldest cstate. Send an
303 * uncompressed packet that tells the other side what
304 * connection number we're using for this conversation.
306 * Note that since the state list is circular, the oldest
307 * state points to the newest and we only need to set
308 * xmit_oldest to update the lru linkage.
310 comp
->sls_o_misses
++;
311 comp
->xmit_oldest
= lcs
->cs_this
;
316 * Found it -- move to the front on the connection list.
319 /* found at most recently used */
320 } else if (cs
== ocs
) {
321 /* found at least recently used */
322 comp
->xmit_oldest
= lcs
->cs_this
;
324 /* more than 2 elements */
325 lcs
->next
= cs
->next
;
326 cs
->next
= ocs
->next
;
331 * Make sure that only what we expect to change changed.
332 * Check the following:
333 * IP protocol version, header length & type of service.
334 * The "Don't fragment" bit.
335 * The time-to-live field.
336 * The TCP header length.
337 * IP options, if any.
338 * TCP options, if any.
339 * If any of these things are different between the previous &
340 * current datagram, we send the current datagram `uncompressed'.
344 if(ip
->version
!= cs
->cs_ip
.version
|| ip
->ihl
!= cs
->cs_ip
.ihl
345 || ip
->tos
!= cs
->cs_ip
.tos
346 || (ip
->frag_off
& htons(0x4000)) != (cs
->cs_ip
.frag_off
& htons(0x4000))
347 || ip
->ttl
!= cs
->cs_ip
.ttl
348 || th
->doff
!= cs
->cs_tcp
.doff
349 || (ip
->ihl
> 5 && memcmp(ip
+1,cs
->cs_ipopt
,((ip
->ihl
)-5)*4) != 0)
350 || (th
->doff
> 5 && memcmp(th
+1,cs
->cs_tcpopt
,((th
->doff
)-5)*4) != 0)){
355 * Figure out which of the changing fields changed. The
356 * receiver expects changes in the order: urgent, window,
357 * ack, seq (the order minimizes the number of temporaries
358 * needed in this section of code).
361 deltaS
= ntohs(th
->urg_ptr
);
362 cp
= encode(cp
,deltaS
);
364 } else if(th
->urg_ptr
!= oth
->urg_ptr
){
365 /* argh! URG not set but urp changed -- a sensible
366 * implementation should never do this but RFC793
367 * doesn't prohibit the change so we have to deal
371 if((deltaS
= ntohs(th
->window
) - ntohs(oth
->window
)) != 0){
372 cp
= encode(cp
,deltaS
);
375 if((deltaA
= ntohl(th
->ack_seq
) - ntohl(oth
->ack_seq
)) != 0L){
376 if(deltaA
> 0x0000ffff)
378 cp
= encode(cp
,deltaA
);
381 if((deltaS
= ntohl(th
->seq
) - ntohl(oth
->seq
)) != 0L){
382 if(deltaS
> 0x0000ffff)
384 cp
= encode(cp
,deltaS
);
389 case 0: /* Nothing changed. If this packet contains data and the
390 * last one didn't, this is probably a data packet following
391 * an ack (normal on an interactive connection) and we send
392 * it compressed. Otherwise it's probably a retransmit,
393 * retransmitted ack or window probe. Send it uncompressed
394 * in case the other side missed the compressed version.
396 if(ip
->tot_len
!= cs
->cs_ip
.tot_len
&&
397 ntohs(cs
->cs_ip
.tot_len
) == hlen
)
403 /* actual changes match one of our special case encodings --
404 * send packet uncompressed.
408 if(deltaS
== deltaA
&&
409 deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
410 /* special case for echoed terminal traffic */
416 if(deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
417 /* special case for data xfer */
423 deltaS
= ntohs(ip
->id
) - ntohs(cs
->cs_ip
.id
);
425 cp
= encode(cp
,deltaS
);
429 changes
|= TCP_PUSH_BIT
;
430 /* Grab the cksum before we overwrite it below. Then update our
431 * state with this packet's header.
434 memcpy(&cs
->cs_ip
,ip
,20);
435 memcpy(&cs
->cs_tcp
,th
,20);
436 /* We want to use the original packet as our compressed packet.
437 * (cp - new_seq) is the number of bytes we need for compressed
438 * sequence numbers. In addition we need one byte for the change
439 * mask, one for the connection id and two for the tcp checksum.
440 * So, (cp - new_seq) + 4 bytes of header are needed.
442 deltaS
= cp
- new_seq
;
443 if(compress_cid
== 0 || comp
->xmit_current
!= cs
->cs_this
){
446 *cp
++ = changes
| NEW_C
;
448 comp
->xmit_current
= cs
->cs_this
;
454 *(__sum16
*)cp
= csum
;
456 /* deltaS is now the size of the change section of the compressed header */
457 memcpy(cp
,new_seq
,deltaS
); /* Write list of deltas */
458 memcpy(cp
+deltaS
,icp
+hlen
,isize
-hlen
);
459 comp
->sls_o_compressed
++;
460 ocp
[0] |= SL_TYPE_COMPRESSED_TCP
;
461 return isize
- hlen
+ deltaS
+ (cp
- ocp
);
463 /* Update connection state cs & send uncompressed packet (i.e.,
464 * a regular ip/tcp packet but with the 'conversation id' we hope
465 * to use on future compressed packets in the protocol field).
468 memcpy(&cs
->cs_ip
,ip
,20);
469 memcpy(&cs
->cs_tcp
,th
,20);
471 memcpy(cs
->cs_ipopt
, ip
+1, ((ip
->ihl
) - 5) * 4);
473 memcpy(cs
->cs_tcpopt
, th
+1, ((th
->doff
) - 5) * 4);
474 comp
->xmit_current
= cs
->cs_this
;
475 comp
->sls_o_uncompressed
++;
476 memcpy(ocp
, icp
, isize
);
478 ocp
[9] = cs
->cs_this
;
479 ocp
[0] |= SL_TYPE_UNCOMPRESSED_TCP
;
485 slhc_uncompress(struct slcompress
*comp
, unsigned char *icp
, int isize
)
487 register int changes
;
489 register struct tcphdr
*thp
;
490 register struct iphdr
*ip
;
491 register struct cstate
*cs
;
493 unsigned char *cp
= icp
;
495 /* We've got a compressed packet; read the change byte */
496 comp
->sls_i_compressed
++;
503 /* Make sure the state index is in range, then grab the state.
504 * If we have a good state index, clear the 'discard' flag.
506 x
= *cp
++; /* Read conn index */
507 if(x
< 0 || x
> comp
->rslot_limit
)
510 comp
->flags
&=~ SLF_TOSS
;
511 comp
->recv_current
= x
;
513 /* this packet has an implicit state index. If we've
514 * had a line error since the last time we got an
515 * explicit state index, we have to toss the packet. */
516 if(comp
->flags
& SLF_TOSS
){
517 comp
->sls_i_tossed
++;
521 cs
= &comp
->rstate
[comp
->recv_current
];
525 thp
->check
= *(__sum16
*)cp
;
528 thp
->psh
= (changes
& TCP_PUSH_BIT
) ? 1 : 0;
530 * we can use the same number for the length of the saved header and
531 * the current one, because the packet wouldn't have been sent
532 * as compressed unless the options were the same as the previous one
535 hdrlen
= ip
->ihl
* 4 + thp
->doff
* 4;
537 switch(changes
& SPECIALS_MASK
){
538 case SPECIAL_I
: /* Echoed terminal traffic */
541 i
= ntohs(ip
->tot_len
) - hdrlen
;
542 thp
->ack_seq
= htonl( ntohl(thp
->ack_seq
) + i
);
543 thp
->seq
= htonl( ntohl(thp
->seq
) + i
);
547 case SPECIAL_D
: /* Unidirectional data */
548 thp
->seq
= htonl( ntohl(thp
->seq
) +
549 ntohs(ip
->tot_len
) - hdrlen
);
555 if((x
= decode(&cp
)) == -1) {
558 thp
->urg_ptr
= htons(x
);
562 if((x
= decode(&cp
)) == -1) {
565 thp
->window
= htons( ntohs(thp
->window
) + x
);
568 if((x
= decode(&cp
)) == -1) {
571 thp
->ack_seq
= htonl( ntohl(thp
->ack_seq
) + x
);
574 if((x
= decode(&cp
)) == -1) {
577 thp
->seq
= htonl( ntohl(thp
->seq
) + x
);
582 if((x
= decode(&cp
)) == -1) {
585 ip
->id
= htons (ntohs (ip
->id
) + x
);
587 ip
->id
= htons (ntohs (ip
->id
) + 1);
590 * At this point, cp points to the first byte of data in the
591 * packet. Put the reconstructed TCP and IP headers back on the
592 * packet. Recalculate IP checksum (but not TCP checksum).
595 len
= isize
- (cp
- icp
);
599 ip
->tot_len
= htons(len
);
602 memmove(icp
+ hdrlen
, cp
, len
- hdrlen
);
609 memcpy(cp
, cs
->cs_ipopt
, (ip
->ihl
- 5) * 4);
610 cp
+= (ip
->ihl
- 5) * 4;
613 put_unaligned(ip_fast_csum(icp
, ip
->ihl
),
614 &((struct iphdr
*)icp
)->check
);
620 memcpy(cp
, cs
->cs_tcpopt
, ((thp
->doff
) - 5) * 4);
621 cp
+= ((thp
->doff
) - 5) * 4;
627 return slhc_toss( comp
);
632 slhc_remember(struct slcompress
*comp
, unsigned char *icp
, int isize
)
634 register struct cstate
*cs
;
640 /* The packet is shorter than a legal IP header */
642 return slhc_toss( comp
);
644 /* Peek at the IP header's IHL field to find its length */
647 /* The IP header length field is too small */
649 return slhc_toss( comp
);
652 icp
[9] = IPPROTO_TCP
;
654 if (ip_fast_csum(icp
, ihl
)) {
655 /* Bad IP header checksum; discard */
656 comp
->sls_i_badcheck
++;
657 return slhc_toss( comp
);
659 if(index
> comp
->rslot_limit
) {
661 return slhc_toss(comp
);
664 /* Update local state */
665 cs
= &comp
->rstate
[comp
->recv_current
= index
];
666 comp
->flags
&=~ SLF_TOSS
;
667 memcpy(&cs
->cs_ip
,icp
,20);
668 memcpy(&cs
->cs_tcp
,icp
+ ihl
*4,20);
670 memcpy(cs
->cs_ipopt
, icp
+ sizeof(struct iphdr
), (ihl
- 5) * 4);
671 if (cs
->cs_tcp
.doff
> 5)
672 memcpy(cs
->cs_tcpopt
, icp
+ ihl
*4 + sizeof(struct tcphdr
), (cs
->cs_tcp
.doff
- 5) * 4);
673 cs
->cs_hsize
= ihl
*2 + cs
->cs_tcp
.doff
*2;
674 /* Put headers back on packet
675 * Neither header checksum is recalculated
677 comp
->sls_i_uncompressed
++;
682 slhc_toss(struct slcompress
*comp
)
684 if ( comp
== NULLSLCOMPR
)
687 comp
->flags
|= SLF_TOSS
;
692 /* VJ header compression */
693 EXPORT_SYMBOL(slhc_init
);
694 EXPORT_SYMBOL(slhc_free
);
695 EXPORT_SYMBOL(slhc_remember
);
696 EXPORT_SYMBOL(slhc_compress
);
697 EXPORT_SYMBOL(slhc_uncompress
);
698 EXPORT_SYMBOL(slhc_toss
);
700 #else /* CONFIG_INET */
704 slhc_toss(struct slcompress
*comp
)
706 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_toss");
710 slhc_uncompress(struct slcompress
*comp
, unsigned char *icp
, int isize
)
712 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_uncompress");
716 slhc_compress(struct slcompress
*comp
, unsigned char *icp
, int isize
,
717 unsigned char *ocp
, unsigned char **cpp
, int compress_cid
)
719 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_compress");
724 slhc_remember(struct slcompress
*comp
, unsigned char *icp
, int isize
)
726 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_remember");
731 slhc_free(struct slcompress
*comp
)
733 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_free");
736 slhc_init(int rslots
, int tslots
)
738 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_init");
741 EXPORT_SYMBOL(slhc_init
);
742 EXPORT_SYMBOL(slhc_free
);
743 EXPORT_SYMBOL(slhc_remember
);
744 EXPORT_SYMBOL(slhc_compress
);
745 EXPORT_SYMBOL(slhc_uncompress
);
746 EXPORT_SYMBOL(slhc_toss
);
748 #endif /* CONFIG_INET */
749 MODULE_LICENSE("Dual BSD/GPL");