xref: /freebsd/usr.sbin/ppp/slcompress.h (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * Definitions for tcp compression routines.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
34  *	- Initial distribution.
35  */
36 
37 #define MIN_VJ_STATES 3
38 #define MAX_VJ_STATES 255
39 #define DEF_VJ_STATES 16		/* must be > 2 and < 256 */
40 #define MAX_HDR 128
41 
42 /*
43  * Compressed packet format:
44  *
45  * The first octet contains the packet type (top 3 bits), TCP
46  * 'push' bit, and flags that indicate which of the 4 TCP sequence
47  * numbers have changed (bottom 5 bits).  The next octet is a
48  * conversation number that associates a saved IP/TCP header with
49  * the compressed packet.  The next two octets are the TCP checksum
50  * from the original datagram.  The next 0 to 15 octets are
51  * sequence number changes, one change per bit set in the header
52  * (there may be no changes and there are two special cases where
53  * the receiver implicitly knows what changed -- see below).
54  *
55  * There are 5 numbers which can change (they are always inserted
56  * in the following order): TCP urgent pointer, window,
57  * acknowlegement, sequence number and IP ID.  (The urgent pointer
58  * is different from the others in that its value is sent, not the
59  * change in value.)  Since typical use of SLIP links is biased
60  * toward small packets (see comments on MTU/MSS below), changes
61  * use a variable length coding with one octet for numbers in the
62  * range 1 - 255 and 3 octets (0, MSB, LSB) for numbers in the
63  * range 256 - 65535 or 0.  (If the change in sequence number or
64  * ack is more than 65535, an uncompressed packet is sent.)
65  */
66 
67 /*
68  * Packet types (must not conflict with IP protocol version)
69  *
70  * The top nibble of the first octet is the packet type.  There are
71  * three possible types: IP (not proto TCP or tcp with one of the
72  * control flags set); uncompressed TCP (a normal IP/TCP packet but
73  * with the 8-bit protocol field replaced by an 8-bit connection id --
74  * this type of packet syncs the sender & receiver); and compressed
75  * TCP (described above).
76  *
77  * LSB of 4-bit field is TCP "PUSH" bit (a worthless anachronism) and
78  * is logically part of the 4-bit "changes" field that follows.  Top
79  * three bits are actual packet type.  For backward compatibility
80  * and in the interest of conserving bits, numbers are chosen so the
81  * IP protocol version number (4) which normally appears in this nibble
82  * means "IP packet".
83  */
84 
85 /* packet types */
86 #define TYPE_IP 0x40
87 #define TYPE_UNCOMPRESSED_TCP 0x70
88 #define TYPE_COMPRESSED_TCP 0x80
89 #define TYPE_ERROR 0x00
90 
91 /* Bits in first octet of compressed packet */
92 #define NEW_C	0x40		/* flag bits for what changed in a packet */
93 #define NEW_I	0x20
94 #define NEW_S	0x08
95 #define NEW_A	0x04
96 #define NEW_W	0x02
97 #define NEW_U	0x01
98 
99 /* reserved, special-case values of above */
100 #define SPECIAL_I (NEW_S|NEW_W|NEW_U)	/* echoed interactive traffic */
101 #define SPECIAL_D (NEW_S|NEW_A|NEW_W|NEW_U)	/* unidirectional data */
102 #define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U)
103 
104 #define TCP_PUSH_BIT 0x10
105 
106 /*
107  * "state" data for each active tcp conversation on the wire.  This is
108  * basically a copy of the entire IP/TCP header from the last packet
109  * we saw from the conversation together with a small identifier
110  * the transmit & receive ends of the line use to locate saved header.
111  */
112 struct cstate {
113   struct cstate *cs_next;	/* next most recently used cstate (xmit only) */
114   u_short cs_hlen;		/* size of hdr (receive only) */
115   u_char cs_id;			/* connection # associated with this state */
116   u_char cs_filler;
117   union {
118     char csu_hdr[MAX_HDR];
119     struct ip csu_ip;		/* ip/tcp hdr from most recent packet */
120   } slcs_u;
121 };
122 
123 #define cs_ip slcs_u.csu_ip
124 #define cs_hdr slcs_u.csu_hdr
125 
126 /*
127  * all the state data for one serial line (we need one of these
128  * per line).
129  */
130 struct slcompress {
131   struct cstate *last_cs;	/* most recently used tstate */
132   u_char last_recv;		/* last rcvd conn. id */
133   u_char last_xmit;		/* last sent conn. id */
134   u_short flags;
135   struct cstate tstate[MAX_VJ_STATES];	/* xmit connection states */
136   struct cstate rstate[MAX_VJ_STATES];	/* receive connection states */
137 };
138 
139 struct slstat {
140   int sls_packets;		/* outbound packets */
141   int sls_compressed;		/* outbound compressed packets */
142   int sls_searches;		/* searches for connection state */
143   int sls_misses;		/* times couldn't find conn. state */
144   int sls_uncompressedin;	/* inbound uncompressed packets */
145   int sls_compressedin;		/* inbound compressed packets */
146   int sls_errorin;		/* inbound unknown type packets */
147   int sls_tossed;		/* inbound packets tossed because of error */
148 };
149 
150 /* flag values */
151 #define SLF_TOSS 1		/* tossing rcvd frames because of input err */
152 
153 struct mbuf;
154 struct cmdargs;
155 
156 extern void sl_compress_init(struct slcompress *, int);
157 extern u_char sl_compress_tcp(struct mbuf *, struct ip *, struct slcompress *,
158                               struct slstat *, int);
159 extern int sl_uncompress_tcp(u_char **, int, u_int, struct slcompress *,
160                              struct slstat *, int);
161 extern int sl_Show(struct cmdargs const *);
162