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