2 * Routines to compress and uncompess tcp packets (for transmission
3 * over low speed serial lines.
5 * Copyright (c) 1989 Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
21 * - Initial distribution.
23 * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
24 * so that the entire packet being decompressed doesn't have
25 * to be in contiguous memory (just the compressed header).
27 * $Id: slcompress.c,v 1.4 1997/03/04 03:45:18 paulus Exp $
30 #include "../h/types.h"
31 #include "../h/param.h"
32 #include "../h/mbuf.h"
33 #include "../net/netinet/in.h"
34 #include "../net/netinet/in_systm.h"
35 #include "../net/netinet/ip.h"
36 #include "../net/netinet/tcp.h"
39 #include "slcompress.h"
42 #define INCR(counter) ++comp->counter;
47 #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
48 #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
55 sl_compress_init(comp)
56 struct slcompress *comp;
59 register struct cstate *tstate = comp->tstate;
61 bzero((char *)comp, sizeof(*comp));
62 for (i = MAX_STATES - 1; i > 0; --i) {
64 tstate[i].cs_next = &tstate[i - 1];
66 tstate[0].cs_next = &tstate[MAX_STATES - 1];
68 comp->last_cs = &tstate[0];
69 comp->last_recv = 255;
70 comp->last_xmit = 255;
71 comp->flags = SLF_TOSS;
76 * Like sl_compress_init, but we get to specify the maximum connection
77 * ID to use on transmission.
80 sl_compress_setup(comp, max_state)
81 struct slcompress *comp;
85 register struct cstate *tstate = comp->tstate;
87 if ((unsigned) max_state > MAX_STATES - 1)
88 max_state = MAX_STATES - 1;
89 bzero((char *)comp, sizeof(*comp));
90 for (i = max_state; i > 0; --i) {
92 tstate[i].cs_next = &tstate[i - 1];
94 tstate[0].cs_next = &tstate[max_state];
96 comp->last_cs = &tstate[0];
97 comp->last_recv = 255;
98 comp->last_xmit = 255;
99 comp->flags = SLF_TOSS;
103 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
104 * checks for zero (since zero has to be encoded in the long, 3 byte
107 #define ENCODE(n) { \
108 if ((u_short)(n) >= 256) { \
117 #define ENCODEZ(n) { \
118 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
128 #define DECODEL(f) { \
130 (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
133 (f) = htonl(ntohl(f) + (u_long)*cp++); \
137 #define DECODES(f) { \
139 (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
142 (f) = htons(ntohs(f) + (u_long)*cp++); \
146 #define DECODEU(f) { \
148 (f) = htons((cp[1] << 8) | cp[2]); \
151 (f) = htons((u_long)*cp++); \
156 sl_compress_tcp(m, ip, comp, compress_cid)
158 register struct ip *ip;
159 struct slcompress *comp;
162 register struct cstate *cs = comp->last_cs->cs_next;
163 register u_int hlen = ip->ip_hl;
164 register struct tcphdr *oth;
165 register struct tcphdr *th;
166 register u_int deltaS, deltaA;
167 register u_int changes = 0;
169 register u_char *cp = new_seq;
172 * Bail if this is an IP fragment or if the TCP packet isn't
173 * `compressible' (i.e., ACK isn't set or some other control bit is
174 * set). (We assume that the caller has already made sure the
175 * packet is IP proto TCP).
177 if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40)
180 th = (struct tcphdr *)&((int *)ip)[hlen];
181 if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
184 * Packet is compressible -- we're going to send either a
185 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
186 * to locate (or create) the connection state. Special case the
187 * most recently used connection since it's most likely to be used
188 * again & we don't have to do any reordering if it's used.
191 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
192 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
193 *(int *)th != ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl]) {
195 * Wasn't the first -- search for it.
197 * States are kept in a circularly linked list with
198 * last_cs pointing to the end of the list. The
199 * list is kept in lru order by moving a state to the
200 * head of the list whenever it is referenced. Since
201 * the list is short and, empirically, the connection
202 * we want is almost always near the front, we locate
203 * states via linear search. If we don't find a state
204 * for the datagram, the oldest state is (re-)used.
206 register struct cstate *lcs;
207 register struct cstate *lastcs = comp->last_cs;
210 lcs = cs; cs = cs->cs_next;
212 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
213 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
214 && *(int *)th == ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl])
216 } while (cs != lastcs);
219 * Didn't find it -- re-use oldest cstate. Send an
220 * uncompressed packet that tells the other side what
221 * connection number we're using for this conversation.
222 * Note that since the state list is circular, the oldest
223 * state points to the newest and we only need to set
224 * last_cs to update the lru linkage.
236 * Found it -- move to the front on the connection list.
241 lcs->cs_next = cs->cs_next;
242 cs->cs_next = lastcs->cs_next;
243 lastcs->cs_next = cs;
248 * Make sure that only what we expect to change changed. The first
249 * line of the `if' checks the IP protocol version, header length &
250 * type of service. The 2nd line checks the "Don't fragment" bit.
251 * The 3rd line checks the time-to-live and protocol (the protocol
252 * check is unnecessary but costless). The 4th line checks the TCP
253 * header length. The 5th line checks IP options, if any. The 6th
254 * line checks TCP options, if any. If any of these things are
255 * different between the previous & current datagram, we send the
256 * current datagram `uncompressed'.
258 oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen];
265 if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] ||
266 ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] ||
267 ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] ||
268 th->th_off != oth->th_off ||
270 BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
272 BCMP(th + 1, oth + 1, (th->th_off - 5) << 2)))
276 * Figure out which of the changing fields changed. The
277 * receiver expects changes in the order: urgent, window,
278 * ack, seq (the order minimizes the number of temporaries
279 * needed in this section of code).
281 if (th->th_flags & TH_URG) {
282 deltaS = ntohs(th->th_urp);
285 } else if (th->th_urp != oth->th_urp)
286 /* argh! URG not set but urp changed -- a sensible
287 * implementation should never do this but RFC793
288 * doesn't prohibit the change so we have to deal
292 if (deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) {
297 if (deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) {
304 if (deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) {
315 * Nothing changed. If this packet contains data and the
316 * last one didn't, this is probably a data packet following
317 * an ack (normal on an interactive connection) and we send
318 * it compressed. Otherwise it's probably a retransmit,
319 * retransmitted ack or window probe. Send it uncompressed
320 * in case the other side missed the compressed version.
322 if (ip->ip_len != cs->cs_ip.ip_len &&
323 ntohs(cs->cs_ip.ip_len) == hlen)
331 * actual changes match one of our special case encodings --
332 * send packet uncompressed.
337 if (deltaS == deltaA &&
338 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
339 /* special case for echoed terminal traffic */
346 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
347 /* special case for data xfer */
354 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
359 if (th->th_flags & TH_PUSH)
360 changes |= TCP_PUSH_BIT;
362 * Grab the cksum before we overwrite it below. Then update our
363 * state with this packet's header.
365 deltaA = ntohs(th->th_sum);
366 BCOPY(ip, &cs->cs_ip, hlen);
369 * We want to use the original packet as our compressed packet.
370 * (cp - new_seq) is the number of bytes we need for compressed
371 * sequence numbers. In addition we need one byte for the change
372 * mask, one for the connection id and two for the tcp checksum.
373 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
374 * many bytes of the original packet to toss so subtract the two to
375 * get the new packet size.
377 deltaS = cp - new_seq;
379 if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
380 comp->last_xmit = cs->cs_id;
383 *cp++ = changes | NEW_C;
394 BCOPY(new_seq, cp, deltaS);
396 return (TYPE_COMPRESSED_TCP);
399 * Update connection state cs & send uncompressed packet ('uncompressed'
400 * means a regular ip/tcp packet but with the 'conversation id' we hope
401 * to use on future compressed packets in the protocol field).
404 BCOPY(ip, &cs->cs_ip, hlen);
405 ip->ip_p = cs->cs_id;
406 comp->last_xmit = cs->cs_id;
407 return (TYPE_UNCOMPRESSED_TCP);
412 sl_uncompress_tcp(bufp, len, type, comp)
416 struct slcompress *comp;
422 cp = bufp? *bufp: NULL;
423 vjlen = sl_uncompress_tcp_core(cp, len, len, type, comp, &hdr, &hlen);
425 return (0); /* error */
427 return (len); /* was uncompressed already */
433 * At this point, cp points to the first byte of data in the
434 * packet. If we're not aligned on a 4-byte boundary, copy the
435 * data down so the ip & tcp headers will be aligned. Then back up
436 * cp by the tcp/ip header length to make room for the reconstructed
437 * header (we assume the packet we were handed has enough space to
438 * prepend 128 bytes of header).
442 (void) ovbcopy(cp, (caddr_t)((int)cp &~ 3), len);
443 cp = (u_char *)((int)cp &~ 3);
447 BCOPY(hdr, cp, hlen);
454 * Uncompress a packet of total length total_len. The first buflen
455 * bytes are at buf; this must include the entire (compressed or
456 * uncompressed) TCP/IP header. This procedure returns the length
457 * of the VJ header, with a pointer to the uncompressed IP header
458 * in *hdrp and its length in *hlenp.
461 sl_uncompress_tcp_core(buf, buflen, total_len, type, comp, hdrp, hlenp)
463 int buflen, total_len;
465 struct slcompress *comp;
470 register u_int hlen, changes;
471 register struct tcphdr *th;
472 register struct cstate *cs;
473 register struct ip *ip;
474 register u_short *bp;
475 register u_int vjlen;
479 case TYPE_UNCOMPRESSED_TCP:
480 ip = (struct ip *) buf;
481 if (ip->ip_p >= MAX_STATES)
483 cs = &comp->rstate[comp->last_recv = ip->ip_p];
484 comp->flags &=~ SLF_TOSS;
485 ip->ip_p = IPPROTO_TCP;
487 * Calculate the size of the TCP/IP header and make sure that
488 * we don't overflow the space we have available for it.
490 hlen = ip->ip_hl << 2;
491 if (hlen + sizeof(struct tcphdr) > buflen)
493 hlen += ((struct tcphdr *)&((char *)ip)[hlen])->th_off << 2;
494 if (hlen > MAX_HDR || hlen > buflen)
496 BCOPY(ip, &cs->cs_ip, hlen);
498 INCR(sls_uncompressedin)
499 *hdrp = (u_char *) &cs->cs_ip;
506 case TYPE_COMPRESSED_TCP:
509 /* We've got a compressed packet. */
510 INCR(sls_compressedin)
513 if (changes & NEW_C) {
514 /* Make sure the state index is in range, then grab the state.
515 * If we have a good state index, clear the 'discard' flag. */
516 if (*cp >= MAX_STATES)
519 comp->flags &=~ SLF_TOSS;
520 comp->last_recv = *cp++;
522 /* this packet has an implicit state index. If we've
523 * had a line error since the last time we got an
524 * explicit state index, we have to toss the packet. */
525 if (comp->flags & SLF_TOSS) {
530 cs = &comp->rstate[comp->last_recv];
531 hlen = cs->cs_ip.ip_hl << 2;
532 th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
533 th->th_sum = htons((*cp << 8) | cp[1]);
535 if (changes & TCP_PUSH_BIT)
536 th->th_flags |= TH_PUSH;
538 th->th_flags &=~ TH_PUSH;
540 switch (changes & SPECIALS_MASK) {
543 register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
544 th->th_ack = htonl(ntohl(th->th_ack) + i);
545 th->th_seq = htonl(ntohl(th->th_seq) + i);
550 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
555 if (changes & NEW_U) {
556 th->th_flags |= TH_URG;
559 th->th_flags &=~ TH_URG;
568 if (changes & NEW_I) {
569 DECODES(cs->cs_ip.ip_id)
571 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
574 * At this point, cp points to the first byte of data in the
575 * packet. Fill in the IP total length and update the IP
581 /* we must have dropped some characters (crc should detect
582 * this but the old slip framing won't) */
585 total_len += cs->cs_hlen - vjlen;
586 cs->cs_ip.ip_len = htons(total_len);
588 /* recompute the ip header checksum */
589 bp = (u_short *) &cs->cs_ip;
590 cs->cs_ip.ip_sum = 0;
591 for (changes = 0; hlen > 0; hlen -= 2)
593 changes = (changes & 0xffff) + (changes >> 16);
594 changes = (changes & 0xffff) + (changes >> 16);
595 cs->cs_ip.ip_sum = ~ changes;
597 *hdrp = (u_char *) &cs->cs_ip;
598 *hlenp = cs->cs_hlen;
602 comp->flags |= SLF_TOSS;