xref: /freebsd/usr.sbin/ppp/vjcomp.c (revision 53c9f6c0c48df668bb8fa282cd0b12cb8690bcce)
1af57ed9fSAtsushi Murai /*
2af57ed9fSAtsushi Murai  *	       Input/Output VJ Compressed packets
3af57ed9fSAtsushi Murai  *
4af57ed9fSAtsushi Murai  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5af57ed9fSAtsushi Murai  *
6af57ed9fSAtsushi Murai  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7af57ed9fSAtsushi Murai  *
8af57ed9fSAtsushi Murai  * Redistribution and use in source and binary forms are permitted
9af57ed9fSAtsushi Murai  * provided that the above copyright notice and this paragraph are
10af57ed9fSAtsushi Murai  * duplicated in all such forms and that any documentation,
11af57ed9fSAtsushi Murai  * advertising materials, and other materials related to such
12af57ed9fSAtsushi Murai  * distribution and use acknowledge that the software was developed
13af57ed9fSAtsushi Murai  * by the Internet Initiative Japan, Inc.  The name of the
14af57ed9fSAtsushi Murai  * IIJ may not be used to endorse or promote products derived
15af57ed9fSAtsushi Murai  * from this software without specific prior written permission.
16af57ed9fSAtsushi Murai  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17af57ed9fSAtsushi Murai  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18af57ed9fSAtsushi Murai  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19af57ed9fSAtsushi Murai  *
20af57ed9fSAtsushi Murai  * $Id:$
21af57ed9fSAtsushi Murai  *
22af57ed9fSAtsushi Murai  *  TODO:
23af57ed9fSAtsushi Murai  */
24af57ed9fSAtsushi Murai #include "fsm.h"
25af57ed9fSAtsushi Murai #include "lcpproto.h"
26af57ed9fSAtsushi Murai #include <netinet/in_systm.h>
27af57ed9fSAtsushi Murai #include <netinet/ip.h>
28af57ed9fSAtsushi Murai #include "slcompress.h"
29af57ed9fSAtsushi Murai #include "hdlc.h"
30af57ed9fSAtsushi Murai #include "ipcp.h"
31af57ed9fSAtsushi Murai 
32af57ed9fSAtsushi Murai #define MAX_VJHEADER 16	/* Maximum size of compressed header */
33af57ed9fSAtsushi Murai 
34af57ed9fSAtsushi Murai struct slcompress cslc;
35af57ed9fSAtsushi Murai 
36af57ed9fSAtsushi Murai void
37af57ed9fSAtsushi Murai VjInit()
38af57ed9fSAtsushi Murai {
39af57ed9fSAtsushi Murai   sl_compress_init(&cslc);
40af57ed9fSAtsushi Murai }
41af57ed9fSAtsushi Murai 
42af57ed9fSAtsushi Murai void
4353c9f6c0SAtsushi Murai SendPppFrame(pri, bp)
44af57ed9fSAtsushi Murai int pri;
45af57ed9fSAtsushi Murai struct mbuf *bp;
46af57ed9fSAtsushi Murai {
47af57ed9fSAtsushi Murai   int type;
48af57ed9fSAtsushi Murai   int proto;
49af57ed9fSAtsushi Murai   int cproto = IpcpInfo.his_compproto >> 16;
50af57ed9fSAtsushi Murai 
51af57ed9fSAtsushi Murai #ifdef DEBUG
5253c9f6c0SAtsushi Murai   logprintf("SendPppFrame: proto = %x\n", IpcpInfo.his_compproto);
53af57ed9fSAtsushi Murai #endif
54af57ed9fSAtsushi Murai   if (cproto== PROTO_VJCOMP) {
5553c9f6c0SAtsushi Murai     type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp), &cslc, IpcpInfo.his_compproto & 0xff);
56af57ed9fSAtsushi Murai 
57af57ed9fSAtsushi Murai #ifdef DEBUG
58af57ed9fSAtsushi Murai     logprintf("type = %x\n", type);
59af57ed9fSAtsushi Murai #endif
60af57ed9fSAtsushi Murai     switch (type) {
61af57ed9fSAtsushi Murai     case TYPE_IP:
62af57ed9fSAtsushi Murai       proto = PROTO_IP;
63af57ed9fSAtsushi Murai       break;
64af57ed9fSAtsushi Murai     case TYPE_UNCOMPRESSED_TCP:
65af57ed9fSAtsushi Murai       proto = PROTO_VJUNCOMP;
66af57ed9fSAtsushi Murai       break;
67af57ed9fSAtsushi Murai     case TYPE_COMPRESSED_TCP:
68af57ed9fSAtsushi Murai       proto = PROTO_VJCOMP;
69af57ed9fSAtsushi Murai       break;
70af57ed9fSAtsushi Murai     default:
71af57ed9fSAtsushi Murai       logprintf("unknown type %x\n", type);
72af57ed9fSAtsushi Murai       pfree(bp);
73af57ed9fSAtsushi Murai       return;
74af57ed9fSAtsushi Murai     }
75af57ed9fSAtsushi Murai   } else
76af57ed9fSAtsushi Murai     proto = PROTO_IP;
77af57ed9fSAtsushi Murai   HdlcOutput(pri, proto, bp);
78af57ed9fSAtsushi Murai }
79af57ed9fSAtsushi Murai 
80af57ed9fSAtsushi Murai static struct mbuf *
81af57ed9fSAtsushi Murai VjUncompressTcp(bp, type)
82af57ed9fSAtsushi Murai struct mbuf *bp;
83af57ed9fSAtsushi Murai u_char type;
84af57ed9fSAtsushi Murai {
85af57ed9fSAtsushi Murai   u_char *bufp;
86af57ed9fSAtsushi Murai   int len, olen, rlen;
87af57ed9fSAtsushi Murai   struct mbuf *nbp;
88af57ed9fSAtsushi Murai   u_char work[MAX_HDR+MAX_VJHEADER];   /* enough to hold TCP/IP header */
89af57ed9fSAtsushi Murai 
90af57ed9fSAtsushi Murai   olen = len = plength(bp);
91af57ed9fSAtsushi Murai   if (type == TYPE_UNCOMPRESSED_TCP) {
92af57ed9fSAtsushi Murai     /*
93af57ed9fSAtsushi Murai      * Uncompressed packet does NOT change its size, so that we can
94af57ed9fSAtsushi Murai      * use mbuf space for uncompression job.
95af57ed9fSAtsushi Murai      */
96af57ed9fSAtsushi Murai     bufp = MBUF_CTOP(bp);
97af57ed9fSAtsushi Murai     len = sl_uncompress_tcp(&bufp, len, type, &cslc);
9853c9f6c0SAtsushi Murai     if (len <= 0) {
9953c9f6c0SAtsushi Murai       pfree(bp);
10053c9f6c0SAtsushi Murai       bp = NULLBUFF;
10153c9f6c0SAtsushi Murai     }
102af57ed9fSAtsushi Murai     return(bp);
103af57ed9fSAtsushi Murai   }
104af57ed9fSAtsushi Murai   /*
105af57ed9fSAtsushi Murai    *  Handle compressed packet.
106af57ed9fSAtsushi Murai    *    1) Read upto MAX_VJHEADER bytes into work space.
107af57ed9fSAtsushi Murai    *	2) Try to uncompress it.
108af57ed9fSAtsushi Murai    *    3) Compute amount of necesary space.
109af57ed9fSAtsushi Murai    *    4) Copy unread data info there.
110af57ed9fSAtsushi Murai    */
111af57ed9fSAtsushi Murai   if (len > MAX_VJHEADER) len = MAX_VJHEADER;
112af57ed9fSAtsushi Murai   rlen = len;
113af57ed9fSAtsushi Murai   bufp = work + MAX_HDR;
114af57ed9fSAtsushi Murai   bp = mbread(bp, bufp, rlen);
115af57ed9fSAtsushi Murai   len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
11653c9f6c0SAtsushi Murai   if (len <= 0) {
11753c9f6c0SAtsushi Murai     pfree(bp);
11853c9f6c0SAtsushi Murai     return NULLBUFF;
11953c9f6c0SAtsushi Murai   }
120af57ed9fSAtsushi Murai   len -= olen;
121af57ed9fSAtsushi Murai   len += rlen;
122af57ed9fSAtsushi Murai   nbp = mballoc(len, MB_VJCOMP);
123af57ed9fSAtsushi Murai   bcopy(bufp, MBUF_CTOP(nbp), len);
124af57ed9fSAtsushi Murai   nbp->next = bp;
125af57ed9fSAtsushi Murai   return(nbp);
126af57ed9fSAtsushi Murai }
127af57ed9fSAtsushi Murai 
128af57ed9fSAtsushi Murai struct mbuf *
129af57ed9fSAtsushi Murai VjCompInput(bp, proto)
130af57ed9fSAtsushi Murai struct mbuf *bp;
131af57ed9fSAtsushi Murai int proto;
132af57ed9fSAtsushi Murai {
133af57ed9fSAtsushi Murai   u_char type;
134af57ed9fSAtsushi Murai 
135af57ed9fSAtsushi Murai #ifdef DEBUG
136af57ed9fSAtsushi Murai   logprintf("VjCompInput (%02x):\n", proto);
137af57ed9fSAtsushi Murai   DumpBp(bp);
138af57ed9fSAtsushi Murai #endif
139af57ed9fSAtsushi Murai 
140af57ed9fSAtsushi Murai   switch (proto) {
141af57ed9fSAtsushi Murai   case PROTO_VJCOMP:
142af57ed9fSAtsushi Murai     type = TYPE_COMPRESSED_TCP;
143af57ed9fSAtsushi Murai     break;
144af57ed9fSAtsushi Murai   case PROTO_VJUNCOMP:
145af57ed9fSAtsushi Murai     type = TYPE_UNCOMPRESSED_TCP;
146af57ed9fSAtsushi Murai     break;
147af57ed9fSAtsushi Murai   default:
148af57ed9fSAtsushi Murai     logprintf("???\n");
149af57ed9fSAtsushi Murai     return(bp);
150af57ed9fSAtsushi Murai   }
151af57ed9fSAtsushi Murai   bp = VjUncompressTcp(bp, type);
152af57ed9fSAtsushi Murai   return(bp);
153af57ed9fSAtsushi Murai }
154