CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / ppp / modules / vjcompress.c
blobc0073ac48a9b21e4d62e13f6cef9bfa25b5a5e34
1 /*
2 * Routines to compress and uncompess tcp packets (for transmission
3 * over low speed serial lines.
5 * Copyright (c) 1989 Regents of the University of California.
6 * All rights reserved.
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.
20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
21 * - Initial distribution.
23 * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
24 * so that the entire packet being decompressed doesn't have
25 * to be in contiguous memory (just the compressed header).
29 * This version is used under SunOS 4.x, Digital UNIX, AIX 4.x,
30 * and SVR4 systems including Solaris 2.
32 * $Id: vjcompress.c,v 1.1.1.4 2003/10/14 08:09:53 sparq Exp $
35 #include <sys/types.h>
36 #include <sys/param.h>
38 #ifdef SVR4
39 #ifndef __GNUC__
40 #include <sys/byteorder.h> /* for ntohl, etc. */
41 #else
42 /* make sure we don't get the gnu "fixed" one! */
43 #include "/usr/include/sys/byteorder.h"
44 #endif
45 #endif
47 #ifdef __osf__
48 #include <net/net_globals.h>
49 #endif
50 #include <netinet/in.h>
52 #ifdef AIX4
53 #define _NETINET_IN_SYSTM_H_
54 typedef u_long n_long;
55 #else
56 #include <netinet/in_systm.h>
57 #endif
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
62 #include <net/ppp_defs.h>
63 #include <net/vjcompress.h>
65 #ifndef VJ_NO_STATS
66 #define INCR(counter) ++comp->stats.counter
67 #else
68 #define INCR(counter)
69 #endif
71 #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
72 #undef BCOPY
73 #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
74 #ifndef KERNEL
75 #define ovbcopy bcopy
76 #endif
78 #ifdef __osf__
79 #define getip_hl(base) (((base).ip_vhl)&0xf)
80 #define getth_off(base) ((((base).th_xoff)&0xf0)>>4)
82 #else
83 #define getip_hl(base) ((base).ip_hl)
84 #define getth_off(base) ((base).th_off)
85 #endif
87 void
88 vj_compress_init(comp, max_state)
89 struct vjcompress *comp;
90 int max_state;
92 register u_int i;
93 register struct cstate *tstate = comp->tstate;
95 if (max_state == -1)
96 max_state = MAX_STATES - 1;
97 bzero((char *)comp, sizeof(*comp));
98 for (i = max_state; i > 0; --i) {
99 tstate[i].cs_id = i;
100 tstate[i].cs_next = &tstate[i - 1];
102 tstate[0].cs_next = &tstate[max_state];
103 tstate[0].cs_id = 0;
104 comp->last_cs = &tstate[0];
105 comp->last_recv = 255;
106 comp->last_xmit = 255;
107 comp->flags = VJF_TOSS;
111 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
112 * checks for zero (since zero has to be encoded in the long, 3 byte
113 * form).
115 #define ENCODE(n) { \
116 if ((u_short)(n) >= 256) { \
117 *cp++ = 0; \
118 cp[1] = (n); \
119 cp[0] = (n) >> 8; \
120 cp += 2; \
121 } else { \
122 *cp++ = (n); \
125 #define ENCODEZ(n) { \
126 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
127 *cp++ = 0; \
128 cp[1] = (n); \
129 cp[0] = (n) >> 8; \
130 cp += 2; \
131 } else { \
132 *cp++ = (n); \
136 #define DECODEL(f) { \
137 if (*cp == 0) {\
138 u_int32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
139 (f) = htonl(tmp); \
140 cp += 3; \
141 } else { \
142 u_int32_t tmp = ntohl(f) + (u_int32_t)*cp++; \
143 (f) = htonl(tmp); \
147 #define DECODES(f) { \
148 if (*cp == 0) {\
149 u_short tmp = ntohs(f) + ((cp[1] << 8) | cp[2]); \
150 (f) = htons(tmp); \
151 cp += 3; \
152 } else { \
153 u_short tmp = ntohs(f) + (u_int32_t)*cp++; \
154 (f) = htons(tmp); \
158 #define DECODEU(f) { \
159 if (*cp == 0) {\
160 (f) = htons((cp[1] << 8) | cp[2]); \
161 cp += 3; \
162 } else { \
163 (f) = htons((u_int32_t)*cp++); \
167 u_int
168 vj_compress_tcp(ip, mlen, comp, compress_cid, vjhdrp)
169 register struct ip *ip;
170 u_int mlen;
171 struct vjcompress *comp;
172 int compress_cid;
173 u_char **vjhdrp;
175 register struct cstate *cs = comp->last_cs->cs_next;
176 register u_int hlen = getip_hl(*ip);
177 register struct tcphdr *oth;
178 register struct tcphdr *th;
179 register u_int deltaS, deltaA;
180 register u_int changes = 0;
181 u_char new_seq[16];
182 register u_char *cp = new_seq;
185 * Bail if this is an IP fragment or if the TCP packet isn't
186 * `compressible' (i.e., ACK isn't set or some other control bit is
187 * set). (We assume that the caller has already made sure the
188 * packet is IP proto TCP).
190 if ((ip->ip_off & htons(0x3fff)) || mlen < 40)
191 return (TYPE_IP);
193 th = (struct tcphdr *)&((int *)ip)[hlen];
194 if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
195 return (TYPE_IP);
197 * Packet is compressible -- we're going to send either a
198 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
199 * to locate (or create) the connection state. Special case the
200 * most recently used connection since it's most likely to be used
201 * again & we don't have to do any reordering if it's used.
203 INCR(vjs_packets);
204 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
205 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
206 *(int *)th != ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)]) {
208 * Wasn't the first -- search for it.
210 * States are kept in a circularly linked list with
211 * last_cs pointing to the end of the list. The
212 * list is kept in lru order by moving a state to the
213 * head of the list whenever it is referenced. Since
214 * the list is short and, empirically, the connection
215 * we want is almost always near the front, we locate
216 * states via linear search. If we don't find a state
217 * for the datagram, the oldest state is (re-)used.
219 register struct cstate *lcs;
220 register struct cstate *lastcs = comp->last_cs;
222 do {
223 lcs = cs; cs = cs->cs_next;
224 INCR(vjs_searches);
225 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
226 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
227 && *(int *)th == ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)])
228 goto found;
229 } while (cs != lastcs);
232 * Didn't find it -- re-use oldest cstate. Send an
233 * uncompressed packet that tells the other side what
234 * connection number we're using for this conversation.
235 * Note that since the state list is circular, the oldest
236 * state points to the newest and we only need to set
237 * last_cs to update the lru linkage.
239 INCR(vjs_misses);
240 comp->last_cs = lcs;
241 hlen += getth_off(*th);
242 hlen <<= 2;
243 if (hlen > mlen)
244 return (TYPE_IP);
245 goto uncompressed;
247 found:
249 * Found it -- move to the front on the connection list.
251 if (cs == lastcs)
252 comp->last_cs = lcs;
253 else {
254 lcs->cs_next = cs->cs_next;
255 cs->cs_next = lastcs->cs_next;
256 lastcs->cs_next = cs;
261 * Make sure that only what we expect to change changed. The first
262 * line of the `if' checks the IP protocol version, header length &
263 * type of service. The 2nd line checks the "Don't fragment" bit.
264 * The 3rd line checks the time-to-live and protocol (the protocol
265 * check is unnecessary but costless). The 4th line checks the TCP
266 * header length. The 5th line checks IP options, if any. The 6th
267 * line checks TCP options, if any. If any of these things are
268 * different between the previous & current datagram, we send the
269 * current datagram `uncompressed'.
271 oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen];
272 deltaS = hlen;
273 hlen += getth_off(*th);
274 hlen <<= 2;
275 if (hlen > mlen)
276 return (TYPE_IP);
278 if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] ||
279 ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] ||
280 ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] ||
281 getth_off(*th) != getth_off(*oth) ||
282 (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
283 (getth_off(*th) > 5 && BCMP(th + 1, oth + 1, (getth_off(*th) - 5) << 2)))
284 goto uncompressed;
287 * Figure out which of the changing fields changed. The
288 * receiver expects changes in the order: urgent, window,
289 * ack, seq (the order minimizes the number of temporaries
290 * needed in this section of code).
292 if (th->th_flags & TH_URG) {
293 deltaS = ntohs(th->th_urp);
294 ENCODEZ(deltaS);
295 changes |= NEW_U;
296 } else if (th->th_urp != oth->th_urp)
297 /* argh! URG not set but urp changed -- a sensible
298 * implementation should never do this but RFC793
299 * doesn't prohibit the change so we have to deal
300 * with it. */
301 goto uncompressed;
303 if ((deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) > 0) {
304 ENCODE(deltaS);
305 changes |= NEW_W;
308 if ((deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) > 0) {
309 if (deltaA > 0xffff)
310 goto uncompressed;
311 ENCODE(deltaA);
312 changes |= NEW_A;
315 if ((deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) > 0) {
316 if (deltaS > 0xffff)
317 goto uncompressed;
318 ENCODE(deltaS);
319 changes |= NEW_S;
322 switch(changes) {
324 case 0:
326 * Nothing changed. If this packet contains data and the
327 * last one didn't, this is probably a data packet following
328 * an ack (normal on an interactive connection) and we send
329 * it compressed. Otherwise it's probably a retransmit,
330 * retransmitted ack or window probe. Send it uncompressed
331 * in case the other side missed the compressed version.
333 if (ip->ip_len != cs->cs_ip.ip_len &&
334 ntohs(cs->cs_ip.ip_len) == hlen)
335 break;
337 /* (fall through) */
339 case SPECIAL_I:
340 case SPECIAL_D:
342 * actual changes match one of our special case encodings --
343 * send packet uncompressed.
345 goto uncompressed;
347 case NEW_S|NEW_A:
348 if (deltaS == deltaA && deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
349 /* special case for echoed terminal traffic */
350 changes = SPECIAL_I;
351 cp = new_seq;
353 break;
355 case NEW_S:
356 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
357 /* special case for data xfer */
358 changes = SPECIAL_D;
359 cp = new_seq;
361 break;
364 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
365 if (deltaS != 1) {
366 ENCODEZ(deltaS);
367 changes |= NEW_I;
369 if (th->th_flags & TH_PUSH)
370 changes |= TCP_PUSH_BIT;
372 * Grab the cksum before we overwrite it below. Then update our
373 * state with this packet's header.
375 deltaA = ntohs(th->th_sum);
376 BCOPY(ip, &cs->cs_ip, hlen);
379 * We want to use the original packet as our compressed packet.
380 * (cp - new_seq) is the number of bytes we need for compressed
381 * sequence numbers. In addition we need one byte for the change
382 * mask, one for the connection id and two for the tcp checksum.
383 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
384 * many bytes of the original packet to toss so subtract the two to
385 * get the new packet size.
387 deltaS = cp - new_seq;
388 cp = (u_char *)ip;
389 if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
390 comp->last_xmit = cs->cs_id;
391 hlen -= deltaS + 4;
392 *vjhdrp = (cp += hlen);
393 *cp++ = changes | NEW_C;
394 *cp++ = cs->cs_id;
395 } else {
396 hlen -= deltaS + 3;
397 *vjhdrp = (cp += hlen);
398 *cp++ = changes;
400 *cp++ = deltaA >> 8;
401 *cp++ = deltaA;
402 BCOPY(new_seq, cp, deltaS);
403 INCR(vjs_compressed);
404 return (TYPE_COMPRESSED_TCP);
407 * Update connection state cs & send uncompressed packet (that is,
408 * a regular ip/tcp packet but with the 'conversation id' we hope
409 * to use on future compressed packets in the protocol field).
411 uncompressed:
412 BCOPY(ip, &cs->cs_ip, hlen);
413 ip->ip_p = cs->cs_id;
414 comp->last_xmit = cs->cs_id;
415 return (TYPE_UNCOMPRESSED_TCP);
419 * Called when we may have missed a packet.
421 void
422 vj_uncompress_err(comp)
423 struct vjcompress *comp;
425 comp->flags |= VJF_TOSS;
426 INCR(vjs_errorin);
430 * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
433 vj_uncompress_uncomp(buf, buflen, comp)
434 u_char *buf;
435 int buflen;
436 struct vjcompress *comp;
438 register u_int hlen;
439 register struct cstate *cs;
440 register struct ip *ip;
442 ip = (struct ip *) buf;
443 hlen = getip_hl(*ip) << 2;
444 if (ip->ip_p >= MAX_STATES
445 || hlen + sizeof(struct tcphdr) > buflen
446 || (hlen += getth_off(*((struct tcphdr *)&((char *)ip)[hlen])) << 2)
447 > buflen
448 || hlen > MAX_HDR) {
449 comp->flags |= VJF_TOSS;
450 INCR(vjs_errorin);
451 return (0);
453 cs = &comp->rstate[comp->last_recv = ip->ip_p];
454 comp->flags &=~ VJF_TOSS;
455 ip->ip_p = IPPROTO_TCP;
456 BCOPY(ip, &cs->cs_ip, hlen);
457 cs->cs_hlen = hlen;
458 INCR(vjs_uncompressedin);
459 return (1);
463 * Uncompress a packet of type TYPE_COMPRESSED_TCP.
464 * The packet starts at buf and is of total length total_len.
465 * The first buflen bytes are at buf; this must include the entire
466 * compressed TCP/IP header. This procedure returns the length
467 * of the VJ header, with a pointer to the uncompressed IP header
468 * in *hdrp and its length in *hlenp.
471 vj_uncompress_tcp(buf, buflen, total_len, comp, hdrp, hlenp)
472 u_char *buf;
473 int buflen, total_len;
474 struct vjcompress *comp;
475 u_char **hdrp;
476 u_int *hlenp;
478 register u_char *cp;
479 register u_int hlen, changes;
480 register struct tcphdr *th;
481 register struct cstate *cs;
482 register u_short *bp;
483 register u_int vjlen;
484 register u_int32_t tmp;
486 INCR(vjs_compressedin);
487 cp = buf;
488 changes = *cp++;
489 if (changes & NEW_C) {
490 /* Make sure the state index is in range, then grab the state.
491 * If we have a good state index, clear the 'discard' flag. */
492 if (*cp >= MAX_STATES)
493 goto bad;
495 comp->flags &=~ VJF_TOSS;
496 comp->last_recv = *cp++;
497 } else {
498 /* this packet has an implicit state index. If we've
499 * had a line error since the last time we got an
500 * explicit state index, we have to toss the packet. */
501 if (comp->flags & VJF_TOSS) {
502 INCR(vjs_tossed);
503 return (-1);
506 cs = &comp->rstate[comp->last_recv];
507 hlen = getip_hl(cs->cs_ip) << 2;
508 th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
509 th->th_sum = htons((*cp << 8) | cp[1]);
510 cp += 2;
511 if (changes & TCP_PUSH_BIT)
512 th->th_flags |= TH_PUSH;
513 else
514 th->th_flags &=~ TH_PUSH;
516 switch (changes & SPECIALS_MASK) {
517 case SPECIAL_I:
519 register u_int32_t i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
520 /* some compilers can't nest inline assembler.. */
521 tmp = ntohl(th->th_ack) + i;
522 th->th_ack = htonl(tmp);
523 tmp = ntohl(th->th_seq) + i;
524 th->th_seq = htonl(tmp);
526 break;
528 case SPECIAL_D:
529 /* some compilers can't nest inline assembler.. */
530 tmp = ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
531 th->th_seq = htonl(tmp);
532 break;
534 default:
535 if (changes & NEW_U) {
536 th->th_flags |= TH_URG;
537 DECODEU(th->th_urp);
538 } else
539 th->th_flags &=~ TH_URG;
540 if (changes & NEW_W)
541 DECODES(th->th_win);
542 if (changes & NEW_A)
543 DECODEL(th->th_ack);
544 if (changes & NEW_S)
545 DECODEL(th->th_seq);
546 break;
548 if (changes & NEW_I) {
549 DECODES(cs->cs_ip.ip_id);
550 } else {
551 cs->cs_ip.ip_id = ntohs(cs->cs_ip.ip_id) + 1;
552 cs->cs_ip.ip_id = htons(cs->cs_ip.ip_id);
556 * At this point, cp points to the first byte of data in the
557 * packet. Fill in the IP total length and update the IP
558 * header checksum.
560 vjlen = cp - buf;
561 buflen -= vjlen;
562 if (buflen < 0)
563 /* we must have dropped some characters (crc should detect
564 * this but the old slip framing won't) */
565 goto bad;
567 total_len += cs->cs_hlen - vjlen;
568 cs->cs_ip.ip_len = htons(total_len);
570 /* recompute the ip header checksum */
571 bp = (u_short *) &cs->cs_ip;
572 cs->cs_ip.ip_sum = 0;
573 for (changes = 0; hlen > 0; hlen -= 2)
574 changes += *bp++;
575 changes = (changes & 0xffff) + (changes >> 16);
576 changes = (changes & 0xffff) + (changes >> 16);
577 cs->cs_ip.ip_sum = ~ changes;
579 *hdrp = (u_char *) &cs->cs_ip;
580 *hlenp = cs->cs_hlen;
581 return vjlen;
583 bad:
584 comp->flags |= VJF_TOSS;
585 INCR(vjs_errorin);
586 return (-1);