slcompress.c revision 36960
159769Sgrog/*
259769Sgrog * Routines to compress and uncompess tcp packets (for transmission
324424Swosch * over low speed serial lines.
424424Swosch *
524424Swosch * Copyright (c) 1989 Regents of the University of California.
624424Swosch * All rights reserved.
724424Swosch *
824424Swosch * Redistribution and use in source and binary forms are permitted
924424Swosch * provided that the above copyright notice and this paragraph are
1024424Swosch * duplicated in all such forms and that any documentation,
1124424Swosch * advertising materials, and other materials related to such
1224424Swosch * distribution and use acknowledge that the software was developed
1324424Swosch * by the University of California, Berkeley.  The name of the
1424424Swosch * University may not be used to endorse or promote products derived
1542704Swosch * from this software without specific prior written permission.
1642704Swosch * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1742704Swosch * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1824424Swosch * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1942704Swosch *
2042704Swosch * $Id: slcompress.c,v 1.16 1998/05/21 21:48:27 brian Exp $
2142704Swosch *
2242704Swosch *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
2342704Swosch *	- Initial distribution.
2442704Swosch */
2542704Swosch
2642704Swosch#include <sys/types.h>
2742704Swosch#include <netinet/in_systm.h>
2842704Swosch#include <netinet/in.h>
2942704Swosch#include <netinet/tcp.h>
3059769Sgrog#include <netinet/ip.h>
3159769Sgrog#include <sys/un.h>
3259769Sgrog
3359769Sgrog#include <stdio.h>
3459769Sgrog#include <string.h>
3559769Sgrog#include <termios.h>
3659769Sgrog
3759769Sgrog#include "command.h"
3859769Sgrog#include "mbuf.h"
3924424Swosch#include "log.h"
4042704Swosch#include "defs.h"
4124424Swosch#include "slcompress.h"
4242704Swosch#include "descriptor.h"
4324424Swosch#include "prompt.h"
4442704Swosch#include "timer.h"
4524424Swosch#include "fsm.h"
4624424Swosch#include "throughput.h"
4724424Swosch#include "iplist.h"
4842704Swosch#include "ipcp.h"
4925031Swosch#include "filter.h"
5059156Swosch#include "lqr.h"
5125031Swosch#include "hdlc.h"
5225031Swosch#include "lcp.h"
5324424Swosch#include "ccp.h"
5424424Swosch#include "link.h"
5524424Swosch#include "mp.h"
5624424Swosch#include "bundle.h"
5771231Sitojun
5824424Swoschvoid
5971231Sitojunsl_compress_init(struct slcompress * comp, int max_state)
6025031Swosch{
6171231Sitojun  register u_int i;
6224424Swosch  register struct cstate *tstate = comp->tstate;
6325031Swosch
6425031Swosch  memset(comp, '\0', sizeof *comp);
6571231Sitojun  for (i = max_state; i > 0; --i) {
6625031Swosch    tstate[i].cs_id = i;
6771231Sitojun    tstate[i].cs_next = &tstate[i - 1];
6870110Swosch  }
6970110Swosch  tstate[0].cs_next = &tstate[max_state];
7070110Swosch  tstate[0].cs_id = 0;
7170110Swosch  comp->last_cs = &tstate[0];
7270110Swosch  comp->last_recv = 255;
7370110Swosch  comp->last_xmit = 255;
7470110Swosch  comp->flags = SLF_TOSS;
7570110Swosch}
7670110Swosch
7770110Swosch
7870110Swosch/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
7970110Swosch * checks for zero (since zero has to be encoded in the long, 3 byte
8070110Swosch * form).
8170110Swosch */
8270110Swosch#define ENCODE(n) { \
8370110Swosch	if ((u_short)(n) >= 256) { \
8470110Swosch		*cp++ = 0; \
8570110Swosch		cp[1] = (n); \
8670110Swosch		cp[0] = (n) >> 8; \
8770110Swosch		cp += 2; \
8870110Swosch	} else { \
8970110Swosch		*cp++ = (n); \
9070110Swosch	} \
9170110Swosch}
9270110Swosch#define ENCODEZ(n) { \
9370110Swosch	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
9470110Swosch		*cp++ = 0; \
9570110Swosch		cp[1] = (n); \
9670110Swosch		cp[0] = (n) >> 8; \
9770110Swosch		cp += 2; \
9870110Swosch	} else { \
9970110Swosch		*cp++ = (n); \
10070110Swosch	} \
10170110Swosch}
10270110Swosch
10370110Swosch#define DECODEL(f) { \
10470110Swosch	if (*cp == 0) {\
10570110Swosch		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
10670110Swosch		cp += 3; \
10770110Swosch	} else { \
10870110Swosch		(f) = htonl(ntohl(f) + (u_long)*cp++); \
10970110Swosch	} \
11070110Swosch}
11170110Swosch
11270110Swosch#define DECODES(f) { \
11370110Swosch	if (*cp == 0) {\
11470110Swosch		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
11570110Swosch		cp += 3; \
11670110Swosch	} else { \
11770110Swosch		(f) = htons(ntohs(f) + (u_long)*cp++); \
11870110Swosch	} \
11970110Swosch}
12070110Swosch
12170110Swosch#define DECODEU(f) { \
12270110Swosch	if (*cp == 0) {\
12370110Swosch		(f) = htons((cp[1] << 8) | cp[2]); \
12470110Swosch		cp += 3; \
12570110Swosch	} else { \
12670110Swosch		(f) = htons((u_long)*cp++); \
12770110Swosch	} \
12870119Swosch}
12970119Swosch
13070119Swosch
13170119Swoschu_char
13270119Swoschsl_compress_tcp(struct mbuf * m,
13370119Swosch		struct ip * ip,
13470119Swosch		struct slcompress *comp,
13570119Swosch                struct slstat *slstat,
13670119Swosch		int compress_cid)
13771231Sitojun{
13870119Swosch  register struct cstate *cs = comp->last_cs->cs_next;
13970119Swosch  register u_int hlen = ip->ip_hl;
14072877Swosch  register struct tcphdr *oth;
14172877Swosch  register struct tcphdr *th;
14272877Swosch  register u_int deltaS, deltaA;
14372877Swosch  register u_int changes = 0;
14465412Swosch  u_char new_seq[16];
14565412Swosch  register u_char *cp = new_seq;
14665412Swosch
14765412Swosch  /*
14824424Swosch   * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
14924424Swosch   * (i.e., ACK isn't set or some other control bit is set).  (We assume that
15024424Swosch   * the caller has already made sure the packet is IP proto TCP).
15124424Swosch   */
15224424Swosch  if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) {
15369277Sasmodai    log_Printf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n",
15469277Sasmodai	      ip->ip_off, m->cnt);
15524424Swosch    log_DumpBp(LogDEBUG, "", m);
15625031Swosch    return (TYPE_IP);
15725031Swosch  }
15825031Swosch  th = (struct tcphdr *) & ((int *) ip)[hlen];
15925031Swosch  if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
16025031Swosch    log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
16125031Swosch    log_DumpBp(LogDEBUG, "", m);
16225031Swosch    return (TYPE_IP);
16325031Swosch  }
16425031Swosch
16525031Swosch  /*
16625031Swosch   * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
16725031Swosch   * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
16825031Swosch   * connection state.  Special case the most recently used connection since
16925031Swosch   * it's most likely to be used again & we don't have to do any reordering
17025031Swosch   * if it's used.
17138440Sjkh   */
17245349Swosch  slstat->sls_packets++;
17345349Swosch  if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
17442704Swosch      ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
17570110Swosch      *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
17625031Swosch
17724424Swosch    /*
17859769Sgrog     * Wasn't the first -- search for it.
17925031Swosch     *
18025031Swosch     * States are kept in a circularly linked list with last_cs pointing to the
18125031Swosch     * end of the list.  The list is kept in lru order by moving a state to
18225031Swosch     * the head of the list whenever it is referenced.  Since the list is
18359769Sgrog     * short and, empirically, the connection we want is almost always near
18425031Swosch     * the front, we locate states via linear search.  If we don't find a
18525031Swosch     * state for the datagram, the oldest state is (re-)used.
18625031Swosch     */
18725031Swosch    register struct cstate *lcs;
18824424Swosch    register struct cstate *lastcs = comp->last_cs;
18925031Swosch
19025031Swosch    do {
19125031Swosch      lcs = cs;
19225031Swosch      cs = cs->cs_next;
19325031Swosch      slstat->sls_searches++;
19459769Sgrog      if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
19559769Sgrog	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
19642704Swosch	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
19742704Swosch	goto found;
19842704Swosch    } while (cs != lastcs);
19970110Swosch
20042704Swosch    /*
20142704Swosch     * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
20225031Swosch     * that tells the other side what connection number we're using for this
20325031Swosch     * conversation. Note that since the state list is circular, the oldest
20424424Swosch     * state points to the newest and we only need to set last_cs to update
20525031Swosch     * the lru linkage.
20625031Swosch     */
20725031Swosch    slstat->sls_misses++;
20825031Swosch      comp->last_cs = lcs;
20925031Swosch#define	THOFFSET(th)	(th->th_off)
21025031Swosch    hlen += th->th_off;
21125031Swosch    hlen <<= 2;
21225031Swosch    if (hlen > m->cnt)
21325031Swosch      return (TYPE_IP);
21424424Swosch    goto uncompressed;
21525031Swosch
21625031Swoschfound:
21725031Swosch
21825031Swosch    /*
21925031Swosch     * Found it -- move to the front on the connection list.
22025031Swosch     */
22125031Swosch    if (cs == lastcs)
22225031Swosch      comp->last_cs = lcs;
22359156Swosch    else {
22425031Swosch      lcs->cs_next = cs->cs_next;
22525031Swosch      cs->cs_next = lastcs->cs_next;
22625031Swosch      lastcs->cs_next = cs;
22725031Swosch    }
22825031Swosch  }
22925031Swosch
23025031Swosch  /*
23125031Swosch   * Make sure that only what we expect to change changed. The first line of
23225031Swosch   * the `if' checks the IP protocol version, header length & type of
23325031Swosch   * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
23424424Swosch   * checks the time-to-live and protocol (the protocol check is unnecessary
23525031Swosch   * but costless).  The 4th line checks the TCP header length.  The 5th line
23625031Swosch   * checks IP options, if any.  The 6th line checks TCP options, if any.  If
23725031Swosch   * any of these things are different between the previous & current
23825031Swosch   * datagram, we send the current datagram `uncompressed'.
23925031Swosch   */
24025031Swosch  oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
24125031Swosch  deltaS = hlen;
24225031Swosch  hlen += th->th_off;
24370110Swosch  hlen <<= 2;
24471231Sitojun  if (hlen > m->cnt)
24570110Swosch    return (TYPE_IP);
24625031Swosch
24771231Sitojun  if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
24871231Sitojun      ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
24969278Sasmodai      ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
25025031Swosch      THOFFSET(th) != THOFFSET(oth) ||
25171231Sitojun      (deltaS > 5 &&
25270110Swosch       memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
25371231Sitojun      (THOFFSET(th) > 5 &&
25470110Swosch       memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
25570110Swosch    goto uncompressed;
25671231Sitojun  }
25770110Swosch
25857000Swosch  /*
25925031Swosch   * Figure out which of the changing fields changed.  The receiver expects
26045349Swosch   * changes in the order: urgent, window, ack, seq (the order minimizes the
26175732Swosch   * number of temporaries needed in this section of code).
26271231Sitojun   */
26325031Swosch  if (th->th_flags & TH_URG) {
26438440Sjkh    deltaS = ntohs(th->th_urp);
26538440Sjkh    ENCODEZ(deltaS);
26649392Swosch    changes |= NEW_U;
26757000Swosch  } else if (th->th_urp != oth->th_urp) {
26838440Sjkh
26938440Sjkh    /*
27070110Swosch     * argh! URG not set but urp changed -- a sensible implementation should
27169278Sasmodai     * never do this but RFC793 doesn't prohibit the change so we have to
27270110Swosch     * deal with it.
27325031Swosch     */
27425031Swosch    goto uncompressed;
27569278Sasmodai  }
27645349Swosch  deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
27770110Swosch  if (deltaS) {
27869278Sasmodai    ENCODE(deltaS);
27945349Swosch    changes |= NEW_W;
28045349Swosch  }
28169278Sasmodai  deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
28269278Sasmodai  if (deltaA) {
28369278Sasmodai    if (deltaA > 0xffff) {
28470110Swosch      goto uncompressed;
28569278Sasmodai    }
28669278Sasmodai    ENCODE(deltaA);
28769278Sasmodai    changes |= NEW_A;
28857000Swosch  }
28945349Swosch  deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
29069277Sasmodai  if (deltaS) {
29145349Swosch    if (deltaS > 0xffff) {
29266542Sitojun      goto uncompressed;
29369277Sasmodai    }
29457000Swosch    ENCODE(deltaS);
29570110Swosch    changes |= NEW_S;
29645349Swosch  }
29757000Swosch  switch (changes) {
29869277Sasmodai
29970110Swosch  case 0:
30070110Swosch
30142589Swosch    /*
30270110Swosch     * Nothing changed. If this packet contains data and the last one didn't,
30370110Swosch     * this is probably a data packet following an ack (normal on an
30446321Swosch     * interactive connection) and we send it compressed.  Otherwise it's
30545349Swosch     * probably a retransmit, retransmitted ack or window probe.  Send it
30645349Swosch     * uncompressed in case the other side missed the compressed version.
30757000Swosch     */
30846318Swosch    if (ip->ip_len != cs->cs_ip.ip_len &&
30970110Swosch	ntohs(cs->cs_ip.ip_len) == hlen)
31056406Swosch      break;
31155389Sbillf
31255389Sbillf    /* (fall through) */
31357000Swosch
31455389Sbillf  case SPECIAL_I:
31555389Sbillf  case SPECIAL_D:
31655389Sbillf
31770110Swosch    /*
31858448Swosch     * actual changes match one of our special case encodings -- send packet
31958448Swosch     * uncompressed.
32065412Swosch     */
32164612Salex    goto uncompressed;
32264612Salex
32365411Swosch  case NEW_S | NEW_A:
32465974Swosch    if (deltaS == deltaA &&
32569277Sasmodai	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
32669277Sasmodai      /* special case for echoed terminal traffic */
32770119Swosch      changes = SPECIAL_I;
32869277Sasmodai      cp = new_seq;
32970111Swosch    }
33075833Swosch    break;
33124424Swosch
33225031Swosch  case NEW_S:
33324424Swosch    if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
33424424Swosch      /* special case for data xfer */
33524424Swosch      changes = SPECIAL_D;
33624424Swosch      cp = new_seq;
33724424Swosch    }
33824424Swosch    break;
33924424Swosch  }
34024424Swosch
34124424Swosch  deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
34224424Swosch  if (deltaS != 1) {
34324424Swosch    ENCODEZ(deltaS);
34424424Swosch    changes |= NEW_I;
34524424Swosch  }
34624424Swosch  if (th->th_flags & TH_PUSH)
34725031Swosch    changes |= TCP_PUSH_BIT;
34825031Swosch
34924424Swosch  /*
35024424Swosch   * Grab the cksum before we overwrite it below.  Then update our state with
35124424Swosch   * this packet's header.
35224424Swosch   */
35359769Sgrog  deltaA = ntohs(th->th_sum);
35424424Swosch  memcpy(&cs->cs_ip, ip, hlen);
35524424Swosch
35625031Swosch  /*
35725031Swosch   * We want to use the original packet as our compressed packet. (cp -
35825031Swosch   * new_seq) is the number of bytes we need for compressed sequence numbers.
35925031Swosch   * In addition we need one byte for the change mask, one for the connection
36059769Sgrog   * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
36125031Swosch   * are needed.  hlen is how many bytes of the original packet to toss so
36231658Swosch   * subtract the two to get the new packet size.
36359769Sgrog   */
36431658Swosch  deltaS = cp - new_seq;
36559769Sgrog  cp = (u_char *) ip;
36659769Sgrog
36759769Sgrog  /*
36865415Swosch   * Since fastq traffic can jump ahead of the background traffic, we don't
36965415Swosch   * know what order packets will go on the line.  In this case, we always
37065415Swosch   * send a "new" connection id so the receiver state stays synchronized.
37175834Swosch   */
37275834Swosch  if (comp->last_xmit == cs->cs_id && compress_cid) {
37375834Swosch    hlen -= deltaS + 3;
37475834Swosch    cp += hlen;
37575834Swosch    *cp++ = changes;
37625031Swosch  } else {
37725031Swosch    comp->last_xmit = cs->cs_id;
37825031Swosch    hlen -= deltaS + 4;
37959769Sgrog    cp += hlen;
38025031Swosch    *cp++ = changes | NEW_C;
38125031Swosch    *cp++ = cs->cs_id;
38231658Swosch  }
38325031Swosch  m->cnt -= hlen;
38424424Swosch  m->offset += hlen;
38572877Swosch  *cp++ = deltaA >> 8;
38667388Swosch  *cp++ = deltaA;
38742589Swosch  memcpy(cp, new_seq, deltaS);
38850970Speter  slstat->sls_compressed++;
38959769Sgrog  return (TYPE_COMPRESSED_TCP);
390
391  /*
392   * Update connection state cs & send uncompressed packet ('uncompressed'
393   * means a regular ip/tcp packet but with the 'conversation id' we hope to
394   * use on future compressed packets in the protocol field).
395   */
396uncompressed:
397  memcpy(&cs->cs_ip, ip, hlen);
398  ip->ip_p = cs->cs_id;
399  comp->last_xmit = cs->cs_id;
400  return (TYPE_UNCOMPRESSED_TCP);
401}
402
403
404int
405sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
406                  struct slstat *slstat, int max_state)
407{
408  register u_char *cp;
409  register u_int hlen, changes;
410  register struct tcphdr *th;
411  register struct cstate *cs;
412  register struct ip *ip;
413
414  switch (type) {
415
416  case TYPE_UNCOMPRESSED_TCP:
417    ip = (struct ip *) * bufp;
418    if (ip->ip_p > max_state)
419      goto bad;
420    cs = &comp->rstate[comp->last_recv = ip->ip_p];
421    comp->flags &= ~SLF_TOSS;
422    ip->ip_p = IPPROTO_TCP;
423
424    /*
425     * Calculate the size of the TCP/IP header and make sure that we don't
426     * overflow the space we have available for it.
427     */
428    hlen = ip->ip_hl << 2;
429    if (hlen + sizeof(struct tcphdr) > len)
430      goto bad;
431    th = (struct tcphdr *) & ((char *) ip)[hlen];
432    hlen += THOFFSET(th) << 2;
433    if (hlen > MAX_HDR)
434      goto bad;
435    memcpy(&cs->cs_ip, ip, hlen);
436    cs->cs_ip.ip_sum = 0;
437    cs->cs_hlen = hlen;
438    slstat->sls_uncompressedin++;
439    return (len);
440
441  default:
442    goto bad;
443
444  case TYPE_COMPRESSED_TCP:
445    break;
446  }
447  /* We've got a compressed packet. */
448  slstat->sls_compressedin++;
449  cp = *bufp;
450  changes = *cp++;
451  log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
452  if (changes & NEW_C) {
453
454    /*
455     * Make sure the state index is in range, then grab the state. If we have
456     * a good state index, clear the 'discard' flag.
457     */
458    if (*cp > max_state || comp->last_recv == 255) {
459      goto bad;
460    }
461
462    comp->flags &= ~SLF_TOSS;
463    comp->last_recv = *cp++;
464  } else {
465
466    /*
467     * this packet has an implicit state index.  If we've had a line error
468     * since the last time we got an explicit state index, we have to toss
469     * the packet.
470     */
471    if (comp->flags & SLF_TOSS) {
472      slstat->sls_tossed++;
473      return (0);
474    }
475  }
476  cs = &comp->rstate[comp->last_recv];
477  hlen = cs->cs_ip.ip_hl << 2;
478  if (hlen == 0)
479    goto bad;    /* We've been pointed at a not-yet-used slot ! */
480  th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
481  th->th_sum = htons((*cp << 8) | cp[1]);
482  cp += 2;
483  if (changes & TCP_PUSH_BIT)
484    th->th_flags |= TH_PUSH;
485  else
486    th->th_flags &= ~TH_PUSH;
487
488  switch (changes & SPECIALS_MASK) {
489  case SPECIAL_I:
490    {
491      register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
492
493      th->th_ack = htonl(ntohl(th->th_ack) + i);
494      th->th_seq = htonl(ntohl(th->th_seq) + i);
495    }
496    break;
497
498  case SPECIAL_D:
499    th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
500		       - cs->cs_hlen);
501    break;
502
503  default:
504    if (changes & NEW_U) {
505      th->th_flags |= TH_URG;
506      DECODEU(th->th_urp)
507    } else
508      th->th_flags &= ~TH_URG;
509    if (changes & NEW_W)
510      DECODES(th->th_win)
511	if (changes & NEW_A)
512	DECODEL(th->th_ack)
513	  if (changes & NEW_S) {
514	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
515		    *cp, cp[1], cp[2]);
516	  DECODEL(th->th_seq)
517	}
518    break;
519  }
520  if (changes & NEW_I) {
521    DECODES(cs->cs_ip.ip_id)
522  } else
523    cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
524
525  log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
526	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
527
528  /*
529   * At this point, cp points to the first byte of data in the packet.  If
530   * we're not aligned on a 4-byte boundary, copy the data down so the ip &
531   * tcp headers will be aligned.  Then back up cp by the tcp/ip header
532   * length to make room for the reconstructed header (we assume the packet
533   * we were handed has enough space to prepend 128 bytes of header).  Adjust
534   * the length to account for the new header & fill in the IP total length.
535   */
536  len -= (cp - *bufp);
537  if (len < 0)
538
539    /*
540     * we must have dropped some characters (crc should detect this but the
541     * old slip framing won't)
542     */
543    goto bad;
544
545#ifdef notdef
546  if ((int) cp & 3) {
547    if (len > 0)
548      (void) bcopy(cp, (caddr_t) ((int) cp & ~3), len);
549    cp = (u_char *) ((int) cp & ~3);
550  }
551#endif
552
553  cp -= cs->cs_hlen;
554  len += cs->cs_hlen;
555  cs->cs_ip.ip_len = htons(len);
556  memcpy(cp, &cs->cs_ip, cs->cs_hlen);
557  *bufp = cp;
558
559  /* recompute the ip header checksum */
560  {
561    register u_short *bp = (u_short *) cp;
562
563    for (changes = 0; hlen > 0; hlen -= 2)
564      changes += *bp++;
565    changes = (changes & 0xffff) + (changes >> 16);
566    changes = (changes & 0xffff) + (changes >> 16);
567    ((struct ip *) cp)->ip_sum = ~changes;
568  }
569  return (len);
570bad:
571  comp->flags |= SLF_TOSS;
572  slstat->sls_errorin++;
573  return (0);
574}
575
576int
577sl_Show(struct cmdargs const *arg)
578{
579  prompt_Printf(arg->prompt, "VJ compression statistics:\n");
580  prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
581	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
582                arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
583  prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
584	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
585                arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
586  prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
587	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
588                arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
589  prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
590	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
591                arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
592  return 0;
593}
594