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.
22 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
23 * - Initial distribution.
26 #include <sys/param.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <netinet/ip.h>
42 #include "slcompress.h"
43 #include "descriptor.h"
47 #include "throughput.h"
63 sl_compress_init(struct slcompress *comp, int max_state)
66 register struct cstate *tstate = comp->tstate;
68 memset(comp, '\0', sizeof *comp);
69 for (i = max_state; i > 0; --i) {
71 tstate[i].cs_next = &tstate[i - 1];
73 tstate[0].cs_next = &tstate[max_state];
75 comp->last_cs = &tstate[0];
76 comp->last_recv = 255;
77 comp->last_xmit = 255;
78 comp->flags = SLF_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 32-bit, 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 (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
112 (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
116 #define DECODES(f) { \
118 (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
121 (f) = htons(ntohs(f) + (u_int32_t)*cp++); \
125 #define DECODEU(f) { \
127 (f) = htons((cp[1] << 8) | cp[2]); \
130 (f) = htons((u_int32_t)*cp++); \
136 sl_compress_tcp(struct mbuf * m,
138 struct slcompress *comp,
139 struct slstat *slstat,
142 register struct cstate *cs = comp->last_cs->cs_next;
143 register u_int hlen = ip->ip_hl;
144 register struct tcphdr *oth;
145 register struct tcphdr *th;
146 register u_int deltaS, deltaA;
147 register u_int changes = 0;
149 register u_char *cp = new_seq;
152 * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
153 * (i.e., ACK isn't set or some other control bit is set). (We assume that
154 * the caller has already made sure the packet is IP proto TCP).
156 if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
157 log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
158 ip->ip_off, (unsigned long)m->m_len);
159 log_DumpBp(LogDEBUG, "", m);
162 th = (struct tcphdr *) & ((int *) ip)[hlen];
163 if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
164 log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
165 log_DumpBp(LogDEBUG, "", m);
170 * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
171 * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the
172 * connection state. Special case the most recently used connection since
173 * it's most likely to be used again & we don't have to do any reordering
176 slstat->sls_packets++;
177 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
178 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
179 *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
182 * Wasn't the first -- search for it.
184 * States are kept in a circularly linked list with last_cs pointing to the
185 * end of the list. The list is kept in lru order by moving a state to
186 * the head of the list whenever it is referenced. Since the list is
187 * short and, empirically, the connection we want is almost always near
188 * the front, we locate states via linear search. If we don't find a
189 * state for the datagram, the oldest state is (re-)used.
191 register struct cstate *lcs;
192 register struct cstate *lastcs = comp->last_cs;
197 slstat->sls_searches++;
198 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
199 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
200 && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
202 } while (cs != lastcs);
205 * Didn't find it -- re-use oldest cstate. Send an uncompressed packet
206 * that tells the other side what connection number we're using for this
207 * conversation. Note that since the state list is circular, the oldest
208 * state points to the newest and we only need to set last_cs to update
211 slstat->sls_misses++;
213 #define THOFFSET(th) (th->th_off)
223 * Found it -- move to the front on the connection list.
228 lcs->cs_next = cs->cs_next;
229 cs->cs_next = lastcs->cs_next;
230 lastcs->cs_next = cs;
235 * Make sure that only what we expect to change changed. The first line of
236 * the `if' checks the IP protocol version, header length & type of
237 * service. The 2nd line checks the "Don't fragment" bit. The 3rd line
238 * checks the time-to-live and protocol (the protocol check is unnecessary
239 * but costless). The 4th line checks the TCP header length. The 5th line
240 * checks IP options, if any. The 6th line checks TCP options, if any. If
241 * any of these things are different between the previous & current
242 * datagram, we send the current datagram `uncompressed'.
244 oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
251 if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
252 ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
253 ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
254 THOFFSET(th) != THOFFSET(oth) ||
256 memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
258 memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
263 * Figure out which of the changing fields changed. The receiver expects
264 * changes in the order: urgent, window, ack, seq (the order minimizes the
265 * number of temporaries needed in this section of code).
267 if (th->th_flags & TH_URG) {
268 deltaS = ntohs(th->th_urp);
271 } else if (th->th_urp != oth->th_urp) {
274 * argh! URG not set but urp changed -- a sensible implementation should
275 * never do this but RFC793 doesn't prohibit the change so we have to
280 deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
285 deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
287 if (deltaA > 0xffff) {
293 deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
295 if (deltaS > 0xffff) {
306 * Nothing changed. If this packet contains data and the last one didn't,
307 * this is probably a data packet following an ack (normal on an
308 * interactive connection) and we send it compressed. Otherwise it's
309 * probably a retransmit, retransmitted ack or window probe. Send it
310 * uncompressed in case the other side missed the compressed version.
312 if (ip->ip_len != cs->cs_ip.ip_len &&
313 ntohs(cs->cs_ip.ip_len) == hlen)
322 * actual changes match one of our special case encodings -- send packet
328 if (deltaS == deltaA &&
329 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
330 /* special case for echoed terminal traffic */
337 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
338 /* special case for data xfer */
345 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
350 if (th->th_flags & TH_PUSH)
351 changes |= TCP_PUSH_BIT;
354 * Grab the cksum before we overwrite it below. Then update our state with
355 * this packet's header.
357 deltaA = ntohs(th->th_sum);
358 memcpy(&cs->cs_ip, ip, hlen);
361 * We want to use the original packet as our compressed packet. (cp -
362 * new_seq) is the number of bytes we need for compressed sequence numbers.
363 * In addition we need one byte for the change mask, one for the connection
364 * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
365 * are needed. hlen is how many bytes of the original packet to toss so
366 * subtract the two to get the new packet size.
368 deltaS = cp - new_seq;
372 * Since fastq traffic can jump ahead of the background traffic, we don't
373 * know what order packets will go on the line. In this case, we always
374 * send a "new" connection id so the receiver state stays synchronized.
376 if (comp->last_xmit == cs->cs_id && compress_cid) {
381 comp->last_xmit = cs->cs_id;
384 *cp++ = changes | NEW_C;
391 memcpy(cp, new_seq, deltaS);
392 slstat->sls_compressed++;
393 return (TYPE_COMPRESSED_TCP);
396 * Update connection state cs & send uncompressed packet ('uncompressed'
397 * means a regular ip/tcp packet but with the 'conversation id' we hope to
398 * use on future compressed packets in the protocol field).
401 memcpy(&cs->cs_ip, ip, hlen);
402 ip->ip_p = cs->cs_id;
403 comp->last_xmit = cs->cs_id;
404 return (TYPE_UNCOMPRESSED_TCP);
409 sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
410 struct slstat *slstat, int max_state)
413 register u_int hlen, changes;
414 register struct tcphdr *th;
415 register struct cstate *cs;
416 register struct ip *ip;
421 case TYPE_UNCOMPRESSED_TCP:
422 ip = (struct ip *) * bufp;
423 if (ip->ip_p > max_state)
425 cs = &comp->rstate[comp->last_recv = ip->ip_p];
426 comp->flags &= ~SLF_TOSS;
427 ip->ip_p = IPPROTO_TCP;
430 * Calculate the size of the TCP/IP header and make sure that we don't
431 * overflow the space we have available for it.
433 hlen = ip->ip_hl << 2;
434 if (hlen + sizeof(struct tcphdr) > len)
436 th = (struct tcphdr *) & ((char *) ip)[hlen];
437 hlen += THOFFSET(th) << 2;
440 memcpy(&cs->cs_ip, ip, hlen);
442 slstat->sls_uncompressedin++;
448 case TYPE_COMPRESSED_TCP:
452 /* We've got a compressed packet. */
453 slstat->sls_compressedin++;
456 log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
458 if (changes & NEW_C) {
460 * Make sure the state index is in range, then grab the state. If we have
461 * a good state index, clear the 'discard' flag.
463 if (*cp > max_state || comp->last_recv == 255)
466 comp->flags &= ~SLF_TOSS;
467 comp->last_recv = *cp++;
470 * this packet has an implicit state index. If we've had a line error
471 * since the last time we got an explicit state index, we have to toss
474 if (comp->flags & SLF_TOSS) {
475 slstat->sls_tossed++;
479 cs = &comp->rstate[comp->last_recv];
480 hlen = cs->cs_ip.ip_hl << 2;
481 th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
482 th->th_sum = htons((*cp << 8) | cp[1]);
484 if (changes & TCP_PUSH_BIT)
485 th->th_flags |= TH_PUSH;
487 th->th_flags &= ~TH_PUSH;
489 switch (changes & SPECIALS_MASK) {
492 register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
494 th->th_ack = htonl(ntohl(th->th_ack) + i);
495 th->th_seq = htonl(ntohl(th->th_seq) + i);
500 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
505 if (changes & NEW_U) {
506 th->th_flags |= TH_URG;
509 th->th_flags &= ~TH_URG;
514 if (changes & NEW_S) {
515 log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
521 if (changes & NEW_I) {
522 DECODES(cs->cs_ip.ip_id)
524 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
526 log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
527 cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
530 * At this point, cp points to the first byte of data in the packet.
531 * Back up cp by the tcp/ip header length to make room for the
532 * reconstructed header (we assume the packet we were handed has enough
533 * space to prepend 128 bytes of header). Adjust the length to account
534 * for the new header & fill in the IP total length.
539 * we must have dropped some characters (crc should detect this but the
540 * old slip framing won't)
544 *bufp = cp - cs->cs_hlen;
546 cs->cs_ip.ip_len = htons(len);
548 /* recompute the ip header checksum */
549 cs->cs_ip.ip_sum = 0;
550 bp = (u_short *)&cs->cs_ip;
551 for (changes = 0; hlen > 0; hlen -= 2)
553 changes = (changes & 0xffff) + (changes >> 16);
554 changes = (changes & 0xffff) + (changes >> 16);
555 cs->cs_ip.ip_sum = ~changes;
557 /* And copy the result into our buffer */
558 memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
562 comp->flags |= SLF_TOSS;
563 slstat->sls_errorin++;
568 sl_Show(struct cmdargs const *arg)
570 prompt_Printf(arg->prompt, "VJ compression statistics:\n");
571 prompt_Printf(arg->prompt, " Out: %d (compress) / %d (total)",
572 arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
573 arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
574 prompt_Printf(arg->prompt, " %d (miss) / %d (search)\n",
575 arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
576 arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
577 prompt_Printf(arg->prompt, " In: %d (compress), %d (uncompress)",
578 arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
579 arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
580 prompt_Printf(arg->prompt, " %d (error), %d (tossed)\n",
581 arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
582 arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);