xref: /freebsd/usr.sbin/ppp/vjcomp.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 
38 #include <stdio.h>
39 #include <string.h>		/* strlen/memcpy */
40 #include <termios.h>
41 
42 #include "layer.h"
43 #include "mbuf.h"
44 #include "log.h"
45 #include "timer.h"
46 #include "fsm.h"
47 #include "proto.h"
48 #include "slcompress.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "defs.h"
52 #include "iplist.h"
53 #include "throughput.h"
54 #include "ncpaddr.h"
55 #include "ip.h"
56 #include "ipcp.h"
57 #include "lcp.h"
58 #include "ccp.h"
59 #include "link.h"
60 #include "filter.h"
61 #include "descriptor.h"
62 #include "mp.h"
63 #ifndef NORADIUS
64 #include "radius.h"
65 #endif
66 #include "ipv6cp.h"
67 #include "ncp.h"
68 #include "bundle.h"
69 #include "vjcomp.h"
70 
71 #define MAX_VJHEADER 16		/* Maximum size of compressed header */
72 
73 static struct mbuf *
74 vj_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp, int pri,
75              u_short *proto)
76 {
77   int type;
78   struct ip *pip;
79   u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
80 
81   bp = m_pullup(bp);
82   pip = (struct ip *)MBUF_CTOP(bp);
83   if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP &&
84       cproto == PROTO_VJCOMP) {
85     type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc,
86                            &bundle->ncp.ipcp.vj.slstat,
87                            bundle->ncp.ipcp.peer_compproto & 0xff);
88     log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type);
89     switch (type) {
90     case TYPE_IP:
91       break;
92 
93     case TYPE_UNCOMPRESSED_TCP:
94       *proto = PROTO_VJUNCOMP;
95       log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
96       m_settype(bp, MB_VJOUT);
97       break;
98 
99     case TYPE_COMPRESSED_TCP:
100       *proto = PROTO_VJCOMP;
101       log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
102       m_settype(bp, MB_VJOUT);
103       break;
104 
105     default:
106       log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
107       m_freem(bp);
108       return NULL;
109     }
110   }
111 
112   return bp;
113 }
114 
115 static struct mbuf *
116 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
117 {
118   u_char *bufp;
119   int len, olen, rlen;
120   u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
121 
122   bp = m_pullup(bp);
123   olen = len = m_length(bp);
124   if (type == TYPE_UNCOMPRESSED_TCP) {
125     /*
126      * Uncompressed packet does NOT change its size, so that we can use mbuf
127      * space for uncompression job.
128      */
129     bufp = MBUF_CTOP(bp);
130     len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
131                             (ipcp->my_compproto >> 8) & 255);
132     if (len <= 0) {
133       m_freem(bp);
134       bp = NULL;
135     } else
136       m_settype(bp, MB_VJIN);
137     return bp;
138   }
139 
140   /*
141    * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
142    * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
143    * Copy unread data info there.
144    */
145   if (len > MAX_VJHEADER)
146     len = MAX_VJHEADER;
147   rlen = len;
148   bufp = work + MAX_HDR;
149   bp = mbuf_Read(bp, bufp, rlen);
150   len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
151                           (ipcp->my_compproto >> 8) & 255);
152   if (len <= 0) {
153     m_freem(bp);
154     return NULL;
155   }
156   len -= olen;
157   len += rlen;
158 
159   bp = m_prepend(bp, bufp, len, 0);
160   m_settype(bp, MB_VJIN);
161 
162   return bp;
163 }
164 
165 static struct mbuf *
166 vj_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
167              u_short *proto)
168 {
169   u_char type;
170 
171   switch (*proto) {
172   case PROTO_VJCOMP:
173     type = TYPE_COMPRESSED_TCP;
174     log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n");
175     break;
176   case PROTO_VJUNCOMP:
177     type = TYPE_UNCOMPRESSED_TCP;
178     log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n");
179     break;
180   default:
181     return bp;
182   }
183 
184   *proto = PROTO_IP;
185   return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
186 }
187 
188 const char *
189 vj2asc(u_int32_t val)
190 {
191   static char asc[50];		/* The return value is used immediately */
192 
193   if (val)
194     snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
195             (int)((val>>8)&15)+1, val & 1 ?  "" : "out");
196   else
197     strcpy(asc, "VJ disabled");
198   return asc;
199 }
200 
201 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };
202