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