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