SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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