2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <sys/socket.h>
41 #include <string.h> /* strlen/memcpy */
50 #include "slcompress.h"
55 #include "throughput.h"
62 #include "descriptor.h"
72 #define MAX_VJHEADER 16 /* Maximum size of compressed header */
75 vj_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
76 int pri __unused, u_short *proto)
80 u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
83 pip = (struct ip *)MBUF_CTOP(bp);
84 if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP &&
85 cproto == PROTO_VJCOMP) {
86 type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc,
87 &bundle->ncp.ipcp.vj.slstat,
88 bundle->ncp.ipcp.peer_compproto & 0xff);
89 log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type);
94 case TYPE_UNCOMPRESSED_TCP:
95 *proto = PROTO_VJUNCOMP;
96 log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
97 m_settype(bp, MB_VJOUT);
100 case TYPE_COMPRESSED_TCP:
101 *proto = PROTO_VJCOMP;
102 log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
103 m_settype(bp, MB_VJOUT);
107 log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
117 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
121 u_char work[MAX_HDR + MAX_VJHEADER]; /* enough to hold TCP/IP header */
124 olen = len = m_length(bp);
125 if (type == TYPE_UNCOMPRESSED_TCP) {
127 * Uncompressed packet does NOT change its size, so that we can use mbuf
128 * space for uncompression job.
130 bufp = MBUF_CTOP(bp);
131 len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
132 (ipcp->my_compproto >> 8) & 255);
137 m_settype(bp, MB_VJIN);
142 * Handle compressed packet. 1) Read up to MAX_VJHEADER bytes into work
143 * space. 2) Try to uncompress it. 3) Compute amount of necessary space. 4)
144 * Copy unread data info there.
146 if (len > MAX_VJHEADER)
149 bufp = work + MAX_HDR;
150 bp = mbuf_Read(bp, bufp, rlen);
151 len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
152 (ipcp->my_compproto >> 8) & 255);
160 bp = m_prepend(bp, bufp, len, 0);
161 m_settype(bp, MB_VJIN);
167 vj_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp,
174 type = TYPE_COMPRESSED_TCP;
175 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n");
178 type = TYPE_UNCOMPRESSED_TCP;
179 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n");
186 return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
190 vj2asc(u_int32_t val)
192 static char asc[50]; /* The return value is used immediately */
195 snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
196 (int)((val>>8)&15)+1, val & 1 ? "" : "out");
198 strcpy(asc, "VJ disabled");
202 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };