xref: /freebsd/usr.sbin/ppp/slcompress.c (revision 0fc7bdc978366abb4351b0b76b50a5848cc5d982)
1ec91ed91SBrian Somers /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4ec91ed91SBrian Somers  * Copyright (c) 1989, 1993, 1994
5ec91ed91SBrian Somers  *	The Regents of the University of California.  All rights reserved.
6ec91ed91SBrian Somers  *
7ec91ed91SBrian Somers  * Redistribution and use in source and binary forms, with or without
8ec91ed91SBrian Somers  * modification, are permitted provided that the following conditions
9ec91ed91SBrian Somers  * are met:
10ec91ed91SBrian Somers  * 1. Redistributions of source code must retain the above copyright
11ec91ed91SBrian Somers  *    notice, this list of conditions and the following disclaimer.
12ec91ed91SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
13ec91ed91SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
14ec91ed91SBrian Somers  *    documentation and/or other materials provided with the distribution.
15ec91ed91SBrian Somers  * 3. Neither the name of the University nor the names of its contributors
16ec91ed91SBrian Somers  *    may be used to endorse or promote products derived from this software
17ec91ed91SBrian Somers  *    without specific prior written permission.
18ec91ed91SBrian Somers  *
19ec91ed91SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ec91ed91SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ec91ed91SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ec91ed91SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ec91ed91SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ec91ed91SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ec91ed91SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ec91ed91SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ec91ed91SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ec91ed91SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ec91ed91SBrian Somers  * SUCH DAMAGE.
30ec91ed91SBrian Somers  */
31ec91ed91SBrian Somers 
32af57ed9fSAtsushi Murai /*
33af57ed9fSAtsushi Murai  * Routines to compress and uncompess tcp packets (for transmission
34af57ed9fSAtsushi Murai  * over low speed serial lines.
35af57ed9fSAtsushi Murai  *
36af57ed9fSAtsushi Murai  * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
37af57ed9fSAtsushi Murai  *	- Initial distribution.
38af57ed9fSAtsushi Murai  */
39944f7098SBrian Somers 
40972a1bcfSBrian Somers #include <sys/param.h>
41af57ed9fSAtsushi Murai #include <netinet/in_systm.h>
42af57ed9fSAtsushi Murai #include <netinet/in.h>
43af57ed9fSAtsushi Murai #include <netinet/tcp.h>
44af57ed9fSAtsushi Murai #include <netinet/ip.h>
4530949fd4SBrian Somers #include <sys/socket.h>
461fa665f5SBrian Somers #include <sys/un.h>
4775240ed1SBrian Somers 
486eafd353SBrian Somers #include <stdarg.h>
4975240ed1SBrian Somers #include <stdio.h>
5075240ed1SBrian Somers #include <string.h>
5185b542cfSBrian Somers #include <termios.h>
5275240ed1SBrian Somers 
535d9e6103SBrian Somers #include "layer.h"
54c9e11a11SBrian Somers #include "defs.h"
55b6e82f33SBrian Somers #include "command.h"
5675240ed1SBrian Somers #include "mbuf.h"
5775240ed1SBrian Somers #include "log.h"
58af57ed9fSAtsushi Murai #include "slcompress.h"
5985b542cfSBrian Somers #include "descriptor.h"
6085b542cfSBrian Somers #include "prompt.h"
61eaa4df37SBrian Somers #include "timer.h"
62eaa4df37SBrian Somers #include "fsm.h"
63eaa4df37SBrian Somers #include "throughput.h"
64eaa4df37SBrian Somers #include "iplist.h"
653b0f8d2eSBrian Somers #include "lqr.h"
663b0f8d2eSBrian Somers #include "hdlc.h"
6730949fd4SBrian Somers #include "ncpaddr.h"
685a72b6edSBrian Somers #include "ipcp.h"
695a72b6edSBrian Somers #include "filter.h"
703b0f8d2eSBrian Somers #include "lcp.h"
713b0f8d2eSBrian Somers #include "ccp.h"
723b0f8d2eSBrian Somers #include "link.h"
733b0f8d2eSBrian Somers #include "mp.h"
74972a1bcfSBrian Somers #ifndef NORADIUS
75972a1bcfSBrian Somers #include "radius.h"
76972a1bcfSBrian Somers #endif
7730949fd4SBrian Somers #include "ipv6cp.h"
7830949fd4SBrian Somers #include "ncp.h"
79eaa4df37SBrian Somers #include "bundle.h"
80af57ed9fSAtsushi Murai 
81af57ed9fSAtsushi Murai void
sl_compress_init(struct slcompress * comp,int max_state)8203604f35SBrian Somers sl_compress_init(struct slcompress *comp, int max_state)
83af57ed9fSAtsushi Murai {
84af57ed9fSAtsushi Murai   register u_int i;
85af57ed9fSAtsushi Murai   register struct cstate *tstate = comp->tstate;
86af57ed9fSAtsushi Murai 
8770ee81ffSBrian Somers   memset(comp, '\0', sizeof *comp);
8803604f35SBrian Somers   for (i = max_state; i > 0; --i) {
89af57ed9fSAtsushi Murai     tstate[i].cs_id = i;
90af57ed9fSAtsushi Murai     tstate[i].cs_next = &tstate[i - 1];
91af57ed9fSAtsushi Murai   }
9203604f35SBrian Somers   tstate[0].cs_next = &tstate[max_state];
93af57ed9fSAtsushi Murai   tstate[0].cs_id = 0;
94af57ed9fSAtsushi Murai   comp->last_cs = &tstate[0];
95af57ed9fSAtsushi Murai   comp->last_recv = 255;
96af57ed9fSAtsushi Murai   comp->last_xmit = 255;
97af57ed9fSAtsushi Murai   comp->flags = SLF_TOSS;
98af57ed9fSAtsushi Murai }
99af57ed9fSAtsushi Murai 
100af57ed9fSAtsushi Murai 
101af57ed9fSAtsushi Murai /* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
1023f06c599SBrian Somers  * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
103af57ed9fSAtsushi Murai  * form).
104af57ed9fSAtsushi Murai  */
105af57ed9fSAtsushi Murai #define ENCODE(n) { \
106af57ed9fSAtsushi Murai 	if ((u_short)(n) >= 256) { \
107af57ed9fSAtsushi Murai 		*cp++ = 0; \
108af57ed9fSAtsushi Murai 		cp[1] = (n); \
109af57ed9fSAtsushi Murai 		cp[0] = (n) >> 8; \
110af57ed9fSAtsushi Murai 		cp += 2; \
111af57ed9fSAtsushi Murai 	} else { \
112af57ed9fSAtsushi Murai 		*cp++ = (n); \
113af57ed9fSAtsushi Murai 	} \
114af57ed9fSAtsushi Murai }
115af57ed9fSAtsushi Murai #define ENCODEZ(n) { \
116af57ed9fSAtsushi Murai 	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
117af57ed9fSAtsushi Murai 		*cp++ = 0; \
118af57ed9fSAtsushi Murai 		cp[1] = (n); \
119af57ed9fSAtsushi Murai 		cp[0] = (n) >> 8; \
120af57ed9fSAtsushi Murai 		cp += 2; \
121af57ed9fSAtsushi Murai 	} else { \
122af57ed9fSAtsushi Murai 		*cp++ = (n); \
123af57ed9fSAtsushi Murai 	} \
124af57ed9fSAtsushi Murai }
125af57ed9fSAtsushi Murai 
126af57ed9fSAtsushi Murai #define DECODEL(f) { \
127af57ed9fSAtsushi Murai 	if (*cp == 0) {\
128af57ed9fSAtsushi Murai 		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
129af57ed9fSAtsushi Murai 		cp += 3; \
130af57ed9fSAtsushi Murai 	} else { \
1313f06c599SBrian Somers 		(f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
132af57ed9fSAtsushi Murai 	} \
133af57ed9fSAtsushi Murai }
134af57ed9fSAtsushi Murai 
135af57ed9fSAtsushi Murai #define DECODES(f) { \
136af57ed9fSAtsushi Murai 	if (*cp == 0) {\
137af57ed9fSAtsushi Murai 		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
138af57ed9fSAtsushi Murai 		cp += 3; \
139af57ed9fSAtsushi Murai 	} else { \
1403f06c599SBrian Somers 		(f) = htons(ntohs(f) + (u_int32_t)*cp++); \
141af57ed9fSAtsushi Murai 	} \
142af57ed9fSAtsushi Murai }
143af57ed9fSAtsushi Murai 
144af57ed9fSAtsushi Murai #define DECODEU(f) { \
145af57ed9fSAtsushi Murai 	if (*cp == 0) {\
146af57ed9fSAtsushi Murai 		(f) = htons((cp[1] << 8) | cp[2]); \
147af57ed9fSAtsushi Murai 		cp += 3; \
148af57ed9fSAtsushi Murai 	} else { \
1493f06c599SBrian Somers 		(f) = htons((u_int32_t)*cp++); \
150af57ed9fSAtsushi Murai 	} \
151af57ed9fSAtsushi Murai }
152af57ed9fSAtsushi Murai 
153af57ed9fSAtsushi Murai 
154af57ed9fSAtsushi Murai u_char
sl_compress_tcp(struct mbuf * m,struct ip * ip,struct slcompress * comp,struct slstat * slstat,int compress_cid)155944f7098SBrian Somers sl_compress_tcp(struct mbuf * m,
156944f7098SBrian Somers 		struct ip * ip,
157944f7098SBrian Somers 		struct slcompress *comp,
158eaa4df37SBrian Somers                 struct slstat *slstat,
159944f7098SBrian Somers 		int compress_cid)
160af57ed9fSAtsushi Murai {
161af57ed9fSAtsushi Murai   register struct cstate *cs = comp->last_cs->cs_next;
162af57ed9fSAtsushi Murai   register u_int hlen = ip->ip_hl;
163af57ed9fSAtsushi Murai   register struct tcphdr *oth;
164af57ed9fSAtsushi Murai   register struct tcphdr *th;
165af57ed9fSAtsushi Murai   register u_int deltaS, deltaA;
166af57ed9fSAtsushi Murai   register u_int changes = 0;
167af57ed9fSAtsushi Murai   u_char new_seq[16];
168af57ed9fSAtsushi Murai   register u_char *cp = new_seq;
169af57ed9fSAtsushi Murai 
170af57ed9fSAtsushi Murai   /*
171944f7098SBrian Somers    * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
172944f7098SBrian Somers    * (i.e., ACK isn't set or some other control bit is set).  (We assume that
173944f7098SBrian Somers    * the caller has already made sure the packet is IP proto TCP).
174af57ed9fSAtsushi Murai    */
17526af0ae9SBrian Somers   if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
176209dc102SBrian Somers     log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
177209dc102SBrian Somers 	      ip->ip_off, (unsigned long)m->m_len);
178dd7e2610SBrian Somers     log_DumpBp(LogDEBUG, "", m);
179af57ed9fSAtsushi Murai     return (TYPE_IP);
180af57ed9fSAtsushi Murai   }
181af57ed9fSAtsushi Murai   th = (struct tcphdr *) & ((int *) ip)[hlen];
182*0fc7bdc9SRichard Scheffenegger   if ((__tcp_get_flags(th) & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
183*0fc7bdc9SRichard Scheffenegger     log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", __tcp_get_flags(th));
184dd7e2610SBrian Somers     log_DumpBp(LogDEBUG, "", m);
185af57ed9fSAtsushi Murai     return (TYPE_IP);
186af57ed9fSAtsushi Murai   }
187af57ed9fSAtsushi Murai 
188af57ed9fSAtsushi Murai   /*
189944f7098SBrian Somers    * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
190944f7098SBrian Somers    * UNCOMPRESSED_TCP packet.  Either way we need to locate (or create) the
191944f7098SBrian Somers    * connection state.  Special case the most recently used connection since
192944f7098SBrian Somers    * it's most likely to be used again & we don't have to do any reordering
193944f7098SBrian Somers    * if it's used.
194af57ed9fSAtsushi Murai    */
195eaa4df37SBrian Somers   slstat->sls_packets++;
196af57ed9fSAtsushi Murai   if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
197af57ed9fSAtsushi Murai       ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
198af57ed9fSAtsushi Murai       *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
199944f7098SBrian Somers 
200af57ed9fSAtsushi Murai     /*
201af57ed9fSAtsushi Murai      * Wasn't the first -- search for it.
202af57ed9fSAtsushi Murai      *
203944f7098SBrian Somers      * States are kept in a circularly linked list with last_cs pointing to the
204944f7098SBrian Somers      * end of the list.  The list is kept in lru order by moving a state to
205944f7098SBrian Somers      * the head of the list whenever it is referenced.  Since the list is
206944f7098SBrian Somers      * short and, empirically, the connection we want is almost always near
207944f7098SBrian Somers      * the front, we locate states via linear search.  If we don't find a
208944f7098SBrian Somers      * state for the datagram, the oldest state is (re-)used.
209af57ed9fSAtsushi Murai      */
210af57ed9fSAtsushi Murai     register struct cstate *lcs;
211af57ed9fSAtsushi Murai     register struct cstate *lastcs = comp->last_cs;
212af57ed9fSAtsushi Murai 
213af57ed9fSAtsushi Murai     do {
214944f7098SBrian Somers       lcs = cs;
215944f7098SBrian Somers       cs = cs->cs_next;
216eaa4df37SBrian Somers       slstat->sls_searches++;
217af57ed9fSAtsushi Murai       if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
218af57ed9fSAtsushi Murai 	  && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
219af57ed9fSAtsushi Murai 	  && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
220af57ed9fSAtsushi Murai 	goto found;
221af57ed9fSAtsushi Murai     } while (cs != lastcs);
222af57ed9fSAtsushi Murai 
223af57ed9fSAtsushi Murai     /*
224944f7098SBrian Somers      * Didn't find it -- re-use oldest cstate.  Send an uncompressed packet
225944f7098SBrian Somers      * that tells the other side what connection number we're using for this
226944f7098SBrian Somers      * conversation. Note that since the state list is circular, the oldest
227944f7098SBrian Somers      * state points to the newest and we only need to set last_cs to update
228944f7098SBrian Somers      * the lru linkage.
229af57ed9fSAtsushi Murai      */
230eaa4df37SBrian Somers     slstat->sls_misses++;
231af57ed9fSAtsushi Murai       comp->last_cs = lcs;
232af57ed9fSAtsushi Murai #define	THOFFSET(th)	(th->th_off)
233af57ed9fSAtsushi Murai     hlen += th->th_off;
234af57ed9fSAtsushi Murai     hlen <<= 2;
23526af0ae9SBrian Somers     if (hlen > m->m_len)
236af57ed9fSAtsushi Murai       return (TYPE_IP);
237af57ed9fSAtsushi Murai     goto uncompressed;
238af57ed9fSAtsushi Murai 
239af57ed9fSAtsushi Murai found:
240944f7098SBrian Somers 
241af57ed9fSAtsushi Murai     /*
242af57ed9fSAtsushi Murai      * Found it -- move to the front on the connection list.
243af57ed9fSAtsushi Murai      */
244af57ed9fSAtsushi Murai     if (cs == lastcs)
245af57ed9fSAtsushi Murai       comp->last_cs = lcs;
246af57ed9fSAtsushi Murai     else {
247af57ed9fSAtsushi Murai       lcs->cs_next = cs->cs_next;
248af57ed9fSAtsushi Murai       cs->cs_next = lastcs->cs_next;
249af57ed9fSAtsushi Murai       lastcs->cs_next = cs;
250af57ed9fSAtsushi Murai     }
251af57ed9fSAtsushi Murai   }
252af57ed9fSAtsushi Murai 
253af57ed9fSAtsushi Murai   /*
254944f7098SBrian Somers    * Make sure that only what we expect to change changed. The first line of
255944f7098SBrian Somers    * the `if' checks the IP protocol version, header length & type of
256944f7098SBrian Somers    * service.  The 2nd line checks the "Don't fragment" bit. The 3rd line
257944f7098SBrian Somers    * checks the time-to-live and protocol (the protocol check is unnecessary
258944f7098SBrian Somers    * but costless).  The 4th line checks the TCP header length.  The 5th line
259944f7098SBrian Somers    * checks IP options, if any.  The 6th line checks TCP options, if any.  If
260944f7098SBrian Somers    * any of these things are different between the previous & current
261944f7098SBrian Somers    * datagram, we send the current datagram `uncompressed'.
262af57ed9fSAtsushi Murai    */
263af57ed9fSAtsushi Murai   oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
264af57ed9fSAtsushi Murai   deltaS = hlen;
265af57ed9fSAtsushi Murai   hlen += th->th_off;
266af57ed9fSAtsushi Murai   hlen <<= 2;
26726af0ae9SBrian Somers   if (hlen > m->m_len)
268af57ed9fSAtsushi Murai     return (TYPE_IP);
269af57ed9fSAtsushi Murai 
270af57ed9fSAtsushi Murai   if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
271af57ed9fSAtsushi Murai       ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
272af57ed9fSAtsushi Murai       ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
273af57ed9fSAtsushi Murai       THOFFSET(th) != THOFFSET(oth) ||
274af57ed9fSAtsushi Murai       (deltaS > 5 &&
27575240ed1SBrian Somers        memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
276af57ed9fSAtsushi Murai       (THOFFSET(th) > 5 &&
27775240ed1SBrian Somers        memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
278af57ed9fSAtsushi Murai     goto uncompressed;
279af57ed9fSAtsushi Murai   }
280af57ed9fSAtsushi Murai 
281af57ed9fSAtsushi Murai   /*
282944f7098SBrian Somers    * Figure out which of the changing fields changed.  The receiver expects
283944f7098SBrian Somers    * changes in the order: urgent, window, ack, seq (the order minimizes the
284944f7098SBrian Somers    * number of temporaries needed in this section of code).
285af57ed9fSAtsushi Murai    */
286*0fc7bdc9SRichard Scheffenegger   if (__tcp_get_flags(th) & TH_URG) {
287af57ed9fSAtsushi Murai     deltaS = ntohs(th->th_urp);
288af57ed9fSAtsushi Murai     ENCODEZ(deltaS);
289af57ed9fSAtsushi Murai     changes |= NEW_U;
290af57ed9fSAtsushi Murai   } else if (th->th_urp != oth->th_urp) {
291944f7098SBrian Somers 
292944f7098SBrian Somers     /*
293944f7098SBrian Somers      * argh! URG not set but urp changed -- a sensible implementation should
294944f7098SBrian Somers      * never do this but RFC793 doesn't prohibit the change so we have to
295944f7098SBrian Somers      * deal with it.
296944f7098SBrian Somers      */
297af57ed9fSAtsushi Murai     goto uncompressed;
298af57ed9fSAtsushi Murai   }
299af57ed9fSAtsushi Murai   deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
300af57ed9fSAtsushi Murai   if (deltaS) {
301af57ed9fSAtsushi Murai     ENCODE(deltaS);
302af57ed9fSAtsushi Murai     changes |= NEW_W;
303af57ed9fSAtsushi Murai   }
304af57ed9fSAtsushi Murai   deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
305af57ed9fSAtsushi Murai   if (deltaA) {
306af57ed9fSAtsushi Murai     if (deltaA > 0xffff) {
307af57ed9fSAtsushi Murai       goto uncompressed;
308af57ed9fSAtsushi Murai     }
309af57ed9fSAtsushi Murai     ENCODE(deltaA);
310af57ed9fSAtsushi Murai     changes |= NEW_A;
311af57ed9fSAtsushi Murai   }
312af57ed9fSAtsushi Murai   deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
313af57ed9fSAtsushi Murai   if (deltaS) {
314af57ed9fSAtsushi Murai     if (deltaS > 0xffff) {
315af57ed9fSAtsushi Murai       goto uncompressed;
316af57ed9fSAtsushi Murai     }
317af57ed9fSAtsushi Murai     ENCODE(deltaS);
318af57ed9fSAtsushi Murai     changes |= NEW_S;
319af57ed9fSAtsushi Murai   }
320af57ed9fSAtsushi Murai   switch (changes) {
321af57ed9fSAtsushi Murai 
322af57ed9fSAtsushi Murai   case 0:
323944f7098SBrian Somers 
324af57ed9fSAtsushi Murai     /*
325944f7098SBrian Somers      * Nothing changed. If this packet contains data and the last one didn't,
326944f7098SBrian Somers      * this is probably a data packet following an ack (normal on an
327944f7098SBrian Somers      * interactive connection) and we send it compressed.  Otherwise it's
328944f7098SBrian Somers      * probably a retransmit, retransmitted ack or window probe.  Send it
329944f7098SBrian Somers      * uncompressed in case the other side missed the compressed version.
330af57ed9fSAtsushi Murai      */
331af57ed9fSAtsushi Murai     if (ip->ip_len != cs->cs_ip.ip_len &&
332af57ed9fSAtsushi Murai 	ntohs(cs->cs_ip.ip_len) == hlen)
333af57ed9fSAtsushi Murai       break;
334af57ed9fSAtsushi Murai 
335f0067240SPhilippe Charnier     /* FALLTHROUGH */
336af57ed9fSAtsushi Murai 
337af57ed9fSAtsushi Murai   case SPECIAL_I:
338af57ed9fSAtsushi Murai   case SPECIAL_D:
339944f7098SBrian Somers 
340af57ed9fSAtsushi Murai     /*
341944f7098SBrian Somers      * actual changes match one of our special case encodings -- send packet
342944f7098SBrian Somers      * uncompressed.
343af57ed9fSAtsushi Murai      */
344af57ed9fSAtsushi Murai     goto uncompressed;
345af57ed9fSAtsushi Murai 
346af57ed9fSAtsushi Murai   case NEW_S | NEW_A:
347af57ed9fSAtsushi Murai     if (deltaS == deltaA &&
348af57ed9fSAtsushi Murai 	deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
349af57ed9fSAtsushi Murai       /* special case for echoed terminal traffic */
350af57ed9fSAtsushi Murai       changes = SPECIAL_I;
351af57ed9fSAtsushi Murai       cp = new_seq;
352af57ed9fSAtsushi Murai     }
353af57ed9fSAtsushi Murai     break;
354af57ed9fSAtsushi Murai 
355af57ed9fSAtsushi Murai   case NEW_S:
356af57ed9fSAtsushi Murai     if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
357af57ed9fSAtsushi Murai       /* special case for data xfer */
358af57ed9fSAtsushi Murai       changes = SPECIAL_D;
359af57ed9fSAtsushi Murai       cp = new_seq;
360af57ed9fSAtsushi Murai     }
361af57ed9fSAtsushi Murai     break;
362af57ed9fSAtsushi Murai   }
363af57ed9fSAtsushi Murai 
364af57ed9fSAtsushi Murai   deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
365af57ed9fSAtsushi Murai   if (deltaS != 1) {
366af57ed9fSAtsushi Murai     ENCODEZ(deltaS);
367af57ed9fSAtsushi Murai     changes |= NEW_I;
368af57ed9fSAtsushi Murai   }
369*0fc7bdc9SRichard Scheffenegger   if (__tcp_get_flags(th) & TH_PUSH)
370af57ed9fSAtsushi Murai     changes |= TCP_PUSH_BIT;
371944f7098SBrian Somers 
372af57ed9fSAtsushi Murai   /*
373944f7098SBrian Somers    * Grab the cksum before we overwrite it below.  Then update our state with
374944f7098SBrian Somers    * this packet's header.
375af57ed9fSAtsushi Murai    */
376af57ed9fSAtsushi Murai   deltaA = ntohs(th->th_sum);
37775240ed1SBrian Somers   memcpy(&cs->cs_ip, ip, hlen);
378af57ed9fSAtsushi Murai 
379af57ed9fSAtsushi Murai   /*
380944f7098SBrian Somers    * We want to use the original packet as our compressed packet. (cp -
381944f7098SBrian Somers    * new_seq) is the number of bytes we need for compressed sequence numbers.
382944f7098SBrian Somers    * In addition we need one byte for the change mask, one for the connection
383944f7098SBrian Somers    * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
384944f7098SBrian Somers    * are needed.  hlen is how many bytes of the original packet to toss so
385944f7098SBrian Somers    * subtract the two to get the new packet size.
386af57ed9fSAtsushi Murai    */
387af57ed9fSAtsushi Murai   deltaS = cp - new_seq;
388af57ed9fSAtsushi Murai   cp = (u_char *) ip;
389af57ed9fSAtsushi Murai 
390af57ed9fSAtsushi Murai   /*
391944f7098SBrian Somers    * Since fastq traffic can jump ahead of the background traffic, we don't
392944f7098SBrian Somers    * know what order packets will go on the line.  In this case, we always
393944f7098SBrian Somers    * send a "new" connection id so the receiver state stays synchronized.
394af57ed9fSAtsushi Murai    */
39503604f35SBrian Somers   if (comp->last_xmit == cs->cs_id && compress_cid) {
396af57ed9fSAtsushi Murai     hlen -= deltaS + 3;
397af57ed9fSAtsushi Murai     cp += hlen;
398af57ed9fSAtsushi Murai     *cp++ = changes;
39903604f35SBrian Somers   } else {
400af57ed9fSAtsushi Murai     comp->last_xmit = cs->cs_id;
401af57ed9fSAtsushi Murai     hlen -= deltaS + 4;
402af57ed9fSAtsushi Murai     cp += hlen;
403af57ed9fSAtsushi Murai     *cp++ = changes | NEW_C;
404af57ed9fSAtsushi Murai     *cp++ = cs->cs_id;
405af57ed9fSAtsushi Murai   }
40626af0ae9SBrian Somers   m->m_len -= hlen;
40726af0ae9SBrian Somers   m->m_offset += hlen;
408af57ed9fSAtsushi Murai   *cp++ = deltaA >> 8;
409af57ed9fSAtsushi Murai   *cp++ = deltaA;
41075240ed1SBrian Somers   memcpy(cp, new_seq, deltaS);
411eaa4df37SBrian Somers   slstat->sls_compressed++;
412af57ed9fSAtsushi Murai   return (TYPE_COMPRESSED_TCP);
413af57ed9fSAtsushi Murai 
414af57ed9fSAtsushi Murai   /*
415af57ed9fSAtsushi Murai    * Update connection state cs & send uncompressed packet ('uncompressed'
416944f7098SBrian Somers    * means a regular ip/tcp packet but with the 'conversation id' we hope to
417944f7098SBrian Somers    * use on future compressed packets in the protocol field).
418af57ed9fSAtsushi Murai    */
419af57ed9fSAtsushi Murai uncompressed:
42075240ed1SBrian Somers   memcpy(&cs->cs_ip, ip, hlen);
421af57ed9fSAtsushi Murai   ip->ip_p = cs->cs_id;
422af57ed9fSAtsushi Murai   comp->last_xmit = cs->cs_id;
423af57ed9fSAtsushi Murai   return (TYPE_UNCOMPRESSED_TCP);
424af57ed9fSAtsushi Murai }
425af57ed9fSAtsushi Murai 
426af57ed9fSAtsushi Murai 
427af57ed9fSAtsushi Murai int
sl_uncompress_tcp(u_char ** bufp,int len,u_int type,struct slcompress * comp,struct slstat * slstat,int max_state)42842c57c86SBrian Somers sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
42942c57c86SBrian Somers                   struct slstat *slstat, int max_state)
430af57ed9fSAtsushi Murai {
431af57ed9fSAtsushi Murai   register u_char *cp;
432af57ed9fSAtsushi Murai   register u_int hlen, changes;
433af57ed9fSAtsushi Murai   register struct tcphdr *th;
434af57ed9fSAtsushi Murai   register struct cstate *cs;
435af57ed9fSAtsushi Murai   register struct ip *ip;
43671981dcfSBrian Somers   u_short *bp;
437af57ed9fSAtsushi Murai 
438af57ed9fSAtsushi Murai   switch (type) {
439af57ed9fSAtsushi Murai 
440af57ed9fSAtsushi Murai   case TYPE_UNCOMPRESSED_TCP:
441af57ed9fSAtsushi Murai     ip = (struct ip *) * bufp;
44242c57c86SBrian Somers     if (ip->ip_p > max_state)
443af57ed9fSAtsushi Murai       goto bad;
444af57ed9fSAtsushi Murai     cs = &comp->rstate[comp->last_recv = ip->ip_p];
445af57ed9fSAtsushi Murai     comp->flags &= ~SLF_TOSS;
446af57ed9fSAtsushi Murai     ip->ip_p = IPPROTO_TCP;
447944f7098SBrian Somers 
448a78ca332SDavid Greenman     /*
449944f7098SBrian Somers      * Calculate the size of the TCP/IP header and make sure that we don't
450944f7098SBrian Somers      * overflow the space we have available for it.
451a78ca332SDavid Greenman      */
452a78ca332SDavid Greenman     hlen = ip->ip_hl << 2;
453057f1760SBrian Somers     if ((int)(hlen + sizeof(struct tcphdr)) > len)
454a78ca332SDavid Greenman       goto bad;
455dc7c7b92SDavid Greenman     th = (struct tcphdr *) & ((char *) ip)[hlen];
456a78ca332SDavid Greenman     hlen += THOFFSET(th) << 2;
457a78ca332SDavid Greenman     if (hlen > MAX_HDR)
458a78ca332SDavid Greenman       goto bad;
45975240ed1SBrian Somers     memcpy(&cs->cs_ip, ip, hlen);
460af57ed9fSAtsushi Murai     cs->cs_hlen = hlen;
461eaa4df37SBrian Somers     slstat->sls_uncompressedin++;
462af57ed9fSAtsushi Murai     return (len);
463af57ed9fSAtsushi Murai 
464af57ed9fSAtsushi Murai   default:
465af57ed9fSAtsushi Murai     goto bad;
466af57ed9fSAtsushi Murai 
467af57ed9fSAtsushi Murai   case TYPE_COMPRESSED_TCP:
468af57ed9fSAtsushi Murai     break;
469af57ed9fSAtsushi Murai   }
470c2b0f58cSBrian Somers 
471af57ed9fSAtsushi Murai   /* We've got a compressed packet. */
472eaa4df37SBrian Somers   slstat->sls_compressedin++;
473af57ed9fSAtsushi Murai   cp = *bufp;
474af57ed9fSAtsushi Murai   changes = *cp++;
475dd7e2610SBrian Somers   log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
476944f7098SBrian Somers 
477c2b0f58cSBrian Somers   if (changes & NEW_C) {
478944f7098SBrian Somers     /*
479944f7098SBrian Somers      * Make sure the state index is in range, then grab the state. If we have
480944f7098SBrian Somers      * a good state index, clear the 'discard' flag.
481944f7098SBrian Somers      */
4825d9e6103SBrian Somers     if (*cp > max_state || comp->last_recv == 255)
483af57ed9fSAtsushi Murai       goto bad;
484af57ed9fSAtsushi Murai 
485af57ed9fSAtsushi Murai     comp->flags &= ~SLF_TOSS;
486af57ed9fSAtsushi Murai     comp->last_recv = *cp++;
487af57ed9fSAtsushi Murai   } else {
488944f7098SBrian Somers     /*
489944f7098SBrian Somers      * this packet has an implicit state index.  If we've had a line error
490944f7098SBrian Somers      * since the last time we got an explicit state index, we have to toss
491944f7098SBrian Somers      * the packet.
492944f7098SBrian Somers      */
493af57ed9fSAtsushi Murai     if (comp->flags & SLF_TOSS) {
494eaa4df37SBrian Somers       slstat->sls_tossed++;
495af57ed9fSAtsushi Murai       return (0);
496af57ed9fSAtsushi Murai     }
497af57ed9fSAtsushi Murai   }
498af57ed9fSAtsushi Murai   cs = &comp->rstate[comp->last_recv];
499af57ed9fSAtsushi Murai   hlen = cs->cs_ip.ip_hl << 2;
500af57ed9fSAtsushi Murai   th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
501af57ed9fSAtsushi Murai   th->th_sum = htons((*cp << 8) | cp[1]);
502af57ed9fSAtsushi Murai   cp += 2;
503af57ed9fSAtsushi Murai   if (changes & TCP_PUSH_BIT)
504*0fc7bdc9SRichard Scheffenegger     __tcp_set_flags(th, __tcp_get_flags(th) | TH_PUSH);
505af57ed9fSAtsushi Murai   else
506*0fc7bdc9SRichard Scheffenegger     __tcp_set_flags(th, __tcp_get_flags(th) & ~TH_PUSH);
507af57ed9fSAtsushi Murai 
508af57ed9fSAtsushi Murai   switch (changes & SPECIALS_MASK) {
509af57ed9fSAtsushi Murai   case SPECIAL_I:
510af57ed9fSAtsushi Murai     {
511af57ed9fSAtsushi Murai       register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
512944f7098SBrian Somers 
513af57ed9fSAtsushi Murai       th->th_ack = htonl(ntohl(th->th_ack) + i);
514af57ed9fSAtsushi Murai       th->th_seq = htonl(ntohl(th->th_seq) + i);
515af57ed9fSAtsushi Murai     }
516af57ed9fSAtsushi Murai     break;
517af57ed9fSAtsushi Murai 
518af57ed9fSAtsushi Murai   case SPECIAL_D:
519af57ed9fSAtsushi Murai     th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
520af57ed9fSAtsushi Murai 		       - cs->cs_hlen);
521af57ed9fSAtsushi Murai     break;
522af57ed9fSAtsushi Murai 
523af57ed9fSAtsushi Murai   default:
524af57ed9fSAtsushi Murai     if (changes & NEW_U) {
525*0fc7bdc9SRichard Scheffenegger       __tcp_set_flags(th, __tcp_get_flags(th) | TH_URG);
526af57ed9fSAtsushi Murai       DECODEU(th->th_urp)
527af57ed9fSAtsushi Murai     } else
528*0fc7bdc9SRichard Scheffenegger       __tcp_set_flags(th, __tcp_get_flags(th) & ~TH_URG);
529af57ed9fSAtsushi Murai     if (changes & NEW_W)
530af57ed9fSAtsushi Murai       DECODES(th->th_win)
531af57ed9fSAtsushi Murai 	if (changes & NEW_A)
532af57ed9fSAtsushi Murai 	DECODEL(th->th_ack)
533af57ed9fSAtsushi Murai 	  if (changes & NEW_S) {
534dd7e2610SBrian Somers 	  log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
535927145beSBrian Somers 		    *cp, cp[1], cp[2]);
536af57ed9fSAtsushi Murai 	  DECODEL(th->th_seq)
537af57ed9fSAtsushi Murai 	}
538af57ed9fSAtsushi Murai     break;
539af57ed9fSAtsushi Murai   }
540af57ed9fSAtsushi Murai   if (changes & NEW_I) {
541af57ed9fSAtsushi Murai     DECODES(cs->cs_ip.ip_id)
542af57ed9fSAtsushi Murai   } else
543af57ed9fSAtsushi Murai     cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
544927145beSBrian Somers 
545dd7e2610SBrian Somers   log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
546e43ebac1SBrian Somers 	    cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
547af57ed9fSAtsushi Murai 
548af57ed9fSAtsushi Murai   /*
549c2b0f58cSBrian Somers    * At this point, cp points to the first byte of data in the packet.
550c2b0f58cSBrian Somers    * Back up cp by the tcp/ip header length to make room for the
551c2b0f58cSBrian Somers    * reconstructed header (we assume the packet we were handed has enough
552c2b0f58cSBrian Somers    * space to prepend 128 bytes of header).  Adjust the length to account
553c2b0f58cSBrian Somers    * for the new header & fill in the IP total length.
554af57ed9fSAtsushi Murai    */
555af57ed9fSAtsushi Murai   len -= (cp - *bufp);
556af57ed9fSAtsushi Murai   if (len < 0)
557944f7098SBrian Somers     /*
558944f7098SBrian Somers      * we must have dropped some characters (crc should detect this but the
559944f7098SBrian Somers      * old slip framing won't)
560944f7098SBrian Somers      */
561af57ed9fSAtsushi Murai     goto bad;
562af57ed9fSAtsushi Murai 
56371981dcfSBrian Somers   *bufp = cp - cs->cs_hlen;
564af57ed9fSAtsushi Murai   len += cs->cs_hlen;
565af57ed9fSAtsushi Murai   cs->cs_ip.ip_len = htons(len);
566af57ed9fSAtsushi Murai 
567af57ed9fSAtsushi Murai   /* recompute the ip header checksum */
56871981dcfSBrian Somers   cs->cs_ip.ip_sum = 0;
56971981dcfSBrian Somers   bp = (u_short *)&cs->cs_ip;
570af57ed9fSAtsushi Murai   for (changes = 0; hlen > 0; hlen -= 2)
571af57ed9fSAtsushi Murai     changes += *bp++;
572af57ed9fSAtsushi Murai   changes = (changes & 0xffff) + (changes >> 16);
573af57ed9fSAtsushi Murai   changes = (changes & 0xffff) + (changes >> 16);
57471981dcfSBrian Somers   cs->cs_ip.ip_sum = ~changes;
575d3b12113SBrian Somers 
57671981dcfSBrian Somers   /* And copy the result into our buffer */
57771981dcfSBrian Somers   memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
57871981dcfSBrian Somers 
579af57ed9fSAtsushi Murai   return (len);
580af57ed9fSAtsushi Murai bad:
581af57ed9fSAtsushi Murai   comp->flags |= SLF_TOSS;
582eaa4df37SBrian Somers   slstat->sls_errorin++;
583af57ed9fSAtsushi Murai   return (0);
584af57ed9fSAtsushi Murai }
585af57ed9fSAtsushi Murai 
586af57ed9fSAtsushi Murai int
sl_Show(struct cmdargs const * arg)587dd7e2610SBrian Somers sl_Show(struct cmdargs const *arg)
588af57ed9fSAtsushi Murai {
589b6217683SBrian Somers   prompt_Printf(arg->prompt, "VJ compression statistics:\n");
590b6217683SBrian Somers   prompt_Printf(arg->prompt, "  Out:  %d (compress) / %d (total)",
591eaa4df37SBrian Somers 	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
592eaa4df37SBrian Somers                 arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
593b6217683SBrian Somers   prompt_Printf(arg->prompt, "  %d (miss) / %d (search)\n",
594eaa4df37SBrian Somers 	        arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
595eaa4df37SBrian Somers                 arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
596b6217683SBrian Somers   prompt_Printf(arg->prompt, "  In:  %d (compress), %d (uncompress)",
597eaa4df37SBrian Somers 	        arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
598eaa4df37SBrian Somers                 arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
599b6217683SBrian Somers   prompt_Printf(arg->prompt, "  %d (error),  %d (tossed)\n",
600eaa4df37SBrian Somers 	        arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
601eaa4df37SBrian Somers                 arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
602927145beSBrian Somers   return 0;
603af57ed9fSAtsushi Murai }
604