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)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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>
39 #include <string.h> /* strlen/memcpy */
48 #include "slcompress.h"
53 #include "throughput.h"
60 #include "descriptor.h"
70 #define MAX_VJHEADER 16 /* Maximum size of compressed header */
73 vj_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
74 int pri __unused, u_short *proto)
78 u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
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);
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);
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);
105 log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
115 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
119 u_char work[MAX_HDR + MAX_VJHEADER]; /* enough to hold TCP/IP header */
122 olen = len = m_length(bp);
123 if (type == TYPE_UNCOMPRESSED_TCP) {
125 * Uncompressed packet does NOT change its size, so that we can use mbuf
126 * space for uncompression job.
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);
135 m_settype(bp, MB_VJIN);
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.
144 if (len > MAX_VJHEADER)
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);
158 bp = m_prepend(bp, bufp, len, 0);
159 m_settype(bp, MB_VJIN);
165 vj_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
172 type = TYPE_COMPRESSED_TCP;
173 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n");
176 type = TYPE_UNCOMPRESSED_TCP;
177 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n");
184 return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
188 vj2asc(u_int32_t val)
190 static char asc[50]; /* The return value is used immediately */
193 snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
194 (int)((val>>8)&15)+1, val & 1 ? "" : "out");
196 strcpy(asc, "VJ disabled");
200 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };