slcompress.c revision 28679
16059Samurai/*
26059Samurai * Routines to compress and uncompess tcp packets (for transmission
36059Samurai * over low speed serial lines.
46059Samurai *
56059Samurai * Copyright (c) 1989 Regents of the University of California.
66059Samurai * All rights reserved.
76059Samurai *
86059Samurai * Redistribution and use in source and binary forms are permitted
96059Samurai * provided that the above copyright notice and this paragraph are
106059Samurai * duplicated in all such forms and that any documentation,
116059Samurai * advertising materials, and other materials related to such
126059Samurai * distribution and use acknowledge that the software was developed
136059Samurai * by the University of California, Berkeley.  The name of the
146059Samurai * University may not be used to endorse or promote products derived
156059Samurai * from this software without specific prior written permission.
166059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
176059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
186059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
196059Samurai *
2028679Sbrian * $Id: slcompress.c,v 1.9 1997/06/09 03:27:37 brian Exp $
218857Srgrimes *
226059Samurai *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
236059Samurai *	- Initial distribution.
246059Samurai */
256059Samurai#ifndef lint
2628679Sbrianstatic char const rcsid[] = "$Id: slcompress.c,v 1.9 1997/06/09 03:27:37 brian Exp $";
2728679Sbrian
286059Samurai#endif
296059Samurai
306059Samurai#include "defs.h"
316059Samurai#include <netinet/in_systm.h>
326059Samurai#include <netinet/in.h>
336059Samurai#include <netinet/tcp.h>
346059Samurai#include <netinet/ip.h>
356059Samurai#include "slcompress.h"
3626516Sbrian#include "loadalias.h"
3726516Sbrian#include "vars.h"
386059Samurai
396059Samuraistruct slstat slstat;
406059Samurai
416059Samurai#define INCR(counter)	slstat.counter++;
426059Samurai
436059Samurai#define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
446059Samurai#define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
456059Samurai#ifndef KERNEL
466059Samurai#define ovbcopy bcopy
476059Samurai#endif
486059Samurai
496059Samuraivoid
5028679Sbriansl_compress_init(struct slcompress * comp)
516059Samurai{
5228679Sbrian  register u_int i;
5328679Sbrian  register struct cstate *tstate = comp->tstate;
546059Samurai
5528679Sbrian  bzero((char *) comp, sizeof(*comp));
5628679Sbrian  for (i = MAX_STATES - 1; i > 0; --i) {
5728679Sbrian    tstate[i].cs_id = i;
5828679Sbrian    tstate[i].cs_next = &tstate[i - 1];
5928679Sbrian  }
6028679Sbrian  tstate[0].cs_next = &tstate[MAX_STATES - 1];
6128679Sbrian  tstate[0].cs_id = 0;
6228679Sbrian  comp->last_cs = &tstate[0];
6328679Sbrian  comp->last_recv = 255;
6428679Sbrian  comp->last_xmit = 255;
6528679Sbrian  comp->flags = SLF_TOSS;
666059Samurai}
676059Samurai
686059Samurai
696059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
706059Samurai * checks for zero (since zero has to be encoded in the long, 3 byte
716059Samurai * form).
726059Samurai */
736059Samurai#define ENCODE(n) { \
746059Samurai	if ((u_short)(n) >= 256) { \
756059Samurai		*cp++ = 0; \
766059Samurai		cp[1] = (n); \
776059Samurai		cp[0] = (n) >> 8; \
786059Samurai		cp += 2; \
796059Samurai	} else { \
806059Samurai		*cp++ = (n); \
816059Samurai	} \
826059Samurai}
836059Samurai#define ENCODEZ(n) { \
846059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
856059Samurai		*cp++ = 0; \
866059Samurai		cp[1] = (n); \
876059Samurai		cp[0] = (n) >> 8; \
886059Samurai		cp += 2; \
896059Samurai	} else { \
906059Samurai		*cp++ = (n); \
916059Samurai	} \
926059Samurai}
936059Samurai
946059Samurai#define DECODEL(f) { \
956059Samurai	if (*cp == 0) {\
966059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
976059Samurai		cp += 3; \
986059Samurai	} else { \
996059Samurai		(f) = htonl(ntohl(f) + (u_long)*cp++); \
1006059Samurai	} \
1016059Samurai}
1026059Samurai
1036059Samurai#define DECODES(f) { \
1046059Samurai	if (*cp == 0) {\
1056059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1066059Samurai		cp += 3; \
1076059Samurai	} else { \
1086059Samurai		(f) = htons(ntohs(f) + (u_long)*cp++); \
1096059Samurai	} \
1106059Samurai}
1116059Samurai
1126059Samurai#define DECODEU(f) { \
1136059Samurai	if (*cp == 0) {\
1146059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1156059Samurai		cp += 3; \
1166059Samurai	} else { \
1176059Samurai		(f) = htons((u_long)*cp++); \
1186059Samurai	} \
1196059Samurai}
1206059Samurai
1216059Samurai
1226059Samuraiu_char
12328679Sbriansl_compress_tcp(struct mbuf * m,
12428679Sbrian		struct ip * ip,
12528679Sbrian		struct slcompress * comp,
12628679Sbrian		int compress_cid)
1276059Samurai{
12828679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
12928679Sbrian  register u_int hlen = ip->ip_hl;
13028679Sbrian  register struct tcphdr *oth;
13128679Sbrian  register struct tcphdr *th;
13228679Sbrian  register u_int deltaS, deltaA;
13328679Sbrian  register u_int changes = 0;
13428679Sbrian  u_char new_seq[16];
13528679Sbrian  register u_char *cp = new_seq;
1366059Samurai
13728679Sbrian  /*
13828679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
13928679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
14028679Sbrian   * the caller has already made sure the packet is IP proto TCP).
14128679Sbrian   */
14228679Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) {
14328679Sbrian    LogPrintf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n",
14428679Sbrian	      ip->ip_off, m->cnt);
14528679Sbrian    LogDumpBp(LogDEBUG, "", m);
14628679Sbrian    return (TYPE_IP);
14728679Sbrian  }
14828679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
14928679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
15028679Sbrian    LogPrintf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
15128679Sbrian    LogDumpBp(LogDEBUG, "", m);
15228679Sbrian    return (TYPE_IP);
15328679Sbrian  }
1546059Samurai
15528679Sbrian  /*
15628679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
15728679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
15828679Sbrian   * connection state.  Special case the most recently used connection since
15928679Sbrian   * it's most likely to be used again & we don't have to do any reordering
16028679Sbrian   * if it's used.
16128679Sbrian   */
16228679Sbrian  INCR(sls_packets)
16328679Sbrian    if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
16428679Sbrian	ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
16528679Sbrian	*(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1666059Samurai
16728679Sbrian    /*
16828679Sbrian     * Wasn't the first -- search for it.
16928679Sbrian     *
17028679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
17128679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
17228679Sbrian     * the head of the list whenever it is referenced.  Since the list is
17328679Sbrian     * short and, empirically, the connection we want is almost always near
17428679Sbrian     * the front, we locate states via linear search.  If we don't find a
17528679Sbrian     * state for the datagram, the oldest state is (re-)used.
17628679Sbrian     */
17728679Sbrian    register struct cstate *lcs;
17828679Sbrian    register struct cstate *lastcs = comp->last_cs;
1796059Samurai
18028679Sbrian    do {
18128679Sbrian      lcs = cs;
18228679Sbrian      cs = cs->cs_next;
18328679Sbrian      INCR(sls_searches)
18428679Sbrian	if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
18528679Sbrian	    && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
18628679Sbrian	    && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
18728679Sbrian	goto found;
18828679Sbrian    } while (cs != lastcs);
1896059Samurai
19028679Sbrian    /*
19128679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
19228679Sbrian     * that tells the other side what connection number we're using for this
19328679Sbrian     * conversation. Note that since the state list is circular, the oldest
19428679Sbrian     * state points to the newest and we only need to set last_cs to update
19528679Sbrian     * the lru linkage.
19628679Sbrian     */
19728679Sbrian    INCR(sls_misses)
19828679Sbrian      comp->last_cs = lcs;
1996059Samurai#define	THOFFSET(th)	(th->th_off)
20028679Sbrian    hlen += th->th_off;
20128679Sbrian    hlen <<= 2;
20228679Sbrian    if (hlen > m->cnt)
20328679Sbrian      return (TYPE_IP);
20428679Sbrian    goto uncompressed;
2056059Samurai
20628679Sbrianfound:
2076059Samurai
20828679Sbrian    /*
20928679Sbrian     * Found it -- move to the front on the connection list.
21028679Sbrian     */
21128679Sbrian    if (cs == lastcs)
21228679Sbrian      comp->last_cs = lcs;
21328679Sbrian    else {
21428679Sbrian      lcs->cs_next = cs->cs_next;
21528679Sbrian      cs->cs_next = lastcs->cs_next;
21628679Sbrian      lastcs->cs_next = cs;
21728679Sbrian    }
21828679Sbrian  }
2196059Samurai
22028679Sbrian  /*
22128679Sbrian   * Make sure that only what we expect to change changed. The first line of
22228679Sbrian   * the `if' checks the IP protocol version, header length & type of
22328679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
22428679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
22528679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
22628679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
22728679Sbrian   * any of these things are different between the previous & current
22828679Sbrian   * datagram, we send the current datagram `uncompressed'.
22928679Sbrian   */
23028679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
23128679Sbrian  deltaS = hlen;
23228679Sbrian  hlen += th->th_off;
23328679Sbrian  hlen <<= 2;
23428679Sbrian  if (hlen > m->cnt)
23528679Sbrian    return (TYPE_IP);
2366059Samurai
23728679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
23828679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
23928679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
24028679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
24128679Sbrian      (deltaS > 5 &&
24228679Sbrian       BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
24328679Sbrian      (THOFFSET(th) > 5 &&
24428679Sbrian       BCMP(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
24528679Sbrian    goto uncompressed;
24628679Sbrian  }
2476059Samurai
24828679Sbrian  /*
24928679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
25028679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
25128679Sbrian   * number of temporaries needed in this section of code).
25228679Sbrian   */
25328679Sbrian  if (th->th_flags & TH_URG) {
25428679Sbrian    deltaS = ntohs(th->th_urp);
25528679Sbrian    ENCODEZ(deltaS);
25628679Sbrian    changes |= NEW_U;
25728679Sbrian  } else if (th->th_urp != oth->th_urp) {
2586059Samurai
25928679Sbrian    /*
26028679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
26128679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
26228679Sbrian     * deal with it.
26328679Sbrian     */
26428679Sbrian    goto uncompressed;
26528679Sbrian  }
26628679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
26728679Sbrian  if (deltaS) {
26828679Sbrian    ENCODE(deltaS);
26928679Sbrian    changes |= NEW_W;
27028679Sbrian  }
27128679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
27228679Sbrian  if (deltaA) {
27328679Sbrian    if (deltaA > 0xffff) {
27428679Sbrian      goto uncompressed;
27528679Sbrian    }
27628679Sbrian    ENCODE(deltaA);
27728679Sbrian    changes |= NEW_A;
27828679Sbrian  }
27928679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
28028679Sbrian  if (deltaS) {
28128679Sbrian    if (deltaS > 0xffff) {
28228679Sbrian      goto uncompressed;
28328679Sbrian    }
28428679Sbrian    ENCODE(deltaS);
28528679Sbrian    changes |= NEW_S;
28628679Sbrian  }
28728679Sbrian  switch (changes) {
2886059Samurai
28928679Sbrian  case 0:
2906059Samurai
29128679Sbrian    /*
29228679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
29328679Sbrian     * this is probably a data packet following an ack (normal on an
29428679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
29528679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
29628679Sbrian     * uncompressed in case the other side missed the compressed version.
29728679Sbrian     */
29828679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
29928679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
30028679Sbrian      break;
3016059Samurai
30228679Sbrian    /* (fall through) */
3036059Samurai
30428679Sbrian  case SPECIAL_I:
30528679Sbrian  case SPECIAL_D:
3066059Samurai
30728679Sbrian    /*
30828679Sbrian     * actual changes match one of our special case encodings -- send packet
30928679Sbrian     * uncompressed.
31028679Sbrian     */
31128679Sbrian    goto uncompressed;
3126059Samurai
31328679Sbrian  case NEW_S | NEW_A:
31428679Sbrian    if (deltaS == deltaA &&
31528679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
31628679Sbrian      /* special case for echoed terminal traffic */
31728679Sbrian      changes = SPECIAL_I;
31828679Sbrian      cp = new_seq;
31928679Sbrian    }
32028679Sbrian    break;
3216059Samurai
32228679Sbrian  case NEW_S:
32328679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
32428679Sbrian      /* special case for data xfer */
32528679Sbrian      changes = SPECIAL_D;
32628679Sbrian      cp = new_seq;
32728679Sbrian    }
32828679Sbrian    break;
32928679Sbrian  }
3306059Samurai
33128679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
33228679Sbrian  if (deltaS != 1) {
33328679Sbrian    ENCODEZ(deltaS);
33428679Sbrian    changes |= NEW_I;
33528679Sbrian  }
33628679Sbrian  if (th->th_flags & TH_PUSH)
33728679Sbrian    changes |= TCP_PUSH_BIT;
3386059Samurai
33928679Sbrian  /*
34028679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
34128679Sbrian   * this packet's header.
34228679Sbrian   */
34328679Sbrian  deltaA = ntohs(th->th_sum);
34428679Sbrian  BCOPY(ip, &cs->cs_ip, hlen);
3456059Samurai
34628679Sbrian  /*
34728679Sbrian   * We want to use the original packet as our compressed packet. (cp -
34828679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
34928679Sbrian   * In addition we need one byte for the change mask, one for the connection
35028679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
35128679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
35228679Sbrian   * subtract the two to get the new packet size.
35328679Sbrian   */
35428679Sbrian  deltaS = cp - new_seq;
35528679Sbrian  cp = (u_char *) ip;
35628679Sbrian
35728679Sbrian  /*
35828679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
35928679Sbrian   * know what order packets will go on the line.  In this case, we always
36028679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
36128679Sbrian   */
3626059Samurai#ifdef SL_NOFASTQ
36328679Sbrian  if (comp->last_xmit == cs->cs_id) {
36428679Sbrian    hlen -= deltaS + 3;
36528679Sbrian    cp += hlen;
36628679Sbrian    *cp++ = changes;
36728679Sbrian  } else
3686059Samurai#endif
36928679Sbrian  {
37028679Sbrian    comp->last_xmit = cs->cs_id;
37128679Sbrian    hlen -= deltaS + 4;
37228679Sbrian    cp += hlen;
37328679Sbrian    *cp++ = changes | NEW_C;
37428679Sbrian    *cp++ = cs->cs_id;
37528679Sbrian  }
37628679Sbrian  m->cnt -= hlen;
37728679Sbrian  m->offset += hlen;
37828679Sbrian  *cp++ = deltaA >> 8;
37928679Sbrian  *cp++ = deltaA;
38028679Sbrian  BCOPY(new_seq, cp, deltaS);
38128679Sbrian  INCR(sls_compressed)
38228679Sbrian    return (TYPE_COMPRESSED_TCP);
3836059Samurai
38428679Sbrian  /*
38528679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
38628679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
38728679Sbrian   * use on future compressed packets in the protocol field).
38828679Sbrian   */
3896059Samuraiuncompressed:
39028679Sbrian  BCOPY(ip, &cs->cs_ip, hlen);
39128679Sbrian  ip->ip_p = cs->cs_id;
39228679Sbrian  comp->last_xmit = cs->cs_id;
39328679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
3946059Samurai}
3956059Samurai
3966059Samurai
3976059Samuraiint
39828679Sbriansl_uncompress_tcp(u_char ** bufp,
39928679Sbrian		  int len,
40028679Sbrian		  u_int type,
40128679Sbrian		  struct slcompress * comp)
4026059Samurai{
40328679Sbrian  register u_char *cp;
40428679Sbrian  register u_int hlen, changes;
40528679Sbrian  register struct tcphdr *th;
40628679Sbrian  register struct cstate *cs;
40728679Sbrian  register struct ip *ip;
4086059Samurai
40928679Sbrian  switch (type) {
4106059Samurai
41128679Sbrian  case TYPE_UNCOMPRESSED_TCP:
41228679Sbrian    ip = (struct ip *) * bufp;
41328679Sbrian    if (ip->ip_p >= MAX_STATES)
41428679Sbrian      goto bad;
41528679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
41628679Sbrian    comp->flags &= ~SLF_TOSS;
41728679Sbrian    ip->ip_p = IPPROTO_TCP;
4186059Samurai
41928679Sbrian    /*
42028679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
42128679Sbrian     * overflow the space we have available for it.
42228679Sbrian     */
42328679Sbrian    hlen = ip->ip_hl << 2;
42428679Sbrian    if (hlen + sizeof(struct tcphdr) > len)
42528679Sbrian      goto bad;
42628679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
42728679Sbrian    hlen += THOFFSET(th) << 2;
42828679Sbrian    if (hlen > MAX_HDR)
42928679Sbrian      goto bad;
43028679Sbrian    BCOPY(ip, &cs->cs_ip, hlen);
43128679Sbrian    cs->cs_ip.ip_sum = 0;
43228679Sbrian    cs->cs_hlen = hlen;
43328679Sbrian    INCR(sls_uncompressedin)
43428679Sbrian      return (len);
4356059Samurai
43628679Sbrian  default:
43728679Sbrian    goto bad;
4386059Samurai
43928679Sbrian  case TYPE_COMPRESSED_TCP:
44028679Sbrian    break;
44128679Sbrian  }
44228679Sbrian  /* We've got a compressed packet. */
44328679Sbrian  INCR(sls_compressedin)
44428679Sbrian    cp = *bufp;
44528679Sbrian  changes = *cp++;
44628679Sbrian  LogPrintf(LogDEBUG, "compressed: changes = %02x\n", changes);
44728679Sbrian  if (changes & NEW_C) {
4486059Samurai
44928679Sbrian    /*
45028679Sbrian     * Make sure the state index is in range, then grab the state. If we have
45128679Sbrian     * a good state index, clear the 'discard' flag.
45228679Sbrian     */
45328679Sbrian    if (*cp >= MAX_STATES || comp->last_recv == 255)
45428679Sbrian      goto bad;
4556059Samurai
45628679Sbrian    comp->flags &= ~SLF_TOSS;
45728679Sbrian    comp->last_recv = *cp++;
45828679Sbrian  } else {
4596059Samurai
46028679Sbrian    /*
46128679Sbrian     * this packet has an implicit state index.  If we've had a line error
46228679Sbrian     * since the last time we got an explicit state index, we have to toss
46328679Sbrian     * the packet.
46428679Sbrian     */
46528679Sbrian    if (comp->flags & SLF_TOSS) {
46628679Sbrian      INCR(sls_tossed)
46728679Sbrian	return (0);
46828679Sbrian    }
46928679Sbrian  }
47028679Sbrian  cs = &comp->rstate[comp->last_recv];
47128679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
47228679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
47328679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
47428679Sbrian  cp += 2;
47528679Sbrian  if (changes & TCP_PUSH_BIT)
47628679Sbrian    th->th_flags |= TH_PUSH;
47728679Sbrian  else
47828679Sbrian    th->th_flags &= ~TH_PUSH;
47928679Sbrian
48028679Sbrian  switch (changes & SPECIALS_MASK) {
48128679Sbrian  case SPECIAL_I:
48228679Sbrian    {
48328679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
48428679Sbrian
48528679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
48628679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
48728679Sbrian    }
48828679Sbrian    break;
48928679Sbrian
49028679Sbrian  case SPECIAL_D:
49128679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
49228679Sbrian		       - cs->cs_hlen);
49328679Sbrian    break;
49428679Sbrian
49528679Sbrian  default:
49628679Sbrian    if (changes & NEW_U) {
49728679Sbrian      th->th_flags |= TH_URG;
49828679Sbrian      DECODEU(th->th_urp)
49928679Sbrian    } else
50028679Sbrian      th->th_flags &= ~TH_URG;
50128679Sbrian    if (changes & NEW_W)
50228679Sbrian      DECODES(th->th_win)
50328679Sbrian	if (changes & NEW_A)
50428679Sbrian	DECODEL(th->th_ack)
50528679Sbrian	  if (changes & NEW_S) {
50628679Sbrian	  LogPrintf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
50728679Sbrian		    *cp, cp[1], cp[2]);
50828679Sbrian	  DECODEL(th->th_seq)
5096059Samurai	}
51028679Sbrian    break;
51128679Sbrian  }
51228679Sbrian  if (changes & NEW_I) {
51328679Sbrian    DECODES(cs->cs_ip.ip_id)
51428679Sbrian  } else
51528679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5166059Samurai
51728679Sbrian  LogPrintf(LogDEBUG, "Uncompress: id = %04x, seq = %08x\n",
51828679Sbrian	    cs->cs_ip.ip_id, ntohl(th->th_seq));
51926516Sbrian
52028679Sbrian  /*
52128679Sbrian   * At this point, cp points to the first byte of data in the packet.  If
52228679Sbrian   * we're not aligned on a 4-byte boundary, copy the data down so the ip &
52328679Sbrian   * tcp headers will be aligned.  Then back up cp by the tcp/ip header
52428679Sbrian   * length to make room for the reconstructed header (we assume the packet
52528679Sbrian   * we were handed has enough space to prepend 128 bytes of header).  Adjust
52628679Sbrian   * the length to account for the new header & fill in the IP total length.
52728679Sbrian   */
52828679Sbrian  len -= (cp - *bufp);
52928679Sbrian  if (len < 0)
5306059Samurai
53128679Sbrian    /*
53228679Sbrian     * we must have dropped some characters (crc should detect this but the
53328679Sbrian     * old slip framing won't)
53428679Sbrian     */
53528679Sbrian    goto bad;
53628679Sbrian
5376059Samurai#ifdef notdef
53828679Sbrian  if ((int) cp & 3) {
53928679Sbrian    if (len > 0)
54028679Sbrian      (void) ovbcopy(cp, (caddr_t) ((int) cp & ~3), len);
54128679Sbrian    cp = (u_char *) ((int) cp & ~3);
54228679Sbrian  }
5436059Samurai#endif
5446059Samurai
54528679Sbrian  cp -= cs->cs_hlen;
54628679Sbrian  len += cs->cs_hlen;
54728679Sbrian  cs->cs_ip.ip_len = htons(len);
54828679Sbrian  BCOPY(&cs->cs_ip, cp, cs->cs_hlen);
54928679Sbrian  *bufp = cp;
5506059Samurai
55128679Sbrian  /* recompute the ip header checksum */
55228679Sbrian  {
55328679Sbrian    register u_short *bp = (u_short *) cp;
55428679Sbrian
55528679Sbrian    for (changes = 0; hlen > 0; hlen -= 2)
55628679Sbrian      changes += *bp++;
55728679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
55828679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
55928679Sbrian    ((struct ip *) cp)->ip_sum = ~changes;
56028679Sbrian  }
56128679Sbrian  return (len);
5626059Samuraibad:
56328679Sbrian  comp->flags |= SLF_TOSS;
56428679Sbrian  INCR(sls_errorin)
56528679Sbrian    return (0);
5666059Samurai}
5676059Samurai
5686059Samuraiint
5696059SamuraiReportCompress()
5706059Samurai{
57126516Sbrian  if (!VarTerm)
57226516Sbrian    return 1;
57326516Sbrian
57426516Sbrian  fprintf(VarTerm, "Out:  %d (compress) / %d (total)",
57528679Sbrian	  slstat.sls_compressed, slstat.sls_packets);
57626516Sbrian  fprintf(VarTerm, "  %d (miss) / %d (search)\n",
57728679Sbrian	  slstat.sls_misses, slstat.sls_searches);
57826516Sbrian  fprintf(VarTerm, "In:  %d (compress), %d (uncompress)",
57928679Sbrian	  slstat.sls_compressedin, slstat.sls_uncompressedin);
58026516Sbrian  fprintf(VarTerm, "  %d (error),  %d (tossed)\n",
58128679Sbrian	  slstat.sls_errorin, slstat.sls_tossed);
58226516Sbrian  return 0;
5836059Samurai}
584