1316958Sdchagin/*
259243Sobrien * Definitions for tcp compression routines.
359243Sobrien *
459243Sobrien * Copyright (c) 1989, 1993
559243Sobrien *	The Regents of the University of California.  All rights reserved.
659243Sobrien *
759243Sobrien * Redistribution and use in source and binary forms, with or without
859243Sobrien * modification, are permitted provided that the following conditions
959243Sobrien * are met:
1059243Sobrien * 1. Redistributions of source code must retain the above copyright
1159243Sobrien *    notice, this list of conditions and the following disclaimer.
1259243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer in the
1459243Sobrien *    documentation and/or other materials provided with the distribution.
1559243Sobrien * 3. Neither the name of the University nor the names of its contributors
1659243Sobrien *    may be used to endorse or promote products derived from this software
17100616Smp *    without specific prior written permission.
1859243Sobrien *
1959243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2059243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2159243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2259243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2359243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2459243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2559243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2659243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2759243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2859243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2959243Sobrien * SUCH DAMAGE.
3059243Sobrien *
3159243Sobrien *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
3259243Sobrien *	- Initial distribution.
3359243Sobrien *
3459243Sobrien * $FreeBSD$
35316958Sdchagin */
3659243Sobrien
3759243Sobrien#define MIN_VJ_STATES 3
3859243Sobrien#define MAX_VJ_STATES 255
3969408Sache#define DEF_VJ_STATES 16		/* must be > 2 and < 256 */
4059243Sobrien#define MAX_HDR 128
4169408Sache
4259243Sobrien/*
4359243Sobrien * Compressed packet format:
4459243Sobrien *
4559243Sobrien * The first octet contains the packet type (top 3 bits), TCP
4659243Sobrien * 'push' bit, and flags that indicate which of the 4 TCP sequence
4759243Sobrien * numbers have changed (bottom 5 bits).  The next octet is a
4859243Sobrien * conversation number that associates a saved IP/TCP header with
4959243Sobrien * the compressed packet.  The next two octets are the TCP checksum
5059243Sobrien * from the original datagram.  The next 0 to 15 octets are
5159243Sobrien * sequence number changes, one change per bit set in the header
5259243Sobrien * (there may be no changes and there are two special cases where
5359243Sobrien * the receiver implicitly knows what changed -- see below).
5459243Sobrien *
5559243Sobrien * There are 5 numbers which can change (they are always inserted
5659243Sobrien * in the following order): TCP urgent pointer, window,
5759243Sobrien * acknowlegement, sequence number and IP ID.  (The urgent pointer
5859243Sobrien * is different from the others in that its value is sent, not the
5959243Sobrien * change in value.)  Since typical use of SLIP links is biased
6059243Sobrien * toward small packets (see comments on MTU/MSS below), changes
6159243Sobrien * use a variable length coding with one octet for numbers in the
6259243Sobrien * range 1 - 255 and 3 octets (0, MSB, LSB) for numbers in the
6359243Sobrien * range 256 - 65535 or 0.  (If the change in sequence number or
6459243Sobrien * ack is more than 65535, an uncompressed packet is sent.)
6559243Sobrien */
6659243Sobrien
6759243Sobrien/*
6859243Sobrien * Packet types (must not conflict with IP protocol version)
6959243Sobrien *
7059243Sobrien * The top nibble of the first octet is the packet type.  There are
7159243Sobrien * three possible types: IP (not proto TCP or tcp with one of the
7259243Sobrien * control flags set); uncompressed TCP (a normal IP/TCP packet but
7359243Sobrien * with the 8-bit protocol field replaced by an 8-bit connection id --
7459243Sobrien * this type of packet syncs the sender & receiver); and compressed
7559243Sobrien * TCP (described above).
7659243Sobrien *
7759243Sobrien * LSB of 4-bit field is TCP "PUSH" bit (a worthless anachronism) and
7859243Sobrien * is logically part of the 4-bit "changes" field that follows.  Top
7959243Sobrien * three bits are actual packet type.  For backward compatibility
80231990Smp * and in the interest of conserving bits, numbers are chosen so the
8159243Sobrien * IP protocol version number (4) which normally appears in this nibble
8259243Sobrien * means "IP packet".
8359243Sobrien */
8459243Sobrien
8559243Sobrien/* packet types */
8659243Sobrien#define TYPE_IP 0x40
8759243Sobrien#define TYPE_UNCOMPRESSED_TCP 0x70
8859243Sobrien#define TYPE_COMPRESSED_TCP 0x80
8959243Sobrien#define TYPE_ERROR 0x00
9059243Sobrien
9159243Sobrien/* Bits in first octet of compressed packet */
9259243Sobrien#define NEW_C	0x40		/* flag bits for what changed in a packet */
9359243Sobrien#define NEW_I	0x20
9459243Sobrien#define NEW_S	0x08
9559243Sobrien#define NEW_A	0x04
96145479Smp#define NEW_W	0x02
9759243Sobrien#define NEW_U	0x01
98145479Smp
9959243Sobrien/* reserved, special-case values of above */
10059243Sobrien#define SPECIAL_I (NEW_S|NEW_W|NEW_U)	/* echoed interactive traffic */
10159243Sobrien#define SPECIAL_D (NEW_S|NEW_A|NEW_W|NEW_U)	/* unidirectional data */
10259243Sobrien#define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U)
10359243Sobrien
10459243Sobrien#define TCP_PUSH_BIT 0x10
10559243Sobrien
10659243Sobrien/*
10759243Sobrien * "state" data for each active tcp conversation on the wire.  This is
10859243Sobrien * basically a copy of the entire IP/TCP header from the last packet
10959243Sobrien * we saw from the conversation together with a small identifier
11059243Sobrien * the transmit & receive ends of the line use to locate saved header.
11159243Sobrien */
11259243Sobrienstruct cstate {
11359243Sobrien  struct cstate *cs_next;	/* next most recently used cstate (xmit only) */
11459243Sobrien  u_short cs_hlen;		/* size of hdr (receive only) */
11559243Sobrien  u_char cs_id;			/* connection # associated with this state */
11659243Sobrien  u_char cs_filler;
11759243Sobrien  union {
11859243Sobrien    char csu_hdr[MAX_HDR];
11959243Sobrien    struct ip csu_ip;		/* ip/tcp hdr from most recent packet */
12059243Sobrien  } slcs_u;
12159243Sobrien};
12259243Sobrien
12359243Sobrien#define cs_ip slcs_u.csu_ip
12459243Sobrien#define cs_hdr slcs_u.csu_hdr
12559243Sobrien
12659243Sobrien/*
12759243Sobrien * all the state data for one serial line (we need one of these
12859243Sobrien * per line).
12959243Sobrien */
13059243Sobrienstruct slcompress {
13159243Sobrien  struct cstate *last_cs;	/* most recently used tstate */
13259243Sobrien  u_char last_recv;		/* last rcvd conn. id */
13359243Sobrien  u_char last_xmit;		/* last sent conn. id */
13459243Sobrien  u_short flags;
13559243Sobrien  struct cstate tstate[MAX_VJ_STATES];	/* xmit connection states */
13659243Sobrien  struct cstate rstate[MAX_VJ_STATES];	/* receive connection states */
13759243Sobrien};
13859243Sobrien
13959243Sobrienstruct slstat {
14059243Sobrien  int sls_packets;		/* outbound packets */
14159243Sobrien  int sls_compressed;		/* outbound compressed packets */
14259243Sobrien  int sls_searches;		/* searches for connection state */
14359243Sobrien  int sls_misses;		/* times couldn't find conn. state */
144231990Smp  int sls_uncompressedin;	/* inbound uncompressed packets */
145167465Smp  int sls_compressedin;		/* inbound compressed packets */
146167465Smp  int sls_errorin;		/* inbound unknown type packets */
147167465Smp  int sls_tossed;		/* inbound packets tossed because of error */
14859243Sobrien};
14959243Sobrien
150167465Smp/* flag values */
15159243Sobrien#define SLF_TOSS 1		/* tossing rcvd frames because of input err */
152231990Smp
153100616Smpstruct mbuf;
154231990Smpstruct cmdargs;
155100616Smp
15659243Sobrienextern void sl_compress_init(struct slcompress *, int);
15759243Sobrienextern u_char sl_compress_tcp(struct mbuf *, struct ip *, struct slcompress *,
15859243Sobrien                              struct slstat *, int);
15959243Sobrienextern int sl_uncompress_tcp(u_char **, int, u_int, struct slcompress *,
16059243Sobrien                             struct slstat *, int);
16159243Sobrienextern int sl_Show(struct cmdargs const *);
16259243Sobrien