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