xref: /freebsd/usr.sbin/ppp/vjcomp.c (revision 12f05b84463baacfada5a79eaed061a4899d98aa)
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 "ipcp.h"
56 #include "lcp.h"
57 #include "ccp.h"
58 #include "link.h"
59 #include "filter.h"
60 #include "descriptor.h"
61 #include "mp.h"
62 #ifndef NORADIUS
63 #include "radius.h"
64 #endif
65 #include "ipv6cp.h"
66 #include "ncp.h"
67 #include "bundle.h"
68 #include "vjcomp.h"
69 
70 #define MAX_VJHEADER 16		/* Maximum size of compressed header */
71 
72 static struct mbuf *
73 vj_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
74 	     int pri __unused, u_short *proto)
75 {
76   int type;
77   struct ip *pip;
78   u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
79 
80   bp = m_pullup(bp);
81   pip = (struct ip *)MBUF_CTOP(bp);
82   if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP &&
83       cproto == PROTO_VJCOMP) {
84     type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc,
85                            &bundle->ncp.ipcp.vj.slstat,
86                            bundle->ncp.ipcp.peer_compproto & 0xff);
87     log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type);
88     switch (type) {
89     case TYPE_IP:
90       break;
91 
92     case TYPE_UNCOMPRESSED_TCP:
93       *proto = PROTO_VJUNCOMP;
94       log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
95       m_settype(bp, MB_VJOUT);
96       break;
97 
98     case TYPE_COMPRESSED_TCP:
99       *proto = PROTO_VJCOMP;
100       log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
101       m_settype(bp, MB_VJOUT);
102       break;
103 
104     default:
105       log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
106       m_freem(bp);
107       return NULL;
108     }
109   }
110 
111   return bp;
112 }
113 
114 static struct mbuf *
115 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
116 {
117   u_char *bufp;
118   int len, olen, rlen;
119   u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
120 
121   bp = m_pullup(bp);
122   olen = len = m_length(bp);
123   if (type == TYPE_UNCOMPRESSED_TCP) {
124     /*
125      * Uncompressed packet does NOT change its size, so that we can use mbuf
126      * space for uncompression job.
127      */
128     bufp = MBUF_CTOP(bp);
129     len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
130                             (ipcp->my_compproto >> 8) & 255);
131     if (len <= 0) {
132       m_freem(bp);
133       bp = NULL;
134     } else
135       m_settype(bp, MB_VJIN);
136     return bp;
137   }
138 
139   /*
140    * Handle compressed packet. 1) Read up to MAX_VJHEADER bytes into work
141    * space. 2) Try to uncompress it. 3) Compute amount of necessary space. 4)
142    * Copy unread data info there.
143    */
144   if (len > MAX_VJHEADER)
145     len = MAX_VJHEADER;
146   rlen = len;
147   bufp = work + MAX_HDR;
148   bp = mbuf_Read(bp, bufp, rlen);
149   len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
150                           (ipcp->my_compproto >> 8) & 255);
151   if (len <= 0) {
152     m_freem(bp);
153     return NULL;
154   }
155   len -= olen;
156   len += rlen;
157 
158   bp = m_prepend(bp, bufp, len, 0);
159   m_settype(bp, MB_VJIN);
160 
161   return bp;
162 }
163 
164 static struct mbuf *
165 vj_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
166              u_short *proto)
167 {
168   u_char type;
169 
170   switch (*proto) {
171   case PROTO_VJCOMP:
172     type = TYPE_COMPRESSED_TCP;
173     log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n");
174     break;
175   case PROTO_VJUNCOMP:
176     type = TYPE_UNCOMPRESSED_TCP;
177     log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n");
178     break;
179   default:
180     return bp;
181   }
182 
183   *proto = PROTO_IP;
184   return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
185 }
186 
187 const char *
188 vj2asc(u_int32_t val)
189 {
190   static char asc[50];		/* The return value is used immediately */
191 
192   if (val)
193     snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
194             (int)((val>>8)&15)+1, val & 1 ?  "" : "out");
195   else
196     strcpy(asc, "VJ disabled");
197   return asc;
198 }
199 
200 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };
201