slcompress.c revision 37009
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 *
2037009Sbrian * $Id: slcompress.c,v 1.17 1998/06/14 00:56:11 brian Exp $
218857Srgrimes *
226059Samurai *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
236059Samurai *	- Initial distribution.
246059Samurai */
2528679Sbrian
2636285Sbrian#include <sys/types.h>
276059Samurai#include <netinet/in_systm.h>
286059Samurai#include <netinet/in.h>
296059Samurai#include <netinet/tcp.h>
306059Samurai#include <netinet/ip.h>
3136285Sbrian#include <sys/un.h>
3230715Sbrian
3330715Sbrian#include <stdio.h>
3430715Sbrian#include <string.h>
3536285Sbrian#include <termios.h>
3630715Sbrian
3737009Sbrian#include "defs.h"
3831343Sbrian#include "command.h"
3930715Sbrian#include "mbuf.h"
4030715Sbrian#include "log.h"
416059Samurai#include "slcompress.h"
4236285Sbrian#include "descriptor.h"
4336285Sbrian#include "prompt.h"
4436285Sbrian#include "timer.h"
4536285Sbrian#include "fsm.h"
4636285Sbrian#include "throughput.h"
4736285Sbrian#include "iplist.h"
4836285Sbrian#include "ipcp.h"
4936285Sbrian#include "filter.h"
5036285Sbrian#include "lqr.h"
5136285Sbrian#include "hdlc.h"
5236285Sbrian#include "lcp.h"
5336285Sbrian#include "ccp.h"
5436285Sbrian#include "link.h"
5536285Sbrian#include "mp.h"
5636285Sbrian#include "bundle.h"
576059Samurai
586059Samuraivoid
5930187Sbriansl_compress_init(struct slcompress * comp, int max_state)
606059Samurai{
6128679Sbrian  register u_int i;
6228679Sbrian  register struct cstate *tstate = comp->tstate;
636059Samurai
6431962Sbrian  memset(comp, '\0', sizeof *comp);
6530187Sbrian  for (i = max_state; i > 0; --i) {
6628679Sbrian    tstate[i].cs_id = i;
6728679Sbrian    tstate[i].cs_next = &tstate[i - 1];
6828679Sbrian  }
6930187Sbrian  tstate[0].cs_next = &tstate[max_state];
7028679Sbrian  tstate[0].cs_id = 0;
7128679Sbrian  comp->last_cs = &tstate[0];
7228679Sbrian  comp->last_recv = 255;
7328679Sbrian  comp->last_xmit = 255;
7428679Sbrian  comp->flags = SLF_TOSS;
756059Samurai}
766059Samurai
776059Samurai
786059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
796059Samurai * checks for zero (since zero has to be encoded in the long, 3 byte
806059Samurai * form).
816059Samurai */
826059Samurai#define ENCODE(n) { \
836059Samurai	if ((u_short)(n) >= 256) { \
846059Samurai		*cp++ = 0; \
856059Samurai		cp[1] = (n); \
866059Samurai		cp[0] = (n) >> 8; \
876059Samurai		cp += 2; \
886059Samurai	} else { \
896059Samurai		*cp++ = (n); \
906059Samurai	} \
916059Samurai}
926059Samurai#define ENCODEZ(n) { \
936059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
946059Samurai		*cp++ = 0; \
956059Samurai		cp[1] = (n); \
966059Samurai		cp[0] = (n) >> 8; \
976059Samurai		cp += 2; \
986059Samurai	} else { \
996059Samurai		*cp++ = (n); \
1006059Samurai	} \
1016059Samurai}
1026059Samurai
1036059Samurai#define DECODEL(f) { \
1046059Samurai	if (*cp == 0) {\
1056059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
1066059Samurai		cp += 3; \
1076059Samurai	} else { \
1086059Samurai		(f) = htonl(ntohl(f) + (u_long)*cp++); \
1096059Samurai	} \
1106059Samurai}
1116059Samurai
1126059Samurai#define DECODES(f) { \
1136059Samurai	if (*cp == 0) {\
1146059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1156059Samurai		cp += 3; \
1166059Samurai	} else { \
1176059Samurai		(f) = htons(ntohs(f) + (u_long)*cp++); \
1186059Samurai	} \
1196059Samurai}
1206059Samurai
1216059Samurai#define DECODEU(f) { \
1226059Samurai	if (*cp == 0) {\
1236059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1246059Samurai		cp += 3; \
1256059Samurai	} else { \
1266059Samurai		(f) = htons((u_long)*cp++); \
1276059Samurai	} \
1286059Samurai}
1296059Samurai
1306059Samurai
1316059Samuraiu_char
13228679Sbriansl_compress_tcp(struct mbuf * m,
13328679Sbrian		struct ip * ip,
13436285Sbrian		struct slcompress *comp,
13536285Sbrian                struct slstat *slstat,
13628679Sbrian		int compress_cid)
1376059Samurai{
13828679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
13928679Sbrian  register u_int hlen = ip->ip_hl;
14028679Sbrian  register struct tcphdr *oth;
14128679Sbrian  register struct tcphdr *th;
14228679Sbrian  register u_int deltaS, deltaA;
14328679Sbrian  register u_int changes = 0;
14428679Sbrian  u_char new_seq[16];
14528679Sbrian  register u_char *cp = new_seq;
1466059Samurai
14728679Sbrian  /*
14828679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
14928679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
15028679Sbrian   * the caller has already made sure the packet is IP proto TCP).
15128679Sbrian   */
15228679Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) {
15336285Sbrian    log_Printf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n",
15428679Sbrian	      ip->ip_off, m->cnt);
15536285Sbrian    log_DumpBp(LogDEBUG, "", m);
15628679Sbrian    return (TYPE_IP);
15728679Sbrian  }
15828679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
15928679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
16036285Sbrian    log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
16136285Sbrian    log_DumpBp(LogDEBUG, "", m);
16228679Sbrian    return (TYPE_IP);
16328679Sbrian  }
1646059Samurai
16528679Sbrian  /*
16628679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
16728679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
16828679Sbrian   * connection state.  Special case the most recently used connection since
16928679Sbrian   * it's most likely to be used again & we don't have to do any reordering
17028679Sbrian   * if it's used.
17128679Sbrian   */
17236285Sbrian  slstat->sls_packets++;
17336285Sbrian  if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
17436285Sbrian      ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
17536285Sbrian      *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1766059Samurai
17728679Sbrian    /*
17828679Sbrian     * Wasn't the first -- search for it.
17928679Sbrian     *
18028679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
18128679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
18228679Sbrian     * the head of the list whenever it is referenced.  Since the list is
18328679Sbrian     * short and, empirically, the connection we want is almost always near
18428679Sbrian     * the front, we locate states via linear search.  If we don't find a
18528679Sbrian     * state for the datagram, the oldest state is (re-)used.
18628679Sbrian     */
18728679Sbrian    register struct cstate *lcs;
18828679Sbrian    register struct cstate *lastcs = comp->last_cs;
1896059Samurai
19028679Sbrian    do {
19128679Sbrian      lcs = cs;
19228679Sbrian      cs = cs->cs_next;
19336285Sbrian      slstat->sls_searches++;
19436285Sbrian      if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
19536285Sbrian	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
19636285Sbrian	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
19728679Sbrian	goto found;
19828679Sbrian    } while (cs != lastcs);
1996059Samurai
20028679Sbrian    /*
20128679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
20228679Sbrian     * that tells the other side what connection number we're using for this
20328679Sbrian     * conversation. Note that since the state list is circular, the oldest
20428679Sbrian     * state points to the newest and we only need to set last_cs to update
20528679Sbrian     * the lru linkage.
20628679Sbrian     */
20736285Sbrian    slstat->sls_misses++;
20828679Sbrian      comp->last_cs = lcs;
2096059Samurai#define	THOFFSET(th)	(th->th_off)
21028679Sbrian    hlen += th->th_off;
21128679Sbrian    hlen <<= 2;
21228679Sbrian    if (hlen > m->cnt)
21328679Sbrian      return (TYPE_IP);
21428679Sbrian    goto uncompressed;
2156059Samurai
21628679Sbrianfound:
2176059Samurai
21828679Sbrian    /*
21928679Sbrian     * Found it -- move to the front on the connection list.
22028679Sbrian     */
22128679Sbrian    if (cs == lastcs)
22228679Sbrian      comp->last_cs = lcs;
22328679Sbrian    else {
22428679Sbrian      lcs->cs_next = cs->cs_next;
22528679Sbrian      cs->cs_next = lastcs->cs_next;
22628679Sbrian      lastcs->cs_next = cs;
22728679Sbrian    }
22828679Sbrian  }
2296059Samurai
23028679Sbrian  /*
23128679Sbrian   * Make sure that only what we expect to change changed. The first line of
23228679Sbrian   * the `if' checks the IP protocol version, header length & type of
23328679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
23428679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
23528679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
23628679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
23728679Sbrian   * any of these things are different between the previous & current
23828679Sbrian   * datagram, we send the current datagram `uncompressed'.
23928679Sbrian   */
24028679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
24128679Sbrian  deltaS = hlen;
24228679Sbrian  hlen += th->th_off;
24328679Sbrian  hlen <<= 2;
24428679Sbrian  if (hlen > m->cnt)
24528679Sbrian    return (TYPE_IP);
2466059Samurai
24728679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
24828679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
24928679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
25028679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
25128679Sbrian      (deltaS > 5 &&
25230715Sbrian       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
25328679Sbrian      (THOFFSET(th) > 5 &&
25430715Sbrian       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
25528679Sbrian    goto uncompressed;
25628679Sbrian  }
2576059Samurai
25828679Sbrian  /*
25928679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
26028679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
26128679Sbrian   * number of temporaries needed in this section of code).
26228679Sbrian   */
26328679Sbrian  if (th->th_flags & TH_URG) {
26428679Sbrian    deltaS = ntohs(th->th_urp);
26528679Sbrian    ENCODEZ(deltaS);
26628679Sbrian    changes |= NEW_U;
26728679Sbrian  } else if (th->th_urp != oth->th_urp) {
2686059Samurai
26928679Sbrian    /*
27028679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
27128679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
27228679Sbrian     * deal with it.
27328679Sbrian     */
27428679Sbrian    goto uncompressed;
27528679Sbrian  }
27628679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
27728679Sbrian  if (deltaS) {
27828679Sbrian    ENCODE(deltaS);
27928679Sbrian    changes |= NEW_W;
28028679Sbrian  }
28128679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
28228679Sbrian  if (deltaA) {
28328679Sbrian    if (deltaA > 0xffff) {
28428679Sbrian      goto uncompressed;
28528679Sbrian    }
28628679Sbrian    ENCODE(deltaA);
28728679Sbrian    changes |= NEW_A;
28828679Sbrian  }
28928679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
29028679Sbrian  if (deltaS) {
29128679Sbrian    if (deltaS > 0xffff) {
29228679Sbrian      goto uncompressed;
29328679Sbrian    }
29428679Sbrian    ENCODE(deltaS);
29528679Sbrian    changes |= NEW_S;
29628679Sbrian  }
29728679Sbrian  switch (changes) {
2986059Samurai
29928679Sbrian  case 0:
3006059Samurai
30128679Sbrian    /*
30228679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
30328679Sbrian     * this is probably a data packet following an ack (normal on an
30428679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
30528679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
30628679Sbrian     * uncompressed in case the other side missed the compressed version.
30728679Sbrian     */
30828679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
30928679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
31028679Sbrian      break;
3116059Samurai
31228679Sbrian    /* (fall through) */
3136059Samurai
31428679Sbrian  case SPECIAL_I:
31528679Sbrian  case SPECIAL_D:
3166059Samurai
31728679Sbrian    /*
31828679Sbrian     * actual changes match one of our special case encodings -- send packet
31928679Sbrian     * uncompressed.
32028679Sbrian     */
32128679Sbrian    goto uncompressed;
3226059Samurai
32328679Sbrian  case NEW_S | NEW_A:
32428679Sbrian    if (deltaS == deltaA &&
32528679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
32628679Sbrian      /* special case for echoed terminal traffic */
32728679Sbrian      changes = SPECIAL_I;
32828679Sbrian      cp = new_seq;
32928679Sbrian    }
33028679Sbrian    break;
3316059Samurai
33228679Sbrian  case NEW_S:
33328679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33428679Sbrian      /* special case for data xfer */
33528679Sbrian      changes = SPECIAL_D;
33628679Sbrian      cp = new_seq;
33728679Sbrian    }
33828679Sbrian    break;
33928679Sbrian  }
3406059Samurai
34128679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
34228679Sbrian  if (deltaS != 1) {
34328679Sbrian    ENCODEZ(deltaS);
34428679Sbrian    changes |= NEW_I;
34528679Sbrian  }
34628679Sbrian  if (th->th_flags & TH_PUSH)
34728679Sbrian    changes |= TCP_PUSH_BIT;
3486059Samurai
34928679Sbrian  /*
35028679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
35128679Sbrian   * this packet's header.
35228679Sbrian   */
35328679Sbrian  deltaA = ntohs(th->th_sum);
35430715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
3556059Samurai
35628679Sbrian  /*
35728679Sbrian   * We want to use the original packet as our compressed packet. (cp -
35828679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
35928679Sbrian   * In addition we need one byte for the change mask, one for the connection
36028679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
36128679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
36228679Sbrian   * subtract the two to get the new packet size.
36328679Sbrian   */
36428679Sbrian  deltaS = cp - new_seq;
36528679Sbrian  cp = (u_char *) ip;
36628679Sbrian
36728679Sbrian  /*
36828679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
36928679Sbrian   * know what order packets will go on the line.  In this case, we always
37028679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
37128679Sbrian   */
37230187Sbrian  if (comp->last_xmit == cs->cs_id && compress_cid) {
37328679Sbrian    hlen -= deltaS + 3;
37428679Sbrian    cp += hlen;
37528679Sbrian    *cp++ = changes;
37630187Sbrian  } else {
37728679Sbrian    comp->last_xmit = cs->cs_id;
37828679Sbrian    hlen -= deltaS + 4;
37928679Sbrian    cp += hlen;
38028679Sbrian    *cp++ = changes | NEW_C;
38128679Sbrian    *cp++ = cs->cs_id;
38228679Sbrian  }
38328679Sbrian  m->cnt -= hlen;
38428679Sbrian  m->offset += hlen;
38528679Sbrian  *cp++ = deltaA >> 8;
38628679Sbrian  *cp++ = deltaA;
38730715Sbrian  memcpy(cp, new_seq, deltaS);
38836285Sbrian  slstat->sls_compressed++;
38936285Sbrian  return (TYPE_COMPRESSED_TCP);
3906059Samurai
39128679Sbrian  /*
39228679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
39328679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
39428679Sbrian   * use on future compressed packets in the protocol field).
39528679Sbrian   */
3966059Samuraiuncompressed:
39730715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
39828679Sbrian  ip->ip_p = cs->cs_id;
39928679Sbrian  comp->last_xmit = cs->cs_id;
40028679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
4016059Samurai}
4026059Samurai
4036059Samurai
4046059Samuraiint
40536960Sbriansl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
40636960Sbrian                  struct slstat *slstat, int max_state)
4076059Samurai{
40828679Sbrian  register u_char *cp;
40928679Sbrian  register u_int hlen, changes;
41028679Sbrian  register struct tcphdr *th;
41128679Sbrian  register struct cstate *cs;
41228679Sbrian  register struct ip *ip;
4136059Samurai
41428679Sbrian  switch (type) {
4156059Samurai
41628679Sbrian  case TYPE_UNCOMPRESSED_TCP:
41728679Sbrian    ip = (struct ip *) * bufp;
41836960Sbrian    if (ip->ip_p > max_state)
41928679Sbrian      goto bad;
42028679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
42128679Sbrian    comp->flags &= ~SLF_TOSS;
42228679Sbrian    ip->ip_p = IPPROTO_TCP;
4236059Samurai
42428679Sbrian    /*
42528679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
42628679Sbrian     * overflow the space we have available for it.
42728679Sbrian     */
42828679Sbrian    hlen = ip->ip_hl << 2;
42928679Sbrian    if (hlen + sizeof(struct tcphdr) > len)
43028679Sbrian      goto bad;
43128679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
43228679Sbrian    hlen += THOFFSET(th) << 2;
43328679Sbrian    if (hlen > MAX_HDR)
43428679Sbrian      goto bad;
43530715Sbrian    memcpy(&cs->cs_ip, ip, hlen);
43630357Sbrian    cs->cs_ip.ip_sum = 0;
43728679Sbrian    cs->cs_hlen = hlen;
43836285Sbrian    slstat->sls_uncompressedin++;
43936285Sbrian    return (len);
4406059Samurai
44128679Sbrian  default:
44228679Sbrian    goto bad;
4436059Samurai
44428679Sbrian  case TYPE_COMPRESSED_TCP:
44528679Sbrian    break;
44628679Sbrian  }
44728679Sbrian  /* We've got a compressed packet. */
44836285Sbrian  slstat->sls_compressedin++;
44936285Sbrian  cp = *bufp;
45028679Sbrian  changes = *cp++;
45136285Sbrian  log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
45228679Sbrian  if (changes & NEW_C) {
4536059Samurai
45428679Sbrian    /*
45528679Sbrian     * Make sure the state index is in range, then grab the state. If we have
45628679Sbrian     * a good state index, clear the 'discard' flag.
45728679Sbrian     */
45836960Sbrian    if (*cp > max_state || comp->last_recv == 255) {
45928679Sbrian      goto bad;
46036960Sbrian    }
4616059Samurai
46228679Sbrian    comp->flags &= ~SLF_TOSS;
46328679Sbrian    comp->last_recv = *cp++;
46428679Sbrian  } else {
4656059Samurai
46628679Sbrian    /*
46728679Sbrian     * this packet has an implicit state index.  If we've had a line error
46828679Sbrian     * since the last time we got an explicit state index, we have to toss
46928679Sbrian     * the packet.
47028679Sbrian     */
47128679Sbrian    if (comp->flags & SLF_TOSS) {
47236285Sbrian      slstat->sls_tossed++;
47336285Sbrian      return (0);
47428679Sbrian    }
47528679Sbrian  }
47628679Sbrian  cs = &comp->rstate[comp->last_recv];
47728679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
47836960Sbrian  if (hlen == 0)
47936960Sbrian    goto bad;    /* We've been pointed at a not-yet-used slot ! */
48028679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
48128679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
48228679Sbrian  cp += 2;
48328679Sbrian  if (changes & TCP_PUSH_BIT)
48428679Sbrian    th->th_flags |= TH_PUSH;
48528679Sbrian  else
48628679Sbrian    th->th_flags &= ~TH_PUSH;
48728679Sbrian
48828679Sbrian  switch (changes & SPECIALS_MASK) {
48928679Sbrian  case SPECIAL_I:
49028679Sbrian    {
49128679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
49228679Sbrian
49328679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
49428679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
49528679Sbrian    }
49628679Sbrian    break;
49728679Sbrian
49828679Sbrian  case SPECIAL_D:
49928679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
50028679Sbrian		       - cs->cs_hlen);
50128679Sbrian    break;
50228679Sbrian
50328679Sbrian  default:
50428679Sbrian    if (changes & NEW_U) {
50528679Sbrian      th->th_flags |= TH_URG;
50628679Sbrian      DECODEU(th->th_urp)
50728679Sbrian    } else
50828679Sbrian      th->th_flags &= ~TH_URG;
50928679Sbrian    if (changes & NEW_W)
51028679Sbrian      DECODES(th->th_win)
51128679Sbrian	if (changes & NEW_A)
51228679Sbrian	DECODEL(th->th_ack)
51328679Sbrian	  if (changes & NEW_S) {
51436285Sbrian	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
51528679Sbrian		    *cp, cp[1], cp[2]);
51628679Sbrian	  DECODEL(th->th_seq)
5176059Samurai	}
51828679Sbrian    break;
51928679Sbrian  }
52028679Sbrian  if (changes & NEW_I) {
52128679Sbrian    DECODES(cs->cs_ip.ip_id)
52228679Sbrian  } else
52328679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5246059Samurai
52536285Sbrian  log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
52636285Sbrian	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
52726516Sbrian
52828679Sbrian  /*
52928679Sbrian   * At this point, cp points to the first byte of data in the packet.  If
53028679Sbrian   * we're not aligned on a 4-byte boundary, copy the data down so the ip &
53128679Sbrian   * tcp headers will be aligned.  Then back up cp by the tcp/ip header
53228679Sbrian   * length to make room for the reconstructed header (we assume the packet
53328679Sbrian   * we were handed has enough space to prepend 128 bytes of header).  Adjust
53428679Sbrian   * the length to account for the new header & fill in the IP total length.
53528679Sbrian   */
53628679Sbrian  len -= (cp - *bufp);
53728679Sbrian  if (len < 0)
5386059Samurai
53928679Sbrian    /*
54028679Sbrian     * we must have dropped some characters (crc should detect this but the
54128679Sbrian     * old slip framing won't)
54228679Sbrian     */
54328679Sbrian    goto bad;
54428679Sbrian
5456059Samurai#ifdef notdef
54628679Sbrian  if ((int) cp & 3) {
54728679Sbrian    if (len > 0)
54830715Sbrian      (void) bcopy(cp, (caddr_t) ((int) cp & ~3), len);
54928679Sbrian    cp = (u_char *) ((int) cp & ~3);
55028679Sbrian  }
5516059Samurai#endif
5526059Samurai
55328679Sbrian  cp -= cs->cs_hlen;
55428679Sbrian  len += cs->cs_hlen;
55528679Sbrian  cs->cs_ip.ip_len = htons(len);
55630715Sbrian  memcpy(cp, &cs->cs_ip, cs->cs_hlen);
55728679Sbrian  *bufp = cp;
5586059Samurai
55928679Sbrian  /* recompute the ip header checksum */
56028679Sbrian  {
56128679Sbrian    register u_short *bp = (u_short *) cp;
56228679Sbrian
56328679Sbrian    for (changes = 0; hlen > 0; hlen -= 2)
56428679Sbrian      changes += *bp++;
56528679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
56628679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
56728679Sbrian    ((struct ip *) cp)->ip_sum = ~changes;
56828679Sbrian  }
56928679Sbrian  return (len);
5706059Samuraibad:
57128679Sbrian  comp->flags |= SLF_TOSS;
57236285Sbrian  slstat->sls_errorin++;
57336285Sbrian  return (0);
5746059Samurai}
5756059Samurai
5766059Samuraiint
57736285Sbriansl_Show(struct cmdargs const *arg)
5786059Samurai{
57936285Sbrian  prompt_Printf(arg->prompt, "VJ compression statistics:\n");
58036285Sbrian  prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
58136285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
58236285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
58336285Sbrian  prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
58436285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
58536285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
58636285Sbrian  prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
58736285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
58836285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
58936285Sbrian  prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
59036285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
59136285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
59226516Sbrian  return 0;
5936059Samurai}
594