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