Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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