]> git.ozlabs.org Git - ppp.git/blob - NeXT/vjcompress.c
Support PPP Deflate compression
[ppp.git] / NeXT / 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
28 /*
29  * This version is used under SunOS 4.x, DEC Alpha OSF/1, AIX 4.x,
30  * and SVR4 systems including Solaris 2.
31  *
32  * $Id: vjcompress.c,v 1.1 1995/12/18 03:30:20 paulus Exp $
33  */
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37
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
46
47 #ifdef __osf__
48 #include <net/net_globals.h>
49 #endif
50 #include <netinet/in.h>
51
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
58
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
61
62 #include <net/ppp_defs.h>
63 #include <net/vjcompress.h>
64
65 #ifndef VJ_NO_STATS
66 #define INCR(counter) ++comp->stats.counter
67 #else
68 #define INCR(counter)
69 #endif
70
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
77
78 #ifdef __osf__
79 #define getip_hl(base)  (((base).ip_vhl)&0xf)
80 #define getth_off(base) ((((base).th_xoff)&0xf0)>>4)
81
82 #else
83 #define getip_hl(base)  ((base).ip_hl)
84 #define getth_off(base) ((base).th_off)
85 #endif
86
87 void
88 vj_compress_init(comp, max_state)
89     struct vjcompress *comp;
90     int max_state;
91 {
92     register u_int i;
93     register struct cstate *tstate = comp->tstate;
94
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];
101     }
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;
108 }
109
110
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).
114  */
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); \
123         } \
124 }
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); \
133         } \
134 }
135
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); \
144         } \
145 }
146
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); \
155         } \
156 }
157
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++); \
164         } \
165 }
166
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;
174 {
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;
183
184     /*
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).
189      */
190     if ((ip->ip_off & htons(0x3fff)) || mlen < 40)
191         return (TYPE_IP);
192
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);
196     /*
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.
202      */
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)]) {
207         /*
208          * Wasn't the first -- search for it.
209          *
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.
218          */
219         register struct cstate *lcs;
220         register struct cstate *lastcs = comp->last_cs;
221
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);
230
231         /*
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.
238          */
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;
246
247     found:
248         /*
249          * Found it -- move to the front on the connection list.
250          */
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;
257         }
258     }
259
260     /*
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'.
270      */
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);
277
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;
285
286     /*
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).
291      */
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;
302
303     if (deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) {
304         ENCODE(deltaS);
305         changes |= NEW_W;
306     }
307
308     if (deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) {
309         if (deltaA > 0xffff)
310             goto uncompressed;
311         ENCODE(deltaA);
312         changes |= NEW_A;
313     }
314
315     if (deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) {
316         if (deltaS > 0xffff)
317             goto uncompressed;
318         ENCODE(deltaS);
319         changes |= NEW_S;
320     }
321
322     switch(changes) {
323
324     case 0:
325         /*
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.
332          */
333         if (ip->ip_len != cs->cs_ip.ip_len &&
334             ntohs(cs->cs_ip.ip_len) == hlen)
335             break;
336
337         /* (fall through) */
338
339     case SPECIAL_I:
340     case SPECIAL_D:
341         /*
342          * actual changes match one of our special case encodings --
343          * send packet uncompressed.
344          */
345         goto uncompressed;
346
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;
352         }
353         break;
354
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;
360         }
361         break;
362     }
363
364     deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
365     if (deltaS != 1) {
366         ENCODEZ(deltaS);
367         changes |= NEW_I;
368     }
369     if (th->th_flags & TH_PUSH)
370         changes |= TCP_PUSH_BIT;
371     /*
372      * Grab the cksum before we overwrite it below.  Then update our
373      * state with this packet's header.
374      */
375     deltaA = ntohs(th->th_sum);
376     BCOPY(ip, &cs->cs_ip, hlen);
377
378     /*
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.
386      */
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;
399     }
400     *cp++ = deltaA >> 8;
401     *cp++ = deltaA;
402     BCOPY(new_seq, cp, deltaS);
403     INCR(vjs_compressed);
404     return (TYPE_COMPRESSED_TCP);
405
406     /*
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).
410      */
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);
416 }
417
418 /*
419  * Called when we may have missed a packet.
420  */
421 void
422 vj_uncompress_err(comp)
423     struct vjcompress *comp;
424 {
425     comp->flags |= VJF_TOSS;
426     INCR(vjs_errorin);
427 }
428
429 /*
430  * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
431  */
432 int
433 vj_uncompress_uncomp(buf, comp)
434     u_char *buf;
435     struct vjcompress *comp;
436 {
437     register u_int hlen;
438     register struct cstate *cs;
439     register struct ip *ip;
440
441     ip = (struct ip *) buf;
442     if (ip->ip_p >= MAX_STATES) {
443         comp->flags |= VJF_TOSS;
444         INCR(vjs_errorin);
445         return (0);
446     }
447     cs = &comp->rstate[comp->last_recv = ip->ip_p];
448     comp->flags &=~ VJF_TOSS;
449     ip->ip_p = IPPROTO_TCP;
450     hlen = getip_hl(*ip);
451     hlen += getth_off(*((struct tcphdr *)&((int *)ip)[hlen]));
452     hlen <<= 2;
453     BCOPY(ip, &cs->cs_ip, hlen);
454     cs->cs_hlen = hlen;
455     INCR(vjs_uncompressedin);
456     return (1);
457 }
458
459 /*
460  * Uncompress a packet of type TYPE_COMPRESSED_TCP.
461  * The packet starts at buf and is of total length total_len.
462  * The first buflen bytes are at buf; this must include the entire
463  * compressed TCP/IP header.  This procedure returns the length
464  * of the VJ header, with a pointer to the uncompressed IP header
465  * in *hdrp and its length in *hlenp.
466  */
467 int
468 vj_uncompress_tcp(buf, buflen, total_len, comp, hdrp, hlenp)
469     u_char *buf;
470     int buflen, total_len;
471     struct vjcompress *comp;
472     u_char **hdrp;
473     u_int *hlenp;
474 {
475     register u_char *cp;
476     register u_int hlen, changes;
477     register struct tcphdr *th;
478     register struct cstate *cs;
479     register u_short *bp;
480     register u_int vjlen;
481     register u_int32_t tmp;
482
483     INCR(vjs_compressedin);
484     cp = buf;
485     changes = *cp++;
486     if (changes & NEW_C) {
487         /* Make sure the state index is in range, then grab the state.
488          * If we have a good state index, clear the 'discard' flag. */
489         if (*cp >= MAX_STATES)
490             goto bad;
491
492         comp->flags &=~ VJF_TOSS;
493         comp->last_recv = *cp++;
494     } else {
495         /* this packet has an implicit state index.  If we've
496          * had a line error since the last time we got an
497          * explicit state index, we have to toss the packet. */
498         if (comp->flags & VJF_TOSS) {
499             INCR(vjs_tossed);
500             return (-1);
501         }
502     }
503     cs = &comp->rstate[comp->last_recv];
504     hlen = getip_hl(cs->cs_ip) << 2;
505     th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
506     th->th_sum = htons((*cp << 8) | cp[1]);
507     cp += 2;
508     if (changes & TCP_PUSH_BIT)
509         th->th_flags |= TH_PUSH;
510     else
511         th->th_flags &=~ TH_PUSH;
512
513     switch (changes & SPECIALS_MASK) {
514     case SPECIAL_I:
515         {
516             register u_int32_t i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
517             /* some compilers can't nest inline assembler.. */
518             tmp = ntohl(th->th_ack) + i;
519             th->th_ack = htonl(tmp);
520             tmp = ntohl(th->th_seq) + i;
521             th->th_seq = htonl(tmp);
522         }
523         break;
524
525     case SPECIAL_D:
526         /* some compilers can't nest inline assembler.. */
527         tmp = ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
528         th->th_seq = htonl(tmp);
529         break;
530
531     default:
532         if (changes & NEW_U) {
533             th->th_flags |= TH_URG;
534             DECODEU(th->th_urp);
535         } else
536             th->th_flags &=~ TH_URG;
537         if (changes & NEW_W)
538             DECODES(th->th_win);
539         if (changes & NEW_A)
540             DECODEL(th->th_ack);
541         if (changes & NEW_S)
542             DECODEL(th->th_seq);
543         break;
544     }
545     if (changes & NEW_I) {
546         DECODES(cs->cs_ip.ip_id);
547     } else {
548         cs->cs_ip.ip_id = ntohs(cs->cs_ip.ip_id) + 1;
549         cs->cs_ip.ip_id = htons(cs->cs_ip.ip_id);
550     }
551
552     /*
553      * At this point, cp points to the first byte of data in the
554      * packet.  Fill in the IP total length and update the IP
555      * header checksum.
556      */
557     vjlen = cp - buf;
558     buflen -= vjlen;
559     if (buflen < 0)
560         /* we must have dropped some characters (crc should detect
561          * this but the old slip framing won't) */
562         goto bad;
563
564     total_len += cs->cs_hlen - vjlen;
565     cs->cs_ip.ip_len = htons(total_len);
566
567     /* recompute the ip header checksum */
568     bp = (u_short *) &cs->cs_ip;
569     cs->cs_ip.ip_sum = 0;
570     for (changes = 0; hlen > 0; hlen -= 2)
571         changes += *bp++;
572     changes = (changes & 0xffff) + (changes >> 16);
573     changes = (changes & 0xffff) + (changes >> 16);
574     cs->cs_ip.ip_sum = ~ changes;
575
576     *hdrp = (u_char *) &cs->cs_ip;
577     *hlenp = cs->cs_hlen;
578     return vjlen;
579
580  bad:
581     comp->flags |= VJF_TOSS;
582     INCR(vjs_errorin);
583     return (-1);
584 }