2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)slcompress.c 8.2 (Berkeley) 4/16/94
33 * Routines to compress and uncompess tcp packets (for transmission
34 * over low speed serial lines.
36 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
37 * - Initial distribution.
42 #include <sys/param.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <netinet/ip.h>
47 #include <sys/socket.h>
60 #include "slcompress.h"
61 #include "descriptor.h"
65 #include "throughput.h"
84 sl_compress_init(struct slcompress *comp, int max_state)
87 register struct cstate *tstate = comp->tstate;
89 memset(comp, '\0', sizeof *comp);
90 for (i = max_state; i > 0; --i) {
92 tstate[i].cs_next = &tstate[i - 1];
94 tstate[0].cs_next = &tstate[max_state];
96 comp->last_cs = &tstate[0];
97 comp->last_recv = 255;
98 comp->last_xmit = 255;
99 comp->flags = SLF_TOSS;
103 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
104 * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
107 #define ENCODE(n) { \
108 if ((u_short)(n) >= 256) { \
117 #define ENCODEZ(n) { \
118 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
128 #define DECODEL(f) { \
130 (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
133 (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
137 #define DECODES(f) { \
139 (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
142 (f) = htons(ntohs(f) + (u_int32_t)*cp++); \
146 #define DECODEU(f) { \
148 (f) = htons((cp[1] << 8) | cp[2]); \
151 (f) = htons((u_int32_t)*cp++); \
157 sl_compress_tcp(struct mbuf * m,
159 struct slcompress *comp,
160 struct slstat *slstat,
163 register struct cstate *cs = comp->last_cs->cs_next;
164 register u_int hlen = ip->ip_hl;
165 register struct tcphdr *oth;
166 register struct tcphdr *th;
167 register u_int deltaS, deltaA;
168 register u_int changes = 0;
170 register u_char *cp = new_seq;
173 * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
174 * (i.e., ACK isn't set or some other control bit is set). (We assume that
175 * the caller has already made sure the packet is IP proto TCP).
177 if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
178 log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
179 ip->ip_off, (unsigned long)m->m_len);
180 log_DumpBp(LogDEBUG, "", m);
183 th = (struct tcphdr *) & ((int *) ip)[hlen];
184 if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
185 log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
186 log_DumpBp(LogDEBUG, "", m);
191 * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
192 * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the
193 * connection state. Special case the most recently used connection since
194 * it's most likely to be used again & we don't have to do any reordering
197 slstat->sls_packets++;
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]) {
203 * Wasn't the first -- search for it.
205 * States are kept in a circularly linked list with last_cs pointing to the
206 * end of the list. The list is kept in lru order by moving a state to
207 * the head of the list whenever it is referenced. Since the list is
208 * short and, empirically, the connection we want is almost always near
209 * the front, we locate states via linear search. If we don't find a
210 * state for the datagram, the oldest state is (re-)used.
212 register struct cstate *lcs;
213 register struct cstate *lastcs = comp->last_cs;
218 slstat->sls_searches++;
219 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
220 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
221 && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
223 } while (cs != lastcs);
226 * Didn't find it -- re-use oldest cstate. Send an uncompressed packet
227 * that tells the other side what connection number we're using for this
228 * conversation. Note that since the state list is circular, the oldest
229 * state points to the newest and we only need to set last_cs to update
232 slstat->sls_misses++;
234 #define THOFFSET(th) (th->th_off)
244 * Found it -- move to the front on the connection list.
249 lcs->cs_next = cs->cs_next;
250 cs->cs_next = lastcs->cs_next;
251 lastcs->cs_next = cs;
256 * Make sure that only what we expect to change changed. The first line of
257 * the `if' checks the IP protocol version, header length & type of
258 * service. The 2nd line checks the "Don't fragment" bit. The 3rd line
259 * checks the time-to-live and protocol (the protocol check is unnecessary
260 * but costless). The 4th line checks the TCP header length. The 5th line
261 * checks IP options, if any. The 6th line checks TCP options, if any. If
262 * any of these things are different between the previous & current
263 * datagram, we send the current datagram `uncompressed'.
265 oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
272 if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
273 ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
274 ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
275 THOFFSET(th) != THOFFSET(oth) ||
277 memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
279 memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
284 * Figure out which of the changing fields changed. The receiver expects
285 * changes in the order: urgent, window, ack, seq (the order minimizes the
286 * number of temporaries needed in this section of code).
288 if (th->th_flags & TH_URG) {
289 deltaS = ntohs(th->th_urp);
292 } else if (th->th_urp != oth->th_urp) {
295 * argh! URG not set but urp changed -- a sensible implementation should
296 * never do this but RFC793 doesn't prohibit the change so we have to
301 deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
306 deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
308 if (deltaA > 0xffff) {
314 deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
316 if (deltaS > 0xffff) {
327 * Nothing changed. If this packet contains data and the last one didn't,
328 * this is probably a data packet following an ack (normal on an
329 * interactive connection) and we send it compressed. Otherwise it's
330 * probably a retransmit, retransmitted ack or window probe. Send it
331 * uncompressed 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)
343 * actual changes match one of our special case encodings -- send packet
349 if (deltaS == deltaA &&
350 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
351 /* special case for echoed terminal traffic */
358 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
359 /* special case for data xfer */
366 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
371 if (th->th_flags & TH_PUSH)
372 changes |= TCP_PUSH_BIT;
375 * Grab the cksum before we overwrite it below. Then update our state with
376 * this packet's header.
378 deltaA = ntohs(th->th_sum);
379 memcpy(&cs->cs_ip, ip, hlen);
382 * We want to use the original packet as our compressed packet. (cp -
383 * new_seq) is the number of bytes we need for compressed sequence numbers.
384 * In addition we need one byte for the change mask, one for the connection
385 * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
386 * are needed. hlen is how many bytes of the original packet to toss so
387 * subtract the two to get the new packet size.
389 deltaS = cp - new_seq;
393 * Since fastq traffic can jump ahead of the background traffic, we don't
394 * know what order packets will go on the line. In this case, we always
395 * send a "new" connection id so the receiver state stays synchronized.
397 if (comp->last_xmit == cs->cs_id && compress_cid) {
402 comp->last_xmit = cs->cs_id;
405 *cp++ = changes | NEW_C;
412 memcpy(cp, new_seq, deltaS);
413 slstat->sls_compressed++;
414 return (TYPE_COMPRESSED_TCP);
417 * Update connection state cs & send uncompressed packet ('uncompressed'
418 * means a regular ip/tcp packet but with the 'conversation id' we hope to
419 * use on future compressed packets in the protocol field).
422 memcpy(&cs->cs_ip, ip, hlen);
423 ip->ip_p = cs->cs_id;
424 comp->last_xmit = cs->cs_id;
425 return (TYPE_UNCOMPRESSED_TCP);
430 sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
431 struct slstat *slstat, int max_state)
434 register u_int hlen, changes;
435 register struct tcphdr *th;
436 register struct cstate *cs;
437 register struct ip *ip;
442 case TYPE_UNCOMPRESSED_TCP:
443 ip = (struct ip *) * bufp;
444 if (ip->ip_p > max_state)
446 cs = &comp->rstate[comp->last_recv = ip->ip_p];
447 comp->flags &= ~SLF_TOSS;
448 ip->ip_p = IPPROTO_TCP;
451 * Calculate the size of the TCP/IP header and make sure that we don't
452 * overflow the space we have available for it.
454 hlen = ip->ip_hl << 2;
455 if ((int)(hlen + sizeof(struct tcphdr)) > len)
457 th = (struct tcphdr *) & ((char *) ip)[hlen];
458 hlen += THOFFSET(th) << 2;
461 memcpy(&cs->cs_ip, ip, hlen);
463 slstat->sls_uncompressedin++;
469 case TYPE_COMPRESSED_TCP:
473 /* We've got a compressed packet. */
474 slstat->sls_compressedin++;
477 log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
479 if (changes & NEW_C) {
481 * Make sure the state index is in range, then grab the state. If we have
482 * a good state index, clear the 'discard' flag.
484 if (*cp > max_state || comp->last_recv == 255)
487 comp->flags &= ~SLF_TOSS;
488 comp->last_recv = *cp++;
491 * this packet has an implicit state index. If we've had a line error
492 * since the last time we got an explicit state index, we have to toss
495 if (comp->flags & SLF_TOSS) {
496 slstat->sls_tossed++;
500 cs = &comp->rstate[comp->last_recv];
501 hlen = cs->cs_ip.ip_hl << 2;
502 th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
503 th->th_sum = htons((*cp << 8) | cp[1]);
505 if (changes & TCP_PUSH_BIT)
506 th->th_flags |= TH_PUSH;
508 th->th_flags &= ~TH_PUSH;
510 switch (changes & SPECIALS_MASK) {
513 register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
515 th->th_ack = htonl(ntohl(th->th_ack) + i);
516 th->th_seq = htonl(ntohl(th->th_seq) + i);
521 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
526 if (changes & NEW_U) {
527 th->th_flags |= TH_URG;
530 th->th_flags &= ~TH_URG;
535 if (changes & NEW_S) {
536 log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
542 if (changes & NEW_I) {
543 DECODES(cs->cs_ip.ip_id)
545 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
547 log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
548 cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
551 * At this point, cp points to the first byte of data in the packet.
552 * Back up cp by the tcp/ip header length to make room for the
553 * reconstructed header (we assume the packet we were handed has enough
554 * space to prepend 128 bytes of header). Adjust the length to account
555 * for the new header & fill in the IP total length.
560 * we must have dropped some characters (crc should detect this but the
561 * old slip framing won't)
565 *bufp = cp - cs->cs_hlen;
567 cs->cs_ip.ip_len = htons(len);
569 /* recompute the ip header checksum */
570 cs->cs_ip.ip_sum = 0;
571 bp = (u_short *)&cs->cs_ip;
572 for (changes = 0; hlen > 0; hlen -= 2)
574 changes = (changes & 0xffff) + (changes >> 16);
575 changes = (changes & 0xffff) + (changes >> 16);
576 cs->cs_ip.ip_sum = ~changes;
578 /* And copy the result into our buffer */
579 memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
583 comp->flags |= SLF_TOSS;
584 slstat->sls_errorin++;
589 sl_Show(struct cmdargs const *arg)
591 prompt_Printf(arg->prompt, "VJ compression statistics:\n");
592 prompt_Printf(arg->prompt, " Out: %d (compress) / %d (total)",
593 arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
594 arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
595 prompt_Printf(arg->prompt, " %d (miss) / %d (search)\n",
596 arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
597 arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
598 prompt_Printf(arg->prompt, " In: %d (compress), %d (uncompress)",
599 arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
600 arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
601 prompt_Printf(arg->prompt, " %d (error), %d (tossed)\n",
602 arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
603 arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);