slcompress.c revision 98243
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 *
2050479Speter * $FreeBSD: head/usr.sbin/ppp/slcompress.c 98243 2002-06-15 08:03:30Z brian $
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>
3181634Sbrian#include <sys/socket.h>
3236285Sbrian#include <sys/un.h>
3330715Sbrian
3430715Sbrian#include <stdio.h>
3530715Sbrian#include <string.h>
3636285Sbrian#include <termios.h>
3730715Sbrian
3846686Sbrian#include "layer.h"
3937009Sbrian#include "defs.h"
4031343Sbrian#include "command.h"
4130715Sbrian#include "mbuf.h"
4230715Sbrian#include "log.h"
436059Samurai#include "slcompress.h"
4436285Sbrian#include "descriptor.h"
4536285Sbrian#include "prompt.h"
4636285Sbrian#include "timer.h"
4736285Sbrian#include "fsm.h"
4836285Sbrian#include "throughput.h"
4936285Sbrian#include "iplist.h"
5038557Sbrian#include "lqr.h"
5138557Sbrian#include "hdlc.h"
5281634Sbrian#include "ncpaddr.h"
5336285Sbrian#include "ipcp.h"
5436285Sbrian#include "filter.h"
5536285Sbrian#include "lcp.h"
5636285Sbrian#include "ccp.h"
5736285Sbrian#include "link.h"
5836285Sbrian#include "mp.h"
5943313Sbrian#ifndef NORADIUS
6043313Sbrian#include "radius.h"
6143313Sbrian#endif
6281634Sbrian#include "ipv6cp.h"
6381634Sbrian#include "ncp.h"
6436285Sbrian#include "bundle.h"
656059Samurai
666059Samuraivoid
6746828Sbriansl_compress_init(struct slcompress *comp, int max_state)
686059Samurai{
6928679Sbrian  register u_int i;
7028679Sbrian  register struct cstate *tstate = comp->tstate;
716059Samurai
7231962Sbrian  memset(comp, '\0', sizeof *comp);
7330187Sbrian  for (i = max_state; i > 0; --i) {
7428679Sbrian    tstate[i].cs_id = i;
7528679Sbrian    tstate[i].cs_next = &tstate[i - 1];
7628679Sbrian  }
7730187Sbrian  tstate[0].cs_next = &tstate[max_state];
7828679Sbrian  tstate[0].cs_id = 0;
7928679Sbrian  comp->last_cs = &tstate[0];
8028679Sbrian  comp->last_recv = 255;
8128679Sbrian  comp->last_xmit = 255;
8228679Sbrian  comp->flags = SLF_TOSS;
836059Samurai}
846059Samurai
856059Samurai
866059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
8737189Sbrian * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
886059Samurai * form).
896059Samurai */
906059Samurai#define ENCODE(n) { \
916059Samurai	if ((u_short)(n) >= 256) { \
926059Samurai		*cp++ = 0; \
936059Samurai		cp[1] = (n); \
946059Samurai		cp[0] = (n) >> 8; \
956059Samurai		cp += 2; \
966059Samurai	} else { \
976059Samurai		*cp++ = (n); \
986059Samurai	} \
996059Samurai}
1006059Samurai#define ENCODEZ(n) { \
1016059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
1026059Samurai		*cp++ = 0; \
1036059Samurai		cp[1] = (n); \
1046059Samurai		cp[0] = (n) >> 8; \
1056059Samurai		cp += 2; \
1066059Samurai	} else { \
1076059Samurai		*cp++ = (n); \
1086059Samurai	} \
1096059Samurai}
1106059Samurai
1116059Samurai#define DECODEL(f) { \
1126059Samurai	if (*cp == 0) {\
1136059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
1146059Samurai		cp += 3; \
1156059Samurai	} else { \
11637189Sbrian		(f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
1176059Samurai	} \
1186059Samurai}
1196059Samurai
1206059Samurai#define DECODES(f) { \
1216059Samurai	if (*cp == 0) {\
1226059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1236059Samurai		cp += 3; \
1246059Samurai	} else { \
12537189Sbrian		(f) = htons(ntohs(f) + (u_int32_t)*cp++); \
1266059Samurai	} \
1276059Samurai}
1286059Samurai
1296059Samurai#define DECODEU(f) { \
1306059Samurai	if (*cp == 0) {\
1316059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1326059Samurai		cp += 3; \
1336059Samurai	} else { \
13437189Sbrian		(f) = htons((u_int32_t)*cp++); \
1356059Samurai	} \
1366059Samurai}
1376059Samurai
1386059Samurai
1396059Samuraiu_char
14028679Sbriansl_compress_tcp(struct mbuf * m,
14128679Sbrian		struct ip * ip,
14236285Sbrian		struct slcompress *comp,
14336285Sbrian                struct slstat *slstat,
14428679Sbrian		int compress_cid)
1456059Samurai{
14628679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
14728679Sbrian  register u_int hlen = ip->ip_hl;
14828679Sbrian  register struct tcphdr *oth;
14928679Sbrian  register struct tcphdr *th;
15028679Sbrian  register u_int deltaS, deltaA;
15128679Sbrian  register u_int changes = 0;
15228679Sbrian  u_char new_seq[16];
15328679Sbrian  register u_char *cp = new_seq;
1546059Samurai
15528679Sbrian  /*
15628679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
15728679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
15828679Sbrian   * the caller has already made sure the packet is IP proto TCP).
15928679Sbrian   */
16054912Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
16158042Sbrian    log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
16258042Sbrian	      ip->ip_off, (unsigned long)m->m_len);
16336285Sbrian    log_DumpBp(LogDEBUG, "", m);
16428679Sbrian    return (TYPE_IP);
16528679Sbrian  }
16628679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
16728679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
16836285Sbrian    log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
16936285Sbrian    log_DumpBp(LogDEBUG, "", m);
17028679Sbrian    return (TYPE_IP);
17128679Sbrian  }
1726059Samurai
17328679Sbrian  /*
17428679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
17528679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
17628679Sbrian   * connection state.  Special case the most recently used connection since
17728679Sbrian   * it's most likely to be used again & we don't have to do any reordering
17828679Sbrian   * if it's used.
17928679Sbrian   */
18036285Sbrian  slstat->sls_packets++;
18136285Sbrian  if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
18236285Sbrian      ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
18336285Sbrian      *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1846059Samurai
18528679Sbrian    /*
18628679Sbrian     * Wasn't the first -- search for it.
18798243Sbrian     *
18828679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
18928679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
19028679Sbrian     * the head of the list whenever it is referenced.  Since the list is
19128679Sbrian     * short and, empirically, the connection we want is almost always near
19228679Sbrian     * the front, we locate states via linear search.  If we don't find a
19328679Sbrian     * state for the datagram, the oldest state is (re-)used.
19428679Sbrian     */
19528679Sbrian    register struct cstate *lcs;
19628679Sbrian    register struct cstate *lastcs = comp->last_cs;
1976059Samurai
19828679Sbrian    do {
19928679Sbrian      lcs = cs;
20028679Sbrian      cs = cs->cs_next;
20136285Sbrian      slstat->sls_searches++;
20236285Sbrian      if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
20336285Sbrian	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
20436285Sbrian	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
20528679Sbrian	goto found;
20628679Sbrian    } while (cs != lastcs);
2076059Samurai
20828679Sbrian    /*
20928679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
21028679Sbrian     * that tells the other side what connection number we're using for this
21128679Sbrian     * conversation. Note that since the state list is circular, the oldest
21228679Sbrian     * state points to the newest and we only need to set last_cs to update
21328679Sbrian     * the lru linkage.
21428679Sbrian     */
21536285Sbrian    slstat->sls_misses++;
21628679Sbrian      comp->last_cs = lcs;
2176059Samurai#define	THOFFSET(th)	(th->th_off)
21828679Sbrian    hlen += th->th_off;
21928679Sbrian    hlen <<= 2;
22054912Sbrian    if (hlen > m->m_len)
22128679Sbrian      return (TYPE_IP);
22228679Sbrian    goto uncompressed;
2236059Samurai
22428679Sbrianfound:
2256059Samurai
22628679Sbrian    /*
22728679Sbrian     * Found it -- move to the front on the connection list.
22828679Sbrian     */
22928679Sbrian    if (cs == lastcs)
23028679Sbrian      comp->last_cs = lcs;
23128679Sbrian    else {
23228679Sbrian      lcs->cs_next = cs->cs_next;
23328679Sbrian      cs->cs_next = lastcs->cs_next;
23428679Sbrian      lastcs->cs_next = cs;
23528679Sbrian    }
23628679Sbrian  }
2376059Samurai
23828679Sbrian  /*
23928679Sbrian   * Make sure that only what we expect to change changed. The first line of
24028679Sbrian   * the `if' checks the IP protocol version, header length & type of
24128679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
24228679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
24328679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
24428679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
24528679Sbrian   * any of these things are different between the previous & current
24628679Sbrian   * datagram, we send the current datagram `uncompressed'.
24728679Sbrian   */
24828679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
24928679Sbrian  deltaS = hlen;
25028679Sbrian  hlen += th->th_off;
25128679Sbrian  hlen <<= 2;
25254912Sbrian  if (hlen > m->m_len)
25328679Sbrian    return (TYPE_IP);
2546059Samurai
25528679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
25628679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
25728679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
25828679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
25928679Sbrian      (deltaS > 5 &&
26030715Sbrian       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
26128679Sbrian      (THOFFSET(th) > 5 &&
26230715Sbrian       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
26328679Sbrian    goto uncompressed;
26428679Sbrian  }
2656059Samurai
26628679Sbrian  /*
26728679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
26828679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
26928679Sbrian   * number of temporaries needed in this section of code).
27028679Sbrian   */
27128679Sbrian  if (th->th_flags & TH_URG) {
27228679Sbrian    deltaS = ntohs(th->th_urp);
27328679Sbrian    ENCODEZ(deltaS);
27428679Sbrian    changes |= NEW_U;
27528679Sbrian  } else if (th->th_urp != oth->th_urp) {
2766059Samurai
27728679Sbrian    /*
27828679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
27928679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
28028679Sbrian     * deal with it.
28128679Sbrian     */
28228679Sbrian    goto uncompressed;
28328679Sbrian  }
28428679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
28528679Sbrian  if (deltaS) {
28628679Sbrian    ENCODE(deltaS);
28728679Sbrian    changes |= NEW_W;
28828679Sbrian  }
28928679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
29028679Sbrian  if (deltaA) {
29128679Sbrian    if (deltaA > 0xffff) {
29228679Sbrian      goto uncompressed;
29328679Sbrian    }
29428679Sbrian    ENCODE(deltaA);
29528679Sbrian    changes |= NEW_A;
29628679Sbrian  }
29728679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
29828679Sbrian  if (deltaS) {
29928679Sbrian    if (deltaS > 0xffff) {
30028679Sbrian      goto uncompressed;
30128679Sbrian    }
30228679Sbrian    ENCODE(deltaS);
30328679Sbrian    changes |= NEW_S;
30428679Sbrian  }
30528679Sbrian  switch (changes) {
3066059Samurai
30728679Sbrian  case 0:
3086059Samurai
30928679Sbrian    /*
31028679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
31128679Sbrian     * this is probably a data packet following an ack (normal on an
31228679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
31328679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
31428679Sbrian     * uncompressed in case the other side missed the compressed version.
31528679Sbrian     */
31628679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
31728679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
31828679Sbrian      break;
3196059Samurai
32028679Sbrian    /* (fall through) */
3216059Samurai
32228679Sbrian  case SPECIAL_I:
32328679Sbrian  case SPECIAL_D:
3246059Samurai
32528679Sbrian    /*
32628679Sbrian     * actual changes match one of our special case encodings -- send packet
32728679Sbrian     * uncompressed.
32828679Sbrian     */
32928679Sbrian    goto uncompressed;
3306059Samurai
33128679Sbrian  case NEW_S | NEW_A:
33228679Sbrian    if (deltaS == deltaA &&
33328679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33428679Sbrian      /* special case for echoed terminal traffic */
33528679Sbrian      changes = SPECIAL_I;
33628679Sbrian      cp = new_seq;
33728679Sbrian    }
33828679Sbrian    break;
3396059Samurai
34028679Sbrian  case NEW_S:
34128679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
34228679Sbrian      /* special case for data xfer */
34328679Sbrian      changes = SPECIAL_D;
34428679Sbrian      cp = new_seq;
34528679Sbrian    }
34628679Sbrian    break;
34728679Sbrian  }
3486059Samurai
34928679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
35028679Sbrian  if (deltaS != 1) {
35128679Sbrian    ENCODEZ(deltaS);
35228679Sbrian    changes |= NEW_I;
35328679Sbrian  }
35428679Sbrian  if (th->th_flags & TH_PUSH)
35528679Sbrian    changes |= TCP_PUSH_BIT;
3566059Samurai
35728679Sbrian  /*
35828679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
35928679Sbrian   * this packet's header.
36028679Sbrian   */
36128679Sbrian  deltaA = ntohs(th->th_sum);
36230715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
3636059Samurai
36428679Sbrian  /*
36528679Sbrian   * We want to use the original packet as our compressed packet. (cp -
36628679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
36728679Sbrian   * In addition we need one byte for the change mask, one for the connection
36828679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
36928679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
37028679Sbrian   * subtract the two to get the new packet size.
37128679Sbrian   */
37228679Sbrian  deltaS = cp - new_seq;
37328679Sbrian  cp = (u_char *) ip;
37428679Sbrian
37528679Sbrian  /*
37628679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
37728679Sbrian   * know what order packets will go on the line.  In this case, we always
37828679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
37928679Sbrian   */
38030187Sbrian  if (comp->last_xmit == cs->cs_id && compress_cid) {
38128679Sbrian    hlen -= deltaS + 3;
38228679Sbrian    cp += hlen;
38328679Sbrian    *cp++ = changes;
38430187Sbrian  } else {
38528679Sbrian    comp->last_xmit = cs->cs_id;
38628679Sbrian    hlen -= deltaS + 4;
38728679Sbrian    cp += hlen;
38828679Sbrian    *cp++ = changes | NEW_C;
38928679Sbrian    *cp++ = cs->cs_id;
39028679Sbrian  }
39154912Sbrian  m->m_len -= hlen;
39254912Sbrian  m->m_offset += hlen;
39328679Sbrian  *cp++ = deltaA >> 8;
39428679Sbrian  *cp++ = deltaA;
39530715Sbrian  memcpy(cp, new_seq, deltaS);
39636285Sbrian  slstat->sls_compressed++;
39736285Sbrian  return (TYPE_COMPRESSED_TCP);
3986059Samurai
39928679Sbrian  /*
40028679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
40128679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
40228679Sbrian   * use on future compressed packets in the protocol field).
40328679Sbrian   */
4046059Samuraiuncompressed:
40530715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
40628679Sbrian  ip->ip_p = cs->cs_id;
40728679Sbrian  comp->last_xmit = cs->cs_id;
40828679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
4096059Samurai}
4106059Samurai
4116059Samurai
4126059Samuraiint
41336960Sbriansl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
41436960Sbrian                  struct slstat *slstat, int max_state)
4156059Samurai{
41628679Sbrian  register u_char *cp;
41728679Sbrian  register u_int hlen, changes;
41828679Sbrian  register struct tcphdr *th;
41928679Sbrian  register struct cstate *cs;
42028679Sbrian  register struct ip *ip;
42152197Sbrian  u_short *bp;
4226059Samurai
42328679Sbrian  switch (type) {
4246059Samurai
42528679Sbrian  case TYPE_UNCOMPRESSED_TCP:
42628679Sbrian    ip = (struct ip *) * bufp;
42736960Sbrian    if (ip->ip_p > max_state)
42828679Sbrian      goto bad;
42928679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
43028679Sbrian    comp->flags &= ~SLF_TOSS;
43128679Sbrian    ip->ip_p = IPPROTO_TCP;
4326059Samurai
43328679Sbrian    /*
43428679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
43528679Sbrian     * overflow the space we have available for it.
43628679Sbrian     */
43728679Sbrian    hlen = ip->ip_hl << 2;
43828679Sbrian    if (hlen + sizeof(struct tcphdr) > len)
43928679Sbrian      goto bad;
44028679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
44128679Sbrian    hlen += THOFFSET(th) << 2;
44228679Sbrian    if (hlen > MAX_HDR)
44328679Sbrian      goto bad;
44430715Sbrian    memcpy(&cs->cs_ip, ip, hlen);
44528679Sbrian    cs->cs_hlen = hlen;
44636285Sbrian    slstat->sls_uncompressedin++;
44736285Sbrian    return (len);
4486059Samurai
44928679Sbrian  default:
45028679Sbrian    goto bad;
4516059Samurai
45228679Sbrian  case TYPE_COMPRESSED_TCP:
45328679Sbrian    break;
45428679Sbrian  }
45545138Sbrian
45628679Sbrian  /* We've got a compressed packet. */
45736285Sbrian  slstat->sls_compressedin++;
45836285Sbrian  cp = *bufp;
45928679Sbrian  changes = *cp++;
46036285Sbrian  log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
46145138Sbrian
46228679Sbrian  if (changes & NEW_C) {
46328679Sbrian    /*
46428679Sbrian     * Make sure the state index is in range, then grab the state. If we have
46528679Sbrian     * a good state index, clear the 'discard' flag.
46628679Sbrian     */
46746686Sbrian    if (*cp > max_state || comp->last_recv == 255)
46828679Sbrian      goto bad;
4696059Samurai
47028679Sbrian    comp->flags &= ~SLF_TOSS;
47128679Sbrian    comp->last_recv = *cp++;
47228679Sbrian  } else {
47328679Sbrian    /*
47428679Sbrian     * this packet has an implicit state index.  If we've had a line error
47528679Sbrian     * since the last time we got an explicit state index, we have to toss
47628679Sbrian     * the packet.
47728679Sbrian     */
47828679Sbrian    if (comp->flags & SLF_TOSS) {
47936285Sbrian      slstat->sls_tossed++;
48036285Sbrian      return (0);
48128679Sbrian    }
48228679Sbrian  }
48328679Sbrian  cs = &comp->rstate[comp->last_recv];
48428679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
48528679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
48628679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
48728679Sbrian  cp += 2;
48828679Sbrian  if (changes & TCP_PUSH_BIT)
48928679Sbrian    th->th_flags |= TH_PUSH;
49028679Sbrian  else
49128679Sbrian    th->th_flags &= ~TH_PUSH;
49228679Sbrian
49328679Sbrian  switch (changes & SPECIALS_MASK) {
49428679Sbrian  case SPECIAL_I:
49528679Sbrian    {
49628679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
49728679Sbrian
49828679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
49928679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
50028679Sbrian    }
50128679Sbrian    break;
50228679Sbrian
50328679Sbrian  case SPECIAL_D:
50428679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
50528679Sbrian		       - cs->cs_hlen);
50628679Sbrian    break;
50728679Sbrian
50828679Sbrian  default:
50928679Sbrian    if (changes & NEW_U) {
51028679Sbrian      th->th_flags |= TH_URG;
51128679Sbrian      DECODEU(th->th_urp)
51228679Sbrian    } else
51328679Sbrian      th->th_flags &= ~TH_URG;
51428679Sbrian    if (changes & NEW_W)
51528679Sbrian      DECODES(th->th_win)
51628679Sbrian	if (changes & NEW_A)
51728679Sbrian	DECODEL(th->th_ack)
51828679Sbrian	  if (changes & NEW_S) {
51936285Sbrian	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
52028679Sbrian		    *cp, cp[1], cp[2]);
52128679Sbrian	  DECODEL(th->th_seq)
5226059Samurai	}
52328679Sbrian    break;
52428679Sbrian  }
52528679Sbrian  if (changes & NEW_I) {
52628679Sbrian    DECODES(cs->cs_ip.ip_id)
52728679Sbrian  } else
52828679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5296059Samurai
53036285Sbrian  log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
53136285Sbrian	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
53226516Sbrian
53328679Sbrian  /*
53445138Sbrian   * At this point, cp points to the first byte of data in the packet.
53545138Sbrian   * Back up cp by the tcp/ip header length to make room for the
53645138Sbrian   * reconstructed header (we assume the packet we were handed has enough
53745138Sbrian   * space to prepend 128 bytes of header).  Adjust the length to account
53845138Sbrian   * for the new header & fill in the IP total length.
53928679Sbrian   */
54028679Sbrian  len -= (cp - *bufp);
54128679Sbrian  if (len < 0)
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
54852197Sbrian  *bufp = cp - cs->cs_hlen;
54928679Sbrian  len += cs->cs_hlen;
55028679Sbrian  cs->cs_ip.ip_len = htons(len);
5516059Samurai
55228679Sbrian  /* recompute the ip header checksum */
55352197Sbrian  cs->cs_ip.ip_sum = 0;
55452197Sbrian  bp = (u_short *)&cs->cs_ip;
55552197Sbrian  for (changes = 0; hlen > 0; hlen -= 2)
55652197Sbrian    changes += *bp++;
55752197Sbrian  changes = (changes & 0xffff) + (changes >> 16);
55852197Sbrian  changes = (changes & 0xffff) + (changes >> 16);
55952197Sbrian  cs->cs_ip.ip_sum = ~changes;
56028679Sbrian
56152197Sbrian  /* And copy the result into our buffer */
56252197Sbrian  memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
56345185Sbrian
56428679Sbrian  return (len);
5656059Samuraibad:
56628679Sbrian  comp->flags |= SLF_TOSS;
56736285Sbrian  slstat->sls_errorin++;
56836285Sbrian  return (0);
5696059Samurai}
5706059Samurai
5716059Samuraiint
57236285Sbriansl_Show(struct cmdargs const *arg)
5736059Samurai{
57436285Sbrian  prompt_Printf(arg->prompt, "VJ compression statistics:\n");
57536285Sbrian  prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
57636285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
57736285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
57836285Sbrian  prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
57936285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
58036285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
58136285Sbrian  prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
58236285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
58336285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
58436285Sbrian  prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
58536285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
58636285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
58726516Sbrian  return 0;
5886059Samurai}
589