16059Samurai/*
26059Samurai * Definitions for tcp compression routines.
36059Samurai *
4139979Sbrian * Copyright (c) 1989, 1993
5139979Sbrian *	The Regents of the University of California.  All rights reserved.
66059Samurai *
7139979Sbrian * Redistribution and use in source and binary forms, with or without
8139979Sbrian * modification, are permitted provided that the following conditions
9139979Sbrian * are met:
10139979Sbrian * 1. Redistributions of source code must retain the above copyright
11139979Sbrian *    notice, this list of conditions and the following disclaimer.
12139979Sbrian * 2. Redistributions in binary form must reproduce the above copyright
13139979Sbrian *    notice, this list of conditions and the following disclaimer in the
14139979Sbrian *    documentation and/or other materials provided with the distribution.
15139979Sbrian * 3. Neither the name of the University nor the names of its contributors
16139979Sbrian *    may be used to endorse or promote products derived from this software
17139979Sbrian *    without specific prior written permission.
188857Srgrimes *
19139979Sbrian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20139979Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21139979Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22139979Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23139979Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24139979Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25139979Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26139979Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27139979Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28139979Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29139979Sbrian * SUCH DAMAGE.
308857Srgrimes *
316059Samurai *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
326059Samurai *	- Initial distribution.
33139979Sbrian *
34139979Sbrian * $FreeBSD$
356059Samurai */
366059Samurai
3736285Sbrian#define MIN_VJ_STATES 3
3836285Sbrian#define MAX_VJ_STATES 255
3936285Sbrian#define DEF_VJ_STATES 16		/* must be > 2 and < 256 */
4036285Sbrian#define MAX_HDR 128
416059Samurai
426059Samurai/*
436059Samurai * Compressed packet format:
446059Samurai *
456059Samurai * The first octet contains the packet type (top 3 bits), TCP
466059Samurai * 'push' bit, and flags that indicate which of the 4 TCP sequence
476059Samurai * numbers have changed (bottom 5 bits).  The next octet is a
486059Samurai * conversation number that associates a saved IP/TCP header with
496059Samurai * the compressed packet.  The next two octets are the TCP checksum
506059Samurai * from the original datagram.  The next 0 to 15 octets are
516059Samurai * sequence number changes, one change per bit set in the header
526059Samurai * (there may be no changes and there are two special cases where
536059Samurai * the receiver implicitly knows what changed -- see below).
548857Srgrimes *
556059Samurai * There are 5 numbers which can change (they are always inserted
566059Samurai * in the following order): TCP urgent pointer, window,
576059Samurai * acknowlegement, sequence number and IP ID.  (The urgent pointer
586059Samurai * is different from the others in that its value is sent, not the
596059Samurai * change in value.)  Since typical use of SLIP links is biased
606059Samurai * toward small packets (see comments on MTU/MSS below), changes
616059Samurai * use a variable length coding with one octet for numbers in the
626059Samurai * range 1 - 255 and 3 octets (0, MSB, LSB) for numbers in the
636059Samurai * range 256 - 65535 or 0.  (If the change in sequence number or
646059Samurai * ack is more than 65535, an uncompressed packet is sent.)
656059Samurai */
666059Samurai
676059Samurai/*
686059Samurai * Packet types (must not conflict with IP protocol version)
696059Samurai *
706059Samurai * The top nibble of the first octet is the packet type.  There are
716059Samurai * three possible types: IP (not proto TCP or tcp with one of the
726059Samurai * control flags set); uncompressed TCP (a normal IP/TCP packet but
736059Samurai * with the 8-bit protocol field replaced by an 8-bit connection id --
746059Samurai * this type of packet syncs the sender & receiver); and compressed
756059Samurai * TCP (described above).
766059Samurai *
776059Samurai * LSB of 4-bit field is TCP "PUSH" bit (a worthless anachronism) and
786059Samurai * is logically part of the 4-bit "changes" field that follows.  Top
796059Samurai * three bits are actual packet type.  For backward compatibility
806059Samurai * and in the interest of conserving bits, numbers are chosen so the
816059Samurai * IP protocol version number (4) which normally appears in this nibble
826059Samurai * means "IP packet".
836059Samurai */
846059Samurai
856059Samurai/* packet types */
866059Samurai#define TYPE_IP 0x40
876059Samurai#define TYPE_UNCOMPRESSED_TCP 0x70
886059Samurai#define TYPE_COMPRESSED_TCP 0x80
896059Samurai#define TYPE_ERROR 0x00
906059Samurai
916059Samurai/* Bits in first octet of compressed packet */
9228679Sbrian#define NEW_C	0x40		/* flag bits for what changed in a packet */
936059Samurai#define NEW_I	0x20
946059Samurai#define NEW_S	0x08
956059Samurai#define NEW_A	0x04
966059Samurai#define NEW_W	0x02
976059Samurai#define NEW_U	0x01
986059Samurai
996059Samurai/* reserved, special-case values of above */
10028679Sbrian#define SPECIAL_I (NEW_S|NEW_W|NEW_U)	/* echoed interactive traffic */
1016059Samurai#define SPECIAL_D (NEW_S|NEW_A|NEW_W|NEW_U)	/* unidirectional data */
1026059Samurai#define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U)
1036059Samurai
1046059Samurai#define TCP_PUSH_BIT 0x10
1056059Samurai
1066059Samurai/*
1076059Samurai * "state" data for each active tcp conversation on the wire.  This is
1086059Samurai * basically a copy of the entire IP/TCP header from the last packet
1096059Samurai * we saw from the conversation together with a small identifier
1106059Samurai * the transmit & receive ends of the line use to locate saved header.
1116059Samurai */
1126059Samuraistruct cstate {
11328679Sbrian  struct cstate *cs_next;	/* next most recently used cstate (xmit only) */
11428679Sbrian  u_short cs_hlen;		/* size of hdr (receive only) */
11528679Sbrian  u_char cs_id;			/* connection # associated with this state */
11628679Sbrian  u_char cs_filler;
11759005Sgj  union {
11859005Sgj    char csu_hdr[MAX_HDR];
11959005Sgj    struct ip csu_ip;		/* ip/tcp hdr from most recent packet */
12059005Sgj  } slcs_u;
12159187Sbrian};
12228679Sbrian
12359005Sgj#define cs_ip slcs_u.csu_ip
12459005Sgj#define cs_hdr slcs_u.csu_hdr
12559005Sgj
1266059Samurai/*
1276059Samurai * all the state data for one serial line (we need one of these
1286059Samurai * per line).
1296059Samurai */
1306059Samuraistruct slcompress {
13128679Sbrian  struct cstate *last_cs;	/* most recently used tstate */
13228679Sbrian  u_char last_recv;		/* last rcvd conn. id */
13328679Sbrian  u_char last_xmit;		/* last sent conn. id */
13428679Sbrian  u_short flags;
13536285Sbrian  struct cstate tstate[MAX_VJ_STATES];	/* xmit connection states */
13636285Sbrian  struct cstate rstate[MAX_VJ_STATES];	/* receive connection states */
1376059Samurai};
1386059Samurai
13936285Sbrianstruct slstat {
14036285Sbrian  int sls_packets;		/* outbound packets */
14136285Sbrian  int sls_compressed;		/* outbound compressed packets */
14236285Sbrian  int sls_searches;		/* searches for connection state */
14336285Sbrian  int sls_misses;		/* times couldn't find conn. state */
14436285Sbrian  int sls_uncompressedin;	/* inbound uncompressed packets */
14536285Sbrian  int sls_compressedin;		/* inbound compressed packets */
14636285Sbrian  int sls_errorin;		/* inbound unknown type packets */
14736285Sbrian  int sls_tossed;		/* inbound packets tossed because of error */
14836285Sbrian};
14936285Sbrian
1506059Samurai/* flag values */
1516059Samurai#define SLF_TOSS 1		/* tossing rcvd frames because of input err */
1526059Samurai
15336285Sbrianstruct mbuf;
15436285Sbrianstruct cmdargs;
15536285Sbrian
15630715Sbrianextern void sl_compress_init(struct slcompress *, int);
15736285Sbrianextern u_char sl_compress_tcp(struct mbuf *, struct ip *, struct slcompress *,
15836285Sbrian                              struct slstat *, int);
15936285Sbrianextern int sl_uncompress_tcp(u_char **, int, u_int, struct slcompress *,
16036960Sbrian                             struct slstat *, int);
16136285Sbrianextern int sl_Show(struct cmdargs const *);
162