Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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