ZG2100 Network interface source

Committer:
donatien
Date:
Fri Aug 06 10:23:41 2010 +0000
Revision:
1:3a7c15057192
Parent:
0:b802fc31f1db

        

Who changed what in which revision?

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