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