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 <linux/uaccess.h>
79 #include <net/checksum.h>
80 #include <asm/unaligned.h>
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
);
87 /* Allocate compression data structure
88 * slots must be in range 0 to 255 (zero meaning no compression)
89 * Returns pointer to structure or ERR_PTR() on error.
92 slhc_init(int rslots
, int tslots
)
95 register struct cstate
*ts
;
96 struct slcompress
*comp
;
98 if (rslots
< 0 || rslots
> 255 || tslots
< 0 || tslots
> 255)
99 return ERR_PTR(-EINVAL
);
101 comp
= kzalloc(sizeof(struct slcompress
), GFP_KERNEL
);
106 size_t rsize
= rslots
* sizeof(struct cstate
);
107 comp
->rstate
= kzalloc(rsize
, GFP_KERNEL
);
110 comp
->rslot_limit
= rslots
- 1;
114 size_t tsize
= tslots
* sizeof(struct cstate
);
115 comp
->tstate
= kzalloc(tsize
, GFP_KERNEL
);
118 comp
->tslot_limit
= tslots
- 1;
121 comp
->xmit_oldest
= 0;
122 comp
->xmit_current
= 255;
123 comp
->recv_current
= 255;
125 * don't accept any packets with implicit index until we get
126 * one with an explicit index. Otherwise the uncompress code
127 * will try to use connection 255, which is almost certainly
130 comp
->flags
|= SLF_TOSS
;
134 for(i
= comp
->tslot_limit
; i
> 0; --i
){
136 ts
[i
].next
= &(ts
[i
- 1]);
138 ts
[0].next
= &(ts
[comp
->tslot_limit
]);
148 return ERR_PTR(-ENOMEM
);
152 /* Free a compression data structure */
154 slhc_free(struct slcompress
*comp
)
156 if ( comp
== NULLSLCOMPR
)
159 if ( comp
->tstate
!= NULLSLSTATE
)
160 kfree( comp
->tstate
);
162 if ( comp
->rstate
!= NULLSLSTATE
)
163 kfree( comp
->rstate
);
169 /* Put a short in host order into a char array in network order */
170 static inline unsigned char *
171 put16(unsigned char *cp
, unsigned short x
)
180 /* Encode a number */
181 static unsigned char *
182 encode(unsigned char *cp
, unsigned short n
)
184 if(n
>= 256 || n
== 0){
193 /* Pull a 16-bit integer in host order from buffer in network byte order */
194 static unsigned short
195 pull16(unsigned char **cpp
)
205 /* Decode a number */
207 decode(unsigned char **cpp
)
213 return pull16(cpp
) & 0xffff; /* pull16 returns -1 on error */
215 return x
& 0xff; /* -1 if PULLCHAR returned error */
220 * icp and isize are the original packet.
221 * ocp is a place to put a copy if necessary.
222 * cpp is initially a pointer to icp. If the copy is used,
227 slhc_compress(struct slcompress
*comp
, unsigned char *icp
, int isize
,
228 unsigned char *ocp
, unsigned char **cpp
, int compress_cid
)
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;
236 unsigned char new_seq
[16];
237 register unsigned char *cp
= new_seq
;
239 struct tcphdr
*th
, *oth
;
244 * Don't play with runt packets.
247 if(isize
<sizeof(struct iphdr
))
250 ip
= (struct iphdr
*) icp
;
252 /* Bail if this packet isn't TCP, or is an IP fragment */
253 if (ip
->protocol
!= IPPROTO_TCP
|| (ntohs(ip
->frag_off
) & 0x3fff)) {
254 /* Send as regular IP */
255 if(ip
->protocol
!= IPPROTO_TCP
)
256 comp
->sls_o_nontcp
++;
261 /* Extract TCP header */
263 th
= (struct tcphdr
*)(((unsigned char *)ip
) + ip
->ihl
*4);
264 hlen
= ip
->ihl
*4 + th
->doff
*4;
266 /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
267 * some other control bit is set). Also uncompressible if
270 if(hlen
> isize
|| th
->syn
|| th
->fin
|| th
->rst
||
272 /* TCP connection stuff; send as regular IP */
277 * Packet is compressible -- we're going to send either a
278 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
279 * we need to locate (or create) the connection state.
281 * States are kept in a circularly linked list with
282 * xmit_oldest pointing to the end of the list. The
283 * list is kept in lru order by moving a state to the
284 * head of the list whenever it is referenced. Since
285 * the list is short and, empirically, the connection
286 * we want is almost always near the front, we locate
287 * states via linear search. If we don't find a state
288 * for the datagram, the oldest state is (re-)used.
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
)
297 /* if current equal oldest, at end of list */
302 comp
->sls_o_searches
++;
305 * Didn't find it -- re-use oldest cstate. Send an
306 * uncompressed packet that tells the other side what
307 * connection number we're using for this conversation.
309 * Note that since the state list is circular, the oldest
310 * state points to the newest and we only need to set
311 * xmit_oldest to update the lru linkage.
313 comp
->sls_o_misses
++;
314 comp
->xmit_oldest
= lcs
->cs_this
;
319 * Found it -- move to the front on the connection list.
322 /* found at most recently used */
323 } else if (cs
== ocs
) {
324 /* found at least recently used */
325 comp
->xmit_oldest
= lcs
->cs_this
;
327 /* more than 2 elements */
328 lcs
->next
= cs
->next
;
329 cs
->next
= ocs
->next
;
334 * Make sure that only what we expect to change changed.
335 * Check the following:
336 * IP protocol version, header length & type of service.
337 * The "Don't fragment" bit.
338 * The time-to-live field.
339 * The TCP header length.
340 * IP options, if any.
341 * TCP options, if any.
342 * If any of these things are different between the previous &
343 * current datagram, we send the current datagram `uncompressed'.
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)){
358 * Figure out which of the changing fields changed. The
359 * receiver expects changes in the order: urgent, window,
360 * ack, seq (the order minimizes the number of temporaries
361 * needed in this section of code).
364 deltaS
= ntohs(th
->urg_ptr
);
365 cp
= encode(cp
,deltaS
);
367 } else if(th
->urg_ptr
!= oth
->urg_ptr
){
368 /* argh! URG not set but urp changed -- a sensible
369 * implementation should never do this but RFC793
370 * doesn't prohibit the change so we have to deal
374 if((deltaS
= ntohs(th
->window
) - ntohs(oth
->window
)) != 0){
375 cp
= encode(cp
,deltaS
);
378 if((deltaA
= ntohl(th
->ack_seq
) - ntohl(oth
->ack_seq
)) != 0L){
379 if(deltaA
> 0x0000ffff)
381 cp
= encode(cp
,deltaA
);
384 if((deltaS
= ntohl(th
->seq
) - ntohl(oth
->seq
)) != 0L){
385 if(deltaS
> 0x0000ffff)
387 cp
= encode(cp
,deltaS
);
392 case 0: /* Nothing changed. If this packet contains data and the
393 * last one didn't, this is probably a data packet following
394 * an ack (normal on an interactive connection) and we send
395 * it compressed. Otherwise it's probably a retransmit,
396 * retransmitted ack or window probe. Send it uncompressed
397 * in case the other side missed the compressed version.
399 if(ip
->tot_len
!= cs
->cs_ip
.tot_len
&&
400 ntohs(cs
->cs_ip
.tot_len
) == hlen
)
405 /* actual changes match one of our special case encodings --
406 * send packet uncompressed.
410 if(deltaS
== deltaA
&&
411 deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
412 /* special case for echoed terminal traffic */
418 if(deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
419 /* special case for data xfer */
425 deltaS
= ntohs(ip
->id
) - ntohs(cs
->cs_ip
.id
);
427 cp
= encode(cp
,deltaS
);
431 changes
|= TCP_PUSH_BIT
;
432 /* Grab the cksum before we overwrite it below. Then update our
433 * state with this packet's header.
436 memcpy(&cs
->cs_ip
,ip
,20);
437 memcpy(&cs
->cs_tcp
,th
,20);
438 /* We want to use the original packet as our compressed packet.
439 * (cp - new_seq) is the number of bytes we need for compressed
440 * sequence numbers. In addition we need one byte for the change
441 * mask, one for the connection id and two for the tcp checksum.
442 * So, (cp - new_seq) + 4 bytes of header are needed.
444 deltaS
= cp
- new_seq
;
445 if(compress_cid
== 0 || comp
->xmit_current
!= cs
->cs_this
){
448 *cp
++ = changes
| NEW_C
;
450 comp
->xmit_current
= cs
->cs_this
;
456 *(__sum16
*)cp
= csum
;
458 /* deltaS is now the size of the change section of the compressed header */
459 memcpy(cp
,new_seq
,deltaS
); /* Write list of 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
);
465 /* Update connection state cs & send uncompressed packet (i.e.,
466 * a regular ip/tcp packet but with the 'conversation id' we hope
467 * to use on future compressed packets in the protocol field).
470 memcpy(&cs
->cs_ip
,ip
,20);
471 memcpy(&cs
->cs_tcp
,th
,20);
473 memcpy(cs
->cs_ipopt
, ip
+1, ((ip
->ihl
) - 5) * 4);
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
);
480 ocp
[9] = cs
->cs_this
;
481 ocp
[0] |= SL_TYPE_UNCOMPRESSED_TCP
;
487 slhc_uncompress(struct slcompress
*comp
, unsigned char *icp
, int isize
)
489 register int changes
;
491 register struct tcphdr
*thp
;
492 register struct iphdr
*ip
;
493 register struct cstate
*cs
;
495 unsigned char *cp
= icp
;
497 /* We've got a compressed packet; read the change byte */
498 comp
->sls_i_compressed
++;
505 /* Make sure the state index is in range, then grab the state.
506 * If we have a good state index, clear the 'discard' flag.
508 x
= *cp
++; /* Read conn index */
509 if(x
< 0 || x
> comp
->rslot_limit
)
512 /* Check if the cstate is initialized */
513 if (!comp
->rstate
[x
].initialized
)
516 comp
->flags
&=~ SLF_TOSS
;
517 comp
->recv_current
= x
;
519 /* this packet has an implicit state index. If we've
520 * had a line error since the last time we got an
521 * explicit state index, we have to toss the packet. */
522 if(comp
->flags
& SLF_TOSS
){
523 comp
->sls_i_tossed
++;
527 cs
= &comp
->rstate
[comp
->recv_current
];
531 thp
->check
= *(__sum16
*)cp
;
534 thp
->psh
= (changes
& TCP_PUSH_BIT
) ? 1 : 0;
536 * we can use the same number for the length of the saved header and
537 * the current one, because the packet wouldn't have been sent
538 * as compressed unless the options were the same as the previous one
541 hdrlen
= ip
->ihl
* 4 + thp
->doff
* 4;
543 switch(changes
& SPECIALS_MASK
){
544 case SPECIAL_I
: /* Echoed terminal traffic */
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
);
553 case SPECIAL_D
: /* Unidirectional data */
554 thp
->seq
= htonl( ntohl(thp
->seq
) +
555 ntohs(ip
->tot_len
) - hdrlen
);
561 if((x
= decode(&cp
)) == -1) {
564 thp
->urg_ptr
= htons(x
);
568 if((x
= decode(&cp
)) == -1) {
571 thp
->window
= htons( ntohs(thp
->window
) + x
);
574 if((x
= decode(&cp
)) == -1) {
577 thp
->ack_seq
= htonl( ntohl(thp
->ack_seq
) + x
);
580 if((x
= decode(&cp
)) == -1) {
583 thp
->seq
= htonl( ntohl(thp
->seq
) + x
);
588 if((x
= decode(&cp
)) == -1) {
591 ip
->id
= htons (ntohs (ip
->id
) + x
);
593 ip
->id
= htons (ntohs (ip
->id
) + 1);
596 * At this point, cp points to the first byte of data in the
597 * packet. Put the reconstructed TCP and IP headers back on the
598 * packet. Recalculate IP checksum (but not TCP checksum).
601 len
= isize
- (cp
- icp
);
605 ip
->tot_len
= htons(len
);
608 memmove(icp
+ hdrlen
, cp
, len
- hdrlen
);
615 memcpy(cp
, cs
->cs_ipopt
, (ip
->ihl
- 5) * 4);
616 cp
+= (ip
->ihl
- 5) * 4;
619 put_unaligned(ip_fast_csum(icp
, ip
->ihl
),
620 &((struct iphdr
*)icp
)->check
);
626 memcpy(cp
, cs
->cs_tcpopt
, ((thp
->doff
) - 5) * 4);
627 cp
+= ((thp
->doff
) - 5) * 4;
633 return slhc_toss( comp
);
638 slhc_remember(struct slcompress
*comp
, unsigned char *icp
, int isize
)
640 register struct cstate
*cs
;
646 /* The packet is shorter than a legal IP header */
648 return slhc_toss( comp
);
650 /* Peek at the IP header's IHL field to find its length */
653 /* The IP header length field is too small */
655 return slhc_toss( comp
);
658 icp
[9] = IPPROTO_TCP
;
660 if (ip_fast_csum(icp
, ihl
)) {
661 /* Bad IP header checksum; discard */
662 comp
->sls_i_badcheck
++;
663 return slhc_toss( comp
);
665 if(index
> comp
->rslot_limit
) {
667 return slhc_toss(comp
);
670 /* Update local state */
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);
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 /* Put headers back on packet
682 * Neither header checksum is recalculated
684 comp
->sls_i_uncompressed
++;
689 slhc_toss(struct slcompress
*comp
)
691 if ( comp
== NULLSLCOMPR
)
694 comp
->flags
|= SLF_TOSS
;
698 #else /* CONFIG_INET */
701 slhc_toss(struct slcompress
*comp
)
703 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_toss");
707 slhc_uncompress(struct slcompress
*comp
, unsigned char *icp
, int isize
)
709 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_uncompress");
713 slhc_compress(struct slcompress
*comp
, unsigned char *icp
, int isize
,
714 unsigned char *ocp
, unsigned char **cpp
, int compress_cid
)
716 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_compress");
721 slhc_remember(struct slcompress
*comp
, unsigned char *icp
, int isize
)
723 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_remember");
728 slhc_free(struct slcompress
*comp
)
730 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_free");
733 slhc_init(int rslots
, int tslots
)
735 printk(KERN_DEBUG
"Called IP function on non IP-system: slhc_init");
739 #endif /* CONFIG_INET */
741 /* VJ header compression */
742 EXPORT_SYMBOL(slhc_init
);
743 EXPORT_SYMBOL(slhc_free
);
744 EXPORT_SYMBOL(slhc_remember
);
745 EXPORT_SYMBOL(slhc_compress
);
746 EXPORT_SYMBOL(slhc_uncompress
);
747 EXPORT_SYMBOL(slhc_toss
);
749 MODULE_LICENSE("Dual BSD/GPL");