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