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