First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darran 0:55a05330f8cc 1 /*
darran 0:55a05330f8cc 2 * Routines to compress and uncompess tcp packets (for transmission
darran 0:55a05330f8cc 3 * over low speed serial lines.
darran 0:55a05330f8cc 4 *
darran 0:55a05330f8cc 5 * Copyright (c) 1989 Regents of the University of California.
darran 0:55a05330f8cc 6 * All rights reserved.
darran 0:55a05330f8cc 7 *
darran 0:55a05330f8cc 8 * Redistribution and use in source and binary forms are permitted
darran 0:55a05330f8cc 9 * provided that the above copyright notice and this paragraph are
darran 0:55a05330f8cc 10 * duplicated in all such forms and that any documentation,
darran 0:55a05330f8cc 11 * advertising materials, and other materials related to such
darran 0:55a05330f8cc 12 * distribution and use acknowledge that the software was developed
darran 0:55a05330f8cc 13 * by the University of California, Berkeley. The name of the
darran 0:55a05330f8cc 14 * University may not be used to endorse or promote products derived
darran 0:55a05330f8cc 15 * from this software without specific prior written permission.
darran 0:55a05330f8cc 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
darran 0:55a05330f8cc 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
darran 0:55a05330f8cc 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
darran 0:55a05330f8cc 19 *
darran 0:55a05330f8cc 20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
darran 0:55a05330f8cc 21 * Initial distribution.
darran 0:55a05330f8cc 22 *
darran 0:55a05330f8cc 23 * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
darran 0:55a05330f8cc 24 * so that the entire packet being decompressed doesn't have
darran 0:55a05330f8cc 25 * to be in contiguous memory (just the compressed header).
darran 0:55a05330f8cc 26 *
darran 0:55a05330f8cc 27 * Modified March 1998 by Guy Lancaster, glanca@gesn.com,
darran 0:55a05330f8cc 28 * for a 16 bit processor.
darran 0:55a05330f8cc 29 */
darran 0:55a05330f8cc 30
darran 0:55a05330f8cc 31 #include "lwip/opt.h"
darran 0:55a05330f8cc 32
darran 0:55a05330f8cc 33 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
darran 0:55a05330f8cc 34
darran 0:55a05330f8cc 35 #include "ppp.h"
darran 0:55a05330f8cc 36 #include "pppdebug.h"
darran 0:55a05330f8cc 37
darran 0:55a05330f8cc 38 #include "vj.h"
darran 0:55a05330f8cc 39
darran 0:55a05330f8cc 40 #include <string.h>
darran 0:55a05330f8cc 41
darran 0:55a05330f8cc 42 #if VJ_SUPPORT
darran 0:55a05330f8cc 43
darran 0:55a05330f8cc 44 #if LINK_STATS
darran 0:55a05330f8cc 45 #define INCR(counter) ++comp->stats.counter
darran 0:55a05330f8cc 46 #else
darran 0:55a05330f8cc 47 #define INCR(counter)
darran 0:55a05330f8cc 48 #endif
darran 0:55a05330f8cc 49
darran 0:55a05330f8cc 50 void
darran 0:55a05330f8cc 51 vj_compress_init(struct vjcompress *comp)
darran 0:55a05330f8cc 52 {
darran 0:55a05330f8cc 53 register u_char i;
darran 0:55a05330f8cc 54 register struct cstate *tstate = comp->tstate;
darran 0:55a05330f8cc 55
darran 0:55a05330f8cc 56 #if MAX_SLOTS == 0
darran 0:55a05330f8cc 57 memset((char *)comp, 0, sizeof(*comp));
darran 0:55a05330f8cc 58 #endif
darran 0:55a05330f8cc 59 comp->maxSlotIndex = MAX_SLOTS - 1;
darran 0:55a05330f8cc 60 comp->compressSlot = 0; /* Disable slot ID compression by default. */
darran 0:55a05330f8cc 61 for (i = MAX_SLOTS - 1; i > 0; --i) {
darran 0:55a05330f8cc 62 tstate[i].cs_id = i;
darran 0:55a05330f8cc 63 tstate[i].cs_next = &tstate[i - 1];
darran 0:55a05330f8cc 64 }
darran 0:55a05330f8cc 65 tstate[0].cs_next = &tstate[MAX_SLOTS - 1];
darran 0:55a05330f8cc 66 tstate[0].cs_id = 0;
darran 0:55a05330f8cc 67 comp->last_cs = &tstate[0];
darran 0:55a05330f8cc 68 comp->last_recv = 255;
darran 0:55a05330f8cc 69 comp->last_xmit = 255;
darran 0:55a05330f8cc 70 comp->flags = VJF_TOSS;
darran 0:55a05330f8cc 71 }
darran 0:55a05330f8cc 72
darran 0:55a05330f8cc 73
darran 0:55a05330f8cc 74 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
darran 0:55a05330f8cc 75 * checks for zero (since zero has to be encoded in the long, 3 byte
darran 0:55a05330f8cc 76 * form).
darran 0:55a05330f8cc 77 */
darran 0:55a05330f8cc 78 #define ENCODE(n) { \
darran 0:55a05330f8cc 79 if ((u_short)(n) >= 256) { \
darran 0:55a05330f8cc 80 *cp++ = 0; \
darran 0:55a05330f8cc 81 cp[1] = (u_char)(n); \
darran 0:55a05330f8cc 82 cp[0] = (u_char)((n) >> 8); \
darran 0:55a05330f8cc 83 cp += 2; \
darran 0:55a05330f8cc 84 } else { \
darran 0:55a05330f8cc 85 *cp++ = (u_char)(n); \
darran 0:55a05330f8cc 86 } \
darran 0:55a05330f8cc 87 }
darran 0:55a05330f8cc 88 #define ENCODEZ(n) { \
darran 0:55a05330f8cc 89 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
darran 0:55a05330f8cc 90 *cp++ = 0; \
darran 0:55a05330f8cc 91 cp[1] = (u_char)(n); \
darran 0:55a05330f8cc 92 cp[0] = (u_char)((n) >> 8); \
darran 0:55a05330f8cc 93 cp += 2; \
darran 0:55a05330f8cc 94 } else { \
darran 0:55a05330f8cc 95 *cp++ = (u_char)(n); \
darran 0:55a05330f8cc 96 } \
darran 0:55a05330f8cc 97 }
darran 0:55a05330f8cc 98
darran 0:55a05330f8cc 99 #define DECODEL(f) { \
darran 0:55a05330f8cc 100 if (*cp == 0) {\
darran 0:55a05330f8cc 101 u32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
darran 0:55a05330f8cc 102 (f) = htonl(tmp); \
darran 0:55a05330f8cc 103 cp += 3; \
darran 0:55a05330f8cc 104 } else { \
darran 0:55a05330f8cc 105 u32_t tmp = ntohl(f) + (u32_t)*cp++; \
darran 0:55a05330f8cc 106 (f) = htonl(tmp); \
darran 0:55a05330f8cc 107 } \
darran 0:55a05330f8cc 108 }
darran 0:55a05330f8cc 109
darran 0:55a05330f8cc 110 #define DECODES(f) { \
darran 0:55a05330f8cc 111 if (*cp == 0) {\
darran 0:55a05330f8cc 112 u_short tmp = ntohs(f) + (((u_short)cp[1] << 8) | cp[2]); \
darran 0:55a05330f8cc 113 (f) = htons(tmp); \
darran 0:55a05330f8cc 114 cp += 3; \
darran 0:55a05330f8cc 115 } else { \
darran 0:55a05330f8cc 116 u_short tmp = ntohs(f) + (u_short)*cp++; \
darran 0:55a05330f8cc 117 (f) = htons(tmp); \
darran 0:55a05330f8cc 118 } \
darran 0:55a05330f8cc 119 }
darran 0:55a05330f8cc 120
darran 0:55a05330f8cc 121 #define DECODEU(f) { \
darran 0:55a05330f8cc 122 if (*cp == 0) {\
darran 0:55a05330f8cc 123 (f) = htons(((u_short)cp[1] << 8) | cp[2]); \
darran 0:55a05330f8cc 124 cp += 3; \
darran 0:55a05330f8cc 125 } else { \
darran 0:55a05330f8cc 126 (f) = htons((u_short)*cp++); \
darran 0:55a05330f8cc 127 } \
darran 0:55a05330f8cc 128 }
darran 0:55a05330f8cc 129
darran 0:55a05330f8cc 130 /*
darran 0:55a05330f8cc 131 * vj_compress_tcp - Attempt to do Van Jacobson header compression on a
darran 0:55a05330f8cc 132 * packet. This assumes that nb and comp are not null and that the first
darran 0:55a05330f8cc 133 * buffer of the chain contains a valid IP header.
darran 0:55a05330f8cc 134 * Return the VJ type code indicating whether or not the packet was
darran 0:55a05330f8cc 135 * compressed.
darran 0:55a05330f8cc 136 */
darran 0:55a05330f8cc 137 u_int
darran 0:55a05330f8cc 138 vj_compress_tcp(struct vjcompress *comp, struct pbuf *pb)
darran 0:55a05330f8cc 139 {
darran 0:55a05330f8cc 140 register struct ip_hdr *ip = (struct ip_hdr *)pb->payload;
darran 0:55a05330f8cc 141 register struct cstate *cs = comp->last_cs->cs_next;
darran 0:55a05330f8cc 142 register u_short hlen = IPH_HL(ip);
darran 0:55a05330f8cc 143 register struct tcp_hdr *oth;
darran 0:55a05330f8cc 144 register struct tcp_hdr *th;
darran 0:55a05330f8cc 145 register u_short deltaS, deltaA;
darran 0:55a05330f8cc 146 register u_long deltaL;
darran 0:55a05330f8cc 147 register u_int changes = 0;
darran 0:55a05330f8cc 148 u_char new_seq[16];
darran 0:55a05330f8cc 149 register u_char *cp = new_seq;
darran 0:55a05330f8cc 150
darran 0:55a05330f8cc 151 /*
darran 0:55a05330f8cc 152 * Check that the packet is IP proto TCP.
darran 0:55a05330f8cc 153 */
darran 0:55a05330f8cc 154 if (IPH_PROTO(ip) != IP_PROTO_TCP) {
darran 0:55a05330f8cc 155 return (TYPE_IP);
darran 0:55a05330f8cc 156 }
darran 0:55a05330f8cc 157
darran 0:55a05330f8cc 158 /*
darran 0:55a05330f8cc 159 * Bail if this is an IP fragment or if the TCP packet isn't
darran 0:55a05330f8cc 160 * `compressible' (i.e., ACK isn't set or some other control bit is
darran 0:55a05330f8cc 161 * set).
darran 0:55a05330f8cc 162 */
darran 0:55a05330f8cc 163 if ((IPH_OFFSET(ip) & htons(0x3fff)) || pb->tot_len < 40) {
darran 0:55a05330f8cc 164 return (TYPE_IP);
darran 0:55a05330f8cc 165 }
darran 0:55a05330f8cc 166 th = (struct tcp_hdr *)&((long *)ip)[hlen];
darran 0:55a05330f8cc 167 if ((TCPH_FLAGS(th) & (TCP_SYN|TCP_FIN|TCP_RST|TCP_ACK)) != TCP_ACK) {
darran 0:55a05330f8cc 168 return (TYPE_IP);
darran 0:55a05330f8cc 169 }
darran 0:55a05330f8cc 170 /*
darran 0:55a05330f8cc 171 * Packet is compressible -- we're going to send either a
darran 0:55a05330f8cc 172 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
darran 0:55a05330f8cc 173 * to locate (or create) the connection state. Special case the
darran 0:55a05330f8cc 174 * most recently used connection since it's most likely to be used
darran 0:55a05330f8cc 175 * again & we don't have to do any reordering if it's used.
darran 0:55a05330f8cc 176 */
darran 0:55a05330f8cc 177 INCR(vjs_packets);
darran 0:55a05330f8cc 178 if (!ip_addr_cmp(&ip->src, &cs->cs_ip.src)
darran 0:55a05330f8cc 179 || !ip_addr_cmp(&ip->dest, &cs->cs_ip.dest)
darran 0:55a05330f8cc 180 || *(long *)th != ((long *)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]) {
darran 0:55a05330f8cc 181 /*
darran 0:55a05330f8cc 182 * Wasn't the first -- search for it.
darran 0:55a05330f8cc 183 *
darran 0:55a05330f8cc 184 * States are kept in a circularly linked list with
darran 0:55a05330f8cc 185 * last_cs pointing to the end of the list. The
darran 0:55a05330f8cc 186 * list is kept in lru order by moving a state to the
darran 0:55a05330f8cc 187 * head of the list whenever it is referenced. Since
darran 0:55a05330f8cc 188 * the list is short and, empirically, the connection
darran 0:55a05330f8cc 189 * we want is almost always near the front, we locate
darran 0:55a05330f8cc 190 * states via linear search. If we don't find a state
darran 0:55a05330f8cc 191 * for the datagram, the oldest state is (re-)used.
darran 0:55a05330f8cc 192 */
darran 0:55a05330f8cc 193 register struct cstate *lcs;
darran 0:55a05330f8cc 194 register struct cstate *lastcs = comp->last_cs;
darran 0:55a05330f8cc 195
darran 0:55a05330f8cc 196 do {
darran 0:55a05330f8cc 197 lcs = cs; cs = cs->cs_next;
darran 0:55a05330f8cc 198 INCR(vjs_searches);
darran 0:55a05330f8cc 199 if (ip_addr_cmp(&ip->src, &cs->cs_ip.src)
darran 0:55a05330f8cc 200 && ip_addr_cmp(&ip->dest, &cs->cs_ip.dest)
darran 0:55a05330f8cc 201 && *(long *)th == ((long *)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]) {
darran 0:55a05330f8cc 202 goto found;
darran 0:55a05330f8cc 203 }
darran 0:55a05330f8cc 204 } while (cs != lastcs);
darran 0:55a05330f8cc 205
darran 0:55a05330f8cc 206 /*
darran 0:55a05330f8cc 207 * Didn't find it -- re-use oldest cstate. Send an
darran 0:55a05330f8cc 208 * uncompressed packet that tells the other side what
darran 0:55a05330f8cc 209 * connection number we're using for this conversation.
darran 0:55a05330f8cc 210 * Note that since the state list is circular, the oldest
darran 0:55a05330f8cc 211 * state points to the newest and we only need to set
darran 0:55a05330f8cc 212 * last_cs to update the lru linkage.
darran 0:55a05330f8cc 213 */
darran 0:55a05330f8cc 214 INCR(vjs_misses);
darran 0:55a05330f8cc 215 comp->last_cs = lcs;
darran 0:55a05330f8cc 216 hlen += TCPH_OFFSET(th);
darran 0:55a05330f8cc 217 hlen <<= 2;
darran 0:55a05330f8cc 218 /* Check that the IP/TCP headers are contained in the first buffer. */
darran 0:55a05330f8cc 219 if (hlen > pb->len) {
darran 0:55a05330f8cc 220 return (TYPE_IP);
darran 0:55a05330f8cc 221 }
darran 0:55a05330f8cc 222 goto uncompressed;
darran 0:55a05330f8cc 223
darran 0:55a05330f8cc 224 found:
darran 0:55a05330f8cc 225 /*
darran 0:55a05330f8cc 226 * Found it -- move to the front on the connection list.
darran 0:55a05330f8cc 227 */
darran 0:55a05330f8cc 228 if (cs == lastcs) {
darran 0:55a05330f8cc 229 comp->last_cs = lcs;
darran 0:55a05330f8cc 230 } else {
darran 0:55a05330f8cc 231 lcs->cs_next = cs->cs_next;
darran 0:55a05330f8cc 232 cs->cs_next = lastcs->cs_next;
darran 0:55a05330f8cc 233 lastcs->cs_next = cs;
darran 0:55a05330f8cc 234 }
darran 0:55a05330f8cc 235 }
darran 0:55a05330f8cc 236
darran 0:55a05330f8cc 237 oth = (struct tcp_hdr *)&((long *)&cs->cs_ip)[hlen];
darran 0:55a05330f8cc 238 deltaS = hlen;
darran 0:55a05330f8cc 239 hlen += TCPH_OFFSET(th);
darran 0:55a05330f8cc 240 hlen <<= 2;
darran 0:55a05330f8cc 241 /* Check that the IP/TCP headers are contained in the first buffer. */
darran 0:55a05330f8cc 242 if (hlen > pb->len) {
darran 0:55a05330f8cc 243 PPPDEBUG(LOG_INFO, ("vj_compress_tcp: header len %d spans buffers\n", hlen));
darran 0:55a05330f8cc 244 return (TYPE_IP);
darran 0:55a05330f8cc 245 }
darran 0:55a05330f8cc 246
darran 0:55a05330f8cc 247 /*
darran 0:55a05330f8cc 248 * Make sure that only what we expect to change changed. The first
darran 0:55a05330f8cc 249 * line of the `if' checks the IP protocol version, header length &
darran 0:55a05330f8cc 250 * type of service. The 2nd line checks the "Don't fragment" bit.
darran 0:55a05330f8cc 251 * The 3rd line checks the time-to-live and protocol (the protocol
darran 0:55a05330f8cc 252 * check is unnecessary but costless). The 4th line checks the TCP
darran 0:55a05330f8cc 253 * header length. The 5th line checks IP options, if any. The 6th
darran 0:55a05330f8cc 254 * line checks TCP options, if any. If any of these things are
darran 0:55a05330f8cc 255 * different between the previous & current datagram, we send the
darran 0:55a05330f8cc 256 * current datagram `uncompressed'.
darran 0:55a05330f8cc 257 */
darran 0:55a05330f8cc 258 if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0]
darran 0:55a05330f8cc 259 || ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3]
darran 0:55a05330f8cc 260 || ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4]
darran 0:55a05330f8cc 261 || TCPH_OFFSET(th) != TCPH_OFFSET(oth)
darran 0:55a05330f8cc 262 || (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2))
darran 0:55a05330f8cc 263 || (TCPH_OFFSET(th) > 5 && BCMP(th + 1, oth + 1, (TCPH_OFFSET(th) - 5) << 2))) {
darran 0:55a05330f8cc 264 goto uncompressed;
darran 0:55a05330f8cc 265 }
darran 0:55a05330f8cc 266
darran 0:55a05330f8cc 267 /*
darran 0:55a05330f8cc 268 * Figure out which of the changing fields changed. The
darran 0:55a05330f8cc 269 * receiver expects changes in the order: urgent, window,
darran 0:55a05330f8cc 270 * ack, seq (the order minimizes the number of temporaries
darran 0:55a05330f8cc 271 * needed in this section of code).
darran 0:55a05330f8cc 272 */
darran 0:55a05330f8cc 273 if (TCPH_FLAGS(th) & TCP_URG) {
darran 0:55a05330f8cc 274 deltaS = ntohs(th->urgp);
darran 0:55a05330f8cc 275 ENCODEZ(deltaS);
darran 0:55a05330f8cc 276 changes |= NEW_U;
darran 0:55a05330f8cc 277 } else if (th->urgp != oth->urgp) {
darran 0:55a05330f8cc 278 /* argh! URG not set but urp changed -- a sensible
darran 0:55a05330f8cc 279 * implementation should never do this but RFC793
darran 0:55a05330f8cc 280 * doesn't prohibit the change so we have to deal
darran 0:55a05330f8cc 281 * with it. */
darran 0:55a05330f8cc 282 goto uncompressed;
darran 0:55a05330f8cc 283 }
darran 0:55a05330f8cc 284
darran 0:55a05330f8cc 285 if ((deltaS = (u_short)(ntohs(th->wnd) - ntohs(oth->wnd))) != 0) {
darran 0:55a05330f8cc 286 ENCODE(deltaS);
darran 0:55a05330f8cc 287 changes |= NEW_W;
darran 0:55a05330f8cc 288 }
darran 0:55a05330f8cc 289
darran 0:55a05330f8cc 290 if ((deltaL = ntohl(th->ackno) - ntohl(oth->ackno)) != 0) {
darran 0:55a05330f8cc 291 if (deltaL > 0xffff) {
darran 0:55a05330f8cc 292 goto uncompressed;
darran 0:55a05330f8cc 293 }
darran 0:55a05330f8cc 294 deltaA = (u_short)deltaL;
darran 0:55a05330f8cc 295 ENCODE(deltaA);
darran 0:55a05330f8cc 296 changes |= NEW_A;
darran 0:55a05330f8cc 297 }
darran 0:55a05330f8cc 298
darran 0:55a05330f8cc 299 if ((deltaL = ntohl(th->seqno) - ntohl(oth->seqno)) != 0) {
darran 0:55a05330f8cc 300 if (deltaL > 0xffff) {
darran 0:55a05330f8cc 301 goto uncompressed;
darran 0:55a05330f8cc 302 }
darran 0:55a05330f8cc 303 deltaS = (u_short)deltaL;
darran 0:55a05330f8cc 304 ENCODE(deltaS);
darran 0:55a05330f8cc 305 changes |= NEW_S;
darran 0:55a05330f8cc 306 }
darran 0:55a05330f8cc 307
darran 0:55a05330f8cc 308 switch(changes) {
darran 0:55a05330f8cc 309 case 0:
darran 0:55a05330f8cc 310 /*
darran 0:55a05330f8cc 311 * Nothing changed. If this packet contains data and the
darran 0:55a05330f8cc 312 * last one didn't, this is probably a data packet following
darran 0:55a05330f8cc 313 * an ack (normal on an interactive connection) and we send
darran 0:55a05330f8cc 314 * it compressed. Otherwise it's probably a retransmit,
darran 0:55a05330f8cc 315 * retransmitted ack or window probe. Send it uncompressed
darran 0:55a05330f8cc 316 * in case the other side missed the compressed version.
darran 0:55a05330f8cc 317 */
darran 0:55a05330f8cc 318 if (IPH_LEN(ip) != IPH_LEN(&cs->cs_ip) &&
darran 0:55a05330f8cc 319 ntohs(IPH_LEN(&cs->cs_ip)) == hlen) {
darran 0:55a05330f8cc 320 break;
darran 0:55a05330f8cc 321 }
darran 0:55a05330f8cc 322
darran 0:55a05330f8cc 323 /* (fall through) */
darran 0:55a05330f8cc 324
darran 0:55a05330f8cc 325 case SPECIAL_I:
darran 0:55a05330f8cc 326 case SPECIAL_D:
darran 0:55a05330f8cc 327 /*
darran 0:55a05330f8cc 328 * actual changes match one of our special case encodings --
darran 0:55a05330f8cc 329 * send packet uncompressed.
darran 0:55a05330f8cc 330 */
darran 0:55a05330f8cc 331 goto uncompressed;
darran 0:55a05330f8cc 332
darran 0:55a05330f8cc 333 case NEW_S|NEW_A:
darran 0:55a05330f8cc 334 if (deltaS == deltaA && deltaS == ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
darran 0:55a05330f8cc 335 /* special case for echoed terminal traffic */
darran 0:55a05330f8cc 336 changes = SPECIAL_I;
darran 0:55a05330f8cc 337 cp = new_seq;
darran 0:55a05330f8cc 338 }
darran 0:55a05330f8cc 339 break;
darran 0:55a05330f8cc 340
darran 0:55a05330f8cc 341 case NEW_S:
darran 0:55a05330f8cc 342 if (deltaS == ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
darran 0:55a05330f8cc 343 /* special case for data xfer */
darran 0:55a05330f8cc 344 changes = SPECIAL_D;
darran 0:55a05330f8cc 345 cp = new_seq;
darran 0:55a05330f8cc 346 }
darran 0:55a05330f8cc 347 break;
darran 0:55a05330f8cc 348 }
darran 0:55a05330f8cc 349
darran 0:55a05330f8cc 350 deltaS = (u_short)(ntohs(IPH_ID(ip)) - ntohs(IPH_ID(&cs->cs_ip)));
darran 0:55a05330f8cc 351 if (deltaS != 1) {
darran 0:55a05330f8cc 352 ENCODEZ(deltaS);
darran 0:55a05330f8cc 353 changes |= NEW_I;
darran 0:55a05330f8cc 354 }
darran 0:55a05330f8cc 355 if (TCPH_FLAGS(th) & TCP_PSH) {
darran 0:55a05330f8cc 356 changes |= TCP_PUSH_BIT;
darran 0:55a05330f8cc 357 }
darran 0:55a05330f8cc 358 /*
darran 0:55a05330f8cc 359 * Grab the cksum before we overwrite it below. Then update our
darran 0:55a05330f8cc 360 * state with this packet's header.
darran 0:55a05330f8cc 361 */
darran 0:55a05330f8cc 362 deltaA = ntohs(th->chksum);
darran 0:55a05330f8cc 363 BCOPY(ip, &cs->cs_ip, hlen);
darran 0:55a05330f8cc 364
darran 0:55a05330f8cc 365 /*
darran 0:55a05330f8cc 366 * We want to use the original packet as our compressed packet.
darran 0:55a05330f8cc 367 * (cp - new_seq) is the number of bytes we need for compressed
darran 0:55a05330f8cc 368 * sequence numbers. In addition we need one byte for the change
darran 0:55a05330f8cc 369 * mask, one for the connection id and two for the tcp checksum.
darran 0:55a05330f8cc 370 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
darran 0:55a05330f8cc 371 * many bytes of the original packet to toss so subtract the two to
darran 0:55a05330f8cc 372 * get the new packet size.
darran 0:55a05330f8cc 373 */
darran 0:55a05330f8cc 374 deltaS = (u_short)(cp - new_seq);
darran 0:55a05330f8cc 375 if (!comp->compressSlot || comp->last_xmit != cs->cs_id) {
darran 0:55a05330f8cc 376 comp->last_xmit = cs->cs_id;
darran 0:55a05330f8cc 377 hlen -= deltaS + 4;
darran 0:55a05330f8cc 378 if(pbuf_header(pb, -hlen)){
darran 0:55a05330f8cc 379 /* Can we cope with this failing? Just assert for now */
darran 0:55a05330f8cc 380 LWIP_ASSERT("pbuf_header failed\n", 0);
darran 0:55a05330f8cc 381 }
darran 0:55a05330f8cc 382 cp = (u_char *)pb->payload;
darran 0:55a05330f8cc 383 *cp++ = (u_char)(changes | NEW_C);
darran 0:55a05330f8cc 384 *cp++ = cs->cs_id;
darran 0:55a05330f8cc 385 } else {
darran 0:55a05330f8cc 386 hlen -= deltaS + 3;
darran 0:55a05330f8cc 387 if(pbuf_header(pb, -hlen)) {
darran 0:55a05330f8cc 388 /* Can we cope with this failing? Just assert for now */
darran 0:55a05330f8cc 389 LWIP_ASSERT("pbuf_header failed\n", 0);
darran 0:55a05330f8cc 390 }
darran 0:55a05330f8cc 391 cp = (u_char *)pb->payload;
darran 0:55a05330f8cc 392 *cp++ = (u_char)changes;
darran 0:55a05330f8cc 393 }
darran 0:55a05330f8cc 394 *cp++ = (u_char)(deltaA >> 8);
darran 0:55a05330f8cc 395 *cp++ = (u_char)deltaA;
darran 0:55a05330f8cc 396 BCOPY(new_seq, cp, deltaS);
darran 0:55a05330f8cc 397 INCR(vjs_compressed);
darran 0:55a05330f8cc 398 return (TYPE_COMPRESSED_TCP);
darran 0:55a05330f8cc 399
darran 0:55a05330f8cc 400 /*
darran 0:55a05330f8cc 401 * Update connection state cs & send uncompressed packet (that is,
darran 0:55a05330f8cc 402 * a regular ip/tcp packet but with the 'conversation id' we hope
darran 0:55a05330f8cc 403 * to use on future compressed packets in the protocol field).
darran 0:55a05330f8cc 404 */
darran 0:55a05330f8cc 405 uncompressed:
darran 0:55a05330f8cc 406 BCOPY(ip, &cs->cs_ip, hlen);
darran 0:55a05330f8cc 407 IPH_PROTO_SET(ip, cs->cs_id);
darran 0:55a05330f8cc 408 comp->last_xmit = cs->cs_id;
darran 0:55a05330f8cc 409 return (TYPE_UNCOMPRESSED_TCP);
darran 0:55a05330f8cc 410 }
darran 0:55a05330f8cc 411
darran 0:55a05330f8cc 412 /*
darran 0:55a05330f8cc 413 * Called when we may have missed a packet.
darran 0:55a05330f8cc 414 */
darran 0:55a05330f8cc 415 void
darran 0:55a05330f8cc 416 vj_uncompress_err(struct vjcompress *comp)
darran 0:55a05330f8cc 417 {
darran 0:55a05330f8cc 418 comp->flags |= VJF_TOSS;
darran 0:55a05330f8cc 419 INCR(vjs_errorin);
darran 0:55a05330f8cc 420 }
darran 0:55a05330f8cc 421
darran 0:55a05330f8cc 422 /*
darran 0:55a05330f8cc 423 * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
darran 0:55a05330f8cc 424 * Return 0 on success, -1 on failure.
darran 0:55a05330f8cc 425 */
darran 0:55a05330f8cc 426 int
darran 0:55a05330f8cc 427 vj_uncompress_uncomp(struct pbuf *nb, struct vjcompress *comp)
darran 0:55a05330f8cc 428 {
darran 0:55a05330f8cc 429 register u_int hlen;
darran 0:55a05330f8cc 430 register struct cstate *cs;
darran 0:55a05330f8cc 431 register struct ip_hdr *ip;
darran 0:55a05330f8cc 432
darran 0:55a05330f8cc 433 ip = (struct ip_hdr *)nb->payload;
darran 0:55a05330f8cc 434 hlen = IPH_HL(ip) << 2;
darran 0:55a05330f8cc 435 if (IPH_PROTO(ip) >= MAX_SLOTS
darran 0:55a05330f8cc 436 || hlen + sizeof(struct tcp_hdr) > nb->len
darran 0:55a05330f8cc 437 || (hlen += TCPH_OFFSET(((struct tcp_hdr *)&((char *)ip)[hlen])) << 2)
darran 0:55a05330f8cc 438 > nb->len
darran 0:55a05330f8cc 439 || hlen > MAX_HDR) {
darran 0:55a05330f8cc 440 PPPDEBUG(LOG_INFO, ("vj_uncompress_uncomp: bad cid=%d, hlen=%d buflen=%d\n",
darran 0:55a05330f8cc 441 IPH_PROTO(ip), hlen, nb->len));
darran 0:55a05330f8cc 442 comp->flags |= VJF_TOSS;
darran 0:55a05330f8cc 443 INCR(vjs_errorin);
darran 0:55a05330f8cc 444 return -1;
darran 0:55a05330f8cc 445 }
darran 0:55a05330f8cc 446 cs = &comp->rstate[comp->last_recv = IPH_PROTO(ip)];
darran 0:55a05330f8cc 447 comp->flags &=~ VJF_TOSS;
darran 0:55a05330f8cc 448 IPH_PROTO_SET(ip, IP_PROTO_TCP);
darran 0:55a05330f8cc 449 BCOPY(ip, &cs->cs_ip, hlen);
darran 0:55a05330f8cc 450 cs->cs_hlen = (u_short)hlen;
darran 0:55a05330f8cc 451 INCR(vjs_uncompressedin);
darran 0:55a05330f8cc 452 return 0;
darran 0:55a05330f8cc 453 }
darran 0:55a05330f8cc 454
darran 0:55a05330f8cc 455 /*
darran 0:55a05330f8cc 456 * Uncompress a packet of type TYPE_COMPRESSED_TCP.
darran 0:55a05330f8cc 457 * The packet is composed of a buffer chain and the first buffer
darran 0:55a05330f8cc 458 * must contain an accurate chain length.
darran 0:55a05330f8cc 459 * The first buffer must include the entire compressed TCP/IP header.
darran 0:55a05330f8cc 460 * This procedure replaces the compressed header with the uncompressed
darran 0:55a05330f8cc 461 * header and returns the length of the VJ header.
darran 0:55a05330f8cc 462 */
darran 0:55a05330f8cc 463 int
darran 0:55a05330f8cc 464 vj_uncompress_tcp(struct pbuf **nb, struct vjcompress *comp)
darran 0:55a05330f8cc 465 {
darran 0:55a05330f8cc 466 u_char *cp;
darran 0:55a05330f8cc 467 struct tcp_hdr *th;
darran 0:55a05330f8cc 468 struct cstate *cs;
darran 0:55a05330f8cc 469 u_short *bp;
darran 0:55a05330f8cc 470 struct pbuf *n0 = *nb;
darran 0:55a05330f8cc 471 u32_t tmp;
darran 0:55a05330f8cc 472 u_int vjlen, hlen, changes;
darran 0:55a05330f8cc 473
darran 0:55a05330f8cc 474 INCR(vjs_compressedin);
darran 0:55a05330f8cc 475 cp = (u_char *)n0->payload;
darran 0:55a05330f8cc 476 changes = *cp++;
darran 0:55a05330f8cc 477 if (changes & NEW_C) {
darran 0:55a05330f8cc 478 /*
darran 0:55a05330f8cc 479 * Make sure the state index is in range, then grab the state.
darran 0:55a05330f8cc 480 * If we have a good state index, clear the 'discard' flag.
darran 0:55a05330f8cc 481 */
darran 0:55a05330f8cc 482 if (*cp >= MAX_SLOTS) {
darran 0:55a05330f8cc 483 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: bad cid=%d\n", *cp));
darran 0:55a05330f8cc 484 goto bad;
darran 0:55a05330f8cc 485 }
darran 0:55a05330f8cc 486
darran 0:55a05330f8cc 487 comp->flags &=~ VJF_TOSS;
darran 0:55a05330f8cc 488 comp->last_recv = *cp++;
darran 0:55a05330f8cc 489 } else {
darran 0:55a05330f8cc 490 /*
darran 0:55a05330f8cc 491 * this packet has an implicit state index. If we've
darran 0:55a05330f8cc 492 * had a line error since the last time we got an
darran 0:55a05330f8cc 493 * explicit state index, we have to toss the packet.
darran 0:55a05330f8cc 494 */
darran 0:55a05330f8cc 495 if (comp->flags & VJF_TOSS) {
darran 0:55a05330f8cc 496 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: tossing\n"));
darran 0:55a05330f8cc 497 INCR(vjs_tossed);
darran 0:55a05330f8cc 498 return (-1);
darran 0:55a05330f8cc 499 }
darran 0:55a05330f8cc 500 }
darran 0:55a05330f8cc 501 cs = &comp->rstate[comp->last_recv];
darran 0:55a05330f8cc 502 hlen = IPH_HL(&cs->cs_ip) << 2;
darran 0:55a05330f8cc 503 th = (struct tcp_hdr *)&((u_char *)&cs->cs_ip)[hlen];
darran 0:55a05330f8cc 504 th->chksum = htons((*cp << 8) | cp[1]);
darran 0:55a05330f8cc 505 cp += 2;
darran 0:55a05330f8cc 506 if (changes & TCP_PUSH_BIT) {
darran 0:55a05330f8cc 507 TCPH_SET_FLAG(th, TCP_PSH);
darran 0:55a05330f8cc 508 } else {
darran 0:55a05330f8cc 509 TCPH_UNSET_FLAG(th, TCP_PSH);
darran 0:55a05330f8cc 510 }
darran 0:55a05330f8cc 511
darran 0:55a05330f8cc 512 switch (changes & SPECIALS_MASK) {
darran 0:55a05330f8cc 513 case SPECIAL_I:
darran 0:55a05330f8cc 514 {
darran 0:55a05330f8cc 515 register u32_t i = ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
darran 0:55a05330f8cc 516 /* some compilers can't nest inline assembler.. */
darran 0:55a05330f8cc 517 tmp = ntohl(th->ackno) + i;
darran 0:55a05330f8cc 518 th->ackno = htonl(tmp);
darran 0:55a05330f8cc 519 tmp = ntohl(th->seqno) + i;
darran 0:55a05330f8cc 520 th->seqno = htonl(tmp);
darran 0:55a05330f8cc 521 }
darran 0:55a05330f8cc 522 break;
darran 0:55a05330f8cc 523
darran 0:55a05330f8cc 524 case SPECIAL_D:
darran 0:55a05330f8cc 525 /* some compilers can't nest inline assembler.. */
darran 0:55a05330f8cc 526 tmp = ntohl(th->seqno) + ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
darran 0:55a05330f8cc 527 th->seqno = htonl(tmp);
darran 0:55a05330f8cc 528 break;
darran 0:55a05330f8cc 529
darran 0:55a05330f8cc 530 default:
darran 0:55a05330f8cc 531 if (changes & NEW_U) {
darran 0:55a05330f8cc 532 TCPH_SET_FLAG(th, TCP_URG);
darran 0:55a05330f8cc 533 DECODEU(th->urgp);
darran 0:55a05330f8cc 534 } else {
darran 0:55a05330f8cc 535 TCPH_UNSET_FLAG(th, TCP_URG);
darran 0:55a05330f8cc 536 }
darran 0:55a05330f8cc 537 if (changes & NEW_W) {
darran 0:55a05330f8cc 538 DECODES(th->wnd);
darran 0:55a05330f8cc 539 }
darran 0:55a05330f8cc 540 if (changes & NEW_A) {
darran 0:55a05330f8cc 541 DECODEL(th->ackno);
darran 0:55a05330f8cc 542 }
darran 0:55a05330f8cc 543 if (changes & NEW_S) {
darran 0:55a05330f8cc 544 DECODEL(th->seqno);
darran 0:55a05330f8cc 545 }
darran 0:55a05330f8cc 546 break;
darran 0:55a05330f8cc 547 }
darran 0:55a05330f8cc 548 if (changes & NEW_I) {
darran 0:55a05330f8cc 549 DECODES(cs->cs_ip._id);
darran 0:55a05330f8cc 550 } else {
darran 0:55a05330f8cc 551 IPH_ID_SET(&cs->cs_ip, ntohs(IPH_ID(&cs->cs_ip)) + 1);
darran 0:55a05330f8cc 552 IPH_ID_SET(&cs->cs_ip, htons(IPH_ID(&cs->cs_ip)));
darran 0:55a05330f8cc 553 }
darran 0:55a05330f8cc 554
darran 0:55a05330f8cc 555 /*
darran 0:55a05330f8cc 556 * At this point, cp points to the first byte of data in the
darran 0:55a05330f8cc 557 * packet. Fill in the IP total length and update the IP
darran 0:55a05330f8cc 558 * header checksum.
darran 0:55a05330f8cc 559 */
darran 0:55a05330f8cc 560 vjlen = (u_short)(cp - (u_char*)n0->payload);
darran 0:55a05330f8cc 561 if (n0->len < vjlen) {
darran 0:55a05330f8cc 562 /*
darran 0:55a05330f8cc 563 * We must have dropped some characters (crc should detect
darran 0:55a05330f8cc 564 * this but the old slip framing won't)
darran 0:55a05330f8cc 565 */
darran 0:55a05330f8cc 566 PPPDEBUG(LOG_INFO, ("vj_uncompress_tcp: head buffer %d too short %d\n",
darran 0:55a05330f8cc 567 n0->len, vjlen));
darran 0:55a05330f8cc 568 goto bad;
darran 0:55a05330f8cc 569 }
darran 0:55a05330f8cc 570
darran 0:55a05330f8cc 571 #if BYTE_ORDER == LITTLE_ENDIAN
darran 0:55a05330f8cc 572 tmp = n0->tot_len - vjlen + cs->cs_hlen;
darran 0:55a05330f8cc 573 IPH_LEN_SET(&cs->cs_ip, htons((u_short)tmp));
darran 0:55a05330f8cc 574 #else
darran 0:55a05330f8cc 575 IPH_LEN_SET(&cs->cs_ip, htons(n0->tot_len - vjlen + cs->cs_hlen));
darran 0:55a05330f8cc 576 #endif
darran 0:55a05330f8cc 577
darran 0:55a05330f8cc 578 /* recompute the ip header checksum */
darran 0:55a05330f8cc 579 bp = (u_short *) &cs->cs_ip;
darran 0:55a05330f8cc 580 IPH_CHKSUM_SET(&cs->cs_ip, 0);
darran 0:55a05330f8cc 581 for (tmp = 0; hlen > 0; hlen -= 2) {
darran 0:55a05330f8cc 582 tmp += *bp++;
darran 0:55a05330f8cc 583 }
darran 0:55a05330f8cc 584 tmp = (tmp & 0xffff) + (tmp >> 16);
darran 0:55a05330f8cc 585 tmp = (tmp & 0xffff) + (tmp >> 16);
darran 0:55a05330f8cc 586 IPH_CHKSUM_SET(&cs->cs_ip, (u_short)(~tmp));
darran 0:55a05330f8cc 587
darran 0:55a05330f8cc 588 /* Remove the compressed header and prepend the uncompressed header. */
darran 0:55a05330f8cc 589 if(pbuf_header(n0, -((s16_t)(vjlen)))) {
darran 0:55a05330f8cc 590 /* Can we cope with this failing? Just assert for now */
darran 0:55a05330f8cc 591 LWIP_ASSERT("pbuf_header failed\n", 0);
darran 0:55a05330f8cc 592 goto bad;
darran 0:55a05330f8cc 593 }
darran 0:55a05330f8cc 594
darran 0:55a05330f8cc 595 if(LWIP_MEM_ALIGN(n0->payload) != n0->payload) {
darran 0:55a05330f8cc 596 struct pbuf *np, *q;
darran 0:55a05330f8cc 597 u8_t *bufptr;
darran 0:55a05330f8cc 598
darran 0:55a05330f8cc 599 np = pbuf_alloc(PBUF_RAW, n0->len + cs->cs_hlen, PBUF_POOL);
darran 0:55a05330f8cc 600 if(!np) {
darran 0:55a05330f8cc 601 PPPDEBUG(LOG_WARNING, ("vj_uncompress_tcp: realign failed\n"));
darran 0:55a05330f8cc 602 goto bad;
darran 0:55a05330f8cc 603 }
darran 0:55a05330f8cc 604
darran 0:55a05330f8cc 605 if(pbuf_header(np, -cs->cs_hlen)) {
darran 0:55a05330f8cc 606 /* Can we cope with this failing? Just assert for now */
darran 0:55a05330f8cc 607 LWIP_ASSERT("pbuf_header failed\n", 0);
darran 0:55a05330f8cc 608 goto bad;
darran 0:55a05330f8cc 609 }
darran 0:55a05330f8cc 610
darran 0:55a05330f8cc 611 bufptr = (u8_t*) n0->payload;
darran 0:55a05330f8cc 612 for(q = np; q != NULL; q = q->next) {
darran 0:55a05330f8cc 613 MEMCPY(q->payload, bufptr, q->len);
darran 0:55a05330f8cc 614 bufptr += q->len;
darran 0:55a05330f8cc 615 }
darran 0:55a05330f8cc 616
darran 0:55a05330f8cc 617 if(n0->next) {
darran 0:55a05330f8cc 618 pbuf_chain(np, n0->next);
darran 0:55a05330f8cc 619 pbuf_dechain(n0);
darran 0:55a05330f8cc 620 }
darran 0:55a05330f8cc 621 pbuf_free(n0);
darran 0:55a05330f8cc 622 n0 = np;
darran 0:55a05330f8cc 623 }
darran 0:55a05330f8cc 624
darran 0:55a05330f8cc 625 if(pbuf_header(n0, cs->cs_hlen)) {
darran 0:55a05330f8cc 626 struct pbuf *np;
darran 0:55a05330f8cc 627
darran 0:55a05330f8cc 628 LWIP_ASSERT("vj_uncompress_tcp: cs->cs_hlen <= PBUF_POOL_BUFSIZE", cs->cs_hlen <= PBUF_POOL_BUFSIZE);
darran 0:55a05330f8cc 629 np = pbuf_alloc(PBUF_RAW, cs->cs_hlen, PBUF_POOL);
darran 0:55a05330f8cc 630 if(!np) {
darran 0:55a05330f8cc 631 PPPDEBUG(LOG_WARNING, ("vj_uncompress_tcp: prepend failed\n"));
darran 0:55a05330f8cc 632 goto bad;
darran 0:55a05330f8cc 633 }
darran 0:55a05330f8cc 634 pbuf_cat(np, n0);
darran 0:55a05330f8cc 635 n0 = np;
darran 0:55a05330f8cc 636 }
darran 0:55a05330f8cc 637 LWIP_ASSERT("n0->len >= cs->cs_hlen", n0->len >= cs->cs_hlen);
darran 0:55a05330f8cc 638 MEMCPY(n0->payload, &cs->cs_ip, cs->cs_hlen);
darran 0:55a05330f8cc 639
darran 0:55a05330f8cc 640 *nb = n0;
darran 0:55a05330f8cc 641
darran 0:55a05330f8cc 642 return vjlen;
darran 0:55a05330f8cc 643
darran 0:55a05330f8cc 644 bad:
darran 0:55a05330f8cc 645 comp->flags |= VJF_TOSS;
darran 0:55a05330f8cc 646 INCR(vjs_errorin);
darran 0:55a05330f8cc 647 return (-1);
darran 0:55a05330f8cc 648 }
darran 0:55a05330f8cc 649
darran 0:55a05330f8cc 650 #endif /* VJ_SUPPORT */
darran 0:55a05330f8cc 651
darran 0:55a05330f8cc 652 #endif /* PPP_SUPPORT */