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