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