slcompress.c revision 54912
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 * 2050479Speter * $FreeBSD: head/usr.sbin/ppp/slcompress.c 54912 1999-12-20 20:29:47Z brian $ 218857Srgrimes * 226059Samurai * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: 236059Samurai * - Initial distribution. 246059Samurai */ 2528679Sbrian 2643313Sbrian#include <sys/param.h> 276059Samurai#include <netinet/in_systm.h> 286059Samurai#include <netinet/in.h> 296059Samurai#include <netinet/tcp.h> 306059Samurai#include <netinet/ip.h> 3136285Sbrian#include <sys/un.h> 3230715Sbrian 3330715Sbrian#include <stdio.h> 3430715Sbrian#include <string.h> 3536285Sbrian#include <termios.h> 3630715Sbrian 3746686Sbrian#include "layer.h" 3837009Sbrian#include "defs.h" 3931343Sbrian#include "command.h" 4030715Sbrian#include "mbuf.h" 4130715Sbrian#include "log.h" 426059Samurai#include "slcompress.h" 4336285Sbrian#include "descriptor.h" 4436285Sbrian#include "prompt.h" 4536285Sbrian#include "timer.h" 4636285Sbrian#include "fsm.h" 4736285Sbrian#include "throughput.h" 4836285Sbrian#include "iplist.h" 4938557Sbrian#include "lqr.h" 5038557Sbrian#include "hdlc.h" 5136285Sbrian#include "ipcp.h" 5236285Sbrian#include "filter.h" 5336285Sbrian#include "lcp.h" 5436285Sbrian#include "ccp.h" 5536285Sbrian#include "link.h" 5636285Sbrian#include "mp.h" 5743313Sbrian#ifndef NORADIUS 5843313Sbrian#include "radius.h" 5943313Sbrian#endif 6036285Sbrian#include "bundle.h" 616059Samurai 626059Samuraivoid 6346828Sbriansl_compress_init(struct slcompress *comp, int max_state) 646059Samurai{ 6528679Sbrian register u_int i; 6628679Sbrian register struct cstate *tstate = comp->tstate; 676059Samurai 6831962Sbrian memset(comp, '\0', sizeof *comp); 6930187Sbrian for (i = max_state; i > 0; --i) { 7028679Sbrian tstate[i].cs_id = i; 7128679Sbrian tstate[i].cs_next = &tstate[i - 1]; 7228679Sbrian } 7330187Sbrian tstate[0].cs_next = &tstate[max_state]; 7428679Sbrian tstate[0].cs_id = 0; 7528679Sbrian comp->last_cs = &tstate[0]; 7628679Sbrian comp->last_recv = 255; 7728679Sbrian comp->last_xmit = 255; 7828679Sbrian comp->flags = SLF_TOSS; 796059Samurai} 806059Samurai 816059Samurai 826059Samurai/* ENCODE encodes a number that is known to be non-zero. ENCODEZ 8337189Sbrian * checks for zero (since zero has to be encoded in the 32-bit, 3 byte 846059Samurai * form). 856059Samurai */ 866059Samurai#define ENCODE(n) { \ 876059Samurai if ((u_short)(n) >= 256) { \ 886059Samurai *cp++ = 0; \ 896059Samurai cp[1] = (n); \ 906059Samurai cp[0] = (n) >> 8; \ 916059Samurai cp += 2; \ 926059Samurai } else { \ 936059Samurai *cp++ = (n); \ 946059Samurai } \ 956059Samurai} 966059Samurai#define ENCODEZ(n) { \ 976059Samurai if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \ 986059Samurai *cp++ = 0; \ 996059Samurai cp[1] = (n); \ 1006059Samurai cp[0] = (n) >> 8; \ 1016059Samurai cp += 2; \ 1026059Samurai } else { \ 1036059Samurai *cp++ = (n); \ 1046059Samurai } \ 1056059Samurai} 1066059Samurai 1076059Samurai#define DECODEL(f) { \ 1086059Samurai if (*cp == 0) {\ 1096059Samurai (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \ 1106059Samurai cp += 3; \ 1116059Samurai } else { \ 11237189Sbrian (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \ 1136059Samurai } \ 1146059Samurai} 1156059Samurai 1166059Samurai#define DECODES(f) { \ 1176059Samurai if (*cp == 0) {\ 1186059Samurai (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \ 1196059Samurai cp += 3; \ 1206059Samurai } else { \ 12137189Sbrian (f) = htons(ntohs(f) + (u_int32_t)*cp++); \ 1226059Samurai } \ 1236059Samurai} 1246059Samurai 1256059Samurai#define DECODEU(f) { \ 1266059Samurai if (*cp == 0) {\ 1276059Samurai (f) = htons((cp[1] << 8) | cp[2]); \ 1286059Samurai cp += 3; \ 1296059Samurai } else { \ 13037189Sbrian (f) = htons((u_int32_t)*cp++); \ 1316059Samurai } \ 1326059Samurai} 1336059Samurai 1346059Samurai 1356059Samuraiu_char 13628679Sbriansl_compress_tcp(struct mbuf * m, 13728679Sbrian struct ip * ip, 13836285Sbrian struct slcompress *comp, 13936285Sbrian struct slstat *slstat, 14028679Sbrian int compress_cid) 1416059Samurai{ 14228679Sbrian register struct cstate *cs = comp->last_cs->cs_next; 14328679Sbrian register u_int hlen = ip->ip_hl; 14428679Sbrian register struct tcphdr *oth; 14528679Sbrian register struct tcphdr *th; 14628679Sbrian register u_int deltaS, deltaA; 14728679Sbrian register u_int changes = 0; 14828679Sbrian u_char new_seq[16]; 14928679Sbrian register u_char *cp = new_seq; 1506059Samurai 15128679Sbrian /* 15228679Sbrian * Bail if this is an IP fragment or if the TCP packet isn't `compressible' 15328679Sbrian * (i.e., ACK isn't set or some other control bit is set). (We assume that 15428679Sbrian * the caller has already made sure the packet is IP proto TCP). 15528679Sbrian */ 15654912Sbrian if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) { 15754912Sbrian log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %d\n", 15854912Sbrian ip->ip_off, m->m_len); 15936285Sbrian log_DumpBp(LogDEBUG, "", m); 16028679Sbrian return (TYPE_IP); 16128679Sbrian } 16228679Sbrian th = (struct tcphdr *) & ((int *) ip)[hlen]; 16328679Sbrian if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) { 16436285Sbrian log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags); 16536285Sbrian log_DumpBp(LogDEBUG, "", m); 16628679Sbrian return (TYPE_IP); 16728679Sbrian } 1686059Samurai 16928679Sbrian /* 17028679Sbrian * Packet is compressible -- we're going to send either a COMPRESSED_TCP or 17128679Sbrian * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the 17228679Sbrian * connection state. Special case the most recently used connection since 17328679Sbrian * it's most likely to be used again & we don't have to do any reordering 17428679Sbrian * if it's used. 17528679Sbrian */ 17636285Sbrian slstat->sls_packets++; 17736285Sbrian if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr || 17836285Sbrian ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr || 17936285Sbrian *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) { 1806059Samurai 18128679Sbrian /* 18228679Sbrian * Wasn't the first -- search for it. 18328679Sbrian * 18428679Sbrian * States are kept in a circularly linked list with last_cs pointing to the 18528679Sbrian * end of the list. The list is kept in lru order by moving a state to 18628679Sbrian * the head of the list whenever it is referenced. Since the list is 18728679Sbrian * short and, empirically, the connection we want is almost always near 18828679Sbrian * the front, we locate states via linear search. If we don't find a 18928679Sbrian * state for the datagram, the oldest state is (re-)used. 19028679Sbrian */ 19128679Sbrian register struct cstate *lcs; 19228679Sbrian register struct cstate *lastcs = comp->last_cs; 1936059Samurai 19428679Sbrian do { 19528679Sbrian lcs = cs; 19628679Sbrian cs = cs->cs_next; 19736285Sbrian slstat->sls_searches++; 19836285Sbrian if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr 19936285Sbrian && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr 20036285Sbrian && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) 20128679Sbrian goto found; 20228679Sbrian } while (cs != lastcs); 2036059Samurai 20428679Sbrian /* 20528679Sbrian * Didn't find it -- re-use oldest cstate. Send an uncompressed packet 20628679Sbrian * that tells the other side what connection number we're using for this 20728679Sbrian * conversation. Note that since the state list is circular, the oldest 20828679Sbrian * state points to the newest and we only need to set last_cs to update 20928679Sbrian * the lru linkage. 21028679Sbrian */ 21136285Sbrian slstat->sls_misses++; 21228679Sbrian comp->last_cs = lcs; 2136059Samurai#define THOFFSET(th) (th->th_off) 21428679Sbrian hlen += th->th_off; 21528679Sbrian hlen <<= 2; 21654912Sbrian if (hlen > m->m_len) 21728679Sbrian return (TYPE_IP); 21828679Sbrian goto uncompressed; 2196059Samurai 22028679Sbrianfound: 2216059Samurai 22228679Sbrian /* 22328679Sbrian * Found it -- move to the front on the connection list. 22428679Sbrian */ 22528679Sbrian if (cs == lastcs) 22628679Sbrian comp->last_cs = lcs; 22728679Sbrian else { 22828679Sbrian lcs->cs_next = cs->cs_next; 22928679Sbrian cs->cs_next = lastcs->cs_next; 23028679Sbrian lastcs->cs_next = cs; 23128679Sbrian } 23228679Sbrian } 2336059Samurai 23428679Sbrian /* 23528679Sbrian * Make sure that only what we expect to change changed. The first line of 23628679Sbrian * the `if' checks the IP protocol version, header length & type of 23728679Sbrian * service. The 2nd line checks the "Don't fragment" bit. The 3rd line 23828679Sbrian * checks the time-to-live and protocol (the protocol check is unnecessary 23928679Sbrian * but costless). The 4th line checks the TCP header length. The 5th line 24028679Sbrian * checks IP options, if any. The 6th line checks TCP options, if any. If 24128679Sbrian * any of these things are different between the previous & current 24228679Sbrian * datagram, we send the current datagram `uncompressed'. 24328679Sbrian */ 24428679Sbrian oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen]; 24528679Sbrian deltaS = hlen; 24628679Sbrian hlen += th->th_off; 24728679Sbrian hlen <<= 2; 24854912Sbrian if (hlen > m->m_len) 24928679Sbrian return (TYPE_IP); 2506059Samurai 25128679Sbrian if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] || 25228679Sbrian ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] || 25328679Sbrian ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] || 25428679Sbrian THOFFSET(th) != THOFFSET(oth) || 25528679Sbrian (deltaS > 5 && 25630715Sbrian memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) || 25728679Sbrian (THOFFSET(th) > 5 && 25830715Sbrian memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) { 25928679Sbrian goto uncompressed; 26028679Sbrian } 2616059Samurai 26228679Sbrian /* 26328679Sbrian * Figure out which of the changing fields changed. The receiver expects 26428679Sbrian * changes in the order: urgent, window, ack, seq (the order minimizes the 26528679Sbrian * number of temporaries needed in this section of code). 26628679Sbrian */ 26728679Sbrian if (th->th_flags & TH_URG) { 26828679Sbrian deltaS = ntohs(th->th_urp); 26928679Sbrian ENCODEZ(deltaS); 27028679Sbrian changes |= NEW_U; 27128679Sbrian } else if (th->th_urp != oth->th_urp) { 2726059Samurai 27328679Sbrian /* 27428679Sbrian * argh! URG not set but urp changed -- a sensible implementation should 27528679Sbrian * never do this but RFC793 doesn't prohibit the change so we have to 27628679Sbrian * deal with it. 27728679Sbrian */ 27828679Sbrian goto uncompressed; 27928679Sbrian } 28028679Sbrian deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win)); 28128679Sbrian if (deltaS) { 28228679Sbrian ENCODE(deltaS); 28328679Sbrian changes |= NEW_W; 28428679Sbrian } 28528679Sbrian deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack); 28628679Sbrian if (deltaA) { 28728679Sbrian if (deltaA > 0xffff) { 28828679Sbrian goto uncompressed; 28928679Sbrian } 29028679Sbrian ENCODE(deltaA); 29128679Sbrian changes |= NEW_A; 29228679Sbrian } 29328679Sbrian deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq); 29428679Sbrian if (deltaS) { 29528679Sbrian if (deltaS > 0xffff) { 29628679Sbrian goto uncompressed; 29728679Sbrian } 29828679Sbrian ENCODE(deltaS); 29928679Sbrian changes |= NEW_S; 30028679Sbrian } 30128679Sbrian switch (changes) { 3026059Samurai 30328679Sbrian case 0: 3046059Samurai 30528679Sbrian /* 30628679Sbrian * Nothing changed. If this packet contains data and the last one didn't, 30728679Sbrian * this is probably a data packet following an ack (normal on an 30828679Sbrian * interactive connection) and we send it compressed. Otherwise it's 30928679Sbrian * probably a retransmit, retransmitted ack or window probe. Send it 31028679Sbrian * uncompressed in case the other side missed the compressed version. 31128679Sbrian */ 31228679Sbrian if (ip->ip_len != cs->cs_ip.ip_len && 31328679Sbrian ntohs(cs->cs_ip.ip_len) == hlen) 31428679Sbrian break; 3156059Samurai 31628679Sbrian /* (fall through) */ 3176059Samurai 31828679Sbrian case SPECIAL_I: 31928679Sbrian case SPECIAL_D: 3206059Samurai 32128679Sbrian /* 32228679Sbrian * actual changes match one of our special case encodings -- send packet 32328679Sbrian * uncompressed. 32428679Sbrian */ 32528679Sbrian goto uncompressed; 3266059Samurai 32728679Sbrian case NEW_S | NEW_A: 32828679Sbrian if (deltaS == deltaA && 32928679Sbrian deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 33028679Sbrian /* special case for echoed terminal traffic */ 33128679Sbrian changes = SPECIAL_I; 33228679Sbrian cp = new_seq; 33328679Sbrian } 33428679Sbrian break; 3356059Samurai 33628679Sbrian case NEW_S: 33728679Sbrian if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 33828679Sbrian /* special case for data xfer */ 33928679Sbrian changes = SPECIAL_D; 34028679Sbrian cp = new_seq; 34128679Sbrian } 34228679Sbrian break; 34328679Sbrian } 3446059Samurai 34528679Sbrian deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id); 34628679Sbrian if (deltaS != 1) { 34728679Sbrian ENCODEZ(deltaS); 34828679Sbrian changes |= NEW_I; 34928679Sbrian } 35028679Sbrian if (th->th_flags & TH_PUSH) 35128679Sbrian changes |= TCP_PUSH_BIT; 3526059Samurai 35328679Sbrian /* 35428679Sbrian * Grab the cksum before we overwrite it below. Then update our state with 35528679Sbrian * this packet's header. 35628679Sbrian */ 35728679Sbrian deltaA = ntohs(th->th_sum); 35830715Sbrian memcpy(&cs->cs_ip, ip, hlen); 3596059Samurai 36028679Sbrian /* 36128679Sbrian * We want to use the original packet as our compressed packet. (cp - 36228679Sbrian * new_seq) is the number of bytes we need for compressed sequence numbers. 36328679Sbrian * In addition we need one byte for the change mask, one for the connection 36428679Sbrian * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header 36528679Sbrian * are needed. hlen is how many bytes of the original packet to toss so 36628679Sbrian * subtract the two to get the new packet size. 36728679Sbrian */ 36828679Sbrian deltaS = cp - new_seq; 36928679Sbrian cp = (u_char *) ip; 37028679Sbrian 37128679Sbrian /* 37228679Sbrian * Since fastq traffic can jump ahead of the background traffic, we don't 37328679Sbrian * know what order packets will go on the line. In this case, we always 37428679Sbrian * send a "new" connection id so the receiver state stays synchronized. 37528679Sbrian */ 37630187Sbrian if (comp->last_xmit == cs->cs_id && compress_cid) { 37728679Sbrian hlen -= deltaS + 3; 37828679Sbrian cp += hlen; 37928679Sbrian *cp++ = changes; 38030187Sbrian } else { 38128679Sbrian comp->last_xmit = cs->cs_id; 38228679Sbrian hlen -= deltaS + 4; 38328679Sbrian cp += hlen; 38428679Sbrian *cp++ = changes | NEW_C; 38528679Sbrian *cp++ = cs->cs_id; 38628679Sbrian } 38754912Sbrian m->m_len -= hlen; 38854912Sbrian m->m_offset += hlen; 38928679Sbrian *cp++ = deltaA >> 8; 39028679Sbrian *cp++ = deltaA; 39130715Sbrian memcpy(cp, new_seq, deltaS); 39236285Sbrian slstat->sls_compressed++; 39336285Sbrian return (TYPE_COMPRESSED_TCP); 3946059Samurai 39528679Sbrian /* 39628679Sbrian * Update connection state cs & send uncompressed packet ('uncompressed' 39728679Sbrian * means a regular ip/tcp packet but with the 'conversation id' we hope to 39828679Sbrian * use on future compressed packets in the protocol field). 39928679Sbrian */ 4006059Samuraiuncompressed: 40130715Sbrian memcpy(&cs->cs_ip, ip, hlen); 40228679Sbrian ip->ip_p = cs->cs_id; 40328679Sbrian comp->last_xmit = cs->cs_id; 40428679Sbrian return (TYPE_UNCOMPRESSED_TCP); 4056059Samurai} 4066059Samurai 4076059Samurai 4086059Samuraiint 40936960Sbriansl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp, 41036960Sbrian struct slstat *slstat, int max_state) 4116059Samurai{ 41228679Sbrian register u_char *cp; 41328679Sbrian register u_int hlen, changes; 41428679Sbrian register struct tcphdr *th; 41528679Sbrian register struct cstate *cs; 41628679Sbrian register struct ip *ip; 41752197Sbrian u_short *bp; 4186059Samurai 41928679Sbrian switch (type) { 4206059Samurai 42128679Sbrian case TYPE_UNCOMPRESSED_TCP: 42228679Sbrian ip = (struct ip *) * bufp; 42336960Sbrian if (ip->ip_p > max_state) 42428679Sbrian goto bad; 42528679Sbrian cs = &comp->rstate[comp->last_recv = ip->ip_p]; 42628679Sbrian comp->flags &= ~SLF_TOSS; 42728679Sbrian ip->ip_p = IPPROTO_TCP; 4286059Samurai 42928679Sbrian /* 43028679Sbrian * Calculate the size of the TCP/IP header and make sure that we don't 43128679Sbrian * overflow the space we have available for it. 43228679Sbrian */ 43328679Sbrian hlen = ip->ip_hl << 2; 43428679Sbrian if (hlen + sizeof(struct tcphdr) > len) 43528679Sbrian goto bad; 43628679Sbrian th = (struct tcphdr *) & ((char *) ip)[hlen]; 43728679Sbrian hlen += THOFFSET(th) << 2; 43828679Sbrian if (hlen > MAX_HDR) 43928679Sbrian goto bad; 44030715Sbrian memcpy(&cs->cs_ip, ip, hlen); 44128679Sbrian cs->cs_hlen = hlen; 44236285Sbrian slstat->sls_uncompressedin++; 44336285Sbrian return (len); 4446059Samurai 44528679Sbrian default: 44628679Sbrian goto bad; 4476059Samurai 44828679Sbrian case TYPE_COMPRESSED_TCP: 44928679Sbrian break; 45028679Sbrian } 45145138Sbrian 45228679Sbrian /* We've got a compressed packet. */ 45336285Sbrian slstat->sls_compressedin++; 45436285Sbrian cp = *bufp; 45528679Sbrian changes = *cp++; 45636285Sbrian log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes); 45745138Sbrian 45828679Sbrian if (changes & NEW_C) { 45928679Sbrian /* 46028679Sbrian * Make sure the state index is in range, then grab the state. If we have 46128679Sbrian * a good state index, clear the 'discard' flag. 46228679Sbrian */ 46346686Sbrian if (*cp > max_state || comp->last_recv == 255) 46428679Sbrian goto bad; 4656059Samurai 46628679Sbrian comp->flags &= ~SLF_TOSS; 46728679Sbrian comp->last_recv = *cp++; 46828679Sbrian } else { 46928679Sbrian /* 47028679Sbrian * this packet has an implicit state index. If we've had a line error 47128679Sbrian * since the last time we got an explicit state index, we have to toss 47228679Sbrian * the packet. 47328679Sbrian */ 47428679Sbrian if (comp->flags & SLF_TOSS) { 47536285Sbrian slstat->sls_tossed++; 47636285Sbrian return (0); 47728679Sbrian } 47828679Sbrian } 47928679Sbrian cs = &comp->rstate[comp->last_recv]; 48028679Sbrian hlen = cs->cs_ip.ip_hl << 2; 48128679Sbrian th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen]; 48228679Sbrian th->th_sum = htons((*cp << 8) | cp[1]); 48328679Sbrian cp += 2; 48428679Sbrian if (changes & TCP_PUSH_BIT) 48528679Sbrian th->th_flags |= TH_PUSH; 48628679Sbrian else 48728679Sbrian th->th_flags &= ~TH_PUSH; 48828679Sbrian 48928679Sbrian switch (changes & SPECIALS_MASK) { 49028679Sbrian case SPECIAL_I: 49128679Sbrian { 49228679Sbrian register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; 49328679Sbrian 49428679Sbrian th->th_ack = htonl(ntohl(th->th_ack) + i); 49528679Sbrian th->th_seq = htonl(ntohl(th->th_seq) + i); 49628679Sbrian } 49728679Sbrian break; 49828679Sbrian 49928679Sbrian case SPECIAL_D: 50028679Sbrian th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) 50128679Sbrian - cs->cs_hlen); 50228679Sbrian break; 50328679Sbrian 50428679Sbrian default: 50528679Sbrian if (changes & NEW_U) { 50628679Sbrian th->th_flags |= TH_URG; 50728679Sbrian DECODEU(th->th_urp) 50828679Sbrian } else 50928679Sbrian th->th_flags &= ~TH_URG; 51028679Sbrian if (changes & NEW_W) 51128679Sbrian DECODES(th->th_win) 51228679Sbrian if (changes & NEW_A) 51328679Sbrian DECODEL(th->th_ack) 51428679Sbrian if (changes & NEW_S) { 51536285Sbrian log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n", 51628679Sbrian *cp, cp[1], cp[2]); 51728679Sbrian DECODEL(th->th_seq) 5186059Samurai } 51928679Sbrian break; 52028679Sbrian } 52128679Sbrian if (changes & NEW_I) { 52228679Sbrian DECODES(cs->cs_ip.ip_id) 52328679Sbrian } else 52428679Sbrian cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1); 5256059Samurai 52636285Sbrian log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n", 52736285Sbrian cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq)); 52826516Sbrian 52928679Sbrian /* 53045138Sbrian * At this point, cp points to the first byte of data in the packet. 53145138Sbrian * Back up cp by the tcp/ip header length to make room for the 53245138Sbrian * reconstructed header (we assume the packet we were handed has enough 53345138Sbrian * space to prepend 128 bytes of header). Adjust the length to account 53445138Sbrian * for the new header & fill in the IP total length. 53528679Sbrian */ 53628679Sbrian len -= (cp - *bufp); 53728679Sbrian if (len < 0) 53828679Sbrian /* 53928679Sbrian * we must have dropped some characters (crc should detect this but the 54028679Sbrian * old slip framing won't) 54128679Sbrian */ 54228679Sbrian goto bad; 54328679Sbrian 54452197Sbrian *bufp = cp - cs->cs_hlen; 54528679Sbrian len += cs->cs_hlen; 54628679Sbrian cs->cs_ip.ip_len = htons(len); 5476059Samurai 54828679Sbrian /* recompute the ip header checksum */ 54952197Sbrian cs->cs_ip.ip_sum = 0; 55052197Sbrian bp = (u_short *)&cs->cs_ip; 55152197Sbrian for (changes = 0; hlen > 0; hlen -= 2) 55252197Sbrian changes += *bp++; 55352197Sbrian changes = (changes & 0xffff) + (changes >> 16); 55452197Sbrian changes = (changes & 0xffff) + (changes >> 16); 55552197Sbrian cs->cs_ip.ip_sum = ~changes; 55628679Sbrian 55752197Sbrian /* And copy the result into our buffer */ 55852197Sbrian memcpy(*bufp, &cs->cs_ip, cs->cs_hlen); 55945185Sbrian 56028679Sbrian return (len); 5616059Samuraibad: 56228679Sbrian comp->flags |= SLF_TOSS; 56336285Sbrian slstat->sls_errorin++; 56436285Sbrian return (0); 5656059Samurai} 5666059Samurai 5676059Samuraiint 56836285Sbriansl_Show(struct cmdargs const *arg) 5696059Samurai{ 57036285Sbrian prompt_Printf(arg->prompt, "VJ compression statistics:\n"); 57136285Sbrian prompt_Printf(arg->prompt, " Out: %d (compress) / %d (total)", 57236285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_compressed, 57336285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_packets); 57436285Sbrian prompt_Printf(arg->prompt, " %d (miss) / %d (search)\n", 57536285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_misses, 57636285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_searches); 57736285Sbrian prompt_Printf(arg->prompt, " In: %d (compress), %d (uncompress)", 57836285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin, 57936285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin); 58036285Sbrian prompt_Printf(arg->prompt, " %d (error), %d (tossed)\n", 58136285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_errorin, 58236285Sbrian arg->bundle->ncp.ipcp.vj.slstat.sls_tossed); 58326516Sbrian return 0; 5846059Samurai} 585