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