NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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