2 * Routines to compress and uncompess 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.
23 * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
24 * so that the entire packet being decompressed doesn't have
25 * to be in contiguous memory (just the compressed header).
29 * This version is used under SunOS 4.x, Digital UNIX, AIX 4.x,
30 * and SVR4 systems including Solaris 2.
32 * $Id: vjcompress.c,v 1.1.1.4 2003/10/14 08:09:53 sparq Exp $
35 #include <sys/types.h>
36 #include <sys/param.h>
40 #include <sys/byteorder.h> /* for ntohl, etc. */
42 /* make sure we don't get the gnu "fixed" one! */
43 #include "/usr/include/sys/byteorder.h"
48 #include <net/net_globals.h>
50 #include <netinet/in.h>
53 #define _NETINET_IN_SYSTM_H_
54 typedef u_long n_long
;
56 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
62 #include <net/ppp_defs.h>
63 #include <net/vjcompress.h>
66 #define INCR(counter) ++comp->stats.counter
71 #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
73 #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
79 #define getip_hl(base) (((base).ip_vhl)&0xf)
80 #define getth_off(base) ((((base).th_xoff)&0xf0)>>4)
83 #define getip_hl(base) ((base).ip_hl)
84 #define getth_off(base) ((base).th_off)
88 vj_compress_init(comp
, max_state
)
89 struct vjcompress
*comp
;
93 register struct cstate
*tstate
= comp
->tstate
;
96 max_state
= MAX_STATES
- 1;
97 bzero((char *)comp
, sizeof(*comp
));
98 for (i
= max_state
; i
> 0; --i
) {
100 tstate
[i
].cs_next
= &tstate
[i
- 1];
102 tstate
[0].cs_next
= &tstate
[max_state
];
104 comp
->last_cs
= &tstate
[0];
105 comp
->last_recv
= 255;
106 comp
->last_xmit
= 255;
107 comp
->flags
= VJF_TOSS
;
111 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
112 * checks for zero (since zero has to be encoded in the long, 3 byte
115 #define ENCODE(n) { \
116 if ((u_short)(n) >= 256) { \
125 #define ENCODEZ(n) { \
126 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
136 #define DECODEL(f) { \
138 u_int32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
142 u_int32_t tmp = ntohl(f) + (u_int32_t)*cp++; \
147 #define DECODES(f) { \
149 u_short tmp = ntohs(f) + ((cp[1] << 8) | cp[2]); \
153 u_short tmp = ntohs(f) + (u_int32_t)*cp++; \
158 #define DECODEU(f) { \
160 (f) = htons((cp[1] << 8) | cp[2]); \
163 (f) = htons((u_int32_t)*cp++); \
168 vj_compress_tcp(ip
, mlen
, comp
, compress_cid
, vjhdrp
)
169 register struct ip
*ip
;
171 struct vjcompress
*comp
;
175 register struct cstate
*cs
= comp
->last_cs
->cs_next
;
176 register u_int hlen
= getip_hl(*ip
);
177 register struct tcphdr
*oth
;
178 register struct tcphdr
*th
;
179 register u_int deltaS
, deltaA
;
180 register u_int changes
= 0;
182 register u_char
*cp
= new_seq
;
185 * Bail if this is an IP fragment or if the TCP packet isn't
186 * `compressible' (i.e., ACK isn't set or some other control bit is
187 * set). (We assume that the caller has already made sure the
188 * packet is IP proto TCP).
190 if ((ip
->ip_off
& htons(0x3fff)) || mlen
< 40)
193 th
= (struct tcphdr
*)&((int *)ip
)[hlen
];
194 if ((th
->th_flags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_ACK
)) != TH_ACK
)
197 * Packet is compressible -- we're going to send either a
198 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
199 * to locate (or create) the connection state. Special case the
200 * most recently used connection since it's most likely to be used
201 * again & we don't have to do any reordering if it's used.
204 if (ip
->ip_src
.s_addr
!= cs
->cs_ip
.ip_src
.s_addr
||
205 ip
->ip_dst
.s_addr
!= cs
->cs_ip
.ip_dst
.s_addr
||
206 *(int *)th
!= ((int *)&cs
->cs_ip
)[getip_hl(cs
->cs_ip
)]) {
208 * Wasn't the first -- search for it.
210 * States are kept in a circularly linked list with
211 * last_cs pointing to the end of the list. The
212 * list is kept in lru order by moving a state to the
213 * head of the list whenever it is referenced. Since
214 * the list is short and, empirically, the connection
215 * we want is almost always near the front, we locate
216 * states via linear search. If we don't find a state
217 * for the datagram, the oldest state is (re-)used.
219 register struct cstate
*lcs
;
220 register struct cstate
*lastcs
= comp
->last_cs
;
223 lcs
= cs
; cs
= cs
->cs_next
;
225 if (ip
->ip_src
.s_addr
== cs
->cs_ip
.ip_src
.s_addr
226 && ip
->ip_dst
.s_addr
== cs
->cs_ip
.ip_dst
.s_addr
227 && *(int *)th
== ((int *)&cs
->cs_ip
)[getip_hl(cs
->cs_ip
)])
229 } while (cs
!= lastcs
);
232 * Didn't find it -- re-use oldest cstate. Send an
233 * uncompressed packet that tells the other side what
234 * connection number we're using for this conversation.
235 * Note that since the state list is circular, the oldest
236 * state points to the newest and we only need to set
237 * last_cs to update the lru linkage.
241 hlen
+= getth_off(*th
);
249 * Found it -- move to the front on the connection list.
254 lcs
->cs_next
= cs
->cs_next
;
255 cs
->cs_next
= lastcs
->cs_next
;
256 lastcs
->cs_next
= cs
;
261 * Make sure that only what we expect to change changed. The first
262 * line of the `if' checks the IP protocol version, header length &
263 * type of service. The 2nd line checks the "Don't fragment" bit.
264 * The 3rd line checks the time-to-live and protocol (the protocol
265 * check is unnecessary but costless). The 4th line checks the TCP
266 * header length. The 5th line checks IP options, if any. The 6th
267 * line checks TCP options, if any. If any of these things are
268 * different between the previous & current datagram, we send the
269 * current datagram `uncompressed'.
271 oth
= (struct tcphdr
*)&((int *)&cs
->cs_ip
)[hlen
];
273 hlen
+= getth_off(*th
);
278 if (((u_short
*)ip
)[0] != ((u_short
*)&cs
->cs_ip
)[0] ||
279 ((u_short
*)ip
)[3] != ((u_short
*)&cs
->cs_ip
)[3] ||
280 ((u_short
*)ip
)[4] != ((u_short
*)&cs
->cs_ip
)[4] ||
281 getth_off(*th
) != getth_off(*oth
) ||
282 (deltaS
> 5 && BCMP(ip
+ 1, &cs
->cs_ip
+ 1, (deltaS
- 5) << 2)) ||
283 (getth_off(*th
) > 5 && BCMP(th
+ 1, oth
+ 1, (getth_off(*th
) - 5) << 2)))
287 * Figure out which of the changing fields changed. The
288 * receiver expects changes in the order: urgent, window,
289 * ack, seq (the order minimizes the number of temporaries
290 * needed in this section of code).
292 if (th
->th_flags
& TH_URG
) {
293 deltaS
= ntohs(th
->th_urp
);
296 } else if (th
->th_urp
!= oth
->th_urp
)
297 /* argh! URG not set but urp changed -- a sensible
298 * implementation should never do this but RFC793
299 * doesn't prohibit the change so we have to deal
303 if ((deltaS
= (u_short
)(ntohs(th
->th_win
) - ntohs(oth
->th_win
))) > 0) {
308 if ((deltaA
= ntohl(th
->th_ack
) - ntohl(oth
->th_ack
)) > 0) {
315 if ((deltaS
= ntohl(th
->th_seq
) - ntohl(oth
->th_seq
)) > 0) {
326 * Nothing changed. If this packet contains data and the
327 * last one didn't, this is probably a data packet following
328 * an ack (normal on an interactive connection) and we send
329 * it compressed. Otherwise it's probably a retransmit,
330 * retransmitted ack or window probe. Send it uncompressed
331 * in case the other side missed the compressed version.
333 if (ip
->ip_len
!= cs
->cs_ip
.ip_len
&&
334 ntohs(cs
->cs_ip
.ip_len
) == hlen
)
342 * actual changes match one of our special case encodings --
343 * send packet uncompressed.
348 if (deltaS
== deltaA
&& deltaS
== ntohs(cs
->cs_ip
.ip_len
) - hlen
) {
349 /* special case for echoed terminal traffic */
356 if (deltaS
== ntohs(cs
->cs_ip
.ip_len
) - hlen
) {
357 /* special case for data xfer */
364 deltaS
= ntohs(ip
->ip_id
) - ntohs(cs
->cs_ip
.ip_id
);
369 if (th
->th_flags
& TH_PUSH
)
370 changes
|= TCP_PUSH_BIT
;
372 * Grab the cksum before we overwrite it below. Then update our
373 * state with this packet's header.
375 deltaA
= ntohs(th
->th_sum
);
376 BCOPY(ip
, &cs
->cs_ip
, hlen
);
379 * We want to use the original packet as our compressed packet.
380 * (cp - new_seq) is the number of bytes we need for compressed
381 * sequence numbers. In addition we need one byte for the change
382 * mask, one for the connection id and two for the tcp checksum.
383 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
384 * many bytes of the original packet to toss so subtract the two to
385 * get the new packet size.
387 deltaS
= cp
- new_seq
;
389 if (compress_cid
== 0 || comp
->last_xmit
!= cs
->cs_id
) {
390 comp
->last_xmit
= cs
->cs_id
;
392 *vjhdrp
= (cp
+= hlen
);
393 *cp
++ = changes
| NEW_C
;
397 *vjhdrp
= (cp
+= hlen
);
402 BCOPY(new_seq
, cp
, deltaS
);
403 INCR(vjs_compressed
);
404 return (TYPE_COMPRESSED_TCP
);
407 * Update connection state cs & send uncompressed packet (that is,
408 * a regular ip/tcp packet but with the 'conversation id' we hope
409 * to use on future compressed packets in the protocol field).
412 BCOPY(ip
, &cs
->cs_ip
, hlen
);
413 ip
->ip_p
= cs
->cs_id
;
414 comp
->last_xmit
= cs
->cs_id
;
415 return (TYPE_UNCOMPRESSED_TCP
);
419 * Called when we may have missed a packet.
422 vj_uncompress_err(comp
)
423 struct vjcompress
*comp
;
425 comp
->flags
|= VJF_TOSS
;
430 * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
433 vj_uncompress_uncomp(buf
, buflen
, comp
)
436 struct vjcompress
*comp
;
439 register struct cstate
*cs
;
440 register struct ip
*ip
;
442 ip
= (struct ip
*) buf
;
443 hlen
= getip_hl(*ip
) << 2;
444 if (ip
->ip_p
>= MAX_STATES
445 || hlen
+ sizeof(struct tcphdr
) > buflen
446 || (hlen
+= getth_off(*((struct tcphdr
*)&((char *)ip
)[hlen
])) << 2)
449 comp
->flags
|= VJF_TOSS
;
453 cs
= &comp
->rstate
[comp
->last_recv
= ip
->ip_p
];
454 comp
->flags
&=~ VJF_TOSS
;
455 ip
->ip_p
= IPPROTO_TCP
;
456 BCOPY(ip
, &cs
->cs_ip
, hlen
);
458 INCR(vjs_uncompressedin
);
463 * Uncompress a packet of type TYPE_COMPRESSED_TCP.
464 * The packet starts at buf and is of total length total_len.
465 * The first buflen bytes are at buf; this must include the entire
466 * compressed TCP/IP header. This procedure returns the length
467 * of the VJ header, with a pointer to the uncompressed IP header
468 * in *hdrp and its length in *hlenp.
471 vj_uncompress_tcp(buf
, buflen
, total_len
, comp
, hdrp
, hlenp
)
473 int buflen
, total_len
;
474 struct vjcompress
*comp
;
479 register u_int hlen
, changes
;
480 register struct tcphdr
*th
;
481 register struct cstate
*cs
;
482 register u_short
*bp
;
483 register u_int vjlen
;
484 register u_int32_t tmp
;
486 INCR(vjs_compressedin
);
489 if (changes
& NEW_C
) {
490 /* Make sure the state index is in range, then grab the state.
491 * If we have a good state index, clear the 'discard' flag. */
492 if (*cp
>= MAX_STATES
)
495 comp
->flags
&=~ VJF_TOSS
;
496 comp
->last_recv
= *cp
++;
498 /* this packet has an implicit state index. If we've
499 * had a line error since the last time we got an
500 * explicit state index, we have to toss the packet. */
501 if (comp
->flags
& VJF_TOSS
) {
506 cs
= &comp
->rstate
[comp
->last_recv
];
507 hlen
= getip_hl(cs
->cs_ip
) << 2;
508 th
= (struct tcphdr
*)&((u_char
*)&cs
->cs_ip
)[hlen
];
509 th
->th_sum
= htons((*cp
<< 8) | cp
[1]);
511 if (changes
& TCP_PUSH_BIT
)
512 th
->th_flags
|= TH_PUSH
;
514 th
->th_flags
&=~ TH_PUSH
;
516 switch (changes
& SPECIALS_MASK
) {
519 register u_int32_t i
= ntohs(cs
->cs_ip
.ip_len
) - cs
->cs_hlen
;
520 /* some compilers can't nest inline assembler.. */
521 tmp
= ntohl(th
->th_ack
) + i
;
522 th
->th_ack
= htonl(tmp
);
523 tmp
= ntohl(th
->th_seq
) + i
;
524 th
->th_seq
= htonl(tmp
);
529 /* some compilers can't nest inline assembler.. */
530 tmp
= ntohl(th
->th_seq
) + ntohs(cs
->cs_ip
.ip_len
) - cs
->cs_hlen
;
531 th
->th_seq
= htonl(tmp
);
535 if (changes
& NEW_U
) {
536 th
->th_flags
|= TH_URG
;
539 th
->th_flags
&=~ TH_URG
;
548 if (changes
& NEW_I
) {
549 DECODES(cs
->cs_ip
.ip_id
);
551 cs
->cs_ip
.ip_id
= ntohs(cs
->cs_ip
.ip_id
) + 1;
552 cs
->cs_ip
.ip_id
= htons(cs
->cs_ip
.ip_id
);
556 * At this point, cp points to the first byte of data in the
557 * packet. Fill in the IP total length and update the IP
563 /* we must have dropped some characters (crc should detect
564 * this but the old slip framing won't) */
567 total_len
+= cs
->cs_hlen
- vjlen
;
568 cs
->cs_ip
.ip_len
= htons(total_len
);
570 /* recompute the ip header checksum */
571 bp
= (u_short
*) &cs
->cs_ip
;
572 cs
->cs_ip
.ip_sum
= 0;
573 for (changes
= 0; hlen
> 0; hlen
-= 2)
575 changes
= (changes
& 0xffff) + (changes
>> 16);
576 changes
= (changes
& 0xffff) + (changes
>> 16);
577 cs
->cs_ip
.ip_sum
= ~ changes
;
579 *hdrp
= (u_char
*) &cs
->cs_ip
;
580 *hlenp
= cs
->cs_hlen
;
584 comp
->flags
|= VJF_TOSS
;