Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / usr.sbin / ppp / vjcomp.c
blobc62b82fc8d62c80e3a1ca2369d197c1bbbe584d6
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.
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.
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.
28 * $FreeBSD: src/usr.sbin/ppp/vjcomp.c,v 1.35.2.2 2002/09/01 02:12:32 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/vjcomp.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
32 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
39 #include <stdio.h>
40 #include <string.h> /* strlen/memcpy */
41 #include <termios.h>
43 #include "layer.h"
44 #include "mbuf.h"
45 #include "log.h"
46 #include "timer.h"
47 #include "fsm.h"
48 #include "proto.h"
49 #include "slcompress.h"
50 #include "lqr.h"
51 #include "hdlc.h"
52 #include "defs.h"
53 #include "iplist.h"
54 #include "throughput.h"
55 #include "ncpaddr.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"
71 #define MAX_VJHEADER 16 /* Maximum size of compressed header */
73 static struct mbuf *
74 vj_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp, int pri,
75 u_short *proto)
77 int type;
78 struct ip *pip;
79 u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
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;
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;
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;
105 default:
106 log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
107 m_freem(bp);
108 return NULL;
112 return bp;
115 static struct mbuf *
116 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
118 u_char *bufp;
119 int len, olen, rlen;
120 u_char work[MAX_HDR + MAX_VJHEADER]; /* enough to hold TCP/IP header */
122 bp = m_pullup(bp);
123 olen = len = m_length(bp);
124 if (type == TYPE_UNCOMPRESSED_TCP) {
126 * Uncompressed packet does NOT change its size, so that we can use mbuf
127 * space for uncompression job.
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;
141 * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
142 * space. 2) Try to uncompress it. 3) Compute amount of necessary space. 4)
143 * Copy unread data info there.
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;
156 len -= olen;
157 len += rlen;
159 bp = m_prepend(bp, bufp, len, 0);
160 m_settype(bp, MB_VJIN);
162 return bp;
165 static struct mbuf *
166 vj_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
167 u_short *proto)
169 u_char type;
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;
184 *proto = PROTO_IP;
185 return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
188 const char *
189 vj2asc(u_int32_t val)
191 static char asc[50]; /* The return value is used immediately */
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;
201 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };