Home | History | Annotate | Line # | Download | only in net
slcompress.c revision 1.4
      1  1.1      cgd /*-
      2  1.4  deraadt  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.4  deraadt  * Redistribution and use in source and binary forms, with or without
      6  1.4  deraadt  * modification, are permitted provided that the following conditions
      7  1.4  deraadt  * are met:
      8  1.4  deraadt  * 1. Redistributions of source code must retain the above copyright
      9  1.4  deraadt  *    notice, this list of conditions and the following disclaimer.
     10  1.4  deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.4  deraadt  *    notice, this list of conditions and the following disclaimer in the
     12  1.4  deraadt  *    documentation and/or other materials provided with the distribution.
     13  1.4  deraadt  * 3. All advertising materials mentioning features or use of this software
     14  1.4  deraadt  *    must display the following acknowledgement:
     15  1.4  deraadt  *	This product includes software developed by the University of
     16  1.4  deraadt  *	California, Berkeley and its contributors.
     17  1.4  deraadt  * 4. Neither the name of the University nor the names of its contributors
     18  1.4  deraadt  *    may be used to endorse or promote products derived from this software
     19  1.4  deraadt  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.4  deraadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.4  deraadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.4  deraadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.4  deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.4  deraadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.4  deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.4  deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.4  deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.4  deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.4  deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.4  deraadt  * SUCH DAMAGE.
     32  1.4  deraadt  *
     33  1.4  deraadt  *	@(#)slcompress.c	7.7 (Berkeley) 5/7/91
     34  1.4  deraadt  */
     35  1.4  deraadt 
     36  1.4  deraadt /*
     37  1.4  deraadt  * Routines to compress and uncompess tcp packets (for transmission
     38  1.4  deraadt  * over low speed serial lines.
     39  1.4  deraadt  *
     40  1.4  deraadt  * Van Jacobson (van (at) helios.ee.lbl.gov), Dec 31, 1989:
     41  1.2      cgd  *    - Initial distribution.
     42  1.3      cgd  *
     43  1.4  deraadt  * Modified June 1993 by Paul Mackerras, paulus (at) cs.anu.edu.au,
     44  1.4  deraadt  * so that the entire packet being decompressed doesn't have
     45  1.4  deraadt  * to be in contiguous memory (just the compressed header).
     46  1.4  deraadt  *
     47  1.4  deraadt  *	$Id: slcompress.c,v 1.4 1993/08/14 06:38:40 deraadt Exp $
     48  1.1      cgd  */
     49  1.2      cgd 
     50  1.2      cgd #include <sys/types.h>
     51  1.1      cgd #include <sys/param.h>
     52  1.1      cgd #include <sys/mbuf.h>
     53  1.1      cgd #include <netinet/in.h>
     54  1.1      cgd #include <netinet/in_systm.h>
     55  1.1      cgd #include <netinet/ip.h>
     56  1.1      cgd #include <netinet/tcp.h>
     57  1.1      cgd 
     58  1.1      cgd #include "slcompress.h"
     59  1.1      cgd 
     60  1.1      cgd #ifndef SL_NO_STATS
     61  1.1      cgd #define INCR(counter) ++comp->counter;
     62  1.1      cgd #else
     63  1.1      cgd #define INCR(counter)
     64  1.1      cgd #endif
     65  1.1      cgd 
     66  1.1      cgd #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
     67  1.1      cgd #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
     68  1.1      cgd #ifndef KERNEL
     69  1.1      cgd #define ovbcopy bcopy
     70  1.1      cgd #endif
     71  1.1      cgd 
     72  1.1      cgd 
     73  1.1      cgd void
     74  1.1      cgd sl_compress_init(comp)
     75  1.1      cgd 	struct slcompress *comp;
     76  1.1      cgd {
     77  1.1      cgd 	register u_int i;
     78  1.1      cgd 	register struct cstate *tstate = comp->tstate;
     79  1.1      cgd 
     80  1.1      cgd 	bzero((char *)comp, sizeof(*comp));
     81  1.1      cgd 	for (i = MAX_STATES - 1; i > 0; --i) {
     82  1.1      cgd 		tstate[i].cs_id = i;
     83  1.1      cgd 		tstate[i].cs_next = &tstate[i - 1];
     84  1.1      cgd 	}
     85  1.1      cgd 	tstate[0].cs_next = &tstate[MAX_STATES - 1];
     86  1.1      cgd 	tstate[0].cs_id = 0;
     87  1.1      cgd 	comp->last_cs = &tstate[0];
     88  1.1      cgd 	comp->last_recv = 255;
     89  1.1      cgd 	comp->last_xmit = 255;
     90  1.2      cgd 	comp->flags = SLF_TOSS;
     91  1.1      cgd }
     92  1.1      cgd 
     93  1.1      cgd 
     94  1.1      cgd /* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
     95  1.1      cgd  * checks for zero (since zero has to be encoded in the long, 3 byte
     96  1.1      cgd  * form).
     97  1.1      cgd  */
     98  1.1      cgd #define ENCODE(n) { \
     99  1.1      cgd 	if ((u_short)(n) >= 256) { \
    100  1.1      cgd 		*cp++ = 0; \
    101  1.1      cgd 		cp[1] = (n); \
    102  1.1      cgd 		cp[0] = (n) >> 8; \
    103  1.1      cgd 		cp += 2; \
    104  1.1      cgd 	} else { \
    105  1.1      cgd 		*cp++ = (n); \
    106  1.1      cgd 	} \
    107  1.1      cgd }
    108  1.1      cgd #define ENCODEZ(n) { \
    109  1.1      cgd 	if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
    110  1.1      cgd 		*cp++ = 0; \
    111  1.1      cgd 		cp[1] = (n); \
    112  1.1      cgd 		cp[0] = (n) >> 8; \
    113  1.1      cgd 		cp += 2; \
    114  1.1      cgd 	} else { \
    115  1.1      cgd 		*cp++ = (n); \
    116  1.1      cgd 	} \
    117  1.1      cgd }
    118  1.1      cgd 
    119  1.1      cgd #define DECODEL(f) { \
    120  1.1      cgd 	if (*cp == 0) {\
    121  1.1      cgd 		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
    122  1.1      cgd 		cp += 3; \
    123  1.1      cgd 	} else { \
    124  1.1      cgd 		(f) = htonl(ntohl(f) + (u_long)*cp++); \
    125  1.1      cgd 	} \
    126  1.1      cgd }
    127  1.1      cgd 
    128  1.1      cgd #define DECODES(f) { \
    129  1.1      cgd 	if (*cp == 0) {\
    130  1.1      cgd 		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
    131  1.1      cgd 		cp += 3; \
    132  1.1      cgd 	} else { \
    133  1.1      cgd 		(f) = htons(ntohs(f) + (u_long)*cp++); \
    134  1.1      cgd 	} \
    135  1.1      cgd }
    136  1.1      cgd 
    137  1.1      cgd #define DECODEU(f) { \
    138  1.1      cgd 	if (*cp == 0) {\
    139  1.1      cgd 		(f) = htons((cp[1] << 8) | cp[2]); \
    140  1.1      cgd 		cp += 3; \
    141  1.1      cgd 	} else { \
    142  1.1      cgd 		(f) = htons((u_long)*cp++); \
    143  1.1      cgd 	} \
    144  1.1      cgd }
    145  1.1      cgd 
    146  1.1      cgd 
    147  1.1      cgd u_char
    148  1.1      cgd sl_compress_tcp(m, ip, comp, compress_cid)
    149  1.1      cgd 	struct mbuf *m;
    150  1.1      cgd 	register struct ip *ip;
    151  1.1      cgd 	struct slcompress *comp;
    152  1.1      cgd 	int compress_cid;
    153  1.1      cgd {
    154  1.1      cgd 	register struct cstate *cs = comp->last_cs->cs_next;
    155  1.1      cgd 	register u_int hlen = ip->ip_hl;
    156  1.1      cgd 	register struct tcphdr *oth;
    157  1.1      cgd 	register struct tcphdr *th;
    158  1.1      cgd 	register u_int deltaS, deltaA;
    159  1.1      cgd 	register u_int changes = 0;
    160  1.1      cgd 	u_char new_seq[16];
    161  1.1      cgd 	register u_char *cp = new_seq;
    162  1.1      cgd 
    163  1.1      cgd 	/*
    164  1.1      cgd 	 * Bail if this is an IP fragment or if the TCP packet isn't
    165  1.1      cgd 	 * `compressible' (i.e., ACK isn't set or some other control bit is
    166  1.1      cgd 	 * set).  (We assume that the caller has already made sure the
    167  1.1      cgd 	 * packet is IP proto TCP).
    168  1.1      cgd 	 */
    169  1.1      cgd 	if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40)
    170  1.1      cgd 		return (TYPE_IP);
    171  1.1      cgd 
    172  1.1      cgd 	th = (struct tcphdr *)&((int *)ip)[hlen];
    173  1.1      cgd 	if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
    174  1.1      cgd 		return (TYPE_IP);
    175  1.1      cgd 	/*
    176  1.1      cgd 	 * Packet is compressible -- we're going to send either a
    177  1.1      cgd 	 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet.  Either way we need
    178  1.1      cgd 	 * to locate (or create) the connection state.  Special case the
    179  1.1      cgd 	 * most recently used connection since it's most likely to be used
    180  1.1      cgd 	 * again & we don't have to do any reordering if it's used.
    181  1.1      cgd 	 */
    182  1.1      cgd 	INCR(sls_packets)
    183  1.1      cgd 	if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
    184  1.1      cgd 	    ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
    185  1.1      cgd 	    *(int *)th != ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl]) {
    186  1.1      cgd 		/*
    187  1.1      cgd 		 * Wasn't the first -- search for it.
    188  1.1      cgd 		 *
    189  1.1      cgd 		 * States are kept in a circularly linked list with
    190  1.1      cgd 		 * last_cs pointing to the end of the list.  The
    191  1.1      cgd 		 * list is kept in lru order by moving a state to the
    192  1.1      cgd 		 * head of the list whenever it is referenced.  Since
    193  1.1      cgd 		 * the list is short and, empirically, the connection
    194  1.1      cgd 		 * we want is almost always near the front, we locate
    195  1.1      cgd 		 * states via linear search.  If we don't find a state
    196  1.1      cgd 		 * for the datagram, the oldest state is (re-)used.
    197  1.1      cgd 		 */
    198  1.1      cgd 		register struct cstate *lcs;
    199  1.1      cgd 		register struct cstate *lastcs = comp->last_cs;
    200  1.1      cgd 
    201  1.1      cgd 		do {
    202  1.1      cgd 			lcs = cs; cs = cs->cs_next;
    203  1.1      cgd 			INCR(sls_searches)
    204  1.1      cgd 			if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
    205  1.1      cgd 			    && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
    206  1.1      cgd 			    && *(int *)th == ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl])
    207  1.1      cgd 				goto found;
    208  1.1      cgd 		} while (cs != lastcs);
    209  1.1      cgd 
    210  1.1      cgd 		/*
    211  1.1      cgd 		 * Didn't find it -- re-use oldest cstate.  Send an
    212  1.1      cgd 		 * uncompressed packet that tells the other side what
    213  1.1      cgd 		 * connection number we're using for this conversation.
    214  1.1      cgd 		 * Note that since the state list is circular, the oldest
    215  1.1      cgd 		 * state points to the newest and we only need to set
    216  1.1      cgd 		 * last_cs to update the lru linkage.
    217  1.1      cgd 		 */
    218  1.1      cgd 		INCR(sls_misses)
    219  1.1      cgd 		comp->last_cs = lcs;
    220  1.1      cgd 		hlen += th->th_off;
    221  1.1      cgd 		hlen <<= 2;
    222  1.2      cgd 		if (hlen > m->m_len)
    223  1.2      cgd 			return (TYPE_IP);
    224  1.1      cgd 		goto uncompressed;
    225  1.1      cgd 
    226  1.1      cgd 	found:
    227  1.1      cgd 		/*
    228  1.1      cgd 		 * Found it -- move to the front on the connection list.
    229  1.1      cgd 		 */
    230  1.1      cgd 		if (cs == lastcs)
    231  1.1      cgd 			comp->last_cs = lcs;
    232  1.1      cgd 		else {
    233  1.1      cgd 			lcs->cs_next = cs->cs_next;
    234  1.1      cgd 			cs->cs_next = lastcs->cs_next;
    235  1.1      cgd 			lastcs->cs_next = cs;
    236  1.1      cgd 		}
    237  1.1      cgd 	}
    238  1.1      cgd 
    239  1.1      cgd 	/*
    240  1.1      cgd 	 * Make sure that only what we expect to change changed. The first
    241  1.1      cgd 	 * line of the `if' checks the IP protocol version, header length &
    242  1.1      cgd 	 * type of service.  The 2nd line checks the "Don't fragment" bit.
    243  1.1      cgd 	 * The 3rd line checks the time-to-live and protocol (the protocol
    244  1.1      cgd 	 * check is unnecessary but costless).  The 4th line checks the TCP
    245  1.1      cgd 	 * header length.  The 5th line checks IP options, if any.  The 6th
    246  1.1      cgd 	 * line checks TCP options, if any.  If any of these things are
    247  1.1      cgd 	 * different between the previous & current datagram, we send the
    248  1.1      cgd 	 * current datagram `uncompressed'.
    249  1.1      cgd 	 */
    250  1.1      cgd 	oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen];
    251  1.1      cgd 	deltaS = hlen;
    252  1.1      cgd 	hlen += th->th_off;
    253  1.1      cgd 	hlen <<= 2;
    254  1.2      cgd 	if (hlen > m->m_len)
    255  1.2      cgd 		return (TYPE_IP);
    256  1.1      cgd 
    257  1.1      cgd 	if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] ||
    258  1.1      cgd 	    ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] ||
    259  1.1      cgd 	    ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] ||
    260  1.1      cgd 	    th->th_off != oth->th_off ||
    261  1.1      cgd 	    (deltaS > 5 &&
    262  1.1      cgd 	     BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
    263  1.1      cgd 	    (th->th_off > 5 &&
    264  1.1      cgd 	     BCMP(th + 1, oth + 1, (th->th_off - 5) << 2)))
    265  1.1      cgd 		goto uncompressed;
    266  1.1      cgd 
    267  1.1      cgd 	/*
    268  1.1      cgd 	 * Figure out which of the changing fields changed.  The
    269  1.1      cgd 	 * receiver expects changes in the order: urgent, window,
    270  1.1      cgd 	 * ack, seq (the order minimizes the number of temporaries
    271  1.1      cgd 	 * needed in this section of code).
    272  1.1      cgd 	 */
    273  1.1      cgd 	if (th->th_flags & TH_URG) {
    274  1.1      cgd 		deltaS = ntohs(th->th_urp);
    275  1.1      cgd 		ENCODEZ(deltaS);
    276  1.1      cgd 		changes |= NEW_U;
    277  1.1      cgd 	} else if (th->th_urp != oth->th_urp)
    278  1.1      cgd 		/* argh! URG not set but urp changed -- a sensible
    279  1.1      cgd 		 * implementation should never do this but RFC793
    280  1.1      cgd 		 * doesn't prohibit the change so we have to deal
    281  1.1      cgd 		 * with it. */
    282  1.1      cgd 		 goto uncompressed;
    283  1.1      cgd 
    284  1.1      cgd 	if (deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) {
    285  1.1      cgd 		ENCODE(deltaS);
    286  1.1      cgd 		changes |= NEW_W;
    287  1.1      cgd 	}
    288  1.1      cgd 
    289  1.1      cgd 	if (deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) {
    290  1.1      cgd 		if (deltaA > 0xffff)
    291  1.1      cgd 			goto uncompressed;
    292  1.1      cgd 		ENCODE(deltaA);
    293  1.1      cgd 		changes |= NEW_A;
    294  1.1      cgd 	}
    295  1.1      cgd 
    296  1.1      cgd 	if (deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) {
    297  1.1      cgd 		if (deltaS > 0xffff)
    298  1.1      cgd 			goto uncompressed;
    299  1.1      cgd 		ENCODE(deltaS);
    300  1.1      cgd 		changes |= NEW_S;
    301  1.1      cgd 	}
    302  1.1      cgd 
    303  1.1      cgd 	switch(changes) {
    304  1.1      cgd 
    305  1.1      cgd 	case 0:
    306  1.1      cgd 		/*
    307  1.1      cgd 		 * Nothing changed. If this packet contains data and the
    308  1.1      cgd 		 * last one didn't, this is probably a data packet following
    309  1.1      cgd 		 * an ack (normal on an interactive connection) and we send
    310  1.1      cgd 		 * it compressed.  Otherwise it's probably a retransmit,
    311  1.1      cgd 		 * retransmitted ack or window probe.  Send it uncompressed
    312  1.1      cgd 		 * in case the other side missed the compressed version.
    313  1.1      cgd 		 */
    314  1.1      cgd 		if (ip->ip_len != cs->cs_ip.ip_len &&
    315  1.1      cgd 		    ntohs(cs->cs_ip.ip_len) == hlen)
    316  1.1      cgd 			break;
    317  1.1      cgd 
    318  1.1      cgd 		/* (fall through) */
    319  1.1      cgd 
    320  1.1      cgd 	case SPECIAL_I:
    321  1.1      cgd 	case SPECIAL_D:
    322  1.1      cgd 		/*
    323  1.1      cgd 		 * actual changes match one of our special case encodings --
    324  1.1      cgd 		 * send packet uncompressed.
    325  1.1      cgd 		 */
    326  1.1      cgd 		goto uncompressed;
    327  1.1      cgd 
    328  1.1      cgd 	case NEW_S|NEW_A:
    329  1.1      cgd 		if (deltaS == deltaA &&
    330  1.1      cgd 		    deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
    331  1.1      cgd 			/* special case for echoed terminal traffic */
    332  1.1      cgd 			changes = SPECIAL_I;
    333  1.1      cgd 			cp = new_seq;
    334  1.1      cgd 		}
    335  1.1      cgd 		break;
    336  1.1      cgd 
    337  1.1      cgd 	case NEW_S:
    338  1.1      cgd 		if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
    339  1.1      cgd 			/* special case for data xfer */
    340  1.1      cgd 			changes = SPECIAL_D;
    341  1.1      cgd 			cp = new_seq;
    342  1.1      cgd 		}
    343  1.1      cgd 		break;
    344  1.1      cgd 	}
    345  1.1      cgd 
    346  1.1      cgd 	deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
    347  1.1      cgd 	if (deltaS != 1) {
    348  1.1      cgd 		ENCODEZ(deltaS);
    349  1.1      cgd 		changes |= NEW_I;
    350  1.1      cgd 	}
    351  1.1      cgd 	if (th->th_flags & TH_PUSH)
    352  1.1      cgd 		changes |= TCP_PUSH_BIT;
    353  1.1      cgd 	/*
    354  1.1      cgd 	 * Grab the cksum before we overwrite it below.  Then update our
    355  1.1      cgd 	 * state with this packet's header.
    356  1.1      cgd 	 */
    357  1.1      cgd 	deltaA = ntohs(th->th_sum);
    358  1.1      cgd 	BCOPY(ip, &cs->cs_ip, hlen);
    359  1.1      cgd 
    360  1.1      cgd 	/*
    361  1.1      cgd 	 * We want to use the original packet as our compressed packet.
    362  1.1      cgd 	 * (cp - new_seq) is the number of bytes we need for compressed
    363  1.1      cgd 	 * sequence numbers.  In addition we need one byte for the change
    364  1.1      cgd 	 * mask, one for the connection id and two for the tcp checksum.
    365  1.1      cgd 	 * So, (cp - new_seq) + 4 bytes of header are needed.  hlen is how
    366  1.1      cgd 	 * many bytes of the original packet to toss so subtract the two to
    367  1.1      cgd 	 * get the new packet size.
    368  1.1      cgd 	 */
    369  1.1      cgd 	deltaS = cp - new_seq;
    370  1.1      cgd 	cp = (u_char *)ip;
    371  1.1      cgd 	if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
    372  1.1      cgd 		comp->last_xmit = cs->cs_id;
    373  1.1      cgd 		hlen -= deltaS + 4;
    374  1.1      cgd 		cp += hlen;
    375  1.1      cgd 		*cp++ = changes | NEW_C;
    376  1.1      cgd 		*cp++ = cs->cs_id;
    377  1.1      cgd 	} else {
    378  1.1      cgd 		hlen -= deltaS + 3;
    379  1.1      cgd 		cp += hlen;
    380  1.1      cgd 		*cp++ = changes;
    381  1.1      cgd 	}
    382  1.1      cgd 	m->m_len -= hlen;
    383  1.1      cgd 	m->m_data += hlen;
    384  1.1      cgd 	*cp++ = deltaA >> 8;
    385  1.1      cgd 	*cp++ = deltaA;
    386  1.1      cgd 	BCOPY(new_seq, cp, deltaS);
    387  1.1      cgd 	INCR(sls_compressed)
    388  1.1      cgd 	return (TYPE_COMPRESSED_TCP);
    389  1.1      cgd 
    390  1.1      cgd 	/*
    391  1.1      cgd 	 * Update connection state cs & send uncompressed packet ('uncompressed'
    392  1.1      cgd 	 * means a regular ip/tcp packet but with the 'conversation id' we hope
    393  1.1      cgd 	 * to use on future compressed packets in the protocol field).
    394  1.1      cgd 	 */
    395  1.1      cgd uncompressed:
    396  1.1      cgd 	BCOPY(ip, &cs->cs_ip, hlen);
    397  1.1      cgd 	ip->ip_p = cs->cs_id;
    398  1.1      cgd 	comp->last_xmit = cs->cs_id;
    399  1.1      cgd 	return (TYPE_UNCOMPRESSED_TCP);
    400  1.1      cgd }
    401  1.1      cgd 
    402  1.1      cgd 
    403  1.1      cgd int
    404  1.1      cgd sl_uncompress_tcp(bufp, len, type, comp)
    405  1.1      cgd 	u_char **bufp;
    406  1.1      cgd 	int len;
    407  1.1      cgd 	u_int type;
    408  1.1      cgd 	struct slcompress *comp;
    409  1.1      cgd {
    410  1.4  deraadt 	return sl_uncompress_tcp_part(bufp, len, len, type, comp);
    411  1.4  deraadt }
    412  1.4  deraadt 
    413  1.4  deraadt 
    414  1.4  deraadt /*
    415  1.4  deraadt  * Uncompress a packet of total length total_len.  The first buflen
    416  1.4  deraadt  * bytes are at *bufp; this must include the entire (compressed or
    417  1.4  deraadt  * uncompressed) TCP/IP header.  In addition, there must be enough
    418  1.4  deraadt  * clear space before *bufp to build a full-length TCP/IP header.
    419  1.4  deraadt  */
    420  1.4  deraadt int
    421  1.4  deraadt sl_uncompress_tcp_part(bufp, buflen, total_len, type, comp)
    422  1.4  deraadt 	u_char **bufp;
    423  1.4  deraadt 	int buflen, total_len;
    424  1.4  deraadt 	u_int type;
    425  1.4  deraadt 	struct slcompress *comp;
    426  1.4  deraadt {
    427  1.1      cgd 	register u_char *cp;
    428  1.1      cgd 	register u_int hlen, changes;
    429  1.1      cgd 	register struct tcphdr *th;
    430  1.1      cgd 	register struct cstate *cs;
    431  1.1      cgd 	register struct ip *ip;
    432  1.1      cgd 
    433  1.1      cgd 	switch (type) {
    434  1.1      cgd 
    435  1.1      cgd 	case TYPE_UNCOMPRESSED_TCP:
    436  1.1      cgd 		ip = (struct ip *) *bufp;
    437  1.1      cgd 		if (ip->ip_p >= MAX_STATES)
    438  1.1      cgd 			goto bad;
    439  1.1      cgd 		cs = &comp->rstate[comp->last_recv = ip->ip_p];
    440  1.1      cgd 		comp->flags &=~ SLF_TOSS;
    441  1.1      cgd 		ip->ip_p = IPPROTO_TCP;
    442  1.1      cgd 		hlen = ip->ip_hl;
    443  1.1      cgd 		hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
    444  1.1      cgd 		hlen <<= 2;
    445  1.1      cgd 		BCOPY(ip, &cs->cs_ip, hlen);
    446  1.1      cgd 		cs->cs_ip.ip_sum = 0;
    447  1.1      cgd 		cs->cs_hlen = hlen;
    448  1.1      cgd 		INCR(sls_uncompressedin)
    449  1.4  deraadt 		return (total_len);
    450  1.1      cgd 
    451  1.1      cgd 	default:
    452  1.1      cgd 		goto bad;
    453  1.1      cgd 
    454  1.1      cgd 	case TYPE_COMPRESSED_TCP:
    455  1.1      cgd 		break;
    456  1.1      cgd 	}
    457  1.1      cgd 	/* We've got a compressed packet. */
    458  1.1      cgd 	INCR(sls_compressedin)
    459  1.1      cgd 	cp = *bufp;
    460  1.1      cgd 	changes = *cp++;
    461  1.1      cgd 	if (changes & NEW_C) {
    462  1.1      cgd 		/* Make sure the state index is in range, then grab the state.
    463  1.1      cgd 		 * If we have a good state index, clear the 'discard' flag. */
    464  1.1      cgd 		if (*cp >= MAX_STATES)
    465  1.1      cgd 			goto bad;
    466  1.1      cgd 
    467  1.1      cgd 		comp->flags &=~ SLF_TOSS;
    468  1.1      cgd 		comp->last_recv = *cp++;
    469  1.1      cgd 	} else {
    470  1.1      cgd 		/* this packet has an implicit state index.  If we've
    471  1.1      cgd 		 * had a line error since the last time we got an
    472  1.1      cgd 		 * explicit state index, we have to toss the packet. */
    473  1.1      cgd 		if (comp->flags & SLF_TOSS) {
    474  1.1      cgd 			INCR(sls_tossed)
    475  1.1      cgd 			return (0);
    476  1.1      cgd 		}
    477  1.1      cgd 	}
    478  1.1      cgd 	cs = &comp->rstate[comp->last_recv];
    479  1.1      cgd 	hlen = cs->cs_ip.ip_hl << 2;
    480  1.1      cgd 	th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
    481  1.1      cgd 	th->th_sum = htons((*cp << 8) | cp[1]);
    482  1.1      cgd 	cp += 2;
    483  1.1      cgd 	if (changes & TCP_PUSH_BIT)
    484  1.1      cgd 		th->th_flags |= TH_PUSH;
    485  1.1      cgd 	else
    486  1.1      cgd 		th->th_flags &=~ TH_PUSH;
    487  1.1      cgd 
    488  1.1      cgd 	switch (changes & SPECIALS_MASK) {
    489  1.1      cgd 	case SPECIAL_I:
    490  1.1      cgd 		{
    491  1.1      cgd 		register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
    492  1.1      cgd 		th->th_ack = htonl(ntohl(th->th_ack) + i);
    493  1.1      cgd 		th->th_seq = htonl(ntohl(th->th_seq) + i);
    494  1.1      cgd 		}
    495  1.1      cgd 		break;
    496  1.1      cgd 
    497  1.1      cgd 	case SPECIAL_D:
    498  1.1      cgd 		th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
    499  1.1      cgd 				   - cs->cs_hlen);
    500  1.1      cgd 		break;
    501  1.1      cgd 
    502  1.1      cgd 	default:
    503  1.1      cgd 		if (changes & NEW_U) {
    504  1.1      cgd 			th->th_flags |= TH_URG;
    505  1.1      cgd 			DECODEU(th->th_urp)
    506  1.1      cgd 		} else
    507  1.1      cgd 			th->th_flags &=~ TH_URG;
    508  1.1      cgd 		if (changes & NEW_W)
    509  1.1      cgd 			DECODES(th->th_win)
    510  1.1      cgd 		if (changes & NEW_A)
    511  1.1      cgd 			DECODEL(th->th_ack)
    512  1.1      cgd 		if (changes & NEW_S)
    513  1.1      cgd 			DECODEL(th->th_seq)
    514  1.1      cgd 		break;
    515  1.1      cgd 	}
    516  1.1      cgd 	if (changes & NEW_I) {
    517  1.1      cgd 		DECODES(cs->cs_ip.ip_id)
    518  1.1      cgd 	} else
    519  1.1      cgd 		cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
    520  1.1      cgd 
    521  1.1      cgd 	/*
    522  1.1      cgd 	 * At this point, cp points to the first byte of data in the
    523  1.1      cgd 	 * packet.  If we're not aligned on a 4-byte boundary, copy the
    524  1.1      cgd 	 * data down so the ip & tcp headers will be aligned.  Then back up
    525  1.1      cgd 	 * cp by the tcp/ip header length to make room for the reconstructed
    526  1.1      cgd 	 * header (we assume the packet we were handed has enough space to
    527  1.1      cgd 	 * prepend 128 bytes of header).  Adjust the length to account for
    528  1.1      cgd 	 * the new header & fill in the IP total length.
    529  1.1      cgd 	 */
    530  1.4  deraadt 	buflen -= (cp - *bufp);
    531  1.4  deraadt 	total_len -= (cp - *bufp);
    532  1.4  deraadt 	if (buflen < 0)
    533  1.1      cgd 		/* we must have dropped some characters (crc should detect
    534  1.1      cgd 		 * this but the old slip framing won't) */
    535  1.1      cgd 		goto bad;
    536  1.1      cgd 
    537  1.1      cgd 	if ((int)cp & 3) {
    538  1.4  deraadt 		if (buflen > 0)
    539  1.4  deraadt 			(void) ovbcopy(cp, (caddr_t)((int)cp &~ 3), buflen);
    540  1.1      cgd 		cp = (u_char *)((int)cp &~ 3);
    541  1.1      cgd 	}
    542  1.1      cgd 	cp -= cs->cs_hlen;
    543  1.4  deraadt 	total_len += cs->cs_hlen;
    544  1.4  deraadt 	cs->cs_ip.ip_len = htons(total_len);
    545  1.1      cgd 	BCOPY(&cs->cs_ip, cp, cs->cs_hlen);
    546  1.1      cgd 	*bufp = cp;
    547  1.1      cgd 
    548  1.1      cgd 	/* recompute the ip header checksum */
    549  1.1      cgd 	{
    550  1.1      cgd 		register u_short *bp = (u_short *)cp;
    551  1.1      cgd 		for (changes = 0; hlen > 0; hlen -= 2)
    552  1.1      cgd 			changes += *bp++;
    553  1.1      cgd 		changes = (changes & 0xffff) + (changes >> 16);
    554  1.1      cgd 		changes = (changes & 0xffff) + (changes >> 16);
    555  1.1      cgd 		((struct ip *)cp)->ip_sum = ~ changes;
    556  1.1      cgd 	}
    557  1.4  deraadt 	return (total_len);
    558  1.1      cgd bad:
    559  1.1      cgd 	comp->flags |= SLF_TOSS;
    560  1.1      cgd 	INCR(sls_errorin)
    561  1.1      cgd 	return (0);
    562  1.1      cgd }
    563