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).
27 * Modified March 1998 by Guy Lancaster, glanca@gesn.com,
28 * for a 16 bit processor.
33 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
45 #define INCR(counter) ++comp->stats.counter
50 #if defined(NO_CHAR_BITFIELDS)
51 #define getip_hl(base) ((base).ip_hl_v&0xf)
52 #define getth_off(base) (((base).th_x2_off&0xf0)>>4)
54 #define getip_hl(base) ((base).ip_hl)
55 #define getth_off(base) ((base).th_off)
59 vj_compress_init(struct vjcompress
*comp
)
62 register struct cstate
*tstate
= comp
->tstate
;
65 memset((char *)comp
, 0, sizeof(*comp
));
67 comp
->maxSlotIndex
= MAX_SLOTS
- 1;
68 comp
->compressSlot
= 0; /* Disable slot ID compression by default. */
69 for (i
= MAX_SLOTS
- 1; i
> 0; --i
) {
71 tstate
[i
].cs_next
= &tstate
[i
- 1];
73 tstate
[0].cs_next
= &tstate
[MAX_SLOTS
- 1];
75 comp
->last_cs
= &tstate
[0];
76 comp
->last_recv
= 255;
77 comp
->last_xmit
= 255;
78 comp
->flags
= VJF_TOSS
;
82 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
83 * checks for zero (since zero has to be encoded in the long, 3 byte
87 if ((u_short)(n) >= 256) { \
96 #define ENCODEZ(n) { \
97 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
107 #define DECODEL(f) { \
109 u32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
113 u32_t tmp = ntohl(f) + (u32_t)*cp++; \
118 #define DECODES(f) { \
120 u_short tmp = ntohs(f) + (((u_short)cp[1] << 8) | cp[2]); \
124 u_short tmp = ntohs(f) + (u_short)*cp++; \
129 #define DECODEU(f) { \
131 (f) = htons(((u_short)cp[1] << 8) | cp[2]); \
134 (f) = htons((u_short)*cp++); \
139 * vj_compress_tcp - Attempt to do Van Jacobsen header compression on a
140 * packet. This assumes that nb and comp are not null and that the first
141 * buffer of the chain contains a valid IP header.
142 * Return the VJ type code indicating whether or not the packet was
146 vj_compress_tcp(struct vjcompress
*comp
, struct pbuf
*pb
)
148 register struct ip
*ip
= (struct ip
*)pb
->payload
;
149 register struct cstate
*cs
= comp
->last_cs
->cs_next
;
150 register u_short hlen
= getip_hl(*ip
);
151 register struct tcphdr
*oth
;
152 register struct tcphdr
*th
;
153 register u_short deltaS
, deltaA
;
154 register u_long deltaL
;
155 register u_int changes
= 0;
157 register u_char
*cp
= new_seq
;
160 * Check that the packet is IP proto TCP.
162 if (ip
->ip_p
!= IPPROTO_TCP
) {
167 * Bail if this is an IP fragment or if the TCP packet isn't
168 * `compressible' (i.e., ACK isn't set or some other control bit is
171 if ((ip
->ip_off
& htons(0x3fff)) || pb
->tot_len
< 40) {
174 th
= (struct tcphdr
*)&((long *)ip
)[hlen
];
175 if ((th
->th_flags
& (TCP_SYN
|TCP_FIN
|TCP_RST
|TCP_ACK
)) != TCP_ACK
) {
179 * Packet is compressible -- we're going to send either a
180 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
181 * to locate (or create) the connection state. Special case the
182 * most recently used connection since it's most likely to be used
183 * again & we don't have to do any reordering if it's used.
186 if (ip
->ip_src
.s_addr
!= cs
->cs_ip
.ip_src
.s_addr
187 || ip
->ip_dst
.s_addr
!= cs
->cs_ip
.ip_dst
.s_addr
188 || *(long *)th
!= ((long *)&cs
->cs_ip
)[getip_hl(cs
->cs_ip
)]) {
190 * Wasn't the first -- search for it.
192 * States are kept in a circularly linked list with
193 * last_cs pointing to the end of the list. The
194 * list is kept in lru order by moving a state to the
195 * head of the list whenever it is referenced. Since
196 * the list is short and, empirically, the connection
197 * we want is almost always near the front, we locate
198 * states via linear search. If we don't find a state
199 * for the datagram, the oldest state is (re-)used.
201 register struct cstate
*lcs
;
202 register struct cstate
*lastcs
= comp
->last_cs
;
205 lcs
= cs
; cs
= cs
->cs_next
;
207 if (ip
->ip_src
.s_addr
== cs
->cs_ip
.ip_src
.s_addr
208 && ip
->ip_dst
.s_addr
== cs
->cs_ip
.ip_dst
.s_addr
209 && *(long *)th
== ((long *)&cs
->cs_ip
)[getip_hl(cs
->cs_ip
)]) {
212 } while (cs
!= lastcs
);
215 * Didn't find it -- re-use oldest cstate. Send an
216 * uncompressed packet that tells the other side what
217 * connection number we're using for this conversation.
218 * Note that since the state list is circular, the oldest
219 * state points to the newest and we only need to set
220 * last_cs to update the lru linkage.
224 hlen
+= getth_off(*th
);
226 /* Check that the IP/TCP headers are contained in the first buffer. */
227 if (hlen
> pb
->len
) {
234 * Found it -- move to the front on the connection list.
239 lcs
->cs_next
= cs
->cs_next
;
240 cs
->cs_next
= lastcs
->cs_next
;
241 lastcs
->cs_next
= cs
;
245 oth
= (struct tcphdr
*)&((long *)&cs
->cs_ip
)[hlen
];
247 hlen
+= getth_off(*th
);
249 /* Check that the IP/TCP headers are contained in the first buffer. */
250 if (hlen
> pb
->len
) {
251 PPPDEBUG((LOG_INFO
, "vj_compress_tcp: header len %d spans buffers\n", hlen
));
256 * Make sure that only what we expect to change changed. The first
257 * line of the `if' checks the IP protocol version, header length &
258 * type of service. The 2nd line checks the "Don't fragment" bit.
259 * The 3rd line checks the time-to-live and protocol (the protocol
260 * check is unnecessary but costless). The 4th line checks the TCP
261 * header length. The 5th line checks IP options, if any. The 6th
262 * line checks TCP options, if any. If any of these things are
263 * different between the previous & current datagram, we send the
264 * current datagram `uncompressed'.
266 if (((u_short
*)ip
)[0] != ((u_short
*)&cs
->cs_ip
)[0]
267 || ((u_short
*)ip
)[3] != ((u_short
*)&cs
->cs_ip
)[3]
268 || ((u_short
*)ip
)[4] != ((u_short
*)&cs
->cs_ip
)[4]
269 || getth_off(*th
) != getth_off(*oth
)
270 || (deltaS
> 5 && BCMP(ip
+ 1, &cs
->cs_ip
+ 1, (deltaS
- 5) << 2))
271 || (getth_off(*th
) > 5 && BCMP(th
+ 1, oth
+ 1, (getth_off(*th
) - 5) << 2))) {
276 * Figure out which of the changing fields changed. The
277 * receiver expects changes in the order: urgent, window,
278 * ack, seq (the order minimizes the number of temporaries
279 * needed in this section of code).
281 if (th
->th_flags
& TCP_URG
) {
282 deltaS
= ntohs(th
->th_urp
);
285 } else if (th
->th_urp
!= oth
->th_urp
) {
286 /* argh! URG not set but urp changed -- a sensible
287 * implementation should never do this but RFC793
288 * doesn't prohibit the change so we have to deal
293 if ((deltaS
= (u_short
)(ntohs(th
->th_win
) - ntohs(oth
->th_win
))) != 0) {
298 if ((deltaL
= ntohl(th
->th_ack
) - ntohl(oth
->th_ack
)) != 0) {
299 if (deltaL
> 0xffff) {
302 deltaA
= (u_short
)deltaL
;
307 if ((deltaL
= ntohl(th
->th_seq
) - ntohl(oth
->th_seq
)) != 0) {
308 if (deltaL
> 0xffff) {
311 deltaS
= (u_short
)deltaL
;
319 * Nothing changed. If this packet contains data and the
320 * last one didn't, this is probably a data packet following
321 * an ack (normal on an interactive connection) and we send
322 * it compressed. Otherwise it's probably a retransmit,
323 * retransmitted ack or window probe. Send it uncompressed
324 * in case the other side missed the compressed version.
326 if (ip
->ip_len
!= cs
->cs_ip
.ip_len
&&
327 ntohs(cs
->cs_ip
.ip_len
) == hlen
) {
336 * actual changes match one of our special case encodings --
337 * send packet uncompressed.
342 if (deltaS
== deltaA
&& deltaS
== ntohs(cs
->cs_ip
.ip_len
) - hlen
) {
343 /* special case for echoed terminal traffic */
350 if (deltaS
== ntohs(cs
->cs_ip
.ip_len
) - hlen
) {
351 /* special case for data xfer */
358 deltaS
= (u_short
)(ntohs(ip
->ip_id
) - ntohs(cs
->cs_ip
.ip_id
));
363 if (th
->th_flags
& TCP_PSH
) {
364 changes
|= TCP_PUSH_BIT
;
367 * Grab the cksum before we overwrite it below. Then update our
368 * state with this packet's header.
370 deltaA
= ntohs(th
->th_sum
);
371 BCOPY(ip
, &cs
->cs_ip
, hlen
);
374 * We want to use the original packet as our compressed packet.
375 * (cp - new_seq) is the number of bytes we need for compressed
376 * sequence numbers. In addition we need one byte for the change
377 * mask, one for the connection id and two for the tcp checksum.
378 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
379 * many bytes of the original packet to toss so subtract the two to
380 * get the new packet size.
382 deltaS
= (u_short
)(cp
- new_seq
);
383 if (!comp
->compressSlot
|| comp
->last_xmit
!= cs
->cs_id
) {
384 comp
->last_xmit
= cs
->cs_id
;
386 if(pbuf_header(pb
, -hlen
)){
387 /* Can we cope with this failing? Just assert for now */
388 LWIP_ASSERT("pbuf_header failed\n", 0);
390 cp
= (u_char
*)pb
->payload
;
391 *cp
++ = changes
| NEW_C
;
395 if(pbuf_header(pb
, -hlen
)) {
396 /* Can we cope with this failing? Just assert for now */
397 LWIP_ASSERT("pbuf_header failed\n", 0);
399 cp
= (u_char
*)pb
->payload
;
404 BCOPY(new_seq
, cp
, deltaS
);
405 INCR(vjs_compressed
);
406 return (TYPE_COMPRESSED_TCP
);
409 * Update connection state cs & send uncompressed packet (that is,
410 * a regular ip/tcp packet but with the 'conversation id' we hope
411 * to use on future compressed packets in the protocol field).
414 BCOPY(ip
, &cs
->cs_ip
, hlen
);
415 ip
->ip_p
= cs
->cs_id
;
416 comp
->last_xmit
= cs
->cs_id
;
417 return (TYPE_UNCOMPRESSED_TCP
);
421 * Called when we may have missed a packet.
424 vj_uncompress_err(struct vjcompress
*comp
)
426 comp
->flags
|= VJF_TOSS
;
431 * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
432 * Return 0 on success, -1 on failure.
435 vj_uncompress_uncomp(struct pbuf
*nb
, struct vjcompress
*comp
)
438 register struct cstate
*cs
;
439 register struct ip
*ip
;
441 ip
= (struct ip
*)nb
->payload
;
442 hlen
= getip_hl(*ip
) << 2;
443 if (ip
->ip_p
>= MAX_SLOTS
444 || hlen
+ sizeof(struct tcphdr
) > nb
->len
445 || (hlen
+= getth_off(*((struct tcphdr
*)&((char *)ip
)[hlen
])) << 2)
448 PPPDEBUG((LOG_INFO
, "vj_uncompress_uncomp: bad cid=%d, hlen=%d buflen=%d\n",
449 ip
->ip_p
, hlen
, nb
->len
));
450 comp
->flags
|= VJF_TOSS
;
454 cs
= &comp
->rstate
[comp
->last_recv
= ip
->ip_p
];
455 comp
->flags
&=~ VJF_TOSS
;
456 ip
->ip_p
= IPPROTO_TCP
;
457 BCOPY(ip
, &cs
->cs_ip
, hlen
);
459 INCR(vjs_uncompressedin
);
464 * Uncompress a packet of type TYPE_COMPRESSED_TCP.
465 * The packet is composed of a buffer chain and the first buffer
466 * must contain an accurate chain length.
467 * The first buffer must include the entire compressed TCP/IP header.
468 * This procedure replaces the compressed header with the uncompressed
469 * header and returns the length of the VJ header.
472 vj_uncompress_tcp(struct pbuf
**nb
, struct vjcompress
*comp
)
478 struct pbuf
*n0
= *nb
;
480 u_int vjlen
, hlen
, changes
;
482 INCR(vjs_compressedin
);
483 cp
= (u_char
*)n0
->payload
;
485 if (changes
& NEW_C
) {
487 * Make sure the state index is in range, then grab the state.
488 * If we have a good state index, clear the 'discard' flag.
490 if (*cp
>= MAX_SLOTS
) {
491 PPPDEBUG((LOG_INFO
, "vj_uncompress_tcp: bad cid=%d\n", *cp
));
495 comp
->flags
&=~ VJF_TOSS
;
496 comp
->last_recv
= *cp
++;
499 * this packet has an implicit state index. If we've
500 * had a line error since the last time we got an
501 * explicit state index, we have to toss the packet.
503 if (comp
->flags
& VJF_TOSS
) {
504 PPPDEBUG((LOG_INFO
, "vj_uncompress_tcp: tossing\n"));
509 cs
= &comp
->rstate
[comp
->last_recv
];
510 hlen
= getip_hl(cs
->cs_ip
) << 2;
511 th
= (struct tcphdr
*)&((u_char
*)&cs
->cs_ip
)[hlen
];
512 th
->th_sum
= htons((*cp
<< 8) | cp
[1]);
514 if (changes
& TCP_PUSH_BIT
) {
515 th
->th_flags
|= TCP_PSH
;
517 th
->th_flags
&=~ TCP_PSH
;
520 switch (changes
& SPECIALS_MASK
) {
523 register u32_t i
= ntohs(cs
->cs_ip
.ip_len
) - cs
->cs_hlen
;
524 /* some compilers can't nest inline assembler.. */
525 tmp
= ntohl(th
->th_ack
) + i
;
526 th
->th_ack
= htonl(tmp
);
527 tmp
= ntohl(th
->th_seq
) + i
;
528 th
->th_seq
= htonl(tmp
);
533 /* some compilers can't nest inline assembler.. */
534 tmp
= ntohl(th
->th_seq
) + ntohs(cs
->cs_ip
.ip_len
) - cs
->cs_hlen
;
535 th
->th_seq
= htonl(tmp
);
539 if (changes
& NEW_U
) {
540 th
->th_flags
|= TCP_URG
;
543 th
->th_flags
&=~ TCP_URG
;
545 if (changes
& NEW_W
) {
548 if (changes
& NEW_A
) {
551 if (changes
& NEW_S
) {
556 if (changes
& NEW_I
) {
557 DECODES(cs
->cs_ip
.ip_id
);
559 cs
->cs_ip
.ip_id
= ntohs(cs
->cs_ip
.ip_id
) + 1;
560 cs
->cs_ip
.ip_id
= htons(cs
->cs_ip
.ip_id
);
564 * At this point, cp points to the first byte of data in the
565 * packet. Fill in the IP total length and update the IP
568 vjlen
= (u_short
)(cp
- (u_char
*)n0
->payload
);
569 if (n0
->len
< vjlen
) {
571 * We must have dropped some characters (crc should detect
572 * this but the old slip framing won't)
574 PPPDEBUG((LOG_INFO
, "vj_uncompress_tcp: head buffer %d too short %d\n",
579 #if BYTE_ORDER == LITTLE_ENDIAN
580 tmp
= n0
->tot_len
- vjlen
+ cs
->cs_hlen
;
581 cs
->cs_ip
.ip_len
= htons(tmp
);
583 cs
->cs_ip
.ip_len
= htons(n0
->tot_len
- vjlen
+ cs
->cs_hlen
);
586 /* recompute the ip header checksum */
587 bp
= (u_short
*) &cs
->cs_ip
;
588 cs
->cs_ip
.ip_sum
= 0;
589 for (tmp
= 0; hlen
> 0; hlen
-= 2) {
592 tmp
= (tmp
& 0xffff) + (tmp
>> 16);
593 tmp
= (tmp
& 0xffff) + (tmp
>> 16);
594 cs
->cs_ip
.ip_sum
= (u_short
)(~tmp
);
596 /* Remove the compressed header and prepend the uncompressed header. */
597 if(pbuf_header(n0
, -((s16_t
)(vjlen
)))) {
598 /* Can we cope with this failing? Just assert for now */
599 LWIP_ASSERT("pbuf_header failed\n", 0);
603 if(LWIP_MEM_ALIGN(n0
->payload
) != n0
->payload
) {
607 np
= pbuf_alloc(PBUF_RAW
, n0
->len
+ cs
->cs_hlen
, PBUF_POOL
);
609 PPPDEBUG((LOG_WARNING
, "vj_uncompress_tcp: realign failed\n"));
613 if(pbuf_header(np
, -cs
->cs_hlen
)) {
614 /* Can we cope with this failing? Just assert for now */
615 LWIP_ASSERT("pbuf_header failed\n", 0);
619 bufptr
= n0
->payload
;
620 for(q
= np
; q
!= NULL
; q
= q
->next
) {
621 MEMCPY(q
->payload
, bufptr
, q
->len
);
626 pbuf_chain(np
, n0
->next
);
633 if(pbuf_header(n0
, cs
->cs_hlen
)) {
636 LWIP_ASSERT("vj_uncompress_tcp: cs->cs_hlen <= PBUF_POOL_BUFSIZE", cs
->cs_hlen
<= PBUF_POOL_BUFSIZE
);
637 np
= pbuf_alloc(PBUF_RAW
, cs
->cs_hlen
, PBUF_POOL
);
639 PPPDEBUG((LOG_WARNING
, "vj_uncompress_tcp: prepend failed\n"));
645 LWIP_ASSERT("n0->len >= cs->cs_hlen", n0
->len
>= cs
->cs_hlen
);
646 MEMCPY(n0
->payload
, &cs
->cs_ip
, cs
->cs_hlen
);
653 comp
->flags
|= VJF_TOSS
;
658 #endif /* VJ_SUPPORT */
660 #endif /* PPP_SUPPORT */