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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)slcompress.c	8.2 (Berkeley) 4/16/94
30 * $FreeBSD$
31 */
32
33/*
34 * Routines to compress and uncompess tcp packets (for transmission
35 * over low speed serial lines.
36 *
37 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
38 *	- Initial distribution.
39 *
40 */
41
42#include <sys/param.h>
43#include <sys/mbuf.h>
44#include <sys/systm.h>
45
46#include <netinet/in.h>
47#include <netinet/in_systm.h>
48#include <netinet/ip.h>
49#include <netinet/tcp.h>
50
51#include <net/slcompress.h>
52
53#ifndef SL_NO_STATS
54#define INCR(counter) ++comp->counter;
55#else
56#define INCR(counter)
57#endif
58
59#define BCMP(p1, p2, n) bcmp((void *)(p1), (void *)(p2), (int)(n))
60#define BCOPY(p1, p2, n) bcopy((void *)(p1), (void *)(p2), (int)(n))
61
62void
63sl_compress_init(comp, max_state)
64	struct slcompress *comp;
65	int max_state;
66{
67	register u_int i;
68	register struct cstate *tstate = comp->tstate;
69
70	if (max_state == -1) {
71		max_state = MAX_STATES - 1;
72		bzero((char *)comp, sizeof(*comp));
73	} else {
74		/* Don't reset statistics */
75		bzero((char *)comp->tstate, sizeof(comp->tstate));
76		bzero((char *)comp->rstate, sizeof(comp->rstate));
77	}
78  	for (i = max_state; i > 0; --i) {
79		tstate[i].cs_id = i;
80		tstate[i].cs_next = &tstate[i - 1];
81	}
82	tstate[0].cs_next = &tstate[max_state];
83	tstate[0].cs_id = 0;
84	comp->last_cs = &tstate[0];
85	comp->last_recv = 255;
86	comp->last_xmit = 255;
87	comp->flags = SLF_TOSS;
88}
89
90
91/* ENCODE encodes a number that is known to be non-zero.  ENCODEZ
92 * checks for zero (since zero has to be encoded in the long, 3 byte
93 * form).
94 */
95#define ENCODE(n) { \
96	if ((u_int16_t)(n) >= 256) { \
97		*cp++ = 0; \
98		cp[1] = (n); \
99		cp[0] = (n) >> 8; \
100		cp += 2; \
101	} else { \
102		*cp++ = (n); \
103	} \
104}
105#define ENCODEZ(n) { \
106	if ((u_int16_t)(n) >= 256 || (u_int16_t)(n) == 0) { \
107		*cp++ = 0; \
108		cp[1] = (n); \
109		cp[0] = (n) >> 8; \
110		cp += 2; \
111	} else { \
112		*cp++ = (n); \
113	} \
114}
115
116#define DECODEL(f) { \
117	if (*cp == 0) {\
118		(f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
119		cp += 3; \
120	} else { \
121		(f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
122	} \
123}
124
125#define DECODES(f) { \
126	if (*cp == 0) {\
127		(f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
128		cp += 3; \
129	} else { \
130		(f) = htons(ntohs(f) + (u_int32_t)*cp++); \
131	} \
132}
133
134#define DECODEU(f) { \
135	if (*cp == 0) {\
136		(f) = htons((cp[1] << 8) | cp[2]); \
137		cp += 3; \
138	} else { \
139		(f) = htons((u_int32_t)*cp++); \
140	} \
141}
142
143/*
144 * Attempt to compress an outgoing TCP packet and return the type of
145 * the result.  The caller must have already verified that the protocol
146 * is TCP.  The first mbuf must contain the complete IP and TCP headers,
147 * and "ip" must be == mtod(m, struct ip *).  "comp" supplies the
148 * compression state, and "compress_cid" tells us whether it is OK
149 * to leave out the CID field when feasible.
150 *
151 * The caller is responsible for adjusting m->m_pkthdr.len upon return,
152 * if m is an M_PKTHDR mbuf.
153 */
154u_int
155sl_compress_tcp(m, ip, comp, compress_cid)
156	struct mbuf *m;
157	register struct ip *ip;
158	struct slcompress *comp;
159	int compress_cid;
160{
161	register struct cstate *cs = comp->last_cs->cs_next;
162	register u_int hlen = ip->ip_hl;
163	register struct tcphdr *oth;
164	register struct tcphdr *th;
165	register u_int deltaS, deltaA;
166	register u_int changes = 0;
167	u_char new_seq[16];
168	register u_char *cp = new_seq;
169
170	/*
171	 * Bail if this is an IP fragment or if the TCP packet isn't
172	 * `compressible' (i.e., ACK isn't set or some other control bit is
173	 * set).  (We assume that the caller has already made sure the
174	 * packet is IP proto TCP).
175	 */
176	if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40)
177		return (TYPE_IP);
178
179	th = (struct tcphdr *)&((int32_t *)ip)[hlen];
180	if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
181		return (TYPE_IP);
182	/*
183	 * Packet is compressible -- we're going to send either a
184	 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet.  Either way we need
185	 * to locate (or create) the connection state.  Special case the
186	 * most recently used connection since it's most likely to be used
187	 * again & we don't have to do any reordering if it's used.
188	 */
189	INCR(sls_packets)
190	if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
191	    ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
192	    *(int32_t *)th != ((int32_t *)&cs->cs_ip)[cs->cs_ip.ip_hl]) {
193		/*
194		 * Wasn't the first -- search for it.
195		 *
196		 * States are kept in a circularly linked list with
197		 * last_cs pointing to the end of the list.  The
198		 * list is kept in lru order by moving a state to the
199		 * head of the list whenever it is referenced.  Since
200		 * the list is short and, empirically, the connection
201		 * we want is almost always near the front, we locate
202		 * states via linear search.  If we don't find a state
203		 * for the datagram, the oldest state is (re-)used.
204		 */
205		register struct cstate *lcs;
206		register struct cstate *lastcs = comp->last_cs;
207
208		do {
209			lcs = cs; cs = cs->cs_next;
210			INCR(sls_searches)
211			if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
212			    && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
213			    && *(int32_t *)th ==
214			    ((int32_t *)&cs->cs_ip)[cs->cs_ip.ip_hl])
215				goto found;
216		} while (cs != lastcs);
217
218		/*
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.
225		 */
226		INCR(sls_misses)
227		comp->last_cs = lcs;
228		hlen += th->th_off;
229		hlen <<= 2;
230		if (hlen > m->m_len)
231		    return TYPE_IP;
232		goto uncompressed;
233
234	found:
235		/*
236		 * Found it -- move to the front on the connection list.
237		 */
238		if (cs == lastcs)
239			comp->last_cs = lcs;
240		else {
241			lcs->cs_next = cs->cs_next;
242			cs->cs_next = lastcs->cs_next;
243			lastcs->cs_next = cs;
244		}
245	}
246
247	/*
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'.
257	 */
258	oth = (struct tcphdr *)&((int32_t *)&cs->cs_ip)[hlen];
259	deltaS = hlen;
260	hlen += th->th_off;
261	hlen <<= 2;
262	if (hlen > m->m_len)
263	    return TYPE_IP;
264
265	if (((u_int16_t *)ip)[0] != ((u_int16_t *)&cs->cs_ip)[0] ||
266	    ((u_int16_t *)ip)[3] != ((u_int16_t *)&cs->cs_ip)[3] ||
267	    ((u_int16_t *)ip)[4] != ((u_int16_t *)&cs->cs_ip)[4] ||
268	    th->th_off != oth->th_off ||
269	    (deltaS > 5 &&
270	     BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
271	    (th->th_off > 5 &&
272	     BCMP(th + 1, oth + 1, (th->th_off - 5) << 2)))
273		goto uncompressed;
274
275	/*
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).
280	 */
281	if (th->th_flags & TH_URG) {
282		deltaS = ntohs(th->th_urp);
283		ENCODEZ(deltaS);
284		changes |= NEW_U;
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
289		 * with it. */
290		 goto uncompressed;
291
292	deltaS = (u_int16_t)(ntohs(th->th_win) - ntohs(oth->th_win));
293	if (deltaS) {
294		ENCODE(deltaS);
295		changes |= NEW_W;
296	}
297
298	deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
299	if (deltaA) {
300		if (deltaA > 0xffff)
301			goto uncompressed;
302		ENCODE(deltaA);
303		changes |= NEW_A;
304	}
305
306	deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
307	if (deltaS) {
308		if (deltaS > 0xffff)
309			goto uncompressed;
310		ENCODE(deltaS);
311		changes |= NEW_S;
312	}
313
314	switch(changes) {
315
316	case 0:
317		/*
318		 * Nothing changed. If this packet contains data and the
319		 * last one didn't, this is probably a data packet following
320		 * an ack (normal on an interactive connection) and we send
321		 * it compressed.  Otherwise it's probably a retransmit,
322		 * retransmitted ack or window probe.  Send it uncompressed
323		 * in case the other side missed the compressed version.
324		 */
325		if (ip->ip_len != cs->cs_ip.ip_len &&
326		    ntohs(cs->cs_ip.ip_len) == hlen)
327			break;
328
329		/* FALLTHROUGH */
330
331	case SPECIAL_I:
332	case SPECIAL_D:
333		/*
334		 * actual changes match one of our special case encodings --
335		 * send packet uncompressed.
336		 */
337		goto uncompressed;
338
339	case NEW_S|NEW_A:
340		if (deltaS == deltaA &&
341		    deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
342			/* special case for echoed terminal traffic */
343			changes = SPECIAL_I;
344			cp = new_seq;
345		}
346		break;
347
348	case NEW_S:
349		if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
350			/* special case for data xfer */
351			changes = SPECIAL_D;
352			cp = new_seq;
353		}
354		break;
355	}
356
357	deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
358	if (deltaS != 1) {
359		ENCODEZ(deltaS);
360		changes |= NEW_I;
361	}
362	if (th->th_flags & TH_PUSH)
363		changes |= TCP_PUSH_BIT;
364	/*
365	 * Grab the cksum before we overwrite it below.  Then update our
366	 * state with this packet's header.
367	 */
368	deltaA = ntohs(th->th_sum);
369	BCOPY(ip, &cs->cs_ip, hlen);
370
371	/*
372	 * We want to use the original packet as our compressed packet.
373	 * (cp - new_seq) is the number of bytes we need for compressed
374	 * sequence numbers.  In addition we need one byte for the change
375	 * mask, one for the connection id and two for the tcp checksum.
376	 * So, (cp - new_seq) + 4 bytes of header are needed.  hlen is how
377	 * many bytes of the original packet to toss so subtract the two to
378	 * get the new packet size.
379	 */
380	deltaS = cp - new_seq;
381	cp = (u_char *)ip;
382	if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
383		comp->last_xmit = cs->cs_id;
384		hlen -= deltaS + 4;
385		cp += hlen;
386		*cp++ = changes | NEW_C;
387		*cp++ = cs->cs_id;
388	} else {
389		hlen -= deltaS + 3;
390		cp += hlen;
391		*cp++ = changes;
392	}
393	m->m_len -= hlen;
394	m->m_data += hlen;
395	*cp++ = deltaA >> 8;
396	*cp++ = deltaA;
397	BCOPY(new_seq, cp, deltaS);
398	INCR(sls_compressed)
399	return (TYPE_COMPRESSED_TCP);
400
401	/*
402	 * Update connection state cs & send uncompressed packet ('uncompressed'
403	 * means a regular ip/tcp packet but with the 'conversation id' we hope
404	 * to use on future compressed packets in the protocol field).
405	 */
406uncompressed:
407	BCOPY(ip, &cs->cs_ip, hlen);
408	ip->ip_p = cs->cs_id;
409	comp->last_xmit = cs->cs_id;
410	return (TYPE_UNCOMPRESSED_TCP);
411}
412
413
414int
415sl_uncompress_tcp(bufp, len, type, comp)
416	u_char **bufp;
417	int len;
418	u_int type;
419	struct slcompress *comp;
420{
421	u_char *hdr, *cp;
422	int hlen, vjlen;
423
424	cp = bufp? *bufp: NULL;
425	vjlen = sl_uncompress_tcp_core(cp, len, len, type, comp, &hdr, &hlen);
426	if (vjlen < 0)
427		return (0);	/* error */
428	if (vjlen == 0)
429		return (len);	/* was uncompressed already */
430
431	cp += vjlen;
432	len -= vjlen;
433
434	/*
435	 * At this point, cp points to the first byte of data in the
436	 * packet.  If we're not aligned on a 4-byte boundary, copy the
437	 * data down so the ip & tcp headers will be aligned.  Then back up
438	 * cp by the tcp/ip header length to make room for the reconstructed
439	 * header (we assume the packet we were handed has enough space to
440	 * prepend 128 bytes of header).
441	 */
442	if ((intptr_t)cp & 3) {
443		if (len > 0)
444			BCOPY(cp, ((intptr_t)cp &~ 3), len);
445		cp = (u_char *)((intptr_t)cp &~ 3);
446	}
447	cp -= hlen;
448	len += hlen;
449	BCOPY(hdr, cp, hlen);
450
451	*bufp = cp;
452	return (len);
453}
454
455/*
456 * Uncompress a packet of total length total_len.  The first buflen
457 * bytes are at buf; this must include the entire (compressed or
458 * uncompressed) TCP/IP header.  This procedure returns the length
459 * of the VJ header, with a pointer to the uncompressed IP header
460 * in *hdrp and its length in *hlenp.
461 */
462int
463sl_uncompress_tcp_core(buf, buflen, total_len, type, comp, hdrp, hlenp)
464	u_char *buf;
465	int buflen, total_len;
466	u_int type;
467	struct slcompress *comp;
468	u_char **hdrp;
469	u_int *hlenp;
470{
471	register u_char *cp;
472	register u_int hlen, changes;
473	register struct tcphdr *th;
474	register struct cstate *cs;
475	register struct ip *ip;
476	register u_int16_t *bp;
477	register u_int vjlen;
478
479	switch (type) {
480
481	case TYPE_UNCOMPRESSED_TCP:
482		ip = (struct ip *) buf;
483		if (ip->ip_p >= MAX_STATES)
484			goto bad;
485		cs = &comp->rstate[comp->last_recv = ip->ip_p];
486		comp->flags &=~ SLF_TOSS;
487		ip->ip_p = IPPROTO_TCP;
488		/*
489		 * Calculate the size of the TCP/IP header and make sure that
490		 * we don't overflow the space we have available for it.
491		 */
492		hlen = ip->ip_hl << 2;
493		if (hlen + sizeof(struct tcphdr) > buflen)
494			goto bad;
495		hlen += ((struct tcphdr *)&((char *)ip)[hlen])->th_off << 2;
496		if (hlen > MAX_HDR || hlen > buflen)
497			goto bad;
498		BCOPY(ip, &cs->cs_ip, hlen);
499		cs->cs_hlen = hlen;
500		INCR(sls_uncompressedin)
501		*hdrp = (u_char *) &cs->cs_ip;
502		*hlenp = hlen;
503		return (0);
504
505	default:
506		goto bad;
507
508	case TYPE_COMPRESSED_TCP:
509		break;
510	}
511	/* We've got a compressed packet. */
512	INCR(sls_compressedin)
513	cp = buf;
514	changes = *cp++;
515	if (changes & NEW_C) {
516		/* Make sure the state index is in range, then grab the state.
517		 * If we have a good state index, clear the 'discard' flag. */
518		if (*cp >= MAX_STATES)
519			goto bad;
520
521		comp->flags &=~ SLF_TOSS;
522		comp->last_recv = *cp++;
523	} else {
524		/* this packet has an implicit state index.  If we've
525		 * had a line error since the last time we got an
526		 * explicit state index, we have to toss the packet. */
527		if (comp->flags & SLF_TOSS) {
528			INCR(sls_tossed)
529			return (-1);
530		}
531	}
532	cs = &comp->rstate[comp->last_recv];
533	hlen = cs->cs_ip.ip_hl << 2;
534	th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
535	th->th_sum = htons((*cp << 8) | cp[1]);
536	cp += 2;
537	if (changes & TCP_PUSH_BIT)
538		th->th_flags |= TH_PUSH;
539	else
540		th->th_flags &=~ TH_PUSH;
541
542	switch (changes & SPECIALS_MASK) {
543	case SPECIAL_I:
544		{
545		register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
546		th->th_ack = htonl(ntohl(th->th_ack) + i);
547		th->th_seq = htonl(ntohl(th->th_seq) + i);
548		}
549		break;
550
551	case SPECIAL_D:
552		th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
553				   - cs->cs_hlen);
554		break;
555
556	default:
557		if (changes & NEW_U) {
558			th->th_flags |= TH_URG;
559			DECODEU(th->th_urp)
560		} else
561			th->th_flags &=~ TH_URG;
562		if (changes & NEW_W)
563			DECODES(th->th_win)
564		if (changes & NEW_A)
565			DECODEL(th->th_ack)
566		if (changes & NEW_S)
567			DECODEL(th->th_seq)
568		break;
569	}
570	if (changes & NEW_I) {
571		DECODES(cs->cs_ip.ip_id)
572	} else
573		cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
574
575	/*
576	 * At this point, cp points to the first byte of data in the
577	 * packet.  Fill in the IP total length and update the IP
578	 * header checksum.
579	 */
580	vjlen = cp - buf;
581	buflen -= vjlen;
582	if (buflen < 0)
583		/* we must have dropped some characters (crc should detect
584		 * this but the old slip framing won't) */
585		goto bad;
586
587	total_len += cs->cs_hlen - vjlen;
588	cs->cs_ip.ip_len = htons(total_len);
589
590	/* recompute the ip header checksum */
591	bp = (u_int16_t *) &cs->cs_ip;
592	cs->cs_ip.ip_sum = 0;
593		for (changes = 0; hlen > 0; hlen -= 2)
594			changes += *bp++;
595		changes = (changes & 0xffff) + (changes >> 16);
596		changes = (changes & 0xffff) + (changes >> 16);
597	cs->cs_ip.ip_sum = ~ changes;
598
599	*hdrp = (u_char *) &cs->cs_ip;
600	*hlenp = cs->cs_hlen;
601	return vjlen;
602
603bad:
604	comp->flags |= SLF_TOSS;
605	INCR(sls_errorin)
606	return (-1);
607}
608