slcompress.c revision 1.21 1 1.21 augustss /* $NetBSD: slcompress.c,v 1.21 2000/03/30 09:45:41 augustss Exp $ */
2 1.16 christos /* Id: slcompress.c,v 1.3 1996/05/24 07:04:47 paulus Exp */
3 1.9 cgd
4 1.8 mycroft /*
5 1.8 mycroft * Copyright (c) 1989, 1993, 1994
6 1.8 mycroft * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.4 deraadt * Redistribution and use in source and binary forms, with or without
9 1.4 deraadt * modification, are permitted provided that the following conditions
10 1.4 deraadt * are met:
11 1.4 deraadt * 1. Redistributions of source code must retain the above copyright
12 1.4 deraadt * notice, this list of conditions and the following disclaimer.
13 1.4 deraadt * 2. Redistributions in binary form must reproduce the above copyright
14 1.4 deraadt * notice, this list of conditions and the following disclaimer in the
15 1.4 deraadt * documentation and/or other materials provided with the distribution.
16 1.4 deraadt * 3. All advertising materials mentioning features or use of this software
17 1.4 deraadt * must display the following acknowledgement:
18 1.4 deraadt * This product includes software developed by the University of
19 1.4 deraadt * California, Berkeley and its contributors.
20 1.4 deraadt * 4. Neither the name of the University nor the names of its contributors
21 1.4 deraadt * may be used to endorse or promote products derived from this software
22 1.4 deraadt * without specific prior written permission.
23 1.1 cgd *
24 1.4 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.4 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.4 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.4 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.4 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.4 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.4 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.4 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.4 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.4 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.4 deraadt * SUCH DAMAGE.
35 1.4 deraadt *
36 1.9 cgd * @(#)slcompress.c 8.2 (Berkeley) 4/16/94
37 1.4 deraadt */
38 1.4 deraadt
39 1.4 deraadt /*
40 1.4 deraadt * Routines to compress and uncompess tcp packets (for transmission
41 1.4 deraadt * over low speed serial lines.
42 1.4 deraadt *
43 1.4 deraadt * Van Jacobson (van (at) helios.ee.lbl.gov), Dec 31, 1989:
44 1.8 mycroft * - Initial distribution.
45 1.1 cgd */
46 1.8 mycroft
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <sys/mbuf.h>
49 1.14 christos #include <sys/systm.h>
50 1.5 mycroft
51 1.1 cgd #include <netinet/in.h>
52 1.1 cgd #include <netinet/in_systm.h>
53 1.1 cgd #include <netinet/ip.h>
54 1.1 cgd #include <netinet/tcp.h>
55 1.1 cgd
56 1.5 mycroft #include <net/slcompress.h>
57 1.1 cgd
58 1.1 cgd #ifndef SL_NO_STATS
59 1.1 cgd #define INCR(counter) ++comp->counter;
60 1.1 cgd #else
61 1.1 cgd #define INCR(counter)
62 1.1 cgd #endif
63 1.1 cgd
64 1.1 cgd #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
65 1.1 cgd #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
66 1.1 cgd
67 1.17 christos
68 1.1 cgd void
69 1.17 christos sl_compress_init(comp)
70 1.7 paulus struct slcompress *comp;
71 1.17 christos {
72 1.21 augustss u_int i;
73 1.21 augustss struct cstate *tstate = comp->tstate;
74 1.17 christos
75 1.17 christos bzero((char *)comp, sizeof(*comp));
76 1.17 christos for (i = MAX_STATES - 1; i > 0; --i) {
77 1.17 christos tstate[i].cs_id = i;
78 1.17 christos tstate[i].cs_next = &tstate[i - 1];
79 1.17 christos }
80 1.17 christos tstate[0].cs_next = &tstate[MAX_STATES - 1];
81 1.17 christos tstate[0].cs_id = 0;
82 1.17 christos comp->last_cs = &tstate[0];
83 1.17 christos comp->last_recv = 255;
84 1.17 christos comp->last_xmit = 255;
85 1.17 christos comp->flags = SLF_TOSS;
86 1.17 christos }
87 1.17 christos
88 1.17 christos
89 1.17 christos /*
90 1.17 christos * Like sl_compress_init, but we get to specify the maximum connection
91 1.17 christos * ID to use on transmission.
92 1.17 christos */
93 1.17 christos void
94 1.17 christos sl_compress_setup(comp, max_state)
95 1.17 christos struct slcompress *comp;
96 1.17 christos int max_state;
97 1.7 paulus {
98 1.21 augustss u_int i;
99 1.21 augustss struct cstate *tstate = comp->tstate;
100 1.7 paulus
101 1.15 paulus if (max_state == -1) {
102 1.7 paulus max_state = MAX_STATES - 1;
103 1.15 paulus bzero((char *)comp, sizeof(*comp));
104 1.15 paulus } else {
105 1.15 paulus /* Don't reset statistics */
106 1.15 paulus bzero((char *)comp->tstate, sizeof(comp->tstate));
107 1.15 paulus bzero((char *)comp->rstate, sizeof(comp->rstate));
108 1.15 paulus }
109 1.7 paulus for (i = max_state; i > 0; --i) {
110 1.7 paulus tstate[i].cs_id = i;
111 1.7 paulus tstate[i].cs_next = &tstate[i - 1];
112 1.7 paulus }
113 1.7 paulus tstate[0].cs_next = &tstate[max_state];
114 1.1 cgd tstate[0].cs_id = 0;
115 1.1 cgd comp->last_cs = &tstate[0];
116 1.1 cgd comp->last_recv = 255;
117 1.1 cgd comp->last_xmit = 255;
118 1.2 cgd comp->flags = SLF_TOSS;
119 1.1 cgd }
120 1.1 cgd
121 1.1 cgd
122 1.1 cgd /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
123 1.1 cgd * checks for zero (since zero has to be encoded in the long, 3 byte
124 1.1 cgd * form).
125 1.1 cgd */
126 1.1 cgd #define ENCODE(n) { \
127 1.10 cgd if ((u_int16_t)(n) >= 256) { \
128 1.1 cgd *cp++ = 0; \
129 1.1 cgd cp[1] = (n); \
130 1.1 cgd cp[0] = (n) >> 8; \
131 1.1 cgd cp += 2; \
132 1.1 cgd } else { \
133 1.1 cgd *cp++ = (n); \
134 1.1 cgd } \
135 1.1 cgd }
136 1.1 cgd #define ENCODEZ(n) { \
137 1.10 cgd if ((u_int16_t)(n) >= 256 || (u_int16_t)(n) == 0) { \
138 1.1 cgd *cp++ = 0; \
139 1.1 cgd cp[1] = (n); \
140 1.1 cgd cp[0] = (n) >> 8; \
141 1.1 cgd cp += 2; \
142 1.1 cgd } else { \
143 1.1 cgd *cp++ = (n); \
144 1.1 cgd } \
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd #define DECODEL(f) { \
148 1.1 cgd if (*cp == 0) {\
149 1.1 cgd (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
150 1.1 cgd cp += 3; \
151 1.1 cgd } else { \
152 1.10 cgd (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
153 1.1 cgd } \
154 1.1 cgd }
155 1.1 cgd
156 1.1 cgd #define DECODES(f) { \
157 1.1 cgd if (*cp == 0) {\
158 1.1 cgd (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
159 1.1 cgd cp += 3; \
160 1.1 cgd } else { \
161 1.10 cgd (f) = htons(ntohs(f) + (u_int32_t)*cp++); \
162 1.1 cgd } \
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd #define DECODEU(f) { \
166 1.1 cgd if (*cp == 0) {\
167 1.1 cgd (f) = htons((cp[1] << 8) | cp[2]); \
168 1.1 cgd cp += 3; \
169 1.1 cgd } else { \
170 1.10 cgd (f) = htons((u_int32_t)*cp++); \
171 1.1 cgd } \
172 1.1 cgd }
173 1.1 cgd
174 1.8 mycroft u_int
175 1.1 cgd sl_compress_tcp(m, ip, comp, compress_cid)
176 1.1 cgd struct mbuf *m;
177 1.21 augustss struct ip *ip;
178 1.1 cgd struct slcompress *comp;
179 1.1 cgd int compress_cid;
180 1.1 cgd {
181 1.21 augustss struct cstate *cs = comp->last_cs->cs_next;
182 1.21 augustss u_int hlen = ip->ip_hl;
183 1.21 augustss struct tcphdr *oth;
184 1.21 augustss struct tcphdr *th;
185 1.21 augustss u_int deltaS, deltaA;
186 1.21 augustss u_int changes = 0;
187 1.1 cgd u_char new_seq[16];
188 1.21 augustss u_char *cp = new_seq;
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Bail if this is an IP fragment or if the TCP packet isn't
192 1.1 cgd * `compressible' (i.e., ACK isn't set or some other control bit is
193 1.1 cgd * set). (We assume that the caller has already made sure the
194 1.1 cgd * packet is IP proto TCP).
195 1.1 cgd */
196 1.1 cgd if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40)
197 1.1 cgd return (TYPE_IP);
198 1.1 cgd
199 1.10 cgd th = (struct tcphdr *)&((int32_t *)ip)[hlen];
200 1.1 cgd if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
201 1.1 cgd return (TYPE_IP);
202 1.1 cgd /*
203 1.1 cgd * Packet is compressible -- we're going to send either a
204 1.1 cgd * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
205 1.1 cgd * to locate (or create) the connection state. Special case the
206 1.1 cgd * most recently used connection since it's most likely to be used
207 1.1 cgd * again & we don't have to do any reordering if it's used.
208 1.1 cgd */
209 1.1 cgd INCR(sls_packets)
210 1.1 cgd if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
211 1.1 cgd ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
212 1.10 cgd *(int32_t *)th != ((int32_t *)&cs->cs_ip)[cs->cs_ip.ip_hl]) {
213 1.1 cgd /*
214 1.1 cgd * Wasn't the first -- search for it.
215 1.1 cgd *
216 1.1 cgd * States are kept in a circularly linked list with
217 1.1 cgd * last_cs pointing to the end of the list. The
218 1.1 cgd * list is kept in lru order by moving a state to the
219 1.1 cgd * head of the list whenever it is referenced. Since
220 1.1 cgd * the list is short and, empirically, the connection
221 1.1 cgd * we want is almost always near the front, we locate
222 1.1 cgd * states via linear search. If we don't find a state
223 1.1 cgd * for the datagram, the oldest state is (re-)used.
224 1.1 cgd */
225 1.21 augustss struct cstate *lcs;
226 1.21 augustss struct cstate *lastcs = comp->last_cs;
227 1.1 cgd
228 1.1 cgd do {
229 1.1 cgd lcs = cs; cs = cs->cs_next;
230 1.1 cgd INCR(sls_searches)
231 1.1 cgd if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
232 1.1 cgd && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
233 1.10 cgd && *(int32_t *)th ==
234 1.10 cgd ((int32_t *)&cs->cs_ip)[cs->cs_ip.ip_hl])
235 1.1 cgd goto found;
236 1.1 cgd } while (cs != lastcs);
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * Didn't find it -- re-use oldest cstate. Send an
240 1.1 cgd * uncompressed packet that tells the other side what
241 1.1 cgd * connection number we're using for this conversation.
242 1.1 cgd * Note that since the state list is circular, the oldest
243 1.1 cgd * state points to the newest and we only need to set
244 1.1 cgd * last_cs to update the lru linkage.
245 1.1 cgd */
246 1.1 cgd INCR(sls_misses)
247 1.1 cgd comp->last_cs = lcs;
248 1.1 cgd hlen += th->th_off;
249 1.1 cgd hlen <<= 2;
250 1.18 christos if (hlen > m->m_len)
251 1.18 christos return (TYPE_IP);
252 1.1 cgd goto uncompressed;
253 1.1 cgd
254 1.1 cgd found:
255 1.1 cgd /*
256 1.1 cgd * Found it -- move to the front on the connection list.
257 1.1 cgd */
258 1.1 cgd if (cs == lastcs)
259 1.1 cgd comp->last_cs = lcs;
260 1.1 cgd else {
261 1.1 cgd lcs->cs_next = cs->cs_next;
262 1.1 cgd cs->cs_next = lastcs->cs_next;
263 1.1 cgd lastcs->cs_next = cs;
264 1.1 cgd }
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * Make sure that only what we expect to change changed. The first
269 1.1 cgd * line of the `if' checks the IP protocol version, header length &
270 1.1 cgd * type of service. The 2nd line checks the "Don't fragment" bit.
271 1.1 cgd * The 3rd line checks the time-to-live and protocol (the protocol
272 1.1 cgd * check is unnecessary but costless). The 4th line checks the TCP
273 1.1 cgd * header length. The 5th line checks IP options, if any. The 6th
274 1.1 cgd * line checks TCP options, if any. If any of these things are
275 1.1 cgd * different between the previous & current datagram, we send the
276 1.1 cgd * current datagram `uncompressed'.
277 1.1 cgd */
278 1.10 cgd oth = (struct tcphdr *)&((int32_t *)&cs->cs_ip)[hlen];
279 1.1 cgd deltaS = hlen;
280 1.1 cgd hlen += th->th_off;
281 1.1 cgd hlen <<= 2;
282 1.18 christos if (hlen > m->m_len)
283 1.18 christos return (TYPE_IP);
284 1.1 cgd
285 1.10 cgd if (((u_int16_t *)ip)[0] != ((u_int16_t *)&cs->cs_ip)[0] ||
286 1.10 cgd ((u_int16_t *)ip)[3] != ((u_int16_t *)&cs->cs_ip)[3] ||
287 1.10 cgd ((u_int16_t *)ip)[4] != ((u_int16_t *)&cs->cs_ip)[4] ||
288 1.1 cgd th->th_off != oth->th_off ||
289 1.1 cgd (deltaS > 5 &&
290 1.1 cgd BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
291 1.1 cgd (th->th_off > 5 &&
292 1.1 cgd BCMP(th + 1, oth + 1, (th->th_off - 5) << 2)))
293 1.1 cgd goto uncompressed;
294 1.1 cgd
295 1.1 cgd /*
296 1.1 cgd * Figure out which of the changing fields changed. The
297 1.1 cgd * receiver expects changes in the order: urgent, window,
298 1.1 cgd * ack, seq (the order minimizes the number of temporaries
299 1.1 cgd * needed in this section of code).
300 1.1 cgd */
301 1.1 cgd if (th->th_flags & TH_URG) {
302 1.1 cgd deltaS = ntohs(th->th_urp);
303 1.1 cgd ENCODEZ(deltaS);
304 1.1 cgd changes |= NEW_U;
305 1.1 cgd } else if (th->th_urp != oth->th_urp)
306 1.1 cgd /* argh! URG not set but urp changed -- a sensible
307 1.1 cgd * implementation should never do this but RFC793
308 1.1 cgd * doesn't prohibit the change so we have to deal
309 1.1 cgd * with it. */
310 1.1 cgd goto uncompressed;
311 1.1 cgd
312 1.14 christos deltaS = (u_int16_t)(ntohs(th->th_win) - ntohs(oth->th_win));
313 1.14 christos if (deltaS) {
314 1.1 cgd ENCODE(deltaS);
315 1.1 cgd changes |= NEW_W;
316 1.1 cgd }
317 1.1 cgd
318 1.14 christos deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
319 1.14 christos if (deltaA) {
320 1.1 cgd if (deltaA > 0xffff)
321 1.1 cgd goto uncompressed;
322 1.1 cgd ENCODE(deltaA);
323 1.1 cgd changes |= NEW_A;
324 1.1 cgd }
325 1.1 cgd
326 1.14 christos deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
327 1.14 christos if (deltaS) {
328 1.1 cgd if (deltaS > 0xffff)
329 1.1 cgd goto uncompressed;
330 1.1 cgd ENCODE(deltaS);
331 1.1 cgd changes |= NEW_S;
332 1.1 cgd }
333 1.1 cgd
334 1.1 cgd switch(changes) {
335 1.1 cgd
336 1.1 cgd case 0:
337 1.1 cgd /*
338 1.1 cgd * Nothing changed. If this packet contains data and the
339 1.1 cgd * last one didn't, this is probably a data packet following
340 1.1 cgd * an ack (normal on an interactive connection) and we send
341 1.1 cgd * it compressed. Otherwise it's probably a retransmit,
342 1.1 cgd * retransmitted ack or window probe. Send it uncompressed
343 1.1 cgd * in case the other side missed the compressed version.
344 1.1 cgd */
345 1.1 cgd if (ip->ip_len != cs->cs_ip.ip_len &&
346 1.1 cgd ntohs(cs->cs_ip.ip_len) == hlen)
347 1.1 cgd break;
348 1.1 cgd
349 1.1 cgd /* (fall through) */
350 1.1 cgd
351 1.1 cgd case SPECIAL_I:
352 1.1 cgd case SPECIAL_D:
353 1.1 cgd /*
354 1.1 cgd * actual changes match one of our special case encodings --
355 1.1 cgd * send packet uncompressed.
356 1.1 cgd */
357 1.1 cgd goto uncompressed;
358 1.1 cgd
359 1.1 cgd case NEW_S|NEW_A:
360 1.1 cgd if (deltaS == deltaA &&
361 1.1 cgd deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
362 1.1 cgd /* special case for echoed terminal traffic */
363 1.1 cgd changes = SPECIAL_I;
364 1.1 cgd cp = new_seq;
365 1.1 cgd }
366 1.1 cgd break;
367 1.1 cgd
368 1.1 cgd case NEW_S:
369 1.1 cgd if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
370 1.1 cgd /* special case for data xfer */
371 1.1 cgd changes = SPECIAL_D;
372 1.1 cgd cp = new_seq;
373 1.1 cgd }
374 1.1 cgd break;
375 1.1 cgd }
376 1.1 cgd
377 1.1 cgd deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
378 1.1 cgd if (deltaS != 1) {
379 1.1 cgd ENCODEZ(deltaS);
380 1.1 cgd changes |= NEW_I;
381 1.1 cgd }
382 1.1 cgd if (th->th_flags & TH_PUSH)
383 1.1 cgd changes |= TCP_PUSH_BIT;
384 1.1 cgd /*
385 1.1 cgd * Grab the cksum before we overwrite it below. Then update our
386 1.1 cgd * state with this packet's header.
387 1.1 cgd */
388 1.1 cgd deltaA = ntohs(th->th_sum);
389 1.1 cgd BCOPY(ip, &cs->cs_ip, hlen);
390 1.1 cgd
391 1.1 cgd /*
392 1.1 cgd * We want to use the original packet as our compressed packet.
393 1.1 cgd * (cp - new_seq) is the number of bytes we need for compressed
394 1.1 cgd * sequence numbers. In addition we need one byte for the change
395 1.1 cgd * mask, one for the connection id and two for the tcp checksum.
396 1.1 cgd * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
397 1.1 cgd * many bytes of the original packet to toss so subtract the two to
398 1.1 cgd * get the new packet size.
399 1.1 cgd */
400 1.1 cgd deltaS = cp - new_seq;
401 1.1 cgd cp = (u_char *)ip;
402 1.1 cgd if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
403 1.1 cgd comp->last_xmit = cs->cs_id;
404 1.1 cgd hlen -= deltaS + 4;
405 1.1 cgd cp += hlen;
406 1.1 cgd *cp++ = changes | NEW_C;
407 1.1 cgd *cp++ = cs->cs_id;
408 1.1 cgd } else {
409 1.1 cgd hlen -= deltaS + 3;
410 1.1 cgd cp += hlen;
411 1.1 cgd *cp++ = changes;
412 1.1 cgd }
413 1.1 cgd m->m_len -= hlen;
414 1.1 cgd m->m_data += hlen;
415 1.1 cgd *cp++ = deltaA >> 8;
416 1.1 cgd *cp++ = deltaA;
417 1.1 cgd BCOPY(new_seq, cp, deltaS);
418 1.1 cgd INCR(sls_compressed)
419 1.1 cgd return (TYPE_COMPRESSED_TCP);
420 1.1 cgd
421 1.1 cgd /*
422 1.1 cgd * Update connection state cs & send uncompressed packet ('uncompressed'
423 1.1 cgd * means a regular ip/tcp packet but with the 'conversation id' we hope
424 1.1 cgd * to use on future compressed packets in the protocol field).
425 1.1 cgd */
426 1.1 cgd uncompressed:
427 1.1 cgd BCOPY(ip, &cs->cs_ip, hlen);
428 1.1 cgd ip->ip_p = cs->cs_id;
429 1.1 cgd comp->last_xmit = cs->cs_id;
430 1.1 cgd return (TYPE_UNCOMPRESSED_TCP);
431 1.1 cgd }
432 1.1 cgd
433 1.1 cgd
434 1.1 cgd int
435 1.1 cgd sl_uncompress_tcp(bufp, len, type, comp)
436 1.1 cgd u_char **bufp;
437 1.1 cgd int len;
438 1.1 cgd u_int type;
439 1.1 cgd struct slcompress *comp;
440 1.1 cgd {
441 1.12 paulus u_char *hdr, *cp;
442 1.18 christos int vjlen;
443 1.18 christos u_int hlen;
444 1.8 mycroft
445 1.12 paulus cp = bufp? *bufp: NULL;
446 1.12 paulus vjlen = sl_uncompress_tcp_core(cp, len, len, type, comp, &hdr, &hlen);
447 1.12 paulus if (vjlen < 0)
448 1.12 paulus return (0); /* error */
449 1.12 paulus if (vjlen == 0)
450 1.12 paulus return (len); /* was uncompressed already */
451 1.12 paulus
452 1.12 paulus cp += vjlen;
453 1.12 paulus len -= vjlen;
454 1.12 paulus
455 1.12 paulus /*
456 1.12 paulus * At this point, cp points to the first byte of data in the
457 1.12 paulus * packet. If we're not aligned on a 4-byte boundary, copy the
458 1.12 paulus * data down so the ip & tcp headers will be aligned. Then back up
459 1.12 paulus * cp by the tcp/ip header length to make room for the reconstructed
460 1.12 paulus * header (we assume the packet we were handed has enough space to
461 1.12 paulus * prepend 128 bytes of header).
462 1.12 paulus */
463 1.13 cgd if ((long)cp & 3) {
464 1.12 paulus if (len > 0)
465 1.20 drochner memmove((caddr_t)((long)cp &~ 3), cp, len);
466 1.13 cgd cp = (u_char *)((long)cp &~ 3);
467 1.12 paulus }
468 1.12 paulus cp -= hlen;
469 1.12 paulus len += hlen;
470 1.12 paulus BCOPY(hdr, cp, hlen);
471 1.12 paulus
472 1.12 paulus *bufp = cp;
473 1.12 paulus return (len);
474 1.4 deraadt }
475 1.4 deraadt
476 1.4 deraadt /*
477 1.4 deraadt * Uncompress a packet of total length total_len. The first buflen
478 1.12 paulus * bytes are at buf; this must include the entire (compressed or
479 1.12 paulus * uncompressed) TCP/IP header. This procedure returns the length
480 1.12 paulus * of the VJ header, with a pointer to the uncompressed IP header
481 1.12 paulus * in *hdrp and its length in *hlenp.
482 1.4 deraadt */
483 1.4 deraadt int
484 1.12 paulus sl_uncompress_tcp_core(buf, buflen, total_len, type, comp, hdrp, hlenp)
485 1.12 paulus u_char *buf;
486 1.4 deraadt int buflen, total_len;
487 1.4 deraadt u_int type;
488 1.4 deraadt struct slcompress *comp;
489 1.12 paulus u_char **hdrp;
490 1.12 paulus u_int *hlenp;
491 1.4 deraadt {
492 1.21 augustss u_char *cp;
493 1.21 augustss u_int hlen, changes;
494 1.21 augustss struct tcphdr *th;
495 1.21 augustss struct cstate *cs;
496 1.21 augustss struct ip *ip;
497 1.21 augustss u_int16_t *bp;
498 1.21 augustss u_int vjlen;
499 1.1 cgd
500 1.1 cgd switch (type) {
501 1.1 cgd
502 1.1 cgd case TYPE_UNCOMPRESSED_TCP:
503 1.12 paulus ip = (struct ip *) buf;
504 1.1 cgd if (ip->ip_p >= MAX_STATES)
505 1.1 cgd goto bad;
506 1.1 cgd cs = &comp->rstate[comp->last_recv = ip->ip_p];
507 1.1 cgd comp->flags &=~ SLF_TOSS;
508 1.1 cgd ip->ip_p = IPPROTO_TCP;
509 1.16 christos /*
510 1.16 christos * Calculate the size of the TCP/IP header and make sure that
511 1.16 christos * we don't overflow the space we have available for it.
512 1.16 christos */
513 1.16 christos hlen = ip->ip_hl << 2;
514 1.16 christos if (hlen + sizeof(struct tcphdr) > buflen)
515 1.16 christos goto bad;
516 1.16 christos hlen += ((struct tcphdr *)&((char *)ip)[hlen])->th_off << 2;
517 1.16 christos if (hlen > MAX_HDR || hlen > buflen)
518 1.16 christos goto bad;
519 1.1 cgd BCOPY(ip, &cs->cs_ip, hlen);
520 1.1 cgd cs->cs_hlen = hlen;
521 1.1 cgd INCR(sls_uncompressedin)
522 1.12 paulus *hdrp = (u_char *) &cs->cs_ip;
523 1.12 paulus *hlenp = hlen;
524 1.12 paulus return (0);
525 1.1 cgd
526 1.1 cgd default:
527 1.1 cgd goto bad;
528 1.1 cgd
529 1.1 cgd case TYPE_COMPRESSED_TCP:
530 1.1 cgd break;
531 1.1 cgd }
532 1.1 cgd /* We've got a compressed packet. */
533 1.1 cgd INCR(sls_compressedin)
534 1.12 paulus cp = buf;
535 1.1 cgd changes = *cp++;
536 1.1 cgd if (changes & NEW_C) {
537 1.1 cgd /* Make sure the state index is in range, then grab the state.
538 1.1 cgd * If we have a good state index, clear the 'discard' flag. */
539 1.1 cgd if (*cp >= MAX_STATES)
540 1.1 cgd goto bad;
541 1.1 cgd
542 1.1 cgd comp->flags &=~ SLF_TOSS;
543 1.1 cgd comp->last_recv = *cp++;
544 1.1 cgd } else {
545 1.1 cgd /* this packet has an implicit state index. If we've
546 1.1 cgd * had a line error since the last time we got an
547 1.1 cgd * explicit state index, we have to toss the packet. */
548 1.1 cgd if (comp->flags & SLF_TOSS) {
549 1.1 cgd INCR(sls_tossed)
550 1.12 paulus return (-1);
551 1.1 cgd }
552 1.1 cgd }
553 1.1 cgd cs = &comp->rstate[comp->last_recv];
554 1.1 cgd hlen = cs->cs_ip.ip_hl << 2;
555 1.1 cgd th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
556 1.1 cgd th->th_sum = htons((*cp << 8) | cp[1]);
557 1.1 cgd cp += 2;
558 1.1 cgd if (changes & TCP_PUSH_BIT)
559 1.1 cgd th->th_flags |= TH_PUSH;
560 1.1 cgd else
561 1.1 cgd th->th_flags &=~ TH_PUSH;
562 1.1 cgd
563 1.1 cgd switch (changes & SPECIALS_MASK) {
564 1.1 cgd case SPECIAL_I:
565 1.1 cgd {
566 1.21 augustss u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
567 1.1 cgd th->th_ack = htonl(ntohl(th->th_ack) + i);
568 1.1 cgd th->th_seq = htonl(ntohl(th->th_seq) + i);
569 1.1 cgd }
570 1.1 cgd break;
571 1.1 cgd
572 1.1 cgd case SPECIAL_D:
573 1.1 cgd th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
574 1.1 cgd - cs->cs_hlen);
575 1.1 cgd break;
576 1.1 cgd
577 1.1 cgd default:
578 1.1 cgd if (changes & NEW_U) {
579 1.1 cgd th->th_flags |= TH_URG;
580 1.1 cgd DECODEU(th->th_urp)
581 1.1 cgd } else
582 1.1 cgd th->th_flags &=~ TH_URG;
583 1.1 cgd if (changes & NEW_W)
584 1.1 cgd DECODES(th->th_win)
585 1.1 cgd if (changes & NEW_A)
586 1.1 cgd DECODEL(th->th_ack)
587 1.1 cgd if (changes & NEW_S)
588 1.1 cgd DECODEL(th->th_seq)
589 1.1 cgd break;
590 1.1 cgd }
591 1.1 cgd if (changes & NEW_I) {
592 1.1 cgd DECODES(cs->cs_ip.ip_id)
593 1.1 cgd } else
594 1.1 cgd cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
595 1.1 cgd
596 1.1 cgd /*
597 1.1 cgd * At this point, cp points to the first byte of data in the
598 1.12 paulus * packet. Fill in the IP total length and update the IP
599 1.12 paulus * header checksum.
600 1.1 cgd */
601 1.12 paulus vjlen = cp - buf;
602 1.12 paulus buflen -= vjlen;
603 1.4 deraadt if (buflen < 0)
604 1.1 cgd /* we must have dropped some characters (crc should detect
605 1.1 cgd * this but the old slip framing won't) */
606 1.1 cgd goto bad;
607 1.1 cgd
608 1.12 paulus total_len += cs->cs_hlen - vjlen;
609 1.4 deraadt cs->cs_ip.ip_len = htons(total_len);
610 1.1 cgd
611 1.1 cgd /* recompute the ip header checksum */
612 1.12 paulus bp = (u_int16_t *) &cs->cs_ip;
613 1.12 paulus cs->cs_ip.ip_sum = 0;
614 1.12 paulus for (changes = 0; hlen > 0; hlen -= 2)
615 1.12 paulus changes += *bp++;
616 1.12 paulus changes = (changes & 0xffff) + (changes >> 16);
617 1.12 paulus changes = (changes & 0xffff) + (changes >> 16);
618 1.12 paulus cs->cs_ip.ip_sum = ~ changes;
619 1.12 paulus
620 1.12 paulus *hdrp = (u_char *) &cs->cs_ip;
621 1.12 paulus *hlenp = cs->cs_hlen;
622 1.12 paulus return vjlen;
623 1.12 paulus
624 1.1 cgd bad:
625 1.1 cgd comp->flags |= SLF_TOSS;
626 1.1 cgd INCR(sls_errorin)
627 1.12 paulus return (-1);
628 1.1 cgd }
629