slcompress.c revision 134789
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 134789 2004-09-05 01:46:52Z 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
34102500Sbrian#include <stdarg.h>
3530715Sbrian#include <stdio.h>
3630715Sbrian#include <string.h>
3736285Sbrian#include <termios.h>
3830715Sbrian
3946686Sbrian#include "layer.h"
4037009Sbrian#include "defs.h"
4131343Sbrian#include "command.h"
4230715Sbrian#include "mbuf.h"
4330715Sbrian#include "log.h"
446059Samurai#include "slcompress.h"
4536285Sbrian#include "descriptor.h"
4636285Sbrian#include "prompt.h"
4736285Sbrian#include "timer.h"
4836285Sbrian#include "fsm.h"
4936285Sbrian#include "throughput.h"
5036285Sbrian#include "iplist.h"
5138557Sbrian#include "lqr.h"
5238557Sbrian#include "hdlc.h"
5381634Sbrian#include "ncpaddr.h"
5436285Sbrian#include "ipcp.h"
5536285Sbrian#include "filter.h"
5636285Sbrian#include "lcp.h"
5736285Sbrian#include "ccp.h"
5836285Sbrian#include "link.h"
5936285Sbrian#include "mp.h"
6043313Sbrian#ifndef NORADIUS
6143313Sbrian#include "radius.h"
6243313Sbrian#endif
6381634Sbrian#include "ipv6cp.h"
6481634Sbrian#include "ncp.h"
6536285Sbrian#include "bundle.h"
666059Samurai
676059Samuraivoid
6846828Sbriansl_compress_init(struct slcompress *comp, int max_state)
696059Samurai{
7028679Sbrian  register u_int i;
7128679Sbrian  register struct cstate *tstate = comp->tstate;
726059Samurai
7331962Sbrian  memset(comp, '\0', sizeof *comp);
7430187Sbrian  for (i = max_state; i > 0; --i) {
7528679Sbrian    tstate[i].cs_id = i;
7628679Sbrian    tstate[i].cs_next = &tstate[i - 1];
7728679Sbrian  }
7830187Sbrian  tstate[0].cs_next = &tstate[max_state];
7928679Sbrian  tstate[0].cs_id = 0;
8028679Sbrian  comp->last_cs = &tstate[0];
8128679Sbrian  comp->last_recv = 255;
8228679Sbrian  comp->last_xmit = 255;
8328679Sbrian  comp->flags = SLF_TOSS;
846059Samurai}
856059Samurai
866059Samurai
876059Samurai/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
8837189Sbrian * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
896059Samurai * form).
906059Samurai */
916059Samurai#define ENCODE(n) { \
926059Samurai	if ((u_short)(n) >= 256) { \
936059Samurai		*cp++ = 0; \
946059Samurai		cp[1] = (n); \
956059Samurai		cp[0] = (n) >> 8; \
966059Samurai		cp += 2; \
976059Samurai	} else { \
986059Samurai		*cp++ = (n); \
996059Samurai	} \
1006059Samurai}
1016059Samurai#define ENCODEZ(n) { \
1026059Samurai	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
1036059Samurai		*cp++ = 0; \
1046059Samurai		cp[1] = (n); \
1056059Samurai		cp[0] = (n) >> 8; \
1066059Samurai		cp += 2; \
1076059Samurai	} else { \
1086059Samurai		*cp++ = (n); \
1096059Samurai	} \
1106059Samurai}
1116059Samurai
1126059Samurai#define DECODEL(f) { \
1136059Samurai	if (*cp == 0) {\
1146059Samurai		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
1156059Samurai		cp += 3; \
1166059Samurai	} else { \
11737189Sbrian		(f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
1186059Samurai	} \
1196059Samurai}
1206059Samurai
1216059Samurai#define DECODES(f) { \
1226059Samurai	if (*cp == 0) {\
1236059Samurai		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
1246059Samurai		cp += 3; \
1256059Samurai	} else { \
12637189Sbrian		(f) = htons(ntohs(f) + (u_int32_t)*cp++); \
1276059Samurai	} \
1286059Samurai}
1296059Samurai
1306059Samurai#define DECODEU(f) { \
1316059Samurai	if (*cp == 0) {\
1326059Samurai		(f) = htons((cp[1] << 8) | cp[2]); \
1336059Samurai		cp += 3; \
1346059Samurai	} else { \
13537189Sbrian		(f) = htons((u_int32_t)*cp++); \
1366059Samurai	} \
1376059Samurai}
1386059Samurai
1396059Samurai
1406059Samuraiu_char
14128679Sbriansl_compress_tcp(struct mbuf * m,
14228679Sbrian		struct ip * ip,
14336285Sbrian		struct slcompress *comp,
14436285Sbrian                struct slstat *slstat,
14528679Sbrian		int compress_cid)
1466059Samurai{
14728679Sbrian  register struct cstate *cs = comp->last_cs->cs_next;
14828679Sbrian  register u_int hlen = ip->ip_hl;
14928679Sbrian  register struct tcphdr *oth;
15028679Sbrian  register struct tcphdr *th;
15128679Sbrian  register u_int deltaS, deltaA;
15228679Sbrian  register u_int changes = 0;
15328679Sbrian  u_char new_seq[16];
15428679Sbrian  register u_char *cp = new_seq;
1556059Samurai
15628679Sbrian  /*
15728679Sbrian   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
15828679Sbrian   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
15928679Sbrian   * the caller has already made sure the packet is IP proto TCP).
16028679Sbrian   */
16154912Sbrian  if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
16258042Sbrian    log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
16358042Sbrian	      ip->ip_off, (unsigned long)m->m_len);
16436285Sbrian    log_DumpBp(LogDEBUG, "", m);
16528679Sbrian    return (TYPE_IP);
16628679Sbrian  }
16728679Sbrian  th = (struct tcphdr *) & ((int *) ip)[hlen];
16828679Sbrian  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
16936285Sbrian    log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
17036285Sbrian    log_DumpBp(LogDEBUG, "", m);
17128679Sbrian    return (TYPE_IP);
17228679Sbrian  }
1736059Samurai
17428679Sbrian  /*
17528679Sbrian   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
17628679Sbrian   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
17728679Sbrian   * connection state.  Special case the most recently used connection since
17828679Sbrian   * it's most likely to be used again & we don't have to do any reordering
17928679Sbrian   * if it's used.
18028679Sbrian   */
18136285Sbrian  slstat->sls_packets++;
18236285Sbrian  if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
18336285Sbrian      ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
18436285Sbrian      *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
1856059Samurai
18628679Sbrian    /*
18728679Sbrian     * Wasn't the first -- search for it.
18898243Sbrian     *
18928679Sbrian     * States are kept in a circularly linked list with last_cs pointing to the
19028679Sbrian     * end of the list.  The list is kept in lru order by moving a state to
19128679Sbrian     * the head of the list whenever it is referenced.  Since the list is
19228679Sbrian     * short and, empirically, the connection we want is almost always near
19328679Sbrian     * the front, we locate states via linear search.  If we don't find a
19428679Sbrian     * state for the datagram, the oldest state is (re-)used.
19528679Sbrian     */
19628679Sbrian    register struct cstate *lcs;
19728679Sbrian    register struct cstate *lastcs = comp->last_cs;
1986059Samurai
19928679Sbrian    do {
20028679Sbrian      lcs = cs;
20128679Sbrian      cs = cs->cs_next;
20236285Sbrian      slstat->sls_searches++;
20336285Sbrian      if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
20436285Sbrian	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
20536285Sbrian	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
20628679Sbrian	goto found;
20728679Sbrian    } while (cs != lastcs);
2086059Samurai
20928679Sbrian    /*
21028679Sbrian     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
21128679Sbrian     * that tells the other side what connection number we're using for this
21228679Sbrian     * conversation. Note that since the state list is circular, the oldest
21328679Sbrian     * state points to the newest and we only need to set last_cs to update
21428679Sbrian     * the lru linkage.
21528679Sbrian     */
21636285Sbrian    slstat->sls_misses++;
21728679Sbrian      comp->last_cs = lcs;
2186059Samurai#define	THOFFSET(th)	(th->th_off)
21928679Sbrian    hlen += th->th_off;
22028679Sbrian    hlen <<= 2;
22154912Sbrian    if (hlen > m->m_len)
22228679Sbrian      return (TYPE_IP);
22328679Sbrian    goto uncompressed;
2246059Samurai
22528679Sbrianfound:
2266059Samurai
22728679Sbrian    /*
22828679Sbrian     * Found it -- move to the front on the connection list.
22928679Sbrian     */
23028679Sbrian    if (cs == lastcs)
23128679Sbrian      comp->last_cs = lcs;
23228679Sbrian    else {
23328679Sbrian      lcs->cs_next = cs->cs_next;
23428679Sbrian      cs->cs_next = lastcs->cs_next;
23528679Sbrian      lastcs->cs_next = cs;
23628679Sbrian    }
23728679Sbrian  }
2386059Samurai
23928679Sbrian  /*
24028679Sbrian   * Make sure that only what we expect to change changed. The first line of
24128679Sbrian   * the `if' checks the IP protocol version, header length & type of
24228679Sbrian   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
24328679Sbrian   * checks the time-to-live and protocol (the protocol check is unnecessary
24428679Sbrian   * but costless).  The 4th line checks the TCP header length.  The 5th line
24528679Sbrian   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
24628679Sbrian   * any of these things are different between the previous & current
24728679Sbrian   * datagram, we send the current datagram `uncompressed'.
24828679Sbrian   */
24928679Sbrian  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
25028679Sbrian  deltaS = hlen;
25128679Sbrian  hlen += th->th_off;
25228679Sbrian  hlen <<= 2;
25354912Sbrian  if (hlen > m->m_len)
25428679Sbrian    return (TYPE_IP);
2556059Samurai
25628679Sbrian  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
25728679Sbrian      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
25828679Sbrian      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
25928679Sbrian      THOFFSET(th) != THOFFSET(oth) ||
26028679Sbrian      (deltaS > 5 &&
26130715Sbrian       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
26228679Sbrian      (THOFFSET(th) > 5 &&
26330715Sbrian       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
26428679Sbrian    goto uncompressed;
26528679Sbrian  }
2666059Samurai
26728679Sbrian  /*
26828679Sbrian   * Figure out which of the changing fields changed.  The receiver expects
26928679Sbrian   * changes in the order: urgent, window, ack, seq (the order minimizes the
27028679Sbrian   * number of temporaries needed in this section of code).
27128679Sbrian   */
27228679Sbrian  if (th->th_flags & TH_URG) {
27328679Sbrian    deltaS = ntohs(th->th_urp);
27428679Sbrian    ENCODEZ(deltaS);
27528679Sbrian    changes |= NEW_U;
27628679Sbrian  } else if (th->th_urp != oth->th_urp) {
2776059Samurai
27828679Sbrian    /*
27928679Sbrian     * argh! URG not set but urp changed -- a sensible implementation should
28028679Sbrian     * never do this but RFC793 doesn't prohibit the change so we have to
28128679Sbrian     * deal with it.
28228679Sbrian     */
28328679Sbrian    goto uncompressed;
28428679Sbrian  }
28528679Sbrian  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
28628679Sbrian  if (deltaS) {
28728679Sbrian    ENCODE(deltaS);
28828679Sbrian    changes |= NEW_W;
28928679Sbrian  }
29028679Sbrian  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
29128679Sbrian  if (deltaA) {
29228679Sbrian    if (deltaA > 0xffff) {
29328679Sbrian      goto uncompressed;
29428679Sbrian    }
29528679Sbrian    ENCODE(deltaA);
29628679Sbrian    changes |= NEW_A;
29728679Sbrian  }
29828679Sbrian  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
29928679Sbrian  if (deltaS) {
30028679Sbrian    if (deltaS > 0xffff) {
30128679Sbrian      goto uncompressed;
30228679Sbrian    }
30328679Sbrian    ENCODE(deltaS);
30428679Sbrian    changes |= NEW_S;
30528679Sbrian  }
30628679Sbrian  switch (changes) {
3076059Samurai
30828679Sbrian  case 0:
3096059Samurai
31028679Sbrian    /*
31128679Sbrian     * Nothing changed. If this packet contains data and the last one didn't,
31228679Sbrian     * this is probably a data packet following an ack (normal on an
31328679Sbrian     * interactive connection) and we send it compressed.  Otherwise it's
31428679Sbrian     * probably a retransmit, retransmitted ack or window probe.  Send it
31528679Sbrian     * uncompressed in case the other side missed the compressed version.
31628679Sbrian     */
31728679Sbrian    if (ip->ip_len != cs->cs_ip.ip_len &&
31828679Sbrian	ntohs(cs->cs_ip.ip_len) == hlen)
31928679Sbrian      break;
3206059Samurai
321102413Scharnier    /* FALLTHROUGH */
3226059Samurai
32328679Sbrian  case SPECIAL_I:
32428679Sbrian  case SPECIAL_D:
3256059Samurai
32628679Sbrian    /*
32728679Sbrian     * actual changes match one of our special case encodings -- send packet
32828679Sbrian     * uncompressed.
32928679Sbrian     */
33028679Sbrian    goto uncompressed;
3316059Samurai
33228679Sbrian  case NEW_S | NEW_A:
33328679Sbrian    if (deltaS == deltaA &&
33428679Sbrian	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33528679Sbrian      /* special case for echoed terminal traffic */
33628679Sbrian      changes = SPECIAL_I;
33728679Sbrian      cp = new_seq;
33828679Sbrian    }
33928679Sbrian    break;
3406059Samurai
34128679Sbrian  case NEW_S:
34228679Sbrian    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
34328679Sbrian      /* special case for data xfer */
34428679Sbrian      changes = SPECIAL_D;
34528679Sbrian      cp = new_seq;
34628679Sbrian    }
34728679Sbrian    break;
34828679Sbrian  }
3496059Samurai
35028679Sbrian  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
35128679Sbrian  if (deltaS != 1) {
35228679Sbrian    ENCODEZ(deltaS);
35328679Sbrian    changes |= NEW_I;
35428679Sbrian  }
35528679Sbrian  if (th->th_flags & TH_PUSH)
35628679Sbrian    changes |= TCP_PUSH_BIT;
3576059Samurai
35828679Sbrian  /*
35928679Sbrian   * Grab the cksum before we overwrite it below.  Then update our state with
36028679Sbrian   * this packet's header.
36128679Sbrian   */
36228679Sbrian  deltaA = ntohs(th->th_sum);
36330715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
3646059Samurai
36528679Sbrian  /*
36628679Sbrian   * We want to use the original packet as our compressed packet. (cp -
36728679Sbrian   * new_seq) is the number of bytes we need for compressed sequence numbers.
36828679Sbrian   * In addition we need one byte for the change mask, one for the connection
36928679Sbrian   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
37028679Sbrian   * are needed.  hlen is how many bytes of the original packet to toss so
37128679Sbrian   * subtract the two to get the new packet size.
37228679Sbrian   */
37328679Sbrian  deltaS = cp - new_seq;
37428679Sbrian  cp = (u_char *) ip;
37528679Sbrian
37628679Sbrian  /*
37728679Sbrian   * Since fastq traffic can jump ahead of the background traffic, we don't
37828679Sbrian   * know what order packets will go on the line.  In this case, we always
37928679Sbrian   * send a "new" connection id so the receiver state stays synchronized.
38028679Sbrian   */
38130187Sbrian  if (comp->last_xmit == cs->cs_id && compress_cid) {
38228679Sbrian    hlen -= deltaS + 3;
38328679Sbrian    cp += hlen;
38428679Sbrian    *cp++ = changes;
38530187Sbrian  } else {
38628679Sbrian    comp->last_xmit = cs->cs_id;
38728679Sbrian    hlen -= deltaS + 4;
38828679Sbrian    cp += hlen;
38928679Sbrian    *cp++ = changes | NEW_C;
39028679Sbrian    *cp++ = cs->cs_id;
39128679Sbrian  }
39254912Sbrian  m->m_len -= hlen;
39354912Sbrian  m->m_offset += hlen;
39428679Sbrian  *cp++ = deltaA >> 8;
39528679Sbrian  *cp++ = deltaA;
39630715Sbrian  memcpy(cp, new_seq, deltaS);
39736285Sbrian  slstat->sls_compressed++;
39836285Sbrian  return (TYPE_COMPRESSED_TCP);
3996059Samurai
40028679Sbrian  /*
40128679Sbrian   * Update connection state cs & send uncompressed packet ('uncompressed'
40228679Sbrian   * means a regular ip/tcp packet but with the 'conversation id' we hope to
40328679Sbrian   * use on future compressed packets in the protocol field).
40428679Sbrian   */
4056059Samuraiuncompressed:
40630715Sbrian  memcpy(&cs->cs_ip, ip, hlen);
40728679Sbrian  ip->ip_p = cs->cs_id;
40828679Sbrian  comp->last_xmit = cs->cs_id;
40928679Sbrian  return (TYPE_UNCOMPRESSED_TCP);
4106059Samurai}
4116059Samurai
4126059Samurai
4136059Samuraiint
41436960Sbriansl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
41536960Sbrian                  struct slstat *slstat, int max_state)
4166059Samurai{
41728679Sbrian  register u_char *cp;
41828679Sbrian  register u_int hlen, changes;
41928679Sbrian  register struct tcphdr *th;
42028679Sbrian  register struct cstate *cs;
42128679Sbrian  register struct ip *ip;
42252197Sbrian  u_short *bp;
4236059Samurai
42428679Sbrian  switch (type) {
4256059Samurai
42628679Sbrian  case TYPE_UNCOMPRESSED_TCP:
42728679Sbrian    ip = (struct ip *) * bufp;
42836960Sbrian    if (ip->ip_p > max_state)
42928679Sbrian      goto bad;
43028679Sbrian    cs = &comp->rstate[comp->last_recv = ip->ip_p];
43128679Sbrian    comp->flags &= ~SLF_TOSS;
43228679Sbrian    ip->ip_p = IPPROTO_TCP;
4336059Samurai
43428679Sbrian    /*
43528679Sbrian     * Calculate the size of the TCP/IP header and make sure that we don't
43628679Sbrian     * overflow the space we have available for it.
43728679Sbrian     */
43828679Sbrian    hlen = ip->ip_hl << 2;
439134789Sbrian    if ((int)(hlen + sizeof(struct tcphdr)) > len)
44028679Sbrian      goto bad;
44128679Sbrian    th = (struct tcphdr *) & ((char *) ip)[hlen];
44228679Sbrian    hlen += THOFFSET(th) << 2;
44328679Sbrian    if (hlen > MAX_HDR)
44428679Sbrian      goto bad;
44530715Sbrian    memcpy(&cs->cs_ip, ip, hlen);
44628679Sbrian    cs->cs_hlen = hlen;
44736285Sbrian    slstat->sls_uncompressedin++;
44836285Sbrian    return (len);
4496059Samurai
45028679Sbrian  default:
45128679Sbrian    goto bad;
4526059Samurai
45328679Sbrian  case TYPE_COMPRESSED_TCP:
45428679Sbrian    break;
45528679Sbrian  }
45645138Sbrian
45728679Sbrian  /* We've got a compressed packet. */
45836285Sbrian  slstat->sls_compressedin++;
45936285Sbrian  cp = *bufp;
46028679Sbrian  changes = *cp++;
46136285Sbrian  log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
46245138Sbrian
46328679Sbrian  if (changes & NEW_C) {
46428679Sbrian    /*
46528679Sbrian     * Make sure the state index is in range, then grab the state. If we have
46628679Sbrian     * a good state index, clear the 'discard' flag.
46728679Sbrian     */
46846686Sbrian    if (*cp > max_state || comp->last_recv == 255)
46928679Sbrian      goto bad;
4706059Samurai
47128679Sbrian    comp->flags &= ~SLF_TOSS;
47228679Sbrian    comp->last_recv = *cp++;
47328679Sbrian  } else {
47428679Sbrian    /*
47528679Sbrian     * this packet has an implicit state index.  If we've had a line error
47628679Sbrian     * since the last time we got an explicit state index, we have to toss
47728679Sbrian     * the packet.
47828679Sbrian     */
47928679Sbrian    if (comp->flags & SLF_TOSS) {
48036285Sbrian      slstat->sls_tossed++;
48136285Sbrian      return (0);
48228679Sbrian    }
48328679Sbrian  }
48428679Sbrian  cs = &comp->rstate[comp->last_recv];
48528679Sbrian  hlen = cs->cs_ip.ip_hl << 2;
48628679Sbrian  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
48728679Sbrian  th->th_sum = htons((*cp << 8) | cp[1]);
48828679Sbrian  cp += 2;
48928679Sbrian  if (changes & TCP_PUSH_BIT)
49028679Sbrian    th->th_flags |= TH_PUSH;
49128679Sbrian  else
49228679Sbrian    th->th_flags &= ~TH_PUSH;
49328679Sbrian
49428679Sbrian  switch (changes & SPECIALS_MASK) {
49528679Sbrian  case SPECIAL_I:
49628679Sbrian    {
49728679Sbrian      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
49828679Sbrian
49928679Sbrian      th->th_ack = htonl(ntohl(th->th_ack) + i);
50028679Sbrian      th->th_seq = htonl(ntohl(th->th_seq) + i);
50128679Sbrian    }
50228679Sbrian    break;
50328679Sbrian
50428679Sbrian  case SPECIAL_D:
50528679Sbrian    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
50628679Sbrian		       - cs->cs_hlen);
50728679Sbrian    break;
50828679Sbrian
50928679Sbrian  default:
51028679Sbrian    if (changes & NEW_U) {
51128679Sbrian      th->th_flags |= TH_URG;
51228679Sbrian      DECODEU(th->th_urp)
51328679Sbrian    } else
51428679Sbrian      th->th_flags &= ~TH_URG;
51528679Sbrian    if (changes & NEW_W)
51628679Sbrian      DECODES(th->th_win)
51728679Sbrian	if (changes & NEW_A)
51828679Sbrian	DECODEL(th->th_ack)
51928679Sbrian	  if (changes & NEW_S) {
52036285Sbrian	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
52128679Sbrian		    *cp, cp[1], cp[2]);
52228679Sbrian	  DECODEL(th->th_seq)
5236059Samurai	}
52428679Sbrian    break;
52528679Sbrian  }
52628679Sbrian  if (changes & NEW_I) {
52728679Sbrian    DECODES(cs->cs_ip.ip_id)
52828679Sbrian  } else
52928679Sbrian    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
5306059Samurai
53136285Sbrian  log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
53236285Sbrian	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
53326516Sbrian
53428679Sbrian  /*
53545138Sbrian   * At this point, cp points to the first byte of data in the packet.
53645138Sbrian   * Back up cp by the tcp/ip header length to make room for the
53745138Sbrian   * reconstructed header (we assume the packet we were handed has enough
53845138Sbrian   * space to prepend 128 bytes of header).  Adjust the length to account
53945138Sbrian   * for the new header & fill in the IP total length.
54028679Sbrian   */
54128679Sbrian  len -= (cp - *bufp);
54228679Sbrian  if (len < 0)
54328679Sbrian    /*
54428679Sbrian     * we must have dropped some characters (crc should detect this but the
54528679Sbrian     * old slip framing won't)
54628679Sbrian     */
54728679Sbrian    goto bad;
54828679Sbrian
54952197Sbrian  *bufp = cp - cs->cs_hlen;
55028679Sbrian  len += cs->cs_hlen;
55128679Sbrian  cs->cs_ip.ip_len = htons(len);
5526059Samurai
55328679Sbrian  /* recompute the ip header checksum */
55452197Sbrian  cs->cs_ip.ip_sum = 0;
55552197Sbrian  bp = (u_short *)&cs->cs_ip;
55652197Sbrian  for (changes = 0; hlen > 0; hlen -= 2)
55752197Sbrian    changes += *bp++;
55852197Sbrian  changes = (changes & 0xffff) + (changes >> 16);
55952197Sbrian  changes = (changes & 0xffff) + (changes >> 16);
56052197Sbrian  cs->cs_ip.ip_sum = ~changes;
56128679Sbrian
56252197Sbrian  /* And copy the result into our buffer */
56352197Sbrian  memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
56445185Sbrian
56528679Sbrian  return (len);
5666059Samuraibad:
56728679Sbrian  comp->flags |= SLF_TOSS;
56836285Sbrian  slstat->sls_errorin++;
56936285Sbrian  return (0);
5706059Samurai}
5716059Samurai
5726059Samuraiint
57336285Sbriansl_Show(struct cmdargs const *arg)
5746059Samurai{
57536285Sbrian  prompt_Printf(arg->prompt, "VJ compression statistics:\n");
57636285Sbrian  prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
57736285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
57836285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
57936285Sbrian  prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
58036285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
58136285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
58236285Sbrian  prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
58336285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
58436285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
58536285Sbrian  prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
58636285Sbrian	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
58736285Sbrian                arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
58826516Sbrian  return 0;
5896059Samurai}
590