EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
4:fd826cad83c0

        

Who changed what in which revision?

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