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