slcompress.c revision 30187
16059Samurai/* 26059Samurai * Routines to compress and uncompess tcp packets (for transmission 36059Samurai * over low speed serial lines. 46059Samurai * 56059Samurai * Copyright (c) 1989 Regents of the University of California. 66059Samurai * All rights reserved. 76059Samurai * 86059Samurai * Redistribution and use in source and binary forms are permitted 96059Samurai * provided that the above copyright notice and this paragraph are 106059Samurai * duplicated in all such forms and that any documentation, 116059Samurai * advertising materials, and other materials related to such 126059Samurai * distribution and use acknowledge that the software was developed 136059Samurai * by the University of California, Berkeley. The name of the 146059Samurai * University may not be used to endorse or promote products derived 156059Samurai * from this software without specific prior written permission. 166059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 176059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 186059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 196059Samurai * 2030187Sbrian * $Id: slcompress.c,v 1.10 1997/08/25 00:29:28 brian Exp $ 218857Srgrimes * 226059Samurai * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: 236059Samurai * - Initial distribution. 246059Samurai */ 256059Samurai#ifndef lint 2630187Sbrianstatic char const rcsid[] = "$Id: slcompress.c,v 1.10 1997/08/25 00:29:28 brian Exp $"; 2728679Sbrian 286059Samurai#endif 296059Samurai 306059Samurai#include "defs.h" 316059Samurai#include <netinet/in_systm.h> 326059Samurai#include <netinet/in.h> 336059Samurai#include <netinet/tcp.h> 346059Samurai#include <netinet/ip.h> 356059Samurai#include "slcompress.h" 3626516Sbrian#include "loadalias.h" 3726516Sbrian#include "vars.h" 386059Samurai 396059Samuraistruct slstat slstat; 406059Samurai 416059Samurai#define INCR(counter) slstat.counter++; 426059Samurai 436059Samurai#define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n)) 446059Samurai#define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n)) 456059Samurai#ifndef KERNEL 466059Samurai#define ovbcopy bcopy 476059Samurai#endif 486059Samurai 496059Samuraivoid 5030187Sbriansl_compress_init(struct slcompress * comp, int max_state) 516059Samurai{ 5228679Sbrian register u_int i; 5328679Sbrian register struct cstate *tstate = comp->tstate; 546059Samurai 5528679Sbrian bzero((char *) comp, sizeof(*comp)); 5630187Sbrian for (i = max_state; i > 0; --i) { 5728679Sbrian tstate[i].cs_id = i; 5828679Sbrian tstate[i].cs_next = &tstate[i - 1]; 5928679Sbrian } 6030187Sbrian tstate[0].cs_next = &tstate[max_state]; 6128679Sbrian tstate[0].cs_id = 0; 6228679Sbrian comp->last_cs = &tstate[0]; 6328679Sbrian comp->last_recv = 255; 6428679Sbrian comp->last_xmit = 255; 6528679Sbrian comp->flags = SLF_TOSS; 666059Samurai} 676059Samurai 686059Samurai 696059Samurai/* ENCODE encodes a number that is known to be non-zero. ENCODEZ 706059Samurai * checks for zero (since zero has to be encoded in the long, 3 byte 716059Samurai * form). 726059Samurai */ 736059Samurai#define ENCODE(n) { \ 746059Samurai if ((u_short)(n) >= 256) { \ 756059Samurai *cp++ = 0; \ 766059Samurai cp[1] = (n); \ 776059Samurai cp[0] = (n) >> 8; \ 786059Samurai cp += 2; \ 796059Samurai } else { \ 806059Samurai *cp++ = (n); \ 816059Samurai } \ 826059Samurai} 836059Samurai#define ENCODEZ(n) { \ 846059Samurai if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \ 856059Samurai *cp++ = 0; \ 866059Samurai cp[1] = (n); \ 876059Samurai cp[0] = (n) >> 8; \ 886059Samurai cp += 2; \ 896059Samurai } else { \ 906059Samurai *cp++ = (n); \ 916059Samurai } \ 926059Samurai} 936059Samurai 946059Samurai#define DECODEL(f) { \ 956059Samurai if (*cp == 0) {\ 966059Samurai (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \ 976059Samurai cp += 3; \ 986059Samurai } else { \ 996059Samurai (f) = htonl(ntohl(f) + (u_long)*cp++); \ 1006059Samurai } \ 1016059Samurai} 1026059Samurai 1036059Samurai#define DECODES(f) { \ 1046059Samurai if (*cp == 0) {\ 1056059Samurai (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \ 1066059Samurai cp += 3; \ 1076059Samurai } else { \ 1086059Samurai (f) = htons(ntohs(f) + (u_long)*cp++); \ 1096059Samurai } \ 1106059Samurai} 1116059Samurai 1126059Samurai#define DECODEU(f) { \ 1136059Samurai if (*cp == 0) {\ 1146059Samurai (f) = htons((cp[1] << 8) | cp[2]); \ 1156059Samurai cp += 3; \ 1166059Samurai } else { \ 1176059Samurai (f) = htons((u_long)*cp++); \ 1186059Samurai } \ 1196059Samurai} 1206059Samurai 1216059Samurai 1226059Samuraiu_char 12328679Sbriansl_compress_tcp(struct mbuf * m, 12428679Sbrian struct ip * ip, 12528679Sbrian struct slcompress * comp, 12628679Sbrian int compress_cid) 1276059Samurai{ 12828679Sbrian register struct cstate *cs = comp->last_cs->cs_next; 12928679Sbrian register u_int hlen = ip->ip_hl; 13028679Sbrian register struct tcphdr *oth; 13128679Sbrian register struct tcphdr *th; 13228679Sbrian register u_int deltaS, deltaA; 13328679Sbrian register u_int changes = 0; 13428679Sbrian u_char new_seq[16]; 13528679Sbrian register u_char *cp = new_seq; 1366059Samurai 13728679Sbrian /* 13828679Sbrian * Bail if this is an IP fragment or if the TCP packet isn't `compressible' 13928679Sbrian * (i.e., ACK isn't set or some other control bit is set). (We assume that 14028679Sbrian * the caller has already made sure the packet is IP proto TCP). 14128679Sbrian */ 14228679Sbrian if ((ip->ip_off & htons(0x3fff)) || m->cnt < 40) { 14328679Sbrian LogPrintf(LogDEBUG, "??? 1 ip_off = %x, cnt = %d\n", 14428679Sbrian ip->ip_off, m->cnt); 14528679Sbrian LogDumpBp(LogDEBUG, "", m); 14628679Sbrian return (TYPE_IP); 14728679Sbrian } 14828679Sbrian th = (struct tcphdr *) & ((int *) ip)[hlen]; 14928679Sbrian if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) { 15028679Sbrian LogPrintf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags); 15128679Sbrian LogDumpBp(LogDEBUG, "", m); 15228679Sbrian return (TYPE_IP); 15328679Sbrian } 1546059Samurai 15528679Sbrian /* 15628679Sbrian * Packet is compressible -- we're going to send either a COMPRESSED_TCP or 15728679Sbrian * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the 15828679Sbrian * connection state. Special case the most recently used connection since 15928679Sbrian * it's most likely to be used again & we don't have to do any reordering 16028679Sbrian * if it's used. 16128679Sbrian */ 16228679Sbrian INCR(sls_packets) 16328679Sbrian if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr || 16428679Sbrian ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr || 16528679Sbrian *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) { 1666059Samurai 16728679Sbrian /* 16828679Sbrian * Wasn't the first -- search for it. 16928679Sbrian * 17028679Sbrian * States are kept in a circularly linked list with last_cs pointing to the 17128679Sbrian * end of the list. The list is kept in lru order by moving a state to 17228679Sbrian * the head of the list whenever it is referenced. Since the list is 17328679Sbrian * short and, empirically, the connection we want is almost always near 17428679Sbrian * the front, we locate states via linear search. If we don't find a 17528679Sbrian * state for the datagram, the oldest state is (re-)used. 17628679Sbrian */ 17728679Sbrian register struct cstate *lcs; 17828679Sbrian register struct cstate *lastcs = comp->last_cs; 1796059Samurai 18028679Sbrian do { 18128679Sbrian lcs = cs; 18228679Sbrian cs = cs->cs_next; 18328679Sbrian INCR(sls_searches) 18428679Sbrian if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr 18528679Sbrian && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr 18628679Sbrian && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) 18728679Sbrian goto found; 18828679Sbrian } while (cs != lastcs); 1896059Samurai 19028679Sbrian /* 19128679Sbrian * Didn't find it -- re-use oldest cstate. Send an uncompressed packet 19228679Sbrian * that tells the other side what connection number we're using for this 19328679Sbrian * conversation. Note that since the state list is circular, the oldest 19428679Sbrian * state points to the newest and we only need to set last_cs to update 19528679Sbrian * the lru linkage. 19628679Sbrian */ 19728679Sbrian INCR(sls_misses) 19828679Sbrian comp->last_cs = lcs; 1996059Samurai#define THOFFSET(th) (th->th_off) 20028679Sbrian hlen += th->th_off; 20128679Sbrian hlen <<= 2; 20228679Sbrian if (hlen > m->cnt) 20328679Sbrian return (TYPE_IP); 20428679Sbrian goto uncompressed; 2056059Samurai 20628679Sbrianfound: 2076059Samurai 20828679Sbrian /* 20928679Sbrian * Found it -- move to the front on the connection list. 21028679Sbrian */ 21128679Sbrian if (cs == lastcs) 21228679Sbrian comp->last_cs = lcs; 21328679Sbrian else { 21428679Sbrian lcs->cs_next = cs->cs_next; 21528679Sbrian cs->cs_next = lastcs->cs_next; 21628679Sbrian lastcs->cs_next = cs; 21728679Sbrian } 21828679Sbrian } 2196059Samurai 22028679Sbrian /* 22128679Sbrian * Make sure that only what we expect to change changed. The first line of 22228679Sbrian * the `if' checks the IP protocol version, header length & type of 22328679Sbrian * service. The 2nd line checks the "Don't fragment" bit. The 3rd line 22428679Sbrian * checks the time-to-live and protocol (the protocol check is unnecessary 22528679Sbrian * but costless). The 4th line checks the TCP header length. The 5th line 22628679Sbrian * checks IP options, if any. The 6th line checks TCP options, if any. If 22728679Sbrian * any of these things are different between the previous & current 22828679Sbrian * datagram, we send the current datagram `uncompressed'. 22928679Sbrian */ 23028679Sbrian oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen]; 23128679Sbrian deltaS = hlen; 23228679Sbrian hlen += th->th_off; 23328679Sbrian hlen <<= 2; 23428679Sbrian if (hlen > m->cnt) 23528679Sbrian return (TYPE_IP); 2366059Samurai 23728679Sbrian if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] || 23828679Sbrian ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] || 23928679Sbrian ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] || 24028679Sbrian THOFFSET(th) != THOFFSET(oth) || 24128679Sbrian (deltaS > 5 && 24228679Sbrian BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) || 24328679Sbrian (THOFFSET(th) > 5 && 24428679Sbrian BCMP(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) { 24528679Sbrian goto uncompressed; 24628679Sbrian } 2476059Samurai 24828679Sbrian /* 24928679Sbrian * Figure out which of the changing fields changed. The receiver expects 25028679Sbrian * changes in the order: urgent, window, ack, seq (the order minimizes the 25128679Sbrian * number of temporaries needed in this section of code). 25228679Sbrian */ 25328679Sbrian if (th->th_flags & TH_URG) { 25428679Sbrian deltaS = ntohs(th->th_urp); 25528679Sbrian ENCODEZ(deltaS); 25628679Sbrian changes |= NEW_U; 25728679Sbrian } else if (th->th_urp != oth->th_urp) { 2586059Samurai 25928679Sbrian /* 26028679Sbrian * argh! URG not set but urp changed -- a sensible implementation should 26128679Sbrian * never do this but RFC793 doesn't prohibit the change so we have to 26228679Sbrian * deal with it. 26328679Sbrian */ 26428679Sbrian goto uncompressed; 26528679Sbrian } 26628679Sbrian deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win)); 26728679Sbrian if (deltaS) { 26828679Sbrian ENCODE(deltaS); 26928679Sbrian changes |= NEW_W; 27028679Sbrian } 27128679Sbrian deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack); 27228679Sbrian if (deltaA) { 27328679Sbrian if (deltaA > 0xffff) { 27428679Sbrian goto uncompressed; 27528679Sbrian } 27628679Sbrian ENCODE(deltaA); 27728679Sbrian changes |= NEW_A; 27828679Sbrian } 27928679Sbrian deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq); 28028679Sbrian if (deltaS) { 28128679Sbrian if (deltaS > 0xffff) { 28228679Sbrian goto uncompressed; 28328679Sbrian } 28428679Sbrian ENCODE(deltaS); 28528679Sbrian changes |= NEW_S; 28628679Sbrian } 28728679Sbrian switch (changes) { 2886059Samurai 28928679Sbrian case 0: 2906059Samurai 29128679Sbrian /* 29228679Sbrian * Nothing changed. If this packet contains data and the last one didn't, 29328679Sbrian * this is probably a data packet following an ack (normal on an 29428679Sbrian * interactive connection) and we send it compressed. Otherwise it's 29528679Sbrian * probably a retransmit, retransmitted ack or window probe. Send it 29628679Sbrian * uncompressed in case the other side missed the compressed version. 29728679Sbrian */ 29828679Sbrian if (ip->ip_len != cs->cs_ip.ip_len && 29928679Sbrian ntohs(cs->cs_ip.ip_len) == hlen) 30028679Sbrian break; 3016059Samurai 30228679Sbrian /* (fall through) */ 3036059Samurai 30428679Sbrian case SPECIAL_I: 30528679Sbrian case SPECIAL_D: 3066059Samurai 30728679Sbrian /* 30828679Sbrian * actual changes match one of our special case encodings -- send packet 30928679Sbrian * uncompressed. 31028679Sbrian */ 31128679Sbrian goto uncompressed; 3126059Samurai 31328679Sbrian case NEW_S | NEW_A: 31428679Sbrian if (deltaS == deltaA && 31528679Sbrian deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 31628679Sbrian /* special case for echoed terminal traffic */ 31728679Sbrian changes = SPECIAL_I; 31828679Sbrian cp = new_seq; 31928679Sbrian } 32028679Sbrian break; 3216059Samurai 32228679Sbrian case NEW_S: 32328679Sbrian if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 32428679Sbrian /* special case for data xfer */ 32528679Sbrian changes = SPECIAL_D; 32628679Sbrian cp = new_seq; 32728679Sbrian } 32828679Sbrian break; 32928679Sbrian } 3306059Samurai 33128679Sbrian deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id); 33228679Sbrian if (deltaS != 1) { 33328679Sbrian ENCODEZ(deltaS); 33428679Sbrian changes |= NEW_I; 33528679Sbrian } 33628679Sbrian if (th->th_flags & TH_PUSH) 33728679Sbrian changes |= TCP_PUSH_BIT; 3386059Samurai 33928679Sbrian /* 34028679Sbrian * Grab the cksum before we overwrite it below. Then update our state with 34128679Sbrian * this packet's header. 34228679Sbrian */ 34328679Sbrian deltaA = ntohs(th->th_sum); 34428679Sbrian BCOPY(ip, &cs->cs_ip, hlen); 3456059Samurai 34628679Sbrian /* 34728679Sbrian * We want to use the original packet as our compressed packet. (cp - 34828679Sbrian * new_seq) is the number of bytes we need for compressed sequence numbers. 34928679Sbrian * In addition we need one byte for the change mask, one for the connection 35028679Sbrian * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header 35128679Sbrian * are needed. hlen is how many bytes of the original packet to toss so 35228679Sbrian * subtract the two to get the new packet size. 35328679Sbrian */ 35428679Sbrian deltaS = cp - new_seq; 35528679Sbrian cp = (u_char *) ip; 35628679Sbrian 35728679Sbrian /* 35828679Sbrian * Since fastq traffic can jump ahead of the background traffic, we don't 35928679Sbrian * know what order packets will go on the line. In this case, we always 36028679Sbrian * send a "new" connection id so the receiver state stays synchronized. 36128679Sbrian */ 36230187Sbrian if (comp->last_xmit == cs->cs_id && compress_cid) { 36328679Sbrian hlen -= deltaS + 3; 36428679Sbrian cp += hlen; 36528679Sbrian *cp++ = changes; 36630187Sbrian } else { 36728679Sbrian comp->last_xmit = cs->cs_id; 36828679Sbrian hlen -= deltaS + 4; 36928679Sbrian cp += hlen; 37028679Sbrian *cp++ = changes | NEW_C; 37128679Sbrian *cp++ = cs->cs_id; 37228679Sbrian } 37328679Sbrian m->cnt -= hlen; 37428679Sbrian m->offset += hlen; 37528679Sbrian *cp++ = deltaA >> 8; 37628679Sbrian *cp++ = deltaA; 37728679Sbrian BCOPY(new_seq, cp, deltaS); 37828679Sbrian INCR(sls_compressed) 37928679Sbrian return (TYPE_COMPRESSED_TCP); 3806059Samurai 38128679Sbrian /* 38228679Sbrian * Update connection state cs & send uncompressed packet ('uncompressed' 38328679Sbrian * means a regular ip/tcp packet but with the 'conversation id' we hope to 38428679Sbrian * use on future compressed packets in the protocol field). 38528679Sbrian */ 3866059Samuraiuncompressed: 38728679Sbrian BCOPY(ip, &cs->cs_ip, hlen); 38828679Sbrian ip->ip_p = cs->cs_id; 38928679Sbrian comp->last_xmit = cs->cs_id; 39028679Sbrian return (TYPE_UNCOMPRESSED_TCP); 3916059Samurai} 3926059Samurai 3936059Samurai 3946059Samuraiint 39528679Sbriansl_uncompress_tcp(u_char ** bufp, 39628679Sbrian int len, 39728679Sbrian u_int type, 39828679Sbrian struct slcompress * comp) 3996059Samurai{ 40028679Sbrian register u_char *cp; 40128679Sbrian register u_int hlen, changes; 40228679Sbrian register struct tcphdr *th; 40328679Sbrian register struct cstate *cs; 40428679Sbrian register struct ip *ip; 4056059Samurai 40628679Sbrian switch (type) { 4076059Samurai 40828679Sbrian case TYPE_UNCOMPRESSED_TCP: 40928679Sbrian ip = (struct ip *) * bufp; 41028679Sbrian if (ip->ip_p >= MAX_STATES) 41128679Sbrian goto bad; 41228679Sbrian cs = &comp->rstate[comp->last_recv = ip->ip_p]; 41328679Sbrian comp->flags &= ~SLF_TOSS; 41428679Sbrian ip->ip_p = IPPROTO_TCP; 4156059Samurai 41628679Sbrian /* 41728679Sbrian * Calculate the size of the TCP/IP header and make sure that we don't 41828679Sbrian * overflow the space we have available for it. 41928679Sbrian */ 42028679Sbrian hlen = ip->ip_hl << 2; 42128679Sbrian if (hlen + sizeof(struct tcphdr) > len) 42228679Sbrian goto bad; 42328679Sbrian th = (struct tcphdr *) & ((char *) ip)[hlen]; 42428679Sbrian hlen += THOFFSET(th) << 2; 42528679Sbrian if (hlen > MAX_HDR) 42628679Sbrian goto bad; 42728679Sbrian BCOPY(ip, &cs->cs_ip, hlen); 42830187Sbrian /* cs->cs_ip.ip_sum = 0; */ 42928679Sbrian cs->cs_hlen = hlen; 43028679Sbrian INCR(sls_uncompressedin) 43128679Sbrian return (len); 4326059Samurai 43328679Sbrian default: 43428679Sbrian goto bad; 4356059Samurai 43628679Sbrian case TYPE_COMPRESSED_TCP: 43728679Sbrian break; 43828679Sbrian } 43928679Sbrian /* We've got a compressed packet. */ 44028679Sbrian INCR(sls_compressedin) 44128679Sbrian cp = *bufp; 44228679Sbrian changes = *cp++; 44328679Sbrian LogPrintf(LogDEBUG, "compressed: changes = %02x\n", changes); 44428679Sbrian if (changes & NEW_C) { 4456059Samurai 44628679Sbrian /* 44728679Sbrian * Make sure the state index is in range, then grab the state. If we have 44828679Sbrian * a good state index, clear the 'discard' flag. 44928679Sbrian */ 45028679Sbrian if (*cp >= MAX_STATES || comp->last_recv == 255) 45128679Sbrian goto bad; 4526059Samurai 45328679Sbrian comp->flags &= ~SLF_TOSS; 45428679Sbrian comp->last_recv = *cp++; 45528679Sbrian } else { 4566059Samurai 45728679Sbrian /* 45828679Sbrian * this packet has an implicit state index. If we've had a line error 45928679Sbrian * since the last time we got an explicit state index, we have to toss 46028679Sbrian * the packet. 46128679Sbrian */ 46228679Sbrian if (comp->flags & SLF_TOSS) { 46328679Sbrian INCR(sls_tossed) 46428679Sbrian return (0); 46528679Sbrian } 46628679Sbrian } 46728679Sbrian cs = &comp->rstate[comp->last_recv]; 46828679Sbrian hlen = cs->cs_ip.ip_hl << 2; 46928679Sbrian th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen]; 47028679Sbrian th->th_sum = htons((*cp << 8) | cp[1]); 47128679Sbrian cp += 2; 47228679Sbrian if (changes & TCP_PUSH_BIT) 47328679Sbrian th->th_flags |= TH_PUSH; 47428679Sbrian else 47528679Sbrian th->th_flags &= ~TH_PUSH; 47628679Sbrian 47728679Sbrian switch (changes & SPECIALS_MASK) { 47828679Sbrian case SPECIAL_I: 47928679Sbrian { 48028679Sbrian register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; 48128679Sbrian 48228679Sbrian th->th_ack = htonl(ntohl(th->th_ack) + i); 48328679Sbrian th->th_seq = htonl(ntohl(th->th_seq) + i); 48428679Sbrian } 48528679Sbrian break; 48628679Sbrian 48728679Sbrian case SPECIAL_D: 48828679Sbrian th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) 48928679Sbrian - cs->cs_hlen); 49028679Sbrian break; 49128679Sbrian 49228679Sbrian default: 49328679Sbrian if (changes & NEW_U) { 49428679Sbrian th->th_flags |= TH_URG; 49528679Sbrian DECODEU(th->th_urp) 49628679Sbrian } else 49728679Sbrian th->th_flags &= ~TH_URG; 49828679Sbrian if (changes & NEW_W) 49928679Sbrian DECODES(th->th_win) 50028679Sbrian if (changes & NEW_A) 50128679Sbrian DECODEL(th->th_ack) 50228679Sbrian if (changes & NEW_S) { 50328679Sbrian LogPrintf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n", 50428679Sbrian *cp, cp[1], cp[2]); 50528679Sbrian DECODEL(th->th_seq) 5066059Samurai } 50728679Sbrian break; 50828679Sbrian } 50928679Sbrian if (changes & NEW_I) { 51028679Sbrian DECODES(cs->cs_ip.ip_id) 51128679Sbrian } else 51228679Sbrian cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1); 5136059Samurai 51428679Sbrian LogPrintf(LogDEBUG, "Uncompress: id = %04x, seq = %08x\n", 51528679Sbrian cs->cs_ip.ip_id, ntohl(th->th_seq)); 51626516Sbrian 51728679Sbrian /* 51828679Sbrian * At this point, cp points to the first byte of data in the packet. If 51928679Sbrian * we're not aligned on a 4-byte boundary, copy the data down so the ip & 52028679Sbrian * tcp headers will be aligned. Then back up cp by the tcp/ip header 52128679Sbrian * length to make room for the reconstructed header (we assume the packet 52228679Sbrian * we were handed has enough space to prepend 128 bytes of header). Adjust 52328679Sbrian * the length to account for the new header & fill in the IP total length. 52428679Sbrian */ 52528679Sbrian len -= (cp - *bufp); 52628679Sbrian if (len < 0) 5276059Samurai 52828679Sbrian /* 52928679Sbrian * we must have dropped some characters (crc should detect this but the 53028679Sbrian * old slip framing won't) 53128679Sbrian */ 53228679Sbrian goto bad; 53328679Sbrian 5346059Samurai#ifdef notdef 53528679Sbrian if ((int) cp & 3) { 53628679Sbrian if (len > 0) 53728679Sbrian (void) ovbcopy(cp, (caddr_t) ((int) cp & ~3), len); 53828679Sbrian cp = (u_char *) ((int) cp & ~3); 53928679Sbrian } 5406059Samurai#endif 5416059Samurai 54228679Sbrian cp -= cs->cs_hlen; 54328679Sbrian len += cs->cs_hlen; 54428679Sbrian cs->cs_ip.ip_len = htons(len); 54528679Sbrian BCOPY(&cs->cs_ip, cp, cs->cs_hlen); 54628679Sbrian *bufp = cp; 5476059Samurai 54828679Sbrian /* recompute the ip header checksum */ 54928679Sbrian { 55028679Sbrian register u_short *bp = (u_short *) cp; 55128679Sbrian 55230187Sbrian cs->cs_ip.ip_sum = 0; 55328679Sbrian for (changes = 0; hlen > 0; hlen -= 2) 55428679Sbrian changes += *bp++; 55528679Sbrian changes = (changes & 0xffff) + (changes >> 16); 55628679Sbrian changes = (changes & 0xffff) + (changes >> 16); 55728679Sbrian ((struct ip *) cp)->ip_sum = ~changes; 55828679Sbrian } 55928679Sbrian return (len); 5606059Samuraibad: 56128679Sbrian comp->flags |= SLF_TOSS; 56228679Sbrian INCR(sls_errorin) 56328679Sbrian return (0); 5646059Samurai} 5656059Samurai 5666059Samuraiint 5676059SamuraiReportCompress() 5686059Samurai{ 56926516Sbrian if (!VarTerm) 57026516Sbrian return 1; 57126516Sbrian 57226516Sbrian fprintf(VarTerm, "Out: %d (compress) / %d (total)", 57328679Sbrian slstat.sls_compressed, slstat.sls_packets); 57426516Sbrian fprintf(VarTerm, " %d (miss) / %d (search)\n", 57528679Sbrian slstat.sls_misses, slstat.sls_searches); 57626516Sbrian fprintf(VarTerm, "In: %d (compress), %d (uncompress)", 57728679Sbrian slstat.sls_compressedin, slstat.sls_uncompressedin); 57826516Sbrian fprintf(VarTerm, " %d (error), %d (tossed)\n", 57928679Sbrian slstat.sls_errorin, slstat.sls_tossed); 58026516Sbrian return 0; 5816059Samurai} 582