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