slcompress.c revision 43313
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 *
2043313Sbrian * $Id: slcompress.c,v 1.20 1998/08/26 17:39:37 brian Exp $
218857Srgrimes *
226059Samurai *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
236059Samurai *	- Initial distribution.
246059Samurai */
2528679Sbrian
2643313Sbrian#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>
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"
4838557Sbrian#include "lqr.h"
4938557Sbrian#include "hdlc.h"
5036285Sbrian#include "ipcp.h"
5136285Sbrian#include "filter.h"
5236285Sbrian#include "lcp.h"
5336285Sbrian#include "ccp.h"
5436285Sbrian#include "link.h"
5536285Sbrian#include "mp.h"
5643313Sbrian#ifndef NORADIUS
5743313Sbrian#include "radius.h"
5843313Sbrian#endif
5936285Sbrian#include "bundle.h"
606059Samurai
616059Samuraivoid
6230187Sbriansl_compress_init(struct slcompress * comp, int max_state)
636059Samurai{
6428679Sbrian  register u_int i;
6528679Sbrian  register struct cstate *tstate = comp->tstate;
666059Samurai
6731962Sbrian  memset(comp, '\0', sizeof *comp);
6830187Sbrian  for (i = max_state; i > 0; --i) {
6928679Sbrian    tstate[i].cs_id = i;
7028679Sbrian    tstate[i].cs_next = &tstate[i - 1];
7128679Sbrian  }
7230187Sbrian  tstate[0].cs_next = &tstate[max_state];
7328679Sbrian  tstate[0].cs_id = 0;
7428679Sbrian  comp->last_cs = &tstate[0];
7528679Sbrian  comp->last_recv = 255;
7628679Sbrian  comp->last_xmit = 255;
7728679Sbrian  comp->flags = SLF_TOSS;
786059Samurai}
796059Samurai
806059Samurai
816059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
8237189Sbrian * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
836059Samurai * form).
846059Samurai */
856059Samurai#define ENCODE(n) { \
866059Samurai	if ((u_short)(n) >= 256) { \
876059Samurai		*cp++ = 0; \
886059Samurai		cp[1] = (n); \
896059Samurai		cp[0] = (n) >> 8; \
906059Samurai		cp += 2; \
916059Samurai	} else { \
926059Samurai		*cp++ = (n); \
936059Samurai	} \
946059Samurai}
956059Samurai#define ENCODEZ(n) { \
966059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
976059Samurai		*cp++ = 0; \
986059Samurai		cp[1] = (n); \
996059Samurai		cp[0] = (n) >> 8; \
1006059Samurai		cp += 2; \
1016059Samurai	} else { \
1026059Samurai		*cp++ = (n); \
1036059Samurai	} \
1046059Samurai}
1056059Samurai
1066059Samurai#define DECODEL(f) { \
1076059Samurai	if (*cp == 0) {\
1086059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
1096059Samurai		cp += 3; \
1106059Samurai	} else { \
11137189Sbrian		(f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
1126059Samurai	} \
1136059Samurai}
1146059Samurai
1156059Samurai#define DECODES(f) { \
1166059Samurai	if (*cp == 0) {\
1176059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1186059Samurai		cp += 3; \
1196059Samurai	} else { \
12037189Sbrian		(f) = htons(ntohs(f) + (u_int32_t)*cp++); \
1216059Samurai	} \
1226059Samurai}
1236059Samurai
1246059Samurai#define DECODEU(f) { \
1256059Samurai	if (*cp == 0) {\
1266059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1276059Samurai		cp += 3; \
1286059Samurai	} else { \
12937189Sbrian		(f) = htons((u_int32_t)*cp++); \
1306059Samurai	} \
1316059Samurai}
1326059Samurai
1336059Samurai
1346059Samuraiu_char
13528679Sbriansl_compress_tcp(struct mbuf * m,
13628679Sbrian		struct ip * ip,
13736285Sbrian		struct slcompress *comp,
13836285Sbrian                struct slstat *slstat,
13928679Sbrian		int compress_cid)
1406059Samurai{
14128679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
14228679Sbrian  register u_int hlen = ip->ip_hl;
14328679Sbrian  register struct tcphdr *oth;
14428679Sbrian  register struct tcphdr *th;
14528679Sbrian  register u_int deltaS, deltaA;
14628679Sbrian  register u_int changes = 0;
14728679Sbrian  u_char new_seq[16];
14828679Sbrian  register u_char *cp = new_seq;
1496059Samurai
15028679Sbrian  /*
15128679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
15228679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
15328679Sbrian   * the caller has already made sure the packet is IP proto TCP).
15428679Sbrian   */
15528679Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) {
15636285Sbrian    log_Printf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n",
15728679Sbrian	      ip->ip_off, m->cnt);
15836285Sbrian    log_DumpBp(LogDEBUG, "", m);
15928679Sbrian    return (TYPE_IP);
16028679Sbrian  }
16128679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
16228679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
16336285Sbrian    log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
16436285Sbrian    log_DumpBp(LogDEBUG, "", m);
16528679Sbrian    return (TYPE_IP);
16628679Sbrian  }
1676059Samurai
16828679Sbrian  /*
16928679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
17028679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
17128679Sbrian   * connection state.  Special case the most recently used connection since
17228679Sbrian   * it's most likely to be used again & we don't have to do any reordering
17328679Sbrian   * if it's used.
17428679Sbrian   */
17536285Sbrian  slstat->sls_packets++;
17636285Sbrian  if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
17736285Sbrian      ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
17836285Sbrian      *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1796059Samurai
18028679Sbrian    /*
18128679Sbrian     * Wasn't the first -- search for it.
18228679Sbrian     *
18328679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
18428679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
18528679Sbrian     * the head of the list whenever it is referenced.  Since the list is
18628679Sbrian     * short and, empirically, the connection we want is almost always near
18728679Sbrian     * the front, we locate states via linear search.  If we don't find a
18828679Sbrian     * state for the datagram, the oldest state is (re-)used.
18928679Sbrian     */
19028679Sbrian    register struct cstate *lcs;
19128679Sbrian    register struct cstate *lastcs = comp->last_cs;
1926059Samurai
19328679Sbrian    do {
19428679Sbrian      lcs = cs;
19528679Sbrian      cs = cs->cs_next;
19636285Sbrian      slstat->sls_searches++;
19736285Sbrian      if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
19836285Sbrian	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
19936285Sbrian	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
20028679Sbrian	goto found;
20128679Sbrian    } while (cs != lastcs);
2026059Samurai
20328679Sbrian    /*
20428679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
20528679Sbrian     * that tells the other side what connection number we're using for this
20628679Sbrian     * conversation. Note that since the state list is circular, the oldest
20728679Sbrian     * state points to the newest and we only need to set last_cs to update
20828679Sbrian     * the lru linkage.
20928679Sbrian     */
21036285Sbrian    slstat->sls_misses++;
21128679Sbrian      comp->last_cs = lcs;
2126059Samurai#define	THOFFSET(th)	(th->th_off)
21328679Sbrian    hlen += th->th_off;
21428679Sbrian    hlen <<= 2;
21528679Sbrian    if (hlen > m->cnt)
21628679Sbrian      return (TYPE_IP);
21728679Sbrian    goto uncompressed;
2186059Samurai
21928679Sbrianfound:
2206059Samurai
22128679Sbrian    /*
22228679Sbrian     * Found it -- move to the front on the connection list.
22328679Sbrian     */
22428679Sbrian    if (cs == lastcs)
22528679Sbrian      comp->last_cs = lcs;
22628679Sbrian    else {
22728679Sbrian      lcs->cs_next = cs->cs_next;
22828679Sbrian      cs->cs_next = lastcs->cs_next;
22928679Sbrian      lastcs->cs_next = cs;
23028679Sbrian    }
23128679Sbrian  }
2326059Samurai
23328679Sbrian  /*
23428679Sbrian   * Make sure that only what we expect to change changed. The first line of
23528679Sbrian   * the `if' checks the IP protocol version, header length & type of
23628679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
23728679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
23828679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
23928679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
24028679Sbrian   * any of these things are different between the previous & current
24128679Sbrian   * datagram, we send the current datagram `uncompressed'.
24228679Sbrian   */
24328679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
24428679Sbrian  deltaS = hlen;
24528679Sbrian  hlen += th->th_off;
24628679Sbrian  hlen <<= 2;
24728679Sbrian  if (hlen > m->cnt)
24828679Sbrian    return (TYPE_IP);
2496059Samurai
25028679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
25128679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
25228679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
25328679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
25428679Sbrian      (deltaS > 5 &&
25530715Sbrian       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
25628679Sbrian      (THOFFSET(th) > 5 &&
25730715Sbrian       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
25828679Sbrian    goto uncompressed;
25928679Sbrian  }
2606059Samurai
26128679Sbrian  /*
26228679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
26328679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
26428679Sbrian   * number of temporaries needed in this section of code).
26528679Sbrian   */
26628679Sbrian  if (th->th_flags & TH_URG) {
26728679Sbrian    deltaS = ntohs(th->th_urp);
26828679Sbrian    ENCODEZ(deltaS);
26928679Sbrian    changes |= NEW_U;
27028679Sbrian  } else if (th->th_urp != oth->th_urp) {
2716059Samurai
27228679Sbrian    /*
27328679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
27428679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
27528679Sbrian     * deal with it.
27628679Sbrian     */
27728679Sbrian    goto uncompressed;
27828679Sbrian  }
27928679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
28028679Sbrian  if (deltaS) {
28128679Sbrian    ENCODE(deltaS);
28228679Sbrian    changes |= NEW_W;
28328679Sbrian  }
28428679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
28528679Sbrian  if (deltaA) {
28628679Sbrian    if (deltaA > 0xffff) {
28728679Sbrian      goto uncompressed;
28828679Sbrian    }
28928679Sbrian    ENCODE(deltaA);
29028679Sbrian    changes |= NEW_A;
29128679Sbrian  }
29228679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
29328679Sbrian  if (deltaS) {
29428679Sbrian    if (deltaS > 0xffff) {
29528679Sbrian      goto uncompressed;
29628679Sbrian    }
29728679Sbrian    ENCODE(deltaS);
29828679Sbrian    changes |= NEW_S;
29928679Sbrian  }
30028679Sbrian  switch (changes) {
3016059Samurai
30228679Sbrian  case 0:
3036059Samurai
30428679Sbrian    /*
30528679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
30628679Sbrian     * this is probably a data packet following an ack (normal on an
30728679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
30828679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
30928679Sbrian     * uncompressed in case the other side missed the compressed version.
31028679Sbrian     */
31128679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
31228679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
31328679Sbrian      break;
3146059Samurai
31528679Sbrian    /* (fall through) */
3166059Samurai
31728679Sbrian  case SPECIAL_I:
31828679Sbrian  case SPECIAL_D:
3196059Samurai
32028679Sbrian    /*
32128679Sbrian     * actual changes match one of our special case encodings -- send packet
32228679Sbrian     * uncompressed.
32328679Sbrian     */
32428679Sbrian    goto uncompressed;
3256059Samurai
32628679Sbrian  case NEW_S | NEW_A:
32728679Sbrian    if (deltaS == deltaA &&
32828679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
32928679Sbrian      /* special case for echoed terminal traffic */
33028679Sbrian      changes = SPECIAL_I;
33128679Sbrian      cp = new_seq;
33228679Sbrian    }
33328679Sbrian    break;
3346059Samurai
33528679Sbrian  case NEW_S:
33628679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33728679Sbrian      /* special case for data xfer */
33828679Sbrian      changes = SPECIAL_D;
33928679Sbrian      cp = new_seq;
34028679Sbrian    }
34128679Sbrian    break;
34228679Sbrian  }
3436059Samurai
34428679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
34528679Sbrian  if (deltaS != 1) {
34628679Sbrian    ENCODEZ(deltaS);
34728679Sbrian    changes |= NEW_I;
34828679Sbrian  }
34928679Sbrian  if (th->th_flags & TH_PUSH)
35028679Sbrian    changes |= TCP_PUSH_BIT;
3516059Samurai
35228679Sbrian  /*
35328679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
35428679Sbrian   * this packet's header.
35528679Sbrian   */
35628679Sbrian  deltaA = ntohs(th->th_sum);
35730715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
3586059Samurai
35928679Sbrian  /*
36028679Sbrian   * We want to use the original packet as our compressed packet. (cp -
36128679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
36228679Sbrian   * In addition we need one byte for the change mask, one for the connection
36328679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
36428679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
36528679Sbrian   * subtract the two to get the new packet size.
36628679Sbrian   */
36728679Sbrian  deltaS = cp - new_seq;
36828679Sbrian  cp = (u_char *) ip;
36928679Sbrian
37028679Sbrian  /*
37128679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
37228679Sbrian   * know what order packets will go on the line.  In this case, we always
37328679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
37428679Sbrian   */
37530187Sbrian  if (comp->last_xmit == cs->cs_id && compress_cid) {
37628679Sbrian    hlen -= deltaS + 3;
37728679Sbrian    cp += hlen;
37828679Sbrian    *cp++ = changes;
37930187Sbrian  } else {
38028679Sbrian    comp->last_xmit = cs->cs_id;
38128679Sbrian    hlen -= deltaS + 4;
38228679Sbrian    cp += hlen;
38328679Sbrian    *cp++ = changes | NEW_C;
38428679Sbrian    *cp++ = cs->cs_id;
38528679Sbrian  }
38628679Sbrian  m->cnt -= hlen;
38728679Sbrian  m->offset += hlen;
38828679Sbrian  *cp++ = deltaA >> 8;
38928679Sbrian  *cp++ = deltaA;
39030715Sbrian  memcpy(cp, new_seq, deltaS);
39136285Sbrian  slstat->sls_compressed++;
39236285Sbrian  return (TYPE_COMPRESSED_TCP);
3936059Samurai
39428679Sbrian  /*
39528679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
39628679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
39728679Sbrian   * use on future compressed packets in the protocol field).
39828679Sbrian   */
3996059Samuraiuncompressed:
40030715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
40128679Sbrian  ip->ip_p = cs->cs_id;
40228679Sbrian  comp->last_xmit = cs->cs_id;
40328679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
4046059Samurai}
4056059Samurai
4066059Samurai
4076059Samuraiint
40836960Sbriansl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
40936960Sbrian                  struct slstat *slstat, int max_state)
4106059Samurai{
41128679Sbrian  register u_char *cp;
41228679Sbrian  register u_int hlen, changes;
41328679Sbrian  register struct tcphdr *th;
41428679Sbrian  register struct cstate *cs;
41528679Sbrian  register struct ip *ip;
4166059Samurai
41728679Sbrian  switch (type) {
4186059Samurai
41928679Sbrian  case TYPE_UNCOMPRESSED_TCP:
42028679Sbrian    ip = (struct ip *) * bufp;
42136960Sbrian    if (ip->ip_p > max_state)
42228679Sbrian      goto bad;
42328679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
42428679Sbrian    comp->flags &= ~SLF_TOSS;
42528679Sbrian    ip->ip_p = IPPROTO_TCP;
4266059Samurai
42728679Sbrian    /*
42828679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
42928679Sbrian     * overflow the space we have available for it.
43028679Sbrian     */
43128679Sbrian    hlen = ip->ip_hl << 2;
43228679Sbrian    if (hlen + sizeof(struct tcphdr) > len)
43328679Sbrian      goto bad;
43428679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
43528679Sbrian    hlen += THOFFSET(th) << 2;
43628679Sbrian    if (hlen > MAX_HDR)
43728679Sbrian      goto bad;
43830715Sbrian    memcpy(&cs->cs_ip, ip, hlen);
43930357Sbrian    cs->cs_ip.ip_sum = 0;
44028679Sbrian    cs->cs_hlen = hlen;
44136285Sbrian    slstat->sls_uncompressedin++;
44236285Sbrian    return (len);
4436059Samurai
44428679Sbrian  default:
44528679Sbrian    goto bad;
4466059Samurai
44728679Sbrian  case TYPE_COMPRESSED_TCP:
44828679Sbrian    break;
44928679Sbrian  }
45028679Sbrian  /* We've got a compressed packet. */
45136285Sbrian  slstat->sls_compressedin++;
45236285Sbrian  cp = *bufp;
45328679Sbrian  changes = *cp++;
45436285Sbrian  log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
45528679Sbrian  if (changes & NEW_C) {
4566059Samurai
45728679Sbrian    /*
45828679Sbrian     * Make sure the state index is in range, then grab the state. If we have
45928679Sbrian     * a good state index, clear the 'discard' flag.
46028679Sbrian     */
46136960Sbrian    if (*cp > max_state || comp->last_recv == 255) {
46228679Sbrian      goto bad;
46336960Sbrian    }
4646059Samurai
46528679Sbrian    comp->flags &= ~SLF_TOSS;
46628679Sbrian    comp->last_recv = *cp++;
46728679Sbrian  } else {
4686059Samurai
46928679Sbrian    /*
47028679Sbrian     * this packet has an implicit state index.  If we've had a line error
47128679Sbrian     * since the last time we got an explicit state index, we have to toss
47228679Sbrian     * the packet.
47328679Sbrian     */
47428679Sbrian    if (comp->flags & SLF_TOSS) {
47536285Sbrian      slstat->sls_tossed++;
47636285Sbrian      return (0);
47728679Sbrian    }
47828679Sbrian  }
47928679Sbrian  cs = &comp->rstate[comp->last_recv];
48028679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
48136960Sbrian  if (hlen == 0)
48236960Sbrian    goto bad;    /* We've been pointed at a not-yet-used slot ! */
48328679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
48428679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
48528679Sbrian  cp += 2;
48628679Sbrian  if (changes & TCP_PUSH_BIT)
48728679Sbrian    th->th_flags |= TH_PUSH;
48828679Sbrian  else
48928679Sbrian    th->th_flags &= ~TH_PUSH;
49028679Sbrian
49128679Sbrian  switch (changes & SPECIALS_MASK) {
49228679Sbrian  case SPECIAL_I:
49328679Sbrian    {
49428679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
49528679Sbrian
49628679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
49728679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
49828679Sbrian    }
49928679Sbrian    break;
50028679Sbrian
50128679Sbrian  case SPECIAL_D:
50228679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
50328679Sbrian		       - cs->cs_hlen);
50428679Sbrian    break;
50528679Sbrian
50628679Sbrian  default:
50728679Sbrian    if (changes & NEW_U) {
50828679Sbrian      th->th_flags |= TH_URG;
50928679Sbrian      DECODEU(th->th_urp)
51028679Sbrian    } else
51128679Sbrian      th->th_flags &= ~TH_URG;
51228679Sbrian    if (changes & NEW_W)
51328679Sbrian      DECODES(th->th_win)
51428679Sbrian	if (changes & NEW_A)
51528679Sbrian	DECODEL(th->th_ack)
51628679Sbrian	  if (changes & NEW_S) {
51736285Sbrian	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
51828679Sbrian		    *cp, cp[1], cp[2]);
51928679Sbrian	  DECODEL(th->th_seq)
5206059Samurai	}
52128679Sbrian    break;
52228679Sbrian  }
52328679Sbrian  if (changes & NEW_I) {
52428679Sbrian    DECODES(cs->cs_ip.ip_id)
52528679Sbrian  } else
52628679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5276059Samurai
52836285Sbrian  log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
52936285Sbrian	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
53026516Sbrian
53128679Sbrian  /*
53228679Sbrian   * At this point, cp points to the first byte of data in the packet.  If
53328679Sbrian   * we're not aligned on a 4-byte boundary, copy the data down so the ip &
53428679Sbrian   * tcp headers will be aligned.  Then back up cp by the tcp/ip header
53528679Sbrian   * length to make room for the reconstructed header (we assume the packet
53628679Sbrian   * we were handed has enough space to prepend 128 bytes of header).  Adjust
53728679Sbrian   * the length to account for the new header & fill in the IP total length.
53828679Sbrian   */
53928679Sbrian  len -= (cp - *bufp);
54028679Sbrian  if (len < 0)
5416059Samurai
54228679Sbrian    /*
54328679Sbrian     * we must have dropped some characters (crc should detect this but the
54428679Sbrian     * old slip framing won't)
54528679Sbrian     */
54628679Sbrian    goto bad;
54728679Sbrian
5486059Samurai#ifdef notdef
54928679Sbrian  if ((int) cp & 3) {
55028679Sbrian    if (len > 0)
55130715Sbrian      (void) bcopy(cp, (caddr_t) ((int) cp & ~3), len);
55228679Sbrian    cp = (u_char *) ((int) cp & ~3);
55328679Sbrian  }
5546059Samurai#endif
5556059Samurai
55628679Sbrian  cp -= cs->cs_hlen;
55728679Sbrian  len += cs->cs_hlen;
55828679Sbrian  cs->cs_ip.ip_len = htons(len);
55930715Sbrian  memcpy(cp, &cs->cs_ip, cs->cs_hlen);
56028679Sbrian  *bufp = cp;
5616059Samurai
56228679Sbrian  /* recompute the ip header checksum */
56328679Sbrian  {
56428679Sbrian    register u_short *bp = (u_short *) cp;
56528679Sbrian
56628679Sbrian    for (changes = 0; hlen > 0; hlen -= 2)
56728679Sbrian      changes += *bp++;
56828679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
56928679Sbrian    changes = (changes & 0xffff) + (changes >> 16);
57028679Sbrian    ((struct ip *) cp)->ip_sum = ~changes;
57128679Sbrian  }
57228679Sbrian  return (len);
5736059Samuraibad:
57428679Sbrian  comp->flags |= SLF_TOSS;
57536285Sbrian  slstat->sls_errorin++;
57636285Sbrian  return (0);
5776059Samurai}
5786059Samurai
5796059Samuraiint
58036285Sbriansl_Show(struct cmdargs const *arg)
5816059Samurai{
58236285Sbrian  prompt_Printf(arg->prompt, "VJ compression statistics:\n");
58336285Sbrian  prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
58436285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
58536285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
58636285Sbrian  prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
58736285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
58836285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
58936285Sbrian  prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
59036285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
59136285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
59236285Sbrian  prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
59336285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
59436285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
59526516Sbrian  return 0;
5966059Samurai}
597