slcompress.c revision 30715
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 *
2030715Sbrian * $Id: slcompress.c,v 1.12 1997/10/12 21:43:55 brian Exp $
218857Srgrimes *
226059Samurai *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
236059Samurai *	- Initial distribution.
246059Samurai */
2528679Sbrian
2630715Sbrian#include <sys/param.h>
276059Samurai#include <netinet/in_systm.h>
286059Samurai#include <netinet/in.h>
296059Samurai#include <netinet/tcp.h>
306059Samurai#include <netinet/ip.h>
3130715Sbrian
3230715Sbrian#include <stdio.h>
3330715Sbrian#include <string.h>
3430715Sbrian
3530715Sbrian#include "mbuf.h"
3630715Sbrian#include "log.h"
3730715Sbrian#include "defs.h"
386059Samurai#include "slcompress.h"
3926516Sbrian#include "loadalias.h"
4030715Sbrian#include "command.h"
4126516Sbrian#include "vars.h"
426059Samurai
4330715Sbrianstatic struct slstat {
4430715Sbrian  int sls_packets;		/* outbound packets */
4530715Sbrian  int sls_compressed;		/* outbound compressed packets */
4630715Sbrian  int sls_searches;		/* searches for connection state */
4730715Sbrian  int sls_misses;		/* times couldn't find conn. state */
4830715Sbrian  int sls_uncompressedin;	/* inbound uncompressed packets */
4930715Sbrian  int sls_compressedin;		/* inbound compressed packets */
5030715Sbrian  int sls_errorin;		/* inbound unknown type packets */
5130715Sbrian  int sls_tossed;		/* inbound packets tossed because of error */
5230715Sbrian} slstat;
536059Samurai
546059Samurai#define INCR(counter)	slstat.counter++;
556059Samurai
566059Samuraivoid
5730187Sbriansl_compress_init(struct slcompress * comp, int max_state)
586059Samurai{
5928679Sbrian  register u_int i;
6028679Sbrian  register struct cstate *tstate = comp->tstate;
616059Samurai
6230715Sbrian  memset(comp, '\0', sizeof(*comp));
6330187Sbrian  for (i = max_state; i > 0; --i) {
6428679Sbrian    tstate[i].cs_id = i;
6528679Sbrian    tstate[i].cs_next = &tstate[i - 1];
6628679Sbrian  }
6730187Sbrian  tstate[0].cs_next = &tstate[max_state];
6828679Sbrian  tstate[0].cs_id = 0;
6928679Sbrian  comp->last_cs = &tstate[0];
7028679Sbrian  comp->last_recv = 255;
7128679Sbrian  comp->last_xmit = 255;
7228679Sbrian  comp->flags = SLF_TOSS;
736059Samurai}
746059Samurai
756059Samurai
766059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
776059Samurai * checks for zero (since zero has to be encoded in the long, 3 byte
786059Samurai * form).
796059Samurai */
806059Samurai#define ENCODE(n) { \
816059Samurai	if ((u_short)(n) >= 256) { \
826059Samurai		*cp++ = 0; \
836059Samurai		cp[1] = (n); \
846059Samurai		cp[0] = (n) >> 8; \
856059Samurai		cp += 2; \
866059Samurai	} else { \
876059Samurai		*cp++ = (n); \
886059Samurai	} \
896059Samurai}
906059Samurai#define ENCODEZ(n) { \
916059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
926059Samurai		*cp++ = 0; \
936059Samurai		cp[1] = (n); \
946059Samurai		cp[0] = (n) >> 8; \
956059Samurai		cp += 2; \
966059Samurai	} else { \
976059Samurai		*cp++ = (n); \
986059Samurai	} \
996059Samurai}
1006059Samurai
1016059Samurai#define DECODEL(f) { \
1026059Samurai	if (*cp == 0) {\
1036059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
1046059Samurai		cp += 3; \
1056059Samurai	} else { \
1066059Samurai		(f) = htonl(ntohl(f) + (u_long)*cp++); \
1076059Samurai	} \
1086059Samurai}
1096059Samurai
1106059Samurai#define DECODES(f) { \
1116059Samurai	if (*cp == 0) {\
1126059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1136059Samurai		cp += 3; \
1146059Samurai	} else { \
1156059Samurai		(f) = htons(ntohs(f) + (u_long)*cp++); \
1166059Samurai	} \
1176059Samurai}
1186059Samurai
1196059Samurai#define DECODEU(f) { \
1206059Samurai	if (*cp == 0) {\
1216059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1226059Samurai		cp += 3; \
1236059Samurai	} else { \
1246059Samurai		(f) = htons((u_long)*cp++); \
1256059Samurai	} \
1266059Samurai}
1276059Samurai
1286059Samurai
1296059Samuraiu_char
13028679Sbriansl_compress_tcp(struct mbuf * m,
13128679Sbrian		struct ip * ip,
13228679Sbrian		struct slcompress * comp,
13328679Sbrian		int compress_cid)
1346059Samurai{
13528679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
13628679Sbrian  register u_int hlen = ip->ip_hl;
13728679Sbrian  register struct tcphdr *oth;
13828679Sbrian  register struct tcphdr *th;
13928679Sbrian  register u_int deltaS, deltaA;
14028679Sbrian  register u_int changes = 0;
14128679Sbrian  u_char new_seq[16];
14228679Sbrian  register u_char *cp = new_seq;
1436059Samurai
14428679Sbrian  /*
14528679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
14628679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
14728679Sbrian   * the caller has already made sure the packet is IP proto TCP).
14828679Sbrian   */
14928679Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) {
15028679Sbrian    LogPrintf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n",
15128679Sbrian	      ip->ip_off, m->cnt);
15228679Sbrian    LogDumpBp(LogDEBUG, "", m);
15328679Sbrian    return (TYPE_IP);
15428679Sbrian  }
15528679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
15628679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
15728679Sbrian    LogPrintf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
15828679Sbrian    LogDumpBp(LogDEBUG, "", m);
15928679Sbrian    return (TYPE_IP);
16028679Sbrian  }
1616059Samurai
16228679Sbrian  /*
16328679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
16428679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
16528679Sbrian   * connection state.  Special case the most recently used connection since
16628679Sbrian   * it's most likely to be used again & we don't have to do any reordering
16728679Sbrian   * if it's used.
16828679Sbrian   */
16928679Sbrian  INCR(sls_packets)
17028679Sbrian    if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
17128679Sbrian	ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
17228679Sbrian	*(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1736059Samurai
17428679Sbrian    /*
17528679Sbrian     * Wasn't the first -- search for it.
17628679Sbrian     *
17728679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
17828679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
17928679Sbrian     * the head of the list whenever it is referenced.  Since the list is
18028679Sbrian     * short and, empirically, the connection we want is almost always near
18128679Sbrian     * the front, we locate states via linear search.  If we don't find a
18228679Sbrian     * state for the datagram, the oldest state is (re-)used.
18328679Sbrian     */
18428679Sbrian    register struct cstate *lcs;
18528679Sbrian    register struct cstate *lastcs = comp->last_cs;
1866059Samurai
18728679Sbrian    do {
18828679Sbrian      lcs = cs;
18928679Sbrian      cs = cs->cs_next;
19028679Sbrian      INCR(sls_searches)
19128679Sbrian	if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
19228679Sbrian	    && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
19328679Sbrian	    && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
19428679Sbrian	goto found;
19528679Sbrian    } while (cs != lastcs);
1966059Samurai
19728679Sbrian    /*
19828679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
19928679Sbrian     * that tells the other side what connection number we're using for this
20028679Sbrian     * conversation. Note that since the state list is circular, the oldest
20128679Sbrian     * state points to the newest and we only need to set last_cs to update
20228679Sbrian     * the lru linkage.
20328679Sbrian     */
20428679Sbrian    INCR(sls_misses)
20528679Sbrian      comp->last_cs = lcs;
2066059Samurai#define	THOFFSET(th)	(th->th_off)
20728679Sbrian    hlen += th->th_off;
20828679Sbrian    hlen <<= 2;
20928679Sbrian    if (hlen > m->cnt)
21028679Sbrian      return (TYPE_IP);
21128679Sbrian    goto uncompressed;
2126059Samurai
21328679Sbrianfound:
2146059Samurai
21528679Sbrian    /*
21628679Sbrian     * Found it -- move to the front on the connection list.
21728679Sbrian     */
21828679Sbrian    if (cs == lastcs)
21928679Sbrian      comp->last_cs = lcs;
22028679Sbrian    else {
22128679Sbrian      lcs->cs_next = cs->cs_next;
22228679Sbrian      cs->cs_next = lastcs->cs_next;
22328679Sbrian      lastcs->cs_next = cs;
22428679Sbrian    }
22528679Sbrian  }
2266059Samurai
22728679Sbrian  /*
22828679Sbrian   * Make sure that only what we expect to change changed. The first line of
22928679Sbrian   * the `if' checks the IP protocol version, header length & type of
23028679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
23128679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
23228679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
23328679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
23428679Sbrian   * any of these things are different between the previous & current
23528679Sbrian   * datagram, we send the current datagram `uncompressed'.
23628679Sbrian   */
23728679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
23828679Sbrian  deltaS = hlen;
23928679Sbrian  hlen += th->th_off;
24028679Sbrian  hlen <<= 2;
24128679Sbrian  if (hlen > m->cnt)
24228679Sbrian    return (TYPE_IP);
2436059Samurai
24428679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
24528679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
24628679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
24728679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
24828679Sbrian      (deltaS > 5 &&
24930715Sbrian       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
25028679Sbrian      (THOFFSET(th) > 5 &&
25130715Sbrian       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
25228679Sbrian    goto uncompressed;
25328679Sbrian  }
2546059Samurai
25528679Sbrian  /*
25628679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
25728679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
25828679Sbrian   * number of temporaries needed in this section of code).
25928679Sbrian   */
26028679Sbrian  if (th->th_flags & TH_URG) {
26128679Sbrian    deltaS = ntohs(th->th_urp);
26228679Sbrian    ENCODEZ(deltaS);
26328679Sbrian    changes |= NEW_U;
26428679Sbrian  } else if (th->th_urp != oth->th_urp) {
2656059Samurai
26628679Sbrian    /*
26728679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
26828679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
26928679Sbrian     * deal with it.
27028679Sbrian     */
27128679Sbrian    goto uncompressed;
27228679Sbrian  }
27328679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
27428679Sbrian  if (deltaS) {
27528679Sbrian    ENCODE(deltaS);
27628679Sbrian    changes |= NEW_W;
27728679Sbrian  }
27828679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
27928679Sbrian  if (deltaA) {
28028679Sbrian    if (deltaA > 0xffff) {
28128679Sbrian      goto uncompressed;
28228679Sbrian    }
28328679Sbrian    ENCODE(deltaA);
28428679Sbrian    changes |= NEW_A;
28528679Sbrian  }
28628679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
28728679Sbrian  if (deltaS) {
28828679Sbrian    if (deltaS > 0xffff) {
28928679Sbrian      goto uncompressed;
29028679Sbrian    }
29128679Sbrian    ENCODE(deltaS);
29228679Sbrian    changes |= NEW_S;
29328679Sbrian  }
29428679Sbrian  switch (changes) {
2956059Samurai
29628679Sbrian  case 0:
2976059Samurai
29828679Sbrian    /*
29928679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
30028679Sbrian     * this is probably a data packet following an ack (normal on an
30128679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
30228679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
30328679Sbrian     * uncompressed in case the other side missed the compressed version.
30428679Sbrian     */
30528679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
30628679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
30728679Sbrian      break;
3086059Samurai
30928679Sbrian    /* (fall through) */
3106059Samurai
31128679Sbrian  case SPECIAL_I:
31228679Sbrian  case SPECIAL_D:
3136059Samurai
31428679Sbrian    /*
31528679Sbrian     * actual changes match one of our special case encodings -- send packet
31628679Sbrian     * uncompressed.
31728679Sbrian     */
31828679Sbrian    goto uncompressed;
3196059Samurai
32028679Sbrian  case NEW_S | NEW_A:
32128679Sbrian    if (deltaS == deltaA &&
32228679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
32328679Sbrian      /* special case for echoed terminal traffic */
32428679Sbrian      changes = SPECIAL_I;
32528679Sbrian      cp = new_seq;
32628679Sbrian    }
32728679Sbrian    break;
3286059Samurai
32928679Sbrian  case NEW_S:
33028679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33128679Sbrian      /* special case for data xfer */
33228679Sbrian      changes = SPECIAL_D;
33328679Sbrian      cp = new_seq;
33428679Sbrian    }
33528679Sbrian    break;
33628679Sbrian  }
3376059Samurai
33828679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
33928679Sbrian  if (deltaS != 1) {
34028679Sbrian    ENCODEZ(deltaS);
34128679Sbrian    changes |= NEW_I;
34228679Sbrian  }
34328679Sbrian  if (th->th_flags & TH_PUSH)
34428679Sbrian    changes |= TCP_PUSH_BIT;
3456059Samurai
34628679Sbrian  /*
34728679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
34828679Sbrian   * this packet's header.
34928679Sbrian   */
35028679Sbrian  deltaA = ntohs(th->th_sum);
35130715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
3526059Samurai
35328679Sbrian  /*
35428679Sbrian   * We want to use the original packet as our compressed packet. (cp -
35528679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
35628679Sbrian   * In addition we need one byte for the change mask, one for the connection
35728679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
35828679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
35928679Sbrian   * subtract the two to get the new packet size.
36028679Sbrian   */
36128679Sbrian  deltaS = cp - new_seq;
36228679Sbrian  cp = (u_char *) ip;
36328679Sbrian
36428679Sbrian  /*
36528679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
36628679Sbrian   * know what order packets will go on the line.  In this case, we always
36728679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
36828679Sbrian   */
36930187Sbrian  if (comp->last_xmit == cs->cs_id && compress_cid) {
37028679Sbrian    hlen -= deltaS + 3;
37128679Sbrian    cp += hlen;
37228679Sbrian    *cp++ = changes;
37330187Sbrian  } else {
37428679Sbrian    comp->last_xmit = cs->cs_id;
37528679Sbrian    hlen -= deltaS + 4;
37628679Sbrian    cp += hlen;
37728679Sbrian    *cp++ = changes | NEW_C;
37828679Sbrian    *cp++ = cs->cs_id;
37928679Sbrian  }
38028679Sbrian  m->cnt -= hlen;
38128679Sbrian  m->offset += hlen;
38228679Sbrian  *cp++ = deltaA >> 8;
38328679Sbrian  *cp++ = deltaA;
38430715Sbrian  memcpy(cp, new_seq, deltaS);
38528679Sbrian  INCR(sls_compressed)
38628679Sbrian    return (TYPE_COMPRESSED_TCP);
3876059Samurai
38828679Sbrian  /*
38928679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
39028679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
39128679Sbrian   * use on future compressed packets in the protocol field).
39228679Sbrian   */
3936059Samuraiuncompressed:
39430715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
39528679Sbrian  ip->ip_p = cs->cs_id;
39628679Sbrian  comp->last_xmit = cs->cs_id;
39728679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
3986059Samurai}
3996059Samurai
4006059Samurai
4016059Samuraiint
40228679Sbriansl_uncompress_tcp(u_char ** bufp,
40328679Sbrian		  int len,
40428679Sbrian		  u_int type,
40528679Sbrian		  struct slcompress * comp)
4066059Samurai{
40728679Sbrian  register u_char *cp;
40828679Sbrian  register u_int hlen, changes;
40928679Sbrian  register struct tcphdr *th;
41028679Sbrian  register struct cstate *cs;
41128679Sbrian  register struct ip *ip;
4126059Samurai
41328679Sbrian  switch (type) {
4146059Samurai
41528679Sbrian  case TYPE_UNCOMPRESSED_TCP:
41628679Sbrian    ip = (struct ip *) * bufp;
41728679Sbrian    if (ip->ip_p >= MAX_STATES)
41828679Sbrian      goto bad;
41928679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
42028679Sbrian    comp->flags &= ~SLF_TOSS;
42128679Sbrian    ip->ip_p = IPPROTO_TCP;
4226059Samurai
42328679Sbrian    /*
42428679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
42528679Sbrian     * overflow the space we have available for it.
42628679Sbrian     */
42728679Sbrian    hlen = ip->ip_hl << 2;
42828679Sbrian    if (hlen + sizeof(struct tcphdr) > len)
42928679Sbrian      goto bad;
43028679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
43128679Sbrian    hlen += THOFFSET(th) << 2;
43228679Sbrian    if (hlen > MAX_HDR)
43328679Sbrian      goto bad;
43430715Sbrian    memcpy(&cs->cs_ip, ip, hlen);
43530357Sbrian    cs->cs_ip.ip_sum = 0;
43628679Sbrian    cs->cs_hlen = hlen;
43728679Sbrian    INCR(sls_uncompressedin)
43828679Sbrian      return (len);
4396059Samurai
44028679Sbrian  default:
44128679Sbrian    goto bad;
4426059Samurai
44328679Sbrian  case TYPE_COMPRESSED_TCP:
44428679Sbrian    break;
44528679Sbrian  }
44628679Sbrian  /* We've got a compressed packet. */
44728679Sbrian  INCR(sls_compressedin)
44828679Sbrian    cp = *bufp;
44928679Sbrian  changes = *cp++;
45028679Sbrian  LogPrintf(LogDEBUG, "compressed: changes = %02x\n", changes);
45128679Sbrian  if (changes & NEW_C) {
4526059Samurai
45328679Sbrian    /*
45428679Sbrian     * Make sure the state index is in range, then grab the state. If we have
45528679Sbrian     * a good state index, clear the 'discard' flag.
45628679Sbrian     */
45728679Sbrian    if (*cp >= MAX_STATES || comp->last_recv == 255)
45828679Sbrian      goto bad;
4596059Samurai
46028679Sbrian    comp->flags &= ~SLF_TOSS;
46128679Sbrian    comp->last_recv = *cp++;
46228679Sbrian  } else {
4636059Samurai
46428679Sbrian    /*
46528679Sbrian     * this packet has an implicit state index.  If we've had a line error
46628679Sbrian     * since the last time we got an explicit state index, we have to toss
46728679Sbrian     * the packet.
46828679Sbrian     */
46928679Sbrian    if (comp->flags & SLF_TOSS) {
47028679Sbrian      INCR(sls_tossed)
47128679Sbrian	return (0);
47228679Sbrian    }
47328679Sbrian  }
47428679Sbrian  cs = &comp->rstate[comp->last_recv];
47528679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
47628679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
47728679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
47828679Sbrian  cp += 2;
47928679Sbrian  if (changes & TCP_PUSH_BIT)
48028679Sbrian    th->th_flags |= TH_PUSH;
48128679Sbrian  else
48228679Sbrian    th->th_flags &= ~TH_PUSH;
48328679Sbrian
48428679Sbrian  switch (changes & SPECIALS_MASK) {
48528679Sbrian  case SPECIAL_I:
48628679Sbrian    {
48728679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
48828679Sbrian
48928679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
49028679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
49128679Sbrian    }
49228679Sbrian    break;
49328679Sbrian
49428679Sbrian  case SPECIAL_D:
49528679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
49628679Sbrian		       - cs->cs_hlen);
49728679Sbrian    break;
49828679Sbrian
49928679Sbrian  default:
50028679Sbrian    if (changes & NEW_U) {
50128679Sbrian      th->th_flags |= TH_URG;
50228679Sbrian      DECODEU(th->th_urp)
50328679Sbrian    } else
50428679Sbrian      th->th_flags &= ~TH_URG;
50528679Sbrian    if (changes & NEW_W)
50628679Sbrian      DECODES(th->th_win)
50728679Sbrian	if (changes & NEW_A)
50828679Sbrian	DECODEL(th->th_ack)
50928679Sbrian	  if (changes & NEW_S) {
51028679Sbrian	  LogPrintf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
51128679Sbrian		    *cp, cp[1], cp[2]);
51228679Sbrian	  DECODEL(th->th_seq)
5136059Samurai	}
51428679Sbrian    break;
51528679Sbrian  }
51628679Sbrian  if (changes & NEW_I) {
51728679Sbrian    DECODES(cs->cs_ip.ip_id)
51828679Sbrian  } else
51928679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5206059Samurai
52128679Sbrian  LogPrintf(LogDEBUG, "Uncompress: id = %04x, seq = %08x\n",
52228679Sbrian	    cs->cs_ip.ip_id, ntohl(th->th_seq));
52326516Sbrian
52428679Sbrian  /*
52528679Sbrian   * At this point, cp points to the first byte of data in the packet.  If
52628679Sbrian   * we're not aligned on a 4-byte boundary, copy the data down so the ip &
52728679Sbrian   * tcp headers will be aligned.  Then back up cp by the tcp/ip header
52828679Sbrian   * length to make room for the reconstructed header (we assume the packet
52928679Sbrian   * we were handed has enough space to prepend 128 bytes of header).  Adjust
53028679Sbrian   * the length to account for the new header & fill in the IP total length.
53128679Sbrian   */
53228679Sbrian  len -= (cp - *bufp);
53328679Sbrian  if (len < 0)
5346059Samurai
53528679Sbrian    /*
53628679Sbrian     * we must have dropped some characters (crc should detect this but the
53728679Sbrian     * old slip framing won't)
53828679Sbrian     */
53928679Sbrian    goto bad;
54028679Sbrian
5416059Samurai#ifdef notdef
54228679Sbrian  if ((int) cp & 3) {
54328679Sbrian    if (len > 0)
54430715Sbrian      (void) bcopy(cp, (caddr_t) ((int) cp & ~3), len);
54528679Sbrian    cp = (u_char *) ((int) cp & ~3);
54628679Sbrian  }
5476059Samurai#endif
5486059Samurai
54928679Sbrian  cp -= cs->cs_hlen;
55028679Sbrian  len += cs->cs_hlen;
55128679Sbrian  cs->cs_ip.ip_len = htons(len);
55230715Sbrian  memcpy(cp, &cs->cs_ip, cs->cs_hlen);
55328679Sbrian  *bufp = cp;
5546059Samurai
55528679Sbrian  /* recompute the ip header checksum */
55628679Sbrian  {
55728679Sbrian    register u_short *bp = (u_short *) cp;
55828679Sbrian
55928679Sbrian    for (changes = 0; hlen > 0; hlen -= 2)
56028679Sbrian      changes += *bp++;
56128679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
56228679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
56328679Sbrian    ((struct ip *) cp)->ip_sum = ~changes;
56428679Sbrian  }
56528679Sbrian  return (len);
5666059Samuraibad:
56728679Sbrian  comp->flags |= SLF_TOSS;
56828679Sbrian  INCR(sls_errorin)
56928679Sbrian    return (0);
5706059Samurai}
5716059Samurai
5726059Samuraiint
5736059SamuraiReportCompress()
5746059Samurai{
57526516Sbrian  if (!VarTerm)
57626516Sbrian    return 1;
57726516Sbrian
57826516Sbrian  fprintf(VarTerm, "Out:  %d (compress) / %d (total)",
57928679Sbrian	  slstat.sls_compressed, slstat.sls_packets);
58026516Sbrian  fprintf(VarTerm, "  %d (miss) / %d (search)\n",
58128679Sbrian	  slstat.sls_misses, slstat.sls_searches);
58226516Sbrian  fprintf(VarTerm, "In:  %d (compress), %d (uncompress)",
58328679Sbrian	  slstat.sls_compressedin, slstat.sls_uncompressedin);
58426516Sbrian  fprintf(VarTerm, "  %d (error),  %d (tossed)\n",
58528679Sbrian	  slstat.sls_errorin, slstat.sls_tossed);
58626516Sbrian  return 0;
5876059Samurai}
588