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 MERCHANTIBILITY 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
41 #include <linux/types.h>
42 #include <linux/sched.h>
44 #include <linux/string.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/termios.h>
49 #include <linux/fcntl.h>
59 #include <linux/errno.h>
60 #include <linux/timer.h>
61 #include <asm/system.h>
62 #include <asm/segment.h>
70 static unsigned char *encode(unsigned char *cp
,int n
);
71 static long decode(unsigned char **cpp
);
72 static unsigned char * put16(unsigned char *cp
, unsigned short x
);
73 static unsigned short pull16(unsigned char **cpp
);
75 extern int ip_csum(struct iphdr
*iph
);
78 /* Initialize compression data structure
79 * slots must be in range 0 to 255 (zero meaning no compression)
82 slhc_init(int rslots
, int tslots
)
85 register struct cstate
*ts
;
86 struct slcompress
*comp
;
88 comp
= (struct slcompress
*)kmalloc(sizeof(struct slcompress
),
93 memset(comp
, 0, sizeof(struct slcompress
));
95 if ( rslots
> 0 && rslots
< 256 ) {
97 (struct cstate
*)kmalloc(rslots
* sizeof(struct cstate
),
101 memset(comp
->rstate
, 0, rslots
* sizeof(struct cstate
));
102 comp
->rslot_limit
= rslots
- 1;
105 if ( tslots
> 0 && tslots
< 256 ) {
107 (struct cstate
*)kmalloc(tslots
* sizeof(struct cstate
),
111 memset(comp
->tstate
, 0, rslots
* sizeof(struct cstate
));
112 comp
->tslot_limit
= tslots
- 1;
115 comp
->xmit_oldest
= 0;
116 comp
->xmit_current
= 255;
117 comp
->recv_current
= 255;
119 * don't accept any packets with implicit index until we get
120 * one with an explicit index. Otherwise the uncompress code
121 * will try to use connection 255, which is almost certainly
124 comp
->flags
|= SLF_TOSS
;
128 for(i
= comp
->tslot_limit
; i
> 0; --i
){
130 ts
[i
].next
= &(ts
[i
- 1]);
132 ts
[0].next
= &(ts
[comp
->tslot_limit
]);
139 /* Free a compression data structure */
141 slhc_free(struct slcompress
*comp
)
143 if ( comp
== NULLSLCOMPR
)
146 if ( comp
->rstate
!= NULLSLSTATE
)
147 kfree( comp
->rstate
);
149 if ( comp
->tstate
!= NULLSLSTATE
)
150 kfree( comp
->tstate
);
156 /* Put a short in host order into a char array in network order */
157 static unsigned char *
158 put16(unsigned char *cp
, unsigned short x
)
167 /* Encode a number */
169 encode(unsigned char *cp
, int n
)
171 if(n
>= 256 || n
== 0){
180 /* Pull a 16-bit integer in host order from buffer in network byte order */
181 static unsigned short
182 pull16(unsigned char **cpp
)
192 /* Decode a number */
194 decode(unsigned char **cpp
)
200 return pull16(cpp
) & 0xffff; /* pull16 returns -1 on error */
202 return x
& 0xff; /* -1 if PULLCHAR returned error */
207 * icp and isize are the original packet.
208 * ocp is a place to put a copy if necessary.
209 * cpp is initially a pointer to icp. If the copy is used,
214 slhc_compress(struct slcompress
*comp
, unsigned char *icp
, int isize
,
215 unsigned char *ocp
, unsigned char **cpp
, int compress_cid
)
217 register struct cstate
*ocs
= &(comp
->tstate
[comp
->xmit_oldest
]);
218 register struct cstate
*lcs
= ocs
;
219 register struct cstate
*cs
= lcs
->next
;
220 register unsigned long deltaS
, deltaA
;
221 register short changes
= 0;
223 unsigned char new_seq
[16];
224 register unsigned char *cp
= new_seq
;
226 struct tcphdr
*th
, *oth
;
228 ip
= (struct iphdr
*) icp
;
230 /* Bail if this packet isn't TCP, or is an IP fragment */
231 if(ip
->protocol
!= IPPROTO_TCP
|| (ntohs(ip
->frag_off
) & 0x1fff) ||
232 (ip
->frag_off
& 32)){
233 DPRINT(("comp: noncomp 1 %d %d %d\n", ip
->protocol
,
234 ntohs(ip
->frag_off
), ip
->frag_off
));
235 /* Send as regular IP */
236 if(ip
->protocol
!= IPPROTO_TCP
)
237 comp
->sls_o_nontcp
++;
242 /* Extract TCP header */
244 th
= (struct tcphdr
*)(((unsigned char *)ip
) + ip
->ihl
*4);
245 hlen
= ip
->ihl
*4 + th
->doff
*4;
247 /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
248 * some other control bit is set).
250 if(th
->syn
|| th
->fin
|| th
->rst
||
252 DPRINT(("comp: noncomp 2 %x %x %d %d %d %d\n", ip
, th
,
253 th
->syn
, th
->fin
, th
->rst
, th
->ack
));
254 /* TCP connection stuff; send as regular IP */
259 * Packet is compressible -- we're going to send either a
260 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
261 * we need to locate (or create) the connection state.
263 * States are kept in a circularly linked list with
264 * xmit_oldest pointing to the end of the list. The
265 * list is kept in lru order by moving a state to the
266 * head of the list whenever it is referenced. Since
267 * the list is short and, empirically, the connection
268 * we want is almost always near the front, we locate
269 * states via linear search. If we don't find a state
270 * for the datagram, the oldest state is (re-)used.
273 if( ip
->saddr
== cs
->cs_ip
.saddr
274 && ip
->daddr
== cs
->cs_ip
.daddr
275 && th
->source
== cs
->cs_tcp
.source
276 && th
->dest
== cs
->cs_tcp
.dest
)
279 /* if current equal oldest, at end of list */
284 comp
->sls_o_searches
++;
287 * Didn't find it -- re-use oldest cstate. Send an
288 * uncompressed packet that tells the other side what
289 * connection number we're using for this conversation.
291 * Note that since the state list is circular, the oldest
292 * state points to the newest and we only need to set
293 * xmit_oldest to update the lru linkage.
295 comp
->sls_o_misses
++;
296 comp
->xmit_oldest
= lcs
->cs_this
;
297 DPRINT(("comp: not found\n"));
302 * Found it -- move to the front on the connection list.
305 /* found at most recently used */
306 } else if (cs
== ocs
) {
307 /* found at least recently used */
308 comp
->xmit_oldest
= lcs
->cs_this
;
310 /* more than 2 elements */
311 lcs
->next
= cs
->next
;
312 cs
->next
= ocs
->next
;
317 * Make sure that only what we expect to change changed.
318 * Check the following:
319 * IP protocol version, header length & type of service.
320 * The "Don't fragment" bit.
321 * The time-to-live field.
322 * The TCP header length.
323 * IP options, if any.
324 * TCP options, if any.
325 * If any of these things are different between the previous &
326 * current datagram, we send the current datagram `uncompressed'.
331 || ip
->version
!= cs
->cs_ip
.version
|| ip
->ihl
!= cs
->cs_ip
.ihl
332 || ip
->tos
!= cs
->cs_ip
.tos
333 || (ip
->frag_off
& 64) != (cs
->cs_ip
.frag_off
& 64)
334 || ip
->ttl
!= cs
->cs_ip
.ttl
335 || th
->doff
!= cs
->cs_tcp
.doff
336 || (ip
->ihl
> 5 && memcmp(ip
+1,cs
->cs_ipopt
,((ip
->ihl
)-5)*4) != 0)
337 || (th
->doff
> 5 && memcmp(th
+1,cs
->cs_tcpopt
,((th
->doff
)-5)*4 != 0))){
338 DPRINT(("comp: incompat\n"));
343 * Figure out which of the changing fields changed. The
344 * receiver expects changes in the order: urgent, window,
345 * ack, seq (the order minimizes the number of temporaries
346 * needed in this section of code).
349 deltaS
= ntohs(th
->urg_ptr
);
350 cp
= encode(cp
,deltaS
);
352 } else if(th
->urg_ptr
!= oth
->urg_ptr
){
353 /* argh! URG not set but urp changed -- a sensible
354 * implementation should never do this but RFC793
355 * doesn't prohibit the change so we have to deal
357 DPRINT(("comp: urg incompat\n"));
360 if((deltaS
= ntohs(th
->window
) - ntohs(oth
->window
)) != 0){
361 cp
= encode(cp
,deltaS
);
364 if((deltaA
= ntohl(th
->ack_seq
) - ntohl(oth
->ack_seq
)) != 0L){
365 if(deltaA
> 0x0000ffff)
367 cp
= encode(cp
,deltaA
);
370 if((deltaS
= ntohl(th
->seq
) - ntohl(oth
->seq
)) != 0L){
371 if(deltaS
> 0x0000ffff)
373 cp
= encode(cp
,deltaS
);
378 case 0: /* Nothing changed. If this packet contains data and the
379 * last one didn't, this is probably a data packet following
380 * an ack (normal on an interactive connection) and we send
381 * it compressed. Otherwise it's probably a retransmit,
382 * retransmitted ack or window probe. Send it uncompressed
383 * in case the other side missed the compressed version.
385 if(ip
->tot_len
!= cs
->cs_ip
.tot_len
&&
386 ntohs(cs
->cs_ip
.tot_len
) == hlen
)
388 DPRINT(("comp: retrans\n"));
393 /* actual changes match one of our special case encodings --
394 * send packet uncompressed.
396 DPRINT(("comp: special\n"));
399 if(deltaS
== deltaA
&&
400 deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
401 /* special case for echoed terminal traffic */
407 if(deltaS
== ntohs(cs
->cs_ip
.tot_len
) - hlen
){
408 /* special case for data xfer */
414 deltaS
= ntohs(ip
->id
) - ntohs(cs
->cs_ip
.id
);
416 cp
= encode(cp
,deltaS
);
420 changes
|= TCP_PUSH_BIT
;
421 /* Grab the cksum before we overwrite it below. Then update our
422 * state with this packet's header.
424 deltaA
= ntohs(th
->check
);
425 memcpy(&cs
->cs_ip
,ip
,20);
426 memcpy(&cs
->cs_tcp
,th
,20);
427 /* We want to use the original packet as our compressed packet.
428 * (cp - new_seq) is the number of bytes we need for compressed
429 * sequence numbers. In addition we need one byte for the change
430 * mask, one for the connection id and two for the tcp checksum.
431 * So, (cp - new_seq) + 4 bytes of header are needed.
433 deltaS
= cp
- new_seq
;
434 if(compress_cid
== 0 || comp
->xmit_current
!= cs
->cs_this
){
437 *cp
++ = changes
| NEW_C
;
439 comp
->xmit_current
= cs
->cs_this
;
445 cp
= put16(cp
,(short)deltaA
); /* Write TCP checksum */
446 /* deltaS is now the size of the change section of the compressed header */
447 DPRINT(("comp: %x %x %x %d %d\n", icp
, cp
, new_seq
, hlen
, deltaS
));
448 memcpy(cp
,new_seq
,deltaS
); /* Write list of deltas */
449 memcpy(cp
+deltaS
,icp
+hlen
,isize
-hlen
);
450 comp
->sls_o_compressed
++;
451 ocp
[0] |= SL_TYPE_COMPRESSED_TCP
;
452 return isize
- hlen
+ deltaS
+ (cp
- ocp
);
454 /* Update connection state cs & send uncompressed packet (i.e.,
455 * a regular ip/tcp packet but with the 'conversation id' we hope
456 * to use on future compressed packets in the protocol field).
459 memcpy(&cs
->cs_ip
,ip
,20);
460 memcpy(&cs
->cs_tcp
,th
,20);
462 memcpy(cs
->cs_ipopt
, ip
+1, ((ip
->ihl
) - 5) * 4);
464 memcpy(cs
->cs_tcpopt
, th
+1, ((th
->doff
) - 5) * 4);
465 comp
->xmit_current
= cs
->cs_this
;
466 comp
->sls_o_uncompressed
++;
467 memcpy(ocp
, icp
, isize
);
469 ocp
[9] = cs
->cs_this
;
470 ocp
[0] |= SL_TYPE_UNCOMPRESSED_TCP
;
476 slhc_uncompress(struct slcompress
*comp
, unsigned char *icp
, int isize
)
478 register int changes
;
480 register struct tcphdr
*thp
;
481 register struct iphdr
*ip
;
482 register struct cstate
*cs
;
484 unsigned char *cp
= icp
;
486 /* We've got a compressed packet; read the change byte */
487 comp
->sls_i_compressed
++;
490 DPRINT(("uncomp: runt\n"));
495 /* Make sure the state index is in range, then grab the state.
496 * If we have a good state index, clear the 'discard' flag.
498 x
= *cp
++; /* Read conn index */
499 if(x
< 0 || x
> comp
->rslot_limit
)
502 comp
->flags
&=~ SLF_TOSS
;
503 comp
->recv_current
= x
;
505 /* this packet has an implicit state index. If we've
506 * had a line error since the last time we got an
507 * explicit state index, we have to toss the packet. */
508 if(comp
->flags
& SLF_TOSS
){
509 comp
->sls_i_tossed
++;
510 DPRINT(("uncomp: toss\n"));
514 cs
= &comp
->rstate
[comp
->recv_current
];
518 if((x
= pull16(&cp
)) == -1) { /* Read the TCP checksum */
519 DPRINT(("uncomp: bad tcp chk\n"));
522 thp
->check
= htons(x
);
524 thp
->psh
= (changes
& TCP_PUSH_BIT
) ? 1 : 0;
526 * we can use the same number for the length of the saved header and
527 * the current one, because the packet wouldn't have been sent
528 * as compressed unless the options were the same as the previous one
531 hdrlen
= ip
->ihl
* 4 + thp
->doff
* 4;
533 switch(changes
& SPECIALS_MASK
){
534 case SPECIAL_I
: /* Echoed terminal traffic */
537 i
= ntohs(ip
->tot_len
) - hdrlen
;
538 thp
->ack_seq
= htonl( ntohl(thp
->ack_seq
) + i
);
539 thp
->seq
= htonl( ntohl(thp
->seq
) + i
);
543 case SPECIAL_D
: /* Unidirectional data */
544 thp
->seq
= htonl( ntohl(thp
->seq
) +
545 ntohs(ip
->tot_len
) - hdrlen
);
551 if((x
= decode(&cp
)) == -1) {
552 DPRINT(("uncomp: bad U\n"));
555 thp
->urg_ptr
= htons(x
);
559 if((x
= decode(&cp
)) == -1) {
560 DPRINT(("uncomp: bad W\n"));
563 thp
->window
= htons( ntohs(thp
->window
) + x
);
566 if((x
= decode(&cp
)) == -1) {
567 DPRINT(("uncomp: bad A\n"));
570 thp
->ack_seq
= htonl( ntohl(thp
->ack_seq
) + x
);
573 if((x
= decode(&cp
)) == -1) {
574 DPRINT(("uncomp: bad S\n"));
577 thp
->seq
= htonl( ntohl(thp
->seq
) + x
);
582 if((x
= decode(&cp
)) == -1) {
583 DPRINT(("uncomp: bad I\n"));
586 ip
->id
= htons (ntohs (ip
->id
) + x
);
588 ip
->id
= htons (ntohs (ip
->id
) + 1);
591 * At this point, cp points to the first byte of data in the
592 * packet. Put the reconstructed TCP and IP headers back on the
593 * packet. Recalculate IP checksum (but not TCP checksum).
596 len
= isize
- (cp
- icp
);
600 ip
->tot_len
= htons(len
);
603 DPRINT(("uncomp: %d %d %d %d\n", cp
- icp
, hdrlen
, isize
, len
));
605 memmove(icp
+ hdrlen
, cp
, len
- hdrlen
);
612 memcpy(cp
, cs
->cs_ipopt
, ((ip
->ihl
) - 5) * 4);
613 cp
+= ((ip
->ihl
) - 5) * 4;
616 ((struct iphdr
*)icp
)->check
= ip_csum((struct iphdr
*)icp
);
622 memcpy(cp
, cs
->cs_tcpopt
, ((thp
->doff
) - 5) * 4);
623 cp
+= ((thp
->doff
) - 5) * 4;
626 if (inet_debug
== DBG_SLIP
) printk("\runcomp: change %x len %d\n", changes
, len
);
630 return slhc_toss( comp
);
635 slhc_remember(struct slcompress
*comp
, unsigned char *icp
, int isize
)
637 register struct cstate
*cs
;
645 /* The packet is shorter than a legal IP header */
647 return slhc_toss( comp
);
649 /* Sneak a peek at the IP header's IHL field to find its length */
650 ip_len
= (icp
[0] & 0xf) << 2;
652 /* The IP header length field is too small */
654 return slhc_toss( comp
);
657 icp
[9] = IPPROTO_TCP
;
658 ip
= (struct iphdr
*) icp
;
661 /* Bad IP header checksum; discard */
662 comp
->sls_i_badcheck
++;
663 return slhc_toss( comp
);
665 thp
= (struct tcphdr
*)(((unsigned char *)ip
) + ip
->ihl
*4);
666 if(index
> comp
->rslot_limit
) {
668 return slhc_toss(comp
);
671 /* Update local state */
672 cs
= &comp
->rstate
[comp
->recv_current
= index
];
673 comp
->flags
&=~ SLF_TOSS
;
674 memcpy(&cs
->cs_ip
,ip
,20);
675 memcpy(&cs
->cs_tcp
,thp
,20);
677 memcpy(cs
->cs_ipopt
, ip
+1, ((ip
->ihl
) - 5) * 4);
679 memcpy(cs
->cs_tcpopt
, thp
+1, ((thp
->doff
) - 5) * 4);
680 cs
->cs_hsize
= ip
->ihl
*2 + thp
->doff
*2;
681 /* Put headers back on packet
682 * Neither header checksum is recalculated
684 comp
->sls_i_uncompressed
++;
690 slhc_toss(struct slcompress
*comp
)
692 if ( comp
== NULLSLCOMPR
)
695 comp
->flags
|= SLF_TOSS
;
700 void slhc_i_status(struct slcompress
*comp
)
702 if (comp
!= NULLSLCOMPR
) {
703 printk("\t%ld Cmp, %ld Uncmp, %ld Bad, %ld Tossed\n",
704 comp
->sls_i_compressed
,
705 comp
->sls_i_uncompressed
,
712 void slhc_o_status(struct slcompress
*comp
)
714 if (comp
!= NULLSLCOMPR
) {
715 printk("\t%ld Cmp, %ld Uncmp, %ld AsIs, %ld NotTCP\n",
716 comp
->sls_o_compressed
,
717 comp
->sls_o_uncompressed
,
720 printk("\t%10ld Searches, %10ld Misses\n",
721 comp
->sls_o_searches
,